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