1 /* Machine-dependent ELF dynamic relocation inline functions. SH version.
2 Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004
3 Free Software Foundation, Inc.
4 This file is part of the GNU C Library.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
24 /* Only dummy. This doesn't work. */
26 #define ELF_MACHINE_NAME "SH"
28 #include <sys/param.h>
32 /* Return nonzero iff ELF header is compatible with the running host. */
33 static inline int __attribute__ ((unused))
34 elf_machine_matches_host (const Elf32_Ehdr *ehdr)
36 return ehdr->e_machine == EM_SH;
40 /* Return the link-time address of _DYNAMIC. Conveniently, this is the
41 first element of the GOT. This must be inlined in a function which
43 static inline Elf32_Addr __attribute__ ((unused))
44 elf_machine_dynamic (void)
46 register Elf32_Addr *got;
47 asm ("mov r12,%0" :"=r" (got));
52 /* Return the run-time load address of the shared object. */
53 static inline Elf32_Addr __attribute__ ((unused))
54 elf_machine_load_address (void)
64 1: .long _dl_start@GOT\n\
65 3: .long _dl_start@GOTOFF\n\
67 : "=r" (addr) : : "r0", "r1", "r2");
72 /* Set up the loaded object described by L so its unrelocated PLT
73 entries will jump to the on-demand fixup code in dl-runtime.c. */
75 static inline int __attribute__ ((unused))
76 elf_machine_runtime_setup (struct link_map *l, int lazy, int profile)
79 extern void _dl_runtime_resolve (Elf32_Word);
80 extern void _dl_runtime_profile (Elf32_Word);
82 if (l->l_info[DT_JMPREL] && lazy)
84 /* The GOT entries for functions in the PLT have not yet been filled
85 in. Their initial contents will arrange when called to load an
86 offset into the .rela.plt section and _GLOBAL_OFFSET_TABLE_[1],
87 and then jump to _GLOBAL_OFFSET_TABLE[2]. */
88 got = (Elf32_Addr *) D_PTR (l, l_info[DT_PLTGOT]);
89 /* If a library is prelinked but we have to relocate anyway,
90 we have to be able to undo the prelinking of .got.plt.
91 The prelinker saved us here address of .plt + 36. */
94 l->l_mach.plt = got[1] + l->l_addr;
95 l->l_mach.gotplt = (Elf32_Addr) &got[3];
97 got[1] = (Elf32_Addr) l; /* Identify this shared object. */
99 /* The got[2] entry contains the address of a function which gets
100 called to get the address of a so far unresolved function and
101 jump to it. The profiling extension of the dynamic linker allows
102 to intercept the calls to collect information. In this case we
103 don't store the address in the GOT so that all future calls also
104 end in this function. */
107 got[2] = (Elf32_Addr) &_dl_runtime_profile;
108 /* Say that we really want profiling and the timers are started. */
109 GL(dl_profile_map) = l;
112 /* This function will get called to fix up the GOT entry indicated by
113 the offset on the stack, and then jump to the resolved address. */
114 got[2] = (Elf32_Addr) &_dl_runtime_resolve;
119 /* This code is used in dl-runtime.c to call the `fixup' function
120 and then redirect to the address it returns. */
122 #define ELF_MACHINE_RUNTIME_FIXUP_ARGS int plt_type
129 add r0,r2 ! Get GOT address in r2\n\
131 1: .long _GLOBAL_OFFSET_TABLE_\n\
134 #define GOTJMP(x) #x "@GOTOFF"
143 sts.l fpscr, @-r15\n\
147 fmov.s fr11, @-r15\n\
148 fmov.s fr10, @-r15\n\
162 fmov.s @r15+, fr10\n\
163 fmov.s @r15+, fr11\n\
171 # define ELF_MACHINE_RUNTIME_TRAMPOLINE asm ("\
173 .globl _dl_runtime_resolve\n\
174 .type _dl_runtime_resolve, @function\n\
176 _dl_runtime_resolve:\n\
184 movt r3 ! Save T flag.\n\
192 mov r0,r4 ! PLT type\n\
193 mov r2,r5 ! link map address\n\
195 jsr @r0 ! Call resolver.\n\
196 mov r1,r6 ! reloc offset\n\
197 lds.l @r15+,pr ! Get register content back.\n\
200 shal r3 ! Lode T flag.\n\
207 jmp @r0 ! Jump to function address.\n\
211 .long " GOTJMP (fixup) "\n\
212 .size _dl_runtime_resolve, .-_dl_runtime_resolve\n\
214 .globl _dl_runtime_profile\n\
215 .type _dl_runtime_profile, @function\n\
217 _dl_runtime_profile:\n\
225 movt r3 ! Save T flag.\n\
233 mov r0,r4 ! PLT type\n\
234 mov r2,r5 ! link map address\n\
235 sts pr,r7 ! return address\n\
237 jsr @r0 ! Call resolver.\n\
238 mov r1,r6 ! reloc offset\n\
239 lds.l @r15+,pr ! Get register content back.\n\
242 shal r3 ! Lode T flag.\n\
249 jmp @r0 ! Jump to function address.\n\
253 .long " GOTJMP (profile_fixup) "\n\
254 .size _dl_runtime_profile, .-_dl_runtime_profile\n\
258 # define ELF_MACHINE_RUNTIME_TRAMPOLINE asm ("\
260 .globl _dl_runtime_resolve\n\
261 .globl _dl_runtime_profile\n\
262 .type _dl_runtime_resolve, @function\n\
263 .type _dl_runtime_profile, @function\n\
265 _dl_runtime_resolve:\n\
266 _dl_runtime_profile:\n\
274 movt r3 ! Save T flag.\n\
282 mov r0,r4 ! PLT type\n\
283 mov r2,r5 ! link map address\n\
284 sts pr,r7 ! return address\n\
286 jsr @r0 ! Call resolver.\n\
287 mov r1,r6 ! reloc offset\n\
288 lds.l @r15+,pr ! Get register content back.\n\
291 shal r3 ! Lode T flag.\n\
298 jmp @r0 ! Jump to function address.\n\
302 .long " GOTJMP (fixup) "\n\
303 .size _dl_runtime_resolve, .-_dl_runtime_resolve\n\
304 .size _dl_runtime_profile, .-_dl_runtime_profile\n\
309 /* Mask identifying addresses reserved for the user program,
310 where the dynamic linker should not map anything. */
311 #define ELF_MACHINE_USER_ADDRESS_MASK 0x80000000UL
313 /* Initial entry point code for the dynamic linker.
314 The C function `_dl_start' is the real entry point;
315 its return value is the user program's entry point. */
317 #define RTLD_START asm ("\
320 .globl _dl_start_user\n\
323 mov.l .L_dl_start,r1\n\
324 mova .L_dl_start,r0\n\
329 ! Save the user entry point address in r8.\n\
331 ! Point r12 at the GOT.\n\
337 1: .long _GLOBAL_OFFSET_TABLE_\n\
338 2: ! See if we were run as a command with the executable file\n\
339 ! name as an extra leading argument.\n\
340 mov.l .L_dl_skip_args,r0\n\
341 mov.l @(r0,r12),r0\n\
343 ! Get the original argument count.\n\
345 ! Subtract _dl_skip_args from it.\n\
347 ! Adjust the stack pointer to skip _dl_skip_args words.\n\
350 ! Store back the modified argument count.\n\
352 ! Compute argv address and envp.\n\
358 mov.l .L_dl_loaded,r0\n\
359 mov.l @(r0,r12),r0\n\
362 mov.l .L_dl_init,r1\n\
363 mova .L_dl_init,r0\n\
367 1: ! Pass our finalizer function to the user in r4, as per ELF ABI.\n\
368 mov.l .L_dl_fini,r0\n\
369 mov.l @(r0,r12),r4\n\
370 ! Jump to the user's entry point.\n\
375 .long _dl_start@PLT\n\
377 .long _dl_skip_args@GOT\n\
379 .long _dl_init_internal@PLT\n\
381 .long _rtld_local@GOT\n\
383 .long _dl_fini@GOT\n\
387 /* ELF_RTYPE_CLASS_PLT iff TYPE describes relocation of a PLT entry or
388 TLS variable, so undefined references should not be allowed to
390 ELF_RTYPE_CLASS_NOCOPY iff TYPE should not be allowed to resolve to one
391 of the main executable's symbols, as for a COPY reloc. */
392 #if defined USE_TLS && (!defined RTLD_BOOTSTRAP || USE___THREAD)
393 # define elf_machine_type_class(type) \
394 ((((type) == R_SH_JMP_SLOT || (type) == R_SH_TLS_DTPMOD32 \
395 || (type) == R_SH_TLS_DTPOFF32 || (type) == R_SH_TLS_TPOFF32) \
396 * ELF_RTYPE_CLASS_PLT) \
397 | (((type) == R_SH_COPY) * ELF_RTYPE_CLASS_COPY))
399 #define elf_machine_type_class(type) \
400 ((((type) == R_SH_JMP_SLOT) * ELF_RTYPE_CLASS_PLT) \
401 | (((type) == R_SH_COPY) * ELF_RTYPE_CLASS_COPY))
404 /* A reloc type used for ld.so cmdline arg lookups to reject PLT entries. */
405 #define ELF_MACHINE_JMP_SLOT R_SH_JMP_SLOT
407 /* We define an initialization functions. This is called very early in
409 #define DL_PLATFORM_INIT dl_platform_init ()
411 static inline void __attribute__ ((unused))
412 dl_platform_init (void)
414 if (GLRO(dl_platform) != NULL && *GLRO(dl_platform) == '\0')
415 /* Avoid an empty string which would disturb us. */
416 GLRO(dl_platform) = NULL;
419 static inline Elf32_Addr
420 elf_machine_fixup_plt (struct link_map *map, lookup_t t,
421 const Elf32_Rela *reloc,
422 Elf32_Addr *reloc_addr, Elf32_Addr value)
424 return *reloc_addr = value;
427 /* Return the final value of a plt relocation. */
428 static inline Elf32_Addr
429 elf_machine_plt_value (struct link_map *map, const Elf32_Rela *reloc,
432 return value + reloc->r_addend;
435 #endif /* !dl_machine_h */
439 /* SH never uses Elf32_Rel relocations. */
440 #define ELF_MACHINE_NO_REL 1
442 /* Perform the relocation specified by RELOC and SYM (which is fully resolved).
443 MAP is the object containing the reloc. */
446 elf_machine_rela (struct link_map *map, const Elf32_Rela *reloc,
447 const Elf32_Sym *sym, const struct r_found_version *version,
448 void *const reloc_addr_arg)
450 Elf32_Addr *const reloc_addr = reloc_addr_arg;
451 const unsigned int r_type = ELF32_R_TYPE (reloc->r_info);
454 #define COPY_UNALIGNED_WORD(swp, twp, align) \
456 void *__s = (swp), *__t = (twp); \
457 unsigned char *__s1 = __s, *__t1 = __t; \
458 unsigned short *__s2 = __s, *__t2 = __t; \
459 unsigned long *__s4 = __s, *__t4 = __t; \
478 if (__builtin_expect (r_type == R_SH_RELATIVE, 0))
480 #ifndef RTLD_BOOTSTRAP
481 if (map != &GL(dl_rtld_map)) /* Already done in rtld itself. */
485 value = map->l_addr + reloc->r_addend;
488 COPY_UNALIGNED_WORD (reloc_addr_arg, &value,
489 (int) reloc_addr_arg & 3);
490 value += map->l_addr;
492 COPY_UNALIGNED_WORD (&value, reloc_addr_arg,
493 (int) reloc_addr_arg & 3);
496 #ifndef RTLD_BOOTSTRAP
497 else if (__builtin_expect (r_type == R_SH_NONE, 0))
502 const Elf32_Sym *const refsym = sym;
503 #if defined USE_TLS && !defined RTLD_BOOTSTRAP
504 struct link_map *sym_map = RESOLVE_MAP (&sym, version, r_type);
506 value = sym == NULL ? 0 : sym_map->l_addr + sym->st_value;
509 value = RESOLVE (&sym, version, r_type);
510 # ifndef RTLD_BOOTSTRAP
513 value += sym->st_value;
515 value += reloc->r_addend;
521 /* This can happen in trace mode if an object could not be
524 if (sym->st_size > refsym->st_size
525 || (sym->st_size < refsym->st_size && GLRO(dl_verbose)))
529 strtab = (const char *) D_PTR (map, l_info[DT_STRTAB]);
531 %s: Symbol `%s' has different size in shared object, consider re-linking\n",
532 rtld_progname ?: "<program name unknown>",
533 strtab + refsym->st_name);
535 memcpy (reloc_addr_arg, (void *) value,
536 MIN (sym->st_size, refsym->st_size));
540 /* These addresses are always aligned. */
543 #if defined USE_TLS && (!defined RTLD_BOOTSTRAP || USE___THREAD)
544 /* XXX Remove TLS relocations which are not needed. */
545 case R_SH_TLS_DTPMOD32:
546 # ifdef RTLD_BOOTSTRAP
547 /* During startup the dynamic linker is always the module
549 XXX If this relocation is necessary move before RESOLVE
553 /* Get the information from the link map returned by the
556 *reloc_addr = sym_map->l_tls_modid;
559 case R_SH_TLS_DTPOFF32:
560 # ifndef RTLD_BOOTSTRAP
561 /* During relocation all TLS symbols are defined and used.
562 Therefore the offset is already correct. */
564 *reloc_addr = sym->st_value;
567 case R_SH_TLS_TPOFF32:
568 /* The offset is positive, afterward from the thread pointer. */
569 # ifdef RTLD_BOOTSTRAP
570 *reloc_addr = map->l_tls_offset + sym->st_value + reloc->r_addend;
572 /* We know the offset of object the symbol is contained in.
573 It is a positive value which will be added to the thread
574 pointer. To get the variable position in the TLS block
575 we add the offset from that of the TLS block. */
578 CHECK_STATIC_TLS (map, sym_map);
579 *reloc_addr = sym_map->l_tls_offset + sym->st_value
587 #ifndef RTLD_BOOTSTRAP
588 /* This is defined in rtld.c, but nowhere in the static
589 libc.a; make the reference weak so static programs can
590 still link. This declaration cannot be done when
591 compiling rtld.c (i.e. #ifdef RTLD_BOOTSTRAP) because
592 rtld.c contains the common defn for _dl_rtld_map, which
593 is incompatible with a weak decl in the same file. */
595 weak_extern (_dl_rtld_map);
597 if (map == &GL(dl_rtld_map))
598 /* Undo the relocation done here during bootstrapping.
599 Now we will relocate it anew, possibly using a
600 binding found in the user program or a loaded library
601 rather than the dynamic linker's built-in definitions
602 used while loading those libraries. */
603 value -= map->l_addr + refsym->st_value + reloc->r_addend;
605 COPY_UNALIGNED_WORD (&value, reloc_addr_arg,
606 (int) reloc_addr_arg & 3);
610 value = (value - (Elf32_Addr) reloc_addr);
611 COPY_UNALIGNED_WORD (&value, reloc_addr_arg,
612 (int) reloc_addr_arg & 3);
615 _dl_reloc_bad_type (map, r_type, 0);
622 elf_machine_rela_relative (Elf32_Addr l_addr, const Elf32_Rela *reloc,
623 void *const reloc_addr_arg)
628 value = l_addr + reloc->r_addend;
631 COPY_UNALIGNED_WORD (reloc_addr_arg, &value, (int) reloc_addr_arg & 3);
634 COPY_UNALIGNED_WORD (&value, reloc_addr_arg, (int) reloc_addr_arg & 3);
636 #undef COPY_UNALIGNED_WORD
640 elf_machine_lazy_rel (struct link_map *map,
641 Elf32_Addr l_addr, const Elf32_Rela *reloc)
643 Elf32_Addr *const reloc_addr = (void *) (l_addr + reloc->r_offset);
644 /* Check for unexpected PLT reloc type. */
645 if (ELF32_R_TYPE (reloc->r_info) == R_SH_JMP_SLOT)
647 if (__builtin_expect (map->l_mach.plt, 0) == 0)
648 *reloc_addr += l_addr;
652 + (((Elf32_Addr) reloc_addr) - map->l_mach.gotplt) * 7;
655 _dl_reloc_bad_type (map, ELF32_R_TYPE (reloc->r_info), 1);