reloc_static_pie.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. #define IS_IN_rtld // force inline function calls
  16. #include <link.h>
  17. #include <elf.h>
  18. #include <dl-elf.h>
  19. #include <ldso.h>
  20. #if defined(__m68k__) || defined(__mips__) || defined(__xtensa__)
  21. #include <dl-startup.h>
  22. #endif
  23. extern ElfW(Addr) _dl_load_base;
  24. void
  25. reloc_static_pie (ElfW(Addr) load_addr);
  26. void
  27. reloc_static_pie(ElfW(Addr) load_addr)
  28. {
  29. int indx;
  30. ElfW(Addr) got;
  31. ElfW(Dyn) *dpnt;
  32. struct elf_resolve tpnt_tmp;
  33. struct elf_resolve *tpnt = &tpnt_tmp;
  34. DL_BOOT_COMPUTE_GOT(got);
  35. DL_BOOT_COMPUTE_DYN(dpnt, got, (DL_LOADADDR_TYPE)load_addr);
  36. _dl_memset(tpnt, 0, sizeof(struct elf_resolve));
  37. tpnt->loadaddr = load_addr;
  38. tpnt->dynamic_addr = dpnt;
  39. __dl_parse_dynamic_info(dpnt, tpnt->dynamic_info, NULL, load_addr);
  40. #if defined(PERFORM_BOOTSTRAP_GOT)
  41. /* some arches (like MIPS) we have to tweak the GOT before relocations */
  42. PERFORM_BOOTSTRAP_GOT(tpnt);
  43. #endif
  44. #if !defined(__FDPIC__)
  45. DL_RELOCATE_RELR(tpnt);
  46. #endif
  47. #if defined(ELF_MACHINE_PLTREL_OVERLAP)
  48. # define INDX_MAX 1
  49. #else
  50. # define INDX_MAX 2
  51. #endif
  52. for (indx = 0; indx < INDX_MAX; indx++) {
  53. unsigned long rel_addr, rel_size;
  54. ElfW(Word) relative_count = tpnt->dynamic_info[DT_RELCONT_IDX];
  55. rel_addr = (indx ? tpnt->dynamic_info[DT_JMPREL] :
  56. tpnt->dynamic_info[DT_RELOC_TABLE_ADDR]);
  57. rel_size = (indx ? tpnt->dynamic_info[DT_PLTRELSZ] :
  58. tpnt->dynamic_info[DT_RELOC_TABLE_SIZE]);
  59. if (!rel_addr)
  60. continue;
  61. if((0 == indx) && relative_count) {
  62. rel_size -= relative_count * sizeof(ELF_RELOC);
  63. elf_machine_relative(load_addr, rel_addr, relative_count);
  64. rel_addr += relative_count * sizeof(ELF_RELOC);
  65. }
  66. #ifdef ARCH_NEEDS_BOOTSTRAP_RELOCS
  67. {
  68. ELF_RELOC *rpnt;
  69. unsigned int i;
  70. ElfW(Sym) *sym;
  71. unsigned long symbol_addr;
  72. int symtab_index;
  73. unsigned long *reloc_addr;
  74. /* Now parse the relocation information */
  75. rpnt = (ELF_RELOC *) rel_addr;
  76. for (i = 0; i < rel_size; i += sizeof(ELF_RELOC), rpnt++) {
  77. reloc_addr = (unsigned long *) DL_RELOC_ADDR(load_addr, (unsigned long)rpnt->r_offset);
  78. symtab_index = ELF_R_SYM(rpnt->r_info);
  79. symbol_addr = 0;
  80. sym = NULL;
  81. if (symtab_index) {
  82. ElfW(Sym) *symtab;
  83. symtab = (ElfW(Sym) *) tpnt->dynamic_info[DT_SYMTAB];
  84. sym = &symtab[symtab_index];
  85. symbol_addr = (unsigned long) DL_RELOC_ADDR(load_addr, sym->st_value);
  86. }
  87. /* Use this machine-specific macro to perform the actual relocation. */
  88. PERFORM_BOOTSTRAP_RELOC(rpnt, reloc_addr, symbol_addr, load_addr, sym);
  89. }
  90. }
  91. #endif
  92. }
  93. _dl_load_base = load_addr;
  94. }