Replace FSF snail mail address with URLs.
[glibc.git] / sysdeps / powerpc / powerpc32 / dl-machine.h
blobb3177d1591f7af98817cf5197927555ed7d82f98
1 /* Machine-dependent ELF dynamic relocation inline functions. PowerPC version.
2 Copyright (C) 1995-2003, 2005, 2006, 2011 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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.
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.
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 <http://www.gnu.org/licenses/>. */
19 #ifndef dl_machine_h
20 #define dl_machine_h
22 #define ELF_MACHINE_NAME "powerpc"
24 #include <assert.h>
25 #include <dl-tls.h>
27 /* Translate a processor specific dynamic tag to the index
28 in l_info array. */
29 #define DT_PPC(x) (DT_PPC_##x - DT_LOPROC + DT_NUM)
31 /* Return nonzero iff ELF header is compatible with the running host. */
32 static inline int
33 elf_machine_matches_host (const Elf32_Ehdr *ehdr)
35 return ehdr->e_machine == EM_PPC;
38 /* Return the value of the GOT pointer. */
39 static inline Elf32_Addr * __attribute__ ((const))
40 ppc_got (void)
42 Elf32_Addr *got;
44 asm ("bcl 20,31,1f\n"
45 "1: mflr %0\n"
46 " addis %0,%0,_GLOBAL_OFFSET_TABLE_-1b@ha\n"
47 " addi %0,%0,_GLOBAL_OFFSET_TABLE_-1b@l\n"
48 : "=b" (got) : : "lr");
50 return got;
53 /* Return the link-time address of _DYNAMIC, stored as
54 the first value in the GOT. */
55 static inline Elf32_Addr __attribute__ ((const))
56 elf_machine_dynamic (void)
58 return *ppc_got ();
61 /* Return the run-time load address of the shared object. */
62 static inline Elf32_Addr __attribute__ ((const))
63 elf_machine_load_address (void)
65 Elf32_Addr *branchaddr;
66 Elf32_Addr runtime_dynamic;
68 /* This is much harder than you'd expect. Possibly I'm missing something.
69 The 'obvious' way:
71 Apparently, "bcl 20,31,$+4" is what should be used to load LR
72 with the address of the next instruction.
73 I think this is so that machines that do bl/blr pairing don't
74 get confused.
76 asm ("bcl 20,31,0f ;"
77 "0: mflr 0 ;"
78 "lis %0,0b@ha;"
79 "addi %0,%0,0b@l;"
80 "subf %0,%0,0"
81 : "=b" (addr) : : "r0", "lr");
83 doesn't work, because the linker doesn't have to (and in fact doesn't)
84 update the @ha and @l references; the loader (which runs after this
85 code) will do that.
87 Instead, we use the following trick:
89 The linker puts the _link-time_ address of _DYNAMIC at the first
90 word in the GOT. We could branch to that address, if we wanted,
91 by using an @local reloc; the linker works this out, so it's safe
92 to use now. We can't, of course, actually branch there, because
93 we'd cause an illegal instruction exception; so we need to compute
94 the address ourselves. That gives us the following code: */
96 /* Get address of the 'b _DYNAMIC@local'... */
97 asm ("bcl 20,31,0f;"
98 "b _DYNAMIC@local;"
99 "0:"
100 : "=l" (branchaddr));
102 /* So now work out the difference between where the branch actually points,
103 and the offset of that location in memory from the start of the file. */
104 runtime_dynamic = ((Elf32_Addr) branchaddr
105 + ((Elf32_Sword) (*branchaddr << 6 & 0xffffff00) >> 6));
107 return runtime_dynamic - elf_machine_dynamic ();
110 #define ELF_MACHINE_BEFORE_RTLD_RELOC(dynamic_info) /* nothing */
112 /* The PLT uses Elf32_Rela relocs. */
113 #define elf_machine_relplt elf_machine_rela
115 /* Mask identifying addresses reserved for the user program,
116 where the dynamic linker should not map anything. */
117 #define ELF_MACHINE_USER_ADDRESS_MASK 0xf0000000UL
119 /* The actual _start code is in dl-start.S. Use a really
120 ugly bit of assembler to let dl-start.o see _dl_start. */
121 #define RTLD_START asm (".globl _dl_start");
123 /* Decide where a relocatable object should be loaded. */
124 extern ElfW(Addr)
125 __elf_preferred_address(struct link_map *loader, size_t maplength,
126 ElfW(Addr) mapstartpref);
127 #define ELF_PREFERRED_ADDRESS(loader, maplength, mapstartpref) \
128 __elf_preferred_address (loader, maplength, mapstartpref)
130 /* ELF_RTYPE_CLASS_PLT iff TYPE describes relocation of a PLT entry, so
131 PLT entries should not be allowed to define the value.
132 ELF_RTYPE_CLASS_NOCOPY iff TYPE should not be allowed to resolve to one
133 of the main executable's symbols, as for a COPY reloc. */
134 /* We never want to use a PLT entry as the destination of a
135 reloc, when what is being relocated is a branch. This is
136 partly for efficiency, but mostly so we avoid loops. */
137 #define elf_machine_type_class(type) \
138 ((((type) == R_PPC_JMP_SLOT \
139 || (type) == R_PPC_REL24 \
140 || ((type) >= R_PPC_DTPMOD32 /* contiguous TLS */ \
141 && (type) <= R_PPC_DTPREL32) \
142 || (type) == R_PPC_ADDR24) * ELF_RTYPE_CLASS_PLT) \
143 | (((type) == R_PPC_COPY) * ELF_RTYPE_CLASS_COPY))
145 /* A reloc type used for ld.so cmdline arg lookups to reject PLT entries. */
146 #define ELF_MACHINE_JMP_SLOT R_PPC_JMP_SLOT
148 /* The PowerPC never uses REL relocations. */
149 #define ELF_MACHINE_NO_REL 1
151 /* Set up the loaded object described by MAP so its unrelocated PLT
152 entries will jump to the on-demand fixup code in dl-runtime.c.
153 Also install a small trampoline to be used by entries that have
154 been relocated to an address too far away for a single branch. */
155 extern int __elf_machine_runtime_setup (struct link_map *map,
156 int lazy, int profile);
158 static inline int
159 elf_machine_runtime_setup (struct link_map *map,
160 int lazy, int profile)
162 if (map->l_info[DT_JMPREL] == 0)
163 return lazy;
165 if (map->l_info[DT_PPC(GOT)] == 0)
166 /* Handle old style PLT. */
167 return __elf_machine_runtime_setup (map, lazy, profile);
169 /* New style non-exec PLT consisting of an array of addresses. */
170 map->l_info[DT_PPC(GOT)]->d_un.d_ptr += map->l_addr;
171 if (lazy)
173 Elf32_Addr *plt, *got, glink;
174 Elf32_Word num_plt_entries;
175 void (*dlrr) (void);
176 extern void _dl_runtime_resolve (void);
177 extern void _dl_prof_resolve (void);
179 if (__builtin_expect (!profile, 1))
180 dlrr = _dl_runtime_resolve;
181 else
183 if (GLRO(dl_profile) != NULL
184 &&_dl_name_match_p (GLRO(dl_profile), map))
185 GL(dl_profile_map) = map;
186 dlrr = _dl_prof_resolve;
188 got = (Elf32_Addr *) map->l_info[DT_PPC(GOT)]->d_un.d_ptr;
189 glink = got[1];
190 got[1] = (Elf32_Addr) dlrr;
191 got[2] = (Elf32_Addr) map;
193 /* Relocate everything in .plt by the load address offset. */
194 plt = (Elf32_Addr *) D_PTR (map, l_info[DT_PLTGOT]);
195 num_plt_entries = (map->l_info[DT_PLTRELSZ]->d_un.d_val
196 / sizeof (Elf32_Rela));
198 /* If a library is prelinked but we have to relocate anyway,
199 we have to be able to undo the prelinking of .plt section.
200 The prelinker saved us at got[1] address of .glink
201 section's start. */
202 if (glink)
204 glink += map->l_addr;
205 while (num_plt_entries-- != 0)
206 *plt++ = glink, glink += 4;
208 else
209 while (num_plt_entries-- != 0)
210 *plt++ += map->l_addr;
212 return lazy;
215 /* Change the PLT entry whose reloc is 'reloc' to call the actual routine. */
216 extern Elf32_Addr __elf_machine_fixup_plt (struct link_map *map,
217 Elf32_Addr *reloc_addr,
218 Elf32_Addr finaladdr);
220 static inline Elf32_Addr
221 elf_machine_fixup_plt (struct link_map *map, lookup_t t,
222 const Elf32_Rela *reloc,
223 Elf32_Addr *reloc_addr, Elf64_Addr finaladdr)
225 if (map->l_info[DT_PPC(GOT)] == 0)
226 /* Handle old style PLT. */
227 return __elf_machine_fixup_plt (map, reloc_addr, finaladdr);
229 *reloc_addr = finaladdr;
230 return finaladdr;
233 /* Return the final value of a plt relocation. */
234 static inline Elf32_Addr
235 elf_machine_plt_value (struct link_map *map, const Elf32_Rela *reloc,
236 Elf32_Addr value)
238 return value + reloc->r_addend;
242 /* Names of the architecture-specific auditing callback functions. */
243 #define ARCH_LA_PLTENTER ppc32_gnu_pltenter
244 #define ARCH_LA_PLTEXIT ppc32_gnu_pltexit
246 #endif /* dl_machine_h */
248 #ifdef RESOLVE_MAP
250 /* Do the actual processing of a reloc, once its target address
251 has been determined. */
252 extern void __process_machine_rela (struct link_map *map,
253 const Elf32_Rela *reloc,
254 struct link_map *sym_map,
255 const Elf32_Sym *sym,
256 const Elf32_Sym *refsym,
257 Elf32_Addr *const reloc_addr,
258 Elf32_Addr finaladdr,
259 int rinfo) attribute_hidden;
261 /* Call _dl_signal_error when a resolved value overflows a relocated area. */
262 extern void _dl_reloc_overflow (struct link_map *map,
263 const char *name,
264 Elf32_Addr *const reloc_addr,
265 const Elf32_Sym *refsym) attribute_hidden;
267 /* Perform the relocation specified by RELOC and SYM (which is fully resolved).
268 LOADADDR is the load address of the object; INFO is an array indexed
269 by DT_* of the .dynamic section info. */
271 auto inline void __attribute__ ((always_inline))
272 elf_machine_rela (struct link_map *map, const Elf32_Rela *reloc,
273 const Elf32_Sym *sym, const struct r_found_version *version,
274 void *const reloc_addr_arg, int skip_ifunc)
276 Elf32_Addr *const reloc_addr = reloc_addr_arg;
277 const Elf32_Sym *const refsym = sym;
278 Elf32_Addr value;
279 const int r_type = ELF32_R_TYPE (reloc->r_info);
280 struct link_map *sym_map = NULL;
282 #ifndef RESOLVE_CONFLICT_FIND_MAP
283 if (r_type == R_PPC_RELATIVE)
285 *reloc_addr = map->l_addr + reloc->r_addend;
286 return;
289 if (__builtin_expect (r_type == R_PPC_NONE, 0))
290 return;
292 /* binutils on ppc32 includes st_value in r_addend for relocations
293 against local symbols. */
294 if (__builtin_expect (ELF32_ST_BIND (sym->st_info) == STB_LOCAL, 0)
295 && sym->st_shndx != SHN_UNDEF)
296 value = map->l_addr;
297 else
299 sym_map = RESOLVE_MAP (&sym, version, r_type);
300 value = sym_map == NULL ? 0 : sym_map->l_addr + sym->st_value;
302 value += reloc->r_addend;
303 #else
304 value = reloc->r_addend;
305 #endif
307 if (sym != NULL
308 && __builtin_expect (ELFW(ST_TYPE) (sym->st_info) == STT_GNU_IFUNC, 0)
309 && __builtin_expect (sym->st_shndx != SHN_UNDEF, 1)
310 && __builtin_expect (!skip_ifunc, 1))
311 value = ((Elf32_Addr (*) (void)) value) ();
313 /* A small amount of code is duplicated here for speed. In libc,
314 more than 90% of the relocs are R_PPC_RELATIVE; in the X11 shared
315 libraries, 60% are R_PPC_RELATIVE, 24% are R_PPC_GLOB_DAT or
316 R_PPC_ADDR32, and 16% are R_PPC_JMP_SLOT (which this routine
317 wouldn't usually handle). As an bonus, doing this here allows
318 the switch statement in __process_machine_rela to work. */
319 switch (r_type)
321 case R_PPC_GLOB_DAT:
322 case R_PPC_ADDR32:
323 *reloc_addr = value;
324 break;
326 #ifndef RESOLVE_CONFLICT_FIND_MAP
327 # ifdef RTLD_BOOTSTRAP
328 # define NOT_BOOTSTRAP 0
329 # else
330 # define NOT_BOOTSTRAP 1
331 # endif
333 case R_PPC_DTPMOD32:
334 if (!NOT_BOOTSTRAP)
335 /* During startup the dynamic linker is always index 1. */
336 *reloc_addr = 1;
337 else if (sym_map != NULL)
338 /* Get the information from the link map returned by the
339 RESOLVE_MAP function. */
340 *reloc_addr = sym_map->l_tls_modid;
341 break;
342 case R_PPC_DTPREL32:
343 /* During relocation all TLS symbols are defined and used.
344 Therefore the offset is already correct. */
345 if (NOT_BOOTSTRAP && sym_map != NULL)
346 *reloc_addr = TLS_DTPREL_VALUE (sym, reloc);
347 break;
348 case R_PPC_TPREL32:
349 if (!NOT_BOOTSTRAP || sym_map != NULL)
351 if (NOT_BOOTSTRAP)
352 CHECK_STATIC_TLS (map, sym_map);
353 *reloc_addr = TLS_TPREL_VALUE (sym_map, sym, reloc);
355 break;
356 #endif
358 case R_PPC_JMP_SLOT:
359 #ifdef RESOLVE_CONFLICT_FIND_MAP
360 RESOLVE_CONFLICT_FIND_MAP (map, reloc_addr);
361 #endif
362 if (map->l_info[DT_PPC(GOT)] != 0)
364 *reloc_addr = value;
365 break;
367 /* FALLTHROUGH */
369 default:
370 __process_machine_rela (map, reloc, sym_map, sym, refsym,
371 reloc_addr, value, r_type);
375 auto inline void __attribute__ ((always_inline))
376 elf_machine_rela_relative (Elf32_Addr l_addr, const Elf32_Rela *reloc,
377 void *const reloc_addr_arg)
379 Elf32_Addr *const reloc_addr = reloc_addr_arg;
380 *reloc_addr = l_addr + reloc->r_addend;
383 auto inline void __attribute__ ((always_inline))
384 elf_machine_lazy_rel (struct link_map *map,
385 Elf32_Addr l_addr, const Elf32_Rela *reloc,
386 int skip_ifunc)
388 /* elf_machine_runtime_setup handles this. */
391 /* The SVR4 ABI specifies that the JMPREL relocs must be inside the
392 DT_RELA table. */
393 #define ELF_MACHINE_PLTREL_OVERLAP 1
395 #endif /* RESOLVE_MAP */