dynamic_link.h 9.8 KB

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