dl-elf.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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 LINUXELF_H
  8. #define LINUXELF_H
  9. #include <dl-string.h> /* before elf.h to get ELF_USES_RELOCA right */
  10. #include <elf.h>
  11. #include <link.h>
  12. /* Forward declarations for stuff defined in ld_hash.h */
  13. struct dyn_elf;
  14. struct elf_resolve;
  15. #include <dl-defs.h>
  16. #ifdef __LDSO_CACHE_SUPPORT__
  17. extern int _dl_map_cache(void);
  18. extern int _dl_unmap_cache(void);
  19. #else
  20. static inline void _dl_map_cache(void) { }
  21. static inline void _dl_unmap_cache(void) { }
  22. #endif
  23. /* Function prototypes for non-static stuff in readelflib1.c */
  24. extern void _dl_parse_lazy_relocation_information(struct dyn_elf *rpnt,
  25. unsigned long rel_addr, unsigned long rel_size);
  26. extern int _dl_parse_relocation_information(struct dyn_elf *rpnt,
  27. unsigned long rel_addr, unsigned long rel_size);
  28. extern struct elf_resolve * _dl_load_shared_library(int secure,
  29. struct dyn_elf **rpnt, struct elf_resolve *tpnt, char *full_libname,
  30. int trace_loaded_objects);
  31. extern struct elf_resolve * _dl_load_elf_shared_library(int secure,
  32. struct dyn_elf **rpnt, char *libname);
  33. extern struct elf_resolve *_dl_check_if_named_library_is_loaded(const char *full_libname,
  34. int trace_loaded_objects);
  35. extern int _dl_linux_resolve(void);
  36. extern int _dl_fixup(struct dyn_elf *rpnt, int flag);
  37. extern void _dl_protect_relro (struct elf_resolve *l);
  38. /*
  39. * Bitsize related settings for things ElfW()
  40. * does not handle already
  41. */
  42. #if __WORDSIZE == 64
  43. # define ELF_ST_BIND(val) ELF64_ST_BIND(val)
  44. # define ELF_ST_TYPE(val) ELF64_ST_TYPE(val)
  45. # define ELF_R_SYM(i) ELF64_R_SYM(i)
  46. # define ELF_R_TYPE(i) ELF64_R_TYPE(i)
  47. # ifndef ELF_CLASS
  48. # define ELF_CLASS ELFCLASS64
  49. # endif
  50. #else
  51. # define ELF_ST_BIND(val) ELF32_ST_BIND(val)
  52. # define ELF_ST_TYPE(val) ELF32_ST_TYPE(val)
  53. # define ELF_R_SYM(i) ELF32_R_SYM(i)
  54. # define ELF_R_TYPE(i) ELF32_R_TYPE(i)
  55. # ifndef ELF_CLASS
  56. # define ELF_CLASS ELFCLASS32
  57. # endif
  58. #endif
  59. /*
  60. * Datatype of a relocation on this platform
  61. */
  62. #ifdef ELF_USES_RELOCA
  63. # define ELF_RELOC ElfW(Rela)
  64. # define DT_RELOC_TABLE_ADDR DT_RELA
  65. # define DT_RELOC_TABLE_SIZE DT_RELASZ
  66. # define DT_RELOCCOUNT DT_RELACOUNT
  67. # define UNSUPPORTED_RELOC_TYPE DT_REL
  68. # define UNSUPPORTED_RELOC_STR "REL"
  69. #else
  70. # define ELF_RELOC ElfW(Rel)
  71. # define DT_RELOC_TABLE_ADDR DT_REL
  72. # define DT_RELOC_TABLE_SIZE DT_RELSZ
  73. # define DT_RELOCCOUNT DT_RELCOUNT
  74. # define UNSUPPORTED_RELOC_TYPE DT_RELA
  75. # define UNSUPPORTED_RELOC_STR "RELA"
  76. #endif
  77. /* OS and/or GNU dynamic extensions */
  78. #define OS_NUM 1
  79. #define DT_RELCONT_IDX DT_NUM
  80. #ifndef ARCH_DYNAMIC_INFO
  81. /* define in arch specific code, if needed */
  82. # define ARCH_NUM 0
  83. #endif
  84. #define DYNAMIC_SIZE (DT_NUM+OS_NUM+ARCH_NUM)
  85. extern void _dl_parse_dynamic_info(ElfW(Dyn) *dpnt, unsigned long dynamic_info[], void *debug_addr, ElfW(Addr) load_off);
  86. static __always_inline
  87. void __dl_parse_dynamic_info(ElfW(Dyn) *dpnt, unsigned long dynamic_info[], void *debug_addr, ElfW(Addr) load_off)
  88. {
  89. for (; dpnt->d_tag; dpnt++) {
  90. if (dpnt->d_tag < DT_NUM) {
  91. dynamic_info[dpnt->d_tag] = dpnt->d_un.d_val;
  92. #ifndef __mips__
  93. if (dpnt->d_tag == DT_DEBUG)
  94. dpnt->d_un.d_val = (unsigned long)debug_addr;
  95. #endif
  96. if (dpnt->d_tag == DT_BIND_NOW)
  97. dynamic_info[DT_BIND_NOW] = 1;
  98. if (dpnt->d_tag == DT_FLAGS &&
  99. (dpnt->d_un.d_val & DF_BIND_NOW))
  100. dynamic_info[DT_BIND_NOW] = 1;
  101. if (dpnt->d_tag == DT_TEXTREL)
  102. dynamic_info[DT_TEXTREL] = 1;
  103. #ifdef __LDSO_RUNPATH__
  104. if (dpnt->d_tag == DT_RUNPATH)
  105. dynamic_info[DT_RPATH] = 0;
  106. if (dpnt->d_tag == DT_RPATH && dynamic_info[DT_RUNPATH])
  107. dynamic_info[DT_RPATH] = 0;
  108. #endif
  109. } else if (dpnt->d_tag < DT_LOPROC) {
  110. if (dpnt->d_tag == DT_RELOCCOUNT)
  111. dynamic_info[DT_RELCONT_IDX] = dpnt->d_un.d_val;
  112. if (dpnt->d_tag == DT_FLAGS_1 &&
  113. (dpnt->d_un.d_val & DF_1_NOW))
  114. dynamic_info[DT_BIND_NOW] = 1;
  115. }
  116. #ifdef ARCH_DYNAMIC_INFO
  117. else {
  118. ARCH_DYNAMIC_INFO(dpnt, dynamic_info, debug_addr);
  119. }
  120. #endif
  121. }
  122. #define ADJUST_DYN_INFO(tag, load_off) \
  123. do { \
  124. if (dynamic_info[tag]) \
  125. dynamic_info[tag] += load_off; \
  126. } while(0)
  127. ADJUST_DYN_INFO(DT_HASH, load_off);
  128. ADJUST_DYN_INFO(DT_PLTGOT, load_off);
  129. ADJUST_DYN_INFO(DT_STRTAB, load_off);
  130. ADJUST_DYN_INFO(DT_SYMTAB, load_off);
  131. ADJUST_DYN_INFO(DT_RELOC_TABLE_ADDR, load_off);
  132. ADJUST_DYN_INFO(DT_JMPREL, load_off);
  133. #undef ADJUST_DYN_INFO
  134. }
  135. /* Reloc type classes as returned by elf_machine_type_class().
  136. ELF_RTYPE_CLASS_PLT means this reloc should not be satisfied by
  137. some PLT symbol, ELF_RTYPE_CLASS_COPY means this reloc should not be
  138. satisfied by any symbol in the executable. Some architectures do
  139. not support copy relocations. In this case we define the macro to
  140. zero so that the code for handling them gets automatically optimized
  141. out. */
  142. #ifdef DL_NO_COPY_RELOCS
  143. # define ELF_RTYPE_CLASS_COPY (0x0)
  144. #else
  145. # define ELF_RTYPE_CLASS_COPY (0x2)
  146. #endif
  147. #define ELF_RTYPE_CLASS_PLT (0x1)
  148. /* Convert between the Linux flags for page protections and the
  149. ones specified in the ELF standard. */
  150. #define LXFLAGS(X) ( (((X) & PF_R) ? PROT_READ : 0) | \
  151. (((X) & PF_W) ? PROT_WRITE : 0) | \
  152. (((X) & PF_X) ? PROT_EXEC : 0))
  153. #endif /* LINUXELF_H */