* config.bfd: Add x86-64 vector to NetBSD/i386 if 64bit BFD is
[binutils.git] / bfd / elf32-ppc.c
blob8bd7115bf9019d0320aadd5d7236a68b357a7e15
1 /* PowerPC-specific support for 32-bit ELF
2 Copyright 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
3 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 /* If needed, initialize this section's cooked size. */
1683 if (isec->_cooked_size == 0)
1684 isec->_cooked_size = isec->_raw_size;
1686 trampoff = (isec->_cooked_size + 3) & (bfd_vma) -4;
1687 /* Space for a branch around any trampolines. */
1688 trampoff += 4;
1690 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1692 /* Get a copy of the native relocations. */
1693 internal_relocs = _bfd_elf_link_read_relocs (abfd, isec, NULL, NULL,
1694 link_info->keep_memory);
1695 if (internal_relocs == NULL)
1696 goto error_return;
1698 ppc_info = ppc_elf_hash_table (link_info);
1699 irelend = internal_relocs + isec->reloc_count;
1701 /* Get the section contents. */
1702 /* Get cached copy if it exists. */
1703 if (elf_section_data (isec)->this_hdr.contents != NULL)
1704 contents = elf_section_data (isec)->this_hdr.contents;
1705 else
1707 /* Go get them off disk. */
1708 contents = bfd_malloc (isec->_raw_size);
1709 if (contents == NULL)
1710 goto error_return;
1712 if (!bfd_get_section_contents (abfd, isec, contents, 0, isec->_raw_size))
1713 goto error_return;
1716 for (irel = internal_relocs; irel < irelend; irel++)
1718 unsigned long r_type = ELF32_R_TYPE (irel->r_info);
1719 bfd_vma symaddr, reladdr, toff, roff;
1720 asection *tsec;
1721 struct one_fixup *f;
1722 size_t insn_offset = 0;
1723 bfd_vma max_branch_offset, val;
1724 bfd_byte *hit_addr;
1725 unsigned long t0;
1727 switch (r_type)
1729 case R_PPC_REL24:
1730 case R_PPC_LOCAL24PC:
1731 case R_PPC_PLTREL24:
1732 max_branch_offset = 1 << 25;
1733 break;
1735 case R_PPC_REL14:
1736 case R_PPC_REL14_BRTAKEN:
1737 case R_PPC_REL14_BRNTAKEN:
1738 max_branch_offset = 1 << 15;
1739 break;
1741 default:
1742 continue;
1745 /* Get the value of the symbol referred to by the reloc. */
1746 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
1748 /* A local symbol. */
1749 Elf_Internal_Sym *isym;
1751 /* Read this BFD's local symbols. */
1752 if (isymbuf == NULL)
1754 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
1755 if (isymbuf == NULL)
1756 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
1757 symtab_hdr->sh_info, 0,
1758 NULL, NULL, NULL);
1759 if (isymbuf == 0)
1760 goto error_return;
1762 isym = isymbuf + ELF32_R_SYM (irel->r_info);
1763 if (isym->st_shndx == SHN_UNDEF)
1764 continue; /* We can't do anything with undefined symbols. */
1765 else if (isym->st_shndx == SHN_ABS)
1766 tsec = bfd_abs_section_ptr;
1767 else if (isym->st_shndx == SHN_COMMON)
1768 tsec = bfd_com_section_ptr;
1769 else
1770 tsec = bfd_section_from_elf_index (abfd, isym->st_shndx);
1772 toff = isym->st_value;
1774 else
1776 /* Global symbol handling. */
1777 unsigned long indx;
1778 struct elf_link_hash_entry *h;
1780 indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
1781 h = elf_sym_hashes (abfd)[indx];
1783 while (h->root.type == bfd_link_hash_indirect
1784 || h->root.type == bfd_link_hash_warning)
1785 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1787 if (r_type == R_PPC_PLTREL24
1788 && ppc_info->plt != NULL
1789 && h->plt.offset != (bfd_vma) -1)
1791 tsec = ppc_info->plt;
1792 toff = h->plt.offset;
1794 else if (h->root.type == bfd_link_hash_defined
1795 || h->root.type == bfd_link_hash_defweak)
1797 tsec = h->root.u.def.section;
1798 toff = h->root.u.def.value;
1800 else
1801 continue;
1804 /* If the branch and target are in the same section, you have
1805 no hope of adding stubs. We'll error out later should the
1806 branch overflow. */
1807 if (tsec == isec)
1808 continue;
1810 toff += irel->r_addend;
1811 if (tsec->sec_info_type == ELF_INFO_TYPE_MERGE)
1812 toff = _bfd_merged_section_offset (abfd, &tsec,
1813 elf_section_data (tsec)->sec_info,
1814 toff, 0);
1816 symaddr = tsec->output_section->vma + tsec->output_offset + toff;
1818 roff = irel->r_offset;
1820 reladdr = (isec->output_section->vma
1821 + isec->output_offset
1822 + roff);
1824 /* If the branch is in range, no need to do anything. */
1825 if (symaddr - reladdr + max_branch_offset < 2 * max_branch_offset)
1826 continue;
1828 /* Look for an existing fixup to this address. */
1829 for (f = fixups; f ; f = f->next)
1830 if (f->tsec == tsec && f->toff == toff)
1831 break;
1833 if (f == NULL)
1835 size_t size;
1836 unsigned long stub_rtype;
1838 val = trampoff - roff;
1839 if (val >= max_branch_offset)
1840 /* Oh dear, we can't reach a trampoline. Don't try to add
1841 one. We'll report an error later. */
1842 continue;
1844 if (link_info->shared)
1846 size = 4 * ARRAY_SIZE (shared_stub_entry);
1847 insn_offset = 12;
1848 stub_rtype = R_PPC_RELAX32PC;
1850 else
1852 size = 4 * ARRAY_SIZE (stub_entry);
1853 insn_offset = 0;
1854 stub_rtype = R_PPC_RELAX32;
1857 /* Hijack the old relocation. Since we need two
1858 relocations for this use a "composite" reloc. */
1859 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
1860 stub_rtype);
1861 irel->r_offset = trampoff + insn_offset;
1863 /* Record the fixup so we don't do it again this section. */
1864 f = bfd_malloc (sizeof (*f));
1865 f->next = fixups;
1866 f->tsec = tsec;
1867 f->toff = toff;
1868 f->trampoff = trampoff;
1869 fixups = f;
1871 trampoff += size;
1873 else
1875 val = f->trampoff - roff;
1876 if (val >= max_branch_offset)
1877 continue;
1879 /* Nop out the reloc, since we're finalizing things here. */
1880 irel->r_info = ELF32_R_INFO (0, R_PPC_NONE);
1883 /* Fix up the existing branch to hit the trampoline. */
1884 hit_addr = contents + roff;
1885 switch (r_type)
1887 case R_PPC_REL24:
1888 case R_PPC_LOCAL24PC:
1889 case R_PPC_PLTREL24:
1890 t0 = bfd_get_32 (abfd, hit_addr);
1891 t0 &= ~0x3fffffc;
1892 t0 |= val & 0x3fffffc;
1893 bfd_put_32 (abfd, t0, hit_addr);
1894 break;
1896 case R_PPC_REL14:
1897 case R_PPC_REL14_BRTAKEN:
1898 case R_PPC_REL14_BRNTAKEN:
1899 t0 = bfd_get_32 (abfd, hit_addr);
1900 t0 &= ~0xfffc;
1901 t0 |= val & 0xfffc;
1902 bfd_put_32 (abfd, t0, hit_addr);
1903 break;
1907 /* Write out the trampolines. */
1908 changed = fixups != NULL;
1909 if (fixups != NULL)
1911 const int *stub;
1912 bfd_byte *dest;
1913 bfd_vma val;
1914 int i, size;
1918 struct one_fixup *f = fixups;
1919 fixups = fixups->next;
1920 free (f);
1922 while (fixups);
1924 contents = bfd_realloc (contents, trampoff);
1925 if (contents == NULL)
1926 goto error_return;
1928 isec->_cooked_size = (isec->_cooked_size + 3) & (bfd_vma) -4;
1929 /* Branch around the trampolines. */
1930 val = trampoff - isec->_cooked_size + 0x48000000;
1931 dest = contents + isec->_cooked_size;
1932 isec->_cooked_size = trampoff;
1933 bfd_put_32 (abfd, val, dest);
1934 dest += 4;
1936 if (link_info->shared)
1938 stub = shared_stub_entry;
1939 size = ARRAY_SIZE (shared_stub_entry);
1941 else
1943 stub = stub_entry;
1944 size = ARRAY_SIZE (stub_entry);
1947 i = 0;
1948 while (dest < contents + trampoff)
1950 bfd_put_32 (abfd, stub[i], dest);
1951 i++;
1952 if (i == size)
1953 i = 0;
1954 dest += 4;
1956 BFD_ASSERT (i == 0);
1959 if (isymbuf != NULL
1960 && symtab_hdr->contents != (unsigned char *) isymbuf)
1962 if (! link_info->keep_memory)
1963 free (isymbuf);
1964 else
1966 /* Cache the symbols for elf_link_input_bfd. */
1967 symtab_hdr->contents = (unsigned char *) isymbuf;
1971 if (contents != NULL
1972 && elf_section_data (isec)->this_hdr.contents != contents)
1974 if (!changed && !link_info->keep_memory)
1975 free (contents);
1976 else
1978 /* Cache the section contents for elf_link_input_bfd. */
1979 elf_section_data (isec)->this_hdr.contents = contents;
1983 if (elf_section_data (isec)->relocs != internal_relocs)
1985 if (!changed)
1986 free (internal_relocs);
1987 else
1988 elf_section_data (isec)->relocs = internal_relocs;
1991 *again = changed;
1992 return TRUE;
1994 error_return:
1995 if (isymbuf != NULL && (unsigned char *) isymbuf != symtab_hdr->contents)
1996 free (isymbuf);
1997 if (contents != NULL
1998 && elf_section_data (isec)->this_hdr.contents != contents)
1999 free (contents);
2000 if (internal_relocs != NULL
2001 && elf_section_data (isec)->relocs != internal_relocs)
2002 free (internal_relocs);
2003 return FALSE;
2006 static reloc_howto_type *
2007 ppc_elf_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
2008 bfd_reloc_code_real_type code)
2010 enum elf_ppc_reloc_type r;
2012 /* Initialize howto table if not already done. */
2013 if (!ppc_elf_howto_table[R_PPC_ADDR32])
2014 ppc_elf_howto_init ();
2016 switch (code)
2018 default:
2019 return NULL;
2021 case BFD_RELOC_NONE: r = R_PPC_NONE; break;
2022 case BFD_RELOC_32: r = R_PPC_ADDR32; break;
2023 case BFD_RELOC_PPC_BA26: r = R_PPC_ADDR24; break;
2024 case BFD_RELOC_16: r = R_PPC_ADDR16; break;
2025 case BFD_RELOC_LO16: r = R_PPC_ADDR16_LO; break;
2026 case BFD_RELOC_HI16: r = R_PPC_ADDR16_HI; break;
2027 case BFD_RELOC_HI16_S: r = R_PPC_ADDR16_HA; break;
2028 case BFD_RELOC_PPC_BA16: r = R_PPC_ADDR14; break;
2029 case BFD_RELOC_PPC_BA16_BRTAKEN: r = R_PPC_ADDR14_BRTAKEN; break;
2030 case BFD_RELOC_PPC_BA16_BRNTAKEN: r = R_PPC_ADDR14_BRNTAKEN; break;
2031 case BFD_RELOC_PPC_B26: r = R_PPC_REL24; break;
2032 case BFD_RELOC_PPC_B16: r = R_PPC_REL14; break;
2033 case BFD_RELOC_PPC_B16_BRTAKEN: r = R_PPC_REL14_BRTAKEN; break;
2034 case BFD_RELOC_PPC_B16_BRNTAKEN: r = R_PPC_REL14_BRNTAKEN; break;
2035 case BFD_RELOC_16_GOTOFF: r = R_PPC_GOT16; break;
2036 case BFD_RELOC_LO16_GOTOFF: r = R_PPC_GOT16_LO; break;
2037 case BFD_RELOC_HI16_GOTOFF: r = R_PPC_GOT16_HI; break;
2038 case BFD_RELOC_HI16_S_GOTOFF: r = R_PPC_GOT16_HA; break;
2039 case BFD_RELOC_24_PLT_PCREL: r = R_PPC_PLTREL24; break;
2040 case BFD_RELOC_PPC_COPY: r = R_PPC_COPY; break;
2041 case BFD_RELOC_PPC_GLOB_DAT: r = R_PPC_GLOB_DAT; break;
2042 case BFD_RELOC_PPC_LOCAL24PC: r = R_PPC_LOCAL24PC; break;
2043 case BFD_RELOC_32_PCREL: r = R_PPC_REL32; break;
2044 case BFD_RELOC_32_PLTOFF: r = R_PPC_PLT32; break;
2045 case BFD_RELOC_32_PLT_PCREL: r = R_PPC_PLTREL32; break;
2046 case BFD_RELOC_LO16_PLTOFF: r = R_PPC_PLT16_LO; break;
2047 case BFD_RELOC_HI16_PLTOFF: r = R_PPC_PLT16_HI; break;
2048 case BFD_RELOC_HI16_S_PLTOFF: r = R_PPC_PLT16_HA; break;
2049 case BFD_RELOC_GPREL16: r = R_PPC_SDAREL16; break;
2050 case BFD_RELOC_16_BASEREL: r = R_PPC_SECTOFF; break;
2051 case BFD_RELOC_LO16_BASEREL: r = R_PPC_SECTOFF_LO; break;
2052 case BFD_RELOC_HI16_BASEREL: r = R_PPC_SECTOFF_HI; break;
2053 case BFD_RELOC_HI16_S_BASEREL: r = R_PPC_SECTOFF_HA; break;
2054 case BFD_RELOC_CTOR: r = R_PPC_ADDR32; break;
2055 case BFD_RELOC_PPC_TOC16: r = R_PPC_TOC16; break;
2056 case BFD_RELOC_PPC_TLS: r = R_PPC_TLS; break;
2057 case BFD_RELOC_PPC_DTPMOD: r = R_PPC_DTPMOD32; break;
2058 case BFD_RELOC_PPC_TPREL16: r = R_PPC_TPREL16; break;
2059 case BFD_RELOC_PPC_TPREL16_LO: r = R_PPC_TPREL16_LO; break;
2060 case BFD_RELOC_PPC_TPREL16_HI: r = R_PPC_TPREL16_HI; break;
2061 case BFD_RELOC_PPC_TPREL16_HA: r = R_PPC_TPREL16_HA; break;
2062 case BFD_RELOC_PPC_TPREL: r = R_PPC_TPREL32; break;
2063 case BFD_RELOC_PPC_DTPREL16: r = R_PPC_DTPREL16; break;
2064 case BFD_RELOC_PPC_DTPREL16_LO: r = R_PPC_DTPREL16_LO; break;
2065 case BFD_RELOC_PPC_DTPREL16_HI: r = R_PPC_DTPREL16_HI; break;
2066 case BFD_RELOC_PPC_DTPREL16_HA: r = R_PPC_DTPREL16_HA; break;
2067 case BFD_RELOC_PPC_DTPREL: r = R_PPC_DTPREL32; break;
2068 case BFD_RELOC_PPC_GOT_TLSGD16: r = R_PPC_GOT_TLSGD16; break;
2069 case BFD_RELOC_PPC_GOT_TLSGD16_LO: r = R_PPC_GOT_TLSGD16_LO; break;
2070 case BFD_RELOC_PPC_GOT_TLSGD16_HI: r = R_PPC_GOT_TLSGD16_HI; break;
2071 case BFD_RELOC_PPC_GOT_TLSGD16_HA: r = R_PPC_GOT_TLSGD16_HA; break;
2072 case BFD_RELOC_PPC_GOT_TLSLD16: r = R_PPC_GOT_TLSLD16; break;
2073 case BFD_RELOC_PPC_GOT_TLSLD16_LO: r = R_PPC_GOT_TLSLD16_LO; break;
2074 case BFD_RELOC_PPC_GOT_TLSLD16_HI: r = R_PPC_GOT_TLSLD16_HI; break;
2075 case BFD_RELOC_PPC_GOT_TLSLD16_HA: r = R_PPC_GOT_TLSLD16_HA; break;
2076 case BFD_RELOC_PPC_GOT_TPREL16: r = R_PPC_GOT_TPREL16; break;
2077 case BFD_RELOC_PPC_GOT_TPREL16_LO: r = R_PPC_GOT_TPREL16_LO; break;
2078 case BFD_RELOC_PPC_GOT_TPREL16_HI: r = R_PPC_GOT_TPREL16_HI; break;
2079 case BFD_RELOC_PPC_GOT_TPREL16_HA: r = R_PPC_GOT_TPREL16_HA; break;
2080 case BFD_RELOC_PPC_GOT_DTPREL16: r = R_PPC_GOT_DTPREL16; break;
2081 case BFD_RELOC_PPC_GOT_DTPREL16_LO: r = R_PPC_GOT_DTPREL16_LO; break;
2082 case BFD_RELOC_PPC_GOT_DTPREL16_HI: r = R_PPC_GOT_DTPREL16_HI; break;
2083 case BFD_RELOC_PPC_GOT_DTPREL16_HA: r = R_PPC_GOT_DTPREL16_HA; break;
2084 case BFD_RELOC_PPC_EMB_NADDR32: r = R_PPC_EMB_NADDR32; break;
2085 case BFD_RELOC_PPC_EMB_NADDR16: r = R_PPC_EMB_NADDR16; break;
2086 case BFD_RELOC_PPC_EMB_NADDR16_LO: r = R_PPC_EMB_NADDR16_LO; break;
2087 case BFD_RELOC_PPC_EMB_NADDR16_HI: r = R_PPC_EMB_NADDR16_HI; break;
2088 case BFD_RELOC_PPC_EMB_NADDR16_HA: r = R_PPC_EMB_NADDR16_HA; break;
2089 case BFD_RELOC_PPC_EMB_SDAI16: r = R_PPC_EMB_SDAI16; break;
2090 case BFD_RELOC_PPC_EMB_SDA2I16: r = R_PPC_EMB_SDA2I16; break;
2091 case BFD_RELOC_PPC_EMB_SDA2REL: r = R_PPC_EMB_SDA2REL; break;
2092 case BFD_RELOC_PPC_EMB_SDA21: r = R_PPC_EMB_SDA21; break;
2093 case BFD_RELOC_PPC_EMB_MRKREF: r = R_PPC_EMB_MRKREF; break;
2094 case BFD_RELOC_PPC_EMB_RELSEC16: r = R_PPC_EMB_RELSEC16; break;
2095 case BFD_RELOC_PPC_EMB_RELST_LO: r = R_PPC_EMB_RELST_LO; break;
2096 case BFD_RELOC_PPC_EMB_RELST_HI: r = R_PPC_EMB_RELST_HI; break;
2097 case BFD_RELOC_PPC_EMB_RELST_HA: r = R_PPC_EMB_RELST_HA; break;
2098 case BFD_RELOC_PPC_EMB_BIT_FLD: r = R_PPC_EMB_BIT_FLD; break;
2099 case BFD_RELOC_PPC_EMB_RELSDA: r = R_PPC_EMB_RELSDA; break;
2100 case BFD_RELOC_VTABLE_INHERIT: r = R_PPC_GNU_VTINHERIT; break;
2101 case BFD_RELOC_VTABLE_ENTRY: r = R_PPC_GNU_VTENTRY; break;
2104 return ppc_elf_howto_table[r];
2107 /* Set the howto pointer for a PowerPC ELF reloc. */
2109 static void
2110 ppc_elf_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED,
2111 arelent *cache_ptr,
2112 Elf_Internal_Rela *dst)
2114 /* Initialize howto table if not already done. */
2115 if (!ppc_elf_howto_table[R_PPC_ADDR32])
2116 ppc_elf_howto_init ();
2118 BFD_ASSERT (ELF32_R_TYPE (dst->r_info) < (unsigned int) R_PPC_max);
2119 cache_ptr->howto = ppc_elf_howto_table[ELF32_R_TYPE (dst->r_info)];
2122 /* Handle the R_PPC_ADDR16_HA reloc. */
2124 static bfd_reloc_status_type
2125 ppc_elf_addr16_ha_reloc (bfd *abfd ATTRIBUTE_UNUSED,
2126 arelent *reloc_entry,
2127 asymbol *symbol,
2128 void *data ATTRIBUTE_UNUSED,
2129 asection *input_section,
2130 bfd *output_bfd,
2131 char **error_message ATTRIBUTE_UNUSED)
2133 bfd_vma relocation;
2135 if (output_bfd != NULL)
2137 reloc_entry->address += input_section->output_offset;
2138 return bfd_reloc_ok;
2141 if (reloc_entry->address > input_section->_cooked_size)
2142 return bfd_reloc_outofrange;
2144 if (bfd_is_com_section (symbol->section))
2145 relocation = 0;
2146 else
2147 relocation = symbol->value;
2149 relocation += symbol->section->output_section->vma;
2150 relocation += symbol->section->output_offset;
2151 relocation += reloc_entry->addend;
2153 reloc_entry->addend += (relocation & 0x8000) << 1;
2155 return bfd_reloc_continue;
2158 static bfd_reloc_status_type
2159 ppc_elf_unhandled_reloc (bfd *abfd,
2160 arelent *reloc_entry,
2161 asymbol *symbol,
2162 void *data,
2163 asection *input_section,
2164 bfd *output_bfd,
2165 char **error_message)
2167 /* If this is a relocatable link (output_bfd test tells us), just
2168 call the generic function. Any adjustment will be done at final
2169 link time. */
2170 if (output_bfd != NULL)
2171 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2172 input_section, output_bfd, error_message);
2174 if (error_message != NULL)
2176 static char buf[60];
2177 sprintf (buf, _("generic linker can't handle %s"),
2178 reloc_entry->howto->name);
2179 *error_message = buf;
2181 return bfd_reloc_dangerous;
2184 /* Fix bad default arch selected for a 32 bit input bfd when the
2185 default is 64 bit. */
2187 static bfd_boolean
2188 ppc_elf_object_p (bfd *abfd)
2190 if (abfd->arch_info->the_default && abfd->arch_info->bits_per_word == 64)
2192 Elf_Internal_Ehdr *i_ehdr = elf_elfheader (abfd);
2194 if (i_ehdr->e_ident[EI_CLASS] == ELFCLASS32)
2196 /* Relies on arch after 64 bit default being 32 bit default. */
2197 abfd->arch_info = abfd->arch_info->next;
2198 BFD_ASSERT (abfd->arch_info->bits_per_word == 32);
2201 return TRUE;
2204 /* Function to set whether a module needs the -mrelocatable bit set. */
2206 static bfd_boolean
2207 ppc_elf_set_private_flags (bfd *abfd, flagword flags)
2209 BFD_ASSERT (!elf_flags_init (abfd)
2210 || elf_elfheader (abfd)->e_flags == flags);
2212 elf_elfheader (abfd)->e_flags = flags;
2213 elf_flags_init (abfd) = TRUE;
2214 return TRUE;
2217 /* Merge backend specific data from an object file to the output
2218 object file when linking. */
2220 static bfd_boolean
2221 ppc_elf_merge_private_bfd_data (bfd *ibfd, bfd *obfd)
2223 flagword old_flags;
2224 flagword new_flags;
2225 bfd_boolean error;
2227 /* Check if we have the same endianess. */
2228 if (! _bfd_generic_verify_endian_match (ibfd, obfd))
2229 return FALSE;
2231 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
2232 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
2233 return TRUE;
2235 new_flags = elf_elfheader (ibfd)->e_flags;
2236 old_flags = elf_elfheader (obfd)->e_flags;
2237 if (!elf_flags_init (obfd))
2239 /* First call, no flags set. */
2240 elf_flags_init (obfd) = TRUE;
2241 elf_elfheader (obfd)->e_flags = new_flags;
2244 /* Compatible flags are ok. */
2245 else if (new_flags == old_flags)
2248 /* Incompatible flags. */
2249 else
2251 /* Warn about -mrelocatable mismatch. Allow -mrelocatable-lib
2252 to be linked with either. */
2253 error = FALSE;
2254 if ((new_flags & EF_PPC_RELOCATABLE) != 0
2255 && (old_flags & (EF_PPC_RELOCATABLE | EF_PPC_RELOCATABLE_LIB)) == 0)
2257 error = TRUE;
2258 (*_bfd_error_handler)
2259 (_("%s: compiled with -mrelocatable and linked with "
2260 "modules compiled normally"),
2261 bfd_archive_filename (ibfd));
2263 else if ((new_flags & (EF_PPC_RELOCATABLE | EF_PPC_RELOCATABLE_LIB)) == 0
2264 && (old_flags & EF_PPC_RELOCATABLE) != 0)
2266 error = TRUE;
2267 (*_bfd_error_handler)
2268 (_("%s: compiled normally and linked with "
2269 "modules compiled with -mrelocatable"),
2270 bfd_archive_filename (ibfd));
2273 /* The output is -mrelocatable-lib iff both the input files are. */
2274 if (! (new_flags & EF_PPC_RELOCATABLE_LIB))
2275 elf_elfheader (obfd)->e_flags &= ~EF_PPC_RELOCATABLE_LIB;
2277 /* The output is -mrelocatable iff it can't be -mrelocatable-lib,
2278 but each input file is either -mrelocatable or -mrelocatable-lib. */
2279 if (! (elf_elfheader (obfd)->e_flags & EF_PPC_RELOCATABLE_LIB)
2280 && (new_flags & (EF_PPC_RELOCATABLE_LIB | EF_PPC_RELOCATABLE))
2281 && (old_flags & (EF_PPC_RELOCATABLE_LIB | EF_PPC_RELOCATABLE)))
2282 elf_elfheader (obfd)->e_flags |= EF_PPC_RELOCATABLE;
2284 /* Do not warn about eabi vs. V.4 mismatch, just or in the bit if
2285 any module uses it. */
2286 elf_elfheader (obfd)->e_flags |= (new_flags & EF_PPC_EMB);
2288 new_flags &= ~(EF_PPC_RELOCATABLE | EF_PPC_RELOCATABLE_LIB | EF_PPC_EMB);
2289 old_flags &= ~(EF_PPC_RELOCATABLE | EF_PPC_RELOCATABLE_LIB | EF_PPC_EMB);
2291 /* Warn about any other mismatches. */
2292 if (new_flags != old_flags)
2294 error = TRUE;
2295 (*_bfd_error_handler)
2296 (_("%s: uses different e_flags (0x%lx) fields "
2297 "than previous modules (0x%lx)"),
2298 bfd_archive_filename (ibfd), (long) new_flags, (long) old_flags);
2301 if (error)
2303 bfd_set_error (bfd_error_bad_value);
2304 return FALSE;
2308 return TRUE;
2311 /* Handle a PowerPC specific section when reading an object file. This
2312 is called when elfcode.h finds a section with an unknown type. */
2314 static bfd_boolean
2315 ppc_elf_section_from_shdr (bfd *abfd, Elf_Internal_Shdr *hdr, const char *name)
2317 asection *newsect;
2318 flagword flags;
2320 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name))
2321 return FALSE;
2323 newsect = hdr->bfd_section;
2324 flags = bfd_get_section_flags (abfd, newsect);
2325 if (hdr->sh_flags & SHF_EXCLUDE)
2326 flags |= SEC_EXCLUDE;
2328 if (hdr->sh_type == SHT_ORDERED)
2329 flags |= SEC_SORT_ENTRIES;
2331 bfd_set_section_flags (abfd, newsect, flags);
2332 return TRUE;
2335 /* Set up any other section flags and such that may be necessary. */
2337 static bfd_boolean
2338 ppc_elf_fake_sections (bfd *abfd ATTRIBUTE_UNUSED,
2339 Elf_Internal_Shdr *shdr,
2340 asection *asect)
2342 if ((asect->flags & SEC_EXCLUDE) != 0)
2343 shdr->sh_flags |= SHF_EXCLUDE;
2345 if ((asect->flags & SEC_SORT_ENTRIES) != 0)
2346 shdr->sh_type = SHT_ORDERED;
2348 return TRUE;
2351 /* Find a linker generated pointer with a given addend and type. */
2353 static elf_linker_section_pointers_t *
2354 elf_find_pointer_linker_section
2355 (elf_linker_section_pointers_t *linker_pointers,
2356 bfd_vma addend,
2357 elf_linker_section_t *lsect)
2359 for ( ; linker_pointers != NULL; linker_pointers = linker_pointers->next)
2360 if (lsect == linker_pointers->lsect && addend == linker_pointers->addend)
2361 return linker_pointers;
2363 return NULL;
2366 /* Allocate a pointer to live in a linker created section. */
2368 static bfd_boolean
2369 elf_create_pointer_linker_section (bfd *abfd,
2370 struct bfd_link_info *info,
2371 elf_linker_section_t *lsect,
2372 struct elf_link_hash_entry *h,
2373 const Elf_Internal_Rela *rel)
2375 elf_linker_section_pointers_t **ptr_linker_section_ptr = NULL;
2376 elf_linker_section_pointers_t *linker_section_ptr;
2377 unsigned long r_symndx = ELF32_R_SYM (rel->r_info);
2378 bfd_size_type amt;
2380 BFD_ASSERT (lsect != NULL);
2382 /* Is this a global symbol? */
2383 if (h != NULL)
2385 struct ppc_elf_link_hash_entry *eh;
2387 /* Has this symbol already been allocated? If so, our work is done. */
2388 eh = (struct ppc_elf_link_hash_entry *) h;
2389 if (elf_find_pointer_linker_section (eh->linker_section_pointer,
2390 rel->r_addend,
2391 lsect))
2392 return TRUE;
2394 ptr_linker_section_ptr = &eh->linker_section_pointer;
2395 /* Make sure this symbol is output as a dynamic symbol. */
2396 if (h->dynindx == -1)
2398 if (! _bfd_elf_link_record_dynamic_symbol (info, h))
2399 return FALSE;
2402 if (lsect->rel_section)
2403 lsect->rel_section->_raw_size += sizeof (Elf32_External_Rela);
2405 else
2407 /* Allocation of a pointer to a local symbol. */
2408 elf_linker_section_pointers_t **ptr = elf_local_ptr_offsets (abfd);
2410 /* Allocate a table to hold the local symbols if first time. */
2411 if (!ptr)
2413 unsigned int num_symbols = elf_tdata (abfd)->symtab_hdr.sh_info;
2415 amt = num_symbols;
2416 amt *= sizeof (elf_linker_section_pointers_t *);
2417 ptr = bfd_zalloc (abfd, amt);
2419 if (!ptr)
2420 return FALSE;
2422 elf_local_ptr_offsets (abfd) = ptr;
2425 /* Has this symbol already been allocated? If so, our work is done. */
2426 if (elf_find_pointer_linker_section (ptr[r_symndx],
2427 rel->r_addend,
2428 lsect))
2429 return TRUE;
2431 ptr_linker_section_ptr = &ptr[r_symndx];
2433 if (info->shared)
2435 /* If we are generating a shared object, we need to
2436 output a R_<xxx>_RELATIVE reloc so that the
2437 dynamic linker can adjust this GOT entry. */
2438 BFD_ASSERT (lsect->rel_section != NULL);
2439 lsect->rel_section->_raw_size += sizeof (Elf32_External_Rela);
2443 /* Allocate space for a pointer in the linker section, and allocate
2444 a new pointer record from internal memory. */
2445 BFD_ASSERT (ptr_linker_section_ptr != NULL);
2446 amt = sizeof (elf_linker_section_pointers_t);
2447 linker_section_ptr = bfd_alloc (abfd, amt);
2449 if (!linker_section_ptr)
2450 return FALSE;
2452 linker_section_ptr->next = *ptr_linker_section_ptr;
2453 linker_section_ptr->addend = rel->r_addend;
2454 linker_section_ptr->lsect = lsect;
2455 linker_section_ptr->written_address_p = FALSE;
2456 *ptr_linker_section_ptr = linker_section_ptr;
2458 linker_section_ptr->offset = lsect->section->_raw_size;
2459 lsect->section->_raw_size += 4;
2461 #ifdef DEBUG
2462 fprintf (stderr,
2463 "Create pointer in linker section %s, offset = %ld, section size = %ld\n",
2464 lsect->name, (long) linker_section_ptr->offset,
2465 (long) lsect->section->_raw_size);
2466 #endif
2468 return TRUE;
2471 #define bfd_put_ptr(BFD, VAL, ADDR) bfd_put_32 (BFD, VAL, ADDR)
2473 /* Fill in the address for a pointer generated in a linker section. */
2475 static bfd_vma
2476 elf_finish_pointer_linker_section (bfd *output_bfd,
2477 bfd *input_bfd,
2478 struct bfd_link_info *info,
2479 elf_linker_section_t *lsect,
2480 struct elf_link_hash_entry *h,
2481 bfd_vma relocation,
2482 const Elf_Internal_Rela *rel,
2483 int relative_reloc)
2485 elf_linker_section_pointers_t *linker_section_ptr;
2487 BFD_ASSERT (lsect != NULL);
2489 if (h != NULL)
2491 /* Handle global symbol. */
2492 struct ppc_elf_link_hash_entry *eh;
2494 eh = (struct ppc_elf_link_hash_entry *) h;
2495 linker_section_ptr
2496 = elf_find_pointer_linker_section (eh->linker_section_pointer,
2497 rel->r_addend,
2498 lsect);
2500 BFD_ASSERT (linker_section_ptr != NULL);
2502 if (! elf_hash_table (info)->dynamic_sections_created
2503 || (info->shared
2504 && info->symbolic
2505 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR)))
2507 /* This is actually a static link, or it is a
2508 -Bsymbolic link and the symbol is defined
2509 locally. We must initialize this entry in the
2510 global section.
2512 When doing a dynamic link, we create a .rela.<xxx>
2513 relocation entry to initialize the value. This
2514 is done in the finish_dynamic_symbol routine. */
2515 if (!linker_section_ptr->written_address_p)
2517 linker_section_ptr->written_address_p = TRUE;
2518 bfd_put_ptr (output_bfd,
2519 relocation + linker_section_ptr->addend,
2520 (lsect->section->contents
2521 + linker_section_ptr->offset));
2525 else
2527 /* Handle local symbol. */
2528 unsigned long r_symndx = ELF32_R_SYM (rel->r_info);
2529 BFD_ASSERT (elf_local_ptr_offsets (input_bfd) != NULL);
2530 BFD_ASSERT (elf_local_ptr_offsets (input_bfd)[r_symndx] != NULL);
2531 linker_section_ptr = (elf_find_pointer_linker_section
2532 (elf_local_ptr_offsets (input_bfd)[r_symndx],
2533 rel->r_addend,
2534 lsect));
2536 BFD_ASSERT (linker_section_ptr != NULL);
2538 /* Write out pointer if it hasn't been rewritten out before. */
2539 if (!linker_section_ptr->written_address_p)
2541 linker_section_ptr->written_address_p = TRUE;
2542 bfd_put_ptr (output_bfd, relocation + linker_section_ptr->addend,
2543 lsect->section->contents + linker_section_ptr->offset);
2545 if (info->shared)
2547 /* We need to generate a relative reloc for the dynamic
2548 linker. */
2550 asection *srel = lsect->rel_section;
2551 Elf_Internal_Rela outrel[MAX_INT_RELS_PER_EXT_REL];
2552 bfd_byte *erel;
2553 const struct elf_backend_data *bed;
2554 unsigned int i;
2556 BFD_ASSERT (srel != NULL);
2558 bed = get_elf_backend_data (output_bfd);
2559 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
2561 outrel[i].r_offset = (lsect->section->output_section->vma
2562 + lsect->section->output_offset
2563 + linker_section_ptr->offset);
2564 outrel[i].r_info = 0;
2565 outrel[i].r_addend = 0;
2567 outrel[0].r_info = ELF32_R_INFO (0, relative_reloc);
2568 erel = lsect->section->contents;
2569 erel += (elf_section_data (lsect->section)->rel_count++
2570 * sizeof (Elf32_External_Rela));
2571 bfd_elf32_swap_reloca_out (output_bfd, outrel, erel);
2576 relocation = (lsect->section->output_offset
2577 + linker_section_ptr->offset
2578 - lsect->sym_offset);
2580 #ifdef DEBUG
2581 fprintf (stderr,
2582 "Finish pointer in linker section %s, offset = %ld (0x%lx)\n",
2583 lsect->name, (long) relocation, (long) relocation);
2584 #endif
2586 /* Subtract out the addend, because it will get added back in by the normal
2587 processing. */
2588 return relocation - linker_section_ptr->addend;
2591 /* Create a special linker section */
2592 static elf_linker_section_t *
2593 ppc_elf_create_linker_section (bfd *abfd,
2594 struct bfd_link_info *info,
2595 enum elf_linker_section_enum which)
2597 elf_linker_section_t *lsect;
2598 struct ppc_elf_link_hash_table *htab = ppc_elf_hash_table (info);
2599 asection *s;
2600 bfd_size_type amt;
2601 flagword flags;
2602 const char *name;
2603 const char *rel_name;
2604 const char *sym_name;
2605 bfd_vma sym_offset;
2607 /* Both of these sections are (technically) created by the user
2608 putting data in them, so they shouldn't be marked
2609 SEC_LINKER_CREATED.
2611 The linker creates them so it has somewhere to attach their
2612 respective symbols. In fact, if they were empty it would
2613 be OK to leave the symbol set to 0 (or any random number), because
2614 the appropriate register should never be used. */
2615 flags = SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY;
2616 sym_offset = 32768;
2618 switch (which)
2620 default:
2621 abort ();
2622 return NULL;
2624 case LINKER_SECTION_SDATA: /* .sdata/.sbss section */
2625 name = ".sdata";
2626 rel_name = ".rela.sdata";
2627 sym_name = "_SDA_BASE_";
2628 break;
2630 case LINKER_SECTION_SDATA2: /* .sdata2/.sbss2 section */
2631 name = ".sdata2";
2632 rel_name = ".rela.sdata2";
2633 sym_name = "_SDA2_BASE_";
2634 flags |= SEC_READONLY;
2635 break;
2638 /* Record the first bfd that needs the special sections. */
2639 if (!htab->elf.dynobj)
2640 htab->elf.dynobj = abfd;
2642 amt = sizeof (elf_linker_section_t);
2643 lsect = bfd_zalloc (htab->elf.dynobj, amt);
2645 lsect->sym_offset = sym_offset;
2647 /* See if the sections already exist. */
2648 s = bfd_get_section_by_name (htab->elf.dynobj, name);
2649 if (s == NULL || (s->flags & flags) != flags)
2651 s = bfd_make_section_anyway (htab->elf.dynobj, name);
2652 if (s == NULL
2653 || !bfd_set_section_flags (htab->elf.dynobj, s, flags))
2654 return NULL;
2656 lsect->section = s;
2658 if (bfd_get_section_alignment (htab->elf.dynobj, s) < 2
2659 && !bfd_set_section_alignment (htab->elf.dynobj, s, 2))
2660 return NULL;
2662 s->_raw_size = align_power (s->_raw_size, 2);
2664 #ifdef DEBUG
2665 fprintf (stderr, "Creating section %s, current size = %ld\n",
2666 name, (long) s->_raw_size);
2667 #endif
2669 if (sym_name)
2671 struct elf_link_hash_entry *h;
2672 struct bfd_link_hash_entry *bh;
2674 #ifdef DEBUG
2675 fprintf (stderr, "Adding %s to section %s\n", sym_name, name);
2676 #endif
2677 bh = bfd_link_hash_lookup (info->hash, sym_name,
2678 FALSE, FALSE, FALSE);
2680 if ((bh == NULL || bh->type == bfd_link_hash_undefined)
2681 && !(_bfd_generic_link_add_one_symbol
2682 (info, abfd, sym_name, BSF_GLOBAL, s, sym_offset, NULL,
2683 FALSE, get_elf_backend_data (abfd)->collect, &bh)))
2684 return NULL;
2685 h = (struct elf_link_hash_entry *) bh;
2687 h->type = STT_OBJECT;
2688 lsect->sym_hash = h;
2690 if (info->shared
2691 && ! _bfd_elf_link_record_dynamic_symbol (info, h))
2692 return NULL;
2695 if (info->shared)
2697 s = bfd_make_section_anyway (htab->elf.dynobj, rel_name);
2698 lsect->rel_section = s;
2699 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
2700 | SEC_LINKER_CREATED | SEC_READONLY);
2701 if (s == NULL
2702 || ! bfd_set_section_flags (htab->elf.dynobj, s, flags)
2703 || ! bfd_set_section_alignment (htab->elf.dynobj, s, 2))
2704 return NULL;
2707 return lsect;
2710 /* If we have a non-zero sized .sbss2 or .PPC.EMB.sbss0 sections, we
2711 need to bump up the number of section headers. */
2713 static int
2714 ppc_elf_additional_program_headers (bfd *abfd)
2716 asection *s;
2717 int ret;
2719 ret = 0;
2721 s = bfd_get_section_by_name (abfd, ".interp");
2722 if (s != NULL)
2723 ++ret;
2725 s = bfd_get_section_by_name (abfd, ".sbss2");
2726 if (s != NULL && (s->flags & SEC_LOAD) != 0 && s->_raw_size > 0)
2727 ++ret;
2729 s = bfd_get_section_by_name (abfd, ".PPC.EMB.sbss0");
2730 if (s != NULL && (s->flags & SEC_LOAD) != 0 && s->_raw_size > 0)
2731 ++ret;
2733 return ret;
2736 /* Modify the segment map if needed. */
2738 static bfd_boolean
2739 ppc_elf_modify_segment_map (bfd *abfd ATTRIBUTE_UNUSED,
2740 struct bfd_link_info *info ATTRIBUTE_UNUSED)
2742 return TRUE;
2745 /* The powerpc .got has a blrl instruction in it. Mark it executable. */
2747 static bfd_boolean
2748 ppc_elf_create_got (bfd *abfd, struct bfd_link_info *info)
2750 struct ppc_elf_link_hash_table *htab;
2751 asection *s;
2752 flagword flags;
2754 if (!_bfd_elf_create_got_section (abfd, info))
2755 return FALSE;
2757 htab = ppc_elf_hash_table (info);
2758 htab->got = s = bfd_get_section_by_name (abfd, ".got");
2759 if (s == NULL)
2760 abort ();
2762 flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_HAS_CONTENTS | SEC_IN_MEMORY
2763 | SEC_LINKER_CREATED);
2764 if (!bfd_set_section_flags (abfd, s, flags))
2765 return FALSE;
2767 htab->relgot = bfd_make_section (abfd, ".rela.got");
2768 if (!htab->relgot
2769 || ! bfd_set_section_flags (abfd, htab->relgot,
2770 (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
2771 | SEC_IN_MEMORY | SEC_LINKER_CREATED
2772 | SEC_READONLY))
2773 || ! bfd_set_section_alignment (abfd, htab->relgot, 2))
2774 return FALSE;
2776 return TRUE;
2779 /* We have to create .dynsbss and .rela.sbss here so that they get mapped
2780 to output sections (just like _bfd_elf_create_dynamic_sections has
2781 to create .dynbss and .rela.bss). */
2783 static bfd_boolean
2784 ppc_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
2786 struct ppc_elf_link_hash_table *htab;
2787 asection *s;
2788 flagword flags;
2790 htab = ppc_elf_hash_table (info);
2792 if (htab->got == NULL
2793 && !ppc_elf_create_got (abfd, info))
2794 return FALSE;
2796 if (!_bfd_elf_create_dynamic_sections (abfd, info))
2797 return FALSE;
2799 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
2800 | SEC_LINKER_CREATED);
2802 htab->dynbss = bfd_get_section_by_name (abfd, ".dynbss");
2803 htab->dynsbss = s = bfd_make_section (abfd, ".dynsbss");
2804 if (s == NULL
2805 || ! bfd_set_section_flags (abfd, s, SEC_ALLOC))
2806 return FALSE;
2808 if (! info->shared)
2810 htab->relbss = bfd_get_section_by_name (abfd, ".rela.bss");
2811 htab->relsbss = s = bfd_make_section (abfd, ".rela.sbss");
2812 if (s == NULL
2813 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
2814 || ! bfd_set_section_alignment (abfd, s, 2))
2815 return FALSE;
2818 htab->relplt = bfd_get_section_by_name (abfd, ".rela.plt");
2819 htab->plt = s = bfd_get_section_by_name (abfd, ".plt");
2820 if (s == NULL)
2821 abort ();
2823 flags = SEC_ALLOC | SEC_CODE | SEC_IN_MEMORY | SEC_LINKER_CREATED;
2824 return bfd_set_section_flags (abfd, s, flags);
2827 /* Adjust a symbol defined by a dynamic object and referenced by a
2828 regular object. The current definition is in some section of the
2829 dynamic object, but we're not including those sections. We have to
2830 change the definition to something the rest of the link can
2831 understand. */
2833 static bfd_boolean
2834 ppc_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
2835 struct elf_link_hash_entry *h)
2837 struct ppc_elf_link_hash_table *htab;
2838 asection *s;
2839 unsigned int power_of_two;
2841 #ifdef DEBUG
2842 fprintf (stderr, "ppc_elf_adjust_dynamic_symbol called for %s\n",
2843 h->root.root.string);
2844 #endif
2846 /* Make sure we know what is going on here. */
2847 htab = ppc_elf_hash_table (info);
2848 BFD_ASSERT (htab->elf.dynobj != NULL
2849 && ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT)
2850 || h->weakdef != NULL
2851 || ((h->elf_link_hash_flags
2852 & ELF_LINK_HASH_DEF_DYNAMIC) != 0
2853 && (h->elf_link_hash_flags
2854 & ELF_LINK_HASH_REF_REGULAR) != 0
2855 && (h->elf_link_hash_flags
2856 & ELF_LINK_HASH_DEF_REGULAR) == 0)));
2858 /* Deal with function syms. */
2859 if (h->type == STT_FUNC
2860 || (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0)
2862 /* Clear procedure linkage table information for any symbol that
2863 won't need a .plt entry. */
2864 if (h->plt.refcount <= 0
2865 || SYMBOL_CALLS_LOCAL (info, h)
2866 || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2867 && h->root.type == bfd_link_hash_undefweak))
2869 /* A PLT entry is not required/allowed when:
2871 1. We are not using ld.so; because then the PLT entry
2872 can't be set up, so we can't use one. In this case,
2873 ppc_elf_adjust_dynamic_symbol won't even be called.
2875 2. GC has rendered the entry unused.
2877 3. We know for certain that a call to this symbol
2878 will go to this object, or will remain undefined. */
2879 h->plt.offset = (bfd_vma) -1;
2880 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
2882 return TRUE;
2884 else
2885 h->plt.offset = (bfd_vma) -1;
2887 /* If this is a weak symbol, and there is a real definition, the
2888 processor independent code will have arranged for us to see the
2889 real definition first, and we can just use the same value. */
2890 if (h->weakdef != NULL)
2892 BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined
2893 || h->weakdef->root.type == bfd_link_hash_defweak);
2894 h->root.u.def.section = h->weakdef->root.u.def.section;
2895 h->root.u.def.value = h->weakdef->root.u.def.value;
2896 if (ELIMINATE_COPY_RELOCS)
2897 h->elf_link_hash_flags
2898 = ((h->elf_link_hash_flags & ~ELF_LINK_NON_GOT_REF)
2899 | (h->weakdef->elf_link_hash_flags & ELF_LINK_NON_GOT_REF));
2900 return TRUE;
2903 /* This is a reference to a symbol defined by a dynamic object which
2904 is not a function. */
2906 /* If we are creating a shared library, we must presume that the
2907 only references to the symbol are via the global offset table.
2908 For such cases we need not do anything here; the relocations will
2909 be handled correctly by relocate_section. */
2910 if (info->shared)
2911 return TRUE;
2913 /* If there are no references to this symbol that do not use the
2914 GOT, we don't need to generate a copy reloc. */
2915 if ((h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0)
2916 return TRUE;
2918 if (ELIMINATE_COPY_RELOCS)
2920 struct ppc_elf_dyn_relocs *p;
2921 for (p = ppc_elf_hash_entry (h)->dyn_relocs; p != NULL; p = p->next)
2923 s = p->sec->output_section;
2924 if (s != NULL && (s->flags & SEC_READONLY) != 0)
2925 break;
2928 /* If we didn't find any dynamic relocs in read-only sections, then
2929 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
2930 if (p == NULL)
2932 h->elf_link_hash_flags &= ~ELF_LINK_NON_GOT_REF;
2933 return TRUE;
2937 /* We must allocate the symbol in our .dynbss section, which will
2938 become part of the .bss section of the executable. There will be
2939 an entry for this symbol in the .dynsym section. The dynamic
2940 object will contain position independent code, so all references
2941 from the dynamic object to this symbol will go through the global
2942 offset table. The dynamic linker will use the .dynsym entry to
2943 determine the address it must put in the global offset table, so
2944 both the dynamic object and the regular object will refer to the
2945 same memory location for the variable.
2947 Of course, if the symbol is sufficiently small, we must instead
2948 allocate it in .sbss. FIXME: It would be better to do this if and
2949 only if there were actually SDAREL relocs for that symbol. */
2951 if (h->size <= elf_gp_size (htab->elf.dynobj))
2952 s = htab->dynsbss;
2953 else
2954 s = htab->dynbss;
2955 BFD_ASSERT (s != NULL);
2957 /* We must generate a R_PPC_COPY reloc to tell the dynamic linker to
2958 copy the initial value out of the dynamic object and into the
2959 runtime process image. We need to remember the offset into the
2960 .rela.bss section we are going to use. */
2961 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
2963 asection *srel;
2965 if (h->size <= elf_gp_size (htab->elf.dynobj))
2966 srel = htab->relsbss;
2967 else
2968 srel = htab->relbss;
2969 BFD_ASSERT (srel != NULL);
2970 srel->_raw_size += sizeof (Elf32_External_Rela);
2971 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_COPY;
2974 /* We need to figure out the alignment required for this symbol. I
2975 have no idea how ELF linkers handle this. */
2976 power_of_two = bfd_log2 (h->size);
2977 if (power_of_two > 4)
2978 power_of_two = 4;
2980 /* Apply the required alignment. */
2981 s->_raw_size = BFD_ALIGN (s->_raw_size,
2982 (bfd_size_type) (1 << power_of_two));
2983 if (power_of_two > bfd_get_section_alignment (htab->elf.dynobj, s))
2985 if (! bfd_set_section_alignment (htab->elf.dynobj, s, power_of_two))
2986 return FALSE;
2989 /* Define the symbol as being at this point in the section. */
2990 h->root.u.def.section = s;
2991 h->root.u.def.value = s->_raw_size;
2993 /* Increment the section size to make room for the symbol. */
2994 s->_raw_size += h->size;
2996 return TRUE;
2999 /* This is the condition under which finish_dynamic_symbol will be
3000 called from elflink.h. If elflink.h doesn't call our
3001 finish_dynamic_symbol routine, we'll need to do something about
3002 initializing any .plt and .got entries in relocate_section. */
3003 #define WILL_CALL_FINISH_DYNAMIC_SYMBOL(DYN, SHARED, H) \
3004 ((DYN) \
3005 && ((SHARED) \
3006 || ((H)->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0) \
3007 && ((H)->dynindx != -1 \
3008 || ((H)->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0))
3010 /* Of those relocs that might be copied as dynamic relocs, this macro
3011 selects those that must be copied when linking a shared library,
3012 even when the symbol is local. */
3014 #define MUST_BE_DYN_RELOC(RTYPE) \
3015 ((RTYPE) != R_PPC_REL24 \
3016 && (RTYPE) != R_PPC_REL14 \
3017 && (RTYPE) != R_PPC_REL14_BRTAKEN \
3018 && (RTYPE) != R_PPC_REL14_BRNTAKEN \
3019 && (RTYPE) != R_PPC_REL32)
3021 /* Allocate space in associated reloc sections for dynamic relocs. */
3023 static bfd_boolean
3024 allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
3026 struct bfd_link_info *info = inf;
3027 struct ppc_elf_link_hash_entry *eh;
3028 struct ppc_elf_link_hash_table *htab;
3029 struct ppc_elf_dyn_relocs *p;
3031 if (h->root.type == bfd_link_hash_indirect)
3032 return TRUE;
3034 if (h->root.type == bfd_link_hash_warning)
3035 /* When warning symbols are created, they **replace** the "real"
3036 entry in the hash table, thus we never get to see the real
3037 symbol in a hash traversal. So look at it now. */
3038 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3040 htab = ppc_elf_hash_table (info);
3041 if (htab->elf.dynamic_sections_created
3042 && h->plt.refcount > 0)
3044 /* Make sure this symbol is output as a dynamic symbol. */
3045 if (h->dynindx == -1
3046 && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
3048 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
3049 return FALSE;
3052 if (info->shared
3053 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h))
3055 asection *s = htab->plt;
3057 /* If this is the first .plt entry, make room for the special
3058 first entry. */
3059 if (s->_raw_size == 0)
3060 s->_raw_size += PLT_INITIAL_ENTRY_SIZE;
3062 /* The PowerPC PLT is actually composed of two parts, the
3063 first part is 2 words (for a load and a jump), and then
3064 there is a remaining word available at the end. */
3065 h->plt.offset = (PLT_INITIAL_ENTRY_SIZE
3066 + (PLT_SLOT_SIZE
3067 * ((s->_raw_size - PLT_INITIAL_ENTRY_SIZE)
3068 / PLT_ENTRY_SIZE)));
3070 /* If this symbol is not defined in a regular file, and we
3071 are not generating a shared library, then set the symbol
3072 to this location in the .plt. This is required to make
3073 function pointers compare as equal between the normal
3074 executable and the shared library. */
3075 if (! info->shared
3076 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
3078 h->root.u.def.section = s;
3079 h->root.u.def.value = h->plt.offset;
3082 /* Make room for this entry. After the 8192nd entry, room
3083 for two entries is allocated. */
3084 s->_raw_size += PLT_ENTRY_SIZE;
3085 if ((s->_raw_size - PLT_INITIAL_ENTRY_SIZE) / PLT_ENTRY_SIZE
3086 > PLT_NUM_SINGLE_ENTRIES)
3087 s->_raw_size += PLT_ENTRY_SIZE;
3089 /* We also need to make an entry in the .rela.plt section. */
3090 htab->relplt->_raw_size += sizeof (Elf32_External_Rela);
3092 else
3094 h->plt.offset = (bfd_vma) -1;
3095 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
3098 else
3100 h->plt.offset = (bfd_vma) -1;
3101 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
3104 eh = (struct ppc_elf_link_hash_entry *) h;
3105 if (eh->elf.got.refcount > 0)
3107 /* Make sure this symbol is output as a dynamic symbol. */
3108 if (eh->elf.dynindx == -1
3109 && (eh->elf.elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
3111 if (!bfd_elf32_link_record_dynamic_symbol (info, &eh->elf))
3112 return FALSE;
3115 if (eh->tls_mask == (TLS_TLS | TLS_LD)
3116 && !(eh->elf.elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC))
3117 /* If just an LD reloc, we'll just use htab->tlsld_got.offset. */
3118 eh->elf.got.offset = (bfd_vma) -1;
3119 else
3121 bfd_boolean dyn;
3122 eh->elf.got.offset = htab->got->_raw_size;
3123 if ((eh->tls_mask & TLS_TLS) != 0)
3125 if ((eh->tls_mask & TLS_LD) != 0)
3126 htab->got->_raw_size += 8;
3127 if ((eh->tls_mask & TLS_GD) != 0)
3128 htab->got->_raw_size += 8;
3129 if ((eh->tls_mask & (TLS_TPREL | TLS_TPRELGD)) != 0)
3130 htab->got->_raw_size += 4;
3131 if ((eh->tls_mask & TLS_DTPREL) != 0)
3132 htab->got->_raw_size += 4;
3134 else
3135 htab->got->_raw_size += 4;
3136 dyn = htab->elf.dynamic_sections_created;
3137 if ((info->shared
3138 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, &eh->elf))
3139 && (ELF_ST_VISIBILITY (eh->elf.other) == STV_DEFAULT
3140 || eh->elf.root.type != bfd_link_hash_undefweak))
3142 /* All the entries we allocated need relocs. */
3143 htab->relgot->_raw_size
3144 += ((htab->got->_raw_size - eh->elf.got.offset) / 4
3145 * sizeof (Elf32_External_Rela));
3146 /* Except LD only needs one. */
3147 if ((eh->tls_mask & TLS_LD) != 0)
3148 htab->relgot->_raw_size -= sizeof (Elf32_External_Rela);
3152 else
3153 eh->elf.got.offset = (bfd_vma) -1;
3155 if (eh->dyn_relocs == NULL)
3156 return TRUE;
3158 /* In the shared -Bsymbolic case, discard space allocated for
3159 dynamic pc-relative relocs against symbols which turn out to be
3160 defined in regular objects. For the normal shared case, discard
3161 space for relocs that have become local due to symbol visibility
3162 changes. */
3164 if (info->shared)
3166 /* Relocs that use pc_count are those that appear on a call insn,
3167 or certain REL relocs (see MUST_BE_DYN_RELOC) that can be
3168 generated via assembly. We want calls to protected symbols to
3169 resolve directly to the function rather than going via the plt.
3170 If people want function pointer comparisons to work as expected
3171 then they should avoid writing weird assembly. */
3172 if (SYMBOL_CALLS_LOCAL (info, h))
3174 struct ppc_elf_dyn_relocs **pp;
3176 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
3178 p->count -= p->pc_count;
3179 p->pc_count = 0;
3180 if (p->count == 0)
3181 *pp = p->next;
3182 else
3183 pp = &p->next;
3187 /* Also discard relocs on undefined weak syms with non-default
3188 visibility. */
3189 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
3190 && h->root.type == bfd_link_hash_undefweak)
3191 eh->dyn_relocs = NULL;
3193 /* Make sure undefined weak symbols are output as a dynamic symbol
3194 in PIEs. */
3195 if (info->pie
3196 && eh->dyn_relocs != NULL
3197 && h->dynindx == -1
3198 && h->root.type == bfd_link_hash_undefweak
3199 && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
3201 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
3202 return FALSE;
3205 else if (ELIMINATE_COPY_RELOCS)
3207 /* For the non-shared case, discard space for relocs against
3208 symbols which turn out to need copy relocs or are not
3209 dynamic. */
3211 if ((h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0
3212 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
3213 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
3215 /* Make sure this symbol is output as a dynamic symbol.
3216 Undefined weak syms won't yet be marked as dynamic. */
3217 if (h->dynindx == -1
3218 && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
3220 if (! bfd_elf64_link_record_dynamic_symbol (info, h))
3221 return FALSE;
3224 /* If that succeeded, we know we'll be keeping all the
3225 relocs. */
3226 if (h->dynindx != -1)
3227 goto keep;
3230 eh->dyn_relocs = NULL;
3232 keep: ;
3235 /* Finally, allocate space. */
3236 for (p = eh->dyn_relocs; p != NULL; p = p->next)
3238 asection *sreloc = elf_section_data (p->sec)->sreloc;
3239 sreloc->_raw_size += p->count * sizeof (Elf32_External_Rela);
3242 return TRUE;
3245 /* Find any dynamic relocs that apply to read-only sections. */
3247 static bfd_boolean
3248 readonly_dynrelocs (struct elf_link_hash_entry *h, void *info)
3250 struct ppc_elf_dyn_relocs *p;
3252 if (h->root.type == bfd_link_hash_indirect)
3253 return TRUE;
3255 if (h->root.type == bfd_link_hash_warning)
3256 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3258 for (p = ppc_elf_hash_entry (h)->dyn_relocs; p != NULL; p = p->next)
3260 asection *s = p->sec->output_section;
3262 if (s != NULL
3263 && ((s->flags & (SEC_READONLY | SEC_ALLOC))
3264 == (SEC_READONLY | SEC_ALLOC)))
3266 ((struct bfd_link_info *) info)->flags |= DF_TEXTREL;
3268 /* Not an error, just cut short the traversal. */
3269 return FALSE;
3272 return TRUE;
3275 /* Set the sizes of the dynamic sections. */
3277 static bfd_boolean
3278 ppc_elf_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
3279 struct bfd_link_info *info)
3281 struct ppc_elf_link_hash_table *htab;
3282 asection *s;
3283 bfd_boolean relocs;
3284 bfd *ibfd;
3286 #ifdef DEBUG
3287 fprintf (stderr, "ppc_elf_size_dynamic_sections called\n");
3288 #endif
3290 htab = ppc_elf_hash_table (info);
3291 BFD_ASSERT (htab->elf.dynobj != NULL);
3293 if (elf_hash_table (info)->dynamic_sections_created)
3295 /* Set the contents of the .interp section to the interpreter. */
3296 if (info->executable)
3298 s = bfd_get_section_by_name (htab->elf.dynobj, ".interp");
3299 BFD_ASSERT (s != NULL);
3300 s->_raw_size = sizeof ELF_DYNAMIC_INTERPRETER;
3301 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
3305 if (htab->tlsld_got.refcount > 0)
3307 htab->tlsld_got.offset = htab->got->_raw_size;
3308 htab->got->_raw_size += 8;
3309 if (info->shared)
3310 htab->relgot->_raw_size += sizeof (Elf32_External_Rela);
3312 else
3313 htab->tlsld_got.offset = (bfd_vma) -1;
3315 /* Set up .got offsets for local syms, and space for local dynamic
3316 relocs. */
3317 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
3319 bfd_signed_vma *local_got;
3320 bfd_signed_vma *end_local_got;
3321 char *lgot_masks;
3322 bfd_size_type locsymcount;
3323 Elf_Internal_Shdr *symtab_hdr;
3324 asection *srel;
3326 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
3327 continue;
3329 for (s = ibfd->sections; s != NULL; s = s->next)
3331 struct ppc_elf_dyn_relocs *p;
3333 for (p = ((struct ppc_elf_dyn_relocs *)
3334 elf_section_data (s)->local_dynrel);
3335 p != NULL;
3336 p = p->next)
3338 if (!bfd_is_abs_section (p->sec)
3339 && bfd_is_abs_section (p->sec->output_section))
3341 /* Input section has been discarded, either because
3342 it is a copy of a linkonce section or due to
3343 linker script /DISCARD/, so we'll be discarding
3344 the relocs too. */
3346 else if (p->count != 0)
3348 elf_section_data (p->sec)->sreloc->_raw_size
3349 += p->count * sizeof (Elf32_External_Rela);
3350 if ((p->sec->output_section->flags
3351 & (SEC_READONLY | SEC_ALLOC))
3352 == (SEC_READONLY | SEC_ALLOC))
3353 info->flags |= DF_TEXTREL;
3358 local_got = elf_local_got_refcounts (ibfd);
3359 if (!local_got)
3360 continue;
3362 symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
3363 locsymcount = symtab_hdr->sh_info;
3364 end_local_got = local_got + locsymcount;
3365 lgot_masks = (char *) end_local_got;
3366 s = htab->got;
3367 srel = htab->relgot;
3368 for (; local_got < end_local_got; ++local_got, ++lgot_masks)
3369 if (*local_got > 0)
3371 if (*lgot_masks == (TLS_TLS | TLS_LD))
3373 /* If just an LD reloc, we'll just use
3374 htab->tlsld_got.offset. */
3375 if (htab->tlsld_got.offset == (bfd_vma) -1)
3377 htab->tlsld_got.offset = s->_raw_size;
3378 s->_raw_size += 8;
3379 if (info->shared)
3380 srel->_raw_size += sizeof (Elf32_External_Rela);
3382 *local_got = (bfd_vma) -1;
3384 else
3386 *local_got = s->_raw_size;
3387 if ((*lgot_masks & TLS_TLS) != 0)
3389 if ((*lgot_masks & TLS_GD) != 0)
3390 s->_raw_size += 8;
3391 if ((*lgot_masks & (TLS_TPREL | TLS_TPRELGD)) != 0)
3392 s->_raw_size += 4;
3393 if ((*lgot_masks & TLS_DTPREL) != 0)
3394 s->_raw_size += 4;
3396 else
3397 s->_raw_size += 4;
3398 if (info->shared)
3399 srel->_raw_size += ((s->_raw_size - *local_got) / 4
3400 * sizeof (Elf32_External_Rela));
3403 else
3404 *local_got = (bfd_vma) -1;
3407 /* Allocate space for global sym dynamic relocs. */
3408 elf_link_hash_traverse (elf_hash_table (info), allocate_dynrelocs, info);
3410 /* We've now determined the sizes of the various dynamic sections.
3411 Allocate memory for them. */
3412 relocs = FALSE;
3413 for (s = htab->elf.dynobj->sections; s != NULL; s = s->next)
3415 if ((s->flags & SEC_LINKER_CREATED) == 0)
3416 continue;
3418 if (s == htab->plt
3419 || s == htab->got
3420 || (htab->sdata != NULL && s == htab->sdata->section)
3421 || (htab->sdata2 != NULL && s == htab->sdata2->section))
3423 /* Strip this section if we don't need it; see the
3424 comment below. */
3426 else if (strncmp (bfd_get_section_name (dynobj, s), ".rela", 5) == 0)
3428 if (s->_raw_size == 0)
3430 /* If we don't need this section, strip it from the
3431 output file. This is mostly to handle .rela.bss and
3432 .rela.plt. We must create both sections in
3433 create_dynamic_sections, because they must be created
3434 before the linker maps input sections to output
3435 sections. The linker does that before
3436 adjust_dynamic_symbol is called, and it is that
3437 function which decides whether anything needs to go
3438 into these sections. */
3440 else
3442 /* Remember whether there are any relocation sections. */
3443 relocs = TRUE;
3445 /* We use the reloc_count field as a counter if we need
3446 to copy relocs into the output file. */
3447 s->reloc_count = 0;
3450 else
3452 /* It's not one of our sections, so don't allocate space. */
3453 continue;
3456 if (s->_raw_size == 0)
3458 _bfd_strip_section_from_output (info, s);
3459 continue;
3462 /* Allocate memory for the section contents. */
3463 s->contents = bfd_zalloc (htab->elf.dynobj, s->_raw_size);
3464 if (s->contents == NULL)
3465 return FALSE;
3468 if (htab->elf.dynamic_sections_created)
3470 /* Add some entries to the .dynamic section. We fill in the
3471 values later, in ppc_elf_finish_dynamic_sections, but we
3472 must add the entries now so that we get the correct size for
3473 the .dynamic section. The DT_DEBUG entry is filled in by the
3474 dynamic linker and used by the debugger. */
3475 #define add_dynamic_entry(TAG, VAL) \
3476 bfd_elf32_add_dynamic_entry (info, (TAG), (VAL))
3478 if (info->executable)
3480 if (!add_dynamic_entry (DT_DEBUG, 0))
3481 return FALSE;
3484 if (htab->plt != NULL && htab->plt->_raw_size != 0)
3486 if (!add_dynamic_entry (DT_PLTGOT, 0)
3487 || !add_dynamic_entry (DT_PLTRELSZ, 0)
3488 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
3489 || !add_dynamic_entry (DT_JMPREL, 0))
3490 return FALSE;
3493 if (relocs)
3495 if (!add_dynamic_entry (DT_RELA, 0)
3496 || !add_dynamic_entry (DT_RELASZ, 0)
3497 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf32_External_Rela)))
3498 return FALSE;
3501 /* If any dynamic relocs apply to a read-only section, then we
3502 need a DT_TEXTREL entry. */
3503 if ((info->flags & DF_TEXTREL) == 0)
3504 elf_link_hash_traverse (elf_hash_table (info), readonly_dynrelocs,
3505 info);
3507 if ((info->flags & DF_TEXTREL) != 0)
3509 if (!add_dynamic_entry (DT_TEXTREL, 0))
3510 return FALSE;
3513 #undef add_dynamic_entry
3515 return TRUE;
3518 static bfd_boolean
3519 update_local_sym_info (bfd *abfd,
3520 Elf_Internal_Shdr *symtab_hdr,
3521 unsigned long r_symndx,
3522 int tls_type)
3524 bfd_signed_vma *local_got_refcounts = elf_local_got_refcounts (abfd);
3525 char *local_got_tls_masks;
3527 if (local_got_refcounts == NULL)
3529 bfd_size_type size = symtab_hdr->sh_info;
3531 size *= sizeof (*local_got_refcounts) + sizeof (*local_got_tls_masks);
3532 local_got_refcounts = bfd_zalloc (abfd, size);
3533 if (local_got_refcounts == NULL)
3534 return FALSE;
3535 elf_local_got_refcounts (abfd) = local_got_refcounts;
3538 local_got_refcounts[r_symndx] += 1;
3539 local_got_tls_masks = (char *) (local_got_refcounts + symtab_hdr->sh_info);
3540 local_got_tls_masks[r_symndx] |= tls_type;
3541 return TRUE;
3544 static void
3545 bad_shared_reloc (bfd *abfd, enum elf_ppc_reloc_type r_type)
3547 (*_bfd_error_handler)
3548 (_("%s: relocation %s cannot be used when making a shared object"),
3549 bfd_archive_filename (abfd),
3550 ppc_elf_howto_table[r_type]->name);
3551 bfd_set_error (bfd_error_bad_value);
3554 /* Look through the relocs for a section during the first phase, and
3555 allocate space in the global offset table or procedure linkage
3556 table. */
3558 static bfd_boolean
3559 ppc_elf_check_relocs (bfd *abfd,
3560 struct bfd_link_info *info,
3561 asection *sec,
3562 const Elf_Internal_Rela *relocs)
3564 struct ppc_elf_link_hash_table *htab;
3565 Elf_Internal_Shdr *symtab_hdr;
3566 struct elf_link_hash_entry **sym_hashes;
3567 const Elf_Internal_Rela *rel;
3568 const Elf_Internal_Rela *rel_end;
3569 asection *sreloc;
3571 if (info->relocatable)
3572 return TRUE;
3574 #ifdef DEBUG
3575 fprintf (stderr, "ppc_elf_check_relocs called for section %s in %s\n",
3576 bfd_get_section_name (abfd, sec),
3577 bfd_archive_filename (abfd));
3578 #endif
3580 /* Initialize howto table if not already done. */
3581 if (!ppc_elf_howto_table[R_PPC_ADDR32])
3582 ppc_elf_howto_init ();
3584 /* Create the linker generated sections all the time so that the
3585 special symbols are created. */
3586 htab = ppc_elf_hash_table (info);
3587 if (htab->sdata == NULL)
3589 htab->sdata = ppc_elf_create_linker_section (abfd, info,
3590 LINKER_SECTION_SDATA);
3591 if (htab->sdata == NULL)
3592 return FALSE;
3595 if (htab->sdata2 == NULL)
3597 htab->sdata2 = ppc_elf_create_linker_section (abfd, info,
3598 LINKER_SECTION_SDATA2);
3599 if (htab->sdata2 == NULL)
3600 return FALSE;
3603 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
3604 sym_hashes = elf_sym_hashes (abfd);
3605 sreloc = NULL;
3607 rel_end = relocs + sec->reloc_count;
3608 for (rel = relocs; rel < rel_end; rel++)
3610 unsigned long r_symndx;
3611 enum elf_ppc_reloc_type r_type;
3612 struct elf_link_hash_entry *h;
3613 int tls_type = 0;
3615 r_symndx = ELF32_R_SYM (rel->r_info);
3616 if (r_symndx < symtab_hdr->sh_info)
3617 h = NULL;
3618 else
3619 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
3621 /* If a relocation refers to _GLOBAL_OFFSET_TABLE_, create the .got.
3622 This shows up in particular in an R_PPC_ADDR32 in the eabi
3623 startup code. */
3624 if (h && strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
3626 if (htab->got == NULL)
3628 if (htab->elf.dynobj == NULL)
3629 htab->elf.dynobj = abfd;
3630 if (!ppc_elf_create_got (htab->elf.dynobj, info))
3631 return FALSE;
3635 r_type = ELF32_R_TYPE (rel->r_info);
3636 switch (r_type)
3638 case R_PPC_GOT_TLSLD16:
3639 case R_PPC_GOT_TLSLD16_LO:
3640 case R_PPC_GOT_TLSLD16_HI:
3641 case R_PPC_GOT_TLSLD16_HA:
3642 htab->tlsld_got.refcount += 1;
3643 tls_type = TLS_TLS | TLS_LD;
3644 goto dogottls;
3646 case R_PPC_GOT_TLSGD16:
3647 case R_PPC_GOT_TLSGD16_LO:
3648 case R_PPC_GOT_TLSGD16_HI:
3649 case R_PPC_GOT_TLSGD16_HA:
3650 tls_type = TLS_TLS | TLS_GD;
3651 goto dogottls;
3653 case R_PPC_GOT_TPREL16:
3654 case R_PPC_GOT_TPREL16_LO:
3655 case R_PPC_GOT_TPREL16_HI:
3656 case R_PPC_GOT_TPREL16_HA:
3657 if (info->shared)
3658 info->flags |= DF_STATIC_TLS;
3659 tls_type = TLS_TLS | TLS_TPREL;
3660 goto dogottls;
3662 case R_PPC_GOT_DTPREL16:
3663 case R_PPC_GOT_DTPREL16_LO:
3664 case R_PPC_GOT_DTPREL16_HI:
3665 case R_PPC_GOT_DTPREL16_HA:
3666 tls_type = TLS_TLS | TLS_DTPREL;
3667 dogottls:
3668 sec->has_tls_reloc = 1;
3669 /* Fall thru */
3671 /* GOT16 relocations */
3672 case R_PPC_GOT16:
3673 case R_PPC_GOT16_LO:
3674 case R_PPC_GOT16_HI:
3675 case R_PPC_GOT16_HA:
3676 /* This symbol requires a global offset table entry. */
3677 if (htab->got == NULL)
3679 if (htab->elf.dynobj == NULL)
3680 htab->elf.dynobj = abfd;
3681 if (!ppc_elf_create_got (htab->elf.dynobj, info))
3682 return FALSE;
3684 if (h != NULL)
3686 h->got.refcount += 1;
3687 ppc_elf_hash_entry (h)->tls_mask |= tls_type;
3689 else
3690 /* This is a global offset table entry for a local symbol. */
3691 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx, tls_type))
3692 return FALSE;
3693 break;
3695 /* Indirect .sdata relocation. */
3696 case R_PPC_EMB_SDAI16:
3697 if (info->shared)
3699 bad_shared_reloc (abfd, r_type);
3700 return FALSE;
3702 if (!elf_create_pointer_linker_section (abfd, info,
3703 htab->sdata, h, rel))
3704 return FALSE;
3705 break;
3707 /* Indirect .sdata2 relocation. */
3708 case R_PPC_EMB_SDA2I16:
3709 if (info->shared)
3711 bad_shared_reloc (abfd, r_type);
3712 return FALSE;
3714 if (!elf_create_pointer_linker_section (abfd, info,
3715 htab->sdata2, h, rel))
3716 return FALSE;
3717 break;
3719 case R_PPC_SDAREL16:
3720 case R_PPC_EMB_SDA2REL:
3721 case R_PPC_EMB_SDA21:
3722 case R_PPC_EMB_RELSDA:
3723 case R_PPC_EMB_NADDR32:
3724 case R_PPC_EMB_NADDR16:
3725 case R_PPC_EMB_NADDR16_LO:
3726 case R_PPC_EMB_NADDR16_HI:
3727 case R_PPC_EMB_NADDR16_HA:
3728 if (info->shared)
3730 bad_shared_reloc (abfd, r_type);
3731 return FALSE;
3733 break;
3735 case R_PPC_PLT32:
3736 case R_PPC_PLTREL24:
3737 case R_PPC_PLTREL32:
3738 case R_PPC_PLT16_LO:
3739 case R_PPC_PLT16_HI:
3740 case R_PPC_PLT16_HA:
3741 #ifdef DEBUG
3742 fprintf (stderr, "Reloc requires a PLT entry\n");
3743 #endif
3744 /* This symbol requires a procedure linkage table entry. We
3745 actually build the entry in finish_dynamic_symbol,
3746 because this might be a case of linking PIC code without
3747 linking in any dynamic objects, in which case we don't
3748 need to generate a procedure linkage table after all. */
3750 if (h == NULL)
3752 /* It does not make sense to have a procedure linkage
3753 table entry for a local symbol. */
3754 (*_bfd_error_handler) (_("%s(%s+0x%lx): %s reloc against "
3755 "local symbol"),
3756 bfd_archive_filename (abfd),
3757 sec->name,
3758 (long) rel->r_offset,
3759 ppc_elf_howto_table[r_type]->name);
3760 bfd_set_error (bfd_error_bad_value);
3761 return FALSE;
3764 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
3765 h->plt.refcount++;
3766 break;
3768 /* The following relocations don't need to propagate the
3769 relocation if linking a shared object since they are
3770 section relative. */
3771 case R_PPC_SECTOFF:
3772 case R_PPC_SECTOFF_LO:
3773 case R_PPC_SECTOFF_HI:
3774 case R_PPC_SECTOFF_HA:
3775 case R_PPC_DTPREL16:
3776 case R_PPC_DTPREL16_LO:
3777 case R_PPC_DTPREL16_HI:
3778 case R_PPC_DTPREL16_HA:
3779 case R_PPC_TOC16:
3780 break;
3782 /* This are just markers. */
3783 case R_PPC_TLS:
3784 case R_PPC_EMB_MRKREF:
3785 case R_PPC_NONE:
3786 case R_PPC_max:
3787 break;
3789 /* These should only appear in dynamic objects. */
3790 case R_PPC_COPY:
3791 case R_PPC_GLOB_DAT:
3792 case R_PPC_JMP_SLOT:
3793 case R_PPC_RELATIVE:
3794 break;
3796 /* These aren't handled yet. We'll report an error later. */
3797 case R_PPC_ADDR30:
3798 case R_PPC_EMB_RELSEC16:
3799 case R_PPC_EMB_RELST_LO:
3800 case R_PPC_EMB_RELST_HI:
3801 case R_PPC_EMB_RELST_HA:
3802 case R_PPC_EMB_BIT_FLD:
3803 break;
3805 /* This refers only to functions defined in the shared library. */
3806 case R_PPC_LOCAL24PC:
3807 break;
3809 /* This relocation describes the C++ object vtable hierarchy.
3810 Reconstruct it for later use during GC. */
3811 case R_PPC_GNU_VTINHERIT:
3812 if (!_bfd_elf32_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
3813 return FALSE;
3814 break;
3816 /* This relocation describes which C++ vtable entries are actually
3817 used. Record for later use during GC. */
3818 case R_PPC_GNU_VTENTRY:
3819 if (!_bfd_elf32_gc_record_vtentry (abfd, sec, h, rel->r_addend))
3820 return FALSE;
3821 break;
3823 /* We shouldn't really be seeing these. */
3824 case R_PPC_TPREL32:
3825 if (info->shared)
3826 info->flags |= DF_STATIC_TLS;
3827 goto dodyn;
3829 /* Nor these. */
3830 case R_PPC_DTPMOD32:
3831 case R_PPC_DTPREL32:
3832 goto dodyn;
3834 case R_PPC_TPREL16:
3835 case R_PPC_TPREL16_LO:
3836 case R_PPC_TPREL16_HI:
3837 case R_PPC_TPREL16_HA:
3838 if (info->shared)
3839 info->flags |= DF_STATIC_TLS;
3840 goto dodyn;
3842 /* When creating a shared object, we must copy these
3843 relocs into the output file. We create a reloc
3844 section in dynobj and make room for the reloc. */
3845 case R_PPC_REL24:
3846 case R_PPC_REL14:
3847 case R_PPC_REL14_BRTAKEN:
3848 case R_PPC_REL14_BRNTAKEN:
3849 case R_PPC_REL32:
3850 if (h == NULL
3851 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
3852 break;
3853 /* fall through */
3855 case R_PPC_ADDR32:
3856 case R_PPC_ADDR24:
3857 case R_PPC_ADDR16:
3858 case R_PPC_ADDR16_LO:
3859 case R_PPC_ADDR16_HI:
3860 case R_PPC_ADDR16_HA:
3861 case R_PPC_ADDR14:
3862 case R_PPC_ADDR14_BRTAKEN:
3863 case R_PPC_ADDR14_BRNTAKEN:
3864 case R_PPC_UADDR32:
3865 case R_PPC_UADDR16:
3866 if (h != NULL && !info->shared)
3868 /* We may need a plt entry if the symbol turns out to be
3869 a function defined in a dynamic object. */
3870 h->plt.refcount++;
3872 /* We may need a copy reloc too. */
3873 h->elf_link_hash_flags |= ELF_LINK_NON_GOT_REF;
3876 dodyn:
3877 /* If we are creating a shared library, and this is a reloc
3878 against a global symbol, or a non PC relative reloc
3879 against a local symbol, then we need to copy the reloc
3880 into the shared library. However, if we are linking with
3881 -Bsymbolic, we do not need to copy a reloc against a
3882 global symbol which is defined in an object we are
3883 including in the link (i.e., DEF_REGULAR is set). At
3884 this point we have not seen all the input files, so it is
3885 possible that DEF_REGULAR is not set now but will be set
3886 later (it is never cleared). In case of a weak definition,
3887 DEF_REGULAR may be cleared later by a strong definition in
3888 a shared library. We account for that possibility below by
3889 storing information in the dyn_relocs field of the hash
3890 table entry. A similar situation occurs when creating
3891 shared libraries and symbol visibility changes render the
3892 symbol local.
3894 If on the other hand, we are creating an executable, we
3895 may need to keep relocations for symbols satisfied by a
3896 dynamic library if we manage to avoid copy relocs for the
3897 symbol. */
3898 if ((info->shared
3899 && (MUST_BE_DYN_RELOC (r_type)
3900 || (h != NULL
3901 && (! info->symbolic
3902 || h->root.type == bfd_link_hash_defweak
3903 || (h->elf_link_hash_flags
3904 & ELF_LINK_HASH_DEF_REGULAR) == 0))))
3905 || (ELIMINATE_COPY_RELOCS
3906 && !info->shared
3907 && (sec->flags & SEC_ALLOC) != 0
3908 && h != NULL
3909 && (h->root.type == bfd_link_hash_defweak
3910 || (h->elf_link_hash_flags
3911 & ELF_LINK_HASH_DEF_REGULAR) == 0)))
3913 struct ppc_elf_dyn_relocs *p;
3914 struct ppc_elf_dyn_relocs **head;
3916 #ifdef DEBUG
3917 fprintf (stderr,
3918 "ppc_elf_check_relocs needs to "
3919 "create relocation for %s\n",
3920 (h && h->root.root.string
3921 ? h->root.root.string : "<unknown>"));
3922 #endif
3923 if (sreloc == NULL)
3925 const char *name;
3927 name = (bfd_elf_string_from_elf_section
3928 (abfd,
3929 elf_elfheader (abfd)->e_shstrndx,
3930 elf_section_data (sec)->rel_hdr.sh_name));
3931 if (name == NULL)
3932 return FALSE;
3934 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
3935 && strcmp (bfd_get_section_name (abfd, sec),
3936 name + 5) == 0);
3938 sreloc = bfd_get_section_by_name (htab->elf.dynobj, name);
3939 if (sreloc == NULL)
3941 flagword flags;
3943 sreloc = bfd_make_section (htab->elf.dynobj, name);
3944 flags = (SEC_HAS_CONTENTS | SEC_READONLY
3945 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3946 if ((sec->flags & SEC_ALLOC) != 0)
3947 flags |= SEC_ALLOC | SEC_LOAD;
3948 if (sreloc == NULL
3949 || ! bfd_set_section_flags (htab->elf.dynobj,
3950 sreloc, flags)
3951 || ! bfd_set_section_alignment (htab->elf.dynobj,
3952 sreloc, 2))
3953 return FALSE;
3955 elf_section_data (sec)->sreloc = sreloc;
3958 /* If this is a global symbol, we count the number of
3959 relocations we need for this symbol. */
3960 if (h != NULL)
3962 head = &ppc_elf_hash_entry (h)->dyn_relocs;
3964 else
3966 /* Track dynamic relocs needed for local syms too.
3967 We really need local syms available to do this
3968 easily. Oh well. */
3970 asection *s;
3971 s = bfd_section_from_r_symndx (abfd, &htab->sym_sec,
3972 sec, r_symndx);
3973 if (s == NULL)
3974 return FALSE;
3976 head = ((struct ppc_elf_dyn_relocs **)
3977 &elf_section_data (s)->local_dynrel);
3980 p = *head;
3981 if (p == NULL || p->sec != sec)
3983 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
3984 if (p == NULL)
3985 return FALSE;
3986 p->next = *head;
3987 *head = p;
3988 p->sec = sec;
3989 p->count = 0;
3990 p->pc_count = 0;
3993 p->count += 1;
3994 if (!MUST_BE_DYN_RELOC (r_type))
3995 p->pc_count += 1;
3998 break;
4002 return TRUE;
4005 /* Return the section that should be marked against GC for a given
4006 relocation. */
4008 static asection *
4009 ppc_elf_gc_mark_hook (asection *sec,
4010 struct bfd_link_info *info ATTRIBUTE_UNUSED,
4011 Elf_Internal_Rela *rel,
4012 struct elf_link_hash_entry *h,
4013 Elf_Internal_Sym *sym)
4015 if (h != NULL)
4017 switch (ELF32_R_TYPE (rel->r_info))
4019 case R_PPC_GNU_VTINHERIT:
4020 case R_PPC_GNU_VTENTRY:
4021 break;
4023 default:
4024 switch (h->root.type)
4026 case bfd_link_hash_defined:
4027 case bfd_link_hash_defweak:
4028 return h->root.u.def.section;
4030 case bfd_link_hash_common:
4031 return h->root.u.c.p->section;
4033 default:
4034 break;
4038 else
4039 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
4041 return NULL;
4044 /* Update the got, plt and dynamic reloc reference counts for the
4045 section being removed. */
4047 static bfd_boolean
4048 ppc_elf_gc_sweep_hook (bfd *abfd,
4049 struct bfd_link_info *info,
4050 asection *sec,
4051 const Elf_Internal_Rela *relocs)
4053 struct ppc_elf_link_hash_table *htab;
4054 Elf_Internal_Shdr *symtab_hdr;
4055 struct elf_link_hash_entry **sym_hashes;
4056 bfd_signed_vma *local_got_refcounts;
4057 const Elf_Internal_Rela *rel, *relend;
4059 elf_section_data (sec)->local_dynrel = NULL;
4061 htab = ppc_elf_hash_table (info);
4062 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
4063 sym_hashes = elf_sym_hashes (abfd);
4064 local_got_refcounts = elf_local_got_refcounts (abfd);
4066 relend = relocs + sec->reloc_count;
4067 for (rel = relocs; rel < relend; rel++)
4069 unsigned long r_symndx;
4070 enum elf_ppc_reloc_type r_type;
4071 struct elf_link_hash_entry *h = NULL;
4073 r_symndx = ELF32_R_SYM (rel->r_info);
4074 if (r_symndx >= symtab_hdr->sh_info)
4076 struct ppc_elf_dyn_relocs **pp, *p;
4077 struct ppc_elf_link_hash_entry *eh;
4079 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
4080 eh = (struct ppc_elf_link_hash_entry *) h;
4082 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
4083 if (p->sec == sec)
4085 /* Everything must go for SEC. */
4086 *pp = p->next;
4087 break;
4091 r_type = ELF32_R_TYPE (rel->r_info);
4092 switch (r_type)
4094 case R_PPC_GOT_TLSLD16:
4095 case R_PPC_GOT_TLSLD16_LO:
4096 case R_PPC_GOT_TLSLD16_HI:
4097 case R_PPC_GOT_TLSLD16_HA:
4098 htab->tlsld_got.refcount -= 1;
4099 /* Fall thru */
4101 case R_PPC_GOT_TLSGD16:
4102 case R_PPC_GOT_TLSGD16_LO:
4103 case R_PPC_GOT_TLSGD16_HI:
4104 case R_PPC_GOT_TLSGD16_HA:
4105 case R_PPC_GOT_TPREL16:
4106 case R_PPC_GOT_TPREL16_LO:
4107 case R_PPC_GOT_TPREL16_HI:
4108 case R_PPC_GOT_TPREL16_HA:
4109 case R_PPC_GOT_DTPREL16:
4110 case R_PPC_GOT_DTPREL16_LO:
4111 case R_PPC_GOT_DTPREL16_HI:
4112 case R_PPC_GOT_DTPREL16_HA:
4113 case R_PPC_GOT16:
4114 case R_PPC_GOT16_LO:
4115 case R_PPC_GOT16_HI:
4116 case R_PPC_GOT16_HA:
4117 if (h != NULL)
4119 if (h->got.refcount > 0)
4120 h->got.refcount--;
4122 else if (local_got_refcounts != NULL)
4124 if (local_got_refcounts[r_symndx] > 0)
4125 local_got_refcounts[r_symndx]--;
4127 break;
4129 case R_PPC_REL24:
4130 case R_PPC_REL14:
4131 case R_PPC_REL14_BRTAKEN:
4132 case R_PPC_REL14_BRNTAKEN:
4133 case R_PPC_REL32:
4134 if (h == NULL
4135 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
4136 break;
4137 /* Fall thru */
4139 case R_PPC_ADDR32:
4140 case R_PPC_ADDR24:
4141 case R_PPC_ADDR16:
4142 case R_PPC_ADDR16_LO:
4143 case R_PPC_ADDR16_HI:
4144 case R_PPC_ADDR16_HA:
4145 case R_PPC_ADDR14:
4146 case R_PPC_ADDR14_BRTAKEN:
4147 case R_PPC_ADDR14_BRNTAKEN:
4148 case R_PPC_UADDR32:
4149 case R_PPC_UADDR16:
4150 case R_PPC_PLT32:
4151 case R_PPC_PLTREL24:
4152 case R_PPC_PLT16_LO:
4153 case R_PPC_PLT16_HI:
4154 case R_PPC_PLT16_HA:
4155 if (h != NULL)
4157 if (h->plt.refcount > 0)
4158 h->plt.refcount--;
4160 break;
4162 default:
4163 break;
4166 return TRUE;
4169 /* Set htab->tls_get_addr and call the generic ELF tls_setup function. */
4171 asection *
4172 ppc_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
4174 struct ppc_elf_link_hash_table *htab;
4176 htab = ppc_elf_hash_table (info);
4177 htab->tls_get_addr = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
4178 FALSE, FALSE, TRUE);
4180 return _bfd_elf_tls_setup (obfd, info);
4183 /* Run through all the TLS relocs looking for optimization
4184 opportunities. */
4186 bfd_boolean
4187 ppc_elf_tls_optimize (bfd *obfd ATTRIBUTE_UNUSED,
4188 struct bfd_link_info *info)
4190 bfd *ibfd;
4191 asection *sec;
4192 struct ppc_elf_link_hash_table *htab;
4194 if (info->relocatable || info->shared)
4195 return TRUE;
4197 htab = ppc_elf_hash_table (info);
4198 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
4200 Elf_Internal_Sym *locsyms = NULL;
4201 Elf_Internal_Shdr *symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
4203 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
4204 if (sec->has_tls_reloc && !bfd_is_abs_section (sec->output_section))
4206 Elf_Internal_Rela *relstart, *rel, *relend;
4207 int expecting_tls_get_addr;
4209 /* Read the relocations. */
4210 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
4211 info->keep_memory);
4212 if (relstart == NULL)
4213 return FALSE;
4215 expecting_tls_get_addr = 0;
4216 relend = relstart + sec->reloc_count;
4217 for (rel = relstart; rel < relend; rel++)
4219 enum elf_ppc_reloc_type r_type;
4220 unsigned long r_symndx;
4221 struct elf_link_hash_entry *h = NULL;
4222 char *tls_mask;
4223 char tls_set, tls_clear;
4224 bfd_boolean is_local;
4226 r_symndx = ELF32_R_SYM (rel->r_info);
4227 if (r_symndx >= symtab_hdr->sh_info)
4229 struct elf_link_hash_entry **sym_hashes;
4231 sym_hashes = elf_sym_hashes (ibfd);
4232 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
4233 while (h->root.type == bfd_link_hash_indirect
4234 || h->root.type == bfd_link_hash_warning)
4235 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4238 is_local = FALSE;
4239 if (h == NULL
4240 || !(h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC))
4241 is_local = TRUE;
4243 r_type = ELF32_R_TYPE (rel->r_info);
4244 switch (r_type)
4246 case R_PPC_GOT_TLSLD16:
4247 case R_PPC_GOT_TLSLD16_LO:
4248 case R_PPC_GOT_TLSLD16_HI:
4249 case R_PPC_GOT_TLSLD16_HA:
4250 /* These relocs should never be against a symbol
4251 defined in a shared lib. Leave them alone if
4252 that turns out to be the case. */
4253 expecting_tls_get_addr = 0;
4254 htab->tlsld_got.refcount -= 1;
4255 if (!is_local)
4256 continue;
4258 /* LD -> LE */
4259 tls_set = 0;
4260 tls_clear = TLS_LD;
4261 expecting_tls_get_addr = 1;
4262 break;
4264 case R_PPC_GOT_TLSGD16:
4265 case R_PPC_GOT_TLSGD16_LO:
4266 case R_PPC_GOT_TLSGD16_HI:
4267 case R_PPC_GOT_TLSGD16_HA:
4268 if (is_local)
4269 /* GD -> LE */
4270 tls_set = 0;
4271 else
4272 /* GD -> IE */
4273 tls_set = TLS_TLS | TLS_TPRELGD;
4274 tls_clear = TLS_GD;
4275 expecting_tls_get_addr = 1;
4276 break;
4278 case R_PPC_GOT_TPREL16:
4279 case R_PPC_GOT_TPREL16_LO:
4280 case R_PPC_GOT_TPREL16_HI:
4281 case R_PPC_GOT_TPREL16_HA:
4282 expecting_tls_get_addr = 0;
4283 if (is_local)
4285 /* IE -> LE */
4286 tls_set = 0;
4287 tls_clear = TLS_TPREL;
4288 break;
4290 else
4291 continue;
4293 case R_PPC_REL14:
4294 case R_PPC_REL14_BRTAKEN:
4295 case R_PPC_REL14_BRNTAKEN:
4296 case R_PPC_REL24:
4297 if (expecting_tls_get_addr
4298 && h != NULL
4299 && h == htab->tls_get_addr)
4301 if (h->plt.refcount > 0)
4302 h->plt.refcount -= 1;
4304 expecting_tls_get_addr = 0;
4305 continue;
4307 default:
4308 expecting_tls_get_addr = 0;
4309 continue;
4312 if (h != NULL)
4314 if (tls_set == 0)
4316 /* We managed to get rid of a got entry. */
4317 if (h->got.refcount > 0)
4318 h->got.refcount -= 1;
4320 tls_mask = &ppc_elf_hash_entry (h)->tls_mask;
4322 else
4324 Elf_Internal_Sym *sym;
4325 bfd_signed_vma *lgot_refs;
4326 char *lgot_masks;
4328 if (locsyms == NULL)
4330 locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
4331 if (locsyms == NULL)
4332 locsyms = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
4333 symtab_hdr->sh_info,
4334 0, NULL, NULL, NULL);
4335 if (locsyms == NULL)
4337 if (elf_section_data (sec)->relocs != relstart)
4338 free (relstart);
4339 return FALSE;
4342 sym = locsyms + r_symndx;
4343 lgot_refs = elf_local_got_refcounts (ibfd);
4344 if (lgot_refs == NULL)
4345 abort ();
4346 if (tls_set == 0)
4348 /* We managed to get rid of a got entry. */
4349 if (lgot_refs[r_symndx] > 0)
4350 lgot_refs[r_symndx] -= 1;
4352 lgot_masks = (char *) (lgot_refs + symtab_hdr->sh_info);
4353 tls_mask = &lgot_masks[r_symndx];
4356 *tls_mask |= tls_set;
4357 *tls_mask &= ~tls_clear;
4360 if (elf_section_data (sec)->relocs != relstart)
4361 free (relstart);
4364 if (locsyms != NULL
4365 && (symtab_hdr->contents != (unsigned char *) locsyms))
4367 if (!info->keep_memory)
4368 free (locsyms);
4369 else
4370 symtab_hdr->contents = (unsigned char *) locsyms;
4373 return TRUE;
4376 /* Hook called by the linker routine which adds symbols from an object
4377 file. We use it to put .comm items in .sbss, and not .bss. */
4379 static bfd_boolean
4380 ppc_elf_add_symbol_hook (bfd *abfd,
4381 struct bfd_link_info *info,
4382 const Elf_Internal_Sym *sym,
4383 const char **namep ATTRIBUTE_UNUSED,
4384 flagword *flagsp ATTRIBUTE_UNUSED,
4385 asection **secp,
4386 bfd_vma *valp)
4388 if (sym->st_shndx == SHN_COMMON
4389 && !info->relocatable
4390 && sym->st_size <= elf_gp_size (abfd)
4391 && (info->hash->creator == abfd->xvec
4392 || info->hash->creator == abfd->xvec->alternative_target))
4394 /* Common symbols less than or equal to -G nn bytes are automatically
4395 put into .sbss. */
4396 struct ppc_elf_link_hash_table *htab;
4398 htab = ppc_elf_hash_table (info);
4399 if (htab->sbss == NULL)
4401 flagword flags = SEC_IS_COMMON;
4403 htab->sbss = bfd_make_section_anyway (abfd, ".sbss");
4404 if (htab->sbss == NULL
4405 || ! bfd_set_section_flags (abfd, htab->sbss, flags))
4406 return FALSE;
4409 *secp = htab->sbss;
4410 *valp = sym->st_size;
4413 return TRUE;
4416 /* Finish up dynamic symbol handling. We set the contents of various
4417 dynamic sections here. */
4419 static bfd_boolean
4420 ppc_elf_finish_dynamic_symbol (bfd *output_bfd,
4421 struct bfd_link_info *info,
4422 struct elf_link_hash_entry *h,
4423 Elf_Internal_Sym *sym)
4425 struct ppc_elf_link_hash_table *htab;
4427 #ifdef DEBUG
4428 fprintf (stderr, "ppc_elf_finish_dynamic_symbol called for %s",
4429 h->root.root.string);
4430 #endif
4432 htab = ppc_elf_hash_table (info);
4433 BFD_ASSERT (htab->elf.dynobj != NULL);
4435 if (h->plt.offset != (bfd_vma) -1)
4437 Elf_Internal_Rela rela;
4438 bfd_byte *loc;
4439 bfd_vma reloc_index;
4441 #ifdef DEBUG
4442 fprintf (stderr, ", plt_offset = %d", h->plt.offset);
4443 #endif
4445 /* This symbol has an entry in the procedure linkage table. Set
4446 it up. */
4448 BFD_ASSERT (h->dynindx != -1);
4449 BFD_ASSERT (htab->plt != NULL && htab->relplt != NULL);
4451 /* We don't need to fill in the .plt. The ppc dynamic linker
4452 will fill it in. */
4454 /* Fill in the entry in the .rela.plt section. */
4455 rela.r_offset = (htab->plt->output_section->vma
4456 + htab->plt->output_offset
4457 + h->plt.offset);
4458 rela.r_info = ELF32_R_INFO (h->dynindx, R_PPC_JMP_SLOT);
4459 rela.r_addend = 0;
4461 reloc_index = (h->plt.offset - PLT_INITIAL_ENTRY_SIZE) / PLT_SLOT_SIZE;
4462 if (reloc_index > PLT_NUM_SINGLE_ENTRIES)
4463 reloc_index -= (reloc_index - PLT_NUM_SINGLE_ENTRIES) / 2;
4464 loc = (htab->relplt->contents
4465 + reloc_index * sizeof (Elf32_External_Rela));
4466 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
4468 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
4470 /* Mark the symbol as undefined, rather than as defined in
4471 the .plt section. Leave the value alone. */
4472 sym->st_shndx = SHN_UNDEF;
4473 /* If the symbol is weak, we do need to clear the value.
4474 Otherwise, the PLT entry would provide a definition for
4475 the symbol even if the symbol wasn't defined anywhere,
4476 and so the symbol would never be NULL. */
4477 if ((h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR_NONWEAK)
4478 == 0)
4479 sym->st_value = 0;
4483 if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_COPY) != 0)
4485 asection *s;
4486 Elf_Internal_Rela rela;
4487 bfd_byte *loc;
4489 /* This symbols needs a copy reloc. Set it up. */
4491 #ifdef DEBUG
4492 fprintf (stderr, ", copy");
4493 #endif
4495 BFD_ASSERT (h->dynindx != -1);
4497 if (h->size <= elf_gp_size (htab->elf.dynobj))
4498 s = htab->relsbss;
4499 else
4500 s = htab->relbss;
4501 BFD_ASSERT (s != NULL);
4503 rela.r_offset = (h->root.u.def.value
4504 + h->root.u.def.section->output_section->vma
4505 + h->root.u.def.section->output_offset);
4506 rela.r_info = ELF32_R_INFO (h->dynindx, R_PPC_COPY);
4507 rela.r_addend = 0;
4508 loc = s->contents + s->reloc_count++ * sizeof (Elf32_External_Rela);
4509 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
4512 #ifdef DEBUG
4513 fprintf (stderr, "\n");
4514 #endif
4516 /* Mark some specially defined symbols as absolute. */
4517 if (strcmp (h->root.root.string, "_DYNAMIC") == 0
4518 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0
4519 || strcmp (h->root.root.string, "_PROCEDURE_LINKAGE_TABLE_") == 0)
4520 sym->st_shndx = SHN_ABS;
4522 return TRUE;
4525 /* Finish up the dynamic sections. */
4527 static bfd_boolean
4528 ppc_elf_finish_dynamic_sections (bfd *output_bfd,
4529 struct bfd_link_info *info)
4531 asection *sdyn;
4532 struct ppc_elf_link_hash_table *htab;
4534 #ifdef DEBUG
4535 fprintf (stderr, "ppc_elf_finish_dynamic_sections called\n");
4536 #endif
4538 htab = ppc_elf_hash_table (info);
4539 sdyn = bfd_get_section_by_name (htab->elf.dynobj, ".dynamic");
4541 if (htab->elf.dynamic_sections_created)
4543 Elf32_External_Dyn *dyncon, *dynconend;
4545 BFD_ASSERT (htab->plt != NULL && sdyn != NULL);
4547 dyncon = (Elf32_External_Dyn *) sdyn->contents;
4548 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->_raw_size);
4549 for (; dyncon < dynconend; dyncon++)
4551 Elf_Internal_Dyn dyn;
4552 asection *s;
4554 bfd_elf32_swap_dyn_in (htab->elf.dynobj, dyncon, &dyn);
4556 switch (dyn.d_tag)
4558 case DT_PLTGOT:
4559 s = htab->plt;
4560 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
4561 break;
4563 case DT_PLTRELSZ:
4564 dyn.d_un.d_val = htab->relplt->_raw_size;
4565 break;
4567 case DT_JMPREL:
4568 s = htab->relplt;
4569 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
4570 break;
4572 default:
4573 continue;
4576 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
4580 /* Add a blrl instruction at _GLOBAL_OFFSET_TABLE_-4 so that a function can
4581 easily find the address of the _GLOBAL_OFFSET_TABLE_. */
4582 if (htab->got)
4584 unsigned char *contents = htab->got->contents;
4585 bfd_put_32 (output_bfd, 0x4e800021 /* blrl */, contents);
4587 if (sdyn == NULL)
4588 bfd_put_32 (output_bfd, 0, contents + 4);
4589 else
4590 bfd_put_32 (output_bfd,
4591 sdyn->output_section->vma + sdyn->output_offset,
4592 contents + 4);
4594 elf_section_data (htab->got->output_section)->this_hdr.sh_entsize = 4;
4597 return TRUE;
4600 /* The RELOCATE_SECTION function is called by the ELF backend linker
4601 to handle the relocations for a section.
4603 The relocs are always passed as Rela structures; if the section
4604 actually uses Rel structures, the r_addend field will always be
4605 zero.
4607 This function is responsible for adjust the section contents as
4608 necessary, and (if using Rela relocs and generating a
4609 relocatable output file) adjusting the reloc addend as
4610 necessary.
4612 This function does not have to worry about setting the reloc
4613 address or the reloc symbol index.
4615 LOCAL_SYMS is a pointer to the swapped in local symbols.
4617 LOCAL_SECTIONS is an array giving the section in the input file
4618 corresponding to the st_shndx field of each local symbol.
4620 The global hash table entry for the global symbols can be found
4621 via elf_sym_hashes (input_bfd).
4623 When generating relocatable output, this function must handle
4624 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
4625 going to be the section symbol corresponding to the output
4626 section, which means that the addend must be adjusted
4627 accordingly. */
4629 static bfd_boolean
4630 ppc_elf_relocate_section (bfd *output_bfd,
4631 struct bfd_link_info *info,
4632 bfd *input_bfd,
4633 asection *input_section,
4634 bfd_byte *contents,
4635 Elf_Internal_Rela *relocs,
4636 Elf_Internal_Sym *local_syms,
4637 asection **local_sections)
4639 Elf_Internal_Shdr *symtab_hdr;
4640 struct elf_link_hash_entry **sym_hashes;
4641 struct ppc_elf_link_hash_table *htab;
4642 Elf_Internal_Rela *rel;
4643 Elf_Internal_Rela *relend;
4644 Elf_Internal_Rela outrel;
4645 bfd_byte *loc;
4646 asection *sreloc = NULL;
4647 bfd_vma *local_got_offsets;
4648 bfd_boolean ret = TRUE;
4650 #ifdef DEBUG
4651 fprintf (stderr, "ppc_elf_relocate_section called for %s section %s, "
4652 "%ld relocations%s\n",
4653 bfd_archive_filename (input_bfd),
4654 bfd_section_name(input_bfd, input_section),
4655 (long) input_section->reloc_count,
4656 (info->relocatable) ? " (relocatable)" : "");
4657 #endif
4659 if (info->relocatable)
4660 return TRUE;
4662 /* Initialize howto table if not already done. */
4663 if (!ppc_elf_howto_table[R_PPC_ADDR32])
4664 ppc_elf_howto_init ();
4666 htab = ppc_elf_hash_table (info);
4667 local_got_offsets = elf_local_got_offsets (input_bfd);
4668 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
4669 sym_hashes = elf_sym_hashes (input_bfd);
4670 rel = relocs;
4671 relend = relocs + input_section->reloc_count;
4672 for (; rel < relend; rel++)
4674 enum elf_ppc_reloc_type r_type;
4675 bfd_vma addend;
4676 bfd_reloc_status_type r;
4677 Elf_Internal_Sym *sym;
4678 asection *sec;
4679 struct elf_link_hash_entry *h;
4680 const char *sym_name;
4681 reloc_howto_type *howto;
4682 unsigned long r_symndx;
4683 bfd_vma relocation;
4684 bfd_vma branch_bit, insn, from;
4685 bfd_boolean unresolved_reloc;
4686 bfd_boolean warned;
4687 unsigned int tls_type, tls_mask, tls_gd;
4689 r_type = ELF32_R_TYPE (rel->r_info);
4690 sym = NULL;
4691 sec = NULL;
4692 h = NULL;
4693 unresolved_reloc = FALSE;
4694 warned = FALSE;
4695 r_symndx = ELF32_R_SYM (rel->r_info);
4697 if (r_symndx < symtab_hdr->sh_info)
4699 sym = local_syms + r_symndx;
4700 sec = local_sections[r_symndx];
4701 sym_name = bfd_elf_local_sym_name (input_bfd, sym);
4703 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
4705 else
4707 RELOC_FOR_GLOBAL_SYMBOL (h, sym_hashes, r_symndx,
4708 symtab_hdr, relocation, sec,
4709 unresolved_reloc, info,
4710 warned);
4712 sym_name = h->root.root.string;
4715 /* TLS optimizations. Replace instruction sequences and relocs
4716 based on information we collected in tls_optimize. We edit
4717 RELOCS so that --emit-relocs will output something sensible
4718 for the final instruction stream. */
4719 tls_mask = 0;
4720 tls_gd = 0;
4721 if (IS_PPC_TLS_RELOC (r_type))
4723 if (h != NULL)
4724 tls_mask = ((struct ppc_elf_link_hash_entry *) h)->tls_mask;
4725 else if (local_got_offsets != NULL)
4727 char *lgot_masks;
4728 lgot_masks = (char *) (local_got_offsets + symtab_hdr->sh_info);
4729 tls_mask = lgot_masks[r_symndx];
4733 /* Ensure reloc mapping code below stays sane. */
4734 if ((R_PPC_GOT_TLSLD16 & 3) != (R_PPC_GOT_TLSGD16 & 3)
4735 || (R_PPC_GOT_TLSLD16_LO & 3) != (R_PPC_GOT_TLSGD16_LO & 3)
4736 || (R_PPC_GOT_TLSLD16_HI & 3) != (R_PPC_GOT_TLSGD16_HI & 3)
4737 || (R_PPC_GOT_TLSLD16_HA & 3) != (R_PPC_GOT_TLSGD16_HA & 3)
4738 || (R_PPC_GOT_TLSLD16 & 3) != (R_PPC_GOT_TPREL16 & 3)
4739 || (R_PPC_GOT_TLSLD16_LO & 3) != (R_PPC_GOT_TPREL16_LO & 3)
4740 || (R_PPC_GOT_TLSLD16_HI & 3) != (R_PPC_GOT_TPREL16_HI & 3)
4741 || (R_PPC_GOT_TLSLD16_HA & 3) != (R_PPC_GOT_TPREL16_HA & 3))
4742 abort ();
4743 switch (r_type)
4745 default:
4746 break;
4748 case R_PPC_GOT_TPREL16:
4749 case R_PPC_GOT_TPREL16_LO:
4750 if (tls_mask != 0
4751 && (tls_mask & TLS_TPREL) == 0)
4753 bfd_vma insn;
4754 insn = bfd_get_32 (output_bfd, contents + rel->r_offset - 2);
4755 insn &= 31 << 21;
4756 insn |= 0x3c020000; /* addis 0,2,0 */
4757 bfd_put_32 (output_bfd, insn, contents + rel->r_offset - 2);
4758 r_type = R_PPC_TPREL16_HA;
4759 rel->r_info = ELF32_R_INFO (r_symndx, r_type);
4761 break;
4763 case R_PPC_TLS:
4764 if (tls_mask != 0
4765 && (tls_mask & TLS_TPREL) == 0)
4767 bfd_vma insn, rtra;
4768 insn = bfd_get_32 (output_bfd, contents + rel->r_offset);
4769 if ((insn & ((31 << 26) | (31 << 11)))
4770 == ((31 << 26) | (2 << 11)))
4771 rtra = insn & ((1 << 26) - (1 << 16));
4772 else if ((insn & ((31 << 26) | (31 << 16)))
4773 == ((31 << 26) | (2 << 16)))
4774 rtra = (insn & (31 << 21)) | ((insn & (31 << 11)) << 5);
4775 else
4776 abort ();
4777 if ((insn & ((1 << 11) - (1 << 1))) == 266 << 1)
4778 /* add -> addi. */
4779 insn = 14 << 26;
4780 else if ((insn & (31 << 1)) == 23 << 1
4781 && ((insn & (31 << 6)) < 14 << 6
4782 || ((insn & (31 << 6)) >= 16 << 6
4783 && (insn & (31 << 6)) < 24 << 6)))
4784 /* load and store indexed -> dform. */
4785 insn = (32 | ((insn >> 6) & 31)) << 26;
4786 else if ((insn & (31 << 1)) == 21 << 1
4787 && (insn & (0x1a << 6)) == 0)
4788 /* ldx, ldux, stdx, stdux -> ld, ldu, std, stdu. */
4789 insn = (((58 | ((insn >> 6) & 4)) << 26)
4790 | ((insn >> 6) & 1));
4791 else if ((insn & (31 << 1)) == 21 << 1
4792 && (insn & ((1 << 11) - (1 << 1))) == 341 << 1)
4793 /* lwax -> lwa. */
4794 insn = (58 << 26) | 2;
4795 else
4796 abort ();
4797 insn |= rtra;
4798 bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
4799 r_type = R_PPC_TPREL16_LO;
4800 rel->r_info = ELF32_R_INFO (r_symndx, r_type);
4801 /* Was PPC_TLS which sits on insn boundary, now
4802 PPC_TPREL16_LO which is at insn+2. */
4803 rel->r_offset += 2;
4805 break;
4807 case R_PPC_GOT_TLSGD16_HI:
4808 case R_PPC_GOT_TLSGD16_HA:
4809 tls_gd = TLS_TPRELGD;
4810 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
4811 goto tls_gdld_hi;
4812 break;
4814 case R_PPC_GOT_TLSLD16_HI:
4815 case R_PPC_GOT_TLSLD16_HA:
4816 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
4818 tls_gdld_hi:
4819 if ((tls_mask & tls_gd) != 0)
4820 r_type = (((r_type - (R_PPC_GOT_TLSGD16 & 3)) & 3)
4821 + R_PPC_GOT_TPREL16);
4822 else
4824 bfd_put_32 (output_bfd, NOP, contents + rel->r_offset);
4825 rel->r_offset -= 2;
4826 r_type = R_PPC_NONE;
4828 rel->r_info = ELF32_R_INFO (r_symndx, r_type);
4830 break;
4832 case R_PPC_GOT_TLSGD16:
4833 case R_PPC_GOT_TLSGD16_LO:
4834 tls_gd = TLS_TPRELGD;
4835 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
4836 goto tls_get_addr_check;
4837 break;
4839 case R_PPC_GOT_TLSLD16:
4840 case R_PPC_GOT_TLSLD16_LO:
4841 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
4843 tls_get_addr_check:
4844 if (rel + 1 < relend)
4846 enum elf_ppc_reloc_type r_type2;
4847 unsigned long r_symndx2;
4848 struct elf_link_hash_entry *h2;
4849 bfd_vma insn1, insn2;
4850 bfd_vma offset;
4852 /* The next instruction should be a call to
4853 __tls_get_addr. Peek at the reloc to be sure. */
4854 r_type2 = ELF32_R_TYPE (rel[1].r_info);
4855 r_symndx2 = ELF32_R_SYM (rel[1].r_info);
4856 if (r_symndx2 < symtab_hdr->sh_info
4857 || (r_type2 != R_PPC_REL14
4858 && r_type2 != R_PPC_REL14_BRTAKEN
4859 && r_type2 != R_PPC_REL14_BRNTAKEN
4860 && r_type2 != R_PPC_REL24
4861 && r_type2 != R_PPC_PLTREL24))
4862 break;
4864 h2 = sym_hashes[r_symndx2 - symtab_hdr->sh_info];
4865 while (h2->root.type == bfd_link_hash_indirect
4866 || h2->root.type == bfd_link_hash_warning)
4867 h2 = (struct elf_link_hash_entry *) h2->root.u.i.link;
4868 if (h2 == NULL || h2 != htab->tls_get_addr)
4869 break;
4871 /* OK, it checks out. Replace the call. */
4872 offset = rel[1].r_offset;
4873 insn1 = bfd_get_32 (output_bfd,
4874 contents + rel->r_offset - 2);
4875 if ((tls_mask & tls_gd) != 0)
4877 /* IE */
4878 insn1 &= (1 << 26) - 1;
4879 insn1 |= 32 << 26; /* lwz */
4880 insn2 = 0x7c631214; /* add 3,3,2 */
4881 rel[1].r_info = ELF32_R_INFO (r_symndx2, R_PPC_NONE);
4882 r_type = (((r_type - (R_PPC_GOT_TLSGD16 & 3)) & 3)
4883 + R_PPC_GOT_TPREL16);
4884 rel->r_info = ELF32_R_INFO (r_symndx, r_type);
4886 else
4888 /* LE */
4889 insn1 = 0x3c620000; /* addis 3,2,0 */
4890 insn2 = 0x38630000; /* addi 3,3,0 */
4891 if (tls_gd == 0)
4893 /* Was an LD reloc. */
4894 r_symndx = 0;
4895 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
4896 rel[1].r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
4898 r_type = R_PPC_TPREL16_HA;
4899 rel->r_info = ELF32_R_INFO (r_symndx, r_type);
4900 rel[1].r_info = ELF32_R_INFO (r_symndx,
4901 R_PPC_TPREL16_LO);
4902 rel[1].r_offset += 2;
4904 bfd_put_32 (output_bfd, insn1, contents + rel->r_offset - 2);
4905 bfd_put_32 (output_bfd, insn2, contents + offset);
4906 if (tls_gd == 0)
4908 /* We changed the symbol on an LD reloc. Start over
4909 in order to get h, sym, sec etc. right. */
4910 rel--;
4911 continue;
4915 break;
4918 /* Handle other relocations that tweak non-addend part of insn. */
4919 branch_bit = 0;
4920 switch (r_type)
4922 default:
4923 break;
4925 /* Branch taken prediction relocations. */
4926 case R_PPC_ADDR14_BRTAKEN:
4927 case R_PPC_REL14_BRTAKEN:
4928 branch_bit = BRANCH_PREDICT_BIT;
4929 /* Fall thru */
4931 /* Branch not taken prediction relocations. */
4932 case R_PPC_ADDR14_BRNTAKEN:
4933 case R_PPC_REL14_BRNTAKEN:
4934 insn = bfd_get_32 (output_bfd, contents + rel->r_offset);
4935 insn &= ~BRANCH_PREDICT_BIT;
4936 insn |= branch_bit;
4938 from = (rel->r_offset
4939 + input_section->output_offset
4940 + input_section->output_section->vma);
4942 /* Invert 'y' bit if not the default. */
4943 if ((bfd_signed_vma) (relocation + rel->r_addend - from) < 0)
4944 insn ^= BRANCH_PREDICT_BIT;
4946 bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
4947 break;
4950 addend = rel->r_addend;
4951 tls_type = 0;
4952 howto = NULL;
4953 if (r_type < R_PPC_max)
4954 howto = ppc_elf_howto_table[r_type];
4955 switch (r_type)
4957 default:
4958 (*_bfd_error_handler)
4959 (_("%s: unknown relocation type %d for symbol %s"),
4960 bfd_archive_filename (input_bfd), (int) r_type, sym_name);
4962 bfd_set_error (bfd_error_bad_value);
4963 ret = FALSE;
4964 continue;
4966 case R_PPC_NONE:
4967 case R_PPC_TLS:
4968 case R_PPC_EMB_MRKREF:
4969 case R_PPC_GNU_VTINHERIT:
4970 case R_PPC_GNU_VTENTRY:
4971 continue;
4973 /* GOT16 relocations. Like an ADDR16 using the symbol's
4974 address in the GOT as relocation value instead of the
4975 symbol's value itself. Also, create a GOT entry for the
4976 symbol and put the symbol value there. */
4977 case R_PPC_GOT_TLSGD16:
4978 case R_PPC_GOT_TLSGD16_LO:
4979 case R_PPC_GOT_TLSGD16_HI:
4980 case R_PPC_GOT_TLSGD16_HA:
4981 tls_type = TLS_TLS | TLS_GD;
4982 goto dogot;
4984 case R_PPC_GOT_TLSLD16:
4985 case R_PPC_GOT_TLSLD16_LO:
4986 case R_PPC_GOT_TLSLD16_HI:
4987 case R_PPC_GOT_TLSLD16_HA:
4988 tls_type = TLS_TLS | TLS_LD;
4989 goto dogot;
4991 case R_PPC_GOT_TPREL16:
4992 case R_PPC_GOT_TPREL16_LO:
4993 case R_PPC_GOT_TPREL16_HI:
4994 case R_PPC_GOT_TPREL16_HA:
4995 tls_type = TLS_TLS | TLS_TPREL;
4996 goto dogot;
4998 case R_PPC_GOT_DTPREL16:
4999 case R_PPC_GOT_DTPREL16_LO:
5000 case R_PPC_GOT_DTPREL16_HI:
5001 case R_PPC_GOT_DTPREL16_HA:
5002 tls_type = TLS_TLS | TLS_DTPREL;
5003 goto dogot;
5005 case R_PPC_GOT16:
5006 case R_PPC_GOT16_LO:
5007 case R_PPC_GOT16_HI:
5008 case R_PPC_GOT16_HA:
5009 dogot:
5011 /* Relocation is to the entry for this symbol in the global
5012 offset table. */
5013 bfd_vma off;
5014 bfd_vma *offp;
5015 unsigned long indx;
5017 if (htab->got == NULL)
5018 abort ();
5020 indx = 0;
5021 if (tls_type == (TLS_TLS | TLS_LD)
5022 && (h == NULL
5023 || !(h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC)))
5024 offp = &htab->tlsld_got.offset;
5025 else if (h != NULL)
5027 bfd_boolean dyn;
5028 dyn = htab->elf.dynamic_sections_created;
5029 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
5030 || (info->shared
5031 && SYMBOL_REFERENCES_LOCAL (info, h)))
5032 /* This is actually a static link, or it is a
5033 -Bsymbolic link and the symbol is defined
5034 locally, or the symbol was forced to be local
5035 because of a version file. */
5037 else
5039 indx = h->dynindx;
5040 unresolved_reloc = FALSE;
5042 offp = &h->got.offset;
5044 else
5046 if (local_got_offsets == NULL)
5047 abort ();
5048 offp = &local_got_offsets[r_symndx];
5051 /* The offset must always be a multiple of 4. We use the
5052 least significant bit to record whether we have already
5053 processed this entry. */
5054 off = *offp;
5055 if ((off & 1) != 0)
5056 off &= ~1;
5057 else
5059 unsigned int tls_m = (tls_mask
5060 & (TLS_LD | TLS_GD | TLS_DTPREL
5061 | TLS_TPREL | TLS_TPRELGD));
5063 if (offp == &htab->tlsld_got.offset)
5064 tls_m = TLS_LD;
5065 else if (h == NULL
5066 || !(h->elf_link_hash_flags
5067 & ELF_LINK_HASH_DEF_DYNAMIC))
5068 tls_m &= ~TLS_LD;
5070 /* We might have multiple got entries for this sym.
5071 Initialize them all. */
5074 int tls_ty = 0;
5076 if ((tls_m & TLS_LD) != 0)
5078 tls_ty = TLS_TLS | TLS_LD;
5079 tls_m &= ~TLS_LD;
5081 else if ((tls_m & TLS_GD) != 0)
5083 tls_ty = TLS_TLS | TLS_GD;
5084 tls_m &= ~TLS_GD;
5086 else if ((tls_m & TLS_DTPREL) != 0)
5088 tls_ty = TLS_TLS | TLS_DTPREL;
5089 tls_m &= ~TLS_DTPREL;
5091 else if ((tls_m & (TLS_TPREL | TLS_TPRELGD)) != 0)
5093 tls_ty = TLS_TLS | TLS_TPREL;
5094 tls_m = 0;
5097 /* Generate relocs for the dynamic linker. */
5098 if ((info->shared || indx != 0)
5099 && (h == NULL
5100 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
5101 || h->root.type != bfd_link_hash_undefweak))
5103 outrel.r_offset = (htab->got->output_section->vma
5104 + htab->got->output_offset
5105 + off);
5106 outrel.r_addend = 0;
5107 if (tls_ty & (TLS_LD | TLS_GD))
5109 outrel.r_info = ELF32_R_INFO (indx, R_PPC_DTPMOD32);
5110 if (tls_ty == (TLS_TLS | TLS_GD))
5112 loc = htab->relgot->contents;
5113 loc += (htab->relgot->reloc_count++
5114 * sizeof (Elf32_External_Rela));
5115 bfd_elf32_swap_reloca_out (output_bfd,
5116 &outrel, loc);
5117 outrel.r_offset += 4;
5118 outrel.r_info
5119 = ELF32_R_INFO (indx, R_PPC_DTPREL32);
5122 else if (tls_ty == (TLS_TLS | TLS_DTPREL))
5123 outrel.r_info = ELF32_R_INFO (indx, R_PPC_DTPREL32);
5124 else if (tls_ty == (TLS_TLS | TLS_TPREL))
5125 outrel.r_info = ELF32_R_INFO (indx, R_PPC_TPREL32);
5126 else if (indx == 0)
5127 outrel.r_info = ELF32_R_INFO (indx, R_PPC_RELATIVE);
5128 else
5129 outrel.r_info = ELF32_R_INFO (indx, R_PPC_GLOB_DAT);
5130 if (indx == 0)
5132 outrel.r_addend += relocation;
5133 if (tls_ty & (TLS_GD | TLS_DTPREL | TLS_TPREL))
5134 outrel.r_addend -= htab->elf.tls_sec->vma;
5136 loc = htab->relgot->contents;
5137 loc += (htab->relgot->reloc_count++
5138 * sizeof (Elf32_External_Rela));
5139 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
5142 /* Init the .got section contents if we're not
5143 emitting a reloc. */
5144 else
5146 bfd_vma value = relocation;
5148 if (tls_ty == (TLS_TLS | TLS_LD))
5149 value = 1;
5150 else if (tls_ty != 0)
5152 value -= htab->elf.tls_sec->vma + DTP_OFFSET;
5153 if (tls_ty == (TLS_TLS | TLS_TPREL))
5154 value += DTP_OFFSET - TP_OFFSET;
5156 if (tls_ty == (TLS_TLS | TLS_GD))
5158 bfd_put_32 (output_bfd, value,
5159 htab->got->contents + off + 4);
5160 value = 1;
5163 bfd_put_32 (output_bfd, value,
5164 htab->got->contents + off);
5167 off += 4;
5168 if (tls_ty & (TLS_LD | TLS_GD))
5169 off += 4;
5171 while (tls_m != 0);
5173 off = *offp;
5174 *offp = off | 1;
5177 if (off >= (bfd_vma) -2)
5178 abort ();
5180 if ((tls_type & TLS_TLS) != 0)
5182 if (tls_type != (TLS_TLS | TLS_LD))
5184 if ((tls_mask & TLS_LD) != 0
5185 && !(h == NULL
5186 || !(h->elf_link_hash_flags
5187 & ELF_LINK_HASH_DEF_DYNAMIC)))
5188 off += 8;
5189 if (tls_type != (TLS_TLS | TLS_GD))
5191 if ((tls_mask & TLS_GD) != 0)
5192 off += 8;
5193 if (tls_type != (TLS_TLS | TLS_DTPREL))
5195 if ((tls_mask & TLS_DTPREL) != 0)
5196 off += 4;
5202 relocation = htab->got->output_offset + off - 4;
5204 /* Addends on got relocations don't make much sense.
5205 x+off@got is actually x@got+off, and since the got is
5206 generated by a hash table traversal, the value in the
5207 got at entry m+n bears little relation to the entry m. */
5208 if (addend != 0)
5209 (*_bfd_error_handler)
5210 (_("%s(%s+0x%lx): non-zero addend on %s reloc against `%s'"),
5211 bfd_archive_filename (input_bfd),
5212 bfd_get_section_name (input_bfd, input_section),
5213 (long) rel->r_offset,
5214 howto->name,
5215 sym_name);
5217 break;
5219 /* Relocations that need no special processing. */
5220 case R_PPC_LOCAL24PC:
5221 /* It makes no sense to point a local relocation
5222 at a symbol not in this object. */
5223 if (unresolved_reloc)
5225 if (! (*info->callbacks->undefined_symbol) (info,
5226 h->root.root.string,
5227 input_bfd,
5228 input_section,
5229 rel->r_offset,
5230 TRUE))
5231 return FALSE;
5232 continue;
5234 break;
5236 case R_PPC_DTPREL16:
5237 case R_PPC_DTPREL16_LO:
5238 case R_PPC_DTPREL16_HI:
5239 case R_PPC_DTPREL16_HA:
5240 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
5241 break;
5243 /* Relocations that may need to be propagated if this is a shared
5244 object. */
5245 case R_PPC_TPREL16:
5246 case R_PPC_TPREL16_LO:
5247 case R_PPC_TPREL16_HI:
5248 case R_PPC_TPREL16_HA:
5249 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
5250 /* The TPREL16 relocs shouldn't really be used in shared
5251 libs as they will result in DT_TEXTREL being set, but
5252 support them anyway. */
5253 goto dodyn;
5255 case R_PPC_TPREL32:
5256 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
5257 goto dodyn;
5259 case R_PPC_DTPREL32:
5260 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
5261 goto dodyn;
5263 case R_PPC_DTPMOD32:
5264 relocation = 1;
5265 addend = 0;
5266 goto dodyn;
5268 case R_PPC_REL24:
5269 case R_PPC_REL32:
5270 case R_PPC_REL14:
5271 case R_PPC_REL14_BRTAKEN:
5272 case R_PPC_REL14_BRNTAKEN:
5273 /* If these relocations are not to a named symbol, they can be
5274 handled right here, no need to bother the dynamic linker. */
5275 if (SYMBOL_REFERENCES_LOCAL (info, h)
5276 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
5277 break;
5278 /* fall through */
5280 /* Relocations that always need to be propagated if this is a shared
5281 object. */
5282 case R_PPC_ADDR32:
5283 case R_PPC_ADDR24:
5284 case R_PPC_ADDR16:
5285 case R_PPC_ADDR16_LO:
5286 case R_PPC_ADDR16_HI:
5287 case R_PPC_ADDR16_HA:
5288 case R_PPC_ADDR14:
5289 case R_PPC_ADDR14_BRTAKEN:
5290 case R_PPC_ADDR14_BRNTAKEN:
5291 case R_PPC_UADDR32:
5292 case R_PPC_UADDR16:
5293 /* r_symndx will be zero only for relocs against symbols
5294 from removed linkonce sections, or sections discarded by
5295 a linker script. */
5296 dodyn:
5297 if (r_symndx == 0)
5298 break;
5299 /* Fall thru. */
5301 if ((info->shared
5302 && (h == NULL
5303 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
5304 || h->root.type != bfd_link_hash_undefweak)
5305 && (MUST_BE_DYN_RELOC (r_type)
5306 || !SYMBOL_CALLS_LOCAL (info, h)))
5307 || (ELIMINATE_COPY_RELOCS
5308 && !info->shared
5309 && (input_section->flags & SEC_ALLOC) != 0
5310 && h != NULL
5311 && h->dynindx != -1
5312 && (h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0
5313 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
5314 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0))
5316 int skip;
5318 #ifdef DEBUG
5319 fprintf (stderr, "ppc_elf_relocate_section needs to "
5320 "create relocation for %s\n",
5321 (h && h->root.root.string
5322 ? h->root.root.string : "<unknown>"));
5323 #endif
5325 /* When generating a shared object, these relocations
5326 are copied into the output file to be resolved at run
5327 time. */
5328 if (sreloc == NULL)
5330 const char *name;
5332 name = (bfd_elf_string_from_elf_section
5333 (input_bfd,
5334 elf_elfheader (input_bfd)->e_shstrndx,
5335 elf_section_data (input_section)->rel_hdr.sh_name));
5336 if (name == NULL)
5337 return FALSE;
5339 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
5340 && strcmp (bfd_get_section_name (input_bfd,
5341 input_section),
5342 name + 5) == 0);
5344 sreloc = bfd_get_section_by_name (htab->elf.dynobj, name);
5345 BFD_ASSERT (sreloc != NULL);
5348 skip = 0;
5350 outrel.r_offset =
5351 _bfd_elf_section_offset (output_bfd, info, input_section,
5352 rel->r_offset);
5353 if (outrel.r_offset == (bfd_vma) -1
5354 || outrel.r_offset == (bfd_vma) -2)
5355 skip = (int) outrel.r_offset;
5356 outrel.r_offset += (input_section->output_section->vma
5357 + input_section->output_offset);
5359 if (skip)
5360 memset (&outrel, 0, sizeof outrel);
5361 else if (!SYMBOL_REFERENCES_LOCAL (info, h))
5363 unresolved_reloc = FALSE;
5364 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
5365 outrel.r_addend = rel->r_addend;
5367 else
5369 outrel.r_addend = relocation + rel->r_addend;
5371 if (r_type == R_PPC_ADDR32)
5372 outrel.r_info = ELF32_R_INFO (0, R_PPC_RELATIVE);
5373 else
5375 long indx;
5377 if (bfd_is_abs_section (sec))
5378 indx = 0;
5379 else if (sec == NULL || sec->owner == NULL)
5381 bfd_set_error (bfd_error_bad_value);
5382 return FALSE;
5384 else
5386 asection *osec;
5388 /* We are turning this relocation into one
5389 against a section symbol. It would be
5390 proper to subtract the symbol's value,
5391 osec->vma, from the emitted reloc addend,
5392 but ld.so expects buggy relocs. */
5393 osec = sec->output_section;
5394 indx = elf_section_data (osec)->dynindx;
5395 BFD_ASSERT (indx > 0);
5396 #ifdef DEBUG
5397 if (indx <= 0)
5398 printf ("indx=%d section=%s flags=%08x name=%s\n",
5399 indx, osec->name, osec->flags,
5400 h->root.root.string);
5401 #endif
5404 outrel.r_info = ELF32_R_INFO (indx, r_type);
5408 loc = sreloc->contents;
5409 loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rela);
5410 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
5412 if (skip == -1)
5413 continue;
5415 /* This reloc will be computed at runtime. We clear the memory
5416 so that it contains predictable value. */
5417 if (! skip
5418 && ((input_section->flags & SEC_ALLOC) != 0
5419 || ELF32_R_TYPE (outrel.r_info) != R_PPC_RELATIVE))
5421 relocation = howto->pc_relative ? outrel.r_offset : 0;
5422 addend = 0;
5423 break;
5426 break;
5428 case R_PPC_RELAX32PC:
5429 relocation -= (input_section->output_section->vma
5430 + input_section->output_offset
5431 + rel->r_offset - 4);
5432 /* Fall thru */
5433 case R_PPC_RELAX32:
5435 unsigned long t0;
5436 unsigned long t1;
5438 t0 = bfd_get_32 (output_bfd, contents + rel->r_offset);
5439 t1 = bfd_get_32 (output_bfd, contents + rel->r_offset + 4);
5441 /* We're clearing the bits for R_PPC_ADDR16_HA
5442 and R_PPC_ADDR16_LO here. */
5443 t0 &= ~0xffff;
5444 t1 &= ~0xffff;
5446 /* t0 is HA, t1 is LO */
5447 relocation += addend;
5448 t0 |= ((relocation + 0x8000) >> 16) & 0xffff;
5449 t1 |= relocation & 0xffff;
5451 bfd_put_32 (output_bfd, t0, contents + rel->r_offset);
5452 bfd_put_32 (output_bfd, t1, contents + rel->r_offset + 4);
5454 continue;
5456 /* Indirect .sdata relocation. */
5457 case R_PPC_EMB_SDAI16:
5458 BFD_ASSERT (htab->sdata != NULL);
5459 relocation
5460 = elf_finish_pointer_linker_section (output_bfd, input_bfd, info,
5461 htab->sdata, h, relocation,
5462 rel, R_PPC_RELATIVE);
5463 break;
5465 /* Indirect .sdata2 relocation. */
5466 case R_PPC_EMB_SDA2I16:
5467 BFD_ASSERT (htab->sdata2 != NULL);
5468 relocation
5469 = elf_finish_pointer_linker_section (output_bfd, input_bfd, info,
5470 htab->sdata2, h, relocation,
5471 rel, R_PPC_RELATIVE);
5472 break;
5474 /* Handle the TOC16 reloc. We want to use the offset within the .got
5475 section, not the actual VMA. This is appropriate when generating
5476 an embedded ELF object, for which the .got section acts like the
5477 AIX .toc section. */
5478 case R_PPC_TOC16: /* phony GOT16 relocations */
5479 BFD_ASSERT (sec != NULL);
5480 BFD_ASSERT (bfd_is_und_section (sec)
5481 || strcmp (bfd_get_section_name (abfd, sec), ".got") == 0
5482 || strcmp (bfd_get_section_name (abfd, sec), ".cgot") == 0)
5484 addend -= sec->output_section->vma + sec->output_offset + 0x8000;
5485 break;
5487 case R_PPC_PLTREL24:
5488 /* Relocation is to the entry for this symbol in the
5489 procedure linkage table. */
5490 BFD_ASSERT (h != NULL);
5492 if (h->plt.offset == (bfd_vma) -1
5493 || htab->plt == NULL)
5495 /* We didn't make a PLT entry for this symbol. This
5496 happens when statically linking PIC code, or when
5497 using -Bsymbolic. */
5498 break;
5501 unresolved_reloc = FALSE;
5502 relocation = (htab->plt->output_section->vma
5503 + htab->plt->output_offset
5504 + h->plt.offset);
5505 break;
5507 /* Relocate against _SDA_BASE_. */
5508 case R_PPC_SDAREL16:
5510 const char *name;
5511 const struct elf_link_hash_entry *sh;
5513 BFD_ASSERT (sec != NULL);
5514 name = bfd_get_section_name (abfd, sec->output_section);
5515 if (! ((strncmp (name, ".sdata", 6) == 0
5516 && (name[6] == 0 || name[6] == '.'))
5517 || (strncmp (name, ".sbss", 5) == 0
5518 && (name[5] == 0 || name[5] == '.'))))
5520 (*_bfd_error_handler)
5521 (_("%s: the target (%s) of a %s relocation is "
5522 "in the wrong output section (%s)"),
5523 bfd_archive_filename (input_bfd),
5524 sym_name,
5525 howto->name,
5526 name);
5528 sh = htab->sdata->sym_hash;
5529 addend -= (sh->root.u.def.value
5530 + sh->root.u.def.section->output_section->vma
5531 + sh->root.u.def.section->output_offset);
5533 break;
5535 /* Relocate against _SDA2_BASE_. */
5536 case R_PPC_EMB_SDA2REL:
5538 const char *name;
5539 const struct elf_link_hash_entry *sh;
5541 BFD_ASSERT (sec != NULL);
5542 name = bfd_get_section_name (abfd, sec->output_section);
5543 if (! (strncmp (name, ".sdata2", 7) == 0
5544 || strncmp (name, ".sbss2", 6) == 0))
5546 (*_bfd_error_handler)
5547 (_("%s: the target (%s) of a %s relocation is "
5548 "in the wrong output section (%s)"),
5549 bfd_archive_filename (input_bfd),
5550 sym_name,
5551 howto->name,
5552 name);
5554 bfd_set_error (bfd_error_bad_value);
5555 ret = FALSE;
5556 continue;
5558 sh = htab->sdata2->sym_hash;
5559 addend -= (sh->root.u.def.value
5560 + sh->root.u.def.section->output_section->vma
5561 + sh->root.u.def.section->output_offset);
5563 break;
5565 /* Relocate against either _SDA_BASE_, _SDA2_BASE_, or 0. */
5566 case R_PPC_EMB_SDA21:
5567 case R_PPC_EMB_RELSDA:
5569 const char *name;
5570 const struct elf_link_hash_entry *sh;
5571 int reg;
5573 BFD_ASSERT (sec != NULL);
5574 name = bfd_get_section_name (abfd, sec->output_section);
5575 if (((strncmp (name, ".sdata", 6) == 0
5576 && (name[6] == 0 || name[6] == '.'))
5577 || (strncmp (name, ".sbss", 5) == 0
5578 && (name[5] == 0 || name[5] == '.'))))
5580 reg = 13;
5581 sh = htab->sdata->sym_hash;
5582 addend -= (sh->root.u.def.value
5583 + sh->root.u.def.section->output_section->vma
5584 + sh->root.u.def.section->output_offset);
5587 else if (strncmp (name, ".sdata2", 7) == 0
5588 || strncmp (name, ".sbss2", 6) == 0)
5590 reg = 2;
5591 sh = htab->sdata2->sym_hash;
5592 addend -= (sh->root.u.def.value
5593 + sh->root.u.def.section->output_section->vma
5594 + sh->root.u.def.section->output_offset);
5597 else if (strcmp (name, ".PPC.EMB.sdata0") == 0
5598 || strcmp (name, ".PPC.EMB.sbss0") == 0)
5600 reg = 0;
5603 else
5605 (*_bfd_error_handler)
5606 (_("%s: the target (%s) of a %s relocation is "
5607 "in the wrong output section (%s)"),
5608 bfd_archive_filename (input_bfd),
5609 sym_name,
5610 howto->name,
5611 name);
5613 bfd_set_error (bfd_error_bad_value);
5614 ret = FALSE;
5615 continue;
5618 if (r_type == R_PPC_EMB_SDA21)
5619 { /* fill in register field */
5620 insn = bfd_get_32 (output_bfd, contents + rel->r_offset);
5621 insn = (insn & ~RA_REGISTER_MASK) | (reg << RA_REGISTER_SHIFT);
5622 bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
5625 break;
5627 /* Relocate against the beginning of the section. */
5628 case R_PPC_SECTOFF:
5629 case R_PPC_SECTOFF_LO:
5630 case R_PPC_SECTOFF_HI:
5631 case R_PPC_SECTOFF_HA:
5632 BFD_ASSERT (sec != NULL);
5633 addend -= sec->output_section->vma;
5634 break;
5636 /* Negative relocations. */
5637 case R_PPC_EMB_NADDR32:
5638 case R_PPC_EMB_NADDR16:
5639 case R_PPC_EMB_NADDR16_LO:
5640 case R_PPC_EMB_NADDR16_HI:
5641 case R_PPC_EMB_NADDR16_HA:
5642 addend -= 2 * relocation;
5643 break;
5645 case R_PPC_COPY:
5646 case R_PPC_GLOB_DAT:
5647 case R_PPC_JMP_SLOT:
5648 case R_PPC_RELATIVE:
5649 case R_PPC_PLT32:
5650 case R_PPC_PLTREL32:
5651 case R_PPC_PLT16_LO:
5652 case R_PPC_PLT16_HI:
5653 case R_PPC_PLT16_HA:
5654 case R_PPC_ADDR30:
5655 case R_PPC_EMB_RELSEC16:
5656 case R_PPC_EMB_RELST_LO:
5657 case R_PPC_EMB_RELST_HI:
5658 case R_PPC_EMB_RELST_HA:
5659 case R_PPC_EMB_BIT_FLD:
5660 (*_bfd_error_handler)
5661 (_("%s: relocation %s is not yet supported for symbol %s."),
5662 bfd_archive_filename (input_bfd),
5663 howto->name,
5664 sym_name);
5666 bfd_set_error (bfd_error_invalid_operation);
5667 ret = FALSE;
5668 continue;
5671 /* Do any further special processing. */
5672 switch (r_type)
5674 default:
5675 break;
5677 case R_PPC_ADDR16_HA:
5678 case R_PPC_GOT16_HA:
5679 case R_PPC_PLT16_HA:
5680 case R_PPC_SECTOFF_HA:
5681 case R_PPC_TPREL16_HA:
5682 case R_PPC_DTPREL16_HA:
5683 case R_PPC_GOT_TLSGD16_HA:
5684 case R_PPC_GOT_TLSLD16_HA:
5685 case R_PPC_GOT_TPREL16_HA:
5686 case R_PPC_GOT_DTPREL16_HA:
5687 case R_PPC_EMB_NADDR16_HA:
5688 case R_PPC_EMB_RELST_HA:
5689 /* It's just possible that this symbol is a weak symbol
5690 that's not actually defined anywhere. In that case,
5691 'sec' would be NULL, and we should leave the symbol
5692 alone (it will be set to zero elsewhere in the link). */
5693 if (sec != NULL)
5694 /* Add 0x10000 if sign bit in 0:15 is set.
5695 Bits 0:15 are not used. */
5696 addend += 0x8000;
5697 break;
5700 #ifdef DEBUG
5701 fprintf (stderr, "\ttype = %s (%d), name = %s, symbol index = %ld, "
5702 "offset = %ld, addend = %ld\n",
5703 howto->name,
5704 (int) r_type,
5705 sym_name,
5706 r_symndx,
5707 (long) rel->r_offset,
5708 (long) addend);
5709 #endif
5711 if (unresolved_reloc
5712 && !((input_section->flags & SEC_DEBUGGING) != 0
5713 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0))
5715 (*_bfd_error_handler)
5716 (_("%s(%s+0x%lx): unresolvable %s relocation against symbol `%s'"),
5717 bfd_archive_filename (input_bfd),
5718 bfd_get_section_name (input_bfd, input_section),
5719 (long) rel->r_offset,
5720 howto->name,
5721 sym_name);
5722 ret = FALSE;
5725 r = _bfd_final_link_relocate (howto,
5726 input_bfd,
5727 input_section,
5728 contents,
5729 rel->r_offset,
5730 relocation,
5731 addend);
5733 if (r != bfd_reloc_ok)
5735 if (sym_name == NULL)
5736 sym_name = "(null)";
5737 if (r == bfd_reloc_overflow)
5739 if (warned)
5740 continue;
5741 if (h != NULL
5742 && h->root.type == bfd_link_hash_undefweak
5743 && howto->pc_relative)
5745 /* Assume this is a call protected by other code that
5746 detect the symbol is undefined. If this is the case,
5747 we can safely ignore the overflow. If not, the
5748 program is hosed anyway, and a little warning isn't
5749 going to help. */
5751 continue;
5754 if (! (*info->callbacks->reloc_overflow) (info,
5755 sym_name,
5756 howto->name,
5757 rel->r_addend,
5758 input_bfd,
5759 input_section,
5760 rel->r_offset))
5761 return FALSE;
5763 else
5765 (*_bfd_error_handler)
5766 (_("%s(%s+0x%lx): %s reloc against `%s': error %d"),
5767 bfd_archive_filename (input_bfd),
5768 bfd_get_section_name (input_bfd, input_section),
5769 (long) rel->r_offset, howto->name, sym_name, (int) r);
5770 ret = FALSE;
5775 #ifdef DEBUG
5776 fprintf (stderr, "\n");
5777 #endif
5779 return ret;
5782 static enum elf_reloc_type_class
5783 ppc_elf_reloc_type_class (const Elf_Internal_Rela *rela)
5785 switch (ELF32_R_TYPE (rela->r_info))
5787 case R_PPC_RELATIVE:
5788 return reloc_class_relative;
5789 case R_PPC_REL24:
5790 case R_PPC_ADDR24:
5791 case R_PPC_JMP_SLOT:
5792 return reloc_class_plt;
5793 case R_PPC_COPY:
5794 return reloc_class_copy;
5795 default:
5796 return reloc_class_normal;
5800 /* Support for core dump NOTE sections. */
5802 static bfd_boolean
5803 ppc_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
5805 int offset;
5806 unsigned int raw_size;
5808 switch (note->descsz)
5810 default:
5811 return FALSE;
5813 case 268: /* Linux/PPC. */
5814 /* pr_cursig */
5815 elf_tdata (abfd)->core_signal = bfd_get_16 (abfd, note->descdata + 12);
5817 /* pr_pid */
5818 elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, note->descdata + 24);
5820 /* pr_reg */
5821 offset = 72;
5822 raw_size = 192;
5824 break;
5827 /* Make a ".reg/999" section. */
5828 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
5829 raw_size, note->descpos + offset);
5832 static bfd_boolean
5833 ppc_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
5835 switch (note->descsz)
5837 default:
5838 return FALSE;
5840 case 128: /* Linux/PPC elf_prpsinfo. */
5841 elf_tdata (abfd)->core_program
5842 = _bfd_elfcore_strndup (abfd, note->descdata + 32, 16);
5843 elf_tdata (abfd)->core_command
5844 = _bfd_elfcore_strndup (abfd, note->descdata + 48, 80);
5847 /* Note that for some reason, a spurious space is tacked
5848 onto the end of the args in some (at least one anyway)
5849 implementations, so strip it off if it exists. */
5852 char *command = elf_tdata (abfd)->core_command;
5853 int n = strlen (command);
5855 if (0 < n && command[n - 1] == ' ')
5856 command[n - 1] = '\0';
5859 return TRUE;
5862 /* Very simple linked list structure for recording apuinfo values. */
5863 typedef struct apuinfo_list
5865 struct apuinfo_list *next;
5866 unsigned long value;
5868 apuinfo_list;
5870 static apuinfo_list *head;
5873 static void
5874 apuinfo_list_init (void)
5876 head = NULL;
5879 static void
5880 apuinfo_list_add (unsigned long value)
5882 apuinfo_list *entry = head;
5884 while (entry != NULL)
5886 if (entry->value == value)
5887 return;
5888 entry = entry->next;
5891 entry = bfd_malloc (sizeof (* entry));
5892 if (entry == NULL)
5893 return;
5895 entry->value = value;
5896 entry->next = head;
5897 head = entry;
5900 static unsigned
5901 apuinfo_list_length (void)
5903 apuinfo_list *entry;
5904 unsigned long count;
5906 for (entry = head, count = 0;
5907 entry;
5908 entry = entry->next)
5909 ++ count;
5911 return count;
5914 static inline unsigned long
5915 apuinfo_list_element (unsigned long number)
5917 apuinfo_list * entry;
5919 for (entry = head;
5920 entry && number --;
5921 entry = entry->next)
5924 return entry ? entry->value : 0;
5927 static void
5928 apuinfo_list_finish (void)
5930 apuinfo_list *entry;
5932 for (entry = head; entry;)
5934 apuinfo_list *next = entry->next;
5935 free (entry);
5936 entry = next;
5939 head = NULL;
5942 #define APUINFO_SECTION_NAME ".PPC.EMB.apuinfo"
5943 #define APUINFO_LABEL "APUinfo"
5945 /* Scan the input BFDs and create a linked list of
5946 the APUinfo values that will need to be emitted. */
5948 static void
5949 ppc_elf_begin_write_processing (bfd *abfd, struct bfd_link_info *link_info)
5951 bfd *ibfd;
5952 asection *asec;
5953 char *buffer;
5954 unsigned num_input_sections;
5955 bfd_size_type output_section_size;
5956 unsigned i;
5957 unsigned num_entries;
5958 unsigned long offset;
5959 unsigned long length;
5960 const char *error_message = NULL;
5962 if (link_info == NULL)
5963 return;
5965 /* Scan the input bfds, looking for apuinfo sections. */
5966 num_input_sections = 0;
5967 output_section_size = 0;
5969 for (ibfd = link_info->input_bfds; ibfd; ibfd = ibfd->link_next)
5971 asec = bfd_get_section_by_name (ibfd, APUINFO_SECTION_NAME);
5972 if (asec)
5974 ++ num_input_sections;
5975 output_section_size += asec->_raw_size;
5979 /* We need at least one input sections
5980 in order to make merging worthwhile. */
5981 if (num_input_sections < 1)
5982 return;
5984 /* Just make sure that the output section exists as well. */
5985 asec = bfd_get_section_by_name (abfd, APUINFO_SECTION_NAME);
5986 if (asec == NULL)
5987 return;
5989 /* Allocate a buffer for the contents of the input sections. */
5990 buffer = bfd_malloc (output_section_size);
5991 if (buffer == NULL)
5992 return;
5994 offset = 0;
5995 apuinfo_list_init ();
5997 /* Read in the input sections contents. */
5998 for (ibfd = link_info->input_bfds; ibfd; ibfd = ibfd->link_next)
6000 unsigned long datum;
6001 char *ptr;
6003 asec = bfd_get_section_by_name (ibfd, APUINFO_SECTION_NAME);
6004 if (asec == NULL)
6005 continue;
6007 length = asec->_raw_size;
6008 if (length < 24)
6010 error_message = _("corrupt or empty %s section in %s");
6011 goto fail;
6014 if (bfd_seek (ibfd, asec->filepos, SEEK_SET) != 0
6015 || (bfd_bread (buffer + offset, length, ibfd) != length))
6017 error_message = _("unable to read in %s section from %s");
6018 goto fail;
6021 /* Process the contents of the section. */
6022 ptr = buffer + offset;
6023 error_message = _("corrupt %s section in %s");
6025 /* Verify the contents of the header. Note - we have to
6026 extract the values this way in order to allow for a
6027 host whose endian-ness is different from the target. */
6028 datum = bfd_get_32 (ibfd, ptr);
6029 if (datum != sizeof APUINFO_LABEL)
6030 goto fail;
6032 datum = bfd_get_32 (ibfd, ptr + 8);
6033 if (datum != 0x2)
6034 goto fail;
6036 if (strcmp (ptr + 12, APUINFO_LABEL) != 0)
6037 goto fail;
6039 /* Get the number of bytes used for apuinfo entries. */
6040 datum = bfd_get_32 (ibfd, ptr + 4);
6041 if (datum + 20 != length)
6042 goto fail;
6044 /* Make sure that we do not run off the end of the section. */
6045 if (offset + length > output_section_size)
6046 goto fail;
6048 /* Scan the apuinfo section, building a list of apuinfo numbers. */
6049 for (i = 0; i < datum; i += 4)
6050 apuinfo_list_add (bfd_get_32 (ibfd, ptr + 20 + i));
6052 /* Update the offset. */
6053 offset += length;
6056 error_message = NULL;
6058 /* Compute the size of the output section. */
6059 num_entries = apuinfo_list_length ();
6060 output_section_size = 20 + num_entries * 4;
6062 asec = bfd_get_section_by_name (abfd, APUINFO_SECTION_NAME);
6064 if (! bfd_set_section_size (abfd, asec, output_section_size))
6065 ibfd = abfd,
6066 error_message = _("warning: unable to set size of %s section in %s");
6068 fail:
6069 free (buffer);
6071 if (error_message)
6072 (*_bfd_error_handler) (error_message, APUINFO_SECTION_NAME,
6073 bfd_archive_filename (ibfd));
6077 /* Prevent the output section from accumulating the input sections'
6078 contents. We have already stored this in our linked list structure. */
6080 static bfd_boolean
6081 ppc_elf_write_section (bfd *abfd ATTRIBUTE_UNUSED,
6082 asection *asec,
6083 bfd_byte *contents ATTRIBUTE_UNUSED)
6085 return (apuinfo_list_length ()
6086 && strcmp (asec->name, APUINFO_SECTION_NAME) == 0);
6090 /* Finally we can generate the output section. */
6092 static void
6093 ppc_elf_final_write_processing (bfd *abfd, bfd_boolean linker ATTRIBUTE_UNUSED)
6095 bfd_byte *buffer;
6096 asection *asec;
6097 unsigned i;
6098 unsigned num_entries;
6099 bfd_size_type length;
6101 asec = bfd_get_section_by_name (abfd, APUINFO_SECTION_NAME);
6102 if (asec == NULL)
6103 return;
6105 if (apuinfo_list_length () == 0)
6106 return;
6108 length = asec->_raw_size;
6109 if (length < 20)
6110 return;
6112 buffer = bfd_malloc (length);
6113 if (buffer == NULL)
6115 (*_bfd_error_handler)
6116 (_("failed to allocate space for new APUinfo section."));
6117 return;
6120 /* Create the apuinfo header. */
6121 num_entries = apuinfo_list_length ();
6122 bfd_put_32 (abfd, sizeof APUINFO_LABEL, buffer);
6123 bfd_put_32 (abfd, num_entries * 4, buffer + 4);
6124 bfd_put_32 (abfd, 0x2, buffer + 8);
6125 strcpy (buffer + 12, APUINFO_LABEL);
6127 length = 20;
6128 for (i = 0; i < num_entries; i++)
6130 bfd_put_32 (abfd, apuinfo_list_element (i), buffer + length);
6131 length += 4;
6134 if (length != asec->_raw_size)
6135 (*_bfd_error_handler) (_("failed to compute new APUinfo section."));
6137 if (! bfd_set_section_contents (abfd, asec, buffer, (file_ptr) 0, length))
6138 (*_bfd_error_handler) (_("failed to install new APUinfo section."));
6140 free (buffer);
6142 apuinfo_list_finish ();
6145 /* Add extra PPC sections -- Note, for now, make .sbss2 and
6146 .PPC.EMB.sbss0 a normal section, and not a bss section so
6147 that the linker doesn't crater when trying to make more than
6148 2 sections. */
6150 static struct bfd_elf_special_section const ppc_elf_special_sections[]=
6152 { ".tags", 5, 0, SHT_ORDERED, SHF_ALLOC },
6153 { ".sdata", 6, -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
6154 { ".sbss", 5, -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
6155 { ".sdata2", 7, -2, SHT_PROGBITS, SHF_ALLOC },
6156 { ".sbss2", 6, -2, SHT_PROGBITS, SHF_ALLOC },
6157 { ".PPC.EMB.apuinfo", 16, 0, SHT_NOTE, 0 },
6158 { ".PPC.EMB.sdata0", 15, 0, SHT_PROGBITS, SHF_ALLOC },
6159 { ".PPC.EMB.sbss0", 14, 0, SHT_PROGBITS, SHF_ALLOC },
6160 { ".plt", 4, 0, SHT_NOBITS, SHF_ALLOC + SHF_EXECINSTR },
6161 { NULL, 0, 0, 0, 0 }
6164 #define TARGET_LITTLE_SYM bfd_elf32_powerpcle_vec
6165 #define TARGET_LITTLE_NAME "elf32-powerpcle"
6166 #define TARGET_BIG_SYM bfd_elf32_powerpc_vec
6167 #define TARGET_BIG_NAME "elf32-powerpc"
6168 #define ELF_ARCH bfd_arch_powerpc
6169 #define ELF_MACHINE_CODE EM_PPC
6170 #ifdef __QNXTARGET__
6171 #define ELF_MAXPAGESIZE 0x1000
6172 #else
6173 #define ELF_MAXPAGESIZE 0x10000
6174 #endif
6175 #define elf_info_to_howto ppc_elf_info_to_howto
6177 #ifdef EM_CYGNUS_POWERPC
6178 #define ELF_MACHINE_ALT1 EM_CYGNUS_POWERPC
6179 #endif
6181 #ifdef EM_PPC_OLD
6182 #define ELF_MACHINE_ALT2 EM_PPC_OLD
6183 #endif
6185 #define elf_backend_plt_not_loaded 1
6186 #define elf_backend_got_symbol_offset 4
6187 #define elf_backend_can_gc_sections 1
6188 #define elf_backend_can_refcount 1
6189 #define elf_backend_got_header_size 12
6190 #define elf_backend_rela_normal 1
6192 #define bfd_elf32_mkobject ppc_elf_mkobject
6193 #define bfd_elf32_bfd_merge_private_bfd_data ppc_elf_merge_private_bfd_data
6194 #define bfd_elf32_bfd_relax_section ppc_elf_relax_section
6195 #define bfd_elf32_bfd_reloc_type_lookup ppc_elf_reloc_type_lookup
6196 #define bfd_elf32_bfd_set_private_flags ppc_elf_set_private_flags
6197 #define bfd_elf32_bfd_link_hash_table_create ppc_elf_link_hash_table_create
6199 #define elf_backend_object_p ppc_elf_object_p
6200 #define elf_backend_gc_mark_hook ppc_elf_gc_mark_hook
6201 #define elf_backend_gc_sweep_hook ppc_elf_gc_sweep_hook
6202 #define elf_backend_section_from_shdr ppc_elf_section_from_shdr
6203 #define elf_backend_relocate_section ppc_elf_relocate_section
6204 #define elf_backend_create_dynamic_sections ppc_elf_create_dynamic_sections
6205 #define elf_backend_check_relocs ppc_elf_check_relocs
6206 #define elf_backend_copy_indirect_symbol ppc_elf_copy_indirect_symbol
6207 #define elf_backend_adjust_dynamic_symbol ppc_elf_adjust_dynamic_symbol
6208 #define elf_backend_add_symbol_hook ppc_elf_add_symbol_hook
6209 #define elf_backend_size_dynamic_sections ppc_elf_size_dynamic_sections
6210 #define elf_backend_finish_dynamic_symbol ppc_elf_finish_dynamic_symbol
6211 #define elf_backend_finish_dynamic_sections ppc_elf_finish_dynamic_sections
6212 #define elf_backend_fake_sections ppc_elf_fake_sections
6213 #define elf_backend_additional_program_headers ppc_elf_additional_program_headers
6214 #define elf_backend_modify_segment_map ppc_elf_modify_segment_map
6215 #define elf_backend_grok_prstatus ppc_elf_grok_prstatus
6216 #define elf_backend_grok_psinfo ppc_elf_grok_psinfo
6217 #define elf_backend_reloc_type_class ppc_elf_reloc_type_class
6218 #define elf_backend_begin_write_processing ppc_elf_begin_write_processing
6219 #define elf_backend_final_write_processing ppc_elf_final_write_processing
6220 #define elf_backend_write_section ppc_elf_write_section
6221 #define elf_backend_special_sections ppc_elf_special_sections
6223 #include "elf32-target.h"