Add initX, finiX, ctors, dtors sections to respect C++ constructor/destructor.
[binutils.git] / bfd / elf32-ppc.c
blob186f39d82b68248b17cb4bed489c267146c50e7e
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 Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
22 /* This file is based on a preliminary PowerPC ELF ABI. The
23 information may not match the final PowerPC ELF ABI. It includes
24 suggestions from the in-progress Embedded PowerPC ABI, and that
25 information may also not match. */
27 #include "bfd.h"
28 #include "sysdep.h"
29 #include "bfdlink.h"
30 #include "libbfd.h"
31 #include "elf-bfd.h"
32 #include "elf/ppc.h"
33 #include "elf32-ppc.h"
35 /* RELA relocations are used here. */
37 static struct bfd_hash_entry *ppc_elf_link_hash_newfunc
38 PARAMS ((struct bfd_hash_entry *entry, struct bfd_hash_table *table,
39 const char *string));
40 static struct bfd_link_hash_table *ppc_elf_link_hash_table_create
41 PARAMS ((bfd *abfd));
42 static void ppc_elf_copy_indirect_symbol
43 PARAMS ((struct elf_backend_data *bed, struct elf_link_hash_entry *dir,
44 struct elf_link_hash_entry *ind));
45 static reloc_howto_type *ppc_elf_reloc_type_lookup
46 PARAMS ((bfd *abfd, bfd_reloc_code_real_type code));
47 static void ppc_elf_info_to_howto
48 PARAMS ((bfd *abfd, arelent *cache_ptr, Elf_Internal_Rela *dst));
49 static void ppc_elf_howto_init
50 PARAMS ((void));
51 static int ppc_elf_sort_rela
52 PARAMS ((const PTR, const PTR));
53 static bfd_boolean ppc_elf_relax_section
54 PARAMS ((bfd *, asection *, struct bfd_link_info *, bfd_boolean *));
55 static bfd_reloc_status_type ppc_elf_addr16_ha_reloc
56 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
57 static bfd_reloc_status_type ppc_elf_unhandled_reloc
58 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
59 static bfd_boolean ppc_elf_object_p
60 PARAMS ((bfd *));
61 static bfd_boolean ppc_elf_set_private_flags
62 PARAMS ((bfd *, flagword));
63 static bfd_boolean ppc_elf_merge_private_bfd_data
64 PARAMS ((bfd *, bfd *));
65 static int ppc_elf_additional_program_headers
66 PARAMS ((bfd *));
67 static bfd_boolean ppc_elf_modify_segment_map
68 PARAMS ((bfd *));
69 static bfd_boolean ppc_elf_create_got
70 PARAMS ((bfd *, struct bfd_link_info *));
71 static bfd_boolean ppc_elf_create_dynamic_sections
72 PARAMS ((bfd *, struct bfd_link_info *));
73 static bfd_boolean ppc_elf_section_from_shdr
74 PARAMS ((bfd *, Elf_Internal_Shdr *, const char *));
75 static bfd_boolean ppc_elf_fake_sections
76 PARAMS ((bfd *, Elf_Internal_Shdr *, asection *));
77 static elf_linker_section_t *ppc_elf_create_linker_section
78 PARAMS ((bfd *abfd, struct bfd_link_info *info,
79 enum elf_linker_section_enum));
80 static bfd_boolean update_local_sym_info
81 PARAMS ((bfd *, Elf_Internal_Shdr *, unsigned long, int));
82 static void bad_shared_reloc
83 PARAMS ((bfd *, enum elf_ppc_reloc_type));
84 static bfd_boolean ppc_elf_check_relocs
85 PARAMS ((bfd *, struct bfd_link_info *, asection *,
86 const Elf_Internal_Rela *));
87 static asection *ppc_elf_gc_mark_hook
88 PARAMS ((asection *sec, struct bfd_link_info *info, Elf_Internal_Rela *rel,
89 struct elf_link_hash_entry *h, Elf_Internal_Sym *sym));
90 static bfd_boolean ppc_elf_gc_sweep_hook
91 PARAMS ((bfd *abfd, struct bfd_link_info *info, asection *sec,
92 const Elf_Internal_Rela *relocs));
93 static bfd_boolean ppc_elf_adjust_dynamic_symbol
94 PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *));
95 static bfd_boolean allocate_dynrelocs
96 PARAMS ((struct elf_link_hash_entry *, PTR));
97 static bfd_boolean readonly_dynrelocs
98 PARAMS ((struct elf_link_hash_entry *, PTR));
99 static bfd_boolean ppc_elf_size_dynamic_sections
100 PARAMS ((bfd *, struct bfd_link_info *));
101 static bfd_boolean ppc_elf_relocate_section
102 PARAMS ((bfd *, struct bfd_link_info *info, bfd *, asection *, bfd_byte *,
103 Elf_Internal_Rela *relocs, Elf_Internal_Sym *local_syms,
104 asection **));
105 static bfd_boolean ppc_elf_add_symbol_hook
106 PARAMS ((bfd *, struct bfd_link_info *, const Elf_Internal_Sym *,
107 const char **, flagword *, asection **, bfd_vma *));
108 static bfd_boolean ppc_elf_finish_dynamic_symbol
109 PARAMS ((bfd *, struct bfd_link_info *, struct elf_link_hash_entry *,
110 Elf_Internal_Sym *));
111 static bfd_boolean ppc_elf_finish_dynamic_sections
112 PARAMS ((bfd *, struct bfd_link_info *));
113 static enum elf_reloc_type_class ppc_elf_reloc_type_class
114 PARAMS ((const Elf_Internal_Rela *));
115 static bfd_boolean ppc_elf_grok_prstatus
116 PARAMS ((bfd *abfd, Elf_Internal_Note *note));
117 static bfd_boolean ppc_elf_grok_psinfo
118 PARAMS ((bfd *abfd, Elf_Internal_Note *note));
120 /* Branch prediction bit for branch taken relocs. */
121 #define BRANCH_PREDICT_BIT 0x200000
122 /* Mask to set RA in memory instructions. */
123 #define RA_REGISTER_MASK 0x001f0000
124 /* Value to shift register by to insert RA. */
125 #define RA_REGISTER_SHIFT 16
127 /* The name of the dynamic interpreter. This is put in the .interp
128 section. */
129 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
131 /* The size in bytes of an entry in the procedure linkage table. */
132 #define PLT_ENTRY_SIZE 12
133 /* The initial size of the plt reserved for the dynamic linker. */
134 #define PLT_INITIAL_ENTRY_SIZE 72
135 /* The size of the gap between entries in the PLT. */
136 #define PLT_SLOT_SIZE 8
137 /* The number of single-slot PLT entries (the rest use two slots). */
138 #define PLT_NUM_SINGLE_ENTRIES 8192
140 /* Some nop instructions. */
141 #define NOP 0x60000000
142 #define CROR_151515 0x4def7b82
143 #define CROR_313131 0x4ffffb82
145 /* Offset of tp and dtp pointers from start of TLS block. */
146 #define TP_OFFSET 0x7000
147 #define DTP_OFFSET 0x8000
149 /* Will references to this symbol always reference the symbol
150 in this object? STV_PROTECTED is excluded from the visibility test
151 here so that function pointer comparisons work properly. Since
152 function symbols not defined in an app are set to their .plt entry,
153 it's necessary for shared libs to also reference the .plt even
154 though the symbol is really local to the shared lib. */
155 #define SYMBOL_REFERENCES_LOCAL(INFO, H) \
156 ((! INFO->shared \
157 || INFO->symbolic \
158 || H->dynindx == -1 \
159 || ELF_ST_VISIBILITY (H->other) == STV_INTERNAL \
160 || ELF_ST_VISIBILITY (H->other) == STV_HIDDEN) \
161 && (H->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0)
163 /* Will _calls_ to this symbol always call the version in this object? */
164 #define SYMBOL_CALLS_LOCAL(INFO, H) \
165 ((! INFO->shared \
166 || INFO->symbolic \
167 || H->dynindx == -1 \
168 || ELF_ST_VISIBILITY (H->other) != STV_DEFAULT) \
169 && (H->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0)
171 /* The PPC linker needs to keep track of the number of relocs that it
172 decides to copy as dynamic relocs in check_relocs for each symbol.
173 This is so that it can later discard them if they are found to be
174 unnecessary. We store the information in a field extending the
175 regular ELF linker hash table. */
177 struct ppc_elf_dyn_relocs
179 struct ppc_elf_dyn_relocs *next;
181 /* The input section of the reloc. */
182 asection *sec;
184 /* Total number of relocs copied for the input section. */
185 bfd_size_type count;
187 /* Number of pc-relative relocs copied for the input section. */
188 bfd_size_type pc_count;
191 /* PPC ELF linker hash entry. */
193 struct ppc_elf_link_hash_entry
195 struct elf_link_hash_entry elf;
197 /* Track dynamic relocs copied for this symbol. */
198 struct ppc_elf_dyn_relocs *dyn_relocs;
200 /* Contexts in which symbol is used in the GOT (or TOC).
201 TLS_GD .. TLS_EXPLICIT bits are or'd into the mask as the
202 corresponding relocs are encountered during check_relocs.
203 tls_optimize clears TLS_GD .. TLS_TPREL when optimizing to
204 indicate the corresponding GOT entry type is not needed. */
205 #define TLS_GD 1 /* GD reloc. */
206 #define TLS_LD 2 /* LD reloc. */
207 #define TLS_TPREL 4 /* TPREL reloc, => IE. */
208 #define TLS_DTPREL 8 /* DTPREL reloc, => LD. */
209 #define TLS_TLS 16 /* Any TLS reloc. */
210 #define TLS_TPRELGD 32 /* TPREL reloc resulting from GD->IE. */
211 char tls_mask;
214 #define ppc_elf_hash_entry(ent) ((struct ppc_elf_link_hash_entry *) (ent))
216 /* PPC ELF linker hash table. */
218 struct ppc_elf_link_hash_table
220 struct elf_link_hash_table elf;
222 /* Short-cuts to get to dynamic linker sections. */
223 asection *got;
224 asection *relgot;
225 asection *plt;
226 asection *relplt;
227 asection *dynbss;
228 asection *relbss;
229 asection *dynsbss;
230 asection *relsbss;
231 elf_linker_section_t *sdata;
232 elf_linker_section_t *sdata2;
234 /* Short-cut to first output tls section. */
235 asection *tls_sec;
237 /* Shortcut to .__tls_get_addr. */
238 struct elf_link_hash_entry *tls_get_addr;
240 /* TLS local dynamic got entry handling. */
241 union {
242 bfd_signed_vma refcount;
243 bfd_vma offset;
244 } tlsld_got;
246 /* Small local sym to section mapping cache. */
247 struct sym_sec_cache sym_sec;
250 /* Get the PPC ELF linker hash table from a link_info structure. */
252 #define ppc_elf_hash_table(p) \
253 ((struct ppc_elf_link_hash_table *) (p)->hash)
255 /* Create an entry in a PPC ELF linker hash table. */
257 static struct bfd_hash_entry *
258 ppc_elf_link_hash_newfunc (entry, table, string)
259 struct bfd_hash_entry *entry;
260 struct bfd_hash_table *table;
261 const char *string;
263 /* Allocate the structure if it has not already been allocated by a
264 subclass. */
265 if (entry == NULL)
267 entry = bfd_hash_allocate (table,
268 sizeof (struct ppc_elf_link_hash_entry));
269 if (entry == NULL)
270 return entry;
273 /* Call the allocation method of the superclass. */
274 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
275 if (entry != NULL)
277 ppc_elf_hash_entry (entry)->dyn_relocs = NULL;
278 ppc_elf_hash_entry (entry)->tls_mask = 0;
281 return entry;
284 /* Create a PPC ELF linker hash table. */
286 static struct bfd_link_hash_table *
287 ppc_elf_link_hash_table_create (abfd)
288 bfd *abfd;
290 struct ppc_elf_link_hash_table *ret;
292 ret = ((struct ppc_elf_link_hash_table *)
293 bfd_malloc (sizeof (struct ppc_elf_link_hash_table)));
294 if (ret == NULL)
295 return NULL;
297 if (! _bfd_elf_link_hash_table_init (&ret->elf, abfd,
298 ppc_elf_link_hash_newfunc))
300 free (ret);
301 return NULL;
304 ret->got = NULL;
305 ret->relgot = NULL;
306 ret->plt = NULL;
307 ret->relplt = NULL;
308 ret->dynbss = NULL;
309 ret->relbss = NULL;
310 ret->dynsbss = NULL;
311 ret->relsbss = NULL;
312 ret->sdata = NULL;
313 ret->sdata2 = NULL;
314 ret->tls_sec = NULL;
315 ret->tls_get_addr = NULL;
316 ret->tlsld_got.refcount = 0;
317 ret->sym_sec.abfd = NULL;
319 return &ret->elf.root;
322 /* Copy the extra info we tack onto an elf_link_hash_entry. */
324 static void
325 ppc_elf_copy_indirect_symbol (bed, dir, ind)
326 struct elf_backend_data *bed;
327 struct elf_link_hash_entry *dir, *ind;
329 struct ppc_elf_link_hash_entry *edir, *eind;
331 edir = (struct ppc_elf_link_hash_entry *) dir;
332 eind = (struct ppc_elf_link_hash_entry *) ind;
334 if (eind->dyn_relocs != NULL)
336 if (edir->dyn_relocs != NULL)
338 struct ppc_elf_dyn_relocs **pp;
339 struct ppc_elf_dyn_relocs *p;
341 if (ind->root.type == bfd_link_hash_indirect)
342 abort ();
344 /* Add reloc counts against the weak sym to the strong sym
345 list. Merge any entries against the same section. */
346 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
348 struct ppc_elf_dyn_relocs *q;
350 for (q = edir->dyn_relocs; q != NULL; q = q->next)
351 if (q->sec == p->sec)
353 q->pc_count += p->pc_count;
354 q->count += p->count;
355 *pp = p->next;
356 break;
358 if (q == NULL)
359 pp = &p->next;
361 *pp = edir->dyn_relocs;
364 edir->dyn_relocs = eind->dyn_relocs;
365 eind->dyn_relocs = NULL;
368 edir->tls_mask |= eind->tls_mask;
370 _bfd_elf_link_hash_copy_indirect (bed, dir, ind);
373 static reloc_howto_type *ppc_elf_howto_table[(int) R_PPC_max];
375 static reloc_howto_type ppc_elf_howto_raw[] = {
376 /* This reloc does nothing. */
377 HOWTO (R_PPC_NONE, /* type */
378 0, /* rightshift */
379 2, /* size (0 = byte, 1 = short, 2 = long) */
380 32, /* bitsize */
381 FALSE, /* pc_relative */
382 0, /* bitpos */
383 complain_overflow_bitfield, /* complain_on_overflow */
384 bfd_elf_generic_reloc, /* special_function */
385 "R_PPC_NONE", /* name */
386 FALSE, /* partial_inplace */
387 0, /* src_mask */
388 0, /* dst_mask */
389 FALSE), /* pcrel_offset */
391 /* A standard 32 bit relocation. */
392 HOWTO (R_PPC_ADDR32, /* type */
393 0, /* rightshift */
394 2, /* size (0 = byte, 1 = short, 2 = long) */
395 32, /* bitsize */
396 FALSE, /* pc_relative */
397 0, /* bitpos */
398 complain_overflow_bitfield, /* complain_on_overflow */
399 bfd_elf_generic_reloc, /* special_function */
400 "R_PPC_ADDR32", /* name */
401 FALSE, /* partial_inplace */
402 0, /* src_mask */
403 0xffffffff, /* dst_mask */
404 FALSE), /* pcrel_offset */
406 /* An absolute 26 bit branch; the lower two bits must be zero.
407 FIXME: we don't check that, we just clear them. */
408 HOWTO (R_PPC_ADDR24, /* type */
409 0, /* rightshift */
410 2, /* size (0 = byte, 1 = short, 2 = long) */
411 26, /* bitsize */
412 FALSE, /* pc_relative */
413 0, /* bitpos */
414 complain_overflow_bitfield, /* complain_on_overflow */
415 bfd_elf_generic_reloc, /* special_function */
416 "R_PPC_ADDR24", /* name */
417 FALSE, /* partial_inplace */
418 0, /* src_mask */
419 0x3fffffc, /* dst_mask */
420 FALSE), /* pcrel_offset */
422 /* A standard 16 bit relocation. */
423 HOWTO (R_PPC_ADDR16, /* type */
424 0, /* rightshift */
425 1, /* size (0 = byte, 1 = short, 2 = long) */
426 16, /* bitsize */
427 FALSE, /* pc_relative */
428 0, /* bitpos */
429 complain_overflow_bitfield, /* complain_on_overflow */
430 bfd_elf_generic_reloc, /* special_function */
431 "R_PPC_ADDR16", /* name */
432 FALSE, /* partial_inplace */
433 0, /* src_mask */
434 0xffff, /* dst_mask */
435 FALSE), /* pcrel_offset */
437 /* A 16 bit relocation without overflow. */
438 HOWTO (R_PPC_ADDR16_LO, /* type */
439 0, /* rightshift */
440 1, /* size (0 = byte, 1 = short, 2 = long) */
441 16, /* bitsize */
442 FALSE, /* pc_relative */
443 0, /* bitpos */
444 complain_overflow_dont,/* complain_on_overflow */
445 bfd_elf_generic_reloc, /* special_function */
446 "R_PPC_ADDR16_LO", /* name */
447 FALSE, /* partial_inplace */
448 0, /* src_mask */
449 0xffff, /* dst_mask */
450 FALSE), /* pcrel_offset */
452 /* The high order 16 bits of an address. */
453 HOWTO (R_PPC_ADDR16_HI, /* type */
454 16, /* rightshift */
455 1, /* size (0 = byte, 1 = short, 2 = long) */
456 16, /* bitsize */
457 FALSE, /* pc_relative */
458 0, /* bitpos */
459 complain_overflow_dont, /* complain_on_overflow */
460 bfd_elf_generic_reloc, /* special_function */
461 "R_PPC_ADDR16_HI", /* name */
462 FALSE, /* partial_inplace */
463 0, /* src_mask */
464 0xffff, /* dst_mask */
465 FALSE), /* pcrel_offset */
467 /* The high order 16 bits of an address, plus 1 if the contents of
468 the low 16 bits, treated as a signed number, is negative. */
469 HOWTO (R_PPC_ADDR16_HA, /* type */
470 16, /* rightshift */
471 1, /* size (0 = byte, 1 = short, 2 = long) */
472 16, /* bitsize */
473 FALSE, /* pc_relative */
474 0, /* bitpos */
475 complain_overflow_dont, /* complain_on_overflow */
476 ppc_elf_addr16_ha_reloc, /* special_function */
477 "R_PPC_ADDR16_HA", /* name */
478 FALSE, /* partial_inplace */
479 0, /* src_mask */
480 0xffff, /* dst_mask */
481 FALSE), /* pcrel_offset */
483 /* An absolute 16 bit branch; the lower two bits must be zero.
484 FIXME: we don't check that, we just clear them. */
485 HOWTO (R_PPC_ADDR14, /* type */
486 0, /* rightshift */
487 2, /* size (0 = byte, 1 = short, 2 = long) */
488 16, /* bitsize */
489 FALSE, /* pc_relative */
490 0, /* bitpos */
491 complain_overflow_bitfield, /* complain_on_overflow */
492 bfd_elf_generic_reloc, /* special_function */
493 "R_PPC_ADDR14", /* name */
494 FALSE, /* partial_inplace */
495 0, /* src_mask */
496 0xfffc, /* dst_mask */
497 FALSE), /* pcrel_offset */
499 /* An absolute 16 bit branch, for which bit 10 should be set to
500 indicate that the branch is expected to be taken. The lower two
501 bits must be zero. */
502 HOWTO (R_PPC_ADDR14_BRTAKEN, /* type */
503 0, /* rightshift */
504 2, /* size (0 = byte, 1 = short, 2 = long) */
505 16, /* bitsize */
506 FALSE, /* pc_relative */
507 0, /* bitpos */
508 complain_overflow_bitfield, /* complain_on_overflow */
509 bfd_elf_generic_reloc, /* special_function */
510 "R_PPC_ADDR14_BRTAKEN",/* name */
511 FALSE, /* partial_inplace */
512 0, /* src_mask */
513 0xfffc, /* dst_mask */
514 FALSE), /* pcrel_offset */
516 /* An absolute 16 bit branch, for which bit 10 should be set to
517 indicate that the branch is not expected to be taken. The lower
518 two bits must be zero. */
519 HOWTO (R_PPC_ADDR14_BRNTAKEN, /* type */
520 0, /* rightshift */
521 2, /* size (0 = byte, 1 = short, 2 = long) */
522 16, /* bitsize */
523 FALSE, /* pc_relative */
524 0, /* bitpos */
525 complain_overflow_bitfield, /* complain_on_overflow */
526 bfd_elf_generic_reloc, /* special_function */
527 "R_PPC_ADDR14_BRNTAKEN",/* name */
528 FALSE, /* partial_inplace */
529 0, /* src_mask */
530 0xfffc, /* dst_mask */
531 FALSE), /* pcrel_offset */
533 /* A relative 26 bit branch; the lower two bits must be zero. */
534 HOWTO (R_PPC_REL24, /* type */
535 0, /* rightshift */
536 2, /* size (0 = byte, 1 = short, 2 = long) */
537 26, /* bitsize */
538 TRUE, /* pc_relative */
539 0, /* bitpos */
540 complain_overflow_signed, /* complain_on_overflow */
541 bfd_elf_generic_reloc, /* special_function */
542 "R_PPC_REL24", /* name */
543 FALSE, /* partial_inplace */
544 0, /* src_mask */
545 0x3fffffc, /* dst_mask */
546 TRUE), /* pcrel_offset */
548 /* A relative 16 bit branch; the lower two bits must be zero. */
549 HOWTO (R_PPC_REL14, /* type */
550 0, /* rightshift */
551 2, /* size (0 = byte, 1 = short, 2 = long) */
552 16, /* bitsize */
553 TRUE, /* pc_relative */
554 0, /* bitpos */
555 complain_overflow_signed, /* complain_on_overflow */
556 bfd_elf_generic_reloc, /* special_function */
557 "R_PPC_REL14", /* name */
558 FALSE, /* partial_inplace */
559 0, /* src_mask */
560 0xfffc, /* dst_mask */
561 TRUE), /* pcrel_offset */
563 /* A relative 16 bit branch. Bit 10 should be set to indicate that
564 the branch is expected to be taken. The lower two bits must be
565 zero. */
566 HOWTO (R_PPC_REL14_BRTAKEN, /* type */
567 0, /* rightshift */
568 2, /* size (0 = byte, 1 = short, 2 = long) */
569 16, /* bitsize */
570 TRUE, /* pc_relative */
571 0, /* bitpos */
572 complain_overflow_signed, /* complain_on_overflow */
573 bfd_elf_generic_reloc, /* special_function */
574 "R_PPC_REL14_BRTAKEN", /* name */
575 FALSE, /* partial_inplace */
576 0, /* src_mask */
577 0xfffc, /* dst_mask */
578 TRUE), /* pcrel_offset */
580 /* A relative 16 bit branch. Bit 10 should be set to indicate that
581 the branch is not expected to be taken. The lower two bits must
582 be zero. */
583 HOWTO (R_PPC_REL14_BRNTAKEN, /* type */
584 0, /* rightshift */
585 2, /* size (0 = byte, 1 = short, 2 = long) */
586 16, /* bitsize */
587 TRUE, /* pc_relative */
588 0, /* bitpos */
589 complain_overflow_signed, /* complain_on_overflow */
590 bfd_elf_generic_reloc, /* special_function */
591 "R_PPC_REL14_BRNTAKEN",/* name */
592 FALSE, /* partial_inplace */
593 0, /* src_mask */
594 0xfffc, /* dst_mask */
595 TRUE), /* pcrel_offset */
597 /* Like R_PPC_ADDR16, but referring to the GOT table entry for the
598 symbol. */
599 HOWTO (R_PPC_GOT16, /* type */
600 0, /* rightshift */
601 1, /* size (0 = byte, 1 = short, 2 = long) */
602 16, /* bitsize */
603 FALSE, /* pc_relative */
604 0, /* bitpos */
605 complain_overflow_signed, /* complain_on_overflow */
606 bfd_elf_generic_reloc, /* special_function */
607 "R_PPC_GOT16", /* name */
608 FALSE, /* partial_inplace */
609 0, /* src_mask */
610 0xffff, /* dst_mask */
611 FALSE), /* pcrel_offset */
613 /* Like R_PPC_ADDR16_LO, but referring to the GOT table entry for
614 the symbol. */
615 HOWTO (R_PPC_GOT16_LO, /* type */
616 0, /* rightshift */
617 1, /* size (0 = byte, 1 = short, 2 = long) */
618 16, /* bitsize */
619 FALSE, /* pc_relative */
620 0, /* bitpos */
621 complain_overflow_dont, /* complain_on_overflow */
622 bfd_elf_generic_reloc, /* special_function */
623 "R_PPC_GOT16_LO", /* name */
624 FALSE, /* partial_inplace */
625 0, /* src_mask */
626 0xffff, /* dst_mask */
627 FALSE), /* pcrel_offset */
629 /* Like R_PPC_ADDR16_HI, but referring to the GOT table entry for
630 the symbol. */
631 HOWTO (R_PPC_GOT16_HI, /* type */
632 16, /* rightshift */
633 1, /* size (0 = byte, 1 = short, 2 = long) */
634 16, /* bitsize */
635 FALSE, /* pc_relative */
636 0, /* bitpos */
637 complain_overflow_bitfield, /* complain_on_overflow */
638 bfd_elf_generic_reloc, /* special_function */
639 "R_PPC_GOT16_HI", /* name */
640 FALSE, /* partial_inplace */
641 0, /* src_mask */
642 0xffff, /* dst_mask */
643 FALSE), /* pcrel_offset */
645 /* Like R_PPC_ADDR16_HA, but referring to the GOT table entry for
646 the symbol. */
647 HOWTO (R_PPC_GOT16_HA, /* type */
648 16, /* rightshift */
649 1, /* size (0 = byte, 1 = short, 2 = long) */
650 16, /* bitsize */
651 FALSE, /* pc_relative */
652 0, /* bitpos */
653 complain_overflow_bitfield, /* complain_on_overflow */
654 ppc_elf_addr16_ha_reloc, /* special_function */
655 "R_PPC_GOT16_HA", /* name */
656 FALSE, /* partial_inplace */
657 0, /* src_mask */
658 0xffff, /* dst_mask */
659 FALSE), /* pcrel_offset */
661 /* Like R_PPC_REL24, but referring to the procedure linkage table
662 entry for the symbol. */
663 HOWTO (R_PPC_PLTREL24, /* type */
664 0, /* rightshift */
665 2, /* size (0 = byte, 1 = short, 2 = long) */
666 26, /* bitsize */
667 TRUE, /* pc_relative */
668 0, /* bitpos */
669 complain_overflow_signed, /* complain_on_overflow */
670 bfd_elf_generic_reloc, /* special_function */
671 "R_PPC_PLTREL24", /* name */
672 FALSE, /* partial_inplace */
673 0, /* src_mask */
674 0x3fffffc, /* dst_mask */
675 TRUE), /* pcrel_offset */
677 /* This is used only by the dynamic linker. The symbol should exist
678 both in the object being run and in some shared library. The
679 dynamic linker copies the data addressed by the symbol from the
680 shared library into the object, because the object being
681 run has to have the data at some particular address. */
682 HOWTO (R_PPC_COPY, /* type */
683 0, /* rightshift */
684 2, /* size (0 = byte, 1 = short, 2 = long) */
685 32, /* bitsize */
686 FALSE, /* pc_relative */
687 0, /* bitpos */
688 complain_overflow_bitfield, /* complain_on_overflow */
689 bfd_elf_generic_reloc, /* special_function */
690 "R_PPC_COPY", /* name */
691 FALSE, /* partial_inplace */
692 0, /* src_mask */
693 0, /* dst_mask */
694 FALSE), /* pcrel_offset */
696 /* Like R_PPC_ADDR32, but used when setting global offset table
697 entries. */
698 HOWTO (R_PPC_GLOB_DAT, /* type */
699 0, /* rightshift */
700 2, /* size (0 = byte, 1 = short, 2 = long) */
701 32, /* bitsize */
702 FALSE, /* pc_relative */
703 0, /* bitpos */
704 complain_overflow_bitfield, /* complain_on_overflow */
705 bfd_elf_generic_reloc, /* special_function */
706 "R_PPC_GLOB_DAT", /* name */
707 FALSE, /* partial_inplace */
708 0, /* src_mask */
709 0xffffffff, /* dst_mask */
710 FALSE), /* pcrel_offset */
712 /* Marks a procedure linkage table entry for a symbol. */
713 HOWTO (R_PPC_JMP_SLOT, /* type */
714 0, /* rightshift */
715 2, /* size (0 = byte, 1 = short, 2 = long) */
716 32, /* bitsize */
717 FALSE, /* pc_relative */
718 0, /* bitpos */
719 complain_overflow_bitfield, /* complain_on_overflow */
720 bfd_elf_generic_reloc, /* special_function */
721 "R_PPC_JMP_SLOT", /* name */
722 FALSE, /* partial_inplace */
723 0, /* src_mask */
724 0, /* dst_mask */
725 FALSE), /* pcrel_offset */
727 /* Used only by the dynamic linker. When the object is run, this
728 longword is set to the load address of the object, plus the
729 addend. */
730 HOWTO (R_PPC_RELATIVE, /* type */
731 0, /* rightshift */
732 2, /* size (0 = byte, 1 = short, 2 = long) */
733 32, /* bitsize */
734 FALSE, /* pc_relative */
735 0, /* bitpos */
736 complain_overflow_bitfield, /* complain_on_overflow */
737 bfd_elf_generic_reloc, /* special_function */
738 "R_PPC_RELATIVE", /* name */
739 FALSE, /* partial_inplace */
740 0, /* src_mask */
741 0xffffffff, /* dst_mask */
742 FALSE), /* pcrel_offset */
744 /* Like R_PPC_REL24, but uses the value of the symbol within the
745 object rather than the final value. Normally used for
746 _GLOBAL_OFFSET_TABLE_. */
747 HOWTO (R_PPC_LOCAL24PC, /* type */
748 0, /* rightshift */
749 2, /* size (0 = byte, 1 = short, 2 = long) */
750 26, /* bitsize */
751 TRUE, /* pc_relative */
752 0, /* bitpos */
753 complain_overflow_signed, /* complain_on_overflow */
754 bfd_elf_generic_reloc, /* special_function */
755 "R_PPC_LOCAL24PC", /* name */
756 FALSE, /* partial_inplace */
757 0, /* src_mask */
758 0x3fffffc, /* dst_mask */
759 TRUE), /* pcrel_offset */
761 /* Like R_PPC_ADDR32, but may be unaligned. */
762 HOWTO (R_PPC_UADDR32, /* type */
763 0, /* rightshift */
764 2, /* size (0 = byte, 1 = short, 2 = long) */
765 32, /* bitsize */
766 FALSE, /* pc_relative */
767 0, /* bitpos */
768 complain_overflow_bitfield, /* complain_on_overflow */
769 bfd_elf_generic_reloc, /* special_function */
770 "R_PPC_UADDR32", /* name */
771 FALSE, /* partial_inplace */
772 0, /* src_mask */
773 0xffffffff, /* dst_mask */
774 FALSE), /* pcrel_offset */
776 /* Like R_PPC_ADDR16, but may be unaligned. */
777 HOWTO (R_PPC_UADDR16, /* type */
778 0, /* rightshift */
779 1, /* size (0 = byte, 1 = short, 2 = long) */
780 16, /* bitsize */
781 FALSE, /* pc_relative */
782 0, /* bitpos */
783 complain_overflow_bitfield, /* complain_on_overflow */
784 bfd_elf_generic_reloc, /* special_function */
785 "R_PPC_UADDR16", /* name */
786 FALSE, /* partial_inplace */
787 0, /* src_mask */
788 0xffff, /* dst_mask */
789 FALSE), /* pcrel_offset */
791 /* 32-bit PC relative */
792 HOWTO (R_PPC_REL32, /* type */
793 0, /* rightshift */
794 2, /* size (0 = byte, 1 = short, 2 = long) */
795 32, /* bitsize */
796 TRUE, /* pc_relative */
797 0, /* bitpos */
798 complain_overflow_bitfield, /* complain_on_overflow */
799 bfd_elf_generic_reloc, /* special_function */
800 "R_PPC_REL32", /* name */
801 FALSE, /* partial_inplace */
802 0, /* src_mask */
803 0xffffffff, /* dst_mask */
804 TRUE), /* pcrel_offset */
806 /* 32-bit relocation to the symbol's procedure linkage table.
807 FIXME: not supported. */
808 HOWTO (R_PPC_PLT32, /* type */
809 0, /* rightshift */
810 2, /* size (0 = byte, 1 = short, 2 = long) */
811 32, /* bitsize */
812 FALSE, /* pc_relative */
813 0, /* bitpos */
814 complain_overflow_bitfield, /* complain_on_overflow */
815 bfd_elf_generic_reloc, /* special_function */
816 "R_PPC_PLT32", /* name */
817 FALSE, /* partial_inplace */
818 0, /* src_mask */
819 0, /* dst_mask */
820 FALSE), /* pcrel_offset */
822 /* 32-bit PC relative relocation to the symbol's procedure linkage table.
823 FIXME: not supported. */
824 HOWTO (R_PPC_PLTREL32, /* type */
825 0, /* rightshift */
826 2, /* size (0 = byte, 1 = short, 2 = long) */
827 32, /* bitsize */
828 TRUE, /* pc_relative */
829 0, /* bitpos */
830 complain_overflow_bitfield, /* complain_on_overflow */
831 bfd_elf_generic_reloc, /* special_function */
832 "R_PPC_PLTREL32", /* name */
833 FALSE, /* partial_inplace */
834 0, /* src_mask */
835 0, /* dst_mask */
836 TRUE), /* pcrel_offset */
838 /* Like R_PPC_ADDR16_LO, but referring to the PLT table entry for
839 the symbol. */
840 HOWTO (R_PPC_PLT16_LO, /* type */
841 0, /* rightshift */
842 1, /* size (0 = byte, 1 = short, 2 = long) */
843 16, /* bitsize */
844 FALSE, /* pc_relative */
845 0, /* bitpos */
846 complain_overflow_dont, /* complain_on_overflow */
847 bfd_elf_generic_reloc, /* special_function */
848 "R_PPC_PLT16_LO", /* name */
849 FALSE, /* partial_inplace */
850 0, /* src_mask */
851 0xffff, /* dst_mask */
852 FALSE), /* pcrel_offset */
854 /* Like R_PPC_ADDR16_HI, but referring to the PLT table entry for
855 the symbol. */
856 HOWTO (R_PPC_PLT16_HI, /* type */
857 16, /* rightshift */
858 1, /* size (0 = byte, 1 = short, 2 = long) */
859 16, /* bitsize */
860 FALSE, /* pc_relative */
861 0, /* bitpos */
862 complain_overflow_bitfield, /* complain_on_overflow */
863 bfd_elf_generic_reloc, /* special_function */
864 "R_PPC_PLT16_HI", /* name */
865 FALSE, /* partial_inplace */
866 0, /* src_mask */
867 0xffff, /* dst_mask */
868 FALSE), /* pcrel_offset */
870 /* Like R_PPC_ADDR16_HA, but referring to the PLT table entry for
871 the symbol. */
872 HOWTO (R_PPC_PLT16_HA, /* type */
873 16, /* rightshift */
874 1, /* size (0 = byte, 1 = short, 2 = long) */
875 16, /* bitsize */
876 FALSE, /* pc_relative */
877 0, /* bitpos */
878 complain_overflow_bitfield, /* complain_on_overflow */
879 ppc_elf_addr16_ha_reloc, /* special_function */
880 "R_PPC_PLT16_HA", /* name */
881 FALSE, /* partial_inplace */
882 0, /* src_mask */
883 0xffff, /* dst_mask */
884 FALSE), /* pcrel_offset */
886 /* A sign-extended 16 bit value relative to _SDA_BASE_, for use with
887 small data items. */
888 HOWTO (R_PPC_SDAREL16, /* type */
889 0, /* rightshift */
890 1, /* size (0 = byte, 1 = short, 2 = long) */
891 16, /* bitsize */
892 FALSE, /* pc_relative */
893 0, /* bitpos */
894 complain_overflow_signed, /* complain_on_overflow */
895 bfd_elf_generic_reloc, /* special_function */
896 "R_PPC_SDAREL16", /* name */
897 FALSE, /* partial_inplace */
898 0, /* src_mask */
899 0xffff, /* dst_mask */
900 FALSE), /* pcrel_offset */
902 /* 16-bit section relative relocation. */
903 HOWTO (R_PPC_SECTOFF, /* type */
904 0, /* rightshift */
905 1, /* size (0 = byte, 1 = short, 2 = long) */
906 16, /* bitsize */
907 FALSE, /* pc_relative */
908 0, /* bitpos */
909 complain_overflow_bitfield, /* complain_on_overflow */
910 bfd_elf_generic_reloc, /* special_function */
911 "R_PPC_SECTOFF", /* name */
912 FALSE, /* partial_inplace */
913 0, /* src_mask */
914 0xffff, /* dst_mask */
915 FALSE), /* pcrel_offset */
917 /* 16-bit lower half section relative relocation. */
918 HOWTO (R_PPC_SECTOFF_LO, /* type */
919 0, /* rightshift */
920 1, /* size (0 = byte, 1 = short, 2 = long) */
921 16, /* bitsize */
922 FALSE, /* pc_relative */
923 0, /* bitpos */
924 complain_overflow_dont, /* complain_on_overflow */
925 bfd_elf_generic_reloc, /* special_function */
926 "R_PPC_SECTOFF_LO", /* name */
927 FALSE, /* partial_inplace */
928 0, /* src_mask */
929 0xffff, /* dst_mask */
930 FALSE), /* pcrel_offset */
932 /* 16-bit upper half section relative relocation. */
933 HOWTO (R_PPC_SECTOFF_HI, /* type */
934 16, /* rightshift */
935 1, /* size (0 = byte, 1 = short, 2 = long) */
936 16, /* bitsize */
937 FALSE, /* pc_relative */
938 0, /* bitpos */
939 complain_overflow_bitfield, /* complain_on_overflow */
940 bfd_elf_generic_reloc, /* special_function */
941 "R_PPC_SECTOFF_HI", /* name */
942 FALSE, /* partial_inplace */
943 0, /* src_mask */
944 0xffff, /* dst_mask */
945 FALSE), /* pcrel_offset */
947 /* 16-bit upper half adjusted section relative relocation. */
948 HOWTO (R_PPC_SECTOFF_HA, /* type */
949 16, /* rightshift */
950 1, /* size (0 = byte, 1 = short, 2 = long) */
951 16, /* bitsize */
952 FALSE, /* pc_relative */
953 0, /* bitpos */
954 complain_overflow_bitfield, /* complain_on_overflow */
955 ppc_elf_addr16_ha_reloc, /* special_function */
956 "R_PPC_SECTOFF_HA", /* name */
957 FALSE, /* partial_inplace */
958 0, /* src_mask */
959 0xffff, /* dst_mask */
960 FALSE), /* pcrel_offset */
962 /* Marker reloc for TLS. */
963 HOWTO (R_PPC_TLS,
964 0, /* rightshift */
965 2, /* size (0 = byte, 1 = short, 2 = long) */
966 32, /* bitsize */
967 FALSE, /* pc_relative */
968 0, /* bitpos */
969 complain_overflow_dont, /* complain_on_overflow */
970 bfd_elf_generic_reloc, /* special_function */
971 "R_PPC_TLS", /* name */
972 FALSE, /* partial_inplace */
973 0, /* src_mask */
974 0, /* dst_mask */
975 FALSE), /* pcrel_offset */
977 /* Computes the load module index of the load module that contains the
978 definition of its TLS sym. */
979 HOWTO (R_PPC_DTPMOD32,
980 0, /* rightshift */
981 2, /* size (0 = byte, 1 = short, 2 = long) */
982 32, /* bitsize */
983 FALSE, /* pc_relative */
984 0, /* bitpos */
985 complain_overflow_dont, /* complain_on_overflow */
986 ppc_elf_unhandled_reloc, /* special_function */
987 "R_PPC_DTPMOD32", /* name */
988 FALSE, /* partial_inplace */
989 0, /* src_mask */
990 0xffffffff, /* dst_mask */
991 FALSE), /* pcrel_offset */
993 /* Computes a dtv-relative displacement, the difference between the value
994 of sym+add and the base address of the thread-local storage block that
995 contains the definition of sym, minus 0x8000. */
996 HOWTO (R_PPC_DTPREL32,
997 0, /* rightshift */
998 2, /* size (0 = byte, 1 = short, 2 = long) */
999 32, /* bitsize */
1000 FALSE, /* pc_relative */
1001 0, /* bitpos */
1002 complain_overflow_dont, /* complain_on_overflow */
1003 ppc_elf_unhandled_reloc, /* special_function */
1004 "R_PPC_DTPREL32", /* name */
1005 FALSE, /* partial_inplace */
1006 0, /* src_mask */
1007 0xffffffff, /* dst_mask */
1008 FALSE), /* pcrel_offset */
1010 /* A 16 bit dtprel reloc. */
1011 HOWTO (R_PPC_DTPREL16,
1012 0, /* rightshift */
1013 1, /* size (0 = byte, 1 = short, 2 = long) */
1014 16, /* bitsize */
1015 FALSE, /* pc_relative */
1016 0, /* bitpos */
1017 complain_overflow_signed, /* complain_on_overflow */
1018 ppc_elf_unhandled_reloc, /* special_function */
1019 "R_PPC_DTPREL16", /* name */
1020 FALSE, /* partial_inplace */
1021 0, /* src_mask */
1022 0xffff, /* dst_mask */
1023 FALSE), /* pcrel_offset */
1025 /* Like DTPREL16, but no overflow. */
1026 HOWTO (R_PPC_DTPREL16_LO,
1027 0, /* rightshift */
1028 1, /* size (0 = byte, 1 = short, 2 = long) */
1029 16, /* bitsize */
1030 FALSE, /* pc_relative */
1031 0, /* bitpos */
1032 complain_overflow_dont, /* complain_on_overflow */
1033 ppc_elf_unhandled_reloc, /* special_function */
1034 "R_PPC_DTPREL16_LO", /* name */
1035 FALSE, /* partial_inplace */
1036 0, /* src_mask */
1037 0xffff, /* dst_mask */
1038 FALSE), /* pcrel_offset */
1040 /* Like DTPREL16_LO, but next higher group of 16 bits. */
1041 HOWTO (R_PPC_DTPREL16_HI,
1042 16, /* rightshift */
1043 1, /* size (0 = byte, 1 = short, 2 = long) */
1044 16, /* bitsize */
1045 FALSE, /* pc_relative */
1046 0, /* bitpos */
1047 complain_overflow_dont, /* complain_on_overflow */
1048 ppc_elf_unhandled_reloc, /* special_function */
1049 "R_PPC_DTPREL16_HI", /* name */
1050 FALSE, /* partial_inplace */
1051 0, /* src_mask */
1052 0xffff, /* dst_mask */
1053 FALSE), /* pcrel_offset */
1055 /* Like DTPREL16_HI, but adjust for low 16 bits. */
1056 HOWTO (R_PPC_DTPREL16_HA,
1057 16, /* rightshift */
1058 1, /* size (0 = byte, 1 = short, 2 = long) */
1059 16, /* bitsize */
1060 FALSE, /* pc_relative */
1061 0, /* bitpos */
1062 complain_overflow_dont, /* complain_on_overflow */
1063 ppc_elf_unhandled_reloc, /* special_function */
1064 "R_PPC_DTPREL16_HA", /* name */
1065 FALSE, /* partial_inplace */
1066 0, /* src_mask */
1067 0xffff, /* dst_mask */
1068 FALSE), /* pcrel_offset */
1070 /* Computes a tp-relative displacement, the difference between the value of
1071 sym+add and the value of the thread pointer (r13). */
1072 HOWTO (R_PPC_TPREL32,
1073 0, /* rightshift */
1074 2, /* size (0 = byte, 1 = short, 2 = long) */
1075 32, /* bitsize */
1076 FALSE, /* pc_relative */
1077 0, /* bitpos */
1078 complain_overflow_dont, /* complain_on_overflow */
1079 ppc_elf_unhandled_reloc, /* special_function */
1080 "R_PPC_TPREL32", /* name */
1081 FALSE, /* partial_inplace */
1082 0, /* src_mask */
1083 0xffffffff, /* dst_mask */
1084 FALSE), /* pcrel_offset */
1086 /* A 16 bit tprel reloc. */
1087 HOWTO (R_PPC_TPREL16,
1088 0, /* rightshift */
1089 1, /* size (0 = byte, 1 = short, 2 = long) */
1090 16, /* bitsize */
1091 FALSE, /* pc_relative */
1092 0, /* bitpos */
1093 complain_overflow_signed, /* complain_on_overflow */
1094 ppc_elf_unhandled_reloc, /* special_function */
1095 "R_PPC_TPREL16", /* name */
1096 FALSE, /* partial_inplace */
1097 0, /* src_mask */
1098 0xffff, /* dst_mask */
1099 FALSE), /* pcrel_offset */
1101 /* Like TPREL16, but no overflow. */
1102 HOWTO (R_PPC_TPREL16_LO,
1103 0, /* rightshift */
1104 1, /* size (0 = byte, 1 = short, 2 = long) */
1105 16, /* bitsize */
1106 FALSE, /* pc_relative */
1107 0, /* bitpos */
1108 complain_overflow_dont, /* complain_on_overflow */
1109 ppc_elf_unhandled_reloc, /* special_function */
1110 "R_PPC_TPREL16_LO", /* name */
1111 FALSE, /* partial_inplace */
1112 0, /* src_mask */
1113 0xffff, /* dst_mask */
1114 FALSE), /* pcrel_offset */
1116 /* Like TPREL16_LO, but next higher group of 16 bits. */
1117 HOWTO (R_PPC_TPREL16_HI,
1118 16, /* rightshift */
1119 1, /* size (0 = byte, 1 = short, 2 = long) */
1120 16, /* bitsize */
1121 FALSE, /* pc_relative */
1122 0, /* bitpos */
1123 complain_overflow_dont, /* complain_on_overflow */
1124 ppc_elf_unhandled_reloc, /* special_function */
1125 "R_PPC_TPREL16_HI", /* name */
1126 FALSE, /* partial_inplace */
1127 0, /* src_mask */
1128 0xffff, /* dst_mask */
1129 FALSE), /* pcrel_offset */
1131 /* Like TPREL16_HI, but adjust for low 16 bits. */
1132 HOWTO (R_PPC_TPREL16_HA,
1133 16, /* rightshift */
1134 1, /* size (0 = byte, 1 = short, 2 = long) */
1135 16, /* bitsize */
1136 FALSE, /* pc_relative */
1137 0, /* bitpos */
1138 complain_overflow_dont, /* complain_on_overflow */
1139 ppc_elf_unhandled_reloc, /* special_function */
1140 "R_PPC_TPREL16_HA", /* name */
1141 FALSE, /* partial_inplace */
1142 0, /* src_mask */
1143 0xffff, /* dst_mask */
1144 FALSE), /* pcrel_offset */
1146 /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
1147 with values (sym+add)@dtpmod and (sym+add)@dtprel, and computes the offset
1148 to the first entry. */
1149 HOWTO (R_PPC_GOT_TLSGD16,
1150 0, /* rightshift */
1151 1, /* size (0 = byte, 1 = short, 2 = long) */
1152 16, /* bitsize */
1153 FALSE, /* pc_relative */
1154 0, /* bitpos */
1155 complain_overflow_signed, /* complain_on_overflow */
1156 ppc_elf_unhandled_reloc, /* special_function */
1157 "R_PPC_GOT_TLSGD16", /* name */
1158 FALSE, /* partial_inplace */
1159 0, /* src_mask */
1160 0xffff, /* dst_mask */
1161 FALSE), /* pcrel_offset */
1163 /* Like GOT_TLSGD16, but no overflow. */
1164 HOWTO (R_PPC_GOT_TLSGD16_LO,
1165 0, /* rightshift */
1166 1, /* size (0 = byte, 1 = short, 2 = long) */
1167 16, /* bitsize */
1168 FALSE, /* pc_relative */
1169 0, /* bitpos */
1170 complain_overflow_dont, /* complain_on_overflow */
1171 ppc_elf_unhandled_reloc, /* special_function */
1172 "R_PPC_GOT_TLSGD16_LO", /* name */
1173 FALSE, /* partial_inplace */
1174 0, /* src_mask */
1175 0xffff, /* dst_mask */
1176 FALSE), /* pcrel_offset */
1178 /* Like GOT_TLSGD16_LO, but next higher group of 16 bits. */
1179 HOWTO (R_PPC_GOT_TLSGD16_HI,
1180 16, /* rightshift */
1181 1, /* size (0 = byte, 1 = short, 2 = long) */
1182 16, /* bitsize */
1183 FALSE, /* pc_relative */
1184 0, /* bitpos */
1185 complain_overflow_dont, /* complain_on_overflow */
1186 ppc_elf_unhandled_reloc, /* special_function */
1187 "R_PPC_GOT_TLSGD16_HI", /* name */
1188 FALSE, /* partial_inplace */
1189 0, /* src_mask */
1190 0xffff, /* dst_mask */
1191 FALSE), /* pcrel_offset */
1193 /* Like GOT_TLSGD16_HI, but adjust for low 16 bits. */
1194 HOWTO (R_PPC_GOT_TLSGD16_HA,
1195 16, /* rightshift */
1196 1, /* size (0 = byte, 1 = short, 2 = long) */
1197 16, /* bitsize */
1198 FALSE, /* pc_relative */
1199 0, /* bitpos */
1200 complain_overflow_dont, /* complain_on_overflow */
1201 ppc_elf_unhandled_reloc, /* special_function */
1202 "R_PPC_GOT_TLSGD16_HA", /* name */
1203 FALSE, /* partial_inplace */
1204 0, /* src_mask */
1205 0xffff, /* dst_mask */
1206 FALSE), /* pcrel_offset */
1208 /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
1209 with values (sym+add)@dtpmod and zero, and computes the offset to the
1210 first entry. */
1211 HOWTO (R_PPC_GOT_TLSLD16,
1212 0, /* rightshift */
1213 1, /* size (0 = byte, 1 = short, 2 = long) */
1214 16, /* bitsize */
1215 FALSE, /* pc_relative */
1216 0, /* bitpos */
1217 complain_overflow_signed, /* complain_on_overflow */
1218 ppc_elf_unhandled_reloc, /* special_function */
1219 "R_PPC_GOT_TLSLD16", /* name */
1220 FALSE, /* partial_inplace */
1221 0, /* src_mask */
1222 0xffff, /* dst_mask */
1223 FALSE), /* pcrel_offset */
1225 /* Like GOT_TLSLD16, but no overflow. */
1226 HOWTO (R_PPC_GOT_TLSLD16_LO,
1227 0, /* rightshift */
1228 1, /* size (0 = byte, 1 = short, 2 = long) */
1229 16, /* bitsize */
1230 FALSE, /* pc_relative */
1231 0, /* bitpos */
1232 complain_overflow_dont, /* complain_on_overflow */
1233 ppc_elf_unhandled_reloc, /* special_function */
1234 "R_PPC_GOT_TLSLD16_LO", /* name */
1235 FALSE, /* partial_inplace */
1236 0, /* src_mask */
1237 0xffff, /* dst_mask */
1238 FALSE), /* pcrel_offset */
1240 /* Like GOT_TLSLD16_LO, but next higher group of 16 bits. */
1241 HOWTO (R_PPC_GOT_TLSLD16_HI,
1242 16, /* rightshift */
1243 1, /* size (0 = byte, 1 = short, 2 = long) */
1244 16, /* bitsize */
1245 FALSE, /* pc_relative */
1246 0, /* bitpos */
1247 complain_overflow_dont, /* complain_on_overflow */
1248 ppc_elf_unhandled_reloc, /* special_function */
1249 "R_PPC_GOT_TLSLD16_HI", /* name */
1250 FALSE, /* partial_inplace */
1251 0, /* src_mask */
1252 0xffff, /* dst_mask */
1253 FALSE), /* pcrel_offset */
1255 /* Like GOT_TLSLD16_HI, but adjust for low 16 bits. */
1256 HOWTO (R_PPC_GOT_TLSLD16_HA,
1257 16, /* rightshift */
1258 1, /* size (0 = byte, 1 = short, 2 = long) */
1259 16, /* bitsize */
1260 FALSE, /* pc_relative */
1261 0, /* bitpos */
1262 complain_overflow_dont, /* complain_on_overflow */
1263 ppc_elf_unhandled_reloc, /* special_function */
1264 "R_PPC_GOT_TLSLD16_HA", /* name */
1265 FALSE, /* partial_inplace */
1266 0, /* src_mask */
1267 0xffff, /* dst_mask */
1268 FALSE), /* pcrel_offset */
1270 /* Allocates an entry in the GOT with value (sym+add)@dtprel, and computes
1271 the offset to the entry. */
1272 HOWTO (R_PPC_GOT_DTPREL16,
1273 0, /* rightshift */
1274 1, /* size (0 = byte, 1 = short, 2 = long) */
1275 16, /* bitsize */
1276 FALSE, /* pc_relative */
1277 0, /* bitpos */
1278 complain_overflow_signed, /* complain_on_overflow */
1279 ppc_elf_unhandled_reloc, /* special_function */
1280 "R_PPC_GOT_DTPREL16", /* name */
1281 FALSE, /* partial_inplace */
1282 0, /* src_mask */
1283 0xffff, /* dst_mask */
1284 FALSE), /* pcrel_offset */
1286 /* Like GOT_DTPREL16, but no overflow. */
1287 HOWTO (R_PPC_GOT_DTPREL16_LO,
1288 0, /* rightshift */
1289 1, /* size (0 = byte, 1 = short, 2 = long) */
1290 16, /* bitsize */
1291 FALSE, /* pc_relative */
1292 0, /* bitpos */
1293 complain_overflow_dont, /* complain_on_overflow */
1294 ppc_elf_unhandled_reloc, /* special_function */
1295 "R_PPC_GOT_DTPREL16_LO", /* name */
1296 FALSE, /* partial_inplace */
1297 0, /* src_mask */
1298 0xffff, /* dst_mask */
1299 FALSE), /* pcrel_offset */
1301 /* Like GOT_DTPREL16_LO, but next higher group of 16 bits. */
1302 HOWTO (R_PPC_GOT_DTPREL16_HI,
1303 16, /* rightshift */
1304 1, /* size (0 = byte, 1 = short, 2 = long) */
1305 16, /* bitsize */
1306 FALSE, /* pc_relative */
1307 0, /* bitpos */
1308 complain_overflow_dont, /* complain_on_overflow */
1309 ppc_elf_unhandled_reloc, /* special_function */
1310 "R_PPC_GOT_DTPREL16_HI", /* name */
1311 FALSE, /* partial_inplace */
1312 0, /* src_mask */
1313 0xffff, /* dst_mask */
1314 FALSE), /* pcrel_offset */
1316 /* Like GOT_DTPREL16_HI, but adjust for low 16 bits. */
1317 HOWTO (R_PPC_GOT_DTPREL16_HA,
1318 16, /* rightshift */
1319 1, /* size (0 = byte, 1 = short, 2 = long) */
1320 16, /* bitsize */
1321 FALSE, /* pc_relative */
1322 0, /* bitpos */
1323 complain_overflow_dont, /* complain_on_overflow */
1324 ppc_elf_unhandled_reloc, /* special_function */
1325 "R_PPC_GOT_DTPREL16_HA", /* name */
1326 FALSE, /* partial_inplace */
1327 0, /* src_mask */
1328 0xffff, /* dst_mask */
1329 FALSE), /* pcrel_offset */
1331 /* Allocates an entry in the GOT with value (sym+add)@tprel, and computes the
1332 offset to the entry. */
1333 HOWTO (R_PPC_GOT_TPREL16,
1334 0, /* rightshift */
1335 1, /* size (0 = byte, 1 = short, 2 = long) */
1336 16, /* bitsize */
1337 FALSE, /* pc_relative */
1338 0, /* bitpos */
1339 complain_overflow_signed, /* complain_on_overflow */
1340 ppc_elf_unhandled_reloc, /* special_function */
1341 "R_PPC_GOT_TPREL16", /* name */
1342 FALSE, /* partial_inplace */
1343 0, /* src_mask */
1344 0xffff, /* dst_mask */
1345 FALSE), /* pcrel_offset */
1347 /* Like GOT_TPREL16, but no overflow. */
1348 HOWTO (R_PPC_GOT_TPREL16_LO,
1349 0, /* rightshift */
1350 1, /* size (0 = byte, 1 = short, 2 = long) */
1351 16, /* bitsize */
1352 FALSE, /* pc_relative */
1353 0, /* bitpos */
1354 complain_overflow_dont, /* complain_on_overflow */
1355 ppc_elf_unhandled_reloc, /* special_function */
1356 "R_PPC_GOT_TPREL16_LO", /* name */
1357 FALSE, /* partial_inplace */
1358 0, /* src_mask */
1359 0xffff, /* dst_mask */
1360 FALSE), /* pcrel_offset */
1362 /* Like GOT_TPREL16_LO, but next higher group of 16 bits. */
1363 HOWTO (R_PPC_GOT_TPREL16_HI,
1364 16, /* rightshift */
1365 1, /* size (0 = byte, 1 = short, 2 = long) */
1366 16, /* bitsize */
1367 FALSE, /* pc_relative */
1368 0, /* bitpos */
1369 complain_overflow_dont, /* complain_on_overflow */
1370 ppc_elf_unhandled_reloc, /* special_function */
1371 "R_PPC_GOT_TPREL16_HI", /* name */
1372 FALSE, /* partial_inplace */
1373 0, /* src_mask */
1374 0xffff, /* dst_mask */
1375 FALSE), /* pcrel_offset */
1377 /* Like GOT_TPREL16_HI, but adjust for low 16 bits. */
1378 HOWTO (R_PPC_GOT_TPREL16_HA,
1379 16, /* rightshift */
1380 1, /* size (0 = byte, 1 = short, 2 = long) */
1381 16, /* bitsize */
1382 FALSE, /* pc_relative */
1383 0, /* bitpos */
1384 complain_overflow_dont, /* complain_on_overflow */
1385 ppc_elf_unhandled_reloc, /* special_function */
1386 "R_PPC_GOT_TPREL16_HA", /* name */
1387 FALSE, /* partial_inplace */
1388 0, /* src_mask */
1389 0xffff, /* dst_mask */
1390 FALSE), /* pcrel_offset */
1392 /* The remaining relocs are from the Embedded ELF ABI, and are not
1393 in the SVR4 ELF ABI. */
1395 /* 32 bit value resulting from the addend minus the symbol. */
1396 HOWTO (R_PPC_EMB_NADDR32, /* type */
1397 0, /* rightshift */
1398 2, /* size (0 = byte, 1 = short, 2 = long) */
1399 32, /* bitsize */
1400 FALSE, /* pc_relative */
1401 0, /* bitpos */
1402 complain_overflow_bitfield, /* complain_on_overflow */
1403 bfd_elf_generic_reloc, /* special_function */
1404 "R_PPC_EMB_NADDR32", /* name */
1405 FALSE, /* partial_inplace */
1406 0, /* src_mask */
1407 0xffffffff, /* dst_mask */
1408 FALSE), /* pcrel_offset */
1410 /* 16 bit value resulting from the addend minus the symbol. */
1411 HOWTO (R_PPC_EMB_NADDR16, /* type */
1412 0, /* rightshift */
1413 1, /* size (0 = byte, 1 = short, 2 = long) */
1414 16, /* bitsize */
1415 FALSE, /* pc_relative */
1416 0, /* bitpos */
1417 complain_overflow_bitfield, /* complain_on_overflow */
1418 bfd_elf_generic_reloc, /* special_function */
1419 "R_PPC_EMB_NADDR16", /* name */
1420 FALSE, /* partial_inplace */
1421 0, /* src_mask */
1422 0xffff, /* dst_mask */
1423 FALSE), /* pcrel_offset */
1425 /* 16 bit value resulting from the addend minus the symbol. */
1426 HOWTO (R_PPC_EMB_NADDR16_LO, /* type */
1427 0, /* rightshift */
1428 1, /* size (0 = byte, 1 = short, 2 = long) */
1429 16, /* bitsize */
1430 FALSE, /* pc_relative */
1431 0, /* bitpos */
1432 complain_overflow_dont,/* complain_on_overflow */
1433 bfd_elf_generic_reloc, /* special_function */
1434 "R_PPC_EMB_ADDR16_LO", /* name */
1435 FALSE, /* partial_inplace */
1436 0, /* src_mask */
1437 0xffff, /* dst_mask */
1438 FALSE), /* pcrel_offset */
1440 /* The high order 16 bits of the addend minus the symbol. */
1441 HOWTO (R_PPC_EMB_NADDR16_HI, /* type */
1442 16, /* rightshift */
1443 1, /* size (0 = byte, 1 = short, 2 = long) */
1444 16, /* bitsize */
1445 FALSE, /* pc_relative */
1446 0, /* bitpos */
1447 complain_overflow_dont, /* complain_on_overflow */
1448 bfd_elf_generic_reloc, /* special_function */
1449 "R_PPC_EMB_NADDR16_HI", /* name */
1450 FALSE, /* partial_inplace */
1451 0, /* src_mask */
1452 0xffff, /* dst_mask */
1453 FALSE), /* pcrel_offset */
1455 /* The high order 16 bits of the result of the addend minus the address,
1456 plus 1 if the contents of the low 16 bits, treated as a signed number,
1457 is negative. */
1458 HOWTO (R_PPC_EMB_NADDR16_HA, /* type */
1459 16, /* rightshift */
1460 1, /* size (0 = byte, 1 = short, 2 = long) */
1461 16, /* bitsize */
1462 FALSE, /* pc_relative */
1463 0, /* bitpos */
1464 complain_overflow_dont, /* complain_on_overflow */
1465 ppc_elf_addr16_ha_reloc, /* special_function */
1466 "R_PPC_EMB_NADDR16_HA", /* name */
1467 FALSE, /* partial_inplace */
1468 0, /* src_mask */
1469 0xffff, /* dst_mask */
1470 FALSE), /* pcrel_offset */
1472 /* 16 bit value resulting from allocating a 4 byte word to hold an
1473 address in the .sdata section, and returning the offset from
1474 _SDA_BASE_ for that relocation. */
1475 HOWTO (R_PPC_EMB_SDAI16, /* type */
1476 0, /* rightshift */
1477 1, /* size (0 = byte, 1 = short, 2 = long) */
1478 16, /* bitsize */
1479 FALSE, /* pc_relative */
1480 0, /* bitpos */
1481 complain_overflow_bitfield, /* complain_on_overflow */
1482 bfd_elf_generic_reloc, /* special_function */
1483 "R_PPC_EMB_SDAI16", /* name */
1484 FALSE, /* partial_inplace */
1485 0, /* src_mask */
1486 0xffff, /* dst_mask */
1487 FALSE), /* pcrel_offset */
1489 /* 16 bit value resulting from allocating a 4 byte word to hold an
1490 address in the .sdata2 section, and returning the offset from
1491 _SDA2_BASE_ for that relocation. */
1492 HOWTO (R_PPC_EMB_SDA2I16, /* type */
1493 0, /* rightshift */
1494 1, /* size (0 = byte, 1 = short, 2 = long) */
1495 16, /* bitsize */
1496 FALSE, /* pc_relative */
1497 0, /* bitpos */
1498 complain_overflow_bitfield, /* complain_on_overflow */
1499 bfd_elf_generic_reloc, /* special_function */
1500 "R_PPC_EMB_SDA2I16", /* name */
1501 FALSE, /* partial_inplace */
1502 0, /* src_mask */
1503 0xffff, /* dst_mask */
1504 FALSE), /* pcrel_offset */
1506 /* A sign-extended 16 bit value relative to _SDA2_BASE_, for use with
1507 small data items. */
1508 HOWTO (R_PPC_EMB_SDA2REL, /* type */
1509 0, /* rightshift */
1510 1, /* size (0 = byte, 1 = short, 2 = long) */
1511 16, /* bitsize */
1512 FALSE, /* pc_relative */
1513 0, /* bitpos */
1514 complain_overflow_signed, /* complain_on_overflow */
1515 bfd_elf_generic_reloc, /* special_function */
1516 "R_PPC_EMB_SDA2REL", /* name */
1517 FALSE, /* partial_inplace */
1518 0, /* src_mask */
1519 0xffff, /* dst_mask */
1520 FALSE), /* pcrel_offset */
1522 /* Relocate against either _SDA_BASE_ or _SDA2_BASE_, filling in the 16 bit
1523 signed offset from the appropriate base, and filling in the register
1524 field with the appropriate register (0, 2, or 13). */
1525 HOWTO (R_PPC_EMB_SDA21, /* type */
1526 0, /* rightshift */
1527 2, /* size (0 = byte, 1 = short, 2 = long) */
1528 16, /* bitsize */
1529 FALSE, /* pc_relative */
1530 0, /* bitpos */
1531 complain_overflow_signed, /* complain_on_overflow */
1532 bfd_elf_generic_reloc, /* special_function */
1533 "R_PPC_EMB_SDA21", /* name */
1534 FALSE, /* partial_inplace */
1535 0, /* src_mask */
1536 0xffff, /* dst_mask */
1537 FALSE), /* pcrel_offset */
1539 /* Relocation not handled: R_PPC_EMB_MRKREF */
1540 /* Relocation not handled: R_PPC_EMB_RELSEC16 */
1541 /* Relocation not handled: R_PPC_EMB_RELST_LO */
1542 /* Relocation not handled: R_PPC_EMB_RELST_HI */
1543 /* Relocation not handled: R_PPC_EMB_RELST_HA */
1544 /* Relocation not handled: R_PPC_EMB_BIT_FLD */
1546 /* PC relative relocation against either _SDA_BASE_ or _SDA2_BASE_, filling
1547 in the 16 bit signed offset from the appropriate base, and filling in the
1548 register field with the appropriate register (0, 2, or 13). */
1549 HOWTO (R_PPC_EMB_RELSDA, /* type */
1550 0, /* rightshift */
1551 1, /* size (0 = byte, 1 = short, 2 = long) */
1552 16, /* bitsize */
1553 TRUE, /* pc_relative */
1554 0, /* bitpos */
1555 complain_overflow_signed, /* complain_on_overflow */
1556 bfd_elf_generic_reloc, /* special_function */
1557 "R_PPC_EMB_RELSDA", /* name */
1558 FALSE, /* partial_inplace */
1559 0, /* src_mask */
1560 0xffff, /* dst_mask */
1561 FALSE), /* pcrel_offset */
1563 /* GNU extension to record C++ vtable hierarchy. */
1564 HOWTO (R_PPC_GNU_VTINHERIT, /* type */
1565 0, /* rightshift */
1566 0, /* size (0 = byte, 1 = short, 2 = long) */
1567 0, /* bitsize */
1568 FALSE, /* pc_relative */
1569 0, /* bitpos */
1570 complain_overflow_dont, /* complain_on_overflow */
1571 NULL, /* special_function */
1572 "R_PPC_GNU_VTINHERIT", /* name */
1573 FALSE, /* partial_inplace */
1574 0, /* src_mask */
1575 0, /* dst_mask */
1576 FALSE), /* pcrel_offset */
1578 /* GNU extension to record C++ vtable member usage. */
1579 HOWTO (R_PPC_GNU_VTENTRY, /* type */
1580 0, /* rightshift */
1581 0, /* size (0 = byte, 1 = short, 2 = long) */
1582 0, /* bitsize */
1583 FALSE, /* pc_relative */
1584 0, /* bitpos */
1585 complain_overflow_dont, /* complain_on_overflow */
1586 NULL, /* special_function */
1587 "R_PPC_GNU_VTENTRY", /* name */
1588 FALSE, /* partial_inplace */
1589 0, /* src_mask */
1590 0, /* dst_mask */
1591 FALSE), /* pcrel_offset */
1593 /* Phony reloc to handle AIX style TOC entries. */
1594 HOWTO (R_PPC_TOC16, /* type */
1595 0, /* rightshift */
1596 1, /* size (0 = byte, 1 = short, 2 = long) */
1597 16, /* bitsize */
1598 FALSE, /* pc_relative */
1599 0, /* bitpos */
1600 complain_overflow_signed, /* complain_on_overflow */
1601 bfd_elf_generic_reloc, /* special_function */
1602 "R_PPC_TOC16", /* name */
1603 FALSE, /* partial_inplace */
1604 0, /* src_mask */
1605 0xffff, /* dst_mask */
1606 FALSE), /* pcrel_offset */
1609 /* Initialize the ppc_elf_howto_table, so that linear accesses can be done. */
1611 static void
1612 ppc_elf_howto_init ()
1614 unsigned int i, type;
1616 for (i = 0; i < sizeof (ppc_elf_howto_raw) / sizeof (ppc_elf_howto_raw[0]); i++)
1618 type = ppc_elf_howto_raw[i].type;
1619 BFD_ASSERT (type < sizeof (ppc_elf_howto_table) / sizeof (ppc_elf_howto_table[0]));
1620 ppc_elf_howto_table[type] = &ppc_elf_howto_raw[i];
1624 /* This function handles relaxing for the PPC with option --mpc860c0[=<n>].
1626 The MPC860, revision C0 or earlier contains a bug in the die.
1627 If all of the following conditions are true, the next instruction
1628 to be executed *may* be treated as a no-op.
1629 1/ A forward branch is executed.
1630 2/ The branch is predicted as not taken.
1631 3/ The branch is taken.
1632 4/ The branch is located in the last 5 words of a page.
1633 (The EOP limit is 5 by default but may be specified as any value from 1-10.)
1635 Our software solution is to detect these problematic branches in a
1636 linker pass and modify them as follows:
1637 1/ Unconditional branches - Since these are always predicted taken,
1638 there is no problem and no action is required.
1639 2/ Conditional backward branches - No problem, no action required.
1640 3/ Conditional forward branches - Ensure that the "inverse prediction
1641 bit" is set (ensure it is predicted taken).
1642 4/ Conditional register branches - Ensure that the "y bit" is set
1643 (ensure it is predicted taken). */
1645 /* Sort sections by address. */
1647 static int
1648 ppc_elf_sort_rela (arg1, arg2)
1649 const PTR arg1;
1650 const PTR arg2;
1652 const Elf_Internal_Rela **rela1 = (const Elf_Internal_Rela**) arg1;
1653 const Elf_Internal_Rela **rela2 = (const Elf_Internal_Rela**) arg2;
1655 /* Sort by offset. */
1656 return ((*rela1)->r_offset - (*rela2)->r_offset);
1659 static bfd_boolean
1660 ppc_elf_relax_section (abfd, isec, link_info, again)
1661 bfd *abfd;
1662 asection *isec;
1663 struct bfd_link_info *link_info;
1664 bfd_boolean *again;
1666 #define PAGESIZE 0x1000
1668 bfd_byte *contents = NULL;
1669 bfd_byte *free_contents = NULL;
1670 Elf_Internal_Rela *internal_relocs = NULL;
1671 Elf_Internal_Rela *free_relocs = NULL;
1672 Elf_Internal_Rela **rela_comb = NULL;
1673 int comb_curr, comb_count;
1675 /* We never have to do this more than once per input section. */
1676 *again = FALSE;
1678 /* If needed, initialize this section's cooked size. */
1679 if (isec->_cooked_size == 0)
1680 isec->_cooked_size = isec->_raw_size;
1682 /* We're only interested in text sections which overlap the
1683 troublesome area at the end of a page. */
1684 if (link_info->mpc860c0 && (isec->flags & SEC_CODE) && isec->_cooked_size)
1686 bfd_vma dot, end_page, end_section;
1687 bfd_boolean section_modified;
1689 /* Get the section contents. */
1690 /* Get cached copy if it exists. */
1691 if (elf_section_data (isec)->this_hdr.contents != NULL)
1692 contents = elf_section_data (isec)->this_hdr.contents;
1693 else
1695 /* Go get them off disk. */
1696 contents = (bfd_byte *) bfd_malloc (isec->_raw_size);
1697 if (contents == NULL)
1698 goto error_return;
1699 free_contents = contents;
1701 if (! bfd_get_section_contents (abfd, isec, contents,
1702 (file_ptr) 0, isec->_raw_size))
1703 goto error_return;
1706 comb_curr = 0;
1707 comb_count = 0;
1708 if (isec->reloc_count)
1710 unsigned n;
1711 bfd_size_type amt;
1713 /* Get a copy of the native relocations. */
1714 internal_relocs
1715 = _bfd_elf32_link_read_relocs (abfd, isec, (PTR) NULL,
1716 (Elf_Internal_Rela *) NULL,
1717 link_info->keep_memory);
1718 if (internal_relocs == NULL)
1719 goto error_return;
1720 if (! link_info->keep_memory)
1721 free_relocs = internal_relocs;
1723 /* Setup a faster access method for the reloc info we need. */
1724 amt = isec->reloc_count;
1725 amt *= sizeof (Elf_Internal_Rela*);
1726 rela_comb = (Elf_Internal_Rela**) bfd_malloc (amt);
1727 if (rela_comb == NULL)
1728 goto error_return;
1729 for (n = 0; n < isec->reloc_count; ++n)
1731 long r_type;
1733 r_type = ELF32_R_TYPE (internal_relocs[n].r_info);
1734 if (r_type < 0 || r_type >= (int) R_PPC_max)
1735 goto error_return;
1737 /* Prologue constants are sometimes present in the ".text"
1738 sections and they can be identified by their associated
1739 relocation. We don't want to process those words and
1740 some others which can also be identified by their
1741 relocations. However, not all conditional branches will
1742 have a relocation so we will only ignore words that
1743 1) have a reloc, and 2) the reloc is not applicable to a
1744 conditional branch. The array rela_comb is built here
1745 for use in the EOP scan loop. */
1746 switch (r_type)
1748 case R_PPC_ADDR14_BRNTAKEN:
1749 case R_PPC_REL14:
1750 case R_PPC_REL14_BRNTAKEN:
1751 /* We should check the instruction. */
1752 break;
1753 default:
1754 /* The word is not a conditional branch - ignore it. */
1755 rela_comb[comb_count++] = &internal_relocs[n];
1756 break;
1759 if (comb_count > 1)
1760 qsort (rela_comb, (size_t) comb_count, sizeof (int),
1761 ppc_elf_sort_rela);
1764 /* Enumerate each EOP region that overlaps this section. */
1765 end_section = isec->vma + isec->_cooked_size;
1766 dot = end_page = (isec->vma | (PAGESIZE - 1)) + 1;
1767 dot -= link_info->mpc860c0;
1768 section_modified = FALSE;
1769 /* Increment the start position if this section begins in the
1770 middle of its first EOP region. */
1771 if (dot < isec->vma)
1772 dot = isec->vma;
1773 for (;
1774 dot < end_section;
1775 dot += PAGESIZE, end_page += PAGESIZE)
1777 /* Check each word in this EOP region. */
1778 for (; dot < end_page; dot += 4)
1780 bfd_vma isec_offset;
1781 unsigned long insn;
1782 bfd_boolean skip, modified;
1784 /* Don't process this word if there is a relocation for it
1785 and the relocation indicates the word is not a
1786 conditional branch. */
1787 skip = FALSE;
1788 isec_offset = dot - isec->vma;
1789 for (; comb_curr<comb_count; ++comb_curr)
1791 bfd_vma r_offset;
1793 r_offset = rela_comb[comb_curr]->r_offset;
1794 if (r_offset >= isec_offset)
1796 if (r_offset == isec_offset) skip = TRUE;
1797 break;
1800 if (skip) continue;
1802 /* Check the current word for a problematic conditional
1803 branch. */
1804 #define BO0(insn) ((insn) & 0x02000000)
1805 #define BO2(insn) ((insn) & 0x00800000)
1806 #define BO4(insn) ((insn) & 0x00200000)
1807 insn = (unsigned long) bfd_get_32 (abfd, contents + isec_offset);
1808 modified = FALSE;
1809 if ((insn & 0xFc000000) == 0x40000000)
1811 /* Instruction is BCx */
1812 if ((!BO0(insn) || !BO2(insn)) && !BO4(insn))
1814 bfd_vma target;
1816 /* This branch is predicted as "normal".
1817 If this is a forward branch, it is problematic. */
1818 target = insn & 0x0000Fffc;
1819 target = (target ^ 0x8000) - 0x8000;
1820 if ((insn & 0x00000002) == 0)
1821 /* Convert to abs. */
1822 target += dot;
1823 if (target > dot)
1825 /* Set the prediction bit. */
1826 insn |= 0x00200000;
1827 modified = TRUE;
1831 else if ((insn & 0xFc00Fffe) == 0x4c000420)
1833 /* Instruction is BCCTRx. */
1834 if ((!BO0(insn) || !BO2(insn)) && !BO4(insn))
1836 /* This branch is predicted as not-taken.
1837 If this is a forward branch, it is problematic.
1838 Since we can't tell statically if it will branch
1839 forward, always set the prediction bit. */
1840 insn |= 0x00200000;
1841 modified = TRUE;
1844 else if ((insn & 0xFc00Fffe) == 0x4c000020)
1846 /* Instruction is BCLRx */
1847 if ((!BO0(insn) || !BO2(insn)) && !BO4(insn))
1849 /* This branch is predicted as not-taken.
1850 If this is a forward branch, it is problematic.
1851 Since we can't tell statically if it will branch
1852 forward, always set the prediction bit. */
1853 insn |= 0x00200000;
1854 modified = TRUE;
1857 #undef BO0
1858 #undef BO2
1859 #undef BO4
1860 if (modified)
1862 bfd_put_32 (abfd, (bfd_vma) insn, contents + isec_offset);
1863 section_modified = TRUE;
1867 if (section_modified)
1869 elf_section_data (isec)->this_hdr.contents = contents;
1870 free_contents = NULL;
1874 if (rela_comb != NULL)
1876 free (rela_comb);
1877 rela_comb = NULL;
1880 if (free_relocs != NULL)
1882 free (free_relocs);
1883 free_relocs = NULL;
1886 if (free_contents != NULL)
1888 if (! link_info->keep_memory)
1889 free (free_contents);
1890 else
1892 /* Cache the section contents for elf_link_input_bfd. */
1893 elf_section_data (isec)->this_hdr.contents = contents;
1895 free_contents = NULL;
1898 return TRUE;
1900 error_return:
1901 if (rela_comb != NULL)
1902 free (rela_comb);
1903 if (free_relocs != NULL)
1904 free (free_relocs);
1905 if (free_contents != NULL)
1906 free (free_contents);
1907 return FALSE;
1910 static reloc_howto_type *
1911 ppc_elf_reloc_type_lookup (abfd, code)
1912 bfd *abfd ATTRIBUTE_UNUSED;
1913 bfd_reloc_code_real_type code;
1915 enum elf_ppc_reloc_type r;
1917 if (!ppc_elf_howto_table[R_PPC_ADDR32])
1918 /* Initialize howto table if needed. */
1919 ppc_elf_howto_init ();
1921 switch ((int) code)
1923 default:
1924 return (reloc_howto_type *) NULL;
1926 case BFD_RELOC_NONE: r = R_PPC_NONE; break;
1927 case BFD_RELOC_32: r = R_PPC_ADDR32; break;
1928 case BFD_RELOC_PPC_BA26: r = R_PPC_ADDR24; break;
1929 case BFD_RELOC_16: r = R_PPC_ADDR16; break;
1930 case BFD_RELOC_LO16: r = R_PPC_ADDR16_LO; break;
1931 case BFD_RELOC_HI16: r = R_PPC_ADDR16_HI; break;
1932 case BFD_RELOC_HI16_S: r = R_PPC_ADDR16_HA; break;
1933 case BFD_RELOC_PPC_BA16: r = R_PPC_ADDR14; break;
1934 case BFD_RELOC_PPC_BA16_BRTAKEN: r = R_PPC_ADDR14_BRTAKEN; break;
1935 case BFD_RELOC_PPC_BA16_BRNTAKEN: r = R_PPC_ADDR14_BRNTAKEN; break;
1936 case BFD_RELOC_PPC_B26: r = R_PPC_REL24; break;
1937 case BFD_RELOC_PPC_B16: r = R_PPC_REL14; break;
1938 case BFD_RELOC_PPC_B16_BRTAKEN: r = R_PPC_REL14_BRTAKEN; break;
1939 case BFD_RELOC_PPC_B16_BRNTAKEN: r = R_PPC_REL14_BRNTAKEN; break;
1940 case BFD_RELOC_16_GOTOFF: r = R_PPC_GOT16; break;
1941 case BFD_RELOC_LO16_GOTOFF: r = R_PPC_GOT16_LO; break;
1942 case BFD_RELOC_HI16_GOTOFF: r = R_PPC_GOT16_HI; break;
1943 case BFD_RELOC_HI16_S_GOTOFF: r = R_PPC_GOT16_HA; break;
1944 case BFD_RELOC_24_PLT_PCREL: r = R_PPC_PLTREL24; break;
1945 case BFD_RELOC_PPC_COPY: r = R_PPC_COPY; break;
1946 case BFD_RELOC_PPC_GLOB_DAT: r = R_PPC_GLOB_DAT; break;
1947 case BFD_RELOC_PPC_LOCAL24PC: r = R_PPC_LOCAL24PC; break;
1948 case BFD_RELOC_32_PCREL: r = R_PPC_REL32; break;
1949 case BFD_RELOC_32_PLTOFF: r = R_PPC_PLT32; break;
1950 case BFD_RELOC_32_PLT_PCREL: r = R_PPC_PLTREL32; break;
1951 case BFD_RELOC_LO16_PLTOFF: r = R_PPC_PLT16_LO; break;
1952 case BFD_RELOC_HI16_PLTOFF: r = R_PPC_PLT16_HI; break;
1953 case BFD_RELOC_HI16_S_PLTOFF: r = R_PPC_PLT16_HA; break;
1954 case BFD_RELOC_GPREL16: r = R_PPC_SDAREL16; break;
1955 case BFD_RELOC_16_BASEREL: r = R_PPC_SECTOFF; break;
1956 case BFD_RELOC_LO16_BASEREL: r = R_PPC_SECTOFF_LO; break;
1957 case BFD_RELOC_HI16_BASEREL: r = R_PPC_SECTOFF_HI; break;
1958 case BFD_RELOC_HI16_S_BASEREL: r = R_PPC_SECTOFF_HA; break;
1959 case BFD_RELOC_CTOR: r = R_PPC_ADDR32; break;
1960 case BFD_RELOC_PPC_TOC16: r = R_PPC_TOC16; break;
1961 case BFD_RELOC_PPC_TLS: r = R_PPC_TLS; break;
1962 case BFD_RELOC_PPC_DTPMOD: r = R_PPC_DTPMOD32; break;
1963 case BFD_RELOC_PPC_TPREL16: r = R_PPC_TPREL16; break;
1964 case BFD_RELOC_PPC_TPREL16_LO: r = R_PPC_TPREL16_LO; break;
1965 case BFD_RELOC_PPC_TPREL16_HI: r = R_PPC_TPREL16_HI; break;
1966 case BFD_RELOC_PPC_TPREL16_HA: r = R_PPC_TPREL16_HA; break;
1967 case BFD_RELOC_PPC_TPREL: r = R_PPC_TPREL32; break;
1968 case BFD_RELOC_PPC_DTPREL16: r = R_PPC_DTPREL16; break;
1969 case BFD_RELOC_PPC_DTPREL16_LO: r = R_PPC_DTPREL16_LO; break;
1970 case BFD_RELOC_PPC_DTPREL16_HI: r = R_PPC_DTPREL16_HI; break;
1971 case BFD_RELOC_PPC_DTPREL16_HA: r = R_PPC_DTPREL16_HA; break;
1972 case BFD_RELOC_PPC_DTPREL: r = R_PPC_DTPREL32; break;
1973 case BFD_RELOC_PPC_GOT_TLSGD16: r = R_PPC_GOT_TLSGD16; break;
1974 case BFD_RELOC_PPC_GOT_TLSGD16_LO: r = R_PPC_GOT_TLSGD16_LO; break;
1975 case BFD_RELOC_PPC_GOT_TLSGD16_HI: r = R_PPC_GOT_TLSGD16_HI; break;
1976 case BFD_RELOC_PPC_GOT_TLSGD16_HA: r = R_PPC_GOT_TLSGD16_HA; break;
1977 case BFD_RELOC_PPC_GOT_TLSLD16: r = R_PPC_GOT_TLSLD16; break;
1978 case BFD_RELOC_PPC_GOT_TLSLD16_LO: r = R_PPC_GOT_TLSLD16_LO; break;
1979 case BFD_RELOC_PPC_GOT_TLSLD16_HI: r = R_PPC_GOT_TLSLD16_HI; break;
1980 case BFD_RELOC_PPC_GOT_TLSLD16_HA: r = R_PPC_GOT_TLSLD16_HA; break;
1981 case BFD_RELOC_PPC_GOT_TPREL16: r = R_PPC_GOT_TPREL16; break;
1982 case BFD_RELOC_PPC_GOT_TPREL16_LO: r = R_PPC_GOT_TPREL16_LO; break;
1983 case BFD_RELOC_PPC_GOT_TPREL16_HI: r = R_PPC_GOT_TPREL16_HI; break;
1984 case BFD_RELOC_PPC_GOT_TPREL16_HA: r = R_PPC_GOT_TPREL16_HA; break;
1985 case BFD_RELOC_PPC_GOT_DTPREL16: r = R_PPC_GOT_DTPREL16; break;
1986 case BFD_RELOC_PPC_GOT_DTPREL16_LO: r = R_PPC_GOT_DTPREL16_LO; break;
1987 case BFD_RELOC_PPC_GOT_DTPREL16_HI: r = R_PPC_GOT_DTPREL16_HI; break;
1988 case BFD_RELOC_PPC_GOT_DTPREL16_HA: r = R_PPC_GOT_DTPREL16_HA; break;
1989 case BFD_RELOC_PPC_EMB_NADDR32: r = R_PPC_EMB_NADDR32; break;
1990 case BFD_RELOC_PPC_EMB_NADDR16: r = R_PPC_EMB_NADDR16; break;
1991 case BFD_RELOC_PPC_EMB_NADDR16_LO: r = R_PPC_EMB_NADDR16_LO; break;
1992 case BFD_RELOC_PPC_EMB_NADDR16_HI: r = R_PPC_EMB_NADDR16_HI; break;
1993 case BFD_RELOC_PPC_EMB_NADDR16_HA: r = R_PPC_EMB_NADDR16_HA; break;
1994 case BFD_RELOC_PPC_EMB_SDAI16: r = R_PPC_EMB_SDAI16; break;
1995 case BFD_RELOC_PPC_EMB_SDA2I16: r = R_PPC_EMB_SDA2I16; break;
1996 case BFD_RELOC_PPC_EMB_SDA2REL: r = R_PPC_EMB_SDA2REL; break;
1997 case BFD_RELOC_PPC_EMB_SDA21: r = R_PPC_EMB_SDA21; break;
1998 case BFD_RELOC_PPC_EMB_MRKREF: r = R_PPC_EMB_MRKREF; break;
1999 case BFD_RELOC_PPC_EMB_RELSEC16: r = R_PPC_EMB_RELSEC16; break;
2000 case BFD_RELOC_PPC_EMB_RELST_LO: r = R_PPC_EMB_RELST_LO; break;
2001 case BFD_RELOC_PPC_EMB_RELST_HI: r = R_PPC_EMB_RELST_HI; break;
2002 case BFD_RELOC_PPC_EMB_RELST_HA: r = R_PPC_EMB_RELST_HA; break;
2003 case BFD_RELOC_PPC_EMB_BIT_FLD: r = R_PPC_EMB_BIT_FLD; break;
2004 case BFD_RELOC_PPC_EMB_RELSDA: r = R_PPC_EMB_RELSDA; break;
2005 case BFD_RELOC_VTABLE_INHERIT: r = R_PPC_GNU_VTINHERIT; break;
2006 case BFD_RELOC_VTABLE_ENTRY: r = R_PPC_GNU_VTENTRY; break;
2009 return ppc_elf_howto_table[(int) r];
2012 /* Set the howto pointer for a PowerPC ELF reloc. */
2014 static void
2015 ppc_elf_info_to_howto (abfd, cache_ptr, dst)
2016 bfd *abfd ATTRIBUTE_UNUSED;
2017 arelent *cache_ptr;
2018 Elf_Internal_Rela *dst;
2020 if (!ppc_elf_howto_table[R_PPC_ADDR32])
2021 /* Initialize howto table if needed. */
2022 ppc_elf_howto_init ();
2024 BFD_ASSERT (ELF32_R_TYPE (dst->r_info) < (unsigned int) R_PPC_max);
2025 cache_ptr->howto = ppc_elf_howto_table[ELF32_R_TYPE (dst->r_info)];
2028 /* Handle the R_PPC_ADDR16_HA reloc. */
2030 static bfd_reloc_status_type
2031 ppc_elf_addr16_ha_reloc (abfd, reloc_entry, symbol, data, input_section,
2032 output_bfd, error_message)
2033 bfd *abfd ATTRIBUTE_UNUSED;
2034 arelent *reloc_entry;
2035 asymbol *symbol;
2036 PTR data ATTRIBUTE_UNUSED;
2037 asection *input_section;
2038 bfd *output_bfd;
2039 char **error_message ATTRIBUTE_UNUSED;
2041 bfd_vma relocation;
2043 if (output_bfd != NULL)
2045 reloc_entry->address += input_section->output_offset;
2046 return bfd_reloc_ok;
2049 if (reloc_entry->address > input_section->_cooked_size)
2050 return bfd_reloc_outofrange;
2052 if (bfd_is_com_section (symbol->section))
2053 relocation = 0;
2054 else
2055 relocation = symbol->value;
2057 relocation += symbol->section->output_section->vma;
2058 relocation += symbol->section->output_offset;
2059 relocation += reloc_entry->addend;
2061 reloc_entry->addend += (relocation & 0x8000) << 1;
2063 return bfd_reloc_continue;
2066 static bfd_reloc_status_type
2067 ppc_elf_unhandled_reloc (abfd, reloc_entry, symbol, data,
2068 input_section, output_bfd, error_message)
2069 bfd *abfd;
2070 arelent *reloc_entry;
2071 asymbol *symbol;
2072 PTR data;
2073 asection *input_section;
2074 bfd *output_bfd;
2075 char **error_message;
2077 /* If this is a relocatable link (output_bfd test tells us), just
2078 call the generic function. Any adjustment will be done at final
2079 link time. */
2080 if (output_bfd != NULL)
2081 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2082 input_section, output_bfd, error_message);
2084 if (error_message != NULL)
2086 static char buf[60];
2087 sprintf (buf, "generic linker can't handle %s",
2088 reloc_entry->howto->name);
2089 *error_message = buf;
2091 return bfd_reloc_dangerous;
2094 /* Fix bad default arch selected for a 32 bit input bfd when the
2095 default is 64 bit. */
2097 static bfd_boolean
2098 ppc_elf_object_p (abfd)
2099 bfd *abfd;
2101 if (abfd->arch_info->the_default && abfd->arch_info->bits_per_word == 64)
2103 Elf_Internal_Ehdr *i_ehdr = elf_elfheader (abfd);
2105 if (i_ehdr->e_ident[EI_CLASS] == ELFCLASS32)
2107 /* Relies on arch after 64 bit default being 32 bit default. */
2108 abfd->arch_info = abfd->arch_info->next;
2109 BFD_ASSERT (abfd->arch_info->bits_per_word == 32);
2112 return TRUE;
2115 /* Function to set whether a module needs the -mrelocatable bit set. */
2117 static bfd_boolean
2118 ppc_elf_set_private_flags (abfd, flags)
2119 bfd *abfd;
2120 flagword flags;
2122 BFD_ASSERT (!elf_flags_init (abfd)
2123 || elf_elfheader (abfd)->e_flags == flags);
2125 elf_elfheader (abfd)->e_flags = flags;
2126 elf_flags_init (abfd) = TRUE;
2127 return TRUE;
2130 /* Merge backend specific data from an object file to the output
2131 object file when linking. */
2133 static bfd_boolean
2134 ppc_elf_merge_private_bfd_data (ibfd, obfd)
2135 bfd *ibfd;
2136 bfd *obfd;
2138 flagword old_flags;
2139 flagword new_flags;
2140 bfd_boolean error;
2142 /* Check if we have the same endianess. */
2143 if (! _bfd_generic_verify_endian_match (ibfd, obfd))
2144 return FALSE;
2146 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
2147 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
2148 return TRUE;
2150 new_flags = elf_elfheader (ibfd)->e_flags;
2151 old_flags = elf_elfheader (obfd)->e_flags;
2152 if (!elf_flags_init (obfd))
2154 /* First call, no flags set. */
2155 elf_flags_init (obfd) = TRUE;
2156 elf_elfheader (obfd)->e_flags = new_flags;
2159 /* Compatible flags are ok. */
2160 else if (new_flags == old_flags)
2163 /* Incompatible flags. */
2164 else
2166 /* Warn about -mrelocatable mismatch. Allow -mrelocatable-lib
2167 to be linked with either. */
2168 error = FALSE;
2169 if ((new_flags & EF_PPC_RELOCATABLE) != 0
2170 && (old_flags & (EF_PPC_RELOCATABLE | EF_PPC_RELOCATABLE_LIB)) == 0)
2172 error = TRUE;
2173 (*_bfd_error_handler)
2174 (_("%s: compiled with -mrelocatable and linked with modules compiled normally"),
2175 bfd_archive_filename (ibfd));
2177 else if ((new_flags & (EF_PPC_RELOCATABLE | EF_PPC_RELOCATABLE_LIB)) == 0
2178 && (old_flags & EF_PPC_RELOCATABLE) != 0)
2180 error = TRUE;
2181 (*_bfd_error_handler)
2182 (_("%s: compiled normally and linked with modules compiled with -mrelocatable"),
2183 bfd_archive_filename (ibfd));
2186 /* The output is -mrelocatable-lib iff both the input files are. */
2187 if (! (new_flags & EF_PPC_RELOCATABLE_LIB))
2188 elf_elfheader (obfd)->e_flags &= ~EF_PPC_RELOCATABLE_LIB;
2190 /* The output is -mrelocatable iff it can't be -mrelocatable-lib,
2191 but each input file is either -mrelocatable or -mrelocatable-lib. */
2192 if (! (elf_elfheader (obfd)->e_flags & EF_PPC_RELOCATABLE_LIB)
2193 && (new_flags & (EF_PPC_RELOCATABLE_LIB | EF_PPC_RELOCATABLE))
2194 && (old_flags & (EF_PPC_RELOCATABLE_LIB | EF_PPC_RELOCATABLE)))
2195 elf_elfheader (obfd)->e_flags |= EF_PPC_RELOCATABLE;
2197 /* Do not warn about eabi vs. V.4 mismatch, just or in the bit if
2198 any module uses it. */
2199 elf_elfheader (obfd)->e_flags |= (new_flags & EF_PPC_EMB);
2201 new_flags &= ~(EF_PPC_RELOCATABLE | EF_PPC_RELOCATABLE_LIB | EF_PPC_EMB);
2202 old_flags &= ~(EF_PPC_RELOCATABLE | EF_PPC_RELOCATABLE_LIB | EF_PPC_EMB);
2204 /* Warn about any other mismatches. */
2205 if (new_flags != old_flags)
2207 error = TRUE;
2208 (*_bfd_error_handler)
2209 (_("%s: uses different e_flags (0x%lx) fields than previous modules (0x%lx)"),
2210 bfd_archive_filename (ibfd), (long) new_flags, (long) old_flags);
2213 if (error)
2215 bfd_set_error (bfd_error_bad_value);
2216 return FALSE;
2220 return TRUE;
2223 /* Handle a PowerPC specific section when reading an object file. This
2224 is called when elfcode.h finds a section with an unknown type. */
2226 static bfd_boolean
2227 ppc_elf_section_from_shdr (abfd, hdr, name)
2228 bfd *abfd;
2229 Elf_Internal_Shdr *hdr;
2230 const char *name;
2232 asection *newsect;
2233 flagword flags;
2235 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name))
2236 return FALSE;
2238 newsect = hdr->bfd_section;
2239 flags = bfd_get_section_flags (abfd, newsect);
2240 if (hdr->sh_flags & SHF_EXCLUDE)
2241 flags |= SEC_EXCLUDE;
2243 if (hdr->sh_type == SHT_ORDERED)
2244 flags |= SEC_SORT_ENTRIES;
2246 bfd_set_section_flags (abfd, newsect, flags);
2247 return TRUE;
2250 /* Set up any other section flags and such that may be necessary. */
2252 static bfd_boolean
2253 ppc_elf_fake_sections (abfd, shdr, asect)
2254 bfd *abfd ATTRIBUTE_UNUSED;
2255 Elf_Internal_Shdr *shdr;
2256 asection *asect;
2258 if ((asect->flags & SEC_EXCLUDE) != 0)
2259 shdr->sh_flags |= SHF_EXCLUDE;
2261 if ((asect->flags & SEC_SORT_ENTRIES) != 0)
2262 shdr->sh_type = SHT_ORDERED;
2264 return TRUE;
2267 /* Create a special linker section */
2268 static elf_linker_section_t *
2269 ppc_elf_create_linker_section (abfd, info, which)
2270 bfd *abfd;
2271 struct bfd_link_info *info;
2272 enum elf_linker_section_enum which;
2274 bfd *dynobj = elf_hash_table (info)->dynobj;
2275 elf_linker_section_t *lsect;
2277 /* Record the first bfd section that needs the special section. */
2278 if (!dynobj)
2279 dynobj = elf_hash_table (info)->dynobj = abfd;
2281 /* If this is the first time, create the section. */
2282 lsect = elf_linker_section (dynobj, which);
2283 if (!lsect)
2285 elf_linker_section_t defaults;
2286 static elf_linker_section_t zero_section;
2288 defaults = zero_section;
2289 defaults.which = which;
2290 defaults.hole_written_p = FALSE;
2291 defaults.alignment = 2;
2293 /* Both of these sections are (technically) created by the user
2294 putting data in them, so they shouldn't be marked
2295 SEC_LINKER_CREATED.
2297 The linker creates them so it has somewhere to attach their
2298 respective symbols. In fact, if they were empty it would
2299 be OK to leave the symbol set to 0 (or any random number), because
2300 the appropriate register should never be used. */
2301 defaults.flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
2302 | SEC_IN_MEMORY);
2304 switch (which)
2306 default:
2307 (*_bfd_error_handler) (_("%s: unknown special linker type %d"),
2308 bfd_get_filename (abfd),
2309 (int) which);
2311 bfd_set_error (bfd_error_bad_value);
2312 return (elf_linker_section_t *) 0;
2314 case LINKER_SECTION_SDATA: /* .sdata/.sbss section */
2315 defaults.name = ".sdata";
2316 defaults.rel_name = ".rela.sdata";
2317 defaults.bss_name = ".sbss";
2318 defaults.sym_name = "_SDA_BASE_";
2319 defaults.sym_offset = 32768;
2320 break;
2322 case LINKER_SECTION_SDATA2: /* .sdata2/.sbss2 section */
2323 defaults.name = ".sdata2";
2324 defaults.rel_name = ".rela.sdata2";
2325 defaults.bss_name = ".sbss2";
2326 defaults.sym_name = "_SDA2_BASE_";
2327 defaults.sym_offset = 32768;
2328 defaults.flags |= SEC_READONLY;
2329 break;
2332 lsect = _bfd_elf_create_linker_section (abfd, info, which, &defaults);
2335 return lsect;
2338 /* If we have a non-zero sized .sbss2 or .PPC.EMB.sbss0 sections, we
2339 need to bump up the number of section headers. */
2341 static int
2342 ppc_elf_additional_program_headers (abfd)
2343 bfd *abfd;
2345 asection *s;
2346 int ret;
2348 ret = 0;
2350 s = bfd_get_section_by_name (abfd, ".interp");
2351 if (s != NULL)
2352 ++ret;
2354 s = bfd_get_section_by_name (abfd, ".sbss2");
2355 if (s != NULL && (s->flags & SEC_LOAD) != 0 && s->_raw_size > 0)
2356 ++ret;
2358 s = bfd_get_section_by_name (abfd, ".PPC.EMB.sbss0");
2359 if (s != NULL && (s->flags & SEC_LOAD) != 0 && s->_raw_size > 0)
2360 ++ret;
2362 return ret;
2365 /* Modify the segment map if needed. */
2367 static bfd_boolean
2368 ppc_elf_modify_segment_map (abfd)
2369 bfd *abfd ATTRIBUTE_UNUSED;
2371 return TRUE;
2374 /* The powerpc .got has a blrl instruction in it. Mark it executable. */
2376 static bfd_boolean
2377 ppc_elf_create_got (abfd, info)
2378 bfd *abfd;
2379 struct bfd_link_info *info;
2381 struct ppc_elf_link_hash_table *htab;
2382 asection *s;
2383 flagword flags;
2385 if (!_bfd_elf_create_got_section (abfd, info))
2386 return FALSE;
2388 htab = ppc_elf_hash_table (info);
2389 htab->got = s = bfd_get_section_by_name (abfd, ".got");
2390 if (s == NULL)
2391 abort ();
2393 flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_HAS_CONTENTS | SEC_IN_MEMORY
2394 | SEC_LINKER_CREATED);
2395 if (!bfd_set_section_flags (abfd, s, flags))
2396 return FALSE;
2398 htab->relgot = bfd_make_section (abfd, ".rela.got");
2399 if (!htab->relgot
2400 || ! bfd_set_section_flags (abfd, htab->relgot,
2401 (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
2402 | SEC_IN_MEMORY | SEC_LINKER_CREATED
2403 | SEC_READONLY))
2404 || ! bfd_set_section_alignment (abfd, htab->relgot, 2))
2405 return FALSE;
2407 return TRUE;
2410 /* We have to create .dynsbss and .rela.sbss here so that they get mapped
2411 to output sections (just like _bfd_elf_create_dynamic_sections has
2412 to create .dynbss and .rela.bss). */
2414 static bfd_boolean
2415 ppc_elf_create_dynamic_sections (abfd, info)
2416 bfd *abfd;
2417 struct bfd_link_info *info;
2419 struct ppc_elf_link_hash_table *htab;
2420 asection *s;
2421 flagword flags;
2423 if (!ppc_elf_create_got (abfd, info))
2424 return FALSE;
2426 if (!_bfd_elf_create_dynamic_sections (abfd, info))
2427 return FALSE;
2429 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
2430 | SEC_LINKER_CREATED);
2432 htab = ppc_elf_hash_table (info);
2433 htab->dynbss = bfd_get_section_by_name (abfd, ".dynbss");
2434 htab->dynsbss = s = bfd_make_section (abfd, ".dynsbss");
2435 if (s == NULL
2436 || ! bfd_set_section_flags (abfd, s, SEC_ALLOC))
2437 return FALSE;
2439 if (! info->shared)
2441 htab->relbss = bfd_get_section_by_name (abfd, ".rela.bss");
2442 htab->relsbss = s = bfd_make_section (abfd, ".rela.sbss");
2443 if (s == NULL
2444 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
2445 || ! bfd_set_section_alignment (abfd, s, 2))
2446 return FALSE;
2449 htab->relplt = bfd_get_section_by_name (abfd, ".rela.plt");
2450 htab->plt = s = bfd_get_section_by_name (abfd, ".plt");
2451 if (s == NULL)
2452 abort ();
2454 flags = SEC_ALLOC | SEC_CODE | SEC_IN_MEMORY | SEC_LINKER_CREATED;
2455 return bfd_set_section_flags (abfd, s, flags);
2458 /* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
2459 copying dynamic variables from a shared lib into an app's dynbss
2460 section, and instead use a dynamic relocation to point into the
2461 shared lib. */
2462 #define ELIMINATE_COPY_RELOCS 1
2464 /* Adjust a symbol defined by a dynamic object and referenced by a
2465 regular object. The current definition is in some section of the
2466 dynamic object, but we're not including those sections. We have to
2467 change the definition to something the rest of the link can
2468 understand. */
2470 static bfd_boolean
2471 ppc_elf_adjust_dynamic_symbol (info, h)
2472 struct bfd_link_info *info;
2473 struct elf_link_hash_entry *h;
2475 struct ppc_elf_link_hash_table *htab;
2476 asection *s;
2477 unsigned int power_of_two;
2479 #ifdef DEBUG
2480 fprintf (stderr, "ppc_elf_adjust_dynamic_symbol called for %s\n",
2481 h->root.root.string);
2482 #endif
2484 /* Make sure we know what is going on here. */
2485 htab = ppc_elf_hash_table (info);
2486 BFD_ASSERT (htab->elf.dynobj != NULL
2487 && ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT)
2488 || h->weakdef != NULL
2489 || ((h->elf_link_hash_flags
2490 & ELF_LINK_HASH_DEF_DYNAMIC) != 0
2491 && (h->elf_link_hash_flags
2492 & ELF_LINK_HASH_REF_REGULAR) != 0
2493 && (h->elf_link_hash_flags
2494 & ELF_LINK_HASH_DEF_REGULAR) == 0)));
2496 /* Deal with function syms. */
2497 if (h->type == STT_FUNC
2498 || (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0)
2500 /* Clear procedure linkage table information for any symbol that
2501 won't need a .plt entry. */
2502 if (! htab->elf.dynamic_sections_created
2503 || SYMBOL_CALLS_LOCAL (info, h)
2504 || h->plt.refcount <= 0)
2506 /* A PLT entry is not required/allowed when:
2508 1. We are not using ld.so; because then the PLT entry
2509 can't be set up, so we can't use one.
2511 2. We know for certain that a call to this symbol
2512 will go to this object.
2514 3. GC has rendered the entry unused. */
2515 h->plt.offset = (bfd_vma) -1;
2516 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
2518 return TRUE;
2520 else
2521 h->plt.offset = (bfd_vma) -1;
2523 /* If this is a weak symbol, and there is a real definition, the
2524 processor independent code will have arranged for us to see the
2525 real definition first, and we can just use the same value. */
2526 if (h->weakdef != NULL)
2528 BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined
2529 || h->weakdef->root.type == bfd_link_hash_defweak);
2530 h->root.u.def.section = h->weakdef->root.u.def.section;
2531 h->root.u.def.value = h->weakdef->root.u.def.value;
2532 if (ELIMINATE_COPY_RELOCS)
2533 h->elf_link_hash_flags
2534 = ((h->elf_link_hash_flags & ~ELF_LINK_NON_GOT_REF)
2535 | (h->weakdef->elf_link_hash_flags & ELF_LINK_NON_GOT_REF));
2536 return TRUE;
2539 /* This is a reference to a symbol defined by a dynamic object which
2540 is not a function. */
2542 /* If we are creating a shared library, we must presume that the
2543 only references to the symbol are via the global offset table.
2544 For such cases we need not do anything here; the relocations will
2545 be handled correctly by relocate_section. */
2546 if (info->shared)
2547 return TRUE;
2549 /* If there are no references to this symbol that do not use the
2550 GOT, we don't need to generate a copy reloc. */
2551 if ((h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0)
2552 return TRUE;
2554 if (ELIMINATE_COPY_RELOCS)
2556 struct ppc_elf_dyn_relocs *p;
2557 for (p = ppc_elf_hash_entry (h)->dyn_relocs; p != NULL; p = p->next)
2559 s = p->sec->output_section;
2560 if (s != NULL && (s->flags & SEC_READONLY) != 0)
2561 break;
2564 /* If we didn't find any dynamic relocs in read-only sections, then
2565 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
2566 if (p == NULL)
2568 h->elf_link_hash_flags &= ~ELF_LINK_NON_GOT_REF;
2569 return TRUE;
2573 /* We must allocate the symbol in our .dynbss section, which will
2574 become part of the .bss section of the executable. There will be
2575 an entry for this symbol in the .dynsym section. The dynamic
2576 object will contain position independent code, so all references
2577 from the dynamic object to this symbol will go through the global
2578 offset table. The dynamic linker will use the .dynsym entry to
2579 determine the address it must put in the global offset table, so
2580 both the dynamic object and the regular object will refer to the
2581 same memory location for the variable.
2583 Of course, if the symbol is sufficiently small, we must instead
2584 allocate it in .sbss. FIXME: It would be better to do this if and
2585 only if there were actually SDAREL relocs for that symbol. */
2587 if (h->size <= elf_gp_size (htab->elf.dynobj))
2588 s = htab->dynsbss;
2589 else
2590 s = htab->dynbss;
2591 BFD_ASSERT (s != NULL);
2593 /* We must generate a R_PPC_COPY reloc to tell the dynamic linker to
2594 copy the initial value out of the dynamic object and into the
2595 runtime process image. We need to remember the offset into the
2596 .rela.bss section we are going to use. */
2597 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
2599 asection *srel;
2601 if (h->size <= elf_gp_size (htab->elf.dynobj))
2602 srel = htab->relsbss;
2603 else
2604 srel = htab->relbss;
2605 BFD_ASSERT (srel != NULL);
2606 srel->_raw_size += sizeof (Elf32_External_Rela);
2607 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_COPY;
2610 /* We need to figure out the alignment required for this symbol. I
2611 have no idea how ELF linkers handle this. */
2612 power_of_two = bfd_log2 (h->size);
2613 if (power_of_two > 4)
2614 power_of_two = 4;
2616 /* Apply the required alignment. */
2617 s->_raw_size = BFD_ALIGN (s->_raw_size,
2618 (bfd_size_type) (1 << power_of_two));
2619 if (power_of_two > bfd_get_section_alignment (htab->elf.dynobj, s))
2621 if (! bfd_set_section_alignment (htab->elf.dynobj, s, power_of_two))
2622 return FALSE;
2625 /* Define the symbol as being at this point in the section. */
2626 h->root.u.def.section = s;
2627 h->root.u.def.value = s->_raw_size;
2629 /* Increment the section size to make room for the symbol. */
2630 s->_raw_size += h->size;
2632 return TRUE;
2635 /* This is the condition under which finish_dynamic_symbol will be
2636 called from elflink.h. If elflink.h doesn't call our
2637 finish_dynamic_symbol routine, we'll need to do something about
2638 initializing any .plt and .got entries in relocate_section. */
2639 #define WILL_CALL_FINISH_DYNAMIC_SYMBOL(DYN, SHARED, H) \
2640 ((DYN) \
2641 && ((SHARED) \
2642 || ((H)->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0) \
2643 && ((H)->dynindx != -1 \
2644 || ((H)->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0))
2646 /* Of those relocs that might be copied as dynamic relocs, this macro
2647 selects those that must be copied when linking a shared library,
2648 even when the symbol is local. */
2650 #define MUST_BE_DYN_RELOC(RTYPE) \
2651 ((RTYPE) != R_PPC_REL24 \
2652 && (RTYPE) != R_PPC_REL14 \
2653 && (RTYPE) != R_PPC_REL14_BRTAKEN \
2654 && (RTYPE) != R_PPC_REL14_BRNTAKEN \
2655 && (RTYPE) != R_PPC_REL32)
2657 /* Allocate space in associated reloc sections for dynamic relocs. */
2659 static bfd_boolean
2660 allocate_dynrelocs (h, inf)
2661 struct elf_link_hash_entry *h;
2662 PTR inf;
2664 struct bfd_link_info *info = (struct bfd_link_info *) inf;
2665 struct ppc_elf_link_hash_entry *eh;
2666 struct ppc_elf_link_hash_table *htab;
2667 struct ppc_elf_dyn_relocs *p;
2669 if (h->root.type == bfd_link_hash_indirect)
2670 return TRUE;
2672 if (h->root.type == bfd_link_hash_warning)
2673 /* When warning symbols are created, they **replace** the "real"
2674 entry in the hash table, thus we never get to see the real
2675 symbol in a hash traversal. So look at it now. */
2676 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2678 htab = ppc_elf_hash_table (info);
2679 if (htab->elf.dynamic_sections_created
2680 && h->plt.refcount > 0)
2682 /* Make sure this symbol is output as a dynamic symbol. */
2683 if (h->dynindx == -1
2684 && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
2686 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
2687 return FALSE;
2690 if (info->shared
2691 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h))
2693 asection *s = htab->plt;
2695 /* If this is the first .plt entry, make room for the special
2696 first entry. */
2697 if (s->_raw_size == 0)
2698 s->_raw_size += PLT_INITIAL_ENTRY_SIZE;
2700 /* The PowerPC PLT is actually composed of two parts, the
2701 first part is 2 words (for a load and a jump), and then
2702 there is a remaining word available at the end. */
2703 h->plt.offset = (PLT_INITIAL_ENTRY_SIZE
2704 + (PLT_SLOT_SIZE
2705 * ((s->_raw_size - PLT_INITIAL_ENTRY_SIZE)
2706 / PLT_ENTRY_SIZE)));
2708 /* If this symbol is not defined in a regular file, and we
2709 are not generating a shared library, then set the symbol
2710 to this location in the .plt. This is required to make
2711 function pointers compare as equal between the normal
2712 executable and the shared library. */
2713 if (! info->shared
2714 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
2716 h->root.u.def.section = s;
2717 h->root.u.def.value = h->plt.offset;
2720 /* Make room for this entry. After the 8192nd entry, room
2721 for two entries is allocated. */
2722 s->_raw_size += PLT_ENTRY_SIZE;
2723 if ((s->_raw_size - PLT_INITIAL_ENTRY_SIZE) / PLT_ENTRY_SIZE
2724 >= PLT_NUM_SINGLE_ENTRIES)
2725 s->_raw_size += PLT_ENTRY_SIZE;
2727 /* We also need to make an entry in the .rela.plt section. */
2728 htab->relplt->_raw_size += sizeof (Elf32_External_Rela);
2730 else
2732 h->plt.offset = (bfd_vma) -1;
2733 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
2736 else
2738 h->plt.offset = (bfd_vma) -1;
2739 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
2742 eh = (struct ppc_elf_link_hash_entry *) h;
2743 if (eh->elf.got.refcount > 0)
2745 /* Make sure this symbol is output as a dynamic symbol. */
2746 if (eh->elf.dynindx == -1
2747 && (eh->elf.elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
2749 if (!bfd_elf32_link_record_dynamic_symbol (info, &eh->elf))
2750 return FALSE;
2753 if (eh->tls_mask == (TLS_TLS | TLS_LD)
2754 && !(eh->elf.elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC))
2755 /* If just an LD reloc, we'll just use htab->tlsld_got.offset. */
2756 eh->elf.got.offset = (bfd_vma) -1;
2757 else
2759 bfd_boolean dyn;
2760 eh->elf.got.offset = htab->got->_raw_size;
2761 if ((eh->tls_mask & TLS_TLS) != 0)
2763 if ((eh->tls_mask & TLS_LD) != 0)
2764 htab->got->_raw_size += 8;
2765 if ((eh->tls_mask & TLS_GD) != 0)
2766 htab->got->_raw_size += 8;
2767 if ((eh->tls_mask & (TLS_TPREL | TLS_TPRELGD)) != 0)
2768 htab->got->_raw_size += 4;
2769 if ((eh->tls_mask & TLS_DTPREL) != 0)
2770 htab->got->_raw_size += 4;
2772 else
2773 htab->got->_raw_size += 4;
2774 dyn = htab->elf.dynamic_sections_created;
2775 if (info->shared
2776 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, &eh->elf))
2778 /* All the entries we allocated need relocs. */
2779 htab->relgot->_raw_size
2780 += ((htab->got->_raw_size - eh->elf.got.offset) / 4
2781 * sizeof (Elf32_External_Rela));
2782 /* Except LD only needs one. */
2783 if ((eh->tls_mask & TLS_LD) != 0)
2784 htab->relgot->_raw_size -= sizeof (Elf32_External_Rela);
2788 else
2789 eh->elf.got.offset = (bfd_vma) -1;
2791 if (eh->dyn_relocs == NULL)
2792 return TRUE;
2794 /* In the shared -Bsymbolic case, discard space allocated for
2795 dynamic pc-relative relocs against symbols which turn out to be
2796 defined in regular objects. For the normal shared case, discard
2797 space for relocs that have become local due to symbol visibility
2798 changes. */
2799 if (info->shared)
2801 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0
2802 && ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0
2803 || info->symbolic))
2805 struct ppc_elf_dyn_relocs **pp;
2807 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
2809 p->count -= p->pc_count;
2810 p->pc_count = 0;
2811 if (p->count == 0)
2812 *pp = p->next;
2813 else
2814 pp = &p->next;
2818 else if (ELIMINATE_COPY_RELOCS)
2820 /* For the non-shared case, discard space for relocs against
2821 symbols which turn out to need copy relocs or are not
2822 dynamic. */
2824 if ((h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0
2825 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
2826 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
2828 /* Make sure this symbol is output as a dynamic symbol.
2829 Undefined weak syms won't yet be marked as dynamic. */
2830 if (h->dynindx == -1
2831 && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
2833 if (! bfd_elf64_link_record_dynamic_symbol (info, h))
2834 return FALSE;
2837 /* If that succeeded, we know we'll be keeping all the
2838 relocs. */
2839 if (h->dynindx != -1)
2840 goto keep;
2843 eh->dyn_relocs = NULL;
2845 keep: ;
2848 /* Finally, allocate space. */
2849 for (p = eh->dyn_relocs; p != NULL; p = p->next)
2851 asection *sreloc = elf_section_data (p->sec)->sreloc;
2852 sreloc->_raw_size += p->count * sizeof (Elf32_External_Rela);
2855 return TRUE;
2858 /* Find any dynamic relocs that apply to read-only sections. */
2860 static bfd_boolean
2861 readonly_dynrelocs (h, info)
2862 struct elf_link_hash_entry *h;
2863 PTR info;
2865 struct ppc_elf_dyn_relocs *p;
2867 if (h->root.type == bfd_link_hash_indirect)
2868 return TRUE;
2870 if (h->root.type == bfd_link_hash_warning)
2871 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2873 for (p = ppc_elf_hash_entry (h)->dyn_relocs; p != NULL; p = p->next)
2875 asection *s = p->sec->output_section;
2877 if (s != NULL
2878 && ((s->flags & (SEC_READONLY | SEC_ALLOC))
2879 == (SEC_READONLY | SEC_ALLOC)))
2881 ((struct bfd_link_info *) info)->flags |= DF_TEXTREL;
2883 /* Not an error, just cut short the traversal. */
2884 return FALSE;
2887 return TRUE;
2890 /* Set the sizes of the dynamic sections. */
2892 static bfd_boolean
2893 ppc_elf_size_dynamic_sections (output_bfd, info)
2894 bfd *output_bfd ATTRIBUTE_UNUSED;
2895 struct bfd_link_info *info;
2897 struct ppc_elf_link_hash_table *htab;
2898 asection *s;
2899 bfd_boolean relocs;
2900 bfd *ibfd;
2902 #ifdef DEBUG
2903 fprintf (stderr, "ppc_elf_size_dynamic_sections called\n");
2904 #endif
2906 htab = ppc_elf_hash_table (info);
2907 BFD_ASSERT (htab->elf.dynobj != NULL);
2909 if (elf_hash_table (info)->dynamic_sections_created)
2911 /* Set the contents of the .interp section to the interpreter. */
2912 if (! info->shared)
2914 s = bfd_get_section_by_name (htab->elf.dynobj, ".interp");
2915 BFD_ASSERT (s != NULL);
2916 s->_raw_size = sizeof ELF_DYNAMIC_INTERPRETER;
2917 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
2921 if (htab->tlsld_got.refcount > 0)
2923 htab->tlsld_got.offset = htab->got->_raw_size;
2924 htab->got->_raw_size += 8;
2925 if (info->shared)
2926 htab->relgot->_raw_size += sizeof (Elf32_External_Rela);
2928 else
2929 htab->tlsld_got.offset = (bfd_vma) -1;
2931 /* Set up .got offsets for local syms, and space for local dynamic
2932 relocs. */
2933 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
2935 bfd_signed_vma *local_got;
2936 bfd_signed_vma *end_local_got;
2937 char *lgot_masks;
2938 bfd_size_type locsymcount;
2939 Elf_Internal_Shdr *symtab_hdr;
2940 asection *srel;
2942 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
2943 continue;
2945 for (s = ibfd->sections; s != NULL; s = s->next)
2947 struct ppc_elf_dyn_relocs *p;
2949 for (p = ((struct ppc_elf_dyn_relocs *)
2950 elf_section_data (s)->local_dynrel);
2951 p != NULL;
2952 p = p->next)
2954 if (!bfd_is_abs_section (p->sec)
2955 && bfd_is_abs_section (p->sec->output_section))
2957 /* Input section has been discarded, either because
2958 it is a copy of a linkonce section or due to
2959 linker script /DISCARD/, so we'll be discarding
2960 the relocs too. */
2962 else if (p->count != 0)
2964 elf_section_data (p->sec)->sreloc->_raw_size
2965 += p->count * sizeof (Elf32_External_Rela);
2966 if ((p->sec->output_section->flags
2967 & (SEC_READONLY | SEC_ALLOC))
2968 == (SEC_READONLY | SEC_ALLOC))
2969 info->flags |= DF_TEXTREL;
2974 local_got = elf_local_got_refcounts (ibfd);
2975 if (!local_got)
2976 continue;
2978 symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
2979 locsymcount = symtab_hdr->sh_info;
2980 end_local_got = local_got + locsymcount;
2981 lgot_masks = (char *) end_local_got;
2982 s = htab->got;
2983 srel = htab->relgot;
2984 for (; local_got < end_local_got; ++local_got, ++lgot_masks)
2985 if (*local_got > 0)
2987 if (*lgot_masks == (TLS_TLS | TLS_LD))
2989 /* If just an LD reloc, we'll just use
2990 htab->tlsld_got.offset. */
2991 if (htab->tlsld_got.offset == (bfd_vma) -1)
2993 htab->tlsld_got.offset = s->_raw_size;
2994 s->_raw_size += 8;
2995 if (info->shared)
2996 srel->_raw_size += sizeof (Elf32_External_Rela);
2998 *local_got = (bfd_vma) -1;
3000 else
3002 *local_got = s->_raw_size;
3003 if ((*lgot_masks & TLS_TLS) != 0)
3005 if ((*lgot_masks & TLS_GD) != 0)
3006 s->_raw_size += 8;
3007 if ((*lgot_masks & (TLS_TPREL | TLS_TPRELGD)) != 0)
3008 s->_raw_size += 4;
3009 if ((*lgot_masks & TLS_DTPREL) != 0)
3010 s->_raw_size += 4;
3012 else
3013 s->_raw_size += 4;
3014 if (info->shared)
3015 srel->_raw_size += ((s->_raw_size - *local_got) / 4
3016 * sizeof (Elf32_External_Rela));
3019 else
3020 *local_got = (bfd_vma) -1;
3023 /* Allocate space for global sym dynamic relocs. */
3024 elf_link_hash_traverse (elf_hash_table (info), allocate_dynrelocs, info);
3026 /* We've now determined the sizes of the various dynamic sections.
3027 Allocate memory for them. */
3028 relocs = FALSE;
3029 for (s = htab->elf.dynobj->sections; s != NULL; s = s->next)
3031 if ((s->flags & SEC_LINKER_CREATED) == 0)
3032 continue;
3034 if (s == htab->plt
3035 || s == htab->got
3036 || (htab->sdata != NULL && s == htab->sdata->section)
3037 || (htab->sdata2 != NULL && s == htab->sdata2->section))
3039 /* Strip this section if we don't need it; see the
3040 comment below. */
3042 else if (strncmp (bfd_get_section_name (dynobj, s), ".rela", 5) == 0)
3044 if (s->_raw_size == 0)
3046 /* If we don't need this section, strip it from the
3047 output file. This is mostly to handle .rela.bss and
3048 .rela.plt. We must create both sections in
3049 create_dynamic_sections, because they must be created
3050 before the linker maps input sections to output
3051 sections. The linker does that before
3052 adjust_dynamic_symbol is called, and it is that
3053 function which decides whether anything needs to go
3054 into these sections. */
3056 else
3058 /* Remember whether there are any relocation sections. */
3059 relocs = TRUE;
3061 /* We use the reloc_count field as a counter if we need
3062 to copy relocs into the output file. */
3063 s->reloc_count = 0;
3066 else
3068 /* It's not one of our sections, so don't allocate space. */
3069 continue;
3072 if (s->_raw_size == 0)
3074 _bfd_strip_section_from_output (info, s);
3075 continue;
3078 /* Allocate memory for the section contents. */
3079 s->contents = (bfd_byte *) bfd_zalloc (htab->elf.dynobj, s->_raw_size);
3080 if (s->contents == NULL)
3081 return FALSE;
3084 if (htab->elf.dynamic_sections_created)
3086 /* Add some entries to the .dynamic section. We fill in the
3087 values later, in ppc_elf_finish_dynamic_sections, but we
3088 must add the entries now so that we get the correct size for
3089 the .dynamic section. The DT_DEBUG entry is filled in by the
3090 dynamic linker and used by the debugger. */
3091 #define add_dynamic_entry(TAG, VAL) \
3092 bfd_elf32_add_dynamic_entry (info, (bfd_vma) (TAG), (bfd_vma) (VAL))
3094 if (!info->shared)
3096 if (!add_dynamic_entry (DT_DEBUG, 0))
3097 return FALSE;
3100 if (htab->plt != NULL && htab->plt->_raw_size != 0)
3102 if (!add_dynamic_entry (DT_PLTGOT, 0)
3103 || !add_dynamic_entry (DT_PLTRELSZ, 0)
3104 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
3105 || !add_dynamic_entry (DT_JMPREL, 0))
3106 return FALSE;
3109 if (relocs)
3111 if (!add_dynamic_entry (DT_RELA, 0)
3112 || !add_dynamic_entry (DT_RELASZ, 0)
3113 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf32_External_Rela)))
3114 return FALSE;
3117 /* If any dynamic relocs apply to a read-only section, then we
3118 need a DT_TEXTREL entry. */
3119 if ((info->flags & DF_TEXTREL) == 0)
3120 elf_link_hash_traverse (elf_hash_table (info), readonly_dynrelocs,
3121 (PTR) info);
3123 if ((info->flags & DF_TEXTREL) != 0)
3125 if (!add_dynamic_entry (DT_TEXTREL, 0))
3126 return FALSE;
3129 #undef add_dynamic_entry
3131 return TRUE;
3134 static bfd_boolean
3135 update_local_sym_info (abfd, symtab_hdr, r_symndx, tls_type)
3136 bfd *abfd;
3137 Elf_Internal_Shdr *symtab_hdr;
3138 unsigned long r_symndx;
3139 int tls_type;
3141 bfd_signed_vma *local_got_refcounts = elf_local_got_refcounts (abfd);
3142 char *local_got_tls_masks;
3144 if (local_got_refcounts == NULL)
3146 bfd_size_type size = symtab_hdr->sh_info;
3148 size *= sizeof (*local_got_refcounts) + sizeof (*local_got_tls_masks);
3149 local_got_refcounts = (bfd_signed_vma *) bfd_zalloc (abfd, size);
3150 if (local_got_refcounts == NULL)
3151 return FALSE;
3152 elf_local_got_refcounts (abfd) = local_got_refcounts;
3155 local_got_refcounts[r_symndx] += 1;
3156 local_got_tls_masks = (char *) (local_got_refcounts + symtab_hdr->sh_info);
3157 local_got_tls_masks[r_symndx] |= tls_type;
3158 return TRUE;
3161 static void
3162 bad_shared_reloc (abfd, r_type)
3163 bfd *abfd;
3164 enum elf_ppc_reloc_type r_type;
3166 (*_bfd_error_handler)
3167 (_("%s: relocation %s cannot be used when making a shared object"),
3168 bfd_archive_filename (abfd),
3169 ppc_elf_howto_table[(int) r_type]->name);
3170 bfd_set_error (bfd_error_bad_value);
3173 /* Look through the relocs for a section during the first phase, and
3174 allocate space in the global offset table or procedure linkage
3175 table. */
3177 static bfd_boolean
3178 ppc_elf_check_relocs (abfd, info, sec, relocs)
3179 bfd *abfd;
3180 struct bfd_link_info *info;
3181 asection *sec;
3182 const Elf_Internal_Rela *relocs;
3184 struct ppc_elf_link_hash_table *htab;
3185 Elf_Internal_Shdr *symtab_hdr;
3186 struct elf_link_hash_entry **sym_hashes;
3187 const Elf_Internal_Rela *rel;
3188 const Elf_Internal_Rela *rel_end;
3189 asection *sreloc;
3191 if (info->relocateable)
3192 return TRUE;
3194 #ifdef DEBUG
3195 fprintf (stderr, "ppc_elf_check_relocs called for section %s in %s\n",
3196 bfd_get_section_name (abfd, sec),
3197 bfd_archive_filename (abfd));
3198 #endif
3200 /* Create the linker generated sections all the time so that the
3201 special symbols are created. */
3203 htab = ppc_elf_hash_table (info);
3204 if (htab->sdata == NULL)
3206 htab->sdata = elf_linker_section (abfd, LINKER_SECTION_SDATA);
3207 if (htab->sdata == NULL)
3208 htab->sdata = ppc_elf_create_linker_section (abfd, info,
3209 LINKER_SECTION_SDATA);
3210 if (htab->sdata == NULL)
3211 return FALSE;
3214 if (htab->sdata2 == NULL)
3216 htab->sdata2 = elf_linker_section (abfd, LINKER_SECTION_SDATA2);
3217 if (htab->sdata2 == NULL)
3218 htab->sdata2 = ppc_elf_create_linker_section (abfd, info,
3219 LINKER_SECTION_SDATA2);
3220 if (htab->sdata2 == NULL)
3221 return FALSE;
3224 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
3225 sym_hashes = elf_sym_hashes (abfd);
3226 sreloc = NULL;
3228 rel_end = relocs + sec->reloc_count;
3229 for (rel = relocs; rel < rel_end; rel++)
3231 unsigned long r_symndx;
3232 enum elf_ppc_reloc_type r_type;
3233 struct elf_link_hash_entry *h;
3234 int tls_type = 0;
3236 r_symndx = ELF32_R_SYM (rel->r_info);
3237 if (r_symndx < symtab_hdr->sh_info)
3238 h = NULL;
3239 else
3240 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
3242 /* If a relocation refers to _GLOBAL_OFFSET_TABLE_, create the .got.
3243 This shows up in particular in an R_PPC_ADDR32 in the eabi
3244 startup code. */
3245 if (h && strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
3247 if (htab->got == NULL)
3249 if (htab->elf.dynobj == NULL)
3250 htab->elf.dynobj = abfd;
3251 if (!ppc_elf_create_got (htab->elf.dynobj, info))
3252 return FALSE;
3256 r_type = (enum elf_ppc_reloc_type) ELF32_R_TYPE (rel->r_info);
3257 switch (r_type)
3259 case R_PPC_GOT_TLSLD16:
3260 case R_PPC_GOT_TLSLD16_LO:
3261 case R_PPC_GOT_TLSLD16_HI:
3262 case R_PPC_GOT_TLSLD16_HA:
3263 htab->tlsld_got.refcount += 1;
3264 tls_type = TLS_TLS | TLS_LD;
3265 goto dogottls;
3267 case R_PPC_GOT_TLSGD16:
3268 case R_PPC_GOT_TLSGD16_LO:
3269 case R_PPC_GOT_TLSGD16_HI:
3270 case R_PPC_GOT_TLSGD16_HA:
3271 tls_type = TLS_TLS | TLS_GD;
3272 goto dogottls;
3274 case R_PPC_GOT_TPREL16:
3275 case R_PPC_GOT_TPREL16_LO:
3276 case R_PPC_GOT_TPREL16_HI:
3277 case R_PPC_GOT_TPREL16_HA:
3278 if (info->shared)
3279 info->flags |= DF_STATIC_TLS;
3280 tls_type = TLS_TLS | TLS_TPREL;
3281 goto dogottls;
3283 case R_PPC_GOT_DTPREL16:
3284 case R_PPC_GOT_DTPREL16_LO:
3285 case R_PPC_GOT_DTPREL16_HI:
3286 case R_PPC_GOT_DTPREL16_HA:
3287 tls_type = TLS_TLS | TLS_DTPREL;
3288 dogottls:
3289 sec->has_tls_reloc = 1;
3290 /* Fall thru */
3292 /* GOT16 relocations */
3293 case R_PPC_GOT16:
3294 case R_PPC_GOT16_LO:
3295 case R_PPC_GOT16_HI:
3296 case R_PPC_GOT16_HA:
3297 /* This symbol requires a global offset table entry. */
3298 if (htab->got == NULL)
3300 if (htab->elf.dynobj == NULL)
3301 htab->elf.dynobj = abfd;
3302 if (!ppc_elf_create_got (htab->elf.dynobj, info))
3303 return FALSE;
3305 if (h != NULL)
3307 h->got.refcount += 1;
3308 ppc_elf_hash_entry (h)->tls_mask |= tls_type;
3310 else
3311 /* This is a global offset table entry for a local symbol. */
3312 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx, tls_type))
3313 return FALSE;
3314 break;
3316 /* Indirect .sdata relocation. */
3317 case R_PPC_EMB_SDAI16:
3318 if (info->shared)
3320 bad_shared_reloc (abfd, r_type);
3321 return FALSE;
3323 if (!bfd_elf32_create_pointer_linker_section (abfd, info,
3324 htab->sdata, h, rel))
3325 return FALSE;
3326 break;
3328 /* Indirect .sdata2 relocation. */
3329 case R_PPC_EMB_SDA2I16:
3330 if (info->shared)
3332 bad_shared_reloc (abfd, r_type);
3333 return FALSE;
3335 if (!bfd_elf32_create_pointer_linker_section (abfd, info,
3336 htab->sdata2, h, rel))
3337 return FALSE;
3338 break;
3340 case R_PPC_SDAREL16:
3341 case R_PPC_EMB_SDA2REL:
3342 case R_PPC_EMB_SDA21:
3343 case R_PPC_EMB_RELSDA:
3344 case R_PPC_EMB_NADDR32:
3345 case R_PPC_EMB_NADDR16:
3346 case R_PPC_EMB_NADDR16_LO:
3347 case R_PPC_EMB_NADDR16_HI:
3348 case R_PPC_EMB_NADDR16_HA:
3349 if (info->shared)
3351 bad_shared_reloc (abfd, r_type);
3352 return FALSE;
3354 break;
3356 case R_PPC_PLT32:
3357 case R_PPC_PLTREL24:
3358 case R_PPC_PLTREL32:
3359 case R_PPC_PLT16_LO:
3360 case R_PPC_PLT16_HI:
3361 case R_PPC_PLT16_HA:
3362 #ifdef DEBUG
3363 fprintf (stderr, "Reloc requires a PLT entry\n");
3364 #endif
3365 /* This symbol requires a procedure linkage table entry. We
3366 actually build the entry in finish_dynamic_symbol,
3367 because this might be a case of linking PIC code without
3368 linking in any dynamic objects, in which case we don't
3369 need to generate a procedure linkage table after all. */
3371 if (h == NULL)
3373 /* It does not make sense to have a procedure linkage
3374 table entry for a local symbol. */
3375 bfd_set_error (bfd_error_bad_value);
3376 return FALSE;
3379 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
3380 h->plt.refcount++;
3381 break;
3383 /* The following relocations don't need to propagate the
3384 relocation if linking a shared object since they are
3385 section relative. */
3386 case R_PPC_SECTOFF:
3387 case R_PPC_SECTOFF_LO:
3388 case R_PPC_SECTOFF_HI:
3389 case R_PPC_SECTOFF_HA:
3390 case R_PPC_DTPREL16:
3391 case R_PPC_DTPREL16_LO:
3392 case R_PPC_DTPREL16_HI:
3393 case R_PPC_DTPREL16_HA:
3394 case R_PPC_TOC16:
3395 break;
3397 /* This are just markers. */
3398 case R_PPC_TLS:
3399 case R_PPC_EMB_MRKREF:
3400 case R_PPC_NONE:
3401 case R_PPC_max:
3402 break;
3404 /* These should only appear in dynamic objects. */
3405 case R_PPC_COPY:
3406 case R_PPC_GLOB_DAT:
3407 case R_PPC_JMP_SLOT:
3408 case R_PPC_RELATIVE:
3409 break;
3411 /* These aren't handled yet. We'll report an error later. */
3412 case R_PPC_ADDR30:
3413 case R_PPC_EMB_RELSEC16:
3414 case R_PPC_EMB_RELST_LO:
3415 case R_PPC_EMB_RELST_HI:
3416 case R_PPC_EMB_RELST_HA:
3417 case R_PPC_EMB_BIT_FLD:
3418 break;
3420 /* This refers only to functions defined in the shared library. */
3421 case R_PPC_LOCAL24PC:
3422 break;
3424 /* This relocation describes the C++ object vtable hierarchy.
3425 Reconstruct it for later use during GC. */
3426 case R_PPC_GNU_VTINHERIT:
3427 if (!_bfd_elf32_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
3428 return FALSE;
3429 break;
3431 /* This relocation describes which C++ vtable entries are actually
3432 used. Record for later use during GC. */
3433 case R_PPC_GNU_VTENTRY:
3434 if (!_bfd_elf32_gc_record_vtentry (abfd, sec, h, rel->r_addend))
3435 return FALSE;
3436 break;
3438 /* We shouldn't really be seeing these. */
3439 case R_PPC_TPREL32:
3440 if (info->shared)
3441 info->flags |= DF_STATIC_TLS;
3442 goto dodyn;
3444 /* Nor these. */
3445 case R_PPC_DTPMOD32:
3446 case R_PPC_DTPREL32:
3447 goto dodyn;
3449 case R_PPC_TPREL16:
3450 case R_PPC_TPREL16_LO:
3451 case R_PPC_TPREL16_HI:
3452 case R_PPC_TPREL16_HA:
3453 if (info->shared)
3454 info->flags |= DF_STATIC_TLS;
3455 goto dodyn;
3457 /* When creating a shared object, we must copy these
3458 relocs into the output file. We create a reloc
3459 section in dynobj and make room for the reloc. */
3460 case R_PPC_REL24:
3461 case R_PPC_REL14:
3462 case R_PPC_REL14_BRTAKEN:
3463 case R_PPC_REL14_BRNTAKEN:
3464 case R_PPC_REL32:
3465 if (h == NULL
3466 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
3467 break;
3468 /* fall through */
3470 case R_PPC_ADDR32:
3471 case R_PPC_ADDR24:
3472 case R_PPC_ADDR16:
3473 case R_PPC_ADDR16_LO:
3474 case R_PPC_ADDR16_HI:
3475 case R_PPC_ADDR16_HA:
3476 case R_PPC_ADDR14:
3477 case R_PPC_ADDR14_BRTAKEN:
3478 case R_PPC_ADDR14_BRNTAKEN:
3479 case R_PPC_UADDR32:
3480 case R_PPC_UADDR16:
3481 if (h != NULL && !info->shared)
3483 /* We may need a plt entry if the symbol turns out to be
3484 a function defined in a dynamic object. */
3485 h->plt.refcount++;
3487 /* We may need a copy reloc too. */
3488 h->elf_link_hash_flags |= ELF_LINK_NON_GOT_REF;
3491 dodyn:
3492 /* If we are creating a shared library, and this is a reloc
3493 against a global symbol, or a non PC relative reloc
3494 against a local symbol, then we need to copy the reloc
3495 into the shared library. However, if we are linking with
3496 -Bsymbolic, we do not need to copy a reloc against a
3497 global symbol which is defined in an object we are
3498 including in the link (i.e., DEF_REGULAR is set). At
3499 this point we have not seen all the input files, so it is
3500 possible that DEF_REGULAR is not set now but will be set
3501 later (it is never cleared). In case of a weak definition,
3502 DEF_REGULAR may be cleared later by a strong definition in
3503 a shared library. We account for that possibility below by
3504 storing information in the dyn_relocs field of the hash
3505 table entry. A similar situation occurs when creating
3506 shared libraries and symbol visibility changes render the
3507 symbol local.
3509 If on the other hand, we are creating an executable, we
3510 may need to keep relocations for symbols satisfied by a
3511 dynamic library if we manage to avoid copy relocs for the
3512 symbol. */
3513 if ((info->shared
3514 && (MUST_BE_DYN_RELOC (r_type)
3515 || (h != NULL
3516 && (! info->symbolic
3517 || h->root.type == bfd_link_hash_defweak
3518 || (h->elf_link_hash_flags
3519 & ELF_LINK_HASH_DEF_REGULAR) == 0))))
3520 || (ELIMINATE_COPY_RELOCS
3521 && !info->shared
3522 && (sec->flags & SEC_ALLOC) != 0
3523 && h != NULL
3524 && (h->root.type == bfd_link_hash_defweak
3525 || (h->elf_link_hash_flags
3526 & ELF_LINK_HASH_DEF_REGULAR) == 0)))
3528 struct ppc_elf_dyn_relocs *p;
3529 struct ppc_elf_dyn_relocs **head;
3531 #ifdef DEBUG
3532 fprintf (stderr, "ppc_elf_check_relocs need to create relocation for %s\n",
3533 (h && h->root.root.string
3534 ? h->root.root.string : "<unknown>"));
3535 #endif
3536 if (sreloc == NULL)
3538 const char *name;
3540 name = (bfd_elf_string_from_elf_section
3541 (abfd,
3542 elf_elfheader (abfd)->e_shstrndx,
3543 elf_section_data (sec)->rel_hdr.sh_name));
3544 if (name == NULL)
3545 return FALSE;
3547 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
3548 && strcmp (bfd_get_section_name (abfd, sec),
3549 name + 5) == 0);
3551 sreloc = bfd_get_section_by_name (htab->elf.dynobj, name);
3552 if (sreloc == NULL)
3554 flagword flags;
3556 sreloc = bfd_make_section (htab->elf.dynobj, name);
3557 flags = (SEC_HAS_CONTENTS | SEC_READONLY
3558 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3559 if ((sec->flags & SEC_ALLOC) != 0)
3560 flags |= SEC_ALLOC | SEC_LOAD;
3561 if (sreloc == NULL
3562 || ! bfd_set_section_flags (htab->elf.dynobj,
3563 sreloc, flags)
3564 || ! bfd_set_section_alignment (htab->elf.dynobj,
3565 sreloc, 2))
3566 return FALSE;
3568 elf_section_data (sec)->sreloc = sreloc;
3571 /* If this is a global symbol, we count the number of
3572 relocations we need for this symbol. */
3573 if (h != NULL)
3575 head = &ppc_elf_hash_entry (h)->dyn_relocs;
3577 else
3579 /* Track dynamic relocs needed for local syms too.
3580 We really need local syms available to do this
3581 easily. Oh well. */
3583 asection *s;
3584 s = bfd_section_from_r_symndx (abfd, &htab->sym_sec,
3585 sec, r_symndx);
3586 if (s == NULL)
3587 return FALSE;
3589 head = ((struct ppc_elf_dyn_relocs **)
3590 &elf_section_data (s)->local_dynrel);
3593 p = *head;
3594 if (p == NULL || p->sec != sec)
3596 p = ((struct ppc_elf_dyn_relocs *)
3597 bfd_alloc (htab->elf.dynobj, sizeof *p));
3598 if (p == NULL)
3599 return FALSE;
3600 p->next = *head;
3601 *head = p;
3602 p->sec = sec;
3603 p->count = 0;
3604 p->pc_count = 0;
3607 p->count += 1;
3608 if (!MUST_BE_DYN_RELOC (r_type))
3609 p->pc_count += 1;
3612 break;
3616 return TRUE;
3619 /* Return the section that should be marked against GC for a given
3620 relocation. */
3622 static asection *
3623 ppc_elf_gc_mark_hook (sec, info, rel, h, sym)
3624 asection *sec;
3625 struct bfd_link_info *info ATTRIBUTE_UNUSED;
3626 Elf_Internal_Rela *rel;
3627 struct elf_link_hash_entry *h;
3628 Elf_Internal_Sym *sym;
3630 if (h != NULL)
3632 switch (ELF32_R_TYPE (rel->r_info))
3634 case R_PPC_GNU_VTINHERIT:
3635 case R_PPC_GNU_VTENTRY:
3636 break;
3638 default:
3639 switch (h->root.type)
3641 case bfd_link_hash_defined:
3642 case bfd_link_hash_defweak:
3643 return h->root.u.def.section;
3645 case bfd_link_hash_common:
3646 return h->root.u.c.p->section;
3648 default:
3649 break;
3653 else
3654 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
3656 return NULL;
3659 /* Update the got, plt and dynamic reloc reference counts for the
3660 section being removed. */
3662 static bfd_boolean
3663 ppc_elf_gc_sweep_hook (abfd, info, sec, relocs)
3664 bfd *abfd;
3665 struct bfd_link_info *info;
3666 asection *sec;
3667 const Elf_Internal_Rela *relocs;
3669 struct ppc_elf_link_hash_table *htab;
3670 Elf_Internal_Shdr *symtab_hdr;
3671 struct elf_link_hash_entry **sym_hashes;
3672 bfd_signed_vma *local_got_refcounts;
3673 const Elf_Internal_Rela *rel, *relend;
3675 elf_section_data (sec)->local_dynrel = NULL;
3677 htab = ppc_elf_hash_table (info);
3678 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
3679 sym_hashes = elf_sym_hashes (abfd);
3680 local_got_refcounts = elf_local_got_refcounts (abfd);
3682 relend = relocs + sec->reloc_count;
3683 for (rel = relocs; rel < relend; rel++)
3685 unsigned long r_symndx;
3686 enum elf_ppc_reloc_type r_type;
3687 struct elf_link_hash_entry *h = NULL;
3689 r_symndx = ELF32_R_SYM (rel->r_info);
3690 if (r_symndx >= symtab_hdr->sh_info)
3692 struct ppc_elf_dyn_relocs **pp, *p;
3693 struct ppc_elf_link_hash_entry *eh;
3695 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
3696 eh = (struct ppc_elf_link_hash_entry *) h;
3698 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
3699 if (p->sec == sec)
3701 /* Everything must go for SEC. */
3702 *pp = p->next;
3703 break;
3707 r_type = (enum elf_ppc_reloc_type) ELF32_R_TYPE (rel->r_info);
3708 switch (r_type)
3710 case R_PPC_GOT_TLSLD16:
3711 case R_PPC_GOT_TLSLD16_LO:
3712 case R_PPC_GOT_TLSLD16_HI:
3713 case R_PPC_GOT_TLSLD16_HA:
3714 htab->tlsld_got.refcount -= 1;
3715 /* Fall thru */
3717 case R_PPC_GOT_TLSGD16:
3718 case R_PPC_GOT_TLSGD16_LO:
3719 case R_PPC_GOT_TLSGD16_HI:
3720 case R_PPC_GOT_TLSGD16_HA:
3721 case R_PPC_GOT_TPREL16:
3722 case R_PPC_GOT_TPREL16_LO:
3723 case R_PPC_GOT_TPREL16_HI:
3724 case R_PPC_GOT_TPREL16_HA:
3725 case R_PPC_GOT_DTPREL16:
3726 case R_PPC_GOT_DTPREL16_LO:
3727 case R_PPC_GOT_DTPREL16_HI:
3728 case R_PPC_GOT_DTPREL16_HA:
3729 case R_PPC_GOT16:
3730 case R_PPC_GOT16_LO:
3731 case R_PPC_GOT16_HI:
3732 case R_PPC_GOT16_HA:
3733 if (h != NULL)
3735 if (h->got.refcount > 0)
3736 h->got.refcount--;
3738 else if (local_got_refcounts != NULL)
3740 if (local_got_refcounts[r_symndx] > 0)
3741 local_got_refcounts[r_symndx]--;
3743 break;
3745 case R_PPC_REL24:
3746 case R_PPC_REL14:
3747 case R_PPC_REL14_BRTAKEN:
3748 case R_PPC_REL14_BRNTAKEN:
3749 case R_PPC_REL32:
3750 if (h == NULL
3751 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
3752 break;
3753 /* Fall thru */
3755 case R_PPC_ADDR32:
3756 case R_PPC_ADDR24:
3757 case R_PPC_ADDR16:
3758 case R_PPC_ADDR16_LO:
3759 case R_PPC_ADDR16_HI:
3760 case R_PPC_ADDR16_HA:
3761 case R_PPC_ADDR14:
3762 case R_PPC_ADDR14_BRTAKEN:
3763 case R_PPC_ADDR14_BRNTAKEN:
3764 case R_PPC_UADDR32:
3765 case R_PPC_UADDR16:
3766 case R_PPC_PLT32:
3767 case R_PPC_PLTREL24:
3768 case R_PPC_PLT16_LO:
3769 case R_PPC_PLT16_HI:
3770 case R_PPC_PLT16_HA:
3771 if (h != NULL)
3773 if (h->plt.refcount > 0)
3774 h->plt.refcount--;
3776 break;
3778 default:
3779 break;
3782 return TRUE;
3785 /* Set htab->tls_sec and htab->tls_get_addr. */
3787 bfd_boolean
3788 ppc_elf_tls_setup (obfd, info)
3789 bfd *obfd;
3790 struct bfd_link_info *info;
3792 asection *tls;
3793 struct ppc_elf_link_hash_table *htab;
3795 htab = ppc_elf_hash_table (info);
3796 htab->tls_get_addr = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
3797 FALSE, FALSE, TRUE);
3799 for (tls = obfd->sections; tls != NULL; tls = tls->next)
3800 if ((tls->flags & (SEC_THREAD_LOCAL | SEC_LOAD))
3801 == (SEC_THREAD_LOCAL | SEC_LOAD))
3802 break;
3803 htab->tls_sec = tls;
3805 return tls != NULL;
3808 /* Run through all the TLS relocs looking for optimization
3809 opportunities. */
3811 bfd_boolean
3812 ppc_elf_tls_optimize (obfd, info)
3813 bfd *obfd ATTRIBUTE_UNUSED;
3814 struct bfd_link_info *info;
3816 bfd *ibfd;
3817 asection *sec;
3818 struct ppc_elf_link_hash_table *htab;
3820 if (info->relocateable || info->shared)
3821 return TRUE;
3823 htab = ppc_elf_hash_table (info);
3824 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
3826 Elf_Internal_Sym *locsyms = NULL;
3827 Elf_Internal_Shdr *symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
3829 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
3830 if (sec->has_tls_reloc && !bfd_is_abs_section (sec->output_section))
3832 Elf_Internal_Rela *relstart, *rel, *relend;
3833 int expecting_tls_get_addr;
3835 /* Read the relocations. */
3836 relstart = _bfd_elf32_link_read_relocs (ibfd, sec, (PTR) NULL,
3837 (Elf_Internal_Rela *) NULL,
3838 info->keep_memory);
3839 if (relstart == NULL)
3840 return FALSE;
3842 expecting_tls_get_addr = 0;
3843 relend = relstart + sec->reloc_count;
3844 for (rel = relstart; rel < relend; rel++)
3846 enum elf_ppc_reloc_type r_type;
3847 unsigned long r_symndx;
3848 struct elf_link_hash_entry *h = NULL;
3849 char *tls_mask;
3850 char tls_set, tls_clear;
3851 bfd_boolean is_local;
3853 r_symndx = ELF32_R_SYM (rel->r_info);
3854 if (r_symndx >= symtab_hdr->sh_info)
3856 struct elf_link_hash_entry **sym_hashes;
3858 sym_hashes = elf_sym_hashes (ibfd);
3859 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
3860 while (h->root.type == bfd_link_hash_indirect
3861 || h->root.type == bfd_link_hash_warning)
3862 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3865 is_local = FALSE;
3866 if (h == NULL
3867 || !(h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC))
3868 is_local = TRUE;
3870 r_type = (enum elf_ppc_reloc_type) ELF32_R_TYPE (rel->r_info);
3871 switch (r_type)
3873 case R_PPC_GOT_TLSLD16:
3874 case R_PPC_GOT_TLSLD16_LO:
3875 case R_PPC_GOT_TLSLD16_HI:
3876 case R_PPC_GOT_TLSLD16_HA:
3877 /* These relocs should never be against a symbol
3878 defined in a shared lib. Leave them alone if
3879 that turns out to be the case. */
3880 expecting_tls_get_addr = 0;
3881 htab->tlsld_got.refcount -= 1;
3882 if (!is_local)
3883 continue;
3885 /* LD -> LE */
3886 tls_set = 0;
3887 tls_clear = TLS_LD;
3888 expecting_tls_get_addr = 1;
3889 break;
3891 case R_PPC_GOT_TLSGD16:
3892 case R_PPC_GOT_TLSGD16_LO:
3893 case R_PPC_GOT_TLSGD16_HI:
3894 case R_PPC_GOT_TLSGD16_HA:
3895 if (is_local)
3896 /* GD -> LE */
3897 tls_set = 0;
3898 else
3899 /* GD -> IE */
3900 tls_set = TLS_TLS | TLS_TPRELGD;
3901 tls_clear = TLS_GD;
3902 expecting_tls_get_addr = 1;
3903 break;
3905 case R_PPC_GOT_TPREL16:
3906 case R_PPC_GOT_TPREL16_LO:
3907 case R_PPC_GOT_TPREL16_HI:
3908 case R_PPC_GOT_TPREL16_HA:
3909 expecting_tls_get_addr = 0;
3910 if (is_local)
3912 /* IE -> LE */
3913 tls_set = 0;
3914 tls_clear = TLS_TPREL;
3915 break;
3917 else
3918 continue;
3920 case R_PPC_REL14:
3921 case R_PPC_REL14_BRTAKEN:
3922 case R_PPC_REL14_BRNTAKEN:
3923 case R_PPC_REL24:
3924 if (expecting_tls_get_addr
3925 && h != NULL
3926 && h == htab->tls_get_addr)
3928 if (h->plt.refcount > 0)
3929 h->plt.refcount -= 1;
3931 expecting_tls_get_addr = 0;
3932 continue;
3934 default:
3935 expecting_tls_get_addr = 0;
3936 continue;
3939 if (h != NULL)
3941 if (tls_set == 0)
3943 /* We managed to get rid of a got entry. */
3944 if (h->got.refcount > 0)
3945 h->got.refcount -= 1;
3947 tls_mask = &ppc_elf_hash_entry (h)->tls_mask;
3949 else
3951 Elf_Internal_Sym *sym;
3952 bfd_signed_vma *lgot_refs;
3953 char *lgot_masks;
3955 if (locsyms == NULL)
3957 locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
3958 if (locsyms == NULL)
3959 locsyms = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
3960 symtab_hdr->sh_info,
3961 0, NULL, NULL, NULL);
3962 if (locsyms == NULL)
3964 if (elf_section_data (sec)->relocs != relstart)
3965 free (relstart);
3966 return FALSE;
3969 sym = locsyms + r_symndx;
3970 lgot_refs = elf_local_got_refcounts (ibfd);
3971 if (lgot_refs == NULL)
3972 abort ();
3973 if (tls_set == 0)
3975 /* We managed to get rid of a got entry. */
3976 if (lgot_refs[r_symndx] > 0)
3977 lgot_refs[r_symndx] -= 1;
3979 lgot_masks = (char *) (lgot_refs + symtab_hdr->sh_info);
3980 tls_mask = &lgot_masks[r_symndx];
3983 *tls_mask |= tls_set;
3984 *tls_mask &= ~tls_clear;
3987 if (elf_section_data (sec)->relocs != relstart)
3988 free (relstart);
3991 if (locsyms != NULL
3992 && (symtab_hdr->contents != (unsigned char *) locsyms))
3994 if (!info->keep_memory)
3995 free (locsyms);
3996 else
3997 symtab_hdr->contents = (unsigned char *) locsyms;
4000 return TRUE;
4003 /* Hook called by the linker routine which adds symbols from an object
4004 file. We use it to put .comm items in .sbss, and not .bss. */
4006 static bfd_boolean
4007 ppc_elf_add_symbol_hook (abfd, info, sym, namep, flagsp, secp, valp)
4008 bfd *abfd;
4009 struct bfd_link_info *info;
4010 const Elf_Internal_Sym *sym;
4011 const char **namep ATTRIBUTE_UNUSED;
4012 flagword *flagsp ATTRIBUTE_UNUSED;
4013 asection **secp;
4014 bfd_vma *valp;
4016 if (sym->st_shndx == SHN_COMMON
4017 && !info->relocateable
4018 && sym->st_size <= elf_gp_size (abfd)
4019 && info->hash->creator->flavour == bfd_target_elf_flavour)
4021 /* Common symbols less than or equal to -G nn bytes are automatically
4022 put into .sdata. */
4023 elf_linker_section_t *sdata
4024 = ppc_elf_create_linker_section (abfd, info, LINKER_SECTION_SDATA);
4026 if (!sdata->bss_section)
4028 bfd_size_type amt;
4030 /* We don't go through bfd_make_section, because we don't
4031 want to attach this common section to DYNOBJ. The linker
4032 will move the symbols to the appropriate output section
4033 when it defines common symbols. */
4034 amt = sizeof (asection);
4035 sdata->bss_section = (asection *) bfd_zalloc (abfd, amt);
4036 if (sdata->bss_section == NULL)
4037 return FALSE;
4038 sdata->bss_section->name = sdata->bss_name;
4039 sdata->bss_section->flags = SEC_IS_COMMON;
4040 sdata->bss_section->output_section = sdata->bss_section;
4041 amt = sizeof (asymbol);
4042 sdata->bss_section->symbol = (asymbol *) bfd_zalloc (abfd, amt);
4043 amt = sizeof (asymbol *);
4044 sdata->bss_section->symbol_ptr_ptr =
4045 (asymbol **) bfd_zalloc (abfd, amt);
4046 if (sdata->bss_section->symbol == NULL
4047 || sdata->bss_section->symbol_ptr_ptr == NULL)
4048 return FALSE;
4049 sdata->bss_section->symbol->name = sdata->bss_name;
4050 sdata->bss_section->symbol->flags = BSF_SECTION_SYM;
4051 sdata->bss_section->symbol->section = sdata->bss_section;
4052 *sdata->bss_section->symbol_ptr_ptr = sdata->bss_section->symbol;
4055 *secp = sdata->bss_section;
4056 *valp = sym->st_size;
4059 return TRUE;
4062 /* Finish up dynamic symbol handling. We set the contents of various
4063 dynamic sections here. */
4065 static bfd_boolean
4066 ppc_elf_finish_dynamic_symbol (output_bfd, info, h, sym)
4067 bfd *output_bfd;
4068 struct bfd_link_info *info;
4069 struct elf_link_hash_entry *h;
4070 Elf_Internal_Sym *sym;
4072 struct ppc_elf_link_hash_table *htab;
4074 #ifdef DEBUG
4075 fprintf (stderr, "ppc_elf_finish_dynamic_symbol called for %s",
4076 h->root.root.string);
4077 #endif
4079 htab = ppc_elf_hash_table (info);
4080 BFD_ASSERT (htab->elf.dynobj != NULL);
4082 if (h->plt.offset != (bfd_vma) -1)
4084 Elf_Internal_Rela rela;
4085 bfd_byte *loc;
4086 bfd_vma reloc_index;
4088 #ifdef DEBUG
4089 fprintf (stderr, ", plt_offset = %d", h->plt.offset);
4090 #endif
4092 /* This symbol has an entry in the procedure linkage table. Set
4093 it up. */
4095 BFD_ASSERT (h->dynindx != -1);
4096 BFD_ASSERT (htab->plt != NULL && htab->relplt != NULL);
4098 /* We don't need to fill in the .plt. The ppc dynamic linker
4099 will fill it in. */
4101 /* Fill in the entry in the .rela.plt section. */
4102 rela.r_offset = (htab->plt->output_section->vma
4103 + htab->plt->output_offset
4104 + h->plt.offset);
4105 rela.r_info = ELF32_R_INFO (h->dynindx, R_PPC_JMP_SLOT);
4106 rela.r_addend = 0;
4108 reloc_index = (h->plt.offset - PLT_INITIAL_ENTRY_SIZE) / PLT_SLOT_SIZE;
4109 if (reloc_index > PLT_NUM_SINGLE_ENTRIES)
4110 reloc_index -= (reloc_index - PLT_NUM_SINGLE_ENTRIES) / 2;
4111 loc = (htab->relplt->contents
4112 + reloc_index * sizeof (Elf32_External_Rela));
4113 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
4115 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
4117 /* Mark the symbol as undefined, rather than as defined in
4118 the .plt section. Leave the value alone. */
4119 sym->st_shndx = SHN_UNDEF;
4120 /* If the symbol is weak, we do need to clear the value.
4121 Otherwise, the PLT entry would provide a definition for
4122 the symbol even if the symbol wasn't defined anywhere,
4123 and so the symbol would never be NULL. */
4124 if ((h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR_NONWEAK)
4125 == 0)
4126 sym->st_value = 0;
4130 if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_COPY) != 0)
4132 asection *s;
4133 Elf_Internal_Rela rela;
4134 bfd_byte *loc;
4136 /* This symbols needs a copy reloc. Set it up. */
4138 #ifdef DEBUG
4139 fprintf (stderr, ", copy");
4140 #endif
4142 BFD_ASSERT (h->dynindx != -1);
4144 if (h->size <= elf_gp_size (htab->elf.dynobj))
4145 s = htab->relsbss;
4146 else
4147 s = htab->relbss;
4148 BFD_ASSERT (s != NULL);
4150 rela.r_offset = (h->root.u.def.value
4151 + h->root.u.def.section->output_section->vma
4152 + h->root.u.def.section->output_offset);
4153 rela.r_info = ELF32_R_INFO (h->dynindx, R_PPC_COPY);
4154 rela.r_addend = 0;
4155 loc = s->contents + s->reloc_count++ * sizeof (Elf32_External_Rela);
4156 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
4159 #ifdef DEBUG
4160 fprintf (stderr, "\n");
4161 #endif
4163 /* Mark some specially defined symbols as absolute. */
4164 if (strcmp (h->root.root.string, "_DYNAMIC") == 0
4165 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0
4166 || strcmp (h->root.root.string, "_PROCEDURE_LINKAGE_TABLE_") == 0)
4167 sym->st_shndx = SHN_ABS;
4169 return TRUE;
4172 /* Finish up the dynamic sections. */
4174 static bfd_boolean
4175 ppc_elf_finish_dynamic_sections (output_bfd, info)
4176 bfd *output_bfd;
4177 struct bfd_link_info *info;
4179 asection *sdyn;
4180 struct ppc_elf_link_hash_table *htab;
4182 #ifdef DEBUG
4183 fprintf (stderr, "ppc_elf_finish_dynamic_sections called\n");
4184 #endif
4186 htab = ppc_elf_hash_table (info);
4187 sdyn = bfd_get_section_by_name (htab->elf.dynobj, ".dynamic");
4189 if (htab->elf.dynamic_sections_created)
4191 Elf32_External_Dyn *dyncon, *dynconend;
4193 BFD_ASSERT (htab->plt != NULL && sdyn != NULL);
4195 dyncon = (Elf32_External_Dyn *) sdyn->contents;
4196 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->_raw_size);
4197 for (; dyncon < dynconend; dyncon++)
4199 Elf_Internal_Dyn dyn;
4200 asection *s;
4202 bfd_elf32_swap_dyn_in (htab->elf.dynobj, dyncon, &dyn);
4204 switch (dyn.d_tag)
4206 case DT_PLTGOT:
4207 s = htab->plt;
4208 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
4209 break;
4211 case DT_PLTRELSZ:
4212 dyn.d_un.d_val = htab->relplt->_raw_size;
4213 break;
4215 case DT_JMPREL:
4216 s = htab->relplt;
4217 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
4218 break;
4220 default:
4221 continue;
4224 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
4228 /* Add a blrl instruction at _GLOBAL_OFFSET_TABLE_-4 so that a function can
4229 easily find the address of the _GLOBAL_OFFSET_TABLE_. */
4230 if (htab->got)
4232 unsigned char *contents = htab->got->contents;
4233 bfd_put_32 (output_bfd, (bfd_vma) 0x4e800021 /* blrl */, contents);
4235 if (sdyn == NULL)
4236 bfd_put_32 (output_bfd, (bfd_vma) 0, contents + 4);
4237 else
4238 bfd_put_32 (output_bfd,
4239 sdyn->output_section->vma + sdyn->output_offset,
4240 contents + 4);
4242 elf_section_data (htab->got->output_section)->this_hdr.sh_entsize = 4;
4245 return TRUE;
4248 /* The RELOCATE_SECTION function is called by the ELF backend linker
4249 to handle the relocations for a section.
4251 The relocs are always passed as Rela structures; if the section
4252 actually uses Rel structures, the r_addend field will always be
4253 zero.
4255 This function is responsible for adjust the section contents as
4256 necessary, and (if using Rela relocs and generating a
4257 relocateable output file) adjusting the reloc addend as
4258 necessary.
4260 This function does not have to worry about setting the reloc
4261 address or the reloc symbol index.
4263 LOCAL_SYMS is a pointer to the swapped in local symbols.
4265 LOCAL_SECTIONS is an array giving the section in the input file
4266 corresponding to the st_shndx field of each local symbol.
4268 The global hash table entry for the global symbols can be found
4269 via elf_sym_hashes (input_bfd).
4271 When generating relocateable output, this function must handle
4272 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
4273 going to be the section symbol corresponding to the output
4274 section, which means that the addend must be adjusted
4275 accordingly. */
4277 static bfd_boolean
4278 ppc_elf_relocate_section (output_bfd, info, input_bfd, input_section,
4279 contents, relocs, local_syms, local_sections)
4280 bfd *output_bfd;
4281 struct bfd_link_info *info;
4282 bfd *input_bfd;
4283 asection *input_section;
4284 bfd_byte *contents;
4285 Elf_Internal_Rela *relocs;
4286 Elf_Internal_Sym *local_syms;
4287 asection **local_sections;
4289 Elf_Internal_Shdr *symtab_hdr;
4290 struct elf_link_hash_entry **sym_hashes;
4291 struct ppc_elf_link_hash_table *htab;
4292 Elf_Internal_Rela *rel;
4293 Elf_Internal_Rela *relend;
4294 Elf_Internal_Rela outrel;
4295 bfd_byte *loc;
4296 asection *sreloc = NULL;
4297 bfd_vma *local_got_offsets;
4298 bfd_boolean ret = TRUE;
4300 #ifdef DEBUG
4301 fprintf (stderr, "ppc_elf_relocate_section called for %s section %s, %ld relocations%s\n",
4302 bfd_archive_filename (input_bfd),
4303 bfd_section_name(input_bfd, input_section),
4304 (long) input_section->reloc_count,
4305 (info->relocateable) ? " (relocatable)" : "");
4306 #endif
4308 if (info->relocateable)
4309 return TRUE;
4311 if (!ppc_elf_howto_table[R_PPC_ADDR32])
4312 /* Initialize howto table if needed. */
4313 ppc_elf_howto_init ();
4315 htab = ppc_elf_hash_table (info);
4316 local_got_offsets = elf_local_got_offsets (input_bfd);
4317 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
4318 sym_hashes = elf_sym_hashes (input_bfd);
4319 rel = relocs;
4320 relend = relocs + input_section->reloc_count;
4321 for (; rel < relend; rel++)
4323 enum elf_ppc_reloc_type r_type;
4324 bfd_vma addend;
4325 bfd_reloc_status_type r;
4326 Elf_Internal_Sym *sym;
4327 asection *sec;
4328 struct elf_link_hash_entry *h;
4329 const char *sym_name;
4330 reloc_howto_type *howto;
4331 unsigned long r_symndx;
4332 bfd_vma relocation;
4333 bfd_vma branch_bit, insn, from;
4334 bfd_boolean unresolved_reloc;
4335 bfd_boolean warned;
4336 unsigned int tls_type, tls_mask, tls_gd;
4338 r_type = (enum elf_ppc_reloc_type)ELF32_R_TYPE (rel->r_info);
4339 sym = (Elf_Internal_Sym *) 0;
4340 sec = (asection *) 0;
4341 h = (struct elf_link_hash_entry *) 0;
4342 unresolved_reloc = FALSE;
4343 warned = FALSE;
4344 r_symndx = ELF32_R_SYM (rel->r_info);
4345 if (r_symndx < symtab_hdr->sh_info)
4347 sym = local_syms + r_symndx;
4348 sec = local_sections[r_symndx];
4349 sym_name = bfd_elf_local_sym_name (input_bfd, sym);
4351 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, sec, rel);
4353 else
4355 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
4356 while (h->root.type == bfd_link_hash_indirect
4357 || h->root.type == bfd_link_hash_warning)
4358 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4359 sym_name = h->root.root.string;
4361 relocation = 0;
4362 if (h->root.type == bfd_link_hash_defined
4363 || h->root.type == bfd_link_hash_defweak)
4365 sec = h->root.u.def.section;
4366 /* Set a flag that will be cleared later if we find a
4367 relocation value for this symbol. output_section
4368 is typically NULL for symbols satisfied by a shared
4369 library. */
4370 if (sec->output_section == NULL)
4371 unresolved_reloc = TRUE;
4372 else
4373 relocation = (h->root.u.def.value
4374 + sec->output_section->vma
4375 + sec->output_offset);
4377 else if (h->root.type == bfd_link_hash_undefweak)
4379 else if (info->shared
4380 && !info->no_undefined
4381 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
4383 else
4385 if (! ((*info->callbacks->undefined_symbol)
4386 (info, h->root.root.string, input_bfd, input_section,
4387 rel->r_offset, (!info->shared
4388 || info->no_undefined
4389 || ELF_ST_VISIBILITY (h->other)))))
4390 return FALSE;
4391 warned = TRUE;
4395 /* TLS optimizations. Replace instruction sequences and relocs
4396 based on information we collected in tls_optimize. We edit
4397 RELOCS so that --emit-relocs will output something sensible
4398 for the final instruction stream. */
4399 tls_mask = 0;
4400 tls_gd = 0;
4401 if (IS_PPC_TLS_RELOC (r_type))
4403 if (h != NULL)
4404 tls_mask = ((struct ppc_elf_link_hash_entry *) h)->tls_mask;
4405 else if (local_got_offsets != NULL)
4407 char *lgot_masks;
4408 lgot_masks = (char *) (local_got_offsets + symtab_hdr->sh_info);
4409 tls_mask = lgot_masks[r_symndx];
4413 /* Ensure reloc mapping code below stays sane. */
4414 if ((R_PPC_GOT_TLSLD16 & 3) != (R_PPC_GOT_TLSGD16 & 3)
4415 || (R_PPC_GOT_TLSLD16_LO & 3) != (R_PPC_GOT_TLSGD16_LO & 3)
4416 || (R_PPC_GOT_TLSLD16_HI & 3) != (R_PPC_GOT_TLSGD16_HI & 3)
4417 || (R_PPC_GOT_TLSLD16_HA & 3) != (R_PPC_GOT_TLSGD16_HA & 3)
4418 || (R_PPC_GOT_TLSLD16 & 3) != (R_PPC_GOT_TPREL16 & 3)
4419 || (R_PPC_GOT_TLSLD16_LO & 3) != (R_PPC_GOT_TPREL16_LO & 3)
4420 || (R_PPC_GOT_TLSLD16_HI & 3) != (R_PPC_GOT_TPREL16_HI & 3)
4421 || (R_PPC_GOT_TLSLD16_HA & 3) != (R_PPC_GOT_TPREL16_HA & 3))
4422 abort ();
4423 switch (r_type)
4425 default:
4426 break;
4428 case R_PPC_GOT_TPREL16:
4429 case R_PPC_GOT_TPREL16_LO:
4430 if (tls_mask != 0
4431 && (tls_mask & TLS_TPREL) == 0)
4433 bfd_vma insn;
4434 insn = bfd_get_32 (output_bfd, contents + rel->r_offset - 2);
4435 insn &= 31 << 21;
4436 insn |= 0x3c020000; /* addis 0,2,0 */
4437 bfd_put_32 (output_bfd, insn, contents + rel->r_offset - 2);
4438 r_type = R_PPC_TPREL16_HA;
4439 rel->r_info = ELF32_R_INFO (r_symndx, r_type);
4441 break;
4443 case R_PPC_TLS:
4444 if (tls_mask != 0
4445 && (tls_mask & TLS_TPREL) == 0)
4447 bfd_vma insn, rtra;
4448 insn = bfd_get_32 (output_bfd, contents + rel->r_offset);
4449 if ((insn & ((31 << 26) | (31 << 11)))
4450 == ((31 << 26) | (2 << 11)))
4451 rtra = insn & ((1 << 26) - (1 << 16));
4452 else if ((insn & ((31 << 26) | (31 << 16)))
4453 == ((31 << 26) | (2 << 16)))
4454 rtra = (insn & (31 << 21)) | ((insn & (31 << 11)) << 5);
4455 else
4456 abort ();
4457 if ((insn & ((1 << 11) - (1 << 1))) == 266 << 1)
4458 /* add -> addi. */
4459 insn = 14 << 26;
4460 else if ((insn & (31 << 1)) == 23 << 1
4461 && ((insn & (31 << 6)) < 14 << 6
4462 || ((insn & (31 << 6)) >= 16 << 6
4463 && (insn & (31 << 6)) < 24 << 6)))
4464 /* load and store indexed -> dform. */
4465 insn = (32 | ((insn >> 6) & 31)) << 26;
4466 else if ((insn & (31 << 1)) == 21 << 1
4467 && (insn & (0x1a << 6)) == 0)
4468 /* ldx, ldux, stdx, stdux -> ld, ldu, std, stdu. */
4469 insn = (((58 | ((insn >> 6) & 4)) << 26)
4470 | ((insn >> 6) & 1));
4471 else if ((insn & (31 << 1)) == 21 << 1
4472 && (insn & ((1 << 11) - (1 << 1))) == 341 << 1)
4473 /* lwax -> lwa. */
4474 insn = (58 << 26) | 2;
4475 else
4476 abort ();
4477 insn |= rtra;
4478 bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
4479 r_type = R_PPC_TPREL16_LO;
4480 rel->r_info = ELF32_R_INFO (r_symndx, r_type);
4481 /* Was PPC_TLS which sits on insn boundary, now
4482 PPC_TPREL16_LO which is at insn+2. */
4483 rel->r_offset += 2;
4485 break;
4487 case R_PPC_GOT_TLSGD16_HI:
4488 case R_PPC_GOT_TLSGD16_HA:
4489 tls_gd = TLS_TPRELGD;
4490 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
4491 goto tls_gdld_hi;
4492 break;
4494 case R_PPC_GOT_TLSLD16_HI:
4495 case R_PPC_GOT_TLSLD16_HA:
4496 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
4498 tls_gdld_hi:
4499 if ((tls_mask & tls_gd) != 0)
4500 r_type = (((r_type - (R_PPC_GOT_TLSGD16 & 3)) & 3)
4501 + R_PPC_GOT_TPREL16);
4502 else
4504 bfd_put_32 (output_bfd, NOP, contents + rel->r_offset);
4505 rel->r_offset -= 2;
4506 r_type = R_PPC_NONE;
4508 rel->r_info = ELF32_R_INFO (r_symndx, r_type);
4510 break;
4512 case R_PPC_GOT_TLSGD16:
4513 case R_PPC_GOT_TLSGD16_LO:
4514 tls_gd = TLS_TPRELGD;
4515 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
4516 goto tls_get_addr_check;
4517 break;
4519 case R_PPC_GOT_TLSLD16:
4520 case R_PPC_GOT_TLSLD16_LO:
4521 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
4523 tls_get_addr_check:
4524 if (rel + 1 < relend)
4526 enum elf_ppc_reloc_type r_type2;
4527 unsigned long r_symndx2;
4528 struct elf_link_hash_entry *h2;
4529 bfd_vma insn1, insn2;
4530 bfd_vma offset;
4532 /* The next instruction should be a call to
4533 __tls_get_addr. Peek at the reloc to be sure. */
4534 r_type2
4535 = (enum elf_ppc_reloc_type) ELF32_R_TYPE (rel[1].r_info);
4536 r_symndx2 = ELF32_R_SYM (rel[1].r_info);
4537 if (r_symndx2 < symtab_hdr->sh_info
4538 || (r_type2 != R_PPC_REL14
4539 && r_type2 != R_PPC_REL14_BRTAKEN
4540 && r_type2 != R_PPC_REL14_BRNTAKEN
4541 && r_type2 != R_PPC_REL24
4542 && r_type2 != R_PPC_PLTREL24))
4543 break;
4545 h2 = sym_hashes[r_symndx2 - symtab_hdr->sh_info];
4546 while (h2->root.type == bfd_link_hash_indirect
4547 || h2->root.type == bfd_link_hash_warning)
4548 h2 = (struct elf_link_hash_entry *) h2->root.u.i.link;
4549 if (h2 == NULL || h2 != htab->tls_get_addr)
4550 break;
4552 /* OK, it checks out. Replace the call. */
4553 offset = rel[1].r_offset;
4554 insn1 = bfd_get_32 (output_bfd,
4555 contents + rel->r_offset - 2);
4556 if ((tls_mask & tls_gd) != 0)
4558 /* IE */
4559 insn1 &= (1 << 26) - 1;
4560 insn1 |= 32 << 26; /* lwz */
4561 insn2 = 0x7c631214; /* add 3,3,2 */
4562 rel[1].r_info = ELF32_R_INFO (r_symndx2, R_PPC_NONE);
4563 r_type = (((r_type - (R_PPC_GOT_TLSGD16 & 3)) & 3)
4564 + R_PPC_GOT_TPREL16);
4565 rel->r_info = ELF32_R_INFO (r_symndx, r_type);
4567 else
4569 /* LE */
4570 insn1 = 0x3c620000; /* addis 3,2,0 */
4571 insn2 = 0x38630000; /* addi 3,3,0 */
4572 if (tls_gd == 0)
4574 /* Was an LD reloc. */
4575 r_symndx = 0;
4576 rel->r_addend = htab->tls_sec->vma + DTP_OFFSET;
4577 rel[1].r_addend = htab->tls_sec->vma + DTP_OFFSET;
4579 r_type = R_PPC_TPREL16_HA;
4580 rel->r_info = ELF32_R_INFO (r_symndx, r_type);
4581 rel[1].r_info = ELF32_R_INFO (r_symndx,
4582 R_PPC_TPREL16_LO);
4583 rel[1].r_offset += 2;
4585 bfd_put_32 (output_bfd, insn1, contents + rel->r_offset - 2);
4586 bfd_put_32 (output_bfd, insn2, contents + offset);
4587 if (tls_gd == 0)
4589 /* We changed the symbol on an LD reloc. Start over
4590 in order to get h, sym, sec etc. right. */
4591 rel--;
4592 continue;
4596 break;
4599 /* Handle other relocations that tweak non-addend part of insn. */
4600 branch_bit = 0;
4601 switch (r_type)
4603 default:
4604 break;
4606 /* Branch taken prediction relocations. */
4607 case R_PPC_ADDR14_BRTAKEN:
4608 case R_PPC_REL14_BRTAKEN:
4609 branch_bit = BRANCH_PREDICT_BIT;
4610 /* Fall thru */
4612 /* Branch not taken predicition relocations. */
4613 case R_PPC_ADDR14_BRNTAKEN:
4614 case R_PPC_REL14_BRNTAKEN:
4615 insn = bfd_get_32 (output_bfd, contents + rel->r_offset);
4616 insn &= ~BRANCH_PREDICT_BIT;
4617 insn |= branch_bit;
4619 from = (rel->r_offset
4620 + input_section->output_offset
4621 + input_section->output_section->vma);
4623 /* Invert 'y' bit if not the default. */
4624 if ((bfd_signed_vma) (relocation + rel->r_addend - from) < 0)
4625 insn ^= BRANCH_PREDICT_BIT;
4627 bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
4628 break;
4631 addend = rel->r_addend;
4632 tls_type = 0;
4633 howto = NULL;
4634 if ((unsigned) r_type < (unsigned) R_PPC_max)
4635 howto = ppc_elf_howto_table[(int) r_type];
4636 switch (r_type)
4638 default:
4639 (*_bfd_error_handler)
4640 (_("%s: unknown relocation type %d for symbol %s"),
4641 bfd_archive_filename (input_bfd), (int) r_type, sym_name);
4643 bfd_set_error (bfd_error_bad_value);
4644 ret = FALSE;
4645 continue;
4647 case R_PPC_NONE:
4648 case R_PPC_TLS:
4649 case R_PPC_EMB_MRKREF:
4650 case R_PPC_GNU_VTINHERIT:
4651 case R_PPC_GNU_VTENTRY:
4652 continue;
4654 /* GOT16 relocations. Like an ADDR16 using the symbol's
4655 address in the GOT as relocation value instead of the
4656 symbol's value itself. Also, create a GOT entry for the
4657 symbol and put the symbol value there. */
4658 case R_PPC_GOT_TLSGD16:
4659 case R_PPC_GOT_TLSGD16_LO:
4660 case R_PPC_GOT_TLSGD16_HI:
4661 case R_PPC_GOT_TLSGD16_HA:
4662 tls_type = TLS_TLS | TLS_GD;
4663 goto dogot;
4665 case R_PPC_GOT_TLSLD16:
4666 case R_PPC_GOT_TLSLD16_LO:
4667 case R_PPC_GOT_TLSLD16_HI:
4668 case R_PPC_GOT_TLSLD16_HA:
4669 tls_type = TLS_TLS | TLS_LD;
4670 goto dogot;
4672 case R_PPC_GOT_TPREL16:
4673 case R_PPC_GOT_TPREL16_LO:
4674 case R_PPC_GOT_TPREL16_HI:
4675 case R_PPC_GOT_TPREL16_HA:
4676 tls_type = TLS_TLS | TLS_TPREL;
4677 goto dogot;
4679 case R_PPC_GOT_DTPREL16:
4680 case R_PPC_GOT_DTPREL16_LO:
4681 case R_PPC_GOT_DTPREL16_HI:
4682 case R_PPC_GOT_DTPREL16_HA:
4683 tls_type = TLS_TLS | TLS_DTPREL;
4684 goto dogot;
4686 case R_PPC_GOT16:
4687 case R_PPC_GOT16_LO:
4688 case R_PPC_GOT16_HI:
4689 case R_PPC_GOT16_HA:
4690 dogot:
4692 /* Relocation is to the entry for this symbol in the global
4693 offset table. */
4694 bfd_vma off;
4695 bfd_vma *offp;
4696 unsigned long indx;
4698 if (htab->got == NULL)
4699 abort ();
4701 indx = 0;
4702 if (tls_type == (TLS_TLS | TLS_LD)
4703 && (h == NULL
4704 || !(h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC)))
4705 offp = &htab->tlsld_got.offset;
4706 else if (h != NULL)
4708 bfd_boolean dyn;
4709 dyn = htab->elf.dynamic_sections_created;
4710 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
4711 || (info->shared
4712 && SYMBOL_REFERENCES_LOCAL (info, h)))
4713 /* This is actually a static link, or it is a
4714 -Bsymbolic link and the symbol is defined
4715 locally, or the symbol was forced to be local
4716 because of a version file. */
4718 else
4720 indx = h->dynindx;
4721 unresolved_reloc = FALSE;
4723 offp = &h->got.offset;
4725 else
4727 if (local_got_offsets == NULL)
4728 abort ();
4729 offp = &local_got_offsets[r_symndx];
4732 /* The offset must always be a multiple of 4. We use the
4733 least significant bit to record whether we have already
4734 processed this entry. */
4735 off = *offp;
4736 if ((off & 1) != 0)
4737 off &= ~1;
4738 else
4740 unsigned int tls_m = (tls_mask
4741 & (TLS_LD | TLS_GD | TLS_DTPREL
4742 | TLS_TPREL | TLS_TPRELGD));
4744 if (offp == &htab->tlsld_got.offset)
4745 tls_m = TLS_LD;
4746 else if (h == NULL
4747 || !(h->elf_link_hash_flags
4748 & ELF_LINK_HASH_DEF_DYNAMIC))
4749 tls_m &= ~TLS_LD;
4751 /* We might have multiple got entries for this sym.
4752 Initialize them all. */
4755 int tls_ty = 0;
4757 if ((tls_m & TLS_LD) != 0)
4759 tls_ty = TLS_TLS | TLS_LD;
4760 tls_m &= ~TLS_LD;
4762 else if ((tls_m & TLS_GD) != 0)
4764 tls_ty = TLS_TLS | TLS_GD;
4765 tls_m &= ~TLS_GD;
4767 else if ((tls_m & TLS_DTPREL) != 0)
4769 tls_ty = TLS_TLS | TLS_DTPREL;
4770 tls_m &= ~TLS_DTPREL;
4772 else if ((tls_m & (TLS_TPREL | TLS_TPRELGD)) != 0)
4774 tls_ty = TLS_TLS | TLS_TPREL;
4775 tls_m = 0;
4778 /* Generate relocs for the dynamic linker. */
4779 if (info->shared || indx != 0)
4781 outrel.r_offset = (htab->got->output_section->vma
4782 + htab->got->output_offset
4783 + off);
4784 outrel.r_addend = 0;
4785 if (tls_ty & (TLS_LD | TLS_GD))
4787 outrel.r_info = ELF32_R_INFO (indx, R_PPC_DTPMOD32);
4788 if (tls_ty == (TLS_TLS | TLS_GD))
4790 loc = htab->relgot->contents;
4791 loc += (htab->relgot->reloc_count++
4792 * sizeof (Elf32_External_Rela));
4793 bfd_elf32_swap_reloca_out (output_bfd,
4794 &outrel, loc);
4795 outrel.r_offset += 4;
4796 outrel.r_info
4797 = ELF32_R_INFO (indx, R_PPC_DTPREL32);
4800 else if (tls_ty == (TLS_TLS | TLS_DTPREL))
4801 outrel.r_info = ELF32_R_INFO (indx, R_PPC_DTPREL32);
4802 else if (tls_ty == (TLS_TLS | TLS_TPREL))
4803 outrel.r_info = ELF32_R_INFO (indx, R_PPC_TPREL32);
4804 else if (indx == 0)
4805 outrel.r_info = ELF32_R_INFO (indx, R_PPC_RELATIVE);
4806 else
4807 outrel.r_info = ELF32_R_INFO (indx, R_PPC_GLOB_DAT);
4808 if (indx == 0)
4810 outrel.r_addend += relocation;
4811 if (tls_ty & (TLS_GD | TLS_DTPREL | TLS_TPREL))
4812 outrel.r_addend -= htab->tls_sec->vma;
4814 loc = htab->relgot->contents;
4815 loc += (htab->relgot->reloc_count++
4816 * sizeof (Elf32_External_Rela));
4817 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
4820 /* Init the .got section contents if we're not
4821 emitting a reloc. */
4822 else
4824 bfd_vma value = relocation;
4826 if (tls_ty == (TLS_TLS | TLS_LD))
4827 value = 1;
4828 else if (tls_ty != 0)
4830 value -= htab->tls_sec->vma + DTP_OFFSET;
4831 if (tls_ty == (TLS_TLS | TLS_TPREL))
4832 value += DTP_OFFSET - TP_OFFSET;
4834 if (tls_ty == (TLS_TLS | TLS_GD))
4836 bfd_put_32 (output_bfd, value,
4837 htab->got->contents + off + 4);
4838 value = 1;
4841 bfd_put_32 (output_bfd, value,
4842 htab->got->contents + off);
4845 off += 4;
4846 if (tls_ty & (TLS_LD | TLS_GD))
4847 off += 4;
4849 while (tls_m != 0);
4851 off = *offp;
4852 *offp = off | 1;
4855 if (off >= (bfd_vma) -2)
4856 abort ();
4858 if ((tls_type & TLS_TLS) != 0)
4860 if (tls_type != (TLS_TLS | TLS_LD))
4862 if ((tls_mask & TLS_LD) != 0
4863 && !(h == NULL
4864 || !(h->elf_link_hash_flags
4865 & ELF_LINK_HASH_DEF_DYNAMIC)))
4866 off += 8;
4867 if (tls_type != (TLS_TLS | TLS_GD))
4869 if ((tls_mask & TLS_GD) != 0)
4870 off += 8;
4871 if (tls_type != (TLS_TLS | TLS_DTPREL))
4873 if ((tls_mask & TLS_DTPREL) != 0)
4874 off += 4;
4880 relocation = htab->got->output_offset + off - 4;
4882 /* Addends on got relocations don't make much sense.
4883 x+off@got is actually x@got+off, and since the got is
4884 generated by a hash table traversal, the value in the
4885 got at entry m+n bears little relation to the entry m. */
4886 if (addend != 0)
4887 (*_bfd_error_handler)
4888 (_("%s(%s+0x%lx): non-zero addend on %s reloc against `%s'"),
4889 bfd_archive_filename (input_bfd),
4890 bfd_get_section_name (input_bfd, input_section),
4891 (long) rel->r_offset,
4892 howto->name,
4893 sym_name);
4895 break;
4897 /* Relocations that need no special processing. */
4898 case R_PPC_LOCAL24PC:
4899 /* It makes no sense to point a local relocation
4900 at a symbol not in this object. */
4901 if (unresolved_reloc)
4903 if (! (*info->callbacks->undefined_symbol) (info,
4904 h->root.root.string,
4905 input_bfd,
4906 input_section,
4907 rel->r_offset,
4908 TRUE))
4909 return FALSE;
4910 continue;
4912 break;
4914 case R_PPC_DTPREL16:
4915 case R_PPC_DTPREL16_LO:
4916 case R_PPC_DTPREL16_HI:
4917 case R_PPC_DTPREL16_HA:
4918 addend -= htab->tls_sec->vma + DTP_OFFSET;
4919 break;
4921 /* Relocations that may need to be propagated if this is a shared
4922 object. */
4923 case R_PPC_TPREL16:
4924 case R_PPC_TPREL16_LO:
4925 case R_PPC_TPREL16_HI:
4926 case R_PPC_TPREL16_HA:
4927 addend -= htab->tls_sec->vma + TP_OFFSET;
4928 /* The TPREL16 relocs shouldn't really be used in shared
4929 libs as they will result in DT_TEXTREL being set, but
4930 support them anyway. */
4931 goto dodyn;
4933 case R_PPC_TPREL32:
4934 addend -= htab->tls_sec->vma + TP_OFFSET;
4935 goto dodyn;
4937 case R_PPC_DTPREL32:
4938 addend -= htab->tls_sec->vma + DTP_OFFSET;
4939 goto dodyn;
4941 case R_PPC_DTPMOD32:
4942 relocation = 1;
4943 addend = 0;
4944 goto dodyn;
4946 case R_PPC_REL24:
4947 case R_PPC_REL32:
4948 case R_PPC_REL14:
4949 case R_PPC_REL14_BRTAKEN:
4950 case R_PPC_REL14_BRNTAKEN:
4951 /* If these relocations are not to a named symbol, they can be
4952 handled right here, no need to bother the dynamic linker. */
4953 if (h == NULL
4954 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0
4955 || SYMBOL_REFERENCES_LOCAL (info, h))
4956 break;
4957 /* fall through */
4959 /* Relocations that always need to be propagated if this is a shared
4960 object. */
4961 case R_PPC_ADDR32:
4962 case R_PPC_ADDR24:
4963 case R_PPC_ADDR16:
4964 case R_PPC_ADDR16_LO:
4965 case R_PPC_ADDR16_HI:
4966 case R_PPC_ADDR16_HA:
4967 case R_PPC_ADDR14:
4968 case R_PPC_ADDR14_BRTAKEN:
4969 case R_PPC_ADDR14_BRNTAKEN:
4970 case R_PPC_UADDR32:
4971 case R_PPC_UADDR16:
4972 /* r_symndx will be zero only for relocs against symbols
4973 from removed linkonce sections, or sections discarded by
4974 a linker script. */
4975 dodyn:
4976 if (r_symndx == 0)
4977 break;
4978 /* Fall thru. */
4980 if ((info->shared
4981 && (MUST_BE_DYN_RELOC (r_type)
4982 || (h != NULL
4983 && h->dynindx != -1
4984 && (!info->symbolic
4985 || (h->elf_link_hash_flags
4986 & ELF_LINK_HASH_DEF_REGULAR) == 0))))
4987 || (ELIMINATE_COPY_RELOCS
4988 && !info->shared
4989 && (input_section->flags & SEC_ALLOC) != 0
4990 && h != NULL
4991 && h->dynindx != -1
4992 && (h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0
4993 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
4994 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0))
4996 int skip;
4998 #ifdef DEBUG
4999 fprintf (stderr, "ppc_elf_relocate_section need to create relocation for %s\n",
5000 (h && h->root.root.string
5001 ? h->root.root.string : "<unknown>"));
5002 #endif
5004 /* When generating a shared object, these relocations
5005 are copied into the output file to be resolved at run
5006 time. */
5007 if (sreloc == NULL)
5009 const char *name;
5011 name = (bfd_elf_string_from_elf_section
5012 (input_bfd,
5013 elf_elfheader (input_bfd)->e_shstrndx,
5014 elf_section_data (input_section)->rel_hdr.sh_name));
5015 if (name == NULL)
5016 return FALSE;
5018 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
5019 && strcmp (bfd_get_section_name (input_bfd,
5020 input_section),
5021 name + 5) == 0);
5023 sreloc = bfd_get_section_by_name (htab->elf.dynobj, name);
5024 BFD_ASSERT (sreloc != NULL);
5027 skip = 0;
5029 outrel.r_offset =
5030 _bfd_elf_section_offset (output_bfd, info, input_section,
5031 rel->r_offset);
5032 if (outrel.r_offset == (bfd_vma) -1
5033 || outrel.r_offset == (bfd_vma) -2)
5034 skip = (int) outrel.r_offset;
5035 outrel.r_offset += (input_section->output_section->vma
5036 + input_section->output_offset);
5038 if (skip)
5039 memset (&outrel, 0, sizeof outrel);
5040 else if (h != NULL
5041 && !SYMBOL_REFERENCES_LOCAL (info, h))
5043 unresolved_reloc = FALSE;
5044 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
5045 outrel.r_addend = rel->r_addend;
5047 else
5049 outrel.r_addend = relocation + rel->r_addend;
5051 if (r_type == R_PPC_ADDR32)
5052 outrel.r_info = ELF32_R_INFO (0, R_PPC_RELATIVE);
5053 else
5055 long indx;
5057 if (bfd_is_abs_section (sec))
5058 indx = 0;
5059 else if (sec == NULL || sec->owner == NULL)
5061 bfd_set_error (bfd_error_bad_value);
5062 return FALSE;
5064 else
5066 asection *osec;
5068 /* We are turning this relocation into one
5069 against a section symbol. It would be
5070 proper to subtract the symbol's value,
5071 osec->vma, from the emitted reloc addend,
5072 but ld.so expects buggy relocs. */
5073 osec = sec->output_section;
5074 indx = elf_section_data (osec)->dynindx;
5075 BFD_ASSERT (indx > 0);
5076 #ifdef DEBUG
5077 if (indx <= 0)
5079 printf ("indx=%d section=%s flags=%08x name=%s\n",
5080 indx, osec->name, osec->flags,
5081 h->root.root.string);
5083 #endif
5086 outrel.r_info = ELF32_R_INFO (indx, r_type);
5090 loc = sreloc->contents;
5091 loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rela);
5092 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
5094 if (skip == -1)
5095 continue;
5097 /* This reloc will be computed at runtime. We clear the memory
5098 so that it contains predictable value. */
5099 if (! skip
5100 && ((input_section->flags & SEC_ALLOC) != 0
5101 || ELF32_R_TYPE (outrel.r_info) != R_PPC_RELATIVE))
5103 relocation = howto->pc_relative ? outrel.r_offset : 0;
5104 addend = 0;
5105 break;
5108 break;
5110 /* Indirect .sdata relocation. */
5111 case R_PPC_EMB_SDAI16:
5112 BFD_ASSERT (htab->sdata != NULL);
5113 relocation
5114 = bfd_elf32_finish_pointer_linker_section (output_bfd, input_bfd,
5115 info, htab->sdata, h,
5116 relocation, rel,
5117 R_PPC_RELATIVE);
5118 break;
5120 /* Indirect .sdata2 relocation. */
5121 case R_PPC_EMB_SDA2I16:
5122 BFD_ASSERT (htab->sdata2 != NULL);
5123 relocation
5124 = bfd_elf32_finish_pointer_linker_section (output_bfd, input_bfd,
5125 info, htab->sdata2, h,
5126 relocation, rel,
5127 R_PPC_RELATIVE);
5128 break;
5130 /* Handle the TOC16 reloc. We want to use the offset within the .got
5131 section, not the actual VMA. This is appropriate when generating
5132 an embedded ELF object, for which the .got section acts like the
5133 AIX .toc section. */
5134 case R_PPC_TOC16: /* phony GOT16 relocations */
5135 BFD_ASSERT (sec != (asection *) 0);
5136 BFD_ASSERT (bfd_is_und_section (sec)
5137 || strcmp (bfd_get_section_name (abfd, sec), ".got") == 0
5138 || strcmp (bfd_get_section_name (abfd, sec), ".cgot") == 0)
5140 addend -= sec->output_section->vma + sec->output_offset + 0x8000;
5141 break;
5143 case R_PPC_PLTREL24:
5144 /* Relocation is to the entry for this symbol in the
5145 procedure linkage table. */
5146 BFD_ASSERT (h != NULL);
5148 if (h->plt.offset == (bfd_vma) -1
5149 || htab->plt == NULL)
5151 /* We didn't make a PLT entry for this symbol. This
5152 happens when statically linking PIC code, or when
5153 using -Bsymbolic. */
5154 break;
5157 unresolved_reloc = FALSE;
5158 relocation = (htab->plt->output_section->vma
5159 + htab->plt->output_offset
5160 + h->plt.offset);
5161 break;
5163 /* Relocate against _SDA_BASE_. */
5164 case R_PPC_SDAREL16:
5166 const char *name;
5167 const struct elf_link_hash_entry *sh;
5169 BFD_ASSERT (sec != (asection *) 0);
5170 name = bfd_get_section_name (abfd, sec->output_section);
5171 if (! ((strncmp (name, ".sdata", 6) == 0
5172 && (name[6] == 0 || name[6] == '.'))
5173 || (strncmp (name, ".sbss", 5) == 0
5174 && (name[5] == 0 || name[5] == '.'))))
5176 (*_bfd_error_handler) (_("%s: the target (%s) of a %s relocation is in the wrong output section (%s)"),
5177 bfd_archive_filename (input_bfd),
5178 sym_name,
5179 howto->name,
5180 name);
5182 sh = htab->sdata->sym_hash;
5183 addend -= (sh->root.u.def.value
5184 + sh->root.u.def.section->output_section->vma
5185 + sh->root.u.def.section->output_offset);
5187 break;
5189 /* Relocate against _SDA2_BASE_. */
5190 case R_PPC_EMB_SDA2REL:
5192 const char *name;
5193 const struct elf_link_hash_entry *sh;
5195 BFD_ASSERT (sec != (asection *) 0);
5196 name = bfd_get_section_name (abfd, sec->output_section);
5197 if (! (strncmp (name, ".sdata2", 7) == 0
5198 || strncmp (name, ".sbss2", 6) == 0))
5200 (*_bfd_error_handler) (_("%s: the target (%s) of a %s relocation is in the wrong output section (%s)"),
5201 bfd_archive_filename (input_bfd),
5202 sym_name,
5203 howto->name,
5204 name);
5206 bfd_set_error (bfd_error_bad_value);
5207 ret = FALSE;
5208 continue;
5210 sh = htab->sdata2->sym_hash;
5211 addend -= (sh->root.u.def.value
5212 + sh->root.u.def.section->output_section->vma
5213 + sh->root.u.def.section->output_offset);
5215 break;
5217 /* Relocate against either _SDA_BASE_, _SDA2_BASE_, or 0. */
5218 case R_PPC_EMB_SDA21:
5219 case R_PPC_EMB_RELSDA:
5221 const char *name;
5222 const struct elf_link_hash_entry *sh;
5223 int reg;
5225 BFD_ASSERT (sec != (asection *) 0);
5226 name = bfd_get_section_name (abfd, sec->output_section);
5227 if (((strncmp (name, ".sdata", 6) == 0
5228 && (name[6] == 0 || name[6] == '.'))
5229 || (strncmp (name, ".sbss", 5) == 0
5230 && (name[5] == 0 || name[5] == '.'))))
5232 reg = 13;
5233 sh = htab->sdata->sym_hash;
5234 addend -= (sh->root.u.def.value
5235 + sh->root.u.def.section->output_section->vma
5236 + sh->root.u.def.section->output_offset);
5239 else if (strncmp (name, ".sdata2", 7) == 0
5240 || strncmp (name, ".sbss2", 6) == 0)
5242 reg = 2;
5243 sh = htab->sdata2->sym_hash;
5244 addend -= (sh->root.u.def.value
5245 + sh->root.u.def.section->output_section->vma
5246 + sh->root.u.def.section->output_offset);
5249 else if (strcmp (name, ".PPC.EMB.sdata0") == 0
5250 || strcmp (name, ".PPC.EMB.sbss0") == 0)
5252 reg = 0;
5255 else
5257 (*_bfd_error_handler) (_("%s: the target (%s) of a %s relocation is in the wrong output section (%s)"),
5258 bfd_archive_filename (input_bfd),
5259 sym_name,
5260 howto->name,
5261 name);
5263 bfd_set_error (bfd_error_bad_value);
5264 ret = FALSE;
5265 continue;
5268 if (r_type == R_PPC_EMB_SDA21)
5269 { /* fill in register field */
5270 insn = bfd_get_32 (output_bfd, contents + rel->r_offset);
5271 insn = (insn & ~RA_REGISTER_MASK) | (reg << RA_REGISTER_SHIFT);
5272 bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
5275 break;
5277 /* Relocate against the beginning of the section. */
5278 case R_PPC_SECTOFF:
5279 case R_PPC_SECTOFF_LO:
5280 case R_PPC_SECTOFF_HI:
5281 case R_PPC_SECTOFF_HA:
5282 BFD_ASSERT (sec != (asection *) 0);
5283 addend -= sec->output_section->vma;
5284 break;
5286 /* Negative relocations. */
5287 case R_PPC_EMB_NADDR32:
5288 case R_PPC_EMB_NADDR16:
5289 case R_PPC_EMB_NADDR16_LO:
5290 case R_PPC_EMB_NADDR16_HI:
5291 case R_PPC_EMB_NADDR16_HA:
5292 addend -= 2 * relocation;
5293 break;
5295 case R_PPC_COPY:
5296 case R_PPC_GLOB_DAT:
5297 case R_PPC_JMP_SLOT:
5298 case R_PPC_RELATIVE:
5299 case R_PPC_PLT32:
5300 case R_PPC_PLTREL32:
5301 case R_PPC_PLT16_LO:
5302 case R_PPC_PLT16_HI:
5303 case R_PPC_PLT16_HA:
5304 case R_PPC_ADDR30:
5305 case R_PPC_EMB_RELSEC16:
5306 case R_PPC_EMB_RELST_LO:
5307 case R_PPC_EMB_RELST_HI:
5308 case R_PPC_EMB_RELST_HA:
5309 case R_PPC_EMB_BIT_FLD:
5310 (*_bfd_error_handler)
5311 (_("%s: relocation %s is not yet supported for symbol %s."),
5312 bfd_archive_filename (input_bfd),
5313 howto->name,
5314 sym_name);
5316 bfd_set_error (bfd_error_invalid_operation);
5317 ret = FALSE;
5318 continue;
5321 /* Do any further special processing. */
5322 switch (r_type)
5324 default:
5325 break;
5327 case R_PPC_ADDR16_HA:
5328 case R_PPC_GOT16_HA:
5329 case R_PPC_PLT16_HA:
5330 case R_PPC_SECTOFF_HA:
5331 case R_PPC_TPREL16_HA:
5332 case R_PPC_DTPREL16_HA:
5333 case R_PPC_GOT_TLSGD16_HA:
5334 case R_PPC_GOT_TLSLD16_HA:
5335 case R_PPC_GOT_TPREL16_HA:
5336 case R_PPC_GOT_DTPREL16_HA:
5337 case R_PPC_EMB_NADDR16_HA:
5338 case R_PPC_EMB_RELST_HA:
5339 /* It's just possible that this symbol is a weak symbol
5340 that's not actually defined anywhere. In that case,
5341 'sec' would be NULL, and we should leave the symbol
5342 alone (it will be set to zero elsewhere in the link). */
5343 if (sec != NULL)
5344 /* Add 0x10000 if sign bit in 0:15 is set.
5345 Bits 0:15 are not used. */
5346 addend += 0x8000;
5347 break;
5350 #ifdef DEBUG
5351 fprintf (stderr, "\ttype = %s (%d), name = %s, symbol index = %ld, offset = %ld, addend = %ld\n",
5352 howto->name,
5353 (int) r_type,
5354 sym_name,
5355 r_symndx,
5356 (long) rel->r_offset,
5357 (long) addend);
5358 #endif
5360 if (unresolved_reloc
5361 && !((input_section->flags & SEC_DEBUGGING) != 0
5362 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0))
5364 (*_bfd_error_handler)
5365 (_("%s(%s+0x%lx): unresolvable %s relocation against symbol `%s'"),
5366 bfd_archive_filename (input_bfd),
5367 bfd_get_section_name (input_bfd, input_section),
5368 (long) rel->r_offset,
5369 howto->name,
5370 sym_name);
5371 ret = FALSE;
5374 r = _bfd_final_link_relocate (howto,
5375 input_bfd,
5376 input_section,
5377 contents,
5378 rel->r_offset,
5379 relocation,
5380 addend);
5382 if (r != bfd_reloc_ok)
5384 if (sym_name == NULL)
5385 sym_name = "(null)";
5386 if (r == bfd_reloc_overflow)
5388 if (warned)
5389 continue;
5390 if (h != NULL
5391 && h->root.type == bfd_link_hash_undefweak
5392 && howto->pc_relative)
5394 /* Assume this is a call protected by other code that
5395 detect the symbol is undefined. If this is the case,
5396 we can safely ignore the overflow. If not, the
5397 program is hosed anyway, and a little warning isn't
5398 going to help. */
5400 continue;
5403 if (! (*info->callbacks->reloc_overflow) (info,
5404 sym_name,
5405 howto->name,
5406 rel->r_addend,
5407 input_bfd,
5408 input_section,
5409 rel->r_offset))
5410 return FALSE;
5412 else
5414 (*_bfd_error_handler)
5415 (_("%s(%s+0x%lx): %s reloc against `%s': error %d"),
5416 bfd_archive_filename (input_bfd),
5417 bfd_get_section_name (input_bfd, input_section),
5418 (long) rel->r_offset, howto->name, sym_name, (int) r);
5419 ret = FALSE;
5424 #ifdef DEBUG
5425 fprintf (stderr, "\n");
5426 #endif
5428 return ret;
5431 static enum elf_reloc_type_class
5432 ppc_elf_reloc_type_class (rela)
5433 const Elf_Internal_Rela *rela;
5435 switch ((int) ELF32_R_TYPE (rela->r_info))
5437 case R_PPC_RELATIVE:
5438 return reloc_class_relative;
5439 case R_PPC_REL24:
5440 case R_PPC_ADDR24:
5441 case R_PPC_JMP_SLOT:
5442 return reloc_class_plt;
5443 case R_PPC_COPY:
5444 return reloc_class_copy;
5445 default:
5446 return reloc_class_normal;
5450 /* Support for core dump NOTE sections. */
5452 static bfd_boolean
5453 ppc_elf_grok_prstatus (abfd, note)
5454 bfd *abfd;
5455 Elf_Internal_Note *note;
5457 int offset;
5458 unsigned int raw_size;
5460 switch (note->descsz)
5462 default:
5463 return FALSE;
5465 case 268: /* Linux/PPC. */
5466 /* pr_cursig */
5467 elf_tdata (abfd)->core_signal = bfd_get_16 (abfd, note->descdata + 12);
5469 /* pr_pid */
5470 elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, note->descdata + 24);
5472 /* pr_reg */
5473 offset = 72;
5474 raw_size = 192;
5476 break;
5479 /* Make a ".reg/999" section. */
5480 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
5481 raw_size, note->descpos + offset);
5484 static bfd_boolean
5485 ppc_elf_grok_psinfo (abfd, note)
5486 bfd *abfd;
5487 Elf_Internal_Note *note;
5489 switch (note->descsz)
5491 default:
5492 return FALSE;
5494 case 128: /* Linux/PPC elf_prpsinfo. */
5495 elf_tdata (abfd)->core_program
5496 = _bfd_elfcore_strndup (abfd, note->descdata + 32, 16);
5497 elf_tdata (abfd)->core_command
5498 = _bfd_elfcore_strndup (abfd, note->descdata + 48, 80);
5501 /* Note that for some reason, a spurious space is tacked
5502 onto the end of the args in some (at least one anyway)
5503 implementations, so strip it off if it exists. */
5506 char *command = elf_tdata (abfd)->core_command;
5507 int n = strlen (command);
5509 if (0 < n && command[n - 1] == ' ')
5510 command[n - 1] = '\0';
5513 return TRUE;
5516 /* Very simple linked list structure for recording apuinfo values. */
5517 typedef struct apuinfo_list
5519 struct apuinfo_list *next;
5520 unsigned long value;
5522 apuinfo_list;
5524 static apuinfo_list * head;
5526 static void apuinfo_list_init PARAMS ((void));
5527 static void apuinfo_list_add PARAMS ((unsigned long));
5528 static unsigned apuinfo_list_length PARAMS ((void));
5529 static unsigned long apuinfo_list_element PARAMS ((unsigned long));
5530 static void apuinfo_list_finish PARAMS ((void));
5532 extern void ppc_elf_begin_write_processing
5533 PARAMS ((bfd *, struct bfd_link_info *));
5534 extern void ppc_elf_final_write_processing
5535 PARAMS ((bfd *, bfd_boolean));
5536 extern bfd_boolean ppc_elf_write_section
5537 PARAMS ((bfd *, asection *, bfd_byte *));
5540 static void
5541 apuinfo_list_init PARAMS ((void))
5543 head = NULL;
5546 static void
5547 apuinfo_list_add (value)
5548 unsigned long value;
5550 apuinfo_list *entry = head;
5552 while (entry != NULL)
5554 if (entry->value == value)
5555 return;
5556 entry = entry->next;
5559 entry = bfd_malloc (sizeof (* entry));
5560 if (entry == NULL)
5561 return;
5563 entry->value = value;
5564 entry->next = head;
5565 head = entry;
5568 static unsigned
5569 apuinfo_list_length PARAMS ((void))
5571 apuinfo_list *entry;
5572 unsigned long count;
5574 for (entry = head, count = 0;
5575 entry;
5576 entry = entry->next)
5577 ++ count;
5579 return count;
5582 static inline unsigned long
5583 apuinfo_list_element (number)
5584 unsigned long number;
5586 apuinfo_list * entry;
5588 for (entry = head;
5589 entry && number --;
5590 entry = entry->next)
5593 return entry ? entry->value : 0;
5596 static void
5597 apuinfo_list_finish PARAMS ((void))
5599 apuinfo_list *entry;
5601 for (entry = head; entry;)
5603 apuinfo_list *next = entry->next;
5604 free (entry);
5605 entry = next;
5608 head = NULL;
5611 #define APUINFO_SECTION_NAME ".PPC.EMB.apuinfo"
5612 #define APUINFO_LABEL "APUinfo"
5614 /* Scan the input BFDs and create a linked list of
5615 the APUinfo values that will need to be emitted. */
5617 void
5618 ppc_elf_begin_write_processing (abfd, link_info)
5619 bfd *abfd;
5620 struct bfd_link_info *link_info;
5622 bfd *ibfd;
5623 asection *asec;
5624 char *buffer;
5625 unsigned num_input_sections;
5626 bfd_size_type output_section_size;
5627 unsigned i;
5628 unsigned num_entries;
5629 unsigned long offset;
5630 unsigned long length;
5631 const char *error_message = NULL;
5633 if (link_info == NULL)
5634 return;
5636 /* Scan the input bfds, looking for apuinfo sections. */
5637 num_input_sections = 0;
5638 output_section_size = 0;
5640 for (ibfd = link_info->input_bfds; ibfd; ibfd = ibfd->link_next)
5642 asec = bfd_get_section_by_name (ibfd, APUINFO_SECTION_NAME);
5643 if (asec)
5645 ++ num_input_sections;
5646 output_section_size += asec->_raw_size;
5650 /* We need at least one input sections
5651 in order to make merging worthwhile. */
5652 if (num_input_sections < 1)
5653 return;
5655 /* Just make sure that the output section exists as well. */
5656 asec = bfd_get_section_by_name (abfd, APUINFO_SECTION_NAME);
5657 if (asec == NULL)
5658 return;
5660 /* Allocate a buffer for the contents of the input sections. */
5661 buffer = bfd_malloc (output_section_size);
5662 if (buffer == NULL)
5663 return;
5665 offset = 0;
5666 apuinfo_list_init ();
5668 /* Read in the input sections contents. */
5669 for (ibfd = link_info->input_bfds; ibfd; ibfd = ibfd->link_next)
5671 unsigned long datum;
5672 char *ptr;
5674 asec = bfd_get_section_by_name (ibfd, APUINFO_SECTION_NAME);
5675 if (asec == NULL)
5676 continue;
5678 length = asec->_raw_size;
5679 if (length < 24)
5681 error_message = _("corrupt or empty %s section in %s");
5682 goto fail;
5685 if (bfd_seek (ibfd, asec->filepos, SEEK_SET) != 0
5686 || (bfd_bread (buffer + offset, length, ibfd) != length))
5688 error_message = _("unable to read in %s section from %s");
5689 goto fail;
5692 /* Process the contents of the section. */
5693 ptr = buffer + offset;
5694 error_message = _("corrupt %s section in %s");
5696 /* Verify the contents of the header. Note - we have to
5697 extract the values this way in order to allow for a
5698 host whose endian-ness is different from the target. */
5699 datum = bfd_get_32 (ibfd, ptr);
5700 if (datum != sizeof APUINFO_LABEL)
5701 goto fail;
5703 datum = bfd_get_32 (ibfd, ptr + 8);
5704 if (datum != 0x2)
5705 goto fail;
5707 if (strcmp (ptr + 12, APUINFO_LABEL) != 0)
5708 goto fail;
5710 /* Get the number of apuinfo entries. */
5711 datum = bfd_get_32 (ibfd, ptr + 4);
5712 if ((datum * 4 + 20) != length)
5713 goto fail;
5715 /* Make sure that we do not run off the end of the section. */
5716 if (offset + length > output_section_size)
5717 goto fail;
5719 /* Scan the apuinfo section, building a list of apuinfo numbers. */
5720 for (i = 0; i < datum; i++)
5721 apuinfo_list_add (bfd_get_32 (ibfd, ptr + 20 + (i * 4)));
5723 /* Update the offset. */
5724 offset += length;
5727 error_message = NULL;
5729 /* Compute the size of the output section. */
5730 num_entries = apuinfo_list_length ();
5731 output_section_size = 20 + num_entries * 4;
5733 asec = bfd_get_section_by_name (abfd, APUINFO_SECTION_NAME);
5735 if (! bfd_set_section_size (abfd, asec, output_section_size))
5736 ibfd = abfd,
5737 error_message = _("warning: unable to set size of %s section in %s");
5739 fail:
5740 free (buffer);
5742 if (error_message)
5743 (*_bfd_error_handler) (error_message, APUINFO_SECTION_NAME,
5744 bfd_archive_filename (ibfd));
5748 /* Prevent the output section from accumulating the input sections'
5749 contents. We have already stored this in our linked list structure. */
5751 bfd_boolean
5752 ppc_elf_write_section (abfd, asec, contents)
5753 bfd *abfd ATTRIBUTE_UNUSED;
5754 asection *asec;
5755 bfd_byte *contents ATTRIBUTE_UNUSED;
5757 return (apuinfo_list_length ()
5758 && strcmp (asec->name, APUINFO_SECTION_NAME) == 0);
5762 /* Finally we can generate the output section. */
5764 void
5765 ppc_elf_final_write_processing (abfd, linker)
5766 bfd *abfd;
5767 bfd_boolean linker ATTRIBUTE_UNUSED;
5769 bfd_byte *buffer;
5770 asection *asec;
5771 unsigned i;
5772 unsigned num_entries;
5773 bfd_size_type length;
5775 asec = bfd_get_section_by_name (abfd, APUINFO_SECTION_NAME);
5776 if (asec == NULL)
5777 return;
5779 if (apuinfo_list_length () == 0)
5780 return;
5782 length = asec->_raw_size;
5783 if (length < 20)
5784 return;
5786 buffer = bfd_malloc (length);
5787 if (buffer == NULL)
5789 (*_bfd_error_handler)
5790 (_("failed to allocate space for new APUinfo section."));
5791 return;
5794 /* Create the apuinfo header. */
5795 num_entries = apuinfo_list_length ();
5796 bfd_put_32 (abfd, sizeof APUINFO_LABEL, buffer);
5797 bfd_put_32 (abfd, num_entries, buffer + 4);
5798 bfd_put_32 (abfd, 0x2, buffer + 8);
5799 strcpy (buffer + 12, APUINFO_LABEL);
5801 length = 20;
5802 for (i = 0; i < num_entries; i++)
5804 bfd_put_32 (abfd, apuinfo_list_element (i), buffer + length);
5805 length += 4;
5808 if (length != asec->_raw_size)
5809 (*_bfd_error_handler) (_("failed to compute new APUinfo section."));
5811 if (! bfd_set_section_contents (abfd, asec, buffer, (file_ptr) 0, length))
5812 (*_bfd_error_handler) (_("failed to install new APUinfo section."));
5814 free (buffer);
5816 apuinfo_list_finish ();
5819 #define TARGET_LITTLE_SYM bfd_elf32_powerpcle_vec
5820 #define TARGET_LITTLE_NAME "elf32-powerpcle"
5821 #define TARGET_BIG_SYM bfd_elf32_powerpc_vec
5822 #define TARGET_BIG_NAME "elf32-powerpc"
5823 #define ELF_ARCH bfd_arch_powerpc
5824 #define ELF_MACHINE_CODE EM_PPC
5825 #define ELF_MAXPAGESIZE 0x10000
5826 #define elf_info_to_howto ppc_elf_info_to_howto
5828 #ifdef EM_CYGNUS_POWERPC
5829 #define ELF_MACHINE_ALT1 EM_CYGNUS_POWERPC
5830 #endif
5832 #ifdef EM_PPC_OLD
5833 #define ELF_MACHINE_ALT2 EM_PPC_OLD
5834 #endif
5836 #define elf_backend_plt_not_loaded 1
5837 #define elf_backend_got_symbol_offset 4
5838 #define elf_backend_can_gc_sections 1
5839 #define elf_backend_can_refcount 1
5840 #define elf_backend_got_header_size 12
5841 #define elf_backend_plt_header_size PLT_INITIAL_ENTRY_SIZE
5842 #define elf_backend_rela_normal 1
5844 #define bfd_elf32_bfd_merge_private_bfd_data ppc_elf_merge_private_bfd_data
5845 #define bfd_elf32_bfd_relax_section ppc_elf_relax_section
5846 #define bfd_elf32_bfd_reloc_type_lookup ppc_elf_reloc_type_lookup
5847 #define bfd_elf32_bfd_set_private_flags ppc_elf_set_private_flags
5848 #define bfd_elf32_bfd_link_hash_table_create ppc_elf_link_hash_table_create
5850 #define elf_backend_object_p ppc_elf_object_p
5851 #define elf_backend_gc_mark_hook ppc_elf_gc_mark_hook
5852 #define elf_backend_gc_sweep_hook ppc_elf_gc_sweep_hook
5853 #define elf_backend_section_from_shdr ppc_elf_section_from_shdr
5854 #define elf_backend_relocate_section ppc_elf_relocate_section
5855 #define elf_backend_create_dynamic_sections ppc_elf_create_dynamic_sections
5856 #define elf_backend_check_relocs ppc_elf_check_relocs
5857 #define elf_backend_copy_indirect_symbol ppc_elf_copy_indirect_symbol
5858 #define elf_backend_adjust_dynamic_symbol ppc_elf_adjust_dynamic_symbol
5859 #define elf_backend_add_symbol_hook ppc_elf_add_symbol_hook
5860 #define elf_backend_size_dynamic_sections ppc_elf_size_dynamic_sections
5861 #define elf_backend_finish_dynamic_symbol ppc_elf_finish_dynamic_symbol
5862 #define elf_backend_finish_dynamic_sections ppc_elf_finish_dynamic_sections
5863 #define elf_backend_fake_sections ppc_elf_fake_sections
5864 #define elf_backend_additional_program_headers ppc_elf_additional_program_headers
5865 #define elf_backend_modify_segment_map ppc_elf_modify_segment_map
5866 #define elf_backend_grok_prstatus ppc_elf_grok_prstatus
5867 #define elf_backend_grok_psinfo ppc_elf_grok_psinfo
5868 #define elf_backend_reloc_type_class ppc_elf_reloc_type_class
5869 #define elf_backend_begin_write_processing ppc_elf_begin_write_processing
5870 #define elf_backend_final_write_processing ppc_elf_final_write_processing
5871 #define elf_backend_write_section ppc_elf_write_section
5873 #include "elf32-target.h"