dl-sysdep.h 5.3 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 the runtime address of GOT[0]
  110. */
  111. static __always_inline Elf32_Addr elf_machine_dynamic(void)
  112. {
  113. Elf32_Addr dyn;
  114. __asm__("ld %0,[pcl,_DYNAMIC@gotpc]\n\t" : "=r" (dyn));
  115. return dyn;
  116. }
  117. /* Return the run-time load address of the shared object. */
  118. static __always_inline Elf32_Addr elf_machine_load_address(void)
  119. {
  120. /* To find the loadaddr we subtract the runtime addr of a non-local symbol
  121. * say _DYNAMIC from it's build-time addr.
  122. * N.B., gotpc loads get optimized by the linker if it finds the symbol
  123. * is resolved locally.
  124. * A more robust - and efficient - solution would be to use a symbol
  125. * set by the linker. To make it actually save space, we'd have to
  126. * suppress the unwanted text relocation in the linked dso, though.
  127. * (I.e. in ldso.so.*, though it's just another dso as far as bfd/ld
  128. * are concerned.)
  129. */
  130. Elf32_Addr addr, tmp;
  131. __asm__ (
  132. "ld %1, [pcl, _DYNAMIC@gotpc] ;build addr of _DYNAMIC" "\n"
  133. "add %0, pcl, _DYNAMIC@pcl ;runtime addr of _DYNAMIC" "\n"
  134. "sub %0, %0, %1 ;delta" "\n"
  135. : "=&r" (addr), "=r"(tmp)
  136. );
  137. return addr;
  138. }
  139. static __always_inline void
  140. elf_machine_relative (Elf32_Addr load_off, const Elf32_Addr rel_addr,
  141. Elf32_Word relative_count)
  142. {
  143. Elf32_Rela * rpnt = (void *) rel_addr;
  144. --rpnt;
  145. do {
  146. Elf32_Addr *const reloc_addr = (void *) (load_off + (++rpnt)->r_offset);
  147. *reloc_addr += load_off;
  148. } while (--relative_count);
  149. }