* elf.c (_bfd_elf_rela_local_sym): Accept asection **, and return
[binutils.git] / bfd / elf32-ppc.c
blob71a5d454211beda5c47e346e951a19bf64e51b1b
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 else
341 _bfd_elf_link_hash_copy_indirect (bed, dir, ind);
344 static reloc_howto_type *ppc_elf_howto_table[R_PPC_max];
346 static reloc_howto_type ppc_elf_howto_raw[] = {
347 /* This reloc does nothing. */
348 HOWTO (R_PPC_NONE, /* type */
349 0, /* rightshift */
350 2, /* size (0 = byte, 1 = short, 2 = long) */
351 32, /* bitsize */
352 FALSE, /* pc_relative */
353 0, /* bitpos */
354 complain_overflow_bitfield, /* complain_on_overflow */
355 bfd_elf_generic_reloc, /* special_function */
356 "R_PPC_NONE", /* name */
357 FALSE, /* partial_inplace */
358 0, /* src_mask */
359 0, /* dst_mask */
360 FALSE), /* pcrel_offset */
362 /* A standard 32 bit relocation. */
363 HOWTO (R_PPC_ADDR32, /* type */
364 0, /* rightshift */
365 2, /* size (0 = byte, 1 = short, 2 = long) */
366 32, /* bitsize */
367 FALSE, /* pc_relative */
368 0, /* bitpos */
369 complain_overflow_bitfield, /* complain_on_overflow */
370 bfd_elf_generic_reloc, /* special_function */
371 "R_PPC_ADDR32", /* name */
372 FALSE, /* partial_inplace */
373 0, /* src_mask */
374 0xffffffff, /* dst_mask */
375 FALSE), /* pcrel_offset */
377 /* An absolute 26 bit branch; the lower two bits must be zero.
378 FIXME: we don't check that, we just clear them. */
379 HOWTO (R_PPC_ADDR24, /* type */
380 0, /* rightshift */
381 2, /* size (0 = byte, 1 = short, 2 = long) */
382 26, /* bitsize */
383 FALSE, /* pc_relative */
384 0, /* bitpos */
385 complain_overflow_bitfield, /* complain_on_overflow */
386 bfd_elf_generic_reloc, /* special_function */
387 "R_PPC_ADDR24", /* name */
388 FALSE, /* partial_inplace */
389 0, /* src_mask */
390 0x3fffffc, /* dst_mask */
391 FALSE), /* pcrel_offset */
393 /* A standard 16 bit relocation. */
394 HOWTO (R_PPC_ADDR16, /* type */
395 0, /* rightshift */
396 1, /* size (0 = byte, 1 = short, 2 = long) */
397 16, /* bitsize */
398 FALSE, /* pc_relative */
399 0, /* bitpos */
400 complain_overflow_bitfield, /* complain_on_overflow */
401 bfd_elf_generic_reloc, /* special_function */
402 "R_PPC_ADDR16", /* name */
403 FALSE, /* partial_inplace */
404 0, /* src_mask */
405 0xffff, /* dst_mask */
406 FALSE), /* pcrel_offset */
408 /* A 16 bit relocation without overflow. */
409 HOWTO (R_PPC_ADDR16_LO, /* type */
410 0, /* rightshift */
411 1, /* size (0 = byte, 1 = short, 2 = long) */
412 16, /* bitsize */
413 FALSE, /* pc_relative */
414 0, /* bitpos */
415 complain_overflow_dont,/* complain_on_overflow */
416 bfd_elf_generic_reloc, /* special_function */
417 "R_PPC_ADDR16_LO", /* name */
418 FALSE, /* partial_inplace */
419 0, /* src_mask */
420 0xffff, /* dst_mask */
421 FALSE), /* pcrel_offset */
423 /* The high order 16 bits of an address. */
424 HOWTO (R_PPC_ADDR16_HI, /* type */
425 16, /* rightshift */
426 1, /* size (0 = byte, 1 = short, 2 = long) */
427 16, /* bitsize */
428 FALSE, /* pc_relative */
429 0, /* bitpos */
430 complain_overflow_dont, /* complain_on_overflow */
431 bfd_elf_generic_reloc, /* special_function */
432 "R_PPC_ADDR16_HI", /* name */
433 FALSE, /* partial_inplace */
434 0, /* src_mask */
435 0xffff, /* dst_mask */
436 FALSE), /* pcrel_offset */
438 /* The high order 16 bits of an address, plus 1 if the contents of
439 the low 16 bits, treated as a signed number, is negative. */
440 HOWTO (R_PPC_ADDR16_HA, /* type */
441 16, /* rightshift */
442 1, /* size (0 = byte, 1 = short, 2 = long) */
443 16, /* bitsize */
444 FALSE, /* pc_relative */
445 0, /* bitpos */
446 complain_overflow_dont, /* complain_on_overflow */
447 ppc_elf_addr16_ha_reloc, /* special_function */
448 "R_PPC_ADDR16_HA", /* name */
449 FALSE, /* partial_inplace */
450 0, /* src_mask */
451 0xffff, /* dst_mask */
452 FALSE), /* pcrel_offset */
454 /* An absolute 16 bit branch; the lower two bits must be zero.
455 FIXME: we don't check that, we just clear them. */
456 HOWTO (R_PPC_ADDR14, /* type */
457 0, /* rightshift */
458 2, /* size (0 = byte, 1 = short, 2 = long) */
459 16, /* bitsize */
460 FALSE, /* pc_relative */
461 0, /* bitpos */
462 complain_overflow_bitfield, /* complain_on_overflow */
463 bfd_elf_generic_reloc, /* special_function */
464 "R_PPC_ADDR14", /* name */
465 FALSE, /* partial_inplace */
466 0, /* src_mask */
467 0xfffc, /* dst_mask */
468 FALSE), /* pcrel_offset */
470 /* An absolute 16 bit branch, for which bit 10 should be set to
471 indicate that the branch is expected to be taken. The lower two
472 bits must be zero. */
473 HOWTO (R_PPC_ADDR14_BRTAKEN, /* type */
474 0, /* rightshift */
475 2, /* size (0 = byte, 1 = short, 2 = long) */
476 16, /* bitsize */
477 FALSE, /* pc_relative */
478 0, /* bitpos */
479 complain_overflow_bitfield, /* complain_on_overflow */
480 bfd_elf_generic_reloc, /* special_function */
481 "R_PPC_ADDR14_BRTAKEN",/* name */
482 FALSE, /* partial_inplace */
483 0, /* src_mask */
484 0xfffc, /* dst_mask */
485 FALSE), /* pcrel_offset */
487 /* An absolute 16 bit branch, for which bit 10 should be set to
488 indicate that the branch is not expected to be taken. The lower
489 two bits must be zero. */
490 HOWTO (R_PPC_ADDR14_BRNTAKEN, /* type */
491 0, /* rightshift */
492 2, /* size (0 = byte, 1 = short, 2 = long) */
493 16, /* bitsize */
494 FALSE, /* pc_relative */
495 0, /* bitpos */
496 complain_overflow_bitfield, /* complain_on_overflow */
497 bfd_elf_generic_reloc, /* special_function */
498 "R_PPC_ADDR14_BRNTAKEN",/* name */
499 FALSE, /* partial_inplace */
500 0, /* src_mask */
501 0xfffc, /* dst_mask */
502 FALSE), /* pcrel_offset */
504 /* A relative 26 bit branch; the lower two bits must be zero. */
505 HOWTO (R_PPC_REL24, /* type */
506 0, /* rightshift */
507 2, /* size (0 = byte, 1 = short, 2 = long) */
508 26, /* bitsize */
509 TRUE, /* pc_relative */
510 0, /* bitpos */
511 complain_overflow_signed, /* complain_on_overflow */
512 bfd_elf_generic_reloc, /* special_function */
513 "R_PPC_REL24", /* name */
514 FALSE, /* partial_inplace */
515 0, /* src_mask */
516 0x3fffffc, /* dst_mask */
517 TRUE), /* pcrel_offset */
519 /* A relative 16 bit branch; the lower two bits must be zero. */
520 HOWTO (R_PPC_REL14, /* type */
521 0, /* rightshift */
522 2, /* size (0 = byte, 1 = short, 2 = long) */
523 16, /* bitsize */
524 TRUE, /* pc_relative */
525 0, /* bitpos */
526 complain_overflow_signed, /* complain_on_overflow */
527 bfd_elf_generic_reloc, /* special_function */
528 "R_PPC_REL14", /* name */
529 FALSE, /* partial_inplace */
530 0, /* src_mask */
531 0xfffc, /* dst_mask */
532 TRUE), /* pcrel_offset */
534 /* A relative 16 bit branch. Bit 10 should be set to indicate that
535 the branch is expected to be taken. The lower two bits must be
536 zero. */
537 HOWTO (R_PPC_REL14_BRTAKEN, /* type */
538 0, /* rightshift */
539 2, /* size (0 = byte, 1 = short, 2 = long) */
540 16, /* bitsize */
541 TRUE, /* pc_relative */
542 0, /* bitpos */
543 complain_overflow_signed, /* complain_on_overflow */
544 bfd_elf_generic_reloc, /* special_function */
545 "R_PPC_REL14_BRTAKEN", /* name */
546 FALSE, /* partial_inplace */
547 0, /* src_mask */
548 0xfffc, /* dst_mask */
549 TRUE), /* pcrel_offset */
551 /* A relative 16 bit branch. Bit 10 should be set to indicate that
552 the branch is not expected to be taken. The lower two bits must
553 be zero. */
554 HOWTO (R_PPC_REL14_BRNTAKEN, /* type */
555 0, /* rightshift */
556 2, /* size (0 = byte, 1 = short, 2 = long) */
557 16, /* bitsize */
558 TRUE, /* pc_relative */
559 0, /* bitpos */
560 complain_overflow_signed, /* complain_on_overflow */
561 bfd_elf_generic_reloc, /* special_function */
562 "R_PPC_REL14_BRNTAKEN",/* name */
563 FALSE, /* partial_inplace */
564 0, /* src_mask */
565 0xfffc, /* dst_mask */
566 TRUE), /* pcrel_offset */
568 /* Like R_PPC_ADDR16, but referring to the GOT table entry for the
569 symbol. */
570 HOWTO (R_PPC_GOT16, /* type */
571 0, /* rightshift */
572 1, /* size (0 = byte, 1 = short, 2 = long) */
573 16, /* bitsize */
574 FALSE, /* pc_relative */
575 0, /* bitpos */
576 complain_overflow_signed, /* complain_on_overflow */
577 bfd_elf_generic_reloc, /* special_function */
578 "R_PPC_GOT16", /* name */
579 FALSE, /* partial_inplace */
580 0, /* src_mask */
581 0xffff, /* dst_mask */
582 FALSE), /* pcrel_offset */
584 /* Like R_PPC_ADDR16_LO, but referring to the GOT table entry for
585 the symbol. */
586 HOWTO (R_PPC_GOT16_LO, /* type */
587 0, /* rightshift */
588 1, /* size (0 = byte, 1 = short, 2 = long) */
589 16, /* bitsize */
590 FALSE, /* pc_relative */
591 0, /* bitpos */
592 complain_overflow_dont, /* complain_on_overflow */
593 bfd_elf_generic_reloc, /* special_function */
594 "R_PPC_GOT16_LO", /* name */
595 FALSE, /* partial_inplace */
596 0, /* src_mask */
597 0xffff, /* dst_mask */
598 FALSE), /* pcrel_offset */
600 /* Like R_PPC_ADDR16_HI, but referring to the GOT table entry for
601 the symbol. */
602 HOWTO (R_PPC_GOT16_HI, /* type */
603 16, /* rightshift */
604 1, /* size (0 = byte, 1 = short, 2 = long) */
605 16, /* bitsize */
606 FALSE, /* pc_relative */
607 0, /* bitpos */
608 complain_overflow_bitfield, /* complain_on_overflow */
609 bfd_elf_generic_reloc, /* special_function */
610 "R_PPC_GOT16_HI", /* name */
611 FALSE, /* partial_inplace */
612 0, /* src_mask */
613 0xffff, /* dst_mask */
614 FALSE), /* pcrel_offset */
616 /* Like R_PPC_ADDR16_HA, but referring to the GOT table entry for
617 the symbol. */
618 HOWTO (R_PPC_GOT16_HA, /* type */
619 16, /* rightshift */
620 1, /* size (0 = byte, 1 = short, 2 = long) */
621 16, /* bitsize */
622 FALSE, /* pc_relative */
623 0, /* bitpos */
624 complain_overflow_bitfield, /* complain_on_overflow */
625 ppc_elf_addr16_ha_reloc, /* special_function */
626 "R_PPC_GOT16_HA", /* name */
627 FALSE, /* partial_inplace */
628 0, /* src_mask */
629 0xffff, /* dst_mask */
630 FALSE), /* pcrel_offset */
632 /* Like R_PPC_REL24, but referring to the procedure linkage table
633 entry for the symbol. */
634 HOWTO (R_PPC_PLTREL24, /* type */
635 0, /* rightshift */
636 2, /* size (0 = byte, 1 = short, 2 = long) */
637 26, /* bitsize */
638 TRUE, /* pc_relative */
639 0, /* bitpos */
640 complain_overflow_signed, /* complain_on_overflow */
641 bfd_elf_generic_reloc, /* special_function */
642 "R_PPC_PLTREL24", /* name */
643 FALSE, /* partial_inplace */
644 0, /* src_mask */
645 0x3fffffc, /* dst_mask */
646 TRUE), /* pcrel_offset */
648 /* This is used only by the dynamic linker. The symbol should exist
649 both in the object being run and in some shared library. The
650 dynamic linker copies the data addressed by the symbol from the
651 shared library into the object, because the object being
652 run has to have the data at some particular address. */
653 HOWTO (R_PPC_COPY, /* type */
654 0, /* rightshift */
655 2, /* size (0 = byte, 1 = short, 2 = long) */
656 32, /* bitsize */
657 FALSE, /* pc_relative */
658 0, /* bitpos */
659 complain_overflow_bitfield, /* complain_on_overflow */
660 bfd_elf_generic_reloc, /* special_function */
661 "R_PPC_COPY", /* name */
662 FALSE, /* partial_inplace */
663 0, /* src_mask */
664 0, /* dst_mask */
665 FALSE), /* pcrel_offset */
667 /* Like R_PPC_ADDR32, but used when setting global offset table
668 entries. */
669 HOWTO (R_PPC_GLOB_DAT, /* type */
670 0, /* rightshift */
671 2, /* size (0 = byte, 1 = short, 2 = long) */
672 32, /* bitsize */
673 FALSE, /* pc_relative */
674 0, /* bitpos */
675 complain_overflow_bitfield, /* complain_on_overflow */
676 bfd_elf_generic_reloc, /* special_function */
677 "R_PPC_GLOB_DAT", /* name */
678 FALSE, /* partial_inplace */
679 0, /* src_mask */
680 0xffffffff, /* dst_mask */
681 FALSE), /* pcrel_offset */
683 /* Marks a procedure linkage table entry for a symbol. */
684 HOWTO (R_PPC_JMP_SLOT, /* type */
685 0, /* rightshift */
686 2, /* size (0 = byte, 1 = short, 2 = long) */
687 32, /* bitsize */
688 FALSE, /* pc_relative */
689 0, /* bitpos */
690 complain_overflow_bitfield, /* complain_on_overflow */
691 bfd_elf_generic_reloc, /* special_function */
692 "R_PPC_JMP_SLOT", /* name */
693 FALSE, /* partial_inplace */
694 0, /* src_mask */
695 0, /* dst_mask */
696 FALSE), /* pcrel_offset */
698 /* Used only by the dynamic linker. When the object is run, this
699 longword is set to the load address of the object, plus the
700 addend. */
701 HOWTO (R_PPC_RELATIVE, /* type */
702 0, /* rightshift */
703 2, /* size (0 = byte, 1 = short, 2 = long) */
704 32, /* bitsize */
705 FALSE, /* pc_relative */
706 0, /* bitpos */
707 complain_overflow_bitfield, /* complain_on_overflow */
708 bfd_elf_generic_reloc, /* special_function */
709 "R_PPC_RELATIVE", /* name */
710 FALSE, /* partial_inplace */
711 0, /* src_mask */
712 0xffffffff, /* dst_mask */
713 FALSE), /* pcrel_offset */
715 /* Like R_PPC_REL24, but uses the value of the symbol within the
716 object rather than the final value. Normally used for
717 _GLOBAL_OFFSET_TABLE_. */
718 HOWTO (R_PPC_LOCAL24PC, /* type */
719 0, /* rightshift */
720 2, /* size (0 = byte, 1 = short, 2 = long) */
721 26, /* bitsize */
722 TRUE, /* pc_relative */
723 0, /* bitpos */
724 complain_overflow_signed, /* complain_on_overflow */
725 bfd_elf_generic_reloc, /* special_function */
726 "R_PPC_LOCAL24PC", /* name */
727 FALSE, /* partial_inplace */
728 0, /* src_mask */
729 0x3fffffc, /* dst_mask */
730 TRUE), /* pcrel_offset */
732 /* Like R_PPC_ADDR32, but may be unaligned. */
733 HOWTO (R_PPC_UADDR32, /* type */
734 0, /* rightshift */
735 2, /* size (0 = byte, 1 = short, 2 = long) */
736 32, /* bitsize */
737 FALSE, /* pc_relative */
738 0, /* bitpos */
739 complain_overflow_bitfield, /* complain_on_overflow */
740 bfd_elf_generic_reloc, /* special_function */
741 "R_PPC_UADDR32", /* name */
742 FALSE, /* partial_inplace */
743 0, /* src_mask */
744 0xffffffff, /* dst_mask */
745 FALSE), /* pcrel_offset */
747 /* Like R_PPC_ADDR16, but may be unaligned. */
748 HOWTO (R_PPC_UADDR16, /* type */
749 0, /* rightshift */
750 1, /* size (0 = byte, 1 = short, 2 = long) */
751 16, /* bitsize */
752 FALSE, /* pc_relative */
753 0, /* bitpos */
754 complain_overflow_bitfield, /* complain_on_overflow */
755 bfd_elf_generic_reloc, /* special_function */
756 "R_PPC_UADDR16", /* name */
757 FALSE, /* partial_inplace */
758 0, /* src_mask */
759 0xffff, /* dst_mask */
760 FALSE), /* pcrel_offset */
762 /* 32-bit PC relative */
763 HOWTO (R_PPC_REL32, /* type */
764 0, /* rightshift */
765 2, /* size (0 = byte, 1 = short, 2 = long) */
766 32, /* bitsize */
767 TRUE, /* pc_relative */
768 0, /* bitpos */
769 complain_overflow_bitfield, /* complain_on_overflow */
770 bfd_elf_generic_reloc, /* special_function */
771 "R_PPC_REL32", /* name */
772 FALSE, /* partial_inplace */
773 0, /* src_mask */
774 0xffffffff, /* dst_mask */
775 TRUE), /* pcrel_offset */
777 /* 32-bit relocation to the symbol's procedure linkage table.
778 FIXME: not supported. */
779 HOWTO (R_PPC_PLT32, /* type */
780 0, /* rightshift */
781 2, /* size (0 = byte, 1 = short, 2 = long) */
782 32, /* bitsize */
783 FALSE, /* pc_relative */
784 0, /* bitpos */
785 complain_overflow_bitfield, /* complain_on_overflow */
786 bfd_elf_generic_reloc, /* special_function */
787 "R_PPC_PLT32", /* name */
788 FALSE, /* partial_inplace */
789 0, /* src_mask */
790 0, /* dst_mask */
791 FALSE), /* pcrel_offset */
793 /* 32-bit PC relative relocation to the symbol's procedure linkage table.
794 FIXME: not supported. */
795 HOWTO (R_PPC_PLTREL32, /* type */
796 0, /* rightshift */
797 2, /* size (0 = byte, 1 = short, 2 = long) */
798 32, /* bitsize */
799 TRUE, /* pc_relative */
800 0, /* bitpos */
801 complain_overflow_bitfield, /* complain_on_overflow */
802 bfd_elf_generic_reloc, /* special_function */
803 "R_PPC_PLTREL32", /* name */
804 FALSE, /* partial_inplace */
805 0, /* src_mask */
806 0, /* dst_mask */
807 TRUE), /* pcrel_offset */
809 /* Like R_PPC_ADDR16_LO, but referring to the PLT table entry for
810 the symbol. */
811 HOWTO (R_PPC_PLT16_LO, /* type */
812 0, /* rightshift */
813 1, /* size (0 = byte, 1 = short, 2 = long) */
814 16, /* bitsize */
815 FALSE, /* pc_relative */
816 0, /* bitpos */
817 complain_overflow_dont, /* complain_on_overflow */
818 bfd_elf_generic_reloc, /* special_function */
819 "R_PPC_PLT16_LO", /* name */
820 FALSE, /* partial_inplace */
821 0, /* src_mask */
822 0xffff, /* dst_mask */
823 FALSE), /* pcrel_offset */
825 /* Like R_PPC_ADDR16_HI, but referring to the PLT table entry for
826 the symbol. */
827 HOWTO (R_PPC_PLT16_HI, /* type */
828 16, /* rightshift */
829 1, /* size (0 = byte, 1 = short, 2 = long) */
830 16, /* bitsize */
831 FALSE, /* pc_relative */
832 0, /* bitpos */
833 complain_overflow_bitfield, /* complain_on_overflow */
834 bfd_elf_generic_reloc, /* special_function */
835 "R_PPC_PLT16_HI", /* name */
836 FALSE, /* partial_inplace */
837 0, /* src_mask */
838 0xffff, /* dst_mask */
839 FALSE), /* pcrel_offset */
841 /* Like R_PPC_ADDR16_HA, but referring to the PLT table entry for
842 the symbol. */
843 HOWTO (R_PPC_PLT16_HA, /* type */
844 16, /* rightshift */
845 1, /* size (0 = byte, 1 = short, 2 = long) */
846 16, /* bitsize */
847 FALSE, /* pc_relative */
848 0, /* bitpos */
849 complain_overflow_bitfield, /* complain_on_overflow */
850 ppc_elf_addr16_ha_reloc, /* special_function */
851 "R_PPC_PLT16_HA", /* name */
852 FALSE, /* partial_inplace */
853 0, /* src_mask */
854 0xffff, /* dst_mask */
855 FALSE), /* pcrel_offset */
857 /* A sign-extended 16 bit value relative to _SDA_BASE_, for use with
858 small data items. */
859 HOWTO (R_PPC_SDAREL16, /* type */
860 0, /* rightshift */
861 1, /* size (0 = byte, 1 = short, 2 = long) */
862 16, /* bitsize */
863 FALSE, /* pc_relative */
864 0, /* bitpos */
865 complain_overflow_signed, /* complain_on_overflow */
866 bfd_elf_generic_reloc, /* special_function */
867 "R_PPC_SDAREL16", /* name */
868 FALSE, /* partial_inplace */
869 0, /* src_mask */
870 0xffff, /* dst_mask */
871 FALSE), /* pcrel_offset */
873 /* 16-bit section relative relocation. */
874 HOWTO (R_PPC_SECTOFF, /* type */
875 0, /* rightshift */
876 1, /* size (0 = byte, 1 = short, 2 = long) */
877 16, /* bitsize */
878 FALSE, /* pc_relative */
879 0, /* bitpos */
880 complain_overflow_bitfield, /* complain_on_overflow */
881 bfd_elf_generic_reloc, /* special_function */
882 "R_PPC_SECTOFF", /* name */
883 FALSE, /* partial_inplace */
884 0, /* src_mask */
885 0xffff, /* dst_mask */
886 FALSE), /* pcrel_offset */
888 /* 16-bit lower half section relative relocation. */
889 HOWTO (R_PPC_SECTOFF_LO, /* type */
890 0, /* rightshift */
891 1, /* size (0 = byte, 1 = short, 2 = long) */
892 16, /* bitsize */
893 FALSE, /* pc_relative */
894 0, /* bitpos */
895 complain_overflow_dont, /* complain_on_overflow */
896 bfd_elf_generic_reloc, /* special_function */
897 "R_PPC_SECTOFF_LO", /* name */
898 FALSE, /* partial_inplace */
899 0, /* src_mask */
900 0xffff, /* dst_mask */
901 FALSE), /* pcrel_offset */
903 /* 16-bit upper half section relative relocation. */
904 HOWTO (R_PPC_SECTOFF_HI, /* type */
905 16, /* rightshift */
906 1, /* size (0 = byte, 1 = short, 2 = long) */
907 16, /* bitsize */
908 FALSE, /* pc_relative */
909 0, /* bitpos */
910 complain_overflow_bitfield, /* complain_on_overflow */
911 bfd_elf_generic_reloc, /* special_function */
912 "R_PPC_SECTOFF_HI", /* name */
913 FALSE, /* partial_inplace */
914 0, /* src_mask */
915 0xffff, /* dst_mask */
916 FALSE), /* pcrel_offset */
918 /* 16-bit upper half adjusted section relative relocation. */
919 HOWTO (R_PPC_SECTOFF_HA, /* type */
920 16, /* rightshift */
921 1, /* size (0 = byte, 1 = short, 2 = long) */
922 16, /* bitsize */
923 FALSE, /* pc_relative */
924 0, /* bitpos */
925 complain_overflow_bitfield, /* complain_on_overflow */
926 ppc_elf_addr16_ha_reloc, /* special_function */
927 "R_PPC_SECTOFF_HA", /* name */
928 FALSE, /* partial_inplace */
929 0, /* src_mask */
930 0xffff, /* dst_mask */
931 FALSE), /* pcrel_offset */
933 /* Marker reloc for TLS. */
934 HOWTO (R_PPC_TLS,
935 0, /* rightshift */
936 2, /* size (0 = byte, 1 = short, 2 = long) */
937 32, /* bitsize */
938 FALSE, /* pc_relative */
939 0, /* bitpos */
940 complain_overflow_dont, /* complain_on_overflow */
941 bfd_elf_generic_reloc, /* special_function */
942 "R_PPC_TLS", /* name */
943 FALSE, /* partial_inplace */
944 0, /* src_mask */
945 0, /* dst_mask */
946 FALSE), /* pcrel_offset */
948 /* Computes the load module index of the load module that contains the
949 definition of its TLS sym. */
950 HOWTO (R_PPC_DTPMOD32,
951 0, /* rightshift */
952 2, /* size (0 = byte, 1 = short, 2 = long) */
953 32, /* bitsize */
954 FALSE, /* pc_relative */
955 0, /* bitpos */
956 complain_overflow_dont, /* complain_on_overflow */
957 ppc_elf_unhandled_reloc, /* special_function */
958 "R_PPC_DTPMOD32", /* name */
959 FALSE, /* partial_inplace */
960 0, /* src_mask */
961 0xffffffff, /* dst_mask */
962 FALSE), /* pcrel_offset */
964 /* Computes a dtv-relative displacement, the difference between the value
965 of sym+add and the base address of the thread-local storage block that
966 contains the definition of sym, minus 0x8000. */
967 HOWTO (R_PPC_DTPREL32,
968 0, /* rightshift */
969 2, /* size (0 = byte, 1 = short, 2 = long) */
970 32, /* bitsize */
971 FALSE, /* pc_relative */
972 0, /* bitpos */
973 complain_overflow_dont, /* complain_on_overflow */
974 ppc_elf_unhandled_reloc, /* special_function */
975 "R_PPC_DTPREL32", /* name */
976 FALSE, /* partial_inplace */
977 0, /* src_mask */
978 0xffffffff, /* dst_mask */
979 FALSE), /* pcrel_offset */
981 /* A 16 bit dtprel reloc. */
982 HOWTO (R_PPC_DTPREL16,
983 0, /* rightshift */
984 1, /* size (0 = byte, 1 = short, 2 = long) */
985 16, /* bitsize */
986 FALSE, /* pc_relative */
987 0, /* bitpos */
988 complain_overflow_signed, /* complain_on_overflow */
989 ppc_elf_unhandled_reloc, /* special_function */
990 "R_PPC_DTPREL16", /* name */
991 FALSE, /* partial_inplace */
992 0, /* src_mask */
993 0xffff, /* dst_mask */
994 FALSE), /* pcrel_offset */
996 /* Like DTPREL16, but no overflow. */
997 HOWTO (R_PPC_DTPREL16_LO,
998 0, /* rightshift */
999 1, /* size (0 = byte, 1 = short, 2 = long) */
1000 16, /* bitsize */
1001 FALSE, /* pc_relative */
1002 0, /* bitpos */
1003 complain_overflow_dont, /* complain_on_overflow */
1004 ppc_elf_unhandled_reloc, /* special_function */
1005 "R_PPC_DTPREL16_LO", /* name */
1006 FALSE, /* partial_inplace */
1007 0, /* src_mask */
1008 0xffff, /* dst_mask */
1009 FALSE), /* pcrel_offset */
1011 /* Like DTPREL16_LO, but next higher group of 16 bits. */
1012 HOWTO (R_PPC_DTPREL16_HI,
1013 16, /* rightshift */
1014 1, /* size (0 = byte, 1 = short, 2 = long) */
1015 16, /* bitsize */
1016 FALSE, /* pc_relative */
1017 0, /* bitpos */
1018 complain_overflow_dont, /* complain_on_overflow */
1019 ppc_elf_unhandled_reloc, /* special_function */
1020 "R_PPC_DTPREL16_HI", /* name */
1021 FALSE, /* partial_inplace */
1022 0, /* src_mask */
1023 0xffff, /* dst_mask */
1024 FALSE), /* pcrel_offset */
1026 /* Like DTPREL16_HI, but adjust for low 16 bits. */
1027 HOWTO (R_PPC_DTPREL16_HA,
1028 16, /* rightshift */
1029 1, /* size (0 = byte, 1 = short, 2 = long) */
1030 16, /* bitsize */
1031 FALSE, /* pc_relative */
1032 0, /* bitpos */
1033 complain_overflow_dont, /* complain_on_overflow */
1034 ppc_elf_unhandled_reloc, /* special_function */
1035 "R_PPC_DTPREL16_HA", /* name */
1036 FALSE, /* partial_inplace */
1037 0, /* src_mask */
1038 0xffff, /* dst_mask */
1039 FALSE), /* pcrel_offset */
1041 /* Computes a tp-relative displacement, the difference between the value of
1042 sym+add and the value of the thread pointer (r13). */
1043 HOWTO (R_PPC_TPREL32,
1044 0, /* rightshift */
1045 2, /* size (0 = byte, 1 = short, 2 = long) */
1046 32, /* bitsize */
1047 FALSE, /* pc_relative */
1048 0, /* bitpos */
1049 complain_overflow_dont, /* complain_on_overflow */
1050 ppc_elf_unhandled_reloc, /* special_function */
1051 "R_PPC_TPREL32", /* name */
1052 FALSE, /* partial_inplace */
1053 0, /* src_mask */
1054 0xffffffff, /* dst_mask */
1055 FALSE), /* pcrel_offset */
1057 /* A 16 bit tprel reloc. */
1058 HOWTO (R_PPC_TPREL16,
1059 0, /* rightshift */
1060 1, /* size (0 = byte, 1 = short, 2 = long) */
1061 16, /* bitsize */
1062 FALSE, /* pc_relative */
1063 0, /* bitpos */
1064 complain_overflow_signed, /* complain_on_overflow */
1065 ppc_elf_unhandled_reloc, /* special_function */
1066 "R_PPC_TPREL16", /* name */
1067 FALSE, /* partial_inplace */
1068 0, /* src_mask */
1069 0xffff, /* dst_mask */
1070 FALSE), /* pcrel_offset */
1072 /* Like TPREL16, but no overflow. */
1073 HOWTO (R_PPC_TPREL16_LO,
1074 0, /* rightshift */
1075 1, /* size (0 = byte, 1 = short, 2 = long) */
1076 16, /* bitsize */
1077 FALSE, /* pc_relative */
1078 0, /* bitpos */
1079 complain_overflow_dont, /* complain_on_overflow */
1080 ppc_elf_unhandled_reloc, /* special_function */
1081 "R_PPC_TPREL16_LO", /* name */
1082 FALSE, /* partial_inplace */
1083 0, /* src_mask */
1084 0xffff, /* dst_mask */
1085 FALSE), /* pcrel_offset */
1087 /* Like TPREL16_LO, but next higher group of 16 bits. */
1088 HOWTO (R_PPC_TPREL16_HI,
1089 16, /* rightshift */
1090 1, /* size (0 = byte, 1 = short, 2 = long) */
1091 16, /* bitsize */
1092 FALSE, /* pc_relative */
1093 0, /* bitpos */
1094 complain_overflow_dont, /* complain_on_overflow */
1095 ppc_elf_unhandled_reloc, /* special_function */
1096 "R_PPC_TPREL16_HI", /* name */
1097 FALSE, /* partial_inplace */
1098 0, /* src_mask */
1099 0xffff, /* dst_mask */
1100 FALSE), /* pcrel_offset */
1102 /* Like TPREL16_HI, but adjust for low 16 bits. */
1103 HOWTO (R_PPC_TPREL16_HA,
1104 16, /* rightshift */
1105 1, /* size (0 = byte, 1 = short, 2 = long) */
1106 16, /* bitsize */
1107 FALSE, /* pc_relative */
1108 0, /* bitpos */
1109 complain_overflow_dont, /* complain_on_overflow */
1110 ppc_elf_unhandled_reloc, /* special_function */
1111 "R_PPC_TPREL16_HA", /* name */
1112 FALSE, /* partial_inplace */
1113 0, /* src_mask */
1114 0xffff, /* dst_mask */
1115 FALSE), /* pcrel_offset */
1117 /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
1118 with values (sym+add)@dtpmod and (sym+add)@dtprel, and computes the offset
1119 to the first entry. */
1120 HOWTO (R_PPC_GOT_TLSGD16,
1121 0, /* rightshift */
1122 1, /* size (0 = byte, 1 = short, 2 = long) */
1123 16, /* bitsize */
1124 FALSE, /* pc_relative */
1125 0, /* bitpos */
1126 complain_overflow_signed, /* complain_on_overflow */
1127 ppc_elf_unhandled_reloc, /* special_function */
1128 "R_PPC_GOT_TLSGD16", /* name */
1129 FALSE, /* partial_inplace */
1130 0, /* src_mask */
1131 0xffff, /* dst_mask */
1132 FALSE), /* pcrel_offset */
1134 /* Like GOT_TLSGD16, but no overflow. */
1135 HOWTO (R_PPC_GOT_TLSGD16_LO,
1136 0, /* rightshift */
1137 1, /* size (0 = byte, 1 = short, 2 = long) */
1138 16, /* bitsize */
1139 FALSE, /* pc_relative */
1140 0, /* bitpos */
1141 complain_overflow_dont, /* complain_on_overflow */
1142 ppc_elf_unhandled_reloc, /* special_function */
1143 "R_PPC_GOT_TLSGD16_LO", /* name */
1144 FALSE, /* partial_inplace */
1145 0, /* src_mask */
1146 0xffff, /* dst_mask */
1147 FALSE), /* pcrel_offset */
1149 /* Like GOT_TLSGD16_LO, but next higher group of 16 bits. */
1150 HOWTO (R_PPC_GOT_TLSGD16_HI,
1151 16, /* rightshift */
1152 1, /* size (0 = byte, 1 = short, 2 = long) */
1153 16, /* bitsize */
1154 FALSE, /* pc_relative */
1155 0, /* bitpos */
1156 complain_overflow_dont, /* complain_on_overflow */
1157 ppc_elf_unhandled_reloc, /* special_function */
1158 "R_PPC_GOT_TLSGD16_HI", /* name */
1159 FALSE, /* partial_inplace */
1160 0, /* src_mask */
1161 0xffff, /* dst_mask */
1162 FALSE), /* pcrel_offset */
1164 /* Like GOT_TLSGD16_HI, but adjust for low 16 bits. */
1165 HOWTO (R_PPC_GOT_TLSGD16_HA,
1166 16, /* rightshift */
1167 1, /* size (0 = byte, 1 = short, 2 = long) */
1168 16, /* bitsize */
1169 FALSE, /* pc_relative */
1170 0, /* bitpos */
1171 complain_overflow_dont, /* complain_on_overflow */
1172 ppc_elf_unhandled_reloc, /* special_function */
1173 "R_PPC_GOT_TLSGD16_HA", /* name */
1174 FALSE, /* partial_inplace */
1175 0, /* src_mask */
1176 0xffff, /* dst_mask */
1177 FALSE), /* pcrel_offset */
1179 /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
1180 with values (sym+add)@dtpmod and zero, and computes the offset to the
1181 first entry. */
1182 HOWTO (R_PPC_GOT_TLSLD16,
1183 0, /* rightshift */
1184 1, /* size (0 = byte, 1 = short, 2 = long) */
1185 16, /* bitsize */
1186 FALSE, /* pc_relative */
1187 0, /* bitpos */
1188 complain_overflow_signed, /* complain_on_overflow */
1189 ppc_elf_unhandled_reloc, /* special_function */
1190 "R_PPC_GOT_TLSLD16", /* name */
1191 FALSE, /* partial_inplace */
1192 0, /* src_mask */
1193 0xffff, /* dst_mask */
1194 FALSE), /* pcrel_offset */
1196 /* Like GOT_TLSLD16, but no overflow. */
1197 HOWTO (R_PPC_GOT_TLSLD16_LO,
1198 0, /* rightshift */
1199 1, /* size (0 = byte, 1 = short, 2 = long) */
1200 16, /* bitsize */
1201 FALSE, /* pc_relative */
1202 0, /* bitpos */
1203 complain_overflow_dont, /* complain_on_overflow */
1204 ppc_elf_unhandled_reloc, /* special_function */
1205 "R_PPC_GOT_TLSLD16_LO", /* name */
1206 FALSE, /* partial_inplace */
1207 0, /* src_mask */
1208 0xffff, /* dst_mask */
1209 FALSE), /* pcrel_offset */
1211 /* Like GOT_TLSLD16_LO, but next higher group of 16 bits. */
1212 HOWTO (R_PPC_GOT_TLSLD16_HI,
1213 16, /* rightshift */
1214 1, /* size (0 = byte, 1 = short, 2 = long) */
1215 16, /* bitsize */
1216 FALSE, /* pc_relative */
1217 0, /* bitpos */
1218 complain_overflow_dont, /* complain_on_overflow */
1219 ppc_elf_unhandled_reloc, /* special_function */
1220 "R_PPC_GOT_TLSLD16_HI", /* name */
1221 FALSE, /* partial_inplace */
1222 0, /* src_mask */
1223 0xffff, /* dst_mask */
1224 FALSE), /* pcrel_offset */
1226 /* Like GOT_TLSLD16_HI, but adjust for low 16 bits. */
1227 HOWTO (R_PPC_GOT_TLSLD16_HA,
1228 16, /* rightshift */
1229 1, /* size (0 = byte, 1 = short, 2 = long) */
1230 16, /* bitsize */
1231 FALSE, /* pc_relative */
1232 0, /* bitpos */
1233 complain_overflow_dont, /* complain_on_overflow */
1234 ppc_elf_unhandled_reloc, /* special_function */
1235 "R_PPC_GOT_TLSLD16_HA", /* name */
1236 FALSE, /* partial_inplace */
1237 0, /* src_mask */
1238 0xffff, /* dst_mask */
1239 FALSE), /* pcrel_offset */
1241 /* Allocates an entry in the GOT with value (sym+add)@dtprel, and computes
1242 the offset to the entry. */
1243 HOWTO (R_PPC_GOT_DTPREL16,
1244 0, /* rightshift */
1245 1, /* size (0 = byte, 1 = short, 2 = long) */
1246 16, /* bitsize */
1247 FALSE, /* pc_relative */
1248 0, /* bitpos */
1249 complain_overflow_signed, /* complain_on_overflow */
1250 ppc_elf_unhandled_reloc, /* special_function */
1251 "R_PPC_GOT_DTPREL16", /* name */
1252 FALSE, /* partial_inplace */
1253 0, /* src_mask */
1254 0xffff, /* dst_mask */
1255 FALSE), /* pcrel_offset */
1257 /* Like GOT_DTPREL16, but no overflow. */
1258 HOWTO (R_PPC_GOT_DTPREL16_LO,
1259 0, /* rightshift */
1260 1, /* size (0 = byte, 1 = short, 2 = long) */
1261 16, /* bitsize */
1262 FALSE, /* pc_relative */
1263 0, /* bitpos */
1264 complain_overflow_dont, /* complain_on_overflow */
1265 ppc_elf_unhandled_reloc, /* special_function */
1266 "R_PPC_GOT_DTPREL16_LO", /* name */
1267 FALSE, /* partial_inplace */
1268 0, /* src_mask */
1269 0xffff, /* dst_mask */
1270 FALSE), /* pcrel_offset */
1272 /* Like GOT_DTPREL16_LO, but next higher group of 16 bits. */
1273 HOWTO (R_PPC_GOT_DTPREL16_HI,
1274 16, /* rightshift */
1275 1, /* size (0 = byte, 1 = short, 2 = long) */
1276 16, /* bitsize */
1277 FALSE, /* pc_relative */
1278 0, /* bitpos */
1279 complain_overflow_dont, /* complain_on_overflow */
1280 ppc_elf_unhandled_reloc, /* special_function */
1281 "R_PPC_GOT_DTPREL16_HI", /* name */
1282 FALSE, /* partial_inplace */
1283 0, /* src_mask */
1284 0xffff, /* dst_mask */
1285 FALSE), /* pcrel_offset */
1287 /* Like GOT_DTPREL16_HI, but adjust for low 16 bits. */
1288 HOWTO (R_PPC_GOT_DTPREL16_HA,
1289 16, /* rightshift */
1290 1, /* size (0 = byte, 1 = short, 2 = long) */
1291 16, /* bitsize */
1292 FALSE, /* pc_relative */
1293 0, /* bitpos */
1294 complain_overflow_dont, /* complain_on_overflow */
1295 ppc_elf_unhandled_reloc, /* special_function */
1296 "R_PPC_GOT_DTPREL16_HA", /* name */
1297 FALSE, /* partial_inplace */
1298 0, /* src_mask */
1299 0xffff, /* dst_mask */
1300 FALSE), /* pcrel_offset */
1302 /* Allocates an entry in the GOT with value (sym+add)@tprel, and computes the
1303 offset to the entry. */
1304 HOWTO (R_PPC_GOT_TPREL16,
1305 0, /* rightshift */
1306 1, /* size (0 = byte, 1 = short, 2 = long) */
1307 16, /* bitsize */
1308 FALSE, /* pc_relative */
1309 0, /* bitpos */
1310 complain_overflow_signed, /* complain_on_overflow */
1311 ppc_elf_unhandled_reloc, /* special_function */
1312 "R_PPC_GOT_TPREL16", /* name */
1313 FALSE, /* partial_inplace */
1314 0, /* src_mask */
1315 0xffff, /* dst_mask */
1316 FALSE), /* pcrel_offset */
1318 /* Like GOT_TPREL16, but no overflow. */
1319 HOWTO (R_PPC_GOT_TPREL16_LO,
1320 0, /* rightshift */
1321 1, /* size (0 = byte, 1 = short, 2 = long) */
1322 16, /* bitsize */
1323 FALSE, /* pc_relative */
1324 0, /* bitpos */
1325 complain_overflow_dont, /* complain_on_overflow */
1326 ppc_elf_unhandled_reloc, /* special_function */
1327 "R_PPC_GOT_TPREL16_LO", /* name */
1328 FALSE, /* partial_inplace */
1329 0, /* src_mask */
1330 0xffff, /* dst_mask */
1331 FALSE), /* pcrel_offset */
1333 /* Like GOT_TPREL16_LO, but next higher group of 16 bits. */
1334 HOWTO (R_PPC_GOT_TPREL16_HI,
1335 16, /* rightshift */
1336 1, /* size (0 = byte, 1 = short, 2 = long) */
1337 16, /* bitsize */
1338 FALSE, /* pc_relative */
1339 0, /* bitpos */
1340 complain_overflow_dont, /* complain_on_overflow */
1341 ppc_elf_unhandled_reloc, /* special_function */
1342 "R_PPC_GOT_TPREL16_HI", /* name */
1343 FALSE, /* partial_inplace */
1344 0, /* src_mask */
1345 0xffff, /* dst_mask */
1346 FALSE), /* pcrel_offset */
1348 /* Like GOT_TPREL16_HI, but adjust for low 16 bits. */
1349 HOWTO (R_PPC_GOT_TPREL16_HA,
1350 16, /* rightshift */
1351 1, /* size (0 = byte, 1 = short, 2 = long) */
1352 16, /* bitsize */
1353 FALSE, /* pc_relative */
1354 0, /* bitpos */
1355 complain_overflow_dont, /* complain_on_overflow */
1356 ppc_elf_unhandled_reloc, /* special_function */
1357 "R_PPC_GOT_TPREL16_HA", /* name */
1358 FALSE, /* partial_inplace */
1359 0, /* src_mask */
1360 0xffff, /* dst_mask */
1361 FALSE), /* pcrel_offset */
1363 /* The remaining relocs are from the Embedded ELF ABI, and are not
1364 in the SVR4 ELF ABI. */
1366 /* 32 bit value resulting from the addend minus the symbol. */
1367 HOWTO (R_PPC_EMB_NADDR32, /* type */
1368 0, /* rightshift */
1369 2, /* size (0 = byte, 1 = short, 2 = long) */
1370 32, /* bitsize */
1371 FALSE, /* pc_relative */
1372 0, /* bitpos */
1373 complain_overflow_bitfield, /* complain_on_overflow */
1374 bfd_elf_generic_reloc, /* special_function */
1375 "R_PPC_EMB_NADDR32", /* name */
1376 FALSE, /* partial_inplace */
1377 0, /* src_mask */
1378 0xffffffff, /* dst_mask */
1379 FALSE), /* pcrel_offset */
1381 /* 16 bit value resulting from the addend minus the symbol. */
1382 HOWTO (R_PPC_EMB_NADDR16, /* type */
1383 0, /* rightshift */
1384 1, /* size (0 = byte, 1 = short, 2 = long) */
1385 16, /* bitsize */
1386 FALSE, /* pc_relative */
1387 0, /* bitpos */
1388 complain_overflow_bitfield, /* complain_on_overflow */
1389 bfd_elf_generic_reloc, /* special_function */
1390 "R_PPC_EMB_NADDR16", /* name */
1391 FALSE, /* partial_inplace */
1392 0, /* src_mask */
1393 0xffff, /* dst_mask */
1394 FALSE), /* pcrel_offset */
1396 /* 16 bit value resulting from the addend minus the symbol. */
1397 HOWTO (R_PPC_EMB_NADDR16_LO, /* type */
1398 0, /* rightshift */
1399 1, /* size (0 = byte, 1 = short, 2 = long) */
1400 16, /* bitsize */
1401 FALSE, /* pc_relative */
1402 0, /* bitpos */
1403 complain_overflow_dont,/* complain_on_overflow */
1404 bfd_elf_generic_reloc, /* special_function */
1405 "R_PPC_EMB_ADDR16_LO", /* name */
1406 FALSE, /* partial_inplace */
1407 0, /* src_mask */
1408 0xffff, /* dst_mask */
1409 FALSE), /* pcrel_offset */
1411 /* The high order 16 bits of the addend minus the symbol. */
1412 HOWTO (R_PPC_EMB_NADDR16_HI, /* type */
1413 16, /* rightshift */
1414 1, /* size (0 = byte, 1 = short, 2 = long) */
1415 16, /* bitsize */
1416 FALSE, /* pc_relative */
1417 0, /* bitpos */
1418 complain_overflow_dont, /* complain_on_overflow */
1419 bfd_elf_generic_reloc, /* special_function */
1420 "R_PPC_EMB_NADDR16_HI", /* name */
1421 FALSE, /* partial_inplace */
1422 0, /* src_mask */
1423 0xffff, /* dst_mask */
1424 FALSE), /* pcrel_offset */
1426 /* The high order 16 bits of the result of the addend minus the address,
1427 plus 1 if the contents of the low 16 bits, treated as a signed number,
1428 is negative. */
1429 HOWTO (R_PPC_EMB_NADDR16_HA, /* type */
1430 16, /* rightshift */
1431 1, /* size (0 = byte, 1 = short, 2 = long) */
1432 16, /* bitsize */
1433 FALSE, /* pc_relative */
1434 0, /* bitpos */
1435 complain_overflow_dont, /* complain_on_overflow */
1436 ppc_elf_addr16_ha_reloc, /* special_function */
1437 "R_PPC_EMB_NADDR16_HA", /* name */
1438 FALSE, /* partial_inplace */
1439 0, /* src_mask */
1440 0xffff, /* dst_mask */
1441 FALSE), /* pcrel_offset */
1443 /* 16 bit value resulting from allocating a 4 byte word to hold an
1444 address in the .sdata section, and returning the offset from
1445 _SDA_BASE_ for that relocation. */
1446 HOWTO (R_PPC_EMB_SDAI16, /* type */
1447 0, /* rightshift */
1448 1, /* size (0 = byte, 1 = short, 2 = long) */
1449 16, /* bitsize */
1450 FALSE, /* pc_relative */
1451 0, /* bitpos */
1452 complain_overflow_bitfield, /* complain_on_overflow */
1453 bfd_elf_generic_reloc, /* special_function */
1454 "R_PPC_EMB_SDAI16", /* name */
1455 FALSE, /* partial_inplace */
1456 0, /* src_mask */
1457 0xffff, /* dst_mask */
1458 FALSE), /* pcrel_offset */
1460 /* 16 bit value resulting from allocating a 4 byte word to hold an
1461 address in the .sdata2 section, and returning the offset from
1462 _SDA2_BASE_ for that relocation. */
1463 HOWTO (R_PPC_EMB_SDA2I16, /* type */
1464 0, /* rightshift */
1465 1, /* size (0 = byte, 1 = short, 2 = long) */
1466 16, /* bitsize */
1467 FALSE, /* pc_relative */
1468 0, /* bitpos */
1469 complain_overflow_bitfield, /* complain_on_overflow */
1470 bfd_elf_generic_reloc, /* special_function */
1471 "R_PPC_EMB_SDA2I16", /* name */
1472 FALSE, /* partial_inplace */
1473 0, /* src_mask */
1474 0xffff, /* dst_mask */
1475 FALSE), /* pcrel_offset */
1477 /* A sign-extended 16 bit value relative to _SDA2_BASE_, for use with
1478 small data items. */
1479 HOWTO (R_PPC_EMB_SDA2REL, /* type */
1480 0, /* rightshift */
1481 1, /* size (0 = byte, 1 = short, 2 = long) */
1482 16, /* bitsize */
1483 FALSE, /* pc_relative */
1484 0, /* bitpos */
1485 complain_overflow_signed, /* complain_on_overflow */
1486 bfd_elf_generic_reloc, /* special_function */
1487 "R_PPC_EMB_SDA2REL", /* name */
1488 FALSE, /* partial_inplace */
1489 0, /* src_mask */
1490 0xffff, /* dst_mask */
1491 FALSE), /* pcrel_offset */
1493 /* Relocate against either _SDA_BASE_ or _SDA2_BASE_, filling in the 16 bit
1494 signed offset from the appropriate base, and filling in the register
1495 field with the appropriate register (0, 2, or 13). */
1496 HOWTO (R_PPC_EMB_SDA21, /* type */
1497 0, /* rightshift */
1498 2, /* size (0 = byte, 1 = short, 2 = long) */
1499 16, /* bitsize */
1500 FALSE, /* pc_relative */
1501 0, /* bitpos */
1502 complain_overflow_signed, /* complain_on_overflow */
1503 bfd_elf_generic_reloc, /* special_function */
1504 "R_PPC_EMB_SDA21", /* name */
1505 FALSE, /* partial_inplace */
1506 0, /* src_mask */
1507 0xffff, /* dst_mask */
1508 FALSE), /* pcrel_offset */
1510 /* Relocation not handled: R_PPC_EMB_MRKREF */
1511 /* Relocation not handled: R_PPC_EMB_RELSEC16 */
1512 /* Relocation not handled: R_PPC_EMB_RELST_LO */
1513 /* Relocation not handled: R_PPC_EMB_RELST_HI */
1514 /* Relocation not handled: R_PPC_EMB_RELST_HA */
1515 /* Relocation not handled: R_PPC_EMB_BIT_FLD */
1517 /* PC relative relocation against either _SDA_BASE_ or _SDA2_BASE_, filling
1518 in the 16 bit signed offset from the appropriate base, and filling in the
1519 register field with the appropriate register (0, 2, or 13). */
1520 HOWTO (R_PPC_EMB_RELSDA, /* type */
1521 0, /* rightshift */
1522 1, /* size (0 = byte, 1 = short, 2 = long) */
1523 16, /* bitsize */
1524 TRUE, /* pc_relative */
1525 0, /* bitpos */
1526 complain_overflow_signed, /* complain_on_overflow */
1527 bfd_elf_generic_reloc, /* special_function */
1528 "R_PPC_EMB_RELSDA", /* name */
1529 FALSE, /* partial_inplace */
1530 0, /* src_mask */
1531 0xffff, /* dst_mask */
1532 FALSE), /* pcrel_offset */
1534 /* Phony reloc to handle branch stubs. */
1535 HOWTO (R_PPC_RELAX32, /* type */
1536 0, /* rightshift */
1537 0, /* size */
1538 0, /* bitsize */
1539 FALSE, /* pc_relative */
1540 0, /* bitpos */
1541 complain_overflow_dont, /* complain_on_overflow */
1542 bfd_elf_generic_reloc, /* special_function */
1543 "R_PPC_RELAX32", /* name */
1544 FALSE, /* partial_inplace */
1545 0, /* src_mask */
1546 0, /* dst_mask */
1547 FALSE), /* pcrel_offset */
1549 /* GNU extension to record C++ vtable hierarchy. */
1550 HOWTO (R_PPC_GNU_VTINHERIT, /* type */
1551 0, /* rightshift */
1552 0, /* size (0 = byte, 1 = short, 2 = long) */
1553 0, /* bitsize */
1554 FALSE, /* pc_relative */
1555 0, /* bitpos */
1556 complain_overflow_dont, /* complain_on_overflow */
1557 NULL, /* special_function */
1558 "R_PPC_GNU_VTINHERIT", /* name */
1559 FALSE, /* partial_inplace */
1560 0, /* src_mask */
1561 0, /* dst_mask */
1562 FALSE), /* pcrel_offset */
1564 /* GNU extension to record C++ vtable member usage. */
1565 HOWTO (R_PPC_GNU_VTENTRY, /* 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_VTENTRY", /* name */
1574 FALSE, /* partial_inplace */
1575 0, /* src_mask */
1576 0, /* dst_mask */
1577 FALSE), /* pcrel_offset */
1579 /* Phony reloc to handle AIX style TOC entries. */
1580 HOWTO (R_PPC_TOC16, /* type */
1581 0, /* rightshift */
1582 1, /* size (0 = byte, 1 = short, 2 = long) */
1583 16, /* bitsize */
1584 FALSE, /* pc_relative */
1585 0, /* bitpos */
1586 complain_overflow_signed, /* complain_on_overflow */
1587 bfd_elf_generic_reloc, /* special_function */
1588 "R_PPC_TOC16", /* name */
1589 FALSE, /* partial_inplace */
1590 0, /* src_mask */
1591 0xffff, /* dst_mask */
1592 FALSE), /* pcrel_offset */
1595 /* Initialize the ppc_elf_howto_table, so that linear accesses can be done. */
1597 static void
1598 ppc_elf_howto_init (void)
1600 unsigned int i, type;
1602 for (i = 0;
1603 i < sizeof (ppc_elf_howto_raw) / sizeof (ppc_elf_howto_raw[0]);
1604 i++)
1606 type = ppc_elf_howto_raw[i].type;
1607 if (type >= (sizeof (ppc_elf_howto_table)
1608 / sizeof (ppc_elf_howto_table[0])))
1609 abort ();
1610 ppc_elf_howto_table[type] = &ppc_elf_howto_raw[i];
1614 static bfd_reloc_status_type
1615 ppc_elf_install_value (bfd *abfd,
1616 bfd_byte *hit_addr,
1617 bfd_vma v,
1618 unsigned int r_type)
1620 bfd_vma t0, t1;
1621 #ifdef BFD_HOST_U_64_BIT
1622 BFD_HOST_U_64_BIT val = (BFD_HOST_U_64_BIT) v;
1623 #else
1624 bfd_vma val = v;
1625 #endif
1627 switch (r_type)
1629 case R_PPC_RELAX32:
1630 /* Do stuff here. */
1631 t0 = bfd_get_32 (abfd, hit_addr);
1632 t1 = bfd_get_32 (abfd, hit_addr + 4);
1634 /* We're clearing the bits for R_PPC_ADDR16_HA
1635 and R_PPC_ADDR16_LO here. */
1636 t0 &= ~0xffff;
1637 t1 &= ~0xffff;
1639 /* t0 is HA, t1 is lo */
1640 t0 |= ((val + 0x8000) >> 16) & 0xffff;
1641 t1 |= val & 0xffff;
1643 bfd_put_32 (abfd, t0, hit_addr);
1644 bfd_put_32 (abfd, t1, hit_addr + 4);
1645 break;
1647 case R_PPC_REL24:
1648 t0 = bfd_get_32 (abfd, hit_addr);
1649 t0 &= ~0x3fffffc;
1650 t0 |= val & 0x3fffffc;
1651 bfd_put_32 (abfd, t0, hit_addr);
1652 break;
1654 case R_PPC_REL14:
1655 case R_PPC_REL14_BRTAKEN:
1656 case R_PPC_REL14_BRNTAKEN:
1657 t0 = bfd_get_32 (abfd, hit_addr);
1658 t0 &= ~0xfffc;
1659 t0 |= val & 0xfffc;
1660 bfd_put_32 (abfd, t0, hit_addr);
1661 break;
1663 case R_PPC_LOCAL24PC:
1664 case R_PPC_PLTREL24:
1665 t0 = bfd_get_32 (abfd, hit_addr);
1666 t0 &= ~0x3fffffc;
1667 t0 |= val & 0x3fffffc;
1668 bfd_put_32 (abfd, t0, hit_addr);
1669 break;
1671 default:
1672 return bfd_reloc_notsupported;
1675 return bfd_reloc_ok;
1678 static const bfd_byte shared_stub_entry[] =
1680 0x48, 0x00, 0x00, 0x24, /* b .+36 */
1681 0x7c, 0x08, 0x02, 0xa6, /* mflr 0 */
1682 0x42, 0x9f, 0x00, 0x05, /* bcl 20, 31, .Lxxx */
1683 0x7d, 0x68, 0x02, 0xa6, /* mflr 11 */
1684 0x3d, 0x60, 0x00, 0x00, /* addis 11, 11, (xxx-.Lxxx)@ha */
1685 0x39, 0x6b, 0x00, 0x18, /* addi 11, 11, (xxx-.Lxxx)@l */
1686 0x7c, 0x08, 0x03, 0xa6, /* mtlr 0 */
1687 0x7d, 0x69, 0x03, 0xa6, /* mtctr 11 */
1688 0x4e, 0x80, 0x04, 0x20, /* bctr */
1691 static const bfd_byte stub_entry[] =
1693 0x48, 0x00, 0x00, 0x14, /* b .+20 */
1694 0x3d, 0x60, 0x00, 0x00, /* lis 11,xxx@ha */
1695 0x39, 0x6b, 0x00, 0x00, /* addi 11,11,xxx@l */
1696 0x7d, 0x69, 0x03, 0xa6, /* mtctr 11 */
1697 0x4e, 0x80, 0x04, 0x20, /* bctr */
1701 static bfd_boolean
1702 ppc_elf_relax_section (bfd *abfd,
1703 asection *isec,
1704 struct bfd_link_info *link_info,
1705 bfd_boolean *again)
1707 struct one_fixup
1709 struct one_fixup *next;
1710 asection *tsec;
1711 bfd_vma toff;
1712 bfd_vma trampoff;
1715 Elf_Internal_Shdr *symtab_hdr;
1716 bfd_byte *contents = NULL;
1717 Elf_Internal_Sym *isymbuf = NULL;
1718 Elf_Internal_Rela *internal_relocs = NULL;
1719 Elf_Internal_Rela *irel, *irelend;
1720 struct one_fixup *fixups = NULL;
1721 bfd_boolean changed_contents = FALSE;
1722 bfd_boolean changed_relocs = FALSE;
1723 struct ppc_elf_link_hash_table *ppc_info;
1725 *again = FALSE;
1727 /* Nothing to do if there are no relocations and no need for
1728 the relax finalize pass. */
1729 if ((isec->flags & SEC_RELOC) == 0
1730 || isec->reloc_count == 0
1731 || link_info->relax_finalizing)
1732 return TRUE;
1734 /* If needed, initialize this section's cooked size. */
1735 if (isec->_cooked_size == 0)
1736 isec->_cooked_size = isec->_raw_size;
1738 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1740 /* Get a copy of the native relocations. */
1741 internal_relocs = _bfd_elf_link_read_relocs (abfd, isec, NULL, NULL,
1742 link_info->keep_memory);
1743 if (internal_relocs == NULL)
1744 goto error_return;
1746 ppc_info = ppc_elf_hash_table (link_info);
1747 irelend = internal_relocs + isec->reloc_count;
1749 /* Get the section contents. */
1750 /* Get cached copy if it exists. */
1751 if (elf_section_data (isec)->this_hdr.contents != NULL)
1752 contents = elf_section_data (isec)->this_hdr.contents;
1753 else
1755 /* Go get them off disk. */
1756 contents = bfd_malloc (isec->_raw_size);
1757 if (contents == NULL)
1758 goto error_return;
1760 if (!bfd_get_section_contents (abfd, isec, contents, 0, isec->_raw_size))
1761 goto error_return;
1764 for (irel = internal_relocs; irel < irelend; irel++)
1766 unsigned long r_type = ELF32_R_TYPE (irel->r_info);
1767 bfd_vma symaddr, reladdr, trampoff, toff, roff;
1768 asection *tsec;
1769 bfd_size_type amt;
1770 struct one_fixup *f;
1771 size_t insn_offset = 0;
1772 bfd_vma max_branch_offset;
1774 switch (r_type)
1776 case R_PPC_REL24:
1777 case R_PPC_LOCAL24PC:
1778 case R_PPC_REL14:
1779 case R_PPC_REL14_BRTAKEN:
1780 case R_PPC_REL14_BRNTAKEN:
1781 case R_PPC_PLTREL24:
1782 break;
1784 default:
1785 continue;
1788 /* Get the value of the symbol referred to by the reloc. */
1789 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
1791 /* A local symbol. */
1792 Elf_Internal_Sym *isym;
1794 /* Read this BFD's local symbols. */
1795 if (isymbuf == NULL)
1797 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
1798 if (isymbuf == NULL)
1799 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
1800 symtab_hdr->sh_info, 0,
1801 NULL, NULL, NULL);
1802 if (isymbuf == 0)
1803 goto error_return;
1805 isym = isymbuf + ELF32_R_SYM (irel->r_info);
1806 if (isym->st_shndx == SHN_UNDEF)
1807 continue; /* We can't do anthing with undefined symbols. */
1808 else if (isym->st_shndx == SHN_ABS)
1809 tsec = bfd_abs_section_ptr;
1810 else if (isym->st_shndx == SHN_COMMON)
1811 tsec = bfd_com_section_ptr;
1812 else
1813 tsec = bfd_section_from_elf_index (abfd, isym->st_shndx);
1815 toff = isym->st_value;
1817 else
1819 /* Need dynamic symbol handling. */
1820 unsigned long indx;
1821 struct elf_link_hash_entry *h;
1823 indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
1824 h = elf_sym_hashes (abfd)[indx];
1826 while (h->root.type == bfd_link_hash_indirect
1827 || h->root.type == bfd_link_hash_warning)
1828 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1830 if (r_type == R_PPC_PLTREL24)
1832 Elf_Internal_Sym *isym;
1834 if (h->plt.offset == (bfd_vma) -1
1835 || ppc_info->plt == NULL)
1838 /* Read this BFD's local symbols. */
1839 if (isymbuf == NULL)
1841 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
1842 if (isymbuf == NULL)
1843 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
1844 symtab_hdr->sh_info, 0,
1845 NULL, NULL, NULL);
1846 if (isymbuf == 0)
1847 goto error_return;
1849 isym = isymbuf + ELF32_R_SYM (irel->r_info);
1851 if (isym->st_shndx == SHN_UNDEF)
1852 /* We can't do anthing with undefined symbols. */
1853 continue;
1854 else if (isym->st_shndx == SHN_ABS)
1855 tsec = bfd_abs_section_ptr;
1856 else if (isym->st_shndx == SHN_COMMON)
1857 tsec = bfd_com_section_ptr;
1858 else
1859 tsec = h->root.u.def.section;
1861 toff = h->root.u.def.value;
1863 else
1865 tsec = ppc_info->plt;
1866 toff = h->plt.offset;
1869 else if (h->root.type == bfd_link_hash_undefined
1870 || h->root.type == bfd_link_hash_undefweak)
1871 continue;
1873 else
1875 tsec = h->root.u.def.section;
1876 toff = h->root.u.def.value;
1880 if (tsec->sec_info_type == ELF_INFO_TYPE_MERGE)
1881 toff = _bfd_merged_section_offset (abfd, &tsec,
1882 elf_section_data (tsec)->sec_info,
1883 toff + irel->r_addend, 0);
1884 else
1885 toff += irel->r_addend;
1887 symaddr = tsec->output_section->vma + tsec->output_offset + toff;
1889 roff = irel->r_offset;
1891 reladdr = (isec->output_section->vma
1892 + isec->output_offset
1893 + roff) & (bfd_vma) -4;
1895 /* If the branch is in range, no need to do anything. */
1896 max_branch_offset = 1 << 25;
1897 if (r_type != R_PPC_REL24
1898 && r_type != R_PPC_LOCAL24PC
1899 && r_type != R_PPC_PLTREL24)
1900 max_branch_offset = 1 << 15;
1902 if ((bfd_vma) (symaddr - reladdr) + max_branch_offset
1903 <= 2 * max_branch_offset)
1904 continue;
1906 /* If the branch and target are in the same section, you have
1907 no hope. We'll error out later. */
1908 if (tsec == isec)
1909 continue;
1911 /* Look for an existing fixup to this address. */
1912 for (f = fixups; f ; f = f->next)
1913 if (f->tsec == tsec && f->toff == toff)
1914 break;
1916 if (f == NULL)
1918 size_t size;
1920 if (link_info->shared
1921 || tsec == ppc_info->plt
1922 || r_type == R_PPC_LOCAL24PC)
1924 size = sizeof (shared_stub_entry);
1925 insn_offset = 16;
1927 else
1929 size = sizeof (stub_entry);
1930 insn_offset = 4;
1933 /* Resize the current section to make room for the new branch. */
1934 trampoff = (isec->_cooked_size + 3) & (bfd_vma) - 4;
1935 amt = trampoff + size;
1936 contents = bfd_realloc (contents, amt);
1937 if (contents == NULL)
1938 abort ();
1940 isec->_cooked_size = amt;
1942 if (link_info->shared
1943 || tsec == ppc_info->plt
1944 || r_type == R_PPC_LOCAL24PC)
1945 memcpy (contents + trampoff, shared_stub_entry, size);
1946 else
1947 memcpy (contents + trampoff, stub_entry, size);
1949 /* Hijack the old relocation. Since we need two
1950 relocations for this use a "composite" reloc. */
1951 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
1952 R_PPC_RELAX32);
1953 irel->r_offset = trampoff + insn_offset;
1955 /* Record the fixup so we don't do it again this section. */
1956 f = bfd_malloc (sizeof (*f));
1957 f->next = fixups;
1958 f->tsec = tsec;
1959 f->toff = toff;
1960 f->trampoff = trampoff;
1961 fixups = f;
1963 else
1965 /* Nop out the reloc, since we're finalizing things here. */
1966 irel->r_info = ELF32_R_INFO (0, R_PPC_NONE);
1969 /* Fix up the existing branch to hit the trampoline. Hope like
1970 hell this doesn't overflow too. */
1971 if (ppc_elf_install_value (abfd, contents + roff,
1972 f->trampoff - (roff & (bfd_vma) -3) + 4,
1973 r_type) != bfd_reloc_ok)
1974 abort ();
1976 changed_contents = TRUE;
1977 changed_relocs = TRUE;
1980 /* Clean up. */
1981 while (fixups)
1983 struct one_fixup *f = fixups;
1984 fixups = fixups->next;
1985 free (f);
1987 if (isymbuf != NULL
1988 && symtab_hdr->contents != (unsigned char *) isymbuf)
1990 if (! link_info->keep_memory)
1991 free (isymbuf);
1992 else
1994 /* Cache the symbols for elf_link_input_bfd. */
1995 symtab_hdr->contents = (unsigned char *) isymbuf;
1999 if (contents != NULL
2000 && elf_section_data (isec)->this_hdr.contents != contents)
2002 if (!changed_contents && !link_info->keep_memory)
2003 free (contents);
2004 else
2006 /* Cache the section contents for elf_link_input_bfd. */
2007 elf_section_data (isec)->this_hdr.contents = contents;
2011 if (elf_section_data (isec)->relocs != internal_relocs)
2013 if (!changed_relocs)
2014 free (internal_relocs);
2015 else
2016 elf_section_data (isec)->relocs = internal_relocs;
2019 *again = changed_contents || changed_relocs;
2020 return TRUE;
2022 error_return:
2023 if (isymbuf != NULL && (unsigned char *) isymbuf != symtab_hdr->contents)
2024 free (isymbuf);
2025 if (contents != NULL
2026 && elf_section_data (isec)->this_hdr.contents != contents)
2027 free (contents);
2028 if (internal_relocs != NULL
2029 && elf_section_data (isec)->relocs != internal_relocs)
2030 free (internal_relocs);
2031 return FALSE;
2034 static reloc_howto_type *
2035 ppc_elf_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
2036 bfd_reloc_code_real_type code)
2038 enum elf_ppc_reloc_type r;
2040 /* Initialize howto table if not already done. */
2041 if (!ppc_elf_howto_table[R_PPC_ADDR32])
2042 ppc_elf_howto_init ();
2044 switch (code)
2046 default:
2047 return NULL;
2049 case BFD_RELOC_NONE: r = R_PPC_NONE; break;
2050 case BFD_RELOC_32: r = R_PPC_ADDR32; break;
2051 case BFD_RELOC_PPC_BA26: r = R_PPC_ADDR24; break;
2052 case BFD_RELOC_16: r = R_PPC_ADDR16; break;
2053 case BFD_RELOC_LO16: r = R_PPC_ADDR16_LO; break;
2054 case BFD_RELOC_HI16: r = R_PPC_ADDR16_HI; break;
2055 case BFD_RELOC_HI16_S: r = R_PPC_ADDR16_HA; break;
2056 case BFD_RELOC_PPC_BA16: r = R_PPC_ADDR14; break;
2057 case BFD_RELOC_PPC_BA16_BRTAKEN: r = R_PPC_ADDR14_BRTAKEN; break;
2058 case BFD_RELOC_PPC_BA16_BRNTAKEN: r = R_PPC_ADDR14_BRNTAKEN; break;
2059 case BFD_RELOC_PPC_B26: r = R_PPC_REL24; break;
2060 case BFD_RELOC_PPC_B16: r = R_PPC_REL14; break;
2061 case BFD_RELOC_PPC_B16_BRTAKEN: r = R_PPC_REL14_BRTAKEN; break;
2062 case BFD_RELOC_PPC_B16_BRNTAKEN: r = R_PPC_REL14_BRNTAKEN; break;
2063 case BFD_RELOC_16_GOTOFF: r = R_PPC_GOT16; break;
2064 case BFD_RELOC_LO16_GOTOFF: r = R_PPC_GOT16_LO; break;
2065 case BFD_RELOC_HI16_GOTOFF: r = R_PPC_GOT16_HI; break;
2066 case BFD_RELOC_HI16_S_GOTOFF: r = R_PPC_GOT16_HA; break;
2067 case BFD_RELOC_24_PLT_PCREL: r = R_PPC_PLTREL24; break;
2068 case BFD_RELOC_PPC_COPY: r = R_PPC_COPY; break;
2069 case BFD_RELOC_PPC_GLOB_DAT: r = R_PPC_GLOB_DAT; break;
2070 case BFD_RELOC_PPC_LOCAL24PC: r = R_PPC_LOCAL24PC; break;
2071 case BFD_RELOC_32_PCREL: r = R_PPC_REL32; break;
2072 case BFD_RELOC_32_PLTOFF: r = R_PPC_PLT32; break;
2073 case BFD_RELOC_32_PLT_PCREL: r = R_PPC_PLTREL32; break;
2074 case BFD_RELOC_LO16_PLTOFF: r = R_PPC_PLT16_LO; break;
2075 case BFD_RELOC_HI16_PLTOFF: r = R_PPC_PLT16_HI; break;
2076 case BFD_RELOC_HI16_S_PLTOFF: r = R_PPC_PLT16_HA; break;
2077 case BFD_RELOC_GPREL16: r = R_PPC_SDAREL16; break;
2078 case BFD_RELOC_16_BASEREL: r = R_PPC_SECTOFF; break;
2079 case BFD_RELOC_LO16_BASEREL: r = R_PPC_SECTOFF_LO; break;
2080 case BFD_RELOC_HI16_BASEREL: r = R_PPC_SECTOFF_HI; break;
2081 case BFD_RELOC_HI16_S_BASEREL: r = R_PPC_SECTOFF_HA; break;
2082 case BFD_RELOC_CTOR: r = R_PPC_ADDR32; break;
2083 case BFD_RELOC_PPC_TOC16: r = R_PPC_TOC16; break;
2084 case BFD_RELOC_PPC_TLS: r = R_PPC_TLS; break;
2085 case BFD_RELOC_PPC_DTPMOD: r = R_PPC_DTPMOD32; break;
2086 case BFD_RELOC_PPC_TPREL16: r = R_PPC_TPREL16; break;
2087 case BFD_RELOC_PPC_TPREL16_LO: r = R_PPC_TPREL16_LO; break;
2088 case BFD_RELOC_PPC_TPREL16_HI: r = R_PPC_TPREL16_HI; break;
2089 case BFD_RELOC_PPC_TPREL16_HA: r = R_PPC_TPREL16_HA; break;
2090 case BFD_RELOC_PPC_TPREL: r = R_PPC_TPREL32; break;
2091 case BFD_RELOC_PPC_DTPREL16: r = R_PPC_DTPREL16; break;
2092 case BFD_RELOC_PPC_DTPREL16_LO: r = R_PPC_DTPREL16_LO; break;
2093 case BFD_RELOC_PPC_DTPREL16_HI: r = R_PPC_DTPREL16_HI; break;
2094 case BFD_RELOC_PPC_DTPREL16_HA: r = R_PPC_DTPREL16_HA; break;
2095 case BFD_RELOC_PPC_DTPREL: r = R_PPC_DTPREL32; break;
2096 case BFD_RELOC_PPC_GOT_TLSGD16: r = R_PPC_GOT_TLSGD16; break;
2097 case BFD_RELOC_PPC_GOT_TLSGD16_LO: r = R_PPC_GOT_TLSGD16_LO; break;
2098 case BFD_RELOC_PPC_GOT_TLSGD16_HI: r = R_PPC_GOT_TLSGD16_HI; break;
2099 case BFD_RELOC_PPC_GOT_TLSGD16_HA: r = R_PPC_GOT_TLSGD16_HA; break;
2100 case BFD_RELOC_PPC_GOT_TLSLD16: r = R_PPC_GOT_TLSLD16; break;
2101 case BFD_RELOC_PPC_GOT_TLSLD16_LO: r = R_PPC_GOT_TLSLD16_LO; break;
2102 case BFD_RELOC_PPC_GOT_TLSLD16_HI: r = R_PPC_GOT_TLSLD16_HI; break;
2103 case BFD_RELOC_PPC_GOT_TLSLD16_HA: r = R_PPC_GOT_TLSLD16_HA; break;
2104 case BFD_RELOC_PPC_GOT_TPREL16: r = R_PPC_GOT_TPREL16; break;
2105 case BFD_RELOC_PPC_GOT_TPREL16_LO: r = R_PPC_GOT_TPREL16_LO; break;
2106 case BFD_RELOC_PPC_GOT_TPREL16_HI: r = R_PPC_GOT_TPREL16_HI; break;
2107 case BFD_RELOC_PPC_GOT_TPREL16_HA: r = R_PPC_GOT_TPREL16_HA; break;
2108 case BFD_RELOC_PPC_GOT_DTPREL16: r = R_PPC_GOT_DTPREL16; break;
2109 case BFD_RELOC_PPC_GOT_DTPREL16_LO: r = R_PPC_GOT_DTPREL16_LO; break;
2110 case BFD_RELOC_PPC_GOT_DTPREL16_HI: r = R_PPC_GOT_DTPREL16_HI; break;
2111 case BFD_RELOC_PPC_GOT_DTPREL16_HA: r = R_PPC_GOT_DTPREL16_HA; break;
2112 case BFD_RELOC_PPC_EMB_NADDR32: r = R_PPC_EMB_NADDR32; break;
2113 case BFD_RELOC_PPC_EMB_NADDR16: r = R_PPC_EMB_NADDR16; break;
2114 case BFD_RELOC_PPC_EMB_NADDR16_LO: r = R_PPC_EMB_NADDR16_LO; break;
2115 case BFD_RELOC_PPC_EMB_NADDR16_HI: r = R_PPC_EMB_NADDR16_HI; break;
2116 case BFD_RELOC_PPC_EMB_NADDR16_HA: r = R_PPC_EMB_NADDR16_HA; break;
2117 case BFD_RELOC_PPC_EMB_SDAI16: r = R_PPC_EMB_SDAI16; break;
2118 case BFD_RELOC_PPC_EMB_SDA2I16: r = R_PPC_EMB_SDA2I16; break;
2119 case BFD_RELOC_PPC_EMB_SDA2REL: r = R_PPC_EMB_SDA2REL; break;
2120 case BFD_RELOC_PPC_EMB_SDA21: r = R_PPC_EMB_SDA21; break;
2121 case BFD_RELOC_PPC_EMB_MRKREF: r = R_PPC_EMB_MRKREF; break;
2122 case BFD_RELOC_PPC_EMB_RELSEC16: r = R_PPC_EMB_RELSEC16; break;
2123 case BFD_RELOC_PPC_EMB_RELST_LO: r = R_PPC_EMB_RELST_LO; break;
2124 case BFD_RELOC_PPC_EMB_RELST_HI: r = R_PPC_EMB_RELST_HI; break;
2125 case BFD_RELOC_PPC_EMB_RELST_HA: r = R_PPC_EMB_RELST_HA; break;
2126 case BFD_RELOC_PPC_EMB_BIT_FLD: r = R_PPC_EMB_BIT_FLD; break;
2127 case BFD_RELOC_PPC_EMB_RELSDA: r = R_PPC_EMB_RELSDA; break;
2128 case BFD_RELOC_VTABLE_INHERIT: r = R_PPC_GNU_VTINHERIT; break;
2129 case BFD_RELOC_VTABLE_ENTRY: r = R_PPC_GNU_VTENTRY; break;
2132 return ppc_elf_howto_table[r];
2135 /* Set the howto pointer for a PowerPC ELF reloc. */
2137 static void
2138 ppc_elf_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED,
2139 arelent *cache_ptr,
2140 Elf_Internal_Rela *dst)
2142 /* Initialize howto table if not already done. */
2143 if (!ppc_elf_howto_table[R_PPC_ADDR32])
2144 ppc_elf_howto_init ();
2146 BFD_ASSERT (ELF32_R_TYPE (dst->r_info) < (unsigned int) R_PPC_max);
2147 cache_ptr->howto = ppc_elf_howto_table[ELF32_R_TYPE (dst->r_info)];
2150 /* Handle the R_PPC_ADDR16_HA reloc. */
2152 static bfd_reloc_status_type
2153 ppc_elf_addr16_ha_reloc (bfd *abfd ATTRIBUTE_UNUSED,
2154 arelent *reloc_entry,
2155 asymbol *symbol,
2156 void *data ATTRIBUTE_UNUSED,
2157 asection *input_section,
2158 bfd *output_bfd,
2159 char **error_message ATTRIBUTE_UNUSED)
2161 bfd_vma relocation;
2163 if (output_bfd != NULL)
2165 reloc_entry->address += input_section->output_offset;
2166 return bfd_reloc_ok;
2169 if (reloc_entry->address > input_section->_cooked_size)
2170 return bfd_reloc_outofrange;
2172 if (bfd_is_com_section (symbol->section))
2173 relocation = 0;
2174 else
2175 relocation = symbol->value;
2177 relocation += symbol->section->output_section->vma;
2178 relocation += symbol->section->output_offset;
2179 relocation += reloc_entry->addend;
2181 reloc_entry->addend += (relocation & 0x8000) << 1;
2183 return bfd_reloc_continue;
2186 static bfd_reloc_status_type
2187 ppc_elf_unhandled_reloc (bfd *abfd,
2188 arelent *reloc_entry,
2189 asymbol *symbol,
2190 void *data,
2191 asection *input_section,
2192 bfd *output_bfd,
2193 char **error_message)
2195 /* If this is a relocatable link (output_bfd test tells us), just
2196 call the generic function. Any adjustment will be done at final
2197 link time. */
2198 if (output_bfd != NULL)
2199 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2200 input_section, output_bfd, error_message);
2202 if (error_message != NULL)
2204 static char buf[60];
2205 sprintf (buf, _("generic linker can't handle %s"),
2206 reloc_entry->howto->name);
2207 *error_message = buf;
2209 return bfd_reloc_dangerous;
2212 /* Fix bad default arch selected for a 32 bit input bfd when the
2213 default is 64 bit. */
2215 static bfd_boolean
2216 ppc_elf_object_p (bfd *abfd)
2218 if (abfd->arch_info->the_default && abfd->arch_info->bits_per_word == 64)
2220 Elf_Internal_Ehdr *i_ehdr = elf_elfheader (abfd);
2222 if (i_ehdr->e_ident[EI_CLASS] == ELFCLASS32)
2224 /* Relies on arch after 64 bit default being 32 bit default. */
2225 abfd->arch_info = abfd->arch_info->next;
2226 BFD_ASSERT (abfd->arch_info->bits_per_word == 32);
2229 return TRUE;
2232 /* Function to set whether a module needs the -mrelocatable bit set. */
2234 static bfd_boolean
2235 ppc_elf_set_private_flags (bfd *abfd, flagword flags)
2237 BFD_ASSERT (!elf_flags_init (abfd)
2238 || elf_elfheader (abfd)->e_flags == flags);
2240 elf_elfheader (abfd)->e_flags = flags;
2241 elf_flags_init (abfd) = TRUE;
2242 return TRUE;
2245 /* Merge backend specific data from an object file to the output
2246 object file when linking. */
2248 static bfd_boolean
2249 ppc_elf_merge_private_bfd_data (bfd *ibfd, bfd *obfd)
2251 flagword old_flags;
2252 flagword new_flags;
2253 bfd_boolean error;
2255 /* Check if we have the same endianess. */
2256 if (! _bfd_generic_verify_endian_match (ibfd, obfd))
2257 return FALSE;
2259 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
2260 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
2261 return TRUE;
2263 new_flags = elf_elfheader (ibfd)->e_flags;
2264 old_flags = elf_elfheader (obfd)->e_flags;
2265 if (!elf_flags_init (obfd))
2267 /* First call, no flags set. */
2268 elf_flags_init (obfd) = TRUE;
2269 elf_elfheader (obfd)->e_flags = new_flags;
2272 /* Compatible flags are ok. */
2273 else if (new_flags == old_flags)
2276 /* Incompatible flags. */
2277 else
2279 /* Warn about -mrelocatable mismatch. Allow -mrelocatable-lib
2280 to be linked with either. */
2281 error = FALSE;
2282 if ((new_flags & EF_PPC_RELOCATABLE) != 0
2283 && (old_flags & (EF_PPC_RELOCATABLE | EF_PPC_RELOCATABLE_LIB)) == 0)
2285 error = TRUE;
2286 (*_bfd_error_handler)
2287 (_("%s: compiled with -mrelocatable and linked with "
2288 "modules compiled normally"),
2289 bfd_archive_filename (ibfd));
2291 else if ((new_flags & (EF_PPC_RELOCATABLE | EF_PPC_RELOCATABLE_LIB)) == 0
2292 && (old_flags & EF_PPC_RELOCATABLE) != 0)
2294 error = TRUE;
2295 (*_bfd_error_handler)
2296 (_("%s: compiled normally and linked with "
2297 "modules compiled with -mrelocatable"),
2298 bfd_archive_filename (ibfd));
2301 /* The output is -mrelocatable-lib iff both the input files are. */
2302 if (! (new_flags & EF_PPC_RELOCATABLE_LIB))
2303 elf_elfheader (obfd)->e_flags &= ~EF_PPC_RELOCATABLE_LIB;
2305 /* The output is -mrelocatable iff it can't be -mrelocatable-lib,
2306 but each input file is either -mrelocatable or -mrelocatable-lib. */
2307 if (! (elf_elfheader (obfd)->e_flags & EF_PPC_RELOCATABLE_LIB)
2308 && (new_flags & (EF_PPC_RELOCATABLE_LIB | EF_PPC_RELOCATABLE))
2309 && (old_flags & (EF_PPC_RELOCATABLE_LIB | EF_PPC_RELOCATABLE)))
2310 elf_elfheader (obfd)->e_flags |= EF_PPC_RELOCATABLE;
2312 /* Do not warn about eabi vs. V.4 mismatch, just or in the bit if
2313 any module uses it. */
2314 elf_elfheader (obfd)->e_flags |= (new_flags & EF_PPC_EMB);
2316 new_flags &= ~(EF_PPC_RELOCATABLE | EF_PPC_RELOCATABLE_LIB | EF_PPC_EMB);
2317 old_flags &= ~(EF_PPC_RELOCATABLE | EF_PPC_RELOCATABLE_LIB | EF_PPC_EMB);
2319 /* Warn about any other mismatches. */
2320 if (new_flags != old_flags)
2322 error = TRUE;
2323 (*_bfd_error_handler)
2324 (_("%s: uses different e_flags (0x%lx) fields "
2325 "than previous modules (0x%lx)"),
2326 bfd_archive_filename (ibfd), (long) new_flags, (long) old_flags);
2329 if (error)
2331 bfd_set_error (bfd_error_bad_value);
2332 return FALSE;
2336 return TRUE;
2339 /* Handle a PowerPC specific section when reading an object file. This
2340 is called when elfcode.h finds a section with an unknown type. */
2342 static bfd_boolean
2343 ppc_elf_section_from_shdr (bfd *abfd, Elf_Internal_Shdr *hdr, const char *name)
2345 asection *newsect;
2346 flagword flags;
2348 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name))
2349 return FALSE;
2351 newsect = hdr->bfd_section;
2352 flags = bfd_get_section_flags (abfd, newsect);
2353 if (hdr->sh_flags & SHF_EXCLUDE)
2354 flags |= SEC_EXCLUDE;
2356 if (hdr->sh_type == SHT_ORDERED)
2357 flags |= SEC_SORT_ENTRIES;
2359 bfd_set_section_flags (abfd, newsect, flags);
2360 return TRUE;
2363 /* Set up any other section flags and such that may be necessary. */
2365 static bfd_boolean
2366 ppc_elf_fake_sections (bfd *abfd ATTRIBUTE_UNUSED,
2367 Elf_Internal_Shdr *shdr,
2368 asection *asect)
2370 if ((asect->flags & SEC_EXCLUDE) != 0)
2371 shdr->sh_flags |= SHF_EXCLUDE;
2373 if ((asect->flags & SEC_SORT_ENTRIES) != 0)
2374 shdr->sh_type = SHT_ORDERED;
2376 return TRUE;
2379 /* Find a linker generated pointer with a given addend and type. */
2381 static elf_linker_section_pointers_t *
2382 elf_find_pointer_linker_section
2383 (elf_linker_section_pointers_t *linker_pointers,
2384 bfd_vma addend,
2385 elf_linker_section_t *lsect)
2387 for ( ; linker_pointers != NULL; linker_pointers = linker_pointers->next)
2388 if (lsect == linker_pointers->lsect && addend == linker_pointers->addend)
2389 return linker_pointers;
2391 return NULL;
2394 /* Allocate a pointer to live in a linker created section. */
2396 static bfd_boolean
2397 elf_create_pointer_linker_section (bfd *abfd,
2398 struct bfd_link_info *info,
2399 elf_linker_section_t *lsect,
2400 struct elf_link_hash_entry *h,
2401 const Elf_Internal_Rela *rel)
2403 elf_linker_section_pointers_t **ptr_linker_section_ptr = NULL;
2404 elf_linker_section_pointers_t *linker_section_ptr;
2405 unsigned long r_symndx = ELF32_R_SYM (rel->r_info);
2406 bfd_size_type amt;
2408 BFD_ASSERT (lsect != NULL);
2410 /* Is this a global symbol? */
2411 if (h != NULL)
2413 struct ppc_elf_link_hash_entry *eh;
2415 /* Has this symbol already been allocated? If so, our work is done. */
2416 eh = (struct ppc_elf_link_hash_entry *) h;
2417 if (elf_find_pointer_linker_section (eh->linker_section_pointer,
2418 rel->r_addend,
2419 lsect))
2420 return TRUE;
2422 ptr_linker_section_ptr = &eh->linker_section_pointer;
2423 /* Make sure this symbol is output as a dynamic symbol. */
2424 if (h->dynindx == -1)
2426 if (! _bfd_elf_link_record_dynamic_symbol (info, h))
2427 return FALSE;
2430 if (lsect->rel_section)
2431 lsect->rel_section->_raw_size += sizeof (Elf32_External_Rela);
2433 else
2435 /* Allocation of a pointer to a local symbol. */
2436 elf_linker_section_pointers_t **ptr = elf_local_ptr_offsets (abfd);
2438 /* Allocate a table to hold the local symbols if first time. */
2439 if (!ptr)
2441 unsigned int num_symbols = elf_tdata (abfd)->symtab_hdr.sh_info;
2443 amt = num_symbols;
2444 amt *= sizeof (elf_linker_section_pointers_t *);
2445 ptr = bfd_zalloc (abfd, amt);
2447 if (!ptr)
2448 return FALSE;
2450 elf_local_ptr_offsets (abfd) = ptr;
2453 /* Has this symbol already been allocated? If so, our work is done. */
2454 if (elf_find_pointer_linker_section (ptr[r_symndx],
2455 rel->r_addend,
2456 lsect))
2457 return TRUE;
2459 ptr_linker_section_ptr = &ptr[r_symndx];
2461 if (info->shared)
2463 /* If we are generating a shared object, we need to
2464 output a R_<xxx>_RELATIVE reloc so that the
2465 dynamic linker can adjust this GOT entry. */
2466 BFD_ASSERT (lsect->rel_section != NULL);
2467 lsect->rel_section->_raw_size += sizeof (Elf32_External_Rela);
2471 /* Allocate space for a pointer in the linker section, and allocate
2472 a new pointer record from internal memory. */
2473 BFD_ASSERT (ptr_linker_section_ptr != NULL);
2474 amt = sizeof (elf_linker_section_pointers_t);
2475 linker_section_ptr = bfd_alloc (abfd, amt);
2477 if (!linker_section_ptr)
2478 return FALSE;
2480 linker_section_ptr->next = *ptr_linker_section_ptr;
2481 linker_section_ptr->addend = rel->r_addend;
2482 linker_section_ptr->lsect = lsect;
2483 linker_section_ptr->written_address_p = FALSE;
2484 *ptr_linker_section_ptr = linker_section_ptr;
2486 linker_section_ptr->offset = lsect->section->_raw_size;
2487 lsect->section->_raw_size += 4;
2489 #ifdef DEBUG
2490 fprintf (stderr,
2491 "Create pointer in linker section %s, offset = %ld, section size = %ld\n",
2492 lsect->name, (long) linker_section_ptr->offset,
2493 (long) lsect->section->_raw_size);
2494 #endif
2496 return TRUE;
2499 #define bfd_put_ptr(BFD, VAL, ADDR) bfd_put_32 (BFD, VAL, ADDR)
2501 /* Fill in the address for a pointer generated in a linker section. */
2503 static bfd_vma
2504 elf_finish_pointer_linker_section (bfd *output_bfd,
2505 bfd *input_bfd,
2506 struct bfd_link_info *info,
2507 elf_linker_section_t *lsect,
2508 struct elf_link_hash_entry *h,
2509 bfd_vma relocation,
2510 const Elf_Internal_Rela *rel,
2511 int relative_reloc)
2513 elf_linker_section_pointers_t *linker_section_ptr;
2515 BFD_ASSERT (lsect != NULL);
2517 if (h != NULL)
2519 /* Handle global symbol. */
2520 struct ppc_elf_link_hash_entry *eh;
2522 eh = (struct ppc_elf_link_hash_entry *) h;
2523 linker_section_ptr
2524 = elf_find_pointer_linker_section (eh->linker_section_pointer,
2525 rel->r_addend,
2526 lsect);
2528 BFD_ASSERT (linker_section_ptr != NULL);
2530 if (! elf_hash_table (info)->dynamic_sections_created
2531 || (info->shared
2532 && info->symbolic
2533 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR)))
2535 /* This is actually a static link, or it is a
2536 -Bsymbolic link and the symbol is defined
2537 locally. We must initialize this entry in the
2538 global section.
2540 When doing a dynamic link, we create a .rela.<xxx>
2541 relocation entry to initialize the value. This
2542 is done in the finish_dynamic_symbol routine. */
2543 if (!linker_section_ptr->written_address_p)
2545 linker_section_ptr->written_address_p = TRUE;
2546 bfd_put_ptr (output_bfd,
2547 relocation + linker_section_ptr->addend,
2548 (lsect->section->contents
2549 + linker_section_ptr->offset));
2553 else
2555 /* Handle local symbol. */
2556 unsigned long r_symndx = ELF32_R_SYM (rel->r_info);
2557 BFD_ASSERT (elf_local_ptr_offsets (input_bfd) != NULL);
2558 BFD_ASSERT (elf_local_ptr_offsets (input_bfd)[r_symndx] != NULL);
2559 linker_section_ptr = (elf_find_pointer_linker_section
2560 (elf_local_ptr_offsets (input_bfd)[r_symndx],
2561 rel->r_addend,
2562 lsect));
2564 BFD_ASSERT (linker_section_ptr != NULL);
2566 /* Write out pointer if it hasn't been rewritten out before. */
2567 if (!linker_section_ptr->written_address_p)
2569 linker_section_ptr->written_address_p = TRUE;
2570 bfd_put_ptr (output_bfd, relocation + linker_section_ptr->addend,
2571 lsect->section->contents + linker_section_ptr->offset);
2573 if (info->shared)
2575 /* We need to generate a relative reloc for the dynamic
2576 linker. */
2578 asection *srel = lsect->rel_section;
2579 Elf_Internal_Rela outrel[MAX_INT_RELS_PER_EXT_REL];
2580 bfd_byte *erel;
2581 const struct elf_backend_data *bed;
2582 unsigned int i;
2584 BFD_ASSERT (srel != NULL);
2586 bed = get_elf_backend_data (output_bfd);
2587 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
2589 outrel[i].r_offset = (lsect->section->output_section->vma
2590 + lsect->section->output_offset
2591 + linker_section_ptr->offset);
2592 outrel[i].r_info = 0;
2593 outrel[i].r_addend = 0;
2595 outrel[0].r_info = ELF32_R_INFO (0, relative_reloc);
2596 erel = lsect->section->contents;
2597 erel += (elf_section_data (lsect->section)->rel_count++
2598 * sizeof (Elf32_External_Rela));
2599 bfd_elf32_swap_reloca_out (output_bfd, outrel, erel);
2604 relocation = (lsect->section->output_offset
2605 + linker_section_ptr->offset
2606 - lsect->sym_offset);
2608 #ifdef DEBUG
2609 fprintf (stderr,
2610 "Finish pointer in linker section %s, offset = %ld (0x%lx)\n",
2611 lsect->name, (long) relocation, (long) relocation);
2612 #endif
2614 /* Subtract out the addend, because it will get added back in by the normal
2615 processing. */
2616 return relocation - linker_section_ptr->addend;
2619 /* Create a special linker section */
2620 static elf_linker_section_t *
2621 ppc_elf_create_linker_section (bfd *abfd,
2622 struct bfd_link_info *info,
2623 enum elf_linker_section_enum which)
2625 elf_linker_section_t *lsect;
2626 struct ppc_elf_link_hash_table *htab = ppc_elf_hash_table (info);
2627 asection *s;
2628 bfd_size_type amt;
2629 flagword flags;
2630 const char *name;
2631 const char *rel_name;
2632 const char *sym_name;
2633 bfd_vma sym_offset;
2635 /* Both of these sections are (technically) created by the user
2636 putting data in them, so they shouldn't be marked
2637 SEC_LINKER_CREATED.
2639 The linker creates them so it has somewhere to attach their
2640 respective symbols. In fact, if they were empty it would
2641 be OK to leave the symbol set to 0 (or any random number), because
2642 the appropriate register should never be used. */
2643 flags = SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY;
2644 sym_offset = 32768;
2646 switch (which)
2648 default:
2649 abort ();
2650 return NULL;
2652 case LINKER_SECTION_SDATA: /* .sdata/.sbss section */
2653 name = ".sdata";
2654 rel_name = ".rela.sdata";
2655 sym_name = "_SDA_BASE_";
2656 break;
2658 case LINKER_SECTION_SDATA2: /* .sdata2/.sbss2 section */
2659 name = ".sdata2";
2660 rel_name = ".rela.sdata2";
2661 sym_name = "_SDA2_BASE_";
2662 flags |= SEC_READONLY;
2663 break;
2666 /* Record the first bfd that needs the special sections. */
2667 if (!htab->elf.dynobj)
2668 htab->elf.dynobj = abfd;
2670 amt = sizeof (elf_linker_section_t);
2671 lsect = bfd_zalloc (htab->elf.dynobj, amt);
2673 lsect->sym_offset = sym_offset;
2675 /* See if the sections already exist. */
2676 s = bfd_get_section_by_name (htab->elf.dynobj, name);
2677 if (s == NULL || (s->flags & flags) != flags)
2679 s = bfd_make_section_anyway (htab->elf.dynobj, name);
2680 if (s == NULL
2681 || !bfd_set_section_flags (htab->elf.dynobj, s, flags))
2682 return NULL;
2684 lsect->section = s;
2686 if (bfd_get_section_alignment (htab->elf.dynobj, s) < 2
2687 && !bfd_set_section_alignment (htab->elf.dynobj, s, 2))
2688 return NULL;
2690 s->_raw_size = align_power (s->_raw_size, 2);
2692 #ifdef DEBUG
2693 fprintf (stderr, "Creating section %s, current size = %ld\n",
2694 name, (long) s->_raw_size);
2695 #endif
2697 if (sym_name)
2699 struct elf_link_hash_entry *h;
2700 struct bfd_link_hash_entry *bh;
2702 #ifdef DEBUG
2703 fprintf (stderr, "Adding %s to section %s\n", sym_name, name);
2704 #endif
2705 bh = bfd_link_hash_lookup (info->hash, sym_name,
2706 FALSE, FALSE, FALSE);
2708 if ((bh == NULL || bh->type == bfd_link_hash_undefined)
2709 && !(_bfd_generic_link_add_one_symbol
2710 (info, abfd, sym_name, BSF_GLOBAL, s, sym_offset, NULL,
2711 FALSE, get_elf_backend_data (abfd)->collect, &bh)))
2712 return NULL;
2713 h = (struct elf_link_hash_entry *) bh;
2715 h->type = STT_OBJECT;
2716 lsect->sym_hash = h;
2718 if (info->shared
2719 && ! _bfd_elf_link_record_dynamic_symbol (info, h))
2720 return NULL;
2723 if (info->shared)
2725 s = bfd_make_section_anyway (htab->elf.dynobj, rel_name);
2726 lsect->rel_section = s;
2727 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
2728 | SEC_LINKER_CREATED | SEC_READONLY);
2729 if (s == NULL
2730 || ! bfd_set_section_flags (htab->elf.dynobj, s, flags)
2731 || ! bfd_set_section_alignment (htab->elf.dynobj, s, 2))
2732 return NULL;
2735 return lsect;
2738 /* If we have a non-zero sized .sbss2 or .PPC.EMB.sbss0 sections, we
2739 need to bump up the number of section headers. */
2741 static int
2742 ppc_elf_additional_program_headers (bfd *abfd)
2744 asection *s;
2745 int ret;
2747 ret = 0;
2749 s = bfd_get_section_by_name (abfd, ".interp");
2750 if (s != NULL)
2751 ++ret;
2753 s = bfd_get_section_by_name (abfd, ".sbss2");
2754 if (s != NULL && (s->flags & SEC_LOAD) != 0 && s->_raw_size > 0)
2755 ++ret;
2757 s = bfd_get_section_by_name (abfd, ".PPC.EMB.sbss0");
2758 if (s != NULL && (s->flags & SEC_LOAD) != 0 && s->_raw_size > 0)
2759 ++ret;
2761 return ret;
2764 /* Modify the segment map if needed. */
2766 static bfd_boolean
2767 ppc_elf_modify_segment_map (bfd *abfd ATTRIBUTE_UNUSED)
2769 return TRUE;
2772 /* The powerpc .got has a blrl instruction in it. Mark it executable. */
2774 static bfd_boolean
2775 ppc_elf_create_got (bfd *abfd, struct bfd_link_info *info)
2777 struct ppc_elf_link_hash_table *htab;
2778 asection *s;
2779 flagword flags;
2781 if (!_bfd_elf_create_got_section (abfd, info))
2782 return FALSE;
2784 htab = ppc_elf_hash_table (info);
2785 htab->got = s = bfd_get_section_by_name (abfd, ".got");
2786 if (s == NULL)
2787 abort ();
2789 flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_HAS_CONTENTS | SEC_IN_MEMORY
2790 | SEC_LINKER_CREATED);
2791 if (!bfd_set_section_flags (abfd, s, flags))
2792 return FALSE;
2794 htab->relgot = bfd_make_section (abfd, ".rela.got");
2795 if (!htab->relgot
2796 || ! bfd_set_section_flags (abfd, htab->relgot,
2797 (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
2798 | SEC_IN_MEMORY | SEC_LINKER_CREATED
2799 | SEC_READONLY))
2800 || ! bfd_set_section_alignment (abfd, htab->relgot, 2))
2801 return FALSE;
2803 return TRUE;
2806 /* We have to create .dynsbss and .rela.sbss here so that they get mapped
2807 to output sections (just like _bfd_elf_create_dynamic_sections has
2808 to create .dynbss and .rela.bss). */
2810 static bfd_boolean
2811 ppc_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
2813 struct ppc_elf_link_hash_table *htab;
2814 asection *s;
2815 flagword flags;
2817 htab = ppc_elf_hash_table (info);
2819 if (htab->got == NULL
2820 && !ppc_elf_create_got (abfd, info))
2821 return FALSE;
2823 if (!_bfd_elf_create_dynamic_sections (abfd, info))
2824 return FALSE;
2826 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
2827 | SEC_LINKER_CREATED);
2829 htab->dynbss = bfd_get_section_by_name (abfd, ".dynbss");
2830 htab->dynsbss = s = bfd_make_section (abfd, ".dynsbss");
2831 if (s == NULL
2832 || ! bfd_set_section_flags (abfd, s, SEC_ALLOC))
2833 return FALSE;
2835 if (! info->shared)
2837 htab->relbss = bfd_get_section_by_name (abfd, ".rela.bss");
2838 htab->relsbss = s = bfd_make_section (abfd, ".rela.sbss");
2839 if (s == NULL
2840 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
2841 || ! bfd_set_section_alignment (abfd, s, 2))
2842 return FALSE;
2845 htab->relplt = bfd_get_section_by_name (abfd, ".rela.plt");
2846 htab->plt = s = bfd_get_section_by_name (abfd, ".plt");
2847 if (s == NULL)
2848 abort ();
2850 flags = SEC_ALLOC | SEC_CODE | SEC_IN_MEMORY | SEC_LINKER_CREATED;
2851 return bfd_set_section_flags (abfd, s, flags);
2854 /* Adjust a symbol defined by a dynamic object and referenced by a
2855 regular object. The current definition is in some section of the
2856 dynamic object, but we're not including those sections. We have to
2857 change the definition to something the rest of the link can
2858 understand. */
2860 static bfd_boolean
2861 ppc_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
2862 struct elf_link_hash_entry *h)
2864 struct ppc_elf_link_hash_table *htab;
2865 asection *s;
2866 unsigned int power_of_two;
2868 #ifdef DEBUG
2869 fprintf (stderr, "ppc_elf_adjust_dynamic_symbol called for %s\n",
2870 h->root.root.string);
2871 #endif
2873 /* Make sure we know what is going on here. */
2874 htab = ppc_elf_hash_table (info);
2875 BFD_ASSERT (htab->elf.dynobj != NULL
2876 && ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT)
2877 || h->weakdef != NULL
2878 || ((h->elf_link_hash_flags
2879 & ELF_LINK_HASH_DEF_DYNAMIC) != 0
2880 && (h->elf_link_hash_flags
2881 & ELF_LINK_HASH_REF_REGULAR) != 0
2882 && (h->elf_link_hash_flags
2883 & ELF_LINK_HASH_DEF_REGULAR) == 0)));
2885 /* Deal with function syms. */
2886 if (h->type == STT_FUNC
2887 || (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0)
2889 /* Clear procedure linkage table information for any symbol that
2890 won't need a .plt entry. */
2891 if (h->plt.refcount <= 0
2892 || SYMBOL_CALLS_LOCAL (info, h)
2893 || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2894 && h->root.type == bfd_link_hash_undefweak))
2896 /* A PLT entry is not required/allowed when:
2898 1. We are not using ld.so; because then the PLT entry
2899 can't be set up, so we can't use one. In this case,
2900 ppc_elf_adjust_dynamic_symbol won't even be called.
2902 2. GC has rendered the entry unused.
2904 3. We know for certain that a call to this symbol
2905 will go to this object, or will remain undefined. */
2906 h->plt.offset = (bfd_vma) -1;
2907 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
2909 return TRUE;
2911 else
2912 h->plt.offset = (bfd_vma) -1;
2914 /* If this is a weak symbol, and there is a real definition, the
2915 processor independent code will have arranged for us to see the
2916 real definition first, and we can just use the same value. */
2917 if (h->weakdef != NULL)
2919 BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined
2920 || h->weakdef->root.type == bfd_link_hash_defweak);
2921 h->root.u.def.section = h->weakdef->root.u.def.section;
2922 h->root.u.def.value = h->weakdef->root.u.def.value;
2923 if (ELIMINATE_COPY_RELOCS)
2924 h->elf_link_hash_flags
2925 = ((h->elf_link_hash_flags & ~ELF_LINK_NON_GOT_REF)
2926 | (h->weakdef->elf_link_hash_flags & ELF_LINK_NON_GOT_REF));
2927 return TRUE;
2930 /* This is a reference to a symbol defined by a dynamic object which
2931 is not a function. */
2933 /* If we are creating a shared library, we must presume that the
2934 only references to the symbol are via the global offset table.
2935 For such cases we need not do anything here; the relocations will
2936 be handled correctly by relocate_section. */
2937 if (info->shared)
2938 return TRUE;
2940 /* If there are no references to this symbol that do not use the
2941 GOT, we don't need to generate a copy reloc. */
2942 if ((h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0)
2943 return TRUE;
2945 if (ELIMINATE_COPY_RELOCS)
2947 struct ppc_elf_dyn_relocs *p;
2948 for (p = ppc_elf_hash_entry (h)->dyn_relocs; p != NULL; p = p->next)
2950 s = p->sec->output_section;
2951 if (s != NULL && (s->flags & SEC_READONLY) != 0)
2952 break;
2955 /* If we didn't find any dynamic relocs in read-only sections, then
2956 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
2957 if (p == NULL)
2959 h->elf_link_hash_flags &= ~ELF_LINK_NON_GOT_REF;
2960 return TRUE;
2964 /* We must allocate the symbol in our .dynbss section, which will
2965 become part of the .bss section of the executable. There will be
2966 an entry for this symbol in the .dynsym section. The dynamic
2967 object will contain position independent code, so all references
2968 from the dynamic object to this symbol will go through the global
2969 offset table. The dynamic linker will use the .dynsym entry to
2970 determine the address it must put in the global offset table, so
2971 both the dynamic object and the regular object will refer to the
2972 same memory location for the variable.
2974 Of course, if the symbol is sufficiently small, we must instead
2975 allocate it in .sbss. FIXME: It would be better to do this if and
2976 only if there were actually SDAREL relocs for that symbol. */
2978 if (h->size <= elf_gp_size (htab->elf.dynobj))
2979 s = htab->dynsbss;
2980 else
2981 s = htab->dynbss;
2982 BFD_ASSERT (s != NULL);
2984 /* We must generate a R_PPC_COPY reloc to tell the dynamic linker to
2985 copy the initial value out of the dynamic object and into the
2986 runtime process image. We need to remember the offset into the
2987 .rela.bss section we are going to use. */
2988 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
2990 asection *srel;
2992 if (h->size <= elf_gp_size (htab->elf.dynobj))
2993 srel = htab->relsbss;
2994 else
2995 srel = htab->relbss;
2996 BFD_ASSERT (srel != NULL);
2997 srel->_raw_size += sizeof (Elf32_External_Rela);
2998 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_COPY;
3001 /* We need to figure out the alignment required for this symbol. I
3002 have no idea how ELF linkers handle this. */
3003 power_of_two = bfd_log2 (h->size);
3004 if (power_of_two > 4)
3005 power_of_two = 4;
3007 /* Apply the required alignment. */
3008 s->_raw_size = BFD_ALIGN (s->_raw_size,
3009 (bfd_size_type) (1 << power_of_two));
3010 if (power_of_two > bfd_get_section_alignment (htab->elf.dynobj, s))
3012 if (! bfd_set_section_alignment (htab->elf.dynobj, s, power_of_two))
3013 return FALSE;
3016 /* Define the symbol as being at this point in the section. */
3017 h->root.u.def.section = s;
3018 h->root.u.def.value = s->_raw_size;
3020 /* Increment the section size to make room for the symbol. */
3021 s->_raw_size += h->size;
3023 return TRUE;
3026 /* This is the condition under which finish_dynamic_symbol will be
3027 called from elflink.h. If elflink.h doesn't call our
3028 finish_dynamic_symbol routine, we'll need to do something about
3029 initializing any .plt and .got entries in relocate_section. */
3030 #define WILL_CALL_FINISH_DYNAMIC_SYMBOL(DYN, SHARED, H) \
3031 ((DYN) \
3032 && ((SHARED) \
3033 || ((H)->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0) \
3034 && ((H)->dynindx != -1 \
3035 || ((H)->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0))
3037 /* Of those relocs that might be copied as dynamic relocs, this macro
3038 selects those that must be copied when linking a shared library,
3039 even when the symbol is local. */
3041 #define MUST_BE_DYN_RELOC(RTYPE) \
3042 ((RTYPE) != R_PPC_REL24 \
3043 && (RTYPE) != R_PPC_REL14 \
3044 && (RTYPE) != R_PPC_REL14_BRTAKEN \
3045 && (RTYPE) != R_PPC_REL14_BRNTAKEN \
3046 && (RTYPE) != R_PPC_REL32)
3048 /* Allocate space in associated reloc sections for dynamic relocs. */
3050 static bfd_boolean
3051 allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
3053 struct bfd_link_info *info = inf;
3054 struct ppc_elf_link_hash_entry *eh;
3055 struct ppc_elf_link_hash_table *htab;
3056 struct ppc_elf_dyn_relocs *p;
3058 if (h->root.type == bfd_link_hash_indirect)
3059 return TRUE;
3061 if (h->root.type == bfd_link_hash_warning)
3062 /* When warning symbols are created, they **replace** the "real"
3063 entry in the hash table, thus we never get to see the real
3064 symbol in a hash traversal. So look at it now. */
3065 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3067 htab = ppc_elf_hash_table (info);
3068 if (htab->elf.dynamic_sections_created
3069 && h->plt.refcount > 0)
3071 /* Make sure this symbol is output as a dynamic symbol. */
3072 if (h->dynindx == -1
3073 && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
3075 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
3076 return FALSE;
3079 if (info->shared
3080 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h))
3082 asection *s = htab->plt;
3084 /* If this is the first .plt entry, make room for the special
3085 first entry. */
3086 if (s->_raw_size == 0)
3087 s->_raw_size += PLT_INITIAL_ENTRY_SIZE;
3089 /* The PowerPC PLT is actually composed of two parts, the
3090 first part is 2 words (for a load and a jump), and then
3091 there is a remaining word available at the end. */
3092 h->plt.offset = (PLT_INITIAL_ENTRY_SIZE
3093 + (PLT_SLOT_SIZE
3094 * ((s->_raw_size - PLT_INITIAL_ENTRY_SIZE)
3095 / PLT_ENTRY_SIZE)));
3097 /* If this symbol is not defined in a regular file, and we
3098 are not generating a shared library, then set the symbol
3099 to this location in the .plt. This is required to make
3100 function pointers compare as equal between the normal
3101 executable and the shared library. */
3102 if (! info->shared
3103 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
3105 h->root.u.def.section = s;
3106 h->root.u.def.value = h->plt.offset;
3109 /* Make room for this entry. After the 8192nd entry, room
3110 for two entries is allocated. */
3111 s->_raw_size += PLT_ENTRY_SIZE;
3112 if ((s->_raw_size - PLT_INITIAL_ENTRY_SIZE) / PLT_ENTRY_SIZE
3113 > PLT_NUM_SINGLE_ENTRIES)
3114 s->_raw_size += PLT_ENTRY_SIZE;
3116 /* We also need to make an entry in the .rela.plt section. */
3117 htab->relplt->_raw_size += sizeof (Elf32_External_Rela);
3119 else
3121 h->plt.offset = (bfd_vma) -1;
3122 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
3125 else
3127 h->plt.offset = (bfd_vma) -1;
3128 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
3131 eh = (struct ppc_elf_link_hash_entry *) h;
3132 if (eh->elf.got.refcount > 0)
3134 /* Make sure this symbol is output as a dynamic symbol. */
3135 if (eh->elf.dynindx == -1
3136 && (eh->elf.elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
3138 if (!bfd_elf32_link_record_dynamic_symbol (info, &eh->elf))
3139 return FALSE;
3142 if (eh->tls_mask == (TLS_TLS | TLS_LD)
3143 && !(eh->elf.elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC))
3144 /* If just an LD reloc, we'll just use htab->tlsld_got.offset. */
3145 eh->elf.got.offset = (bfd_vma) -1;
3146 else
3148 bfd_boolean dyn;
3149 eh->elf.got.offset = htab->got->_raw_size;
3150 if ((eh->tls_mask & TLS_TLS) != 0)
3152 if ((eh->tls_mask & TLS_LD) != 0)
3153 htab->got->_raw_size += 8;
3154 if ((eh->tls_mask & TLS_GD) != 0)
3155 htab->got->_raw_size += 8;
3156 if ((eh->tls_mask & (TLS_TPREL | TLS_TPRELGD)) != 0)
3157 htab->got->_raw_size += 4;
3158 if ((eh->tls_mask & TLS_DTPREL) != 0)
3159 htab->got->_raw_size += 4;
3161 else
3162 htab->got->_raw_size += 4;
3163 dyn = htab->elf.dynamic_sections_created;
3164 if ((info->shared
3165 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, &eh->elf))
3166 && (ELF_ST_VISIBILITY (eh->elf.other) == STV_DEFAULT
3167 || eh->elf.root.type != bfd_link_hash_undefweak))
3169 /* All the entries we allocated need relocs. */
3170 htab->relgot->_raw_size
3171 += ((htab->got->_raw_size - eh->elf.got.offset) / 4
3172 * sizeof (Elf32_External_Rela));
3173 /* Except LD only needs one. */
3174 if ((eh->tls_mask & TLS_LD) != 0)
3175 htab->relgot->_raw_size -= sizeof (Elf32_External_Rela);
3179 else
3180 eh->elf.got.offset = (bfd_vma) -1;
3182 if (eh->dyn_relocs == NULL)
3183 return TRUE;
3185 /* In the shared -Bsymbolic case, discard space allocated for
3186 dynamic pc-relative relocs against symbols which turn out to be
3187 defined in regular objects. For the normal shared case, discard
3188 space for relocs that have become local due to symbol visibility
3189 changes. */
3191 if (info->shared)
3193 /* Relocs that use pc_count are those that appear on a call insn,
3194 or certain REL relocs (see MUST_BE_DYN_RELOC) that can be
3195 generated via assembly. We want calls to protected symbols to
3196 resolve directly to the function rather than going via the plt.
3197 If people want function pointer comparisons to work as expected
3198 then they should avoid writing weird assembly. */
3199 if (SYMBOL_CALLS_LOCAL (info, h))
3201 struct ppc_elf_dyn_relocs **pp;
3203 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
3205 p->count -= p->pc_count;
3206 p->pc_count = 0;
3207 if (p->count == 0)
3208 *pp = p->next;
3209 else
3210 pp = &p->next;
3214 /* Also discard relocs on undefined weak syms with non-default
3215 visibility. */
3216 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
3217 && h->root.type == bfd_link_hash_undefweak)
3218 eh->dyn_relocs = NULL;
3220 else if (ELIMINATE_COPY_RELOCS)
3222 /* For the non-shared case, discard space for relocs against
3223 symbols which turn out to need copy relocs or are not
3224 dynamic. */
3226 if ((h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0
3227 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
3228 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
3230 /* Make sure this symbol is output as a dynamic symbol.
3231 Undefined weak syms won't yet be marked as dynamic. */
3232 if (h->dynindx == -1
3233 && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
3235 if (! bfd_elf64_link_record_dynamic_symbol (info, h))
3236 return FALSE;
3239 /* If that succeeded, we know we'll be keeping all the
3240 relocs. */
3241 if (h->dynindx != -1)
3242 goto keep;
3245 eh->dyn_relocs = NULL;
3247 keep: ;
3250 /* Finally, allocate space. */
3251 for (p = eh->dyn_relocs; p != NULL; p = p->next)
3253 asection *sreloc = elf_section_data (p->sec)->sreloc;
3254 sreloc->_raw_size += p->count * sizeof (Elf32_External_Rela);
3257 return TRUE;
3260 /* Find any dynamic relocs that apply to read-only sections. */
3262 static bfd_boolean
3263 readonly_dynrelocs (struct elf_link_hash_entry *h, void *info)
3265 struct ppc_elf_dyn_relocs *p;
3267 if (h->root.type == bfd_link_hash_indirect)
3268 return TRUE;
3270 if (h->root.type == bfd_link_hash_warning)
3271 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3273 for (p = ppc_elf_hash_entry (h)->dyn_relocs; p != NULL; p = p->next)
3275 asection *s = p->sec->output_section;
3277 if (s != NULL
3278 && ((s->flags & (SEC_READONLY | SEC_ALLOC))
3279 == (SEC_READONLY | SEC_ALLOC)))
3281 ((struct bfd_link_info *) info)->flags |= DF_TEXTREL;
3283 /* Not an error, just cut short the traversal. */
3284 return FALSE;
3287 return TRUE;
3290 /* Set the sizes of the dynamic sections. */
3292 static bfd_boolean
3293 ppc_elf_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
3294 struct bfd_link_info *info)
3296 struct ppc_elf_link_hash_table *htab;
3297 asection *s;
3298 bfd_boolean relocs;
3299 bfd *ibfd;
3301 #ifdef DEBUG
3302 fprintf (stderr, "ppc_elf_size_dynamic_sections called\n");
3303 #endif
3305 htab = ppc_elf_hash_table (info);
3306 BFD_ASSERT (htab->elf.dynobj != NULL);
3308 if (elf_hash_table (info)->dynamic_sections_created)
3310 /* Set the contents of the .interp section to the interpreter. */
3311 if (info->executable)
3313 s = bfd_get_section_by_name (htab->elf.dynobj, ".interp");
3314 BFD_ASSERT (s != NULL);
3315 s->_raw_size = sizeof ELF_DYNAMIC_INTERPRETER;
3316 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
3320 if (htab->tlsld_got.refcount > 0)
3322 htab->tlsld_got.offset = htab->got->_raw_size;
3323 htab->got->_raw_size += 8;
3324 if (info->shared)
3325 htab->relgot->_raw_size += sizeof (Elf32_External_Rela);
3327 else
3328 htab->tlsld_got.offset = (bfd_vma) -1;
3330 /* Set up .got offsets for local syms, and space for local dynamic
3331 relocs. */
3332 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
3334 bfd_signed_vma *local_got;
3335 bfd_signed_vma *end_local_got;
3336 char *lgot_masks;
3337 bfd_size_type locsymcount;
3338 Elf_Internal_Shdr *symtab_hdr;
3339 asection *srel;
3341 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
3342 continue;
3344 for (s = ibfd->sections; s != NULL; s = s->next)
3346 struct ppc_elf_dyn_relocs *p;
3348 for (p = ((struct ppc_elf_dyn_relocs *)
3349 elf_section_data (s)->local_dynrel);
3350 p != NULL;
3351 p = p->next)
3353 if (!bfd_is_abs_section (p->sec)
3354 && bfd_is_abs_section (p->sec->output_section))
3356 /* Input section has been discarded, either because
3357 it is a copy of a linkonce section or due to
3358 linker script /DISCARD/, so we'll be discarding
3359 the relocs too. */
3361 else if (p->count != 0)
3363 elf_section_data (p->sec)->sreloc->_raw_size
3364 += p->count * sizeof (Elf32_External_Rela);
3365 if ((p->sec->output_section->flags
3366 & (SEC_READONLY | SEC_ALLOC))
3367 == (SEC_READONLY | SEC_ALLOC))
3368 info->flags |= DF_TEXTREL;
3373 local_got = elf_local_got_refcounts (ibfd);
3374 if (!local_got)
3375 continue;
3377 symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
3378 locsymcount = symtab_hdr->sh_info;
3379 end_local_got = local_got + locsymcount;
3380 lgot_masks = (char *) end_local_got;
3381 s = htab->got;
3382 srel = htab->relgot;
3383 for (; local_got < end_local_got; ++local_got, ++lgot_masks)
3384 if (*local_got > 0)
3386 if (*lgot_masks == (TLS_TLS | TLS_LD))
3388 /* If just an LD reloc, we'll just use
3389 htab->tlsld_got.offset. */
3390 if (htab->tlsld_got.offset == (bfd_vma) -1)
3392 htab->tlsld_got.offset = s->_raw_size;
3393 s->_raw_size += 8;
3394 if (info->shared)
3395 srel->_raw_size += sizeof (Elf32_External_Rela);
3397 *local_got = (bfd_vma) -1;
3399 else
3401 *local_got = s->_raw_size;
3402 if ((*lgot_masks & TLS_TLS) != 0)
3404 if ((*lgot_masks & TLS_GD) != 0)
3405 s->_raw_size += 8;
3406 if ((*lgot_masks & (TLS_TPREL | TLS_TPRELGD)) != 0)
3407 s->_raw_size += 4;
3408 if ((*lgot_masks & TLS_DTPREL) != 0)
3409 s->_raw_size += 4;
3411 else
3412 s->_raw_size += 4;
3413 if (info->shared)
3414 srel->_raw_size += ((s->_raw_size - *local_got) / 4
3415 * sizeof (Elf32_External_Rela));
3418 else
3419 *local_got = (bfd_vma) -1;
3422 /* Allocate space for global sym dynamic relocs. */
3423 elf_link_hash_traverse (elf_hash_table (info), allocate_dynrelocs, info);
3425 /* We've now determined the sizes of the various dynamic sections.
3426 Allocate memory for them. */
3427 relocs = FALSE;
3428 for (s = htab->elf.dynobj->sections; s != NULL; s = s->next)
3430 if ((s->flags & SEC_LINKER_CREATED) == 0)
3431 continue;
3433 if (s == htab->plt
3434 || s == htab->got
3435 || (htab->sdata != NULL && s == htab->sdata->section)
3436 || (htab->sdata2 != NULL && s == htab->sdata2->section))
3438 /* Strip this section if we don't need it; see the
3439 comment below. */
3441 else if (strncmp (bfd_get_section_name (dynobj, s), ".rela", 5) == 0)
3443 if (s->_raw_size == 0)
3445 /* If we don't need this section, strip it from the
3446 output file. This is mostly to handle .rela.bss and
3447 .rela.plt. We must create both sections in
3448 create_dynamic_sections, because they must be created
3449 before the linker maps input sections to output
3450 sections. The linker does that before
3451 adjust_dynamic_symbol is called, and it is that
3452 function which decides whether anything needs to go
3453 into these sections. */
3455 else
3457 /* Remember whether there are any relocation sections. */
3458 relocs = TRUE;
3460 /* We use the reloc_count field as a counter if we need
3461 to copy relocs into the output file. */
3462 s->reloc_count = 0;
3465 else
3467 /* It's not one of our sections, so don't allocate space. */
3468 continue;
3471 if (s->_raw_size == 0)
3473 _bfd_strip_section_from_output (info, s);
3474 continue;
3477 /* Allocate memory for the section contents. */
3478 s->contents = bfd_zalloc (htab->elf.dynobj, s->_raw_size);
3479 if (s->contents == NULL)
3480 return FALSE;
3483 if (htab->elf.dynamic_sections_created)
3485 /* Add some entries to the .dynamic section. We fill in the
3486 values later, in ppc_elf_finish_dynamic_sections, but we
3487 must add the entries now so that we get the correct size for
3488 the .dynamic section. The DT_DEBUG entry is filled in by the
3489 dynamic linker and used by the debugger. */
3490 #define add_dynamic_entry(TAG, VAL) \
3491 bfd_elf32_add_dynamic_entry (info, (TAG), (VAL))
3493 if (info->executable)
3495 if (!add_dynamic_entry (DT_DEBUG, 0))
3496 return FALSE;
3499 if (htab->plt != NULL && htab->plt->_raw_size != 0)
3501 if (!add_dynamic_entry (DT_PLTGOT, 0)
3502 || !add_dynamic_entry (DT_PLTRELSZ, 0)
3503 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
3504 || !add_dynamic_entry (DT_JMPREL, 0))
3505 return FALSE;
3508 if (relocs)
3510 if (!add_dynamic_entry (DT_RELA, 0)
3511 || !add_dynamic_entry (DT_RELASZ, 0)
3512 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf32_External_Rela)))
3513 return FALSE;
3516 /* If any dynamic relocs apply to a read-only section, then we
3517 need a DT_TEXTREL entry. */
3518 if ((info->flags & DF_TEXTREL) == 0)
3519 elf_link_hash_traverse (elf_hash_table (info), readonly_dynrelocs,
3520 info);
3522 if ((info->flags & DF_TEXTREL) != 0)
3524 if (!add_dynamic_entry (DT_TEXTREL, 0))
3525 return FALSE;
3528 #undef add_dynamic_entry
3530 return TRUE;
3533 static bfd_boolean
3534 update_local_sym_info (bfd *abfd,
3535 Elf_Internal_Shdr *symtab_hdr,
3536 unsigned long r_symndx,
3537 int tls_type)
3539 bfd_signed_vma *local_got_refcounts = elf_local_got_refcounts (abfd);
3540 char *local_got_tls_masks;
3542 if (local_got_refcounts == NULL)
3544 bfd_size_type size = symtab_hdr->sh_info;
3546 size *= sizeof (*local_got_refcounts) + sizeof (*local_got_tls_masks);
3547 local_got_refcounts = bfd_zalloc (abfd, size);
3548 if (local_got_refcounts == NULL)
3549 return FALSE;
3550 elf_local_got_refcounts (abfd) = local_got_refcounts;
3553 local_got_refcounts[r_symndx] += 1;
3554 local_got_tls_masks = (char *) (local_got_refcounts + symtab_hdr->sh_info);
3555 local_got_tls_masks[r_symndx] |= tls_type;
3556 return TRUE;
3559 static void
3560 bad_shared_reloc (bfd *abfd, enum elf_ppc_reloc_type r_type)
3562 (*_bfd_error_handler)
3563 (_("%s: relocation %s cannot be used when making a shared object"),
3564 bfd_archive_filename (abfd),
3565 ppc_elf_howto_table[r_type]->name);
3566 bfd_set_error (bfd_error_bad_value);
3569 /* Look through the relocs for a section during the first phase, and
3570 allocate space in the global offset table or procedure linkage
3571 table. */
3573 static bfd_boolean
3574 ppc_elf_check_relocs (bfd *abfd,
3575 struct bfd_link_info *info,
3576 asection *sec,
3577 const Elf_Internal_Rela *relocs)
3579 struct ppc_elf_link_hash_table *htab;
3580 Elf_Internal_Shdr *symtab_hdr;
3581 struct elf_link_hash_entry **sym_hashes;
3582 const Elf_Internal_Rela *rel;
3583 const Elf_Internal_Rela *rel_end;
3584 asection *sreloc;
3586 if (info->relocatable)
3587 return TRUE;
3589 #ifdef DEBUG
3590 fprintf (stderr, "ppc_elf_check_relocs called for section %s in %s\n",
3591 bfd_get_section_name (abfd, sec),
3592 bfd_archive_filename (abfd));
3593 #endif
3595 /* Initialize howto table if not already done. */
3596 if (!ppc_elf_howto_table[R_PPC_ADDR32])
3597 ppc_elf_howto_init ();
3599 /* Create the linker generated sections all the time so that the
3600 special symbols are created. */
3601 htab = ppc_elf_hash_table (info);
3602 if (htab->sdata == NULL)
3604 htab->sdata = ppc_elf_create_linker_section (abfd, info,
3605 LINKER_SECTION_SDATA);
3606 if (htab->sdata == NULL)
3607 return FALSE;
3610 if (htab->sdata2 == NULL)
3612 htab->sdata2 = ppc_elf_create_linker_section (abfd, info,
3613 LINKER_SECTION_SDATA2);
3614 if (htab->sdata2 == NULL)
3615 return FALSE;
3618 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
3619 sym_hashes = elf_sym_hashes (abfd);
3620 sreloc = NULL;
3622 rel_end = relocs + sec->reloc_count;
3623 for (rel = relocs; rel < rel_end; rel++)
3625 unsigned long r_symndx;
3626 enum elf_ppc_reloc_type r_type;
3627 struct elf_link_hash_entry *h;
3628 int tls_type = 0;
3630 r_symndx = ELF32_R_SYM (rel->r_info);
3631 if (r_symndx < symtab_hdr->sh_info)
3632 h = NULL;
3633 else
3634 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
3636 /* If a relocation refers to _GLOBAL_OFFSET_TABLE_, create the .got.
3637 This shows up in particular in an R_PPC_ADDR32 in the eabi
3638 startup code. */
3639 if (h && strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
3641 if (htab->got == NULL)
3643 if (htab->elf.dynobj == NULL)
3644 htab->elf.dynobj = abfd;
3645 if (!ppc_elf_create_got (htab->elf.dynobj, info))
3646 return FALSE;
3650 r_type = ELF32_R_TYPE (rel->r_info);
3651 switch (r_type)
3653 case R_PPC_GOT_TLSLD16:
3654 case R_PPC_GOT_TLSLD16_LO:
3655 case R_PPC_GOT_TLSLD16_HI:
3656 case R_PPC_GOT_TLSLD16_HA:
3657 htab->tlsld_got.refcount += 1;
3658 tls_type = TLS_TLS | TLS_LD;
3659 goto dogottls;
3661 case R_PPC_GOT_TLSGD16:
3662 case R_PPC_GOT_TLSGD16_LO:
3663 case R_PPC_GOT_TLSGD16_HI:
3664 case R_PPC_GOT_TLSGD16_HA:
3665 tls_type = TLS_TLS | TLS_GD;
3666 goto dogottls;
3668 case R_PPC_GOT_TPREL16:
3669 case R_PPC_GOT_TPREL16_LO:
3670 case R_PPC_GOT_TPREL16_HI:
3671 case R_PPC_GOT_TPREL16_HA:
3672 if (info->shared)
3673 info->flags |= DF_STATIC_TLS;
3674 tls_type = TLS_TLS | TLS_TPREL;
3675 goto dogottls;
3677 case R_PPC_GOT_DTPREL16:
3678 case R_PPC_GOT_DTPREL16_LO:
3679 case R_PPC_GOT_DTPREL16_HI:
3680 case R_PPC_GOT_DTPREL16_HA:
3681 tls_type = TLS_TLS | TLS_DTPREL;
3682 dogottls:
3683 sec->has_tls_reloc = 1;
3684 /* Fall thru */
3686 /* GOT16 relocations */
3687 case R_PPC_GOT16:
3688 case R_PPC_GOT16_LO:
3689 case R_PPC_GOT16_HI:
3690 case R_PPC_GOT16_HA:
3691 /* This symbol requires a global offset table entry. */
3692 if (htab->got == NULL)
3694 if (htab->elf.dynobj == NULL)
3695 htab->elf.dynobj = abfd;
3696 if (!ppc_elf_create_got (htab->elf.dynobj, info))
3697 return FALSE;
3699 if (h != NULL)
3701 h->got.refcount += 1;
3702 ppc_elf_hash_entry (h)->tls_mask |= tls_type;
3704 else
3705 /* This is a global offset table entry for a local symbol. */
3706 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx, tls_type))
3707 return FALSE;
3708 break;
3710 /* Indirect .sdata relocation. */
3711 case R_PPC_EMB_SDAI16:
3712 if (info->shared)
3714 bad_shared_reloc (abfd, r_type);
3715 return FALSE;
3717 if (!elf_create_pointer_linker_section (abfd, info,
3718 htab->sdata, h, rel))
3719 return FALSE;
3720 break;
3722 /* Indirect .sdata2 relocation. */
3723 case R_PPC_EMB_SDA2I16:
3724 if (info->shared)
3726 bad_shared_reloc (abfd, r_type);
3727 return FALSE;
3729 if (!elf_create_pointer_linker_section (abfd, info,
3730 htab->sdata2, h, rel))
3731 return FALSE;
3732 break;
3734 case R_PPC_SDAREL16:
3735 case R_PPC_EMB_SDA2REL:
3736 case R_PPC_EMB_SDA21:
3737 case R_PPC_EMB_RELSDA:
3738 case R_PPC_EMB_NADDR32:
3739 case R_PPC_EMB_NADDR16:
3740 case R_PPC_EMB_NADDR16_LO:
3741 case R_PPC_EMB_NADDR16_HI:
3742 case R_PPC_EMB_NADDR16_HA:
3743 if (info->shared)
3745 bad_shared_reloc (abfd, r_type);
3746 return FALSE;
3748 break;
3750 case R_PPC_PLT32:
3751 case R_PPC_PLTREL24:
3752 case R_PPC_PLTREL32:
3753 case R_PPC_PLT16_LO:
3754 case R_PPC_PLT16_HI:
3755 case R_PPC_PLT16_HA:
3756 #ifdef DEBUG
3757 fprintf (stderr, "Reloc requires a PLT entry\n");
3758 #endif
3759 /* This symbol requires a procedure linkage table entry. We
3760 actually build the entry in finish_dynamic_symbol,
3761 because this might be a case of linking PIC code without
3762 linking in any dynamic objects, in which case we don't
3763 need to generate a procedure linkage table after all. */
3765 if (h == NULL)
3767 /* It does not make sense to have a procedure linkage
3768 table entry for a local symbol. */
3769 (*_bfd_error_handler) (_("%s(%s+0x%lx): %s reloc against "
3770 "local symbol"),
3771 bfd_archive_filename (abfd),
3772 sec->name,
3773 (long) rel->r_offset,
3774 ppc_elf_howto_table[r_type]->name);
3775 bfd_set_error (bfd_error_bad_value);
3776 return FALSE;
3779 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
3780 h->plt.refcount++;
3781 break;
3783 /* The following relocations don't need to propagate the
3784 relocation if linking a shared object since they are
3785 section relative. */
3786 case R_PPC_SECTOFF:
3787 case R_PPC_SECTOFF_LO:
3788 case R_PPC_SECTOFF_HI:
3789 case R_PPC_SECTOFF_HA:
3790 case R_PPC_DTPREL16:
3791 case R_PPC_DTPREL16_LO:
3792 case R_PPC_DTPREL16_HI:
3793 case R_PPC_DTPREL16_HA:
3794 case R_PPC_TOC16:
3795 break;
3797 /* This are just markers. */
3798 case R_PPC_TLS:
3799 case R_PPC_EMB_MRKREF:
3800 case R_PPC_NONE:
3801 case R_PPC_max:
3802 break;
3804 /* These should only appear in dynamic objects. */
3805 case R_PPC_COPY:
3806 case R_PPC_GLOB_DAT:
3807 case R_PPC_JMP_SLOT:
3808 case R_PPC_RELATIVE:
3809 break;
3811 /* These aren't handled yet. We'll report an error later. */
3812 case R_PPC_ADDR30:
3813 case R_PPC_EMB_RELSEC16:
3814 case R_PPC_EMB_RELST_LO:
3815 case R_PPC_EMB_RELST_HI:
3816 case R_PPC_EMB_RELST_HA:
3817 case R_PPC_EMB_BIT_FLD:
3818 break;
3820 /* This refers only to functions defined in the shared library. */
3821 case R_PPC_LOCAL24PC:
3822 break;
3824 /* This relocation describes the C++ object vtable hierarchy.
3825 Reconstruct it for later use during GC. */
3826 case R_PPC_GNU_VTINHERIT:
3827 if (!_bfd_elf32_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
3828 return FALSE;
3829 break;
3831 /* This relocation describes which C++ vtable entries are actually
3832 used. Record for later use during GC. */
3833 case R_PPC_GNU_VTENTRY:
3834 if (!_bfd_elf32_gc_record_vtentry (abfd, sec, h, rel->r_addend))
3835 return FALSE;
3836 break;
3838 /* We shouldn't really be seeing these. */
3839 case R_PPC_TPREL32:
3840 if (info->shared)
3841 info->flags |= DF_STATIC_TLS;
3842 goto dodyn;
3844 /* Nor these. */
3845 case R_PPC_DTPMOD32:
3846 case R_PPC_DTPREL32:
3847 goto dodyn;
3849 case R_PPC_TPREL16:
3850 case R_PPC_TPREL16_LO:
3851 case R_PPC_TPREL16_HI:
3852 case R_PPC_TPREL16_HA:
3853 if (info->shared)
3854 info->flags |= DF_STATIC_TLS;
3855 goto dodyn;
3857 /* When creating a shared object, we must copy these
3858 relocs into the output file. We create a reloc
3859 section in dynobj and make room for the reloc. */
3860 case R_PPC_REL24:
3861 case R_PPC_REL14:
3862 case R_PPC_REL14_BRTAKEN:
3863 case R_PPC_REL14_BRNTAKEN:
3864 case R_PPC_REL32:
3865 if (h == NULL
3866 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
3867 break;
3868 /* fall through */
3870 case R_PPC_ADDR32:
3871 case R_PPC_ADDR24:
3872 case R_PPC_ADDR16:
3873 case R_PPC_ADDR16_LO:
3874 case R_PPC_ADDR16_HI:
3875 case R_PPC_ADDR16_HA:
3876 case R_PPC_ADDR14:
3877 case R_PPC_ADDR14_BRTAKEN:
3878 case R_PPC_ADDR14_BRNTAKEN:
3879 case R_PPC_UADDR32:
3880 case R_PPC_UADDR16:
3881 if (h != NULL && !info->shared)
3883 /* We may need a plt entry if the symbol turns out to be
3884 a function defined in a dynamic object. */
3885 h->plt.refcount++;
3887 /* We may need a copy reloc too. */
3888 h->elf_link_hash_flags |= ELF_LINK_NON_GOT_REF;
3891 dodyn:
3892 /* If we are creating a shared library, and this is a reloc
3893 against a global symbol, or a non PC relative reloc
3894 against a local symbol, then we need to copy the reloc
3895 into the shared library. However, if we are linking with
3896 -Bsymbolic, we do not need to copy a reloc against a
3897 global symbol which is defined in an object we are
3898 including in the link (i.e., DEF_REGULAR is set). At
3899 this point we have not seen all the input files, so it is
3900 possible that DEF_REGULAR is not set now but will be set
3901 later (it is never cleared). In case of a weak definition,
3902 DEF_REGULAR may be cleared later by a strong definition in
3903 a shared library. We account for that possibility below by
3904 storing information in the dyn_relocs field of the hash
3905 table entry. A similar situation occurs when creating
3906 shared libraries and symbol visibility changes render the
3907 symbol local.
3909 If on the other hand, we are creating an executable, we
3910 may need to keep relocations for symbols satisfied by a
3911 dynamic library if we manage to avoid copy relocs for the
3912 symbol. */
3913 if ((info->shared
3914 && (MUST_BE_DYN_RELOC (r_type)
3915 || (h != NULL
3916 && (! info->symbolic
3917 || h->root.type == bfd_link_hash_defweak
3918 || (h->elf_link_hash_flags
3919 & ELF_LINK_HASH_DEF_REGULAR) == 0))))
3920 || (ELIMINATE_COPY_RELOCS
3921 && !info->shared
3922 && (sec->flags & SEC_ALLOC) != 0
3923 && h != NULL
3924 && (h->root.type == bfd_link_hash_defweak
3925 || (h->elf_link_hash_flags
3926 & ELF_LINK_HASH_DEF_REGULAR) == 0)))
3928 struct ppc_elf_dyn_relocs *p;
3929 struct ppc_elf_dyn_relocs **head;
3931 #ifdef DEBUG
3932 fprintf (stderr,
3933 "ppc_elf_check_relocs needs to "
3934 "create relocation for %s\n",
3935 (h && h->root.root.string
3936 ? h->root.root.string : "<unknown>"));
3937 #endif
3938 if (sreloc == NULL)
3940 const char *name;
3942 name = (bfd_elf_string_from_elf_section
3943 (abfd,
3944 elf_elfheader (abfd)->e_shstrndx,
3945 elf_section_data (sec)->rel_hdr.sh_name));
3946 if (name == NULL)
3947 return FALSE;
3949 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
3950 && strcmp (bfd_get_section_name (abfd, sec),
3951 name + 5) == 0);
3953 sreloc = bfd_get_section_by_name (htab->elf.dynobj, name);
3954 if (sreloc == NULL)
3956 flagword flags;
3958 sreloc = bfd_make_section (htab->elf.dynobj, name);
3959 flags = (SEC_HAS_CONTENTS | SEC_READONLY
3960 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3961 if ((sec->flags & SEC_ALLOC) != 0)
3962 flags |= SEC_ALLOC | SEC_LOAD;
3963 if (sreloc == NULL
3964 || ! bfd_set_section_flags (htab->elf.dynobj,
3965 sreloc, flags)
3966 || ! bfd_set_section_alignment (htab->elf.dynobj,
3967 sreloc, 2))
3968 return FALSE;
3970 elf_section_data (sec)->sreloc = sreloc;
3973 /* If this is a global symbol, we count the number of
3974 relocations we need for this symbol. */
3975 if (h != NULL)
3977 head = &ppc_elf_hash_entry (h)->dyn_relocs;
3979 else
3981 /* Track dynamic relocs needed for local syms too.
3982 We really need local syms available to do this
3983 easily. Oh well. */
3985 asection *s;
3986 s = bfd_section_from_r_symndx (abfd, &htab->sym_sec,
3987 sec, r_symndx);
3988 if (s == NULL)
3989 return FALSE;
3991 head = ((struct ppc_elf_dyn_relocs **)
3992 &elf_section_data (s)->local_dynrel);
3995 p = *head;
3996 if (p == NULL || p->sec != sec)
3998 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
3999 if (p == NULL)
4000 return FALSE;
4001 p->next = *head;
4002 *head = p;
4003 p->sec = sec;
4004 p->count = 0;
4005 p->pc_count = 0;
4008 p->count += 1;
4009 if (!MUST_BE_DYN_RELOC (r_type))
4010 p->pc_count += 1;
4013 break;
4017 return TRUE;
4020 /* Return the section that should be marked against GC for a given
4021 relocation. */
4023 static asection *
4024 ppc_elf_gc_mark_hook (asection *sec,
4025 struct bfd_link_info *info ATTRIBUTE_UNUSED,
4026 Elf_Internal_Rela *rel,
4027 struct elf_link_hash_entry *h,
4028 Elf_Internal_Sym *sym)
4030 if (h != NULL)
4032 switch (ELF32_R_TYPE (rel->r_info))
4034 case R_PPC_GNU_VTINHERIT:
4035 case R_PPC_GNU_VTENTRY:
4036 break;
4038 default:
4039 switch (h->root.type)
4041 case bfd_link_hash_defined:
4042 case bfd_link_hash_defweak:
4043 return h->root.u.def.section;
4045 case bfd_link_hash_common:
4046 return h->root.u.c.p->section;
4048 default:
4049 break;
4053 else
4054 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
4056 return NULL;
4059 /* Update the got, plt and dynamic reloc reference counts for the
4060 section being removed. */
4062 static bfd_boolean
4063 ppc_elf_gc_sweep_hook (bfd *abfd,
4064 struct bfd_link_info *info,
4065 asection *sec,
4066 const Elf_Internal_Rela *relocs)
4068 struct ppc_elf_link_hash_table *htab;
4069 Elf_Internal_Shdr *symtab_hdr;
4070 struct elf_link_hash_entry **sym_hashes;
4071 bfd_signed_vma *local_got_refcounts;
4072 const Elf_Internal_Rela *rel, *relend;
4074 elf_section_data (sec)->local_dynrel = NULL;
4076 htab = ppc_elf_hash_table (info);
4077 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
4078 sym_hashes = elf_sym_hashes (abfd);
4079 local_got_refcounts = elf_local_got_refcounts (abfd);
4081 relend = relocs + sec->reloc_count;
4082 for (rel = relocs; rel < relend; rel++)
4084 unsigned long r_symndx;
4085 enum elf_ppc_reloc_type r_type;
4086 struct elf_link_hash_entry *h = NULL;
4088 r_symndx = ELF32_R_SYM (rel->r_info);
4089 if (r_symndx >= symtab_hdr->sh_info)
4091 struct ppc_elf_dyn_relocs **pp, *p;
4092 struct ppc_elf_link_hash_entry *eh;
4094 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
4095 eh = (struct ppc_elf_link_hash_entry *) h;
4097 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
4098 if (p->sec == sec)
4100 /* Everything must go for SEC. */
4101 *pp = p->next;
4102 break;
4106 r_type = ELF32_R_TYPE (rel->r_info);
4107 switch (r_type)
4109 case R_PPC_GOT_TLSLD16:
4110 case R_PPC_GOT_TLSLD16_LO:
4111 case R_PPC_GOT_TLSLD16_HI:
4112 case R_PPC_GOT_TLSLD16_HA:
4113 htab->tlsld_got.refcount -= 1;
4114 /* Fall thru */
4116 case R_PPC_GOT_TLSGD16:
4117 case R_PPC_GOT_TLSGD16_LO:
4118 case R_PPC_GOT_TLSGD16_HI:
4119 case R_PPC_GOT_TLSGD16_HA:
4120 case R_PPC_GOT_TPREL16:
4121 case R_PPC_GOT_TPREL16_LO:
4122 case R_PPC_GOT_TPREL16_HI:
4123 case R_PPC_GOT_TPREL16_HA:
4124 case R_PPC_GOT_DTPREL16:
4125 case R_PPC_GOT_DTPREL16_LO:
4126 case R_PPC_GOT_DTPREL16_HI:
4127 case R_PPC_GOT_DTPREL16_HA:
4128 case R_PPC_GOT16:
4129 case R_PPC_GOT16_LO:
4130 case R_PPC_GOT16_HI:
4131 case R_PPC_GOT16_HA:
4132 if (h != NULL)
4134 if (h->got.refcount > 0)
4135 h->got.refcount--;
4137 else if (local_got_refcounts != NULL)
4139 if (local_got_refcounts[r_symndx] > 0)
4140 local_got_refcounts[r_symndx]--;
4142 break;
4144 case R_PPC_REL24:
4145 case R_PPC_REL14:
4146 case R_PPC_REL14_BRTAKEN:
4147 case R_PPC_REL14_BRNTAKEN:
4148 case R_PPC_REL32:
4149 if (h == NULL
4150 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
4151 break;
4152 /* Fall thru */
4154 case R_PPC_ADDR32:
4155 case R_PPC_ADDR24:
4156 case R_PPC_ADDR16:
4157 case R_PPC_ADDR16_LO:
4158 case R_PPC_ADDR16_HI:
4159 case R_PPC_ADDR16_HA:
4160 case R_PPC_ADDR14:
4161 case R_PPC_ADDR14_BRTAKEN:
4162 case R_PPC_ADDR14_BRNTAKEN:
4163 case R_PPC_UADDR32:
4164 case R_PPC_UADDR16:
4165 case R_PPC_PLT32:
4166 case R_PPC_PLTREL24:
4167 case R_PPC_PLT16_LO:
4168 case R_PPC_PLT16_HI:
4169 case R_PPC_PLT16_HA:
4170 if (h != NULL)
4172 if (h->plt.refcount > 0)
4173 h->plt.refcount--;
4175 break;
4177 default:
4178 break;
4181 return TRUE;
4184 /* Set htab->tls_get_addr and call the generic ELF tls_setup function. */
4186 asection *
4187 ppc_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
4189 struct ppc_elf_link_hash_table *htab;
4191 htab = ppc_elf_hash_table (info);
4192 htab->tls_get_addr = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
4193 FALSE, FALSE, TRUE);
4195 return _bfd_elf_tls_setup (obfd, info);
4198 /* Run through all the TLS relocs looking for optimization
4199 opportunities. */
4201 bfd_boolean
4202 ppc_elf_tls_optimize (bfd *obfd ATTRIBUTE_UNUSED,
4203 struct bfd_link_info *info)
4205 bfd *ibfd;
4206 asection *sec;
4207 struct ppc_elf_link_hash_table *htab;
4209 if (info->relocatable || info->shared)
4210 return TRUE;
4212 htab = ppc_elf_hash_table (info);
4213 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
4215 Elf_Internal_Sym *locsyms = NULL;
4216 Elf_Internal_Shdr *symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
4218 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
4219 if (sec->has_tls_reloc && !bfd_is_abs_section (sec->output_section))
4221 Elf_Internal_Rela *relstart, *rel, *relend;
4222 int expecting_tls_get_addr;
4224 /* Read the relocations. */
4225 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
4226 info->keep_memory);
4227 if (relstart == NULL)
4228 return FALSE;
4230 expecting_tls_get_addr = 0;
4231 relend = relstart + sec->reloc_count;
4232 for (rel = relstart; rel < relend; rel++)
4234 enum elf_ppc_reloc_type r_type;
4235 unsigned long r_symndx;
4236 struct elf_link_hash_entry *h = NULL;
4237 char *tls_mask;
4238 char tls_set, tls_clear;
4239 bfd_boolean is_local;
4241 r_symndx = ELF32_R_SYM (rel->r_info);
4242 if (r_symndx >= symtab_hdr->sh_info)
4244 struct elf_link_hash_entry **sym_hashes;
4246 sym_hashes = elf_sym_hashes (ibfd);
4247 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
4248 while (h->root.type == bfd_link_hash_indirect
4249 || h->root.type == bfd_link_hash_warning)
4250 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4253 is_local = FALSE;
4254 if (h == NULL
4255 || !(h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC))
4256 is_local = TRUE;
4258 r_type = ELF32_R_TYPE (rel->r_info);
4259 switch (r_type)
4261 case R_PPC_GOT_TLSLD16:
4262 case R_PPC_GOT_TLSLD16_LO:
4263 case R_PPC_GOT_TLSLD16_HI:
4264 case R_PPC_GOT_TLSLD16_HA:
4265 /* These relocs should never be against a symbol
4266 defined in a shared lib. Leave them alone if
4267 that turns out to be the case. */
4268 expecting_tls_get_addr = 0;
4269 htab->tlsld_got.refcount -= 1;
4270 if (!is_local)
4271 continue;
4273 /* LD -> LE */
4274 tls_set = 0;
4275 tls_clear = TLS_LD;
4276 expecting_tls_get_addr = 1;
4277 break;
4279 case R_PPC_GOT_TLSGD16:
4280 case R_PPC_GOT_TLSGD16_LO:
4281 case R_PPC_GOT_TLSGD16_HI:
4282 case R_PPC_GOT_TLSGD16_HA:
4283 if (is_local)
4284 /* GD -> LE */
4285 tls_set = 0;
4286 else
4287 /* GD -> IE */
4288 tls_set = TLS_TLS | TLS_TPRELGD;
4289 tls_clear = TLS_GD;
4290 expecting_tls_get_addr = 1;
4291 break;
4293 case R_PPC_GOT_TPREL16:
4294 case R_PPC_GOT_TPREL16_LO:
4295 case R_PPC_GOT_TPREL16_HI:
4296 case R_PPC_GOT_TPREL16_HA:
4297 expecting_tls_get_addr = 0;
4298 if (is_local)
4300 /* IE -> LE */
4301 tls_set = 0;
4302 tls_clear = TLS_TPREL;
4303 break;
4305 else
4306 continue;
4308 case R_PPC_REL14:
4309 case R_PPC_REL14_BRTAKEN:
4310 case R_PPC_REL14_BRNTAKEN:
4311 case R_PPC_REL24:
4312 if (expecting_tls_get_addr
4313 && h != NULL
4314 && h == htab->tls_get_addr)
4316 if (h->plt.refcount > 0)
4317 h->plt.refcount -= 1;
4319 expecting_tls_get_addr = 0;
4320 continue;
4322 default:
4323 expecting_tls_get_addr = 0;
4324 continue;
4327 if (h != NULL)
4329 if (tls_set == 0)
4331 /* We managed to get rid of a got entry. */
4332 if (h->got.refcount > 0)
4333 h->got.refcount -= 1;
4335 tls_mask = &ppc_elf_hash_entry (h)->tls_mask;
4337 else
4339 Elf_Internal_Sym *sym;
4340 bfd_signed_vma *lgot_refs;
4341 char *lgot_masks;
4343 if (locsyms == NULL)
4345 locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
4346 if (locsyms == NULL)
4347 locsyms = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
4348 symtab_hdr->sh_info,
4349 0, NULL, NULL, NULL);
4350 if (locsyms == NULL)
4352 if (elf_section_data (sec)->relocs != relstart)
4353 free (relstart);
4354 return FALSE;
4357 sym = locsyms + r_symndx;
4358 lgot_refs = elf_local_got_refcounts (ibfd);
4359 if (lgot_refs == NULL)
4360 abort ();
4361 if (tls_set == 0)
4363 /* We managed to get rid of a got entry. */
4364 if (lgot_refs[r_symndx] > 0)
4365 lgot_refs[r_symndx] -= 1;
4367 lgot_masks = (char *) (lgot_refs + symtab_hdr->sh_info);
4368 tls_mask = &lgot_masks[r_symndx];
4371 *tls_mask |= tls_set;
4372 *tls_mask &= ~tls_clear;
4375 if (elf_section_data (sec)->relocs != relstart)
4376 free (relstart);
4379 if (locsyms != NULL
4380 && (symtab_hdr->contents != (unsigned char *) locsyms))
4382 if (!info->keep_memory)
4383 free (locsyms);
4384 else
4385 symtab_hdr->contents = (unsigned char *) locsyms;
4388 return TRUE;
4391 /* Hook called by the linker routine which adds symbols from an object
4392 file. We use it to put .comm items in .sbss, and not .bss. */
4394 static bfd_boolean
4395 ppc_elf_add_symbol_hook (bfd *abfd,
4396 struct bfd_link_info *info,
4397 const Elf_Internal_Sym *sym,
4398 const char **namep ATTRIBUTE_UNUSED,
4399 flagword *flagsp ATTRIBUTE_UNUSED,
4400 asection **secp,
4401 bfd_vma *valp)
4403 if (sym->st_shndx == SHN_COMMON
4404 && !info->relocatable
4405 && sym->st_size <= elf_gp_size (abfd)
4406 && (info->hash->creator == abfd->xvec
4407 || info->hash->creator == abfd->xvec->alternative_target))
4409 /* Common symbols less than or equal to -G nn bytes are automatically
4410 put into .sbss. */
4411 struct ppc_elf_link_hash_table *htab;
4413 htab = ppc_elf_hash_table (info);
4414 if (htab->sbss == NULL)
4416 flagword flags = SEC_IS_COMMON;
4418 htab->sbss = bfd_make_section_anyway (abfd, ".sbss");
4419 if (htab->sbss == NULL
4420 || ! bfd_set_section_flags (abfd, htab->sbss, flags))
4421 return FALSE;
4424 *secp = htab->sbss;
4425 *valp = sym->st_size;
4428 return TRUE;
4431 /* Finish up dynamic symbol handling. We set the contents of various
4432 dynamic sections here. */
4434 static bfd_boolean
4435 ppc_elf_finish_dynamic_symbol (bfd *output_bfd,
4436 struct bfd_link_info *info,
4437 struct elf_link_hash_entry *h,
4438 Elf_Internal_Sym *sym)
4440 struct ppc_elf_link_hash_table *htab;
4442 #ifdef DEBUG
4443 fprintf (stderr, "ppc_elf_finish_dynamic_symbol called for %s",
4444 h->root.root.string);
4445 #endif
4447 htab = ppc_elf_hash_table (info);
4448 BFD_ASSERT (htab->elf.dynobj != NULL);
4450 if (h->plt.offset != (bfd_vma) -1)
4452 Elf_Internal_Rela rela;
4453 bfd_byte *loc;
4454 bfd_vma reloc_index;
4456 #ifdef DEBUG
4457 fprintf (stderr, ", plt_offset = %d", h->plt.offset);
4458 #endif
4460 /* This symbol has an entry in the procedure linkage table. Set
4461 it up. */
4463 BFD_ASSERT (h->dynindx != -1);
4464 BFD_ASSERT (htab->plt != NULL && htab->relplt != NULL);
4466 /* We don't need to fill in the .plt. The ppc dynamic linker
4467 will fill it in. */
4469 /* Fill in the entry in the .rela.plt section. */
4470 rela.r_offset = (htab->plt->output_section->vma
4471 + htab->plt->output_offset
4472 + h->plt.offset);
4473 rela.r_info = ELF32_R_INFO (h->dynindx, R_PPC_JMP_SLOT);
4474 rela.r_addend = 0;
4476 reloc_index = (h->plt.offset - PLT_INITIAL_ENTRY_SIZE) / PLT_SLOT_SIZE;
4477 if (reloc_index > PLT_NUM_SINGLE_ENTRIES)
4478 reloc_index -= (reloc_index - PLT_NUM_SINGLE_ENTRIES) / 2;
4479 loc = (htab->relplt->contents
4480 + reloc_index * sizeof (Elf32_External_Rela));
4481 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
4483 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
4485 /* Mark the symbol as undefined, rather than as defined in
4486 the .plt section. Leave the value alone. */
4487 sym->st_shndx = SHN_UNDEF;
4488 /* If the symbol is weak, we do need to clear the value.
4489 Otherwise, the PLT entry would provide a definition for
4490 the symbol even if the symbol wasn't defined anywhere,
4491 and so the symbol would never be NULL. */
4492 if ((h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR_NONWEAK)
4493 == 0)
4494 sym->st_value = 0;
4498 if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_COPY) != 0)
4500 asection *s;
4501 Elf_Internal_Rela rela;
4502 bfd_byte *loc;
4504 /* This symbols needs a copy reloc. Set it up. */
4506 #ifdef DEBUG
4507 fprintf (stderr, ", copy");
4508 #endif
4510 BFD_ASSERT (h->dynindx != -1);
4512 if (h->size <= elf_gp_size (htab->elf.dynobj))
4513 s = htab->relsbss;
4514 else
4515 s = htab->relbss;
4516 BFD_ASSERT (s != NULL);
4518 rela.r_offset = (h->root.u.def.value
4519 + h->root.u.def.section->output_section->vma
4520 + h->root.u.def.section->output_offset);
4521 rela.r_info = ELF32_R_INFO (h->dynindx, R_PPC_COPY);
4522 rela.r_addend = 0;
4523 loc = s->contents + s->reloc_count++ * sizeof (Elf32_External_Rela);
4524 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
4527 #ifdef DEBUG
4528 fprintf (stderr, "\n");
4529 #endif
4531 /* Mark some specially defined symbols as absolute. */
4532 if (strcmp (h->root.root.string, "_DYNAMIC") == 0
4533 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0
4534 || strcmp (h->root.root.string, "_PROCEDURE_LINKAGE_TABLE_") == 0)
4535 sym->st_shndx = SHN_ABS;
4537 return TRUE;
4540 /* Finish up the dynamic sections. */
4542 static bfd_boolean
4543 ppc_elf_finish_dynamic_sections (bfd *output_bfd,
4544 struct bfd_link_info *info)
4546 asection *sdyn;
4547 struct ppc_elf_link_hash_table *htab;
4549 #ifdef DEBUG
4550 fprintf (stderr, "ppc_elf_finish_dynamic_sections called\n");
4551 #endif
4553 htab = ppc_elf_hash_table (info);
4554 sdyn = bfd_get_section_by_name (htab->elf.dynobj, ".dynamic");
4556 if (htab->elf.dynamic_sections_created)
4558 Elf32_External_Dyn *dyncon, *dynconend;
4560 BFD_ASSERT (htab->plt != NULL && sdyn != NULL);
4562 dyncon = (Elf32_External_Dyn *) sdyn->contents;
4563 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->_raw_size);
4564 for (; dyncon < dynconend; dyncon++)
4566 Elf_Internal_Dyn dyn;
4567 asection *s;
4569 bfd_elf32_swap_dyn_in (htab->elf.dynobj, dyncon, &dyn);
4571 switch (dyn.d_tag)
4573 case DT_PLTGOT:
4574 s = htab->plt;
4575 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
4576 break;
4578 case DT_PLTRELSZ:
4579 dyn.d_un.d_val = htab->relplt->_raw_size;
4580 break;
4582 case DT_JMPREL:
4583 s = htab->relplt;
4584 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
4585 break;
4587 default:
4588 continue;
4591 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
4595 /* Add a blrl instruction at _GLOBAL_OFFSET_TABLE_-4 so that a function can
4596 easily find the address of the _GLOBAL_OFFSET_TABLE_. */
4597 if (htab->got)
4599 unsigned char *contents = htab->got->contents;
4600 bfd_put_32 (output_bfd, 0x4e800021 /* blrl */, contents);
4602 if (sdyn == NULL)
4603 bfd_put_32 (output_bfd, 0, contents + 4);
4604 else
4605 bfd_put_32 (output_bfd,
4606 sdyn->output_section->vma + sdyn->output_offset,
4607 contents + 4);
4609 elf_section_data (htab->got->output_section)->this_hdr.sh_entsize = 4;
4612 return TRUE;
4615 /* The RELOCATE_SECTION function is called by the ELF backend linker
4616 to handle the relocations for a section.
4618 The relocs are always passed as Rela structures; if the section
4619 actually uses Rel structures, the r_addend field will always be
4620 zero.
4622 This function is responsible for adjust the section contents as
4623 necessary, and (if using Rela relocs and generating a
4624 relocatable output file) adjusting the reloc addend as
4625 necessary.
4627 This function does not have to worry about setting the reloc
4628 address or the reloc symbol index.
4630 LOCAL_SYMS is a pointer to the swapped in local symbols.
4632 LOCAL_SECTIONS is an array giving the section in the input file
4633 corresponding to the st_shndx field of each local symbol.
4635 The global hash table entry for the global symbols can be found
4636 via elf_sym_hashes (input_bfd).
4638 When generating relocatable output, this function must handle
4639 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
4640 going to be the section symbol corresponding to the output
4641 section, which means that the addend must be adjusted
4642 accordingly. */
4644 static bfd_boolean
4645 ppc_elf_relocate_section (bfd *output_bfd,
4646 struct bfd_link_info *info,
4647 bfd *input_bfd,
4648 asection *input_section,
4649 bfd_byte *contents,
4650 Elf_Internal_Rela *relocs,
4651 Elf_Internal_Sym *local_syms,
4652 asection **local_sections)
4654 Elf_Internal_Shdr *symtab_hdr;
4655 struct elf_link_hash_entry **sym_hashes;
4656 struct ppc_elf_link_hash_table *htab;
4657 Elf_Internal_Rela *rel;
4658 Elf_Internal_Rela *relend;
4659 Elf_Internal_Rela outrel;
4660 bfd_byte *loc;
4661 asection *sreloc = NULL;
4662 bfd_vma *local_got_offsets;
4663 bfd_boolean ret = TRUE;
4665 #ifdef DEBUG
4666 fprintf (stderr, "ppc_elf_relocate_section called for %s section %s, "
4667 "%ld relocations%s\n",
4668 bfd_archive_filename (input_bfd),
4669 bfd_section_name(input_bfd, input_section),
4670 (long) input_section->reloc_count,
4671 (info->relocatable) ? " (relocatable)" : "");
4672 #endif
4674 if (info->relocatable)
4675 return TRUE;
4677 /* Initialize howto table if not already done. */
4678 if (!ppc_elf_howto_table[R_PPC_ADDR32])
4679 ppc_elf_howto_init ();
4681 htab = ppc_elf_hash_table (info);
4682 local_got_offsets = elf_local_got_offsets (input_bfd);
4683 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
4684 sym_hashes = elf_sym_hashes (input_bfd);
4685 rel = relocs;
4686 relend = relocs + input_section->reloc_count;
4687 for (; rel < relend; rel++)
4689 enum elf_ppc_reloc_type r_type;
4690 bfd_vma addend;
4691 bfd_reloc_status_type r;
4692 Elf_Internal_Sym *sym;
4693 asection *sec;
4694 struct elf_link_hash_entry *h;
4695 const char *sym_name;
4696 reloc_howto_type *howto;
4697 unsigned long r_symndx;
4698 bfd_vma relocation;
4699 bfd_vma branch_bit, insn, from;
4700 bfd_boolean unresolved_reloc;
4701 bfd_boolean warned;
4702 unsigned int tls_type, tls_mask, tls_gd;
4704 r_type = ELF32_R_TYPE (rel->r_info);
4705 sym = NULL;
4706 sec = NULL;
4707 h = NULL;
4708 unresolved_reloc = FALSE;
4709 warned = FALSE;
4710 r_symndx = ELF32_R_SYM (rel->r_info);
4712 if (r_symndx < symtab_hdr->sh_info)
4714 sym = local_syms + r_symndx;
4715 sec = local_sections[r_symndx];
4716 sym_name = bfd_elf_local_sym_name (input_bfd, sym);
4718 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
4720 else
4722 RELOC_FOR_GLOBAL_SYMBOL (h, sym_hashes, r_symndx,
4723 symtab_hdr, relocation, sec,
4724 unresolved_reloc, info,
4725 warned);
4727 sym_name = h->root.root.string;
4730 /* TLS optimizations. Replace instruction sequences and relocs
4731 based on information we collected in tls_optimize. We edit
4732 RELOCS so that --emit-relocs will output something sensible
4733 for the final instruction stream. */
4734 tls_mask = 0;
4735 tls_gd = 0;
4736 if (IS_PPC_TLS_RELOC (r_type))
4738 if (h != NULL)
4739 tls_mask = ((struct ppc_elf_link_hash_entry *) h)->tls_mask;
4740 else if (local_got_offsets != NULL)
4742 char *lgot_masks;
4743 lgot_masks = (char *) (local_got_offsets + symtab_hdr->sh_info);
4744 tls_mask = lgot_masks[r_symndx];
4748 /* Ensure reloc mapping code below stays sane. */
4749 if ((R_PPC_GOT_TLSLD16 & 3) != (R_PPC_GOT_TLSGD16 & 3)
4750 || (R_PPC_GOT_TLSLD16_LO & 3) != (R_PPC_GOT_TLSGD16_LO & 3)
4751 || (R_PPC_GOT_TLSLD16_HI & 3) != (R_PPC_GOT_TLSGD16_HI & 3)
4752 || (R_PPC_GOT_TLSLD16_HA & 3) != (R_PPC_GOT_TLSGD16_HA & 3)
4753 || (R_PPC_GOT_TLSLD16 & 3) != (R_PPC_GOT_TPREL16 & 3)
4754 || (R_PPC_GOT_TLSLD16_LO & 3) != (R_PPC_GOT_TPREL16_LO & 3)
4755 || (R_PPC_GOT_TLSLD16_HI & 3) != (R_PPC_GOT_TPREL16_HI & 3)
4756 || (R_PPC_GOT_TLSLD16_HA & 3) != (R_PPC_GOT_TPREL16_HA & 3))
4757 abort ();
4758 switch (r_type)
4760 default:
4761 break;
4763 case R_PPC_GOT_TPREL16:
4764 case R_PPC_GOT_TPREL16_LO:
4765 if (tls_mask != 0
4766 && (tls_mask & TLS_TPREL) == 0)
4768 bfd_vma insn;
4769 insn = bfd_get_32 (output_bfd, contents + rel->r_offset - 2);
4770 insn &= 31 << 21;
4771 insn |= 0x3c020000; /* addis 0,2,0 */
4772 bfd_put_32 (output_bfd, insn, contents + rel->r_offset - 2);
4773 r_type = R_PPC_TPREL16_HA;
4774 rel->r_info = ELF32_R_INFO (r_symndx, r_type);
4776 break;
4778 case R_PPC_TLS:
4779 if (tls_mask != 0
4780 && (tls_mask & TLS_TPREL) == 0)
4782 bfd_vma insn, rtra;
4783 insn = bfd_get_32 (output_bfd, contents + rel->r_offset);
4784 if ((insn & ((31 << 26) | (31 << 11)))
4785 == ((31 << 26) | (2 << 11)))
4786 rtra = insn & ((1 << 26) - (1 << 16));
4787 else if ((insn & ((31 << 26) | (31 << 16)))
4788 == ((31 << 26) | (2 << 16)))
4789 rtra = (insn & (31 << 21)) | ((insn & (31 << 11)) << 5);
4790 else
4791 abort ();
4792 if ((insn & ((1 << 11) - (1 << 1))) == 266 << 1)
4793 /* add -> addi. */
4794 insn = 14 << 26;
4795 else if ((insn & (31 << 1)) == 23 << 1
4796 && ((insn & (31 << 6)) < 14 << 6
4797 || ((insn & (31 << 6)) >= 16 << 6
4798 && (insn & (31 << 6)) < 24 << 6)))
4799 /* load and store indexed -> dform. */
4800 insn = (32 | ((insn >> 6) & 31)) << 26;
4801 else if ((insn & (31 << 1)) == 21 << 1
4802 && (insn & (0x1a << 6)) == 0)
4803 /* ldx, ldux, stdx, stdux -> ld, ldu, std, stdu. */
4804 insn = (((58 | ((insn >> 6) & 4)) << 26)
4805 | ((insn >> 6) & 1));
4806 else if ((insn & (31 << 1)) == 21 << 1
4807 && (insn & ((1 << 11) - (1 << 1))) == 341 << 1)
4808 /* lwax -> lwa. */
4809 insn = (58 << 26) | 2;
4810 else
4811 abort ();
4812 insn |= rtra;
4813 bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
4814 r_type = R_PPC_TPREL16_LO;
4815 rel->r_info = ELF32_R_INFO (r_symndx, r_type);
4816 /* Was PPC_TLS which sits on insn boundary, now
4817 PPC_TPREL16_LO which is at insn+2. */
4818 rel->r_offset += 2;
4820 break;
4822 case R_PPC_GOT_TLSGD16_HI:
4823 case R_PPC_GOT_TLSGD16_HA:
4824 tls_gd = TLS_TPRELGD;
4825 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
4826 goto tls_gdld_hi;
4827 break;
4829 case R_PPC_GOT_TLSLD16_HI:
4830 case R_PPC_GOT_TLSLD16_HA:
4831 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
4833 tls_gdld_hi:
4834 if ((tls_mask & tls_gd) != 0)
4835 r_type = (((r_type - (R_PPC_GOT_TLSGD16 & 3)) & 3)
4836 + R_PPC_GOT_TPREL16);
4837 else
4839 bfd_put_32 (output_bfd, NOP, contents + rel->r_offset);
4840 rel->r_offset -= 2;
4841 r_type = R_PPC_NONE;
4843 rel->r_info = ELF32_R_INFO (r_symndx, r_type);
4845 break;
4847 case R_PPC_GOT_TLSGD16:
4848 case R_PPC_GOT_TLSGD16_LO:
4849 tls_gd = TLS_TPRELGD;
4850 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
4851 goto tls_get_addr_check;
4852 break;
4854 case R_PPC_GOT_TLSLD16:
4855 case R_PPC_GOT_TLSLD16_LO:
4856 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
4858 tls_get_addr_check:
4859 if (rel + 1 < relend)
4861 enum elf_ppc_reloc_type r_type2;
4862 unsigned long r_symndx2;
4863 struct elf_link_hash_entry *h2;
4864 bfd_vma insn1, insn2;
4865 bfd_vma offset;
4867 /* The next instruction should be a call to
4868 __tls_get_addr. Peek at the reloc to be sure. */
4869 r_type2 = ELF32_R_TYPE (rel[1].r_info);
4870 r_symndx2 = ELF32_R_SYM (rel[1].r_info);
4871 if (r_symndx2 < symtab_hdr->sh_info
4872 || (r_type2 != R_PPC_REL14
4873 && r_type2 != R_PPC_REL14_BRTAKEN
4874 && r_type2 != R_PPC_REL14_BRNTAKEN
4875 && r_type2 != R_PPC_REL24
4876 && r_type2 != R_PPC_PLTREL24))
4877 break;
4879 h2 = sym_hashes[r_symndx2 - symtab_hdr->sh_info];
4880 while (h2->root.type == bfd_link_hash_indirect
4881 || h2->root.type == bfd_link_hash_warning)
4882 h2 = (struct elf_link_hash_entry *) h2->root.u.i.link;
4883 if (h2 == NULL || h2 != htab->tls_get_addr)
4884 break;
4886 /* OK, it checks out. Replace the call. */
4887 offset = rel[1].r_offset;
4888 insn1 = bfd_get_32 (output_bfd,
4889 contents + rel->r_offset - 2);
4890 if ((tls_mask & tls_gd) != 0)
4892 /* IE */
4893 insn1 &= (1 << 26) - 1;
4894 insn1 |= 32 << 26; /* lwz */
4895 insn2 = 0x7c631214; /* add 3,3,2 */
4896 rel[1].r_info = ELF32_R_INFO (r_symndx2, R_PPC_NONE);
4897 r_type = (((r_type - (R_PPC_GOT_TLSGD16 & 3)) & 3)
4898 + R_PPC_GOT_TPREL16);
4899 rel->r_info = ELF32_R_INFO (r_symndx, r_type);
4901 else
4903 /* LE */
4904 insn1 = 0x3c620000; /* addis 3,2,0 */
4905 insn2 = 0x38630000; /* addi 3,3,0 */
4906 if (tls_gd == 0)
4908 /* Was an LD reloc. */
4909 r_symndx = 0;
4910 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
4911 rel[1].r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
4913 r_type = R_PPC_TPREL16_HA;
4914 rel->r_info = ELF32_R_INFO (r_symndx, r_type);
4915 rel[1].r_info = ELF32_R_INFO (r_symndx,
4916 R_PPC_TPREL16_LO);
4917 rel[1].r_offset += 2;
4919 bfd_put_32 (output_bfd, insn1, contents + rel->r_offset - 2);
4920 bfd_put_32 (output_bfd, insn2, contents + offset);
4921 if (tls_gd == 0)
4923 /* We changed the symbol on an LD reloc. Start over
4924 in order to get h, sym, sec etc. right. */
4925 rel--;
4926 continue;
4930 break;
4933 /* Handle other relocations that tweak non-addend part of insn. */
4934 branch_bit = 0;
4935 switch (r_type)
4937 default:
4938 break;
4940 /* Branch taken prediction relocations. */
4941 case R_PPC_ADDR14_BRTAKEN:
4942 case R_PPC_REL14_BRTAKEN:
4943 branch_bit = BRANCH_PREDICT_BIT;
4944 /* Fall thru */
4946 /* Branch not taken predicition relocations. */
4947 case R_PPC_ADDR14_BRNTAKEN:
4948 case R_PPC_REL14_BRNTAKEN:
4949 insn = bfd_get_32 (output_bfd, contents + rel->r_offset);
4950 insn &= ~BRANCH_PREDICT_BIT;
4951 insn |= branch_bit;
4953 from = (rel->r_offset
4954 + input_section->output_offset
4955 + input_section->output_section->vma);
4957 /* Invert 'y' bit if not the default. */
4958 if ((bfd_signed_vma) (relocation + rel->r_addend - from) < 0)
4959 insn ^= BRANCH_PREDICT_BIT;
4961 bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
4962 break;
4965 addend = rel->r_addend;
4966 tls_type = 0;
4967 howto = NULL;
4968 if (r_type < R_PPC_max)
4969 howto = ppc_elf_howto_table[r_type];
4970 switch (r_type)
4972 default:
4973 (*_bfd_error_handler)
4974 (_("%s: unknown relocation type %d for symbol %s"),
4975 bfd_archive_filename (input_bfd), (int) r_type, sym_name);
4977 bfd_set_error (bfd_error_bad_value);
4978 ret = FALSE;
4979 continue;
4981 case R_PPC_NONE:
4982 case R_PPC_TLS:
4983 case R_PPC_EMB_MRKREF:
4984 case R_PPC_GNU_VTINHERIT:
4985 case R_PPC_GNU_VTENTRY:
4986 continue;
4988 /* GOT16 relocations. Like an ADDR16 using the symbol's
4989 address in the GOT as relocation value instead of the
4990 symbol's value itself. Also, create a GOT entry for the
4991 symbol and put the symbol value there. */
4992 case R_PPC_GOT_TLSGD16:
4993 case R_PPC_GOT_TLSGD16_LO:
4994 case R_PPC_GOT_TLSGD16_HI:
4995 case R_PPC_GOT_TLSGD16_HA:
4996 tls_type = TLS_TLS | TLS_GD;
4997 goto dogot;
4999 case R_PPC_GOT_TLSLD16:
5000 case R_PPC_GOT_TLSLD16_LO:
5001 case R_PPC_GOT_TLSLD16_HI:
5002 case R_PPC_GOT_TLSLD16_HA:
5003 tls_type = TLS_TLS | TLS_LD;
5004 goto dogot;
5006 case R_PPC_GOT_TPREL16:
5007 case R_PPC_GOT_TPREL16_LO:
5008 case R_PPC_GOT_TPREL16_HI:
5009 case R_PPC_GOT_TPREL16_HA:
5010 tls_type = TLS_TLS | TLS_TPREL;
5011 goto dogot;
5013 case R_PPC_GOT_DTPREL16:
5014 case R_PPC_GOT_DTPREL16_LO:
5015 case R_PPC_GOT_DTPREL16_HI:
5016 case R_PPC_GOT_DTPREL16_HA:
5017 tls_type = TLS_TLS | TLS_DTPREL;
5018 goto dogot;
5020 case R_PPC_GOT16:
5021 case R_PPC_GOT16_LO:
5022 case R_PPC_GOT16_HI:
5023 case R_PPC_GOT16_HA:
5024 dogot:
5026 /* Relocation is to the entry for this symbol in the global
5027 offset table. */
5028 bfd_vma off;
5029 bfd_vma *offp;
5030 unsigned long indx;
5032 if (htab->got == NULL)
5033 abort ();
5035 indx = 0;
5036 if (tls_type == (TLS_TLS | TLS_LD)
5037 && (h == NULL
5038 || !(h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC)))
5039 offp = &htab->tlsld_got.offset;
5040 else if (h != NULL)
5042 bfd_boolean dyn;
5043 dyn = htab->elf.dynamic_sections_created;
5044 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
5045 || (info->shared
5046 && SYMBOL_REFERENCES_LOCAL (info, h)))
5047 /* This is actually a static link, or it is a
5048 -Bsymbolic link and the symbol is defined
5049 locally, or the symbol was forced to be local
5050 because of a version file. */
5052 else
5054 indx = h->dynindx;
5055 unresolved_reloc = FALSE;
5057 offp = &h->got.offset;
5059 else
5061 if (local_got_offsets == NULL)
5062 abort ();
5063 offp = &local_got_offsets[r_symndx];
5066 /* The offset must always be a multiple of 4. We use the
5067 least significant bit to record whether we have already
5068 processed this entry. */
5069 off = *offp;
5070 if ((off & 1) != 0)
5071 off &= ~1;
5072 else
5074 unsigned int tls_m = (tls_mask
5075 & (TLS_LD | TLS_GD | TLS_DTPREL
5076 | TLS_TPREL | TLS_TPRELGD));
5078 if (offp == &htab->tlsld_got.offset)
5079 tls_m = TLS_LD;
5080 else if (h == NULL
5081 || !(h->elf_link_hash_flags
5082 & ELF_LINK_HASH_DEF_DYNAMIC))
5083 tls_m &= ~TLS_LD;
5085 /* We might have multiple got entries for this sym.
5086 Initialize them all. */
5089 int tls_ty = 0;
5091 if ((tls_m & TLS_LD) != 0)
5093 tls_ty = TLS_TLS | TLS_LD;
5094 tls_m &= ~TLS_LD;
5096 else if ((tls_m & TLS_GD) != 0)
5098 tls_ty = TLS_TLS | TLS_GD;
5099 tls_m &= ~TLS_GD;
5101 else if ((tls_m & TLS_DTPREL) != 0)
5103 tls_ty = TLS_TLS | TLS_DTPREL;
5104 tls_m &= ~TLS_DTPREL;
5106 else if ((tls_m & (TLS_TPREL | TLS_TPRELGD)) != 0)
5108 tls_ty = TLS_TLS | TLS_TPREL;
5109 tls_m = 0;
5112 /* Generate relocs for the dynamic linker. */
5113 if ((info->shared || indx != 0)
5114 && (h == NULL
5115 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
5116 || h->root.type != bfd_link_hash_undefweak))
5118 outrel.r_offset = (htab->got->output_section->vma
5119 + htab->got->output_offset
5120 + off);
5121 outrel.r_addend = 0;
5122 if (tls_ty & (TLS_LD | TLS_GD))
5124 outrel.r_info = ELF32_R_INFO (indx, R_PPC_DTPMOD32);
5125 if (tls_ty == (TLS_TLS | TLS_GD))
5127 loc = htab->relgot->contents;
5128 loc += (htab->relgot->reloc_count++
5129 * sizeof (Elf32_External_Rela));
5130 bfd_elf32_swap_reloca_out (output_bfd,
5131 &outrel, loc);
5132 outrel.r_offset += 4;
5133 outrel.r_info
5134 = ELF32_R_INFO (indx, R_PPC_DTPREL32);
5137 else if (tls_ty == (TLS_TLS | TLS_DTPREL))
5138 outrel.r_info = ELF32_R_INFO (indx, R_PPC_DTPREL32);
5139 else if (tls_ty == (TLS_TLS | TLS_TPREL))
5140 outrel.r_info = ELF32_R_INFO (indx, R_PPC_TPREL32);
5141 else if (indx == 0)
5142 outrel.r_info = ELF32_R_INFO (indx, R_PPC_RELATIVE);
5143 else
5144 outrel.r_info = ELF32_R_INFO (indx, R_PPC_GLOB_DAT);
5145 if (indx == 0)
5147 outrel.r_addend += relocation;
5148 if (tls_ty & (TLS_GD | TLS_DTPREL | TLS_TPREL))
5149 outrel.r_addend -= htab->elf.tls_sec->vma;
5151 loc = htab->relgot->contents;
5152 loc += (htab->relgot->reloc_count++
5153 * sizeof (Elf32_External_Rela));
5154 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
5157 /* Init the .got section contents if we're not
5158 emitting a reloc. */
5159 else
5161 bfd_vma value = relocation;
5163 if (tls_ty == (TLS_TLS | TLS_LD))
5164 value = 1;
5165 else if (tls_ty != 0)
5167 value -= htab->elf.tls_sec->vma + DTP_OFFSET;
5168 if (tls_ty == (TLS_TLS | TLS_TPREL))
5169 value += DTP_OFFSET - TP_OFFSET;
5171 if (tls_ty == (TLS_TLS | TLS_GD))
5173 bfd_put_32 (output_bfd, value,
5174 htab->got->contents + off + 4);
5175 value = 1;
5178 bfd_put_32 (output_bfd, value,
5179 htab->got->contents + off);
5182 off += 4;
5183 if (tls_ty & (TLS_LD | TLS_GD))
5184 off += 4;
5186 while (tls_m != 0);
5188 off = *offp;
5189 *offp = off | 1;
5192 if (off >= (bfd_vma) -2)
5193 abort ();
5195 if ((tls_type & TLS_TLS) != 0)
5197 if (tls_type != (TLS_TLS | TLS_LD))
5199 if ((tls_mask & TLS_LD) != 0
5200 && !(h == NULL
5201 || !(h->elf_link_hash_flags
5202 & ELF_LINK_HASH_DEF_DYNAMIC)))
5203 off += 8;
5204 if (tls_type != (TLS_TLS | TLS_GD))
5206 if ((tls_mask & TLS_GD) != 0)
5207 off += 8;
5208 if (tls_type != (TLS_TLS | TLS_DTPREL))
5210 if ((tls_mask & TLS_DTPREL) != 0)
5211 off += 4;
5217 relocation = htab->got->output_offset + off - 4;
5219 /* Addends on got relocations don't make much sense.
5220 x+off@got is actually x@got+off, and since the got is
5221 generated by a hash table traversal, the value in the
5222 got at entry m+n bears little relation to the entry m. */
5223 if (addend != 0)
5224 (*_bfd_error_handler)
5225 (_("%s(%s+0x%lx): non-zero addend on %s reloc against `%s'"),
5226 bfd_archive_filename (input_bfd),
5227 bfd_get_section_name (input_bfd, input_section),
5228 (long) rel->r_offset,
5229 howto->name,
5230 sym_name);
5232 break;
5234 /* Relocations that need no special processing. */
5235 case R_PPC_LOCAL24PC:
5236 /* It makes no sense to point a local relocation
5237 at a symbol not in this object. */
5238 if (unresolved_reloc)
5240 if (! (*info->callbacks->undefined_symbol) (info,
5241 h->root.root.string,
5242 input_bfd,
5243 input_section,
5244 rel->r_offset,
5245 TRUE))
5246 return FALSE;
5247 continue;
5249 break;
5251 case R_PPC_DTPREL16:
5252 case R_PPC_DTPREL16_LO:
5253 case R_PPC_DTPREL16_HI:
5254 case R_PPC_DTPREL16_HA:
5255 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
5256 break;
5258 /* Relocations that may need to be propagated if this is a shared
5259 object. */
5260 case R_PPC_TPREL16:
5261 case R_PPC_TPREL16_LO:
5262 case R_PPC_TPREL16_HI:
5263 case R_PPC_TPREL16_HA:
5264 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
5265 /* The TPREL16 relocs shouldn't really be used in shared
5266 libs as they will result in DT_TEXTREL being set, but
5267 support them anyway. */
5268 goto dodyn;
5270 case R_PPC_TPREL32:
5271 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
5272 goto dodyn;
5274 case R_PPC_DTPREL32:
5275 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
5276 goto dodyn;
5278 case R_PPC_DTPMOD32:
5279 relocation = 1;
5280 addend = 0;
5281 goto dodyn;
5283 case R_PPC_REL24:
5284 case R_PPC_REL32:
5285 case R_PPC_REL14:
5286 case R_PPC_REL14_BRTAKEN:
5287 case R_PPC_REL14_BRNTAKEN:
5288 /* If these relocations are not to a named symbol, they can be
5289 handled right here, no need to bother the dynamic linker. */
5290 if (SYMBOL_REFERENCES_LOCAL (info, h)
5291 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
5292 break;
5293 /* fall through */
5295 /* Relocations that always need to be propagated if this is a shared
5296 object. */
5297 case R_PPC_ADDR32:
5298 case R_PPC_ADDR24:
5299 case R_PPC_ADDR16:
5300 case R_PPC_ADDR16_LO:
5301 case R_PPC_ADDR16_HI:
5302 case R_PPC_ADDR16_HA:
5303 case R_PPC_ADDR14:
5304 case R_PPC_ADDR14_BRTAKEN:
5305 case R_PPC_ADDR14_BRNTAKEN:
5306 case R_PPC_UADDR32:
5307 case R_PPC_UADDR16:
5308 /* r_symndx will be zero only for relocs against symbols
5309 from removed linkonce sections, or sections discarded by
5310 a linker script. */
5311 dodyn:
5312 if (r_symndx == 0)
5313 break;
5314 /* Fall thru. */
5316 if ((info->shared
5317 && (h == NULL
5318 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
5319 || h->root.type != bfd_link_hash_undefweak)
5320 && (MUST_BE_DYN_RELOC (r_type)
5321 || !SYMBOL_CALLS_LOCAL (info, h)))
5322 || (ELIMINATE_COPY_RELOCS
5323 && !info->shared
5324 && (input_section->flags & SEC_ALLOC) != 0
5325 && h != NULL
5326 && h->dynindx != -1
5327 && (h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0
5328 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
5329 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0))
5331 int skip;
5333 #ifdef DEBUG
5334 fprintf (stderr, "ppc_elf_relocate_section needs to "
5335 "create relocation for %s\n",
5336 (h && h->root.root.string
5337 ? h->root.root.string : "<unknown>"));
5338 #endif
5340 /* When generating a shared object, these relocations
5341 are copied into the output file to be resolved at run
5342 time. */
5343 if (sreloc == NULL)
5345 const char *name;
5347 name = (bfd_elf_string_from_elf_section
5348 (input_bfd,
5349 elf_elfheader (input_bfd)->e_shstrndx,
5350 elf_section_data (input_section)->rel_hdr.sh_name));
5351 if (name == NULL)
5352 return FALSE;
5354 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
5355 && strcmp (bfd_get_section_name (input_bfd,
5356 input_section),
5357 name + 5) == 0);
5359 sreloc = bfd_get_section_by_name (htab->elf.dynobj, name);
5360 BFD_ASSERT (sreloc != NULL);
5363 skip = 0;
5365 outrel.r_offset =
5366 _bfd_elf_section_offset (output_bfd, info, input_section,
5367 rel->r_offset);
5368 if (outrel.r_offset == (bfd_vma) -1
5369 || outrel.r_offset == (bfd_vma) -2)
5370 skip = (int) outrel.r_offset;
5371 outrel.r_offset += (input_section->output_section->vma
5372 + input_section->output_offset);
5374 if (skip)
5375 memset (&outrel, 0, sizeof outrel);
5376 else if (!SYMBOL_REFERENCES_LOCAL (info, h))
5378 unresolved_reloc = FALSE;
5379 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
5380 outrel.r_addend = rel->r_addend;
5382 else
5384 outrel.r_addend = relocation + rel->r_addend;
5386 if (r_type == R_PPC_ADDR32)
5387 outrel.r_info = ELF32_R_INFO (0, R_PPC_RELATIVE);
5388 else
5390 long indx;
5392 if (bfd_is_abs_section (sec))
5393 indx = 0;
5394 else if (sec == NULL || sec->owner == NULL)
5396 bfd_set_error (bfd_error_bad_value);
5397 return FALSE;
5399 else
5401 asection *osec;
5403 /* We are turning this relocation into one
5404 against a section symbol. It would be
5405 proper to subtract the symbol's value,
5406 osec->vma, from the emitted reloc addend,
5407 but ld.so expects buggy relocs. */
5408 osec = sec->output_section;
5409 indx = elf_section_data (osec)->dynindx;
5410 BFD_ASSERT (indx > 0);
5411 #ifdef DEBUG
5412 if (indx <= 0)
5413 printf ("indx=%d section=%s flags=%08x name=%s\n",
5414 indx, osec->name, osec->flags,
5415 h->root.root.string);
5416 #endif
5419 outrel.r_info = ELF32_R_INFO (indx, r_type);
5423 loc = sreloc->contents;
5424 loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rela);
5425 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
5427 if (skip == -1)
5428 continue;
5430 /* This reloc will be computed at runtime. We clear the memory
5431 so that it contains predictable value. */
5432 if (! skip
5433 && ((input_section->flags & SEC_ALLOC) != 0
5434 || ELF32_R_TYPE (outrel.r_info) != R_PPC_RELATIVE))
5436 relocation = howto->pc_relative ? outrel.r_offset : 0;
5437 addend = 0;
5438 break;
5441 break;
5443 case R_PPC_RELAX32:
5444 ppc_elf_install_value (output_bfd, contents + rel->r_offset,
5445 relocation + addend, r_type);
5446 continue;
5448 /* Indirect .sdata relocation. */
5449 case R_PPC_EMB_SDAI16:
5450 BFD_ASSERT (htab->sdata != NULL);
5451 relocation
5452 = elf_finish_pointer_linker_section (output_bfd, input_bfd, info,
5453 htab->sdata, h, relocation,
5454 rel, R_PPC_RELATIVE);
5455 break;
5457 /* Indirect .sdata2 relocation. */
5458 case R_PPC_EMB_SDA2I16:
5459 BFD_ASSERT (htab->sdata2 != NULL);
5460 relocation
5461 = elf_finish_pointer_linker_section (output_bfd, input_bfd, info,
5462 htab->sdata2, h, relocation,
5463 rel, R_PPC_RELATIVE);
5464 break;
5466 /* Handle the TOC16 reloc. We want to use the offset within the .got
5467 section, not the actual VMA. This is appropriate when generating
5468 an embedded ELF object, for which the .got section acts like the
5469 AIX .toc section. */
5470 case R_PPC_TOC16: /* phony GOT16 relocations */
5471 BFD_ASSERT (sec != NULL);
5472 BFD_ASSERT (bfd_is_und_section (sec)
5473 || strcmp (bfd_get_section_name (abfd, sec), ".got") == 0
5474 || strcmp (bfd_get_section_name (abfd, sec), ".cgot") == 0)
5476 addend -= sec->output_section->vma + sec->output_offset + 0x8000;
5477 break;
5479 case R_PPC_PLTREL24:
5480 /* Relocation is to the entry for this symbol in the
5481 procedure linkage table. */
5482 BFD_ASSERT (h != NULL);
5484 if (h->plt.offset == (bfd_vma) -1
5485 || htab->plt == NULL)
5487 /* We didn't make a PLT entry for this symbol. This
5488 happens when statically linking PIC code, or when
5489 using -Bsymbolic. */
5490 break;
5493 unresolved_reloc = FALSE;
5494 relocation = (htab->plt->output_section->vma
5495 + htab->plt->output_offset
5496 + h->plt.offset);
5497 break;
5499 /* Relocate against _SDA_BASE_. */
5500 case R_PPC_SDAREL16:
5502 const char *name;
5503 const struct elf_link_hash_entry *sh;
5505 BFD_ASSERT (sec != NULL);
5506 name = bfd_get_section_name (abfd, sec->output_section);
5507 if (! ((strncmp (name, ".sdata", 6) == 0
5508 && (name[6] == 0 || name[6] == '.'))
5509 || (strncmp (name, ".sbss", 5) == 0
5510 && (name[5] == 0 || name[5] == '.'))))
5512 (*_bfd_error_handler)
5513 (_("%s: the target (%s) of a %s relocation is "
5514 "in the wrong output section (%s)"),
5515 bfd_archive_filename (input_bfd),
5516 sym_name,
5517 howto->name,
5518 name);
5520 sh = htab->sdata->sym_hash;
5521 addend -= (sh->root.u.def.value
5522 + sh->root.u.def.section->output_section->vma
5523 + sh->root.u.def.section->output_offset);
5525 break;
5527 /* Relocate against _SDA2_BASE_. */
5528 case R_PPC_EMB_SDA2REL:
5530 const char *name;
5531 const struct elf_link_hash_entry *sh;
5533 BFD_ASSERT (sec != NULL);
5534 name = bfd_get_section_name (abfd, sec->output_section);
5535 if (! (strncmp (name, ".sdata2", 7) == 0
5536 || strncmp (name, ".sbss2", 6) == 0))
5538 (*_bfd_error_handler)
5539 (_("%s: the target (%s) of a %s relocation is "
5540 "in the wrong output section (%s)"),
5541 bfd_archive_filename (input_bfd),
5542 sym_name,
5543 howto->name,
5544 name);
5546 bfd_set_error (bfd_error_bad_value);
5547 ret = FALSE;
5548 continue;
5550 sh = htab->sdata2->sym_hash;
5551 addend -= (sh->root.u.def.value
5552 + sh->root.u.def.section->output_section->vma
5553 + sh->root.u.def.section->output_offset);
5555 break;
5557 /* Relocate against either _SDA_BASE_, _SDA2_BASE_, or 0. */
5558 case R_PPC_EMB_SDA21:
5559 case R_PPC_EMB_RELSDA:
5561 const char *name;
5562 const struct elf_link_hash_entry *sh;
5563 int reg;
5565 BFD_ASSERT (sec != NULL);
5566 name = bfd_get_section_name (abfd, sec->output_section);
5567 if (((strncmp (name, ".sdata", 6) == 0
5568 && (name[6] == 0 || name[6] == '.'))
5569 || (strncmp (name, ".sbss", 5) == 0
5570 && (name[5] == 0 || name[5] == '.'))))
5572 reg = 13;
5573 sh = htab->sdata->sym_hash;
5574 addend -= (sh->root.u.def.value
5575 + sh->root.u.def.section->output_section->vma
5576 + sh->root.u.def.section->output_offset);
5579 else if (strncmp (name, ".sdata2", 7) == 0
5580 || strncmp (name, ".sbss2", 6) == 0)
5582 reg = 2;
5583 sh = htab->sdata2->sym_hash;
5584 addend -= (sh->root.u.def.value
5585 + sh->root.u.def.section->output_section->vma
5586 + sh->root.u.def.section->output_offset);
5589 else if (strcmp (name, ".PPC.EMB.sdata0") == 0
5590 || strcmp (name, ".PPC.EMB.sbss0") == 0)
5592 reg = 0;
5595 else
5597 (*_bfd_error_handler)
5598 (_("%s: the target (%s) of a %s relocation is "
5599 "in the wrong output section (%s)"),
5600 bfd_archive_filename (input_bfd),
5601 sym_name,
5602 howto->name,
5603 name);
5605 bfd_set_error (bfd_error_bad_value);
5606 ret = FALSE;
5607 continue;
5610 if (r_type == R_PPC_EMB_SDA21)
5611 { /* fill in register field */
5612 insn = bfd_get_32 (output_bfd, contents + rel->r_offset);
5613 insn = (insn & ~RA_REGISTER_MASK) | (reg << RA_REGISTER_SHIFT);
5614 bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
5617 break;
5619 /* Relocate against the beginning of the section. */
5620 case R_PPC_SECTOFF:
5621 case R_PPC_SECTOFF_LO:
5622 case R_PPC_SECTOFF_HI:
5623 case R_PPC_SECTOFF_HA:
5624 BFD_ASSERT (sec != NULL);
5625 addend -= sec->output_section->vma;
5626 break;
5628 /* Negative relocations. */
5629 case R_PPC_EMB_NADDR32:
5630 case R_PPC_EMB_NADDR16:
5631 case R_PPC_EMB_NADDR16_LO:
5632 case R_PPC_EMB_NADDR16_HI:
5633 case R_PPC_EMB_NADDR16_HA:
5634 addend -= 2 * relocation;
5635 break;
5637 case R_PPC_COPY:
5638 case R_PPC_GLOB_DAT:
5639 case R_PPC_JMP_SLOT:
5640 case R_PPC_RELATIVE:
5641 case R_PPC_PLT32:
5642 case R_PPC_PLTREL32:
5643 case R_PPC_PLT16_LO:
5644 case R_PPC_PLT16_HI:
5645 case R_PPC_PLT16_HA:
5646 case R_PPC_ADDR30:
5647 case R_PPC_EMB_RELSEC16:
5648 case R_PPC_EMB_RELST_LO:
5649 case R_PPC_EMB_RELST_HI:
5650 case R_PPC_EMB_RELST_HA:
5651 case R_PPC_EMB_BIT_FLD:
5652 (*_bfd_error_handler)
5653 (_("%s: relocation %s is not yet supported for symbol %s."),
5654 bfd_archive_filename (input_bfd),
5655 howto->name,
5656 sym_name);
5658 bfd_set_error (bfd_error_invalid_operation);
5659 ret = FALSE;
5660 continue;
5663 /* Do any further special processing. */
5664 switch (r_type)
5666 default:
5667 break;
5669 case R_PPC_ADDR16_HA:
5670 case R_PPC_GOT16_HA:
5671 case R_PPC_PLT16_HA:
5672 case R_PPC_SECTOFF_HA:
5673 case R_PPC_TPREL16_HA:
5674 case R_PPC_DTPREL16_HA:
5675 case R_PPC_GOT_TLSGD16_HA:
5676 case R_PPC_GOT_TLSLD16_HA:
5677 case R_PPC_GOT_TPREL16_HA:
5678 case R_PPC_GOT_DTPREL16_HA:
5679 case R_PPC_EMB_NADDR16_HA:
5680 case R_PPC_EMB_RELST_HA:
5681 /* It's just possible that this symbol is a weak symbol
5682 that's not actually defined anywhere. In that case,
5683 'sec' would be NULL, and we should leave the symbol
5684 alone (it will be set to zero elsewhere in the link). */
5685 if (sec != NULL)
5686 /* Add 0x10000 if sign bit in 0:15 is set.
5687 Bits 0:15 are not used. */
5688 addend += 0x8000;
5689 break;
5692 #ifdef DEBUG
5693 fprintf (stderr, "\ttype = %s (%d), name = %s, symbol index = %ld, "
5694 "offset = %ld, addend = %ld\n",
5695 howto->name,
5696 (int) r_type,
5697 sym_name,
5698 r_symndx,
5699 (long) rel->r_offset,
5700 (long) addend);
5701 #endif
5703 if (unresolved_reloc
5704 && !((input_section->flags & SEC_DEBUGGING) != 0
5705 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0))
5707 (*_bfd_error_handler)
5708 (_("%s(%s+0x%lx): unresolvable %s relocation against symbol `%s'"),
5709 bfd_archive_filename (input_bfd),
5710 bfd_get_section_name (input_bfd, input_section),
5711 (long) rel->r_offset,
5712 howto->name,
5713 sym_name);
5714 ret = FALSE;
5717 r = _bfd_final_link_relocate (howto,
5718 input_bfd,
5719 input_section,
5720 contents,
5721 rel->r_offset,
5722 relocation,
5723 addend);
5725 if (r != bfd_reloc_ok)
5727 if (sym_name == NULL)
5728 sym_name = "(null)";
5729 if (r == bfd_reloc_overflow)
5731 if (warned)
5732 continue;
5733 if (h != NULL
5734 && h->root.type == bfd_link_hash_undefweak
5735 && howto->pc_relative)
5737 /* Assume this is a call protected by other code that
5738 detect the symbol is undefined. If this is the case,
5739 we can safely ignore the overflow. If not, the
5740 program is hosed anyway, and a little warning isn't
5741 going to help. */
5743 continue;
5746 if (! (*info->callbacks->reloc_overflow) (info,
5747 sym_name,
5748 howto->name,
5749 rel->r_addend,
5750 input_bfd,
5751 input_section,
5752 rel->r_offset))
5753 return FALSE;
5755 else
5757 (*_bfd_error_handler)
5758 (_("%s(%s+0x%lx): %s reloc against `%s': error %d"),
5759 bfd_archive_filename (input_bfd),
5760 bfd_get_section_name (input_bfd, input_section),
5761 (long) rel->r_offset, howto->name, sym_name, (int) r);
5762 ret = FALSE;
5767 #ifdef DEBUG
5768 fprintf (stderr, "\n");
5769 #endif
5771 return ret;
5774 static enum elf_reloc_type_class
5775 ppc_elf_reloc_type_class (const Elf_Internal_Rela *rela)
5777 switch (ELF32_R_TYPE (rela->r_info))
5779 case R_PPC_RELATIVE:
5780 return reloc_class_relative;
5781 case R_PPC_REL24:
5782 case R_PPC_ADDR24:
5783 case R_PPC_JMP_SLOT:
5784 return reloc_class_plt;
5785 case R_PPC_COPY:
5786 return reloc_class_copy;
5787 default:
5788 return reloc_class_normal;
5792 /* Support for core dump NOTE sections. */
5794 static bfd_boolean
5795 ppc_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
5797 int offset;
5798 unsigned int raw_size;
5800 switch (note->descsz)
5802 default:
5803 return FALSE;
5805 case 268: /* Linux/PPC. */
5806 /* pr_cursig */
5807 elf_tdata (abfd)->core_signal = bfd_get_16 (abfd, note->descdata + 12);
5809 /* pr_pid */
5810 elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, note->descdata + 24);
5812 /* pr_reg */
5813 offset = 72;
5814 raw_size = 192;
5816 break;
5819 /* Make a ".reg/999" section. */
5820 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
5821 raw_size, note->descpos + offset);
5824 static bfd_boolean
5825 ppc_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
5827 switch (note->descsz)
5829 default:
5830 return FALSE;
5832 case 128: /* Linux/PPC elf_prpsinfo. */
5833 elf_tdata (abfd)->core_program
5834 = _bfd_elfcore_strndup (abfd, note->descdata + 32, 16);
5835 elf_tdata (abfd)->core_command
5836 = _bfd_elfcore_strndup (abfd, note->descdata + 48, 80);
5839 /* Note that for some reason, a spurious space is tacked
5840 onto the end of the args in some (at least one anyway)
5841 implementations, so strip it off if it exists. */
5844 char *command = elf_tdata (abfd)->core_command;
5845 int n = strlen (command);
5847 if (0 < n && command[n - 1] == ' ')
5848 command[n - 1] = '\0';
5851 return TRUE;
5854 /* Very simple linked list structure for recording apuinfo values. */
5855 typedef struct apuinfo_list
5857 struct apuinfo_list *next;
5858 unsigned long value;
5860 apuinfo_list;
5862 static apuinfo_list *head;
5865 static void
5866 apuinfo_list_init (void)
5868 head = NULL;
5871 static void
5872 apuinfo_list_add (unsigned long value)
5874 apuinfo_list *entry = head;
5876 while (entry != NULL)
5878 if (entry->value == value)
5879 return;
5880 entry = entry->next;
5883 entry = bfd_malloc (sizeof (* entry));
5884 if (entry == NULL)
5885 return;
5887 entry->value = value;
5888 entry->next = head;
5889 head = entry;
5892 static unsigned
5893 apuinfo_list_length (void)
5895 apuinfo_list *entry;
5896 unsigned long count;
5898 for (entry = head, count = 0;
5899 entry;
5900 entry = entry->next)
5901 ++ count;
5903 return count;
5906 static inline unsigned long
5907 apuinfo_list_element (unsigned long number)
5909 apuinfo_list * entry;
5911 for (entry = head;
5912 entry && number --;
5913 entry = entry->next)
5916 return entry ? entry->value : 0;
5919 static void
5920 apuinfo_list_finish (void)
5922 apuinfo_list *entry;
5924 for (entry = head; entry;)
5926 apuinfo_list *next = entry->next;
5927 free (entry);
5928 entry = next;
5931 head = NULL;
5934 #define APUINFO_SECTION_NAME ".PPC.EMB.apuinfo"
5935 #define APUINFO_LABEL "APUinfo"
5937 /* Scan the input BFDs and create a linked list of
5938 the APUinfo values that will need to be emitted. */
5940 static void
5941 ppc_elf_begin_write_processing (bfd *abfd, struct bfd_link_info *link_info)
5943 bfd *ibfd;
5944 asection *asec;
5945 char *buffer;
5946 unsigned num_input_sections;
5947 bfd_size_type output_section_size;
5948 unsigned i;
5949 unsigned num_entries;
5950 unsigned long offset;
5951 unsigned long length;
5952 const char *error_message = NULL;
5954 if (link_info == NULL)
5955 return;
5957 /* Scan the input bfds, looking for apuinfo sections. */
5958 num_input_sections = 0;
5959 output_section_size = 0;
5961 for (ibfd = link_info->input_bfds; ibfd; ibfd = ibfd->link_next)
5963 asec = bfd_get_section_by_name (ibfd, APUINFO_SECTION_NAME);
5964 if (asec)
5966 ++ num_input_sections;
5967 output_section_size += asec->_raw_size;
5971 /* We need at least one input sections
5972 in order to make merging worthwhile. */
5973 if (num_input_sections < 1)
5974 return;
5976 /* Just make sure that the output section exists as well. */
5977 asec = bfd_get_section_by_name (abfd, APUINFO_SECTION_NAME);
5978 if (asec == NULL)
5979 return;
5981 /* Allocate a buffer for the contents of the input sections. */
5982 buffer = bfd_malloc (output_section_size);
5983 if (buffer == NULL)
5984 return;
5986 offset = 0;
5987 apuinfo_list_init ();
5989 /* Read in the input sections contents. */
5990 for (ibfd = link_info->input_bfds; ibfd; ibfd = ibfd->link_next)
5992 unsigned long datum;
5993 char *ptr;
5995 asec = bfd_get_section_by_name (ibfd, APUINFO_SECTION_NAME);
5996 if (asec == NULL)
5997 continue;
5999 length = asec->_raw_size;
6000 if (length < 24)
6002 error_message = _("corrupt or empty %s section in %s");
6003 goto fail;
6006 if (bfd_seek (ibfd, asec->filepos, SEEK_SET) != 0
6007 || (bfd_bread (buffer + offset, length, ibfd) != length))
6009 error_message = _("unable to read in %s section from %s");
6010 goto fail;
6013 /* Process the contents of the section. */
6014 ptr = buffer + offset;
6015 error_message = _("corrupt %s section in %s");
6017 /* Verify the contents of the header. Note - we have to
6018 extract the values this way in order to allow for a
6019 host whose endian-ness is different from the target. */
6020 datum = bfd_get_32 (ibfd, ptr);
6021 if (datum != sizeof APUINFO_LABEL)
6022 goto fail;
6024 datum = bfd_get_32 (ibfd, ptr + 8);
6025 if (datum != 0x2)
6026 goto fail;
6028 if (strcmp (ptr + 12, APUINFO_LABEL) != 0)
6029 goto fail;
6031 /* Get the number of bytes used for apuinfo entries. */
6032 datum = bfd_get_32 (ibfd, ptr + 4);
6033 if (datum + 20 != length)
6034 goto fail;
6036 /* Make sure that we do not run off the end of the section. */
6037 if (offset + length > output_section_size)
6038 goto fail;
6040 /* Scan the apuinfo section, building a list of apuinfo numbers. */
6041 for (i = 0; i < datum; i += 4)
6042 apuinfo_list_add (bfd_get_32 (ibfd, ptr + 20 + i));
6044 /* Update the offset. */
6045 offset += length;
6048 error_message = NULL;
6050 /* Compute the size of the output section. */
6051 num_entries = apuinfo_list_length ();
6052 output_section_size = 20 + num_entries * 4;
6054 asec = bfd_get_section_by_name (abfd, APUINFO_SECTION_NAME);
6056 if (! bfd_set_section_size (abfd, asec, output_section_size))
6057 ibfd = abfd,
6058 error_message = _("warning: unable to set size of %s section in %s");
6060 fail:
6061 free (buffer);
6063 if (error_message)
6064 (*_bfd_error_handler) (error_message, APUINFO_SECTION_NAME,
6065 bfd_archive_filename (ibfd));
6069 /* Prevent the output section from accumulating the input sections'
6070 contents. We have already stored this in our linked list structure. */
6072 static bfd_boolean
6073 ppc_elf_write_section (bfd *abfd ATTRIBUTE_UNUSED,
6074 asection *asec,
6075 bfd_byte *contents ATTRIBUTE_UNUSED)
6077 return (apuinfo_list_length ()
6078 && strcmp (asec->name, APUINFO_SECTION_NAME) == 0);
6082 /* Finally we can generate the output section. */
6084 static void
6085 ppc_elf_final_write_processing (bfd *abfd, bfd_boolean linker ATTRIBUTE_UNUSED)
6087 bfd_byte *buffer;
6088 asection *asec;
6089 unsigned i;
6090 unsigned num_entries;
6091 bfd_size_type length;
6093 asec = bfd_get_section_by_name (abfd, APUINFO_SECTION_NAME);
6094 if (asec == NULL)
6095 return;
6097 if (apuinfo_list_length () == 0)
6098 return;
6100 length = asec->_raw_size;
6101 if (length < 20)
6102 return;
6104 buffer = bfd_malloc (length);
6105 if (buffer == NULL)
6107 (*_bfd_error_handler)
6108 (_("failed to allocate space for new APUinfo section."));
6109 return;
6112 /* Create the apuinfo header. */
6113 num_entries = apuinfo_list_length ();
6114 bfd_put_32 (abfd, sizeof APUINFO_LABEL, buffer);
6115 bfd_put_32 (abfd, num_entries * 4, buffer + 4);
6116 bfd_put_32 (abfd, 0x2, buffer + 8);
6117 strcpy (buffer + 12, APUINFO_LABEL);
6119 length = 20;
6120 for (i = 0; i < num_entries; i++)
6122 bfd_put_32 (abfd, apuinfo_list_element (i), buffer + length);
6123 length += 4;
6126 if (length != asec->_raw_size)
6127 (*_bfd_error_handler) (_("failed to compute new APUinfo section."));
6129 if (! bfd_set_section_contents (abfd, asec, buffer, (file_ptr) 0, length))
6130 (*_bfd_error_handler) (_("failed to install new APUinfo section."));
6132 free (buffer);
6134 apuinfo_list_finish ();
6137 /* Add extra PPC sections -- Note, for now, make .sbss2 and
6138 .PPC.EMB.sbss0 a normal section, and not a bss section so
6139 that the linker doesn't crater when trying to make more than
6140 2 sections. */
6142 static struct bfd_elf_special_section const ppc_elf_special_sections[]=
6144 { ".tags", 5, 0, SHT_ORDERED, SHF_ALLOC },
6145 { ".sdata", 6, -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
6146 { ".sbss", 5, -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
6147 { ".sdata2", 7, -2, SHT_PROGBITS, SHF_ALLOC },
6148 { ".sbss2", 6, -2, SHT_PROGBITS, SHF_ALLOC },
6149 { ".PPC.EMB.apuinfo", 16, 0, SHT_NOTE, 0 },
6150 { ".PPC.EMB.sdata0", 15, 0, SHT_PROGBITS, SHF_ALLOC },
6151 { ".PPC.EMB.sbss0", 14, 0, SHT_PROGBITS, SHF_ALLOC },
6152 { ".plt", 4, 0, SHT_NOBITS, SHF_ALLOC + SHF_EXECINSTR },
6153 { NULL, 0, 0, 0, 0 }
6156 #define TARGET_LITTLE_SYM bfd_elf32_powerpcle_vec
6157 #define TARGET_LITTLE_NAME "elf32-powerpcle"
6158 #define TARGET_BIG_SYM bfd_elf32_powerpc_vec
6159 #define TARGET_BIG_NAME "elf32-powerpc"
6160 #define ELF_ARCH bfd_arch_powerpc
6161 #define ELF_MACHINE_CODE EM_PPC
6162 #ifdef __QNXTARGET__
6163 #define ELF_MAXPAGESIZE 0x1000
6164 #else
6165 #define ELF_MAXPAGESIZE 0x10000
6166 #endif
6167 #define elf_info_to_howto ppc_elf_info_to_howto
6169 #ifdef EM_CYGNUS_POWERPC
6170 #define ELF_MACHINE_ALT1 EM_CYGNUS_POWERPC
6171 #endif
6173 #ifdef EM_PPC_OLD
6174 #define ELF_MACHINE_ALT2 EM_PPC_OLD
6175 #endif
6177 #define elf_backend_plt_not_loaded 1
6178 #define elf_backend_got_symbol_offset 4
6179 #define elf_backend_can_gc_sections 1
6180 #define elf_backend_can_refcount 1
6181 #define elf_backend_got_header_size 12
6182 #define elf_backend_rela_normal 1
6184 #define bfd_elf32_mkobject ppc_elf_mkobject
6185 #define bfd_elf32_bfd_merge_private_bfd_data ppc_elf_merge_private_bfd_data
6186 #define bfd_elf32_bfd_relax_section ppc_elf_relax_section
6187 #define bfd_elf32_bfd_reloc_type_lookup ppc_elf_reloc_type_lookup
6188 #define bfd_elf32_bfd_set_private_flags ppc_elf_set_private_flags
6189 #define bfd_elf32_bfd_link_hash_table_create ppc_elf_link_hash_table_create
6191 #define elf_backend_object_p ppc_elf_object_p
6192 #define elf_backend_gc_mark_hook ppc_elf_gc_mark_hook
6193 #define elf_backend_gc_sweep_hook ppc_elf_gc_sweep_hook
6194 #define elf_backend_section_from_shdr ppc_elf_section_from_shdr
6195 #define elf_backend_relocate_section ppc_elf_relocate_section
6196 #define elf_backend_create_dynamic_sections ppc_elf_create_dynamic_sections
6197 #define elf_backend_check_relocs ppc_elf_check_relocs
6198 #define elf_backend_copy_indirect_symbol ppc_elf_copy_indirect_symbol
6199 #define elf_backend_adjust_dynamic_symbol ppc_elf_adjust_dynamic_symbol
6200 #define elf_backend_add_symbol_hook ppc_elf_add_symbol_hook
6201 #define elf_backend_size_dynamic_sections ppc_elf_size_dynamic_sections
6202 #define elf_backend_finish_dynamic_symbol ppc_elf_finish_dynamic_symbol
6203 #define elf_backend_finish_dynamic_sections ppc_elf_finish_dynamic_sections
6204 #define elf_backend_fake_sections ppc_elf_fake_sections
6205 #define elf_backend_additional_program_headers ppc_elf_additional_program_headers
6206 #define elf_backend_modify_segment_map ppc_elf_modify_segment_map
6207 #define elf_backend_grok_prstatus ppc_elf_grok_prstatus
6208 #define elf_backend_grok_psinfo ppc_elf_grok_psinfo
6209 #define elf_backend_reloc_type_class ppc_elf_reloc_type_class
6210 #define elf_backend_begin_write_processing ppc_elf_begin_write_processing
6211 #define elf_backend_final_write_processing ppc_elf_final_write_processing
6212 #define elf_backend_write_section ppc_elf_write_section
6213 #define elf_backend_special_sections ppc_elf_special_sections
6215 #include "elf32-target.h"