powerpc: Update ulps
[glibc.git] / sysdeps / sparc / sparc64 / dl-machine.h
blob7f55e7d600e424a0cb17234b207f1dbc1580aeb2
1 /* Machine-dependent ELF dynamic relocation inline functions. Sparc64 version.
2 Copyright (C) 1997-2024 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, see
17 <https://www.gnu.org/licenses/>. */
19 #ifndef dl_machine_h
20 #define dl_machine_h
22 #define ELF_MACHINE_NAME "sparc64"
24 #include <string.h>
25 #include <sys/param.h>
26 #include <ldsodefs.h>
27 #include <sysdep.h>
28 #include <dl-plt.h>
29 #include <dl-static-tls.h>
30 #include <dl-machine-rel.h>
32 #define ELF64_R_TYPE_ID(info) ((info) & 0xff)
33 #define ELF64_R_TYPE_DATA(info) ((info) >> 8)
35 /* Return nonzero iff ELF header is compatible with the running host. */
36 static inline int
37 elf_machine_matches_host (const Elf64_Ehdr *ehdr)
39 return ehdr->e_machine == EM_SPARCV9;
42 /* We have to do this because elf_machine_{dynamic,load_address} can be
43 invoked from functions that have no GOT references, and thus the compiler
44 has no obligation to load the PIC register. */
45 #define LOAD_PIC_REG(PIC_REG) \
46 do { Elf64_Addr tmp; \
47 __asm("sethi %%hi(_GLOBAL_OFFSET_TABLE_-4), %1\n\t" \
48 "rd %%pc, %0\n\t" \
49 "add %1, %%lo(_GLOBAL_OFFSET_TABLE_+4), %1\n\t" \
50 "add %0, %1, %0" \
51 : "=r" (PIC_REG), "=r" (tmp)); \
52 } while (0)
54 /* Return the link-time address of _DYNAMIC. Conveniently, this is the
55 first element of the GOT. This must be inlined in a function which
56 uses global data. */
57 static inline Elf64_Addr
58 elf_machine_dynamic (void)
60 register Elf64_Addr *elf_pic_register __asm__("%l7");
62 LOAD_PIC_REG (elf_pic_register);
64 return *elf_pic_register;
67 /* Return the run-time load address of the shared object. */
68 static inline Elf64_Addr
69 elf_machine_load_address (void)
71 register Elf32_Addr *pc __asm ("%o7");
72 register Elf64_Addr *got __asm ("%l7");
74 __asm ("sethi %%hi(_GLOBAL_OFFSET_TABLE_-4), %1\n\t"
75 "call 1f\n\t"
76 " add %1, %%lo(_GLOBAL_OFFSET_TABLE_+4), %1\n\t"
77 "call _DYNAMIC\n\t"
78 "call _GLOBAL_OFFSET_TABLE_\n"
79 "1:\tadd %1, %0, %1\n\t" : "=r" (pc), "=r" (got));
81 /* got is now l_addr + _GLOBAL_OFFSET_TABLE_
82 *got is _DYNAMIC
83 pc[2]*4 is l_addr + _DYNAMIC - (long)pc - 8
84 pc[3]*4 is l_addr + _GLOBAL_OFFSET_TABLE_ - (long)pc - 12 */
85 return (Elf64_Addr) got - *got + (Elf32_Sword) ((pc[2] - pc[3]) * 4) - 4;
88 static inline Elf64_Addr __attribute__ ((always_inline))
89 elf_machine_fixup_plt (struct link_map *map, lookup_t t,
90 const ElfW(Sym) *refsym, const ElfW(Sym) *sym,
91 const Elf64_Rela *reloc,
92 Elf64_Addr *reloc_addr, Elf64_Addr value)
94 sparc64_fixup_plt (map, reloc, reloc_addr, value + reloc->r_addend,
95 reloc->r_addend, 1);
96 return value;
99 /* Return the final value of a plt relocation. */
100 static inline Elf64_Addr
101 elf_machine_plt_value (struct link_map *map, const Elf64_Rela *reloc,
102 Elf64_Addr value)
104 /* Don't add addend here, but in elf_machine_fixup_plt instead.
105 value + reloc->r_addend is the value which should actually be
106 stored into .plt data slot. */
107 return value;
110 /* ELF_RTYPE_CLASS_PLT iff TYPE describes relocation of a PLT entry, so
111 PLT entries should not be allowed to define the value.
112 ELF_RTYPE_CLASS_COPY iff TYPE should not be allowed to resolve to one
113 of the main executable's symbols, as for a COPY reloc. */
114 #define elf_machine_type_class(type) \
115 ((((type) == R_SPARC_JMP_SLOT \
116 || ((type) >= R_SPARC_TLS_GD_HI22 && (type) <= R_SPARC_TLS_TPOFF64)) \
117 * ELF_RTYPE_CLASS_PLT) \
118 | (((type) == R_SPARC_COPY) * ELF_RTYPE_CLASS_COPY))
120 /* A reloc type used for ld.so cmdline arg lookups to reject PLT entries. */
121 #define ELF_MACHINE_JMP_SLOT R_SPARC_JMP_SLOT
123 /* Set up the loaded object described by L so its unrelocated PLT
124 entries will jump to the on-demand fixup code in dl-runtime.c. */
126 static inline int
127 elf_machine_runtime_setup (struct link_map *l, struct r_scope_elem *scope[],
128 int lazy, int profile)
130 if (l->l_info[DT_JMPREL] && lazy)
132 extern void _dl_runtime_resolve_0 (void);
133 extern void _dl_runtime_resolve_1 (void);
134 extern void _dl_runtime_profile_0 (void);
135 extern void _dl_runtime_profile_1 (void);
136 Elf64_Addr res0_addr, res1_addr;
137 unsigned int *plt = (void *) D_PTR (l, l_info[DT_PLTGOT]);
139 #ifdef SHARED
140 if (__glibc_unlikely (profile))
142 res0_addr = (Elf64_Addr) &_dl_runtime_profile_0;
143 res1_addr = (Elf64_Addr) &_dl_runtime_profile_1;
145 if (GLRO(dl_profile) != NULL
146 && _dl_name_match_p (GLRO(dl_profile), l))
147 GL(dl_profile_map) = l;
149 else
150 #endif
152 res0_addr = (Elf64_Addr) &_dl_runtime_resolve_0;
153 res1_addr = (Elf64_Addr) &_dl_runtime_resolve_1;
156 /* PLT0 looks like:
158 sethi %uhi(_dl_runtime_{resolve,profile}_0), %g4
159 sethi %hi(_dl_runtime_{resolve,profile}_0), %g5
160 or %g4, %ulo(_dl_runtime_{resolve,profile}_0), %g4
161 or %g5, %lo(_dl_runtime_{resolve,profile}_0), %g5
162 sllx %g4, 32, %g4
163 add %g4, %g5, %g5
164 jmpl %g5, %g4
168 plt[0] = 0x09000000 | (res0_addr >> (64 - 22));
169 plt[1] = 0x0b000000 | ((res0_addr >> 10) & 0x003fffff);
170 plt[2] = 0x88112000 | ((res0_addr >> 32) & 0x3ff);
171 plt[3] = 0x8a116000 | (res0_addr & 0x3ff);
172 plt[4] = 0x89293020;
173 plt[5] = 0x8a010005;
174 plt[6] = 0x89c14000;
175 plt[7] = 0x01000000;
177 /* PLT1 looks like:
179 sethi %uhi(_dl_runtime_{resolve,profile}_1), %g4
180 sethi %hi(_dl_runtime_{resolve,profile}_1), %g5
181 or %g4, %ulo(_dl_runtime_{resolve,profile}_1), %g4
182 or %g5, %lo(_dl_runtime_{resolve,profile}_1), %g5
183 sllx %g4, 32, %g4
184 add %g4, %g5, %g5
185 jmpl %g5, %g4
189 plt[8] = 0x09000000 | (res1_addr >> (64 - 22));
190 plt[9] = 0x0b000000 | ((res1_addr >> 10) & 0x003fffff);
191 plt[10] = 0x88112000 | ((res1_addr >> 32) & 0x3ff);
192 plt[11] = 0x8a116000 | (res1_addr & 0x3ff);
193 plt[12] = 0x89293020;
194 plt[13] = 0x8a010005;
195 plt[14] = 0x89c14000;
196 plt[15] = 0x01000000;
198 /* Now put the magic cookie at the beginning of .PLT2
199 Entry .PLT3 is unused by this implementation. */
200 *((struct link_map **)(&plt[16])) = l;
203 return lazy;
206 /* The PLT uses Elf64_Rela relocs. */
207 #define elf_machine_relplt elf_machine_rela
209 /* Undo the sub %sp, 6*8, %sp; add %sp, STACK_BIAS + 22*8, %o0 below
210 (but w/o STACK_BIAS) to get at the value we want in __libc_stack_end. */
211 #define DL_STACK_END(cookie) \
212 ((void *) (((long) (cookie)) - (22 - 6) * 8))
214 /* Initial entry point code for the dynamic linker.
215 The C function `_dl_start' is the real entry point;
216 its return value is the user program's entry point. */
218 #define RTLD_GOT_ADDRESS(pic_reg, reg, symbol) \
219 "sethi %gdop_hix22(" #symbol "), " #reg "\n\t" \
220 "xor " #reg ", %gdop_lox10(" #symbol "), " #reg "\n\t" \
221 "ldx [" #pic_reg " + " #reg "], " #reg ", %gdop(" #symbol ")\n"
223 #define __S1(x) #x
224 #define __S(x) __S1(x)
226 #define RTLD_START __asm__ ( "\n" \
227 " .text\n" \
228 " .global _start\n" \
229 " .type _start, @function\n" \
230 " .align 32\n" \
231 "_start:\n" \
232 " /* Make room for functions to drop their arguments on the stack. */\n" \
233 " sub %sp, 6*8, %sp\n" \
234 " /* Pass pointer to argument block to _dl_start. */\n" \
235 " call _dl_start\n" \
236 " add %sp," __S(STACK_BIAS) "+22*8,%o0\n" \
237 " /* FALLTHRU */\n" \
238 " .size _start, .-_start\n" \
239 "\n" \
240 " .global _dl_start_user\n" \
241 " .type _dl_start_user, @function\n" \
242 "_dl_start_user:\n" \
243 " /* Load the GOT register. */\n" \
244 "1: call 11f\n" \
245 " sethi %hi(_GLOBAL_OFFSET_TABLE_-(1b-.)), %l7\n" \
246 "11: or %l7, %lo(_GLOBAL_OFFSET_TABLE_-(1b-.)), %l7\n" \
247 " add %l7, %o7, %l7\n" \
248 " /* Save the user entry point address in %l0. */\n" \
249 " mov %o0, %l0\n" \
250 " ldx [%sp + " __S(STACK_BIAS) " + 22*8], %i5\n" \
251 " /* %o0 = _dl_loaded, %o1 = argc, %o2 = argv, %o3 = envp. */\n" \
252 "" RTLD_GOT_ADDRESS(%l7, %o0, _rtld_local) \
253 " sllx %i5, 3, %o3\n" \
254 " add %sp, " __S(STACK_BIAS) " + 23*8, %o2\n" \
255 " add %o3, 8, %o3\n" \
256 " mov %i5, %o1\n" \
257 " add %o2, %o3, %o3\n" \
258 " call _dl_init\n" \
259 " ldx [%o0], %o0\n" \
260 " /* Pass our finalizer function to the user in %g1. */\n" \
261 RTLD_GOT_ADDRESS(%l7, %g1, _dl_fini) \
262 " /* Jump to the user's entry point and deallocate the extra stack we got. */\n" \
263 " jmp %l0\n" \
264 " add %sp, 6*8, %sp\n" \
265 " .size _dl_start_user, . - _dl_start_user\n" \
266 " .previous\n");
268 #endif /* dl_machine_h */
270 #define ARCH_LA_PLTENTER sparc64_gnu_pltenter
271 #define ARCH_LA_PLTEXIT sparc64_gnu_pltexit
273 #ifdef RESOLVE_MAP
275 /* Perform the relocation specified by RELOC and SYM (which is fully resolved).
276 MAP is the object containing the reloc. */
278 static inline void
279 __attribute__ ((always_inline))
280 elf_machine_rela (struct link_map *map, struct r_scope_elem *scope[],
281 const Elf64_Rela *reloc, const Elf64_Sym *sym,
282 const struct r_found_version *version,
283 void *const reloc_addr_arg, int skip_ifunc)
285 Elf64_Addr *const reloc_addr = reloc_addr_arg;
286 #if !defined RTLD_BOOTSTRAP
287 const Elf64_Sym *const refsym = sym;
288 #endif
289 Elf64_Addr value;
290 const unsigned long int r_type = ELF64_R_TYPE_ID (reloc->r_info);
291 struct link_map *sym_map = NULL;
293 if (__glibc_unlikely (r_type == R_SPARC_NONE))
294 return;
296 if (__glibc_unlikely (r_type == R_SPARC_SIZE64))
298 *reloc_addr = sym->st_size + reloc->r_addend;
299 return;
302 #if !defined RTLD_BOOTSTRAP
303 if (__glibc_unlikely (r_type == R_SPARC_RELATIVE))
305 *reloc_addr += map->l_addr + reloc->r_addend;
306 return;
308 #endif
310 if (__builtin_expect (ELF64_ST_BIND (sym->st_info) == STB_LOCAL, 0)
311 && sym->st_shndx != SHN_UNDEF)
313 sym_map = map;
314 value = map->l_addr;
316 else
318 sym_map = RESOLVE_MAP (map, scope, &sym, version, r_type);
319 value = SYMBOL_ADDRESS (sym_map, sym, true);
322 value += reloc->r_addend; /* Assume copy relocs have zero addend. */
324 if (sym != NULL
325 && __builtin_expect (ELFW(ST_TYPE) (sym->st_info) == STT_GNU_IFUNC, 0)
326 && __builtin_expect (sym->st_shndx != SHN_UNDEF, 1)
327 && __builtin_expect (!skip_ifunc, 1))
328 value = ((Elf64_Addr (*) (int)) value) (GLRO(dl_hwcap));
330 switch (r_type)
332 #if !defined RTLD_BOOTSTRAP
333 case R_SPARC_COPY:
334 if (sym == NULL)
335 /* This can happen in trace mode if an object could not be
336 found. */
337 break;
338 if (sym->st_size > refsym->st_size
339 || (GLRO(dl_verbose) && sym->st_size < refsym->st_size))
341 const char *strtab;
343 strtab = (const void *) D_PTR (map, l_info[DT_STRTAB]);
344 _dl_error_printf ("\
345 %s: Symbol `%s' has different size in shared object, consider re-linking\n",
346 RTLD_PROGNAME, strtab + refsym->st_name);
348 memcpy (reloc_addr_arg, (void *) value,
349 MIN (sym->st_size, refsym->st_size));
350 break;
351 #endif
352 case R_SPARC_64:
353 case R_SPARC_GLOB_DAT:
354 *reloc_addr = value;
355 break;
356 case R_SPARC_IRELATIVE:
357 if (__glibc_likely (!skip_ifunc))
358 value = ((Elf64_Addr (*) (int)) value) (GLRO(dl_hwcap));
359 *reloc_addr = value;
360 break;
361 case R_SPARC_JMP_IREL:
362 if (__glibc_likely (!skip_ifunc))
363 value = ((Elf64_Addr (*) (int)) value) (GLRO(dl_hwcap));
364 /* 'high' is always zero, for large PLT entries the linker
365 emits an R_SPARC_IRELATIVE. */
366 sparc64_fixup_plt (map, reloc, reloc_addr, value, 0, 0);
367 break;
368 case R_SPARC_JMP_SLOT:
369 sparc64_fixup_plt (map, reloc, reloc_addr, value, reloc->r_addend, 0);
370 break;
371 case R_SPARC_TLS_DTPMOD64:
372 /* Get the information from the link map returned by the
373 resolv function. */
374 if (sym_map != NULL)
375 *reloc_addr = sym_map->l_tls_modid;
376 break;
377 case R_SPARC_TLS_DTPOFF64:
378 /* During relocation all TLS symbols are defined and used.
379 Therefore the offset is already correct. */
380 *reloc_addr = (sym == NULL ? 0 : sym->st_value) + reloc->r_addend;
381 break;
382 case R_SPARC_TLS_TPOFF64:
383 /* The offset is negative, forward from the thread pointer. */
384 /* We know the offset of object the symbol is contained in.
385 It is a negative value which will be added to the
386 thread pointer. */
387 if (sym != NULL)
389 CHECK_STATIC_TLS (map, sym_map);
390 *reloc_addr = sym->st_value - sym_map->l_tls_offset
391 + reloc->r_addend;
393 break;
394 #ifndef RTLD_BOOTSTRAP
395 case R_SPARC_TLS_LE_HIX22:
396 case R_SPARC_TLS_LE_LOX10:
397 if (sym != NULL)
399 CHECK_STATIC_TLS (map, sym_map);
400 value = sym->st_value - sym_map->l_tls_offset
401 + reloc->r_addend;
402 if (r_type == R_SPARC_TLS_LE_HIX22)
403 *(unsigned int *)reloc_addr =
404 ((*(unsigned int *)reloc_addr & 0xffc00000)
405 | (((~value) >> 10) & 0x3fffff));
406 else
407 *(unsigned int *)reloc_addr =
408 ((*(unsigned int *)reloc_addr & 0xffffe000) | (value & 0x3ff)
409 | 0x1c00);
411 break;
412 #endif
413 #ifndef RTLD_BOOTSTRAP
414 case R_SPARC_8:
415 *(char *) reloc_addr = value;
416 break;
417 case R_SPARC_16:
418 *(short *) reloc_addr = value;
419 break;
420 case R_SPARC_32:
421 *(unsigned int *) reloc_addr = value;
422 break;
423 case R_SPARC_DISP8:
424 *(char *) reloc_addr = (value - (Elf64_Addr) reloc_addr);
425 break;
426 case R_SPARC_DISP16:
427 *(short *) reloc_addr = (value - (Elf64_Addr) reloc_addr);
428 break;
429 case R_SPARC_DISP32:
430 *(unsigned int *) reloc_addr = (value - (Elf64_Addr) reloc_addr);
431 break;
432 case R_SPARC_DISP64:
433 *reloc_addr = (value - (Elf64_Addr) reloc_addr);
434 break;
435 case R_SPARC_REGISTER:
436 *reloc_addr = value;
437 break;
438 case R_SPARC_WDISP30:
439 *(unsigned int *) reloc_addr =
440 ((*(unsigned int *)reloc_addr & 0xc0000000)
441 | (((value - (Elf64_Addr) reloc_addr) >> 2) & 0x3fffffff));
442 break;
444 /* MEDLOW code model relocs */
445 case R_SPARC_LO10:
446 *(unsigned int *) reloc_addr =
447 ((*(unsigned int *)reloc_addr & ~0x3ff)
448 | (value & 0x3ff));
449 break;
450 case R_SPARC_HI22:
451 *(unsigned int *) reloc_addr =
452 ((*(unsigned int *)reloc_addr & 0xffc00000)
453 | ((value >> 10) & 0x3fffff));
454 break;
455 case R_SPARC_OLO10:
456 *(unsigned int *) reloc_addr =
457 ((*(unsigned int *)reloc_addr & ~0x1fff)
458 | (((value & 0x3ff) + ELF64_R_TYPE_DATA (reloc->r_info)) & 0x1fff));
459 break;
461 /* ABS34 code model reloc */
462 case R_SPARC_H34:
463 *(unsigned int *) reloc_addr =
464 ((*(unsigned int *)reloc_addr & 0xffc00000)
465 | ((value >> 12) & 0x3fffff));
466 break;
468 /* MEDMID code model relocs */
469 case R_SPARC_H44:
470 *(unsigned int *) reloc_addr =
471 ((*(unsigned int *)reloc_addr & 0xffc00000)
472 | ((value >> 22) & 0x3fffff));
473 break;
474 case R_SPARC_M44:
475 *(unsigned int *) reloc_addr =
476 ((*(unsigned int *)reloc_addr & ~0x3ff)
477 | ((value >> 12) & 0x3ff));
478 break;
479 case R_SPARC_L44:
480 *(unsigned int *) reloc_addr =
481 ((*(unsigned int *)reloc_addr & ~0xfff)
482 | (value & 0xfff));
483 break;
485 /* MEDANY code model relocs */
486 case R_SPARC_HH22:
487 *(unsigned int *) reloc_addr =
488 ((*(unsigned int *)reloc_addr & 0xffc00000)
489 | (value >> 42));
490 break;
491 case R_SPARC_HM10:
492 *(unsigned int *) reloc_addr =
493 ((*(unsigned int *)reloc_addr & ~0x3ff)
494 | ((value >> 32) & 0x3ff));
495 break;
496 case R_SPARC_LM22:
497 *(unsigned int *) reloc_addr =
498 ((*(unsigned int *)reloc_addr & 0xffc00000)
499 | ((value >> 10) & 0x003fffff));
500 break;
501 case R_SPARC_UA16:
502 ((unsigned char *) reloc_addr_arg) [0] = value >> 8;
503 ((unsigned char *) reloc_addr_arg) [1] = value;
504 break;
505 case R_SPARC_UA32:
506 ((unsigned char *) reloc_addr_arg) [0] = value >> 24;
507 ((unsigned char *) reloc_addr_arg) [1] = value >> 16;
508 ((unsigned char *) reloc_addr_arg) [2] = value >> 8;
509 ((unsigned char *) reloc_addr_arg) [3] = value;
510 break;
511 case R_SPARC_UA64:
512 if (! ((long) reloc_addr_arg & 3))
514 /* Common in .eh_frame */
515 ((unsigned int *) reloc_addr_arg) [0] = value >> 32;
516 ((unsigned int *) reloc_addr_arg) [1] = value;
517 break;
519 ((unsigned char *) reloc_addr_arg) [0] = value >> 56;
520 ((unsigned char *) reloc_addr_arg) [1] = value >> 48;
521 ((unsigned char *) reloc_addr_arg) [2] = value >> 40;
522 ((unsigned char *) reloc_addr_arg) [3] = value >> 32;
523 ((unsigned char *) reloc_addr_arg) [4] = value >> 24;
524 ((unsigned char *) reloc_addr_arg) [5] = value >> 16;
525 ((unsigned char *) reloc_addr_arg) [6] = value >> 8;
526 ((unsigned char *) reloc_addr_arg) [7] = value;
527 break;
528 #endif
529 #if !defined RTLD_BOOTSTRAP || defined _NDEBUG
530 default:
531 _dl_reloc_bad_type (map, r_type, 0);
532 break;
533 #endif
537 static inline void
538 __attribute__ ((always_inline))
539 elf_machine_rela_relative (Elf64_Addr l_addr, const Elf64_Rela *reloc,
540 void *const reloc_addr_arg)
542 Elf64_Addr *const reloc_addr = reloc_addr_arg;
543 *reloc_addr = l_addr + reloc->r_addend;
546 static inline void
547 __attribute__ ((always_inline))
548 elf_machine_lazy_rel (struct link_map *map, struct r_scope_elem *scope[],
549 Elf64_Addr l_addr, const Elf64_Rela *reloc,
550 int skip_ifunc)
552 Elf64_Addr *const reloc_addr = (void *) (l_addr + reloc->r_offset);
553 const unsigned int r_type = ELF64_R_TYPE (reloc->r_info);
555 if (__glibc_likely (r_type == R_SPARC_JMP_SLOT))
557 else if (r_type == R_SPARC_JMP_IREL
558 || r_type == R_SPARC_IRELATIVE)
560 Elf64_Addr value = map->l_addr + reloc->r_addend;
561 if (__glibc_likely (!skip_ifunc))
562 value = ((Elf64_Addr (*) (int)) value) (GLRO(dl_hwcap));
563 if (r_type == R_SPARC_JMP_IREL)
565 /* 'high' is always zero, for large PLT entries the linker
566 emits an R_SPARC_IRELATIVE. */
567 sparc64_fixup_plt (map, reloc, reloc_addr, value, 0, 1);
569 else
570 *reloc_addr = value;
572 else if (r_type == R_SPARC_NONE)
574 else
575 _dl_reloc_bad_type (map, r_type, 1);
578 #endif /* RESOLVE_MAP */