dl-sysdep.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /* vi: set sw=8 ts=8: */
  2. /*
  3. * Various assembly language/system dependent hacks that are required
  4. * so that we can minimize the amount of platform specific code.
  5. */
  6. /* Define this if the system uses RELOCA. */
  7. #undef ELF_USES_RELOCA
  8. #include <elf.h>
  9. #ifdef __mips64 /* from glibc sysdeps/mips/elf/ldsodefs.h 1.4 */
  10. /* The 64-bit MIPS ELF ABI uses an unusual reloc format. Each
  11. relocation entry specifies up to three actual relocations, all at
  12. the same address. The first relocation which required a symbol
  13. uses the symbol in the r_sym field. The second relocation which
  14. requires a symbol uses the symbol in the r_ssym field. If all
  15. three relocations require a symbol, the third one uses a zero
  16. value.
  17. We define these structures in internal headers because we're not
  18. sure we want to make them part of the ABI yet. Eventually, some of
  19. this may move into elf/elf.h. */
  20. /* An entry in a 64 bit SHT_REL section. */
  21. typedef struct
  22. {
  23. Elf32_Word r_sym; /* Symbol index */
  24. unsigned char r_ssym; /* Special symbol for 2nd relocation */
  25. unsigned char r_type3; /* 3rd relocation type */
  26. unsigned char r_type2; /* 2nd relocation type */
  27. unsigned char r_type1; /* 1st relocation type */
  28. } _Elf64_Mips_R_Info;
  29. typedef union
  30. {
  31. Elf64_Xword r_info_number;
  32. _Elf64_Mips_R_Info r_info_fields;
  33. } _Elf64_Mips_R_Info_union;
  34. typedef struct
  35. {
  36. Elf64_Addr r_offset; /* Address */
  37. _Elf64_Mips_R_Info_union r_info; /* Relocation type and symbol index */
  38. } Elf64_Mips_Rel;
  39. typedef struct
  40. {
  41. Elf64_Addr r_offset; /* Address */
  42. _Elf64_Mips_R_Info_union r_info; /* Relocation type and symbol index */
  43. Elf64_Sxword r_addend; /* Addend */
  44. } Elf64_Mips_Rela;
  45. #define ELF64_MIPS_R_SYM(i) \
  46. ((__extension__ (_Elf64_Mips_R_Info_union)(i)).r_info_fields.r_sym)
  47. #define ELF64_MIPS_R_TYPE(i) \
  48. (((_Elf64_Mips_R_Info_union)(i)).r_info_fields.r_type1 \
  49. | ((Elf32_Word)(__extension__ (_Elf64_Mips_R_Info_union)(i) \
  50. ).r_info_fields.r_type2 << 8) \
  51. | ((Elf32_Word)(__extension__ (_Elf64_Mips_R_Info_union)(i) \
  52. ).r_info_fields.r_type3 << 16) \
  53. | ((Elf32_Word)(__extension__ (_Elf64_Mips_R_Info_union)(i) \
  54. ).r_info_fields.r_ssym << 24))
  55. #define ELF64_MIPS_R_INFO(sym, type) \
  56. (__extension__ (_Elf64_Mips_R_Info_union) \
  57. (__extension__ (_Elf64_Mips_R_Info) \
  58. { (sym), ELF64_MIPS_R_SSYM (type), \
  59. ELF64_MIPS_R_TYPE3 (type), \
  60. ELF64_MIPS_R_TYPE2 (type), \
  61. ELF64_MIPS_R_TYPE1 (type) \
  62. }).r_info_number)
  63. /* These macros decompose the value returned by ELF64_MIPS_R_TYPE, and
  64. compose it back into a value that it can be used as an argument to
  65. ELF64_MIPS_R_INFO. */
  66. #define ELF64_MIPS_R_SSYM(i) (((i) >> 24) & 0xff)
  67. #define ELF64_MIPS_R_TYPE3(i) (((i) >> 16) & 0xff)
  68. #define ELF64_MIPS_R_TYPE2(i) (((i) >> 8) & 0xff)
  69. #define ELF64_MIPS_R_TYPE1(i) ((i) & 0xff)
  70. #define ELF64_MIPS_R_TYPEENC(type1, type2, type3, ssym) \
  71. ((type1) \
  72. | ((Elf32_Word)(type2) << 8) \
  73. | ((Elf32_Word)(type3) << 16) \
  74. | ((Elf32_Word)(ssym) << 24))
  75. #undef ELF64_R_SYM
  76. #define ELF64_R_SYM(i) ELF64_MIPS_R_SYM (i)
  77. #undef ELF64_R_TYPE
  78. #define ELF64_R_TYPE(i) ELF64_MIPS_R_TYPE (i)
  79. #undef ELF64_R_INFO
  80. #define ELF64_R_INFO(sym, type) ELF64_MIPS_R_INFO ((sym), (type))
  81. #endif /* __mips64 */
  82. #include <link.h>
  83. #define ARCH_NUM 4
  84. #define DT_MIPS_GOTSYM_IDX (DT_NUM + OS_NUM)
  85. #define DT_MIPS_LOCAL_GOTNO_IDX (DT_NUM + OS_NUM +1)
  86. #define DT_MIPS_SYMTABNO_IDX (DT_NUM + OS_NUM +2)
  87. #define DT_MIPS_PLTGOT_IDX (DT_NUM + OS_NUM +3)
  88. #define ARCH_DYNAMIC_INFO(dpnt, dynamic, debug_addr) \
  89. do { \
  90. if (dpnt->d_tag == DT_MIPS_GOTSYM) \
  91. dynamic[DT_MIPS_GOTSYM_IDX] = dpnt->d_un.d_val; \
  92. else if (dpnt->d_tag == DT_MIPS_LOCAL_GOTNO) \
  93. dynamic[DT_MIPS_LOCAL_GOTNO_IDX] = dpnt->d_un.d_val; \
  94. else if (dpnt->d_tag == DT_MIPS_SYMTABNO) \
  95. dynamic[DT_MIPS_SYMTABNO_IDX] = dpnt->d_un.d_val; \
  96. else if (dpnt->d_tag == DT_MIPS_PLTGOT) \
  97. dynamic[DT_MIPS_PLTGOT_IDX] = dpnt->d_un.d_val; \
  98. else if ((dpnt->d_tag == DT_MIPS_RLD_MAP) && (dpnt->d_un.d_ptr)) \
  99. *(ElfW(Addr) *)(dpnt->d_un.d_ptr) = (ElfW(Addr)) debug_addr; \
  100. } while (0)
  101. #define ARCH_SKIP_RELOC(type_class, sym) \
  102. ((sym)->st_shndx == SHN_UNDEF && !((sym)->st_other & STO_MIPS_PLT))
  103. /* Initialization sequence for the application/library GOT. */
  104. #define INIT_GOT(GOT_BASE,MODULE) \
  105. do { \
  106. unsigned long idx; \
  107. unsigned long *pltgot; \
  108. \
  109. /* Check if this is the dynamic linker itself */ \
  110. if (MODULE->libtype == program_interpreter) \
  111. continue; \
  112. \
  113. /* Fill in first two GOT entries according to the ABI */ \
  114. GOT_BASE[0] = (unsigned long) _dl_runtime_resolve; \
  115. GOT_BASE[1] = (unsigned long) MODULE; \
  116. \
  117. pltgot = (unsigned long *) MODULE->dynamic_info[DT_MIPS_PLTGOT_IDX]; \
  118. if (pltgot) { \
  119. pltgot[0] = (unsigned long) _dl_runtime_pltresolve; \
  120. pltgot[1] = (unsigned long) MODULE; \
  121. } \
  122. \
  123. /* Add load address displacement to all local GOT entries */ \
  124. idx = 2; \
  125. while (idx < MODULE->dynamic_info[DT_MIPS_LOCAL_GOTNO_IDX]) \
  126. GOT_BASE[idx++] += (unsigned long) MODULE->loadaddr; \
  127. \
  128. } while (0)
  129. /* Here we define the magic numbers that this dynamic loader should accept */
  130. #define MAGIC1 EM_MIPS
  131. #define MAGIC2 EM_MIPS_RS3_LE
  132. /* Used for error messages */
  133. #define ELF_TARGET "MIPS"
  134. /* Need bootstrap relocations */
  135. #define ARCH_NEEDS_BOOTSTRAP_RELOCS
  136. unsigned long __dl_runtime_resolve(unsigned long sym_index,
  137. unsigned long old_gpreg);
  138. struct elf_resolve;
  139. unsigned long __dl_runtime_pltresolve(struct elf_resolve *tpnt,
  140. int reloc_entry);
  141. void _dl_perform_mips_global_got_relocations(struct elf_resolve *tpnt, int lazy);
  142. /* 4096 bytes alignment */
  143. #if _MIPS_SIM == _MIPS_SIM_ABI64
  144. #define OFFS_ALIGN (0x10000000000UL-0x1000)
  145. #endif /* O32 || N32 */
  146. #if defined USE_TLS
  147. # if _MIPS_SIM == _MIPS_SIM_ABI64
  148. # define elf_machine_type_class(type) \
  149. ((((type) == R_MIPS_JUMP_SLOT || (type) == R_MIPS_TLS_DTPMOD64 \
  150. || (type) == R_MIPS_TLS_DTPREL64 || (type) == R_MIPS_TLS_TPREL64) \
  151. * ELF_RTYPE_CLASS_PLT) \
  152. | (((type) == R_MIPS_COPY) * ELF_RTYPE_CLASS_COPY))
  153. # else
  154. # define elf_machine_type_class(type) \
  155. ((((type) == R_MIPS_JUMP_SLOT || (type) == R_MIPS_TLS_DTPMOD32 \
  156. || (type) == R_MIPS_TLS_DTPREL32 || (type) == R_MIPS_TLS_TPREL32) \
  157. * ELF_RTYPE_CLASS_PLT) \
  158. | (((type) == R_MIPS_COPY) * ELF_RTYPE_CLASS_COPY))
  159. # endif /* _MIPS_SIM == _MIPS_SIM_ABI64 */
  160. #else
  161. #define elf_machine_type_class(type) \
  162. ((((type) == R_MIPS_JUMP_SLOT) * ELF_RTYPE_CLASS_PLT) \
  163. | (((type) == R_MIPS_COPY) * ELF_RTYPE_CLASS_COPY))
  164. #endif /* USE_TLS */
  165. #define OFFSET_GP_GOT 0x7ff0
  166. static __always_inline ElfW(Addr) *
  167. elf_mips_got_from_gpreg (ElfW(Addr) gpreg)
  168. {
  169. /* FIXME: the offset of gp from GOT may be system-dependent. */
  170. return (ElfW(Addr) *) (gpreg - OFFSET_GP_GOT);
  171. }
  172. /* Return the link-time address of _DYNAMIC. Conveniently, this is the
  173. first element of the GOT. This must be inlined in a function which
  174. uses global data. We assume its $gp points to the primary GOT. */
  175. static __always_inline ElfW(Addr)
  176. elf_machine_dynamic (void)
  177. {
  178. register ElfW(Addr) gp __asm__ ("$28");
  179. return *elf_mips_got_from_gpreg (gp);
  180. }
  181. #define STRINGXP(X) __STRING(X)
  182. #define STRINGXV(X) STRINGV_(X)
  183. #define STRINGV_(...) # __VA_ARGS__
  184. #if _MIPS_SIM == _MIPS_SIM_ABI64
  185. #define PTR_LA dla
  186. #define PTR_SUBU dsubu
  187. #else
  188. #define PTR_LA la
  189. #define PTR_SUBU subu
  190. #endif
  191. /* Return the run-time load address of the shared object. */
  192. static __always_inline ElfW(Addr)
  193. elf_machine_load_address (void)
  194. {
  195. ElfW(Addr) addr;
  196. __asm__ (" .set noreorder\n"
  197. # if !defined __mips_isa_rev || __mips_isa_rev < 6
  198. " " STRINGXP (PTR_LA) " %0, 0f\n"
  199. " bltzal $0, 0f\n"
  200. " nop\n"
  201. "0: " STRINGXP (PTR_SUBU) " %0, $31, %0\n"
  202. #else
  203. "0: lapc $31, 0\n"
  204. " " STRINGXP (PTR_SUBU) " %0, $31, %0\n"
  205. #endif
  206. " .set reorder\n"
  207. : "=r" (addr)
  208. : /* No inputs */
  209. : "$31");
  210. return addr;
  211. }
  212. static __always_inline void
  213. elf_machine_relative (ElfW(Addr) load_off, const ElfW(Addr) rel_addr,
  214. ElfW(Word) relative_count)
  215. {
  216. /* No RELATIVE relocs in MIPS? */
  217. }
  218. #ifdef __mips64
  219. #define DL_MALLOC_ALIGN 8 /* N64/N32 needs 8 byte alignment */
  220. #endif