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