2011-03-25 Tristan Gingold <gingold@adacore.com>
[binutils.git] / bfd / elf32-ppc.c
blob9e097ad624dd4b4691b3a6edcb81826b74a5b8bf
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 FALSE, /* 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_lwpid = 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_SORT_ENTRIES) != 0)
1998 shdr->sh_type = SHT_ORDERED;
2000 return TRUE;
2003 /* If we have .sbss2 or .PPC.EMB.sbss0 output sections, we
2004 need to bump up the number of section headers. */
2006 static int
2007 ppc_elf_additional_program_headers (bfd *abfd,
2008 struct bfd_link_info *info ATTRIBUTE_UNUSED)
2010 asection *s;
2011 int ret = 0;
2013 s = bfd_get_section_by_name (abfd, ".sbss2");
2014 if (s != NULL && (s->flags & SEC_ALLOC) != 0)
2015 ++ret;
2017 s = bfd_get_section_by_name (abfd, ".PPC.EMB.sbss0");
2018 if (s != NULL && (s->flags & SEC_ALLOC) != 0)
2019 ++ret;
2021 return ret;
2024 /* Add extra PPC sections -- Note, for now, make .sbss2 and
2025 .PPC.EMB.sbss0 a normal section, and not a bss section so
2026 that the linker doesn't crater when trying to make more than
2027 2 sections. */
2029 static const struct bfd_elf_special_section ppc_elf_special_sections[] =
2031 { STRING_COMMA_LEN (".plt"), 0, SHT_NOBITS, SHF_ALLOC + SHF_EXECINSTR },
2032 { STRING_COMMA_LEN (".sbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
2033 { STRING_COMMA_LEN (".sbss2"), -2, SHT_PROGBITS, SHF_ALLOC },
2034 { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2035 { STRING_COMMA_LEN (".sdata2"), -2, SHT_PROGBITS, SHF_ALLOC },
2036 { STRING_COMMA_LEN (".tags"), 0, SHT_ORDERED, SHF_ALLOC },
2037 { STRING_COMMA_LEN (".PPC.EMB.apuinfo"), 0, SHT_NOTE, 0 },
2038 { STRING_COMMA_LEN (".PPC.EMB.sbss0"), 0, SHT_PROGBITS, SHF_ALLOC },
2039 { STRING_COMMA_LEN (".PPC.EMB.sdata0"), 0, SHT_PROGBITS, SHF_ALLOC },
2040 { NULL, 0, 0, 0, 0 }
2043 /* This is what we want for new plt/got. */
2044 static struct bfd_elf_special_section ppc_alt_plt =
2045 { STRING_COMMA_LEN (".plt"), 0, SHT_PROGBITS, SHF_ALLOC };
2047 static const struct bfd_elf_special_section *
2048 ppc_elf_get_sec_type_attr (bfd *abfd ATTRIBUTE_UNUSED, asection *sec)
2050 const struct bfd_elf_special_section *ssect;
2052 /* See if this is one of the special sections. */
2053 if (sec->name == NULL)
2054 return NULL;
2056 ssect = _bfd_elf_get_special_section (sec->name, ppc_elf_special_sections,
2057 sec->use_rela_p);
2058 if (ssect != NULL)
2060 if (ssect == ppc_elf_special_sections && (sec->flags & SEC_LOAD) != 0)
2061 ssect = &ppc_alt_plt;
2062 return ssect;
2065 return _bfd_elf_get_sec_type_attr (abfd, sec);
2068 /* Very simple linked list structure for recording apuinfo values. */
2069 typedef struct apuinfo_list
2071 struct apuinfo_list *next;
2072 unsigned long value;
2074 apuinfo_list;
2076 static apuinfo_list *head;
2077 static bfd_boolean apuinfo_set;
2079 static void
2080 apuinfo_list_init (void)
2082 head = NULL;
2083 apuinfo_set = FALSE;
2086 static void
2087 apuinfo_list_add (unsigned long value)
2089 apuinfo_list *entry = head;
2091 while (entry != NULL)
2093 if (entry->value == value)
2094 return;
2095 entry = entry->next;
2098 entry = bfd_malloc (sizeof (* entry));
2099 if (entry == NULL)
2100 return;
2102 entry->value = value;
2103 entry->next = head;
2104 head = entry;
2107 static unsigned
2108 apuinfo_list_length (void)
2110 apuinfo_list *entry;
2111 unsigned long count;
2113 for (entry = head, count = 0;
2114 entry;
2115 entry = entry->next)
2116 ++ count;
2118 return count;
2121 static inline unsigned long
2122 apuinfo_list_element (unsigned long number)
2124 apuinfo_list * entry;
2126 for (entry = head;
2127 entry && number --;
2128 entry = entry->next)
2131 return entry ? entry->value : 0;
2134 static void
2135 apuinfo_list_finish (void)
2137 apuinfo_list *entry;
2139 for (entry = head; entry;)
2141 apuinfo_list *next = entry->next;
2142 free (entry);
2143 entry = next;
2146 head = NULL;
2149 #define APUINFO_SECTION_NAME ".PPC.EMB.apuinfo"
2150 #define APUINFO_LABEL "APUinfo"
2152 /* Scan the input BFDs and create a linked list of
2153 the APUinfo values that will need to be emitted. */
2155 static void
2156 ppc_elf_begin_write_processing (bfd *abfd, struct bfd_link_info *link_info)
2158 bfd *ibfd;
2159 asection *asec;
2160 char *buffer = NULL;
2161 bfd_size_type largest_input_size = 0;
2162 unsigned i;
2163 unsigned long length;
2164 const char *error_message = NULL;
2166 if (link_info == NULL)
2167 return;
2169 apuinfo_list_init ();
2171 /* Read in the input sections contents. */
2172 for (ibfd = link_info->input_bfds; ibfd; ibfd = ibfd->link_next)
2174 unsigned long datum;
2176 asec = bfd_get_section_by_name (ibfd, APUINFO_SECTION_NAME);
2177 if (asec == NULL)
2178 continue;
2180 error_message = _("corrupt %s section in %B");
2181 length = asec->size;
2182 if (length < 20)
2183 goto fail;
2185 apuinfo_set = TRUE;
2186 if (largest_input_size < asec->size)
2188 if (buffer)
2189 free (buffer);
2190 largest_input_size = asec->size;
2191 buffer = bfd_malloc (largest_input_size);
2192 if (!buffer)
2193 return;
2196 if (bfd_seek (ibfd, asec->filepos, SEEK_SET) != 0
2197 || (bfd_bread (buffer, length, ibfd) != length))
2199 error_message = _("unable to read in %s section from %B");
2200 goto fail;
2203 /* Verify the contents of the header. Note - we have to
2204 extract the values this way in order to allow for a
2205 host whose endian-ness is different from the target. */
2206 datum = bfd_get_32 (ibfd, buffer);
2207 if (datum != sizeof APUINFO_LABEL)
2208 goto fail;
2210 datum = bfd_get_32 (ibfd, buffer + 8);
2211 if (datum != 0x2)
2212 goto fail;
2214 if (strcmp (buffer + 12, APUINFO_LABEL) != 0)
2215 goto fail;
2217 /* Get the number of bytes used for apuinfo entries. */
2218 datum = bfd_get_32 (ibfd, buffer + 4);
2219 if (datum + 20 != length)
2220 goto fail;
2222 /* Scan the apuinfo section, building a list of apuinfo numbers. */
2223 for (i = 0; i < datum; i += 4)
2224 apuinfo_list_add (bfd_get_32 (ibfd, buffer + 20 + i));
2227 error_message = NULL;
2229 if (apuinfo_set)
2231 /* Compute the size of the output section. */
2232 unsigned num_entries = apuinfo_list_length ();
2234 /* Set the output section size, if it exists. */
2235 asec = bfd_get_section_by_name (abfd, APUINFO_SECTION_NAME);
2237 if (asec && ! bfd_set_section_size (abfd, asec, 20 + num_entries * 4))
2239 ibfd = abfd;
2240 error_message = _("warning: unable to set size of %s section in %B");
2244 fail:
2245 if (buffer)
2246 free (buffer);
2248 if (error_message)
2249 (*_bfd_error_handler) (error_message, ibfd, APUINFO_SECTION_NAME);
2252 /* Prevent the output section from accumulating the input sections'
2253 contents. We have already stored this in our linked list structure. */
2255 static bfd_boolean
2256 ppc_elf_write_section (bfd *abfd ATTRIBUTE_UNUSED,
2257 struct bfd_link_info *link_info ATTRIBUTE_UNUSED,
2258 asection *asec,
2259 bfd_byte *contents ATTRIBUTE_UNUSED)
2261 return apuinfo_set && strcmp (asec->name, APUINFO_SECTION_NAME) == 0;
2264 /* Finally we can generate the output section. */
2266 static void
2267 ppc_elf_final_write_processing (bfd *abfd, bfd_boolean linker ATTRIBUTE_UNUSED)
2269 bfd_byte *buffer;
2270 asection *asec;
2271 unsigned i;
2272 unsigned num_entries;
2273 bfd_size_type length;
2275 asec = bfd_get_section_by_name (abfd, APUINFO_SECTION_NAME);
2276 if (asec == NULL)
2277 return;
2279 if (!apuinfo_set)
2280 return;
2282 length = asec->size;
2283 if (length < 20)
2284 return;
2286 buffer = bfd_malloc (length);
2287 if (buffer == NULL)
2289 (*_bfd_error_handler)
2290 (_("failed to allocate space for new APUinfo section."));
2291 return;
2294 /* Create the apuinfo header. */
2295 num_entries = apuinfo_list_length ();
2296 bfd_put_32 (abfd, sizeof APUINFO_LABEL, buffer);
2297 bfd_put_32 (abfd, num_entries * 4, buffer + 4);
2298 bfd_put_32 (abfd, 0x2, buffer + 8);
2299 strcpy ((char *) buffer + 12, APUINFO_LABEL);
2301 length = 20;
2302 for (i = 0; i < num_entries; i++)
2304 bfd_put_32 (abfd, apuinfo_list_element (i), buffer + length);
2305 length += 4;
2308 if (length != asec->size)
2309 (*_bfd_error_handler) (_("failed to compute new APUinfo section."));
2311 if (! bfd_set_section_contents (abfd, asec, buffer, (file_ptr) 0, length))
2312 (*_bfd_error_handler) (_("failed to install new APUinfo section."));
2314 free (buffer);
2316 apuinfo_list_finish ();
2319 static bfd_boolean
2320 is_nonpic_glink_stub (bfd *abfd, asection *glink, bfd_vma off)
2322 bfd_byte buf[GLINK_ENTRY_SIZE];
2324 if (!bfd_get_section_contents (abfd, glink, buf, off, GLINK_ENTRY_SIZE))
2325 return FALSE;
2327 return ((bfd_get_32 (abfd, buf + 0) & 0xffff0000) == LIS_11
2328 && (bfd_get_32 (abfd, buf + 4) & 0xffff0000) == LWZ_11_11
2329 && bfd_get_32 (abfd, buf + 8) == MTCTR_11
2330 && bfd_get_32 (abfd, buf + 12) == BCTR);
2333 static bfd_boolean
2334 section_covers_vma (bfd *abfd ATTRIBUTE_UNUSED, asection *section, void *ptr)
2336 bfd_vma vma = *(bfd_vma *) ptr;
2337 return ((section->flags & SEC_ALLOC) != 0
2338 && section->vma <= vma
2339 && vma < section->vma + section->size);
2342 static long
2343 ppc_elf_get_synthetic_symtab (bfd *abfd, long symcount, asymbol **syms,
2344 long dynsymcount, asymbol **dynsyms,
2345 asymbol **ret)
2347 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
2348 asection *plt, *relplt, *dynamic, *glink;
2349 bfd_vma glink_vma = 0;
2350 bfd_vma resolv_vma = 0;
2351 bfd_vma stub_vma;
2352 asymbol *s;
2353 arelent *p;
2354 long count, i;
2355 size_t size;
2356 char *names;
2357 bfd_byte buf[4];
2359 *ret = NULL;
2361 if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0)
2362 return 0;
2364 if (dynsymcount <= 0)
2365 return 0;
2367 relplt = bfd_get_section_by_name (abfd, ".rela.plt");
2368 if (relplt == NULL)
2369 return 0;
2371 plt = bfd_get_section_by_name (abfd, ".plt");
2372 if (plt == NULL)
2373 return 0;
2375 /* Call common code to handle old-style executable PLTs. */
2376 if (elf_section_flags (plt) & SHF_EXECINSTR)
2377 return _bfd_elf_get_synthetic_symtab (abfd, symcount, syms,
2378 dynsymcount, dynsyms, ret);
2380 /* If this object was prelinked, the prelinker stored the address
2381 of .glink at got[1]. If it wasn't prelinked, got[1] will be zero. */
2382 dynamic = bfd_get_section_by_name (abfd, ".dynamic");
2383 if (dynamic != NULL)
2385 bfd_byte *dynbuf, *extdyn, *extdynend;
2386 size_t extdynsize;
2387 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
2389 if (!bfd_malloc_and_get_section (abfd, dynamic, &dynbuf))
2390 return -1;
2392 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
2393 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
2395 extdyn = dynbuf;
2396 extdynend = extdyn + dynamic->size;
2397 for (; extdyn < extdynend; extdyn += extdynsize)
2399 Elf_Internal_Dyn dyn;
2400 (*swap_dyn_in) (abfd, extdyn, &dyn);
2402 if (dyn.d_tag == DT_NULL)
2403 break;
2405 if (dyn.d_tag == DT_PPC_GOT)
2407 unsigned int g_o_t = dyn.d_un.d_val;
2408 asection *got = bfd_get_section_by_name (abfd, ".got");
2409 if (got != NULL
2410 && bfd_get_section_contents (abfd, got, buf,
2411 g_o_t - got->vma + 4, 4))
2412 glink_vma = bfd_get_32 (abfd, buf);
2413 break;
2416 free (dynbuf);
2419 /* Otherwise we read the first plt entry. */
2420 if (glink_vma == 0)
2422 if (bfd_get_section_contents (abfd, plt, buf, 0, 4))
2423 glink_vma = bfd_get_32 (abfd, buf);
2426 if (glink_vma == 0)
2427 return 0;
2429 /* The .glink section usually does not survive the final
2430 link; search for the section (usually .text) where the
2431 glink stubs now reside. */
2432 glink = bfd_sections_find_if (abfd, section_covers_vma, &glink_vma);
2433 if (glink == NULL)
2434 return 0;
2436 /* Determine glink PLT resolver by reading the relative branch
2437 from the first glink stub. */
2438 if (bfd_get_section_contents (abfd, glink, buf,
2439 glink_vma - glink->vma, 4))
2441 unsigned int insn = bfd_get_32 (abfd, buf);
2443 /* The first glink stub may either branch to the resolver ... */
2444 insn ^= B;
2445 if ((insn & ~0x3fffffc) == 0)
2446 resolv_vma = glink_vma + (insn ^ 0x2000000) - 0x2000000;
2448 /* ... or fall through a bunch of NOPs. */
2449 else if ((insn ^ B ^ NOP) == 0)
2450 for (i = 4;
2451 bfd_get_section_contents (abfd, glink, buf,
2452 glink_vma - glink->vma + i, 4);
2453 i += 4)
2454 if (bfd_get_32 (abfd, buf) != NOP)
2456 resolv_vma = glink_vma + i;
2457 break;
2461 count = relplt->size / sizeof (Elf32_External_Rela);
2462 stub_vma = glink_vma - (bfd_vma) count * 16;
2463 /* If the stubs are those for -shared/-pie then we might have
2464 multiple stubs for each plt entry. If that is the case then
2465 there is no way to associate stubs with their plt entries short
2466 of figuring out the GOT pointer value used in the stub. */
2467 if (!is_nonpic_glink_stub (abfd, glink,
2468 glink_vma - GLINK_ENTRY_SIZE - glink->vma))
2469 return 0;
2471 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
2472 if (! (*slurp_relocs) (abfd, relplt, dynsyms, TRUE))
2473 return -1;
2475 size = count * sizeof (asymbol);
2476 p = relplt->relocation;
2477 for (i = 0; i < count; i++, p++)
2479 size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
2480 if (p->addend != 0)
2481 size += sizeof ("+0x") - 1 + 8;
2484 size += sizeof (asymbol) + sizeof ("__glink");
2486 if (resolv_vma)
2487 size += sizeof (asymbol) + sizeof ("__glink_PLTresolve");
2489 s = *ret = bfd_malloc (size);
2490 if (s == NULL)
2491 return -1;
2493 names = (char *) (s + count + 1 + (resolv_vma != 0));
2494 p = relplt->relocation;
2495 for (i = 0; i < count; i++, p++)
2497 size_t len;
2499 *s = **p->sym_ptr_ptr;
2500 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
2501 we are defining a symbol, ensure one of them is set. */
2502 if ((s->flags & BSF_LOCAL) == 0)
2503 s->flags |= BSF_GLOBAL;
2504 s->flags |= BSF_SYNTHETIC;
2505 s->section = glink;
2506 s->value = stub_vma - glink->vma;
2507 s->name = names;
2508 s->udata.p = NULL;
2509 len = strlen ((*p->sym_ptr_ptr)->name);
2510 memcpy (names, (*p->sym_ptr_ptr)->name, len);
2511 names += len;
2512 if (p->addend != 0)
2514 memcpy (names, "+0x", sizeof ("+0x") - 1);
2515 names += sizeof ("+0x") - 1;
2516 bfd_sprintf_vma (abfd, names, p->addend);
2517 names += strlen (names);
2519 memcpy (names, "@plt", sizeof ("@plt"));
2520 names += sizeof ("@plt");
2521 ++s;
2522 stub_vma += 16;
2525 /* Add a symbol at the start of the glink branch table. */
2526 memset (s, 0, sizeof *s);
2527 s->the_bfd = abfd;
2528 s->flags = BSF_GLOBAL | BSF_SYNTHETIC;
2529 s->section = glink;
2530 s->value = glink_vma - glink->vma;
2531 s->name = names;
2532 memcpy (names, "__glink", sizeof ("__glink"));
2533 names += sizeof ("__glink");
2534 s++;
2535 count++;
2537 if (resolv_vma)
2539 /* Add a symbol for the glink PLT resolver. */
2540 memset (s, 0, sizeof *s);
2541 s->the_bfd = abfd;
2542 s->flags = BSF_GLOBAL | BSF_SYNTHETIC;
2543 s->section = glink;
2544 s->value = resolv_vma - glink->vma;
2545 s->name = names;
2546 memcpy (names, "__glink_PLTresolve", sizeof ("__glink_PLTresolve"));
2547 names += sizeof ("__glink_PLTresolve");
2548 s++;
2549 count++;
2552 return count;
2555 /* The following functions are specific to the ELF linker, while
2556 functions above are used generally. They appear in this file more
2557 or less in the order in which they are called. eg.
2558 ppc_elf_check_relocs is called early in the link process,
2559 ppc_elf_finish_dynamic_sections is one of the last functions
2560 called. */
2562 /* The PPC linker needs to keep track of the number of relocs that it
2563 decides to copy as dynamic relocs in check_relocs for each symbol.
2564 This is so that it can later discard them if they are found to be
2565 unnecessary. We store the information in a field extending the
2566 regular ELF linker hash table. */
2568 struct ppc_elf_dyn_relocs
2570 struct ppc_elf_dyn_relocs *next;
2572 /* The input section of the reloc. */
2573 asection *sec;
2575 /* Total number of relocs copied for the input section. */
2576 bfd_size_type count;
2578 /* Number of pc-relative relocs copied for the input section. */
2579 bfd_size_type pc_count;
2582 /* Track PLT entries needed for a given symbol. We might need more
2583 than one glink entry per symbol when generating a pic binary. */
2584 struct plt_entry
2586 struct plt_entry *next;
2588 /* -fPIC uses multiple GOT sections, one per file, called ".got2".
2589 This field stores the offset into .got2 used to initialise the
2590 GOT pointer reg. It will always be at least 32768. (Current
2591 gcc always uses an offset of 32768, but ld -r will pack .got2
2592 sections together resulting in larger offsets). */
2593 bfd_vma addend;
2595 /* The .got2 section. */
2596 asection *sec;
2598 /* PLT refcount or offset. */
2599 union
2601 bfd_signed_vma refcount;
2602 bfd_vma offset;
2603 } plt;
2605 /* .glink stub offset. */
2606 bfd_vma glink_offset;
2609 /* Of those relocs that might be copied as dynamic relocs, this function
2610 selects those that must be copied when linking a shared library,
2611 even when the symbol is local. */
2613 static int
2614 must_be_dyn_reloc (struct bfd_link_info *info,
2615 enum elf_ppc_reloc_type r_type)
2617 switch (r_type)
2619 default:
2620 return 1;
2622 case R_PPC_REL24:
2623 case R_PPC_REL14:
2624 case R_PPC_REL14_BRTAKEN:
2625 case R_PPC_REL14_BRNTAKEN:
2626 case R_PPC_REL32:
2627 return 0;
2629 case R_PPC_TPREL32:
2630 case R_PPC_TPREL16:
2631 case R_PPC_TPREL16_LO:
2632 case R_PPC_TPREL16_HI:
2633 case R_PPC_TPREL16_HA:
2634 return !info->executable;
2638 /* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
2639 copying dynamic variables from a shared lib into an app's dynbss
2640 section, and instead use a dynamic relocation to point into the
2641 shared lib. */
2642 #define ELIMINATE_COPY_RELOCS 1
2644 /* PPC ELF linker hash entry. */
2646 struct ppc_elf_link_hash_entry
2648 struct elf_link_hash_entry elf;
2650 /* If this symbol is used in the linker created sections, the processor
2651 specific backend uses this field to map the field into the offset
2652 from the beginning of the section. */
2653 elf_linker_section_pointers_t *linker_section_pointer;
2655 /* Track dynamic relocs copied for this symbol. */
2656 struct ppc_elf_dyn_relocs *dyn_relocs;
2658 /* Contexts in which symbol is used in the GOT (or TOC).
2659 TLS_GD .. TLS_TLS bits are or'd into the mask as the
2660 corresponding relocs are encountered during check_relocs.
2661 tls_optimize clears TLS_GD .. TLS_TPREL when optimizing to
2662 indicate the corresponding GOT entry type is not needed. */
2663 #define TLS_GD 1 /* GD reloc. */
2664 #define TLS_LD 2 /* LD reloc. */
2665 #define TLS_TPREL 4 /* TPREL reloc, => IE. */
2666 #define TLS_DTPREL 8 /* DTPREL reloc, => LD. */
2667 #define TLS_TLS 16 /* Any TLS reloc. */
2668 #define TLS_TPRELGD 32 /* TPREL reloc resulting from GD->IE. */
2669 #define PLT_IFUNC 64 /* STT_GNU_IFUNC. */
2670 char tls_mask;
2672 /* Nonzero if we have seen a small data relocation referring to this
2673 symbol. */
2674 unsigned char has_sda_refs;
2677 #define ppc_elf_hash_entry(ent) ((struct ppc_elf_link_hash_entry *) (ent))
2679 /* PPC ELF linker hash table. */
2681 struct ppc_elf_link_hash_table
2683 struct elf_link_hash_table elf;
2685 /* Short-cuts to get to dynamic linker sections. */
2686 asection *got;
2687 asection *relgot;
2688 asection *glink;
2689 asection *plt;
2690 asection *relplt;
2691 asection *iplt;
2692 asection *reliplt;
2693 asection *dynbss;
2694 asection *relbss;
2695 asection *dynsbss;
2696 asection *relsbss;
2697 elf_linker_section_t sdata[2];
2698 asection *sbss;
2700 /* The (unloaded but important) .rela.plt.unloaded on VxWorks. */
2701 asection *srelplt2;
2703 /* The .got.plt section (VxWorks only)*/
2704 asection *sgotplt;
2706 /* Shortcut to __tls_get_addr. */
2707 struct elf_link_hash_entry *tls_get_addr;
2709 /* The bfd that forced an old-style PLT. */
2710 bfd *old_bfd;
2712 /* TLS local dynamic got entry handling. */
2713 union {
2714 bfd_signed_vma refcount;
2715 bfd_vma offset;
2716 } tlsld_got;
2718 /* Offset of branch table to PltResolve function in glink. */
2719 bfd_vma glink_pltresolve;
2721 /* Size of reserved GOT entries. */
2722 unsigned int got_header_size;
2723 /* Non-zero if allocating the header left a gap. */
2724 unsigned int got_gap;
2726 /* The type of PLT we have chosen to use. */
2727 enum ppc_elf_plt_type plt_type;
2729 /* Set if we should emit symbols for stubs. */
2730 unsigned int emit_stub_syms:1;
2732 /* Set if __tls_get_addr optimization should not be done. */
2733 unsigned int no_tls_get_addr_opt:1;
2735 /* True if the target system is VxWorks. */
2736 unsigned int is_vxworks:1;
2738 /* The size of PLT entries. */
2739 int plt_entry_size;
2740 /* The distance between adjacent PLT slots. */
2741 int plt_slot_size;
2742 /* The size of the first PLT entry. */
2743 int plt_initial_entry_size;
2745 /* Small local sym cache. */
2746 struct sym_cache sym_cache;
2749 /* Rename some of the generic section flags to better document how they
2750 are used here. */
2752 /* Nonzero if this section has TLS related relocations. */
2753 #define has_tls_reloc sec_flg0
2755 /* Nonzero if this section has a call to __tls_get_addr. */
2756 #define has_tls_get_addr_call sec_flg1
2758 /* Get the PPC ELF linker hash table from a link_info structure. */
2760 #define ppc_elf_hash_table(p) \
2761 (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
2762 == PPC32_ELF_DATA ? ((struct ppc_elf_link_hash_table *) ((p)->hash)) : NULL)
2764 /* Create an entry in a PPC ELF linker hash table. */
2766 static struct bfd_hash_entry *
2767 ppc_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
2768 struct bfd_hash_table *table,
2769 const char *string)
2771 /* Allocate the structure if it has not already been allocated by a
2772 subclass. */
2773 if (entry == NULL)
2775 entry = bfd_hash_allocate (table,
2776 sizeof (struct ppc_elf_link_hash_entry));
2777 if (entry == NULL)
2778 return entry;
2781 /* Call the allocation method of the superclass. */
2782 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
2783 if (entry != NULL)
2785 ppc_elf_hash_entry (entry)->linker_section_pointer = NULL;
2786 ppc_elf_hash_entry (entry)->dyn_relocs = NULL;
2787 ppc_elf_hash_entry (entry)->tls_mask = 0;
2788 ppc_elf_hash_entry (entry)->has_sda_refs = 0;
2791 return entry;
2794 /* Create a PPC ELF linker hash table. */
2796 static struct bfd_link_hash_table *
2797 ppc_elf_link_hash_table_create (bfd *abfd)
2799 struct ppc_elf_link_hash_table *ret;
2801 ret = bfd_zmalloc (sizeof (struct ppc_elf_link_hash_table));
2802 if (ret == NULL)
2803 return NULL;
2805 if (!_bfd_elf_link_hash_table_init (&ret->elf, abfd,
2806 ppc_elf_link_hash_newfunc,
2807 sizeof (struct ppc_elf_link_hash_entry),
2808 PPC32_ELF_DATA))
2810 free (ret);
2811 return NULL;
2814 ret->elf.init_plt_refcount.refcount = 0;
2815 ret->elf.init_plt_refcount.glist = NULL;
2816 ret->elf.init_plt_offset.offset = 0;
2817 ret->elf.init_plt_offset.glist = NULL;
2819 ret->sdata[0].name = ".sdata";
2820 ret->sdata[0].sym_name = "_SDA_BASE_";
2821 ret->sdata[0].bss_name = ".sbss";
2823 ret->sdata[1].name = ".sdata2";
2824 ret->sdata[1].sym_name = "_SDA2_BASE_";
2825 ret->sdata[1].bss_name = ".sbss2";
2827 ret->plt_entry_size = 12;
2828 ret->plt_slot_size = 8;
2829 ret->plt_initial_entry_size = 72;
2831 return &ret->elf.root;
2834 /* Create .got and the related sections. */
2836 static bfd_boolean
2837 ppc_elf_create_got (bfd *abfd, struct bfd_link_info *info)
2839 struct ppc_elf_link_hash_table *htab;
2840 asection *s;
2841 flagword flags;
2843 if (!_bfd_elf_create_got_section (abfd, info))
2844 return FALSE;
2846 htab = ppc_elf_hash_table (info);
2847 htab->got = s = bfd_get_section_by_name (abfd, ".got");
2848 if (s == NULL)
2849 abort ();
2851 if (htab->is_vxworks)
2853 htab->sgotplt = bfd_get_section_by_name (abfd, ".got.plt");
2854 if (!htab->sgotplt)
2855 abort ();
2857 else
2859 /* The powerpc .got has a blrl instruction in it. Mark it
2860 executable. */
2861 flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_HAS_CONTENTS
2862 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
2863 if (!bfd_set_section_flags (abfd, s, flags))
2864 return FALSE;
2867 htab->relgot = bfd_get_section_by_name (abfd, ".rela.got");
2868 if (!htab->relgot)
2869 abort ();
2871 return TRUE;
2874 static bfd_boolean
2875 ppc_elf_create_glink (bfd *abfd, struct bfd_link_info *info)
2877 struct ppc_elf_link_hash_table *htab = ppc_elf_hash_table (info);
2878 asection *s;
2879 flagword flags;
2881 flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY | SEC_HAS_CONTENTS
2882 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
2883 s = bfd_make_section_anyway_with_flags (abfd, ".glink", flags);
2884 htab->glink = s;
2885 if (s == NULL
2886 || !bfd_set_section_alignment (abfd, s, 4))
2887 return FALSE;
2889 flags = SEC_ALLOC | SEC_LINKER_CREATED;
2890 s = bfd_make_section_anyway_with_flags (abfd, ".iplt", flags);
2891 htab->iplt = s;
2892 if (s == NULL
2893 || !bfd_set_section_alignment (abfd, s, 4))
2894 return FALSE;
2896 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_HAS_CONTENTS
2897 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
2898 s = bfd_make_section_with_flags (abfd, ".rela.iplt", flags);
2899 htab->reliplt = s;
2900 if (s == NULL
2901 || ! bfd_set_section_alignment (abfd, s, 2))
2902 return FALSE;
2903 return TRUE;
2906 /* We have to create .dynsbss and .rela.sbss here so that they get mapped
2907 to output sections (just like _bfd_elf_create_dynamic_sections has
2908 to create .dynbss and .rela.bss). */
2910 static bfd_boolean
2911 ppc_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
2913 struct ppc_elf_link_hash_table *htab;
2914 asection *s;
2915 flagword flags;
2917 htab = ppc_elf_hash_table (info);
2919 if (htab->got == NULL
2920 && !ppc_elf_create_got (abfd, info))
2921 return FALSE;
2923 if (!_bfd_elf_create_dynamic_sections (abfd, info))
2924 return FALSE;
2926 if (htab->glink == NULL
2927 && !ppc_elf_create_glink (abfd, info))
2928 return FALSE;
2930 htab->dynbss = bfd_get_section_by_name (abfd, ".dynbss");
2931 s = bfd_make_section_with_flags (abfd, ".dynsbss",
2932 SEC_ALLOC | SEC_LINKER_CREATED);
2933 htab->dynsbss = s;
2934 if (s == NULL)
2935 return FALSE;
2937 if (! info->shared)
2939 htab->relbss = bfd_get_section_by_name (abfd, ".rela.bss");
2940 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_HAS_CONTENTS
2941 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
2942 s = bfd_make_section_with_flags (abfd, ".rela.sbss", flags);
2943 htab->relsbss = s;
2944 if (s == NULL
2945 || ! bfd_set_section_alignment (abfd, s, 2))
2946 return FALSE;
2949 if (htab->is_vxworks
2950 && !elf_vxworks_create_dynamic_sections (abfd, info, &htab->srelplt2))
2951 return FALSE;
2953 htab->relplt = bfd_get_section_by_name (abfd, ".rela.plt");
2954 htab->plt = s = bfd_get_section_by_name (abfd, ".plt");
2955 if (s == NULL)
2956 abort ();
2958 flags = SEC_ALLOC | SEC_CODE | SEC_LINKER_CREATED;
2959 if (htab->plt_type == PLT_VXWORKS)
2960 /* The VxWorks PLT is a loaded section with contents. */
2961 flags |= SEC_HAS_CONTENTS | SEC_LOAD | SEC_READONLY;
2962 return bfd_set_section_flags (abfd, s, flags);
2965 /* Copy the extra info we tack onto an elf_link_hash_entry. */
2967 static void
2968 ppc_elf_copy_indirect_symbol (struct bfd_link_info *info,
2969 struct elf_link_hash_entry *dir,
2970 struct elf_link_hash_entry *ind)
2972 struct ppc_elf_link_hash_entry *edir, *eind;
2974 edir = (struct ppc_elf_link_hash_entry *) dir;
2975 eind = (struct ppc_elf_link_hash_entry *) ind;
2977 if (eind->dyn_relocs != NULL)
2979 if (edir->dyn_relocs != NULL)
2981 struct ppc_elf_dyn_relocs **pp;
2982 struct ppc_elf_dyn_relocs *p;
2984 /* Add reloc counts against the indirect sym to the direct sym
2985 list. Merge any entries against the same section. */
2986 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
2988 struct ppc_elf_dyn_relocs *q;
2990 for (q = edir->dyn_relocs; q != NULL; q = q->next)
2991 if (q->sec == p->sec)
2993 q->pc_count += p->pc_count;
2994 q->count += p->count;
2995 *pp = p->next;
2996 break;
2998 if (q == NULL)
2999 pp = &p->next;
3001 *pp = edir->dyn_relocs;
3004 edir->dyn_relocs = eind->dyn_relocs;
3005 eind->dyn_relocs = NULL;
3008 edir->tls_mask |= eind->tls_mask;
3009 edir->has_sda_refs |= eind->has_sda_refs;
3011 /* If called to transfer flags for a weakdef during processing
3012 of elf_adjust_dynamic_symbol, don't copy non_got_ref.
3013 We clear it ourselves for ELIMINATE_COPY_RELOCS. */
3014 if (!(ELIMINATE_COPY_RELOCS
3015 && eind->elf.root.type != bfd_link_hash_indirect
3016 && edir->elf.dynamic_adjusted))
3017 edir->elf.non_got_ref |= eind->elf.non_got_ref;
3019 edir->elf.ref_dynamic |= eind->elf.ref_dynamic;
3020 edir->elf.ref_regular |= eind->elf.ref_regular;
3021 edir->elf.ref_regular_nonweak |= eind->elf.ref_regular_nonweak;
3022 edir->elf.needs_plt |= eind->elf.needs_plt;
3023 edir->elf.pointer_equality_needed |= eind->elf.pointer_equality_needed;
3025 /* If we were called to copy over info for a weak sym, that's all. */
3026 if (eind->elf.root.type != bfd_link_hash_indirect)
3027 return;
3029 /* Copy over the GOT refcount entries that we may have already seen to
3030 the symbol which just became indirect. */
3031 edir->elf.got.refcount += eind->elf.got.refcount;
3032 eind->elf.got.refcount = 0;
3034 /* And plt entries. */
3035 if (eind->elf.plt.plist != NULL)
3037 if (edir->elf.plt.plist != NULL)
3039 struct plt_entry **entp;
3040 struct plt_entry *ent;
3042 for (entp = &eind->elf.plt.plist; (ent = *entp) != NULL; )
3044 struct plt_entry *dent;
3046 for (dent = edir->elf.plt.plist; dent != NULL; dent = dent->next)
3047 if (dent->sec == ent->sec && dent->addend == ent->addend)
3049 dent->plt.refcount += ent->plt.refcount;
3050 *entp = ent->next;
3051 break;
3053 if (dent == NULL)
3054 entp = &ent->next;
3056 *entp = edir->elf.plt.plist;
3059 edir->elf.plt.plist = eind->elf.plt.plist;
3060 eind->elf.plt.plist = NULL;
3063 if (eind->elf.dynindx != -1)
3065 if (edir->elf.dynindx != -1)
3066 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
3067 edir->elf.dynstr_index);
3068 edir->elf.dynindx = eind->elf.dynindx;
3069 edir->elf.dynstr_index = eind->elf.dynstr_index;
3070 eind->elf.dynindx = -1;
3071 eind->elf.dynstr_index = 0;
3075 /* Hook called by the linker routine which adds symbols from an object
3076 file. We use it to put .comm items in .sbss, and not .bss. */
3078 static bfd_boolean
3079 ppc_elf_add_symbol_hook (bfd *abfd,
3080 struct bfd_link_info *info,
3081 Elf_Internal_Sym *sym,
3082 const char **namep ATTRIBUTE_UNUSED,
3083 flagword *flagsp ATTRIBUTE_UNUSED,
3084 asection **secp,
3085 bfd_vma *valp)
3087 if (sym->st_shndx == SHN_COMMON
3088 && !info->relocatable
3089 && is_ppc_elf (info->output_bfd)
3090 && sym->st_size <= elf_gp_size (abfd))
3092 /* Common symbols less than or equal to -G nn bytes are automatically
3093 put into .sbss. */
3094 struct ppc_elf_link_hash_table *htab;
3096 htab = ppc_elf_hash_table (info);
3097 if (htab->sbss == NULL)
3099 flagword flags = SEC_IS_COMMON | SEC_LINKER_CREATED;
3101 if (!htab->elf.dynobj)
3102 htab->elf.dynobj = abfd;
3104 htab->sbss = bfd_make_section_anyway_with_flags (htab->elf.dynobj,
3105 ".sbss",
3106 flags);
3107 if (htab->sbss == NULL)
3108 return FALSE;
3111 *secp = htab->sbss;
3112 *valp = sym->st_size;
3115 if ((abfd->flags & DYNAMIC) == 0
3116 && ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
3117 elf_tdata (info->output_bfd)->has_ifunc_symbols = TRUE;
3119 return TRUE;
3122 static bfd_boolean
3123 create_sdata_sym (struct bfd_link_info *info, elf_linker_section_t *lsect)
3125 struct ppc_elf_link_hash_table *htab = ppc_elf_hash_table (info);
3127 lsect->sym = elf_link_hash_lookup (&htab->elf, lsect->sym_name,
3128 TRUE, FALSE, TRUE);
3129 if (lsect->sym == NULL)
3130 return FALSE;
3131 if (lsect->sym->root.type == bfd_link_hash_new)
3132 lsect->sym->non_elf = 0;
3133 lsect->sym->ref_regular = 1;
3134 _bfd_elf_link_hash_hide_symbol (info, lsect->sym, TRUE);
3135 return TRUE;
3138 /* Create a special linker section. */
3140 static bfd_boolean
3141 ppc_elf_create_linker_section (bfd *abfd,
3142 struct bfd_link_info *info,
3143 flagword flags,
3144 elf_linker_section_t *lsect)
3146 struct ppc_elf_link_hash_table *htab = ppc_elf_hash_table (info);
3147 asection *s;
3149 flags |= (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
3150 | SEC_LINKER_CREATED);
3152 /* Record the first bfd that needs the special sections. */
3153 if (!htab->elf.dynobj)
3154 htab->elf.dynobj = abfd;
3156 s = bfd_make_section_anyway_with_flags (htab->elf.dynobj,
3157 lsect->name,
3158 flags);
3159 if (s == NULL
3160 || !bfd_set_section_alignment (htab->elf.dynobj, s, 2))
3161 return FALSE;
3162 lsect->section = s;
3164 return create_sdata_sym (info, lsect);
3167 /* Find a linker generated pointer with a given addend and type. */
3169 static elf_linker_section_pointers_t *
3170 elf_find_pointer_linker_section
3171 (elf_linker_section_pointers_t *linker_pointers,
3172 bfd_vma addend,
3173 elf_linker_section_t *lsect)
3175 for ( ; linker_pointers != NULL; linker_pointers = linker_pointers->next)
3176 if (lsect == linker_pointers->lsect && addend == linker_pointers->addend)
3177 return linker_pointers;
3179 return NULL;
3182 /* Allocate a pointer to live in a linker created section. */
3184 static bfd_boolean
3185 elf_create_pointer_linker_section (bfd *abfd,
3186 elf_linker_section_t *lsect,
3187 struct elf_link_hash_entry *h,
3188 const Elf_Internal_Rela *rel)
3190 elf_linker_section_pointers_t **ptr_linker_section_ptr = NULL;
3191 elf_linker_section_pointers_t *linker_section_ptr;
3192 unsigned long r_symndx = ELF32_R_SYM (rel->r_info);
3193 bfd_size_type amt;
3195 BFD_ASSERT (lsect != NULL);
3197 /* Is this a global symbol? */
3198 if (h != NULL)
3200 struct ppc_elf_link_hash_entry *eh;
3202 /* Has this symbol already been allocated? If so, our work is done. */
3203 eh = (struct ppc_elf_link_hash_entry *) h;
3204 if (elf_find_pointer_linker_section (eh->linker_section_pointer,
3205 rel->r_addend,
3206 lsect))
3207 return TRUE;
3209 ptr_linker_section_ptr = &eh->linker_section_pointer;
3211 else
3213 BFD_ASSERT (is_ppc_elf (abfd));
3215 /* Allocation of a pointer to a local symbol. */
3216 elf_linker_section_pointers_t **ptr = elf_local_ptr_offsets (abfd);
3218 /* Allocate a table to hold the local symbols if first time. */
3219 if (!ptr)
3221 unsigned int num_symbols = elf_symtab_hdr (abfd).sh_info;
3223 amt = num_symbols;
3224 amt *= sizeof (elf_linker_section_pointers_t *);
3225 ptr = bfd_zalloc (abfd, amt);
3227 if (!ptr)
3228 return FALSE;
3230 elf_local_ptr_offsets (abfd) = ptr;
3233 /* Has this symbol already been allocated? If so, our work is done. */
3234 if (elf_find_pointer_linker_section (ptr[r_symndx],
3235 rel->r_addend,
3236 lsect))
3237 return TRUE;
3239 ptr_linker_section_ptr = &ptr[r_symndx];
3242 /* Allocate space for a pointer in the linker section, and allocate
3243 a new pointer record from internal memory. */
3244 BFD_ASSERT (ptr_linker_section_ptr != NULL);
3245 amt = sizeof (elf_linker_section_pointers_t);
3246 linker_section_ptr = bfd_alloc (abfd, amt);
3248 if (!linker_section_ptr)
3249 return FALSE;
3251 linker_section_ptr->next = *ptr_linker_section_ptr;
3252 linker_section_ptr->addend = rel->r_addend;
3253 linker_section_ptr->lsect = lsect;
3254 *ptr_linker_section_ptr = linker_section_ptr;
3256 linker_section_ptr->offset = lsect->section->size;
3257 lsect->section->size += 4;
3259 #ifdef DEBUG
3260 fprintf (stderr,
3261 "Create pointer in linker section %s, offset = %ld, section size = %ld\n",
3262 lsect->name, (long) linker_section_ptr->offset,
3263 (long) lsect->section->size);
3264 #endif
3266 return TRUE;
3269 static struct plt_entry **
3270 update_local_sym_info (bfd *abfd,
3271 Elf_Internal_Shdr *symtab_hdr,
3272 unsigned long r_symndx,
3273 int tls_type)
3275 bfd_signed_vma *local_got_refcounts = elf_local_got_refcounts (abfd);
3276 struct plt_entry **local_plt;
3277 char *local_got_tls_masks;
3279 if (local_got_refcounts == NULL)
3281 bfd_size_type size = symtab_hdr->sh_info;
3283 size *= (sizeof (*local_got_refcounts)
3284 + sizeof (*local_plt)
3285 + sizeof (*local_got_tls_masks));
3286 local_got_refcounts = bfd_zalloc (abfd, size);
3287 if (local_got_refcounts == NULL)
3288 return NULL;
3289 elf_local_got_refcounts (abfd) = local_got_refcounts;
3292 local_plt = (struct plt_entry **) (local_got_refcounts + symtab_hdr->sh_info);
3293 local_got_tls_masks = (char *) (local_plt + symtab_hdr->sh_info);
3294 local_got_tls_masks[r_symndx] |= tls_type;
3295 if (tls_type != PLT_IFUNC)
3296 local_got_refcounts[r_symndx] += 1;
3297 return local_plt + r_symndx;
3300 static bfd_boolean
3301 update_plt_info (bfd *abfd, struct plt_entry **plist,
3302 asection *sec, bfd_vma addend)
3304 struct plt_entry *ent;
3306 if (addend < 32768)
3307 sec = NULL;
3308 for (ent = *plist; ent != NULL; ent = ent->next)
3309 if (ent->sec == sec && ent->addend == addend)
3310 break;
3311 if (ent == NULL)
3313 bfd_size_type amt = sizeof (*ent);
3314 ent = bfd_alloc (abfd, amt);
3315 if (ent == NULL)
3316 return FALSE;
3317 ent->next = *plist;
3318 ent->sec = sec;
3319 ent->addend = addend;
3320 ent->plt.refcount = 0;
3321 *plist = ent;
3323 ent->plt.refcount += 1;
3324 return TRUE;
3327 static struct plt_entry *
3328 find_plt_ent (struct plt_entry **plist, asection *sec, bfd_vma addend)
3330 struct plt_entry *ent;
3332 if (addend < 32768)
3333 sec = NULL;
3334 for (ent = *plist; ent != NULL; ent = ent->next)
3335 if (ent->sec == sec && ent->addend == addend)
3336 break;
3337 return ent;
3340 static bfd_boolean
3341 is_branch_reloc (enum elf_ppc_reloc_type r_type)
3343 return (r_type == R_PPC_PLTREL24
3344 || r_type == R_PPC_LOCAL24PC
3345 || r_type == R_PPC_REL24
3346 || r_type == R_PPC_REL14
3347 || r_type == R_PPC_REL14_BRTAKEN
3348 || r_type == R_PPC_REL14_BRNTAKEN
3349 || r_type == R_PPC_ADDR24
3350 || r_type == R_PPC_ADDR14
3351 || r_type == R_PPC_ADDR14_BRTAKEN
3352 || r_type == R_PPC_ADDR14_BRNTAKEN);
3355 static void
3356 bad_shared_reloc (bfd *abfd, enum elf_ppc_reloc_type r_type)
3358 (*_bfd_error_handler)
3359 (_("%B: relocation %s cannot be used when making a shared object"),
3360 abfd,
3361 ppc_elf_howto_table[r_type]->name);
3362 bfd_set_error (bfd_error_bad_value);
3365 /* Look through the relocs for a section during the first phase, and
3366 allocate space in the global offset table or procedure linkage
3367 table. */
3369 static bfd_boolean
3370 ppc_elf_check_relocs (bfd *abfd,
3371 struct bfd_link_info *info,
3372 asection *sec,
3373 const Elf_Internal_Rela *relocs)
3375 struct ppc_elf_link_hash_table *htab;
3376 Elf_Internal_Shdr *symtab_hdr;
3377 struct elf_link_hash_entry **sym_hashes;
3378 const Elf_Internal_Rela *rel;
3379 const Elf_Internal_Rela *rel_end;
3380 asection *got2, *sreloc;
3381 struct elf_link_hash_entry *tga;
3383 if (info->relocatable)
3384 return TRUE;
3386 /* Don't do anything special with non-loaded, non-alloced sections.
3387 In particular, any relocs in such sections should not affect GOT
3388 and PLT reference counting (ie. we don't allow them to create GOT
3389 or PLT entries), there's no possibility or desire to optimize TLS
3390 relocs, and there's not much point in propagating relocs to shared
3391 libs that the dynamic linker won't relocate. */
3392 if ((sec->flags & SEC_ALLOC) == 0)
3393 return TRUE;
3395 #ifdef DEBUG
3396 _bfd_error_handler ("ppc_elf_check_relocs called for section %A in %B",
3397 sec, abfd);
3398 #endif
3400 BFD_ASSERT (is_ppc_elf (abfd));
3402 /* Initialize howto table if not already done. */
3403 if (!ppc_elf_howto_table[R_PPC_ADDR32])
3404 ppc_elf_howto_init ();
3406 htab = ppc_elf_hash_table (info);
3407 if (htab->glink == NULL)
3409 if (htab->elf.dynobj == NULL)
3410 htab->elf.dynobj = abfd;
3411 if (!ppc_elf_create_glink (htab->elf.dynobj, info))
3412 return FALSE;
3414 tga = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
3415 FALSE, FALSE, TRUE);
3416 symtab_hdr = &elf_symtab_hdr (abfd);
3417 sym_hashes = elf_sym_hashes (abfd);
3418 got2 = bfd_get_section_by_name (abfd, ".got2");
3419 sreloc = NULL;
3421 rel_end = relocs + sec->reloc_count;
3422 for (rel = relocs; rel < rel_end; rel++)
3424 unsigned long r_symndx;
3425 enum elf_ppc_reloc_type r_type;
3426 struct elf_link_hash_entry *h;
3427 int tls_type;
3429 r_symndx = ELF32_R_SYM (rel->r_info);
3430 if (r_symndx < symtab_hdr->sh_info)
3431 h = NULL;
3432 else
3434 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
3435 while (h->root.type == bfd_link_hash_indirect
3436 || h->root.type == bfd_link_hash_warning)
3437 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3440 /* If a relocation refers to _GLOBAL_OFFSET_TABLE_, create the .got.
3441 This shows up in particular in an R_PPC_ADDR32 in the eabi
3442 startup code. */
3443 if (h != NULL
3444 && htab->got == NULL
3445 && strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
3447 if (htab->elf.dynobj == NULL)
3448 htab->elf.dynobj = abfd;
3449 if (!ppc_elf_create_got (htab->elf.dynobj, info))
3450 return FALSE;
3451 BFD_ASSERT (h == htab->elf.hgot);
3454 tls_type = 0;
3455 r_type = ELF32_R_TYPE (rel->r_info);
3456 if (h == NULL && !htab->is_vxworks)
3458 Elf_Internal_Sym *isym = bfd_sym_from_r_symndx (&htab->sym_cache,
3459 abfd, r_symndx);
3460 if (isym == NULL)
3461 return FALSE;
3463 if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC
3464 && (!info->shared
3465 || is_branch_reloc (r_type)))
3467 struct plt_entry **ifunc;
3468 bfd_vma addend;
3470 ifunc = update_local_sym_info (abfd, symtab_hdr, r_symndx,
3471 PLT_IFUNC);
3472 if (ifunc == NULL)
3473 return FALSE;
3475 /* STT_GNU_IFUNC symbols must have a PLT entry;
3476 In a non-pie executable even when there are
3477 no plt calls. */
3478 addend = 0;
3479 if (r_type == R_PPC_PLTREL24)
3481 ppc_elf_tdata (abfd)->makes_plt_call = 1;
3482 if (info->shared)
3483 addend = rel->r_addend;
3485 if (!update_plt_info (abfd, ifunc, got2, addend))
3486 return FALSE;
3490 if (!htab->is_vxworks
3491 && is_branch_reloc (r_type)
3492 && h != NULL
3493 && h == tga)
3495 if (rel != relocs
3496 && (ELF32_R_TYPE (rel[-1].r_info) == R_PPC_TLSGD
3497 || ELF32_R_TYPE (rel[-1].r_info) == R_PPC_TLSLD))
3498 /* We have a new-style __tls_get_addr call with a marker
3499 reloc. */
3501 else
3502 /* Mark this section as having an old-style call. */
3503 sec->has_tls_get_addr_call = 1;
3506 switch (r_type)
3508 case R_PPC_TLSGD:
3509 case R_PPC_TLSLD:
3510 /* These special tls relocs tie a call to __tls_get_addr with
3511 its parameter symbol. */
3512 break;
3514 case R_PPC_GOT_TLSLD16:
3515 case R_PPC_GOT_TLSLD16_LO:
3516 case R_PPC_GOT_TLSLD16_HI:
3517 case R_PPC_GOT_TLSLD16_HA:
3518 tls_type = TLS_TLS | TLS_LD;
3519 goto dogottls;
3521 case R_PPC_GOT_TLSGD16:
3522 case R_PPC_GOT_TLSGD16_LO:
3523 case R_PPC_GOT_TLSGD16_HI:
3524 case R_PPC_GOT_TLSGD16_HA:
3525 tls_type = TLS_TLS | TLS_GD;
3526 goto dogottls;
3528 case R_PPC_GOT_TPREL16:
3529 case R_PPC_GOT_TPREL16_LO:
3530 case R_PPC_GOT_TPREL16_HI:
3531 case R_PPC_GOT_TPREL16_HA:
3532 if (!info->executable)
3533 info->flags |= DF_STATIC_TLS;
3534 tls_type = TLS_TLS | TLS_TPREL;
3535 goto dogottls;
3537 case R_PPC_GOT_DTPREL16:
3538 case R_PPC_GOT_DTPREL16_LO:
3539 case R_PPC_GOT_DTPREL16_HI:
3540 case R_PPC_GOT_DTPREL16_HA:
3541 tls_type = TLS_TLS | TLS_DTPREL;
3542 dogottls:
3543 sec->has_tls_reloc = 1;
3544 /* Fall thru */
3546 /* GOT16 relocations */
3547 case R_PPC_GOT16:
3548 case R_PPC_GOT16_LO:
3549 case R_PPC_GOT16_HI:
3550 case R_PPC_GOT16_HA:
3551 /* This symbol requires a global offset table entry. */
3552 if (htab->got == NULL)
3554 if (htab->elf.dynobj == NULL)
3555 htab->elf.dynobj = abfd;
3556 if (!ppc_elf_create_got (htab->elf.dynobj, info))
3557 return FALSE;
3559 if (h != NULL)
3561 h->got.refcount += 1;
3562 ppc_elf_hash_entry (h)->tls_mask |= tls_type;
3564 else
3565 /* This is a global offset table entry for a local symbol. */
3566 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx, tls_type))
3567 return FALSE;
3569 /* We may also need a plt entry if the symbol turns out to be
3570 an ifunc. */
3571 if (h != NULL && !info->shared)
3573 if (!update_plt_info (abfd, &h->plt.plist, NULL, 0))
3574 return FALSE;
3576 break;
3578 /* Indirect .sdata relocation. */
3579 case R_PPC_EMB_SDAI16:
3580 if (info->shared)
3582 bad_shared_reloc (abfd, r_type);
3583 return FALSE;
3585 if (htab->sdata[0].section == NULL
3586 && !ppc_elf_create_linker_section (abfd, info, 0,
3587 &htab->sdata[0]))
3588 return FALSE;
3589 if (!elf_create_pointer_linker_section (abfd, &htab->sdata[0],
3590 h, rel))
3591 return FALSE;
3592 if (h != NULL)
3594 ppc_elf_hash_entry (h)->has_sda_refs = TRUE;
3595 h->non_got_ref = TRUE;
3597 break;
3599 /* Indirect .sdata2 relocation. */
3600 case R_PPC_EMB_SDA2I16:
3601 if (info->shared)
3603 bad_shared_reloc (abfd, r_type);
3604 return FALSE;
3606 if (htab->sdata[1].section == NULL
3607 && !ppc_elf_create_linker_section (abfd, info, SEC_READONLY,
3608 &htab->sdata[1]))
3609 return FALSE;
3610 if (!elf_create_pointer_linker_section (abfd, &htab->sdata[1],
3611 h, rel))
3612 return FALSE;
3613 if (h != NULL)
3615 ppc_elf_hash_entry (h)->has_sda_refs = TRUE;
3616 h->non_got_ref = TRUE;
3618 break;
3620 case R_PPC_SDAREL16:
3621 if (htab->sdata[0].sym == NULL
3622 && !create_sdata_sym (info, &htab->sdata[0]))
3623 return FALSE;
3624 if (h != NULL)
3626 ppc_elf_hash_entry (h)->has_sda_refs = TRUE;
3627 h->non_got_ref = TRUE;
3629 break;
3631 case R_PPC_EMB_SDA2REL:
3632 if (info->shared)
3634 bad_shared_reloc (abfd, r_type);
3635 return FALSE;
3637 if (htab->sdata[1].sym == NULL
3638 && !create_sdata_sym (info, &htab->sdata[1]))
3639 return FALSE;
3640 if (h != NULL)
3642 ppc_elf_hash_entry (h)->has_sda_refs = TRUE;
3643 h->non_got_ref = TRUE;
3645 break;
3647 case R_PPC_EMB_SDA21:
3648 case R_PPC_EMB_RELSDA:
3649 if (info->shared)
3651 bad_shared_reloc (abfd, r_type);
3652 return FALSE;
3654 if (htab->sdata[0].sym == NULL
3655 && !create_sdata_sym (info, &htab->sdata[0]))
3656 return FALSE;
3657 if (htab->sdata[1].sym == NULL
3658 && !create_sdata_sym (info, &htab->sdata[1]))
3659 return FALSE;
3660 if (h != NULL)
3662 ppc_elf_hash_entry (h)->has_sda_refs = TRUE;
3663 h->non_got_ref = TRUE;
3665 break;
3667 case R_PPC_EMB_NADDR32:
3668 case R_PPC_EMB_NADDR16:
3669 case R_PPC_EMB_NADDR16_LO:
3670 case R_PPC_EMB_NADDR16_HI:
3671 case R_PPC_EMB_NADDR16_HA:
3672 if (info->shared)
3674 bad_shared_reloc (abfd, r_type);
3675 return FALSE;
3677 if (h != NULL)
3678 h->non_got_ref = TRUE;
3679 break;
3681 case R_PPC_PLTREL24:
3682 if (h == NULL)
3683 break;
3684 /* Fall through */
3685 case R_PPC_PLT32:
3686 case R_PPC_PLTREL32:
3687 case R_PPC_PLT16_LO:
3688 case R_PPC_PLT16_HI:
3689 case R_PPC_PLT16_HA:
3690 #ifdef DEBUG
3691 fprintf (stderr, "Reloc requires a PLT entry\n");
3692 #endif
3693 /* This symbol requires a procedure linkage table entry. We
3694 actually build the entry in finish_dynamic_symbol,
3695 because this might be a case of linking PIC code without
3696 linking in any dynamic objects, in which case we don't
3697 need to generate a procedure linkage table after all. */
3699 if (h == NULL)
3701 /* It does not make sense to have a procedure linkage
3702 table entry for a local symbol. */
3703 (*_bfd_error_handler) (_("%B(%A+0x%lx): %s reloc against "
3704 "local symbol"),
3705 abfd,
3706 sec,
3707 (long) rel->r_offset,
3708 ppc_elf_howto_table[r_type]->name);
3709 bfd_set_error (bfd_error_bad_value);
3710 return FALSE;
3712 else
3714 bfd_vma addend = 0;
3716 if (r_type == R_PPC_PLTREL24)
3718 ppc_elf_tdata (abfd)->makes_plt_call = 1;
3719 if (info->shared)
3720 addend = rel->r_addend;
3722 h->needs_plt = 1;
3723 if (!update_plt_info (abfd, &h->plt.plist, got2, addend))
3724 return FALSE;
3726 break;
3728 /* The following relocations don't need to propagate the
3729 relocation if linking a shared object since they are
3730 section relative. */
3731 case R_PPC_SECTOFF:
3732 case R_PPC_SECTOFF_LO:
3733 case R_PPC_SECTOFF_HI:
3734 case R_PPC_SECTOFF_HA:
3735 case R_PPC_DTPREL16:
3736 case R_PPC_DTPREL16_LO:
3737 case R_PPC_DTPREL16_HI:
3738 case R_PPC_DTPREL16_HA:
3739 case R_PPC_TOC16:
3740 break;
3742 case R_PPC_REL16:
3743 case R_PPC_REL16_LO:
3744 case R_PPC_REL16_HI:
3745 case R_PPC_REL16_HA:
3746 ppc_elf_tdata (abfd)->has_rel16 = 1;
3747 break;
3749 /* These are just markers. */
3750 case R_PPC_TLS:
3751 case R_PPC_EMB_MRKREF:
3752 case R_PPC_NONE:
3753 case R_PPC_max:
3754 case R_PPC_RELAX:
3755 case R_PPC_RELAX_PLT:
3756 case R_PPC_RELAX_PLTREL24:
3757 break;
3759 /* These should only appear in dynamic objects. */
3760 case R_PPC_COPY:
3761 case R_PPC_GLOB_DAT:
3762 case R_PPC_JMP_SLOT:
3763 case R_PPC_RELATIVE:
3764 case R_PPC_IRELATIVE:
3765 break;
3767 /* These aren't handled yet. We'll report an error later. */
3768 case R_PPC_ADDR30:
3769 case R_PPC_EMB_RELSEC16:
3770 case R_PPC_EMB_RELST_LO:
3771 case R_PPC_EMB_RELST_HI:
3772 case R_PPC_EMB_RELST_HA:
3773 case R_PPC_EMB_BIT_FLD:
3774 break;
3776 /* This refers only to functions defined in the shared library. */
3777 case R_PPC_LOCAL24PC:
3778 if (h != NULL && h == htab->elf.hgot && htab->plt_type == PLT_UNSET)
3780 htab->plt_type = PLT_OLD;
3781 htab->old_bfd = abfd;
3783 break;
3785 /* This relocation describes the C++ object vtable hierarchy.
3786 Reconstruct it for later use during GC. */
3787 case R_PPC_GNU_VTINHERIT:
3788 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
3789 return FALSE;
3790 break;
3792 /* This relocation describes which C++ vtable entries are actually
3793 used. Record for later use during GC. */
3794 case R_PPC_GNU_VTENTRY:
3795 BFD_ASSERT (h != NULL);
3796 if (h != NULL
3797 && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
3798 return FALSE;
3799 break;
3801 /* We shouldn't really be seeing these. */
3802 case R_PPC_TPREL32:
3803 case R_PPC_TPREL16:
3804 case R_PPC_TPREL16_LO:
3805 case R_PPC_TPREL16_HI:
3806 case R_PPC_TPREL16_HA:
3807 if (!info->executable)
3808 info->flags |= DF_STATIC_TLS;
3809 goto dodyn;
3811 /* Nor these. */
3812 case R_PPC_DTPMOD32:
3813 case R_PPC_DTPREL32:
3814 goto dodyn;
3816 case R_PPC_REL32:
3817 if (h == NULL
3818 && got2 != NULL
3819 && (sec->flags & SEC_CODE) != 0
3820 && info->shared
3821 && htab->plt_type == PLT_UNSET)
3823 /* Old -fPIC gcc code has .long LCTOC1-LCFx just before
3824 the start of a function, which assembles to a REL32
3825 reference to .got2. If we detect one of these, then
3826 force the old PLT layout because the linker cannot
3827 reliably deduce the GOT pointer value needed for
3828 PLT call stubs. */
3829 asection *s;
3830 Elf_Internal_Sym *isym;
3832 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
3833 abfd, r_symndx);
3834 if (isym == NULL)
3835 return FALSE;
3837 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
3838 if (s == got2)
3840 htab->plt_type = PLT_OLD;
3841 htab->old_bfd = abfd;
3844 if (h == NULL || h == htab->elf.hgot)
3845 break;
3846 /* fall through */
3848 case R_PPC_ADDR32:
3849 case R_PPC_ADDR16:
3850 case R_PPC_ADDR16_LO:
3851 case R_PPC_ADDR16_HI:
3852 case R_PPC_ADDR16_HA:
3853 case R_PPC_UADDR32:
3854 case R_PPC_UADDR16:
3855 if (h != NULL && !info->shared)
3857 /* We may need a plt entry if the symbol turns out to be
3858 a function defined in a dynamic object. */
3859 if (!update_plt_info (abfd, &h->plt.plist, NULL, 0))
3860 return FALSE;
3862 /* We may need a copy reloc too. */
3863 h->non_got_ref = 1;
3864 h->pointer_equality_needed = 1;
3866 goto dodyn;
3868 case R_PPC_REL24:
3869 case R_PPC_REL14:
3870 case R_PPC_REL14_BRTAKEN:
3871 case R_PPC_REL14_BRNTAKEN:
3872 if (h == NULL)
3873 break;
3874 if (h == htab->elf.hgot)
3876 if (htab->plt_type == PLT_UNSET)
3878 htab->plt_type = PLT_OLD;
3879 htab->old_bfd = abfd;
3881 break;
3883 /* fall through */
3885 case R_PPC_ADDR24:
3886 case R_PPC_ADDR14:
3887 case R_PPC_ADDR14_BRTAKEN:
3888 case R_PPC_ADDR14_BRNTAKEN:
3889 if (h != NULL && !info->shared)
3891 /* We may need a plt entry if the symbol turns out to be
3892 a function defined in a dynamic object. */
3893 h->needs_plt = 1;
3894 if (!update_plt_info (abfd, &h->plt.plist, NULL, 0))
3895 return FALSE;
3896 break;
3899 dodyn:
3900 /* If we are creating a shared library, and this is a reloc
3901 against a global symbol, or a non PC relative reloc
3902 against a local symbol, then we need to copy the reloc
3903 into the shared library. However, if we are linking with
3904 -Bsymbolic, we do not need to copy a reloc against a
3905 global symbol which is defined in an object we are
3906 including in the link (i.e., DEF_REGULAR is set). At
3907 this point we have not seen all the input files, so it is
3908 possible that DEF_REGULAR is not set now but will be set
3909 later (it is never cleared). In case of a weak definition,
3910 DEF_REGULAR may be cleared later by a strong definition in
3911 a shared library. We account for that possibility below by
3912 storing information in the dyn_relocs field of the hash
3913 table entry. A similar situation occurs when creating
3914 shared libraries and symbol visibility changes render the
3915 symbol local.
3917 If on the other hand, we are creating an executable, we
3918 may need to keep relocations for symbols satisfied by a
3919 dynamic library if we manage to avoid copy relocs for the
3920 symbol. */
3921 if ((info->shared
3922 && (must_be_dyn_reloc (info, r_type)
3923 || (h != NULL
3924 && (! info->symbolic
3925 || h->root.type == bfd_link_hash_defweak
3926 || !h->def_regular))))
3927 || (ELIMINATE_COPY_RELOCS
3928 && !info->shared
3929 && h != NULL
3930 && (h->root.type == bfd_link_hash_defweak
3931 || !h->def_regular)))
3933 struct ppc_elf_dyn_relocs *p;
3934 struct ppc_elf_dyn_relocs **rel_head;
3936 #ifdef DEBUG
3937 fprintf (stderr,
3938 "ppc_elf_check_relocs needs to "
3939 "create relocation for %s\n",
3940 (h && h->root.root.string
3941 ? h->root.root.string : "<unknown>"));
3942 #endif
3943 if (sreloc == NULL)
3945 if (htab->elf.dynobj == NULL)
3946 htab->elf.dynobj = abfd;
3948 sreloc = _bfd_elf_make_dynamic_reloc_section
3949 (sec, htab->elf.dynobj, 2, abfd, /*rela?*/ TRUE);
3951 if (sreloc == NULL)
3952 return FALSE;
3955 /* If this is a global symbol, we count the number of
3956 relocations we need for this symbol. */
3957 if (h != NULL)
3959 rel_head = &ppc_elf_hash_entry (h)->dyn_relocs;
3961 else
3963 /* Track dynamic relocs needed for local syms too.
3964 We really need local syms available to do this
3965 easily. Oh well. */
3966 asection *s;
3967 void *vpp;
3968 Elf_Internal_Sym *isym;
3970 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
3971 abfd, r_symndx);
3972 if (isym == NULL)
3973 return FALSE;
3975 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
3976 if (s == NULL)
3977 s = sec;
3979 vpp = &elf_section_data (s)->local_dynrel;
3980 rel_head = (struct ppc_elf_dyn_relocs **) vpp;
3983 p = *rel_head;
3984 if (p == NULL || p->sec != sec)
3986 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
3987 if (p == NULL)
3988 return FALSE;
3989 p->next = *rel_head;
3990 *rel_head = p;
3991 p->sec = sec;
3992 p->count = 0;
3993 p->pc_count = 0;
3996 p->count += 1;
3997 if (!must_be_dyn_reloc (info, r_type))
3998 p->pc_count += 1;
4001 break;
4005 return TRUE;
4009 /* Merge object attributes from IBFD into OBFD. Raise an error if
4010 there are conflicting attributes. */
4011 static bfd_boolean
4012 ppc_elf_merge_obj_attributes (bfd *ibfd, bfd *obfd)
4014 obj_attribute *in_attr, *in_attrs;
4015 obj_attribute *out_attr, *out_attrs;
4017 if (!elf_known_obj_attributes_proc (obfd)[0].i)
4019 /* This is the first object. Copy the attributes. */
4020 _bfd_elf_copy_obj_attributes (ibfd, obfd);
4022 /* Use the Tag_null value to indicate the attributes have been
4023 initialized. */
4024 elf_known_obj_attributes_proc (obfd)[0].i = 1;
4026 return TRUE;
4029 in_attrs = elf_known_obj_attributes (ibfd)[OBJ_ATTR_GNU];
4030 out_attrs = elf_known_obj_attributes (obfd)[OBJ_ATTR_GNU];
4032 /* Check for conflicting Tag_GNU_Power_ABI_FP attributes and merge
4033 non-conflicting ones. */
4034 in_attr = &in_attrs[Tag_GNU_Power_ABI_FP];
4035 out_attr = &out_attrs[Tag_GNU_Power_ABI_FP];
4036 if (in_attr->i != out_attr->i)
4038 out_attr->type = 1;
4039 if (out_attr->i == 0)
4040 out_attr->i = in_attr->i;
4041 else if (in_attr->i == 0)
4043 else if (out_attr->i == 1 && in_attr->i == 2)
4044 _bfd_error_handler
4045 (_("Warning: %B uses hard float, %B uses soft float"), obfd, ibfd);
4046 else if (out_attr->i == 1 && in_attr->i == 3)
4047 _bfd_error_handler
4048 (_("Warning: %B uses double-precision hard float, %B uses single-precision hard float"),
4049 obfd, ibfd);
4050 else if (out_attr->i == 3 && in_attr->i == 1)
4051 _bfd_error_handler
4052 (_("Warning: %B uses double-precision hard float, %B uses single-precision hard float"),
4053 ibfd, obfd);
4054 else if (out_attr->i == 3 && in_attr->i == 2)
4055 _bfd_error_handler
4056 (_("Warning: %B uses soft float, %B uses single-precision hard float"),
4057 ibfd, obfd);
4058 else if (out_attr->i == 2 && (in_attr->i == 1 || in_attr->i == 3))
4059 _bfd_error_handler
4060 (_("Warning: %B uses hard float, %B uses soft float"), ibfd, obfd);
4061 else if (in_attr->i > 3)
4062 _bfd_error_handler
4063 (_("Warning: %B uses unknown floating point ABI %d"), ibfd,
4064 in_attr->i);
4065 else
4066 _bfd_error_handler
4067 (_("Warning: %B uses unknown floating point ABI %d"), obfd,
4068 out_attr->i);
4071 /* Check for conflicting Tag_GNU_Power_ABI_Vector attributes and
4072 merge non-conflicting ones. */
4073 in_attr = &in_attrs[Tag_GNU_Power_ABI_Vector];
4074 out_attr = &out_attrs[Tag_GNU_Power_ABI_Vector];
4075 if (in_attr->i != out_attr->i)
4077 const char *in_abi = NULL, *out_abi = NULL;
4079 switch (in_attr->i)
4081 case 1: in_abi = "generic"; break;
4082 case 2: in_abi = "AltiVec"; break;
4083 case 3: in_abi = "SPE"; break;
4086 switch (out_attr->i)
4088 case 1: out_abi = "generic"; break;
4089 case 2: out_abi = "AltiVec"; break;
4090 case 3: out_abi = "SPE"; break;
4093 out_attr->type = 1;
4094 if (out_attr->i == 0)
4095 out_attr->i = in_attr->i;
4096 else if (in_attr->i == 0)
4098 /* For now, allow generic to transition to AltiVec or SPE
4099 without a warning. If GCC marked files with their stack
4100 alignment and used don't-care markings for files which are
4101 not affected by the vector ABI, we could warn about this
4102 case too. */
4103 else if (out_attr->i == 1)
4104 out_attr->i = in_attr->i;
4105 else if (in_attr->i == 1)
4107 else if (in_abi == NULL)
4108 _bfd_error_handler
4109 (_("Warning: %B uses unknown vector ABI %d"), ibfd,
4110 in_attr->i);
4111 else if (out_abi == NULL)
4112 _bfd_error_handler
4113 (_("Warning: %B uses unknown vector ABI %d"), obfd,
4114 in_attr->i);
4115 else
4116 _bfd_error_handler
4117 (_("Warning: %B uses vector ABI \"%s\", %B uses \"%s\""),
4118 ibfd, obfd, in_abi, out_abi);
4121 /* Check for conflicting Tag_GNU_Power_ABI_Struct_Return attributes
4122 and merge non-conflicting ones. */
4123 in_attr = &in_attrs[Tag_GNU_Power_ABI_Struct_Return];
4124 out_attr = &out_attrs[Tag_GNU_Power_ABI_Struct_Return];
4125 if (in_attr->i != out_attr->i)
4127 out_attr->type = 1;
4128 if (out_attr->i == 0)
4129 out_attr->i = in_attr->i;
4130 else if (in_attr->i == 0)
4132 else if (out_attr->i == 1 && in_attr->i == 2)
4133 _bfd_error_handler
4134 (_("Warning: %B uses r3/r4 for small structure returns, %B uses memory"), obfd, ibfd);
4135 else if (out_attr->i == 2 && in_attr->i == 1)
4136 _bfd_error_handler
4137 (_("Warning: %B uses r3/r4 for small structure returns, %B uses memory"), ibfd, obfd);
4138 else if (in_attr->i > 2)
4139 _bfd_error_handler
4140 (_("Warning: %B uses unknown small structure return convention %d"), ibfd,
4141 in_attr->i);
4142 else
4143 _bfd_error_handler
4144 (_("Warning: %B uses unknown small structure return convention %d"), obfd,
4145 out_attr->i);
4148 /* Merge Tag_compatibility attributes and any common GNU ones. */
4149 _bfd_elf_merge_object_attributes (ibfd, obfd);
4151 return TRUE;
4154 /* Merge backend specific data from an object file to the output
4155 object file when linking. */
4157 static bfd_boolean
4158 ppc_elf_merge_private_bfd_data (bfd *ibfd, bfd *obfd)
4160 flagword old_flags;
4161 flagword new_flags;
4162 bfd_boolean error;
4164 if (!is_ppc_elf (ibfd) || !is_ppc_elf (obfd))
4165 return TRUE;
4167 /* Check if we have the same endianess. */
4168 if (! _bfd_generic_verify_endian_match (ibfd, obfd))
4169 return FALSE;
4171 if (!ppc_elf_merge_obj_attributes (ibfd, obfd))
4172 return FALSE;
4174 new_flags = elf_elfheader (ibfd)->e_flags;
4175 old_flags = elf_elfheader (obfd)->e_flags;
4176 if (!elf_flags_init (obfd))
4178 /* First call, no flags set. */
4179 elf_flags_init (obfd) = TRUE;
4180 elf_elfheader (obfd)->e_flags = new_flags;
4183 /* Compatible flags are ok. */
4184 else if (new_flags == old_flags)
4187 /* Incompatible flags. */
4188 else
4190 /* Warn about -mrelocatable mismatch. Allow -mrelocatable-lib
4191 to be linked with either. */
4192 error = FALSE;
4193 if ((new_flags & EF_PPC_RELOCATABLE) != 0
4194 && (old_flags & (EF_PPC_RELOCATABLE | EF_PPC_RELOCATABLE_LIB)) == 0)
4196 error = TRUE;
4197 (*_bfd_error_handler)
4198 (_("%B: compiled with -mrelocatable and linked with "
4199 "modules compiled normally"), ibfd);
4201 else if ((new_flags & (EF_PPC_RELOCATABLE | EF_PPC_RELOCATABLE_LIB)) == 0
4202 && (old_flags & EF_PPC_RELOCATABLE) != 0)
4204 error = TRUE;
4205 (*_bfd_error_handler)
4206 (_("%B: compiled normally and linked with "
4207 "modules compiled with -mrelocatable"), ibfd);
4210 /* The output is -mrelocatable-lib iff both the input files are. */
4211 if (! (new_flags & EF_PPC_RELOCATABLE_LIB))
4212 elf_elfheader (obfd)->e_flags &= ~EF_PPC_RELOCATABLE_LIB;
4214 /* The output is -mrelocatable iff it can't be -mrelocatable-lib,
4215 but each input file is either -mrelocatable or -mrelocatable-lib. */
4216 if (! (elf_elfheader (obfd)->e_flags & EF_PPC_RELOCATABLE_LIB)
4217 && (new_flags & (EF_PPC_RELOCATABLE_LIB | EF_PPC_RELOCATABLE))
4218 && (old_flags & (EF_PPC_RELOCATABLE_LIB | EF_PPC_RELOCATABLE)))
4219 elf_elfheader (obfd)->e_flags |= EF_PPC_RELOCATABLE;
4221 /* Do not warn about eabi vs. V.4 mismatch, just or in the bit if
4222 any module uses it. */
4223 elf_elfheader (obfd)->e_flags |= (new_flags & EF_PPC_EMB);
4225 new_flags &= ~(EF_PPC_RELOCATABLE | EF_PPC_RELOCATABLE_LIB | EF_PPC_EMB);
4226 old_flags &= ~(EF_PPC_RELOCATABLE | EF_PPC_RELOCATABLE_LIB | EF_PPC_EMB);
4228 /* Warn about any other mismatches. */
4229 if (new_flags != old_flags)
4231 error = TRUE;
4232 (*_bfd_error_handler)
4233 (_("%B: uses different e_flags (0x%lx) fields "
4234 "than previous modules (0x%lx)"),
4235 ibfd, (long) new_flags, (long) old_flags);
4238 if (error)
4240 bfd_set_error (bfd_error_bad_value);
4241 return FALSE;
4245 return TRUE;
4248 /* Choose which PLT scheme to use, and set .plt flags appropriately.
4249 Returns -1 on error, 0 for old PLT, 1 for new PLT. */
4251 ppc_elf_select_plt_layout (bfd *output_bfd ATTRIBUTE_UNUSED,
4252 struct bfd_link_info *info,
4253 enum ppc_elf_plt_type plt_style,
4254 int emit_stub_syms)
4256 struct ppc_elf_link_hash_table *htab;
4257 flagword flags;
4259 htab = ppc_elf_hash_table (info);
4261 htab->emit_stub_syms = emit_stub_syms;
4263 if (htab->plt_type == PLT_UNSET)
4265 if (plt_style == PLT_OLD)
4266 htab->plt_type = PLT_OLD;
4267 else
4269 bfd *ibfd;
4270 enum ppc_elf_plt_type plt_type = plt_style;
4272 /* Look through the reloc flags left by ppc_elf_check_relocs.
4273 Use the old style bss plt if a file makes plt calls
4274 without using the new relocs, and if ld isn't given
4275 --secure-plt and we never see REL16 relocs. */
4276 if (plt_type == PLT_UNSET)
4277 plt_type = PLT_OLD;
4278 for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link_next)
4279 if (is_ppc_elf (ibfd))
4281 if (ppc_elf_tdata (ibfd)->has_rel16)
4282 plt_type = PLT_NEW;
4283 else if (ppc_elf_tdata (ibfd)->makes_plt_call)
4285 plt_type = PLT_OLD;
4286 htab->old_bfd = ibfd;
4287 break;
4290 htab->plt_type = plt_type;
4293 if (htab->plt_type == PLT_OLD && plt_style == PLT_NEW)
4294 info->callbacks->info (_("Using bss-plt due to %B"), htab->old_bfd);
4296 BFD_ASSERT (htab->plt_type != PLT_VXWORKS);
4298 if (htab->plt_type == PLT_NEW)
4300 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
4301 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4303 /* The new PLT is a loaded section. */
4304 if (htab->plt != NULL
4305 && !bfd_set_section_flags (htab->elf.dynobj, htab->plt, flags))
4306 return -1;
4308 /* The new GOT is not executable. */
4309 if (htab->got != NULL
4310 && !bfd_set_section_flags (htab->elf.dynobj, htab->got, flags))
4311 return -1;
4313 else
4315 /* Stop an unused .glink section from affecting .text alignment. */
4316 if (htab->glink != NULL
4317 && !bfd_set_section_alignment (htab->elf.dynobj, htab->glink, 0))
4318 return -1;
4320 return htab->plt_type == PLT_NEW;
4323 /* Return the section that should be marked against GC for a given
4324 relocation. */
4326 static asection *
4327 ppc_elf_gc_mark_hook (asection *sec,
4328 struct bfd_link_info *info,
4329 Elf_Internal_Rela *rel,
4330 struct elf_link_hash_entry *h,
4331 Elf_Internal_Sym *sym)
4333 if (h != NULL)
4334 switch (ELF32_R_TYPE (rel->r_info))
4336 case R_PPC_GNU_VTINHERIT:
4337 case R_PPC_GNU_VTENTRY:
4338 return NULL;
4341 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
4344 /* Update the got, plt and dynamic reloc reference counts for the
4345 section being removed. */
4347 static bfd_boolean
4348 ppc_elf_gc_sweep_hook (bfd *abfd,
4349 struct bfd_link_info *info,
4350 asection *sec,
4351 const Elf_Internal_Rela *relocs)
4353 struct ppc_elf_link_hash_table *htab;
4354 Elf_Internal_Shdr *symtab_hdr;
4355 struct elf_link_hash_entry **sym_hashes;
4356 bfd_signed_vma *local_got_refcounts;
4357 const Elf_Internal_Rela *rel, *relend;
4358 asection *got2;
4360 if (info->relocatable)
4361 return TRUE;
4363 if ((sec->flags & SEC_ALLOC) == 0)
4364 return TRUE;
4366 elf_section_data (sec)->local_dynrel = NULL;
4368 htab = ppc_elf_hash_table (info);
4369 symtab_hdr = &elf_symtab_hdr (abfd);
4370 sym_hashes = elf_sym_hashes (abfd);
4371 local_got_refcounts = elf_local_got_refcounts (abfd);
4372 got2 = bfd_get_section_by_name (abfd, ".got2");
4374 relend = relocs + sec->reloc_count;
4375 for (rel = relocs; rel < relend; rel++)
4377 unsigned long r_symndx;
4378 enum elf_ppc_reloc_type r_type;
4379 struct elf_link_hash_entry *h = NULL;
4381 r_symndx = ELF32_R_SYM (rel->r_info);
4382 if (r_symndx >= symtab_hdr->sh_info)
4384 struct ppc_elf_dyn_relocs **pp, *p;
4385 struct ppc_elf_link_hash_entry *eh;
4387 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
4388 while (h->root.type == bfd_link_hash_indirect
4389 || h->root.type == bfd_link_hash_warning)
4390 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4391 eh = (struct ppc_elf_link_hash_entry *) h;
4393 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
4394 if (p->sec == sec)
4396 /* Everything must go for SEC. */
4397 *pp = p->next;
4398 break;
4402 r_type = ELF32_R_TYPE (rel->r_info);
4403 if (!htab->is_vxworks
4404 && h == NULL
4405 && local_got_refcounts != NULL
4406 && (!info->shared
4407 || is_branch_reloc (r_type)))
4409 struct plt_entry **local_plt = (struct plt_entry **)
4410 (local_got_refcounts + symtab_hdr->sh_info);
4411 char *local_got_tls_masks = (char *)
4412 (local_plt + symtab_hdr->sh_info);
4413 if ((local_got_tls_masks[r_symndx] & PLT_IFUNC) != 0)
4415 struct plt_entry **ifunc = local_plt + r_symndx;
4416 bfd_vma addend = 0;
4417 struct plt_entry *ent;
4419 if (r_type == R_PPC_PLTREL24 && info->shared)
4420 addend = rel->r_addend;
4421 ent = find_plt_ent (ifunc, got2, addend);
4422 if (ent->plt.refcount > 0)
4423 ent->plt.refcount -= 1;
4424 continue;
4428 switch (r_type)
4430 case R_PPC_GOT_TLSLD16:
4431 case R_PPC_GOT_TLSLD16_LO:
4432 case R_PPC_GOT_TLSLD16_HI:
4433 case R_PPC_GOT_TLSLD16_HA:
4434 case R_PPC_GOT_TLSGD16:
4435 case R_PPC_GOT_TLSGD16_LO:
4436 case R_PPC_GOT_TLSGD16_HI:
4437 case R_PPC_GOT_TLSGD16_HA:
4438 case R_PPC_GOT_TPREL16:
4439 case R_PPC_GOT_TPREL16_LO:
4440 case R_PPC_GOT_TPREL16_HI:
4441 case R_PPC_GOT_TPREL16_HA:
4442 case R_PPC_GOT_DTPREL16:
4443 case R_PPC_GOT_DTPREL16_LO:
4444 case R_PPC_GOT_DTPREL16_HI:
4445 case R_PPC_GOT_DTPREL16_HA:
4446 case R_PPC_GOT16:
4447 case R_PPC_GOT16_LO:
4448 case R_PPC_GOT16_HI:
4449 case R_PPC_GOT16_HA:
4450 if (h != NULL)
4452 if (h->got.refcount > 0)
4453 h->got.refcount--;
4454 if (!info->shared)
4456 struct plt_entry *ent;
4458 ent = find_plt_ent (&h->plt.plist, NULL, 0);
4459 if (ent != NULL && ent->plt.refcount > 0)
4460 ent->plt.refcount -= 1;
4463 else if (local_got_refcounts != NULL)
4465 if (local_got_refcounts[r_symndx] > 0)
4466 local_got_refcounts[r_symndx]--;
4468 break;
4470 case R_PPC_REL24:
4471 case R_PPC_REL14:
4472 case R_PPC_REL14_BRTAKEN:
4473 case R_PPC_REL14_BRNTAKEN:
4474 case R_PPC_REL32:
4475 if (h == NULL || h == htab->elf.hgot)
4476 break;
4477 /* Fall thru */
4479 case R_PPC_ADDR32:
4480 case R_PPC_ADDR24:
4481 case R_PPC_ADDR16:
4482 case R_PPC_ADDR16_LO:
4483 case R_PPC_ADDR16_HI:
4484 case R_PPC_ADDR16_HA:
4485 case R_PPC_ADDR14:
4486 case R_PPC_ADDR14_BRTAKEN:
4487 case R_PPC_ADDR14_BRNTAKEN:
4488 case R_PPC_UADDR32:
4489 case R_PPC_UADDR16:
4490 if (info->shared)
4491 break;
4493 case R_PPC_PLT32:
4494 case R_PPC_PLTREL24:
4495 case R_PPC_PLTREL32:
4496 case R_PPC_PLT16_LO:
4497 case R_PPC_PLT16_HI:
4498 case R_PPC_PLT16_HA:
4499 if (h != NULL)
4501 bfd_vma addend = 0;
4502 struct plt_entry *ent;
4504 if (r_type == R_PPC_PLTREL24 && info->shared)
4505 addend = rel->r_addend;
4506 ent = find_plt_ent (&h->plt.plist, got2, addend);
4507 if (ent != NULL && ent->plt.refcount > 0)
4508 ent->plt.refcount -= 1;
4510 break;
4512 default:
4513 break;
4516 return TRUE;
4519 /* Set plt output section type, htab->tls_get_addr, and call the
4520 generic ELF tls_setup function. */
4522 asection *
4523 ppc_elf_tls_setup (bfd *obfd,
4524 struct bfd_link_info *info,
4525 int no_tls_get_addr_opt)
4527 struct ppc_elf_link_hash_table *htab;
4529 htab = ppc_elf_hash_table (info);
4530 htab->tls_get_addr = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
4531 FALSE, FALSE, TRUE);
4532 if (!no_tls_get_addr_opt)
4534 struct elf_link_hash_entry *opt, *tga;
4535 opt = elf_link_hash_lookup (&htab->elf, "__tls_get_addr_opt",
4536 FALSE, FALSE, TRUE);
4537 if (opt != NULL
4538 && (opt->root.type == bfd_link_hash_defined
4539 || opt->root.type == bfd_link_hash_defweak))
4541 /* If glibc supports an optimized __tls_get_addr call stub,
4542 signalled by the presence of __tls_get_addr_opt, and we'll
4543 be calling __tls_get_addr via a plt call stub, then
4544 make __tls_get_addr point to __tls_get_addr_opt. */
4545 tga = htab->tls_get_addr;
4546 if (htab->elf.dynamic_sections_created
4547 && tga != NULL
4548 && (tga->type == STT_FUNC
4549 || tga->needs_plt)
4550 && !(SYMBOL_CALLS_LOCAL (info, tga)
4551 || (ELF_ST_VISIBILITY (tga->other) != STV_DEFAULT
4552 && tga->root.type == bfd_link_hash_undefweak)))
4554 struct plt_entry *ent;
4555 for (ent = tga->plt.plist; ent != NULL; ent = ent->next)
4556 if (ent->plt.refcount > 0)
4557 break;
4558 if (ent != NULL)
4560 tga->root.type = bfd_link_hash_indirect;
4561 tga->root.u.i.link = &opt->root;
4562 ppc_elf_copy_indirect_symbol (info, opt, tga);
4563 if (opt->dynindx != -1)
4565 /* Use __tls_get_addr_opt in dynamic relocations. */
4566 opt->dynindx = -1;
4567 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
4568 opt->dynstr_index);
4569 if (!bfd_elf_link_record_dynamic_symbol (info, opt))
4570 return FALSE;
4572 htab->tls_get_addr = opt;
4576 else
4577 no_tls_get_addr_opt = TRUE;
4579 htab->no_tls_get_addr_opt = no_tls_get_addr_opt;
4580 if (htab->plt_type == PLT_NEW
4581 && htab->plt != NULL
4582 && htab->plt->output_section != NULL)
4584 elf_section_type (htab->plt->output_section) = SHT_PROGBITS;
4585 elf_section_flags (htab->plt->output_section) = SHF_ALLOC + SHF_WRITE;
4588 return _bfd_elf_tls_setup (obfd, info);
4591 /* Return TRUE iff REL is a branch reloc with a global symbol matching
4592 HASH. */
4594 static bfd_boolean
4595 branch_reloc_hash_match (const bfd *ibfd,
4596 const Elf_Internal_Rela *rel,
4597 const struct elf_link_hash_entry *hash)
4599 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
4600 enum elf_ppc_reloc_type r_type = ELF32_R_TYPE (rel->r_info);
4601 unsigned int r_symndx = ELF32_R_SYM (rel->r_info);
4603 if (r_symndx >= symtab_hdr->sh_info && is_branch_reloc (r_type))
4605 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
4606 struct elf_link_hash_entry *h;
4608 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
4609 while (h->root.type == bfd_link_hash_indirect
4610 || h->root.type == bfd_link_hash_warning)
4611 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4612 if (h == hash)
4613 return TRUE;
4615 return FALSE;
4618 /* Run through all the TLS relocs looking for optimization
4619 opportunities. */
4621 bfd_boolean
4622 ppc_elf_tls_optimize (bfd *obfd ATTRIBUTE_UNUSED,
4623 struct bfd_link_info *info)
4625 bfd *ibfd;
4626 asection *sec;
4627 struct ppc_elf_link_hash_table *htab;
4628 int pass;
4630 if (info->relocatable || !info->executable)
4631 return TRUE;
4633 htab = ppc_elf_hash_table (info);
4634 if (htab == NULL)
4635 return FALSE;
4637 /* Make two passes through the relocs. First time check that tls
4638 relocs involved in setting up a tls_get_addr call are indeed
4639 followed by such a call. If they are not, don't do any tls
4640 optimization. On the second pass twiddle tls_mask flags to
4641 notify relocate_section that optimization can be done, and
4642 adjust got and plt refcounts. */
4643 for (pass = 0; pass < 2; ++pass)
4644 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
4646 Elf_Internal_Sym *locsyms = NULL;
4647 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
4648 asection *got2 = bfd_get_section_by_name (ibfd, ".got2");
4650 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
4651 if (sec->has_tls_reloc && !bfd_is_abs_section (sec->output_section))
4653 Elf_Internal_Rela *relstart, *rel, *relend;
4654 int expecting_tls_get_addr = 0;
4656 /* Read the relocations. */
4657 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
4658 info->keep_memory);
4659 if (relstart == NULL)
4660 return FALSE;
4662 relend = relstart + sec->reloc_count;
4663 for (rel = relstart; rel < relend; rel++)
4665 enum elf_ppc_reloc_type r_type;
4666 unsigned long r_symndx;
4667 struct elf_link_hash_entry *h = NULL;
4668 char *tls_mask;
4669 char tls_set, tls_clear;
4670 bfd_boolean is_local;
4671 bfd_signed_vma *got_count;
4673 r_symndx = ELF32_R_SYM (rel->r_info);
4674 if (r_symndx >= symtab_hdr->sh_info)
4676 struct elf_link_hash_entry **sym_hashes;
4678 sym_hashes = elf_sym_hashes (ibfd);
4679 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
4680 while (h->root.type == bfd_link_hash_indirect
4681 || h->root.type == bfd_link_hash_warning)
4682 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4685 is_local = FALSE;
4686 if (h == NULL
4687 || !h->def_dynamic)
4688 is_local = TRUE;
4690 r_type = ELF32_R_TYPE (rel->r_info);
4691 /* If this section has old-style __tls_get_addr calls
4692 without marker relocs, then check that each
4693 __tls_get_addr call reloc is preceded by a reloc
4694 that conceivably belongs to the __tls_get_addr arg
4695 setup insn. If we don't find matching arg setup
4696 relocs, don't do any tls optimization. */
4697 if (pass == 0
4698 && sec->has_tls_get_addr_call
4699 && h != NULL
4700 && h == htab->tls_get_addr
4701 && !expecting_tls_get_addr
4702 && is_branch_reloc (r_type))
4704 info->callbacks->minfo ("%C __tls_get_addr lost arg, "
4705 "TLS optimization disabled\n",
4706 ibfd, sec, rel->r_offset);
4707 if (elf_section_data (sec)->relocs != relstart)
4708 free (relstart);
4709 return TRUE;
4712 expecting_tls_get_addr = 0;
4713 switch (r_type)
4715 case R_PPC_GOT_TLSLD16:
4716 case R_PPC_GOT_TLSLD16_LO:
4717 expecting_tls_get_addr = 1;
4718 /* Fall thru */
4720 case R_PPC_GOT_TLSLD16_HI:
4721 case R_PPC_GOT_TLSLD16_HA:
4722 /* These relocs should never be against a symbol
4723 defined in a shared lib. Leave them alone if
4724 that turns out to be the case. */
4725 if (!is_local)
4726 continue;
4728 /* LD -> LE */
4729 tls_set = 0;
4730 tls_clear = TLS_LD;
4731 break;
4733 case R_PPC_GOT_TLSGD16:
4734 case R_PPC_GOT_TLSGD16_LO:
4735 expecting_tls_get_addr = 1;
4736 /* Fall thru */
4738 case R_PPC_GOT_TLSGD16_HI:
4739 case R_PPC_GOT_TLSGD16_HA:
4740 if (is_local)
4741 /* GD -> LE */
4742 tls_set = 0;
4743 else
4744 /* GD -> IE */
4745 tls_set = TLS_TLS | TLS_TPRELGD;
4746 tls_clear = TLS_GD;
4747 break;
4749 case R_PPC_GOT_TPREL16:
4750 case R_PPC_GOT_TPREL16_LO:
4751 case R_PPC_GOT_TPREL16_HI:
4752 case R_PPC_GOT_TPREL16_HA:
4753 if (is_local)
4755 /* IE -> LE */
4756 tls_set = 0;
4757 tls_clear = TLS_TPREL;
4758 break;
4760 else
4761 continue;
4763 case R_PPC_TLSGD:
4764 case R_PPC_TLSLD:
4765 expecting_tls_get_addr = 2;
4766 tls_set = 0;
4767 tls_clear = 0;
4768 break;
4770 default:
4771 continue;
4774 if (pass == 0)
4776 if (!expecting_tls_get_addr
4777 || (expecting_tls_get_addr == 1
4778 && !sec->has_tls_get_addr_call))
4779 continue;
4781 if (rel + 1 < relend
4782 && branch_reloc_hash_match (ibfd, rel + 1,
4783 htab->tls_get_addr))
4784 continue;
4786 /* Uh oh, we didn't find the expected call. We
4787 could just mark this symbol to exclude it
4788 from tls optimization but it's safer to skip
4789 the entire optimization. */
4790 info->callbacks->minfo (_("%C arg lost __tls_get_addr, "
4791 "TLS optimization disabled\n"),
4792 ibfd, sec, rel->r_offset);
4793 if (elf_section_data (sec)->relocs != relstart)
4794 free (relstart);
4795 return TRUE;
4798 if (expecting_tls_get_addr)
4800 struct plt_entry *ent;
4801 bfd_vma addend = 0;
4803 if (info->shared
4804 && ELF32_R_TYPE (rel[1].r_info) == R_PPC_PLTREL24)
4805 addend = rel[1].r_addend;
4806 ent = find_plt_ent (&htab->tls_get_addr->plt.plist,
4807 got2, addend);
4808 if (ent != NULL && ent->plt.refcount > 0)
4809 ent->plt.refcount -= 1;
4811 if (expecting_tls_get_addr == 2)
4812 continue;
4815 if (h != NULL)
4817 tls_mask = &ppc_elf_hash_entry (h)->tls_mask;
4818 got_count = &h->got.refcount;
4820 else
4822 bfd_signed_vma *lgot_refs;
4823 struct plt_entry **local_plt;
4824 char *lgot_masks;
4826 if (locsyms == NULL)
4828 locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
4829 if (locsyms == NULL)
4830 locsyms = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
4831 symtab_hdr->sh_info,
4832 0, NULL, NULL, NULL);
4833 if (locsyms == NULL)
4835 if (elf_section_data (sec)->relocs != relstart)
4836 free (relstart);
4837 return FALSE;
4840 lgot_refs = elf_local_got_refcounts (ibfd);
4841 if (lgot_refs == NULL)
4842 abort ();
4843 local_plt = (struct plt_entry **)
4844 (lgot_refs + symtab_hdr->sh_info);
4845 lgot_masks = (char *) (local_plt + symtab_hdr->sh_info);
4846 tls_mask = &lgot_masks[r_symndx];
4847 got_count = &lgot_refs[r_symndx];
4850 if (tls_set == 0)
4852 /* We managed to get rid of a got entry. */
4853 if (*got_count > 0)
4854 *got_count -= 1;
4857 *tls_mask |= tls_set;
4858 *tls_mask &= ~tls_clear;
4861 if (elf_section_data (sec)->relocs != relstart)
4862 free (relstart);
4865 if (locsyms != NULL
4866 && (symtab_hdr->contents != (unsigned char *) locsyms))
4868 if (!info->keep_memory)
4869 free (locsyms);
4870 else
4871 symtab_hdr->contents = (unsigned char *) locsyms;
4874 return TRUE;
4877 /* Return true if we have dynamic relocs that apply to read-only sections. */
4879 static bfd_boolean
4880 readonly_dynrelocs (struct elf_link_hash_entry *h)
4882 struct ppc_elf_dyn_relocs *p;
4884 for (p = ppc_elf_hash_entry (h)->dyn_relocs; p != NULL; p = p->next)
4886 asection *s = p->sec->output_section;
4888 if (s != NULL
4889 && ((s->flags & (SEC_READONLY | SEC_ALLOC))
4890 == (SEC_READONLY | SEC_ALLOC)))
4891 return TRUE;
4893 return FALSE;
4896 /* Adjust a symbol defined by a dynamic object and referenced by a
4897 regular object. The current definition is in some section of the
4898 dynamic object, but we're not including those sections. We have to
4899 change the definition to something the rest of the link can
4900 understand. */
4902 static bfd_boolean
4903 ppc_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
4904 struct elf_link_hash_entry *h)
4906 struct ppc_elf_link_hash_table *htab;
4907 asection *s;
4909 #ifdef DEBUG
4910 fprintf (stderr, "ppc_elf_adjust_dynamic_symbol called for %s\n",
4911 h->root.root.string);
4912 #endif
4914 /* Make sure we know what is going on here. */
4915 htab = ppc_elf_hash_table (info);
4916 BFD_ASSERT (htab->elf.dynobj != NULL
4917 && (h->needs_plt
4918 || h->type == STT_GNU_IFUNC
4919 || h->u.weakdef != NULL
4920 || (h->def_dynamic
4921 && h->ref_regular
4922 && !h->def_regular)));
4924 /* Deal with function syms. */
4925 if (h->type == STT_FUNC
4926 || h->type == STT_GNU_IFUNC
4927 || h->needs_plt)
4929 /* Clear procedure linkage table information for any symbol that
4930 won't need a .plt entry. */
4931 struct plt_entry *ent;
4932 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
4933 if (ent->plt.refcount > 0)
4934 break;
4935 if (ent == NULL
4936 || (h->type != STT_GNU_IFUNC
4937 && (SYMBOL_CALLS_LOCAL (info, h)
4938 || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
4939 && h->root.type == bfd_link_hash_undefweak))))
4941 /* A PLT entry is not required/allowed when:
4943 1. We are not using ld.so; because then the PLT entry
4944 can't be set up, so we can't use one. In this case,
4945 ppc_elf_adjust_dynamic_symbol won't even be called.
4947 2. GC has rendered the entry unused.
4949 3. We know for certain that a call to this symbol
4950 will go to this object, or will remain undefined. */
4951 h->plt.plist = NULL;
4952 h->needs_plt = 0;
4954 else
4956 /* After adjust_dynamic_symbol, non_got_ref set in the
4957 non-shared case means that we have allocated space in
4958 .dynbss for the symbol and thus dyn_relocs for this
4959 symbol should be discarded.
4960 If we get here we know we are making a PLT entry for this
4961 symbol, and in an executable we'd normally resolve
4962 relocations against this symbol to the PLT entry. Allow
4963 dynamic relocs if the reference is weak, and the dynamic
4964 relocs will not cause text relocation. */
4965 if (!h->ref_regular_nonweak
4966 && h->non_got_ref
4967 && h->type != STT_GNU_IFUNC
4968 && !htab->is_vxworks
4969 && !ppc_elf_hash_entry (h)->has_sda_refs
4970 && !readonly_dynrelocs (h))
4971 h->non_got_ref = 0;
4973 return TRUE;
4975 else
4976 h->plt.plist = NULL;
4978 /* If this is a weak symbol, and there is a real definition, the
4979 processor independent code will have arranged for us to see the
4980 real definition first, and we can just use the same value. */
4981 if (h->u.weakdef != NULL)
4983 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
4984 || h->u.weakdef->root.type == bfd_link_hash_defweak);
4985 h->root.u.def.section = h->u.weakdef->root.u.def.section;
4986 h->root.u.def.value = h->u.weakdef->root.u.def.value;
4987 if (ELIMINATE_COPY_RELOCS)
4988 h->non_got_ref = h->u.weakdef->non_got_ref;
4989 return TRUE;
4992 /* This is a reference to a symbol defined by a dynamic object which
4993 is not a function. */
4995 /* If we are creating a shared library, we must presume that the
4996 only references to the symbol are via the global offset table.
4997 For such cases we need not do anything here; the relocations will
4998 be handled correctly by relocate_section. */
4999 if (info->shared)
5000 return TRUE;
5002 /* If there are no references to this symbol that do not use the
5003 GOT, we don't need to generate a copy reloc. */
5004 if (!h->non_got_ref)
5005 return TRUE;
5007 /* If we didn't find any dynamic relocs in read-only sections, then
5008 we'll be keeping the dynamic relocs and avoiding the copy reloc.
5009 We can't do this if there are any small data relocations. This
5010 doesn't work on VxWorks, where we can not have dynamic
5011 relocations (other than copy and jump slot relocations) in an
5012 executable. */
5013 if (ELIMINATE_COPY_RELOCS
5014 && !ppc_elf_hash_entry (h)->has_sda_refs
5015 && !htab->is_vxworks
5016 && !h->def_regular
5017 && !readonly_dynrelocs (h))
5019 h->non_got_ref = 0;
5020 return TRUE;
5023 if (h->size == 0)
5025 (*_bfd_error_handler) (_("dynamic variable `%s' is zero size"),
5026 h->root.root.string);
5027 return TRUE;
5030 /* We must allocate the symbol in our .dynbss section, which will
5031 become part of the .bss section of the executable. There will be
5032 an entry for this symbol in the .dynsym section. The dynamic
5033 object will contain position independent code, so all references
5034 from the dynamic object to this symbol will go through the global
5035 offset table. The dynamic linker will use the .dynsym entry to
5036 determine the address it must put in the global offset table, so
5037 both the dynamic object and the regular object will refer to the
5038 same memory location for the variable.
5040 Of course, if the symbol is referenced using SDAREL relocs, we
5041 must instead allocate it in .sbss. */
5043 if (ppc_elf_hash_entry (h)->has_sda_refs)
5044 s = htab->dynsbss;
5045 else
5046 s = htab->dynbss;
5047 BFD_ASSERT (s != NULL);
5049 /* We must generate a R_PPC_COPY reloc to tell the dynamic linker to
5050 copy the initial value out of the dynamic object and into the
5051 runtime process image. We need to remember the offset into the
5052 .rela.bss section we are going to use. */
5053 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
5055 asection *srel;
5057 if (ppc_elf_hash_entry (h)->has_sda_refs)
5058 srel = htab->relsbss;
5059 else
5060 srel = htab->relbss;
5061 BFD_ASSERT (srel != NULL);
5062 srel->size += sizeof (Elf32_External_Rela);
5063 h->needs_copy = 1;
5066 return _bfd_elf_adjust_dynamic_copy (h, s);
5069 /* Generate a symbol to mark plt call stubs. For non-PIC code the sym is
5070 xxxxxxxx.plt_call32.<callee> where xxxxxxxx is a hex number, usually 0,
5071 specifying the addend on the plt relocation. For -fpic code, the sym
5072 is xxxxxxxx.plt_pic32.<callee>, and for -fPIC
5073 xxxxxxxx.got2.plt_pic32.<callee>. */
5075 static bfd_boolean
5076 add_stub_sym (struct plt_entry *ent,
5077 struct elf_link_hash_entry *h,
5078 struct bfd_link_info *info)
5080 struct elf_link_hash_entry *sh;
5081 size_t len1, len2, len3;
5082 char *name;
5083 const char *stub;
5084 struct ppc_elf_link_hash_table *htab = ppc_elf_hash_table (info);
5086 if (info->shared)
5087 stub = ".plt_pic32.";
5088 else
5089 stub = ".plt_call32.";
5091 len1 = strlen (h->root.root.string);
5092 len2 = strlen (stub);
5093 len3 = 0;
5094 if (ent->sec)
5095 len3 = strlen (ent->sec->name);
5096 name = bfd_malloc (len1 + len2 + len3 + 9);
5097 if (name == NULL)
5098 return FALSE;
5099 sprintf (name, "%08x", (unsigned) ent->addend & 0xffffffff);
5100 if (ent->sec)
5101 memcpy (name + 8, ent->sec->name, len3);
5102 memcpy (name + 8 + len3, stub, len2);
5103 memcpy (name + 8 + len3 + len2, h->root.root.string, len1 + 1);
5104 sh = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
5105 if (sh == NULL)
5106 return FALSE;
5107 if (sh->root.type == bfd_link_hash_new)
5109 sh->root.type = bfd_link_hash_defined;
5110 sh->root.u.def.section = htab->glink;
5111 sh->root.u.def.value = ent->glink_offset;
5112 sh->ref_regular = 1;
5113 sh->def_regular = 1;
5114 sh->ref_regular_nonweak = 1;
5115 sh->forced_local = 1;
5116 sh->non_elf = 0;
5118 return TRUE;
5121 /* Allocate NEED contiguous space in .got, and return the offset.
5122 Handles allocation of the got header when crossing 32k. */
5124 static bfd_vma
5125 allocate_got (struct ppc_elf_link_hash_table *htab, unsigned int need)
5127 bfd_vma where;
5128 unsigned int max_before_header;
5130 if (htab->plt_type == PLT_VXWORKS)
5132 where = htab->got->size;
5133 htab->got->size += need;
5135 else
5137 max_before_header = htab->plt_type == PLT_NEW ? 32768 : 32764;
5138 if (need <= htab->got_gap)
5140 where = max_before_header - htab->got_gap;
5141 htab->got_gap -= need;
5143 else
5145 if (htab->got->size + need > max_before_header
5146 && htab->got->size <= max_before_header)
5148 htab->got_gap = max_before_header - htab->got->size;
5149 htab->got->size = max_before_header + htab->got_header_size;
5151 where = htab->got->size;
5152 htab->got->size += need;
5155 return where;
5158 /* Allocate space in associated reloc sections for dynamic relocs. */
5160 static bfd_boolean
5161 allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
5163 struct bfd_link_info *info = inf;
5164 struct ppc_elf_link_hash_entry *eh;
5165 struct ppc_elf_link_hash_table *htab;
5166 struct ppc_elf_dyn_relocs *p;
5168 if (h->root.type == bfd_link_hash_indirect)
5169 return TRUE;
5171 if (h->root.type == bfd_link_hash_warning)
5172 /* When warning symbols are created, they **replace** the "real"
5173 entry in the hash table, thus we never get to see the real
5174 symbol in a hash traversal. So look at it now. */
5175 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5177 htab = ppc_elf_hash_table (info);
5178 if (htab->elf.dynamic_sections_created
5179 || h->type == STT_GNU_IFUNC)
5181 struct plt_entry *ent;
5182 bfd_boolean doneone = FALSE;
5183 bfd_vma plt_offset = 0, glink_offset = 0;
5184 bfd_boolean dyn;
5186 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
5187 if (ent->plt.refcount > 0)
5189 /* Make sure this symbol is output as a dynamic symbol. */
5190 if (h->dynindx == -1
5191 && !h->forced_local
5192 && !h->def_regular
5193 && htab->elf.dynamic_sections_created)
5195 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5196 return FALSE;
5199 dyn = htab->elf.dynamic_sections_created;
5200 if (info->shared
5201 || h->type == STT_GNU_IFUNC
5202 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h))
5204 asection *s = htab->plt;
5205 if (!dyn || h->dynindx == -1)
5206 s = htab->iplt;
5208 if (htab->plt_type == PLT_NEW || !dyn || h->dynindx == -1)
5210 if (!doneone)
5212 plt_offset = s->size;
5213 s->size += 4;
5215 ent->plt.offset = plt_offset;
5217 s = htab->glink;
5218 if (!doneone || info->shared)
5220 glink_offset = s->size;
5221 s->size += GLINK_ENTRY_SIZE;
5222 if (h == htab->tls_get_addr
5223 && !htab->no_tls_get_addr_opt)
5224 s->size += TLS_GET_ADDR_GLINK_SIZE - GLINK_ENTRY_SIZE;
5226 if (!doneone
5227 && !info->shared
5228 && h->def_dynamic
5229 && !h->def_regular)
5231 h->root.u.def.section = s;
5232 h->root.u.def.value = glink_offset;
5234 ent->glink_offset = glink_offset;
5236 if (htab->emit_stub_syms
5237 && !add_stub_sym (ent, h, info))
5238 return FALSE;
5240 else
5242 if (!doneone)
5244 /* If this is the first .plt entry, make room
5245 for the special first entry. */
5246 if (s->size == 0)
5247 s->size += htab->plt_initial_entry_size;
5249 /* The PowerPC PLT is actually composed of two
5250 parts, the first part is 2 words (for a load
5251 and a jump), and then there is a remaining
5252 word available at the end. */
5253 plt_offset = (htab->plt_initial_entry_size
5254 + (htab->plt_slot_size
5255 * ((s->size
5256 - htab->plt_initial_entry_size)
5257 / htab->plt_entry_size)));
5259 /* If this symbol is not defined in a regular
5260 file, and we are not generating a shared
5261 library, then set the symbol to this location
5262 in the .plt. This is to avoid text
5263 relocations, and is required to make
5264 function pointers compare as equal between
5265 the normal executable and the shared library. */
5266 if (! info->shared
5267 && h->def_dynamic
5268 && !h->def_regular)
5270 h->root.u.def.section = s;
5271 h->root.u.def.value = plt_offset;
5274 /* Make room for this entry. */
5275 s->size += htab->plt_entry_size;
5276 /* After the 8192nd entry, room for two entries
5277 is allocated. */
5278 if (htab->plt_type == PLT_OLD
5279 && (s->size - htab->plt_initial_entry_size)
5280 / htab->plt_entry_size
5281 > PLT_NUM_SINGLE_ENTRIES)
5282 s->size += htab->plt_entry_size;
5284 ent->plt.offset = plt_offset;
5287 /* We also need to make an entry in the .rela.plt section. */
5288 if (!doneone)
5290 if (!htab->elf.dynamic_sections_created
5291 || h->dynindx == -1)
5292 htab->reliplt->size += sizeof (Elf32_External_Rela);
5293 else
5295 htab->relplt->size += sizeof (Elf32_External_Rela);
5297 if (htab->plt_type == PLT_VXWORKS)
5299 /* Allocate space for the unloaded relocations. */
5300 if (!info->shared
5301 && htab->elf.dynamic_sections_created)
5303 if (ent->plt.offset
5304 == (bfd_vma) htab->plt_initial_entry_size)
5306 htab->srelplt2->size
5307 += (sizeof (Elf32_External_Rela)
5308 * VXWORKS_PLTRESOLVE_RELOCS);
5311 htab->srelplt2->size
5312 += (sizeof (Elf32_External_Rela)
5313 * VXWORKS_PLT_NON_JMP_SLOT_RELOCS);
5316 /* Every PLT entry has an associated GOT entry in
5317 .got.plt. */
5318 htab->sgotplt->size += 4;
5321 doneone = TRUE;
5324 else
5325 ent->plt.offset = (bfd_vma) -1;
5327 else
5328 ent->plt.offset = (bfd_vma) -1;
5330 if (!doneone)
5332 h->plt.plist = NULL;
5333 h->needs_plt = 0;
5336 else
5338 h->plt.plist = NULL;
5339 h->needs_plt = 0;
5342 eh = (struct ppc_elf_link_hash_entry *) h;
5343 if (eh->elf.got.refcount > 0)
5345 bfd_boolean dyn;
5346 unsigned int need;
5348 /* Make sure this symbol is output as a dynamic symbol. */
5349 if (eh->elf.dynindx == -1
5350 && !eh->elf.forced_local
5351 && eh->elf.type != STT_GNU_IFUNC
5352 && htab->elf.dynamic_sections_created)
5354 if (!bfd_elf_link_record_dynamic_symbol (info, &eh->elf))
5355 return FALSE;
5358 need = 0;
5359 if ((eh->tls_mask & TLS_TLS) != 0)
5361 if ((eh->tls_mask & TLS_LD) != 0)
5363 if (!eh->elf.def_dynamic)
5364 /* We'll just use htab->tlsld_got.offset. This should
5365 always be the case. It's a little odd if we have
5366 a local dynamic reloc against a non-local symbol. */
5367 htab->tlsld_got.refcount += 1;
5368 else
5369 need += 8;
5371 if ((eh->tls_mask & TLS_GD) != 0)
5372 need += 8;
5373 if ((eh->tls_mask & (TLS_TPREL | TLS_TPRELGD)) != 0)
5374 need += 4;
5375 if ((eh->tls_mask & TLS_DTPREL) != 0)
5376 need += 4;
5378 else
5379 need += 4;
5380 if (need == 0)
5381 eh->elf.got.offset = (bfd_vma) -1;
5382 else
5384 eh->elf.got.offset = allocate_got (htab, need);
5385 dyn = htab->elf.dynamic_sections_created;
5386 if ((info->shared
5387 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, &eh->elf))
5388 && (ELF_ST_VISIBILITY (eh->elf.other) == STV_DEFAULT
5389 || eh->elf.root.type != bfd_link_hash_undefweak))
5391 asection *rsec = htab->relgot;
5392 /* All the entries we allocated need relocs.
5393 Except LD only needs one. */
5394 if ((eh->tls_mask & TLS_LD) != 0
5395 && eh->elf.def_dynamic)
5396 need -= 4;
5397 rsec->size += need * (sizeof (Elf32_External_Rela) / 4);
5401 else
5402 eh->elf.got.offset = (bfd_vma) -1;
5404 if (eh->dyn_relocs == NULL
5405 || !htab->elf.dynamic_sections_created)
5406 return TRUE;
5408 /* In the shared -Bsymbolic case, discard space allocated for
5409 dynamic pc-relative relocs against symbols which turn out to be
5410 defined in regular objects. For the normal shared case, discard
5411 space for relocs that have become local due to symbol visibility
5412 changes. */
5414 if (info->shared)
5416 /* Relocs that use pc_count are those that appear on a call insn,
5417 or certain REL relocs (see must_be_dyn_reloc) that can be
5418 generated via assembly. We want calls to protected symbols to
5419 resolve directly to the function rather than going via the plt.
5420 If people want function pointer comparisons to work as expected
5421 then they should avoid writing weird assembly. */
5422 if (SYMBOL_CALLS_LOCAL (info, h))
5424 struct ppc_elf_dyn_relocs **pp;
5426 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
5428 p->count -= p->pc_count;
5429 p->pc_count = 0;
5430 if (p->count == 0)
5431 *pp = p->next;
5432 else
5433 pp = &p->next;
5437 if (htab->is_vxworks)
5439 struct ppc_elf_dyn_relocs **pp;
5441 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
5443 if (strcmp (p->sec->output_section->name, ".tls_vars") == 0)
5444 *pp = p->next;
5445 else
5446 pp = &p->next;
5450 /* Discard relocs on undefined symbols that must be local. */
5451 if (eh->dyn_relocs != NULL
5452 && h->root.type == bfd_link_hash_undefined
5453 && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
5454 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
5455 eh->dyn_relocs = NULL;
5457 /* Also discard relocs on undefined weak syms with non-default
5458 visibility. */
5459 if (eh->dyn_relocs != NULL
5460 && h->root.type == bfd_link_hash_undefweak)
5462 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
5463 eh->dyn_relocs = NULL;
5465 /* Make sure undefined weak symbols are output as a dynamic
5466 symbol in PIEs. */
5467 else if (h->dynindx == -1
5468 && !h->forced_local
5469 && !h->def_regular)
5471 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5472 return FALSE;
5476 else if (ELIMINATE_COPY_RELOCS)
5478 /* For the non-shared case, discard space for relocs against
5479 symbols which turn out to need copy relocs or are not
5480 dynamic. */
5482 if (!h->non_got_ref
5483 && !h->def_regular)
5485 /* Make sure this symbol is output as a dynamic symbol.
5486 Undefined weak syms won't yet be marked as dynamic. */
5487 if (h->dynindx == -1
5488 && !h->forced_local)
5490 if (! bfd_elf_link_record_dynamic_symbol (info, h))
5491 return FALSE;
5494 /* If that succeeded, we know we'll be keeping all the
5495 relocs. */
5496 if (h->dynindx != -1)
5497 goto keep;
5500 eh->dyn_relocs = NULL;
5502 keep: ;
5505 /* Finally, allocate space. */
5506 for (p = eh->dyn_relocs; p != NULL; p = p->next)
5508 asection *sreloc = elf_section_data (p->sec)->sreloc;
5509 if (!htab->elf.dynamic_sections_created)
5510 sreloc = htab->reliplt;
5511 sreloc->size += p->count * sizeof (Elf32_External_Rela);
5514 return TRUE;
5517 /* Set DF_TEXTREL if we find any dynamic relocs that apply to
5518 read-only sections. */
5520 static bfd_boolean
5521 maybe_set_textrel (struct elf_link_hash_entry *h, void *info)
5523 if (h->root.type == bfd_link_hash_indirect)
5524 return TRUE;
5526 if (h->root.type == bfd_link_hash_warning)
5527 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5529 if (readonly_dynrelocs (h))
5531 ((struct bfd_link_info *) info)->flags |= DF_TEXTREL;
5533 /* Not an error, just cut short the traversal. */
5534 return FALSE;
5536 return TRUE;
5539 /* Set the sizes of the dynamic sections. */
5541 static bfd_boolean
5542 ppc_elf_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
5543 struct bfd_link_info *info)
5545 struct ppc_elf_link_hash_table *htab;
5546 asection *s;
5547 bfd_boolean relocs;
5548 bfd *ibfd;
5550 #ifdef DEBUG
5551 fprintf (stderr, "ppc_elf_size_dynamic_sections called\n");
5552 #endif
5554 htab = ppc_elf_hash_table (info);
5555 BFD_ASSERT (htab->elf.dynobj != NULL);
5557 if (elf_hash_table (info)->dynamic_sections_created)
5559 /* Set the contents of the .interp section to the interpreter. */
5560 if (info->executable)
5562 s = bfd_get_section_by_name (htab->elf.dynobj, ".interp");
5563 BFD_ASSERT (s != NULL);
5564 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
5565 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
5569 if (htab->plt_type == PLT_OLD)
5570 htab->got_header_size = 16;
5571 else if (htab->plt_type == PLT_NEW)
5572 htab->got_header_size = 12;
5574 /* Set up .got offsets for local syms, and space for local dynamic
5575 relocs. */
5576 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
5578 bfd_signed_vma *local_got;
5579 bfd_signed_vma *end_local_got;
5580 struct plt_entry **local_plt;
5581 struct plt_entry **end_local_plt;
5582 char *lgot_masks;
5583 bfd_size_type locsymcount;
5584 Elf_Internal_Shdr *symtab_hdr;
5586 if (!is_ppc_elf (ibfd))
5587 continue;
5589 for (s = ibfd->sections; s != NULL; s = s->next)
5591 struct ppc_elf_dyn_relocs *p;
5593 for (p = ((struct ppc_elf_dyn_relocs *)
5594 elf_section_data (s)->local_dynrel);
5595 p != NULL;
5596 p = p->next)
5598 if (!bfd_is_abs_section (p->sec)
5599 && bfd_is_abs_section (p->sec->output_section))
5601 /* Input section has been discarded, either because
5602 it is a copy of a linkonce section or due to
5603 linker script /DISCARD/, so we'll be discarding
5604 the relocs too. */
5606 else if (htab->is_vxworks
5607 && strcmp (p->sec->output_section->name,
5608 ".tls_vars") == 0)
5610 /* Relocations in vxworks .tls_vars sections are
5611 handled specially by the loader. */
5613 else if (p->count != 0)
5615 asection *sreloc = elf_section_data (p->sec)->sreloc;
5616 if (!htab->elf.dynamic_sections_created)
5617 sreloc = htab->reliplt;
5618 sreloc->size += p->count * sizeof (Elf32_External_Rela);
5619 if ((p->sec->output_section->flags
5620 & (SEC_READONLY | SEC_ALLOC))
5621 == (SEC_READONLY | SEC_ALLOC))
5622 info->flags |= DF_TEXTREL;
5627 local_got = elf_local_got_refcounts (ibfd);
5628 if (!local_got)
5629 continue;
5631 symtab_hdr = &elf_symtab_hdr (ibfd);
5632 locsymcount = symtab_hdr->sh_info;
5633 end_local_got = local_got + locsymcount;
5634 local_plt = (struct plt_entry **) end_local_got;
5635 end_local_plt = local_plt + locsymcount;
5636 lgot_masks = (char *) end_local_plt;
5638 for (; local_got < end_local_got; ++local_got, ++lgot_masks)
5639 if (*local_got > 0)
5641 unsigned int need = 0;
5642 if ((*lgot_masks & TLS_TLS) != 0)
5644 if ((*lgot_masks & TLS_GD) != 0)
5645 need += 8;
5646 if ((*lgot_masks & TLS_LD) != 0)
5647 htab->tlsld_got.refcount += 1;
5648 if ((*lgot_masks & (TLS_TPREL | TLS_TPRELGD)) != 0)
5649 need += 4;
5650 if ((*lgot_masks & TLS_DTPREL) != 0)
5651 need += 4;
5653 else
5654 need += 4;
5655 if (need == 0)
5656 *local_got = (bfd_vma) -1;
5657 else
5659 *local_got = allocate_got (htab, need);
5660 if (info->shared)
5661 htab->relgot->size += (need
5662 * (sizeof (Elf32_External_Rela) / 4));
5665 else
5666 *local_got = (bfd_vma) -1;
5668 if (htab->is_vxworks)
5669 continue;
5671 /* Allocate space for calls to local STT_GNU_IFUNC syms in .iplt. */
5672 for (; local_plt < end_local_plt; ++local_plt)
5674 struct plt_entry *ent;
5675 bfd_boolean doneone = FALSE;
5676 bfd_vma plt_offset = 0, glink_offset = 0;
5678 for (ent = *local_plt; ent != NULL; ent = ent->next)
5679 if (ent->plt.refcount > 0)
5681 s = htab->iplt;
5683 if (!doneone)
5685 plt_offset = s->size;
5686 s->size += 4;
5688 ent->plt.offset = plt_offset;
5690 s = htab->glink;
5691 if (!doneone || info->shared)
5693 glink_offset = s->size;
5694 s->size += GLINK_ENTRY_SIZE;
5696 ent->glink_offset = glink_offset;
5698 if (!doneone)
5700 htab->reliplt->size += sizeof (Elf32_External_Rela);
5701 doneone = TRUE;
5704 else
5705 ent->plt.offset = (bfd_vma) -1;
5709 /* Allocate space for global sym dynamic relocs. */
5710 elf_link_hash_traverse (elf_hash_table (info), allocate_dynrelocs, info);
5712 if (htab->tlsld_got.refcount > 0)
5714 htab->tlsld_got.offset = allocate_got (htab, 8);
5715 if (info->shared)
5716 htab->relgot->size += sizeof (Elf32_External_Rela);
5718 else
5719 htab->tlsld_got.offset = (bfd_vma) -1;
5721 if (htab->got != NULL && htab->plt_type != PLT_VXWORKS)
5723 unsigned int g_o_t = 32768;
5725 /* If we haven't allocated the header, do so now. When we get here,
5726 for old plt/got the got size will be 0 to 32764 (not allocated),
5727 or 32780 to 65536 (header allocated). For new plt/got, the
5728 corresponding ranges are 0 to 32768 and 32780 to 65536. */
5729 if (htab->got->size <= 32768)
5731 g_o_t = htab->got->size;
5732 if (htab->plt_type == PLT_OLD)
5733 g_o_t += 4;
5734 htab->got->size += htab->got_header_size;
5737 htab->elf.hgot->root.u.def.value = g_o_t;
5739 if (info->shared)
5741 struct elf_link_hash_entry *sda = htab->sdata[0].sym;
5742 if (sda != NULL
5743 && !(sda->root.type == bfd_link_hash_defined
5744 || sda->root.type == bfd_link_hash_defweak))
5746 sda->root.type = bfd_link_hash_defined;
5747 sda->root.u.def.section = htab->elf.hgot->root.u.def.section;
5748 sda->root.u.def.value = htab->elf.hgot->root.u.def.value;
5752 if (htab->glink != NULL
5753 && htab->glink->size != 0
5754 && htab->elf.dynamic_sections_created)
5756 htab->glink_pltresolve = htab->glink->size;
5757 /* Space for the branch table. */
5758 htab->glink->size += htab->glink->size / (GLINK_ENTRY_SIZE / 4) - 4;
5759 /* Pad out to align the start of PLTresolve. */
5760 htab->glink->size += -htab->glink->size & 15;
5761 htab->glink->size += GLINK_PLTRESOLVE;
5763 if (htab->emit_stub_syms)
5765 struct elf_link_hash_entry *sh;
5766 sh = elf_link_hash_lookup (&htab->elf, "__glink",
5767 TRUE, FALSE, FALSE);
5768 if (sh == NULL)
5769 return FALSE;
5770 if (sh->root.type == bfd_link_hash_new)
5772 sh->root.type = bfd_link_hash_defined;
5773 sh->root.u.def.section = htab->glink;
5774 sh->root.u.def.value = htab->glink_pltresolve;
5775 sh->ref_regular = 1;
5776 sh->def_regular = 1;
5777 sh->ref_regular_nonweak = 1;
5778 sh->forced_local = 1;
5779 sh->non_elf = 0;
5781 sh = elf_link_hash_lookup (&htab->elf, "__glink_PLTresolve",
5782 TRUE, FALSE, FALSE);
5783 if (sh == NULL)
5784 return FALSE;
5785 if (sh->root.type == bfd_link_hash_new)
5787 sh->root.type = bfd_link_hash_defined;
5788 sh->root.u.def.section = htab->glink;
5789 sh->root.u.def.value = htab->glink->size - GLINK_PLTRESOLVE;
5790 sh->ref_regular = 1;
5791 sh->def_regular = 1;
5792 sh->ref_regular_nonweak = 1;
5793 sh->forced_local = 1;
5794 sh->non_elf = 0;
5799 /* We've now determined the sizes of the various dynamic sections.
5800 Allocate memory for them. */
5801 relocs = FALSE;
5802 for (s = htab->elf.dynobj->sections; s != NULL; s = s->next)
5804 bfd_boolean strip_section = TRUE;
5806 if ((s->flags & SEC_LINKER_CREATED) == 0)
5807 continue;
5809 if (s == htab->plt
5810 || s == htab->got)
5812 /* We'd like to strip these sections if they aren't needed, but if
5813 we've exported dynamic symbols from them we must leave them.
5814 It's too late to tell BFD to get rid of the symbols. */
5815 if (htab->elf.hplt != NULL)
5816 strip_section = FALSE;
5817 /* Strip this section if we don't need it; see the
5818 comment below. */
5820 else if (s == htab->iplt
5821 || s == htab->glink
5822 || s == htab->sgotplt
5823 || s == htab->sbss
5824 || s == htab->dynbss
5825 || s == htab->dynsbss
5826 || s == htab->sdata[0].section
5827 || s == htab->sdata[1].section)
5829 /* Strip these too. */
5831 else if (CONST_STRNEQ (bfd_get_section_name (dynobj, s), ".rela"))
5833 if (s->size != 0)
5835 /* Remember whether there are any relocation sections. */
5836 relocs = TRUE;
5838 /* We use the reloc_count field as a counter if we need
5839 to copy relocs into the output file. */
5840 s->reloc_count = 0;
5843 else
5845 /* It's not one of our sections, so don't allocate space. */
5846 continue;
5849 if (s->size == 0 && strip_section)
5851 /* If we don't need this section, strip it from the
5852 output file. This is mostly to handle .rela.bss and
5853 .rela.plt. We must create both sections in
5854 create_dynamic_sections, because they must be created
5855 before the linker maps input sections to output
5856 sections. The linker does that before
5857 adjust_dynamic_symbol is called, and it is that
5858 function which decides whether anything needs to go
5859 into these sections. */
5860 s->flags |= SEC_EXCLUDE;
5861 continue;
5864 if ((s->flags & SEC_HAS_CONTENTS) == 0)
5865 continue;
5867 /* Allocate memory for the section contents. */
5868 s->contents = bfd_zalloc (htab->elf.dynobj, s->size);
5869 if (s->contents == NULL)
5870 return FALSE;
5873 if (htab->elf.dynamic_sections_created)
5875 /* Add some entries to the .dynamic section. We fill in the
5876 values later, in ppc_elf_finish_dynamic_sections, but we
5877 must add the entries now so that we get the correct size for
5878 the .dynamic section. The DT_DEBUG entry is filled in by the
5879 dynamic linker and used by the debugger. */
5880 #define add_dynamic_entry(TAG, VAL) \
5881 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
5883 if (info->executable)
5885 if (!add_dynamic_entry (DT_DEBUG, 0))
5886 return FALSE;
5889 if (htab->plt != NULL && htab->plt->size != 0)
5891 if (!add_dynamic_entry (DT_PLTGOT, 0)
5892 || !add_dynamic_entry (DT_PLTRELSZ, 0)
5893 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
5894 || !add_dynamic_entry (DT_JMPREL, 0))
5895 return FALSE;
5898 if (htab->glink != NULL && htab->glink->size != 0)
5900 if (!add_dynamic_entry (DT_PPC_GOT, 0))
5901 return FALSE;
5902 if (!htab->no_tls_get_addr_opt
5903 && htab->tls_get_addr != NULL
5904 && htab->tls_get_addr->plt.plist != NULL
5905 && !add_dynamic_entry (DT_PPC_TLSOPT, 0))
5906 return FALSE;
5909 if (relocs)
5911 if (!add_dynamic_entry (DT_RELA, 0)
5912 || !add_dynamic_entry (DT_RELASZ, 0)
5913 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf32_External_Rela)))
5914 return FALSE;
5917 /* If any dynamic relocs apply to a read-only section, then we
5918 need a DT_TEXTREL entry. */
5919 if ((info->flags & DF_TEXTREL) == 0)
5920 elf_link_hash_traverse (elf_hash_table (info), maybe_set_textrel,
5921 info);
5923 if ((info->flags & DF_TEXTREL) != 0)
5925 if (!add_dynamic_entry (DT_TEXTREL, 0))
5926 return FALSE;
5928 if (htab->is_vxworks
5929 && !elf_vxworks_add_dynamic_entries (output_bfd, info))
5930 return FALSE;
5932 #undef add_dynamic_entry
5934 return TRUE;
5937 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
5939 static bfd_boolean
5940 ppc_elf_hash_symbol (struct elf_link_hash_entry *h)
5942 if (h->plt.plist != NULL
5943 && !h->def_regular
5944 && (!h->pointer_equality_needed
5945 || !h->ref_regular_nonweak))
5946 return FALSE;
5948 return _bfd_elf_hash_symbol (h);
5951 #define ARRAY_SIZE(a) (sizeof (a) / sizeof ((a)[0]))
5953 /* Relaxation trampolines. r12 is available for clobbering (r11, is
5954 used for some functions that are allowed to break the ABI). */
5955 static const int shared_stub_entry[] =
5957 0x7c0802a6, /* mflr 0 */
5958 0x429f0005, /* bcl 20, 31, .Lxxx */
5959 0x7d8802a6, /* mflr 12 */
5960 0x3d8c0000, /* addis 12, 12, (xxx-.Lxxx)@ha */
5961 0x398c0008, /* addi 12, 12, (xxx-.Lxxx)@l */
5962 0x7c0803a6, /* mtlr 0 */
5963 0x7d8903a6, /* mtctr 12 */
5964 0x4e800420, /* bctr */
5967 static const int stub_entry[] =
5969 0x3d800000, /* lis 12,xxx@ha */
5970 0x398c0000, /* addi 12,12,xxx@l */
5971 0x7d8903a6, /* mtctr 12 */
5972 0x4e800420, /* bctr */
5975 static bfd_boolean
5976 ppc_elf_relax_section (bfd *abfd,
5977 asection *isec,
5978 struct bfd_link_info *link_info,
5979 bfd_boolean *again)
5981 struct one_fixup
5983 struct one_fixup *next;
5984 asection *tsec;
5985 /* Final link, can use the symbol offset. For a
5986 relocatable link we use the symbol's index. */
5987 bfd_vma toff;
5988 bfd_vma trampoff;
5991 Elf_Internal_Shdr *symtab_hdr;
5992 bfd_byte *contents = NULL;
5993 Elf_Internal_Sym *isymbuf = NULL;
5994 Elf_Internal_Rela *internal_relocs = NULL;
5995 Elf_Internal_Rela *irel, *irelend;
5996 struct one_fixup *fixups = NULL;
5997 unsigned changes = 0;
5998 struct ppc_elf_link_hash_table *htab;
5999 bfd_size_type trampoff;
6000 asection *got2;
6001 bfd_boolean maybe_pasted;
6003 *again = FALSE;
6005 /* Nothing to do if there are no relocations, and no need to do
6006 anything with non-alloc or non-code sections. */
6007 if ((isec->flags & SEC_ALLOC) == 0
6008 || (isec->flags & SEC_CODE) == 0
6009 || (isec->flags & SEC_RELOC) == 0
6010 || isec->reloc_count == 0)
6011 return TRUE;
6013 /* We cannot represent the required PIC relocs in the output, so don't
6014 do anything. The linker doesn't support mixing -shared and -r
6015 anyway. */
6016 if (link_info->relocatable && link_info->shared)
6017 return TRUE;
6019 trampoff = (isec->size + 3) & (bfd_vma) -4;
6020 maybe_pasted = (strcmp (isec->output_section->name, ".init") == 0
6021 || strcmp (isec->output_section->name, ".fini") == 0);
6022 /* Space for a branch around any trampolines. */
6023 if (maybe_pasted)
6024 trampoff += 4;
6026 symtab_hdr = &elf_symtab_hdr (abfd);
6028 /* Get a copy of the native relocations. */
6029 internal_relocs = _bfd_elf_link_read_relocs (abfd, isec, NULL, NULL,
6030 link_info->keep_memory);
6031 if (internal_relocs == NULL)
6032 goto error_return;
6034 htab = ppc_elf_hash_table (link_info);
6035 got2 = bfd_get_section_by_name (abfd, ".got2");
6037 irelend = internal_relocs + isec->reloc_count;
6038 for (irel = internal_relocs; irel < irelend; irel++)
6040 unsigned long r_type = ELF32_R_TYPE (irel->r_info);
6041 bfd_vma toff, roff;
6042 asection *tsec;
6043 struct one_fixup *f;
6044 size_t insn_offset = 0;
6045 bfd_vma max_branch_offset, val;
6046 bfd_byte *hit_addr;
6047 unsigned long t0;
6048 struct elf_link_hash_entry *h;
6049 struct plt_entry **plist;
6050 unsigned char sym_type;
6052 switch (r_type)
6054 case R_PPC_REL24:
6055 case R_PPC_LOCAL24PC:
6056 case R_PPC_PLTREL24:
6057 max_branch_offset = 1 << 25;
6058 break;
6060 case R_PPC_REL14:
6061 case R_PPC_REL14_BRTAKEN:
6062 case R_PPC_REL14_BRNTAKEN:
6063 max_branch_offset = 1 << 15;
6064 break;
6066 default:
6067 continue;
6070 /* Get the value of the symbol referred to by the reloc. */
6071 h = NULL;
6072 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
6074 /* A local symbol. */
6075 Elf_Internal_Sym *isym;
6077 /* Read this BFD's local symbols. */
6078 if (isymbuf == NULL)
6080 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
6081 if (isymbuf == NULL)
6082 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
6083 symtab_hdr->sh_info, 0,
6084 NULL, NULL, NULL);
6085 if (isymbuf == 0)
6086 goto error_return;
6088 isym = isymbuf + ELF32_R_SYM (irel->r_info);
6089 if (isym->st_shndx == SHN_UNDEF)
6090 tsec = bfd_und_section_ptr;
6091 else if (isym->st_shndx == SHN_ABS)
6092 tsec = bfd_abs_section_ptr;
6093 else if (isym->st_shndx == SHN_COMMON)
6094 tsec = bfd_com_section_ptr;
6095 else
6096 tsec = bfd_section_from_elf_index (abfd, isym->st_shndx);
6098 toff = isym->st_value;
6099 sym_type = ELF_ST_TYPE (isym->st_info);
6101 else
6103 /* Global symbol handling. */
6104 unsigned long indx;
6106 indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
6107 h = elf_sym_hashes (abfd)[indx];
6109 while (h->root.type == bfd_link_hash_indirect
6110 || h->root.type == bfd_link_hash_warning)
6111 h = (struct elf_link_hash_entry *) h->root.u.i.link;
6113 if (h->root.type == bfd_link_hash_defined
6114 || h->root.type == bfd_link_hash_defweak)
6116 tsec = h->root.u.def.section;
6117 toff = h->root.u.def.value;
6119 else if (h->root.type == bfd_link_hash_undefined
6120 || h->root.type == bfd_link_hash_undefweak)
6122 tsec = bfd_und_section_ptr;
6123 toff = link_info->relocatable ? indx : 0;
6125 else
6126 continue;
6128 sym_type = h->type;
6131 /* The condition here under which we call find_plt_ent must
6132 match that in relocate_section. If we call find_plt_ent here
6133 but not in relocate_section, or vice versa, then the branch
6134 destination used here may be incorrect. */
6135 plist = NULL;
6136 if (h != NULL)
6138 /* We know is_branch_reloc (r_type) is true. */
6139 if (h->type == STT_GNU_IFUNC
6140 || r_type == R_PPC_PLTREL24)
6141 plist = &h->plt.plist;
6143 else if (sym_type == STT_GNU_IFUNC
6144 && elf_local_got_offsets (abfd) != NULL)
6146 bfd_vma *local_got_offsets = elf_local_got_offsets (abfd);
6147 struct plt_entry **local_plt = (struct plt_entry **)
6148 (local_got_offsets + symtab_hdr->sh_info);
6149 plist = local_plt + ELF32_R_SYM (irel->r_info);
6151 if (plist != NULL)
6153 bfd_vma addend = 0;
6154 struct plt_entry *ent;
6156 if (r_type == R_PPC_PLTREL24 && link_info->shared)
6157 addend = irel->r_addend;
6158 ent = find_plt_ent (plist, got2, addend);
6159 if (ent != NULL)
6161 if (htab->plt_type == PLT_NEW
6162 || h == NULL
6163 || !htab->elf.dynamic_sections_created
6164 || h->dynindx == -1)
6166 tsec = htab->glink;
6167 toff = ent->glink_offset;
6169 else
6171 tsec = htab->plt;
6172 toff = ent->plt.offset;
6177 /* If the branch and target are in the same section, you have
6178 no hope of adding stubs. We'll error out later should the
6179 branch overflow. */
6180 if (tsec == isec)
6181 continue;
6183 /* There probably isn't any reason to handle symbols in
6184 SEC_MERGE sections; SEC_MERGE doesn't seem a likely
6185 attribute for a code section, and we are only looking at
6186 branches. However, implement it correctly here as a
6187 reference for other target relax_section functions. */
6188 if (0 && tsec->sec_info_type == ELF_INFO_TYPE_MERGE)
6190 /* At this stage in linking, no SEC_MERGE symbol has been
6191 adjusted, so all references to such symbols need to be
6192 passed through _bfd_merged_section_offset. (Later, in
6193 relocate_section, all SEC_MERGE symbols *except* for
6194 section symbols have been adjusted.)
6196 gas may reduce relocations against symbols in SEC_MERGE
6197 sections to a relocation against the section symbol when
6198 the original addend was zero. When the reloc is against
6199 a section symbol we should include the addend in the
6200 offset passed to _bfd_merged_section_offset, since the
6201 location of interest is the original symbol. On the
6202 other hand, an access to "sym+addend" where "sym" is not
6203 a section symbol should not include the addend; Such an
6204 access is presumed to be an offset from "sym"; The
6205 location of interest is just "sym". */
6206 if (sym_type == STT_SECTION)
6207 toff += irel->r_addend;
6209 toff = _bfd_merged_section_offset (abfd, &tsec,
6210 elf_section_data (tsec)->sec_info,
6211 toff);
6213 if (sym_type != STT_SECTION)
6214 toff += irel->r_addend;
6216 /* PLTREL24 addends are special. */
6217 else if (r_type != R_PPC_PLTREL24)
6218 toff += irel->r_addend;
6220 /* Attempted -shared link of non-pic code loses. */
6221 if (tsec->output_section == NULL)
6222 continue;
6224 roff = irel->r_offset;
6226 /* If the branch is in range, no need to do anything. */
6227 if (tsec != bfd_und_section_ptr
6228 && (!link_info->relocatable
6229 /* A relocatable link may have sections moved during
6230 final link, so do not presume they remain in range. */
6231 || tsec->output_section == isec->output_section))
6233 bfd_vma symaddr, reladdr;
6235 symaddr = tsec->output_section->vma + tsec->output_offset + toff;
6236 reladdr = isec->output_section->vma + isec->output_offset + roff;
6237 if (symaddr - reladdr + max_branch_offset < 2 * max_branch_offset)
6238 continue;
6241 /* Look for an existing fixup to this address. */
6242 for (f = fixups; f ; f = f->next)
6243 if (f->tsec == tsec && f->toff == toff)
6244 break;
6246 if (f == NULL)
6248 size_t size;
6249 unsigned long stub_rtype;
6251 val = trampoff - roff;
6252 if (val >= max_branch_offset)
6253 /* Oh dear, we can't reach a trampoline. Don't try to add
6254 one. We'll report an error later. */
6255 continue;
6257 if (link_info->shared)
6259 size = 4 * ARRAY_SIZE (shared_stub_entry);
6260 insn_offset = 12;
6262 else
6264 size = 4 * ARRAY_SIZE (stub_entry);
6265 insn_offset = 0;
6267 stub_rtype = R_PPC_RELAX;
6268 if (tsec == htab->plt
6269 || tsec == htab->glink)
6271 stub_rtype = R_PPC_RELAX_PLT;
6272 if (r_type == R_PPC_PLTREL24)
6273 stub_rtype = R_PPC_RELAX_PLTREL24;
6276 /* Hijack the old relocation. Since we need two
6277 relocations for this use a "composite" reloc. */
6278 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
6279 stub_rtype);
6280 irel->r_offset = trampoff + insn_offset;
6281 if (r_type == R_PPC_PLTREL24
6282 && stub_rtype != R_PPC_RELAX_PLTREL24)
6283 irel->r_addend = 0;
6285 /* Record the fixup so we don't do it again this section. */
6286 f = bfd_malloc (sizeof (*f));
6287 f->next = fixups;
6288 f->tsec = tsec;
6289 f->toff = toff;
6290 f->trampoff = trampoff;
6291 fixups = f;
6293 trampoff += size;
6294 changes++;
6296 else
6298 val = f->trampoff - roff;
6299 if (val >= max_branch_offset)
6300 continue;
6302 /* Nop out the reloc, since we're finalizing things here. */
6303 irel->r_info = ELF32_R_INFO (0, R_PPC_NONE);
6306 /* Get the section contents. */
6307 if (contents == NULL)
6309 /* Get cached copy if it exists. */
6310 if (elf_section_data (isec)->this_hdr.contents != NULL)
6311 contents = elf_section_data (isec)->this_hdr.contents;
6312 else
6314 /* Go get them off disk. */
6315 if (!bfd_malloc_and_get_section (abfd, isec, &contents))
6316 goto error_return;
6320 /* Fix up the existing branch to hit the trampoline. */
6321 hit_addr = contents + roff;
6322 switch (r_type)
6324 case R_PPC_REL24:
6325 case R_PPC_LOCAL24PC:
6326 case R_PPC_PLTREL24:
6327 t0 = bfd_get_32 (abfd, hit_addr);
6328 t0 &= ~0x3fffffc;
6329 t0 |= val & 0x3fffffc;
6330 bfd_put_32 (abfd, t0, hit_addr);
6331 break;
6333 case R_PPC_REL14:
6334 case R_PPC_REL14_BRTAKEN:
6335 case R_PPC_REL14_BRNTAKEN:
6336 t0 = bfd_get_32 (abfd, hit_addr);
6337 t0 &= ~0xfffc;
6338 t0 |= val & 0xfffc;
6339 bfd_put_32 (abfd, t0, hit_addr);
6340 break;
6344 /* Write out the trampolines. */
6345 if (fixups != NULL)
6347 const int *stub;
6348 bfd_byte *dest;
6349 int i, size;
6353 struct one_fixup *f = fixups;
6354 fixups = fixups->next;
6355 free (f);
6357 while (fixups);
6359 contents = bfd_realloc_or_free (contents, trampoff);
6360 if (contents == NULL)
6361 goto error_return;
6363 isec->size = (isec->size + 3) & (bfd_vma) -4;
6364 dest = contents + isec->size;
6365 /* Branch around the trampolines. */
6366 if (maybe_pasted)
6368 bfd_vma val = B + trampoff - isec->size;
6369 bfd_put_32 (abfd, val, dest);
6370 dest += 4;
6372 isec->size = trampoff;
6374 if (link_info->shared)
6376 stub = shared_stub_entry;
6377 size = ARRAY_SIZE (shared_stub_entry);
6379 else
6381 stub = stub_entry;
6382 size = ARRAY_SIZE (stub_entry);
6385 i = 0;
6386 while (dest < contents + trampoff)
6388 bfd_put_32 (abfd, stub[i], dest);
6389 i++;
6390 if (i == size)
6391 i = 0;
6392 dest += 4;
6394 BFD_ASSERT (i == 0);
6397 if (isymbuf != NULL
6398 && symtab_hdr->contents != (unsigned char *) isymbuf)
6400 if (! link_info->keep_memory)
6401 free (isymbuf);
6402 else
6404 /* Cache the symbols for elf_link_input_bfd. */
6405 symtab_hdr->contents = (unsigned char *) isymbuf;
6409 if (contents != NULL
6410 && elf_section_data (isec)->this_hdr.contents != contents)
6412 if (!changes && !link_info->keep_memory)
6413 free (contents);
6414 else
6416 /* Cache the section contents for elf_link_input_bfd. */
6417 elf_section_data (isec)->this_hdr.contents = contents;
6421 if (changes != 0)
6423 /* Append sufficient NOP relocs so we can write out relocation
6424 information for the trampolines. */
6425 Elf_Internal_Shdr *rel_hdr;
6426 Elf_Internal_Rela *new_relocs = bfd_malloc ((changes + isec->reloc_count)
6427 * sizeof (*new_relocs));
6428 unsigned ix;
6430 if (!new_relocs)
6431 goto error_return;
6432 memcpy (new_relocs, internal_relocs,
6433 isec->reloc_count * sizeof (*new_relocs));
6434 for (ix = changes; ix--;)
6436 irel = new_relocs + ix + isec->reloc_count;
6438 irel->r_info = ELF32_R_INFO (0, R_PPC_NONE);
6440 if (internal_relocs != elf_section_data (isec)->relocs)
6441 free (internal_relocs);
6442 elf_section_data (isec)->relocs = new_relocs;
6443 isec->reloc_count += changes;
6444 rel_hdr = _bfd_elf_single_rel_hdr (isec);
6445 rel_hdr->sh_size += changes * rel_hdr->sh_entsize;
6447 else if (elf_section_data (isec)->relocs != internal_relocs)
6448 free (internal_relocs);
6450 *again = changes != 0;
6451 if (!*again && link_info->relocatable)
6453 /* Convert the internal relax relocs to external form. */
6454 for (irel = internal_relocs; irel < irelend; irel++)
6455 if (ELF32_R_TYPE (irel->r_info) == R_PPC_RELAX)
6457 unsigned long r_symndx = ELF32_R_SYM (irel->r_info);
6459 /* Rewrite the reloc and convert one of the trailing nop
6460 relocs to describe this relocation. */
6461 BFD_ASSERT (ELF32_R_TYPE (irelend[-1].r_info) == R_PPC_NONE);
6462 /* The relocs are at the bottom 2 bytes */
6463 irel[0].r_offset += 2;
6464 memmove (irel + 1, irel, (irelend - irel - 1) * sizeof (*irel));
6465 irel[0].r_info = ELF32_R_INFO (r_symndx, R_PPC_ADDR16_HA);
6466 irel[1].r_offset += 4;
6467 irel[1].r_info = ELF32_R_INFO (r_symndx, R_PPC_ADDR16_LO);
6468 irel++;
6472 return TRUE;
6474 error_return:
6475 if (isymbuf != NULL && (unsigned char *) isymbuf != symtab_hdr->contents)
6476 free (isymbuf);
6477 if (contents != NULL
6478 && elf_section_data (isec)->this_hdr.contents != contents)
6479 free (contents);
6480 if (internal_relocs != NULL
6481 && elf_section_data (isec)->relocs != internal_relocs)
6482 free (internal_relocs);
6483 return FALSE;
6486 /* What to do when ld finds relocations against symbols defined in
6487 discarded sections. */
6489 static unsigned int
6490 ppc_elf_action_discarded (asection *sec)
6492 if (strcmp (".fixup", sec->name) == 0)
6493 return 0;
6495 if (strcmp (".got2", sec->name) == 0)
6496 return 0;
6498 return _bfd_elf_default_action_discarded (sec);
6501 /* Fill in the address for a pointer generated in a linker section. */
6503 static bfd_vma
6504 elf_finish_pointer_linker_section (bfd *input_bfd,
6505 elf_linker_section_t *lsect,
6506 struct elf_link_hash_entry *h,
6507 bfd_vma relocation,
6508 const Elf_Internal_Rela *rel)
6510 elf_linker_section_pointers_t *linker_section_ptr;
6512 BFD_ASSERT (lsect != NULL);
6514 if (h != NULL)
6516 /* Handle global symbol. */
6517 struct ppc_elf_link_hash_entry *eh;
6519 eh = (struct ppc_elf_link_hash_entry *) h;
6520 BFD_ASSERT (eh->elf.def_regular);
6521 linker_section_ptr = eh->linker_section_pointer;
6523 else
6525 /* Handle local symbol. */
6526 unsigned long r_symndx = ELF32_R_SYM (rel->r_info);
6528 BFD_ASSERT (is_ppc_elf (input_bfd));
6529 BFD_ASSERT (elf_local_ptr_offsets (input_bfd) != NULL);
6530 linker_section_ptr = elf_local_ptr_offsets (input_bfd)[r_symndx];
6533 linker_section_ptr = elf_find_pointer_linker_section (linker_section_ptr,
6534 rel->r_addend,
6535 lsect);
6536 BFD_ASSERT (linker_section_ptr != NULL);
6538 /* Offset will always be a multiple of four, so use the bottom bit
6539 as a "written" flag. */
6540 if ((linker_section_ptr->offset & 1) == 0)
6542 bfd_put_32 (lsect->section->owner,
6543 relocation + linker_section_ptr->addend,
6544 lsect->section->contents + linker_section_ptr->offset);
6545 linker_section_ptr->offset += 1;
6548 relocation = (lsect->section->output_section->vma
6549 + lsect->section->output_offset
6550 + linker_section_ptr->offset - 1
6551 - SYM_VAL (lsect->sym));
6553 #ifdef DEBUG
6554 fprintf (stderr,
6555 "Finish pointer in linker section %s, offset = %ld (0x%lx)\n",
6556 lsect->name, (long) relocation, (long) relocation);
6557 #endif
6559 return relocation;
6562 #define PPC_LO(v) ((v) & 0xffff)
6563 #define PPC_HI(v) (((v) >> 16) & 0xffff)
6564 #define PPC_HA(v) PPC_HI ((v) + 0x8000)
6566 static void
6567 write_glink_stub (struct plt_entry *ent, asection *plt_sec, unsigned char *p,
6568 struct bfd_link_info *info)
6570 struct ppc_elf_link_hash_table *htab = ppc_elf_hash_table (info);
6571 bfd *output_bfd = info->output_bfd;
6572 bfd_vma plt;
6574 plt = ((ent->plt.offset & ~1)
6575 + plt_sec->output_section->vma
6576 + plt_sec->output_offset);
6578 if (info->shared)
6580 bfd_vma got = 0;
6582 if (ent->addend >= 32768)
6583 got = (ent->addend
6584 + ent->sec->output_section->vma
6585 + ent->sec->output_offset);
6586 else if (htab->elf.hgot != NULL)
6587 got = SYM_VAL (htab->elf.hgot);
6589 plt -= got;
6591 if (plt + 0x8000 < 0x10000)
6593 bfd_put_32 (output_bfd, LWZ_11_30 + PPC_LO (plt), p);
6594 p += 4;
6595 bfd_put_32 (output_bfd, MTCTR_11, p);
6596 p += 4;
6597 bfd_put_32 (output_bfd, BCTR, p);
6598 p += 4;
6599 bfd_put_32 (output_bfd, NOP, p);
6600 p += 4;
6602 else
6604 bfd_put_32 (output_bfd, ADDIS_11_30 + PPC_HA (plt), p);
6605 p += 4;
6606 bfd_put_32 (output_bfd, LWZ_11_11 + PPC_LO (plt), p);
6607 p += 4;
6608 bfd_put_32 (output_bfd, MTCTR_11, p);
6609 p += 4;
6610 bfd_put_32 (output_bfd, BCTR, p);
6611 p += 4;
6614 else
6616 bfd_put_32 (output_bfd, LIS_11 + PPC_HA (plt), p);
6617 p += 4;
6618 bfd_put_32 (output_bfd, LWZ_11_11 + PPC_LO (plt), p);
6619 p += 4;
6620 bfd_put_32 (output_bfd, MTCTR_11, p);
6621 p += 4;
6622 bfd_put_32 (output_bfd, BCTR, p);
6623 p += 4;
6627 /* Return true if symbol is defined statically. */
6629 static bfd_boolean
6630 is_static_defined (struct elf_link_hash_entry *h)
6632 return ((h->root.type == bfd_link_hash_defined
6633 || h->root.type == bfd_link_hash_defweak)
6634 && h->root.u.def.section != NULL
6635 && h->root.u.def.section->output_section != NULL);
6638 /* If INSN is an opcode that may be used with an @tls operand, return
6639 the transformed insn for TLS optimisation, otherwise return 0. If
6640 REG is non-zero only match an insn with RB or RA equal to REG. */
6642 unsigned int
6643 _bfd_elf_ppc_at_tls_transform (unsigned int insn, unsigned int reg)
6645 unsigned int rtra;
6647 if ((insn & (0x3f << 26)) != 31 << 26)
6648 return 0;
6650 if (reg == 0 || ((insn >> 11) & 0x1f) == reg)
6651 rtra = insn & ((1 << 26) - (1 << 16));
6652 else if (((insn >> 16) & 0x1f) == reg)
6653 rtra = (insn & (0x1f << 21)) | ((insn & (0x1f << 11)) << 5);
6654 else
6655 return 0;
6657 if ((insn & (0x3ff << 1)) == 266 << 1)
6658 /* add -> addi. */
6659 insn = 14 << 26;
6660 else if ((insn & (0x1f << 1)) == 23 << 1
6661 && ((insn & (0x1f << 6)) < 14 << 6
6662 || ((insn & (0x1f << 6)) >= 16 << 6
6663 && (insn & (0x1f << 6)) < 24 << 6)))
6664 /* load and store indexed -> dform. */
6665 insn = (32 | ((insn >> 6) & 0x1f)) << 26;
6666 else if ((insn & (((0x1a << 5) | 0x1f) << 1)) == 21 << 1)
6667 /* ldx, ldux, stdx, stdux -> ld, ldu, std, stdu. */
6668 insn = ((58 | ((insn >> 6) & 4)) << 26) | ((insn >> 6) & 1);
6669 else if ((insn & (((0x1f << 5) | 0x1f) << 1)) == 341 << 1)
6670 /* lwax -> lwa. */
6671 insn = (58 << 26) | 2;
6672 else
6673 return 0;
6674 insn |= rtra;
6675 return insn;
6678 /* If INSN is an opcode that may be used with an @tprel operand, return
6679 the transformed insn for an undefined weak symbol, ie. with the
6680 thread pointer REG operand removed. Otherwise return 0. */
6682 unsigned int
6683 _bfd_elf_ppc_at_tprel_transform (unsigned int insn, unsigned int reg)
6685 if ((insn & (0x1f << 16)) == reg << 16
6686 && ((insn & (0x3f << 26)) == 14u << 26 /* addi */
6687 || (insn & (0x3f << 26)) == 15u << 26 /* addis */
6688 || (insn & (0x3f << 26)) == 32u << 26 /* lwz */
6689 || (insn & (0x3f << 26)) == 34u << 26 /* lbz */
6690 || (insn & (0x3f << 26)) == 36u << 26 /* stw */
6691 || (insn & (0x3f << 26)) == 38u << 26 /* stb */
6692 || (insn & (0x3f << 26)) == 40u << 26 /* lhz */
6693 || (insn & (0x3f << 26)) == 42u << 26 /* lha */
6694 || (insn & (0x3f << 26)) == 44u << 26 /* sth */
6695 || (insn & (0x3f << 26)) == 46u << 26 /* lmw */
6696 || (insn & (0x3f << 26)) == 47u << 26 /* stmw */
6697 || (insn & (0x3f << 26)) == 48u << 26 /* lfs */
6698 || (insn & (0x3f << 26)) == 50u << 26 /* lfd */
6699 || (insn & (0x3f << 26)) == 52u << 26 /* stfs */
6700 || (insn & (0x3f << 26)) == 54u << 26 /* stfd */
6701 || ((insn & (0x3f << 26)) == 58u << 26 /* lwa,ld,lmd */
6702 && (insn & 3) != 1)
6703 || ((insn & (0x3f << 26)) == 62u << 26 /* std, stmd */
6704 && ((insn & 3) == 0 || (insn & 3) == 3))))
6706 insn &= ~(0x1f << 16);
6708 else if ((insn & (0x1f << 21)) == reg << 21
6709 && ((insn & (0x3e << 26)) == 24u << 26 /* ori, oris */
6710 || (insn & (0x3e << 26)) == 26u << 26 /* xori,xoris */
6711 || (insn & (0x3e << 26)) == 28u << 26 /* andi,andis */))
6713 insn &= ~(0x1f << 21);
6714 insn |= (insn & (0x1f << 16)) << 5;
6715 if ((insn & (0x3e << 26)) == 26 << 26 /* xori,xoris */)
6716 insn -= 2 >> 26; /* convert to ori,oris */
6718 else
6719 insn = 0;
6720 return insn;
6723 /* The RELOCATE_SECTION function is called by the ELF backend linker
6724 to handle the relocations for a section.
6726 The relocs are always passed as Rela structures; if the section
6727 actually uses Rel structures, the r_addend field will always be
6728 zero.
6730 This function is responsible for adjust the section contents as
6731 necessary, and (if using Rela relocs and generating a
6732 relocatable output file) adjusting the reloc addend as
6733 necessary.
6735 This function does not have to worry about setting the reloc
6736 address or the reloc symbol index.
6738 LOCAL_SYMS is a pointer to the swapped in local symbols.
6740 LOCAL_SECTIONS is an array giving the section in the input file
6741 corresponding to the st_shndx field of each local symbol.
6743 The global hash table entry for the global symbols can be found
6744 via elf_sym_hashes (input_bfd).
6746 When generating relocatable output, this function must handle
6747 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
6748 going to be the section symbol corresponding to the output
6749 section, which means that the addend must be adjusted
6750 accordingly. */
6752 static bfd_boolean
6753 ppc_elf_relocate_section (bfd *output_bfd,
6754 struct bfd_link_info *info,
6755 bfd *input_bfd,
6756 asection *input_section,
6757 bfd_byte *contents,
6758 Elf_Internal_Rela *relocs,
6759 Elf_Internal_Sym *local_syms,
6760 asection **local_sections)
6762 Elf_Internal_Shdr *symtab_hdr;
6763 struct elf_link_hash_entry **sym_hashes;
6764 struct ppc_elf_link_hash_table *htab;
6765 Elf_Internal_Rela *rel;
6766 Elf_Internal_Rela *relend;
6767 Elf_Internal_Rela outrel;
6768 asection *got2, *sreloc = NULL;
6769 bfd_vma *local_got_offsets;
6770 bfd_boolean ret = TRUE;
6771 bfd_vma d_offset = (bfd_big_endian (output_bfd) ? 2 : 0);
6772 bfd_boolean is_vxworks_tls;
6774 #ifdef DEBUG
6775 _bfd_error_handler ("ppc_elf_relocate_section called for %B section %A, "
6776 "%ld relocations%s",
6777 input_bfd, input_section,
6778 (long) input_section->reloc_count,
6779 (info->relocatable) ? " (relocatable)" : "");
6780 #endif
6782 got2 = bfd_get_section_by_name (input_bfd, ".got2");
6784 /* Initialize howto table if not already done. */
6785 if (!ppc_elf_howto_table[R_PPC_ADDR32])
6786 ppc_elf_howto_init ();
6788 htab = ppc_elf_hash_table (info);
6789 local_got_offsets = elf_local_got_offsets (input_bfd);
6790 symtab_hdr = &elf_symtab_hdr (input_bfd);
6791 sym_hashes = elf_sym_hashes (input_bfd);
6792 /* We have to handle relocations in vxworks .tls_vars sections
6793 specially, because the dynamic loader is 'weird'. */
6794 is_vxworks_tls = (htab->is_vxworks && info->shared
6795 && !strcmp (input_section->output_section->name,
6796 ".tls_vars"));
6797 rel = relocs;
6798 relend = relocs + input_section->reloc_count;
6799 for (; rel < relend; rel++)
6801 enum elf_ppc_reloc_type r_type;
6802 bfd_vma addend;
6803 bfd_reloc_status_type r;
6804 Elf_Internal_Sym *sym;
6805 asection *sec;
6806 struct elf_link_hash_entry *h;
6807 const char *sym_name;
6808 reloc_howto_type *howto;
6809 unsigned long r_symndx;
6810 bfd_vma relocation;
6811 bfd_vma branch_bit, from;
6812 bfd_boolean unresolved_reloc;
6813 bfd_boolean warned;
6814 unsigned int tls_type, tls_mask, tls_gd;
6815 struct plt_entry **ifunc;
6817 r_type = ELF32_R_TYPE (rel->r_info);
6818 sym = NULL;
6819 sec = NULL;
6820 h = NULL;
6821 unresolved_reloc = FALSE;
6822 warned = FALSE;
6823 r_symndx = ELF32_R_SYM (rel->r_info);
6825 if (r_symndx < symtab_hdr->sh_info)
6827 sym = local_syms + r_symndx;
6828 sec = local_sections[r_symndx];
6829 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym, sec);
6831 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
6833 else
6835 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
6836 r_symndx, symtab_hdr, sym_hashes,
6837 h, sec, relocation,
6838 unresolved_reloc, warned);
6840 sym_name = h->root.root.string;
6843 if (sec != NULL && elf_discarded_section (sec))
6845 /* For relocs against symbols from removed linkonce sections,
6846 or sections discarded by a linker script, we just want the
6847 section contents zeroed. Avoid any special processing. */
6848 howto = NULL;
6849 if (r_type < R_PPC_max)
6850 howto = ppc_elf_howto_table[r_type];
6851 RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
6852 rel, relend, howto, contents);
6855 if (info->relocatable)
6857 if (got2 != NULL
6858 && r_type == R_PPC_PLTREL24
6859 && rel->r_addend >= 32768)
6861 /* R_PPC_PLTREL24 is rather special. If non-zero, the
6862 addend specifies the GOT pointer offset within .got2. */
6863 rel->r_addend += got2->output_offset;
6865 continue;
6868 /* TLS optimizations. Replace instruction sequences and relocs
6869 based on information we collected in tls_optimize. We edit
6870 RELOCS so that --emit-relocs will output something sensible
6871 for the final instruction stream. */
6872 tls_mask = 0;
6873 tls_gd = 0;
6874 if (h != NULL)
6875 tls_mask = ((struct ppc_elf_link_hash_entry *) h)->tls_mask;
6876 else if (local_got_offsets != NULL)
6878 struct plt_entry **local_plt;
6879 char *lgot_masks;
6880 local_plt
6881 = (struct plt_entry **) (local_got_offsets + symtab_hdr->sh_info);
6882 lgot_masks = (char *) (local_plt + symtab_hdr->sh_info);
6883 tls_mask = lgot_masks[r_symndx];
6886 /* Ensure reloc mapping code below stays sane. */
6887 if ((R_PPC_GOT_TLSLD16 & 3) != (R_PPC_GOT_TLSGD16 & 3)
6888 || (R_PPC_GOT_TLSLD16_LO & 3) != (R_PPC_GOT_TLSGD16_LO & 3)
6889 || (R_PPC_GOT_TLSLD16_HI & 3) != (R_PPC_GOT_TLSGD16_HI & 3)
6890 || (R_PPC_GOT_TLSLD16_HA & 3) != (R_PPC_GOT_TLSGD16_HA & 3)
6891 || (R_PPC_GOT_TLSLD16 & 3) != (R_PPC_GOT_TPREL16 & 3)
6892 || (R_PPC_GOT_TLSLD16_LO & 3) != (R_PPC_GOT_TPREL16_LO & 3)
6893 || (R_PPC_GOT_TLSLD16_HI & 3) != (R_PPC_GOT_TPREL16_HI & 3)
6894 || (R_PPC_GOT_TLSLD16_HA & 3) != (R_PPC_GOT_TPREL16_HA & 3))
6895 abort ();
6896 switch (r_type)
6898 default:
6899 break;
6901 case R_PPC_GOT_TPREL16:
6902 case R_PPC_GOT_TPREL16_LO:
6903 if ((tls_mask & TLS_TLS) != 0
6904 && (tls_mask & TLS_TPREL) == 0)
6906 bfd_vma insn;
6908 insn = bfd_get_32 (output_bfd, contents + rel->r_offset - d_offset);
6909 insn &= 31 << 21;
6910 insn |= 0x3c020000; /* addis 0,2,0 */
6911 bfd_put_32 (output_bfd, insn, contents + rel->r_offset - d_offset);
6912 r_type = R_PPC_TPREL16_HA;
6913 rel->r_info = ELF32_R_INFO (r_symndx, r_type);
6915 break;
6917 case R_PPC_TLS:
6918 if ((tls_mask & TLS_TLS) != 0
6919 && (tls_mask & TLS_TPREL) == 0)
6921 bfd_vma insn;
6923 insn = bfd_get_32 (output_bfd, contents + rel->r_offset);
6924 insn = _bfd_elf_ppc_at_tls_transform (insn, 2);
6925 if (insn == 0)
6926 abort ();
6927 bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
6928 r_type = R_PPC_TPREL16_LO;
6929 rel->r_info = ELF32_R_INFO (r_symndx, r_type);
6931 /* Was PPC_TLS which sits on insn boundary, now
6932 PPC_TPREL16_LO which is at low-order half-word. */
6933 rel->r_offset += d_offset;
6935 break;
6937 case R_PPC_GOT_TLSGD16_HI:
6938 case R_PPC_GOT_TLSGD16_HA:
6939 tls_gd = TLS_TPRELGD;
6940 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0)
6941 goto tls_gdld_hi;
6942 break;
6944 case R_PPC_GOT_TLSLD16_HI:
6945 case R_PPC_GOT_TLSLD16_HA:
6946 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0)
6948 tls_gdld_hi:
6949 if ((tls_mask & tls_gd) != 0)
6950 r_type = (((r_type - (R_PPC_GOT_TLSGD16 & 3)) & 3)
6951 + R_PPC_GOT_TPREL16);
6952 else
6954 bfd_put_32 (output_bfd, NOP, contents + rel->r_offset);
6955 rel->r_offset -= d_offset;
6956 r_type = R_PPC_NONE;
6958 rel->r_info = ELF32_R_INFO (r_symndx, r_type);
6960 break;
6962 case R_PPC_GOT_TLSGD16:
6963 case R_PPC_GOT_TLSGD16_LO:
6964 tls_gd = TLS_TPRELGD;
6965 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0)
6966 goto tls_ldgd_opt;
6967 break;
6969 case R_PPC_GOT_TLSLD16:
6970 case R_PPC_GOT_TLSLD16_LO:
6971 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0)
6973 unsigned int insn1, insn2;
6974 bfd_vma offset;
6976 tls_ldgd_opt:
6977 offset = (bfd_vma) -1;
6978 /* If not using the newer R_PPC_TLSGD/LD to mark
6979 __tls_get_addr calls, we must trust that the call
6980 stays with its arg setup insns, ie. that the next
6981 reloc is the __tls_get_addr call associated with
6982 the current reloc. Edit both insns. */
6983 if (input_section->has_tls_get_addr_call
6984 && rel + 1 < relend
6985 && branch_reloc_hash_match (input_bfd, rel + 1,
6986 htab->tls_get_addr))
6987 offset = rel[1].r_offset;
6988 if ((tls_mask & tls_gd) != 0)
6990 /* IE */
6991 insn1 = bfd_get_32 (output_bfd,
6992 contents + rel->r_offset - d_offset);
6993 insn1 &= (1 << 26) - 1;
6994 insn1 |= 32 << 26; /* lwz */
6995 if (offset != (bfd_vma) -1)
6997 rel[1].r_info = ELF32_R_INFO (STN_UNDEF, R_PPC_NONE);
6998 insn2 = 0x7c631214; /* add 3,3,2 */
6999 bfd_put_32 (output_bfd, insn2, contents + offset);
7001 r_type = (((r_type - (R_PPC_GOT_TLSGD16 & 3)) & 3)
7002 + R_PPC_GOT_TPREL16);
7003 rel->r_info = ELF32_R_INFO (r_symndx, r_type);
7005 else
7007 /* LE */
7008 insn1 = 0x3c620000; /* addis 3,2,0 */
7009 if (tls_gd == 0)
7011 /* Was an LD reloc. */
7012 for (r_symndx = 0;
7013 r_symndx < symtab_hdr->sh_info;
7014 r_symndx++)
7015 if (local_sections[r_symndx] == sec)
7016 break;
7017 if (r_symndx >= symtab_hdr->sh_info)
7018 r_symndx = STN_UNDEF;
7019 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
7020 if (r_symndx != STN_UNDEF)
7021 rel->r_addend -= (local_syms[r_symndx].st_value
7022 + sec->output_offset
7023 + sec->output_section->vma);
7025 r_type = R_PPC_TPREL16_HA;
7026 rel->r_info = ELF32_R_INFO (r_symndx, r_type);
7027 if (offset != (bfd_vma) -1)
7029 rel[1].r_info = ELF32_R_INFO (r_symndx, R_PPC_TPREL16_LO);
7030 rel[1].r_offset = offset + d_offset;
7031 rel[1].r_addend = rel->r_addend;
7032 insn2 = 0x38630000; /* addi 3,3,0 */
7033 bfd_put_32 (output_bfd, insn2, contents + offset);
7036 bfd_put_32 (output_bfd, insn1,
7037 contents + rel->r_offset - d_offset);
7038 if (tls_gd == 0)
7040 /* We changed the symbol on an LD reloc. Start over
7041 in order to get h, sym, sec etc. right. */
7042 rel--;
7043 continue;
7046 break;
7048 case R_PPC_TLSGD:
7049 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0)
7051 unsigned int insn2;
7052 bfd_vma offset = rel->r_offset;
7054 if ((tls_mask & TLS_TPRELGD) != 0)
7056 /* IE */
7057 r_type = R_PPC_NONE;
7058 insn2 = 0x7c631214; /* add 3,3,2 */
7060 else
7062 /* LE */
7063 r_type = R_PPC_TPREL16_LO;
7064 rel->r_offset += d_offset;
7065 insn2 = 0x38630000; /* addi 3,3,0 */
7067 rel->r_info = ELF32_R_INFO (r_symndx, r_type);
7068 bfd_put_32 (output_bfd, insn2, contents + offset);
7069 /* Zap the reloc on the _tls_get_addr call too. */
7070 BFD_ASSERT (offset == rel[1].r_offset);
7071 rel[1].r_info = ELF32_R_INFO (STN_UNDEF, R_PPC_NONE);
7073 break;
7075 case R_PPC_TLSLD:
7076 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0)
7078 unsigned int insn2;
7080 for (r_symndx = 0;
7081 r_symndx < symtab_hdr->sh_info;
7082 r_symndx++)
7083 if (local_sections[r_symndx] == sec)
7084 break;
7085 if (r_symndx >= symtab_hdr->sh_info)
7086 r_symndx = STN_UNDEF;
7087 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
7088 if (r_symndx != STN_UNDEF)
7089 rel->r_addend -= (local_syms[r_symndx].st_value
7090 + sec->output_offset
7091 + sec->output_section->vma);
7093 rel->r_info = ELF32_R_INFO (r_symndx, R_PPC_TPREL16_LO);
7094 rel->r_offset += d_offset;
7095 insn2 = 0x38630000; /* addi 3,3,0 */
7096 bfd_put_32 (output_bfd, insn2,
7097 contents + rel->r_offset - d_offset);
7098 /* Zap the reloc on the _tls_get_addr call too. */
7099 BFD_ASSERT (rel->r_offset - d_offset == rel[1].r_offset);
7100 rel[1].r_info = ELF32_R_INFO (STN_UNDEF, R_PPC_NONE);
7101 rel--;
7102 continue;
7104 break;
7107 /* Handle other relocations that tweak non-addend part of insn. */
7108 branch_bit = 0;
7109 switch (r_type)
7111 default:
7112 break;
7114 /* Branch taken prediction relocations. */
7115 case R_PPC_ADDR14_BRTAKEN:
7116 case R_PPC_REL14_BRTAKEN:
7117 branch_bit = BRANCH_PREDICT_BIT;
7118 /* Fall thru */
7120 /* Branch not taken prediction relocations. */
7121 case R_PPC_ADDR14_BRNTAKEN:
7122 case R_PPC_REL14_BRNTAKEN:
7124 bfd_vma insn;
7126 insn = bfd_get_32 (output_bfd, contents + rel->r_offset);
7127 insn &= ~BRANCH_PREDICT_BIT;
7128 insn |= branch_bit;
7130 from = (rel->r_offset
7131 + input_section->output_offset
7132 + input_section->output_section->vma);
7134 /* Invert 'y' bit if not the default. */
7135 if ((bfd_signed_vma) (relocation + rel->r_addend - from) < 0)
7136 insn ^= BRANCH_PREDICT_BIT;
7138 bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
7139 break;
7143 ifunc = NULL;
7144 if (!htab->is_vxworks)
7146 struct plt_entry *ent;
7148 if (h != NULL)
7150 if (h->type == STT_GNU_IFUNC)
7151 ifunc = &h->plt.plist;
7153 else if (local_got_offsets != NULL
7154 && ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
7156 struct plt_entry **local_plt;
7158 local_plt = (struct plt_entry **) (local_got_offsets
7159 + symtab_hdr->sh_info);
7160 ifunc = local_plt + r_symndx;
7163 ent = NULL;
7164 if (ifunc != NULL
7165 && (!info->shared
7166 || is_branch_reloc (r_type)))
7168 addend = 0;
7169 if (r_type == R_PPC_PLTREL24 && info->shared)
7170 addend = rel->r_addend;
7171 ent = find_plt_ent (ifunc, got2, addend);
7173 if (ent != NULL)
7175 if (h == NULL && (ent->plt.offset & 1) == 0)
7177 Elf_Internal_Rela rela;
7178 bfd_byte *loc;
7180 rela.r_offset = (htab->iplt->output_section->vma
7181 + htab->iplt->output_offset
7182 + ent->plt.offset);
7183 rela.r_info = ELF32_R_INFO (0, R_PPC_IRELATIVE);
7184 rela.r_addend = relocation;
7185 loc = htab->reliplt->contents;
7186 loc += (htab->reliplt->reloc_count++
7187 * sizeof (Elf32_External_Rela));
7188 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
7190 ent->plt.offset |= 1;
7192 if (h == NULL && (ent->glink_offset & 1) == 0)
7194 unsigned char *p = ((unsigned char *) htab->glink->contents
7195 + ent->glink_offset);
7196 write_glink_stub (ent, htab->iplt, p, info);
7197 ent->glink_offset |= 1;
7200 unresolved_reloc = FALSE;
7201 if (htab->plt_type == PLT_NEW
7202 || !htab->elf.dynamic_sections_created
7203 || h == NULL)
7204 relocation = (htab->glink->output_section->vma
7205 + htab->glink->output_offset
7206 + (ent->glink_offset & ~1));
7207 else
7208 relocation = (htab->plt->output_section->vma
7209 + htab->plt->output_offset
7210 + ent->plt.offset);
7214 addend = rel->r_addend;
7215 tls_type = 0;
7216 howto = NULL;
7217 if (r_type < R_PPC_max)
7218 howto = ppc_elf_howto_table[r_type];
7219 switch (r_type)
7221 default:
7222 (*_bfd_error_handler)
7223 (_("%B: unknown relocation type %d for symbol %s"),
7224 input_bfd, (int) r_type, sym_name);
7226 bfd_set_error (bfd_error_bad_value);
7227 ret = FALSE;
7228 continue;
7230 case R_PPC_NONE:
7231 case R_PPC_TLS:
7232 case R_PPC_TLSGD:
7233 case R_PPC_TLSLD:
7234 case R_PPC_EMB_MRKREF:
7235 case R_PPC_GNU_VTINHERIT:
7236 case R_PPC_GNU_VTENTRY:
7237 continue;
7239 /* GOT16 relocations. Like an ADDR16 using the symbol's
7240 address in the GOT as relocation value instead of the
7241 symbol's value itself. Also, create a GOT entry for the
7242 symbol and put the symbol value there. */
7243 case R_PPC_GOT_TLSGD16:
7244 case R_PPC_GOT_TLSGD16_LO:
7245 case R_PPC_GOT_TLSGD16_HI:
7246 case R_PPC_GOT_TLSGD16_HA:
7247 tls_type = TLS_TLS | TLS_GD;
7248 goto dogot;
7250 case R_PPC_GOT_TLSLD16:
7251 case R_PPC_GOT_TLSLD16_LO:
7252 case R_PPC_GOT_TLSLD16_HI:
7253 case R_PPC_GOT_TLSLD16_HA:
7254 tls_type = TLS_TLS | TLS_LD;
7255 goto dogot;
7257 case R_PPC_GOT_TPREL16:
7258 case R_PPC_GOT_TPREL16_LO:
7259 case R_PPC_GOT_TPREL16_HI:
7260 case R_PPC_GOT_TPREL16_HA:
7261 tls_type = TLS_TLS | TLS_TPREL;
7262 goto dogot;
7264 case R_PPC_GOT_DTPREL16:
7265 case R_PPC_GOT_DTPREL16_LO:
7266 case R_PPC_GOT_DTPREL16_HI:
7267 case R_PPC_GOT_DTPREL16_HA:
7268 tls_type = TLS_TLS | TLS_DTPREL;
7269 goto dogot;
7271 case R_PPC_GOT16:
7272 case R_PPC_GOT16_LO:
7273 case R_PPC_GOT16_HI:
7274 case R_PPC_GOT16_HA:
7275 tls_mask = 0;
7276 dogot:
7278 /* Relocation is to the entry for this symbol in the global
7279 offset table. */
7280 bfd_vma off;
7281 bfd_vma *offp;
7282 unsigned long indx;
7284 if (htab->got == NULL)
7285 abort ();
7287 indx = 0;
7288 if (tls_type == (TLS_TLS | TLS_LD)
7289 && (h == NULL
7290 || !h->def_dynamic))
7291 offp = &htab->tlsld_got.offset;
7292 else if (h != NULL)
7294 bfd_boolean dyn;
7295 dyn = htab->elf.dynamic_sections_created;
7296 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
7297 || (info->shared
7298 && SYMBOL_REFERENCES_LOCAL (info, h)))
7299 /* This is actually a static link, or it is a
7300 -Bsymbolic link and the symbol is defined
7301 locally, or the symbol was forced to be local
7302 because of a version file. */
7304 else
7306 indx = h->dynindx;
7307 unresolved_reloc = FALSE;
7309 offp = &h->got.offset;
7311 else
7313 if (local_got_offsets == NULL)
7314 abort ();
7315 offp = &local_got_offsets[r_symndx];
7318 /* The offset must always be a multiple of 4. We use the
7319 least significant bit to record whether we have already
7320 processed this entry. */
7321 off = *offp;
7322 if ((off & 1) != 0)
7323 off &= ~1;
7324 else
7326 unsigned int tls_m = (tls_mask
7327 & (TLS_LD | TLS_GD | TLS_DTPREL
7328 | TLS_TPREL | TLS_TPRELGD));
7330 if (offp == &htab->tlsld_got.offset)
7331 tls_m = TLS_LD;
7332 else if (h == NULL
7333 || !h->def_dynamic)
7334 tls_m &= ~TLS_LD;
7336 /* We might have multiple got entries for this sym.
7337 Initialize them all. */
7340 int tls_ty = 0;
7342 if ((tls_m & TLS_LD) != 0)
7344 tls_ty = TLS_TLS | TLS_LD;
7345 tls_m &= ~TLS_LD;
7347 else if ((tls_m & TLS_GD) != 0)
7349 tls_ty = TLS_TLS | TLS_GD;
7350 tls_m &= ~TLS_GD;
7352 else if ((tls_m & TLS_DTPREL) != 0)
7354 tls_ty = TLS_TLS | TLS_DTPREL;
7355 tls_m &= ~TLS_DTPREL;
7357 else if ((tls_m & (TLS_TPREL | TLS_TPRELGD)) != 0)
7359 tls_ty = TLS_TLS | TLS_TPREL;
7360 tls_m = 0;
7363 /* Generate relocs for the dynamic linker. */
7364 if ((info->shared || indx != 0)
7365 && (offp == &htab->tlsld_got.offset
7366 || h == NULL
7367 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
7368 || h->root.type != bfd_link_hash_undefweak))
7370 asection *rsec = htab->relgot;
7371 bfd_byte * loc;
7373 outrel.r_offset = (htab->got->output_section->vma
7374 + htab->got->output_offset
7375 + off);
7376 outrel.r_addend = 0;
7377 if (tls_ty & (TLS_LD | TLS_GD))
7379 outrel.r_info = ELF32_R_INFO (indx, R_PPC_DTPMOD32);
7380 if (tls_ty == (TLS_TLS | TLS_GD))
7382 loc = rsec->contents;
7383 loc += (rsec->reloc_count++
7384 * sizeof (Elf32_External_Rela));
7385 bfd_elf32_swap_reloca_out (output_bfd,
7386 &outrel, loc);
7387 outrel.r_offset += 4;
7388 outrel.r_info
7389 = ELF32_R_INFO (indx, R_PPC_DTPREL32);
7392 else if (tls_ty == (TLS_TLS | TLS_DTPREL))
7393 outrel.r_info = ELF32_R_INFO (indx, R_PPC_DTPREL32);
7394 else if (tls_ty == (TLS_TLS | TLS_TPREL))
7395 outrel.r_info = ELF32_R_INFO (indx, R_PPC_TPREL32);
7396 else if (indx != 0)
7397 outrel.r_info = ELF32_R_INFO (indx, R_PPC_GLOB_DAT);
7398 else if (ifunc != NULL)
7399 outrel.r_info = ELF32_R_INFO (0, R_PPC_IRELATIVE);
7400 else
7401 outrel.r_info = ELF32_R_INFO (0, R_PPC_RELATIVE);
7402 if (indx == 0 && tls_ty != (TLS_TLS | TLS_LD))
7404 outrel.r_addend += relocation;
7405 if (tls_ty & (TLS_GD | TLS_DTPREL | TLS_TPREL))
7406 outrel.r_addend -= htab->elf.tls_sec->vma;
7408 loc = rsec->contents;
7409 loc += (rsec->reloc_count++
7410 * sizeof (Elf32_External_Rela));
7411 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
7414 /* Init the .got section contents if we're not
7415 emitting a reloc. */
7416 else
7418 bfd_vma value = relocation;
7420 if (tls_ty == (TLS_TLS | TLS_LD))
7421 value = 1;
7422 else if (tls_ty != 0)
7424 value -= htab->elf.tls_sec->vma + DTP_OFFSET;
7425 if (tls_ty == (TLS_TLS | TLS_TPREL))
7426 value += DTP_OFFSET - TP_OFFSET;
7428 if (tls_ty == (TLS_TLS | TLS_GD))
7430 bfd_put_32 (output_bfd, value,
7431 htab->got->contents + off + 4);
7432 value = 1;
7435 bfd_put_32 (output_bfd, value,
7436 htab->got->contents + off);
7439 off += 4;
7440 if (tls_ty & (TLS_LD | TLS_GD))
7441 off += 4;
7443 while (tls_m != 0);
7445 off = *offp;
7446 *offp = off | 1;
7449 if (off >= (bfd_vma) -2)
7450 abort ();
7452 if ((tls_type & TLS_TLS) != 0)
7454 if (tls_type != (TLS_TLS | TLS_LD))
7456 if ((tls_mask & TLS_LD) != 0
7457 && !(h == NULL
7458 || !h->def_dynamic))
7459 off += 8;
7460 if (tls_type != (TLS_TLS | TLS_GD))
7462 if ((tls_mask & TLS_GD) != 0)
7463 off += 8;
7464 if (tls_type != (TLS_TLS | TLS_DTPREL))
7466 if ((tls_mask & TLS_DTPREL) != 0)
7467 off += 4;
7473 relocation = (htab->got->output_section->vma
7474 + htab->got->output_offset
7475 + off
7476 - SYM_VAL (htab->elf.hgot));
7478 /* Addends on got relocations don't make much sense.
7479 x+off@got is actually x@got+off, and since the got is
7480 generated by a hash table traversal, the value in the
7481 got at entry m+n bears little relation to the entry m. */
7482 if (addend != 0)
7483 (*_bfd_error_handler)
7484 (_("%B(%A+0x%lx): non-zero addend on %s reloc against `%s'"),
7485 input_bfd,
7486 input_section,
7487 (long) rel->r_offset,
7488 howto->name,
7489 sym_name);
7491 break;
7493 /* Relocations that need no special processing. */
7494 case R_PPC_LOCAL24PC:
7495 /* It makes no sense to point a local relocation
7496 at a symbol not in this object. */
7497 if (unresolved_reloc)
7499 if (! (*info->callbacks->undefined_symbol) (info,
7500 h->root.root.string,
7501 input_bfd,
7502 input_section,
7503 rel->r_offset,
7504 TRUE))
7505 return FALSE;
7506 continue;
7508 break;
7510 case R_PPC_DTPREL16:
7511 case R_PPC_DTPREL16_LO:
7512 case R_PPC_DTPREL16_HI:
7513 case R_PPC_DTPREL16_HA:
7514 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
7515 break;
7517 /* Relocations that may need to be propagated if this is a shared
7518 object. */
7519 case R_PPC_TPREL16:
7520 case R_PPC_TPREL16_LO:
7521 case R_PPC_TPREL16_HI:
7522 case R_PPC_TPREL16_HA:
7523 if (h != NULL
7524 && h->root.type == bfd_link_hash_undefweak
7525 && h->dynindx == -1)
7527 /* Make this relocation against an undefined weak symbol
7528 resolve to zero. This is really just a tweak, since
7529 code using weak externs ought to check that they are
7530 defined before using them. */
7531 bfd_byte *p = contents + rel->r_offset - d_offset;
7532 unsigned int insn = bfd_get_32 (output_bfd, p);
7533 insn = _bfd_elf_ppc_at_tprel_transform (insn, 2);
7534 if (insn != 0)
7535 bfd_put_32 (output_bfd, insn, p);
7536 break;
7538 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
7539 /* The TPREL16 relocs shouldn't really be used in shared
7540 libs as they will result in DT_TEXTREL being set, but
7541 support them anyway. */
7542 goto dodyn;
7544 case R_PPC_TPREL32:
7545 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
7546 goto dodyn;
7548 case R_PPC_DTPREL32:
7549 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
7550 goto dodyn;
7552 case R_PPC_DTPMOD32:
7553 relocation = 1;
7554 addend = 0;
7555 goto dodyn;
7557 case R_PPC_REL16:
7558 case R_PPC_REL16_LO:
7559 case R_PPC_REL16_HI:
7560 case R_PPC_REL16_HA:
7561 break;
7563 case R_PPC_REL32:
7564 if (h == NULL || h == htab->elf.hgot)
7565 break;
7566 /* fall through */
7568 case R_PPC_ADDR32:
7569 case R_PPC_ADDR16:
7570 case R_PPC_ADDR16_LO:
7571 case R_PPC_ADDR16_HI:
7572 case R_PPC_ADDR16_HA:
7573 case R_PPC_UADDR32:
7574 case R_PPC_UADDR16:
7575 goto dodyn;
7577 case R_PPC_REL24:
7578 case R_PPC_REL14:
7579 case R_PPC_REL14_BRTAKEN:
7580 case R_PPC_REL14_BRNTAKEN:
7581 /* If these relocations are not to a named symbol, they can be
7582 handled right here, no need to bother the dynamic linker. */
7583 if (SYMBOL_CALLS_LOCAL (info, h)
7584 || h == htab->elf.hgot)
7585 break;
7586 /* fall through */
7588 case R_PPC_ADDR24:
7589 case R_PPC_ADDR14:
7590 case R_PPC_ADDR14_BRTAKEN:
7591 case R_PPC_ADDR14_BRNTAKEN:
7592 if (h != NULL && !info->shared)
7593 break;
7594 /* fall through */
7596 dodyn:
7597 if ((input_section->flags & SEC_ALLOC) == 0
7598 || is_vxworks_tls)
7599 break;
7601 if ((info->shared
7602 && !(h != NULL
7603 && ((h->root.type == bfd_link_hash_undefined
7604 && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
7605 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
7606 || (h->root.type == bfd_link_hash_undefweak
7607 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)))
7608 && (must_be_dyn_reloc (info, r_type)
7609 || !SYMBOL_CALLS_LOCAL (info, h)))
7610 || (ELIMINATE_COPY_RELOCS
7611 && !info->shared
7612 && h != NULL
7613 && h->dynindx != -1
7614 && !h->non_got_ref
7615 && !h->def_regular))
7617 int skip;
7618 bfd_byte * loc;
7619 #ifdef DEBUG
7620 fprintf (stderr, "ppc_elf_relocate_section needs to "
7621 "create relocation for %s\n",
7622 (h && h->root.root.string
7623 ? h->root.root.string : "<unknown>"));
7624 #endif
7626 /* When generating a shared object, these relocations
7627 are copied into the output file to be resolved at run
7628 time. */
7629 if (sreloc == NULL)
7631 sreloc = elf_section_data (input_section)->sreloc;
7632 if (!htab->elf.dynamic_sections_created)
7633 sreloc = htab->reliplt;
7634 if (sreloc == NULL)
7635 return FALSE;
7638 skip = 0;
7639 outrel.r_offset = _bfd_elf_section_offset (output_bfd, info,
7640 input_section,
7641 rel->r_offset);
7642 if (outrel.r_offset == (bfd_vma) -1
7643 || outrel.r_offset == (bfd_vma) -2)
7644 skip = (int) outrel.r_offset;
7645 outrel.r_offset += (input_section->output_section->vma
7646 + input_section->output_offset);
7648 if (skip)
7649 memset (&outrel, 0, sizeof outrel);
7650 else if ((h != NULL
7651 && (h->root.type == bfd_link_hash_undefined
7652 || h->root.type == bfd_link_hash_undefweak))
7653 || !SYMBOL_REFERENCES_LOCAL (info, h))
7655 unresolved_reloc = FALSE;
7656 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
7657 outrel.r_addend = rel->r_addend;
7659 else
7661 outrel.r_addend = relocation + rel->r_addend;
7663 if (r_type != R_PPC_ADDR32)
7665 long indx = 0;
7667 if (ifunc != NULL)
7669 /* If we get here when building a static
7670 executable, then the libc startup function
7671 responsible for applying indirect function
7672 relocations is going to complain about
7673 the reloc type.
7674 If we get here when building a dynamic
7675 executable, it will be because we have
7676 a text relocation. The dynamic loader
7677 will set the text segment writable and
7678 non-executable to apply text relocations.
7679 So we'll segfault when trying to run the
7680 indirection function to resolve the reloc. */
7681 (*_bfd_error_handler)
7682 (_("%B(%A+0x%lx): relocation %s for indirect "
7683 "function %s unsupported"),
7684 input_bfd,
7685 input_section,
7686 (long) rel->r_offset,
7687 howto->name,
7688 sym_name);
7689 ret = FALSE;
7691 else if (r_symndx == STN_UNDEF || bfd_is_abs_section (sec))
7693 else if (sec == NULL || sec->owner == NULL)
7695 bfd_set_error (bfd_error_bad_value);
7696 ret = FALSE;
7698 else
7700 asection *osec;
7702 /* We are turning this relocation into one
7703 against a section symbol. It would be
7704 proper to subtract the symbol's value,
7705 osec->vma, from the emitted reloc addend,
7706 but ld.so expects buggy relocs.
7707 FIXME: Why not always use a zero index? */
7708 osec = sec->output_section;
7709 indx = elf_section_data (osec)->dynindx;
7710 if (indx == 0)
7712 osec = htab->elf.text_index_section;
7713 indx = elf_section_data (osec)->dynindx;
7715 BFD_ASSERT (indx != 0);
7716 #ifdef DEBUG
7717 if (indx == 0)
7718 printf ("indx=%ld section=%s flags=%08x name=%s\n",
7719 indx, osec->name, osec->flags,
7720 h->root.root.string);
7721 #endif
7724 outrel.r_info = ELF32_R_INFO (indx, r_type);
7726 else if (ifunc != NULL)
7727 outrel.r_info = ELF32_R_INFO (0, R_PPC_IRELATIVE);
7728 else
7729 outrel.r_info = ELF32_R_INFO (0, R_PPC_RELATIVE);
7732 loc = sreloc->contents;
7733 loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rela);
7734 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
7736 if (skip == -1)
7737 continue;
7739 /* This reloc will be computed at runtime. We clear the memory
7740 so that it contains predictable value. */
7741 if (! skip
7742 && ((input_section->flags & SEC_ALLOC) != 0
7743 || ELF32_R_TYPE (outrel.r_info) != R_PPC_RELATIVE))
7745 relocation = howto->pc_relative ? outrel.r_offset : 0;
7746 addend = 0;
7747 break;
7750 break;
7752 case R_PPC_RELAX_PLT:
7753 case R_PPC_RELAX_PLTREL24:
7754 if (h != NULL)
7756 struct plt_entry *ent;
7757 bfd_vma got2_addend = 0;
7759 if (r_type == R_PPC_RELAX_PLTREL24)
7761 if (info->shared)
7762 got2_addend = addend;
7763 addend = 0;
7765 ent = find_plt_ent (&h->plt.plist, got2, got2_addend);
7766 if (htab->plt_type == PLT_NEW)
7767 relocation = (htab->glink->output_section->vma
7768 + htab->glink->output_offset
7769 + ent->glink_offset);
7770 else
7771 relocation = (htab->plt->output_section->vma
7772 + htab->plt->output_offset
7773 + ent->plt.offset);
7775 /* Fall thru */
7777 case R_PPC_RELAX:
7778 if (info->shared)
7779 relocation -= (input_section->output_section->vma
7780 + input_section->output_offset
7781 + rel->r_offset - 4);
7784 unsigned long t0;
7785 unsigned long t1;
7787 t0 = bfd_get_32 (output_bfd, contents + rel->r_offset);
7788 t1 = bfd_get_32 (output_bfd, contents + rel->r_offset + 4);
7790 /* We're clearing the bits for R_PPC_ADDR16_HA
7791 and R_PPC_ADDR16_LO here. */
7792 t0 &= ~0xffff;
7793 t1 &= ~0xffff;
7795 /* t0 is HA, t1 is LO */
7796 relocation += addend;
7797 t0 |= ((relocation + 0x8000) >> 16) & 0xffff;
7798 t1 |= relocation & 0xffff;
7800 bfd_put_32 (output_bfd, t0, contents + rel->r_offset);
7801 bfd_put_32 (output_bfd, t1, contents + rel->r_offset + 4);
7803 /* Rewrite the reloc and convert one of the trailing nop
7804 relocs to describe this relocation. */
7805 BFD_ASSERT (ELF32_R_TYPE (relend[-1].r_info) == R_PPC_NONE);
7806 /* The relocs are at the bottom 2 bytes */
7807 rel[0].r_offset += 2;
7808 memmove (rel + 1, rel, (relend - rel - 1) * sizeof (*rel));
7809 rel[0].r_info = ELF32_R_INFO (r_symndx, R_PPC_ADDR16_HA);
7810 rel[1].r_offset += 4;
7811 rel[1].r_info = ELF32_R_INFO (r_symndx, R_PPC_ADDR16_LO);
7812 rel++;
7814 continue;
7816 /* Indirect .sdata relocation. */
7817 case R_PPC_EMB_SDAI16:
7818 BFD_ASSERT (htab->sdata[0].section != NULL);
7819 if (!is_static_defined (htab->sdata[0].sym))
7821 unresolved_reloc = TRUE;
7822 break;
7824 relocation
7825 = elf_finish_pointer_linker_section (input_bfd, &htab->sdata[0],
7826 h, relocation, rel);
7827 addend = 0;
7828 break;
7830 /* Indirect .sdata2 relocation. */
7831 case R_PPC_EMB_SDA2I16:
7832 BFD_ASSERT (htab->sdata[1].section != NULL);
7833 if (!is_static_defined (htab->sdata[1].sym))
7835 unresolved_reloc = TRUE;
7836 break;
7838 relocation
7839 = elf_finish_pointer_linker_section (input_bfd, &htab->sdata[1],
7840 h, relocation, rel);
7841 addend = 0;
7842 break;
7844 /* Handle the TOC16 reloc. We want to use the offset within the .got
7845 section, not the actual VMA. This is appropriate when generating
7846 an embedded ELF object, for which the .got section acts like the
7847 AIX .toc section. */
7848 case R_PPC_TOC16: /* phony GOT16 relocations */
7849 if (sec == NULL || sec->output_section == NULL)
7851 unresolved_reloc = TRUE;
7852 break;
7854 BFD_ASSERT (strcmp (bfd_get_section_name (abfd, sec), ".got") == 0
7855 || strcmp (bfd_get_section_name (abfd, sec), ".cgot") == 0);
7857 addend -= sec->output_section->vma + sec->output_offset + 0x8000;
7858 break;
7860 case R_PPC_PLTREL24:
7861 if (h == NULL || ifunc != NULL)
7862 break;
7863 /* Relocation is to the entry for this symbol in the
7864 procedure linkage table. */
7866 struct plt_entry *ent = find_plt_ent (&h->plt.plist, got2,
7867 info->shared ? addend : 0);
7868 addend = 0;
7869 if (ent == NULL
7870 || htab->plt == NULL)
7872 /* We didn't make a PLT entry for this symbol. This
7873 happens when statically linking PIC code, or when
7874 using -Bsymbolic. */
7875 break;
7878 unresolved_reloc = FALSE;
7879 if (htab->plt_type == PLT_NEW)
7880 relocation = (htab->glink->output_section->vma
7881 + htab->glink->output_offset
7882 + ent->glink_offset);
7883 else
7884 relocation = (htab->plt->output_section->vma
7885 + htab->plt->output_offset
7886 + ent->plt.offset);
7888 break;
7890 /* Relocate against _SDA_BASE_. */
7891 case R_PPC_SDAREL16:
7893 const char *name;
7894 struct elf_link_hash_entry *sda = htab->sdata[0].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, ".sdata")
7907 && (name[6] == 0 || name[6] == '.'))
7908 || (CONST_STRNEQ (name, ".sbss")
7909 && (name[5] == 0 || name[5] == '.'))))
7911 (*_bfd_error_handler)
7912 (_("%B: the target (%s) of a %s relocation is "
7913 "in the wrong output section (%s)"),
7914 input_bfd,
7915 sym_name,
7916 howto->name,
7917 name);
7920 break;
7922 /* Relocate against _SDA2_BASE_. */
7923 case R_PPC_EMB_SDA2REL:
7925 const char *name;
7926 struct elf_link_hash_entry *sda = htab->sdata[1].sym;
7928 if (sec == NULL
7929 || sec->output_section == NULL
7930 || !is_static_defined (sda))
7932 unresolved_reloc = TRUE;
7933 break;
7935 addend -= SYM_VAL (sda);
7937 name = bfd_get_section_name (abfd, sec->output_section);
7938 if (! (CONST_STRNEQ (name, ".sdata2")
7939 || CONST_STRNEQ (name, ".sbss2")))
7941 (*_bfd_error_handler)
7942 (_("%B: the target (%s) of a %s relocation is "
7943 "in the wrong output section (%s)"),
7944 input_bfd,
7945 sym_name,
7946 howto->name,
7947 name);
7950 break;
7952 /* Relocate against either _SDA_BASE_, _SDA2_BASE_, or 0. */
7953 case R_PPC_EMB_SDA21:
7954 case R_PPC_EMB_RELSDA:
7956 const char *name;
7957 int reg;
7958 struct elf_link_hash_entry *sda = NULL;
7960 if (sec == NULL || sec->output_section == NULL)
7962 unresolved_reloc = TRUE;
7963 break;
7966 name = bfd_get_section_name (abfd, sec->output_section);
7967 if (((CONST_STRNEQ (name, ".sdata")
7968 && (name[6] == 0 || name[6] == '.'))
7969 || (CONST_STRNEQ (name, ".sbss")
7970 && (name[5] == 0 || name[5] == '.'))))
7972 reg = 13;
7973 sda = htab->sdata[0].sym;
7975 else if (CONST_STRNEQ (name, ".sdata2")
7976 || CONST_STRNEQ (name, ".sbss2"))
7978 reg = 2;
7979 sda = htab->sdata[1].sym;
7981 else if (strcmp (name, ".PPC.EMB.sdata0") == 0
7982 || strcmp (name, ".PPC.EMB.sbss0") == 0)
7984 reg = 0;
7986 else
7988 (*_bfd_error_handler)
7989 (_("%B: the target (%s) of a %s relocation is "
7990 "in the wrong output section (%s)"),
7991 input_bfd,
7992 sym_name,
7993 howto->name,
7994 name);
7996 bfd_set_error (bfd_error_bad_value);
7997 ret = FALSE;
7998 continue;
8001 if (sda != NULL)
8003 if (!is_static_defined (sda))
8005 unresolved_reloc = TRUE;
8006 break;
8008 addend -= SYM_VAL (sda);
8011 if (r_type == R_PPC_EMB_SDA21)
8013 bfd_vma insn; /* Fill in register field. */
8015 insn = bfd_get_32 (output_bfd, contents + rel->r_offset);
8016 insn = (insn & ~RA_REGISTER_MASK) | (reg << RA_REGISTER_SHIFT);
8017 bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
8020 break;
8022 /* Relocate against the beginning of the section. */
8023 case R_PPC_SECTOFF:
8024 case R_PPC_SECTOFF_LO:
8025 case R_PPC_SECTOFF_HI:
8026 case R_PPC_SECTOFF_HA:
8027 if (sec == NULL || sec->output_section == NULL)
8029 unresolved_reloc = TRUE;
8030 break;
8032 addend -= sec->output_section->vma;
8033 break;
8035 /* Negative relocations. */
8036 case R_PPC_EMB_NADDR32:
8037 case R_PPC_EMB_NADDR16:
8038 case R_PPC_EMB_NADDR16_LO:
8039 case R_PPC_EMB_NADDR16_HI:
8040 case R_PPC_EMB_NADDR16_HA:
8041 addend -= 2 * relocation;
8042 break;
8044 case R_PPC_COPY:
8045 case R_PPC_GLOB_DAT:
8046 case R_PPC_JMP_SLOT:
8047 case R_PPC_RELATIVE:
8048 case R_PPC_IRELATIVE:
8049 case R_PPC_PLT32:
8050 case R_PPC_PLTREL32:
8051 case R_PPC_PLT16_LO:
8052 case R_PPC_PLT16_HI:
8053 case R_PPC_PLT16_HA:
8054 case R_PPC_ADDR30:
8055 case R_PPC_EMB_RELSEC16:
8056 case R_PPC_EMB_RELST_LO:
8057 case R_PPC_EMB_RELST_HI:
8058 case R_PPC_EMB_RELST_HA:
8059 case R_PPC_EMB_BIT_FLD:
8060 (*_bfd_error_handler)
8061 (_("%B: relocation %s is not yet supported for symbol %s."),
8062 input_bfd,
8063 howto->name,
8064 sym_name);
8066 bfd_set_error (bfd_error_invalid_operation);
8067 ret = FALSE;
8068 continue;
8071 /* Do any further special processing. */
8072 switch (r_type)
8074 default:
8075 break;
8077 case R_PPC_ADDR16_HA:
8078 case R_PPC_REL16_HA:
8079 case R_PPC_SECTOFF_HA:
8080 case R_PPC_TPREL16_HA:
8081 case R_PPC_DTPREL16_HA:
8082 case R_PPC_EMB_NADDR16_HA:
8083 case R_PPC_EMB_RELST_HA:
8084 /* It's just possible that this symbol is a weak symbol
8085 that's not actually defined anywhere. In that case,
8086 'sec' would be NULL, and we should leave the symbol
8087 alone (it will be set to zero elsewhere in the link). */
8088 if (sec == NULL)
8089 break;
8090 /* Fall thru */
8092 case R_PPC_PLT16_HA:
8093 case R_PPC_GOT16_HA:
8094 case R_PPC_GOT_TLSGD16_HA:
8095 case R_PPC_GOT_TLSLD16_HA:
8096 case R_PPC_GOT_TPREL16_HA:
8097 case R_PPC_GOT_DTPREL16_HA:
8098 /* Add 0x10000 if sign bit in 0:15 is set.
8099 Bits 0:15 are not used. */
8100 addend += 0x8000;
8101 break;
8104 #ifdef DEBUG
8105 fprintf (stderr, "\ttype = %s (%d), name = %s, symbol index = %ld, "
8106 "offset = %ld, addend = %ld\n",
8107 howto->name,
8108 (int) r_type,
8109 sym_name,
8110 r_symndx,
8111 (long) rel->r_offset,
8112 (long) addend);
8113 #endif
8115 if (unresolved_reloc
8116 && !((input_section->flags & SEC_DEBUGGING) != 0
8117 && h->def_dynamic))
8119 (*_bfd_error_handler)
8120 (_("%B(%A+0x%lx): unresolvable %s relocation against symbol `%s'"),
8121 input_bfd,
8122 input_section,
8123 (long) rel->r_offset,
8124 howto->name,
8125 sym_name);
8126 ret = FALSE;
8129 r = _bfd_final_link_relocate (howto,
8130 input_bfd,
8131 input_section,
8132 contents,
8133 rel->r_offset,
8134 relocation,
8135 addend);
8137 if (r != bfd_reloc_ok)
8139 if (r == bfd_reloc_overflow)
8141 if (warned)
8142 continue;
8143 if (h != NULL
8144 && h->root.type == bfd_link_hash_undefweak
8145 && howto->pc_relative)
8147 /* Assume this is a call protected by other code that
8148 detect the symbol is undefined. If this is the case,
8149 we can safely ignore the overflow. If not, the
8150 program is hosed anyway, and a little warning isn't
8151 going to help. */
8153 continue;
8156 if (! (*info->callbacks->reloc_overflow) (info,
8157 (h ? &h->root : NULL),
8158 sym_name,
8159 howto->name,
8160 rel->r_addend,
8161 input_bfd,
8162 input_section,
8163 rel->r_offset))
8164 return FALSE;
8166 else
8168 (*_bfd_error_handler)
8169 (_("%B(%A+0x%lx): %s reloc against `%s': error %d"),
8170 input_bfd, input_section,
8171 (long) rel->r_offset, howto->name, sym_name, (int) r);
8172 ret = FALSE;
8177 #ifdef DEBUG
8178 fprintf (stderr, "\n");
8179 #endif
8181 return ret;
8184 /* Finish up dynamic symbol handling. We set the contents of various
8185 dynamic sections here. */
8187 static bfd_boolean
8188 ppc_elf_finish_dynamic_symbol (bfd *output_bfd,
8189 struct bfd_link_info *info,
8190 struct elf_link_hash_entry *h,
8191 Elf_Internal_Sym *sym)
8193 struct ppc_elf_link_hash_table *htab;
8194 struct plt_entry *ent;
8195 bfd_boolean doneone;
8197 #ifdef DEBUG
8198 fprintf (stderr, "ppc_elf_finish_dynamic_symbol called for %s",
8199 h->root.root.string);
8200 #endif
8202 htab = ppc_elf_hash_table (info);
8203 BFD_ASSERT (htab->elf.dynobj != NULL);
8205 doneone = FALSE;
8206 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
8207 if (ent->plt.offset != (bfd_vma) -1)
8209 if (!doneone)
8211 Elf_Internal_Rela rela;
8212 bfd_byte *loc;
8213 bfd_vma reloc_index;
8215 if (htab->plt_type == PLT_NEW
8216 || !htab->elf.dynamic_sections_created
8217 || h->dynindx == -1)
8218 reloc_index = ent->plt.offset / 4;
8219 else
8221 reloc_index = ((ent->plt.offset - htab->plt_initial_entry_size)
8222 / htab->plt_slot_size);
8223 if (reloc_index > PLT_NUM_SINGLE_ENTRIES
8224 && htab->plt_type == PLT_OLD)
8225 reloc_index -= (reloc_index - PLT_NUM_SINGLE_ENTRIES) / 2;
8228 /* This symbol has an entry in the procedure linkage table.
8229 Set it up. */
8230 if (htab->plt_type == PLT_VXWORKS
8231 && htab->elf.dynamic_sections_created
8232 && h->dynindx != -1)
8234 bfd_vma got_offset;
8235 const bfd_vma *plt_entry;
8237 /* The first three entries in .got.plt are reserved. */
8238 got_offset = (reloc_index + 3) * 4;
8240 /* Use the right PLT. */
8241 plt_entry = info->shared ? ppc_elf_vxworks_pic_plt_entry
8242 : ppc_elf_vxworks_plt_entry;
8244 /* Fill in the .plt on VxWorks. */
8245 if (info->shared)
8247 bfd_put_32 (output_bfd,
8248 plt_entry[0] | PPC_HA (got_offset),
8249 htab->plt->contents + ent->plt.offset + 0);
8250 bfd_put_32 (output_bfd,
8251 plt_entry[1] | PPC_LO (got_offset),
8252 htab->plt->contents + ent->plt.offset + 4);
8254 else
8256 bfd_vma got_loc = got_offset + SYM_VAL (htab->elf.hgot);
8258 bfd_put_32 (output_bfd,
8259 plt_entry[0] | PPC_HA (got_loc),
8260 htab->plt->contents + ent->plt.offset + 0);
8261 bfd_put_32 (output_bfd,
8262 plt_entry[1] | PPC_LO (got_loc),
8263 htab->plt->contents + ent->plt.offset + 4);
8266 bfd_put_32 (output_bfd, plt_entry[2],
8267 htab->plt->contents + ent->plt.offset + 8);
8268 bfd_put_32 (output_bfd, plt_entry[3],
8269 htab->plt->contents + ent->plt.offset + 12);
8271 /* This instruction is an immediate load. The value loaded is
8272 the byte offset of the R_PPC_JMP_SLOT relocation from the
8273 start of the .rela.plt section. The value is stored in the
8274 low-order 16 bits of the load instruction. */
8275 /* NOTE: It appears that this is now an index rather than a
8276 prescaled offset. */
8277 bfd_put_32 (output_bfd,
8278 plt_entry[4] | reloc_index,
8279 htab->plt->contents + ent->plt.offset + 16);
8280 /* This instruction is a PC-relative branch whose target is
8281 the start of the PLT section. The address of this branch
8282 instruction is 20 bytes beyond the start of this PLT entry.
8283 The address is encoded in bits 6-29, inclusive. The value
8284 stored is right-shifted by two bits, permitting a 26-bit
8285 offset. */
8286 bfd_put_32 (output_bfd,
8287 (plt_entry[5]
8288 | (-(ent->plt.offset + 20) & 0x03fffffc)),
8289 htab->plt->contents + ent->plt.offset + 20);
8290 bfd_put_32 (output_bfd, plt_entry[6],
8291 htab->plt->contents + ent->plt.offset + 24);
8292 bfd_put_32 (output_bfd, plt_entry[7],
8293 htab->plt->contents + ent->plt.offset + 28);
8295 /* Fill in the GOT entry corresponding to this PLT slot with
8296 the address immediately after the the "bctr" instruction
8297 in this PLT entry. */
8298 bfd_put_32 (output_bfd, (htab->plt->output_section->vma
8299 + htab->plt->output_offset
8300 + ent->plt.offset + 16),
8301 htab->sgotplt->contents + got_offset);
8303 if (!info->shared)
8305 /* Fill in a couple of entries in .rela.plt.unloaded. */
8306 loc = htab->srelplt2->contents
8307 + ((VXWORKS_PLTRESOLVE_RELOCS + reloc_index
8308 * VXWORKS_PLT_NON_JMP_SLOT_RELOCS)
8309 * sizeof (Elf32_External_Rela));
8311 /* Provide the @ha relocation for the first instruction. */
8312 rela.r_offset = (htab->plt->output_section->vma
8313 + htab->plt->output_offset
8314 + ent->plt.offset + 2);
8315 rela.r_info = ELF32_R_INFO (htab->elf.hgot->indx,
8316 R_PPC_ADDR16_HA);
8317 rela.r_addend = got_offset;
8318 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
8319 loc += sizeof (Elf32_External_Rela);
8321 /* Provide the @l relocation for the second instruction. */
8322 rela.r_offset = (htab->plt->output_section->vma
8323 + htab->plt->output_offset
8324 + ent->plt.offset + 6);
8325 rela.r_info = ELF32_R_INFO (htab->elf.hgot->indx,
8326 R_PPC_ADDR16_LO);
8327 rela.r_addend = got_offset;
8328 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
8329 loc += sizeof (Elf32_External_Rela);
8331 /* Provide a relocation for the GOT entry corresponding to this
8332 PLT slot. Point it at the middle of the .plt entry. */
8333 rela.r_offset = (htab->sgotplt->output_section->vma
8334 + htab->sgotplt->output_offset
8335 + got_offset);
8336 rela.r_info = ELF32_R_INFO (htab->elf.hplt->indx,
8337 R_PPC_ADDR32);
8338 rela.r_addend = ent->plt.offset + 16;
8339 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
8342 /* VxWorks uses non-standard semantics for R_PPC_JMP_SLOT.
8343 In particular, the offset for the relocation is not the
8344 address of the PLT entry for this function, as specified
8345 by the ABI. Instead, the offset is set to the address of
8346 the GOT slot for this function. See EABI 4.4.4.1. */
8347 rela.r_offset = (htab->sgotplt->output_section->vma
8348 + htab->sgotplt->output_offset
8349 + got_offset);
8352 else
8354 asection *splt = htab->plt;
8355 if (!htab->elf.dynamic_sections_created
8356 || h->dynindx == -1)
8357 splt = htab->iplt;
8359 rela.r_offset = (splt->output_section->vma
8360 + splt->output_offset
8361 + ent->plt.offset);
8362 if (htab->plt_type == PLT_OLD
8363 || !htab->elf.dynamic_sections_created
8364 || h->dynindx == -1)
8366 /* We don't need to fill in the .plt. The ppc dynamic
8367 linker will fill it in. */
8369 else
8371 bfd_vma val = (htab->glink_pltresolve + ent->plt.offset
8372 + htab->glink->output_section->vma
8373 + htab->glink->output_offset);
8374 bfd_put_32 (output_bfd, val,
8375 splt->contents + ent->plt.offset);
8379 /* Fill in the entry in the .rela.plt section. */
8380 rela.r_addend = 0;
8381 if (!htab->elf.dynamic_sections_created
8382 || h->dynindx == -1)
8384 BFD_ASSERT (h->type == STT_GNU_IFUNC
8385 && h->def_regular
8386 && (h->root.type == bfd_link_hash_defined
8387 || h->root.type == bfd_link_hash_defweak));
8388 rela.r_info = ELF32_R_INFO (0, R_PPC_IRELATIVE);
8389 rela.r_addend = SYM_VAL (h);
8391 else
8392 rela.r_info = ELF32_R_INFO (h->dynindx, R_PPC_JMP_SLOT);
8394 if (!htab->elf.dynamic_sections_created
8395 || h->dynindx == -1)
8396 loc = (htab->reliplt->contents
8397 + (htab->reliplt->reloc_count++
8398 * sizeof (Elf32_External_Rela)));
8399 else
8400 loc = (htab->relplt->contents
8401 + reloc_index * sizeof (Elf32_External_Rela));
8402 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
8404 if (!h->def_regular)
8406 /* Mark the symbol as undefined, rather than as
8407 defined in the .plt section. Leave the value if
8408 there were any relocations where pointer equality
8409 matters (this is a clue for the dynamic linker, to
8410 make function pointer comparisons work between an
8411 application and shared library), otherwise set it
8412 to zero. */
8413 sym->st_shndx = SHN_UNDEF;
8414 if (!h->pointer_equality_needed)
8415 sym->st_value = 0;
8416 else if (!h->ref_regular_nonweak)
8418 /* This breaks function pointer comparisons, but
8419 that is better than breaking tests for a NULL
8420 function pointer. */
8421 sym->st_value = 0;
8424 else if (h->type == STT_GNU_IFUNC
8425 && !info->shared)
8427 /* Set the value of ifunc symbols in a non-pie
8428 executable to the glink entry. This is to avoid
8429 text relocations. We can't do this for ifunc in
8430 allocate_dynrelocs, as we do for normal dynamic
8431 function symbols with plt entries, because we need
8432 to keep the original value around for the ifunc
8433 relocation. */
8434 sym->st_shndx = (_bfd_elf_section_from_bfd_section
8435 (output_bfd, htab->glink->output_section));
8436 sym->st_value = (ent->glink_offset
8437 + htab->glink->output_offset
8438 + htab->glink->output_section->vma);
8440 doneone = TRUE;
8443 if (htab->plt_type == PLT_NEW
8444 || !htab->elf.dynamic_sections_created
8445 || h->dynindx == -1)
8447 unsigned char *p;
8448 asection *splt = htab->plt;
8449 if (!htab->elf.dynamic_sections_created
8450 || h->dynindx == -1)
8451 splt = htab->iplt;
8453 p = (unsigned char *) htab->glink->contents + ent->glink_offset;
8455 if (h == htab->tls_get_addr && !htab->no_tls_get_addr_opt)
8457 bfd_put_32 (output_bfd, LWZ_11_3, p);
8458 p += 4;
8459 bfd_put_32 (output_bfd, LWZ_12_3 + 4, p);
8460 p += 4;
8461 bfd_put_32 (output_bfd, MR_0_3, p);
8462 p += 4;
8463 bfd_put_32 (output_bfd, CMPWI_11_0, p);
8464 p += 4;
8465 bfd_put_32 (output_bfd, ADD_3_12_2, p);
8466 p += 4;
8467 bfd_put_32 (output_bfd, BEQLR, p);
8468 p += 4;
8469 bfd_put_32 (output_bfd, MR_3_0, p);
8470 p += 4;
8471 bfd_put_32 (output_bfd, NOP, p);
8472 p += 4;
8475 write_glink_stub (ent, splt, p, info);
8477 if (!info->shared)
8478 /* We only need one non-PIC glink stub. */
8479 break;
8481 else
8482 break;
8485 if (h->needs_copy)
8487 asection *s;
8488 Elf_Internal_Rela rela;
8489 bfd_byte *loc;
8491 /* This symbols needs a copy reloc. Set it up. */
8493 #ifdef DEBUG
8494 fprintf (stderr, ", copy");
8495 #endif
8497 BFD_ASSERT (h->dynindx != -1);
8499 if (ppc_elf_hash_entry (h)->has_sda_refs)
8500 s = htab->relsbss;
8501 else
8502 s = htab->relbss;
8503 BFD_ASSERT (s != NULL);
8505 rela.r_offset = SYM_VAL (h);
8506 rela.r_info = ELF32_R_INFO (h->dynindx, R_PPC_COPY);
8507 rela.r_addend = 0;
8508 loc = s->contents + s->reloc_count++ * sizeof (Elf32_External_Rela);
8509 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
8512 #ifdef DEBUG
8513 fprintf (stderr, "\n");
8514 #endif
8516 /* Mark some specially defined symbols as absolute. */
8517 if (strcmp (h->root.root.string, "_DYNAMIC") == 0
8518 || (!htab->is_vxworks
8519 && (h == htab->elf.hgot
8520 || strcmp (h->root.root.string,
8521 "_PROCEDURE_LINKAGE_TABLE_") == 0)))
8522 sym->st_shndx = SHN_ABS;
8524 return TRUE;
8527 static enum elf_reloc_type_class
8528 ppc_elf_reloc_type_class (const Elf_Internal_Rela *rela)
8530 switch (ELF32_R_TYPE (rela->r_info))
8532 case R_PPC_RELATIVE:
8533 return reloc_class_relative;
8534 case R_PPC_REL24:
8535 case R_PPC_ADDR24:
8536 case R_PPC_JMP_SLOT:
8537 return reloc_class_plt;
8538 case R_PPC_COPY:
8539 return reloc_class_copy;
8540 default:
8541 return reloc_class_normal;
8545 /* Finish up the dynamic sections. */
8547 static bfd_boolean
8548 ppc_elf_finish_dynamic_sections (bfd *output_bfd,
8549 struct bfd_link_info *info)
8551 asection *sdyn;
8552 asection *splt;
8553 struct ppc_elf_link_hash_table *htab;
8554 bfd_vma got;
8555 bfd *dynobj;
8556 bfd_boolean ret = TRUE;
8558 #ifdef DEBUG
8559 fprintf (stderr, "ppc_elf_finish_dynamic_sections called\n");
8560 #endif
8562 htab = ppc_elf_hash_table (info);
8563 dynobj = elf_hash_table (info)->dynobj;
8564 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
8565 if (htab->is_vxworks)
8566 splt = bfd_get_section_by_name (dynobj, ".plt");
8567 else
8568 splt = NULL;
8570 got = 0;
8571 if (htab->elf.hgot != NULL)
8572 got = SYM_VAL (htab->elf.hgot);
8574 if (htab->elf.dynamic_sections_created)
8576 Elf32_External_Dyn *dyncon, *dynconend;
8578 BFD_ASSERT (htab->plt != NULL && sdyn != NULL);
8580 dyncon = (Elf32_External_Dyn *) sdyn->contents;
8581 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
8582 for (; dyncon < dynconend; dyncon++)
8584 Elf_Internal_Dyn dyn;
8585 asection *s;
8587 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
8589 switch (dyn.d_tag)
8591 case DT_PLTGOT:
8592 if (htab->is_vxworks)
8593 s = htab->sgotplt;
8594 else
8595 s = htab->plt;
8596 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
8597 break;
8599 case DT_PLTRELSZ:
8600 dyn.d_un.d_val = htab->relplt->size;
8601 break;
8603 case DT_JMPREL:
8604 s = htab->relplt;
8605 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
8606 break;
8608 case DT_PPC_GOT:
8609 dyn.d_un.d_ptr = got;
8610 break;
8612 case DT_RELASZ:
8613 if (htab->is_vxworks)
8615 if (htab->relplt)
8616 dyn.d_un.d_ptr -= htab->relplt->size;
8617 break;
8619 continue;
8621 default:
8622 if (htab->is_vxworks
8623 && elf_vxworks_finish_dynamic_entry (output_bfd, &dyn))
8624 break;
8625 continue;
8628 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
8632 if (htab->got != NULL)
8634 if (htab->elf.hgot->root.u.def.section == htab->got
8635 || htab->elf.hgot->root.u.def.section == htab->sgotplt)
8637 unsigned char *p = htab->elf.hgot->root.u.def.section->contents;
8639 p += htab->elf.hgot->root.u.def.value;
8640 if (htab->plt_type == PLT_OLD)
8642 /* Add a blrl instruction at _GLOBAL_OFFSET_TABLE_-4
8643 so that a function can easily find the address of
8644 _GLOBAL_OFFSET_TABLE_. */
8645 BFD_ASSERT (htab->elf.hgot->root.u.def.value - 4
8646 < htab->elf.hgot->root.u.def.section->size);
8647 bfd_put_32 (output_bfd, 0x4e800021, p - 4);
8650 if (sdyn != NULL)
8652 bfd_vma val = sdyn->output_section->vma + sdyn->output_offset;
8653 BFD_ASSERT (htab->elf.hgot->root.u.def.value
8654 < htab->elf.hgot->root.u.def.section->size);
8655 bfd_put_32 (output_bfd, val, p);
8658 else
8660 (*_bfd_error_handler) (_("%s not defined in linker created %s"),
8661 htab->elf.hgot->root.root.string,
8662 (htab->sgotplt != NULL
8663 ? htab->sgotplt->name : htab->got->name));
8664 bfd_set_error (bfd_error_bad_value);
8665 ret = FALSE;
8668 elf_section_data (htab->got->output_section)->this_hdr.sh_entsize = 4;
8671 /* Fill in the first entry in the VxWorks procedure linkage table. */
8672 if (splt && splt->size > 0)
8674 /* Use the right PLT. */
8675 const bfd_vma *plt_entry = (info->shared
8676 ? ppc_elf_vxworks_pic_plt0_entry
8677 : ppc_elf_vxworks_plt0_entry);
8679 if (!info->shared)
8681 bfd_vma got_value = SYM_VAL (htab->elf.hgot);
8683 bfd_put_32 (output_bfd, plt_entry[0] | PPC_HA (got_value),
8684 splt->contents + 0);
8685 bfd_put_32 (output_bfd, plt_entry[1] | PPC_LO (got_value),
8686 splt->contents + 4);
8688 else
8690 bfd_put_32 (output_bfd, plt_entry[0], splt->contents + 0);
8691 bfd_put_32 (output_bfd, plt_entry[1], splt->contents + 4);
8693 bfd_put_32 (output_bfd, plt_entry[2], splt->contents + 8);
8694 bfd_put_32 (output_bfd, plt_entry[3], splt->contents + 12);
8695 bfd_put_32 (output_bfd, plt_entry[4], splt->contents + 16);
8696 bfd_put_32 (output_bfd, plt_entry[5], splt->contents + 20);
8697 bfd_put_32 (output_bfd, plt_entry[6], splt->contents + 24);
8698 bfd_put_32 (output_bfd, plt_entry[7], splt->contents + 28);
8700 if (! info->shared)
8702 Elf_Internal_Rela rela;
8703 bfd_byte *loc;
8705 loc = htab->srelplt2->contents;
8707 /* Output the @ha relocation for the first instruction. */
8708 rela.r_offset = (htab->plt->output_section->vma
8709 + htab->plt->output_offset
8710 + 2);
8711 rela.r_info = ELF32_R_INFO (htab->elf.hgot->indx, R_PPC_ADDR16_HA);
8712 rela.r_addend = 0;
8713 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
8714 loc += sizeof (Elf32_External_Rela);
8716 /* Output the @l relocation for the second instruction. */
8717 rela.r_offset = (htab->plt->output_section->vma
8718 + htab->plt->output_offset
8719 + 6);
8720 rela.r_info = ELF32_R_INFO (htab->elf.hgot->indx, R_PPC_ADDR16_LO);
8721 rela.r_addend = 0;
8722 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
8723 loc += sizeof (Elf32_External_Rela);
8725 /* Fix up the remaining relocations. They may have the wrong
8726 symbol index for _G_O_T_ or _P_L_T_ depending on the order
8727 in which symbols were output. */
8728 while (loc < htab->srelplt2->contents + htab->srelplt2->size)
8730 Elf_Internal_Rela rel;
8732 bfd_elf32_swap_reloc_in (output_bfd, loc, &rel);
8733 rel.r_info = ELF32_R_INFO (htab->elf.hgot->indx, R_PPC_ADDR16_HA);
8734 bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
8735 loc += sizeof (Elf32_External_Rela);
8737 bfd_elf32_swap_reloc_in (output_bfd, loc, &rel);
8738 rel.r_info = ELF32_R_INFO (htab->elf.hgot->indx, R_PPC_ADDR16_LO);
8739 bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
8740 loc += sizeof (Elf32_External_Rela);
8742 bfd_elf32_swap_reloc_in (output_bfd, loc, &rel);
8743 rel.r_info = ELF32_R_INFO (htab->elf.hplt->indx, R_PPC_ADDR32);
8744 bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
8745 loc += sizeof (Elf32_External_Rela);
8750 if (htab->glink != NULL
8751 && htab->glink->contents != NULL
8752 && htab->elf.dynamic_sections_created)
8754 unsigned char *p;
8755 unsigned char *endp;
8756 bfd_vma res0;
8757 unsigned int i;
8760 * PIC glink code is the following:
8762 * # ith PLT code stub.
8763 * addis 11,30,(plt+(i-1)*4-got)@ha
8764 * lwz 11,(plt+(i-1)*4-got)@l(11)
8765 * mtctr 11
8766 * bctr
8768 * # A table of branches, one for each plt entry.
8769 * # The idea is that the plt call stub loads ctr and r11 with these
8770 * # addresses, so (r11 - res_0) gives the plt index * 4.
8771 * res_0: b PLTresolve
8772 * res_1: b PLTresolve
8774 * # Some number of entries towards the end can be nops
8775 * res_n_m3: nop
8776 * res_n_m2: nop
8777 * res_n_m1:
8779 * PLTresolve:
8780 * addis 11,11,(1f-res_0)@ha
8781 * mflr 0
8782 * bcl 20,31,1f
8783 * 1: addi 11,11,(1b-res_0)@l
8784 * mflr 12
8785 * mtlr 0
8786 * sub 11,11,12 # r11 = index * 4
8787 * addis 12,12,(got+4-1b)@ha
8788 * lwz 0,(got+4-1b)@l(12) # got[1] address of dl_runtime_resolve
8789 * lwz 12,(got+8-1b)@l(12) # got[2] contains the map address
8790 * mtctr 0
8791 * add 0,11,11
8792 * add 11,0,11 # r11 = index * 12 = reloc offset.
8793 * bctr
8795 static const unsigned int pic_plt_resolve[] =
8797 ADDIS_11_11,
8798 MFLR_0,
8799 BCL_20_31,
8800 ADDI_11_11,
8801 MFLR_12,
8802 MTLR_0,
8803 SUB_11_11_12,
8804 ADDIS_12_12,
8805 LWZ_0_12,
8806 LWZ_12_12,
8807 MTCTR_0,
8808 ADD_0_11_11,
8809 ADD_11_0_11,
8810 BCTR,
8811 NOP,
8816 * Non-PIC glink code is a little simpler.
8818 * # ith PLT code stub.
8819 * lis 11,(plt+(i-1)*4)@ha
8820 * lwz 11,(plt+(i-1)*4)@l(11)
8821 * mtctr 11
8822 * bctr
8824 * The branch table is the same, then comes
8826 * PLTresolve:
8827 * lis 12,(got+4)@ha
8828 * addis 11,11,(-res_0)@ha
8829 * lwz 0,(got+4)@l(12) # got[1] address of dl_runtime_resolve
8830 * addi 11,11,(-res_0)@l # r11 = index * 4
8831 * mtctr 0
8832 * add 0,11,11
8833 * lwz 12,(got+8)@l(12) # got[2] contains the map address
8834 * add 11,0,11 # r11 = index * 12 = reloc offset.
8835 * bctr
8837 static const unsigned int plt_resolve[] =
8839 LIS_12,
8840 ADDIS_11_11,
8841 LWZ_0_12,
8842 ADDI_11_11,
8843 MTCTR_0,
8844 ADD_0_11_11,
8845 LWZ_12_12,
8846 ADD_11_0_11,
8847 BCTR,
8848 NOP,
8849 NOP,
8850 NOP,
8851 NOP,
8852 NOP,
8853 NOP,
8857 if (ARRAY_SIZE (pic_plt_resolve) != GLINK_PLTRESOLVE / 4)
8858 abort ();
8859 if (ARRAY_SIZE (plt_resolve) != GLINK_PLTRESOLVE / 4)
8860 abort ();
8862 /* Build the branch table, one for each plt entry (less one),
8863 and perhaps some padding. */
8864 p = htab->glink->contents;
8865 p += htab->glink_pltresolve;
8866 endp = htab->glink->contents;
8867 endp += htab->glink->size - GLINK_PLTRESOLVE;
8868 while (p < endp - 8 * 4)
8870 bfd_put_32 (output_bfd, B + endp - p, p);
8871 p += 4;
8873 while (p < endp)
8875 bfd_put_32 (output_bfd, NOP, p);
8876 p += 4;
8879 res0 = (htab->glink_pltresolve
8880 + htab->glink->output_section->vma
8881 + htab->glink->output_offset);
8883 /* Last comes the PLTresolve stub. */
8884 if (info->shared)
8886 bfd_vma bcl;
8888 for (i = 0; i < ARRAY_SIZE (pic_plt_resolve); i++)
8890 bfd_put_32 (output_bfd, pic_plt_resolve[i], p);
8891 p += 4;
8893 p -= 4 * ARRAY_SIZE (pic_plt_resolve);
8895 bcl = (htab->glink->size - GLINK_PLTRESOLVE + 3*4
8896 + htab->glink->output_section->vma
8897 + htab->glink->output_offset);
8899 bfd_put_32 (output_bfd,
8900 ADDIS_11_11 + PPC_HA (bcl - res0), p + 0*4);
8901 bfd_put_32 (output_bfd,
8902 ADDI_11_11 + PPC_LO (bcl - res0), p + 3*4);
8903 bfd_put_32 (output_bfd,
8904 ADDIS_12_12 + PPC_HA (got + 4 - bcl), p + 7*4);
8905 if (PPC_HA (got + 4 - bcl) == PPC_HA (got + 8 - bcl))
8907 bfd_put_32 (output_bfd,
8908 LWZ_0_12 + PPC_LO (got + 4 - bcl), p + 8*4);
8909 bfd_put_32 (output_bfd,
8910 LWZ_12_12 + PPC_LO (got + 8 - bcl), p + 9*4);
8912 else
8914 bfd_put_32 (output_bfd,
8915 LWZU_0_12 + PPC_LO (got + 4 - bcl), p + 8*4);
8916 bfd_put_32 (output_bfd,
8917 LWZ_12_12 + 4, p + 9*4);
8920 else
8922 for (i = 0; i < ARRAY_SIZE (plt_resolve); i++)
8924 bfd_put_32 (output_bfd, plt_resolve[i], p);
8925 p += 4;
8927 p -= 4 * ARRAY_SIZE (plt_resolve);
8929 bfd_put_32 (output_bfd,
8930 LIS_12 + PPC_HA (got + 4), p + 0*4);
8931 bfd_put_32 (output_bfd,
8932 ADDIS_11_11 + PPC_HA (-res0), p + 1*4);
8933 bfd_put_32 (output_bfd,
8934 ADDI_11_11 + PPC_LO (-res0), p + 3*4);
8935 if (PPC_HA (got + 4) == PPC_HA (got + 8))
8937 bfd_put_32 (output_bfd,
8938 LWZ_0_12 + PPC_LO (got + 4), p + 2*4);
8939 bfd_put_32 (output_bfd,
8940 LWZ_12_12 + PPC_LO (got + 8), p + 6*4);
8942 else
8944 bfd_put_32 (output_bfd,
8945 LWZU_0_12 + PPC_LO (got + 4), p + 2*4);
8946 bfd_put_32 (output_bfd,
8947 LWZ_12_12 + 4, p + 6*4);
8952 return ret;
8955 #define TARGET_LITTLE_SYM bfd_elf32_powerpcle_vec
8956 #define TARGET_LITTLE_NAME "elf32-powerpcle"
8957 #define TARGET_BIG_SYM bfd_elf32_powerpc_vec
8958 #define TARGET_BIG_NAME "elf32-powerpc"
8959 #define ELF_ARCH bfd_arch_powerpc
8960 #define ELF_TARGET_ID PPC32_ELF_DATA
8961 #define ELF_MACHINE_CODE EM_PPC
8962 #ifdef __QNXTARGET__
8963 #define ELF_MAXPAGESIZE 0x1000
8964 #else
8965 #define ELF_MAXPAGESIZE 0x10000
8966 #endif
8967 #define ELF_MINPAGESIZE 0x1000
8968 #define ELF_COMMONPAGESIZE 0x1000
8969 #define elf_info_to_howto ppc_elf_info_to_howto
8971 #ifdef EM_CYGNUS_POWERPC
8972 #define ELF_MACHINE_ALT1 EM_CYGNUS_POWERPC
8973 #endif
8975 #ifdef EM_PPC_OLD
8976 #define ELF_MACHINE_ALT2 EM_PPC_OLD
8977 #endif
8979 #define elf_backend_plt_not_loaded 1
8980 #define elf_backend_can_gc_sections 1
8981 #define elf_backend_can_refcount 1
8982 #define elf_backend_rela_normal 1
8984 #define bfd_elf32_mkobject ppc_elf_mkobject
8985 #define bfd_elf32_bfd_merge_private_bfd_data ppc_elf_merge_private_bfd_data
8986 #define bfd_elf32_bfd_relax_section ppc_elf_relax_section
8987 #define bfd_elf32_bfd_reloc_type_lookup ppc_elf_reloc_type_lookup
8988 #define bfd_elf32_bfd_reloc_name_lookup ppc_elf_reloc_name_lookup
8989 #define bfd_elf32_bfd_set_private_flags ppc_elf_set_private_flags
8990 #define bfd_elf32_bfd_link_hash_table_create ppc_elf_link_hash_table_create
8991 #define bfd_elf32_get_synthetic_symtab ppc_elf_get_synthetic_symtab
8993 #define elf_backend_object_p ppc_elf_object_p
8994 #define elf_backend_gc_mark_hook ppc_elf_gc_mark_hook
8995 #define elf_backend_gc_sweep_hook ppc_elf_gc_sweep_hook
8996 #define elf_backend_section_from_shdr ppc_elf_section_from_shdr
8997 #define elf_backend_relocate_section ppc_elf_relocate_section
8998 #define elf_backend_create_dynamic_sections ppc_elf_create_dynamic_sections
8999 #define elf_backend_check_relocs ppc_elf_check_relocs
9000 #define elf_backend_copy_indirect_symbol ppc_elf_copy_indirect_symbol
9001 #define elf_backend_adjust_dynamic_symbol ppc_elf_adjust_dynamic_symbol
9002 #define elf_backend_add_symbol_hook ppc_elf_add_symbol_hook
9003 #define elf_backend_size_dynamic_sections ppc_elf_size_dynamic_sections
9004 #define elf_backend_hash_symbol ppc_elf_hash_symbol
9005 #define elf_backend_finish_dynamic_symbol ppc_elf_finish_dynamic_symbol
9006 #define elf_backend_finish_dynamic_sections ppc_elf_finish_dynamic_sections
9007 #define elf_backend_fake_sections ppc_elf_fake_sections
9008 #define elf_backend_additional_program_headers ppc_elf_additional_program_headers
9009 #define elf_backend_grok_prstatus ppc_elf_grok_prstatus
9010 #define elf_backend_grok_psinfo ppc_elf_grok_psinfo
9011 #define elf_backend_write_core_note ppc_elf_write_core_note
9012 #define elf_backend_reloc_type_class ppc_elf_reloc_type_class
9013 #define elf_backend_begin_write_processing ppc_elf_begin_write_processing
9014 #define elf_backend_final_write_processing ppc_elf_final_write_processing
9015 #define elf_backend_write_section ppc_elf_write_section
9016 #define elf_backend_get_sec_type_attr ppc_elf_get_sec_type_attr
9017 #define elf_backend_plt_sym_val ppc_elf_plt_sym_val
9018 #define elf_backend_action_discarded ppc_elf_action_discarded
9019 #define elf_backend_init_index_section _bfd_elf_init_1_index_section
9020 #define elf_backend_post_process_headers _bfd_elf_set_osabi
9022 #include "elf32-target.h"
9024 /* VxWorks Target */
9026 #undef TARGET_LITTLE_SYM
9027 #undef TARGET_LITTLE_NAME
9029 #undef TARGET_BIG_SYM
9030 #define TARGET_BIG_SYM bfd_elf32_powerpc_vxworks_vec
9031 #undef TARGET_BIG_NAME
9032 #define TARGET_BIG_NAME "elf32-powerpc-vxworks"
9034 /* VxWorks uses the elf default section flags for .plt. */
9035 static const struct bfd_elf_special_section *
9036 ppc_elf_vxworks_get_sec_type_attr (bfd *abfd ATTRIBUTE_UNUSED, asection *sec)
9038 if (sec->name == NULL)
9039 return NULL;
9041 if (strcmp (sec->name, ".plt") == 0)
9042 return _bfd_elf_get_sec_type_attr (abfd, sec);
9044 return ppc_elf_get_sec_type_attr (abfd, sec);
9047 /* Like ppc_elf_link_hash_table_create, but overrides
9048 appropriately for VxWorks. */
9049 static struct bfd_link_hash_table *
9050 ppc_elf_vxworks_link_hash_table_create (bfd *abfd)
9052 struct bfd_link_hash_table *ret;
9054 ret = ppc_elf_link_hash_table_create (abfd);
9055 if (ret)
9057 struct ppc_elf_link_hash_table *htab
9058 = (struct ppc_elf_link_hash_table *)ret;
9059 htab->is_vxworks = 1;
9060 htab->plt_type = PLT_VXWORKS;
9061 htab->plt_entry_size = VXWORKS_PLT_ENTRY_SIZE;
9062 htab->plt_slot_size = VXWORKS_PLT_ENTRY_SIZE;
9063 htab->plt_initial_entry_size = VXWORKS_PLT_INITIAL_ENTRY_SIZE;
9065 return ret;
9068 /* Tweak magic VxWorks symbols as they are loaded. */
9069 static bfd_boolean
9070 ppc_elf_vxworks_add_symbol_hook (bfd *abfd,
9071 struct bfd_link_info *info,
9072 Elf_Internal_Sym *sym,
9073 const char **namep ATTRIBUTE_UNUSED,
9074 flagword *flagsp ATTRIBUTE_UNUSED,
9075 asection **secp,
9076 bfd_vma *valp)
9078 if (!elf_vxworks_add_symbol_hook(abfd, info, sym,namep, flagsp, secp,
9079 valp))
9080 return FALSE;
9082 return ppc_elf_add_symbol_hook(abfd, info, sym,namep, flagsp, secp, valp);
9085 static void
9086 ppc_elf_vxworks_final_write_processing (bfd *abfd, bfd_boolean linker)
9088 ppc_elf_final_write_processing(abfd, linker);
9089 elf_vxworks_final_write_processing(abfd, linker);
9092 /* On VxWorks, we emit relocations against _PROCEDURE_LINKAGE_TABLE_, so
9093 define it. */
9094 #undef elf_backend_want_plt_sym
9095 #define elf_backend_want_plt_sym 1
9096 #undef elf_backend_want_got_plt
9097 #define elf_backend_want_got_plt 1
9098 #undef elf_backend_got_symbol_offset
9099 #define elf_backend_got_symbol_offset 0
9100 #undef elf_backend_plt_not_loaded
9101 #define elf_backend_plt_not_loaded 0
9102 #undef elf_backend_plt_readonly
9103 #define elf_backend_plt_readonly 1
9104 #undef elf_backend_got_header_size
9105 #define elf_backend_got_header_size 12
9107 #undef bfd_elf32_get_synthetic_symtab
9109 #undef bfd_elf32_bfd_link_hash_table_create
9110 #define bfd_elf32_bfd_link_hash_table_create \
9111 ppc_elf_vxworks_link_hash_table_create
9112 #undef elf_backend_add_symbol_hook
9113 #define elf_backend_add_symbol_hook \
9114 ppc_elf_vxworks_add_symbol_hook
9115 #undef elf_backend_link_output_symbol_hook
9116 #define elf_backend_link_output_symbol_hook \
9117 elf_vxworks_link_output_symbol_hook
9118 #undef elf_backend_final_write_processing
9119 #define elf_backend_final_write_processing \
9120 ppc_elf_vxworks_final_write_processing
9121 #undef elf_backend_get_sec_type_attr
9122 #define elf_backend_get_sec_type_attr \
9123 ppc_elf_vxworks_get_sec_type_attr
9124 #undef elf_backend_emit_relocs
9125 #define elf_backend_emit_relocs \
9126 elf_vxworks_emit_relocs
9128 #undef elf32_bed
9129 #define elf32_bed ppc_elf_vxworks_bed
9130 #undef elf_backend_post_process_headers
9132 #include "elf32-target.h"