dl-sysdep.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*
  2. * Various assmbly language/system dependent hacks that are required
  3. * so that we can minimize the amount of platform specific code.
  4. */
  5. /*
  6. * Define this if the system uses RELOCA.
  7. */
  8. #define ELF_USES_RELOCA
  9. #include <elf.h>
  10. /*
  11. * Initialization sequence for a GOT.
  12. */
  13. #define INIT_GOT(GOT_BASE,MODULE) _dl_init_got(GOT_BASE,MODULE)
  14. /* Stuff for the PLT. */
  15. #define PLT_INITIAL_ENTRY_WORDS 18
  16. #define PLT_LONGBRANCH_ENTRY_WORDS 0
  17. #define PLT_TRAMPOLINE_ENTRY_WORDS 6
  18. #define PLT_DOUBLE_SIZE (1<<13)
  19. #define PLT_ENTRY_START_WORDS(entry_number) \
  20. (PLT_INITIAL_ENTRY_WORDS + (entry_number)*2 \
  21. + ((entry_number) > PLT_DOUBLE_SIZE \
  22. ? ((entry_number) - PLT_DOUBLE_SIZE)*2 \
  23. : 0))
  24. #define PLT_DATA_START_WORDS(num_entries) PLT_ENTRY_START_WORDS(num_entries)
  25. /* Macros to build PowerPC opcode words. */
  26. #define OPCODE_ADDI(rd,ra,simm) \
  27. (0x38000000 | (rd) << 21 | (ra) << 16 | ((simm) & 0xffff))
  28. #define OPCODE_ADDIS(rd,ra,simm) \
  29. (0x3c000000 | (rd) << 21 | (ra) << 16 | ((simm) & 0xffff))
  30. #define OPCODE_ADD(rd,ra,rb) \
  31. (0x7c000214 | (rd) << 21 | (ra) << 16 | (rb) << 11)
  32. #define OPCODE_B(target) (0x48000000 | ((target) & 0x03fffffc))
  33. #define OPCODE_BA(target) (0x48000002 | ((target) & 0x03fffffc))
  34. #define OPCODE_BCTR() 0x4e800420
  35. #define OPCODE_LWZ(rd,d,ra) \
  36. (0x80000000 | (rd) << 21 | (ra) << 16 | ((d) & 0xffff))
  37. #define OPCODE_LWZU(rd,d,ra) \
  38. (0x84000000 | (rd) << 21 | (ra) << 16 | ((d) & 0xffff))
  39. #define OPCODE_MTCTR(rd) (0x7C0903A6 | (rd) << 21)
  40. #define OPCODE_RLWINM(ra,rs,sh,mb,me) \
  41. (0x54000000 | (rs) << 21 | (ra) << 16 | (sh) << 11 | (mb) << 6 | (me) << 1)
  42. #define OPCODE_LI(rd,simm) OPCODE_ADDI(rd,0,simm)
  43. #define OPCODE_ADDIS_HI(rd,ra,value) \
  44. OPCODE_ADDIS(rd,ra,((value) + 0x8000) >> 16)
  45. #define OPCODE_LIS_HI(rd,value) OPCODE_ADDIS_HI(rd,0,value)
  46. #define OPCODE_SLWI(ra,rs,sh) OPCODE_RLWINM(ra,rs,sh,0,31-sh)
  47. #define PPC_DCBST(where) __asm__ __volatile__ ("dcbst 0,%0" : : "r"(where) : "memory")
  48. #define PPC_SYNC __asm__ __volatile__ ("sync" : : : "memory")
  49. #define PPC_ISYNC __asm__ __volatile__ ("sync; isync" : : : "memory")
  50. #define PPC_ICBI(where) __asm__ __volatile__ ("icbi 0,%0" : : "r"(where) : "memory")
  51. #define PPC_DIE __asm__ __volatile__ ("tweq 0,0")
  52. /* Here we define the magic numbers that this dynamic loader should accept */
  53. #define MAGIC1 EM_PPC
  54. #undef MAGIC2
  55. /* Used for error messages */
  56. #define ELF_TARGET "powerpc"
  57. struct elf_resolve;
  58. extern unsigned long _dl_linux_resolver(struct elf_resolve * tpnt, int reloc_entry);
  59. void _dl_init_got(unsigned long *lpnt,struct elf_resolve *tpnt);
  60. /* 4096 bytes alignment */
  61. #define PAGE_ALIGN 0xfffff000
  62. #define ADDR_ALIGN 0xfff
  63. #define OFFS_ALIGN 0x7ffff000
  64. /* ELF_RTYPE_CLASS_PLT iff TYPE describes relocation of a PLT entry, so
  65. PLT entries should not be allowed to define the value.
  66. ELF_RTYPE_CLASS_NOCOPY iff TYPE should not be allowed to resolve to one
  67. of the main executable's symbols, as for a COPY reloc. */
  68. /* We never want to use a PLT entry as the destination of a
  69. reloc, when what is being relocated is a branch. This is
  70. partly for efficiency, but mostly so we avoid loops. */
  71. #define elf_machine_type_class(type) \
  72. ((((type) == R_PPC_JMP_SLOT \
  73. || (type) == R_PPC_REL24 \
  74. || (type) == R_PPC_ADDR24) * ELF_RTYPE_CLASS_PLT) \
  75. | (((type) == R_PPC_COPY) * ELF_RTYPE_CLASS_COPY))
  76. /* The SVR4 ABI specifies that the JMPREL relocs must be inside the
  77. DT_RELA table. */
  78. #define ELF_MACHINE_PLTREL_OVERLAP 1
  79. /* Return the value of the GOT pointer. */
  80. static __inline__ Elf32_Addr * __attribute__ ((const))
  81. ppc_got (void)
  82. {
  83. Elf32_Addr *got;
  84. #ifdef HAVE_ASM_PPC_REL16
  85. __asm__ (" bcl 20,31,1f\n"
  86. "1:mflr %0\n"
  87. " addis %0,%0,_GLOBAL_OFFSET_TABLE_-1b@ha\n"
  88. " addi %0,%0,_GLOBAL_OFFSET_TABLE_-1b@l\n"
  89. : "=b" (got) : : "lr");
  90. #else
  91. __asm__ (" bl _GLOBAL_OFFSET_TABLE_-4@local"
  92. : "=l" (got));
  93. #endif
  94. return got;
  95. }
  96. /* Return the link-time address of _DYNAMIC, stored as
  97. the first value in the GOT. */
  98. static __inline__ Elf32_Addr __attribute__ ((const))
  99. elf_machine_dynamic (void)
  100. {
  101. return *ppc_got();
  102. }
  103. /* Return the run-time load address of the shared object. */
  104. static __inline__ Elf32_Addr __attribute__ ((const))
  105. elf_machine_load_address (void)
  106. {
  107. Elf32_Addr *branchaddr;
  108. Elf32_Addr runtime_dynamic;
  109. /* This is much harder than you'd expect. Possibly I'm missing something.
  110. The 'obvious' way:
  111. Apparently, "bcl 20,31,$+4" is what should be used to load LR
  112. with the address of the next instruction.
  113. I think this is so that machines that do bl/blr pairing don't
  114. get confused.
  115. __asm__ ("bcl 20,31,0f ;"
  116. "0: mflr 0 ;"
  117. "lis %0,0b@ha;"
  118. "addi %0,%0,0b@l;"
  119. "subf %0,%0,0"
  120. : "=b" (addr) : : "r0", "lr");
  121. doesn't work, because the linker doesn't have to (and in fact doesn't)
  122. update the @ha and @l references; the loader (which runs after this
  123. code) will do that.
  124. Instead, we use the following trick:
  125. The linker puts the _link-time_ address of _DYNAMIC at the first
  126. word in the GOT. We could branch to that address, if we wanted,
  127. by using an @local reloc; the linker works this out, so it's safe
  128. to use now. We can't, of course, actually branch there, because
  129. we'd cause an illegal instruction exception; so we need to compute
  130. the address ourselves. That gives us the following code: */
  131. /* Get address of the 'b _DYNAMIC@local'... */
  132. __asm__ ("bcl 20,31,0f;"
  133. "b _DYNAMIC@local;"
  134. "0:"
  135. : "=l"(branchaddr));
  136. /* So now work out the difference between where the branch actually points,
  137. and the offset of that location in memory from the start of the file. */
  138. runtime_dynamic = ((Elf32_Addr) branchaddr
  139. + ((Elf32_Sword) (*branchaddr << 6 & 0xffffff00) >> 6));
  140. return runtime_dynamic - elf_machine_dynamic ();
  141. }
  142. static __inline__ void
  143. elf_machine_relative (Elf32_Addr load_off, const Elf32_Addr rel_addr,
  144. Elf32_Word relative_count)
  145. {
  146. Elf32_Rela * rpnt = (void *)rel_addr;
  147. --rpnt;
  148. do { /* PowerPC handles pre increment/decrement better */
  149. Elf32_Addr *const reloc_addr = (void *) (load_off + (++rpnt)->r_offset);
  150. *reloc_addr = load_off + rpnt->r_addend;
  151. } while (--relative_count);
  152. }
  153. #define ARCH_NUM 1
  154. #define DT_PPC_GOT_IDX (DT_NUM + OS_NUM)
  155. #define ARCH_DYNAMIC_INFO(dpnt, dynamic, debug_addr) \
  156. do { \
  157. if (dpnt->d_tag == DT_PPC_GOT) \
  158. dynamic[DT_PPC_GOT_IDX] = dpnt->d_un.d_ptr; \
  159. } while (0)