S390: Influence hwcaps/stfle via GLIBC_TUNABLES.
[glibc.git] / sysdeps / s390 / s390-64 / dl-machine.h
blob9fabb097500739631234e9cfd0c50a9224fbdc4a
1 /* Machine-dependent ELF dynamic relocation inline functions.
2 64 bit S/390 Version.
3 Copyright (C) 2001-2023 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 if (__glibc_unlikely (profile))
116 #if defined HAVE_S390_VX_ASM_SUPPORT
117 if (GLRO(dl_hwcap) & HWCAP_S390_VX)
118 got[2] = (Elf64_Addr) &_dl_runtime_profile_vx;
119 else
120 got[2] = (Elf64_Addr) &_dl_runtime_profile;
121 #else
122 got[2] = (Elf64_Addr) &_dl_runtime_profile;
123 #endif
125 if (GLRO(dl_profile) != NULL
126 && _dl_name_match_p (GLRO(dl_profile), l))
127 /* This is the object we are looking for. Say that we really
128 want profiling and the timers are started. */
129 GL(dl_profile_map) = l;
131 else
133 /* This function will get called to fix up the GOT entry indicated by
134 the offset on the stack, and then jump to the resolved address. */
135 #if defined HAVE_S390_VX_ASM_SUPPORT
136 if (GLRO(dl_hwcap) & HWCAP_S390_VX)
137 got[2] = (Elf64_Addr) &_dl_runtime_resolve_vx;
138 else
139 got[2] = (Elf64_Addr) &_dl_runtime_resolve;
140 #else
141 got[2] = (Elf64_Addr) &_dl_runtime_resolve;
142 #endif
146 return lazy;
149 /* Initial entry point code for the dynamic linker.
150 The C function `_dl_start' is the real entry point;
151 its return value is the user program's entry point. */
153 #define RTLD_START __asm__ ("\n\
154 .text\n\
155 .align 4\n\
156 .globl _start\n\
157 .globl _dl_start_user\n\
158 _start:\n\
159 lgr %r2,%r15\n\
160 # Alloc stack frame\n\
161 aghi %r15,-160\n\
162 # Set the back chain to zero\n\
163 xc 0(8,%r15),0(%r15)\n\
164 # Call _dl_start with %r2 pointing to arg on stack\n\
165 brasl %r14,_dl_start # call _dl_start\n\
166 _dl_start_user:\n\
167 # Save the user entry point address in %r8.\n\
168 lgr %r8,%r2\n\
169 # Point %r12 at the GOT.\n\
170 larl %r12,_GLOBAL_OFFSET_TABLE_\n\
171 # The special initializer gets called with the stack just\n\
172 # as the application's entry point will see it; it can\n\
173 # switch stacks if it moves these contents over.\n\
174 " RTLD_START_SPECIAL_INIT "\n\
175 # Call the function to run the initializers.\n\
176 # Load the parameters:\n\
177 # (%r2, %r3, %r4, %r5) = (_dl_loaded, argc, argv, envp)\n\
178 lghi %r2,_rtld_local@GOT\n\
179 lg %r2,0(%r2,%r12)\n\
180 lg %r2,0(%r2)\n\
181 lg %r3,160(%r15)\n\
182 la %r4,168(%r15)\n\
183 lgr %r5,%r3\n\
184 sllg %r5,%r5,3\n\
185 la %r5,176(%r5,%r15)\n\
186 brasl %r14,_dl_init@PLT\n\
187 # Pass our finalizer function to the user in %r14, as per ELF ABI.\n\
188 lghi %r14,_dl_fini@GOT\n\
189 lg %r14,0(%r14,%r12)\n\
190 # Free stack frame\n\
191 aghi %r15,160\n\
192 # Jump to the user's entry point (saved in %r8).\n\
193 br %r8\n\
196 #ifndef RTLD_START_SPECIAL_INIT
197 #define RTLD_START_SPECIAL_INIT /* nothing */
198 #endif
200 /* ELF_RTYPE_CLASS_PLT iff TYPE describes relocation of a PLT entry or
201 TLS variable, so undefined references should not be allowed to
202 define the value.
203 ELF_RTYPE_CLASS_COPY iff TYPE should not be allowed to resolve to one
204 of the main executable's symbols, as for a COPY reloc. */
205 #define elf_machine_type_class(type) \
206 ((((type) == R_390_JMP_SLOT || (type) == R_390_TLS_DTPMOD \
207 || (type) == R_390_TLS_DTPOFF || (type) == R_390_TLS_TPOFF) \
208 * ELF_RTYPE_CLASS_PLT) \
209 | (((type) == R_390_COPY) * ELF_RTYPE_CLASS_COPY))
211 /* A reloc type used for ld.so cmdline arg lookups to reject PLT entries. */
212 #define ELF_MACHINE_JMP_SLOT R_390_JMP_SLOT
214 /* We define an initialization functions. This is called very early in
215 _dl_sysdep_start. */
216 #define DL_PLATFORM_INIT dl_platform_init ()
218 static inline void __attribute__ ((unused))
219 dl_platform_init (void)
221 if (GLRO(dl_platform) != NULL && *GLRO(dl_platform) == '\0')
222 /* Avoid an empty string which would disturb us. */
223 GLRO(dl_platform) = NULL;
225 #ifdef SHARED
226 /* init_cpu_features has been called early from __libc_start_main in
227 static executable. */
228 init_cpu_features (&GLRO(dl_s390_cpu_features));
229 #endif
233 static inline Elf64_Addr
234 elf_machine_fixup_plt (struct link_map *map, lookup_t t,
235 const ElfW(Sym) *refsym, const ElfW(Sym) *sym,
236 const Elf64_Rela *reloc,
237 Elf64_Addr *reloc_addr, Elf64_Addr value)
239 return *reloc_addr = value;
242 /* Return the final value of a plt relocation. */
243 static inline Elf64_Addr
244 elf_machine_plt_value (struct link_map *map, const Elf64_Rela *reloc,
245 Elf64_Addr value)
247 return value;
250 /* Names of the architecture-specific auditing callback functions. */
251 #define ARCH_LA_PLTENTER s390_64_gnu_pltenter
252 #define ARCH_LA_PLTEXIT s390_64_gnu_pltexit
254 #endif /* !dl_machine_h */
256 #ifdef RESOLVE_MAP
258 /* Perform the relocation specified by RELOC and SYM (which is fully resolved).
259 MAP is the object containing the reloc. */
261 static inline void
262 __attribute__ ((always_inline))
263 elf_machine_rela (struct link_map *map, struct r_scope_elem *scope[],
264 const Elf64_Rela *reloc, const Elf64_Sym *sym,
265 const struct r_found_version *version,
266 void *const reloc_addr_arg, int skip_ifunc)
268 Elf64_Addr *const reloc_addr = reloc_addr_arg;
269 const unsigned int r_type = ELF64_R_TYPE (reloc->r_info);
271 #if !defined RTLD_BOOTSTRAP
272 if (__glibc_unlikely (r_type == R_390_RELATIVE))
273 *reloc_addr = map->l_addr + reloc->r_addend;
274 else
275 #endif
276 if (__glibc_unlikely (r_type == R_390_NONE))
277 return;
278 else
280 #if !defined RTLD_BOOTSTRAP
281 /* Only needed for R_390_COPY below. */
282 const Elf64_Sym *const refsym = sym;
283 #endif
284 struct link_map *sym_map = RESOLVE_MAP (map, scope, &sym, version,
285 r_type);
286 Elf64_Addr value = SYMBOL_ADDRESS (sym_map, sym, true);
288 if (sym != NULL
289 && __builtin_expect (ELFW(ST_TYPE) (sym->st_info) == STT_GNU_IFUNC,
291 && __builtin_expect (sym->st_shndx != SHN_UNDEF, 1)
292 && __builtin_expect (!skip_ifunc, 1))
293 value = elf_ifunc_invoke (value);
295 switch (r_type)
297 case R_390_IRELATIVE:
298 value = map->l_addr + reloc->r_addend;
299 if (__glibc_likely (!skip_ifunc))
300 value = elf_ifunc_invoke (value);
301 *reloc_addr = value;
302 break;
303 case R_390_GLOB_DAT:
304 case R_390_JMP_SLOT:
305 *reloc_addr = value + reloc->r_addend;
306 break;
308 case R_390_TLS_DTPMOD:
309 #ifdef RTLD_BOOTSTRAP
310 /* During startup the dynamic linker is always the module
311 with index 1.
312 XXX If this relocation is necessary move before RESOLVE
313 call. */
314 *reloc_addr = 1;
315 #else
316 /* Get the information from the link map returned by the
317 resolv function. */
318 if (sym_map != NULL)
319 *reloc_addr = sym_map->l_tls_modid;
320 #endif
321 break;
322 case R_390_TLS_DTPOFF:
323 #ifndef RTLD_BOOTSTRAP
324 /* During relocation all TLS symbols are defined and used.
325 Therefore the offset is already correct. */
326 if (sym != NULL)
327 *reloc_addr = sym->st_value + reloc->r_addend;
328 #endif
329 break;
330 case R_390_TLS_TPOFF:
331 /* The offset is negative, forward from the thread pointer. */
332 #ifdef RTLD_BOOTSTRAP
333 *reloc_addr = sym->st_value + reloc->r_addend - map->l_tls_offset;
334 #else
335 /* We know the offset of the object the symbol is contained in.
336 It is a negative value which will be added to the
337 thread pointer. */
338 if (sym != NULL)
340 CHECK_STATIC_TLS (map, sym_map);
341 *reloc_addr = (sym->st_value + reloc->r_addend
342 - sym_map->l_tls_offset);
344 #endif
345 break;
347 #ifndef RTLD_BOOTSTRAP
348 /* Not needed for dl-conflict.c. */
349 case R_390_COPY:
350 if (sym == NULL)
351 /* This can happen in trace mode if an object could not be
352 found. */
353 break;
354 if (__builtin_expect (sym->st_size > refsym->st_size, 0)
355 || (__builtin_expect (sym->st_size < refsym->st_size, 0)
356 && __builtin_expect (GLRO(dl_verbose), 0)))
358 const char *strtab;
360 strtab = (const char *) D_PTR (map,l_info[DT_STRTAB]);
361 _dl_error_printf ("\
362 %s: Symbol `%s' has different size in shared object, consider re-linking\n",
363 RTLD_PROGNAME, strtab + refsym->st_name);
365 memcpy (reloc_addr_arg, (void *) value,
366 MIN (sym->st_size, refsym->st_size));
367 break;
368 case R_390_64:
369 *reloc_addr = value + reloc->r_addend;
370 break;
371 case R_390_32:
372 *(unsigned int *) reloc_addr = value + reloc->r_addend;
373 break;
374 case R_390_16:
375 *(unsigned short *) reloc_addr = value + reloc->r_addend;
376 break;
377 case R_390_8:
378 *(char *) reloc_addr = value + reloc->r_addend;
379 break;
380 case R_390_PC64:
381 *reloc_addr = value +reloc->r_addend - (Elf64_Addr) reloc_addr;
382 break;
383 case R_390_PC32DBL:
384 *(unsigned int *) reloc_addr = (unsigned int)
385 ((int) (value + reloc->r_addend - (Elf64_Addr) reloc_addr) >> 1);
386 break;
387 case R_390_PC32:
388 *(unsigned int *) reloc_addr =
389 value + reloc->r_addend - (Elf64_Addr) reloc_addr;
390 break;
391 case R_390_PC16DBL:
392 *(unsigned short *) reloc_addr = (unsigned short)
393 ((short) (value + reloc->r_addend - (Elf64_Addr) reloc_addr) >> 1);
394 break;
395 case R_390_PC16:
396 *(unsigned short *) reloc_addr =
397 value + reloc->r_addend - (Elf64_Addr) reloc_addr;
398 break;
399 case R_390_NONE:
400 break;
401 #endif
402 #if !defined(RTLD_BOOTSTRAP) || defined(_NDEBUG)
403 default:
404 /* We add these checks in the version to relocate ld.so only
405 if we are still debugging. */
406 _dl_reloc_bad_type (map, r_type, 0);
407 break;
408 #endif
413 static inline void
414 __attribute__ ((always_inline))
415 elf_machine_rela_relative (Elf64_Addr l_addr, const Elf64_Rela *reloc,
416 void *const reloc_addr_arg)
418 Elf64_Addr *const reloc_addr = reloc_addr_arg;
419 *reloc_addr = l_addr + reloc->r_addend;
422 static inline void
423 __attribute__ ((always_inline))
424 elf_machine_lazy_rel (struct link_map *map, struct r_scope_elem *scope[],
425 Elf64_Addr l_addr, const Elf64_Rela *reloc,
426 int skip_ifunc)
428 Elf64_Addr *const reloc_addr = (void *) (l_addr + reloc->r_offset);
429 const unsigned int r_type = ELF64_R_TYPE (reloc->r_info);
430 /* Check for unexpected PLT reloc type. */
431 if (__glibc_likely (r_type == R_390_JMP_SLOT))
433 if (__builtin_expect (map->l_mach.plt, 0) == 0)
434 *reloc_addr += l_addr;
435 else
436 *reloc_addr = map->l_mach.plt + (reloc - map->l_mach.jmprel) * 32;
438 else if (__glibc_likely (r_type == R_390_IRELATIVE))
440 Elf64_Addr value = map->l_addr + reloc->r_addend;
441 if (__glibc_likely (!skip_ifunc))
442 value = elf_ifunc_invoke (value);
443 *reloc_addr = value;
445 else
446 _dl_reloc_bad_type (map, r_type, 1);
449 #endif /* RESOLVE_MAP */