]> git.ipfire.org Git - thirdparty/glibc.git/blob - sysdeps/s390/s390-64/dl-machine.h
.
[thirdparty/glibc.git] / sysdeps / s390 / s390-64 / dl-machine.h
1 /* Machine-dependent ELF dynamic relocation inline functions.
2 64 bit S/390 Version.
3 Copyright (C) 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
4 Contributed by Martin Schwidefsky (schwidefsky@de.ibm.com).
5 This file is part of the GNU C Library.
6
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.
11
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.
16
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. */
21
22 #ifndef dl_machine_h
23 #define dl_machine_h
24
25 #define ELF_MACHINE_NAME "s390x"
26
27 #include <sys/param.h>
28 #include <string.h>
29 #include <link.h>
30
31 /* This is an older, now obsolete value. */
32 #define EM_S390_OLD 0xA390
33
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)
37 {
38 return (ehdr->e_machine == EM_S390 || ehdr->e_machine == EM_S390_OLD)
39 && ehdr->e_ident[EI_CLASS] == ELFCLASS64;
40 }
41
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. */
45
46 static inline Elf64_Addr
47 elf_machine_dynamic (void)
48 {
49 register Elf64_Addr *got;
50
51 asm( " larl %0,_GLOBAL_OFFSET_TABLE_\n"
52 : "=&a" (got) : : "0" );
53
54 return *got;
55 }
56
57 /* Return the run-time load address of the shared object. */
58 static inline Elf64_Addr
59 elf_machine_load_address (void)
60 {
61 Elf64_Addr addr;
62
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;
69 }
70
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. */
73
74 static inline int __attribute__ ((unused))
75 elf_machine_runtime_setup (struct link_map *l, int lazy, int profile)
76 {
77 extern void _dl_runtime_resolve (Elf64_Word);
78 extern void _dl_runtime_profile (Elf64_Word);
79
80 if (l->l_info[DT_JMPREL] && lazy)
81 {
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])
92 {
93 l->l_mach.plt = got[1] + l->l_addr;
94 l->l_mach.gotplt = (Elf64_Addr) &got[3];
95 }
96 got[1] = (Elf64_Addr) l; /* Identify this shared object. */
97
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))
105 {
106 got[2] = (Elf64_Addr) &_dl_runtime_profile;
107
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;
113 }
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;
118 }
119
120 return lazy;
121 }
122
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. */
126
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\
184 ");
185
186 #ifndef RTLD_START_SPECIAL_INIT
187 #define RTLD_START_SPECIAL_INIT /* nothing */
188 #endif
189
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 #ifdef USE_TLS
196 # define elf_machine_type_class(type) \
197 ((((type) == R_390_JMP_SLOT || (type) == R_390_TLS_DTPMOD \
198 || (type) == R_390_TLS_DTPOFF || (type) == R_390_TLS_TPOFF) \
199 * ELF_RTYPE_CLASS_PLT) \
200 | (((type) == R_390_COPY) * ELF_RTYPE_CLASS_COPY))
201 #else
202 # define elf_machine_type_class(type) \
203 ((((type) == R_390_JMP_SLOT) * ELF_RTYPE_CLASS_PLT) \
204 | (((type) == R_390_COPY) * ELF_RTYPE_CLASS_COPY))
205 #endif
206
207 /* A reloc type used for ld.so cmdline arg lookups to reject PLT entries. */
208 #define ELF_MACHINE_JMP_SLOT R_390_JMP_SLOT
209
210 /* The 64 bit S/390 never uses Elf64_Rel relocations. */
211 #define ELF_MACHINE_NO_REL 1
212
213 /* We define an initialization functions. This is called very early in
214 _dl_sysdep_start. */
215 #define DL_PLATFORM_INIT dl_platform_init ()
216
217 static inline void __attribute__ ((unused))
218 dl_platform_init (void)
219 {
220 if (GLRO(dl_platform) != NULL && *GLRO(dl_platform) == '\0')
221 /* Avoid an empty string which would disturb us. */
222 GLRO(dl_platform) = NULL;
223 }
224
225 static inline Elf64_Addr
226 elf_machine_fixup_plt (struct link_map *map, lookup_t t,
227 const Elf64_Rela *reloc,
228 Elf64_Addr *reloc_addr, Elf64_Addr value)
229 {
230 return *reloc_addr = value;
231 }
232
233 /* Return the final value of a plt relocation. */
234 static inline Elf64_Addr
235 elf_machine_plt_value (struct link_map *map, const Elf64_Rela *reloc,
236 Elf64_Addr value)
237 {
238 return value;
239 }
240
241 /* Names of the architecture-specific auditing callback functions. */
242 #define ARCH_LA_PLTENTER s390_64_gnu_pltenter
243 #define ARCH_LA_PLTEXIT s390_64_gnu_pltexit
244
245 #endif /* !dl_machine_h */
246
247 #ifdef RESOLVE_MAP
248
249 /* Perform the relocation specified by RELOC and SYM (which is fully resolved).
250 MAP is the object containing the reloc. */
251
252 auto inline void
253 __attribute__ ((always_inline))
254 elf_machine_rela (struct link_map *map, const Elf64_Rela *reloc,
255 const Elf64_Sym *sym, const struct r_found_version *version,
256 void *const reloc_addr_arg)
257 {
258 Elf64_Addr *const reloc_addr = reloc_addr_arg;
259 const unsigned int r_type = ELF64_R_TYPE (reloc->r_info);
260
261 #if !defined RTLD_BOOTSTRAP || !defined HAVE_Z_COMBRELOC
262 if (__builtin_expect (r_type == R_390_RELATIVE, 0))
263 {
264 # if !defined RTLD_BOOTSTRAP && !defined HAVE_Z_COMBRELOC
265 /* This is defined in rtld.c, but nowhere in the static libc.a;
266 make the reference weak so static programs can still link.
267 This declaration cannot be done when compiling rtld.c
268 (i.e. #ifdef RTLD_BOOTSTRAP) because rtld.c contains the
269 common defn for _dl_rtld_map, which is incompatible with a
270 weak decl in the same file. */
271 # ifndef SHARED
272 weak_extern (GL(dl_rtld_map));
273 # endif
274 if (map != &GL(dl_rtld_map)) /* Already done in rtld itself. */
275 # endif
276 *reloc_addr = map->l_addr + reloc->r_addend;
277 }
278 else
279 #endif
280 if (__builtin_expect (r_type == R_390_NONE, 0))
281 return;
282 else
283 {
284 #ifndef RESOLVE_CONFLICT_FIND_MAP
285 const Elf64_Sym *const refsym = sym;
286 #endif
287 struct link_map *sym_map = RESOLVE_MAP (&sym, version, r_type);
288 Elf64_Addr value = sym == NULL ? 0 : sym_map->l_addr + sym->st_value;
289
290 switch (r_type)
291 {
292 case R_390_GLOB_DAT:
293 case R_390_JMP_SLOT:
294 *reloc_addr = value + reloc->r_addend;
295 break;
296
297 #if defined USE_TLS && (!defined RTLD_BOOTSTRAP || USE___THREAD) \
298 && !defined RESOLVE_CONFLICT_FIND_MAP
299 case R_390_TLS_DTPMOD:
300 # ifdef RTLD_BOOTSTRAP
301 /* During startup the dynamic linker is always the module
302 with index 1.
303 XXX If this relocation is necessary move before RESOLVE
304 call. */
305 *reloc_addr = 1;
306 # else
307 /* Get the information from the link map returned by the
308 resolv function. */
309 if (sym_map != NULL)
310 *reloc_addr = sym_map->l_tls_modid;
311 # endif
312 break;
313 case R_390_TLS_DTPOFF:
314 # ifndef RTLD_BOOTSTRAP
315 /* During relocation all TLS symbols are defined and used.
316 Therefore the offset is already correct. */
317 if (sym != NULL)
318 *reloc_addr = sym->st_value + reloc->r_addend;
319 # endif
320 break;
321 case R_390_TLS_TPOFF:
322 /* The offset is negative, forward from the thread pointer. */
323 # ifdef RTLD_BOOTSTRAP
324 *reloc_addr = sym->st_value + reloc->r_addend - map->l_tls_offset;
325 # else
326 /* We know the offset of the object the symbol is contained in.
327 It is a negative value which will be added to the
328 thread pointer. */
329 if (sym != NULL)
330 {
331 CHECK_STATIC_TLS (map, sym_map);
332 *reloc_addr = (sym->st_value + reloc->r_addend
333 - sym_map->l_tls_offset);
334 }
335 #endif
336 break;
337 #endif /* use TLS */
338
339 #ifndef RTLD_BOOTSTRAP
340 # ifndef RESOLVE_CONFLICT_FIND_MAP
341 /* Not needed for dl-conflict.c. */
342 case R_390_COPY:
343 if (sym == NULL)
344 /* This can happen in trace mode if an object could not be
345 found. */
346 break;
347 if (__builtin_expect (sym->st_size > refsym->st_size, 0)
348 || (__builtin_expect (sym->st_size < refsym->st_size, 0)
349 && __builtin_expect (GLRO(dl_verbose), 0)))
350 {
351 const char *strtab;
352
353 strtab = (const char *) D_PTR (map,l_info[DT_STRTAB]);
354 _dl_error_printf ("\
355 %s: Symbol `%s' has different size in shared object, consider re-linking\n",
356 rtld_progname ?: "<program name unknown>",
357 strtab + refsym->st_name);
358 }
359 memcpy (reloc_addr_arg, (void *) value,
360 MIN (sym->st_size, refsym->st_size));
361 break;
362 # endif
363 case R_390_64:
364 *reloc_addr = value + reloc->r_addend;
365 break;
366 case R_390_32:
367 *(unsigned int *) reloc_addr = value + reloc->r_addend;
368 break;
369 case R_390_16:
370 *(unsigned short *) reloc_addr = value + reloc->r_addend;
371 break;
372 case R_390_8:
373 *(char *) reloc_addr = value + reloc->r_addend;
374 break;
375 # ifndef RESOLVE_CONFLICT_FIND_MAP
376 case R_390_PC64:
377 *reloc_addr = value +reloc->r_addend - (Elf64_Addr) reloc_addr;
378 break;
379 case R_390_PC32DBL:
380 case R_390_PLT32DBL:
381 *(unsigned int *) reloc_addr = (unsigned int)
382 ((int) (value + reloc->r_addend - (Elf64_Addr) reloc_addr) >> 1);
383 break;
384 case R_390_PC32:
385 *(unsigned int *) reloc_addr =
386 value + reloc->r_addend - (Elf64_Addr) reloc_addr;
387 break;
388 case R_390_PC16DBL:
389 case R_390_PLT16DBL:
390 *(unsigned short *) reloc_addr = (unsigned short)
391 ((short) (value + reloc->r_addend - (Elf64_Addr) reloc_addr) >> 1);
392 break;
393 case R_390_PC16:
394 *(unsigned short *) reloc_addr =
395 value + reloc->r_addend - (Elf64_Addr) reloc_addr;
396 break;
397 case R_390_NONE:
398 break;
399 # endif
400 #endif
401 #if !defined(RTLD_BOOTSTRAP) || defined(_NDEBUG)
402 default:
403 /* We add these checks in the version to relocate ld.so only
404 if we are still debugging. */
405 _dl_reloc_bad_type (map, r_type, 0);
406 break;
407 #endif
408 }
409 }
410 }
411
412 auto inline void
413 __attribute__ ((always_inline))
414 elf_machine_rela_relative (Elf64_Addr l_addr, const Elf64_Rela *reloc,
415 void *const reloc_addr_arg)
416 {
417 Elf64_Addr *const reloc_addr = reloc_addr_arg;
418 *reloc_addr = l_addr + reloc->r_addend;
419 }
420
421 auto inline void
422 __attribute__ ((always_inline))
423 elf_machine_lazy_rel (struct link_map *map,
424 Elf64_Addr l_addr, const Elf64_Rela *reloc)
425 {
426 Elf64_Addr *const reloc_addr = (void *) (l_addr + reloc->r_offset);
427 const unsigned int r_type = ELF64_R_TYPE (reloc->r_info);
428 /* Check for unexpected PLT reloc type. */
429 if (__builtin_expect (r_type == R_390_JMP_SLOT, 1))
430 {
431 if (__builtin_expect (map->l_mach.plt, 0) == 0)
432 *reloc_addr += l_addr;
433 else
434 *reloc_addr =
435 map->l_mach.plt
436 + (((Elf64_Addr) reloc_addr) - map->l_mach.gotplt) * 4;
437 }
438 else
439 _dl_reloc_bad_type (map, r_type, 1);
440 }
441
442 #endif /* RESOLVE_MAP */