dl-machine.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. /* Machine-dependent ELF dynamic relocation inline functions. RISC-V version.
  2. Copyright (C) 2011-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 "RISC-V"
  18. #include <entry.h>
  19. #include <elf/elf.h>
  20. #include <sys/asm.h>
  21. #include <dl-tls.h>
  22. #include <dl-irel.h>
  23. #include <dl-static-tls.h>
  24. #include <dl-machine-rel.h>
  25. #ifndef _RTLD_PROLOGUE
  26. # define _RTLD_PROLOGUE(entry) \
  27. ".globl\t" __STRING (entry) "\n\t" \
  28. ".type\t" __STRING (entry) ", @function\n" \
  29. __STRING (entry) ":\n\t"
  30. #endif
  31. #ifndef _RTLD_EPILOGUE
  32. # define _RTLD_EPILOGUE(entry) \
  33. ".size\t" __STRING (entry) ", . - " __STRING (entry) "\n\t"
  34. #endif
  35. #define ELF_MACHINE_JMP_SLOT R_RISCV_JUMP_SLOT
  36. #define elf_machine_type_class(type) \
  37. ((ELF_RTYPE_CLASS_PLT * ((type) == ELF_MACHINE_JMP_SLOT \
  38. || (__WORDSIZE == 32 && (type) == R_RISCV_TLS_DTPREL32) \
  39. || (__WORDSIZE == 32 && (type) == R_RISCV_TLS_DTPMOD32) \
  40. || (__WORDSIZE == 32 && (type) == R_RISCV_TLS_TPREL32) \
  41. || (__WORDSIZE == 64 && (type) == R_RISCV_TLS_DTPREL64) \
  42. || (__WORDSIZE == 64 && (type) == R_RISCV_TLS_DTPMOD64) \
  43. || (__WORDSIZE == 64 && (type) == R_RISCV_TLS_TPREL64))) \
  44. | (ELF_RTYPE_CLASS_COPY * ((type) == R_RISCV_COPY)))
  45. /* Return nonzero iff ELF header is compatible with the running host. */
  46. static inline int __attribute_used__
  47. elf_machine_matches_host (const ElfW(Ehdr) *ehdr)
  48. {
  49. /* We can only run RISC-V binaries. */
  50. if (ehdr->e_machine != EM_RISCV)
  51. return 0;
  52. /* Ensure the library's floating-point ABI matches that of the running
  53. system. For now we don't support mixing XLEN, so there's no need (or way)
  54. to check it matches. */
  55. #ifdef __riscv_float_abi_double
  56. if ((ehdr->e_flags & EF_RISCV_FLOAT_ABI) != EF_RISCV_FLOAT_ABI_DOUBLE)
  57. return 0;
  58. #else
  59. if ((ehdr->e_flags & EF_RISCV_FLOAT_ABI) != EF_RISCV_FLOAT_ABI_SOFT)
  60. return 0;
  61. #endif
  62. return 1;
  63. }
  64. /* Return the run-time load address of the shared object. */
  65. static inline ElfW(Addr)
  66. elf_machine_load_address (void)
  67. {
  68. extern const ElfW(Ehdr) __ehdr_start attribute_hidden;
  69. return (ElfW(Addr)) &__ehdr_start;
  70. }
  71. /* Return the link-time address of _DYNAMIC. */
  72. static inline ElfW(Addr)
  73. elf_machine_dynamic (void)
  74. {
  75. extern ElfW(Dyn) _DYNAMIC[] attribute_hidden;
  76. return (ElfW(Addr)) _DYNAMIC - elf_machine_load_address ();
  77. }
  78. #define STRINGXP(X) __STRING (X)
  79. #define STRINGXV(X) STRINGV_ (X)
  80. #define STRINGV_(...) # __VA_ARGS__
  81. /* Initial entry point code for the dynamic linker.
  82. The C function `_dl_start' is the real entry point;
  83. its return value is the user program's entry point. */
  84. #define RTLD_START asm (\
  85. ".text\n\
  86. " _RTLD_PROLOGUE (ENTRY_POINT) "\
  87. mv a0, sp\n\
  88. jal _dl_start\n\
  89. " _RTLD_PROLOGUE (_dl_start_user) "\
  90. # Stash user entry point in s0.\n\
  91. mv s0, a0\n\
  92. # Load the adjusted argument count.\n\
  93. " STRINGXP (REG_L) " a1, 0(sp)\n\
  94. # Call _dl_init (struct link_map *main_map, int argc, char **argv, char **env) \n\
  95. " STRINGXP (REG_L) " a0, _rtld_local\n\
  96. add a2, sp, " STRINGXP (SZREG) "\n\
  97. sll a3, a1, " STRINGXP (PTRLOG) "\n\
  98. add a3, a3, a2\n\
  99. add a3, a3, " STRINGXP (SZREG) "\n\
  100. # Stash the stack pointer in s1.\n\
  101. mv s1, sp\n\
  102. # Align stack to 128 bits for the _dl_init call.\n\
  103. andi sp, sp,-16\n\
  104. # Call the function to run the initializers.\n\
  105. jal _dl_init\n\
  106. # Restore the stack pointer for _start.\n\
  107. mv sp, s1\n\
  108. # Pass our finalizer function to _start.\n\
  109. lla a0, _dl_fini\n\
  110. # Jump to the user entry point.\n\
  111. jr s0\n\
  112. " _RTLD_EPILOGUE (ENTRY_POINT) \
  113. _RTLD_EPILOGUE (_dl_start_user) "\
  114. .previous" \
  115. );
  116. /* Names of the architecture-specific auditing callback functions. */
  117. #define ARCH_LA_PLTENTER riscv_gnu_pltenter
  118. #define ARCH_LA_PLTEXIT riscv_gnu_pltexit
  119. /* Bias .got.plt entry by the offset requested by the PLT header. */
  120. #define elf_machine_plt_value(map, reloc, value) (value)
  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. #endif /* !dl_machine_h */
  130. #ifdef RESOLVE_MAP
  131. static inline void
  132. __attribute__ ((always_inline))
  133. elf_machine_rela_relative (ElfW(Addr) l_addr, const ElfW(Rela) *reloc,
  134. void *const reloc_addr)
  135. {
  136. /* R_RISCV_RELATIVE might located in debug info section which might not
  137. aligned to XLEN bytes. Also support relocations on unaligned offsets. */
  138. ElfW(Addr) value = l_addr + reloc->r_addend;
  139. memcpy (reloc_addr, &value, sizeof value);
  140. }
  141. /* Perform a relocation described by R_INFO at the location pointed to
  142. by RELOC_ADDR. SYM is the relocation symbol specified by R_INFO and
  143. MAP is the object containing the reloc. */
  144. static inline void
  145. __attribute__ ((always_inline))
  146. elf_machine_rela (struct link_map *map, struct r_scope_elem *scope[],
  147. const ElfW(Rela) *reloc, const ElfW(Sym) *sym,
  148. const struct r_found_version *version,
  149. void *const reloc_addr, int skip_ifunc)
  150. {
  151. ElfW(Addr) r_info = reloc->r_info;
  152. const unsigned long int r_type = ELFW (R_TYPE) (r_info);
  153. ElfW(Addr) *addr_field = (ElfW(Addr) *) reloc_addr;
  154. const ElfW(Sym) *const __attribute__ ((unused)) refsym = sym;
  155. struct link_map *sym_map = RESOLVE_MAP (map, scope, &sym, version, r_type);
  156. ElfW(Addr) value = 0;
  157. if (sym_map != NULL)
  158. value = SYMBOL_ADDRESS (sym_map, sym, true) + reloc->r_addend;
  159. if (sym != NULL
  160. && __glibc_unlikely (ELFW(ST_TYPE) (sym->st_info) == STT_GNU_IFUNC)
  161. && __glibc_likely (sym->st_shndx != SHN_UNDEF)
  162. && __glibc_likely (!skip_ifunc))
  163. value = elf_ifunc_invoke (value);
  164. switch (r_type)
  165. {
  166. case R_RISCV_RELATIVE:
  167. elf_machine_rela_relative (map->l_addr, reloc, addr_field);
  168. break;
  169. case R_RISCV_JUMP_SLOT:
  170. case __WORDSIZE == 64 ? R_RISCV_64 : R_RISCV_32:
  171. *addr_field = value;
  172. break;
  173. # ifndef RTLD_BOOTSTRAP
  174. case __WORDSIZE == 64 ? R_RISCV_TLS_DTPMOD64 : R_RISCV_TLS_DTPMOD32:
  175. if (sym_map)
  176. *addr_field = sym_map->l_tls_modid;
  177. break;
  178. case __WORDSIZE == 64 ? R_RISCV_TLS_DTPREL64 : R_RISCV_TLS_DTPREL32:
  179. if (sym != NULL)
  180. *addr_field = TLS_DTPREL_VALUE (sym) + reloc->r_addend;
  181. break;
  182. case __WORDSIZE == 64 ? R_RISCV_TLS_TPREL64 : R_RISCV_TLS_TPREL32:
  183. if (sym != NULL)
  184. {
  185. CHECK_STATIC_TLS (map, sym_map);
  186. *addr_field = TLS_TPREL_VALUE (sym_map, sym) + reloc->r_addend;
  187. }
  188. break;
  189. case R_RISCV_COPY:
  190. {
  191. if (__glibc_unlikely (sym == NULL))
  192. /* This can happen in trace mode if an object could not be
  193. found. */
  194. break;
  195. /* Handle TLS copy relocations. */
  196. if (__glibc_unlikely (ELFW (ST_TYPE) (sym->st_info) == STT_TLS))
  197. {
  198. /* There's nothing to do if the symbol is in .tbss. */
  199. if (__glibc_likely (sym->st_value >= sym_map->l_tls_initimage_size))
  200. break;
  201. value += (ElfW(Addr)) sym_map->l_tls_initimage - sym_map->l_addr;
  202. }
  203. size_t size = sym->st_size;
  204. if (__glibc_unlikely (sym->st_size != refsym->st_size))
  205. {
  206. const char *strtab = (const void *) D_PTR (map, l_info[DT_STRTAB]);
  207. if (sym->st_size > refsym->st_size)
  208. size = refsym->st_size;
  209. if (sym->st_size > refsym->st_size || GLRO(dl_verbose))
  210. _dl_error_printf ("\
  211. %s: Symbol `%s' has different size in shared object, consider re-linking\n",
  212. rtld_progname ?: "<program name unknown>",
  213. strtab + refsym->st_name);
  214. }
  215. memcpy (reloc_addr, (void *)value, size);
  216. break;
  217. }
  218. case R_RISCV_IRELATIVE:
  219. value = map->l_addr + reloc->r_addend;
  220. if (__glibc_likely (!skip_ifunc))
  221. value = elf_ifunc_invoke (value);
  222. *addr_field = value;
  223. break;
  224. case R_RISCV_NONE:
  225. break;
  226. # endif /* !RTLD_BOOTSTRAP */
  227. default:
  228. _dl_reloc_bad_type (map, r_type, 0);
  229. break;
  230. }
  231. }
  232. static inline void
  233. __attribute__ ((always_inline))
  234. elf_machine_lazy_rel (struct link_map *map, struct r_scope_elem *scope[],
  235. ElfW(Addr) l_addr, const ElfW(Rela) *reloc,
  236. int skip_ifunc)
  237. {
  238. ElfW(Addr) *const reloc_addr = (void *) (l_addr + reloc->r_offset);
  239. const unsigned int r_type = ELFW (R_TYPE) (reloc->r_info);
  240. /* Check for unexpected PLT reloc type. */
  241. if (__glibc_likely (r_type == R_RISCV_JUMP_SLOT))
  242. {
  243. if (__glibc_unlikely (map->l_mach.plt == 0))
  244. {
  245. if (l_addr)
  246. *reloc_addr += l_addr;
  247. }
  248. else
  249. *reloc_addr = map->l_mach.plt;
  250. }
  251. else if (__glibc_unlikely (r_type == R_RISCV_IRELATIVE))
  252. {
  253. ElfW(Addr) value = map->l_addr + reloc->r_addend;
  254. if (__glibc_likely (!skip_ifunc))
  255. value = elf_ifunc_invoke (value);
  256. *reloc_addr = value;
  257. }
  258. else
  259. _dl_reloc_bad_type (map, r_type, 1);
  260. }
  261. /* Set up the loaded object described by L so its stub function
  262. will jump to the on-demand fixup code __dl_runtime_resolve. */
  263. static inline int
  264. __attribute__ ((always_inline))
  265. elf_machine_runtime_setup (struct link_map *l, struct r_scope_elem *scope[],
  266. int lazy, int profile)
  267. {
  268. #ifndef RTLD_BOOTSTRAP
  269. /* If using PLTs, fill in the first two entries of .got.plt. */
  270. if (l->l_info[DT_JMPREL])
  271. {
  272. extern void _dl_runtime_resolve (void) __attribute__ ((visibility ("hidden")));
  273. extern void _dl_runtime_profile (void) __attribute__ ((visibility ("hidden")));
  274. ElfW(Addr) *gotplt = (ElfW(Addr) *) D_PTR (l, l_info[DT_PLTGOT]);
  275. /* If a library is prelinked but we have to relocate anyway,
  276. we have to be able to undo the prelinking of .got.plt.
  277. The prelinker saved the address of .plt for us here. */
  278. if (gotplt[1])
  279. l->l_mach.plt = gotplt[1] + l->l_addr;
  280. /* The gotplt[0] entry contains the address of a function which gets
  281. called to get the address of a so far unresolved function and
  282. jump to it. The profiling extension of the dynamic linker allows
  283. to intercept the calls to collect information. In this case we
  284. don't store the address in the GOT so that all future calls also
  285. end in this function. */
  286. #ifdef SHARED
  287. if (profile != 0)
  288. {
  289. gotplt[0] = (ElfW(Addr)) &_dl_runtime_profile;
  290. if (GLRO(dl_profile) != NULL
  291. && _dl_name_match_p (GLRO(dl_profile), l))
  292. /* Say that we really want profiling and the timers are
  293. started. */
  294. GL(dl_profile_map) = l;
  295. }
  296. else
  297. #endif
  298. {
  299. /* This function will get called to fix up the GOT entry
  300. indicated by the offset on the stack, and then jump to
  301. the resolved address. */
  302. gotplt[0] = (ElfW(Addr)) &_dl_runtime_resolve;
  303. }
  304. gotplt[1] = (ElfW(Addr)) l;
  305. }
  306. #ifdef SHARED
  307. if (l->l_type == lt_executable)
  308. {
  309. /* The __global_pointer$ may not be defined by the linker if the
  310. $gp register does not be used to access the global variable
  311. in the executable program. Therefore, the search symbol is
  312. set to a weak symbol to avoid we error out if the
  313. __global_pointer$ is not found. */
  314. ElfW(Sym) gp_sym = { 0 };
  315. gp_sym.st_info = (unsigned char) ELFW (ST_INFO (STB_WEAK, STT_NOTYPE));
  316. const ElfW(Sym) *ref = &gp_sym;
  317. _dl_lookup_symbol_x ("__global_pointer$", l, &ref,
  318. l->l_scope, NULL, 0, 0, NULL);
  319. if (ref)
  320. asm (
  321. "mv gp, %0\n"
  322. :
  323. : "r" (ref->st_value + l->l_addr)
  324. /* Don't use SYMBOL_ADDRESS here since __global_pointer$
  325. can be SHN_ABS type, but we need the address relative to
  326. PC, not the absolute address. */
  327. );
  328. }
  329. #endif
  330. #endif
  331. return lazy;
  332. }
  333. #endif /* RESOLVE_MAP */