dl-sysdep.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. #endif
  74. #undef MAGIC2
  75. struct elf_resolve;
  76. extern unsigned long _dl_linux_resolver(struct elf_resolve * tpnt,
  77. unsigned int plt_pc);
  78. extern unsigned __udivmodsi4(unsigned, unsigned) attribute_hidden;
  79. #ifdef __A7__
  80. /* using "C" causes an indirection via __umodsi3 -> __udivmodsi4 */
  81. #define do_rem(result, n, base) ((result) = \
  82. \
  83. __builtin_constant_p (base) ? (n) % (unsigned) (base) : \
  84. __extension__ ({ \
  85. register unsigned r1 __asm__ ("r1") = (base); \
  86. \
  87. __asm__("bl.d @__udivmodsi4` mov r0,%1" \
  88. : "=r" (r1) \
  89. : "r" (n), "r" (r1) \
  90. : "r0", "r2", "r3", "r4", "lp_count", "blink", "cc"); \
  91. \
  92. r1; \
  93. }) \
  94. )
  95. #elif defined(__HS__)
  96. /* ARCv2 has hardware assisted divide/mod */
  97. #define do_rem(result, n, base) ((result) = (n) % (unsigned) (base))
  98. #endif
  99. /* ELF_RTYPE_CLASS_PLT iff TYPE describes relocation of a PLT entry or
  100. TLS variable so PLT entries should not be allowed to define the value.
  101. ELF_RTYPE_CLASS_NOCOPY iff TYPE should not be allowed to resolve to one
  102. of the main executable's symbols, as for a COPY reloc. */
  103. #define elf_machine_type_class(type) \
  104. ((((type) == R_ARC_JMP_SLOT || (type) == R_ARC_TLS_DTPMOD || \
  105. (type) == R_ARC_TLS_DTPOFF || (type) == R_ARC_TLS_TPOFF) \
  106. * ELF_RTYPE_CLASS_PLT) \
  107. | (((type) == R_ARC_COPY) * ELF_RTYPE_CLASS_COPY))
  108. /*
  109. * Get build time address of .dynamic as setup in GOT[0]
  110. * This is called very early in _dl_start() so it has not been relocated to
  111. * runtime value
  112. */
  113. static __always_inline Elf32_Addr elf_machine_dynamic(void)
  114. {
  115. extern const Elf32_Addr _GLOBAL_OFFSET_TABLE_[] attribute_hidden;
  116. return _GLOBAL_OFFSET_TABLE_[0];
  117. }
  118. /* Return the run-time load address of the shared object. */
  119. static __always_inline Elf32_Addr elf_machine_load_address(void)
  120. {
  121. /* To find the loadaddr we subtract the runtime addr of a non-local symbol
  122. * say _DYNAMIC from it's build-time addr.
  123. * N.B., gotpc loads get optimized by the linker if it finds the symbol
  124. * is resolved locally.
  125. * A more robust - and efficient - solution would be to use a symbol
  126. * set by the linker. To make it actually save space, we'd have to
  127. * suppress the unwanted text relocation in the linked dso, though.
  128. * (I.e. in ldso.so.*, though it's just another dso as far as bfd/ld
  129. * are concerned.)
  130. */
  131. Elf32_Addr addr, tmp;
  132. __asm__ (
  133. "ld %1, [pcl, _dl_start@gotpc] ;build addr of _DYNAMIC" "\n"
  134. "add %0, pcl, _dl_start@pcl ;runtime addr of _DYNAMIC" "\n"
  135. "sub %0, %0, %1 ;delta" "\n"
  136. : "=&r" (addr), "=r"(tmp)
  137. );
  138. return addr;
  139. }
  140. static __always_inline void
  141. elf_machine_relative (Elf32_Addr load_off, const Elf32_Addr rel_addr,
  142. Elf32_Word relative_count)
  143. {
  144. Elf32_Rela * rpnt = (void *) rel_addr;
  145. --rpnt;
  146. do {
  147. Elf32_Addr *const reloc_addr = (void *) (load_off + (++rpnt)->r_offset);
  148. *reloc_addr += load_off;
  149. } while (--relative_count);
  150. }