reloc_static_pie.c 3.6 KB

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