2.9
[glibc/nacl-glibc.git] / sysdeps / s390 / s390-64 / dl-machine.h
blobfa893befdf5c9e571de4b5379cd97efed50a4c8a
1 /* Machine-dependent ELF dynamic relocation inline functions.
2 64 bit S/390 Version.
3 Copyright (C) 2001-2005, 2006 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 (GLRO(dl_profile) != NULL
109 && _dl_name_match_p (GLRO(dl_profile), l))
110 /* This is the object we are looking for. Say that we really
111 want profiling and the timers are started. */
112 GL(dl_profile_map) = l;
114 else
115 /* This function will get called to fix up the GOT entry indicated by
116 the offset on the stack, and then jump to the resolved address. */
117 got[2] = (Elf64_Addr) &_dl_runtime_resolve;
120 return lazy;
123 /* Initial entry point code for the dynamic linker.
124 The C function `_dl_start' is the real entry point;
125 its return value is the user program's entry point. */
127 #define RTLD_START asm ("\n\
128 .text\n\
129 .align 4\n\
130 .globl _start\n\
131 .globl _dl_start_user\n\
132 _start:\n\
133 lgr %r2,%r15\n\
134 # Alloc stack frame\n\
135 aghi %r15,-160\n\
136 # Set the back chain to zero\n\
137 xc 0(8,%r15),0(%r15)\n\
138 # Call _dl_start with %r2 pointing to arg on stack\n\
139 brasl %r14,_dl_start # call _dl_start\n\
140 _dl_start_user:\n\
141 # Save the user entry point address in %r8.\n\
142 lgr %r8,%r2\n\
143 # Point %r12 at the GOT.\n\
144 larl %r12,_GLOBAL_OFFSET_TABLE_\n\
145 # See if we were run as a command with the executable file\n\
146 # name as an extra leading argument.\n\
147 lghi %r1,_dl_skip_args@GOT\n\
148 lg %r1,0(%r1,%r12)\n\
149 lgf %r1,0(%r1) # load _dl_skip_args\n\
150 # Get the original argument count.\n\
151 lg %r0,160(%r15)\n\
152 # Subtract _dl_skip_args from it.\n\
153 sgr %r0,%r1\n\
154 # Adjust the stack pointer to skip _dl_skip_args words.\n\
155 sllg %r1,%r1,3\n\
156 agr %r15,%r1\n\
157 # Set the back chain to zero again\n\
158 xc 0(8,%r15),0(%r15)\n\
159 # Store back the modified argument count.\n\
160 stg %r0,160(%r15)\n\
161 # The special initializer gets called with the stack just\n\
162 # as the application's entry point will see it; it can\n\
163 # switch stacks if it moves these contents over.\n\
164 " RTLD_START_SPECIAL_INIT "\n\
165 # Call the function to run the initializers.\n\
166 # Load the parameters:\n\
167 # (%r2, %r3, %r4, %r5) = (_dl_loaded, argc, argv, envp)\n\
168 lghi %r2,_rtld_local@GOT\n\
169 lg %r2,0(%r2,%r12)\n\
170 lg %r2,0(%r2)\n\
171 lg %r3,160(%r15)\n\
172 la %r4,168(%r15)\n\
173 lgr %r5,%r3\n\
174 sllg %r5,%r5,3\n\
175 la %r5,176(%r5,%r15)\n\
176 brasl %r14,_dl_init_internal@PLT\n\
177 # Pass our finalizer function to the user in %r14, as per ELF ABI.\n\
178 lghi %r14,_dl_fini@GOT\n\
179 lg %r14,0(%r14,%r12)\n\
180 # Free stack frame\n\
181 aghi %r15,160\n\
182 # Jump to the user's entry point (saved in %r8).\n\
183 br %r8\n\
186 #ifndef RTLD_START_SPECIAL_INIT
187 #define RTLD_START_SPECIAL_INIT /* nothing */
188 #endif
190 /* ELF_RTYPE_CLASS_PLT iff TYPE describes relocation of a PLT entry or
191 TLS variable, so undefined references should not be allowed to
192 define the value.
193 ELF_RTYPE_CLASS_NOCOPY iff TYPE should not be allowed to resolve to one
194 of the main executable's symbols, as for a COPY reloc. */
195 #define elf_machine_type_class(type) \
196 ((((type) == R_390_JMP_SLOT || (type) == R_390_TLS_DTPMOD \
197 || (type) == R_390_TLS_DTPOFF || (type) == R_390_TLS_TPOFF) \
198 * ELF_RTYPE_CLASS_PLT) \
199 | (((type) == R_390_COPY) * ELF_RTYPE_CLASS_COPY))
201 /* A reloc type used for ld.so cmdline arg lookups to reject PLT entries. */
202 #define ELF_MACHINE_JMP_SLOT R_390_JMP_SLOT
204 /* The 64 bit S/390 never uses Elf64_Rel relocations. */
205 #define ELF_MACHINE_NO_REL 1
207 /* We define an initialization functions. This is called very early in
208 _dl_sysdep_start. */
209 #define DL_PLATFORM_INIT dl_platform_init ()
211 static inline void __attribute__ ((unused))
212 dl_platform_init (void)
214 if (GLRO(dl_platform) != NULL && *GLRO(dl_platform) == '\0')
215 /* Avoid an empty string which would disturb us. */
216 GLRO(dl_platform) = NULL;
219 static inline Elf64_Addr
220 elf_machine_fixup_plt (struct link_map *map, lookup_t t,
221 const Elf64_Rela *reloc,
222 Elf64_Addr *reloc_addr, Elf64_Addr value)
224 return *reloc_addr = value;
227 /* Return the final value of a plt relocation. */
228 static inline Elf64_Addr
229 elf_machine_plt_value (struct link_map *map, const Elf64_Rela *reloc,
230 Elf64_Addr value)
232 return value;
235 /* Names of the architecture-specific auditing callback functions. */
236 #define ARCH_LA_PLTENTER s390_64_gnu_pltenter
237 #define ARCH_LA_PLTEXIT s390_64_gnu_pltexit
239 #endif /* !dl_machine_h */
241 #ifdef RESOLVE_MAP
243 /* Perform the relocation specified by RELOC and SYM (which is fully resolved).
244 MAP is the object containing the reloc. */
246 auto inline void
247 __attribute__ ((always_inline))
248 elf_machine_rela (struct link_map *map, const Elf64_Rela *reloc,
249 const Elf64_Sym *sym, const struct r_found_version *version,
250 void *const reloc_addr_arg)
252 Elf64_Addr *const reloc_addr = reloc_addr_arg;
253 const unsigned int r_type = ELF64_R_TYPE (reloc->r_info);
255 #if !defined RTLD_BOOTSTRAP || !defined HAVE_Z_COMBRELOC
256 if (__builtin_expect (r_type == R_390_RELATIVE, 0))
258 # if !defined RTLD_BOOTSTRAP && !defined HAVE_Z_COMBRELOC
259 /* This is defined in rtld.c, but nowhere in the static libc.a;
260 make the reference weak so static programs can still link.
261 This declaration cannot be done when compiling rtld.c
262 (i.e. #ifdef RTLD_BOOTSTRAP) because rtld.c contains the
263 common defn for _dl_rtld_map, which is incompatible with a
264 weak decl in the same file. */
265 # ifndef SHARED
266 weak_extern (GL(dl_rtld_map));
267 # endif
268 if (map != &GL(dl_rtld_map)) /* Already done in rtld itself. */
269 # endif
270 *reloc_addr = map->l_addr + reloc->r_addend;
272 else
273 #endif
274 if (__builtin_expect (r_type == R_390_NONE, 0))
275 return;
276 else
278 #ifndef RESOLVE_CONFLICT_FIND_MAP
279 const Elf64_Sym *const refsym = sym;
280 #endif
281 struct link_map *sym_map = RESOLVE_MAP (&sym, version, r_type);
282 Elf64_Addr value = sym == NULL ? 0 : sym_map->l_addr + sym->st_value;
284 switch (r_type)
286 case R_390_GLOB_DAT:
287 case R_390_JMP_SLOT:
288 *reloc_addr = value + reloc->r_addend;
289 break;
291 #if (!defined RTLD_BOOTSTRAP || USE___THREAD) \
292 && !defined RESOLVE_CONFLICT_FIND_MAP
293 case R_390_TLS_DTPMOD:
294 # ifdef RTLD_BOOTSTRAP
295 /* During startup the dynamic linker is always the module
296 with index 1.
297 XXX If this relocation is necessary move before RESOLVE
298 call. */
299 *reloc_addr = 1;
300 # else
301 /* Get the information from the link map returned by the
302 resolv function. */
303 if (sym_map != NULL)
304 *reloc_addr = sym_map->l_tls_modid;
305 # endif
306 break;
307 case R_390_TLS_DTPOFF:
308 # ifndef RTLD_BOOTSTRAP
309 /* During relocation all TLS symbols are defined and used.
310 Therefore the offset is already correct. */
311 if (sym != NULL)
312 *reloc_addr = sym->st_value + reloc->r_addend;
313 # endif
314 break;
315 case R_390_TLS_TPOFF:
316 /* The offset is negative, forward from the thread pointer. */
317 # ifdef RTLD_BOOTSTRAP
318 *reloc_addr = sym->st_value + reloc->r_addend - map->l_tls_offset;
319 # else
320 /* We know the offset of the object the symbol is contained in.
321 It is a negative value which will be added to the
322 thread pointer. */
323 if (sym != NULL)
325 CHECK_STATIC_TLS (map, sym_map);
326 *reloc_addr = (sym->st_value + reloc->r_addend
327 - sym_map->l_tls_offset);
329 #endif
330 break;
331 #endif /* use TLS */
333 #ifndef RTLD_BOOTSTRAP
334 # ifndef RESOLVE_CONFLICT_FIND_MAP
335 /* Not needed for dl-conflict.c. */
336 case R_390_COPY:
337 if (sym == NULL)
338 /* This can happen in trace mode if an object could not be
339 found. */
340 break;
341 if (__builtin_expect (sym->st_size > refsym->st_size, 0)
342 || (__builtin_expect (sym->st_size < refsym->st_size, 0)
343 && __builtin_expect (GLRO(dl_verbose), 0)))
345 const char *strtab;
347 strtab = (const char *) D_PTR (map,l_info[DT_STRTAB]);
348 _dl_error_printf ("\
349 %s: Symbol `%s' has different size in shared object, consider re-linking\n",
350 rtld_progname ?: "<program name unknown>",
351 strtab + refsym->st_name);
353 memcpy (reloc_addr_arg, (void *) value,
354 MIN (sym->st_size, refsym->st_size));
355 break;
356 # endif
357 case R_390_64:
358 *reloc_addr = value + reloc->r_addend;
359 break;
360 case R_390_32:
361 *(unsigned int *) reloc_addr = value + reloc->r_addend;
362 break;
363 case R_390_16:
364 *(unsigned short *) reloc_addr = value + reloc->r_addend;
365 break;
366 case R_390_8:
367 *(char *) reloc_addr = value + reloc->r_addend;
368 break;
369 # ifndef RESOLVE_CONFLICT_FIND_MAP
370 case R_390_PC64:
371 *reloc_addr = value +reloc->r_addend - (Elf64_Addr) reloc_addr;
372 break;
373 case R_390_PC32DBL:
374 case R_390_PLT32DBL:
375 *(unsigned int *) reloc_addr = (unsigned int)
376 ((int) (value + reloc->r_addend - (Elf64_Addr) reloc_addr) >> 1);
377 break;
378 case R_390_PC32:
379 *(unsigned int *) reloc_addr =
380 value + reloc->r_addend - (Elf64_Addr) reloc_addr;
381 break;
382 case R_390_PC16DBL:
383 case R_390_PLT16DBL:
384 *(unsigned short *) reloc_addr = (unsigned short)
385 ((short) (value + reloc->r_addend - (Elf64_Addr) reloc_addr) >> 1);
386 break;
387 case R_390_PC16:
388 *(unsigned short *) reloc_addr =
389 value + reloc->r_addend - (Elf64_Addr) reloc_addr;
390 break;
391 case R_390_NONE:
392 break;
393 # endif
394 #endif
395 #if !defined(RTLD_BOOTSTRAP) || defined(_NDEBUG)
396 default:
397 /* We add these checks in the version to relocate ld.so only
398 if we are still debugging. */
399 _dl_reloc_bad_type (map, r_type, 0);
400 break;
401 #endif
406 auto inline void
407 __attribute__ ((always_inline))
408 elf_machine_rela_relative (Elf64_Addr l_addr, const Elf64_Rela *reloc,
409 void *const reloc_addr_arg)
411 Elf64_Addr *const reloc_addr = reloc_addr_arg;
412 *reloc_addr = l_addr + reloc->r_addend;
415 auto inline void
416 __attribute__ ((always_inline))
417 elf_machine_lazy_rel (struct link_map *map,
418 Elf64_Addr l_addr, const Elf64_Rela *reloc)
420 Elf64_Addr *const reloc_addr = (void *) (l_addr + reloc->r_offset);
421 const unsigned int r_type = ELF64_R_TYPE (reloc->r_info);
422 /* Check for unexpected PLT reloc type. */
423 if (__builtin_expect (r_type == R_390_JMP_SLOT, 1))
425 if (__builtin_expect (map->l_mach.plt, 0) == 0)
426 *reloc_addr += l_addr;
427 else
428 *reloc_addr =
429 map->l_mach.plt
430 + (((Elf64_Addr) reloc_addr) - map->l_mach.gotplt) * 4;
432 else
433 _dl_reloc_bad_type (map, r_type, 1);
436 #endif /* RESOLVE_MAP */