]> git.ipfire.org Git - thirdparty/glibc.git/blob - sysdeps/sh/dl-machine.h
4d5d9353511b59875e442e3b04e8959629b501f1
[thirdparty/glibc.git] / sysdeps / sh / dl-machine.h
1 /* Machine-dependent ELF dynamic relocation inline functions. SH version.
2 Copyright (C) 1999-2020 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
18
19 #ifndef dl_machine_h
20 #define dl_machine_h
21
22 #define ELF_MACHINE_NAME "SH"
23
24 #include <sys/param.h>
25 #include <sysdep.h>
26 #include <assert.h>
27
28 /* Return nonzero iff ELF header is compatible with the running host. */
29 static inline int __attribute__ ((unused))
30 elf_machine_matches_host (const Elf32_Ehdr *ehdr)
31 {
32 return ehdr->e_machine == EM_SH;
33 }
34
35
36 /* Return the link-time address of _DYNAMIC. Conveniently, this is the
37 first element of the GOT. This must be inlined in a function which
38 uses global data. */
39 static inline Elf32_Addr __attribute__ ((unused))
40 elf_machine_dynamic (void)
41 {
42 register Elf32_Addr *got;
43 asm ("mov r12,%0" :"=r" (got));
44 return *got;
45 }
46
47
48 /* Return the run-time load address of the shared object. */
49 static inline Elf32_Addr __attribute__ ((unused))
50 elf_machine_load_address (void)
51 {
52 Elf32_Addr addr;
53 asm ("mov.l 1f,r0\n\
54 mov.l 3f,r2\n\
55 add r12,r2\n\
56 mov.l @(r0,r12),r0\n\
57 bra 2f\n\
58 sub r0,r2\n\
59 .align 2\n\
60 1: .long _dl_start@GOT\n\
61 3: .long _dl_start@GOTOFF\n\
62 2: mov r2,%0"
63 : "=r" (addr) : : "r0", "r1", "r2");
64 return addr;
65 }
66
67
68 /* Set up the loaded object described by L so its unrelocated PLT
69 entries will jump to the on-demand fixup code in dl-runtime.c. */
70
71 static inline int __attribute__ ((unused, always_inline))
72 elf_machine_runtime_setup (struct link_map *l, int lazy, int profile)
73 {
74 Elf32_Addr *got;
75 extern void _dl_runtime_resolve (Elf32_Word);
76 extern void _dl_runtime_profile (Elf32_Word);
77
78 if (l->l_info[DT_JMPREL] && lazy)
79 {
80 /* The GOT entries for functions in the PLT have not yet been filled
81 in. Their initial contents will arrange when called to load an
82 offset into the .rela.plt section and _GLOBAL_OFFSET_TABLE_[1],
83 and then jump to _GLOBAL_OFFSET_TABLE[2]. */
84 got = (Elf32_Addr *) D_PTR (l, l_info[DT_PLTGOT]);
85 /* If a library is prelinked but we have to relocate anyway,
86 we have to be able to undo the prelinking of .got.plt.
87 The prelinker saved us here address of .plt + 36. */
88 if (got[1])
89 {
90 l->l_mach.plt = got[1] + l->l_addr;
91 l->l_mach.gotplt = (Elf32_Addr) &got[3];
92 }
93 got[1] = (Elf32_Addr) l; /* Identify this shared object. */
94
95 /* The got[2] entry contains the address of a function which gets
96 called to get the address of a so far unresolved function and
97 jump to it. The profiling extension of the dynamic linker allows
98 to intercept the calls to collect information. In this case we
99 don't store the address in the GOT so that all future calls also
100 end in this function. */
101 if (profile)
102 {
103 got[2] = (Elf32_Addr) &_dl_runtime_profile;
104 /* Say that we really want profiling and the timers are started. */
105 if (GLRO(dl_profile) != NULL
106 && _dl_name_match_p (GLRO(dl_profile), l))
107 GL(dl_profile_map) = l;
108 }
109 else
110 /* This function will get called to fix up the GOT entry indicated by
111 the offset on the stack, and then jump to the resolved address. */
112 got[2] = (Elf32_Addr) &_dl_runtime_resolve;
113 }
114 return lazy;
115 }
116
117 #define ELF_MACHINE_RUNTIME_FIXUP_ARGS int plt_type
118 #define ELF_MACHINE_RUNTIME_FIXUP_PARAMS plt_type
119
120 /* Mask identifying addresses reserved for the user program,
121 where the dynamic linker should not map anything. */
122 #define ELF_MACHINE_USER_ADDRESS_MASK 0x80000000UL
123
124 /* Initial entry point code for the dynamic linker.
125 The C function `_dl_start' is the real entry point;
126 its return value is the user program's entry point. */
127
128 #define RTLD_START asm ("\
129 .text\n\
130 .globl _start\n\
131 .globl _dl_start_user\n\
132 _start:\n\
133 mov r15,r4\n\
134 mov.l .L_dl_start,r1\n\
135 mova .L_dl_start,r0\n\
136 add r1,r0\n\
137 jsr @r0\n\
138 nop\n\
139 _dl_start_user:\n\
140 ! Save the user entry point address in r8.\n\
141 mov r0,r8\n\
142 ! Point r12 at the GOT.\n\
143 mov.l 1f,r12\n\
144 mova 1f,r0\n\
145 bra 2f\n\
146 add r0,r12\n\
147 .align 2\n\
148 1: .long _GLOBAL_OFFSET_TABLE_\n\
149 2: ! See if we were run as a command with the executable file\n\
150 ! name as an extra leading argument.\n\
151 mov.l .L_dl_skip_args,r0\n\
152 mov.l @(r0,r12),r0\n\
153 mov.l @r0,r0\n\
154 ! Get the original argument count.\n\
155 mov.l @r15,r5\n\
156 ! Subtract _dl_skip_args from it.\n\
157 sub r0,r5\n\
158 ! Adjust the stack pointer to skip _dl_skip_args words.\n\
159 shll2 r0\n\
160 add r0,r15\n\
161 ! Store back the modified argument count.\n\
162 mov.l r5,@r15\n\
163 ! Compute argv address and envp.\n\
164 mov r15,r6\n\
165 add #4,r6\n\
166 mov r5,r7\n\
167 shll2 r7\n\
168 add r15,r7\n\
169 add #8,r7\n\
170 mov.l .L_dl_loaded,r0\n\
171 mov.l @(r0,r12),r0\n\
172 mov.l @r0,r4\n\
173 ! Call _dl_init.\n\
174 mov.l .L_dl_init,r1\n\
175 mova .L_dl_init,r0\n\
176 add r1,r0\n\
177 jsr @r0\n\
178 nop\n\
179 1: ! Pass our finalizer function to the user in r4, as per ELF ABI.\n\
180 mov.l .L_dl_fini,r0\n\
181 mov.l @(r0,r12),r4\n\
182 ! Jump to the user's entry point.\n\
183 jmp @r8\n\
184 nop\n\
185 .align 2\n\
186 .L_dl_start:\n\
187 .long _dl_start@PLT\n\
188 .L_dl_skip_args:\n\
189 .long _dl_skip_args@GOT\n\
190 .L_dl_init:\n\
191 .long _dl_init@PLT\n\
192 .L_dl_loaded:\n\
193 .long _rtld_local@GOT\n\
194 .L_dl_fini:\n\
195 .long _dl_fini@GOT\n\
196 .type __fpscr_values,@object\n\
197 .global __fpscr_values\n\
198 __fpscr_values:\n\
199 .long 0\n\
200 .long 0x80000\n\
201 .weak __fpscr_values\n\
202 .previous\n\
203 ");
204
205 /* ELF_RTYPE_CLASS_PLT iff TYPE describes relocation of a PLT entry or
206 TLS variable, so undefined references should not be allowed to
207 define the value.
208 ELF_RTYPE_CLASS_COPY iff TYPE should not be allowed to resolve to one
209 of the main executable's symbols, as for a COPY reloc. */
210 #define elf_machine_type_class(type) \
211 ((((type) == R_SH_JMP_SLOT || (type) == R_SH_TLS_DTPMOD32 \
212 || (type) == R_SH_TLS_DTPOFF32 || (type) == R_SH_TLS_TPOFF32) \
213 * ELF_RTYPE_CLASS_PLT) \
214 | (((type) == R_SH_COPY) * ELF_RTYPE_CLASS_COPY))
215
216 /* A reloc type used for ld.so cmdline arg lookups to reject PLT entries. */
217 #define ELF_MACHINE_JMP_SLOT R_SH_JMP_SLOT
218
219 /* We define an initialization functions. This is called very early in
220 _dl_sysdep_start. */
221 #define DL_PLATFORM_INIT dl_platform_init ()
222
223 static inline void __attribute__ ((unused))
224 dl_platform_init (void)
225 {
226 if (GLRO(dl_platform) != NULL && *GLRO(dl_platform) == '\0')
227 /* Avoid an empty string which would disturb us. */
228 GLRO(dl_platform) = NULL;
229 }
230
231 static inline Elf32_Addr
232 elf_machine_fixup_plt (struct link_map *map, lookup_t t,
233 const ElfW(Sym) *refsym, const ElfW(Sym) *sym,
234 const Elf32_Rela *reloc,
235 Elf32_Addr *reloc_addr, Elf32_Addr value)
236 {
237 return *reloc_addr = value;
238 }
239
240 /* Return the final value of a plt relocation. */
241 static inline Elf32_Addr
242 elf_machine_plt_value (struct link_map *map, const Elf32_Rela *reloc,
243 Elf32_Addr value)
244 {
245 return value + reloc->r_addend;
246 }
247
248 #define ARCH_LA_PLTENTER sh_gnu_pltenter
249 #define ARCH_LA_PLTEXIT sh_gnu_pltexit
250
251 #endif /* !dl_machine_h */
252
253 /* SH never uses Elf32_Rel relocations. */
254 #define ELF_MACHINE_NO_REL 1
255 #define ELF_MACHINE_NO_RELA 0
256
257 #ifdef RESOLVE_MAP
258
259 /* Perform the relocation specified by RELOC and SYM (which is fully resolved).
260 MAP is the object containing the reloc. */
261
262 auto inline void
263 __attribute ((always_inline))
264 elf_machine_rela (struct link_map *map, const Elf32_Rela *reloc,
265 const Elf32_Sym *sym, const struct r_found_version *version,
266 void *const reloc_addr_arg, int skip_ifunc)
267 {
268 Elf32_Addr *const reloc_addr = reloc_addr_arg;
269 const unsigned int r_type = ELF32_R_TYPE (reloc->r_info);
270 Elf32_Addr value;
271
272 #define COPY_UNALIGNED_WORD(swp, twp, align) \
273 { \
274 void *__s = (swp), *__t = (twp); \
275 unsigned char *__s1 = __s, *__t1 = __t; \
276 unsigned short *__s2 = __s, *__t2 = __t; \
277 unsigned long *__s4 = __s, *__t4 = __t; \
278 switch ((align)) \
279 { \
280 case 0: \
281 *__t4 = *__s4; \
282 break; \
283 case 2: \
284 *__t2++ = *__s2++; \
285 *__t2 = *__s2; \
286 break; \
287 default: \
288 *__t1++ = *__s1++; \
289 *__t1++ = *__s1++; \
290 *__t1++ = *__s1++; \
291 *__t1 = *__s1; \
292 break; \
293 } \
294 }
295
296 if (__glibc_unlikely (r_type == R_SH_RELATIVE))
297 {
298 #ifndef RTLD_BOOTSTRAP
299 if (map != &GL(dl_rtld_map)) /* Already done in rtld itself. */
300 #endif
301 {
302 if (reloc->r_addend)
303 value = map->l_addr + reloc->r_addend;
304 else
305 {
306 COPY_UNALIGNED_WORD (reloc_addr_arg, &value,
307 (int) reloc_addr_arg & 3);
308 value += map->l_addr;
309 }
310 COPY_UNALIGNED_WORD (&value, reloc_addr_arg,
311 (int) reloc_addr_arg & 3);
312 }
313 }
314 #ifndef RTLD_BOOTSTRAP
315 else if (__glibc_unlikely (r_type == R_SH_NONE))
316 return;
317 #endif
318 else
319 {
320 const Elf32_Sym *const refsym = sym;
321 struct link_map *sym_map = RESOLVE_MAP (&sym, version, r_type);
322
323 value = SYMBOL_ADDRESS (sym_map, sym, true);
324 value += reloc->r_addend;
325
326 switch (r_type)
327 {
328 case R_SH_COPY:
329 if (sym == NULL)
330 /* This can happen in trace mode if an object could not be
331 found. */
332 break;
333 if (sym->st_size > refsym->st_size
334 || (sym->st_size < refsym->st_size && GLRO(dl_verbose)))
335 {
336 const char *strtab;
337
338 strtab = (const char *) D_PTR (map, l_info[DT_STRTAB]);
339 _dl_error_printf ("\
340 %s: Symbol `%s' has different size in shared object, consider re-linking\n",
341 RTLD_PROGNAME, strtab + refsym->st_name);
342 }
343 memcpy (reloc_addr_arg, (void *) value,
344 MIN (sym->st_size, refsym->st_size));
345 break;
346 case R_SH_GLOB_DAT:
347 case R_SH_JMP_SLOT:
348 /* These addresses are always aligned. */
349 *reloc_addr = value;
350 break;
351 /* XXX Remove TLS relocations which are not needed. */
352 case R_SH_TLS_DTPMOD32:
353 #ifdef RTLD_BOOTSTRAP
354 /* During startup the dynamic linker is always the module
355 with index 1.
356 XXX If this relocation is necessary move before RESOLVE
357 call. */
358 *reloc_addr = 1;
359 #else
360 /* Get the information from the link map returned by the
361 resolv function. */
362 if (sym_map != NULL)
363 *reloc_addr = sym_map->l_tls_modid;
364 #endif
365 break;
366 case R_SH_TLS_DTPOFF32:
367 #ifndef RTLD_BOOTSTRAP
368 /* During relocation all TLS symbols are defined and used.
369 Therefore the offset is already correct. */
370 if (sym != NULL)
371 *reloc_addr = sym->st_value;
372 #endif
373 break;
374 case R_SH_TLS_TPOFF32:
375 /* The offset is positive, afterward from the thread pointer. */
376 #ifdef RTLD_BOOTSTRAP
377 *reloc_addr = map->l_tls_offset + sym->st_value + reloc->r_addend;
378 #else
379 /* We know the offset of object the symbol is contained in.
380 It is a positive value which will be added to the thread
381 pointer. To get the variable position in the TLS block
382 we add the offset from that of the TLS block. */
383 if (sym != NULL)
384 {
385 CHECK_STATIC_TLS (map, sym_map);
386 *reloc_addr = sym_map->l_tls_offset + sym->st_value
387 + reloc->r_addend;
388 }
389 #endif
390 break;
391 case R_SH_DIR32:
392 {
393 #if !defined RTLD_BOOTSTRAP && !defined RESOLVE_CONFLICT_FIND_MAP
394 /* This is defined in rtld.c, but nowhere in the static
395 libc.a; make the reference weak so static programs can
396 still link. This declaration cannot be done when
397 compiling rtld.c (i.e. #ifdef RTLD_BOOTSTRAP) because
398 rtld.c contains the common defn for _dl_rtld_map, which
399 is incompatible with a weak decl in the same file. */
400 # ifndef SHARED
401 weak_extern (_dl_rtld_map);
402 # endif
403 if (map == &GL(dl_rtld_map))
404 /* Undo the relocation done here during bootstrapping.
405 Now we will relocate it anew, possibly using a
406 binding found in the user program or a loaded library
407 rather than the dynamic linker's built-in definitions
408 used while loading those libraries. */
409 value -= SYMBOL_ADDRESS (map, refsym, true) + reloc->r_addend;
410 #endif
411 COPY_UNALIGNED_WORD (&value, reloc_addr_arg,
412 (int) reloc_addr_arg & 3);
413 break;
414 }
415 case R_SH_REL32:
416 value = (value - (Elf32_Addr) reloc_addr);
417 COPY_UNALIGNED_WORD (&value, reloc_addr_arg,
418 (int) reloc_addr_arg & 3);
419 break;
420 default:
421 _dl_reloc_bad_type (map, r_type, 0);
422 break;
423 }
424 }
425 }
426
427 auto inline void
428 __attribute__ ((always_inline))
429 elf_machine_rela_relative (Elf32_Addr l_addr, const Elf32_Rela *reloc,
430 void *const reloc_addr_arg)
431 {
432 Elf32_Addr value;
433
434 if (reloc->r_addend)
435 value = l_addr + reloc->r_addend;
436 else
437 {
438 COPY_UNALIGNED_WORD (reloc_addr_arg, &value, (int) reloc_addr_arg & 3);
439 value += l_addr;
440 }
441 COPY_UNALIGNED_WORD (&value, reloc_addr_arg, (int) reloc_addr_arg & 3);
442
443 #undef COPY_UNALIGNED_WORD
444 }
445
446 auto inline void
447 __attribute__ ((always_inline))
448 elf_machine_lazy_rel (struct link_map *map,
449 Elf32_Addr l_addr, const Elf32_Rela *reloc,
450 int skip_ifunc)
451 {
452 Elf32_Addr *const reloc_addr = (void *) (l_addr + reloc->r_offset);
453 /* Check for unexpected PLT reloc type. */
454 if (ELF32_R_TYPE (reloc->r_info) == R_SH_JMP_SLOT)
455 {
456 if (__builtin_expect (map->l_mach.plt, 0) == 0)
457 *reloc_addr += l_addr;
458 else
459 *reloc_addr =
460 map->l_mach.plt
461 + (((Elf32_Addr) reloc_addr) - map->l_mach.gotplt) * 7;
462 }
463 else
464 _dl_reloc_bad_type (map, ELF32_R_TYPE (reloc->r_info), 1);
465 }
466
467 #endif /* RESOLVE_MAP */