1 /* Machine-dependent ELF dynamic relocation inline functions. PowerPC version.
2 Copyright (C) 1995-2002, 2003 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, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
23 #define ELF_MACHINE_NAME "powerpc"
28 /* Return nonzero iff ELF header is compatible with the running host. */
30 elf_machine_matches_host (const Elf32_Ehdr *ehdr)
32 return ehdr->e_machine == EM_PPC;
36 /* Return the link-time address of _DYNAMIC, stored as
37 the first value in the GOT. */
38 static inline Elf32_Addr
39 elf_machine_dynamic (void)
42 asm (" bl _GLOBAL_OFFSET_TABLE_-4@local"
47 /* Return the run-time load address of the shared object. */
48 static inline Elf32_Addr
49 elf_machine_load_address (void)
52 unsigned int *branchaddr;
54 /* This is much harder than you'd expect. Possibly I'm missing something.
57 Apparently, "bcl 20,31,$+4" is what should be used to load LR
58 with the address of the next instruction.
59 I think this is so that machines that do bl/blr pairing don't
67 : "=b" (addr) : : "r0", "lr");
69 doesn't work, because the linker doesn't have to (and in fact doesn't)
70 update the @ha and @l references; the loader (which runs after this
73 Instead, we use the following trick:
75 The linker puts the _link-time_ address of _DYNAMIC at the first
76 word in the GOT. We could branch to that address, if we wanted,
77 by using an @local reloc; the linker works this out, so it's safe
78 to use now. We can't, of course, actually branch there, because
79 we'd cause an illegal instruction exception; so we need to compute
80 the address ourselves. That gives us the following code: */
82 /* Get address of the 'b _DYNAMIC@local'... */
88 /* ... and the address of the GOT. */
89 asm (" bl _GLOBAL_OFFSET_TABLE_-4@local"
92 /* So now work out the difference between where the branch actually points,
93 and the offset of that location in memory from the start of the file. */
94 return ((Elf32_Addr)branchaddr - *got
95 + ((int)(*branchaddr << 6 & 0xffffff00) >> 6));
98 #define ELF_MACHINE_BEFORE_RTLD_RELOC(dynamic_info) /* nothing */
100 /* The PLT uses Elf32_Rela relocs. */
101 #define elf_machine_relplt elf_machine_rela
103 /* This code is used in dl-runtime.c to call the `fixup' function
104 and then redirect to the address it returns. It is called
105 from code built in the PLT by elf_machine_runtime_setup. */
107 #define ELF_MACHINE_RUNTIME_TRAMPOLINE asm ("\n\
108 .section \".text\" \n\
110 .globl _dl_runtime_resolve \n\
111 .type _dl_runtime_resolve,@function \n\
112 _dl_runtime_resolve: \n\
113 # We need to save the registers used to pass parameters, and register 0,\n\
114 # which is used by _mcount; the registers are saved in a stack frame.\n\
119 # The code that calls this has put parameters for `fixup' in r12 and r11.\n\
125 # We also need to save some of the condition register fields.\n\
134 # 'fixup' returns the address we want to branch to.\n\
136 # Put the registers back...\n\
150 # ...unwind the stack frame, and jump to the PLT entry we updated.\n\
153 .size _dl_runtime_resolve,.-_dl_runtime_resolve \n\
156 .globl _dl_prof_resolve \n\
157 .type _dl_prof_resolve,@function \n\
158 _dl_prof_resolve: \n\
159 # We need to save the registers used to pass parameters, and register 0,\n\
160 # which is used by _mcount; the registers are saved in a stack frame.\n\
165 # The code that calls this has put parameters for `fixup' in r12 and r11.\n\
171 # We also need to save some of the condition register fields.\n\
179 bl profile_fixup@local \n\
180 # 'fixup' returns the address we want to branch to.\n\
182 # Put the registers back...\n\
196 # ...unwind the stack frame, and jump to the PLT entry we updated.\n\
199 .size _dl_prof_resolve,.-_dl_prof_resolve \n\
200 # Undo '.section text'.\n\
204 # define ELF_MACHINE_RUNTIME_TRAMPOLINE asm ("\n\
205 .section \".text\" \n\
207 .globl _dl_runtime_resolve \n\
208 .globl _dl_prof_resolve \n\
209 .type _dl_runtime_resolve,@function \n\
210 .type _dl_prof_resolve,@function \n\
211 _dl_runtime_resolve: \n\
212 _dl_prof_resolve: \n\
213 # We need to save the registers used to pass parameters, and register 0,\n\
214 # which is used by _mcount; the registers are saved in a stack frame.\n\
219 # The code that calls this has put parameters for `fixup' in r12 and r11.\n\
225 # We also need to save some of the condition register fields.\n\
234 # 'fixup' returns the address we want to branch to.\n\
236 # Put the registers back...\n\
250 # ...unwind the stack frame, and jump to the PLT entry we updated.\n\
253 .size _dl_runtime_resolve,.-_dl_runtime_resolve \n\
257 /* Mask identifying addresses reserved for the user program,
258 where the dynamic linker should not map anything. */
259 #define ELF_MACHINE_USER_ADDRESS_MASK 0xf0000000UL
261 /* The actual _start code is in dl-start.S. Use a really
262 ugly bit of assembler to let dl-start.o see _dl_start. */
263 #define RTLD_START asm (".globl _dl_start");
265 /* Decide where a relocatable object should be loaded. */
267 __elf_preferred_address(struct link_map *loader, size_t maplength,
268 ElfW(Addr) mapstartpref);
269 #define ELF_PREFERRED_ADDRESS(loader, maplength, mapstartpref) \
270 __elf_preferred_address (loader, maplength, mapstartpref)
272 /* ELF_RTYPE_CLASS_PLT iff TYPE describes relocation of a PLT entry, so
273 PLT entries should not be allowed to define the value.
274 ELF_RTYPE_CLASS_NOCOPY iff TYPE should not be allowed to resolve to one
275 of the main executable's symbols, as for a COPY reloc. */
276 /* We never want to use a PLT entry as the destination of a
277 reloc, when what is being relocated is a branch. This is
278 partly for efficiency, but mostly so we avoid loops. */
279 #if defined USE_TLS && (!defined RTLD_BOOTSTRAP || USE___THREAD)
280 #define elf_machine_type_class(type) \
281 ((((type) == R_PPC_JMP_SLOT \
282 || (type) == R_PPC_REL24 \
283 || ((type) >= R_PPC_DTPMOD32 /* contiguous TLS */ \
284 && (type) <= R_PPC_DTPREL32) \
285 || (type) == R_PPC_ADDR24) * ELF_RTYPE_CLASS_PLT) \
286 | (((type) == R_PPC_COPY) * ELF_RTYPE_CLASS_COPY))
288 #define elf_machine_type_class(type) \
289 ((((type) == R_PPC_JMP_SLOT \
290 || (type) == R_PPC_REL24 \
291 || (type) == R_PPC_ADDR24) * ELF_RTYPE_CLASS_PLT) \
292 | (((type) == R_PPC_COPY) * ELF_RTYPE_CLASS_COPY))
295 /* A reloc type used for ld.so cmdline arg lookups to reject PLT entries. */
296 #define ELF_MACHINE_JMP_SLOT R_PPC_JMP_SLOT
298 /* The PowerPC never uses REL relocations. */
299 #define ELF_MACHINE_NO_REL 1
301 /* Set up the loaded object described by L so its unrelocated PLT
302 entries will jump to the on-demand fixup code in dl-runtime.c.
303 Also install a small trampoline to be used by entries that have
304 been relocated to an address too far away for a single branch. */
305 extern int __elf_machine_runtime_setup (struct link_map *map,
306 int lazy, int profile);
307 #define elf_machine_runtime_setup __elf_machine_runtime_setup
309 /* Change the PLT entry whose reloc is 'reloc' to call the actual routine. */
310 extern Elf32_Addr __elf_machine_fixup_plt (struct link_map *map,
311 const Elf32_Rela *reloc,
312 Elf32_Addr *reloc_addr,
313 Elf32_Addr finaladdr);
315 static inline Elf32_Addr
316 elf_machine_fixup_plt (struct link_map *map, lookup_t t,
317 const Elf32_Rela *reloc,
318 Elf32_Addr *reloc_addr, Elf64_Addr finaladdr)
320 return __elf_machine_fixup_plt (map, reloc, reloc_addr, finaladdr);
323 /* Return the final value of a plt relocation. */
324 static inline Elf32_Addr
325 elf_machine_plt_value (struct link_map *map, const Elf32_Rela *reloc,
328 return value + reloc->r_addend;
331 #endif /* dl_machine_h */
335 /* Do the actual processing of a reloc, once its target address
336 has been determined. */
337 extern void __process_machine_rela (struct link_map *map,
338 const Elf32_Rela *reloc,
339 struct link_map *sym_map,
340 const Elf32_Sym *sym,
341 const Elf32_Sym *refsym,
342 Elf32_Addr *const reloc_addr,
343 Elf32_Addr finaladdr,
344 int rinfo) attribute_hidden;
346 /* Call _dl_signal_error when a resolved value overflows a relocated area. */
347 extern void _dl_reloc_overflow (struct link_map *map,
349 Elf32_Addr *const reloc_addr,
350 const Elf32_Sym *sym,
351 const Elf32_Sym *refsym) attribute_hidden;
353 /* Perform the relocation specified by RELOC and SYM (which is fully resolved).
354 LOADADDR is the load address of the object; INFO is an array indexed
355 by DT_* of the .dynamic section info. */
358 elf_machine_rela (struct link_map *map, const Elf32_Rela *reloc,
359 const Elf32_Sym *sym, const struct r_found_version *version,
360 Elf32_Addr *const reloc_addr)
362 const Elf32_Sym *const refsym = sym;
364 const int r_type = ELF32_R_TYPE (reloc->r_info);
365 struct link_map *sym_map = NULL;
367 #ifndef RESOLVE_CONFLICT_FIND_MAP
368 if (r_type == R_PPC_RELATIVE)
370 *reloc_addr = map->l_addr + reloc->r_addend;
374 if (__builtin_expect (r_type == R_PPC_NONE, 0))
377 /* binutils on ppc32 includes st_value in r_addend for relocations
378 against local symbols. */
379 if (__builtin_expect (ELF32_ST_BIND (sym->st_info) == STB_LOCAL, 0)
380 && sym->st_shndx != SHN_UNDEF)
384 # if defined USE_TLS && !defined RTLD_BOOTSTRAP
385 sym_map = RESOLVE_MAP (&sym, version, r_type);
386 value = sym == NULL ? 0 : sym_map->l_addr + sym->st_value;
388 value = RESOLVE (&sym, version, r_type);
389 # ifndef RTLD_BOOTSTRAP
392 value += sym->st_value;
395 value += reloc->r_addend;
397 value = reloc->r_addend;
400 /* A small amount of code is duplicated here for speed. In libc,
401 more than 90% of the relocs are R_PPC_RELATIVE; in the X11 shared
402 libraries, 60% are R_PPC_RELATIVE, 24% are R_PPC_GLOB_DAT or
403 R_PPC_ADDR32, and 16% are R_PPC_JMP_SLOT (which this routine
404 wouldn't usually handle). As an bonus, doing this here allows
405 the switch statement in __process_machine_rela to work. */
413 #if defined USE_TLS && (!defined RTLD_BOOTSTRAP || USE___THREAD) \
414 && !defined RESOLVE_CONFLICT_FIND_MAP
415 # ifdef RTLD_BOOTSTRAP
416 # define NOT_BOOTSTRAP 0
418 # define NOT_BOOTSTRAP 1
423 /* During startup the dynamic linker is always index 1. */
425 else if (sym_map != NULL)
426 /* Get the information from the link map returned by the
427 RESOLVE_MAP function. */
428 *reloc_addr = sym_map->l_tls_modid;
431 /* During relocation all TLS symbols are defined and used.
432 Therefore the offset is already correct. */
433 if (NOT_BOOTSTRAP && sym_map != NULL)
434 *reloc_addr = TLS_DTPREL_VALUE (sym, reloc);
437 if (!NOT_BOOTSTRAP || sym_map != NULL)
440 CHECK_STATIC_TLS (map, sym_map);
441 *reloc_addr = TLS_TPREL_VALUE (sym_map, sym, reloc);
444 #endif /* USE_TLS etc. */
446 #ifdef RESOLVE_CONFLICT_FIND_MAP
448 RESOLVE_CONFLICT_FIND_MAP (map, reloc_addr);
453 __process_machine_rela (map, reloc, sym_map, sym, refsym,
454 reloc_addr, value, r_type);
459 elf_machine_rela_relative (Elf32_Addr l_addr, const Elf32_Rela *reloc,
460 Elf32_Addr *const reloc_addr)
462 *reloc_addr = l_addr + reloc->r_addend;
466 elf_machine_lazy_rel (struct link_map *map,
467 Elf32_Addr l_addr, const Elf32_Rela *reloc)
469 /* elf_machine_runtime_setup handles this. */
472 /* The SVR4 ABI specifies that the JMPREL relocs must be inside the
474 #define ELF_MACHINE_PLTREL_OVERLAP 1