dl-machine.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. /* Copyright (C) 1995-2026 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. The GNU C Library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public License as
  5. published by the Free Software Foundation; either version 2.1 of the
  6. License, or (at your option) any later version.
  7. The GNU C Library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with the GNU C Library; if not, see
  13. <https://www.gnu.org/licenses/>. */
  14. #ifndef dl_machine_h
  15. #define dl_machine_h
  16. #define ELF_MACHINE_NAME "aarch64"
  17. #include <sysdep.h>
  18. #include <tls.h>
  19. #include <dl-tlsdesc.h>
  20. #include <dl-static-tls.h>
  21. #include <dl-irel.h>
  22. #include <dl-machine-rel.h>
  23. #include <cpu-features.c>
  24. /* Translate a processor specific dynamic tag to the index in l_info array. */
  25. #define DT_AARCH64(x) (DT_AARCH64_##x - DT_LOPROC + DT_NUM)
  26. /* Return nonzero iff ELF header is compatible with the running host. */
  27. static inline int __attribute__ ((unused))
  28. elf_machine_matches_host (const ElfW(Ehdr) *ehdr)
  29. {
  30. return ehdr->e_machine == EM_AARCH64;
  31. }
  32. /* Return the run-time load address of the shared object. */
  33. static inline ElfW(Addr) __attribute__ ((unused))
  34. elf_machine_load_address (void)
  35. {
  36. extern const ElfW(Ehdr) __ehdr_start attribute_hidden;
  37. return (ElfW(Addr)) &__ehdr_start;
  38. }
  39. /* Return the link-time address of _DYNAMIC. */
  40. static inline ElfW(Addr) __attribute__ ((unused))
  41. elf_machine_dynamic (void)
  42. {
  43. extern ElfW(Dyn) _DYNAMIC[] attribute_hidden;
  44. return (ElfW(Addr)) _DYNAMIC - elf_machine_load_address ();
  45. }
  46. /* Set up the loaded object described by L so its unrelocated PLT
  47. entries will jump to the on-demand fixup code in dl-runtime.c. */
  48. static inline int __attribute__ ((unused))
  49. elf_machine_runtime_setup (struct link_map *l, struct r_scope_elem *scope[],
  50. int lazy, int profile)
  51. {
  52. if (l->l_info[DT_JMPREL] && lazy)
  53. {
  54. ElfW(Addr) *got;
  55. extern void _dl_runtime_resolve (ElfW(Word));
  56. got = (ElfW(Addr) *) D_PTR (l, l_info[DT_PLTGOT]);
  57. if (got[1])
  58. {
  59. l->l_mach.plt = got[1] + l->l_addr;
  60. }
  61. got[1] = (ElfW(Addr)) l;
  62. /* The got[2] entry contains the address of a function which gets
  63. called to get the address of a so far unresolved function and
  64. jump to it. The profiling extension of the dynamic linker allows
  65. to intercept the calls to collect information. In this case we
  66. don't store the address in the GOT so that all future calls also
  67. end in this function. */
  68. #ifdef SHARED
  69. extern void _dl_runtime_profile (ElfW(Word));
  70. if ( profile)
  71. {
  72. got[2] = (ElfW(Addr)) &_dl_runtime_profile;
  73. if (GLRO(dl_profile) != NULL
  74. && _dl_name_match_p (GLRO(dl_profile), l))
  75. /* Say that we really want profiling and the timers are
  76. started. */
  77. GL(dl_profile_map) = l;
  78. }
  79. else
  80. #endif
  81. {
  82. /* This function will get called to fix up the GOT entry
  83. indicated by the offset on the stack, and then jump to
  84. the resolved address. */
  85. got[2] = (ElfW(Addr)) &_dl_runtime_resolve;
  86. }
  87. }
  88. return lazy;
  89. }
  90. /* In elf/rtld.c _dl_start should be global so dl-start.S can reference it. */
  91. #define RTLD_START asm (".globl _dl_start");
  92. #define elf_machine_type_class(type) \
  93. ((((type) == R_AARCH64_JUMP_SLOT || \
  94. (type) == R_AARCH64_TLS_DTPMOD || \
  95. (type) == R_AARCH64_TLS_DTPREL || \
  96. (type) == R_AARCH64_TLS_TPREL || \
  97. (type) == R_AARCH64_TLSDESC) * ELF_RTYPE_CLASS_PLT) \
  98. | (((type) == R_AARCH64_COPY) * ELF_RTYPE_CLASS_COPY))
  99. #define ELF_MACHINE_JMP_SLOT R_AARCH64_JUMP_SLOT
  100. #define DL_PLATFORM_INIT dl_platform_init ()
  101. static inline void __attribute__ ((unused))
  102. dl_platform_init (void)
  103. {
  104. if (GLRO(dl_platform) != NULL && *GLRO(dl_platform) == '\0')
  105. /* Avoid an empty string which would disturb us. */
  106. GLRO(dl_platform) = NULL;
  107. #ifdef SHARED
  108. /* init_cpu_features has been called early from __libc_start_main in
  109. static executable. */
  110. init_cpu_features (&GLRO(dl_aarch64_cpu_features));
  111. #endif
  112. }
  113. static inline ElfW(Addr)
  114. elf_machine_fixup_plt (struct link_map *map, lookup_t t,
  115. const ElfW(Sym) *refsym, const ElfW(Sym) *sym,
  116. const ElfW(Rela) *reloc,
  117. ElfW(Addr) *reloc_addr,
  118. ElfW(Addr) value)
  119. {
  120. return *reloc_addr = value;
  121. }
  122. /* Return the final value of a plt relocation. */
  123. static inline ElfW(Addr)
  124. elf_machine_plt_value (struct link_map *map,
  125. const ElfW(Rela) *reloc,
  126. ElfW(Addr) value)
  127. {
  128. return value;
  129. }
  130. #endif
  131. /* Names of the architecture-specific auditing callback functions. */
  132. #define ARCH_LA_PLTENTER aarch64_gnu_pltenter
  133. #define ARCH_LA_PLTEXIT aarch64_gnu_pltexit
  134. #ifdef RESOLVE_MAP
  135. static inline void
  136. __attribute__ ((always_inline))
  137. elf_machine_rela (struct link_map *map, struct r_scope_elem *scope[],
  138. const ElfW(Rela) *reloc, const ElfW(Sym) *sym,
  139. const struct r_found_version *version,
  140. void *const reloc_addr_arg, int skip_ifunc)
  141. {
  142. ElfW(Addr) *const reloc_addr = reloc_addr_arg;
  143. const unsigned int r_type = ELFW (R_TYPE) (reloc->r_info);
  144. if (__builtin_expect (r_type == R_AARCH64_RELATIVE, 0))
  145. *reloc_addr = map->l_addr + reloc->r_addend;
  146. else if (__builtin_expect (r_type == R_AARCH64_NONE, 0))
  147. return;
  148. else
  149. {
  150. # ifndef RTLD_BOOTSTRAP
  151. const ElfW(Sym) *const refsym = sym;
  152. # endif
  153. struct link_map *sym_map = RESOLVE_MAP (map, scope, &sym, version,
  154. r_type);
  155. ElfW(Addr) value = SYMBOL_ADDRESS (sym_map, sym, true);
  156. if (sym != NULL
  157. && __glibc_unlikely (ELFW(ST_TYPE) (sym->st_info) == STT_GNU_IFUNC)
  158. && __glibc_likely (sym->st_shndx != SHN_UNDEF)
  159. && __glibc_likely (!skip_ifunc))
  160. value = elf_ifunc_invoke (value);
  161. switch (r_type)
  162. {
  163. case R_AARCH64_GLOB_DAT:
  164. case R_AARCH64_JUMP_SLOT:
  165. *reloc_addr = value + reloc->r_addend;
  166. break;
  167. # ifndef RTLD_BOOTSTRAP
  168. case R_AARCH64_ABS32:
  169. case R_AARCH64_ABS64:
  170. *reloc_addr = value + reloc->r_addend;
  171. break;
  172. case R_AARCH64_COPY:
  173. if (sym == NULL)
  174. break;
  175. if (sym->st_size > refsym->st_size
  176. || (GLRO(dl_verbose) && sym->st_size < refsym->st_size))
  177. {
  178. const char *strtab;
  179. strtab = (const void *) D_PTR (map, l_info[DT_STRTAB]);
  180. _dl_error_printf ("\
  181. %s: Symbol `%s' has different size in shared object, consider re-linking\n",
  182. RTLD_PROGNAME, strtab + refsym->st_name);
  183. }
  184. memcpy (reloc_addr_arg, (void *) value,
  185. sym->st_size < refsym->st_size
  186. ? sym->st_size : refsym->st_size);
  187. break;
  188. case R_AARCH64_TLSDESC:
  189. {
  190. struct tlsdesc volatile *td =
  191. (struct tlsdesc volatile *)reloc_addr;
  192. if (! sym)
  193. {
  194. td->arg = (void*)reloc->r_addend;
  195. td->entry = _dl_tlsdesc_undefweak;
  196. }
  197. else
  198. {
  199. # ifndef SHARED
  200. CHECK_STATIC_TLS (map, sym_map);
  201. # else
  202. if (!TRY_STATIC_TLS (map, sym_map))
  203. {
  204. td->arg = _dl_make_tlsdesc_dynamic
  205. (sym_map, sym->st_value + reloc->r_addend);
  206. td->entry = _dl_tlsdesc_dynamic;
  207. }
  208. else
  209. # endif
  210. {
  211. td->arg = (void*)(sym->st_value + sym_map->l_tls_offset
  212. + reloc->r_addend);
  213. td->entry = _dl_tlsdesc_return;
  214. }
  215. }
  216. break;
  217. }
  218. case R_AARCH64_TLS_DTPMOD:
  219. if (sym_map != NULL)
  220. {
  221. *reloc_addr = sym_map->l_tls_modid;
  222. }
  223. break;
  224. case R_AARCH64_TLS_DTPREL:
  225. if (sym)
  226. *reloc_addr = sym->st_value + reloc->r_addend;
  227. break;
  228. case R_AARCH64_TLS_TPREL:
  229. if (sym)
  230. {
  231. CHECK_STATIC_TLS (map, sym_map);
  232. *reloc_addr =
  233. sym->st_value + reloc->r_addend + sym_map->l_tls_offset;
  234. }
  235. break;
  236. case R_AARCH64_IRELATIVE:
  237. value = map->l_addr + reloc->r_addend;
  238. if (__glibc_likely (!skip_ifunc))
  239. value = elf_ifunc_invoke (value);
  240. *reloc_addr = value;
  241. break;
  242. # endif /* !RTLD_BOOTSTRAP */
  243. default:
  244. _dl_reloc_bad_type (map, r_type, 0);
  245. break;
  246. }
  247. }
  248. }
  249. static inline void
  250. __attribute__ ((always_inline))
  251. elf_machine_rela_relative (ElfW(Addr) l_addr,
  252. const ElfW(Rela) *reloc,
  253. void *const reloc_addr_arg)
  254. {
  255. ElfW(Addr) *const reloc_addr = reloc_addr_arg;
  256. *reloc_addr = l_addr + reloc->r_addend;
  257. }
  258. static inline void
  259. __attribute__ ((always_inline))
  260. elf_machine_lazy_rel (struct link_map *map, struct r_scope_elem *scope[],
  261. ElfW(Addr) l_addr,
  262. const ElfW(Rela) *reloc,
  263. int skip_ifunc)
  264. {
  265. ElfW(Addr) *const reloc_addr = (void *) (l_addr + reloc->r_offset);
  266. const unsigned int r_type = ELFW (R_TYPE) (reloc->r_info);
  267. /* Check for unexpected PLT reloc type. */
  268. if (__builtin_expect (r_type == R_AARCH64_JUMP_SLOT, 1))
  269. {
  270. if (__glibc_unlikely (map->l_info[DT_AARCH64 (VARIANT_PCS)] != NULL))
  271. {
  272. /* Check the symbol table for variant PCS symbols. */
  273. const Elf_Symndx symndx = ELFW (R_SYM) (reloc->r_info);
  274. const ElfW (Sym) *symtab =
  275. (const void *)D_PTR (map, l_info[DT_SYMTAB]);
  276. const ElfW (Sym) *sym = &symtab[symndx];
  277. if (__glibc_unlikely (sym->st_other & STO_AARCH64_VARIANT_PCS))
  278. {
  279. /* Avoid lazy resolution of variant PCS symbols. */
  280. const struct r_found_version *version = NULL;
  281. if (map->l_info[VERSYMIDX (DT_VERSYM)] != NULL)
  282. {
  283. const ElfW (Half) *vernum =
  284. (const void *)D_PTR (map, l_info[VERSYMIDX (DT_VERSYM)]);
  285. version = &map->l_versions[vernum[symndx] & 0x7fff];
  286. }
  287. elf_machine_rela (map, scope, reloc, sym, version, reloc_addr,
  288. skip_ifunc);
  289. return;
  290. }
  291. }
  292. if (map->l_mach.plt == 0)
  293. *reloc_addr += l_addr;
  294. else
  295. *reloc_addr = map->l_mach.plt;
  296. }
  297. else if (__builtin_expect (r_type == R_AARCH64_TLSDESC, 1))
  298. {
  299. const Elf_Symndx symndx = ELFW (R_SYM) (reloc->r_info);
  300. const ElfW (Sym) *symtab = (const void *)D_PTR (map, l_info[DT_SYMTAB]);
  301. const ElfW (Sym) *sym = &symtab[symndx];
  302. const struct r_found_version *version = NULL;
  303. if (map->l_info[VERSYMIDX (DT_VERSYM)] != NULL)
  304. {
  305. const ElfW (Half) *vernum =
  306. (const void *)D_PTR (map, l_info[VERSYMIDX (DT_VERSYM)]);
  307. version = &map->l_versions[vernum[symndx] & 0x7fff];
  308. }
  309. /* Always initialize TLS descriptors completely, because lazy
  310. initialization requires synchronization at every TLS access. */
  311. elf_machine_rela (map, scope, reloc, sym, version, reloc_addr,
  312. skip_ifunc);
  313. }
  314. else if (__glibc_unlikely (r_type == R_AARCH64_IRELATIVE))
  315. {
  316. ElfW(Addr) value = map->l_addr + reloc->r_addend;
  317. if (__glibc_likely (!skip_ifunc))
  318. value = elf_ifunc_invoke (value);
  319. *reloc_addr = value;
  320. }
  321. else
  322. _dl_reloc_bad_type (map, r_type, 1);
  323. }
  324. #endif