1 /* Machine-dependent ELF dynamic relocation inline functions.
3 Copyright (C) 2001 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 Library General Public License as
9 published by the Free Software Foundation; either version 2 of the
10 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 Library General Public License for more details.
17 You should have received a copy of the GNU Library General Public
18 License along with the GNU C Library; see the file COPYING.LIB. If
19 not, write to the Free Software Foundation, Inc.,
20 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
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 got[1] = (Elf64_Addr) l; /* Identify this shared object. */
90 /* The got[2] entry contains the address of a function which gets
91 called to get the address of a so far unresolved function and
92 jump to it. The profiling extension of the dynamic linker allows
93 to intercept the calls to collect information. In this case we
94 don't store the address in the GOT so that all future calls also
95 end in this function. */
96 if (__builtin_expect (profile, 0))
98 got[2] = (Elf64_Addr) &_dl_runtime_profile;
100 if (_dl_name_match_p (_dl_profile, l))
101 /* This is the object we are looking for. Say that we really
102 want profiling and the timers are started. */
106 /* This function will get called to fix up the GOT entry indicated by
107 the offset on the stack, and then jump to the resolved address. */
108 got[2] = (Elf64_Addr) &_dl_runtime_resolve;
114 /* This code is used in dl-runtime.c to call the `fixup' function
115 and then redirect to the address it returns. */
118 Arguments are in register.
119 r2 - r7 holds the original parameters for the function call, fixup
120 and trampoline code use r0-r5 and r14-15. For the correct function
121 call r2-r5 and r14-15 must be restored.
122 Arguments from the PLT are stored at 48(r15) and 56(r15)
123 and must be moved to r2 and r3 for the fixup call (see elf32-s390.c
124 in the binutils for the PLT code).
125 Fixup function address in r2.
128 #define ELF_MACHINE_RUNTIME_TRAMPOLINE \
131 .globl _dl_runtime_resolve\n\
132 .type _dl_runtime_resolve, @function\n\
134 _dl_runtime_resolve:\n\
141 # load args saved by PLT\n\
143 brasl 14,fixup # call fixup
144 lgr 1,2 # function addr returned in r2\n\
145 # restore registers\n\
150 .size _dl_runtime_resolve, .-_dl_runtime_resolve\n\
152 .globl _dl_runtime_profile\n\
153 .type _dl_runtime_profile, @function\n\
155 _dl_runtime_profile:\n\
162 # load args saved by PLT\n\
164 # load return address as third parameter\n\
166 brasl 14,profile_fixup # call fixup\n\
167 lgr 1,2 # function addr returned in r2\n\
168 # restore registers\n\
173 .size _dl_runtime_profile, .-_dl_runtime_profile\n\
176 #define ELF_MACHINE_RUNTIME_TRAMPOLINE \
179 .globl _dl_runtime_resolve\n\
180 .globl _dl_runtime_profile\n\
181 .type _dl_runtime_resolve, @function\n\
182 .type _dl_runtime_profile, @function\n\
184 _dl_runtime_resolve:\n\
185 _dl_runtime_profile:\n\
192 # load args saved by PLT\n\
194 # load return address as third parameter\n\
196 brasl 14,profile_fixup # call fixup\n\
197 lgr 1,2 # function addr returned in r2\n\
198 # restore registers\n\
203 .size _dl_runtime_resolve, .-_dl_runtime_resolve\n\
204 .size _dl_runtime_profile, .-_dl_runtime_profile\n\
208 /* Initial entry point code for the dynamic linker.
209 The C function `_dl_start' is the real entry point;
210 its return value is the user program's entry point. */
212 #define RTLD_START asm ("\n\
216 .globl _dl_start_user\n\
219 # Alloc stack frame\n\
221 # Set the back chain to zero\n\
222 xc 0(8,%r15),0(%r15)\n\
223 # Call _dl_start with %r2 pointing to arg on stack\n\
224 brasl %r14,_dl_start # call _dl_start\n\
226 # Save the user entry point address in %r8.\n\
228 # Point %r12 at the GOT.\n\
229 larl %r12,_GLOBAL_OFFSET_TABLE_\n\
230 # Store the highest stack address\n\
231 lghi %r1,__libc_stack_end@GOT
232 lg %r1,0(%r1,%r12)\n\
234 # See if we were run as a command with the executable file\n\
235 # name as an extra leading argument.\n\
236 lghi %r1,_dl_skip_args@GOT
237 lg %r1,0(%r1,%r12)\n\
238 lgf %r1,0(%r1) # load _dl_skip_args\n\
239 # Get the original argument count.\n\
241 # Subtract _dl_skip_args from it.\n\
243 # Adjust the stack pointer to skip _dl_skip_args words.\n\
246 # Set the back chain to zero again\n\
247 xc 0(8,%r15),0(%r15)\n\
248 # Store back the modified argument count.\n\
250 # The special initializer gets called with the stack just\n\
251 # as the application's entry point will see it; it can\n\
252 # switch stacks if it moves these contents over.\n\
253 " RTLD_START_SPECIAL_INIT "\n\
254 # Call the function to run the initializers.\n\
255 # Load the parameters:\n\
256 # (%r2, %r3, %r4, %r5) = (_dl_loaded, argc, argv, envp)\n\
257 lghi %r2,_dl_loaded@GOT
258 lg %r2,0(%r2,%r12)\n\
264 la %r5,176(%r5,%r15)\n\
265 brasl %r14,_dl_init@PLT\n
266 # Pass our finalizer function to the user in %r14, as per ELF ABI.\n\
267 lghi %r14,_dl_fini@GOT
268 lg %r14,0(%r14,%r12)\n\
269 # Free stack frame\n\
271 # Jump to the user's entry point (saved in %r8).\n\
275 #ifndef RTLD_START_SPECIAL_INIT
276 #define RTLD_START_SPECIAL_INIT /* nothing */
279 /* Nonzero iff TYPE should not be allowed to resolve to one of
280 the main executable's symbols, as for a COPY reloc. */
281 #define elf_machine_lookup_noexec_p(type) ((type) == R_390_COPY)
283 /* Nonzero iff TYPE describes relocation of a PLT entry, so
284 PLT entries should not be allowed to define the value. */
285 #define elf_machine_lookup_noplt_p(type) ((type) == R_390_JMP_SLOT)
287 /* A reloc type used for ld.so cmdline arg lookups to reject PLT entries. */
288 #define ELF_MACHINE_JMP_SLOT R_390_JMP_SLOT
290 /* The 64 bit S/390 never uses Elf64_Rel relocations. */
291 #define ELF_MACHINE_NO_REL 1
293 /* We define an initialization functions. This is called very early in
295 #define DL_PLATFORM_INIT dl_platform_init ()
297 extern const char *_dl_platform;
299 static inline void __attribute__ ((unused))
300 dl_platform_init (void)
302 if (_dl_platform != NULL && *_dl_platform == '\0')
303 /* Avoid an empty string which would disturb us. */
307 static inline Elf64_Addr
308 elf_machine_fixup_plt (struct link_map *map, lookup_t t,
309 const Elf64_Rela *reloc,
310 Elf64_Addr *reloc_addr, Elf64_Addr value)
312 return *reloc_addr = value;
315 /* Return the final value of a plt relocation. */
316 static inline Elf64_Addr
317 elf_machine_plt_value (struct link_map *map, const Elf64_Rela *reloc,
323 #endif /* !dl_machine_h */
327 /* Perform the relocation specified by RELOC and SYM (which is fully resolved).
328 MAP is the object containing the reloc. */
331 elf_machine_rela (struct link_map *map, const Elf64_Rela *reloc,
332 const Elf64_Sym *sym, const struct r_found_version *version,
333 Elf64_Addr *const reloc_addr)
335 if (ELF64_R_TYPE (reloc->r_info) == R_390_RELATIVE) {
336 #ifndef RTLD_BOOTSTRAP
337 weak_extern (_dl_rtld_map);
338 if (map != &_dl_rtld_map) /* Already done in rtld itself. */
340 *reloc_addr = map->l_addr + reloc->r_addend;
342 else if (ELF64_R_TYPE (reloc->r_info) != R_390_NONE)
344 const Elf64_Sym *const refsym = sym;
345 Elf64_Addr value = RESOLVE (&sym, version, ELF64_R_TYPE (reloc->r_info));
347 value += sym->st_value;
349 switch (ELF64_R_TYPE (reloc->r_info))
353 *reloc_addr = value + reloc->r_addend;
355 #ifndef RTLD_BOOTSTRAP
358 /* This can happen in trace mode if an object could not be
361 if (__builtin_expect (sym->st_size > refsym->st_size, 0)
362 || (__builtin_expect (sym->st_size < refsym->st_size, 0)
363 && __builtin_expect (_dl_verbose, 0)))
367 strtab = (const char *) D_PTR (map,l_info[DT_STRTAB]);
369 %s: Symbol `%s' has different size in shared object, consider re-linking\n",
370 _dl_argv[0] ?: "<program name unknown>",
371 strtab + refsym->st_name);
373 memcpy (reloc_addr, (void *) value, MIN (sym->st_size,
377 *reloc_addr = value + reloc->r_addend;
380 *(unsigned int *) reloc_addr = value + reloc->r_addend;
383 *(unsigned short *) reloc_addr = value + reloc->r_addend;
386 *(char *) reloc_addr = value + reloc->r_addend;
389 *reloc_addr = value +reloc->r_addend - (Elf64_Addr) reloc_addr;
393 *(unsigned int *) reloc_addr = (unsigned int)
394 ((int) (value + reloc->r_addend - (Elf64_Addr) reloc_addr) >> 1);
397 *(unsigned int *) reloc_addr =
398 value + reloc->r_addend - (Elf64_Addr) reloc_addr;
402 *(unsigned short *) reloc_addr = (unsigned short)
403 ((short) (value + reloc->r_addend - (Elf64_Addr) reloc_addr) >> 1);
406 *(unsigned short *) reloc_addr =
407 value + reloc->r_addend - (Elf64_Addr) reloc_addr;
410 #if !defined(RTLD_BOOTSTRAP) || defined(_NDEBUG)
412 /* We add these checks in the version to relocate ld.so only
413 if we are still debugging. */
414 _dl_reloc_bad_type (map, ELFW(R_TYPE) (reloc->r_info), 0);
422 elf_machine_lazy_rel (struct link_map *map,
423 Elf64_Addr l_addr, const Elf64_Rela *reloc)
425 Elf64_Addr *const reloc_addr = (void *) (l_addr + reloc->r_offset);
426 /* Check for unexpected PLT reloc type. */
427 if (__builtin_expect (ELF64_R_TYPE (reloc->r_info), R_390_JMP_SLOT)
429 *reloc_addr += l_addr;
431 _dl_reloc_bad_type (map, ELFW(R_TYPE) (reloc->r_info), 1);