dl-sysdep.h 8.1 KB

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