dl-sysdep.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. #define ELF_USES_RELOCA
  8. #include <elf.h>
  9. /*
  10. * Initialization sequence for a GOT.
  11. */
  12. #define INIT_GOT(GOT_BASE,MODULE) \
  13. { \
  14. GOT_BASE[2] = (unsigned long)_dl_linux_resolve; \
  15. GOT_BASE[1] = (unsigned long)(MODULE); \
  16. }
  17. /* Here we define the magic numbers that this dynamic loader should accept */
  18. #define MAGIC1 EM_SH
  19. #undef MAGIC2
  20. /* Used for error messages */
  21. #define ELF_TARGET "sh64"
  22. /* Need bootstrap relocations */
  23. #define ARCH_NEEDS_BOOTSTRAP_RELOCS
  24. struct elf_resolve;
  25. extern unsigned long _dl_linux_resolver(struct elf_resolve * tpnt, int reloc_entry);
  26. /* ELF_RTYPE_CLASS_PLT iff TYPE describes relocation of a PLT entry or
  27. TLS variable, so undefined references should not be allowed to
  28. define the value.
  29. ELF_RTYPE_CLASS_NOCOPY iff TYPE should not be allowed to resolve to one
  30. of the main executable's symbols, as for a COPY reloc. */
  31. #define elf_machine_type_class(type) \
  32. ((((type) == R_SH_JMP_SLOT) * ELF_RTYPE_CLASS_PLT) \
  33. | (((type) == R_SH_COPY) * ELF_RTYPE_CLASS_COPY))
  34. /* Return the link-time address of _DYNAMIC. Conveniently, this is the
  35. first element of the GOT. This must be inlined in a function which
  36. uses global data. */
  37. static __always_inline Elf32_Addr elf_machine_dynamic(void)
  38. {
  39. register Elf32_Addr *got;
  40. /*
  41. * The toolchain adds 32768 to the GOT address, we compensate for
  42. * that in the movi/sub pair.
  43. *
  44. * XXX: If this is cleaned up in the toolchain, we can end up
  45. * saving 2 instructions and subsequently free up r1 from the
  46. * clobber list..
  47. */
  48. __asm__ (
  49. "movi\t(((datalabel _GLOBAL_OFFSET_TABLE_-(.LZZZ1-.)) >> 16) & 0xffff), r2\n\t"
  50. "shori\t((datalabel _GLOBAL_OFFSET_TABLE_-(.LZZZ1-.)) & 0xffff), r2\n\t"
  51. ".LZZZ1:\tptrel/u r2, tr0\n\t"
  52. "movi\t32768, r1\n\t"
  53. "gettr\ttr0, r2\n\t"
  54. "sub\tr2, r1, %0\n\t"
  55. : "=r" (got)
  56. : /* no inputs */
  57. : "r1", "r2", "tr0"
  58. );
  59. return *got;
  60. }
  61. /* Return the run-time load address of the shared object. */
  62. static __always_inline Elf32_Addr elf_machine_load_address(void)
  63. {
  64. Elf32_Addr addr;
  65. __asm__ (
  66. "movi\t(((datalabel _GLOBAL_OFFSET_TABLE_-(.LZZZ2-.)) >> 16) & 0xffff), r0\n\t"
  67. "shori\t((datalabel _GLOBAL_OFFSET_TABLE_-(.LZZZ2-.)) & 0xffff), r0\n\t"
  68. ".LZZZ2:\tptrel/u r0, tr0\n\t"
  69. "movi\t(((_dl_start@GOTOFF) >> 16) & 0xffff), r2\n\t"
  70. "shori\t((_dl_start@GOTOFF) & 0xffff), r2\n\t"
  71. "gettr\ttr0, r0\n\t"
  72. "add\tr2, r0, r2\n\t"
  73. "movi\t(((_dl_start@GOT) >> 16) & 0xffff), r1\n\t"
  74. "shori\t((_dl_start@GOT) & 0xffff), r1\n\t"
  75. "ldx.l\tr1, r0, r1\n\t"
  76. "sub\tr2, r1, %0\n\t"
  77. : "=r" (addr)
  78. : /* no inputs */
  79. : "r0", "r1", "r2", "tr0"
  80. );
  81. return addr;
  82. }
  83. /*
  84. * XXX: As we don't need to worry about r25 clobbering, we could probably
  85. * get away with inlining {st,ld}{x,}.l and friends here instead and
  86. * forego gcc's idea of code generation.
  87. */
  88. #define COPY_UNALIGNED_WORD(swp, twp, align) \
  89. { \
  90. void *__s = (swp), *__t = (twp); \
  91. unsigned char *__s1 = __s, *__t1 = __t; \
  92. unsigned short *__s2 = __s, *__t2 = __t; \
  93. unsigned long *__s4 = __s, *__t4 = __t; \
  94. \
  95. switch ((align)) { \
  96. case 0: \
  97. *__t4 = *__s4; \
  98. break; \
  99. case 2: \
  100. *__t2++ = *__s2++; \
  101. *__t2 = *__s2; \
  102. break; \
  103. default: \
  104. *__t1++ = *__s1++; \
  105. *__t1++ = *__s1++; \
  106. *__t1++ = *__s1++; \
  107. *__t1 = *__s1; \
  108. break; \
  109. } \
  110. }
  111. static __always_inline void
  112. elf_machine_relative(Elf32_Addr load_off, const Elf32_Addr rel_addr,
  113. Elf32_Word relative_count)
  114. {
  115. Elf32_Addr value, word;
  116. Elf32_Rela *rpnt = (void *)rel_addr;
  117. int reloc_type = ELF32_R_TYPE(rpnt->r_info);
  118. do {
  119. Elf32_Addr *const reloc_addr =
  120. (void *)(load_off + rpnt->r_offset);
  121. int align = (int)reloc_addr & 3;
  122. switch (reloc_type) {
  123. case R_SH_RELATIVE_LOW16:
  124. COPY_UNALIGNED_WORD(reloc_addr, &word, align);
  125. word &= ~0x3fffc00;
  126. value = (rpnt->r_addend + load_off);
  127. word |= (value & 0xffff) << 10;
  128. COPY_UNALIGNED_WORD(&word, reloc_addr, align);
  129. break;
  130. case R_SH_RELATIVE_MEDLOW16:
  131. COPY_UNALIGNED_WORD(reloc_addr, &word, align);
  132. word &= ~0x3fffc00;
  133. value = (rpnt->r_addend + load_off) >> 16;
  134. word |= (value & 0xffff) << 10;
  135. COPY_UNALIGNED_WORD(&word, reloc_addr, align);
  136. break;
  137. default:
  138. if (rpnt->r_addend) {
  139. value = load_off + rpnt->r_addend;
  140. } else {
  141. COPY_UNALIGNED_WORD(reloc_addr, &value, align);
  142. value += load_off;
  143. }
  144. COPY_UNALIGNED_WORD(&value, reloc_addr, align);
  145. break;
  146. }
  147. rpnt++;
  148. } while (--relative_count);
  149. #undef COPY_UNALIGNED_WORD
  150. }