]>
git.ipfire.org Git - thirdparty/glibc.git/blob - sysdeps/s390/s390-64/dl-machine.h
1 /* Machine-dependent ELF dynamic relocation inline functions.
3 Copyright (C) 2001, 2002, 2003 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
25 #define ELF_MACHINE_NAME "s390x"
27 #include <sys/param.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. */
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
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" );
57 /* Return the run-time load address of the shared object. */
58 static inline Elf64_Addr
59 elf_machine_load_address (void)
63 asm( " larl %0,_dl_start\n"
64 " larl 1,_GLOBAL_OFFSET_TABLE_\n"
65 " lghi 2,_dl_start@GOT\n"
67 : "=&d" (addr
) : : "1", "2" );
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]. */
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. */
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 (GL(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
;
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
;
122 /* This code is used in dl-runtime.c to call the `fixup' function
123 and then redirect to the address it returns. */
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.
136 #define ELF_MACHINE_RUNTIME_TRAMPOLINE \
139 .globl _dl_runtime_resolve\n\
140 .type _dl_runtime_resolve, @function\n\
143 _dl_runtime_resolve:\n\
149 " CFI_ADJUST_CFA_OFFSET(160)"\n\
151 # load args saved by PLT\n\
153 brasl 14,fixup # call fixup\n\
154 lgr 1,2 # function addr returned in r2\n\
155 # restore registers\n\
157 " CFI_ADJUST_CFA_OFFSET(-160)" \n\
162 .size _dl_runtime_resolve, .-_dl_runtime_resolve\n\
164 .globl _dl_runtime_profile\n\
165 .type _dl_runtime_profile, @function\n\
168 _dl_runtime_profile:\n\
174 " CFI_ADJUST_CFA_OFFSET(160)"\n\
176 # load args saved by PLT\n\
178 # load return address as third parameter\n\
180 brasl 14,profile_fixup # call fixup\n\
181 lgr 1,2 # function addr returned in r2\n\
182 # restore registers\n\
184 " CFI_ADJUST_CFA_OFFSET(-160)" \n\
189 .size _dl_runtime_profile, .-_dl_runtime_profile\n\
192 #define ELF_MACHINE_RUNTIME_TRAMPOLINE \
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\
201 _dl_runtime_resolve:\n\
202 _dl_runtime_profile:\n\
208 " CFI_ADJUST_CFA_OFFSET(160)"\n\
210 # load args saved by PLT\n\
212 # load return address as third parameter\n\
214 brasl 14,profile_fixup # call fixup\n\
215 lgr 1,2 # function addr returned in r2\n\
216 # restore registers\n\
218 " CFI_ADJUST_CFA_OFFSET(-160)" \n\
223 .size _dl_runtime_resolve, .-_dl_runtime_resolve\n\
224 .size _dl_runtime_profile, .-_dl_runtime_profile\n\
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\
236 .globl _dl_start_user\n\
239 # Alloc stack frame\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\
246 # Save the user entry point address in %r8.\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\
257 # Subtract _dl_skip_args from it.\n\
259 # Adjust the stack pointer to skip _dl_skip_args words.\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\
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\
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\
287 # Jump to the user's entry point (saved in %r8).\n\
291 #ifndef RTLD_START_SPECIAL_INIT
292 #define RTLD_START_SPECIAL_INIT /* nothing */
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
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. */
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))
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))
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
320 #define DL_PLATFORM_INIT dl_platform_init ()
322 static inline void __attribute__ ((unused
))
323 dl_platform_init (void)
325 if (GL(dl_platform
) != NULL
&& *GL(dl_platform
) == '\0')
326 /* Avoid an empty string which would disturb us. */
327 GL(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
,
346 #endif /* !dl_machine_h */
350 /* Perform the relocation specified by RELOC and SYM (which is fully resolved).
351 MAP is the object containing the reloc. */
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. */
372 weak_extern (GL(dl_rtld_map
));
374 if (map
!= &GL(dl_rtld_map
)) /* Already done in rtld itself. */
376 *reloc_addr
= map
->l_addr
+ reloc
->r_addend
;
380 if (__builtin_expect (r_type
== R_390_NONE
, 0))
384 #ifndef RESOLVE_CONFLICT_FIND_MAP
385 const Elf64_Sym
*const refsym
= sym
;
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
;
391 Elf64_Addr value
= RESOLVE (&sym
, version
, r_type
);
393 # ifndef RTLD_BOOTSTRAP
396 value
+= sym
->st_value
;
397 #endif /* use TLS and !RTLD_BOOTSTRAP */
403 *reloc_addr
= value
+ reloc
->r_addend
;
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
412 XXX If this relocation is necessary move before RESOLVE
416 /* Get the information from the link map returned by the
419 *reloc_addr
= sym_map
->l_tls_modid
;
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. */
427 *reloc_addr
= sym
->st_value
+ reloc
->r_addend
;
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
;
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
440 CHECK_STATIC_TLS (map
, sym_map
);
441 *reloc_addr
= (sym
->st_value
+ reloc
->r_addend
442 - sym_map
->l_tls_offset
);
448 #ifndef RTLD_BOOTSTRAP
449 # ifndef RESOLVE_CONFLICT_FIND_MAP
450 /* Not needed for dl-conflict.c. */
453 /* This can happen in trace mode if an object could not be
456 if (__builtin_expect (sym
->st_size
> refsym
->st_size
, 0)
457 || (__builtin_expect (sym
->st_size
< refsym
->st_size
, 0)
458 && __builtin_expect (GL(dl_verbose
), 0)))
462 strtab
= (const char *) D_PTR (map
,l_info
[DT_STRTAB
]);
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
));
473 *reloc_addr
= value
+ reloc
->r_addend
;
476 *(unsigned int *) reloc_addr
= value
+ reloc
->r_addend
;
479 *(unsigned short *) reloc_addr
= value
+ reloc
->r_addend
;
482 *(char *) reloc_addr
= value
+ reloc
->r_addend
;
484 # ifndef RESOLVE_CONFLICT_FIND_MAP
486 *reloc_addr
= value
+reloc
->r_addend
- (Elf64_Addr
) reloc_addr
;
490 *(unsigned int *) reloc_addr
= (unsigned int)
491 ((int) (value
+ reloc
->r_addend
- (Elf64_Addr
) reloc_addr
) >> 1);
494 *(unsigned int *) reloc_addr
=
495 value
+ reloc
->r_addend
- (Elf64_Addr
) reloc_addr
;
499 *(unsigned short *) reloc_addr
= (unsigned short)
500 ((short) (value
+ reloc
->r_addend
- (Elf64_Addr
) reloc_addr
) >> 1);
503 *(unsigned short *) reloc_addr
=
504 value
+ reloc
->r_addend
- (Elf64_Addr
) reloc_addr
;
510 #if !defined(RTLD_BOOTSTRAP) || defined(_NDEBUG)
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);
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
;
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
;
543 + (((Elf64_Addr
) reloc_addr
) - map
->l_mach
.gotplt
) * 4;
546 _dl_reloc_bad_type (map
, r_type
, 1);