]> git.ipfire.org Git - thirdparty/glibc.git/blame - sysdeps/powerpc/powerpc32/dl-machine.h
.
[thirdparty/glibc.git] / sysdeps / powerpc / powerpc32 / dl-machine.h
CommitLineData
4cca6b86 1/* Machine-dependent ELF dynamic relocation inline functions. PowerPC version.
32c075e1 2 Copyright (C) 1995-2002, 2003, 2005 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
AJ
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
4cca6b86 19
1f205a47
UD
20#ifndef dl_machine_h
21#define dl_machine_h
22
4cca6b86
UD
23#define ELF_MACHINE_NAME "powerpc"
24
25#include <assert.h>
bb0ddc2f 26#include <dl-tls.h>
4cca6b86 27
99c7f870
UD
28/* Translate a processor specific dynamic tag to the index
29 in l_info array. */
30#define DT_PPC(x) (DT_PPC_##x - DT_LOPROC + DT_NUM)
31
ceb579a3 32/* Return nonzero iff ELF header is compatible with the running host. */
c2a32973 33static inline int
ceb579a3 34elf_machine_matches_host (const Elf32_Ehdr *ehdr)
4cca6b86 35{
ceb579a3 36 return ehdr->e_machine == EM_PPC;
4cca6b86
UD
37}
38
99c7f870
UD
39/* Return the value of the GOT pointer. */
40static inline Elf32_Addr * __attribute__ ((const))
41ppc_got (void)
42{
43 Elf32_Addr *got;
44#ifdef HAVE_ASM_PPC_REL16
45 asm ("bcl 20,31,1f\n"
46 "1: mflr %0\n"
47 " addis %0,%0,_GLOBAL_OFFSET_TABLE_-1b@ha\n"
48 " addi %0,%0,_GLOBAL_OFFSET_TABLE_-1b@l\n"
49 : "=b" (got) : : "lr");
50#else
51 asm (" bl _GLOBAL_OFFSET_TABLE_-4@local"
52 : "=l" (got));
53#endif
54 return got;
55}
4cca6b86 56
e61abf83
UD
57/* Return the link-time address of _DYNAMIC, stored as
58 the first value in the GOT. */
99c7f870 59static inline Elf32_Addr __attribute__ ((const))
4cca6b86
UD
60elf_machine_dynamic (void)
61{
99c7f870 62 return *ppc_got ();
4cca6b86
UD
63}
64
65/* Return the run-time load address of the shared object. */
99c7f870 66static inline Elf32_Addr __attribute__ ((const))
4cca6b86
UD
67elf_machine_load_address (void)
68{
99c7f870
UD
69 Elf32_Addr *branchaddr;
70 Elf32_Addr runtime_dynamic;
4cca6b86
UD
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 ;"
e61abf83
UD
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");
4cca6b86
UD
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'... */
99c7f870 101 asm ("bcl 20,31,0f;"
4cca6b86
UD
102 "b _DYNAMIC@local;"
103 "0:"
99c7f870 104 : "=l" (branchaddr));
4cca6b86
UD
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. */
99c7f870
UD
108 runtime_dynamic = ((Elf32_Addr) branchaddr
109 + ((Elf32_Sword) (*branchaddr << 6 & 0xffffff00) >> 6));
110
111 return runtime_dynamic - elf_machine_dynamic ();
4cca6b86
UD
112}
113
114#define ELF_MACHINE_BEFORE_RTLD_RELOC(dynamic_info) /* nothing */
115
1f205a47
UD
116/* The PLT uses Elf32_Rela relocs. */
117#define elf_machine_relplt elf_machine_rela
4cca6b86 118
9ce8b3c8
RM
119/* Mask identifying addresses reserved for the user program,
120 where the dynamic linker should not map anything. */
121#define ELF_MACHINE_USER_ADDRESS_MASK 0xf0000000UL
122
052b6a6c
UD
123/* The actual _start code is in dl-start.S. Use a really
124 ugly bit of assembler to let dl-start.o see _dl_start. */
125#define RTLD_START asm (".globl _dl_start");
4cca6b86 126
052b6a6c
UD
127/* Decide where a relocatable object should be loaded. */
128extern ElfW(Addr)
129__elf_preferred_address(struct link_map *loader, size_t maplength,
130 ElfW(Addr) mapstartpref);
131#define ELF_PREFERRED_ADDRESS(loader, maplength, mapstartpref) \
132 __elf_preferred_address (loader, maplength, mapstartpref)
e61abf83 133
cf5a372e
UD
134/* ELF_RTYPE_CLASS_PLT iff TYPE describes relocation of a PLT entry, so
135 PLT entries should not be allowed to define the value.
136 ELF_RTYPE_CLASS_NOCOPY iff TYPE should not be allowed to resolve to one
137 of the main executable's symbols, as for a COPY reloc. */
1f205a47
UD
138/* We never want to use a PLT entry as the destination of a
139 reloc, when what is being relocated is a branch. This is
140 partly for efficiency, but mostly so we avoid loops. */
32c075e1 141#if defined USE_TLS && (!defined RTLD_BOOTSTRAP || USE___THREAD)
bb0ddc2f
RM
142#define elf_machine_type_class(type) \
143 ((((type) == R_PPC_JMP_SLOT \
144 || (type) == R_PPC_REL24 \
7551556f
RM
145 || ((type) >= R_PPC_DTPMOD32 /* contiguous TLS */ \
146 && (type) <= R_PPC_DTPREL32) \
bb0ddc2f
RM
147 || (type) == R_PPC_ADDR24) * ELF_RTYPE_CLASS_PLT) \
148 | (((type) == R_PPC_COPY) * ELF_RTYPE_CLASS_COPY))
149#else
cf5a372e
UD
150#define elf_machine_type_class(type) \
151 ((((type) == R_PPC_JMP_SLOT \
152 || (type) == R_PPC_REL24 \
153 || (type) == R_PPC_ADDR24) * ELF_RTYPE_CLASS_PLT) \
154 | (((type) == R_PPC_COPY) * ELF_RTYPE_CLASS_COPY))
bb0ddc2f 155#endif
4cca6b86 156
1f205a47 157/* A reloc type used for ld.so cmdline arg lookups to reject PLT entries. */
a2b08ee5 158#define ELF_MACHINE_JMP_SLOT R_PPC_JMP_SLOT
4cca6b86 159
4839f592
AJ
160/* The PowerPC never uses REL relocations. */
161#define ELF_MACHINE_NO_REL 1
162
99c7f870 163/* Set up the loaded object described by MAP so its unrelocated PLT
5929563f
UD
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. */
052b6a6c
UD
167extern int __elf_machine_runtime_setup (struct link_map *map,
168 int lazy, int profile);
99c7f870
UD
169
170static inline int
171elf_machine_runtime_setup (struct link_map *map,
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 (__builtin_expect (!profile, 1))
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}
5929563f 226
052b6a6c 227/* Change the PLT entry whose reloc is 'reloc' to call the actual routine. */
c0282c06
UD
228extern Elf32_Addr __elf_machine_fixup_plt (struct link_map *map,
229 const Elf32_Rela *reloc,
230 Elf32_Addr *reloc_addr,
231 Elf32_Addr finaladdr);
232
233static inline Elf32_Addr
b6299091 234elf_machine_fixup_plt (struct link_map *map, lookup_t t,
c0282c06 235 const Elf32_Rela *reloc,
b6299091 236 Elf32_Addr *reloc_addr, Elf64_Addr finaladdr)
c0282c06 237{
99c7f870
UD
238 if (map->l_info[DT_PPC(GOT)] == 0)
239 /* Handle old style PLT. */
240 return __elf_machine_fixup_plt (map, reloc, reloc_addr, finaladdr);
241
242 *reloc_addr = finaladdr;
243 return finaladdr;
c0282c06 244}
a2b08ee5 245
dfd2257a 246/* Return the final value of a plt relocation. */
c2a32973 247static inline Elf32_Addr
dfd2257a
UD
248elf_machine_plt_value (struct link_map *map, const Elf32_Rela *reloc,
249 Elf32_Addr value)
250{
251 return value + reloc->r_addend;
252}
253
2413fdba
UD
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
1f205a47 259#endif /* dl_machine_h */
4cca6b86 260
2413fdba 261#ifdef RESOLVE_MAP
4cca6b86 262
052b6a6c
UD
263/* Do the actual processing of a reloc, once its target address
264 has been determined. */
265extern void __process_machine_rela (struct link_map *map,
266 const Elf32_Rela *reloc,
545dbc93 267 struct link_map *sym_map,
052b6a6c
UD
268 const Elf32_Sym *sym,
269 const Elf32_Sym *refsym,
270 Elf32_Addr *const reloc_addr,
271 Elf32_Addr finaladdr,
7551556f
RM
272 int rinfo) attribute_hidden;
273
274/* Call _dl_signal_error when a resolved value overflows a relocated area. */
275extern void _dl_reloc_overflow (struct link_map *map,
276 const char *name,
277 Elf32_Addr *const reloc_addr,
7551556f 278 const Elf32_Sym *refsym) attribute_hidden;
052b6a6c 279
1f205a47
UD
280/* Perform the relocation specified by RELOC and SYM (which is fully resolved).
281 LOADADDR is the load address of the object; INFO is an array indexed
282 by DT_* of the .dynamic section info. */
4cca6b86 283
7de00121 284auto inline void __attribute__ ((always_inline))
1f205a47 285elf_machine_rela (struct link_map *map, const Elf32_Rela *reloc,
3996f34b 286 const Elf32_Sym *sym, const struct r_found_version *version,
87d254a7 287 void *const reloc_addr_arg)
1f205a47 288{
87d254a7 289 Elf32_Addr *const reloc_addr = reloc_addr_arg;
1f205a47 290 const Elf32_Sym *const refsym = sym;
bb0ddc2f
RM
291 Elf32_Addr value;
292 const int r_type = ELF32_R_TYPE (reloc->r_info);
545dbc93 293 struct link_map *sym_map = NULL;
4cca6b86 294
aa3f2410 295#ifndef RESOLVE_CONFLICT_FIND_MAP
bb0ddc2f 296 if (r_type == R_PPC_RELATIVE)
1f205a47 297 {
bb0ddc2f
RM
298 *reloc_addr = map->l_addr + reloc->r_addend;
299 return;
1f205a47 300 }
bb0ddc2f
RM
301
302 if (__builtin_expect (r_type == R_PPC_NONE, 0))
303 return;
304
213cdddb
RM
305 /* binutils on ppc32 includes st_value in r_addend for relocations
306 against local symbols. */
307 if (__builtin_expect (ELF32_ST_BIND (sym->st_info) == STB_LOCAL, 0)
308 && sym->st_shndx != SHN_UNDEF)
309 value = map->l_addr;
310 else
311 {
213cdddb 312 sym_map = RESOLVE_MAP (&sym, version, r_type);
d026cf5f 313 value = sym_map == NULL ? 0 : sym_map->l_addr + sym->st_value;
213cdddb 314 }
bb0ddc2f 315 value += reloc->r_addend;
aa3f2410
UD
316#else
317 value = reloc->r_addend;
318#endif
4cca6b86 319
052b6a6c
UD
320 /* A small amount of code is duplicated here for speed. In libc,
321 more than 90% of the relocs are R_PPC_RELATIVE; in the X11 shared
322 libraries, 60% are R_PPC_RELATIVE, 24% are R_PPC_GLOB_DAT or
323 R_PPC_ADDR32, and 16% are R_PPC_JMP_SLOT (which this routine
324 wouldn't usually handle). As an bonus, doing this here allows
325 the switch statement in __process_machine_rela to work. */
bb0ddc2f 326 switch (r_type)
1f205a47 327 {
bb0ddc2f
RM
328 case R_PPC_GLOB_DAT:
329 case R_PPC_ADDR32:
330 *reloc_addr = value;
331 break;
332
32c075e1 333#if defined USE_TLS && (!defined RTLD_BOOTSTRAP || USE___THREAD) \
545dbc93 334 && !defined RESOLVE_CONFLICT_FIND_MAP
7551556f
RM
335# ifdef RTLD_BOOTSTRAP
336# define NOT_BOOTSTRAP 0
337# else
338# define NOT_BOOTSTRAP 1
bb0ddc2f 339# endif
7551556f 340
545dbc93
RM
341 case R_PPC_DTPMOD32:
342 if (!NOT_BOOTSTRAP)
343 /* During startup the dynamic linker is always index 1. */
344 *reloc_addr = 1;
345 else if (sym_map != NULL)
346 /* Get the information from the link map returned by the
347 RESOLVE_MAP function. */
348 *reloc_addr = sym_map->l_tls_modid;
349 break;
350 case R_PPC_DTPREL32:
351 /* During relocation all TLS symbols are defined and used.
352 Therefore the offset is already correct. */
353 if (NOT_BOOTSTRAP && sym_map != NULL)
354 *reloc_addr = TLS_DTPREL_VALUE (sym, reloc);
355 break;
356 case R_PPC_TPREL32:
357 if (!NOT_BOOTSTRAP || sym_map != NULL)
bb0ddc2f 358 {
545dbc93
RM
359 if (NOT_BOOTSTRAP)
360 CHECK_STATIC_TLS (map, sym_map);
361 *reloc_addr = TLS_TPREL_VALUE (sym_map, sym, reloc);
bb0ddc2f 362 }
545dbc93 363 break;
32c075e1 364#endif /* USE_TLS etc. */
bb0ddc2f 365
a334319f 366 case R_PPC_JMP_SLOT:
99c7f870 367#ifdef RESOLVE_CONFLICT_FIND_MAP
bb0ddc2f 368 RESOLVE_CONFLICT_FIND_MAP (map, reloc_addr);
a334319f 369#endif
99c7f870
UD
370 if (map->l_info[DT_PPC(GOT)] != 0)
371 {
372 *reloc_addr = value;
373 break;
374 }
375 /* FALLTHROUGH */
bb0ddc2f
RM
376
377 default:
545dbc93 378 __process_machine_rela (map, reloc, sym_map, sym, refsym,
bb0ddc2f 379 reloc_addr, value, r_type);
1f205a47 380 }
1f205a47
UD
381}
382
7de00121 383auto inline void __attribute__ ((always_inline))
1f2a1df3 384elf_machine_rela_relative (Elf32_Addr l_addr, const Elf32_Rela *reloc,
87d254a7 385 void *const reloc_addr_arg)
1721af3f 386{
87d254a7 387 Elf32_Addr *const reloc_addr = reloc_addr_arg;
1721af3f
UD
388 *reloc_addr = l_addr + reloc->r_addend;
389}
1f205a47 390
7de00121 391auto inline void __attribute__ ((always_inline))
7ba7c829
UD
392elf_machine_lazy_rel (struct link_map *map,
393 Elf32_Addr l_addr, const Elf32_Rela *reloc)
394{
395 /* elf_machine_runtime_setup handles this. */
396}
397
052b6a6c
UD
398/* The SVR4 ABI specifies that the JMPREL relocs must be inside the
399 DT_RELA table. */
400#define ELF_MACHINE_PLTREL_OVERLAP 1
401
2413fdba 402#endif /* RESOLVE_MAP */