dl-machine.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  1. /* Machine-dependent ELF dynamic relocation inline functions. SH version.
  2. Copyright (C) 1999-2019 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. <http://www.gnu.org/licenses/>. */
  15. #ifndef dl_machine_h
  16. #define dl_machine_h
  17. #define ELF_MACHINE_NAME "SH"
  18. #include <sys/param.h>
  19. #include <sysdep.h>
  20. #include <assert.h>
  21. /* Return nonzero iff ELF header is compatible with the running host. */
  22. static inline int __attribute__ ((unused))
  23. elf_machine_matches_host (const Elf32_Ehdr *ehdr)
  24. {
  25. return ehdr->e_machine == EM_SH;
  26. }
  27. /* Return the link-time address of _DYNAMIC. Conveniently, this is the
  28. first element of the GOT. This must be inlined in a function which
  29. uses global data. */
  30. static inline Elf32_Addr __attribute__ ((unused))
  31. elf_machine_dynamic (void)
  32. {
  33. register Elf32_Addr *got;
  34. asm ("mov r12,%0" :"=r" (got));
  35. return *got;
  36. }
  37. /* Return the run-time load address of the shared object. */
  38. static inline Elf32_Addr __attribute__ ((unused))
  39. elf_machine_load_address (void)
  40. {
  41. Elf32_Addr addr;
  42. asm ("mov.l 1f,r0\n\
  43. mov.l 3f,r2\n\
  44. add r12,r2\n\
  45. mov.l @(r0,r12),r0\n\
  46. bra 2f\n\
  47. sub r0,r2\n\
  48. .align 2\n\
  49. 1: .long _dl_start@GOT\n\
  50. 3: .long _dl_start@GOTOFF\n\
  51. 2: mov r2,%0"
  52. : "=r" (addr) : : "r0", "r1", "r2");
  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__ ((unused, always_inline))
  58. elf_machine_runtime_setup (struct link_map *l, int lazy, int profile)
  59. {
  60. Elf32_Addr *got;
  61. extern void _dl_runtime_resolve (Elf32_Word);
  62. extern void _dl_runtime_profile (Elf32_Word);
  63. if (l->l_info[DT_JMPREL] && lazy)
  64. {
  65. /* The GOT entries for functions in the PLT have not yet been filled
  66. in. Their initial contents will arrange when called to load an
  67. offset into the .rela.plt section and _GLOBAL_OFFSET_TABLE_[1],
  68. and then jump to _GLOBAL_OFFSET_TABLE[2]. */
  69. got = (Elf32_Addr *) D_PTR (l, l_info[DT_PLTGOT]);
  70. /* If a library is prelinked but we have to relocate anyway,
  71. we have to be able to undo the prelinking of .got.plt.
  72. The prelinker saved us here address of .plt + 36. */
  73. if (got[1])
  74. {
  75. l->l_mach.plt = got[1] + l->l_addr;
  76. l->l_mach.gotplt = (Elf32_Addr) &got[3];
  77. }
  78. got[1] = (Elf32_Addr) l; /* Identify this shared object. */
  79. /* The got[2] entry contains the address of a function which gets
  80. called to get the address of a so far unresolved function and
  81. jump to it. The profiling extension of the dynamic linker allows
  82. to intercept the calls to collect information. In this case we
  83. don't store the address in the GOT so that all future calls also
  84. end in this function. */
  85. if (profile)
  86. {
  87. got[2] = (Elf32_Addr) &_dl_runtime_profile;
  88. /* Say that we really want profiling and the timers are started. */
  89. if (GLRO(dl_profile) != NULL
  90. && _dl_name_match_p (GLRO(dl_profile), l))
  91. GL(dl_profile_map) = l;
  92. }
  93. else
  94. /* This function will get called to fix up the GOT entry indicated by
  95. the offset on the stack, and then jump to the resolved address. */
  96. got[2] = (Elf32_Addr) &_dl_runtime_resolve;
  97. }
  98. return lazy;
  99. }
  100. #define ELF_MACHINE_RUNTIME_FIXUP_ARGS int plt_type
  101. #define ELF_MACHINE_RUNTIME_FIXUP_PARAMS plt_type
  102. /* Mask identifying addresses reserved for the user program,
  103. where the dynamic linker should not map anything. */
  104. #define ELF_MACHINE_USER_ADDRESS_MASK 0x80000000UL
  105. /* Initial entry point code for the dynamic linker.
  106. The C function `_dl_start' is the real entry point;
  107. its return value is the user program's entry point. */
  108. #define RTLD_START asm ("\
  109. .text\n\
  110. .globl _start\n\
  111. .globl _dl_start_user\n\
  112. _start:\n\
  113. mov r15,r4\n\
  114. mov.l .L_dl_start,r1\n\
  115. mova .L_dl_start,r0\n\
  116. add r1,r0\n\
  117. jsr @r0\n\
  118. nop\n\
  119. _dl_start_user:\n\
  120. ! Save the user entry point address in r8.\n\
  121. mov r0,r8\n\
  122. ! Point r12 at the GOT.\n\
  123. mov.l 1f,r12\n\
  124. mova 1f,r0\n\
  125. bra 2f\n\
  126. add r0,r12\n\
  127. .align 2\n\
  128. 1: .long _GLOBAL_OFFSET_TABLE_\n\
  129. 2: ! See if we were run as a command with the executable file\n\
  130. ! name as an extra leading argument.\n\
  131. mov.l .L_dl_skip_args,r0\n\
  132. mov.l @(r0,r12),r0\n\
  133. mov.l @r0,r0\n\
  134. ! Get the original argument count.\n\
  135. mov.l @r15,r5\n\
  136. ! Subtract _dl_skip_args from it.\n\
  137. sub r0,r5\n\
  138. ! Adjust the stack pointer to skip _dl_skip_args words.\n\
  139. shll2 r0\n\
  140. add r0,r15\n\
  141. ! Store back the modified argument count.\n\
  142. mov.l r5,@r15\n\
  143. ! Compute argv address and envp.\n\
  144. mov r15,r6\n\
  145. add #4,r6\n\
  146. mov r5,r7\n\
  147. shll2 r7\n\
  148. add r15,r7\n\
  149. add #8,r7\n\
  150. mov.l .L_dl_loaded,r0\n\
  151. mov.l @(r0,r12),r0\n\
  152. mov.l @r0,r4\n\
  153. ! Call _dl_init.\n\
  154. mov.l .L_dl_init,r1\n\
  155. mova .L_dl_init,r0\n\
  156. add r1,r0\n\
  157. jsr @r0\n\
  158. nop\n\
  159. 1: ! Pass our finalizer function to the user in r4, as per ELF ABI.\n\
  160. mov.l .L_dl_fini,r0\n\
  161. mov.l @(r0,r12),r4\n\
  162. ! Jump to the user's entry point.\n\
  163. jmp @r8\n\
  164. nop\n\
  165. .align 2\n\
  166. .L_dl_start:\n\
  167. .long _dl_start@PLT\n\
  168. .L_dl_skip_args:\n\
  169. .long _dl_skip_args@GOT\n\
  170. .L_dl_init:\n\
  171. .long _dl_init@PLT\n\
  172. .L_dl_loaded:\n\
  173. .long _rtld_local@GOT\n\
  174. .L_dl_fini:\n\
  175. .long _dl_fini@GOT\n\
  176. .type __fpscr_values,@object\n\
  177. .global __fpscr_values\n\
  178. __fpscr_values:\n\
  179. .long 0\n\
  180. .long 0x80000\n\
  181. .weak __fpscr_values\n\
  182. .previous\n\
  183. ");
  184. /* ELF_RTYPE_CLASS_PLT iff TYPE describes relocation of a PLT entry or
  185. TLS variable, so undefined references should not be allowed to
  186. define the value.
  187. ELF_RTYPE_CLASS_COPY iff TYPE should not be allowed to resolve to one
  188. of the main executable's symbols, as for a COPY reloc. */
  189. #define elf_machine_type_class(type) \
  190. ((((type) == R_SH_JMP_SLOT || (type) == R_SH_TLS_DTPMOD32 \
  191. || (type) == R_SH_TLS_DTPOFF32 || (type) == R_SH_TLS_TPOFF32) \
  192. * ELF_RTYPE_CLASS_PLT) \
  193. | (((type) == R_SH_COPY) * ELF_RTYPE_CLASS_COPY))
  194. /* A reloc type used for ld.so cmdline arg lookups to reject PLT entries. */
  195. #define ELF_MACHINE_JMP_SLOT R_SH_JMP_SLOT
  196. /* We define an initialization functions. This is called very early in
  197. _dl_sysdep_start. */
  198. #define DL_PLATFORM_INIT dl_platform_init ()
  199. static inline void __attribute__ ((unused))
  200. dl_platform_init (void)
  201. {
  202. if (GLRO(dl_platform) != NULL && *GLRO(dl_platform) == '\0')
  203. /* Avoid an empty string which would disturb us. */
  204. GLRO(dl_platform) = NULL;
  205. }
  206. static inline Elf32_Addr
  207. elf_machine_fixup_plt (struct link_map *map, lookup_t t,
  208. const ElfW(Sym) *refsym, const ElfW(Sym) *sym,
  209. const Elf32_Rela *reloc,
  210. Elf32_Addr *reloc_addr, Elf32_Addr value)
  211. {
  212. return *reloc_addr = value;
  213. }
  214. /* Return the final value of a plt relocation. */
  215. static inline Elf32_Addr
  216. elf_machine_plt_value (struct link_map *map, const Elf32_Rela *reloc,
  217. Elf32_Addr value)
  218. {
  219. return value + reloc->r_addend;
  220. }
  221. #define ARCH_LA_PLTENTER sh_gnu_pltenter
  222. #define ARCH_LA_PLTEXIT sh_gnu_pltexit
  223. #endif /* !dl_machine_h */
  224. /* SH never uses Elf32_Rel relocations. */
  225. #define ELF_MACHINE_NO_REL 1
  226. #define ELF_MACHINE_NO_RELA 0
  227. #ifdef RESOLVE_MAP
  228. /* Perform the relocation specified by RELOC and SYM (which is fully resolved).
  229. MAP is the object containing the reloc. */
  230. auto inline void
  231. __attribute ((always_inline))
  232. elf_machine_rela (struct link_map *map, const Elf32_Rela *reloc,
  233. const Elf32_Sym *sym, const struct r_found_version *version,
  234. void *const reloc_addr_arg, int skip_ifunc)
  235. {
  236. Elf32_Addr *const reloc_addr = reloc_addr_arg;
  237. const unsigned int r_type = ELF32_R_TYPE (reloc->r_info);
  238. Elf32_Addr value;
  239. #define COPY_UNALIGNED_WORD(swp, twp, align) \
  240. { \
  241. void *__s = (swp), *__t = (twp); \
  242. unsigned char *__s1 = __s, *__t1 = __t; \
  243. unsigned short *__s2 = __s, *__t2 = __t; \
  244. unsigned long *__s4 = __s, *__t4 = __t; \
  245. switch ((align)) \
  246. { \
  247. case 0: \
  248. *__t4 = *__s4; \
  249. break; \
  250. case 2: \
  251. *__t2++ = *__s2++; \
  252. *__t2 = *__s2; \
  253. break; \
  254. default: \
  255. *__t1++ = *__s1++; \
  256. *__t1++ = *__s1++; \
  257. *__t1++ = *__s1++; \
  258. *__t1 = *__s1; \
  259. break; \
  260. } \
  261. }
  262. if (__glibc_unlikely (r_type == R_SH_RELATIVE))
  263. {
  264. #ifndef RTLD_BOOTSTRAP
  265. if (map != &GL(dl_rtld_map)) /* Already done in rtld itself. */
  266. #endif
  267. {
  268. if (reloc->r_addend)
  269. value = map->l_addr + reloc->r_addend;
  270. else
  271. {
  272. COPY_UNALIGNED_WORD (reloc_addr_arg, &value,
  273. (int) reloc_addr_arg & 3);
  274. value += map->l_addr;
  275. }
  276. COPY_UNALIGNED_WORD (&value, reloc_addr_arg,
  277. (int) reloc_addr_arg & 3);
  278. }
  279. }
  280. #ifndef RTLD_BOOTSTRAP
  281. else if (__glibc_unlikely (r_type == R_SH_NONE))
  282. return;
  283. #endif
  284. else
  285. {
  286. const Elf32_Sym *const refsym = sym;
  287. struct link_map *sym_map = RESOLVE_MAP (&sym, version, r_type);
  288. value = SYMBOL_ADDRESS (sym_map, sym, true);
  289. value += reloc->r_addend;
  290. switch (r_type)
  291. {
  292. case R_SH_COPY:
  293. if (sym == NULL)
  294. /* This can happen in trace mode if an object could not be
  295. found. */
  296. break;
  297. if (sym->st_size > refsym->st_size
  298. || (sym->st_size < refsym->st_size && GLRO(dl_verbose)))
  299. {
  300. const char *strtab;
  301. strtab = (const char *) D_PTR (map, l_info[DT_STRTAB]);
  302. _dl_error_printf ("\
  303. %s: Symbol `%s' has different size in shared object, consider re-linking\n",
  304. RTLD_PROGNAME, strtab + refsym->st_name);
  305. }
  306. memcpy (reloc_addr_arg, (void *) value,
  307. MIN (sym->st_size, refsym->st_size));
  308. break;
  309. case R_SH_GLOB_DAT:
  310. case R_SH_JMP_SLOT:
  311. /* These addresses are always aligned. */
  312. *reloc_addr = value;
  313. break;
  314. /* XXX Remove TLS relocations which are not needed. */
  315. case R_SH_TLS_DTPMOD32:
  316. #ifdef RTLD_BOOTSTRAP
  317. /* During startup the dynamic linker is always the module
  318. with index 1.
  319. XXX If this relocation is necessary move before RESOLVE
  320. call. */
  321. *reloc_addr = 1;
  322. #else
  323. /* Get the information from the link map returned by the
  324. resolv function. */
  325. if (sym_map != NULL)
  326. *reloc_addr = sym_map->l_tls_modid;
  327. #endif
  328. break;
  329. case R_SH_TLS_DTPOFF32:
  330. #ifndef RTLD_BOOTSTRAP
  331. /* During relocation all TLS symbols are defined and used.
  332. Therefore the offset is already correct. */
  333. if (sym != NULL)
  334. *reloc_addr = sym->st_value;
  335. #endif
  336. break;
  337. case R_SH_TLS_TPOFF32:
  338. /* The offset is positive, afterward from the thread pointer. */
  339. #ifdef RTLD_BOOTSTRAP
  340. *reloc_addr = map->l_tls_offset + sym->st_value + reloc->r_addend;
  341. #else
  342. /* We know the offset of object the symbol is contained in.
  343. It is a positive value which will be added to the thread
  344. pointer. To get the variable position in the TLS block
  345. we add the offset from that of the TLS block. */
  346. if (sym != NULL)
  347. {
  348. CHECK_STATIC_TLS (map, sym_map);
  349. *reloc_addr = sym_map->l_tls_offset + sym->st_value
  350. + reloc->r_addend;
  351. }
  352. #endif
  353. break;
  354. case R_SH_DIR32:
  355. {
  356. #if !defined RTLD_BOOTSTRAP && !defined RESOLVE_CONFLICT_FIND_MAP
  357. /* This is defined in rtld.c, but nowhere in the static
  358. libc.a; make the reference weak so static programs can
  359. still link. This declaration cannot be done when
  360. compiling rtld.c (i.e. #ifdef RTLD_BOOTSTRAP) because
  361. rtld.c contains the common defn for _dl_rtld_map, which
  362. is incompatible with a weak decl in the same file. */
  363. # ifndef SHARED
  364. weak_extern (_dl_rtld_map);
  365. # endif
  366. if (map == &GL(dl_rtld_map))
  367. /* Undo the relocation done here during bootstrapping.
  368. Now we will relocate it anew, possibly using a
  369. binding found in the user program or a loaded library
  370. rather than the dynamic linker's built-in definitions
  371. used while loading those libraries. */
  372. value -= SYMBOL_ADDRESS (map, refsym, true) + reloc->r_addend;
  373. #endif
  374. COPY_UNALIGNED_WORD (&value, reloc_addr_arg,
  375. (int) reloc_addr_arg & 3);
  376. break;
  377. }
  378. case R_SH_REL32:
  379. value = (value - (Elf32_Addr) reloc_addr);
  380. COPY_UNALIGNED_WORD (&value, reloc_addr_arg,
  381. (int) reloc_addr_arg & 3);
  382. break;
  383. default:
  384. _dl_reloc_bad_type (map, r_type, 0);
  385. break;
  386. }
  387. }
  388. }
  389. auto inline void
  390. __attribute__ ((always_inline))
  391. elf_machine_rela_relative (Elf32_Addr l_addr, const Elf32_Rela *reloc,
  392. void *const reloc_addr_arg)
  393. {
  394. Elf32_Addr value;
  395. if (reloc->r_addend)
  396. value = l_addr + reloc->r_addend;
  397. else
  398. {
  399. COPY_UNALIGNED_WORD (reloc_addr_arg, &value, (int) reloc_addr_arg & 3);
  400. value += l_addr;
  401. }
  402. COPY_UNALIGNED_WORD (&value, reloc_addr_arg, (int) reloc_addr_arg & 3);
  403. #undef COPY_UNALIGNED_WORD
  404. }
  405. auto inline void
  406. __attribute__ ((always_inline))
  407. elf_machine_lazy_rel (struct link_map *map,
  408. Elf32_Addr l_addr, const Elf32_Rela *reloc,
  409. int skip_ifunc)
  410. {
  411. Elf32_Addr *const reloc_addr = (void *) (l_addr + reloc->r_offset);
  412. /* Check for unexpected PLT reloc type. */
  413. if (ELF32_R_TYPE (reloc->r_info) == R_SH_JMP_SLOT)
  414. {
  415. if (__builtin_expect (map->l_mach.plt, 0) == 0)
  416. *reloc_addr += l_addr;
  417. else
  418. *reloc_addr =
  419. map->l_mach.plt
  420. + (((Elf32_Addr) reloc_addr) - map->l_mach.gotplt) * 7;
  421. }
  422. else
  423. _dl_reloc_bad_type (map, ELF32_R_TYPE (reloc->r_info), 1);
  424. }
  425. #endif /* RESOLVE_MAP */