]> git.ipfire.org Git - thirdparty/glibc.git/blob - sysdeps/powerpc/powerpc32/dl-machine.h
Update copyright dates with scripts/update-copyrights
[thirdparty/glibc.git] / sysdeps / powerpc / powerpc32 / dl-machine.h
1 /* Machine-dependent ELF dynamic relocation inline functions. PowerPC version.
2 Copyright (C) 1995-2023 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 "powerpc"
23
24 #include <assert.h>
25 #include <dl-tls.h>
26 #include <dl-irel.h>
27 #include <hwcapinfo.h>
28 #include <dl-static-tls.h>
29 #include <dl-machine-rel.h>
30
31 /* Translate a processor specific dynamic tag to the index
32 in l_info array. */
33 #define DT_PPC(x) (DT_PPC_##x - DT_LOPROC + DT_NUM)
34
35 /* Return nonzero iff ELF header is compatible with the running host. */
36 static inline int
37 elf_machine_matches_host (const Elf32_Ehdr *ehdr)
38 {
39 return ehdr->e_machine == EM_PPC;
40 }
41
42 /* Return the value of the GOT pointer. */
43 static inline Elf32_Addr * __attribute__ ((const))
44 ppc_got (void)
45 {
46 Elf32_Addr *got;
47
48 asm ("bcl 20,31,1f\n"
49 "1: mflr %0\n"
50 " addis %0,%0,_GLOBAL_OFFSET_TABLE_-1b@ha\n"
51 " addi %0,%0,_GLOBAL_OFFSET_TABLE_-1b@l\n"
52 : "=b" (got) : : "lr");
53
54 return got;
55 }
56
57 /* Return the link-time address of _DYNAMIC, stored as
58 the first value in the GOT. */
59 static inline Elf32_Addr __attribute__ ((const))
60 elf_machine_dynamic (void)
61 {
62 return *ppc_got ();
63 }
64
65 /* Return the run-time load address of the shared object. */
66 static inline Elf32_Addr __attribute__ ((const))
67 elf_machine_load_address (void)
68 {
69 Elf32_Addr *branchaddr;
70 Elf32_Addr runtime_dynamic;
71
72 /* This is much harder than you'd expect. Possibly I'm missing something.
73 The 'obvious' way:
74
75 Apparently, "bcl 20,31,$+4" is what should be used to load LR
76 with the address of the next instruction.
77 I think this is so that machines that do bl/blr pairing don't
78 get confused.
79
80 asm ("bcl 20,31,0f ;"
81 "0: mflr 0 ;"
82 "lis %0,0b@ha;"
83 "addi %0,%0,0b@l;"
84 "subf %0,%0,0"
85 : "=b" (addr) : : "r0", "lr");
86
87 doesn't work, because the linker doesn't have to (and in fact doesn't)
88 update the @ha and @l references; the loader (which runs after this
89 code) will do that.
90
91 Instead, we use the following trick:
92
93 The linker puts the _link-time_ address of _DYNAMIC at the first
94 word in the GOT. We could branch to that address, if we wanted,
95 by using an @local reloc; the linker works this out, so it's safe
96 to use now. We can't, of course, actually branch there, because
97 we'd cause an illegal instruction exception; so we need to compute
98 the address ourselves. That gives us the following code: */
99
100 /* Get address of the 'b _DYNAMIC@local'... */
101 asm ("bcl 20,31,0f;"
102 "b _DYNAMIC@local;"
103 "0:"
104 : "=l" (branchaddr));
105
106 /* So now work out the difference between where the branch actually points,
107 and the offset of that location in memory from the start of the file. */
108 runtime_dynamic = ((Elf32_Addr) branchaddr
109 + ((Elf32_Sword) (*branchaddr << 6 & 0xffffff00) >> 6));
110
111 return runtime_dynamic - elf_machine_dynamic ();
112 }
113
114 /* The PLT uses Elf32_Rela relocs. */
115 #define elf_machine_relplt elf_machine_rela
116
117 /* Mask identifying addresses reserved for the user program,
118 where the dynamic linker should not map anything. */
119 #define ELF_MACHINE_USER_ADDRESS_MASK 0xf0000000UL
120
121 /* The actual _start code is in dl-start.S. Use a really
122 ugly bit of assembler to let dl-start.o see _dl_start. */
123 #define RTLD_START asm (".globl _dl_start");
124
125 /* Decide where a relocatable object should be loaded. */
126 extern ElfW(Addr)
127 __elf_preferred_address(struct link_map *loader, size_t maplength,
128 ElfW(Addr) mapstartpref);
129 #define ELF_PREFERRED_ADDRESS(loader, maplength, mapstartpref) \
130 __elf_preferred_address (loader, maplength, mapstartpref)
131
132 /* ELF_RTYPE_CLASS_PLT iff TYPE describes relocation of a PLT entry, so
133 PLT entries should not be allowed to define the value.
134 ELF_RTYPE_CLASS_COPY iff TYPE should not be allowed to resolve to one
135 of the main executable's symbols, as for a COPY reloc. */
136 /* We never want to use a PLT entry as the destination of a
137 reloc, when what is being relocated is a branch. This is
138 partly for efficiency, but mostly so we avoid loops. */
139 #define elf_machine_type_class(type) \
140 ((((type) == R_PPC_JMP_SLOT \
141 || (type) == R_PPC_REL24 \
142 || ((type) >= R_PPC_DTPMOD32 /* contiguous TLS */ \
143 && (type) <= R_PPC_DTPREL32) \
144 || (type) == R_PPC_ADDR24) * ELF_RTYPE_CLASS_PLT) \
145 | (((type) == R_PPC_COPY) * ELF_RTYPE_CLASS_COPY))
146
147 /* A reloc type used for ld.so cmdline arg lookups to reject PLT entries. */
148 #define ELF_MACHINE_JMP_SLOT R_PPC_JMP_SLOT
149
150 /* We define an initialization function to initialize HWCAP/HWCAP2 and
151 platform data so it can be copied into the TCB later. This is called
152 very early in _dl_sysdep_start for dynamically linked binaries. */
153 #ifdef SHARED
154 # define DL_PLATFORM_INIT dl_platform_init ()
155
156 static inline void __attribute__ ((unused))
157 dl_platform_init (void)
158 {
159 __tcb_parse_hwcap_and_convert_at_platform ();
160 }
161 #endif
162
163 /* Set up the loaded object described by MAP so its unrelocated PLT
164 entries will jump to the on-demand fixup code in dl-runtime.c.
165 Also install a small trampoline to be used by entries that have
166 been relocated to an address too far away for a single branch. */
167 extern int __elf_machine_runtime_setup (struct link_map *map,
168 int lazy, int profile);
169
170 static inline int
171 elf_machine_runtime_setup (struct link_map *map, struct r_scope_elem *scope[],
172 int lazy, int profile)
173 {
174 if (map->l_info[DT_JMPREL] == 0)
175 return lazy;
176
177 if (map->l_info[DT_PPC(GOT)] == 0)
178 /* Handle old style PLT. */
179 return __elf_machine_runtime_setup (map, lazy, profile);
180
181 /* New style non-exec PLT consisting of an array of addresses. */
182 map->l_info[DT_PPC(GOT)]->d_un.d_ptr += map->l_addr;
183 if (lazy)
184 {
185 Elf32_Addr *plt, *got, glink;
186 Elf32_Word num_plt_entries;
187 void (*dlrr) (void);
188 extern void _dl_runtime_resolve (void);
189 extern void _dl_prof_resolve (void);
190
191 if (__glibc_likely (!profile))
192 dlrr = _dl_runtime_resolve;
193 else
194 {
195 if (GLRO(dl_profile) != NULL
196 &&_dl_name_match_p (GLRO(dl_profile), map))
197 GL(dl_profile_map) = map;
198 dlrr = _dl_prof_resolve;
199 }
200 got = (Elf32_Addr *) map->l_info[DT_PPC(GOT)]->d_un.d_ptr;
201 glink = got[1];
202 got[1] = (Elf32_Addr) dlrr;
203 got[2] = (Elf32_Addr) map;
204
205 /* Relocate everything in .plt by the load address offset. */
206 plt = (Elf32_Addr *) D_PTR (map, l_info[DT_PLTGOT]);
207 num_plt_entries = (map->l_info[DT_PLTRELSZ]->d_un.d_val
208 / sizeof (Elf32_Rela));
209
210 /* If a library is prelinked but we have to relocate anyway,
211 we have to be able to undo the prelinking of .plt section.
212 The prelinker saved us at got[1] address of .glink
213 section's start. */
214 if (glink)
215 {
216 glink += map->l_addr;
217 while (num_plt_entries-- != 0)
218 *plt++ = glink, glink += 4;
219 }
220 else
221 while (num_plt_entries-- != 0)
222 *plt++ += map->l_addr;
223 }
224 return lazy;
225 }
226
227 /* Change the PLT entry whose reloc is 'reloc' to call the actual routine. */
228 extern Elf32_Addr __elf_machine_fixup_plt (struct link_map *map,
229 Elf32_Addr *reloc_addr,
230 Elf32_Addr finaladdr);
231
232 static inline Elf32_Addr
233 elf_machine_fixup_plt (struct link_map *map, lookup_t t,
234 const ElfW(Sym) *refsym, const ElfW(Sym) *sym,
235 const Elf32_Rela *reloc,
236 Elf32_Addr *reloc_addr, Elf64_Addr finaladdr)
237 {
238 if (map->l_info[DT_PPC(GOT)] == 0)
239 /* Handle old style PLT. */
240 return __elf_machine_fixup_plt (map, reloc_addr, finaladdr);
241
242 *reloc_addr = finaladdr;
243 return finaladdr;
244 }
245
246 /* Return the final value of a plt relocation. */
247 static inline Elf32_Addr
248 elf_machine_plt_value (struct link_map *map, const Elf32_Rela *reloc,
249 Elf32_Addr value)
250 {
251 return value + reloc->r_addend;
252 }
253
254
255 /* Names of the architecture-specific auditing callback functions. */
256 #define ARCH_LA_PLTENTER ppc32_gnu_pltenter
257 #define ARCH_LA_PLTEXIT ppc32_gnu_pltexit
258
259 #endif /* dl_machine_h */
260
261 #ifdef RESOLVE_MAP
262
263 /* Do the actual processing of a reloc, once its target address
264 has been determined. */
265 extern void __process_machine_rela (struct link_map *map,
266 const Elf32_Rela *reloc,
267 struct link_map *sym_map,
268 const Elf32_Sym *sym,
269 const Elf32_Sym *refsym,
270 Elf32_Addr *const reloc_addr,
271 Elf32_Addr finaladdr,
272 int rinfo, bool skip_ifunc)
273 attribute_hidden;
274
275 /* Call _dl_signal_error when a resolved value overflows a relocated area. */
276 extern void _dl_reloc_overflow (struct link_map *map,
277 const char *name,
278 Elf32_Addr *const reloc_addr,
279 const Elf32_Sym *refsym) attribute_hidden;
280
281 /* Perform the relocation specified by RELOC and SYM (which is fully resolved).
282 LOADADDR is the load address of the object; INFO is an array indexed
283 by DT_* of the .dynamic section info. */
284
285 static inline void __attribute__ ((always_inline))
286 elf_machine_rela (struct link_map *map, struct r_scope_elem *scope[],
287 const Elf32_Rela *reloc, const Elf32_Sym *sym,
288 const struct r_found_version *version,
289 void *const reloc_addr_arg, int skip_ifunc)
290 {
291 Elf32_Addr *const reloc_addr = reloc_addr_arg;
292 const Elf32_Sym *const refsym = sym;
293 Elf32_Addr value;
294 const int r_type = ELF32_R_TYPE (reloc->r_info);
295 struct link_map *sym_map = NULL;
296
297 if (r_type == R_PPC_RELATIVE)
298 {
299 *reloc_addr = map->l_addr + reloc->r_addend;
300 return;
301 }
302
303 if (__glibc_unlikely (r_type == R_PPC_NONE))
304 return;
305
306 /* binutils on ppc32 includes st_value in r_addend for relocations
307 against local symbols. */
308 if (__builtin_expect (ELF32_ST_BIND (sym->st_info) == STB_LOCAL, 0)
309 && sym->st_shndx != SHN_UNDEF)
310 {
311 sym_map = map;
312 value = map->l_addr;
313 }
314 else
315 {
316 sym_map = RESOLVE_MAP (map, scope, &sym, version, r_type);
317 value = SYMBOL_ADDRESS (sym_map, sym, true);
318 }
319 value += reloc->r_addend;
320
321 if (sym != NULL
322 && __builtin_expect (ELFW(ST_TYPE) (sym->st_info) == STT_GNU_IFUNC, 0)
323 && __builtin_expect (sym->st_shndx != SHN_UNDEF, 1)
324 && __builtin_expect (!skip_ifunc, 1))
325 value = elf_ifunc_invoke (value);
326
327 /* A small amount of code is duplicated here for speed. In libc,
328 more than 90% of the relocs are R_PPC_RELATIVE; in the X11 shared
329 libraries, 60% are R_PPC_RELATIVE, 24% are R_PPC_GLOB_DAT or
330 R_PPC_ADDR32, and 16% are R_PPC_JMP_SLOT (which this routine
331 wouldn't usually handle). As an bonus, doing this here allows
332 the switch statement in __process_machine_rela to work. */
333 switch (r_type)
334 {
335 case R_PPC_GLOB_DAT:
336 case R_PPC_ADDR32:
337 *reloc_addr = value;
338 break;
339
340 #ifdef RTLD_BOOTSTRAP
341 # define NOT_BOOTSTRAP 0
342 #else
343 # define NOT_BOOTSTRAP 1
344 #endif
345
346 case R_PPC_DTPMOD32:
347 if (map->l_info[DT_PPC(OPT)]
348 && (map->l_info[DT_PPC(OPT)]->d_un.d_val & PPC_OPT_TLS))
349 {
350 if (!NOT_BOOTSTRAP)
351 {
352 reloc_addr[0] = 0;
353 reloc_addr[1] = (sym_map->l_tls_offset - TLS_TP_OFFSET
354 + TLS_DTV_OFFSET);
355 break;
356 }
357 else if (sym_map != NULL)
358 {
359 #ifndef SHARED
360 CHECK_STATIC_TLS (map, sym_map);
361 #else
362 if (TRY_STATIC_TLS (map, sym_map))
363 #endif
364 {
365 reloc_addr[0] = 0;
366 /* Set up for local dynamic. */
367 reloc_addr[1] = (sym_map->l_tls_offset - TLS_TP_OFFSET
368 + TLS_DTV_OFFSET);
369 break;
370 }
371 }
372 }
373 if (!NOT_BOOTSTRAP)
374 /* During startup the dynamic linker is always index 1. */
375 *reloc_addr = 1;
376 else if (sym_map != NULL)
377 /* Get the information from the link map returned by the
378 RESOLVE_MAP function. */
379 *reloc_addr = sym_map->l_tls_modid;
380 break;
381 case R_PPC_DTPREL32:
382 if (map->l_info[DT_PPC(OPT)]
383 && (map->l_info[DT_PPC(OPT)]->d_un.d_val & PPC_OPT_TLS))
384 {
385 if (!NOT_BOOTSTRAP)
386 {
387 *reloc_addr = TLS_TPREL_VALUE (sym_map, sym, reloc);
388 break;
389 }
390 else if (sym_map != NULL)
391 {
392 /* This reloc is always preceded by R_PPC_DTPMOD32. */
393 #ifndef SHARED
394 assert (HAVE_STATIC_TLS (map, sym_map));
395 #else
396 if (HAVE_STATIC_TLS (map, sym_map))
397 #endif
398 {
399 *reloc_addr = TLS_TPREL_VALUE (sym_map, sym, reloc);
400 break;
401 }
402 }
403 }
404 /* During relocation all TLS symbols are defined and used.
405 Therefore the offset is already correct. */
406 if (NOT_BOOTSTRAP && sym_map != NULL)
407 *reloc_addr = TLS_DTPREL_VALUE (sym, reloc);
408 break;
409 case R_PPC_TPREL32:
410 if (!NOT_BOOTSTRAP || sym_map != NULL)
411 {
412 if (NOT_BOOTSTRAP)
413 CHECK_STATIC_TLS (map, sym_map);
414 *reloc_addr = TLS_TPREL_VALUE (sym_map, sym, reloc);
415 }
416 break;
417
418 case R_PPC_JMP_SLOT:
419 if (map->l_info[DT_PPC(GOT)] != 0)
420 {
421 *reloc_addr = value;
422 break;
423 }
424 /* FALLTHROUGH */
425
426 default:
427 __process_machine_rela (map, reloc, sym_map, sym, refsym,
428 reloc_addr, value, r_type, skip_ifunc);
429 }
430 }
431
432 static inline void __attribute__ ((always_inline))
433 elf_machine_rela_relative (Elf32_Addr l_addr, const Elf32_Rela *reloc,
434 void *const reloc_addr_arg)
435 {
436 Elf32_Addr *const reloc_addr = reloc_addr_arg;
437 *reloc_addr = l_addr + reloc->r_addend;
438 }
439
440 static inline void __attribute__ ((always_inline))
441 elf_machine_lazy_rel (struct link_map *map, struct r_scope_elem *scope[],
442 Elf32_Addr l_addr, const Elf32_Rela *reloc,
443 int skip_ifunc)
444 {
445 /* elf_machine_runtime_setup handles this. */
446 }
447
448 #endif /* RESOLVE_MAP */