1 /* Machine-dependent ELF dynamic relocation inline functions. PowerPC version.
2 Copyright (C) 1995-2016 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/>. */
22 #define ELF_MACHINE_NAME "powerpc"
27 #include <hwcapinfo.h>
29 /* Translate a processor specific dynamic tag to the index
31 #define DT_PPC(x) (DT_PPC_##x - DT_LOPROC + DT_NUM)
33 /* Return nonzero iff ELF header is compatible with the running host. */
35 elf_machine_matches_host (const Elf32_Ehdr
*ehdr
)
37 return ehdr
->e_machine
== EM_PPC
;
40 /* Return the value of the GOT pointer. */
41 static inline Elf32_Addr
* __attribute__ ((const))
48 " addis %0,%0,_GLOBAL_OFFSET_TABLE_-1b@ha\n"
49 " addi %0,%0,_GLOBAL_OFFSET_TABLE_-1b@l\n"
50 : "=b" (got
) : : "lr");
55 /* Return the link-time address of _DYNAMIC, stored as
56 the first value in the GOT. */
57 static inline Elf32_Addr
__attribute__ ((const))
58 elf_machine_dynamic (void)
63 /* Return the run-time load address of the shared object. */
64 static inline Elf32_Addr
__attribute__ ((const))
65 elf_machine_load_address (void)
67 Elf32_Addr
*branchaddr
;
68 Elf32_Addr runtime_dynamic
;
70 /* This is much harder than you'd expect. Possibly I'm missing something.
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
83 : "=b" (addr) : : "r0", "lr");
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
89 Instead, we use the following trick:
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: */
98 /* Get address of the 'b _DYNAMIC@local'... */
102 : "=l" (branchaddr
));
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. */
106 runtime_dynamic
= ((Elf32_Addr
) branchaddr
107 + ((Elf32_Sword
) (*branchaddr
<< 6 & 0xffffff00) >> 6));
109 return runtime_dynamic
- elf_machine_dynamic ();
112 #define ELF_MACHINE_BEFORE_RTLD_RELOC(dynamic_info) /* nothing */
114 /* The PLT uses Elf32_Rela relocs. */
115 #define elf_machine_relplt elf_machine_rela
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
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");
125 /* Decide where a relocatable object should be loaded. */
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)
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))
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
150 /* The PowerPC never uses REL relocations. */
151 #define ELF_MACHINE_NO_REL 1
152 #define ELF_MACHINE_NO_RELA 0
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. */
158 # define DL_PLATFORM_INIT dl_platform_init ()
160 static inline void __attribute__ ((unused
))
161 dl_platform_init (void)
163 __tcb_parse_hwcap_and_convert_at_platform ();
167 /* Set up the loaded object described by MAP so its unrelocated PLT
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. */
171 extern int __elf_machine_runtime_setup (struct link_map
*map
,
172 int lazy
, int profile
);
175 elf_machine_runtime_setup (struct link_map
*map
,
176 int lazy
, int profile
)
178 if (map
->l_info
[DT_JMPREL
] == 0)
181 if (map
->l_info
[DT_PPC(GOT
)] == 0)
182 /* Handle old style PLT. */
183 return __elf_machine_runtime_setup (map
, lazy
, profile
);
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
;
189 Elf32_Addr
*plt
, *got
, glink
;
190 Elf32_Word num_plt_entries
;
192 extern void _dl_runtime_resolve (void);
193 extern void _dl_prof_resolve (void);
195 if (__glibc_likely (!profile
))
196 dlrr
= _dl_runtime_resolve
;
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
;
204 got
= (Elf32_Addr
*) map
->l_info
[DT_PPC(GOT
)]->d_un
.d_ptr
;
206 got
[1] = (Elf32_Addr
) dlrr
;
207 got
[2] = (Elf32_Addr
) map
;
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
));
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
220 glink
+= map
->l_addr
;
221 while (num_plt_entries
-- != 0)
222 *plt
++ = glink
, glink
+= 4;
225 while (num_plt_entries
-- != 0)
226 *plt
++ += map
->l_addr
;
231 /* Change the PLT entry whose reloc is 'reloc' to call the actual routine. */
232 extern Elf32_Addr
__elf_machine_fixup_plt (struct link_map
*map
,
233 Elf32_Addr
*reloc_addr
,
234 Elf32_Addr finaladdr
);
236 static inline Elf32_Addr
237 elf_machine_fixup_plt (struct link_map
*map
, lookup_t t
,
238 const Elf32_Rela
*reloc
,
239 Elf32_Addr
*reloc_addr
, Elf64_Addr finaladdr
)
241 if (map
->l_info
[DT_PPC(GOT
)] == 0)
242 /* Handle old style PLT. */
243 return __elf_machine_fixup_plt (map
, reloc_addr
, finaladdr
);
245 *reloc_addr
= finaladdr
;
249 /* Return the final value of a plt relocation. */
250 static inline Elf32_Addr
251 elf_machine_plt_value (struct link_map
*map
, const Elf32_Rela
*reloc
,
254 return value
+ reloc
->r_addend
;
258 /* Names of the architecture-specific auditing callback functions. */
259 #define ARCH_LA_PLTENTER ppc32_gnu_pltenter
260 #define ARCH_LA_PLTEXIT ppc32_gnu_pltexit
262 #endif /* dl_machine_h */
266 /* Do the actual processing of a reloc, once its target address
267 has been determined. */
268 extern void __process_machine_rela (struct link_map
*map
,
269 const Elf32_Rela
*reloc
,
270 struct link_map
*sym_map
,
271 const Elf32_Sym
*sym
,
272 const Elf32_Sym
*refsym
,
273 Elf32_Addr
*const reloc_addr
,
274 Elf32_Addr finaladdr
,
275 int rinfo
) attribute_hidden
;
277 /* Call _dl_signal_error when a resolved value overflows a relocated area. */
278 extern void _dl_reloc_overflow (struct link_map
*map
,
280 Elf32_Addr
*const reloc_addr
,
281 const Elf32_Sym
*refsym
) attribute_hidden
;
283 /* Perform the relocation specified by RELOC and SYM (which is fully resolved).
284 LOADADDR is the load address of the object; INFO is an array indexed
285 by DT_* of the .dynamic section info. */
287 auto inline void __attribute__ ((always_inline
))
288 elf_machine_rela (struct link_map
*map
, const Elf32_Rela
*reloc
,
289 const Elf32_Sym
*sym
, const struct r_found_version
*version
,
290 void *const reloc_addr_arg
, int skip_ifunc
)
292 Elf32_Addr
*const reloc_addr
= reloc_addr_arg
;
293 const Elf32_Sym
*const refsym
= sym
;
295 const int r_type
= ELF32_R_TYPE (reloc
->r_info
);
296 struct link_map
*sym_map
= NULL
;
298 #ifndef RESOLVE_CONFLICT_FIND_MAP
299 if (r_type
== R_PPC_RELATIVE
)
301 *reloc_addr
= map
->l_addr
+ reloc
->r_addend
;
305 if (__glibc_unlikely (r_type
== R_PPC_NONE
))
308 /* binutils on ppc32 includes st_value in r_addend for relocations
309 against local symbols. */
310 if (__builtin_expect (ELF32_ST_BIND (sym
->st_info
) == STB_LOCAL
, 0)
311 && sym
->st_shndx
!= SHN_UNDEF
)
315 sym_map
= RESOLVE_MAP (&sym
, version
, r_type
);
316 value
= sym_map
== NULL
? 0 : sym_map
->l_addr
+ sym
->st_value
;
318 value
+= reloc
->r_addend
;
320 value
= reloc
->r_addend
;
324 && __builtin_expect (ELFW(ST_TYPE
) (sym
->st_info
) == STT_GNU_IFUNC
, 0)
325 && __builtin_expect (sym
->st_shndx
!= SHN_UNDEF
, 1)
326 && __builtin_expect (!skip_ifunc
, 1))
327 value
= elf_ifunc_invoke (value
);
329 /* A small amount of code is duplicated here for speed. In libc,
330 more than 90% of the relocs are R_PPC_RELATIVE; in the X11 shared
331 libraries, 60% are R_PPC_RELATIVE, 24% are R_PPC_GLOB_DAT or
332 R_PPC_ADDR32, and 16% are R_PPC_JMP_SLOT (which this routine
333 wouldn't usually handle). As an bonus, doing this here allows
334 the switch statement in __process_machine_rela to work. */
342 #ifndef RESOLVE_CONFLICT_FIND_MAP
343 # ifdef RTLD_BOOTSTRAP
344 # define NOT_BOOTSTRAP 0
346 # define NOT_BOOTSTRAP 1
350 if (map
->l_info
[DT_PPC(OPT
)]
351 && (map
->l_info
[DT_PPC(OPT
)]->d_un
.d_val
& PPC_OPT_TLS
))
356 reloc_addr
[1] = (sym_map
->l_tls_offset
- TLS_TP_OFFSET
360 else if (sym_map
!= NULL
)
363 CHECK_STATIC_TLS (map
, sym_map
);
365 if (TRY_STATIC_TLS (map
, sym_map
))
369 /* Set up for local dynamic. */
370 reloc_addr
[1] = (sym_map
->l_tls_offset
- TLS_TP_OFFSET
377 /* During startup the dynamic linker is always index 1. */
379 else if (sym_map
!= NULL
)
380 /* Get the information from the link map returned by the
381 RESOLVE_MAP function. */
382 *reloc_addr
= sym_map
->l_tls_modid
;
385 if (map
->l_info
[DT_PPC(OPT
)]
386 && (map
->l_info
[DT_PPC(OPT
)]->d_un
.d_val
& PPC_OPT_TLS
))
390 *reloc_addr
= TLS_TPREL_VALUE (sym_map
, sym
, reloc
);
393 else if (sym_map
!= NULL
)
395 /* This reloc is always preceded by R_PPC_DTPMOD32. */
397 assert (HAVE_STATIC_TLS (map
, sym_map
));
399 if (HAVE_STATIC_TLS (map
, sym_map
))
402 *reloc_addr
= TLS_TPREL_VALUE (sym_map
, sym
, reloc
);
407 /* During relocation all TLS symbols are defined and used.
408 Therefore the offset is already correct. */
409 if (NOT_BOOTSTRAP
&& sym_map
!= NULL
)
410 *reloc_addr
= TLS_DTPREL_VALUE (sym
, reloc
);
413 if (!NOT_BOOTSTRAP
|| sym_map
!= NULL
)
416 CHECK_STATIC_TLS (map
, sym_map
);
417 *reloc_addr
= TLS_TPREL_VALUE (sym_map
, sym
, reloc
);
423 #ifdef RESOLVE_CONFLICT_FIND_MAP
424 RESOLVE_CONFLICT_FIND_MAP (map
, reloc_addr
);
426 if (map
->l_info
[DT_PPC(GOT
)] != 0)
434 __process_machine_rela (map
, reloc
, sym_map
, sym
, refsym
,
435 reloc_addr
, value
, r_type
);
439 auto inline void __attribute__ ((always_inline
))
440 elf_machine_rela_relative (Elf32_Addr l_addr
, const Elf32_Rela
*reloc
,
441 void *const reloc_addr_arg
)
443 Elf32_Addr
*const reloc_addr
= reloc_addr_arg
;
444 *reloc_addr
= l_addr
+ reloc
->r_addend
;
447 auto inline void __attribute__ ((always_inline
))
448 elf_machine_lazy_rel (struct link_map
*map
,
449 Elf32_Addr l_addr
, const Elf32_Rela
*reloc
,
452 /* elf_machine_runtime_setup handles this. */
455 #endif /* RESOLVE_MAP */