dl-elf.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Copyright (C) 2000-2005 by Erik Andersen <andersen@codepoet.org>
  4. *
  5. * GNU Lesser General Public License version 2.1 or later.
  6. */
  7. #ifndef _DL_ELF_H
  8. #define _DL_ELF_H
  9. #include <features.h>
  10. #include <bits/wordsize.h>
  11. #include <dl-string.h> /* before elf.h to get ELF_USES_RELOCA right */
  12. #include <elf.h>
  13. #include <link.h>
  14. #include <dl-defs.h>
  15. #include <dlfcn.h>
  16. /* Forward declarations for stuff defined in dl-hash.h */
  17. struct dyn_elf;
  18. struct elf_resolve;
  19. struct r_scope_elem;
  20. #ifdef __LDSO_CACHE_SUPPORT__
  21. extern int _dl_map_cache(void);
  22. extern int _dl_unmap_cache(void);
  23. #else
  24. static __inline__ void _dl_map_cache(void) { }
  25. static __inline__ void _dl_unmap_cache(void) { }
  26. #endif
  27. #define DL_RESOLVE_SECURE 0x0001
  28. #define DL_RESOLVE_NOLOAD 0x0002
  29. /* Function prototypes for non-static stuff in elfinterp.c */
  30. extern void _dl_parse_lazy_relocation_information(struct dyn_elf *rpnt,
  31. unsigned long rel_addr, unsigned long rel_size);
  32. extern int _dl_parse_relocation_information(struct dyn_elf *rpnt,
  33. struct r_scope_elem *scope, unsigned long rel_addr, unsigned long rel_size);
  34. extern struct elf_resolve * _dl_load_shared_library(unsigned rflags,
  35. struct dyn_elf **rpnt, struct elf_resolve *tpnt, char *full_libname,
  36. int trace_loaded_objects);
  37. extern struct elf_resolve * _dl_load_elf_shared_library(unsigned rflags,
  38. struct dyn_elf **rpnt, const char *libname);
  39. extern int _dl_linux_resolve(void);
  40. extern int _dl_fixup(struct dyn_elf *rpnt, struct r_scope_elem *scope, int flag);
  41. extern void _dl_protect_relro (struct elf_resolve *l);
  42. /*
  43. * Bitsize related settings for things ElfW()
  44. * does not handle already
  45. */
  46. #if __WORDSIZE == 64
  47. # define ELF_ST_BIND(val) ELF64_ST_BIND(val)
  48. # define ELF_ST_TYPE(val) ELF64_ST_TYPE(val)
  49. # define ELF_R_SYM(i) ELF64_R_SYM(i)
  50. # define ELF_R_TYPE(i) ELF64_R_TYPE(i)
  51. # ifndef ELF_CLASS
  52. # define ELF_CLASS ELFCLASS64
  53. # endif
  54. #else
  55. # define ELF_ST_BIND(val) ELF32_ST_BIND(val)
  56. # define ELF_ST_TYPE(val) ELF32_ST_TYPE(val)
  57. # define ELF_R_SYM(i) ELF32_R_SYM(i)
  58. # define ELF_R_TYPE(i) ELF32_R_TYPE(i)
  59. # ifndef ELF_CLASS
  60. # define ELF_CLASS ELFCLASS32
  61. # endif
  62. #endif
  63. /*
  64. * Datatype of a relocation on this platform
  65. */
  66. #ifdef ELF_USES_RELOCA
  67. # define ELF_RELOC ElfW(Rela)
  68. # define DT_RELOC_TABLE_ADDR DT_RELA
  69. # define DT_RELOC_TABLE_SIZE DT_RELASZ
  70. # define DT_RELOCCOUNT DT_RELACOUNT
  71. # define UNSUPPORTED_RELOC_TYPE DT_REL
  72. # define UNSUPPORTED_RELOC_STR "REL"
  73. #else
  74. # define ELF_RELOC ElfW(Rel)
  75. # define DT_RELOC_TABLE_ADDR DT_REL
  76. # define DT_RELOC_TABLE_SIZE DT_RELSZ
  77. # define DT_RELOCCOUNT DT_RELCOUNT
  78. # define UNSUPPORTED_RELOC_TYPE DT_RELA
  79. # define UNSUPPORTED_RELOC_STR "RELA"
  80. #endif
  81. /* OS and/or GNU dynamic extensions */
  82. #define OS_NUM_BASE 1 /* for DT_RELOCCOUNT */
  83. #ifdef __LDSO_GNU_HASH_SUPPORT__
  84. # define OS_NUM_GNU_HASH 1 /* for DT_GNU_HASH entry */
  85. #else
  86. # define OS_NUM_GNU_HASH 0
  87. #endif
  88. #ifdef __LDSO_PRELINK_SUPPORT__
  89. # define OS_NUM_PRELINK 6 /* for DT_GNU_PRELINKED entry */
  90. #else
  91. # define OS_NUM_PRELINK 0
  92. #endif
  93. #define OS_NUM (OS_NUM_BASE + OS_NUM_GNU_HASH + OS_NUM_PRELINK)
  94. #ifndef ARCH_DYNAMIC_INFO
  95. /* define in arch specific code, if needed */
  96. # define ARCH_NUM 0
  97. #endif
  98. #define DYNAMIC_SIZE (DT_NUM + OS_NUM + ARCH_NUM)
  99. /* Keep ARCH specific entries into dynamic section at the end of the array */
  100. #define DT_RELCONT_IDX (DYNAMIC_SIZE - OS_NUM - ARCH_NUM)
  101. #ifdef __LDSO_GNU_HASH_SUPPORT__
  102. /* GNU hash comes just after the relocation count */
  103. # define DT_GNU_HASH_IDX (DT_RELCONT_IDX + 1)
  104. #else
  105. # define DT_GNU_HASH_IDX DT_RELCONT_IDX
  106. #endif
  107. #ifdef __LDSO_PRELINK_SUPPORT__
  108. /* GNU prelink comes just after the GNU hash if present */
  109. #define DT_GNU_PRELINKED_IDX (DT_GNU_HASH_IDX + 1)
  110. #define DT_GNU_CONFLICT_IDX (DT_GNU_HASH_IDX + 2)
  111. #define DT_GNU_CONFLICTSZ_IDX (DT_GNU_HASH_IDX + 3)
  112. #define DT_GNU_LIBLIST_IDX (DT_GNU_HASH_IDX + 4)
  113. #define DT_GNU_LIBLISTSZ_IDX (DT_GNU_HASH_IDX + 5)
  114. #define DT_CHECKSUM_IDX (DT_GNU_HASH_IDX + 6)
  115. #endif
  116. extern unsigned int _dl_parse_dynamic_info(ElfW(Dyn) *dpnt, unsigned long dynamic_info[],
  117. void *debug_addr, DL_LOADADDR_TYPE load_off);
  118. static __always_inline
  119. unsigned int __dl_parse_dynamic_info(ElfW(Dyn) *dpnt, unsigned long dynamic_info[],
  120. void *debug_addr, DL_LOADADDR_TYPE load_off)
  121. {
  122. unsigned int rtld_flags = 0;
  123. for (; dpnt->d_tag; dpnt++) {
  124. if (dpnt->d_tag < DT_NUM) {
  125. dynamic_info[dpnt->d_tag] = dpnt->d_un.d_val;
  126. #ifndef __mips__
  127. /* we disable for mips because normally this page is readonly
  128. * and modifying the value here needlessly dirties a page.
  129. * see this post for more info:
  130. * http://uclibc.org/lists/uclibc/2006-April/015224.html */
  131. if (dpnt->d_tag == DT_DEBUG)
  132. dpnt->d_un.d_val = (unsigned long)debug_addr;
  133. #endif
  134. if (dpnt->d_tag == DT_BIND_NOW)
  135. dynamic_info[DT_BIND_NOW] = 1;
  136. if (dpnt->d_tag == DT_FLAGS &&
  137. (dpnt->d_un.d_val & DF_BIND_NOW))
  138. dynamic_info[DT_BIND_NOW] = 1;
  139. if (dpnt->d_tag == DT_TEXTREL)
  140. dynamic_info[DT_TEXTREL] = 1;
  141. #ifdef __LDSO_RUNPATH__
  142. if (dpnt->d_tag == DT_RUNPATH)
  143. dynamic_info[DT_RPATH] = 0;
  144. if (dpnt->d_tag == DT_RPATH && dynamic_info[DT_RUNPATH])
  145. dynamic_info[DT_RPATH] = 0;
  146. #endif
  147. } else if (dpnt->d_tag < DT_LOPROC) {
  148. if (dpnt->d_tag == DT_RELOCCOUNT)
  149. dynamic_info[DT_RELCONT_IDX] = dpnt->d_un.d_val;
  150. if (dpnt->d_tag == DT_FLAGS_1) {
  151. if (dpnt->d_un.d_val & DF_1_NOW)
  152. dynamic_info[DT_BIND_NOW] = 1;
  153. if (dpnt->d_un.d_val & DF_1_NODELETE)
  154. rtld_flags |= RTLD_NODELETE;
  155. }
  156. #ifdef __LDSO_GNU_HASH_SUPPORT__
  157. if (dpnt->d_tag == DT_GNU_HASH)
  158. dynamic_info[DT_GNU_HASH_IDX] = dpnt->d_un.d_ptr;
  159. #endif
  160. #ifdef __LDSO_PRELINK_SUPPORT__
  161. if (dpnt->d_tag == DT_GNU_PRELINKED)
  162. dynamic_info[DT_GNU_PRELINKED_IDX] = dpnt->d_un.d_val;
  163. if (dpnt->d_tag == DT_GNU_CONFLICT)
  164. dynamic_info[DT_GNU_CONFLICT_IDX] = dpnt->d_un.d_ptr;
  165. if (dpnt->d_tag == DT_GNU_CONFLICTSZ)
  166. dynamic_info[DT_GNU_CONFLICTSZ_IDX] = dpnt->d_un.d_val;
  167. if (dpnt->d_tag == DT_GNU_LIBLIST)
  168. dynamic_info[DT_GNU_LIBLIST_IDX] = dpnt->d_un.d_ptr;
  169. if (dpnt->d_tag == DT_GNU_LIBLISTSZ)
  170. dynamic_info[DT_GNU_LIBLISTSZ_IDX] = dpnt->d_un.d_val;
  171. if (dpnt->d_tag == DT_CHECKSUM)
  172. dynamic_info[DT_CHECKSUM_IDX] = dpnt->d_un.d_val;
  173. #endif
  174. }
  175. #ifdef ARCH_DYNAMIC_INFO
  176. else {
  177. ARCH_DYNAMIC_INFO(dpnt, dynamic_info, debug_addr);
  178. }
  179. #endif
  180. }
  181. #define ADJUST_DYN_INFO(tag, load_off) \
  182. do { \
  183. if (dynamic_info[tag]) \
  184. dynamic_info[tag] = (unsigned long) DL_RELOC_ADDR(load_off, dynamic_info[tag]); \
  185. } while (0)
  186. /* Don't adjust .dynamic unnecessarily. For FDPIC targets,
  187. we'd have to walk all the loadsegs to find out if it was
  188. actually unnecessary, so skip this optimization. */
  189. #if !defined __FDPIC__ && !defined __DSBT__
  190. if (load_off != 0)
  191. #endif
  192. {
  193. ADJUST_DYN_INFO(DT_HASH, load_off);
  194. ADJUST_DYN_INFO(DT_PLTGOT, load_off);
  195. ADJUST_DYN_INFO(DT_STRTAB, load_off);
  196. ADJUST_DYN_INFO(DT_SYMTAB, load_off);
  197. ADJUST_DYN_INFO(DT_RELOC_TABLE_ADDR, load_off);
  198. ADJUST_DYN_INFO(DT_JMPREL, load_off);
  199. #ifdef __LDSO_GNU_HASH_SUPPORT__
  200. ADJUST_DYN_INFO(DT_GNU_HASH_IDX, load_off);
  201. #endif
  202. }
  203. #ifdef __DSBT__
  204. /* Get the mapped address of the DSBT base. */
  205. ADJUST_DYN_INFO(DT_DSBT_BASE_IDX, load_off);
  206. #endif
  207. #undef ADJUST_DYN_INFO
  208. return rtld_flags;
  209. }
  210. /* Reloc type classes as returned by elf_machine_type_class().
  211. ELF_RTYPE_CLASS_PLT means this reloc should not be satisfied by
  212. some PLT symbol, ELF_RTYPE_CLASS_COPY means this reloc should not be
  213. satisfied by any symbol in the executable. Some architectures do
  214. not support copy relocations. In this case we define the macro to
  215. zero so that the code for handling them gets automatically optimized
  216. out. */
  217. #ifdef DL_NO_COPY_RELOCS
  218. # define ELF_RTYPE_CLASS_COPY (0x0)
  219. #else
  220. # define ELF_RTYPE_CLASS_COPY (0x2)
  221. #endif
  222. #define ELF_RTYPE_CLASS_PLT (0x1)
  223. /* dlsym() calls _dl_find_hash with this value, that enables
  224. DL_FIND_HASH_VALUE to return something different than the symbol
  225. itself, e.g., a function descriptor. */
  226. #define ELF_RTYPE_CLASS_DLSYM 0x80000000
  227. /* Convert between the Linux flags for page protections and the
  228. ones specified in the ELF standard. */
  229. #define LXFLAGS(X) ( (((X) & PF_R) ? PROT_READ : 0) | \
  230. (((X) & PF_W) ? PROT_WRITE : 0) | \
  231. (((X) & PF_X) ? PROT_EXEC : 0))
  232. #endif /* _DL_ELF_H */