]>
git.ipfire.org Git - thirdparty/glibc.git/blob - sysdeps/sh/dl-machine.h
1 /* Machine-dependent ELF dynamic relocation inline functions. SH version.
2 Copyright (C) 1999, 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
23 /* Only dummy. This doesn't work. */
25 #define ELF_MACHINE_NAME "SH"
27 #include <sys/param.h>
31 /* Return nonzero iff ELF header is compatible with the running host. */
32 static inline int __attribute__ ((unused
))
33 elf_machine_matches_host (const Elf32_Ehdr
*ehdr
)
35 return ehdr
->e_machine
== EM_SH
;
39 /* Return the link-time address of _DYNAMIC. Conveniently, this is the
40 first element of the GOT. This must be inlined in a function which
42 static inline Elf32_Addr
__attribute__ ((unused
))
43 elf_machine_dynamic (void)
45 register Elf32_Addr
*got
;
46 asm ("mov r12,%0" :"=r" (got
));
51 /* Return the run-time load address of the shared object. */
52 static inline Elf32_Addr
__attribute__ ((unused
))
53 elf_machine_load_address (void)
63 1: .long _dl_start@GOT\n\
64 3: .long _dl_start@GOTOFF\n\
66 : "=r" (addr
) : : "r0", "r1", "r2");
71 /* Set up the loaded object described by L so its unrelocated PLT
72 entries will jump to the on-demand fixup code in dl-runtime.c. */
74 static inline int __attribute__ ((unused
))
75 elf_machine_runtime_setup (struct link_map
*l
, int lazy
, int profile
)
78 extern void _dl_runtime_resolve (Elf32_Word
);
79 extern void _dl_runtime_profile (Elf32_Word
);
81 if (l
->l_info
[DT_JMPREL
] && lazy
)
83 /* The GOT entries for functions in the PLT have not yet been filled
84 in. Their initial contents will arrange when called to load an
85 offset into the .rela.plt section and _GLOBAL_OFFSET_TABLE_[1],
86 and then jump to _GLOBAL_OFFSET_TABLE[2]. */
87 got
= (Elf32_Addr
*) D_PTR (l
, l_info
[DT_PLTGOT
]);
88 /* If a library is prelinked but we have to relocate anyway,
89 we have to be able to undo the prelinking of .got.plt.
90 The prelinker saved us here address of .plt + 36. */
93 l
->l_mach
.plt
= got
[1] + l
->l_addr
;
94 l
->l_mach
.gotplt
= (Elf32_Addr
) &got
[3];
96 got
[1] = (Elf32_Addr
) l
; /* Identify this shared object. */
98 /* The got[2] entry contains the address of a function which gets
99 called to get the address of a so far unresolved function and
100 jump to it. The profiling extension of the dynamic linker allows
101 to intercept the calls to collect information. In this case we
102 don't store the address in the GOT so that all future calls also
103 end in this function. */
106 got
[2] = (Elf32_Addr
) &_dl_runtime_profile
;
107 /* Say that we really want profiling and the timers are started. */
108 GL(dl_profile_map
) = l
;
111 /* This function will get called to fix up the GOT entry indicated by
112 the offset on the stack, and then jump to the resolved address. */
113 got
[2] = (Elf32_Addr
) &_dl_runtime_resolve
;
118 /* This code is used in dl-runtime.c to call the `fixup' function
119 and then redirect to the address it returns. */
121 #define ELF_MACHINE_RUNTIME_FIXUP_ARGS int plt_type
128 add r0,r2 ! Get GOT address in r2\n\
130 1: .long _GLOBAL_OFFSET_TABLE_\n\
133 #define GOTJMP(x) #x "@GOTOFF"
142 sts.l fpscr, @-r15\n\
146 fmov.s fr11, @-r15\n\
147 fmov.s fr10, @-r15\n\
161 fmov.s @r15+, fr10\n\
162 fmov.s @r15+, fr11\n\
170 # define ELF_MACHINE_RUNTIME_TRAMPOLINE asm ("\
172 .globl _dl_runtime_resolve\n\
173 .type _dl_runtime_resolve, @function\n\
175 _dl_runtime_resolve:\n\
183 movt r3 ! Save T flag.\n\
191 mov r0,r4 ! PLT type\n\
192 mov r2,r5 ! link map address\n\
194 jsr @r0 ! Call resolver.\n\
195 mov r1,r6 ! reloc offset\n\
196 lds.l @r15+,pr ! Get register content back.\n\
199 shal r3 ! Lode T flag.\n\
206 jmp @r0 ! Jump to function address.\n\
210 .long " GOTJMP (fixup) "\n\
211 .size _dl_runtime_resolve, .-_dl_runtime_resolve\n\
213 .globl _dl_runtime_profile\n\
214 .type _dl_runtime_profile, @function\n\
216 _dl_runtime_profile:\n\
224 movt r3 ! Save T flag.\n\
232 mov r0,r4 ! PLT type\n\
233 mov r2,r5 ! link map address\n\
234 sts pr,r7 ! return address\n\
236 jsr @r0 ! Call resolver.\n\
237 mov r1,r6 ! reloc offset\n\
238 lds.l @r15+,pr ! Get register content back.\n\
241 shal r3 ! Lode T flag.\n\
248 jmp @r0 ! Jump to function address.\n\
252 .long " GOTJMP (profile_fixup) "\n\
253 .size _dl_runtime_profile, .-_dl_runtime_profile\n\
257 # define ELF_MACHINE_RUNTIME_TRAMPOLINE asm ("\
259 .globl _dl_runtime_resolve\n\
260 .globl _dl_runtime_profile\n\
261 .type _dl_runtime_resolve, @function\n\
262 .type _dl_runtime_profile, @function\n\
264 _dl_runtime_resolve:\n\
265 _dl_runtime_profile:\n\
273 movt r3 ! Save T flag.\n\
281 mov r0,r4 ! PLT type\n\
282 mov r2,r5 ! link map address\n\
283 sts pr,r7 ! return address\n\
285 jsr @r0 ! Call resolver.\n\
286 mov r1,r6 ! reloc offset\n\
287 lds.l @r15+,pr ! Get register content back.\n\
290 shal r3 ! Lode T flag.\n\
297 jmp @r0 ! Jump to function address.\n\
301 .long " GOTJMP (fixup) "\n\
302 .size _dl_runtime_resolve, .-_dl_runtime_resolve\n\
303 .size _dl_runtime_profile, .-_dl_runtime_profile\n\
308 /* Mask identifying addresses reserved for the user program,
309 where the dynamic linker should not map anything. */
310 #define ELF_MACHINE_USER_ADDRESS_MASK 0x80000000UL
312 /* Initial entry point code for the dynamic linker.
313 The C function `_dl_start' is the real entry point;
314 its return value is the user program's entry point. */
316 #define RTLD_START asm ("\
319 .globl _dl_start_user\n\
322 mov.l .L_dl_start,r1\n\
323 mova .L_dl_start,r0\n\
328 ! Save the user entry point address in r8.\n\
330 ! Point r12 at the GOT.\n\
336 1: .long _GLOBAL_OFFSET_TABLE_\n\
337 2: ! See if we were run as a command with the executable file\n\
338 ! name as an extra leading argument.\n\
339 mov.l .L_dl_skip_args,r0\n\
340 mov.l @(r0,r12),r0\n\
342 ! Get the original argument count.\n\
344 ! Subtract _dl_skip_args from it.\n\
346 ! Adjust the stack pointer to skip _dl_skip_args words.\n\
349 ! Store back the modified argument count.\n\
351 ! Compute argv address and envp.\n\
357 mov.l .L_dl_loaded,r0\n\
358 mov.l @(r0,r12),r0\n\
361 mov.l .L_dl_init,r1\n\
362 mova .L_dl_init,r0\n\
366 1: ! Clear the startup flag.\n\
367 mov.l .L_dl_starting_up,r0\n\
368 mov.l @(r0,r12),r0\n\
371 ! Pass our finalizer function to the user in r4, as per ELF ABI.\n\
372 mov.l .L_dl_fini,r0\n\
373 mov.l @(r0,r12),r4\n\
374 ! Jump to the user's entry point.\n\
379 .long _dl_start@PLT\n\
381 .long _dl_skip_args@GOT\n\
383 .long _dl_init_internal@PLT\n\
385 .long _rtld_local@GOT\n\
386 .L_dl_starting_up:\n\
387 .long _dl_starting_up@GOT\n\
389 .long _dl_fini@GOT\n\
393 /* ELF_RTYPE_CLASS_PLT iff TYPE describes relocation of a PLT entry or
394 TLS variable, so undefined references should not be allowed to
396 ELF_RTYPE_CLASS_NOCOPY iff TYPE should not be allowed to resolve to one
397 of the main executable's symbols, as for a COPY reloc. */
398 #if defined USE_TLS && (!defined RTLD_BOOTSTRAP || USE___THREAD)
399 # define elf_machine_type_class(type) \
400 ((((type) == R_SH_JMP_SLOT || (type) == R_SH_TLS_DTPMOD32 \
401 || (type) == R_SH_TLS_DTPOFF32 || (type) == R_SH_TLS_TPOFF32) \
402 * ELF_RTYPE_CLASS_PLT) \
403 | (((type) == R_SH_COPY) * ELF_RTYPE_CLASS_COPY))
405 #define elf_machine_type_class(type) \
406 ((((type) == R_SH_JMP_SLOT) * ELF_RTYPE_CLASS_PLT) \
407 | (((type) == R_SH_COPY) * ELF_RTYPE_CLASS_COPY))
410 /* A reloc type used for ld.so cmdline arg lookups to reject PLT entries. */
411 #define ELF_MACHINE_JMP_SLOT R_SH_JMP_SLOT
413 /* We define an initialization functions. This is called very early in
415 #define DL_PLATFORM_INIT dl_platform_init ()
417 static inline void __attribute__ ((unused
))
418 dl_platform_init (void)
420 if (GL(dl_platform
) != NULL
&& *GL(dl_platform
) == '\0')
421 /* Avoid an empty string which would disturb us. */
422 GL(dl_platform
) = NULL
;
425 static inline Elf32_Addr
426 elf_machine_fixup_plt (struct link_map
*map
, lookup_t t
,
427 const Elf32_Rela
*reloc
,
428 Elf32_Addr
*reloc_addr
, Elf32_Addr value
)
430 return *reloc_addr
= value
;
433 /* Return the final value of a plt relocation. */
434 static inline Elf32_Addr
435 elf_machine_plt_value (struct link_map
*map
, const Elf32_Rela
*reloc
,
438 return value
+ reloc
->r_addend
;
441 #endif /* !dl_machine_h */
445 /* SH never uses Elf32_Rel relocations. */
446 #define ELF_MACHINE_NO_REL 1
448 /* Perform the relocation specified by RELOC and SYM (which is fully resolved).
449 MAP is the object containing the reloc. */
452 elf_machine_rela (struct link_map
*map
, const Elf32_Rela
*reloc
,
453 const Elf32_Sym
*sym
, const struct r_found_version
*version
,
454 void *const reloc_addr_arg
)
456 Elf32_Addr
*const reloc_addr
= reloc_addr_arg
;
457 const unsigned int r_type
= ELF32_R_TYPE (reloc
->r_info
);
460 #define COPY_UNALIGNED_WORD(swp, twp, align) \
462 void *__s = (swp), *__t = (twp); \
466 *(unsigned long *) __t = *(unsigned long *) __s; \
469 *((unsigned short *) __t)++ = *((unsigned short *) __s)++; \
470 *((unsigned short *) __t) = *((unsigned short *) __s); \
473 *((unsigned char *) __t)++ = *((unsigned char *) __s)++; \
474 *((unsigned char *) __t)++ = *((unsigned char *) __s)++; \
475 *((unsigned char *) __t)++ = *((unsigned char *) __s)++; \
476 *((unsigned char *) __t) = *((unsigned char *) __s); \
481 if (__builtin_expect (r_type
== R_SH_RELATIVE
, 0))
483 #ifndef RTLD_BOOTSTRAP
484 if (map
!= &GL(dl_rtld_map
)) /* Already done in rtld itself. */
488 value
= map
->l_addr
+ reloc
->r_addend
;
491 COPY_UNALIGNED_WORD (reloc_addr_arg
, &value
,
492 (int) reloc_addr_arg
& 3);
493 value
+= map
->l_addr
;
495 COPY_UNALIGNED_WORD (&value
, reloc_addr_arg
,
496 (int) reloc_addr_arg
& 3);
499 #ifndef RTLD_BOOTSTRAP
500 else if (__builtin_expect (r_type
== R_SH_NONE
, 0))
505 const Elf32_Sym
*const refsym
= sym
;
506 #if defined USE_TLS && !defined RTLD_BOOTSTRAP
507 struct link_map
*sym_map
= RESOLVE_MAP (&sym
, version
, r_type
);
509 value
= sym
== NULL
? 0 : sym_map
->l_addr
+ sym
->st_value
;
512 value
= RESOLVE (&sym
, version
, r_type
);
513 # ifndef RTLD_BOOTSTRAP
516 value
+= sym
->st_value
;
518 value
+= reloc
->r_addend
;
524 /* This can happen in trace mode if an object could not be
527 if (sym
->st_size
> refsym
->st_size
528 || (sym
->st_size
< refsym
->st_size
&& GL(dl_verbose
)))
532 strtab
= (const char *) D_PTR (map
, l_info
[DT_STRTAB
]);
534 %s: Symbol `%s' has different size in shared object, consider re-linking\n",
535 rtld_progname
?: "<program name unknown>",
536 strtab
+ refsym
->st_name
);
538 memcpy (reloc_addr_arg
, (void *) value
,
539 MIN (sym
->st_size
, refsym
->st_size
));
543 /* These addresses are always aligned. */
546 #if defined USE_TLS && (!defined RTLD_BOOTSTRAP || USE___THREAD)
547 /* XXX Remove TLS relocations which are not needed. */
548 case R_SH_TLS_DTPMOD32
:
549 # ifdef RTLD_BOOTSTRAP
550 /* During startup the dynamic linker is always the module
552 XXX If this relocation is necessary move before RESOLVE
556 /* Get the information from the link map returned by the
559 *reloc_addr
= sym_map
->l_tls_modid
;
562 case R_SH_TLS_DTPOFF32
:
563 # ifndef RTLD_BOOTSTRAP
564 /* During relocation all TLS symbols are defined and used.
565 Therefore the offset is already correct. */
567 *reloc_addr
= sym
->st_value
;
570 case R_SH_TLS_TPOFF32
:
571 /* The offset is positive, afterward from the thread pointer. */
572 # ifdef RTLD_BOOTSTRAP
573 *reloc_addr
= map
->l_tls_offset
+ sym
->st_value
+ reloc
->r_addend
;
575 /* We know the offset of object the symbol is contained in.
576 It is a positive value which will be added to the thread
577 pointer. To get the variable position in the TLS block
578 we add the offset from that of the TLS block. */
579 CHECK_STATIC_TLS (map
, sym_map
);
581 = ((sym
== NULL
? 0 : sym_map
->l_tls_offset
+ sym
->st_value
)
588 #ifndef RTLD_BOOTSTRAP
589 /* This is defined in rtld.c, but nowhere in the static
590 libc.a; make the reference weak so static programs can
591 still link. This declaration cannot be done when
592 compiling rtld.c (i.e. #ifdef RTLD_BOOTSTRAP) because
593 rtld.c contains the common defn for _dl_rtld_map, which
594 is incompatible with a weak decl in the same file. */
596 weak_extern (GL(dl_rtld_map
));
598 if (map
== &GL(dl_rtld_map
))
599 /* Undo the relocation done here during bootstrapping.
600 Now we will relocate it anew, possibly using a
601 binding found in the user program or a loaded library
602 rather than the dynamic linker's built-in definitions
603 used while loading those libraries. */
604 value
-= map
->l_addr
+ refsym
->st_value
+ reloc
->r_addend
;
606 COPY_UNALIGNED_WORD (&value
, reloc_addr_arg
,
607 (int) reloc_addr_arg
& 3);
611 value
= (value
- (Elf32_Addr
) reloc_addr
);
612 COPY_UNALIGNED_WORD (&value
, reloc_addr_arg
,
613 (int) reloc_addr_arg
& 3);
616 _dl_reloc_bad_type (map
, r_type
, 0);
623 elf_machine_rela_relative (Elf32_Addr l_addr
, const Elf32_Rela
*reloc
,
624 void *const reloc_addr_arg
)
626 Elf32_Addr
*const reloc_addr
= reloc_addr_arg
;
630 value
= l_addr
+ reloc
->r_addend
;
633 COPY_UNALIGNED_WORD (reloc_addr_arg
, &value
, (int) reloc_addr_arg
& 3);
636 COPY_UNALIGNED_WORD (&value
, reloc_addr_arg
, (int) reloc_addr_arg
& 3);
638 #undef COPY_UNALIGNED_WORD
642 elf_machine_lazy_rel (struct link_map
*map
,
643 Elf32_Addr l_addr
, const Elf32_Rela
*reloc
)
645 Elf32_Addr
*const reloc_addr
= (void *) (l_addr
+ reloc
->r_offset
);
646 /* Check for unexpected PLT reloc type. */
647 if (ELF32_R_TYPE (reloc
->r_info
) == R_SH_JMP_SLOT
)
649 if (__builtin_expect (map
->l_mach
.plt
, 0) == 0)
650 *reloc_addr
+= l_addr
;
654 + (((Elf32_Addr
) reloc_addr
) - map
->l_mach
.gotplt
) * 7;
657 _dl_reloc_bad_type (map
, ELF32_R_TYPE (reloc
->r_info
), 1);