Fix typo introduced by previous delta
[binutils.git] / bfd / elf32-ppc.c
blob5b32f147db711637d41cc385724b5557d9afe2ca
1 /* PowerPC-specific support for 32-bit ELF
2 Copyright 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
3 2004, 2005 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 2 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., 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
23 /* This file is based on a preliminary PowerPC ELF ABI. The
24 information may not match the final PowerPC ELF ABI. It includes
25 suggestions from the in-progress Embedded PowerPC ABI, and that
26 information may also not match. */
28 #include "bfd.h"
29 #include "sysdep.h"
30 #include "bfdlink.h"
31 #include "libbfd.h"
32 #include "elf-bfd.h"
33 #include "elf/ppc.h"
34 #include "elf32-ppc.h"
36 /* RELA relocations are used here. */
38 static bfd_reloc_status_type ppc_elf_addr16_ha_reloc
39 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
40 static bfd_reloc_status_type ppc_elf_unhandled_reloc
41 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
43 /* Branch prediction bit for branch taken relocs. */
44 #define BRANCH_PREDICT_BIT 0x200000
45 /* Mask to set RA in memory instructions. */
46 #define RA_REGISTER_MASK 0x001f0000
47 /* Value to shift register by to insert RA. */
48 #define RA_REGISTER_SHIFT 16
50 /* The name of the dynamic interpreter. This is put in the .interp
51 section. */
52 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
54 /* The size in bytes of an entry in the procedure linkage table. */
55 #define PLT_ENTRY_SIZE 12
56 /* The initial size of the plt reserved for the dynamic linker. */
57 #define PLT_INITIAL_ENTRY_SIZE 72
58 /* The size of the gap between entries in the PLT. */
59 #define PLT_SLOT_SIZE 8
60 /* The number of single-slot PLT entries (the rest use two slots). */
61 #define PLT_NUM_SINGLE_ENTRIES 8192
63 /* Some nop instructions. */
64 #define NOP 0x60000000
65 #define CROR_151515 0x4def7b82
66 #define CROR_313131 0x4ffffb82
68 /* Offset of tp and dtp pointers from start of TLS block. */
69 #define TP_OFFSET 0x7000
70 #define DTP_OFFSET 0x8000
72 static reloc_howto_type *ppc_elf_howto_table[R_PPC_max];
74 static reloc_howto_type ppc_elf_howto_raw[] = {
75 /* This reloc does nothing. */
76 HOWTO (R_PPC_NONE, /* type */
77 0, /* rightshift */
78 2, /* size (0 = byte, 1 = short, 2 = long) */
79 32, /* bitsize */
80 FALSE, /* pc_relative */
81 0, /* bitpos */
82 complain_overflow_bitfield, /* complain_on_overflow */
83 bfd_elf_generic_reloc, /* special_function */
84 "R_PPC_NONE", /* name */
85 FALSE, /* partial_inplace */
86 0, /* src_mask */
87 0, /* dst_mask */
88 FALSE), /* pcrel_offset */
90 /* A standard 32 bit relocation. */
91 HOWTO (R_PPC_ADDR32, /* type */
92 0, /* rightshift */
93 2, /* size (0 = byte, 1 = short, 2 = long) */
94 32, /* bitsize */
95 FALSE, /* pc_relative */
96 0, /* bitpos */
97 complain_overflow_bitfield, /* complain_on_overflow */
98 bfd_elf_generic_reloc, /* special_function */
99 "R_PPC_ADDR32", /* name */
100 FALSE, /* partial_inplace */
101 0, /* src_mask */
102 0xffffffff, /* dst_mask */
103 FALSE), /* pcrel_offset */
105 /* An absolute 26 bit branch; the lower two bits must be zero.
106 FIXME: we don't check that, we just clear them. */
107 HOWTO (R_PPC_ADDR24, /* type */
108 0, /* rightshift */
109 2, /* size (0 = byte, 1 = short, 2 = long) */
110 26, /* bitsize */
111 FALSE, /* pc_relative */
112 0, /* bitpos */
113 complain_overflow_bitfield, /* complain_on_overflow */
114 bfd_elf_generic_reloc, /* special_function */
115 "R_PPC_ADDR24", /* name */
116 FALSE, /* partial_inplace */
117 0, /* src_mask */
118 0x3fffffc, /* dst_mask */
119 FALSE), /* pcrel_offset */
121 /* A standard 16 bit relocation. */
122 HOWTO (R_PPC_ADDR16, /* type */
123 0, /* rightshift */
124 1, /* size (0 = byte, 1 = short, 2 = long) */
125 16, /* bitsize */
126 FALSE, /* pc_relative */
127 0, /* bitpos */
128 complain_overflow_bitfield, /* complain_on_overflow */
129 bfd_elf_generic_reloc, /* special_function */
130 "R_PPC_ADDR16", /* name */
131 FALSE, /* partial_inplace */
132 0, /* src_mask */
133 0xffff, /* dst_mask */
134 FALSE), /* pcrel_offset */
136 /* A 16 bit relocation without overflow. */
137 HOWTO (R_PPC_ADDR16_LO, /* type */
138 0, /* rightshift */
139 1, /* size (0 = byte, 1 = short, 2 = long) */
140 16, /* bitsize */
141 FALSE, /* pc_relative */
142 0, /* bitpos */
143 complain_overflow_dont,/* complain_on_overflow */
144 bfd_elf_generic_reloc, /* special_function */
145 "R_PPC_ADDR16_LO", /* name */
146 FALSE, /* partial_inplace */
147 0, /* src_mask */
148 0xffff, /* dst_mask */
149 FALSE), /* pcrel_offset */
151 /* The high order 16 bits of an address. */
152 HOWTO (R_PPC_ADDR16_HI, /* type */
153 16, /* rightshift */
154 1, /* size (0 = byte, 1 = short, 2 = long) */
155 16, /* bitsize */
156 FALSE, /* pc_relative */
157 0, /* bitpos */
158 complain_overflow_dont, /* complain_on_overflow */
159 bfd_elf_generic_reloc, /* special_function */
160 "R_PPC_ADDR16_HI", /* name */
161 FALSE, /* partial_inplace */
162 0, /* src_mask */
163 0xffff, /* dst_mask */
164 FALSE), /* pcrel_offset */
166 /* The high order 16 bits of an address, plus 1 if the contents of
167 the low 16 bits, treated as a signed number, is negative. */
168 HOWTO (R_PPC_ADDR16_HA, /* type */
169 16, /* rightshift */
170 1, /* size (0 = byte, 1 = short, 2 = long) */
171 16, /* bitsize */
172 FALSE, /* pc_relative */
173 0, /* bitpos */
174 complain_overflow_dont, /* complain_on_overflow */
175 ppc_elf_addr16_ha_reloc, /* special_function */
176 "R_PPC_ADDR16_HA", /* name */
177 FALSE, /* partial_inplace */
178 0, /* src_mask */
179 0xffff, /* dst_mask */
180 FALSE), /* pcrel_offset */
182 /* An absolute 16 bit branch; the lower two bits must be zero.
183 FIXME: we don't check that, we just clear them. */
184 HOWTO (R_PPC_ADDR14, /* type */
185 0, /* rightshift */
186 2, /* size (0 = byte, 1 = short, 2 = long) */
187 16, /* bitsize */
188 FALSE, /* pc_relative */
189 0, /* bitpos */
190 complain_overflow_bitfield, /* complain_on_overflow */
191 bfd_elf_generic_reloc, /* special_function */
192 "R_PPC_ADDR14", /* name */
193 FALSE, /* partial_inplace */
194 0, /* src_mask */
195 0xfffc, /* dst_mask */
196 FALSE), /* pcrel_offset */
198 /* An absolute 16 bit branch, for which bit 10 should be set to
199 indicate that the branch is expected to be taken. The lower two
200 bits must be zero. */
201 HOWTO (R_PPC_ADDR14_BRTAKEN, /* type */
202 0, /* rightshift */
203 2, /* size (0 = byte, 1 = short, 2 = long) */
204 16, /* bitsize */
205 FALSE, /* pc_relative */
206 0, /* bitpos */
207 complain_overflow_bitfield, /* complain_on_overflow */
208 bfd_elf_generic_reloc, /* special_function */
209 "R_PPC_ADDR14_BRTAKEN",/* name */
210 FALSE, /* partial_inplace */
211 0, /* src_mask */
212 0xfffc, /* dst_mask */
213 FALSE), /* pcrel_offset */
215 /* An absolute 16 bit branch, for which bit 10 should be set to
216 indicate that the branch is not expected to be taken. The lower
217 two bits must be zero. */
218 HOWTO (R_PPC_ADDR14_BRNTAKEN, /* type */
219 0, /* rightshift */
220 2, /* size (0 = byte, 1 = short, 2 = long) */
221 16, /* bitsize */
222 FALSE, /* pc_relative */
223 0, /* bitpos */
224 complain_overflow_bitfield, /* complain_on_overflow */
225 bfd_elf_generic_reloc, /* special_function */
226 "R_PPC_ADDR14_BRNTAKEN",/* name */
227 FALSE, /* partial_inplace */
228 0, /* src_mask */
229 0xfffc, /* dst_mask */
230 FALSE), /* pcrel_offset */
232 /* A relative 26 bit branch; the lower two bits must be zero. */
233 HOWTO (R_PPC_REL24, /* type */
234 0, /* rightshift */
235 2, /* size (0 = byte, 1 = short, 2 = long) */
236 26, /* bitsize */
237 TRUE, /* pc_relative */
238 0, /* bitpos */
239 complain_overflow_signed, /* complain_on_overflow */
240 bfd_elf_generic_reloc, /* special_function */
241 "R_PPC_REL24", /* name */
242 FALSE, /* partial_inplace */
243 0, /* src_mask */
244 0x3fffffc, /* dst_mask */
245 TRUE), /* pcrel_offset */
247 /* A relative 16 bit branch; the lower two bits must be zero. */
248 HOWTO (R_PPC_REL14, /* type */
249 0, /* rightshift */
250 2, /* size (0 = byte, 1 = short, 2 = long) */
251 16, /* bitsize */
252 TRUE, /* pc_relative */
253 0, /* bitpos */
254 complain_overflow_signed, /* complain_on_overflow */
255 bfd_elf_generic_reloc, /* special_function */
256 "R_PPC_REL14", /* name */
257 FALSE, /* partial_inplace */
258 0, /* src_mask */
259 0xfffc, /* dst_mask */
260 TRUE), /* pcrel_offset */
262 /* A relative 16 bit branch. Bit 10 should be set to indicate that
263 the branch is expected to be taken. The lower two bits must be
264 zero. */
265 HOWTO (R_PPC_REL14_BRTAKEN, /* type */
266 0, /* rightshift */
267 2, /* size (0 = byte, 1 = short, 2 = long) */
268 16, /* bitsize */
269 TRUE, /* pc_relative */
270 0, /* bitpos */
271 complain_overflow_signed, /* complain_on_overflow */
272 bfd_elf_generic_reloc, /* special_function */
273 "R_PPC_REL14_BRTAKEN", /* name */
274 FALSE, /* partial_inplace */
275 0, /* src_mask */
276 0xfffc, /* dst_mask */
277 TRUE), /* pcrel_offset */
279 /* A relative 16 bit branch. Bit 10 should be set to indicate that
280 the branch is not expected to be taken. The lower two bits must
281 be zero. */
282 HOWTO (R_PPC_REL14_BRNTAKEN, /* type */
283 0, /* rightshift */
284 2, /* size (0 = byte, 1 = short, 2 = long) */
285 16, /* bitsize */
286 TRUE, /* pc_relative */
287 0, /* bitpos */
288 complain_overflow_signed, /* complain_on_overflow */
289 bfd_elf_generic_reloc, /* special_function */
290 "R_PPC_REL14_BRNTAKEN",/* name */
291 FALSE, /* partial_inplace */
292 0, /* src_mask */
293 0xfffc, /* dst_mask */
294 TRUE), /* pcrel_offset */
296 /* Like R_PPC_ADDR16, but referring to the GOT table entry for the
297 symbol. */
298 HOWTO (R_PPC_GOT16, /* type */
299 0, /* rightshift */
300 1, /* size (0 = byte, 1 = short, 2 = long) */
301 16, /* bitsize */
302 FALSE, /* pc_relative */
303 0, /* bitpos */
304 complain_overflow_signed, /* complain_on_overflow */
305 bfd_elf_generic_reloc, /* special_function */
306 "R_PPC_GOT16", /* name */
307 FALSE, /* partial_inplace */
308 0, /* src_mask */
309 0xffff, /* dst_mask */
310 FALSE), /* pcrel_offset */
312 /* Like R_PPC_ADDR16_LO, but referring to the GOT table entry for
313 the symbol. */
314 HOWTO (R_PPC_GOT16_LO, /* type */
315 0, /* rightshift */
316 1, /* size (0 = byte, 1 = short, 2 = long) */
317 16, /* bitsize */
318 FALSE, /* pc_relative */
319 0, /* bitpos */
320 complain_overflow_dont, /* complain_on_overflow */
321 bfd_elf_generic_reloc, /* special_function */
322 "R_PPC_GOT16_LO", /* name */
323 FALSE, /* partial_inplace */
324 0, /* src_mask */
325 0xffff, /* dst_mask */
326 FALSE), /* pcrel_offset */
328 /* Like R_PPC_ADDR16_HI, but referring to the GOT table entry for
329 the symbol. */
330 HOWTO (R_PPC_GOT16_HI, /* type */
331 16, /* rightshift */
332 1, /* size (0 = byte, 1 = short, 2 = long) */
333 16, /* bitsize */
334 FALSE, /* pc_relative */
335 0, /* bitpos */
336 complain_overflow_bitfield, /* complain_on_overflow */
337 bfd_elf_generic_reloc, /* special_function */
338 "R_PPC_GOT16_HI", /* name */
339 FALSE, /* partial_inplace */
340 0, /* src_mask */
341 0xffff, /* dst_mask */
342 FALSE), /* pcrel_offset */
344 /* Like R_PPC_ADDR16_HA, but referring to the GOT table entry for
345 the symbol. */
346 HOWTO (R_PPC_GOT16_HA, /* type */
347 16, /* rightshift */
348 1, /* size (0 = byte, 1 = short, 2 = long) */
349 16, /* bitsize */
350 FALSE, /* pc_relative */
351 0, /* bitpos */
352 complain_overflow_bitfield, /* complain_on_overflow */
353 ppc_elf_addr16_ha_reloc, /* special_function */
354 "R_PPC_GOT16_HA", /* name */
355 FALSE, /* partial_inplace */
356 0, /* src_mask */
357 0xffff, /* dst_mask */
358 FALSE), /* pcrel_offset */
360 /* Like R_PPC_REL24, but referring to the procedure linkage table
361 entry for the symbol. */
362 HOWTO (R_PPC_PLTREL24, /* type */
363 0, /* rightshift */
364 2, /* size (0 = byte, 1 = short, 2 = long) */
365 26, /* bitsize */
366 TRUE, /* pc_relative */
367 0, /* bitpos */
368 complain_overflow_signed, /* complain_on_overflow */
369 bfd_elf_generic_reloc, /* special_function */
370 "R_PPC_PLTREL24", /* name */
371 FALSE, /* partial_inplace */
372 0, /* src_mask */
373 0x3fffffc, /* dst_mask */
374 TRUE), /* pcrel_offset */
376 /* This is used only by the dynamic linker. The symbol should exist
377 both in the object being run and in some shared library. The
378 dynamic linker copies the data addressed by the symbol from the
379 shared library into the object, because the object being
380 run has to have the data at some particular address. */
381 HOWTO (R_PPC_COPY, /* type */
382 0, /* rightshift */
383 2, /* size (0 = byte, 1 = short, 2 = long) */
384 32, /* bitsize */
385 FALSE, /* pc_relative */
386 0, /* bitpos */
387 complain_overflow_bitfield, /* complain_on_overflow */
388 bfd_elf_generic_reloc, /* special_function */
389 "R_PPC_COPY", /* name */
390 FALSE, /* partial_inplace */
391 0, /* src_mask */
392 0, /* dst_mask */
393 FALSE), /* pcrel_offset */
395 /* Like R_PPC_ADDR32, but used when setting global offset table
396 entries. */
397 HOWTO (R_PPC_GLOB_DAT, /* type */
398 0, /* rightshift */
399 2, /* size (0 = byte, 1 = short, 2 = long) */
400 32, /* bitsize */
401 FALSE, /* pc_relative */
402 0, /* bitpos */
403 complain_overflow_bitfield, /* complain_on_overflow */
404 bfd_elf_generic_reloc, /* special_function */
405 "R_PPC_GLOB_DAT", /* name */
406 FALSE, /* partial_inplace */
407 0, /* src_mask */
408 0xffffffff, /* dst_mask */
409 FALSE), /* pcrel_offset */
411 /* Marks a procedure linkage table entry for a symbol. */
412 HOWTO (R_PPC_JMP_SLOT, /* type */
413 0, /* rightshift */
414 2, /* size (0 = byte, 1 = short, 2 = long) */
415 32, /* bitsize */
416 FALSE, /* pc_relative */
417 0, /* bitpos */
418 complain_overflow_bitfield, /* complain_on_overflow */
419 bfd_elf_generic_reloc, /* special_function */
420 "R_PPC_JMP_SLOT", /* name */
421 FALSE, /* partial_inplace */
422 0, /* src_mask */
423 0, /* dst_mask */
424 FALSE), /* pcrel_offset */
426 /* Used only by the dynamic linker. When the object is run, this
427 longword is set to the load address of the object, plus the
428 addend. */
429 HOWTO (R_PPC_RELATIVE, /* type */
430 0, /* rightshift */
431 2, /* size (0 = byte, 1 = short, 2 = long) */
432 32, /* bitsize */
433 FALSE, /* pc_relative */
434 0, /* bitpos */
435 complain_overflow_bitfield, /* complain_on_overflow */
436 bfd_elf_generic_reloc, /* special_function */
437 "R_PPC_RELATIVE", /* name */
438 FALSE, /* partial_inplace */
439 0, /* src_mask */
440 0xffffffff, /* dst_mask */
441 FALSE), /* pcrel_offset */
443 /* Like R_PPC_REL24, but uses the value of the symbol within the
444 object rather than the final value. Normally used for
445 _GLOBAL_OFFSET_TABLE_. */
446 HOWTO (R_PPC_LOCAL24PC, /* type */
447 0, /* rightshift */
448 2, /* size (0 = byte, 1 = short, 2 = long) */
449 26, /* bitsize */
450 TRUE, /* pc_relative */
451 0, /* bitpos */
452 complain_overflow_signed, /* complain_on_overflow */
453 bfd_elf_generic_reloc, /* special_function */
454 "R_PPC_LOCAL24PC", /* name */
455 FALSE, /* partial_inplace */
456 0, /* src_mask */
457 0x3fffffc, /* dst_mask */
458 TRUE), /* pcrel_offset */
460 /* Like R_PPC_ADDR32, but may be unaligned. */
461 HOWTO (R_PPC_UADDR32, /* type */
462 0, /* rightshift */
463 2, /* size (0 = byte, 1 = short, 2 = long) */
464 32, /* bitsize */
465 FALSE, /* pc_relative */
466 0, /* bitpos */
467 complain_overflow_bitfield, /* complain_on_overflow */
468 bfd_elf_generic_reloc, /* special_function */
469 "R_PPC_UADDR32", /* name */
470 FALSE, /* partial_inplace */
471 0, /* src_mask */
472 0xffffffff, /* dst_mask */
473 FALSE), /* pcrel_offset */
475 /* Like R_PPC_ADDR16, but may be unaligned. */
476 HOWTO (R_PPC_UADDR16, /* type */
477 0, /* rightshift */
478 1, /* size (0 = byte, 1 = short, 2 = long) */
479 16, /* bitsize */
480 FALSE, /* pc_relative */
481 0, /* bitpos */
482 complain_overflow_bitfield, /* complain_on_overflow */
483 bfd_elf_generic_reloc, /* special_function */
484 "R_PPC_UADDR16", /* name */
485 FALSE, /* partial_inplace */
486 0, /* src_mask */
487 0xffff, /* dst_mask */
488 FALSE), /* pcrel_offset */
490 /* 32-bit PC relative */
491 HOWTO (R_PPC_REL32, /* type */
492 0, /* rightshift */
493 2, /* size (0 = byte, 1 = short, 2 = long) */
494 32, /* bitsize */
495 TRUE, /* pc_relative */
496 0, /* bitpos */
497 complain_overflow_bitfield, /* complain_on_overflow */
498 bfd_elf_generic_reloc, /* special_function */
499 "R_PPC_REL32", /* name */
500 FALSE, /* partial_inplace */
501 0, /* src_mask */
502 0xffffffff, /* dst_mask */
503 TRUE), /* pcrel_offset */
505 /* 32-bit relocation to the symbol's procedure linkage table.
506 FIXME: not supported. */
507 HOWTO (R_PPC_PLT32, /* type */
508 0, /* rightshift */
509 2, /* size (0 = byte, 1 = short, 2 = long) */
510 32, /* bitsize */
511 FALSE, /* pc_relative */
512 0, /* bitpos */
513 complain_overflow_bitfield, /* complain_on_overflow */
514 bfd_elf_generic_reloc, /* special_function */
515 "R_PPC_PLT32", /* name */
516 FALSE, /* partial_inplace */
517 0, /* src_mask */
518 0, /* dst_mask */
519 FALSE), /* pcrel_offset */
521 /* 32-bit PC relative relocation to the symbol's procedure linkage table.
522 FIXME: not supported. */
523 HOWTO (R_PPC_PLTREL32, /* type */
524 0, /* rightshift */
525 2, /* size (0 = byte, 1 = short, 2 = long) */
526 32, /* bitsize */
527 TRUE, /* pc_relative */
528 0, /* bitpos */
529 complain_overflow_bitfield, /* complain_on_overflow */
530 bfd_elf_generic_reloc, /* special_function */
531 "R_PPC_PLTREL32", /* name */
532 FALSE, /* partial_inplace */
533 0, /* src_mask */
534 0, /* dst_mask */
535 TRUE), /* pcrel_offset */
537 /* Like R_PPC_ADDR16_LO, but referring to the PLT table entry for
538 the symbol. */
539 HOWTO (R_PPC_PLT16_LO, /* type */
540 0, /* rightshift */
541 1, /* size (0 = byte, 1 = short, 2 = long) */
542 16, /* bitsize */
543 FALSE, /* pc_relative */
544 0, /* bitpos */
545 complain_overflow_dont, /* complain_on_overflow */
546 bfd_elf_generic_reloc, /* special_function */
547 "R_PPC_PLT16_LO", /* name */
548 FALSE, /* partial_inplace */
549 0, /* src_mask */
550 0xffff, /* dst_mask */
551 FALSE), /* pcrel_offset */
553 /* Like R_PPC_ADDR16_HI, but referring to the PLT table entry for
554 the symbol. */
555 HOWTO (R_PPC_PLT16_HI, /* type */
556 16, /* rightshift */
557 1, /* size (0 = byte, 1 = short, 2 = long) */
558 16, /* bitsize */
559 FALSE, /* pc_relative */
560 0, /* bitpos */
561 complain_overflow_bitfield, /* complain_on_overflow */
562 bfd_elf_generic_reloc, /* special_function */
563 "R_PPC_PLT16_HI", /* name */
564 FALSE, /* partial_inplace */
565 0, /* src_mask */
566 0xffff, /* dst_mask */
567 FALSE), /* pcrel_offset */
569 /* Like R_PPC_ADDR16_HA, but referring to the PLT table entry for
570 the symbol. */
571 HOWTO (R_PPC_PLT16_HA, /* type */
572 16, /* rightshift */
573 1, /* size (0 = byte, 1 = short, 2 = long) */
574 16, /* bitsize */
575 FALSE, /* pc_relative */
576 0, /* bitpos */
577 complain_overflow_bitfield, /* complain_on_overflow */
578 ppc_elf_addr16_ha_reloc, /* special_function */
579 "R_PPC_PLT16_HA", /* name */
580 FALSE, /* partial_inplace */
581 0, /* src_mask */
582 0xffff, /* dst_mask */
583 FALSE), /* pcrel_offset */
585 /* A sign-extended 16 bit value relative to _SDA_BASE_, for use with
586 small data items. */
587 HOWTO (R_PPC_SDAREL16, /* type */
588 0, /* rightshift */
589 1, /* size (0 = byte, 1 = short, 2 = long) */
590 16, /* bitsize */
591 FALSE, /* pc_relative */
592 0, /* bitpos */
593 complain_overflow_signed, /* complain_on_overflow */
594 bfd_elf_generic_reloc, /* special_function */
595 "R_PPC_SDAREL16", /* name */
596 FALSE, /* partial_inplace */
597 0, /* src_mask */
598 0xffff, /* dst_mask */
599 FALSE), /* pcrel_offset */
601 /* 16-bit section relative relocation. */
602 HOWTO (R_PPC_SECTOFF, /* type */
603 0, /* rightshift */
604 1, /* size (0 = byte, 1 = short, 2 = long) */
605 16, /* bitsize */
606 FALSE, /* pc_relative */
607 0, /* bitpos */
608 complain_overflow_bitfield, /* complain_on_overflow */
609 bfd_elf_generic_reloc, /* special_function */
610 "R_PPC_SECTOFF", /* name */
611 FALSE, /* partial_inplace */
612 0, /* src_mask */
613 0xffff, /* dst_mask */
614 FALSE), /* pcrel_offset */
616 /* 16-bit lower half section relative relocation. */
617 HOWTO (R_PPC_SECTOFF_LO, /* type */
618 0, /* rightshift */
619 1, /* size (0 = byte, 1 = short, 2 = long) */
620 16, /* bitsize */
621 FALSE, /* pc_relative */
622 0, /* bitpos */
623 complain_overflow_dont, /* complain_on_overflow */
624 bfd_elf_generic_reloc, /* special_function */
625 "R_PPC_SECTOFF_LO", /* name */
626 FALSE, /* partial_inplace */
627 0, /* src_mask */
628 0xffff, /* dst_mask */
629 FALSE), /* pcrel_offset */
631 /* 16-bit upper half section relative relocation. */
632 HOWTO (R_PPC_SECTOFF_HI, /* type */
633 16, /* rightshift */
634 1, /* size (0 = byte, 1 = short, 2 = long) */
635 16, /* bitsize */
636 FALSE, /* pc_relative */
637 0, /* bitpos */
638 complain_overflow_bitfield, /* complain_on_overflow */
639 bfd_elf_generic_reloc, /* special_function */
640 "R_PPC_SECTOFF_HI", /* name */
641 FALSE, /* partial_inplace */
642 0, /* src_mask */
643 0xffff, /* dst_mask */
644 FALSE), /* pcrel_offset */
646 /* 16-bit upper half adjusted section relative relocation. */
647 HOWTO (R_PPC_SECTOFF_HA, /* type */
648 16, /* rightshift */
649 1, /* size (0 = byte, 1 = short, 2 = long) */
650 16, /* bitsize */
651 FALSE, /* pc_relative */
652 0, /* bitpos */
653 complain_overflow_bitfield, /* complain_on_overflow */
654 ppc_elf_addr16_ha_reloc, /* special_function */
655 "R_PPC_SECTOFF_HA", /* name */
656 FALSE, /* partial_inplace */
657 0, /* src_mask */
658 0xffff, /* dst_mask */
659 FALSE), /* pcrel_offset */
661 /* Marker reloc for TLS. */
662 HOWTO (R_PPC_TLS,
663 0, /* rightshift */
664 2, /* size (0 = byte, 1 = short, 2 = long) */
665 32, /* bitsize */
666 FALSE, /* pc_relative */
667 0, /* bitpos */
668 complain_overflow_dont, /* complain_on_overflow */
669 bfd_elf_generic_reloc, /* special_function */
670 "R_PPC_TLS", /* name */
671 FALSE, /* partial_inplace */
672 0, /* src_mask */
673 0, /* dst_mask */
674 FALSE), /* pcrel_offset */
676 /* Computes the load module index of the load module that contains the
677 definition of its TLS sym. */
678 HOWTO (R_PPC_DTPMOD32,
679 0, /* rightshift */
680 2, /* size (0 = byte, 1 = short, 2 = long) */
681 32, /* bitsize */
682 FALSE, /* pc_relative */
683 0, /* bitpos */
684 complain_overflow_dont, /* complain_on_overflow */
685 ppc_elf_unhandled_reloc, /* special_function */
686 "R_PPC_DTPMOD32", /* name */
687 FALSE, /* partial_inplace */
688 0, /* src_mask */
689 0xffffffff, /* dst_mask */
690 FALSE), /* pcrel_offset */
692 /* Computes a dtv-relative displacement, the difference between the value
693 of sym+add and the base address of the thread-local storage block that
694 contains the definition of sym, minus 0x8000. */
695 HOWTO (R_PPC_DTPREL32,
696 0, /* rightshift */
697 2, /* size (0 = byte, 1 = short, 2 = long) */
698 32, /* bitsize */
699 FALSE, /* pc_relative */
700 0, /* bitpos */
701 complain_overflow_dont, /* complain_on_overflow */
702 ppc_elf_unhandled_reloc, /* special_function */
703 "R_PPC_DTPREL32", /* name */
704 FALSE, /* partial_inplace */
705 0, /* src_mask */
706 0xffffffff, /* dst_mask */
707 FALSE), /* pcrel_offset */
709 /* A 16 bit dtprel reloc. */
710 HOWTO (R_PPC_DTPREL16,
711 0, /* rightshift */
712 1, /* size (0 = byte, 1 = short, 2 = long) */
713 16, /* bitsize */
714 FALSE, /* pc_relative */
715 0, /* bitpos */
716 complain_overflow_signed, /* complain_on_overflow */
717 ppc_elf_unhandled_reloc, /* special_function */
718 "R_PPC_DTPREL16", /* name */
719 FALSE, /* partial_inplace */
720 0, /* src_mask */
721 0xffff, /* dst_mask */
722 FALSE), /* pcrel_offset */
724 /* Like DTPREL16, but no overflow. */
725 HOWTO (R_PPC_DTPREL16_LO,
726 0, /* rightshift */
727 1, /* size (0 = byte, 1 = short, 2 = long) */
728 16, /* bitsize */
729 FALSE, /* pc_relative */
730 0, /* bitpos */
731 complain_overflow_dont, /* complain_on_overflow */
732 ppc_elf_unhandled_reloc, /* special_function */
733 "R_PPC_DTPREL16_LO", /* name */
734 FALSE, /* partial_inplace */
735 0, /* src_mask */
736 0xffff, /* dst_mask */
737 FALSE), /* pcrel_offset */
739 /* Like DTPREL16_LO, but next higher group of 16 bits. */
740 HOWTO (R_PPC_DTPREL16_HI,
741 16, /* rightshift */
742 1, /* size (0 = byte, 1 = short, 2 = long) */
743 16, /* bitsize */
744 FALSE, /* pc_relative */
745 0, /* bitpos */
746 complain_overflow_dont, /* complain_on_overflow */
747 ppc_elf_unhandled_reloc, /* special_function */
748 "R_PPC_DTPREL16_HI", /* name */
749 FALSE, /* partial_inplace */
750 0, /* src_mask */
751 0xffff, /* dst_mask */
752 FALSE), /* pcrel_offset */
754 /* Like DTPREL16_HI, but adjust for low 16 bits. */
755 HOWTO (R_PPC_DTPREL16_HA,
756 16, /* rightshift */
757 1, /* size (0 = byte, 1 = short, 2 = long) */
758 16, /* bitsize */
759 FALSE, /* pc_relative */
760 0, /* bitpos */
761 complain_overflow_dont, /* complain_on_overflow */
762 ppc_elf_unhandled_reloc, /* special_function */
763 "R_PPC_DTPREL16_HA", /* name */
764 FALSE, /* partial_inplace */
765 0, /* src_mask */
766 0xffff, /* dst_mask */
767 FALSE), /* pcrel_offset */
769 /* Computes a tp-relative displacement, the difference between the value of
770 sym+add and the value of the thread pointer (r13). */
771 HOWTO (R_PPC_TPREL32,
772 0, /* rightshift */
773 2, /* size (0 = byte, 1 = short, 2 = long) */
774 32, /* bitsize */
775 FALSE, /* pc_relative */
776 0, /* bitpos */
777 complain_overflow_dont, /* complain_on_overflow */
778 ppc_elf_unhandled_reloc, /* special_function */
779 "R_PPC_TPREL32", /* name */
780 FALSE, /* partial_inplace */
781 0, /* src_mask */
782 0xffffffff, /* dst_mask */
783 FALSE), /* pcrel_offset */
785 /* A 16 bit tprel reloc. */
786 HOWTO (R_PPC_TPREL16,
787 0, /* rightshift */
788 1, /* size (0 = byte, 1 = short, 2 = long) */
789 16, /* bitsize */
790 FALSE, /* pc_relative */
791 0, /* bitpos */
792 complain_overflow_signed, /* complain_on_overflow */
793 ppc_elf_unhandled_reloc, /* special_function */
794 "R_PPC_TPREL16", /* name */
795 FALSE, /* partial_inplace */
796 0, /* src_mask */
797 0xffff, /* dst_mask */
798 FALSE), /* pcrel_offset */
800 /* Like TPREL16, but no overflow. */
801 HOWTO (R_PPC_TPREL16_LO,
802 0, /* rightshift */
803 1, /* size (0 = byte, 1 = short, 2 = long) */
804 16, /* bitsize */
805 FALSE, /* pc_relative */
806 0, /* bitpos */
807 complain_overflow_dont, /* complain_on_overflow */
808 ppc_elf_unhandled_reloc, /* special_function */
809 "R_PPC_TPREL16_LO", /* name */
810 FALSE, /* partial_inplace */
811 0, /* src_mask */
812 0xffff, /* dst_mask */
813 FALSE), /* pcrel_offset */
815 /* Like TPREL16_LO, but next higher group of 16 bits. */
816 HOWTO (R_PPC_TPREL16_HI,
817 16, /* rightshift */
818 1, /* size (0 = byte, 1 = short, 2 = long) */
819 16, /* bitsize */
820 FALSE, /* pc_relative */
821 0, /* bitpos */
822 complain_overflow_dont, /* complain_on_overflow */
823 ppc_elf_unhandled_reloc, /* special_function */
824 "R_PPC_TPREL16_HI", /* name */
825 FALSE, /* partial_inplace */
826 0, /* src_mask */
827 0xffff, /* dst_mask */
828 FALSE), /* pcrel_offset */
830 /* Like TPREL16_HI, but adjust for low 16 bits. */
831 HOWTO (R_PPC_TPREL16_HA,
832 16, /* rightshift */
833 1, /* size (0 = byte, 1 = short, 2 = long) */
834 16, /* bitsize */
835 FALSE, /* pc_relative */
836 0, /* bitpos */
837 complain_overflow_dont, /* complain_on_overflow */
838 ppc_elf_unhandled_reloc, /* special_function */
839 "R_PPC_TPREL16_HA", /* name */
840 FALSE, /* partial_inplace */
841 0, /* src_mask */
842 0xffff, /* dst_mask */
843 FALSE), /* pcrel_offset */
845 /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
846 with values (sym+add)@dtpmod and (sym+add)@dtprel, and computes the offset
847 to the first entry. */
848 HOWTO (R_PPC_GOT_TLSGD16,
849 0, /* rightshift */
850 1, /* size (0 = byte, 1 = short, 2 = long) */
851 16, /* bitsize */
852 FALSE, /* pc_relative */
853 0, /* bitpos */
854 complain_overflow_signed, /* complain_on_overflow */
855 ppc_elf_unhandled_reloc, /* special_function */
856 "R_PPC_GOT_TLSGD16", /* name */
857 FALSE, /* partial_inplace */
858 0, /* src_mask */
859 0xffff, /* dst_mask */
860 FALSE), /* pcrel_offset */
862 /* Like GOT_TLSGD16, but no overflow. */
863 HOWTO (R_PPC_GOT_TLSGD16_LO,
864 0, /* rightshift */
865 1, /* size (0 = byte, 1 = short, 2 = long) */
866 16, /* bitsize */
867 FALSE, /* pc_relative */
868 0, /* bitpos */
869 complain_overflow_dont, /* complain_on_overflow */
870 ppc_elf_unhandled_reloc, /* special_function */
871 "R_PPC_GOT_TLSGD16_LO", /* name */
872 FALSE, /* partial_inplace */
873 0, /* src_mask */
874 0xffff, /* dst_mask */
875 FALSE), /* pcrel_offset */
877 /* Like GOT_TLSGD16_LO, but next higher group of 16 bits. */
878 HOWTO (R_PPC_GOT_TLSGD16_HI,
879 16, /* rightshift */
880 1, /* size (0 = byte, 1 = short, 2 = long) */
881 16, /* bitsize */
882 FALSE, /* pc_relative */
883 0, /* bitpos */
884 complain_overflow_dont, /* complain_on_overflow */
885 ppc_elf_unhandled_reloc, /* special_function */
886 "R_PPC_GOT_TLSGD16_HI", /* name */
887 FALSE, /* partial_inplace */
888 0, /* src_mask */
889 0xffff, /* dst_mask */
890 FALSE), /* pcrel_offset */
892 /* Like GOT_TLSGD16_HI, but adjust for low 16 bits. */
893 HOWTO (R_PPC_GOT_TLSGD16_HA,
894 16, /* rightshift */
895 1, /* size (0 = byte, 1 = short, 2 = long) */
896 16, /* bitsize */
897 FALSE, /* pc_relative */
898 0, /* bitpos */
899 complain_overflow_dont, /* complain_on_overflow */
900 ppc_elf_unhandled_reloc, /* special_function */
901 "R_PPC_GOT_TLSGD16_HA", /* name */
902 FALSE, /* partial_inplace */
903 0, /* src_mask */
904 0xffff, /* dst_mask */
905 FALSE), /* pcrel_offset */
907 /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
908 with values (sym+add)@dtpmod and zero, and computes the offset to the
909 first entry. */
910 HOWTO (R_PPC_GOT_TLSLD16,
911 0, /* rightshift */
912 1, /* size (0 = byte, 1 = short, 2 = long) */
913 16, /* bitsize */
914 FALSE, /* pc_relative */
915 0, /* bitpos */
916 complain_overflow_signed, /* complain_on_overflow */
917 ppc_elf_unhandled_reloc, /* special_function */
918 "R_PPC_GOT_TLSLD16", /* name */
919 FALSE, /* partial_inplace */
920 0, /* src_mask */
921 0xffff, /* dst_mask */
922 FALSE), /* pcrel_offset */
924 /* Like GOT_TLSLD16, but no overflow. */
925 HOWTO (R_PPC_GOT_TLSLD16_LO,
926 0, /* rightshift */
927 1, /* size (0 = byte, 1 = short, 2 = long) */
928 16, /* bitsize */
929 FALSE, /* pc_relative */
930 0, /* bitpos */
931 complain_overflow_dont, /* complain_on_overflow */
932 ppc_elf_unhandled_reloc, /* special_function */
933 "R_PPC_GOT_TLSLD16_LO", /* name */
934 FALSE, /* partial_inplace */
935 0, /* src_mask */
936 0xffff, /* dst_mask */
937 FALSE), /* pcrel_offset */
939 /* Like GOT_TLSLD16_LO, but next higher group of 16 bits. */
940 HOWTO (R_PPC_GOT_TLSLD16_HI,
941 16, /* rightshift */
942 1, /* size (0 = byte, 1 = short, 2 = long) */
943 16, /* bitsize */
944 FALSE, /* pc_relative */
945 0, /* bitpos */
946 complain_overflow_dont, /* complain_on_overflow */
947 ppc_elf_unhandled_reloc, /* special_function */
948 "R_PPC_GOT_TLSLD16_HI", /* name */
949 FALSE, /* partial_inplace */
950 0, /* src_mask */
951 0xffff, /* dst_mask */
952 FALSE), /* pcrel_offset */
954 /* Like GOT_TLSLD16_HI, but adjust for low 16 bits. */
955 HOWTO (R_PPC_GOT_TLSLD16_HA,
956 16, /* rightshift */
957 1, /* size (0 = byte, 1 = short, 2 = long) */
958 16, /* bitsize */
959 FALSE, /* pc_relative */
960 0, /* bitpos */
961 complain_overflow_dont, /* complain_on_overflow */
962 ppc_elf_unhandled_reloc, /* special_function */
963 "R_PPC_GOT_TLSLD16_HA", /* name */
964 FALSE, /* partial_inplace */
965 0, /* src_mask */
966 0xffff, /* dst_mask */
967 FALSE), /* pcrel_offset */
969 /* Allocates an entry in the GOT with value (sym+add)@dtprel, and computes
970 the offset to the entry. */
971 HOWTO (R_PPC_GOT_DTPREL16,
972 0, /* rightshift */
973 1, /* size (0 = byte, 1 = short, 2 = long) */
974 16, /* bitsize */
975 FALSE, /* pc_relative */
976 0, /* bitpos */
977 complain_overflow_signed, /* complain_on_overflow */
978 ppc_elf_unhandled_reloc, /* special_function */
979 "R_PPC_GOT_DTPREL16", /* name */
980 FALSE, /* partial_inplace */
981 0, /* src_mask */
982 0xffff, /* dst_mask */
983 FALSE), /* pcrel_offset */
985 /* Like GOT_DTPREL16, but no overflow. */
986 HOWTO (R_PPC_GOT_DTPREL16_LO,
987 0, /* rightshift */
988 1, /* size (0 = byte, 1 = short, 2 = long) */
989 16, /* bitsize */
990 FALSE, /* pc_relative */
991 0, /* bitpos */
992 complain_overflow_dont, /* complain_on_overflow */
993 ppc_elf_unhandled_reloc, /* special_function */
994 "R_PPC_GOT_DTPREL16_LO", /* name */
995 FALSE, /* partial_inplace */
996 0, /* src_mask */
997 0xffff, /* dst_mask */
998 FALSE), /* pcrel_offset */
1000 /* Like GOT_DTPREL16_LO, but next higher group of 16 bits. */
1001 HOWTO (R_PPC_GOT_DTPREL16_HI,
1002 16, /* rightshift */
1003 1, /* size (0 = byte, 1 = short, 2 = long) */
1004 16, /* bitsize */
1005 FALSE, /* pc_relative */
1006 0, /* bitpos */
1007 complain_overflow_dont, /* complain_on_overflow */
1008 ppc_elf_unhandled_reloc, /* special_function */
1009 "R_PPC_GOT_DTPREL16_HI", /* name */
1010 FALSE, /* partial_inplace */
1011 0, /* src_mask */
1012 0xffff, /* dst_mask */
1013 FALSE), /* pcrel_offset */
1015 /* Like GOT_DTPREL16_HI, but adjust for low 16 bits. */
1016 HOWTO (R_PPC_GOT_DTPREL16_HA,
1017 16, /* rightshift */
1018 1, /* size (0 = byte, 1 = short, 2 = long) */
1019 16, /* bitsize */
1020 FALSE, /* pc_relative */
1021 0, /* bitpos */
1022 complain_overflow_dont, /* complain_on_overflow */
1023 ppc_elf_unhandled_reloc, /* special_function */
1024 "R_PPC_GOT_DTPREL16_HA", /* name */
1025 FALSE, /* partial_inplace */
1026 0, /* src_mask */
1027 0xffff, /* dst_mask */
1028 FALSE), /* pcrel_offset */
1030 /* Allocates an entry in the GOT with value (sym+add)@tprel, and computes the
1031 offset to the entry. */
1032 HOWTO (R_PPC_GOT_TPREL16,
1033 0, /* rightshift */
1034 1, /* size (0 = byte, 1 = short, 2 = long) */
1035 16, /* bitsize */
1036 FALSE, /* pc_relative */
1037 0, /* bitpos */
1038 complain_overflow_signed, /* complain_on_overflow */
1039 ppc_elf_unhandled_reloc, /* special_function */
1040 "R_PPC_GOT_TPREL16", /* name */
1041 FALSE, /* partial_inplace */
1042 0, /* src_mask */
1043 0xffff, /* dst_mask */
1044 FALSE), /* pcrel_offset */
1046 /* Like GOT_TPREL16, but no overflow. */
1047 HOWTO (R_PPC_GOT_TPREL16_LO,
1048 0, /* rightshift */
1049 1, /* size (0 = byte, 1 = short, 2 = long) */
1050 16, /* bitsize */
1051 FALSE, /* pc_relative */
1052 0, /* bitpos */
1053 complain_overflow_dont, /* complain_on_overflow */
1054 ppc_elf_unhandled_reloc, /* special_function */
1055 "R_PPC_GOT_TPREL16_LO", /* name */
1056 FALSE, /* partial_inplace */
1057 0, /* src_mask */
1058 0xffff, /* dst_mask */
1059 FALSE), /* pcrel_offset */
1061 /* Like GOT_TPREL16_LO, but next higher group of 16 bits. */
1062 HOWTO (R_PPC_GOT_TPREL16_HI,
1063 16, /* rightshift */
1064 1, /* size (0 = byte, 1 = short, 2 = long) */
1065 16, /* bitsize */
1066 FALSE, /* pc_relative */
1067 0, /* bitpos */
1068 complain_overflow_dont, /* complain_on_overflow */
1069 ppc_elf_unhandled_reloc, /* special_function */
1070 "R_PPC_GOT_TPREL16_HI", /* name */
1071 FALSE, /* partial_inplace */
1072 0, /* src_mask */
1073 0xffff, /* dst_mask */
1074 FALSE), /* pcrel_offset */
1076 /* Like GOT_TPREL16_HI, but adjust for low 16 bits. */
1077 HOWTO (R_PPC_GOT_TPREL16_HA,
1078 16, /* rightshift */
1079 1, /* size (0 = byte, 1 = short, 2 = long) */
1080 16, /* bitsize */
1081 FALSE, /* pc_relative */
1082 0, /* bitpos */
1083 complain_overflow_dont, /* complain_on_overflow */
1084 ppc_elf_unhandled_reloc, /* special_function */
1085 "R_PPC_GOT_TPREL16_HA", /* name */
1086 FALSE, /* partial_inplace */
1087 0, /* src_mask */
1088 0xffff, /* dst_mask */
1089 FALSE), /* pcrel_offset */
1091 /* The remaining relocs are from the Embedded ELF ABI, and are not
1092 in the SVR4 ELF ABI. */
1094 /* 32 bit value resulting from the addend minus the symbol. */
1095 HOWTO (R_PPC_EMB_NADDR32, /* type */
1096 0, /* rightshift */
1097 2, /* size (0 = byte, 1 = short, 2 = long) */
1098 32, /* bitsize */
1099 FALSE, /* pc_relative */
1100 0, /* bitpos */
1101 complain_overflow_bitfield, /* complain_on_overflow */
1102 bfd_elf_generic_reloc, /* special_function */
1103 "R_PPC_EMB_NADDR32", /* name */
1104 FALSE, /* partial_inplace */
1105 0, /* src_mask */
1106 0xffffffff, /* dst_mask */
1107 FALSE), /* pcrel_offset */
1109 /* 16 bit value resulting from the addend minus the symbol. */
1110 HOWTO (R_PPC_EMB_NADDR16, /* type */
1111 0, /* rightshift */
1112 1, /* size (0 = byte, 1 = short, 2 = long) */
1113 16, /* bitsize */
1114 FALSE, /* pc_relative */
1115 0, /* bitpos */
1116 complain_overflow_bitfield, /* complain_on_overflow */
1117 bfd_elf_generic_reloc, /* special_function */
1118 "R_PPC_EMB_NADDR16", /* name */
1119 FALSE, /* partial_inplace */
1120 0, /* src_mask */
1121 0xffff, /* dst_mask */
1122 FALSE), /* pcrel_offset */
1124 /* 16 bit value resulting from the addend minus the symbol. */
1125 HOWTO (R_PPC_EMB_NADDR16_LO, /* type */
1126 0, /* rightshift */
1127 1, /* size (0 = byte, 1 = short, 2 = long) */
1128 16, /* bitsize */
1129 FALSE, /* pc_relative */
1130 0, /* bitpos */
1131 complain_overflow_dont,/* complain_on_overflow */
1132 bfd_elf_generic_reloc, /* special_function */
1133 "R_PPC_EMB_ADDR16_LO", /* name */
1134 FALSE, /* partial_inplace */
1135 0, /* src_mask */
1136 0xffff, /* dst_mask */
1137 FALSE), /* pcrel_offset */
1139 /* The high order 16 bits of the addend minus the symbol. */
1140 HOWTO (R_PPC_EMB_NADDR16_HI, /* type */
1141 16, /* rightshift */
1142 1, /* size (0 = byte, 1 = short, 2 = long) */
1143 16, /* bitsize */
1144 FALSE, /* pc_relative */
1145 0, /* bitpos */
1146 complain_overflow_dont, /* complain_on_overflow */
1147 bfd_elf_generic_reloc, /* special_function */
1148 "R_PPC_EMB_NADDR16_HI", /* name */
1149 FALSE, /* partial_inplace */
1150 0, /* src_mask */
1151 0xffff, /* dst_mask */
1152 FALSE), /* pcrel_offset */
1154 /* The high order 16 bits of the result of the addend minus the address,
1155 plus 1 if the contents of the low 16 bits, treated as a signed number,
1156 is negative. */
1157 HOWTO (R_PPC_EMB_NADDR16_HA, /* type */
1158 16, /* rightshift */
1159 1, /* size (0 = byte, 1 = short, 2 = long) */
1160 16, /* bitsize */
1161 FALSE, /* pc_relative */
1162 0, /* bitpos */
1163 complain_overflow_dont, /* complain_on_overflow */
1164 ppc_elf_addr16_ha_reloc, /* special_function */
1165 "R_PPC_EMB_NADDR16_HA", /* name */
1166 FALSE, /* partial_inplace */
1167 0, /* src_mask */
1168 0xffff, /* dst_mask */
1169 FALSE), /* pcrel_offset */
1171 /* 16 bit value resulting from allocating a 4 byte word to hold an
1172 address in the .sdata section, and returning the offset from
1173 _SDA_BASE_ for that relocation. */
1174 HOWTO (R_PPC_EMB_SDAI16, /* type */
1175 0, /* rightshift */
1176 1, /* size (0 = byte, 1 = short, 2 = long) */
1177 16, /* bitsize */
1178 FALSE, /* pc_relative */
1179 0, /* bitpos */
1180 complain_overflow_bitfield, /* complain_on_overflow */
1181 bfd_elf_generic_reloc, /* special_function */
1182 "R_PPC_EMB_SDAI16", /* name */
1183 FALSE, /* partial_inplace */
1184 0, /* src_mask */
1185 0xffff, /* dst_mask */
1186 FALSE), /* pcrel_offset */
1188 /* 16 bit value resulting from allocating a 4 byte word to hold an
1189 address in the .sdata2 section, and returning the offset from
1190 _SDA2_BASE_ for that relocation. */
1191 HOWTO (R_PPC_EMB_SDA2I16, /* type */
1192 0, /* rightshift */
1193 1, /* size (0 = byte, 1 = short, 2 = long) */
1194 16, /* bitsize */
1195 FALSE, /* pc_relative */
1196 0, /* bitpos */
1197 complain_overflow_bitfield, /* complain_on_overflow */
1198 bfd_elf_generic_reloc, /* special_function */
1199 "R_PPC_EMB_SDA2I16", /* name */
1200 FALSE, /* partial_inplace */
1201 0, /* src_mask */
1202 0xffff, /* dst_mask */
1203 FALSE), /* pcrel_offset */
1205 /* A sign-extended 16 bit value relative to _SDA2_BASE_, for use with
1206 small data items. */
1207 HOWTO (R_PPC_EMB_SDA2REL, /* type */
1208 0, /* rightshift */
1209 1, /* size (0 = byte, 1 = short, 2 = long) */
1210 16, /* bitsize */
1211 FALSE, /* pc_relative */
1212 0, /* bitpos */
1213 complain_overflow_signed, /* complain_on_overflow */
1214 bfd_elf_generic_reloc, /* special_function */
1215 "R_PPC_EMB_SDA2REL", /* name */
1216 FALSE, /* partial_inplace */
1217 0, /* src_mask */
1218 0xffff, /* dst_mask */
1219 FALSE), /* pcrel_offset */
1221 /* Relocate against either _SDA_BASE_ or _SDA2_BASE_, filling in the 16 bit
1222 signed offset from the appropriate base, and filling in the register
1223 field with the appropriate register (0, 2, or 13). */
1224 HOWTO (R_PPC_EMB_SDA21, /* type */
1225 0, /* rightshift */
1226 2, /* size (0 = byte, 1 = short, 2 = long) */
1227 16, /* bitsize */
1228 FALSE, /* pc_relative */
1229 0, /* bitpos */
1230 complain_overflow_signed, /* complain_on_overflow */
1231 bfd_elf_generic_reloc, /* special_function */
1232 "R_PPC_EMB_SDA21", /* name */
1233 FALSE, /* partial_inplace */
1234 0, /* src_mask */
1235 0xffff, /* dst_mask */
1236 FALSE), /* pcrel_offset */
1238 /* Relocation not handled: R_PPC_EMB_MRKREF */
1239 /* Relocation not handled: R_PPC_EMB_RELSEC16 */
1240 /* Relocation not handled: R_PPC_EMB_RELST_LO */
1241 /* Relocation not handled: R_PPC_EMB_RELST_HI */
1242 /* Relocation not handled: R_PPC_EMB_RELST_HA */
1243 /* Relocation not handled: R_PPC_EMB_BIT_FLD */
1245 /* PC relative relocation against either _SDA_BASE_ or _SDA2_BASE_, filling
1246 in the 16 bit signed offset from the appropriate base, and filling in the
1247 register field with the appropriate register (0, 2, or 13). */
1248 HOWTO (R_PPC_EMB_RELSDA, /* type */
1249 0, /* rightshift */
1250 1, /* size (0 = byte, 1 = short, 2 = long) */
1251 16, /* bitsize */
1252 TRUE, /* pc_relative */
1253 0, /* bitpos */
1254 complain_overflow_signed, /* complain_on_overflow */
1255 bfd_elf_generic_reloc, /* special_function */
1256 "R_PPC_EMB_RELSDA", /* name */
1257 FALSE, /* partial_inplace */
1258 0, /* src_mask */
1259 0xffff, /* dst_mask */
1260 FALSE), /* pcrel_offset */
1262 /* GNU extension to record C++ vtable hierarchy. */
1263 HOWTO (R_PPC_GNU_VTINHERIT, /* type */
1264 0, /* rightshift */
1265 0, /* size (0 = byte, 1 = short, 2 = long) */
1266 0, /* bitsize */
1267 FALSE, /* pc_relative */
1268 0, /* bitpos */
1269 complain_overflow_dont, /* complain_on_overflow */
1270 NULL, /* special_function */
1271 "R_PPC_GNU_VTINHERIT", /* name */
1272 FALSE, /* partial_inplace */
1273 0, /* src_mask */
1274 0, /* dst_mask */
1275 FALSE), /* pcrel_offset */
1277 /* GNU extension to record C++ vtable member usage. */
1278 HOWTO (R_PPC_GNU_VTENTRY, /* type */
1279 0, /* rightshift */
1280 0, /* size (0 = byte, 1 = short, 2 = long) */
1281 0, /* bitsize */
1282 FALSE, /* pc_relative */
1283 0, /* bitpos */
1284 complain_overflow_dont, /* complain_on_overflow */
1285 NULL, /* special_function */
1286 "R_PPC_GNU_VTENTRY", /* name */
1287 FALSE, /* partial_inplace */
1288 0, /* src_mask */
1289 0, /* dst_mask */
1290 FALSE), /* pcrel_offset */
1292 /* Phony reloc to handle AIX style TOC entries. */
1293 HOWTO (R_PPC_TOC16, /* type */
1294 0, /* rightshift */
1295 1, /* size (0 = byte, 1 = short, 2 = long) */
1296 16, /* bitsize */
1297 FALSE, /* pc_relative */
1298 0, /* bitpos */
1299 complain_overflow_signed, /* complain_on_overflow */
1300 bfd_elf_generic_reloc, /* special_function */
1301 "R_PPC_TOC16", /* name */
1302 FALSE, /* partial_inplace */
1303 0, /* src_mask */
1304 0xffff, /* dst_mask */
1305 FALSE), /* pcrel_offset */
1308 /* Initialize the ppc_elf_howto_table, so that linear accesses can be done. */
1310 static void
1311 ppc_elf_howto_init (void)
1313 unsigned int i, type;
1315 for (i = 0;
1316 i < sizeof (ppc_elf_howto_raw) / sizeof (ppc_elf_howto_raw[0]);
1317 i++)
1319 type = ppc_elf_howto_raw[i].type;
1320 if (type >= (sizeof (ppc_elf_howto_table)
1321 / sizeof (ppc_elf_howto_table[0])))
1322 abort ();
1323 ppc_elf_howto_table[type] = &ppc_elf_howto_raw[i];
1327 static reloc_howto_type *
1328 ppc_elf_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
1329 bfd_reloc_code_real_type code)
1331 enum elf_ppc_reloc_type r;
1333 /* Initialize howto table if not already done. */
1334 if (!ppc_elf_howto_table[R_PPC_ADDR32])
1335 ppc_elf_howto_init ();
1337 switch (code)
1339 default:
1340 return NULL;
1342 case BFD_RELOC_NONE: r = R_PPC_NONE; break;
1343 case BFD_RELOC_32: r = R_PPC_ADDR32; break;
1344 case BFD_RELOC_PPC_BA26: r = R_PPC_ADDR24; break;
1345 case BFD_RELOC_16: r = R_PPC_ADDR16; break;
1346 case BFD_RELOC_LO16: r = R_PPC_ADDR16_LO; break;
1347 case BFD_RELOC_HI16: r = R_PPC_ADDR16_HI; break;
1348 case BFD_RELOC_HI16_S: r = R_PPC_ADDR16_HA; break;
1349 case BFD_RELOC_PPC_BA16: r = R_PPC_ADDR14; break;
1350 case BFD_RELOC_PPC_BA16_BRTAKEN: r = R_PPC_ADDR14_BRTAKEN; break;
1351 case BFD_RELOC_PPC_BA16_BRNTAKEN: r = R_PPC_ADDR14_BRNTAKEN; break;
1352 case BFD_RELOC_PPC_B26: r = R_PPC_REL24; break;
1353 case BFD_RELOC_PPC_B16: r = R_PPC_REL14; break;
1354 case BFD_RELOC_PPC_B16_BRTAKEN: r = R_PPC_REL14_BRTAKEN; break;
1355 case BFD_RELOC_PPC_B16_BRNTAKEN: r = R_PPC_REL14_BRNTAKEN; break;
1356 case BFD_RELOC_16_GOTOFF: r = R_PPC_GOT16; break;
1357 case BFD_RELOC_LO16_GOTOFF: r = R_PPC_GOT16_LO; break;
1358 case BFD_RELOC_HI16_GOTOFF: r = R_PPC_GOT16_HI; break;
1359 case BFD_RELOC_HI16_S_GOTOFF: r = R_PPC_GOT16_HA; break;
1360 case BFD_RELOC_24_PLT_PCREL: r = R_PPC_PLTREL24; break;
1361 case BFD_RELOC_PPC_COPY: r = R_PPC_COPY; break;
1362 case BFD_RELOC_PPC_GLOB_DAT: r = R_PPC_GLOB_DAT; break;
1363 case BFD_RELOC_PPC_LOCAL24PC: r = R_PPC_LOCAL24PC; break;
1364 case BFD_RELOC_32_PCREL: r = R_PPC_REL32; break;
1365 case BFD_RELOC_32_PLTOFF: r = R_PPC_PLT32; break;
1366 case BFD_RELOC_32_PLT_PCREL: r = R_PPC_PLTREL32; break;
1367 case BFD_RELOC_LO16_PLTOFF: r = R_PPC_PLT16_LO; break;
1368 case BFD_RELOC_HI16_PLTOFF: r = R_PPC_PLT16_HI; break;
1369 case BFD_RELOC_HI16_S_PLTOFF: r = R_PPC_PLT16_HA; break;
1370 case BFD_RELOC_GPREL16: r = R_PPC_SDAREL16; break;
1371 case BFD_RELOC_16_BASEREL: r = R_PPC_SECTOFF; break;
1372 case BFD_RELOC_LO16_BASEREL: r = R_PPC_SECTOFF_LO; break;
1373 case BFD_RELOC_HI16_BASEREL: r = R_PPC_SECTOFF_HI; break;
1374 case BFD_RELOC_HI16_S_BASEREL: r = R_PPC_SECTOFF_HA; break;
1375 case BFD_RELOC_CTOR: r = R_PPC_ADDR32; break;
1376 case BFD_RELOC_PPC_TOC16: r = R_PPC_TOC16; break;
1377 case BFD_RELOC_PPC_TLS: r = R_PPC_TLS; break;
1378 case BFD_RELOC_PPC_DTPMOD: r = R_PPC_DTPMOD32; break;
1379 case BFD_RELOC_PPC_TPREL16: r = R_PPC_TPREL16; break;
1380 case BFD_RELOC_PPC_TPREL16_LO: r = R_PPC_TPREL16_LO; break;
1381 case BFD_RELOC_PPC_TPREL16_HI: r = R_PPC_TPREL16_HI; break;
1382 case BFD_RELOC_PPC_TPREL16_HA: r = R_PPC_TPREL16_HA; break;
1383 case BFD_RELOC_PPC_TPREL: r = R_PPC_TPREL32; break;
1384 case BFD_RELOC_PPC_DTPREL16: r = R_PPC_DTPREL16; break;
1385 case BFD_RELOC_PPC_DTPREL16_LO: r = R_PPC_DTPREL16_LO; break;
1386 case BFD_RELOC_PPC_DTPREL16_HI: r = R_PPC_DTPREL16_HI; break;
1387 case BFD_RELOC_PPC_DTPREL16_HA: r = R_PPC_DTPREL16_HA; break;
1388 case BFD_RELOC_PPC_DTPREL: r = R_PPC_DTPREL32; break;
1389 case BFD_RELOC_PPC_GOT_TLSGD16: r = R_PPC_GOT_TLSGD16; break;
1390 case BFD_RELOC_PPC_GOT_TLSGD16_LO: r = R_PPC_GOT_TLSGD16_LO; break;
1391 case BFD_RELOC_PPC_GOT_TLSGD16_HI: r = R_PPC_GOT_TLSGD16_HI; break;
1392 case BFD_RELOC_PPC_GOT_TLSGD16_HA: r = R_PPC_GOT_TLSGD16_HA; break;
1393 case BFD_RELOC_PPC_GOT_TLSLD16: r = R_PPC_GOT_TLSLD16; break;
1394 case BFD_RELOC_PPC_GOT_TLSLD16_LO: r = R_PPC_GOT_TLSLD16_LO; break;
1395 case BFD_RELOC_PPC_GOT_TLSLD16_HI: r = R_PPC_GOT_TLSLD16_HI; break;
1396 case BFD_RELOC_PPC_GOT_TLSLD16_HA: r = R_PPC_GOT_TLSLD16_HA; break;
1397 case BFD_RELOC_PPC_GOT_TPREL16: r = R_PPC_GOT_TPREL16; break;
1398 case BFD_RELOC_PPC_GOT_TPREL16_LO: r = R_PPC_GOT_TPREL16_LO; break;
1399 case BFD_RELOC_PPC_GOT_TPREL16_HI: r = R_PPC_GOT_TPREL16_HI; break;
1400 case BFD_RELOC_PPC_GOT_TPREL16_HA: r = R_PPC_GOT_TPREL16_HA; break;
1401 case BFD_RELOC_PPC_GOT_DTPREL16: r = R_PPC_GOT_DTPREL16; break;
1402 case BFD_RELOC_PPC_GOT_DTPREL16_LO: r = R_PPC_GOT_DTPREL16_LO; break;
1403 case BFD_RELOC_PPC_GOT_DTPREL16_HI: r = R_PPC_GOT_DTPREL16_HI; break;
1404 case BFD_RELOC_PPC_GOT_DTPREL16_HA: r = R_PPC_GOT_DTPREL16_HA; break;
1405 case BFD_RELOC_PPC_EMB_NADDR32: r = R_PPC_EMB_NADDR32; break;
1406 case BFD_RELOC_PPC_EMB_NADDR16: r = R_PPC_EMB_NADDR16; break;
1407 case BFD_RELOC_PPC_EMB_NADDR16_LO: r = R_PPC_EMB_NADDR16_LO; break;
1408 case BFD_RELOC_PPC_EMB_NADDR16_HI: r = R_PPC_EMB_NADDR16_HI; break;
1409 case BFD_RELOC_PPC_EMB_NADDR16_HA: r = R_PPC_EMB_NADDR16_HA; break;
1410 case BFD_RELOC_PPC_EMB_SDAI16: r = R_PPC_EMB_SDAI16; break;
1411 case BFD_RELOC_PPC_EMB_SDA2I16: r = R_PPC_EMB_SDA2I16; break;
1412 case BFD_RELOC_PPC_EMB_SDA2REL: r = R_PPC_EMB_SDA2REL; break;
1413 case BFD_RELOC_PPC_EMB_SDA21: r = R_PPC_EMB_SDA21; break;
1414 case BFD_RELOC_PPC_EMB_MRKREF: r = R_PPC_EMB_MRKREF; break;
1415 case BFD_RELOC_PPC_EMB_RELSEC16: r = R_PPC_EMB_RELSEC16; break;
1416 case BFD_RELOC_PPC_EMB_RELST_LO: r = R_PPC_EMB_RELST_LO; break;
1417 case BFD_RELOC_PPC_EMB_RELST_HI: r = R_PPC_EMB_RELST_HI; break;
1418 case BFD_RELOC_PPC_EMB_RELST_HA: r = R_PPC_EMB_RELST_HA; break;
1419 case BFD_RELOC_PPC_EMB_BIT_FLD: r = R_PPC_EMB_BIT_FLD; break;
1420 case BFD_RELOC_PPC_EMB_RELSDA: r = R_PPC_EMB_RELSDA; break;
1421 case BFD_RELOC_VTABLE_INHERIT: r = R_PPC_GNU_VTINHERIT; break;
1422 case BFD_RELOC_VTABLE_ENTRY: r = R_PPC_GNU_VTENTRY; break;
1425 return ppc_elf_howto_table[r];
1428 /* Set the howto pointer for a PowerPC ELF reloc. */
1430 static void
1431 ppc_elf_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED,
1432 arelent *cache_ptr,
1433 Elf_Internal_Rela *dst)
1435 /* Initialize howto table if not already done. */
1436 if (!ppc_elf_howto_table[R_PPC_ADDR32])
1437 ppc_elf_howto_init ();
1439 BFD_ASSERT (ELF32_R_TYPE (dst->r_info) < (unsigned int) R_PPC_max);
1440 cache_ptr->howto = ppc_elf_howto_table[ELF32_R_TYPE (dst->r_info)];
1443 /* Handle the R_PPC_ADDR16_HA reloc. */
1445 static bfd_reloc_status_type
1446 ppc_elf_addr16_ha_reloc (bfd *abfd ATTRIBUTE_UNUSED,
1447 arelent *reloc_entry,
1448 asymbol *symbol,
1449 void *data ATTRIBUTE_UNUSED,
1450 asection *input_section,
1451 bfd *output_bfd,
1452 char **error_message ATTRIBUTE_UNUSED)
1454 bfd_vma relocation;
1456 if (output_bfd != NULL)
1458 reloc_entry->address += input_section->output_offset;
1459 return bfd_reloc_ok;
1462 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
1463 return bfd_reloc_outofrange;
1465 if (bfd_is_com_section (symbol->section))
1466 relocation = 0;
1467 else
1468 relocation = symbol->value;
1470 relocation += symbol->section->output_section->vma;
1471 relocation += symbol->section->output_offset;
1472 relocation += reloc_entry->addend;
1474 reloc_entry->addend += (relocation & 0x8000) << 1;
1476 return bfd_reloc_continue;
1479 static bfd_reloc_status_type
1480 ppc_elf_unhandled_reloc (bfd *abfd,
1481 arelent *reloc_entry,
1482 asymbol *symbol,
1483 void *data,
1484 asection *input_section,
1485 bfd *output_bfd,
1486 char **error_message)
1488 /* If this is a relocatable link (output_bfd test tells us), just
1489 call the generic function. Any adjustment will be done at final
1490 link time. */
1491 if (output_bfd != NULL)
1492 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1493 input_section, output_bfd, error_message);
1495 if (error_message != NULL)
1497 static char buf[60];
1498 sprintf (buf, _("generic linker can't handle %s"),
1499 reloc_entry->howto->name);
1500 *error_message = buf;
1502 return bfd_reloc_dangerous;
1505 /* Sections created by the linker. */
1507 typedef struct elf_linker_section
1509 /* pointer to the section */
1510 asection *section;
1511 /* pointer to the relocations needed for this section */
1512 asection *rel_section;
1513 /* pointer to the created symbol hash value */
1514 struct elf_link_hash_entry *sym_hash;
1515 /* offset of symbol from beginning of section */
1516 bfd_vma sym_offset;
1517 } elf_linker_section_t;
1519 /* Linked list of allocated pointer entries. This hangs off of the
1520 symbol lists, and provides allows us to return different pointers,
1521 based on different addend's. */
1523 typedef struct elf_linker_section_pointers
1525 /* next allocated pointer for this symbol */
1526 struct elf_linker_section_pointers *next;
1527 /* offset of pointer from beginning of section */
1528 bfd_vma offset;
1529 /* addend used */
1530 bfd_vma addend;
1531 /* which linker section this is */
1532 elf_linker_section_t *lsect;
1533 /* whether address was written yet */
1534 bfd_boolean written_address_p;
1535 } elf_linker_section_pointers_t;
1537 struct ppc_elf_obj_tdata
1539 struct elf_obj_tdata elf;
1541 /* A mapping from local symbols to offsets into the various linker
1542 sections added. This is index by the symbol index. */
1543 elf_linker_section_pointers_t **linker_section_pointers;
1546 #define ppc_elf_tdata(bfd) \
1547 ((struct ppc_elf_obj_tdata *) (bfd)->tdata.any)
1549 #define elf_local_ptr_offsets(bfd) \
1550 (ppc_elf_tdata (bfd)->linker_section_pointers)
1552 /* Override the generic function because we store some extras. */
1554 static bfd_boolean
1555 ppc_elf_mkobject (bfd *abfd)
1557 bfd_size_type amt = sizeof (struct ppc_elf_obj_tdata);
1558 abfd->tdata.any = bfd_zalloc (abfd, amt);
1559 if (abfd->tdata.any == NULL)
1560 return FALSE;
1561 return TRUE;
1564 /* Fix bad default arch selected for a 32 bit input bfd when the
1565 default is 64 bit. */
1567 static bfd_boolean
1568 ppc_elf_object_p (bfd *abfd)
1570 if (abfd->arch_info->the_default && abfd->arch_info->bits_per_word == 64)
1572 Elf_Internal_Ehdr *i_ehdr = elf_elfheader (abfd);
1574 if (i_ehdr->e_ident[EI_CLASS] == ELFCLASS32)
1576 /* Relies on arch after 64 bit default being 32 bit default. */
1577 abfd->arch_info = abfd->arch_info->next;
1578 BFD_ASSERT (abfd->arch_info->bits_per_word == 32);
1581 return TRUE;
1584 /* Function to set whether a module needs the -mrelocatable bit set. */
1586 static bfd_boolean
1587 ppc_elf_set_private_flags (bfd *abfd, flagword flags)
1589 BFD_ASSERT (!elf_flags_init (abfd)
1590 || elf_elfheader (abfd)->e_flags == flags);
1592 elf_elfheader (abfd)->e_flags = flags;
1593 elf_flags_init (abfd) = TRUE;
1594 return TRUE;
1597 /* Merge backend specific data from an object file to the output
1598 object file when linking. */
1600 static bfd_boolean
1601 ppc_elf_merge_private_bfd_data (bfd *ibfd, bfd *obfd)
1603 flagword old_flags;
1604 flagword new_flags;
1605 bfd_boolean error;
1607 /* Check if we have the same endianess. */
1608 if (! _bfd_generic_verify_endian_match (ibfd, obfd))
1609 return FALSE;
1611 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
1612 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
1613 return TRUE;
1615 new_flags = elf_elfheader (ibfd)->e_flags;
1616 old_flags = elf_elfheader (obfd)->e_flags;
1617 if (!elf_flags_init (obfd))
1619 /* First call, no flags set. */
1620 elf_flags_init (obfd) = TRUE;
1621 elf_elfheader (obfd)->e_flags = new_flags;
1624 /* Compatible flags are ok. */
1625 else if (new_flags == old_flags)
1628 /* Incompatible flags. */
1629 else
1631 /* Warn about -mrelocatable mismatch. Allow -mrelocatable-lib
1632 to be linked with either. */
1633 error = FALSE;
1634 if ((new_flags & EF_PPC_RELOCATABLE) != 0
1635 && (old_flags & (EF_PPC_RELOCATABLE | EF_PPC_RELOCATABLE_LIB)) == 0)
1637 error = TRUE;
1638 (*_bfd_error_handler)
1639 (_("%B: compiled with -mrelocatable and linked with "
1640 "modules compiled normally"), ibfd);
1642 else if ((new_flags & (EF_PPC_RELOCATABLE | EF_PPC_RELOCATABLE_LIB)) == 0
1643 && (old_flags & EF_PPC_RELOCATABLE) != 0)
1645 error = TRUE;
1646 (*_bfd_error_handler)
1647 (_("%B: compiled normally and linked with "
1648 "modules compiled with -mrelocatable"), ibfd);
1651 /* The output is -mrelocatable-lib iff both the input files are. */
1652 if (! (new_flags & EF_PPC_RELOCATABLE_LIB))
1653 elf_elfheader (obfd)->e_flags &= ~EF_PPC_RELOCATABLE_LIB;
1655 /* The output is -mrelocatable iff it can't be -mrelocatable-lib,
1656 but each input file is either -mrelocatable or -mrelocatable-lib. */
1657 if (! (elf_elfheader (obfd)->e_flags & EF_PPC_RELOCATABLE_LIB)
1658 && (new_flags & (EF_PPC_RELOCATABLE_LIB | EF_PPC_RELOCATABLE))
1659 && (old_flags & (EF_PPC_RELOCATABLE_LIB | EF_PPC_RELOCATABLE)))
1660 elf_elfheader (obfd)->e_flags |= EF_PPC_RELOCATABLE;
1662 /* Do not warn about eabi vs. V.4 mismatch, just or in the bit if
1663 any module uses it. */
1664 elf_elfheader (obfd)->e_flags |= (new_flags & EF_PPC_EMB);
1666 new_flags &= ~(EF_PPC_RELOCATABLE | EF_PPC_RELOCATABLE_LIB | EF_PPC_EMB);
1667 old_flags &= ~(EF_PPC_RELOCATABLE | EF_PPC_RELOCATABLE_LIB | EF_PPC_EMB);
1669 /* Warn about any other mismatches. */
1670 if (new_flags != old_flags)
1672 error = TRUE;
1673 (*_bfd_error_handler)
1674 (_("%B: uses different e_flags (0x%lx) fields "
1675 "than previous modules (0x%lx)"),
1676 ibfd, (long) new_flags, (long) old_flags);
1679 if (error)
1681 bfd_set_error (bfd_error_bad_value);
1682 return FALSE;
1686 return TRUE;
1689 /* Support for core dump NOTE sections. */
1691 static bfd_boolean
1692 ppc_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
1694 int offset;
1695 unsigned int size;
1697 switch (note->descsz)
1699 default:
1700 return FALSE;
1702 case 268: /* Linux/PPC. */
1703 /* pr_cursig */
1704 elf_tdata (abfd)->core_signal = bfd_get_16 (abfd, note->descdata + 12);
1706 /* pr_pid */
1707 elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, note->descdata + 24);
1709 /* pr_reg */
1710 offset = 72;
1711 size = 192;
1713 break;
1716 /* Make a ".reg/999" section. */
1717 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
1718 size, note->descpos + offset);
1721 static bfd_boolean
1722 ppc_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
1724 switch (note->descsz)
1726 default:
1727 return FALSE;
1729 case 128: /* Linux/PPC elf_prpsinfo. */
1730 elf_tdata (abfd)->core_program
1731 = _bfd_elfcore_strndup (abfd, note->descdata + 32, 16);
1732 elf_tdata (abfd)->core_command
1733 = _bfd_elfcore_strndup (abfd, note->descdata + 48, 80);
1736 /* Note that for some reason, a spurious space is tacked
1737 onto the end of the args in some (at least one anyway)
1738 implementations, so strip it off if it exists. */
1741 char *command = elf_tdata (abfd)->core_command;
1742 int n = strlen (command);
1744 if (0 < n && command[n - 1] == ' ')
1745 command[n - 1] = '\0';
1748 return TRUE;
1751 /* Return address for Ith PLT stub in section PLT, for relocation REL
1752 or (bfd_vma) -1 if it should not be included. */
1754 static bfd_vma
1755 ppc_elf_plt_sym_val (bfd_vma i ATTRIBUTE_UNUSED,
1756 const asection *plt ATTRIBUTE_UNUSED,
1757 const arelent *rel)
1759 return rel->address;
1762 /* Handle a PowerPC specific section when reading an object file. This
1763 is called when elfcode.h finds a section with an unknown type. */
1765 static bfd_boolean
1766 ppc_elf_section_from_shdr (bfd *abfd, Elf_Internal_Shdr *hdr, const char *name)
1768 asection *newsect;
1769 flagword flags;
1771 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name))
1772 return FALSE;
1774 newsect = hdr->bfd_section;
1775 flags = bfd_get_section_flags (abfd, newsect);
1776 if (hdr->sh_flags & SHF_EXCLUDE)
1777 flags |= SEC_EXCLUDE;
1779 if (hdr->sh_type == SHT_ORDERED)
1780 flags |= SEC_SORT_ENTRIES;
1782 bfd_set_section_flags (abfd, newsect, flags);
1783 return TRUE;
1786 /* Set up any other section flags and such that may be necessary. */
1788 static bfd_boolean
1789 ppc_elf_fake_sections (bfd *abfd ATTRIBUTE_UNUSED,
1790 Elf_Internal_Shdr *shdr,
1791 asection *asect)
1793 if ((asect->flags & SEC_EXCLUDE) != 0)
1794 shdr->sh_flags |= SHF_EXCLUDE;
1796 if ((asect->flags & SEC_SORT_ENTRIES) != 0)
1797 shdr->sh_type = SHT_ORDERED;
1799 return TRUE;
1802 /* If we have .sbss2 or .PPC.EMB.sbss0 output sections, we
1803 need to bump up the number of section headers. */
1805 static int
1806 ppc_elf_additional_program_headers (bfd *abfd)
1808 asection *s;
1809 int ret = 0;
1811 s = bfd_get_section_by_name (abfd, ".sbss2");
1812 if (s != NULL && (s->flags & SEC_ALLOC) != 0)
1813 ++ret;
1815 s = bfd_get_section_by_name (abfd, ".PPC.EMB.sbss0");
1816 if (s != NULL && (s->flags & SEC_ALLOC) != 0)
1817 ++ret;
1819 return ret;
1822 /* Add extra PPC sections -- Note, for now, make .sbss2 and
1823 .PPC.EMB.sbss0 a normal section, and not a bss section so
1824 that the linker doesn't crater when trying to make more than
1825 2 sections. */
1827 static struct bfd_elf_special_section const ppc_elf_special_sections[]=
1829 { ".tags", 5, 0, SHT_ORDERED, SHF_ALLOC },
1830 { ".sdata", 6, -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
1831 { ".sbss", 5, -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
1832 { ".sdata2", 7, -2, SHT_PROGBITS, SHF_ALLOC },
1833 { ".sbss2", 6, -2, SHT_PROGBITS, SHF_ALLOC },
1834 { ".PPC.EMB.apuinfo", 16, 0, SHT_NOTE, 0 },
1835 { ".PPC.EMB.sdata0", 15, 0, SHT_PROGBITS, SHF_ALLOC },
1836 { ".PPC.EMB.sbss0", 14, 0, SHT_PROGBITS, SHF_ALLOC },
1837 { ".plt", 4, 0, SHT_NOBITS, SHF_ALLOC + SHF_EXECINSTR },
1838 { NULL, 0, 0, 0, 0 }
1841 /* Very simple linked list structure for recording apuinfo values. */
1842 typedef struct apuinfo_list
1844 struct apuinfo_list *next;
1845 unsigned long value;
1847 apuinfo_list;
1849 static apuinfo_list *head;
1852 static void
1853 apuinfo_list_init (void)
1855 head = NULL;
1858 static void
1859 apuinfo_list_add (unsigned long value)
1861 apuinfo_list *entry = head;
1863 while (entry != NULL)
1865 if (entry->value == value)
1866 return;
1867 entry = entry->next;
1870 entry = bfd_malloc (sizeof (* entry));
1871 if (entry == NULL)
1872 return;
1874 entry->value = value;
1875 entry->next = head;
1876 head = entry;
1879 static unsigned
1880 apuinfo_list_length (void)
1882 apuinfo_list *entry;
1883 unsigned long count;
1885 for (entry = head, count = 0;
1886 entry;
1887 entry = entry->next)
1888 ++ count;
1890 return count;
1893 static inline unsigned long
1894 apuinfo_list_element (unsigned long number)
1896 apuinfo_list * entry;
1898 for (entry = head;
1899 entry && number --;
1900 entry = entry->next)
1903 return entry ? entry->value : 0;
1906 static void
1907 apuinfo_list_finish (void)
1909 apuinfo_list *entry;
1911 for (entry = head; entry;)
1913 apuinfo_list *next = entry->next;
1914 free (entry);
1915 entry = next;
1918 head = NULL;
1921 #define APUINFO_SECTION_NAME ".PPC.EMB.apuinfo"
1922 #define APUINFO_LABEL "APUinfo"
1924 /* Scan the input BFDs and create a linked list of
1925 the APUinfo values that will need to be emitted. */
1927 static void
1928 ppc_elf_begin_write_processing (bfd *abfd, struct bfd_link_info *link_info)
1930 bfd *ibfd;
1931 asection *asec;
1932 char *buffer;
1933 unsigned num_input_sections;
1934 bfd_size_type output_section_size;
1935 unsigned i;
1936 unsigned num_entries;
1937 unsigned long offset;
1938 unsigned long length;
1939 const char *error_message = NULL;
1941 if (link_info == NULL)
1942 return;
1944 /* Scan the input bfds, looking for apuinfo sections. */
1945 num_input_sections = 0;
1946 output_section_size = 0;
1948 for (ibfd = link_info->input_bfds; ibfd; ibfd = ibfd->link_next)
1950 asec = bfd_get_section_by_name (ibfd, APUINFO_SECTION_NAME);
1951 if (asec)
1953 ++ num_input_sections;
1954 output_section_size += asec->size;
1958 /* We need at least one input sections
1959 in order to make merging worthwhile. */
1960 if (num_input_sections < 1)
1961 return;
1963 /* Just make sure that the output section exists as well. */
1964 asec = bfd_get_section_by_name (abfd, APUINFO_SECTION_NAME);
1965 if (asec == NULL)
1966 return;
1968 /* Allocate a buffer for the contents of the input sections. */
1969 buffer = bfd_malloc (output_section_size);
1970 if (buffer == NULL)
1971 return;
1973 offset = 0;
1974 apuinfo_list_init ();
1976 /* Read in the input sections contents. */
1977 for (ibfd = link_info->input_bfds; ibfd; ibfd = ibfd->link_next)
1979 unsigned long datum;
1980 char *ptr;
1982 asec = bfd_get_section_by_name (ibfd, APUINFO_SECTION_NAME);
1983 if (asec == NULL)
1984 continue;
1986 length = asec->size;
1987 if (length < 24)
1989 error_message = _("corrupt or empty %s section in %B");
1990 goto fail;
1993 if (bfd_seek (ibfd, asec->filepos, SEEK_SET) != 0
1994 || (bfd_bread (buffer + offset, length, ibfd) != length))
1996 error_message = _("unable to read in %s section from %B");
1997 goto fail;
2000 /* Process the contents of the section. */
2001 ptr = buffer + offset;
2002 error_message = _("corrupt %s section in %B");
2004 /* Verify the contents of the header. Note - we have to
2005 extract the values this way in order to allow for a
2006 host whose endian-ness is different from the target. */
2007 datum = bfd_get_32 (ibfd, ptr);
2008 if (datum != sizeof APUINFO_LABEL)
2009 goto fail;
2011 datum = bfd_get_32 (ibfd, ptr + 8);
2012 if (datum != 0x2)
2013 goto fail;
2015 if (strcmp (ptr + 12, APUINFO_LABEL) != 0)
2016 goto fail;
2018 /* Get the number of bytes used for apuinfo entries. */
2019 datum = bfd_get_32 (ibfd, ptr + 4);
2020 if (datum + 20 != length)
2021 goto fail;
2023 /* Make sure that we do not run off the end of the section. */
2024 if (offset + length > output_section_size)
2025 goto fail;
2027 /* Scan the apuinfo section, building a list of apuinfo numbers. */
2028 for (i = 0; i < datum; i += 4)
2029 apuinfo_list_add (bfd_get_32 (ibfd, ptr + 20 + i));
2031 /* Update the offset. */
2032 offset += length;
2035 error_message = NULL;
2037 /* Compute the size of the output section. */
2038 num_entries = apuinfo_list_length ();
2039 output_section_size = 20 + num_entries * 4;
2041 asec = bfd_get_section_by_name (abfd, APUINFO_SECTION_NAME);
2043 if (! bfd_set_section_size (abfd, asec, output_section_size))
2044 ibfd = abfd,
2045 error_message = _("warning: unable to set size of %s section in %B");
2047 fail:
2048 free (buffer);
2050 if (error_message)
2051 (*_bfd_error_handler) (error_message, ibfd, APUINFO_SECTION_NAME);
2054 /* Prevent the output section from accumulating the input sections'
2055 contents. We have already stored this in our linked list structure. */
2057 static bfd_boolean
2058 ppc_elf_write_section (bfd *abfd ATTRIBUTE_UNUSED,
2059 asection *asec,
2060 bfd_byte *contents ATTRIBUTE_UNUSED)
2062 return (apuinfo_list_length ()
2063 && strcmp (asec->name, APUINFO_SECTION_NAME) == 0);
2066 /* Finally we can generate the output section. */
2068 static void
2069 ppc_elf_final_write_processing (bfd *abfd, bfd_boolean linker ATTRIBUTE_UNUSED)
2071 bfd_byte *buffer;
2072 asection *asec;
2073 unsigned i;
2074 unsigned num_entries;
2075 bfd_size_type length;
2077 asec = bfd_get_section_by_name (abfd, APUINFO_SECTION_NAME);
2078 if (asec == NULL)
2079 return;
2081 if (apuinfo_list_length () == 0)
2082 return;
2084 length = asec->size;
2085 if (length < 20)
2086 return;
2088 buffer = bfd_malloc (length);
2089 if (buffer == NULL)
2091 (*_bfd_error_handler)
2092 (_("failed to allocate space for new APUinfo section."));
2093 return;
2096 /* Create the apuinfo header. */
2097 num_entries = apuinfo_list_length ();
2098 bfd_put_32 (abfd, sizeof APUINFO_LABEL, buffer);
2099 bfd_put_32 (abfd, num_entries * 4, buffer + 4);
2100 bfd_put_32 (abfd, 0x2, buffer + 8);
2101 strcpy ((char *) buffer + 12, APUINFO_LABEL);
2103 length = 20;
2104 for (i = 0; i < num_entries; i++)
2106 bfd_put_32 (abfd, apuinfo_list_element (i), buffer + length);
2107 length += 4;
2110 if (length != asec->size)
2111 (*_bfd_error_handler) (_("failed to compute new APUinfo section."));
2113 if (! bfd_set_section_contents (abfd, asec, buffer, (file_ptr) 0, length))
2114 (*_bfd_error_handler) (_("failed to install new APUinfo section."));
2116 free (buffer);
2118 apuinfo_list_finish ();
2121 /* The following functions are specific to the ELF linker, while
2122 functions above are used generally. They appear in this file more
2123 or less in the order in which they are called. eg.
2124 ppc_elf_check_relocs is called early in the link process,
2125 ppc_elf_finish_dynamic_sections is one of the last functions
2126 called. */
2128 /* The PPC linker needs to keep track of the number of relocs that it
2129 decides to copy as dynamic relocs in check_relocs for each symbol.
2130 This is so that it can later discard them if they are found to be
2131 unnecessary. We store the information in a field extending the
2132 regular ELF linker hash table. */
2134 struct ppc_elf_dyn_relocs
2136 struct ppc_elf_dyn_relocs *next;
2138 /* The input section of the reloc. */
2139 asection *sec;
2141 /* Total number of relocs copied for the input section. */
2142 bfd_size_type count;
2144 /* Number of pc-relative relocs copied for the input section. */
2145 bfd_size_type pc_count;
2148 /* Of those relocs that might be copied as dynamic relocs, this macro
2149 selects those that must be copied when linking a shared library,
2150 even when the symbol is local. */
2152 #define MUST_BE_DYN_RELOC(RTYPE) \
2153 ((RTYPE) != R_PPC_REL24 \
2154 && (RTYPE) != R_PPC_REL14 \
2155 && (RTYPE) != R_PPC_REL14_BRTAKEN \
2156 && (RTYPE) != R_PPC_REL14_BRNTAKEN \
2157 && (RTYPE) != R_PPC_REL32)
2159 /* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
2160 copying dynamic variables from a shared lib into an app's dynbss
2161 section, and instead use a dynamic relocation to point into the
2162 shared lib. */
2163 #define ELIMINATE_COPY_RELOCS 1
2165 /* PPC ELF linker hash entry. */
2167 struct ppc_elf_link_hash_entry
2169 struct elf_link_hash_entry elf;
2171 /* If this symbol is used in the linker created sections, the processor
2172 specific backend uses this field to map the field into the offset
2173 from the beginning of the section. */
2174 elf_linker_section_pointers_t *linker_section_pointer;
2176 /* Track dynamic relocs copied for this symbol. */
2177 struct ppc_elf_dyn_relocs *dyn_relocs;
2179 /* Contexts in which symbol is used in the GOT (or TOC).
2180 TLS_GD .. TLS_TLS bits are or'd into the mask as the
2181 corresponding relocs are encountered during check_relocs.
2182 tls_optimize clears TLS_GD .. TLS_TPREL when optimizing to
2183 indicate the corresponding GOT entry type is not needed. */
2184 #define TLS_GD 1 /* GD reloc. */
2185 #define TLS_LD 2 /* LD reloc. */
2186 #define TLS_TPREL 4 /* TPREL reloc, => IE. */
2187 #define TLS_DTPREL 8 /* DTPREL reloc, => LD. */
2188 #define TLS_TLS 16 /* Any TLS reloc. */
2189 #define TLS_TPRELGD 32 /* TPREL reloc resulting from GD->IE. */
2190 char tls_mask;
2193 #define ppc_elf_hash_entry(ent) ((struct ppc_elf_link_hash_entry *) (ent))
2195 /* PPC ELF linker hash table. */
2197 struct ppc_elf_link_hash_table
2199 struct elf_link_hash_table elf;
2201 /* Short-cuts to get to dynamic linker sections. */
2202 asection *got;
2203 asection *relgot;
2204 asection *plt;
2205 asection *relplt;
2206 asection *dynbss;
2207 asection *relbss;
2208 asection *dynsbss;
2209 asection *relsbss;
2210 elf_linker_section_t *sdata;
2211 elf_linker_section_t *sdata2;
2212 asection *sbss;
2214 /* Shortcut to .__tls_get_addr. */
2215 struct elf_link_hash_entry *tls_get_addr;
2217 /* TLS local dynamic got entry handling. */
2218 union {
2219 bfd_signed_vma refcount;
2220 bfd_vma offset;
2221 } tlsld_got;
2223 /* Small local sym to section mapping cache. */
2224 struct sym_sec_cache sym_sec;
2227 /* Get the PPC ELF linker hash table from a link_info structure. */
2229 #define ppc_elf_hash_table(p) \
2230 ((struct ppc_elf_link_hash_table *) (p)->hash)
2232 /* Create an entry in a PPC ELF linker hash table. */
2234 static struct bfd_hash_entry *
2235 ppc_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
2236 struct bfd_hash_table *table,
2237 const char *string)
2239 /* Allocate the structure if it has not already been allocated by a
2240 subclass. */
2241 if (entry == NULL)
2243 entry = bfd_hash_allocate (table,
2244 sizeof (struct ppc_elf_link_hash_entry));
2245 if (entry == NULL)
2246 return entry;
2249 /* Call the allocation method of the superclass. */
2250 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
2251 if (entry != NULL)
2253 ppc_elf_hash_entry (entry)->linker_section_pointer = NULL;
2254 ppc_elf_hash_entry (entry)->dyn_relocs = NULL;
2255 ppc_elf_hash_entry (entry)->tls_mask = 0;
2258 return entry;
2261 /* Create a PPC ELF linker hash table. */
2263 static struct bfd_link_hash_table *
2264 ppc_elf_link_hash_table_create (bfd *abfd)
2266 struct ppc_elf_link_hash_table *ret;
2268 ret = bfd_zmalloc (sizeof (struct ppc_elf_link_hash_table));
2269 if (ret == NULL)
2270 return NULL;
2272 if (! _bfd_elf_link_hash_table_init (&ret->elf, abfd,
2273 ppc_elf_link_hash_newfunc))
2275 free (ret);
2276 return NULL;
2279 return &ret->elf.root;
2282 /* The powerpc .got has a blrl instruction in it. Mark it executable. */
2284 static bfd_boolean
2285 ppc_elf_create_got (bfd *abfd, struct bfd_link_info *info)
2287 struct ppc_elf_link_hash_table *htab;
2288 asection *s;
2289 flagword flags;
2291 if (!_bfd_elf_create_got_section (abfd, info))
2292 return FALSE;
2294 htab = ppc_elf_hash_table (info);
2295 htab->got = s = bfd_get_section_by_name (abfd, ".got");
2296 if (s == NULL)
2297 abort ();
2299 flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_HAS_CONTENTS | SEC_IN_MEMORY
2300 | SEC_LINKER_CREATED);
2301 if (!bfd_set_section_flags (abfd, s, flags))
2302 return FALSE;
2304 htab->relgot = bfd_make_section (abfd, ".rela.got");
2305 if (!htab->relgot
2306 || ! bfd_set_section_flags (abfd, htab->relgot,
2307 (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
2308 | SEC_IN_MEMORY | SEC_LINKER_CREATED
2309 | SEC_READONLY))
2310 || ! bfd_set_section_alignment (abfd, htab->relgot, 2))
2311 return FALSE;
2313 return TRUE;
2316 /* We have to create .dynsbss and .rela.sbss here so that they get mapped
2317 to output sections (just like _bfd_elf_create_dynamic_sections has
2318 to create .dynbss and .rela.bss). */
2320 static bfd_boolean
2321 ppc_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
2323 struct ppc_elf_link_hash_table *htab;
2324 asection *s;
2325 flagword flags;
2327 htab = ppc_elf_hash_table (info);
2329 if (htab->got == NULL
2330 && !ppc_elf_create_got (abfd, info))
2331 return FALSE;
2333 if (!_bfd_elf_create_dynamic_sections (abfd, info))
2334 return FALSE;
2336 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
2337 | SEC_LINKER_CREATED);
2339 htab->dynbss = bfd_get_section_by_name (abfd, ".dynbss");
2340 htab->dynsbss = s = bfd_make_section (abfd, ".dynsbss");
2341 if (s == NULL
2342 || ! bfd_set_section_flags (abfd, s, SEC_ALLOC | SEC_LINKER_CREATED))
2343 return FALSE;
2345 if (! info->shared)
2347 htab->relbss = bfd_get_section_by_name (abfd, ".rela.bss");
2348 htab->relsbss = s = bfd_make_section (abfd, ".rela.sbss");
2349 if (s == NULL
2350 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
2351 || ! bfd_set_section_alignment (abfd, s, 2))
2352 return FALSE;
2355 htab->relplt = bfd_get_section_by_name (abfd, ".rela.plt");
2356 htab->plt = s = bfd_get_section_by_name (abfd, ".plt");
2357 if (s == NULL)
2358 abort ();
2360 flags = SEC_ALLOC | SEC_CODE | SEC_IN_MEMORY | SEC_LINKER_CREATED;
2361 return bfd_set_section_flags (abfd, s, flags);
2364 /* Copy the extra info we tack onto an elf_link_hash_entry. */
2366 static void
2367 ppc_elf_copy_indirect_symbol (const struct elf_backend_data *bed,
2368 struct elf_link_hash_entry *dir,
2369 struct elf_link_hash_entry *ind)
2371 struct ppc_elf_link_hash_entry *edir, *eind;
2373 edir = (struct ppc_elf_link_hash_entry *) dir;
2374 eind = (struct ppc_elf_link_hash_entry *) ind;
2376 if (eind->dyn_relocs != NULL)
2378 if (edir->dyn_relocs != NULL)
2380 struct ppc_elf_dyn_relocs **pp;
2381 struct ppc_elf_dyn_relocs *p;
2383 if (ind->root.type == bfd_link_hash_indirect)
2384 abort ();
2386 /* Add reloc counts against the weak sym to the strong sym
2387 list. Merge any entries against the same section. */
2388 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
2390 struct ppc_elf_dyn_relocs *q;
2392 for (q = edir->dyn_relocs; q != NULL; q = q->next)
2393 if (q->sec == p->sec)
2395 q->pc_count += p->pc_count;
2396 q->count += p->count;
2397 *pp = p->next;
2398 break;
2400 if (q == NULL)
2401 pp = &p->next;
2403 *pp = edir->dyn_relocs;
2406 edir->dyn_relocs = eind->dyn_relocs;
2407 eind->dyn_relocs = NULL;
2410 edir->tls_mask |= eind->tls_mask;
2412 if (ELIMINATE_COPY_RELOCS
2413 && ind->root.type != bfd_link_hash_indirect
2414 && dir->dynamic_adjusted)
2416 /* If called to transfer flags for a weakdef during processing
2417 of elf_adjust_dynamic_symbol, don't copy non_got_ref.
2418 We clear it ourselves for ELIMINATE_COPY_RELOCS. */
2419 dir->ref_dynamic |= ind->ref_dynamic;
2420 dir->ref_regular |= ind->ref_regular;
2421 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
2422 dir->needs_plt |= ind->needs_plt;
2424 else
2425 _bfd_elf_link_hash_copy_indirect (bed, dir, ind);
2428 /* Hook called by the linker routine which adds symbols from an object
2429 file. We use it to put .comm items in .sbss, and not .bss. */
2431 static bfd_boolean
2432 ppc_elf_add_symbol_hook (bfd *abfd,
2433 struct bfd_link_info *info,
2434 Elf_Internal_Sym *sym,
2435 const char **namep ATTRIBUTE_UNUSED,
2436 flagword *flagsp ATTRIBUTE_UNUSED,
2437 asection **secp,
2438 bfd_vma *valp)
2440 if (sym->st_shndx == SHN_COMMON
2441 && !info->relocatable
2442 && sym->st_size <= elf_gp_size (abfd)
2443 && (info->hash->creator == abfd->xvec
2444 || info->hash->creator == abfd->xvec->alternative_target))
2446 /* Common symbols less than or equal to -G nn bytes are automatically
2447 put into .sbss. */
2448 struct ppc_elf_link_hash_table *htab;
2450 htab = ppc_elf_hash_table (info);
2451 if (htab->sbss == NULL)
2453 flagword flags = SEC_IS_COMMON;
2455 htab->sbss = bfd_make_section_anyway (abfd, ".sbss");
2456 if (htab->sbss == NULL
2457 || ! bfd_set_section_flags (abfd, htab->sbss, flags))
2458 return FALSE;
2461 *secp = htab->sbss;
2462 *valp = sym->st_size;
2465 return TRUE;
2468 /* Enumeration to specify the special section. */
2469 enum elf_linker_section_enum
2471 LINKER_SECTION_SDATA,
2472 LINKER_SECTION_SDATA2
2475 /* Create a special linker section */
2476 static elf_linker_section_t *
2477 ppc_elf_create_linker_section (bfd *abfd,
2478 struct bfd_link_info *info,
2479 enum elf_linker_section_enum which)
2481 elf_linker_section_t *lsect;
2482 struct ppc_elf_link_hash_table *htab = ppc_elf_hash_table (info);
2483 asection *s, *sym_sec;
2484 bfd_size_type amt;
2485 flagword flags;
2486 const char *name;
2487 const char *rel_name;
2488 const char *sym_name;
2489 bfd_vma sym_offset;
2491 /* The linker creates these sections so it has somewhere to attach
2492 their respective symbols. Startup code (crt1.o) uses these symbols
2493 to initialize a register pointing to the section. If the output
2494 sections corresponding to these input sections were empty it would
2495 be OK to set the symbol to 0 (or any random number), because the
2496 associated register should never be used.
2497 FIXME: Setting a symbol this way is silly. The symbols ought to
2498 be set the same way other backends set gp. */
2499 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
2500 | SEC_LINKER_CREATED);
2501 sym_offset = 32768;
2503 switch (which)
2505 default:
2506 abort ();
2507 return NULL;
2509 case LINKER_SECTION_SDATA: /* .sdata/.sbss section */
2510 name = ".sdata";
2511 rel_name = ".rela.sdata";
2512 sym_name = "_SDA_BASE_";
2513 break;
2515 case LINKER_SECTION_SDATA2: /* .sdata2/.sbss2 section */
2516 name = ".sdata2";
2517 rel_name = ".rela.sdata2";
2518 sym_name = "_SDA2_BASE_";
2519 flags |= SEC_READONLY;
2520 break;
2523 /* Record the first bfd that needs the special sections. */
2524 if (!htab->elf.dynobj)
2525 htab->elf.dynobj = abfd;
2527 amt = sizeof (elf_linker_section_t);
2528 lsect = bfd_zalloc (htab->elf.dynobj, amt);
2530 lsect->sym_offset = sym_offset;
2532 /* See if the sections already exist. */
2533 sym_sec = s = bfd_get_section_by_name (htab->elf.dynobj, name);
2534 if (s == NULL || (s->flags & flags) != flags)
2536 s = bfd_make_section_anyway (htab->elf.dynobj, name);
2537 if (s == NULL
2538 || !bfd_set_section_flags (htab->elf.dynobj, s, flags))
2539 return NULL;
2540 if (sym_sec == NULL)
2541 sym_sec = s;
2543 lsect->section = s;
2545 if (bfd_get_section_alignment (htab->elf.dynobj, s) < 2
2546 && !bfd_set_section_alignment (htab->elf.dynobj, s, 2))
2547 return NULL;
2549 s->size = align_power (s->size, 2);
2551 #ifdef DEBUG
2552 fprintf (stderr, "Creating section %s, current size = %ld\n",
2553 name, (long) s->size);
2554 #endif
2556 if (sym_name)
2558 struct elf_link_hash_entry *h;
2559 struct bfd_link_hash_entry *bh;
2561 #ifdef DEBUG
2562 fprintf (stderr, "Adding %s to section %s\n", sym_name, name);
2563 #endif
2564 bh = bfd_link_hash_lookup (info->hash, sym_name,
2565 FALSE, FALSE, FALSE);
2567 if ((bh == NULL || bh->type == bfd_link_hash_undefined)
2568 && !(_bfd_generic_link_add_one_symbol
2569 (info, abfd, sym_name, BSF_GLOBAL, sym_sec, sym_offset, NULL,
2570 FALSE, get_elf_backend_data (abfd)->collect, &bh)))
2571 return NULL;
2572 h = (struct elf_link_hash_entry *) bh;
2574 h->type = STT_OBJECT;
2575 lsect->sym_hash = h;
2577 if (info->shared
2578 && ! bfd_elf_link_record_dynamic_symbol (info, h))
2579 return NULL;
2582 if (info->shared)
2584 s = bfd_make_section_anyway (htab->elf.dynobj, rel_name);
2585 lsect->rel_section = s;
2586 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
2587 | SEC_LINKER_CREATED | SEC_READONLY);
2588 if (s == NULL
2589 || ! bfd_set_section_flags (htab->elf.dynobj, s, flags)
2590 || ! bfd_set_section_alignment (htab->elf.dynobj, s, 2))
2591 return NULL;
2594 return lsect;
2597 /* Find a linker generated pointer with a given addend and type. */
2599 static elf_linker_section_pointers_t *
2600 elf_find_pointer_linker_section
2601 (elf_linker_section_pointers_t *linker_pointers,
2602 bfd_vma addend,
2603 elf_linker_section_t *lsect)
2605 for ( ; linker_pointers != NULL; linker_pointers = linker_pointers->next)
2606 if (lsect == linker_pointers->lsect && addend == linker_pointers->addend)
2607 return linker_pointers;
2609 return NULL;
2612 /* Allocate a pointer to live in a linker created section. */
2614 static bfd_boolean
2615 elf_create_pointer_linker_section (bfd *abfd,
2616 struct bfd_link_info *info,
2617 elf_linker_section_t *lsect,
2618 struct elf_link_hash_entry *h,
2619 const Elf_Internal_Rela *rel)
2621 elf_linker_section_pointers_t **ptr_linker_section_ptr = NULL;
2622 elf_linker_section_pointers_t *linker_section_ptr;
2623 unsigned long r_symndx = ELF32_R_SYM (rel->r_info);
2624 bfd_size_type amt;
2626 BFD_ASSERT (lsect != NULL);
2628 /* Is this a global symbol? */
2629 if (h != NULL)
2631 struct ppc_elf_link_hash_entry *eh;
2633 /* Has this symbol already been allocated? If so, our work is done. */
2634 eh = (struct ppc_elf_link_hash_entry *) h;
2635 if (elf_find_pointer_linker_section (eh->linker_section_pointer,
2636 rel->r_addend,
2637 lsect))
2638 return TRUE;
2640 ptr_linker_section_ptr = &eh->linker_section_pointer;
2641 /* Make sure this symbol is output as a dynamic symbol. */
2642 if (h->dynindx == -1)
2644 if (! bfd_elf_link_record_dynamic_symbol (info, h))
2645 return FALSE;
2648 if (lsect->rel_section)
2649 lsect->rel_section->size += sizeof (Elf32_External_Rela);
2651 else
2653 /* Allocation of a pointer to a local symbol. */
2654 elf_linker_section_pointers_t **ptr = elf_local_ptr_offsets (abfd);
2656 /* Allocate a table to hold the local symbols if first time. */
2657 if (!ptr)
2659 unsigned int num_symbols = elf_tdata (abfd)->symtab_hdr.sh_info;
2661 amt = num_symbols;
2662 amt *= sizeof (elf_linker_section_pointers_t *);
2663 ptr = bfd_zalloc (abfd, amt);
2665 if (!ptr)
2666 return FALSE;
2668 elf_local_ptr_offsets (abfd) = ptr;
2671 /* Has this symbol already been allocated? If so, our work is done. */
2672 if (elf_find_pointer_linker_section (ptr[r_symndx],
2673 rel->r_addend,
2674 lsect))
2675 return TRUE;
2677 ptr_linker_section_ptr = &ptr[r_symndx];
2679 if (info->shared)
2681 /* If we are generating a shared object, we need to
2682 output a R_<xxx>_RELATIVE reloc so that the
2683 dynamic linker can adjust this GOT entry. */
2684 BFD_ASSERT (lsect->rel_section != NULL);
2685 lsect->rel_section->size += sizeof (Elf32_External_Rela);
2689 /* Allocate space for a pointer in the linker section, and allocate
2690 a new pointer record from internal memory. */
2691 BFD_ASSERT (ptr_linker_section_ptr != NULL);
2692 amt = sizeof (elf_linker_section_pointers_t);
2693 linker_section_ptr = bfd_alloc (abfd, amt);
2695 if (!linker_section_ptr)
2696 return FALSE;
2698 linker_section_ptr->next = *ptr_linker_section_ptr;
2699 linker_section_ptr->addend = rel->r_addend;
2700 linker_section_ptr->lsect = lsect;
2701 linker_section_ptr->written_address_p = FALSE;
2702 *ptr_linker_section_ptr = linker_section_ptr;
2704 linker_section_ptr->offset = lsect->section->size;
2705 lsect->section->size += 4;
2707 #ifdef DEBUG
2708 fprintf (stderr,
2709 "Create pointer in linker section %s, offset = %ld, section size = %ld\n",
2710 lsect->name, (long) linker_section_ptr->offset,
2711 (long) lsect->section->size);
2712 #endif
2714 return TRUE;
2717 static bfd_boolean
2718 update_local_sym_info (bfd *abfd,
2719 Elf_Internal_Shdr *symtab_hdr,
2720 unsigned long r_symndx,
2721 int tls_type)
2723 bfd_signed_vma *local_got_refcounts = elf_local_got_refcounts (abfd);
2724 char *local_got_tls_masks;
2726 if (local_got_refcounts == NULL)
2728 bfd_size_type size = symtab_hdr->sh_info;
2730 size *= sizeof (*local_got_refcounts) + sizeof (*local_got_tls_masks);
2731 local_got_refcounts = bfd_zalloc (abfd, size);
2732 if (local_got_refcounts == NULL)
2733 return FALSE;
2734 elf_local_got_refcounts (abfd) = local_got_refcounts;
2737 local_got_refcounts[r_symndx] += 1;
2738 local_got_tls_masks = (char *) (local_got_refcounts + symtab_hdr->sh_info);
2739 local_got_tls_masks[r_symndx] |= tls_type;
2740 return TRUE;
2743 static void
2744 bad_shared_reloc (bfd *abfd, enum elf_ppc_reloc_type r_type)
2746 (*_bfd_error_handler)
2747 (_("%B: relocation %s cannot be used when making a shared object"),
2748 abfd,
2749 ppc_elf_howto_table[r_type]->name);
2750 bfd_set_error (bfd_error_bad_value);
2753 /* Look through the relocs for a section during the first phase, and
2754 allocate space in the global offset table or procedure linkage
2755 table. */
2757 static bfd_boolean
2758 ppc_elf_check_relocs (bfd *abfd,
2759 struct bfd_link_info *info,
2760 asection *sec,
2761 const Elf_Internal_Rela *relocs)
2763 struct ppc_elf_link_hash_table *htab;
2764 Elf_Internal_Shdr *symtab_hdr;
2765 struct elf_link_hash_entry **sym_hashes;
2766 const Elf_Internal_Rela *rel;
2767 const Elf_Internal_Rela *rel_end;
2768 asection *sreloc;
2770 if (info->relocatable)
2771 return TRUE;
2773 #ifdef DEBUG
2774 _bfd_error_handler ("ppc_elf_check_relocs called for section %A in %B",
2775 sec, abfd);
2776 #endif
2778 /* Initialize howto table if not already done. */
2779 if (!ppc_elf_howto_table[R_PPC_ADDR32])
2780 ppc_elf_howto_init ();
2782 /* Create the linker generated sections all the time so that the
2783 special symbols are created. */
2784 htab = ppc_elf_hash_table (info);
2785 if (htab->sdata == NULL)
2787 htab->sdata = ppc_elf_create_linker_section (abfd, info,
2788 LINKER_SECTION_SDATA);
2789 if (htab->sdata == NULL)
2790 return FALSE;
2793 if (htab->sdata2 == NULL)
2795 htab->sdata2 = ppc_elf_create_linker_section (abfd, info,
2796 LINKER_SECTION_SDATA2);
2797 if (htab->sdata2 == NULL)
2798 return FALSE;
2801 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2802 sym_hashes = elf_sym_hashes (abfd);
2803 sreloc = NULL;
2805 rel_end = relocs + sec->reloc_count;
2806 for (rel = relocs; rel < rel_end; rel++)
2808 unsigned long r_symndx;
2809 enum elf_ppc_reloc_type r_type;
2810 struct elf_link_hash_entry *h;
2811 int tls_type = 0;
2813 r_symndx = ELF32_R_SYM (rel->r_info);
2814 if (r_symndx < symtab_hdr->sh_info)
2815 h = NULL;
2816 else
2817 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
2819 /* If a relocation refers to _GLOBAL_OFFSET_TABLE_, create the .got.
2820 This shows up in particular in an R_PPC_ADDR32 in the eabi
2821 startup code. */
2822 if (h && strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
2824 if (htab->got == NULL)
2826 if (htab->elf.dynobj == NULL)
2827 htab->elf.dynobj = abfd;
2828 if (!ppc_elf_create_got (htab->elf.dynobj, info))
2829 return FALSE;
2833 r_type = ELF32_R_TYPE (rel->r_info);
2834 switch (r_type)
2836 case R_PPC_GOT_TLSLD16:
2837 case R_PPC_GOT_TLSLD16_LO:
2838 case R_PPC_GOT_TLSLD16_HI:
2839 case R_PPC_GOT_TLSLD16_HA:
2840 htab->tlsld_got.refcount += 1;
2841 tls_type = TLS_TLS | TLS_LD;
2842 goto dogottls;
2844 case R_PPC_GOT_TLSGD16:
2845 case R_PPC_GOT_TLSGD16_LO:
2846 case R_PPC_GOT_TLSGD16_HI:
2847 case R_PPC_GOT_TLSGD16_HA:
2848 tls_type = TLS_TLS | TLS_GD;
2849 goto dogottls;
2851 case R_PPC_GOT_TPREL16:
2852 case R_PPC_GOT_TPREL16_LO:
2853 case R_PPC_GOT_TPREL16_HI:
2854 case R_PPC_GOT_TPREL16_HA:
2855 if (info->shared)
2856 info->flags |= DF_STATIC_TLS;
2857 tls_type = TLS_TLS | TLS_TPREL;
2858 goto dogottls;
2860 case R_PPC_GOT_DTPREL16:
2861 case R_PPC_GOT_DTPREL16_LO:
2862 case R_PPC_GOT_DTPREL16_HI:
2863 case R_PPC_GOT_DTPREL16_HA:
2864 tls_type = TLS_TLS | TLS_DTPREL;
2865 dogottls:
2866 sec->has_tls_reloc = 1;
2867 /* Fall thru */
2869 /* GOT16 relocations */
2870 case R_PPC_GOT16:
2871 case R_PPC_GOT16_LO:
2872 case R_PPC_GOT16_HI:
2873 case R_PPC_GOT16_HA:
2874 /* This symbol requires a global offset table entry. */
2875 if (htab->got == NULL)
2877 if (htab->elf.dynobj == NULL)
2878 htab->elf.dynobj = abfd;
2879 if (!ppc_elf_create_got (htab->elf.dynobj, info))
2880 return FALSE;
2882 if (h != NULL)
2884 h->got.refcount += 1;
2885 ppc_elf_hash_entry (h)->tls_mask |= tls_type;
2887 else
2888 /* This is a global offset table entry for a local symbol. */
2889 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx, tls_type))
2890 return FALSE;
2891 break;
2893 /* Indirect .sdata relocation. */
2894 case R_PPC_EMB_SDAI16:
2895 if (info->shared)
2897 bad_shared_reloc (abfd, r_type);
2898 return FALSE;
2900 if (!elf_create_pointer_linker_section (abfd, info,
2901 htab->sdata, h, rel))
2902 return FALSE;
2903 break;
2905 /* Indirect .sdata2 relocation. */
2906 case R_PPC_EMB_SDA2I16:
2907 if (info->shared)
2909 bad_shared_reloc (abfd, r_type);
2910 return FALSE;
2912 if (!elf_create_pointer_linker_section (abfd, info,
2913 htab->sdata2, h, rel))
2914 return FALSE;
2915 break;
2917 case R_PPC_SDAREL16:
2918 case R_PPC_EMB_SDA2REL:
2919 case R_PPC_EMB_SDA21:
2920 case R_PPC_EMB_RELSDA:
2921 case R_PPC_EMB_NADDR32:
2922 case R_PPC_EMB_NADDR16:
2923 case R_PPC_EMB_NADDR16_LO:
2924 case R_PPC_EMB_NADDR16_HI:
2925 case R_PPC_EMB_NADDR16_HA:
2926 if (info->shared)
2928 bad_shared_reloc (abfd, r_type);
2929 return FALSE;
2931 break;
2933 case R_PPC_PLT32:
2934 case R_PPC_PLTREL24:
2935 case R_PPC_PLTREL32:
2936 case R_PPC_PLT16_LO:
2937 case R_PPC_PLT16_HI:
2938 case R_PPC_PLT16_HA:
2939 #ifdef DEBUG
2940 fprintf (stderr, "Reloc requires a PLT entry\n");
2941 #endif
2942 /* This symbol requires a procedure linkage table entry. We
2943 actually build the entry in finish_dynamic_symbol,
2944 because this might be a case of linking PIC code without
2945 linking in any dynamic objects, in which case we don't
2946 need to generate a procedure linkage table after all. */
2948 if (h == NULL)
2950 /* It does not make sense to have a procedure linkage
2951 table entry for a local symbol. */
2952 (*_bfd_error_handler) (_("%B(%A+0x%lx): %s reloc against "
2953 "local symbol"),
2954 abfd,
2955 sec,
2956 (long) rel->r_offset,
2957 ppc_elf_howto_table[r_type]->name);
2958 bfd_set_error (bfd_error_bad_value);
2959 return FALSE;
2962 h->needs_plt = 1;
2963 h->plt.refcount++;
2964 break;
2966 /* The following relocations don't need to propagate the
2967 relocation if linking a shared object since they are
2968 section relative. */
2969 case R_PPC_SECTOFF:
2970 case R_PPC_SECTOFF_LO:
2971 case R_PPC_SECTOFF_HI:
2972 case R_PPC_SECTOFF_HA:
2973 case R_PPC_DTPREL16:
2974 case R_PPC_DTPREL16_LO:
2975 case R_PPC_DTPREL16_HI:
2976 case R_PPC_DTPREL16_HA:
2977 case R_PPC_TOC16:
2978 break;
2980 /* This are just markers. */
2981 case R_PPC_TLS:
2982 case R_PPC_EMB_MRKREF:
2983 case R_PPC_NONE:
2984 case R_PPC_max:
2985 break;
2987 /* These should only appear in dynamic objects. */
2988 case R_PPC_COPY:
2989 case R_PPC_GLOB_DAT:
2990 case R_PPC_JMP_SLOT:
2991 case R_PPC_RELATIVE:
2992 break;
2994 /* These aren't handled yet. We'll report an error later. */
2995 case R_PPC_ADDR30:
2996 case R_PPC_EMB_RELSEC16:
2997 case R_PPC_EMB_RELST_LO:
2998 case R_PPC_EMB_RELST_HI:
2999 case R_PPC_EMB_RELST_HA:
3000 case R_PPC_EMB_BIT_FLD:
3001 break;
3003 /* This refers only to functions defined in the shared library. */
3004 case R_PPC_LOCAL24PC:
3005 break;
3007 /* This relocation describes the C++ object vtable hierarchy.
3008 Reconstruct it for later use during GC. */
3009 case R_PPC_GNU_VTINHERIT:
3010 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
3011 return FALSE;
3012 break;
3014 /* This relocation describes which C++ vtable entries are actually
3015 used. Record for later use during GC. */
3016 case R_PPC_GNU_VTENTRY:
3017 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
3018 return FALSE;
3019 break;
3021 /* We shouldn't really be seeing these. */
3022 case R_PPC_TPREL32:
3023 if (info->shared)
3024 info->flags |= DF_STATIC_TLS;
3025 goto dodyn;
3027 /* Nor these. */
3028 case R_PPC_DTPMOD32:
3029 case R_PPC_DTPREL32:
3030 goto dodyn;
3032 case R_PPC_TPREL16:
3033 case R_PPC_TPREL16_LO:
3034 case R_PPC_TPREL16_HI:
3035 case R_PPC_TPREL16_HA:
3036 if (info->shared)
3037 info->flags |= DF_STATIC_TLS;
3038 goto dodyn;
3040 /* When creating a shared object, we must copy these
3041 relocs into the output file. We create a reloc
3042 section in dynobj and make room for the reloc. */
3043 case R_PPC_REL24:
3044 case R_PPC_REL14:
3045 case R_PPC_REL14_BRTAKEN:
3046 case R_PPC_REL14_BRNTAKEN:
3047 case R_PPC_REL32:
3048 if (h == NULL
3049 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
3050 break;
3051 /* fall through */
3053 case R_PPC_ADDR32:
3054 case R_PPC_ADDR24:
3055 case R_PPC_ADDR16:
3056 case R_PPC_ADDR16_LO:
3057 case R_PPC_ADDR16_HI:
3058 case R_PPC_ADDR16_HA:
3059 case R_PPC_ADDR14:
3060 case R_PPC_ADDR14_BRTAKEN:
3061 case R_PPC_ADDR14_BRNTAKEN:
3062 case R_PPC_UADDR32:
3063 case R_PPC_UADDR16:
3064 if (h != NULL && !info->shared)
3066 /* We may need a plt entry if the symbol turns out to be
3067 a function defined in a dynamic object. */
3068 h->plt.refcount++;
3070 /* We may need a copy reloc too. */
3071 h->non_got_ref = 1;
3074 dodyn:
3075 /* If we are creating a shared library, and this is a reloc
3076 against a global symbol, or a non PC relative reloc
3077 against a local symbol, then we need to copy the reloc
3078 into the shared library. However, if we are linking with
3079 -Bsymbolic, we do not need to copy a reloc against a
3080 global symbol which is defined in an object we are
3081 including in the link (i.e., DEF_REGULAR is set). At
3082 this point we have not seen all the input files, so it is
3083 possible that DEF_REGULAR is not set now but will be set
3084 later (it is never cleared). In case of a weak definition,
3085 DEF_REGULAR may be cleared later by a strong definition in
3086 a shared library. We account for that possibility below by
3087 storing information in the dyn_relocs field of the hash
3088 table entry. A similar situation occurs when creating
3089 shared libraries and symbol visibility changes render the
3090 symbol local.
3092 If on the other hand, we are creating an executable, we
3093 may need to keep relocations for symbols satisfied by a
3094 dynamic library if we manage to avoid copy relocs for the
3095 symbol. */
3096 if ((info->shared
3097 && (MUST_BE_DYN_RELOC (r_type)
3098 || (h != NULL
3099 && (! info->symbolic
3100 || h->root.type == bfd_link_hash_defweak
3101 || !h->def_regular))))
3102 || (ELIMINATE_COPY_RELOCS
3103 && !info->shared
3104 && (sec->flags & SEC_ALLOC) != 0
3105 && h != NULL
3106 && (h->root.type == bfd_link_hash_defweak
3107 || !h->def_regular)))
3109 struct ppc_elf_dyn_relocs *p;
3110 struct ppc_elf_dyn_relocs **head;
3112 #ifdef DEBUG
3113 fprintf (stderr,
3114 "ppc_elf_check_relocs needs to "
3115 "create relocation for %s\n",
3116 (h && h->root.root.string
3117 ? h->root.root.string : "<unknown>"));
3118 #endif
3119 if (sreloc == NULL)
3121 const char *name;
3123 name = (bfd_elf_string_from_elf_section
3124 (abfd,
3125 elf_elfheader (abfd)->e_shstrndx,
3126 elf_section_data (sec)->rel_hdr.sh_name));
3127 if (name == NULL)
3128 return FALSE;
3130 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
3131 && strcmp (bfd_get_section_name (abfd, sec),
3132 name + 5) == 0);
3134 sreloc = bfd_get_section_by_name (htab->elf.dynobj, name);
3135 if (sreloc == NULL)
3137 flagword flags;
3139 sreloc = bfd_make_section (htab->elf.dynobj, name);
3140 flags = (SEC_HAS_CONTENTS | SEC_READONLY
3141 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3142 if ((sec->flags & SEC_ALLOC) != 0)
3143 flags |= SEC_ALLOC | SEC_LOAD;
3144 if (sreloc == NULL
3145 || ! bfd_set_section_flags (htab->elf.dynobj,
3146 sreloc, flags)
3147 || ! bfd_set_section_alignment (htab->elf.dynobj,
3148 sreloc, 2))
3149 return FALSE;
3151 elf_section_data (sec)->sreloc = sreloc;
3154 /* If this is a global symbol, we count the number of
3155 relocations we need for this symbol. */
3156 if (h != NULL)
3158 head = &ppc_elf_hash_entry (h)->dyn_relocs;
3160 else
3162 /* Track dynamic relocs needed for local syms too.
3163 We really need local syms available to do this
3164 easily. Oh well. */
3166 asection *s;
3167 s = bfd_section_from_r_symndx (abfd, &htab->sym_sec,
3168 sec, r_symndx);
3169 if (s == NULL)
3170 return FALSE;
3172 head = ((struct ppc_elf_dyn_relocs **)
3173 &elf_section_data (s)->local_dynrel);
3176 p = *head;
3177 if (p == NULL || p->sec != sec)
3179 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
3180 if (p == NULL)
3181 return FALSE;
3182 p->next = *head;
3183 *head = p;
3184 p->sec = sec;
3185 p->count = 0;
3186 p->pc_count = 0;
3189 p->count += 1;
3190 if (!MUST_BE_DYN_RELOC (r_type))
3191 p->pc_count += 1;
3194 break;
3198 return TRUE;
3201 /* Return the section that should be marked against GC for a given
3202 relocation. */
3204 static asection *
3205 ppc_elf_gc_mark_hook (asection *sec,
3206 struct bfd_link_info *info ATTRIBUTE_UNUSED,
3207 Elf_Internal_Rela *rel,
3208 struct elf_link_hash_entry *h,
3209 Elf_Internal_Sym *sym)
3211 if (h != NULL)
3213 switch (ELF32_R_TYPE (rel->r_info))
3215 case R_PPC_GNU_VTINHERIT:
3216 case R_PPC_GNU_VTENTRY:
3217 break;
3219 default:
3220 switch (h->root.type)
3222 case bfd_link_hash_defined:
3223 case bfd_link_hash_defweak:
3224 return h->root.u.def.section;
3226 case bfd_link_hash_common:
3227 return h->root.u.c.p->section;
3229 default:
3230 break;
3234 else
3235 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
3237 return NULL;
3240 /* Update the got, plt and dynamic reloc reference counts for the
3241 section being removed. */
3243 static bfd_boolean
3244 ppc_elf_gc_sweep_hook (bfd *abfd,
3245 struct bfd_link_info *info,
3246 asection *sec,
3247 const Elf_Internal_Rela *relocs)
3249 struct ppc_elf_link_hash_table *htab;
3250 Elf_Internal_Shdr *symtab_hdr;
3251 struct elf_link_hash_entry **sym_hashes;
3252 bfd_signed_vma *local_got_refcounts;
3253 const Elf_Internal_Rela *rel, *relend;
3255 elf_section_data (sec)->local_dynrel = NULL;
3257 htab = ppc_elf_hash_table (info);
3258 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
3259 sym_hashes = elf_sym_hashes (abfd);
3260 local_got_refcounts = elf_local_got_refcounts (abfd);
3262 relend = relocs + sec->reloc_count;
3263 for (rel = relocs; rel < relend; rel++)
3265 unsigned long r_symndx;
3266 enum elf_ppc_reloc_type r_type;
3267 struct elf_link_hash_entry *h = NULL;
3269 r_symndx = ELF32_R_SYM (rel->r_info);
3270 if (r_symndx >= symtab_hdr->sh_info)
3272 struct ppc_elf_dyn_relocs **pp, *p;
3273 struct ppc_elf_link_hash_entry *eh;
3275 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
3276 while (h->root.type == bfd_link_hash_indirect
3277 || h->root.type == bfd_link_hash_warning)
3278 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3279 eh = (struct ppc_elf_link_hash_entry *) h;
3281 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
3282 if (p->sec == sec)
3284 /* Everything must go for SEC. */
3285 *pp = p->next;
3286 break;
3290 r_type = ELF32_R_TYPE (rel->r_info);
3291 switch (r_type)
3293 case R_PPC_GOT_TLSLD16:
3294 case R_PPC_GOT_TLSLD16_LO:
3295 case R_PPC_GOT_TLSLD16_HI:
3296 case R_PPC_GOT_TLSLD16_HA:
3297 htab->tlsld_got.refcount -= 1;
3298 /* Fall thru */
3300 case R_PPC_GOT_TLSGD16:
3301 case R_PPC_GOT_TLSGD16_LO:
3302 case R_PPC_GOT_TLSGD16_HI:
3303 case R_PPC_GOT_TLSGD16_HA:
3304 case R_PPC_GOT_TPREL16:
3305 case R_PPC_GOT_TPREL16_LO:
3306 case R_PPC_GOT_TPREL16_HI:
3307 case R_PPC_GOT_TPREL16_HA:
3308 case R_PPC_GOT_DTPREL16:
3309 case R_PPC_GOT_DTPREL16_LO:
3310 case R_PPC_GOT_DTPREL16_HI:
3311 case R_PPC_GOT_DTPREL16_HA:
3312 case R_PPC_GOT16:
3313 case R_PPC_GOT16_LO:
3314 case R_PPC_GOT16_HI:
3315 case R_PPC_GOT16_HA:
3316 if (h != NULL)
3318 if (h->got.refcount > 0)
3319 h->got.refcount--;
3321 else if (local_got_refcounts != NULL)
3323 if (local_got_refcounts[r_symndx] > 0)
3324 local_got_refcounts[r_symndx]--;
3326 break;
3328 case R_PPC_REL24:
3329 case R_PPC_REL14:
3330 case R_PPC_REL14_BRTAKEN:
3331 case R_PPC_REL14_BRNTAKEN:
3332 case R_PPC_REL32:
3333 if (h == NULL
3334 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
3335 break;
3336 /* Fall thru */
3338 case R_PPC_ADDR32:
3339 case R_PPC_ADDR24:
3340 case R_PPC_ADDR16:
3341 case R_PPC_ADDR16_LO:
3342 case R_PPC_ADDR16_HI:
3343 case R_PPC_ADDR16_HA:
3344 case R_PPC_ADDR14:
3345 case R_PPC_ADDR14_BRTAKEN:
3346 case R_PPC_ADDR14_BRNTAKEN:
3347 case R_PPC_UADDR32:
3348 case R_PPC_UADDR16:
3349 case R_PPC_PLT32:
3350 case R_PPC_PLTREL24:
3351 case R_PPC_PLT16_LO:
3352 case R_PPC_PLT16_HI:
3353 case R_PPC_PLT16_HA:
3354 if (h != NULL)
3356 if (h->plt.refcount > 0)
3357 h->plt.refcount--;
3359 break;
3361 default:
3362 break;
3365 return TRUE;
3368 /* Set htab->tls_get_addr and call the generic ELF tls_setup function. */
3370 asection *
3371 ppc_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
3373 struct ppc_elf_link_hash_table *htab;
3375 htab = ppc_elf_hash_table (info);
3376 htab->tls_get_addr = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
3377 FALSE, FALSE, TRUE);
3379 return _bfd_elf_tls_setup (obfd, info);
3382 /* Run through all the TLS relocs looking for optimization
3383 opportunities. */
3385 bfd_boolean
3386 ppc_elf_tls_optimize (bfd *obfd ATTRIBUTE_UNUSED,
3387 struct bfd_link_info *info)
3389 bfd *ibfd;
3390 asection *sec;
3391 struct ppc_elf_link_hash_table *htab;
3393 if (info->relocatable || info->shared)
3394 return TRUE;
3396 htab = ppc_elf_hash_table (info);
3397 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
3399 Elf_Internal_Sym *locsyms = NULL;
3400 Elf_Internal_Shdr *symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
3402 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
3403 if (sec->has_tls_reloc && !bfd_is_abs_section (sec->output_section))
3405 Elf_Internal_Rela *relstart, *rel, *relend;
3406 int expecting_tls_get_addr;
3408 /* Read the relocations. */
3409 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
3410 info->keep_memory);
3411 if (relstart == NULL)
3412 return FALSE;
3414 expecting_tls_get_addr = 0;
3415 relend = relstart + sec->reloc_count;
3416 for (rel = relstart; rel < relend; rel++)
3418 enum elf_ppc_reloc_type r_type;
3419 unsigned long r_symndx;
3420 struct elf_link_hash_entry *h = NULL;
3421 char *tls_mask;
3422 char tls_set, tls_clear;
3423 bfd_boolean is_local;
3425 r_symndx = ELF32_R_SYM (rel->r_info);
3426 if (r_symndx >= symtab_hdr->sh_info)
3428 struct elf_link_hash_entry **sym_hashes;
3430 sym_hashes = elf_sym_hashes (ibfd);
3431 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
3432 while (h->root.type == bfd_link_hash_indirect
3433 || h->root.type == bfd_link_hash_warning)
3434 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3437 is_local = FALSE;
3438 if (h == NULL
3439 || !h->def_dynamic)
3440 is_local = TRUE;
3442 r_type = ELF32_R_TYPE (rel->r_info);
3443 switch (r_type)
3445 case R_PPC_GOT_TLSLD16:
3446 case R_PPC_GOT_TLSLD16_LO:
3447 case R_PPC_GOT_TLSLD16_HI:
3448 case R_PPC_GOT_TLSLD16_HA:
3449 /* These relocs should never be against a symbol
3450 defined in a shared lib. Leave them alone if
3451 that turns out to be the case. */
3452 expecting_tls_get_addr = 0;
3453 htab->tlsld_got.refcount -= 1;
3454 if (!is_local)
3455 continue;
3457 /* LD -> LE */
3458 tls_set = 0;
3459 tls_clear = TLS_LD;
3460 expecting_tls_get_addr = 1;
3461 break;
3463 case R_PPC_GOT_TLSGD16:
3464 case R_PPC_GOT_TLSGD16_LO:
3465 case R_PPC_GOT_TLSGD16_HI:
3466 case R_PPC_GOT_TLSGD16_HA:
3467 if (is_local)
3468 /* GD -> LE */
3469 tls_set = 0;
3470 else
3471 /* GD -> IE */
3472 tls_set = TLS_TLS | TLS_TPRELGD;
3473 tls_clear = TLS_GD;
3474 expecting_tls_get_addr = 1;
3475 break;
3477 case R_PPC_GOT_TPREL16:
3478 case R_PPC_GOT_TPREL16_LO:
3479 case R_PPC_GOT_TPREL16_HI:
3480 case R_PPC_GOT_TPREL16_HA:
3481 expecting_tls_get_addr = 0;
3482 if (is_local)
3484 /* IE -> LE */
3485 tls_set = 0;
3486 tls_clear = TLS_TPREL;
3487 break;
3489 else
3490 continue;
3492 case R_PPC_REL14:
3493 case R_PPC_REL14_BRTAKEN:
3494 case R_PPC_REL14_BRNTAKEN:
3495 case R_PPC_REL24:
3496 if (expecting_tls_get_addr
3497 && h != NULL
3498 && h == htab->tls_get_addr)
3500 if (h->plt.refcount > 0)
3501 h->plt.refcount -= 1;
3503 expecting_tls_get_addr = 0;
3504 continue;
3506 default:
3507 expecting_tls_get_addr = 0;
3508 continue;
3511 if (h != NULL)
3513 if (tls_set == 0)
3515 /* We managed to get rid of a got entry. */
3516 if (h->got.refcount > 0)
3517 h->got.refcount -= 1;
3519 tls_mask = &ppc_elf_hash_entry (h)->tls_mask;
3521 else
3523 Elf_Internal_Sym *sym;
3524 bfd_signed_vma *lgot_refs;
3525 char *lgot_masks;
3527 if (locsyms == NULL)
3529 locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
3530 if (locsyms == NULL)
3531 locsyms = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
3532 symtab_hdr->sh_info,
3533 0, NULL, NULL, NULL);
3534 if (locsyms == NULL)
3536 if (elf_section_data (sec)->relocs != relstart)
3537 free (relstart);
3538 return FALSE;
3541 sym = locsyms + r_symndx;
3542 lgot_refs = elf_local_got_refcounts (ibfd);
3543 if (lgot_refs == NULL)
3544 abort ();
3545 if (tls_set == 0)
3547 /* We managed to get rid of a got entry. */
3548 if (lgot_refs[r_symndx] > 0)
3549 lgot_refs[r_symndx] -= 1;
3551 lgot_masks = (char *) (lgot_refs + symtab_hdr->sh_info);
3552 tls_mask = &lgot_masks[r_symndx];
3555 *tls_mask |= tls_set;
3556 *tls_mask &= ~tls_clear;
3559 if (elf_section_data (sec)->relocs != relstart)
3560 free (relstart);
3563 if (locsyms != NULL
3564 && (symtab_hdr->contents != (unsigned char *) locsyms))
3566 if (!info->keep_memory)
3567 free (locsyms);
3568 else
3569 symtab_hdr->contents = (unsigned char *) locsyms;
3572 return TRUE;
3575 /* Adjust a symbol defined by a dynamic object and referenced by a
3576 regular object. The current definition is in some section of the
3577 dynamic object, but we're not including those sections. We have to
3578 change the definition to something the rest of the link can
3579 understand. */
3581 static bfd_boolean
3582 ppc_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
3583 struct elf_link_hash_entry *h)
3585 struct ppc_elf_link_hash_table *htab;
3586 asection *s;
3587 unsigned int power_of_two;
3589 #ifdef DEBUG
3590 fprintf (stderr, "ppc_elf_adjust_dynamic_symbol called for %s\n",
3591 h->root.root.string);
3592 #endif
3594 /* Make sure we know what is going on here. */
3595 htab = ppc_elf_hash_table (info);
3596 BFD_ASSERT (htab->elf.dynobj != NULL
3597 && (h->needs_plt
3598 || h->u.weakdef != NULL
3599 || (h->def_dynamic
3600 && h->ref_regular
3601 && !h->def_regular)));
3603 /* Deal with function syms. */
3604 if (h->type == STT_FUNC
3605 || h->needs_plt)
3607 /* Clear procedure linkage table information for any symbol that
3608 won't need a .plt entry. */
3609 if (h->plt.refcount <= 0
3610 || SYMBOL_CALLS_LOCAL (info, h)
3611 || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
3612 && h->root.type == bfd_link_hash_undefweak))
3614 /* A PLT entry is not required/allowed when:
3616 1. We are not using ld.so; because then the PLT entry
3617 can't be set up, so we can't use one. In this case,
3618 ppc_elf_adjust_dynamic_symbol won't even be called.
3620 2. GC has rendered the entry unused.
3622 3. We know for certain that a call to this symbol
3623 will go to this object, or will remain undefined. */
3624 h->plt.offset = (bfd_vma) -1;
3625 h->needs_plt = 0;
3627 return TRUE;
3629 else
3630 h->plt.offset = (bfd_vma) -1;
3632 /* If this is a weak symbol, and there is a real definition, the
3633 processor independent code will have arranged for us to see the
3634 real definition first, and we can just use the same value. */
3635 if (h->u.weakdef != NULL)
3637 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
3638 || h->u.weakdef->root.type == bfd_link_hash_defweak);
3639 h->root.u.def.section = h->u.weakdef->root.u.def.section;
3640 h->root.u.def.value = h->u.weakdef->root.u.def.value;
3641 if (ELIMINATE_COPY_RELOCS)
3642 h->non_got_ref = h->u.weakdef->non_got_ref;
3643 return TRUE;
3646 /* This is a reference to a symbol defined by a dynamic object which
3647 is not a function. */
3649 /* If we are creating a shared library, we must presume that the
3650 only references to the symbol are via the global offset table.
3651 For such cases we need not do anything here; the relocations will
3652 be handled correctly by relocate_section. */
3653 if (info->shared)
3654 return TRUE;
3656 /* If there are no references to this symbol that do not use the
3657 GOT, we don't need to generate a copy reloc. */
3658 if (!h->non_got_ref)
3659 return TRUE;
3661 if (ELIMINATE_COPY_RELOCS)
3663 struct ppc_elf_dyn_relocs *p;
3664 for (p = ppc_elf_hash_entry (h)->dyn_relocs; p != NULL; p = p->next)
3666 s = p->sec->output_section;
3667 if (s != NULL && (s->flags & SEC_READONLY) != 0)
3668 break;
3671 /* If we didn't find any dynamic relocs in read-only sections, then
3672 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
3673 if (p == NULL)
3675 h->non_got_ref = 0;
3676 return TRUE;
3680 /* We must allocate the symbol in our .dynbss section, which will
3681 become part of the .bss section of the executable. There will be
3682 an entry for this symbol in the .dynsym section. The dynamic
3683 object will contain position independent code, so all references
3684 from the dynamic object to this symbol will go through the global
3685 offset table. The dynamic linker will use the .dynsym entry to
3686 determine the address it must put in the global offset table, so
3687 both the dynamic object and the regular object will refer to the
3688 same memory location for the variable.
3690 Of course, if the symbol is sufficiently small, we must instead
3691 allocate it in .sbss. FIXME: It would be better to do this if and
3692 only if there were actually SDAREL relocs for that symbol. */
3694 if (h->size <= elf_gp_size (htab->elf.dynobj))
3695 s = htab->dynsbss;
3696 else
3697 s = htab->dynbss;
3698 BFD_ASSERT (s != NULL);
3700 /* We must generate a R_PPC_COPY reloc to tell the dynamic linker to
3701 copy the initial value out of the dynamic object and into the
3702 runtime process image. We need to remember the offset into the
3703 .rela.bss section we are going to use. */
3704 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
3706 asection *srel;
3708 if (h->size <= elf_gp_size (htab->elf.dynobj))
3709 srel = htab->relsbss;
3710 else
3711 srel = htab->relbss;
3712 BFD_ASSERT (srel != NULL);
3713 srel->size += sizeof (Elf32_External_Rela);
3714 h->needs_copy = 1;
3717 /* We need to figure out the alignment required for this symbol. I
3718 have no idea how ELF linkers handle this. */
3719 power_of_two = bfd_log2 (h->size);
3720 if (power_of_two > 4)
3721 power_of_two = 4;
3723 /* Apply the required alignment. */
3724 s->size = BFD_ALIGN (s->size, (bfd_size_type) (1 << power_of_two));
3725 if (power_of_two > bfd_get_section_alignment (htab->elf.dynobj, s))
3727 if (! bfd_set_section_alignment (htab->elf.dynobj, s, power_of_two))
3728 return FALSE;
3731 /* Define the symbol as being at this point in the section. */
3732 h->root.u.def.section = s;
3733 h->root.u.def.value = s->size;
3735 /* Increment the section size to make room for the symbol. */
3736 s->size += h->size;
3738 return TRUE;
3741 /* Allocate space in associated reloc sections for dynamic relocs. */
3743 static bfd_boolean
3744 allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
3746 struct bfd_link_info *info = inf;
3747 struct ppc_elf_link_hash_entry *eh;
3748 struct ppc_elf_link_hash_table *htab;
3749 struct ppc_elf_dyn_relocs *p;
3751 if (h->root.type == bfd_link_hash_indirect)
3752 return TRUE;
3754 if (h->root.type == bfd_link_hash_warning)
3755 /* When warning symbols are created, they **replace** the "real"
3756 entry in the hash table, thus we never get to see the real
3757 symbol in a hash traversal. So look at it now. */
3758 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3760 htab = ppc_elf_hash_table (info);
3761 if (htab->elf.dynamic_sections_created
3762 && h->plt.refcount > 0)
3764 /* Make sure this symbol is output as a dynamic symbol. */
3765 if (h->dynindx == -1
3766 && !h->forced_local)
3768 if (! bfd_elf_link_record_dynamic_symbol (info, h))
3769 return FALSE;
3772 if (info->shared
3773 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h))
3775 asection *s = htab->plt;
3777 /* If this is the first .plt entry, make room for the special
3778 first entry. */
3779 if (s->size == 0)
3780 s->size += PLT_INITIAL_ENTRY_SIZE;
3782 /* The PowerPC PLT is actually composed of two parts, the
3783 first part is 2 words (for a load and a jump), and then
3784 there is a remaining word available at the end. */
3785 h->plt.offset = (PLT_INITIAL_ENTRY_SIZE
3786 + (PLT_SLOT_SIZE
3787 * ((s->size - PLT_INITIAL_ENTRY_SIZE)
3788 / PLT_ENTRY_SIZE)));
3790 /* If this symbol is not defined in a regular file, and we
3791 are not generating a shared library, then set the symbol
3792 to this location in the .plt. This is required to make
3793 function pointers compare as equal between the normal
3794 executable and the shared library. */
3795 if (! info->shared
3796 && !h->def_regular)
3798 h->root.u.def.section = s;
3799 h->root.u.def.value = h->plt.offset;
3802 /* Make room for this entry. After the 8192nd entry, room
3803 for two entries is allocated. */
3804 s->size += PLT_ENTRY_SIZE;
3805 if ((s->size - PLT_INITIAL_ENTRY_SIZE) / PLT_ENTRY_SIZE
3806 > PLT_NUM_SINGLE_ENTRIES)
3807 s->size += PLT_ENTRY_SIZE;
3809 /* We also need to make an entry in the .rela.plt section. */
3810 htab->relplt->size += sizeof (Elf32_External_Rela);
3812 else
3814 h->plt.offset = (bfd_vma) -1;
3815 h->needs_plt = 0;
3818 else
3820 h->plt.offset = (bfd_vma) -1;
3821 h->needs_plt = 0;
3824 eh = (struct ppc_elf_link_hash_entry *) h;
3825 if (eh->elf.got.refcount > 0)
3827 /* Make sure this symbol is output as a dynamic symbol. */
3828 if (eh->elf.dynindx == -1
3829 && !eh->elf.forced_local)
3831 if (!bfd_elf_link_record_dynamic_symbol (info, &eh->elf))
3832 return FALSE;
3835 if (eh->tls_mask == (TLS_TLS | TLS_LD)
3836 && !eh->elf.def_dynamic)
3837 /* If just an LD reloc, we'll just use htab->tlsld_got.offset. */
3838 eh->elf.got.offset = (bfd_vma) -1;
3839 else
3841 bfd_boolean dyn;
3842 eh->elf.got.offset = htab->got->size;
3843 if ((eh->tls_mask & TLS_TLS) != 0)
3845 if ((eh->tls_mask & TLS_LD) != 0)
3846 htab->got->size += 8;
3847 if ((eh->tls_mask & TLS_GD) != 0)
3848 htab->got->size += 8;
3849 if ((eh->tls_mask & (TLS_TPREL | TLS_TPRELGD)) != 0)
3850 htab->got->size += 4;
3851 if ((eh->tls_mask & TLS_DTPREL) != 0)
3852 htab->got->size += 4;
3854 else
3855 htab->got->size += 4;
3856 dyn = htab->elf.dynamic_sections_created;
3857 if ((info->shared
3858 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, &eh->elf))
3859 && (ELF_ST_VISIBILITY (eh->elf.other) == STV_DEFAULT
3860 || eh->elf.root.type != bfd_link_hash_undefweak))
3862 /* All the entries we allocated need relocs. */
3863 htab->relgot->size
3864 += ((htab->got->size - eh->elf.got.offset) / 4
3865 * sizeof (Elf32_External_Rela));
3866 /* Except LD only needs one. */
3867 if ((eh->tls_mask & TLS_LD) != 0)
3868 htab->relgot->size -= sizeof (Elf32_External_Rela);
3872 else
3873 eh->elf.got.offset = (bfd_vma) -1;
3875 if (eh->dyn_relocs == NULL)
3876 return TRUE;
3878 /* In the shared -Bsymbolic case, discard space allocated for
3879 dynamic pc-relative relocs against symbols which turn out to be
3880 defined in regular objects. For the normal shared case, discard
3881 space for relocs that have become local due to symbol visibility
3882 changes. */
3884 if (info->shared)
3886 /* Relocs that use pc_count are those that appear on a call insn,
3887 or certain REL relocs (see MUST_BE_DYN_RELOC) that can be
3888 generated via assembly. We want calls to protected symbols to
3889 resolve directly to the function rather than going via the plt.
3890 If people want function pointer comparisons to work as expected
3891 then they should avoid writing weird assembly. */
3892 if (SYMBOL_CALLS_LOCAL (info, h))
3894 struct ppc_elf_dyn_relocs **pp;
3896 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
3898 p->count -= p->pc_count;
3899 p->pc_count = 0;
3900 if (p->count == 0)
3901 *pp = p->next;
3902 else
3903 pp = &p->next;
3907 /* Also discard relocs on undefined weak syms with non-default
3908 visibility. */
3909 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
3910 && h->root.type == bfd_link_hash_undefweak)
3911 eh->dyn_relocs = NULL;
3913 /* Make sure undefined weak symbols are output as a dynamic symbol
3914 in PIEs. */
3915 if (info->pie
3916 && eh->dyn_relocs != NULL
3917 && h->dynindx == -1
3918 && h->root.type == bfd_link_hash_undefweak
3919 && !h->forced_local)
3921 if (! bfd_elf_link_record_dynamic_symbol (info, h))
3922 return FALSE;
3925 else if (ELIMINATE_COPY_RELOCS)
3927 /* For the non-shared case, discard space for relocs against
3928 symbols which turn out to need copy relocs or are not
3929 dynamic. */
3931 if (!h->non_got_ref
3932 && h->def_dynamic
3933 && !h->def_regular)
3935 /* Make sure this symbol is output as a dynamic symbol.
3936 Undefined weak syms won't yet be marked as dynamic. */
3937 if (h->dynindx == -1
3938 && !h->forced_local)
3940 if (! bfd_elf_link_record_dynamic_symbol (info, h))
3941 return FALSE;
3944 /* If that succeeded, we know we'll be keeping all the
3945 relocs. */
3946 if (h->dynindx != -1)
3947 goto keep;
3950 eh->dyn_relocs = NULL;
3952 keep: ;
3955 /* Finally, allocate space. */
3956 for (p = eh->dyn_relocs; p != NULL; p = p->next)
3958 asection *sreloc = elf_section_data (p->sec)->sreloc;
3959 sreloc->size += p->count * sizeof (Elf32_External_Rela);
3962 return TRUE;
3965 /* Find any dynamic relocs that apply to read-only sections. */
3967 static bfd_boolean
3968 readonly_dynrelocs (struct elf_link_hash_entry *h, void *info)
3970 struct ppc_elf_dyn_relocs *p;
3972 if (h->root.type == bfd_link_hash_indirect)
3973 return TRUE;
3975 if (h->root.type == bfd_link_hash_warning)
3976 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3978 for (p = ppc_elf_hash_entry (h)->dyn_relocs; p != NULL; p = p->next)
3980 asection *s = p->sec->output_section;
3982 if (s != NULL
3983 && ((s->flags & (SEC_READONLY | SEC_ALLOC))
3984 == (SEC_READONLY | SEC_ALLOC)))
3986 ((struct bfd_link_info *) info)->flags |= DF_TEXTREL;
3988 /* Not an error, just cut short the traversal. */
3989 return FALSE;
3992 return TRUE;
3995 /* Set the sizes of the dynamic sections. */
3997 static bfd_boolean
3998 ppc_elf_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
3999 struct bfd_link_info *info)
4001 struct ppc_elf_link_hash_table *htab;
4002 asection *s;
4003 bfd_boolean relocs;
4004 bfd *ibfd;
4006 #ifdef DEBUG
4007 fprintf (stderr, "ppc_elf_size_dynamic_sections called\n");
4008 #endif
4010 htab = ppc_elf_hash_table (info);
4011 BFD_ASSERT (htab->elf.dynobj != NULL);
4013 if (elf_hash_table (info)->dynamic_sections_created)
4015 /* Set the contents of the .interp section to the interpreter. */
4016 if (info->executable)
4018 s = bfd_get_section_by_name (htab->elf.dynobj, ".interp");
4019 BFD_ASSERT (s != NULL);
4020 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
4021 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
4025 if (htab->tlsld_got.refcount > 0)
4027 htab->tlsld_got.offset = htab->got->size;
4028 htab->got->size += 8;
4029 if (info->shared)
4030 htab->relgot->size += sizeof (Elf32_External_Rela);
4032 else
4033 htab->tlsld_got.offset = (bfd_vma) -1;
4035 /* Set up .got offsets for local syms, and space for local dynamic
4036 relocs. */
4037 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
4039 bfd_signed_vma *local_got;
4040 bfd_signed_vma *end_local_got;
4041 char *lgot_masks;
4042 bfd_size_type locsymcount;
4043 Elf_Internal_Shdr *symtab_hdr;
4044 asection *srel;
4046 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
4047 continue;
4049 for (s = ibfd->sections; s != NULL; s = s->next)
4051 struct ppc_elf_dyn_relocs *p;
4053 for (p = ((struct ppc_elf_dyn_relocs *)
4054 elf_section_data (s)->local_dynrel);
4055 p != NULL;
4056 p = p->next)
4058 if (!bfd_is_abs_section (p->sec)
4059 && bfd_is_abs_section (p->sec->output_section))
4061 /* Input section has been discarded, either because
4062 it is a copy of a linkonce section or due to
4063 linker script /DISCARD/, so we'll be discarding
4064 the relocs too. */
4066 else if (p->count != 0)
4068 elf_section_data (p->sec)->sreloc->size
4069 += p->count * sizeof (Elf32_External_Rela);
4070 if ((p->sec->output_section->flags
4071 & (SEC_READONLY | SEC_ALLOC))
4072 == (SEC_READONLY | SEC_ALLOC))
4073 info->flags |= DF_TEXTREL;
4078 local_got = elf_local_got_refcounts (ibfd);
4079 if (!local_got)
4080 continue;
4082 symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
4083 locsymcount = symtab_hdr->sh_info;
4084 end_local_got = local_got + locsymcount;
4085 lgot_masks = (char *) end_local_got;
4086 s = htab->got;
4087 srel = htab->relgot;
4088 for (; local_got < end_local_got; ++local_got, ++lgot_masks)
4089 if (*local_got > 0)
4091 if (*lgot_masks == (TLS_TLS | TLS_LD))
4093 /* If just an LD reloc, we'll just use
4094 htab->tlsld_got.offset. */
4095 if (htab->tlsld_got.offset == (bfd_vma) -1)
4097 htab->tlsld_got.offset = s->size;
4098 s->size += 8;
4099 if (info->shared)
4100 srel->size += sizeof (Elf32_External_Rela);
4102 *local_got = (bfd_vma) -1;
4104 else
4106 *local_got = s->size;
4107 if ((*lgot_masks & TLS_TLS) != 0)
4109 if ((*lgot_masks & TLS_GD) != 0)
4110 s->size += 8;
4111 if ((*lgot_masks & (TLS_TPREL | TLS_TPRELGD)) != 0)
4112 s->size += 4;
4113 if ((*lgot_masks & TLS_DTPREL) != 0)
4114 s->size += 4;
4116 else
4117 s->size += 4;
4118 if (info->shared)
4119 srel->size += ((s->size - *local_got) / 4
4120 * sizeof (Elf32_External_Rela));
4123 else
4124 *local_got = (bfd_vma) -1;
4127 /* Allocate space for global sym dynamic relocs. */
4128 elf_link_hash_traverse (elf_hash_table (info), allocate_dynrelocs, info);
4130 /* We've now determined the sizes of the various dynamic sections.
4131 Allocate memory for them. */
4132 relocs = FALSE;
4133 for (s = htab->elf.dynobj->sections; s != NULL; s = s->next)
4135 if ((s->flags & SEC_LINKER_CREATED) == 0)
4136 continue;
4138 if (s == htab->plt
4139 || s == htab->got)
4141 /* Strip this section if we don't need it; see the
4142 comment below. */
4144 else if ((htab->sdata != NULL && s == htab->sdata->section)
4145 || (htab->sdata2 != NULL && s == htab->sdata2->section))
4147 if (s->size == 0)
4148 /* Don't strip these sections. We need them because they
4149 define _SDA_BASE_ and _SDA2_BASE_. crt1.o makes a
4150 reference to _SDA_BASE_ to set up r13. */
4151 continue;
4153 else if (strncmp (bfd_get_section_name (dynobj, s), ".rela", 5) == 0)
4155 if (s->size == 0)
4157 /* If we don't need this section, strip it from the
4158 output file. This is mostly to handle .rela.bss and
4159 .rela.plt. We must create both sections in
4160 create_dynamic_sections, because they must be created
4161 before the linker maps input sections to output
4162 sections. The linker does that before
4163 adjust_dynamic_symbol is called, and it is that
4164 function which decides whether anything needs to go
4165 into these sections. */
4167 else
4169 /* Remember whether there are any relocation sections. */
4170 relocs = TRUE;
4172 /* We use the reloc_count field as a counter if we need
4173 to copy relocs into the output file. */
4174 s->reloc_count = 0;
4177 else
4179 /* It's not one of our sections, so don't allocate space. */
4180 continue;
4183 if (s->size == 0)
4185 _bfd_strip_section_from_output (info, s);
4186 continue;
4189 /* Allocate memory for the section contents. */
4190 s->contents = bfd_zalloc (htab->elf.dynobj, s->size);
4191 if (s->contents == NULL)
4192 return FALSE;
4195 if (htab->elf.dynamic_sections_created)
4197 /* Add some entries to the .dynamic section. We fill in the
4198 values later, in ppc_elf_finish_dynamic_sections, but we
4199 must add the entries now so that we get the correct size for
4200 the .dynamic section. The DT_DEBUG entry is filled in by the
4201 dynamic linker and used by the debugger. */
4202 #define add_dynamic_entry(TAG, VAL) \
4203 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
4205 if (info->executable)
4207 if (!add_dynamic_entry (DT_DEBUG, 0))
4208 return FALSE;
4211 if (htab->plt != NULL && htab->plt->size != 0)
4213 if (!add_dynamic_entry (DT_PLTGOT, 0)
4214 || !add_dynamic_entry (DT_PLTRELSZ, 0)
4215 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
4216 || !add_dynamic_entry (DT_JMPREL, 0))
4217 return FALSE;
4220 if (relocs)
4222 if (!add_dynamic_entry (DT_RELA, 0)
4223 || !add_dynamic_entry (DT_RELASZ, 0)
4224 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf32_External_Rela)))
4225 return FALSE;
4228 /* If any dynamic relocs apply to a read-only section, then we
4229 need a DT_TEXTREL entry. */
4230 if ((info->flags & DF_TEXTREL) == 0)
4231 elf_link_hash_traverse (elf_hash_table (info), readonly_dynrelocs,
4232 info);
4234 if ((info->flags & DF_TEXTREL) != 0)
4236 if (!add_dynamic_entry (DT_TEXTREL, 0))
4237 return FALSE;
4240 #undef add_dynamic_entry
4242 return TRUE;
4245 #define ARRAY_SIZE(a) (sizeof (a) / sizeof ((a)[0]))
4247 static const int shared_stub_entry[] =
4249 0x7c0802a6, /* mflr 0 */
4250 0x429f0005, /* bcl 20, 31, .Lxxx */
4251 0x7d6802a6, /* mflr 11 */
4252 0x3d6b0000, /* addis 11, 11, (xxx-.Lxxx)@ha */
4253 0x396b0018, /* addi 11, 11, (xxx-.Lxxx)@l */
4254 0x7c0803a6, /* mtlr 0 */
4255 0x7d6903a6, /* mtctr 11 */
4256 0x4e800420, /* bctr */
4259 static const int stub_entry[] =
4261 0x3d600000, /* lis 11,xxx@ha */
4262 0x396b0000, /* addi 11,11,xxx@l */
4263 0x7d6903a6, /* mtctr 11 */
4264 0x4e800420, /* bctr */
4267 static bfd_boolean
4268 ppc_elf_relax_section (bfd *abfd,
4269 asection *isec,
4270 struct bfd_link_info *link_info,
4271 bfd_boolean *again)
4273 struct one_fixup
4275 struct one_fixup *next;
4276 asection *tsec;
4277 bfd_vma toff;
4278 bfd_vma trampoff;
4281 Elf_Internal_Shdr *symtab_hdr;
4282 bfd_byte *contents = NULL;
4283 Elf_Internal_Sym *isymbuf = NULL;
4284 Elf_Internal_Rela *internal_relocs = NULL;
4285 Elf_Internal_Rela *irel, *irelend;
4286 struct one_fixup *fixups = NULL;
4287 bfd_boolean changed;
4288 struct ppc_elf_link_hash_table *ppc_info;
4289 bfd_size_type trampoff;
4291 *again = FALSE;
4293 /* Nothing to do if there are no relocations. */
4294 if ((isec->flags & SEC_RELOC) == 0 || isec->reloc_count == 0)
4295 return TRUE;
4297 trampoff = (isec->size + 3) & (bfd_vma) -4;
4298 /* Space for a branch around any trampolines. */
4299 trampoff += 4;
4301 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
4303 /* Get a copy of the native relocations. */
4304 internal_relocs = _bfd_elf_link_read_relocs (abfd, isec, NULL, NULL,
4305 link_info->keep_memory);
4306 if (internal_relocs == NULL)
4307 goto error_return;
4309 ppc_info = ppc_elf_hash_table (link_info);
4310 irelend = internal_relocs + isec->reloc_count;
4312 for (irel = internal_relocs; irel < irelend; irel++)
4314 unsigned long r_type = ELF32_R_TYPE (irel->r_info);
4315 bfd_vma symaddr, reladdr, toff, roff;
4316 asection *tsec;
4317 struct one_fixup *f;
4318 size_t insn_offset = 0;
4319 bfd_vma max_branch_offset, val;
4320 bfd_byte *hit_addr;
4321 unsigned long t0;
4322 unsigned char sym_type;
4324 switch (r_type)
4326 case R_PPC_REL24:
4327 case R_PPC_LOCAL24PC:
4328 case R_PPC_PLTREL24:
4329 max_branch_offset = 1 << 25;
4330 break;
4332 case R_PPC_REL14:
4333 case R_PPC_REL14_BRTAKEN:
4334 case R_PPC_REL14_BRNTAKEN:
4335 max_branch_offset = 1 << 15;
4336 break;
4338 default:
4339 continue;
4342 /* Get the value of the symbol referred to by the reloc. */
4343 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
4345 /* A local symbol. */
4346 Elf_Internal_Sym *isym;
4348 /* Read this BFD's local symbols. */
4349 if (isymbuf == NULL)
4351 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
4352 if (isymbuf == NULL)
4353 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
4354 symtab_hdr->sh_info, 0,
4355 NULL, NULL, NULL);
4356 if (isymbuf == 0)
4357 goto error_return;
4359 isym = isymbuf + ELF32_R_SYM (irel->r_info);
4360 if (isym->st_shndx == SHN_UNDEF)
4361 continue; /* We can't do anything with undefined symbols. */
4362 else if (isym->st_shndx == SHN_ABS)
4363 tsec = bfd_abs_section_ptr;
4364 else if (isym->st_shndx == SHN_COMMON)
4365 tsec = bfd_com_section_ptr;
4366 else
4367 tsec = bfd_section_from_elf_index (abfd, isym->st_shndx);
4369 toff = isym->st_value;
4370 sym_type = ELF_ST_TYPE (isym->st_info);
4372 else
4374 /* Global symbol handling. */
4375 unsigned long indx;
4376 struct elf_link_hash_entry *h;
4378 indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
4379 h = elf_sym_hashes (abfd)[indx];
4381 while (h->root.type == bfd_link_hash_indirect
4382 || h->root.type == bfd_link_hash_warning)
4383 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4385 if (r_type == R_PPC_PLTREL24
4386 && ppc_info->plt != NULL
4387 && h->plt.offset != (bfd_vma) -1)
4389 tsec = ppc_info->plt;
4390 toff = h->plt.offset;
4392 else if (h->root.type == bfd_link_hash_defined
4393 || h->root.type == bfd_link_hash_defweak)
4395 tsec = h->root.u.def.section;
4396 toff = h->root.u.def.value;
4398 else
4399 continue;
4401 sym_type = h->type;
4404 /* If the branch and target are in the same section, you have
4405 no hope of adding stubs. We'll error out later should the
4406 branch overflow. */
4407 if (tsec == isec)
4408 continue;
4410 /* There probably isn't any reason to handle symbols in
4411 SEC_MERGE sections; SEC_MERGE doesn't seem a likely
4412 attribute for a code section, and we are only looking at
4413 branches. However, implement it correctly here as a
4414 reference for other target relax_section functions. */
4415 if (0 && tsec->sec_info_type == ELF_INFO_TYPE_MERGE)
4417 /* At this stage in linking, no SEC_MERGE symbol has been
4418 adjusted, so all references to such symbols need to be
4419 passed through _bfd_merged_section_offset. (Later, in
4420 relocate_section, all SEC_MERGE symbols *except* for
4421 section symbols have been adjusted.)
4423 gas may reduce relocations against symbols in SEC_MERGE
4424 sections to a relocation against the section symbol when
4425 the original addend was zero. When the reloc is against
4426 a section symbol we should include the addend in the
4427 offset passed to _bfd_merged_section_offset, since the
4428 location of interest is the original symbol. On the
4429 other hand, an access to "sym+addend" where "sym" is not
4430 a section symbol should not include the addend; Such an
4431 access is presumed to be an offset from "sym"; The
4432 location of interest is just "sym". */
4433 if (sym_type == STT_SECTION)
4434 toff += irel->r_addend;
4436 toff = _bfd_merged_section_offset (abfd, &tsec,
4437 elf_section_data (tsec)->sec_info,
4438 toff);
4440 if (sym_type != STT_SECTION)
4441 toff += irel->r_addend;
4443 else
4444 toff += irel->r_addend;
4446 symaddr = tsec->output_section->vma + tsec->output_offset + toff;
4448 roff = irel->r_offset;
4449 reladdr = isec->output_section->vma + isec->output_offset + roff;
4451 /* If the branch is in range, no need to do anything. */
4452 if (symaddr - reladdr + max_branch_offset < 2 * max_branch_offset)
4453 continue;
4455 /* Look for an existing fixup to this address. */
4456 for (f = fixups; f ; f = f->next)
4457 if (f->tsec == tsec && f->toff == toff)
4458 break;
4460 if (f == NULL)
4462 size_t size;
4463 unsigned long stub_rtype;
4465 val = trampoff - roff;
4466 if (val >= max_branch_offset)
4467 /* Oh dear, we can't reach a trampoline. Don't try to add
4468 one. We'll report an error later. */
4469 continue;
4471 if (link_info->shared)
4473 size = 4 * ARRAY_SIZE (shared_stub_entry);
4474 insn_offset = 12;
4475 stub_rtype = R_PPC_RELAX32PC;
4477 else
4479 size = 4 * ARRAY_SIZE (stub_entry);
4480 insn_offset = 0;
4481 stub_rtype = R_PPC_RELAX32;
4484 if (R_PPC_RELAX32_PLT - R_PPC_RELAX32
4485 != R_PPC_RELAX32PC_PLT - R_PPC_RELAX32PC)
4486 abort ();
4487 if (tsec == ppc_info->plt)
4488 stub_rtype += R_PPC_RELAX32_PLT - R_PPC_RELAX32;
4490 /* Hijack the old relocation. Since we need two
4491 relocations for this use a "composite" reloc. */
4492 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
4493 stub_rtype);
4494 irel->r_offset = trampoff + insn_offset;
4496 /* Record the fixup so we don't do it again this section. */
4497 f = bfd_malloc (sizeof (*f));
4498 f->next = fixups;
4499 f->tsec = tsec;
4500 f->toff = toff;
4501 f->trampoff = trampoff;
4502 fixups = f;
4504 trampoff += size;
4506 else
4508 val = f->trampoff - roff;
4509 if (val >= max_branch_offset)
4510 continue;
4512 /* Nop out the reloc, since we're finalizing things here. */
4513 irel->r_info = ELF32_R_INFO (0, R_PPC_NONE);
4516 /* Get the section contents. */
4517 if (contents == NULL)
4519 /* Get cached copy if it exists. */
4520 if (elf_section_data (isec)->this_hdr.contents != NULL)
4521 contents = elf_section_data (isec)->this_hdr.contents;
4522 else
4524 /* Go get them off disk. */
4525 if (!bfd_malloc_and_get_section (abfd, isec, &contents))
4526 goto error_return;
4530 /* Fix up the existing branch to hit the trampoline. */
4531 hit_addr = contents + roff;
4532 switch (r_type)
4534 case R_PPC_REL24:
4535 case R_PPC_LOCAL24PC:
4536 case R_PPC_PLTREL24:
4537 t0 = bfd_get_32 (abfd, hit_addr);
4538 t0 &= ~0x3fffffc;
4539 t0 |= val & 0x3fffffc;
4540 bfd_put_32 (abfd, t0, hit_addr);
4541 break;
4543 case R_PPC_REL14:
4544 case R_PPC_REL14_BRTAKEN:
4545 case R_PPC_REL14_BRNTAKEN:
4546 t0 = bfd_get_32 (abfd, hit_addr);
4547 t0 &= ~0xfffc;
4548 t0 |= val & 0xfffc;
4549 bfd_put_32 (abfd, t0, hit_addr);
4550 break;
4554 /* Write out the trampolines. */
4555 changed = fixups != NULL;
4556 if (fixups != NULL)
4558 const int *stub;
4559 bfd_byte *dest;
4560 bfd_vma val;
4561 int i, size;
4565 struct one_fixup *f = fixups;
4566 fixups = fixups->next;
4567 free (f);
4569 while (fixups);
4571 contents = bfd_realloc (contents, trampoff);
4572 if (contents == NULL)
4573 goto error_return;
4575 isec->size = (isec->size + 3) & (bfd_vma) -4;
4576 /* Branch around the trampolines. */
4577 val = trampoff - isec->size + 0x48000000;
4578 dest = contents + isec->size;
4579 isec->size = trampoff;
4580 bfd_put_32 (abfd, val, dest);
4581 dest += 4;
4583 if (link_info->shared)
4585 stub = shared_stub_entry;
4586 size = ARRAY_SIZE (shared_stub_entry);
4588 else
4590 stub = stub_entry;
4591 size = ARRAY_SIZE (stub_entry);
4594 i = 0;
4595 while (dest < contents + trampoff)
4597 bfd_put_32 (abfd, stub[i], dest);
4598 i++;
4599 if (i == size)
4600 i = 0;
4601 dest += 4;
4603 BFD_ASSERT (i == 0);
4606 if (isymbuf != NULL
4607 && symtab_hdr->contents != (unsigned char *) isymbuf)
4609 if (! link_info->keep_memory)
4610 free (isymbuf);
4611 else
4613 /* Cache the symbols for elf_link_input_bfd. */
4614 symtab_hdr->contents = (unsigned char *) isymbuf;
4618 if (contents != NULL
4619 && elf_section_data (isec)->this_hdr.contents != contents)
4621 if (!changed && !link_info->keep_memory)
4622 free (contents);
4623 else
4625 /* Cache the section contents for elf_link_input_bfd. */
4626 elf_section_data (isec)->this_hdr.contents = contents;
4630 if (elf_section_data (isec)->relocs != internal_relocs)
4632 if (!changed)
4633 free (internal_relocs);
4634 else
4635 elf_section_data (isec)->relocs = internal_relocs;
4638 *again = changed;
4639 return TRUE;
4641 error_return:
4642 if (isymbuf != NULL && (unsigned char *) isymbuf != symtab_hdr->contents)
4643 free (isymbuf);
4644 if (contents != NULL
4645 && elf_section_data (isec)->this_hdr.contents != contents)
4646 free (contents);
4647 if (internal_relocs != NULL
4648 && elf_section_data (isec)->relocs != internal_relocs)
4649 free (internal_relocs);
4650 return FALSE;
4653 #define bfd_put_ptr(BFD, VAL, ADDR) bfd_put_32 (BFD, VAL, ADDR)
4655 /* Fill in the address for a pointer generated in a linker section. */
4657 static bfd_vma
4658 elf_finish_pointer_linker_section (bfd *output_bfd,
4659 bfd *input_bfd,
4660 struct bfd_link_info *info,
4661 elf_linker_section_t *lsect,
4662 struct elf_link_hash_entry *h,
4663 bfd_vma relocation,
4664 const Elf_Internal_Rela *rel,
4665 int relative_reloc)
4667 elf_linker_section_pointers_t *linker_section_ptr;
4669 BFD_ASSERT (lsect != NULL);
4671 if (h != NULL)
4673 /* Handle global symbol. */
4674 struct ppc_elf_link_hash_entry *eh;
4676 eh = (struct ppc_elf_link_hash_entry *) h;
4677 linker_section_ptr
4678 = elf_find_pointer_linker_section (eh->linker_section_pointer,
4679 rel->r_addend,
4680 lsect);
4682 BFD_ASSERT (linker_section_ptr != NULL);
4684 if (! elf_hash_table (info)->dynamic_sections_created
4685 || (info->shared
4686 && info->symbolic
4687 && h->def_regular))
4689 /* This is actually a static link, or it is a
4690 -Bsymbolic link and the symbol is defined
4691 locally. We must initialize this entry in the
4692 global section.
4694 When doing a dynamic link, we create a .rela.<xxx>
4695 relocation entry to initialize the value. This
4696 is done in the finish_dynamic_symbol routine. */
4697 if (!linker_section_ptr->written_address_p)
4699 linker_section_ptr->written_address_p = TRUE;
4700 bfd_put_ptr (output_bfd,
4701 relocation + linker_section_ptr->addend,
4702 (lsect->section->contents
4703 + linker_section_ptr->offset));
4707 else
4709 /* Handle local symbol. */
4710 unsigned long r_symndx = ELF32_R_SYM (rel->r_info);
4711 BFD_ASSERT (elf_local_ptr_offsets (input_bfd) != NULL);
4712 BFD_ASSERT (elf_local_ptr_offsets (input_bfd)[r_symndx] != NULL);
4713 linker_section_ptr = (elf_find_pointer_linker_section
4714 (elf_local_ptr_offsets (input_bfd)[r_symndx],
4715 rel->r_addend,
4716 lsect));
4718 BFD_ASSERT (linker_section_ptr != NULL);
4720 /* Write out pointer if it hasn't been rewritten out before. */
4721 if (!linker_section_ptr->written_address_p)
4723 linker_section_ptr->written_address_p = TRUE;
4724 bfd_put_ptr (output_bfd, relocation + linker_section_ptr->addend,
4725 lsect->section->contents + linker_section_ptr->offset);
4727 if (info->shared)
4729 /* We need to generate a relative reloc for the dynamic
4730 linker. */
4732 asection *srel = lsect->rel_section;
4733 Elf_Internal_Rela outrel[MAX_INT_RELS_PER_EXT_REL];
4734 bfd_byte *erel;
4735 const struct elf_backend_data *bed;
4736 unsigned int i;
4738 BFD_ASSERT (srel != NULL);
4740 bed = get_elf_backend_data (output_bfd);
4741 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
4743 outrel[i].r_offset = (lsect->section->output_section->vma
4744 + lsect->section->output_offset
4745 + linker_section_ptr->offset);
4746 outrel[i].r_info = 0;
4747 outrel[i].r_addend = 0;
4749 outrel[0].r_info = ELF32_R_INFO (0, relative_reloc);
4750 erel = lsect->section->contents;
4751 erel += (elf_section_data (lsect->section)->rel_count++
4752 * sizeof (Elf32_External_Rela));
4753 bfd_elf32_swap_reloca_out (output_bfd, outrel, erel);
4758 relocation = (lsect->section->output_offset
4759 + linker_section_ptr->offset
4760 - lsect->sym_offset);
4762 #ifdef DEBUG
4763 fprintf (stderr,
4764 "Finish pointer in linker section %s, offset = %ld (0x%lx)\n",
4765 lsect->name, (long) relocation, (long) relocation);
4766 #endif
4768 /* Subtract out the addend, because it will get added back in by the normal
4769 processing. */
4770 return relocation - linker_section_ptr->addend;
4773 /* The RELOCATE_SECTION function is called by the ELF backend linker
4774 to handle the relocations for a section.
4776 The relocs are always passed as Rela structures; if the section
4777 actually uses Rel structures, the r_addend field will always be
4778 zero.
4780 This function is responsible for adjust the section contents as
4781 necessary, and (if using Rela relocs and generating a
4782 relocatable output file) adjusting the reloc addend as
4783 necessary.
4785 This function does not have to worry about setting the reloc
4786 address or the reloc symbol index.
4788 LOCAL_SYMS is a pointer to the swapped in local symbols.
4790 LOCAL_SECTIONS is an array giving the section in the input file
4791 corresponding to the st_shndx field of each local symbol.
4793 The global hash table entry for the global symbols can be found
4794 via elf_sym_hashes (input_bfd).
4796 When generating relocatable output, this function must handle
4797 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
4798 going to be the section symbol corresponding to the output
4799 section, which means that the addend must be adjusted
4800 accordingly. */
4802 static bfd_boolean
4803 ppc_elf_relocate_section (bfd *output_bfd,
4804 struct bfd_link_info *info,
4805 bfd *input_bfd,
4806 asection *input_section,
4807 bfd_byte *contents,
4808 Elf_Internal_Rela *relocs,
4809 Elf_Internal_Sym *local_syms,
4810 asection **local_sections)
4812 Elf_Internal_Shdr *symtab_hdr;
4813 struct elf_link_hash_entry **sym_hashes;
4814 struct ppc_elf_link_hash_table *htab;
4815 Elf_Internal_Rela *rel;
4816 Elf_Internal_Rela *relend;
4817 Elf_Internal_Rela outrel;
4818 bfd_byte *loc;
4819 asection *sreloc = NULL;
4820 bfd_vma *local_got_offsets;
4821 bfd_boolean ret = TRUE;
4823 #ifdef DEBUG
4824 _bfd_error_handler ("ppc_elf_relocate_section called for %B section %A, "
4825 "%ld relocations%s",
4826 input_bfd, input_section,
4827 (long) input_section->reloc_count,
4828 (info->relocatable) ? " (relocatable)" : "");
4829 #endif
4831 if (info->relocatable)
4832 return TRUE;
4834 /* Initialize howto table if not already done. */
4835 if (!ppc_elf_howto_table[R_PPC_ADDR32])
4836 ppc_elf_howto_init ();
4838 htab = ppc_elf_hash_table (info);
4839 local_got_offsets = elf_local_got_offsets (input_bfd);
4840 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
4841 sym_hashes = elf_sym_hashes (input_bfd);
4842 rel = relocs;
4843 relend = relocs + input_section->reloc_count;
4844 for (; rel < relend; rel++)
4846 enum elf_ppc_reloc_type r_type;
4847 bfd_vma addend;
4848 bfd_reloc_status_type r;
4849 Elf_Internal_Sym *sym;
4850 asection *sec;
4851 struct elf_link_hash_entry *h;
4852 const char *sym_name;
4853 reloc_howto_type *howto;
4854 unsigned long r_symndx;
4855 bfd_vma relocation;
4856 bfd_vma branch_bit, insn, from;
4857 bfd_boolean unresolved_reloc;
4858 bfd_boolean warned;
4859 unsigned int tls_type, tls_mask, tls_gd;
4861 r_type = ELF32_R_TYPE (rel->r_info);
4862 sym = NULL;
4863 sec = NULL;
4864 h = NULL;
4865 unresolved_reloc = FALSE;
4866 warned = FALSE;
4867 r_symndx = ELF32_R_SYM (rel->r_info);
4869 if (r_symndx < symtab_hdr->sh_info)
4871 sym = local_syms + r_symndx;
4872 sec = local_sections[r_symndx];
4873 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym);
4875 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
4877 else
4879 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
4880 r_symndx, symtab_hdr, sym_hashes,
4881 h, sec, relocation,
4882 unresolved_reloc, warned);
4884 sym_name = h->root.root.string;
4887 /* TLS optimizations. Replace instruction sequences and relocs
4888 based on information we collected in tls_optimize. We edit
4889 RELOCS so that --emit-relocs will output something sensible
4890 for the final instruction stream. */
4891 tls_mask = 0;
4892 tls_gd = 0;
4893 if (IS_PPC_TLS_RELOC (r_type))
4895 if (h != NULL)
4896 tls_mask = ((struct ppc_elf_link_hash_entry *) h)->tls_mask;
4897 else if (local_got_offsets != NULL)
4899 char *lgot_masks;
4900 lgot_masks = (char *) (local_got_offsets + symtab_hdr->sh_info);
4901 tls_mask = lgot_masks[r_symndx];
4905 /* Ensure reloc mapping code below stays sane. */
4906 if ((R_PPC_GOT_TLSLD16 & 3) != (R_PPC_GOT_TLSGD16 & 3)
4907 || (R_PPC_GOT_TLSLD16_LO & 3) != (R_PPC_GOT_TLSGD16_LO & 3)
4908 || (R_PPC_GOT_TLSLD16_HI & 3) != (R_PPC_GOT_TLSGD16_HI & 3)
4909 || (R_PPC_GOT_TLSLD16_HA & 3) != (R_PPC_GOT_TLSGD16_HA & 3)
4910 || (R_PPC_GOT_TLSLD16 & 3) != (R_PPC_GOT_TPREL16 & 3)
4911 || (R_PPC_GOT_TLSLD16_LO & 3) != (R_PPC_GOT_TPREL16_LO & 3)
4912 || (R_PPC_GOT_TLSLD16_HI & 3) != (R_PPC_GOT_TPREL16_HI & 3)
4913 || (R_PPC_GOT_TLSLD16_HA & 3) != (R_PPC_GOT_TPREL16_HA & 3))
4914 abort ();
4915 switch (r_type)
4917 default:
4918 break;
4920 case R_PPC_GOT_TPREL16:
4921 case R_PPC_GOT_TPREL16_LO:
4922 if (tls_mask != 0
4923 && (tls_mask & TLS_TPREL) == 0)
4925 bfd_vma insn;
4926 insn = bfd_get_32 (output_bfd, contents + rel->r_offset - 2);
4927 insn &= 31 << 21;
4928 insn |= 0x3c020000; /* addis 0,2,0 */
4929 bfd_put_32 (output_bfd, insn, contents + rel->r_offset - 2);
4930 r_type = R_PPC_TPREL16_HA;
4931 rel->r_info = ELF32_R_INFO (r_symndx, r_type);
4933 break;
4935 case R_PPC_TLS:
4936 if (tls_mask != 0
4937 && (tls_mask & TLS_TPREL) == 0)
4939 bfd_vma insn, rtra;
4940 insn = bfd_get_32 (output_bfd, contents + rel->r_offset);
4941 if ((insn & ((31 << 26) | (31 << 11)))
4942 == ((31 << 26) | (2 << 11)))
4943 rtra = insn & ((1 << 26) - (1 << 16));
4944 else if ((insn & ((31 << 26) | (31 << 16)))
4945 == ((31 << 26) | (2 << 16)))
4946 rtra = (insn & (31 << 21)) | ((insn & (31 << 11)) << 5);
4947 else
4948 abort ();
4949 if ((insn & ((1 << 11) - (1 << 1))) == 266 << 1)
4950 /* add -> addi. */
4951 insn = 14 << 26;
4952 else if ((insn & (31 << 1)) == 23 << 1
4953 && ((insn & (31 << 6)) < 14 << 6
4954 || ((insn & (31 << 6)) >= 16 << 6
4955 && (insn & (31 << 6)) < 24 << 6)))
4956 /* load and store indexed -> dform. */
4957 insn = (32 | ((insn >> 6) & 31)) << 26;
4958 else if ((insn & (31 << 1)) == 21 << 1
4959 && (insn & (0x1a << 6)) == 0)
4960 /* ldx, ldux, stdx, stdux -> ld, ldu, std, stdu. */
4961 insn = (((58 | ((insn >> 6) & 4)) << 26)
4962 | ((insn >> 6) & 1));
4963 else if ((insn & (31 << 1)) == 21 << 1
4964 && (insn & ((1 << 11) - (1 << 1))) == 341 << 1)
4965 /* lwax -> lwa. */
4966 insn = (58 << 26) | 2;
4967 else
4968 abort ();
4969 insn |= rtra;
4970 bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
4971 r_type = R_PPC_TPREL16_LO;
4972 rel->r_info = ELF32_R_INFO (r_symndx, r_type);
4973 /* Was PPC_TLS which sits on insn boundary, now
4974 PPC_TPREL16_LO which is at insn+2. */
4975 rel->r_offset += 2;
4977 break;
4979 case R_PPC_GOT_TLSGD16_HI:
4980 case R_PPC_GOT_TLSGD16_HA:
4981 tls_gd = TLS_TPRELGD;
4982 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
4983 goto tls_gdld_hi;
4984 break;
4986 case R_PPC_GOT_TLSLD16_HI:
4987 case R_PPC_GOT_TLSLD16_HA:
4988 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
4990 tls_gdld_hi:
4991 if ((tls_mask & tls_gd) != 0)
4992 r_type = (((r_type - (R_PPC_GOT_TLSGD16 & 3)) & 3)
4993 + R_PPC_GOT_TPREL16);
4994 else
4996 bfd_put_32 (output_bfd, NOP, contents + rel->r_offset);
4997 rel->r_offset -= 2;
4998 r_type = R_PPC_NONE;
5000 rel->r_info = ELF32_R_INFO (r_symndx, r_type);
5002 break;
5004 case R_PPC_GOT_TLSGD16:
5005 case R_PPC_GOT_TLSGD16_LO:
5006 tls_gd = TLS_TPRELGD;
5007 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
5008 goto tls_get_addr_check;
5009 break;
5011 case R_PPC_GOT_TLSLD16:
5012 case R_PPC_GOT_TLSLD16_LO:
5013 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
5015 tls_get_addr_check:
5016 if (rel + 1 < relend)
5018 enum elf_ppc_reloc_type r_type2;
5019 unsigned long r_symndx2;
5020 struct elf_link_hash_entry *h2;
5021 bfd_vma insn1, insn2;
5022 bfd_vma offset;
5024 /* The next instruction should be a call to
5025 __tls_get_addr. Peek at the reloc to be sure. */
5026 r_type2 = ELF32_R_TYPE (rel[1].r_info);
5027 r_symndx2 = ELF32_R_SYM (rel[1].r_info);
5028 if (r_symndx2 < symtab_hdr->sh_info
5029 || (r_type2 != R_PPC_REL14
5030 && r_type2 != R_PPC_REL14_BRTAKEN
5031 && r_type2 != R_PPC_REL14_BRNTAKEN
5032 && r_type2 != R_PPC_REL24
5033 && r_type2 != R_PPC_PLTREL24))
5034 break;
5036 h2 = sym_hashes[r_symndx2 - symtab_hdr->sh_info];
5037 while (h2->root.type == bfd_link_hash_indirect
5038 || h2->root.type == bfd_link_hash_warning)
5039 h2 = (struct elf_link_hash_entry *) h2->root.u.i.link;
5040 if (h2 == NULL || h2 != htab->tls_get_addr)
5041 break;
5043 /* OK, it checks out. Replace the call. */
5044 offset = rel[1].r_offset;
5045 insn1 = bfd_get_32 (output_bfd,
5046 contents + rel->r_offset - 2);
5047 if ((tls_mask & tls_gd) != 0)
5049 /* IE */
5050 insn1 &= (1 << 26) - 1;
5051 insn1 |= 32 << 26; /* lwz */
5052 insn2 = 0x7c631214; /* add 3,3,2 */
5053 rel[1].r_info = ELF32_R_INFO (r_symndx2, R_PPC_NONE);
5054 r_type = (((r_type - (R_PPC_GOT_TLSGD16 & 3)) & 3)
5055 + R_PPC_GOT_TPREL16);
5056 rel->r_info = ELF32_R_INFO (r_symndx, r_type);
5058 else
5060 /* LE */
5061 insn1 = 0x3c620000; /* addis 3,2,0 */
5062 insn2 = 0x38630000; /* addi 3,3,0 */
5063 if (tls_gd == 0)
5065 /* Was an LD reloc. */
5066 r_symndx = 0;
5067 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
5068 rel[1].r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
5070 r_type = R_PPC_TPREL16_HA;
5071 rel->r_info = ELF32_R_INFO (r_symndx, r_type);
5072 rel[1].r_info = ELF32_R_INFO (r_symndx,
5073 R_PPC_TPREL16_LO);
5074 rel[1].r_offset += 2;
5076 bfd_put_32 (output_bfd, insn1, contents + rel->r_offset - 2);
5077 bfd_put_32 (output_bfd, insn2, contents + offset);
5078 if (tls_gd == 0)
5080 /* We changed the symbol on an LD reloc. Start over
5081 in order to get h, sym, sec etc. right. */
5082 rel--;
5083 continue;
5087 break;
5090 /* Handle other relocations that tweak non-addend part of insn. */
5091 branch_bit = 0;
5092 switch (r_type)
5094 default:
5095 break;
5097 /* Branch taken prediction relocations. */
5098 case R_PPC_ADDR14_BRTAKEN:
5099 case R_PPC_REL14_BRTAKEN:
5100 branch_bit = BRANCH_PREDICT_BIT;
5101 /* Fall thru */
5103 /* Branch not taken prediction relocations. */
5104 case R_PPC_ADDR14_BRNTAKEN:
5105 case R_PPC_REL14_BRNTAKEN:
5106 insn = bfd_get_32 (output_bfd, contents + rel->r_offset);
5107 insn &= ~BRANCH_PREDICT_BIT;
5108 insn |= branch_bit;
5110 from = (rel->r_offset
5111 + input_section->output_offset
5112 + input_section->output_section->vma);
5114 /* Invert 'y' bit if not the default. */
5115 if ((bfd_signed_vma) (relocation + rel->r_addend - from) < 0)
5116 insn ^= BRANCH_PREDICT_BIT;
5118 bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
5119 break;
5122 addend = rel->r_addend;
5123 tls_type = 0;
5124 howto = NULL;
5125 if (r_type < R_PPC_max)
5126 howto = ppc_elf_howto_table[r_type];
5127 switch (r_type)
5129 default:
5130 (*_bfd_error_handler)
5131 (_("%B: unknown relocation type %d for symbol %s"),
5132 input_bfd, (int) r_type, sym_name);
5134 bfd_set_error (bfd_error_bad_value);
5135 ret = FALSE;
5136 continue;
5138 case R_PPC_NONE:
5139 case R_PPC_TLS:
5140 case R_PPC_EMB_MRKREF:
5141 case R_PPC_GNU_VTINHERIT:
5142 case R_PPC_GNU_VTENTRY:
5143 continue;
5145 /* GOT16 relocations. Like an ADDR16 using the symbol's
5146 address in the GOT as relocation value instead of the
5147 symbol's value itself. Also, create a GOT entry for the
5148 symbol and put the symbol value there. */
5149 case R_PPC_GOT_TLSGD16:
5150 case R_PPC_GOT_TLSGD16_LO:
5151 case R_PPC_GOT_TLSGD16_HI:
5152 case R_PPC_GOT_TLSGD16_HA:
5153 tls_type = TLS_TLS | TLS_GD;
5154 goto dogot;
5156 case R_PPC_GOT_TLSLD16:
5157 case R_PPC_GOT_TLSLD16_LO:
5158 case R_PPC_GOT_TLSLD16_HI:
5159 case R_PPC_GOT_TLSLD16_HA:
5160 tls_type = TLS_TLS | TLS_LD;
5161 goto dogot;
5163 case R_PPC_GOT_TPREL16:
5164 case R_PPC_GOT_TPREL16_LO:
5165 case R_PPC_GOT_TPREL16_HI:
5166 case R_PPC_GOT_TPREL16_HA:
5167 tls_type = TLS_TLS | TLS_TPREL;
5168 goto dogot;
5170 case R_PPC_GOT_DTPREL16:
5171 case R_PPC_GOT_DTPREL16_LO:
5172 case R_PPC_GOT_DTPREL16_HI:
5173 case R_PPC_GOT_DTPREL16_HA:
5174 tls_type = TLS_TLS | TLS_DTPREL;
5175 goto dogot;
5177 case R_PPC_GOT16:
5178 case R_PPC_GOT16_LO:
5179 case R_PPC_GOT16_HI:
5180 case R_PPC_GOT16_HA:
5181 dogot:
5183 /* Relocation is to the entry for this symbol in the global
5184 offset table. */
5185 bfd_vma off;
5186 bfd_vma *offp;
5187 unsigned long indx;
5189 if (htab->got == NULL)
5190 abort ();
5192 indx = 0;
5193 if (tls_type == (TLS_TLS | TLS_LD)
5194 && (h == NULL
5195 || !h->def_dynamic))
5196 offp = &htab->tlsld_got.offset;
5197 else if (h != NULL)
5199 bfd_boolean dyn;
5200 dyn = htab->elf.dynamic_sections_created;
5201 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
5202 || (info->shared
5203 && SYMBOL_REFERENCES_LOCAL (info, h)))
5204 /* This is actually a static link, or it is a
5205 -Bsymbolic link and the symbol is defined
5206 locally, or the symbol was forced to be local
5207 because of a version file. */
5209 else
5211 indx = h->dynindx;
5212 unresolved_reloc = FALSE;
5214 offp = &h->got.offset;
5216 else
5218 if (local_got_offsets == NULL)
5219 abort ();
5220 offp = &local_got_offsets[r_symndx];
5223 /* The offset must always be a multiple of 4. We use the
5224 least significant bit to record whether we have already
5225 processed this entry. */
5226 off = *offp;
5227 if ((off & 1) != 0)
5228 off &= ~1;
5229 else
5231 unsigned int tls_m = (tls_mask
5232 & (TLS_LD | TLS_GD | TLS_DTPREL
5233 | TLS_TPREL | TLS_TPRELGD));
5235 if (offp == &htab->tlsld_got.offset)
5236 tls_m = TLS_LD;
5237 else if (h == NULL
5238 || !h->def_dynamic)
5239 tls_m &= ~TLS_LD;
5241 /* We might have multiple got entries for this sym.
5242 Initialize them all. */
5245 int tls_ty = 0;
5247 if ((tls_m & TLS_LD) != 0)
5249 tls_ty = TLS_TLS | TLS_LD;
5250 tls_m &= ~TLS_LD;
5252 else if ((tls_m & TLS_GD) != 0)
5254 tls_ty = TLS_TLS | TLS_GD;
5255 tls_m &= ~TLS_GD;
5257 else if ((tls_m & TLS_DTPREL) != 0)
5259 tls_ty = TLS_TLS | TLS_DTPREL;
5260 tls_m &= ~TLS_DTPREL;
5262 else if ((tls_m & (TLS_TPREL | TLS_TPRELGD)) != 0)
5264 tls_ty = TLS_TLS | TLS_TPREL;
5265 tls_m = 0;
5268 /* Generate relocs for the dynamic linker. */
5269 if ((info->shared || indx != 0)
5270 && (h == NULL
5271 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
5272 || h->root.type != bfd_link_hash_undefweak))
5274 outrel.r_offset = (htab->got->output_section->vma
5275 + htab->got->output_offset
5276 + off);
5277 outrel.r_addend = 0;
5278 if (tls_ty & (TLS_LD | TLS_GD))
5280 outrel.r_info = ELF32_R_INFO (indx, R_PPC_DTPMOD32);
5281 if (tls_ty == (TLS_TLS | TLS_GD))
5283 loc = htab->relgot->contents;
5284 loc += (htab->relgot->reloc_count++
5285 * sizeof (Elf32_External_Rela));
5286 bfd_elf32_swap_reloca_out (output_bfd,
5287 &outrel, loc);
5288 outrel.r_offset += 4;
5289 outrel.r_info
5290 = ELF32_R_INFO (indx, R_PPC_DTPREL32);
5293 else if (tls_ty == (TLS_TLS | TLS_DTPREL))
5294 outrel.r_info = ELF32_R_INFO (indx, R_PPC_DTPREL32);
5295 else if (tls_ty == (TLS_TLS | TLS_TPREL))
5296 outrel.r_info = ELF32_R_INFO (indx, R_PPC_TPREL32);
5297 else if (indx == 0)
5298 outrel.r_info = ELF32_R_INFO (indx, R_PPC_RELATIVE);
5299 else
5300 outrel.r_info = ELF32_R_INFO (indx, R_PPC_GLOB_DAT);
5301 if (indx == 0)
5303 outrel.r_addend += relocation;
5304 if (tls_ty & (TLS_GD | TLS_DTPREL | TLS_TPREL))
5305 outrel.r_addend -= htab->elf.tls_sec->vma;
5307 loc = htab->relgot->contents;
5308 loc += (htab->relgot->reloc_count++
5309 * sizeof (Elf32_External_Rela));
5310 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
5313 /* Init the .got section contents if we're not
5314 emitting a reloc. */
5315 else
5317 bfd_vma value = relocation;
5319 if (tls_ty == (TLS_TLS | TLS_LD))
5320 value = 1;
5321 else if (tls_ty != 0)
5323 value -= htab->elf.tls_sec->vma + DTP_OFFSET;
5324 if (tls_ty == (TLS_TLS | TLS_TPREL))
5325 value += DTP_OFFSET - TP_OFFSET;
5327 if (tls_ty == (TLS_TLS | TLS_GD))
5329 bfd_put_32 (output_bfd, value,
5330 htab->got->contents + off + 4);
5331 value = 1;
5334 bfd_put_32 (output_bfd, value,
5335 htab->got->contents + off);
5338 off += 4;
5339 if (tls_ty & (TLS_LD | TLS_GD))
5340 off += 4;
5342 while (tls_m != 0);
5344 off = *offp;
5345 *offp = off | 1;
5348 if (off >= (bfd_vma) -2)
5349 abort ();
5351 if ((tls_type & TLS_TLS) != 0)
5353 if (tls_type != (TLS_TLS | TLS_LD))
5355 if ((tls_mask & TLS_LD) != 0
5356 && !(h == NULL
5357 || !h->def_dynamic))
5358 off += 8;
5359 if (tls_type != (TLS_TLS | TLS_GD))
5361 if ((tls_mask & TLS_GD) != 0)
5362 off += 8;
5363 if (tls_type != (TLS_TLS | TLS_DTPREL))
5365 if ((tls_mask & TLS_DTPREL) != 0)
5366 off += 4;
5372 relocation = htab->got->output_offset + off - 4;
5374 /* Addends on got relocations don't make much sense.
5375 x+off@got is actually x@got+off, and since the got is
5376 generated by a hash table traversal, the value in the
5377 got at entry m+n bears little relation to the entry m. */
5378 if (addend != 0)
5379 (*_bfd_error_handler)
5380 (_("%B(%A+0x%lx): non-zero addend on %s reloc against `%s'"),
5381 input_bfd,
5382 input_section,
5383 (long) rel->r_offset,
5384 howto->name,
5385 sym_name);
5387 break;
5389 /* Relocations that need no special processing. */
5390 case R_PPC_LOCAL24PC:
5391 /* It makes no sense to point a local relocation
5392 at a symbol not in this object. */
5393 if (unresolved_reloc)
5395 if (! (*info->callbacks->undefined_symbol) (info,
5396 h->root.root.string,
5397 input_bfd,
5398 input_section,
5399 rel->r_offset,
5400 TRUE))
5401 return FALSE;
5402 continue;
5404 break;
5406 case R_PPC_DTPREL16:
5407 case R_PPC_DTPREL16_LO:
5408 case R_PPC_DTPREL16_HI:
5409 case R_PPC_DTPREL16_HA:
5410 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
5411 break;
5413 /* Relocations that may need to be propagated if this is a shared
5414 object. */
5415 case R_PPC_TPREL16:
5416 case R_PPC_TPREL16_LO:
5417 case R_PPC_TPREL16_HI:
5418 case R_PPC_TPREL16_HA:
5419 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
5420 /* The TPREL16 relocs shouldn't really be used in shared
5421 libs as they will result in DT_TEXTREL being set, but
5422 support them anyway. */
5423 goto dodyn;
5425 case R_PPC_TPREL32:
5426 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
5427 goto dodyn;
5429 case R_PPC_DTPREL32:
5430 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
5431 goto dodyn;
5433 case R_PPC_DTPMOD32:
5434 relocation = 1;
5435 addend = 0;
5436 goto dodyn;
5438 case R_PPC_REL24:
5439 case R_PPC_REL32:
5440 case R_PPC_REL14:
5441 case R_PPC_REL14_BRTAKEN:
5442 case R_PPC_REL14_BRNTAKEN:
5443 /* If these relocations are not to a named symbol, they can be
5444 handled right here, no need to bother the dynamic linker. */
5445 if (SYMBOL_REFERENCES_LOCAL (info, h)
5446 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
5447 break;
5448 /* fall through */
5450 /* Relocations that always need to be propagated if this is a shared
5451 object. */
5452 case R_PPC_ADDR32:
5453 case R_PPC_ADDR24:
5454 case R_PPC_ADDR16:
5455 case R_PPC_ADDR16_LO:
5456 case R_PPC_ADDR16_HI:
5457 case R_PPC_ADDR16_HA:
5458 case R_PPC_ADDR14:
5459 case R_PPC_ADDR14_BRTAKEN:
5460 case R_PPC_ADDR14_BRNTAKEN:
5461 case R_PPC_UADDR32:
5462 case R_PPC_UADDR16:
5463 /* r_symndx will be zero only for relocs against symbols
5464 from removed linkonce sections, or sections discarded by
5465 a linker script. */
5466 dodyn:
5467 if (r_symndx == 0)
5468 break;
5469 /* Fall thru. */
5471 if ((info->shared
5472 && (h == NULL
5473 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
5474 || h->root.type != bfd_link_hash_undefweak)
5475 && (MUST_BE_DYN_RELOC (r_type)
5476 || !SYMBOL_CALLS_LOCAL (info, h)))
5477 || (ELIMINATE_COPY_RELOCS
5478 && !info->shared
5479 && (input_section->flags & SEC_ALLOC) != 0
5480 && h != NULL
5481 && h->dynindx != -1
5482 && !h->non_got_ref
5483 && h->def_dynamic
5484 && !h->def_regular))
5486 int skip;
5488 #ifdef DEBUG
5489 fprintf (stderr, "ppc_elf_relocate_section needs to "
5490 "create relocation for %s\n",
5491 (h && h->root.root.string
5492 ? h->root.root.string : "<unknown>"));
5493 #endif
5495 /* When generating a shared object, these relocations
5496 are copied into the output file to be resolved at run
5497 time. */
5498 if (sreloc == NULL)
5500 const char *name;
5502 name = (bfd_elf_string_from_elf_section
5503 (input_bfd,
5504 elf_elfheader (input_bfd)->e_shstrndx,
5505 elf_section_data (input_section)->rel_hdr.sh_name));
5506 if (name == NULL)
5507 return FALSE;
5509 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
5510 && strcmp (bfd_get_section_name (input_bfd,
5511 input_section),
5512 name + 5) == 0);
5514 sreloc = bfd_get_section_by_name (htab->elf.dynobj, name);
5515 BFD_ASSERT (sreloc != NULL);
5518 skip = 0;
5520 outrel.r_offset =
5521 _bfd_elf_section_offset (output_bfd, info, input_section,
5522 rel->r_offset);
5523 if (outrel.r_offset == (bfd_vma) -1
5524 || outrel.r_offset == (bfd_vma) -2)
5525 skip = (int) outrel.r_offset;
5526 outrel.r_offset += (input_section->output_section->vma
5527 + input_section->output_offset);
5529 if (skip)
5530 memset (&outrel, 0, sizeof outrel);
5531 else if (!SYMBOL_REFERENCES_LOCAL (info, h))
5533 unresolved_reloc = FALSE;
5534 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
5535 outrel.r_addend = rel->r_addend;
5537 else
5539 outrel.r_addend = relocation + rel->r_addend;
5541 if (r_type == R_PPC_ADDR32)
5542 outrel.r_info = ELF32_R_INFO (0, R_PPC_RELATIVE);
5543 else
5545 long indx;
5547 if (bfd_is_abs_section (sec))
5548 indx = 0;
5549 else if (sec == NULL || sec->owner == NULL)
5551 bfd_set_error (bfd_error_bad_value);
5552 return FALSE;
5554 else
5556 asection *osec;
5558 /* We are turning this relocation into one
5559 against a section symbol. It would be
5560 proper to subtract the symbol's value,
5561 osec->vma, from the emitted reloc addend,
5562 but ld.so expects buggy relocs. */
5563 osec = sec->output_section;
5564 indx = elf_section_data (osec)->dynindx;
5565 BFD_ASSERT (indx > 0);
5566 #ifdef DEBUG
5567 if (indx <= 0)
5568 printf ("indx=%d section=%s flags=%08x name=%s\n",
5569 indx, osec->name, osec->flags,
5570 h->root.root.string);
5571 #endif
5574 outrel.r_info = ELF32_R_INFO (indx, r_type);
5578 loc = sreloc->contents;
5579 loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rela);
5580 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
5582 if (skip == -1)
5583 continue;
5585 /* This reloc will be computed at runtime. We clear the memory
5586 so that it contains predictable value. */
5587 if (! skip
5588 && ((input_section->flags & SEC_ALLOC) != 0
5589 || ELF32_R_TYPE (outrel.r_info) != R_PPC_RELATIVE))
5591 relocation = howto->pc_relative ? outrel.r_offset : 0;
5592 addend = 0;
5593 break;
5596 break;
5598 case R_PPC_RELAX32PC_PLT:
5599 case R_PPC_RELAX32_PLT:
5600 BFD_ASSERT (h != NULL
5601 && h->plt.offset != (bfd_vma) -1
5602 && htab->plt != NULL);
5604 relocation = (htab->plt->output_section->vma
5605 + htab->plt->output_offset
5606 + h->plt.offset);
5607 if (r_type == R_PPC_RELAX32_PLT)
5608 goto relax32;
5609 /* Fall thru */
5611 case R_PPC_RELAX32PC:
5612 relocation -= (input_section->output_section->vma
5613 + input_section->output_offset
5614 + rel->r_offset - 4);
5615 /* Fall thru */
5617 case R_PPC_RELAX32:
5618 relax32:
5620 unsigned long t0;
5621 unsigned long t1;
5623 t0 = bfd_get_32 (output_bfd, contents + rel->r_offset);
5624 t1 = bfd_get_32 (output_bfd, contents + rel->r_offset + 4);
5626 /* We're clearing the bits for R_PPC_ADDR16_HA
5627 and R_PPC_ADDR16_LO here. */
5628 t0 &= ~0xffff;
5629 t1 &= ~0xffff;
5631 /* t0 is HA, t1 is LO */
5632 relocation += addend;
5633 t0 |= ((relocation + 0x8000) >> 16) & 0xffff;
5634 t1 |= relocation & 0xffff;
5636 bfd_put_32 (output_bfd, t0, contents + rel->r_offset);
5637 bfd_put_32 (output_bfd, t1, contents + rel->r_offset + 4);
5639 continue;
5641 /* Indirect .sdata relocation. */
5642 case R_PPC_EMB_SDAI16:
5643 BFD_ASSERT (htab->sdata != NULL);
5644 relocation
5645 = elf_finish_pointer_linker_section (output_bfd, input_bfd, info,
5646 htab->sdata, h, relocation,
5647 rel, R_PPC_RELATIVE);
5648 break;
5650 /* Indirect .sdata2 relocation. */
5651 case R_PPC_EMB_SDA2I16:
5652 BFD_ASSERT (htab->sdata2 != NULL);
5653 relocation
5654 = elf_finish_pointer_linker_section (output_bfd, input_bfd, info,
5655 htab->sdata2, h, relocation,
5656 rel, R_PPC_RELATIVE);
5657 break;
5659 /* Handle the TOC16 reloc. We want to use the offset within the .got
5660 section, not the actual VMA. This is appropriate when generating
5661 an embedded ELF object, for which the .got section acts like the
5662 AIX .toc section. */
5663 case R_PPC_TOC16: /* phony GOT16 relocations */
5664 BFD_ASSERT (sec != NULL);
5665 BFD_ASSERT (bfd_is_und_section (sec)
5666 || strcmp (bfd_get_section_name (abfd, sec), ".got") == 0
5667 || strcmp (bfd_get_section_name (abfd, sec), ".cgot") == 0);
5669 addend -= sec->output_section->vma + sec->output_offset + 0x8000;
5670 break;
5672 case R_PPC_PLTREL24:
5673 /* Relocation is to the entry for this symbol in the
5674 procedure linkage table. */
5675 BFD_ASSERT (h != NULL);
5677 if (h->plt.offset == (bfd_vma) -1
5678 || htab->plt == NULL)
5680 /* We didn't make a PLT entry for this symbol. This
5681 happens when statically linking PIC code, or when
5682 using -Bsymbolic. */
5683 break;
5686 unresolved_reloc = FALSE;
5687 relocation = (htab->plt->output_section->vma
5688 + htab->plt->output_offset
5689 + h->plt.offset);
5690 break;
5692 /* Relocate against _SDA_BASE_. */
5693 case R_PPC_SDAREL16:
5695 const char *name;
5696 const struct elf_link_hash_entry *sh;
5698 BFD_ASSERT (sec != NULL);
5699 name = bfd_get_section_name (abfd, sec->output_section);
5700 if (! ((strncmp (name, ".sdata", 6) == 0
5701 && (name[6] == 0 || name[6] == '.'))
5702 || (strncmp (name, ".sbss", 5) == 0
5703 && (name[5] == 0 || name[5] == '.'))))
5705 (*_bfd_error_handler)
5706 (_("%B: the target (%s) of a %s relocation is "
5707 "in the wrong output section (%s)"),
5708 input_bfd,
5709 sym_name,
5710 howto->name,
5711 name);
5713 sh = htab->sdata->sym_hash;
5714 addend -= (sh->root.u.def.value
5715 + sh->root.u.def.section->output_section->vma
5716 + sh->root.u.def.section->output_offset);
5718 break;
5720 /* Relocate against _SDA2_BASE_. */
5721 case R_PPC_EMB_SDA2REL:
5723 const char *name;
5724 const struct elf_link_hash_entry *sh;
5726 BFD_ASSERT (sec != NULL);
5727 name = bfd_get_section_name (abfd, sec->output_section);
5728 if (! (strncmp (name, ".sdata2", 7) == 0
5729 || strncmp (name, ".sbss2", 6) == 0))
5731 (*_bfd_error_handler)
5732 (_("%B: the target (%s) of a %s relocation is "
5733 "in the wrong output section (%s)"),
5734 input_bfd,
5735 sym_name,
5736 howto->name,
5737 name);
5739 bfd_set_error (bfd_error_bad_value);
5740 ret = FALSE;
5741 continue;
5743 sh = htab->sdata2->sym_hash;
5744 addend -= (sh->root.u.def.value
5745 + sh->root.u.def.section->output_section->vma
5746 + sh->root.u.def.section->output_offset);
5748 break;
5750 /* Relocate against either _SDA_BASE_, _SDA2_BASE_, or 0. */
5751 case R_PPC_EMB_SDA21:
5752 case R_PPC_EMB_RELSDA:
5754 const char *name;
5755 const struct elf_link_hash_entry *sh;
5756 int reg;
5758 BFD_ASSERT (sec != NULL);
5759 name = bfd_get_section_name (abfd, sec->output_section);
5760 if (((strncmp (name, ".sdata", 6) == 0
5761 && (name[6] == 0 || name[6] == '.'))
5762 || (strncmp (name, ".sbss", 5) == 0
5763 && (name[5] == 0 || name[5] == '.'))))
5765 reg = 13;
5766 sh = htab->sdata->sym_hash;
5767 addend -= (sh->root.u.def.value
5768 + sh->root.u.def.section->output_section->vma
5769 + sh->root.u.def.section->output_offset);
5772 else if (strncmp (name, ".sdata2", 7) == 0
5773 || strncmp (name, ".sbss2", 6) == 0)
5775 reg = 2;
5776 sh = htab->sdata2->sym_hash;
5777 addend -= (sh->root.u.def.value
5778 + sh->root.u.def.section->output_section->vma
5779 + sh->root.u.def.section->output_offset);
5782 else if (strcmp (name, ".PPC.EMB.sdata0") == 0
5783 || strcmp (name, ".PPC.EMB.sbss0") == 0)
5785 reg = 0;
5788 else
5790 (*_bfd_error_handler)
5791 (_("%B: the target (%s) of a %s relocation is "
5792 "in the wrong output section (%s)"),
5793 input_bfd,
5794 sym_name,
5795 howto->name,
5796 name);
5798 bfd_set_error (bfd_error_bad_value);
5799 ret = FALSE;
5800 continue;
5803 if (r_type == R_PPC_EMB_SDA21)
5804 { /* fill in register field */
5805 insn = bfd_get_32 (output_bfd, contents + rel->r_offset);
5806 insn = (insn & ~RA_REGISTER_MASK) | (reg << RA_REGISTER_SHIFT);
5807 bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
5810 break;
5812 /* Relocate against the beginning of the section. */
5813 case R_PPC_SECTOFF:
5814 case R_PPC_SECTOFF_LO:
5815 case R_PPC_SECTOFF_HI:
5816 case R_PPC_SECTOFF_HA:
5817 BFD_ASSERT (sec != NULL);
5818 addend -= sec->output_section->vma;
5819 break;
5821 /* Negative relocations. */
5822 case R_PPC_EMB_NADDR32:
5823 case R_PPC_EMB_NADDR16:
5824 case R_PPC_EMB_NADDR16_LO:
5825 case R_PPC_EMB_NADDR16_HI:
5826 case R_PPC_EMB_NADDR16_HA:
5827 addend -= 2 * relocation;
5828 break;
5830 case R_PPC_COPY:
5831 case R_PPC_GLOB_DAT:
5832 case R_PPC_JMP_SLOT:
5833 case R_PPC_RELATIVE:
5834 case R_PPC_PLT32:
5835 case R_PPC_PLTREL32:
5836 case R_PPC_PLT16_LO:
5837 case R_PPC_PLT16_HI:
5838 case R_PPC_PLT16_HA:
5839 case R_PPC_ADDR30:
5840 case R_PPC_EMB_RELSEC16:
5841 case R_PPC_EMB_RELST_LO:
5842 case R_PPC_EMB_RELST_HI:
5843 case R_PPC_EMB_RELST_HA:
5844 case R_PPC_EMB_BIT_FLD:
5845 (*_bfd_error_handler)
5846 (_("%B: relocation %s is not yet supported for symbol %s."),
5847 input_bfd,
5848 howto->name,
5849 sym_name);
5851 bfd_set_error (bfd_error_invalid_operation);
5852 ret = FALSE;
5853 continue;
5856 /* Do any further special processing. */
5857 switch (r_type)
5859 default:
5860 break;
5862 case R_PPC_ADDR16_HA:
5863 case R_PPC_GOT16_HA:
5864 case R_PPC_PLT16_HA:
5865 case R_PPC_SECTOFF_HA:
5866 case R_PPC_TPREL16_HA:
5867 case R_PPC_DTPREL16_HA:
5868 case R_PPC_GOT_TLSGD16_HA:
5869 case R_PPC_GOT_TLSLD16_HA:
5870 case R_PPC_GOT_TPREL16_HA:
5871 case R_PPC_GOT_DTPREL16_HA:
5872 case R_PPC_EMB_NADDR16_HA:
5873 case R_PPC_EMB_RELST_HA:
5874 /* It's just possible that this symbol is a weak symbol
5875 that's not actually defined anywhere. In that case,
5876 'sec' would be NULL, and we should leave the symbol
5877 alone (it will be set to zero elsewhere in the link). */
5878 if (sec != NULL)
5879 /* Add 0x10000 if sign bit in 0:15 is set.
5880 Bits 0:15 are not used. */
5881 addend += 0x8000;
5882 break;
5885 #ifdef DEBUG
5886 fprintf (stderr, "\ttype = %s (%d), name = %s, symbol index = %ld, "
5887 "offset = %ld, addend = %ld\n",
5888 howto->name,
5889 (int) r_type,
5890 sym_name,
5891 r_symndx,
5892 (long) rel->r_offset,
5893 (long) addend);
5894 #endif
5896 if (unresolved_reloc
5897 && !((input_section->flags & SEC_DEBUGGING) != 0
5898 && h->def_dynamic))
5900 (*_bfd_error_handler)
5901 (_("%B(%A+0x%lx): unresolvable %s relocation against symbol `%s'"),
5902 input_bfd,
5903 input_section,
5904 (long) rel->r_offset,
5905 howto->name,
5906 sym_name);
5907 ret = FALSE;
5910 r = _bfd_final_link_relocate (howto,
5911 input_bfd,
5912 input_section,
5913 contents,
5914 rel->r_offset,
5915 relocation,
5916 addend);
5918 if (r != bfd_reloc_ok)
5920 if (sym_name == NULL)
5921 sym_name = "(null)";
5922 if (r == bfd_reloc_overflow)
5924 if (warned)
5925 continue;
5926 if (h != NULL
5927 && h->root.type == bfd_link_hash_undefweak
5928 && howto->pc_relative)
5930 /* Assume this is a call protected by other code that
5931 detect the symbol is undefined. If this is the case,
5932 we can safely ignore the overflow. If not, the
5933 program is hosed anyway, and a little warning isn't
5934 going to help. */
5936 continue;
5939 if (! (*info->callbacks->reloc_overflow) (info,
5940 (h ? &h->root : NULL),
5941 sym_name,
5942 howto->name,
5943 rel->r_addend,
5944 input_bfd,
5945 input_section,
5946 rel->r_offset))
5947 return FALSE;
5949 else
5951 (*_bfd_error_handler)
5952 (_("%B(%A+0x%lx): %s reloc against `%s': error %d"),
5953 input_bfd, input_section,
5954 (long) rel->r_offset, howto->name, sym_name, (int) r);
5955 ret = FALSE;
5960 #ifdef DEBUG
5961 fprintf (stderr, "\n");
5962 #endif
5964 return ret;
5967 /* Finish up dynamic symbol handling. We set the contents of various
5968 dynamic sections here. */
5970 static bfd_boolean
5971 ppc_elf_finish_dynamic_symbol (bfd *output_bfd,
5972 struct bfd_link_info *info,
5973 struct elf_link_hash_entry *h,
5974 Elf_Internal_Sym *sym)
5976 struct ppc_elf_link_hash_table *htab;
5978 #ifdef DEBUG
5979 fprintf (stderr, "ppc_elf_finish_dynamic_symbol called for %s",
5980 h->root.root.string);
5981 #endif
5983 htab = ppc_elf_hash_table (info);
5984 BFD_ASSERT (htab->elf.dynobj != NULL);
5986 if (h->plt.offset != (bfd_vma) -1)
5988 Elf_Internal_Rela rela;
5989 bfd_byte *loc;
5990 bfd_vma reloc_index;
5992 #ifdef DEBUG
5993 fprintf (stderr, ", plt_offset = %d", h->plt.offset);
5994 #endif
5996 /* This symbol has an entry in the procedure linkage table. Set
5997 it up. */
5999 BFD_ASSERT (h->dynindx != -1);
6000 BFD_ASSERT (htab->plt != NULL && htab->relplt != NULL);
6002 /* We don't need to fill in the .plt. The ppc dynamic linker
6003 will fill it in. */
6005 /* Fill in the entry in the .rela.plt section. */
6006 rela.r_offset = (htab->plt->output_section->vma
6007 + htab->plt->output_offset
6008 + h->plt.offset);
6009 rela.r_info = ELF32_R_INFO (h->dynindx, R_PPC_JMP_SLOT);
6010 rela.r_addend = 0;
6012 reloc_index = (h->plt.offset - PLT_INITIAL_ENTRY_SIZE) / PLT_SLOT_SIZE;
6013 if (reloc_index > PLT_NUM_SINGLE_ENTRIES)
6014 reloc_index -= (reloc_index - PLT_NUM_SINGLE_ENTRIES) / 2;
6015 loc = (htab->relplt->contents
6016 + reloc_index * sizeof (Elf32_External_Rela));
6017 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
6019 if (!h->def_regular)
6021 /* Mark the symbol as undefined, rather than as defined in
6022 the .plt section. Leave the value alone. */
6023 sym->st_shndx = SHN_UNDEF;
6024 /* If the symbol is weak, we do need to clear the value.
6025 Otherwise, the PLT entry would provide a definition for
6026 the symbol even if the symbol wasn't defined anywhere,
6027 and so the symbol would never be NULL. */
6028 if (!h->ref_regular_nonweak)
6029 sym->st_value = 0;
6033 if (h->needs_copy)
6035 asection *s;
6036 Elf_Internal_Rela rela;
6037 bfd_byte *loc;
6039 /* This symbols needs a copy reloc. Set it up. */
6041 #ifdef DEBUG
6042 fprintf (stderr, ", copy");
6043 #endif
6045 BFD_ASSERT (h->dynindx != -1);
6047 if (h->size <= elf_gp_size (htab->elf.dynobj))
6048 s = htab->relsbss;
6049 else
6050 s = htab->relbss;
6051 BFD_ASSERT (s != NULL);
6053 rela.r_offset = (h->root.u.def.value
6054 + h->root.u.def.section->output_section->vma
6055 + h->root.u.def.section->output_offset);
6056 rela.r_info = ELF32_R_INFO (h->dynindx, R_PPC_COPY);
6057 rela.r_addend = 0;
6058 loc = s->contents + s->reloc_count++ * sizeof (Elf32_External_Rela);
6059 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
6062 #ifdef DEBUG
6063 fprintf (stderr, "\n");
6064 #endif
6066 /* Mark some specially defined symbols as absolute. */
6067 if (strcmp (h->root.root.string, "_DYNAMIC") == 0
6068 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0
6069 || strcmp (h->root.root.string, "_PROCEDURE_LINKAGE_TABLE_") == 0)
6070 sym->st_shndx = SHN_ABS;
6072 return TRUE;
6075 static enum elf_reloc_type_class
6076 ppc_elf_reloc_type_class (const Elf_Internal_Rela *rela)
6078 switch (ELF32_R_TYPE (rela->r_info))
6080 case R_PPC_RELATIVE:
6081 return reloc_class_relative;
6082 case R_PPC_REL24:
6083 case R_PPC_ADDR24:
6084 case R_PPC_JMP_SLOT:
6085 return reloc_class_plt;
6086 case R_PPC_COPY:
6087 return reloc_class_copy;
6088 default:
6089 return reloc_class_normal;
6093 /* Finish up the dynamic sections. */
6095 static bfd_boolean
6096 ppc_elf_finish_dynamic_sections (bfd *output_bfd,
6097 struct bfd_link_info *info)
6099 asection *sdyn;
6100 struct ppc_elf_link_hash_table *htab;
6102 #ifdef DEBUG
6103 fprintf (stderr, "ppc_elf_finish_dynamic_sections called\n");
6104 #endif
6106 htab = ppc_elf_hash_table (info);
6107 sdyn = bfd_get_section_by_name (htab->elf.dynobj, ".dynamic");
6109 if (htab->elf.dynamic_sections_created)
6111 Elf32_External_Dyn *dyncon, *dynconend;
6113 BFD_ASSERT (htab->plt != NULL && sdyn != NULL);
6115 dyncon = (Elf32_External_Dyn *) sdyn->contents;
6116 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
6117 for (; dyncon < dynconend; dyncon++)
6119 Elf_Internal_Dyn dyn;
6120 asection *s;
6122 bfd_elf32_swap_dyn_in (htab->elf.dynobj, dyncon, &dyn);
6124 switch (dyn.d_tag)
6126 case DT_PLTGOT:
6127 s = htab->plt;
6128 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
6129 break;
6131 case DT_PLTRELSZ:
6132 dyn.d_un.d_val = htab->relplt->size;
6133 break;
6135 case DT_JMPREL:
6136 s = htab->relplt;
6137 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
6138 break;
6140 default:
6141 continue;
6144 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
6148 /* Add a blrl instruction at _GLOBAL_OFFSET_TABLE_-4 so that a function can
6149 easily find the address of the _GLOBAL_OFFSET_TABLE_. */
6150 if (htab->got)
6152 unsigned char *contents = htab->got->contents;
6153 bfd_put_32 (output_bfd, 0x4e800021 /* blrl */, contents);
6155 if (sdyn == NULL)
6156 bfd_put_32 (output_bfd, 0, contents + 4);
6157 else
6158 bfd_put_32 (output_bfd,
6159 sdyn->output_section->vma + sdyn->output_offset,
6160 contents + 4);
6162 elf_section_data (htab->got->output_section)->this_hdr.sh_entsize = 4;
6165 return TRUE;
6168 #define TARGET_LITTLE_SYM bfd_elf32_powerpcle_vec
6169 #define TARGET_LITTLE_NAME "elf32-powerpcle"
6170 #define TARGET_BIG_SYM bfd_elf32_powerpc_vec
6171 #define TARGET_BIG_NAME "elf32-powerpc"
6172 #define ELF_ARCH bfd_arch_powerpc
6173 #define ELF_MACHINE_CODE EM_PPC
6174 #ifdef __QNXTARGET__
6175 #define ELF_MAXPAGESIZE 0x1000
6176 #else
6177 #define ELF_MAXPAGESIZE 0x10000
6178 #endif
6179 #define ELF_MINPAGESIZE 0x1000
6180 #define elf_info_to_howto ppc_elf_info_to_howto
6182 #ifdef EM_CYGNUS_POWERPC
6183 #define ELF_MACHINE_ALT1 EM_CYGNUS_POWERPC
6184 #endif
6186 #ifdef EM_PPC_OLD
6187 #define ELF_MACHINE_ALT2 EM_PPC_OLD
6188 #endif
6190 #define elf_backend_plt_not_loaded 1
6191 #define elf_backend_got_symbol_offset 4
6192 #define elf_backend_can_gc_sections 1
6193 #define elf_backend_can_refcount 1
6194 #define elf_backend_got_header_size 12
6195 #define elf_backend_rela_normal 1
6197 #define bfd_elf32_mkobject ppc_elf_mkobject
6198 #define bfd_elf32_bfd_merge_private_bfd_data ppc_elf_merge_private_bfd_data
6199 #define bfd_elf32_bfd_relax_section ppc_elf_relax_section
6200 #define bfd_elf32_bfd_reloc_type_lookup ppc_elf_reloc_type_lookup
6201 #define bfd_elf32_bfd_set_private_flags ppc_elf_set_private_flags
6202 #define bfd_elf32_bfd_link_hash_table_create ppc_elf_link_hash_table_create
6204 #define elf_backend_object_p ppc_elf_object_p
6205 #define elf_backend_gc_mark_hook ppc_elf_gc_mark_hook
6206 #define elf_backend_gc_sweep_hook ppc_elf_gc_sweep_hook
6207 #define elf_backend_section_from_shdr ppc_elf_section_from_shdr
6208 #define elf_backend_relocate_section ppc_elf_relocate_section
6209 #define elf_backend_create_dynamic_sections ppc_elf_create_dynamic_sections
6210 #define elf_backend_check_relocs ppc_elf_check_relocs
6211 #define elf_backend_copy_indirect_symbol ppc_elf_copy_indirect_symbol
6212 #define elf_backend_adjust_dynamic_symbol ppc_elf_adjust_dynamic_symbol
6213 #define elf_backend_add_symbol_hook ppc_elf_add_symbol_hook
6214 #define elf_backend_size_dynamic_sections ppc_elf_size_dynamic_sections
6215 #define elf_backend_finish_dynamic_symbol ppc_elf_finish_dynamic_symbol
6216 #define elf_backend_finish_dynamic_sections ppc_elf_finish_dynamic_sections
6217 #define elf_backend_fake_sections ppc_elf_fake_sections
6218 #define elf_backend_additional_program_headers ppc_elf_additional_program_headers
6219 #define elf_backend_grok_prstatus ppc_elf_grok_prstatus
6220 #define elf_backend_grok_psinfo ppc_elf_grok_psinfo
6221 #define elf_backend_reloc_type_class ppc_elf_reloc_type_class
6222 #define elf_backend_begin_write_processing ppc_elf_begin_write_processing
6223 #define elf_backend_final_write_processing ppc_elf_final_write_processing
6224 #define elf_backend_write_section ppc_elf_write_section
6225 #define elf_backend_special_sections ppc_elf_special_sections
6226 #define elf_backend_plt_sym_val ppc_elf_plt_sym_val
6228 #include "elf32-target.h"