Warning: This file is not a C or C++ file. It does not have highlighting.

1/* Machine-dependent ELF dynamic relocation inline functions. SH version.
2 Copyright (C) 1999-2022 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#include <dl-static-tls.h>
28#include <dl-machine-rel.h>
29
30/* Return nonzero iff ELF header is compatible with the running host. */
31static inline int __attribute__ ((unused))
32elf_machine_matches_host (const Elf32_Ehdr *ehdr)
33{
34 return ehdr->e_machine == EM_SH;
35}
36
37
38/* Return the link-time address of _DYNAMIC. Conveniently, this is the
39 first element of the GOT. This must be inlined in a function which
40 uses global data. */
41static inline Elf32_Addr __attribute__ ((unused))
42elf_machine_dynamic (void)
43{
44 register Elf32_Addr *got;
45 asm ("mov r12,%0" :"=r" (got));
46 return *got;
47}
48
49
50/* Return the run-time load address of the shared object. */
51static inline Elf32_Addr __attribute__ ((unused))
52elf_machine_load_address (void)
53{
54 Elf32_Addr addr;
55 asm ("mov.l 1f,r0\n\
56 mov.l 3f,r2\n\
57 add r12,r2\n\
58 mov.l @(r0,r12),r0\n\
59 bra 2f\n\
60 sub r0,r2\n\
61 .align 2\n\
62 1: .long _dl_start@GOT\n\
63 3: .long _dl_start@GOTOFF\n\
64 2: mov r2,%0"
65 : "=r" (addr) : : "r0", "r1", "r2");
66 return addr;
67}
68
69
70/* Set up the loaded object described by L so its unrelocated PLT
71 entries will jump to the on-demand fixup code in dl-runtime.c. */
72
73static inline int __attribute__ ((unused, always_inline))
74elf_machine_runtime_setup (struct link_map *l, struct r_scope_elem *scope[],
75 int lazy, int profile)
76{
77 Elf32_Addr *got;
78 extern void _dl_runtime_resolve (Elf32_Word);
79 extern void _dl_runtime_profile (Elf32_Word);
80
81 if (l->l_info[DT_JMPREL] && lazy)
82 {
83 /* The GOT entries for functions in the PLT have not yet been filled
84 in. Their initial contents will arrange when called to load an
85 offset into the .rela.plt section and _GLOBAL_OFFSET_TABLE_[1],
86 and then jump to _GLOBAL_OFFSET_TABLE[2]. */
87 got = (Elf32_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 + 36. */
91 if (got[1])
92 {
93 l->l_mach.plt = got[1] + l->l_addr;
94 l->l_mach.gotplt = (Elf32_Addr) &got[3];
95 }
96 got[1] = (Elf32_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 (profile)
105 {
106 got[2] = (Elf32_Addr) &_dl_runtime_profile;
107 /* Say that we really want profiling and the timers are started. */
108 if (GLRO(dl_profile) != NULL
109 && _dl_name_match_p (GLRO(dl_profile), l))
110 GL(dl_profile_map) = l;
111 }
112 else
113 /* This function will get called to fix up the GOT entry indicated by
114 the offset on the stack, and then jump to the resolved address. */
115 got[2] = (Elf32_Addr) &_dl_runtime_resolve;
116 }
117 return lazy;
118}
119
120#define ELF_MACHINE_RUNTIME_FIXUP_ARGS int plt_type
121#define ELF_MACHINE_RUNTIME_FIXUP_PARAMS plt_type
122
123/* Mask identifying addresses reserved for the user program,
124 where the dynamic linker should not map anything. */
125#define ELF_MACHINE_USER_ADDRESS_MASK 0x80000000UL
126
127/* Initial entry point code for the dynamic linker.
128 The C function `_dl_start' is the real entry point;
129 its return value is the user program's entry point. */
130
131#define RTLD_START asm ("\
132.text\n\
133.globl _start\n\
134.globl _dl_start_user\n\
135_start:\n\
136 mov r15,r4\n\
137 mov.l .L_dl_start,r1\n\
138 mova .L_dl_start,r0\n\
139 add r1,r0\n\
140 jsr @r0\n\
141 nop\n\
142_dl_start_user:\n\
143 ! Save the user entry point address in r8.\n\
144 mov r0,r8\n\
145 ! Point r12 at the GOT.\n\
146 mov.l 1f,r12\n\
147 mova 1f,r0\n\
148 bra 2f\n\
149 add r0,r12\n\
150 .align 2\n\
1511: .long _GLOBAL_OFFSET_TABLE_\n\
1522: ! See if we were run as a command with the executable file\n\
153 ! name as an extra leading argument.\n\
154 mov.l .L_dl_skip_args,r0\n\
155 mov.l @(r0,r12),r0\n\
156 mov.l @r0,r0\n\
157 ! Get the original argument count.\n\
158 mov.l @r15,r5\n\
159 ! Subtract _dl_skip_args from it.\n\
160 sub r0,r5\n\
161 ! Adjust the stack pointer to skip _dl_skip_args words.\n\
162 shll2 r0\n\
163 add r0,r15\n\
164 ! Store back the modified argument count.\n\
165 mov.l r5,@r15\n\
166 ! Compute argv address and envp.\n\
167 mov r15,r6\n\
168 add #4,r6\n\
169 mov r5,r7\n\
170 shll2 r7\n\
171 add r15,r7\n\
172 add #8,r7\n\
173 mov.l .L_dl_loaded,r0\n\
174 mov.l @(r0,r12),r0\n\
175 mov.l @r0,r4\n\
176 ! Call _dl_init.\n\
177 mov.l .L_dl_init,r1\n\
178 mova .L_dl_init,r0\n\
179 add r1,r0\n\
180 jsr @r0\n\
181 nop\n\
1821: ! Pass our finalizer function to the user in r4, as per ELF ABI.\n\
183 mov.l .L_dl_fini,r0\n\
184 mov.l @(r0,r12),r4\n\
185 ! Jump to the user's entry point.\n\
186 jmp @r8\n\
187 nop\n\
188 .align 2\n\
189.L_dl_start:\n\
190 .long _dl_start@PLT\n\
191.L_dl_skip_args:\n\
192 .long _dl_skip_args@GOT\n\
193.L_dl_init:\n\
194 .long _dl_init@PLT\n\
195.L_dl_loaded:\n\
196 .long _rtld_local@GOT\n\
197.L_dl_fini:\n\
198 .long _dl_fini@GOT\n\
199 .type __fpscr_values,@object\n\
200 .global __fpscr_values\n\
201__fpscr_values:\n\
202 .long 0\n\
203 .long 0x80000\n\
204 .weak __fpscr_values\n\
205.previous\n\
206");
207
208/* ELF_RTYPE_CLASS_PLT iff TYPE describes relocation of a PLT entry or
209 TLS variable, so undefined references should not be allowed to
210 define the value.
211 ELF_RTYPE_CLASS_COPY iff TYPE should not be allowed to resolve to one
212 of the main executable's symbols, as for a COPY reloc. */
213#define elf_machine_type_class(type) \
214 ((((type) == R_SH_JMP_SLOT || (type) == R_SH_TLS_DTPMOD32 \
215 || (type) == R_SH_TLS_DTPOFF32 || (type) == R_SH_TLS_TPOFF32) \
216 * ELF_RTYPE_CLASS_PLT) \
217 | (((type) == R_SH_COPY) * ELF_RTYPE_CLASS_COPY))
218
219/* A reloc type used for ld.so cmdline arg lookups to reject PLT entries. */
220#define ELF_MACHINE_JMP_SLOT R_SH_JMP_SLOT
221
222/* We define an initialization functions. This is called very early in
223 _dl_sysdep_start. */
224#define DL_PLATFORM_INIT dl_platform_init ()
225
226static inline void __attribute__ ((unused))
227dl_platform_init (void)
228{
229 if (GLRO(dl_platform) != NULL && *GLRO(dl_platform) == '\0')
230 /* Avoid an empty string which would disturb us. */
231 GLRO(dl_platform) = NULL;
232}
233
234static inline Elf32_Addr
235elf_machine_fixup_plt (struct link_map *map, lookup_t t,
236 const ElfW(Sym) *refsym, const ElfW(Sym) *sym,
237 const Elf32_Rela *reloc,
238 Elf32_Addr *reloc_addr, Elf32_Addr value)
239{
240 return *reloc_addr = value;
241}
242
243/* Return the final value of a plt relocation. */
244static inline Elf32_Addr
245elf_machine_plt_value (struct link_map *map, const Elf32_Rela *reloc,
246 Elf32_Addr value)
247{
248 return value + reloc->r_addend;
249}
250
251#define ARCH_LA_PLTENTER sh_gnu_pltenter
252#define ARCH_LA_PLTEXIT sh_gnu_pltexit
253
254#endif /* !dl_machine_h */
255
256#ifdef RESOLVE_MAP
257
258/* Perform the relocation specified by RELOC and SYM (which is fully resolved).
259 MAP is the object containing the reloc. */
260
261static inline void
262__attribute ((always_inline))
263elf_machine_rela (struct link_map *map, struct r_scope_elem *scope[],
264 const Elf32_Rela *reloc, const Elf32_Sym *sym,
265 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 (map, scope, &sym, version,
322 r_type);
323
324 value = SYMBOL_ADDRESS (sym_map, sym, true);
325 value += reloc->r_addend;
326
327 switch (r_type)
328 {
329 case R_SH_COPY:
330 if (sym == NULL)
331 /* This can happen in trace mode if an object could not be
332 found. */
333 break;
334 if (sym->st_size > refsym->st_size
335 || (sym->st_size < refsym->st_size && GLRO(dl_verbose)))
336 {
337 const char *strtab;
338
339 strtab = (const char *) D_PTR (map, l_info[DT_STRTAB]);
340 _dl_error_printf ("\
341%s: Symbol `%s' has different size in shared object, consider re-linking\n",
342 RTLD_PROGNAME, strtab + refsym->st_name);
343 }
344 memcpy (reloc_addr_arg, (void *) value,
345 MIN (sym->st_size, refsym->st_size));
346 break;
347 case R_SH_GLOB_DAT:
348 case R_SH_JMP_SLOT:
349 /* These addresses are always aligned. */
350 *reloc_addr = value;
351 break;
352 /* XXX Remove TLS relocations which are not needed. */
353 case R_SH_TLS_DTPMOD32:
354#ifdef RTLD_BOOTSTRAP
355 /* During startup the dynamic linker is always the module
356 with index 1.
357 XXX If this relocation is necessary move before RESOLVE
358 call. */
359 *reloc_addr = 1;
360#else
361 /* Get the information from the link map returned by the
362 resolv function. */
363 if (sym_map != NULL)
364 *reloc_addr = sym_map->l_tls_modid;
365#endif
366 break;
367 case R_SH_TLS_DTPOFF32:
368#ifndef RTLD_BOOTSTRAP
369 /* During relocation all TLS symbols are defined and used.
370 Therefore the offset is already correct. */
371 if (sym != NULL)
372 *reloc_addr = sym->st_value;
373#endif
374 break;
375 case R_SH_TLS_TPOFF32:
376 /* The offset is positive, afterward from the thread pointer. */
377#ifdef RTLD_BOOTSTRAP
378 *reloc_addr = map->l_tls_offset + sym->st_value + reloc->r_addend;
379#else
380 /* We know the offset of object the symbol is contained in.
381 It is a positive value which will be added to the thread
382 pointer. To get the variable position in the TLS block
383 we add the offset from that of the TLS block. */
384 if (sym != NULL)
385 {
386 CHECK_STATIC_TLS (map, sym_map);
387 *reloc_addr = sym_map->l_tls_offset + sym->st_value
388 + reloc->r_addend;
389 }
390#endif
391 break;
392 case R_SH_DIR32:
393 {
394#if !defined RTLD_BOOTSTRAP && !defined RESOLVE_CONFLICT_FIND_MAP
395 /* This is defined in rtld.c, but nowhere in the static
396 libc.a; make the reference weak so static programs can
397 still link. This declaration cannot be done when
398 compiling rtld.c (i.e. #ifdef RTLD_BOOTSTRAP) because
399 rtld.c contains the common defn for _dl_rtld_map, which
400 is incompatible with a weak decl in the same file. */
401# ifndef SHARED
402 weak_extern (_dl_rtld_map);
403# endif
404 if (map == &GL(dl_rtld_map))
405 /* Undo the relocation done here during bootstrapping.
406 Now we will relocate it anew, possibly using a
407 binding found in the user program or a loaded library
408 rather than the dynamic linker's built-in definitions
409 used while loading those libraries. */
410 value -= SYMBOL_ADDRESS (map, refsym, true) + reloc->r_addend;
411#endif
412 COPY_UNALIGNED_WORD (&value, reloc_addr_arg,
413 (int) reloc_addr_arg & 3);
414 break;
415 }
416 case R_SH_REL32:
417 value = (value - (Elf32_Addr) reloc_addr);
418 COPY_UNALIGNED_WORD (&value, reloc_addr_arg,
419 (int) reloc_addr_arg & 3);
420 break;
421 default:
422 _dl_reloc_bad_type (map, r_type, 0);
423 break;
424 }
425 }
426}
427
428static inline void
429__attribute__ ((always_inline))
430elf_machine_rela_relative (Elf32_Addr l_addr, const Elf32_Rela *reloc,
431 void *const reloc_addr_arg)
432{
433 Elf32_Addr value;
434
435 if (reloc->r_addend)
436 value = l_addr + reloc->r_addend;
437 else
438 {
439 COPY_UNALIGNED_WORD (reloc_addr_arg, &value, (int) reloc_addr_arg & 3);
440 value += l_addr;
441 }
442 COPY_UNALIGNED_WORD (&value, reloc_addr_arg, (int) reloc_addr_arg & 3);
443
444#undef COPY_UNALIGNED_WORD
445}
446
447static inline void
448__attribute__ ((always_inline))
449elf_machine_lazy_rel (struct link_map *map, struct r_scope_elem *scope[],
450 Elf32_Addr l_addr, const Elf32_Rela *reloc,
451 int skip_ifunc)
452{
453 Elf32_Addr *const reloc_addr = (void *) (l_addr + reloc->r_offset);
454 /* Check for unexpected PLT reloc type. */
455 if (ELF32_R_TYPE (reloc->r_info) == R_SH_JMP_SLOT)
456 {
457 if (__builtin_expect (map->l_mach.plt, 0) == 0)
458 *reloc_addr += l_addr;
459 else
460 *reloc_addr =
461 map->l_mach.plt
462 + (((Elf32_Addr) reloc_addr) - map->l_mach.gotplt) * 7;
463 }
464 else
465 _dl_reloc_bad_type (map, ELF32_R_TYPE (reloc->r_info), 1);
466}
467
468#endif /* RESOLVE_MAP */
469

Warning: This file is not a C or C++ file. It does not have highlighting.

source code of glibc/sysdeps/sh/dl-machine.h