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