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