dynamic_link.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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 OSCAR lab, 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 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 General Public License for more details.
  13. You should have received a copy of the GNU 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. while (dyn->d_tag != DT_NULL) {
  62. if ((d_tag_utype) dyn->d_tag < DT_NUM)
  63. l_info[dyn->d_tag] = dyn;
  64. else if (dyn->d_tag >= DT_LOPROC &&
  65. 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
  71. + DT_VERSIONTAGNUM] = dyn;
  72. else if ((d_tag_utype) DT_VALTAGIDX (dyn->d_tag) < DT_VALNUM)
  73. l_info[DT_VALTAGIDX (dyn->d_tag) + DT_NUM + DT_THISPROCNUM
  74. + DT_VERSIONTAGNUM + DT_EXTRANUM] = dyn;
  75. else if ((d_tag_utype) DT_ADDRTAGIDX (dyn->d_tag) < DT_ADDRNUM)
  76. l_info[DT_ADDRTAGIDX (dyn->d_tag) + DT_NUM + DT_THISPROCNUM
  77. + DT_VERSIONTAGNUM + DT_EXTRANUM + DT_VALNUM] = dyn;
  78. ++dyn;
  79. }
  80. if (l_addr != 0) {
  81. # define ADJUST_DYN_INFO(tag) \
  82. do { \
  83. if (l_info[tag]) \
  84. l_info[tag]->d_un.d_ptr += l_addr; \
  85. } while(0);
  86. ADJUST_DYN_INFO (DT_HASH);
  87. ADJUST_DYN_INFO (DT_PLTGOT);
  88. ADJUST_DYN_INFO (DT_STRTAB);
  89. ADJUST_DYN_INFO (DT_SYMTAB);
  90. # if ! ELF_MACHINE_NO_RELA
  91. ADJUST_DYN_INFO (DT_RELA);
  92. # endif
  93. # if ! ELF_MACHINE_NO_REL
  94. ADJUST_DYN_INFO (DT_REL);
  95. # endif
  96. ADJUST_DYN_INFO (DT_JMPREL);
  97. ADJUST_DYN_INFO (VERSYMIDX (DT_VERSYM));
  98. ADJUST_DYN_INFO (DT_ADDRTAGIDX (DT_GNU_HASH) + DT_NUM + DT_THISPROCNUM
  99. + DT_VERSIONTAGNUM + DT_EXTRANUM + DT_VALNUM);
  100. # undef ADJUST_DYN_INFO
  101. }
  102. /* Then a bunch of assertion, we could kind of ignore them */
  103. if (l_info[DT_PLTREL]) {
  104. #if ELF_MACHINE_NO_RELA
  105. assert (l_info[DT_PLTREL]->d_un.d_val == DT_REL);
  106. #elif ELF_MACHINE_NO_REL
  107. assert (l_info[DT_PLTREL]->d_un.d_val == DT_RELA);
  108. #else
  109. assert (l_info[DT_PLTREL]->d_un.d_val == DT_REL
  110. || l_info[DT_PLTREL]->d_un.d_val == DT_RELA);
  111. #endif
  112. }
  113. #if ! ELF_MACHINE_NO_RELA
  114. if (l_info[DT_RELA])
  115. assert (l_info[DT_RELAENT]->d_un.d_val == sizeof (ElfW(Rela)));
  116. # endif
  117. # if ! ELF_MACHINE_NO_REL
  118. if (l_info[DT_REL])
  119. assert (l_info[DT_RELENT]->d_un.d_val == sizeof (ElfW(Rel)));
  120. #endif
  121. #ifdef RTLD_BOOTSTRAP
  122. /* Only the bind now flags are allowed. */
  123. assert (!l_info[VERSYMIDX (DT_FLAGS_1)]
  124. || l_info[VERSYMIDX (DT_FLAGS_1)]->d_un.d_val == DF_1_NOW);
  125. assert (!l_info[DT_FLAGS]
  126. || l_info[DT_FLAGS]->d_un.d_val == DF_BIND_NOW);
  127. /* Flags must not be set for ld.so. */
  128. assert (!l_info[DT_RUNPATH]);
  129. assert (!l_info[DT_RPATH]);
  130. #endif
  131. }
  132. #ifdef RTLD_BOOTSTRAP
  133. # define ELF_DURING_STARTUP (1)
  134. #else
  135. # define ELF_DURING_STARTUP (0)
  136. #endif
  137. /* Get the definitions of `elf_dynamic_do_rel' and `elf_dynamic_do_rela'.
  138. These functions are almost identical, so we use cpp magic to avoid
  139. duplicating their code. It cannot be done in a more general function
  140. because we must be able to completely inline. */
  141. /* On some machines, notably SPARC, DT_REL* includes DT_JMPREL in its
  142. range. Note that according to the ELF spec, this is completely legal!
  143. But conditionally define things so that on machines we know this will
  144. not happen we do something more optimal. */
  145. #ifdef ELF_MACHINE_PLTREL_OVERLAP
  146. /* ELF_MACHINE_PLTREL_OVERLAP is only used for s390, powerpc and sparc.
  147. We will keep it for now */
  148. static void
  149. _elf_dynamic_do_reloc(struct link_map *l, int dt_reloc, int dt_reloc_sz,
  150. void (*do_reloc) (struct link_map *, ElfW(Addr), int))
  151. {
  152. struct { ElfW(Addr) start, size; } ranges[3];
  153. int ranges_index;
  154. ranges[0].size = ranges[1].size = ranges[2].size = 0;
  155. if (l->l_info[dt_reloc]) {
  156. ranges[0].start = D_PTR (l->l_info[dt_reloc]);
  157. ranges[0].size = l->l_info[dt_reloc_sz]->d_un.d_val;
  158. }
  159. for (ranges_index = 0; ranges_index < 3; ++ranges_index)
  160. (*do_reloc) (l,
  161. ranges[ranges_index].start,
  162. ranges[ranges_index].size);
  163. }
  164. #else
  165. /* Now this part is for our x86s machines */
  166. static void __attribute_unused
  167. _elf_dynamic_do_reloc(struct link_map * l, int dt_reloc, int dt_reloc_sz,
  168. void (*do_reloc) (struct link_map *, ElfW(Addr), int))
  169. {
  170. struct { ElfW(Addr) start, size; } ranges[2];
  171. ranges[0].size = ranges[1].size = 0;
  172. ranges[0].start = ranges[1].start = 0;
  173. if (l->l_info[dt_reloc]) {
  174. ranges[0].start = D_PTR (l->l_info[dt_reloc]);
  175. ranges[0].size = l->l_info[dt_reloc_sz]->d_un.d_val;
  176. }
  177. if (l->l_info[DT_PLTREL]
  178. && l->l_info[DT_PLTREL]->d_un.d_val == dt_reloc) {
  179. ElfW(Addr) start = D_PTR (l->l_info[DT_JMPREL]);
  180. if (!ELF_DURING_STARTUP &&
  181. /* This test does not only detect whether the relocation
  182. sections are in the right order, it also checks whether
  183. there is a DT_REL/DT_RELA section. */
  184. ranges[0].start + ranges[0].size != start) {
  185. ranges[1].start = start;
  186. ranges[1].size = l->l_info[DT_PLTRELSZ]->d_un.d_val;
  187. } else {
  188. /* Combine processing the sections. */
  189. assert (ranges[0].start + ranges[0].size == start);
  190. ranges[0].size += l->l_info[DT_PLTRELSZ]->d_un.d_val;
  191. }
  192. }
  193. /* This is interesting, don't make it lazy. */
  194. if (ELF_DURING_STARTUP) {
  195. (*do_reloc) (l, ranges[0].start, ranges[0].size);
  196. } else {
  197. int ranges_index;
  198. for (ranges_index = 0; ranges_index < 2; ++ranges_index)
  199. (*do_reloc) (l,
  200. ranges[ranges_index].start,
  201. ranges[ranges_index].size);
  202. }
  203. }
  204. #endif
  205. #define _ELF_DYNAMIC_DO_RELOC(RELOC, reloc, l) \
  206. _elf_dynamic_do_reloc(l, DT_##RELOC, DT_##RELOC##SZ, \
  207. &elf_dynamic_do_##reloc)
  208. #if ELF_MACHINE_NO_REL || ELF_MACHINE_NO_RELA
  209. # define _ELF_CHECK_REL 0
  210. #else
  211. # define _ELF_CHECK_REL 1
  212. #endif
  213. #if ! ELF_MACHINE_NO_REL
  214. # include "do-rel.h"
  215. # define ELF_DYNAMIC_DO_REL(l) _ELF_DYNAMIC_DO_RELOC (REL, rel, l)
  216. #else
  217. /* nothing to do */
  218. # define ELF_DYNAMIC_DO_REL(l)
  219. #endif
  220. #if ! ELF_MACHINE_NO_RELA
  221. # define DO_RELA
  222. # include "do-rel.h"
  223. # define ELF_DYNAMIC_DO_RELA(l) _ELF_DYNAMIC_DO_RELOC (RELA, rela, l)
  224. #else
  225. /* nothing to do */
  226. # define ELF_DYNAMIC_DO_RELA(l)
  227. #endif
  228. /* This can't just be an inline function because GCC is too dumb
  229. to inline functions containing inlines themselves. */
  230. # define ELF_DYNAMIC_RELOCATE(l) \
  231. do { \
  232. ELF_DYNAMIC_DO_REL (l); \
  233. ELF_DYNAMIC_DO_RELA(l); \
  234. } while (0)