1 /* Machine-dependent ELF dynamic relocation inline functions.
3 Copyright (C) 2001-2024 Free Software Foundation, Inc.
4 This file is part of the GNU C Library.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, see
18 <https://www.gnu.org/licenses/>. */
23 #define ELF_MACHINE_NAME "s390x"
25 #include <sys/param.h>
28 #include <sysdeps/s390/dl-procinfo.h>
30 #include <dl-static-tls.h>
31 #include <dl-machine-rel.h>
32 #include <cpu-features.c>
34 #define ELF_MACHINE_IRELATIVE R_390_IRELATIVE
36 /* This is an older, now obsolete value. */
37 #define EM_S390_OLD 0xA390
39 /* Return nonzero iff E_MACHINE is compatible with the running host. */
41 elf_machine_matches_host (const Elf64_Ehdr
*ehdr
)
43 return (ehdr
->e_machine
== EM_S390
|| ehdr
->e_machine
== EM_S390_OLD
)
44 && ehdr
->e_ident
[EI_CLASS
] == ELFCLASS64
;
47 /* Return the link-time address of _DYNAMIC. Conveniently, this is the
48 first element of the GOT. This must be inlined in a function which
51 static inline Elf64_Addr
52 elf_machine_dynamic (void)
54 register Elf64_Addr
*got
;
56 __asm__ ( " larl %0,_GLOBAL_OFFSET_TABLE_\n"
57 : "=&a" (got
) : : "0" );
62 /* Return the run-time load address of the shared object. */
63 static inline Elf64_Addr
64 elf_machine_load_address (void)
68 __asm__( " larl %0,_dl_start\n"
69 " larl 1,_GLOBAL_OFFSET_TABLE_\n"
70 " lghi 2,_dl_start@GOT\n"
72 : "=&d" (addr
) : : "1", "2" );
76 /* Set up the loaded object described by L so its unrelocated PLT
77 entries will jump to the on-demand fixup code in dl-runtime.c. */
79 static inline int __attribute__ ((unused
))
80 elf_machine_runtime_setup (struct link_map
*l
, struct r_scope_elem
*scope
[],
81 int lazy
, int profile
)
83 extern void _dl_runtime_resolve (Elf64_Word
);
84 extern void _dl_runtime_profile (Elf64_Word
);
85 #if defined HAVE_S390_VX_ASM_SUPPORT
86 extern void _dl_runtime_resolve_vx (Elf64_Word
);
87 extern void _dl_runtime_profile_vx (Elf64_Word
);
90 if (l
->l_info
[DT_JMPREL
] && lazy
)
92 /* The GOT entries for functions in the PLT have not yet been filled
93 in. Their initial contents will arrange when called to push an
94 offset into the .rela.plt section, push _GLOBAL_OFFSET_TABLE_[1],
95 and then jump to _GLOBAL_OFFSET_TABLE[2]. */
97 got
= (Elf64_Addr
*) D_PTR (l
, l_info
[DT_PLTGOT
]);
98 /* If a library is prelinked but we have to relocate anyway,
99 we have to be able to undo the prelinking of .got.plt.
100 The prelinker saved us here address of .plt + 0x2e. */
103 l
->l_mach
.plt
= got
[1] + l
->l_addr
;
104 l
->l_mach
.jmprel
= (const Elf64_Rela
*) D_PTR (l
, l_info
[DT_JMPREL
]);
106 got
[1] = (Elf64_Addr
) l
; /* Identify this shared object. */
108 /* The got[2] entry contains the address of a function which gets
109 called to get the address of a so far unresolved function and
110 jump to it. The profiling extension of the dynamic linker allows
111 to intercept the calls to collect information. In this case we
112 don't store the address in the GOT so that all future calls also
113 end in this function. */
115 if (__glibc_unlikely (profile
))
117 # if defined HAVE_S390_VX_ASM_SUPPORT
118 if (GLRO(dl_hwcap
) & HWCAP_S390_VX
)
119 got
[2] = (Elf64_Addr
) &_dl_runtime_profile_vx
;
121 got
[2] = (Elf64_Addr
) &_dl_runtime_profile
;
123 got
[2] = (Elf64_Addr
) &_dl_runtime_profile
;
126 if (GLRO(dl_profile
) != NULL
127 && _dl_name_match_p (GLRO(dl_profile
), l
))
128 /* This is the object we are looking for. Say that we really
129 want profiling and the timers are started. */
130 GL(dl_profile_map
) = l
;
135 /* This function will get called to fix up the GOT entry indicated by
136 the offset on the stack, and then jump to the resolved address. */
137 #if defined HAVE_S390_VX_ASM_SUPPORT
138 if (GLRO(dl_hwcap
) & HWCAP_S390_VX
)
139 got
[2] = (Elf64_Addr
) &_dl_runtime_resolve_vx
;
141 got
[2] = (Elf64_Addr
) &_dl_runtime_resolve
;
143 got
[2] = (Elf64_Addr
) &_dl_runtime_resolve
;
151 /* Initial entry point code for the dynamic linker.
152 The C function `_dl_start' is the real entry point;
153 its return value is the user program's entry point. */
155 #define RTLD_START __asm__ ("\n\
159 .globl _dl_start_user\n\
162 # Alloc stack frame\n\
164 # Set the back chain to zero\n\
165 xc 0(8,%r15),0(%r15)\n\
166 # Call _dl_start with %r2 pointing to arg on stack\n\
167 brasl %r14,_dl_start # call _dl_start\n\
169 # Save the user entry point address in %r8.\n\
171 # Point %r12 at the GOT.\n\
172 larl %r12,_GLOBAL_OFFSET_TABLE_\n\
173 # The special initializer gets called with the stack just\n\
174 # as the application's entry point will see it; it can\n\
175 # switch stacks if it moves these contents over.\n\
176 " RTLD_START_SPECIAL_INIT "\n\
177 # Call the function to run the initializers.\n\
178 # Load the parameters:\n\
179 # (%r2, %r3, %r4, %r5) = (_dl_loaded, argc, argv, envp)\n\
180 lghi %r2,_rtld_local@GOT\n\
181 lg %r2,0(%r2,%r12)\n\
187 la %r5,176(%r5,%r15)\n\
188 brasl %r14,_dl_init@PLT\n\
189 # Pass our finalizer function to the user in %r14, as per ELF ABI.\n\
190 lghi %r14,_dl_fini@GOT\n\
191 lg %r14,0(%r14,%r12)\n\
192 # Free stack frame\n\
194 # Jump to the user's entry point (saved in %r8).\n\
198 #ifndef RTLD_START_SPECIAL_INIT
199 #define RTLD_START_SPECIAL_INIT /* nothing */
202 /* ELF_RTYPE_CLASS_PLT iff TYPE describes relocation of a PLT entry or
203 TLS variable, so undefined references should not be allowed to
205 ELF_RTYPE_CLASS_COPY iff TYPE should not be allowed to resolve to one
206 of the main executable's symbols, as for a COPY reloc. */
207 #define elf_machine_type_class(type) \
208 ((((type) == R_390_JMP_SLOT || (type) == R_390_TLS_DTPMOD \
209 || (type) == R_390_TLS_DTPOFF || (type) == R_390_TLS_TPOFF) \
210 * ELF_RTYPE_CLASS_PLT) \
211 | (((type) == R_390_COPY) * ELF_RTYPE_CLASS_COPY))
213 /* A reloc type used for ld.so cmdline arg lookups to reject PLT entries. */
214 #define ELF_MACHINE_JMP_SLOT R_390_JMP_SLOT
216 /* We define an initialization functions. This is called very early in
218 #define DL_PLATFORM_INIT dl_platform_init ()
220 static inline void __attribute__ ((unused
))
221 dl_platform_init (void)
223 if (GLRO(dl_platform
) != NULL
&& *GLRO(dl_platform
) == '\0')
224 /* Avoid an empty string which would disturb us. */
225 GLRO(dl_platform
) = NULL
;
228 /* init_cpu_features has been called early from __libc_start_main in
229 static executable. */
230 init_cpu_features (&GLRO(dl_s390_cpu_features
));
235 static inline Elf64_Addr
236 elf_machine_fixup_plt (struct link_map
*map
, lookup_t t
,
237 const ElfW(Sym
) *refsym
, const ElfW(Sym
) *sym
,
238 const Elf64_Rela
*reloc
,
239 Elf64_Addr
*reloc_addr
, Elf64_Addr value
)
241 return *reloc_addr
= value
;
244 /* Return the final value of a plt relocation. */
245 static inline Elf64_Addr
246 elf_machine_plt_value (struct link_map
*map
, const Elf64_Rela
*reloc
,
252 /* Names of the architecture-specific auditing callback functions. */
253 #define ARCH_LA_PLTENTER s390_64_gnu_pltenter
254 #define ARCH_LA_PLTEXIT s390_64_gnu_pltexit
256 #endif /* !dl_machine_h */
260 /* Perform the relocation specified by RELOC and SYM (which is fully resolved).
261 MAP is the object containing the reloc. */
264 __attribute__ ((always_inline
))
265 elf_machine_rela (struct link_map
*map
, struct r_scope_elem
*scope
[],
266 const Elf64_Rela
*reloc
, const Elf64_Sym
*sym
,
267 const struct r_found_version
*version
,
268 void *const reloc_addr_arg
, int skip_ifunc
)
270 Elf64_Addr
*const reloc_addr
= reloc_addr_arg
;
271 const unsigned int r_type
= ELF64_R_TYPE (reloc
->r_info
);
273 #if !defined RTLD_BOOTSTRAP
274 if (__glibc_unlikely (r_type
== R_390_RELATIVE
))
275 *reloc_addr
= map
->l_addr
+ reloc
->r_addend
;
278 if (__glibc_unlikely (r_type
== R_390_NONE
))
282 #if !defined RTLD_BOOTSTRAP
283 /* Only needed for R_390_COPY below. */
284 const Elf64_Sym
*const refsym
= sym
;
286 struct link_map
*sym_map
= RESOLVE_MAP (map
, scope
, &sym
, version
,
288 Elf64_Addr value
= SYMBOL_ADDRESS (sym_map
, sym
, true);
291 && __builtin_expect (ELFW(ST_TYPE
) (sym
->st_info
) == STT_GNU_IFUNC
,
293 && __builtin_expect (sym
->st_shndx
!= SHN_UNDEF
, 1)
294 && __builtin_expect (!skip_ifunc
, 1))
295 value
= elf_ifunc_invoke (value
);
299 case R_390_IRELATIVE
:
300 value
= map
->l_addr
+ reloc
->r_addend
;
301 if (__glibc_likely (!skip_ifunc
))
302 value
= elf_ifunc_invoke (value
);
307 *reloc_addr
= value
+ reloc
->r_addend
;
310 case R_390_TLS_DTPMOD
:
311 #ifdef RTLD_BOOTSTRAP
312 /* During startup the dynamic linker is always the module
314 XXX If this relocation is necessary move before RESOLVE
318 /* Get the information from the link map returned by the
321 *reloc_addr
= sym_map
->l_tls_modid
;
324 case R_390_TLS_DTPOFF
:
325 #ifndef RTLD_BOOTSTRAP
326 /* During relocation all TLS symbols are defined and used.
327 Therefore the offset is already correct. */
329 *reloc_addr
= sym
->st_value
+ reloc
->r_addend
;
332 case R_390_TLS_TPOFF
:
333 /* The offset is negative, forward from the thread pointer. */
334 #ifdef RTLD_BOOTSTRAP
335 *reloc_addr
= sym
->st_value
+ reloc
->r_addend
- map
->l_tls_offset
;
337 /* We know the offset of the object the symbol is contained in.
338 It is a negative value which will be added to the
342 CHECK_STATIC_TLS (map
, sym_map
);
343 *reloc_addr
= (sym
->st_value
+ reloc
->r_addend
344 - sym_map
->l_tls_offset
);
349 #ifndef RTLD_BOOTSTRAP
350 /* Not needed for dl-conflict.c. */
353 /* This can happen in trace mode if an object could not be
356 if (__builtin_expect (sym
->st_size
> refsym
->st_size
, 0)
357 || (__builtin_expect (sym
->st_size
< refsym
->st_size
, 0)
358 && __builtin_expect (GLRO(dl_verbose
), 0)))
362 strtab
= (const char *) D_PTR (map
,l_info
[DT_STRTAB
]);
364 %s: Symbol `%s' has different size in shared object, consider re-linking\n",
365 RTLD_PROGNAME
, strtab
+ refsym
->st_name
);
367 memcpy (reloc_addr_arg
, (void *) value
,
368 MIN (sym
->st_size
, refsym
->st_size
));
371 *reloc_addr
= value
+ reloc
->r_addend
;
374 *(unsigned int *) reloc_addr
= value
+ reloc
->r_addend
;
377 *(unsigned short *) reloc_addr
= value
+ reloc
->r_addend
;
380 *(char *) reloc_addr
= value
+ reloc
->r_addend
;
383 *reloc_addr
= value
+reloc
->r_addend
- (Elf64_Addr
) reloc_addr
;
386 *(unsigned int *) reloc_addr
= (unsigned int)
387 ((int) (value
+ reloc
->r_addend
- (Elf64_Addr
) reloc_addr
) >> 1);
390 *(unsigned int *) reloc_addr
=
391 value
+ reloc
->r_addend
- (Elf64_Addr
) reloc_addr
;
394 *(unsigned short *) reloc_addr
= (unsigned short)
395 ((short) (value
+ reloc
->r_addend
- (Elf64_Addr
) reloc_addr
) >> 1);
398 *(unsigned short *) reloc_addr
=
399 value
+ reloc
->r_addend
- (Elf64_Addr
) reloc_addr
;
404 #if !defined(RTLD_BOOTSTRAP) || defined(_NDEBUG)
406 /* We add these checks in the version to relocate ld.so only
407 if we are still debugging. */
408 _dl_reloc_bad_type (map
, r_type
, 0);
416 __attribute__ ((always_inline
))
417 elf_machine_rela_relative (Elf64_Addr l_addr
, const Elf64_Rela
*reloc
,
418 void *const reloc_addr_arg
)
420 Elf64_Addr
*const reloc_addr
= reloc_addr_arg
;
421 *reloc_addr
= l_addr
+ reloc
->r_addend
;
425 __attribute__ ((always_inline
))
426 elf_machine_lazy_rel (struct link_map
*map
, struct r_scope_elem
*scope
[],
427 Elf64_Addr l_addr
, const Elf64_Rela
*reloc
,
430 Elf64_Addr
*const reloc_addr
= (void *) (l_addr
+ reloc
->r_offset
);
431 const unsigned int r_type
= ELF64_R_TYPE (reloc
->r_info
);
432 /* Check for unexpected PLT reloc type. */
433 if (__glibc_likely (r_type
== R_390_JMP_SLOT
))
435 if (__builtin_expect (map
->l_mach
.plt
, 0) == 0)
436 *reloc_addr
+= l_addr
;
438 *reloc_addr
= map
->l_mach
.plt
+ (reloc
- map
->l_mach
.jmprel
) * 32;
440 else if (__glibc_likely (r_type
== R_390_IRELATIVE
))
442 Elf64_Addr value
= map
->l_addr
+ reloc
->r_addend
;
443 if (__glibc_likely (!skip_ifunc
))
444 value
= elf_ifunc_invoke (value
);
448 _dl_reloc_bad_type (map
, r_type
, 1);
451 #endif /* RESOLVE_MAP */