]> git.ipfire.org Git - thirdparty/glibc.git/blame - sysdeps/s390/s390-64/dl-machine.h
Update.
[thirdparty/glibc.git] / sysdeps / s390 / s390-64 / dl-machine.h
CommitLineData
ffeac417
UD
1/* Machine-dependent ELF dynamic relocation inline functions.
2 64 bit S/390 Version.
3 Copyright (C) 2001 Free Software Foundation, Inc.
4 Contributed by Martin Schwidefsky (schwidefsky@de.ibm.com).
847b055c
AJ
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
41bdb6e2
AJ
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.
847b055c
AJ
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
41bdb6e2 15 Lesser General Public License for more details.
847b055c 16
41bdb6e2
AJ
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. */
847b055c
AJ
21
22#ifndef dl_machine_h
23#define dl_machine_h
1721af3f 24
ffeac417 25#define ELF_MACHINE_NAME "s390x"
847b055c
AJ
26
27#include <sys/param.h>
28#include <string.h>
29#include <link.h>
30
9672b937
AJ
31/* This is an older, now obsolete value. */
32#define EM_S390_OLD 0xA390
847b055c 33
ffeac417 34/* Return nonzero iff E_MACHINE is compatible with the running host. */
847b055c 35static inline int
ffeac417 36elf_machine_matches_host (const Elf64_Ehdr *ehdr)
847b055c 37{
ffeac417
UD
38 return (ehdr->e_machine == EM_S390 || ehdr->e_machine == EM_S390_OLD)
39 && ehdr->e_ident[EI_CLASS] == ELFCLASS64;
847b055c
AJ
40}
41
847b055c
AJ
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. */
45
ffeac417 46static inline Elf64_Addr
847b055c
AJ
47elf_machine_dynamic (void)
48{
ffeac417 49 register Elf64_Addr *got;
847b055c 50
ffeac417 51 asm( " larl %0,_GLOBAL_OFFSET_TABLE_\n"
847b055c
AJ
52 : "=&a" (got) : : "0" );
53
54 return *got;
55}
56
847b055c 57/* Return the run-time load address of the shared object. */
ffeac417 58static inline Elf64_Addr
847b055c
AJ
59elf_machine_load_address (void)
60{
ffeac417
UD
61 Elf64_Addr addr;
62
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" );
847b055c
AJ
68 return addr;
69}
70
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. */
73
74static inline int __attribute__ ((unused))
75elf_machine_runtime_setup (struct link_map *l, int lazy, int profile)
76{
ffeac417
UD
77 extern void _dl_runtime_resolve (Elf64_Word);
78 extern void _dl_runtime_profile (Elf64_Word);
847b055c 79
1721af3f 80 if (l->l_info[DT_JMPREL] && lazy)
847b055c
AJ
81 {
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
ffeac417 84 offset into the .rela.plt section, push _GLOBAL_OFFSET_TABLE_[1],
847b055c 85 and then jump to _GLOBAL_OFFSET_TABLE[2]. */
ffeac417
UD
86 Elf64_Addr *got;
87 got = (Elf64_Addr *) D_PTR (l, l_info[DT_PLTGOT]);
32e6df36
UD
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])
92 {
93 l->l_mach.plt = got[1] + l->l_addr;
94 l->l_mach.gotplt = (Elf64_Addr) &got[3];
95 }
ffeac417 96 got[1] = (Elf64_Addr) l; /* Identify this shared object. */
847b055c
AJ
97
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
ffeac417 101 to intercept the calls to collect information. In this case we
847b055c 102 don't store the address in the GOT so that all future calls also
ffeac417 103 end in this function. */
847b055c
AJ
104 if (__builtin_expect (profile, 0))
105 {
ffeac417 106 got[2] = (Elf64_Addr) &_dl_runtime_profile;
1721af3f 107
847b055c
AJ
108 if (_dl_name_match_p (_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 _dl_profile_map = l;
112 }
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. */
ffeac417 116 got[2] = (Elf64_Addr) &_dl_runtime_resolve;
847b055c
AJ
117 }
118
119 return lazy;
120}
121
122/* This code is used in dl-runtime.c to call the `fixup' function
ffeac417 123 and then redirect to the address it returns. */
847b055c
AJ
124
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.
ffeac417 130 Arguments from the PLT are stored at 48(r15) and 56(r15)
847b055c
AJ
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.
134*/
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_dl_runtime_resolve:\n\
143 # save registers\n\
ffeac417
UD
144 stmg 2,5,64(15)\n\
145 stg 14,96(15)\n\
146 lgr 0,15\n\
147 aghi 15,-160\n\
148 stg 0,0(15)\n\
847b055c 149 # load args saved by PLT\n\
ffeac417
UD
150 lmg 2,3,208(15)\n\
151 brasl 14,fixup # call fixup
152 lgr 1,2 # function addr returned in r2\n\
847b055c 153 # restore registers\n\
ffeac417
UD
154 aghi 15,160\n\
155 lg 14,96(15)\n\
156 lmg 2,5,64(15)\n\
157 br 1\n\
847b055c
AJ
158 .size _dl_runtime_resolve, .-_dl_runtime_resolve\n\
159\n\
160 .globl _dl_runtime_profile\n\
161 .type _dl_runtime_profile, @function\n\
162 .align 16\n\
163_dl_runtime_profile:\n\
164 # save registers\n\
ffeac417
UD
165 stmg 2,5,64(15)\n\
166 stg 14,96(15)\n\
167 lgr 0,15\n\
168 aghi 15,-160\n\
169 stg 0,0(15)\n\
847b055c 170 # load args saved by PLT\n\
ffeac417 171 lmg 2,3,208(15)\n\
847b055c 172 # load return address as third parameter\n\
ffeac417
UD
173 lgr 4,14\n\
174 brasl 14,profile_fixup # call fixup\n\
175 lgr 1,2 # function addr returned in r2\n\
847b055c 176 # restore registers\n\
ffeac417
UD
177 aghi 15,160\n\
178 lg 14,96(15)\n\
179 lmg 2,5,64(15)\n\
180 br 1\n\
847b055c
AJ
181 .size _dl_runtime_profile, .-_dl_runtime_profile\n\
182");
183#else
184#define ELF_MACHINE_RUNTIME_TRAMPOLINE \
185 asm ( "\
186 .text\n\
187 .globl _dl_runtime_resolve\n\
188 .globl _dl_runtime_profile\n\
189 .type _dl_runtime_resolve, @function\n\
190 .type _dl_runtime_profile, @function\n\
191 .align 16\n\
192_dl_runtime_resolve:\n\
193_dl_runtime_profile:\n\
194 # save registers\n\
ffeac417
UD
195 stmg 2,5,64(15)\n\
196 stg 14,96(15)\n\
197 lgr 0,15\n\
198 aghi 15,-160\n\
199 stg 0,0(15)\n\
847b055c 200 # load args saved by PLT\n\
ffeac417 201 lmg 2,3,208(15)\n\
847b055c 202 # load return address as third parameter\n\
ffeac417
UD
203 lgr 4,14\n\
204 brasl 14,profile_fixup # call fixup\n\
205 lgr 1,2 # function addr returned in r2\n\
847b055c 206 # restore registers\n\
ffeac417
UD
207 aghi 15,160\n\
208 lg 14,96(15)\n\
209 lmg 2,5,64(15)\n\
210 br 1\n\
847b055c
AJ
211 .size _dl_runtime_resolve, .-_dl_runtime_resolve\n\
212 .size _dl_runtime_profile, .-_dl_runtime_profile\n\
213");
1721af3f 214#endif
847b055c
AJ
215
216/* Initial entry point code for the dynamic linker.
217 The C function `_dl_start' is the real entry point;
ffeac417 218 its return value is the user program's entry point. */
847b055c
AJ
219
220#define RTLD_START asm ("\n\
221.text\n\
222.align 4\n\
223.globl _start\n\
224.globl _dl_start_user\n\
225_start:\n\
ffeac417 226 lgr %r2,%r15\n\
847b055c 227 # Alloc stack frame\n\
ffeac417 228 aghi %r15,-160\n\
847b055c 229 # Set the back chain to zero\n\
ffeac417 230 xc 0(8,%r15),0(%r15)\n\
847b055c 231 # Call _dl_start with %r2 pointing to arg on stack\n\
ffeac417 232 brasl %r14,_dl_start # call _dl_start\n\
847b055c
AJ
233_dl_start_user:\n\
234 # Save the user entry point address in %r8.\n\
ffeac417 235 lgr %r8,%r2\n\
847b055c 236 # Point %r12 at the GOT.\n\
ffeac417 237 larl %r12,_GLOBAL_OFFSET_TABLE_\n\
847b055c 238 # Store the highest stack address\n\
ffeac417
UD
239 lghi %r1,__libc_stack_end@GOT
240 lg %r1,0(%r1,%r12)\n\
241 stg %r15, 0(%r1)\n\
847b055c
AJ
242 # See if we were run as a command with the executable file\n\
243 # name as an extra leading argument.\n\
ffeac417
UD
244 lghi %r1,_dl_skip_args@GOT
245 lg %r1,0(%r1,%r12)\n\
246 lgf %r1,0(%r1) # load _dl_skip_args\n\
847b055c 247 # Get the original argument count.\n\
ffeac417 248 lg %r0,160(%r15)\n\
847b055c 249 # Subtract _dl_skip_args from it.\n\
ffeac417 250 sgr %r0,%r1\n\
847b055c 251 # Adjust the stack pointer to skip _dl_skip_args words.\n\
ffeac417
UD
252 sllg %r1,%r1,3\n\
253 agr %r15,%r1\n\
847b055c 254 # Set the back chain to zero again\n\
ffeac417 255 xc 0(8,%r15),0(%r15)\n\
847b055c 256 # Store back the modified argument count.\n\
ffeac417 257 stg %r0,160(%r15)\n\
847b055c
AJ
258 # The special initializer gets called with the stack just\n\
259 # as the application's entry point will see it; it can\n\
260 # switch stacks if it moves these contents over.\n\
261" RTLD_START_SPECIAL_INIT "\n\
262 # Call the function to run the initializers.\n\
263 # Load the parameters:\n\
264 # (%r2, %r3, %r4, %r5) = (_dl_loaded, argc, argv, envp)\n\
ffeac417
UD
265 lghi %r2,_dl_loaded@GOT
266 lg %r2,0(%r2,%r12)\n\
267 lg %r2,0(%r2)\n\
268 lg %r3,160(%r15)\n\
269 la %r4,168(%r15)\n\
270 lgr %r5,%r3\n\
271 sllg %r5,%r5,3\n\
272 la %r5,176(%r5,%r15)\n\
273 brasl %r14,_dl_init@PLT\n
847b055c 274 # Pass our finalizer function to the user in %r14, as per ELF ABI.\n\
ffeac417
UD
275 lghi %r14,_dl_fini@GOT
276 lg %r14,0(%r14,%r12)\n\
847b055c 277 # Free stack frame\n\
ffeac417 278 aghi %r15,160\n\
847b055c
AJ
279 # Jump to the user's entry point (saved in %r8).\n\
280 br %r8\n\
847b055c
AJ
281");
282
283#ifndef RTLD_START_SPECIAL_INIT
284#define RTLD_START_SPECIAL_INIT /* nothing */
285#endif
286
cf5a372e
UD
287/* ELF_RTYPE_CLASS_PLT iff TYPE describes relocation of a PLT entry, so
288 PLT entries should not be allowed to define the value.
289 ELF_RTYPE_CLASS_NOCOPY iff TYPE should not be allowed to resolve to one
290 of the main executable's symbols, as for a COPY reloc. */
291#define elf_machine_type_class(type) \
292 ((((type) == R_390_JMP_SLOT) * ELF_RTYPE_CLASS_PLT) \
293 | (((type) == R_390_COPY) * ELF_RTYPE_CLASS_COPY))
847b055c
AJ
294
295/* A reloc type used for ld.so cmdline arg lookups to reject PLT entries. */
1721af3f 296#define ELF_MACHINE_JMP_SLOT R_390_JMP_SLOT
847b055c 297
ffeac417 298/* The 64 bit S/390 never uses Elf64_Rel relocations. */
847b055c
AJ
299#define ELF_MACHINE_NO_REL 1
300
847b055c
AJ
301/* We define an initialization functions. This is called very early in
302 _dl_sysdep_start. */
303#define DL_PLATFORM_INIT dl_platform_init ()
304
305extern const char *_dl_platform;
306
307static inline void __attribute__ ((unused))
308dl_platform_init (void)
309{
310 if (_dl_platform != NULL && *_dl_platform == '\0')
311 /* Avoid an empty string which would disturb us. */
312 _dl_platform = NULL;
313}
314
ffeac417 315static inline Elf64_Addr
847b055c 316elf_machine_fixup_plt (struct link_map *map, lookup_t t,
ffeac417
UD
317 const Elf64_Rela *reloc,
318 Elf64_Addr *reloc_addr, Elf64_Addr value)
847b055c
AJ
319{
320 return *reloc_addr = value;
321}
322
ffeac417
UD
323/* Return the final value of a plt relocation. */
324static inline Elf64_Addr
325elf_machine_plt_value (struct link_map *map, const Elf64_Rela *reloc,
326 Elf64_Addr value)
847b055c
AJ
327{
328 return value;
1721af3f 329}
847b055c 330
1721af3f 331#endif /* !dl_machine_h */
847b055c
AJ
332
333#ifdef RESOLVE
334
335/* Perform the relocation specified by RELOC and SYM (which is fully resolved).
336 MAP is the object containing the reloc. */
337
338static inline void
ffeac417
UD
339elf_machine_rela (struct link_map *map, const Elf64_Rela *reloc,
340 const Elf64_Sym *sym, const struct r_found_version *version,
341 Elf64_Addr *const reloc_addr)
847b055c 342{
1721af3f
UD
343 const unsigned int r_type = ELF64_R_TYPE (reloc->r_info);
344
345 if (__builtin_expect (r_type == R_390_RELATIVE, 0))
346 *reloc_addr = map->l_addr + reloc->r_addend;
847b055c 347#ifndef RTLD_BOOTSTRAP
1721af3f
UD
348 else if (__builtin_expect (r_type == R_390_NONE, 0))
349 return;
847b055c 350#endif
1721af3f 351 else
847b055c 352 {
ffeac417 353 const Elf64_Sym *const refsym = sym;
1721af3f 354 Elf64_Addr value = RESOLVE (&sym, version, r_type);
847b055c
AJ
355 if (sym)
356 value += sym->st_value;
1721af3f
UD
357
358 switch (r_type)
847b055c 359 {
ffeac417
UD
360 case R_390_GLOB_DAT:
361 case R_390_JMP_SLOT:
362 *reloc_addr = value + reloc->r_addend;
363 break;
364#ifndef RTLD_BOOTSTRAP
847b055c
AJ
365 case R_390_COPY:
366 if (sym == NULL)
367 /* This can happen in trace mode if an object could not be
368 found. */
369 break;
370 if (__builtin_expect (sym->st_size > refsym->st_size, 0)
371 || (__builtin_expect (sym->st_size < refsym->st_size, 0)
372 && __builtin_expect (_dl_verbose, 0)))
373 {
374 const char *strtab;
375
ffeac417 376 strtab = (const char *) D_PTR (map,l_info[DT_STRTAB]);
35fc382a
UD
377 _dl_error_printf ("\
378%s: Symbol `%s' has different size in shared object, consider re-linking\n",
379 _dl_argv[0] ?: "<program name unknown>",
380 strtab + refsym->st_name);
847b055c
AJ
381 }
382 memcpy (reloc_addr, (void *) value, MIN (sym->st_size,
383 refsym->st_size));
1721af3f 384 break;
ffeac417
UD
385 case R_390_64:
386 *reloc_addr = value + reloc->r_addend;
847b055c
AJ
387 break;
388 case R_390_32:
ffeac417
UD
389 *(unsigned int *) reloc_addr = value + reloc->r_addend;
390 break;
391 case R_390_16:
392 *(unsigned short *) reloc_addr = value + reloc->r_addend;
393 break;
394 case R_390_8:
395 *(char *) reloc_addr = value + reloc->r_addend;
396 break;
397 case R_390_PC64:
398 *reloc_addr = value +reloc->r_addend - (Elf64_Addr) reloc_addr;
399 break;
400 case R_390_PC32DBL:
401 case R_390_PLT32DBL:
402 *(unsigned int *) reloc_addr = (unsigned int)
403 ((int) (value + reloc->r_addend - (Elf64_Addr) reloc_addr) >> 1);
404 break;
847b055c 405 case R_390_PC32:
1721af3f 406 *(unsigned int *) reloc_addr =
ffeac417
UD
407 value + reloc->r_addend - (Elf64_Addr) reloc_addr;
408 break;
409 case R_390_PC16DBL:
410 case R_390_PLT16DBL:
411 *(unsigned short *) reloc_addr = (unsigned short)
412 ((short) (value + reloc->r_addend - (Elf64_Addr) reloc_addr) >> 1);
847b055c 413 break;
ffeac417 414 case R_390_PC16:
1721af3f 415 *(unsigned short *) reloc_addr =
ffeac417 416 value + reloc->r_addend - (Elf64_Addr) reloc_addr;
847b055c 417 break;
961d4a2e
UD
418 case R_390_NONE:
419 break;
ffeac417
UD
420#endif
421#if !defined(RTLD_BOOTSTRAP) || defined(_NDEBUG)
847b055c 422 default:
ffeac417
UD
423 /* We add these checks in the version to relocate ld.so only
424 if we are still debugging. */
1721af3f 425 _dl_reloc_bad_type (map, r_type, 0);
847b055c 426 break;
ffeac417 427#endif
847b055c
AJ
428 }
429 }
1721af3f
UD
430}
431
432static inline void
7c69dc8b
UD
433elf_machine_rela_relative (Elf64_Addr l_addr, const Elf64_Rela *reloc,
434 Elf64_Addr *const reloc_addr)
1721af3f
UD
435{
436 *reloc_addr = l_addr + reloc->r_addend;
437}
847b055c
AJ
438
439static inline void
440elf_machine_lazy_rel (struct link_map *map,
ffeac417 441 Elf64_Addr l_addr, const Elf64_Rela *reloc)
847b055c 442{
ffeac417 443 Elf64_Addr *const reloc_addr = (void *) (l_addr + reloc->r_offset);
1721af3f 444 const unsigned int r_type = ELF32_R_TYPE (reloc->r_info);
847b055c 445 /* Check for unexpected PLT reloc type. */
1721af3f 446 if (__builtin_expect (r_type == R_390_JMP_SLOT, 1))
32e6df36
UD
447 {
448 if (__builtin_expect (map->l_mach.plt, 0) == 0)
449 *reloc_addr += l_addr;
450 else
451 *reloc_addr =
452 map->l_mach.plt
453 + (((Elf64_Addr) reloc_addr) - map->l_mach.gotplt) * 4;
454 }
847b055c 455 else
1721af3f 456 _dl_reloc_bad_type (map, r_type, 1);
847b055c
AJ
457}
458
459#endif /* RESOLVE */