* config/tc-mips.c (mips_fix_loongson2f, mips_fix_loongson2f_nop,
[binutils/dougsmingw.git] / bfd / elf32-ppc.c
blob6e4cbc1238ebd0a70dfc36a800b04e77776ab2d7
1 /* PowerPC-specific support for 32-bit ELF
2 Copyright 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
3 2004, 2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
4 Written by Ian Lance Taylor, Cygnus Support.
6 This file is part of BFD, the Binary File Descriptor library.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the
20 Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
21 Boston, MA 02110-1301, USA. */
24 /* This file is based on a preliminary PowerPC ELF ABI. The
25 information may not match the final PowerPC ELF ABI. It includes
26 suggestions from the in-progress Embedded PowerPC ABI, and that
27 information may also not match. */
29 #include "sysdep.h"
30 #include <stdarg.h>
31 #include "bfd.h"
32 #include "bfdlink.h"
33 #include "libbfd.h"
34 #include "elf-bfd.h"
35 #include "elf/ppc.h"
36 #include "elf32-ppc.h"
37 #include "elf-vxworks.h"
39 /* RELA relocations are used here. */
41 static bfd_reloc_status_type ppc_elf_addr16_ha_reloc
42 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
43 static bfd_reloc_status_type ppc_elf_unhandled_reloc
44 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
46 /* Branch prediction bit for branch taken relocs. */
47 #define BRANCH_PREDICT_BIT 0x200000
48 /* Mask to set RA in memory instructions. */
49 #define RA_REGISTER_MASK 0x001f0000
50 /* Value to shift register by to insert RA. */
51 #define RA_REGISTER_SHIFT 16
53 /* The name of the dynamic interpreter. This is put in the .interp
54 section. */
55 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
57 /* For old-style PLT. */
58 /* The number of single-slot PLT entries (the rest use two slots). */
59 #define PLT_NUM_SINGLE_ENTRIES 8192
61 /* For new-style .glink and .plt. */
62 #define GLINK_PLTRESOLVE 16*4
63 #define GLINK_ENTRY_SIZE 4*4
64 #define TLS_GET_ADDR_GLINK_SIZE 12*4
66 /* VxWorks uses its own plt layout, filled in by the static linker. */
68 /* The standard VxWorks PLT entry. */
69 #define VXWORKS_PLT_ENTRY_SIZE 32
70 static const bfd_vma ppc_elf_vxworks_plt_entry
71 [VXWORKS_PLT_ENTRY_SIZE / 4] =
73 0x3d800000, /* lis r12,0 */
74 0x818c0000, /* lwz r12,0(r12) */
75 0x7d8903a6, /* mtctr r12 */
76 0x4e800420, /* bctr */
77 0x39600000, /* li r11,0 */
78 0x48000000, /* b 14 <.PLT0resolve+0x4> */
79 0x60000000, /* nop */
80 0x60000000, /* nop */
82 static const bfd_vma ppc_elf_vxworks_pic_plt_entry
83 [VXWORKS_PLT_ENTRY_SIZE / 4] =
85 0x3d9e0000, /* addis r12,r30,0 */
86 0x818c0000, /* lwz r12,0(r12) */
87 0x7d8903a6, /* mtctr r12 */
88 0x4e800420, /* bctr */
89 0x39600000, /* li r11,0 */
90 0x48000000, /* b 14 <.PLT0resolve+0x4> 14: R_PPC_REL24 .PLTresolve */
91 0x60000000, /* nop */
92 0x60000000, /* nop */
95 /* The initial VxWorks PLT entry. */
96 #define VXWORKS_PLT_INITIAL_ENTRY_SIZE 32
97 static const bfd_vma ppc_elf_vxworks_plt0_entry
98 [VXWORKS_PLT_INITIAL_ENTRY_SIZE / 4] =
100 0x3d800000, /* lis r12,0 */
101 0x398c0000, /* addi r12,r12,0 */
102 0x800c0008, /* lwz r0,8(r12) */
103 0x7c0903a6, /* mtctr r0 */
104 0x818c0004, /* lwz r12,4(r12) */
105 0x4e800420, /* bctr */
106 0x60000000, /* nop */
107 0x60000000, /* nop */
109 static const bfd_vma ppc_elf_vxworks_pic_plt0_entry
110 [VXWORKS_PLT_INITIAL_ENTRY_SIZE / 4] =
112 0x819e0008, /* lwz r12,8(r30) */
113 0x7d8903a6, /* mtctr r12 */
114 0x819e0004, /* lwz r12,4(r30) */
115 0x4e800420, /* bctr */
116 0x60000000, /* nop */
117 0x60000000, /* nop */
118 0x60000000, /* nop */
119 0x60000000, /* nop */
122 /* For executables, we have some additional relocations in
123 .rela.plt.unloaded, for the kernel loader. */
125 /* The number of non-JMP_SLOT relocations per PLT0 slot. */
126 #define VXWORKS_PLT_NON_JMP_SLOT_RELOCS 3
127 /* The number of relocations in the PLTResolve slot. */
128 #define VXWORKS_PLTRESOLVE_RELOCS 2
129 /* The number of relocations in the PLTResolve slot when when creating
130 a shared library. */
131 #define VXWORKS_PLTRESOLVE_RELOCS_SHLIB 0
133 /* Some instructions. */
134 #define ADDIS_11_11 0x3d6b0000
135 #define ADDIS_11_30 0x3d7e0000
136 #define ADDIS_12_12 0x3d8c0000
137 #define ADDI_11_11 0x396b0000
138 #define ADD_0_11_11 0x7c0b5a14
139 #define ADD_3_12_2 0x7c6c1214
140 #define ADD_11_0_11 0x7d605a14
141 #define B 0x48000000
142 #define BCL_20_31 0x429f0005
143 #define BCTR 0x4e800420
144 #define BEQLR 0x4d820020
145 #define CMPWI_11_0 0x2c0b0000
146 #define LIS_11 0x3d600000
147 #define LIS_12 0x3d800000
148 #define LWZU_0_12 0x840c0000
149 #define LWZ_0_12 0x800c0000
150 #define LWZ_11_3 0x81630000
151 #define LWZ_11_11 0x816b0000
152 #define LWZ_11_30 0x817e0000
153 #define LWZ_12_3 0x81830000
154 #define LWZ_12_12 0x818c0000
155 #define MR_0_3 0x7c601b78
156 #define MR_3_0 0x7c030378
157 #define MFLR_0 0x7c0802a6
158 #define MFLR_12 0x7d8802a6
159 #define MTCTR_0 0x7c0903a6
160 #define MTCTR_11 0x7d6903a6
161 #define MTLR_0 0x7c0803a6
162 #define NOP 0x60000000
163 #define SUB_11_11_12 0x7d6c5850
165 /* Offset of tp and dtp pointers from start of TLS block. */
166 #define TP_OFFSET 0x7000
167 #define DTP_OFFSET 0x8000
169 /* The value of a defined global symbol. */
170 #define SYM_VAL(SYM) \
171 ((SYM)->root.u.def.section->output_section->vma \
172 + (SYM)->root.u.def.section->output_offset \
173 + (SYM)->root.u.def.value)
175 static reloc_howto_type *ppc_elf_howto_table[R_PPC_max];
177 static reloc_howto_type ppc_elf_howto_raw[] = {
178 /* This reloc does nothing. */
179 HOWTO (R_PPC_NONE, /* type */
180 0, /* rightshift */
181 2, /* size (0 = byte, 1 = short, 2 = long) */
182 32, /* bitsize */
183 FALSE, /* pc_relative */
184 0, /* bitpos */
185 complain_overflow_bitfield, /* complain_on_overflow */
186 bfd_elf_generic_reloc, /* special_function */
187 "R_PPC_NONE", /* name */
188 FALSE, /* partial_inplace */
189 0, /* src_mask */
190 0, /* dst_mask */
191 FALSE), /* pcrel_offset */
193 /* A standard 32 bit relocation. */
194 HOWTO (R_PPC_ADDR32, /* type */
195 0, /* rightshift */
196 2, /* size (0 = byte, 1 = short, 2 = long) */
197 32, /* bitsize */
198 FALSE, /* pc_relative */
199 0, /* bitpos */
200 complain_overflow_bitfield, /* complain_on_overflow */
201 bfd_elf_generic_reloc, /* special_function */
202 "R_PPC_ADDR32", /* name */
203 FALSE, /* partial_inplace */
204 0, /* src_mask */
205 0xffffffff, /* dst_mask */
206 FALSE), /* pcrel_offset */
208 /* An absolute 26 bit branch; the lower two bits must be zero.
209 FIXME: we don't check that, we just clear them. */
210 HOWTO (R_PPC_ADDR24, /* type */
211 0, /* rightshift */
212 2, /* size (0 = byte, 1 = short, 2 = long) */
213 26, /* bitsize */
214 FALSE, /* pc_relative */
215 0, /* bitpos */
216 complain_overflow_bitfield, /* complain_on_overflow */
217 bfd_elf_generic_reloc, /* special_function */
218 "R_PPC_ADDR24", /* name */
219 FALSE, /* partial_inplace */
220 0, /* src_mask */
221 0x3fffffc, /* dst_mask */
222 FALSE), /* pcrel_offset */
224 /* A standard 16 bit relocation. */
225 HOWTO (R_PPC_ADDR16, /* type */
226 0, /* rightshift */
227 1, /* size (0 = byte, 1 = short, 2 = long) */
228 16, /* bitsize */
229 FALSE, /* pc_relative */
230 0, /* bitpos */
231 complain_overflow_bitfield, /* complain_on_overflow */
232 bfd_elf_generic_reloc, /* special_function */
233 "R_PPC_ADDR16", /* name */
234 FALSE, /* partial_inplace */
235 0, /* src_mask */
236 0xffff, /* dst_mask */
237 FALSE), /* pcrel_offset */
239 /* A 16 bit relocation without overflow. */
240 HOWTO (R_PPC_ADDR16_LO, /* type */
241 0, /* rightshift */
242 1, /* size (0 = byte, 1 = short, 2 = long) */
243 16, /* bitsize */
244 FALSE, /* pc_relative */
245 0, /* bitpos */
246 complain_overflow_dont,/* complain_on_overflow */
247 bfd_elf_generic_reloc, /* special_function */
248 "R_PPC_ADDR16_LO", /* name */
249 FALSE, /* partial_inplace */
250 0, /* src_mask */
251 0xffff, /* dst_mask */
252 FALSE), /* pcrel_offset */
254 /* The high order 16 bits of an address. */
255 HOWTO (R_PPC_ADDR16_HI, /* type */
256 16, /* rightshift */
257 1, /* size (0 = byte, 1 = short, 2 = long) */
258 16, /* bitsize */
259 FALSE, /* pc_relative */
260 0, /* bitpos */
261 complain_overflow_dont, /* complain_on_overflow */
262 bfd_elf_generic_reloc, /* special_function */
263 "R_PPC_ADDR16_HI", /* name */
264 FALSE, /* partial_inplace */
265 0, /* src_mask */
266 0xffff, /* dst_mask */
267 FALSE), /* pcrel_offset */
269 /* The high order 16 bits of an address, plus 1 if the contents of
270 the low 16 bits, treated as a signed number, is negative. */
271 HOWTO (R_PPC_ADDR16_HA, /* type */
272 16, /* rightshift */
273 1, /* size (0 = byte, 1 = short, 2 = long) */
274 16, /* bitsize */
275 FALSE, /* pc_relative */
276 0, /* bitpos */
277 complain_overflow_dont, /* complain_on_overflow */
278 ppc_elf_addr16_ha_reloc, /* special_function */
279 "R_PPC_ADDR16_HA", /* name */
280 FALSE, /* partial_inplace */
281 0, /* src_mask */
282 0xffff, /* dst_mask */
283 FALSE), /* pcrel_offset */
285 /* An absolute 16 bit branch; the lower two bits must be zero.
286 FIXME: we don't check that, we just clear them. */
287 HOWTO (R_PPC_ADDR14, /* type */
288 0, /* rightshift */
289 2, /* size (0 = byte, 1 = short, 2 = long) */
290 16, /* bitsize */
291 FALSE, /* pc_relative */
292 0, /* bitpos */
293 complain_overflow_bitfield, /* complain_on_overflow */
294 bfd_elf_generic_reloc, /* special_function */
295 "R_PPC_ADDR14", /* name */
296 FALSE, /* partial_inplace */
297 0, /* src_mask */
298 0xfffc, /* dst_mask */
299 FALSE), /* pcrel_offset */
301 /* An absolute 16 bit branch, for which bit 10 should be set to
302 indicate that the branch is expected to be taken. The lower two
303 bits must be zero. */
304 HOWTO (R_PPC_ADDR14_BRTAKEN, /* type */
305 0, /* rightshift */
306 2, /* size (0 = byte, 1 = short, 2 = long) */
307 16, /* bitsize */
308 FALSE, /* pc_relative */
309 0, /* bitpos */
310 complain_overflow_bitfield, /* complain_on_overflow */
311 bfd_elf_generic_reloc, /* special_function */
312 "R_PPC_ADDR14_BRTAKEN",/* name */
313 FALSE, /* partial_inplace */
314 0, /* src_mask */
315 0xfffc, /* dst_mask */
316 FALSE), /* pcrel_offset */
318 /* An absolute 16 bit branch, for which bit 10 should be set to
319 indicate that the branch is not expected to be taken. The lower
320 two bits must be zero. */
321 HOWTO (R_PPC_ADDR14_BRNTAKEN, /* type */
322 0, /* rightshift */
323 2, /* size (0 = byte, 1 = short, 2 = long) */
324 16, /* bitsize */
325 FALSE, /* pc_relative */
326 0, /* bitpos */
327 complain_overflow_bitfield, /* complain_on_overflow */
328 bfd_elf_generic_reloc, /* special_function */
329 "R_PPC_ADDR14_BRNTAKEN",/* name */
330 FALSE, /* partial_inplace */
331 0, /* src_mask */
332 0xfffc, /* dst_mask */
333 FALSE), /* pcrel_offset */
335 /* A relative 26 bit branch; the lower two bits must be zero. */
336 HOWTO (R_PPC_REL24, /* type */
337 0, /* rightshift */
338 2, /* size (0 = byte, 1 = short, 2 = long) */
339 26, /* bitsize */
340 TRUE, /* pc_relative */
341 0, /* bitpos */
342 complain_overflow_signed, /* complain_on_overflow */
343 bfd_elf_generic_reloc, /* special_function */
344 "R_PPC_REL24", /* name */
345 FALSE, /* partial_inplace */
346 0, /* src_mask */
347 0x3fffffc, /* dst_mask */
348 TRUE), /* pcrel_offset */
350 /* A relative 16 bit branch; the lower two bits must be zero. */
351 HOWTO (R_PPC_REL14, /* type */
352 0, /* rightshift */
353 2, /* size (0 = byte, 1 = short, 2 = long) */
354 16, /* bitsize */
355 TRUE, /* pc_relative */
356 0, /* bitpos */
357 complain_overflow_signed, /* complain_on_overflow */
358 bfd_elf_generic_reloc, /* special_function */
359 "R_PPC_REL14", /* name */
360 FALSE, /* partial_inplace */
361 0, /* src_mask */
362 0xfffc, /* dst_mask */
363 TRUE), /* pcrel_offset */
365 /* A relative 16 bit branch. Bit 10 should be set to indicate that
366 the branch is expected to be taken. The lower two bits must be
367 zero. */
368 HOWTO (R_PPC_REL14_BRTAKEN, /* type */
369 0, /* rightshift */
370 2, /* size (0 = byte, 1 = short, 2 = long) */
371 16, /* bitsize */
372 TRUE, /* pc_relative */
373 0, /* bitpos */
374 complain_overflow_signed, /* complain_on_overflow */
375 bfd_elf_generic_reloc, /* special_function */
376 "R_PPC_REL14_BRTAKEN", /* name */
377 FALSE, /* partial_inplace */
378 0, /* src_mask */
379 0xfffc, /* dst_mask */
380 TRUE), /* pcrel_offset */
382 /* A relative 16 bit branch. Bit 10 should be set to indicate that
383 the branch is not expected to be taken. The lower two bits must
384 be zero. */
385 HOWTO (R_PPC_REL14_BRNTAKEN, /* type */
386 0, /* rightshift */
387 2, /* size (0 = byte, 1 = short, 2 = long) */
388 16, /* bitsize */
389 TRUE, /* pc_relative */
390 0, /* bitpos */
391 complain_overflow_signed, /* complain_on_overflow */
392 bfd_elf_generic_reloc, /* special_function */
393 "R_PPC_REL14_BRNTAKEN",/* name */
394 FALSE, /* partial_inplace */
395 0, /* src_mask */
396 0xfffc, /* dst_mask */
397 TRUE), /* pcrel_offset */
399 /* Like R_PPC_ADDR16, but referring to the GOT table entry for the
400 symbol. */
401 HOWTO (R_PPC_GOT16, /* type */
402 0, /* rightshift */
403 1, /* size (0 = byte, 1 = short, 2 = long) */
404 16, /* bitsize */
405 FALSE, /* pc_relative */
406 0, /* bitpos */
407 complain_overflow_signed, /* complain_on_overflow */
408 bfd_elf_generic_reloc, /* special_function */
409 "R_PPC_GOT16", /* name */
410 FALSE, /* partial_inplace */
411 0, /* src_mask */
412 0xffff, /* dst_mask */
413 FALSE), /* pcrel_offset */
415 /* Like R_PPC_ADDR16_LO, but referring to the GOT table entry for
416 the symbol. */
417 HOWTO (R_PPC_GOT16_LO, /* type */
418 0, /* rightshift */
419 1, /* size (0 = byte, 1 = short, 2 = long) */
420 16, /* bitsize */
421 FALSE, /* pc_relative */
422 0, /* bitpos */
423 complain_overflow_dont, /* complain_on_overflow */
424 bfd_elf_generic_reloc, /* special_function */
425 "R_PPC_GOT16_LO", /* name */
426 FALSE, /* partial_inplace */
427 0, /* src_mask */
428 0xffff, /* dst_mask */
429 FALSE), /* pcrel_offset */
431 /* Like R_PPC_ADDR16_HI, but referring to the GOT table entry for
432 the symbol. */
433 HOWTO (R_PPC_GOT16_HI, /* type */
434 16, /* rightshift */
435 1, /* size (0 = byte, 1 = short, 2 = long) */
436 16, /* bitsize */
437 FALSE, /* pc_relative */
438 0, /* bitpos */
439 complain_overflow_bitfield, /* complain_on_overflow */
440 bfd_elf_generic_reloc, /* special_function */
441 "R_PPC_GOT16_HI", /* name */
442 FALSE, /* partial_inplace */
443 0, /* src_mask */
444 0xffff, /* dst_mask */
445 FALSE), /* pcrel_offset */
447 /* Like R_PPC_ADDR16_HA, but referring to the GOT table entry for
448 the symbol. */
449 HOWTO (R_PPC_GOT16_HA, /* type */
450 16, /* rightshift */
451 1, /* size (0 = byte, 1 = short, 2 = long) */
452 16, /* bitsize */
453 FALSE, /* pc_relative */
454 0, /* bitpos */
455 complain_overflow_bitfield, /* complain_on_overflow */
456 ppc_elf_addr16_ha_reloc, /* special_function */
457 "R_PPC_GOT16_HA", /* name */
458 FALSE, /* partial_inplace */
459 0, /* src_mask */
460 0xffff, /* dst_mask */
461 FALSE), /* pcrel_offset */
463 /* Like R_PPC_REL24, but referring to the procedure linkage table
464 entry for the symbol. */
465 HOWTO (R_PPC_PLTREL24, /* type */
466 0, /* rightshift */
467 2, /* size (0 = byte, 1 = short, 2 = long) */
468 26, /* bitsize */
469 TRUE, /* pc_relative */
470 0, /* bitpos */
471 complain_overflow_signed, /* complain_on_overflow */
472 bfd_elf_generic_reloc, /* special_function */
473 "R_PPC_PLTREL24", /* name */
474 FALSE, /* partial_inplace */
475 0, /* src_mask */
476 0x3fffffc, /* dst_mask */
477 TRUE), /* pcrel_offset */
479 /* This is used only by the dynamic linker. The symbol should exist
480 both in the object being run and in some shared library. The
481 dynamic linker copies the data addressed by the symbol from the
482 shared library into the object, because the object being
483 run has to have the data at some particular address. */
484 HOWTO (R_PPC_COPY, /* type */
485 0, /* rightshift */
486 2, /* size (0 = byte, 1 = short, 2 = long) */
487 32, /* bitsize */
488 FALSE, /* pc_relative */
489 0, /* bitpos */
490 complain_overflow_bitfield, /* complain_on_overflow */
491 bfd_elf_generic_reloc, /* special_function */
492 "R_PPC_COPY", /* name */
493 FALSE, /* partial_inplace */
494 0, /* src_mask */
495 0, /* dst_mask */
496 FALSE), /* pcrel_offset */
498 /* Like R_PPC_ADDR32, but used when setting global offset table
499 entries. */
500 HOWTO (R_PPC_GLOB_DAT, /* type */
501 0, /* rightshift */
502 2, /* size (0 = byte, 1 = short, 2 = long) */
503 32, /* bitsize */
504 FALSE, /* pc_relative */
505 0, /* bitpos */
506 complain_overflow_bitfield, /* complain_on_overflow */
507 bfd_elf_generic_reloc, /* special_function */
508 "R_PPC_GLOB_DAT", /* name */
509 FALSE, /* partial_inplace */
510 0, /* src_mask */
511 0xffffffff, /* dst_mask */
512 FALSE), /* pcrel_offset */
514 /* Marks a procedure linkage table entry for a symbol. */
515 HOWTO (R_PPC_JMP_SLOT, /* type */
516 0, /* rightshift */
517 2, /* size (0 = byte, 1 = short, 2 = long) */
518 32, /* bitsize */
519 FALSE, /* pc_relative */
520 0, /* bitpos */
521 complain_overflow_bitfield, /* complain_on_overflow */
522 bfd_elf_generic_reloc, /* special_function */
523 "R_PPC_JMP_SLOT", /* name */
524 FALSE, /* partial_inplace */
525 0, /* src_mask */
526 0, /* dst_mask */
527 FALSE), /* pcrel_offset */
529 /* Used only by the dynamic linker. When the object is run, this
530 longword is set to the load address of the object, plus the
531 addend. */
532 HOWTO (R_PPC_RELATIVE, /* type */
533 0, /* rightshift */
534 2, /* size (0 = byte, 1 = short, 2 = long) */
535 32, /* bitsize */
536 FALSE, /* pc_relative */
537 0, /* bitpos */
538 complain_overflow_bitfield, /* complain_on_overflow */
539 bfd_elf_generic_reloc, /* special_function */
540 "R_PPC_RELATIVE", /* name */
541 FALSE, /* partial_inplace */
542 0, /* src_mask */
543 0xffffffff, /* dst_mask */
544 FALSE), /* pcrel_offset */
546 /* Like R_PPC_REL24, but uses the value of the symbol within the
547 object rather than the final value. Normally used for
548 _GLOBAL_OFFSET_TABLE_. */
549 HOWTO (R_PPC_LOCAL24PC, /* type */
550 0, /* rightshift */
551 2, /* size (0 = byte, 1 = short, 2 = long) */
552 26, /* bitsize */
553 TRUE, /* pc_relative */
554 0, /* bitpos */
555 complain_overflow_signed, /* complain_on_overflow */
556 bfd_elf_generic_reloc, /* special_function */
557 "R_PPC_LOCAL24PC", /* name */
558 FALSE, /* partial_inplace */
559 0, /* src_mask */
560 0x3fffffc, /* dst_mask */
561 TRUE), /* pcrel_offset */
563 /* Like R_PPC_ADDR32, but may be unaligned. */
564 HOWTO (R_PPC_UADDR32, /* type */
565 0, /* rightshift */
566 2, /* size (0 = byte, 1 = short, 2 = long) */
567 32, /* bitsize */
568 FALSE, /* pc_relative */
569 0, /* bitpos */
570 complain_overflow_bitfield, /* complain_on_overflow */
571 bfd_elf_generic_reloc, /* special_function */
572 "R_PPC_UADDR32", /* name */
573 FALSE, /* partial_inplace */
574 0, /* src_mask */
575 0xffffffff, /* dst_mask */
576 FALSE), /* pcrel_offset */
578 /* Like R_PPC_ADDR16, but may be unaligned. */
579 HOWTO (R_PPC_UADDR16, /* type */
580 0, /* rightshift */
581 1, /* size (0 = byte, 1 = short, 2 = long) */
582 16, /* bitsize */
583 FALSE, /* pc_relative */
584 0, /* bitpos */
585 complain_overflow_bitfield, /* complain_on_overflow */
586 bfd_elf_generic_reloc, /* special_function */
587 "R_PPC_UADDR16", /* name */
588 FALSE, /* partial_inplace */
589 0, /* src_mask */
590 0xffff, /* dst_mask */
591 FALSE), /* pcrel_offset */
593 /* 32-bit PC relative */
594 HOWTO (R_PPC_REL32, /* type */
595 0, /* rightshift */
596 2, /* size (0 = byte, 1 = short, 2 = long) */
597 32, /* bitsize */
598 TRUE, /* pc_relative */
599 0, /* bitpos */
600 complain_overflow_bitfield, /* complain_on_overflow */
601 bfd_elf_generic_reloc, /* special_function */
602 "R_PPC_REL32", /* name */
603 FALSE, /* partial_inplace */
604 0, /* src_mask */
605 0xffffffff, /* dst_mask */
606 TRUE), /* pcrel_offset */
608 /* 32-bit relocation to the symbol's procedure linkage table.
609 FIXME: not supported. */
610 HOWTO (R_PPC_PLT32, /* type */
611 0, /* rightshift */
612 2, /* size (0 = byte, 1 = short, 2 = long) */
613 32, /* bitsize */
614 FALSE, /* pc_relative */
615 0, /* bitpos */
616 complain_overflow_bitfield, /* complain_on_overflow */
617 bfd_elf_generic_reloc, /* special_function */
618 "R_PPC_PLT32", /* name */
619 FALSE, /* partial_inplace */
620 0, /* src_mask */
621 0, /* dst_mask */
622 FALSE), /* pcrel_offset */
624 /* 32-bit PC relative relocation to the symbol's procedure linkage table.
625 FIXME: not supported. */
626 HOWTO (R_PPC_PLTREL32, /* type */
627 0, /* rightshift */
628 2, /* size (0 = byte, 1 = short, 2 = long) */
629 32, /* bitsize */
630 TRUE, /* pc_relative */
631 0, /* bitpos */
632 complain_overflow_bitfield, /* complain_on_overflow */
633 bfd_elf_generic_reloc, /* special_function */
634 "R_PPC_PLTREL32", /* name */
635 FALSE, /* partial_inplace */
636 0, /* src_mask */
637 0, /* dst_mask */
638 TRUE), /* pcrel_offset */
640 /* Like R_PPC_ADDR16_LO, but referring to the PLT table entry for
641 the symbol. */
642 HOWTO (R_PPC_PLT16_LO, /* type */
643 0, /* rightshift */
644 1, /* size (0 = byte, 1 = short, 2 = long) */
645 16, /* bitsize */
646 FALSE, /* pc_relative */
647 0, /* bitpos */
648 complain_overflow_dont, /* complain_on_overflow */
649 bfd_elf_generic_reloc, /* special_function */
650 "R_PPC_PLT16_LO", /* name */
651 FALSE, /* partial_inplace */
652 0, /* src_mask */
653 0xffff, /* dst_mask */
654 FALSE), /* pcrel_offset */
656 /* Like R_PPC_ADDR16_HI, but referring to the PLT table entry for
657 the symbol. */
658 HOWTO (R_PPC_PLT16_HI, /* type */
659 16, /* rightshift */
660 1, /* size (0 = byte, 1 = short, 2 = long) */
661 16, /* bitsize */
662 FALSE, /* pc_relative */
663 0, /* bitpos */
664 complain_overflow_bitfield, /* complain_on_overflow */
665 bfd_elf_generic_reloc, /* special_function */
666 "R_PPC_PLT16_HI", /* name */
667 FALSE, /* partial_inplace */
668 0, /* src_mask */
669 0xffff, /* dst_mask */
670 FALSE), /* pcrel_offset */
672 /* Like R_PPC_ADDR16_HA, but referring to the PLT table entry for
673 the symbol. */
674 HOWTO (R_PPC_PLT16_HA, /* type */
675 16, /* rightshift */
676 1, /* size (0 = byte, 1 = short, 2 = long) */
677 16, /* bitsize */
678 FALSE, /* pc_relative */
679 0, /* bitpos */
680 complain_overflow_bitfield, /* complain_on_overflow */
681 ppc_elf_addr16_ha_reloc, /* special_function */
682 "R_PPC_PLT16_HA", /* name */
683 FALSE, /* partial_inplace */
684 0, /* src_mask */
685 0xffff, /* dst_mask */
686 FALSE), /* pcrel_offset */
688 /* A sign-extended 16 bit value relative to _SDA_BASE_, for use with
689 small data items. */
690 HOWTO (R_PPC_SDAREL16, /* type */
691 0, /* rightshift */
692 1, /* size (0 = byte, 1 = short, 2 = long) */
693 16, /* bitsize */
694 FALSE, /* pc_relative */
695 0, /* bitpos */
696 complain_overflow_signed, /* complain_on_overflow */
697 bfd_elf_generic_reloc, /* special_function */
698 "R_PPC_SDAREL16", /* name */
699 FALSE, /* partial_inplace */
700 0, /* src_mask */
701 0xffff, /* dst_mask */
702 FALSE), /* pcrel_offset */
704 /* 16-bit section relative relocation. */
705 HOWTO (R_PPC_SECTOFF, /* type */
706 0, /* rightshift */
707 1, /* size (0 = byte, 1 = short, 2 = long) */
708 16, /* bitsize */
709 FALSE, /* pc_relative */
710 0, /* bitpos */
711 complain_overflow_bitfield, /* complain_on_overflow */
712 bfd_elf_generic_reloc, /* special_function */
713 "R_PPC_SECTOFF", /* name */
714 FALSE, /* partial_inplace */
715 0, /* src_mask */
716 0xffff, /* dst_mask */
717 FALSE), /* pcrel_offset */
719 /* 16-bit lower half section relative relocation. */
720 HOWTO (R_PPC_SECTOFF_LO, /* type */
721 0, /* rightshift */
722 1, /* size (0 = byte, 1 = short, 2 = long) */
723 16, /* bitsize */
724 FALSE, /* pc_relative */
725 0, /* bitpos */
726 complain_overflow_dont, /* complain_on_overflow */
727 bfd_elf_generic_reloc, /* special_function */
728 "R_PPC_SECTOFF_LO", /* name */
729 FALSE, /* partial_inplace */
730 0, /* src_mask */
731 0xffff, /* dst_mask */
732 FALSE), /* pcrel_offset */
734 /* 16-bit upper half section relative relocation. */
735 HOWTO (R_PPC_SECTOFF_HI, /* type */
736 16, /* rightshift */
737 1, /* size (0 = byte, 1 = short, 2 = long) */
738 16, /* bitsize */
739 FALSE, /* pc_relative */
740 0, /* bitpos */
741 complain_overflow_bitfield, /* complain_on_overflow */
742 bfd_elf_generic_reloc, /* special_function */
743 "R_PPC_SECTOFF_HI", /* name */
744 FALSE, /* partial_inplace */
745 0, /* src_mask */
746 0xffff, /* dst_mask */
747 FALSE), /* pcrel_offset */
749 /* 16-bit upper half adjusted section relative relocation. */
750 HOWTO (R_PPC_SECTOFF_HA, /* type */
751 16, /* rightshift */
752 1, /* size (0 = byte, 1 = short, 2 = long) */
753 16, /* bitsize */
754 FALSE, /* pc_relative */
755 0, /* bitpos */
756 complain_overflow_bitfield, /* complain_on_overflow */
757 ppc_elf_addr16_ha_reloc, /* special_function */
758 "R_PPC_SECTOFF_HA", /* name */
759 FALSE, /* partial_inplace */
760 0, /* src_mask */
761 0xffff, /* dst_mask */
762 FALSE), /* pcrel_offset */
764 /* Marker relocs for TLS. */
765 HOWTO (R_PPC_TLS,
766 0, /* rightshift */
767 2, /* size (0 = byte, 1 = short, 2 = long) */
768 32, /* bitsize */
769 FALSE, /* pc_relative */
770 0, /* bitpos */
771 complain_overflow_dont, /* complain_on_overflow */
772 bfd_elf_generic_reloc, /* special_function */
773 "R_PPC_TLS", /* name */
774 FALSE, /* partial_inplace */
775 0, /* src_mask */
776 0, /* dst_mask */
777 FALSE), /* pcrel_offset */
779 HOWTO (R_PPC_TLSGD,
780 0, /* rightshift */
781 2, /* size (0 = byte, 1 = short, 2 = long) */
782 32, /* bitsize */
783 FALSE, /* pc_relative */
784 0, /* bitpos */
785 complain_overflow_dont, /* complain_on_overflow */
786 bfd_elf_generic_reloc, /* special_function */
787 "R_PPC_TLSGD", /* name */
788 FALSE, /* partial_inplace */
789 0, /* src_mask */
790 0, /* dst_mask */
791 FALSE), /* pcrel_offset */
793 HOWTO (R_PPC_TLSLD,
794 0, /* rightshift */
795 2, /* size (0 = byte, 1 = short, 2 = long) */
796 32, /* bitsize */
797 FALSE, /* pc_relative */
798 0, /* bitpos */
799 complain_overflow_dont, /* complain_on_overflow */
800 bfd_elf_generic_reloc, /* special_function */
801 "R_PPC_TLSLD", /* name */
802 FALSE, /* partial_inplace */
803 0, /* src_mask */
804 0, /* dst_mask */
805 FALSE), /* pcrel_offset */
807 /* Computes the load module index of the load module that contains the
808 definition of its TLS sym. */
809 HOWTO (R_PPC_DTPMOD32,
810 0, /* rightshift */
811 2, /* size (0 = byte, 1 = short, 2 = long) */
812 32, /* bitsize */
813 FALSE, /* pc_relative */
814 0, /* bitpos */
815 complain_overflow_dont, /* complain_on_overflow */
816 ppc_elf_unhandled_reloc, /* special_function */
817 "R_PPC_DTPMOD32", /* name */
818 FALSE, /* partial_inplace */
819 0, /* src_mask */
820 0xffffffff, /* dst_mask */
821 FALSE), /* pcrel_offset */
823 /* Computes a dtv-relative displacement, the difference between the value
824 of sym+add and the base address of the thread-local storage block that
825 contains the definition of sym, minus 0x8000. */
826 HOWTO (R_PPC_DTPREL32,
827 0, /* rightshift */
828 2, /* size (0 = byte, 1 = short, 2 = long) */
829 32, /* bitsize */
830 FALSE, /* pc_relative */
831 0, /* bitpos */
832 complain_overflow_dont, /* complain_on_overflow */
833 ppc_elf_unhandled_reloc, /* special_function */
834 "R_PPC_DTPREL32", /* name */
835 FALSE, /* partial_inplace */
836 0, /* src_mask */
837 0xffffffff, /* dst_mask */
838 FALSE), /* pcrel_offset */
840 /* A 16 bit dtprel reloc. */
841 HOWTO (R_PPC_DTPREL16,
842 0, /* rightshift */
843 1, /* size (0 = byte, 1 = short, 2 = long) */
844 16, /* bitsize */
845 FALSE, /* pc_relative */
846 0, /* bitpos */
847 complain_overflow_signed, /* complain_on_overflow */
848 ppc_elf_unhandled_reloc, /* special_function */
849 "R_PPC_DTPREL16", /* name */
850 FALSE, /* partial_inplace */
851 0, /* src_mask */
852 0xffff, /* dst_mask */
853 FALSE), /* pcrel_offset */
855 /* Like DTPREL16, but no overflow. */
856 HOWTO (R_PPC_DTPREL16_LO,
857 0, /* rightshift */
858 1, /* size (0 = byte, 1 = short, 2 = long) */
859 16, /* bitsize */
860 FALSE, /* pc_relative */
861 0, /* bitpos */
862 complain_overflow_dont, /* complain_on_overflow */
863 ppc_elf_unhandled_reloc, /* special_function */
864 "R_PPC_DTPREL16_LO", /* name */
865 FALSE, /* partial_inplace */
866 0, /* src_mask */
867 0xffff, /* dst_mask */
868 FALSE), /* pcrel_offset */
870 /* Like DTPREL16_LO, but next higher group of 16 bits. */
871 HOWTO (R_PPC_DTPREL16_HI,
872 16, /* rightshift */
873 1, /* size (0 = byte, 1 = short, 2 = long) */
874 16, /* bitsize */
875 FALSE, /* pc_relative */
876 0, /* bitpos */
877 complain_overflow_dont, /* complain_on_overflow */
878 ppc_elf_unhandled_reloc, /* special_function */
879 "R_PPC_DTPREL16_HI", /* name */
880 FALSE, /* partial_inplace */
881 0, /* src_mask */
882 0xffff, /* dst_mask */
883 FALSE), /* pcrel_offset */
885 /* Like DTPREL16_HI, but adjust for low 16 bits. */
886 HOWTO (R_PPC_DTPREL16_HA,
887 16, /* rightshift */
888 1, /* size (0 = byte, 1 = short, 2 = long) */
889 16, /* bitsize */
890 FALSE, /* pc_relative */
891 0, /* bitpos */
892 complain_overflow_dont, /* complain_on_overflow */
893 ppc_elf_unhandled_reloc, /* special_function */
894 "R_PPC_DTPREL16_HA", /* name */
895 FALSE, /* partial_inplace */
896 0, /* src_mask */
897 0xffff, /* dst_mask */
898 FALSE), /* pcrel_offset */
900 /* Computes a tp-relative displacement, the difference between the value of
901 sym+add and the value of the thread pointer (r13). */
902 HOWTO (R_PPC_TPREL32,
903 0, /* rightshift */
904 2, /* size (0 = byte, 1 = short, 2 = long) */
905 32, /* bitsize */
906 FALSE, /* pc_relative */
907 0, /* bitpos */
908 complain_overflow_dont, /* complain_on_overflow */
909 ppc_elf_unhandled_reloc, /* special_function */
910 "R_PPC_TPREL32", /* name */
911 FALSE, /* partial_inplace */
912 0, /* src_mask */
913 0xffffffff, /* dst_mask */
914 FALSE), /* pcrel_offset */
916 /* A 16 bit tprel reloc. */
917 HOWTO (R_PPC_TPREL16,
918 0, /* rightshift */
919 1, /* size (0 = byte, 1 = short, 2 = long) */
920 16, /* bitsize */
921 FALSE, /* pc_relative */
922 0, /* bitpos */
923 complain_overflow_signed, /* complain_on_overflow */
924 ppc_elf_unhandled_reloc, /* special_function */
925 "R_PPC_TPREL16", /* name */
926 FALSE, /* partial_inplace */
927 0, /* src_mask */
928 0xffff, /* dst_mask */
929 FALSE), /* pcrel_offset */
931 /* Like TPREL16, but no overflow. */
932 HOWTO (R_PPC_TPREL16_LO,
933 0, /* rightshift */
934 1, /* size (0 = byte, 1 = short, 2 = long) */
935 16, /* bitsize */
936 FALSE, /* pc_relative */
937 0, /* bitpos */
938 complain_overflow_dont, /* complain_on_overflow */
939 ppc_elf_unhandled_reloc, /* special_function */
940 "R_PPC_TPREL16_LO", /* name */
941 FALSE, /* partial_inplace */
942 0, /* src_mask */
943 0xffff, /* dst_mask */
944 FALSE), /* pcrel_offset */
946 /* Like TPREL16_LO, but next higher group of 16 bits. */
947 HOWTO (R_PPC_TPREL16_HI,
948 16, /* rightshift */
949 1, /* size (0 = byte, 1 = short, 2 = long) */
950 16, /* bitsize */
951 FALSE, /* pc_relative */
952 0, /* bitpos */
953 complain_overflow_dont, /* complain_on_overflow */
954 ppc_elf_unhandled_reloc, /* special_function */
955 "R_PPC_TPREL16_HI", /* name */
956 FALSE, /* partial_inplace */
957 0, /* src_mask */
958 0xffff, /* dst_mask */
959 FALSE), /* pcrel_offset */
961 /* Like TPREL16_HI, but adjust for low 16 bits. */
962 HOWTO (R_PPC_TPREL16_HA,
963 16, /* rightshift */
964 1, /* size (0 = byte, 1 = short, 2 = long) */
965 16, /* bitsize */
966 FALSE, /* pc_relative */
967 0, /* bitpos */
968 complain_overflow_dont, /* complain_on_overflow */
969 ppc_elf_unhandled_reloc, /* special_function */
970 "R_PPC_TPREL16_HA", /* name */
971 FALSE, /* partial_inplace */
972 0, /* src_mask */
973 0xffff, /* dst_mask */
974 FALSE), /* pcrel_offset */
976 /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
977 with values (sym+add)@dtpmod and (sym+add)@dtprel, and computes the offset
978 to the first entry. */
979 HOWTO (R_PPC_GOT_TLSGD16,
980 0, /* rightshift */
981 1, /* size (0 = byte, 1 = short, 2 = long) */
982 16, /* bitsize */
983 FALSE, /* pc_relative */
984 0, /* bitpos */
985 complain_overflow_signed, /* complain_on_overflow */
986 ppc_elf_unhandled_reloc, /* special_function */
987 "R_PPC_GOT_TLSGD16", /* name */
988 FALSE, /* partial_inplace */
989 0, /* src_mask */
990 0xffff, /* dst_mask */
991 FALSE), /* pcrel_offset */
993 /* Like GOT_TLSGD16, but no overflow. */
994 HOWTO (R_PPC_GOT_TLSGD16_LO,
995 0, /* rightshift */
996 1, /* size (0 = byte, 1 = short, 2 = long) */
997 16, /* bitsize */
998 FALSE, /* pc_relative */
999 0, /* bitpos */
1000 complain_overflow_dont, /* complain_on_overflow */
1001 ppc_elf_unhandled_reloc, /* special_function */
1002 "R_PPC_GOT_TLSGD16_LO", /* name */
1003 FALSE, /* partial_inplace */
1004 0, /* src_mask */
1005 0xffff, /* dst_mask */
1006 FALSE), /* pcrel_offset */
1008 /* Like GOT_TLSGD16_LO, but next higher group of 16 bits. */
1009 HOWTO (R_PPC_GOT_TLSGD16_HI,
1010 16, /* rightshift */
1011 1, /* size (0 = byte, 1 = short, 2 = long) */
1012 16, /* bitsize */
1013 FALSE, /* pc_relative */
1014 0, /* bitpos */
1015 complain_overflow_dont, /* complain_on_overflow */
1016 ppc_elf_unhandled_reloc, /* special_function */
1017 "R_PPC_GOT_TLSGD16_HI", /* name */
1018 FALSE, /* partial_inplace */
1019 0, /* src_mask */
1020 0xffff, /* dst_mask */
1021 FALSE), /* pcrel_offset */
1023 /* Like GOT_TLSGD16_HI, but adjust for low 16 bits. */
1024 HOWTO (R_PPC_GOT_TLSGD16_HA,
1025 16, /* rightshift */
1026 1, /* size (0 = byte, 1 = short, 2 = long) */
1027 16, /* bitsize */
1028 FALSE, /* pc_relative */
1029 0, /* bitpos */
1030 complain_overflow_dont, /* complain_on_overflow */
1031 ppc_elf_unhandled_reloc, /* special_function */
1032 "R_PPC_GOT_TLSGD16_HA", /* name */
1033 FALSE, /* partial_inplace */
1034 0, /* src_mask */
1035 0xffff, /* dst_mask */
1036 FALSE), /* pcrel_offset */
1038 /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
1039 with values (sym+add)@dtpmod and zero, and computes the offset to the
1040 first entry. */
1041 HOWTO (R_PPC_GOT_TLSLD16,
1042 0, /* rightshift */
1043 1, /* size (0 = byte, 1 = short, 2 = long) */
1044 16, /* bitsize */
1045 FALSE, /* pc_relative */
1046 0, /* bitpos */
1047 complain_overflow_signed, /* complain_on_overflow */
1048 ppc_elf_unhandled_reloc, /* special_function */
1049 "R_PPC_GOT_TLSLD16", /* name */
1050 FALSE, /* partial_inplace */
1051 0, /* src_mask */
1052 0xffff, /* dst_mask */
1053 FALSE), /* pcrel_offset */
1055 /* Like GOT_TLSLD16, but no overflow. */
1056 HOWTO (R_PPC_GOT_TLSLD16_LO,
1057 0, /* rightshift */
1058 1, /* size (0 = byte, 1 = short, 2 = long) */
1059 16, /* bitsize */
1060 FALSE, /* pc_relative */
1061 0, /* bitpos */
1062 complain_overflow_dont, /* complain_on_overflow */
1063 ppc_elf_unhandled_reloc, /* special_function */
1064 "R_PPC_GOT_TLSLD16_LO", /* name */
1065 FALSE, /* partial_inplace */
1066 0, /* src_mask */
1067 0xffff, /* dst_mask */
1068 FALSE), /* pcrel_offset */
1070 /* Like GOT_TLSLD16_LO, but next higher group of 16 bits. */
1071 HOWTO (R_PPC_GOT_TLSLD16_HI,
1072 16, /* rightshift */
1073 1, /* size (0 = byte, 1 = short, 2 = long) */
1074 16, /* bitsize */
1075 FALSE, /* pc_relative */
1076 0, /* bitpos */
1077 complain_overflow_dont, /* complain_on_overflow */
1078 ppc_elf_unhandled_reloc, /* special_function */
1079 "R_PPC_GOT_TLSLD16_HI", /* name */
1080 FALSE, /* partial_inplace */
1081 0, /* src_mask */
1082 0xffff, /* dst_mask */
1083 FALSE), /* pcrel_offset */
1085 /* Like GOT_TLSLD16_HI, but adjust for low 16 bits. */
1086 HOWTO (R_PPC_GOT_TLSLD16_HA,
1087 16, /* rightshift */
1088 1, /* size (0 = byte, 1 = short, 2 = long) */
1089 16, /* bitsize */
1090 FALSE, /* pc_relative */
1091 0, /* bitpos */
1092 complain_overflow_dont, /* complain_on_overflow */
1093 ppc_elf_unhandled_reloc, /* special_function */
1094 "R_PPC_GOT_TLSLD16_HA", /* name */
1095 FALSE, /* partial_inplace */
1096 0, /* src_mask */
1097 0xffff, /* dst_mask */
1098 FALSE), /* pcrel_offset */
1100 /* Allocates an entry in the GOT with value (sym+add)@dtprel, and computes
1101 the offset to the entry. */
1102 HOWTO (R_PPC_GOT_DTPREL16,
1103 0, /* rightshift */
1104 1, /* size (0 = byte, 1 = short, 2 = long) */
1105 16, /* bitsize */
1106 FALSE, /* pc_relative */
1107 0, /* bitpos */
1108 complain_overflow_signed, /* complain_on_overflow */
1109 ppc_elf_unhandled_reloc, /* special_function */
1110 "R_PPC_GOT_DTPREL16", /* name */
1111 FALSE, /* partial_inplace */
1112 0, /* src_mask */
1113 0xffff, /* dst_mask */
1114 FALSE), /* pcrel_offset */
1116 /* Like GOT_DTPREL16, but no overflow. */
1117 HOWTO (R_PPC_GOT_DTPREL16_LO,
1118 0, /* rightshift */
1119 1, /* size (0 = byte, 1 = short, 2 = long) */
1120 16, /* bitsize */
1121 FALSE, /* pc_relative */
1122 0, /* bitpos */
1123 complain_overflow_dont, /* complain_on_overflow */
1124 ppc_elf_unhandled_reloc, /* special_function */
1125 "R_PPC_GOT_DTPREL16_LO", /* name */
1126 FALSE, /* partial_inplace */
1127 0, /* src_mask */
1128 0xffff, /* dst_mask */
1129 FALSE), /* pcrel_offset */
1131 /* Like GOT_DTPREL16_LO, but next higher group of 16 bits. */
1132 HOWTO (R_PPC_GOT_DTPREL16_HI,
1133 16, /* rightshift */
1134 1, /* size (0 = byte, 1 = short, 2 = long) */
1135 16, /* bitsize */
1136 FALSE, /* pc_relative */
1137 0, /* bitpos */
1138 complain_overflow_dont, /* complain_on_overflow */
1139 ppc_elf_unhandled_reloc, /* special_function */
1140 "R_PPC_GOT_DTPREL16_HI", /* name */
1141 FALSE, /* partial_inplace */
1142 0, /* src_mask */
1143 0xffff, /* dst_mask */
1144 FALSE), /* pcrel_offset */
1146 /* Like GOT_DTPREL16_HI, but adjust for low 16 bits. */
1147 HOWTO (R_PPC_GOT_DTPREL16_HA,
1148 16, /* rightshift */
1149 1, /* size (0 = byte, 1 = short, 2 = long) */
1150 16, /* bitsize */
1151 FALSE, /* pc_relative */
1152 0, /* bitpos */
1153 complain_overflow_dont, /* complain_on_overflow */
1154 ppc_elf_unhandled_reloc, /* special_function */
1155 "R_PPC_GOT_DTPREL16_HA", /* name */
1156 FALSE, /* partial_inplace */
1157 0, /* src_mask */
1158 0xffff, /* dst_mask */
1159 FALSE), /* pcrel_offset */
1161 /* Allocates an entry in the GOT with value (sym+add)@tprel, and computes the
1162 offset to the entry. */
1163 HOWTO (R_PPC_GOT_TPREL16,
1164 0, /* rightshift */
1165 1, /* size (0 = byte, 1 = short, 2 = long) */
1166 16, /* bitsize */
1167 FALSE, /* pc_relative */
1168 0, /* bitpos */
1169 complain_overflow_signed, /* complain_on_overflow */
1170 ppc_elf_unhandled_reloc, /* special_function */
1171 "R_PPC_GOT_TPREL16", /* name */
1172 FALSE, /* partial_inplace */
1173 0, /* src_mask */
1174 0xffff, /* dst_mask */
1175 FALSE), /* pcrel_offset */
1177 /* Like GOT_TPREL16, but no overflow. */
1178 HOWTO (R_PPC_GOT_TPREL16_LO,
1179 0, /* rightshift */
1180 1, /* size (0 = byte, 1 = short, 2 = long) */
1181 16, /* bitsize */
1182 FALSE, /* pc_relative */
1183 0, /* bitpos */
1184 complain_overflow_dont, /* complain_on_overflow */
1185 ppc_elf_unhandled_reloc, /* special_function */
1186 "R_PPC_GOT_TPREL16_LO", /* name */
1187 FALSE, /* partial_inplace */
1188 0, /* src_mask */
1189 0xffff, /* dst_mask */
1190 FALSE), /* pcrel_offset */
1192 /* Like GOT_TPREL16_LO, but next higher group of 16 bits. */
1193 HOWTO (R_PPC_GOT_TPREL16_HI,
1194 16, /* rightshift */
1195 1, /* size (0 = byte, 1 = short, 2 = long) */
1196 16, /* bitsize */
1197 FALSE, /* pc_relative */
1198 0, /* bitpos */
1199 complain_overflow_dont, /* complain_on_overflow */
1200 ppc_elf_unhandled_reloc, /* special_function */
1201 "R_PPC_GOT_TPREL16_HI", /* name */
1202 FALSE, /* partial_inplace */
1203 0, /* src_mask */
1204 0xffff, /* dst_mask */
1205 FALSE), /* pcrel_offset */
1207 /* Like GOT_TPREL16_HI, but adjust for low 16 bits. */
1208 HOWTO (R_PPC_GOT_TPREL16_HA,
1209 16, /* rightshift */
1210 1, /* size (0 = byte, 1 = short, 2 = long) */
1211 16, /* bitsize */
1212 FALSE, /* pc_relative */
1213 0, /* bitpos */
1214 complain_overflow_dont, /* complain_on_overflow */
1215 ppc_elf_unhandled_reloc, /* special_function */
1216 "R_PPC_GOT_TPREL16_HA", /* name */
1217 FALSE, /* partial_inplace */
1218 0, /* src_mask */
1219 0xffff, /* dst_mask */
1220 FALSE), /* pcrel_offset */
1222 /* The remaining relocs are from the Embedded ELF ABI, and are not
1223 in the SVR4 ELF ABI. */
1225 /* 32 bit value resulting from the addend minus the symbol. */
1226 HOWTO (R_PPC_EMB_NADDR32, /* type */
1227 0, /* rightshift */
1228 2, /* size (0 = byte, 1 = short, 2 = long) */
1229 32, /* bitsize */
1230 FALSE, /* pc_relative */
1231 0, /* bitpos */
1232 complain_overflow_bitfield, /* complain_on_overflow */
1233 bfd_elf_generic_reloc, /* special_function */
1234 "R_PPC_EMB_NADDR32", /* name */
1235 FALSE, /* partial_inplace */
1236 0, /* src_mask */
1237 0xffffffff, /* dst_mask */
1238 FALSE), /* pcrel_offset */
1240 /* 16 bit value resulting from the addend minus the symbol. */
1241 HOWTO (R_PPC_EMB_NADDR16, /* type */
1242 0, /* rightshift */
1243 1, /* size (0 = byte, 1 = short, 2 = long) */
1244 16, /* bitsize */
1245 FALSE, /* pc_relative */
1246 0, /* bitpos */
1247 complain_overflow_bitfield, /* complain_on_overflow */
1248 bfd_elf_generic_reloc, /* special_function */
1249 "R_PPC_EMB_NADDR16", /* name */
1250 FALSE, /* partial_inplace */
1251 0, /* src_mask */
1252 0xffff, /* dst_mask */
1253 FALSE), /* pcrel_offset */
1255 /* 16 bit value resulting from the addend minus the symbol. */
1256 HOWTO (R_PPC_EMB_NADDR16_LO, /* type */
1257 0, /* rightshift */
1258 1, /* size (0 = byte, 1 = short, 2 = long) */
1259 16, /* bitsize */
1260 FALSE, /* pc_relative */
1261 0, /* bitpos */
1262 complain_overflow_dont,/* complain_on_overflow */
1263 bfd_elf_generic_reloc, /* special_function */
1264 "R_PPC_EMB_ADDR16_LO", /* name */
1265 FALSE, /* partial_inplace */
1266 0, /* src_mask */
1267 0xffff, /* dst_mask */
1268 FALSE), /* pcrel_offset */
1270 /* The high order 16 bits of the addend minus the symbol. */
1271 HOWTO (R_PPC_EMB_NADDR16_HI, /* type */
1272 16, /* rightshift */
1273 1, /* size (0 = byte, 1 = short, 2 = long) */
1274 16, /* bitsize */
1275 FALSE, /* pc_relative */
1276 0, /* bitpos */
1277 complain_overflow_dont, /* complain_on_overflow */
1278 bfd_elf_generic_reloc, /* special_function */
1279 "R_PPC_EMB_NADDR16_HI", /* name */
1280 FALSE, /* partial_inplace */
1281 0, /* src_mask */
1282 0xffff, /* dst_mask */
1283 FALSE), /* pcrel_offset */
1285 /* The high order 16 bits of the result of the addend minus the address,
1286 plus 1 if the contents of the low 16 bits, treated as a signed number,
1287 is negative. */
1288 HOWTO (R_PPC_EMB_NADDR16_HA, /* type */
1289 16, /* rightshift */
1290 1, /* size (0 = byte, 1 = short, 2 = long) */
1291 16, /* bitsize */
1292 FALSE, /* pc_relative */
1293 0, /* bitpos */
1294 complain_overflow_dont, /* complain_on_overflow */
1295 ppc_elf_addr16_ha_reloc, /* special_function */
1296 "R_PPC_EMB_NADDR16_HA", /* name */
1297 FALSE, /* partial_inplace */
1298 0, /* src_mask */
1299 0xffff, /* dst_mask */
1300 FALSE), /* pcrel_offset */
1302 /* 16 bit value resulting from allocating a 4 byte word to hold an
1303 address in the .sdata section, and returning the offset from
1304 _SDA_BASE_ for that relocation. */
1305 HOWTO (R_PPC_EMB_SDAI16, /* type */
1306 0, /* rightshift */
1307 1, /* size (0 = byte, 1 = short, 2 = long) */
1308 16, /* bitsize */
1309 FALSE, /* pc_relative */
1310 0, /* bitpos */
1311 complain_overflow_signed, /* complain_on_overflow */
1312 bfd_elf_generic_reloc, /* special_function */
1313 "R_PPC_EMB_SDAI16", /* name */
1314 FALSE, /* partial_inplace */
1315 0, /* src_mask */
1316 0xffff, /* dst_mask */
1317 FALSE), /* pcrel_offset */
1319 /* 16 bit value resulting from allocating a 4 byte word to hold an
1320 address in the .sdata2 section, and returning the offset from
1321 _SDA2_BASE_ for that relocation. */
1322 HOWTO (R_PPC_EMB_SDA2I16, /* type */
1323 0, /* rightshift */
1324 1, /* size (0 = byte, 1 = short, 2 = long) */
1325 16, /* bitsize */
1326 FALSE, /* pc_relative */
1327 0, /* bitpos */
1328 complain_overflow_signed, /* complain_on_overflow */
1329 bfd_elf_generic_reloc, /* special_function */
1330 "R_PPC_EMB_SDA2I16", /* name */
1331 FALSE, /* partial_inplace */
1332 0, /* src_mask */
1333 0xffff, /* dst_mask */
1334 FALSE), /* pcrel_offset */
1336 /* A sign-extended 16 bit value relative to _SDA2_BASE_, for use with
1337 small data items. */
1338 HOWTO (R_PPC_EMB_SDA2REL, /* type */
1339 0, /* rightshift */
1340 1, /* size (0 = byte, 1 = short, 2 = long) */
1341 16, /* bitsize */
1342 FALSE, /* pc_relative */
1343 0, /* bitpos */
1344 complain_overflow_signed, /* complain_on_overflow */
1345 bfd_elf_generic_reloc, /* special_function */
1346 "R_PPC_EMB_SDA2REL", /* name */
1347 FALSE, /* partial_inplace */
1348 0, /* src_mask */
1349 0xffff, /* dst_mask */
1350 FALSE), /* pcrel_offset */
1352 /* Relocate against either _SDA_BASE_ or _SDA2_BASE_, filling in the 16 bit
1353 signed offset from the appropriate base, and filling in the register
1354 field with the appropriate register (0, 2, or 13). */
1355 HOWTO (R_PPC_EMB_SDA21, /* type */
1356 0, /* rightshift */
1357 2, /* size (0 = byte, 1 = short, 2 = long) */
1358 16, /* bitsize */
1359 FALSE, /* pc_relative */
1360 0, /* bitpos */
1361 complain_overflow_signed, /* complain_on_overflow */
1362 bfd_elf_generic_reloc, /* special_function */
1363 "R_PPC_EMB_SDA21", /* name */
1364 FALSE, /* partial_inplace */
1365 0, /* src_mask */
1366 0xffff, /* dst_mask */
1367 FALSE), /* pcrel_offset */
1369 /* Relocation not handled: R_PPC_EMB_MRKREF */
1370 /* Relocation not handled: R_PPC_EMB_RELSEC16 */
1371 /* Relocation not handled: R_PPC_EMB_RELST_LO */
1372 /* Relocation not handled: R_PPC_EMB_RELST_HI */
1373 /* Relocation not handled: R_PPC_EMB_RELST_HA */
1374 /* Relocation not handled: R_PPC_EMB_BIT_FLD */
1376 /* PC relative relocation against either _SDA_BASE_ or _SDA2_BASE_, filling
1377 in the 16 bit signed offset from the appropriate base, and filling in the
1378 register field with the appropriate register (0, 2, or 13). */
1379 HOWTO (R_PPC_EMB_RELSDA, /* type */
1380 0, /* rightshift */
1381 1, /* size (0 = byte, 1 = short, 2 = long) */
1382 16, /* bitsize */
1383 TRUE, /* pc_relative */
1384 0, /* bitpos */
1385 complain_overflow_signed, /* complain_on_overflow */
1386 bfd_elf_generic_reloc, /* special_function */
1387 "R_PPC_EMB_RELSDA", /* name */
1388 FALSE, /* partial_inplace */
1389 0, /* src_mask */
1390 0xffff, /* dst_mask */
1391 FALSE), /* pcrel_offset */
1393 HOWTO (R_PPC_IRELATIVE, /* type */
1394 0, /* rightshift */
1395 2, /* size (0 = byte, 1 = short, 2 = long) */
1396 32, /* bitsize */
1397 FALSE, /* pc_relative */
1398 0, /* bitpos */
1399 complain_overflow_bitfield, /* complain_on_overflow */
1400 bfd_elf_generic_reloc, /* special_function */
1401 "R_PPC_IRELATIVE", /* name */
1402 FALSE, /* partial_inplace */
1403 0, /* src_mask */
1404 0xffffffff, /* dst_mask */
1405 FALSE), /* pcrel_offset */
1407 /* A 16 bit relative relocation. */
1408 HOWTO (R_PPC_REL16, /* type */
1409 0, /* rightshift */
1410 1, /* size (0 = byte, 1 = short, 2 = long) */
1411 16, /* bitsize */
1412 TRUE, /* pc_relative */
1413 0, /* bitpos */
1414 complain_overflow_bitfield, /* complain_on_overflow */
1415 bfd_elf_generic_reloc, /* special_function */
1416 "R_PPC_REL16", /* name */
1417 FALSE, /* partial_inplace */
1418 0, /* src_mask */
1419 0xffff, /* dst_mask */
1420 TRUE), /* pcrel_offset */
1422 /* A 16 bit relative relocation without overflow. */
1423 HOWTO (R_PPC_REL16_LO, /* type */
1424 0, /* rightshift */
1425 1, /* size (0 = byte, 1 = short, 2 = long) */
1426 16, /* bitsize */
1427 TRUE, /* pc_relative */
1428 0, /* bitpos */
1429 complain_overflow_dont,/* complain_on_overflow */
1430 bfd_elf_generic_reloc, /* special_function */
1431 "R_PPC_REL16_LO", /* name */
1432 FALSE, /* partial_inplace */
1433 0, /* src_mask */
1434 0xffff, /* dst_mask */
1435 TRUE), /* pcrel_offset */
1437 /* The high order 16 bits of a relative address. */
1438 HOWTO (R_PPC_REL16_HI, /* type */
1439 16, /* rightshift */
1440 1, /* size (0 = byte, 1 = short, 2 = long) */
1441 16, /* bitsize */
1442 TRUE, /* pc_relative */
1443 0, /* bitpos */
1444 complain_overflow_dont, /* complain_on_overflow */
1445 bfd_elf_generic_reloc, /* special_function */
1446 "R_PPC_REL16_HI", /* name */
1447 FALSE, /* partial_inplace */
1448 0, /* src_mask */
1449 0xffff, /* dst_mask */
1450 TRUE), /* pcrel_offset */
1452 /* The high order 16 bits of a relative address, plus 1 if the contents of
1453 the low 16 bits, treated as a signed number, is negative. */
1454 HOWTO (R_PPC_REL16_HA, /* type */
1455 16, /* rightshift */
1456 1, /* size (0 = byte, 1 = short, 2 = long) */
1457 16, /* bitsize */
1458 TRUE, /* pc_relative */
1459 0, /* bitpos */
1460 complain_overflow_dont, /* complain_on_overflow */
1461 ppc_elf_addr16_ha_reloc, /* special_function */
1462 "R_PPC_REL16_HA", /* name */
1463 FALSE, /* partial_inplace */
1464 0, /* src_mask */
1465 0xffff, /* dst_mask */
1466 TRUE), /* pcrel_offset */
1468 /* GNU extension to record C++ vtable hierarchy. */
1469 HOWTO (R_PPC_GNU_VTINHERIT, /* type */
1470 0, /* rightshift */
1471 0, /* size (0 = byte, 1 = short, 2 = long) */
1472 0, /* bitsize */
1473 FALSE, /* pc_relative */
1474 0, /* bitpos */
1475 complain_overflow_dont, /* complain_on_overflow */
1476 NULL, /* special_function */
1477 "R_PPC_GNU_VTINHERIT", /* name */
1478 FALSE, /* partial_inplace */
1479 0, /* src_mask */
1480 0, /* dst_mask */
1481 FALSE), /* pcrel_offset */
1483 /* GNU extension to record C++ vtable member usage. */
1484 HOWTO (R_PPC_GNU_VTENTRY, /* type */
1485 0, /* rightshift */
1486 0, /* size (0 = byte, 1 = short, 2 = long) */
1487 0, /* bitsize */
1488 FALSE, /* pc_relative */
1489 0, /* bitpos */
1490 complain_overflow_dont, /* complain_on_overflow */
1491 NULL, /* special_function */
1492 "R_PPC_GNU_VTENTRY", /* name */
1493 FALSE, /* partial_inplace */
1494 0, /* src_mask */
1495 0, /* dst_mask */
1496 FALSE), /* pcrel_offset */
1498 /* Phony reloc to handle AIX style TOC entries. */
1499 HOWTO (R_PPC_TOC16, /* type */
1500 0, /* rightshift */
1501 1, /* size (0 = byte, 1 = short, 2 = long) */
1502 16, /* bitsize */
1503 FALSE, /* pc_relative */
1504 0, /* bitpos */
1505 complain_overflow_signed, /* complain_on_overflow */
1506 bfd_elf_generic_reloc, /* special_function */
1507 "R_PPC_TOC16", /* name */
1508 FALSE, /* partial_inplace */
1509 0, /* src_mask */
1510 0xffff, /* dst_mask */
1511 FALSE), /* pcrel_offset */
1514 /* Initialize the ppc_elf_howto_table, so that linear accesses can be done. */
1516 static void
1517 ppc_elf_howto_init (void)
1519 unsigned int i, type;
1521 for (i = 0;
1522 i < sizeof (ppc_elf_howto_raw) / sizeof (ppc_elf_howto_raw[0]);
1523 i++)
1525 type = ppc_elf_howto_raw[i].type;
1526 if (type >= (sizeof (ppc_elf_howto_table)
1527 / sizeof (ppc_elf_howto_table[0])))
1528 abort ();
1529 ppc_elf_howto_table[type] = &ppc_elf_howto_raw[i];
1533 static reloc_howto_type *
1534 ppc_elf_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
1535 bfd_reloc_code_real_type code)
1537 enum elf_ppc_reloc_type r;
1539 /* Initialize howto table if not already done. */
1540 if (!ppc_elf_howto_table[R_PPC_ADDR32])
1541 ppc_elf_howto_init ();
1543 switch (code)
1545 default:
1546 return NULL;
1548 case BFD_RELOC_NONE: r = R_PPC_NONE; break;
1549 case BFD_RELOC_32: r = R_PPC_ADDR32; break;
1550 case BFD_RELOC_PPC_BA26: r = R_PPC_ADDR24; break;
1551 case BFD_RELOC_16: r = R_PPC_ADDR16; break;
1552 case BFD_RELOC_LO16: r = R_PPC_ADDR16_LO; break;
1553 case BFD_RELOC_HI16: r = R_PPC_ADDR16_HI; break;
1554 case BFD_RELOC_HI16_S: r = R_PPC_ADDR16_HA; break;
1555 case BFD_RELOC_PPC_BA16: r = R_PPC_ADDR14; break;
1556 case BFD_RELOC_PPC_BA16_BRTAKEN: r = R_PPC_ADDR14_BRTAKEN; break;
1557 case BFD_RELOC_PPC_BA16_BRNTAKEN: r = R_PPC_ADDR14_BRNTAKEN; break;
1558 case BFD_RELOC_PPC_B26: r = R_PPC_REL24; break;
1559 case BFD_RELOC_PPC_B16: r = R_PPC_REL14; break;
1560 case BFD_RELOC_PPC_B16_BRTAKEN: r = R_PPC_REL14_BRTAKEN; break;
1561 case BFD_RELOC_PPC_B16_BRNTAKEN: r = R_PPC_REL14_BRNTAKEN; break;
1562 case BFD_RELOC_16_GOTOFF: r = R_PPC_GOT16; break;
1563 case BFD_RELOC_LO16_GOTOFF: r = R_PPC_GOT16_LO; break;
1564 case BFD_RELOC_HI16_GOTOFF: r = R_PPC_GOT16_HI; break;
1565 case BFD_RELOC_HI16_S_GOTOFF: r = R_PPC_GOT16_HA; break;
1566 case BFD_RELOC_24_PLT_PCREL: r = R_PPC_PLTREL24; break;
1567 case BFD_RELOC_PPC_COPY: r = R_PPC_COPY; break;
1568 case BFD_RELOC_PPC_GLOB_DAT: r = R_PPC_GLOB_DAT; break;
1569 case BFD_RELOC_PPC_LOCAL24PC: r = R_PPC_LOCAL24PC; break;
1570 case BFD_RELOC_32_PCREL: r = R_PPC_REL32; break;
1571 case BFD_RELOC_32_PLTOFF: r = R_PPC_PLT32; break;
1572 case BFD_RELOC_32_PLT_PCREL: r = R_PPC_PLTREL32; break;
1573 case BFD_RELOC_LO16_PLTOFF: r = R_PPC_PLT16_LO; break;
1574 case BFD_RELOC_HI16_PLTOFF: r = R_PPC_PLT16_HI; break;
1575 case BFD_RELOC_HI16_S_PLTOFF: r = R_PPC_PLT16_HA; break;
1576 case BFD_RELOC_GPREL16: r = R_PPC_SDAREL16; break;
1577 case BFD_RELOC_16_BASEREL: r = R_PPC_SECTOFF; break;
1578 case BFD_RELOC_LO16_BASEREL: r = R_PPC_SECTOFF_LO; break;
1579 case BFD_RELOC_HI16_BASEREL: r = R_PPC_SECTOFF_HI; break;
1580 case BFD_RELOC_HI16_S_BASEREL: r = R_PPC_SECTOFF_HA; break;
1581 case BFD_RELOC_CTOR: r = R_PPC_ADDR32; break;
1582 case BFD_RELOC_PPC_TOC16: r = R_PPC_TOC16; break;
1583 case BFD_RELOC_PPC_TLS: r = R_PPC_TLS; break;
1584 case BFD_RELOC_PPC_TLSGD: r = R_PPC_TLSGD; break;
1585 case BFD_RELOC_PPC_TLSLD: r = R_PPC_TLSLD; break;
1586 case BFD_RELOC_PPC_DTPMOD: r = R_PPC_DTPMOD32; break;
1587 case BFD_RELOC_PPC_TPREL16: r = R_PPC_TPREL16; break;
1588 case BFD_RELOC_PPC_TPREL16_LO: r = R_PPC_TPREL16_LO; break;
1589 case BFD_RELOC_PPC_TPREL16_HI: r = R_PPC_TPREL16_HI; break;
1590 case BFD_RELOC_PPC_TPREL16_HA: r = R_PPC_TPREL16_HA; break;
1591 case BFD_RELOC_PPC_TPREL: r = R_PPC_TPREL32; break;
1592 case BFD_RELOC_PPC_DTPREL16: r = R_PPC_DTPREL16; break;
1593 case BFD_RELOC_PPC_DTPREL16_LO: r = R_PPC_DTPREL16_LO; break;
1594 case BFD_RELOC_PPC_DTPREL16_HI: r = R_PPC_DTPREL16_HI; break;
1595 case BFD_RELOC_PPC_DTPREL16_HA: r = R_PPC_DTPREL16_HA; break;
1596 case BFD_RELOC_PPC_DTPREL: r = R_PPC_DTPREL32; break;
1597 case BFD_RELOC_PPC_GOT_TLSGD16: r = R_PPC_GOT_TLSGD16; break;
1598 case BFD_RELOC_PPC_GOT_TLSGD16_LO: r = R_PPC_GOT_TLSGD16_LO; break;
1599 case BFD_RELOC_PPC_GOT_TLSGD16_HI: r = R_PPC_GOT_TLSGD16_HI; break;
1600 case BFD_RELOC_PPC_GOT_TLSGD16_HA: r = R_PPC_GOT_TLSGD16_HA; break;
1601 case BFD_RELOC_PPC_GOT_TLSLD16: r = R_PPC_GOT_TLSLD16; break;
1602 case BFD_RELOC_PPC_GOT_TLSLD16_LO: r = R_PPC_GOT_TLSLD16_LO; break;
1603 case BFD_RELOC_PPC_GOT_TLSLD16_HI: r = R_PPC_GOT_TLSLD16_HI; break;
1604 case BFD_RELOC_PPC_GOT_TLSLD16_HA: r = R_PPC_GOT_TLSLD16_HA; break;
1605 case BFD_RELOC_PPC_GOT_TPREL16: r = R_PPC_GOT_TPREL16; break;
1606 case BFD_RELOC_PPC_GOT_TPREL16_LO: r = R_PPC_GOT_TPREL16_LO; break;
1607 case BFD_RELOC_PPC_GOT_TPREL16_HI: r = R_PPC_GOT_TPREL16_HI; break;
1608 case BFD_RELOC_PPC_GOT_TPREL16_HA: r = R_PPC_GOT_TPREL16_HA; break;
1609 case BFD_RELOC_PPC_GOT_DTPREL16: r = R_PPC_GOT_DTPREL16; break;
1610 case BFD_RELOC_PPC_GOT_DTPREL16_LO: r = R_PPC_GOT_DTPREL16_LO; break;
1611 case BFD_RELOC_PPC_GOT_DTPREL16_HI: r = R_PPC_GOT_DTPREL16_HI; break;
1612 case BFD_RELOC_PPC_GOT_DTPREL16_HA: r = R_PPC_GOT_DTPREL16_HA; break;
1613 case BFD_RELOC_PPC_EMB_NADDR32: r = R_PPC_EMB_NADDR32; break;
1614 case BFD_RELOC_PPC_EMB_NADDR16: r = R_PPC_EMB_NADDR16; break;
1615 case BFD_RELOC_PPC_EMB_NADDR16_LO: r = R_PPC_EMB_NADDR16_LO; break;
1616 case BFD_RELOC_PPC_EMB_NADDR16_HI: r = R_PPC_EMB_NADDR16_HI; break;
1617 case BFD_RELOC_PPC_EMB_NADDR16_HA: r = R_PPC_EMB_NADDR16_HA; break;
1618 case BFD_RELOC_PPC_EMB_SDAI16: r = R_PPC_EMB_SDAI16; break;
1619 case BFD_RELOC_PPC_EMB_SDA2I16: r = R_PPC_EMB_SDA2I16; break;
1620 case BFD_RELOC_PPC_EMB_SDA2REL: r = R_PPC_EMB_SDA2REL; break;
1621 case BFD_RELOC_PPC_EMB_SDA21: r = R_PPC_EMB_SDA21; break;
1622 case BFD_RELOC_PPC_EMB_MRKREF: r = R_PPC_EMB_MRKREF; break;
1623 case BFD_RELOC_PPC_EMB_RELSEC16: r = R_PPC_EMB_RELSEC16; break;
1624 case BFD_RELOC_PPC_EMB_RELST_LO: r = R_PPC_EMB_RELST_LO; break;
1625 case BFD_RELOC_PPC_EMB_RELST_HI: r = R_PPC_EMB_RELST_HI; break;
1626 case BFD_RELOC_PPC_EMB_RELST_HA: r = R_PPC_EMB_RELST_HA; break;
1627 case BFD_RELOC_PPC_EMB_BIT_FLD: r = R_PPC_EMB_BIT_FLD; break;
1628 case BFD_RELOC_PPC_EMB_RELSDA: r = R_PPC_EMB_RELSDA; break;
1629 case BFD_RELOC_16_PCREL: r = R_PPC_REL16; break;
1630 case BFD_RELOC_LO16_PCREL: r = R_PPC_REL16_LO; break;
1631 case BFD_RELOC_HI16_PCREL: r = R_PPC_REL16_HI; break;
1632 case BFD_RELOC_HI16_S_PCREL: r = R_PPC_REL16_HA; break;
1633 case BFD_RELOC_VTABLE_INHERIT: r = R_PPC_GNU_VTINHERIT; break;
1634 case BFD_RELOC_VTABLE_ENTRY: r = R_PPC_GNU_VTENTRY; break;
1637 return ppc_elf_howto_table[r];
1640 static reloc_howto_type *
1641 ppc_elf_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
1642 const char *r_name)
1644 unsigned int i;
1646 for (i = 0;
1647 i < sizeof (ppc_elf_howto_raw) / sizeof (ppc_elf_howto_raw[0]);
1648 i++)
1649 if (ppc_elf_howto_raw[i].name != NULL
1650 && strcasecmp (ppc_elf_howto_raw[i].name, r_name) == 0)
1651 return &ppc_elf_howto_raw[i];
1653 return NULL;
1656 /* Set the howto pointer for a PowerPC ELF reloc. */
1658 static void
1659 ppc_elf_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED,
1660 arelent *cache_ptr,
1661 Elf_Internal_Rela *dst)
1663 /* Initialize howto table if not already done. */
1664 if (!ppc_elf_howto_table[R_PPC_ADDR32])
1665 ppc_elf_howto_init ();
1667 BFD_ASSERT (ELF32_R_TYPE (dst->r_info) < (unsigned int) R_PPC_max);
1668 cache_ptr->howto = ppc_elf_howto_table[ELF32_R_TYPE (dst->r_info)];
1670 /* Just because the above assert didn't trigger doesn't mean that
1671 ELF32_R_TYPE (dst->r_info) is necessarily a valid relocation. */
1672 if (!cache_ptr->howto)
1674 (*_bfd_error_handler) (_("%B: invalid relocation type %d"),
1675 abfd, ELF32_R_TYPE (dst->r_info));
1676 bfd_set_error (bfd_error_bad_value);
1678 cache_ptr->howto = ppc_elf_howto_table[R_PPC_NONE];
1682 /* Handle the R_PPC_ADDR16_HA and R_PPC_REL16_HA relocs. */
1684 static bfd_reloc_status_type
1685 ppc_elf_addr16_ha_reloc (bfd *abfd ATTRIBUTE_UNUSED,
1686 arelent *reloc_entry,
1687 asymbol *symbol,
1688 void *data ATTRIBUTE_UNUSED,
1689 asection *input_section,
1690 bfd *output_bfd,
1691 char **error_message ATTRIBUTE_UNUSED)
1693 bfd_vma relocation;
1695 if (output_bfd != NULL)
1697 reloc_entry->address += input_section->output_offset;
1698 return bfd_reloc_ok;
1701 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
1702 return bfd_reloc_outofrange;
1704 if (bfd_is_com_section (symbol->section))
1705 relocation = 0;
1706 else
1707 relocation = symbol->value;
1709 relocation += symbol->section->output_section->vma;
1710 relocation += symbol->section->output_offset;
1711 relocation += reloc_entry->addend;
1712 if (reloc_entry->howto->pc_relative)
1713 relocation -= reloc_entry->address;
1715 reloc_entry->addend += (relocation & 0x8000) << 1;
1717 return bfd_reloc_continue;
1720 static bfd_reloc_status_type
1721 ppc_elf_unhandled_reloc (bfd *abfd,
1722 arelent *reloc_entry,
1723 asymbol *symbol,
1724 void *data,
1725 asection *input_section,
1726 bfd *output_bfd,
1727 char **error_message)
1729 /* If this is a relocatable link (output_bfd test tells us), just
1730 call the generic function. Any adjustment will be done at final
1731 link time. */
1732 if (output_bfd != NULL)
1733 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1734 input_section, output_bfd, error_message);
1736 if (error_message != NULL)
1738 static char buf[60];
1739 sprintf (buf, _("generic linker can't handle %s"),
1740 reloc_entry->howto->name);
1741 *error_message = buf;
1743 return bfd_reloc_dangerous;
1746 /* Sections created by the linker. */
1748 typedef struct elf_linker_section
1750 /* Pointer to the bfd section. */
1751 asection *section;
1752 /* Section name. */
1753 const char *name;
1754 /* Associated bss section name. */
1755 const char *bss_name;
1756 /* Associated symbol name. */
1757 const char *sym_name;
1758 /* Associated symbol. */
1759 struct elf_link_hash_entry *sym;
1760 } elf_linker_section_t;
1762 /* Linked list of allocated pointer entries. This hangs off of the
1763 symbol lists, and provides allows us to return different pointers,
1764 based on different addend's. */
1766 typedef struct elf_linker_section_pointers
1768 /* next allocated pointer for this symbol */
1769 struct elf_linker_section_pointers *next;
1770 /* offset of pointer from beginning of section */
1771 bfd_vma offset;
1772 /* addend used */
1773 bfd_vma addend;
1774 /* which linker section this is */
1775 elf_linker_section_t *lsect;
1776 } elf_linker_section_pointers_t;
1778 struct ppc_elf_obj_tdata
1780 struct elf_obj_tdata elf;
1782 /* A mapping from local symbols to offsets into the various linker
1783 sections added. This is index by the symbol index. */
1784 elf_linker_section_pointers_t **linker_section_pointers;
1786 /* Flags used to auto-detect plt type. */
1787 unsigned int makes_plt_call : 1;
1788 unsigned int has_rel16 : 1;
1791 #define ppc_elf_tdata(bfd) \
1792 ((struct ppc_elf_obj_tdata *) (bfd)->tdata.any)
1794 #define elf_local_ptr_offsets(bfd) \
1795 (ppc_elf_tdata (bfd)->linker_section_pointers)
1797 #define is_ppc_elf(bfd) \
1798 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
1799 && elf_object_id (bfd) == PPC32_ELF_DATA)
1801 /* Override the generic function because we store some extras. */
1803 static bfd_boolean
1804 ppc_elf_mkobject (bfd *abfd)
1806 return bfd_elf_allocate_object (abfd, sizeof (struct ppc_elf_obj_tdata),
1807 PPC32_ELF_DATA);
1810 /* Fix bad default arch selected for a 32 bit input bfd when the
1811 default is 64 bit. */
1813 static bfd_boolean
1814 ppc_elf_object_p (bfd *abfd)
1816 if (abfd->arch_info->the_default && abfd->arch_info->bits_per_word == 64)
1818 Elf_Internal_Ehdr *i_ehdr = elf_elfheader (abfd);
1820 if (i_ehdr->e_ident[EI_CLASS] == ELFCLASS32)
1822 /* Relies on arch after 64 bit default being 32 bit default. */
1823 abfd->arch_info = abfd->arch_info->next;
1824 BFD_ASSERT (abfd->arch_info->bits_per_word == 32);
1827 return TRUE;
1830 /* Function to set whether a module needs the -mrelocatable bit set. */
1832 static bfd_boolean
1833 ppc_elf_set_private_flags (bfd *abfd, flagword flags)
1835 BFD_ASSERT (!elf_flags_init (abfd)
1836 || elf_elfheader (abfd)->e_flags == flags);
1838 elf_elfheader (abfd)->e_flags = flags;
1839 elf_flags_init (abfd) = TRUE;
1840 return TRUE;
1843 /* Support for core dump NOTE sections. */
1845 static bfd_boolean
1846 ppc_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
1848 int offset;
1849 unsigned int size;
1851 switch (note->descsz)
1853 default:
1854 return FALSE;
1856 case 268: /* Linux/PPC. */
1857 /* pr_cursig */
1858 elf_tdata (abfd)->core_signal = bfd_get_16 (abfd, note->descdata + 12);
1860 /* pr_pid */
1861 elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, note->descdata + 24);
1863 /* pr_reg */
1864 offset = 72;
1865 size = 192;
1867 break;
1870 /* Make a ".reg/999" section. */
1871 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
1872 size, note->descpos + offset);
1875 static bfd_boolean
1876 ppc_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
1878 switch (note->descsz)
1880 default:
1881 return FALSE;
1883 case 128: /* Linux/PPC elf_prpsinfo. */
1884 elf_tdata (abfd)->core_program
1885 = _bfd_elfcore_strndup (abfd, note->descdata + 32, 16);
1886 elf_tdata (abfd)->core_command
1887 = _bfd_elfcore_strndup (abfd, note->descdata + 48, 80);
1890 /* Note that for some reason, a spurious space is tacked
1891 onto the end of the args in some (at least one anyway)
1892 implementations, so strip it off if it exists. */
1895 char *command = elf_tdata (abfd)->core_command;
1896 int n = strlen (command);
1898 if (0 < n && command[n - 1] == ' ')
1899 command[n - 1] = '\0';
1902 return TRUE;
1905 static char *
1906 ppc_elf_write_core_note (bfd *abfd, char *buf, int *bufsiz, int note_type, ...)
1908 switch (note_type)
1910 default:
1911 return NULL;
1913 case NT_PRPSINFO:
1915 char data[128];
1916 va_list ap;
1918 va_start (ap, note_type);
1919 memset (data, 0, 32);
1920 strncpy (data + 32, va_arg (ap, const char *), 16);
1921 strncpy (data + 48, va_arg (ap, const char *), 80);
1922 va_end (ap);
1923 return elfcore_write_note (abfd, buf, bufsiz,
1924 "CORE", note_type, data, sizeof (data));
1927 case NT_PRSTATUS:
1929 char data[268];
1930 va_list ap;
1931 long pid;
1932 int cursig;
1933 const void *greg;
1935 va_start (ap, note_type);
1936 memset (data, 0, 72);
1937 pid = va_arg (ap, long);
1938 bfd_put_32 (abfd, pid, data + 24);
1939 cursig = va_arg (ap, int);
1940 bfd_put_16 (abfd, cursig, data + 12);
1941 greg = va_arg (ap, const void *);
1942 memcpy (data + 72, greg, 192);
1943 memset (data + 264, 0, 4);
1944 va_end (ap);
1945 return elfcore_write_note (abfd, buf, bufsiz,
1946 "CORE", note_type, data, sizeof (data));
1951 /* Return address for Ith PLT stub in section PLT, for relocation REL
1952 or (bfd_vma) -1 if it should not be included. */
1954 static bfd_vma
1955 ppc_elf_plt_sym_val (bfd_vma i ATTRIBUTE_UNUSED,
1956 const asection *plt ATTRIBUTE_UNUSED,
1957 const arelent *rel)
1959 return rel->address;
1962 /* Handle a PowerPC specific section when reading an object file. This
1963 is called when bfd_section_from_shdr finds a section with an unknown
1964 type. */
1966 static bfd_boolean
1967 ppc_elf_section_from_shdr (bfd *abfd,
1968 Elf_Internal_Shdr *hdr,
1969 const char *name,
1970 int shindex)
1972 asection *newsect;
1973 flagword flags;
1975 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
1976 return FALSE;
1978 newsect = hdr->bfd_section;
1979 flags = bfd_get_section_flags (abfd, newsect);
1980 if (hdr->sh_flags & SHF_EXCLUDE)
1981 flags |= SEC_EXCLUDE;
1983 if (hdr->sh_type == SHT_ORDERED)
1984 flags |= SEC_SORT_ENTRIES;
1986 bfd_set_section_flags (abfd, newsect, flags);
1987 return TRUE;
1990 /* Set up any other section flags and such that may be necessary. */
1992 static bfd_boolean
1993 ppc_elf_fake_sections (bfd *abfd ATTRIBUTE_UNUSED,
1994 Elf_Internal_Shdr *shdr,
1995 asection *asect)
1997 if ((asect->flags & (SEC_GROUP | SEC_EXCLUDE)) == SEC_EXCLUDE)
1998 shdr->sh_flags |= SHF_EXCLUDE;
2000 if ((asect->flags & SEC_SORT_ENTRIES) != 0)
2001 shdr->sh_type = SHT_ORDERED;
2003 return TRUE;
2006 /* If we have .sbss2 or .PPC.EMB.sbss0 output sections, we
2007 need to bump up the number of section headers. */
2009 static int
2010 ppc_elf_additional_program_headers (bfd *abfd,
2011 struct bfd_link_info *info ATTRIBUTE_UNUSED)
2013 asection *s;
2014 int ret = 0;
2016 s = bfd_get_section_by_name (abfd, ".sbss2");
2017 if (s != NULL && (s->flags & SEC_ALLOC) != 0)
2018 ++ret;
2020 s = bfd_get_section_by_name (abfd, ".PPC.EMB.sbss0");
2021 if (s != NULL && (s->flags & SEC_ALLOC) != 0)
2022 ++ret;
2024 return ret;
2027 /* Add extra PPC sections -- Note, for now, make .sbss2 and
2028 .PPC.EMB.sbss0 a normal section, and not a bss section so
2029 that the linker doesn't crater when trying to make more than
2030 2 sections. */
2032 static const struct bfd_elf_special_section ppc_elf_special_sections[] =
2034 { STRING_COMMA_LEN (".plt"), 0, SHT_NOBITS, SHF_ALLOC + SHF_EXECINSTR },
2035 { STRING_COMMA_LEN (".sbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
2036 { STRING_COMMA_LEN (".sbss2"), -2, SHT_PROGBITS, SHF_ALLOC },
2037 { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2038 { STRING_COMMA_LEN (".sdata2"), -2, SHT_PROGBITS, SHF_ALLOC },
2039 { STRING_COMMA_LEN (".tags"), 0, SHT_ORDERED, SHF_ALLOC },
2040 { STRING_COMMA_LEN (".PPC.EMB.apuinfo"), 0, SHT_NOTE, 0 },
2041 { STRING_COMMA_LEN (".PPC.EMB.sbss0"), 0, SHT_PROGBITS, SHF_ALLOC },
2042 { STRING_COMMA_LEN (".PPC.EMB.sdata0"), 0, SHT_PROGBITS, SHF_ALLOC },
2043 { NULL, 0, 0, 0, 0 }
2046 /* This is what we want for new plt/got. */
2047 static struct bfd_elf_special_section ppc_alt_plt =
2048 { STRING_COMMA_LEN (".plt"), 0, SHT_PROGBITS, SHF_ALLOC };
2050 static const struct bfd_elf_special_section *
2051 ppc_elf_get_sec_type_attr (bfd *abfd ATTRIBUTE_UNUSED, asection *sec)
2053 const struct bfd_elf_special_section *ssect;
2055 /* See if this is one of the special sections. */
2056 if (sec->name == NULL)
2057 return NULL;
2059 ssect = _bfd_elf_get_special_section (sec->name, ppc_elf_special_sections,
2060 sec->use_rela_p);
2061 if (ssect != NULL)
2063 if (ssect == ppc_elf_special_sections && (sec->flags & SEC_LOAD) != 0)
2064 ssect = &ppc_alt_plt;
2065 return ssect;
2068 return _bfd_elf_get_sec_type_attr (abfd, sec);
2071 /* Very simple linked list structure for recording apuinfo values. */
2072 typedef struct apuinfo_list
2074 struct apuinfo_list *next;
2075 unsigned long value;
2077 apuinfo_list;
2079 static apuinfo_list *head;
2082 static void
2083 apuinfo_list_init (void)
2085 head = NULL;
2088 static void
2089 apuinfo_list_add (unsigned long value)
2091 apuinfo_list *entry = head;
2093 while (entry != NULL)
2095 if (entry->value == value)
2096 return;
2097 entry = entry->next;
2100 entry = bfd_malloc (sizeof (* entry));
2101 if (entry == NULL)
2102 return;
2104 entry->value = value;
2105 entry->next = head;
2106 head = entry;
2109 static unsigned
2110 apuinfo_list_length (void)
2112 apuinfo_list *entry;
2113 unsigned long count;
2115 for (entry = head, count = 0;
2116 entry;
2117 entry = entry->next)
2118 ++ count;
2120 return count;
2123 static inline unsigned long
2124 apuinfo_list_element (unsigned long number)
2126 apuinfo_list * entry;
2128 for (entry = head;
2129 entry && number --;
2130 entry = entry->next)
2133 return entry ? entry->value : 0;
2136 static void
2137 apuinfo_list_finish (void)
2139 apuinfo_list *entry;
2141 for (entry = head; entry;)
2143 apuinfo_list *next = entry->next;
2144 free (entry);
2145 entry = next;
2148 head = NULL;
2151 #define APUINFO_SECTION_NAME ".PPC.EMB.apuinfo"
2152 #define APUINFO_LABEL "APUinfo"
2154 /* Scan the input BFDs and create a linked list of
2155 the APUinfo values that will need to be emitted. */
2157 static void
2158 ppc_elf_begin_write_processing (bfd *abfd, struct bfd_link_info *link_info)
2160 bfd *ibfd;
2161 asection *asec;
2162 char *buffer = NULL;
2163 bfd_size_type largest_input_size = 0;
2164 unsigned i;
2165 unsigned num_entries;
2166 unsigned long length;
2167 const char *error_message = NULL;
2169 if (link_info == NULL)
2170 return;
2172 apuinfo_list_init ();
2174 /* Read in the input sections contents. */
2175 for (ibfd = link_info->input_bfds; ibfd; ibfd = ibfd->link_next)
2177 unsigned long datum;
2179 asec = bfd_get_section_by_name (ibfd, APUINFO_SECTION_NAME);
2180 if (asec == NULL)
2181 continue;
2183 error_message = _("corrupt %s section in %B");
2184 length = asec->size;
2185 if (length < 20)
2186 goto fail;
2188 if (largest_input_size < asec->size)
2190 if (buffer)
2191 free (buffer);
2192 largest_input_size = asec->size;
2193 buffer = bfd_malloc (largest_input_size);
2194 if (!buffer)
2195 return;
2198 if (bfd_seek (ibfd, asec->filepos, SEEK_SET) != 0
2199 || (bfd_bread (buffer, length, ibfd) != length))
2201 error_message = _("unable to read in %s section from %B");
2202 goto fail;
2205 /* Verify the contents of the header. Note - we have to
2206 extract the values this way in order to allow for a
2207 host whose endian-ness is different from the target. */
2208 datum = bfd_get_32 (ibfd, buffer);
2209 if (datum != sizeof APUINFO_LABEL)
2210 goto fail;
2212 datum = bfd_get_32 (ibfd, buffer + 8);
2213 if (datum != 0x2)
2214 goto fail;
2216 if (strcmp (buffer + 12, APUINFO_LABEL) != 0)
2217 goto fail;
2219 /* Get the number of bytes used for apuinfo entries. */
2220 datum = bfd_get_32 (ibfd, buffer + 4);
2221 if (datum + 20 != length)
2222 goto fail;
2224 /* Scan the apuinfo section, building a list of apuinfo numbers. */
2225 for (i = 0; i < datum; i += 4)
2226 apuinfo_list_add (bfd_get_32 (ibfd, buffer + 20 + i));
2229 error_message = NULL;
2231 /* Compute the size of the output section. */
2232 num_entries = apuinfo_list_length ();
2234 if (num_entries)
2236 /* Set the output section size, if it exists. */
2237 asec = bfd_get_section_by_name (abfd, APUINFO_SECTION_NAME);
2238 if (asec && ! bfd_set_section_size (abfd, asec, 20 + num_entries * 4))
2240 ibfd = abfd;
2241 error_message = _("warning: unable to set size of %s section in %B");
2245 fail:
2246 if (buffer)
2247 free (buffer);
2249 if (error_message)
2250 (*_bfd_error_handler) (error_message, ibfd, APUINFO_SECTION_NAME);
2253 /* Prevent the output section from accumulating the input sections'
2254 contents. We have already stored this in our linked list structure. */
2256 static bfd_boolean
2257 ppc_elf_write_section (bfd *abfd ATTRIBUTE_UNUSED,
2258 struct bfd_link_info *link_info ATTRIBUTE_UNUSED,
2259 asection *asec,
2260 bfd_byte *contents ATTRIBUTE_UNUSED)
2262 return (apuinfo_list_length ()
2263 && strcmp (asec->name, APUINFO_SECTION_NAME) == 0);
2266 /* Finally we can generate the output section. */
2268 static void
2269 ppc_elf_final_write_processing (bfd *abfd, bfd_boolean linker ATTRIBUTE_UNUSED)
2271 bfd_byte *buffer;
2272 asection *asec;
2273 unsigned i;
2274 unsigned num_entries;
2275 bfd_size_type length;
2277 asec = bfd_get_section_by_name (abfd, APUINFO_SECTION_NAME);
2278 if (asec == NULL)
2279 return;
2281 if (apuinfo_list_length () == 0)
2282 return;
2284 length = asec->size;
2285 if (length < 20)
2286 return;
2288 buffer = bfd_malloc (length);
2289 if (buffer == NULL)
2291 (*_bfd_error_handler)
2292 (_("failed to allocate space for new APUinfo section."));
2293 return;
2296 /* Create the apuinfo header. */
2297 num_entries = apuinfo_list_length ();
2298 bfd_put_32 (abfd, sizeof APUINFO_LABEL, buffer);
2299 bfd_put_32 (abfd, num_entries * 4, buffer + 4);
2300 bfd_put_32 (abfd, 0x2, buffer + 8);
2301 strcpy ((char *) buffer + 12, APUINFO_LABEL);
2303 length = 20;
2304 for (i = 0; i < num_entries; i++)
2306 bfd_put_32 (abfd, apuinfo_list_element (i), buffer + length);
2307 length += 4;
2310 if (length != asec->size)
2311 (*_bfd_error_handler) (_("failed to compute new APUinfo section."));
2313 if (! bfd_set_section_contents (abfd, asec, buffer, (file_ptr) 0, length))
2314 (*_bfd_error_handler) (_("failed to install new APUinfo section."));
2316 free (buffer);
2318 apuinfo_list_finish ();
2321 static bfd_boolean
2322 is_nonpic_glink_stub (bfd *abfd, asection *glink, bfd_vma off)
2324 bfd_byte buf[GLINK_ENTRY_SIZE];
2326 if (!bfd_get_section_contents (abfd, glink, buf, off, GLINK_ENTRY_SIZE))
2327 return FALSE;
2329 return ((bfd_get_32 (abfd, buf + 0) & 0xffff0000) == LIS_11
2330 && (bfd_get_32 (abfd, buf + 4) & 0xffff0000) == LWZ_11_11
2331 && bfd_get_32 (abfd, buf + 8) == MTCTR_11
2332 && bfd_get_32 (abfd, buf + 12) == BCTR);
2335 static bfd_boolean
2336 section_covers_vma (bfd *abfd ATTRIBUTE_UNUSED, asection *section, void *ptr)
2338 bfd_vma vma = *(bfd_vma *) ptr;
2339 return ((section->flags & SEC_ALLOC) != 0
2340 && section->vma <= vma
2341 && vma < section->vma + section->size);
2344 static long
2345 ppc_elf_get_synthetic_symtab (bfd *abfd, long symcount, asymbol **syms,
2346 long dynsymcount, asymbol **dynsyms,
2347 asymbol **ret)
2349 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
2350 asection *plt, *relplt, *dynamic, *glink;
2351 bfd_vma glink_vma = 0;
2352 bfd_vma resolv_vma = 0;
2353 bfd_vma stub_vma;
2354 asymbol *s;
2355 arelent *p;
2356 long count, i;
2357 size_t size;
2358 char *names;
2359 bfd_byte buf[4];
2361 *ret = NULL;
2363 if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0)
2364 return 0;
2366 if (dynsymcount <= 0)
2367 return 0;
2369 relplt = bfd_get_section_by_name (abfd, ".rela.plt");
2370 if (relplt == NULL)
2371 return 0;
2373 plt = bfd_get_section_by_name (abfd, ".plt");
2374 if (plt == NULL)
2375 return 0;
2377 /* Call common code to handle old-style executable PLTs. */
2378 if (elf_section_flags (plt) & SHF_EXECINSTR)
2379 return _bfd_elf_get_synthetic_symtab (abfd, symcount, syms,
2380 dynsymcount, dynsyms, ret);
2382 /* If this object was prelinked, the prelinker stored the address
2383 of .glink at got[1]. If it wasn't prelinked, got[1] will be zero. */
2384 dynamic = bfd_get_section_by_name (abfd, ".dynamic");
2385 if (dynamic != NULL)
2387 bfd_byte *dynbuf, *extdyn, *extdynend;
2388 size_t extdynsize;
2389 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
2391 if (!bfd_malloc_and_get_section (abfd, dynamic, &dynbuf))
2392 return -1;
2394 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
2395 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
2397 extdyn = dynbuf;
2398 extdynend = extdyn + dynamic->size;
2399 for (; extdyn < extdynend; extdyn += extdynsize)
2401 Elf_Internal_Dyn dyn;
2402 (*swap_dyn_in) (abfd, extdyn, &dyn);
2404 if (dyn.d_tag == DT_NULL)
2405 break;
2407 if (dyn.d_tag == DT_PPC_GOT)
2409 unsigned int g_o_t = dyn.d_un.d_val;
2410 asection *got = bfd_get_section_by_name (abfd, ".got");
2411 if (got != NULL
2412 && bfd_get_section_contents (abfd, got, buf,
2413 g_o_t - got->vma + 4, 4))
2414 glink_vma = bfd_get_32 (abfd, buf);
2415 break;
2418 free (dynbuf);
2421 /* Otherwise we read the first plt entry. */
2422 if (glink_vma == 0)
2424 if (bfd_get_section_contents (abfd, plt, buf, 0, 4))
2425 glink_vma = bfd_get_32 (abfd, buf);
2428 if (glink_vma == 0)
2429 return 0;
2431 /* The .glink section usually does not survive the final
2432 link; search for the section (usually .text) where the
2433 glink stubs now reside. */
2434 glink = bfd_sections_find_if (abfd, section_covers_vma, &glink_vma);
2435 if (glink == NULL)
2436 return 0;
2438 /* Determine glink PLT resolver by reading the relative branch
2439 from the first glink stub. */
2440 if (bfd_get_section_contents (abfd, glink, buf,
2441 glink_vma - glink->vma, 4))
2443 unsigned int insn = bfd_get_32 (abfd, buf);
2445 /* The first glink stub may either branch to the resolver ... */
2446 insn ^= B;
2447 if ((insn & ~0x3fffffc) == 0)
2448 resolv_vma = glink_vma + (insn ^ 0x2000000) - 0x2000000;
2450 /* ... or fall through a bunch of NOPs. */
2451 else if ((insn ^ B ^ NOP) == 0)
2452 for (i = 4;
2453 bfd_get_section_contents (abfd, glink, buf,
2454 glink_vma - glink->vma + i, 4);
2455 i += 4)
2456 if (bfd_get_32 (abfd, buf) != NOP)
2458 resolv_vma = glink_vma + i;
2459 break;
2463 count = relplt->size / sizeof (Elf32_External_Rela);
2464 stub_vma = glink_vma - (bfd_vma) count * 16;
2465 /* If the stubs are those for -shared/-pie then we might have
2466 multiple stubs for each plt entry. If that is the case then
2467 there is no way to associate stubs with their plt entries short
2468 of figuring out the GOT pointer value used in the stub. */
2469 if (!is_nonpic_glink_stub (abfd, glink,
2470 glink_vma - GLINK_ENTRY_SIZE - glink->vma))
2471 return 0;
2473 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
2474 if (! (*slurp_relocs) (abfd, relplt, dynsyms, TRUE))
2475 return -1;
2477 size = count * sizeof (asymbol);
2478 p = relplt->relocation;
2479 for (i = 0; i < count; i++, p++)
2481 size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
2482 if (p->addend != 0)
2483 size += sizeof ("+0x") - 1 + 8;
2486 size += sizeof (asymbol) + sizeof ("__glink");
2488 if (resolv_vma)
2489 size += sizeof (asymbol) + sizeof ("__glink_PLTresolve");
2491 s = *ret = bfd_malloc (size);
2492 if (s == NULL)
2493 return -1;
2495 names = (char *) (s + count + 1 + (resolv_vma != 0));
2496 p = relplt->relocation;
2497 for (i = 0; i < count; i++, p++)
2499 size_t len;
2501 *s = **p->sym_ptr_ptr;
2502 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
2503 we are defining a symbol, ensure one of them is set. */
2504 if ((s->flags & BSF_LOCAL) == 0)
2505 s->flags |= BSF_GLOBAL;
2506 s->flags |= BSF_SYNTHETIC;
2507 s->section = glink;
2508 s->value = stub_vma - glink->vma;
2509 s->name = names;
2510 s->udata.p = NULL;
2511 len = strlen ((*p->sym_ptr_ptr)->name);
2512 memcpy (names, (*p->sym_ptr_ptr)->name, len);
2513 names += len;
2514 if (p->addend != 0)
2516 memcpy (names, "+0x", sizeof ("+0x") - 1);
2517 names += sizeof ("+0x") - 1;
2518 bfd_sprintf_vma (abfd, names, p->addend);
2519 names += strlen (names);
2521 memcpy (names, "@plt", sizeof ("@plt"));
2522 names += sizeof ("@plt");
2523 ++s;
2524 stub_vma += 16;
2527 /* Add a symbol at the start of the glink branch table. */
2528 memset (s, 0, sizeof *s);
2529 s->the_bfd = abfd;
2530 s->flags = BSF_GLOBAL | BSF_SYNTHETIC;
2531 s->section = glink;
2532 s->value = glink_vma - glink->vma;
2533 s->name = names;
2534 memcpy (names, "__glink", sizeof ("__glink"));
2535 names += sizeof ("__glink");
2536 s++;
2537 count++;
2539 if (resolv_vma)
2541 /* Add a symbol for the glink PLT resolver. */
2542 memset (s, 0, sizeof *s);
2543 s->the_bfd = abfd;
2544 s->flags = BSF_GLOBAL | BSF_SYNTHETIC;
2545 s->section = glink;
2546 s->value = resolv_vma - glink->vma;
2547 s->name = names;
2548 memcpy (names, "__glink_PLTresolve", sizeof ("__glink_PLTresolve"));
2549 names += sizeof ("__glink_PLTresolve");
2550 s++;
2551 count++;
2554 return count;
2557 /* The following functions are specific to the ELF linker, while
2558 functions above are used generally. They appear in this file more
2559 or less in the order in which they are called. eg.
2560 ppc_elf_check_relocs is called early in the link process,
2561 ppc_elf_finish_dynamic_sections is one of the last functions
2562 called. */
2564 /* The PPC linker needs to keep track of the number of relocs that it
2565 decides to copy as dynamic relocs in check_relocs for each symbol.
2566 This is so that it can later discard them if they are found to be
2567 unnecessary. We store the information in a field extending the
2568 regular ELF linker hash table. */
2570 struct ppc_elf_dyn_relocs
2572 struct ppc_elf_dyn_relocs *next;
2574 /* The input section of the reloc. */
2575 asection *sec;
2577 /* Total number of relocs copied for the input section. */
2578 bfd_size_type count;
2580 /* Number of pc-relative relocs copied for the input section. */
2581 bfd_size_type pc_count;
2584 /* Track PLT entries needed for a given symbol. We might need more
2585 than one glink entry per symbol when generating a pic binary. */
2586 struct plt_entry
2588 struct plt_entry *next;
2590 /* -fPIC uses multiple GOT sections, one per file, called ".got2".
2591 This field stores the offset into .got2 used to initialise the
2592 GOT pointer reg. It will always be at least 32768. (Current
2593 gcc always uses an offset of 32768, but ld -r will pack .got2
2594 sections together resulting in larger offsets). */
2595 bfd_vma addend;
2597 /* The .got2 section. */
2598 asection *sec;
2600 /* PLT refcount or offset. */
2601 union
2603 bfd_signed_vma refcount;
2604 bfd_vma offset;
2605 } plt;
2607 /* .glink stub offset. */
2608 bfd_vma glink_offset;
2611 /* Of those relocs that might be copied as dynamic relocs, this function
2612 selects those that must be copied when linking a shared library,
2613 even when the symbol is local. */
2615 static int
2616 must_be_dyn_reloc (struct bfd_link_info *info,
2617 enum elf_ppc_reloc_type r_type)
2619 switch (r_type)
2621 default:
2622 return 1;
2624 case R_PPC_REL24:
2625 case R_PPC_REL14:
2626 case R_PPC_REL14_BRTAKEN:
2627 case R_PPC_REL14_BRNTAKEN:
2628 case R_PPC_REL32:
2629 return 0;
2631 case R_PPC_TPREL32:
2632 case R_PPC_TPREL16:
2633 case R_PPC_TPREL16_LO:
2634 case R_PPC_TPREL16_HI:
2635 case R_PPC_TPREL16_HA:
2636 return !info->executable;
2640 /* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
2641 copying dynamic variables from a shared lib into an app's dynbss
2642 section, and instead use a dynamic relocation to point into the
2643 shared lib. */
2644 #define ELIMINATE_COPY_RELOCS 1
2646 /* PPC ELF linker hash entry. */
2648 struct ppc_elf_link_hash_entry
2650 struct elf_link_hash_entry elf;
2652 /* If this symbol is used in the linker created sections, the processor
2653 specific backend uses this field to map the field into the offset
2654 from the beginning of the section. */
2655 elf_linker_section_pointers_t *linker_section_pointer;
2657 /* Track dynamic relocs copied for this symbol. */
2658 struct ppc_elf_dyn_relocs *dyn_relocs;
2660 /* Contexts in which symbol is used in the GOT (or TOC).
2661 TLS_GD .. TLS_TLS bits are or'd into the mask as the
2662 corresponding relocs are encountered during check_relocs.
2663 tls_optimize clears TLS_GD .. TLS_TPREL when optimizing to
2664 indicate the corresponding GOT entry type is not needed. */
2665 #define TLS_GD 1 /* GD reloc. */
2666 #define TLS_LD 2 /* LD reloc. */
2667 #define TLS_TPREL 4 /* TPREL reloc, => IE. */
2668 #define TLS_DTPREL 8 /* DTPREL reloc, => LD. */
2669 #define TLS_TLS 16 /* Any TLS reloc. */
2670 #define TLS_TPRELGD 32 /* TPREL reloc resulting from GD->IE. */
2671 #define PLT_IFUNC 64 /* STT_GNU_IFUNC. */
2672 char tls_mask;
2674 /* Nonzero if we have seen a small data relocation referring to this
2675 symbol. */
2676 unsigned char has_sda_refs;
2679 #define ppc_elf_hash_entry(ent) ((struct ppc_elf_link_hash_entry *) (ent))
2681 /* PPC ELF linker hash table. */
2683 struct ppc_elf_link_hash_table
2685 struct elf_link_hash_table elf;
2687 /* Short-cuts to get to dynamic linker sections. */
2688 asection *got;
2689 asection *relgot;
2690 asection *glink;
2691 asection *plt;
2692 asection *relplt;
2693 asection *iplt;
2694 asection *reliplt;
2695 asection *dynbss;
2696 asection *relbss;
2697 asection *dynsbss;
2698 asection *relsbss;
2699 elf_linker_section_t sdata[2];
2700 asection *sbss;
2702 /* The (unloaded but important) .rela.plt.unloaded on VxWorks. */
2703 asection *srelplt2;
2705 /* The .got.plt section (VxWorks only)*/
2706 asection *sgotplt;
2708 /* Shortcut to __tls_get_addr. */
2709 struct elf_link_hash_entry *tls_get_addr;
2711 /* The bfd that forced an old-style PLT. */
2712 bfd *old_bfd;
2714 /* TLS local dynamic got entry handling. */
2715 union {
2716 bfd_signed_vma refcount;
2717 bfd_vma offset;
2718 } tlsld_got;
2720 /* Offset of branch table to PltResolve function in glink. */
2721 bfd_vma glink_pltresolve;
2723 /* Size of reserved GOT entries. */
2724 unsigned int got_header_size;
2725 /* Non-zero if allocating the header left a gap. */
2726 unsigned int got_gap;
2728 /* The type of PLT we have chosen to use. */
2729 enum ppc_elf_plt_type plt_type;
2731 /* Set if we should emit symbols for stubs. */
2732 unsigned int emit_stub_syms:1;
2734 /* Set if __tls_get_addr optimization should not be done. */
2735 unsigned int no_tls_get_addr_opt:1;
2737 /* True if the target system is VxWorks. */
2738 unsigned int is_vxworks:1;
2740 /* The size of PLT entries. */
2741 int plt_entry_size;
2742 /* The distance between adjacent PLT slots. */
2743 int plt_slot_size;
2744 /* The size of the first PLT entry. */
2745 int plt_initial_entry_size;
2747 /* Small local sym cache. */
2748 struct sym_cache sym_cache;
2751 /* Rename some of the generic section flags to better document how they
2752 are used here. */
2754 /* Nonzero if this section has TLS related relocations. */
2755 #define has_tls_reloc sec_flg0
2757 /* Nonzero if this section has a call to __tls_get_addr. */
2758 #define has_tls_get_addr_call sec_flg1
2760 /* Get the PPC ELF linker hash table from a link_info structure. */
2762 #define ppc_elf_hash_table(p) \
2763 (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
2764 == PPC32_ELF_DATA ? ((struct ppc_elf_link_hash_table *) ((p)->hash)) : NULL)
2766 /* Create an entry in a PPC ELF linker hash table. */
2768 static struct bfd_hash_entry *
2769 ppc_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
2770 struct bfd_hash_table *table,
2771 const char *string)
2773 /* Allocate the structure if it has not already been allocated by a
2774 subclass. */
2775 if (entry == NULL)
2777 entry = bfd_hash_allocate (table,
2778 sizeof (struct ppc_elf_link_hash_entry));
2779 if (entry == NULL)
2780 return entry;
2783 /* Call the allocation method of the superclass. */
2784 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
2785 if (entry != NULL)
2787 ppc_elf_hash_entry (entry)->linker_section_pointer = NULL;
2788 ppc_elf_hash_entry (entry)->dyn_relocs = NULL;
2789 ppc_elf_hash_entry (entry)->tls_mask = 0;
2792 return entry;
2795 /* Create a PPC ELF linker hash table. */
2797 static struct bfd_link_hash_table *
2798 ppc_elf_link_hash_table_create (bfd *abfd)
2800 struct ppc_elf_link_hash_table *ret;
2802 ret = bfd_zmalloc (sizeof (struct ppc_elf_link_hash_table));
2803 if (ret == NULL)
2804 return NULL;
2806 if (!_bfd_elf_link_hash_table_init (&ret->elf, abfd,
2807 ppc_elf_link_hash_newfunc,
2808 sizeof (struct ppc_elf_link_hash_entry),
2809 PPC32_ELF_DATA))
2811 free (ret);
2812 return NULL;
2815 ret->elf.init_plt_refcount.refcount = 0;
2816 ret->elf.init_plt_refcount.glist = NULL;
2817 ret->elf.init_plt_offset.offset = 0;
2818 ret->elf.init_plt_offset.glist = NULL;
2820 ret->sdata[0].name = ".sdata";
2821 ret->sdata[0].sym_name = "_SDA_BASE_";
2822 ret->sdata[0].bss_name = ".sbss";
2824 ret->sdata[1].name = ".sdata2";
2825 ret->sdata[1].sym_name = "_SDA2_BASE_";
2826 ret->sdata[1].bss_name = ".sbss2";
2828 ret->plt_entry_size = 12;
2829 ret->plt_slot_size = 8;
2830 ret->plt_initial_entry_size = 72;
2832 return &ret->elf.root;
2835 /* Create .got and the related sections. */
2837 static bfd_boolean
2838 ppc_elf_create_got (bfd *abfd, struct bfd_link_info *info)
2840 struct ppc_elf_link_hash_table *htab;
2841 asection *s;
2842 flagword flags;
2844 if (!_bfd_elf_create_got_section (abfd, info))
2845 return FALSE;
2847 htab = ppc_elf_hash_table (info);
2848 htab->got = s = bfd_get_section_by_name (abfd, ".got");
2849 if (s == NULL)
2850 abort ();
2852 if (htab->is_vxworks)
2854 htab->sgotplt = bfd_get_section_by_name (abfd, ".got.plt");
2855 if (!htab->sgotplt)
2856 abort ();
2858 else
2860 /* The powerpc .got has a blrl instruction in it. Mark it
2861 executable. */
2862 flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_HAS_CONTENTS
2863 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
2864 if (!bfd_set_section_flags (abfd, s, flags))
2865 return FALSE;
2868 htab->relgot = bfd_get_section_by_name (abfd, ".rela.got");
2869 if (!htab->relgot)
2870 abort ();
2872 return TRUE;
2875 static bfd_boolean
2876 ppc_elf_create_glink (bfd *abfd, struct bfd_link_info *info)
2878 struct ppc_elf_link_hash_table *htab = ppc_elf_hash_table (info);
2879 asection *s;
2880 flagword flags;
2882 flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY | SEC_HAS_CONTENTS
2883 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
2884 s = bfd_make_section_anyway_with_flags (abfd, ".glink", flags);
2885 htab->glink = s;
2886 if (s == NULL
2887 || !bfd_set_section_alignment (abfd, s, 4))
2888 return FALSE;
2890 flags = SEC_ALLOC | SEC_LINKER_CREATED;
2891 s = bfd_make_section_anyway_with_flags (abfd, ".iplt", flags);
2892 htab->iplt = s;
2893 if (s == NULL
2894 || !bfd_set_section_alignment (abfd, s, 4))
2895 return FALSE;
2897 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_HAS_CONTENTS
2898 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
2899 s = bfd_make_section_with_flags (abfd, ".rela.iplt", flags);
2900 htab->reliplt = s;
2901 if (s == NULL
2902 || ! bfd_set_section_alignment (abfd, s, 2))
2903 return FALSE;
2904 return TRUE;
2907 /* We have to create .dynsbss and .rela.sbss here so that they get mapped
2908 to output sections (just like _bfd_elf_create_dynamic_sections has
2909 to create .dynbss and .rela.bss). */
2911 static bfd_boolean
2912 ppc_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
2914 struct ppc_elf_link_hash_table *htab;
2915 asection *s;
2916 flagword flags;
2918 htab = ppc_elf_hash_table (info);
2920 if (htab->got == NULL
2921 && !ppc_elf_create_got (abfd, info))
2922 return FALSE;
2924 if (!_bfd_elf_create_dynamic_sections (abfd, info))
2925 return FALSE;
2927 if (htab->glink == NULL
2928 && !ppc_elf_create_glink (abfd, info))
2929 return FALSE;
2931 htab->dynbss = bfd_get_section_by_name (abfd, ".dynbss");
2932 s = bfd_make_section_with_flags (abfd, ".dynsbss",
2933 SEC_ALLOC | SEC_LINKER_CREATED);
2934 htab->dynsbss = s;
2935 if (s == NULL)
2936 return FALSE;
2938 if (! info->shared)
2940 htab->relbss = bfd_get_section_by_name (abfd, ".rela.bss");
2941 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_HAS_CONTENTS
2942 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
2943 s = bfd_make_section_with_flags (abfd, ".rela.sbss", flags);
2944 htab->relsbss = s;
2945 if (s == NULL
2946 || ! bfd_set_section_alignment (abfd, s, 2))
2947 return FALSE;
2950 if (htab->is_vxworks
2951 && !elf_vxworks_create_dynamic_sections (abfd, info, &htab->srelplt2))
2952 return FALSE;
2954 htab->relplt = bfd_get_section_by_name (abfd, ".rela.plt");
2955 htab->plt = s = bfd_get_section_by_name (abfd, ".plt");
2956 if (s == NULL)
2957 abort ();
2959 flags = SEC_ALLOC | SEC_CODE | SEC_LINKER_CREATED;
2960 if (htab->plt_type == PLT_VXWORKS)
2961 /* The VxWorks PLT is a loaded section with contents. */
2962 flags |= SEC_HAS_CONTENTS | SEC_LOAD | SEC_READONLY;
2963 return bfd_set_section_flags (abfd, s, flags);
2966 /* Copy the extra info we tack onto an elf_link_hash_entry. */
2968 static void
2969 ppc_elf_copy_indirect_symbol (struct bfd_link_info *info,
2970 struct elf_link_hash_entry *dir,
2971 struct elf_link_hash_entry *ind)
2973 struct ppc_elf_link_hash_entry *edir, *eind;
2975 edir = (struct ppc_elf_link_hash_entry *) dir;
2976 eind = (struct ppc_elf_link_hash_entry *) ind;
2978 if (eind->dyn_relocs != NULL)
2980 if (edir->dyn_relocs != NULL)
2982 struct ppc_elf_dyn_relocs **pp;
2983 struct ppc_elf_dyn_relocs *p;
2985 /* Add reloc counts against the indirect sym to the direct sym
2986 list. Merge any entries against the same section. */
2987 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
2989 struct ppc_elf_dyn_relocs *q;
2991 for (q = edir->dyn_relocs; q != NULL; q = q->next)
2992 if (q->sec == p->sec)
2994 q->pc_count += p->pc_count;
2995 q->count += p->count;
2996 *pp = p->next;
2997 break;
2999 if (q == NULL)
3000 pp = &p->next;
3002 *pp = edir->dyn_relocs;
3005 edir->dyn_relocs = eind->dyn_relocs;
3006 eind->dyn_relocs = NULL;
3009 edir->tls_mask |= eind->tls_mask;
3010 edir->has_sda_refs |= eind->has_sda_refs;
3012 /* If called to transfer flags for a weakdef during processing
3013 of elf_adjust_dynamic_symbol, don't copy non_got_ref.
3014 We clear it ourselves for ELIMINATE_COPY_RELOCS. */
3015 if (!(ELIMINATE_COPY_RELOCS
3016 && eind->elf.root.type != bfd_link_hash_indirect
3017 && edir->elf.dynamic_adjusted))
3018 edir->elf.non_got_ref |= eind->elf.non_got_ref;
3020 edir->elf.ref_dynamic |= eind->elf.ref_dynamic;
3021 edir->elf.ref_regular |= eind->elf.ref_regular;
3022 edir->elf.ref_regular_nonweak |= eind->elf.ref_regular_nonweak;
3023 edir->elf.needs_plt |= eind->elf.needs_plt;
3024 edir->elf.pointer_equality_needed |= eind->elf.pointer_equality_needed;
3026 /* If we were called to copy over info for a weak sym, that's all. */
3027 if (eind->elf.root.type != bfd_link_hash_indirect)
3028 return;
3030 /* Copy over the GOT refcount entries that we may have already seen to
3031 the symbol which just became indirect. */
3032 edir->elf.got.refcount += eind->elf.got.refcount;
3033 eind->elf.got.refcount = 0;
3035 /* And plt entries. */
3036 if (eind->elf.plt.plist != NULL)
3038 if (edir->elf.plt.plist != NULL)
3040 struct plt_entry **entp;
3041 struct plt_entry *ent;
3043 for (entp = &eind->elf.plt.plist; (ent = *entp) != NULL; )
3045 struct plt_entry *dent;
3047 for (dent = edir->elf.plt.plist; dent != NULL; dent = dent->next)
3048 if (dent->sec == ent->sec && dent->addend == ent->addend)
3050 dent->plt.refcount += ent->plt.refcount;
3051 *entp = ent->next;
3052 break;
3054 if (dent == NULL)
3055 entp = &ent->next;
3057 *entp = edir->elf.plt.plist;
3060 edir->elf.plt.plist = eind->elf.plt.plist;
3061 eind->elf.plt.plist = NULL;
3064 if (eind->elf.dynindx != -1)
3066 if (edir->elf.dynindx != -1)
3067 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
3068 edir->elf.dynstr_index);
3069 edir->elf.dynindx = eind->elf.dynindx;
3070 edir->elf.dynstr_index = eind->elf.dynstr_index;
3071 eind->elf.dynindx = -1;
3072 eind->elf.dynstr_index = 0;
3076 /* Hook called by the linker routine which adds symbols from an object
3077 file. We use it to put .comm items in .sbss, and not .bss. */
3079 static bfd_boolean
3080 ppc_elf_add_symbol_hook (bfd *abfd,
3081 struct bfd_link_info *info,
3082 Elf_Internal_Sym *sym,
3083 const char **namep ATTRIBUTE_UNUSED,
3084 flagword *flagsp ATTRIBUTE_UNUSED,
3085 asection **secp,
3086 bfd_vma *valp)
3088 if (sym->st_shndx == SHN_COMMON
3089 && !info->relocatable
3090 && is_ppc_elf (info->output_bfd)
3091 && sym->st_size <= elf_gp_size (abfd))
3093 /* Common symbols less than or equal to -G nn bytes are automatically
3094 put into .sbss. */
3095 struct ppc_elf_link_hash_table *htab;
3097 htab = ppc_elf_hash_table (info);
3098 if (htab->sbss == NULL)
3100 flagword flags = SEC_IS_COMMON | SEC_LINKER_CREATED;
3102 if (!htab->elf.dynobj)
3103 htab->elf.dynobj = abfd;
3105 htab->sbss = bfd_make_section_anyway_with_flags (htab->elf.dynobj,
3106 ".sbss",
3107 flags);
3108 if (htab->sbss == NULL)
3109 return FALSE;
3112 *secp = htab->sbss;
3113 *valp = sym->st_size;
3116 if ((abfd->flags & DYNAMIC) == 0
3117 && ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
3118 elf_tdata (info->output_bfd)->has_ifunc_symbols = TRUE;
3120 return TRUE;
3123 static bfd_boolean
3124 create_sdata_sym (struct bfd_link_info *info, elf_linker_section_t *lsect)
3126 struct ppc_elf_link_hash_table *htab = ppc_elf_hash_table (info);
3128 lsect->sym = elf_link_hash_lookup (&htab->elf, lsect->sym_name,
3129 TRUE, FALSE, TRUE);
3130 if (lsect->sym == NULL)
3131 return FALSE;
3132 if (lsect->sym->root.type == bfd_link_hash_new)
3133 lsect->sym->non_elf = 0;
3134 lsect->sym->ref_regular = 1;
3135 _bfd_elf_link_hash_hide_symbol (info, lsect->sym, TRUE);
3136 return TRUE;
3139 /* Create a special linker section. */
3141 static bfd_boolean
3142 ppc_elf_create_linker_section (bfd *abfd,
3143 struct bfd_link_info *info,
3144 flagword flags,
3145 elf_linker_section_t *lsect)
3147 struct ppc_elf_link_hash_table *htab = ppc_elf_hash_table (info);
3148 asection *s;
3150 flags |= (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
3151 | SEC_LINKER_CREATED);
3153 /* Record the first bfd that needs the special sections. */
3154 if (!htab->elf.dynobj)
3155 htab->elf.dynobj = abfd;
3157 s = bfd_make_section_anyway_with_flags (htab->elf.dynobj,
3158 lsect->name,
3159 flags);
3160 if (s == NULL
3161 || !bfd_set_section_alignment (htab->elf.dynobj, s, 2))
3162 return FALSE;
3163 lsect->section = s;
3165 return create_sdata_sym (info, lsect);
3168 /* Find a linker generated pointer with a given addend and type. */
3170 static elf_linker_section_pointers_t *
3171 elf_find_pointer_linker_section
3172 (elf_linker_section_pointers_t *linker_pointers,
3173 bfd_vma addend,
3174 elf_linker_section_t *lsect)
3176 for ( ; linker_pointers != NULL; linker_pointers = linker_pointers->next)
3177 if (lsect == linker_pointers->lsect && addend == linker_pointers->addend)
3178 return linker_pointers;
3180 return NULL;
3183 /* Allocate a pointer to live in a linker created section. */
3185 static bfd_boolean
3186 elf_create_pointer_linker_section (bfd *abfd,
3187 elf_linker_section_t *lsect,
3188 struct elf_link_hash_entry *h,
3189 const Elf_Internal_Rela *rel)
3191 elf_linker_section_pointers_t **ptr_linker_section_ptr = NULL;
3192 elf_linker_section_pointers_t *linker_section_ptr;
3193 unsigned long r_symndx = ELF32_R_SYM (rel->r_info);
3194 bfd_size_type amt;
3196 BFD_ASSERT (lsect != NULL);
3198 /* Is this a global symbol? */
3199 if (h != NULL)
3201 struct ppc_elf_link_hash_entry *eh;
3203 /* Has this symbol already been allocated? If so, our work is done. */
3204 eh = (struct ppc_elf_link_hash_entry *) h;
3205 if (elf_find_pointer_linker_section (eh->linker_section_pointer,
3206 rel->r_addend,
3207 lsect))
3208 return TRUE;
3210 ptr_linker_section_ptr = &eh->linker_section_pointer;
3212 else
3214 BFD_ASSERT (is_ppc_elf (abfd));
3216 /* Allocation of a pointer to a local symbol. */
3217 elf_linker_section_pointers_t **ptr = elf_local_ptr_offsets (abfd);
3219 /* Allocate a table to hold the local symbols if first time. */
3220 if (!ptr)
3222 unsigned int num_symbols = elf_symtab_hdr (abfd).sh_info;
3224 amt = num_symbols;
3225 amt *= sizeof (elf_linker_section_pointers_t *);
3226 ptr = bfd_zalloc (abfd, amt);
3228 if (!ptr)
3229 return FALSE;
3231 elf_local_ptr_offsets (abfd) = ptr;
3234 /* Has this symbol already been allocated? If so, our work is done. */
3235 if (elf_find_pointer_linker_section (ptr[r_symndx],
3236 rel->r_addend,
3237 lsect))
3238 return TRUE;
3240 ptr_linker_section_ptr = &ptr[r_symndx];
3243 /* Allocate space for a pointer in the linker section, and allocate
3244 a new pointer record from internal memory. */
3245 BFD_ASSERT (ptr_linker_section_ptr != NULL);
3246 amt = sizeof (elf_linker_section_pointers_t);
3247 linker_section_ptr = bfd_alloc (abfd, amt);
3249 if (!linker_section_ptr)
3250 return FALSE;
3252 linker_section_ptr->next = *ptr_linker_section_ptr;
3253 linker_section_ptr->addend = rel->r_addend;
3254 linker_section_ptr->lsect = lsect;
3255 *ptr_linker_section_ptr = linker_section_ptr;
3257 linker_section_ptr->offset = lsect->section->size;
3258 lsect->section->size += 4;
3260 #ifdef DEBUG
3261 fprintf (stderr,
3262 "Create pointer in linker section %s, offset = %ld, section size = %ld\n",
3263 lsect->name, (long) linker_section_ptr->offset,
3264 (long) lsect->section->size);
3265 #endif
3267 return TRUE;
3270 static struct plt_entry **
3271 update_local_sym_info (bfd *abfd,
3272 Elf_Internal_Shdr *symtab_hdr,
3273 unsigned long r_symndx,
3274 int tls_type)
3276 bfd_signed_vma *local_got_refcounts = elf_local_got_refcounts (abfd);
3277 struct plt_entry **local_plt;
3278 char *local_got_tls_masks;
3280 if (local_got_refcounts == NULL)
3282 bfd_size_type size = symtab_hdr->sh_info;
3284 size *= (sizeof (*local_got_refcounts)
3285 + sizeof (*local_plt)
3286 + sizeof (*local_got_tls_masks));
3287 local_got_refcounts = bfd_zalloc (abfd, size);
3288 if (local_got_refcounts == NULL)
3289 return NULL;
3290 elf_local_got_refcounts (abfd) = local_got_refcounts;
3293 local_plt = (struct plt_entry **) (local_got_refcounts + symtab_hdr->sh_info);
3294 local_got_tls_masks = (char *) (local_plt + symtab_hdr->sh_info);
3295 local_got_tls_masks[r_symndx] |= tls_type;
3296 if (tls_type != PLT_IFUNC)
3297 local_got_refcounts[r_symndx] += 1;
3298 return local_plt + r_symndx;
3301 static bfd_boolean
3302 update_plt_info (bfd *abfd, struct plt_entry **plist,
3303 asection *sec, bfd_vma addend)
3305 struct plt_entry *ent;
3307 if (addend < 32768)
3308 sec = NULL;
3309 for (ent = *plist; ent != NULL; ent = ent->next)
3310 if (ent->sec == sec && ent->addend == addend)
3311 break;
3312 if (ent == NULL)
3314 bfd_size_type amt = sizeof (*ent);
3315 ent = bfd_alloc (abfd, amt);
3316 if (ent == NULL)
3317 return FALSE;
3318 ent->next = *plist;
3319 ent->sec = sec;
3320 ent->addend = addend;
3321 ent->plt.refcount = 0;
3322 *plist = ent;
3324 ent->plt.refcount += 1;
3325 return TRUE;
3328 static struct plt_entry *
3329 find_plt_ent (struct plt_entry **plist, asection *sec, bfd_vma addend)
3331 struct plt_entry *ent;
3333 if (addend < 32768)
3334 sec = NULL;
3335 for (ent = *plist; ent != NULL; ent = ent->next)
3336 if (ent->sec == sec && ent->addend == addend)
3337 break;
3338 return ent;
3341 static bfd_boolean
3342 is_branch_reloc (enum elf_ppc_reloc_type r_type)
3344 return (r_type == R_PPC_PLTREL24
3345 || r_type == R_PPC_LOCAL24PC
3346 || r_type == R_PPC_REL24
3347 || r_type == R_PPC_REL14
3348 || r_type == R_PPC_REL14_BRTAKEN
3349 || r_type == R_PPC_REL14_BRNTAKEN
3350 || r_type == R_PPC_ADDR24
3351 || r_type == R_PPC_ADDR14
3352 || r_type == R_PPC_ADDR14_BRTAKEN
3353 || r_type == R_PPC_ADDR14_BRNTAKEN);
3356 static void
3357 bad_shared_reloc (bfd *abfd, enum elf_ppc_reloc_type r_type)
3359 (*_bfd_error_handler)
3360 (_("%B: relocation %s cannot be used when making a shared object"),
3361 abfd,
3362 ppc_elf_howto_table[r_type]->name);
3363 bfd_set_error (bfd_error_bad_value);
3366 /* Look through the relocs for a section during the first phase, and
3367 allocate space in the global offset table or procedure linkage
3368 table. */
3370 static bfd_boolean
3371 ppc_elf_check_relocs (bfd *abfd,
3372 struct bfd_link_info *info,
3373 asection *sec,
3374 const Elf_Internal_Rela *relocs)
3376 struct ppc_elf_link_hash_table *htab;
3377 Elf_Internal_Shdr *symtab_hdr;
3378 struct elf_link_hash_entry **sym_hashes;
3379 const Elf_Internal_Rela *rel;
3380 const Elf_Internal_Rela *rel_end;
3381 asection *got2, *sreloc;
3382 struct elf_link_hash_entry *tga;
3384 if (info->relocatable)
3385 return TRUE;
3387 /* Don't do anything special with non-loaded, non-alloced sections.
3388 In particular, any relocs in such sections should not affect GOT
3389 and PLT reference counting (ie. we don't allow them to create GOT
3390 or PLT entries), there's no possibility or desire to optimize TLS
3391 relocs, and there's not much point in propagating relocs to shared
3392 libs that the dynamic linker won't relocate. */
3393 if ((sec->flags & SEC_ALLOC) == 0)
3394 return TRUE;
3396 #ifdef DEBUG
3397 _bfd_error_handler ("ppc_elf_check_relocs called for section %A in %B",
3398 sec, abfd);
3399 #endif
3401 BFD_ASSERT (is_ppc_elf (abfd));
3403 /* Initialize howto table if not already done. */
3404 if (!ppc_elf_howto_table[R_PPC_ADDR32])
3405 ppc_elf_howto_init ();
3407 htab = ppc_elf_hash_table (info);
3408 if (htab->glink == NULL)
3410 if (htab->elf.dynobj == NULL)
3411 htab->elf.dynobj = abfd;
3412 if (!ppc_elf_create_glink (htab->elf.dynobj, info))
3413 return FALSE;
3415 tga = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
3416 FALSE, FALSE, TRUE);
3417 symtab_hdr = &elf_symtab_hdr (abfd);
3418 sym_hashes = elf_sym_hashes (abfd);
3419 got2 = bfd_get_section_by_name (abfd, ".got2");
3420 sreloc = NULL;
3422 rel_end = relocs + sec->reloc_count;
3423 for (rel = relocs; rel < rel_end; rel++)
3425 unsigned long r_symndx;
3426 enum elf_ppc_reloc_type r_type;
3427 struct elf_link_hash_entry *h;
3428 int tls_type;
3430 r_symndx = ELF32_R_SYM (rel->r_info);
3431 if (r_symndx < symtab_hdr->sh_info)
3432 h = NULL;
3433 else
3435 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
3436 while (h->root.type == bfd_link_hash_indirect
3437 || h->root.type == bfd_link_hash_warning)
3438 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3441 /* If a relocation refers to _GLOBAL_OFFSET_TABLE_, create the .got.
3442 This shows up in particular in an R_PPC_ADDR32 in the eabi
3443 startup code. */
3444 if (h != NULL
3445 && htab->got == NULL
3446 && strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
3448 if (htab->elf.dynobj == NULL)
3449 htab->elf.dynobj = abfd;
3450 if (!ppc_elf_create_got (htab->elf.dynobj, info))
3451 return FALSE;
3452 BFD_ASSERT (h == htab->elf.hgot);
3455 tls_type = 0;
3456 r_type = ELF32_R_TYPE (rel->r_info);
3457 if (h == NULL && !htab->is_vxworks)
3459 Elf_Internal_Sym *isym = bfd_sym_from_r_symndx (&htab->sym_cache,
3460 abfd, r_symndx);
3461 if (isym == NULL)
3462 return FALSE;
3464 if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC
3465 && (!info->shared
3466 || is_branch_reloc (r_type)))
3468 struct plt_entry **ifunc;
3469 bfd_vma addend;
3471 ifunc = update_local_sym_info (abfd, symtab_hdr, r_symndx,
3472 PLT_IFUNC);
3473 if (ifunc == NULL)
3474 return FALSE;
3476 /* STT_GNU_IFUNC symbols must have a PLT entry;
3477 In a non-pie executable even when there are
3478 no plt calls. */
3479 addend = 0;
3480 if (r_type == R_PPC_PLTREL24)
3482 ppc_elf_tdata (abfd)->makes_plt_call = 1;
3483 if (info->shared)
3484 addend = rel->r_addend;
3486 if (!update_plt_info (abfd, ifunc, got2, addend))
3487 return FALSE;
3491 if (!htab->is_vxworks
3492 && is_branch_reloc (r_type)
3493 && h != NULL
3494 && h == tga)
3496 if (rel != relocs
3497 && (ELF32_R_TYPE (rel[-1].r_info) == R_PPC_TLSGD
3498 || ELF32_R_TYPE (rel[-1].r_info) == R_PPC_TLSLD))
3499 /* We have a new-style __tls_get_addr call with a marker
3500 reloc. */
3502 else
3503 /* Mark this section as having an old-style call. */
3504 sec->has_tls_get_addr_call = 1;
3507 switch (r_type)
3509 case R_PPC_TLSGD:
3510 case R_PPC_TLSLD:
3511 /* These special tls relocs tie a call to __tls_get_addr with
3512 its parameter symbol. */
3513 break;
3515 case R_PPC_GOT_TLSLD16:
3516 case R_PPC_GOT_TLSLD16_LO:
3517 case R_PPC_GOT_TLSLD16_HI:
3518 case R_PPC_GOT_TLSLD16_HA:
3519 tls_type = TLS_TLS | TLS_LD;
3520 goto dogottls;
3522 case R_PPC_GOT_TLSGD16:
3523 case R_PPC_GOT_TLSGD16_LO:
3524 case R_PPC_GOT_TLSGD16_HI:
3525 case R_PPC_GOT_TLSGD16_HA:
3526 tls_type = TLS_TLS | TLS_GD;
3527 goto dogottls;
3529 case R_PPC_GOT_TPREL16:
3530 case R_PPC_GOT_TPREL16_LO:
3531 case R_PPC_GOT_TPREL16_HI:
3532 case R_PPC_GOT_TPREL16_HA:
3533 if (!info->executable)
3534 info->flags |= DF_STATIC_TLS;
3535 tls_type = TLS_TLS | TLS_TPREL;
3536 goto dogottls;
3538 case R_PPC_GOT_DTPREL16:
3539 case R_PPC_GOT_DTPREL16_LO:
3540 case R_PPC_GOT_DTPREL16_HI:
3541 case R_PPC_GOT_DTPREL16_HA:
3542 tls_type = TLS_TLS | TLS_DTPREL;
3543 dogottls:
3544 sec->has_tls_reloc = 1;
3545 /* Fall thru */
3547 /* GOT16 relocations */
3548 case R_PPC_GOT16:
3549 case R_PPC_GOT16_LO:
3550 case R_PPC_GOT16_HI:
3551 case R_PPC_GOT16_HA:
3552 /* This symbol requires a global offset table entry. */
3553 if (htab->got == NULL)
3555 if (htab->elf.dynobj == NULL)
3556 htab->elf.dynobj = abfd;
3557 if (!ppc_elf_create_got (htab->elf.dynobj, info))
3558 return FALSE;
3560 if (h != NULL)
3562 h->got.refcount += 1;
3563 ppc_elf_hash_entry (h)->tls_mask |= tls_type;
3565 else
3566 /* This is a global offset table entry for a local symbol. */
3567 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx, tls_type))
3568 return FALSE;
3570 /* We may also need a plt entry if the symbol turns out to be
3571 an ifunc. */
3572 if (h != NULL && !info->shared)
3574 if (!update_plt_info (abfd, &h->plt.plist, NULL, 0))
3575 return FALSE;
3577 break;
3579 /* Indirect .sdata relocation. */
3580 case R_PPC_EMB_SDAI16:
3581 if (info->shared)
3583 bad_shared_reloc (abfd, r_type);
3584 return FALSE;
3586 if (htab->sdata[0].section == NULL
3587 && !ppc_elf_create_linker_section (abfd, info, 0,
3588 &htab->sdata[0]))
3589 return FALSE;
3590 if (!elf_create_pointer_linker_section (abfd, &htab->sdata[0],
3591 h, rel))
3592 return FALSE;
3593 if (h != NULL)
3595 ppc_elf_hash_entry (h)->has_sda_refs = TRUE;
3596 h->non_got_ref = TRUE;
3598 break;
3600 /* Indirect .sdata2 relocation. */
3601 case R_PPC_EMB_SDA2I16:
3602 if (info->shared)
3604 bad_shared_reloc (abfd, r_type);
3605 return FALSE;
3607 if (htab->sdata[1].section == NULL
3608 && !ppc_elf_create_linker_section (abfd, info, SEC_READONLY,
3609 &htab->sdata[1]))
3610 return FALSE;
3611 if (!elf_create_pointer_linker_section (abfd, &htab->sdata[1],
3612 h, rel))
3613 return FALSE;
3614 if (h != NULL)
3616 ppc_elf_hash_entry (h)->has_sda_refs = TRUE;
3617 h->non_got_ref = TRUE;
3619 break;
3621 case R_PPC_SDAREL16:
3622 if (htab->sdata[0].sym == NULL
3623 && !create_sdata_sym (info, &htab->sdata[0]))
3624 return FALSE;
3625 if (h != NULL)
3627 ppc_elf_hash_entry (h)->has_sda_refs = TRUE;
3628 h->non_got_ref = TRUE;
3630 break;
3632 case R_PPC_EMB_SDA2REL:
3633 if (info->shared)
3635 bad_shared_reloc (abfd, r_type);
3636 return FALSE;
3638 if (htab->sdata[1].sym == NULL
3639 && !create_sdata_sym (info, &htab->sdata[1]))
3640 return FALSE;
3641 if (h != NULL)
3643 ppc_elf_hash_entry (h)->has_sda_refs = TRUE;
3644 h->non_got_ref = TRUE;
3646 break;
3648 case R_PPC_EMB_SDA21:
3649 case R_PPC_EMB_RELSDA:
3650 if (info->shared)
3652 bad_shared_reloc (abfd, r_type);
3653 return FALSE;
3655 if (htab->sdata[0].sym == NULL
3656 && !create_sdata_sym (info, &htab->sdata[0]))
3657 return FALSE;
3658 if (htab->sdata[1].sym == NULL
3659 && !create_sdata_sym (info, &htab->sdata[1]))
3660 return FALSE;
3661 if (h != NULL)
3663 ppc_elf_hash_entry (h)->has_sda_refs = TRUE;
3664 h->non_got_ref = TRUE;
3666 break;
3668 case R_PPC_EMB_NADDR32:
3669 case R_PPC_EMB_NADDR16:
3670 case R_PPC_EMB_NADDR16_LO:
3671 case R_PPC_EMB_NADDR16_HI:
3672 case R_PPC_EMB_NADDR16_HA:
3673 if (info->shared)
3675 bad_shared_reloc (abfd, r_type);
3676 return FALSE;
3678 if (h != NULL)
3679 h->non_got_ref = TRUE;
3680 break;
3682 case R_PPC_PLTREL24:
3683 if (h == NULL)
3684 break;
3685 /* Fall through */
3686 case R_PPC_PLT32:
3687 case R_PPC_PLTREL32:
3688 case R_PPC_PLT16_LO:
3689 case R_PPC_PLT16_HI:
3690 case R_PPC_PLT16_HA:
3691 #ifdef DEBUG
3692 fprintf (stderr, "Reloc requires a PLT entry\n");
3693 #endif
3694 /* This symbol requires a procedure linkage table entry. We
3695 actually build the entry in finish_dynamic_symbol,
3696 because this might be a case of linking PIC code without
3697 linking in any dynamic objects, in which case we don't
3698 need to generate a procedure linkage table after all. */
3700 if (h == NULL)
3702 /* It does not make sense to have a procedure linkage
3703 table entry for a local symbol. */
3704 (*_bfd_error_handler) (_("%B(%A+0x%lx): %s reloc against "
3705 "local symbol"),
3706 abfd,
3707 sec,
3708 (long) rel->r_offset,
3709 ppc_elf_howto_table[r_type]->name);
3710 bfd_set_error (bfd_error_bad_value);
3711 return FALSE;
3713 else
3715 bfd_vma addend = 0;
3717 if (r_type == R_PPC_PLTREL24)
3719 ppc_elf_tdata (abfd)->makes_plt_call = 1;
3720 if (info->shared)
3721 addend = rel->r_addend;
3723 h->needs_plt = 1;
3724 if (!update_plt_info (abfd, &h->plt.plist, got2, addend))
3725 return FALSE;
3727 break;
3729 /* The following relocations don't need to propagate the
3730 relocation if linking a shared object since they are
3731 section relative. */
3732 case R_PPC_SECTOFF:
3733 case R_PPC_SECTOFF_LO:
3734 case R_PPC_SECTOFF_HI:
3735 case R_PPC_SECTOFF_HA:
3736 case R_PPC_DTPREL16:
3737 case R_PPC_DTPREL16_LO:
3738 case R_PPC_DTPREL16_HI:
3739 case R_PPC_DTPREL16_HA:
3740 case R_PPC_TOC16:
3741 break;
3743 case R_PPC_REL16:
3744 case R_PPC_REL16_LO:
3745 case R_PPC_REL16_HI:
3746 case R_PPC_REL16_HA:
3747 ppc_elf_tdata (abfd)->has_rel16 = 1;
3748 break;
3750 /* These are just markers. */
3751 case R_PPC_TLS:
3752 case R_PPC_EMB_MRKREF:
3753 case R_PPC_NONE:
3754 case R_PPC_max:
3755 case R_PPC_RELAX:
3756 case R_PPC_RELAX_PLT:
3757 case R_PPC_RELAX_PLTREL24:
3758 break;
3760 /* These should only appear in dynamic objects. */
3761 case R_PPC_COPY:
3762 case R_PPC_GLOB_DAT:
3763 case R_PPC_JMP_SLOT:
3764 case R_PPC_RELATIVE:
3765 case R_PPC_IRELATIVE:
3766 break;
3768 /* These aren't handled yet. We'll report an error later. */
3769 case R_PPC_ADDR30:
3770 case R_PPC_EMB_RELSEC16:
3771 case R_PPC_EMB_RELST_LO:
3772 case R_PPC_EMB_RELST_HI:
3773 case R_PPC_EMB_RELST_HA:
3774 case R_PPC_EMB_BIT_FLD:
3775 break;
3777 /* This refers only to functions defined in the shared library. */
3778 case R_PPC_LOCAL24PC:
3779 if (h != NULL && h == htab->elf.hgot && htab->plt_type == PLT_UNSET)
3781 htab->plt_type = PLT_OLD;
3782 htab->old_bfd = abfd;
3784 break;
3786 /* This relocation describes the C++ object vtable hierarchy.
3787 Reconstruct it for later use during GC. */
3788 case R_PPC_GNU_VTINHERIT:
3789 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
3790 return FALSE;
3791 break;
3793 /* This relocation describes which C++ vtable entries are actually
3794 used. Record for later use during GC. */
3795 case R_PPC_GNU_VTENTRY:
3796 BFD_ASSERT (h != NULL);
3797 if (h != NULL
3798 && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
3799 return FALSE;
3800 break;
3802 /* We shouldn't really be seeing these. */
3803 case R_PPC_TPREL32:
3804 case R_PPC_TPREL16:
3805 case R_PPC_TPREL16_LO:
3806 case R_PPC_TPREL16_HI:
3807 case R_PPC_TPREL16_HA:
3808 if (!info->executable)
3809 info->flags |= DF_STATIC_TLS;
3810 goto dodyn;
3812 /* Nor these. */
3813 case R_PPC_DTPMOD32:
3814 case R_PPC_DTPREL32:
3815 goto dodyn;
3817 case R_PPC_REL32:
3818 if (h == NULL
3819 && got2 != NULL
3820 && (sec->flags & SEC_CODE) != 0
3821 && info->shared
3822 && htab->plt_type == PLT_UNSET)
3824 /* Old -fPIC gcc code has .long LCTOC1-LCFx just before
3825 the start of a function, which assembles to a REL32
3826 reference to .got2. If we detect one of these, then
3827 force the old PLT layout because the linker cannot
3828 reliably deduce the GOT pointer value needed for
3829 PLT call stubs. */
3830 asection *s;
3831 Elf_Internal_Sym *isym;
3833 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
3834 abfd, r_symndx);
3835 if (isym == NULL)
3836 return FALSE;
3838 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
3839 if (s == got2)
3841 htab->plt_type = PLT_OLD;
3842 htab->old_bfd = abfd;
3845 if (h == NULL || h == htab->elf.hgot)
3846 break;
3847 /* fall through */
3849 case R_PPC_ADDR32:
3850 case R_PPC_ADDR16:
3851 case R_PPC_ADDR16_LO:
3852 case R_PPC_ADDR16_HI:
3853 case R_PPC_ADDR16_HA:
3854 case R_PPC_UADDR32:
3855 case R_PPC_UADDR16:
3856 if (h != NULL && !info->shared)
3858 /* We may need a plt entry if the symbol turns out to be
3859 a function defined in a dynamic object. */
3860 if (!update_plt_info (abfd, &h->plt.plist, NULL, 0))
3861 return FALSE;
3863 /* We may need a copy reloc too. */
3864 h->non_got_ref = 1;
3865 h->pointer_equality_needed = 1;
3867 goto dodyn;
3869 case R_PPC_REL24:
3870 case R_PPC_REL14:
3871 case R_PPC_REL14_BRTAKEN:
3872 case R_PPC_REL14_BRNTAKEN:
3873 if (h == NULL)
3874 break;
3875 if (h == htab->elf.hgot)
3877 if (htab->plt_type == PLT_UNSET)
3879 htab->plt_type = PLT_OLD;
3880 htab->old_bfd = abfd;
3882 break;
3884 /* fall through */
3886 case R_PPC_ADDR24:
3887 case R_PPC_ADDR14:
3888 case R_PPC_ADDR14_BRTAKEN:
3889 case R_PPC_ADDR14_BRNTAKEN:
3890 if (h != NULL && !info->shared)
3892 /* We may need a plt entry if the symbol turns out to be
3893 a function defined in a dynamic object. */
3894 h->needs_plt = 1;
3895 if (!update_plt_info (abfd, &h->plt.plist, NULL, 0))
3896 return FALSE;
3897 break;
3900 dodyn:
3901 /* If we are creating a shared library, and this is a reloc
3902 against a global symbol, or a non PC relative reloc
3903 against a local symbol, then we need to copy the reloc
3904 into the shared library. However, if we are linking with
3905 -Bsymbolic, we do not need to copy a reloc against a
3906 global symbol which is defined in an object we are
3907 including in the link (i.e., DEF_REGULAR is set). At
3908 this point we have not seen all the input files, so it is
3909 possible that DEF_REGULAR is not set now but will be set
3910 later (it is never cleared). In case of a weak definition,
3911 DEF_REGULAR may be cleared later by a strong definition in
3912 a shared library. We account for that possibility below by
3913 storing information in the dyn_relocs field of the hash
3914 table entry. A similar situation occurs when creating
3915 shared libraries and symbol visibility changes render the
3916 symbol local.
3918 If on the other hand, we are creating an executable, we
3919 may need to keep relocations for symbols satisfied by a
3920 dynamic library if we manage to avoid copy relocs for the
3921 symbol. */
3922 if ((info->shared
3923 && (must_be_dyn_reloc (info, r_type)
3924 || (h != NULL
3925 && (! info->symbolic
3926 || h->root.type == bfd_link_hash_defweak
3927 || !h->def_regular))))
3928 || (ELIMINATE_COPY_RELOCS
3929 && !info->shared
3930 && h != NULL
3931 && (h->root.type == bfd_link_hash_defweak
3932 || !h->def_regular)))
3934 struct ppc_elf_dyn_relocs *p;
3935 struct ppc_elf_dyn_relocs **rel_head;
3937 #ifdef DEBUG
3938 fprintf (stderr,
3939 "ppc_elf_check_relocs needs to "
3940 "create relocation for %s\n",
3941 (h && h->root.root.string
3942 ? h->root.root.string : "<unknown>"));
3943 #endif
3944 if (sreloc == NULL)
3946 if (htab->elf.dynobj == NULL)
3947 htab->elf.dynobj = abfd;
3949 sreloc = _bfd_elf_make_dynamic_reloc_section
3950 (sec, htab->elf.dynobj, 2, abfd, /*rela?*/ TRUE);
3952 if (sreloc == NULL)
3953 return FALSE;
3956 /* If this is a global symbol, we count the number of
3957 relocations we need for this symbol. */
3958 if (h != NULL)
3960 rel_head = &ppc_elf_hash_entry (h)->dyn_relocs;
3962 else
3964 /* Track dynamic relocs needed for local syms too.
3965 We really need local syms available to do this
3966 easily. Oh well. */
3967 asection *s;
3968 void *vpp;
3969 Elf_Internal_Sym *isym;
3971 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
3972 abfd, r_symndx);
3973 if (isym == NULL)
3974 return FALSE;
3976 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
3977 if (s == NULL)
3978 s = sec;
3980 vpp = &elf_section_data (s)->local_dynrel;
3981 rel_head = (struct ppc_elf_dyn_relocs **) vpp;
3984 p = *rel_head;
3985 if (p == NULL || p->sec != sec)
3987 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
3988 if (p == NULL)
3989 return FALSE;
3990 p->next = *rel_head;
3991 *rel_head = p;
3992 p->sec = sec;
3993 p->count = 0;
3994 p->pc_count = 0;
3997 p->count += 1;
3998 if (!must_be_dyn_reloc (info, r_type))
3999 p->pc_count += 1;
4002 break;
4006 return TRUE;
4010 /* Merge object attributes from IBFD into OBFD. Raise an error if
4011 there are conflicting attributes. */
4012 static bfd_boolean
4013 ppc_elf_merge_obj_attributes (bfd *ibfd, bfd *obfd)
4015 obj_attribute *in_attr, *in_attrs;
4016 obj_attribute *out_attr, *out_attrs;
4018 if (!elf_known_obj_attributes_proc (obfd)[0].i)
4020 /* This is the first object. Copy the attributes. */
4021 _bfd_elf_copy_obj_attributes (ibfd, obfd);
4023 /* Use the Tag_null value to indicate the attributes have been
4024 initialized. */
4025 elf_known_obj_attributes_proc (obfd)[0].i = 1;
4027 return TRUE;
4030 in_attrs = elf_known_obj_attributes (ibfd)[OBJ_ATTR_GNU];
4031 out_attrs = elf_known_obj_attributes (obfd)[OBJ_ATTR_GNU];
4033 /* Check for conflicting Tag_GNU_Power_ABI_FP attributes and merge
4034 non-conflicting ones. */
4035 in_attr = &in_attrs[Tag_GNU_Power_ABI_FP];
4036 out_attr = &out_attrs[Tag_GNU_Power_ABI_FP];
4037 if (in_attr->i != out_attr->i)
4039 out_attr->type = 1;
4040 if (out_attr->i == 0)
4041 out_attr->i = in_attr->i;
4042 else if (in_attr->i == 0)
4044 else if (out_attr->i == 1 && in_attr->i == 2)
4045 _bfd_error_handler
4046 (_("Warning: %B uses hard float, %B uses soft float"), obfd, ibfd);
4047 else if (out_attr->i == 1 && in_attr->i == 3)
4048 _bfd_error_handler
4049 (_("Warning: %B uses double-precision hard float, %B uses single-precision hard float"),
4050 obfd, ibfd);
4051 else if (out_attr->i == 3 && in_attr->i == 1)
4052 _bfd_error_handler
4053 (_("Warning: %B uses double-precision hard float, %B uses single-precision hard float"),
4054 ibfd, obfd);
4055 else if (out_attr->i == 3 && in_attr->i == 2)
4056 _bfd_error_handler
4057 (_("Warning: %B uses soft float, %B uses single-precision hard float"),
4058 ibfd, obfd);
4059 else if (out_attr->i == 2 && (in_attr->i == 1 || in_attr->i == 3))
4060 _bfd_error_handler
4061 (_("Warning: %B uses hard float, %B uses soft float"), ibfd, obfd);
4062 else if (in_attr->i > 3)
4063 _bfd_error_handler
4064 (_("Warning: %B uses unknown floating point ABI %d"), ibfd,
4065 in_attr->i);
4066 else
4067 _bfd_error_handler
4068 (_("Warning: %B uses unknown floating point ABI %d"), obfd,
4069 out_attr->i);
4072 /* Check for conflicting Tag_GNU_Power_ABI_Vector attributes and
4073 merge non-conflicting ones. */
4074 in_attr = &in_attrs[Tag_GNU_Power_ABI_Vector];
4075 out_attr = &out_attrs[Tag_GNU_Power_ABI_Vector];
4076 if (in_attr->i != out_attr->i)
4078 const char *in_abi = NULL, *out_abi = NULL;
4080 switch (in_attr->i)
4082 case 1: in_abi = "generic"; break;
4083 case 2: in_abi = "AltiVec"; break;
4084 case 3: in_abi = "SPE"; break;
4087 switch (out_attr->i)
4089 case 1: out_abi = "generic"; break;
4090 case 2: out_abi = "AltiVec"; break;
4091 case 3: out_abi = "SPE"; break;
4094 out_attr->type = 1;
4095 if (out_attr->i == 0)
4096 out_attr->i = in_attr->i;
4097 else if (in_attr->i == 0)
4099 /* For now, allow generic to transition to AltiVec or SPE
4100 without a warning. If GCC marked files with their stack
4101 alignment and used don't-care markings for files which are
4102 not affected by the vector ABI, we could warn about this
4103 case too. */
4104 else if (out_attr->i == 1)
4105 out_attr->i = in_attr->i;
4106 else if (in_attr->i == 1)
4108 else if (in_abi == NULL)
4109 _bfd_error_handler
4110 (_("Warning: %B uses unknown vector ABI %d"), ibfd,
4111 in_attr->i);
4112 else if (out_abi == NULL)
4113 _bfd_error_handler
4114 (_("Warning: %B uses unknown vector ABI %d"), obfd,
4115 in_attr->i);
4116 else
4117 _bfd_error_handler
4118 (_("Warning: %B uses vector ABI \"%s\", %B uses \"%s\""),
4119 ibfd, obfd, in_abi, out_abi);
4122 /* Check for conflicting Tag_GNU_Power_ABI_Struct_Return attributes
4123 and merge non-conflicting ones. */
4124 in_attr = &in_attrs[Tag_GNU_Power_ABI_Struct_Return];
4125 out_attr = &out_attrs[Tag_GNU_Power_ABI_Struct_Return];
4126 if (in_attr->i != out_attr->i)
4128 out_attr->type = 1;
4129 if (out_attr->i == 0)
4130 out_attr->i = in_attr->i;
4131 else if (in_attr->i == 0)
4133 else if (out_attr->i == 1 && in_attr->i == 2)
4134 _bfd_error_handler
4135 (_("Warning: %B uses r3/r4 for small structure returns, %B uses memory"), obfd, ibfd);
4136 else if (out_attr->i == 2 && in_attr->i == 1)
4137 _bfd_error_handler
4138 (_("Warning: %B uses r3/r4 for small structure returns, %B uses memory"), ibfd, obfd);
4139 else if (in_attr->i > 2)
4140 _bfd_error_handler
4141 (_("Warning: %B uses unknown small structure return convention %d"), ibfd,
4142 in_attr->i);
4143 else
4144 _bfd_error_handler
4145 (_("Warning: %B uses unknown small structure return convention %d"), obfd,
4146 out_attr->i);
4149 /* Merge Tag_compatibility attributes and any common GNU ones. */
4150 _bfd_elf_merge_object_attributes (ibfd, obfd);
4152 return TRUE;
4155 /* Merge backend specific data from an object file to the output
4156 object file when linking. */
4158 static bfd_boolean
4159 ppc_elf_merge_private_bfd_data (bfd *ibfd, bfd *obfd)
4161 flagword old_flags;
4162 flagword new_flags;
4163 bfd_boolean error;
4165 if (!is_ppc_elf (ibfd) || !is_ppc_elf (obfd))
4166 return TRUE;
4168 /* Check if we have the same endianess. */
4169 if (! _bfd_generic_verify_endian_match (ibfd, obfd))
4170 return FALSE;
4172 if (!ppc_elf_merge_obj_attributes (ibfd, obfd))
4173 return FALSE;
4175 new_flags = elf_elfheader (ibfd)->e_flags;
4176 old_flags = elf_elfheader (obfd)->e_flags;
4177 if (!elf_flags_init (obfd))
4179 /* First call, no flags set. */
4180 elf_flags_init (obfd) = TRUE;
4181 elf_elfheader (obfd)->e_flags = new_flags;
4184 /* Compatible flags are ok. */
4185 else if (new_flags == old_flags)
4188 /* Incompatible flags. */
4189 else
4191 /* Warn about -mrelocatable mismatch. Allow -mrelocatable-lib
4192 to be linked with either. */
4193 error = FALSE;
4194 if ((new_flags & EF_PPC_RELOCATABLE) != 0
4195 && (old_flags & (EF_PPC_RELOCATABLE | EF_PPC_RELOCATABLE_LIB)) == 0)
4197 error = TRUE;
4198 (*_bfd_error_handler)
4199 (_("%B: compiled with -mrelocatable and linked with "
4200 "modules compiled normally"), ibfd);
4202 else if ((new_flags & (EF_PPC_RELOCATABLE | EF_PPC_RELOCATABLE_LIB)) == 0
4203 && (old_flags & EF_PPC_RELOCATABLE) != 0)
4205 error = TRUE;
4206 (*_bfd_error_handler)
4207 (_("%B: compiled normally and linked with "
4208 "modules compiled with -mrelocatable"), ibfd);
4211 /* The output is -mrelocatable-lib iff both the input files are. */
4212 if (! (new_flags & EF_PPC_RELOCATABLE_LIB))
4213 elf_elfheader (obfd)->e_flags &= ~EF_PPC_RELOCATABLE_LIB;
4215 /* The output is -mrelocatable iff it can't be -mrelocatable-lib,
4216 but each input file is either -mrelocatable or -mrelocatable-lib. */
4217 if (! (elf_elfheader (obfd)->e_flags & EF_PPC_RELOCATABLE_LIB)
4218 && (new_flags & (EF_PPC_RELOCATABLE_LIB | EF_PPC_RELOCATABLE))
4219 && (old_flags & (EF_PPC_RELOCATABLE_LIB | EF_PPC_RELOCATABLE)))
4220 elf_elfheader (obfd)->e_flags |= EF_PPC_RELOCATABLE;
4222 /* Do not warn about eabi vs. V.4 mismatch, just or in the bit if
4223 any module uses it. */
4224 elf_elfheader (obfd)->e_flags |= (new_flags & EF_PPC_EMB);
4226 new_flags &= ~(EF_PPC_RELOCATABLE | EF_PPC_RELOCATABLE_LIB | EF_PPC_EMB);
4227 old_flags &= ~(EF_PPC_RELOCATABLE | EF_PPC_RELOCATABLE_LIB | EF_PPC_EMB);
4229 /* Warn about any other mismatches. */
4230 if (new_flags != old_flags)
4232 error = TRUE;
4233 (*_bfd_error_handler)
4234 (_("%B: uses different e_flags (0x%lx) fields "
4235 "than previous modules (0x%lx)"),
4236 ibfd, (long) new_flags, (long) old_flags);
4239 if (error)
4241 bfd_set_error (bfd_error_bad_value);
4242 return FALSE;
4246 return TRUE;
4249 /* Choose which PLT scheme to use, and set .plt flags appropriately.
4250 Returns -1 on error, 0 for old PLT, 1 for new PLT. */
4252 ppc_elf_select_plt_layout (bfd *output_bfd ATTRIBUTE_UNUSED,
4253 struct bfd_link_info *info,
4254 enum ppc_elf_plt_type plt_style,
4255 int emit_stub_syms)
4257 struct ppc_elf_link_hash_table *htab;
4258 flagword flags;
4260 htab = ppc_elf_hash_table (info);
4262 htab->emit_stub_syms = emit_stub_syms;
4264 if (htab->plt_type == PLT_UNSET)
4266 if (plt_style == PLT_OLD)
4267 htab->plt_type = PLT_OLD;
4268 else
4270 bfd *ibfd;
4271 enum ppc_elf_plt_type plt_type = plt_style;
4273 /* Look through the reloc flags left by ppc_elf_check_relocs.
4274 Use the old style bss plt if a file makes plt calls
4275 without using the new relocs, and if ld isn't given
4276 --secure-plt and we never see REL16 relocs. */
4277 if (plt_type == PLT_UNSET)
4278 plt_type = PLT_OLD;
4279 for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link_next)
4280 if (is_ppc_elf (ibfd))
4282 if (ppc_elf_tdata (ibfd)->has_rel16)
4283 plt_type = PLT_NEW;
4284 else if (ppc_elf_tdata (ibfd)->makes_plt_call)
4286 plt_type = PLT_OLD;
4287 htab->old_bfd = ibfd;
4288 break;
4291 htab->plt_type = plt_type;
4294 if (htab->plt_type == PLT_OLD && plt_style == PLT_NEW)
4295 info->callbacks->info (_("Using bss-plt due to %B"), htab->old_bfd);
4297 BFD_ASSERT (htab->plt_type != PLT_VXWORKS);
4299 if (htab->plt_type == PLT_NEW)
4301 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
4302 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4304 /* The new PLT is a loaded section. */
4305 if (htab->plt != NULL
4306 && !bfd_set_section_flags (htab->elf.dynobj, htab->plt, flags))
4307 return -1;
4309 /* The new GOT is not executable. */
4310 if (htab->got != NULL
4311 && !bfd_set_section_flags (htab->elf.dynobj, htab->got, flags))
4312 return -1;
4314 else
4316 /* Stop an unused .glink section from affecting .text alignment. */
4317 if (htab->glink != NULL
4318 && !bfd_set_section_alignment (htab->elf.dynobj, htab->glink, 0))
4319 return -1;
4321 return htab->plt_type == PLT_NEW;
4324 /* Return the section that should be marked against GC for a given
4325 relocation. */
4327 static asection *
4328 ppc_elf_gc_mark_hook (asection *sec,
4329 struct bfd_link_info *info,
4330 Elf_Internal_Rela *rel,
4331 struct elf_link_hash_entry *h,
4332 Elf_Internal_Sym *sym)
4334 if (h != NULL)
4335 switch (ELF32_R_TYPE (rel->r_info))
4337 case R_PPC_GNU_VTINHERIT:
4338 case R_PPC_GNU_VTENTRY:
4339 return NULL;
4342 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
4345 /* Update the got, plt and dynamic reloc reference counts for the
4346 section being removed. */
4348 static bfd_boolean
4349 ppc_elf_gc_sweep_hook (bfd *abfd,
4350 struct bfd_link_info *info,
4351 asection *sec,
4352 const Elf_Internal_Rela *relocs)
4354 struct ppc_elf_link_hash_table *htab;
4355 Elf_Internal_Shdr *symtab_hdr;
4356 struct elf_link_hash_entry **sym_hashes;
4357 bfd_signed_vma *local_got_refcounts;
4358 const Elf_Internal_Rela *rel, *relend;
4359 asection *got2;
4361 if (info->relocatable)
4362 return TRUE;
4364 if ((sec->flags & SEC_ALLOC) == 0)
4365 return TRUE;
4367 elf_section_data (sec)->local_dynrel = NULL;
4369 htab = ppc_elf_hash_table (info);
4370 symtab_hdr = &elf_symtab_hdr (abfd);
4371 sym_hashes = elf_sym_hashes (abfd);
4372 local_got_refcounts = elf_local_got_refcounts (abfd);
4373 got2 = bfd_get_section_by_name (abfd, ".got2");
4375 relend = relocs + sec->reloc_count;
4376 for (rel = relocs; rel < relend; rel++)
4378 unsigned long r_symndx;
4379 enum elf_ppc_reloc_type r_type;
4380 struct elf_link_hash_entry *h = NULL;
4382 r_symndx = ELF32_R_SYM (rel->r_info);
4383 if (r_symndx >= symtab_hdr->sh_info)
4385 struct ppc_elf_dyn_relocs **pp, *p;
4386 struct ppc_elf_link_hash_entry *eh;
4388 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
4389 while (h->root.type == bfd_link_hash_indirect
4390 || h->root.type == bfd_link_hash_warning)
4391 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4392 eh = (struct ppc_elf_link_hash_entry *) h;
4394 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
4395 if (p->sec == sec)
4397 /* Everything must go for SEC. */
4398 *pp = p->next;
4399 break;
4403 r_type = ELF32_R_TYPE (rel->r_info);
4404 if (!htab->is_vxworks
4405 && h == NULL
4406 && local_got_refcounts != NULL
4407 && (!info->shared
4408 || is_branch_reloc (r_type)))
4410 struct plt_entry **local_plt = (struct plt_entry **)
4411 (local_got_refcounts + symtab_hdr->sh_info);
4412 char *local_got_tls_masks = (char *)
4413 (local_plt + symtab_hdr->sh_info);
4414 if ((local_got_tls_masks[r_symndx] & PLT_IFUNC) != 0)
4416 struct plt_entry **ifunc = local_plt + r_symndx;
4417 bfd_vma addend = 0;
4418 struct plt_entry *ent;
4420 if (r_type == R_PPC_PLTREL24 && info->shared)
4421 addend = rel->r_addend;
4422 ent = find_plt_ent (ifunc, got2, addend);
4423 if (ent->plt.refcount > 0)
4424 ent->plt.refcount -= 1;
4425 continue;
4429 switch (r_type)
4431 case R_PPC_GOT_TLSLD16:
4432 case R_PPC_GOT_TLSLD16_LO:
4433 case R_PPC_GOT_TLSLD16_HI:
4434 case R_PPC_GOT_TLSLD16_HA:
4435 case R_PPC_GOT_TLSGD16:
4436 case R_PPC_GOT_TLSGD16_LO:
4437 case R_PPC_GOT_TLSGD16_HI:
4438 case R_PPC_GOT_TLSGD16_HA:
4439 case R_PPC_GOT_TPREL16:
4440 case R_PPC_GOT_TPREL16_LO:
4441 case R_PPC_GOT_TPREL16_HI:
4442 case R_PPC_GOT_TPREL16_HA:
4443 case R_PPC_GOT_DTPREL16:
4444 case R_PPC_GOT_DTPREL16_LO:
4445 case R_PPC_GOT_DTPREL16_HI:
4446 case R_PPC_GOT_DTPREL16_HA:
4447 case R_PPC_GOT16:
4448 case R_PPC_GOT16_LO:
4449 case R_PPC_GOT16_HI:
4450 case R_PPC_GOT16_HA:
4451 if (h != NULL)
4453 if (h->got.refcount > 0)
4454 h->got.refcount--;
4455 if (!info->shared)
4457 struct plt_entry *ent;
4459 ent = find_plt_ent (&h->plt.plist, NULL, 0);
4460 if (ent != NULL && ent->plt.refcount > 0)
4461 ent->plt.refcount -= 1;
4464 else if (local_got_refcounts != NULL)
4466 if (local_got_refcounts[r_symndx] > 0)
4467 local_got_refcounts[r_symndx]--;
4469 break;
4471 case R_PPC_REL24:
4472 case R_PPC_REL14:
4473 case R_PPC_REL14_BRTAKEN:
4474 case R_PPC_REL14_BRNTAKEN:
4475 case R_PPC_REL32:
4476 if (h == NULL || h == htab->elf.hgot)
4477 break;
4478 /* Fall thru */
4480 case R_PPC_ADDR32:
4481 case R_PPC_ADDR24:
4482 case R_PPC_ADDR16:
4483 case R_PPC_ADDR16_LO:
4484 case R_PPC_ADDR16_HI:
4485 case R_PPC_ADDR16_HA:
4486 case R_PPC_ADDR14:
4487 case R_PPC_ADDR14_BRTAKEN:
4488 case R_PPC_ADDR14_BRNTAKEN:
4489 case R_PPC_UADDR32:
4490 case R_PPC_UADDR16:
4491 if (info->shared)
4492 break;
4494 case R_PPC_PLT32:
4495 case R_PPC_PLTREL24:
4496 case R_PPC_PLTREL32:
4497 case R_PPC_PLT16_LO:
4498 case R_PPC_PLT16_HI:
4499 case R_PPC_PLT16_HA:
4500 if (h != NULL)
4502 bfd_vma addend = 0;
4503 struct plt_entry *ent;
4505 if (r_type == R_PPC_PLTREL24 && info->shared)
4506 addend = rel->r_addend;
4507 ent = find_plt_ent (&h->plt.plist, got2, addend);
4508 if (ent != NULL && ent->plt.refcount > 0)
4509 ent->plt.refcount -= 1;
4511 break;
4513 default:
4514 break;
4517 return TRUE;
4520 /* Set plt output section type, htab->tls_get_addr, and call the
4521 generic ELF tls_setup function. */
4523 asection *
4524 ppc_elf_tls_setup (bfd *obfd,
4525 struct bfd_link_info *info,
4526 int no_tls_get_addr_opt)
4528 struct ppc_elf_link_hash_table *htab;
4530 htab = ppc_elf_hash_table (info);
4531 htab->tls_get_addr = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
4532 FALSE, FALSE, TRUE);
4533 if (!no_tls_get_addr_opt)
4535 struct elf_link_hash_entry *opt, *tga;
4536 opt = elf_link_hash_lookup (&htab->elf, "__tls_get_addr_opt",
4537 FALSE, FALSE, TRUE);
4538 if (opt != NULL
4539 && (opt->root.type == bfd_link_hash_defined
4540 || opt->root.type == bfd_link_hash_defweak))
4542 /* If glibc supports an optimized __tls_get_addr call stub,
4543 signalled by the presence of __tls_get_addr_opt, and we'll
4544 be calling __tls_get_addr via a plt call stub, then
4545 make __tls_get_addr point to __tls_get_addr_opt. */
4546 tga = htab->tls_get_addr;
4547 if (htab->elf.dynamic_sections_created
4548 && tga != NULL
4549 && (tga->type == STT_FUNC
4550 || tga->needs_plt)
4551 && !(SYMBOL_CALLS_LOCAL (info, tga)
4552 || (ELF_ST_VISIBILITY (tga->other) != STV_DEFAULT
4553 && tga->root.type == bfd_link_hash_undefweak)))
4555 struct plt_entry *ent;
4556 for (ent = tga->plt.plist; ent != NULL; ent = ent->next)
4557 if (ent->plt.refcount > 0)
4558 break;
4559 if (ent != NULL)
4561 tga->root.type = bfd_link_hash_indirect;
4562 tga->root.u.i.link = &opt->root;
4563 ppc_elf_copy_indirect_symbol (info, opt, tga);
4564 if (opt->dynindx != -1)
4566 /* Use __tls_get_addr_opt in dynamic relocations. */
4567 opt->dynindx = -1;
4568 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
4569 opt->dynstr_index);
4570 if (!bfd_elf_link_record_dynamic_symbol (info, opt))
4571 return FALSE;
4573 htab->tls_get_addr = opt;
4577 else
4578 no_tls_get_addr_opt = TRUE;
4580 htab->no_tls_get_addr_opt = no_tls_get_addr_opt;
4581 if (htab->plt_type == PLT_NEW
4582 && htab->plt != NULL
4583 && htab->plt->output_section != NULL)
4585 elf_section_type (htab->plt->output_section) = SHT_PROGBITS;
4586 elf_section_flags (htab->plt->output_section) = SHF_ALLOC + SHF_WRITE;
4589 return _bfd_elf_tls_setup (obfd, info);
4592 /* Return TRUE iff REL is a branch reloc with a global symbol matching
4593 HASH. */
4595 static bfd_boolean
4596 branch_reloc_hash_match (const bfd *ibfd,
4597 const Elf_Internal_Rela *rel,
4598 const struct elf_link_hash_entry *hash)
4600 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
4601 enum elf_ppc_reloc_type r_type = ELF32_R_TYPE (rel->r_info);
4602 unsigned int r_symndx = ELF32_R_SYM (rel->r_info);
4604 if (r_symndx >= symtab_hdr->sh_info && is_branch_reloc (r_type))
4606 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
4607 struct elf_link_hash_entry *h;
4609 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
4610 while (h->root.type == bfd_link_hash_indirect
4611 || h->root.type == bfd_link_hash_warning)
4612 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4613 if (h == hash)
4614 return TRUE;
4616 return FALSE;
4619 /* Run through all the TLS relocs looking for optimization
4620 opportunities. */
4622 bfd_boolean
4623 ppc_elf_tls_optimize (bfd *obfd ATTRIBUTE_UNUSED,
4624 struct bfd_link_info *info)
4626 bfd *ibfd;
4627 asection *sec;
4628 struct ppc_elf_link_hash_table *htab;
4629 int pass;
4631 if (info->relocatable || !info->executable)
4632 return TRUE;
4634 htab = ppc_elf_hash_table (info);
4635 /* Make two passes through the relocs. First time check that tls
4636 relocs involved in setting up a tls_get_addr call are indeed
4637 followed by such a call. If they are not, exclude them from
4638 the optimizations done on the second pass. */
4639 for (pass = 0; pass < 2; ++pass)
4640 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
4642 Elf_Internal_Sym *locsyms = NULL;
4643 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
4644 asection *got2 = bfd_get_section_by_name (ibfd, ".got2");
4646 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
4647 if (sec->has_tls_reloc && !bfd_is_abs_section (sec->output_section))
4649 Elf_Internal_Rela *relstart, *rel, *relend;
4651 /* Read the relocations. */
4652 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
4653 info->keep_memory);
4654 if (relstart == NULL)
4655 return FALSE;
4657 relend = relstart + sec->reloc_count;
4658 for (rel = relstart; rel < relend; rel++)
4660 enum elf_ppc_reloc_type r_type;
4661 unsigned long r_symndx;
4662 struct elf_link_hash_entry *h = NULL;
4663 char *tls_mask;
4664 char tls_set, tls_clear;
4665 bfd_boolean is_local;
4666 int expecting_tls_get_addr;
4667 bfd_signed_vma *got_count;
4669 r_symndx = ELF32_R_SYM (rel->r_info);
4670 if (r_symndx >= symtab_hdr->sh_info)
4672 struct elf_link_hash_entry **sym_hashes;
4674 sym_hashes = elf_sym_hashes (ibfd);
4675 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
4676 while (h->root.type == bfd_link_hash_indirect
4677 || h->root.type == bfd_link_hash_warning)
4678 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4681 expecting_tls_get_addr = 0;
4682 is_local = FALSE;
4683 if (h == NULL
4684 || !h->def_dynamic)
4685 is_local = TRUE;
4687 r_type = ELF32_R_TYPE (rel->r_info);
4688 switch (r_type)
4690 case R_PPC_GOT_TLSLD16:
4691 case R_PPC_GOT_TLSLD16_LO:
4692 expecting_tls_get_addr = 1;
4693 /* Fall thru */
4695 case R_PPC_GOT_TLSLD16_HI:
4696 case R_PPC_GOT_TLSLD16_HA:
4697 /* These relocs should never be against a symbol
4698 defined in a shared lib. Leave them alone if
4699 that turns out to be the case. */
4700 if (!is_local)
4701 continue;
4703 /* LD -> LE */
4704 tls_set = 0;
4705 tls_clear = TLS_LD;
4706 break;
4708 case R_PPC_GOT_TLSGD16:
4709 case R_PPC_GOT_TLSGD16_LO:
4710 expecting_tls_get_addr = 1;
4711 /* Fall thru */
4713 case R_PPC_GOT_TLSGD16_HI:
4714 case R_PPC_GOT_TLSGD16_HA:
4715 if (is_local)
4716 /* GD -> LE */
4717 tls_set = 0;
4718 else
4719 /* GD -> IE */
4720 tls_set = TLS_TLS | TLS_TPRELGD;
4721 tls_clear = TLS_GD;
4722 break;
4724 case R_PPC_GOT_TPREL16:
4725 case R_PPC_GOT_TPREL16_LO:
4726 case R_PPC_GOT_TPREL16_HI:
4727 case R_PPC_GOT_TPREL16_HA:
4728 if (is_local)
4730 /* IE -> LE */
4731 tls_set = 0;
4732 tls_clear = TLS_TPREL;
4733 break;
4735 else
4736 continue;
4738 case R_PPC_TLSGD:
4739 case R_PPC_TLSLD:
4740 expecting_tls_get_addr = 2;
4741 tls_set = 0;
4742 tls_clear = 0;
4743 break;
4745 default:
4746 continue;
4749 if (pass == 0)
4751 if (!expecting_tls_get_addr
4752 || (expecting_tls_get_addr == 1
4753 && !sec->has_tls_get_addr_call))
4754 continue;
4756 if (rel + 1 < relend
4757 && branch_reloc_hash_match (ibfd, rel + 1,
4758 htab->tls_get_addr))
4759 continue;
4761 /* Uh oh, we didn't find the expected call. We
4762 could just mark this symbol to exclude it
4763 from tls optimization but it's safer to skip
4764 the entire section. */
4765 sec->has_tls_reloc = 0;
4766 break;
4769 if (expecting_tls_get_addr)
4771 struct plt_entry *ent;
4772 bfd_vma addend = 0;
4774 if (info->shared
4775 && ELF32_R_TYPE (rel[1].r_info) == R_PPC_PLTREL24)
4776 addend = rel[1].r_addend;
4777 ent = find_plt_ent (&htab->tls_get_addr->plt.plist,
4778 got2, addend);
4779 if (ent != NULL && ent->plt.refcount > 0)
4780 ent->plt.refcount -= 1;
4782 if (expecting_tls_get_addr == 2)
4783 continue;
4786 if (h != NULL)
4788 tls_mask = &ppc_elf_hash_entry (h)->tls_mask;
4789 got_count = &h->got.refcount;
4791 else
4793 Elf_Internal_Sym *sym;
4794 bfd_signed_vma *lgot_refs;
4795 struct plt_entry **local_plt;
4796 char *lgot_masks;
4798 if (locsyms == NULL)
4800 locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
4801 if (locsyms == NULL)
4802 locsyms = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
4803 symtab_hdr->sh_info,
4804 0, NULL, NULL, NULL);
4805 if (locsyms == NULL)
4807 if (elf_section_data (sec)->relocs != relstart)
4808 free (relstart);
4809 return FALSE;
4812 sym = locsyms + r_symndx;
4813 lgot_refs = elf_local_got_refcounts (ibfd);
4814 if (lgot_refs == NULL)
4815 abort ();
4816 local_plt = (struct plt_entry **)
4817 (lgot_refs + symtab_hdr->sh_info);
4818 lgot_masks = (char *) (local_plt + symtab_hdr->sh_info);
4819 tls_mask = &lgot_masks[r_symndx];
4820 got_count = &lgot_refs[r_symndx];
4823 if (tls_set == 0)
4825 /* We managed to get rid of a got entry. */
4826 if (*got_count > 0)
4827 *got_count -= 1;
4830 *tls_mask |= tls_set;
4831 *tls_mask &= ~tls_clear;
4834 if (elf_section_data (sec)->relocs != relstart)
4835 free (relstart);
4838 if (locsyms != NULL
4839 && (symtab_hdr->contents != (unsigned char *) locsyms))
4841 if (!info->keep_memory)
4842 free (locsyms);
4843 else
4844 symtab_hdr->contents = (unsigned char *) locsyms;
4847 return TRUE;
4850 /* Return true if we have dynamic relocs that apply to read-only sections. */
4852 static bfd_boolean
4853 readonly_dynrelocs (struct elf_link_hash_entry *h)
4855 struct ppc_elf_dyn_relocs *p;
4857 for (p = ppc_elf_hash_entry (h)->dyn_relocs; p != NULL; p = p->next)
4859 asection *s = p->sec->output_section;
4861 if (s != NULL
4862 && ((s->flags & (SEC_READONLY | SEC_ALLOC))
4863 == (SEC_READONLY | SEC_ALLOC)))
4864 return TRUE;
4866 return FALSE;
4869 /* Adjust a symbol defined by a dynamic object and referenced by a
4870 regular object. The current definition is in some section of the
4871 dynamic object, but we're not including those sections. We have to
4872 change the definition to something the rest of the link can
4873 understand. */
4875 static bfd_boolean
4876 ppc_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
4877 struct elf_link_hash_entry *h)
4879 struct ppc_elf_link_hash_table *htab;
4880 asection *s;
4882 #ifdef DEBUG
4883 fprintf (stderr, "ppc_elf_adjust_dynamic_symbol called for %s\n",
4884 h->root.root.string);
4885 #endif
4887 /* Make sure we know what is going on here. */
4888 htab = ppc_elf_hash_table (info);
4889 BFD_ASSERT (htab->elf.dynobj != NULL
4890 && (h->needs_plt
4891 || h->type == STT_GNU_IFUNC
4892 || h->u.weakdef != NULL
4893 || (h->def_dynamic
4894 && h->ref_regular
4895 && !h->def_regular)));
4897 /* Deal with function syms. */
4898 if (h->type == STT_FUNC
4899 || h->type == STT_GNU_IFUNC
4900 || h->needs_plt)
4902 /* Clear procedure linkage table information for any symbol that
4903 won't need a .plt entry. */
4904 struct plt_entry *ent;
4905 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
4906 if (ent->plt.refcount > 0)
4907 break;
4908 if (ent == NULL
4909 || (h->type != STT_GNU_IFUNC
4910 && (SYMBOL_CALLS_LOCAL (info, h)
4911 || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
4912 && h->root.type == bfd_link_hash_undefweak))))
4914 /* A PLT entry is not required/allowed when:
4916 1. We are not using ld.so; because then the PLT entry
4917 can't be set up, so we can't use one. In this case,
4918 ppc_elf_adjust_dynamic_symbol won't even be called.
4920 2. GC has rendered the entry unused.
4922 3. We know for certain that a call to this symbol
4923 will go to this object, or will remain undefined. */
4924 h->plt.plist = NULL;
4925 h->needs_plt = 0;
4927 else
4929 /* After adjust_dynamic_symbol, non_got_ref set in the
4930 non-shared case means that we have allocated space in
4931 .dynbss for the symbol and thus dyn_relocs for this
4932 symbol should be discarded.
4933 If we get here we know we are making a PLT entry for this
4934 symbol, and in an executable we'd normally resolve
4935 relocations against this symbol to the PLT entry. Allow
4936 dynamic relocs if the reference is weak, and the dynamic
4937 relocs will not cause text relocation. */
4938 if (!h->ref_regular_nonweak
4939 && h->non_got_ref
4940 && h->type != STT_GNU_IFUNC
4941 && !htab->is_vxworks
4942 && !ppc_elf_hash_entry (h)->has_sda_refs
4943 && !readonly_dynrelocs (h))
4944 h->non_got_ref = 0;
4946 return TRUE;
4948 else
4949 h->plt.plist = NULL;
4951 /* If this is a weak symbol, and there is a real definition, the
4952 processor independent code will have arranged for us to see the
4953 real definition first, and we can just use the same value. */
4954 if (h->u.weakdef != NULL)
4956 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
4957 || h->u.weakdef->root.type == bfd_link_hash_defweak);
4958 h->root.u.def.section = h->u.weakdef->root.u.def.section;
4959 h->root.u.def.value = h->u.weakdef->root.u.def.value;
4960 if (ELIMINATE_COPY_RELOCS)
4961 h->non_got_ref = h->u.weakdef->non_got_ref;
4962 return TRUE;
4965 /* This is a reference to a symbol defined by a dynamic object which
4966 is not a function. */
4968 /* If we are creating a shared library, we must presume that the
4969 only references to the symbol are via the global offset table.
4970 For such cases we need not do anything here; the relocations will
4971 be handled correctly by relocate_section. */
4972 if (info->shared)
4973 return TRUE;
4975 /* If there are no references to this symbol that do not use the
4976 GOT, we don't need to generate a copy reloc. */
4977 if (!h->non_got_ref)
4978 return TRUE;
4980 /* If we didn't find any dynamic relocs in read-only sections, then
4981 we'll be keeping the dynamic relocs and avoiding the copy reloc.
4982 We can't do this if there are any small data relocations. This
4983 doesn't work on VxWorks, where we can not have dynamic
4984 relocations (other than copy and jump slot relocations) in an
4985 executable. */
4986 if (ELIMINATE_COPY_RELOCS
4987 && !ppc_elf_hash_entry (h)->has_sda_refs
4988 && !htab->is_vxworks
4989 && !h->def_regular
4990 && !readonly_dynrelocs (h))
4992 h->non_got_ref = 0;
4993 return TRUE;
4996 if (h->size == 0)
4998 (*_bfd_error_handler) (_("dynamic variable `%s' is zero size"),
4999 h->root.root.string);
5000 return TRUE;
5003 /* We must allocate the symbol in our .dynbss section, which will
5004 become part of the .bss section of the executable. There will be
5005 an entry for this symbol in the .dynsym section. The dynamic
5006 object will contain position independent code, so all references
5007 from the dynamic object to this symbol will go through the global
5008 offset table. The dynamic linker will use the .dynsym entry to
5009 determine the address it must put in the global offset table, so
5010 both the dynamic object and the regular object will refer to the
5011 same memory location for the variable.
5013 Of course, if the symbol is referenced using SDAREL relocs, we
5014 must instead allocate it in .sbss. */
5016 if (ppc_elf_hash_entry (h)->has_sda_refs)
5017 s = htab->dynsbss;
5018 else
5019 s = htab->dynbss;
5020 BFD_ASSERT (s != NULL);
5022 /* We must generate a R_PPC_COPY reloc to tell the dynamic linker to
5023 copy the initial value out of the dynamic object and into the
5024 runtime process image. We need to remember the offset into the
5025 .rela.bss section we are going to use. */
5026 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
5028 asection *srel;
5030 if (ppc_elf_hash_entry (h)->has_sda_refs)
5031 srel = htab->relsbss;
5032 else
5033 srel = htab->relbss;
5034 BFD_ASSERT (srel != NULL);
5035 srel->size += sizeof (Elf32_External_Rela);
5036 h->needs_copy = 1;
5039 return _bfd_elf_adjust_dynamic_copy (h, s);
5042 /* Generate a symbol to mark plt call stubs. For non-PIC code the sym is
5043 xxxxxxxx.plt_call32.<callee> where xxxxxxxx is a hex number, usually 0,
5044 specifying the addend on the plt relocation. For -fpic code, the sym
5045 is xxxxxxxx.plt_pic32.<callee>, and for -fPIC
5046 xxxxxxxx.got2.plt_pic32.<callee>. */
5048 static bfd_boolean
5049 add_stub_sym (struct plt_entry *ent,
5050 struct elf_link_hash_entry *h,
5051 struct bfd_link_info *info)
5053 struct elf_link_hash_entry *sh;
5054 size_t len1, len2, len3;
5055 char *name;
5056 const char *stub;
5057 struct ppc_elf_link_hash_table *htab = ppc_elf_hash_table (info);
5059 if (info->shared)
5060 stub = ".plt_pic32.";
5061 else
5062 stub = ".plt_call32.";
5064 len1 = strlen (h->root.root.string);
5065 len2 = strlen (stub);
5066 len3 = 0;
5067 if (ent->sec)
5068 len3 = strlen (ent->sec->name);
5069 name = bfd_malloc (len1 + len2 + len3 + 9);
5070 if (name == NULL)
5071 return FALSE;
5072 sprintf (name, "%08x", (unsigned) ent->addend & 0xffffffff);
5073 if (ent->sec)
5074 memcpy (name + 8, ent->sec->name, len3);
5075 memcpy (name + 8 + len3, stub, len2);
5076 memcpy (name + 8 + len3 + len2, h->root.root.string, len1 + 1);
5077 sh = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
5078 if (sh == NULL)
5079 return FALSE;
5080 if (sh->root.type == bfd_link_hash_new)
5082 sh->root.type = bfd_link_hash_defined;
5083 sh->root.u.def.section = htab->glink;
5084 sh->root.u.def.value = ent->glink_offset;
5085 sh->ref_regular = 1;
5086 sh->def_regular = 1;
5087 sh->ref_regular_nonweak = 1;
5088 sh->forced_local = 1;
5089 sh->non_elf = 0;
5091 return TRUE;
5094 /* Allocate NEED contiguous space in .got, and return the offset.
5095 Handles allocation of the got header when crossing 32k. */
5097 static bfd_vma
5098 allocate_got (struct ppc_elf_link_hash_table *htab, unsigned int need)
5100 bfd_vma where;
5101 unsigned int max_before_header;
5103 if (htab->plt_type == PLT_VXWORKS)
5105 where = htab->got->size;
5106 htab->got->size += need;
5108 else
5110 max_before_header = htab->plt_type == PLT_NEW ? 32768 : 32764;
5111 if (need <= htab->got_gap)
5113 where = max_before_header - htab->got_gap;
5114 htab->got_gap -= need;
5116 else
5118 if (htab->got->size + need > max_before_header
5119 && htab->got->size <= max_before_header)
5121 htab->got_gap = max_before_header - htab->got->size;
5122 htab->got->size = max_before_header + htab->got_header_size;
5124 where = htab->got->size;
5125 htab->got->size += need;
5128 return where;
5131 /* Allocate space in associated reloc sections for dynamic relocs. */
5133 static bfd_boolean
5134 allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
5136 struct bfd_link_info *info = inf;
5137 struct ppc_elf_link_hash_entry *eh;
5138 struct ppc_elf_link_hash_table *htab;
5139 struct ppc_elf_dyn_relocs *p;
5141 if (h->root.type == bfd_link_hash_indirect)
5142 return TRUE;
5144 if (h->root.type == bfd_link_hash_warning)
5145 /* When warning symbols are created, they **replace** the "real"
5146 entry in the hash table, thus we never get to see the real
5147 symbol in a hash traversal. So look at it now. */
5148 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5150 htab = ppc_elf_hash_table (info);
5151 if (htab->elf.dynamic_sections_created
5152 || h->type == STT_GNU_IFUNC)
5154 struct plt_entry *ent;
5155 bfd_boolean doneone = FALSE;
5156 bfd_vma plt_offset = 0, glink_offset = 0;
5157 bfd_boolean dyn;
5159 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
5160 if (ent->plt.refcount > 0)
5162 /* Make sure this symbol is output as a dynamic symbol. */
5163 if (h->dynindx == -1
5164 && !h->forced_local
5165 && !h->def_regular
5166 && htab->elf.dynamic_sections_created)
5168 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5169 return FALSE;
5172 dyn = htab->elf.dynamic_sections_created;
5173 if (info->shared
5174 || h->type == STT_GNU_IFUNC
5175 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h))
5177 asection *s = htab->plt;
5178 if (!dyn || h->dynindx == -1)
5179 s = htab->iplt;
5181 if (htab->plt_type == PLT_NEW || !dyn || h->dynindx == -1)
5183 if (!doneone)
5185 plt_offset = s->size;
5186 s->size += 4;
5188 ent->plt.offset = plt_offset;
5190 s = htab->glink;
5191 if (!doneone || info->shared)
5193 glink_offset = s->size;
5194 s->size += GLINK_ENTRY_SIZE;
5195 if (h == htab->tls_get_addr
5196 && !htab->no_tls_get_addr_opt)
5197 s->size += TLS_GET_ADDR_GLINK_SIZE - GLINK_ENTRY_SIZE;
5199 if (!doneone
5200 && !info->shared
5201 && h->def_dynamic
5202 && !h->def_regular)
5204 h->root.u.def.section = s;
5205 h->root.u.def.value = glink_offset;
5207 ent->glink_offset = glink_offset;
5209 if (htab->emit_stub_syms
5210 && !add_stub_sym (ent, h, info))
5211 return FALSE;
5213 else
5215 if (!doneone)
5217 /* If this is the first .plt entry, make room
5218 for the special first entry. */
5219 if (s->size == 0)
5220 s->size += htab->plt_initial_entry_size;
5222 /* The PowerPC PLT is actually composed of two
5223 parts, the first part is 2 words (for a load
5224 and a jump), and then there is a remaining
5225 word available at the end. */
5226 plt_offset = (htab->plt_initial_entry_size
5227 + (htab->plt_slot_size
5228 * ((s->size
5229 - htab->plt_initial_entry_size)
5230 / htab->plt_entry_size)));
5232 /* If this symbol is not defined in a regular
5233 file, and we are not generating a shared
5234 library, then set the symbol to this location
5235 in the .plt. This is to avoid text
5236 relocations, and is required to make
5237 function pointers compare as equal between
5238 the normal executable and the shared library. */
5239 if (! info->shared
5240 && h->def_dynamic
5241 && !h->def_regular)
5243 h->root.u.def.section = s;
5244 h->root.u.def.value = plt_offset;
5247 /* Make room for this entry. */
5248 s->size += htab->plt_entry_size;
5249 /* After the 8192nd entry, room for two entries
5250 is allocated. */
5251 if (htab->plt_type == PLT_OLD
5252 && (s->size - htab->plt_initial_entry_size)
5253 / htab->plt_entry_size
5254 > PLT_NUM_SINGLE_ENTRIES)
5255 s->size += htab->plt_entry_size;
5257 ent->plt.offset = plt_offset;
5260 /* We also need to make an entry in the .rela.plt section. */
5261 if (!doneone)
5263 if (!htab->elf.dynamic_sections_created
5264 || h->dynindx == -1)
5265 htab->reliplt->size += sizeof (Elf32_External_Rela);
5266 else
5268 htab->relplt->size += sizeof (Elf32_External_Rela);
5270 if (htab->plt_type == PLT_VXWORKS)
5272 /* Allocate space for the unloaded relocations. */
5273 if (!info->shared
5274 && htab->elf.dynamic_sections_created)
5276 if (ent->plt.offset
5277 == (bfd_vma) htab->plt_initial_entry_size)
5279 htab->srelplt2->size
5280 += (sizeof (Elf32_External_Rela)
5281 * VXWORKS_PLTRESOLVE_RELOCS);
5284 htab->srelplt2->size
5285 += (sizeof (Elf32_External_Rela)
5286 * VXWORKS_PLT_NON_JMP_SLOT_RELOCS);
5289 /* Every PLT entry has an associated GOT entry in
5290 .got.plt. */
5291 htab->sgotplt->size += 4;
5294 doneone = TRUE;
5297 else
5298 ent->plt.offset = (bfd_vma) -1;
5300 else
5301 ent->plt.offset = (bfd_vma) -1;
5303 if (!doneone)
5305 h->plt.plist = NULL;
5306 h->needs_plt = 0;
5309 else
5311 h->plt.plist = NULL;
5312 h->needs_plt = 0;
5315 eh = (struct ppc_elf_link_hash_entry *) h;
5316 if (eh->elf.got.refcount > 0)
5318 bfd_boolean dyn;
5319 unsigned int need;
5321 /* Make sure this symbol is output as a dynamic symbol. */
5322 if (eh->elf.dynindx == -1
5323 && !eh->elf.forced_local
5324 && eh->elf.type != STT_GNU_IFUNC
5325 && htab->elf.dynamic_sections_created)
5327 if (!bfd_elf_link_record_dynamic_symbol (info, &eh->elf))
5328 return FALSE;
5331 need = 0;
5332 if ((eh->tls_mask & TLS_TLS) != 0)
5334 if ((eh->tls_mask & TLS_LD) != 0)
5336 if (!eh->elf.def_dynamic)
5337 /* We'll just use htab->tlsld_got.offset. This should
5338 always be the case. It's a little odd if we have
5339 a local dynamic reloc against a non-local symbol. */
5340 htab->tlsld_got.refcount += 1;
5341 else
5342 need += 8;
5344 if ((eh->tls_mask & TLS_GD) != 0)
5345 need += 8;
5346 if ((eh->tls_mask & (TLS_TPREL | TLS_TPRELGD)) != 0)
5347 need += 4;
5348 if ((eh->tls_mask & TLS_DTPREL) != 0)
5349 need += 4;
5351 else
5352 need += 4;
5353 if (need == 0)
5354 eh->elf.got.offset = (bfd_vma) -1;
5355 else
5357 eh->elf.got.offset = allocate_got (htab, need);
5358 dyn = htab->elf.dynamic_sections_created;
5359 if ((info->shared
5360 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, &eh->elf))
5361 && (ELF_ST_VISIBILITY (eh->elf.other) == STV_DEFAULT
5362 || eh->elf.root.type != bfd_link_hash_undefweak))
5364 asection *rsec = htab->relgot;
5365 /* All the entries we allocated need relocs.
5366 Except LD only needs one. */
5367 if ((eh->tls_mask & TLS_LD) != 0
5368 && eh->elf.def_dynamic)
5369 need -= 4;
5370 rsec->size += need * (sizeof (Elf32_External_Rela) / 4);
5374 else
5375 eh->elf.got.offset = (bfd_vma) -1;
5377 if (eh->dyn_relocs == NULL
5378 || !htab->elf.dynamic_sections_created)
5379 return TRUE;
5381 /* In the shared -Bsymbolic case, discard space allocated for
5382 dynamic pc-relative relocs against symbols which turn out to be
5383 defined in regular objects. For the normal shared case, discard
5384 space for relocs that have become local due to symbol visibility
5385 changes. */
5387 if (info->shared)
5389 /* Relocs that use pc_count are those that appear on a call insn,
5390 or certain REL relocs (see must_be_dyn_reloc) that can be
5391 generated via assembly. We want calls to protected symbols to
5392 resolve directly to the function rather than going via the plt.
5393 If people want function pointer comparisons to work as expected
5394 then they should avoid writing weird assembly. */
5395 if (SYMBOL_CALLS_LOCAL (info, h))
5397 struct ppc_elf_dyn_relocs **pp;
5399 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
5401 p->count -= p->pc_count;
5402 p->pc_count = 0;
5403 if (p->count == 0)
5404 *pp = p->next;
5405 else
5406 pp = &p->next;
5410 if (htab->is_vxworks)
5412 struct ppc_elf_dyn_relocs **pp;
5414 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
5416 if (strcmp (p->sec->output_section->name, ".tls_vars") == 0)
5417 *pp = p->next;
5418 else
5419 pp = &p->next;
5423 /* Discard relocs on undefined symbols that must be local. */
5424 if (eh->dyn_relocs != NULL
5425 && h->root.type == bfd_link_hash_undefined
5426 && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
5427 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
5428 eh->dyn_relocs = NULL;
5430 /* Also discard relocs on undefined weak syms with non-default
5431 visibility. */
5432 if (eh->dyn_relocs != NULL
5433 && h->root.type == bfd_link_hash_undefweak)
5435 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
5436 eh->dyn_relocs = NULL;
5438 /* Make sure undefined weak symbols are output as a dynamic
5439 symbol in PIEs. */
5440 else if (h->dynindx == -1
5441 && !h->forced_local
5442 && !h->def_regular)
5444 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5445 return FALSE;
5449 else if (ELIMINATE_COPY_RELOCS)
5451 /* For the non-shared case, discard space for relocs against
5452 symbols which turn out to need copy relocs or are not
5453 dynamic. */
5455 if (!h->non_got_ref
5456 && !h->def_regular)
5458 /* Make sure this symbol is output as a dynamic symbol.
5459 Undefined weak syms won't yet be marked as dynamic. */
5460 if (h->dynindx == -1
5461 && !h->forced_local)
5463 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5464 return FALSE;
5467 /* If that succeeded, we know we'll be keeping all the
5468 relocs. */
5469 if (h->dynindx != -1)
5470 goto keep;
5473 eh->dyn_relocs = NULL;
5475 keep: ;
5478 /* Finally, allocate space. */
5479 for (p = eh->dyn_relocs; p != NULL; p = p->next)
5481 asection *sreloc = elf_section_data (p->sec)->sreloc;
5482 if (!htab->elf.dynamic_sections_created)
5483 sreloc = htab->reliplt;
5484 sreloc->size += p->count * sizeof (Elf32_External_Rela);
5487 return TRUE;
5490 /* Set DF_TEXTREL if we find any dynamic relocs that apply to
5491 read-only sections. */
5493 static bfd_boolean
5494 maybe_set_textrel (struct elf_link_hash_entry *h, void *info)
5496 if (h->root.type == bfd_link_hash_indirect)
5497 return TRUE;
5499 if (h->root.type == bfd_link_hash_warning)
5500 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5502 if (readonly_dynrelocs (h))
5504 ((struct bfd_link_info *) info)->flags |= DF_TEXTREL;
5506 /* Not an error, just cut short the traversal. */
5507 return FALSE;
5509 return TRUE;
5512 /* Set the sizes of the dynamic sections. */
5514 static bfd_boolean
5515 ppc_elf_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
5516 struct bfd_link_info *info)
5518 struct ppc_elf_link_hash_table *htab;
5519 asection *s;
5520 bfd_boolean relocs;
5521 bfd *ibfd;
5523 #ifdef DEBUG
5524 fprintf (stderr, "ppc_elf_size_dynamic_sections called\n");
5525 #endif
5527 htab = ppc_elf_hash_table (info);
5528 BFD_ASSERT (htab->elf.dynobj != NULL);
5530 if (elf_hash_table (info)->dynamic_sections_created)
5532 /* Set the contents of the .interp section to the interpreter. */
5533 if (info->executable)
5535 s = bfd_get_section_by_name (htab->elf.dynobj, ".interp");
5536 BFD_ASSERT (s != NULL);
5537 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
5538 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
5542 if (htab->plt_type == PLT_OLD)
5543 htab->got_header_size = 16;
5544 else if (htab->plt_type == PLT_NEW)
5545 htab->got_header_size = 12;
5547 /* Set up .got offsets for local syms, and space for local dynamic
5548 relocs. */
5549 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
5551 bfd_signed_vma *local_got;
5552 bfd_signed_vma *end_local_got;
5553 struct plt_entry **local_plt;
5554 struct plt_entry **end_local_plt;
5555 char *lgot_masks;
5556 bfd_size_type locsymcount;
5557 Elf_Internal_Shdr *symtab_hdr;
5559 if (!is_ppc_elf (ibfd))
5560 continue;
5562 for (s = ibfd->sections; s != NULL; s = s->next)
5564 struct ppc_elf_dyn_relocs *p;
5566 for (p = ((struct ppc_elf_dyn_relocs *)
5567 elf_section_data (s)->local_dynrel);
5568 p != NULL;
5569 p = p->next)
5571 if (!bfd_is_abs_section (p->sec)
5572 && bfd_is_abs_section (p->sec->output_section))
5574 /* Input section has been discarded, either because
5575 it is a copy of a linkonce section or due to
5576 linker script /DISCARD/, so we'll be discarding
5577 the relocs too. */
5579 else if (htab->is_vxworks
5580 && strcmp (p->sec->output_section->name,
5581 ".tls_vars") == 0)
5583 /* Relocations in vxworks .tls_vars sections are
5584 handled specially by the loader. */
5586 else if (p->count != 0)
5588 asection *sreloc = elf_section_data (p->sec)->sreloc;
5589 if (!htab->elf.dynamic_sections_created)
5590 sreloc = htab->reliplt;
5591 sreloc->size += p->count * sizeof (Elf32_External_Rela);
5592 if ((p->sec->output_section->flags
5593 & (SEC_READONLY | SEC_ALLOC))
5594 == (SEC_READONLY | SEC_ALLOC))
5595 info->flags |= DF_TEXTREL;
5600 local_got = elf_local_got_refcounts (ibfd);
5601 if (!local_got)
5602 continue;
5604 symtab_hdr = &elf_symtab_hdr (ibfd);
5605 locsymcount = symtab_hdr->sh_info;
5606 end_local_got = local_got + locsymcount;
5607 local_plt = (struct plt_entry **) end_local_got;
5608 end_local_plt = local_plt + locsymcount;
5609 lgot_masks = (char *) end_local_plt;
5611 for (; local_got < end_local_got; ++local_got, ++lgot_masks)
5612 if (*local_got > 0)
5614 unsigned int need = 0;
5615 if ((*lgot_masks & TLS_TLS) != 0)
5617 if ((*lgot_masks & TLS_GD) != 0)
5618 need += 8;
5619 if ((*lgot_masks & TLS_LD) != 0)
5620 htab->tlsld_got.refcount += 1;
5621 if ((*lgot_masks & (TLS_TPREL | TLS_TPRELGD)) != 0)
5622 need += 4;
5623 if ((*lgot_masks & TLS_DTPREL) != 0)
5624 need += 4;
5626 else
5627 need += 4;
5628 if (need == 0)
5629 *local_got = (bfd_vma) -1;
5630 else
5632 *local_got = allocate_got (htab, need);
5633 if (info->shared)
5634 htab->relgot->size += (need
5635 * (sizeof (Elf32_External_Rela) / 4));
5638 else
5639 *local_got = (bfd_vma) -1;
5641 if (htab->is_vxworks)
5642 continue;
5644 /* Allocate space for calls to local STT_GNU_IFUNC syms in .iplt. */
5645 for (; local_plt < end_local_plt; ++local_plt)
5647 struct plt_entry *ent;
5648 bfd_boolean doneone = FALSE;
5649 bfd_vma plt_offset = 0, glink_offset = 0;
5651 for (ent = *local_plt; ent != NULL; ent = ent->next)
5652 if (ent->plt.refcount > 0)
5654 s = htab->iplt;
5656 if (!doneone)
5658 plt_offset = s->size;
5659 s->size += 4;
5661 ent->plt.offset = plt_offset;
5663 s = htab->glink;
5664 if (!doneone || info->shared)
5666 glink_offset = s->size;
5667 s->size += GLINK_ENTRY_SIZE;
5669 ent->glink_offset = glink_offset;
5671 if (!doneone)
5673 htab->reliplt->size += sizeof (Elf32_External_Rela);
5674 doneone = TRUE;
5677 else
5678 ent->plt.offset = (bfd_vma) -1;
5682 /* Allocate space for global sym dynamic relocs. */
5683 elf_link_hash_traverse (elf_hash_table (info), allocate_dynrelocs, info);
5685 if (htab->tlsld_got.refcount > 0)
5687 htab->tlsld_got.offset = allocate_got (htab, 8);
5688 if (info->shared)
5689 htab->relgot->size += sizeof (Elf32_External_Rela);
5691 else
5692 htab->tlsld_got.offset = (bfd_vma) -1;
5694 if (htab->got != NULL && htab->plt_type != PLT_VXWORKS)
5696 unsigned int g_o_t = 32768;
5698 /* If we haven't allocated the header, do so now. When we get here,
5699 for old plt/got the got size will be 0 to 32764 (not allocated),
5700 or 32780 to 65536 (header allocated). For new plt/got, the
5701 corresponding ranges are 0 to 32768 and 32780 to 65536. */
5702 if (htab->got->size <= 32768)
5704 g_o_t = htab->got->size;
5705 if (htab->plt_type == PLT_OLD)
5706 g_o_t += 4;
5707 htab->got->size += htab->got_header_size;
5710 htab->elf.hgot->root.u.def.value = g_o_t;
5712 if (info->shared)
5714 struct elf_link_hash_entry *sda = htab->sdata[0].sym;
5715 if (sda != NULL
5716 && !(sda->root.type == bfd_link_hash_defined
5717 || sda->root.type == bfd_link_hash_defweak))
5719 sda->root.type = bfd_link_hash_defined;
5720 sda->root.u.def.section = htab->elf.hgot->root.u.def.section;
5721 sda->root.u.def.value = htab->elf.hgot->root.u.def.value;
5725 if (htab->glink != NULL
5726 && htab->glink->size != 0
5727 && htab->elf.dynamic_sections_created)
5729 htab->glink_pltresolve = htab->glink->size;
5730 /* Space for the branch table. */
5731 htab->glink->size += htab->glink->size / (GLINK_ENTRY_SIZE / 4) - 4;
5732 /* Pad out to align the start of PLTresolve. */
5733 htab->glink->size += -htab->glink->size & 15;
5734 htab->glink->size += GLINK_PLTRESOLVE;
5736 if (htab->emit_stub_syms)
5738 struct elf_link_hash_entry *sh;
5739 sh = elf_link_hash_lookup (&htab->elf, "__glink",
5740 TRUE, FALSE, FALSE);
5741 if (sh == NULL)
5742 return FALSE;
5743 if (sh->root.type == bfd_link_hash_new)
5745 sh->root.type = bfd_link_hash_defined;
5746 sh->root.u.def.section = htab->glink;
5747 sh->root.u.def.value = htab->glink_pltresolve;
5748 sh->ref_regular = 1;
5749 sh->def_regular = 1;
5750 sh->ref_regular_nonweak = 1;
5751 sh->forced_local = 1;
5752 sh->non_elf = 0;
5754 sh = elf_link_hash_lookup (&htab->elf, "__glink_PLTresolve",
5755 TRUE, FALSE, FALSE);
5756 if (sh == NULL)
5757 return FALSE;
5758 if (sh->root.type == bfd_link_hash_new)
5760 sh->root.type = bfd_link_hash_defined;
5761 sh->root.u.def.section = htab->glink;
5762 sh->root.u.def.value = htab->glink->size - GLINK_PLTRESOLVE;
5763 sh->ref_regular = 1;
5764 sh->def_regular = 1;
5765 sh->ref_regular_nonweak = 1;
5766 sh->forced_local = 1;
5767 sh->non_elf = 0;
5772 /* We've now determined the sizes of the various dynamic sections.
5773 Allocate memory for them. */
5774 relocs = FALSE;
5775 for (s = htab->elf.dynobj->sections; s != NULL; s = s->next)
5777 bfd_boolean strip_section = TRUE;
5779 if ((s->flags & SEC_LINKER_CREATED) == 0)
5780 continue;
5782 if (s == htab->plt
5783 || s == htab->got)
5785 /* We'd like to strip these sections if they aren't needed, but if
5786 we've exported dynamic symbols from them we must leave them.
5787 It's too late to tell BFD to get rid of the symbols. */
5788 if (htab->elf.hplt != NULL)
5789 strip_section = FALSE;
5790 /* Strip this section if we don't need it; see the
5791 comment below. */
5793 else if (s == htab->iplt
5794 || s == htab->glink
5795 || s == htab->sgotplt
5796 || s == htab->sbss
5797 || s == htab->dynbss
5798 || s == htab->dynsbss
5799 || s == htab->sdata[0].section
5800 || s == htab->sdata[1].section)
5802 /* Strip these too. */
5804 else if (CONST_STRNEQ (bfd_get_section_name (dynobj, s), ".rela"))
5806 if (s->size != 0)
5808 /* Remember whether there are any relocation sections. */
5809 relocs = TRUE;
5811 /* We use the reloc_count field as a counter if we need
5812 to copy relocs into the output file. */
5813 s->reloc_count = 0;
5816 else
5818 /* It's not one of our sections, so don't allocate space. */
5819 continue;
5822 if (s->size == 0 && strip_section)
5824 /* If we don't need this section, strip it from the
5825 output file. This is mostly to handle .rela.bss and
5826 .rela.plt. We must create both sections in
5827 create_dynamic_sections, because they must be created
5828 before the linker maps input sections to output
5829 sections. The linker does that before
5830 adjust_dynamic_symbol is called, and it is that
5831 function which decides whether anything needs to go
5832 into these sections. */
5833 s->flags |= SEC_EXCLUDE;
5834 continue;
5837 if ((s->flags & SEC_HAS_CONTENTS) == 0)
5838 continue;
5840 /* Allocate memory for the section contents. */
5841 s->contents = bfd_zalloc (htab->elf.dynobj, s->size);
5842 if (s->contents == NULL)
5843 return FALSE;
5846 if (htab->elf.dynamic_sections_created)
5848 /* Add some entries to the .dynamic section. We fill in the
5849 values later, in ppc_elf_finish_dynamic_sections, but we
5850 must add the entries now so that we get the correct size for
5851 the .dynamic section. The DT_DEBUG entry is filled in by the
5852 dynamic linker and used by the debugger. */
5853 #define add_dynamic_entry(TAG, VAL) \
5854 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
5856 if (info->executable)
5858 if (!add_dynamic_entry (DT_DEBUG, 0))
5859 return FALSE;
5862 if (htab->plt != NULL && htab->plt->size != 0)
5864 if (!add_dynamic_entry (DT_PLTGOT, 0)
5865 || !add_dynamic_entry (DT_PLTRELSZ, 0)
5866 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
5867 || !add_dynamic_entry (DT_JMPREL, 0))
5868 return FALSE;
5871 if (htab->glink != NULL && htab->glink->size != 0)
5873 if (!add_dynamic_entry (DT_PPC_GOT, 0))
5874 return FALSE;
5875 if (!htab->no_tls_get_addr_opt
5876 && htab->tls_get_addr != NULL
5877 && htab->tls_get_addr->plt.plist != NULL
5878 && !add_dynamic_entry (DT_PPC_TLSOPT, 0))
5879 return FALSE;
5882 if (relocs)
5884 if (!add_dynamic_entry (DT_RELA, 0)
5885 || !add_dynamic_entry (DT_RELASZ, 0)
5886 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf32_External_Rela)))
5887 return FALSE;
5890 /* If any dynamic relocs apply to a read-only section, then we
5891 need a DT_TEXTREL entry. */
5892 if ((info->flags & DF_TEXTREL) == 0)
5893 elf_link_hash_traverse (elf_hash_table (info), maybe_set_textrel,
5894 info);
5896 if ((info->flags & DF_TEXTREL) != 0)
5898 if (!add_dynamic_entry (DT_TEXTREL, 0))
5899 return FALSE;
5901 if (htab->is_vxworks
5902 && !elf_vxworks_add_dynamic_entries (output_bfd, info))
5903 return FALSE;
5905 #undef add_dynamic_entry
5907 return TRUE;
5910 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
5912 static bfd_boolean
5913 ppc_elf_hash_symbol (struct elf_link_hash_entry *h)
5915 if (h->plt.plist != NULL
5916 && !h->def_regular
5917 && (!h->pointer_equality_needed
5918 || !h->ref_regular_nonweak))
5919 return FALSE;
5921 return _bfd_elf_hash_symbol (h);
5924 #define ARRAY_SIZE(a) (sizeof (a) / sizeof ((a)[0]))
5926 /* Relaxation trampolines. r12 is available for clobbering (r11, is
5927 used for some functions that are allowed to break the ABI). */
5928 static const int shared_stub_entry[] =
5930 0x7c0802a6, /* mflr 0 */
5931 0x429f0005, /* bcl 20, 31, .Lxxx */
5932 0x7d8802a6, /* mflr 12 */
5933 0x3d8c0000, /* addis 12, 12, (xxx-.Lxxx)@ha */
5934 0x398c0008, /* addi 12, 12, (xxx-.Lxxx)@l */
5935 0x7c0803a6, /* mtlr 0 */
5936 0x7d8903a6, /* mtctr 12 */
5937 0x4e800420, /* bctr */
5940 static const int stub_entry[] =
5942 0x3d800000, /* lis 12,xxx@ha */
5943 0x398c0000, /* addi 12,12,xxx@l */
5944 0x7d8903a6, /* mtctr 12 */
5945 0x4e800420, /* bctr */
5948 static bfd_boolean
5949 ppc_elf_relax_section (bfd *abfd,
5950 asection *isec,
5951 struct bfd_link_info *link_info,
5952 bfd_boolean *again)
5954 struct one_fixup
5956 struct one_fixup *next;
5957 asection *tsec;
5958 /* Final link, can use the symbol offset. For a
5959 relocatable link we use the symbol's index. */
5960 bfd_vma toff;
5961 bfd_vma trampoff;
5964 Elf_Internal_Shdr *symtab_hdr;
5965 bfd_byte *contents = NULL;
5966 Elf_Internal_Sym *isymbuf = NULL;
5967 Elf_Internal_Rela *internal_relocs = NULL;
5968 Elf_Internal_Rela *irel, *irelend;
5969 struct one_fixup *fixups = NULL;
5970 unsigned changes = 0;
5971 struct ppc_elf_link_hash_table *htab;
5972 bfd_size_type trampoff;
5973 asection *got2;
5975 *again = FALSE;
5977 /* Nothing to do if there are no relocations, and no need to do
5978 anything with non-alloc or non-code sections. */
5979 if ((isec->flags & SEC_ALLOC) == 0
5980 || (isec->flags & SEC_CODE) == 0
5981 || (isec->flags & SEC_RELOC) == 0
5982 || isec->reloc_count == 0)
5983 return TRUE;
5985 /* We cannot represent the required PIC relocs in the output, so don't
5986 do anything. The linker doesn't support mixing -shared and -r
5987 anyway. */
5988 if (link_info->relocatable && link_info->shared)
5989 return TRUE;
5991 trampoff = (isec->size + 3) & (bfd_vma) -4;
5992 /* Space for a branch around any trampolines. */
5993 trampoff += 4;
5995 symtab_hdr = &elf_symtab_hdr (abfd);
5997 /* Get a copy of the native relocations. */
5998 internal_relocs = _bfd_elf_link_read_relocs (abfd, isec, NULL, NULL,
5999 link_info->keep_memory);
6000 if (internal_relocs == NULL)
6001 goto error_return;
6003 htab = ppc_elf_hash_table (link_info);
6004 got2 = bfd_get_section_by_name (abfd, ".got2");
6006 irelend = internal_relocs + isec->reloc_count;
6007 for (irel = internal_relocs; irel < irelend; irel++)
6009 unsigned long r_type = ELF32_R_TYPE (irel->r_info);
6010 bfd_vma toff, roff;
6011 asection *tsec;
6012 struct one_fixup *f;
6013 size_t insn_offset = 0;
6014 bfd_vma max_branch_offset, val;
6015 bfd_byte *hit_addr;
6016 unsigned long t0;
6017 struct elf_link_hash_entry *h;
6018 struct plt_entry **plist;
6019 unsigned char sym_type;
6021 switch (r_type)
6023 case R_PPC_REL24:
6024 case R_PPC_LOCAL24PC:
6025 case R_PPC_PLTREL24:
6026 max_branch_offset = 1 << 25;
6027 break;
6029 case R_PPC_REL14:
6030 case R_PPC_REL14_BRTAKEN:
6031 case R_PPC_REL14_BRNTAKEN:
6032 max_branch_offset = 1 << 15;
6033 break;
6035 default:
6036 continue;
6039 /* Get the value of the symbol referred to by the reloc. */
6040 h = NULL;
6041 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
6043 /* A local symbol. */
6044 Elf_Internal_Sym *isym;
6046 /* Read this BFD's local symbols. */
6047 if (isymbuf == NULL)
6049 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
6050 if (isymbuf == NULL)
6051 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
6052 symtab_hdr->sh_info, 0,
6053 NULL, NULL, NULL);
6054 if (isymbuf == 0)
6055 goto error_return;
6057 isym = isymbuf + ELF32_R_SYM (irel->r_info);
6058 if (isym->st_shndx == SHN_UNDEF)
6059 tsec = bfd_und_section_ptr;
6060 else if (isym->st_shndx == SHN_ABS)
6061 tsec = bfd_abs_section_ptr;
6062 else if (isym->st_shndx == SHN_COMMON)
6063 tsec = bfd_com_section_ptr;
6064 else
6065 tsec = bfd_section_from_elf_index (abfd, isym->st_shndx);
6067 toff = isym->st_value;
6068 sym_type = ELF_ST_TYPE (isym->st_info);
6070 else
6072 /* Global symbol handling. */
6073 unsigned long indx;
6075 indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
6076 h = elf_sym_hashes (abfd)[indx];
6078 while (h->root.type == bfd_link_hash_indirect
6079 || h->root.type == bfd_link_hash_warning)
6080 h = (struct elf_link_hash_entry *) h->root.u.i.link;
6082 if (h->root.type == bfd_link_hash_defined
6083 || h->root.type == bfd_link_hash_defweak)
6085 tsec = h->root.u.def.section;
6086 toff = h->root.u.def.value;
6088 else if (h->root.type == bfd_link_hash_undefined
6089 || h->root.type == bfd_link_hash_undefweak)
6091 tsec = bfd_und_section_ptr;
6092 toff = link_info->relocatable ? indx : 0;
6094 else
6095 continue;
6097 sym_type = h->type;
6100 /* The condition here under which we call find_plt_ent must
6101 match that in relocate_section. If we call find_plt_ent here
6102 but not in relocate_section, or vice versa, then the branch
6103 destination used here may be incorrect. */
6104 plist = NULL;
6105 if (h != NULL)
6107 /* We know is_branch_reloc (r_type) is true. */
6108 if (h->type == STT_GNU_IFUNC
6109 || r_type == R_PPC_PLTREL24)
6110 plist = &h->plt.plist;
6112 else if (sym_type == STT_GNU_IFUNC
6113 && elf_local_got_offsets (abfd) != NULL)
6115 bfd_vma *local_got_offsets = elf_local_got_offsets (abfd);
6116 struct plt_entry **local_plt = (struct plt_entry **)
6117 (local_got_offsets + symtab_hdr->sh_info);
6118 plist = local_plt + ELF32_R_SYM (irel->r_info);
6120 if (plist != NULL)
6122 bfd_vma addend = 0;
6123 struct plt_entry *ent;
6125 if (r_type == R_PPC_PLTREL24 && link_info->shared)
6126 addend = irel->r_addend;
6127 ent = find_plt_ent (plist, got2, addend);
6128 if (ent != NULL)
6130 if (htab->plt_type == PLT_NEW
6131 || h == NULL
6132 || !htab->elf.dynamic_sections_created
6133 || h->dynindx == -1)
6135 tsec = htab->glink;
6136 toff = ent->glink_offset;
6138 else
6140 tsec = htab->plt;
6141 toff = ent->plt.offset;
6146 /* If the branch and target are in the same section, you have
6147 no hope of adding stubs. We'll error out later should the
6148 branch overflow. */
6149 if (tsec == isec)
6150 continue;
6152 /* There probably isn't any reason to handle symbols in
6153 SEC_MERGE sections; SEC_MERGE doesn't seem a likely
6154 attribute for a code section, and we are only looking at
6155 branches. However, implement it correctly here as a
6156 reference for other target relax_section functions. */
6157 if (0 && tsec->sec_info_type == ELF_INFO_TYPE_MERGE)
6159 /* At this stage in linking, no SEC_MERGE symbol has been
6160 adjusted, so all references to such symbols need to be
6161 passed through _bfd_merged_section_offset. (Later, in
6162 relocate_section, all SEC_MERGE symbols *except* for
6163 section symbols have been adjusted.)
6165 gas may reduce relocations against symbols in SEC_MERGE
6166 sections to a relocation against the section symbol when
6167 the original addend was zero. When the reloc is against
6168 a section symbol we should include the addend in the
6169 offset passed to _bfd_merged_section_offset, since the
6170 location of interest is the original symbol. On the
6171 other hand, an access to "sym+addend" where "sym" is not
6172 a section symbol should not include the addend; Such an
6173 access is presumed to be an offset from "sym"; The
6174 location of interest is just "sym". */
6175 if (sym_type == STT_SECTION)
6176 toff += irel->r_addend;
6178 toff = _bfd_merged_section_offset (abfd, &tsec,
6179 elf_section_data (tsec)->sec_info,
6180 toff);
6182 if (sym_type != STT_SECTION)
6183 toff += irel->r_addend;
6185 /* PLTREL24 addends are special. */
6186 else if (r_type != R_PPC_PLTREL24)
6187 toff += irel->r_addend;
6189 /* Attempted -shared link of non-pic code loses. */
6190 if (tsec->output_section == NULL)
6191 continue;
6193 roff = irel->r_offset;
6195 /* If the branch is in range, no need to do anything. */
6196 if (tsec != bfd_und_section_ptr
6197 && (!link_info->relocatable
6198 /* A relocatable link may have sections moved during
6199 final link, so do not presume they remain in range. */
6200 || tsec->output_section == isec->output_section))
6202 bfd_vma symaddr, reladdr;
6204 symaddr = tsec->output_section->vma + tsec->output_offset + toff;
6205 reladdr = isec->output_section->vma + isec->output_offset + roff;
6206 if (symaddr - reladdr + max_branch_offset < 2 * max_branch_offset)
6207 continue;
6210 /* Look for an existing fixup to this address. */
6211 for (f = fixups; f ; f = f->next)
6212 if (f->tsec == tsec && f->toff == toff)
6213 break;
6215 if (f == NULL)
6217 size_t size;
6218 unsigned long stub_rtype;
6220 val = trampoff - roff;
6221 if (val >= max_branch_offset)
6222 /* Oh dear, we can't reach a trampoline. Don't try to add
6223 one. We'll report an error later. */
6224 continue;
6226 if (link_info->shared)
6228 size = 4 * ARRAY_SIZE (shared_stub_entry);
6229 insn_offset = 12;
6231 else
6233 size = 4 * ARRAY_SIZE (stub_entry);
6234 insn_offset = 0;
6236 stub_rtype = R_PPC_RELAX;
6237 if (tsec == htab->plt
6238 || tsec == htab->glink)
6240 stub_rtype = R_PPC_RELAX_PLT;
6241 if (r_type == R_PPC_PLTREL24)
6242 stub_rtype = R_PPC_RELAX_PLTREL24;
6245 /* Hijack the old relocation. Since we need two
6246 relocations for this use a "composite" reloc. */
6247 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
6248 stub_rtype);
6249 irel->r_offset = trampoff + insn_offset;
6250 if (r_type == R_PPC_PLTREL24
6251 && stub_rtype != R_PPC_RELAX_PLTREL24)
6252 irel->r_addend = 0;
6254 /* Record the fixup so we don't do it again this section. */
6255 f = bfd_malloc (sizeof (*f));
6256 f->next = fixups;
6257 f->tsec = tsec;
6258 f->toff = toff;
6259 f->trampoff = trampoff;
6260 fixups = f;
6262 trampoff += size;
6263 changes++;
6265 else
6267 val = f->trampoff - roff;
6268 if (val >= max_branch_offset)
6269 continue;
6271 /* Nop out the reloc, since we're finalizing things here. */
6272 irel->r_info = ELF32_R_INFO (0, R_PPC_NONE);
6275 /* Get the section contents. */
6276 if (contents == NULL)
6278 /* Get cached copy if it exists. */
6279 if (elf_section_data (isec)->this_hdr.contents != NULL)
6280 contents = elf_section_data (isec)->this_hdr.contents;
6281 else
6283 /* Go get them off disk. */
6284 if (!bfd_malloc_and_get_section (abfd, isec, &contents))
6285 goto error_return;
6289 /* Fix up the existing branch to hit the trampoline. */
6290 hit_addr = contents + roff;
6291 switch (r_type)
6293 case R_PPC_REL24:
6294 case R_PPC_LOCAL24PC:
6295 case R_PPC_PLTREL24:
6296 t0 = bfd_get_32 (abfd, hit_addr);
6297 t0 &= ~0x3fffffc;
6298 t0 |= val & 0x3fffffc;
6299 bfd_put_32 (abfd, t0, hit_addr);
6300 break;
6302 case R_PPC_REL14:
6303 case R_PPC_REL14_BRTAKEN:
6304 case R_PPC_REL14_BRNTAKEN:
6305 t0 = bfd_get_32 (abfd, hit_addr);
6306 t0 &= ~0xfffc;
6307 t0 |= val & 0xfffc;
6308 bfd_put_32 (abfd, t0, hit_addr);
6309 break;
6313 /* Write out the trampolines. */
6314 if (fixups != NULL)
6316 const int *stub;
6317 bfd_byte *dest;
6318 bfd_vma val;
6319 int i, size;
6323 struct one_fixup *f = fixups;
6324 fixups = fixups->next;
6325 free (f);
6327 while (fixups);
6329 contents = bfd_realloc_or_free (contents, trampoff);
6330 if (contents == NULL)
6331 goto error_return;
6333 isec->size = (isec->size + 3) & (bfd_vma) -4;
6334 /* Branch around the trampolines. */
6335 val = B + trampoff - isec->size;
6336 dest = contents + isec->size;
6337 isec->size = trampoff;
6338 bfd_put_32 (abfd, val, dest);
6339 dest += 4;
6341 if (link_info->shared)
6343 stub = shared_stub_entry;
6344 size = ARRAY_SIZE (shared_stub_entry);
6346 else
6348 stub = stub_entry;
6349 size = ARRAY_SIZE (stub_entry);
6352 i = 0;
6353 while (dest < contents + trampoff)
6355 bfd_put_32 (abfd, stub[i], dest);
6356 i++;
6357 if (i == size)
6358 i = 0;
6359 dest += 4;
6361 BFD_ASSERT (i == 0);
6364 if (isymbuf != NULL
6365 && symtab_hdr->contents != (unsigned char *) isymbuf)
6367 if (! link_info->keep_memory)
6368 free (isymbuf);
6369 else
6371 /* Cache the symbols for elf_link_input_bfd. */
6372 symtab_hdr->contents = (unsigned char *) isymbuf;
6376 if (contents != NULL
6377 && elf_section_data (isec)->this_hdr.contents != contents)
6379 if (!changes && !link_info->keep_memory)
6380 free (contents);
6381 else
6383 /* Cache the section contents for elf_link_input_bfd. */
6384 elf_section_data (isec)->this_hdr.contents = contents;
6388 if (changes != 0)
6390 /* Append sufficient NOP relocs so we can write out relocation
6391 information for the trampolines. */
6392 Elf_Internal_Rela *new_relocs = bfd_malloc ((changes + isec->reloc_count)
6393 * sizeof (*new_relocs));
6394 unsigned ix;
6396 if (!new_relocs)
6397 goto error_return;
6398 memcpy (new_relocs, internal_relocs,
6399 isec->reloc_count * sizeof (*new_relocs));
6400 for (ix = changes; ix--;)
6402 irel = new_relocs + ix + isec->reloc_count;
6404 irel->r_info = ELF32_R_INFO (0, R_PPC_NONE);
6406 if (internal_relocs != elf_section_data (isec)->relocs)
6407 free (internal_relocs);
6408 elf_section_data (isec)->relocs = new_relocs;
6409 isec->reloc_count += changes;
6410 elf_section_data (isec)->rel_hdr.sh_size
6411 += changes * elf_section_data (isec)->rel_hdr.sh_entsize;
6413 else if (elf_section_data (isec)->relocs != internal_relocs)
6414 free (internal_relocs);
6416 *again = changes != 0;
6417 if (!*again && link_info->relocatable)
6419 /* Convert the internal relax relocs to external form. */
6420 for (irel = internal_relocs; irel < irelend; irel++)
6421 if (ELF32_R_TYPE (irel->r_info) == R_PPC_RELAX)
6423 unsigned long r_symndx = ELF32_R_SYM (irel->r_info);
6425 /* Rewrite the reloc and convert one of the trailing nop
6426 relocs to describe this relocation. */
6427 BFD_ASSERT (ELF32_R_TYPE (irelend[-1].r_info) == R_PPC_NONE);
6428 /* The relocs are at the bottom 2 bytes */
6429 irel[0].r_offset += 2;
6430 memmove (irel + 1, irel, (irelend - irel - 1) * sizeof (*irel));
6431 irel[0].r_info = ELF32_R_INFO (r_symndx, R_PPC_ADDR16_HA);
6432 irel[1].r_offset += 4;
6433 irel[1].r_info = ELF32_R_INFO (r_symndx, R_PPC_ADDR16_LO);
6434 irel++;
6438 return TRUE;
6440 error_return:
6441 if (isymbuf != NULL && (unsigned char *) isymbuf != symtab_hdr->contents)
6442 free (isymbuf);
6443 if (contents != NULL
6444 && elf_section_data (isec)->this_hdr.contents != contents)
6445 free (contents);
6446 if (internal_relocs != NULL
6447 && elf_section_data (isec)->relocs != internal_relocs)
6448 free (internal_relocs);
6449 return FALSE;
6452 /* What to do when ld finds relocations against symbols defined in
6453 discarded sections. */
6455 static unsigned int
6456 ppc_elf_action_discarded (asection *sec)
6458 if (strcmp (".fixup", sec->name) == 0)
6459 return 0;
6461 if (strcmp (".got2", sec->name) == 0)
6462 return 0;
6464 return _bfd_elf_default_action_discarded (sec);
6467 /* Fill in the address for a pointer generated in a linker section. */
6469 static bfd_vma
6470 elf_finish_pointer_linker_section (bfd *input_bfd,
6471 elf_linker_section_t *lsect,
6472 struct elf_link_hash_entry *h,
6473 bfd_vma relocation,
6474 const Elf_Internal_Rela *rel)
6476 elf_linker_section_pointers_t *linker_section_ptr;
6478 BFD_ASSERT (lsect != NULL);
6480 if (h != NULL)
6482 /* Handle global symbol. */
6483 struct ppc_elf_link_hash_entry *eh;
6485 eh = (struct ppc_elf_link_hash_entry *) h;
6486 BFD_ASSERT (eh->elf.def_regular);
6487 linker_section_ptr = eh->linker_section_pointer;
6489 else
6491 /* Handle local symbol. */
6492 unsigned long r_symndx = ELF32_R_SYM (rel->r_info);
6494 BFD_ASSERT (is_ppc_elf (input_bfd));
6495 BFD_ASSERT (elf_local_ptr_offsets (input_bfd) != NULL);
6496 linker_section_ptr = elf_local_ptr_offsets (input_bfd)[r_symndx];
6499 linker_section_ptr = elf_find_pointer_linker_section (linker_section_ptr,
6500 rel->r_addend,
6501 lsect);
6502 BFD_ASSERT (linker_section_ptr != NULL);
6504 /* Offset will always be a multiple of four, so use the bottom bit
6505 as a "written" flag. */
6506 if ((linker_section_ptr->offset & 1) == 0)
6508 bfd_put_32 (lsect->section->owner,
6509 relocation + linker_section_ptr->addend,
6510 lsect->section->contents + linker_section_ptr->offset);
6511 linker_section_ptr->offset += 1;
6514 relocation = (lsect->section->output_section->vma
6515 + lsect->section->output_offset
6516 + linker_section_ptr->offset - 1
6517 - SYM_VAL (lsect->sym));
6519 #ifdef DEBUG
6520 fprintf (stderr,
6521 "Finish pointer in linker section %s, offset = %ld (0x%lx)\n",
6522 lsect->name, (long) relocation, (long) relocation);
6523 #endif
6525 return relocation;
6528 #define PPC_LO(v) ((v) & 0xffff)
6529 #define PPC_HI(v) (((v) >> 16) & 0xffff)
6530 #define PPC_HA(v) PPC_HI ((v) + 0x8000)
6532 static void
6533 write_glink_stub (struct plt_entry *ent, asection *plt_sec, unsigned char *p,
6534 struct bfd_link_info *info)
6536 struct ppc_elf_link_hash_table *htab = ppc_elf_hash_table (info);
6537 bfd *output_bfd = info->output_bfd;
6538 bfd_vma plt;
6540 plt = ((ent->plt.offset & ~1)
6541 + plt_sec->output_section->vma
6542 + plt_sec->output_offset);
6544 if (info->shared)
6546 bfd_vma got = 0;
6548 if (ent->addend >= 32768)
6549 got = (ent->addend
6550 + ent->sec->output_section->vma
6551 + ent->sec->output_offset);
6552 else if (htab->elf.hgot != NULL)
6553 got = SYM_VAL (htab->elf.hgot);
6555 plt -= got;
6557 if (plt + 0x8000 < 0x10000)
6559 bfd_put_32 (output_bfd, LWZ_11_30 + PPC_LO (plt), p);
6560 p += 4;
6561 bfd_put_32 (output_bfd, MTCTR_11, p);
6562 p += 4;
6563 bfd_put_32 (output_bfd, BCTR, p);
6564 p += 4;
6565 bfd_put_32 (output_bfd, NOP, p);
6566 p += 4;
6568 else
6570 bfd_put_32 (output_bfd, ADDIS_11_30 + PPC_HA (plt), p);
6571 p += 4;
6572 bfd_put_32 (output_bfd, LWZ_11_11 + PPC_LO (plt), p);
6573 p += 4;
6574 bfd_put_32 (output_bfd, MTCTR_11, p);
6575 p += 4;
6576 bfd_put_32 (output_bfd, BCTR, p);
6577 p += 4;
6580 else
6582 bfd_put_32 (output_bfd, LIS_11 + PPC_HA (plt), p);
6583 p += 4;
6584 bfd_put_32 (output_bfd, LWZ_11_11 + PPC_LO (plt), p);
6585 p += 4;
6586 bfd_put_32 (output_bfd, MTCTR_11, p);
6587 p += 4;
6588 bfd_put_32 (output_bfd, BCTR, p);
6589 p += 4;
6593 /* Return true if symbol is defined statically. */
6595 static bfd_boolean
6596 is_static_defined (struct elf_link_hash_entry *h)
6598 return ((h->root.type == bfd_link_hash_defined
6599 || h->root.type == bfd_link_hash_defweak)
6600 && h->root.u.def.section != NULL
6601 && h->root.u.def.section->output_section != NULL);
6604 /* If INSN is an opcode that may be used with an @tls operand, return
6605 the transformed insn for TLS optimisation, otherwise return 0. If
6606 REG is non-zero only match an insn with RB or RA equal to REG. */
6608 unsigned int
6609 _bfd_elf_ppc_at_tls_transform (unsigned int insn, unsigned int reg)
6611 unsigned int rtra;
6613 if ((insn & (0x3f << 26)) != 31 << 26)
6614 return 0;
6616 if (reg == 0 || ((insn >> 11) & 0x1f) == reg)
6617 rtra = insn & ((1 << 26) - (1 << 16));
6618 else if (((insn >> 16) & 0x1f) == reg)
6619 rtra = (insn & (0x1f << 21)) | ((insn & (0x1f << 11)) << 5);
6620 else
6621 return 0;
6623 if ((insn & (0x3ff << 1)) == 266 << 1)
6624 /* add -> addi. */
6625 insn = 14 << 26;
6626 else if ((insn & (0x1f << 1)) == 23 << 1
6627 && ((insn & (0x1f << 6)) < 14 << 6
6628 || ((insn & (0x1f << 6)) >= 16 << 6
6629 && (insn & (0x1f << 6)) < 24 << 6)))
6630 /* load and store indexed -> dform. */
6631 insn = (32 | ((insn >> 6) & 0x1f)) << 26;
6632 else if ((insn & (((0x1a << 5) | 0x1f) << 1)) == 21 << 1)
6633 /* ldx, ldux, stdx, stdux -> ld, ldu, std, stdu. */
6634 insn = ((58 | ((insn >> 6) & 4)) << 26) | ((insn >> 6) & 1);
6635 else if ((insn & (((0x1f << 5) | 0x1f) << 1)) == 341 << 1)
6636 /* lwax -> lwa. */
6637 insn = (58 << 26) | 2;
6638 else
6639 return 0;
6640 insn |= rtra;
6641 return insn;
6644 /* If INSN is an opcode that may be used with an @tprel operand, return
6645 the transformed insn for an undefined weak symbol, ie. with the
6646 thread pointer REG operand removed. Otherwise return 0. */
6648 unsigned int
6649 _bfd_elf_ppc_at_tprel_transform (unsigned int insn, unsigned int reg)
6651 if ((insn & (0x1f << 16)) == reg << 16
6652 && ((insn & (0x3f << 26)) == 14u << 26 /* addi */
6653 || (insn & (0x3f << 26)) == 15u << 26 /* addis */
6654 || (insn & (0x3f << 26)) == 32u << 26 /* lwz */
6655 || (insn & (0x3f << 26)) == 34u << 26 /* lbz */
6656 || (insn & (0x3f << 26)) == 36u << 26 /* stw */
6657 || (insn & (0x3f << 26)) == 38u << 26 /* stb */
6658 || (insn & (0x3f << 26)) == 40u << 26 /* lhz */
6659 || (insn & (0x3f << 26)) == 42u << 26 /* lha */
6660 || (insn & (0x3f << 26)) == 44u << 26 /* sth */
6661 || (insn & (0x3f << 26)) == 46u << 26 /* lmw */
6662 || (insn & (0x3f << 26)) == 47u << 26 /* stmw */
6663 || (insn & (0x3f << 26)) == 48u << 26 /* lfs */
6664 || (insn & (0x3f << 26)) == 50u << 26 /* lfd */
6665 || (insn & (0x3f << 26)) == 52u << 26 /* stfs */
6666 || (insn & (0x3f << 26)) == 54u << 26 /* stfd */
6667 || ((insn & (0x3f << 26)) == 58u << 26 /* lwa,ld,lmd */
6668 && (insn & 3) != 1)
6669 || ((insn & (0x3f << 26)) == 62u << 26 /* std, stmd */
6670 && ((insn & 3) == 0 || (insn & 3) == 3))))
6672 insn &= ~(0x1f << 16);
6674 else if ((insn & (0x1f << 21)) == reg << 21
6675 && ((insn & (0x3e << 26)) == 24u << 26 /* ori, oris */
6676 || (insn & (0x3e << 26)) == 26u << 26 /* xori,xoris */
6677 || (insn & (0x3e << 26)) == 28u << 26 /* andi,andis */))
6679 insn &= ~(0x1f << 21);
6680 insn |= (insn & (0x1f << 16)) << 5;
6681 if ((insn & (0x3e << 26)) == 26 << 26 /* xori,xoris */)
6682 insn -= 2 >> 26; /* convert to ori,oris */
6684 else
6685 insn = 0;
6686 return insn;
6689 /* The RELOCATE_SECTION function is called by the ELF backend linker
6690 to handle the relocations for a section.
6692 The relocs are always passed as Rela structures; if the section
6693 actually uses Rel structures, the r_addend field will always be
6694 zero.
6696 This function is responsible for adjust the section contents as
6697 necessary, and (if using Rela relocs and generating a
6698 relocatable output file) adjusting the reloc addend as
6699 necessary.
6701 This function does not have to worry about setting the reloc
6702 address or the reloc symbol index.
6704 LOCAL_SYMS is a pointer to the swapped in local symbols.
6706 LOCAL_SECTIONS is an array giving the section in the input file
6707 corresponding to the st_shndx field of each local symbol.
6709 The global hash table entry for the global symbols can be found
6710 via elf_sym_hashes (input_bfd).
6712 When generating relocatable output, this function must handle
6713 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
6714 going to be the section symbol corresponding to the output
6715 section, which means that the addend must be adjusted
6716 accordingly. */
6718 static bfd_boolean
6719 ppc_elf_relocate_section (bfd *output_bfd,
6720 struct bfd_link_info *info,
6721 bfd *input_bfd,
6722 asection *input_section,
6723 bfd_byte *contents,
6724 Elf_Internal_Rela *relocs,
6725 Elf_Internal_Sym *local_syms,
6726 asection **local_sections)
6728 Elf_Internal_Shdr *symtab_hdr;
6729 struct elf_link_hash_entry **sym_hashes;
6730 struct ppc_elf_link_hash_table *htab;
6731 Elf_Internal_Rela *rel;
6732 Elf_Internal_Rela *relend;
6733 Elf_Internal_Rela outrel;
6734 asection *got2, *sreloc = NULL;
6735 bfd_vma *local_got_offsets;
6736 bfd_boolean ret = TRUE;
6737 bfd_vma d_offset = (bfd_big_endian (output_bfd) ? 2 : 0);
6738 bfd_boolean is_vxworks_tls;
6740 #ifdef DEBUG
6741 _bfd_error_handler ("ppc_elf_relocate_section called for %B section %A, "
6742 "%ld relocations%s",
6743 input_bfd, input_section,
6744 (long) input_section->reloc_count,
6745 (info->relocatable) ? " (relocatable)" : "");
6746 #endif
6748 got2 = bfd_get_section_by_name (input_bfd, ".got2");
6750 /* Initialize howto table if not already done. */
6751 if (!ppc_elf_howto_table[R_PPC_ADDR32])
6752 ppc_elf_howto_init ();
6754 htab = ppc_elf_hash_table (info);
6755 local_got_offsets = elf_local_got_offsets (input_bfd);
6756 symtab_hdr = &elf_symtab_hdr (input_bfd);
6757 sym_hashes = elf_sym_hashes (input_bfd);
6758 /* We have to handle relocations in vxworks .tls_vars sections
6759 specially, because the dynamic loader is 'weird'. */
6760 is_vxworks_tls = (htab->is_vxworks && info->shared
6761 && !strcmp (input_section->output_section->name,
6762 ".tls_vars"));
6763 rel = relocs;
6764 relend = relocs + input_section->reloc_count;
6765 for (; rel < relend; rel++)
6767 enum elf_ppc_reloc_type r_type;
6768 bfd_vma addend;
6769 bfd_reloc_status_type r;
6770 Elf_Internal_Sym *sym;
6771 asection *sec;
6772 struct elf_link_hash_entry *h;
6773 const char *sym_name;
6774 reloc_howto_type *howto;
6775 unsigned long r_symndx;
6776 bfd_vma relocation;
6777 bfd_vma branch_bit, from;
6778 bfd_boolean unresolved_reloc;
6779 bfd_boolean warned;
6780 unsigned int tls_type, tls_mask, tls_gd;
6781 struct plt_entry **ifunc;
6783 r_type = ELF32_R_TYPE (rel->r_info);
6784 sym = NULL;
6785 sec = NULL;
6786 h = NULL;
6787 unresolved_reloc = FALSE;
6788 warned = FALSE;
6789 r_symndx = ELF32_R_SYM (rel->r_info);
6791 if (r_symndx < symtab_hdr->sh_info)
6793 sym = local_syms + r_symndx;
6794 sec = local_sections[r_symndx];
6795 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym, sec);
6797 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
6799 else
6801 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
6802 r_symndx, symtab_hdr, sym_hashes,
6803 h, sec, relocation,
6804 unresolved_reloc, warned);
6806 sym_name = h->root.root.string;
6809 if (sec != NULL && elf_discarded_section (sec))
6811 /* For relocs against symbols from removed linkonce sections,
6812 or sections discarded by a linker script, we just want the
6813 section contents zeroed. Avoid any special processing. */
6814 howto = NULL;
6815 if (r_type < R_PPC_max)
6816 howto = ppc_elf_howto_table[r_type];
6817 _bfd_clear_contents (howto, input_bfd, contents + rel->r_offset);
6818 rel->r_info = 0;
6819 rel->r_addend = 0;
6820 continue;
6823 if (info->relocatable)
6825 if (got2 != NULL
6826 && r_type == R_PPC_PLTREL24
6827 && rel->r_addend >= 32768)
6829 /* R_PPC_PLTREL24 is rather special. If non-zero, the
6830 addend specifies the GOT pointer offset within .got2. */
6831 rel->r_addend += got2->output_offset;
6833 continue;
6836 /* TLS optimizations. Replace instruction sequences and relocs
6837 based on information we collected in tls_optimize. We edit
6838 RELOCS so that --emit-relocs will output something sensible
6839 for the final instruction stream. */
6840 tls_mask = 0;
6841 tls_gd = 0;
6842 if (h != NULL)
6843 tls_mask = ((struct ppc_elf_link_hash_entry *) h)->tls_mask;
6844 else if (local_got_offsets != NULL)
6846 struct plt_entry **local_plt;
6847 char *lgot_masks;
6848 local_plt
6849 = (struct plt_entry **) (local_got_offsets + symtab_hdr->sh_info);
6850 lgot_masks = (char *) (local_plt + symtab_hdr->sh_info);
6851 tls_mask = lgot_masks[r_symndx];
6854 /* Ensure reloc mapping code below stays sane. */
6855 if ((R_PPC_GOT_TLSLD16 & 3) != (R_PPC_GOT_TLSGD16 & 3)
6856 || (R_PPC_GOT_TLSLD16_LO & 3) != (R_PPC_GOT_TLSGD16_LO & 3)
6857 || (R_PPC_GOT_TLSLD16_HI & 3) != (R_PPC_GOT_TLSGD16_HI & 3)
6858 || (R_PPC_GOT_TLSLD16_HA & 3) != (R_PPC_GOT_TLSGD16_HA & 3)
6859 || (R_PPC_GOT_TLSLD16 & 3) != (R_PPC_GOT_TPREL16 & 3)
6860 || (R_PPC_GOT_TLSLD16_LO & 3) != (R_PPC_GOT_TPREL16_LO & 3)
6861 || (R_PPC_GOT_TLSLD16_HI & 3) != (R_PPC_GOT_TPREL16_HI & 3)
6862 || (R_PPC_GOT_TLSLD16_HA & 3) != (R_PPC_GOT_TPREL16_HA & 3))
6863 abort ();
6864 switch (r_type)
6866 default:
6867 break;
6869 case R_PPC_GOT_TPREL16:
6870 case R_PPC_GOT_TPREL16_LO:
6871 if ((tls_mask & TLS_TLS) != 0
6872 && (tls_mask & TLS_TPREL) == 0)
6874 bfd_vma insn;
6876 insn = bfd_get_32 (output_bfd, contents + rel->r_offset - d_offset);
6877 insn &= 31 << 21;
6878 insn |= 0x3c020000; /* addis 0,2,0 */
6879 bfd_put_32 (output_bfd, insn, contents + rel->r_offset - d_offset);
6880 r_type = R_PPC_TPREL16_HA;
6881 rel->r_info = ELF32_R_INFO (r_symndx, r_type);
6883 break;
6885 case R_PPC_TLS:
6886 if ((tls_mask & TLS_TLS) != 0
6887 && (tls_mask & TLS_TPREL) == 0)
6889 bfd_vma insn;
6891 insn = bfd_get_32 (output_bfd, contents + rel->r_offset);
6892 insn = _bfd_elf_ppc_at_tls_transform (insn, 2);
6893 if (insn == 0)
6894 abort ();
6895 bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
6896 r_type = R_PPC_TPREL16_LO;
6897 rel->r_info = ELF32_R_INFO (r_symndx, r_type);
6899 /* Was PPC_TLS which sits on insn boundary, now
6900 PPC_TPREL16_LO which is at low-order half-word. */
6901 rel->r_offset += d_offset;
6903 break;
6905 case R_PPC_GOT_TLSGD16_HI:
6906 case R_PPC_GOT_TLSGD16_HA:
6907 tls_gd = TLS_TPRELGD;
6908 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0)
6909 goto tls_gdld_hi;
6910 break;
6912 case R_PPC_GOT_TLSLD16_HI:
6913 case R_PPC_GOT_TLSLD16_HA:
6914 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0)
6916 tls_gdld_hi:
6917 if ((tls_mask & tls_gd) != 0)
6918 r_type = (((r_type - (R_PPC_GOT_TLSGD16 & 3)) & 3)
6919 + R_PPC_GOT_TPREL16);
6920 else
6922 bfd_put_32 (output_bfd, NOP, contents + rel->r_offset);
6923 rel->r_offset -= d_offset;
6924 r_type = R_PPC_NONE;
6926 rel->r_info = ELF32_R_INFO (r_symndx, r_type);
6928 break;
6930 case R_PPC_GOT_TLSGD16:
6931 case R_PPC_GOT_TLSGD16_LO:
6932 tls_gd = TLS_TPRELGD;
6933 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0)
6934 goto tls_ldgd_opt;
6935 break;
6937 case R_PPC_GOT_TLSLD16:
6938 case R_PPC_GOT_TLSLD16_LO:
6939 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0)
6941 unsigned int insn1, insn2;
6942 bfd_vma offset;
6944 tls_ldgd_opt:
6945 offset = (bfd_vma) -1;
6946 /* If not using the newer R_PPC_TLSGD/LD to mark
6947 __tls_get_addr calls, we must trust that the call
6948 stays with its arg setup insns, ie. that the next
6949 reloc is the __tls_get_addr call associated with
6950 the current reloc. Edit both insns. */
6951 if (input_section->has_tls_get_addr_call
6952 && rel + 1 < relend
6953 && branch_reloc_hash_match (input_bfd, rel + 1,
6954 htab->tls_get_addr))
6955 offset = rel[1].r_offset;
6956 if ((tls_mask & tls_gd) != 0)
6958 /* IE */
6959 insn1 = bfd_get_32 (output_bfd,
6960 contents + rel->r_offset - d_offset);
6961 insn1 &= (1 << 26) - 1;
6962 insn1 |= 32 << 26; /* lwz */
6963 if (offset != (bfd_vma) -1)
6965 rel[1].r_info = ELF32_R_INFO (STN_UNDEF, R_PPC_NONE);
6966 insn2 = 0x7c631214; /* add 3,3,2 */
6967 bfd_put_32 (output_bfd, insn2, contents + offset);
6969 r_type = (((r_type - (R_PPC_GOT_TLSGD16 & 3)) & 3)
6970 + R_PPC_GOT_TPREL16);
6971 rel->r_info = ELF32_R_INFO (r_symndx, r_type);
6973 else
6975 /* LE */
6976 insn1 = 0x3c620000; /* addis 3,2,0 */
6977 if (tls_gd == 0)
6979 /* Was an LD reloc. */
6980 for (r_symndx = 0;
6981 r_symndx < symtab_hdr->sh_info;
6982 r_symndx++)
6983 if (local_sections[r_symndx] == sec)
6984 break;
6985 if (r_symndx >= symtab_hdr->sh_info)
6986 r_symndx = 0;
6987 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
6988 if (r_symndx != 0)
6989 rel->r_addend -= (local_syms[r_symndx].st_value
6990 + sec->output_offset
6991 + sec->output_section->vma);
6993 r_type = R_PPC_TPREL16_HA;
6994 rel->r_info = ELF32_R_INFO (r_symndx, r_type);
6995 if (offset != (bfd_vma) -1)
6997 rel[1].r_info = ELF32_R_INFO (r_symndx, R_PPC_TPREL16_LO);
6998 rel[1].r_offset = offset + d_offset;
6999 rel[1].r_addend = rel->r_addend;
7000 insn2 = 0x38630000; /* addi 3,3,0 */
7001 bfd_put_32 (output_bfd, insn2, contents + offset);
7004 bfd_put_32 (output_bfd, insn1,
7005 contents + rel->r_offset - d_offset);
7006 if (tls_gd == 0)
7008 /* We changed the symbol on an LD reloc. Start over
7009 in order to get h, sym, sec etc. right. */
7010 rel--;
7011 continue;
7014 break;
7016 case R_PPC_TLSGD:
7017 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0)
7019 unsigned int insn2;
7020 bfd_vma offset = rel->r_offset;
7022 if ((tls_mask & TLS_TPRELGD) != 0)
7024 /* IE */
7025 r_type = R_PPC_NONE;
7026 insn2 = 0x7c631214; /* add 3,3,2 */
7028 else
7030 /* LE */
7031 r_type = R_PPC_TPREL16_LO;
7032 rel->r_offset += d_offset;
7033 insn2 = 0x38630000; /* addi 3,3,0 */
7035 rel->r_info = ELF32_R_INFO (r_symndx, r_type);
7036 bfd_put_32 (output_bfd, insn2, contents + offset);
7037 /* Zap the reloc on the _tls_get_addr call too. */
7038 BFD_ASSERT (offset == rel[1].r_offset);
7039 rel[1].r_info = ELF32_R_INFO (STN_UNDEF, R_PPC_NONE);
7041 break;
7043 case R_PPC_TLSLD:
7044 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0)
7046 unsigned int insn2;
7048 for (r_symndx = 0;
7049 r_symndx < symtab_hdr->sh_info;
7050 r_symndx++)
7051 if (local_sections[r_symndx] == sec)
7052 break;
7053 if (r_symndx >= symtab_hdr->sh_info)
7054 r_symndx = 0;
7055 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
7056 if (r_symndx != 0)
7057 rel->r_addend -= (local_syms[r_symndx].st_value
7058 + sec->output_offset
7059 + sec->output_section->vma);
7061 rel->r_info = ELF32_R_INFO (r_symndx, R_PPC_TPREL16_LO);
7062 rel->r_offset += d_offset;
7063 insn2 = 0x38630000; /* addi 3,3,0 */
7064 bfd_put_32 (output_bfd, insn2,
7065 contents + rel->r_offset - d_offset);
7066 /* Zap the reloc on the _tls_get_addr call too. */
7067 BFD_ASSERT (rel->r_offset - d_offset == rel[1].r_offset);
7068 rel[1].r_info = ELF32_R_INFO (STN_UNDEF, R_PPC_NONE);
7069 rel--;
7070 continue;
7072 break;
7075 /* Handle other relocations that tweak non-addend part of insn. */
7076 branch_bit = 0;
7077 switch (r_type)
7079 default:
7080 break;
7082 /* Branch taken prediction relocations. */
7083 case R_PPC_ADDR14_BRTAKEN:
7084 case R_PPC_REL14_BRTAKEN:
7085 branch_bit = BRANCH_PREDICT_BIT;
7086 /* Fall thru */
7088 /* Branch not taken prediction relocations. */
7089 case R_PPC_ADDR14_BRNTAKEN:
7090 case R_PPC_REL14_BRNTAKEN:
7092 bfd_vma insn;
7094 insn = bfd_get_32 (output_bfd, contents + rel->r_offset);
7095 insn &= ~BRANCH_PREDICT_BIT;
7096 insn |= branch_bit;
7098 from = (rel->r_offset
7099 + input_section->output_offset
7100 + input_section->output_section->vma);
7102 /* Invert 'y' bit if not the default. */
7103 if ((bfd_signed_vma) (relocation + rel->r_addend - from) < 0)
7104 insn ^= BRANCH_PREDICT_BIT;
7106 bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
7107 break;
7111 ifunc = NULL;
7112 if (!htab->is_vxworks)
7114 struct plt_entry *ent;
7116 if (h != NULL)
7118 if (h->type == STT_GNU_IFUNC)
7119 ifunc = &h->plt.plist;
7121 else if (local_got_offsets != NULL
7122 && ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
7124 struct plt_entry **local_plt;
7126 local_plt = (struct plt_entry **) (local_got_offsets
7127 + symtab_hdr->sh_info);
7128 ifunc = local_plt + r_symndx;
7131 ent = NULL;
7132 if (ifunc != NULL
7133 && (!info->shared
7134 || is_branch_reloc (r_type)))
7136 addend = 0;
7137 if (r_type == R_PPC_PLTREL24 && info->shared)
7138 addend = rel->r_addend;
7139 ent = find_plt_ent (ifunc, got2, addend);
7141 if (ent != NULL)
7143 if (h == NULL && (ent->plt.offset & 1) == 0)
7145 Elf_Internal_Rela rela;
7146 bfd_byte *loc;
7148 rela.r_offset = (htab->iplt->output_section->vma
7149 + htab->iplt->output_offset
7150 + ent->plt.offset);
7151 rela.r_info = ELF32_R_INFO (0, R_PPC_IRELATIVE);
7152 rela.r_addend = relocation;
7153 loc = htab->reliplt->contents;
7154 loc += (htab->reliplt->reloc_count++
7155 * sizeof (Elf32_External_Rela));
7156 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
7158 ent->plt.offset |= 1;
7160 if (h == NULL && (ent->glink_offset & 1) == 0)
7162 unsigned char *p = ((unsigned char *) htab->glink->contents
7163 + ent->glink_offset);
7164 write_glink_stub (ent, htab->iplt, p, info);
7165 ent->glink_offset |= 1;
7168 unresolved_reloc = FALSE;
7169 if (htab->plt_type == PLT_NEW
7170 || !htab->elf.dynamic_sections_created
7171 || h == NULL)
7172 relocation = (htab->glink->output_section->vma
7173 + htab->glink->output_offset
7174 + (ent->glink_offset & ~1));
7175 else
7176 relocation = (htab->plt->output_section->vma
7177 + htab->plt->output_offset
7178 + ent->plt.offset);
7182 addend = rel->r_addend;
7183 tls_type = 0;
7184 howto = NULL;
7185 if (r_type < R_PPC_max)
7186 howto = ppc_elf_howto_table[r_type];
7187 switch (r_type)
7189 default:
7190 (*_bfd_error_handler)
7191 (_("%B: unknown relocation type %d for symbol %s"),
7192 input_bfd, (int) r_type, sym_name);
7194 bfd_set_error (bfd_error_bad_value);
7195 ret = FALSE;
7196 continue;
7198 case R_PPC_NONE:
7199 case R_PPC_TLS:
7200 case R_PPC_TLSGD:
7201 case R_PPC_TLSLD:
7202 case R_PPC_EMB_MRKREF:
7203 case R_PPC_GNU_VTINHERIT:
7204 case R_PPC_GNU_VTENTRY:
7205 continue;
7207 /* GOT16 relocations. Like an ADDR16 using the symbol's
7208 address in the GOT as relocation value instead of the
7209 symbol's value itself. Also, create a GOT entry for the
7210 symbol and put the symbol value there. */
7211 case R_PPC_GOT_TLSGD16:
7212 case R_PPC_GOT_TLSGD16_LO:
7213 case R_PPC_GOT_TLSGD16_HI:
7214 case R_PPC_GOT_TLSGD16_HA:
7215 tls_type = TLS_TLS | TLS_GD;
7216 goto dogot;
7218 case R_PPC_GOT_TLSLD16:
7219 case R_PPC_GOT_TLSLD16_LO:
7220 case R_PPC_GOT_TLSLD16_HI:
7221 case R_PPC_GOT_TLSLD16_HA:
7222 tls_type = TLS_TLS | TLS_LD;
7223 goto dogot;
7225 case R_PPC_GOT_TPREL16:
7226 case R_PPC_GOT_TPREL16_LO:
7227 case R_PPC_GOT_TPREL16_HI:
7228 case R_PPC_GOT_TPREL16_HA:
7229 tls_type = TLS_TLS | TLS_TPREL;
7230 goto dogot;
7232 case R_PPC_GOT_DTPREL16:
7233 case R_PPC_GOT_DTPREL16_LO:
7234 case R_PPC_GOT_DTPREL16_HI:
7235 case R_PPC_GOT_DTPREL16_HA:
7236 tls_type = TLS_TLS | TLS_DTPREL;
7237 goto dogot;
7239 case R_PPC_GOT16:
7240 case R_PPC_GOT16_LO:
7241 case R_PPC_GOT16_HI:
7242 case R_PPC_GOT16_HA:
7243 tls_mask = 0;
7244 dogot:
7246 /* Relocation is to the entry for this symbol in the global
7247 offset table. */
7248 bfd_vma off;
7249 bfd_vma *offp;
7250 unsigned long indx;
7252 if (htab->got == NULL)
7253 abort ();
7255 indx = 0;
7256 if (tls_type == (TLS_TLS | TLS_LD)
7257 && (h == NULL
7258 || !h->def_dynamic))
7259 offp = &htab->tlsld_got.offset;
7260 else if (h != NULL)
7262 bfd_boolean dyn;
7263 dyn = htab->elf.dynamic_sections_created;
7264 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
7265 || (info->shared
7266 && SYMBOL_REFERENCES_LOCAL (info, h)))
7267 /* This is actually a static link, or it is a
7268 -Bsymbolic link and the symbol is defined
7269 locally, or the symbol was forced to be local
7270 because of a version file. */
7272 else
7274 indx = h->dynindx;
7275 unresolved_reloc = FALSE;
7277 offp = &h->got.offset;
7279 else
7281 if (local_got_offsets == NULL)
7282 abort ();
7283 offp = &local_got_offsets[r_symndx];
7286 /* The offset must always be a multiple of 4. We use the
7287 least significant bit to record whether we have already
7288 processed this entry. */
7289 off = *offp;
7290 if ((off & 1) != 0)
7291 off &= ~1;
7292 else
7294 unsigned int tls_m = (tls_mask
7295 & (TLS_LD | TLS_GD | TLS_DTPREL
7296 | TLS_TPREL | TLS_TPRELGD));
7298 if (offp == &htab->tlsld_got.offset)
7299 tls_m = TLS_LD;
7300 else if (h == NULL
7301 || !h->def_dynamic)
7302 tls_m &= ~TLS_LD;
7304 /* We might have multiple got entries for this sym.
7305 Initialize them all. */
7308 int tls_ty = 0;
7310 if ((tls_m & TLS_LD) != 0)
7312 tls_ty = TLS_TLS | TLS_LD;
7313 tls_m &= ~TLS_LD;
7315 else if ((tls_m & TLS_GD) != 0)
7317 tls_ty = TLS_TLS | TLS_GD;
7318 tls_m &= ~TLS_GD;
7320 else if ((tls_m & TLS_DTPREL) != 0)
7322 tls_ty = TLS_TLS | TLS_DTPREL;
7323 tls_m &= ~TLS_DTPREL;
7325 else if ((tls_m & (TLS_TPREL | TLS_TPRELGD)) != 0)
7327 tls_ty = TLS_TLS | TLS_TPREL;
7328 tls_m = 0;
7331 /* Generate relocs for the dynamic linker. */
7332 if ((info->shared || indx != 0)
7333 && (offp == &htab->tlsld_got.offset
7334 || h == NULL
7335 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
7336 || h->root.type != bfd_link_hash_undefweak))
7338 asection *rsec = htab->relgot;
7339 bfd_byte * loc;
7341 outrel.r_offset = (htab->got->output_section->vma
7342 + htab->got->output_offset
7343 + off);
7344 outrel.r_addend = 0;
7345 if (tls_ty & (TLS_LD | TLS_GD))
7347 outrel.r_info = ELF32_R_INFO (indx, R_PPC_DTPMOD32);
7348 if (tls_ty == (TLS_TLS | TLS_GD))
7350 loc = rsec->contents;
7351 loc += (rsec->reloc_count++
7352 * sizeof (Elf32_External_Rela));
7353 bfd_elf32_swap_reloca_out (output_bfd,
7354 &outrel, loc);
7355 outrel.r_offset += 4;
7356 outrel.r_info
7357 = ELF32_R_INFO (indx, R_PPC_DTPREL32);
7360 else if (tls_ty == (TLS_TLS | TLS_DTPREL))
7361 outrel.r_info = ELF32_R_INFO (indx, R_PPC_DTPREL32);
7362 else if (tls_ty == (TLS_TLS | TLS_TPREL))
7363 outrel.r_info = ELF32_R_INFO (indx, R_PPC_TPREL32);
7364 else if (indx != 0)
7365 outrel.r_info = ELF32_R_INFO (indx, R_PPC_GLOB_DAT);
7366 else if (ifunc != NULL)
7367 outrel.r_info = ELF32_R_INFO (0, R_PPC_IRELATIVE);
7368 else
7369 outrel.r_info = ELF32_R_INFO (0, R_PPC_RELATIVE);
7370 if (indx == 0 && tls_ty != (TLS_TLS | TLS_LD))
7372 outrel.r_addend += relocation;
7373 if (tls_ty & (TLS_GD | TLS_DTPREL | TLS_TPREL))
7374 outrel.r_addend -= htab->elf.tls_sec->vma;
7376 loc = rsec->contents;
7377 loc += (rsec->reloc_count++
7378 * sizeof (Elf32_External_Rela));
7379 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
7382 /* Init the .got section contents if we're not
7383 emitting a reloc. */
7384 else
7386 bfd_vma value = relocation;
7388 if (tls_ty == (TLS_TLS | TLS_LD))
7389 value = 1;
7390 else if (tls_ty != 0)
7392 value -= htab->elf.tls_sec->vma + DTP_OFFSET;
7393 if (tls_ty == (TLS_TLS | TLS_TPREL))
7394 value += DTP_OFFSET - TP_OFFSET;
7396 if (tls_ty == (TLS_TLS | TLS_GD))
7398 bfd_put_32 (output_bfd, value,
7399 htab->got->contents + off + 4);
7400 value = 1;
7403 bfd_put_32 (output_bfd, value,
7404 htab->got->contents + off);
7407 off += 4;
7408 if (tls_ty & (TLS_LD | TLS_GD))
7409 off += 4;
7411 while (tls_m != 0);
7413 off = *offp;
7414 *offp = off | 1;
7417 if (off >= (bfd_vma) -2)
7418 abort ();
7420 if ((tls_type & TLS_TLS) != 0)
7422 if (tls_type != (TLS_TLS | TLS_LD))
7424 if ((tls_mask & TLS_LD) != 0
7425 && !(h == NULL
7426 || !h->def_dynamic))
7427 off += 8;
7428 if (tls_type != (TLS_TLS | TLS_GD))
7430 if ((tls_mask & TLS_GD) != 0)
7431 off += 8;
7432 if (tls_type != (TLS_TLS | TLS_DTPREL))
7434 if ((tls_mask & TLS_DTPREL) != 0)
7435 off += 4;
7441 relocation = (htab->got->output_section->vma
7442 + htab->got->output_offset
7443 + off
7444 - SYM_VAL (htab->elf.hgot));
7446 /* Addends on got relocations don't make much sense.
7447 x+off@got is actually x@got+off, and since the got is
7448 generated by a hash table traversal, the value in the
7449 got at entry m+n bears little relation to the entry m. */
7450 if (addend != 0)
7451 (*_bfd_error_handler)
7452 (_("%B(%A+0x%lx): non-zero addend on %s reloc against `%s'"),
7453 input_bfd,
7454 input_section,
7455 (long) rel->r_offset,
7456 howto->name,
7457 sym_name);
7459 break;
7461 /* Relocations that need no special processing. */
7462 case R_PPC_LOCAL24PC:
7463 /* It makes no sense to point a local relocation
7464 at a symbol not in this object. */
7465 if (unresolved_reloc)
7467 if (! (*info->callbacks->undefined_symbol) (info,
7468 h->root.root.string,
7469 input_bfd,
7470 input_section,
7471 rel->r_offset,
7472 TRUE))
7473 return FALSE;
7474 continue;
7476 break;
7478 case R_PPC_DTPREL16:
7479 case R_PPC_DTPREL16_LO:
7480 case R_PPC_DTPREL16_HI:
7481 case R_PPC_DTPREL16_HA:
7482 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
7483 break;
7485 /* Relocations that may need to be propagated if this is a shared
7486 object. */
7487 case R_PPC_TPREL16:
7488 case R_PPC_TPREL16_LO:
7489 case R_PPC_TPREL16_HI:
7490 case R_PPC_TPREL16_HA:
7491 if (h != NULL
7492 && h->root.type == bfd_link_hash_undefweak
7493 && h->dynindx == -1)
7495 /* Make this relocation against an undefined weak symbol
7496 resolve to zero. This is really just a tweak, since
7497 code using weak externs ought to check that they are
7498 defined before using them. */
7499 bfd_byte *p = contents + rel->r_offset - d_offset;
7500 unsigned int insn = bfd_get_32 (output_bfd, p);
7501 insn = _bfd_elf_ppc_at_tprel_transform (insn, 2);
7502 if (insn != 0)
7503 bfd_put_32 (output_bfd, insn, p);
7504 break;
7506 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
7507 /* The TPREL16 relocs shouldn't really be used in shared
7508 libs as they will result in DT_TEXTREL being set, but
7509 support them anyway. */
7510 goto dodyn;
7512 case R_PPC_TPREL32:
7513 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
7514 goto dodyn;
7516 case R_PPC_DTPREL32:
7517 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
7518 goto dodyn;
7520 case R_PPC_DTPMOD32:
7521 relocation = 1;
7522 addend = 0;
7523 goto dodyn;
7525 case R_PPC_REL16:
7526 case R_PPC_REL16_LO:
7527 case R_PPC_REL16_HI:
7528 case R_PPC_REL16_HA:
7529 break;
7531 case R_PPC_REL32:
7532 if (h == NULL || h == htab->elf.hgot)
7533 break;
7534 /* fall through */
7536 case R_PPC_ADDR32:
7537 case R_PPC_ADDR16:
7538 case R_PPC_ADDR16_LO:
7539 case R_PPC_ADDR16_HI:
7540 case R_PPC_ADDR16_HA:
7541 case R_PPC_UADDR32:
7542 case R_PPC_UADDR16:
7543 goto dodyn;
7545 case R_PPC_REL24:
7546 case R_PPC_REL14:
7547 case R_PPC_REL14_BRTAKEN:
7548 case R_PPC_REL14_BRNTAKEN:
7549 /* If these relocations are not to a named symbol, they can be
7550 handled right here, no need to bother the dynamic linker. */
7551 if (SYMBOL_CALLS_LOCAL (info, h)
7552 || h == htab->elf.hgot)
7553 break;
7554 /* fall through */
7556 case R_PPC_ADDR24:
7557 case R_PPC_ADDR14:
7558 case R_PPC_ADDR14_BRTAKEN:
7559 case R_PPC_ADDR14_BRNTAKEN:
7560 if (h != NULL && !info->shared)
7561 break;
7562 /* fall through */
7564 dodyn:
7565 if ((input_section->flags & SEC_ALLOC) == 0
7566 || is_vxworks_tls)
7567 break;
7569 if ((info->shared
7570 && !(h != NULL
7571 && ((h->root.type == bfd_link_hash_undefined
7572 && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
7573 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
7574 || (h->root.type == bfd_link_hash_undefweak
7575 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)))
7576 && (must_be_dyn_reloc (info, r_type)
7577 || !SYMBOL_CALLS_LOCAL (info, h)))
7578 || (ELIMINATE_COPY_RELOCS
7579 && !info->shared
7580 && h != NULL
7581 && h->dynindx != -1
7582 && !h->non_got_ref
7583 && !h->def_regular))
7585 int skip;
7586 bfd_byte * loc;
7587 #ifdef DEBUG
7588 fprintf (stderr, "ppc_elf_relocate_section needs to "
7589 "create relocation for %s\n",
7590 (h && h->root.root.string
7591 ? h->root.root.string : "<unknown>"));
7592 #endif
7594 /* When generating a shared object, these relocations
7595 are copied into the output file to be resolved at run
7596 time. */
7597 if (sreloc == NULL)
7599 sreloc = elf_section_data (input_section)->sreloc;
7600 if (!htab->elf.dynamic_sections_created)
7601 sreloc = htab->reliplt;
7602 if (sreloc == NULL)
7603 return FALSE;
7606 skip = 0;
7607 outrel.r_offset =
7608 _bfd_elf_section_offset (output_bfd, info, input_section,
7609 rel->r_offset);
7610 if (outrel.r_offset == (bfd_vma) -1
7611 || outrel.r_offset == (bfd_vma) -2)
7612 skip = (int) outrel.r_offset;
7613 outrel.r_offset += (input_section->output_section->vma
7614 + input_section->output_offset);
7616 if (skip)
7617 memset (&outrel, 0, sizeof outrel);
7618 else if ((h != NULL
7619 && (h->root.type == bfd_link_hash_undefined
7620 || h->root.type == bfd_link_hash_undefweak))
7621 || !SYMBOL_REFERENCES_LOCAL (info, h))
7623 unresolved_reloc = FALSE;
7624 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
7625 outrel.r_addend = rel->r_addend;
7627 else
7629 outrel.r_addend = relocation + rel->r_addend;
7631 if (r_type != R_PPC_ADDR32)
7633 long indx = 0;
7635 if (ifunc != NULL)
7637 /* If we get here when building a static
7638 executable, then the libc startup function
7639 responsible for applying indirect function
7640 relocations is going to complain about
7641 the reloc type.
7642 If we get here when building a dynamic
7643 executable, it will be because we have
7644 a text relocation. The dynamic loader
7645 will set the text segment writable and
7646 non-executable to apply text relocations.
7647 So we'll segfault when trying to run the
7648 indirection function to resolve the reloc. */
7649 (*_bfd_error_handler)
7650 (_("%B(%A+0x%lx): relocation %s for indirect "
7651 "function %s unsupported"),
7652 input_bfd,
7653 input_section,
7654 (long) rel->r_offset,
7655 howto->name,
7656 sym_name);
7657 ret = FALSE;
7659 else if (r_symndx == 0 || bfd_is_abs_section (sec))
7661 else if (sec == NULL || sec->owner == NULL)
7663 bfd_set_error (bfd_error_bad_value);
7664 ret = FALSE;
7666 else
7668 asection *osec;
7670 /* We are turning this relocation into one
7671 against a section symbol. It would be
7672 proper to subtract the symbol's value,
7673 osec->vma, from the emitted reloc addend,
7674 but ld.so expects buggy relocs.
7675 FIXME: Why not always use a zero index? */
7676 osec = sec->output_section;
7677 indx = elf_section_data (osec)->dynindx;
7678 if (indx == 0)
7680 osec = htab->elf.text_index_section;
7681 indx = elf_section_data (osec)->dynindx;
7683 BFD_ASSERT (indx != 0);
7684 #ifdef DEBUG
7685 if (indx == 0)
7686 printf ("indx=%ld section=%s flags=%08x name=%s\n",
7687 indx, osec->name, osec->flags,
7688 h->root.root.string);
7689 #endif
7692 outrel.r_info = ELF32_R_INFO (indx, r_type);
7694 else if (ifunc != NULL)
7695 outrel.r_info = ELF32_R_INFO (0, R_PPC_IRELATIVE);
7696 else
7697 outrel.r_info = ELF32_R_INFO (0, R_PPC_RELATIVE);
7700 loc = sreloc->contents;
7701 loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rela);
7702 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
7704 if (skip == -1)
7705 continue;
7707 /* This reloc will be computed at runtime. We clear the memory
7708 so that it contains predictable value. */
7709 if (! skip
7710 && ((input_section->flags & SEC_ALLOC) != 0
7711 || ELF32_R_TYPE (outrel.r_info) != R_PPC_RELATIVE))
7713 relocation = howto->pc_relative ? outrel.r_offset : 0;
7714 addend = 0;
7715 break;
7718 break;
7720 case R_PPC_RELAX_PLT:
7721 case R_PPC_RELAX_PLTREL24:
7722 if (h != NULL)
7724 struct plt_entry *ent;
7725 bfd_vma got2_addend = 0;
7727 if (r_type == R_PPC_RELAX_PLTREL24)
7729 if (info->shared)
7730 got2_addend = addend;
7731 addend = 0;
7733 ent = find_plt_ent (&h->plt.plist, got2, got2_addend);
7734 if (htab->plt_type == PLT_NEW)
7735 relocation = (htab->glink->output_section->vma
7736 + htab->glink->output_offset
7737 + ent->glink_offset);
7738 else
7739 relocation = (htab->plt->output_section->vma
7740 + htab->plt->output_offset
7741 + ent->plt.offset);
7743 /* Fall thru */
7745 case R_PPC_RELAX:
7746 if (info->shared)
7747 relocation -= (input_section->output_section->vma
7748 + input_section->output_offset
7749 + rel->r_offset - 4);
7752 unsigned long t0;
7753 unsigned long t1;
7755 t0 = bfd_get_32 (output_bfd, contents + rel->r_offset);
7756 t1 = bfd_get_32 (output_bfd, contents + rel->r_offset + 4);
7758 /* We're clearing the bits for R_PPC_ADDR16_HA
7759 and R_PPC_ADDR16_LO here. */
7760 t0 &= ~0xffff;
7761 t1 &= ~0xffff;
7763 /* t0 is HA, t1 is LO */
7764 relocation += addend;
7765 t0 |= ((relocation + 0x8000) >> 16) & 0xffff;
7766 t1 |= relocation & 0xffff;
7768 bfd_put_32 (output_bfd, t0, contents + rel->r_offset);
7769 bfd_put_32 (output_bfd, t1, contents + rel->r_offset + 4);
7771 /* Rewrite the reloc and convert one of the trailing nop
7772 relocs to describe this relocation. */
7773 BFD_ASSERT (ELF32_R_TYPE (relend[-1].r_info) == R_PPC_NONE);
7774 /* The relocs are at the bottom 2 bytes */
7775 rel[0].r_offset += 2;
7776 memmove (rel + 1, rel, (relend - rel - 1) * sizeof (*rel));
7777 rel[0].r_info = ELF32_R_INFO (r_symndx, R_PPC_ADDR16_HA);
7778 rel[1].r_offset += 4;
7779 rel[1].r_info = ELF32_R_INFO (r_symndx, R_PPC_ADDR16_LO);
7780 rel++;
7782 continue;
7784 /* Indirect .sdata relocation. */
7785 case R_PPC_EMB_SDAI16:
7786 BFD_ASSERT (htab->sdata[0].section != NULL);
7787 if (!is_static_defined (htab->sdata[0].sym))
7789 unresolved_reloc = TRUE;
7790 break;
7792 relocation
7793 = elf_finish_pointer_linker_section (input_bfd, &htab->sdata[0],
7794 h, relocation, rel);
7795 addend = 0;
7796 break;
7798 /* Indirect .sdata2 relocation. */
7799 case R_PPC_EMB_SDA2I16:
7800 BFD_ASSERT (htab->sdata[1].section != NULL);
7801 if (!is_static_defined (htab->sdata[1].sym))
7803 unresolved_reloc = TRUE;
7804 break;
7806 relocation
7807 = elf_finish_pointer_linker_section (input_bfd, &htab->sdata[1],
7808 h, relocation, rel);
7809 addend = 0;
7810 break;
7812 /* Handle the TOC16 reloc. We want to use the offset within the .got
7813 section, not the actual VMA. This is appropriate when generating
7814 an embedded ELF object, for which the .got section acts like the
7815 AIX .toc section. */
7816 case R_PPC_TOC16: /* phony GOT16 relocations */
7817 if (sec == NULL || sec->output_section == NULL)
7819 unresolved_reloc = TRUE;
7820 break;
7822 BFD_ASSERT (strcmp (bfd_get_section_name (abfd, sec), ".got") == 0
7823 || strcmp (bfd_get_section_name (abfd, sec), ".cgot") == 0);
7825 addend -= sec->output_section->vma + sec->output_offset + 0x8000;
7826 break;
7828 case R_PPC_PLTREL24:
7829 if (h == NULL || ifunc != NULL)
7830 break;
7831 /* Relocation is to the entry for this symbol in the
7832 procedure linkage table. */
7834 struct plt_entry *ent = find_plt_ent (&h->plt.plist, got2,
7835 info->shared ? addend : 0);
7836 addend = 0;
7837 if (ent == NULL
7838 || htab->plt == NULL)
7840 /* We didn't make a PLT entry for this symbol. This
7841 happens when statically linking PIC code, or when
7842 using -Bsymbolic. */
7843 break;
7846 unresolved_reloc = FALSE;
7847 if (htab->plt_type == PLT_NEW)
7848 relocation = (htab->glink->output_section->vma
7849 + htab->glink->output_offset
7850 + ent->glink_offset);
7851 else
7852 relocation = (htab->plt->output_section->vma
7853 + htab->plt->output_offset
7854 + ent->plt.offset);
7856 break;
7858 /* Relocate against _SDA_BASE_. */
7859 case R_PPC_SDAREL16:
7861 const char *name;
7862 struct elf_link_hash_entry *sda = htab->sdata[0].sym;
7864 if (sec == NULL
7865 || sec->output_section == NULL
7866 || !is_static_defined (sda))
7868 unresolved_reloc = TRUE;
7869 break;
7871 addend -= SYM_VAL (sda);
7873 name = bfd_get_section_name (abfd, sec->output_section);
7874 if (! ((CONST_STRNEQ (name, ".sdata")
7875 && (name[6] == 0 || name[6] == '.'))
7876 || (CONST_STRNEQ (name, ".sbss")
7877 && (name[5] == 0 || name[5] == '.'))))
7879 (*_bfd_error_handler)
7880 (_("%B: the target (%s) of a %s relocation is "
7881 "in the wrong output section (%s)"),
7882 input_bfd,
7883 sym_name,
7884 howto->name,
7885 name);
7888 break;
7890 /* Relocate against _SDA2_BASE_. */
7891 case R_PPC_EMB_SDA2REL:
7893 const char *name;
7894 struct elf_link_hash_entry *sda = htab->sdata[1].sym;
7896 if (sec == NULL
7897 || sec->output_section == NULL
7898 || !is_static_defined (sda))
7900 unresolved_reloc = TRUE;
7901 break;
7903 addend -= SYM_VAL (sda);
7905 name = bfd_get_section_name (abfd, sec->output_section);
7906 if (! (CONST_STRNEQ (name, ".sdata2")
7907 || CONST_STRNEQ (name, ".sbss2")))
7909 (*_bfd_error_handler)
7910 (_("%B: the target (%s) of a %s relocation is "
7911 "in the wrong output section (%s)"),
7912 input_bfd,
7913 sym_name,
7914 howto->name,
7915 name);
7918 break;
7920 /* Relocate against either _SDA_BASE_, _SDA2_BASE_, or 0. */
7921 case R_PPC_EMB_SDA21:
7922 case R_PPC_EMB_RELSDA:
7924 const char *name;
7925 int reg;
7926 struct elf_link_hash_entry *sda = NULL;
7928 if (sec == NULL || sec->output_section == NULL)
7930 unresolved_reloc = TRUE;
7931 break;
7934 name = bfd_get_section_name (abfd, sec->output_section);
7935 if (((CONST_STRNEQ (name, ".sdata")
7936 && (name[6] == 0 || name[6] == '.'))
7937 || (CONST_STRNEQ (name, ".sbss")
7938 && (name[5] == 0 || name[5] == '.'))))
7940 reg = 13;
7941 sda = htab->sdata[0].sym;
7943 else if (CONST_STRNEQ (name, ".sdata2")
7944 || CONST_STRNEQ (name, ".sbss2"))
7946 reg = 2;
7947 sda = htab->sdata[1].sym;
7949 else if (strcmp (name, ".PPC.EMB.sdata0") == 0
7950 || strcmp (name, ".PPC.EMB.sbss0") == 0)
7952 reg = 0;
7954 else
7956 (*_bfd_error_handler)
7957 (_("%B: the target (%s) of a %s relocation is "
7958 "in the wrong output section (%s)"),
7959 input_bfd,
7960 sym_name,
7961 howto->name,
7962 name);
7964 bfd_set_error (bfd_error_bad_value);
7965 ret = FALSE;
7966 continue;
7969 if (sda != NULL)
7971 if (!is_static_defined (sda))
7973 unresolved_reloc = TRUE;
7974 break;
7976 addend -= SYM_VAL (sda);
7979 if (r_type == R_PPC_EMB_SDA21)
7981 bfd_vma insn; /* Fill in register field. */
7983 insn = bfd_get_32 (output_bfd, contents + rel->r_offset);
7984 insn = (insn & ~RA_REGISTER_MASK) | (reg << RA_REGISTER_SHIFT);
7985 bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
7988 break;
7990 /* Relocate against the beginning of the section. */
7991 case R_PPC_SECTOFF:
7992 case R_PPC_SECTOFF_LO:
7993 case R_PPC_SECTOFF_HI:
7994 case R_PPC_SECTOFF_HA:
7995 if (sec == NULL || sec->output_section == NULL)
7997 unresolved_reloc = TRUE;
7998 break;
8000 addend -= sec->output_section->vma;
8001 break;
8003 /* Negative relocations. */
8004 case R_PPC_EMB_NADDR32:
8005 case R_PPC_EMB_NADDR16:
8006 case R_PPC_EMB_NADDR16_LO:
8007 case R_PPC_EMB_NADDR16_HI:
8008 case R_PPC_EMB_NADDR16_HA:
8009 addend -= 2 * relocation;
8010 break;
8012 case R_PPC_COPY:
8013 case R_PPC_GLOB_DAT:
8014 case R_PPC_JMP_SLOT:
8015 case R_PPC_RELATIVE:
8016 case R_PPC_IRELATIVE:
8017 case R_PPC_PLT32:
8018 case R_PPC_PLTREL32:
8019 case R_PPC_PLT16_LO:
8020 case R_PPC_PLT16_HI:
8021 case R_PPC_PLT16_HA:
8022 case R_PPC_ADDR30:
8023 case R_PPC_EMB_RELSEC16:
8024 case R_PPC_EMB_RELST_LO:
8025 case R_PPC_EMB_RELST_HI:
8026 case R_PPC_EMB_RELST_HA:
8027 case R_PPC_EMB_BIT_FLD:
8028 (*_bfd_error_handler)
8029 (_("%B: relocation %s is not yet supported for symbol %s."),
8030 input_bfd,
8031 howto->name,
8032 sym_name);
8034 bfd_set_error (bfd_error_invalid_operation);
8035 ret = FALSE;
8036 continue;
8039 /* Do any further special processing. */
8040 switch (r_type)
8042 default:
8043 break;
8045 case R_PPC_ADDR16_HA:
8046 case R_PPC_REL16_HA:
8047 case R_PPC_SECTOFF_HA:
8048 case R_PPC_TPREL16_HA:
8049 case R_PPC_DTPREL16_HA:
8050 case R_PPC_EMB_NADDR16_HA:
8051 case R_PPC_EMB_RELST_HA:
8052 /* It's just possible that this symbol is a weak symbol
8053 that's not actually defined anywhere. In that case,
8054 'sec' would be NULL, and we should leave the symbol
8055 alone (it will be set to zero elsewhere in the link). */
8056 if (sec == NULL)
8057 break;
8058 /* Fall thru */
8060 case R_PPC_PLT16_HA:
8061 case R_PPC_GOT16_HA:
8062 case R_PPC_GOT_TLSGD16_HA:
8063 case R_PPC_GOT_TLSLD16_HA:
8064 case R_PPC_GOT_TPREL16_HA:
8065 case R_PPC_GOT_DTPREL16_HA:
8066 /* Add 0x10000 if sign bit in 0:15 is set.
8067 Bits 0:15 are not used. */
8068 addend += 0x8000;
8069 break;
8072 #ifdef DEBUG
8073 fprintf (stderr, "\ttype = %s (%d), name = %s, symbol index = %ld, "
8074 "offset = %ld, addend = %ld\n",
8075 howto->name,
8076 (int) r_type,
8077 sym_name,
8078 r_symndx,
8079 (long) rel->r_offset,
8080 (long) addend);
8081 #endif
8083 if (unresolved_reloc
8084 && !((input_section->flags & SEC_DEBUGGING) != 0
8085 && h->def_dynamic))
8087 (*_bfd_error_handler)
8088 (_("%B(%A+0x%lx): unresolvable %s relocation against symbol `%s'"),
8089 input_bfd,
8090 input_section,
8091 (long) rel->r_offset,
8092 howto->name,
8093 sym_name);
8094 ret = FALSE;
8097 r = _bfd_final_link_relocate (howto,
8098 input_bfd,
8099 input_section,
8100 contents,
8101 rel->r_offset,
8102 relocation,
8103 addend);
8105 if (r != bfd_reloc_ok)
8107 if (r == bfd_reloc_overflow)
8109 if (warned)
8110 continue;
8111 if (h != NULL
8112 && h->root.type == bfd_link_hash_undefweak
8113 && howto->pc_relative)
8115 /* Assume this is a call protected by other code that
8116 detect the symbol is undefined. If this is the case,
8117 we can safely ignore the overflow. If not, the
8118 program is hosed anyway, and a little warning isn't
8119 going to help. */
8121 continue;
8124 if (! (*info->callbacks->reloc_overflow) (info,
8125 (h ? &h->root : NULL),
8126 sym_name,
8127 howto->name,
8128 rel->r_addend,
8129 input_bfd,
8130 input_section,
8131 rel->r_offset))
8132 return FALSE;
8134 else
8136 (*_bfd_error_handler)
8137 (_("%B(%A+0x%lx): %s reloc against `%s': error %d"),
8138 input_bfd, input_section,
8139 (long) rel->r_offset, howto->name, sym_name, (int) r);
8140 ret = FALSE;
8145 #ifdef DEBUG
8146 fprintf (stderr, "\n");
8147 #endif
8149 return ret;
8152 /* Finish up dynamic symbol handling. We set the contents of various
8153 dynamic sections here. */
8155 static bfd_boolean
8156 ppc_elf_finish_dynamic_symbol (bfd *output_bfd,
8157 struct bfd_link_info *info,
8158 struct elf_link_hash_entry *h,
8159 Elf_Internal_Sym *sym)
8161 struct ppc_elf_link_hash_table *htab;
8162 struct plt_entry *ent;
8163 bfd_boolean doneone;
8165 #ifdef DEBUG
8166 fprintf (stderr, "ppc_elf_finish_dynamic_symbol called for %s",
8167 h->root.root.string);
8168 #endif
8170 htab = ppc_elf_hash_table (info);
8171 BFD_ASSERT (htab->elf.dynobj != NULL);
8173 doneone = FALSE;
8174 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
8175 if (ent->plt.offset != (bfd_vma) -1)
8177 if (!doneone)
8179 Elf_Internal_Rela rela;
8180 bfd_byte *loc;
8181 bfd_vma reloc_index;
8183 if (htab->plt_type == PLT_NEW
8184 || !htab->elf.dynamic_sections_created
8185 || h->dynindx == -1)
8186 reloc_index = ent->plt.offset / 4;
8187 else
8189 reloc_index = ((ent->plt.offset - htab->plt_initial_entry_size)
8190 / htab->plt_slot_size);
8191 if (reloc_index > PLT_NUM_SINGLE_ENTRIES
8192 && htab->plt_type == PLT_OLD)
8193 reloc_index -= (reloc_index - PLT_NUM_SINGLE_ENTRIES) / 2;
8196 /* This symbol has an entry in the procedure linkage table.
8197 Set it up. */
8198 if (htab->plt_type == PLT_VXWORKS
8199 && htab->elf.dynamic_sections_created
8200 && h->dynindx != -1)
8202 bfd_vma got_offset;
8203 const bfd_vma *plt_entry;
8205 /* The first three entries in .got.plt are reserved. */
8206 got_offset = (reloc_index + 3) * 4;
8208 /* Use the right PLT. */
8209 plt_entry = info->shared ? ppc_elf_vxworks_pic_plt_entry
8210 : ppc_elf_vxworks_plt_entry;
8212 /* Fill in the .plt on VxWorks. */
8213 if (info->shared)
8215 bfd_put_32 (output_bfd,
8216 plt_entry[0] | PPC_HA (got_offset),
8217 htab->plt->contents + ent->plt.offset + 0);
8218 bfd_put_32 (output_bfd,
8219 plt_entry[1] | PPC_LO (got_offset),
8220 htab->plt->contents + ent->plt.offset + 4);
8222 else
8224 bfd_vma got_loc = got_offset + SYM_VAL (htab->elf.hgot);
8226 bfd_put_32 (output_bfd,
8227 plt_entry[0] | PPC_HA (got_loc),
8228 htab->plt->contents + ent->plt.offset + 0);
8229 bfd_put_32 (output_bfd,
8230 plt_entry[1] | PPC_LO (got_loc),
8231 htab->plt->contents + ent->plt.offset + 4);
8234 bfd_put_32 (output_bfd, plt_entry[2],
8235 htab->plt->contents + ent->plt.offset + 8);
8236 bfd_put_32 (output_bfd, plt_entry[3],
8237 htab->plt->contents + ent->plt.offset + 12);
8239 /* This instruction is an immediate load. The value loaded is
8240 the byte offset of the R_PPC_JMP_SLOT relocation from the
8241 start of the .rela.plt section. The value is stored in the
8242 low-order 16 bits of the load instruction. */
8243 /* NOTE: It appears that this is now an index rather than a
8244 prescaled offset. */
8245 bfd_put_32 (output_bfd,
8246 plt_entry[4] | reloc_index,
8247 htab->plt->contents + ent->plt.offset + 16);
8248 /* This instruction is a PC-relative branch whose target is
8249 the start of the PLT section. The address of this branch
8250 instruction is 20 bytes beyond the start of this PLT entry.
8251 The address is encoded in bits 6-29, inclusive. The value
8252 stored is right-shifted by two bits, permitting a 26-bit
8253 offset. */
8254 bfd_put_32 (output_bfd,
8255 (plt_entry[5]
8256 | (-(ent->plt.offset + 20) & 0x03fffffc)),
8257 htab->plt->contents + ent->plt.offset + 20);
8258 bfd_put_32 (output_bfd, plt_entry[6],
8259 htab->plt->contents + ent->plt.offset + 24);
8260 bfd_put_32 (output_bfd, plt_entry[7],
8261 htab->plt->contents + ent->plt.offset + 28);
8263 /* Fill in the GOT entry corresponding to this PLT slot with
8264 the address immediately after the the "bctr" instruction
8265 in this PLT entry. */
8266 bfd_put_32 (output_bfd, (htab->plt->output_section->vma
8267 + htab->plt->output_offset
8268 + ent->plt.offset + 16),
8269 htab->sgotplt->contents + got_offset);
8271 if (!info->shared)
8273 /* Fill in a couple of entries in .rela.plt.unloaded. */
8274 loc = htab->srelplt2->contents
8275 + ((VXWORKS_PLTRESOLVE_RELOCS + reloc_index
8276 * VXWORKS_PLT_NON_JMP_SLOT_RELOCS)
8277 * sizeof (Elf32_External_Rela));
8279 /* Provide the @ha relocation for the first instruction. */
8280 rela.r_offset = (htab->plt->output_section->vma
8281 + htab->plt->output_offset
8282 + ent->plt.offset + 2);
8283 rela.r_info = ELF32_R_INFO (htab->elf.hgot->indx,
8284 R_PPC_ADDR16_HA);
8285 rela.r_addend = got_offset;
8286 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
8287 loc += sizeof (Elf32_External_Rela);
8289 /* Provide the @l relocation for the second instruction. */
8290 rela.r_offset = (htab->plt->output_section->vma
8291 + htab->plt->output_offset
8292 + ent->plt.offset + 6);
8293 rela.r_info = ELF32_R_INFO (htab->elf.hgot->indx,
8294 R_PPC_ADDR16_LO);
8295 rela.r_addend = got_offset;
8296 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
8297 loc += sizeof (Elf32_External_Rela);
8299 /* Provide a relocation for the GOT entry corresponding to this
8300 PLT slot. Point it at the middle of the .plt entry. */
8301 rela.r_offset = (htab->sgotplt->output_section->vma
8302 + htab->sgotplt->output_offset
8303 + got_offset);
8304 rela.r_info = ELF32_R_INFO (htab->elf.hplt->indx,
8305 R_PPC_ADDR32);
8306 rela.r_addend = ent->plt.offset + 16;
8307 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
8310 /* VxWorks uses non-standard semantics for R_PPC_JMP_SLOT.
8311 In particular, the offset for the relocation is not the
8312 address of the PLT entry for this function, as specified
8313 by the ABI. Instead, the offset is set to the address of
8314 the GOT slot for this function. See EABI 4.4.4.1. */
8315 rela.r_offset = (htab->sgotplt->output_section->vma
8316 + htab->sgotplt->output_offset
8317 + got_offset);
8320 else
8322 asection *splt = htab->plt;
8323 if (!htab->elf.dynamic_sections_created
8324 || h->dynindx == -1)
8325 splt = htab->iplt;
8327 rela.r_offset = (splt->output_section->vma
8328 + splt->output_offset
8329 + ent->plt.offset);
8330 if (htab->plt_type == PLT_OLD
8331 || !htab->elf.dynamic_sections_created
8332 || h->dynindx == -1)
8334 /* We don't need to fill in the .plt. The ppc dynamic
8335 linker will fill it in. */
8337 else
8339 bfd_vma val = (htab->glink_pltresolve + ent->plt.offset
8340 + htab->glink->output_section->vma
8341 + htab->glink->output_offset);
8342 bfd_put_32 (output_bfd, val,
8343 splt->contents + ent->plt.offset);
8347 /* Fill in the entry in the .rela.plt section. */
8348 rela.r_addend = 0;
8349 if (!htab->elf.dynamic_sections_created
8350 || h->dynindx == -1)
8352 BFD_ASSERT (h->type == STT_GNU_IFUNC
8353 && h->def_regular
8354 && (h->root.type == bfd_link_hash_defined
8355 || h->root.type == bfd_link_hash_defweak));
8356 rela.r_info = ELF32_R_INFO (0, R_PPC_IRELATIVE);
8357 rela.r_addend = SYM_VAL (h);
8359 else
8360 rela.r_info = ELF32_R_INFO (h->dynindx, R_PPC_JMP_SLOT);
8362 if (!htab->elf.dynamic_sections_created
8363 || h->dynindx == -1)
8364 loc = (htab->reliplt->contents
8365 + (htab->reliplt->reloc_count++
8366 * sizeof (Elf32_External_Rela)));
8367 else
8368 loc = (htab->relplt->contents
8369 + reloc_index * sizeof (Elf32_External_Rela));
8370 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
8372 if (!h->def_regular)
8374 /* Mark the symbol as undefined, rather than as
8375 defined in the .plt section. Leave the value if
8376 there were any relocations where pointer equality
8377 matters (this is a clue for the dynamic linker, to
8378 make function pointer comparisons work between an
8379 application and shared library), otherwise set it
8380 to zero. */
8381 sym->st_shndx = SHN_UNDEF;
8382 if (!h->pointer_equality_needed)
8383 sym->st_value = 0;
8384 else if (!h->ref_regular_nonweak)
8386 /* This breaks function pointer comparisons, but
8387 that is better than breaking tests for a NULL
8388 function pointer. */
8389 sym->st_value = 0;
8392 else if (h->type == STT_GNU_IFUNC
8393 && !info->shared)
8395 /* Set the value of ifunc symbols in a non-pie
8396 executable to the glink entry. This is to avoid
8397 text relocations. We can't do this for ifunc in
8398 allocate_dynrelocs, as we do for normal dynamic
8399 function symbols with plt entries, because we need
8400 to keep the original value around for the ifunc
8401 relocation. */
8402 sym->st_shndx = (_bfd_elf_section_from_bfd_section
8403 (output_bfd, htab->glink->output_section));
8404 sym->st_value = (ent->glink_offset +
8405 htab->glink->output_offset
8406 + htab->glink->output_section->vma);
8408 doneone = TRUE;
8411 if (htab->plt_type == PLT_NEW
8412 || !htab->elf.dynamic_sections_created
8413 || h->dynindx == -1)
8415 unsigned char *p;
8416 asection *splt = htab->plt;
8417 if (!htab->elf.dynamic_sections_created
8418 || h->dynindx == -1)
8419 splt = htab->iplt;
8421 p = (unsigned char *) htab->glink->contents + ent->glink_offset;
8423 if (h == htab->tls_get_addr && !htab->no_tls_get_addr_opt)
8425 bfd_put_32 (output_bfd, LWZ_11_3, p);
8426 p += 4;
8427 bfd_put_32 (output_bfd, LWZ_12_3 + 4, p);
8428 p += 4;
8429 bfd_put_32 (output_bfd, MR_0_3, p);
8430 p += 4;
8431 bfd_put_32 (output_bfd, CMPWI_11_0, p);
8432 p += 4;
8433 bfd_put_32 (output_bfd, ADD_3_12_2, p);
8434 p += 4;
8435 bfd_put_32 (output_bfd, BEQLR, p);
8436 p += 4;
8437 bfd_put_32 (output_bfd, MR_3_0, p);
8438 p += 4;
8439 bfd_put_32 (output_bfd, NOP, p);
8440 p += 4;
8443 write_glink_stub (ent, splt, p, info);
8445 if (!info->shared)
8446 /* We only need one non-PIC glink stub. */
8447 break;
8449 else
8450 break;
8453 if (h->needs_copy)
8455 asection *s;
8456 Elf_Internal_Rela rela;
8457 bfd_byte *loc;
8459 /* This symbols needs a copy reloc. Set it up. */
8461 #ifdef DEBUG
8462 fprintf (stderr, ", copy");
8463 #endif
8465 BFD_ASSERT (h->dynindx != -1);
8467 if (ppc_elf_hash_entry (h)->has_sda_refs)
8468 s = htab->relsbss;
8469 else
8470 s = htab->relbss;
8471 BFD_ASSERT (s != NULL);
8473 rela.r_offset = SYM_VAL (h);
8474 rela.r_info = ELF32_R_INFO (h->dynindx, R_PPC_COPY);
8475 rela.r_addend = 0;
8476 loc = s->contents + s->reloc_count++ * sizeof (Elf32_External_Rela);
8477 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
8480 #ifdef DEBUG
8481 fprintf (stderr, "\n");
8482 #endif
8484 /* Mark some specially defined symbols as absolute. */
8485 if (strcmp (h->root.root.string, "_DYNAMIC") == 0
8486 || (!htab->is_vxworks
8487 && (h == htab->elf.hgot
8488 || strcmp (h->root.root.string,
8489 "_PROCEDURE_LINKAGE_TABLE_") == 0)))
8490 sym->st_shndx = SHN_ABS;
8492 return TRUE;
8495 static enum elf_reloc_type_class
8496 ppc_elf_reloc_type_class (const Elf_Internal_Rela *rela)
8498 switch (ELF32_R_TYPE (rela->r_info))
8500 case R_PPC_RELATIVE:
8501 return reloc_class_relative;
8502 case R_PPC_REL24:
8503 case R_PPC_ADDR24:
8504 case R_PPC_JMP_SLOT:
8505 return reloc_class_plt;
8506 case R_PPC_COPY:
8507 return reloc_class_copy;
8508 default:
8509 return reloc_class_normal;
8513 /* Finish up the dynamic sections. */
8515 static bfd_boolean
8516 ppc_elf_finish_dynamic_sections (bfd *output_bfd,
8517 struct bfd_link_info *info)
8519 asection *sdyn;
8520 asection *splt;
8521 struct ppc_elf_link_hash_table *htab;
8522 bfd_vma got;
8523 bfd *dynobj;
8524 bfd_boolean ret = TRUE;
8526 #ifdef DEBUG
8527 fprintf (stderr, "ppc_elf_finish_dynamic_sections called\n");
8528 #endif
8530 htab = ppc_elf_hash_table (info);
8531 dynobj = elf_hash_table (info)->dynobj;
8532 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
8533 if (htab->is_vxworks)
8534 splt = bfd_get_section_by_name (dynobj, ".plt");
8535 else
8536 splt = NULL;
8538 got = 0;
8539 if (htab->elf.hgot != NULL)
8540 got = SYM_VAL (htab->elf.hgot);
8542 if (htab->elf.dynamic_sections_created)
8544 Elf32_External_Dyn *dyncon, *dynconend;
8546 BFD_ASSERT (htab->plt != NULL && sdyn != NULL);
8548 dyncon = (Elf32_External_Dyn *) sdyn->contents;
8549 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
8550 for (; dyncon < dynconend; dyncon++)
8552 Elf_Internal_Dyn dyn;
8553 asection *s;
8555 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
8557 switch (dyn.d_tag)
8559 case DT_PLTGOT:
8560 if (htab->is_vxworks)
8561 s = htab->sgotplt;
8562 else
8563 s = htab->plt;
8564 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
8565 break;
8567 case DT_PLTRELSZ:
8568 dyn.d_un.d_val = htab->relplt->size;
8569 break;
8571 case DT_JMPREL:
8572 s = htab->relplt;
8573 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
8574 break;
8576 case DT_PPC_GOT:
8577 dyn.d_un.d_ptr = got;
8578 break;
8580 case DT_RELASZ:
8581 if (htab->is_vxworks)
8583 if (htab->relplt)
8584 dyn.d_un.d_ptr -= htab->relplt->size;
8585 break;
8587 continue;
8589 default:
8590 if (htab->is_vxworks
8591 && elf_vxworks_finish_dynamic_entry (output_bfd, &dyn))
8592 break;
8593 continue;
8596 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
8600 if (htab->got != NULL)
8602 if (htab->elf.hgot->root.u.def.section == htab->got
8603 || htab->elf.hgot->root.u.def.section == htab->sgotplt)
8605 unsigned char *p = htab->elf.hgot->root.u.def.section->contents;
8607 p += htab->elf.hgot->root.u.def.value;
8608 if (htab->plt_type == PLT_OLD)
8610 /* Add a blrl instruction at _GLOBAL_OFFSET_TABLE_-4
8611 so that a function can easily find the address of
8612 _GLOBAL_OFFSET_TABLE_. */
8613 BFD_ASSERT (htab->elf.hgot->root.u.def.value - 4
8614 < htab->elf.hgot->root.u.def.section->size);
8615 bfd_put_32 (output_bfd, 0x4e800021, p - 4);
8618 if (sdyn != NULL)
8620 bfd_vma val = sdyn->output_section->vma + sdyn->output_offset;
8621 BFD_ASSERT (htab->elf.hgot->root.u.def.value
8622 < htab->elf.hgot->root.u.def.section->size);
8623 bfd_put_32 (output_bfd, val, p);
8626 else
8628 (*_bfd_error_handler) (_("%s not defined in linker created %s"),
8629 htab->elf.hgot->root.root.string,
8630 (htab->sgotplt != NULL
8631 ? htab->sgotplt->name : htab->got->name));
8632 bfd_set_error (bfd_error_bad_value);
8633 ret = FALSE;
8636 elf_section_data (htab->got->output_section)->this_hdr.sh_entsize = 4;
8639 /* Fill in the first entry in the VxWorks procedure linkage table. */
8640 if (splt && splt->size > 0)
8642 /* Use the right PLT. */
8643 static const bfd_vma *plt_entry = NULL;
8644 plt_entry = info->shared ?
8645 ppc_elf_vxworks_pic_plt0_entry : ppc_elf_vxworks_plt0_entry;
8647 if (!info->shared)
8649 bfd_vma got_value = SYM_VAL (htab->elf.hgot);
8651 bfd_put_32 (output_bfd, plt_entry[0] | PPC_HA (got_value),
8652 splt->contents + 0);
8653 bfd_put_32 (output_bfd, plt_entry[1] | PPC_LO (got_value),
8654 splt->contents + 4);
8656 else
8658 bfd_put_32 (output_bfd, plt_entry[0], splt->contents + 0);
8659 bfd_put_32 (output_bfd, plt_entry[1], splt->contents + 4);
8661 bfd_put_32 (output_bfd, plt_entry[2], splt->contents + 8);
8662 bfd_put_32 (output_bfd, plt_entry[3], splt->contents + 12);
8663 bfd_put_32 (output_bfd, plt_entry[4], splt->contents + 16);
8664 bfd_put_32 (output_bfd, plt_entry[5], splt->contents + 20);
8665 bfd_put_32 (output_bfd, plt_entry[6], splt->contents + 24);
8666 bfd_put_32 (output_bfd, plt_entry[7], splt->contents + 28);
8668 if (! info->shared)
8670 Elf_Internal_Rela rela;
8671 bfd_byte *loc;
8673 loc = htab->srelplt2->contents;
8675 /* Output the @ha relocation for the first instruction. */
8676 rela.r_offset = (htab->plt->output_section->vma
8677 + htab->plt->output_offset
8678 + 2);
8679 rela.r_info = ELF32_R_INFO (htab->elf.hgot->indx, R_PPC_ADDR16_HA);
8680 rela.r_addend = 0;
8681 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
8682 loc += sizeof (Elf32_External_Rela);
8684 /* Output the @l relocation for the second instruction. */
8685 rela.r_offset = (htab->plt->output_section->vma
8686 + htab->plt->output_offset
8687 + 6);
8688 rela.r_info = ELF32_R_INFO (htab->elf.hgot->indx, R_PPC_ADDR16_LO);
8689 rela.r_addend = 0;
8690 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
8691 loc += sizeof (Elf32_External_Rela);
8693 /* Fix up the remaining relocations. They may have the wrong
8694 symbol index for _G_O_T_ or _P_L_T_ depending on the order
8695 in which symbols were output. */
8696 while (loc < htab->srelplt2->contents + htab->srelplt2->size)
8698 Elf_Internal_Rela rel;
8700 bfd_elf32_swap_reloc_in (output_bfd, loc, &rel);
8701 rel.r_info = ELF32_R_INFO (htab->elf.hgot->indx, R_PPC_ADDR16_HA);
8702 bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
8703 loc += sizeof (Elf32_External_Rela);
8705 bfd_elf32_swap_reloc_in (output_bfd, loc, &rel);
8706 rel.r_info = ELF32_R_INFO (htab->elf.hgot->indx, R_PPC_ADDR16_LO);
8707 bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
8708 loc += sizeof (Elf32_External_Rela);
8710 bfd_elf32_swap_reloc_in (output_bfd, loc, &rel);
8711 rel.r_info = ELF32_R_INFO (htab->elf.hplt->indx, R_PPC_ADDR32);
8712 bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
8713 loc += sizeof (Elf32_External_Rela);
8718 if (htab->glink != NULL
8719 && htab->glink->contents != NULL
8720 && htab->elf.dynamic_sections_created)
8722 unsigned char *p;
8723 unsigned char *endp;
8724 bfd_vma res0;
8725 unsigned int i;
8728 * PIC glink code is the following:
8730 * # ith PLT code stub.
8731 * addis 11,30,(plt+(i-1)*4-got)@ha
8732 * lwz 11,(plt+(i-1)*4-got)@l(11)
8733 * mtctr 11
8734 * bctr
8736 * # A table of branches, one for each plt entry.
8737 * # The idea is that the plt call stub loads ctr and r11 with these
8738 * # addresses, so (r11 - res_0) gives the plt index * 4.
8739 * res_0: b PLTresolve
8740 * res_1: b PLTresolve
8742 * # Some number of entries towards the end can be nops
8743 * res_n_m3: nop
8744 * res_n_m2: nop
8745 * res_n_m1:
8747 * PLTresolve:
8748 * addis 11,11,(1f-res_0)@ha
8749 * mflr 0
8750 * bcl 20,31,1f
8751 * 1: addi 11,11,(1b-res_0)@l
8752 * mflr 12
8753 * mtlr 0
8754 * sub 11,11,12 # r11 = index * 4
8755 * addis 12,12,(got+4-1b)@ha
8756 * lwz 0,(got+4-1b)@l(12) # got[1] address of dl_runtime_resolve
8757 * lwz 12,(got+8-1b)@l(12) # got[2] contains the map address
8758 * mtctr 0
8759 * add 0,11,11
8760 * add 11,0,11 # r11 = index * 12 = reloc offset.
8761 * bctr
8763 static const unsigned int pic_plt_resolve[] =
8765 ADDIS_11_11,
8766 MFLR_0,
8767 BCL_20_31,
8768 ADDI_11_11,
8769 MFLR_12,
8770 MTLR_0,
8771 SUB_11_11_12,
8772 ADDIS_12_12,
8773 LWZ_0_12,
8774 LWZ_12_12,
8775 MTCTR_0,
8776 ADD_0_11_11,
8777 ADD_11_0_11,
8778 BCTR,
8779 NOP,
8784 * Non-PIC glink code is a little simpler.
8786 * # ith PLT code stub.
8787 * lis 11,(plt+(i-1)*4)@ha
8788 * lwz 11,(plt+(i-1)*4)@l(11)
8789 * mtctr 11
8790 * bctr
8792 * The branch table is the same, then comes
8794 * PLTresolve:
8795 * lis 12,(got+4)@ha
8796 * addis 11,11,(-res_0)@ha
8797 * lwz 0,(got+4)@l(12) # got[1] address of dl_runtime_resolve
8798 * addi 11,11,(-res_0)@l # r11 = index * 4
8799 * mtctr 0
8800 * add 0,11,11
8801 * lwz 12,(got+8)@l(12) # got[2] contains the map address
8802 * add 11,0,11 # r11 = index * 12 = reloc offset.
8803 * bctr
8805 static const unsigned int plt_resolve[] =
8807 LIS_12,
8808 ADDIS_11_11,
8809 LWZ_0_12,
8810 ADDI_11_11,
8811 MTCTR_0,
8812 ADD_0_11_11,
8813 LWZ_12_12,
8814 ADD_11_0_11,
8815 BCTR,
8816 NOP,
8817 NOP,
8818 NOP,
8819 NOP,
8820 NOP,
8821 NOP,
8825 if (ARRAY_SIZE (pic_plt_resolve) != GLINK_PLTRESOLVE / 4)
8826 abort ();
8827 if (ARRAY_SIZE (plt_resolve) != GLINK_PLTRESOLVE / 4)
8828 abort ();
8830 /* Build the branch table, one for each plt entry (less one),
8831 and perhaps some padding. */
8832 p = htab->glink->contents;
8833 p += htab->glink_pltresolve;
8834 endp = htab->glink->contents;
8835 endp += htab->glink->size - GLINK_PLTRESOLVE;
8836 while (p < endp - 8 * 4)
8838 bfd_put_32 (output_bfd, B + endp - p, p);
8839 p += 4;
8841 while (p < endp)
8843 bfd_put_32 (output_bfd, NOP, p);
8844 p += 4;
8847 res0 = (htab->glink_pltresolve
8848 + htab->glink->output_section->vma
8849 + htab->glink->output_offset);
8851 /* Last comes the PLTresolve stub. */
8852 if (info->shared)
8854 bfd_vma bcl;
8856 for (i = 0; i < ARRAY_SIZE (pic_plt_resolve); i++)
8858 bfd_put_32 (output_bfd, pic_plt_resolve[i], p);
8859 p += 4;
8861 p -= 4 * ARRAY_SIZE (pic_plt_resolve);
8863 bcl = (htab->glink->size - GLINK_PLTRESOLVE + 3*4
8864 + htab->glink->output_section->vma
8865 + htab->glink->output_offset);
8867 bfd_put_32 (output_bfd,
8868 ADDIS_11_11 + PPC_HA (bcl - res0), p + 0*4);
8869 bfd_put_32 (output_bfd,
8870 ADDI_11_11 + PPC_LO (bcl - res0), p + 3*4);
8871 bfd_put_32 (output_bfd,
8872 ADDIS_12_12 + PPC_HA (got + 4 - bcl), p + 7*4);
8873 if (PPC_HA (got + 4 - bcl) == PPC_HA (got + 8 - bcl))
8875 bfd_put_32 (output_bfd,
8876 LWZ_0_12 + PPC_LO (got + 4 - bcl), p + 8*4);
8877 bfd_put_32 (output_bfd,
8878 LWZ_12_12 + PPC_LO (got + 8 - bcl), p + 9*4);
8880 else
8882 bfd_put_32 (output_bfd,
8883 LWZU_0_12 + PPC_LO (got + 4 - bcl), p + 8*4);
8884 bfd_put_32 (output_bfd,
8885 LWZ_12_12 + 4, p + 9*4);
8888 else
8890 for (i = 0; i < ARRAY_SIZE (plt_resolve); i++)
8892 bfd_put_32 (output_bfd, plt_resolve[i], p);
8893 p += 4;
8895 p -= 4 * ARRAY_SIZE (plt_resolve);
8897 bfd_put_32 (output_bfd,
8898 LIS_12 + PPC_HA (got + 4), p + 0*4);
8899 bfd_put_32 (output_bfd,
8900 ADDIS_11_11 + PPC_HA (-res0), p + 1*4);
8901 bfd_put_32 (output_bfd,
8902 ADDI_11_11 + PPC_LO (-res0), p + 3*4);
8903 if (PPC_HA (got + 4) == PPC_HA (got + 8))
8905 bfd_put_32 (output_bfd,
8906 LWZ_0_12 + PPC_LO (got + 4), p + 2*4);
8907 bfd_put_32 (output_bfd,
8908 LWZ_12_12 + PPC_LO (got + 8), p + 6*4);
8910 else
8912 bfd_put_32 (output_bfd,
8913 LWZU_0_12 + PPC_LO (got + 4), p + 2*4);
8914 bfd_put_32 (output_bfd,
8915 LWZ_12_12 + 4, p + 6*4);
8920 return ret;
8923 #define TARGET_LITTLE_SYM bfd_elf32_powerpcle_vec
8924 #define TARGET_LITTLE_NAME "elf32-powerpcle"
8925 #define TARGET_BIG_SYM bfd_elf32_powerpc_vec
8926 #define TARGET_BIG_NAME "elf32-powerpc"
8927 #define ELF_ARCH bfd_arch_powerpc
8928 #define ELF_MACHINE_CODE EM_PPC
8929 #ifdef __QNXTARGET__
8930 #define ELF_MAXPAGESIZE 0x1000
8931 #else
8932 #define ELF_MAXPAGESIZE 0x10000
8933 #endif
8934 #define ELF_MINPAGESIZE 0x1000
8935 #define ELF_COMMONPAGESIZE 0x1000
8936 #define elf_info_to_howto ppc_elf_info_to_howto
8938 #ifdef EM_CYGNUS_POWERPC
8939 #define ELF_MACHINE_ALT1 EM_CYGNUS_POWERPC
8940 #endif
8942 #ifdef EM_PPC_OLD
8943 #define ELF_MACHINE_ALT2 EM_PPC_OLD
8944 #endif
8946 #define elf_backend_plt_not_loaded 1
8947 #define elf_backend_can_gc_sections 1
8948 #define elf_backend_can_refcount 1
8949 #define elf_backend_rela_normal 1
8951 #define bfd_elf32_mkobject ppc_elf_mkobject
8952 #define bfd_elf32_bfd_merge_private_bfd_data ppc_elf_merge_private_bfd_data
8953 #define bfd_elf32_bfd_relax_section ppc_elf_relax_section
8954 #define bfd_elf32_bfd_reloc_type_lookup ppc_elf_reloc_type_lookup
8955 #define bfd_elf32_bfd_reloc_name_lookup ppc_elf_reloc_name_lookup
8956 #define bfd_elf32_bfd_set_private_flags ppc_elf_set_private_flags
8957 #define bfd_elf32_bfd_link_hash_table_create ppc_elf_link_hash_table_create
8958 #define bfd_elf32_get_synthetic_symtab ppc_elf_get_synthetic_symtab
8960 #define elf_backend_object_p ppc_elf_object_p
8961 #define elf_backend_gc_mark_hook ppc_elf_gc_mark_hook
8962 #define elf_backend_gc_sweep_hook ppc_elf_gc_sweep_hook
8963 #define elf_backend_section_from_shdr ppc_elf_section_from_shdr
8964 #define elf_backend_relocate_section ppc_elf_relocate_section
8965 #define elf_backend_create_dynamic_sections ppc_elf_create_dynamic_sections
8966 #define elf_backend_check_relocs ppc_elf_check_relocs
8967 #define elf_backend_copy_indirect_symbol ppc_elf_copy_indirect_symbol
8968 #define elf_backend_adjust_dynamic_symbol ppc_elf_adjust_dynamic_symbol
8969 #define elf_backend_add_symbol_hook ppc_elf_add_symbol_hook
8970 #define elf_backend_size_dynamic_sections ppc_elf_size_dynamic_sections
8971 #define elf_backend_hash_symbol ppc_elf_hash_symbol
8972 #define elf_backend_finish_dynamic_symbol ppc_elf_finish_dynamic_symbol
8973 #define elf_backend_finish_dynamic_sections ppc_elf_finish_dynamic_sections
8974 #define elf_backend_fake_sections ppc_elf_fake_sections
8975 #define elf_backend_additional_program_headers ppc_elf_additional_program_headers
8976 #define elf_backend_grok_prstatus ppc_elf_grok_prstatus
8977 #define elf_backend_grok_psinfo ppc_elf_grok_psinfo
8978 #define elf_backend_write_core_note ppc_elf_write_core_note
8979 #define elf_backend_reloc_type_class ppc_elf_reloc_type_class
8980 #define elf_backend_begin_write_processing ppc_elf_begin_write_processing
8981 #define elf_backend_final_write_processing ppc_elf_final_write_processing
8982 #define elf_backend_write_section ppc_elf_write_section
8983 #define elf_backend_get_sec_type_attr ppc_elf_get_sec_type_attr
8984 #define elf_backend_plt_sym_val ppc_elf_plt_sym_val
8985 #define elf_backend_action_discarded ppc_elf_action_discarded
8986 #define elf_backend_init_index_section _bfd_elf_init_1_index_section
8987 #define elf_backend_post_process_headers _bfd_elf_set_osabi
8989 #include "elf32-target.h"
8991 /* VxWorks Target */
8993 #undef TARGET_LITTLE_SYM
8994 #undef TARGET_LITTLE_NAME
8996 #undef TARGET_BIG_SYM
8997 #define TARGET_BIG_SYM bfd_elf32_powerpc_vxworks_vec
8998 #undef TARGET_BIG_NAME
8999 #define TARGET_BIG_NAME "elf32-powerpc-vxworks"
9001 /* VxWorks uses the elf default section flags for .plt. */
9002 static const struct bfd_elf_special_section *
9003 ppc_elf_vxworks_get_sec_type_attr (bfd *abfd ATTRIBUTE_UNUSED, asection *sec)
9005 if (sec->name == NULL)
9006 return NULL;
9008 if (strcmp (sec->name, ".plt") == 0)
9009 return _bfd_elf_get_sec_type_attr (abfd, sec);
9011 return ppc_elf_get_sec_type_attr (abfd, sec);
9014 /* Like ppc_elf_link_hash_table_create, but overrides
9015 appropriately for VxWorks. */
9016 static struct bfd_link_hash_table *
9017 ppc_elf_vxworks_link_hash_table_create (bfd *abfd)
9019 struct bfd_link_hash_table *ret;
9021 ret = ppc_elf_link_hash_table_create (abfd);
9022 if (ret)
9024 struct ppc_elf_link_hash_table *htab
9025 = (struct ppc_elf_link_hash_table *)ret;
9026 htab->is_vxworks = 1;
9027 htab->plt_type = PLT_VXWORKS;
9028 htab->plt_entry_size = VXWORKS_PLT_ENTRY_SIZE;
9029 htab->plt_slot_size = VXWORKS_PLT_ENTRY_SIZE;
9030 htab->plt_initial_entry_size = VXWORKS_PLT_INITIAL_ENTRY_SIZE;
9032 return ret;
9035 /* Tweak magic VxWorks symbols as they are loaded. */
9036 static bfd_boolean
9037 ppc_elf_vxworks_add_symbol_hook (bfd *abfd,
9038 struct bfd_link_info *info,
9039 Elf_Internal_Sym *sym,
9040 const char **namep ATTRIBUTE_UNUSED,
9041 flagword *flagsp ATTRIBUTE_UNUSED,
9042 asection **secp,
9043 bfd_vma *valp)
9045 if (!elf_vxworks_add_symbol_hook(abfd, info, sym,namep, flagsp, secp,
9046 valp))
9047 return FALSE;
9049 return ppc_elf_add_symbol_hook(abfd, info, sym,namep, flagsp, secp, valp);
9052 static void
9053 ppc_elf_vxworks_final_write_processing (bfd *abfd, bfd_boolean linker)
9055 ppc_elf_final_write_processing(abfd, linker);
9056 elf_vxworks_final_write_processing(abfd, linker);
9059 /* On VxWorks, we emit relocations against _PROCEDURE_LINKAGE_TABLE_, so
9060 define it. */
9061 #undef elf_backend_want_plt_sym
9062 #define elf_backend_want_plt_sym 1
9063 #undef elf_backend_want_got_plt
9064 #define elf_backend_want_got_plt 1
9065 #undef elf_backend_got_symbol_offset
9066 #define elf_backend_got_symbol_offset 0
9067 #undef elf_backend_plt_not_loaded
9068 #define elf_backend_plt_not_loaded 0
9069 #undef elf_backend_plt_readonly
9070 #define elf_backend_plt_readonly 1
9071 #undef elf_backend_got_header_size
9072 #define elf_backend_got_header_size 12
9074 #undef bfd_elf32_get_synthetic_symtab
9076 #undef bfd_elf32_bfd_link_hash_table_create
9077 #define bfd_elf32_bfd_link_hash_table_create \
9078 ppc_elf_vxworks_link_hash_table_create
9079 #undef elf_backend_add_symbol_hook
9080 #define elf_backend_add_symbol_hook \
9081 ppc_elf_vxworks_add_symbol_hook
9082 #undef elf_backend_link_output_symbol_hook
9083 #define elf_backend_link_output_symbol_hook \
9084 elf_vxworks_link_output_symbol_hook
9085 #undef elf_backend_final_write_processing
9086 #define elf_backend_final_write_processing \
9087 ppc_elf_vxworks_final_write_processing
9088 #undef elf_backend_get_sec_type_attr
9089 #define elf_backend_get_sec_type_attr \
9090 ppc_elf_vxworks_get_sec_type_attr
9091 #undef elf_backend_emit_relocs
9092 #define elf_backend_emit_relocs \
9093 elf_vxworks_emit_relocs
9095 #undef elf32_bed
9096 #define elf32_bed ppc_elf_vxworks_bed
9097 #undef elf_backend_post_process_headers
9099 #include "elf32-target.h"