1 /* Machine-dependent ELF dynamic relocation inline functions.
3 Copyright (C) 2001, 2002 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\
142 _dl_runtime_resolve:\n\
149 # load args saved by PLT\n\
151 brasl 14,fixup # call fixup
152 lgr 1,2 # function addr returned in r2\n\
153 # restore registers\n\
158 .size _dl_runtime_resolve, .-_dl_runtime_resolve\n\
160 .globl _dl_runtime_profile\n\
161 .type _dl_runtime_profile, @function\n\
163 _dl_runtime_profile:\n\
170 # load args saved by PLT\n\
172 # load return address as third parameter\n\
174 brasl 14,profile_fixup # call fixup\n\
175 lgr 1,2 # function addr returned in r2\n\
176 # restore registers\n\
181 .size _dl_runtime_profile, .-_dl_runtime_profile\n\
184 #define ELF_MACHINE_RUNTIME_TRAMPOLINE \
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\
192 _dl_runtime_resolve:\n\
193 _dl_runtime_profile:\n\
200 # load args saved by PLT\n\
202 # load return address as third parameter\n\
204 brasl 14,profile_fixup # call fixup\n\
205 lgr 1,2 # function addr returned in r2\n\
206 # restore registers\n\
211 .size _dl_runtime_resolve, .-_dl_runtime_resolve\n\
212 .size _dl_runtime_profile, .-_dl_runtime_profile\n\
216 /* Initial entry point code for the dynamic linker.
217 The C function `_dl_start' is the real entry point;
218 its return value is the user program's entry point. */
220 #define RTLD_START asm ("\n\
224 .globl _dl_start_user\n\
227 # Alloc stack frame\n\
229 # Set the back chain to zero\n\
230 xc 0(8,%r15),0(%r15)\n\
231 # Call _dl_start with %r2 pointing to arg on stack\n\
232 brasl %r14,_dl_start # call _dl_start\n\
234 # Save the user entry point address in %r8.\n\
236 # Point %r12 at the GOT.\n\
237 larl %r12,_GLOBAL_OFFSET_TABLE_\n\
238 # Store the highest stack address\n\
239 lghi %r1,__libc_stack_end@GOT
240 lg %r1,0(%r1,%r12)\n\
242 # See if we were run as a command with the executable file\n\
243 # name as an extra leading argument.\n\
244 lghi %r1,_dl_skip_args@GOT
245 lg %r1,0(%r1,%r12)\n\
246 lgf %r1,0(%r1) # load _dl_skip_args\n\
247 # Get the original argument count.\n\
249 # Subtract _dl_skip_args from it.\n\
251 # Adjust the stack pointer to skip _dl_skip_args words.\n\
254 # Set the back chain to zero again\n\
255 xc 0(8,%r15),0(%r15)\n\
256 # Store back the modified argument count.\n\
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\
265 lghi %r2,_rtld_local@GOT
266 lg %r2,0(%r2,%r12)\n\
272 la %r5,176(%r5,%r15)\n\
273 brasl %r14,_dl_init_internal@PLT\n
274 # Pass our finalizer function to the user in %r14, as per ELF ABI.\n\
275 lghi %r14,_dl_fini@GOT
276 lg %r14,0(%r14,%r12)\n\
277 # Free stack frame\n\
279 # Jump to the user's entry point (saved in %r8).\n\
283 #ifndef RTLD_START_SPECIAL_INIT
284 #define RTLD_START_SPECIAL_INIT /* nothing */
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))
295 /* A reloc type used for ld.so cmdline arg lookups to reject PLT entries. */
296 #define ELF_MACHINE_JMP_SLOT R_390_JMP_SLOT
298 /* The 64 bit S/390 never uses Elf64_Rel relocations. */
299 #define ELF_MACHINE_NO_REL 1
301 /* We define an initialization functions. This is called very early in
303 #define DL_PLATFORM_INIT dl_platform_init ()
305 static inline void __attribute__ ((unused))
306 dl_platform_init (void)
308 if (GL(dl_platform) != NULL && *GL(dl_platform) == '\0')
309 /* Avoid an empty string which would disturb us. */
310 GL(dl_platform) = NULL;
313 static inline Elf64_Addr
314 elf_machine_fixup_plt (struct link_map *map, lookup_t t,
315 const Elf64_Rela *reloc,
316 Elf64_Addr *reloc_addr, Elf64_Addr value)
318 return *reloc_addr = value;
321 /* Return the final value of a plt relocation. */
322 static inline Elf64_Addr
323 elf_machine_plt_value (struct link_map *map, const Elf64_Rela *reloc,
329 #endif /* !dl_machine_h */
333 /* Perform the relocation specified by RELOC and SYM (which is fully resolved).
334 MAP is the object containing the reloc. */
337 elf_machine_rela (struct link_map *map, const Elf64_Rela *reloc,
338 const Elf64_Sym *sym, const struct r_found_version *version,
339 Elf64_Addr *const reloc_addr)
341 const unsigned int r_type = ELF64_R_TYPE (reloc->r_info);
343 if (__builtin_expect (r_type == R_390_RELATIVE, 0))
344 *reloc_addr = map->l_addr + reloc->r_addend;
345 #ifndef RTLD_BOOTSTRAP
346 else if (__builtin_expect (r_type == R_390_NONE, 0))
351 const Elf64_Sym *const refsym = sym;
352 Elf64_Addr value = RESOLVE (&sym, version, r_type);
354 value += sym->st_value;
360 *reloc_addr = value + reloc->r_addend;
362 #ifndef RTLD_BOOTSTRAP
365 /* This can happen in trace mode if an object could not be
368 if (__builtin_expect (sym->st_size > refsym->st_size, 0)
369 || (__builtin_expect (sym->st_size < refsym->st_size, 0)
370 && __builtin_expect (GL(dl_verbose), 0)))
374 strtab = (const char *) D_PTR (map,l_info[DT_STRTAB]);
376 %s: Symbol `%s' has different size in shared object, consider re-linking\n",
377 rtld_progname ?: "<program name unknown>",
378 strtab + refsym->st_name);
380 memcpy (reloc_addr, (void *) value, MIN (sym->st_size,
384 *reloc_addr = value + reloc->r_addend;
387 *(unsigned int *) reloc_addr = value + reloc->r_addend;
390 *(unsigned short *) reloc_addr = value + reloc->r_addend;
393 *(char *) reloc_addr = value + reloc->r_addend;
396 *reloc_addr = value +reloc->r_addend - (Elf64_Addr) reloc_addr;
400 *(unsigned int *) reloc_addr = (unsigned int)
401 ((int) (value + reloc->r_addend - (Elf64_Addr) reloc_addr) >> 1);
404 *(unsigned int *) reloc_addr =
405 value + reloc->r_addend - (Elf64_Addr) reloc_addr;
409 *(unsigned short *) reloc_addr = (unsigned short)
410 ((short) (value + reloc->r_addend - (Elf64_Addr) reloc_addr) >> 1);
413 *(unsigned short *) reloc_addr =
414 value + reloc->r_addend - (Elf64_Addr) reloc_addr;
419 #if !defined(RTLD_BOOTSTRAP) || defined(_NDEBUG)
421 /* We add these checks in the version to relocate ld.so only
422 if we are still debugging. */
423 _dl_reloc_bad_type (map, r_type, 0);
431 elf_machine_rela_relative (Elf64_Addr l_addr, const Elf64_Rela *reloc,
432 Elf64_Addr *const reloc_addr)
434 *reloc_addr = l_addr + reloc->r_addend;
438 elf_machine_lazy_rel (struct link_map *map,
439 Elf64_Addr l_addr, const Elf64_Rela *reloc)
441 Elf64_Addr *const reloc_addr = (void *) (l_addr + reloc->r_offset);
442 const unsigned int r_type = ELF32_R_TYPE (reloc->r_info);
443 /* Check for unexpected PLT reloc type. */
444 if (__builtin_expect (r_type == R_390_JMP_SLOT, 1))
446 if (__builtin_expect (map->l_mach.plt, 0) == 0)
447 *reloc_addr += l_addr;
451 + (((Elf64_Addr) reloc_addr) - map->l_mach.gotplt) * 4;
454 _dl_reloc_bad_type (map, r_type, 1);