Update.
[glibc.git] / sysdeps / powerpc / dl-machine.h
blob6ab79e79ac9bb0b47bcff6f5780cf0c4494b94f2
1 /* Machine-dependent ELF dynamic relocation inline functions. PowerPC version.
2 Copyright (C) 1995, 1996, 1997 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>
26 #include <string.h>
27 #include <link.h>
28 #include <sys/param.h>
31 /* stuff for the PLT */
32 #define PLT_INITIAL_ENTRY_WORDS 18
33 #define PLT_LONGBRANCH_ENTRY_WORDS 10
34 #define PLT_DOUBLE_SIZE (1<<13)
35 #define PLT_ENTRY_START_WORDS(entry_number) \
36 (PLT_INITIAL_ENTRY_WORDS + (entry_number)*2 + \
37 ((entry_number) > PLT_DOUBLE_SIZE ? \
38 ((entry_number) - PLT_DOUBLE_SIZE)*2 : \
39 0))
40 #define PLT_DATA_START_WORDS(num_entries) PLT_ENTRY_START_WORDS(num_entries)
42 #define OPCODE_ADDI(rd,ra,simm) \
43 (0x38000000 | (rd) << 21 | (ra) << 16 | (simm) & 0xffff)
44 #define OPCODE_ADDIS(rd,ra,simm) \
45 (0x3c000000 | (rd) << 21 | (ra) << 16 | (simm) & 0xffff)
46 #define OPCODE_ADD(rd,ra,rb) \
47 (0x7c000214 | (rd) << 21 | (ra) << 16 | (rb) << 11)
48 #define OPCODE_B(target) (0x48000000 | (target) & 0x03fffffc)
49 #define OPCODE_BA(target) (0x48000002 | (target) & 0x03fffffc)
50 #define OPCODE_BCTR() 0x4e800420
51 #define OPCODE_LWZ(rd,d,ra) \
52 (0x80000000 | (rd) << 21 | (ra) << 16 | (d) & 0xffff)
53 #define OPCODE_MTCTR(rd) (0x7C0903A6 | (rd) << 21)
54 #define OPCODE_RLWINM(ra,rs,sh,mb,me) \
55 (0x54000000 | (rs) << 21 | (ra) << 16 | (sh) << 11 | (mb) << 6 | (me) << 1)
57 #define OPCODE_LI(rd,simm) OPCODE_ADDI(rd,0,simm)
58 #define OPCODE_SLWI(ra,rs,sh) OPCODE_RLWINM(ra,rs,sh,0,31-sh)
60 #define PPC_DCBST(where) asm volatile ("dcbst 0,%0" : : "r"(where))
61 #define PPC_SYNC asm volatile ("sync")
62 #define PPC_ISYNC asm volatile ("sync; isync")
63 #define PPC_ICBI(where) asm volatile ("icbi 0,%0" : : "r"(where))
64 #define PPC_DIE asm volatile ("tweq 0,0")
66 /* Use this when you've modified some code, but it won't be in the
67 instruction fetch queue (or when it doesn't matter if it is). */
68 #define MODIFIED_CODE_NOQUEUE(where) \
69 do { PPC_DCBST(where); PPC_SYNC; PPC_ICBI(where); } while (0)
70 /* Use this when it might be in the instruction queue. */
71 #define MODIFIED_CODE(where) \
72 do { PPC_DCBST(where); PPC_SYNC; PPC_ICBI(where); PPC_ISYNC; } while (0)
75 /* Return nonzero iff E_MACHINE is compatible with the running host. */
76 static inline int
77 elf_machine_matches_host (Elf32_Half e_machine)
79 return e_machine == EM_PPC;
83 /* Return the link-time address of _DYNAMIC, stored as
84 the first value in the GOT. */
85 static inline Elf32_Addr
86 elf_machine_dynamic (void)
88 Elf32_Addr *got;
89 asm (" bl _GLOBAL_OFFSET_TABLE_-4@local"
90 : "=l"(got));
91 return *got;
94 /* Return the run-time load address of the shared object. */
95 static inline Elf32_Addr
96 elf_machine_load_address (void)
98 unsigned *got;
99 unsigned *branchaddr;
101 /* This is much harder than you'd expect. Possibly I'm missing something.
102 The 'obvious' way:
104 Apparently, "bcl 20,31,$+4" is what should be used to load LR
105 with the address of the next instruction.
106 I think this is so that machines that do bl/blr pairing don't
107 get confused.
109 asm ("bcl 20,31,0f ;"
110 "0: mflr 0 ;"
111 "lis %0,0b@ha;"
112 "addi %0,%0,0b@l;"
113 "subf %0,%0,0"
114 : "=b" (addr) : : "r0", "lr");
116 doesn't work, because the linker doesn't have to (and in fact doesn't)
117 update the @ha and @l references; the loader (which runs after this
118 code) will do that.
120 Instead, we use the following trick:
122 The linker puts the _link-time_ address of _DYNAMIC at the first
123 word in the GOT. We could branch to that address, if we wanted,
124 by using an @local reloc; the linker works this out, so it's safe
125 to use now. We can't, of course, actually branch there, because
126 we'd cause an illegal instruction exception; so we need to compute
127 the address ourselves. That gives us the following code: */
129 /* Get address of the 'b _DYNAMIC@local'... */
130 asm ("bl 0f ;"
131 "b _DYNAMIC@local;"
132 "0:"
133 : "=l"(branchaddr));
135 /* ... and the address of the GOT. */
136 asm (" bl _GLOBAL_OFFSET_TABLE_-4@local"
137 : "=l"(got));
139 /* So now work out the difference between where the branch actually points,
140 and the offset of that location in memory from the start of the file. */
141 return ((Elf32_Addr)branchaddr - *got
142 + (*branchaddr & 0x3fffffc
143 | (int)(*branchaddr << 6 & 0x80000000) >> 6));
146 #define ELF_MACHINE_BEFORE_RTLD_RELOC(dynamic_info) /* nothing */
148 /* The PLT uses Elf32_Rela relocs. */
149 #define elf_machine_relplt elf_machine_rela
151 /* This code is used in dl-runtime.c to call the `fixup' function
152 and then redirect to the address it returns. It is called
153 from code built in the PLT by elf_machine_runtime_setup. */
154 #define ELF_MACHINE_RUNTIME_TRAMPOLINE asm ("\
155 .section \".text\"
156 .align 2
157 .globl _dl_runtime_resolve
158 .type _dl_runtime_resolve,@function
159 _dl_runtime_resolve:
160 # We need to save the registers used to pass parameters, and register 0,
161 # which is used by _mcount; the registers are saved in a stack frame.
162 stwu 1,-48(1)
163 stw 0,12(1)
164 stw 3,16(1)
165 stw 4,20(1)
166 # The code that calls this has put parameters for `fixup' in r12 and r11.
167 mr 3,12
168 stw 5,24(1)
169 mr 4,11
170 stw 6,28(1)
171 mflr 0
172 # We also need to save some of the condition register fields.
173 stw 7,32(1)
174 stw 0,52(1)
175 stw 8,36(1)
176 mfcr 0
177 stw 9,40(1)
178 stw 10,44(1)
179 stw 0,8(1)
180 bl fixup@local
181 # 'fixup' returns the address we want to branch to.
182 mtctr 3
183 # Put the registers back...
184 lwz 0,52(1)
185 lwz 10,44(1)
186 lwz 9,40(1)
187 mtlr 0
188 lwz 8,36(1)
189 lwz 0,8(1)
190 lwz 7,32(1)
191 lwz 6,28(1)
192 mtcrf 0xFF,0
193 lwz 5,24(1)
194 lwz 4,20(1)
195 lwz 3,16(1)
196 lwz 0,12(1)
197 # ...unwind the stack frame, and jump to the PLT entry we updated.
198 addi 1,1,48
199 bctr
200 .size _dl_runtime_resolve,.-_dl_runtime_resolve
202 .align 2
203 .globl _dl_prof_resolve
204 .type _dl_prof_resolve,@function
205 _dl_prof_resolve:
206 # We need to save the registers used to pass parameters, and register 0,
207 # which is used by _mcount; the registers are saved in a stack frame.
208 stwu 1,-48(1)
209 stw 0,12(1)
210 stw 3,16(1)
211 stw 4,20(1)
212 # The code that calls this has put parameters for `fixup' in r12 and r11.
213 mr 3,12
214 stw 5,24(1)
215 mr 4,11
216 stw 6,28(1)
217 mflr 5
218 # We also need to save some of the condition register fields.
219 stw 7,32(1)
220 stw 5,52(1)
221 stw 8,36(1)
222 mfcr 0
223 stw 9,40(1)
224 stw 10,44(1)
225 stw 0,8(1)
226 bl profile_fixup@local
227 # 'fixup' returns the address we want to branch to.
228 mtctr 3
229 # Put the registers back...
230 lwz 0,52(1)
231 lwz 10,44(1)
232 lwz 9,40(1)
233 mtlr 0
234 lwz 8,36(1)
235 lwz 0,8(1)
236 lwz 7,32(1)
237 lwz 6,28(1)
238 mtcrf 0xFF,0
239 lwz 5,24(1)
240 lwz 4,20(1)
241 lwz 3,16(1)
242 lwz 0,12(1)
243 # ...unwind the stack frame, and jump to the PLT entry we updated.
244 addi 1,1,48
245 bctr
247 .size _dl_prof_resolve,0b-_dl_prof_resolve
248 # Undo '.section text'.
249 .previous
252 /* Initial entry point code for the dynamic linker.
253 The C function `_dl_start' is the real entry point;
254 its return value is the user program's entry point. */
255 #define RTLD_START \
256 static ElfW(Addr) _dl_start (void *arg) __attribute__((unused)); \
257 asm ("\
258 .section \".text\"
259 .align 2
260 .globl _start
261 .type _start,@function
262 _start:
263 # We start with the following on the stack, from top:
264 # argc (4 bytes);
265 # arguments for program (terminated by NULL);
266 # environment variables (terminated by NULL);
267 # arguments for the program loader.
268 # FIXME: perhaps this should do the same trick as elf/start.c?
270 # Call _dl_start with one parameter pointing at argc
271 mr 3,1
272 # (we have to frob the stack pointer a bit to allow room for
273 # _dl_start to save the link register)
274 li 4,0
275 addi 1,1,-16
276 stw 4,0(1)
277 bl _dl_start@local
279 # Now, we do our main work of calling initialisation procedures.
280 # The ELF ABI doesn't say anything about parameters for these,
281 # so we just pass argc, argv, and the environment.
282 # Changing these is strongly discouraged (not least because argc is
283 # passed by value!).
285 # Put our GOT pointer in r31,
286 bl _GLOBAL_OFFSET_TABLE_-4@local
287 mflr 31
288 # the address of _start in r30,
289 mr 30,3
290 # &_dl_argc in 29, &_dl_argv in 27, and _dl_default_scope in 28.
291 lwz 28,_dl_default_scope@got(31)
292 lwz 29,_dl_argc@got(31)
293 lwz 27,_dl_argv@got(31)
295 # Set initfunc = _dl_init_next(_dl_default_scope[2])
296 lwz 3,8(28)
297 bl _dl_init_next@plt
298 # If initfunc is NULL, we exit the loop; otherwise,
299 cmpwi 3,0
300 beq 1f
301 # call initfunc(_dl_argc, _dl_argv, _dl_argv+_dl_argc+1)
302 mtlr 3
303 lwz 3,0(29)
304 lwz 4,0(27)
305 slwi 5,3,2
306 add 5,4,5
307 addi 5,5,4
308 blrl
309 # and loop.
310 b 0b
312 # Now, to conform to the ELF ABI, we have to:
313 # Pass argc (actually _dl_argc) in r3;
314 lwz 3,0(29)
315 # pass argv (actually _dl_argv) in r4;
316 lwz 4,0(27)
317 # pass envp (actually _dl_argv+_dl_argc+1) in r5;
318 slwi 5,3,2
319 add 6,4,5
320 addi 5,6,4
321 # pass the auxilary vector in r6. This is passed to us just after _envp.
322 2: lwzu 0,4(6)
323 cmpwi 0,0,0
324 bne 2b
325 addi 6,6,4
326 # Pass a termination function pointer (in this case _dl_fini) in r7.
327 lwz 7,_dl_fini@got(31)
328 # Now, call the start function in r30...
329 mtctr 30
330 lwz 26,_dl_starting_up@got(31)
331 # Pass the stack pointer in r1 (so far so good), pointing to a NULL value.
332 # (This lets our startup code distinguish between a program linked statically,
333 # which linux will call with argc on top of the stack which will hopefully
334 # never be zero, and a dynamically linked program which will always have
335 # a NULL on the top of the stack).
336 # Take the opportunity to clear LR, so anyone who accidentally returns
337 # from _start gets SEGV. Also clear the next few words of the stack.
338 li 31,0
339 stw 31,0(1)
340 mtlr 31
341 stw 31,4(1)
342 stw 31,8(1)
343 stw 31,12(1)
344 # Clear _dl_starting_up.
345 stw 31,0(26)
346 # Go do it!
347 bctr
349 .size _start,0b-_start
350 # Undo '.section text'.
351 .previous
354 /* The idea here is that to conform to the ABI, we are supposed to try
355 to load dynamic objects between 0x10000 (we actually use 0x40000 as
356 the lower bound, to increase the chance of a memory reference from
357 a null pointer giving a segfault) and the program's load address.
358 Regrettably, in this code we can't find the program's load address,
359 so we punt and choose 0x01800000, which is below the ABI's
360 recommended default, and what GNU ld currently chooses. We only use
361 the address as a preference for mmap, so if we get it wrong the
362 worst that happens is that it gets mapped somewhere else.
364 FIXME: Unfortunately, 'somewhere else' is probably right after the
365 program's break, which causes malloc to fail. We really need more
366 information here about the way memory is mapped. */
368 #define ELF_PREFERRED_ADDRESS_DATA \
369 static ElfW(Addr) _dl_preferred_address = 1
371 #define ELF_PREFERRED_ADDRESS(loader, maplength, mapstartpref) \
372 ( { \
373 ElfW(Addr) prefd; \
374 if (mapstartpref != 0 && _dl_preferred_address == 1) \
375 _dl_preferred_address = mapstartpref; \
376 if (mapstartpref != 0) \
377 prefd = mapstartpref; \
378 else if (_dl_preferred_address == 1) \
379 prefd = _dl_preferred_address = \
380 (0x01800000 - maplength - 0x10000) & \
381 ~(_dl_pagesize - 1); \
382 else if (_dl_preferred_address < maplength + 0x50000) \
383 prefd = 0; \
384 else \
385 prefd = _dl_preferred_address = \
386 ((_dl_preferred_address - maplength - 0x10000) \
387 & ~(_dl_pagesize - 1)); \
388 prefd; \
391 #define ELF_FIXED_ADDRESS(loader, mapstart) \
392 ( { \
393 if (mapstart != 0 && _dl_preferred_address == 1) \
394 _dl_preferred_address = mapstart; \
397 /* Nonzero iff TYPE should not be allowed to resolve to one of
398 the main executable's symbols, as for a COPY reloc. */
399 #define elf_machine_lookup_noexec_p(type) ((type) == R_PPC_COPY)
401 /* Nonzero iff TYPE describes relocation of a PLT entry, so
402 PLT entries should not be allowed to define the value. */
403 /* We never want to use a PLT entry as the destination of a
404 reloc, when what is being relocated is a branch. This is
405 partly for efficiency, but mostly so we avoid loops. */
406 #define elf_machine_lookup_noplt_p(type) ((type) == R_PPC_REL24 || \
407 (type) == R_PPC_ADDR24 || \
408 (type) == R_PPC_JMP_SLOT)
410 /* A reloc type used for ld.so cmdline arg lookups to reject PLT entries. */
411 #define ELF_MACHINE_JMP_SLOT R_PPC_JMP_SLOT
413 /* Nonzero iff TYPE describes relocation of a PLT entry, so
414 PLT entries should not be allowed to define the value. */
415 #define elf_machine_pltrel_p(type) ((type) == R_PPC_JMP_SLOT)
417 /* Set up the loaded object described by L so its unrelocated PLT
418 entries will jump to the on-demand fixup code in dl-runtime.c.
419 Also install a small trampoline to be used by entries that have
420 been relocated to an address too far away for a single branch. */
422 /* A PLT entry does one of three things:
423 (i) Jumps to the actual routine. Such entries are set up above, in
424 elf_machine_rela.
426 (ii) Jumps to the actual routine via glue at the start of the PLT.
427 We do this by putting the address of the routine in space
428 allocated at the end of the PLT, and when the PLT entry is
429 called we load the offset of that word (from the start of the
430 space) into r11, then call the glue, which loads the word and
431 branches to that address. These entries are set up in
432 elf_machine_rela, but the glue is set up here.
434 (iii) Loads the index of this PLT entry (we count the double-size
435 entries as one entry for this purpose) into r11, then
436 branches to code at the start of the PLT. This code then
437 calls `fixup', in dl-runtime.c, via the glue in the macro
438 ELF_MACHINE_RUNTIME_TRAMPOLINE, which resets the PLT entry to
439 be one of the above two types. These entries are set up here. */
440 static inline int
441 elf_machine_runtime_setup (struct link_map *map, int lazy, int profile)
443 if (map->l_info[DT_JMPREL])
445 Elf32_Word i;
446 /* Fill in the PLT. Its initial contents are directed to a
447 function earlier in the PLT which arranges for the dynamic
448 linker to be called back. */
449 Elf32_Word *plt = (Elf32_Word *) ((char *) map->l_addr
450 + map->l_info[DT_PLTGOT]->d_un.d_val);
451 Elf32_Word num_plt_entries = (map->l_info[DT_PLTRELSZ]->d_un.d_val
452 / sizeof (Elf32_Rela));
453 Elf32_Word rel_offset_words = PLT_DATA_START_WORDS (num_plt_entries);
454 Elf32_Word size_modified;
455 extern void _dl_runtime_resolve (void);
456 extern void _dl_prof_resolve (void);
457 Elf32_Word dlrr;
459 dlrr = (Elf32_Word)(char *)(profile
460 ? _dl_prof_resolve
461 : _dl_runtime_resolve);
463 if (lazy)
464 for (i = 0; i < num_plt_entries; i++)
466 Elf32_Word offset = PLT_ENTRY_START_WORDS (i);
468 if (i >= PLT_DOUBLE_SIZE)
470 plt[offset ] = OPCODE_LI (11, i * 4);
471 plt[offset+1] = OPCODE_ADDIS (11, 11, (i * 4 + 0x8000) >> 16);
472 plt[offset+2] = OPCODE_B (-(4 * (offset + 2)));
474 else
476 plt[offset ] = OPCODE_LI (11, i * 4);
477 plt[offset+1] = OPCODE_B (-(4 * (offset + 1)));
481 /* Multiply index of entry by 3 (in r11). */
482 plt[0] = OPCODE_SLWI (12, 11, 1);
483 plt[1] = OPCODE_ADD (11, 12, 11);
484 if (dlrr <= 0x01fffffc || dlrr >= 0xfe000000)
486 /* Load address of link map in r12. */
487 plt[2] = OPCODE_LI (12, (Elf32_Word) (char *) map);
488 plt[3] = OPCODE_ADDIS (12, 12, (((Elf32_Word) (char *) map
489 + 0x8000) >> 16));
491 /* Call _dl_runtime_resolve. */
492 plt[4] = OPCODE_BA (dlrr);
494 else
496 /* Get address of _dl_runtime_resolve in CTR. */
497 plt[2] = OPCODE_LI (12, dlrr);
498 plt[3] = OPCODE_ADDIS (12, 12, (dlrr + 0x8000) >> 16);
499 plt[4] = OPCODE_MTCTR (12);
501 /* Load address of link map in r12. */
502 plt[5] = OPCODE_LI (12, (Elf32_Word) (char *) map);
503 plt[6] = OPCODE_ADDIS (12, 12, (((Elf32_Word) (char *) map
504 + 0x8000) >> 16));
506 /* Call _dl_runtime_resolve. */
507 plt[7] = OPCODE_BCTR ();
511 /* Convert the index in r11 into an actual address, and get the
512 word at that address. */
513 plt[PLT_LONGBRANCH_ENTRY_WORDS] =
514 OPCODE_ADDIS (11, 11, (((Elf32_Word) (char*) (plt + rel_offset_words)
515 + 0x8000) >> 16));
516 plt[PLT_LONGBRANCH_ENTRY_WORDS+1] =
517 OPCODE_LWZ (11, (Elf32_Word) (char*) (plt+rel_offset_words), 11);
519 /* Call the procedure at that address. */
520 plt[PLT_LONGBRANCH_ENTRY_WORDS+2] = OPCODE_MTCTR (11);
521 plt[PLT_LONGBRANCH_ENTRY_WORDS+3] = OPCODE_BCTR ();
524 /* Now, we've modified code (quite a lot of code, possibly). We
525 need to write the changes from the data cache to a
526 second-level unified cache, then make sure that stale data in
527 the instruction cache is removed. (In a multiprocessor
528 system, the effect is more complex.)
530 Assumes the cache line size is at least 32 bytes, or at least
531 that dcbst and icbi apply to 32-byte lines. At present, all
532 PowerPC processors have line sizes of exactly 32 bytes. */
534 size_modified = lazy ? rel_offset_words : PLT_INITIAL_ENTRY_WORDS;
535 for (i = 0; i < size_modified; i+=8)
536 PPC_DCBST (plt + i);
537 PPC_SYNC;
538 for (i = 0; i < size_modified; i+=8)
539 PPC_ICBI (plt + i);
540 PPC_ISYNC;
543 return lazy;
546 static inline void
547 elf_machine_lazy_rel (struct link_map *map, const Elf32_Rela *reloc)
549 /* elf_machine_runtime_setup handles this. */
552 static inline void
553 elf_machine_fixup_plt(struct link_map *map, const Elf32_Rela *reloc,
554 Elf32_Addr *reloc_addr, Elf32_Addr finaladdr)
556 Elf32_Sword delta = finaladdr - (Elf32_Word) (char *) reloc_addr;
557 if (delta << 6 >> 6 == delta)
558 *reloc_addr = OPCODE_B (delta);
559 else if (finaladdr <= 0x01fffffc || finaladdr >= 0xfe000000)
560 *reloc_addr = OPCODE_BA (finaladdr);
561 else
563 Elf32_Word *plt;
564 Elf32_Word index;
566 plt = (Elf32_Word *)((char *)map->l_addr
567 + map->l_info[DT_PLTGOT]->d_un.d_val);
568 index = (reloc_addr - plt - PLT_INITIAL_ENTRY_WORDS)/2;
569 if (index >= PLT_DOUBLE_SIZE)
571 /* Slots greater than or equal to 2^13 have 4 words available
572 instead of two. */
573 /* FIXME: There are some possible race conditions in this code,
574 when called from 'fixup'.
576 1) Suppose that a lazy PLT entry is executing, a context switch
577 between threads (or a signal) occurs, and the new thread or
578 signal handler calls the same lazy PLT entry. Then the PLT entry
579 would be changed while it's being run, which will cause a segfault
580 (almost always).
582 2) Suppose the reverse: that a lazy PLT entry is being updated,
583 a context switch occurs, and the new code calls the lazy PLT
584 entry that is being updated. Then the half-fixed PLT entry will
585 be executed, which will also almost always cause a segfault.
587 These problems don't happen with the 2-word entries, because
588 only one of the two instructions are changed when a lazy entry
589 is retargeted at the actual PLT entry; the li instruction stays
590 the same (we have to update it anyway, because we might not be
591 updating a lazy PLT entry). */
593 reloc_addr[0] = OPCODE_LI (11, finaladdr);
594 reloc_addr[1] = OPCODE_ADDIS (11, 11, finaladdr + 0x8000 >> 16);
595 reloc_addr[2] = OPCODE_MTCTR (11);
596 reloc_addr[3] = OPCODE_BCTR ();
598 else
600 Elf32_Word num_plt_entries;
602 num_plt_entries = (map->l_info[DT_PLTRELSZ]->d_un.d_val
603 / sizeof(Elf32_Rela));
605 plt[index+PLT_DATA_START_WORDS (num_plt_entries)] = finaladdr;
606 reloc_addr[0] = OPCODE_LI (11, index*4);
607 reloc_addr[1] = OPCODE_B (-(4*(index*2
609 - PLT_LONGBRANCH_ENTRY_WORDS
610 + PLT_INITIAL_ENTRY_WORDS)));
613 MODIFIED_CODE (reloc_addr);
616 /* Return the final value of a plt relocation. */
617 static inline Elf32_Addr
618 elf_machine_plt_value (struct link_map *map, const Elf32_Rela *reloc,
619 Elf32_Addr value)
621 return value + reloc->r_addend;
624 #endif /* dl_machine_h */
626 #ifdef RESOLVE
628 /* Perform the relocation specified by RELOC and SYM (which is fully resolved).
629 LOADADDR is the load address of the object; INFO is an array indexed
630 by DT_* of the .dynamic section info. */
632 static void
633 elf_machine_rela (struct link_map *map, const Elf32_Rela *reloc,
634 const Elf32_Sym *sym, const struct r_found_version *version,
635 Elf32_Addr *const reloc_addr)
637 #ifndef RTLD_BOOTSTRAP
638 const Elf32_Sym *const refsym = sym;
639 extern char **_dl_argv;
640 #endif
641 Elf32_Word loadbase, finaladdr;
642 const int rinfo = ELF32_R_TYPE (reloc->r_info);
644 if (rinfo == R_PPC_NONE)
645 return;
647 assert (sym != NULL);
648 /* The condition on the next two lines is a hack around a bug in Solaris
649 tools on Sparc. It's not clear whether it should really be here at all,
650 but if not the binutils need to be changed. */
651 if ((sym->st_shndx != SHN_UNDEF
652 && ELF32_ST_BIND (sym->st_info) == STB_LOCAL)
653 || rinfo == R_PPC_RELATIVE)
655 /* Has already been relocated. */
656 loadbase = map->l_addr;
657 finaladdr = loadbase + reloc->r_addend;
659 else
661 loadbase = (Elf32_Word) (char *) (RESOLVE (&sym, version,
662 ELF32_R_TYPE(reloc->r_info)));
663 if (sym == NULL)
665 /* Weak symbol that wasn't actually defined anywhere. */
666 assert(loadbase == 0);
667 finaladdr = reloc->r_addend;
669 else
670 finaladdr = (loadbase + (Elf32_Word) (char *) sym->st_value
671 + reloc->r_addend);
674 /* This is still an if/else if chain because GCC uses the GOT to find
675 the table for table-based switch statements, and we haven't set it
676 up yet. */
677 if (rinfo == R_PPC_UADDR32 ||
678 rinfo == R_PPC_GLOB_DAT ||
679 rinfo == R_PPC_ADDR32 ||
680 rinfo == R_PPC_RELATIVE)
682 *reloc_addr = finaladdr;
684 #ifndef RTLD_BOOTSTRAP
685 else if (rinfo == R_PPC_ADDR16_LO)
687 *(Elf32_Half*) reloc_addr = finaladdr;
689 else if (rinfo == R_PPC_ADDR16_HI)
691 *(Elf32_Half*) reloc_addr = finaladdr >> 16;
693 else if (rinfo == R_PPC_ADDR16_HA)
695 *(Elf32_Half*) reloc_addr = (finaladdr + 0x8000) >> 16;
697 else if (rinfo == R_PPC_REL24)
699 Elf32_Sword delta = finaladdr - (Elf32_Word) (char *) reloc_addr;
700 if (delta << 6 >> 6 != delta)
702 _dl_signal_error(0, map->l_name,
703 "R_PPC_REL24 relocation out of range");
705 *reloc_addr = *reloc_addr & 0xfc000003 | delta & 0x3fffffc;
707 else if (rinfo == R_PPC_ADDR24)
709 if (finaladdr << 6 >> 6 != finaladdr)
711 _dl_signal_error(0, map->l_name,
712 "R_PPC_ADDR24 relocation out of range");
714 *reloc_addr = *reloc_addr & 0xfc000003 | finaladdr & 0x3fffffc;
716 else if (rinfo == R_PPC_COPY)
718 if (sym == NULL)
719 /* This can happen in trace mode when an object could not be
720 found. */
721 return;
722 if (sym->st_size > refsym->st_size
723 || (_dl_verbose && sym->st_size < refsym->st_size))
725 const char *strtab;
727 strtab = ((void *) map->l_addr
728 + map->l_info[DT_STRTAB]->d_un.d_ptr);
729 _dl_sysdep_error (_dl_argv[0] ?: "<program name unknown>",
730 ": Symbol `", strtab + refsym->st_name,
731 "' has different size in shared object, "
732 "consider re-linking\n", NULL);
734 memcpy (reloc_addr, (char *) finaladdr, MIN (sym->st_size,
735 refsym->st_size));
737 #endif
738 else if (rinfo == R_PPC_REL32)
740 *reloc_addr = finaladdr - (Elf32_Word) (char *) reloc_addr;
742 else if (rinfo == R_PPC_JMP_SLOT)
744 elf_machine_fixup_plt (map, reloc, reloc_addr, finalvalue);
746 else
748 #ifdef RTLD_BOOTSTRAP
749 PPC_DIE; /* There is no point calling _dl_sysdep_error, it
750 almost certainly hasn't been relocated properly. */
751 #else
752 _dl_sysdep_error (_dl_argv[0] ?: "<program name unknown>",
753 ": Unknown relocation type\n", NULL);
754 #endif
757 #ifndef RTLD_BOOTSTRAP
758 if (rinfo == R_PPC_ADDR16_LO ||
759 rinfo == R_PPC_ADDR16_HI ||
760 rinfo == R_PPC_ADDR16_HA ||
761 rinfo == R_PPC_REL24 ||
762 rinfo == R_PPC_ADDR24)
763 MODIFIED_CODE_NOQUEUE (reloc_addr);
764 #endif
767 #define ELF_MACHINE_NO_REL 1
769 #endif /* RESOLVE */