* elflink.c (_bfd_elf_link_create_dynamic_sections): Move from
[binutils.git] / bfd / elf32-ppc.c
blob64c2839f42170043552f794e0c45071511156e77
1 /* PowerPC-specific support for 32-bit ELF
2 Copyright 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
3 Free Software Foundation, Inc.
4 Written by Ian Lance Taylor, Cygnus Support.
6 This file is part of BFD, the Binary File Descriptor library.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the
20 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
23 /* This file is based on a preliminary PowerPC ELF ABI. The
24 information may not match the final PowerPC ELF ABI. It includes
25 suggestions from the in-progress Embedded PowerPC ABI, and that
26 information may also not match. */
28 #include "bfd.h"
29 #include "sysdep.h"
30 #include "bfdlink.h"
31 #include "libbfd.h"
32 #include "elf-bfd.h"
33 #include "elf/ppc.h"
34 #include "elf32-ppc.h"
36 /* RELA relocations are used here. */
38 static struct bfd_hash_entry *ppc_elf_link_hash_newfunc
39 PARAMS ((struct bfd_hash_entry *entry, struct bfd_hash_table *table,
40 const char *string));
41 static struct bfd_link_hash_table *ppc_elf_link_hash_table_create
42 PARAMS ((bfd *abfd));
43 static void ppc_elf_copy_indirect_symbol
44 PARAMS ((struct elf_backend_data *bed, struct elf_link_hash_entry *dir,
45 struct elf_link_hash_entry *ind));
46 static reloc_howto_type *ppc_elf_reloc_type_lookup
47 PARAMS ((bfd *abfd, bfd_reloc_code_real_type code));
48 static void ppc_elf_info_to_howto
49 PARAMS ((bfd *abfd, arelent *cache_ptr, Elf_Internal_Rela *dst));
50 static void ppc_elf_howto_init
51 PARAMS ((void));
52 static int ppc_elf_sort_rela
53 PARAMS ((const PTR, const PTR));
54 static bfd_boolean ppc_elf_relax_section
55 PARAMS ((bfd *, asection *, struct bfd_link_info *, bfd_boolean *));
56 static bfd_reloc_status_type ppc_elf_addr16_ha_reloc
57 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
58 static bfd_reloc_status_type ppc_elf_unhandled_reloc
59 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
60 static bfd_boolean ppc_elf_object_p
61 PARAMS ((bfd *));
62 static bfd_boolean ppc_elf_set_private_flags
63 PARAMS ((bfd *, flagword));
64 static bfd_boolean ppc_elf_merge_private_bfd_data
65 PARAMS ((bfd *, bfd *));
66 static int ppc_elf_additional_program_headers
67 PARAMS ((bfd *));
68 static bfd_boolean ppc_elf_modify_segment_map
69 PARAMS ((bfd *));
70 static bfd_boolean ppc_elf_create_got
71 PARAMS ((bfd *, struct bfd_link_info *));
72 static bfd_boolean ppc_elf_create_dynamic_sections
73 PARAMS ((bfd *, struct bfd_link_info *));
74 static bfd_boolean ppc_elf_section_from_shdr
75 PARAMS ((bfd *, Elf_Internal_Shdr *, const char *));
76 static bfd_boolean ppc_elf_fake_sections
77 PARAMS ((bfd *, Elf_Internal_Shdr *, asection *));
78 static elf_linker_section_t *ppc_elf_create_linker_section
79 PARAMS ((bfd *abfd, struct bfd_link_info *info,
80 enum elf_linker_section_enum));
81 static bfd_boolean update_local_sym_info
82 PARAMS ((bfd *, Elf_Internal_Shdr *, unsigned long, int));
83 static void bad_shared_reloc
84 PARAMS ((bfd *, enum elf_ppc_reloc_type));
85 static bfd_boolean ppc_elf_check_relocs
86 PARAMS ((bfd *, struct bfd_link_info *, asection *,
87 const Elf_Internal_Rela *));
88 static asection *ppc_elf_gc_mark_hook
89 PARAMS ((asection *sec, struct bfd_link_info *info, Elf_Internal_Rela *rel,
90 struct elf_link_hash_entry *h, Elf_Internal_Sym *sym));
91 static bfd_boolean ppc_elf_gc_sweep_hook
92 PARAMS ((bfd *abfd, struct bfd_link_info *info, asection *sec,
93 const Elf_Internal_Rela *relocs));
94 static bfd_boolean ppc_elf_adjust_dynamic_symbol
95 PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *));
96 static bfd_boolean allocate_dynrelocs
97 PARAMS ((struct elf_link_hash_entry *, PTR));
98 static bfd_boolean readonly_dynrelocs
99 PARAMS ((struct elf_link_hash_entry *, PTR));
100 static bfd_boolean ppc_elf_size_dynamic_sections
101 PARAMS ((bfd *, struct bfd_link_info *));
102 static bfd_boolean ppc_elf_relocate_section
103 PARAMS ((bfd *, struct bfd_link_info *info, bfd *, asection *, bfd_byte *,
104 Elf_Internal_Rela *relocs, Elf_Internal_Sym *local_syms,
105 asection **));
106 static bfd_boolean ppc_elf_add_symbol_hook
107 PARAMS ((bfd *, struct bfd_link_info *, const Elf_Internal_Sym *,
108 const char **, flagword *, asection **, bfd_vma *));
109 static bfd_boolean ppc_elf_finish_dynamic_symbol
110 PARAMS ((bfd *, struct bfd_link_info *, struct elf_link_hash_entry *,
111 Elf_Internal_Sym *));
112 static bfd_boolean ppc_elf_finish_dynamic_sections
113 PARAMS ((bfd *, struct bfd_link_info *));
114 static enum elf_reloc_type_class ppc_elf_reloc_type_class
115 PARAMS ((const Elf_Internal_Rela *));
116 static bfd_boolean ppc_elf_grok_prstatus
117 PARAMS ((bfd *abfd, Elf_Internal_Note *note));
118 static bfd_boolean ppc_elf_grok_psinfo
119 PARAMS ((bfd *abfd, Elf_Internal_Note *note));
121 /* Branch prediction bit for branch taken relocs. */
122 #define BRANCH_PREDICT_BIT 0x200000
123 /* Mask to set RA in memory instructions. */
124 #define RA_REGISTER_MASK 0x001f0000
125 /* Value to shift register by to insert RA. */
126 #define RA_REGISTER_SHIFT 16
128 /* The name of the dynamic interpreter. This is put in the .interp
129 section. */
130 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
132 /* The size in bytes of an entry in the procedure linkage table. */
133 #define PLT_ENTRY_SIZE 12
134 /* The initial size of the plt reserved for the dynamic linker. */
135 #define PLT_INITIAL_ENTRY_SIZE 72
136 /* The size of the gap between entries in the PLT. */
137 #define PLT_SLOT_SIZE 8
138 /* The number of single-slot PLT entries (the rest use two slots). */
139 #define PLT_NUM_SINGLE_ENTRIES 8192
141 /* Some nop instructions. */
142 #define NOP 0x60000000
143 #define CROR_151515 0x4def7b82
144 #define CROR_313131 0x4ffffb82
146 /* Offset of tp and dtp pointers from start of TLS block. */
147 #define TP_OFFSET 0x7000
148 #define DTP_OFFSET 0x8000
150 /* Will references to this symbol always reference the symbol
151 in this object? STV_PROTECTED is excluded from the visibility test
152 here so that function pointer comparisons work properly. Since
153 function symbols not defined in an app are set to their .plt entry,
154 it's necessary for shared libs to also reference the .plt even
155 though the symbol is really local to the shared lib. */
156 #define SYMBOL_REFERENCES_LOCAL(INFO, H) \
157 ((! INFO->shared \
158 || INFO->symbolic \
159 || H->dynindx == -1 \
160 || ELF_ST_VISIBILITY (H->other) == STV_INTERNAL \
161 || ELF_ST_VISIBILITY (H->other) == STV_HIDDEN) \
162 && (H->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0)
164 /* Will _calls_ to this symbol always call the version in this object? */
165 #define SYMBOL_CALLS_LOCAL(INFO, H) \
166 ((! INFO->shared \
167 || INFO->symbolic \
168 || H->dynindx == -1 \
169 || ELF_ST_VISIBILITY (H->other) != STV_DEFAULT) \
170 && (H->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0)
172 /* The PPC linker needs to keep track of the number of relocs that it
173 decides to copy as dynamic relocs in check_relocs for each symbol.
174 This is so that it can later discard them if they are found to be
175 unnecessary. We store the information in a field extending the
176 regular ELF linker hash table. */
178 struct ppc_elf_dyn_relocs
180 struct ppc_elf_dyn_relocs *next;
182 /* The input section of the reloc. */
183 asection *sec;
185 /* Total number of relocs copied for the input section. */
186 bfd_size_type count;
188 /* Number of pc-relative relocs copied for the input section. */
189 bfd_size_type pc_count;
192 /* PPC ELF linker hash entry. */
194 struct ppc_elf_link_hash_entry
196 struct elf_link_hash_entry elf;
198 /* Track dynamic relocs copied for this symbol. */
199 struct ppc_elf_dyn_relocs *dyn_relocs;
201 /* Contexts in which symbol is used in the GOT (or TOC).
202 TLS_GD .. TLS_TLS bits are or'd into the mask as the
203 corresponding relocs are encountered during check_relocs.
204 tls_optimize clears TLS_GD .. TLS_TPREL when optimizing to
205 indicate the corresponding GOT entry type is not needed. */
206 #define TLS_GD 1 /* GD reloc. */
207 #define TLS_LD 2 /* LD reloc. */
208 #define TLS_TPREL 4 /* TPREL reloc, => IE. */
209 #define TLS_DTPREL 8 /* DTPREL reloc, => LD. */
210 #define TLS_TLS 16 /* Any TLS reloc. */
211 #define TLS_TPRELGD 32 /* TPREL reloc resulting from GD->IE. */
212 char tls_mask;
215 #define ppc_elf_hash_entry(ent) ((struct ppc_elf_link_hash_entry *) (ent))
217 /* PPC ELF linker hash table. */
219 struct ppc_elf_link_hash_table
221 struct elf_link_hash_table elf;
223 /* Short-cuts to get to dynamic linker sections. */
224 asection *got;
225 asection *relgot;
226 asection *plt;
227 asection *relplt;
228 asection *dynbss;
229 asection *relbss;
230 asection *dynsbss;
231 asection *relsbss;
232 elf_linker_section_t *sdata;
233 elf_linker_section_t *sdata2;
235 /* Short-cut to first output tls section. */
236 asection *tls_sec;
238 /* Shortcut to .__tls_get_addr. */
239 struct elf_link_hash_entry *tls_get_addr;
241 /* TLS local dynamic got entry handling. */
242 union {
243 bfd_signed_vma refcount;
244 bfd_vma offset;
245 } tlsld_got;
247 /* Small local sym to section mapping cache. */
248 struct sym_sec_cache sym_sec;
251 /* Get the PPC ELF linker hash table from a link_info structure. */
253 #define ppc_elf_hash_table(p) \
254 ((struct ppc_elf_link_hash_table *) (p)->hash)
256 /* Create an entry in a PPC ELF linker hash table. */
258 static struct bfd_hash_entry *
259 ppc_elf_link_hash_newfunc (entry, table, string)
260 struct bfd_hash_entry *entry;
261 struct bfd_hash_table *table;
262 const char *string;
264 /* Allocate the structure if it has not already been allocated by a
265 subclass. */
266 if (entry == NULL)
268 entry = bfd_hash_allocate (table,
269 sizeof (struct ppc_elf_link_hash_entry));
270 if (entry == NULL)
271 return entry;
274 /* Call the allocation method of the superclass. */
275 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
276 if (entry != NULL)
278 ppc_elf_hash_entry (entry)->dyn_relocs = NULL;
279 ppc_elf_hash_entry (entry)->tls_mask = 0;
282 return entry;
285 /* Create a PPC ELF linker hash table. */
287 static struct bfd_link_hash_table *
288 ppc_elf_link_hash_table_create (abfd)
289 bfd *abfd;
291 struct ppc_elf_link_hash_table *ret;
293 ret = ((struct ppc_elf_link_hash_table *)
294 bfd_malloc (sizeof (struct ppc_elf_link_hash_table)));
295 if (ret == NULL)
296 return NULL;
298 if (! _bfd_elf_link_hash_table_init (&ret->elf, abfd,
299 ppc_elf_link_hash_newfunc))
301 free (ret);
302 return NULL;
305 ret->got = NULL;
306 ret->relgot = NULL;
307 ret->plt = NULL;
308 ret->relplt = NULL;
309 ret->dynbss = NULL;
310 ret->relbss = NULL;
311 ret->dynsbss = NULL;
312 ret->relsbss = NULL;
313 ret->sdata = NULL;
314 ret->sdata2 = NULL;
315 ret->tls_sec = NULL;
316 ret->tls_get_addr = NULL;
317 ret->tlsld_got.refcount = 0;
318 ret->sym_sec.abfd = NULL;
320 return &ret->elf.root;
323 /* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
324 copying dynamic variables from a shared lib into an app's dynbss
325 section, and instead use a dynamic relocation to point into the
326 shared lib. */
327 #define ELIMINATE_COPY_RELOCS 1
329 /* Copy the extra info we tack onto an elf_link_hash_entry. */
331 static void
332 ppc_elf_copy_indirect_symbol (bed, dir, ind)
333 struct elf_backend_data *bed;
334 struct elf_link_hash_entry *dir, *ind;
336 struct ppc_elf_link_hash_entry *edir, *eind;
338 edir = (struct ppc_elf_link_hash_entry *) dir;
339 eind = (struct ppc_elf_link_hash_entry *) ind;
341 if (eind->dyn_relocs != NULL)
343 if (edir->dyn_relocs != NULL)
345 struct ppc_elf_dyn_relocs **pp;
346 struct ppc_elf_dyn_relocs *p;
348 if (ind->root.type == bfd_link_hash_indirect)
349 abort ();
351 /* Add reloc counts against the weak sym to the strong sym
352 list. Merge any entries against the same section. */
353 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
355 struct ppc_elf_dyn_relocs *q;
357 for (q = edir->dyn_relocs; q != NULL; q = q->next)
358 if (q->sec == p->sec)
360 q->pc_count += p->pc_count;
361 q->count += p->count;
362 *pp = p->next;
363 break;
365 if (q == NULL)
366 pp = &p->next;
368 *pp = edir->dyn_relocs;
371 edir->dyn_relocs = eind->dyn_relocs;
372 eind->dyn_relocs = NULL;
375 edir->tls_mask |= eind->tls_mask;
377 if (ELIMINATE_COPY_RELOCS
378 && ind->root.type != bfd_link_hash_indirect
379 && (dir->elf_link_hash_flags & ELF_LINK_HASH_DYNAMIC_ADJUSTED) != 0)
380 /* If called to transfer flags for a weakdef during processing
381 of elf_adjust_dynamic_symbol, don't copy ELF_LINK_NON_GOT_REF.
382 We clear it ourselves for ELIMINATE_COPY_RELOCS. */
383 dir->elf_link_hash_flags |=
384 (ind->elf_link_hash_flags & (ELF_LINK_HASH_REF_DYNAMIC
385 | ELF_LINK_HASH_REF_REGULAR
386 | ELF_LINK_HASH_REF_REGULAR_NONWEAK));
387 else
388 _bfd_elf_link_hash_copy_indirect (bed, dir, ind);
391 static reloc_howto_type *ppc_elf_howto_table[(int) R_PPC_max];
393 static reloc_howto_type ppc_elf_howto_raw[] = {
394 /* This reloc does nothing. */
395 HOWTO (R_PPC_NONE, /* type */
396 0, /* rightshift */
397 2, /* size (0 = byte, 1 = short, 2 = long) */
398 32, /* bitsize */
399 FALSE, /* pc_relative */
400 0, /* bitpos */
401 complain_overflow_bitfield, /* complain_on_overflow */
402 bfd_elf_generic_reloc, /* special_function */
403 "R_PPC_NONE", /* name */
404 FALSE, /* partial_inplace */
405 0, /* src_mask */
406 0, /* dst_mask */
407 FALSE), /* pcrel_offset */
409 /* A standard 32 bit relocation. */
410 HOWTO (R_PPC_ADDR32, /* type */
411 0, /* rightshift */
412 2, /* size (0 = byte, 1 = short, 2 = long) */
413 32, /* bitsize */
414 FALSE, /* pc_relative */
415 0, /* bitpos */
416 complain_overflow_bitfield, /* complain_on_overflow */
417 bfd_elf_generic_reloc, /* special_function */
418 "R_PPC_ADDR32", /* name */
419 FALSE, /* partial_inplace */
420 0, /* src_mask */
421 0xffffffff, /* dst_mask */
422 FALSE), /* pcrel_offset */
424 /* An absolute 26 bit branch; the lower two bits must be zero.
425 FIXME: we don't check that, we just clear them. */
426 HOWTO (R_PPC_ADDR24, /* type */
427 0, /* rightshift */
428 2, /* size (0 = byte, 1 = short, 2 = long) */
429 26, /* bitsize */
430 FALSE, /* pc_relative */
431 0, /* bitpos */
432 complain_overflow_bitfield, /* complain_on_overflow */
433 bfd_elf_generic_reloc, /* special_function */
434 "R_PPC_ADDR24", /* name */
435 FALSE, /* partial_inplace */
436 0, /* src_mask */
437 0x3fffffc, /* dst_mask */
438 FALSE), /* pcrel_offset */
440 /* A standard 16 bit relocation. */
441 HOWTO (R_PPC_ADDR16, /* type */
442 0, /* rightshift */
443 1, /* size (0 = byte, 1 = short, 2 = long) */
444 16, /* bitsize */
445 FALSE, /* pc_relative */
446 0, /* bitpos */
447 complain_overflow_bitfield, /* complain_on_overflow */
448 bfd_elf_generic_reloc, /* special_function */
449 "R_PPC_ADDR16", /* name */
450 FALSE, /* partial_inplace */
451 0, /* src_mask */
452 0xffff, /* dst_mask */
453 FALSE), /* pcrel_offset */
455 /* A 16 bit relocation without overflow. */
456 HOWTO (R_PPC_ADDR16_LO, /* type */
457 0, /* rightshift */
458 1, /* size (0 = byte, 1 = short, 2 = long) */
459 16, /* bitsize */
460 FALSE, /* pc_relative */
461 0, /* bitpos */
462 complain_overflow_dont,/* complain_on_overflow */
463 bfd_elf_generic_reloc, /* special_function */
464 "R_PPC_ADDR16_LO", /* name */
465 FALSE, /* partial_inplace */
466 0, /* src_mask */
467 0xffff, /* dst_mask */
468 FALSE), /* pcrel_offset */
470 /* The high order 16 bits of an address. */
471 HOWTO (R_PPC_ADDR16_HI, /* type */
472 16, /* rightshift */
473 1, /* size (0 = byte, 1 = short, 2 = long) */
474 16, /* bitsize */
475 FALSE, /* pc_relative */
476 0, /* bitpos */
477 complain_overflow_dont, /* complain_on_overflow */
478 bfd_elf_generic_reloc, /* special_function */
479 "R_PPC_ADDR16_HI", /* name */
480 FALSE, /* partial_inplace */
481 0, /* src_mask */
482 0xffff, /* dst_mask */
483 FALSE), /* pcrel_offset */
485 /* The high order 16 bits of an address, plus 1 if the contents of
486 the low 16 bits, treated as a signed number, is negative. */
487 HOWTO (R_PPC_ADDR16_HA, /* type */
488 16, /* rightshift */
489 1, /* size (0 = byte, 1 = short, 2 = long) */
490 16, /* bitsize */
491 FALSE, /* pc_relative */
492 0, /* bitpos */
493 complain_overflow_dont, /* complain_on_overflow */
494 ppc_elf_addr16_ha_reloc, /* special_function */
495 "R_PPC_ADDR16_HA", /* name */
496 FALSE, /* partial_inplace */
497 0, /* src_mask */
498 0xffff, /* dst_mask */
499 FALSE), /* pcrel_offset */
501 /* An absolute 16 bit branch; the lower two bits must be zero.
502 FIXME: we don't check that, we just clear them. */
503 HOWTO (R_PPC_ADDR14, /* type */
504 0, /* rightshift */
505 2, /* size (0 = byte, 1 = short, 2 = long) */
506 16, /* bitsize */
507 FALSE, /* pc_relative */
508 0, /* bitpos */
509 complain_overflow_bitfield, /* complain_on_overflow */
510 bfd_elf_generic_reloc, /* special_function */
511 "R_PPC_ADDR14", /* name */
512 FALSE, /* partial_inplace */
513 0, /* src_mask */
514 0xfffc, /* dst_mask */
515 FALSE), /* pcrel_offset */
517 /* An absolute 16 bit branch, for which bit 10 should be set to
518 indicate that the branch is expected to be taken. The lower two
519 bits must be zero. */
520 HOWTO (R_PPC_ADDR14_BRTAKEN, /* type */
521 0, /* rightshift */
522 2, /* size (0 = byte, 1 = short, 2 = long) */
523 16, /* bitsize */
524 FALSE, /* pc_relative */
525 0, /* bitpos */
526 complain_overflow_bitfield, /* complain_on_overflow */
527 bfd_elf_generic_reloc, /* special_function */
528 "R_PPC_ADDR14_BRTAKEN",/* name */
529 FALSE, /* partial_inplace */
530 0, /* src_mask */
531 0xfffc, /* dst_mask */
532 FALSE), /* pcrel_offset */
534 /* An absolute 16 bit branch, for which bit 10 should be set to
535 indicate that the branch is not expected to be taken. The lower
536 two bits must be zero. */
537 HOWTO (R_PPC_ADDR14_BRNTAKEN, /* type */
538 0, /* rightshift */
539 2, /* size (0 = byte, 1 = short, 2 = long) */
540 16, /* bitsize */
541 FALSE, /* pc_relative */
542 0, /* bitpos */
543 complain_overflow_bitfield, /* complain_on_overflow */
544 bfd_elf_generic_reloc, /* special_function */
545 "R_PPC_ADDR14_BRNTAKEN",/* name */
546 FALSE, /* partial_inplace */
547 0, /* src_mask */
548 0xfffc, /* dst_mask */
549 FALSE), /* pcrel_offset */
551 /* A relative 26 bit branch; the lower two bits must be zero. */
552 HOWTO (R_PPC_REL24, /* type */
553 0, /* rightshift */
554 2, /* size (0 = byte, 1 = short, 2 = long) */
555 26, /* bitsize */
556 TRUE, /* pc_relative */
557 0, /* bitpos */
558 complain_overflow_signed, /* complain_on_overflow */
559 bfd_elf_generic_reloc, /* special_function */
560 "R_PPC_REL24", /* name */
561 FALSE, /* partial_inplace */
562 0, /* src_mask */
563 0x3fffffc, /* dst_mask */
564 TRUE), /* pcrel_offset */
566 /* A relative 16 bit branch; the lower two bits must be zero. */
567 HOWTO (R_PPC_REL14, /* type */
568 0, /* rightshift */
569 2, /* size (0 = byte, 1 = short, 2 = long) */
570 16, /* bitsize */
571 TRUE, /* pc_relative */
572 0, /* bitpos */
573 complain_overflow_signed, /* complain_on_overflow */
574 bfd_elf_generic_reloc, /* special_function */
575 "R_PPC_REL14", /* name */
576 FALSE, /* partial_inplace */
577 0, /* src_mask */
578 0xfffc, /* dst_mask */
579 TRUE), /* pcrel_offset */
581 /* A relative 16 bit branch. Bit 10 should be set to indicate that
582 the branch is expected to be taken. The lower two bits must be
583 zero. */
584 HOWTO (R_PPC_REL14_BRTAKEN, /* type */
585 0, /* rightshift */
586 2, /* size (0 = byte, 1 = short, 2 = long) */
587 16, /* bitsize */
588 TRUE, /* pc_relative */
589 0, /* bitpos */
590 complain_overflow_signed, /* complain_on_overflow */
591 bfd_elf_generic_reloc, /* special_function */
592 "R_PPC_REL14_BRTAKEN", /* name */
593 FALSE, /* partial_inplace */
594 0, /* src_mask */
595 0xfffc, /* dst_mask */
596 TRUE), /* pcrel_offset */
598 /* A relative 16 bit branch. Bit 10 should be set to indicate that
599 the branch is not expected to be taken. The lower two bits must
600 be zero. */
601 HOWTO (R_PPC_REL14_BRNTAKEN, /* type */
602 0, /* rightshift */
603 2, /* size (0 = byte, 1 = short, 2 = long) */
604 16, /* bitsize */
605 TRUE, /* pc_relative */
606 0, /* bitpos */
607 complain_overflow_signed, /* complain_on_overflow */
608 bfd_elf_generic_reloc, /* special_function */
609 "R_PPC_REL14_BRNTAKEN",/* name */
610 FALSE, /* partial_inplace */
611 0, /* src_mask */
612 0xfffc, /* dst_mask */
613 TRUE), /* pcrel_offset */
615 /* Like R_PPC_ADDR16, but referring to the GOT table entry for the
616 symbol. */
617 HOWTO (R_PPC_GOT16, /* type */
618 0, /* rightshift */
619 1, /* size (0 = byte, 1 = short, 2 = long) */
620 16, /* bitsize */
621 FALSE, /* pc_relative */
622 0, /* bitpos */
623 complain_overflow_signed, /* complain_on_overflow */
624 bfd_elf_generic_reloc, /* special_function */
625 "R_PPC_GOT16", /* name */
626 FALSE, /* partial_inplace */
627 0, /* src_mask */
628 0xffff, /* dst_mask */
629 FALSE), /* pcrel_offset */
631 /* Like R_PPC_ADDR16_LO, but referring to the GOT table entry for
632 the symbol. */
633 HOWTO (R_PPC_GOT16_LO, /* type */
634 0, /* rightshift */
635 1, /* size (0 = byte, 1 = short, 2 = long) */
636 16, /* bitsize */
637 FALSE, /* pc_relative */
638 0, /* bitpos */
639 complain_overflow_dont, /* complain_on_overflow */
640 bfd_elf_generic_reloc, /* special_function */
641 "R_PPC_GOT16_LO", /* name */
642 FALSE, /* partial_inplace */
643 0, /* src_mask */
644 0xffff, /* dst_mask */
645 FALSE), /* pcrel_offset */
647 /* Like R_PPC_ADDR16_HI, but referring to the GOT table entry for
648 the symbol. */
649 HOWTO (R_PPC_GOT16_HI, /* type */
650 16, /* rightshift */
651 1, /* size (0 = byte, 1 = short, 2 = long) */
652 16, /* bitsize */
653 FALSE, /* pc_relative */
654 0, /* bitpos */
655 complain_overflow_bitfield, /* complain_on_overflow */
656 bfd_elf_generic_reloc, /* special_function */
657 "R_PPC_GOT16_HI", /* name */
658 FALSE, /* partial_inplace */
659 0, /* src_mask */
660 0xffff, /* dst_mask */
661 FALSE), /* pcrel_offset */
663 /* Like R_PPC_ADDR16_HA, but referring to the GOT table entry for
664 the symbol. */
665 HOWTO (R_PPC_GOT16_HA, /* type */
666 16, /* rightshift */
667 1, /* size (0 = byte, 1 = short, 2 = long) */
668 16, /* bitsize */
669 FALSE, /* pc_relative */
670 0, /* bitpos */
671 complain_overflow_bitfield, /* complain_on_overflow */
672 ppc_elf_addr16_ha_reloc, /* special_function */
673 "R_PPC_GOT16_HA", /* name */
674 FALSE, /* partial_inplace */
675 0, /* src_mask */
676 0xffff, /* dst_mask */
677 FALSE), /* pcrel_offset */
679 /* Like R_PPC_REL24, but referring to the procedure linkage table
680 entry for the symbol. */
681 HOWTO (R_PPC_PLTREL24, /* type */
682 0, /* rightshift */
683 2, /* size (0 = byte, 1 = short, 2 = long) */
684 26, /* bitsize */
685 TRUE, /* pc_relative */
686 0, /* bitpos */
687 complain_overflow_signed, /* complain_on_overflow */
688 bfd_elf_generic_reloc, /* special_function */
689 "R_PPC_PLTREL24", /* name */
690 FALSE, /* partial_inplace */
691 0, /* src_mask */
692 0x3fffffc, /* dst_mask */
693 TRUE), /* pcrel_offset */
695 /* This is used only by the dynamic linker. The symbol should exist
696 both in the object being run and in some shared library. The
697 dynamic linker copies the data addressed by the symbol from the
698 shared library into the object, because the object being
699 run has to have the data at some particular address. */
700 HOWTO (R_PPC_COPY, /* type */
701 0, /* rightshift */
702 2, /* size (0 = byte, 1 = short, 2 = long) */
703 32, /* bitsize */
704 FALSE, /* pc_relative */
705 0, /* bitpos */
706 complain_overflow_bitfield, /* complain_on_overflow */
707 bfd_elf_generic_reloc, /* special_function */
708 "R_PPC_COPY", /* name */
709 FALSE, /* partial_inplace */
710 0, /* src_mask */
711 0, /* dst_mask */
712 FALSE), /* pcrel_offset */
714 /* Like R_PPC_ADDR32, but used when setting global offset table
715 entries. */
716 HOWTO (R_PPC_GLOB_DAT, /* type */
717 0, /* rightshift */
718 2, /* size (0 = byte, 1 = short, 2 = long) */
719 32, /* bitsize */
720 FALSE, /* pc_relative */
721 0, /* bitpos */
722 complain_overflow_bitfield, /* complain_on_overflow */
723 bfd_elf_generic_reloc, /* special_function */
724 "R_PPC_GLOB_DAT", /* name */
725 FALSE, /* partial_inplace */
726 0, /* src_mask */
727 0xffffffff, /* dst_mask */
728 FALSE), /* pcrel_offset */
730 /* Marks a procedure linkage table entry for a symbol. */
731 HOWTO (R_PPC_JMP_SLOT, /* type */
732 0, /* rightshift */
733 2, /* size (0 = byte, 1 = short, 2 = long) */
734 32, /* bitsize */
735 FALSE, /* pc_relative */
736 0, /* bitpos */
737 complain_overflow_bitfield, /* complain_on_overflow */
738 bfd_elf_generic_reloc, /* special_function */
739 "R_PPC_JMP_SLOT", /* name */
740 FALSE, /* partial_inplace */
741 0, /* src_mask */
742 0, /* dst_mask */
743 FALSE), /* pcrel_offset */
745 /* Used only by the dynamic linker. When the object is run, this
746 longword is set to the load address of the object, plus the
747 addend. */
748 HOWTO (R_PPC_RELATIVE, /* type */
749 0, /* rightshift */
750 2, /* size (0 = byte, 1 = short, 2 = long) */
751 32, /* bitsize */
752 FALSE, /* pc_relative */
753 0, /* bitpos */
754 complain_overflow_bitfield, /* complain_on_overflow */
755 bfd_elf_generic_reloc, /* special_function */
756 "R_PPC_RELATIVE", /* name */
757 FALSE, /* partial_inplace */
758 0, /* src_mask */
759 0xffffffff, /* dst_mask */
760 FALSE), /* pcrel_offset */
762 /* Like R_PPC_REL24, but uses the value of the symbol within the
763 object rather than the final value. Normally used for
764 _GLOBAL_OFFSET_TABLE_. */
765 HOWTO (R_PPC_LOCAL24PC, /* type */
766 0, /* rightshift */
767 2, /* size (0 = byte, 1 = short, 2 = long) */
768 26, /* bitsize */
769 TRUE, /* pc_relative */
770 0, /* bitpos */
771 complain_overflow_signed, /* complain_on_overflow */
772 bfd_elf_generic_reloc, /* special_function */
773 "R_PPC_LOCAL24PC", /* name */
774 FALSE, /* partial_inplace */
775 0, /* src_mask */
776 0x3fffffc, /* dst_mask */
777 TRUE), /* pcrel_offset */
779 /* Like R_PPC_ADDR32, but may be unaligned. */
780 HOWTO (R_PPC_UADDR32, /* type */
781 0, /* rightshift */
782 2, /* size (0 = byte, 1 = short, 2 = long) */
783 32, /* bitsize */
784 FALSE, /* pc_relative */
785 0, /* bitpos */
786 complain_overflow_bitfield, /* complain_on_overflow */
787 bfd_elf_generic_reloc, /* special_function */
788 "R_PPC_UADDR32", /* name */
789 FALSE, /* partial_inplace */
790 0, /* src_mask */
791 0xffffffff, /* dst_mask */
792 FALSE), /* pcrel_offset */
794 /* Like R_PPC_ADDR16, but may be unaligned. */
795 HOWTO (R_PPC_UADDR16, /* type */
796 0, /* rightshift */
797 1, /* size (0 = byte, 1 = short, 2 = long) */
798 16, /* bitsize */
799 FALSE, /* pc_relative */
800 0, /* bitpos */
801 complain_overflow_bitfield, /* complain_on_overflow */
802 bfd_elf_generic_reloc, /* special_function */
803 "R_PPC_UADDR16", /* name */
804 FALSE, /* partial_inplace */
805 0, /* src_mask */
806 0xffff, /* dst_mask */
807 FALSE), /* pcrel_offset */
809 /* 32-bit PC relative */
810 HOWTO (R_PPC_REL32, /* type */
811 0, /* rightshift */
812 2, /* size (0 = byte, 1 = short, 2 = long) */
813 32, /* bitsize */
814 TRUE, /* pc_relative */
815 0, /* bitpos */
816 complain_overflow_bitfield, /* complain_on_overflow */
817 bfd_elf_generic_reloc, /* special_function */
818 "R_PPC_REL32", /* name */
819 FALSE, /* partial_inplace */
820 0, /* src_mask */
821 0xffffffff, /* dst_mask */
822 TRUE), /* pcrel_offset */
824 /* 32-bit relocation to the symbol's procedure linkage table.
825 FIXME: not supported. */
826 HOWTO (R_PPC_PLT32, /* type */
827 0, /* rightshift */
828 2, /* size (0 = byte, 1 = short, 2 = long) */
829 32, /* bitsize */
830 FALSE, /* pc_relative */
831 0, /* bitpos */
832 complain_overflow_bitfield, /* complain_on_overflow */
833 bfd_elf_generic_reloc, /* special_function */
834 "R_PPC_PLT32", /* name */
835 FALSE, /* partial_inplace */
836 0, /* src_mask */
837 0, /* dst_mask */
838 FALSE), /* pcrel_offset */
840 /* 32-bit PC relative relocation to the symbol's procedure linkage table.
841 FIXME: not supported. */
842 HOWTO (R_PPC_PLTREL32, /* type */
843 0, /* rightshift */
844 2, /* size (0 = byte, 1 = short, 2 = long) */
845 32, /* bitsize */
846 TRUE, /* pc_relative */
847 0, /* bitpos */
848 complain_overflow_bitfield, /* complain_on_overflow */
849 bfd_elf_generic_reloc, /* special_function */
850 "R_PPC_PLTREL32", /* name */
851 FALSE, /* partial_inplace */
852 0, /* src_mask */
853 0, /* dst_mask */
854 TRUE), /* pcrel_offset */
856 /* Like R_PPC_ADDR16_LO, but referring to the PLT table entry for
857 the symbol. */
858 HOWTO (R_PPC_PLT16_LO, /* type */
859 0, /* rightshift */
860 1, /* size (0 = byte, 1 = short, 2 = long) */
861 16, /* bitsize */
862 FALSE, /* pc_relative */
863 0, /* bitpos */
864 complain_overflow_dont, /* complain_on_overflow */
865 bfd_elf_generic_reloc, /* special_function */
866 "R_PPC_PLT16_LO", /* name */
867 FALSE, /* partial_inplace */
868 0, /* src_mask */
869 0xffff, /* dst_mask */
870 FALSE), /* pcrel_offset */
872 /* Like R_PPC_ADDR16_HI, but referring to the PLT table entry for
873 the symbol. */
874 HOWTO (R_PPC_PLT16_HI, /* type */
875 16, /* rightshift */
876 1, /* size (0 = byte, 1 = short, 2 = long) */
877 16, /* bitsize */
878 FALSE, /* pc_relative */
879 0, /* bitpos */
880 complain_overflow_bitfield, /* complain_on_overflow */
881 bfd_elf_generic_reloc, /* special_function */
882 "R_PPC_PLT16_HI", /* name */
883 FALSE, /* partial_inplace */
884 0, /* src_mask */
885 0xffff, /* dst_mask */
886 FALSE), /* pcrel_offset */
888 /* Like R_PPC_ADDR16_HA, but referring to the PLT table entry for
889 the symbol. */
890 HOWTO (R_PPC_PLT16_HA, /* type */
891 16, /* rightshift */
892 1, /* size (0 = byte, 1 = short, 2 = long) */
893 16, /* bitsize */
894 FALSE, /* pc_relative */
895 0, /* bitpos */
896 complain_overflow_bitfield, /* complain_on_overflow */
897 ppc_elf_addr16_ha_reloc, /* special_function */
898 "R_PPC_PLT16_HA", /* name */
899 FALSE, /* partial_inplace */
900 0, /* src_mask */
901 0xffff, /* dst_mask */
902 FALSE), /* pcrel_offset */
904 /* A sign-extended 16 bit value relative to _SDA_BASE_, for use with
905 small data items. */
906 HOWTO (R_PPC_SDAREL16, /* type */
907 0, /* rightshift */
908 1, /* size (0 = byte, 1 = short, 2 = long) */
909 16, /* bitsize */
910 FALSE, /* pc_relative */
911 0, /* bitpos */
912 complain_overflow_signed, /* complain_on_overflow */
913 bfd_elf_generic_reloc, /* special_function */
914 "R_PPC_SDAREL16", /* name */
915 FALSE, /* partial_inplace */
916 0, /* src_mask */
917 0xffff, /* dst_mask */
918 FALSE), /* pcrel_offset */
920 /* 16-bit section relative relocation. */
921 HOWTO (R_PPC_SECTOFF, /* type */
922 0, /* rightshift */
923 1, /* size (0 = byte, 1 = short, 2 = long) */
924 16, /* bitsize */
925 FALSE, /* pc_relative */
926 0, /* bitpos */
927 complain_overflow_bitfield, /* complain_on_overflow */
928 bfd_elf_generic_reloc, /* special_function */
929 "R_PPC_SECTOFF", /* name */
930 FALSE, /* partial_inplace */
931 0, /* src_mask */
932 0xffff, /* dst_mask */
933 FALSE), /* pcrel_offset */
935 /* 16-bit lower half section relative relocation. */
936 HOWTO (R_PPC_SECTOFF_LO, /* type */
937 0, /* rightshift */
938 1, /* size (0 = byte, 1 = short, 2 = long) */
939 16, /* bitsize */
940 FALSE, /* pc_relative */
941 0, /* bitpos */
942 complain_overflow_dont, /* complain_on_overflow */
943 bfd_elf_generic_reloc, /* special_function */
944 "R_PPC_SECTOFF_LO", /* name */
945 FALSE, /* partial_inplace */
946 0, /* src_mask */
947 0xffff, /* dst_mask */
948 FALSE), /* pcrel_offset */
950 /* 16-bit upper half section relative relocation. */
951 HOWTO (R_PPC_SECTOFF_HI, /* type */
952 16, /* rightshift */
953 1, /* size (0 = byte, 1 = short, 2 = long) */
954 16, /* bitsize */
955 FALSE, /* pc_relative */
956 0, /* bitpos */
957 complain_overflow_bitfield, /* complain_on_overflow */
958 bfd_elf_generic_reloc, /* special_function */
959 "R_PPC_SECTOFF_HI", /* name */
960 FALSE, /* partial_inplace */
961 0, /* src_mask */
962 0xffff, /* dst_mask */
963 FALSE), /* pcrel_offset */
965 /* 16-bit upper half adjusted section relative relocation. */
966 HOWTO (R_PPC_SECTOFF_HA, /* type */
967 16, /* rightshift */
968 1, /* size (0 = byte, 1 = short, 2 = long) */
969 16, /* bitsize */
970 FALSE, /* pc_relative */
971 0, /* bitpos */
972 complain_overflow_bitfield, /* complain_on_overflow */
973 ppc_elf_addr16_ha_reloc, /* special_function */
974 "R_PPC_SECTOFF_HA", /* name */
975 FALSE, /* partial_inplace */
976 0, /* src_mask */
977 0xffff, /* dst_mask */
978 FALSE), /* pcrel_offset */
980 /* Marker reloc for TLS. */
981 HOWTO (R_PPC_TLS,
982 0, /* rightshift */
983 2, /* size (0 = byte, 1 = short, 2 = long) */
984 32, /* bitsize */
985 FALSE, /* pc_relative */
986 0, /* bitpos */
987 complain_overflow_dont, /* complain_on_overflow */
988 bfd_elf_generic_reloc, /* special_function */
989 "R_PPC_TLS", /* name */
990 FALSE, /* partial_inplace */
991 0, /* src_mask */
992 0, /* dst_mask */
993 FALSE), /* pcrel_offset */
995 /* Computes the load module index of the load module that contains the
996 definition of its TLS sym. */
997 HOWTO (R_PPC_DTPMOD32,
998 0, /* rightshift */
999 2, /* size (0 = byte, 1 = short, 2 = long) */
1000 32, /* bitsize */
1001 FALSE, /* pc_relative */
1002 0, /* bitpos */
1003 complain_overflow_dont, /* complain_on_overflow */
1004 ppc_elf_unhandled_reloc, /* special_function */
1005 "R_PPC_DTPMOD32", /* name */
1006 FALSE, /* partial_inplace */
1007 0, /* src_mask */
1008 0xffffffff, /* dst_mask */
1009 FALSE), /* pcrel_offset */
1011 /* Computes a dtv-relative displacement, the difference between the value
1012 of sym+add and the base address of the thread-local storage block that
1013 contains the definition of sym, minus 0x8000. */
1014 HOWTO (R_PPC_DTPREL32,
1015 0, /* rightshift */
1016 2, /* size (0 = byte, 1 = short, 2 = long) */
1017 32, /* bitsize */
1018 FALSE, /* pc_relative */
1019 0, /* bitpos */
1020 complain_overflow_dont, /* complain_on_overflow */
1021 ppc_elf_unhandled_reloc, /* special_function */
1022 "R_PPC_DTPREL32", /* name */
1023 FALSE, /* partial_inplace */
1024 0, /* src_mask */
1025 0xffffffff, /* dst_mask */
1026 FALSE), /* pcrel_offset */
1028 /* A 16 bit dtprel reloc. */
1029 HOWTO (R_PPC_DTPREL16,
1030 0, /* rightshift */
1031 1, /* size (0 = byte, 1 = short, 2 = long) */
1032 16, /* bitsize */
1033 FALSE, /* pc_relative */
1034 0, /* bitpos */
1035 complain_overflow_signed, /* complain_on_overflow */
1036 ppc_elf_unhandled_reloc, /* special_function */
1037 "R_PPC_DTPREL16", /* name */
1038 FALSE, /* partial_inplace */
1039 0, /* src_mask */
1040 0xffff, /* dst_mask */
1041 FALSE), /* pcrel_offset */
1043 /* Like DTPREL16, but no overflow. */
1044 HOWTO (R_PPC_DTPREL16_LO,
1045 0, /* rightshift */
1046 1, /* size (0 = byte, 1 = short, 2 = long) */
1047 16, /* bitsize */
1048 FALSE, /* pc_relative */
1049 0, /* bitpos */
1050 complain_overflow_dont, /* complain_on_overflow */
1051 ppc_elf_unhandled_reloc, /* special_function */
1052 "R_PPC_DTPREL16_LO", /* name */
1053 FALSE, /* partial_inplace */
1054 0, /* src_mask */
1055 0xffff, /* dst_mask */
1056 FALSE), /* pcrel_offset */
1058 /* Like DTPREL16_LO, but next higher group of 16 bits. */
1059 HOWTO (R_PPC_DTPREL16_HI,
1060 16, /* rightshift */
1061 1, /* size (0 = byte, 1 = short, 2 = long) */
1062 16, /* bitsize */
1063 FALSE, /* pc_relative */
1064 0, /* bitpos */
1065 complain_overflow_dont, /* complain_on_overflow */
1066 ppc_elf_unhandled_reloc, /* special_function */
1067 "R_PPC_DTPREL16_HI", /* name */
1068 FALSE, /* partial_inplace */
1069 0, /* src_mask */
1070 0xffff, /* dst_mask */
1071 FALSE), /* pcrel_offset */
1073 /* Like DTPREL16_HI, but adjust for low 16 bits. */
1074 HOWTO (R_PPC_DTPREL16_HA,
1075 16, /* rightshift */
1076 1, /* size (0 = byte, 1 = short, 2 = long) */
1077 16, /* bitsize */
1078 FALSE, /* pc_relative */
1079 0, /* bitpos */
1080 complain_overflow_dont, /* complain_on_overflow */
1081 ppc_elf_unhandled_reloc, /* special_function */
1082 "R_PPC_DTPREL16_HA", /* name */
1083 FALSE, /* partial_inplace */
1084 0, /* src_mask */
1085 0xffff, /* dst_mask */
1086 FALSE), /* pcrel_offset */
1088 /* Computes a tp-relative displacement, the difference between the value of
1089 sym+add and the value of the thread pointer (r13). */
1090 HOWTO (R_PPC_TPREL32,
1091 0, /* rightshift */
1092 2, /* size (0 = byte, 1 = short, 2 = long) */
1093 32, /* bitsize */
1094 FALSE, /* pc_relative */
1095 0, /* bitpos */
1096 complain_overflow_dont, /* complain_on_overflow */
1097 ppc_elf_unhandled_reloc, /* special_function */
1098 "R_PPC_TPREL32", /* name */
1099 FALSE, /* partial_inplace */
1100 0, /* src_mask */
1101 0xffffffff, /* dst_mask */
1102 FALSE), /* pcrel_offset */
1104 /* A 16 bit tprel reloc. */
1105 HOWTO (R_PPC_TPREL16,
1106 0, /* rightshift */
1107 1, /* size (0 = byte, 1 = short, 2 = long) */
1108 16, /* bitsize */
1109 FALSE, /* pc_relative */
1110 0, /* bitpos */
1111 complain_overflow_signed, /* complain_on_overflow */
1112 ppc_elf_unhandled_reloc, /* special_function */
1113 "R_PPC_TPREL16", /* name */
1114 FALSE, /* partial_inplace */
1115 0, /* src_mask */
1116 0xffff, /* dst_mask */
1117 FALSE), /* pcrel_offset */
1119 /* Like TPREL16, but no overflow. */
1120 HOWTO (R_PPC_TPREL16_LO,
1121 0, /* rightshift */
1122 1, /* size (0 = byte, 1 = short, 2 = long) */
1123 16, /* bitsize */
1124 FALSE, /* pc_relative */
1125 0, /* bitpos */
1126 complain_overflow_dont, /* complain_on_overflow */
1127 ppc_elf_unhandled_reloc, /* special_function */
1128 "R_PPC_TPREL16_LO", /* name */
1129 FALSE, /* partial_inplace */
1130 0, /* src_mask */
1131 0xffff, /* dst_mask */
1132 FALSE), /* pcrel_offset */
1134 /* Like TPREL16_LO, but next higher group of 16 bits. */
1135 HOWTO (R_PPC_TPREL16_HI,
1136 16, /* rightshift */
1137 1, /* size (0 = byte, 1 = short, 2 = long) */
1138 16, /* bitsize */
1139 FALSE, /* pc_relative */
1140 0, /* bitpos */
1141 complain_overflow_dont, /* complain_on_overflow */
1142 ppc_elf_unhandled_reloc, /* special_function */
1143 "R_PPC_TPREL16_HI", /* name */
1144 FALSE, /* partial_inplace */
1145 0, /* src_mask */
1146 0xffff, /* dst_mask */
1147 FALSE), /* pcrel_offset */
1149 /* Like TPREL16_HI, but adjust for low 16 bits. */
1150 HOWTO (R_PPC_TPREL16_HA,
1151 16, /* rightshift */
1152 1, /* size (0 = byte, 1 = short, 2 = long) */
1153 16, /* bitsize */
1154 FALSE, /* pc_relative */
1155 0, /* bitpos */
1156 complain_overflow_dont, /* complain_on_overflow */
1157 ppc_elf_unhandled_reloc, /* special_function */
1158 "R_PPC_TPREL16_HA", /* name */
1159 FALSE, /* partial_inplace */
1160 0, /* src_mask */
1161 0xffff, /* dst_mask */
1162 FALSE), /* pcrel_offset */
1164 /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
1165 with values (sym+add)@dtpmod and (sym+add)@dtprel, and computes the offset
1166 to the first entry. */
1167 HOWTO (R_PPC_GOT_TLSGD16,
1168 0, /* rightshift */
1169 1, /* size (0 = byte, 1 = short, 2 = long) */
1170 16, /* bitsize */
1171 FALSE, /* pc_relative */
1172 0, /* bitpos */
1173 complain_overflow_signed, /* complain_on_overflow */
1174 ppc_elf_unhandled_reloc, /* special_function */
1175 "R_PPC_GOT_TLSGD16", /* name */
1176 FALSE, /* partial_inplace */
1177 0, /* src_mask */
1178 0xffff, /* dst_mask */
1179 FALSE), /* pcrel_offset */
1181 /* Like GOT_TLSGD16, but no overflow. */
1182 HOWTO (R_PPC_GOT_TLSGD16_LO,
1183 0, /* rightshift */
1184 1, /* size (0 = byte, 1 = short, 2 = long) */
1185 16, /* bitsize */
1186 FALSE, /* pc_relative */
1187 0, /* bitpos */
1188 complain_overflow_dont, /* complain_on_overflow */
1189 ppc_elf_unhandled_reloc, /* special_function */
1190 "R_PPC_GOT_TLSGD16_LO", /* name */
1191 FALSE, /* partial_inplace */
1192 0, /* src_mask */
1193 0xffff, /* dst_mask */
1194 FALSE), /* pcrel_offset */
1196 /* Like GOT_TLSGD16_LO, but next higher group of 16 bits. */
1197 HOWTO (R_PPC_GOT_TLSGD16_HI,
1198 16, /* rightshift */
1199 1, /* size (0 = byte, 1 = short, 2 = long) */
1200 16, /* bitsize */
1201 FALSE, /* pc_relative */
1202 0, /* bitpos */
1203 complain_overflow_dont, /* complain_on_overflow */
1204 ppc_elf_unhandled_reloc, /* special_function */
1205 "R_PPC_GOT_TLSGD16_HI", /* name */
1206 FALSE, /* partial_inplace */
1207 0, /* src_mask */
1208 0xffff, /* dst_mask */
1209 FALSE), /* pcrel_offset */
1211 /* Like GOT_TLSGD16_HI, but adjust for low 16 bits. */
1212 HOWTO (R_PPC_GOT_TLSGD16_HA,
1213 16, /* rightshift */
1214 1, /* size (0 = byte, 1 = short, 2 = long) */
1215 16, /* bitsize */
1216 FALSE, /* pc_relative */
1217 0, /* bitpos */
1218 complain_overflow_dont, /* complain_on_overflow */
1219 ppc_elf_unhandled_reloc, /* special_function */
1220 "R_PPC_GOT_TLSGD16_HA", /* name */
1221 FALSE, /* partial_inplace */
1222 0, /* src_mask */
1223 0xffff, /* dst_mask */
1224 FALSE), /* pcrel_offset */
1226 /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
1227 with values (sym+add)@dtpmod and zero, and computes the offset to the
1228 first entry. */
1229 HOWTO (R_PPC_GOT_TLSLD16,
1230 0, /* rightshift */
1231 1, /* size (0 = byte, 1 = short, 2 = long) */
1232 16, /* bitsize */
1233 FALSE, /* pc_relative */
1234 0, /* bitpos */
1235 complain_overflow_signed, /* complain_on_overflow */
1236 ppc_elf_unhandled_reloc, /* special_function */
1237 "R_PPC_GOT_TLSLD16", /* name */
1238 FALSE, /* partial_inplace */
1239 0, /* src_mask */
1240 0xffff, /* dst_mask */
1241 FALSE), /* pcrel_offset */
1243 /* Like GOT_TLSLD16, but no overflow. */
1244 HOWTO (R_PPC_GOT_TLSLD16_LO,
1245 0, /* rightshift */
1246 1, /* size (0 = byte, 1 = short, 2 = long) */
1247 16, /* bitsize */
1248 FALSE, /* pc_relative */
1249 0, /* bitpos */
1250 complain_overflow_dont, /* complain_on_overflow */
1251 ppc_elf_unhandled_reloc, /* special_function */
1252 "R_PPC_GOT_TLSLD16_LO", /* name */
1253 FALSE, /* partial_inplace */
1254 0, /* src_mask */
1255 0xffff, /* dst_mask */
1256 FALSE), /* pcrel_offset */
1258 /* Like GOT_TLSLD16_LO, but next higher group of 16 bits. */
1259 HOWTO (R_PPC_GOT_TLSLD16_HI,
1260 16, /* rightshift */
1261 1, /* size (0 = byte, 1 = short, 2 = long) */
1262 16, /* bitsize */
1263 FALSE, /* pc_relative */
1264 0, /* bitpos */
1265 complain_overflow_dont, /* complain_on_overflow */
1266 ppc_elf_unhandled_reloc, /* special_function */
1267 "R_PPC_GOT_TLSLD16_HI", /* name */
1268 FALSE, /* partial_inplace */
1269 0, /* src_mask */
1270 0xffff, /* dst_mask */
1271 FALSE), /* pcrel_offset */
1273 /* Like GOT_TLSLD16_HI, but adjust for low 16 bits. */
1274 HOWTO (R_PPC_GOT_TLSLD16_HA,
1275 16, /* rightshift */
1276 1, /* size (0 = byte, 1 = short, 2 = long) */
1277 16, /* bitsize */
1278 FALSE, /* pc_relative */
1279 0, /* bitpos */
1280 complain_overflow_dont, /* complain_on_overflow */
1281 ppc_elf_unhandled_reloc, /* special_function */
1282 "R_PPC_GOT_TLSLD16_HA", /* name */
1283 FALSE, /* partial_inplace */
1284 0, /* src_mask */
1285 0xffff, /* dst_mask */
1286 FALSE), /* pcrel_offset */
1288 /* Allocates an entry in the GOT with value (sym+add)@dtprel, and computes
1289 the offset to the entry. */
1290 HOWTO (R_PPC_GOT_DTPREL16,
1291 0, /* rightshift */
1292 1, /* size (0 = byte, 1 = short, 2 = long) */
1293 16, /* bitsize */
1294 FALSE, /* pc_relative */
1295 0, /* bitpos */
1296 complain_overflow_signed, /* complain_on_overflow */
1297 ppc_elf_unhandled_reloc, /* special_function */
1298 "R_PPC_GOT_DTPREL16", /* name */
1299 FALSE, /* partial_inplace */
1300 0, /* src_mask */
1301 0xffff, /* dst_mask */
1302 FALSE), /* pcrel_offset */
1304 /* Like GOT_DTPREL16, but no overflow. */
1305 HOWTO (R_PPC_GOT_DTPREL16_LO,
1306 0, /* rightshift */
1307 1, /* size (0 = byte, 1 = short, 2 = long) */
1308 16, /* bitsize */
1309 FALSE, /* pc_relative */
1310 0, /* bitpos */
1311 complain_overflow_dont, /* complain_on_overflow */
1312 ppc_elf_unhandled_reloc, /* special_function */
1313 "R_PPC_GOT_DTPREL16_LO", /* name */
1314 FALSE, /* partial_inplace */
1315 0, /* src_mask */
1316 0xffff, /* dst_mask */
1317 FALSE), /* pcrel_offset */
1319 /* Like GOT_DTPREL16_LO, but next higher group of 16 bits. */
1320 HOWTO (R_PPC_GOT_DTPREL16_HI,
1321 16, /* rightshift */
1322 1, /* size (0 = byte, 1 = short, 2 = long) */
1323 16, /* bitsize */
1324 FALSE, /* pc_relative */
1325 0, /* bitpos */
1326 complain_overflow_dont, /* complain_on_overflow */
1327 ppc_elf_unhandled_reloc, /* special_function */
1328 "R_PPC_GOT_DTPREL16_HI", /* name */
1329 FALSE, /* partial_inplace */
1330 0, /* src_mask */
1331 0xffff, /* dst_mask */
1332 FALSE), /* pcrel_offset */
1334 /* Like GOT_DTPREL16_HI, but adjust for low 16 bits. */
1335 HOWTO (R_PPC_GOT_DTPREL16_HA,
1336 16, /* rightshift */
1337 1, /* size (0 = byte, 1 = short, 2 = long) */
1338 16, /* bitsize */
1339 FALSE, /* pc_relative */
1340 0, /* bitpos */
1341 complain_overflow_dont, /* complain_on_overflow */
1342 ppc_elf_unhandled_reloc, /* special_function */
1343 "R_PPC_GOT_DTPREL16_HA", /* name */
1344 FALSE, /* partial_inplace */
1345 0, /* src_mask */
1346 0xffff, /* dst_mask */
1347 FALSE), /* pcrel_offset */
1349 /* Allocates an entry in the GOT with value (sym+add)@tprel, and computes the
1350 offset to the entry. */
1351 HOWTO (R_PPC_GOT_TPREL16,
1352 0, /* rightshift */
1353 1, /* size (0 = byte, 1 = short, 2 = long) */
1354 16, /* bitsize */
1355 FALSE, /* pc_relative */
1356 0, /* bitpos */
1357 complain_overflow_signed, /* complain_on_overflow */
1358 ppc_elf_unhandled_reloc, /* special_function */
1359 "R_PPC_GOT_TPREL16", /* name */
1360 FALSE, /* partial_inplace */
1361 0, /* src_mask */
1362 0xffff, /* dst_mask */
1363 FALSE), /* pcrel_offset */
1365 /* Like GOT_TPREL16, but no overflow. */
1366 HOWTO (R_PPC_GOT_TPREL16_LO,
1367 0, /* rightshift */
1368 1, /* size (0 = byte, 1 = short, 2 = long) */
1369 16, /* bitsize */
1370 FALSE, /* pc_relative */
1371 0, /* bitpos */
1372 complain_overflow_dont, /* complain_on_overflow */
1373 ppc_elf_unhandled_reloc, /* special_function */
1374 "R_PPC_GOT_TPREL16_LO", /* name */
1375 FALSE, /* partial_inplace */
1376 0, /* src_mask */
1377 0xffff, /* dst_mask */
1378 FALSE), /* pcrel_offset */
1380 /* Like GOT_TPREL16_LO, but next higher group of 16 bits. */
1381 HOWTO (R_PPC_GOT_TPREL16_HI,
1382 16, /* rightshift */
1383 1, /* size (0 = byte, 1 = short, 2 = long) */
1384 16, /* bitsize */
1385 FALSE, /* pc_relative */
1386 0, /* bitpos */
1387 complain_overflow_dont, /* complain_on_overflow */
1388 ppc_elf_unhandled_reloc, /* special_function */
1389 "R_PPC_GOT_TPREL16_HI", /* name */
1390 FALSE, /* partial_inplace */
1391 0, /* src_mask */
1392 0xffff, /* dst_mask */
1393 FALSE), /* pcrel_offset */
1395 /* Like GOT_TPREL16_HI, but adjust for low 16 bits. */
1396 HOWTO (R_PPC_GOT_TPREL16_HA,
1397 16, /* rightshift */
1398 1, /* size (0 = byte, 1 = short, 2 = long) */
1399 16, /* bitsize */
1400 FALSE, /* pc_relative */
1401 0, /* bitpos */
1402 complain_overflow_dont, /* complain_on_overflow */
1403 ppc_elf_unhandled_reloc, /* special_function */
1404 "R_PPC_GOT_TPREL16_HA", /* name */
1405 FALSE, /* partial_inplace */
1406 0, /* src_mask */
1407 0xffff, /* dst_mask */
1408 FALSE), /* pcrel_offset */
1410 /* The remaining relocs are from the Embedded ELF ABI, and are not
1411 in the SVR4 ELF ABI. */
1413 /* 32 bit value resulting from the addend minus the symbol. */
1414 HOWTO (R_PPC_EMB_NADDR32, /* type */
1415 0, /* rightshift */
1416 2, /* size (0 = byte, 1 = short, 2 = long) */
1417 32, /* bitsize */
1418 FALSE, /* pc_relative */
1419 0, /* bitpos */
1420 complain_overflow_bitfield, /* complain_on_overflow */
1421 bfd_elf_generic_reloc, /* special_function */
1422 "R_PPC_EMB_NADDR32", /* name */
1423 FALSE, /* partial_inplace */
1424 0, /* src_mask */
1425 0xffffffff, /* dst_mask */
1426 FALSE), /* pcrel_offset */
1428 /* 16 bit value resulting from the addend minus the symbol. */
1429 HOWTO (R_PPC_EMB_NADDR16, /* type */
1430 0, /* rightshift */
1431 1, /* size (0 = byte, 1 = short, 2 = long) */
1432 16, /* bitsize */
1433 FALSE, /* pc_relative */
1434 0, /* bitpos */
1435 complain_overflow_bitfield, /* complain_on_overflow */
1436 bfd_elf_generic_reloc, /* special_function */
1437 "R_PPC_EMB_NADDR16", /* name */
1438 FALSE, /* partial_inplace */
1439 0, /* src_mask */
1440 0xffff, /* dst_mask */
1441 FALSE), /* pcrel_offset */
1443 /* 16 bit value resulting from the addend minus the symbol. */
1444 HOWTO (R_PPC_EMB_NADDR16_LO, /* type */
1445 0, /* rightshift */
1446 1, /* size (0 = byte, 1 = short, 2 = long) */
1447 16, /* bitsize */
1448 FALSE, /* pc_relative */
1449 0, /* bitpos */
1450 complain_overflow_dont,/* complain_on_overflow */
1451 bfd_elf_generic_reloc, /* special_function */
1452 "R_PPC_EMB_ADDR16_LO", /* name */
1453 FALSE, /* partial_inplace */
1454 0, /* src_mask */
1455 0xffff, /* dst_mask */
1456 FALSE), /* pcrel_offset */
1458 /* The high order 16 bits of the addend minus the symbol. */
1459 HOWTO (R_PPC_EMB_NADDR16_HI, /* type */
1460 16, /* rightshift */
1461 1, /* size (0 = byte, 1 = short, 2 = long) */
1462 16, /* bitsize */
1463 FALSE, /* pc_relative */
1464 0, /* bitpos */
1465 complain_overflow_dont, /* complain_on_overflow */
1466 bfd_elf_generic_reloc, /* special_function */
1467 "R_PPC_EMB_NADDR16_HI", /* name */
1468 FALSE, /* partial_inplace */
1469 0, /* src_mask */
1470 0xffff, /* dst_mask */
1471 FALSE), /* pcrel_offset */
1473 /* The high order 16 bits of the result of the addend minus the address,
1474 plus 1 if the contents of the low 16 bits, treated as a signed number,
1475 is negative. */
1476 HOWTO (R_PPC_EMB_NADDR16_HA, /* type */
1477 16, /* rightshift */
1478 1, /* size (0 = byte, 1 = short, 2 = long) */
1479 16, /* bitsize */
1480 FALSE, /* pc_relative */
1481 0, /* bitpos */
1482 complain_overflow_dont, /* complain_on_overflow */
1483 ppc_elf_addr16_ha_reloc, /* special_function */
1484 "R_PPC_EMB_NADDR16_HA", /* name */
1485 FALSE, /* partial_inplace */
1486 0, /* src_mask */
1487 0xffff, /* dst_mask */
1488 FALSE), /* pcrel_offset */
1490 /* 16 bit value resulting from allocating a 4 byte word to hold an
1491 address in the .sdata section, and returning the offset from
1492 _SDA_BASE_ for that relocation. */
1493 HOWTO (R_PPC_EMB_SDAI16, /* type */
1494 0, /* rightshift */
1495 1, /* size (0 = byte, 1 = short, 2 = long) */
1496 16, /* bitsize */
1497 FALSE, /* pc_relative */
1498 0, /* bitpos */
1499 complain_overflow_bitfield, /* complain_on_overflow */
1500 bfd_elf_generic_reloc, /* special_function */
1501 "R_PPC_EMB_SDAI16", /* name */
1502 FALSE, /* partial_inplace */
1503 0, /* src_mask */
1504 0xffff, /* dst_mask */
1505 FALSE), /* pcrel_offset */
1507 /* 16 bit value resulting from allocating a 4 byte word to hold an
1508 address in the .sdata2 section, and returning the offset from
1509 _SDA2_BASE_ for that relocation. */
1510 HOWTO (R_PPC_EMB_SDA2I16, /* type */
1511 0, /* rightshift */
1512 1, /* size (0 = byte, 1 = short, 2 = long) */
1513 16, /* bitsize */
1514 FALSE, /* pc_relative */
1515 0, /* bitpos */
1516 complain_overflow_bitfield, /* complain_on_overflow */
1517 bfd_elf_generic_reloc, /* special_function */
1518 "R_PPC_EMB_SDA2I16", /* name */
1519 FALSE, /* partial_inplace */
1520 0, /* src_mask */
1521 0xffff, /* dst_mask */
1522 FALSE), /* pcrel_offset */
1524 /* A sign-extended 16 bit value relative to _SDA2_BASE_, for use with
1525 small data items. */
1526 HOWTO (R_PPC_EMB_SDA2REL, /* type */
1527 0, /* rightshift */
1528 1, /* size (0 = byte, 1 = short, 2 = long) */
1529 16, /* bitsize */
1530 FALSE, /* pc_relative */
1531 0, /* bitpos */
1532 complain_overflow_signed, /* complain_on_overflow */
1533 bfd_elf_generic_reloc, /* special_function */
1534 "R_PPC_EMB_SDA2REL", /* name */
1535 FALSE, /* partial_inplace */
1536 0, /* src_mask */
1537 0xffff, /* dst_mask */
1538 FALSE), /* pcrel_offset */
1540 /* Relocate against either _SDA_BASE_ or _SDA2_BASE_, filling in the 16 bit
1541 signed offset from the appropriate base, and filling in the register
1542 field with the appropriate register (0, 2, or 13). */
1543 HOWTO (R_PPC_EMB_SDA21, /* type */
1544 0, /* rightshift */
1545 2, /* size (0 = byte, 1 = short, 2 = long) */
1546 16, /* bitsize */
1547 FALSE, /* pc_relative */
1548 0, /* bitpos */
1549 complain_overflow_signed, /* complain_on_overflow */
1550 bfd_elf_generic_reloc, /* special_function */
1551 "R_PPC_EMB_SDA21", /* name */
1552 FALSE, /* partial_inplace */
1553 0, /* src_mask */
1554 0xffff, /* dst_mask */
1555 FALSE), /* pcrel_offset */
1557 /* Relocation not handled: R_PPC_EMB_MRKREF */
1558 /* Relocation not handled: R_PPC_EMB_RELSEC16 */
1559 /* Relocation not handled: R_PPC_EMB_RELST_LO */
1560 /* Relocation not handled: R_PPC_EMB_RELST_HI */
1561 /* Relocation not handled: R_PPC_EMB_RELST_HA */
1562 /* Relocation not handled: R_PPC_EMB_BIT_FLD */
1564 /* PC relative relocation against either _SDA_BASE_ or _SDA2_BASE_, filling
1565 in the 16 bit signed offset from the appropriate base, and filling in the
1566 register field with the appropriate register (0, 2, or 13). */
1567 HOWTO (R_PPC_EMB_RELSDA, /* type */
1568 0, /* rightshift */
1569 1, /* size (0 = byte, 1 = short, 2 = long) */
1570 16, /* bitsize */
1571 TRUE, /* pc_relative */
1572 0, /* bitpos */
1573 complain_overflow_signed, /* complain_on_overflow */
1574 bfd_elf_generic_reloc, /* special_function */
1575 "R_PPC_EMB_RELSDA", /* name */
1576 FALSE, /* partial_inplace */
1577 0, /* src_mask */
1578 0xffff, /* dst_mask */
1579 FALSE), /* pcrel_offset */
1581 /* GNU extension to record C++ vtable hierarchy. */
1582 HOWTO (R_PPC_GNU_VTINHERIT, /* type */
1583 0, /* rightshift */
1584 0, /* size (0 = byte, 1 = short, 2 = long) */
1585 0, /* bitsize */
1586 FALSE, /* pc_relative */
1587 0, /* bitpos */
1588 complain_overflow_dont, /* complain_on_overflow */
1589 NULL, /* special_function */
1590 "R_PPC_GNU_VTINHERIT", /* name */
1591 FALSE, /* partial_inplace */
1592 0, /* src_mask */
1593 0, /* dst_mask */
1594 FALSE), /* pcrel_offset */
1596 /* GNU extension to record C++ vtable member usage. */
1597 HOWTO (R_PPC_GNU_VTENTRY, /* type */
1598 0, /* rightshift */
1599 0, /* size (0 = byte, 1 = short, 2 = long) */
1600 0, /* bitsize */
1601 FALSE, /* pc_relative */
1602 0, /* bitpos */
1603 complain_overflow_dont, /* complain_on_overflow */
1604 NULL, /* special_function */
1605 "R_PPC_GNU_VTENTRY", /* name */
1606 FALSE, /* partial_inplace */
1607 0, /* src_mask */
1608 0, /* dst_mask */
1609 FALSE), /* pcrel_offset */
1611 /* Phony reloc to handle AIX style TOC entries. */
1612 HOWTO (R_PPC_TOC16, /* type */
1613 0, /* rightshift */
1614 1, /* size (0 = byte, 1 = short, 2 = long) */
1615 16, /* bitsize */
1616 FALSE, /* pc_relative */
1617 0, /* bitpos */
1618 complain_overflow_signed, /* complain_on_overflow */
1619 bfd_elf_generic_reloc, /* special_function */
1620 "R_PPC_TOC16", /* name */
1621 FALSE, /* partial_inplace */
1622 0, /* src_mask */
1623 0xffff, /* dst_mask */
1624 FALSE), /* pcrel_offset */
1627 /* Initialize the ppc_elf_howto_table, so that linear accesses can be done. */
1629 static void
1630 ppc_elf_howto_init ()
1632 unsigned int i, type;
1634 for (i = 0;
1635 i < sizeof (ppc_elf_howto_raw) / sizeof (ppc_elf_howto_raw[0]);
1636 i++)
1638 type = ppc_elf_howto_raw[i].type;
1639 if (type >= (sizeof (ppc_elf_howto_table)
1640 / sizeof (ppc_elf_howto_table[0])))
1641 abort ();
1642 ppc_elf_howto_table[type] = &ppc_elf_howto_raw[i];
1646 /* This function handles relaxing for the PPC with option --mpc860c0[=<n>].
1648 The MPC860, revision C0 or earlier contains a bug in the die.
1649 If all of the following conditions are true, the next instruction
1650 to be executed *may* be treated as a no-op.
1651 1/ A forward branch is executed.
1652 2/ The branch is predicted as not taken.
1653 3/ The branch is taken.
1654 4/ The branch is located in the last 5 words of a page.
1655 (The EOP limit is 5 by default but may be specified as any value
1656 from 1-10.)
1658 Our software solution is to detect these problematic branches in a
1659 linker pass and modify them as follows:
1660 1/ Unconditional branches - Since these are always predicted taken,
1661 there is no problem and no action is required.
1662 2/ Conditional backward branches - No problem, no action required.
1663 3/ Conditional forward branches - Ensure that the "inverse prediction
1664 bit" is set (ensure it is predicted taken).
1665 4/ Conditional register branches - Ensure that the "y bit" is set
1666 (ensure it is predicted taken). */
1668 /* Sort sections by address. */
1670 static int
1671 ppc_elf_sort_rela (arg1, arg2)
1672 const PTR arg1;
1673 const PTR arg2;
1675 const Elf_Internal_Rela **rela1 = (const Elf_Internal_Rela**) arg1;
1676 const Elf_Internal_Rela **rela2 = (const Elf_Internal_Rela**) arg2;
1678 /* Sort by offset. */
1679 return ((*rela1)->r_offset - (*rela2)->r_offset);
1682 static bfd_boolean
1683 ppc_elf_relax_section (abfd, isec, link_info, again)
1684 bfd *abfd;
1685 asection *isec;
1686 struct bfd_link_info *link_info;
1687 bfd_boolean *again;
1689 #define PAGESIZE 0x1000
1691 bfd_byte *contents = NULL;
1692 bfd_byte *free_contents = NULL;
1693 Elf_Internal_Rela *internal_relocs = NULL;
1694 Elf_Internal_Rela *free_relocs = NULL;
1695 Elf_Internal_Rela **rela_comb = NULL;
1696 int comb_curr, comb_count;
1698 /* We never have to do this more than once per input section. */
1699 *again = FALSE;
1701 /* If needed, initialize this section's cooked size. */
1702 if (isec->_cooked_size == 0)
1703 isec->_cooked_size = isec->_raw_size;
1705 /* We're only interested in text sections which overlap the
1706 troublesome area at the end of a page. */
1707 if (link_info->mpc860c0 && (isec->flags & SEC_CODE) && isec->_cooked_size)
1709 bfd_vma dot, end_page, end_section;
1710 bfd_boolean section_modified;
1712 /* Get the section contents. */
1713 /* Get cached copy if it exists. */
1714 if (elf_section_data (isec)->this_hdr.contents != NULL)
1715 contents = elf_section_data (isec)->this_hdr.contents;
1716 else
1718 /* Go get them off disk. */
1719 contents = (bfd_byte *) bfd_malloc (isec->_raw_size);
1720 if (contents == NULL)
1721 goto error_return;
1722 free_contents = contents;
1724 if (! bfd_get_section_contents (abfd, isec, contents,
1725 (file_ptr) 0, isec->_raw_size))
1726 goto error_return;
1729 comb_curr = 0;
1730 comb_count = 0;
1731 if (isec->reloc_count)
1733 unsigned n;
1734 bfd_size_type amt;
1736 /* Get a copy of the native relocations. */
1737 internal_relocs
1738 = _bfd_elf_link_read_relocs (abfd, isec, (PTR) NULL,
1739 (Elf_Internal_Rela *) NULL,
1740 link_info->keep_memory);
1741 if (internal_relocs == NULL)
1742 goto error_return;
1743 if (! link_info->keep_memory)
1744 free_relocs = internal_relocs;
1746 /* Setup a faster access method for the reloc info we need. */
1747 amt = isec->reloc_count;
1748 amt *= sizeof (Elf_Internal_Rela*);
1749 rela_comb = (Elf_Internal_Rela**) bfd_malloc (amt);
1750 if (rela_comb == NULL)
1751 goto error_return;
1752 for (n = 0; n < isec->reloc_count; ++n)
1754 long r_type;
1756 r_type = ELF32_R_TYPE (internal_relocs[n].r_info);
1757 if (r_type < 0 || r_type >= (int) R_PPC_max)
1758 goto error_return;
1760 /* Prologue constants are sometimes present in the ".text"
1761 sections and they can be identified by their associated
1762 relocation. We don't want to process those words and
1763 some others which can also be identified by their
1764 relocations. However, not all conditional branches will
1765 have a relocation so we will only ignore words that
1766 1) have a reloc, and 2) the reloc is not applicable to a
1767 conditional branch. The array rela_comb is built here
1768 for use in the EOP scan loop. */
1769 switch (r_type)
1771 case R_PPC_ADDR14_BRNTAKEN:
1772 case R_PPC_REL14:
1773 case R_PPC_REL14_BRNTAKEN:
1774 /* We should check the instruction. */
1775 break;
1776 default:
1777 /* The word is not a conditional branch - ignore it. */
1778 rela_comb[comb_count++] = &internal_relocs[n];
1779 break;
1782 if (comb_count > 1)
1783 qsort (rela_comb, (size_t) comb_count, sizeof (int),
1784 ppc_elf_sort_rela);
1787 /* Enumerate each EOP region that overlaps this section. */
1788 end_section = isec->vma + isec->_cooked_size;
1789 dot = end_page = (isec->vma | (PAGESIZE - 1)) + 1;
1790 dot -= link_info->mpc860c0;
1791 section_modified = FALSE;
1792 /* Increment the start position if this section begins in the
1793 middle of its first EOP region. */
1794 if (dot < isec->vma)
1795 dot = isec->vma;
1796 for (;
1797 dot < end_section;
1798 dot += PAGESIZE, end_page += PAGESIZE)
1800 /* Check each word in this EOP region. */
1801 for (; dot < end_page; dot += 4)
1803 bfd_vma isec_offset;
1804 unsigned long insn;
1805 bfd_boolean skip, modified;
1807 /* Don't process this word if there is a relocation for it
1808 and the relocation indicates the word is not a
1809 conditional branch. */
1810 skip = FALSE;
1811 isec_offset = dot - isec->vma;
1812 for (; comb_curr<comb_count; ++comb_curr)
1814 bfd_vma r_offset;
1816 r_offset = rela_comb[comb_curr]->r_offset;
1817 if (r_offset >= isec_offset)
1819 if (r_offset == isec_offset) skip = TRUE;
1820 break;
1823 if (skip) continue;
1825 /* Check the current word for a problematic conditional
1826 branch. */
1827 #define BO0(insn) ((insn) & 0x02000000)
1828 #define BO2(insn) ((insn) & 0x00800000)
1829 #define BO4(insn) ((insn) & 0x00200000)
1830 insn = (unsigned long) bfd_get_32 (abfd, contents + isec_offset);
1831 modified = FALSE;
1832 if ((insn & 0xFc000000) == 0x40000000)
1834 /* Instruction is BCx */
1835 if ((!BO0(insn) || !BO2(insn)) && !BO4(insn))
1837 bfd_vma target;
1839 /* This branch is predicted as "normal".
1840 If this is a forward branch, it is problematic. */
1841 target = insn & 0x0000Fffc;
1842 target = (target ^ 0x8000) - 0x8000;
1843 if ((insn & 0x00000002) == 0)
1844 /* Convert to abs. */
1845 target += dot;
1846 if (target > dot)
1848 /* Set the prediction bit. */
1849 insn |= 0x00200000;
1850 modified = TRUE;
1854 else if ((insn & 0xFc00Fffe) == 0x4c000420)
1856 /* Instruction is BCCTRx. */
1857 if ((!BO0(insn) || !BO2(insn)) && !BO4(insn))
1859 /* This branch is predicted as not-taken.
1860 If this is a forward branch, it is problematic.
1861 Since we can't tell statically if it will branch
1862 forward, always set the prediction bit. */
1863 insn |= 0x00200000;
1864 modified = TRUE;
1867 else if ((insn & 0xFc00Fffe) == 0x4c000020)
1869 /* Instruction is BCLRx */
1870 if ((!BO0(insn) || !BO2(insn)) && !BO4(insn))
1872 /* This branch is predicted as not-taken.
1873 If this is a forward branch, it is problematic.
1874 Since we can't tell statically if it will branch
1875 forward, always set the prediction bit. */
1876 insn |= 0x00200000;
1877 modified = TRUE;
1880 #undef BO0
1881 #undef BO2
1882 #undef BO4
1883 if (modified)
1885 bfd_put_32 (abfd, (bfd_vma) insn, contents + isec_offset);
1886 section_modified = TRUE;
1890 if (section_modified)
1892 elf_section_data (isec)->this_hdr.contents = contents;
1893 free_contents = NULL;
1897 if (rela_comb != NULL)
1899 free (rela_comb);
1900 rela_comb = NULL;
1903 if (free_relocs != NULL)
1905 free (free_relocs);
1906 free_relocs = NULL;
1909 if (free_contents != NULL)
1911 if (! link_info->keep_memory)
1912 free (free_contents);
1913 else
1915 /* Cache the section contents for elf_link_input_bfd. */
1916 elf_section_data (isec)->this_hdr.contents = contents;
1918 free_contents = NULL;
1921 return TRUE;
1923 error_return:
1924 if (rela_comb != NULL)
1925 free (rela_comb);
1926 if (free_relocs != NULL)
1927 free (free_relocs);
1928 if (free_contents != NULL)
1929 free (free_contents);
1930 return FALSE;
1933 static reloc_howto_type *
1934 ppc_elf_reloc_type_lookup (abfd, code)
1935 bfd *abfd ATTRIBUTE_UNUSED;
1936 bfd_reloc_code_real_type code;
1938 enum elf_ppc_reloc_type r;
1940 if (!ppc_elf_howto_table[R_PPC_ADDR32])
1941 /* Initialize howto table if needed. */
1942 ppc_elf_howto_init ();
1944 switch ((int) code)
1946 default:
1947 return (reloc_howto_type *) NULL;
1949 case BFD_RELOC_NONE: r = R_PPC_NONE; break;
1950 case BFD_RELOC_32: r = R_PPC_ADDR32; break;
1951 case BFD_RELOC_PPC_BA26: r = R_PPC_ADDR24; break;
1952 case BFD_RELOC_16: r = R_PPC_ADDR16; break;
1953 case BFD_RELOC_LO16: r = R_PPC_ADDR16_LO; break;
1954 case BFD_RELOC_HI16: r = R_PPC_ADDR16_HI; break;
1955 case BFD_RELOC_HI16_S: r = R_PPC_ADDR16_HA; break;
1956 case BFD_RELOC_PPC_BA16: r = R_PPC_ADDR14; break;
1957 case BFD_RELOC_PPC_BA16_BRTAKEN: r = R_PPC_ADDR14_BRTAKEN; break;
1958 case BFD_RELOC_PPC_BA16_BRNTAKEN: r = R_PPC_ADDR14_BRNTAKEN; break;
1959 case BFD_RELOC_PPC_B26: r = R_PPC_REL24; break;
1960 case BFD_RELOC_PPC_B16: r = R_PPC_REL14; break;
1961 case BFD_RELOC_PPC_B16_BRTAKEN: r = R_PPC_REL14_BRTAKEN; break;
1962 case BFD_RELOC_PPC_B16_BRNTAKEN: r = R_PPC_REL14_BRNTAKEN; break;
1963 case BFD_RELOC_16_GOTOFF: r = R_PPC_GOT16; break;
1964 case BFD_RELOC_LO16_GOTOFF: r = R_PPC_GOT16_LO; break;
1965 case BFD_RELOC_HI16_GOTOFF: r = R_PPC_GOT16_HI; break;
1966 case BFD_RELOC_HI16_S_GOTOFF: r = R_PPC_GOT16_HA; break;
1967 case BFD_RELOC_24_PLT_PCREL: r = R_PPC_PLTREL24; break;
1968 case BFD_RELOC_PPC_COPY: r = R_PPC_COPY; break;
1969 case BFD_RELOC_PPC_GLOB_DAT: r = R_PPC_GLOB_DAT; break;
1970 case BFD_RELOC_PPC_LOCAL24PC: r = R_PPC_LOCAL24PC; break;
1971 case BFD_RELOC_32_PCREL: r = R_PPC_REL32; break;
1972 case BFD_RELOC_32_PLTOFF: r = R_PPC_PLT32; break;
1973 case BFD_RELOC_32_PLT_PCREL: r = R_PPC_PLTREL32; break;
1974 case BFD_RELOC_LO16_PLTOFF: r = R_PPC_PLT16_LO; break;
1975 case BFD_RELOC_HI16_PLTOFF: r = R_PPC_PLT16_HI; break;
1976 case BFD_RELOC_HI16_S_PLTOFF: r = R_PPC_PLT16_HA; break;
1977 case BFD_RELOC_GPREL16: r = R_PPC_SDAREL16; break;
1978 case BFD_RELOC_16_BASEREL: r = R_PPC_SECTOFF; break;
1979 case BFD_RELOC_LO16_BASEREL: r = R_PPC_SECTOFF_LO; break;
1980 case BFD_RELOC_HI16_BASEREL: r = R_PPC_SECTOFF_HI; break;
1981 case BFD_RELOC_HI16_S_BASEREL: r = R_PPC_SECTOFF_HA; break;
1982 case BFD_RELOC_CTOR: r = R_PPC_ADDR32; break;
1983 case BFD_RELOC_PPC_TOC16: r = R_PPC_TOC16; break;
1984 case BFD_RELOC_PPC_TLS: r = R_PPC_TLS; break;
1985 case BFD_RELOC_PPC_DTPMOD: r = R_PPC_DTPMOD32; break;
1986 case BFD_RELOC_PPC_TPREL16: r = R_PPC_TPREL16; break;
1987 case BFD_RELOC_PPC_TPREL16_LO: r = R_PPC_TPREL16_LO; break;
1988 case BFD_RELOC_PPC_TPREL16_HI: r = R_PPC_TPREL16_HI; break;
1989 case BFD_RELOC_PPC_TPREL16_HA: r = R_PPC_TPREL16_HA; break;
1990 case BFD_RELOC_PPC_TPREL: r = R_PPC_TPREL32; break;
1991 case BFD_RELOC_PPC_DTPREL16: r = R_PPC_DTPREL16; break;
1992 case BFD_RELOC_PPC_DTPREL16_LO: r = R_PPC_DTPREL16_LO; break;
1993 case BFD_RELOC_PPC_DTPREL16_HI: r = R_PPC_DTPREL16_HI; break;
1994 case BFD_RELOC_PPC_DTPREL16_HA: r = R_PPC_DTPREL16_HA; break;
1995 case BFD_RELOC_PPC_DTPREL: r = R_PPC_DTPREL32; break;
1996 case BFD_RELOC_PPC_GOT_TLSGD16: r = R_PPC_GOT_TLSGD16; break;
1997 case BFD_RELOC_PPC_GOT_TLSGD16_LO: r = R_PPC_GOT_TLSGD16_LO; break;
1998 case BFD_RELOC_PPC_GOT_TLSGD16_HI: r = R_PPC_GOT_TLSGD16_HI; break;
1999 case BFD_RELOC_PPC_GOT_TLSGD16_HA: r = R_PPC_GOT_TLSGD16_HA; break;
2000 case BFD_RELOC_PPC_GOT_TLSLD16: r = R_PPC_GOT_TLSLD16; break;
2001 case BFD_RELOC_PPC_GOT_TLSLD16_LO: r = R_PPC_GOT_TLSLD16_LO; break;
2002 case BFD_RELOC_PPC_GOT_TLSLD16_HI: r = R_PPC_GOT_TLSLD16_HI; break;
2003 case BFD_RELOC_PPC_GOT_TLSLD16_HA: r = R_PPC_GOT_TLSLD16_HA; break;
2004 case BFD_RELOC_PPC_GOT_TPREL16: r = R_PPC_GOT_TPREL16; break;
2005 case BFD_RELOC_PPC_GOT_TPREL16_LO: r = R_PPC_GOT_TPREL16_LO; break;
2006 case BFD_RELOC_PPC_GOT_TPREL16_HI: r = R_PPC_GOT_TPREL16_HI; break;
2007 case BFD_RELOC_PPC_GOT_TPREL16_HA: r = R_PPC_GOT_TPREL16_HA; break;
2008 case BFD_RELOC_PPC_GOT_DTPREL16: r = R_PPC_GOT_DTPREL16; break;
2009 case BFD_RELOC_PPC_GOT_DTPREL16_LO: r = R_PPC_GOT_DTPREL16_LO; break;
2010 case BFD_RELOC_PPC_GOT_DTPREL16_HI: r = R_PPC_GOT_DTPREL16_HI; break;
2011 case BFD_RELOC_PPC_GOT_DTPREL16_HA: r = R_PPC_GOT_DTPREL16_HA; break;
2012 case BFD_RELOC_PPC_EMB_NADDR32: r = R_PPC_EMB_NADDR32; break;
2013 case BFD_RELOC_PPC_EMB_NADDR16: r = R_PPC_EMB_NADDR16; break;
2014 case BFD_RELOC_PPC_EMB_NADDR16_LO: r = R_PPC_EMB_NADDR16_LO; break;
2015 case BFD_RELOC_PPC_EMB_NADDR16_HI: r = R_PPC_EMB_NADDR16_HI; break;
2016 case BFD_RELOC_PPC_EMB_NADDR16_HA: r = R_PPC_EMB_NADDR16_HA; break;
2017 case BFD_RELOC_PPC_EMB_SDAI16: r = R_PPC_EMB_SDAI16; break;
2018 case BFD_RELOC_PPC_EMB_SDA2I16: r = R_PPC_EMB_SDA2I16; break;
2019 case BFD_RELOC_PPC_EMB_SDA2REL: r = R_PPC_EMB_SDA2REL; break;
2020 case BFD_RELOC_PPC_EMB_SDA21: r = R_PPC_EMB_SDA21; break;
2021 case BFD_RELOC_PPC_EMB_MRKREF: r = R_PPC_EMB_MRKREF; break;
2022 case BFD_RELOC_PPC_EMB_RELSEC16: r = R_PPC_EMB_RELSEC16; break;
2023 case BFD_RELOC_PPC_EMB_RELST_LO: r = R_PPC_EMB_RELST_LO; break;
2024 case BFD_RELOC_PPC_EMB_RELST_HI: r = R_PPC_EMB_RELST_HI; break;
2025 case BFD_RELOC_PPC_EMB_RELST_HA: r = R_PPC_EMB_RELST_HA; break;
2026 case BFD_RELOC_PPC_EMB_BIT_FLD: r = R_PPC_EMB_BIT_FLD; break;
2027 case BFD_RELOC_PPC_EMB_RELSDA: r = R_PPC_EMB_RELSDA; break;
2028 case BFD_RELOC_VTABLE_INHERIT: r = R_PPC_GNU_VTINHERIT; break;
2029 case BFD_RELOC_VTABLE_ENTRY: r = R_PPC_GNU_VTENTRY; break;
2032 return ppc_elf_howto_table[(int) r];
2035 /* Set the howto pointer for a PowerPC ELF reloc. */
2037 static void
2038 ppc_elf_info_to_howto (abfd, cache_ptr, dst)
2039 bfd *abfd ATTRIBUTE_UNUSED;
2040 arelent *cache_ptr;
2041 Elf_Internal_Rela *dst;
2043 if (!ppc_elf_howto_table[R_PPC_ADDR32])
2044 /* Initialize howto table if needed. */
2045 ppc_elf_howto_init ();
2047 BFD_ASSERT (ELF32_R_TYPE (dst->r_info) < (unsigned int) R_PPC_max);
2048 cache_ptr->howto = ppc_elf_howto_table[ELF32_R_TYPE (dst->r_info)];
2051 /* Handle the R_PPC_ADDR16_HA reloc. */
2053 static bfd_reloc_status_type
2054 ppc_elf_addr16_ha_reloc (abfd, reloc_entry, symbol, data, input_section,
2055 output_bfd, error_message)
2056 bfd *abfd ATTRIBUTE_UNUSED;
2057 arelent *reloc_entry;
2058 asymbol *symbol;
2059 PTR data ATTRIBUTE_UNUSED;
2060 asection *input_section;
2061 bfd *output_bfd;
2062 char **error_message ATTRIBUTE_UNUSED;
2064 bfd_vma relocation;
2066 if (output_bfd != NULL)
2068 reloc_entry->address += input_section->output_offset;
2069 return bfd_reloc_ok;
2072 if (reloc_entry->address > input_section->_cooked_size)
2073 return bfd_reloc_outofrange;
2075 if (bfd_is_com_section (symbol->section))
2076 relocation = 0;
2077 else
2078 relocation = symbol->value;
2080 relocation += symbol->section->output_section->vma;
2081 relocation += symbol->section->output_offset;
2082 relocation += reloc_entry->addend;
2084 reloc_entry->addend += (relocation & 0x8000) << 1;
2086 return bfd_reloc_continue;
2089 static bfd_reloc_status_type
2090 ppc_elf_unhandled_reloc (abfd, reloc_entry, symbol, data,
2091 input_section, output_bfd, error_message)
2092 bfd *abfd;
2093 arelent *reloc_entry;
2094 asymbol *symbol;
2095 PTR data;
2096 asection *input_section;
2097 bfd *output_bfd;
2098 char **error_message;
2100 /* If this is a relocatable link (output_bfd test tells us), just
2101 call the generic function. Any adjustment will be done at final
2102 link time. */
2103 if (output_bfd != NULL)
2104 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2105 input_section, output_bfd, error_message);
2107 if (error_message != NULL)
2109 static char buf[60];
2110 sprintf (buf, "generic linker can't handle %s",
2111 reloc_entry->howto->name);
2112 *error_message = buf;
2114 return bfd_reloc_dangerous;
2117 /* Fix bad default arch selected for a 32 bit input bfd when the
2118 default is 64 bit. */
2120 static bfd_boolean
2121 ppc_elf_object_p (abfd)
2122 bfd *abfd;
2124 if (abfd->arch_info->the_default && abfd->arch_info->bits_per_word == 64)
2126 Elf_Internal_Ehdr *i_ehdr = elf_elfheader (abfd);
2128 if (i_ehdr->e_ident[EI_CLASS] == ELFCLASS32)
2130 /* Relies on arch after 64 bit default being 32 bit default. */
2131 abfd->arch_info = abfd->arch_info->next;
2132 BFD_ASSERT (abfd->arch_info->bits_per_word == 32);
2135 return TRUE;
2138 /* Function to set whether a module needs the -mrelocatable bit set. */
2140 static bfd_boolean
2141 ppc_elf_set_private_flags (abfd, flags)
2142 bfd *abfd;
2143 flagword flags;
2145 BFD_ASSERT (!elf_flags_init (abfd)
2146 || elf_elfheader (abfd)->e_flags == flags);
2148 elf_elfheader (abfd)->e_flags = flags;
2149 elf_flags_init (abfd) = TRUE;
2150 return TRUE;
2153 /* Merge backend specific data from an object file to the output
2154 object file when linking. */
2156 static bfd_boolean
2157 ppc_elf_merge_private_bfd_data (ibfd, obfd)
2158 bfd *ibfd;
2159 bfd *obfd;
2161 flagword old_flags;
2162 flagword new_flags;
2163 bfd_boolean error;
2165 /* Check if we have the same endianess. */
2166 if (! _bfd_generic_verify_endian_match (ibfd, obfd))
2167 return FALSE;
2169 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
2170 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
2171 return TRUE;
2173 new_flags = elf_elfheader (ibfd)->e_flags;
2174 old_flags = elf_elfheader (obfd)->e_flags;
2175 if (!elf_flags_init (obfd))
2177 /* First call, no flags set. */
2178 elf_flags_init (obfd) = TRUE;
2179 elf_elfheader (obfd)->e_flags = new_flags;
2182 /* Compatible flags are ok. */
2183 else if (new_flags == old_flags)
2186 /* Incompatible flags. */
2187 else
2189 /* Warn about -mrelocatable mismatch. Allow -mrelocatable-lib
2190 to be linked with either. */
2191 error = FALSE;
2192 if ((new_flags & EF_PPC_RELOCATABLE) != 0
2193 && (old_flags & (EF_PPC_RELOCATABLE | EF_PPC_RELOCATABLE_LIB)) == 0)
2195 error = TRUE;
2196 (*_bfd_error_handler)
2197 (_("%s: compiled with -mrelocatable and linked with modules compiled normally"),
2198 bfd_archive_filename (ibfd));
2200 else if ((new_flags & (EF_PPC_RELOCATABLE | EF_PPC_RELOCATABLE_LIB)) == 0
2201 && (old_flags & EF_PPC_RELOCATABLE) != 0)
2203 error = TRUE;
2204 (*_bfd_error_handler)
2205 (_("%s: compiled normally and linked with modules compiled with -mrelocatable"),
2206 bfd_archive_filename (ibfd));
2209 /* The output is -mrelocatable-lib iff both the input files are. */
2210 if (! (new_flags & EF_PPC_RELOCATABLE_LIB))
2211 elf_elfheader (obfd)->e_flags &= ~EF_PPC_RELOCATABLE_LIB;
2213 /* The output is -mrelocatable iff it can't be -mrelocatable-lib,
2214 but each input file is either -mrelocatable or -mrelocatable-lib. */
2215 if (! (elf_elfheader (obfd)->e_flags & EF_PPC_RELOCATABLE_LIB)
2216 && (new_flags & (EF_PPC_RELOCATABLE_LIB | EF_PPC_RELOCATABLE))
2217 && (old_flags & (EF_PPC_RELOCATABLE_LIB | EF_PPC_RELOCATABLE)))
2218 elf_elfheader (obfd)->e_flags |= EF_PPC_RELOCATABLE;
2220 /* Do not warn about eabi vs. V.4 mismatch, just or in the bit if
2221 any module uses it. */
2222 elf_elfheader (obfd)->e_flags |= (new_flags & EF_PPC_EMB);
2224 new_flags &= ~(EF_PPC_RELOCATABLE | EF_PPC_RELOCATABLE_LIB | EF_PPC_EMB);
2225 old_flags &= ~(EF_PPC_RELOCATABLE | EF_PPC_RELOCATABLE_LIB | EF_PPC_EMB);
2227 /* Warn about any other mismatches. */
2228 if (new_flags != old_flags)
2230 error = TRUE;
2231 (*_bfd_error_handler)
2232 (_("%s: uses different e_flags (0x%lx) fields than previous modules (0x%lx)"),
2233 bfd_archive_filename (ibfd), (long) new_flags, (long) old_flags);
2236 if (error)
2238 bfd_set_error (bfd_error_bad_value);
2239 return FALSE;
2243 return TRUE;
2246 /* Handle a PowerPC specific section when reading an object file. This
2247 is called when elfcode.h finds a section with an unknown type. */
2249 static bfd_boolean
2250 ppc_elf_section_from_shdr (abfd, hdr, name)
2251 bfd *abfd;
2252 Elf_Internal_Shdr *hdr;
2253 const char *name;
2255 asection *newsect;
2256 flagword flags;
2258 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name))
2259 return FALSE;
2261 newsect = hdr->bfd_section;
2262 flags = bfd_get_section_flags (abfd, newsect);
2263 if (hdr->sh_flags & SHF_EXCLUDE)
2264 flags |= SEC_EXCLUDE;
2266 if (hdr->sh_type == SHT_ORDERED)
2267 flags |= SEC_SORT_ENTRIES;
2269 bfd_set_section_flags (abfd, newsect, flags);
2270 return TRUE;
2273 /* Set up any other section flags and such that may be necessary. */
2275 static bfd_boolean
2276 ppc_elf_fake_sections (abfd, shdr, asect)
2277 bfd *abfd ATTRIBUTE_UNUSED;
2278 Elf_Internal_Shdr *shdr;
2279 asection *asect;
2281 if ((asect->flags & SEC_EXCLUDE) != 0)
2282 shdr->sh_flags |= SHF_EXCLUDE;
2284 if ((asect->flags & SEC_SORT_ENTRIES) != 0)
2285 shdr->sh_type = SHT_ORDERED;
2287 return TRUE;
2290 /* Create a special linker section */
2291 static elf_linker_section_t *
2292 ppc_elf_create_linker_section (abfd, info, which)
2293 bfd *abfd;
2294 struct bfd_link_info *info;
2295 enum elf_linker_section_enum which;
2297 bfd *dynobj = elf_hash_table (info)->dynobj;
2298 elf_linker_section_t *lsect;
2300 /* Record the first bfd section that needs the special section. */
2301 if (!dynobj)
2302 dynobj = elf_hash_table (info)->dynobj = abfd;
2304 /* If this is the first time, create the section. */
2305 lsect = elf_linker_section (dynobj, which);
2306 if (!lsect)
2308 elf_linker_section_t defaults;
2309 static elf_linker_section_t zero_section;
2311 defaults = zero_section;
2312 defaults.which = which;
2313 defaults.hole_written_p = FALSE;
2314 defaults.alignment = 2;
2316 /* Both of these sections are (technically) created by the user
2317 putting data in them, so they shouldn't be marked
2318 SEC_LINKER_CREATED.
2320 The linker creates them so it has somewhere to attach their
2321 respective symbols. In fact, if they were empty it would
2322 be OK to leave the symbol set to 0 (or any random number), because
2323 the appropriate register should never be used. */
2324 defaults.flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
2325 | SEC_IN_MEMORY);
2327 switch (which)
2329 default:
2330 (*_bfd_error_handler) (_("%s: unknown special linker type %d"),
2331 bfd_get_filename (abfd),
2332 (int) which);
2334 bfd_set_error (bfd_error_bad_value);
2335 return (elf_linker_section_t *) 0;
2337 case LINKER_SECTION_SDATA: /* .sdata/.sbss section */
2338 defaults.name = ".sdata";
2339 defaults.rel_name = ".rela.sdata";
2340 defaults.bss_name = ".sbss";
2341 defaults.sym_name = "_SDA_BASE_";
2342 defaults.sym_offset = 32768;
2343 break;
2345 case LINKER_SECTION_SDATA2: /* .sdata2/.sbss2 section */
2346 defaults.name = ".sdata2";
2347 defaults.rel_name = ".rela.sdata2";
2348 defaults.bss_name = ".sbss2";
2349 defaults.sym_name = "_SDA2_BASE_";
2350 defaults.sym_offset = 32768;
2351 defaults.flags |= SEC_READONLY;
2352 break;
2355 lsect = _bfd_elf_create_linker_section (abfd, info, which, &defaults);
2358 return lsect;
2361 /* If we have a non-zero sized .sbss2 or .PPC.EMB.sbss0 sections, we
2362 need to bump up the number of section headers. */
2364 static int
2365 ppc_elf_additional_program_headers (abfd)
2366 bfd *abfd;
2368 asection *s;
2369 int ret;
2371 ret = 0;
2373 s = bfd_get_section_by_name (abfd, ".interp");
2374 if (s != NULL)
2375 ++ret;
2377 s = bfd_get_section_by_name (abfd, ".sbss2");
2378 if (s != NULL && (s->flags & SEC_LOAD) != 0 && s->_raw_size > 0)
2379 ++ret;
2381 s = bfd_get_section_by_name (abfd, ".PPC.EMB.sbss0");
2382 if (s != NULL && (s->flags & SEC_LOAD) != 0 && s->_raw_size > 0)
2383 ++ret;
2385 return ret;
2388 /* Modify the segment map if needed. */
2390 static bfd_boolean
2391 ppc_elf_modify_segment_map (abfd)
2392 bfd *abfd ATTRIBUTE_UNUSED;
2394 return TRUE;
2397 /* The powerpc .got has a blrl instruction in it. Mark it executable. */
2399 static bfd_boolean
2400 ppc_elf_create_got (abfd, info)
2401 bfd *abfd;
2402 struct bfd_link_info *info;
2404 struct ppc_elf_link_hash_table *htab;
2405 asection *s;
2406 flagword flags;
2408 if (!_bfd_elf_create_got_section (abfd, info))
2409 return FALSE;
2411 htab = ppc_elf_hash_table (info);
2412 htab->got = s = bfd_get_section_by_name (abfd, ".got");
2413 if (s == NULL)
2414 abort ();
2416 flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_HAS_CONTENTS | SEC_IN_MEMORY
2417 | SEC_LINKER_CREATED);
2418 if (!bfd_set_section_flags (abfd, s, flags))
2419 return FALSE;
2421 htab->relgot = bfd_make_section (abfd, ".rela.got");
2422 if (!htab->relgot
2423 || ! bfd_set_section_flags (abfd, htab->relgot,
2424 (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
2425 | SEC_IN_MEMORY | SEC_LINKER_CREATED
2426 | SEC_READONLY))
2427 || ! bfd_set_section_alignment (abfd, htab->relgot, 2))
2428 return FALSE;
2430 return TRUE;
2433 /* We have to create .dynsbss and .rela.sbss here so that they get mapped
2434 to output sections (just like _bfd_elf_create_dynamic_sections has
2435 to create .dynbss and .rela.bss). */
2437 static bfd_boolean
2438 ppc_elf_create_dynamic_sections (abfd, info)
2439 bfd *abfd;
2440 struct bfd_link_info *info;
2442 struct ppc_elf_link_hash_table *htab;
2443 asection *s;
2444 flagword flags;
2446 if (!ppc_elf_create_got (abfd, info))
2447 return FALSE;
2449 if (!_bfd_elf_create_dynamic_sections (abfd, info))
2450 return FALSE;
2452 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
2453 | SEC_LINKER_CREATED);
2455 htab = ppc_elf_hash_table (info);
2456 htab->dynbss = bfd_get_section_by_name (abfd, ".dynbss");
2457 htab->dynsbss = s = bfd_make_section (abfd, ".dynsbss");
2458 if (s == NULL
2459 || ! bfd_set_section_flags (abfd, s, SEC_ALLOC))
2460 return FALSE;
2462 if (! info->shared)
2464 htab->relbss = bfd_get_section_by_name (abfd, ".rela.bss");
2465 htab->relsbss = s = bfd_make_section (abfd, ".rela.sbss");
2466 if (s == NULL
2467 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
2468 || ! bfd_set_section_alignment (abfd, s, 2))
2469 return FALSE;
2472 htab->relplt = bfd_get_section_by_name (abfd, ".rela.plt");
2473 htab->plt = s = bfd_get_section_by_name (abfd, ".plt");
2474 if (s == NULL)
2475 abort ();
2477 flags = SEC_ALLOC | SEC_CODE | SEC_IN_MEMORY | SEC_LINKER_CREATED;
2478 return bfd_set_section_flags (abfd, s, flags);
2481 /* Adjust a symbol defined by a dynamic object and referenced by a
2482 regular object. The current definition is in some section of the
2483 dynamic object, but we're not including those sections. We have to
2484 change the definition to something the rest of the link can
2485 understand. */
2487 static bfd_boolean
2488 ppc_elf_adjust_dynamic_symbol (info, h)
2489 struct bfd_link_info *info;
2490 struct elf_link_hash_entry *h;
2492 struct ppc_elf_link_hash_table *htab;
2493 asection *s;
2494 unsigned int power_of_two;
2496 #ifdef DEBUG
2497 fprintf (stderr, "ppc_elf_adjust_dynamic_symbol called for %s\n",
2498 h->root.root.string);
2499 #endif
2501 /* Make sure we know what is going on here. */
2502 htab = ppc_elf_hash_table (info);
2503 BFD_ASSERT (htab->elf.dynobj != NULL
2504 && ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT)
2505 || h->weakdef != NULL
2506 || ((h->elf_link_hash_flags
2507 & ELF_LINK_HASH_DEF_DYNAMIC) != 0
2508 && (h->elf_link_hash_flags
2509 & ELF_LINK_HASH_REF_REGULAR) != 0
2510 && (h->elf_link_hash_flags
2511 & ELF_LINK_HASH_DEF_REGULAR) == 0)));
2513 /* Deal with function syms. */
2514 if (h->type == STT_FUNC
2515 || (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0)
2517 /* Clear procedure linkage table information for any symbol that
2518 won't need a .plt entry. */
2519 if (! htab->elf.dynamic_sections_created
2520 || SYMBOL_CALLS_LOCAL (info, h)
2521 || h->plt.refcount <= 0)
2523 /* A PLT entry is not required/allowed when:
2525 1. We are not using ld.so; because then the PLT entry
2526 can't be set up, so we can't use one.
2528 2. We know for certain that a call to this symbol
2529 will go to this object.
2531 3. GC has rendered the entry unused. */
2532 h->plt.offset = (bfd_vma) -1;
2533 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
2535 return TRUE;
2537 else
2538 h->plt.offset = (bfd_vma) -1;
2540 /* If this is a weak symbol, and there is a real definition, the
2541 processor independent code will have arranged for us to see the
2542 real definition first, and we can just use the same value. */
2543 if (h->weakdef != NULL)
2545 BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined
2546 || h->weakdef->root.type == bfd_link_hash_defweak);
2547 h->root.u.def.section = h->weakdef->root.u.def.section;
2548 h->root.u.def.value = h->weakdef->root.u.def.value;
2549 if (ELIMINATE_COPY_RELOCS)
2550 h->elf_link_hash_flags
2551 = ((h->elf_link_hash_flags & ~ELF_LINK_NON_GOT_REF)
2552 | (h->weakdef->elf_link_hash_flags & ELF_LINK_NON_GOT_REF));
2553 return TRUE;
2556 /* This is a reference to a symbol defined by a dynamic object which
2557 is not a function. */
2559 /* If we are creating a shared library, we must presume that the
2560 only references to the symbol are via the global offset table.
2561 For such cases we need not do anything here; the relocations will
2562 be handled correctly by relocate_section. */
2563 if (info->shared)
2564 return TRUE;
2566 /* If there are no references to this symbol that do not use the
2567 GOT, we don't need to generate a copy reloc. */
2568 if ((h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0)
2569 return TRUE;
2571 if (ELIMINATE_COPY_RELOCS)
2573 struct ppc_elf_dyn_relocs *p;
2574 for (p = ppc_elf_hash_entry (h)->dyn_relocs; p != NULL; p = p->next)
2576 s = p->sec->output_section;
2577 if (s != NULL && (s->flags & SEC_READONLY) != 0)
2578 break;
2581 /* If we didn't find any dynamic relocs in read-only sections, then
2582 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
2583 if (p == NULL)
2585 h->elf_link_hash_flags &= ~ELF_LINK_NON_GOT_REF;
2586 return TRUE;
2590 /* We must allocate the symbol in our .dynbss section, which will
2591 become part of the .bss section of the executable. There will be
2592 an entry for this symbol in the .dynsym section. The dynamic
2593 object will contain position independent code, so all references
2594 from the dynamic object to this symbol will go through the global
2595 offset table. The dynamic linker will use the .dynsym entry to
2596 determine the address it must put in the global offset table, so
2597 both the dynamic object and the regular object will refer to the
2598 same memory location for the variable.
2600 Of course, if the symbol is sufficiently small, we must instead
2601 allocate it in .sbss. FIXME: It would be better to do this if and
2602 only if there were actually SDAREL relocs for that symbol. */
2604 if (h->size <= elf_gp_size (htab->elf.dynobj))
2605 s = htab->dynsbss;
2606 else
2607 s = htab->dynbss;
2608 BFD_ASSERT (s != NULL);
2610 /* We must generate a R_PPC_COPY reloc to tell the dynamic linker to
2611 copy the initial value out of the dynamic object and into the
2612 runtime process image. We need to remember the offset into the
2613 .rela.bss section we are going to use. */
2614 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
2616 asection *srel;
2618 if (h->size <= elf_gp_size (htab->elf.dynobj))
2619 srel = htab->relsbss;
2620 else
2621 srel = htab->relbss;
2622 BFD_ASSERT (srel != NULL);
2623 srel->_raw_size += sizeof (Elf32_External_Rela);
2624 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_COPY;
2627 /* We need to figure out the alignment required for this symbol. I
2628 have no idea how ELF linkers handle this. */
2629 power_of_two = bfd_log2 (h->size);
2630 if (power_of_two > 4)
2631 power_of_two = 4;
2633 /* Apply the required alignment. */
2634 s->_raw_size = BFD_ALIGN (s->_raw_size,
2635 (bfd_size_type) (1 << power_of_two));
2636 if (power_of_two > bfd_get_section_alignment (htab->elf.dynobj, s))
2638 if (! bfd_set_section_alignment (htab->elf.dynobj, s, power_of_two))
2639 return FALSE;
2642 /* Define the symbol as being at this point in the section. */
2643 h->root.u.def.section = s;
2644 h->root.u.def.value = s->_raw_size;
2646 /* Increment the section size to make room for the symbol. */
2647 s->_raw_size += h->size;
2649 return TRUE;
2652 /* This is the condition under which finish_dynamic_symbol will be
2653 called from elflink.h. If elflink.h doesn't call our
2654 finish_dynamic_symbol routine, we'll need to do something about
2655 initializing any .plt and .got entries in relocate_section. */
2656 #define WILL_CALL_FINISH_DYNAMIC_SYMBOL(DYN, SHARED, H) \
2657 ((DYN) \
2658 && ((SHARED) \
2659 || ((H)->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0) \
2660 && ((H)->dynindx != -1 \
2661 || ((H)->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0))
2663 /* Of those relocs that might be copied as dynamic relocs, this macro
2664 selects those that must be copied when linking a shared library,
2665 even when the symbol is local. */
2667 #define MUST_BE_DYN_RELOC(RTYPE) \
2668 ((RTYPE) != R_PPC_REL24 \
2669 && (RTYPE) != R_PPC_REL14 \
2670 && (RTYPE) != R_PPC_REL14_BRTAKEN \
2671 && (RTYPE) != R_PPC_REL14_BRNTAKEN \
2672 && (RTYPE) != R_PPC_REL32)
2674 /* Allocate space in associated reloc sections for dynamic relocs. */
2676 static bfd_boolean
2677 allocate_dynrelocs (h, inf)
2678 struct elf_link_hash_entry *h;
2679 PTR inf;
2681 struct bfd_link_info *info = (struct bfd_link_info *) inf;
2682 struct ppc_elf_link_hash_entry *eh;
2683 struct ppc_elf_link_hash_table *htab;
2684 struct ppc_elf_dyn_relocs *p;
2686 if (h->root.type == bfd_link_hash_indirect)
2687 return TRUE;
2689 if (h->root.type == bfd_link_hash_warning)
2690 /* When warning symbols are created, they **replace** the "real"
2691 entry in the hash table, thus we never get to see the real
2692 symbol in a hash traversal. So look at it now. */
2693 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2695 htab = ppc_elf_hash_table (info);
2696 if (htab->elf.dynamic_sections_created
2697 && h->plt.refcount > 0
2698 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2699 || h->root.type != bfd_link_hash_undefweak))
2701 /* Make sure this symbol is output as a dynamic symbol. */
2702 if (h->dynindx == -1
2703 && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
2705 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
2706 return FALSE;
2709 if (info->shared
2710 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h))
2712 asection *s = htab->plt;
2714 /* If this is the first .plt entry, make room for the special
2715 first entry. */
2716 if (s->_raw_size == 0)
2717 s->_raw_size += PLT_INITIAL_ENTRY_SIZE;
2719 /* The PowerPC PLT is actually composed of two parts, the
2720 first part is 2 words (for a load and a jump), and then
2721 there is a remaining word available at the end. */
2722 h->plt.offset = (PLT_INITIAL_ENTRY_SIZE
2723 + (PLT_SLOT_SIZE
2724 * ((s->_raw_size - PLT_INITIAL_ENTRY_SIZE)
2725 / PLT_ENTRY_SIZE)));
2727 /* If this symbol is not defined in a regular file, and we
2728 are not generating a shared library, then set the symbol
2729 to this location in the .plt. This is required to make
2730 function pointers compare as equal between the normal
2731 executable and the shared library. */
2732 if (! info->shared
2733 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
2735 h->root.u.def.section = s;
2736 h->root.u.def.value = h->plt.offset;
2739 /* Make room for this entry. After the 8192nd entry, room
2740 for two entries is allocated. */
2741 s->_raw_size += PLT_ENTRY_SIZE;
2742 if ((s->_raw_size - PLT_INITIAL_ENTRY_SIZE) / PLT_ENTRY_SIZE
2743 >= PLT_NUM_SINGLE_ENTRIES)
2744 s->_raw_size += PLT_ENTRY_SIZE;
2746 /* We also need to make an entry in the .rela.plt section. */
2747 htab->relplt->_raw_size += sizeof (Elf32_External_Rela);
2749 else
2751 h->plt.offset = (bfd_vma) -1;
2752 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
2755 else
2757 h->plt.offset = (bfd_vma) -1;
2758 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
2761 eh = (struct ppc_elf_link_hash_entry *) h;
2762 if (eh->elf.got.refcount > 0)
2764 /* Make sure this symbol is output as a dynamic symbol. */
2765 if (eh->elf.dynindx == -1
2766 && (eh->elf.elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
2768 if (!bfd_elf32_link_record_dynamic_symbol (info, &eh->elf))
2769 return FALSE;
2772 if (eh->tls_mask == (TLS_TLS | TLS_LD)
2773 && !(eh->elf.elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC))
2774 /* If just an LD reloc, we'll just use htab->tlsld_got.offset. */
2775 eh->elf.got.offset = (bfd_vma) -1;
2776 else
2778 bfd_boolean dyn;
2779 eh->elf.got.offset = htab->got->_raw_size;
2780 if ((eh->tls_mask & TLS_TLS) != 0)
2782 if ((eh->tls_mask & TLS_LD) != 0)
2783 htab->got->_raw_size += 8;
2784 if ((eh->tls_mask & TLS_GD) != 0)
2785 htab->got->_raw_size += 8;
2786 if ((eh->tls_mask & (TLS_TPREL | TLS_TPRELGD)) != 0)
2787 htab->got->_raw_size += 4;
2788 if ((eh->tls_mask & TLS_DTPREL) != 0)
2789 htab->got->_raw_size += 4;
2791 else
2792 htab->got->_raw_size += 4;
2793 dyn = htab->elf.dynamic_sections_created;
2794 if ((info->shared
2795 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, &eh->elf))
2796 && (ELF_ST_VISIBILITY (eh->elf.other) == STV_DEFAULT
2797 || eh->elf.root.type != bfd_link_hash_undefweak))
2799 /* All the entries we allocated need relocs. */
2800 htab->relgot->_raw_size
2801 += ((htab->got->_raw_size - eh->elf.got.offset) / 4
2802 * sizeof (Elf32_External_Rela));
2803 /* Except LD only needs one. */
2804 if ((eh->tls_mask & TLS_LD) != 0)
2805 htab->relgot->_raw_size -= sizeof (Elf32_External_Rela);
2809 else
2810 eh->elf.got.offset = (bfd_vma) -1;
2812 if (eh->dyn_relocs == NULL)
2813 return TRUE;
2815 /* In the shared -Bsymbolic case, discard space allocated for
2816 dynamic pc-relative relocs against symbols which turn out to be
2817 defined in regular objects. For the normal shared case, discard
2818 space for relocs that have become local due to symbol visibility
2819 changes. */
2820 if (info->shared)
2822 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0
2823 && ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0
2824 || info->symbolic))
2826 struct ppc_elf_dyn_relocs **pp;
2828 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
2830 p->count -= p->pc_count;
2831 p->pc_count = 0;
2832 if (p->count == 0)
2833 *pp = p->next;
2834 else
2835 pp = &p->next;
2839 /* Also discard relocs on undefined weak syms with non-default
2840 visibility. */
2841 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2842 && h->root.type == bfd_link_hash_undefweak)
2843 eh->dyn_relocs = NULL;
2845 else if (ELIMINATE_COPY_RELOCS)
2847 /* For the non-shared case, discard space for relocs against
2848 symbols which turn out to need copy relocs or are not
2849 dynamic. */
2851 if ((h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0
2852 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
2853 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
2855 /* Make sure this symbol is output as a dynamic symbol.
2856 Undefined weak syms won't yet be marked as dynamic. */
2857 if (h->dynindx == -1
2858 && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
2860 if (! bfd_elf64_link_record_dynamic_symbol (info, h))
2861 return FALSE;
2864 /* If that succeeded, we know we'll be keeping all the
2865 relocs. */
2866 if (h->dynindx != -1)
2867 goto keep;
2870 eh->dyn_relocs = NULL;
2872 keep: ;
2875 /* Finally, allocate space. */
2876 for (p = eh->dyn_relocs; p != NULL; p = p->next)
2878 asection *sreloc = elf_section_data (p->sec)->sreloc;
2879 sreloc->_raw_size += p->count * sizeof (Elf32_External_Rela);
2882 return TRUE;
2885 /* Find any dynamic relocs that apply to read-only sections. */
2887 static bfd_boolean
2888 readonly_dynrelocs (h, info)
2889 struct elf_link_hash_entry *h;
2890 PTR info;
2892 struct ppc_elf_dyn_relocs *p;
2894 if (h->root.type == bfd_link_hash_indirect)
2895 return TRUE;
2897 if (h->root.type == bfd_link_hash_warning)
2898 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2900 for (p = ppc_elf_hash_entry (h)->dyn_relocs; p != NULL; p = p->next)
2902 asection *s = p->sec->output_section;
2904 if (s != NULL
2905 && ((s->flags & (SEC_READONLY | SEC_ALLOC))
2906 == (SEC_READONLY | SEC_ALLOC)))
2908 ((struct bfd_link_info *) info)->flags |= DF_TEXTREL;
2910 /* Not an error, just cut short the traversal. */
2911 return FALSE;
2914 return TRUE;
2917 /* Set the sizes of the dynamic sections. */
2919 static bfd_boolean
2920 ppc_elf_size_dynamic_sections (output_bfd, info)
2921 bfd *output_bfd ATTRIBUTE_UNUSED;
2922 struct bfd_link_info *info;
2924 struct ppc_elf_link_hash_table *htab;
2925 asection *s;
2926 bfd_boolean relocs;
2927 bfd *ibfd;
2929 #ifdef DEBUG
2930 fprintf (stderr, "ppc_elf_size_dynamic_sections called\n");
2931 #endif
2933 htab = ppc_elf_hash_table (info);
2934 BFD_ASSERT (htab->elf.dynobj != NULL);
2936 if (elf_hash_table (info)->dynamic_sections_created)
2938 /* Set the contents of the .interp section to the interpreter. */
2939 if (! info->shared)
2941 s = bfd_get_section_by_name (htab->elf.dynobj, ".interp");
2942 BFD_ASSERT (s != NULL);
2943 s->_raw_size = sizeof ELF_DYNAMIC_INTERPRETER;
2944 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
2948 if (htab->tlsld_got.refcount > 0)
2950 htab->tlsld_got.offset = htab->got->_raw_size;
2951 htab->got->_raw_size += 8;
2952 if (info->shared)
2953 htab->relgot->_raw_size += sizeof (Elf32_External_Rela);
2955 else
2956 htab->tlsld_got.offset = (bfd_vma) -1;
2958 /* Set up .got offsets for local syms, and space for local dynamic
2959 relocs. */
2960 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
2962 bfd_signed_vma *local_got;
2963 bfd_signed_vma *end_local_got;
2964 char *lgot_masks;
2965 bfd_size_type locsymcount;
2966 Elf_Internal_Shdr *symtab_hdr;
2967 asection *srel;
2969 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
2970 continue;
2972 for (s = ibfd->sections; s != NULL; s = s->next)
2974 struct ppc_elf_dyn_relocs *p;
2976 for (p = ((struct ppc_elf_dyn_relocs *)
2977 elf_section_data (s)->local_dynrel);
2978 p != NULL;
2979 p = p->next)
2981 if (!bfd_is_abs_section (p->sec)
2982 && bfd_is_abs_section (p->sec->output_section))
2984 /* Input section has been discarded, either because
2985 it is a copy of a linkonce section or due to
2986 linker script /DISCARD/, so we'll be discarding
2987 the relocs too. */
2989 else if (p->count != 0)
2991 elf_section_data (p->sec)->sreloc->_raw_size
2992 += p->count * sizeof (Elf32_External_Rela);
2993 if ((p->sec->output_section->flags
2994 & (SEC_READONLY | SEC_ALLOC))
2995 == (SEC_READONLY | SEC_ALLOC))
2996 info->flags |= DF_TEXTREL;
3001 local_got = elf_local_got_refcounts (ibfd);
3002 if (!local_got)
3003 continue;
3005 symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
3006 locsymcount = symtab_hdr->sh_info;
3007 end_local_got = local_got + locsymcount;
3008 lgot_masks = (char *) end_local_got;
3009 s = htab->got;
3010 srel = htab->relgot;
3011 for (; local_got < end_local_got; ++local_got, ++lgot_masks)
3012 if (*local_got > 0)
3014 if (*lgot_masks == (TLS_TLS | TLS_LD))
3016 /* If just an LD reloc, we'll just use
3017 htab->tlsld_got.offset. */
3018 if (htab->tlsld_got.offset == (bfd_vma) -1)
3020 htab->tlsld_got.offset = s->_raw_size;
3021 s->_raw_size += 8;
3022 if (info->shared)
3023 srel->_raw_size += sizeof (Elf32_External_Rela);
3025 *local_got = (bfd_vma) -1;
3027 else
3029 *local_got = s->_raw_size;
3030 if ((*lgot_masks & TLS_TLS) != 0)
3032 if ((*lgot_masks & TLS_GD) != 0)
3033 s->_raw_size += 8;
3034 if ((*lgot_masks & (TLS_TPREL | TLS_TPRELGD)) != 0)
3035 s->_raw_size += 4;
3036 if ((*lgot_masks & TLS_DTPREL) != 0)
3037 s->_raw_size += 4;
3039 else
3040 s->_raw_size += 4;
3041 if (info->shared)
3042 srel->_raw_size += ((s->_raw_size - *local_got) / 4
3043 * sizeof (Elf32_External_Rela));
3046 else
3047 *local_got = (bfd_vma) -1;
3050 /* Allocate space for global sym dynamic relocs. */
3051 elf_link_hash_traverse (elf_hash_table (info), allocate_dynrelocs, info);
3053 /* We've now determined the sizes of the various dynamic sections.
3054 Allocate memory for them. */
3055 relocs = FALSE;
3056 for (s = htab->elf.dynobj->sections; s != NULL; s = s->next)
3058 if ((s->flags & SEC_LINKER_CREATED) == 0)
3059 continue;
3061 if (s == htab->plt
3062 || s == htab->got
3063 || (htab->sdata != NULL && s == htab->sdata->section)
3064 || (htab->sdata2 != NULL && s == htab->sdata2->section))
3066 /* Strip this section if we don't need it; see the
3067 comment below. */
3069 else if (strncmp (bfd_get_section_name (dynobj, s), ".rela", 5) == 0)
3071 if (s->_raw_size == 0)
3073 /* If we don't need this section, strip it from the
3074 output file. This is mostly to handle .rela.bss and
3075 .rela.plt. We must create both sections in
3076 create_dynamic_sections, because they must be created
3077 before the linker maps input sections to output
3078 sections. The linker does that before
3079 adjust_dynamic_symbol is called, and it is that
3080 function which decides whether anything needs to go
3081 into these sections. */
3083 else
3085 /* Remember whether there are any relocation sections. */
3086 relocs = TRUE;
3088 /* We use the reloc_count field as a counter if we need
3089 to copy relocs into the output file. */
3090 s->reloc_count = 0;
3093 else
3095 /* It's not one of our sections, so don't allocate space. */
3096 continue;
3099 if (s->_raw_size == 0)
3101 _bfd_strip_section_from_output (info, s);
3102 continue;
3105 /* Allocate memory for the section contents. */
3106 s->contents = (bfd_byte *) bfd_zalloc (htab->elf.dynobj, s->_raw_size);
3107 if (s->contents == NULL)
3108 return FALSE;
3111 if (htab->elf.dynamic_sections_created)
3113 /* Add some entries to the .dynamic section. We fill in the
3114 values later, in ppc_elf_finish_dynamic_sections, but we
3115 must add the entries now so that we get the correct size for
3116 the .dynamic section. The DT_DEBUG entry is filled in by the
3117 dynamic linker and used by the debugger. */
3118 #define add_dynamic_entry(TAG, VAL) \
3119 bfd_elf32_add_dynamic_entry (info, (bfd_vma) (TAG), (bfd_vma) (VAL))
3121 if (!info->shared)
3123 if (!add_dynamic_entry (DT_DEBUG, 0))
3124 return FALSE;
3127 if (htab->plt != NULL && htab->plt->_raw_size != 0)
3129 if (!add_dynamic_entry (DT_PLTGOT, 0)
3130 || !add_dynamic_entry (DT_PLTRELSZ, 0)
3131 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
3132 || !add_dynamic_entry (DT_JMPREL, 0))
3133 return FALSE;
3136 if (relocs)
3138 if (!add_dynamic_entry (DT_RELA, 0)
3139 || !add_dynamic_entry (DT_RELASZ, 0)
3140 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf32_External_Rela)))
3141 return FALSE;
3144 /* If any dynamic relocs apply to a read-only section, then we
3145 need a DT_TEXTREL entry. */
3146 if ((info->flags & DF_TEXTREL) == 0)
3147 elf_link_hash_traverse (elf_hash_table (info), readonly_dynrelocs,
3148 (PTR) info);
3150 if ((info->flags & DF_TEXTREL) != 0)
3152 if (!add_dynamic_entry (DT_TEXTREL, 0))
3153 return FALSE;
3156 #undef add_dynamic_entry
3158 return TRUE;
3161 static bfd_boolean
3162 update_local_sym_info (abfd, symtab_hdr, r_symndx, tls_type)
3163 bfd *abfd;
3164 Elf_Internal_Shdr *symtab_hdr;
3165 unsigned long r_symndx;
3166 int tls_type;
3168 bfd_signed_vma *local_got_refcounts = elf_local_got_refcounts (abfd);
3169 char *local_got_tls_masks;
3171 if (local_got_refcounts == NULL)
3173 bfd_size_type size = symtab_hdr->sh_info;
3175 size *= sizeof (*local_got_refcounts) + sizeof (*local_got_tls_masks);
3176 local_got_refcounts = (bfd_signed_vma *) bfd_zalloc (abfd, size);
3177 if (local_got_refcounts == NULL)
3178 return FALSE;
3179 elf_local_got_refcounts (abfd) = local_got_refcounts;
3182 local_got_refcounts[r_symndx] += 1;
3183 local_got_tls_masks = (char *) (local_got_refcounts + symtab_hdr->sh_info);
3184 local_got_tls_masks[r_symndx] |= tls_type;
3185 return TRUE;
3188 static void
3189 bad_shared_reloc (abfd, r_type)
3190 bfd *abfd;
3191 enum elf_ppc_reloc_type r_type;
3193 (*_bfd_error_handler)
3194 (_("%s: relocation %s cannot be used when making a shared object"),
3195 bfd_archive_filename (abfd),
3196 ppc_elf_howto_table[(int) r_type]->name);
3197 bfd_set_error (bfd_error_bad_value);
3200 /* Look through the relocs for a section during the first phase, and
3201 allocate space in the global offset table or procedure linkage
3202 table. */
3204 static bfd_boolean
3205 ppc_elf_check_relocs (abfd, info, sec, relocs)
3206 bfd *abfd;
3207 struct bfd_link_info *info;
3208 asection *sec;
3209 const Elf_Internal_Rela *relocs;
3211 struct ppc_elf_link_hash_table *htab;
3212 Elf_Internal_Shdr *symtab_hdr;
3213 struct elf_link_hash_entry **sym_hashes;
3214 const Elf_Internal_Rela *rel;
3215 const Elf_Internal_Rela *rel_end;
3216 asection *sreloc;
3218 if (info->relocateable)
3219 return TRUE;
3221 #ifdef DEBUG
3222 fprintf (stderr, "ppc_elf_check_relocs called for section %s in %s\n",
3223 bfd_get_section_name (abfd, sec),
3224 bfd_archive_filename (abfd));
3225 #endif
3227 /* Create the linker generated sections all the time so that the
3228 special symbols are created. */
3230 htab = ppc_elf_hash_table (info);
3231 if (htab->sdata == NULL)
3233 htab->sdata = elf_linker_section (abfd, LINKER_SECTION_SDATA);
3234 if (htab->sdata == NULL)
3235 htab->sdata = ppc_elf_create_linker_section (abfd, info,
3236 LINKER_SECTION_SDATA);
3237 if (htab->sdata == NULL)
3238 return FALSE;
3241 if (htab->sdata2 == NULL)
3243 htab->sdata2 = elf_linker_section (abfd, LINKER_SECTION_SDATA2);
3244 if (htab->sdata2 == NULL)
3245 htab->sdata2 = ppc_elf_create_linker_section (abfd, info,
3246 LINKER_SECTION_SDATA2);
3247 if (htab->sdata2 == NULL)
3248 return FALSE;
3251 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
3252 sym_hashes = elf_sym_hashes (abfd);
3253 sreloc = NULL;
3255 rel_end = relocs + sec->reloc_count;
3256 for (rel = relocs; rel < rel_end; rel++)
3258 unsigned long r_symndx;
3259 enum elf_ppc_reloc_type r_type;
3260 struct elf_link_hash_entry *h;
3261 int tls_type = 0;
3263 r_symndx = ELF32_R_SYM (rel->r_info);
3264 if (r_symndx < symtab_hdr->sh_info)
3265 h = NULL;
3266 else
3267 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
3269 /* If a relocation refers to _GLOBAL_OFFSET_TABLE_, create the .got.
3270 This shows up in particular in an R_PPC_ADDR32 in the eabi
3271 startup code. */
3272 if (h && strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
3274 if (htab->got == NULL)
3276 if (htab->elf.dynobj == NULL)
3277 htab->elf.dynobj = abfd;
3278 if (!ppc_elf_create_got (htab->elf.dynobj, info))
3279 return FALSE;
3283 r_type = (enum elf_ppc_reloc_type) ELF32_R_TYPE (rel->r_info);
3284 switch (r_type)
3286 case R_PPC_GOT_TLSLD16:
3287 case R_PPC_GOT_TLSLD16_LO:
3288 case R_PPC_GOT_TLSLD16_HI:
3289 case R_PPC_GOT_TLSLD16_HA:
3290 htab->tlsld_got.refcount += 1;
3291 tls_type = TLS_TLS | TLS_LD;
3292 goto dogottls;
3294 case R_PPC_GOT_TLSGD16:
3295 case R_PPC_GOT_TLSGD16_LO:
3296 case R_PPC_GOT_TLSGD16_HI:
3297 case R_PPC_GOT_TLSGD16_HA:
3298 tls_type = TLS_TLS | TLS_GD;
3299 goto dogottls;
3301 case R_PPC_GOT_TPREL16:
3302 case R_PPC_GOT_TPREL16_LO:
3303 case R_PPC_GOT_TPREL16_HI:
3304 case R_PPC_GOT_TPREL16_HA:
3305 if (info->shared)
3306 info->flags |= DF_STATIC_TLS;
3307 tls_type = TLS_TLS | TLS_TPREL;
3308 goto dogottls;
3310 case R_PPC_GOT_DTPREL16:
3311 case R_PPC_GOT_DTPREL16_LO:
3312 case R_PPC_GOT_DTPREL16_HI:
3313 case R_PPC_GOT_DTPREL16_HA:
3314 tls_type = TLS_TLS | TLS_DTPREL;
3315 dogottls:
3316 sec->has_tls_reloc = 1;
3317 /* Fall thru */
3319 /* GOT16 relocations */
3320 case R_PPC_GOT16:
3321 case R_PPC_GOT16_LO:
3322 case R_PPC_GOT16_HI:
3323 case R_PPC_GOT16_HA:
3324 /* This symbol requires a global offset table entry. */
3325 if (htab->got == NULL)
3327 if (htab->elf.dynobj == NULL)
3328 htab->elf.dynobj = abfd;
3329 if (!ppc_elf_create_got (htab->elf.dynobj, info))
3330 return FALSE;
3332 if (h != NULL)
3334 h->got.refcount += 1;
3335 ppc_elf_hash_entry (h)->tls_mask |= tls_type;
3337 else
3338 /* This is a global offset table entry for a local symbol. */
3339 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx, tls_type))
3340 return FALSE;
3341 break;
3343 /* Indirect .sdata relocation. */
3344 case R_PPC_EMB_SDAI16:
3345 if (info->shared)
3347 bad_shared_reloc (abfd, r_type);
3348 return FALSE;
3350 if (!bfd_elf32_create_pointer_linker_section (abfd, info,
3351 htab->sdata, h, rel))
3352 return FALSE;
3353 break;
3355 /* Indirect .sdata2 relocation. */
3356 case R_PPC_EMB_SDA2I16:
3357 if (info->shared)
3359 bad_shared_reloc (abfd, r_type);
3360 return FALSE;
3362 if (!bfd_elf32_create_pointer_linker_section (abfd, info,
3363 htab->sdata2, h, rel))
3364 return FALSE;
3365 break;
3367 case R_PPC_SDAREL16:
3368 case R_PPC_EMB_SDA2REL:
3369 case R_PPC_EMB_SDA21:
3370 case R_PPC_EMB_RELSDA:
3371 case R_PPC_EMB_NADDR32:
3372 case R_PPC_EMB_NADDR16:
3373 case R_PPC_EMB_NADDR16_LO:
3374 case R_PPC_EMB_NADDR16_HI:
3375 case R_PPC_EMB_NADDR16_HA:
3376 if (info->shared)
3378 bad_shared_reloc (abfd, r_type);
3379 return FALSE;
3381 break;
3383 case R_PPC_PLT32:
3384 case R_PPC_PLTREL24:
3385 case R_PPC_PLTREL32:
3386 case R_PPC_PLT16_LO:
3387 case R_PPC_PLT16_HI:
3388 case R_PPC_PLT16_HA:
3389 #ifdef DEBUG
3390 fprintf (stderr, "Reloc requires a PLT entry\n");
3391 #endif
3392 /* This symbol requires a procedure linkage table entry. We
3393 actually build the entry in finish_dynamic_symbol,
3394 because this might be a case of linking PIC code without
3395 linking in any dynamic objects, in which case we don't
3396 need to generate a procedure linkage table after all. */
3398 if (h == NULL)
3400 /* It does not make sense to have a procedure linkage
3401 table entry for a local symbol. */
3402 bfd_set_error (bfd_error_bad_value);
3403 return FALSE;
3406 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
3407 h->plt.refcount++;
3408 break;
3410 /* The following relocations don't need to propagate the
3411 relocation if linking a shared object since they are
3412 section relative. */
3413 case R_PPC_SECTOFF:
3414 case R_PPC_SECTOFF_LO:
3415 case R_PPC_SECTOFF_HI:
3416 case R_PPC_SECTOFF_HA:
3417 case R_PPC_DTPREL16:
3418 case R_PPC_DTPREL16_LO:
3419 case R_PPC_DTPREL16_HI:
3420 case R_PPC_DTPREL16_HA:
3421 case R_PPC_TOC16:
3422 break;
3424 /* This are just markers. */
3425 case R_PPC_TLS:
3426 case R_PPC_EMB_MRKREF:
3427 case R_PPC_NONE:
3428 case R_PPC_max:
3429 break;
3431 /* These should only appear in dynamic objects. */
3432 case R_PPC_COPY:
3433 case R_PPC_GLOB_DAT:
3434 case R_PPC_JMP_SLOT:
3435 case R_PPC_RELATIVE:
3436 break;
3438 /* These aren't handled yet. We'll report an error later. */
3439 case R_PPC_ADDR30:
3440 case R_PPC_EMB_RELSEC16:
3441 case R_PPC_EMB_RELST_LO:
3442 case R_PPC_EMB_RELST_HI:
3443 case R_PPC_EMB_RELST_HA:
3444 case R_PPC_EMB_BIT_FLD:
3445 break;
3447 /* This refers only to functions defined in the shared library. */
3448 case R_PPC_LOCAL24PC:
3449 break;
3451 /* This relocation describes the C++ object vtable hierarchy.
3452 Reconstruct it for later use during GC. */
3453 case R_PPC_GNU_VTINHERIT:
3454 if (!_bfd_elf32_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
3455 return FALSE;
3456 break;
3458 /* This relocation describes which C++ vtable entries are actually
3459 used. Record for later use during GC. */
3460 case R_PPC_GNU_VTENTRY:
3461 if (!_bfd_elf32_gc_record_vtentry (abfd, sec, h, rel->r_addend))
3462 return FALSE;
3463 break;
3465 /* We shouldn't really be seeing these. */
3466 case R_PPC_TPREL32:
3467 if (info->shared)
3468 info->flags |= DF_STATIC_TLS;
3469 goto dodyn;
3471 /* Nor these. */
3472 case R_PPC_DTPMOD32:
3473 case R_PPC_DTPREL32:
3474 goto dodyn;
3476 case R_PPC_TPREL16:
3477 case R_PPC_TPREL16_LO:
3478 case R_PPC_TPREL16_HI:
3479 case R_PPC_TPREL16_HA:
3480 if (info->shared)
3481 info->flags |= DF_STATIC_TLS;
3482 goto dodyn;
3484 /* When creating a shared object, we must copy these
3485 relocs into the output file. We create a reloc
3486 section in dynobj and make room for the reloc. */
3487 case R_PPC_REL24:
3488 case R_PPC_REL14:
3489 case R_PPC_REL14_BRTAKEN:
3490 case R_PPC_REL14_BRNTAKEN:
3491 case R_PPC_REL32:
3492 if (h == NULL
3493 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
3494 break;
3495 /* fall through */
3497 case R_PPC_ADDR32:
3498 case R_PPC_ADDR24:
3499 case R_PPC_ADDR16:
3500 case R_PPC_ADDR16_LO:
3501 case R_PPC_ADDR16_HI:
3502 case R_PPC_ADDR16_HA:
3503 case R_PPC_ADDR14:
3504 case R_PPC_ADDR14_BRTAKEN:
3505 case R_PPC_ADDR14_BRNTAKEN:
3506 case R_PPC_UADDR32:
3507 case R_PPC_UADDR16:
3508 if (h != NULL && !info->shared)
3510 /* We may need a plt entry if the symbol turns out to be
3511 a function defined in a dynamic object. */
3512 h->plt.refcount++;
3514 /* We may need a copy reloc too. */
3515 h->elf_link_hash_flags |= ELF_LINK_NON_GOT_REF;
3518 dodyn:
3519 /* If we are creating a shared library, and this is a reloc
3520 against a global symbol, or a non PC relative reloc
3521 against a local symbol, then we need to copy the reloc
3522 into the shared library. However, if we are linking with
3523 -Bsymbolic, we do not need to copy a reloc against a
3524 global symbol which is defined in an object we are
3525 including in the link (i.e., DEF_REGULAR is set). At
3526 this point we have not seen all the input files, so it is
3527 possible that DEF_REGULAR is not set now but will be set
3528 later (it is never cleared). In case of a weak definition,
3529 DEF_REGULAR may be cleared later by a strong definition in
3530 a shared library. We account for that possibility below by
3531 storing information in the dyn_relocs field of the hash
3532 table entry. A similar situation occurs when creating
3533 shared libraries and symbol visibility changes render the
3534 symbol local.
3536 If on the other hand, we are creating an executable, we
3537 may need to keep relocations for symbols satisfied by a
3538 dynamic library if we manage to avoid copy relocs for the
3539 symbol. */
3540 if ((info->shared
3541 && (MUST_BE_DYN_RELOC (r_type)
3542 || (h != NULL
3543 && (! info->symbolic
3544 || h->root.type == bfd_link_hash_defweak
3545 || (h->elf_link_hash_flags
3546 & ELF_LINK_HASH_DEF_REGULAR) == 0))))
3547 || (ELIMINATE_COPY_RELOCS
3548 && !info->shared
3549 && (sec->flags & SEC_ALLOC) != 0
3550 && h != NULL
3551 && (h->root.type == bfd_link_hash_defweak
3552 || (h->elf_link_hash_flags
3553 & ELF_LINK_HASH_DEF_REGULAR) == 0)))
3555 struct ppc_elf_dyn_relocs *p;
3556 struct ppc_elf_dyn_relocs **head;
3558 #ifdef DEBUG
3559 fprintf (stderr, "ppc_elf_check_relocs need to create relocation for %s\n",
3560 (h && h->root.root.string
3561 ? h->root.root.string : "<unknown>"));
3562 #endif
3563 if (sreloc == NULL)
3565 const char *name;
3567 name = (bfd_elf_string_from_elf_section
3568 (abfd,
3569 elf_elfheader (abfd)->e_shstrndx,
3570 elf_section_data (sec)->rel_hdr.sh_name));
3571 if (name == NULL)
3572 return FALSE;
3574 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
3575 && strcmp (bfd_get_section_name (abfd, sec),
3576 name + 5) == 0);
3578 sreloc = bfd_get_section_by_name (htab->elf.dynobj, name);
3579 if (sreloc == NULL)
3581 flagword flags;
3583 sreloc = bfd_make_section (htab->elf.dynobj, name);
3584 flags = (SEC_HAS_CONTENTS | SEC_READONLY
3585 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3586 if ((sec->flags & SEC_ALLOC) != 0)
3587 flags |= SEC_ALLOC | SEC_LOAD;
3588 if (sreloc == NULL
3589 || ! bfd_set_section_flags (htab->elf.dynobj,
3590 sreloc, flags)
3591 || ! bfd_set_section_alignment (htab->elf.dynobj,
3592 sreloc, 2))
3593 return FALSE;
3595 elf_section_data (sec)->sreloc = sreloc;
3598 /* If this is a global symbol, we count the number of
3599 relocations we need for this symbol. */
3600 if (h != NULL)
3602 head = &ppc_elf_hash_entry (h)->dyn_relocs;
3604 else
3606 /* Track dynamic relocs needed for local syms too.
3607 We really need local syms available to do this
3608 easily. Oh well. */
3610 asection *s;
3611 s = bfd_section_from_r_symndx (abfd, &htab->sym_sec,
3612 sec, r_symndx);
3613 if (s == NULL)
3614 return FALSE;
3616 head = ((struct ppc_elf_dyn_relocs **)
3617 &elf_section_data (s)->local_dynrel);
3620 p = *head;
3621 if (p == NULL || p->sec != sec)
3623 p = ((struct ppc_elf_dyn_relocs *)
3624 bfd_alloc (htab->elf.dynobj, sizeof *p));
3625 if (p == NULL)
3626 return FALSE;
3627 p->next = *head;
3628 *head = p;
3629 p->sec = sec;
3630 p->count = 0;
3631 p->pc_count = 0;
3634 p->count += 1;
3635 if (!MUST_BE_DYN_RELOC (r_type))
3636 p->pc_count += 1;
3639 break;
3643 return TRUE;
3646 /* Return the section that should be marked against GC for a given
3647 relocation. */
3649 static asection *
3650 ppc_elf_gc_mark_hook (sec, info, rel, h, sym)
3651 asection *sec;
3652 struct bfd_link_info *info ATTRIBUTE_UNUSED;
3653 Elf_Internal_Rela *rel;
3654 struct elf_link_hash_entry *h;
3655 Elf_Internal_Sym *sym;
3657 if (h != NULL)
3659 switch (ELF32_R_TYPE (rel->r_info))
3661 case R_PPC_GNU_VTINHERIT:
3662 case R_PPC_GNU_VTENTRY:
3663 break;
3665 default:
3666 switch (h->root.type)
3668 case bfd_link_hash_defined:
3669 case bfd_link_hash_defweak:
3670 return h->root.u.def.section;
3672 case bfd_link_hash_common:
3673 return h->root.u.c.p->section;
3675 default:
3676 break;
3680 else
3681 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
3683 return NULL;
3686 /* Update the got, plt and dynamic reloc reference counts for the
3687 section being removed. */
3689 static bfd_boolean
3690 ppc_elf_gc_sweep_hook (abfd, info, sec, relocs)
3691 bfd *abfd;
3692 struct bfd_link_info *info;
3693 asection *sec;
3694 const Elf_Internal_Rela *relocs;
3696 struct ppc_elf_link_hash_table *htab;
3697 Elf_Internal_Shdr *symtab_hdr;
3698 struct elf_link_hash_entry **sym_hashes;
3699 bfd_signed_vma *local_got_refcounts;
3700 const Elf_Internal_Rela *rel, *relend;
3702 elf_section_data (sec)->local_dynrel = NULL;
3704 htab = ppc_elf_hash_table (info);
3705 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
3706 sym_hashes = elf_sym_hashes (abfd);
3707 local_got_refcounts = elf_local_got_refcounts (abfd);
3709 relend = relocs + sec->reloc_count;
3710 for (rel = relocs; rel < relend; rel++)
3712 unsigned long r_symndx;
3713 enum elf_ppc_reloc_type r_type;
3714 struct elf_link_hash_entry *h = NULL;
3716 r_symndx = ELF32_R_SYM (rel->r_info);
3717 if (r_symndx >= symtab_hdr->sh_info)
3719 struct ppc_elf_dyn_relocs **pp, *p;
3720 struct ppc_elf_link_hash_entry *eh;
3722 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
3723 eh = (struct ppc_elf_link_hash_entry *) h;
3725 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
3726 if (p->sec == sec)
3728 /* Everything must go for SEC. */
3729 *pp = p->next;
3730 break;
3734 r_type = (enum elf_ppc_reloc_type) ELF32_R_TYPE (rel->r_info);
3735 switch (r_type)
3737 case R_PPC_GOT_TLSLD16:
3738 case R_PPC_GOT_TLSLD16_LO:
3739 case R_PPC_GOT_TLSLD16_HI:
3740 case R_PPC_GOT_TLSLD16_HA:
3741 htab->tlsld_got.refcount -= 1;
3742 /* Fall thru */
3744 case R_PPC_GOT_TLSGD16:
3745 case R_PPC_GOT_TLSGD16_LO:
3746 case R_PPC_GOT_TLSGD16_HI:
3747 case R_PPC_GOT_TLSGD16_HA:
3748 case R_PPC_GOT_TPREL16:
3749 case R_PPC_GOT_TPREL16_LO:
3750 case R_PPC_GOT_TPREL16_HI:
3751 case R_PPC_GOT_TPREL16_HA:
3752 case R_PPC_GOT_DTPREL16:
3753 case R_PPC_GOT_DTPREL16_LO:
3754 case R_PPC_GOT_DTPREL16_HI:
3755 case R_PPC_GOT_DTPREL16_HA:
3756 case R_PPC_GOT16:
3757 case R_PPC_GOT16_LO:
3758 case R_PPC_GOT16_HI:
3759 case R_PPC_GOT16_HA:
3760 if (h != NULL)
3762 if (h->got.refcount > 0)
3763 h->got.refcount--;
3765 else if (local_got_refcounts != NULL)
3767 if (local_got_refcounts[r_symndx] > 0)
3768 local_got_refcounts[r_symndx]--;
3770 break;
3772 case R_PPC_REL24:
3773 case R_PPC_REL14:
3774 case R_PPC_REL14_BRTAKEN:
3775 case R_PPC_REL14_BRNTAKEN:
3776 case R_PPC_REL32:
3777 if (h == NULL
3778 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
3779 break;
3780 /* Fall thru */
3782 case R_PPC_ADDR32:
3783 case R_PPC_ADDR24:
3784 case R_PPC_ADDR16:
3785 case R_PPC_ADDR16_LO:
3786 case R_PPC_ADDR16_HI:
3787 case R_PPC_ADDR16_HA:
3788 case R_PPC_ADDR14:
3789 case R_PPC_ADDR14_BRTAKEN:
3790 case R_PPC_ADDR14_BRNTAKEN:
3791 case R_PPC_UADDR32:
3792 case R_PPC_UADDR16:
3793 case R_PPC_PLT32:
3794 case R_PPC_PLTREL24:
3795 case R_PPC_PLT16_LO:
3796 case R_PPC_PLT16_HI:
3797 case R_PPC_PLT16_HA:
3798 if (h != NULL)
3800 if (h->plt.refcount > 0)
3801 h->plt.refcount--;
3803 break;
3805 default:
3806 break;
3809 return TRUE;
3812 /* Set htab->tls_sec and htab->tls_get_addr. */
3814 bfd_boolean
3815 ppc_elf_tls_setup (obfd, info)
3816 bfd *obfd;
3817 struct bfd_link_info *info;
3819 asection *tls;
3820 struct ppc_elf_link_hash_table *htab;
3822 htab = ppc_elf_hash_table (info);
3823 htab->tls_get_addr = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
3824 FALSE, FALSE, TRUE);
3826 for (tls = obfd->sections; tls != NULL; tls = tls->next)
3827 if ((tls->flags & (SEC_THREAD_LOCAL | SEC_LOAD))
3828 == (SEC_THREAD_LOCAL | SEC_LOAD))
3829 break;
3830 htab->tls_sec = tls;
3832 return tls != NULL;
3835 /* Run through all the TLS relocs looking for optimization
3836 opportunities. */
3838 bfd_boolean
3839 ppc_elf_tls_optimize (obfd, info)
3840 bfd *obfd ATTRIBUTE_UNUSED;
3841 struct bfd_link_info *info;
3843 bfd *ibfd;
3844 asection *sec;
3845 struct ppc_elf_link_hash_table *htab;
3847 if (info->relocateable || info->shared)
3848 return TRUE;
3850 htab = ppc_elf_hash_table (info);
3851 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
3853 Elf_Internal_Sym *locsyms = NULL;
3854 Elf_Internal_Shdr *symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
3856 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
3857 if (sec->has_tls_reloc && !bfd_is_abs_section (sec->output_section))
3859 Elf_Internal_Rela *relstart, *rel, *relend;
3860 int expecting_tls_get_addr;
3862 /* Read the relocations. */
3863 relstart = _bfd_elf_link_read_relocs (ibfd, sec, (PTR) NULL,
3864 (Elf_Internal_Rela *) NULL,
3865 info->keep_memory);
3866 if (relstart == NULL)
3867 return FALSE;
3869 expecting_tls_get_addr = 0;
3870 relend = relstart + sec->reloc_count;
3871 for (rel = relstart; rel < relend; rel++)
3873 enum elf_ppc_reloc_type r_type;
3874 unsigned long r_symndx;
3875 struct elf_link_hash_entry *h = NULL;
3876 char *tls_mask;
3877 char tls_set, tls_clear;
3878 bfd_boolean is_local;
3880 r_symndx = ELF32_R_SYM (rel->r_info);
3881 if (r_symndx >= symtab_hdr->sh_info)
3883 struct elf_link_hash_entry **sym_hashes;
3885 sym_hashes = elf_sym_hashes (ibfd);
3886 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
3887 while (h->root.type == bfd_link_hash_indirect
3888 || h->root.type == bfd_link_hash_warning)
3889 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3892 is_local = FALSE;
3893 if (h == NULL
3894 || !(h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC))
3895 is_local = TRUE;
3897 r_type = (enum elf_ppc_reloc_type) ELF32_R_TYPE (rel->r_info);
3898 switch (r_type)
3900 case R_PPC_GOT_TLSLD16:
3901 case R_PPC_GOT_TLSLD16_LO:
3902 case R_PPC_GOT_TLSLD16_HI:
3903 case R_PPC_GOT_TLSLD16_HA:
3904 /* These relocs should never be against a symbol
3905 defined in a shared lib. Leave them alone if
3906 that turns out to be the case. */
3907 expecting_tls_get_addr = 0;
3908 htab->tlsld_got.refcount -= 1;
3909 if (!is_local)
3910 continue;
3912 /* LD -> LE */
3913 tls_set = 0;
3914 tls_clear = TLS_LD;
3915 expecting_tls_get_addr = 1;
3916 break;
3918 case R_PPC_GOT_TLSGD16:
3919 case R_PPC_GOT_TLSGD16_LO:
3920 case R_PPC_GOT_TLSGD16_HI:
3921 case R_PPC_GOT_TLSGD16_HA:
3922 if (is_local)
3923 /* GD -> LE */
3924 tls_set = 0;
3925 else
3926 /* GD -> IE */
3927 tls_set = TLS_TLS | TLS_TPRELGD;
3928 tls_clear = TLS_GD;
3929 expecting_tls_get_addr = 1;
3930 break;
3932 case R_PPC_GOT_TPREL16:
3933 case R_PPC_GOT_TPREL16_LO:
3934 case R_PPC_GOT_TPREL16_HI:
3935 case R_PPC_GOT_TPREL16_HA:
3936 expecting_tls_get_addr = 0;
3937 if (is_local)
3939 /* IE -> LE */
3940 tls_set = 0;
3941 tls_clear = TLS_TPREL;
3942 break;
3944 else
3945 continue;
3947 case R_PPC_REL14:
3948 case R_PPC_REL14_BRTAKEN:
3949 case R_PPC_REL14_BRNTAKEN:
3950 case R_PPC_REL24:
3951 if (expecting_tls_get_addr
3952 && h != NULL
3953 && h == htab->tls_get_addr)
3955 if (h->plt.refcount > 0)
3956 h->plt.refcount -= 1;
3958 expecting_tls_get_addr = 0;
3959 continue;
3961 default:
3962 expecting_tls_get_addr = 0;
3963 continue;
3966 if (h != NULL)
3968 if (tls_set == 0)
3970 /* We managed to get rid of a got entry. */
3971 if (h->got.refcount > 0)
3972 h->got.refcount -= 1;
3974 tls_mask = &ppc_elf_hash_entry (h)->tls_mask;
3976 else
3978 Elf_Internal_Sym *sym;
3979 bfd_signed_vma *lgot_refs;
3980 char *lgot_masks;
3982 if (locsyms == NULL)
3984 locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
3985 if (locsyms == NULL)
3986 locsyms = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
3987 symtab_hdr->sh_info,
3988 0, NULL, NULL, NULL);
3989 if (locsyms == NULL)
3991 if (elf_section_data (sec)->relocs != relstart)
3992 free (relstart);
3993 return FALSE;
3996 sym = locsyms + r_symndx;
3997 lgot_refs = elf_local_got_refcounts (ibfd);
3998 if (lgot_refs == NULL)
3999 abort ();
4000 if (tls_set == 0)
4002 /* We managed to get rid of a got entry. */
4003 if (lgot_refs[r_symndx] > 0)
4004 lgot_refs[r_symndx] -= 1;
4006 lgot_masks = (char *) (lgot_refs + symtab_hdr->sh_info);
4007 tls_mask = &lgot_masks[r_symndx];
4010 *tls_mask |= tls_set;
4011 *tls_mask &= ~tls_clear;
4014 if (elf_section_data (sec)->relocs != relstart)
4015 free (relstart);
4018 if (locsyms != NULL
4019 && (symtab_hdr->contents != (unsigned char *) locsyms))
4021 if (!info->keep_memory)
4022 free (locsyms);
4023 else
4024 symtab_hdr->contents = (unsigned char *) locsyms;
4027 return TRUE;
4030 /* Hook called by the linker routine which adds symbols from an object
4031 file. We use it to put .comm items in .sbss, and not .bss. */
4033 static bfd_boolean
4034 ppc_elf_add_symbol_hook (abfd, info, sym, namep, flagsp, secp, valp)
4035 bfd *abfd;
4036 struct bfd_link_info *info;
4037 const Elf_Internal_Sym *sym;
4038 const char **namep ATTRIBUTE_UNUSED;
4039 flagword *flagsp ATTRIBUTE_UNUSED;
4040 asection **secp;
4041 bfd_vma *valp;
4043 if (sym->st_shndx == SHN_COMMON
4044 && !info->relocateable
4045 && sym->st_size <= elf_gp_size (abfd)
4046 && info->hash->creator->flavour == bfd_target_elf_flavour)
4048 /* Common symbols less than or equal to -G nn bytes are automatically
4049 put into .sdata. */
4050 elf_linker_section_t *sdata
4051 = ppc_elf_create_linker_section (abfd, info, LINKER_SECTION_SDATA);
4053 if (!sdata->bss_section)
4055 bfd_size_type amt;
4057 /* We don't go through bfd_make_section, because we don't
4058 want to attach this common section to DYNOBJ. The linker
4059 will move the symbols to the appropriate output section
4060 when it defines common symbols. */
4061 amt = sizeof (asection);
4062 sdata->bss_section = (asection *) bfd_zalloc (abfd, amt);
4063 if (sdata->bss_section == NULL)
4064 return FALSE;
4065 sdata->bss_section->name = sdata->bss_name;
4066 sdata->bss_section->flags = SEC_IS_COMMON;
4067 sdata->bss_section->output_section = sdata->bss_section;
4068 amt = sizeof (asymbol);
4069 sdata->bss_section->symbol = (asymbol *) bfd_zalloc (abfd, amt);
4070 amt = sizeof (asymbol *);
4071 sdata->bss_section->symbol_ptr_ptr =
4072 (asymbol **) bfd_zalloc (abfd, amt);
4073 if (sdata->bss_section->symbol == NULL
4074 || sdata->bss_section->symbol_ptr_ptr == NULL)
4075 return FALSE;
4076 sdata->bss_section->symbol->name = sdata->bss_name;
4077 sdata->bss_section->symbol->flags = BSF_SECTION_SYM;
4078 sdata->bss_section->symbol->section = sdata->bss_section;
4079 *sdata->bss_section->symbol_ptr_ptr = sdata->bss_section->symbol;
4082 *secp = sdata->bss_section;
4083 *valp = sym->st_size;
4086 return TRUE;
4089 /* Finish up dynamic symbol handling. We set the contents of various
4090 dynamic sections here. */
4092 static bfd_boolean
4093 ppc_elf_finish_dynamic_symbol (output_bfd, info, h, sym)
4094 bfd *output_bfd;
4095 struct bfd_link_info *info;
4096 struct elf_link_hash_entry *h;
4097 Elf_Internal_Sym *sym;
4099 struct ppc_elf_link_hash_table *htab;
4101 #ifdef DEBUG
4102 fprintf (stderr, "ppc_elf_finish_dynamic_symbol called for %s",
4103 h->root.root.string);
4104 #endif
4106 htab = ppc_elf_hash_table (info);
4107 BFD_ASSERT (htab->elf.dynobj != NULL);
4109 if (h->plt.offset != (bfd_vma) -1)
4111 Elf_Internal_Rela rela;
4112 bfd_byte *loc;
4113 bfd_vma reloc_index;
4115 #ifdef DEBUG
4116 fprintf (stderr, ", plt_offset = %d", h->plt.offset);
4117 #endif
4119 /* This symbol has an entry in the procedure linkage table. Set
4120 it up. */
4122 BFD_ASSERT (h->dynindx != -1);
4123 BFD_ASSERT (htab->plt != NULL && htab->relplt != NULL);
4125 /* We don't need to fill in the .plt. The ppc dynamic linker
4126 will fill it in. */
4128 /* Fill in the entry in the .rela.plt section. */
4129 rela.r_offset = (htab->plt->output_section->vma
4130 + htab->plt->output_offset
4131 + h->plt.offset);
4132 rela.r_info = ELF32_R_INFO (h->dynindx, R_PPC_JMP_SLOT);
4133 rela.r_addend = 0;
4135 reloc_index = (h->plt.offset - PLT_INITIAL_ENTRY_SIZE) / PLT_SLOT_SIZE;
4136 if (reloc_index > PLT_NUM_SINGLE_ENTRIES)
4137 reloc_index -= (reloc_index - PLT_NUM_SINGLE_ENTRIES) / 2;
4138 loc = (htab->relplt->contents
4139 + reloc_index * sizeof (Elf32_External_Rela));
4140 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
4142 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
4144 /* Mark the symbol as undefined, rather than as defined in
4145 the .plt section. Leave the value alone. */
4146 sym->st_shndx = SHN_UNDEF;
4147 /* If the symbol is weak, we do need to clear the value.
4148 Otherwise, the PLT entry would provide a definition for
4149 the symbol even if the symbol wasn't defined anywhere,
4150 and so the symbol would never be NULL. */
4151 if ((h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR_NONWEAK)
4152 == 0)
4153 sym->st_value = 0;
4157 if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_COPY) != 0)
4159 asection *s;
4160 Elf_Internal_Rela rela;
4161 bfd_byte *loc;
4163 /* This symbols needs a copy reloc. Set it up. */
4165 #ifdef DEBUG
4166 fprintf (stderr, ", copy");
4167 #endif
4169 BFD_ASSERT (h->dynindx != -1);
4171 if (h->size <= elf_gp_size (htab->elf.dynobj))
4172 s = htab->relsbss;
4173 else
4174 s = htab->relbss;
4175 BFD_ASSERT (s != NULL);
4177 rela.r_offset = (h->root.u.def.value
4178 + h->root.u.def.section->output_section->vma
4179 + h->root.u.def.section->output_offset);
4180 rela.r_info = ELF32_R_INFO (h->dynindx, R_PPC_COPY);
4181 rela.r_addend = 0;
4182 loc = s->contents + s->reloc_count++ * sizeof (Elf32_External_Rela);
4183 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
4186 #ifdef DEBUG
4187 fprintf (stderr, "\n");
4188 #endif
4190 /* Mark some specially defined symbols as absolute. */
4191 if (strcmp (h->root.root.string, "_DYNAMIC") == 0
4192 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0
4193 || strcmp (h->root.root.string, "_PROCEDURE_LINKAGE_TABLE_") == 0)
4194 sym->st_shndx = SHN_ABS;
4196 return TRUE;
4199 /* Finish up the dynamic sections. */
4201 static bfd_boolean
4202 ppc_elf_finish_dynamic_sections (output_bfd, info)
4203 bfd *output_bfd;
4204 struct bfd_link_info *info;
4206 asection *sdyn;
4207 struct ppc_elf_link_hash_table *htab;
4209 #ifdef DEBUG
4210 fprintf (stderr, "ppc_elf_finish_dynamic_sections called\n");
4211 #endif
4213 htab = ppc_elf_hash_table (info);
4214 sdyn = bfd_get_section_by_name (htab->elf.dynobj, ".dynamic");
4216 if (htab->elf.dynamic_sections_created)
4218 Elf32_External_Dyn *dyncon, *dynconend;
4220 BFD_ASSERT (htab->plt != NULL && sdyn != NULL);
4222 dyncon = (Elf32_External_Dyn *) sdyn->contents;
4223 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->_raw_size);
4224 for (; dyncon < dynconend; dyncon++)
4226 Elf_Internal_Dyn dyn;
4227 asection *s;
4229 bfd_elf32_swap_dyn_in (htab->elf.dynobj, dyncon, &dyn);
4231 switch (dyn.d_tag)
4233 case DT_PLTGOT:
4234 s = htab->plt;
4235 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
4236 break;
4238 case DT_PLTRELSZ:
4239 dyn.d_un.d_val = htab->relplt->_raw_size;
4240 break;
4242 case DT_JMPREL:
4243 s = htab->relplt;
4244 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
4245 break;
4247 default:
4248 continue;
4251 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
4255 /* Add a blrl instruction at _GLOBAL_OFFSET_TABLE_-4 so that a function can
4256 easily find the address of the _GLOBAL_OFFSET_TABLE_. */
4257 if (htab->got)
4259 unsigned char *contents = htab->got->contents;
4260 bfd_put_32 (output_bfd, (bfd_vma) 0x4e800021 /* blrl */, contents);
4262 if (sdyn == NULL)
4263 bfd_put_32 (output_bfd, (bfd_vma) 0, contents + 4);
4264 else
4265 bfd_put_32 (output_bfd,
4266 sdyn->output_section->vma + sdyn->output_offset,
4267 contents + 4);
4269 elf_section_data (htab->got->output_section)->this_hdr.sh_entsize = 4;
4272 return TRUE;
4275 /* The RELOCATE_SECTION function is called by the ELF backend linker
4276 to handle the relocations for a section.
4278 The relocs are always passed as Rela structures; if the section
4279 actually uses Rel structures, the r_addend field will always be
4280 zero.
4282 This function is responsible for adjust the section contents as
4283 necessary, and (if using Rela relocs and generating a
4284 relocateable output file) adjusting the reloc addend as
4285 necessary.
4287 This function does not have to worry about setting the reloc
4288 address or the reloc symbol index.
4290 LOCAL_SYMS is a pointer to the swapped in local symbols.
4292 LOCAL_SECTIONS is an array giving the section in the input file
4293 corresponding to the st_shndx field of each local symbol.
4295 The global hash table entry for the global symbols can be found
4296 via elf_sym_hashes (input_bfd).
4298 When generating relocateable output, this function must handle
4299 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
4300 going to be the section symbol corresponding to the output
4301 section, which means that the addend must be adjusted
4302 accordingly. */
4304 static bfd_boolean
4305 ppc_elf_relocate_section (output_bfd, info, input_bfd, input_section,
4306 contents, relocs, local_syms, local_sections)
4307 bfd *output_bfd;
4308 struct bfd_link_info *info;
4309 bfd *input_bfd;
4310 asection *input_section;
4311 bfd_byte *contents;
4312 Elf_Internal_Rela *relocs;
4313 Elf_Internal_Sym *local_syms;
4314 asection **local_sections;
4316 Elf_Internal_Shdr *symtab_hdr;
4317 struct elf_link_hash_entry **sym_hashes;
4318 struct ppc_elf_link_hash_table *htab;
4319 Elf_Internal_Rela *rel;
4320 Elf_Internal_Rela *relend;
4321 Elf_Internal_Rela outrel;
4322 bfd_byte *loc;
4323 asection *sreloc = NULL;
4324 bfd_vma *local_got_offsets;
4325 bfd_boolean ret = TRUE;
4327 #ifdef DEBUG
4328 fprintf (stderr, "ppc_elf_relocate_section called for %s section %s, %ld relocations%s\n",
4329 bfd_archive_filename (input_bfd),
4330 bfd_section_name(input_bfd, input_section),
4331 (long) input_section->reloc_count,
4332 (info->relocateable) ? " (relocatable)" : "");
4333 #endif
4335 if (info->relocateable)
4336 return TRUE;
4338 if (!ppc_elf_howto_table[R_PPC_ADDR32])
4339 /* Initialize howto table if needed. */
4340 ppc_elf_howto_init ();
4342 htab = ppc_elf_hash_table (info);
4343 local_got_offsets = elf_local_got_offsets (input_bfd);
4344 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
4345 sym_hashes = elf_sym_hashes (input_bfd);
4346 rel = relocs;
4347 relend = relocs + input_section->reloc_count;
4348 for (; rel < relend; rel++)
4350 enum elf_ppc_reloc_type r_type;
4351 bfd_vma addend;
4352 bfd_reloc_status_type r;
4353 Elf_Internal_Sym *sym;
4354 asection *sec;
4355 struct elf_link_hash_entry *h;
4356 const char *sym_name;
4357 reloc_howto_type *howto;
4358 unsigned long r_symndx;
4359 bfd_vma relocation;
4360 bfd_vma branch_bit, insn, from;
4361 bfd_boolean unresolved_reloc;
4362 bfd_boolean warned;
4363 unsigned int tls_type, tls_mask, tls_gd;
4365 r_type = (enum elf_ppc_reloc_type)ELF32_R_TYPE (rel->r_info);
4366 sym = (Elf_Internal_Sym *) 0;
4367 sec = (asection *) 0;
4368 h = (struct elf_link_hash_entry *) 0;
4369 unresolved_reloc = FALSE;
4370 warned = FALSE;
4371 r_symndx = ELF32_R_SYM (rel->r_info);
4372 if (r_symndx < symtab_hdr->sh_info)
4374 sym = local_syms + r_symndx;
4375 sec = local_sections[r_symndx];
4376 sym_name = bfd_elf_local_sym_name (input_bfd, sym);
4378 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, sec, rel);
4380 else
4382 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
4383 while (h->root.type == bfd_link_hash_indirect
4384 || h->root.type == bfd_link_hash_warning)
4385 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4386 sym_name = h->root.root.string;
4388 relocation = 0;
4389 if (h->root.type == bfd_link_hash_defined
4390 || h->root.type == bfd_link_hash_defweak)
4392 sec = h->root.u.def.section;
4393 /* Set a flag that will be cleared later if we find a
4394 relocation value for this symbol. output_section
4395 is typically NULL for symbols satisfied by a shared
4396 library. */
4397 if (sec->output_section == NULL)
4398 unresolved_reloc = TRUE;
4399 else
4400 relocation = (h->root.u.def.value
4401 + sec->output_section->vma
4402 + sec->output_offset);
4404 else if (h->root.type == bfd_link_hash_undefweak)
4406 else if (info->shared
4407 && !info->no_undefined
4408 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
4410 else
4412 if (! ((*info->callbacks->undefined_symbol)
4413 (info, h->root.root.string, input_bfd, input_section,
4414 rel->r_offset, (!info->shared
4415 || info->no_undefined
4416 || ELF_ST_VISIBILITY (h->other)))))
4417 return FALSE;
4418 warned = TRUE;
4422 /* TLS optimizations. Replace instruction sequences and relocs
4423 based on information we collected in tls_optimize. We edit
4424 RELOCS so that --emit-relocs will output something sensible
4425 for the final instruction stream. */
4426 tls_mask = 0;
4427 tls_gd = 0;
4428 if (IS_PPC_TLS_RELOC (r_type))
4430 if (h != NULL)
4431 tls_mask = ((struct ppc_elf_link_hash_entry *) h)->tls_mask;
4432 else if (local_got_offsets != NULL)
4434 char *lgot_masks;
4435 lgot_masks = (char *) (local_got_offsets + symtab_hdr->sh_info);
4436 tls_mask = lgot_masks[r_symndx];
4440 /* Ensure reloc mapping code below stays sane. */
4441 if ((R_PPC_GOT_TLSLD16 & 3) != (R_PPC_GOT_TLSGD16 & 3)
4442 || (R_PPC_GOT_TLSLD16_LO & 3) != (R_PPC_GOT_TLSGD16_LO & 3)
4443 || (R_PPC_GOT_TLSLD16_HI & 3) != (R_PPC_GOT_TLSGD16_HI & 3)
4444 || (R_PPC_GOT_TLSLD16_HA & 3) != (R_PPC_GOT_TLSGD16_HA & 3)
4445 || (R_PPC_GOT_TLSLD16 & 3) != (R_PPC_GOT_TPREL16 & 3)
4446 || (R_PPC_GOT_TLSLD16_LO & 3) != (R_PPC_GOT_TPREL16_LO & 3)
4447 || (R_PPC_GOT_TLSLD16_HI & 3) != (R_PPC_GOT_TPREL16_HI & 3)
4448 || (R_PPC_GOT_TLSLD16_HA & 3) != (R_PPC_GOT_TPREL16_HA & 3))
4449 abort ();
4450 switch (r_type)
4452 default:
4453 break;
4455 case R_PPC_GOT_TPREL16:
4456 case R_PPC_GOT_TPREL16_LO:
4457 if (tls_mask != 0
4458 && (tls_mask & TLS_TPREL) == 0)
4460 bfd_vma insn;
4461 insn = bfd_get_32 (output_bfd, contents + rel->r_offset - 2);
4462 insn &= 31 << 21;
4463 insn |= 0x3c020000; /* addis 0,2,0 */
4464 bfd_put_32 (output_bfd, insn, contents + rel->r_offset - 2);
4465 r_type = R_PPC_TPREL16_HA;
4466 rel->r_info = ELF32_R_INFO (r_symndx, r_type);
4468 break;
4470 case R_PPC_TLS:
4471 if (tls_mask != 0
4472 && (tls_mask & TLS_TPREL) == 0)
4474 bfd_vma insn, rtra;
4475 insn = bfd_get_32 (output_bfd, contents + rel->r_offset);
4476 if ((insn & ((31 << 26) | (31 << 11)))
4477 == ((31 << 26) | (2 << 11)))
4478 rtra = insn & ((1 << 26) - (1 << 16));
4479 else if ((insn & ((31 << 26) | (31 << 16)))
4480 == ((31 << 26) | (2 << 16)))
4481 rtra = (insn & (31 << 21)) | ((insn & (31 << 11)) << 5);
4482 else
4483 abort ();
4484 if ((insn & ((1 << 11) - (1 << 1))) == 266 << 1)
4485 /* add -> addi. */
4486 insn = 14 << 26;
4487 else if ((insn & (31 << 1)) == 23 << 1
4488 && ((insn & (31 << 6)) < 14 << 6
4489 || ((insn & (31 << 6)) >= 16 << 6
4490 && (insn & (31 << 6)) < 24 << 6)))
4491 /* load and store indexed -> dform. */
4492 insn = (32 | ((insn >> 6) & 31)) << 26;
4493 else if ((insn & (31 << 1)) == 21 << 1
4494 && (insn & (0x1a << 6)) == 0)
4495 /* ldx, ldux, stdx, stdux -> ld, ldu, std, stdu. */
4496 insn = (((58 | ((insn >> 6) & 4)) << 26)
4497 | ((insn >> 6) & 1));
4498 else if ((insn & (31 << 1)) == 21 << 1
4499 && (insn & ((1 << 11) - (1 << 1))) == 341 << 1)
4500 /* lwax -> lwa. */
4501 insn = (58 << 26) | 2;
4502 else
4503 abort ();
4504 insn |= rtra;
4505 bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
4506 r_type = R_PPC_TPREL16_LO;
4507 rel->r_info = ELF32_R_INFO (r_symndx, r_type);
4508 /* Was PPC_TLS which sits on insn boundary, now
4509 PPC_TPREL16_LO which is at insn+2. */
4510 rel->r_offset += 2;
4512 break;
4514 case R_PPC_GOT_TLSGD16_HI:
4515 case R_PPC_GOT_TLSGD16_HA:
4516 tls_gd = TLS_TPRELGD;
4517 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
4518 goto tls_gdld_hi;
4519 break;
4521 case R_PPC_GOT_TLSLD16_HI:
4522 case R_PPC_GOT_TLSLD16_HA:
4523 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
4525 tls_gdld_hi:
4526 if ((tls_mask & tls_gd) != 0)
4527 r_type = (((r_type - (R_PPC_GOT_TLSGD16 & 3)) & 3)
4528 + R_PPC_GOT_TPREL16);
4529 else
4531 bfd_put_32 (output_bfd, NOP, contents + rel->r_offset);
4532 rel->r_offset -= 2;
4533 r_type = R_PPC_NONE;
4535 rel->r_info = ELF32_R_INFO (r_symndx, r_type);
4537 break;
4539 case R_PPC_GOT_TLSGD16:
4540 case R_PPC_GOT_TLSGD16_LO:
4541 tls_gd = TLS_TPRELGD;
4542 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
4543 goto tls_get_addr_check;
4544 break;
4546 case R_PPC_GOT_TLSLD16:
4547 case R_PPC_GOT_TLSLD16_LO:
4548 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
4550 tls_get_addr_check:
4551 if (rel + 1 < relend)
4553 enum elf_ppc_reloc_type r_type2;
4554 unsigned long r_symndx2;
4555 struct elf_link_hash_entry *h2;
4556 bfd_vma insn1, insn2;
4557 bfd_vma offset;
4559 /* The next instruction should be a call to
4560 __tls_get_addr. Peek at the reloc to be sure. */
4561 r_type2
4562 = (enum elf_ppc_reloc_type) ELF32_R_TYPE (rel[1].r_info);
4563 r_symndx2 = ELF32_R_SYM (rel[1].r_info);
4564 if (r_symndx2 < symtab_hdr->sh_info
4565 || (r_type2 != R_PPC_REL14
4566 && r_type2 != R_PPC_REL14_BRTAKEN
4567 && r_type2 != R_PPC_REL14_BRNTAKEN
4568 && r_type2 != R_PPC_REL24
4569 && r_type2 != R_PPC_PLTREL24))
4570 break;
4572 h2 = sym_hashes[r_symndx2 - symtab_hdr->sh_info];
4573 while (h2->root.type == bfd_link_hash_indirect
4574 || h2->root.type == bfd_link_hash_warning)
4575 h2 = (struct elf_link_hash_entry *) h2->root.u.i.link;
4576 if (h2 == NULL || h2 != htab->tls_get_addr)
4577 break;
4579 /* OK, it checks out. Replace the call. */
4580 offset = rel[1].r_offset;
4581 insn1 = bfd_get_32 (output_bfd,
4582 contents + rel->r_offset - 2);
4583 if ((tls_mask & tls_gd) != 0)
4585 /* IE */
4586 insn1 &= (1 << 26) - 1;
4587 insn1 |= 32 << 26; /* lwz */
4588 insn2 = 0x7c631214; /* add 3,3,2 */
4589 rel[1].r_info = ELF32_R_INFO (r_symndx2, R_PPC_NONE);
4590 r_type = (((r_type - (R_PPC_GOT_TLSGD16 & 3)) & 3)
4591 + R_PPC_GOT_TPREL16);
4592 rel->r_info = ELF32_R_INFO (r_symndx, r_type);
4594 else
4596 /* LE */
4597 insn1 = 0x3c620000; /* addis 3,2,0 */
4598 insn2 = 0x38630000; /* addi 3,3,0 */
4599 if (tls_gd == 0)
4601 /* Was an LD reloc. */
4602 r_symndx = 0;
4603 rel->r_addend = htab->tls_sec->vma + DTP_OFFSET;
4604 rel[1].r_addend = htab->tls_sec->vma + DTP_OFFSET;
4606 r_type = R_PPC_TPREL16_HA;
4607 rel->r_info = ELF32_R_INFO (r_symndx, r_type);
4608 rel[1].r_info = ELF32_R_INFO (r_symndx,
4609 R_PPC_TPREL16_LO);
4610 rel[1].r_offset += 2;
4612 bfd_put_32 (output_bfd, insn1, contents + rel->r_offset - 2);
4613 bfd_put_32 (output_bfd, insn2, contents + offset);
4614 if (tls_gd == 0)
4616 /* We changed the symbol on an LD reloc. Start over
4617 in order to get h, sym, sec etc. right. */
4618 rel--;
4619 continue;
4623 break;
4626 /* Handle other relocations that tweak non-addend part of insn. */
4627 branch_bit = 0;
4628 switch (r_type)
4630 default:
4631 break;
4633 /* Branch taken prediction relocations. */
4634 case R_PPC_ADDR14_BRTAKEN:
4635 case R_PPC_REL14_BRTAKEN:
4636 branch_bit = BRANCH_PREDICT_BIT;
4637 /* Fall thru */
4639 /* Branch not taken predicition relocations. */
4640 case R_PPC_ADDR14_BRNTAKEN:
4641 case R_PPC_REL14_BRNTAKEN:
4642 insn = bfd_get_32 (output_bfd, contents + rel->r_offset);
4643 insn &= ~BRANCH_PREDICT_BIT;
4644 insn |= branch_bit;
4646 from = (rel->r_offset
4647 + input_section->output_offset
4648 + input_section->output_section->vma);
4650 /* Invert 'y' bit if not the default. */
4651 if ((bfd_signed_vma) (relocation + rel->r_addend - from) < 0)
4652 insn ^= BRANCH_PREDICT_BIT;
4654 bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
4655 break;
4658 addend = rel->r_addend;
4659 tls_type = 0;
4660 howto = NULL;
4661 if ((unsigned) r_type < (unsigned) R_PPC_max)
4662 howto = ppc_elf_howto_table[(int) r_type];
4663 switch (r_type)
4665 default:
4666 (*_bfd_error_handler)
4667 (_("%s: unknown relocation type %d for symbol %s"),
4668 bfd_archive_filename (input_bfd), (int) r_type, sym_name);
4670 bfd_set_error (bfd_error_bad_value);
4671 ret = FALSE;
4672 continue;
4674 case R_PPC_NONE:
4675 case R_PPC_TLS:
4676 case R_PPC_EMB_MRKREF:
4677 case R_PPC_GNU_VTINHERIT:
4678 case R_PPC_GNU_VTENTRY:
4679 continue;
4681 /* GOT16 relocations. Like an ADDR16 using the symbol's
4682 address in the GOT as relocation value instead of the
4683 symbol's value itself. Also, create a GOT entry for the
4684 symbol and put the symbol value there. */
4685 case R_PPC_GOT_TLSGD16:
4686 case R_PPC_GOT_TLSGD16_LO:
4687 case R_PPC_GOT_TLSGD16_HI:
4688 case R_PPC_GOT_TLSGD16_HA:
4689 tls_type = TLS_TLS | TLS_GD;
4690 goto dogot;
4692 case R_PPC_GOT_TLSLD16:
4693 case R_PPC_GOT_TLSLD16_LO:
4694 case R_PPC_GOT_TLSLD16_HI:
4695 case R_PPC_GOT_TLSLD16_HA:
4696 tls_type = TLS_TLS | TLS_LD;
4697 goto dogot;
4699 case R_PPC_GOT_TPREL16:
4700 case R_PPC_GOT_TPREL16_LO:
4701 case R_PPC_GOT_TPREL16_HI:
4702 case R_PPC_GOT_TPREL16_HA:
4703 tls_type = TLS_TLS | TLS_TPREL;
4704 goto dogot;
4706 case R_PPC_GOT_DTPREL16:
4707 case R_PPC_GOT_DTPREL16_LO:
4708 case R_PPC_GOT_DTPREL16_HI:
4709 case R_PPC_GOT_DTPREL16_HA:
4710 tls_type = TLS_TLS | TLS_DTPREL;
4711 goto dogot;
4713 case R_PPC_GOT16:
4714 case R_PPC_GOT16_LO:
4715 case R_PPC_GOT16_HI:
4716 case R_PPC_GOT16_HA:
4717 dogot:
4719 /* Relocation is to the entry for this symbol in the global
4720 offset table. */
4721 bfd_vma off;
4722 bfd_vma *offp;
4723 unsigned long indx;
4725 if (htab->got == NULL)
4726 abort ();
4728 indx = 0;
4729 if (tls_type == (TLS_TLS | TLS_LD)
4730 && (h == NULL
4731 || !(h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC)))
4732 offp = &htab->tlsld_got.offset;
4733 else if (h != NULL)
4735 bfd_boolean dyn;
4736 dyn = htab->elf.dynamic_sections_created;
4737 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
4738 || (info->shared
4739 && SYMBOL_REFERENCES_LOCAL (info, h)))
4740 /* This is actually a static link, or it is a
4741 -Bsymbolic link and the symbol is defined
4742 locally, or the symbol was forced to be local
4743 because of a version file. */
4745 else
4747 indx = h->dynindx;
4748 unresolved_reloc = FALSE;
4750 offp = &h->got.offset;
4752 else
4754 if (local_got_offsets == NULL)
4755 abort ();
4756 offp = &local_got_offsets[r_symndx];
4759 /* The offset must always be a multiple of 4. We use the
4760 least significant bit to record whether we have already
4761 processed this entry. */
4762 off = *offp;
4763 if ((off & 1) != 0)
4764 off &= ~1;
4765 else
4767 unsigned int tls_m = (tls_mask
4768 & (TLS_LD | TLS_GD | TLS_DTPREL
4769 | TLS_TPREL | TLS_TPRELGD));
4771 if (offp == &htab->tlsld_got.offset)
4772 tls_m = TLS_LD;
4773 else if (h == NULL
4774 || !(h->elf_link_hash_flags
4775 & ELF_LINK_HASH_DEF_DYNAMIC))
4776 tls_m &= ~TLS_LD;
4778 /* We might have multiple got entries for this sym.
4779 Initialize them all. */
4782 int tls_ty = 0;
4784 if ((tls_m & TLS_LD) != 0)
4786 tls_ty = TLS_TLS | TLS_LD;
4787 tls_m &= ~TLS_LD;
4789 else if ((tls_m & TLS_GD) != 0)
4791 tls_ty = TLS_TLS | TLS_GD;
4792 tls_m &= ~TLS_GD;
4794 else if ((tls_m & TLS_DTPREL) != 0)
4796 tls_ty = TLS_TLS | TLS_DTPREL;
4797 tls_m &= ~TLS_DTPREL;
4799 else if ((tls_m & (TLS_TPREL | TLS_TPRELGD)) != 0)
4801 tls_ty = TLS_TLS | TLS_TPREL;
4802 tls_m = 0;
4805 /* Generate relocs for the dynamic linker. */
4806 if ((info->shared || indx != 0)
4807 && (h == NULL
4808 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
4809 || h->root.type != bfd_link_hash_undefweak))
4811 outrel.r_offset = (htab->got->output_section->vma
4812 + htab->got->output_offset
4813 + off);
4814 outrel.r_addend = 0;
4815 if (tls_ty & (TLS_LD | TLS_GD))
4817 outrel.r_info = ELF32_R_INFO (indx, R_PPC_DTPMOD32);
4818 if (tls_ty == (TLS_TLS | TLS_GD))
4820 loc = htab->relgot->contents;
4821 loc += (htab->relgot->reloc_count++
4822 * sizeof (Elf32_External_Rela));
4823 bfd_elf32_swap_reloca_out (output_bfd,
4824 &outrel, loc);
4825 outrel.r_offset += 4;
4826 outrel.r_info
4827 = ELF32_R_INFO (indx, R_PPC_DTPREL32);
4830 else if (tls_ty == (TLS_TLS | TLS_DTPREL))
4831 outrel.r_info = ELF32_R_INFO (indx, R_PPC_DTPREL32);
4832 else if (tls_ty == (TLS_TLS | TLS_TPREL))
4833 outrel.r_info = ELF32_R_INFO (indx, R_PPC_TPREL32);
4834 else if (indx == 0)
4835 outrel.r_info = ELF32_R_INFO (indx, R_PPC_RELATIVE);
4836 else
4837 outrel.r_info = ELF32_R_INFO (indx, R_PPC_GLOB_DAT);
4838 if (indx == 0)
4840 outrel.r_addend += relocation;
4841 if (tls_ty & (TLS_GD | TLS_DTPREL | TLS_TPREL))
4842 outrel.r_addend -= htab->tls_sec->vma;
4844 loc = htab->relgot->contents;
4845 loc += (htab->relgot->reloc_count++
4846 * sizeof (Elf32_External_Rela));
4847 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
4850 /* Init the .got section contents if we're not
4851 emitting a reloc. */
4852 else
4854 bfd_vma value = relocation;
4856 if (tls_ty == (TLS_TLS | TLS_LD))
4857 value = 1;
4858 else if (tls_ty != 0)
4860 value -= htab->tls_sec->vma + DTP_OFFSET;
4861 if (tls_ty == (TLS_TLS | TLS_TPREL))
4862 value += DTP_OFFSET - TP_OFFSET;
4864 if (tls_ty == (TLS_TLS | TLS_GD))
4866 bfd_put_32 (output_bfd, value,
4867 htab->got->contents + off + 4);
4868 value = 1;
4871 bfd_put_32 (output_bfd, value,
4872 htab->got->contents + off);
4875 off += 4;
4876 if (tls_ty & (TLS_LD | TLS_GD))
4877 off += 4;
4879 while (tls_m != 0);
4881 off = *offp;
4882 *offp = off | 1;
4885 if (off >= (bfd_vma) -2)
4886 abort ();
4888 if ((tls_type & TLS_TLS) != 0)
4890 if (tls_type != (TLS_TLS | TLS_LD))
4892 if ((tls_mask & TLS_LD) != 0
4893 && !(h == NULL
4894 || !(h->elf_link_hash_flags
4895 & ELF_LINK_HASH_DEF_DYNAMIC)))
4896 off += 8;
4897 if (tls_type != (TLS_TLS | TLS_GD))
4899 if ((tls_mask & TLS_GD) != 0)
4900 off += 8;
4901 if (tls_type != (TLS_TLS | TLS_DTPREL))
4903 if ((tls_mask & TLS_DTPREL) != 0)
4904 off += 4;
4910 relocation = htab->got->output_offset + off - 4;
4912 /* Addends on got relocations don't make much sense.
4913 x+off@got is actually x@got+off, and since the got is
4914 generated by a hash table traversal, the value in the
4915 got at entry m+n bears little relation to the entry m. */
4916 if (addend != 0)
4917 (*_bfd_error_handler)
4918 (_("%s(%s+0x%lx): non-zero addend on %s reloc against `%s'"),
4919 bfd_archive_filename (input_bfd),
4920 bfd_get_section_name (input_bfd, input_section),
4921 (long) rel->r_offset,
4922 howto->name,
4923 sym_name);
4925 break;
4927 /* Relocations that need no special processing. */
4928 case R_PPC_LOCAL24PC:
4929 /* It makes no sense to point a local relocation
4930 at a symbol not in this object. */
4931 if (unresolved_reloc)
4933 if (! (*info->callbacks->undefined_symbol) (info,
4934 h->root.root.string,
4935 input_bfd,
4936 input_section,
4937 rel->r_offset,
4938 TRUE))
4939 return FALSE;
4940 continue;
4942 break;
4944 case R_PPC_DTPREL16:
4945 case R_PPC_DTPREL16_LO:
4946 case R_PPC_DTPREL16_HI:
4947 case R_PPC_DTPREL16_HA:
4948 addend -= htab->tls_sec->vma + DTP_OFFSET;
4949 break;
4951 /* Relocations that may need to be propagated if this is a shared
4952 object. */
4953 case R_PPC_TPREL16:
4954 case R_PPC_TPREL16_LO:
4955 case R_PPC_TPREL16_HI:
4956 case R_PPC_TPREL16_HA:
4957 addend -= htab->tls_sec->vma + TP_OFFSET;
4958 /* The TPREL16 relocs shouldn't really be used in shared
4959 libs as they will result in DT_TEXTREL being set, but
4960 support them anyway. */
4961 goto dodyn;
4963 case R_PPC_TPREL32:
4964 addend -= htab->tls_sec->vma + TP_OFFSET;
4965 goto dodyn;
4967 case R_PPC_DTPREL32:
4968 addend -= htab->tls_sec->vma + DTP_OFFSET;
4969 goto dodyn;
4971 case R_PPC_DTPMOD32:
4972 relocation = 1;
4973 addend = 0;
4974 goto dodyn;
4976 case R_PPC_REL24:
4977 case R_PPC_REL32:
4978 case R_PPC_REL14:
4979 case R_PPC_REL14_BRTAKEN:
4980 case R_PPC_REL14_BRNTAKEN:
4981 /* If these relocations are not to a named symbol, they can be
4982 handled right here, no need to bother the dynamic linker. */
4983 if (h == NULL
4984 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0
4985 || SYMBOL_REFERENCES_LOCAL (info, h))
4986 break;
4987 /* fall through */
4989 /* Relocations that always need to be propagated if this is a shared
4990 object. */
4991 case R_PPC_ADDR32:
4992 case R_PPC_ADDR24:
4993 case R_PPC_ADDR16:
4994 case R_PPC_ADDR16_LO:
4995 case R_PPC_ADDR16_HI:
4996 case R_PPC_ADDR16_HA:
4997 case R_PPC_ADDR14:
4998 case R_PPC_ADDR14_BRTAKEN:
4999 case R_PPC_ADDR14_BRNTAKEN:
5000 case R_PPC_UADDR32:
5001 case R_PPC_UADDR16:
5002 /* r_symndx will be zero only for relocs against symbols
5003 from removed linkonce sections, or sections discarded by
5004 a linker script. */
5005 dodyn:
5006 if (r_symndx == 0)
5007 break;
5008 /* Fall thru. */
5010 if ((info->shared
5011 && (h == NULL
5012 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
5013 || h->root.type != bfd_link_hash_undefweak)
5014 && (MUST_BE_DYN_RELOC (r_type)
5015 || (h != NULL
5016 && h->dynindx != -1
5017 && (!info->symbolic
5018 || (h->elf_link_hash_flags
5019 & ELF_LINK_HASH_DEF_REGULAR) == 0))))
5020 || (ELIMINATE_COPY_RELOCS
5021 && !info->shared
5022 && (input_section->flags & SEC_ALLOC) != 0
5023 && h != NULL
5024 && h->dynindx != -1
5025 && (h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0
5026 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
5027 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0))
5029 int skip;
5031 #ifdef DEBUG
5032 fprintf (stderr, "ppc_elf_relocate_section need to create relocation for %s\n",
5033 (h && h->root.root.string
5034 ? h->root.root.string : "<unknown>"));
5035 #endif
5037 /* When generating a shared object, these relocations
5038 are copied into the output file to be resolved at run
5039 time. */
5040 if (sreloc == NULL)
5042 const char *name;
5044 name = (bfd_elf_string_from_elf_section
5045 (input_bfd,
5046 elf_elfheader (input_bfd)->e_shstrndx,
5047 elf_section_data (input_section)->rel_hdr.sh_name));
5048 if (name == NULL)
5049 return FALSE;
5051 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
5052 && strcmp (bfd_get_section_name (input_bfd,
5053 input_section),
5054 name + 5) == 0);
5056 sreloc = bfd_get_section_by_name (htab->elf.dynobj, name);
5057 BFD_ASSERT (sreloc != NULL);
5060 skip = 0;
5062 outrel.r_offset =
5063 _bfd_elf_section_offset (output_bfd, info, input_section,
5064 rel->r_offset);
5065 if (outrel.r_offset == (bfd_vma) -1
5066 || outrel.r_offset == (bfd_vma) -2)
5067 skip = (int) outrel.r_offset;
5068 outrel.r_offset += (input_section->output_section->vma
5069 + input_section->output_offset);
5071 if (skip)
5072 memset (&outrel, 0, sizeof outrel);
5073 else if (h != NULL
5074 && !SYMBOL_REFERENCES_LOCAL (info, h))
5076 unresolved_reloc = FALSE;
5077 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
5078 outrel.r_addend = rel->r_addend;
5080 else
5082 outrel.r_addend = relocation + rel->r_addend;
5084 if (r_type == R_PPC_ADDR32)
5085 outrel.r_info = ELF32_R_INFO (0, R_PPC_RELATIVE);
5086 else
5088 long indx;
5090 if (bfd_is_abs_section (sec))
5091 indx = 0;
5092 else if (sec == NULL || sec->owner == NULL)
5094 bfd_set_error (bfd_error_bad_value);
5095 return FALSE;
5097 else
5099 asection *osec;
5101 /* We are turning this relocation into one
5102 against a section symbol. It would be
5103 proper to subtract the symbol's value,
5104 osec->vma, from the emitted reloc addend,
5105 but ld.so expects buggy relocs. */
5106 osec = sec->output_section;
5107 indx = elf_section_data (osec)->dynindx;
5108 BFD_ASSERT (indx > 0);
5109 #ifdef DEBUG
5110 if (indx <= 0)
5112 printf ("indx=%d section=%s flags=%08x name=%s\n",
5113 indx, osec->name, osec->flags,
5114 h->root.root.string);
5116 #endif
5119 outrel.r_info = ELF32_R_INFO (indx, r_type);
5123 loc = sreloc->contents;
5124 loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rela);
5125 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
5127 if (skip == -1)
5128 continue;
5130 /* This reloc will be computed at runtime. We clear the memory
5131 so that it contains predictable value. */
5132 if (! skip
5133 && ((input_section->flags & SEC_ALLOC) != 0
5134 || ELF32_R_TYPE (outrel.r_info) != R_PPC_RELATIVE))
5136 relocation = howto->pc_relative ? outrel.r_offset : 0;
5137 addend = 0;
5138 break;
5141 break;
5143 /* Indirect .sdata relocation. */
5144 case R_PPC_EMB_SDAI16:
5145 BFD_ASSERT (htab->sdata != NULL);
5146 relocation
5147 = bfd_elf32_finish_pointer_linker_section (output_bfd, input_bfd,
5148 info, htab->sdata, h,
5149 relocation, rel,
5150 R_PPC_RELATIVE);
5151 break;
5153 /* Indirect .sdata2 relocation. */
5154 case R_PPC_EMB_SDA2I16:
5155 BFD_ASSERT (htab->sdata2 != NULL);
5156 relocation
5157 = bfd_elf32_finish_pointer_linker_section (output_bfd, input_bfd,
5158 info, htab->sdata2, h,
5159 relocation, rel,
5160 R_PPC_RELATIVE);
5161 break;
5163 /* Handle the TOC16 reloc. We want to use the offset within the .got
5164 section, not the actual VMA. This is appropriate when generating
5165 an embedded ELF object, for which the .got section acts like the
5166 AIX .toc section. */
5167 case R_PPC_TOC16: /* phony GOT16 relocations */
5168 BFD_ASSERT (sec != (asection *) 0);
5169 BFD_ASSERT (bfd_is_und_section (sec)
5170 || strcmp (bfd_get_section_name (abfd, sec), ".got") == 0
5171 || strcmp (bfd_get_section_name (abfd, sec), ".cgot") == 0)
5173 addend -= sec->output_section->vma + sec->output_offset + 0x8000;
5174 break;
5176 case R_PPC_PLTREL24:
5177 /* Relocation is to the entry for this symbol in the
5178 procedure linkage table. */
5179 BFD_ASSERT (h != NULL);
5181 if (h->plt.offset == (bfd_vma) -1
5182 || htab->plt == NULL)
5184 /* We didn't make a PLT entry for this symbol. This
5185 happens when statically linking PIC code, or when
5186 using -Bsymbolic. */
5187 break;
5190 unresolved_reloc = FALSE;
5191 relocation = (htab->plt->output_section->vma
5192 + htab->plt->output_offset
5193 + h->plt.offset);
5194 break;
5196 /* Relocate against _SDA_BASE_. */
5197 case R_PPC_SDAREL16:
5199 const char *name;
5200 const struct elf_link_hash_entry *sh;
5202 BFD_ASSERT (sec != (asection *) 0);
5203 name = bfd_get_section_name (abfd, sec->output_section);
5204 if (! ((strncmp (name, ".sdata", 6) == 0
5205 && (name[6] == 0 || name[6] == '.'))
5206 || (strncmp (name, ".sbss", 5) == 0
5207 && (name[5] == 0 || name[5] == '.'))))
5209 (*_bfd_error_handler) (_("%s: the target (%s) of a %s relocation is in the wrong output section (%s)"),
5210 bfd_archive_filename (input_bfd),
5211 sym_name,
5212 howto->name,
5213 name);
5215 sh = htab->sdata->sym_hash;
5216 addend -= (sh->root.u.def.value
5217 + sh->root.u.def.section->output_section->vma
5218 + sh->root.u.def.section->output_offset);
5220 break;
5222 /* Relocate against _SDA2_BASE_. */
5223 case R_PPC_EMB_SDA2REL:
5225 const char *name;
5226 const struct elf_link_hash_entry *sh;
5228 BFD_ASSERT (sec != (asection *) 0);
5229 name = bfd_get_section_name (abfd, sec->output_section);
5230 if (! (strncmp (name, ".sdata2", 7) == 0
5231 || strncmp (name, ".sbss2", 6) == 0))
5233 (*_bfd_error_handler) (_("%s: the target (%s) of a %s relocation is in the wrong output section (%s)"),
5234 bfd_archive_filename (input_bfd),
5235 sym_name,
5236 howto->name,
5237 name);
5239 bfd_set_error (bfd_error_bad_value);
5240 ret = FALSE;
5241 continue;
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);
5248 break;
5250 /* Relocate against either _SDA_BASE_, _SDA2_BASE_, or 0. */
5251 case R_PPC_EMB_SDA21:
5252 case R_PPC_EMB_RELSDA:
5254 const char *name;
5255 const struct elf_link_hash_entry *sh;
5256 int reg;
5258 BFD_ASSERT (sec != (asection *) 0);
5259 name = bfd_get_section_name (abfd, sec->output_section);
5260 if (((strncmp (name, ".sdata", 6) == 0
5261 && (name[6] == 0 || name[6] == '.'))
5262 || (strncmp (name, ".sbss", 5) == 0
5263 && (name[5] == 0 || name[5] == '.'))))
5265 reg = 13;
5266 sh = htab->sdata->sym_hash;
5267 addend -= (sh->root.u.def.value
5268 + sh->root.u.def.section->output_section->vma
5269 + sh->root.u.def.section->output_offset);
5272 else if (strncmp (name, ".sdata2", 7) == 0
5273 || strncmp (name, ".sbss2", 6) == 0)
5275 reg = 2;
5276 sh = htab->sdata2->sym_hash;
5277 addend -= (sh->root.u.def.value
5278 + sh->root.u.def.section->output_section->vma
5279 + sh->root.u.def.section->output_offset);
5282 else if (strcmp (name, ".PPC.EMB.sdata0") == 0
5283 || strcmp (name, ".PPC.EMB.sbss0") == 0)
5285 reg = 0;
5288 else
5290 (*_bfd_error_handler) (_("%s: the target (%s) of a %s relocation is in the wrong output section (%s)"),
5291 bfd_archive_filename (input_bfd),
5292 sym_name,
5293 howto->name,
5294 name);
5296 bfd_set_error (bfd_error_bad_value);
5297 ret = FALSE;
5298 continue;
5301 if (r_type == R_PPC_EMB_SDA21)
5302 { /* fill in register field */
5303 insn = bfd_get_32 (output_bfd, contents + rel->r_offset);
5304 insn = (insn & ~RA_REGISTER_MASK) | (reg << RA_REGISTER_SHIFT);
5305 bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
5308 break;
5310 /* Relocate against the beginning of the section. */
5311 case R_PPC_SECTOFF:
5312 case R_PPC_SECTOFF_LO:
5313 case R_PPC_SECTOFF_HI:
5314 case R_PPC_SECTOFF_HA:
5315 BFD_ASSERT (sec != (asection *) 0);
5316 addend -= sec->output_section->vma;
5317 break;
5319 /* Negative relocations. */
5320 case R_PPC_EMB_NADDR32:
5321 case R_PPC_EMB_NADDR16:
5322 case R_PPC_EMB_NADDR16_LO:
5323 case R_PPC_EMB_NADDR16_HI:
5324 case R_PPC_EMB_NADDR16_HA:
5325 addend -= 2 * relocation;
5326 break;
5328 case R_PPC_COPY:
5329 case R_PPC_GLOB_DAT:
5330 case R_PPC_JMP_SLOT:
5331 case R_PPC_RELATIVE:
5332 case R_PPC_PLT32:
5333 case R_PPC_PLTREL32:
5334 case R_PPC_PLT16_LO:
5335 case R_PPC_PLT16_HI:
5336 case R_PPC_PLT16_HA:
5337 case R_PPC_ADDR30:
5338 case R_PPC_EMB_RELSEC16:
5339 case R_PPC_EMB_RELST_LO:
5340 case R_PPC_EMB_RELST_HI:
5341 case R_PPC_EMB_RELST_HA:
5342 case R_PPC_EMB_BIT_FLD:
5343 (*_bfd_error_handler)
5344 (_("%s: relocation %s is not yet supported for symbol %s."),
5345 bfd_archive_filename (input_bfd),
5346 howto->name,
5347 sym_name);
5349 bfd_set_error (bfd_error_invalid_operation);
5350 ret = FALSE;
5351 continue;
5354 /* Do any further special processing. */
5355 switch (r_type)
5357 default:
5358 break;
5360 case R_PPC_ADDR16_HA:
5361 case R_PPC_GOT16_HA:
5362 case R_PPC_PLT16_HA:
5363 case R_PPC_SECTOFF_HA:
5364 case R_PPC_TPREL16_HA:
5365 case R_PPC_DTPREL16_HA:
5366 case R_PPC_GOT_TLSGD16_HA:
5367 case R_PPC_GOT_TLSLD16_HA:
5368 case R_PPC_GOT_TPREL16_HA:
5369 case R_PPC_GOT_DTPREL16_HA:
5370 case R_PPC_EMB_NADDR16_HA:
5371 case R_PPC_EMB_RELST_HA:
5372 /* It's just possible that this symbol is a weak symbol
5373 that's not actually defined anywhere. In that case,
5374 'sec' would be NULL, and we should leave the symbol
5375 alone (it will be set to zero elsewhere in the link). */
5376 if (sec != NULL)
5377 /* Add 0x10000 if sign bit in 0:15 is set.
5378 Bits 0:15 are not used. */
5379 addend += 0x8000;
5380 break;
5383 #ifdef DEBUG
5384 fprintf (stderr, "\ttype = %s (%d), name = %s, symbol index = %ld, offset = %ld, addend = %ld\n",
5385 howto->name,
5386 (int) r_type,
5387 sym_name,
5388 r_symndx,
5389 (long) rel->r_offset,
5390 (long) addend);
5391 #endif
5393 if (unresolved_reloc
5394 && !((input_section->flags & SEC_DEBUGGING) != 0
5395 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0))
5397 (*_bfd_error_handler)
5398 (_("%s(%s+0x%lx): unresolvable %s relocation against symbol `%s'"),
5399 bfd_archive_filename (input_bfd),
5400 bfd_get_section_name (input_bfd, input_section),
5401 (long) rel->r_offset,
5402 howto->name,
5403 sym_name);
5404 ret = FALSE;
5407 r = _bfd_final_link_relocate (howto,
5408 input_bfd,
5409 input_section,
5410 contents,
5411 rel->r_offset,
5412 relocation,
5413 addend);
5415 if (r != bfd_reloc_ok)
5417 if (sym_name == NULL)
5418 sym_name = "(null)";
5419 if (r == bfd_reloc_overflow)
5421 if (warned)
5422 continue;
5423 if (h != NULL
5424 && h->root.type == bfd_link_hash_undefweak
5425 && howto->pc_relative)
5427 /* Assume this is a call protected by other code that
5428 detect the symbol is undefined. If this is the case,
5429 we can safely ignore the overflow. If not, the
5430 program is hosed anyway, and a little warning isn't
5431 going to help. */
5433 continue;
5436 if (! (*info->callbacks->reloc_overflow) (info,
5437 sym_name,
5438 howto->name,
5439 rel->r_addend,
5440 input_bfd,
5441 input_section,
5442 rel->r_offset))
5443 return FALSE;
5445 else
5447 (*_bfd_error_handler)
5448 (_("%s(%s+0x%lx): %s reloc against `%s': error %d"),
5449 bfd_archive_filename (input_bfd),
5450 bfd_get_section_name (input_bfd, input_section),
5451 (long) rel->r_offset, howto->name, sym_name, (int) r);
5452 ret = FALSE;
5457 #ifdef DEBUG
5458 fprintf (stderr, "\n");
5459 #endif
5461 return ret;
5464 static enum elf_reloc_type_class
5465 ppc_elf_reloc_type_class (rela)
5466 const Elf_Internal_Rela *rela;
5468 switch ((int) ELF32_R_TYPE (rela->r_info))
5470 case R_PPC_RELATIVE:
5471 return reloc_class_relative;
5472 case R_PPC_REL24:
5473 case R_PPC_ADDR24:
5474 case R_PPC_JMP_SLOT:
5475 return reloc_class_plt;
5476 case R_PPC_COPY:
5477 return reloc_class_copy;
5478 default:
5479 return reloc_class_normal;
5483 /* Support for core dump NOTE sections. */
5485 static bfd_boolean
5486 ppc_elf_grok_prstatus (abfd, note)
5487 bfd *abfd;
5488 Elf_Internal_Note *note;
5490 int offset;
5491 unsigned int raw_size;
5493 switch (note->descsz)
5495 default:
5496 return FALSE;
5498 case 268: /* Linux/PPC. */
5499 /* pr_cursig */
5500 elf_tdata (abfd)->core_signal = bfd_get_16 (abfd, note->descdata + 12);
5502 /* pr_pid */
5503 elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, note->descdata + 24);
5505 /* pr_reg */
5506 offset = 72;
5507 raw_size = 192;
5509 break;
5512 /* Make a ".reg/999" section. */
5513 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
5514 raw_size, note->descpos + offset);
5517 static bfd_boolean
5518 ppc_elf_grok_psinfo (abfd, note)
5519 bfd *abfd;
5520 Elf_Internal_Note *note;
5522 switch (note->descsz)
5524 default:
5525 return FALSE;
5527 case 128: /* Linux/PPC elf_prpsinfo. */
5528 elf_tdata (abfd)->core_program
5529 = _bfd_elfcore_strndup (abfd, note->descdata + 32, 16);
5530 elf_tdata (abfd)->core_command
5531 = _bfd_elfcore_strndup (abfd, note->descdata + 48, 80);
5534 /* Note that for some reason, a spurious space is tacked
5535 onto the end of the args in some (at least one anyway)
5536 implementations, so strip it off if it exists. */
5539 char *command = elf_tdata (abfd)->core_command;
5540 int n = strlen (command);
5542 if (0 < n && command[n - 1] == ' ')
5543 command[n - 1] = '\0';
5546 return TRUE;
5549 /* Very simple linked list structure for recording apuinfo values. */
5550 typedef struct apuinfo_list
5552 struct apuinfo_list *next;
5553 unsigned long value;
5555 apuinfo_list;
5557 static apuinfo_list * head;
5559 static void apuinfo_list_init PARAMS ((void));
5560 static void apuinfo_list_add PARAMS ((unsigned long));
5561 static unsigned apuinfo_list_length PARAMS ((void));
5562 static unsigned long apuinfo_list_element PARAMS ((unsigned long));
5563 static void apuinfo_list_finish PARAMS ((void));
5565 extern void ppc_elf_begin_write_processing
5566 PARAMS ((bfd *, struct bfd_link_info *));
5567 extern void ppc_elf_final_write_processing
5568 PARAMS ((bfd *, bfd_boolean));
5569 extern bfd_boolean ppc_elf_write_section
5570 PARAMS ((bfd *, asection *, bfd_byte *));
5573 static void
5574 apuinfo_list_init PARAMS ((void))
5576 head = NULL;
5579 static void
5580 apuinfo_list_add (value)
5581 unsigned long value;
5583 apuinfo_list *entry = head;
5585 while (entry != NULL)
5587 if (entry->value == value)
5588 return;
5589 entry = entry->next;
5592 entry = bfd_malloc (sizeof (* entry));
5593 if (entry == NULL)
5594 return;
5596 entry->value = value;
5597 entry->next = head;
5598 head = entry;
5601 static unsigned
5602 apuinfo_list_length PARAMS ((void))
5604 apuinfo_list *entry;
5605 unsigned long count;
5607 for (entry = head, count = 0;
5608 entry;
5609 entry = entry->next)
5610 ++ count;
5612 return count;
5615 static inline unsigned long
5616 apuinfo_list_element (number)
5617 unsigned long number;
5619 apuinfo_list * entry;
5621 for (entry = head;
5622 entry && number --;
5623 entry = entry->next)
5626 return entry ? entry->value : 0;
5629 static void
5630 apuinfo_list_finish PARAMS ((void))
5632 apuinfo_list *entry;
5634 for (entry = head; entry;)
5636 apuinfo_list *next = entry->next;
5637 free (entry);
5638 entry = next;
5641 head = NULL;
5644 #define APUINFO_SECTION_NAME ".PPC.EMB.apuinfo"
5645 #define APUINFO_LABEL "APUinfo"
5647 /* Scan the input BFDs and create a linked list of
5648 the APUinfo values that will need to be emitted. */
5650 void
5651 ppc_elf_begin_write_processing (abfd, link_info)
5652 bfd *abfd;
5653 struct bfd_link_info *link_info;
5655 bfd *ibfd;
5656 asection *asec;
5657 char *buffer;
5658 unsigned num_input_sections;
5659 bfd_size_type output_section_size;
5660 unsigned i;
5661 unsigned num_entries;
5662 unsigned long offset;
5663 unsigned long length;
5664 const char *error_message = NULL;
5666 if (link_info == NULL)
5667 return;
5669 /* Scan the input bfds, looking for apuinfo sections. */
5670 num_input_sections = 0;
5671 output_section_size = 0;
5673 for (ibfd = link_info->input_bfds; ibfd; ibfd = ibfd->link_next)
5675 asec = bfd_get_section_by_name (ibfd, APUINFO_SECTION_NAME);
5676 if (asec)
5678 ++ num_input_sections;
5679 output_section_size += asec->_raw_size;
5683 /* We need at least one input sections
5684 in order to make merging worthwhile. */
5685 if (num_input_sections < 1)
5686 return;
5688 /* Just make sure that the output section exists as well. */
5689 asec = bfd_get_section_by_name (abfd, APUINFO_SECTION_NAME);
5690 if (asec == NULL)
5691 return;
5693 /* Allocate a buffer for the contents of the input sections. */
5694 buffer = bfd_malloc (output_section_size);
5695 if (buffer == NULL)
5696 return;
5698 offset = 0;
5699 apuinfo_list_init ();
5701 /* Read in the input sections contents. */
5702 for (ibfd = link_info->input_bfds; ibfd; ibfd = ibfd->link_next)
5704 unsigned long datum;
5705 char *ptr;
5707 asec = bfd_get_section_by_name (ibfd, APUINFO_SECTION_NAME);
5708 if (asec == NULL)
5709 continue;
5711 length = asec->_raw_size;
5712 if (length < 24)
5714 error_message = _("corrupt or empty %s section in %s");
5715 goto fail;
5718 if (bfd_seek (ibfd, asec->filepos, SEEK_SET) != 0
5719 || (bfd_bread (buffer + offset, length, ibfd) != length))
5721 error_message = _("unable to read in %s section from %s");
5722 goto fail;
5725 /* Process the contents of the section. */
5726 ptr = buffer + offset;
5727 error_message = _("corrupt %s section in %s");
5729 /* Verify the contents of the header. Note - we have to
5730 extract the values this way in order to allow for a
5731 host whose endian-ness is different from the target. */
5732 datum = bfd_get_32 (ibfd, ptr);
5733 if (datum != sizeof APUINFO_LABEL)
5734 goto fail;
5736 datum = bfd_get_32 (ibfd, ptr + 8);
5737 if (datum != 0x2)
5738 goto fail;
5740 if (strcmp (ptr + 12, APUINFO_LABEL) != 0)
5741 goto fail;
5743 /* Get the number of apuinfo entries. */
5744 datum = bfd_get_32 (ibfd, ptr + 4);
5745 if ((datum * 4 + 20) != length)
5746 goto fail;
5748 /* Make sure that we do not run off the end of the section. */
5749 if (offset + length > output_section_size)
5750 goto fail;
5752 /* Scan the apuinfo section, building a list of apuinfo numbers. */
5753 for (i = 0; i < datum; i++)
5754 apuinfo_list_add (bfd_get_32 (ibfd, ptr + 20 + (i * 4)));
5756 /* Update the offset. */
5757 offset += length;
5760 error_message = NULL;
5762 /* Compute the size of the output section. */
5763 num_entries = apuinfo_list_length ();
5764 output_section_size = 20 + num_entries * 4;
5766 asec = bfd_get_section_by_name (abfd, APUINFO_SECTION_NAME);
5768 if (! bfd_set_section_size (abfd, asec, output_section_size))
5769 ibfd = abfd,
5770 error_message = _("warning: unable to set size of %s section in %s");
5772 fail:
5773 free (buffer);
5775 if (error_message)
5776 (*_bfd_error_handler) (error_message, APUINFO_SECTION_NAME,
5777 bfd_archive_filename (ibfd));
5781 /* Prevent the output section from accumulating the input sections'
5782 contents. We have already stored this in our linked list structure. */
5784 bfd_boolean
5785 ppc_elf_write_section (abfd, asec, contents)
5786 bfd *abfd ATTRIBUTE_UNUSED;
5787 asection *asec;
5788 bfd_byte *contents ATTRIBUTE_UNUSED;
5790 return (apuinfo_list_length ()
5791 && strcmp (asec->name, APUINFO_SECTION_NAME) == 0);
5795 /* Finally we can generate the output section. */
5797 void
5798 ppc_elf_final_write_processing (abfd, linker)
5799 bfd *abfd;
5800 bfd_boolean linker ATTRIBUTE_UNUSED;
5802 bfd_byte *buffer;
5803 asection *asec;
5804 unsigned i;
5805 unsigned num_entries;
5806 bfd_size_type length;
5808 asec = bfd_get_section_by_name (abfd, APUINFO_SECTION_NAME);
5809 if (asec == NULL)
5810 return;
5812 if (apuinfo_list_length () == 0)
5813 return;
5815 length = asec->_raw_size;
5816 if (length < 20)
5817 return;
5819 buffer = bfd_malloc (length);
5820 if (buffer == NULL)
5822 (*_bfd_error_handler)
5823 (_("failed to allocate space for new APUinfo section."));
5824 return;
5827 /* Create the apuinfo header. */
5828 num_entries = apuinfo_list_length ();
5829 bfd_put_32 (abfd, sizeof APUINFO_LABEL, buffer);
5830 bfd_put_32 (abfd, num_entries, buffer + 4);
5831 bfd_put_32 (abfd, 0x2, buffer + 8);
5832 strcpy (buffer + 12, APUINFO_LABEL);
5834 length = 20;
5835 for (i = 0; i < num_entries; i++)
5837 bfd_put_32 (abfd, apuinfo_list_element (i), buffer + length);
5838 length += 4;
5841 if (length != asec->_raw_size)
5842 (*_bfd_error_handler) (_("failed to compute new APUinfo section."));
5844 if (! bfd_set_section_contents (abfd, asec, buffer, (file_ptr) 0, length))
5845 (*_bfd_error_handler) (_("failed to install new APUinfo section."));
5847 free (buffer);
5849 apuinfo_list_finish ();
5852 #define TARGET_LITTLE_SYM bfd_elf32_powerpcle_vec
5853 #define TARGET_LITTLE_NAME "elf32-powerpcle"
5854 #define TARGET_BIG_SYM bfd_elf32_powerpc_vec
5855 #define TARGET_BIG_NAME "elf32-powerpc"
5856 #define ELF_ARCH bfd_arch_powerpc
5857 #define ELF_MACHINE_CODE EM_PPC
5858 #define ELF_MAXPAGESIZE 0x10000
5859 #define elf_info_to_howto ppc_elf_info_to_howto
5861 #ifdef EM_CYGNUS_POWERPC
5862 #define ELF_MACHINE_ALT1 EM_CYGNUS_POWERPC
5863 #endif
5865 #ifdef EM_PPC_OLD
5866 #define ELF_MACHINE_ALT2 EM_PPC_OLD
5867 #endif
5869 #define elf_backend_plt_not_loaded 1
5870 #define elf_backend_got_symbol_offset 4
5871 #define elf_backend_can_gc_sections 1
5872 #define elf_backend_can_refcount 1
5873 #define elf_backend_got_header_size 12
5874 #define elf_backend_plt_header_size PLT_INITIAL_ENTRY_SIZE
5875 #define elf_backend_rela_normal 1
5877 #define bfd_elf32_bfd_merge_private_bfd_data ppc_elf_merge_private_bfd_data
5878 #define bfd_elf32_bfd_relax_section ppc_elf_relax_section
5879 #define bfd_elf32_bfd_reloc_type_lookup ppc_elf_reloc_type_lookup
5880 #define bfd_elf32_bfd_set_private_flags ppc_elf_set_private_flags
5881 #define bfd_elf32_bfd_link_hash_table_create ppc_elf_link_hash_table_create
5883 #define elf_backend_object_p ppc_elf_object_p
5884 #define elf_backend_gc_mark_hook ppc_elf_gc_mark_hook
5885 #define elf_backend_gc_sweep_hook ppc_elf_gc_sweep_hook
5886 #define elf_backend_section_from_shdr ppc_elf_section_from_shdr
5887 #define elf_backend_relocate_section ppc_elf_relocate_section
5888 #define elf_backend_create_dynamic_sections ppc_elf_create_dynamic_sections
5889 #define elf_backend_check_relocs ppc_elf_check_relocs
5890 #define elf_backend_copy_indirect_symbol ppc_elf_copy_indirect_symbol
5891 #define elf_backend_adjust_dynamic_symbol ppc_elf_adjust_dynamic_symbol
5892 #define elf_backend_add_symbol_hook ppc_elf_add_symbol_hook
5893 #define elf_backend_size_dynamic_sections ppc_elf_size_dynamic_sections
5894 #define elf_backend_finish_dynamic_symbol ppc_elf_finish_dynamic_symbol
5895 #define elf_backend_finish_dynamic_sections ppc_elf_finish_dynamic_sections
5896 #define elf_backend_fake_sections ppc_elf_fake_sections
5897 #define elf_backend_additional_program_headers ppc_elf_additional_program_headers
5898 #define elf_backend_modify_segment_map ppc_elf_modify_segment_map
5899 #define elf_backend_grok_prstatus ppc_elf_grok_prstatus
5900 #define elf_backend_grok_psinfo ppc_elf_grok_psinfo
5901 #define elf_backend_reloc_type_class ppc_elf_reloc_type_class
5902 #define elf_backend_begin_write_processing ppc_elf_begin_write_processing
5903 #define elf_backend_final_write_processing ppc_elf_final_write_processing
5904 #define elf_backend_write_section ppc_elf_write_section
5906 #include "elf32-target.h"