]> git.ipfire.org Git - thirdparty/glibc.git/blob - sysdeps/sh/dl-machine.h
Update.
[thirdparty/glibc.git] / sysdeps / sh / dl-machine.h
1 /* Machine-dependent ELF dynamic relocation inline functions. SH version.
2 Copyright (C) 1999, 2000, 2001 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
9
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 Library General Public License for more details.
14
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
19
20 #ifndef dl_machine_h
21 #define dl_machine_h
22
23 /* Only dummy. This doesn't work. */
24
25 #define ELF_MACHINE_NAME "SH"
26
27 #include <sys/param.h>
28
29 #include <assert.h>
30
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)
34 {
35 return ehdr->e_machine == EM_SH;
36 }
37
38
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
41 uses global data. */
42 static inline Elf32_Addr __attribute__ ((unused))
43 elf_machine_dynamic (void)
44 {
45 register Elf32_Addr *got;
46 asm ("mov r12,%0" :"=r" (got));
47 return *got;
48 }
49
50
51 /* Return the run-time load address of the shared object. */
52 static inline Elf32_Addr __attribute__ ((unused))
53 elf_machine_load_address (void)
54 {
55 Elf32_Addr addr;
56 asm ("mov.l .L1,r0
57 mov.l .L3,r2
58 add r12,r2
59 mov.l @(r0,r12),r0
60 bra .L2
61 sub r0,r2
62 .align 2
63 .L1: .long _dl_start@GOT
64 .L3: .long _dl_start@GOTOFF
65 .L2: mov r2,%0"
66 : "=r" (addr) : : "r0", "r1", "r2");
67 return addr;
68 }
69
70
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. */
73
74 static inline int __attribute__ ((unused))
75 elf_machine_runtime_setup (struct link_map *l, int lazy, int profile)
76 {
77 Elf32_Addr *got;
78 extern void _dl_runtime_resolve (Elf32_Word);
79 extern void _dl_runtime_profile (Elf32_Word);
80
81 if (l->l_info[DT_JMPREL] && lazy)
82 {
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 got[1] = (Elf32_Addr) l; /* Identify this shared object. */
89
90 /* The got[2] entry contains the address of a function which gets
91 called to get the address of a so far unresolved function and
92 jump to it. The profiling extension of the dynamic linker allows
93 to intercept the calls to collect information. In this case we
94 don't store the address in the GOT so that all future calls also
95 end in this function. */
96 if (profile)
97 {
98 got[2] = (Elf32_Addr) &_dl_runtime_profile;
99 /* Say that we really want profiling and the timers are started. */
100 _dl_profile_map = l;
101 }
102 else
103 /* This function will get called to fix up the GOT entry indicated by
104 the offset on the stack, and then jump to the resolved address. */
105 got[2] = (Elf32_Addr) &_dl_runtime_resolve;
106 }
107 return lazy;
108 }
109
110 /* This code is used in dl-runtime.c to call the `fixup' function
111 and then redirect to the address it returns. */
112
113 #define ELF_MACHINE_RUNTIME_FIXUP_ARGS int plt_type
114
115 #ifdef SHARED
116 #define FUN_ADDR "\
117 mov.l 1f,r2
118 mova 1f,r0
119 bra 2f
120 add r0,r2 ! Get GOT address in r2
121 0: .align 2
122 1: .long _GLOBAL_OFFSET_TABLE_
123 2: mov.l 3f,r0
124 add r2,r0"
125 #define GOTJMP(x) #x "@GOTOFF"
126 #else
127 #define FUN_ADDR "\
128 mov.l 3f,r0"
129 #define GOTJMP(x) #x
130 #endif
131
132 #ifdef HAVE_FPU
133 #define FGR_SAVE "\
134 sts.l fpscr, @-r15
135 mov #8,r3
136 swap.w r3, r3
137 lds r3, fpscr
138 fmov.s fr11, @-r15
139 fmov.s fr10, @-r15
140 fmov.s fr9, @-r15
141 fmov.s fr8, @-r15
142 fmov.s fr7, @-r15
143 fmov.s fr6, @-r15
144 fmov.s fr5, @-r15
145 fmov.s fr4, @-r15"
146 #define FGR_LOAD "\
147 fmov.s @r15+, fr4
148 fmov.s @r15+, fr5
149 fmov.s @r15+, fr6
150 fmov.s @r15+, fr7
151 fmov.s @r15+, fr8
152 fmov.s @r15+, fr9
153 fmov.s @r15+, fr10
154 fmov.s @r15+, fr11
155 lds.l @r15+, fpscr"
156 #else
157 #define FGR_SAVE ""
158 #define FGR_LOAD ""
159 #endif
160
161 #ifndef PROF
162 # define ELF_MACHINE_RUNTIME_TRAMPOLINE asm ("\
163 .text
164 .globl _dl_runtime_resolve
165 .type _dl_runtime_resolve, @function
166 .align 5
167 _dl_runtime_resolve:
168 mov.l r3,@-r15
169 mov.l r4,@-r15
170 mov.l r5,@-r15
171 mov.l r6,@-r15
172 mov.l r7,@-r15
173 mov.l r12,@-r15
174 movt r3 ! Save T flag.
175 mov.l r3,@-r15
176 " FGR_SAVE "
177 sts.l pr,@-r15
178 mov r0,r4 ! PLT type
179 mov r2,r5 ! link map address
180 " FUN_ADDR "
181 jsr @r0 ! Call resolver.
182 mov r1,r6 ! reloc offset
183 lds.l @r15+,pr ! Get register content back.
184 " FGR_LOAD "
185 mov.l @r15+,r3
186 shal r3 ! Lode T flag.
187 mov.l @r15+,r12
188 mov.l @r15+,r7
189 mov.l @r15+,r6
190 mov.l @r15+,r5
191 mov.l @r15+,r4
192 jmp @r0 ! Jump to function address.
193 mov.l @r15+,r3
194 .align 2
195 3:
196 .long " GOTJMP (fixup) "
197 .size _dl_runtime_resolve, .-_dl_runtime_resolve
198
199 .globl _dl_runtime_profile
200 .type _dl_runtime_profile, @function
201 .align 5
202 _dl_runtime_profile:
203 mov.l r3,@-r15
204 mov.l r4,@-r15
205 mov.l r5,@-r15
206 mov.l r6,@-r15
207 mov.l r7,@-r15
208 mov.l r12,@-r15
209 movt r3 ! Save T flag.
210 mov.l r3,@-r15
211 " FGR_SAVE "
212 sts.l pr,@-r15
213 mov r0,r4 ! PLT type
214 mov r2,r5 ! link map address
215 sts pr,r7 ! return address
216 " FUN_ADDR "
217 jsr @r0 ! Call resolver.
218 mov r1,r6 ! reloc offset
219 lds.l @r15+,pr ! Get register content back.
220 " FGR_LOAD "
221 mov.l @r15+,r3
222 shal r3 ! Lode T flag.
223 mov.l @r15+,r12
224 mov.l @r15+,r7
225 mov.l @r15+,r6
226 mov.l @r15+,r5
227 mov.l @r15+,r4
228 jmp @r0 ! Jump to function address.
229 mov.l @r15+,r3
230 .align 2
231 3:
232 .long " GOTJMP (profile_fixup) "
233 .size _dl_runtime_profile, .-_dl_runtime_profile
234 .previous
235 ");
236 #else
237 # define ELF_MACHINE_RUNTIME_TRAMPOLINE asm ("\
238 .text
239 .globl _dl_runtime_resolve
240 .globl _dl_runtime_profile
241 .type _dl_runtime_resolve, @function
242 .type _dl_runtime_profile, @function
243 .align 5
244 _dl_runtime_resolve:
245 _dl_runtime_profile:
246 mov.l r3,@-r15
247 mov.l r4,@-r15
248 mov.l r5,@-r15
249 mov.l r6,@-r15
250 mov.l r7,@-r15
251 mov.l r12,@-r15
252 movt r3 ! Save T flag.
253 mov.l r3,@-r15
254 " FGR_SAVE "
255 sts.l pr,@-r15
256 mov r0,r4 ! PLT type
257 mov r2,r5 ! link map address
258 sts pr,r7 ! return address
259 " FUN_ADDR "
260 jsr @r0 ! Call resolver.
261 mov r1,r6 ! reloc offset
262 lds.l @r15+,pr ! Get register content back.
263 " FGR_LOAD "
264 mov.l @r15+,r3
265 shal r3 ! Lode T flag.
266 mov.l @r15+,r12
267 mov.l @r15+,r7
268 mov.l @r15+,r6
269 mov.l @r15+,r5
270 mov.l @r15+,r4
271 jmp @r0 ! Jump to function address.
272 mov.l @r15+,r3
273 .align 2
274 3:
275 .long " GOTJMP (fixup) "
276 .size _dl_runtime_resolve, .-_dl_runtime_resolve
277 .size _dl_runtime_profile, .-_dl_runtime_profile
278 .previous
279 ");
280 #endif
281
282 /* Mask identifying addresses reserved for the user program,
283 where the dynamic linker should not map anything. */
284 #define ELF_MACHINE_USER_ADDRESS_MASK 0x80000000UL
285
286 /* Initial entry point code for the dynamic linker.
287 The C function `_dl_start' is the real entry point;
288 its return value is the user program's entry point. */
289
290 #define RTLD_START asm ("\
291 .text\n\
292 .globl _start\n\
293 .globl _dl_start_user\n\
294 _start:\n\
295 mov r15,r4\n\
296 mov.l .L_dl_start,r1\n\
297 mova .L_dl_start,r0\n\
298 add r1,r0\n\
299 jsr @r0\n\
300 nop\n\
301 _dl_start_user:\n\
302 ! Save the user entry point address in r8.\n\
303 mov r0,r8\n\
304 ! Point r12 at the GOT.\n\
305 mov.l 1f,r12\n\
306 mova 1f,r0\n\
307 bra 2f\n\
308 add r0,r12\n\
309 .align 2\n\
310 1: .long _GLOBAL_OFFSET_TABLE_\n\
311 2: ! Store the highest stack address\n\
312 mov.l .L_stack_end,r0\n\
313 mov.l @(r0,r12),r0\n\
314 mov.l r15,@r0\n\
315 ! See if we were run as a command with the executable file\n\
316 ! name as an extra leading argument.\n\
317 mov.l .L_dl_skip_args,r0\n\
318 mov.l @(r0,r12),r0\n\
319 mov.l @r0,r0\n\
320 ! Get the original argument count.\n\
321 mov.l @r15,r5\n\
322 ! Subtract _dl_skip_args from it.\n\
323 sub r0,r5\n\
324 ! Adjust the stack pointer to skip _dl_skip_args words.\n\
325 shll2 r0\n\
326 add r0,r15\n\
327 ! Store back the modified argument count.\n\
328 mov.l r5,@r15\n\
329 ! Compute argv address and envp.\n\
330 mov r15,r6\n\
331 add #4,r6\n\
332 mov r5,r7\n\
333 shll2 r7\n\
334 add r15,r7\n\
335 mov.l .L_dl_loaded,r0\n\
336 mov.l @(r0,r12),r0\n\
337 mov.l @r0,r4\n\
338 ! Call _dl_init.\n\
339 mov.l .L_dl_init,r1\n\
340 mova .L_dl_init,r0\n\
341 add r1,r0\n\
342 jsr @r0\n\
343 nop\n\
344 1: ! Clear the startup flag.\n\
345 mov.l .L_dl_starting_up,r0\n\
346 mov.l @(r0,r12),r0\n\
347 mov #0,r2\n\
348 mov.l r2,@r0\n\
349 ! Pass our finalizer function to the user in r4, as per ELF ABI.\n\
350 mov.l .L_dl_fini,r0\n\
351 mov.l @(r0,r12),r4\n\
352 ! Jump to the user's entry point.\n\
353 jmp @r8\n\
354 nop\n\
355 .align 2\n\
356 .L_dl_start:\n\
357 .long _dl_start@PLT\n\
358 .L_stack_end:\n\
359 .long __libc_stack_end@GOT\n\
360 .L_dl_skip_args:\n\
361 .long _dl_skip_args@GOT\n\
362 .L_dl_init:\n\
363 .long _dl_init@PLT\n\
364 .L_dl_loaded:\n\
365 .long _dl_loaded@GOT\n\
366 .L_dl_starting_up:\n\
367 .long _dl_starting_up@GOT\n\
368 .L_dl_fini:\n\
369 .long _dl_fini@GOT\n\
370 .previous\n\
371 ");
372
373 /* Nonzero iff TYPE should not be allowed to resolve to one of
374 the main executable's symbols, as for a COPY reloc. */
375 #define elf_machine_lookup_noexec_p(type) ((type) == R_SH_COPY)
376
377 /* Nonzero iff TYPE describes relocation of a PLT entry, so
378 PLT entries should not be allowed to define the value. */
379 #define elf_machine_lookup_noplt_p(type) ((type) == R_SH_JMP_SLOT)
380
381 /* A reloc type used for ld.so cmdline arg lookups to reject PLT entries. */
382 #define ELF_MACHINE_JMP_SLOT R_SH_JMP_SLOT
383
384 /* We define an initialization functions. This is called very early in
385 _dl_sysdep_start. */
386 #define DL_PLATFORM_INIT dl_platform_init ()
387
388 extern const char *_dl_platform;
389
390 static inline void __attribute__ ((unused))
391 dl_platform_init (void)
392 {
393 if (_dl_platform != NULL && *_dl_platform == '\0')
394 /* Avoid an empty string which would disturb us. */
395 _dl_platform = NULL;
396 }
397
398 static inline Elf32_Addr
399 elf_machine_fixup_plt (struct link_map *map, lookup_t t,
400 const Elf32_Rela *reloc,
401 Elf32_Addr *reloc_addr, Elf32_Addr value)
402 {
403 return *reloc_addr = value;
404 }
405
406 /* Return the final value of a plt relocation. */
407 static inline Elf32_Addr
408 elf_machine_plt_value (struct link_map *map, const Elf32_Rela *reloc,
409 Elf32_Addr value)
410 {
411 return value + reloc->r_addend;
412 }
413
414 #endif /* !dl_machine_h */
415
416 #ifdef RESOLVE
417
418 /* SH never uses Elf32_Rel relocations. */
419 #define ELF_MACHINE_NO_REL 1
420
421 extern char **_dl_argv;
422
423 /* Perform the relocation specified by RELOC and SYM (which is fully resolved).
424 MAP is the object containing the reloc. */
425
426 static inline void
427 elf_machine_rela (struct link_map *map, const Elf32_Rela *reloc,
428 const Elf32_Sym *sym, const struct r_found_version *version,
429 Elf32_Addr *const reloc_addr)
430 {
431 if (ELF32_R_TYPE (reloc->r_info) == R_SH_RELATIVE)
432 {
433 #ifndef RTLD_BOOTSTRAP
434 if (map != &_dl_rtld_map) /* Already done in rtld itself. */
435 #endif
436 {
437 if (reloc->r_addend)
438 *reloc_addr = map->l_addr + reloc->r_addend;
439 else
440 *reloc_addr += map->l_addr;
441 }
442 }
443 else if (ELF32_R_TYPE (reloc->r_info) != R_SH_NONE)
444 {
445 const Elf32_Sym *const refsym = sym;
446 Elf32_Addr value = RESOLVE (&sym, version, ELF32_R_TYPE (reloc->r_info));
447 if (sym)
448 value += sym->st_value;
449 value += reloc->r_addend;
450
451 switch (ELF32_R_TYPE (reloc->r_info))
452 {
453 case R_SH_COPY:
454 if (sym == NULL)
455 /* This can happen in trace mode if an object could not be
456 found. */
457 break;
458 if (sym->st_size > refsym->st_size
459 || (sym->st_size < refsym->st_size && _dl_verbose))
460 {
461 const char *strtab;
462
463 strtab = (const char *) D_PTR (map, l_info[DT_STRTAB]);
464 _dl_error_printf ("\
465 %s: Symbol `%s' has different size in shared object, consider re-linking\n"
466 _dl_argv[0] ?: "<program name unknown>",
467 strtab + refsym->st_name);
468 }
469 memcpy (reloc_addr, (void *) value, MIN (sym->st_size,
470 refsym->st_size));
471 break;
472 case R_SH_GLOB_DAT:
473 case R_SH_JMP_SLOT:
474 *reloc_addr = value;
475 break;
476 case R_SH_DIR32:
477 {
478 #ifndef RTLD_BOOTSTRAP
479 /* This is defined in rtld.c, but nowhere in the static
480 libc.a; make the reference weak so static programs can
481 still link. This declaration cannot be done when
482 compiling rtld.c (i.e. #ifdef RTLD_BOOTSTRAP) because
483 rtld.c contains the common defn for _dl_rtld_map, which
484 is incompatible with a weak decl in the same file. */
485 weak_extern (_dl_rtld_map);
486 if (map == &_dl_rtld_map)
487 /* Undo the relocation done here during bootstrapping.
488 Now we will relocate it anew, possibly using a
489 binding found in the user program or a loaded library
490 rather than the dynamic linker's built-in definitions
491 used while loading those libraries. */
492 value -= map->l_addr + refsym->st_value + reloc->r_addend;
493 #endif
494 *reloc_addr = value;
495 break;
496 }
497 case R_SH_REL32:
498 *reloc_addr = (value - (Elf32_Addr) reloc_addr);
499 break;
500 default:
501 _dl_reloc_bad_type (map, ELF32_R_TYPE (reloc->r_info), 0);
502 break;
503 }
504 }
505 }
506
507 static inline void
508 elf_machine_lazy_rel (struct link_map *map,
509 Elf32_Addr l_addr, const Elf32_Rela *reloc)
510 {
511 Elf32_Addr *const reloc_addr = (void *) (l_addr + reloc->r_offset);
512 /* Check for unexpected PLT reloc type. */
513 if (ELF32_R_TYPE (reloc->r_info) == R_SH_JMP_SLOT)
514 *reloc_addr += l_addr;
515 else
516 _dl_reloc_bad_type (map, ELF32_R_TYPE (reloc->r_info), 1);
517 }
518
519 #endif /* RESOLVE */