Replace FSF snail mail address with URLs.
[glibc.git] / sysdeps / s390 / s390-64 / dl-machine.h
blob908c024763090b960aa443376406ae6392d4bc77
1 /* Machine-dependent ELF dynamic relocation inline functions.
2 64 bit S/390 Version.
3 Copyright (C) 2001-2005, 2006, 2011 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, see
19 <http://www.gnu.org/licenses/>. */
21 #ifndef dl_machine_h
22 #define dl_machine_h
24 #define ELF_MACHINE_NAME "s390x"
26 #include <sys/param.h>
27 #include <string.h>
28 #include <link.h>
30 /* This is an older, now obsolete value. */
31 #define EM_S390_OLD 0xA390
33 /* Return nonzero iff E_MACHINE is compatible with the running host. */
34 static inline int
35 elf_machine_matches_host (const Elf64_Ehdr *ehdr)
37 return (ehdr->e_machine == EM_S390 || ehdr->e_machine == EM_S390_OLD)
38 && ehdr->e_ident[EI_CLASS] == ELFCLASS64;
41 /* Return the link-time address of _DYNAMIC. Conveniently, this is the
42 first element of the GOT. This must be inlined in a function which
43 uses global data. */
45 static inline Elf64_Addr
46 elf_machine_dynamic (void)
48 register Elf64_Addr *got;
50 asm( " larl %0,_GLOBAL_OFFSET_TABLE_\n"
51 : "=&a" (got) : : "0" );
53 return *got;
56 /* Return the run-time load address of the shared object. */
57 static inline Elf64_Addr
58 elf_machine_load_address (void)
60 Elf64_Addr addr;
62 asm( " larl %0,_dl_start\n"
63 " larl 1,_GLOBAL_OFFSET_TABLE_\n"
64 " lghi 2,_dl_start@GOT\n"
65 " slg %0,0(2,1)"
66 : "=&d" (addr) : : "1", "2" );
67 return addr;
70 /* Set up the loaded object described by L so its unrelocated PLT
71 entries will jump to the on-demand fixup code in dl-runtime.c. */
73 static inline int __attribute__ ((unused))
74 elf_machine_runtime_setup (struct link_map *l, int lazy, int profile)
76 extern void _dl_runtime_resolve (Elf64_Word);
77 extern void _dl_runtime_profile (Elf64_Word);
79 if (l->l_info[DT_JMPREL] && lazy)
81 /* The GOT entries for functions in the PLT have not yet been filled
82 in. Their initial contents will arrange when called to push an
83 offset into the .rela.plt section, push _GLOBAL_OFFSET_TABLE_[1],
84 and then jump to _GLOBAL_OFFSET_TABLE[2]. */
85 Elf64_Addr *got;
86 got = (Elf64_Addr *) D_PTR (l, l_info[DT_PLTGOT]);
87 /* If a library is prelinked but we have to relocate anyway,
88 we have to be able to undo the prelinking of .got.plt.
89 The prelinker saved us here address of .plt + 0x2e. */
90 if (got[1])
92 l->l_mach.plt = got[1] + l->l_addr;
93 l->l_mach.gotplt = (Elf64_Addr) &got[3];
95 got[1] = (Elf64_Addr) l; /* Identify this shared object. */
97 /* The got[2] entry contains the address of a function which gets
98 called to get the address of a so far unresolved function and
99 jump to it. The profiling extension of the dynamic linker allows
100 to intercept the calls to collect information. In this case we
101 don't store the address in the GOT so that all future calls also
102 end in this function. */
103 if (__builtin_expect (profile, 0))
105 got[2] = (Elf64_Addr) &_dl_runtime_profile;
107 if (GLRO(dl_profile) != NULL
108 && _dl_name_match_p (GLRO(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 /* Initial entry point code for the dynamic linker.
123 The C function `_dl_start' is the real entry point;
124 its return value is the user program's entry point. */
126 #define RTLD_START asm ("\n\
127 .text\n\
128 .align 4\n\
129 .globl _start\n\
130 .globl _dl_start_user\n\
131 _start:\n\
132 lgr %r2,%r15\n\
133 # Alloc stack frame\n\
134 aghi %r15,-160\n\
135 # Set the back chain to zero\n\
136 xc 0(8,%r15),0(%r15)\n\
137 # Call _dl_start with %r2 pointing to arg on stack\n\
138 brasl %r14,_dl_start # call _dl_start\n\
139 _dl_start_user:\n\
140 # Save the user entry point address in %r8.\n\
141 lgr %r8,%r2\n\
142 # Point %r12 at the GOT.\n\
143 larl %r12,_GLOBAL_OFFSET_TABLE_\n\
144 # See if we were run as a command with the executable file\n\
145 # name as an extra leading argument.\n\
146 lghi %r1,_dl_skip_args@GOT\n\
147 lg %r1,0(%r1,%r12)\n\
148 lgf %r1,0(%r1) # load _dl_skip_args\n\
149 # Get the original argument count.\n\
150 lg %r0,160(%r15)\n\
151 # Subtract _dl_skip_args from it.\n\
152 sgr %r0,%r1\n\
153 # Adjust the stack pointer to skip _dl_skip_args words.\n\
154 sllg %r1,%r1,3\n\
155 agr %r15,%r1\n\
156 # Set the back chain to zero again\n\
157 xc 0(8,%r15),0(%r15)\n\
158 # Store back the modified argument count.\n\
159 stg %r0,160(%r15)\n\
160 # The special initializer gets called with the stack just\n\
161 # as the application's entry point will see it; it can\n\
162 # switch stacks if it moves these contents over.\n\
163 " RTLD_START_SPECIAL_INIT "\n\
164 # Call the function to run the initializers.\n\
165 # Load the parameters:\n\
166 # (%r2, %r3, %r4, %r5) = (_dl_loaded, argc, argv, envp)\n\
167 lghi %r2,_rtld_local@GOT\n\
168 lg %r2,0(%r2,%r12)\n\
169 lg %r2,0(%r2)\n\
170 lg %r3,160(%r15)\n\
171 la %r4,168(%r15)\n\
172 lgr %r5,%r3\n\
173 sllg %r5,%r5,3\n\
174 la %r5,176(%r5,%r15)\n\
175 brasl %r14,_dl_init_internal@PLT\n\
176 # Pass our finalizer function to the user in %r14, as per ELF ABI.\n\
177 lghi %r14,_dl_fini@GOT\n\
178 lg %r14,0(%r14,%r12)\n\
179 # Free stack frame\n\
180 aghi %r15,160\n\
181 # Jump to the user's entry point (saved in %r8).\n\
182 br %r8\n\
185 #ifndef RTLD_START_SPECIAL_INIT
186 #define RTLD_START_SPECIAL_INIT /* nothing */
187 #endif
189 /* ELF_RTYPE_CLASS_PLT iff TYPE describes relocation of a PLT entry or
190 TLS variable, so undefined references should not be allowed to
191 define the value.
192 ELF_RTYPE_CLASS_NOCOPY iff TYPE should not be allowed to resolve to one
193 of the main executable's symbols, as for a COPY reloc. */
194 #define elf_machine_type_class(type) \
195 ((((type) == R_390_JMP_SLOT || (type) == R_390_TLS_DTPMOD \
196 || (type) == R_390_TLS_DTPOFF || (type) == R_390_TLS_TPOFF) \
197 * ELF_RTYPE_CLASS_PLT) \
198 | (((type) == R_390_COPY) * ELF_RTYPE_CLASS_COPY))
200 /* A reloc type used for ld.so cmdline arg lookups to reject PLT entries. */
201 #define ELF_MACHINE_JMP_SLOT R_390_JMP_SLOT
203 /* The 64 bit S/390 never uses Elf64_Rel relocations. */
204 #define ELF_MACHINE_NO_REL 1
206 /* We define an initialization functions. This is called very early in
207 _dl_sysdep_start. */
208 #define DL_PLATFORM_INIT dl_platform_init ()
210 static inline void __attribute__ ((unused))
211 dl_platform_init (void)
213 if (GLRO(dl_platform) != NULL && *GLRO(dl_platform) == '\0')
214 /* Avoid an empty string which would disturb us. */
215 GLRO(dl_platform) = NULL;
218 static inline Elf64_Addr
219 elf_machine_fixup_plt (struct link_map *map, lookup_t t,
220 const Elf64_Rela *reloc,
221 Elf64_Addr *reloc_addr, Elf64_Addr value)
223 return *reloc_addr = value;
226 /* Return the final value of a plt relocation. */
227 static inline Elf64_Addr
228 elf_machine_plt_value (struct link_map *map, const Elf64_Rela *reloc,
229 Elf64_Addr value)
231 return value;
234 /* Names of the architecture-specific auditing callback functions. */
235 #define ARCH_LA_PLTENTER s390_64_gnu_pltenter
236 #define ARCH_LA_PLTEXIT s390_64_gnu_pltexit
238 #endif /* !dl_machine_h */
240 #ifdef RESOLVE_MAP
242 /* Perform the relocation specified by RELOC and SYM (which is fully resolved).
243 MAP is the object containing the reloc. */
245 auto inline void
246 __attribute__ ((always_inline))
247 elf_machine_rela (struct link_map *map, const Elf64_Rela *reloc,
248 const Elf64_Sym *sym, const struct r_found_version *version,
249 void *const reloc_addr_arg, int skip_ifunc)
251 Elf64_Addr *const reloc_addr = reloc_addr_arg;
252 const unsigned int r_type = ELF64_R_TYPE (reloc->r_info);
254 #if !defined RTLD_BOOTSTRAP || !defined HAVE_Z_COMBRELOC
255 if (__builtin_expect (r_type == R_390_RELATIVE, 0))
257 # if !defined RTLD_BOOTSTRAP && !defined HAVE_Z_COMBRELOC
258 /* This is defined in rtld.c, but nowhere in the static libc.a;
259 make the reference weak so static programs can still link.
260 This declaration cannot be done when compiling rtld.c
261 (i.e. #ifdef RTLD_BOOTSTRAP) because rtld.c contains the
262 common defn for _dl_rtld_map, which is incompatible with a
263 weak decl in the same file. */
264 # ifndef SHARED
265 weak_extern (GL(dl_rtld_map));
266 # endif
267 if (map != &GL(dl_rtld_map)) /* Already done in rtld itself. */
268 # endif
269 *reloc_addr = map->l_addr + reloc->r_addend;
271 else
272 #endif
273 if (__builtin_expect (r_type == R_390_NONE, 0))
274 return;
275 else
277 #ifndef RESOLVE_CONFLICT_FIND_MAP
278 const Elf64_Sym *const refsym = sym;
279 #endif
280 struct link_map *sym_map = RESOLVE_MAP (&sym, version, r_type);
281 Elf64_Addr value = sym == NULL ? 0 : sym_map->l_addr + sym->st_value;
283 switch (r_type)
285 case R_390_GLOB_DAT:
286 case R_390_JMP_SLOT:
287 *reloc_addr = value + reloc->r_addend;
288 break;
290 #ifndef RESOLVE_CONFLICT_FIND_MAP
291 case R_390_TLS_DTPMOD:
292 # ifdef RTLD_BOOTSTRAP
293 /* During startup the dynamic linker is always the module
294 with index 1.
295 XXX If this relocation is necessary move before RESOLVE
296 call. */
297 *reloc_addr = 1;
298 # else
299 /* Get the information from the link map returned by the
300 resolv function. */
301 if (sym_map != NULL)
302 *reloc_addr = sym_map->l_tls_modid;
303 # endif
304 break;
305 case R_390_TLS_DTPOFF:
306 # ifndef RTLD_BOOTSTRAP
307 /* During relocation all TLS symbols are defined and used.
308 Therefore the offset is already correct. */
309 if (sym != NULL)
310 *reloc_addr = sym->st_value + reloc->r_addend;
311 # endif
312 break;
313 case R_390_TLS_TPOFF:
314 /* The offset is negative, forward from the thread pointer. */
315 # ifdef RTLD_BOOTSTRAP
316 *reloc_addr = sym->st_value + reloc->r_addend - map->l_tls_offset;
317 # else
318 /* We know the offset of the object the symbol is contained in.
319 It is a negative value which will be added to the
320 thread pointer. */
321 if (sym != NULL)
323 CHECK_STATIC_TLS (map, sym_map);
324 *reloc_addr = (sym->st_value + reloc->r_addend
325 - sym_map->l_tls_offset);
327 #endif
328 break;
329 #endif /* use TLS */
331 #ifndef RTLD_BOOTSTRAP
332 # ifndef RESOLVE_CONFLICT_FIND_MAP
333 /* Not needed for dl-conflict.c. */
334 case R_390_COPY:
335 if (sym == NULL)
336 /* This can happen in trace mode if an object could not be
337 found. */
338 break;
339 if (__builtin_expect (sym->st_size > refsym->st_size, 0)
340 || (__builtin_expect (sym->st_size < refsym->st_size, 0)
341 && __builtin_expect (GLRO(dl_verbose), 0)))
343 const char *strtab;
345 strtab = (const char *) D_PTR (map,l_info[DT_STRTAB]);
346 _dl_error_printf ("\
347 %s: Symbol `%s' has different size in shared object, consider re-linking\n",
348 rtld_progname ?: "<program name unknown>",
349 strtab + refsym->st_name);
351 memcpy (reloc_addr_arg, (void *) value,
352 MIN (sym->st_size, refsym->st_size));
353 break;
354 # endif
355 case R_390_64:
356 *reloc_addr = value + reloc->r_addend;
357 break;
358 case R_390_32:
359 *(unsigned int *) reloc_addr = value + reloc->r_addend;
360 break;
361 case R_390_16:
362 *(unsigned short *) reloc_addr = value + reloc->r_addend;
363 break;
364 case R_390_8:
365 *(char *) reloc_addr = value + reloc->r_addend;
366 break;
367 # ifndef RESOLVE_CONFLICT_FIND_MAP
368 case R_390_PC64:
369 *reloc_addr = value +reloc->r_addend - (Elf64_Addr) reloc_addr;
370 break;
371 case R_390_PC32DBL:
372 *(unsigned int *) reloc_addr = (unsigned int)
373 ((int) (value + reloc->r_addend - (Elf64_Addr) reloc_addr) >> 1);
374 break;
375 case R_390_PC32:
376 *(unsigned int *) reloc_addr =
377 value + reloc->r_addend - (Elf64_Addr) reloc_addr;
378 break;
379 case R_390_PC16DBL:
380 *(unsigned short *) reloc_addr = (unsigned short)
381 ((short) (value + reloc->r_addend - (Elf64_Addr) reloc_addr) >> 1);
382 break;
383 case R_390_PC16:
384 *(unsigned short *) reloc_addr =
385 value + reloc->r_addend - (Elf64_Addr) reloc_addr;
386 break;
387 case R_390_NONE:
388 break;
389 # endif
390 #endif
391 #if !defined(RTLD_BOOTSTRAP) || defined(_NDEBUG)
392 default:
393 /* We add these checks in the version to relocate ld.so only
394 if we are still debugging. */
395 _dl_reloc_bad_type (map, r_type, 0);
396 break;
397 #endif
402 auto inline void
403 __attribute__ ((always_inline))
404 elf_machine_rela_relative (Elf64_Addr l_addr, const Elf64_Rela *reloc,
405 void *const reloc_addr_arg)
407 Elf64_Addr *const reloc_addr = reloc_addr_arg;
408 *reloc_addr = l_addr + reloc->r_addend;
411 auto inline void
412 __attribute__ ((always_inline))
413 elf_machine_lazy_rel (struct link_map *map,
414 Elf64_Addr l_addr, const Elf64_Rela *reloc,
415 int skip_ifunc)
417 Elf64_Addr *const reloc_addr = (void *) (l_addr + reloc->r_offset);
418 const unsigned int r_type = ELF64_R_TYPE (reloc->r_info);
419 /* Check for unexpected PLT reloc type. */
420 if (__builtin_expect (r_type == R_390_JMP_SLOT, 1))
422 if (__builtin_expect (map->l_mach.plt, 0) == 0)
423 *reloc_addr += l_addr;
424 else
425 *reloc_addr =
426 map->l_mach.plt
427 + (((Elf64_Addr) reloc_addr) - map->l_mach.gotplt) * 4;
429 else
430 _dl_reloc_bad_type (map, r_type, 1);
433 #endif /* RESOLVE_MAP */