dynamic_link.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /* Copyright (C) 2014 Stony Brook University
  2. This file is part of Graphene Library OS.
  3. Graphene Library OS is free software: you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public License
  5. as published by the Free Software Foundation, either version 3 of the
  6. License, or (at your option) any later version.
  7. Graphene Library OS is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  13. /*
  14. * dynamic_link.h
  15. *
  16. * This files contain inline functions for dynamic linking.
  17. * The source code is imported and modified from the GNU C Library.
  18. */
  19. #include <dl-machine-x86_64.h>
  20. #include <elf/elf.h>
  21. #include <pal_internal.h>
  22. #include <pal_rtld.h>
  23. /* We pass reloc_addr as a pointer to void, as opposed to a pointer to
  24. ElfW(Addr), because not all architectures can assume that the
  25. relocated address is properly aligned, whereas the compiler is
  26. entitled to assume that a pointer to a type is properly aligned for
  27. the type. Even if we cast the pointer back to some other type with
  28. less strict alignment requirements, the compiler might still
  29. remember that the pointer was originally more aligned, thereby
  30. optimizing away alignment tests or using word instructions for
  31. copying memory, breaking the very code written to handle the
  32. unaligned cases. */
  33. #if !ELF_MACHINE_NO_REL
  34. static inline void __attribute_always_inline elf_machine_rel(struct link_map* l, ElfW(Rel)* reloc,
  35. ElfW(Sym)* sym,
  36. void* const reloc_addr);
  37. static inline void __attribute_always_inline elf_machine_rel_relative(struct link_map* l,
  38. const ElfW(Rel)* reloc,
  39. void* const reloc_addr);
  40. #endif
  41. #if !ELF_MACHINE_NO_RELA
  42. static inline void __attribute_always_inline elf_machine_rela(struct link_map* l,
  43. ElfW(Rela)* reloc, ElfW(Sym)* sym,
  44. void* const reloc_addr);
  45. static inline void __attribute_always_inline elf_machine_rela_relative(struct link_map* l,
  46. const ElfW(Rela)* reloc,
  47. void* const reloc_addr);
  48. #endif
  49. /* Read the dynamic section at DYN and fill in INFO with indices DT_*. */
  50. static inline void __attribute_unused __attribute_always_inline
  51. elf_get_dynamic_info(ElfW(Dyn)* dyn, ElfW(Dyn)** l_info, ElfW(Addr) l_addr) {
  52. #if __ELF_NATIVE_CLASS == 32
  53. typedef Elf32_Word d_tag_utype;
  54. #elif __ELF_NATIVE_CLASS == 64
  55. typedef Elf64_Xword d_tag_utype;
  56. #endif
  57. #ifndef RTLD_BOOTSTRAP
  58. if (dyn == NULL)
  59. return;
  60. #endif
  61. while (dyn->d_tag != DT_NULL) {
  62. d_tag_utype dt_extranum = DT_EXTRANUM;
  63. if ((d_tag_utype)dyn->d_tag < DT_NUM)
  64. l_info[dyn->d_tag] = dyn;
  65. else if (dyn->d_tag >= DT_LOPROC && dyn->d_tag < DT_LOPROC + DT_THISPROCNUM)
  66. l_info[dyn->d_tag - DT_LOPROC + DT_NUM] = dyn;
  67. else if ((d_tag_utype)DT_VERSIONTAGIDX(dyn->d_tag) < DT_VERSIONTAGNUM)
  68. l_info[VERSYMIDX(dyn->d_tag)] = dyn;
  69. else if ((d_tag_utype)DT_EXTRATAGIDX(dyn->d_tag) < dt_extranum)
  70. l_info[DT_EXTRATAGIDX(dyn->d_tag) + DT_NUM + DT_THISPROCNUM + DT_VERSIONTAGNUM] = dyn;
  71. else if ((d_tag_utype)DT_VALTAGIDX(dyn->d_tag) < DT_VALNUM)
  72. l_info[DT_VALTAGIDX(dyn->d_tag) + DT_NUM + DT_THISPROCNUM + DT_VERSIONTAGNUM +
  73. DT_EXTRANUM] = dyn;
  74. else if ((d_tag_utype)DT_ADDRTAGIDX(dyn->d_tag) < DT_ADDRNUM)
  75. l_info[DT_ADDRTAGIDX(dyn->d_tag) + DT_NUM + DT_THISPROCNUM + DT_VERSIONTAGNUM +
  76. DT_EXTRANUM + DT_VALNUM] = dyn;
  77. ++dyn;
  78. }
  79. if (l_addr != 0) {
  80. #define ADJUST_DYN_INFO(tag) \
  81. do { \
  82. if (l_info[tag]) \
  83. l_info[tag]->d_un.d_ptr += l_addr; \
  84. } while (0);
  85. ADJUST_DYN_INFO(DT_HASH);
  86. ADJUST_DYN_INFO(DT_PLTGOT);
  87. ADJUST_DYN_INFO(DT_STRTAB);
  88. ADJUST_DYN_INFO(DT_SYMTAB);
  89. #if !ELF_MACHINE_NO_RELA
  90. ADJUST_DYN_INFO(DT_RELA);
  91. #endif
  92. #if !ELF_MACHINE_NO_REL
  93. ADJUST_DYN_INFO(DT_REL);
  94. #endif
  95. ADJUST_DYN_INFO(DT_JMPREL);
  96. ADJUST_DYN_INFO(VERSYMIDX(DT_VERSYM));
  97. ADJUST_DYN_INFO(DT_ADDRTAGIDX(DT_GNU_HASH) + DT_NUM + DT_THISPROCNUM + DT_VERSIONTAGNUM +
  98. DT_EXTRANUM + DT_VALNUM);
  99. #undef ADJUST_DYN_INFO
  100. }
  101. /* Then a bunch of assertion, we could kind of ignore them */
  102. if (l_info[DT_PLTREL]) {
  103. #if ELF_MACHINE_NO_RELA
  104. assert(l_info[DT_PLTREL]->d_un.d_val == DT_REL);
  105. #elif ELF_MACHINE_NO_REL
  106. assert(l_info[DT_PLTREL]->d_un.d_val == DT_RELA);
  107. #else
  108. assert(l_info[DT_PLTREL]->d_un.d_val == DT_REL || l_info[DT_PLTREL]->d_un.d_val == DT_RELA);
  109. #endif
  110. }
  111. #if !ELF_MACHINE_NO_RELA
  112. if (l_info[DT_RELA])
  113. assert(l_info[DT_RELAENT]->d_un.d_val == sizeof(ElfW(Rela)));
  114. #endif
  115. #if !ELF_MACHINE_NO_REL
  116. if (l_info[DT_REL])
  117. assert(l_info[DT_RELENT]->d_un.d_val == sizeof(ElfW(Rel)));
  118. #endif
  119. #ifdef RTLD_BOOTSTRAP
  120. /* Only the bind now flags are allowed. */
  121. assert(!l_info[VERSYMIDX(DT_FLAGS_1)] || l_info[VERSYMIDX(DT_FLAGS_1)]->d_un.d_val == DF_1_NOW);
  122. assert(!l_info[DT_FLAGS] || l_info[DT_FLAGS]->d_un.d_val == DF_BIND_NOW);
  123. /* Flags must not be set for ld.so. */
  124. assert(!l_info[DT_RUNPATH]);
  125. assert(!l_info[DT_RPATH]);
  126. #endif
  127. }
  128. #ifdef RTLD_BOOTSTRAP
  129. #define ELF_DURING_STARTUP (1)
  130. #else
  131. #define ELF_DURING_STARTUP (0)
  132. #endif
  133. /* Get the definitions of `elf_dynamic_do_rel' and `elf_dynamic_do_rela'.
  134. These functions are almost identical, so we use cpp magic to avoid
  135. duplicating their code. It cannot be done in a more general function
  136. because we must be able to completely inline. */
  137. /* On some machines, notably SPARC, DT_REL* includes DT_JMPREL in its
  138. range. Note that according to the ELF spec, this is completely legal!
  139. But conditionally define things so that on machines we know this will
  140. not happen we do something more optimal. */
  141. #ifdef ELF_MACHINE_PLTREL_OVERLAP
  142. /* ELF_MACHINE_PLTREL_OVERLAP is only used for s390, powerpc and sparc.
  143. We will keep it for now */
  144. static void _elf_dynamic_do_reloc(struct link_map* l, int dt_reloc, int dt_reloc_sz,
  145. void (*do_reloc)(struct link_map*, ElfW(Addr), int)) {
  146. struct {
  147. ElfW(Addr) start, size;
  148. } ranges[3];
  149. int ranges_index;
  150. ranges[0].size = ranges[1].size = ranges[2].size = 0;
  151. if (l->l_info[dt_reloc]) {
  152. ranges[0].start = D_PTR(l->l_info[dt_reloc]);
  153. ranges[0].size = l->l_info[dt_reloc_sz]->d_un.d_val;
  154. }
  155. for (ranges_index = 0; ranges_index < 3; ++ranges_index)
  156. (*do_reloc)(l, ranges[ranges_index].start, ranges[ranges_index].size);
  157. }
  158. #else
  159. /* Now this part is for our x86s machines */
  160. static void __attribute_unused _elf_dynamic_do_reloc(struct link_map* l, uint64_t dt_reloc,
  161. int dt_reloc_sz,
  162. void (*do_reloc)(struct link_map*, ElfW(Addr),
  163. int)) {
  164. struct {
  165. ElfW(Addr) start, size;
  166. } ranges[2];
  167. ranges[0].size = ranges[1].size = 0;
  168. ranges[0].start = ranges[1].start = 0;
  169. if (l->l_info[dt_reloc]) {
  170. ranges[0].start = D_PTR(l->l_info[dt_reloc]);
  171. ranges[0].size = l->l_info[dt_reloc_sz]->d_un.d_val;
  172. }
  173. if (l->l_info[DT_PLTREL] && l->l_info[DT_PLTREL]->d_un.d_val == dt_reloc) {
  174. ElfW(Addr) start = D_PTR(l->l_info[DT_JMPREL]);
  175. if (!ELF_DURING_STARTUP &&
  176. /* This test does not only detect whether the relocation
  177. sections are in the right order, it also checks whether
  178. there is a DT_REL/DT_RELA section. */
  179. ranges[0].start + ranges[0].size != start) {
  180. ranges[1].start = start;
  181. ranges[1].size = l->l_info[DT_PLTRELSZ]->d_un.d_val;
  182. } else {
  183. /* Combine processing the sections. */
  184. assert(ranges[0].start + ranges[0].size == start);
  185. ranges[0].size += l->l_info[DT_PLTRELSZ]->d_un.d_val;
  186. }
  187. }
  188. /* This is interesting, don't make it lazy. */
  189. if (ELF_DURING_STARTUP) {
  190. (*do_reloc)(l, ranges[0].start, ranges[0].size);
  191. } else {
  192. int ranges_index;
  193. for (ranges_index = 0; ranges_index < 2; ++ranges_index)
  194. (*do_reloc)(l, ranges[ranges_index].start, ranges[ranges_index].size);
  195. }
  196. }
  197. #endif
  198. #define _ELF_DYNAMIC_DO_RELOC(RELOC, reloc, l) \
  199. _elf_dynamic_do_reloc(l, DT_##RELOC, DT_##RELOC##SZ, &elf_dynamic_do_##reloc)
  200. #if ELF_MACHINE_NO_REL || ELF_MACHINE_NO_RELA
  201. #define _ELF_CHECK_REL 0
  202. #else
  203. #define _ELF_CHECK_REL 1
  204. #endif
  205. #if !ELF_MACHINE_NO_REL
  206. #include "do-rel.h"
  207. #define ELF_DYNAMIC_DO_REL(l) _ELF_DYNAMIC_DO_RELOC(REL, rel, l)
  208. #else
  209. /* nothing to do */
  210. #define ELF_DYNAMIC_DO_REL(l)
  211. #endif
  212. #if !ELF_MACHINE_NO_RELA
  213. #define DO_RELA
  214. #include "do-rel.h"
  215. #define ELF_DYNAMIC_DO_RELA(l) _ELF_DYNAMIC_DO_RELOC(RELA, rela, l)
  216. #else
  217. /* nothing to do */
  218. #define ELF_DYNAMIC_DO_RELA(l)
  219. #endif
  220. /* This can't just be an inline function because GCC is too dumb
  221. to inline functions containing inlines themselves. */
  222. #define ELF_DYNAMIC_RELOCATE(l) \
  223. do { \
  224. ELF_DYNAMIC_DO_REL(l); \
  225. ELF_DYNAMIC_DO_RELA(l); \
  226. } while (0)