]>
Commit | Line | Data |
---|---|---|
4cca6b86 | 1 | /* Machine-dependent ELF dynamic relocation inline functions. PowerPC version. |
d4697bc9 | 2 | Copyright (C) 1995-2014 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 PE |
16 | License along with the GNU C Library; if not, see |
17 | <http://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> |
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 | 33 | static inline int |
ceb579a3 | 34 | elf_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. */ |
40 | static inline Elf32_Addr * __attribute__ ((const)) | |
41 | ppc_got (void) | |
42 | { | |
43 | Elf32_Addr *got; | |
41288fbb | 44 | |
99c7f870 UD |
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"); | |
41288fbb | 50 | |
99c7f870 UD |
51 | return got; |
52 | } | |
4cca6b86 | 53 | |
e61abf83 UD |
54 | /* Return the link-time address of _DYNAMIC, stored as |
55 | the first value in the GOT. */ | |
99c7f870 | 56 | static inline Elf32_Addr __attribute__ ((const)) |
4cca6b86 UD |
57 | elf_machine_dynamic (void) |
58 | { | |
99c7f870 | 59 | return *ppc_got (); |
4cca6b86 UD |
60 | } |
61 | ||
62 | /* Return the run-time load address of the shared object. */ | |
99c7f870 | 63 | static inline Elf32_Addr __attribute__ ((const)) |
4cca6b86 UD |
64 | elf_machine_load_address (void) |
65 | { | |
99c7f870 UD |
66 | Elf32_Addr *branchaddr; |
67 | Elf32_Addr runtime_dynamic; | |
4cca6b86 UD |
68 | |
69 | /* This is much harder than you'd expect. Possibly I'm missing something. | |
70 | The 'obvious' way: | |
71 | ||
72 | Apparently, "bcl 20,31,$+4" is what should be used to load LR | |
73 | with the address of the next instruction. | |
74 | I think this is so that machines that do bl/blr pairing don't | |
75 | get confused. | |
76 | ||
77 | asm ("bcl 20,31,0f ;" | |
e61abf83 UD |
78 | "0: mflr 0 ;" |
79 | "lis %0,0b@ha;" | |
80 | "addi %0,%0,0b@l;" | |
81 | "subf %0,%0,0" | |
82 | : "=b" (addr) : : "r0", "lr"); | |
4cca6b86 UD |
83 | |
84 | doesn't work, because the linker doesn't have to (and in fact doesn't) | |
85 | update the @ha and @l references; the loader (which runs after this | |
86 | code) will do that. | |
87 | ||
88 | Instead, we use the following trick: | |
89 | ||
90 | The linker puts the _link-time_ address of _DYNAMIC at the first | |
91 | word in the GOT. We could branch to that address, if we wanted, | |
92 | by using an @local reloc; the linker works this out, so it's safe | |
93 | to use now. We can't, of course, actually branch there, because | |
94 | we'd cause an illegal instruction exception; so we need to compute | |
95 | the address ourselves. That gives us the following code: */ | |
96 | ||
97 | /* Get address of the 'b _DYNAMIC@local'... */ | |
99c7f870 | 98 | asm ("bcl 20,31,0f;" |
4cca6b86 UD |
99 | "b _DYNAMIC@local;" |
100 | "0:" | |
99c7f870 | 101 | : "=l" (branchaddr)); |
4cca6b86 UD |
102 | |
103 | /* So now work out the difference between where the branch actually points, | |
104 | and the offset of that location in memory from the start of the file. */ | |
99c7f870 UD |
105 | runtime_dynamic = ((Elf32_Addr) branchaddr |
106 | + ((Elf32_Sword) (*branchaddr << 6 & 0xffffff00) >> 6)); | |
107 | ||
108 | return runtime_dynamic - elf_machine_dynamic (); | |
4cca6b86 UD |
109 | } |
110 | ||
111 | #define ELF_MACHINE_BEFORE_RTLD_RELOC(dynamic_info) /* nothing */ | |
112 | ||
1f205a47 UD |
113 | /* The PLT uses Elf32_Rela relocs. */ |
114 | #define elf_machine_relplt elf_machine_rela | |
4cca6b86 | 115 | |
9ce8b3c8 RM |
116 | /* Mask identifying addresses reserved for the user program, |
117 | where the dynamic linker should not map anything. */ | |
118 | #define ELF_MACHINE_USER_ADDRESS_MASK 0xf0000000UL | |
119 | ||
052b6a6c UD |
120 | /* The actual _start code is in dl-start.S. Use a really |
121 | ugly bit of assembler to let dl-start.o see _dl_start. */ | |
122 | #define RTLD_START asm (".globl _dl_start"); | |
4cca6b86 | 123 | |
052b6a6c UD |
124 | /* Decide where a relocatable object should be loaded. */ |
125 | extern ElfW(Addr) | |
126 | __elf_preferred_address(struct link_map *loader, size_t maplength, | |
127 | ElfW(Addr) mapstartpref); | |
128 | #define ELF_PREFERRED_ADDRESS(loader, maplength, mapstartpref) \ | |
129 | __elf_preferred_address (loader, maplength, mapstartpref) | |
e61abf83 | 130 | |
cf5a372e UD |
131 | /* ELF_RTYPE_CLASS_PLT iff TYPE describes relocation of a PLT entry, so |
132 | PLT entries should not be allowed to define the value. | |
133 | ELF_RTYPE_CLASS_NOCOPY iff TYPE should not be allowed to resolve to one | |
134 | of the main executable's symbols, as for a COPY reloc. */ | |
1f205a47 UD |
135 | /* We never want to use a PLT entry as the destination of a |
136 | reloc, when what is being relocated is a branch. This is | |
137 | partly for efficiency, but mostly so we avoid loops. */ | |
bb0ddc2f RM |
138 | #define elf_machine_type_class(type) \ |
139 | ((((type) == R_PPC_JMP_SLOT \ | |
140 | || (type) == R_PPC_REL24 \ | |
7551556f RM |
141 | || ((type) >= R_PPC_DTPMOD32 /* contiguous TLS */ \ |
142 | && (type) <= R_PPC_DTPREL32) \ | |
bb0ddc2f RM |
143 | || (type) == R_PPC_ADDR24) * ELF_RTYPE_CLASS_PLT) \ |
144 | | (((type) == R_PPC_COPY) * ELF_RTYPE_CLASS_COPY)) | |
4cca6b86 | 145 | |
1f205a47 | 146 | /* A reloc type used for ld.so cmdline arg lookups to reject PLT entries. */ |
a2b08ee5 | 147 | #define ELF_MACHINE_JMP_SLOT R_PPC_JMP_SLOT |
4cca6b86 | 148 | |
4839f592 AJ |
149 | /* The PowerPC never uses REL relocations. */ |
150 | #define ELF_MACHINE_NO_REL 1 | |
151 | ||
99c7f870 | 152 | /* Set up the loaded object described by MAP so its unrelocated PLT |
5929563f UD |
153 | entries will jump to the on-demand fixup code in dl-runtime.c. |
154 | Also install a small trampoline to be used by entries that have | |
155 | been relocated to an address too far away for a single branch. */ | |
052b6a6c UD |
156 | extern int __elf_machine_runtime_setup (struct link_map *map, |
157 | int lazy, int profile); | |
99c7f870 UD |
158 | |
159 | static inline int | |
160 | elf_machine_runtime_setup (struct link_map *map, | |
161 | int lazy, int profile) | |
162 | { | |
163 | if (map->l_info[DT_JMPREL] == 0) | |
164 | return lazy; | |
165 | ||
166 | if (map->l_info[DT_PPC(GOT)] == 0) | |
167 | /* Handle old style PLT. */ | |
168 | return __elf_machine_runtime_setup (map, lazy, profile); | |
169 | ||
170 | /* New style non-exec PLT consisting of an array of addresses. */ | |
171 | map->l_info[DT_PPC(GOT)]->d_un.d_ptr += map->l_addr; | |
172 | if (lazy) | |
173 | { | |
174 | Elf32_Addr *plt, *got, glink; | |
175 | Elf32_Word num_plt_entries; | |
176 | void (*dlrr) (void); | |
177 | extern void _dl_runtime_resolve (void); | |
178 | extern void _dl_prof_resolve (void); | |
179 | ||
180 | if (__builtin_expect (!profile, 1)) | |
181 | dlrr = _dl_runtime_resolve; | |
182 | else | |
183 | { | |
184 | if (GLRO(dl_profile) != NULL | |
185 | &&_dl_name_match_p (GLRO(dl_profile), map)) | |
186 | GL(dl_profile_map) = map; | |
187 | dlrr = _dl_prof_resolve; | |
188 | } | |
189 | got = (Elf32_Addr *) map->l_info[DT_PPC(GOT)]->d_un.d_ptr; | |
190 | glink = got[1]; | |
191 | got[1] = (Elf32_Addr) dlrr; | |
192 | got[2] = (Elf32_Addr) map; | |
193 | ||
194 | /* Relocate everything in .plt by the load address offset. */ | |
195 | plt = (Elf32_Addr *) D_PTR (map, l_info[DT_PLTGOT]); | |
196 | num_plt_entries = (map->l_info[DT_PLTRELSZ]->d_un.d_val | |
197 | / sizeof (Elf32_Rela)); | |
198 | ||
199 | /* If a library is prelinked but we have to relocate anyway, | |
200 | we have to be able to undo the prelinking of .plt section. | |
201 | The prelinker saved us at got[1] address of .glink | |
202 | section's start. */ | |
203 | if (glink) | |
204 | { | |
205 | glink += map->l_addr; | |
206 | while (num_plt_entries-- != 0) | |
207 | *plt++ = glink, glink += 4; | |
208 | } | |
209 | else | |
210 | while (num_plt_entries-- != 0) | |
211 | *plt++ += map->l_addr; | |
212 | } | |
213 | return lazy; | |
214 | } | |
5929563f | 215 | |
052b6a6c | 216 | /* Change the PLT entry whose reloc is 'reloc' to call the actual routine. */ |
c0282c06 | 217 | extern Elf32_Addr __elf_machine_fixup_plt (struct link_map *map, |
c0282c06 UD |
218 | Elf32_Addr *reloc_addr, |
219 | Elf32_Addr finaladdr); | |
220 | ||
221 | static inline Elf32_Addr | |
b6299091 | 222 | elf_machine_fixup_plt (struct link_map *map, lookup_t t, |
c0282c06 | 223 | const Elf32_Rela *reloc, |
b6299091 | 224 | Elf32_Addr *reloc_addr, Elf64_Addr finaladdr) |
c0282c06 | 225 | { |
99c7f870 UD |
226 | if (map->l_info[DT_PPC(GOT)] == 0) |
227 | /* Handle old style PLT. */ | |
77799d9d | 228 | return __elf_machine_fixup_plt (map, reloc_addr, finaladdr); |
99c7f870 UD |
229 | |
230 | *reloc_addr = finaladdr; | |
231 | return finaladdr; | |
c0282c06 | 232 | } |
a2b08ee5 | 233 | |
dfd2257a | 234 | /* Return the final value of a plt relocation. */ |
c2a32973 | 235 | static inline Elf32_Addr |
dfd2257a UD |
236 | elf_machine_plt_value (struct link_map *map, const Elf32_Rela *reloc, |
237 | Elf32_Addr value) | |
238 | { | |
239 | return value + reloc->r_addend; | |
240 | } | |
241 | ||
2413fdba UD |
242 | |
243 | /* Names of the architecture-specific auditing callback functions. */ | |
244 | #define ARCH_LA_PLTENTER ppc32_gnu_pltenter | |
245 | #define ARCH_LA_PLTEXIT ppc32_gnu_pltexit | |
246 | ||
1f205a47 | 247 | #endif /* dl_machine_h */ |
4cca6b86 | 248 | |
2413fdba | 249 | #ifdef RESOLVE_MAP |
4cca6b86 | 250 | |
052b6a6c UD |
251 | /* Do the actual processing of a reloc, once its target address |
252 | has been determined. */ | |
253 | extern void __process_machine_rela (struct link_map *map, | |
254 | const Elf32_Rela *reloc, | |
545dbc93 | 255 | struct link_map *sym_map, |
052b6a6c UD |
256 | const Elf32_Sym *sym, |
257 | const Elf32_Sym *refsym, | |
258 | Elf32_Addr *const reloc_addr, | |
259 | Elf32_Addr finaladdr, | |
7551556f RM |
260 | int rinfo) attribute_hidden; |
261 | ||
262 | /* Call _dl_signal_error when a resolved value overflows a relocated area. */ | |
263 | extern void _dl_reloc_overflow (struct link_map *map, | |
264 | const char *name, | |
265 | Elf32_Addr *const reloc_addr, | |
7551556f | 266 | const Elf32_Sym *refsym) attribute_hidden; |
052b6a6c | 267 | |
1f205a47 UD |
268 | /* Perform the relocation specified by RELOC and SYM (which is fully resolved). |
269 | LOADADDR is the load address of the object; INFO is an array indexed | |
270 | by DT_* of the .dynamic section info. */ | |
4cca6b86 | 271 | |
7de00121 | 272 | auto inline void __attribute__ ((always_inline)) |
1f205a47 | 273 | elf_machine_rela (struct link_map *map, const Elf32_Rela *reloc, |
3996f34b | 274 | const Elf32_Sym *sym, const struct r_found_version *version, |
3a62d00d | 275 | void *const reloc_addr_arg, int skip_ifunc) |
1f205a47 | 276 | { |
87d254a7 | 277 | Elf32_Addr *const reloc_addr = reloc_addr_arg; |
1f205a47 | 278 | const Elf32_Sym *const refsym = sym; |
bb0ddc2f RM |
279 | Elf32_Addr value; |
280 | const int r_type = ELF32_R_TYPE (reloc->r_info); | |
545dbc93 | 281 | struct link_map *sym_map = NULL; |
4cca6b86 | 282 | |
aa3f2410 | 283 | #ifndef RESOLVE_CONFLICT_FIND_MAP |
bb0ddc2f | 284 | if (r_type == R_PPC_RELATIVE) |
1f205a47 | 285 | { |
bb0ddc2f RM |
286 | *reloc_addr = map->l_addr + reloc->r_addend; |
287 | return; | |
1f205a47 | 288 | } |
bb0ddc2f RM |
289 | |
290 | if (__builtin_expect (r_type == R_PPC_NONE, 0)) | |
291 | return; | |
292 | ||
213cdddb RM |
293 | /* binutils on ppc32 includes st_value in r_addend for relocations |
294 | against local symbols. */ | |
295 | if (__builtin_expect (ELF32_ST_BIND (sym->st_info) == STB_LOCAL, 0) | |
296 | && sym->st_shndx != SHN_UNDEF) | |
297 | value = map->l_addr; | |
298 | else | |
299 | { | |
213cdddb | 300 | sym_map = RESOLVE_MAP (&sym, version, r_type); |
d026cf5f | 301 | value = sym_map == NULL ? 0 : sym_map->l_addr + sym->st_value; |
213cdddb | 302 | } |
bb0ddc2f | 303 | value += reloc->r_addend; |
aa3f2410 UD |
304 | #else |
305 | value = reloc->r_addend; | |
306 | #endif | |
4cca6b86 | 307 | |
77799d9d AM |
308 | if (sym != NULL |
309 | && __builtin_expect (ELFW(ST_TYPE) (sym->st_info) == STT_GNU_IFUNC, 0) | |
3a62d00d AS |
310 | && __builtin_expect (sym->st_shndx != SHN_UNDEF, 1) |
311 | && __builtin_expect (!skip_ifunc, 1)) | |
a386f1cc | 312 | value = elf_ifunc_invoke (value); |
77799d9d | 313 | |
052b6a6c UD |
314 | /* A small amount of code is duplicated here for speed. In libc, |
315 | more than 90% of the relocs are R_PPC_RELATIVE; in the X11 shared | |
316 | libraries, 60% are R_PPC_RELATIVE, 24% are R_PPC_GLOB_DAT or | |
317 | R_PPC_ADDR32, and 16% are R_PPC_JMP_SLOT (which this routine | |
318 | wouldn't usually handle). As an bonus, doing this here allows | |
319 | the switch statement in __process_machine_rela to work. */ | |
bb0ddc2f | 320 | switch (r_type) |
1f205a47 | 321 | { |
bb0ddc2f RM |
322 | case R_PPC_GLOB_DAT: |
323 | case R_PPC_ADDR32: | |
324 | *reloc_addr = value; | |
325 | break; | |
326 | ||
d063d164 | 327 | #ifndef RESOLVE_CONFLICT_FIND_MAP |
7551556f RM |
328 | # ifdef RTLD_BOOTSTRAP |
329 | # define NOT_BOOTSTRAP 0 | |
330 | # else | |
331 | # define NOT_BOOTSTRAP 1 | |
bb0ddc2f | 332 | # endif |
7551556f | 333 | |
545dbc93 RM |
334 | case R_PPC_DTPMOD32: |
335 | if (!NOT_BOOTSTRAP) | |
336 | /* During startup the dynamic linker is always index 1. */ | |
337 | *reloc_addr = 1; | |
338 | else if (sym_map != NULL) | |
339 | /* Get the information from the link map returned by the | |
340 | RESOLVE_MAP function. */ | |
341 | *reloc_addr = sym_map->l_tls_modid; | |
342 | break; | |
343 | case R_PPC_DTPREL32: | |
344 | /* During relocation all TLS symbols are defined and used. | |
345 | Therefore the offset is already correct. */ | |
346 | if (NOT_BOOTSTRAP && sym_map != NULL) | |
347 | *reloc_addr = TLS_DTPREL_VALUE (sym, reloc); | |
348 | break; | |
349 | case R_PPC_TPREL32: | |
350 | if (!NOT_BOOTSTRAP || sym_map != NULL) | |
bb0ddc2f | 351 | { |
545dbc93 RM |
352 | if (NOT_BOOTSTRAP) |
353 | CHECK_STATIC_TLS (map, sym_map); | |
354 | *reloc_addr = TLS_TPREL_VALUE (sym_map, sym, reloc); | |
bb0ddc2f | 355 | } |
545dbc93 | 356 | break; |
11bf311e | 357 | #endif |
bb0ddc2f | 358 | |
a334319f | 359 | case R_PPC_JMP_SLOT: |
99c7f870 | 360 | #ifdef RESOLVE_CONFLICT_FIND_MAP |
bb0ddc2f | 361 | RESOLVE_CONFLICT_FIND_MAP (map, reloc_addr); |
a334319f | 362 | #endif |
99c7f870 UD |
363 | if (map->l_info[DT_PPC(GOT)] != 0) |
364 | { | |
365 | *reloc_addr = value; | |
366 | break; | |
367 | } | |
368 | /* FALLTHROUGH */ | |
bb0ddc2f RM |
369 | |
370 | default: | |
545dbc93 | 371 | __process_machine_rela (map, reloc, sym_map, sym, refsym, |
bb0ddc2f | 372 | reloc_addr, value, r_type); |
1f205a47 | 373 | } |
1f205a47 UD |
374 | } |
375 | ||
7de00121 | 376 | auto inline void __attribute__ ((always_inline)) |
1f2a1df3 | 377 | elf_machine_rela_relative (Elf32_Addr l_addr, const Elf32_Rela *reloc, |
87d254a7 | 378 | void *const reloc_addr_arg) |
1721af3f | 379 | { |
87d254a7 | 380 | Elf32_Addr *const reloc_addr = reloc_addr_arg; |
1721af3f UD |
381 | *reloc_addr = l_addr + reloc->r_addend; |
382 | } | |
1f205a47 | 383 | |
7de00121 | 384 | auto inline void __attribute__ ((always_inline)) |
7ba7c829 | 385 | elf_machine_lazy_rel (struct link_map *map, |
3a62d00d AS |
386 | Elf32_Addr l_addr, const Elf32_Rela *reloc, |
387 | int skip_ifunc) | |
7ba7c829 UD |
388 | { |
389 | /* elf_machine_runtime_setup handles this. */ | |
390 | } | |
391 | ||
2413fdba | 392 | #endif /* RESOLVE_MAP */ |