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