dl-machine.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. /* Machine-dependent ELF dynamic relocation inline functions. OpenRISC version.
  2. Copyright (C) 2022-2026 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <https://www.gnu.org/licenses/>. */
  15. #ifndef dl_machine_h
  16. #define dl_machine_h
  17. #define ELF_MACHINE_NAME "or1k"
  18. #include <sys/cdefs.h>
  19. #include <sys/param.h>
  20. #include <tls.h>
  21. #include <dl-irel.h>
  22. #include <dl-static-tls.h>
  23. #include <dl-machine-rel.h>
  24. /* Return nonzero iff ELF header is compatible with the running host. */
  25. static inline int __attribute__ ((unused))
  26. elf_machine_matches_host (const Elf32_Ehdr *ehdr)
  27. {
  28. return ehdr->e_machine == EM_OPENRISC;
  29. }
  30. static inline Elf32_Addr *
  31. or1k_get_got (void)
  32. {
  33. Elf32_Addr *got;
  34. asm ("l.jal 0x8\n"
  35. " l.movhi %0, gotpchi(_GLOBAL_OFFSET_TABLE_-4)\n"
  36. "l.ori %0, %0, gotpclo(_GLOBAL_OFFSET_TABLE_+0)\n"
  37. "l.add %0, %0, r9\n"
  38. : "=r" (got) : : "r9");
  39. return got;
  40. }
  41. /* Return the link-time address of _DYNAMIC. Conveniently, this is the
  42. first element of the GOT. */
  43. static inline Elf32_Addr
  44. elf_machine_dynamic (void)
  45. {
  46. Elf32_Addr *got = or1k_get_got ();
  47. return *got;
  48. }
  49. /* Return the run-time load address of the shared object. */
  50. static inline Elf32_Addr
  51. elf_machine_load_address (void)
  52. {
  53. /* Compute the difference between the runtime address of _DYNAMIC as seen
  54. by a GOTOFF reference, and the link-time address found in the special
  55. unrelocated first GOT entry. */
  56. Elf32_Addr dyn;
  57. Elf32_Addr *got = or1k_get_got ();
  58. asm ("l.movhi %0, gotoffhi(_DYNAMIC);"
  59. "l.ori %0, %0, gotofflo(_DYNAMIC);"
  60. "l.add %0, %0, %1;"
  61. : "=&r"(dyn) : "r"(got));
  62. return dyn - *got;
  63. }
  64. /* Initial entry point code for the dynamic linker. The function _dl_start
  65. is the real entry point; it's return value is the user program's entry
  66. point.
  67. Code is really located in dl-start.S, just tell the linker that it
  68. exists. */
  69. #define RTLD_START asm (".globl _dl_start");
  70. /* ELF_RTYPE_CLASS_PLT iff TYPE describes relocation of a PLT entry or
  71. TLS variable, so undefined references should not be allowed to
  72. define the value.
  73. ELF_RTYPE_CLASS_NOCOPY iff TYPE should not be allowed to resolve to one
  74. of the main executable's symbols, as for a COPY reloc. */
  75. #define elf_machine_type_class(type) \
  76. (((type) == R_OR1K_JMP_SLOT \
  77. || (type) == R_OR1K_TLS_DTPMOD \
  78. || (type) == R_OR1K_TLS_DTPOFF \
  79. || (type) == R_OR1K_TLS_TPOFF) * ELF_RTYPE_CLASS_PLT \
  80. | ((type) == R_OR1K_COPY) * ELF_RTYPE_CLASS_COPY)
  81. /* A reloc type used for ld.so cmdline arg lookups to reject PLT entries. */
  82. #define ELF_MACHINE_JMP_SLOT R_OR1K_JMP_SLOT
  83. #define ARCH_LA_PLTENTER or1k_gnu_pltenter
  84. #define ARCH_LA_PLTEXIT or1k_gnu_pltexit
  85. /* Set up the loaded object described by L so its unrelocated PLT
  86. entries will jump to the on-demand fixup code in dl-runtime.c. */
  87. static inline int __attribute__ ((unused, always_inline))
  88. elf_machine_runtime_setup (struct link_map *l, struct r_scope_elem *scope[],
  89. int lazy, int profile)
  90. {
  91. ElfW(Addr) *pltgot;
  92. extern void _dl_runtime_resolve (ElfW(Word));
  93. extern void _dl_runtime_profile (ElfW(Word));
  94. if (l->l_info[DT_JMPREL] && lazy)
  95. {
  96. pltgot = (ElfW(Addr) *) D_PTR (l, l_info[DT_PLTGOT]);
  97. /* Fill in initial entries of the plt */
  98. /* Register the link_map address in the plt at pltgot[1].
  99. This will also be used in the resolver for accessing the
  100. link_map structure. */
  101. pltgot[1] = (ElfW(Addr)) l;
  102. /* This function will get called to fix up the GOT entry and
  103. then jump to the resolved address. */
  104. pltgot[2] = (ElfW(Addr)) &_dl_runtime_resolve;
  105. }
  106. return lazy;
  107. }
  108. /* Mask identifying addresses reserved for the user program,
  109. where the dynamic linker should not map anything. */
  110. #define ELF_MACHINE_USER_ADDRESS_MASK 0xf8000000UL
  111. /* We define an initialization functions. This is called very early in
  112. _dl_sysdep_start. */
  113. #define DL_PLATFORM_INIT dl_platform_init ()
  114. static inline void __attribute__ ((unused))
  115. dl_platform_init (void)
  116. {
  117. if (GLRO(dl_platform) != NULL && *GLRO(dl_platform) == '\0')
  118. /* Avoid an empty string which would disturb us. */
  119. GLRO(dl_platform) = NULL;
  120. }
  121. static inline ElfW(Addr)
  122. elf_machine_fixup_plt (struct link_map *map, lookup_t t,
  123. const ElfW(Sym) *refsym, const ElfW(Sym) *sym,
  124. const ElfW(Rela) *reloc,
  125. ElfW(Addr) *reloc_addr, ElfW(Addr) value)
  126. {
  127. return *reloc_addr = value;
  128. }
  129. /* Return the final value of a plt relocation. */
  130. static inline Elf32_Addr
  131. elf_machine_plt_value (struct link_map *map, const Elf32_Rela *reloc,
  132. Elf32_Addr value)
  133. {
  134. return value + reloc->r_addend;
  135. }
  136. #endif /* !dl_machine_h */
  137. #ifdef RESOLVE_MAP
  138. /* Perform the relocation specified by RELOC and SYM (which is fully resolved).
  139. MAP is the object containing the reloc. */
  140. static inline void
  141. __attribute ((always_inline))
  142. elf_machine_rela (struct link_map *map, struct r_scope_elem *scope[],
  143. const ElfW(Rela) *reloc, const ElfW(Sym) *sym,
  144. const struct r_found_version *version,
  145. void *const reloc_addr_arg, int skip_ifunc)
  146. {
  147. Elf32_Addr *const reloc_addr = reloc_addr_arg;
  148. const unsigned int r_type = ELF32_R_TYPE (reloc->r_info);
  149. if (__glibc_unlikely (r_type == R_OR1K_NONE))
  150. return;
  151. else
  152. {
  153. const Elf32_Sym *const refsym = sym;
  154. struct link_map *sym_map = RESOLVE_MAP (map, scope, &sym, version,
  155. r_type);
  156. Elf32_Addr value = SYMBOL_ADDRESS (sym_map, sym, true);
  157. if (sym != NULL
  158. && __glibc_unlikely (ELFW(ST_TYPE) (sym->st_info) == STT_GNU_IFUNC)
  159. && __glibc_likely (sym->st_shndx != SHN_UNDEF)
  160. && __glibc_likely (!skip_ifunc))
  161. value = elf_ifunc_invoke (value);
  162. switch (r_type)
  163. {
  164. case R_OR1K_COPY:
  165. if (sym == NULL)
  166. /* This can happen in trace mode if an object could not be
  167. found. */
  168. break;
  169. if (__glibc_unlikely (sym->st_size > refsym->st_size)
  170. || (__glibc_unlikely (sym->st_size < refsym->st_size)
  171. && GLRO(dl_verbose)))
  172. {
  173. const char *strtab;
  174. strtab = (const char *) D_PTR (map, l_info[DT_STRTAB]);
  175. _dl_error_printf ("\
  176. %s: Symbol `%s' has different size in shared object, consider re-linking\n",
  177. rtld_progname ?: "<program name unknown>",
  178. strtab + refsym->st_name);
  179. }
  180. memcpy (reloc_addr_arg, (void *) value,
  181. MIN (sym->st_size, refsym->st_size));
  182. break;
  183. case R_OR1K_32:
  184. /* Support relocations on mis-aligned offsets. */
  185. value += reloc->r_addend;
  186. memcpy (reloc_addr_arg, &value, 4);
  187. break;
  188. case R_OR1K_GLOB_DAT:
  189. case R_OR1K_JMP_SLOT:
  190. *reloc_addr = value + reloc->r_addend;
  191. break;
  192. case R_OR1K_TLS_DTPMOD:
  193. # ifdef RTLD_BOOTSTRAP
  194. /* During startup the dynamic linker is always the module
  195. with index 1. */
  196. *reloc_addr = 1;
  197. # else
  198. if (sym_map != NULL)
  199. *reloc_addr = sym_map->l_tls_modid;
  200. # endif
  201. break;
  202. case R_OR1K_TLS_DTPOFF:
  203. # ifndef RTLD_BOOTSTRAP
  204. *reloc_addr = (sym == NULL ? 0 : sym->st_value) + reloc->r_addend;
  205. # endif
  206. break;
  207. case R_OR1K_TLS_TPOFF:
  208. # ifdef RTLD_BOOTSTRAP
  209. *reloc_addr = sym->st_value + reloc->r_addend +
  210. map->l_tls_offset - TLS_TCB_SIZE;
  211. # else
  212. if (sym_map != NULL)
  213. {
  214. CHECK_STATIC_TLS (map, sym_map);
  215. *reloc_addr = sym->st_value + reloc->r_addend +
  216. sym_map->l_tls_offset - TLS_TCB_SIZE;
  217. }
  218. # endif
  219. break;
  220. default:
  221. _dl_reloc_bad_type (map, r_type, 0);
  222. break;
  223. }
  224. }
  225. }
  226. static inline void
  227. __attribute__ ((always_inline))
  228. elf_machine_rela_relative (Elf32_Addr l_addr, const Elf32_Rela *reloc,
  229. void *const reloc_addr_arg)
  230. {
  231. Elf32_Addr *const reloc_addr = reloc_addr_arg;
  232. *reloc_addr = l_addr + reloc->r_addend;
  233. }
  234. static inline void
  235. __attribute__ ((always_inline))
  236. elf_machine_lazy_rel (struct link_map *map, struct r_scope_elem *scope[],
  237. ElfW(Addr) l_addr, const ElfW(Rela) *reloc,
  238. int skip_ifunc)
  239. {
  240. Elf32_Addr *const reloc_addr = (void *) (l_addr + reloc->r_offset);
  241. const unsigned int r_type = ELF32_R_TYPE (reloc->r_info);
  242. if (__glibc_likely (r_type == R_OR1K_JMP_SLOT))
  243. *reloc_addr += l_addr;
  244. else if (__glibc_unlikely (r_type == R_OR1K_NONE))
  245. return;
  246. else
  247. _dl_reloc_bad_type (map, r_type, 1);
  248. }
  249. #endif /* RESOLVE_MAP */