reloc_static_pie.c 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. /* Support for relocating static PIE.
  2. Copyright (C) 2017-2022 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <https://www.gnu.org/licenses/>. */
  15. #include <link.h>
  16. #include <elf.h>
  17. #include <dl-elf.h>
  18. ElfW(Addr) _dl_load_base = NULL;
  19. void
  20. reloc_static_pie (ElfW(Addr) load_addr);
  21. void
  22. reloc_static_pie (ElfW(Addr) load_addr)
  23. {
  24. ElfW(Word) relative_count = 0;
  25. ElfW(Addr) rel_addr = 0;
  26. ElfW(Dyn) * dyn_addr = NULL;
  27. unsigned long dynamic_info[DYNAMIC_SIZE] = {0};
  28. /* Read our own dynamic section and fill in the info array. */
  29. dyn_addr = ((void *) load_addr + elf_machine_dynamic ());
  30. /* Use the underlying function to avoid TLS access before initialization */
  31. __dl_parse_dynamic_info(dyn_addr, dynamic_info, NULL, load_addr);
  32. /* Perform relocations */
  33. relative_count = dynamic_info[DT_RELCONT_IDX];
  34. rel_addr = dynamic_info[DT_RELOC_TABLE_ADDR];
  35. elf_machine_relative(load_addr, rel_addr, relative_count);
  36. _dl_load_base = load_addr;
  37. }