Update.
[glibc.git] / sysdeps / sparc / sparc64 / dl-machine.h
blob33def6b45506dd0fdcc32cb6d19e41be58fc8181
1 /* Machine-dependent ELF dynamic relocation inline functions. Sparc64 version.
2 Copyright (C) 1997, 1998 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 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.
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.
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
17 not, write to the Free Software Foundation, Inc.,
18 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
20 #define ELF_MACHINE_NAME "sparc64"
22 #include <assert.h>
23 #include <string.h>
24 #include <sys/param.h>
25 #include <elf/ldsodefs.h>
26 #include <sysdep.h>
28 /* Return nonzero iff E_MACHINE is compatible with the running host. */
29 static inline int
30 elf_machine_matches_host (Elf64_Half e_machine)
32 return e_machine == EM_SPARCV9;
35 /* Return the link-time address of _DYNAMIC. Conveniently, this is the
36 first element of the GOT. This must be inlined in a function which
37 uses global data. */
38 static inline Elf64_Addr
39 elf_machine_dynamic (void)
41 register Elf64_Addr *elf_pic_register __asm__("%l7");
43 return *elf_pic_register;
46 /* Return the run-time load address of the shared object. */
47 static inline Elf64_Addr
48 elf_machine_load_address (void)
50 register Elf64_Addr elf_pic_register __asm__("%l7");
51 Elf64_Addr pc, la;
53 /* Utilize the fact that a local .got entry will be partially
54 initialized at startup awaiting its RELATIVE fixup. */
56 __asm("sethi %%hi(.Load_address), %1\n"
57 ".Load_address:\n\t"
58 "rd %%pc, %0\n\t"
59 "or %1, %%lo(.Load_address), %1\n\t"
60 : "=r"(pc), "=r"(la));
62 return pc - *(Elf64_Addr *)(elf_pic_register + la);
65 /* We have 3 cases to handle. And we code different code sequences
66 for each one. I love V9 code models... */
67 static inline void
68 elf_machine_fixup_plt(struct link_map *map, const Elf64_Rela *reloc,
69 Elf64_Addr *reloc_addr, Elf64_Addr value)
71 unsigned int *insns = (unsigned int *) reloc_addr;
72 Elf64_Addr plt_vaddr = (Elf64_Addr) reloc_addr;
74 /* Now move plt_vaddr up to the call instruction. */
75 plt_vaddr += (2 * 4);
77 /* 32-bit Sparc style, the target is in the lower 32-bits of
78 address space. */
79 if ((value >> 32) == 0)
81 /* sethi %hi(target), %g1
82 jmpl %g1 + %lo(target), %g0 */
84 insns[2] = 0x81c06000 | (value & 0x3ff);
85 __asm __volatile ("flush %0 + 8" : : "r" (insns));
87 insns[1] = 0x03000000 | ((unsigned int)(value >> 10));
88 __asm __volatile ("flush %0 + 4" : : "r" (insns));
90 /* We can also get somewhat simple sequences if the distance between
91 the target and the PLT entry is within +/- 2GB. */
92 else if ((plt_vaddr > value
93 && ((plt_vaddr - value) >> 32) == 0)
94 || (value > plt_vaddr
95 && ((value - plt_vaddr) >> 32) == 0))
97 unsigned int displacement;
99 if (plt_vaddr > value)
100 displacement = (0 - (plt_vaddr - value));
101 else
102 displacement = value - plt_vaddr;
104 /* mov %o7, %g1
105 call displacement
106 mov %g1, %o7 */
108 insns[3] = 0x9e100001;
109 __asm __volatile ("flush %0 + 12" : : "r" (insns));
111 insns[2] = 0x40000000 | (displacement >> 2);
112 __asm __volatile ("flush %0 + 8" : : "r" (insns));
114 insns[1] = 0x8210000f;
115 __asm __volatile ("flush %0 + 4" : : "r" (insns));
117 /* Worst case, ho hum... */
118 else
120 unsigned int high32 = (value >> 32);
121 unsigned int low32 = (unsigned int) value;
123 /* ??? Some tricks can be stolen from the sparc64 egcs backend
124 constant formation code I wrote. -DaveM */
126 /* sethi %hh(value), %g1
127 sethi %lm(value), %g2
128 or %g1, %hl(value), %g1
129 or %g2, %lo(value), %g2
130 sllx %g1, 32, %g1
131 jmpl %g1 + %g2, %g0
132 nop */
134 insns[6] = 0x81c04002;
135 __asm __volatile ("flush %0 + 24" : : "r" (insns));
137 insns[5] = 0x83287020;
138 __asm __volatile ("flush %0 + 20" : : "r" (insns));
140 insns[4] = 0x8410a000 | (low32 & 0x3ff);
141 __asm __volatile ("flush %0 + 16" : : "r" (insns));
143 insns[3] = 0x82106000 | (high32 & 0x3ff);
144 __asm __volatile ("flush %0 + 12" : : "r" (insns));
146 insns[2] = 0x05000000 | (low32 >> 10);
147 __asm __volatile ("flush %0 + 8" : : "r" (insns));
149 insns[1] = 0x03000000 | (high32 >> 10);
150 __asm __volatile ("flush %0 + 4" : : "r" (insns));
154 /* Return the final value of a plt relocation. */
155 static inline Elf64_Addr
156 elf_machine_plt_value (struct link_map *map, const Elf64_Rela *reloc,
157 Elf64_Addr value)
159 return value + reloc->r_addend;
162 #ifdef RESOLVE
164 /* Perform the relocation specified by RELOC and SYM (which is fully resolved).
165 MAP is the object containing the reloc. */
167 static inline void
168 elf_machine_rela (struct link_map *map, const Elf64_Rela *reloc,
169 const Elf64_Sym *sym, const struct r_found_version *version,
170 Elf64_Addr *const reloc_addr)
172 #ifndef RTLD_BOOTSTRAP
173 /* This is defined in rtld.c, but nowhere in the static libc.a; make the
174 reference weak so static programs can still link. This declaration
175 cannot be done when compiling rtld.c (i.e. #ifdef RTLD_BOOTSTRAP)
176 because rtld.c contains the common defn for _dl_rtld_map, which is
177 incompatible with a weak decl in the same file. */
178 weak_extern (_dl_rtld_map);
179 #endif
181 if (ELF64_R_TYPE (reloc->r_info) == R_SPARC_RELATIVE)
183 #ifndef RTLD_BOOTSTRAP
184 if (map != &_dl_rtld_map) /* Already done in rtld itself. */
185 #endif
186 *reloc_addr = map->l_addr + reloc->r_addend;
188 else if (ELF64_R_TYPE (reloc->r_info) != R_SPARC_NONE) /* Who is Wilbur? */
190 const Elf64_Sym *const refsym = sym;
191 Elf64_Addr value;
192 if (sym->st_shndx != SHN_UNDEF &&
193 ELF64_ST_BIND (sym->st_info) == STB_LOCAL)
194 value = map->l_addr;
195 else
197 value = RESOLVE (&sym, version, ELF64_R_TYPE (reloc->r_info));
198 if (sym)
199 value += sym->st_value;
201 value += reloc->r_addend; /* Assume copy relocs have zero addend. */
203 switch (ELF64_R_TYPE (reloc->r_info))
205 case R_SPARC_COPY:
206 if (sym == NULL)
207 /* This can happen in trace mode if an object could not be
208 found. */
209 break;
210 if (sym->st_size > refsym->st_size
211 || (_dl_verbose && sym->st_size < refsym->st_size))
213 extern char **_dl_argv;
214 const char *strtab;
216 strtab = ((void *) map->l_addr
217 + map->l_info[DT_STRTAB]->d_un.d_ptr);
218 _dl_sysdep_error (_dl_argv[0] ?: "<program name unknown>",
219 ": Symbol `", strtab + refsym->st_name,
220 "' has different size in shared object, "
221 "consider re-linking\n", NULL);
223 memcpy (reloc_addr, (void *) value, MIN (sym->st_size,
224 refsym->st_size));
225 break;
227 case R_SPARC_64:
228 case R_SPARC_GLOB_DAT:
229 *reloc_addr = value;
230 break;
231 case R_SPARC_8:
232 *(char *) reloc_addr = value;
233 break;
234 case R_SPARC_16:
235 *(short *) reloc_addr = value;
236 break;
237 case R_SPARC_32:
238 *(unsigned int *) reloc_addr = value;
239 break;
240 case R_SPARC_DISP8:
241 *(char *) reloc_addr = (value - (Elf64_Addr) reloc_addr);
242 break;
243 case R_SPARC_DISP16:
244 *(short *) reloc_addr = (value - (Elf64_Addr) reloc_addr);
245 break;
246 case R_SPARC_DISP32:
247 *(unsigned int *) reloc_addr = (value - (Elf64_Addr) reloc_addr);
248 break;
249 case R_SPARC_WDISP30:
250 *(unsigned int *) reloc_addr =
251 ((*(unsigned int *)reloc_addr & 0xc0000000) |
252 ((value - (Elf64_Addr) reloc_addr) >> 2));
253 break;
255 /* MEDLOW code model relocs */
256 case R_SPARC_LO10:
257 *(unsigned int *) reloc_addr =
258 ((*(unsigned int *)reloc_addr & ~0x3ff) |
259 (value & 0x3ff));
260 break;
261 case R_SPARC_HI22:
262 *(unsigned int *) reloc_addr =
263 ((*(unsigned int *)reloc_addr & 0xffc00000) |
264 (value >> 10));
265 break;
267 /* MEDMID code model relocs */
268 case R_SPARC_H44:
269 *(unsigned int *) reloc_addr =
270 ((*(unsigned int *)reloc_addr & 0xffc00000) |
271 (value >> 22));
272 break;
273 case R_SPARC_M44:
274 *(unsigned int *) reloc_addr =
275 ((*(unsigned int *)reloc_addr & ~0x3ff) |
276 ((value >> 12) & 0x3ff));
277 break;
278 case R_SPARC_L44:
279 *(unsigned int *) reloc_addr =
280 ((*(unsigned int *)reloc_addr & ~0xfff) |
281 (value & 0xfff));
282 break;
284 /* MEDANY code model relocs */
285 case R_SPARC_HH22:
286 *(unsigned int *) reloc_addr =
287 ((*(unsigned int *)reloc_addr & 0xffc00000) |
288 (value >> 42));
289 break;
290 case R_SPARC_HM10:
291 *(unsigned int *) reloc_addr =
292 ((*(unsigned int *)reloc_addr & ~0x3ff) |
293 ((value >> 32) & 0x3ff));
294 break;
295 case R_SPARC_LM22:
296 *(unsigned int *) reloc_addr =
297 ((*(unsigned int *)reloc_addr & 0xffc00000) |
298 ((value >> 10) & 0x003fffff));
299 break;
301 case R_SPARC_JMP_SLOT:
302 elf_machine_fixup_plt(map, reloc, reloc_addr, value);
303 break;
305 default:
306 assert (! "unexpected dynamic reloc type");
307 break;
312 static inline void
313 elf_machine_lazy_rel (Elf64_Addr l_addr, const Elf64_Rela *reloc)
315 switch (ELF64_R_TYPE (reloc->r_info))
317 case R_SPARC_NONE:
318 break;
319 case R_SPARC_JMP_SLOT:
320 break;
321 default:
322 assert (! "unexpected PLT reloc type");
323 break;
327 #endif /* RESOLVE */
329 /* Nonzero iff TYPE should not be allowed to resolve to one of
330 the main executable's symbols, as for a COPY reloc. */
331 #define elf_machine_lookup_noexec_p(type) ((type) == R_SPARC_COPY)
333 /* Nonzero iff TYPE describes relocation of a PLT entry, so
334 PLT entries should not be allowed to define the value. */
335 #define elf_machine_lookup_noplt_p(type) ((type) == R_SPARC_JMP_SLOT)
337 /* A reloc type used for ld.so cmdline arg lookups to reject PLT entries. */
338 #define ELF_MACHINE_JMP_SLOT R_SPARC_JMP_SLOT
340 /* The SPARC never uses Elf64_Rel relocations. */
341 #define ELF_MACHINE_NO_REL 1
343 /* The SPARC overlaps DT_RELA and DT_PLTREL. */
344 #define ELF_MACHINE_PLTREL_OVERLAP 1
346 /* Set up the loaded object described by L so its unrelocated PLT
347 entries will jump to the on-demand fixup code in dl-runtime.c. */
349 static inline int
350 elf_machine_runtime_setup (struct link_map *l, int lazy, int profile)
352 if (l->l_info[DT_JMPREL] && lazy)
354 extern void _dl_runtime_resolve_0 (void);
355 extern void _dl_runtime_resolve_1 (void);
356 extern void _dl_runtime_profile_0 (void);
357 extern void _dl_runtime_profile_1 (void);
358 Elf64_Addr res0_addr, res1_addr;
359 unsigned int *plt = (unsigned int *)
360 (l->l_addr + l->l_info[DT_PLTGOT]->d_un.d_ptr);
362 if (! profile)
364 res0_addr = (Elf64_Addr) &_dl_runtime_resolve_0;
365 res1_addr = (Elf64_Addr) &_dl_runtime_resolve_1;
367 else
369 res0_addr = (Elf64_Addr) &_dl_runtime_profile_0;
370 res1_addr = (Elf64_Addr) &_dl_runtime_profile_1;
371 if (_dl_name_match_p (_dl_profile, l))
372 _dl_profile_map = l;
375 /* PLT0 looks like:
377 save %sp, -192, %sp
378 sethi %hh(_dl_runtime_{resolve,profile}_0), %g3
379 sethi %lm(_dl_runtime_{resolve,profile}_0), %g4
380 or %g3, %hm(_dl_runtime_{resolve,profile}_0), %g3
381 or %g4, %lo(_dl_runtime_{resolve,profile}_0), %g4
382 sllx %g3, 32, %g3
383 jmpl %g3 + %g4, %o0
386 PLT1 is similar except we jump to _dl_runtime_{resolve,profile}_1. */
388 plt[0] = 0x9de3bf40;
389 plt[1] = 0x07000000 | (res0_addr >> (64 - 22));
390 plt[2] = 0x09000000 | ((res0_addr >> 10) & 0x003fffff);
391 plt[3] = 0x8610e000 | ((res0_addr >> 32) & 0x3ff);
392 plt[4] = 0x88112000 | (res0_addr & 0x3ff);
393 plt[5] = 0x8728f020;
394 plt[6] = 0x91c0c004;
395 plt[7] = 0x01000000;
397 plt[8 + 0] = 0x9de3bf40;
398 plt[8 + 1] = 0x07000000 | (res1_addr >> (64 - 22));
399 plt[8 + 2] = 0x09000000 | ((res1_addr >> 10) & 0x003fffff);
400 plt[8 + 3] = 0x8610e000 | ((res1_addr >> 32) & 0x3ff);
401 plt[8 + 4] = 0x88112000 | (res1_addr & 0x3ff);
402 plt[8 + 5] = 0x8728f020;
403 plt[8 + 6] = 0x91c0c004;
404 plt[8 + 7] = 0x01000000;
406 /* Now put the magic cookie at the beginning of .PLT3
407 Entry .PLT4 is unused by this implementation. */
408 *((struct link_map **)(&plt[16 + 0])) = l;
411 return lazy;
414 /* This code is used in dl-runtime.c to call the `fixup' function
415 and then redirect to the address it returns. */
416 #define TRAMPOLINE_TEMPLATE(tramp_name, fixup_name) \
417 asm ("\
418 .text
419 .globl " #tramp_name "_0
420 .type " #tramp_name "_0, @function
421 .align 32
422 " #tramp_name "_0:
423 ldx [%o0 + 32 + 8], %l0
424 sethi %hi(1048576), %g2
425 sub %g1, %o0, %o0
426 xor %g2, -20, %g2
427 sethi %hi(5120), %g3
428 add %o0, %g2, %o0
429 sethi %hi(32768), %o2
430 udivx %o0, %g3, %g3
431 sllx %g3, 2, %g1
432 add %g1, %g3, %g1
433 sllx %g1, 10, %g2
434 sllx %g1, 5, %g1
435 sub %o0, %g2, %o0
436 udivx %o0, 24, %o0
437 add %o0, %o2, %o0
438 add %g1, %o0, %g1
439 sllx %g1, 1, %o1
440 mov %l0, %o0
441 add %o1, %g1, %o1
442 mov %i7, %o2
443 call " #fixup_name "
444 sllx %o1, 3, %o1
445 jmp %o0
446 restore
447 .size " #tramp_name "_0, . - " #tramp_name "_0
449 .globl " #tramp_name "_1
450 .type " #tramp_name "_1, @function
451 .align 32
452 " #tramp_name "_1:
453 srlx %g1, 15, %o1
454 ldx [%o0 + 8], %o0
455 sllx %o1, 1, %o3
456 add %o1, %o3, %o1
457 mov %i7, %o2
458 call " #fixup_name "
459 sllx %o1, 3, %o1
460 jmp %o0
461 restore
462 .size " #tramp_name "_1, . - " #tramp_name "_1
463 .previous");
465 #ifndef PROF
466 #define ELF_MACHINE_RUNTIME_TRAMPOLINE \
467 TRAMPOLINE_TEMPLATE (_dl_runtime_resolve, fixup); \
468 TRAMPOLINE_TEMPLATE (_dl_runtime_profile, profile_fixup);
469 #else
470 #define ELF_MACHINE_RUNTIME_TRAMPOLINE \
471 TRAMPOLINE_TEMPLATE (_dl_runtime_resolve, fixup); \
472 TRAMPOLINE_TEMPLATE (_dl_runtime_profile, fixup);
473 #endif
475 /* The PLT uses Elf64_Rela relocs. */
476 #define elf_machine_relplt elf_machine_rela
478 /* Initial entry point code for the dynamic linker.
479 The C function `_dl_start' is the real entry point;
480 its return value is the user program's entry point. */
482 #define __S1(x) #x
483 #define __S(x) __S1(x)
485 #define RTLD_START __asm__ ( "\
486 .text
487 .global _start
488 .type _start, @function
489 .align 32
490 _start:
491 /* Make room for functions to drop their arguments on the stack. */
492 sub %sp, 6*8, %sp
493 /* Pass pointer to argument block to _dl_start. */
494 call _dl_start
495 add %sp," __S(STACK_BIAS) "+22*8,%o0
496 /* FALLTHRU */
497 .size _start, .-_start
499 .global _dl_start_user
500 .type _dl_start_user, @function
501 _dl_start_user:
502 /* Load the GOT register. */
503 1: call 11f
504 sethi %hi(_GLOBAL_OFFSET_TABLE_-(1b-.)),%l7
505 11: or %l7,%lo(_GLOBAL_OFFSET_TABLE_-(1b-.)),%l7
506 add %l7,%o7,%l7
507 /* Save the user entry point address in %l0. */
508 mov %o0,%l0
509 /* Store the highest stack address. */
510 sethi %hi(__libc_stack_end), %g2
511 or %g2, %lo(__libc_stack_end), %g2
512 ldx [%l7 + %g2], %l1
513 add %sp, 6*8, %l2
514 stx %l2, [%l1]
515 /* See if we were run as a command with the executable file name as an
516 extra leading argument. If so, we must shift things around since we
517 must keep the stack doubleword aligned. */
518 sethi %hi(_dl_skip_args), %g2
519 or %g2, %lo(_dl_skip_args), %g2
520 ldx [%l7+%g2], %i0
521 ld [%i0], %i0
522 brz,pt %i0, 2f
524 /* Find out how far to shift. */
525 ldx [%sp+" __S(STACK_BIAS) "+22*8], %i1
526 sub %i1, %i0, %i1
527 sllx %i0, 3, %i2
528 stx %i1, [%sp+" __S(STACK_BIAS) "+22*8]
529 add %sp, " __S(STACK_BIAS) "+23*8, %i1
530 add %i1, %i2, %i2
531 /* Copy down argv. */
532 12: ldx [%i2], %i3
533 add %i2, 8, %i2
534 stx %i3, [%i1]
535 brnz,pt %i3, 12b
536 add %i1, 8, %i1
537 /* Copy down envp. */
538 13: ldx [%i2], %i3
539 add %i2, 8, %i2
540 stx %i3, [%i1]
541 brnz,pt %i3, 13b
542 add %i1, 8, %i1
543 /* Copy down auxiliary table. */
544 14: ldx [%i2], %i3
545 ldx [%i2+8], %i4
546 add %i2, 16, %i2
547 stx %i3, [%i1]
548 stx %i4, [%i1+8]
549 brnz,pt %i3, 13b
550 add %i1, 16, %i1
551 /* Load searchlist of the main object to pass to _dl_init_next. */
552 2: sethi %hi(_dl_main_searchlist), %g2
553 or %g2, %lo(_dl_main_searchlist), %g2
554 ldx [%l7+%g2], %g2
555 ldx [%g2], %l1
556 /* Call _dl_init_next to return the address of an initializer to run. */
557 3: call _dl_init_next
558 mov %l1, %o0
559 brz,pn %o0, 4f
561 jmpl %o0, %o7
562 sub %o7, 24, %o7
563 /* Clear the startup flag. */
564 4: sethi %hi(_dl_starting_up), %g2
565 or %g2, %lo(_dl_starting_up), %g2
566 ldx [%l7+%g2], %g2
567 st %g0, [%g2]
568 /* Pass our finalizer function to the user in %g1. */
569 sethi %hi(_dl_fini), %g1
570 or %g1, %lo(_dl_fini), %g1
571 ldx [%l7+%g1], %g1
572 /* Jump to the user's entry point and deallocate the extra stack we got. */
573 jmp %l0
574 add %sp, 6*8, %sp
575 .size _dl_start_user, . - _dl_start_user
576 .previous");