]>
Commit | Line | Data |
---|---|---|
ffeac417 UD |
1 | /* Machine-dependent ELF dynamic relocation inline functions. |
2 | 64 bit S/390 Version. | |
3 | Copyright (C) 2001 Free Software Foundation, Inc. | |
4 | Contributed by Martin Schwidefsky (schwidefsky@de.ibm.com). | |
847b055c AJ |
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 | |
41bdb6e2 AJ |
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. | |
847b055c AJ |
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 | |
41bdb6e2 | 15 | Lesser General Public License for more details. |
847b055c | 16 | |
41bdb6e2 AJ |
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. */ | |
847b055c AJ |
21 | |
22 | #ifndef dl_machine_h | |
23 | #define dl_machine_h | |
1721af3f | 24 | |
ffeac417 | 25 | #define ELF_MACHINE_NAME "s390x" |
847b055c AJ |
26 | |
27 | #include <sys/param.h> | |
28 | #include <string.h> | |
29 | #include <link.h> | |
30 | ||
9672b937 AJ |
31 | /* This is an older, now obsolete value. */ |
32 | #define EM_S390_OLD 0xA390 | |
847b055c | 33 | |
ffeac417 | 34 | /* Return nonzero iff E_MACHINE is compatible with the running host. */ |
847b055c | 35 | static inline int |
ffeac417 | 36 | elf_machine_matches_host (const Elf64_Ehdr *ehdr) |
847b055c | 37 | { |
ffeac417 UD |
38 | return (ehdr->e_machine == EM_S390 || ehdr->e_machine == EM_S390_OLD) |
39 | && ehdr->e_ident[EI_CLASS] == ELFCLASS64; | |
847b055c AJ |
40 | } |
41 | ||
847b055c AJ |
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 | ||
ffeac417 | 46 | static inline Elf64_Addr |
847b055c AJ |
47 | elf_machine_dynamic (void) |
48 | { | |
ffeac417 | 49 | register Elf64_Addr *got; |
847b055c | 50 | |
ffeac417 | 51 | asm( " larl %0,_GLOBAL_OFFSET_TABLE_\n" |
847b055c AJ |
52 | : "=&a" (got) : : "0" ); |
53 | ||
54 | return *got; | |
55 | } | |
56 | ||
847b055c | 57 | /* Return the run-time load address of the shared object. */ |
ffeac417 | 58 | static inline Elf64_Addr |
847b055c AJ |
59 | elf_machine_load_address (void) |
60 | { | |
ffeac417 UD |
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" ); | |
847b055c AJ |
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 | { | |
ffeac417 UD |
77 | extern void _dl_runtime_resolve (Elf64_Word); |
78 | extern void _dl_runtime_profile (Elf64_Word); | |
847b055c | 79 | |
1721af3f | 80 | if (l->l_info[DT_JMPREL] && lazy) |
847b055c AJ |
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 | |
ffeac417 | 84 | offset into the .rela.plt section, push _GLOBAL_OFFSET_TABLE_[1], |
847b055c | 85 | and then jump to _GLOBAL_OFFSET_TABLE[2]. */ |
ffeac417 UD |
86 | Elf64_Addr *got; |
87 | got = (Elf64_Addr *) D_PTR (l, l_info[DT_PLTGOT]); | |
88 | got[1] = (Elf64_Addr) l; /* Identify this shared object. */ | |
847b055c AJ |
89 | |
90 | /* The got[2] entry contains the address of a function which gets | |
91 | called to get the address of a so far unresolved function and | |
92 | jump to it. The profiling extension of the dynamic linker allows | |
ffeac417 | 93 | to intercept the calls to collect information. In this case we |
847b055c | 94 | don't store the address in the GOT so that all future calls also |
ffeac417 | 95 | end in this function. */ |
847b055c AJ |
96 | if (__builtin_expect (profile, 0)) |
97 | { | |
ffeac417 | 98 | got[2] = (Elf64_Addr) &_dl_runtime_profile; |
1721af3f | 99 | |
847b055c AJ |
100 | if (_dl_name_match_p (_dl_profile, l)) |
101 | /* This is the object we are looking for. Say that we really | |
102 | want profiling and the timers are started. */ | |
103 | _dl_profile_map = l; | |
104 | } | |
105 | else | |
106 | /* This function will get called to fix up the GOT entry indicated by | |
107 | the offset on the stack, and then jump to the resolved address. */ | |
ffeac417 | 108 | got[2] = (Elf64_Addr) &_dl_runtime_resolve; |
847b055c AJ |
109 | } |
110 | ||
111 | return lazy; | |
112 | } | |
113 | ||
114 | /* This code is used in dl-runtime.c to call the `fixup' function | |
ffeac417 | 115 | and then redirect to the address it returns. */ |
847b055c AJ |
116 | |
117 | /* s390: | |
118 | Arguments are in register. | |
119 | r2 - r7 holds the original parameters for the function call, fixup | |
120 | and trampoline code use r0-r5 and r14-15. For the correct function | |
121 | call r2-r5 and r14-15 must be restored. | |
ffeac417 | 122 | Arguments from the PLT are stored at 48(r15) and 56(r15) |
847b055c AJ |
123 | and must be moved to r2 and r3 for the fixup call (see elf32-s390.c |
124 | in the binutils for the PLT code). | |
125 | Fixup function address in r2. | |
126 | */ | |
127 | #ifndef PROF | |
128 | #define ELF_MACHINE_RUNTIME_TRAMPOLINE \ | |
129 | asm ( "\ | |
130 | .text\n\ | |
131 | .globl _dl_runtime_resolve\n\ | |
132 | .type _dl_runtime_resolve, @function\n\ | |
133 | .align 16\n\ | |
134 | _dl_runtime_resolve:\n\ | |
135 | # save registers\n\ | |
ffeac417 UD |
136 | stmg 2,5,64(15)\n\ |
137 | stg 14,96(15)\n\ | |
138 | lgr 0,15\n\ | |
139 | aghi 15,-160\n\ | |
140 | stg 0,0(15)\n\ | |
847b055c | 141 | # load args saved by PLT\n\ |
ffeac417 UD |
142 | lmg 2,3,208(15)\n\ |
143 | brasl 14,fixup # call fixup | |
144 | lgr 1,2 # function addr returned in r2\n\ | |
847b055c | 145 | # restore registers\n\ |
ffeac417 UD |
146 | aghi 15,160\n\ |
147 | lg 14,96(15)\n\ | |
148 | lmg 2,5,64(15)\n\ | |
149 | br 1\n\ | |
847b055c AJ |
150 | .size _dl_runtime_resolve, .-_dl_runtime_resolve\n\ |
151 | \n\ | |
152 | .globl _dl_runtime_profile\n\ | |
153 | .type _dl_runtime_profile, @function\n\ | |
154 | .align 16\n\ | |
155 | _dl_runtime_profile:\n\ | |
156 | # save registers\n\ | |
ffeac417 UD |
157 | stmg 2,5,64(15)\n\ |
158 | stg 14,96(15)\n\ | |
159 | lgr 0,15\n\ | |
160 | aghi 15,-160\n\ | |
161 | stg 0,0(15)\n\ | |
847b055c | 162 | # load args saved by PLT\n\ |
ffeac417 | 163 | lmg 2,3,208(15)\n\ |
847b055c | 164 | # load return address as third parameter\n\ |
ffeac417 UD |
165 | lgr 4,14\n\ |
166 | brasl 14,profile_fixup # call fixup\n\ | |
167 | lgr 1,2 # function addr returned in r2\n\ | |
847b055c | 168 | # restore registers\n\ |
ffeac417 UD |
169 | aghi 15,160\n\ |
170 | lg 14,96(15)\n\ | |
171 | lmg 2,5,64(15)\n\ | |
172 | br 1\n\ | |
847b055c AJ |
173 | .size _dl_runtime_profile, .-_dl_runtime_profile\n\ |
174 | "); | |
175 | #else | |
176 | #define ELF_MACHINE_RUNTIME_TRAMPOLINE \ | |
177 | asm ( "\ | |
178 | .text\n\ | |
179 | .globl _dl_runtime_resolve\n\ | |
180 | .globl _dl_runtime_profile\n\ | |
181 | .type _dl_runtime_resolve, @function\n\ | |
182 | .type _dl_runtime_profile, @function\n\ | |
183 | .align 16\n\ | |
184 | _dl_runtime_resolve:\n\ | |
185 | _dl_runtime_profile:\n\ | |
186 | # save registers\n\ | |
ffeac417 UD |
187 | stmg 2,5,64(15)\n\ |
188 | stg 14,96(15)\n\ | |
189 | lgr 0,15\n\ | |
190 | aghi 15,-160\n\ | |
191 | stg 0,0(15)\n\ | |
847b055c | 192 | # load args saved by PLT\n\ |
ffeac417 | 193 | lmg 2,3,208(15)\n\ |
847b055c | 194 | # load return address as third parameter\n\ |
ffeac417 UD |
195 | lgr 4,14\n\ |
196 | brasl 14,profile_fixup # call fixup\n\ | |
197 | lgr 1,2 # function addr returned in r2\n\ | |
847b055c | 198 | # restore registers\n\ |
ffeac417 UD |
199 | aghi 15,160\n\ |
200 | lg 14,96(15)\n\ | |
201 | lmg 2,5,64(15)\n\ | |
202 | br 1\n\ | |
847b055c AJ |
203 | .size _dl_runtime_resolve, .-_dl_runtime_resolve\n\ |
204 | .size _dl_runtime_profile, .-_dl_runtime_profile\n\ | |
205 | "); | |
1721af3f | 206 | #endif |
847b055c AJ |
207 | |
208 | /* Initial entry point code for the dynamic linker. | |
209 | The C function `_dl_start' is the real entry point; | |
ffeac417 | 210 | its return value is the user program's entry point. */ |
847b055c AJ |
211 | |
212 | #define RTLD_START asm ("\n\ | |
213 | .text\n\ | |
214 | .align 4\n\ | |
215 | .globl _start\n\ | |
216 | .globl _dl_start_user\n\ | |
217 | _start:\n\ | |
ffeac417 | 218 | lgr %r2,%r15\n\ |
847b055c | 219 | # Alloc stack frame\n\ |
ffeac417 | 220 | aghi %r15,-160\n\ |
847b055c | 221 | # Set the back chain to zero\n\ |
ffeac417 | 222 | xc 0(8,%r15),0(%r15)\n\ |
847b055c | 223 | # Call _dl_start with %r2 pointing to arg on stack\n\ |
ffeac417 | 224 | brasl %r14,_dl_start # call _dl_start\n\ |
847b055c AJ |
225 | _dl_start_user:\n\ |
226 | # Save the user entry point address in %r8.\n\ | |
ffeac417 | 227 | lgr %r8,%r2\n\ |
847b055c | 228 | # Point %r12 at the GOT.\n\ |
ffeac417 | 229 | larl %r12,_GLOBAL_OFFSET_TABLE_\n\ |
847b055c | 230 | # Store the highest stack address\n\ |
ffeac417 UD |
231 | lghi %r1,__libc_stack_end@GOT |
232 | lg %r1,0(%r1,%r12)\n\ | |
233 | stg %r15, 0(%r1)\n\ | |
847b055c AJ |
234 | # See if we were run as a command with the executable file\n\ |
235 | # name as an extra leading argument.\n\ | |
ffeac417 UD |
236 | lghi %r1,_dl_skip_args@GOT |
237 | lg %r1,0(%r1,%r12)\n\ | |
238 | lgf %r1,0(%r1) # load _dl_skip_args\n\ | |
847b055c | 239 | # Get the original argument count.\n\ |
ffeac417 | 240 | lg %r0,160(%r15)\n\ |
847b055c | 241 | # Subtract _dl_skip_args from it.\n\ |
ffeac417 | 242 | sgr %r0,%r1\n\ |
847b055c | 243 | # Adjust the stack pointer to skip _dl_skip_args words.\n\ |
ffeac417 UD |
244 | sllg %r1,%r1,3\n\ |
245 | agr %r15,%r1\n\ | |
847b055c | 246 | # Set the back chain to zero again\n\ |
ffeac417 | 247 | xc 0(8,%r15),0(%r15)\n\ |
847b055c | 248 | # Store back the modified argument count.\n\ |
ffeac417 | 249 | stg %r0,160(%r15)\n\ |
847b055c AJ |
250 | # The special initializer gets called with the stack just\n\ |
251 | # as the application's entry point will see it; it can\n\ | |
252 | # switch stacks if it moves these contents over.\n\ | |
253 | " RTLD_START_SPECIAL_INIT "\n\ | |
254 | # Call the function to run the initializers.\n\ | |
255 | # Load the parameters:\n\ | |
256 | # (%r2, %r3, %r4, %r5) = (_dl_loaded, argc, argv, envp)\n\ | |
ffeac417 UD |
257 | lghi %r2,_dl_loaded@GOT |
258 | lg %r2,0(%r2,%r12)\n\ | |
259 | lg %r2,0(%r2)\n\ | |
260 | lg %r3,160(%r15)\n\ | |
261 | la %r4,168(%r15)\n\ | |
262 | lgr %r5,%r3\n\ | |
263 | sllg %r5,%r5,3\n\ | |
264 | la %r5,176(%r5,%r15)\n\ | |
265 | brasl %r14,_dl_init@PLT\n | |
847b055c | 266 | # Pass our finalizer function to the user in %r14, as per ELF ABI.\n\ |
ffeac417 UD |
267 | lghi %r14,_dl_fini@GOT |
268 | lg %r14,0(%r14,%r12)\n\ | |
847b055c | 269 | # Free stack frame\n\ |
ffeac417 | 270 | aghi %r15,160\n\ |
847b055c AJ |
271 | # Jump to the user's entry point (saved in %r8).\n\ |
272 | br %r8\n\ | |
847b055c AJ |
273 | "); |
274 | ||
275 | #ifndef RTLD_START_SPECIAL_INIT | |
276 | #define RTLD_START_SPECIAL_INIT /* nothing */ | |
277 | #endif | |
278 | ||
cf5a372e UD |
279 | /* ELF_RTYPE_CLASS_PLT iff TYPE describes relocation of a PLT entry, so |
280 | PLT entries should not be allowed to define the value. | |
281 | ELF_RTYPE_CLASS_NOCOPY iff TYPE should not be allowed to resolve to one | |
282 | of the main executable's symbols, as for a COPY reloc. */ | |
283 | #define elf_machine_type_class(type) \ | |
284 | ((((type) == R_390_JMP_SLOT) * ELF_RTYPE_CLASS_PLT) \ | |
285 | | (((type) == R_390_COPY) * ELF_RTYPE_CLASS_COPY)) | |
847b055c AJ |
286 | |
287 | /* A reloc type used for ld.so cmdline arg lookups to reject PLT entries. */ | |
1721af3f | 288 | #define ELF_MACHINE_JMP_SLOT R_390_JMP_SLOT |
847b055c | 289 | |
ffeac417 | 290 | /* The 64 bit S/390 never uses Elf64_Rel relocations. */ |
847b055c AJ |
291 | #define ELF_MACHINE_NO_REL 1 |
292 | ||
847b055c AJ |
293 | /* We define an initialization functions. This is called very early in |
294 | _dl_sysdep_start. */ | |
295 | #define DL_PLATFORM_INIT dl_platform_init () | |
296 | ||
297 | extern const char *_dl_platform; | |
298 | ||
299 | static inline void __attribute__ ((unused)) | |
300 | dl_platform_init (void) | |
301 | { | |
302 | if (_dl_platform != NULL && *_dl_platform == '\0') | |
303 | /* Avoid an empty string which would disturb us. */ | |
304 | _dl_platform = NULL; | |
305 | } | |
306 | ||
ffeac417 | 307 | static inline Elf64_Addr |
847b055c | 308 | elf_machine_fixup_plt (struct link_map *map, lookup_t t, |
ffeac417 UD |
309 | const Elf64_Rela *reloc, |
310 | Elf64_Addr *reloc_addr, Elf64_Addr value) | |
847b055c AJ |
311 | { |
312 | return *reloc_addr = value; | |
313 | } | |
314 | ||
ffeac417 UD |
315 | /* Return the final value of a plt relocation. */ |
316 | static inline Elf64_Addr | |
317 | elf_machine_plt_value (struct link_map *map, const Elf64_Rela *reloc, | |
318 | Elf64_Addr value) | |
847b055c AJ |
319 | { |
320 | return value; | |
1721af3f | 321 | } |
847b055c | 322 | |
1721af3f | 323 | #endif /* !dl_machine_h */ |
847b055c AJ |
324 | |
325 | #ifdef RESOLVE | |
326 | ||
327 | /* Perform the relocation specified by RELOC and SYM (which is fully resolved). | |
328 | MAP is the object containing the reloc. */ | |
329 | ||
330 | static inline void | |
ffeac417 UD |
331 | elf_machine_rela (struct link_map *map, const Elf64_Rela *reloc, |
332 | const Elf64_Sym *sym, const struct r_found_version *version, | |
333 | Elf64_Addr *const reloc_addr) | |
847b055c | 334 | { |
1721af3f UD |
335 | const unsigned int r_type = ELF64_R_TYPE (reloc->r_info); |
336 | ||
337 | if (__builtin_expect (r_type == R_390_RELATIVE, 0)) | |
338 | *reloc_addr = map->l_addr + reloc->r_addend; | |
847b055c | 339 | #ifndef RTLD_BOOTSTRAP |
1721af3f UD |
340 | else if (__builtin_expect (r_type == R_390_NONE, 0)) |
341 | return; | |
847b055c | 342 | #endif |
1721af3f | 343 | else |
847b055c | 344 | { |
ffeac417 | 345 | const Elf64_Sym *const refsym = sym; |
1721af3f | 346 | Elf64_Addr value = RESOLVE (&sym, version, r_type); |
847b055c AJ |
347 | if (sym) |
348 | value += sym->st_value; | |
1721af3f UD |
349 | |
350 | switch (r_type) | |
847b055c | 351 | { |
ffeac417 UD |
352 | case R_390_GLOB_DAT: |
353 | case R_390_JMP_SLOT: | |
354 | *reloc_addr = value + reloc->r_addend; | |
355 | break; | |
356 | #ifndef RTLD_BOOTSTRAP | |
847b055c AJ |
357 | case R_390_COPY: |
358 | if (sym == NULL) | |
359 | /* This can happen in trace mode if an object could not be | |
360 | found. */ | |
361 | break; | |
362 | if (__builtin_expect (sym->st_size > refsym->st_size, 0) | |
363 | || (__builtin_expect (sym->st_size < refsym->st_size, 0) | |
364 | && __builtin_expect (_dl_verbose, 0))) | |
365 | { | |
366 | const char *strtab; | |
367 | ||
ffeac417 | 368 | strtab = (const char *) D_PTR (map,l_info[DT_STRTAB]); |
35fc382a UD |
369 | _dl_error_printf ("\ |
370 | %s: Symbol `%s' has different size in shared object, consider re-linking\n", | |
371 | _dl_argv[0] ?: "<program name unknown>", | |
372 | strtab + refsym->st_name); | |
847b055c AJ |
373 | } |
374 | memcpy (reloc_addr, (void *) value, MIN (sym->st_size, | |
375 | refsym->st_size)); | |
1721af3f | 376 | break; |
ffeac417 UD |
377 | case R_390_64: |
378 | *reloc_addr = value + reloc->r_addend; | |
847b055c AJ |
379 | break; |
380 | case R_390_32: | |
ffeac417 UD |
381 | *(unsigned int *) reloc_addr = value + reloc->r_addend; |
382 | break; | |
383 | case R_390_16: | |
384 | *(unsigned short *) reloc_addr = value + reloc->r_addend; | |
385 | break; | |
386 | case R_390_8: | |
387 | *(char *) reloc_addr = value + reloc->r_addend; | |
388 | break; | |
389 | case R_390_PC64: | |
390 | *reloc_addr = value +reloc->r_addend - (Elf64_Addr) reloc_addr; | |
391 | break; | |
392 | case R_390_PC32DBL: | |
393 | case R_390_PLT32DBL: | |
394 | *(unsigned int *) reloc_addr = (unsigned int) | |
395 | ((int) (value + reloc->r_addend - (Elf64_Addr) reloc_addr) >> 1); | |
396 | break; | |
847b055c | 397 | case R_390_PC32: |
1721af3f | 398 | *(unsigned int *) reloc_addr = |
ffeac417 UD |
399 | value + reloc->r_addend - (Elf64_Addr) reloc_addr; |
400 | break; | |
401 | case R_390_PC16DBL: | |
402 | case R_390_PLT16DBL: | |
403 | *(unsigned short *) reloc_addr = (unsigned short) | |
404 | ((short) (value + reloc->r_addend - (Elf64_Addr) reloc_addr) >> 1); | |
847b055c | 405 | break; |
ffeac417 | 406 | case R_390_PC16: |
1721af3f | 407 | *(unsigned short *) reloc_addr = |
ffeac417 | 408 | value + reloc->r_addend - (Elf64_Addr) reloc_addr; |
847b055c | 409 | break; |
ffeac417 UD |
410 | #endif |
411 | #if !defined(RTLD_BOOTSTRAP) || defined(_NDEBUG) | |
847b055c | 412 | default: |
ffeac417 UD |
413 | /* We add these checks in the version to relocate ld.so only |
414 | if we are still debugging. */ | |
1721af3f | 415 | _dl_reloc_bad_type (map, r_type, 0); |
847b055c | 416 | break; |
ffeac417 | 417 | #endif |
847b055c AJ |
418 | } |
419 | } | |
1721af3f UD |
420 | } |
421 | ||
422 | static inline void | |
7c69dc8b UD |
423 | elf_machine_rela_relative (Elf64_Addr l_addr, const Elf64_Rela *reloc, |
424 | Elf64_Addr *const reloc_addr) | |
1721af3f UD |
425 | { |
426 | *reloc_addr = l_addr + reloc->r_addend; | |
427 | } | |
847b055c AJ |
428 | |
429 | static inline void | |
430 | elf_machine_lazy_rel (struct link_map *map, | |
ffeac417 | 431 | Elf64_Addr l_addr, const Elf64_Rela *reloc) |
847b055c | 432 | { |
ffeac417 | 433 | Elf64_Addr *const reloc_addr = (void *) (l_addr + reloc->r_offset); |
1721af3f | 434 | const unsigned int r_type = ELF32_R_TYPE (reloc->r_info); |
847b055c | 435 | /* Check for unexpected PLT reloc type. */ |
1721af3f | 436 | if (__builtin_expect (r_type == R_390_JMP_SLOT, 1)) |
847b055c AJ |
437 | *reloc_addr += l_addr; |
438 | else | |
1721af3f | 439 | _dl_reloc_bad_type (map, r_type, 1); |
847b055c AJ |
440 | } |
441 | ||
442 | #endif /* RESOLVE */ |