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