1 /* Machine-dependent ELF dynamic relocation inline functions. S390 Version.
2 Copyright (C) 2000-2023 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 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.
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 Lesser General Public License for more details.
14
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/>. */
18
19 #ifndef dl_machine_h
20 #define dl_machine_h
21
22 #define ELF_MACHINE_NAME "s390"
23
24 #include <sys/param.h>
25 #include <string.h>
26 #include <link.h>
27 #include <sysdeps/s390/dl-procinfo.h>
28 #include <dl-irel.h>
29 #include <dl-static-tls.h>
30 #include <dl-machine-rel.h>
31 #include <cpu-features.c>
32
33 #define ELF_MACHINE_IRELATIVE R_390_IRELATIVE
34
35 /* This is an older, now obsolete value. */
36 #define EM_S390_OLD 0xA390
37
38 /* Return nonzero iff ELF header is compatible with the running host. */
39 static inline int
40 elf_machine_matches_host (const Elf32_Ehdr *ehdr)
41 {
42 /* Check if the kernel provides the high gpr facility if needed by
43 the binary. */
44 if ((ehdr->e_flags & EF_S390_HIGH_GPRS)
45 && !(GLRO (dl_hwcap) & HWCAP_S390_HIGH_GPRS))
46 return 0;
47
48 return (ehdr->e_machine == EM_S390 || ehdr->e_machine == EM_S390_OLD)
49 && ehdr->e_ident[EI_CLASS] == ELFCLASS32;
50 }
51
52
53 /* Return the link-time address of _DYNAMIC. Conveniently, this is the
54 first element of the GOT. This must be inlined in a function which
55 uses global data. */
56
57 static inline Elf32_Addr
58 elf_machine_dynamic (void)
59 {
60 register Elf32_Addr *got;
61
62 __asm__( " bras %0,2f\n"
63 "1: .long _GLOBAL_OFFSET_TABLE_-1b\n"
64 "2: al %0,0(%0)"
65 : "=&a" (got) : : "0" );
66
67 return *got;
68 }
69
70
71 /* Return the run-time load address of the shared object. */
72 static inline Elf32_Addr
73 elf_machine_load_address (void)
74 {
75 Elf32_Addr addr;
76
77 __asm__( " bras 1,2f\n"
78 "1: .long _GLOBAL_OFFSET_TABLE_ - 1b\n"
79 " .long (_dl_start - 1b - 0x80000000) & 0x00000000ffffffff\n"
80 "2: l %0,4(1)\n"
81 " ar %0,1\n"
82 " al 1,0(1)\n"
83 " sl %0,_dl_start@GOT(1)"
84 : "=&d" (addr) : : "1" );
85 return addr;
86 }
87
88 /* Set up the loaded object described by L so its unrelocated PLT
89 entries will jump to the on-demand fixup code in dl-runtime.c. */
90
91 static inline int __attribute__ ((unused))
92 elf_machine_runtime_setup (struct link_map *l, struct r_scope_elem *scope[],
93 int lazy, int profile)
94 {
95 extern void _dl_runtime_resolve (Elf32_Word);
96 extern void _dl_runtime_profile (Elf32_Word);
97 #if defined HAVE_S390_VX_ASM_SUPPORT
98 extern void _dl_runtime_resolve_vx (Elf32_Word);
99 extern void _dl_runtime_profile_vx (Elf32_Word);
100 #endif
101
102
103 if (l->l_info[DT_JMPREL] && lazy)
104 {
105 /* The GOT entries for functions in the PLT have not yet been filled
106 in. Their initial contents will arrange when called to push an
107 offset into the .rel.plt section, push _GLOBAL_OFFSET_TABLE_[1],
108 and then jump to _GLOBAL_OFFSET_TABLE[2]. */
109 Elf32_Addr *got;
110 got = (Elf32_Addr *) D_PTR (l, l_info[DT_PLTGOT]);
111 /* If a library is prelinked but we have to relocate anyway,
112 we have to be able to undo the prelinking of .got.plt.
113 The prelinker saved us here address of .plt + 0x2c. */
114 if (got[1])
115 {
116 l->l_mach.plt = got[1] + l->l_addr;
117 l->l_mach.jmprel = (const Elf32_Rela *) D_PTR (l, l_info[DT_JMPREL]);
118 }
119 got[1] = (Elf32_Addr) l; /* Identify this shared object. */
120
121 /* The got[2] entry contains the address of a function which gets
122 called to get the address of a so far unresolved function and
123 jump to it. The profiling extension of the dynamic linker allows
124 to intercept the calls to collect information. In this case we
125 don't store the address in the GOT so that all future calls also
126 end in this function. */
127 if (__glibc_unlikely (profile))
128 {
129 #if defined HAVE_S390_VX_ASM_SUPPORT
130 if (GLRO(dl_hwcap) & HWCAP_S390_VX)
131 got[2] = (Elf32_Addr) &_dl_runtime_profile_vx;
132 else
133 got[2] = (Elf32_Addr) &_dl_runtime_profile;
134 #else
135 got[2] = (Elf32_Addr) &_dl_runtime_profile;
136 #endif
137
138 if (GLRO(dl_profile) != NULL
139 && _dl_name_match_p (GLRO(dl_profile), l))
140 /* This is the object we are looking for. Say that we really
141 want profiling and the timers are started. */
142 GL(dl_profile_map) = l;
143 }
144 else
145 {
146 /* This function will get called to fix up the GOT entry indicated by
147 the offset on the stack, and then jump to the resolved address. */
148 #if defined HAVE_S390_VX_ASM_SUPPORT
149 if (GLRO(dl_hwcap) & HWCAP_S390_VX)
150 got[2] = (Elf32_Addr) &_dl_runtime_resolve_vx;
151 else
152 got[2] = (Elf32_Addr) &_dl_runtime_resolve;
153 #else
154 got[2] = (Elf32_Addr) &_dl_runtime_resolve;
155 #endif
156 }
157 }
158
159 return lazy;
160 }
161
162 /* Mask identifying addresses reserved for the user program,
163 where the dynamic linker should not map anything. */
164 #define ELF_MACHINE_USER_ADDRESS_MASK 0xf8000000UL
165
166 /* Initial entry point code for the dynamic linker.
167 The C function `_dl_start' is the real entry point;
168 its return value is the user program's entry point. */
169
170 #define RTLD_START __asm__ ("\n\
171 .text\n\
172 .align 4\n\
173 .globl _start\n\
174 .globl _dl_start_user\n\
175 _start:\n\
176 basr %r13,0\n\
177 0: ahi %r13,.Llit-0b\n\
178 lr %r2,%r15\n\
179 # Alloc stack frame\n\
180 ahi %r15,-96\n\
181 # Set the back chain to zero\n\
182 xc 0(4,%r15),0(%r15)\n\
183 # Call _dl_start with %r2 pointing to arg on stack\n\
184 l %r14,.Ladr1-.Llit(%r13)\n\
185 bas %r14,0(%r14,%r13) # call _dl_start\n\
186 _dl_start_user:\n\
187 # Save the user entry point address in %r8.\n\
188 lr %r8,%r2\n\
189 # Point %r12 at the GOT.\n\
190 l %r12,.Ladr0-.Llit(%r13)\n\
191 ar %r12,%r13\n\
192 # The special initializer gets called with the stack just\n\
193 # as the application's entry point will see it; it can\n\
194 # switch stacks if it moves these contents over.\n\
195 " RTLD_START_SPECIAL_INIT "\n\
196 # Call the function to run the initializers.\n\
197 # Load the parameters:\n\
198 # (%r2, %r3, %r4, %r5) = (_dl_loaded, argc, argv, envp)\n\
199 4: l %r2,_rtld_local@GOT(%r12)\n\
200 l %r2,0(%r2)\n\
201 l %r3,96(%r15)\n\
202 la %r4,100(%r15)\n\
203 lr %r5,%r3\n\
204 sll %r5,2\n\
205 la %r5,104(%r5,%r15)\n\
206 l %r1,.Ladr4-.Llit(%r13)\n\
207 bas %r14,0(%r1,%r13)\n\
208 # Pass our finalizer function to the user in %r14, as per ELF ABI.\n\
209 l %r14,_dl_fini@GOT(%r12)\n\
210 # Free stack frame\n\
211 ahi %r15,96\n\
212 # Jump to the user's entry point (saved in %r8).\n\
213 br %r8\n\
214 .Llit:\n\
215 .Ladr0: .long _GLOBAL_OFFSET_TABLE_-.Llit\n\
216 .Ladr1: .long _dl_start-.Llit\n\
217 .Ladr4: .long _dl_init@PLT-.Llit\n\
218 ");
219
220 #ifndef RTLD_START_SPECIAL_INIT
221 #define RTLD_START_SPECIAL_INIT /* nothing */
222 #endif
223
224 /* ELF_RTYPE_CLASS_PLT iff TYPE describes relocation of a PLT entry or
225 TLS variable, so undefined references should not be allowed to
226 define the value.
227 ELF_RTYPE_CLASS_COPY iff TYPE should not be allowed to resolve to one
228 of the main executable's symbols, as for a COPY reloc. */
229 #define elf_machine_type_class(type) \
230 ((((type) == R_390_JMP_SLOT || (type) == R_390_TLS_DTPMOD \
231 || (type) == R_390_TLS_DTPOFF || (type) == R_390_TLS_TPOFF) \
232 * ELF_RTYPE_CLASS_PLT) \
233 | (((type) == R_390_COPY) * ELF_RTYPE_CLASS_COPY))
234
235 /* A reloc type used for ld.so cmdline arg lookups to reject PLT entries. */
236 #define ELF_MACHINE_JMP_SLOT R_390_JMP_SLOT
237
238 /* We define an initialization functions. This is called very early in
239 _dl_sysdep_start. */
240 #define DL_PLATFORM_INIT dl_platform_init ()
241
242 static inline void __attribute__ ((unused))
243 dl_platform_init (void)
244 {
245 if (GLRO(dl_platform) != NULL && *GLRO(dl_platform) == '\0')
246 /* Avoid an empty string which would disturb us. */
247 GLRO(dl_platform) = NULL;
248
249 #ifdef SHARED
250 /* init_cpu_features has been called early from __libc_start_main in
251 static executable. */
252 init_cpu_features (&GLRO(dl_s390_cpu_features));
253 #endif
254 }
255
256 static inline Elf32_Addr
257 elf_machine_fixup_plt (struct link_map *map, lookup_t t,
258 const ElfW(Sym) *refsym, const ElfW(Sym) *sym,
259 const Elf32_Rela *reloc,
260 Elf32_Addr *reloc_addr, Elf32_Addr value)
261 {
262 return *reloc_addr = value;
263 }
264
265 /* Return the final value of a plt relocation. */
266 static inline Elf32_Addr
267 elf_machine_plt_value (struct link_map *map, const Elf32_Rela *reloc,
268 Elf32_Addr value)
269 {
270 return value;
271 }
272
273 /* Names of the architecture-specific auditing callback functions. */
274 #define ARCH_LA_PLTENTER s390_32_gnu_pltenter
275 #define ARCH_LA_PLTEXIT s390_32_gnu_pltexit
276
277 #endif /* !dl_machine_h */
278
279
280 #ifdef RESOLVE_MAP
281
282 /* Perform the relocation specified by RELOC and SYM (which is fully resolved).
283 MAP is the object containing the reloc. */
284
285 static inline void
286 __attribute__ ((always_inline))
287 elf_machine_rela (struct link_map *map, struct r_scope_elem *scope[],
288 const Elf32_Rela *reloc, const Elf32_Sym *sym,
289 const struct r_found_version *version,
290 void *const reloc_addr_arg, int skip_ifunc)
291 {
292 Elf32_Addr *const reloc_addr = reloc_addr_arg;
293 const unsigned int r_type = ELF32_R_TYPE (reloc->r_info);
294
295 #if !defined RTLD_BOOTSTRAP
296 if (__glibc_unlikely (r_type == R_390_RELATIVE))
297 *reloc_addr = map->l_addr + reloc->r_addend;
298 else
299 #endif
300 if (__glibc_unlikely (r_type == R_390_NONE))
301 return;
302 else
303 {
304 #if !defined RTLD_BOOTSTRAP
305 /* Only needed for R_390_COPY below. */
306 const Elf32_Sym *const refsym = sym;
307 #endif
308 struct link_map *sym_map = RESOLVE_MAP (map, scope, &sym, version,
309 r_type);
310 Elf32_Addr value = SYMBOL_ADDRESS (sym_map, sym, true);
311
312 if (sym != NULL
313 && __builtin_expect (ELFW(ST_TYPE) (sym->st_info) == STT_GNU_IFUNC, 0)
314 && __builtin_expect (sym->st_shndx != SHN_UNDEF, 1)
315 && __builtin_expect (!skip_ifunc, 1))
316 value = elf_ifunc_invoke (value);
317
318 switch (r_type)
319 {
320 case R_390_IRELATIVE:
321 value = map->l_addr + reloc->r_addend;
322 if (__glibc_likely (!skip_ifunc))
323 value = elf_ifunc_invoke (value);
324 *reloc_addr = value;
325 break;
326
327 case R_390_GLOB_DAT:
328 case R_390_JMP_SLOT:
329 *reloc_addr = value + reloc->r_addend;
330 break;
331
332 case R_390_TLS_DTPMOD:
333 #ifdef RTLD_BOOTSTRAP
334 /* During startup the dynamic linker is always the module
335 with index 1.
336 XXX If this relocation is necessary move before RESOLVE
337 call. */
338 *reloc_addr = 1;
339 #else
340 /* Get the information from the link map returned by the
341 resolv function. */
342 if (sym_map != NULL)
343 *reloc_addr = sym_map->l_tls_modid;
344 #endif
345 break;
346 case R_390_TLS_DTPOFF:
347 #ifndef RTLD_BOOTSTRAP
348 /* During relocation all TLS symbols are defined and used.
349 Therefore the offset is already correct. */
350 if (sym != NULL)
351 *reloc_addr = sym->st_value + reloc->r_addend;
352 #endif
353 break;
354 case R_390_TLS_TPOFF:
355 /* The offset is negative, forward from the thread pointer. */
356 #ifdef RTLD_BOOTSTRAP
357 *reloc_addr = sym->st_value + reloc->r_addend - map->l_tls_offset;
358 #else
359 /* We know the offset of the object the symbol is contained in.
360 It is a negative value which will be added to the
361 thread pointer. */
362 if (sym != NULL)
363 {
364 CHECK_STATIC_TLS (map, sym_map);
365 *reloc_addr = (sym->st_value + reloc->r_addend
366 - sym_map->l_tls_offset);
367 }
368 #endif
369 break;
370
371 #ifndef RTLD_BOOTSTRAP
372 /* Not needed in dl-conflict.c. */
373 case R_390_COPY:
374 if (sym == NULL)
375 /* This can happen in trace mode if an object could not be
376 found. */
377 break;
378 if (__builtin_expect (sym->st_size > refsym->st_size, 0)
379 || (__builtin_expect (sym->st_size < refsym->st_size, 0)
380 && __builtin_expect (GLRO(dl_verbose), 0)))
381 {
382 const char *strtab;
383
384 strtab = (const char *) D_PTR(map,l_info[DT_STRTAB]);
385 _dl_error_printf ("\
386 %s: Symbol `%s' has different size in shared object, consider re-linking\n",
387 RTLD_PROGNAME, strtab + refsym->st_name);
388 }
389 memcpy (reloc_addr_arg, (void *) value,
390 MIN (sym->st_size, refsym->st_size));
391 break;
392 case R_390_32:
393 *reloc_addr = value + reloc->r_addend;
394 break;
395 case R_390_16:
396 *(unsigned short *) reloc_addr = value + reloc->r_addend;
397 break;
398 case R_390_8:
399 *(char *) reloc_addr = value + reloc->r_addend;
400 break;
401 case R_390_PC32:
402 *reloc_addr = value + reloc->r_addend - (Elf32_Addr) reloc_addr;
403 break;
404 case R_390_PC16DBL:
405 *(unsigned short *) reloc_addr = (unsigned short)
406 ((short) (value + reloc->r_addend - (Elf32_Addr) reloc_addr) >> 1);
407 break;
408 case R_390_PC32DBL:
409 *(unsigned int *) reloc_addr = (unsigned int)
410 ((int) (value + reloc->r_addend - (Elf32_Addr) reloc_addr) >> 1);
411 break;
412 case R_390_PC16:
413 *(unsigned short *) reloc_addr =
414 value + reloc->r_addend - (Elf32_Addr) reloc_addr;
415 break;
416 case R_390_NONE:
417 break;
418 #endif
419 #if !defined(RTLD_BOOTSTRAP) || defined(_NDEBUG)
420 default:
421 /* We add these checks in the version to relocate ld.so only
422 if we are still debugging. */
423 _dl_reloc_bad_type (map, r_type, 0);
424 break;
425 #endif
426 }
427 }
428 }
429
430 static inline void
431 __attribute__ ((always_inline))
432 elf_machine_rela_relative (Elf32_Addr l_addr, const Elf32_Rela *reloc,
433 void *const reloc_addr_arg)
434 {
435 Elf32_Addr *const reloc_addr = reloc_addr_arg;
436 *reloc_addr = l_addr + reloc->r_addend;
437 }
438
439 static inline void
440 __attribute__ ((always_inline))
441 elf_machine_lazy_rel (struct link_map *map, struct r_scope_elem *scope[],
442 Elf32_Addr l_addr, const Elf32_Rela *reloc,
443 int skip_ifunc)
444 {
445 Elf32_Addr *const reloc_addr = (void *) (l_addr + reloc->r_offset);
446 const unsigned int r_type = ELF32_R_TYPE (reloc->r_info);
447 /* Check for unexpected PLT reloc type. */
448 if (__glibc_likely (r_type == R_390_JMP_SLOT))
449 {
450 if (__builtin_expect (map->l_mach.plt, 0) == 0)
451 *reloc_addr += l_addr;
452 else
453 *reloc_addr = map->l_mach.plt + (reloc - map->l_mach.jmprel) * 32;
454 }
455 else if (__glibc_likely (r_type == R_390_IRELATIVE))
456 {
457 Elf32_Addr value = map->l_addr + reloc->r_addend;
458 if (__glibc_likely (!skip_ifunc))
459 value = elf_ifunc_invoke (value);
460 *reloc_addr = value;
461 }
462 else
463 _dl_reloc_bad_type (map, r_type, 1);
464 }
465
466 #endif /* RESOLVE_MAP */