dl-sysdep.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /* vi: set sw=8 ts=8: */
  2. /*
  3. * Various assmbly 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 3
  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 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_RLD_MAP) \
  96. *(ElfW(Addr) *)(dpnt->d_un.d_ptr) = (ElfW(Addr)) debug_addr; \
  97. } while (0)
  98. /* Initialization sequence for the application/library GOT. */
  99. #define INIT_GOT(GOT_BASE,MODULE) \
  100. do { \
  101. unsigned long idx; \
  102. \
  103. /* Check if this is the dynamic linker itself */ \
  104. if (MODULE->libtype == program_interpreter) \
  105. continue; \
  106. \
  107. /* Fill in first two GOT entries according to the ABI */ \
  108. GOT_BASE[0] = (unsigned long) _dl_runtime_resolve; \
  109. GOT_BASE[1] = (unsigned long) MODULE; \
  110. \
  111. /* Add load address displacement to all local GOT entries */ \
  112. idx = 2; \
  113. while (idx < MODULE->dynamic_info[DT_MIPS_LOCAL_GOTNO_IDX]) \
  114. GOT_BASE[idx++] += (unsigned long) MODULE->loadaddr; \
  115. \
  116. } while (0)
  117. /* Here we define the magic numbers that this dynamic loader should accept */
  118. #define MAGIC1 EM_MIPS
  119. #define MAGIC2 EM_MIPS_RS3_LE
  120. /* Used for error messages */
  121. #define ELF_TARGET "MIPS"
  122. unsigned long __dl_runtime_resolve(unsigned long sym_index,
  123. unsigned long old_gpreg);
  124. struct elf_resolve;
  125. void _dl_perform_mips_global_got_relocations(struct elf_resolve *tpnt, int lazy);
  126. /* 4096 bytes alignment */
  127. #if _MIPS_SIM == _MIPS_SIM_ABI64
  128. #define PAGE_ALIGN (~0xfffUL)
  129. #define ADDR_ALIGN 0xfffUL
  130. #define OFFS_ALIGN (0x10000000000UL-0x1000)
  131. #else /* O32 || N32 */
  132. #define PAGE_ALIGN 0xfffff000
  133. #define ADDR_ALIGN 0xfff
  134. #define OFFS_ALIGN 0x7ffff000
  135. #endif /* O32 || N32 */
  136. #define elf_machine_type_class(type) ELF_RTYPE_CLASS_PLT
  137. /* MIPS does not have COPY relocs */
  138. #define DL_NO_COPY_RELOCS
  139. #define OFFSET_GP_GOT 0x7ff0
  140. static __inline__ ElfW(Addr) *
  141. elf_mips_got_from_gpreg (ElfW(Addr) gpreg)
  142. {
  143. /* FIXME: the offset of gp from GOT may be system-dependent. */
  144. return (ElfW(Addr) *) (gpreg - OFFSET_GP_GOT);
  145. }
  146. /* Return the link-time address of _DYNAMIC. Conveniently, this is the
  147. first element of the GOT. This must be inlined in a function which
  148. uses global data. We assume its $gp points to the primary GOT. */
  149. static __inline__ ElfW(Addr)
  150. elf_machine_dynamic (void)
  151. {
  152. register ElfW(Addr) gp __asm__ ("$28");
  153. return *elf_mips_got_from_gpreg (gp);
  154. }
  155. #define STRINGXP(X) __STRING(X)
  156. #define STRINGXV(X) STRINGV_(X)
  157. #define STRINGV_(...) # __VA_ARGS__
  158. #if _MIPS_SIM == _MIPS_SIM_ABI64
  159. #define PTR_LA dla
  160. #define PTR_SUBU dsubu
  161. #else
  162. #define PTR_LA la
  163. #define PTR_SUBU subu
  164. #endif
  165. /* Return the run-time load address of the shared object. */
  166. static __inline__ ElfW(Addr)
  167. elf_machine_load_address (void)
  168. {
  169. ElfW(Addr) addr;
  170. __asm__ (" .set noreorder\n"
  171. " " STRINGXP (PTR_LA) " %0, 0f\n"
  172. " bltzal $0, 0f\n"
  173. " nop\n"
  174. "0: " STRINGXP (PTR_SUBU) " %0, $31, %0\n"
  175. " .set reorder\n"
  176. : "=r" (addr)
  177. : /* No inputs */
  178. : "$31");
  179. return addr;
  180. }
  181. static __inline__ void
  182. elf_machine_relative (ElfW(Addr) load_off, const ElfW(Addr) rel_addr,
  183. ElfW(Word) relative_count)
  184. {
  185. /* No RELATIVE relocs in MIPS? */
  186. }
  187. #ifdef __mips64
  188. #define DL_MALLOC_ALIGN 8 /* N64/N32 needs 8 byte alignment */
  189. #endif