Update.
[glibc.git] / sysdeps / powerpc / dl-machine.h
blob961aafbd62dab92d35950ee169e8c1e4f8742474
1 /* Machine-dependent ELF dynamic relocation inline functions. PowerPC version.
2 Copyright (C) 1995-2000, 2001 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 Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 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 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
20 #ifndef dl_machine_h
21 #define dl_machine_h
23 #define ELF_MACHINE_NAME "powerpc"
25 #include <assert.h>
27 /* Return nonzero iff ELF header is compatible with the running host. */
28 static inline int
29 elf_machine_matches_host (const Elf32_Ehdr *ehdr)
31 return ehdr->e_machine == EM_PPC;
35 /* Return the link-time address of _DYNAMIC, stored as
36 the first value in the GOT. */
37 static inline Elf32_Addr
38 elf_machine_dynamic (void)
40 Elf32_Addr *got;
41 asm (" bl _GLOBAL_OFFSET_TABLE_-4@local"
42 : "=l"(got));
43 return *got;
46 /* Return the run-time load address of the shared object. */
47 static inline Elf32_Addr
48 elf_machine_load_address (void)
50 unsigned int *got;
51 unsigned int *branchaddr;
53 /* This is much harder than you'd expect. Possibly I'm missing something.
54 The 'obvious' way:
56 Apparently, "bcl 20,31,$+4" is what should be used to load LR
57 with the address of the next instruction.
58 I think this is so that machines that do bl/blr pairing don't
59 get confused.
61 asm ("bcl 20,31,0f ;"
62 "0: mflr 0 ;"
63 "lis %0,0b@ha;"
64 "addi %0,%0,0b@l;"
65 "subf %0,%0,0"
66 : "=b" (addr) : : "r0", "lr");
68 doesn't work, because the linker doesn't have to (and in fact doesn't)
69 update the @ha and @l references; the loader (which runs after this
70 code) will do that.
72 Instead, we use the following trick:
74 The linker puts the _link-time_ address of _DYNAMIC at the first
75 word in the GOT. We could branch to that address, if we wanted,
76 by using an @local reloc; the linker works this out, so it's safe
77 to use now. We can't, of course, actually branch there, because
78 we'd cause an illegal instruction exception; so we need to compute
79 the address ourselves. That gives us the following code: */
81 /* Get address of the 'b _DYNAMIC@local'... */
82 asm ("bl 0f ;"
83 "b _DYNAMIC@local;"
84 "0:"
85 : "=l"(branchaddr));
87 /* ... and the address of the GOT. */
88 asm (" bl _GLOBAL_OFFSET_TABLE_-4@local"
89 : "=l"(got));
91 /* So now work out the difference between where the branch actually points,
92 and the offset of that location in memory from the start of the file. */
93 return ((Elf32_Addr)branchaddr - *got
94 + ((int)(*branchaddr << 6 & 0xffffff00) >> 6));
97 #define ELF_MACHINE_BEFORE_RTLD_RELOC(dynamic_info) /* nothing */
99 /* The PLT uses Elf32_Rela relocs. */
100 #define elf_machine_relplt elf_machine_rela
102 /* This code is used in dl-runtime.c to call the `fixup' function
103 and then redirect to the address it returns. It is called
104 from code built in the PLT by elf_machine_runtime_setup. */
105 #define ELF_MACHINE_RUNTIME_TRAMPOLINE asm ("\
106 .section \".text\"
107 .align 2
108 .globl _dl_runtime_resolve
109 .type _dl_runtime_resolve,@function
110 _dl_runtime_resolve:
111 # We need to save the registers used to pass parameters, and register 0,
112 # which is used by _mcount; the registers are saved in a stack frame.
113 stwu 1,-64(1)
114 stw 0,12(1)
115 stw 3,16(1)
116 stw 4,20(1)
117 # The code that calls this has put parameters for `fixup' in r12 and r11.
118 mr 3,12
119 stw 5,24(1)
120 mr 4,11
121 stw 6,28(1)
122 mflr 0
123 # We also need to save some of the condition register fields.
124 stw 7,32(1)
125 stw 0,48(1)
126 stw 8,36(1)
127 mfcr 0
128 stw 9,40(1)
129 stw 10,44(1)
130 stw 0,8(1)
131 bl fixup@local
132 # 'fixup' returns the address we want to branch to.
133 mtctr 3
134 # Put the registers back...
135 lwz 0,48(1)
136 lwz 10,44(1)
137 lwz 9,40(1)
138 mtlr 0
139 lwz 8,36(1)
140 lwz 0,8(1)
141 lwz 7,32(1)
142 lwz 6,28(1)
143 mtcrf 0xFF,0
144 lwz 5,24(1)
145 lwz 4,20(1)
146 lwz 3,16(1)
147 lwz 0,12(1)
148 # ...unwind the stack frame, and jump to the PLT entry we updated.
149 addi 1,1,64
150 bctr
151 .size _dl_runtime_resolve,.-_dl_runtime_resolve
153 .align 2
154 .globl _dl_prof_resolve
155 .type _dl_prof_resolve,@function
156 _dl_prof_resolve:
157 # We need to save the registers used to pass parameters, and register 0,
158 # which is used by _mcount; the registers are saved in a stack frame.
159 stwu 1,-64(1)
160 stw 0,12(1)
161 stw 3,16(1)
162 stw 4,20(1)
163 # The code that calls this has put parameters for `fixup' in r12 and r11.
164 mr 3,12
165 stw 5,24(1)
166 mr 4,11
167 stw 6,28(1)
168 mflr 5
169 # We also need to save some of the condition register fields.
170 stw 7,32(1)
171 stw 5,48(1)
172 stw 8,36(1)
173 mfcr 0
174 stw 9,40(1)
175 stw 10,44(1)
176 stw 0,8(1)
177 bl profile_fixup@local
178 # 'fixup' returns the address we want to branch to.
179 mtctr 3
180 # Put the registers back...
181 lwz 0,48(1)
182 lwz 10,44(1)
183 lwz 9,40(1)
184 mtlr 0
185 lwz 8,36(1)
186 lwz 0,8(1)
187 lwz 7,32(1)
188 lwz 6,28(1)
189 mtcrf 0xFF,0
190 lwz 5,24(1)
191 lwz 4,20(1)
192 lwz 3,16(1)
193 lwz 0,12(1)
194 # ...unwind the stack frame, and jump to the PLT entry we updated.
195 addi 1,1,64
196 bctr
197 .size _dl_prof_resolve,.-_dl_prof_resolve
198 # Undo '.section text'.
199 .previous
202 /* The actual _start code is in dl-start.S. Use a really
203 ugly bit of assembler to let dl-start.o see _dl_start. */
204 #define RTLD_START asm (".globl _dl_start");
206 /* Decide where a relocatable object should be loaded. */
207 extern ElfW(Addr)
208 __elf_preferred_address(struct link_map *loader, size_t maplength,
209 ElfW(Addr) mapstartpref);
210 #define ELF_PREFERRED_ADDRESS(loader, maplength, mapstartpref) \
211 __elf_preferred_address (loader, maplength, mapstartpref)
213 /* Nonzero iff TYPE should not be allowed to resolve to one of
214 the main executable's symbols, as for a COPY reloc. */
215 #define elf_machine_lookup_noexec_p(type) ((type) == R_PPC_COPY)
217 /* Nonzero iff TYPE describes relocation of a PLT entry, so
218 PLT entries should not be allowed to define the value. */
219 /* We never want to use a PLT entry as the destination of a
220 reloc, when what is being relocated is a branch. This is
221 partly for efficiency, but mostly so we avoid loops. */
222 #define elf_machine_lookup_noplt_p(type) ((type) == R_PPC_REL24 || \
223 (type) == R_PPC_ADDR24 || \
224 (type) == R_PPC_JMP_SLOT)
226 /* A reloc type used for ld.so cmdline arg lookups to reject PLT entries. */
227 #define ELF_MACHINE_JMP_SLOT R_PPC_JMP_SLOT
229 /* Set up the loaded object described by L so its unrelocated PLT
230 entries will jump to the on-demand fixup code in dl-runtime.c.
231 Also install a small trampoline to be used by entries that have
232 been relocated to an address too far away for a single branch. */
233 extern int __elf_machine_runtime_setup (struct link_map *map,
234 int lazy, int profile);
235 #define elf_machine_runtime_setup __elf_machine_runtime_setup
237 static inline void
238 elf_machine_lazy_rel (struct link_map *map,
239 Elf32_Addr l_addr, const Elf32_Rela *reloc)
241 /* elf_machine_runtime_setup handles this. */
244 /* Change the PLT entry whose reloc is 'reloc' to call the actual routine. */
245 extern Elf32_Addr __elf_machine_fixup_plt (struct link_map *map,
246 const Elf32_Rela *reloc,
247 Elf32_Addr *reloc_addr,
248 Elf32_Addr finaladdr);
250 static inline Elf32_Addr
251 elf_machine_fixup_plt (struct link_map *map, lookup_t t,
252 const Elf32_Rela *reloc,
253 Elf32_Addr *reloc_addr, Elf64_Addr finaladdr)
255 return __elf_machine_fixup_plt (map, reloc, reloc_addr, finaladdr);
258 /* Return the final value of a plt relocation. */
259 static inline Elf32_Addr
260 elf_machine_plt_value (struct link_map *map, const Elf32_Rela *reloc,
261 Elf32_Addr value)
263 return value + reloc->r_addend;
266 #endif /* dl_machine_h */
268 #ifdef RESOLVE
270 /* Do the actual processing of a reloc, once its target address
271 has been determined. */
272 extern void __process_machine_rela (struct link_map *map,
273 const Elf32_Rela *reloc,
274 const Elf32_Sym *sym,
275 const Elf32_Sym *refsym,
276 Elf32_Addr *const reloc_addr,
277 Elf32_Addr finaladdr,
278 int rinfo);
280 /* Perform the relocation specified by RELOC and SYM (which is fully resolved).
281 LOADADDR is the load address of the object; INFO is an array indexed
282 by DT_* of the .dynamic section info. */
284 inline void
285 elf_machine_rela (struct link_map *map, const Elf32_Rela *reloc,
286 const Elf32_Sym *sym, const struct r_found_version *version,
287 Elf32_Addr *const reloc_addr)
289 const Elf32_Sym *const refsym = sym;
290 Elf32_Word loadbase, finaladdr;
291 const int rinfo = ELF32_R_TYPE (reloc->r_info);
293 if (rinfo == R_PPC_NONE)
294 return;
296 /* The condition on the next two lines is a hack around a bug in Solaris
297 tools on Sparc. It's not clear whether it should really be here at all,
298 but if not the binutils need to be changed. */
299 if (rinfo == R_PPC_RELATIVE
300 || (sym->st_shndx != SHN_UNDEF
301 && ELF32_ST_BIND (sym->st_info) == STB_LOCAL))
303 /* Has already been relocated. */
304 loadbase = map->l_addr;
305 finaladdr = loadbase + reloc->r_addend;
307 else
309 loadbase = (Elf32_Word) (char *) (RESOLVE (&sym, version,
310 ELF32_R_TYPE(reloc->r_info)));
311 if (sym == NULL)
313 /* Weak symbol that wasn't actually defined anywhere. */
314 assert(loadbase == 0);
315 finaladdr = reloc->r_addend;
317 else
318 finaladdr = (loadbase + (Elf32_Word) (char *) sym->st_value
319 + reloc->r_addend);
322 /* A small amount of code is duplicated here for speed. In libc,
323 more than 90% of the relocs are R_PPC_RELATIVE; in the X11 shared
324 libraries, 60% are R_PPC_RELATIVE, 24% are R_PPC_GLOB_DAT or
325 R_PPC_ADDR32, and 16% are R_PPC_JMP_SLOT (which this routine
326 wouldn't usually handle). As an bonus, doing this here allows
327 the switch statement in __process_machine_rela to work. */
328 if (rinfo == R_PPC_RELATIVE
329 || rinfo == R_PPC_GLOB_DAT
330 || rinfo == R_PPC_ADDR32)
332 *reloc_addr = finaladdr;
334 else
335 __process_machine_rela (map, reloc, sym, refsym,
336 reloc_addr, finaladdr, rinfo);
339 #define ELF_MACHINE_NO_REL 1
341 /* The SVR4 ABI specifies that the JMPREL relocs must be inside the
342 DT_RELA table. */
343 #define ELF_MACHINE_PLTREL_OVERLAP 1
345 #endif /* RESOLVE */