Update.
[glibc.git] / sysdeps / powerpc / dl-machine.h
blob74436d15c13cf675e66224294fe441e0c7f997f5
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.
161 # We build a stack frame to put them in.
162 stwu 1,-48(1)
163 mflr 0
164 stw 3,16(1)
165 stw 4,20(1)
166 stw 0,52(1)
167 stw 5,24(1)
168 # We also need to save some of the condition register fields.
169 mfcr 0
170 stw 6,28(1)
171 stw 7,32(1)
172 stw 8,36(1)
173 stw 9,40(1)
174 stw 10,44(1)
175 stw 0,12(1)
176 # The code that calls this has put parameters for `fixup' in r12 and r11.
177 mr 3,12
178 mr 4,11
179 bl fixup@local
180 # 'fixup' returns the address we want to branch to.
181 mtctr 3
182 # Put the registers back...
183 lwz 0,52(1)
184 lwz 10,44(1)
185 lwz 9,40(1)
186 mtlr 0
187 lwz 0,12(1)
188 lwz 8,36(1)
189 lwz 7,32(1)
190 lwz 6,28(1)
191 mtcrf 0xFF,0
192 lwz 5,24(1)
193 lwz 4,20(1)
194 lwz 3,16(1)
195 # ...unwind the stack frame, and jump to the PLT entry we updated.
196 addi 1,1,48
197 bctr
199 .size _dl_runtime_resolve,0b-_dl_runtime_resolve
200 # undo '.section text'.
201 .previous
204 /* Initial entry point code for the dynamic linker.
205 The C function `_dl_start' is the real entry point;
206 its return value is the user program's entry point. */
207 #define RTLD_START \
208 static ElfW(Addr) _dl_start (void *arg) __attribute__((unused)); \
209 asm ("\
210 .section \".text\"
211 .align 2
212 .globl _start
213 .type _start,@function
214 _start:
215 # We start with the following on the stack, from top:
216 # argc (4 bytes)
217 # arguments for program (terminated by NULL)
218 # environment variables (terminated by NULL)
219 # arguments for the program loader
220 # FIXME: perhaps this should do the same trick as elf/start.c?
222 # Call _dl_start with one parameter pointing at argc
223 mr 3,1
224 # (we have to frob the stack pointer a bit to allow room for
225 # _dl_start to save the link register)
226 li 4,0
227 addi 1,1,-16
228 stw 4,0(1)
229 bl _dl_start@local
231 # Now, we do our main work of calling initialisation procedures.
232 # The ELF ABI doesn't say anything about parameters for these,
233 # so we just pass argc, argv, and the environment.
234 # Changing these is strongly discouraged (not least because argc is
235 # passed by value!).
237 # put our GOT pointer in r31
238 bl _GLOBAL_OFFSET_TABLE_-4@local
239 mflr 31
240 # the address of _start in r30
241 mr 30,3
242 # &_dl_argc in 29, &_dl_argv in 27, and _dl_default_scope in 28
243 lwz 28,_dl_default_scope@got(31)
244 lwz 29,_dl_argc@got(31)
245 lwz 27,_dl_argv@got(31)
247 # call initfunc = _dl_init_next(_dl_default_scope[2])
248 lwz 3,8(28)
249 bl _dl_init_next@plt
250 # if initfunc is NULL, we exit the loop
251 mr. 0,3
252 beq 1f
253 # call initfunc(_dl_argc, _dl_argv, _dl_argv+_dl_argc+1)
254 mtlr 0
255 lwz 3,0(29)
256 lwz 4,0(27)
257 slwi 5,3,2
258 add 5,4,5
259 addi 5,5,4
260 blrl
261 # and loop.
262 b 0b
264 # Now, to conform to the ELF ABI, we have to:
265 # pass argv (actually _dl_argv) in r4
266 lwz 4,0(27)
267 # pass argc (actually _dl_argc) in r3
268 lwz 3,0(29)
269 # pass envp (actually _dl_argv+_dl_argc+1) in r5
270 slwi 5,3,2
271 add 5,4,5
272 addi 5,5,4
273 # pass the auxilary vector in r6. This is passed just after _envp.
274 addi 6,5,-4
275 2: lwzu 0,4(6)
276 cmpwi 1,0,0
277 bne 2b
278 addi 6,6,4
279 # pass a termination function pointer (in this case _dl_fini) in r7
280 lwz 7,_dl_fini@got(31)
281 # now, call the start function in r30...
282 mtctr 30
283 # pass the stack pointer in r1 (so far so good), pointing to a NULL value
284 # (this lets our startup code distinguish between a program linked statically,
285 # which linux will call with argc on top of the stack which will hopefully
286 # never be zero, and a dynamically linked program which will always have
287 # a NULL on the top of the stack).
288 # Take the opportunity to clear LR, so anyone who accidentally returns
289 # from _start gets SEGV.
290 li 0,0
291 stw 0,0(1)
292 mtlr 0
293 # and also clear _dl_starting_up
294 lwz 26,_dl_starting_up@got(31)
295 stw 0,0(26)
296 # go do it!
297 bctr
299 .size _start,0b-_start
300 # undo '.section text'.
301 .previous
304 /* The idea here is that to conform to the ABI, we are supposed to try
305 to load dynamic objects between 0x10000 (we actually use 0x40000 as
306 the lower bound, to increase the chance of a memory reference from
307 a null pointer giving a segfault) and the program's load address.
308 Regrettably, in this code we can't find the program's load address,
309 so we punt and choose 0x01800000, which is below the ABI's
310 recommended default, and what GNU ld currently chooses. We only use
311 the address as a preference for mmap, so if we get it wrong the
312 worst that happens is that it gets mapped somewhere else.
314 FIXME: Unfortunately, 'somewhere else' is probably right after the
315 program's break, which causes malloc to fail. We really need more
316 information here about the way memory is mapped. */
318 #define ELF_PREFERRED_ADDRESS_DATA \
319 static ElfW(Addr) _dl_preferred_address = 1
321 #define ELF_PREFERRED_ADDRESS(loader, maplength, mapstartpref) \
322 ( { \
323 ElfW(Addr) prefd; \
324 if (mapstartpref != 0 && _dl_preferred_address == 1) \
325 _dl_preferred_address = mapstartpref; \
326 if (mapstartpref != 0) \
327 prefd = mapstartpref; \
328 else if (_dl_preferred_address == 1) \
329 prefd = _dl_preferred_address = \
330 (0x01800000 - maplength - 0x10000) & \
331 ~(_dl_pagesize - 1); \
332 else if (_dl_preferred_address < maplength + 0x50000) \
333 prefd = 0; \
334 else \
335 prefd = _dl_preferred_address = \
336 ((_dl_preferred_address - maplength - 0x10000) \
337 & ~(_dl_pagesize - 1)); \
338 prefd; \
341 #define ELF_FIXED_ADDRESS(loader, mapstart) \
342 ( { \
343 if (mapstart != 0 && _dl_preferred_address == 1) \
344 _dl_preferred_address = mapstart; \
347 /* We require the address of the PLT entry returned from fixup, not
348 the first word of the PLT entry. */
349 #define ELF_FIXUP_RETURNS_ADDRESS 1
351 /* Nonzero iff TYPE should not be allowed to resolve to one of
352 the main executable's symbols, as for a COPY reloc. */
353 #define elf_machine_lookup_noexec_p(type) ((type) == R_PPC_COPY)
355 /* Nonzero iff TYPE describes relocation of a PLT entry, so
356 PLT entries should not be allowed to define the value. */
357 /* We never want to use a PLT entry as the destination of a
358 reloc, when what is being relocated is a branch. This is
359 partly for efficiency, but mostly so we avoid loops. */
360 #define elf_machine_lookup_noplt_p(type) ((type) == R_PPC_REL24 || \
361 (type) == R_PPC_ADDR24 || \
362 (type) == R_PPC_JMP_SLOT)
364 /* A reloc type used for ld.so cmdline arg lookups to reject PLT entries. */
365 #define ELF_MACHINE_RELOC_NOPLT R_PPC_JMP_SLOT
367 /* Nonzero iff TYPE describes relocation of a PLT entry, so
368 PLT entries should not be allowed to define the value. */
369 #define elf_machine_pltrel_p(type) ((type) == R_PPC_JMP_SLOT)
371 /* Set up the loaded object described by L so its unrelocated PLT
372 entries will jump to the on-demand fixup code in dl-runtime.c.
373 Also install a small trampoline to be used by entries that have
374 been relocated to an address too far away for a single branch. */
376 /* A PLT entry does one of three things:
377 (i) Jumps to the actual routine. Such entries are set up above, in
378 elf_machine_rela.
380 (ii) Jumps to the actual routine via glue at the start of the PLT.
381 We do this by putting the address of the routine in space
382 allocated at the end of the PLT, and when the PLT entry is
383 called we load the offset of that word (from the start of the
384 space) into r11, then call the glue, which loads the word and
385 branches to that address. These entries are set up in
386 elf_machine_rela, but the glue is set up here.
388 (iii) Loads the index of this PLT entry (we count the double-size
389 entries as one entry for this purpose) into r11, then
390 branches to code at the start of the PLT. This code then
391 calls `fixup', in dl-runtime.c, via the glue in the macro
392 ELF_MACHINE_RUNTIME_TRAMPOLINE, which resets the PLT entry to
393 be one of the above two types. These entries are set up here. */
394 static inline int
395 elf_machine_runtime_setup (struct link_map *map, int lazy, int profile)
397 if (map->l_info[DT_JMPREL])
399 int i;
400 /* Fill in the PLT. Its initial contents are directed to a
401 function earlier in the PLT which arranges for the dynamic
402 linker to be called back. */
403 Elf32_Word *plt = (Elf32_Word *) ((char *) map->l_addr
404 + map->l_info[DT_PLTGOT]->d_un.d_val);
405 Elf32_Word num_plt_entries = (map->l_info[DT_PLTRELSZ]->d_un.d_val
406 / sizeof (Elf32_Rela));
407 Elf32_Word rel_offset_words = PLT_DATA_START_WORDS (num_plt_entries);
408 extern void _dl_runtime_resolve (void);
409 Elf32_Word size_modified;
411 if (lazy)
412 for (i = 0; i < num_plt_entries; i++)
414 Elf32_Word offset = PLT_ENTRY_START_WORDS (i);
416 if (i >= PLT_DOUBLE_SIZE)
418 plt[offset ] = OPCODE_LI (11, i * 4);
419 plt[offset+1] = OPCODE_ADDIS (11, 11, (i * 4 + 0x8000) >> 16);
420 plt[offset+2] = OPCODE_B (-(4 * (offset + 2)));
422 else
424 plt[offset ] = OPCODE_LI (11, i * 4);
425 plt[offset+1] = OPCODE_B (-(4 * (offset + 1)));
429 /* Multiply index of entry by 3 (in r11). */
430 plt[0] = OPCODE_SLWI (12, 11, 1);
431 plt[1] = OPCODE_ADD (11, 12, 11);
432 if ((Elf32_Word) (char *) _dl_runtime_resolve <= 0x01fffffc ||
433 (Elf32_Word) (char *) _dl_runtime_resolve >= 0xfe000000)
435 /* Load address of link map in r12. */
436 plt[2] = OPCODE_LI (12, (Elf32_Word) (char *) map);
437 plt[3] = OPCODE_ADDIS (12, 12, (((Elf32_Word) (char *) map
438 + 0x8000) >> 16));
440 /* Call _dl_runtime_resolve. */
441 plt[4] = OPCODE_BA ((Elf32_Word) (char *) _dl_runtime_resolve);
443 else
445 /* Get address of _dl_runtime_resolve in CTR. */
446 plt[2] = OPCODE_LI (12, (Elf32_Word) (char *) _dl_runtime_resolve);
447 plt[3] = OPCODE_ADDIS (12, 12, ((((Elf32_Word) (char *)
448 _dl_runtime_resolve)
449 + 0x8000) >> 16));
450 plt[4] = OPCODE_MTCTR (12);
452 /* Load address of link map in r12. */
453 plt[5] = OPCODE_LI (12, (Elf32_Word) (char *) map);
454 plt[6] = OPCODE_ADDIS (12, 12, (((Elf32_Word) (char *) map
455 + 0x8000) >> 16));
457 /* Call _dl_runtime_resolve. */
458 plt[7] = OPCODE_BCTR ();
462 /* Convert the index in r11 into an actual address, and get the
463 word at that address. */
464 plt[PLT_LONGBRANCH_ENTRY_WORDS] =
465 OPCODE_ADDIS (11, 11, (((Elf32_Word) (char*) (plt + rel_offset_words)
466 + 0x8000) >> 16));
467 plt[PLT_LONGBRANCH_ENTRY_WORDS+1] =
468 OPCODE_LWZ (11, (Elf32_Word) (char*) (plt+rel_offset_words), 11);
470 /* Call the procedure at that address. */
471 plt[PLT_LONGBRANCH_ENTRY_WORDS+2] = OPCODE_MTCTR (11);
472 plt[PLT_LONGBRANCH_ENTRY_WORDS+3] = OPCODE_BCTR ();
475 /* Now, we've modified code (quite a lot of code, possibly). We
476 need to write the changes from the data cache to a
477 second-level unified cache, then make sure that stale data in
478 the instruction cache is removed. (In a multiprocessor
479 system, the effect is more complex.)
481 Assumes the cache line size is at least 32 bytes, or at least
482 that dcbst and icbi apply to 32-byte lines. At present, all
483 PowerPC processors have line sizes of exactly 32 bytes. */
485 size_modified = lazy ? rel_offset_words : PLT_INITIAL_ENTRY_WORDS;
486 for (i = 0; i < size_modified; i+=8)
487 PPC_DCBST (plt + i);
488 PPC_SYNC;
489 for (i = 0; i < size_modified; i+=8)
490 PPC_ICBI (plt + i);
491 PPC_ISYNC;
494 return lazy;
497 static inline void
498 elf_machine_lazy_rel (struct link_map *map, const Elf32_Rela *reloc)
500 assert (ELF32_R_TYPE (reloc->r_info) == R_PPC_JMP_SLOT);
501 /* elf_machine_runtime_setup handles this. */
504 #endif /* dl_machine_h */
506 #ifdef RESOLVE
508 /* Perform the relocation specified by RELOC and SYM (which is fully resolved).
509 LOADADDR is the load address of the object; INFO is an array indexed
510 by DT_* of the .dynamic section info. */
512 static inline void
513 elf_machine_rela (struct link_map *map, const Elf32_Rela *reloc,
514 const Elf32_Sym *sym, const struct r_found_version *version,
515 Elf32_Addr *const reloc_addr)
517 #ifndef RTLD_BOOTSTRAP
518 const Elf32_Sym *const refsym = sym;
519 #endif
520 Elf32_Word loadbase, finaladdr;
521 const int rinfo = ELF32_R_TYPE (reloc->r_info);
522 extern char **_dl_argv;
524 if (rinfo == R_PPC_NONE)
525 return;
527 assert (sym != NULL);
528 /* The condition on the next two lines is a hack around a bug in Solaris
529 tools on Sparc. It's not clear whether it should really be here at all,
530 but if not the binutils need to be changed. */
531 if ((sym->st_shndx != SHN_UNDEF
532 && ELF32_ST_BIND (sym->st_info) == STB_LOCAL)
533 || rinfo == R_PPC_RELATIVE)
535 /* Has already been relocated. */
536 loadbase = map->l_addr;
537 finaladdr = loadbase + reloc->r_addend;
539 else
541 loadbase = (Elf32_Word) (char *) (RESOLVE (&sym, version,
542 ELF32_R_TYPE(reloc->r_info)));
543 if (sym == NULL)
545 /* Weak symbol that wasn't actually defined anywhere. */
546 assert(loadbase == 0);
547 finaladdr = reloc->r_addend;
549 else
550 finaladdr = (loadbase + (Elf32_Word) (char *) sym->st_value
551 + reloc->r_addend);
554 /* This is an if/else if chain because GCC 2.7.2.[012] turns case
555 statements into non-PIC table lookups. When a later version
556 comes out that fixes this, this should be changed. */
557 if (rinfo == R_PPC_UADDR32 ||
558 rinfo == R_PPC_GLOB_DAT ||
559 rinfo == R_PPC_ADDR32 ||
560 rinfo == R_PPC_RELATIVE)
562 *reloc_addr = finaladdr;
564 else if (rinfo == R_PPC_ADDR16_LO)
566 *(Elf32_Half*) reloc_addr = finaladdr;
568 else if (rinfo == R_PPC_ADDR16_HI)
570 *(Elf32_Half*) reloc_addr = finaladdr >> 16;
572 else if (rinfo == R_PPC_ADDR16_HA)
574 *(Elf32_Half*) reloc_addr = (finaladdr + 0x8000) >> 16;
576 #ifndef RTLD_BOOTSTRAP
577 else if (rinfo == R_PPC_REL24)
579 Elf32_Sword delta = finaladdr - (Elf32_Word) (char *) reloc_addr;
580 if (delta << 6 >> 6 != delta)
582 _dl_signal_error(0, map->l_name,
583 "R_PPC_REL24 relocation out of range");
585 *reloc_addr = *reloc_addr & 0xfc000003 | delta & 0x3fffffc;
587 else if (rinfo == R_PPC_ADDR24)
589 if (finaladdr << 6 >> 6 != finaladdr)
591 _dl_signal_error(0, map->l_name,
592 "R_PPC_ADDR24 relocation out of range");
594 *reloc_addr = *reloc_addr & 0xfc000003 | finaladdr & 0x3fffffc;
596 else if (rinfo == R_PPC_COPY)
598 if (sym == NULL)
599 /* This can happen in trace mode when an object could not be
600 found. */
601 return;
602 if (sym->st_size > refsym->st_size
603 || (_dl_verbose && sym->st_size < refsym->st_size))
605 const char *strtab;
607 strtab = ((void *) map->l_addr
608 + map->l_info[DT_STRTAB]->d_un.d_ptr);
609 _dl_sysdep_error (_dl_argv[0] ?: "<program name unknown>",
610 ": Symbol `", strtab + refsym->st_name,
611 "' has different size in shared object, "
612 "consider re-linking\n", NULL);
614 memcpy (reloc_addr, (char *) finaladdr, MIN (sym->st_size,
615 refsym->st_size));
617 #endif
618 else if (rinfo == R_PPC_REL32)
620 *reloc_addr = finaladdr - (Elf32_Word) (char *) reloc_addr;
622 else if (rinfo == R_PPC_JMP_SLOT)
624 Elf32_Sword delta = finaladdr - (Elf32_Word) (char *) reloc_addr;
625 if (delta << 6 >> 6 == delta)
626 *reloc_addr = OPCODE_B (delta);
627 else if (finaladdr <= 0x01fffffc || finaladdr >= 0xfe000000)
628 *reloc_addr = OPCODE_BA (finaladdr);
629 else
631 Elf32_Word *plt;
632 Elf32_Word index;
634 plt = (Elf32_Word *)((char *)map->l_addr
635 + map->l_info[DT_PLTGOT]->d_un.d_val);
636 index = (reloc_addr - plt - PLT_INITIAL_ENTRY_WORDS)/2;
637 if (index >= PLT_DOUBLE_SIZE)
639 /* Slots greater than or equal to 2^13 have 4 words available
640 instead of two. */
641 /* FIXME: There are some possible race conditions in this code,
642 when called from 'fixup'.
644 1) Suppose that a lazy PLT entry is executing, a
645 context switch between threads (or a signal) occurs,
646 and the new thread or signal handler calls the same
647 lazy PLT entry. Then the PLT entry would be changed
648 while it's being run, which will cause a segfault
649 (almost always).
651 2) Suppose the reverse: that a lazy PLT entry is
652 being updated, a context switch occurs, and the new
653 code calls the lazy PLT entry that is being updated.
654 Then the half-fixed PLT entry will be executed, which
655 will also almost always cause a segfault.
657 These problems don't happen with the 2-word entries, because
658 only one of the two instructions are changed when a lazy
659 entry is retargeted at the actual PLT entry; the li
660 instruction stays the same (we have to update it anyway,
661 because we might not be updating a lazy PLT entry). */
662 reloc_addr[0] = OPCODE_LI (11, finaladdr);
663 reloc_addr[1] = OPCODE_ADDIS (11, 11, finaladdr + 0x8000 >> 16);
664 reloc_addr[2] = OPCODE_MTCTR (11);
665 reloc_addr[3] = OPCODE_BCTR ();
667 else
669 Elf32_Word num_plt_entries;
671 num_plt_entries = (map->l_info[DT_PLTRELSZ]->d_un.d_val
672 / sizeof(Elf32_Rela));
674 plt[index+PLT_DATA_START_WORDS (num_plt_entries)] = finaladdr;
675 reloc_addr[0] = OPCODE_LI (11, index*4);
676 reloc_addr[1] =
677 OPCODE_B (-(4*(index*2
679 - PLT_LONGBRANCH_ENTRY_WORDS
680 + PLT_INITIAL_ENTRY_WORDS)));
683 MODIFIED_CODE (reloc_addr);
685 else
687 #ifdef RTLD_BOOTSTRAP
688 PPC_DIE; /* There is no point calling _dl_sysdep_error, it
689 almost certainly hasn't been relocated properly. */
690 #else
691 _dl_sysdep_error (_dl_argv[0] ?: "<program name unknown>",
692 ": Unknown relocation type\n", NULL);
693 #endif
696 if (rinfo == R_PPC_ADDR16_LO ||
697 rinfo == R_PPC_ADDR16_HI ||
698 rinfo == R_PPC_ADDR16_HA ||
699 rinfo == R_PPC_REL24 ||
700 rinfo == R_PPC_ADDR24)
701 MODIFIED_CODE_NOQUEUE (reloc_addr);
704 #define ELF_MACHINE_NO_REL 1
706 #endif