Update copyright notices with scripts/update-copyrights
[glibc.git] / sysdeps / s390 / s390-64 / dl-machine.h
blobe919223c6a970200622c23522124a05715d865db
1 /* Machine-dependent ELF dynamic relocation inline functions.
2 64 bit S/390 Version.
3 Copyright (C) 2001-2014 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 (__builtin_expect (profile, 0))
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_internal@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
209 /* We define an initialization functions. This is called very early in
210 _dl_sysdep_start. */
211 #define DL_PLATFORM_INIT dl_platform_init ()
213 static inline void __attribute__ ((unused))
214 dl_platform_init (void)
216 if (GLRO(dl_platform) != NULL && *GLRO(dl_platform) == '\0')
217 /* Avoid an empty string which would disturb us. */
218 GLRO(dl_platform) = NULL;
221 static inline Elf64_Addr
222 elf_machine_fixup_plt (struct link_map *map, lookup_t t,
223 const Elf64_Rela *reloc,
224 Elf64_Addr *reloc_addr, Elf64_Addr value)
226 return *reloc_addr = value;
229 /* Return the final value of a plt relocation. */
230 static inline Elf64_Addr
231 elf_machine_plt_value (struct link_map *map, const Elf64_Rela *reloc,
232 Elf64_Addr value)
234 return value;
237 /* Names of the architecture-specific auditing callback functions. */
238 #define ARCH_LA_PLTENTER s390_64_gnu_pltenter
239 #define ARCH_LA_PLTEXIT s390_64_gnu_pltexit
241 #endif /* !dl_machine_h */
243 #ifdef RESOLVE_MAP
245 /* Perform the relocation specified by RELOC and SYM (which is fully resolved).
246 MAP is the object containing the reloc. */
248 auto inline void
249 __attribute__ ((always_inline))
250 elf_machine_rela (struct link_map *map, const Elf64_Rela *reloc,
251 const Elf64_Sym *sym, const struct r_found_version *version,
252 void *const reloc_addr_arg, int skip_ifunc)
254 Elf64_Addr *const reloc_addr = reloc_addr_arg;
255 const unsigned int r_type = ELF64_R_TYPE (reloc->r_info);
257 #if !defined RTLD_BOOTSTRAP || !defined HAVE_Z_COMBRELOC
258 if (__builtin_expect (r_type == R_390_RELATIVE, 0))
260 # if !defined RTLD_BOOTSTRAP && !defined HAVE_Z_COMBRELOC
261 /* This is defined in rtld.c, but nowhere in the static libc.a;
262 make the reference weak so static programs can still link.
263 This declaration cannot be done when compiling rtld.c
264 (i.e. #ifdef RTLD_BOOTSTRAP) because rtld.c contains the
265 common defn for _dl_rtld_map, which is incompatible with a
266 weak decl in the same file. */
267 # ifndef SHARED
268 weak_extern (GL(dl_rtld_map));
269 # endif
270 if (map != &GL(dl_rtld_map)) /* Already done in rtld itself. */
271 # endif
272 *reloc_addr = map->l_addr + reloc->r_addend;
274 else
275 #endif
276 if (__builtin_expect (r_type == R_390_NONE, 0))
277 return;
278 else
280 #ifndef RESOLVE_CONFLICT_FIND_MAP
281 const Elf64_Sym *const refsym = sym;
282 #endif
283 struct link_map *sym_map = RESOLVE_MAP (&sym, version, r_type);
284 Elf64_Addr value = sym == NULL ? 0 : sym_map->l_addr + sym->st_value;
286 if (sym != NULL
287 && __builtin_expect (ELFW(ST_TYPE) (sym->st_info) == STT_GNU_IFUNC,
289 && __builtin_expect (sym->st_shndx != SHN_UNDEF, 1)
290 && __builtin_expect (!skip_ifunc, 1))
291 value = elf_ifunc_invoke (value);
293 switch (r_type)
295 case R_390_IRELATIVE:
296 value = map->l_addr + reloc->r_addend;
297 if (__builtin_expect (!skip_ifunc, 1))
298 value = elf_ifunc_invoke (value);
299 *reloc_addr = value;
300 break;
301 case R_390_GLOB_DAT:
302 case R_390_JMP_SLOT:
303 *reloc_addr = value + reloc->r_addend;
304 break;
306 #ifndef RESOLVE_CONFLICT_FIND_MAP
307 case R_390_TLS_DTPMOD:
308 # ifdef RTLD_BOOTSTRAP
309 /* During startup the dynamic linker is always the module
310 with index 1.
311 XXX If this relocation is necessary move before RESOLVE
312 call. */
313 *reloc_addr = 1;
314 # else
315 /* Get the information from the link map returned by the
316 resolv function. */
317 if (sym_map != NULL)
318 *reloc_addr = sym_map->l_tls_modid;
319 # endif
320 break;
321 case R_390_TLS_DTPOFF:
322 # ifndef RTLD_BOOTSTRAP
323 /* During relocation all TLS symbols are defined and used.
324 Therefore the offset is already correct. */
325 if (sym != NULL)
326 *reloc_addr = sym->st_value + reloc->r_addend;
327 # endif
328 break;
329 case R_390_TLS_TPOFF:
330 /* The offset is negative, forward from the thread pointer. */
331 # ifdef RTLD_BOOTSTRAP
332 *reloc_addr = sym->st_value + reloc->r_addend - map->l_tls_offset;
333 # else
334 /* We know the offset of the object the symbol is contained in.
335 It is a negative value which will be added to the
336 thread pointer. */
337 if (sym != NULL)
339 CHECK_STATIC_TLS (map, sym_map);
340 *reloc_addr = (sym->st_value + reloc->r_addend
341 - sym_map->l_tls_offset);
343 #endif
344 break;
345 #endif /* use TLS */
347 #ifndef RTLD_BOOTSTRAP
348 # ifndef RESOLVE_CONFLICT_FIND_MAP
349 /* Not needed for dl-conflict.c. */
350 case R_390_COPY:
351 if (sym == NULL)
352 /* This can happen in trace mode if an object could not be
353 found. */
354 break;
355 if (__builtin_expect (sym->st_size > refsym->st_size, 0)
356 || (__builtin_expect (sym->st_size < refsym->st_size, 0)
357 && __builtin_expect (GLRO(dl_verbose), 0)))
359 const char *strtab;
361 strtab = (const char *) D_PTR (map,l_info[DT_STRTAB]);
362 _dl_error_printf ("\
363 %s: Symbol `%s' has different size in shared object, consider re-linking\n",
364 RTLD_PROGNAME, strtab + refsym->st_name);
366 memcpy (reloc_addr_arg, (void *) value,
367 MIN (sym->st_size, refsym->st_size));
368 break;
369 # endif
370 case R_390_64:
371 *reloc_addr = value + reloc->r_addend;
372 break;
373 case R_390_32:
374 *(unsigned int *) reloc_addr = value + reloc->r_addend;
375 break;
376 case R_390_16:
377 *(unsigned short *) reloc_addr = value + reloc->r_addend;
378 break;
379 case R_390_8:
380 *(char *) reloc_addr = value + reloc->r_addend;
381 break;
382 # ifndef RESOLVE_CONFLICT_FIND_MAP
383 case R_390_PC64:
384 *reloc_addr = value +reloc->r_addend - (Elf64_Addr) reloc_addr;
385 break;
386 case R_390_PC32DBL:
387 *(unsigned int *) reloc_addr = (unsigned int)
388 ((int) (value + reloc->r_addend - (Elf64_Addr) reloc_addr) >> 1);
389 break;
390 case R_390_PC32:
391 *(unsigned int *) reloc_addr =
392 value + reloc->r_addend - (Elf64_Addr) reloc_addr;
393 break;
394 case R_390_PC16DBL:
395 *(unsigned short *) reloc_addr = (unsigned short)
396 ((short) (value + reloc->r_addend - (Elf64_Addr) reloc_addr) >> 1);
397 break;
398 case R_390_PC16:
399 *(unsigned short *) reloc_addr =
400 value + reloc->r_addend - (Elf64_Addr) reloc_addr;
401 break;
402 case R_390_NONE:
403 break;
404 # endif
405 #endif
406 #if !defined(RTLD_BOOTSTRAP) || defined(_NDEBUG)
407 default:
408 /* We add these checks in the version to relocate ld.so only
409 if we are still debugging. */
410 _dl_reloc_bad_type (map, r_type, 0);
411 break;
412 #endif
417 auto inline void
418 __attribute__ ((always_inline))
419 elf_machine_rela_relative (Elf64_Addr l_addr, const Elf64_Rela *reloc,
420 void *const reloc_addr_arg)
422 Elf64_Addr *const reloc_addr = reloc_addr_arg;
423 *reloc_addr = l_addr + reloc->r_addend;
426 auto inline void
427 __attribute__ ((always_inline))
428 elf_machine_lazy_rel (struct link_map *map,
429 Elf64_Addr l_addr, const Elf64_Rela *reloc,
430 int skip_ifunc)
432 Elf64_Addr *const reloc_addr = (void *) (l_addr + reloc->r_offset);
433 const unsigned int r_type = ELF64_R_TYPE (reloc->r_info);
434 /* Check for unexpected PLT reloc type. */
435 if (__builtin_expect (r_type == R_390_JMP_SLOT, 1))
437 if (__builtin_expect (map->l_mach.plt, 0) == 0)
438 *reloc_addr += l_addr;
439 else
440 *reloc_addr =
441 map->l_mach.plt
442 + (((Elf64_Addr) reloc_addr) - map->l_mach.gotplt) * 4;
444 else if (__builtin_expect (r_type == R_390_IRELATIVE, 1))
446 Elf64_Addr value = map->l_addr + reloc->r_addend;
447 if (__builtin_expect (!skip_ifunc, 1))
448 value = elf_ifunc_invoke (value);
449 *reloc_addr = value;
451 else
452 _dl_reloc_bad_type (map, r_type, 1);
455 #endif /* RESOLVE_MAP */