]>
Commit | Line | Data |
---|---|---|
4cca6b86 | 1 | /* Machine-dependent ELF dynamic relocation inline functions. PowerPC version. |
2413fdba | 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 | |
ceb579a3 | 28 | /* Return nonzero iff ELF header is compatible with the running host. */ |
c2a32973 | 29 | static inline int |
ceb579a3 | 30 | elf_machine_matches_host (const Elf32_Ehdr *ehdr) |
4cca6b86 | 31 | { |
ceb579a3 | 32 | return ehdr->e_machine == EM_PPC; |
4cca6b86 UD |
33 | } |
34 | ||
35 | ||
e61abf83 UD |
36 | /* Return the link-time address of _DYNAMIC, stored as |
37 | the first value in the GOT. */ | |
a334319f | 38 | static inline Elf32_Addr |
4cca6b86 UD |
39 | elf_machine_dynamic (void) |
40 | { | |
a334319f UD |
41 | Elf32_Addr *got; |
42 | asm (" bl _GLOBAL_OFFSET_TABLE_-4@local" | |
43 | : "=l"(got)); | |
44 | return *got; | |
4cca6b86 UD |
45 | } |
46 | ||
47 | /* Return the run-time load address of the shared object. */ | |
a334319f | 48 | static inline Elf32_Addr |
4cca6b86 UD |
49 | elf_machine_load_address (void) |
50 | { | |
a334319f UD |
51 | unsigned int *got; |
52 | unsigned int *branchaddr; | |
4cca6b86 UD |
53 | |
54 | /* This is much harder than you'd expect. Possibly I'm missing something. | |
55 | The 'obvious' way: | |
56 | ||
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 | |
60 | get confused. | |
61 | ||
62 | asm ("bcl 20,31,0f ;" | |
e61abf83 UD |
63 | "0: mflr 0 ;" |
64 | "lis %0,0b@ha;" | |
65 | "addi %0,%0,0b@l;" | |
66 | "subf %0,%0,0" | |
67 | : "=b" (addr) : : "r0", "lr"); | |
4cca6b86 UD |
68 | |
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 | |
71 | code) will do that. | |
72 | ||
73 | Instead, we use the following trick: | |
74 | ||
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: */ | |
81 | ||
82 | /* Get address of the 'b _DYNAMIC@local'... */ | |
a334319f | 83 | asm ("bl 0f ;" |
4cca6b86 UD |
84 | "b _DYNAMIC@local;" |
85 | "0:" | |
a334319f UD |
86 | : "=l"(branchaddr)); |
87 | ||
88 | /* ... and the address of the GOT. */ | |
89 | asm (" bl _GLOBAL_OFFSET_TABLE_-4@local" | |
90 | : "=l"(got)); | |
4cca6b86 UD |
91 | |
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. */ | |
a334319f UD |
94 | return ((Elf32_Addr)branchaddr - *got |
95 | + ((int)(*branchaddr << 6 & 0xffffff00) >> 6)); | |
4cca6b86 UD |
96 | } |
97 | ||
98 | #define ELF_MACHINE_BEFORE_RTLD_RELOC(dynamic_info) /* nothing */ | |
99 | ||
1f205a47 UD |
100 | /* The PLT uses Elf32_Rela relocs. */ |
101 | #define elf_machine_relplt elf_machine_rela | |
4cca6b86 | 102 | |
9ce8b3c8 RM |
103 | /* Mask identifying addresses reserved for the user program, |
104 | where the dynamic linker should not map anything. */ | |
105 | #define ELF_MACHINE_USER_ADDRESS_MASK 0xf0000000UL | |
106 | ||
052b6a6c UD |
107 | /* The actual _start code is in dl-start.S. Use a really |
108 | ugly bit of assembler to let dl-start.o see _dl_start. */ | |
109 | #define RTLD_START asm (".globl _dl_start"); | |
4cca6b86 | 110 | |
052b6a6c UD |
111 | /* Decide where a relocatable object should be loaded. */ |
112 | extern ElfW(Addr) | |
113 | __elf_preferred_address(struct link_map *loader, size_t maplength, | |
114 | ElfW(Addr) mapstartpref); | |
115 | #define ELF_PREFERRED_ADDRESS(loader, maplength, mapstartpref) \ | |
116 | __elf_preferred_address (loader, maplength, mapstartpref) | |
e61abf83 | 117 | |
cf5a372e UD |
118 | /* ELF_RTYPE_CLASS_PLT iff TYPE describes relocation of a PLT entry, so |
119 | PLT entries should not be allowed to define the value. | |
120 | ELF_RTYPE_CLASS_NOCOPY iff TYPE should not be allowed to resolve to one | |
121 | of the main executable's symbols, as for a COPY reloc. */ | |
1f205a47 UD |
122 | /* We never want to use a PLT entry as the destination of a |
123 | reloc, when what is being relocated is a branch. This is | |
124 | partly for efficiency, but mostly so we avoid loops. */ | |
bb0ddc2f RM |
125 | #if defined USE_TLS && (!defined RTLD_BOOTSTRAP || USE___THREAD) |
126 | #define elf_machine_type_class(type) \ | |
127 | ((((type) == R_PPC_JMP_SLOT \ | |
128 | || (type) == R_PPC_REL24 \ | |
7551556f RM |
129 | || ((type) >= R_PPC_DTPMOD32 /* contiguous TLS */ \ |
130 | && (type) <= R_PPC_DTPREL32) \ | |
bb0ddc2f RM |
131 | || (type) == R_PPC_ADDR24) * ELF_RTYPE_CLASS_PLT) \ |
132 | | (((type) == R_PPC_COPY) * ELF_RTYPE_CLASS_COPY)) | |
133 | #else | |
cf5a372e UD |
134 | #define elf_machine_type_class(type) \ |
135 | ((((type) == R_PPC_JMP_SLOT \ | |
136 | || (type) == R_PPC_REL24 \ | |
137 | || (type) == R_PPC_ADDR24) * ELF_RTYPE_CLASS_PLT) \ | |
138 | | (((type) == R_PPC_COPY) * ELF_RTYPE_CLASS_COPY)) | |
bb0ddc2f | 139 | #endif |
4cca6b86 | 140 | |
1f205a47 | 141 | /* A reloc type used for ld.so cmdline arg lookups to reject PLT entries. */ |
a2b08ee5 | 142 | #define ELF_MACHINE_JMP_SLOT R_PPC_JMP_SLOT |
4cca6b86 | 143 | |
4839f592 AJ |
144 | /* The PowerPC never uses REL relocations. */ |
145 | #define ELF_MACHINE_NO_REL 1 | |
146 | ||
a334319f | 147 | /* Set up the loaded object described by L so its unrelocated PLT |
5929563f UD |
148 | entries will jump to the on-demand fixup code in dl-runtime.c. |
149 | Also install a small trampoline to be used by entries that have | |
150 | been relocated to an address too far away for a single branch. */ | |
052b6a6c UD |
151 | extern int __elf_machine_runtime_setup (struct link_map *map, |
152 | int lazy, int profile); | |
a334319f | 153 | #define elf_machine_runtime_setup __elf_machine_runtime_setup |
5929563f | 154 | |
052b6a6c | 155 | /* Change the PLT entry whose reloc is 'reloc' to call the actual routine. */ |
c0282c06 UD |
156 | extern Elf32_Addr __elf_machine_fixup_plt (struct link_map *map, |
157 | const Elf32_Rela *reloc, | |
158 | Elf32_Addr *reloc_addr, | |
159 | Elf32_Addr finaladdr); | |
160 | ||
161 | static inline Elf32_Addr | |
b6299091 | 162 | elf_machine_fixup_plt (struct link_map *map, lookup_t t, |
c0282c06 | 163 | const Elf32_Rela *reloc, |
b6299091 | 164 | Elf32_Addr *reloc_addr, Elf64_Addr finaladdr) |
c0282c06 | 165 | { |
a334319f | 166 | return __elf_machine_fixup_plt (map, reloc, reloc_addr, finaladdr); |
c0282c06 | 167 | } |
a2b08ee5 | 168 | |
dfd2257a | 169 | /* Return the final value of a plt relocation. */ |
c2a32973 | 170 | static inline Elf32_Addr |
dfd2257a UD |
171 | elf_machine_plt_value (struct link_map *map, const Elf32_Rela *reloc, |
172 | Elf32_Addr value) | |
173 | { | |
174 | return value + reloc->r_addend; | |
175 | } | |
176 | ||
2413fdba UD |
177 | |
178 | /* Names of the architecture-specific auditing callback functions. */ | |
179 | #define ARCH_LA_PLTENTER ppc32_gnu_pltenter | |
180 | #define ARCH_LA_PLTEXIT ppc32_gnu_pltexit | |
181 | ||
1f205a47 | 182 | #endif /* dl_machine_h */ |
4cca6b86 | 183 | |
2413fdba | 184 | #ifdef RESOLVE_MAP |
4cca6b86 | 185 | |
052b6a6c UD |
186 | /* Do the actual processing of a reloc, once its target address |
187 | has been determined. */ | |
188 | extern void __process_machine_rela (struct link_map *map, | |
189 | const Elf32_Rela *reloc, | |
545dbc93 | 190 | struct link_map *sym_map, |
052b6a6c UD |
191 | const Elf32_Sym *sym, |
192 | const Elf32_Sym *refsym, | |
193 | Elf32_Addr *const reloc_addr, | |
194 | Elf32_Addr finaladdr, | |
7551556f RM |
195 | int rinfo) attribute_hidden; |
196 | ||
197 | /* Call _dl_signal_error when a resolved value overflows a relocated area. */ | |
198 | extern void _dl_reloc_overflow (struct link_map *map, | |
199 | const char *name, | |
200 | Elf32_Addr *const reloc_addr, | |
7551556f | 201 | const Elf32_Sym *refsym) attribute_hidden; |
052b6a6c | 202 | |
1f205a47 UD |
203 | /* Perform the relocation specified by RELOC and SYM (which is fully resolved). |
204 | LOADADDR is the load address of the object; INFO is an array indexed | |
205 | by DT_* of the .dynamic section info. */ | |
4cca6b86 | 206 | |
a334319f | 207 | inline void |
1f205a47 | 208 | elf_machine_rela (struct link_map *map, const Elf32_Rela *reloc, |
3996f34b | 209 | const Elf32_Sym *sym, const struct r_found_version *version, |
87d254a7 | 210 | void *const reloc_addr_arg) |
1f205a47 | 211 | { |
87d254a7 | 212 | Elf32_Addr *const reloc_addr = reloc_addr_arg; |
1f205a47 | 213 | const Elf32_Sym *const refsym = sym; |
bb0ddc2f RM |
214 | Elf32_Addr value; |
215 | const int r_type = ELF32_R_TYPE (reloc->r_info); | |
545dbc93 | 216 | struct link_map *sym_map = NULL; |
4cca6b86 | 217 | |
aa3f2410 | 218 | #ifndef RESOLVE_CONFLICT_FIND_MAP |
bb0ddc2f | 219 | if (r_type == R_PPC_RELATIVE) |
1f205a47 | 220 | { |
bb0ddc2f RM |
221 | *reloc_addr = map->l_addr + reloc->r_addend; |
222 | return; | |
1f205a47 | 223 | } |
bb0ddc2f RM |
224 | |
225 | if (__builtin_expect (r_type == R_PPC_NONE, 0)) | |
226 | return; | |
227 | ||
213cdddb RM |
228 | /* binutils on ppc32 includes st_value in r_addend for relocations |
229 | against local symbols. */ | |
230 | if (__builtin_expect (ELF32_ST_BIND (sym->st_info) == STB_LOCAL, 0) | |
231 | && sym->st_shndx != SHN_UNDEF) | |
232 | value = map->l_addr; | |
233 | else | |
234 | { | |
213cdddb | 235 | sym_map = RESOLVE_MAP (&sym, version, r_type); |
a334319f | 236 | value = sym == NULL ? 0 : sym_map->l_addr + sym->st_value; |
213cdddb | 237 | } |
bb0ddc2f | 238 | value += reloc->r_addend; |
aa3f2410 UD |
239 | #else |
240 | value = reloc->r_addend; | |
241 | #endif | |
4cca6b86 | 242 | |
052b6a6c UD |
243 | /* A small amount of code is duplicated here for speed. In libc, |
244 | more than 90% of the relocs are R_PPC_RELATIVE; in the X11 shared | |
245 | libraries, 60% are R_PPC_RELATIVE, 24% are R_PPC_GLOB_DAT or | |
246 | R_PPC_ADDR32, and 16% are R_PPC_JMP_SLOT (which this routine | |
247 | wouldn't usually handle). As an bonus, doing this here allows | |
248 | the switch statement in __process_machine_rela to work. */ | |
bb0ddc2f | 249 | switch (r_type) |
1f205a47 | 250 | { |
bb0ddc2f RM |
251 | case R_PPC_GLOB_DAT: |
252 | case R_PPC_ADDR32: | |
253 | *reloc_addr = value; | |
254 | break; | |
255 | ||
545dbc93 RM |
256 | #if defined USE_TLS && (!defined RTLD_BOOTSTRAP || USE___THREAD) \ |
257 | && !defined RESOLVE_CONFLICT_FIND_MAP | |
7551556f RM |
258 | # ifdef RTLD_BOOTSTRAP |
259 | # define NOT_BOOTSTRAP 0 | |
260 | # else | |
261 | # define NOT_BOOTSTRAP 1 | |
bb0ddc2f | 262 | # endif |
7551556f | 263 | |
545dbc93 RM |
264 | case R_PPC_DTPMOD32: |
265 | if (!NOT_BOOTSTRAP) | |
266 | /* During startup the dynamic linker is always index 1. */ | |
267 | *reloc_addr = 1; | |
268 | else if (sym_map != NULL) | |
269 | /* Get the information from the link map returned by the | |
270 | RESOLVE_MAP function. */ | |
271 | *reloc_addr = sym_map->l_tls_modid; | |
272 | break; | |
273 | case R_PPC_DTPREL32: | |
274 | /* During relocation all TLS symbols are defined and used. | |
275 | Therefore the offset is already correct. */ | |
276 | if (NOT_BOOTSTRAP && sym_map != NULL) | |
277 | *reloc_addr = TLS_DTPREL_VALUE (sym, reloc); | |
278 | break; | |
279 | case R_PPC_TPREL32: | |
280 | if (!NOT_BOOTSTRAP || sym_map != NULL) | |
bb0ddc2f | 281 | { |
545dbc93 RM |
282 | if (NOT_BOOTSTRAP) |
283 | CHECK_STATIC_TLS (map, sym_map); | |
284 | *reloc_addr = TLS_TPREL_VALUE (sym_map, sym, reloc); | |
bb0ddc2f | 285 | } |
545dbc93 | 286 | break; |
bb0ddc2f RM |
287 | #endif /* USE_TLS etc. */ |
288 | ||
0ecb606c | 289 | #ifdef RESOLVE_CONFLICT_FIND_MAP |
a334319f | 290 | case R_PPC_JMP_SLOT: |
bb0ddc2f | 291 | RESOLVE_CONFLICT_FIND_MAP (map, reloc_addr); |
0ecb606c | 292 | /* FALLTHROUGH */ |
a334319f | 293 | #endif |
bb0ddc2f RM |
294 | |
295 | default: | |
545dbc93 | 296 | __process_machine_rela (map, reloc, sym_map, sym, refsym, |
bb0ddc2f | 297 | reloc_addr, value, r_type); |
1f205a47 | 298 | } |
1f205a47 UD |
299 | } |
300 | ||
a334319f | 301 | static inline void |
1f2a1df3 | 302 | elf_machine_rela_relative (Elf32_Addr l_addr, const Elf32_Rela *reloc, |
87d254a7 | 303 | void *const reloc_addr_arg) |
1721af3f | 304 | { |
87d254a7 | 305 | Elf32_Addr *const reloc_addr = reloc_addr_arg; |
1721af3f UD |
306 | *reloc_addr = l_addr + reloc->r_addend; |
307 | } | |
1f205a47 | 308 | |
a334319f | 309 | static inline void |
7ba7c829 UD |
310 | elf_machine_lazy_rel (struct link_map *map, |
311 | Elf32_Addr l_addr, const Elf32_Rela *reloc) | |
312 | { | |
313 | /* elf_machine_runtime_setup handles this. */ | |
314 | } | |
315 | ||
052b6a6c UD |
316 | /* The SVR4 ABI specifies that the JMPREL relocs must be inside the |
317 | DT_RELA table. */ | |
318 | #define ELF_MACHINE_PLTREL_OVERLAP 1 | |
319 | ||
2413fdba | 320 | #endif /* RESOLVE_MAP */ |