]> git.ipfire.org Git - thirdparty/glibc.git/blob - sysdeps/csky/dl-machine.h
elf: Avoid nested functions in the loader [BZ #27220]
[thirdparty/glibc.git] / sysdeps / csky / dl-machine.h
1 /* Machine-dependent ELF dynamic relocation inline functions. C-SKY version.
2 Copyright (C) 2018-2021 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 "csky"
23
24 #include <sys/param.h>
25 #include <sysdep.h>
26 #include <dl-tls.h>
27
28 /* Return nonzero if ELF header is compatible with the running host. */
29 static inline int
30 elf_machine_matches_host (const Elf32_Ehdr *ehdr)
31 {
32 return ehdr->e_machine == EM_CSKY;
33 }
34
35 /* Return the link-time address of _DYNAMIC.
36 This must be inlined in a function which uses global data. */
37 static inline Elf32_Addr
38 elf_machine_dynamic (void)
39 {
40 register Elf32_Addr *got __asm__ ("gb");
41 return *got;
42 }
43
44 /* Return the run-time load address ,of the shared object. */
45 static inline Elf32_Addr
46 elf_machine_load_address (void)
47 {
48 extern Elf32_Addr __dl_start (void *) asm ("_dl_start");
49 Elf32_Addr got_addr = (Elf32_Addr) &__dl_start;
50 Elf32_Addr pcrel_addr;
51 asm ("grs %0,_dl_start\n" : "=r" (pcrel_addr));
52
53 return pcrel_addr - got_addr;
54 }
55
56
57 /* Set up the loaded object described by L so its unrelocated PLT
58 entries will jump to the on-demand fixup code in dl-runtime.c. */
59
60 static inline int __attribute__ ((always_inline))
61 elf_machine_runtime_setup (struct link_map *l, struct r_scope_elem *scope[],
62 int lazy, int profile)
63 {
64 Elf32_Addr *got;
65 extern void _dl_runtime_resolve (Elf32_Word);
66
67 if (l->l_info[DT_JMPREL] && lazy)
68 {
69 /* The GOT entries for functions in the PLT have not yet been
70 filled in. Their initial contents will arrange when called
71 to push an offset into the .rela.plt section, push
72 _GLOBAL_OFFSET_TABLE_[1], and then jump to
73 _GLOBAL_OFFSET_TABLE_[2]. */
74 got = (Elf32_Addr *) D_PTR (l, l_info[DT_PLTGOT]);
75
76 if (got[1])
77 l->l_mach.plt = got[1] + l->l_addr;
78 got[1] = (Elf32_Addr) l; /* Identify this shared object. */
79
80 /* The got[2] entry contains the address of a function which gets
81 called to get the address of a so far unresolved function and
82 jump to it. The profiling extension of the dynamic linker allows
83 to intercept the calls to collect information. In this case we
84 don't store the address in the GOT so that all future calls also
85 end in this function. */
86 got[2] = (Elf32_Addr) &_dl_runtime_resolve;
87 }
88 return lazy;
89 }
90
91 /* Mask identifying addresses reserved for the user program,
92 where the dynamic linker should not map anything. */
93 #define ELF_MACHINE_USER_ADDRESS_MASK 0x80000000UL
94
95 /* Initial entry point code for the dynamic linker.
96 The C function `_dl_start' is the real entry point;
97 its return value is the user program's entry point. */
98 #define RTLD_START asm ("\
99 .text\n\
100 .globl _start\n\
101 .type _start, @function\n\
102 .globl _dl_start_user\n\
103 .type _dl_start_user, @function\n\
104 _start:\n\
105 grs gb, .Lgetpc1\n\
106 .Lgetpc1:\n\
107 lrw t0, .Lgetpc1@GOTPC\n\
108 addu gb, t0\n\
109 mov a0, sp\n\
110 lrw t1, _dl_start@GOTOFF\n\
111 addu t1, gb\n\
112 jsr t1\n\
113 _dl_start_user:\n\
114 /* get _dl_skip_args */ \n\
115 lrw r11, _dl_skip_args@GOTOFF\n\
116 addu r11, gb\n\
117 ldw r11, (r11, 0)\n\
118 /* store program entry address in r11 */ \n\
119 mov r10, a0\n\
120 /* Get argc */\n\
121 ldw a1, (sp, 0)\n\
122 /* Get **argv */\n\
123 mov a2, sp\n\
124 addi a2, 4\n\
125 cmpnei r11, 0\n\
126 bt .L_fixup_stack\n\
127 .L_done_fixup:\n\
128 mov a3, a1\n\
129 lsli a3, 2\n\
130 add a3, a2\n\
131 addi a3, 4\n\
132 lrw a0, _rtld_local@GOTOFF\n\
133 addu a0, gb\n\
134 ldw a0, (a0, 0)\n\
135 lrw t1, _dl_init@PLT\n\
136 addu t1, gb\n\
137 ldw t1, (t1)\n\
138 jsr t1\n\
139 lrw a0, _dl_fini@GOTOFF\n\
140 addu a0, gb\n\
141 jmp r10\n\
142 .L_fixup_stack:\n\
143 subu a1, r11\n\
144 lsli r11, 2\n\
145 addu sp, r11\n\
146 stw a1, (sp, 0)\n\
147 mov a2, sp\n\
148 addi a2, 4\n\
149 lrw a3, _dl_argv@GOTOFF\n\
150 addu a3, gb\n\
151 stw a2, (a3, 0)\n\
152 br .L_done_fixup\n\
153 ");
154
155 /* ELF_RTYPE_CLASS_PLT iff TYPE describes relocation of a PLT entry or
156 TLS variable, so undefined references should not be allowed to
157 define the value.
158 ELF_RTYPE_CLASS_NOCOPY iff TYPE should not be allowed to resolve to one
159 of the main executable's symbols, as for a COPY reloc. */
160 #ifndef RTLD_BOOTSTRAP
161 # define elf_machine_type_class(type) \
162 ((((type) == R_CKCORE_JUMP_SLOT || (type) == R_CKCORE_TLS_DTPMOD32 \
163 || (type) == R_CKCORE_TLS_DTPOFF32 || (type) == R_CKCORE_TLS_TPOFF32) \
164 * ELF_RTYPE_CLASS_PLT) \
165 | (((type) == R_CKCORE_COPY) * ELF_RTYPE_CLASS_COPY))
166 #else
167 # define elf_machine_type_class(type) \
168 ((((type) == R_CKCORE_JUMP_SLOT \
169 | (((type) == R_CKCORE_COPY) * ELF_RTYPE_CLASS_COPY))
170 #endif
171
172 /* A reloc type used for ld.so cmdline arg lookups to reject PLT entries. */
173 #define ELF_MACHINE_JMP_SLOT R_CKCORE_JUMP_SLOT
174
175 /* C-SKY never uses Elf32_Rel relocations. */
176 #define ELF_MACHINE_NO_REL 1
177 #define ELF_MACHINE_NO_RELA 0
178
179 /* We define an initialization functions. This is called very early in
180 _dl_sysdep_start. */
181 #define DL_PLATFORM_INIT dl_platform_init ()
182
183 static inline void __attribute__ ((unused))
184 dl_platform_init (void)
185 {
186 if (GLRO(dl_platform) != NULL && *GLRO(dl_platform) == '\0')
187 /* Avoid an empty string which would disturb us. */
188 GLRO(dl_platform) = NULL;
189 }
190
191 static inline Elf32_Addr
192 elf_machine_fixup_plt (struct link_map *map, lookup_t t,
193 const ElfW(Sym) *refsym, const ElfW(Sym) *sym,
194 const Elf32_Rela *reloc,
195 Elf32_Addr *reloc_addr, Elf32_Addr value)
196 {
197 return *reloc_addr = value;
198 }
199
200 /* Return the final value of a plt relocation. On the csky the JMP_SLOT
201 relocation ignores the addend. */
202 static inline Elf32_Addr
203 elf_machine_plt_value (struct link_map *map, const Elf32_Rela *reloc,
204 Elf32_Addr value)
205 {
206 return value;
207 }
208
209 /* Names of the architecture-specific auditing callback functions. */
210 #define ARCH_LA_PLTENTER csky_gnu_pltenter
211 #define ARCH_LA_PLTEXIT csky_gnu_pltexit
212
213 #endif /* !dl_machine_h */
214 #ifdef RESOLVE_MAP
215
216 /* Perform the relocation specified by RELOC and SYM (which is fully resolved).
217 MAP is the object containing the reloc. */
218
219 static inline void __attribute__ ((unused, always_inline))
220 elf_machine_rela (struct link_map *map, struct r_scope_elem *scope[],
221 const Elf32_Rela *reloc, const Elf32_Sym *sym,
222 const struct r_found_version *version,
223 void *const reloc_addr_arg, int skip_ifunc)
224 {
225 Elf32_Addr *const reloc_addr = reloc_addr_arg;
226 const unsigned int r_type = ELF32_R_TYPE (reloc->r_info);
227 unsigned short __attribute__ ((unused)) *opcode16_addr;
228 Elf32_Addr __attribute__ ((unused)) insn_opcode = 0x0;
229
230 if (__builtin_expect (r_type == R_CKCORE_RELATIVE, 0))
231 *reloc_addr = map->l_addr + reloc->r_addend;
232 else
233 {
234 const Elf32_Sym *const refsym = sym;
235 struct link_map *sym_map = RESOLVE_MAP (map, scope, &sym, version,
236 r_type);
237 ElfW(Addr) value = SYMBOL_ADDRESS (sym_map, sym, true);
238 opcode16_addr = (unsigned short *)reloc_addr;
239
240 switch (r_type)
241 {
242 case R_CKCORE_COPY:
243 if (sym == NULL)
244 /* This can happen in trace mode if an object could not be
245 found. */
246 break;
247 if (sym->st_size > refsym->st_size
248 || (sym->st_size < refsym->st_size && GLRO(dl_verbose)))
249 {
250 const char *strtab;
251
252 strtab = (const void *) D_PTR (map, l_info[DT_STRTAB]);
253 _dl_error_printf ("\
254 %s: Symbol `%s' has different size in shared object, consider re-linking\n",
255 rtld_progname ?: "<program name unknown>",
256 strtab + refsym->st_name);
257 }
258 memcpy (reloc_addr_arg, (void *) value,
259 MIN (sym->st_size, refsym->st_size));
260 break;
261 case R_CKCORE_GLOB_DAT:
262 case R_CKCORE_JUMP_SLOT:
263 *reloc_addr = value;
264 break;
265 case R_CKCORE_ADDR32:
266 *reloc_addr = value + reloc->r_addend;
267 break;
268 case R_CKCORE_PCREL32:
269 *reloc_addr = value + reloc->r_addend - (Elf32_Addr) reloc_addr;
270 break;
271 #if defined(__CK810__) || defined(__CK807__)
272 case R_CKCORE_ADDR_HI16:
273 insn_opcode = (*opcode16_addr << 16) | (*(opcode16_addr + 1));
274 insn_opcode = (insn_opcode & 0xffff0000)
275 | (((value + reloc->r_addend) >> 16) & 0xffff);
276 *(opcode16_addr++) = (unsigned short)(insn_opcode >> 16);
277 *opcode16_addr = (unsigned short)(insn_opcode & 0xffff);
278 break;
279 case R_CKCORE_ADDR_LO16:
280 insn_opcode = (*opcode16_addr << 16) | (*(opcode16_addr + 1));
281 insn_opcode = (insn_opcode & 0xffff0000)
282 | ((value + reloc->r_addend) & 0xffff);
283 *(opcode16_addr++) = (unsigned short)(insn_opcode >> 16);
284 *opcode16_addr = (unsigned short)(insn_opcode & 0xffff);
285 break;
286 case R_CKCORE_PCREL_IMM26BY2:
287 {
288 unsigned int offset = ((value + reloc->r_addend
289 - (unsigned int)reloc_addr) >> 1);
290 insn_opcode = (*opcode16_addr << 16) | (*(opcode16_addr + 1));
291 if (offset > 0x3ffffff){
292 const char *strtab;
293 strtab = (const void *) D_PTR (map, l_info[DT_STRTAB]);
294
295 _dl_error_printf ("\
296 %s:The reloc R_CKCORE_PCREL_IMM26BY2 cannot reach the symbol '%s'.\n",
297 rtld_progname ?: "<program name unknown>",
298 strtab + refsym->st_name);
299 break;
300 }
301 insn_opcode = (insn_opcode & ~0x3ffffff) | offset;
302 *(opcode16_addr++) = (unsigned short)(insn_opcode >> 16);
303 *opcode16_addr = (unsigned short)(insn_opcode & 0xffff);
304 break;
305 }
306 case R_CKCORE_PCREL_JSR_IMM26BY2:
307 break;
308 #endif
309 #ifndef RTLD_BOOTSTRAP
310 case R_CKCORE_TLS_DTPMOD32:
311 /* Get the information from the link map returned by the
312 resolv function. */
313 if (sym_map != NULL)
314 *reloc_addr = sym_map->l_tls_modid;
315 break;
316 case R_CKCORE_TLS_DTPOFF32:
317 if (sym != NULL)
318 *reloc_addr =(sym == NULL ? 0 : sym->st_value) + reloc->r_addend;
319 break;
320 case R_CKCORE_TLS_TPOFF32:
321 if (sym != NULL)
322 {
323 CHECK_STATIC_TLS (map, sym_map);
324 *reloc_addr = (sym->st_value + sym_map->l_tls_offset
325 + reloc->r_addend);
326 }
327 break;
328 #endif /* !RTLD_BOOTSTRAP */
329 case R_CKCORE_NONE:
330 break;
331 default:
332 break;
333 }
334 }
335 }
336
337 static inline void __attribute__ ((unused, always_inline))
338 elf_machine_rela_relative (Elf32_Addr l_addr, const Elf32_Rela *reloc,
339 void *const reloc_addr_arg)
340 {
341 Elf32_Addr *const reloc_addr = reloc_addr_arg;
342 *reloc_addr = l_addr + reloc->r_addend;
343 }
344
345 static inline void __attribute__ ((unused, always_inline))
346 elf_machine_lazy_rel (struct link_map *map, struct r_scope_elem *scope[],
347 Elf32_Addr l_addr, const Elf32_Rela *reloc,
348 int skip_ifunc)
349 {
350 Elf32_Addr *const reloc_addr = (void *) (l_addr + reloc->r_offset);
351 const unsigned int r_type = ELF32_R_TYPE (reloc->r_info);
352 if (ELF32_R_TYPE (reloc->r_info) == R_CKCORE_JUMP_SLOT)
353 {
354 /* Check for unexpected PLT reloc type. */
355 if (__builtin_expect (r_type == R_CKCORE_JUMP_SLOT, 1))
356 {
357 if (__builtin_expect (map->l_mach.plt, 0) == 0)
358 *reloc_addr = l_addr + reloc->r_addend;
359 else
360 *reloc_addr = map->l_mach.plt;
361 }
362 }
363 }
364
365 #endif /* RESOLVE_MAP */