Update.
[glibc.git] / sysdeps / s390 / s390-64 / dl-machine.h
blobfcbd02ed1011ff8cfa9c3ed19bf86cc5a6c015e7
1 /* Machine-dependent ELF dynamic relocation inline functions.
2 64 bit S/390 Version.
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
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 (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;
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 _dl_runtime_resolve:\n\
143 # save registers\n\
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\
149 # load args saved by PLT\n\
150 lmg 2,3,208(15)\n\
151 brasl 14,fixup # call fixup
152 lgr 1,2 # function addr returned in r2\n\
153 # restore registers\n\
154 aghi 15,160\n\
155 lg 14,96(15)\n\
156 lmg 2,5,64(15)\n\
157 br 1\n\
158 .size _dl_runtime_resolve, .-_dl_runtime_resolve\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\
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\
170 # load args saved by PLT\n\
171 lmg 2,3,208(15)\n\
172 # load return address as third parameter\n\
173 lgr 4,14\n\
174 brasl 14,profile_fixup # call fixup\n\
175 lgr 1,2 # function addr returned in r2\n\
176 # restore registers\n\
177 aghi 15,160\n\
178 lg 14,96(15)\n\
179 lmg 2,5,64(15)\n\
180 br 1\n\
181 .size _dl_runtime_profile, .-_dl_runtime_profile\n\
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\
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\
200 # load args saved by PLT\n\
201 lmg 2,3,208(15)\n\
202 # load return address as third parameter\n\
203 lgr 4,14\n\
204 brasl 14,profile_fixup # call fixup\n\
205 lgr 1,2 # function addr returned in r2\n\
206 # restore registers\n\
207 aghi 15,160\n\
208 lg 14,96(15)\n\
209 lmg 2,5,64(15)\n\
210 br 1\n\
211 .size _dl_runtime_resolve, .-_dl_runtime_resolve\n\
212 .size _dl_runtime_profile, .-_dl_runtime_profile\n\
214 #endif
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\
221 .text\n\
222 .align 4\n\
223 .globl _start\n\
224 .globl _dl_start_user\n\
225 _start:\n\
226 lgr %r2,%r15\n\
227 # Alloc stack frame\n\
228 aghi %r15,-160\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\
233 _dl_start_user:\n\
234 # Save the user entry point address in %r8.\n\
235 lgr %r8,%r2\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\
241 stg %r15, 0(%r1)\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\
248 lg %r0,160(%r15)\n\
249 # Subtract _dl_skip_args from it.\n\
250 sgr %r0,%r1\n\
251 # Adjust the stack pointer to skip _dl_skip_args words.\n\
252 sllg %r1,%r1,3\n\
253 agr %r15,%r1\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\
257 stg %r0,160(%r15)\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_global@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_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\
278 aghi %r15,160\n\
279 # Jump to the user's entry point (saved in %r8).\n\
280 br %r8\n\
283 #ifndef RTLD_START_SPECIAL_INIT
284 #define RTLD_START_SPECIAL_INIT /* nothing */
285 #endif
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
302 _dl_sysdep_start. */
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,
324 Elf64_Addr value)
326 return value;
329 #endif /* !dl_machine_h */
331 #ifdef RESOLVE
333 /* Perform the relocation specified by RELOC and SYM (which is fully resolved).
334 MAP is the object containing the reloc. */
336 static inline void
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))
347 return;
348 #endif
349 else
351 const Elf64_Sym *const refsym = sym;
352 Elf64_Addr value = RESOLVE (&sym, version, r_type);
353 if (sym)
354 value += sym->st_value;
356 switch (r_type)
358 case R_390_GLOB_DAT:
359 case R_390_JMP_SLOT:
360 *reloc_addr = value + reloc->r_addend;
361 break;
362 #ifndef RTLD_BOOTSTRAP
363 case R_390_COPY:
364 if (sym == NULL)
365 /* This can happen in trace mode if an object could not be
366 found. */
367 break;
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)))
372 const char *strtab;
374 strtab = (const char *) D_PTR (map,l_info[DT_STRTAB]);
375 _dl_error_printf ("\
376 %s: Symbol `%s' has different size in shared object, consider re-linking\n",
377 _dl_argv[0] ?: "<program name unknown>",
378 strtab + refsym->st_name);
380 memcpy (reloc_addr, (void *) value, MIN (sym->st_size,
381 refsym->st_size));
382 break;
383 case R_390_64:
384 *reloc_addr = value + reloc->r_addend;
385 break;
386 case R_390_32:
387 *(unsigned int *) reloc_addr = value + reloc->r_addend;
388 break;
389 case R_390_16:
390 *(unsigned short *) reloc_addr = value + reloc->r_addend;
391 break;
392 case R_390_8:
393 *(char *) reloc_addr = value + reloc->r_addend;
394 break;
395 case R_390_PC64:
396 *reloc_addr = value +reloc->r_addend - (Elf64_Addr) reloc_addr;
397 break;
398 case R_390_PC32DBL:
399 case R_390_PLT32DBL:
400 *(unsigned int *) reloc_addr = (unsigned int)
401 ((int) (value + reloc->r_addend - (Elf64_Addr) reloc_addr) >> 1);
402 break;
403 case R_390_PC32:
404 *(unsigned int *) reloc_addr =
405 value + reloc->r_addend - (Elf64_Addr) reloc_addr;
406 break;
407 case R_390_PC16DBL:
408 case R_390_PLT16DBL:
409 *(unsigned short *) reloc_addr = (unsigned short)
410 ((short) (value + reloc->r_addend - (Elf64_Addr) reloc_addr) >> 1);
411 break;
412 case R_390_PC16:
413 *(unsigned short *) reloc_addr =
414 value + reloc->r_addend - (Elf64_Addr) reloc_addr;
415 break;
416 case R_390_NONE:
417 break;
418 #endif
419 #if !defined(RTLD_BOOTSTRAP) || defined(_NDEBUG)
420 default:
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);
424 break;
425 #endif
430 static inline void
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;
437 static inline void
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;
448 else
449 *reloc_addr =
450 map->l_mach.plt
451 + (((Elf64_Addr) reloc_addr) - map->l_mach.gotplt) * 4;
453 else
454 _dl_reloc_bad_type (map, r_type, 1);
457 #endif /* RESOLVE */