reloc_static_pie.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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(__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(ELF_MACHINE_PLTREL_OVERLAP)
  45. # define INDX_MAX 1
  46. #else
  47. # define INDX_MAX 2
  48. #endif
  49. for (indx = 0; indx < INDX_MAX; indx++) {
  50. unsigned long rel_addr, rel_size;
  51. ElfW(Word) relative_count = tpnt->dynamic_info[DT_RELCONT_IDX];
  52. rel_addr = (indx ? tpnt->dynamic_info[DT_JMPREL] :
  53. tpnt->dynamic_info[DT_RELOC_TABLE_ADDR]);
  54. rel_size = (indx ? tpnt->dynamic_info[DT_PLTRELSZ] :
  55. tpnt->dynamic_info[DT_RELOC_TABLE_SIZE]);
  56. if (!rel_addr)
  57. continue;
  58. if((0 == indx) && relative_count) {
  59. rel_size -= relative_count * sizeof(ELF_RELOC);
  60. elf_machine_relative(load_addr, rel_addr, relative_count);
  61. rel_addr += relative_count * sizeof(ELF_RELOC);
  62. }
  63. #ifdef ARCH_NEEDS_BOOTSTRAP_RELOCS
  64. {
  65. ELF_RELOC *rpnt;
  66. unsigned int i;
  67. ElfW(Sym) *sym;
  68. unsigned long symbol_addr;
  69. int symtab_index;
  70. unsigned long *reloc_addr;
  71. /* Now parse the relocation information */
  72. rpnt = (ELF_RELOC *) rel_addr;
  73. for (i = 0; i < rel_size; i += sizeof(ELF_RELOC), rpnt++) {
  74. reloc_addr = (unsigned long *) DL_RELOC_ADDR(load_addr, (unsigned long)rpnt->r_offset);
  75. symtab_index = ELF_R_SYM(rpnt->r_info);
  76. symbol_addr = 0;
  77. sym = NULL;
  78. if (symtab_index) {
  79. ElfW(Sym) *symtab;
  80. symtab = (ElfW(Sym) *) tpnt->dynamic_info[DT_SYMTAB];
  81. sym = &symtab[symtab_index];
  82. symbol_addr = (unsigned long) DL_RELOC_ADDR(load_addr, sym->st_value);
  83. }
  84. /* Use this machine-specific macro to perform the actual relocation. */
  85. PERFORM_BOOTSTRAP_RELOC(rpnt, reloc_addr, symbol_addr, load_addr, sym);
  86. }
  87. }
  88. #endif
  89. }
  90. _dl_load_base = load_addr;
  91. }