dl-sysdep.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Various assmbly language/system dependent hacks that are required
  4. * so that we can minimize the amount of platform specific code.
  5. * Copyright (C) 2000-2004 by Erik Andersen <andersen@codepoet.org>
  6. */
  7. /* Define this if the system uses RELOCA. */
  8. #define ELF_USES_RELOCA
  9. #include <elf.h>
  10. /*
  11. * Initialization sequence for a GOT. For the Sparc, this points to the
  12. * PLT, and we need to initialize a couple of the slots. The PLT should
  13. * look like:
  14. *
  15. * save %sp, -64, %sp
  16. * call _dl_linux_resolve
  17. * nop
  18. * .word implementation_dependent
  19. */
  20. #define INIT_GOT(GOT_BASE,MODULE) \
  21. { \
  22. GOT_BASE[0] = 0x9de3bfc0; /* save %sp, -64, %sp */ \
  23. GOT_BASE[1] = 0x40000000 | (((unsigned int) _dl_linux_resolve - (unsigned int) GOT_BASE - 4) >> 2); \
  24. GOT_BASE[2] = 0x01000000; /* nop */ \
  25. GOT_BASE[3] = (int) MODULE; \
  26. }
  27. /* Here we define the magic numbers that this dynamic loader should accept
  28. * Note that SPARCV9 doesn't use EM_SPARCV9 since the userland is still 32-bit.
  29. */
  30. #if defined(__sparc_v9__) || defined(__sparc_v8__)
  31. #define MAGIC1 EM_SPARC32PLUS
  32. #else
  33. #define MAGIC1 EM_SPARC
  34. #endif
  35. #undef MAGIC2
  36. /* Used for error messages */
  37. #define ELF_TARGET "sparc"
  38. struct elf_resolve;
  39. unsigned long _dl_linux_resolver(struct elf_resolve * tpnt, int reloc_entry);
  40. /*
  41. * Define this if you want a dynamic loader that works on Solaris.
  42. */
  43. #ifndef COMPILE_ASM
  44. /* Cheap modulo implementation, taken from arm/ld_sysdep.h. */
  45. static __inline__ unsigned long
  46. sparc_mod(unsigned long m, unsigned long p)
  47. {
  48. unsigned long i, t, inc;
  49. i = p;
  50. t = 0;
  51. while (!(i & (1 << 31))) {
  52. i <<= 1;
  53. t++;
  54. }
  55. t--;
  56. for (inc = t; inc > 2; inc--) {
  57. i = p << inc;
  58. if (i & (1 << 31))
  59. break;
  60. while (m >= i) {
  61. m -= i;
  62. i <<= 1;
  63. if (i & (1 << 31))
  64. break;
  65. if (i < p)
  66. break;
  67. }
  68. }
  69. while (m >= p)
  70. m -= p;
  71. return m;
  72. }
  73. #define do_rem(result, n, base) ((result) = sparc_mod(n, base))
  74. #endif
  75. /* 4096 bytes alignment */
  76. #if defined(__sparc_v9__)
  77. /* ...but 8192 is required for mmap() on sparc64 kernel */
  78. #define PAGE_ALIGN 0xffffe000
  79. #define ADDR_ALIGN 0x1fff
  80. #define OFFS_ALIGN 0x7fffe000
  81. #elif defined(__sparc_v8__)
  82. #define PAGE_ALIGN 0xfffff000
  83. #define ADDR_ALIGN 0xfff
  84. #define OFFS_ALIGN 0x7ffff000
  85. #endif
  86. /* ELF_RTYPE_CLASS_PLT iff TYPE describes relocation of a PLT entry, so
  87. PLT entries should not be allowed to define the value.
  88. ELF_RTYPE_CLASS_NOCOPY iff TYPE should not be allowed to resolve to one
  89. of the main executable's symbols, as for a COPY reloc. */
  90. #define elf_machine_type_class(type) \
  91. ((((type) == R_SPARC_JMP_SLOT) * ELF_RTYPE_CLASS_PLT) \
  92. | (((type) == R_SPARC_COPY) * ELF_RTYPE_CLASS_COPY))
  93. /* The SPARC overlaps DT_RELA and DT_PLTREL. */
  94. #define ELF_MACHINE_PLTREL_OVERLAP 1
  95. /* We have to do this because elf_machine_{dynamic,load_address} can be
  96. invoked from functions that have no GOT references, and thus the compiler
  97. has no obligation to load the PIC register. */
  98. #define LOAD_PIC_REG(PIC_REG) \
  99. do { register Elf32_Addr pc __asm__("o7"); \
  100. __asm__("sethi %%hi(_GLOBAL_OFFSET_TABLE_-4), %1\n\t" \
  101. "call 1f\n\t" \
  102. "add %1, %%lo(_GLOBAL_OFFSET_TABLE_+4), %1\n" \
  103. "1:\tadd %1, %0, %1" \
  104. : "=r" (pc), "=r" (PIC_REG)); \
  105. } while (0)
  106. /* Return the link-time address of _DYNAMIC. Conveniently, this is the
  107. first element of the GOT. This must be inlined in a function which
  108. uses global data. */
  109. static __inline__ Elf32_Addr
  110. elf_machine_dynamic (void)
  111. {
  112. register Elf32_Addr *got __asm__ ("%l7");
  113. LOAD_PIC_REG (got);
  114. return *got;
  115. }
  116. /* Return the run-time load address of the shared object. */
  117. static __inline__ Elf32_Addr
  118. elf_machine_load_address (void)
  119. {
  120. register Elf32_Addr *pc __asm__ ("%o7"), *got __asm ("%l7");
  121. __asm__ ("sethi %%hi(_GLOBAL_OFFSET_TABLE_-4), %1\n\t"
  122. "call 1f\n\t"
  123. " add %1, %%lo(_GLOBAL_OFFSET_TABLE_+4), %1\n\t"
  124. "call _DYNAMIC\n\t"
  125. "call _GLOBAL_OFFSET_TABLE_\n"
  126. "1:\tadd %1, %0, %1\n\t" : "=r" (pc), "=r" (got));
  127. /* got is now l_addr + _GLOBAL_OFFSET_TABLE_
  128. *got is _DYNAMIC
  129. pc[2]*4 is l_addr + _DYNAMIC - (long)pc - 8
  130. pc[3]*4 is l_addr + _GLOBAL_OFFSET_TABLE_ - (long)pc - 12 */
  131. return (Elf32_Addr) got - *got + (pc[2] - pc[3]) * 4 - 4;
  132. }
  133. static __inline__ void
  134. elf_machine_relative (Elf32_Addr load_off, const Elf32_Addr rel_addr,
  135. Elf32_Word relative_count)
  136. {
  137. Elf32_Rela * rpnt = (void *)rel_addr;
  138. --rpnt;
  139. do {
  140. Elf32_Addr *const reloc_addr = (void *) (load_off + (++rpnt)->r_offset);
  141. *reloc_addr = load_off + rpnt->r_addend;
  142. } while (--relative_count);
  143. }