dl-machine.h 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. /* Machine-dependent ELF dynamic relocation inline functions. m68k version.
  2. Copyright (C) 1996-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 "m68k"
  18. #include <sys/param.h>
  19. #include <sysdep.h>
  20. #include <dl-tls.h>
  21. #include <dl-static-tls.h>
  22. #include <dl-machine-rel.h>
  23. /* Return nonzero iff ELF header is compatible with the running host. */
  24. static inline int
  25. elf_machine_matches_host (const Elf32_Ehdr *ehdr)
  26. {
  27. return ehdr->e_machine == EM_68K;
  28. }
  29. /* Return the link-time address of _DYNAMIC.
  30. This must be inlined in a function which uses global data. */
  31. static inline Elf32_Addr
  32. elf_machine_dynamic (void)
  33. {
  34. Elf32_Addr addr;
  35. asm ("move.l _DYNAMIC@GOT.w(%%a5), %0"
  36. : "=a" (addr));
  37. return addr;
  38. }
  39. /* Return the run-time load address of the shared object. */
  40. static inline Elf32_Addr
  41. elf_machine_load_address (void)
  42. {
  43. Elf32_Addr addr;
  44. #ifdef SHARED
  45. asm (PCREL_OP ("lea", "_dl_start", "%0", "%0", "%%pc") "\n\t"
  46. "sub.l _dl_start@GOT.w(%%a5), %0"
  47. : "=a" (addr));
  48. #else
  49. asm (PCREL_OP ("lea", "_dl_relocate_static_pie", "%0", "%0", "%%pc") "\n\t"
  50. "sub.l _dl_relocate_static_pie@GOT.w(%%a5), %0"
  51. : "=a" (addr));
  52. #endif
  53. return addr;
  54. }
  55. /* Set up the loaded object described by L so its unrelocated PLT
  56. entries will jump to the on-demand fixup code in dl-runtime.c. */
  57. static inline int __attribute__ ((always_inline))
  58. elf_machine_runtime_setup (struct link_map *l, struct r_scope_elem *scope[],
  59. int lazy, int profile)
  60. {
  61. Elf32_Addr *got;
  62. extern void _dl_runtime_resolve (Elf32_Word);
  63. if (l->l_info[DT_JMPREL] && lazy)
  64. {
  65. /* The GOT entries for functions in the PLT have not yet been
  66. filled in. Their initial contents will arrange when called
  67. to push an offset into the .rela.plt section, push
  68. _GLOBAL_OFFSET_TABLE_[1], and then jump to
  69. _GLOBAL_OFFSET_TABLE_[2]. */
  70. got = (Elf32_Addr *) D_PTR (l, l_info[DT_PLTGOT]);
  71. got[1] = (Elf32_Addr) l; /* Identify this shared object. */
  72. /* The got[2] entry contains the address of a function which gets
  73. called to get the address of a so far unresolved function and
  74. jump to it. The profiling extension of the dynamic linker allows
  75. to intercept the calls to collect information. In this case we
  76. don't store the address in the GOT so that all future calls also
  77. end in this function. */
  78. #ifdef SHARED
  79. extern void _dl_runtime_profile (Elf32_Word);
  80. if (profile)
  81. {
  82. got[2] = (Elf32_Addr) &_dl_runtime_profile;
  83. if (GLRO(dl_profile) != NULL
  84. && _dl_name_match_p (GLRO(dl_profile), l))
  85. {
  86. /* This is the object we are looking for. Say that we really
  87. want profiling and the timers are started. */
  88. GL(dl_profile_map) = l;
  89. }
  90. }
  91. else
  92. #endif
  93. /* This function will get called to fix up the GOT entry indicated by
  94. the offset on the stack, and then jump to the resolved address. */
  95. got[2] = (Elf32_Addr) &_dl_runtime_resolve;
  96. }
  97. return lazy;
  98. }
  99. #define ELF_MACHINE_RUNTIME_FIXUP_ARGS long int save_a0, long int save_a1
  100. #define ELF_MACHINE_RUNTIME_FIXUP_PARAMS save_a0, save_a1
  101. /* Mask identifying addresses reserved for the user program,
  102. where the dynamic linker should not map anything. */
  103. #define ELF_MACHINE_USER_ADDRESS_MASK 0x80000000UL
  104. /* Initial entry point code for the dynamic linker.
  105. The C function `_dl_start' is the real entry point;
  106. its return value is the user program's entry point. */
  107. #define RTLD_START asm ("\
  108. .text\n\
  109. .globl _start\n\
  110. .type _start,@function\n\
  111. _start:\n\
  112. sub.l %fp, %fp\n\
  113. move.l %sp, -(%sp)\n\
  114. jbsr _dl_start\n\
  115. addq.l #4, %sp\n\
  116. /* FALLTHRU */\n\
  117. \n\
  118. .globl _dl_start_user\n\
  119. .type _dl_start_user,@function\n\
  120. _dl_start_user:\n\
  121. | Save the user entry point address in %a4.\n\
  122. move.l %d0, %a4\n\
  123. | Load the adjusted argument count.\n\
  124. move.l (%sp), %d1\n\
  125. # Call _dl_init (struct link_map *main_map, int argc, char **argv, char **env)\n\
  126. pea 8(%sp, %d1*4)\n\
  127. pea 8(%sp)\n\
  128. move.l %d1, -(%sp)\n\
  129. " PCREL_OP ("move.l", "_rtld_local", "-(%sp)", "%d0", "%pc") "\n\
  130. jbsr _dl_init\n\
  131. addq.l #8, %sp\n\
  132. addq.l #8, %sp\n\
  133. | Pass our finalizer function to the user in %a1.\n\
  134. " PCREL_OP ("lea", "_dl_fini", "%a1", "%a1", "%pc") "\n\
  135. | Initialize %fp with the stack pointer.\n\
  136. move.l %sp, %fp\n\
  137. | Jump to the user's entry point.\n\
  138. jmp (%a4)\n\
  139. .size _dl_start_user, . - _dl_start_user\n\
  140. .previous");
  141. /* ELF_RTYPE_CLASS_PLT iff TYPE describes relocation of a PLT entry or
  142. TLS variable, so undefined references should not be allowed to
  143. define the value.
  144. ELF_RTYPE_CLASS_COPY iff TYPE should not be allowed to resolve to one
  145. of the main executable's symbols, as for a COPY reloc. */
  146. #define elf_machine_type_class(type) \
  147. ((((type) == R_68K_JMP_SLOT \
  148. || (type) == R_68K_TLS_DTPMOD32 \
  149. || (type) == R_68K_TLS_DTPREL32 \
  150. || (type) == R_68K_TLS_TPREL32) * ELF_RTYPE_CLASS_PLT) \
  151. | (((type) == R_68K_COPY) * ELF_RTYPE_CLASS_COPY))
  152. /* A reloc type used for ld.so cmdline arg lookups to reject PLT entries. */
  153. #define ELF_MACHINE_JMP_SLOT R_68K_JMP_SLOT
  154. static inline Elf32_Addr
  155. elf_machine_fixup_plt (struct link_map *map, lookup_t t,
  156. const ElfW(Sym) *refsym, const ElfW(Sym) *sym,
  157. const Elf32_Rela *reloc,
  158. Elf32_Addr *reloc_addr, Elf32_Addr value)
  159. {
  160. return *reloc_addr = value;
  161. }
  162. /* Return the final value of a plt relocation. On the m68k the JMP_SLOT
  163. relocation ignores the addend. */
  164. static inline Elf32_Addr
  165. elf_machine_plt_value (struct link_map *map, const Elf32_Rela *reloc,
  166. Elf32_Addr value)
  167. {
  168. return value;
  169. }
  170. /* Names of the architecture-specific auditing callback functions. */
  171. #define ARCH_LA_PLTENTER m68k_gnu_pltenter
  172. #define ARCH_LA_PLTEXIT m68k_gnu_pltexit
  173. #endif /* !dl_machine_h */
  174. #ifdef RESOLVE_MAP
  175. /* Perform the relocation specified by RELOC and SYM (which is fully resolved).
  176. MAP is the object containing the reloc. */
  177. static inline void __attribute__ ((unused, always_inline))
  178. elf_machine_rela (struct link_map *map, struct r_scope_elem *scope[],
  179. const Elf32_Rela *reloc, const Elf32_Sym *sym,
  180. const struct r_found_version *version,
  181. void *const reloc_addr_arg, int skip_ifunc)
  182. {
  183. Elf32_Addr *const reloc_addr = reloc_addr_arg;
  184. const unsigned int r_type = ELF32_R_TYPE (reloc->r_info);
  185. if (__builtin_expect (r_type == R_68K_RELATIVE, 0))
  186. *reloc_addr = map->l_addr + reloc->r_addend;
  187. else
  188. {
  189. const Elf32_Sym *const refsym = sym;
  190. struct link_map *sym_map = RESOLVE_MAP (map, scope, &sym, version,
  191. r_type);
  192. Elf32_Addr value = SYMBOL_ADDRESS (sym_map, sym, true);
  193. switch (r_type)
  194. {
  195. case R_68K_GLOB_DAT:
  196. case R_68K_JMP_SLOT:
  197. *reloc_addr = value;
  198. break;
  199. #ifndef RTLD_BOOTSTRAP
  200. case R_68K_COPY:
  201. if (sym == NULL)
  202. /* This can happen in trace mode if an object could not be
  203. found. */
  204. break;
  205. if (sym->st_size > refsym->st_size
  206. || (sym->st_size < refsym->st_size && GLRO(dl_verbose)))
  207. {
  208. const char *strtab;
  209. strtab = (const void *) D_PTR (map, l_info[DT_STRTAB]);
  210. _dl_error_printf ("\
  211. %s: Symbol `%s' has different size in shared object, consider re-linking\n",
  212. RTLD_PROGNAME, strtab + refsym->st_name);
  213. }
  214. memcpy (reloc_addr_arg, (void *) value,
  215. MIN (sym->st_size, refsym->st_size));
  216. break;
  217. case R_68K_8:
  218. *(char *) reloc_addr = value + reloc->r_addend;
  219. break;
  220. case R_68K_16:
  221. *(short *) reloc_addr = value + reloc->r_addend;
  222. break;
  223. case R_68K_32:
  224. *reloc_addr = value + reloc->r_addend;
  225. break;
  226. case R_68K_PC8:
  227. *(char *) reloc_addr
  228. = value + reloc->r_addend - (Elf32_Addr) reloc_addr;
  229. break;
  230. case R_68K_PC16:
  231. *(short *) reloc_addr
  232. = value + reloc->r_addend - (Elf32_Addr) reloc_addr;
  233. break;
  234. case R_68K_PC32:
  235. *reloc_addr = value + reloc->r_addend - (Elf32_Addr) reloc_addr;
  236. break;
  237. case R_68K_TLS_DTPMOD32:
  238. /* Get the information from the link map returned by the
  239. resolv function. */
  240. if (sym_map != NULL)
  241. *reloc_addr = sym_map->l_tls_modid;
  242. break;
  243. case R_68K_TLS_DTPREL32:
  244. if (sym != NULL)
  245. *reloc_addr = TLS_DTPREL_VALUE (sym, reloc);
  246. break;
  247. case R_68K_TLS_TPREL32:
  248. if (sym != NULL)
  249. {
  250. CHECK_STATIC_TLS (map, sym_map);
  251. *reloc_addr = TLS_TPREL_VALUE (sym_map, sym, reloc);
  252. }
  253. break;
  254. case R_68K_NONE: /* Alright, Wilbur. */
  255. break;
  256. #endif /* !RTLD_BOOTSTRAP */
  257. default:
  258. _dl_reloc_bad_type (map, r_type, 0);
  259. break;
  260. }
  261. }
  262. }
  263. static inline void __attribute__ ((unused, always_inline))
  264. elf_machine_rela_relative (Elf32_Addr l_addr, const Elf32_Rela *reloc,
  265. void *const reloc_addr_arg)
  266. {
  267. Elf32_Addr *const reloc_addr = reloc_addr_arg;
  268. *reloc_addr = l_addr + reloc->r_addend;
  269. }
  270. static inline void __attribute__ ((unused, always_inline))
  271. elf_machine_lazy_rel (struct link_map *map, struct r_scope_elem *scope[],
  272. Elf32_Addr l_addr, const Elf32_Rela *reloc,
  273. int skip_ifunc)
  274. {
  275. Elf32_Addr *const reloc_addr = (void *) (l_addr + reloc->r_offset);
  276. if (ELF32_R_TYPE (reloc->r_info) == R_68K_JMP_SLOT)
  277. *reloc_addr += l_addr;
  278. else
  279. _dl_reloc_bad_type (map, ELF32_R_TYPE (reloc->r_info), 1);
  280. }
  281. #endif /* RESOLVE_MAP */