dl-elf.h 8.2 KB

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