dl-sysdep.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. * Copyright (C) 2013 Synopsys, Inc. (www.synopsys.com)
  3. *
  4. * Licensed under the LGPL v2.1 or later, see the file COPYING.LIB in this tarball.
  5. */
  6. #include "elf.h"
  7. /*
  8. * Define this if the system uses RELOCA.
  9. */
  10. #define ELF_USES_RELOCA
  11. /*
  12. * Dynamic Linking ABI for ARCompact ISA
  13. *
  14. * PLT
  15. * --------------------------------
  16. * | ld r11, [pcl, off-to-GOT[1] | 0 (20 bytes)
  17. * | | 4
  18. * plt0 | ld r10, [pcl, off-to-GOT[2] | 8
  19. * | | 12
  20. * | j [r10] | 16
  21. * --------------------------------
  22. * | Base address of GOT | 20
  23. * --------------------------------
  24. * | ld r12, [pcl, off-to-GOT[3] | 24 (12 bytes each)
  25. * plt1 | |
  26. * | j_s.d [r12] | 32
  27. * | mov_s r12, pcl | 34
  28. * --------------------------------
  29. * | | 36
  30. * ~ ~
  31. * ~ ~
  32. * | |
  33. * --------------------------------
  34. *
  35. * GOT
  36. * --------------
  37. * | [0] |
  38. * --------------
  39. * | [1] | Module info - setup by ldso
  40. * --------------
  41. * | [2] | resolver entry point
  42. * --------------
  43. * | [3] |
  44. * | ... | Runtime address for function symbols
  45. * | [f] |
  46. * --------------
  47. * | [f+1] |
  48. * | ... | Runtime address for data symbols
  49. * | [last] |
  50. * --------------
  51. */
  52. /*
  53. * Initialization sequence for a GOT.
  54. * Caller elf_resolve() seeds @GOT_BASE from DT_PLTGOT - which essentially is
  55. * pointer to first PLT entry. The actual GOT base is 5th word in PLT
  56. *
  57. */
  58. #define INIT_GOT(GOT_BASE,MODULE) \
  59. do { \
  60. unsigned long *__plt_base = (unsigned long *)GOT_BASE; \
  61. GOT_BASE = (unsigned long *)(__plt_base[5] + \
  62. (unsigned long)MODULE->loadaddr); \
  63. GOT_BASE[1] = (unsigned long) MODULE; \
  64. GOT_BASE[2] = (unsigned long) _dl_linux_resolve; \
  65. } while(0)
  66. /* Here we define the magic numbers that this dynamic loader should accept */
  67. #ifdef __A7__
  68. #define MAGIC1 EM_ARCOMPACT
  69. #define ELF_TARGET "ARCompact" /* For error messages */
  70. #elif defined(__HS__)
  71. #define MAGIC1 EM_ARCV2
  72. #define ELF_TARGET "ARCv2" /* For error messages */
  73. #elif defined(__ARC64_ARCH32__)
  74. #define MAGIC1 EM_ARCV3_32
  75. #define ELF_TARGET "ARCv3_32" /* For error messages */
  76. #endif
  77. #undef MAGIC2
  78. struct elf_resolve;
  79. extern unsigned long _dl_linux_resolver(struct elf_resolve * tpnt,
  80. unsigned int plt_pc);
  81. extern unsigned __udivmodsi4(unsigned, unsigned) attribute_hidden;
  82. #ifdef __A7__
  83. /* using "C" causes an indirection via __umodsi3 -> __udivmodsi4 */
  84. #define do_rem(result, n, base) ((result) = \
  85. \
  86. __builtin_constant_p (base) ? (n) % (unsigned) (base) : \
  87. __extension__ ({ \
  88. register unsigned r1 __asm__ ("r1") = (base); \
  89. \
  90. __asm__("bl.d @__udivmodsi4` mov r0,%1" \
  91. : "=r" (r1) \
  92. : "r" (n), "r" (r1) \
  93. : "r0", "r2", "r3", "r4", "lp_count", "blink", "cc"); \
  94. \
  95. r1; \
  96. }) \
  97. )
  98. #elif defined(__HS__)
  99. /* ARCv2 has hardware assisted divide/mod */
  100. #define do_rem(result, n, base) ((result) = (n) % (unsigned) (base))
  101. #endif
  102. /* ELF_RTYPE_CLASS_PLT iff TYPE describes relocation of a PLT entry or
  103. TLS variable so PLT entries should not be allowed to define the value.
  104. ELF_RTYPE_CLASS_NOCOPY iff TYPE should not be allowed to resolve to one
  105. of the main executable's symbols, as for a COPY reloc. */
  106. #define elf_machine_type_class(type) \
  107. ((((type) == R_ARC_JMP_SLOT || (type) == R_ARC_TLS_DTPMOD || \
  108. (type) == R_ARC_TLS_DTPOFF || (type) == R_ARC_TLS_TPOFF) \
  109. * ELF_RTYPE_CLASS_PLT) \
  110. | (((type) == R_ARC_COPY) * ELF_RTYPE_CLASS_COPY))
  111. /*
  112. * Get build time address of .dynamic as setup in GOT[0]
  113. * This is called very early in _dl_start() so it has not been relocated to
  114. * runtime value
  115. */
  116. static __always_inline Elf32_Addr elf_machine_dynamic(void)
  117. {
  118. extern const Elf32_Addr _GLOBAL_OFFSET_TABLE_[] attribute_hidden;
  119. return _GLOBAL_OFFSET_TABLE_[0];
  120. }
  121. /* Return the run-time load address of the shared object. */
  122. static __always_inline Elf32_Addr elf_machine_load_address(void)
  123. {
  124. /* To find the loadaddr we subtract the runtime addr of a non-local symbol
  125. * say _DYNAMIC from it's build-time addr.
  126. * N.B., gotpc loads get optimized by the linker if it finds the symbol
  127. * is resolved locally.
  128. * A more robust - and efficient - solution would be to use a symbol
  129. * set by the linker. To make it actually save space, we'd have to
  130. * suppress the unwanted text relocation in the linked dso, though.
  131. * (I.e. in ldso.so.*, though it's just another dso as far as bfd/ld
  132. * are concerned.)
  133. */
  134. Elf32_Addr addr, tmp;
  135. __asm__ (
  136. "ld %1, [pcl, _dl_start@gotpc] ;build addr of _DYNAMIC" "\n"
  137. "add %0, pcl, _dl_start@pcl ;runtime addr of _DYNAMIC" "\n"
  138. "sub %0, %0, %1 ;delta" "\n"
  139. : "=&r" (addr), "=r"(tmp)
  140. );
  141. return addr;
  142. }
  143. static __always_inline void
  144. elf_machine_relative (Elf32_Addr load_off, const Elf32_Addr rel_addr,
  145. Elf32_Word relative_count)
  146. {
  147. Elf32_Rela * rpnt = (void *) rel_addr;
  148. --rpnt;
  149. do {
  150. Elf32_Addr *const reloc_addr = (void *) (load_off + (++rpnt)->r_offset);
  151. *reloc_addr += load_off;
  152. } while (--relative_count);
  153. }