dl-elf.h 8.2 KB

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