LoongArch: Update ulps
[glibc.git] / sysdeps / s390 / s390-64 / dl-machine.h
blob5ed0b844e0b35ec904817e6774b257db7ef45e0d
1 /* Machine-dependent ELF dynamic relocation inline functions.
2 64 bit S/390 Version.
3 Copyright (C) 2001-2024 Free Software Foundation, Inc.
4 This file is part of the GNU C Library.
6 The GNU C Library is free software; you can redistribute it and/or
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.
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
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, see
18 <https://www.gnu.org/licenses/>. */
20 #ifndef dl_machine_h
21 #define dl_machine_h
23 #define ELF_MACHINE_NAME "s390x"
25 #include <sys/param.h>
26 #include <string.h>
27 #include <link.h>
28 #include <sysdeps/s390/dl-procinfo.h>
29 #include <dl-irel.h>
30 #include <dl-static-tls.h>
31 #include <dl-machine-rel.h>
32 #include <cpu-features.c>
34 #define ELF_MACHINE_IRELATIVE R_390_IRELATIVE
36 /* This is an older, now obsolete value. */
37 #define EM_S390_OLD 0xA390
39 /* Return nonzero iff E_MACHINE is compatible with the running host. */
40 static inline int
41 elf_machine_matches_host (const Elf64_Ehdr *ehdr)
43 return (ehdr->e_machine == EM_S390 || ehdr->e_machine == EM_S390_OLD)
44 && ehdr->e_ident[EI_CLASS] == ELFCLASS64;
47 /* Return the link-time address of _DYNAMIC. Conveniently, this is the
48 first element of the GOT. This must be inlined in a function which
49 uses global data. */
51 static inline Elf64_Addr
52 elf_machine_dynamic (void)
54 register Elf64_Addr *got;
56 __asm__ ( " larl %0,_GLOBAL_OFFSET_TABLE_\n"
57 : "=&a" (got) : : "0" );
59 return *got;
62 /* Return the run-time load address of the shared object. */
63 static inline Elf64_Addr
64 elf_machine_load_address (void)
66 Elf64_Addr addr;
68 __asm__( " larl %0,_dl_start\n"
69 " larl 1,_GLOBAL_OFFSET_TABLE_\n"
70 " lghi 2,_dl_start@GOT\n"
71 " slg %0,0(2,1)"
72 : "=&d" (addr) : : "1", "2" );
73 return addr;
76 /* Set up the loaded object described by L so its unrelocated PLT
77 entries will jump to the on-demand fixup code in dl-runtime.c. */
79 static inline int __attribute__ ((unused))
80 elf_machine_runtime_setup (struct link_map *l, struct r_scope_elem *scope[],
81 int lazy, int profile)
83 extern void _dl_runtime_resolve (Elf64_Word);
84 extern void _dl_runtime_profile (Elf64_Word);
85 #if defined HAVE_S390_VX_ASM_SUPPORT
86 extern void _dl_runtime_resolve_vx (Elf64_Word);
87 extern void _dl_runtime_profile_vx (Elf64_Word);
88 #endif
90 if (l->l_info[DT_JMPREL] && lazy)
92 /* The GOT entries for functions in the PLT have not yet been filled
93 in. Their initial contents will arrange when called to push an
94 offset into the .rela.plt section, push _GLOBAL_OFFSET_TABLE_[1],
95 and then jump to _GLOBAL_OFFSET_TABLE[2]. */
96 Elf64_Addr *got;
97 got = (Elf64_Addr *) D_PTR (l, l_info[DT_PLTGOT]);
98 /* If a library is prelinked but we have to relocate anyway,
99 we have to be able to undo the prelinking of .got.plt.
100 The prelinker saved us here address of .plt + 0x2e. */
101 if (got[1])
103 l->l_mach.plt = got[1] + l->l_addr;
104 l->l_mach.jmprel = (const Elf64_Rela *) D_PTR (l, l_info[DT_JMPREL]);
106 got[1] = (Elf64_Addr) l; /* Identify this shared object. */
108 /* The got[2] entry contains the address of a function which gets
109 called to get the address of a so far unresolved function and
110 jump to it. The profiling extension of the dynamic linker allows
111 to intercept the calls to collect information. In this case we
112 don't store the address in the GOT so that all future calls also
113 end in this function. */
114 #ifdef SHARED
115 if (__glibc_unlikely (profile))
117 # if defined HAVE_S390_VX_ASM_SUPPORT
118 if (GLRO(dl_hwcap) & HWCAP_S390_VX)
119 got[2] = (Elf64_Addr) &_dl_runtime_profile_vx;
120 else
121 got[2] = (Elf64_Addr) &_dl_runtime_profile;
122 # else
123 got[2] = (Elf64_Addr) &_dl_runtime_profile;
124 # endif
126 if (GLRO(dl_profile) != NULL
127 && _dl_name_match_p (GLRO(dl_profile), l))
128 /* This is the object we are looking for. Say that we really
129 want profiling and the timers are started. */
130 GL(dl_profile_map) = l;
132 else
133 #endif
135 /* This function will get called to fix up the GOT entry indicated by
136 the offset on the stack, and then jump to the resolved address. */
137 #if defined HAVE_S390_VX_ASM_SUPPORT
138 if (GLRO(dl_hwcap) & HWCAP_S390_VX)
139 got[2] = (Elf64_Addr) &_dl_runtime_resolve_vx;
140 else
141 got[2] = (Elf64_Addr) &_dl_runtime_resolve;
142 #else
143 got[2] = (Elf64_Addr) &_dl_runtime_resolve;
144 #endif
148 return lazy;
151 /* Initial entry point code for the dynamic linker.
152 The C function `_dl_start' is the real entry point;
153 its return value is the user program's entry point. */
155 #define RTLD_START __asm__ ("\n\
156 .text\n\
157 .align 4\n\
158 .globl _start\n\
159 .globl _dl_start_user\n\
160 _start:\n\
161 lgr %r2,%r15\n\
162 # Alloc stack frame\n\
163 aghi %r15,-160\n\
164 # Set the back chain to zero\n\
165 xc 0(8,%r15),0(%r15)\n\
166 # Call _dl_start with %r2 pointing to arg on stack\n\
167 brasl %r14,_dl_start # call _dl_start\n\
168 _dl_start_user:\n\
169 # Save the user entry point address in %r8.\n\
170 lgr %r8,%r2\n\
171 # Point %r12 at the GOT.\n\
172 larl %r12,_GLOBAL_OFFSET_TABLE_\n\
173 # The special initializer gets called with the stack just\n\
174 # as the application's entry point will see it; it can\n\
175 # switch stacks if it moves these contents over.\n\
176 " RTLD_START_SPECIAL_INIT "\n\
177 # Call the function to run the initializers.\n\
178 # Load the parameters:\n\
179 # (%r2, %r3, %r4, %r5) = (_dl_loaded, argc, argv, envp)\n\
180 lghi %r2,_rtld_local@GOT\n\
181 lg %r2,0(%r2,%r12)\n\
182 lg %r2,0(%r2)\n\
183 lg %r3,160(%r15)\n\
184 la %r4,168(%r15)\n\
185 lgr %r5,%r3\n\
186 sllg %r5,%r5,3\n\
187 la %r5,176(%r5,%r15)\n\
188 brasl %r14,_dl_init@PLT\n\
189 # Pass our finalizer function to the user in %r14, as per ELF ABI.\n\
190 lghi %r14,_dl_fini@GOT\n\
191 lg %r14,0(%r14,%r12)\n\
192 # Free stack frame\n\
193 aghi %r15,160\n\
194 # Jump to the user's entry point (saved in %r8).\n\
195 br %r8\n\
198 #ifndef RTLD_START_SPECIAL_INIT
199 #define RTLD_START_SPECIAL_INIT /* nothing */
200 #endif
202 /* ELF_RTYPE_CLASS_PLT iff TYPE describes relocation of a PLT entry or
203 TLS variable, so undefined references should not be allowed to
204 define the value.
205 ELF_RTYPE_CLASS_COPY iff TYPE should not be allowed to resolve to one
206 of the main executable's symbols, as for a COPY reloc. */
207 #define elf_machine_type_class(type) \
208 ((((type) == R_390_JMP_SLOT || (type) == R_390_TLS_DTPMOD \
209 || (type) == R_390_TLS_DTPOFF || (type) == R_390_TLS_TPOFF) \
210 * ELF_RTYPE_CLASS_PLT) \
211 | (((type) == R_390_COPY) * ELF_RTYPE_CLASS_COPY))
213 /* A reloc type used for ld.so cmdline arg lookups to reject PLT entries. */
214 #define ELF_MACHINE_JMP_SLOT R_390_JMP_SLOT
216 /* We define an initialization functions. This is called very early in
217 _dl_sysdep_start. */
218 #define DL_PLATFORM_INIT dl_platform_init ()
220 static inline void __attribute__ ((unused))
221 dl_platform_init (void)
223 if (GLRO(dl_platform) != NULL && *GLRO(dl_platform) == '\0')
224 /* Avoid an empty string which would disturb us. */
225 GLRO(dl_platform) = NULL;
227 #ifdef SHARED
228 /* init_cpu_features has been called early from __libc_start_main in
229 static executable. */
230 init_cpu_features (&GLRO(dl_s390_cpu_features));
231 #endif
235 static inline Elf64_Addr
236 elf_machine_fixup_plt (struct link_map *map, lookup_t t,
237 const ElfW(Sym) *refsym, const ElfW(Sym) *sym,
238 const Elf64_Rela *reloc,
239 Elf64_Addr *reloc_addr, Elf64_Addr value)
241 return *reloc_addr = value;
244 /* Return the final value of a plt relocation. */
245 static inline Elf64_Addr
246 elf_machine_plt_value (struct link_map *map, const Elf64_Rela *reloc,
247 Elf64_Addr value)
249 return value;
252 /* Names of the architecture-specific auditing callback functions. */
253 #define ARCH_LA_PLTENTER s390_64_gnu_pltenter
254 #define ARCH_LA_PLTEXIT s390_64_gnu_pltexit
256 #endif /* !dl_machine_h */
258 #ifdef RESOLVE_MAP
260 /* Perform the relocation specified by RELOC and SYM (which is fully resolved).
261 MAP is the object containing the reloc. */
263 static inline void
264 __attribute__ ((always_inline))
265 elf_machine_rela (struct link_map *map, struct r_scope_elem *scope[],
266 const Elf64_Rela *reloc, const Elf64_Sym *sym,
267 const struct r_found_version *version,
268 void *const reloc_addr_arg, int skip_ifunc)
270 Elf64_Addr *const reloc_addr = reloc_addr_arg;
271 const unsigned int r_type = ELF64_R_TYPE (reloc->r_info);
273 #if !defined RTLD_BOOTSTRAP
274 if (__glibc_unlikely (r_type == R_390_RELATIVE))
275 *reloc_addr = map->l_addr + reloc->r_addend;
276 else
277 #endif
278 if (__glibc_unlikely (r_type == R_390_NONE))
279 return;
280 else
282 #if !defined RTLD_BOOTSTRAP
283 /* Only needed for R_390_COPY below. */
284 const Elf64_Sym *const refsym = sym;
285 #endif
286 struct link_map *sym_map = RESOLVE_MAP (map, scope, &sym, version,
287 r_type);
288 Elf64_Addr value = SYMBOL_ADDRESS (sym_map, sym, true);
290 if (sym != NULL
291 && __builtin_expect (ELFW(ST_TYPE) (sym->st_info) == STT_GNU_IFUNC,
293 && __builtin_expect (sym->st_shndx != SHN_UNDEF, 1)
294 && __builtin_expect (!skip_ifunc, 1))
295 value = elf_ifunc_invoke (value);
297 switch (r_type)
299 case R_390_IRELATIVE:
300 value = map->l_addr + reloc->r_addend;
301 if (__glibc_likely (!skip_ifunc))
302 value = elf_ifunc_invoke (value);
303 *reloc_addr = value;
304 break;
305 case R_390_GLOB_DAT:
306 case R_390_JMP_SLOT:
307 *reloc_addr = value + reloc->r_addend;
308 break;
310 case R_390_TLS_DTPMOD:
311 #ifdef RTLD_BOOTSTRAP
312 /* During startup the dynamic linker is always the module
313 with index 1.
314 XXX If this relocation is necessary move before RESOLVE
315 call. */
316 *reloc_addr = 1;
317 #else
318 /* Get the information from the link map returned by the
319 resolv function. */
320 if (sym_map != NULL)
321 *reloc_addr = sym_map->l_tls_modid;
322 #endif
323 break;
324 case R_390_TLS_DTPOFF:
325 #ifndef RTLD_BOOTSTRAP
326 /* During relocation all TLS symbols are defined and used.
327 Therefore the offset is already correct. */
328 if (sym != NULL)
329 *reloc_addr = sym->st_value + reloc->r_addend;
330 #endif
331 break;
332 case R_390_TLS_TPOFF:
333 /* The offset is negative, forward from the thread pointer. */
334 #ifdef RTLD_BOOTSTRAP
335 *reloc_addr = sym->st_value + reloc->r_addend - map->l_tls_offset;
336 #else
337 /* We know the offset of the object the symbol is contained in.
338 It is a negative value which will be added to the
339 thread pointer. */
340 if (sym != NULL)
342 CHECK_STATIC_TLS (map, sym_map);
343 *reloc_addr = (sym->st_value + reloc->r_addend
344 - sym_map->l_tls_offset);
346 #endif
347 break;
349 #ifndef RTLD_BOOTSTRAP
350 /* Not needed for dl-conflict.c. */
351 case R_390_COPY:
352 if (sym == NULL)
353 /* This can happen in trace mode if an object could not be
354 found. */
355 break;
356 if (__builtin_expect (sym->st_size > refsym->st_size, 0)
357 || (__builtin_expect (sym->st_size < refsym->st_size, 0)
358 && __builtin_expect (GLRO(dl_verbose), 0)))
360 const char *strtab;
362 strtab = (const char *) D_PTR (map,l_info[DT_STRTAB]);
363 _dl_error_printf ("\
364 %s: Symbol `%s' has different size in shared object, consider re-linking\n",
365 RTLD_PROGNAME, strtab + refsym->st_name);
367 memcpy (reloc_addr_arg, (void *) value,
368 MIN (sym->st_size, refsym->st_size));
369 break;
370 case R_390_64:
371 *reloc_addr = value + reloc->r_addend;
372 break;
373 case R_390_32:
374 *(unsigned int *) reloc_addr = value + reloc->r_addend;
375 break;
376 case R_390_16:
377 *(unsigned short *) reloc_addr = value + reloc->r_addend;
378 break;
379 case R_390_8:
380 *(char *) reloc_addr = value + reloc->r_addend;
381 break;
382 case R_390_PC64:
383 *reloc_addr = value +reloc->r_addend - (Elf64_Addr) reloc_addr;
384 break;
385 case R_390_PC32DBL:
386 *(unsigned int *) reloc_addr = (unsigned int)
387 ((int) (value + reloc->r_addend - (Elf64_Addr) reloc_addr) >> 1);
388 break;
389 case R_390_PC32:
390 *(unsigned int *) reloc_addr =
391 value + reloc->r_addend - (Elf64_Addr) reloc_addr;
392 break;
393 case R_390_PC16DBL:
394 *(unsigned short *) reloc_addr = (unsigned short)
395 ((short) (value + reloc->r_addend - (Elf64_Addr) reloc_addr) >> 1);
396 break;
397 case R_390_PC16:
398 *(unsigned short *) reloc_addr =
399 value + reloc->r_addend - (Elf64_Addr) reloc_addr;
400 break;
401 case R_390_NONE:
402 break;
403 #endif
404 #if !defined(RTLD_BOOTSTRAP) || defined(_NDEBUG)
405 default:
406 /* We add these checks in the version to relocate ld.so only
407 if we are still debugging. */
408 _dl_reloc_bad_type (map, r_type, 0);
409 break;
410 #endif
415 static inline void
416 __attribute__ ((always_inline))
417 elf_machine_rela_relative (Elf64_Addr l_addr, const Elf64_Rela *reloc,
418 void *const reloc_addr_arg)
420 Elf64_Addr *const reloc_addr = reloc_addr_arg;
421 *reloc_addr = l_addr + reloc->r_addend;
424 static inline void
425 __attribute__ ((always_inline))
426 elf_machine_lazy_rel (struct link_map *map, struct r_scope_elem *scope[],
427 Elf64_Addr l_addr, const Elf64_Rela *reloc,
428 int skip_ifunc)
430 Elf64_Addr *const reloc_addr = (void *) (l_addr + reloc->r_offset);
431 const unsigned int r_type = ELF64_R_TYPE (reloc->r_info);
432 /* Check for unexpected PLT reloc type. */
433 if (__glibc_likely (r_type == R_390_JMP_SLOT))
435 if (__builtin_expect (map->l_mach.plt, 0) == 0)
436 *reloc_addr += l_addr;
437 else
438 *reloc_addr = map->l_mach.plt + (reloc - map->l_mach.jmprel) * 32;
440 else if (__glibc_likely (r_type == R_390_IRELATIVE))
442 Elf64_Addr value = map->l_addr + reloc->r_addend;
443 if (__glibc_likely (!skip_ifunc))
444 value = elf_ifunc_invoke (value);
445 *reloc_addr = value;
447 else
448 _dl_reloc_bad_type (map, r_type, 1);
451 #endif /* RESOLVE_MAP */