(CFLAGS-tst-align.c): Add -mpreferred-stack-boundary=4.
[glibc.git] / sysdeps / s390 / s390-64 / dl-machine.h
blob82ece0be0f9f2079d4b1298cccc80b85a5183537
1 /* Machine-dependent ELF dynamic relocation inline functions.
2 64 bit S/390 Version.
3 Copyright (C) 2001, 2002, 2003, 2004 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, write to the Free
19 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20 02111-1307 USA. */
22 #ifndef dl_machine_h
23 #define dl_machine_h
25 #define ELF_MACHINE_NAME "s390x"
27 #include <sys/param.h>
28 #include <string.h>
29 #include <link.h>
31 /* This is an older, now obsolete value. */
32 #define EM_S390_OLD 0xA390
34 /* Return nonzero iff E_MACHINE is compatible with the running host. */
35 static inline int
36 elf_machine_matches_host (const Elf64_Ehdr *ehdr)
38 return (ehdr->e_machine == EM_S390 || ehdr->e_machine == EM_S390_OLD)
39 && ehdr->e_ident[EI_CLASS] == ELFCLASS64;
42 /* Return the link-time address of _DYNAMIC. Conveniently, this is the
43 first element of the GOT. This must be inlined in a function which
44 uses global data. */
46 static inline Elf64_Addr
47 elf_machine_dynamic (void)
49 register Elf64_Addr *got;
51 asm( " larl %0,_GLOBAL_OFFSET_TABLE_\n"
52 : "=&a" (got) : : "0" );
54 return *got;
57 /* Return the run-time load address of the shared object. */
58 static inline Elf64_Addr
59 elf_machine_load_address (void)
61 Elf64_Addr addr;
63 asm( " larl %0,_dl_start\n"
64 " larl 1,_GLOBAL_OFFSET_TABLE_\n"
65 " lghi 2,_dl_start@GOT\n"
66 " slg %0,0(2,1)"
67 : "=&d" (addr) : : "1", "2" );
68 return addr;
71 /* Set up the loaded object described by L so its unrelocated PLT
72 entries will jump to the on-demand fixup code in dl-runtime.c. */
74 static inline int __attribute__ ((unused))
75 elf_machine_runtime_setup (struct link_map *l, int lazy, int profile)
77 extern void _dl_runtime_resolve (Elf64_Word);
78 extern void _dl_runtime_profile (Elf64_Word);
80 if (l->l_info[DT_JMPREL] && lazy)
82 /* The GOT entries for functions in the PLT have not yet been filled
83 in. Their initial contents will arrange when called to push an
84 offset into the .rela.plt section, push _GLOBAL_OFFSET_TABLE_[1],
85 and then jump to _GLOBAL_OFFSET_TABLE[2]. */
86 Elf64_Addr *got;
87 got = (Elf64_Addr *) D_PTR (l, l_info[DT_PLTGOT]);
88 /* If a library is prelinked but we have to relocate anyway,
89 we have to be able to undo the prelinking of .got.plt.
90 The prelinker saved us here address of .plt + 0x2e. */
91 if (got[1])
93 l->l_mach.plt = got[1] + l->l_addr;
94 l->l_mach.gotplt = (Elf64_Addr) &got[3];
96 got[1] = (Elf64_Addr) l; /* Identify this shared object. */
98 /* The got[2] entry contains the address of a function which gets
99 called to get the address of a so far unresolved function and
100 jump to it. The profiling extension of the dynamic linker allows
101 to intercept the calls to collect information. In this case we
102 don't store the address in the GOT so that all future calls also
103 end in this function. */
104 if (__builtin_expect (profile, 0))
106 got[2] = (Elf64_Addr) &_dl_runtime_profile;
108 if (_dl_name_match_p (GLRO(dl_profile), l))
109 /* This is the object we are looking for. Say that we really
110 want profiling and the timers are started. */
111 GL(dl_profile_map) = l;
113 else
114 /* This function will get called to fix up the GOT entry indicated by
115 the offset on the stack, and then jump to the resolved address. */
116 got[2] = (Elf64_Addr) &_dl_runtime_resolve;
119 return lazy;
122 /* This code is used in dl-runtime.c to call the `fixup' function
123 and then redirect to the address it returns. */
125 /* s390:
126 Arguments are in register.
127 r2 - r7 holds the original parameters for the function call, fixup
128 and trampoline code use r0-r5 and r14-15. For the correct function
129 call r2-r5 and r14-15 must be restored.
130 Arguments from the PLT are stored at 48(r15) and 56(r15)
131 and must be moved to r2 and r3 for the fixup call (see elf32-s390.c
132 in the binutils for the PLT code).
133 Fixup function address in r2.
135 #ifndef PROF
136 #define ELF_MACHINE_RUNTIME_TRAMPOLINE \
137 asm ( "\
138 .text\n\
139 .globl _dl_runtime_resolve\n\
140 .type _dl_runtime_resolve, @function\n\
141 .align 16\n\
142 " CFI_STARTPROC "\n\
143 _dl_runtime_resolve:\n\
144 # save registers\n\
145 stmg 2,5,64(15)\n\
146 stg 14,96(15)\n\
147 lgr 0,15\n\
148 aghi 15,-160\n\
149 " CFI_ADJUST_CFA_OFFSET(160)"\n\
150 stg 0,0(15)\n\
151 # load args saved by PLT\n\
152 lmg 2,3,208(15)\n\
153 brasl 14,fixup # call fixup\n\
154 lgr 1,2 # function addr returned in r2\n\
155 # restore registers\n\
156 aghi 15,160\n\
157 " CFI_ADJUST_CFA_OFFSET(-160)" \n\
158 lg 14,96(15)\n\
159 lmg 2,5,64(15)\n\
160 br 1\n\
161 " CFI_ENDPROC "\n\
162 .size _dl_runtime_resolve, .-_dl_runtime_resolve\n\
164 .globl _dl_runtime_profile\n\
165 .type _dl_runtime_profile, @function\n\
166 .align 16\n\
167 " CFI_STARTPROC "\n\
168 _dl_runtime_profile:\n\
169 # save registers\n\
170 stmg 2,5,64(15)\n\
171 stg 14,96(15)\n\
172 lgr 0,15\n\
173 aghi 15,-160\n\
174 " CFI_ADJUST_CFA_OFFSET(160)"\n\
175 stg 0,0(15)\n\
176 # load args saved by PLT\n\
177 lmg 2,3,208(15)\n\
178 # load return address as third parameter\n\
179 lgr 4,14\n\
180 brasl 14,profile_fixup # call fixup\n\
181 lgr 1,2 # function addr returned in r2\n\
182 # restore registers\n\
183 aghi 15,160\n\
184 " CFI_ADJUST_CFA_OFFSET(-160)" \n\
185 lg 14,96(15)\n\
186 lmg 2,5,64(15)\n\
187 br 1\n\
188 " CFI_ENDPROC "\n\
189 .size _dl_runtime_profile, .-_dl_runtime_profile\n\
191 #else
192 #define ELF_MACHINE_RUNTIME_TRAMPOLINE \
193 asm ( "\
194 .text\n\
195 .globl _dl_runtime_resolve\n\
196 .globl _dl_runtime_profile\n\
197 .type _dl_runtime_resolve, @function\n\
198 .type _dl_runtime_profile, @function\n\
199 .align 16\n\
200 " CFI_STARTPROC "\n\
201 _dl_runtime_resolve:\n\
202 _dl_runtime_profile:\n\
203 # save registers\n\
204 stmg 2,5,64(15)\n\
205 stg 14,96(15)\n\
206 lgr 0,15\n\
207 aghi 15,-160\n\
208 " CFI_ADJUST_CFA_OFFSET(160)"\n\
209 stg 0,0(15)\n\
210 # load args saved by PLT\n\
211 lmg 2,3,208(15)\n\
212 # load return address as third parameter\n\
213 lgr 4,14\n\
214 brasl 14,profile_fixup # call fixup\n\
215 lgr 1,2 # function addr returned in r2\n\
216 # restore registers\n\
217 aghi 15,160\n\
218 " CFI_ADJUST_CFA_OFFSET(-160)" \n\
219 lg 14,96(15)\n\
220 lmg 2,5,64(15)\n\
221 br 1\n\
222 " CFI_ENDPROC "\n\
223 .size _dl_runtime_resolve, .-_dl_runtime_resolve\n\
224 .size _dl_runtime_profile, .-_dl_runtime_profile\n\
226 #endif
228 /* Initial entry point code for the dynamic linker.
229 The C function `_dl_start' is the real entry point;
230 its return value is the user program's entry point. */
232 #define RTLD_START asm ("\n\
233 .text\n\
234 .align 4\n\
235 .globl _start\n\
236 .globl _dl_start_user\n\
237 _start:\n\
238 lgr %r2,%r15\n\
239 # Alloc stack frame\n\
240 aghi %r15,-160\n\
241 # Set the back chain to zero\n\
242 xc 0(8,%r15),0(%r15)\n\
243 # Call _dl_start with %r2 pointing to arg on stack\n\
244 brasl %r14,_dl_start # call _dl_start\n\
245 _dl_start_user:\n\
246 # Save the user entry point address in %r8.\n\
247 lgr %r8,%r2\n\
248 # Point %r12 at the GOT.\n\
249 larl %r12,_GLOBAL_OFFSET_TABLE_\n\
250 # See if we were run as a command with the executable file\n\
251 # name as an extra leading argument.\n\
252 lghi %r1,_dl_skip_args@GOT\n\
253 lg %r1,0(%r1,%r12)\n\
254 lgf %r1,0(%r1) # load _dl_skip_args\n\
255 # Get the original argument count.\n\
256 lg %r0,160(%r15)\n\
257 # Subtract _dl_skip_args from it.\n\
258 sgr %r0,%r1\n\
259 # Adjust the stack pointer to skip _dl_skip_args words.\n\
260 sllg %r1,%r1,3\n\
261 agr %r15,%r1\n\
262 # Set the back chain to zero again\n\
263 xc 0(8,%r15),0(%r15)\n\
264 # Store back the modified argument count.\n\
265 stg %r0,160(%r15)\n\
266 # The special initializer gets called with the stack just\n\
267 # as the application's entry point will see it; it can\n\
268 # switch stacks if it moves these contents over.\n\
269 " RTLD_START_SPECIAL_INIT "\n\
270 # Call the function to run the initializers.\n\
271 # Load the parameters:\n\
272 # (%r2, %r3, %r4, %r5) = (_dl_loaded, argc, argv, envp)\n\
273 lghi %r2,_rtld_local@GOT\n\
274 lg %r2,0(%r2,%r12)\n\
275 lg %r2,0(%r2)\n\
276 lg %r3,160(%r15)\n\
277 la %r4,168(%r15)\n\
278 lgr %r5,%r3\n\
279 sllg %r5,%r5,3\n\
280 la %r5,176(%r5,%r15)\n\
281 brasl %r14,_dl_init_internal@PLT\n\
282 # Pass our finalizer function to the user in %r14, as per ELF ABI.\n\
283 lghi %r14,_dl_fini@GOT\n\
284 lg %r14,0(%r14,%r12)\n\
285 # Free stack frame\n\
286 aghi %r15,160\n\
287 # Jump to the user's entry point (saved in %r8).\n\
288 br %r8\n\
291 #ifndef RTLD_START_SPECIAL_INIT
292 #define RTLD_START_SPECIAL_INIT /* nothing */
293 #endif
295 /* ELF_RTYPE_CLASS_PLT iff TYPE describes relocation of a PLT entry or
296 TLS variable, so undefined references should not be allowed to
297 define the value.
298 ELF_RTYPE_CLASS_NOCOPY iff TYPE should not be allowed to resolve to one
299 of the main executable's symbols, as for a COPY reloc. */
300 #ifdef USE_TLS
301 # define elf_machine_type_class(type) \
302 ((((type) == R_390_JMP_SLOT || (type) == R_390_TLS_DTPMOD \
303 || (type) == R_390_TLS_DTPOFF || (type) == R_390_TLS_TPOFF) \
304 * ELF_RTYPE_CLASS_PLT) \
305 | (((type) == R_390_COPY) * ELF_RTYPE_CLASS_COPY))
306 #else
307 # define elf_machine_type_class(type) \
308 ((((type) == R_390_JMP_SLOT) * ELF_RTYPE_CLASS_PLT) \
309 | (((type) == R_390_COPY) * ELF_RTYPE_CLASS_COPY))
310 #endif
312 /* A reloc type used for ld.so cmdline arg lookups to reject PLT entries. */
313 #define ELF_MACHINE_JMP_SLOT R_390_JMP_SLOT
315 /* The 64 bit S/390 never uses Elf64_Rel relocations. */
316 #define ELF_MACHINE_NO_REL 1
318 /* We define an initialization functions. This is called very early in
319 _dl_sysdep_start. */
320 #define DL_PLATFORM_INIT dl_platform_init ()
322 static inline void __attribute__ ((unused))
323 dl_platform_init (void)
325 if (GLRO(dl_platform) != NULL && *GLRO(dl_platform) == '\0')
326 /* Avoid an empty string which would disturb us. */
327 GLRO(dl_platform) = NULL;
330 static inline Elf64_Addr
331 elf_machine_fixup_plt (struct link_map *map, lookup_t t,
332 const Elf64_Rela *reloc,
333 Elf64_Addr *reloc_addr, Elf64_Addr value)
335 return *reloc_addr = value;
338 /* Return the final value of a plt relocation. */
339 static inline Elf64_Addr
340 elf_machine_plt_value (struct link_map *map, const Elf64_Rela *reloc,
341 Elf64_Addr value)
343 return value;
346 #endif /* !dl_machine_h */
348 #ifdef RESOLVE
350 /* Perform the relocation specified by RELOC and SYM (which is fully resolved).
351 MAP is the object containing the reloc. */
353 static inline void
354 elf_machine_rela (struct link_map *map, const Elf64_Rela *reloc,
355 const Elf64_Sym *sym, const struct r_found_version *version,
356 void *const reloc_addr_arg)
358 Elf64_Addr *const reloc_addr = reloc_addr_arg;
359 const unsigned int r_type = ELF64_R_TYPE (reloc->r_info);
361 #if !defined RTLD_BOOTSTRAP || !defined HAVE_Z_COMBRELOC
362 if (__builtin_expect (r_type == R_390_RELATIVE, 0))
364 # if !defined RTLD_BOOTSTRAP && !defined HAVE_Z_COMBRELOC
365 /* This is defined in rtld.c, but nowhere in the static libc.a;
366 make the reference weak so static programs can still link.
367 This declaration cannot be done when compiling rtld.c
368 (i.e. #ifdef RTLD_BOOTSTRAP) because rtld.c contains the
369 common defn for _dl_rtld_map, which is incompatible with a
370 weak decl in the same file. */
371 # ifndef SHARED
372 weak_extern (GL(dl_rtld_map));
373 # endif
374 if (map != &GL(dl_rtld_map)) /* Already done in rtld itself. */
375 # endif
376 *reloc_addr = map->l_addr + reloc->r_addend;
378 else
379 #endif
380 if (__builtin_expect (r_type == R_390_NONE, 0))
381 return;
382 else
384 #ifndef RESOLVE_CONFLICT_FIND_MAP
385 const Elf64_Sym *const refsym = sym;
386 #endif
387 #if defined USE_TLS && !defined RTLD_BOOTSTRAP
388 struct link_map *sym_map = RESOLVE_MAP (&sym, version, r_type);
389 Elf64_Addr value = sym == NULL ? 0 : sym_map->l_addr + sym->st_value;
390 #else
391 Elf64_Addr value = RESOLVE (&sym, version, r_type);
393 # ifndef RTLD_BOOTSTRAP
394 if (sym)
395 # endif
396 value += sym->st_value;
397 #endif /* use TLS and !RTLD_BOOTSTRAP */
399 switch (r_type)
401 case R_390_GLOB_DAT:
402 case R_390_JMP_SLOT:
403 *reloc_addr = value + reloc->r_addend;
404 break;
406 #if defined USE_TLS && (!defined RTLD_BOOTSTRAP || USE___THREAD) \
407 && !defined RESOLVE_CONFLICT_FIND_MAP
408 case R_390_TLS_DTPMOD:
409 # ifdef RTLD_BOOTSTRAP
410 /* During startup the dynamic linker is always the module
411 with index 1.
412 XXX If this relocation is necessary move before RESOLVE
413 call. */
414 *reloc_addr = 1;
415 # else
416 /* Get the information from the link map returned by the
417 resolv function. */
418 if (sym_map != NULL)
419 *reloc_addr = sym_map->l_tls_modid;
420 # endif
421 break;
422 case R_390_TLS_DTPOFF:
423 # ifndef RTLD_BOOTSTRAP
424 /* During relocation all TLS symbols are defined and used.
425 Therefore the offset is already correct. */
426 if (sym != NULL)
427 *reloc_addr = sym->st_value + reloc->r_addend;
428 # endif
429 break;
430 case R_390_TLS_TPOFF:
431 /* The offset is negative, forward from the thread pointer. */
432 # ifdef RTLD_BOOTSTRAP
433 *reloc_addr = sym->st_value + reloc->r_addend - map->l_tls_offset;
434 # else
435 /* We know the offset of the object the symbol is contained in.
436 It is a negative value which will be added to the
437 thread pointer. */
438 if (sym != NULL)
440 CHECK_STATIC_TLS (map, sym_map);
441 *reloc_addr = (sym->st_value + reloc->r_addend
442 - sym_map->l_tls_offset);
444 #endif
445 break;
446 #endif /* use TLS */
448 #ifndef RTLD_BOOTSTRAP
449 # ifndef RESOLVE_CONFLICT_FIND_MAP
450 /* Not needed for dl-conflict.c. */
451 case R_390_COPY:
452 if (sym == NULL)
453 /* This can happen in trace mode if an object could not be
454 found. */
455 break;
456 if (__builtin_expect (sym->st_size > refsym->st_size, 0)
457 || (__builtin_expect (sym->st_size < refsym->st_size, 0)
458 && __builtin_expect (GLRO(dl_verbose), 0)))
460 const char *strtab;
462 strtab = (const char *) D_PTR (map,l_info[DT_STRTAB]);
463 _dl_error_printf ("\
464 %s: Symbol `%s' has different size in shared object, consider re-linking\n",
465 rtld_progname ?: "<program name unknown>",
466 strtab + refsym->st_name);
468 memcpy (reloc_addr_arg, (void *) value,
469 MIN (sym->st_size, refsym->st_size));
470 break;
471 # endif
472 case R_390_64:
473 *reloc_addr = value + reloc->r_addend;
474 break;
475 case R_390_32:
476 *(unsigned int *) reloc_addr = value + reloc->r_addend;
477 break;
478 case R_390_16:
479 *(unsigned short *) reloc_addr = value + reloc->r_addend;
480 break;
481 case R_390_8:
482 *(char *) reloc_addr = value + reloc->r_addend;
483 break;
484 # ifndef RESOLVE_CONFLICT_FIND_MAP
485 case R_390_PC64:
486 *reloc_addr = value +reloc->r_addend - (Elf64_Addr) reloc_addr;
487 break;
488 case R_390_PC32DBL:
489 case R_390_PLT32DBL:
490 *(unsigned int *) reloc_addr = (unsigned int)
491 ((int) (value + reloc->r_addend - (Elf64_Addr) reloc_addr) >> 1);
492 break;
493 case R_390_PC32:
494 *(unsigned int *) reloc_addr =
495 value + reloc->r_addend - (Elf64_Addr) reloc_addr;
496 break;
497 case R_390_PC16DBL:
498 case R_390_PLT16DBL:
499 *(unsigned short *) reloc_addr = (unsigned short)
500 ((short) (value + reloc->r_addend - (Elf64_Addr) reloc_addr) >> 1);
501 break;
502 case R_390_PC16:
503 *(unsigned short *) reloc_addr =
504 value + reloc->r_addend - (Elf64_Addr) reloc_addr;
505 break;
506 case R_390_NONE:
507 break;
508 # endif
509 #endif
510 #if !defined(RTLD_BOOTSTRAP) || defined(_NDEBUG)
511 default:
512 /* We add these checks in the version to relocate ld.so only
513 if we are still debugging. */
514 _dl_reloc_bad_type (map, r_type, 0);
515 break;
516 #endif
521 static inline void
522 elf_machine_rela_relative (Elf64_Addr l_addr, const Elf64_Rela *reloc,
523 void *const reloc_addr_arg)
525 Elf64_Addr *const reloc_addr = reloc_addr_arg;
526 *reloc_addr = l_addr + reloc->r_addend;
529 static inline void
530 elf_machine_lazy_rel (struct link_map *map,
531 Elf64_Addr l_addr, const Elf64_Rela *reloc)
533 Elf64_Addr *const reloc_addr = (void *) (l_addr + reloc->r_offset);
534 const unsigned int r_type = ELF64_R_TYPE (reloc->r_info);
535 /* Check for unexpected PLT reloc type. */
536 if (__builtin_expect (r_type == R_390_JMP_SLOT, 1))
538 if (__builtin_expect (map->l_mach.plt, 0) == 0)
539 *reloc_addr += l_addr;
540 else
541 *reloc_addr =
542 map->l_mach.plt
543 + (((Elf64_Addr) reloc_addr) - map->l_mach.gotplt) * 4;
545 else
546 _dl_reloc_bad_type (map, r_type, 1);
549 #endif /* RESOLVE */