Set BFD_VERSION_DATE to 20040722.
[binutils.git] / bfd / elf32-ppc.c
blobe53c47d4a241f99b2a93d0892b899ccd8d0b6f58
1 /* PowerPC-specific support for 32-bit ELF
2 Copyright 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
3 2004 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
73 /* Enumeration to specify the special section. */
74 enum elf_linker_section_enum
76 LINKER_SECTION_SDATA,
77 LINKER_SECTION_SDATA2
80 /* Sections created by the linker. */
82 typedef struct elf_linker_section
84 /* pointer to the section */
85 asection *section;
86 /* pointer to the relocations needed for this section */
87 asection *rel_section;
88 /* pointer to the created symbol hash value */
89 struct elf_link_hash_entry *sym_hash;
90 /* offset of symbol from beginning of section */
91 bfd_vma sym_offset;
92 } elf_linker_section_t;
94 /* Linked list of allocated pointer entries. This hangs off of the
95 symbol lists, and provides allows us to return different pointers,
96 based on different addend's. */
98 typedef struct elf_linker_section_pointers
100 /* next allocated pointer for this symbol */
101 struct elf_linker_section_pointers *next;
102 /* offset of pointer from beginning of section */
103 bfd_vma offset;
104 /* addend used */
105 bfd_vma addend;
106 /* which linker section this is */
107 elf_linker_section_t *lsect;
108 /* whether address was written yet */
109 bfd_boolean written_address_p;
110 } elf_linker_section_pointers_t;
112 struct ppc_elf_obj_tdata
114 struct elf_obj_tdata elf;
116 /* A mapping from local symbols to offsets into the various linker
117 sections added. This is index by the symbol index. */
118 elf_linker_section_pointers_t **linker_section_pointers;
121 #define ppc_elf_tdata(bfd) \
122 ((struct ppc_elf_obj_tdata *) (bfd)->tdata.any)
124 #define elf_local_ptr_offsets(bfd) \
125 (ppc_elf_tdata (bfd)->linker_section_pointers)
127 /* Override the generic function because we store some extras. */
129 static bfd_boolean
130 ppc_elf_mkobject (bfd *abfd)
132 bfd_size_type amt = sizeof (struct ppc_elf_obj_tdata);
133 abfd->tdata.any = bfd_zalloc (abfd, amt);
134 if (abfd->tdata.any == NULL)
135 return FALSE;
136 return TRUE;
139 /* The PPC linker needs to keep track of the number of relocs that it
140 decides to copy as dynamic relocs in check_relocs for each symbol.
141 This is so that it can later discard them if they are found to be
142 unnecessary. We store the information in a field extending the
143 regular ELF linker hash table. */
145 struct ppc_elf_dyn_relocs
147 struct ppc_elf_dyn_relocs *next;
149 /* The input section of the reloc. */
150 asection *sec;
152 /* Total number of relocs copied for the input section. */
153 bfd_size_type count;
155 /* Number of pc-relative relocs copied for the input section. */
156 bfd_size_type pc_count;
159 /* PPC ELF linker hash entry. */
161 struct ppc_elf_link_hash_entry
163 struct elf_link_hash_entry elf;
165 /* If this symbol is used in the linker created sections, the processor
166 specific backend uses this field to map the field into the offset
167 from the beginning of the section. */
168 elf_linker_section_pointers_t *linker_section_pointer;
170 /* Track dynamic relocs copied for this symbol. */
171 struct ppc_elf_dyn_relocs *dyn_relocs;
173 /* Contexts in which symbol is used in the GOT (or TOC).
174 TLS_GD .. TLS_TLS bits are or'd into the mask as the
175 corresponding relocs are encountered during check_relocs.
176 tls_optimize clears TLS_GD .. TLS_TPREL when optimizing to
177 indicate the corresponding GOT entry type is not needed. */
178 #define TLS_GD 1 /* GD reloc. */
179 #define TLS_LD 2 /* LD reloc. */
180 #define TLS_TPREL 4 /* TPREL reloc, => IE. */
181 #define TLS_DTPREL 8 /* DTPREL reloc, => LD. */
182 #define TLS_TLS 16 /* Any TLS reloc. */
183 #define TLS_TPRELGD 32 /* TPREL reloc resulting from GD->IE. */
184 char tls_mask;
187 #define ppc_elf_hash_entry(ent) ((struct ppc_elf_link_hash_entry *) (ent))
189 /* PPC ELF linker hash table. */
191 struct ppc_elf_link_hash_table
193 struct elf_link_hash_table elf;
195 /* Short-cuts to get to dynamic linker sections. */
196 asection *got;
197 asection *relgot;
198 asection *plt;
199 asection *relplt;
200 asection *dynbss;
201 asection *relbss;
202 asection *dynsbss;
203 asection *relsbss;
204 elf_linker_section_t *sdata;
205 elf_linker_section_t *sdata2;
206 asection *sbss;
208 /* Shortcut to .__tls_get_addr. */
209 struct elf_link_hash_entry *tls_get_addr;
211 /* TLS local dynamic got entry handling. */
212 union {
213 bfd_signed_vma refcount;
214 bfd_vma offset;
215 } tlsld_got;
217 /* Small local sym to section mapping cache. */
218 struct sym_sec_cache sym_sec;
221 /* Get the PPC ELF linker hash table from a link_info structure. */
223 #define ppc_elf_hash_table(p) \
224 ((struct ppc_elf_link_hash_table *) (p)->hash)
226 /* Create an entry in a PPC ELF linker hash table. */
228 static struct bfd_hash_entry *
229 ppc_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
230 struct bfd_hash_table *table,
231 const char *string)
233 /* Allocate the structure if it has not already been allocated by a
234 subclass. */
235 if (entry == NULL)
237 entry = bfd_hash_allocate (table,
238 sizeof (struct ppc_elf_link_hash_entry));
239 if (entry == NULL)
240 return entry;
243 /* Call the allocation method of the superclass. */
244 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
245 if (entry != NULL)
247 ppc_elf_hash_entry (entry)->linker_section_pointer = NULL;
248 ppc_elf_hash_entry (entry)->dyn_relocs = NULL;
249 ppc_elf_hash_entry (entry)->tls_mask = 0;
252 return entry;
255 /* Create a PPC ELF linker hash table. */
257 static struct bfd_link_hash_table *
258 ppc_elf_link_hash_table_create (bfd *abfd)
260 struct ppc_elf_link_hash_table *ret;
262 ret = bfd_zmalloc (sizeof (struct ppc_elf_link_hash_table));
263 if (ret == NULL)
264 return NULL;
266 if (! _bfd_elf_link_hash_table_init (&ret->elf, abfd,
267 ppc_elf_link_hash_newfunc))
269 free (ret);
270 return NULL;
273 return &ret->elf.root;
276 /* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
277 copying dynamic variables from a shared lib into an app's dynbss
278 section, and instead use a dynamic relocation to point into the
279 shared lib. */
280 #define ELIMINATE_COPY_RELOCS 1
282 /* Copy the extra info we tack onto an elf_link_hash_entry. */
284 static void
285 ppc_elf_copy_indirect_symbol (const struct elf_backend_data *bed,
286 struct elf_link_hash_entry *dir,
287 struct elf_link_hash_entry *ind)
289 struct ppc_elf_link_hash_entry *edir, *eind;
291 edir = (struct ppc_elf_link_hash_entry *) dir;
292 eind = (struct ppc_elf_link_hash_entry *) ind;
294 if (eind->dyn_relocs != NULL)
296 if (edir->dyn_relocs != NULL)
298 struct ppc_elf_dyn_relocs **pp;
299 struct ppc_elf_dyn_relocs *p;
301 if (ind->root.type == bfd_link_hash_indirect)
302 abort ();
304 /* Add reloc counts against the weak sym to the strong sym
305 list. Merge any entries against the same section. */
306 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
308 struct ppc_elf_dyn_relocs *q;
310 for (q = edir->dyn_relocs; q != NULL; q = q->next)
311 if (q->sec == p->sec)
313 q->pc_count += p->pc_count;
314 q->count += p->count;
315 *pp = p->next;
316 break;
318 if (q == NULL)
319 pp = &p->next;
321 *pp = edir->dyn_relocs;
324 edir->dyn_relocs = eind->dyn_relocs;
325 eind->dyn_relocs = NULL;
328 edir->tls_mask |= eind->tls_mask;
330 if (ELIMINATE_COPY_RELOCS
331 && ind->root.type != bfd_link_hash_indirect
332 && (dir->elf_link_hash_flags & ELF_LINK_HASH_DYNAMIC_ADJUSTED) != 0)
333 /* If called to transfer flags for a weakdef during processing
334 of elf_adjust_dynamic_symbol, don't copy ELF_LINK_NON_GOT_REF.
335 We clear it ourselves for ELIMINATE_COPY_RELOCS. */
336 dir->elf_link_hash_flags |=
337 (ind->elf_link_hash_flags & (ELF_LINK_HASH_REF_DYNAMIC
338 | ELF_LINK_HASH_REF_REGULAR
339 | ELF_LINK_HASH_REF_REGULAR_NONWEAK
340 | ELF_LINK_HASH_NEEDS_PLT));
341 else
342 _bfd_elf_link_hash_copy_indirect (bed, dir, ind);
345 static reloc_howto_type *ppc_elf_howto_table[R_PPC_max];
347 static reloc_howto_type ppc_elf_howto_raw[] = {
348 /* This reloc does nothing. */
349 HOWTO (R_PPC_NONE, /* type */
350 0, /* rightshift */
351 2, /* size (0 = byte, 1 = short, 2 = long) */
352 32, /* bitsize */
353 FALSE, /* pc_relative */
354 0, /* bitpos */
355 complain_overflow_bitfield, /* complain_on_overflow */
356 bfd_elf_generic_reloc, /* special_function */
357 "R_PPC_NONE", /* name */
358 FALSE, /* partial_inplace */
359 0, /* src_mask */
360 0, /* dst_mask */
361 FALSE), /* pcrel_offset */
363 /* A standard 32 bit relocation. */
364 HOWTO (R_PPC_ADDR32, /* type */
365 0, /* rightshift */
366 2, /* size (0 = byte, 1 = short, 2 = long) */
367 32, /* bitsize */
368 FALSE, /* pc_relative */
369 0, /* bitpos */
370 complain_overflow_bitfield, /* complain_on_overflow */
371 bfd_elf_generic_reloc, /* special_function */
372 "R_PPC_ADDR32", /* name */
373 FALSE, /* partial_inplace */
374 0, /* src_mask */
375 0xffffffff, /* dst_mask */
376 FALSE), /* pcrel_offset */
378 /* An absolute 26 bit branch; the lower two bits must be zero.
379 FIXME: we don't check that, we just clear them. */
380 HOWTO (R_PPC_ADDR24, /* type */
381 0, /* rightshift */
382 2, /* size (0 = byte, 1 = short, 2 = long) */
383 26, /* bitsize */
384 FALSE, /* pc_relative */
385 0, /* bitpos */
386 complain_overflow_bitfield, /* complain_on_overflow */
387 bfd_elf_generic_reloc, /* special_function */
388 "R_PPC_ADDR24", /* name */
389 FALSE, /* partial_inplace */
390 0, /* src_mask */
391 0x3fffffc, /* dst_mask */
392 FALSE), /* pcrel_offset */
394 /* A standard 16 bit relocation. */
395 HOWTO (R_PPC_ADDR16, /* type */
396 0, /* rightshift */
397 1, /* size (0 = byte, 1 = short, 2 = long) */
398 16, /* bitsize */
399 FALSE, /* pc_relative */
400 0, /* bitpos */
401 complain_overflow_bitfield, /* complain_on_overflow */
402 bfd_elf_generic_reloc, /* special_function */
403 "R_PPC_ADDR16", /* name */
404 FALSE, /* partial_inplace */
405 0, /* src_mask */
406 0xffff, /* dst_mask */
407 FALSE), /* pcrel_offset */
409 /* A 16 bit relocation without overflow. */
410 HOWTO (R_PPC_ADDR16_LO, /* type */
411 0, /* rightshift */
412 1, /* size (0 = byte, 1 = short, 2 = long) */
413 16, /* bitsize */
414 FALSE, /* pc_relative */
415 0, /* bitpos */
416 complain_overflow_dont,/* complain_on_overflow */
417 bfd_elf_generic_reloc, /* special_function */
418 "R_PPC_ADDR16_LO", /* name */
419 FALSE, /* partial_inplace */
420 0, /* src_mask */
421 0xffff, /* dst_mask */
422 FALSE), /* pcrel_offset */
424 /* The high order 16 bits of an address. */
425 HOWTO (R_PPC_ADDR16_HI, /* type */
426 16, /* rightshift */
427 1, /* size (0 = byte, 1 = short, 2 = long) */
428 16, /* bitsize */
429 FALSE, /* pc_relative */
430 0, /* bitpos */
431 complain_overflow_dont, /* complain_on_overflow */
432 bfd_elf_generic_reloc, /* special_function */
433 "R_PPC_ADDR16_HI", /* name */
434 FALSE, /* partial_inplace */
435 0, /* src_mask */
436 0xffff, /* dst_mask */
437 FALSE), /* pcrel_offset */
439 /* The high order 16 bits of an address, plus 1 if the contents of
440 the low 16 bits, treated as a signed number, is negative. */
441 HOWTO (R_PPC_ADDR16_HA, /* type */
442 16, /* rightshift */
443 1, /* size (0 = byte, 1 = short, 2 = long) */
444 16, /* bitsize */
445 FALSE, /* pc_relative */
446 0, /* bitpos */
447 complain_overflow_dont, /* complain_on_overflow */
448 ppc_elf_addr16_ha_reloc, /* special_function */
449 "R_PPC_ADDR16_HA", /* name */
450 FALSE, /* partial_inplace */
451 0, /* src_mask */
452 0xffff, /* dst_mask */
453 FALSE), /* pcrel_offset */
455 /* An absolute 16 bit branch; the lower two bits must be zero.
456 FIXME: we don't check that, we just clear them. */
457 HOWTO (R_PPC_ADDR14, /* type */
458 0, /* rightshift */
459 2, /* size (0 = byte, 1 = short, 2 = long) */
460 16, /* bitsize */
461 FALSE, /* pc_relative */
462 0, /* bitpos */
463 complain_overflow_bitfield, /* complain_on_overflow */
464 bfd_elf_generic_reloc, /* special_function */
465 "R_PPC_ADDR14", /* name */
466 FALSE, /* partial_inplace */
467 0, /* src_mask */
468 0xfffc, /* dst_mask */
469 FALSE), /* pcrel_offset */
471 /* An absolute 16 bit branch, for which bit 10 should be set to
472 indicate that the branch is expected to be taken. The lower two
473 bits must be zero. */
474 HOWTO (R_PPC_ADDR14_BRTAKEN, /* type */
475 0, /* rightshift */
476 2, /* size (0 = byte, 1 = short, 2 = long) */
477 16, /* bitsize */
478 FALSE, /* pc_relative */
479 0, /* bitpos */
480 complain_overflow_bitfield, /* complain_on_overflow */
481 bfd_elf_generic_reloc, /* special_function */
482 "R_PPC_ADDR14_BRTAKEN",/* name */
483 FALSE, /* partial_inplace */
484 0, /* src_mask */
485 0xfffc, /* dst_mask */
486 FALSE), /* pcrel_offset */
488 /* An absolute 16 bit branch, for which bit 10 should be set to
489 indicate that the branch is not expected to be taken. The lower
490 two bits must be zero. */
491 HOWTO (R_PPC_ADDR14_BRNTAKEN, /* type */
492 0, /* rightshift */
493 2, /* size (0 = byte, 1 = short, 2 = long) */
494 16, /* bitsize */
495 FALSE, /* pc_relative */
496 0, /* bitpos */
497 complain_overflow_bitfield, /* complain_on_overflow */
498 bfd_elf_generic_reloc, /* special_function */
499 "R_PPC_ADDR14_BRNTAKEN",/* name */
500 FALSE, /* partial_inplace */
501 0, /* src_mask */
502 0xfffc, /* dst_mask */
503 FALSE), /* pcrel_offset */
505 /* A relative 26 bit branch; the lower two bits must be zero. */
506 HOWTO (R_PPC_REL24, /* type */
507 0, /* rightshift */
508 2, /* size (0 = byte, 1 = short, 2 = long) */
509 26, /* bitsize */
510 TRUE, /* pc_relative */
511 0, /* bitpos */
512 complain_overflow_signed, /* complain_on_overflow */
513 bfd_elf_generic_reloc, /* special_function */
514 "R_PPC_REL24", /* name */
515 FALSE, /* partial_inplace */
516 0, /* src_mask */
517 0x3fffffc, /* dst_mask */
518 TRUE), /* pcrel_offset */
520 /* A relative 16 bit branch; the lower two bits must be zero. */
521 HOWTO (R_PPC_REL14, /* type */
522 0, /* rightshift */
523 2, /* size (0 = byte, 1 = short, 2 = long) */
524 16, /* bitsize */
525 TRUE, /* pc_relative */
526 0, /* bitpos */
527 complain_overflow_signed, /* complain_on_overflow */
528 bfd_elf_generic_reloc, /* special_function */
529 "R_PPC_REL14", /* name */
530 FALSE, /* partial_inplace */
531 0, /* src_mask */
532 0xfffc, /* dst_mask */
533 TRUE), /* pcrel_offset */
535 /* A relative 16 bit branch. Bit 10 should be set to indicate that
536 the branch is expected to be taken. The lower two bits must be
537 zero. */
538 HOWTO (R_PPC_REL14_BRTAKEN, /* type */
539 0, /* rightshift */
540 2, /* size (0 = byte, 1 = short, 2 = long) */
541 16, /* bitsize */
542 TRUE, /* pc_relative */
543 0, /* bitpos */
544 complain_overflow_signed, /* complain_on_overflow */
545 bfd_elf_generic_reloc, /* special_function */
546 "R_PPC_REL14_BRTAKEN", /* name */
547 FALSE, /* partial_inplace */
548 0, /* src_mask */
549 0xfffc, /* dst_mask */
550 TRUE), /* pcrel_offset */
552 /* A relative 16 bit branch. Bit 10 should be set to indicate that
553 the branch is not expected to be taken. The lower two bits must
554 be zero. */
555 HOWTO (R_PPC_REL14_BRNTAKEN, /* type */
556 0, /* rightshift */
557 2, /* size (0 = byte, 1 = short, 2 = long) */
558 16, /* bitsize */
559 TRUE, /* pc_relative */
560 0, /* bitpos */
561 complain_overflow_signed, /* complain_on_overflow */
562 bfd_elf_generic_reloc, /* special_function */
563 "R_PPC_REL14_BRNTAKEN",/* name */
564 FALSE, /* partial_inplace */
565 0, /* src_mask */
566 0xfffc, /* dst_mask */
567 TRUE), /* pcrel_offset */
569 /* Like R_PPC_ADDR16, but referring to the GOT table entry for the
570 symbol. */
571 HOWTO (R_PPC_GOT16, /* type */
572 0, /* rightshift */
573 1, /* size (0 = byte, 1 = short, 2 = long) */
574 16, /* bitsize */
575 FALSE, /* pc_relative */
576 0, /* bitpos */
577 complain_overflow_signed, /* complain_on_overflow */
578 bfd_elf_generic_reloc, /* special_function */
579 "R_PPC_GOT16", /* name */
580 FALSE, /* partial_inplace */
581 0, /* src_mask */
582 0xffff, /* dst_mask */
583 FALSE), /* pcrel_offset */
585 /* Like R_PPC_ADDR16_LO, but referring to the GOT table entry for
586 the symbol. */
587 HOWTO (R_PPC_GOT16_LO, /* 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_dont, /* complain_on_overflow */
594 bfd_elf_generic_reloc, /* special_function */
595 "R_PPC_GOT16_LO", /* name */
596 FALSE, /* partial_inplace */
597 0, /* src_mask */
598 0xffff, /* dst_mask */
599 FALSE), /* pcrel_offset */
601 /* Like R_PPC_ADDR16_HI, but referring to the GOT table entry for
602 the symbol. */
603 HOWTO (R_PPC_GOT16_HI, /* type */
604 16, /* rightshift */
605 1, /* size (0 = byte, 1 = short, 2 = long) */
606 16, /* bitsize */
607 FALSE, /* pc_relative */
608 0, /* bitpos */
609 complain_overflow_bitfield, /* complain_on_overflow */
610 bfd_elf_generic_reloc, /* special_function */
611 "R_PPC_GOT16_HI", /* name */
612 FALSE, /* partial_inplace */
613 0, /* src_mask */
614 0xffff, /* dst_mask */
615 FALSE), /* pcrel_offset */
617 /* Like R_PPC_ADDR16_HA, but referring to the GOT table entry for
618 the symbol. */
619 HOWTO (R_PPC_GOT16_HA, /* type */
620 16, /* rightshift */
621 1, /* size (0 = byte, 1 = short, 2 = long) */
622 16, /* bitsize */
623 FALSE, /* pc_relative */
624 0, /* bitpos */
625 complain_overflow_bitfield, /* complain_on_overflow */
626 ppc_elf_addr16_ha_reloc, /* special_function */
627 "R_PPC_GOT16_HA", /* name */
628 FALSE, /* partial_inplace */
629 0, /* src_mask */
630 0xffff, /* dst_mask */
631 FALSE), /* pcrel_offset */
633 /* Like R_PPC_REL24, but referring to the procedure linkage table
634 entry for the symbol. */
635 HOWTO (R_PPC_PLTREL24, /* type */
636 0, /* rightshift */
637 2, /* size (0 = byte, 1 = short, 2 = long) */
638 26, /* bitsize */
639 TRUE, /* pc_relative */
640 0, /* bitpos */
641 complain_overflow_signed, /* complain_on_overflow */
642 bfd_elf_generic_reloc, /* special_function */
643 "R_PPC_PLTREL24", /* name */
644 FALSE, /* partial_inplace */
645 0, /* src_mask */
646 0x3fffffc, /* dst_mask */
647 TRUE), /* pcrel_offset */
649 /* This is used only by the dynamic linker. The symbol should exist
650 both in the object being run and in some shared library. The
651 dynamic linker copies the data addressed by the symbol from the
652 shared library into the object, because the object being
653 run has to have the data at some particular address. */
654 HOWTO (R_PPC_COPY, /* type */
655 0, /* rightshift */
656 2, /* size (0 = byte, 1 = short, 2 = long) */
657 32, /* bitsize */
658 FALSE, /* pc_relative */
659 0, /* bitpos */
660 complain_overflow_bitfield, /* complain_on_overflow */
661 bfd_elf_generic_reloc, /* special_function */
662 "R_PPC_COPY", /* name */
663 FALSE, /* partial_inplace */
664 0, /* src_mask */
665 0, /* dst_mask */
666 FALSE), /* pcrel_offset */
668 /* Like R_PPC_ADDR32, but used when setting global offset table
669 entries. */
670 HOWTO (R_PPC_GLOB_DAT, /* type */
671 0, /* rightshift */
672 2, /* size (0 = byte, 1 = short, 2 = long) */
673 32, /* bitsize */
674 FALSE, /* pc_relative */
675 0, /* bitpos */
676 complain_overflow_bitfield, /* complain_on_overflow */
677 bfd_elf_generic_reloc, /* special_function */
678 "R_PPC_GLOB_DAT", /* name */
679 FALSE, /* partial_inplace */
680 0, /* src_mask */
681 0xffffffff, /* dst_mask */
682 FALSE), /* pcrel_offset */
684 /* Marks a procedure linkage table entry for a symbol. */
685 HOWTO (R_PPC_JMP_SLOT, /* type */
686 0, /* rightshift */
687 2, /* size (0 = byte, 1 = short, 2 = long) */
688 32, /* bitsize */
689 FALSE, /* pc_relative */
690 0, /* bitpos */
691 complain_overflow_bitfield, /* complain_on_overflow */
692 bfd_elf_generic_reloc, /* special_function */
693 "R_PPC_JMP_SLOT", /* name */
694 FALSE, /* partial_inplace */
695 0, /* src_mask */
696 0, /* dst_mask */
697 FALSE), /* pcrel_offset */
699 /* Used only by the dynamic linker. When the object is run, this
700 longword is set to the load address of the object, plus the
701 addend. */
702 HOWTO (R_PPC_RELATIVE, /* type */
703 0, /* rightshift */
704 2, /* size (0 = byte, 1 = short, 2 = long) */
705 32, /* bitsize */
706 FALSE, /* pc_relative */
707 0, /* bitpos */
708 complain_overflow_bitfield, /* complain_on_overflow */
709 bfd_elf_generic_reloc, /* special_function */
710 "R_PPC_RELATIVE", /* name */
711 FALSE, /* partial_inplace */
712 0, /* src_mask */
713 0xffffffff, /* dst_mask */
714 FALSE), /* pcrel_offset */
716 /* Like R_PPC_REL24, but uses the value of the symbol within the
717 object rather than the final value. Normally used for
718 _GLOBAL_OFFSET_TABLE_. */
719 HOWTO (R_PPC_LOCAL24PC, /* type */
720 0, /* rightshift */
721 2, /* size (0 = byte, 1 = short, 2 = long) */
722 26, /* bitsize */
723 TRUE, /* pc_relative */
724 0, /* bitpos */
725 complain_overflow_signed, /* complain_on_overflow */
726 bfd_elf_generic_reloc, /* special_function */
727 "R_PPC_LOCAL24PC", /* name */
728 FALSE, /* partial_inplace */
729 0, /* src_mask */
730 0x3fffffc, /* dst_mask */
731 TRUE), /* pcrel_offset */
733 /* Like R_PPC_ADDR32, but may be unaligned. */
734 HOWTO (R_PPC_UADDR32, /* type */
735 0, /* rightshift */
736 2, /* size (0 = byte, 1 = short, 2 = long) */
737 32, /* bitsize */
738 FALSE, /* pc_relative */
739 0, /* bitpos */
740 complain_overflow_bitfield, /* complain_on_overflow */
741 bfd_elf_generic_reloc, /* special_function */
742 "R_PPC_UADDR32", /* name */
743 FALSE, /* partial_inplace */
744 0, /* src_mask */
745 0xffffffff, /* dst_mask */
746 FALSE), /* pcrel_offset */
748 /* Like R_PPC_ADDR16, but may be unaligned. */
749 HOWTO (R_PPC_UADDR16, /* type */
750 0, /* rightshift */
751 1, /* size (0 = byte, 1 = short, 2 = long) */
752 16, /* bitsize */
753 FALSE, /* pc_relative */
754 0, /* bitpos */
755 complain_overflow_bitfield, /* complain_on_overflow */
756 bfd_elf_generic_reloc, /* special_function */
757 "R_PPC_UADDR16", /* name */
758 FALSE, /* partial_inplace */
759 0, /* src_mask */
760 0xffff, /* dst_mask */
761 FALSE), /* pcrel_offset */
763 /* 32-bit PC relative */
764 HOWTO (R_PPC_REL32, /* type */
765 0, /* rightshift */
766 2, /* size (0 = byte, 1 = short, 2 = long) */
767 32, /* bitsize */
768 TRUE, /* pc_relative */
769 0, /* bitpos */
770 complain_overflow_bitfield, /* complain_on_overflow */
771 bfd_elf_generic_reloc, /* special_function */
772 "R_PPC_REL32", /* name */
773 FALSE, /* partial_inplace */
774 0, /* src_mask */
775 0xffffffff, /* dst_mask */
776 TRUE), /* pcrel_offset */
778 /* 32-bit relocation to the symbol's procedure linkage table.
779 FIXME: not supported. */
780 HOWTO (R_PPC_PLT32, /* type */
781 0, /* rightshift */
782 2, /* size (0 = byte, 1 = short, 2 = long) */
783 32, /* bitsize */
784 FALSE, /* pc_relative */
785 0, /* bitpos */
786 complain_overflow_bitfield, /* complain_on_overflow */
787 bfd_elf_generic_reloc, /* special_function */
788 "R_PPC_PLT32", /* name */
789 FALSE, /* partial_inplace */
790 0, /* src_mask */
791 0, /* dst_mask */
792 FALSE), /* pcrel_offset */
794 /* 32-bit PC relative relocation to the symbol's procedure linkage table.
795 FIXME: not supported. */
796 HOWTO (R_PPC_PLTREL32, /* type */
797 0, /* rightshift */
798 2, /* size (0 = byte, 1 = short, 2 = long) */
799 32, /* bitsize */
800 TRUE, /* pc_relative */
801 0, /* bitpos */
802 complain_overflow_bitfield, /* complain_on_overflow */
803 bfd_elf_generic_reloc, /* special_function */
804 "R_PPC_PLTREL32", /* name */
805 FALSE, /* partial_inplace */
806 0, /* src_mask */
807 0, /* dst_mask */
808 TRUE), /* pcrel_offset */
810 /* Like R_PPC_ADDR16_LO, but referring to the PLT table entry for
811 the symbol. */
812 HOWTO (R_PPC_PLT16_LO, /* type */
813 0, /* rightshift */
814 1, /* size (0 = byte, 1 = short, 2 = long) */
815 16, /* bitsize */
816 FALSE, /* pc_relative */
817 0, /* bitpos */
818 complain_overflow_dont, /* complain_on_overflow */
819 bfd_elf_generic_reloc, /* special_function */
820 "R_PPC_PLT16_LO", /* name */
821 FALSE, /* partial_inplace */
822 0, /* src_mask */
823 0xffff, /* dst_mask */
824 FALSE), /* pcrel_offset */
826 /* Like R_PPC_ADDR16_HI, but referring to the PLT table entry for
827 the symbol. */
828 HOWTO (R_PPC_PLT16_HI, /* type */
829 16, /* rightshift */
830 1, /* size (0 = byte, 1 = short, 2 = long) */
831 16, /* bitsize */
832 FALSE, /* pc_relative */
833 0, /* bitpos */
834 complain_overflow_bitfield, /* complain_on_overflow */
835 bfd_elf_generic_reloc, /* special_function */
836 "R_PPC_PLT16_HI", /* name */
837 FALSE, /* partial_inplace */
838 0, /* src_mask */
839 0xffff, /* dst_mask */
840 FALSE), /* pcrel_offset */
842 /* Like R_PPC_ADDR16_HA, but referring to the PLT table entry for
843 the symbol. */
844 HOWTO (R_PPC_PLT16_HA, /* type */
845 16, /* rightshift */
846 1, /* size (0 = byte, 1 = short, 2 = long) */
847 16, /* bitsize */
848 FALSE, /* pc_relative */
849 0, /* bitpos */
850 complain_overflow_bitfield, /* complain_on_overflow */
851 ppc_elf_addr16_ha_reloc, /* special_function */
852 "R_PPC_PLT16_HA", /* name */
853 FALSE, /* partial_inplace */
854 0, /* src_mask */
855 0xffff, /* dst_mask */
856 FALSE), /* pcrel_offset */
858 /* A sign-extended 16 bit value relative to _SDA_BASE_, for use with
859 small data items. */
860 HOWTO (R_PPC_SDAREL16, /* type */
861 0, /* rightshift */
862 1, /* size (0 = byte, 1 = short, 2 = long) */
863 16, /* bitsize */
864 FALSE, /* pc_relative */
865 0, /* bitpos */
866 complain_overflow_signed, /* complain_on_overflow */
867 bfd_elf_generic_reloc, /* special_function */
868 "R_PPC_SDAREL16", /* name */
869 FALSE, /* partial_inplace */
870 0, /* src_mask */
871 0xffff, /* dst_mask */
872 FALSE), /* pcrel_offset */
874 /* 16-bit section relative relocation. */
875 HOWTO (R_PPC_SECTOFF, /* type */
876 0, /* rightshift */
877 1, /* size (0 = byte, 1 = short, 2 = long) */
878 16, /* bitsize */
879 FALSE, /* pc_relative */
880 0, /* bitpos */
881 complain_overflow_bitfield, /* complain_on_overflow */
882 bfd_elf_generic_reloc, /* special_function */
883 "R_PPC_SECTOFF", /* name */
884 FALSE, /* partial_inplace */
885 0, /* src_mask */
886 0xffff, /* dst_mask */
887 FALSE), /* pcrel_offset */
889 /* 16-bit lower half section relative relocation. */
890 HOWTO (R_PPC_SECTOFF_LO, /* type */
891 0, /* rightshift */
892 1, /* size (0 = byte, 1 = short, 2 = long) */
893 16, /* bitsize */
894 FALSE, /* pc_relative */
895 0, /* bitpos */
896 complain_overflow_dont, /* complain_on_overflow */
897 bfd_elf_generic_reloc, /* special_function */
898 "R_PPC_SECTOFF_LO", /* name */
899 FALSE, /* partial_inplace */
900 0, /* src_mask */
901 0xffff, /* dst_mask */
902 FALSE), /* pcrel_offset */
904 /* 16-bit upper half section relative relocation. */
905 HOWTO (R_PPC_SECTOFF_HI, /* type */
906 16, /* rightshift */
907 1, /* size (0 = byte, 1 = short, 2 = long) */
908 16, /* bitsize */
909 FALSE, /* pc_relative */
910 0, /* bitpos */
911 complain_overflow_bitfield, /* complain_on_overflow */
912 bfd_elf_generic_reloc, /* special_function */
913 "R_PPC_SECTOFF_HI", /* name */
914 FALSE, /* partial_inplace */
915 0, /* src_mask */
916 0xffff, /* dst_mask */
917 FALSE), /* pcrel_offset */
919 /* 16-bit upper half adjusted section relative relocation. */
920 HOWTO (R_PPC_SECTOFF_HA, /* type */
921 16, /* rightshift */
922 1, /* size (0 = byte, 1 = short, 2 = long) */
923 16, /* bitsize */
924 FALSE, /* pc_relative */
925 0, /* bitpos */
926 complain_overflow_bitfield, /* complain_on_overflow */
927 ppc_elf_addr16_ha_reloc, /* special_function */
928 "R_PPC_SECTOFF_HA", /* name */
929 FALSE, /* partial_inplace */
930 0, /* src_mask */
931 0xffff, /* dst_mask */
932 FALSE), /* pcrel_offset */
934 /* Marker reloc for TLS. */
935 HOWTO (R_PPC_TLS,
936 0, /* rightshift */
937 2, /* size (0 = byte, 1 = short, 2 = long) */
938 32, /* bitsize */
939 FALSE, /* pc_relative */
940 0, /* bitpos */
941 complain_overflow_dont, /* complain_on_overflow */
942 bfd_elf_generic_reloc, /* special_function */
943 "R_PPC_TLS", /* name */
944 FALSE, /* partial_inplace */
945 0, /* src_mask */
946 0, /* dst_mask */
947 FALSE), /* pcrel_offset */
949 /* Computes the load module index of the load module that contains the
950 definition of its TLS sym. */
951 HOWTO (R_PPC_DTPMOD32,
952 0, /* rightshift */
953 2, /* size (0 = byte, 1 = short, 2 = long) */
954 32, /* bitsize */
955 FALSE, /* pc_relative */
956 0, /* bitpos */
957 complain_overflow_dont, /* complain_on_overflow */
958 ppc_elf_unhandled_reloc, /* special_function */
959 "R_PPC_DTPMOD32", /* name */
960 FALSE, /* partial_inplace */
961 0, /* src_mask */
962 0xffffffff, /* dst_mask */
963 FALSE), /* pcrel_offset */
965 /* Computes a dtv-relative displacement, the difference between the value
966 of sym+add and the base address of the thread-local storage block that
967 contains the definition of sym, minus 0x8000. */
968 HOWTO (R_PPC_DTPREL32,
969 0, /* rightshift */
970 2, /* size (0 = byte, 1 = short, 2 = long) */
971 32, /* bitsize */
972 FALSE, /* pc_relative */
973 0, /* bitpos */
974 complain_overflow_dont, /* complain_on_overflow */
975 ppc_elf_unhandled_reloc, /* special_function */
976 "R_PPC_DTPREL32", /* name */
977 FALSE, /* partial_inplace */
978 0, /* src_mask */
979 0xffffffff, /* dst_mask */
980 FALSE), /* pcrel_offset */
982 /* A 16 bit dtprel reloc. */
983 HOWTO (R_PPC_DTPREL16,
984 0, /* rightshift */
985 1, /* size (0 = byte, 1 = short, 2 = long) */
986 16, /* bitsize */
987 FALSE, /* pc_relative */
988 0, /* bitpos */
989 complain_overflow_signed, /* complain_on_overflow */
990 ppc_elf_unhandled_reloc, /* special_function */
991 "R_PPC_DTPREL16", /* name */
992 FALSE, /* partial_inplace */
993 0, /* src_mask */
994 0xffff, /* dst_mask */
995 FALSE), /* pcrel_offset */
997 /* Like DTPREL16, but no overflow. */
998 HOWTO (R_PPC_DTPREL16_LO,
999 0, /* rightshift */
1000 1, /* size (0 = byte, 1 = short, 2 = long) */
1001 16, /* bitsize */
1002 FALSE, /* pc_relative */
1003 0, /* bitpos */
1004 complain_overflow_dont, /* complain_on_overflow */
1005 ppc_elf_unhandled_reloc, /* special_function */
1006 "R_PPC_DTPREL16_LO", /* name */
1007 FALSE, /* partial_inplace */
1008 0, /* src_mask */
1009 0xffff, /* dst_mask */
1010 FALSE), /* pcrel_offset */
1012 /* Like DTPREL16_LO, but next higher group of 16 bits. */
1013 HOWTO (R_PPC_DTPREL16_HI,
1014 16, /* rightshift */
1015 1, /* size (0 = byte, 1 = short, 2 = long) */
1016 16, /* bitsize */
1017 FALSE, /* pc_relative */
1018 0, /* bitpos */
1019 complain_overflow_dont, /* complain_on_overflow */
1020 ppc_elf_unhandled_reloc, /* special_function */
1021 "R_PPC_DTPREL16_HI", /* name */
1022 FALSE, /* partial_inplace */
1023 0, /* src_mask */
1024 0xffff, /* dst_mask */
1025 FALSE), /* pcrel_offset */
1027 /* Like DTPREL16_HI, but adjust for low 16 bits. */
1028 HOWTO (R_PPC_DTPREL16_HA,
1029 16, /* rightshift */
1030 1, /* size (0 = byte, 1 = short, 2 = long) */
1031 16, /* bitsize */
1032 FALSE, /* pc_relative */
1033 0, /* bitpos */
1034 complain_overflow_dont, /* complain_on_overflow */
1035 ppc_elf_unhandled_reloc, /* special_function */
1036 "R_PPC_DTPREL16_HA", /* name */
1037 FALSE, /* partial_inplace */
1038 0, /* src_mask */
1039 0xffff, /* dst_mask */
1040 FALSE), /* pcrel_offset */
1042 /* Computes a tp-relative displacement, the difference between the value of
1043 sym+add and the value of the thread pointer (r13). */
1044 HOWTO (R_PPC_TPREL32,
1045 0, /* rightshift */
1046 2, /* size (0 = byte, 1 = short, 2 = long) */
1047 32, /* bitsize */
1048 FALSE, /* pc_relative */
1049 0, /* bitpos */
1050 complain_overflow_dont, /* complain_on_overflow */
1051 ppc_elf_unhandled_reloc, /* special_function */
1052 "R_PPC_TPREL32", /* name */
1053 FALSE, /* partial_inplace */
1054 0, /* src_mask */
1055 0xffffffff, /* dst_mask */
1056 FALSE), /* pcrel_offset */
1058 /* A 16 bit tprel reloc. */
1059 HOWTO (R_PPC_TPREL16,
1060 0, /* rightshift */
1061 1, /* size (0 = byte, 1 = short, 2 = long) */
1062 16, /* bitsize */
1063 FALSE, /* pc_relative */
1064 0, /* bitpos */
1065 complain_overflow_signed, /* complain_on_overflow */
1066 ppc_elf_unhandled_reloc, /* special_function */
1067 "R_PPC_TPREL16", /* name */
1068 FALSE, /* partial_inplace */
1069 0, /* src_mask */
1070 0xffff, /* dst_mask */
1071 FALSE), /* pcrel_offset */
1073 /* Like TPREL16, but no overflow. */
1074 HOWTO (R_PPC_TPREL16_LO,
1075 0, /* rightshift */
1076 1, /* size (0 = byte, 1 = short, 2 = long) */
1077 16, /* bitsize */
1078 FALSE, /* pc_relative */
1079 0, /* bitpos */
1080 complain_overflow_dont, /* complain_on_overflow */
1081 ppc_elf_unhandled_reloc, /* special_function */
1082 "R_PPC_TPREL16_LO", /* name */
1083 FALSE, /* partial_inplace */
1084 0, /* src_mask */
1085 0xffff, /* dst_mask */
1086 FALSE), /* pcrel_offset */
1088 /* Like TPREL16_LO, but next higher group of 16 bits. */
1089 HOWTO (R_PPC_TPREL16_HI,
1090 16, /* rightshift */
1091 1, /* size (0 = byte, 1 = short, 2 = long) */
1092 16, /* bitsize */
1093 FALSE, /* pc_relative */
1094 0, /* bitpos */
1095 complain_overflow_dont, /* complain_on_overflow */
1096 ppc_elf_unhandled_reloc, /* special_function */
1097 "R_PPC_TPREL16_HI", /* name */
1098 FALSE, /* partial_inplace */
1099 0, /* src_mask */
1100 0xffff, /* dst_mask */
1101 FALSE), /* pcrel_offset */
1103 /* Like TPREL16_HI, but adjust for low 16 bits. */
1104 HOWTO (R_PPC_TPREL16_HA,
1105 16, /* rightshift */
1106 1, /* size (0 = byte, 1 = short, 2 = long) */
1107 16, /* bitsize */
1108 FALSE, /* pc_relative */
1109 0, /* bitpos */
1110 complain_overflow_dont, /* complain_on_overflow */
1111 ppc_elf_unhandled_reloc, /* special_function */
1112 "R_PPC_TPREL16_HA", /* name */
1113 FALSE, /* partial_inplace */
1114 0, /* src_mask */
1115 0xffff, /* dst_mask */
1116 FALSE), /* pcrel_offset */
1118 /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
1119 with values (sym+add)@dtpmod and (sym+add)@dtprel, and computes the offset
1120 to the first entry. */
1121 HOWTO (R_PPC_GOT_TLSGD16,
1122 0, /* rightshift */
1123 1, /* size (0 = byte, 1 = short, 2 = long) */
1124 16, /* bitsize */
1125 FALSE, /* pc_relative */
1126 0, /* bitpos */
1127 complain_overflow_signed, /* complain_on_overflow */
1128 ppc_elf_unhandled_reloc, /* special_function */
1129 "R_PPC_GOT_TLSGD16", /* name */
1130 FALSE, /* partial_inplace */
1131 0, /* src_mask */
1132 0xffff, /* dst_mask */
1133 FALSE), /* pcrel_offset */
1135 /* Like GOT_TLSGD16, but no overflow. */
1136 HOWTO (R_PPC_GOT_TLSGD16_LO,
1137 0, /* rightshift */
1138 1, /* size (0 = byte, 1 = short, 2 = long) */
1139 16, /* bitsize */
1140 FALSE, /* pc_relative */
1141 0, /* bitpos */
1142 complain_overflow_dont, /* complain_on_overflow */
1143 ppc_elf_unhandled_reloc, /* special_function */
1144 "R_PPC_GOT_TLSGD16_LO", /* name */
1145 FALSE, /* partial_inplace */
1146 0, /* src_mask */
1147 0xffff, /* dst_mask */
1148 FALSE), /* pcrel_offset */
1150 /* Like GOT_TLSGD16_LO, but next higher group of 16 bits. */
1151 HOWTO (R_PPC_GOT_TLSGD16_HI,
1152 16, /* rightshift */
1153 1, /* size (0 = byte, 1 = short, 2 = long) */
1154 16, /* bitsize */
1155 FALSE, /* pc_relative */
1156 0, /* bitpos */
1157 complain_overflow_dont, /* complain_on_overflow */
1158 ppc_elf_unhandled_reloc, /* special_function */
1159 "R_PPC_GOT_TLSGD16_HI", /* name */
1160 FALSE, /* partial_inplace */
1161 0, /* src_mask */
1162 0xffff, /* dst_mask */
1163 FALSE), /* pcrel_offset */
1165 /* Like GOT_TLSGD16_HI, but adjust for low 16 bits. */
1166 HOWTO (R_PPC_GOT_TLSGD16_HA,
1167 16, /* rightshift */
1168 1, /* size (0 = byte, 1 = short, 2 = long) */
1169 16, /* bitsize */
1170 FALSE, /* pc_relative */
1171 0, /* bitpos */
1172 complain_overflow_dont, /* complain_on_overflow */
1173 ppc_elf_unhandled_reloc, /* special_function */
1174 "R_PPC_GOT_TLSGD16_HA", /* name */
1175 FALSE, /* partial_inplace */
1176 0, /* src_mask */
1177 0xffff, /* dst_mask */
1178 FALSE), /* pcrel_offset */
1180 /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
1181 with values (sym+add)@dtpmod and zero, and computes the offset to the
1182 first entry. */
1183 HOWTO (R_PPC_GOT_TLSLD16,
1184 0, /* rightshift */
1185 1, /* size (0 = byte, 1 = short, 2 = long) */
1186 16, /* bitsize */
1187 FALSE, /* pc_relative */
1188 0, /* bitpos */
1189 complain_overflow_signed, /* complain_on_overflow */
1190 ppc_elf_unhandled_reloc, /* special_function */
1191 "R_PPC_GOT_TLSLD16", /* name */
1192 FALSE, /* partial_inplace */
1193 0, /* src_mask */
1194 0xffff, /* dst_mask */
1195 FALSE), /* pcrel_offset */
1197 /* Like GOT_TLSLD16, but no overflow. */
1198 HOWTO (R_PPC_GOT_TLSLD16_LO,
1199 0, /* rightshift */
1200 1, /* size (0 = byte, 1 = short, 2 = long) */
1201 16, /* bitsize */
1202 FALSE, /* pc_relative */
1203 0, /* bitpos */
1204 complain_overflow_dont, /* complain_on_overflow */
1205 ppc_elf_unhandled_reloc, /* special_function */
1206 "R_PPC_GOT_TLSLD16_LO", /* name */
1207 FALSE, /* partial_inplace */
1208 0, /* src_mask */
1209 0xffff, /* dst_mask */
1210 FALSE), /* pcrel_offset */
1212 /* Like GOT_TLSLD16_LO, but next higher group of 16 bits. */
1213 HOWTO (R_PPC_GOT_TLSLD16_HI,
1214 16, /* rightshift */
1215 1, /* size (0 = byte, 1 = short, 2 = long) */
1216 16, /* bitsize */
1217 FALSE, /* pc_relative */
1218 0, /* bitpos */
1219 complain_overflow_dont, /* complain_on_overflow */
1220 ppc_elf_unhandled_reloc, /* special_function */
1221 "R_PPC_GOT_TLSLD16_HI", /* name */
1222 FALSE, /* partial_inplace */
1223 0, /* src_mask */
1224 0xffff, /* dst_mask */
1225 FALSE), /* pcrel_offset */
1227 /* Like GOT_TLSLD16_HI, but adjust for low 16 bits. */
1228 HOWTO (R_PPC_GOT_TLSLD16_HA,
1229 16, /* rightshift */
1230 1, /* size (0 = byte, 1 = short, 2 = long) */
1231 16, /* bitsize */
1232 FALSE, /* pc_relative */
1233 0, /* bitpos */
1234 complain_overflow_dont, /* complain_on_overflow */
1235 ppc_elf_unhandled_reloc, /* special_function */
1236 "R_PPC_GOT_TLSLD16_HA", /* name */
1237 FALSE, /* partial_inplace */
1238 0, /* src_mask */
1239 0xffff, /* dst_mask */
1240 FALSE), /* pcrel_offset */
1242 /* Allocates an entry in the GOT with value (sym+add)@dtprel, and computes
1243 the offset to the entry. */
1244 HOWTO (R_PPC_GOT_DTPREL16,
1245 0, /* rightshift */
1246 1, /* size (0 = byte, 1 = short, 2 = long) */
1247 16, /* bitsize */
1248 FALSE, /* pc_relative */
1249 0, /* bitpos */
1250 complain_overflow_signed, /* complain_on_overflow */
1251 ppc_elf_unhandled_reloc, /* special_function */
1252 "R_PPC_GOT_DTPREL16", /* name */
1253 FALSE, /* partial_inplace */
1254 0, /* src_mask */
1255 0xffff, /* dst_mask */
1256 FALSE), /* pcrel_offset */
1258 /* Like GOT_DTPREL16, but no overflow. */
1259 HOWTO (R_PPC_GOT_DTPREL16_LO,
1260 0, /* rightshift */
1261 1, /* size (0 = byte, 1 = short, 2 = long) */
1262 16, /* bitsize */
1263 FALSE, /* pc_relative */
1264 0, /* bitpos */
1265 complain_overflow_dont, /* complain_on_overflow */
1266 ppc_elf_unhandled_reloc, /* special_function */
1267 "R_PPC_GOT_DTPREL16_LO", /* name */
1268 FALSE, /* partial_inplace */
1269 0, /* src_mask */
1270 0xffff, /* dst_mask */
1271 FALSE), /* pcrel_offset */
1273 /* Like GOT_DTPREL16_LO, but next higher group of 16 bits. */
1274 HOWTO (R_PPC_GOT_DTPREL16_HI,
1275 16, /* rightshift */
1276 1, /* size (0 = byte, 1 = short, 2 = long) */
1277 16, /* bitsize */
1278 FALSE, /* pc_relative */
1279 0, /* bitpos */
1280 complain_overflow_dont, /* complain_on_overflow */
1281 ppc_elf_unhandled_reloc, /* special_function */
1282 "R_PPC_GOT_DTPREL16_HI", /* name */
1283 FALSE, /* partial_inplace */
1284 0, /* src_mask */
1285 0xffff, /* dst_mask */
1286 FALSE), /* pcrel_offset */
1288 /* Like GOT_DTPREL16_HI, but adjust for low 16 bits. */
1289 HOWTO (R_PPC_GOT_DTPREL16_HA,
1290 16, /* rightshift */
1291 1, /* size (0 = byte, 1 = short, 2 = long) */
1292 16, /* bitsize */
1293 FALSE, /* pc_relative */
1294 0, /* bitpos */
1295 complain_overflow_dont, /* complain_on_overflow */
1296 ppc_elf_unhandled_reloc, /* special_function */
1297 "R_PPC_GOT_DTPREL16_HA", /* name */
1298 FALSE, /* partial_inplace */
1299 0, /* src_mask */
1300 0xffff, /* dst_mask */
1301 FALSE), /* pcrel_offset */
1303 /* Allocates an entry in the GOT with value (sym+add)@tprel, and computes the
1304 offset to the entry. */
1305 HOWTO (R_PPC_GOT_TPREL16,
1306 0, /* rightshift */
1307 1, /* size (0 = byte, 1 = short, 2 = long) */
1308 16, /* bitsize */
1309 FALSE, /* pc_relative */
1310 0, /* bitpos */
1311 complain_overflow_signed, /* complain_on_overflow */
1312 ppc_elf_unhandled_reloc, /* special_function */
1313 "R_PPC_GOT_TPREL16", /* name */
1314 FALSE, /* partial_inplace */
1315 0, /* src_mask */
1316 0xffff, /* dst_mask */
1317 FALSE), /* pcrel_offset */
1319 /* Like GOT_TPREL16, but no overflow. */
1320 HOWTO (R_PPC_GOT_TPREL16_LO,
1321 0, /* rightshift */
1322 1, /* size (0 = byte, 1 = short, 2 = long) */
1323 16, /* bitsize */
1324 FALSE, /* pc_relative */
1325 0, /* bitpos */
1326 complain_overflow_dont, /* complain_on_overflow */
1327 ppc_elf_unhandled_reloc, /* special_function */
1328 "R_PPC_GOT_TPREL16_LO", /* name */
1329 FALSE, /* partial_inplace */
1330 0, /* src_mask */
1331 0xffff, /* dst_mask */
1332 FALSE), /* pcrel_offset */
1334 /* Like GOT_TPREL16_LO, but next higher group of 16 bits. */
1335 HOWTO (R_PPC_GOT_TPREL16_HI,
1336 16, /* rightshift */
1337 1, /* size (0 = byte, 1 = short, 2 = long) */
1338 16, /* bitsize */
1339 FALSE, /* pc_relative */
1340 0, /* bitpos */
1341 complain_overflow_dont, /* complain_on_overflow */
1342 ppc_elf_unhandled_reloc, /* special_function */
1343 "R_PPC_GOT_TPREL16_HI", /* name */
1344 FALSE, /* partial_inplace */
1345 0, /* src_mask */
1346 0xffff, /* dst_mask */
1347 FALSE), /* pcrel_offset */
1349 /* Like GOT_TPREL16_HI, but adjust for low 16 bits. */
1350 HOWTO (R_PPC_GOT_TPREL16_HA,
1351 16, /* rightshift */
1352 1, /* size (0 = byte, 1 = short, 2 = long) */
1353 16, /* bitsize */
1354 FALSE, /* pc_relative */
1355 0, /* bitpos */
1356 complain_overflow_dont, /* complain_on_overflow */
1357 ppc_elf_unhandled_reloc, /* special_function */
1358 "R_PPC_GOT_TPREL16_HA", /* name */
1359 FALSE, /* partial_inplace */
1360 0, /* src_mask */
1361 0xffff, /* dst_mask */
1362 FALSE), /* pcrel_offset */
1364 /* The remaining relocs are from the Embedded ELF ABI, and are not
1365 in the SVR4 ELF ABI. */
1367 /* 32 bit value resulting from the addend minus the symbol. */
1368 HOWTO (R_PPC_EMB_NADDR32, /* type */
1369 0, /* rightshift */
1370 2, /* size (0 = byte, 1 = short, 2 = long) */
1371 32, /* bitsize */
1372 FALSE, /* pc_relative */
1373 0, /* bitpos */
1374 complain_overflow_bitfield, /* complain_on_overflow */
1375 bfd_elf_generic_reloc, /* special_function */
1376 "R_PPC_EMB_NADDR32", /* name */
1377 FALSE, /* partial_inplace */
1378 0, /* src_mask */
1379 0xffffffff, /* dst_mask */
1380 FALSE), /* pcrel_offset */
1382 /* 16 bit value resulting from the addend minus the symbol. */
1383 HOWTO (R_PPC_EMB_NADDR16, /* type */
1384 0, /* rightshift */
1385 1, /* size (0 = byte, 1 = short, 2 = long) */
1386 16, /* bitsize */
1387 FALSE, /* pc_relative */
1388 0, /* bitpos */
1389 complain_overflow_bitfield, /* complain_on_overflow */
1390 bfd_elf_generic_reloc, /* special_function */
1391 "R_PPC_EMB_NADDR16", /* name */
1392 FALSE, /* partial_inplace */
1393 0, /* src_mask */
1394 0xffff, /* dst_mask */
1395 FALSE), /* pcrel_offset */
1397 /* 16 bit value resulting from the addend minus the symbol. */
1398 HOWTO (R_PPC_EMB_NADDR16_LO, /* type */
1399 0, /* rightshift */
1400 1, /* size (0 = byte, 1 = short, 2 = long) */
1401 16, /* bitsize */
1402 FALSE, /* pc_relative */
1403 0, /* bitpos */
1404 complain_overflow_dont,/* complain_on_overflow */
1405 bfd_elf_generic_reloc, /* special_function */
1406 "R_PPC_EMB_ADDR16_LO", /* name */
1407 FALSE, /* partial_inplace */
1408 0, /* src_mask */
1409 0xffff, /* dst_mask */
1410 FALSE), /* pcrel_offset */
1412 /* The high order 16 bits of the addend minus the symbol. */
1413 HOWTO (R_PPC_EMB_NADDR16_HI, /* type */
1414 16, /* rightshift */
1415 1, /* size (0 = byte, 1 = short, 2 = long) */
1416 16, /* bitsize */
1417 FALSE, /* pc_relative */
1418 0, /* bitpos */
1419 complain_overflow_dont, /* complain_on_overflow */
1420 bfd_elf_generic_reloc, /* special_function */
1421 "R_PPC_EMB_NADDR16_HI", /* name */
1422 FALSE, /* partial_inplace */
1423 0, /* src_mask */
1424 0xffff, /* dst_mask */
1425 FALSE), /* pcrel_offset */
1427 /* The high order 16 bits of the result of the addend minus the address,
1428 plus 1 if the contents of the low 16 bits, treated as a signed number,
1429 is negative. */
1430 HOWTO (R_PPC_EMB_NADDR16_HA, /* type */
1431 16, /* rightshift */
1432 1, /* size (0 = byte, 1 = short, 2 = long) */
1433 16, /* bitsize */
1434 FALSE, /* pc_relative */
1435 0, /* bitpos */
1436 complain_overflow_dont, /* complain_on_overflow */
1437 ppc_elf_addr16_ha_reloc, /* special_function */
1438 "R_PPC_EMB_NADDR16_HA", /* name */
1439 FALSE, /* partial_inplace */
1440 0, /* src_mask */
1441 0xffff, /* dst_mask */
1442 FALSE), /* pcrel_offset */
1444 /* 16 bit value resulting from allocating a 4 byte word to hold an
1445 address in the .sdata section, and returning the offset from
1446 _SDA_BASE_ for that relocation. */
1447 HOWTO (R_PPC_EMB_SDAI16, /* type */
1448 0, /* rightshift */
1449 1, /* size (0 = byte, 1 = short, 2 = long) */
1450 16, /* bitsize */
1451 FALSE, /* pc_relative */
1452 0, /* bitpos */
1453 complain_overflow_bitfield, /* complain_on_overflow */
1454 bfd_elf_generic_reloc, /* special_function */
1455 "R_PPC_EMB_SDAI16", /* name */
1456 FALSE, /* partial_inplace */
1457 0, /* src_mask */
1458 0xffff, /* dst_mask */
1459 FALSE), /* pcrel_offset */
1461 /* 16 bit value resulting from allocating a 4 byte word to hold an
1462 address in the .sdata2 section, and returning the offset from
1463 _SDA2_BASE_ for that relocation. */
1464 HOWTO (R_PPC_EMB_SDA2I16, /* type */
1465 0, /* rightshift */
1466 1, /* size (0 = byte, 1 = short, 2 = long) */
1467 16, /* bitsize */
1468 FALSE, /* pc_relative */
1469 0, /* bitpos */
1470 complain_overflow_bitfield, /* complain_on_overflow */
1471 bfd_elf_generic_reloc, /* special_function */
1472 "R_PPC_EMB_SDA2I16", /* name */
1473 FALSE, /* partial_inplace */
1474 0, /* src_mask */
1475 0xffff, /* dst_mask */
1476 FALSE), /* pcrel_offset */
1478 /* A sign-extended 16 bit value relative to _SDA2_BASE_, for use with
1479 small data items. */
1480 HOWTO (R_PPC_EMB_SDA2REL, /* type */
1481 0, /* rightshift */
1482 1, /* size (0 = byte, 1 = short, 2 = long) */
1483 16, /* bitsize */
1484 FALSE, /* pc_relative */
1485 0, /* bitpos */
1486 complain_overflow_signed, /* complain_on_overflow */
1487 bfd_elf_generic_reloc, /* special_function */
1488 "R_PPC_EMB_SDA2REL", /* name */
1489 FALSE, /* partial_inplace */
1490 0, /* src_mask */
1491 0xffff, /* dst_mask */
1492 FALSE), /* pcrel_offset */
1494 /* Relocate against either _SDA_BASE_ or _SDA2_BASE_, filling in the 16 bit
1495 signed offset from the appropriate base, and filling in the register
1496 field with the appropriate register (0, 2, or 13). */
1497 HOWTO (R_PPC_EMB_SDA21, /* type */
1498 0, /* rightshift */
1499 2, /* size (0 = byte, 1 = short, 2 = long) */
1500 16, /* bitsize */
1501 FALSE, /* pc_relative */
1502 0, /* bitpos */
1503 complain_overflow_signed, /* complain_on_overflow */
1504 bfd_elf_generic_reloc, /* special_function */
1505 "R_PPC_EMB_SDA21", /* name */
1506 FALSE, /* partial_inplace */
1507 0, /* src_mask */
1508 0xffff, /* dst_mask */
1509 FALSE), /* pcrel_offset */
1511 /* Relocation not handled: R_PPC_EMB_MRKREF */
1512 /* Relocation not handled: R_PPC_EMB_RELSEC16 */
1513 /* Relocation not handled: R_PPC_EMB_RELST_LO */
1514 /* Relocation not handled: R_PPC_EMB_RELST_HI */
1515 /* Relocation not handled: R_PPC_EMB_RELST_HA */
1516 /* Relocation not handled: R_PPC_EMB_BIT_FLD */
1518 /* PC relative relocation against either _SDA_BASE_ or _SDA2_BASE_, filling
1519 in the 16 bit signed offset from the appropriate base, and filling in the
1520 register field with the appropriate register (0, 2, or 13). */
1521 HOWTO (R_PPC_EMB_RELSDA, /* type */
1522 0, /* rightshift */
1523 1, /* size (0 = byte, 1 = short, 2 = long) */
1524 16, /* bitsize */
1525 TRUE, /* pc_relative */
1526 0, /* bitpos */
1527 complain_overflow_signed, /* complain_on_overflow */
1528 bfd_elf_generic_reloc, /* special_function */
1529 "R_PPC_EMB_RELSDA", /* name */
1530 FALSE, /* partial_inplace */
1531 0, /* src_mask */
1532 0xffff, /* dst_mask */
1533 FALSE), /* pcrel_offset */
1535 /* Phony relocs to handle branch stubs. */
1536 HOWTO (R_PPC_RELAX32, /* type */
1537 0, /* rightshift */
1538 0, /* size */
1539 0, /* bitsize */
1540 FALSE, /* pc_relative */
1541 0, /* bitpos */
1542 complain_overflow_dont, /* complain_on_overflow */
1543 bfd_elf_generic_reloc, /* special_function */
1544 "R_PPC_RELAX32", /* name */
1545 FALSE, /* partial_inplace */
1546 0, /* src_mask */
1547 0, /* dst_mask */
1548 FALSE), /* pcrel_offset */
1550 HOWTO (R_PPC_RELAX32PC, /* type */
1551 0, /* rightshift */
1552 0, /* size */
1553 0, /* bitsize */
1554 FALSE, /* pc_relative */
1555 0, /* bitpos */
1556 complain_overflow_dont, /* complain_on_overflow */
1557 bfd_elf_generic_reloc, /* special_function */
1558 "R_PPC_RELAX32PC", /* name */
1559 FALSE, /* partial_inplace */
1560 0, /* src_mask */
1561 0, /* dst_mask */
1562 FALSE), /* pcrel_offset */
1564 /* GNU extension to record C++ vtable hierarchy. */
1565 HOWTO (R_PPC_GNU_VTINHERIT, /* type */
1566 0, /* rightshift */
1567 0, /* size (0 = byte, 1 = short, 2 = long) */
1568 0, /* bitsize */
1569 FALSE, /* pc_relative */
1570 0, /* bitpos */
1571 complain_overflow_dont, /* complain_on_overflow */
1572 NULL, /* special_function */
1573 "R_PPC_GNU_VTINHERIT", /* name */
1574 FALSE, /* partial_inplace */
1575 0, /* src_mask */
1576 0, /* dst_mask */
1577 FALSE), /* pcrel_offset */
1579 /* GNU extension to record C++ vtable member usage. */
1580 HOWTO (R_PPC_GNU_VTENTRY, /* type */
1581 0, /* rightshift */
1582 0, /* size (0 = byte, 1 = short, 2 = long) */
1583 0, /* bitsize */
1584 FALSE, /* pc_relative */
1585 0, /* bitpos */
1586 complain_overflow_dont, /* complain_on_overflow */
1587 NULL, /* special_function */
1588 "R_PPC_GNU_VTENTRY", /* name */
1589 FALSE, /* partial_inplace */
1590 0, /* src_mask */
1591 0, /* dst_mask */
1592 FALSE), /* pcrel_offset */
1594 /* Phony reloc to handle AIX style TOC entries. */
1595 HOWTO (R_PPC_TOC16, /* type */
1596 0, /* rightshift */
1597 1, /* size (0 = byte, 1 = short, 2 = long) */
1598 16, /* bitsize */
1599 FALSE, /* pc_relative */
1600 0, /* bitpos */
1601 complain_overflow_signed, /* complain_on_overflow */
1602 bfd_elf_generic_reloc, /* special_function */
1603 "R_PPC_TOC16", /* name */
1604 FALSE, /* partial_inplace */
1605 0, /* src_mask */
1606 0xffff, /* dst_mask */
1607 FALSE), /* pcrel_offset */
1610 /* Initialize the ppc_elf_howto_table, so that linear accesses can be done. */
1612 static void
1613 ppc_elf_howto_init (void)
1615 unsigned int i, type;
1617 for (i = 0;
1618 i < sizeof (ppc_elf_howto_raw) / sizeof (ppc_elf_howto_raw[0]);
1619 i++)
1621 type = ppc_elf_howto_raw[i].type;
1622 if (type >= (sizeof (ppc_elf_howto_table)
1623 / sizeof (ppc_elf_howto_table[0])))
1624 abort ();
1625 ppc_elf_howto_table[type] = &ppc_elf_howto_raw[i];
1629 #define ARRAY_SIZE(a) (sizeof (a) / sizeof ((a)[0]))
1631 static const int shared_stub_entry[] =
1633 0x7c0802a6, /* mflr 0 */
1634 0x429f0005, /* bcl 20, 31, .Lxxx */
1635 0x7d6802a6, /* mflr 11 */
1636 0x3d6b0000, /* addis 11, 11, (xxx-.Lxxx)@ha */
1637 0x396b0018, /* addi 11, 11, (xxx-.Lxxx)@l */
1638 0x7c0803a6, /* mtlr 0 */
1639 0x7d6903a6, /* mtctr 11 */
1640 0x4e800420, /* bctr */
1643 static const int stub_entry[] =
1645 0x3d600000, /* lis 11,xxx@ha */
1646 0x396b0000, /* addi 11,11,xxx@l */
1647 0x7d6903a6, /* mtctr 11 */
1648 0x4e800420, /* bctr */
1652 static bfd_boolean
1653 ppc_elf_relax_section (bfd *abfd,
1654 asection *isec,
1655 struct bfd_link_info *link_info,
1656 bfd_boolean *again)
1658 struct one_fixup
1660 struct one_fixup *next;
1661 asection *tsec;
1662 bfd_vma toff;
1663 bfd_vma trampoff;
1666 Elf_Internal_Shdr *symtab_hdr;
1667 bfd_byte *contents = NULL;
1668 Elf_Internal_Sym *isymbuf = NULL;
1669 Elf_Internal_Rela *internal_relocs = NULL;
1670 Elf_Internal_Rela *irel, *irelend;
1671 struct one_fixup *fixups = NULL;
1672 bfd_boolean changed;
1673 struct ppc_elf_link_hash_table *ppc_info;
1674 bfd_size_type trampoff;
1676 *again = FALSE;
1678 /* Nothing to do if there are no relocations. */
1679 if ((isec->flags & SEC_RELOC) == 0 || isec->reloc_count == 0)
1680 return TRUE;
1682 trampoff = (isec->size + 3) & (bfd_vma) -4;
1683 /* Space for a branch around any trampolines. */
1684 trampoff += 4;
1686 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1688 /* Get a copy of the native relocations. */
1689 internal_relocs = _bfd_elf_link_read_relocs (abfd, isec, NULL, NULL,
1690 link_info->keep_memory);
1691 if (internal_relocs == NULL)
1692 goto error_return;
1694 ppc_info = ppc_elf_hash_table (link_info);
1695 irelend = internal_relocs + isec->reloc_count;
1697 for (irel = internal_relocs; irel < irelend; irel++)
1699 unsigned long r_type = ELF32_R_TYPE (irel->r_info);
1700 bfd_vma symaddr, reladdr, toff, roff;
1701 asection *tsec;
1702 struct one_fixup *f;
1703 size_t insn_offset = 0;
1704 bfd_vma max_branch_offset, val;
1705 bfd_byte *hit_addr;
1706 unsigned long t0;
1707 unsigned char sym_type;
1709 switch (r_type)
1711 case R_PPC_REL24:
1712 case R_PPC_LOCAL24PC:
1713 case R_PPC_PLTREL24:
1714 max_branch_offset = 1 << 25;
1715 break;
1717 case R_PPC_REL14:
1718 case R_PPC_REL14_BRTAKEN:
1719 case R_PPC_REL14_BRNTAKEN:
1720 max_branch_offset = 1 << 15;
1721 break;
1723 default:
1724 continue;
1727 /* Get the value of the symbol referred to by the reloc. */
1728 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
1730 /* A local symbol. */
1731 Elf_Internal_Sym *isym;
1733 /* Read this BFD's local symbols. */
1734 if (isymbuf == NULL)
1736 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
1737 if (isymbuf == NULL)
1738 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
1739 symtab_hdr->sh_info, 0,
1740 NULL, NULL, NULL);
1741 if (isymbuf == 0)
1742 goto error_return;
1744 isym = isymbuf + ELF32_R_SYM (irel->r_info);
1745 if (isym->st_shndx == SHN_UNDEF)
1746 continue; /* We can't do anything with undefined symbols. */
1747 else if (isym->st_shndx == SHN_ABS)
1748 tsec = bfd_abs_section_ptr;
1749 else if (isym->st_shndx == SHN_COMMON)
1750 tsec = bfd_com_section_ptr;
1751 else
1752 tsec = bfd_section_from_elf_index (abfd, isym->st_shndx);
1754 toff = isym->st_value;
1755 sym_type = ELF_ST_TYPE (isym->st_info);
1757 else
1759 /* Global symbol handling. */
1760 unsigned long indx;
1761 struct elf_link_hash_entry *h;
1763 indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
1764 h = elf_sym_hashes (abfd)[indx];
1766 while (h->root.type == bfd_link_hash_indirect
1767 || h->root.type == bfd_link_hash_warning)
1768 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1770 if (r_type == R_PPC_PLTREL24
1771 && ppc_info->plt != NULL
1772 && h->plt.offset != (bfd_vma) -1)
1774 tsec = ppc_info->plt;
1775 toff = h->plt.offset;
1777 else if (h->root.type == bfd_link_hash_defined
1778 || h->root.type == bfd_link_hash_defweak)
1780 tsec = h->root.u.def.section;
1781 toff = h->root.u.def.value;
1783 else
1784 continue;
1786 sym_type = h->type;
1789 /* If the branch and target are in the same section, you have
1790 no hope of adding stubs. We'll error out later should the
1791 branch overflow. */
1792 if (tsec == isec)
1793 continue;
1795 /* There probably isn't any reason to handle symbols in
1796 SEC_MERGE sections; SEC_MERGE doesn't seem a likely
1797 attribute for a code section, and we are only looking at
1798 branches. However, implement it correctly here as a
1799 reference for other target relax_section functions. */
1800 if (0 && tsec->sec_info_type == ELF_INFO_TYPE_MERGE)
1802 /* At this stage in linking, no SEC_MERGE symbol has been
1803 adjusted, so all references to such symbols need to be
1804 passed through _bfd_merged_section_offset. (Later, in
1805 relocate_section, all SEC_MERGE symbols *except* for
1806 section symbols have been adjusted.)
1808 gas may reduce relocations against symbols in SEC_MERGE
1809 sections to a relocation against the section symbol when
1810 the original addend was zero. When the reloc is against
1811 a section symbol we should include the addend in the
1812 offset passed to _bfd_merged_section_offset, since the
1813 location of interest is the original symbol. On the
1814 other hand, an access to "sym+addend" where "sym" is not
1815 a section symbol should not include the addend; Such an
1816 access is presumed to be an offset from "sym"; The
1817 location of interest is just "sym". */
1818 if (sym_type == STT_SECTION)
1819 toff += irel->r_addend;
1821 toff = _bfd_merged_section_offset (abfd, &tsec,
1822 elf_section_data (tsec)->sec_info,
1823 toff);
1825 if (sym_type != STT_SECTION)
1826 toff += irel->r_addend;
1828 else
1829 toff += irel->r_addend;
1831 symaddr = tsec->output_section->vma + tsec->output_offset + toff;
1833 roff = irel->r_offset;
1834 reladdr = isec->output_section->vma + isec->output_offset + roff;
1836 /* If the branch is in range, no need to do anything. */
1837 if (symaddr - reladdr + max_branch_offset < 2 * max_branch_offset)
1838 continue;
1840 /* Look for an existing fixup to this address. */
1841 for (f = fixups; f ; f = f->next)
1842 if (f->tsec == tsec && f->toff == toff)
1843 break;
1845 if (f == NULL)
1847 size_t size;
1848 unsigned long stub_rtype;
1850 val = trampoff - roff;
1851 if (val >= max_branch_offset)
1852 /* Oh dear, we can't reach a trampoline. Don't try to add
1853 one. We'll report an error later. */
1854 continue;
1856 if (link_info->shared)
1858 size = 4 * ARRAY_SIZE (shared_stub_entry);
1859 insn_offset = 12;
1860 stub_rtype = R_PPC_RELAX32PC;
1862 else
1864 size = 4 * ARRAY_SIZE (stub_entry);
1865 insn_offset = 0;
1866 stub_rtype = R_PPC_RELAX32;
1869 /* Hijack the old relocation. Since we need two
1870 relocations for this use a "composite" reloc. */
1871 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
1872 stub_rtype);
1873 irel->r_offset = trampoff + insn_offset;
1875 /* Record the fixup so we don't do it again this section. */
1876 f = bfd_malloc (sizeof (*f));
1877 f->next = fixups;
1878 f->tsec = tsec;
1879 f->toff = toff;
1880 f->trampoff = trampoff;
1881 fixups = f;
1883 trampoff += size;
1885 else
1887 val = f->trampoff - roff;
1888 if (val >= max_branch_offset)
1889 continue;
1891 /* Nop out the reloc, since we're finalizing things here. */
1892 irel->r_info = ELF32_R_INFO (0, R_PPC_NONE);
1895 /* Get the section contents. */
1896 if (contents == NULL)
1898 /* Get cached copy if it exists. */
1899 if (elf_section_data (isec)->this_hdr.contents != NULL)
1900 contents = elf_section_data (isec)->this_hdr.contents;
1901 else
1903 /* Go get them off disk. */
1904 if (!bfd_malloc_and_get_section (abfd, isec, &contents))
1905 goto error_return;
1909 /* Fix up the existing branch to hit the trampoline. */
1910 hit_addr = contents + roff;
1911 switch (r_type)
1913 case R_PPC_REL24:
1914 case R_PPC_LOCAL24PC:
1915 case R_PPC_PLTREL24:
1916 t0 = bfd_get_32 (abfd, hit_addr);
1917 t0 &= ~0x3fffffc;
1918 t0 |= val & 0x3fffffc;
1919 bfd_put_32 (abfd, t0, hit_addr);
1920 break;
1922 case R_PPC_REL14:
1923 case R_PPC_REL14_BRTAKEN:
1924 case R_PPC_REL14_BRNTAKEN:
1925 t0 = bfd_get_32 (abfd, hit_addr);
1926 t0 &= ~0xfffc;
1927 t0 |= val & 0xfffc;
1928 bfd_put_32 (abfd, t0, hit_addr);
1929 break;
1933 /* Write out the trampolines. */
1934 changed = fixups != NULL;
1935 if (fixups != NULL)
1937 const int *stub;
1938 bfd_byte *dest;
1939 bfd_vma val;
1940 int i, size;
1944 struct one_fixup *f = fixups;
1945 fixups = fixups->next;
1946 free (f);
1948 while (fixups);
1950 contents = bfd_realloc (contents, trampoff);
1951 if (contents == NULL)
1952 goto error_return;
1954 isec->size = (isec->size + 3) & (bfd_vma) -4;
1955 /* Branch around the trampolines. */
1956 val = trampoff - isec->size + 0x48000000;
1957 dest = contents + isec->size;
1958 isec->size = trampoff;
1959 bfd_put_32 (abfd, val, dest);
1960 dest += 4;
1962 if (link_info->shared)
1964 stub = shared_stub_entry;
1965 size = ARRAY_SIZE (shared_stub_entry);
1967 else
1969 stub = stub_entry;
1970 size = ARRAY_SIZE (stub_entry);
1973 i = 0;
1974 while (dest < contents + trampoff)
1976 bfd_put_32 (abfd, stub[i], dest);
1977 i++;
1978 if (i == size)
1979 i = 0;
1980 dest += 4;
1982 BFD_ASSERT (i == 0);
1985 if (isymbuf != NULL
1986 && symtab_hdr->contents != (unsigned char *) isymbuf)
1988 if (! link_info->keep_memory)
1989 free (isymbuf);
1990 else
1992 /* Cache the symbols for elf_link_input_bfd. */
1993 symtab_hdr->contents = (unsigned char *) isymbuf;
1997 if (contents != NULL
1998 && elf_section_data (isec)->this_hdr.contents != contents)
2000 if (!changed && !link_info->keep_memory)
2001 free (contents);
2002 else
2004 /* Cache the section contents for elf_link_input_bfd. */
2005 elf_section_data (isec)->this_hdr.contents = contents;
2009 if (elf_section_data (isec)->relocs != internal_relocs)
2011 if (!changed)
2012 free (internal_relocs);
2013 else
2014 elf_section_data (isec)->relocs = internal_relocs;
2017 *again = changed;
2018 return TRUE;
2020 error_return:
2021 if (isymbuf != NULL && (unsigned char *) isymbuf != symtab_hdr->contents)
2022 free (isymbuf);
2023 if (contents != NULL
2024 && elf_section_data (isec)->this_hdr.contents != contents)
2025 free (contents);
2026 if (internal_relocs != NULL
2027 && elf_section_data (isec)->relocs != internal_relocs)
2028 free (internal_relocs);
2029 return FALSE;
2032 static reloc_howto_type *
2033 ppc_elf_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
2034 bfd_reloc_code_real_type code)
2036 enum elf_ppc_reloc_type r;
2038 /* Initialize howto table if not already done. */
2039 if (!ppc_elf_howto_table[R_PPC_ADDR32])
2040 ppc_elf_howto_init ();
2042 switch (code)
2044 default:
2045 return NULL;
2047 case BFD_RELOC_NONE: r = R_PPC_NONE; break;
2048 case BFD_RELOC_32: r = R_PPC_ADDR32; break;
2049 case BFD_RELOC_PPC_BA26: r = R_PPC_ADDR24; break;
2050 case BFD_RELOC_16: r = R_PPC_ADDR16; break;
2051 case BFD_RELOC_LO16: r = R_PPC_ADDR16_LO; break;
2052 case BFD_RELOC_HI16: r = R_PPC_ADDR16_HI; break;
2053 case BFD_RELOC_HI16_S: r = R_PPC_ADDR16_HA; break;
2054 case BFD_RELOC_PPC_BA16: r = R_PPC_ADDR14; break;
2055 case BFD_RELOC_PPC_BA16_BRTAKEN: r = R_PPC_ADDR14_BRTAKEN; break;
2056 case BFD_RELOC_PPC_BA16_BRNTAKEN: r = R_PPC_ADDR14_BRNTAKEN; break;
2057 case BFD_RELOC_PPC_B26: r = R_PPC_REL24; break;
2058 case BFD_RELOC_PPC_B16: r = R_PPC_REL14; break;
2059 case BFD_RELOC_PPC_B16_BRTAKEN: r = R_PPC_REL14_BRTAKEN; break;
2060 case BFD_RELOC_PPC_B16_BRNTAKEN: r = R_PPC_REL14_BRNTAKEN; break;
2061 case BFD_RELOC_16_GOTOFF: r = R_PPC_GOT16; break;
2062 case BFD_RELOC_LO16_GOTOFF: r = R_PPC_GOT16_LO; break;
2063 case BFD_RELOC_HI16_GOTOFF: r = R_PPC_GOT16_HI; break;
2064 case BFD_RELOC_HI16_S_GOTOFF: r = R_PPC_GOT16_HA; break;
2065 case BFD_RELOC_24_PLT_PCREL: r = R_PPC_PLTREL24; break;
2066 case BFD_RELOC_PPC_COPY: r = R_PPC_COPY; break;
2067 case BFD_RELOC_PPC_GLOB_DAT: r = R_PPC_GLOB_DAT; break;
2068 case BFD_RELOC_PPC_LOCAL24PC: r = R_PPC_LOCAL24PC; break;
2069 case BFD_RELOC_32_PCREL: r = R_PPC_REL32; break;
2070 case BFD_RELOC_32_PLTOFF: r = R_PPC_PLT32; break;
2071 case BFD_RELOC_32_PLT_PCREL: r = R_PPC_PLTREL32; break;
2072 case BFD_RELOC_LO16_PLTOFF: r = R_PPC_PLT16_LO; break;
2073 case BFD_RELOC_HI16_PLTOFF: r = R_PPC_PLT16_HI; break;
2074 case BFD_RELOC_HI16_S_PLTOFF: r = R_PPC_PLT16_HA; break;
2075 case BFD_RELOC_GPREL16: r = R_PPC_SDAREL16; break;
2076 case BFD_RELOC_16_BASEREL: r = R_PPC_SECTOFF; break;
2077 case BFD_RELOC_LO16_BASEREL: r = R_PPC_SECTOFF_LO; break;
2078 case BFD_RELOC_HI16_BASEREL: r = R_PPC_SECTOFF_HI; break;
2079 case BFD_RELOC_HI16_S_BASEREL: r = R_PPC_SECTOFF_HA; break;
2080 case BFD_RELOC_CTOR: r = R_PPC_ADDR32; break;
2081 case BFD_RELOC_PPC_TOC16: r = R_PPC_TOC16; break;
2082 case BFD_RELOC_PPC_TLS: r = R_PPC_TLS; break;
2083 case BFD_RELOC_PPC_DTPMOD: r = R_PPC_DTPMOD32; break;
2084 case BFD_RELOC_PPC_TPREL16: r = R_PPC_TPREL16; break;
2085 case BFD_RELOC_PPC_TPREL16_LO: r = R_PPC_TPREL16_LO; break;
2086 case BFD_RELOC_PPC_TPREL16_HI: r = R_PPC_TPREL16_HI; break;
2087 case BFD_RELOC_PPC_TPREL16_HA: r = R_PPC_TPREL16_HA; break;
2088 case BFD_RELOC_PPC_TPREL: r = R_PPC_TPREL32; break;
2089 case BFD_RELOC_PPC_DTPREL16: r = R_PPC_DTPREL16; break;
2090 case BFD_RELOC_PPC_DTPREL16_LO: r = R_PPC_DTPREL16_LO; break;
2091 case BFD_RELOC_PPC_DTPREL16_HI: r = R_PPC_DTPREL16_HI; break;
2092 case BFD_RELOC_PPC_DTPREL16_HA: r = R_PPC_DTPREL16_HA; break;
2093 case BFD_RELOC_PPC_DTPREL: r = R_PPC_DTPREL32; break;
2094 case BFD_RELOC_PPC_GOT_TLSGD16: r = R_PPC_GOT_TLSGD16; break;
2095 case BFD_RELOC_PPC_GOT_TLSGD16_LO: r = R_PPC_GOT_TLSGD16_LO; break;
2096 case BFD_RELOC_PPC_GOT_TLSGD16_HI: r = R_PPC_GOT_TLSGD16_HI; break;
2097 case BFD_RELOC_PPC_GOT_TLSGD16_HA: r = R_PPC_GOT_TLSGD16_HA; break;
2098 case BFD_RELOC_PPC_GOT_TLSLD16: r = R_PPC_GOT_TLSLD16; break;
2099 case BFD_RELOC_PPC_GOT_TLSLD16_LO: r = R_PPC_GOT_TLSLD16_LO; break;
2100 case BFD_RELOC_PPC_GOT_TLSLD16_HI: r = R_PPC_GOT_TLSLD16_HI; break;
2101 case BFD_RELOC_PPC_GOT_TLSLD16_HA: r = R_PPC_GOT_TLSLD16_HA; break;
2102 case BFD_RELOC_PPC_GOT_TPREL16: r = R_PPC_GOT_TPREL16; break;
2103 case BFD_RELOC_PPC_GOT_TPREL16_LO: r = R_PPC_GOT_TPREL16_LO; break;
2104 case BFD_RELOC_PPC_GOT_TPREL16_HI: r = R_PPC_GOT_TPREL16_HI; break;
2105 case BFD_RELOC_PPC_GOT_TPREL16_HA: r = R_PPC_GOT_TPREL16_HA; break;
2106 case BFD_RELOC_PPC_GOT_DTPREL16: r = R_PPC_GOT_DTPREL16; break;
2107 case BFD_RELOC_PPC_GOT_DTPREL16_LO: r = R_PPC_GOT_DTPREL16_LO; break;
2108 case BFD_RELOC_PPC_GOT_DTPREL16_HI: r = R_PPC_GOT_DTPREL16_HI; break;
2109 case BFD_RELOC_PPC_GOT_DTPREL16_HA: r = R_PPC_GOT_DTPREL16_HA; break;
2110 case BFD_RELOC_PPC_EMB_NADDR32: r = R_PPC_EMB_NADDR32; break;
2111 case BFD_RELOC_PPC_EMB_NADDR16: r = R_PPC_EMB_NADDR16; break;
2112 case BFD_RELOC_PPC_EMB_NADDR16_LO: r = R_PPC_EMB_NADDR16_LO; break;
2113 case BFD_RELOC_PPC_EMB_NADDR16_HI: r = R_PPC_EMB_NADDR16_HI; break;
2114 case BFD_RELOC_PPC_EMB_NADDR16_HA: r = R_PPC_EMB_NADDR16_HA; break;
2115 case BFD_RELOC_PPC_EMB_SDAI16: r = R_PPC_EMB_SDAI16; break;
2116 case BFD_RELOC_PPC_EMB_SDA2I16: r = R_PPC_EMB_SDA2I16; break;
2117 case BFD_RELOC_PPC_EMB_SDA2REL: r = R_PPC_EMB_SDA2REL; break;
2118 case BFD_RELOC_PPC_EMB_SDA21: r = R_PPC_EMB_SDA21; break;
2119 case BFD_RELOC_PPC_EMB_MRKREF: r = R_PPC_EMB_MRKREF; break;
2120 case BFD_RELOC_PPC_EMB_RELSEC16: r = R_PPC_EMB_RELSEC16; break;
2121 case BFD_RELOC_PPC_EMB_RELST_LO: r = R_PPC_EMB_RELST_LO; break;
2122 case BFD_RELOC_PPC_EMB_RELST_HI: r = R_PPC_EMB_RELST_HI; break;
2123 case BFD_RELOC_PPC_EMB_RELST_HA: r = R_PPC_EMB_RELST_HA; break;
2124 case BFD_RELOC_PPC_EMB_BIT_FLD: r = R_PPC_EMB_BIT_FLD; break;
2125 case BFD_RELOC_PPC_EMB_RELSDA: r = R_PPC_EMB_RELSDA; break;
2126 case BFD_RELOC_VTABLE_INHERIT: r = R_PPC_GNU_VTINHERIT; break;
2127 case BFD_RELOC_VTABLE_ENTRY: r = R_PPC_GNU_VTENTRY; break;
2130 return ppc_elf_howto_table[r];
2133 /* Set the howto pointer for a PowerPC ELF reloc. */
2135 static void
2136 ppc_elf_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED,
2137 arelent *cache_ptr,
2138 Elf_Internal_Rela *dst)
2140 /* Initialize howto table if not already done. */
2141 if (!ppc_elf_howto_table[R_PPC_ADDR32])
2142 ppc_elf_howto_init ();
2144 BFD_ASSERT (ELF32_R_TYPE (dst->r_info) < (unsigned int) R_PPC_max);
2145 cache_ptr->howto = ppc_elf_howto_table[ELF32_R_TYPE (dst->r_info)];
2148 /* Handle the R_PPC_ADDR16_HA reloc. */
2150 static bfd_reloc_status_type
2151 ppc_elf_addr16_ha_reloc (bfd *abfd ATTRIBUTE_UNUSED,
2152 arelent *reloc_entry,
2153 asymbol *symbol,
2154 void *data ATTRIBUTE_UNUSED,
2155 asection *input_section,
2156 bfd *output_bfd,
2157 char **error_message ATTRIBUTE_UNUSED)
2159 bfd_vma relocation;
2161 if (output_bfd != NULL)
2163 reloc_entry->address += input_section->output_offset;
2164 return bfd_reloc_ok;
2167 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
2168 return bfd_reloc_outofrange;
2170 if (bfd_is_com_section (symbol->section))
2171 relocation = 0;
2172 else
2173 relocation = symbol->value;
2175 relocation += symbol->section->output_section->vma;
2176 relocation += symbol->section->output_offset;
2177 relocation += reloc_entry->addend;
2179 reloc_entry->addend += (relocation & 0x8000) << 1;
2181 return bfd_reloc_continue;
2184 static bfd_reloc_status_type
2185 ppc_elf_unhandled_reloc (bfd *abfd,
2186 arelent *reloc_entry,
2187 asymbol *symbol,
2188 void *data,
2189 asection *input_section,
2190 bfd *output_bfd,
2191 char **error_message)
2193 /* If this is a relocatable link (output_bfd test tells us), just
2194 call the generic function. Any adjustment will be done at final
2195 link time. */
2196 if (output_bfd != NULL)
2197 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2198 input_section, output_bfd, error_message);
2200 if (error_message != NULL)
2202 static char buf[60];
2203 sprintf (buf, _("generic linker can't handle %s"),
2204 reloc_entry->howto->name);
2205 *error_message = buf;
2207 return bfd_reloc_dangerous;
2210 /* Fix bad default arch selected for a 32 bit input bfd when the
2211 default is 64 bit. */
2213 static bfd_boolean
2214 ppc_elf_object_p (bfd *abfd)
2216 if (abfd->arch_info->the_default && abfd->arch_info->bits_per_word == 64)
2218 Elf_Internal_Ehdr *i_ehdr = elf_elfheader (abfd);
2220 if (i_ehdr->e_ident[EI_CLASS] == ELFCLASS32)
2222 /* Relies on arch after 64 bit default being 32 bit default. */
2223 abfd->arch_info = abfd->arch_info->next;
2224 BFD_ASSERT (abfd->arch_info->bits_per_word == 32);
2227 return TRUE;
2230 /* Function to set whether a module needs the -mrelocatable bit set. */
2232 static bfd_boolean
2233 ppc_elf_set_private_flags (bfd *abfd, flagword flags)
2235 BFD_ASSERT (!elf_flags_init (abfd)
2236 || elf_elfheader (abfd)->e_flags == flags);
2238 elf_elfheader (abfd)->e_flags = flags;
2239 elf_flags_init (abfd) = TRUE;
2240 return TRUE;
2243 /* Merge backend specific data from an object file to the output
2244 object file when linking. */
2246 static bfd_boolean
2247 ppc_elf_merge_private_bfd_data (bfd *ibfd, bfd *obfd)
2249 flagword old_flags;
2250 flagword new_flags;
2251 bfd_boolean error;
2253 /* Check if we have the same endianess. */
2254 if (! _bfd_generic_verify_endian_match (ibfd, obfd))
2255 return FALSE;
2257 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
2258 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
2259 return TRUE;
2261 new_flags = elf_elfheader (ibfd)->e_flags;
2262 old_flags = elf_elfheader (obfd)->e_flags;
2263 if (!elf_flags_init (obfd))
2265 /* First call, no flags set. */
2266 elf_flags_init (obfd) = TRUE;
2267 elf_elfheader (obfd)->e_flags = new_flags;
2270 /* Compatible flags are ok. */
2271 else if (new_flags == old_flags)
2274 /* Incompatible flags. */
2275 else
2277 /* Warn about -mrelocatable mismatch. Allow -mrelocatable-lib
2278 to be linked with either. */
2279 error = FALSE;
2280 if ((new_flags & EF_PPC_RELOCATABLE) != 0
2281 && (old_flags & (EF_PPC_RELOCATABLE | EF_PPC_RELOCATABLE_LIB)) == 0)
2283 error = TRUE;
2284 (*_bfd_error_handler)
2285 (_("%s: compiled with -mrelocatable and linked with "
2286 "modules compiled normally"),
2287 bfd_archive_filename (ibfd));
2289 else if ((new_flags & (EF_PPC_RELOCATABLE | EF_PPC_RELOCATABLE_LIB)) == 0
2290 && (old_flags & EF_PPC_RELOCATABLE) != 0)
2292 error = TRUE;
2293 (*_bfd_error_handler)
2294 (_("%s: compiled normally and linked with "
2295 "modules compiled with -mrelocatable"),
2296 bfd_archive_filename (ibfd));
2299 /* The output is -mrelocatable-lib iff both the input files are. */
2300 if (! (new_flags & EF_PPC_RELOCATABLE_LIB))
2301 elf_elfheader (obfd)->e_flags &= ~EF_PPC_RELOCATABLE_LIB;
2303 /* The output is -mrelocatable iff it can't be -mrelocatable-lib,
2304 but each input file is either -mrelocatable or -mrelocatable-lib. */
2305 if (! (elf_elfheader (obfd)->e_flags & EF_PPC_RELOCATABLE_LIB)
2306 && (new_flags & (EF_PPC_RELOCATABLE_LIB | EF_PPC_RELOCATABLE))
2307 && (old_flags & (EF_PPC_RELOCATABLE_LIB | EF_PPC_RELOCATABLE)))
2308 elf_elfheader (obfd)->e_flags |= EF_PPC_RELOCATABLE;
2310 /* Do not warn about eabi vs. V.4 mismatch, just or in the bit if
2311 any module uses it. */
2312 elf_elfheader (obfd)->e_flags |= (new_flags & EF_PPC_EMB);
2314 new_flags &= ~(EF_PPC_RELOCATABLE | EF_PPC_RELOCATABLE_LIB | EF_PPC_EMB);
2315 old_flags &= ~(EF_PPC_RELOCATABLE | EF_PPC_RELOCATABLE_LIB | EF_PPC_EMB);
2317 /* Warn about any other mismatches. */
2318 if (new_flags != old_flags)
2320 error = TRUE;
2321 (*_bfd_error_handler)
2322 (_("%s: uses different e_flags (0x%lx) fields "
2323 "than previous modules (0x%lx)"),
2324 bfd_archive_filename (ibfd), (long) new_flags, (long) old_flags);
2327 if (error)
2329 bfd_set_error (bfd_error_bad_value);
2330 return FALSE;
2334 return TRUE;
2337 /* Handle a PowerPC specific section when reading an object file. This
2338 is called when elfcode.h finds a section with an unknown type. */
2340 static bfd_boolean
2341 ppc_elf_section_from_shdr (bfd *abfd, Elf_Internal_Shdr *hdr, const char *name)
2343 asection *newsect;
2344 flagword flags;
2346 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name))
2347 return FALSE;
2349 newsect = hdr->bfd_section;
2350 flags = bfd_get_section_flags (abfd, newsect);
2351 if (hdr->sh_flags & SHF_EXCLUDE)
2352 flags |= SEC_EXCLUDE;
2354 if (hdr->sh_type == SHT_ORDERED)
2355 flags |= SEC_SORT_ENTRIES;
2357 bfd_set_section_flags (abfd, newsect, flags);
2358 return TRUE;
2361 /* Set up any other section flags and such that may be necessary. */
2363 static bfd_boolean
2364 ppc_elf_fake_sections (bfd *abfd ATTRIBUTE_UNUSED,
2365 Elf_Internal_Shdr *shdr,
2366 asection *asect)
2368 if ((asect->flags & SEC_EXCLUDE) != 0)
2369 shdr->sh_flags |= SHF_EXCLUDE;
2371 if ((asect->flags & SEC_SORT_ENTRIES) != 0)
2372 shdr->sh_type = SHT_ORDERED;
2374 return TRUE;
2377 /* Find a linker generated pointer with a given addend and type. */
2379 static elf_linker_section_pointers_t *
2380 elf_find_pointer_linker_section
2381 (elf_linker_section_pointers_t *linker_pointers,
2382 bfd_vma addend,
2383 elf_linker_section_t *lsect)
2385 for ( ; linker_pointers != NULL; linker_pointers = linker_pointers->next)
2386 if (lsect == linker_pointers->lsect && addend == linker_pointers->addend)
2387 return linker_pointers;
2389 return NULL;
2392 /* Allocate a pointer to live in a linker created section. */
2394 static bfd_boolean
2395 elf_create_pointer_linker_section (bfd *abfd,
2396 struct bfd_link_info *info,
2397 elf_linker_section_t *lsect,
2398 struct elf_link_hash_entry *h,
2399 const Elf_Internal_Rela *rel)
2401 elf_linker_section_pointers_t **ptr_linker_section_ptr = NULL;
2402 elf_linker_section_pointers_t *linker_section_ptr;
2403 unsigned long r_symndx = ELF32_R_SYM (rel->r_info);
2404 bfd_size_type amt;
2406 BFD_ASSERT (lsect != NULL);
2408 /* Is this a global symbol? */
2409 if (h != NULL)
2411 struct ppc_elf_link_hash_entry *eh;
2413 /* Has this symbol already been allocated? If so, our work is done. */
2414 eh = (struct ppc_elf_link_hash_entry *) h;
2415 if (elf_find_pointer_linker_section (eh->linker_section_pointer,
2416 rel->r_addend,
2417 lsect))
2418 return TRUE;
2420 ptr_linker_section_ptr = &eh->linker_section_pointer;
2421 /* Make sure this symbol is output as a dynamic symbol. */
2422 if (h->dynindx == -1)
2424 if (! bfd_elf_link_record_dynamic_symbol (info, h))
2425 return FALSE;
2428 if (lsect->rel_section)
2429 lsect->rel_section->size += sizeof (Elf32_External_Rela);
2431 else
2433 /* Allocation of a pointer to a local symbol. */
2434 elf_linker_section_pointers_t **ptr = elf_local_ptr_offsets (abfd);
2436 /* Allocate a table to hold the local symbols if first time. */
2437 if (!ptr)
2439 unsigned int num_symbols = elf_tdata (abfd)->symtab_hdr.sh_info;
2441 amt = num_symbols;
2442 amt *= sizeof (elf_linker_section_pointers_t *);
2443 ptr = bfd_zalloc (abfd, amt);
2445 if (!ptr)
2446 return FALSE;
2448 elf_local_ptr_offsets (abfd) = ptr;
2451 /* Has this symbol already been allocated? If so, our work is done. */
2452 if (elf_find_pointer_linker_section (ptr[r_symndx],
2453 rel->r_addend,
2454 lsect))
2455 return TRUE;
2457 ptr_linker_section_ptr = &ptr[r_symndx];
2459 if (info->shared)
2461 /* If we are generating a shared object, we need to
2462 output a R_<xxx>_RELATIVE reloc so that the
2463 dynamic linker can adjust this GOT entry. */
2464 BFD_ASSERT (lsect->rel_section != NULL);
2465 lsect->rel_section->size += sizeof (Elf32_External_Rela);
2469 /* Allocate space for a pointer in the linker section, and allocate
2470 a new pointer record from internal memory. */
2471 BFD_ASSERT (ptr_linker_section_ptr != NULL);
2472 amt = sizeof (elf_linker_section_pointers_t);
2473 linker_section_ptr = bfd_alloc (abfd, amt);
2475 if (!linker_section_ptr)
2476 return FALSE;
2478 linker_section_ptr->next = *ptr_linker_section_ptr;
2479 linker_section_ptr->addend = rel->r_addend;
2480 linker_section_ptr->lsect = lsect;
2481 linker_section_ptr->written_address_p = FALSE;
2482 *ptr_linker_section_ptr = linker_section_ptr;
2484 linker_section_ptr->offset = lsect->section->size;
2485 lsect->section->size += 4;
2487 #ifdef DEBUG
2488 fprintf (stderr,
2489 "Create pointer in linker section %s, offset = %ld, section size = %ld\n",
2490 lsect->name, (long) linker_section_ptr->offset,
2491 (long) lsect->section->size);
2492 #endif
2494 return TRUE;
2497 #define bfd_put_ptr(BFD, VAL, ADDR) bfd_put_32 (BFD, VAL, ADDR)
2499 /* Fill in the address for a pointer generated in a linker section. */
2501 static bfd_vma
2502 elf_finish_pointer_linker_section (bfd *output_bfd,
2503 bfd *input_bfd,
2504 struct bfd_link_info *info,
2505 elf_linker_section_t *lsect,
2506 struct elf_link_hash_entry *h,
2507 bfd_vma relocation,
2508 const Elf_Internal_Rela *rel,
2509 int relative_reloc)
2511 elf_linker_section_pointers_t *linker_section_ptr;
2513 BFD_ASSERT (lsect != NULL);
2515 if (h != NULL)
2517 /* Handle global symbol. */
2518 struct ppc_elf_link_hash_entry *eh;
2520 eh = (struct ppc_elf_link_hash_entry *) h;
2521 linker_section_ptr
2522 = elf_find_pointer_linker_section (eh->linker_section_pointer,
2523 rel->r_addend,
2524 lsect);
2526 BFD_ASSERT (linker_section_ptr != NULL);
2528 if (! elf_hash_table (info)->dynamic_sections_created
2529 || (info->shared
2530 && info->symbolic
2531 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR)))
2533 /* This is actually a static link, or it is a
2534 -Bsymbolic link and the symbol is defined
2535 locally. We must initialize this entry in the
2536 global section.
2538 When doing a dynamic link, we create a .rela.<xxx>
2539 relocation entry to initialize the value. This
2540 is done in the finish_dynamic_symbol routine. */
2541 if (!linker_section_ptr->written_address_p)
2543 linker_section_ptr->written_address_p = TRUE;
2544 bfd_put_ptr (output_bfd,
2545 relocation + linker_section_ptr->addend,
2546 (lsect->section->contents
2547 + linker_section_ptr->offset));
2551 else
2553 /* Handle local symbol. */
2554 unsigned long r_symndx = ELF32_R_SYM (rel->r_info);
2555 BFD_ASSERT (elf_local_ptr_offsets (input_bfd) != NULL);
2556 BFD_ASSERT (elf_local_ptr_offsets (input_bfd)[r_symndx] != NULL);
2557 linker_section_ptr = (elf_find_pointer_linker_section
2558 (elf_local_ptr_offsets (input_bfd)[r_symndx],
2559 rel->r_addend,
2560 lsect));
2562 BFD_ASSERT (linker_section_ptr != NULL);
2564 /* Write out pointer if it hasn't been rewritten out before. */
2565 if (!linker_section_ptr->written_address_p)
2567 linker_section_ptr->written_address_p = TRUE;
2568 bfd_put_ptr (output_bfd, relocation + linker_section_ptr->addend,
2569 lsect->section->contents + linker_section_ptr->offset);
2571 if (info->shared)
2573 /* We need to generate a relative reloc for the dynamic
2574 linker. */
2576 asection *srel = lsect->rel_section;
2577 Elf_Internal_Rela outrel[MAX_INT_RELS_PER_EXT_REL];
2578 bfd_byte *erel;
2579 const struct elf_backend_data *bed;
2580 unsigned int i;
2582 BFD_ASSERT (srel != NULL);
2584 bed = get_elf_backend_data (output_bfd);
2585 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
2587 outrel[i].r_offset = (lsect->section->output_section->vma
2588 + lsect->section->output_offset
2589 + linker_section_ptr->offset);
2590 outrel[i].r_info = 0;
2591 outrel[i].r_addend = 0;
2593 outrel[0].r_info = ELF32_R_INFO (0, relative_reloc);
2594 erel = lsect->section->contents;
2595 erel += (elf_section_data (lsect->section)->rel_count++
2596 * sizeof (Elf32_External_Rela));
2597 bfd_elf32_swap_reloca_out (output_bfd, outrel, erel);
2602 relocation = (lsect->section->output_offset
2603 + linker_section_ptr->offset
2604 - lsect->sym_offset);
2606 #ifdef DEBUG
2607 fprintf (stderr,
2608 "Finish pointer in linker section %s, offset = %ld (0x%lx)\n",
2609 lsect->name, (long) relocation, (long) relocation);
2610 #endif
2612 /* Subtract out the addend, because it will get added back in by the normal
2613 processing. */
2614 return relocation - linker_section_ptr->addend;
2617 /* Create a special linker section */
2618 static elf_linker_section_t *
2619 ppc_elf_create_linker_section (bfd *abfd,
2620 struct bfd_link_info *info,
2621 enum elf_linker_section_enum which)
2623 elf_linker_section_t *lsect;
2624 struct ppc_elf_link_hash_table *htab = ppc_elf_hash_table (info);
2625 asection *s, *sym_sec;
2626 bfd_size_type amt;
2627 flagword flags;
2628 const char *name;
2629 const char *rel_name;
2630 const char *sym_name;
2631 bfd_vma sym_offset;
2633 /* Both of these sections are (technically) created by the user
2634 putting data in them, so they shouldn't be marked
2635 SEC_LINKER_CREATED.
2637 The linker creates them so it has somewhere to attach their
2638 respective symbols. In fact, if they were empty it would
2639 be OK to leave the symbol set to 0 (or any random number), because
2640 the appropriate register should never be used. */
2641 flags = SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY;
2642 sym_offset = 32768;
2644 switch (which)
2646 default:
2647 abort ();
2648 return NULL;
2650 case LINKER_SECTION_SDATA: /* .sdata/.sbss section */
2651 name = ".sdata";
2652 rel_name = ".rela.sdata";
2653 sym_name = "_SDA_BASE_";
2654 break;
2656 case LINKER_SECTION_SDATA2: /* .sdata2/.sbss2 section */
2657 name = ".sdata2";
2658 rel_name = ".rela.sdata2";
2659 sym_name = "_SDA2_BASE_";
2660 flags |= SEC_READONLY;
2661 break;
2664 /* Record the first bfd that needs the special sections. */
2665 if (!htab->elf.dynobj)
2666 htab->elf.dynobj = abfd;
2668 amt = sizeof (elf_linker_section_t);
2669 lsect = bfd_zalloc (htab->elf.dynobj, amt);
2671 lsect->sym_offset = sym_offset;
2673 /* See if the sections already exist. */
2674 sym_sec = s = bfd_get_section_by_name (htab->elf.dynobj, name);
2675 if (s == NULL || (s->flags & flags) != flags)
2677 s = bfd_make_section_anyway (htab->elf.dynobj, name);
2678 if (s == NULL
2679 || !bfd_set_section_flags (htab->elf.dynobj, s, flags))
2680 return NULL;
2681 if (sym_sec == NULL)
2682 sym_sec = s;
2684 lsect->section = s;
2686 if (bfd_get_section_alignment (htab->elf.dynobj, s) < 2
2687 && !bfd_set_section_alignment (htab->elf.dynobj, s, 2))
2688 return NULL;
2690 s->size = align_power (s->size, 2);
2692 #ifdef DEBUG
2693 fprintf (stderr, "Creating section %s, current size = %ld\n",
2694 name, (long) s->size);
2695 #endif
2697 if (sym_name)
2699 struct elf_link_hash_entry *h;
2700 struct bfd_link_hash_entry *bh;
2702 #ifdef DEBUG
2703 fprintf (stderr, "Adding %s to section %s\n", sym_name, name);
2704 #endif
2705 bh = bfd_link_hash_lookup (info->hash, sym_name,
2706 FALSE, FALSE, FALSE);
2708 if ((bh == NULL || bh->type == bfd_link_hash_undefined)
2709 && !(_bfd_generic_link_add_one_symbol
2710 (info, abfd, sym_name, BSF_GLOBAL, sym_sec, sym_offset, NULL,
2711 FALSE, get_elf_backend_data (abfd)->collect, &bh)))
2712 return NULL;
2713 h = (struct elf_link_hash_entry *) bh;
2715 h->type = STT_OBJECT;
2716 lsect->sym_hash = h;
2718 if (info->shared
2719 && ! bfd_elf_link_record_dynamic_symbol (info, h))
2720 return NULL;
2723 if (info->shared)
2725 s = bfd_make_section_anyway (htab->elf.dynobj, rel_name);
2726 lsect->rel_section = s;
2727 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
2728 | SEC_LINKER_CREATED | SEC_READONLY);
2729 if (s == NULL
2730 || ! bfd_set_section_flags (htab->elf.dynobj, s, flags)
2731 || ! bfd_set_section_alignment (htab->elf.dynobj, s, 2))
2732 return NULL;
2735 return lsect;
2738 /* If we have a non-zero sized .sbss2 or .PPC.EMB.sbss0 sections, we
2739 need to bump up the number of section headers. */
2741 static int
2742 ppc_elf_additional_program_headers (bfd *abfd)
2744 asection *s;
2745 int ret;
2747 ret = 0;
2749 s = bfd_get_section_by_name (abfd, ".interp");
2750 if (s != NULL)
2751 ++ret;
2753 s = bfd_get_section_by_name (abfd, ".sbss2");
2754 if (s != NULL && (s->flags & SEC_LOAD) != 0 && s->size > 0)
2755 ++ret;
2757 s = bfd_get_section_by_name (abfd, ".PPC.EMB.sbss0");
2758 if (s != NULL && (s->flags & SEC_LOAD) != 0 && s->size > 0)
2759 ++ret;
2761 return ret;
2764 /* Modify the segment map if needed. */
2766 static bfd_boolean
2767 ppc_elf_modify_segment_map (bfd *abfd ATTRIBUTE_UNUSED,
2768 struct bfd_link_info *info ATTRIBUTE_UNUSED)
2770 return TRUE;
2773 /* The powerpc .got has a blrl instruction in it. Mark it executable. */
2775 static bfd_boolean
2776 ppc_elf_create_got (bfd *abfd, struct bfd_link_info *info)
2778 struct ppc_elf_link_hash_table *htab;
2779 asection *s;
2780 flagword flags;
2782 if (!_bfd_elf_create_got_section (abfd, info))
2783 return FALSE;
2785 htab = ppc_elf_hash_table (info);
2786 htab->got = s = bfd_get_section_by_name (abfd, ".got");
2787 if (s == NULL)
2788 abort ();
2790 flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_HAS_CONTENTS | SEC_IN_MEMORY
2791 | SEC_LINKER_CREATED);
2792 if (!bfd_set_section_flags (abfd, s, flags))
2793 return FALSE;
2795 htab->relgot = bfd_make_section (abfd, ".rela.got");
2796 if (!htab->relgot
2797 || ! bfd_set_section_flags (abfd, htab->relgot,
2798 (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
2799 | SEC_IN_MEMORY | SEC_LINKER_CREATED
2800 | SEC_READONLY))
2801 || ! bfd_set_section_alignment (abfd, htab->relgot, 2))
2802 return FALSE;
2804 return TRUE;
2807 /* We have to create .dynsbss and .rela.sbss here so that they get mapped
2808 to output sections (just like _bfd_elf_create_dynamic_sections has
2809 to create .dynbss and .rela.bss). */
2811 static bfd_boolean
2812 ppc_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
2814 struct ppc_elf_link_hash_table *htab;
2815 asection *s;
2816 flagword flags;
2818 htab = ppc_elf_hash_table (info);
2820 if (htab->got == NULL
2821 && !ppc_elf_create_got (abfd, info))
2822 return FALSE;
2824 if (!_bfd_elf_create_dynamic_sections (abfd, info))
2825 return FALSE;
2827 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
2828 | SEC_LINKER_CREATED);
2830 htab->dynbss = bfd_get_section_by_name (abfd, ".dynbss");
2831 htab->dynsbss = s = bfd_make_section (abfd, ".dynsbss");
2832 if (s == NULL
2833 || ! bfd_set_section_flags (abfd, s, SEC_ALLOC))
2834 return FALSE;
2836 if (! info->shared)
2838 htab->relbss = bfd_get_section_by_name (abfd, ".rela.bss");
2839 htab->relsbss = s = bfd_make_section (abfd, ".rela.sbss");
2840 if (s == NULL
2841 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
2842 || ! bfd_set_section_alignment (abfd, s, 2))
2843 return FALSE;
2846 htab->relplt = bfd_get_section_by_name (abfd, ".rela.plt");
2847 htab->plt = s = bfd_get_section_by_name (abfd, ".plt");
2848 if (s == NULL)
2849 abort ();
2851 flags = SEC_ALLOC | SEC_CODE | SEC_IN_MEMORY | SEC_LINKER_CREATED;
2852 return bfd_set_section_flags (abfd, s, flags);
2855 /* Adjust a symbol defined by a dynamic object and referenced by a
2856 regular object. The current definition is in some section of the
2857 dynamic object, but we're not including those sections. We have to
2858 change the definition to something the rest of the link can
2859 understand. */
2861 static bfd_boolean
2862 ppc_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
2863 struct elf_link_hash_entry *h)
2865 struct ppc_elf_link_hash_table *htab;
2866 asection *s;
2867 unsigned int power_of_two;
2869 #ifdef DEBUG
2870 fprintf (stderr, "ppc_elf_adjust_dynamic_symbol called for %s\n",
2871 h->root.root.string);
2872 #endif
2874 /* Make sure we know what is going on here. */
2875 htab = ppc_elf_hash_table (info);
2876 BFD_ASSERT (htab->elf.dynobj != NULL
2877 && ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT)
2878 || h->weakdef != NULL
2879 || ((h->elf_link_hash_flags
2880 & ELF_LINK_HASH_DEF_DYNAMIC) != 0
2881 && (h->elf_link_hash_flags
2882 & ELF_LINK_HASH_REF_REGULAR) != 0
2883 && (h->elf_link_hash_flags
2884 & ELF_LINK_HASH_DEF_REGULAR) == 0)));
2886 /* Deal with function syms. */
2887 if (h->type == STT_FUNC
2888 || (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0)
2890 /* Clear procedure linkage table information for any symbol that
2891 won't need a .plt entry. */
2892 if (h->plt.refcount <= 0
2893 || SYMBOL_CALLS_LOCAL (info, h)
2894 || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2895 && h->root.type == bfd_link_hash_undefweak))
2897 /* A PLT entry is not required/allowed when:
2899 1. We are not using ld.so; because then the PLT entry
2900 can't be set up, so we can't use one. In this case,
2901 ppc_elf_adjust_dynamic_symbol won't even be called.
2903 2. GC has rendered the entry unused.
2905 3. We know for certain that a call to this symbol
2906 will go to this object, or will remain undefined. */
2907 h->plt.offset = (bfd_vma) -1;
2908 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
2910 return TRUE;
2912 else
2913 h->plt.offset = (bfd_vma) -1;
2915 /* If this is a weak symbol, and there is a real definition, the
2916 processor independent code will have arranged for us to see the
2917 real definition first, and we can just use the same value. */
2918 if (h->weakdef != NULL)
2920 BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined
2921 || h->weakdef->root.type == bfd_link_hash_defweak);
2922 h->root.u.def.section = h->weakdef->root.u.def.section;
2923 h->root.u.def.value = h->weakdef->root.u.def.value;
2924 if (ELIMINATE_COPY_RELOCS)
2925 h->elf_link_hash_flags
2926 = ((h->elf_link_hash_flags & ~ELF_LINK_NON_GOT_REF)
2927 | (h->weakdef->elf_link_hash_flags & ELF_LINK_NON_GOT_REF));
2928 return TRUE;
2931 /* This is a reference to a symbol defined by a dynamic object which
2932 is not a function. */
2934 /* If we are creating a shared library, we must presume that the
2935 only references to the symbol are via the global offset table.
2936 For such cases we need not do anything here; the relocations will
2937 be handled correctly by relocate_section. */
2938 if (info->shared)
2939 return TRUE;
2941 /* If there are no references to this symbol that do not use the
2942 GOT, we don't need to generate a copy reloc. */
2943 if ((h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0)
2944 return TRUE;
2946 if (ELIMINATE_COPY_RELOCS)
2948 struct ppc_elf_dyn_relocs *p;
2949 for (p = ppc_elf_hash_entry (h)->dyn_relocs; p != NULL; p = p->next)
2951 s = p->sec->output_section;
2952 if (s != NULL && (s->flags & SEC_READONLY) != 0)
2953 break;
2956 /* If we didn't find any dynamic relocs in read-only sections, then
2957 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
2958 if (p == NULL)
2960 h->elf_link_hash_flags &= ~ELF_LINK_NON_GOT_REF;
2961 return TRUE;
2965 /* We must allocate the symbol in our .dynbss section, which will
2966 become part of the .bss section of the executable. There will be
2967 an entry for this symbol in the .dynsym section. The dynamic
2968 object will contain position independent code, so all references
2969 from the dynamic object to this symbol will go through the global
2970 offset table. The dynamic linker will use the .dynsym entry to
2971 determine the address it must put in the global offset table, so
2972 both the dynamic object and the regular object will refer to the
2973 same memory location for the variable.
2975 Of course, if the symbol is sufficiently small, we must instead
2976 allocate it in .sbss. FIXME: It would be better to do this if and
2977 only if there were actually SDAREL relocs for that symbol. */
2979 if (h->size <= elf_gp_size (htab->elf.dynobj))
2980 s = htab->dynsbss;
2981 else
2982 s = htab->dynbss;
2983 BFD_ASSERT (s != NULL);
2985 /* We must generate a R_PPC_COPY reloc to tell the dynamic linker to
2986 copy the initial value out of the dynamic object and into the
2987 runtime process image. We need to remember the offset into the
2988 .rela.bss section we are going to use. */
2989 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
2991 asection *srel;
2993 if (h->size <= elf_gp_size (htab->elf.dynobj))
2994 srel = htab->relsbss;
2995 else
2996 srel = htab->relbss;
2997 BFD_ASSERT (srel != NULL);
2998 srel->size += sizeof (Elf32_External_Rela);
2999 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_COPY;
3002 /* We need to figure out the alignment required for this symbol. I
3003 have no idea how ELF linkers handle this. */
3004 power_of_two = bfd_log2 (h->size);
3005 if (power_of_two > 4)
3006 power_of_two = 4;
3008 /* Apply the required alignment. */
3009 s->size = BFD_ALIGN (s->size, (bfd_size_type) (1 << power_of_two));
3010 if (power_of_two > bfd_get_section_alignment (htab->elf.dynobj, s))
3012 if (! bfd_set_section_alignment (htab->elf.dynobj, s, power_of_two))
3013 return FALSE;
3016 /* Define the symbol as being at this point in the section. */
3017 h->root.u.def.section = s;
3018 h->root.u.def.value = s->size;
3020 /* Increment the section size to make room for the symbol. */
3021 s->size += h->size;
3023 return TRUE;
3026 /* Of those relocs that might be copied as dynamic relocs, this macro
3027 selects those that must be copied when linking a shared library,
3028 even when the symbol is local. */
3030 #define MUST_BE_DYN_RELOC(RTYPE) \
3031 ((RTYPE) != R_PPC_REL24 \
3032 && (RTYPE) != R_PPC_REL14 \
3033 && (RTYPE) != R_PPC_REL14_BRTAKEN \
3034 && (RTYPE) != R_PPC_REL14_BRNTAKEN \
3035 && (RTYPE) != R_PPC_REL32)
3037 /* Allocate space in associated reloc sections for dynamic relocs. */
3039 static bfd_boolean
3040 allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
3042 struct bfd_link_info *info = inf;
3043 struct ppc_elf_link_hash_entry *eh;
3044 struct ppc_elf_link_hash_table *htab;
3045 struct ppc_elf_dyn_relocs *p;
3047 if (h->root.type == bfd_link_hash_indirect)
3048 return TRUE;
3050 if (h->root.type == bfd_link_hash_warning)
3051 /* When warning symbols are created, they **replace** the "real"
3052 entry in the hash table, thus we never get to see the real
3053 symbol in a hash traversal. So look at it now. */
3054 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3056 htab = ppc_elf_hash_table (info);
3057 if (htab->elf.dynamic_sections_created
3058 && h->plt.refcount > 0)
3060 /* Make sure this symbol is output as a dynamic symbol. */
3061 if (h->dynindx == -1
3062 && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
3064 if (! bfd_elf_link_record_dynamic_symbol (info, h))
3065 return FALSE;
3068 if (info->shared
3069 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h))
3071 asection *s = htab->plt;
3073 /* If this is the first .plt entry, make room for the special
3074 first entry. */
3075 if (s->size == 0)
3076 s->size += PLT_INITIAL_ENTRY_SIZE;
3078 /* The PowerPC PLT is actually composed of two parts, the
3079 first part is 2 words (for a load and a jump), and then
3080 there is a remaining word available at the end. */
3081 h->plt.offset = (PLT_INITIAL_ENTRY_SIZE
3082 + (PLT_SLOT_SIZE
3083 * ((s->size - PLT_INITIAL_ENTRY_SIZE)
3084 / PLT_ENTRY_SIZE)));
3086 /* If this symbol is not defined in a regular file, and we
3087 are not generating a shared library, then set the symbol
3088 to this location in the .plt. This is required to make
3089 function pointers compare as equal between the normal
3090 executable and the shared library. */
3091 if (! info->shared
3092 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
3094 h->root.u.def.section = s;
3095 h->root.u.def.value = h->plt.offset;
3098 /* Make room for this entry. After the 8192nd entry, room
3099 for two entries is allocated. */
3100 s->size += PLT_ENTRY_SIZE;
3101 if ((s->size - PLT_INITIAL_ENTRY_SIZE) / PLT_ENTRY_SIZE
3102 > PLT_NUM_SINGLE_ENTRIES)
3103 s->size += PLT_ENTRY_SIZE;
3105 /* We also need to make an entry in the .rela.plt section. */
3106 htab->relplt->size += sizeof (Elf32_External_Rela);
3108 else
3110 h->plt.offset = (bfd_vma) -1;
3111 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
3114 else
3116 h->plt.offset = (bfd_vma) -1;
3117 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
3120 eh = (struct ppc_elf_link_hash_entry *) h;
3121 if (eh->elf.got.refcount > 0)
3123 /* Make sure this symbol is output as a dynamic symbol. */
3124 if (eh->elf.dynindx == -1
3125 && (eh->elf.elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
3127 if (!bfd_elf_link_record_dynamic_symbol (info, &eh->elf))
3128 return FALSE;
3131 if (eh->tls_mask == (TLS_TLS | TLS_LD)
3132 && !(eh->elf.elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC))
3133 /* If just an LD reloc, we'll just use htab->tlsld_got.offset. */
3134 eh->elf.got.offset = (bfd_vma) -1;
3135 else
3137 bfd_boolean dyn;
3138 eh->elf.got.offset = htab->got->size;
3139 if ((eh->tls_mask & TLS_TLS) != 0)
3141 if ((eh->tls_mask & TLS_LD) != 0)
3142 htab->got->size += 8;
3143 if ((eh->tls_mask & TLS_GD) != 0)
3144 htab->got->size += 8;
3145 if ((eh->tls_mask & (TLS_TPREL | TLS_TPRELGD)) != 0)
3146 htab->got->size += 4;
3147 if ((eh->tls_mask & TLS_DTPREL) != 0)
3148 htab->got->size += 4;
3150 else
3151 htab->got->size += 4;
3152 dyn = htab->elf.dynamic_sections_created;
3153 if ((info->shared
3154 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, &eh->elf))
3155 && (ELF_ST_VISIBILITY (eh->elf.other) == STV_DEFAULT
3156 || eh->elf.root.type != bfd_link_hash_undefweak))
3158 /* All the entries we allocated need relocs. */
3159 htab->relgot->size
3160 += ((htab->got->size - eh->elf.got.offset) / 4
3161 * sizeof (Elf32_External_Rela));
3162 /* Except LD only needs one. */
3163 if ((eh->tls_mask & TLS_LD) != 0)
3164 htab->relgot->size -= sizeof (Elf32_External_Rela);
3168 else
3169 eh->elf.got.offset = (bfd_vma) -1;
3171 if (eh->dyn_relocs == NULL)
3172 return TRUE;
3174 /* In the shared -Bsymbolic case, discard space allocated for
3175 dynamic pc-relative relocs against symbols which turn out to be
3176 defined in regular objects. For the normal shared case, discard
3177 space for relocs that have become local due to symbol visibility
3178 changes. */
3180 if (info->shared)
3182 /* Relocs that use pc_count are those that appear on a call insn,
3183 or certain REL relocs (see MUST_BE_DYN_RELOC) that can be
3184 generated via assembly. We want calls to protected symbols to
3185 resolve directly to the function rather than going via the plt.
3186 If people want function pointer comparisons to work as expected
3187 then they should avoid writing weird assembly. */
3188 if (SYMBOL_CALLS_LOCAL (info, h))
3190 struct ppc_elf_dyn_relocs **pp;
3192 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
3194 p->count -= p->pc_count;
3195 p->pc_count = 0;
3196 if (p->count == 0)
3197 *pp = p->next;
3198 else
3199 pp = &p->next;
3203 /* Also discard relocs on undefined weak syms with non-default
3204 visibility. */
3205 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
3206 && h->root.type == bfd_link_hash_undefweak)
3207 eh->dyn_relocs = NULL;
3209 /* Make sure undefined weak symbols are output as a dynamic symbol
3210 in PIEs. */
3211 if (info->pie
3212 && eh->dyn_relocs != NULL
3213 && h->dynindx == -1
3214 && h->root.type == bfd_link_hash_undefweak
3215 && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
3217 if (! bfd_elf_link_record_dynamic_symbol (info, h))
3218 return FALSE;
3221 else if (ELIMINATE_COPY_RELOCS)
3223 /* For the non-shared case, discard space for relocs against
3224 symbols which turn out to need copy relocs or are not
3225 dynamic. */
3227 if ((h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0
3228 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
3229 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
3231 /* Make sure this symbol is output as a dynamic symbol.
3232 Undefined weak syms won't yet be marked as dynamic. */
3233 if (h->dynindx == -1
3234 && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
3236 if (! bfd_elf_link_record_dynamic_symbol (info, h))
3237 return FALSE;
3240 /* If that succeeded, we know we'll be keeping all the
3241 relocs. */
3242 if (h->dynindx != -1)
3243 goto keep;
3246 eh->dyn_relocs = NULL;
3248 keep: ;
3251 /* Finally, allocate space. */
3252 for (p = eh->dyn_relocs; p != NULL; p = p->next)
3254 asection *sreloc = elf_section_data (p->sec)->sreloc;
3255 sreloc->size += p->count * sizeof (Elf32_External_Rela);
3258 return TRUE;
3261 /* Find any dynamic relocs that apply to read-only sections. */
3263 static bfd_boolean
3264 readonly_dynrelocs (struct elf_link_hash_entry *h, void *info)
3266 struct ppc_elf_dyn_relocs *p;
3268 if (h->root.type == bfd_link_hash_indirect)
3269 return TRUE;
3271 if (h->root.type == bfd_link_hash_warning)
3272 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3274 for (p = ppc_elf_hash_entry (h)->dyn_relocs; p != NULL; p = p->next)
3276 asection *s = p->sec->output_section;
3278 if (s != NULL
3279 && ((s->flags & (SEC_READONLY | SEC_ALLOC))
3280 == (SEC_READONLY | SEC_ALLOC)))
3282 ((struct bfd_link_info *) info)->flags |= DF_TEXTREL;
3284 /* Not an error, just cut short the traversal. */
3285 return FALSE;
3288 return TRUE;
3291 /* Set the sizes of the dynamic sections. */
3293 static bfd_boolean
3294 ppc_elf_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
3295 struct bfd_link_info *info)
3297 struct ppc_elf_link_hash_table *htab;
3298 asection *s;
3299 bfd_boolean relocs;
3300 bfd *ibfd;
3302 #ifdef DEBUG
3303 fprintf (stderr, "ppc_elf_size_dynamic_sections called\n");
3304 #endif
3306 htab = ppc_elf_hash_table (info);
3307 BFD_ASSERT (htab->elf.dynobj != NULL);
3309 if (elf_hash_table (info)->dynamic_sections_created)
3311 /* Set the contents of the .interp section to the interpreter. */
3312 if (info->executable)
3314 s = bfd_get_section_by_name (htab->elf.dynobj, ".interp");
3315 BFD_ASSERT (s != NULL);
3316 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
3317 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
3321 if (htab->tlsld_got.refcount > 0)
3323 htab->tlsld_got.offset = htab->got->size;
3324 htab->got->size += 8;
3325 if (info->shared)
3326 htab->relgot->size += sizeof (Elf32_External_Rela);
3328 else
3329 htab->tlsld_got.offset = (bfd_vma) -1;
3331 /* Set up .got offsets for local syms, and space for local dynamic
3332 relocs. */
3333 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
3335 bfd_signed_vma *local_got;
3336 bfd_signed_vma *end_local_got;
3337 char *lgot_masks;
3338 bfd_size_type locsymcount;
3339 Elf_Internal_Shdr *symtab_hdr;
3340 asection *srel;
3342 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
3343 continue;
3345 for (s = ibfd->sections; s != NULL; s = s->next)
3347 struct ppc_elf_dyn_relocs *p;
3349 for (p = ((struct ppc_elf_dyn_relocs *)
3350 elf_section_data (s)->local_dynrel);
3351 p != NULL;
3352 p = p->next)
3354 if (!bfd_is_abs_section (p->sec)
3355 && bfd_is_abs_section (p->sec->output_section))
3357 /* Input section has been discarded, either because
3358 it is a copy of a linkonce section or due to
3359 linker script /DISCARD/, so we'll be discarding
3360 the relocs too. */
3362 else if (p->count != 0)
3364 elf_section_data (p->sec)->sreloc->size
3365 += p->count * sizeof (Elf32_External_Rela);
3366 if ((p->sec->output_section->flags
3367 & (SEC_READONLY | SEC_ALLOC))
3368 == (SEC_READONLY | SEC_ALLOC))
3369 info->flags |= DF_TEXTREL;
3374 local_got = elf_local_got_refcounts (ibfd);
3375 if (!local_got)
3376 continue;
3378 symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
3379 locsymcount = symtab_hdr->sh_info;
3380 end_local_got = local_got + locsymcount;
3381 lgot_masks = (char *) end_local_got;
3382 s = htab->got;
3383 srel = htab->relgot;
3384 for (; local_got < end_local_got; ++local_got, ++lgot_masks)
3385 if (*local_got > 0)
3387 if (*lgot_masks == (TLS_TLS | TLS_LD))
3389 /* If just an LD reloc, we'll just use
3390 htab->tlsld_got.offset. */
3391 if (htab->tlsld_got.offset == (bfd_vma) -1)
3393 htab->tlsld_got.offset = s->size;
3394 s->size += 8;
3395 if (info->shared)
3396 srel->size += sizeof (Elf32_External_Rela);
3398 *local_got = (bfd_vma) -1;
3400 else
3402 *local_got = s->size;
3403 if ((*lgot_masks & TLS_TLS) != 0)
3405 if ((*lgot_masks & TLS_GD) != 0)
3406 s->size += 8;
3407 if ((*lgot_masks & (TLS_TPREL | TLS_TPRELGD)) != 0)
3408 s->size += 4;
3409 if ((*lgot_masks & TLS_DTPREL) != 0)
3410 s->size += 4;
3412 else
3413 s->size += 4;
3414 if (info->shared)
3415 srel->size += ((s->size - *local_got) / 4
3416 * sizeof (Elf32_External_Rela));
3419 else
3420 *local_got = (bfd_vma) -1;
3423 /* Allocate space for global sym dynamic relocs. */
3424 elf_link_hash_traverse (elf_hash_table (info), allocate_dynrelocs, info);
3426 /* We've now determined the sizes of the various dynamic sections.
3427 Allocate memory for them. */
3428 relocs = FALSE;
3429 for (s = htab->elf.dynobj->sections; s != NULL; s = s->next)
3431 if ((s->flags & SEC_LINKER_CREATED) == 0)
3432 continue;
3434 if (s == htab->plt
3435 || s == htab->got
3436 || (htab->sdata != NULL && s == htab->sdata->section)
3437 || (htab->sdata2 != NULL && s == htab->sdata2->section))
3439 /* Strip this section if we don't need it; see the
3440 comment below. */
3442 else if (strncmp (bfd_get_section_name (dynobj, s), ".rela", 5) == 0)
3444 if (s->size == 0)
3446 /* If we don't need this section, strip it from the
3447 output file. This is mostly to handle .rela.bss and
3448 .rela.plt. We must create both sections in
3449 create_dynamic_sections, because they must be created
3450 before the linker maps input sections to output
3451 sections. The linker does that before
3452 adjust_dynamic_symbol is called, and it is that
3453 function which decides whether anything needs to go
3454 into these sections. */
3456 else
3458 /* Remember whether there are any relocation sections. */
3459 relocs = TRUE;
3461 /* We use the reloc_count field as a counter if we need
3462 to copy relocs into the output file. */
3463 s->reloc_count = 0;
3466 else
3468 /* It's not one of our sections, so don't allocate space. */
3469 continue;
3472 if (s->size == 0)
3474 _bfd_strip_section_from_output (info, s);
3475 continue;
3478 /* Allocate memory for the section contents. */
3479 s->contents = bfd_zalloc (htab->elf.dynobj, s->size);
3480 if (s->contents == NULL)
3481 return FALSE;
3484 if (htab->elf.dynamic_sections_created)
3486 /* Add some entries to the .dynamic section. We fill in the
3487 values later, in ppc_elf_finish_dynamic_sections, but we
3488 must add the entries now so that we get the correct size for
3489 the .dynamic section. The DT_DEBUG entry is filled in by the
3490 dynamic linker and used by the debugger. */
3491 #define add_dynamic_entry(TAG, VAL) \
3492 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
3494 if (info->executable)
3496 if (!add_dynamic_entry (DT_DEBUG, 0))
3497 return FALSE;
3500 if (htab->plt != NULL && htab->plt->size != 0)
3502 if (!add_dynamic_entry (DT_PLTGOT, 0)
3503 || !add_dynamic_entry (DT_PLTRELSZ, 0)
3504 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
3505 || !add_dynamic_entry (DT_JMPREL, 0))
3506 return FALSE;
3509 if (relocs)
3511 if (!add_dynamic_entry (DT_RELA, 0)
3512 || !add_dynamic_entry (DT_RELASZ, 0)
3513 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf32_External_Rela)))
3514 return FALSE;
3517 /* If any dynamic relocs apply to a read-only section, then we
3518 need a DT_TEXTREL entry. */
3519 if ((info->flags & DF_TEXTREL) == 0)
3520 elf_link_hash_traverse (elf_hash_table (info), readonly_dynrelocs,
3521 info);
3523 if ((info->flags & DF_TEXTREL) != 0)
3525 if (!add_dynamic_entry (DT_TEXTREL, 0))
3526 return FALSE;
3529 #undef add_dynamic_entry
3531 return TRUE;
3534 static bfd_boolean
3535 update_local_sym_info (bfd *abfd,
3536 Elf_Internal_Shdr *symtab_hdr,
3537 unsigned long r_symndx,
3538 int tls_type)
3540 bfd_signed_vma *local_got_refcounts = elf_local_got_refcounts (abfd);
3541 char *local_got_tls_masks;
3543 if (local_got_refcounts == NULL)
3545 bfd_size_type size = symtab_hdr->sh_info;
3547 size *= sizeof (*local_got_refcounts) + sizeof (*local_got_tls_masks);
3548 local_got_refcounts = bfd_zalloc (abfd, size);
3549 if (local_got_refcounts == NULL)
3550 return FALSE;
3551 elf_local_got_refcounts (abfd) = local_got_refcounts;
3554 local_got_refcounts[r_symndx] += 1;
3555 local_got_tls_masks = (char *) (local_got_refcounts + symtab_hdr->sh_info);
3556 local_got_tls_masks[r_symndx] |= tls_type;
3557 return TRUE;
3560 static void
3561 bad_shared_reloc (bfd *abfd, enum elf_ppc_reloc_type r_type)
3563 (*_bfd_error_handler)
3564 (_("%s: relocation %s cannot be used when making a shared object"),
3565 bfd_archive_filename (abfd),
3566 ppc_elf_howto_table[r_type]->name);
3567 bfd_set_error (bfd_error_bad_value);
3570 /* Look through the relocs for a section during the first phase, and
3571 allocate space in the global offset table or procedure linkage
3572 table. */
3574 static bfd_boolean
3575 ppc_elf_check_relocs (bfd *abfd,
3576 struct bfd_link_info *info,
3577 asection *sec,
3578 const Elf_Internal_Rela *relocs)
3580 struct ppc_elf_link_hash_table *htab;
3581 Elf_Internal_Shdr *symtab_hdr;
3582 struct elf_link_hash_entry **sym_hashes;
3583 const Elf_Internal_Rela *rel;
3584 const Elf_Internal_Rela *rel_end;
3585 asection *sreloc;
3587 if (info->relocatable)
3588 return TRUE;
3590 #ifdef DEBUG
3591 fprintf (stderr, "ppc_elf_check_relocs called for section %s in %s\n",
3592 bfd_get_section_name (abfd, sec),
3593 bfd_archive_filename (abfd));
3594 #endif
3596 /* Initialize howto table if not already done. */
3597 if (!ppc_elf_howto_table[R_PPC_ADDR32])
3598 ppc_elf_howto_init ();
3600 /* Create the linker generated sections all the time so that the
3601 special symbols are created. */
3602 htab = ppc_elf_hash_table (info);
3603 if (htab->sdata == NULL)
3605 htab->sdata = ppc_elf_create_linker_section (abfd, info,
3606 LINKER_SECTION_SDATA);
3607 if (htab->sdata == NULL)
3608 return FALSE;
3611 if (htab->sdata2 == NULL)
3613 htab->sdata2 = ppc_elf_create_linker_section (abfd, info,
3614 LINKER_SECTION_SDATA2);
3615 if (htab->sdata2 == NULL)
3616 return FALSE;
3619 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
3620 sym_hashes = elf_sym_hashes (abfd);
3621 sreloc = NULL;
3623 rel_end = relocs + sec->reloc_count;
3624 for (rel = relocs; rel < rel_end; rel++)
3626 unsigned long r_symndx;
3627 enum elf_ppc_reloc_type r_type;
3628 struct elf_link_hash_entry *h;
3629 int tls_type = 0;
3631 r_symndx = ELF32_R_SYM (rel->r_info);
3632 if (r_symndx < symtab_hdr->sh_info)
3633 h = NULL;
3634 else
3635 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
3637 /* If a relocation refers to _GLOBAL_OFFSET_TABLE_, create the .got.
3638 This shows up in particular in an R_PPC_ADDR32 in the eabi
3639 startup code. */
3640 if (h && strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
3642 if (htab->got == NULL)
3644 if (htab->elf.dynobj == NULL)
3645 htab->elf.dynobj = abfd;
3646 if (!ppc_elf_create_got (htab->elf.dynobj, info))
3647 return FALSE;
3651 r_type = ELF32_R_TYPE (rel->r_info);
3652 switch (r_type)
3654 case R_PPC_GOT_TLSLD16:
3655 case R_PPC_GOT_TLSLD16_LO:
3656 case R_PPC_GOT_TLSLD16_HI:
3657 case R_PPC_GOT_TLSLD16_HA:
3658 htab->tlsld_got.refcount += 1;
3659 tls_type = TLS_TLS | TLS_LD;
3660 goto dogottls;
3662 case R_PPC_GOT_TLSGD16:
3663 case R_PPC_GOT_TLSGD16_LO:
3664 case R_PPC_GOT_TLSGD16_HI:
3665 case R_PPC_GOT_TLSGD16_HA:
3666 tls_type = TLS_TLS | TLS_GD;
3667 goto dogottls;
3669 case R_PPC_GOT_TPREL16:
3670 case R_PPC_GOT_TPREL16_LO:
3671 case R_PPC_GOT_TPREL16_HI:
3672 case R_PPC_GOT_TPREL16_HA:
3673 if (info->shared)
3674 info->flags |= DF_STATIC_TLS;
3675 tls_type = TLS_TLS | TLS_TPREL;
3676 goto dogottls;
3678 case R_PPC_GOT_DTPREL16:
3679 case R_PPC_GOT_DTPREL16_LO:
3680 case R_PPC_GOT_DTPREL16_HI:
3681 case R_PPC_GOT_DTPREL16_HA:
3682 tls_type = TLS_TLS | TLS_DTPREL;
3683 dogottls:
3684 sec->has_tls_reloc = 1;
3685 /* Fall thru */
3687 /* GOT16 relocations */
3688 case R_PPC_GOT16:
3689 case R_PPC_GOT16_LO:
3690 case R_PPC_GOT16_HI:
3691 case R_PPC_GOT16_HA:
3692 /* This symbol requires a global offset table entry. */
3693 if (htab->got == NULL)
3695 if (htab->elf.dynobj == NULL)
3696 htab->elf.dynobj = abfd;
3697 if (!ppc_elf_create_got (htab->elf.dynobj, info))
3698 return FALSE;
3700 if (h != NULL)
3702 h->got.refcount += 1;
3703 ppc_elf_hash_entry (h)->tls_mask |= tls_type;
3705 else
3706 /* This is a global offset table entry for a local symbol. */
3707 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx, tls_type))
3708 return FALSE;
3709 break;
3711 /* Indirect .sdata relocation. */
3712 case R_PPC_EMB_SDAI16:
3713 if (info->shared)
3715 bad_shared_reloc (abfd, r_type);
3716 return FALSE;
3718 if (!elf_create_pointer_linker_section (abfd, info,
3719 htab->sdata, h, rel))
3720 return FALSE;
3721 break;
3723 /* Indirect .sdata2 relocation. */
3724 case R_PPC_EMB_SDA2I16:
3725 if (info->shared)
3727 bad_shared_reloc (abfd, r_type);
3728 return FALSE;
3730 if (!elf_create_pointer_linker_section (abfd, info,
3731 htab->sdata2, h, rel))
3732 return FALSE;
3733 break;
3735 case R_PPC_SDAREL16:
3736 case R_PPC_EMB_SDA2REL:
3737 case R_PPC_EMB_SDA21:
3738 case R_PPC_EMB_RELSDA:
3739 case R_PPC_EMB_NADDR32:
3740 case R_PPC_EMB_NADDR16:
3741 case R_PPC_EMB_NADDR16_LO:
3742 case R_PPC_EMB_NADDR16_HI:
3743 case R_PPC_EMB_NADDR16_HA:
3744 if (info->shared)
3746 bad_shared_reloc (abfd, r_type);
3747 return FALSE;
3749 break;
3751 case R_PPC_PLT32:
3752 case R_PPC_PLTREL24:
3753 case R_PPC_PLTREL32:
3754 case R_PPC_PLT16_LO:
3755 case R_PPC_PLT16_HI:
3756 case R_PPC_PLT16_HA:
3757 #ifdef DEBUG
3758 fprintf (stderr, "Reloc requires a PLT entry\n");
3759 #endif
3760 /* This symbol requires a procedure linkage table entry. We
3761 actually build the entry in finish_dynamic_symbol,
3762 because this might be a case of linking PIC code without
3763 linking in any dynamic objects, in which case we don't
3764 need to generate a procedure linkage table after all. */
3766 if (h == NULL)
3768 /* It does not make sense to have a procedure linkage
3769 table entry for a local symbol. */
3770 (*_bfd_error_handler) (_("%s(%s+0x%lx): %s reloc against "
3771 "local symbol"),
3772 bfd_archive_filename (abfd),
3773 sec->name,
3774 (long) rel->r_offset,
3775 ppc_elf_howto_table[r_type]->name);
3776 bfd_set_error (bfd_error_bad_value);
3777 return FALSE;
3780 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
3781 h->plt.refcount++;
3782 break;
3784 /* The following relocations don't need to propagate the
3785 relocation if linking a shared object since they are
3786 section relative. */
3787 case R_PPC_SECTOFF:
3788 case R_PPC_SECTOFF_LO:
3789 case R_PPC_SECTOFF_HI:
3790 case R_PPC_SECTOFF_HA:
3791 case R_PPC_DTPREL16:
3792 case R_PPC_DTPREL16_LO:
3793 case R_PPC_DTPREL16_HI:
3794 case R_PPC_DTPREL16_HA:
3795 case R_PPC_TOC16:
3796 break;
3798 /* This are just markers. */
3799 case R_PPC_TLS:
3800 case R_PPC_EMB_MRKREF:
3801 case R_PPC_NONE:
3802 case R_PPC_max:
3803 break;
3805 /* These should only appear in dynamic objects. */
3806 case R_PPC_COPY:
3807 case R_PPC_GLOB_DAT:
3808 case R_PPC_JMP_SLOT:
3809 case R_PPC_RELATIVE:
3810 break;
3812 /* These aren't handled yet. We'll report an error later. */
3813 case R_PPC_ADDR30:
3814 case R_PPC_EMB_RELSEC16:
3815 case R_PPC_EMB_RELST_LO:
3816 case R_PPC_EMB_RELST_HI:
3817 case R_PPC_EMB_RELST_HA:
3818 case R_PPC_EMB_BIT_FLD:
3819 break;
3821 /* This refers only to functions defined in the shared library. */
3822 case R_PPC_LOCAL24PC:
3823 break;
3825 /* This relocation describes the C++ object vtable hierarchy.
3826 Reconstruct it for later use during GC. */
3827 case R_PPC_GNU_VTINHERIT:
3828 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
3829 return FALSE;
3830 break;
3832 /* This relocation describes which C++ vtable entries are actually
3833 used. Record for later use during GC. */
3834 case R_PPC_GNU_VTENTRY:
3835 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
3836 return FALSE;
3837 break;
3839 /* We shouldn't really be seeing these. */
3840 case R_PPC_TPREL32:
3841 if (info->shared)
3842 info->flags |= DF_STATIC_TLS;
3843 goto dodyn;
3845 /* Nor these. */
3846 case R_PPC_DTPMOD32:
3847 case R_PPC_DTPREL32:
3848 goto dodyn;
3850 case R_PPC_TPREL16:
3851 case R_PPC_TPREL16_LO:
3852 case R_PPC_TPREL16_HI:
3853 case R_PPC_TPREL16_HA:
3854 if (info->shared)
3855 info->flags |= DF_STATIC_TLS;
3856 goto dodyn;
3858 /* When creating a shared object, we must copy these
3859 relocs into the output file. We create a reloc
3860 section in dynobj and make room for the reloc. */
3861 case R_PPC_REL24:
3862 case R_PPC_REL14:
3863 case R_PPC_REL14_BRTAKEN:
3864 case R_PPC_REL14_BRNTAKEN:
3865 case R_PPC_REL32:
3866 if (h == NULL
3867 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
3868 break;
3869 /* fall through */
3871 case R_PPC_ADDR32:
3872 case R_PPC_ADDR24:
3873 case R_PPC_ADDR16:
3874 case R_PPC_ADDR16_LO:
3875 case R_PPC_ADDR16_HI:
3876 case R_PPC_ADDR16_HA:
3877 case R_PPC_ADDR14:
3878 case R_PPC_ADDR14_BRTAKEN:
3879 case R_PPC_ADDR14_BRNTAKEN:
3880 case R_PPC_UADDR32:
3881 case R_PPC_UADDR16:
3882 if (h != NULL && !info->shared)
3884 /* We may need a plt entry if the symbol turns out to be
3885 a function defined in a dynamic object. */
3886 h->plt.refcount++;
3888 /* We may need a copy reloc too. */
3889 h->elf_link_hash_flags |= ELF_LINK_NON_GOT_REF;
3892 dodyn:
3893 /* If we are creating a shared library, and this is a reloc
3894 against a global symbol, or a non PC relative reloc
3895 against a local symbol, then we need to copy the reloc
3896 into the shared library. However, if we are linking with
3897 -Bsymbolic, we do not need to copy a reloc against a
3898 global symbol which is defined in an object we are
3899 including in the link (i.e., DEF_REGULAR is set). At
3900 this point we have not seen all the input files, so it is
3901 possible that DEF_REGULAR is not set now but will be set
3902 later (it is never cleared). In case of a weak definition,
3903 DEF_REGULAR may be cleared later by a strong definition in
3904 a shared library. We account for that possibility below by
3905 storing information in the dyn_relocs field of the hash
3906 table entry. A similar situation occurs when creating
3907 shared libraries and symbol visibility changes render the
3908 symbol local.
3910 If on the other hand, we are creating an executable, we
3911 may need to keep relocations for symbols satisfied by a
3912 dynamic library if we manage to avoid copy relocs for the
3913 symbol. */
3914 if ((info->shared
3915 && (MUST_BE_DYN_RELOC (r_type)
3916 || (h != NULL
3917 && (! info->symbolic
3918 || h->root.type == bfd_link_hash_defweak
3919 || (h->elf_link_hash_flags
3920 & ELF_LINK_HASH_DEF_REGULAR) == 0))))
3921 || (ELIMINATE_COPY_RELOCS
3922 && !info->shared
3923 && (sec->flags & SEC_ALLOC) != 0
3924 && h != NULL
3925 && (h->root.type == bfd_link_hash_defweak
3926 || (h->elf_link_hash_flags
3927 & ELF_LINK_HASH_DEF_REGULAR) == 0)))
3929 struct ppc_elf_dyn_relocs *p;
3930 struct ppc_elf_dyn_relocs **head;
3932 #ifdef DEBUG
3933 fprintf (stderr,
3934 "ppc_elf_check_relocs needs to "
3935 "create relocation for %s\n",
3936 (h && h->root.root.string
3937 ? h->root.root.string : "<unknown>"));
3938 #endif
3939 if (sreloc == NULL)
3941 const char *name;
3943 name = (bfd_elf_string_from_elf_section
3944 (abfd,
3945 elf_elfheader (abfd)->e_shstrndx,
3946 elf_section_data (sec)->rel_hdr.sh_name));
3947 if (name == NULL)
3948 return FALSE;
3950 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
3951 && strcmp (bfd_get_section_name (abfd, sec),
3952 name + 5) == 0);
3954 sreloc = bfd_get_section_by_name (htab->elf.dynobj, name);
3955 if (sreloc == NULL)
3957 flagword flags;
3959 sreloc = bfd_make_section (htab->elf.dynobj, name);
3960 flags = (SEC_HAS_CONTENTS | SEC_READONLY
3961 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3962 if ((sec->flags & SEC_ALLOC) != 0)
3963 flags |= SEC_ALLOC | SEC_LOAD;
3964 if (sreloc == NULL
3965 || ! bfd_set_section_flags (htab->elf.dynobj,
3966 sreloc, flags)
3967 || ! bfd_set_section_alignment (htab->elf.dynobj,
3968 sreloc, 2))
3969 return FALSE;
3971 elf_section_data (sec)->sreloc = sreloc;
3974 /* If this is a global symbol, we count the number of
3975 relocations we need for this symbol. */
3976 if (h != NULL)
3978 head = &ppc_elf_hash_entry (h)->dyn_relocs;
3980 else
3982 /* Track dynamic relocs needed for local syms too.
3983 We really need local syms available to do this
3984 easily. Oh well. */
3986 asection *s;
3987 s = bfd_section_from_r_symndx (abfd, &htab->sym_sec,
3988 sec, r_symndx);
3989 if (s == NULL)
3990 return FALSE;
3992 head = ((struct ppc_elf_dyn_relocs **)
3993 &elf_section_data (s)->local_dynrel);
3996 p = *head;
3997 if (p == NULL || p->sec != sec)
3999 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
4000 if (p == NULL)
4001 return FALSE;
4002 p->next = *head;
4003 *head = p;
4004 p->sec = sec;
4005 p->count = 0;
4006 p->pc_count = 0;
4009 p->count += 1;
4010 if (!MUST_BE_DYN_RELOC (r_type))
4011 p->pc_count += 1;
4014 break;
4018 return TRUE;
4021 /* Return the section that should be marked against GC for a given
4022 relocation. */
4024 static asection *
4025 ppc_elf_gc_mark_hook (asection *sec,
4026 struct bfd_link_info *info ATTRIBUTE_UNUSED,
4027 Elf_Internal_Rela *rel,
4028 struct elf_link_hash_entry *h,
4029 Elf_Internal_Sym *sym)
4031 if (h != NULL)
4033 switch (ELF32_R_TYPE (rel->r_info))
4035 case R_PPC_GNU_VTINHERIT:
4036 case R_PPC_GNU_VTENTRY:
4037 break;
4039 default:
4040 switch (h->root.type)
4042 case bfd_link_hash_defined:
4043 case bfd_link_hash_defweak:
4044 return h->root.u.def.section;
4046 case bfd_link_hash_common:
4047 return h->root.u.c.p->section;
4049 default:
4050 break;
4054 else
4055 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
4057 return NULL;
4060 /* Update the got, plt and dynamic reloc reference counts for the
4061 section being removed. */
4063 static bfd_boolean
4064 ppc_elf_gc_sweep_hook (bfd *abfd,
4065 struct bfd_link_info *info,
4066 asection *sec,
4067 const Elf_Internal_Rela *relocs)
4069 struct ppc_elf_link_hash_table *htab;
4070 Elf_Internal_Shdr *symtab_hdr;
4071 struct elf_link_hash_entry **sym_hashes;
4072 bfd_signed_vma *local_got_refcounts;
4073 const Elf_Internal_Rela *rel, *relend;
4075 elf_section_data (sec)->local_dynrel = NULL;
4077 htab = ppc_elf_hash_table (info);
4078 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
4079 sym_hashes = elf_sym_hashes (abfd);
4080 local_got_refcounts = elf_local_got_refcounts (abfd);
4082 relend = relocs + sec->reloc_count;
4083 for (rel = relocs; rel < relend; rel++)
4085 unsigned long r_symndx;
4086 enum elf_ppc_reloc_type r_type;
4087 struct elf_link_hash_entry *h = NULL;
4089 r_symndx = ELF32_R_SYM (rel->r_info);
4090 if (r_symndx >= symtab_hdr->sh_info)
4092 struct ppc_elf_dyn_relocs **pp, *p;
4093 struct ppc_elf_link_hash_entry *eh;
4095 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
4096 eh = (struct ppc_elf_link_hash_entry *) h;
4098 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
4099 if (p->sec == sec)
4101 /* Everything must go for SEC. */
4102 *pp = p->next;
4103 break;
4107 r_type = ELF32_R_TYPE (rel->r_info);
4108 switch (r_type)
4110 case R_PPC_GOT_TLSLD16:
4111 case R_PPC_GOT_TLSLD16_LO:
4112 case R_PPC_GOT_TLSLD16_HI:
4113 case R_PPC_GOT_TLSLD16_HA:
4114 htab->tlsld_got.refcount -= 1;
4115 /* Fall thru */
4117 case R_PPC_GOT_TLSGD16:
4118 case R_PPC_GOT_TLSGD16_LO:
4119 case R_PPC_GOT_TLSGD16_HI:
4120 case R_PPC_GOT_TLSGD16_HA:
4121 case R_PPC_GOT_TPREL16:
4122 case R_PPC_GOT_TPREL16_LO:
4123 case R_PPC_GOT_TPREL16_HI:
4124 case R_PPC_GOT_TPREL16_HA:
4125 case R_PPC_GOT_DTPREL16:
4126 case R_PPC_GOT_DTPREL16_LO:
4127 case R_PPC_GOT_DTPREL16_HI:
4128 case R_PPC_GOT_DTPREL16_HA:
4129 case R_PPC_GOT16:
4130 case R_PPC_GOT16_LO:
4131 case R_PPC_GOT16_HI:
4132 case R_PPC_GOT16_HA:
4133 if (h != NULL)
4135 if (h->got.refcount > 0)
4136 h->got.refcount--;
4138 else if (local_got_refcounts != NULL)
4140 if (local_got_refcounts[r_symndx] > 0)
4141 local_got_refcounts[r_symndx]--;
4143 break;
4145 case R_PPC_REL24:
4146 case R_PPC_REL14:
4147 case R_PPC_REL14_BRTAKEN:
4148 case R_PPC_REL14_BRNTAKEN:
4149 case R_PPC_REL32:
4150 if (h == NULL
4151 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
4152 break;
4153 /* Fall thru */
4155 case R_PPC_ADDR32:
4156 case R_PPC_ADDR24:
4157 case R_PPC_ADDR16:
4158 case R_PPC_ADDR16_LO:
4159 case R_PPC_ADDR16_HI:
4160 case R_PPC_ADDR16_HA:
4161 case R_PPC_ADDR14:
4162 case R_PPC_ADDR14_BRTAKEN:
4163 case R_PPC_ADDR14_BRNTAKEN:
4164 case R_PPC_UADDR32:
4165 case R_PPC_UADDR16:
4166 case R_PPC_PLT32:
4167 case R_PPC_PLTREL24:
4168 case R_PPC_PLT16_LO:
4169 case R_PPC_PLT16_HI:
4170 case R_PPC_PLT16_HA:
4171 if (h != NULL)
4173 if (h->plt.refcount > 0)
4174 h->plt.refcount--;
4176 break;
4178 default:
4179 break;
4182 return TRUE;
4185 /* Set htab->tls_get_addr and call the generic ELF tls_setup function. */
4187 asection *
4188 ppc_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
4190 struct ppc_elf_link_hash_table *htab;
4192 htab = ppc_elf_hash_table (info);
4193 htab->tls_get_addr = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
4194 FALSE, FALSE, TRUE);
4196 return _bfd_elf_tls_setup (obfd, info);
4199 /* Run through all the TLS relocs looking for optimization
4200 opportunities. */
4202 bfd_boolean
4203 ppc_elf_tls_optimize (bfd *obfd ATTRIBUTE_UNUSED,
4204 struct bfd_link_info *info)
4206 bfd *ibfd;
4207 asection *sec;
4208 struct ppc_elf_link_hash_table *htab;
4210 if (info->relocatable || info->shared)
4211 return TRUE;
4213 htab = ppc_elf_hash_table (info);
4214 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
4216 Elf_Internal_Sym *locsyms = NULL;
4217 Elf_Internal_Shdr *symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
4219 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
4220 if (sec->has_tls_reloc && !bfd_is_abs_section (sec->output_section))
4222 Elf_Internal_Rela *relstart, *rel, *relend;
4223 int expecting_tls_get_addr;
4225 /* Read the relocations. */
4226 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
4227 info->keep_memory);
4228 if (relstart == NULL)
4229 return FALSE;
4231 expecting_tls_get_addr = 0;
4232 relend = relstart + sec->reloc_count;
4233 for (rel = relstart; rel < relend; rel++)
4235 enum elf_ppc_reloc_type r_type;
4236 unsigned long r_symndx;
4237 struct elf_link_hash_entry *h = NULL;
4238 char *tls_mask;
4239 char tls_set, tls_clear;
4240 bfd_boolean is_local;
4242 r_symndx = ELF32_R_SYM (rel->r_info);
4243 if (r_symndx >= symtab_hdr->sh_info)
4245 struct elf_link_hash_entry **sym_hashes;
4247 sym_hashes = elf_sym_hashes (ibfd);
4248 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
4249 while (h->root.type == bfd_link_hash_indirect
4250 || h->root.type == bfd_link_hash_warning)
4251 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4254 is_local = FALSE;
4255 if (h == NULL
4256 || !(h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC))
4257 is_local = TRUE;
4259 r_type = ELF32_R_TYPE (rel->r_info);
4260 switch (r_type)
4262 case R_PPC_GOT_TLSLD16:
4263 case R_PPC_GOT_TLSLD16_LO:
4264 case R_PPC_GOT_TLSLD16_HI:
4265 case R_PPC_GOT_TLSLD16_HA:
4266 /* These relocs should never be against a symbol
4267 defined in a shared lib. Leave them alone if
4268 that turns out to be the case. */
4269 expecting_tls_get_addr = 0;
4270 htab->tlsld_got.refcount -= 1;
4271 if (!is_local)
4272 continue;
4274 /* LD -> LE */
4275 tls_set = 0;
4276 tls_clear = TLS_LD;
4277 expecting_tls_get_addr = 1;
4278 break;
4280 case R_PPC_GOT_TLSGD16:
4281 case R_PPC_GOT_TLSGD16_LO:
4282 case R_PPC_GOT_TLSGD16_HI:
4283 case R_PPC_GOT_TLSGD16_HA:
4284 if (is_local)
4285 /* GD -> LE */
4286 tls_set = 0;
4287 else
4288 /* GD -> IE */
4289 tls_set = TLS_TLS | TLS_TPRELGD;
4290 tls_clear = TLS_GD;
4291 expecting_tls_get_addr = 1;
4292 break;
4294 case R_PPC_GOT_TPREL16:
4295 case R_PPC_GOT_TPREL16_LO:
4296 case R_PPC_GOT_TPREL16_HI:
4297 case R_PPC_GOT_TPREL16_HA:
4298 expecting_tls_get_addr = 0;
4299 if (is_local)
4301 /* IE -> LE */
4302 tls_set = 0;
4303 tls_clear = TLS_TPREL;
4304 break;
4306 else
4307 continue;
4309 case R_PPC_REL14:
4310 case R_PPC_REL14_BRTAKEN:
4311 case R_PPC_REL14_BRNTAKEN:
4312 case R_PPC_REL24:
4313 if (expecting_tls_get_addr
4314 && h != NULL
4315 && h == htab->tls_get_addr)
4317 if (h->plt.refcount > 0)
4318 h->plt.refcount -= 1;
4320 expecting_tls_get_addr = 0;
4321 continue;
4323 default:
4324 expecting_tls_get_addr = 0;
4325 continue;
4328 if (h != NULL)
4330 if (tls_set == 0)
4332 /* We managed to get rid of a got entry. */
4333 if (h->got.refcount > 0)
4334 h->got.refcount -= 1;
4336 tls_mask = &ppc_elf_hash_entry (h)->tls_mask;
4338 else
4340 Elf_Internal_Sym *sym;
4341 bfd_signed_vma *lgot_refs;
4342 char *lgot_masks;
4344 if (locsyms == NULL)
4346 locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
4347 if (locsyms == NULL)
4348 locsyms = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
4349 symtab_hdr->sh_info,
4350 0, NULL, NULL, NULL);
4351 if (locsyms == NULL)
4353 if (elf_section_data (sec)->relocs != relstart)
4354 free (relstart);
4355 return FALSE;
4358 sym = locsyms + r_symndx;
4359 lgot_refs = elf_local_got_refcounts (ibfd);
4360 if (lgot_refs == NULL)
4361 abort ();
4362 if (tls_set == 0)
4364 /* We managed to get rid of a got entry. */
4365 if (lgot_refs[r_symndx] > 0)
4366 lgot_refs[r_symndx] -= 1;
4368 lgot_masks = (char *) (lgot_refs + symtab_hdr->sh_info);
4369 tls_mask = &lgot_masks[r_symndx];
4372 *tls_mask |= tls_set;
4373 *tls_mask &= ~tls_clear;
4376 if (elf_section_data (sec)->relocs != relstart)
4377 free (relstart);
4380 if (locsyms != NULL
4381 && (symtab_hdr->contents != (unsigned char *) locsyms))
4383 if (!info->keep_memory)
4384 free (locsyms);
4385 else
4386 symtab_hdr->contents = (unsigned char *) locsyms;
4389 return TRUE;
4392 /* Hook called by the linker routine which adds symbols from an object
4393 file. We use it to put .comm items in .sbss, and not .bss. */
4395 static bfd_boolean
4396 ppc_elf_add_symbol_hook (bfd *abfd,
4397 struct bfd_link_info *info,
4398 Elf_Internal_Sym *sym,
4399 const char **namep ATTRIBUTE_UNUSED,
4400 flagword *flagsp ATTRIBUTE_UNUSED,
4401 asection **secp,
4402 bfd_vma *valp)
4404 if (sym->st_shndx == SHN_COMMON
4405 && !info->relocatable
4406 && sym->st_size <= elf_gp_size (abfd)
4407 && (info->hash->creator == abfd->xvec
4408 || info->hash->creator == abfd->xvec->alternative_target))
4410 /* Common symbols less than or equal to -G nn bytes are automatically
4411 put into .sbss. */
4412 struct ppc_elf_link_hash_table *htab;
4414 htab = ppc_elf_hash_table (info);
4415 if (htab->sbss == NULL)
4417 flagword flags = SEC_IS_COMMON;
4419 htab->sbss = bfd_make_section_anyway (abfd, ".sbss");
4420 if (htab->sbss == NULL
4421 || ! bfd_set_section_flags (abfd, htab->sbss, flags))
4422 return FALSE;
4425 *secp = htab->sbss;
4426 *valp = sym->st_size;
4429 return TRUE;
4432 /* Finish up dynamic symbol handling. We set the contents of various
4433 dynamic sections here. */
4435 static bfd_boolean
4436 ppc_elf_finish_dynamic_symbol (bfd *output_bfd,
4437 struct bfd_link_info *info,
4438 struct elf_link_hash_entry *h,
4439 Elf_Internal_Sym *sym)
4441 struct ppc_elf_link_hash_table *htab;
4443 #ifdef DEBUG
4444 fprintf (stderr, "ppc_elf_finish_dynamic_symbol called for %s",
4445 h->root.root.string);
4446 #endif
4448 htab = ppc_elf_hash_table (info);
4449 BFD_ASSERT (htab->elf.dynobj != NULL);
4451 if (h->plt.offset != (bfd_vma) -1)
4453 Elf_Internal_Rela rela;
4454 bfd_byte *loc;
4455 bfd_vma reloc_index;
4457 #ifdef DEBUG
4458 fprintf (stderr, ", plt_offset = %d", h->plt.offset);
4459 #endif
4461 /* This symbol has an entry in the procedure linkage table. Set
4462 it up. */
4464 BFD_ASSERT (h->dynindx != -1);
4465 BFD_ASSERT (htab->plt != NULL && htab->relplt != NULL);
4467 /* We don't need to fill in the .plt. The ppc dynamic linker
4468 will fill it in. */
4470 /* Fill in the entry in the .rela.plt section. */
4471 rela.r_offset = (htab->plt->output_section->vma
4472 + htab->plt->output_offset
4473 + h->plt.offset);
4474 rela.r_info = ELF32_R_INFO (h->dynindx, R_PPC_JMP_SLOT);
4475 rela.r_addend = 0;
4477 reloc_index = (h->plt.offset - PLT_INITIAL_ENTRY_SIZE) / PLT_SLOT_SIZE;
4478 if (reloc_index > PLT_NUM_SINGLE_ENTRIES)
4479 reloc_index -= (reloc_index - PLT_NUM_SINGLE_ENTRIES) / 2;
4480 loc = (htab->relplt->contents
4481 + reloc_index * sizeof (Elf32_External_Rela));
4482 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
4484 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
4486 /* Mark the symbol as undefined, rather than as defined in
4487 the .plt section. Leave the value alone. */
4488 sym->st_shndx = SHN_UNDEF;
4489 /* If the symbol is weak, we do need to clear the value.
4490 Otherwise, the PLT entry would provide a definition for
4491 the symbol even if the symbol wasn't defined anywhere,
4492 and so the symbol would never be NULL. */
4493 if ((h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR_NONWEAK)
4494 == 0)
4495 sym->st_value = 0;
4499 if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_COPY) != 0)
4501 asection *s;
4502 Elf_Internal_Rela rela;
4503 bfd_byte *loc;
4505 /* This symbols needs a copy reloc. Set it up. */
4507 #ifdef DEBUG
4508 fprintf (stderr, ", copy");
4509 #endif
4511 BFD_ASSERT (h->dynindx != -1);
4513 if (h->size <= elf_gp_size (htab->elf.dynobj))
4514 s = htab->relsbss;
4515 else
4516 s = htab->relbss;
4517 BFD_ASSERT (s != NULL);
4519 rela.r_offset = (h->root.u.def.value
4520 + h->root.u.def.section->output_section->vma
4521 + h->root.u.def.section->output_offset);
4522 rela.r_info = ELF32_R_INFO (h->dynindx, R_PPC_COPY);
4523 rela.r_addend = 0;
4524 loc = s->contents + s->reloc_count++ * sizeof (Elf32_External_Rela);
4525 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
4528 #ifdef DEBUG
4529 fprintf (stderr, "\n");
4530 #endif
4532 /* Mark some specially defined symbols as absolute. */
4533 if (strcmp (h->root.root.string, "_DYNAMIC") == 0
4534 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0
4535 || strcmp (h->root.root.string, "_PROCEDURE_LINKAGE_TABLE_") == 0)
4536 sym->st_shndx = SHN_ABS;
4538 return TRUE;
4541 /* Finish up the dynamic sections. */
4543 static bfd_boolean
4544 ppc_elf_finish_dynamic_sections (bfd *output_bfd,
4545 struct bfd_link_info *info)
4547 asection *sdyn;
4548 struct ppc_elf_link_hash_table *htab;
4550 #ifdef DEBUG
4551 fprintf (stderr, "ppc_elf_finish_dynamic_sections called\n");
4552 #endif
4554 htab = ppc_elf_hash_table (info);
4555 sdyn = bfd_get_section_by_name (htab->elf.dynobj, ".dynamic");
4557 if (htab->elf.dynamic_sections_created)
4559 Elf32_External_Dyn *dyncon, *dynconend;
4561 BFD_ASSERT (htab->plt != NULL && sdyn != NULL);
4563 dyncon = (Elf32_External_Dyn *) sdyn->contents;
4564 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
4565 for (; dyncon < dynconend; dyncon++)
4567 Elf_Internal_Dyn dyn;
4568 asection *s;
4570 bfd_elf32_swap_dyn_in (htab->elf.dynobj, dyncon, &dyn);
4572 switch (dyn.d_tag)
4574 case DT_PLTGOT:
4575 s = htab->plt;
4576 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
4577 break;
4579 case DT_PLTRELSZ:
4580 dyn.d_un.d_val = htab->relplt->size;
4581 break;
4583 case DT_JMPREL:
4584 s = htab->relplt;
4585 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
4586 break;
4588 default:
4589 continue;
4592 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
4596 /* Add a blrl instruction at _GLOBAL_OFFSET_TABLE_-4 so that a function can
4597 easily find the address of the _GLOBAL_OFFSET_TABLE_. */
4598 if (htab->got)
4600 unsigned char *contents = htab->got->contents;
4601 bfd_put_32 (output_bfd, 0x4e800021 /* blrl */, contents);
4603 if (sdyn == NULL)
4604 bfd_put_32 (output_bfd, 0, contents + 4);
4605 else
4606 bfd_put_32 (output_bfd,
4607 sdyn->output_section->vma + sdyn->output_offset,
4608 contents + 4);
4610 elf_section_data (htab->got->output_section)->this_hdr.sh_entsize = 4;
4613 return TRUE;
4616 /* The RELOCATE_SECTION function is called by the ELF backend linker
4617 to handle the relocations for a section.
4619 The relocs are always passed as Rela structures; if the section
4620 actually uses Rel structures, the r_addend field will always be
4621 zero.
4623 This function is responsible for adjust the section contents as
4624 necessary, and (if using Rela relocs and generating a
4625 relocatable output file) adjusting the reloc addend as
4626 necessary.
4628 This function does not have to worry about setting the reloc
4629 address or the reloc symbol index.
4631 LOCAL_SYMS is a pointer to the swapped in local symbols.
4633 LOCAL_SECTIONS is an array giving the section in the input file
4634 corresponding to the st_shndx field of each local symbol.
4636 The global hash table entry for the global symbols can be found
4637 via elf_sym_hashes (input_bfd).
4639 When generating relocatable output, this function must handle
4640 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
4641 going to be the section symbol corresponding to the output
4642 section, which means that the addend must be adjusted
4643 accordingly. */
4645 static bfd_boolean
4646 ppc_elf_relocate_section (bfd *output_bfd,
4647 struct bfd_link_info *info,
4648 bfd *input_bfd,
4649 asection *input_section,
4650 bfd_byte *contents,
4651 Elf_Internal_Rela *relocs,
4652 Elf_Internal_Sym *local_syms,
4653 asection **local_sections)
4655 Elf_Internal_Shdr *symtab_hdr;
4656 struct elf_link_hash_entry **sym_hashes;
4657 struct ppc_elf_link_hash_table *htab;
4658 Elf_Internal_Rela *rel;
4659 Elf_Internal_Rela *relend;
4660 Elf_Internal_Rela outrel;
4661 bfd_byte *loc;
4662 asection *sreloc = NULL;
4663 bfd_vma *local_got_offsets;
4664 bfd_boolean ret = TRUE;
4666 #ifdef DEBUG
4667 fprintf (stderr, "ppc_elf_relocate_section called for %s section %s, "
4668 "%ld relocations%s\n",
4669 bfd_archive_filename (input_bfd),
4670 bfd_section_name(input_bfd, input_section),
4671 (long) input_section->reloc_count,
4672 (info->relocatable) ? " (relocatable)" : "");
4673 #endif
4675 if (info->relocatable)
4676 return TRUE;
4678 /* Initialize howto table if not already done. */
4679 if (!ppc_elf_howto_table[R_PPC_ADDR32])
4680 ppc_elf_howto_init ();
4682 htab = ppc_elf_hash_table (info);
4683 local_got_offsets = elf_local_got_offsets (input_bfd);
4684 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
4685 sym_hashes = elf_sym_hashes (input_bfd);
4686 rel = relocs;
4687 relend = relocs + input_section->reloc_count;
4688 for (; rel < relend; rel++)
4690 enum elf_ppc_reloc_type r_type;
4691 bfd_vma addend;
4692 bfd_reloc_status_type r;
4693 Elf_Internal_Sym *sym;
4694 asection *sec;
4695 struct elf_link_hash_entry *h;
4696 const char *sym_name;
4697 reloc_howto_type *howto;
4698 unsigned long r_symndx;
4699 bfd_vma relocation;
4700 bfd_vma branch_bit, insn, from;
4701 bfd_boolean unresolved_reloc;
4702 bfd_boolean warned;
4703 unsigned int tls_type, tls_mask, tls_gd;
4705 r_type = ELF32_R_TYPE (rel->r_info);
4706 sym = NULL;
4707 sec = NULL;
4708 h = NULL;
4709 unresolved_reloc = FALSE;
4710 warned = FALSE;
4711 r_symndx = ELF32_R_SYM (rel->r_info);
4713 if (r_symndx < symtab_hdr->sh_info)
4715 sym = local_syms + r_symndx;
4716 sec = local_sections[r_symndx];
4717 sym_name = bfd_elf_local_sym_name (input_bfd, sym);
4719 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
4721 else
4723 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
4724 r_symndx, symtab_hdr, sym_hashes,
4725 h, sec, relocation,
4726 unresolved_reloc, warned);
4728 sym_name = h->root.root.string;
4731 /* TLS optimizations. Replace instruction sequences and relocs
4732 based on information we collected in tls_optimize. We edit
4733 RELOCS so that --emit-relocs will output something sensible
4734 for the final instruction stream. */
4735 tls_mask = 0;
4736 tls_gd = 0;
4737 if (IS_PPC_TLS_RELOC (r_type))
4739 if (h != NULL)
4740 tls_mask = ((struct ppc_elf_link_hash_entry *) h)->tls_mask;
4741 else if (local_got_offsets != NULL)
4743 char *lgot_masks;
4744 lgot_masks = (char *) (local_got_offsets + symtab_hdr->sh_info);
4745 tls_mask = lgot_masks[r_symndx];
4749 /* Ensure reloc mapping code below stays sane. */
4750 if ((R_PPC_GOT_TLSLD16 & 3) != (R_PPC_GOT_TLSGD16 & 3)
4751 || (R_PPC_GOT_TLSLD16_LO & 3) != (R_PPC_GOT_TLSGD16_LO & 3)
4752 || (R_PPC_GOT_TLSLD16_HI & 3) != (R_PPC_GOT_TLSGD16_HI & 3)
4753 || (R_PPC_GOT_TLSLD16_HA & 3) != (R_PPC_GOT_TLSGD16_HA & 3)
4754 || (R_PPC_GOT_TLSLD16 & 3) != (R_PPC_GOT_TPREL16 & 3)
4755 || (R_PPC_GOT_TLSLD16_LO & 3) != (R_PPC_GOT_TPREL16_LO & 3)
4756 || (R_PPC_GOT_TLSLD16_HI & 3) != (R_PPC_GOT_TPREL16_HI & 3)
4757 || (R_PPC_GOT_TLSLD16_HA & 3) != (R_PPC_GOT_TPREL16_HA & 3))
4758 abort ();
4759 switch (r_type)
4761 default:
4762 break;
4764 case R_PPC_GOT_TPREL16:
4765 case R_PPC_GOT_TPREL16_LO:
4766 if (tls_mask != 0
4767 && (tls_mask & TLS_TPREL) == 0)
4769 bfd_vma insn;
4770 insn = bfd_get_32 (output_bfd, contents + rel->r_offset - 2);
4771 insn &= 31 << 21;
4772 insn |= 0x3c020000; /* addis 0,2,0 */
4773 bfd_put_32 (output_bfd, insn, contents + rel->r_offset - 2);
4774 r_type = R_PPC_TPREL16_HA;
4775 rel->r_info = ELF32_R_INFO (r_symndx, r_type);
4777 break;
4779 case R_PPC_TLS:
4780 if (tls_mask != 0
4781 && (tls_mask & TLS_TPREL) == 0)
4783 bfd_vma insn, rtra;
4784 insn = bfd_get_32 (output_bfd, contents + rel->r_offset);
4785 if ((insn & ((31 << 26) | (31 << 11)))
4786 == ((31 << 26) | (2 << 11)))
4787 rtra = insn & ((1 << 26) - (1 << 16));
4788 else if ((insn & ((31 << 26) | (31 << 16)))
4789 == ((31 << 26) | (2 << 16)))
4790 rtra = (insn & (31 << 21)) | ((insn & (31 << 11)) << 5);
4791 else
4792 abort ();
4793 if ((insn & ((1 << 11) - (1 << 1))) == 266 << 1)
4794 /* add -> addi. */
4795 insn = 14 << 26;
4796 else if ((insn & (31 << 1)) == 23 << 1
4797 && ((insn & (31 << 6)) < 14 << 6
4798 || ((insn & (31 << 6)) >= 16 << 6
4799 && (insn & (31 << 6)) < 24 << 6)))
4800 /* load and store indexed -> dform. */
4801 insn = (32 | ((insn >> 6) & 31)) << 26;
4802 else if ((insn & (31 << 1)) == 21 << 1
4803 && (insn & (0x1a << 6)) == 0)
4804 /* ldx, ldux, stdx, stdux -> ld, ldu, std, stdu. */
4805 insn = (((58 | ((insn >> 6) & 4)) << 26)
4806 | ((insn >> 6) & 1));
4807 else if ((insn & (31 << 1)) == 21 << 1
4808 && (insn & ((1 << 11) - (1 << 1))) == 341 << 1)
4809 /* lwax -> lwa. */
4810 insn = (58 << 26) | 2;
4811 else
4812 abort ();
4813 insn |= rtra;
4814 bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
4815 r_type = R_PPC_TPREL16_LO;
4816 rel->r_info = ELF32_R_INFO (r_symndx, r_type);
4817 /* Was PPC_TLS which sits on insn boundary, now
4818 PPC_TPREL16_LO which is at insn+2. */
4819 rel->r_offset += 2;
4821 break;
4823 case R_PPC_GOT_TLSGD16_HI:
4824 case R_PPC_GOT_TLSGD16_HA:
4825 tls_gd = TLS_TPRELGD;
4826 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
4827 goto tls_gdld_hi;
4828 break;
4830 case R_PPC_GOT_TLSLD16_HI:
4831 case R_PPC_GOT_TLSLD16_HA:
4832 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
4834 tls_gdld_hi:
4835 if ((tls_mask & tls_gd) != 0)
4836 r_type = (((r_type - (R_PPC_GOT_TLSGD16 & 3)) & 3)
4837 + R_PPC_GOT_TPREL16);
4838 else
4840 bfd_put_32 (output_bfd, NOP, contents + rel->r_offset);
4841 rel->r_offset -= 2;
4842 r_type = R_PPC_NONE;
4844 rel->r_info = ELF32_R_INFO (r_symndx, r_type);
4846 break;
4848 case R_PPC_GOT_TLSGD16:
4849 case R_PPC_GOT_TLSGD16_LO:
4850 tls_gd = TLS_TPRELGD;
4851 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
4852 goto tls_get_addr_check;
4853 break;
4855 case R_PPC_GOT_TLSLD16:
4856 case R_PPC_GOT_TLSLD16_LO:
4857 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
4859 tls_get_addr_check:
4860 if (rel + 1 < relend)
4862 enum elf_ppc_reloc_type r_type2;
4863 unsigned long r_symndx2;
4864 struct elf_link_hash_entry *h2;
4865 bfd_vma insn1, insn2;
4866 bfd_vma offset;
4868 /* The next instruction should be a call to
4869 __tls_get_addr. Peek at the reloc to be sure. */
4870 r_type2 = ELF32_R_TYPE (rel[1].r_info);
4871 r_symndx2 = ELF32_R_SYM (rel[1].r_info);
4872 if (r_symndx2 < symtab_hdr->sh_info
4873 || (r_type2 != R_PPC_REL14
4874 && r_type2 != R_PPC_REL14_BRTAKEN
4875 && r_type2 != R_PPC_REL14_BRNTAKEN
4876 && r_type2 != R_PPC_REL24
4877 && r_type2 != R_PPC_PLTREL24))
4878 break;
4880 h2 = sym_hashes[r_symndx2 - symtab_hdr->sh_info];
4881 while (h2->root.type == bfd_link_hash_indirect
4882 || h2->root.type == bfd_link_hash_warning)
4883 h2 = (struct elf_link_hash_entry *) h2->root.u.i.link;
4884 if (h2 == NULL || h2 != htab->tls_get_addr)
4885 break;
4887 /* OK, it checks out. Replace the call. */
4888 offset = rel[1].r_offset;
4889 insn1 = bfd_get_32 (output_bfd,
4890 contents + rel->r_offset - 2);
4891 if ((tls_mask & tls_gd) != 0)
4893 /* IE */
4894 insn1 &= (1 << 26) - 1;
4895 insn1 |= 32 << 26; /* lwz */
4896 insn2 = 0x7c631214; /* add 3,3,2 */
4897 rel[1].r_info = ELF32_R_INFO (r_symndx2, R_PPC_NONE);
4898 r_type = (((r_type - (R_PPC_GOT_TLSGD16 & 3)) & 3)
4899 + R_PPC_GOT_TPREL16);
4900 rel->r_info = ELF32_R_INFO (r_symndx, r_type);
4902 else
4904 /* LE */
4905 insn1 = 0x3c620000; /* addis 3,2,0 */
4906 insn2 = 0x38630000; /* addi 3,3,0 */
4907 if (tls_gd == 0)
4909 /* Was an LD reloc. */
4910 r_symndx = 0;
4911 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
4912 rel[1].r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
4914 r_type = R_PPC_TPREL16_HA;
4915 rel->r_info = ELF32_R_INFO (r_symndx, r_type);
4916 rel[1].r_info = ELF32_R_INFO (r_symndx,
4917 R_PPC_TPREL16_LO);
4918 rel[1].r_offset += 2;
4920 bfd_put_32 (output_bfd, insn1, contents + rel->r_offset - 2);
4921 bfd_put_32 (output_bfd, insn2, contents + offset);
4922 if (tls_gd == 0)
4924 /* We changed the symbol on an LD reloc. Start over
4925 in order to get h, sym, sec etc. right. */
4926 rel--;
4927 continue;
4931 break;
4934 /* Handle other relocations that tweak non-addend part of insn. */
4935 branch_bit = 0;
4936 switch (r_type)
4938 default:
4939 break;
4941 /* Branch taken prediction relocations. */
4942 case R_PPC_ADDR14_BRTAKEN:
4943 case R_PPC_REL14_BRTAKEN:
4944 branch_bit = BRANCH_PREDICT_BIT;
4945 /* Fall thru */
4947 /* Branch not taken prediction relocations. */
4948 case R_PPC_ADDR14_BRNTAKEN:
4949 case R_PPC_REL14_BRNTAKEN:
4950 insn = bfd_get_32 (output_bfd, contents + rel->r_offset);
4951 insn &= ~BRANCH_PREDICT_BIT;
4952 insn |= branch_bit;
4954 from = (rel->r_offset
4955 + input_section->output_offset
4956 + input_section->output_section->vma);
4958 /* Invert 'y' bit if not the default. */
4959 if ((bfd_signed_vma) (relocation + rel->r_addend - from) < 0)
4960 insn ^= BRANCH_PREDICT_BIT;
4962 bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
4963 break;
4966 addend = rel->r_addend;
4967 tls_type = 0;
4968 howto = NULL;
4969 if (r_type < R_PPC_max)
4970 howto = ppc_elf_howto_table[r_type];
4971 switch (r_type)
4973 default:
4974 (*_bfd_error_handler)
4975 (_("%s: unknown relocation type %d for symbol %s"),
4976 bfd_archive_filename (input_bfd), (int) r_type, sym_name);
4978 bfd_set_error (bfd_error_bad_value);
4979 ret = FALSE;
4980 continue;
4982 case R_PPC_NONE:
4983 case R_PPC_TLS:
4984 case R_PPC_EMB_MRKREF:
4985 case R_PPC_GNU_VTINHERIT:
4986 case R_PPC_GNU_VTENTRY:
4987 continue;
4989 /* GOT16 relocations. Like an ADDR16 using the symbol's
4990 address in the GOT as relocation value instead of the
4991 symbol's value itself. Also, create a GOT entry for the
4992 symbol and put the symbol value there. */
4993 case R_PPC_GOT_TLSGD16:
4994 case R_PPC_GOT_TLSGD16_LO:
4995 case R_PPC_GOT_TLSGD16_HI:
4996 case R_PPC_GOT_TLSGD16_HA:
4997 tls_type = TLS_TLS | TLS_GD;
4998 goto dogot;
5000 case R_PPC_GOT_TLSLD16:
5001 case R_PPC_GOT_TLSLD16_LO:
5002 case R_PPC_GOT_TLSLD16_HI:
5003 case R_PPC_GOT_TLSLD16_HA:
5004 tls_type = TLS_TLS | TLS_LD;
5005 goto dogot;
5007 case R_PPC_GOT_TPREL16:
5008 case R_PPC_GOT_TPREL16_LO:
5009 case R_PPC_GOT_TPREL16_HI:
5010 case R_PPC_GOT_TPREL16_HA:
5011 tls_type = TLS_TLS | TLS_TPREL;
5012 goto dogot;
5014 case R_PPC_GOT_DTPREL16:
5015 case R_PPC_GOT_DTPREL16_LO:
5016 case R_PPC_GOT_DTPREL16_HI:
5017 case R_PPC_GOT_DTPREL16_HA:
5018 tls_type = TLS_TLS | TLS_DTPREL;
5019 goto dogot;
5021 case R_PPC_GOT16:
5022 case R_PPC_GOT16_LO:
5023 case R_PPC_GOT16_HI:
5024 case R_PPC_GOT16_HA:
5025 dogot:
5027 /* Relocation is to the entry for this symbol in the global
5028 offset table. */
5029 bfd_vma off;
5030 bfd_vma *offp;
5031 unsigned long indx;
5033 if (htab->got == NULL)
5034 abort ();
5036 indx = 0;
5037 if (tls_type == (TLS_TLS | TLS_LD)
5038 && (h == NULL
5039 || !(h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC)))
5040 offp = &htab->tlsld_got.offset;
5041 else if (h != NULL)
5043 bfd_boolean dyn;
5044 dyn = htab->elf.dynamic_sections_created;
5045 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
5046 || (info->shared
5047 && SYMBOL_REFERENCES_LOCAL (info, h)))
5048 /* This is actually a static link, or it is a
5049 -Bsymbolic link and the symbol is defined
5050 locally, or the symbol was forced to be local
5051 because of a version file. */
5053 else
5055 indx = h->dynindx;
5056 unresolved_reloc = FALSE;
5058 offp = &h->got.offset;
5060 else
5062 if (local_got_offsets == NULL)
5063 abort ();
5064 offp = &local_got_offsets[r_symndx];
5067 /* The offset must always be a multiple of 4. We use the
5068 least significant bit to record whether we have already
5069 processed this entry. */
5070 off = *offp;
5071 if ((off & 1) != 0)
5072 off &= ~1;
5073 else
5075 unsigned int tls_m = (tls_mask
5076 & (TLS_LD | TLS_GD | TLS_DTPREL
5077 | TLS_TPREL | TLS_TPRELGD));
5079 if (offp == &htab->tlsld_got.offset)
5080 tls_m = TLS_LD;
5081 else if (h == NULL
5082 || !(h->elf_link_hash_flags
5083 & ELF_LINK_HASH_DEF_DYNAMIC))
5084 tls_m &= ~TLS_LD;
5086 /* We might have multiple got entries for this sym.
5087 Initialize them all. */
5090 int tls_ty = 0;
5092 if ((tls_m & TLS_LD) != 0)
5094 tls_ty = TLS_TLS | TLS_LD;
5095 tls_m &= ~TLS_LD;
5097 else if ((tls_m & TLS_GD) != 0)
5099 tls_ty = TLS_TLS | TLS_GD;
5100 tls_m &= ~TLS_GD;
5102 else if ((tls_m & TLS_DTPREL) != 0)
5104 tls_ty = TLS_TLS | TLS_DTPREL;
5105 tls_m &= ~TLS_DTPREL;
5107 else if ((tls_m & (TLS_TPREL | TLS_TPRELGD)) != 0)
5109 tls_ty = TLS_TLS | TLS_TPREL;
5110 tls_m = 0;
5113 /* Generate relocs for the dynamic linker. */
5114 if ((info->shared || indx != 0)
5115 && (h == NULL
5116 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
5117 || h->root.type != bfd_link_hash_undefweak))
5119 outrel.r_offset = (htab->got->output_section->vma
5120 + htab->got->output_offset
5121 + off);
5122 outrel.r_addend = 0;
5123 if (tls_ty & (TLS_LD | TLS_GD))
5125 outrel.r_info = ELF32_R_INFO (indx, R_PPC_DTPMOD32);
5126 if (tls_ty == (TLS_TLS | TLS_GD))
5128 loc = htab->relgot->contents;
5129 loc += (htab->relgot->reloc_count++
5130 * sizeof (Elf32_External_Rela));
5131 bfd_elf32_swap_reloca_out (output_bfd,
5132 &outrel, loc);
5133 outrel.r_offset += 4;
5134 outrel.r_info
5135 = ELF32_R_INFO (indx, R_PPC_DTPREL32);
5138 else if (tls_ty == (TLS_TLS | TLS_DTPREL))
5139 outrel.r_info = ELF32_R_INFO (indx, R_PPC_DTPREL32);
5140 else if (tls_ty == (TLS_TLS | TLS_TPREL))
5141 outrel.r_info = ELF32_R_INFO (indx, R_PPC_TPREL32);
5142 else if (indx == 0)
5143 outrel.r_info = ELF32_R_INFO (indx, R_PPC_RELATIVE);
5144 else
5145 outrel.r_info = ELF32_R_INFO (indx, R_PPC_GLOB_DAT);
5146 if (indx == 0)
5148 outrel.r_addend += relocation;
5149 if (tls_ty & (TLS_GD | TLS_DTPREL | TLS_TPREL))
5150 outrel.r_addend -= htab->elf.tls_sec->vma;
5152 loc = htab->relgot->contents;
5153 loc += (htab->relgot->reloc_count++
5154 * sizeof (Elf32_External_Rela));
5155 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
5158 /* Init the .got section contents if we're not
5159 emitting a reloc. */
5160 else
5162 bfd_vma value = relocation;
5164 if (tls_ty == (TLS_TLS | TLS_LD))
5165 value = 1;
5166 else if (tls_ty != 0)
5168 value -= htab->elf.tls_sec->vma + DTP_OFFSET;
5169 if (tls_ty == (TLS_TLS | TLS_TPREL))
5170 value += DTP_OFFSET - TP_OFFSET;
5172 if (tls_ty == (TLS_TLS | TLS_GD))
5174 bfd_put_32 (output_bfd, value,
5175 htab->got->contents + off + 4);
5176 value = 1;
5179 bfd_put_32 (output_bfd, value,
5180 htab->got->contents + off);
5183 off += 4;
5184 if (tls_ty & (TLS_LD | TLS_GD))
5185 off += 4;
5187 while (tls_m != 0);
5189 off = *offp;
5190 *offp = off | 1;
5193 if (off >= (bfd_vma) -2)
5194 abort ();
5196 if ((tls_type & TLS_TLS) != 0)
5198 if (tls_type != (TLS_TLS | TLS_LD))
5200 if ((tls_mask & TLS_LD) != 0
5201 && !(h == NULL
5202 || !(h->elf_link_hash_flags
5203 & ELF_LINK_HASH_DEF_DYNAMIC)))
5204 off += 8;
5205 if (tls_type != (TLS_TLS | TLS_GD))
5207 if ((tls_mask & TLS_GD) != 0)
5208 off += 8;
5209 if (tls_type != (TLS_TLS | TLS_DTPREL))
5211 if ((tls_mask & TLS_DTPREL) != 0)
5212 off += 4;
5218 relocation = htab->got->output_offset + off - 4;
5220 /* Addends on got relocations don't make much sense.
5221 x+off@got is actually x@got+off, and since the got is
5222 generated by a hash table traversal, the value in the
5223 got at entry m+n bears little relation to the entry m. */
5224 if (addend != 0)
5225 (*_bfd_error_handler)
5226 (_("%s(%s+0x%lx): non-zero addend on %s reloc against `%s'"),
5227 bfd_archive_filename (input_bfd),
5228 bfd_get_section_name (input_bfd, input_section),
5229 (long) rel->r_offset,
5230 howto->name,
5231 sym_name);
5233 break;
5235 /* Relocations that need no special processing. */
5236 case R_PPC_LOCAL24PC:
5237 /* It makes no sense to point a local relocation
5238 at a symbol not in this object. */
5239 if (unresolved_reloc)
5241 if (! (*info->callbacks->undefined_symbol) (info,
5242 h->root.root.string,
5243 input_bfd,
5244 input_section,
5245 rel->r_offset,
5246 TRUE))
5247 return FALSE;
5248 continue;
5250 break;
5252 case R_PPC_DTPREL16:
5253 case R_PPC_DTPREL16_LO:
5254 case R_PPC_DTPREL16_HI:
5255 case R_PPC_DTPREL16_HA:
5256 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
5257 break;
5259 /* Relocations that may need to be propagated if this is a shared
5260 object. */
5261 case R_PPC_TPREL16:
5262 case R_PPC_TPREL16_LO:
5263 case R_PPC_TPREL16_HI:
5264 case R_PPC_TPREL16_HA:
5265 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
5266 /* The TPREL16 relocs shouldn't really be used in shared
5267 libs as they will result in DT_TEXTREL being set, but
5268 support them anyway. */
5269 goto dodyn;
5271 case R_PPC_TPREL32:
5272 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
5273 goto dodyn;
5275 case R_PPC_DTPREL32:
5276 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
5277 goto dodyn;
5279 case R_PPC_DTPMOD32:
5280 relocation = 1;
5281 addend = 0;
5282 goto dodyn;
5284 case R_PPC_REL24:
5285 case R_PPC_REL32:
5286 case R_PPC_REL14:
5287 case R_PPC_REL14_BRTAKEN:
5288 case R_PPC_REL14_BRNTAKEN:
5289 /* If these relocations are not to a named symbol, they can be
5290 handled right here, no need to bother the dynamic linker. */
5291 if (SYMBOL_REFERENCES_LOCAL (info, h)
5292 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
5293 break;
5294 /* fall through */
5296 /* Relocations that always need to be propagated if this is a shared
5297 object. */
5298 case R_PPC_ADDR32:
5299 case R_PPC_ADDR24:
5300 case R_PPC_ADDR16:
5301 case R_PPC_ADDR16_LO:
5302 case R_PPC_ADDR16_HI:
5303 case R_PPC_ADDR16_HA:
5304 case R_PPC_ADDR14:
5305 case R_PPC_ADDR14_BRTAKEN:
5306 case R_PPC_ADDR14_BRNTAKEN:
5307 case R_PPC_UADDR32:
5308 case R_PPC_UADDR16:
5309 /* r_symndx will be zero only for relocs against symbols
5310 from removed linkonce sections, or sections discarded by
5311 a linker script. */
5312 dodyn:
5313 if (r_symndx == 0)
5314 break;
5315 /* Fall thru. */
5317 if ((info->shared
5318 && (h == NULL
5319 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
5320 || h->root.type != bfd_link_hash_undefweak)
5321 && (MUST_BE_DYN_RELOC (r_type)
5322 || !SYMBOL_CALLS_LOCAL (info, h)))
5323 || (ELIMINATE_COPY_RELOCS
5324 && !info->shared
5325 && (input_section->flags & SEC_ALLOC) != 0
5326 && h != NULL
5327 && h->dynindx != -1
5328 && (h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0
5329 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
5330 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0))
5332 int skip;
5334 #ifdef DEBUG
5335 fprintf (stderr, "ppc_elf_relocate_section needs to "
5336 "create relocation for %s\n",
5337 (h && h->root.root.string
5338 ? h->root.root.string : "<unknown>"));
5339 #endif
5341 /* When generating a shared object, these relocations
5342 are copied into the output file to be resolved at run
5343 time. */
5344 if (sreloc == NULL)
5346 const char *name;
5348 name = (bfd_elf_string_from_elf_section
5349 (input_bfd,
5350 elf_elfheader (input_bfd)->e_shstrndx,
5351 elf_section_data (input_section)->rel_hdr.sh_name));
5352 if (name == NULL)
5353 return FALSE;
5355 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
5356 && strcmp (bfd_get_section_name (input_bfd,
5357 input_section),
5358 name + 5) == 0);
5360 sreloc = bfd_get_section_by_name (htab->elf.dynobj, name);
5361 BFD_ASSERT (sreloc != NULL);
5364 skip = 0;
5366 outrel.r_offset =
5367 _bfd_elf_section_offset (output_bfd, info, input_section,
5368 rel->r_offset);
5369 if (outrel.r_offset == (bfd_vma) -1
5370 || outrel.r_offset == (bfd_vma) -2)
5371 skip = (int) outrel.r_offset;
5372 outrel.r_offset += (input_section->output_section->vma
5373 + input_section->output_offset);
5375 if (skip)
5376 memset (&outrel, 0, sizeof outrel);
5377 else if (!SYMBOL_REFERENCES_LOCAL (info, h))
5379 unresolved_reloc = FALSE;
5380 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
5381 outrel.r_addend = rel->r_addend;
5383 else
5385 outrel.r_addend = relocation + rel->r_addend;
5387 if (r_type == R_PPC_ADDR32)
5388 outrel.r_info = ELF32_R_INFO (0, R_PPC_RELATIVE);
5389 else
5391 long indx;
5393 if (bfd_is_abs_section (sec))
5394 indx = 0;
5395 else if (sec == NULL || sec->owner == NULL)
5397 bfd_set_error (bfd_error_bad_value);
5398 return FALSE;
5400 else
5402 asection *osec;
5404 /* We are turning this relocation into one
5405 against a section symbol. It would be
5406 proper to subtract the symbol's value,
5407 osec->vma, from the emitted reloc addend,
5408 but ld.so expects buggy relocs. */
5409 osec = sec->output_section;
5410 indx = elf_section_data (osec)->dynindx;
5411 BFD_ASSERT (indx > 0);
5412 #ifdef DEBUG
5413 if (indx <= 0)
5414 printf ("indx=%d section=%s flags=%08x name=%s\n",
5415 indx, osec->name, osec->flags,
5416 h->root.root.string);
5417 #endif
5420 outrel.r_info = ELF32_R_INFO (indx, r_type);
5424 loc = sreloc->contents;
5425 loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rela);
5426 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
5428 if (skip == -1)
5429 continue;
5431 /* This reloc will be computed at runtime. We clear the memory
5432 so that it contains predictable value. */
5433 if (! skip
5434 && ((input_section->flags & SEC_ALLOC) != 0
5435 || ELF32_R_TYPE (outrel.r_info) != R_PPC_RELATIVE))
5437 relocation = howto->pc_relative ? outrel.r_offset : 0;
5438 addend = 0;
5439 break;
5442 break;
5444 case R_PPC_RELAX32PC:
5445 relocation -= (input_section->output_section->vma
5446 + input_section->output_offset
5447 + rel->r_offset - 4);
5448 /* Fall thru */
5449 case R_PPC_RELAX32:
5451 unsigned long t0;
5452 unsigned long t1;
5454 t0 = bfd_get_32 (output_bfd, contents + rel->r_offset);
5455 t1 = bfd_get_32 (output_bfd, contents + rel->r_offset + 4);
5457 /* We're clearing the bits for R_PPC_ADDR16_HA
5458 and R_PPC_ADDR16_LO here. */
5459 t0 &= ~0xffff;
5460 t1 &= ~0xffff;
5462 /* t0 is HA, t1 is LO */
5463 relocation += addend;
5464 t0 |= ((relocation + 0x8000) >> 16) & 0xffff;
5465 t1 |= relocation & 0xffff;
5467 bfd_put_32 (output_bfd, t0, contents + rel->r_offset);
5468 bfd_put_32 (output_bfd, t1, contents + rel->r_offset + 4);
5470 continue;
5472 /* Indirect .sdata relocation. */
5473 case R_PPC_EMB_SDAI16:
5474 BFD_ASSERT (htab->sdata != NULL);
5475 relocation
5476 = elf_finish_pointer_linker_section (output_bfd, input_bfd, info,
5477 htab->sdata, h, relocation,
5478 rel, R_PPC_RELATIVE);
5479 break;
5481 /* Indirect .sdata2 relocation. */
5482 case R_PPC_EMB_SDA2I16:
5483 BFD_ASSERT (htab->sdata2 != NULL);
5484 relocation
5485 = elf_finish_pointer_linker_section (output_bfd, input_bfd, info,
5486 htab->sdata2, h, relocation,
5487 rel, R_PPC_RELATIVE);
5488 break;
5490 /* Handle the TOC16 reloc. We want to use the offset within the .got
5491 section, not the actual VMA. This is appropriate when generating
5492 an embedded ELF object, for which the .got section acts like the
5493 AIX .toc section. */
5494 case R_PPC_TOC16: /* phony GOT16 relocations */
5495 BFD_ASSERT (sec != NULL);
5496 BFD_ASSERT (bfd_is_und_section (sec)
5497 || strcmp (bfd_get_section_name (abfd, sec), ".got") == 0
5498 || strcmp (bfd_get_section_name (abfd, sec), ".cgot") == 0)
5500 addend -= sec->output_section->vma + sec->output_offset + 0x8000;
5501 break;
5503 case R_PPC_PLTREL24:
5504 /* Relocation is to the entry for this symbol in the
5505 procedure linkage table. */
5506 BFD_ASSERT (h != NULL);
5508 if (h->plt.offset == (bfd_vma) -1
5509 || htab->plt == NULL)
5511 /* We didn't make a PLT entry for this symbol. This
5512 happens when statically linking PIC code, or when
5513 using -Bsymbolic. */
5514 break;
5517 unresolved_reloc = FALSE;
5518 relocation = (htab->plt->output_section->vma
5519 + htab->plt->output_offset
5520 + h->plt.offset);
5521 break;
5523 /* Relocate against _SDA_BASE_. */
5524 case R_PPC_SDAREL16:
5526 const char *name;
5527 const struct elf_link_hash_entry *sh;
5529 BFD_ASSERT (sec != NULL);
5530 name = bfd_get_section_name (abfd, sec->output_section);
5531 if (! ((strncmp (name, ".sdata", 6) == 0
5532 && (name[6] == 0 || name[6] == '.'))
5533 || (strncmp (name, ".sbss", 5) == 0
5534 && (name[5] == 0 || name[5] == '.'))))
5536 (*_bfd_error_handler)
5537 (_("%s: the target (%s) of a %s relocation is "
5538 "in the wrong output section (%s)"),
5539 bfd_archive_filename (input_bfd),
5540 sym_name,
5541 howto->name,
5542 name);
5544 sh = htab->sdata->sym_hash;
5545 addend -= (sh->root.u.def.value
5546 + sh->root.u.def.section->output_section->vma
5547 + sh->root.u.def.section->output_offset);
5549 break;
5551 /* Relocate against _SDA2_BASE_. */
5552 case R_PPC_EMB_SDA2REL:
5554 const char *name;
5555 const struct elf_link_hash_entry *sh;
5557 BFD_ASSERT (sec != NULL);
5558 name = bfd_get_section_name (abfd, sec->output_section);
5559 if (! (strncmp (name, ".sdata2", 7) == 0
5560 || strncmp (name, ".sbss2", 6) == 0))
5562 (*_bfd_error_handler)
5563 (_("%s: the target (%s) of a %s relocation is "
5564 "in the wrong output section (%s)"),
5565 bfd_archive_filename (input_bfd),
5566 sym_name,
5567 howto->name,
5568 name);
5570 bfd_set_error (bfd_error_bad_value);
5571 ret = FALSE;
5572 continue;
5574 sh = htab->sdata2->sym_hash;
5575 addend -= (sh->root.u.def.value
5576 + sh->root.u.def.section->output_section->vma
5577 + sh->root.u.def.section->output_offset);
5579 break;
5581 /* Relocate against either _SDA_BASE_, _SDA2_BASE_, or 0. */
5582 case R_PPC_EMB_SDA21:
5583 case R_PPC_EMB_RELSDA:
5585 const char *name;
5586 const struct elf_link_hash_entry *sh;
5587 int reg;
5589 BFD_ASSERT (sec != NULL);
5590 name = bfd_get_section_name (abfd, sec->output_section);
5591 if (((strncmp (name, ".sdata", 6) == 0
5592 && (name[6] == 0 || name[6] == '.'))
5593 || (strncmp (name, ".sbss", 5) == 0
5594 && (name[5] == 0 || name[5] == '.'))))
5596 reg = 13;
5597 sh = htab->sdata->sym_hash;
5598 addend -= (sh->root.u.def.value
5599 + sh->root.u.def.section->output_section->vma
5600 + sh->root.u.def.section->output_offset);
5603 else if (strncmp (name, ".sdata2", 7) == 0
5604 || strncmp (name, ".sbss2", 6) == 0)
5606 reg = 2;
5607 sh = htab->sdata2->sym_hash;
5608 addend -= (sh->root.u.def.value
5609 + sh->root.u.def.section->output_section->vma
5610 + sh->root.u.def.section->output_offset);
5613 else if (strcmp (name, ".PPC.EMB.sdata0") == 0
5614 || strcmp (name, ".PPC.EMB.sbss0") == 0)
5616 reg = 0;
5619 else
5621 (*_bfd_error_handler)
5622 (_("%s: the target (%s) of a %s relocation is "
5623 "in the wrong output section (%s)"),
5624 bfd_archive_filename (input_bfd),
5625 sym_name,
5626 howto->name,
5627 name);
5629 bfd_set_error (bfd_error_bad_value);
5630 ret = FALSE;
5631 continue;
5634 if (r_type == R_PPC_EMB_SDA21)
5635 { /* fill in register field */
5636 insn = bfd_get_32 (output_bfd, contents + rel->r_offset);
5637 insn = (insn & ~RA_REGISTER_MASK) | (reg << RA_REGISTER_SHIFT);
5638 bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
5641 break;
5643 /* Relocate against the beginning of the section. */
5644 case R_PPC_SECTOFF:
5645 case R_PPC_SECTOFF_LO:
5646 case R_PPC_SECTOFF_HI:
5647 case R_PPC_SECTOFF_HA:
5648 BFD_ASSERT (sec != NULL);
5649 addend -= sec->output_section->vma;
5650 break;
5652 /* Negative relocations. */
5653 case R_PPC_EMB_NADDR32:
5654 case R_PPC_EMB_NADDR16:
5655 case R_PPC_EMB_NADDR16_LO:
5656 case R_PPC_EMB_NADDR16_HI:
5657 case R_PPC_EMB_NADDR16_HA:
5658 addend -= 2 * relocation;
5659 break;
5661 case R_PPC_COPY:
5662 case R_PPC_GLOB_DAT:
5663 case R_PPC_JMP_SLOT:
5664 case R_PPC_RELATIVE:
5665 case R_PPC_PLT32:
5666 case R_PPC_PLTREL32:
5667 case R_PPC_PLT16_LO:
5668 case R_PPC_PLT16_HI:
5669 case R_PPC_PLT16_HA:
5670 case R_PPC_ADDR30:
5671 case R_PPC_EMB_RELSEC16:
5672 case R_PPC_EMB_RELST_LO:
5673 case R_PPC_EMB_RELST_HI:
5674 case R_PPC_EMB_RELST_HA:
5675 case R_PPC_EMB_BIT_FLD:
5676 (*_bfd_error_handler)
5677 (_("%s: relocation %s is not yet supported for symbol %s."),
5678 bfd_archive_filename (input_bfd),
5679 howto->name,
5680 sym_name);
5682 bfd_set_error (bfd_error_invalid_operation);
5683 ret = FALSE;
5684 continue;
5687 /* Do any further special processing. */
5688 switch (r_type)
5690 default:
5691 break;
5693 case R_PPC_ADDR16_HA:
5694 case R_PPC_GOT16_HA:
5695 case R_PPC_PLT16_HA:
5696 case R_PPC_SECTOFF_HA:
5697 case R_PPC_TPREL16_HA:
5698 case R_PPC_DTPREL16_HA:
5699 case R_PPC_GOT_TLSGD16_HA:
5700 case R_PPC_GOT_TLSLD16_HA:
5701 case R_PPC_GOT_TPREL16_HA:
5702 case R_PPC_GOT_DTPREL16_HA:
5703 case R_PPC_EMB_NADDR16_HA:
5704 case R_PPC_EMB_RELST_HA:
5705 /* It's just possible that this symbol is a weak symbol
5706 that's not actually defined anywhere. In that case,
5707 'sec' would be NULL, and we should leave the symbol
5708 alone (it will be set to zero elsewhere in the link). */
5709 if (sec != NULL)
5710 /* Add 0x10000 if sign bit in 0:15 is set.
5711 Bits 0:15 are not used. */
5712 addend += 0x8000;
5713 break;
5716 #ifdef DEBUG
5717 fprintf (stderr, "\ttype = %s (%d), name = %s, symbol index = %ld, "
5718 "offset = %ld, addend = %ld\n",
5719 howto->name,
5720 (int) r_type,
5721 sym_name,
5722 r_symndx,
5723 (long) rel->r_offset,
5724 (long) addend);
5725 #endif
5727 if (unresolved_reloc
5728 && !((input_section->flags & SEC_DEBUGGING) != 0
5729 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0))
5731 (*_bfd_error_handler)
5732 (_("%s(%s+0x%lx): unresolvable %s relocation against symbol `%s'"),
5733 bfd_archive_filename (input_bfd),
5734 bfd_get_section_name (input_bfd, input_section),
5735 (long) rel->r_offset,
5736 howto->name,
5737 sym_name);
5738 ret = FALSE;
5741 r = _bfd_final_link_relocate (howto,
5742 input_bfd,
5743 input_section,
5744 contents,
5745 rel->r_offset,
5746 relocation,
5747 addend);
5749 if (r != bfd_reloc_ok)
5751 if (sym_name == NULL)
5752 sym_name = "(null)";
5753 if (r == bfd_reloc_overflow)
5755 if (warned)
5756 continue;
5757 if (h != NULL
5758 && h->root.type == bfd_link_hash_undefweak
5759 && howto->pc_relative)
5761 /* Assume this is a call protected by other code that
5762 detect the symbol is undefined. If this is the case,
5763 we can safely ignore the overflow. If not, the
5764 program is hosed anyway, and a little warning isn't
5765 going to help. */
5767 continue;
5770 if (! (*info->callbacks->reloc_overflow) (info,
5771 sym_name,
5772 howto->name,
5773 rel->r_addend,
5774 input_bfd,
5775 input_section,
5776 rel->r_offset))
5777 return FALSE;
5779 else
5781 (*_bfd_error_handler)
5782 (_("%s(%s+0x%lx): %s reloc against `%s': error %d"),
5783 bfd_archive_filename (input_bfd),
5784 bfd_get_section_name (input_bfd, input_section),
5785 (long) rel->r_offset, howto->name, sym_name, (int) r);
5786 ret = FALSE;
5791 #ifdef DEBUG
5792 fprintf (stderr, "\n");
5793 #endif
5795 return ret;
5798 static enum elf_reloc_type_class
5799 ppc_elf_reloc_type_class (const Elf_Internal_Rela *rela)
5801 switch (ELF32_R_TYPE (rela->r_info))
5803 case R_PPC_RELATIVE:
5804 return reloc_class_relative;
5805 case R_PPC_REL24:
5806 case R_PPC_ADDR24:
5807 case R_PPC_JMP_SLOT:
5808 return reloc_class_plt;
5809 case R_PPC_COPY:
5810 return reloc_class_copy;
5811 default:
5812 return reloc_class_normal;
5816 /* Support for core dump NOTE sections. */
5818 static bfd_boolean
5819 ppc_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
5821 int offset;
5822 unsigned int size;
5824 switch (note->descsz)
5826 default:
5827 return FALSE;
5829 case 268: /* Linux/PPC. */
5830 /* pr_cursig */
5831 elf_tdata (abfd)->core_signal = bfd_get_16 (abfd, note->descdata + 12);
5833 /* pr_pid */
5834 elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, note->descdata + 24);
5836 /* pr_reg */
5837 offset = 72;
5838 size = 192;
5840 break;
5843 /* Make a ".reg/999" section. */
5844 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
5845 size, note->descpos + offset);
5848 static bfd_boolean
5849 ppc_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
5851 switch (note->descsz)
5853 default:
5854 return FALSE;
5856 case 128: /* Linux/PPC elf_prpsinfo. */
5857 elf_tdata (abfd)->core_program
5858 = _bfd_elfcore_strndup (abfd, note->descdata + 32, 16);
5859 elf_tdata (abfd)->core_command
5860 = _bfd_elfcore_strndup (abfd, note->descdata + 48, 80);
5863 /* Note that for some reason, a spurious space is tacked
5864 onto the end of the args in some (at least one anyway)
5865 implementations, so strip it off if it exists. */
5868 char *command = elf_tdata (abfd)->core_command;
5869 int n = strlen (command);
5871 if (0 < n && command[n - 1] == ' ')
5872 command[n - 1] = '\0';
5875 return TRUE;
5878 /* Very simple linked list structure for recording apuinfo values. */
5879 typedef struct apuinfo_list
5881 struct apuinfo_list *next;
5882 unsigned long value;
5884 apuinfo_list;
5886 static apuinfo_list *head;
5889 static void
5890 apuinfo_list_init (void)
5892 head = NULL;
5895 static void
5896 apuinfo_list_add (unsigned long value)
5898 apuinfo_list *entry = head;
5900 while (entry != NULL)
5902 if (entry->value == value)
5903 return;
5904 entry = entry->next;
5907 entry = bfd_malloc (sizeof (* entry));
5908 if (entry == NULL)
5909 return;
5911 entry->value = value;
5912 entry->next = head;
5913 head = entry;
5916 static unsigned
5917 apuinfo_list_length (void)
5919 apuinfo_list *entry;
5920 unsigned long count;
5922 for (entry = head, count = 0;
5923 entry;
5924 entry = entry->next)
5925 ++ count;
5927 return count;
5930 static inline unsigned long
5931 apuinfo_list_element (unsigned long number)
5933 apuinfo_list * entry;
5935 for (entry = head;
5936 entry && number --;
5937 entry = entry->next)
5940 return entry ? entry->value : 0;
5943 static void
5944 apuinfo_list_finish (void)
5946 apuinfo_list *entry;
5948 for (entry = head; entry;)
5950 apuinfo_list *next = entry->next;
5951 free (entry);
5952 entry = next;
5955 head = NULL;
5958 #define APUINFO_SECTION_NAME ".PPC.EMB.apuinfo"
5959 #define APUINFO_LABEL "APUinfo"
5961 /* Scan the input BFDs and create a linked list of
5962 the APUinfo values that will need to be emitted. */
5964 static void
5965 ppc_elf_begin_write_processing (bfd *abfd, struct bfd_link_info *link_info)
5967 bfd *ibfd;
5968 asection *asec;
5969 char *buffer;
5970 unsigned num_input_sections;
5971 bfd_size_type output_section_size;
5972 unsigned i;
5973 unsigned num_entries;
5974 unsigned long offset;
5975 unsigned long length;
5976 const char *error_message = NULL;
5978 if (link_info == NULL)
5979 return;
5981 /* Scan the input bfds, looking for apuinfo sections. */
5982 num_input_sections = 0;
5983 output_section_size = 0;
5985 for (ibfd = link_info->input_bfds; ibfd; ibfd = ibfd->link_next)
5987 asec = bfd_get_section_by_name (ibfd, APUINFO_SECTION_NAME);
5988 if (asec)
5990 ++ num_input_sections;
5991 output_section_size += asec->size;
5995 /* We need at least one input sections
5996 in order to make merging worthwhile. */
5997 if (num_input_sections < 1)
5998 return;
6000 /* Just make sure that the output section exists as well. */
6001 asec = bfd_get_section_by_name (abfd, APUINFO_SECTION_NAME);
6002 if (asec == NULL)
6003 return;
6005 /* Allocate a buffer for the contents of the input sections. */
6006 buffer = bfd_malloc (output_section_size);
6007 if (buffer == NULL)
6008 return;
6010 offset = 0;
6011 apuinfo_list_init ();
6013 /* Read in the input sections contents. */
6014 for (ibfd = link_info->input_bfds; ibfd; ibfd = ibfd->link_next)
6016 unsigned long datum;
6017 char *ptr;
6019 asec = bfd_get_section_by_name (ibfd, APUINFO_SECTION_NAME);
6020 if (asec == NULL)
6021 continue;
6023 length = asec->size;
6024 if (length < 24)
6026 error_message = _("corrupt or empty %s section in %s");
6027 goto fail;
6030 if (bfd_seek (ibfd, asec->filepos, SEEK_SET) != 0
6031 || (bfd_bread (buffer + offset, length, ibfd) != length))
6033 error_message = _("unable to read in %s section from %s");
6034 goto fail;
6037 /* Process the contents of the section. */
6038 ptr = buffer + offset;
6039 error_message = _("corrupt %s section in %s");
6041 /* Verify the contents of the header. Note - we have to
6042 extract the values this way in order to allow for a
6043 host whose endian-ness is different from the target. */
6044 datum = bfd_get_32 (ibfd, ptr);
6045 if (datum != sizeof APUINFO_LABEL)
6046 goto fail;
6048 datum = bfd_get_32 (ibfd, ptr + 8);
6049 if (datum != 0x2)
6050 goto fail;
6052 if (strcmp (ptr + 12, APUINFO_LABEL) != 0)
6053 goto fail;
6055 /* Get the number of bytes used for apuinfo entries. */
6056 datum = bfd_get_32 (ibfd, ptr + 4);
6057 if (datum + 20 != length)
6058 goto fail;
6060 /* Make sure that we do not run off the end of the section. */
6061 if (offset + length > output_section_size)
6062 goto fail;
6064 /* Scan the apuinfo section, building a list of apuinfo numbers. */
6065 for (i = 0; i < datum; i += 4)
6066 apuinfo_list_add (bfd_get_32 (ibfd, ptr + 20 + i));
6068 /* Update the offset. */
6069 offset += length;
6072 error_message = NULL;
6074 /* Compute the size of the output section. */
6075 num_entries = apuinfo_list_length ();
6076 output_section_size = 20 + num_entries * 4;
6078 asec = bfd_get_section_by_name (abfd, APUINFO_SECTION_NAME);
6080 if (! bfd_set_section_size (abfd, asec, output_section_size))
6081 ibfd = abfd,
6082 error_message = _("warning: unable to set size of %s section in %s");
6084 fail:
6085 free (buffer);
6087 if (error_message)
6088 (*_bfd_error_handler) (error_message, APUINFO_SECTION_NAME,
6089 bfd_archive_filename (ibfd));
6093 /* Prevent the output section from accumulating the input sections'
6094 contents. We have already stored this in our linked list structure. */
6096 static bfd_boolean
6097 ppc_elf_write_section (bfd *abfd ATTRIBUTE_UNUSED,
6098 asection *asec,
6099 bfd_byte *contents ATTRIBUTE_UNUSED)
6101 return (apuinfo_list_length ()
6102 && strcmp (asec->name, APUINFO_SECTION_NAME) == 0);
6106 /* Finally we can generate the output section. */
6108 static void
6109 ppc_elf_final_write_processing (bfd *abfd, bfd_boolean linker ATTRIBUTE_UNUSED)
6111 bfd_byte *buffer;
6112 asection *asec;
6113 unsigned i;
6114 unsigned num_entries;
6115 bfd_size_type length;
6117 asec = bfd_get_section_by_name (abfd, APUINFO_SECTION_NAME);
6118 if (asec == NULL)
6119 return;
6121 if (apuinfo_list_length () == 0)
6122 return;
6124 length = asec->size;
6125 if (length < 20)
6126 return;
6128 buffer = bfd_malloc (length);
6129 if (buffer == NULL)
6131 (*_bfd_error_handler)
6132 (_("failed to allocate space for new APUinfo section."));
6133 return;
6136 /* Create the apuinfo header. */
6137 num_entries = apuinfo_list_length ();
6138 bfd_put_32 (abfd, sizeof APUINFO_LABEL, buffer);
6139 bfd_put_32 (abfd, num_entries * 4, buffer + 4);
6140 bfd_put_32 (abfd, 0x2, buffer + 8);
6141 strcpy (buffer + 12, APUINFO_LABEL);
6143 length = 20;
6144 for (i = 0; i < num_entries; i++)
6146 bfd_put_32 (abfd, apuinfo_list_element (i), buffer + length);
6147 length += 4;
6150 if (length != asec->size)
6151 (*_bfd_error_handler) (_("failed to compute new APUinfo section."));
6153 if (! bfd_set_section_contents (abfd, asec, buffer, (file_ptr) 0, length))
6154 (*_bfd_error_handler) (_("failed to install new APUinfo section."));
6156 free (buffer);
6158 apuinfo_list_finish ();
6161 /* Return address for Ith PLT stub in section PLT, for relocation REL
6162 or (bfd_vma) -1 if it should not be included. */
6164 static bfd_vma
6165 ppc_elf_plt_sym_val (bfd_vma i ATTRIBUTE_UNUSED,
6166 const asection *plt ATTRIBUTE_UNUSED,
6167 const arelent *rel)
6169 return rel->address;
6172 /* Add extra PPC sections -- Note, for now, make .sbss2 and
6173 .PPC.EMB.sbss0 a normal section, and not a bss section so
6174 that the linker doesn't crater when trying to make more than
6175 2 sections. */
6177 static struct bfd_elf_special_section const ppc_elf_special_sections[]=
6179 { ".tags", 5, 0, SHT_ORDERED, SHF_ALLOC },
6180 { ".sdata", 6, -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
6181 { ".sbss", 5, -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
6182 { ".sdata2", 7, -2, SHT_PROGBITS, SHF_ALLOC },
6183 { ".sbss2", 6, -2, SHT_PROGBITS, SHF_ALLOC },
6184 { ".PPC.EMB.apuinfo", 16, 0, SHT_NOTE, 0 },
6185 { ".PPC.EMB.sdata0", 15, 0, SHT_PROGBITS, SHF_ALLOC },
6186 { ".PPC.EMB.sbss0", 14, 0, SHT_PROGBITS, SHF_ALLOC },
6187 { ".plt", 4, 0, SHT_NOBITS, SHF_ALLOC + SHF_EXECINSTR },
6188 { NULL, 0, 0, 0, 0 }
6191 #define TARGET_LITTLE_SYM bfd_elf32_powerpcle_vec
6192 #define TARGET_LITTLE_NAME "elf32-powerpcle"
6193 #define TARGET_BIG_SYM bfd_elf32_powerpc_vec
6194 #define TARGET_BIG_NAME "elf32-powerpc"
6195 #define ELF_ARCH bfd_arch_powerpc
6196 #define ELF_MACHINE_CODE EM_PPC
6197 #ifdef __QNXTARGET__
6198 #define ELF_MAXPAGESIZE 0x1000
6199 #else
6200 #define ELF_MAXPAGESIZE 0x10000
6201 #endif
6202 #define elf_info_to_howto ppc_elf_info_to_howto
6204 #ifdef EM_CYGNUS_POWERPC
6205 #define ELF_MACHINE_ALT1 EM_CYGNUS_POWERPC
6206 #endif
6208 #ifdef EM_PPC_OLD
6209 #define ELF_MACHINE_ALT2 EM_PPC_OLD
6210 #endif
6212 #define elf_backend_plt_not_loaded 1
6213 #define elf_backend_got_symbol_offset 4
6214 #define elf_backend_can_gc_sections 1
6215 #define elf_backend_can_refcount 1
6216 #define elf_backend_got_header_size 12
6217 #define elf_backend_rela_normal 1
6219 #define bfd_elf32_mkobject ppc_elf_mkobject
6220 #define bfd_elf32_bfd_merge_private_bfd_data ppc_elf_merge_private_bfd_data
6221 #define bfd_elf32_bfd_relax_section ppc_elf_relax_section
6222 #define bfd_elf32_bfd_reloc_type_lookup ppc_elf_reloc_type_lookup
6223 #define bfd_elf32_bfd_set_private_flags ppc_elf_set_private_flags
6224 #define bfd_elf32_bfd_link_hash_table_create ppc_elf_link_hash_table_create
6226 #define elf_backend_object_p ppc_elf_object_p
6227 #define elf_backend_gc_mark_hook ppc_elf_gc_mark_hook
6228 #define elf_backend_gc_sweep_hook ppc_elf_gc_sweep_hook
6229 #define elf_backend_section_from_shdr ppc_elf_section_from_shdr
6230 #define elf_backend_relocate_section ppc_elf_relocate_section
6231 #define elf_backend_create_dynamic_sections ppc_elf_create_dynamic_sections
6232 #define elf_backend_check_relocs ppc_elf_check_relocs
6233 #define elf_backend_copy_indirect_symbol ppc_elf_copy_indirect_symbol
6234 #define elf_backend_adjust_dynamic_symbol ppc_elf_adjust_dynamic_symbol
6235 #define elf_backend_add_symbol_hook ppc_elf_add_symbol_hook
6236 #define elf_backend_size_dynamic_sections ppc_elf_size_dynamic_sections
6237 #define elf_backend_finish_dynamic_symbol ppc_elf_finish_dynamic_symbol
6238 #define elf_backend_finish_dynamic_sections ppc_elf_finish_dynamic_sections
6239 #define elf_backend_fake_sections ppc_elf_fake_sections
6240 #define elf_backend_additional_program_headers ppc_elf_additional_program_headers
6241 #define elf_backend_modify_segment_map ppc_elf_modify_segment_map
6242 #define elf_backend_grok_prstatus ppc_elf_grok_prstatus
6243 #define elf_backend_grok_psinfo ppc_elf_grok_psinfo
6244 #define elf_backend_reloc_type_class ppc_elf_reloc_type_class
6245 #define elf_backend_begin_write_processing ppc_elf_begin_write_processing
6246 #define elf_backend_final_write_processing ppc_elf_final_write_processing
6247 #define elf_backend_write_section ppc_elf_write_section
6248 #define elf_backend_special_sections ppc_elf_special_sections
6249 #define elf_backend_plt_sym_val ppc_elf_plt_sym_val
6251 #include "elf32-target.h"