2003-02-27 Andrew Cagney <cagney@redhat.com>
[binutils.git] / bfd / elf32-ppc.c
blob9947af855fee8d996ce6ae2cebf4347ccc39a937
1 /* PowerPC-specific support for 32-bit ELF
2 Copyright 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
3 Free Software Foundation, Inc.
4 Written by Ian Lance Taylor, Cygnus Support.
6 This file is part of BFD, the Binary File Descriptor library.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
22 /* This file is based on a preliminary PowerPC ELF ABI. The
23 information may not match the final PowerPC ELF ABI. It includes
24 suggestions from the in-progress Embedded PowerPC ABI, and that
25 information may also not match. */
27 #include "bfd.h"
28 #include "sysdep.h"
29 #include "bfdlink.h"
30 #include "libbfd.h"
31 #include "elf-bfd.h"
32 #include "elf/ppc.h"
33 #include "elf32-ppc.h"
35 /* RELA relocations are used here. */
37 static struct bfd_hash_entry *ppc_elf_link_hash_newfunc
38 PARAMS ((struct bfd_hash_entry *entry, struct bfd_hash_table *table,
39 const char *string));
40 static struct bfd_link_hash_table *ppc_elf_link_hash_table_create
41 PARAMS ((bfd *abfd));
42 static void ppc_elf_copy_indirect_symbol
43 PARAMS ((struct elf_backend_data *bed, struct elf_link_hash_entry *dir,
44 struct elf_link_hash_entry *ind));
45 static reloc_howto_type *ppc_elf_reloc_type_lookup
46 PARAMS ((bfd *abfd, bfd_reloc_code_real_type code));
47 static void ppc_elf_info_to_howto
48 PARAMS ((bfd *abfd, arelent *cache_ptr, Elf_Internal_Rela *dst));
49 static void ppc_elf_howto_init
50 PARAMS ((void));
51 static int ppc_elf_sort_rela
52 PARAMS ((const PTR, const PTR));
53 static bfd_boolean ppc_elf_relax_section
54 PARAMS ((bfd *, asection *, struct bfd_link_info *, bfd_boolean *));
55 static bfd_reloc_status_type ppc_elf_addr16_ha_reloc
56 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
57 static bfd_reloc_status_type ppc_elf_unhandled_reloc
58 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
59 static bfd_boolean ppc_elf_object_p
60 PARAMS ((bfd *));
61 static bfd_boolean ppc_elf_set_private_flags
62 PARAMS ((bfd *, flagword));
63 static bfd_boolean ppc_elf_merge_private_bfd_data
64 PARAMS ((bfd *, bfd *));
65 static int ppc_elf_additional_program_headers
66 PARAMS ((bfd *));
67 static bfd_boolean ppc_elf_modify_segment_map
68 PARAMS ((bfd *));
69 static bfd_boolean ppc_elf_create_got
70 PARAMS ((bfd *, struct bfd_link_info *));
71 static bfd_boolean ppc_elf_create_dynamic_sections
72 PARAMS ((bfd *, struct bfd_link_info *));
73 static bfd_boolean ppc_elf_section_from_shdr
74 PARAMS ((bfd *, Elf_Internal_Shdr *, const char *));
75 static bfd_boolean ppc_elf_fake_sections
76 PARAMS ((bfd *, Elf_Internal_Shdr *, asection *));
77 static elf_linker_section_t *ppc_elf_create_linker_section
78 PARAMS ((bfd *abfd, struct bfd_link_info *info,
79 enum elf_linker_section_enum));
80 static bfd_boolean update_local_sym_info
81 PARAMS ((bfd *, Elf_Internal_Shdr *, unsigned long, int));
82 static void bad_shared_reloc
83 PARAMS ((bfd *, enum elf_ppc_reloc_type));
84 static bfd_boolean ppc_elf_check_relocs
85 PARAMS ((bfd *, struct bfd_link_info *, asection *,
86 const Elf_Internal_Rela *));
87 static asection *ppc_elf_gc_mark_hook
88 PARAMS ((asection *sec, struct bfd_link_info *info, Elf_Internal_Rela *rel,
89 struct elf_link_hash_entry *h, Elf_Internal_Sym *sym));
90 static bfd_boolean ppc_elf_gc_sweep_hook
91 PARAMS ((bfd *abfd, struct bfd_link_info *info, asection *sec,
92 const Elf_Internal_Rela *relocs));
93 static bfd_boolean ppc_elf_adjust_dynamic_symbol
94 PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *));
95 static bfd_boolean allocate_dynrelocs
96 PARAMS ((struct elf_link_hash_entry *, PTR));
97 static bfd_boolean readonly_dynrelocs
98 PARAMS ((struct elf_link_hash_entry *, PTR));
99 static bfd_boolean ppc_elf_size_dynamic_sections
100 PARAMS ((bfd *, struct bfd_link_info *));
101 static bfd_boolean ppc_elf_relocate_section
102 PARAMS ((bfd *, struct bfd_link_info *info, bfd *, asection *, bfd_byte *,
103 Elf_Internal_Rela *relocs, Elf_Internal_Sym *local_syms,
104 asection **));
105 static bfd_boolean ppc_elf_add_symbol_hook
106 PARAMS ((bfd *, struct bfd_link_info *, const Elf_Internal_Sym *,
107 const char **, flagword *, asection **, bfd_vma *));
108 static bfd_boolean ppc_elf_finish_dynamic_symbol
109 PARAMS ((bfd *, struct bfd_link_info *, struct elf_link_hash_entry *,
110 Elf_Internal_Sym *));
111 static bfd_boolean ppc_elf_finish_dynamic_sections
112 PARAMS ((bfd *, struct bfd_link_info *));
113 static enum elf_reloc_type_class ppc_elf_reloc_type_class
114 PARAMS ((const Elf_Internal_Rela *));
115 static bfd_boolean ppc_elf_grok_prstatus
116 PARAMS ((bfd *abfd, Elf_Internal_Note *note));
117 static bfd_boolean ppc_elf_grok_psinfo
118 PARAMS ((bfd *abfd, Elf_Internal_Note *note));
120 /* Branch prediction bit for branch taken relocs. */
121 #define BRANCH_PREDICT_BIT 0x200000
122 /* Mask to set RA in memory instructions. */
123 #define RA_REGISTER_MASK 0x001f0000
124 /* Value to shift register by to insert RA. */
125 #define RA_REGISTER_SHIFT 16
127 /* The name of the dynamic interpreter. This is put in the .interp
128 section. */
129 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
131 /* The size in bytes of an entry in the procedure linkage table. */
132 #define PLT_ENTRY_SIZE 12
133 /* The initial size of the plt reserved for the dynamic linker. */
134 #define PLT_INITIAL_ENTRY_SIZE 72
135 /* The size of the gap between entries in the PLT. */
136 #define PLT_SLOT_SIZE 8
137 /* The number of single-slot PLT entries (the rest use two slots). */
138 #define PLT_NUM_SINGLE_ENTRIES 8192
140 /* Some nop instructions. */
141 #define NOP 0x60000000
142 #define CROR_151515 0x4def7b82
143 #define CROR_313131 0x4ffffb82
145 /* Offset of tp and dtp pointers from start of TLS block. */
146 #define TP_OFFSET 0x7000
147 #define DTP_OFFSET 0x8000
149 /* Will references to this symbol always reference the symbol
150 in this object? */
151 #define SYMBOL_REFERENCES_LOCAL(INFO, H) \
152 ((! INFO->shared \
153 || INFO->symbolic \
154 || H->dynindx == -1 \
155 || ELF_ST_VISIBILITY (H->other) == STV_INTERNAL \
156 || ELF_ST_VISIBILITY (H->other) == STV_HIDDEN) \
157 && (H->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0)
159 /* Will _calls_ to this symbol always call the version in this object? */
160 #define SYMBOL_CALLS_LOCAL(INFO, H) \
161 ((! INFO->shared \
162 || INFO->symbolic \
163 || H->dynindx == -1 \
164 || ELF_ST_VISIBILITY (H->other) != STV_DEFAULT) \
165 && (H->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0)
167 /* The PPC linker needs to keep track of the number of relocs that it
168 decides to copy as dynamic relocs in check_relocs for each symbol.
169 This is so that it can later discard them if they are found to be
170 unnecessary. We store the information in a field extending the
171 regular ELF linker hash table. */
173 struct ppc_elf_dyn_relocs
175 struct ppc_elf_dyn_relocs *next;
177 /* The input section of the reloc. */
178 asection *sec;
180 /* Total number of relocs copied for the input section. */
181 bfd_size_type count;
183 /* Number of pc-relative relocs copied for the input section. */
184 bfd_size_type pc_count;
187 /* PPC ELF linker hash entry. */
189 struct ppc_elf_link_hash_entry
191 struct elf_link_hash_entry elf;
193 /* Track dynamic relocs copied for this symbol. */
194 struct ppc_elf_dyn_relocs *dyn_relocs;
196 /* Contexts in which symbol is used in the GOT (or TOC).
197 TLS_GD .. TLS_EXPLICIT bits are or'd into the mask as the
198 corresponding relocs are encountered during check_relocs.
199 tls_optimize clears TLS_GD .. TLS_TPREL when optimizing to
200 indicate the corresponding GOT entry type is not needed. */
201 #define TLS_GD 1 /* GD reloc. */
202 #define TLS_LD 2 /* LD reloc. */
203 #define TLS_TPREL 4 /* TPREL reloc, => IE. */
204 #define TLS_DTPREL 8 /* DTPREL reloc, => LD. */
205 #define TLS_TLS 16 /* Any TLS reloc. */
206 #define TLS_TPRELGD 32 /* TPREL reloc resulting from GD->IE. */
207 char tls_mask;
210 #define ppc_elf_hash_entry(ent) ((struct ppc_elf_link_hash_entry *) (ent))
212 /* PPC ELF linker hash table. */
214 struct ppc_elf_link_hash_table
216 struct elf_link_hash_table elf;
218 /* Short-cuts to get to dynamic linker sections. */
219 asection *got;
220 asection *relgot;
221 asection *plt;
222 asection *relplt;
223 asection *dynbss;
224 asection *relbss;
225 asection *dynsbss;
226 asection *relsbss;
227 elf_linker_section_t *sdata;
228 elf_linker_section_t *sdata2;
230 /* Short-cut to first output tls section. */
231 asection *tls_sec;
233 /* Shortcut to .__tls_get_addr. */
234 struct elf_link_hash_entry *tls_get_addr;
236 /* TLS local dynamic got entry handling. */
237 union {
238 bfd_signed_vma refcount;
239 bfd_vma offset;
240 } tlsld_got;
242 /* Small local sym to section mapping cache. */
243 struct sym_sec_cache sym_sec;
246 /* Get the PPC ELF linker hash table from a link_info structure. */
248 #define ppc_elf_hash_table(p) \
249 ((struct ppc_elf_link_hash_table *) (p)->hash)
251 /* Create an entry in a PPC ELF linker hash table. */
253 static struct bfd_hash_entry *
254 ppc_elf_link_hash_newfunc (entry, table, string)
255 struct bfd_hash_entry *entry;
256 struct bfd_hash_table *table;
257 const char *string;
259 /* Allocate the structure if it has not already been allocated by a
260 subclass. */
261 if (entry == NULL)
263 entry = bfd_hash_allocate (table,
264 sizeof (struct ppc_elf_link_hash_entry));
265 if (entry == NULL)
266 return entry;
269 /* Call the allocation method of the superclass. */
270 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
271 if (entry != NULL)
273 ppc_elf_hash_entry (entry)->dyn_relocs = NULL;
274 ppc_elf_hash_entry (entry)->tls_mask = 0;
277 return entry;
280 /* Create a PPC ELF linker hash table. */
282 static struct bfd_link_hash_table *
283 ppc_elf_link_hash_table_create (abfd)
284 bfd *abfd;
286 struct ppc_elf_link_hash_table *ret;
288 ret = ((struct ppc_elf_link_hash_table *)
289 bfd_malloc (sizeof (struct ppc_elf_link_hash_table)));
290 if (ret == NULL)
291 return NULL;
293 if (! _bfd_elf_link_hash_table_init (&ret->elf, abfd,
294 ppc_elf_link_hash_newfunc))
296 free (ret);
297 return NULL;
300 ret->got = NULL;
301 ret->relgot = NULL;
302 ret->plt = NULL;
303 ret->relplt = NULL;
304 ret->dynbss = NULL;
305 ret->relbss = NULL;
306 ret->dynsbss = NULL;
307 ret->relsbss = NULL;
308 ret->sdata = NULL;
309 ret->sdata2 = NULL;
310 ret->tls_sec = NULL;
311 ret->tls_get_addr = NULL;
312 ret->tlsld_got.refcount = 0;
313 ret->sym_sec.abfd = NULL;
315 return &ret->elf.root;
318 /* Copy the extra info we tack onto an elf_link_hash_entry. */
320 static void
321 ppc_elf_copy_indirect_symbol (bed, dir, ind)
322 struct elf_backend_data *bed;
323 struct elf_link_hash_entry *dir, *ind;
325 struct ppc_elf_link_hash_entry *edir, *eind;
327 edir = (struct ppc_elf_link_hash_entry *) dir;
328 eind = (struct ppc_elf_link_hash_entry *) ind;
330 if (eind->dyn_relocs != NULL)
332 if (edir->dyn_relocs != NULL)
334 struct ppc_elf_dyn_relocs **pp;
335 struct ppc_elf_dyn_relocs *p;
337 if (ind->root.type == bfd_link_hash_indirect)
338 abort ();
340 /* Add reloc counts against the weak sym to the strong sym
341 list. Merge any entries against the same section. */
342 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
344 struct ppc_elf_dyn_relocs *q;
346 for (q = edir->dyn_relocs; q != NULL; q = q->next)
347 if (q->sec == p->sec)
349 q->pc_count += p->pc_count;
350 q->count += p->count;
351 *pp = p->next;
352 break;
354 if (q == NULL)
355 pp = &p->next;
357 *pp = edir->dyn_relocs;
360 edir->dyn_relocs = eind->dyn_relocs;
361 eind->dyn_relocs = NULL;
364 edir->tls_mask |= eind->tls_mask;
366 _bfd_elf_link_hash_copy_indirect (bed, dir, ind);
369 static reloc_howto_type *ppc_elf_howto_table[(int) R_PPC_max];
371 static reloc_howto_type ppc_elf_howto_raw[] = {
372 /* This reloc does nothing. */
373 HOWTO (R_PPC_NONE, /* type */
374 0, /* rightshift */
375 2, /* size (0 = byte, 1 = short, 2 = long) */
376 32, /* bitsize */
377 FALSE, /* pc_relative */
378 0, /* bitpos */
379 complain_overflow_bitfield, /* complain_on_overflow */
380 bfd_elf_generic_reloc, /* special_function */
381 "R_PPC_NONE", /* name */
382 FALSE, /* partial_inplace */
383 0, /* src_mask */
384 0, /* dst_mask */
385 FALSE), /* pcrel_offset */
387 /* A standard 32 bit relocation. */
388 HOWTO (R_PPC_ADDR32, /* type */
389 0, /* rightshift */
390 2, /* size (0 = byte, 1 = short, 2 = long) */
391 32, /* bitsize */
392 FALSE, /* pc_relative */
393 0, /* bitpos */
394 complain_overflow_bitfield, /* complain_on_overflow */
395 bfd_elf_generic_reloc, /* special_function */
396 "R_PPC_ADDR32", /* name */
397 FALSE, /* partial_inplace */
398 0, /* src_mask */
399 0xffffffff, /* dst_mask */
400 FALSE), /* pcrel_offset */
402 /* An absolute 26 bit branch; the lower two bits must be zero.
403 FIXME: we don't check that, we just clear them. */
404 HOWTO (R_PPC_ADDR24, /* type */
405 0, /* rightshift */
406 2, /* size (0 = byte, 1 = short, 2 = long) */
407 26, /* bitsize */
408 FALSE, /* pc_relative */
409 0, /* bitpos */
410 complain_overflow_bitfield, /* complain_on_overflow */
411 bfd_elf_generic_reloc, /* special_function */
412 "R_PPC_ADDR24", /* name */
413 FALSE, /* partial_inplace */
414 0, /* src_mask */
415 0x3fffffc, /* dst_mask */
416 FALSE), /* pcrel_offset */
418 /* A standard 16 bit relocation. */
419 HOWTO (R_PPC_ADDR16, /* type */
420 0, /* rightshift */
421 1, /* size (0 = byte, 1 = short, 2 = long) */
422 16, /* bitsize */
423 FALSE, /* pc_relative */
424 0, /* bitpos */
425 complain_overflow_bitfield, /* complain_on_overflow */
426 bfd_elf_generic_reloc, /* special_function */
427 "R_PPC_ADDR16", /* name */
428 FALSE, /* partial_inplace */
429 0, /* src_mask */
430 0xffff, /* dst_mask */
431 FALSE), /* pcrel_offset */
433 /* A 16 bit relocation without overflow. */
434 HOWTO (R_PPC_ADDR16_LO, /* type */
435 0, /* rightshift */
436 1, /* size (0 = byte, 1 = short, 2 = long) */
437 16, /* bitsize */
438 FALSE, /* pc_relative */
439 0, /* bitpos */
440 complain_overflow_dont,/* complain_on_overflow */
441 bfd_elf_generic_reloc, /* special_function */
442 "R_PPC_ADDR16_LO", /* name */
443 FALSE, /* partial_inplace */
444 0, /* src_mask */
445 0xffff, /* dst_mask */
446 FALSE), /* pcrel_offset */
448 /* The high order 16 bits of an address. */
449 HOWTO (R_PPC_ADDR16_HI, /* type */
450 16, /* rightshift */
451 1, /* size (0 = byte, 1 = short, 2 = long) */
452 16, /* bitsize */
453 FALSE, /* pc_relative */
454 0, /* bitpos */
455 complain_overflow_dont, /* complain_on_overflow */
456 bfd_elf_generic_reloc, /* special_function */
457 "R_PPC_ADDR16_HI", /* name */
458 FALSE, /* partial_inplace */
459 0, /* src_mask */
460 0xffff, /* dst_mask */
461 FALSE), /* pcrel_offset */
463 /* The high order 16 bits of an address, plus 1 if the contents of
464 the low 16 bits, treated as a signed number, is negative. */
465 HOWTO (R_PPC_ADDR16_HA, /* type */
466 16, /* rightshift */
467 1, /* size (0 = byte, 1 = short, 2 = long) */
468 16, /* bitsize */
469 FALSE, /* pc_relative */
470 0, /* bitpos */
471 complain_overflow_dont, /* complain_on_overflow */
472 ppc_elf_addr16_ha_reloc, /* special_function */
473 "R_PPC_ADDR16_HA", /* name */
474 FALSE, /* partial_inplace */
475 0, /* src_mask */
476 0xffff, /* dst_mask */
477 FALSE), /* pcrel_offset */
479 /* An absolute 16 bit branch; the lower two bits must be zero.
480 FIXME: we don't check that, we just clear them. */
481 HOWTO (R_PPC_ADDR14, /* type */
482 0, /* rightshift */
483 2, /* size (0 = byte, 1 = short, 2 = long) */
484 16, /* bitsize */
485 FALSE, /* pc_relative */
486 0, /* bitpos */
487 complain_overflow_bitfield, /* complain_on_overflow */
488 bfd_elf_generic_reloc, /* special_function */
489 "R_PPC_ADDR14", /* name */
490 FALSE, /* partial_inplace */
491 0, /* src_mask */
492 0xfffc, /* dst_mask */
493 FALSE), /* pcrel_offset */
495 /* An absolute 16 bit branch, for which bit 10 should be set to
496 indicate that the branch is expected to be taken. The lower two
497 bits must be zero. */
498 HOWTO (R_PPC_ADDR14_BRTAKEN, /* type */
499 0, /* rightshift */
500 2, /* size (0 = byte, 1 = short, 2 = long) */
501 16, /* bitsize */
502 FALSE, /* pc_relative */
503 0, /* bitpos */
504 complain_overflow_bitfield, /* complain_on_overflow */
505 bfd_elf_generic_reloc, /* special_function */
506 "R_PPC_ADDR14_BRTAKEN",/* name */
507 FALSE, /* partial_inplace */
508 0, /* src_mask */
509 0xfffc, /* dst_mask */
510 FALSE), /* pcrel_offset */
512 /* An absolute 16 bit branch, for which bit 10 should be set to
513 indicate that the branch is not expected to be taken. The lower
514 two bits must be zero. */
515 HOWTO (R_PPC_ADDR14_BRNTAKEN, /* type */
516 0, /* rightshift */
517 2, /* size (0 = byte, 1 = short, 2 = long) */
518 16, /* bitsize */
519 FALSE, /* pc_relative */
520 0, /* bitpos */
521 complain_overflow_bitfield, /* complain_on_overflow */
522 bfd_elf_generic_reloc, /* special_function */
523 "R_PPC_ADDR14_BRNTAKEN",/* name */
524 FALSE, /* partial_inplace */
525 0, /* src_mask */
526 0xfffc, /* dst_mask */
527 FALSE), /* pcrel_offset */
529 /* A relative 26 bit branch; the lower two bits must be zero. */
530 HOWTO (R_PPC_REL24, /* type */
531 0, /* rightshift */
532 2, /* size (0 = byte, 1 = short, 2 = long) */
533 26, /* bitsize */
534 TRUE, /* pc_relative */
535 0, /* bitpos */
536 complain_overflow_signed, /* complain_on_overflow */
537 bfd_elf_generic_reloc, /* special_function */
538 "R_PPC_REL24", /* name */
539 FALSE, /* partial_inplace */
540 0, /* src_mask */
541 0x3fffffc, /* dst_mask */
542 TRUE), /* pcrel_offset */
544 /* A relative 16 bit branch; the lower two bits must be zero. */
545 HOWTO (R_PPC_REL14, /* type */
546 0, /* rightshift */
547 2, /* size (0 = byte, 1 = short, 2 = long) */
548 16, /* bitsize */
549 TRUE, /* pc_relative */
550 0, /* bitpos */
551 complain_overflow_signed, /* complain_on_overflow */
552 bfd_elf_generic_reloc, /* special_function */
553 "R_PPC_REL14", /* name */
554 FALSE, /* partial_inplace */
555 0, /* src_mask */
556 0xfffc, /* dst_mask */
557 TRUE), /* pcrel_offset */
559 /* A relative 16 bit branch. Bit 10 should be set to indicate that
560 the branch is expected to be taken. The lower two bits must be
561 zero. */
562 HOWTO (R_PPC_REL14_BRTAKEN, /* type */
563 0, /* rightshift */
564 2, /* size (0 = byte, 1 = short, 2 = long) */
565 16, /* bitsize */
566 TRUE, /* pc_relative */
567 0, /* bitpos */
568 complain_overflow_signed, /* complain_on_overflow */
569 bfd_elf_generic_reloc, /* special_function */
570 "R_PPC_REL14_BRTAKEN", /* name */
571 FALSE, /* partial_inplace */
572 0, /* src_mask */
573 0xfffc, /* dst_mask */
574 TRUE), /* pcrel_offset */
576 /* A relative 16 bit branch. Bit 10 should be set to indicate that
577 the branch is not expected to be taken. The lower two bits must
578 be zero. */
579 HOWTO (R_PPC_REL14_BRNTAKEN, /* type */
580 0, /* rightshift */
581 2, /* size (0 = byte, 1 = short, 2 = long) */
582 16, /* bitsize */
583 TRUE, /* pc_relative */
584 0, /* bitpos */
585 complain_overflow_signed, /* complain_on_overflow */
586 bfd_elf_generic_reloc, /* special_function */
587 "R_PPC_REL14_BRNTAKEN",/* name */
588 FALSE, /* partial_inplace */
589 0, /* src_mask */
590 0xfffc, /* dst_mask */
591 TRUE), /* pcrel_offset */
593 /* Like R_PPC_ADDR16, but referring to the GOT table entry for the
594 symbol. */
595 HOWTO (R_PPC_GOT16, /* type */
596 0, /* rightshift */
597 1, /* size (0 = byte, 1 = short, 2 = long) */
598 16, /* bitsize */
599 FALSE, /* pc_relative */
600 0, /* bitpos */
601 complain_overflow_signed, /* complain_on_overflow */
602 bfd_elf_generic_reloc, /* special_function */
603 "R_PPC_GOT16", /* name */
604 FALSE, /* partial_inplace */
605 0, /* src_mask */
606 0xffff, /* dst_mask */
607 FALSE), /* pcrel_offset */
609 /* Like R_PPC_ADDR16_LO, but referring to the GOT table entry for
610 the symbol. */
611 HOWTO (R_PPC_GOT16_LO, /* type */
612 0, /* rightshift */
613 1, /* size (0 = byte, 1 = short, 2 = long) */
614 16, /* bitsize */
615 FALSE, /* pc_relative */
616 0, /* bitpos */
617 complain_overflow_dont, /* complain_on_overflow */
618 bfd_elf_generic_reloc, /* special_function */
619 "R_PPC_GOT16_LO", /* name */
620 FALSE, /* partial_inplace */
621 0, /* src_mask */
622 0xffff, /* dst_mask */
623 FALSE), /* pcrel_offset */
625 /* Like R_PPC_ADDR16_HI, but referring to the GOT table entry for
626 the symbol. */
627 HOWTO (R_PPC_GOT16_HI, /* type */
628 16, /* rightshift */
629 1, /* size (0 = byte, 1 = short, 2 = long) */
630 16, /* bitsize */
631 FALSE, /* pc_relative */
632 0, /* bitpos */
633 complain_overflow_bitfield, /* complain_on_overflow */
634 bfd_elf_generic_reloc, /* special_function */
635 "R_PPC_GOT16_HI", /* name */
636 FALSE, /* partial_inplace */
637 0, /* src_mask */
638 0xffff, /* dst_mask */
639 FALSE), /* pcrel_offset */
641 /* Like R_PPC_ADDR16_HA, but referring to the GOT table entry for
642 the symbol. */
643 HOWTO (R_PPC_GOT16_HA, /* type */
644 16, /* rightshift */
645 1, /* size (0 = byte, 1 = short, 2 = long) */
646 16, /* bitsize */
647 FALSE, /* pc_relative */
648 0, /* bitpos */
649 complain_overflow_bitfield, /* complain_on_overflow */
650 ppc_elf_addr16_ha_reloc, /* special_function */
651 "R_PPC_GOT16_HA", /* name */
652 FALSE, /* partial_inplace */
653 0, /* src_mask */
654 0xffff, /* dst_mask */
655 FALSE), /* pcrel_offset */
657 /* Like R_PPC_REL24, but referring to the procedure linkage table
658 entry for the symbol. */
659 HOWTO (R_PPC_PLTREL24, /* type */
660 0, /* rightshift */
661 2, /* size (0 = byte, 1 = short, 2 = long) */
662 26, /* bitsize */
663 TRUE, /* pc_relative */
664 0, /* bitpos */
665 complain_overflow_signed, /* complain_on_overflow */
666 bfd_elf_generic_reloc, /* special_function */
667 "R_PPC_PLTREL24", /* name */
668 FALSE, /* partial_inplace */
669 0, /* src_mask */
670 0x3fffffc, /* dst_mask */
671 TRUE), /* pcrel_offset */
673 /* This is used only by the dynamic linker. The symbol should exist
674 both in the object being run and in some shared library. The
675 dynamic linker copies the data addressed by the symbol from the
676 shared library into the object, because the object being
677 run has to have the data at some particular address. */
678 HOWTO (R_PPC_COPY, /* type */
679 0, /* rightshift */
680 2, /* size (0 = byte, 1 = short, 2 = long) */
681 32, /* bitsize */
682 FALSE, /* pc_relative */
683 0, /* bitpos */
684 complain_overflow_bitfield, /* complain_on_overflow */
685 bfd_elf_generic_reloc, /* special_function */
686 "R_PPC_COPY", /* name */
687 FALSE, /* partial_inplace */
688 0, /* src_mask */
689 0, /* dst_mask */
690 FALSE), /* pcrel_offset */
692 /* Like R_PPC_ADDR32, but used when setting global offset table
693 entries. */
694 HOWTO (R_PPC_GLOB_DAT, /* type */
695 0, /* rightshift */
696 2, /* size (0 = byte, 1 = short, 2 = long) */
697 32, /* bitsize */
698 FALSE, /* pc_relative */
699 0, /* bitpos */
700 complain_overflow_bitfield, /* complain_on_overflow */
701 bfd_elf_generic_reloc, /* special_function */
702 "R_PPC_GLOB_DAT", /* name */
703 FALSE, /* partial_inplace */
704 0, /* src_mask */
705 0xffffffff, /* dst_mask */
706 FALSE), /* pcrel_offset */
708 /* Marks a procedure linkage table entry for a symbol. */
709 HOWTO (R_PPC_JMP_SLOT, /* type */
710 0, /* rightshift */
711 2, /* size (0 = byte, 1 = short, 2 = long) */
712 32, /* bitsize */
713 FALSE, /* pc_relative */
714 0, /* bitpos */
715 complain_overflow_bitfield, /* complain_on_overflow */
716 bfd_elf_generic_reloc, /* special_function */
717 "R_PPC_JMP_SLOT", /* name */
718 FALSE, /* partial_inplace */
719 0, /* src_mask */
720 0, /* dst_mask */
721 FALSE), /* pcrel_offset */
723 /* Used only by the dynamic linker. When the object is run, this
724 longword is set to the load address of the object, plus the
725 addend. */
726 HOWTO (R_PPC_RELATIVE, /* type */
727 0, /* rightshift */
728 2, /* size (0 = byte, 1 = short, 2 = long) */
729 32, /* bitsize */
730 FALSE, /* pc_relative */
731 0, /* bitpos */
732 complain_overflow_bitfield, /* complain_on_overflow */
733 bfd_elf_generic_reloc, /* special_function */
734 "R_PPC_RELATIVE", /* name */
735 FALSE, /* partial_inplace */
736 0, /* src_mask */
737 0xffffffff, /* dst_mask */
738 FALSE), /* pcrel_offset */
740 /* Like R_PPC_REL24, but uses the value of the symbol within the
741 object rather than the final value. Normally used for
742 _GLOBAL_OFFSET_TABLE_. */
743 HOWTO (R_PPC_LOCAL24PC, /* type */
744 0, /* rightshift */
745 2, /* size (0 = byte, 1 = short, 2 = long) */
746 26, /* bitsize */
747 TRUE, /* pc_relative */
748 0, /* bitpos */
749 complain_overflow_signed, /* complain_on_overflow */
750 bfd_elf_generic_reloc, /* special_function */
751 "R_PPC_LOCAL24PC", /* name */
752 FALSE, /* partial_inplace */
753 0, /* src_mask */
754 0x3fffffc, /* dst_mask */
755 TRUE), /* pcrel_offset */
757 /* Like R_PPC_ADDR32, but may be unaligned. */
758 HOWTO (R_PPC_UADDR32, /* type */
759 0, /* rightshift */
760 2, /* size (0 = byte, 1 = short, 2 = long) */
761 32, /* bitsize */
762 FALSE, /* pc_relative */
763 0, /* bitpos */
764 complain_overflow_bitfield, /* complain_on_overflow */
765 bfd_elf_generic_reloc, /* special_function */
766 "R_PPC_UADDR32", /* name */
767 FALSE, /* partial_inplace */
768 0, /* src_mask */
769 0xffffffff, /* dst_mask */
770 FALSE), /* pcrel_offset */
772 /* Like R_PPC_ADDR16, but may be unaligned. */
773 HOWTO (R_PPC_UADDR16, /* type */
774 0, /* rightshift */
775 1, /* size (0 = byte, 1 = short, 2 = long) */
776 16, /* bitsize */
777 FALSE, /* pc_relative */
778 0, /* bitpos */
779 complain_overflow_bitfield, /* complain_on_overflow */
780 bfd_elf_generic_reloc, /* special_function */
781 "R_PPC_UADDR16", /* name */
782 FALSE, /* partial_inplace */
783 0, /* src_mask */
784 0xffff, /* dst_mask */
785 FALSE), /* pcrel_offset */
787 /* 32-bit PC relative */
788 HOWTO (R_PPC_REL32, /* type */
789 0, /* rightshift */
790 2, /* size (0 = byte, 1 = short, 2 = long) */
791 32, /* bitsize */
792 TRUE, /* pc_relative */
793 0, /* bitpos */
794 complain_overflow_bitfield, /* complain_on_overflow */
795 bfd_elf_generic_reloc, /* special_function */
796 "R_PPC_REL32", /* name */
797 FALSE, /* partial_inplace */
798 0, /* src_mask */
799 0xffffffff, /* dst_mask */
800 TRUE), /* pcrel_offset */
802 /* 32-bit relocation to the symbol's procedure linkage table.
803 FIXME: not supported. */
804 HOWTO (R_PPC_PLT32, /* type */
805 0, /* rightshift */
806 2, /* size (0 = byte, 1 = short, 2 = long) */
807 32, /* bitsize */
808 FALSE, /* pc_relative */
809 0, /* bitpos */
810 complain_overflow_bitfield, /* complain_on_overflow */
811 bfd_elf_generic_reloc, /* special_function */
812 "R_PPC_PLT32", /* name */
813 FALSE, /* partial_inplace */
814 0, /* src_mask */
815 0, /* dst_mask */
816 FALSE), /* pcrel_offset */
818 /* 32-bit PC relative relocation to the symbol's procedure linkage table.
819 FIXME: not supported. */
820 HOWTO (R_PPC_PLTREL32, /* type */
821 0, /* rightshift */
822 2, /* size (0 = byte, 1 = short, 2 = long) */
823 32, /* bitsize */
824 TRUE, /* pc_relative */
825 0, /* bitpos */
826 complain_overflow_bitfield, /* complain_on_overflow */
827 bfd_elf_generic_reloc, /* special_function */
828 "R_PPC_PLTREL32", /* name */
829 FALSE, /* partial_inplace */
830 0, /* src_mask */
831 0, /* dst_mask */
832 TRUE), /* pcrel_offset */
834 /* Like R_PPC_ADDR16_LO, but referring to the PLT table entry for
835 the symbol. */
836 HOWTO (R_PPC_PLT16_LO, /* type */
837 0, /* rightshift */
838 1, /* size (0 = byte, 1 = short, 2 = long) */
839 16, /* bitsize */
840 FALSE, /* pc_relative */
841 0, /* bitpos */
842 complain_overflow_dont, /* complain_on_overflow */
843 bfd_elf_generic_reloc, /* special_function */
844 "R_PPC_PLT16_LO", /* name */
845 FALSE, /* partial_inplace */
846 0, /* src_mask */
847 0xffff, /* dst_mask */
848 FALSE), /* pcrel_offset */
850 /* Like R_PPC_ADDR16_HI, but referring to the PLT table entry for
851 the symbol. */
852 HOWTO (R_PPC_PLT16_HI, /* type */
853 16, /* rightshift */
854 1, /* size (0 = byte, 1 = short, 2 = long) */
855 16, /* bitsize */
856 FALSE, /* pc_relative */
857 0, /* bitpos */
858 complain_overflow_bitfield, /* complain_on_overflow */
859 bfd_elf_generic_reloc, /* special_function */
860 "R_PPC_PLT16_HI", /* name */
861 FALSE, /* partial_inplace */
862 0, /* src_mask */
863 0xffff, /* dst_mask */
864 FALSE), /* pcrel_offset */
866 /* Like R_PPC_ADDR16_HA, but referring to the PLT table entry for
867 the symbol. */
868 HOWTO (R_PPC_PLT16_HA, /* type */
869 16, /* rightshift */
870 1, /* size (0 = byte, 1 = short, 2 = long) */
871 16, /* bitsize */
872 FALSE, /* pc_relative */
873 0, /* bitpos */
874 complain_overflow_bitfield, /* complain_on_overflow */
875 ppc_elf_addr16_ha_reloc, /* special_function */
876 "R_PPC_PLT16_HA", /* name */
877 FALSE, /* partial_inplace */
878 0, /* src_mask */
879 0xffff, /* dst_mask */
880 FALSE), /* pcrel_offset */
882 /* A sign-extended 16 bit value relative to _SDA_BASE_, for use with
883 small data items. */
884 HOWTO (R_PPC_SDAREL16, /* type */
885 0, /* rightshift */
886 1, /* size (0 = byte, 1 = short, 2 = long) */
887 16, /* bitsize */
888 FALSE, /* pc_relative */
889 0, /* bitpos */
890 complain_overflow_signed, /* complain_on_overflow */
891 bfd_elf_generic_reloc, /* special_function */
892 "R_PPC_SDAREL16", /* name */
893 FALSE, /* partial_inplace */
894 0, /* src_mask */
895 0xffff, /* dst_mask */
896 FALSE), /* pcrel_offset */
898 /* 16-bit section relative relocation. */
899 HOWTO (R_PPC_SECTOFF, /* type */
900 0, /* rightshift */
901 1, /* size (0 = byte, 1 = short, 2 = long) */
902 16, /* bitsize */
903 FALSE, /* pc_relative */
904 0, /* bitpos */
905 complain_overflow_bitfield, /* complain_on_overflow */
906 bfd_elf_generic_reloc, /* special_function */
907 "R_PPC_SECTOFF", /* name */
908 FALSE, /* partial_inplace */
909 0, /* src_mask */
910 0xffff, /* dst_mask */
911 FALSE), /* pcrel_offset */
913 /* 16-bit lower half section relative relocation. */
914 HOWTO (R_PPC_SECTOFF_LO, /* type */
915 0, /* rightshift */
916 1, /* size (0 = byte, 1 = short, 2 = long) */
917 16, /* bitsize */
918 FALSE, /* pc_relative */
919 0, /* bitpos */
920 complain_overflow_dont, /* complain_on_overflow */
921 bfd_elf_generic_reloc, /* special_function */
922 "R_PPC_SECTOFF_LO", /* name */
923 FALSE, /* partial_inplace */
924 0, /* src_mask */
925 0xffff, /* dst_mask */
926 FALSE), /* pcrel_offset */
928 /* 16-bit upper half section relative relocation. */
929 HOWTO (R_PPC_SECTOFF_HI, /* type */
930 16, /* rightshift */
931 1, /* size (0 = byte, 1 = short, 2 = long) */
932 16, /* bitsize */
933 FALSE, /* pc_relative */
934 0, /* bitpos */
935 complain_overflow_bitfield, /* complain_on_overflow */
936 bfd_elf_generic_reloc, /* special_function */
937 "R_PPC_SECTOFF_HI", /* name */
938 FALSE, /* partial_inplace */
939 0, /* src_mask */
940 0xffff, /* dst_mask */
941 FALSE), /* pcrel_offset */
943 /* 16-bit upper half adjusted section relative relocation. */
944 HOWTO (R_PPC_SECTOFF_HA, /* type */
945 16, /* rightshift */
946 1, /* size (0 = byte, 1 = short, 2 = long) */
947 16, /* bitsize */
948 FALSE, /* pc_relative */
949 0, /* bitpos */
950 complain_overflow_bitfield, /* complain_on_overflow */
951 ppc_elf_addr16_ha_reloc, /* special_function */
952 "R_PPC_SECTOFF_HA", /* name */
953 FALSE, /* partial_inplace */
954 0, /* src_mask */
955 0xffff, /* dst_mask */
956 FALSE), /* pcrel_offset */
958 /* Marker reloc for TLS. */
959 HOWTO (R_PPC_TLS,
960 0, /* rightshift */
961 2, /* size (0 = byte, 1 = short, 2 = long) */
962 32, /* bitsize */
963 FALSE, /* pc_relative */
964 0, /* bitpos */
965 complain_overflow_dont, /* complain_on_overflow */
966 bfd_elf_generic_reloc, /* special_function */
967 "R_PPC_TLS", /* name */
968 FALSE, /* partial_inplace */
969 0, /* src_mask */
970 0, /* dst_mask */
971 FALSE), /* pcrel_offset */
973 /* Computes the load module index of the load module that contains the
974 definition of its TLS sym. */
975 HOWTO (R_PPC_DTPMOD32,
976 0, /* rightshift */
977 2, /* size (0 = byte, 1 = short, 2 = long) */
978 32, /* bitsize */
979 FALSE, /* pc_relative */
980 0, /* bitpos */
981 complain_overflow_dont, /* complain_on_overflow */
982 ppc_elf_unhandled_reloc, /* special_function */
983 "R_PPC_DTPMOD32", /* name */
984 FALSE, /* partial_inplace */
985 0, /* src_mask */
986 0xffffffff, /* dst_mask */
987 FALSE), /* pcrel_offset */
989 /* Computes a dtv-relative displacement, the difference between the value
990 of sym+add and the base address of the thread-local storage block that
991 contains the definition of sym, minus 0x8000. */
992 HOWTO (R_PPC_DTPREL32,
993 0, /* rightshift */
994 2, /* size (0 = byte, 1 = short, 2 = long) */
995 32, /* bitsize */
996 FALSE, /* pc_relative */
997 0, /* bitpos */
998 complain_overflow_dont, /* complain_on_overflow */
999 ppc_elf_unhandled_reloc, /* special_function */
1000 "R_PPC_DTPREL32", /* name */
1001 FALSE, /* partial_inplace */
1002 0, /* src_mask */
1003 0xffffffff, /* dst_mask */
1004 FALSE), /* pcrel_offset */
1006 /* A 16 bit dtprel reloc. */
1007 HOWTO (R_PPC_DTPREL16,
1008 0, /* rightshift */
1009 1, /* size (0 = byte, 1 = short, 2 = long) */
1010 16, /* bitsize */
1011 FALSE, /* pc_relative */
1012 0, /* bitpos */
1013 complain_overflow_signed, /* complain_on_overflow */
1014 ppc_elf_unhandled_reloc, /* special_function */
1015 "R_PPC_DTPREL16", /* name */
1016 FALSE, /* partial_inplace */
1017 0, /* src_mask */
1018 0xffff, /* dst_mask */
1019 FALSE), /* pcrel_offset */
1021 /* Like DTPREL16, but no overflow. */
1022 HOWTO (R_PPC_DTPREL16_LO,
1023 0, /* rightshift */
1024 1, /* size (0 = byte, 1 = short, 2 = long) */
1025 16, /* bitsize */
1026 FALSE, /* pc_relative */
1027 0, /* bitpos */
1028 complain_overflow_dont, /* complain_on_overflow */
1029 ppc_elf_unhandled_reloc, /* special_function */
1030 "R_PPC_DTPREL16_LO", /* name */
1031 FALSE, /* partial_inplace */
1032 0, /* src_mask */
1033 0xffff, /* dst_mask */
1034 FALSE), /* pcrel_offset */
1036 /* Like DTPREL16_LO, but next higher group of 16 bits. */
1037 HOWTO (R_PPC_DTPREL16_HI,
1038 16, /* rightshift */
1039 1, /* size (0 = byte, 1 = short, 2 = long) */
1040 16, /* bitsize */
1041 FALSE, /* pc_relative */
1042 0, /* bitpos */
1043 complain_overflow_dont, /* complain_on_overflow */
1044 ppc_elf_unhandled_reloc, /* special_function */
1045 "R_PPC_DTPREL16_HI", /* name */
1046 FALSE, /* partial_inplace */
1047 0, /* src_mask */
1048 0xffff, /* dst_mask */
1049 FALSE), /* pcrel_offset */
1051 /* Like DTPREL16_HI, but adjust for low 16 bits. */
1052 HOWTO (R_PPC_DTPREL16_HA,
1053 16, /* rightshift */
1054 1, /* size (0 = byte, 1 = short, 2 = long) */
1055 16, /* bitsize */
1056 FALSE, /* pc_relative */
1057 0, /* bitpos */
1058 complain_overflow_dont, /* complain_on_overflow */
1059 ppc_elf_unhandled_reloc, /* special_function */
1060 "R_PPC_DTPREL16_HA", /* name */
1061 FALSE, /* partial_inplace */
1062 0, /* src_mask */
1063 0xffff, /* dst_mask */
1064 FALSE), /* pcrel_offset */
1066 /* Computes a tp-relative displacement, the difference between the value of
1067 sym+add and the value of the thread pointer (r13). */
1068 HOWTO (R_PPC_TPREL32,
1069 0, /* rightshift */
1070 2, /* size (0 = byte, 1 = short, 2 = long) */
1071 32, /* bitsize */
1072 FALSE, /* pc_relative */
1073 0, /* bitpos */
1074 complain_overflow_dont, /* complain_on_overflow */
1075 ppc_elf_unhandled_reloc, /* special_function */
1076 "R_PPC_TPREL32", /* name */
1077 FALSE, /* partial_inplace */
1078 0, /* src_mask */
1079 0xffffffff, /* dst_mask */
1080 FALSE), /* pcrel_offset */
1082 /* A 16 bit tprel reloc. */
1083 HOWTO (R_PPC_TPREL16,
1084 0, /* rightshift */
1085 1, /* size (0 = byte, 1 = short, 2 = long) */
1086 16, /* bitsize */
1087 FALSE, /* pc_relative */
1088 0, /* bitpos */
1089 complain_overflow_signed, /* complain_on_overflow */
1090 ppc_elf_unhandled_reloc, /* special_function */
1091 "R_PPC_TPREL16", /* name */
1092 FALSE, /* partial_inplace */
1093 0, /* src_mask */
1094 0xffff, /* dst_mask */
1095 FALSE), /* pcrel_offset */
1097 /* Like TPREL16, but no overflow. */
1098 HOWTO (R_PPC_TPREL16_LO,
1099 0, /* rightshift */
1100 1, /* size (0 = byte, 1 = short, 2 = long) */
1101 16, /* bitsize */
1102 FALSE, /* pc_relative */
1103 0, /* bitpos */
1104 complain_overflow_dont, /* complain_on_overflow */
1105 ppc_elf_unhandled_reloc, /* special_function */
1106 "R_PPC_TPREL16_LO", /* name */
1107 FALSE, /* partial_inplace */
1108 0, /* src_mask */
1109 0xffff, /* dst_mask */
1110 FALSE), /* pcrel_offset */
1112 /* Like TPREL16_LO, but next higher group of 16 bits. */
1113 HOWTO (R_PPC_TPREL16_HI,
1114 16, /* rightshift */
1115 1, /* size (0 = byte, 1 = short, 2 = long) */
1116 16, /* bitsize */
1117 FALSE, /* pc_relative */
1118 0, /* bitpos */
1119 complain_overflow_dont, /* complain_on_overflow */
1120 ppc_elf_unhandled_reloc, /* special_function */
1121 "R_PPC_TPREL16_HI", /* name */
1122 FALSE, /* partial_inplace */
1123 0, /* src_mask */
1124 0xffff, /* dst_mask */
1125 FALSE), /* pcrel_offset */
1127 /* Like TPREL16_HI, but adjust for low 16 bits. */
1128 HOWTO (R_PPC_TPREL16_HA,
1129 16, /* rightshift */
1130 1, /* size (0 = byte, 1 = short, 2 = long) */
1131 16, /* bitsize */
1132 FALSE, /* pc_relative */
1133 0, /* bitpos */
1134 complain_overflow_dont, /* complain_on_overflow */
1135 ppc_elf_unhandled_reloc, /* special_function */
1136 "R_PPC_TPREL16_HA", /* name */
1137 FALSE, /* partial_inplace */
1138 0, /* src_mask */
1139 0xffff, /* dst_mask */
1140 FALSE), /* pcrel_offset */
1142 /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
1143 with values (sym+add)@dtpmod and (sym+add)@dtprel, and computes the offset
1144 to the first entry. */
1145 HOWTO (R_PPC_GOT_TLSGD16,
1146 0, /* rightshift */
1147 1, /* size (0 = byte, 1 = short, 2 = long) */
1148 16, /* bitsize */
1149 FALSE, /* pc_relative */
1150 0, /* bitpos */
1151 complain_overflow_signed, /* complain_on_overflow */
1152 ppc_elf_unhandled_reloc, /* special_function */
1153 "R_PPC_GOT_TLSGD16", /* name */
1154 FALSE, /* partial_inplace */
1155 0, /* src_mask */
1156 0xffff, /* dst_mask */
1157 FALSE), /* pcrel_offset */
1159 /* Like GOT_TLSGD16, but no overflow. */
1160 HOWTO (R_PPC_GOT_TLSGD16_LO,
1161 0, /* rightshift */
1162 1, /* size (0 = byte, 1 = short, 2 = long) */
1163 16, /* bitsize */
1164 FALSE, /* pc_relative */
1165 0, /* bitpos */
1166 complain_overflow_dont, /* complain_on_overflow */
1167 ppc_elf_unhandled_reloc, /* special_function */
1168 "R_PPC_GOT_TLSGD16_LO", /* name */
1169 FALSE, /* partial_inplace */
1170 0, /* src_mask */
1171 0xffff, /* dst_mask */
1172 FALSE), /* pcrel_offset */
1174 /* Like GOT_TLSGD16_LO, but next higher group of 16 bits. */
1175 HOWTO (R_PPC_GOT_TLSGD16_HI,
1176 16, /* rightshift */
1177 1, /* size (0 = byte, 1 = short, 2 = long) */
1178 16, /* bitsize */
1179 FALSE, /* pc_relative */
1180 0, /* bitpos */
1181 complain_overflow_dont, /* complain_on_overflow */
1182 ppc_elf_unhandled_reloc, /* special_function */
1183 "R_PPC_GOT_TLSGD16_HI", /* name */
1184 FALSE, /* partial_inplace */
1185 0, /* src_mask */
1186 0xffff, /* dst_mask */
1187 FALSE), /* pcrel_offset */
1189 /* Like GOT_TLSGD16_HI, but adjust for low 16 bits. */
1190 HOWTO (R_PPC_GOT_TLSGD16_HA,
1191 16, /* rightshift */
1192 1, /* size (0 = byte, 1 = short, 2 = long) */
1193 16, /* bitsize */
1194 FALSE, /* pc_relative */
1195 0, /* bitpos */
1196 complain_overflow_dont, /* complain_on_overflow */
1197 ppc_elf_unhandled_reloc, /* special_function */
1198 "R_PPC_GOT_TLSGD16_HA", /* name */
1199 FALSE, /* partial_inplace */
1200 0, /* src_mask */
1201 0xffff, /* dst_mask */
1202 FALSE), /* pcrel_offset */
1204 /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
1205 with values (sym+add)@dtpmod and zero, and computes the offset to the
1206 first entry. */
1207 HOWTO (R_PPC_GOT_TLSLD16,
1208 0, /* rightshift */
1209 1, /* size (0 = byte, 1 = short, 2 = long) */
1210 16, /* bitsize */
1211 FALSE, /* pc_relative */
1212 0, /* bitpos */
1213 complain_overflow_signed, /* complain_on_overflow */
1214 ppc_elf_unhandled_reloc, /* special_function */
1215 "R_PPC_GOT_TLSLD16", /* name */
1216 FALSE, /* partial_inplace */
1217 0, /* src_mask */
1218 0xffff, /* dst_mask */
1219 FALSE), /* pcrel_offset */
1221 /* Like GOT_TLSLD16, but no overflow. */
1222 HOWTO (R_PPC_GOT_TLSLD16_LO,
1223 0, /* rightshift */
1224 1, /* size (0 = byte, 1 = short, 2 = long) */
1225 16, /* bitsize */
1226 FALSE, /* pc_relative */
1227 0, /* bitpos */
1228 complain_overflow_dont, /* complain_on_overflow */
1229 ppc_elf_unhandled_reloc, /* special_function */
1230 "R_PPC_GOT_TLSLD16_LO", /* name */
1231 FALSE, /* partial_inplace */
1232 0, /* src_mask */
1233 0xffff, /* dst_mask */
1234 FALSE), /* pcrel_offset */
1236 /* Like GOT_TLSLD16_LO, but next higher group of 16 bits. */
1237 HOWTO (R_PPC_GOT_TLSLD16_HI,
1238 16, /* rightshift */
1239 1, /* size (0 = byte, 1 = short, 2 = long) */
1240 16, /* bitsize */
1241 FALSE, /* pc_relative */
1242 0, /* bitpos */
1243 complain_overflow_dont, /* complain_on_overflow */
1244 ppc_elf_unhandled_reloc, /* special_function */
1245 "R_PPC_GOT_TLSLD16_HI", /* name */
1246 FALSE, /* partial_inplace */
1247 0, /* src_mask */
1248 0xffff, /* dst_mask */
1249 FALSE), /* pcrel_offset */
1251 /* Like GOT_TLSLD16_HI, but adjust for low 16 bits. */
1252 HOWTO (R_PPC_GOT_TLSLD16_HA,
1253 16, /* rightshift */
1254 1, /* size (0 = byte, 1 = short, 2 = long) */
1255 16, /* bitsize */
1256 FALSE, /* pc_relative */
1257 0, /* bitpos */
1258 complain_overflow_dont, /* complain_on_overflow */
1259 ppc_elf_unhandled_reloc, /* special_function */
1260 "R_PPC_GOT_TLSLD16_HA", /* name */
1261 FALSE, /* partial_inplace */
1262 0, /* src_mask */
1263 0xffff, /* dst_mask */
1264 FALSE), /* pcrel_offset */
1266 /* Allocates an entry in the GOT with value (sym+add)@dtprel, and computes
1267 the offset to the entry. */
1268 HOWTO (R_PPC_GOT_DTPREL16,
1269 0, /* rightshift */
1270 1, /* size (0 = byte, 1 = short, 2 = long) */
1271 16, /* bitsize */
1272 FALSE, /* pc_relative */
1273 0, /* bitpos */
1274 complain_overflow_signed, /* complain_on_overflow */
1275 ppc_elf_unhandled_reloc, /* special_function */
1276 "R_PPC_GOT_DTPREL16", /* name */
1277 FALSE, /* partial_inplace */
1278 0, /* src_mask */
1279 0xffff, /* dst_mask */
1280 FALSE), /* pcrel_offset */
1282 /* Like GOT_DTPREL16, but no overflow. */
1283 HOWTO (R_PPC_GOT_DTPREL16_LO,
1284 0, /* rightshift */
1285 1, /* size (0 = byte, 1 = short, 2 = long) */
1286 16, /* bitsize */
1287 FALSE, /* pc_relative */
1288 0, /* bitpos */
1289 complain_overflow_dont, /* complain_on_overflow */
1290 ppc_elf_unhandled_reloc, /* special_function */
1291 "R_PPC_GOT_DTPREL16_LO", /* name */
1292 FALSE, /* partial_inplace */
1293 0, /* src_mask */
1294 0xffff, /* dst_mask */
1295 FALSE), /* pcrel_offset */
1297 /* Like GOT_DTPREL16_LO, but next higher group of 16 bits. */
1298 HOWTO (R_PPC_GOT_DTPREL16_HI,
1299 16, /* rightshift */
1300 1, /* size (0 = byte, 1 = short, 2 = long) */
1301 16, /* bitsize */
1302 FALSE, /* pc_relative */
1303 0, /* bitpos */
1304 complain_overflow_dont, /* complain_on_overflow */
1305 ppc_elf_unhandled_reloc, /* special_function */
1306 "R_PPC_GOT_DTPREL16_HI", /* name */
1307 FALSE, /* partial_inplace */
1308 0, /* src_mask */
1309 0xffff, /* dst_mask */
1310 FALSE), /* pcrel_offset */
1312 /* Like GOT_DTPREL16_HI, but adjust for low 16 bits. */
1313 HOWTO (R_PPC_GOT_DTPREL16_HA,
1314 16, /* rightshift */
1315 1, /* size (0 = byte, 1 = short, 2 = long) */
1316 16, /* bitsize */
1317 FALSE, /* pc_relative */
1318 0, /* bitpos */
1319 complain_overflow_dont, /* complain_on_overflow */
1320 ppc_elf_unhandled_reloc, /* special_function */
1321 "R_PPC_GOT_DTPREL16_HA", /* name */
1322 FALSE, /* partial_inplace */
1323 0, /* src_mask */
1324 0xffff, /* dst_mask */
1325 FALSE), /* pcrel_offset */
1327 /* Allocates an entry in the GOT with value (sym+add)@tprel, and computes the
1328 offset to the entry. */
1329 HOWTO (R_PPC_GOT_TPREL16,
1330 0, /* rightshift */
1331 1, /* size (0 = byte, 1 = short, 2 = long) */
1332 16, /* bitsize */
1333 FALSE, /* pc_relative */
1334 0, /* bitpos */
1335 complain_overflow_signed, /* complain_on_overflow */
1336 ppc_elf_unhandled_reloc, /* special_function */
1337 "R_PPC_GOT_TPREL16", /* name */
1338 FALSE, /* partial_inplace */
1339 0, /* src_mask */
1340 0xffff, /* dst_mask */
1341 FALSE), /* pcrel_offset */
1343 /* Like GOT_TPREL16, but no overflow. */
1344 HOWTO (R_PPC_GOT_TPREL16_LO,
1345 0, /* rightshift */
1346 1, /* size (0 = byte, 1 = short, 2 = long) */
1347 16, /* bitsize */
1348 FALSE, /* pc_relative */
1349 0, /* bitpos */
1350 complain_overflow_dont, /* complain_on_overflow */
1351 ppc_elf_unhandled_reloc, /* special_function */
1352 "R_PPC_GOT_TPREL16_LO", /* name */
1353 FALSE, /* partial_inplace */
1354 0, /* src_mask */
1355 0xffff, /* dst_mask */
1356 FALSE), /* pcrel_offset */
1358 /* Like GOT_TPREL16_LO, but next higher group of 16 bits. */
1359 HOWTO (R_PPC_GOT_TPREL16_HI,
1360 16, /* rightshift */
1361 1, /* size (0 = byte, 1 = short, 2 = long) */
1362 16, /* bitsize */
1363 FALSE, /* pc_relative */
1364 0, /* bitpos */
1365 complain_overflow_dont, /* complain_on_overflow */
1366 ppc_elf_unhandled_reloc, /* special_function */
1367 "R_PPC_GOT_TPREL16_HI", /* name */
1368 FALSE, /* partial_inplace */
1369 0, /* src_mask */
1370 0xffff, /* dst_mask */
1371 FALSE), /* pcrel_offset */
1373 /* Like GOT_TPREL16_HI, but adjust for low 16 bits. */
1374 HOWTO (R_PPC_GOT_TPREL16_HA,
1375 16, /* rightshift */
1376 1, /* size (0 = byte, 1 = short, 2 = long) */
1377 16, /* bitsize */
1378 FALSE, /* pc_relative */
1379 0, /* bitpos */
1380 complain_overflow_dont, /* complain_on_overflow */
1381 ppc_elf_unhandled_reloc, /* special_function */
1382 "R_PPC_GOT_TPREL16_HA", /* name */
1383 FALSE, /* partial_inplace */
1384 0, /* src_mask */
1385 0xffff, /* dst_mask */
1386 FALSE), /* pcrel_offset */
1388 /* The remaining relocs are from the Embedded ELF ABI, and are not
1389 in the SVR4 ELF ABI. */
1391 /* 32 bit value resulting from the addend minus the symbol. */
1392 HOWTO (R_PPC_EMB_NADDR32, /* type */
1393 0, /* rightshift */
1394 2, /* size (0 = byte, 1 = short, 2 = long) */
1395 32, /* bitsize */
1396 FALSE, /* pc_relative */
1397 0, /* bitpos */
1398 complain_overflow_bitfield, /* complain_on_overflow */
1399 bfd_elf_generic_reloc, /* special_function */
1400 "R_PPC_EMB_NADDR32", /* name */
1401 FALSE, /* partial_inplace */
1402 0, /* src_mask */
1403 0xffffffff, /* dst_mask */
1404 FALSE), /* pcrel_offset */
1406 /* 16 bit value resulting from the addend minus the symbol. */
1407 HOWTO (R_PPC_EMB_NADDR16, /* type */
1408 0, /* rightshift */
1409 1, /* size (0 = byte, 1 = short, 2 = long) */
1410 16, /* bitsize */
1411 FALSE, /* pc_relative */
1412 0, /* bitpos */
1413 complain_overflow_bitfield, /* complain_on_overflow */
1414 bfd_elf_generic_reloc, /* special_function */
1415 "R_PPC_EMB_NADDR16", /* name */
1416 FALSE, /* partial_inplace */
1417 0, /* src_mask */
1418 0xffff, /* dst_mask */
1419 FALSE), /* pcrel_offset */
1421 /* 16 bit value resulting from the addend minus the symbol. */
1422 HOWTO (R_PPC_EMB_NADDR16_LO, /* type */
1423 0, /* rightshift */
1424 1, /* size (0 = byte, 1 = short, 2 = long) */
1425 16, /* bitsize */
1426 FALSE, /* pc_relative */
1427 0, /* bitpos */
1428 complain_overflow_dont,/* complain_on_overflow */
1429 bfd_elf_generic_reloc, /* special_function */
1430 "R_PPC_EMB_ADDR16_LO", /* name */
1431 FALSE, /* partial_inplace */
1432 0, /* src_mask */
1433 0xffff, /* dst_mask */
1434 FALSE), /* pcrel_offset */
1436 /* The high order 16 bits of the addend minus the symbol. */
1437 HOWTO (R_PPC_EMB_NADDR16_HI, /* type */
1438 16, /* rightshift */
1439 1, /* size (0 = byte, 1 = short, 2 = long) */
1440 16, /* bitsize */
1441 FALSE, /* pc_relative */
1442 0, /* bitpos */
1443 complain_overflow_dont, /* complain_on_overflow */
1444 bfd_elf_generic_reloc, /* special_function */
1445 "R_PPC_EMB_NADDR16_HI", /* name */
1446 FALSE, /* partial_inplace */
1447 0, /* src_mask */
1448 0xffff, /* dst_mask */
1449 FALSE), /* pcrel_offset */
1451 /* The high order 16 bits of the result of the addend minus the address,
1452 plus 1 if the contents of the low 16 bits, treated as a signed number,
1453 is negative. */
1454 HOWTO (R_PPC_EMB_NADDR16_HA, /* type */
1455 16, /* rightshift */
1456 1, /* size (0 = byte, 1 = short, 2 = long) */
1457 16, /* bitsize */
1458 FALSE, /* pc_relative */
1459 0, /* bitpos */
1460 complain_overflow_dont, /* complain_on_overflow */
1461 ppc_elf_addr16_ha_reloc, /* special_function */
1462 "R_PPC_EMB_NADDR16_HA", /* name */
1463 FALSE, /* partial_inplace */
1464 0, /* src_mask */
1465 0xffff, /* dst_mask */
1466 FALSE), /* pcrel_offset */
1468 /* 16 bit value resulting from allocating a 4 byte word to hold an
1469 address in the .sdata section, and returning the offset from
1470 _SDA_BASE_ for that relocation. */
1471 HOWTO (R_PPC_EMB_SDAI16, /* type */
1472 0, /* rightshift */
1473 1, /* size (0 = byte, 1 = short, 2 = long) */
1474 16, /* bitsize */
1475 FALSE, /* pc_relative */
1476 0, /* bitpos */
1477 complain_overflow_bitfield, /* complain_on_overflow */
1478 bfd_elf_generic_reloc, /* special_function */
1479 "R_PPC_EMB_SDAI16", /* name */
1480 FALSE, /* partial_inplace */
1481 0, /* src_mask */
1482 0xffff, /* dst_mask */
1483 FALSE), /* pcrel_offset */
1485 /* 16 bit value resulting from allocating a 4 byte word to hold an
1486 address in the .sdata2 section, and returning the offset from
1487 _SDA2_BASE_ for that relocation. */
1488 HOWTO (R_PPC_EMB_SDA2I16, /* type */
1489 0, /* rightshift */
1490 1, /* size (0 = byte, 1 = short, 2 = long) */
1491 16, /* bitsize */
1492 FALSE, /* pc_relative */
1493 0, /* bitpos */
1494 complain_overflow_bitfield, /* complain_on_overflow */
1495 bfd_elf_generic_reloc, /* special_function */
1496 "R_PPC_EMB_SDA2I16", /* name */
1497 FALSE, /* partial_inplace */
1498 0, /* src_mask */
1499 0xffff, /* dst_mask */
1500 FALSE), /* pcrel_offset */
1502 /* A sign-extended 16 bit value relative to _SDA2_BASE_, for use with
1503 small data items. */
1504 HOWTO (R_PPC_EMB_SDA2REL, /* type */
1505 0, /* rightshift */
1506 1, /* size (0 = byte, 1 = short, 2 = long) */
1507 16, /* bitsize */
1508 FALSE, /* pc_relative */
1509 0, /* bitpos */
1510 complain_overflow_signed, /* complain_on_overflow */
1511 bfd_elf_generic_reloc, /* special_function */
1512 "R_PPC_EMB_SDA2REL", /* name */
1513 FALSE, /* partial_inplace */
1514 0, /* src_mask */
1515 0xffff, /* dst_mask */
1516 FALSE), /* pcrel_offset */
1518 /* Relocate against either _SDA_BASE_ or _SDA2_BASE_, filling in the 16 bit
1519 signed offset from the appropriate base, and filling in the register
1520 field with the appropriate register (0, 2, or 13). */
1521 HOWTO (R_PPC_EMB_SDA21, /* type */
1522 0, /* rightshift */
1523 2, /* size (0 = byte, 1 = short, 2 = long) */
1524 16, /* bitsize */
1525 FALSE, /* pc_relative */
1526 0, /* bitpos */
1527 complain_overflow_signed, /* complain_on_overflow */
1528 bfd_elf_generic_reloc, /* special_function */
1529 "R_PPC_EMB_SDA21", /* name */
1530 FALSE, /* partial_inplace */
1531 0, /* src_mask */
1532 0xffff, /* dst_mask */
1533 FALSE), /* pcrel_offset */
1535 /* Relocation not handled: R_PPC_EMB_MRKREF */
1536 /* Relocation not handled: R_PPC_EMB_RELSEC16 */
1537 /* Relocation not handled: R_PPC_EMB_RELST_LO */
1538 /* Relocation not handled: R_PPC_EMB_RELST_HI */
1539 /* Relocation not handled: R_PPC_EMB_RELST_HA */
1540 /* Relocation not handled: R_PPC_EMB_BIT_FLD */
1542 /* PC relative relocation against either _SDA_BASE_ or _SDA2_BASE_, filling
1543 in the 16 bit signed offset from the appropriate base, and filling in the
1544 register field with the appropriate register (0, 2, or 13). */
1545 HOWTO (R_PPC_EMB_RELSDA, /* type */
1546 0, /* rightshift */
1547 1, /* size (0 = byte, 1 = short, 2 = long) */
1548 16, /* bitsize */
1549 TRUE, /* pc_relative */
1550 0, /* bitpos */
1551 complain_overflow_signed, /* complain_on_overflow */
1552 bfd_elf_generic_reloc, /* special_function */
1553 "R_PPC_EMB_RELSDA", /* name */
1554 FALSE, /* partial_inplace */
1555 0, /* src_mask */
1556 0xffff, /* dst_mask */
1557 FALSE), /* pcrel_offset */
1559 /* GNU extension to record C++ vtable hierarchy. */
1560 HOWTO (R_PPC_GNU_VTINHERIT, /* type */
1561 0, /* rightshift */
1562 0, /* size (0 = byte, 1 = short, 2 = long) */
1563 0, /* bitsize */
1564 FALSE, /* pc_relative */
1565 0, /* bitpos */
1566 complain_overflow_dont, /* complain_on_overflow */
1567 NULL, /* special_function */
1568 "R_PPC_GNU_VTINHERIT", /* name */
1569 FALSE, /* partial_inplace */
1570 0, /* src_mask */
1571 0, /* dst_mask */
1572 FALSE), /* pcrel_offset */
1574 /* GNU extension to record C++ vtable member usage. */
1575 HOWTO (R_PPC_GNU_VTENTRY, /* type */
1576 0, /* rightshift */
1577 0, /* size (0 = byte, 1 = short, 2 = long) */
1578 0, /* bitsize */
1579 FALSE, /* pc_relative */
1580 0, /* bitpos */
1581 complain_overflow_dont, /* complain_on_overflow */
1582 NULL, /* special_function */
1583 "R_PPC_GNU_VTENTRY", /* name */
1584 FALSE, /* partial_inplace */
1585 0, /* src_mask */
1586 0, /* dst_mask */
1587 FALSE), /* pcrel_offset */
1589 /* Phony reloc to handle AIX style TOC entries. */
1590 HOWTO (R_PPC_TOC16, /* type */
1591 0, /* rightshift */
1592 1, /* size (0 = byte, 1 = short, 2 = long) */
1593 16, /* bitsize */
1594 FALSE, /* pc_relative */
1595 0, /* bitpos */
1596 complain_overflow_signed, /* complain_on_overflow */
1597 bfd_elf_generic_reloc, /* special_function */
1598 "R_PPC_TOC16", /* name */
1599 FALSE, /* partial_inplace */
1600 0, /* src_mask */
1601 0xffff, /* dst_mask */
1602 FALSE), /* pcrel_offset */
1605 /* Initialize the ppc_elf_howto_table, so that linear accesses can be done. */
1607 static void
1608 ppc_elf_howto_init ()
1610 unsigned int i, type;
1612 for (i = 0; i < sizeof (ppc_elf_howto_raw) / sizeof (ppc_elf_howto_raw[0]); i++)
1614 type = ppc_elf_howto_raw[i].type;
1615 BFD_ASSERT (type < sizeof (ppc_elf_howto_table) / sizeof (ppc_elf_howto_table[0]));
1616 ppc_elf_howto_table[type] = &ppc_elf_howto_raw[i];
1620 /* This function handles relaxing for the PPC with option --mpc860c0[=<n>].
1622 The MPC860, revision C0 or earlier contains a bug in the die.
1623 If all of the following conditions are true, the next instruction
1624 to be executed *may* be treated as a no-op.
1625 1/ A forward branch is executed.
1626 2/ The branch is predicted as not taken.
1627 3/ The branch is taken.
1628 4/ The branch is located in the last 5 words of a page.
1629 (The EOP limit is 5 by default but may be specified as any value from 1-10.)
1631 Our software solution is to detect these problematic branches in a
1632 linker pass and modify them as follows:
1633 1/ Unconditional branches - Since these are always predicted taken,
1634 there is no problem and no action is required.
1635 2/ Conditional backward branches - No problem, no action required.
1636 3/ Conditional forward branches - Ensure that the "inverse prediction
1637 bit" is set (ensure it is predicted taken).
1638 4/ Conditional register branches - Ensure that the "y bit" is set
1639 (ensure it is predicted taken). */
1641 /* Sort sections by address. */
1643 static int
1644 ppc_elf_sort_rela (arg1, arg2)
1645 const PTR arg1;
1646 const PTR arg2;
1648 const Elf_Internal_Rela **rela1 = (const Elf_Internal_Rela**) arg1;
1649 const Elf_Internal_Rela **rela2 = (const Elf_Internal_Rela**) arg2;
1651 /* Sort by offset. */
1652 return ((*rela1)->r_offset - (*rela2)->r_offset);
1655 static bfd_boolean
1656 ppc_elf_relax_section (abfd, isec, link_info, again)
1657 bfd *abfd;
1658 asection *isec;
1659 struct bfd_link_info *link_info;
1660 bfd_boolean *again;
1662 #define PAGESIZE 0x1000
1664 bfd_byte *contents = NULL;
1665 bfd_byte *free_contents = NULL;
1666 Elf_Internal_Rela *internal_relocs = NULL;
1667 Elf_Internal_Rela *free_relocs = NULL;
1668 Elf_Internal_Rela **rela_comb = NULL;
1669 int comb_curr, comb_count;
1671 /* We never have to do this more than once per input section. */
1672 *again = FALSE;
1674 /* If needed, initialize this section's cooked size. */
1675 if (isec->_cooked_size == 0)
1676 isec->_cooked_size = isec->_raw_size;
1678 /* We're only interested in text sections which overlap the
1679 troublesome area at the end of a page. */
1680 if (link_info->mpc860c0 && (isec->flags & SEC_CODE) && isec->_cooked_size)
1682 bfd_vma dot, end_page, end_section;
1683 bfd_boolean section_modified;
1685 /* Get the section contents. */
1686 /* Get cached copy if it exists. */
1687 if (elf_section_data (isec)->this_hdr.contents != NULL)
1688 contents = elf_section_data (isec)->this_hdr.contents;
1689 else
1691 /* Go get them off disk. */
1692 contents = (bfd_byte *) bfd_malloc (isec->_raw_size);
1693 if (contents == NULL)
1694 goto error_return;
1695 free_contents = contents;
1697 if (! bfd_get_section_contents (abfd, isec, contents,
1698 (file_ptr) 0, isec->_raw_size))
1699 goto error_return;
1702 comb_curr = 0;
1703 comb_count = 0;
1704 if (isec->reloc_count)
1706 unsigned n;
1707 bfd_size_type amt;
1709 /* Get a copy of the native relocations. */
1710 internal_relocs
1711 = _bfd_elf32_link_read_relocs (abfd, isec, (PTR) NULL,
1712 (Elf_Internal_Rela *) NULL,
1713 link_info->keep_memory);
1714 if (internal_relocs == NULL)
1715 goto error_return;
1716 if (! link_info->keep_memory)
1717 free_relocs = internal_relocs;
1719 /* Setup a faster access method for the reloc info we need. */
1720 amt = isec->reloc_count;
1721 amt *= sizeof (Elf_Internal_Rela*);
1722 rela_comb = (Elf_Internal_Rela**) bfd_malloc (amt);
1723 if (rela_comb == NULL)
1724 goto error_return;
1725 for (n = 0; n < isec->reloc_count; ++n)
1727 long r_type;
1729 r_type = ELF32_R_TYPE (internal_relocs[n].r_info);
1730 if (r_type < 0 || r_type >= (int) R_PPC_max)
1731 goto error_return;
1733 /* Prologue constants are sometimes present in the ".text"
1734 sections and they can be identified by their associated
1735 relocation. We don't want to process those words and
1736 some others which can also be identified by their
1737 relocations. However, not all conditional branches will
1738 have a relocation so we will only ignore words that
1739 1) have a reloc, and 2) the reloc is not applicable to a
1740 conditional branch. The array rela_comb is built here
1741 for use in the EOP scan loop. */
1742 switch (r_type)
1744 case R_PPC_ADDR14_BRNTAKEN:
1745 case R_PPC_REL14:
1746 case R_PPC_REL14_BRNTAKEN:
1747 /* We should check the instruction. */
1748 break;
1749 default:
1750 /* The word is not a conditional branch - ignore it. */
1751 rela_comb[comb_count++] = &internal_relocs[n];
1752 break;
1755 if (comb_count > 1)
1756 qsort (rela_comb, (size_t) comb_count, sizeof (int),
1757 ppc_elf_sort_rela);
1760 /* Enumerate each EOP region that overlaps this section. */
1761 end_section = isec->vma + isec->_cooked_size;
1762 dot = end_page = (isec->vma | (PAGESIZE - 1)) + 1;
1763 dot -= link_info->mpc860c0;
1764 section_modified = FALSE;
1765 /* Increment the start position if this section begins in the
1766 middle of its first EOP region. */
1767 if (dot < isec->vma)
1768 dot = isec->vma;
1769 for (;
1770 dot < end_section;
1771 dot += PAGESIZE, end_page += PAGESIZE)
1773 /* Check each word in this EOP region. */
1774 for (; dot < end_page; dot += 4)
1776 bfd_vma isec_offset;
1777 unsigned long insn;
1778 bfd_boolean skip, modified;
1780 /* Don't process this word if there is a relocation for it
1781 and the relocation indicates the word is not a
1782 conditional branch. */
1783 skip = FALSE;
1784 isec_offset = dot - isec->vma;
1785 for (; comb_curr<comb_count; ++comb_curr)
1787 bfd_vma r_offset;
1789 r_offset = rela_comb[comb_curr]->r_offset;
1790 if (r_offset >= isec_offset)
1792 if (r_offset == isec_offset) skip = TRUE;
1793 break;
1796 if (skip) continue;
1798 /* Check the current word for a problematic conditional
1799 branch. */
1800 #define BO0(insn) ((insn) & 0x02000000)
1801 #define BO2(insn) ((insn) & 0x00800000)
1802 #define BO4(insn) ((insn) & 0x00200000)
1803 insn = (unsigned long) bfd_get_32 (abfd, contents + isec_offset);
1804 modified = FALSE;
1805 if ((insn & 0xFc000000) == 0x40000000)
1807 /* Instruction is BCx */
1808 if ((!BO0(insn) || !BO2(insn)) && !BO4(insn))
1810 bfd_vma target;
1812 /* This branch is predicted as "normal".
1813 If this is a forward branch, it is problematic. */
1814 target = insn & 0x0000Fffc;
1815 target = (target ^ 0x8000) - 0x8000;
1816 if ((insn & 0x00000002) == 0)
1817 /* Convert to abs. */
1818 target += dot;
1819 if (target > dot)
1821 /* Set the prediction bit. */
1822 insn |= 0x00200000;
1823 modified = TRUE;
1827 else if ((insn & 0xFc00Fffe) == 0x4c000420)
1829 /* Instruction is BCCTRx. */
1830 if ((!BO0(insn) || !BO2(insn)) && !BO4(insn))
1832 /* This branch is predicted as not-taken.
1833 If this is a forward branch, it is problematic.
1834 Since we can't tell statically if it will branch
1835 forward, always set the prediction bit. */
1836 insn |= 0x00200000;
1837 modified = TRUE;
1840 else if ((insn & 0xFc00Fffe) == 0x4c000020)
1842 /* Instruction is BCLRx */
1843 if ((!BO0(insn) || !BO2(insn)) && !BO4(insn))
1845 /* This branch is predicted as not-taken.
1846 If this is a forward branch, it is problematic.
1847 Since we can't tell statically if it will branch
1848 forward, always set the prediction bit. */
1849 insn |= 0x00200000;
1850 modified = TRUE;
1853 #undef BO0
1854 #undef BO2
1855 #undef BO4
1856 if (modified)
1858 bfd_put_32 (abfd, (bfd_vma) insn, contents + isec_offset);
1859 section_modified = TRUE;
1863 if (section_modified)
1865 elf_section_data (isec)->this_hdr.contents = contents;
1866 free_contents = NULL;
1870 if (rela_comb != NULL)
1872 free (rela_comb);
1873 rela_comb = NULL;
1876 if (free_relocs != NULL)
1878 free (free_relocs);
1879 free_relocs = NULL;
1882 if (free_contents != NULL)
1884 if (! link_info->keep_memory)
1885 free (free_contents);
1886 else
1888 /* Cache the section contents for elf_link_input_bfd. */
1889 elf_section_data (isec)->this_hdr.contents = contents;
1891 free_contents = NULL;
1894 return TRUE;
1896 error_return:
1897 if (rela_comb != NULL)
1898 free (rela_comb);
1899 if (free_relocs != NULL)
1900 free (free_relocs);
1901 if (free_contents != NULL)
1902 free (free_contents);
1903 return FALSE;
1906 static reloc_howto_type *
1907 ppc_elf_reloc_type_lookup (abfd, code)
1908 bfd *abfd ATTRIBUTE_UNUSED;
1909 bfd_reloc_code_real_type code;
1911 enum elf_ppc_reloc_type r;
1913 if (!ppc_elf_howto_table[R_PPC_ADDR32])
1914 /* Initialize howto table if needed. */
1915 ppc_elf_howto_init ();
1917 switch ((int) code)
1919 default:
1920 return (reloc_howto_type *) NULL;
1922 case BFD_RELOC_NONE: r = R_PPC_NONE; break;
1923 case BFD_RELOC_32: r = R_PPC_ADDR32; break;
1924 case BFD_RELOC_PPC_BA26: r = R_PPC_ADDR24; break;
1925 case BFD_RELOC_16: r = R_PPC_ADDR16; break;
1926 case BFD_RELOC_LO16: r = R_PPC_ADDR16_LO; break;
1927 case BFD_RELOC_HI16: r = R_PPC_ADDR16_HI; break;
1928 case BFD_RELOC_HI16_S: r = R_PPC_ADDR16_HA; break;
1929 case BFD_RELOC_PPC_BA16: r = R_PPC_ADDR14; break;
1930 case BFD_RELOC_PPC_BA16_BRTAKEN: r = R_PPC_ADDR14_BRTAKEN; break;
1931 case BFD_RELOC_PPC_BA16_BRNTAKEN: r = R_PPC_ADDR14_BRNTAKEN; break;
1932 case BFD_RELOC_PPC_B26: r = R_PPC_REL24; break;
1933 case BFD_RELOC_PPC_B16: r = R_PPC_REL14; break;
1934 case BFD_RELOC_PPC_B16_BRTAKEN: r = R_PPC_REL14_BRTAKEN; break;
1935 case BFD_RELOC_PPC_B16_BRNTAKEN: r = R_PPC_REL14_BRNTAKEN; break;
1936 case BFD_RELOC_16_GOTOFF: r = R_PPC_GOT16; break;
1937 case BFD_RELOC_LO16_GOTOFF: r = R_PPC_GOT16_LO; break;
1938 case BFD_RELOC_HI16_GOTOFF: r = R_PPC_GOT16_HI; break;
1939 case BFD_RELOC_HI16_S_GOTOFF: r = R_PPC_GOT16_HA; break;
1940 case BFD_RELOC_24_PLT_PCREL: r = R_PPC_PLTREL24; break;
1941 case BFD_RELOC_PPC_COPY: r = R_PPC_COPY; break;
1942 case BFD_RELOC_PPC_GLOB_DAT: r = R_PPC_GLOB_DAT; break;
1943 case BFD_RELOC_PPC_LOCAL24PC: r = R_PPC_LOCAL24PC; break;
1944 case BFD_RELOC_32_PCREL: r = R_PPC_REL32; break;
1945 case BFD_RELOC_32_PLTOFF: r = R_PPC_PLT32; break;
1946 case BFD_RELOC_32_PLT_PCREL: r = R_PPC_PLTREL32; break;
1947 case BFD_RELOC_LO16_PLTOFF: r = R_PPC_PLT16_LO; break;
1948 case BFD_RELOC_HI16_PLTOFF: r = R_PPC_PLT16_HI; break;
1949 case BFD_RELOC_HI16_S_PLTOFF: r = R_PPC_PLT16_HA; break;
1950 case BFD_RELOC_GPREL16: r = R_PPC_SDAREL16; break;
1951 case BFD_RELOC_16_BASEREL: r = R_PPC_SECTOFF; break;
1952 case BFD_RELOC_LO16_BASEREL: r = R_PPC_SECTOFF_LO; break;
1953 case BFD_RELOC_HI16_BASEREL: r = R_PPC_SECTOFF_HI; break;
1954 case BFD_RELOC_HI16_S_BASEREL: r = R_PPC_SECTOFF_HA; break;
1955 case BFD_RELOC_CTOR: r = R_PPC_ADDR32; break;
1956 case BFD_RELOC_PPC_TOC16: r = R_PPC_TOC16; break;
1957 case BFD_RELOC_PPC_TLS: r = R_PPC_TLS; break;
1958 case BFD_RELOC_PPC_DTPMOD: r = R_PPC_DTPMOD32; break;
1959 case BFD_RELOC_PPC_TPREL16: r = R_PPC_TPREL16; break;
1960 case BFD_RELOC_PPC_TPREL16_LO: r = R_PPC_TPREL16_LO; break;
1961 case BFD_RELOC_PPC_TPREL16_HI: r = R_PPC_TPREL16_HI; break;
1962 case BFD_RELOC_PPC_TPREL16_HA: r = R_PPC_TPREL16_HA; break;
1963 case BFD_RELOC_PPC_TPREL: r = R_PPC_TPREL32; break;
1964 case BFD_RELOC_PPC_DTPREL16: r = R_PPC_DTPREL16; break;
1965 case BFD_RELOC_PPC_DTPREL16_LO: r = R_PPC_DTPREL16_LO; break;
1966 case BFD_RELOC_PPC_DTPREL16_HI: r = R_PPC_DTPREL16_HI; break;
1967 case BFD_RELOC_PPC_DTPREL16_HA: r = R_PPC_DTPREL16_HA; break;
1968 case BFD_RELOC_PPC_DTPREL: r = R_PPC_DTPREL32; break;
1969 case BFD_RELOC_PPC_GOT_TLSGD16: r = R_PPC_GOT_TLSGD16; break;
1970 case BFD_RELOC_PPC_GOT_TLSGD16_LO: r = R_PPC_GOT_TLSGD16_LO; break;
1971 case BFD_RELOC_PPC_GOT_TLSGD16_HI: r = R_PPC_GOT_TLSGD16_HI; break;
1972 case BFD_RELOC_PPC_GOT_TLSGD16_HA: r = R_PPC_GOT_TLSGD16_HA; break;
1973 case BFD_RELOC_PPC_GOT_TLSLD16: r = R_PPC_GOT_TLSLD16; break;
1974 case BFD_RELOC_PPC_GOT_TLSLD16_LO: r = R_PPC_GOT_TLSLD16_LO; break;
1975 case BFD_RELOC_PPC_GOT_TLSLD16_HI: r = R_PPC_GOT_TLSLD16_HI; break;
1976 case BFD_RELOC_PPC_GOT_TLSLD16_HA: r = R_PPC_GOT_TLSLD16_HA; break;
1977 case BFD_RELOC_PPC_GOT_TPREL16: r = R_PPC_GOT_TPREL16; break;
1978 case BFD_RELOC_PPC_GOT_TPREL16_LO: r = R_PPC_GOT_TPREL16_LO; break;
1979 case BFD_RELOC_PPC_GOT_TPREL16_HI: r = R_PPC_GOT_TPREL16_HI; break;
1980 case BFD_RELOC_PPC_GOT_TPREL16_HA: r = R_PPC_GOT_TPREL16_HA; break;
1981 case BFD_RELOC_PPC_GOT_DTPREL16: r = R_PPC_GOT_DTPREL16; break;
1982 case BFD_RELOC_PPC_GOT_DTPREL16_LO: r = R_PPC_GOT_DTPREL16_LO; break;
1983 case BFD_RELOC_PPC_GOT_DTPREL16_HI: r = R_PPC_GOT_DTPREL16_HI; break;
1984 case BFD_RELOC_PPC_GOT_DTPREL16_HA: r = R_PPC_GOT_DTPREL16_HA; break;
1985 case BFD_RELOC_PPC_EMB_NADDR32: r = R_PPC_EMB_NADDR32; break;
1986 case BFD_RELOC_PPC_EMB_NADDR16: r = R_PPC_EMB_NADDR16; break;
1987 case BFD_RELOC_PPC_EMB_NADDR16_LO: r = R_PPC_EMB_NADDR16_LO; break;
1988 case BFD_RELOC_PPC_EMB_NADDR16_HI: r = R_PPC_EMB_NADDR16_HI; break;
1989 case BFD_RELOC_PPC_EMB_NADDR16_HA: r = R_PPC_EMB_NADDR16_HA; break;
1990 case BFD_RELOC_PPC_EMB_SDAI16: r = R_PPC_EMB_SDAI16; break;
1991 case BFD_RELOC_PPC_EMB_SDA2I16: r = R_PPC_EMB_SDA2I16; break;
1992 case BFD_RELOC_PPC_EMB_SDA2REL: r = R_PPC_EMB_SDA2REL; break;
1993 case BFD_RELOC_PPC_EMB_SDA21: r = R_PPC_EMB_SDA21; break;
1994 case BFD_RELOC_PPC_EMB_MRKREF: r = R_PPC_EMB_MRKREF; break;
1995 case BFD_RELOC_PPC_EMB_RELSEC16: r = R_PPC_EMB_RELSEC16; break;
1996 case BFD_RELOC_PPC_EMB_RELST_LO: r = R_PPC_EMB_RELST_LO; break;
1997 case BFD_RELOC_PPC_EMB_RELST_HI: r = R_PPC_EMB_RELST_HI; break;
1998 case BFD_RELOC_PPC_EMB_RELST_HA: r = R_PPC_EMB_RELST_HA; break;
1999 case BFD_RELOC_PPC_EMB_BIT_FLD: r = R_PPC_EMB_BIT_FLD; break;
2000 case BFD_RELOC_PPC_EMB_RELSDA: r = R_PPC_EMB_RELSDA; break;
2001 case BFD_RELOC_VTABLE_INHERIT: r = R_PPC_GNU_VTINHERIT; break;
2002 case BFD_RELOC_VTABLE_ENTRY: r = R_PPC_GNU_VTENTRY; break;
2005 return ppc_elf_howto_table[(int) r];
2008 /* Set the howto pointer for a PowerPC ELF reloc. */
2010 static void
2011 ppc_elf_info_to_howto (abfd, cache_ptr, dst)
2012 bfd *abfd ATTRIBUTE_UNUSED;
2013 arelent *cache_ptr;
2014 Elf_Internal_Rela *dst;
2016 if (!ppc_elf_howto_table[R_PPC_ADDR32])
2017 /* Initialize howto table if needed. */
2018 ppc_elf_howto_init ();
2020 BFD_ASSERT (ELF32_R_TYPE (dst->r_info) < (unsigned int) R_PPC_max);
2021 cache_ptr->howto = ppc_elf_howto_table[ELF32_R_TYPE (dst->r_info)];
2024 /* Handle the R_PPC_ADDR16_HA reloc. */
2026 static bfd_reloc_status_type
2027 ppc_elf_addr16_ha_reloc (abfd, reloc_entry, symbol, data, input_section,
2028 output_bfd, error_message)
2029 bfd *abfd ATTRIBUTE_UNUSED;
2030 arelent *reloc_entry;
2031 asymbol *symbol;
2032 PTR data ATTRIBUTE_UNUSED;
2033 asection *input_section;
2034 bfd *output_bfd;
2035 char **error_message ATTRIBUTE_UNUSED;
2037 bfd_vma relocation;
2039 if (output_bfd != NULL)
2041 reloc_entry->address += input_section->output_offset;
2042 return bfd_reloc_ok;
2045 if (reloc_entry->address > input_section->_cooked_size)
2046 return bfd_reloc_outofrange;
2048 if (bfd_is_com_section (symbol->section))
2049 relocation = 0;
2050 else
2051 relocation = symbol->value;
2053 relocation += symbol->section->output_section->vma;
2054 relocation += symbol->section->output_offset;
2055 relocation += reloc_entry->addend;
2057 reloc_entry->addend += (relocation & 0x8000) << 1;
2059 return bfd_reloc_continue;
2062 static bfd_reloc_status_type
2063 ppc_elf_unhandled_reloc (abfd, reloc_entry, symbol, data,
2064 input_section, output_bfd, error_message)
2065 bfd *abfd;
2066 arelent *reloc_entry;
2067 asymbol *symbol;
2068 PTR data;
2069 asection *input_section;
2070 bfd *output_bfd;
2071 char **error_message;
2073 /* If this is a relocatable link (output_bfd test tells us), just
2074 call the generic function. Any adjustment will be done at final
2075 link time. */
2076 if (output_bfd != NULL)
2077 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2078 input_section, output_bfd, error_message);
2080 if (error_message != NULL)
2082 static char buf[60];
2083 sprintf (buf, "generic linker can't handle %s",
2084 reloc_entry->howto->name);
2085 *error_message = buf;
2087 return bfd_reloc_dangerous;
2090 /* Fix bad default arch selected for a 32 bit input bfd when the
2091 default is 64 bit. */
2093 static bfd_boolean
2094 ppc_elf_object_p (abfd)
2095 bfd *abfd;
2097 if (abfd->arch_info->the_default && abfd->arch_info->bits_per_word == 64)
2099 Elf_Internal_Ehdr *i_ehdr = elf_elfheader (abfd);
2101 if (i_ehdr->e_ident[EI_CLASS] == ELFCLASS32)
2103 /* Relies on arch after 64 bit default being 32 bit default. */
2104 abfd->arch_info = abfd->arch_info->next;
2105 BFD_ASSERT (abfd->arch_info->bits_per_word == 32);
2108 return TRUE;
2111 /* Function to set whether a module needs the -mrelocatable bit set. */
2113 static bfd_boolean
2114 ppc_elf_set_private_flags (abfd, flags)
2115 bfd *abfd;
2116 flagword flags;
2118 BFD_ASSERT (!elf_flags_init (abfd)
2119 || elf_elfheader (abfd)->e_flags == flags);
2121 elf_elfheader (abfd)->e_flags = flags;
2122 elf_flags_init (abfd) = TRUE;
2123 return TRUE;
2126 /* Merge backend specific data from an object file to the output
2127 object file when linking. */
2129 static bfd_boolean
2130 ppc_elf_merge_private_bfd_data (ibfd, obfd)
2131 bfd *ibfd;
2132 bfd *obfd;
2134 flagword old_flags;
2135 flagword new_flags;
2136 bfd_boolean error;
2138 /* Check if we have the same endianess. */
2139 if (! _bfd_generic_verify_endian_match (ibfd, obfd))
2140 return FALSE;
2142 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
2143 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
2144 return TRUE;
2146 new_flags = elf_elfheader (ibfd)->e_flags;
2147 old_flags = elf_elfheader (obfd)->e_flags;
2148 if (!elf_flags_init (obfd))
2150 /* First call, no flags set. */
2151 elf_flags_init (obfd) = TRUE;
2152 elf_elfheader (obfd)->e_flags = new_flags;
2155 /* Compatible flags are ok. */
2156 else if (new_flags == old_flags)
2159 /* Incompatible flags. */
2160 else
2162 /* Warn about -mrelocatable mismatch. Allow -mrelocatable-lib
2163 to be linked with either. */
2164 error = FALSE;
2165 if ((new_flags & EF_PPC_RELOCATABLE) != 0
2166 && (old_flags & (EF_PPC_RELOCATABLE | EF_PPC_RELOCATABLE_LIB)) == 0)
2168 error = TRUE;
2169 (*_bfd_error_handler)
2170 (_("%s: compiled with -mrelocatable and linked with modules compiled normally"),
2171 bfd_archive_filename (ibfd));
2173 else if ((new_flags & (EF_PPC_RELOCATABLE | EF_PPC_RELOCATABLE_LIB)) == 0
2174 && (old_flags & EF_PPC_RELOCATABLE) != 0)
2176 error = TRUE;
2177 (*_bfd_error_handler)
2178 (_("%s: compiled normally and linked with modules compiled with -mrelocatable"),
2179 bfd_archive_filename (ibfd));
2182 /* The output is -mrelocatable-lib iff both the input files are. */
2183 if (! (new_flags & EF_PPC_RELOCATABLE_LIB))
2184 elf_elfheader (obfd)->e_flags &= ~EF_PPC_RELOCATABLE_LIB;
2186 /* The output is -mrelocatable iff it can't be -mrelocatable-lib,
2187 but each input file is either -mrelocatable or -mrelocatable-lib. */
2188 if (! (elf_elfheader (obfd)->e_flags & EF_PPC_RELOCATABLE_LIB)
2189 && (new_flags & (EF_PPC_RELOCATABLE_LIB | EF_PPC_RELOCATABLE))
2190 && (old_flags & (EF_PPC_RELOCATABLE_LIB | EF_PPC_RELOCATABLE)))
2191 elf_elfheader (obfd)->e_flags |= EF_PPC_RELOCATABLE;
2193 /* Do not warn about eabi vs. V.4 mismatch, just or in the bit if
2194 any module uses it. */
2195 elf_elfheader (obfd)->e_flags |= (new_flags & EF_PPC_EMB);
2197 new_flags &= ~(EF_PPC_RELOCATABLE | EF_PPC_RELOCATABLE_LIB | EF_PPC_EMB);
2198 old_flags &= ~(EF_PPC_RELOCATABLE | EF_PPC_RELOCATABLE_LIB | EF_PPC_EMB);
2200 /* Warn about any other mismatches. */
2201 if (new_flags != old_flags)
2203 error = TRUE;
2204 (*_bfd_error_handler)
2205 (_("%s: uses different e_flags (0x%lx) fields than previous modules (0x%lx)"),
2206 bfd_archive_filename (ibfd), (long) new_flags, (long) old_flags);
2209 if (error)
2211 bfd_set_error (bfd_error_bad_value);
2212 return FALSE;
2216 return TRUE;
2219 /* Handle a PowerPC specific section when reading an object file. This
2220 is called when elfcode.h finds a section with an unknown type. */
2222 static bfd_boolean
2223 ppc_elf_section_from_shdr (abfd, hdr, name)
2224 bfd *abfd;
2225 Elf_Internal_Shdr *hdr;
2226 const char *name;
2228 asection *newsect;
2229 flagword flags;
2231 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name))
2232 return FALSE;
2234 newsect = hdr->bfd_section;
2235 flags = bfd_get_section_flags (abfd, newsect);
2236 if (hdr->sh_flags & SHF_EXCLUDE)
2237 flags |= SEC_EXCLUDE;
2239 if (hdr->sh_type == SHT_ORDERED)
2240 flags |= SEC_SORT_ENTRIES;
2242 bfd_set_section_flags (abfd, newsect, flags);
2243 return TRUE;
2246 /* Set up any other section flags and such that may be necessary. */
2248 static bfd_boolean
2249 ppc_elf_fake_sections (abfd, shdr, asect)
2250 bfd *abfd ATTRIBUTE_UNUSED;
2251 Elf_Internal_Shdr *shdr;
2252 asection *asect;
2254 if ((asect->flags & SEC_EXCLUDE) != 0)
2255 shdr->sh_flags |= SHF_EXCLUDE;
2257 if ((asect->flags & SEC_SORT_ENTRIES) != 0)
2258 shdr->sh_type = SHT_ORDERED;
2260 return TRUE;
2263 /* Create a special linker section */
2264 static elf_linker_section_t *
2265 ppc_elf_create_linker_section (abfd, info, which)
2266 bfd *abfd;
2267 struct bfd_link_info *info;
2268 enum elf_linker_section_enum which;
2270 bfd *dynobj = elf_hash_table (info)->dynobj;
2271 elf_linker_section_t *lsect;
2273 /* Record the first bfd section that needs the special section. */
2274 if (!dynobj)
2275 dynobj = elf_hash_table (info)->dynobj = abfd;
2277 /* If this is the first time, create the section. */
2278 lsect = elf_linker_section (dynobj, which);
2279 if (!lsect)
2281 elf_linker_section_t defaults;
2282 static elf_linker_section_t zero_section;
2284 defaults = zero_section;
2285 defaults.which = which;
2286 defaults.hole_written_p = FALSE;
2287 defaults.alignment = 2;
2289 /* Both of these sections are (technically) created by the user
2290 putting data in them, so they shouldn't be marked
2291 SEC_LINKER_CREATED.
2293 The linker creates them so it has somewhere to attach their
2294 respective symbols. In fact, if they were empty it would
2295 be OK to leave the symbol set to 0 (or any random number), because
2296 the appropriate register should never be used. */
2297 defaults.flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
2298 | SEC_IN_MEMORY);
2300 switch (which)
2302 default:
2303 (*_bfd_error_handler) (_("%s: Unknown special linker type %d"),
2304 bfd_get_filename (abfd),
2305 (int) which);
2307 bfd_set_error (bfd_error_bad_value);
2308 return (elf_linker_section_t *) 0;
2310 case LINKER_SECTION_SDATA: /* .sdata/.sbss section */
2311 defaults.name = ".sdata";
2312 defaults.rel_name = ".rela.sdata";
2313 defaults.bss_name = ".sbss";
2314 defaults.sym_name = "_SDA_BASE_";
2315 defaults.sym_offset = 32768;
2316 break;
2318 case LINKER_SECTION_SDATA2: /* .sdata2/.sbss2 section */
2319 defaults.name = ".sdata2";
2320 defaults.rel_name = ".rela.sdata2";
2321 defaults.bss_name = ".sbss2";
2322 defaults.sym_name = "_SDA2_BASE_";
2323 defaults.sym_offset = 32768;
2324 defaults.flags |= SEC_READONLY;
2325 break;
2328 lsect = _bfd_elf_create_linker_section (abfd, info, which, &defaults);
2331 return lsect;
2334 /* If we have a non-zero sized .sbss2 or .PPC.EMB.sbss0 sections, we
2335 need to bump up the number of section headers. */
2337 static int
2338 ppc_elf_additional_program_headers (abfd)
2339 bfd *abfd;
2341 asection *s;
2342 int ret;
2344 ret = 0;
2346 s = bfd_get_section_by_name (abfd, ".interp");
2347 if (s != NULL)
2348 ++ret;
2350 s = bfd_get_section_by_name (abfd, ".sbss2");
2351 if (s != NULL && (s->flags & SEC_LOAD) != 0 && s->_raw_size > 0)
2352 ++ret;
2354 s = bfd_get_section_by_name (abfd, ".PPC.EMB.sbss0");
2355 if (s != NULL && (s->flags & SEC_LOAD) != 0 && s->_raw_size > 0)
2356 ++ret;
2358 return ret;
2361 /* Modify the segment map if needed. */
2363 static bfd_boolean
2364 ppc_elf_modify_segment_map (abfd)
2365 bfd *abfd ATTRIBUTE_UNUSED;
2367 return TRUE;
2370 /* The powerpc .got has a blrl instruction in it. Mark it executable. */
2372 static bfd_boolean
2373 ppc_elf_create_got (abfd, info)
2374 bfd *abfd;
2375 struct bfd_link_info *info;
2377 struct ppc_elf_link_hash_table *htab;
2378 asection *s;
2379 flagword flags;
2381 if (!_bfd_elf_create_got_section (abfd, info))
2382 return FALSE;
2384 htab = ppc_elf_hash_table (info);
2385 htab->got = s = bfd_get_section_by_name (abfd, ".got");
2386 if (s == NULL)
2387 abort ();
2389 flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_HAS_CONTENTS | SEC_IN_MEMORY
2390 | SEC_LINKER_CREATED);
2391 if (!bfd_set_section_flags (abfd, s, flags))
2392 return FALSE;
2394 htab->relgot = bfd_make_section (abfd, ".rela.got");
2395 if (!htab->relgot
2396 || ! bfd_set_section_flags (abfd, htab->relgot,
2397 (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
2398 | SEC_IN_MEMORY | SEC_LINKER_CREATED
2399 | SEC_READONLY))
2400 || ! bfd_set_section_alignment (abfd, htab->relgot, 2))
2401 return FALSE;
2403 return TRUE;
2406 /* We have to create .dynsbss and .rela.sbss here so that they get mapped
2407 to output sections (just like _bfd_elf_create_dynamic_sections has
2408 to create .dynbss and .rela.bss). */
2410 static bfd_boolean
2411 ppc_elf_create_dynamic_sections (abfd, info)
2412 bfd *abfd;
2413 struct bfd_link_info *info;
2415 struct ppc_elf_link_hash_table *htab;
2416 asection *s;
2417 flagword flags;
2419 if (!ppc_elf_create_got (abfd, info))
2420 return FALSE;
2422 if (!_bfd_elf_create_dynamic_sections (abfd, info))
2423 return FALSE;
2425 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
2426 | SEC_LINKER_CREATED);
2428 htab = ppc_elf_hash_table (info);
2429 htab->dynbss = bfd_get_section_by_name (abfd, ".dynbss");
2430 htab->dynsbss = s = bfd_make_section (abfd, ".dynsbss");
2431 if (s == NULL
2432 || ! bfd_set_section_flags (abfd, s, SEC_ALLOC))
2433 return FALSE;
2435 if (! info->shared)
2437 htab->relbss = bfd_get_section_by_name (abfd, ".rela.bss");
2438 htab->relsbss = s = bfd_make_section (abfd, ".rela.sbss");
2439 if (s == NULL
2440 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
2441 || ! bfd_set_section_alignment (abfd, s, 2))
2442 return FALSE;
2445 htab->relplt = bfd_get_section_by_name (abfd, ".rela.plt");
2446 htab->plt = s = bfd_get_section_by_name (abfd, ".plt");
2447 if (s == NULL)
2448 abort ();
2450 flags = SEC_ALLOC | SEC_CODE | SEC_IN_MEMORY | SEC_LINKER_CREATED;
2451 return bfd_set_section_flags (abfd, s, flags);
2454 /* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
2455 copying dynamic variables from a shared lib into an app's dynbss
2456 section, and instead use a dynamic relocation to point into the
2457 shared lib. */
2458 #define ELIMINATE_COPY_RELOCS 1
2460 /* Adjust a symbol defined by a dynamic object and referenced by a
2461 regular object. The current definition is in some section of the
2462 dynamic object, but we're not including those sections. We have to
2463 change the definition to something the rest of the link can
2464 understand. */
2466 static bfd_boolean
2467 ppc_elf_adjust_dynamic_symbol (info, h)
2468 struct bfd_link_info *info;
2469 struct elf_link_hash_entry *h;
2471 struct ppc_elf_link_hash_table *htab;
2472 asection *s;
2473 unsigned int power_of_two;
2475 #ifdef DEBUG
2476 fprintf (stderr, "ppc_elf_adjust_dynamic_symbol called for %s\n",
2477 h->root.root.string);
2478 #endif
2480 /* Make sure we know what is going on here. */
2481 htab = ppc_elf_hash_table (info);
2482 BFD_ASSERT (htab->elf.dynobj != NULL
2483 && ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT)
2484 || h->weakdef != NULL
2485 || ((h->elf_link_hash_flags
2486 & ELF_LINK_HASH_DEF_DYNAMIC) != 0
2487 && (h->elf_link_hash_flags
2488 & ELF_LINK_HASH_REF_REGULAR) != 0
2489 && (h->elf_link_hash_flags
2490 & ELF_LINK_HASH_DEF_REGULAR) == 0)));
2492 /* Deal with function syms. */
2493 if (h->type == STT_FUNC
2494 || (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0)
2496 /* Clear procedure linkage table information for any symbol that
2497 won't need a .plt entry. */
2498 if (! htab->elf.dynamic_sections_created
2499 || SYMBOL_CALLS_LOCAL (info, h)
2500 || h->plt.refcount <= 0)
2502 /* A PLT entry is not required/allowed when:
2504 1. We are not using ld.so; because then the PLT entry
2505 can't be set up, so we can't use one.
2507 2. We know for certain that a call to this symbol
2508 will go to this object.
2510 3. GC has rendered the entry unused. */
2511 h->plt.offset = (bfd_vma) -1;
2512 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
2514 return TRUE;
2516 else
2517 h->plt.offset = (bfd_vma) -1;
2519 /* If this is a weak symbol, and there is a real definition, the
2520 processor independent code will have arranged for us to see the
2521 real definition first, and we can just use the same value. */
2522 if (h->weakdef != NULL)
2524 BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined
2525 || h->weakdef->root.type == bfd_link_hash_defweak);
2526 h->root.u.def.section = h->weakdef->root.u.def.section;
2527 h->root.u.def.value = h->weakdef->root.u.def.value;
2528 return TRUE;
2531 /* This is a reference to a symbol defined by a dynamic object which
2532 is not a function. */
2534 /* If we are creating a shared library, we must presume that the
2535 only references to the symbol are via the global offset table.
2536 For such cases we need not do anything here; the relocations will
2537 be handled correctly by relocate_section. */
2538 if (info->shared)
2539 return TRUE;
2541 /* If there are no references to this symbol that do not use the
2542 GOT, we don't need to generate a copy reloc. */
2543 if ((h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0)
2544 return TRUE;
2546 if (ELIMINATE_COPY_RELOCS)
2548 struct ppc_elf_dyn_relocs *p;
2549 for (p = ppc_elf_hash_entry (h)->dyn_relocs; p != NULL; p = p->next)
2551 s = p->sec->output_section;
2552 if (s != NULL && (s->flags & SEC_READONLY) != 0)
2553 break;
2556 /* If we didn't find any dynamic relocs in read-only sections, then
2557 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
2558 if (p == NULL)
2560 h->elf_link_hash_flags &= ~ELF_LINK_NON_GOT_REF;
2561 return TRUE;
2565 /* We must allocate the symbol in our .dynbss section, which will
2566 become part of the .bss section of the executable. There will be
2567 an entry for this symbol in the .dynsym section. The dynamic
2568 object will contain position independent code, so all references
2569 from the dynamic object to this symbol will go through the global
2570 offset table. The dynamic linker will use the .dynsym entry to
2571 determine the address it must put in the global offset table, so
2572 both the dynamic object and the regular object will refer to the
2573 same memory location for the variable.
2575 Of course, if the symbol is sufficiently small, we must instead
2576 allocate it in .sbss. FIXME: It would be better to do this if and
2577 only if there were actually SDAREL relocs for that symbol. */
2579 if (h->size <= elf_gp_size (htab->elf.dynobj))
2580 s = htab->dynsbss;
2581 else
2582 s = htab->dynbss;
2583 BFD_ASSERT (s != NULL);
2585 /* We must generate a R_PPC_COPY reloc to tell the dynamic linker to
2586 copy the initial value out of the dynamic object and into the
2587 runtime process image. We need to remember the offset into the
2588 .rela.bss section we are going to use. */
2589 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
2591 asection *srel;
2593 if (h->size <= elf_gp_size (htab->elf.dynobj))
2594 srel = htab->relsbss;
2595 else
2596 srel = htab->relbss;
2597 BFD_ASSERT (srel != NULL);
2598 srel->_raw_size += sizeof (Elf32_External_Rela);
2599 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_COPY;
2602 /* We need to figure out the alignment required for this symbol. I
2603 have no idea how ELF linkers handle this. */
2604 power_of_two = bfd_log2 (h->size);
2605 if (power_of_two > 4)
2606 power_of_two = 4;
2608 /* Apply the required alignment. */
2609 s->_raw_size = BFD_ALIGN (s->_raw_size,
2610 (bfd_size_type) (1 << power_of_two));
2611 if (power_of_two > bfd_get_section_alignment (htab->elf.dynobj, s))
2613 if (! bfd_set_section_alignment (htab->elf.dynobj, s, power_of_two))
2614 return FALSE;
2617 /* Define the symbol as being at this point in the section. */
2618 h->root.u.def.section = s;
2619 h->root.u.def.value = s->_raw_size;
2621 /* Increment the section size to make room for the symbol. */
2622 s->_raw_size += h->size;
2624 return TRUE;
2627 /* This is the condition under which finish_dynamic_symbol will be
2628 called from elflink.h. If elflink.h doesn't call our
2629 finish_dynamic_symbol routine, we'll need to do something about
2630 initializing any .plt and .got entries in relocate_section. */
2631 #define WILL_CALL_FINISH_DYNAMIC_SYMBOL(DYN, SHARED, H) \
2632 ((DYN) \
2633 && ((SHARED) \
2634 || ((H)->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0) \
2635 && ((H)->dynindx != -1 \
2636 || ((H)->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0))
2638 /* Of those relocs that might be copied as dynamic relocs, this macro
2639 selects those that must be copied when linking a shared library,
2640 even when the symbol is local. */
2642 #define MUST_BE_DYN_RELOC(RTYPE) \
2643 ((RTYPE) != R_PPC_REL24 \
2644 && (RTYPE) != R_PPC_REL14 \
2645 && (RTYPE) != R_PPC_REL14_BRTAKEN \
2646 && (RTYPE) != R_PPC_REL14_BRNTAKEN \
2647 && (RTYPE) != R_PPC_REL32)
2649 /* Allocate space in associated reloc sections for dynamic relocs. */
2651 static bfd_boolean
2652 allocate_dynrelocs (h, inf)
2653 struct elf_link_hash_entry *h;
2654 PTR inf;
2656 struct bfd_link_info *info = (struct bfd_link_info *) inf;
2657 struct ppc_elf_link_hash_entry *eh;
2658 struct ppc_elf_link_hash_table *htab;
2659 struct ppc_elf_dyn_relocs *p;
2661 if (h->root.type == bfd_link_hash_indirect)
2662 return TRUE;
2664 if (h->root.type == bfd_link_hash_warning)
2665 /* When warning symbols are created, they **replace** the "real"
2666 entry in the hash table, thus we never get to see the real
2667 symbol in a hash traversal. So look at it now. */
2668 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2670 htab = ppc_elf_hash_table (info);
2671 if (htab->elf.dynamic_sections_created
2672 && h->plt.refcount > 0)
2674 /* Make sure this symbol is output as a dynamic symbol. */
2675 if (h->dynindx == -1
2676 && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
2678 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
2679 return FALSE;
2682 if (info->shared
2683 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h))
2685 asection *s = htab->plt;
2687 /* If this is the first .plt entry, make room for the special
2688 first entry. */
2689 if (s->_raw_size == 0)
2690 s->_raw_size += PLT_INITIAL_ENTRY_SIZE;
2692 /* The PowerPC PLT is actually composed of two parts, the
2693 first part is 2 words (for a load and a jump), and then
2694 there is a remaining word available at the end. */
2695 h->plt.offset = (PLT_INITIAL_ENTRY_SIZE
2696 + (PLT_SLOT_SIZE
2697 * ((s->_raw_size - PLT_INITIAL_ENTRY_SIZE)
2698 / PLT_ENTRY_SIZE)));
2700 /* If this symbol is not defined in a regular file, and we
2701 are not generating a shared library, then set the symbol
2702 to this location in the .plt. This is required to make
2703 function pointers compare as equal between the normal
2704 executable and the shared library. */
2705 if (! info->shared
2706 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
2708 h->root.u.def.section = s;
2709 h->root.u.def.value = h->plt.offset;
2712 /* Make room for this entry. After the 8192nd entry, room
2713 for two entries is allocated. */
2714 s->_raw_size += PLT_ENTRY_SIZE;
2715 if ((s->_raw_size - PLT_INITIAL_ENTRY_SIZE) / PLT_ENTRY_SIZE
2716 >= PLT_NUM_SINGLE_ENTRIES)
2717 s->_raw_size += PLT_ENTRY_SIZE;
2719 /* We also need to make an entry in the .rela.plt section. */
2720 htab->relplt->_raw_size += sizeof (Elf32_External_Rela);
2722 else
2724 h->plt.offset = (bfd_vma) -1;
2725 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
2728 else
2730 h->plt.offset = (bfd_vma) -1;
2731 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
2734 eh = (struct ppc_elf_link_hash_entry *) h;
2735 if (eh->elf.got.refcount > 0)
2737 /* Make sure this symbol is output as a dynamic symbol. */
2738 if (eh->elf.dynindx == -1
2739 && (eh->elf.elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
2741 if (!bfd_elf32_link_record_dynamic_symbol (info, &eh->elf))
2742 return FALSE;
2745 if (eh->tls_mask == (TLS_TLS | TLS_LD)
2746 && !(eh->elf.elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC))
2747 /* If just an LD reloc, we'll just use htab->tlsld_got.offset. */
2748 eh->elf.got.offset = (bfd_vma) -1;
2749 else
2751 bfd_boolean dyn;
2752 eh->elf.got.offset = htab->got->_raw_size;
2753 if ((eh->tls_mask & TLS_TLS) != 0)
2755 if ((eh->tls_mask & TLS_LD) != 0)
2756 htab->got->_raw_size += 8;
2757 if ((eh->tls_mask & TLS_GD) != 0)
2758 htab->got->_raw_size += 8;
2759 if ((eh->tls_mask & (TLS_TPREL | TLS_TPRELGD)) != 0)
2760 htab->got->_raw_size += 4;
2761 if ((eh->tls_mask & TLS_DTPREL) != 0)
2762 htab->got->_raw_size += 4;
2764 else
2765 htab->got->_raw_size += 4;
2766 dyn = htab->elf.dynamic_sections_created;
2767 if (info->shared
2768 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, &eh->elf))
2770 /* All the entries we allocated need relocs. */
2771 htab->relgot->_raw_size
2772 += ((htab->got->_raw_size - eh->elf.got.offset) / 4
2773 * sizeof (Elf32_External_Rela));
2774 /* Except LD only needs one. */
2775 if ((eh->tls_mask & TLS_LD) != 0)
2776 htab->relgot->_raw_size -= sizeof (Elf32_External_Rela);
2780 else
2781 eh->elf.got.offset = (bfd_vma) -1;
2783 if (eh->dyn_relocs == NULL)
2784 return TRUE;
2786 /* In the shared -Bsymbolic case, discard space allocated for
2787 dynamic pc-relative relocs against symbols which turn out to be
2788 defined in regular objects. For the normal shared case, discard
2789 space for relocs that have become local due to symbol visibility
2790 changes. */
2791 if (info->shared)
2793 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0
2794 && ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0
2795 || info->symbolic))
2797 struct ppc_elf_dyn_relocs **pp;
2799 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
2801 p->count -= p->pc_count;
2802 p->pc_count = 0;
2803 if (p->count == 0)
2804 *pp = p->next;
2805 else
2806 pp = &p->next;
2810 else if (ELIMINATE_COPY_RELOCS)
2812 /* For the non-shared case, discard space for relocs against
2813 symbols which turn out to need copy relocs or are not
2814 dynamic. */
2816 if ((h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0
2817 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
2818 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
2820 /* Make sure this symbol is output as a dynamic symbol.
2821 Undefined weak syms won't yet be marked as dynamic. */
2822 if (h->dynindx == -1
2823 && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
2825 if (! bfd_elf64_link_record_dynamic_symbol (info, h))
2826 return FALSE;
2829 /* If that succeeded, we know we'll be keeping all the
2830 relocs. */
2831 if (h->dynindx != -1)
2832 goto keep;
2835 eh->dyn_relocs = NULL;
2837 keep: ;
2840 /* Finally, allocate space. */
2841 for (p = eh->dyn_relocs; p != NULL; p = p->next)
2843 asection *sreloc = elf_section_data (p->sec)->sreloc;
2844 sreloc->_raw_size += p->count * sizeof (Elf32_External_Rela);
2847 return TRUE;
2850 /* Find any dynamic relocs that apply to read-only sections. */
2852 static bfd_boolean
2853 readonly_dynrelocs (h, info)
2854 struct elf_link_hash_entry *h;
2855 PTR info;
2857 struct ppc_elf_dyn_relocs *p;
2859 if (h->root.type == bfd_link_hash_indirect)
2860 return TRUE;
2862 if (h->root.type == bfd_link_hash_warning)
2863 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2865 for (p = ppc_elf_hash_entry (h)->dyn_relocs; p != NULL; p = p->next)
2867 asection *s = p->sec->output_section;
2869 if (s != NULL
2870 && ((s->flags & (SEC_READONLY | SEC_ALLOC))
2871 == (SEC_READONLY | SEC_ALLOC)))
2873 ((struct bfd_link_info *) info)->flags |= DF_TEXTREL;
2875 /* Not an error, just cut short the traversal. */
2876 return FALSE;
2879 return TRUE;
2882 /* Set the sizes of the dynamic sections. */
2884 static bfd_boolean
2885 ppc_elf_size_dynamic_sections (output_bfd, info)
2886 bfd *output_bfd ATTRIBUTE_UNUSED;
2887 struct bfd_link_info *info;
2889 struct ppc_elf_link_hash_table *htab;
2890 asection *s;
2891 bfd_boolean relocs;
2892 bfd *ibfd;
2894 #ifdef DEBUG
2895 fprintf (stderr, "ppc_elf_size_dynamic_sections called\n");
2896 #endif
2898 htab = ppc_elf_hash_table (info);
2899 BFD_ASSERT (htab->elf.dynobj != NULL);
2901 if (elf_hash_table (info)->dynamic_sections_created)
2903 /* Set the contents of the .interp section to the interpreter. */
2904 if (! info->shared)
2906 s = bfd_get_section_by_name (htab->elf.dynobj, ".interp");
2907 BFD_ASSERT (s != NULL);
2908 s->_raw_size = sizeof ELF_DYNAMIC_INTERPRETER;
2909 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
2913 if (htab->tlsld_got.refcount > 0)
2915 htab->tlsld_got.offset = htab->got->_raw_size;
2916 htab->got->_raw_size += 8;
2917 if (info->shared)
2918 htab->relgot->_raw_size += sizeof (Elf32_External_Rela);
2920 else
2921 htab->tlsld_got.offset = (bfd_vma) -1;
2923 /* Set up .got offsets for local syms, and space for local dynamic
2924 relocs. */
2925 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
2927 bfd_signed_vma *local_got;
2928 bfd_signed_vma *end_local_got;
2929 char *lgot_masks;
2930 bfd_size_type locsymcount;
2931 Elf_Internal_Shdr *symtab_hdr;
2932 asection *srel;
2934 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
2935 continue;
2937 for (s = ibfd->sections; s != NULL; s = s->next)
2939 struct ppc_elf_dyn_relocs *p;
2941 for (p = ((struct ppc_elf_dyn_relocs *)
2942 elf_section_data (s)->local_dynrel);
2943 p != NULL;
2944 p = p->next)
2946 if (!bfd_is_abs_section (p->sec)
2947 && bfd_is_abs_section (p->sec->output_section))
2949 /* Input section has been discarded, either because
2950 it is a copy of a linkonce section or due to
2951 linker script /DISCARD/, so we'll be discarding
2952 the relocs too. */
2954 else if (p->count != 0)
2956 elf_section_data (p->sec)->sreloc->_raw_size
2957 += p->count * sizeof (Elf32_External_Rela);
2958 if ((p->sec->output_section->flags
2959 & (SEC_READONLY | SEC_ALLOC))
2960 == (SEC_READONLY | SEC_ALLOC))
2961 info->flags |= DF_TEXTREL;
2966 local_got = elf_local_got_refcounts (ibfd);
2967 if (!local_got)
2968 continue;
2970 symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
2971 locsymcount = symtab_hdr->sh_info;
2972 end_local_got = local_got + locsymcount;
2973 lgot_masks = (char *) end_local_got;
2974 s = htab->got;
2975 srel = htab->relgot;
2976 for (; local_got < end_local_got; ++local_got, ++lgot_masks)
2977 if (*local_got > 0)
2979 if (*lgot_masks == (TLS_TLS | TLS_LD))
2981 /* If just an LD reloc, we'll just use
2982 htab->tlsld_got.offset. */
2983 if (htab->tlsld_got.offset == (bfd_vma) -1)
2985 htab->tlsld_got.offset = s->_raw_size;
2986 s->_raw_size += 8;
2987 if (info->shared)
2988 srel->_raw_size += sizeof (Elf32_External_Rela);
2990 *local_got = (bfd_vma) -1;
2992 else
2994 *local_got = s->_raw_size;
2995 if ((*lgot_masks & TLS_TLS) != 0)
2997 if ((*lgot_masks & TLS_GD) != 0)
2998 s->_raw_size += 8;
2999 if ((*lgot_masks & (TLS_TPREL | TLS_TPRELGD)) != 0)
3000 s->_raw_size += 4;
3001 if ((*lgot_masks & TLS_DTPREL) != 0)
3002 s->_raw_size += 4;
3004 else
3005 s->_raw_size += 4;
3006 if (info->shared)
3007 srel->_raw_size += ((s->_raw_size - *local_got) / 4
3008 * sizeof (Elf32_External_Rela));
3011 else
3012 *local_got = (bfd_vma) -1;
3015 /* Allocate space for global sym dynamic relocs. */
3016 elf_link_hash_traverse (elf_hash_table (info), allocate_dynrelocs, info);
3018 /* We've now determined the sizes of the various dynamic sections.
3019 Allocate memory for them. */
3020 relocs = FALSE;
3021 for (s = htab->elf.dynobj->sections; s != NULL; s = s->next)
3023 if ((s->flags & SEC_LINKER_CREATED) == 0)
3024 continue;
3026 if (s == htab->plt
3027 || s == htab->got
3028 || (htab->sdata != NULL && s == htab->sdata->section)
3029 || (htab->sdata2 != NULL && s == htab->sdata2->section))
3031 /* Strip this section if we don't need it; see the
3032 comment below. */
3034 else if (strncmp (bfd_get_section_name (dynobj, s), ".rela", 5) == 0)
3036 if (s->_raw_size == 0)
3038 /* If we don't need this section, strip it from the
3039 output file. This is mostly to handle .rela.bss and
3040 .rela.plt. We must create both sections in
3041 create_dynamic_sections, because they must be created
3042 before the linker maps input sections to output
3043 sections. The linker does that before
3044 adjust_dynamic_symbol is called, and it is that
3045 function which decides whether anything needs to go
3046 into these sections. */
3048 else
3050 /* Remember whether there are any relocation sections. */
3051 relocs = TRUE;
3053 /* We use the reloc_count field as a counter if we need
3054 to copy relocs into the output file. */
3055 s->reloc_count = 0;
3058 else
3060 /* It's not one of our sections, so don't allocate space. */
3061 continue;
3064 if (s->_raw_size == 0)
3066 _bfd_strip_section_from_output (info, s);
3067 continue;
3070 /* Allocate memory for the section contents. */
3071 s->contents = (bfd_byte *) bfd_zalloc (htab->elf.dynobj, s->_raw_size);
3072 if (s->contents == NULL)
3073 return FALSE;
3076 if (htab->elf.dynamic_sections_created)
3078 /* Add some entries to the .dynamic section. We fill in the
3079 values later, in ppc_elf_finish_dynamic_sections, but we
3080 must add the entries now so that we get the correct size for
3081 the .dynamic section. The DT_DEBUG entry is filled in by the
3082 dynamic linker and used by the debugger. */
3083 #define add_dynamic_entry(TAG, VAL) \
3084 bfd_elf32_add_dynamic_entry (info, (bfd_vma) (TAG), (bfd_vma) (VAL))
3086 if (!info->shared)
3088 if (!add_dynamic_entry (DT_DEBUG, 0))
3089 return FALSE;
3092 if (htab->plt != NULL && htab->plt->_raw_size != 0)
3094 if (!add_dynamic_entry (DT_PLTGOT, 0)
3095 || !add_dynamic_entry (DT_PLTRELSZ, 0)
3096 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
3097 || !add_dynamic_entry (DT_JMPREL, 0))
3098 return FALSE;
3101 if (relocs)
3103 if (!add_dynamic_entry (DT_RELA, 0)
3104 || !add_dynamic_entry (DT_RELASZ, 0)
3105 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf32_External_Rela)))
3106 return FALSE;
3109 /* If any dynamic relocs apply to a read-only section, then we
3110 need a DT_TEXTREL entry. */
3111 if ((info->flags & DF_TEXTREL) == 0)
3112 elf_link_hash_traverse (elf_hash_table (info), readonly_dynrelocs,
3113 (PTR) info);
3115 if ((info->flags & DF_TEXTREL) != 0)
3117 if (!add_dynamic_entry (DT_TEXTREL, 0))
3118 return FALSE;
3121 #undef add_dynamic_entry
3123 return TRUE;
3126 static bfd_boolean
3127 update_local_sym_info (abfd, symtab_hdr, r_symndx, tls_type)
3128 bfd *abfd;
3129 Elf_Internal_Shdr *symtab_hdr;
3130 unsigned long r_symndx;
3131 int tls_type;
3133 bfd_signed_vma *local_got_refcounts = elf_local_got_refcounts (abfd);
3134 char *local_got_tls_masks;
3136 if (local_got_refcounts == NULL)
3138 bfd_size_type size = symtab_hdr->sh_info;
3140 size *= sizeof (*local_got_refcounts) + sizeof (*local_got_tls_masks);
3141 local_got_refcounts = (bfd_signed_vma *) bfd_zalloc (abfd, size);
3142 if (local_got_refcounts == NULL)
3143 return FALSE;
3144 elf_local_got_refcounts (abfd) = local_got_refcounts;
3147 local_got_refcounts[r_symndx] += 1;
3148 local_got_tls_masks = (char *) (local_got_refcounts + symtab_hdr->sh_info);
3149 local_got_tls_masks[r_symndx] |= tls_type;
3150 return TRUE;
3153 static void
3154 bad_shared_reloc (abfd, r_type)
3155 bfd *abfd;
3156 enum elf_ppc_reloc_type r_type;
3158 (*_bfd_error_handler)
3159 (_("%s: relocation %s cannot be used when making a shared object"),
3160 bfd_archive_filename (abfd),
3161 ppc_elf_howto_table[(int) r_type]->name);
3162 bfd_set_error (bfd_error_bad_value);
3165 /* Look through the relocs for a section during the first phase, and
3166 allocate space in the global offset table or procedure linkage
3167 table. */
3169 static bfd_boolean
3170 ppc_elf_check_relocs (abfd, info, sec, relocs)
3171 bfd *abfd;
3172 struct bfd_link_info *info;
3173 asection *sec;
3174 const Elf_Internal_Rela *relocs;
3176 struct ppc_elf_link_hash_table *htab;
3177 Elf_Internal_Shdr *symtab_hdr;
3178 struct elf_link_hash_entry **sym_hashes;
3179 const Elf_Internal_Rela *rel;
3180 const Elf_Internal_Rela *rel_end;
3181 asection *sreloc;
3183 if (info->relocateable)
3184 return TRUE;
3186 #ifdef DEBUG
3187 fprintf (stderr, "ppc_elf_check_relocs called for section %s in %s\n",
3188 bfd_get_section_name (abfd, sec),
3189 bfd_archive_filename (abfd));
3190 #endif
3192 /* Create the linker generated sections all the time so that the
3193 special symbols are created. */
3195 htab = ppc_elf_hash_table (info);
3196 if (htab->sdata == NULL)
3198 htab->sdata = elf_linker_section (abfd, LINKER_SECTION_SDATA);
3199 if (htab->sdata == NULL)
3200 htab->sdata = ppc_elf_create_linker_section (abfd, info,
3201 LINKER_SECTION_SDATA);
3202 if (htab->sdata == NULL)
3203 return FALSE;
3206 if (htab->sdata2 == NULL)
3208 htab->sdata2 = elf_linker_section (abfd, LINKER_SECTION_SDATA2);
3209 if (htab->sdata2 == NULL)
3210 htab->sdata2 = ppc_elf_create_linker_section (abfd, info,
3211 LINKER_SECTION_SDATA2);
3212 if (htab->sdata2 == NULL)
3213 return FALSE;
3216 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
3217 sym_hashes = elf_sym_hashes (abfd);
3218 sreloc = NULL;
3220 rel_end = relocs + sec->reloc_count;
3221 for (rel = relocs; rel < rel_end; rel++)
3223 unsigned long r_symndx;
3224 enum elf_ppc_reloc_type r_type;
3225 struct elf_link_hash_entry *h;
3226 int tls_type = 0;
3228 r_symndx = ELF32_R_SYM (rel->r_info);
3229 if (r_symndx < symtab_hdr->sh_info)
3230 h = NULL;
3231 else
3232 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
3234 /* If a relocation refers to _GLOBAL_OFFSET_TABLE_, create the .got.
3235 This shows up in particular in an R_PPC_ADDR32 in the eabi
3236 startup code. */
3237 if (h && strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
3239 if (htab->got == NULL)
3241 if (htab->elf.dynobj == NULL)
3242 htab->elf.dynobj = abfd;
3243 if (!ppc_elf_create_got (htab->elf.dynobj, info))
3244 return FALSE;
3248 r_type = (enum elf_ppc_reloc_type) ELF32_R_TYPE (rel->r_info);
3249 switch (r_type)
3251 case R_PPC_GOT_TLSLD16:
3252 case R_PPC_GOT_TLSLD16_LO:
3253 case R_PPC_GOT_TLSLD16_HI:
3254 case R_PPC_GOT_TLSLD16_HA:
3255 htab->tlsld_got.refcount += 1;
3256 tls_type = TLS_TLS | TLS_LD;
3257 goto dogottls;
3259 case R_PPC_GOT_TLSGD16:
3260 case R_PPC_GOT_TLSGD16_LO:
3261 case R_PPC_GOT_TLSGD16_HI:
3262 case R_PPC_GOT_TLSGD16_HA:
3263 tls_type = TLS_TLS | TLS_GD;
3264 goto dogottls;
3266 case R_PPC_GOT_TPREL16:
3267 case R_PPC_GOT_TPREL16_LO:
3268 case R_PPC_GOT_TPREL16_HI:
3269 case R_PPC_GOT_TPREL16_HA:
3270 if (info->shared)
3271 info->flags |= DF_STATIC_TLS;
3272 tls_type = TLS_TLS | TLS_TPREL;
3273 goto dogottls;
3275 case R_PPC_GOT_DTPREL16:
3276 case R_PPC_GOT_DTPREL16_LO:
3277 case R_PPC_GOT_DTPREL16_HI:
3278 case R_PPC_GOT_DTPREL16_HA:
3279 tls_type = TLS_TLS | TLS_DTPREL;
3280 dogottls:
3281 sec->has_tls_reloc = 1;
3282 /* Fall thru */
3284 /* GOT16 relocations */
3285 case R_PPC_GOT16:
3286 case R_PPC_GOT16_LO:
3287 case R_PPC_GOT16_HI:
3288 case R_PPC_GOT16_HA:
3289 /* This symbol requires a global offset table entry. */
3290 if (htab->got == NULL)
3292 if (htab->elf.dynobj == NULL)
3293 htab->elf.dynobj = abfd;
3294 if (!ppc_elf_create_got (htab->elf.dynobj, info))
3295 return FALSE;
3297 if (h != NULL)
3299 h->got.refcount += 1;
3300 ppc_elf_hash_entry (h)->tls_mask |= tls_type;
3302 else
3303 /* This is a global offset table entry for a local symbol. */
3304 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx, tls_type))
3305 return FALSE;
3306 break;
3308 /* Indirect .sdata relocation. */
3309 case R_PPC_EMB_SDAI16:
3310 if (info->shared)
3312 bad_shared_reloc (abfd, r_type);
3313 return FALSE;
3315 if (!bfd_elf32_create_pointer_linker_section (abfd, info,
3316 htab->sdata, h, rel))
3317 return FALSE;
3318 break;
3320 /* Indirect .sdata2 relocation. */
3321 case R_PPC_EMB_SDA2I16:
3322 if (info->shared)
3324 bad_shared_reloc (abfd, r_type);
3325 return FALSE;
3327 if (!bfd_elf32_create_pointer_linker_section (abfd, info,
3328 htab->sdata2, h, rel))
3329 return FALSE;
3330 break;
3332 case R_PPC_SDAREL16:
3333 case R_PPC_EMB_SDA2REL:
3334 case R_PPC_EMB_SDA21:
3335 case R_PPC_EMB_RELSDA:
3336 case R_PPC_EMB_NADDR32:
3337 case R_PPC_EMB_NADDR16:
3338 case R_PPC_EMB_NADDR16_LO:
3339 case R_PPC_EMB_NADDR16_HI:
3340 case R_PPC_EMB_NADDR16_HA:
3341 if (info->shared)
3343 bad_shared_reloc (abfd, r_type);
3344 return FALSE;
3346 break;
3348 case R_PPC_PLT32:
3349 case R_PPC_PLTREL24:
3350 case R_PPC_PLTREL32:
3351 case R_PPC_PLT16_LO:
3352 case R_PPC_PLT16_HI:
3353 case R_PPC_PLT16_HA:
3354 #ifdef DEBUG
3355 fprintf (stderr, "Reloc requires a PLT entry\n");
3356 #endif
3357 /* This symbol requires a procedure linkage table entry. We
3358 actually build the entry in finish_dynamic_symbol,
3359 because this might be a case of linking PIC code without
3360 linking in any dynamic objects, in which case we don't
3361 need to generate a procedure linkage table after all. */
3363 if (h == NULL)
3365 /* It does not make sense to have a procedure linkage
3366 table entry for a local symbol. */
3367 bfd_set_error (bfd_error_bad_value);
3368 return FALSE;
3371 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
3372 h->plt.refcount++;
3373 break;
3375 /* The following relocations don't need to propagate the
3376 relocation if linking a shared object since they are
3377 section relative. */
3378 case R_PPC_SECTOFF:
3379 case R_PPC_SECTOFF_LO:
3380 case R_PPC_SECTOFF_HI:
3381 case R_PPC_SECTOFF_HA:
3382 case R_PPC_DTPREL16:
3383 case R_PPC_DTPREL16_LO:
3384 case R_PPC_DTPREL16_HI:
3385 case R_PPC_DTPREL16_HA:
3386 case R_PPC_TOC16:
3387 break;
3389 /* This are just markers. */
3390 case R_PPC_TLS:
3391 case R_PPC_EMB_MRKREF:
3392 case R_PPC_NONE:
3393 case R_PPC_max:
3394 break;
3396 /* These should only appear in dynamic objects. */
3397 case R_PPC_COPY:
3398 case R_PPC_GLOB_DAT:
3399 case R_PPC_JMP_SLOT:
3400 case R_PPC_RELATIVE:
3401 break;
3403 /* These aren't handled yet. We'll report an error later. */
3404 case R_PPC_ADDR30:
3405 case R_PPC_EMB_RELSEC16:
3406 case R_PPC_EMB_RELST_LO:
3407 case R_PPC_EMB_RELST_HI:
3408 case R_PPC_EMB_RELST_HA:
3409 case R_PPC_EMB_BIT_FLD:
3410 break;
3412 /* This refers only to functions defined in the shared library. */
3413 case R_PPC_LOCAL24PC:
3414 break;
3416 /* This relocation describes the C++ object vtable hierarchy.
3417 Reconstruct it for later use during GC. */
3418 case R_PPC_GNU_VTINHERIT:
3419 if (!_bfd_elf32_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
3420 return FALSE;
3421 break;
3423 /* This relocation describes which C++ vtable entries are actually
3424 used. Record for later use during GC. */
3425 case R_PPC_GNU_VTENTRY:
3426 if (!_bfd_elf32_gc_record_vtentry (abfd, sec, h, rel->r_addend))
3427 return FALSE;
3428 break;
3430 /* We shouldn't really be seeing these. */
3431 case R_PPC_TPREL32:
3432 if (info->shared)
3433 info->flags |= DF_STATIC_TLS;
3434 goto dodyn;
3436 /* Nor these. */
3437 case R_PPC_DTPMOD32:
3438 case R_PPC_DTPREL32:
3439 goto dodyn;
3441 case R_PPC_TPREL16:
3442 case R_PPC_TPREL16_LO:
3443 case R_PPC_TPREL16_HI:
3444 case R_PPC_TPREL16_HA:
3445 if (info->shared)
3446 info->flags |= DF_STATIC_TLS;
3447 goto dodyn;
3449 /* When creating a shared object, we must copy these
3450 relocs into the output file. We create a reloc
3451 section in dynobj and make room for the reloc. */
3452 case R_PPC_REL24:
3453 case R_PPC_REL14:
3454 case R_PPC_REL14_BRTAKEN:
3455 case R_PPC_REL14_BRNTAKEN:
3456 case R_PPC_REL32:
3457 if (h == NULL
3458 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0
3459 || SYMBOL_REFERENCES_LOCAL (info, h))
3460 break;
3461 /* fall through */
3463 case R_PPC_ADDR32:
3464 case R_PPC_ADDR24:
3465 case R_PPC_ADDR16:
3466 case R_PPC_ADDR16_LO:
3467 case R_PPC_ADDR16_HI:
3468 case R_PPC_ADDR16_HA:
3469 case R_PPC_ADDR14:
3470 case R_PPC_ADDR14_BRTAKEN:
3471 case R_PPC_ADDR14_BRNTAKEN:
3472 case R_PPC_UADDR32:
3473 case R_PPC_UADDR16:
3474 if (h != NULL && !info->shared)
3476 /* We may need a plt entry if the symbol turns out to be
3477 a function defined in a dynamic object. */
3478 h->plt.refcount++;
3480 /* We may need a copy reloc too. */
3481 h->elf_link_hash_flags |= ELF_LINK_NON_GOT_REF;
3484 dodyn:
3485 /* If we are creating a shared library, and this is a reloc
3486 against a global symbol, or a non PC relative reloc
3487 against a local symbol, then we need to copy the reloc
3488 into the shared library. However, if we are linking with
3489 -Bsymbolic, we do not need to copy a reloc against a
3490 global symbol which is defined in an object we are
3491 including in the link (i.e., DEF_REGULAR is set). At
3492 this point we have not seen all the input files, so it is
3493 possible that DEF_REGULAR is not set now but will be set
3494 later (it is never cleared). In case of a weak definition,
3495 DEF_REGULAR may be cleared later by a strong definition in
3496 a shared library. We account for that possibility below by
3497 storing information in the dyn_relocs field of the hash
3498 table entry. A similar situation occurs when creating
3499 shared libraries and symbol visibility changes render the
3500 symbol local.
3502 If on the other hand, we are creating an executable, we
3503 may need to keep relocations for symbols satisfied by a
3504 dynamic library if we manage to avoid copy relocs for the
3505 symbol. */
3506 if ((info->shared
3507 && (MUST_BE_DYN_RELOC (r_type)
3508 || (h != NULL
3509 && (! info->symbolic
3510 || h->root.type == bfd_link_hash_defweak
3511 || (h->elf_link_hash_flags
3512 & ELF_LINK_HASH_DEF_REGULAR) == 0))))
3513 || (ELIMINATE_COPY_RELOCS
3514 && !info->shared
3515 && (sec->flags & SEC_ALLOC) != 0
3516 && h != NULL
3517 && (h->root.type == bfd_link_hash_defweak
3518 || (h->elf_link_hash_flags
3519 & ELF_LINK_HASH_DEF_REGULAR) == 0)))
3521 struct ppc_elf_dyn_relocs *p;
3522 struct ppc_elf_dyn_relocs **head;
3524 #ifdef DEBUG
3525 fprintf (stderr, "ppc_elf_check_relocs need to create relocation for %s\n",
3526 (h && h->root.root.string
3527 ? h->root.root.string : "<unknown>"));
3528 #endif
3529 if (sreloc == NULL)
3531 const char *name;
3533 name = (bfd_elf_string_from_elf_section
3534 (abfd,
3535 elf_elfheader (abfd)->e_shstrndx,
3536 elf_section_data (sec)->rel_hdr.sh_name));
3537 if (name == NULL)
3538 return FALSE;
3540 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
3541 && strcmp (bfd_get_section_name (abfd, sec),
3542 name + 5) == 0);
3544 sreloc = bfd_get_section_by_name (htab->elf.dynobj, name);
3545 if (sreloc == NULL)
3547 flagword flags;
3549 sreloc = bfd_make_section (htab->elf.dynobj, name);
3550 flags = (SEC_HAS_CONTENTS | SEC_READONLY
3551 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3552 if ((sec->flags & SEC_ALLOC) != 0)
3553 flags |= SEC_ALLOC | SEC_LOAD;
3554 if (sreloc == NULL
3555 || ! bfd_set_section_flags (htab->elf.dynobj,
3556 sreloc, flags)
3557 || ! bfd_set_section_alignment (htab->elf.dynobj,
3558 sreloc, 2))
3559 return FALSE;
3561 elf_section_data (sec)->sreloc = sreloc;
3564 /* If this is a global symbol, we count the number of
3565 relocations we need for this symbol. */
3566 if (h != NULL)
3568 head = &ppc_elf_hash_entry (h)->dyn_relocs;
3570 else
3572 /* Track dynamic relocs needed for local syms too.
3573 We really need local syms available to do this
3574 easily. Oh well. */
3576 asection *s;
3577 s = bfd_section_from_r_symndx (abfd, &htab->sym_sec,
3578 sec, r_symndx);
3579 if (s == NULL)
3580 return FALSE;
3582 head = ((struct ppc_elf_dyn_relocs **)
3583 &elf_section_data (s)->local_dynrel);
3586 p = *head;
3587 if (p == NULL || p->sec != sec)
3589 p = ((struct ppc_elf_dyn_relocs *)
3590 bfd_alloc (htab->elf.dynobj, sizeof *p));
3591 if (p == NULL)
3592 return FALSE;
3593 p->next = *head;
3594 *head = p;
3595 p->sec = sec;
3596 p->count = 0;
3597 p->pc_count = 0;
3600 p->count += 1;
3601 if (!MUST_BE_DYN_RELOC (r_type))
3602 p->pc_count += 1;
3605 break;
3609 return TRUE;
3612 /* Return the section that should be marked against GC for a given
3613 relocation. */
3615 static asection *
3616 ppc_elf_gc_mark_hook (sec, info, rel, h, sym)
3617 asection *sec;
3618 struct bfd_link_info *info ATTRIBUTE_UNUSED;
3619 Elf_Internal_Rela *rel;
3620 struct elf_link_hash_entry *h;
3621 Elf_Internal_Sym *sym;
3623 if (h != NULL)
3625 switch (ELF32_R_TYPE (rel->r_info))
3627 case R_PPC_GNU_VTINHERIT:
3628 case R_PPC_GNU_VTENTRY:
3629 break;
3631 default:
3632 switch (h->root.type)
3634 case bfd_link_hash_defined:
3635 case bfd_link_hash_defweak:
3636 return h->root.u.def.section;
3638 case bfd_link_hash_common:
3639 return h->root.u.c.p->section;
3641 default:
3642 break;
3646 else
3647 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
3649 return NULL;
3652 /* Update the got, plt and dynamic reloc reference counts for the
3653 section being removed. */
3655 static bfd_boolean
3656 ppc_elf_gc_sweep_hook (abfd, info, sec, relocs)
3657 bfd *abfd;
3658 struct bfd_link_info *info;
3659 asection *sec;
3660 const Elf_Internal_Rela *relocs;
3662 struct ppc_elf_link_hash_table *htab;
3663 Elf_Internal_Shdr *symtab_hdr;
3664 struct elf_link_hash_entry **sym_hashes;
3665 bfd_signed_vma *local_got_refcounts;
3666 const Elf_Internal_Rela *rel, *relend;
3668 elf_section_data (sec)->local_dynrel = NULL;
3670 htab = ppc_elf_hash_table (info);
3671 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
3672 sym_hashes = elf_sym_hashes (abfd);
3673 local_got_refcounts = elf_local_got_refcounts (abfd);
3675 relend = relocs + sec->reloc_count;
3676 for (rel = relocs; rel < relend; rel++)
3678 unsigned long r_symndx;
3679 enum elf_ppc_reloc_type r_type;
3680 struct elf_link_hash_entry *h = NULL;
3682 r_symndx = ELF32_R_SYM (rel->r_info);
3683 if (r_symndx >= symtab_hdr->sh_info)
3685 struct ppc_elf_dyn_relocs **pp, *p;
3686 struct ppc_elf_link_hash_entry *eh;
3688 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
3689 eh = (struct ppc_elf_link_hash_entry *) h;
3691 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
3692 if (p->sec == sec)
3694 /* Everything must go for SEC. */
3695 *pp = p->next;
3696 break;
3700 r_type = (enum elf_ppc_reloc_type) ELF32_R_TYPE (rel->r_info);
3701 switch (r_type)
3703 case R_PPC_GOT_TLSLD16:
3704 case R_PPC_GOT_TLSLD16_LO:
3705 case R_PPC_GOT_TLSLD16_HI:
3706 case R_PPC_GOT_TLSLD16_HA:
3707 htab->tlsld_got.refcount -= 1;
3708 /* Fall thru */
3710 case R_PPC_GOT_TLSGD16:
3711 case R_PPC_GOT_TLSGD16_LO:
3712 case R_PPC_GOT_TLSGD16_HI:
3713 case R_PPC_GOT_TLSGD16_HA:
3714 case R_PPC_GOT_TPREL16:
3715 case R_PPC_GOT_TPREL16_LO:
3716 case R_PPC_GOT_TPREL16_HI:
3717 case R_PPC_GOT_TPREL16_HA:
3718 case R_PPC_GOT_DTPREL16:
3719 case R_PPC_GOT_DTPREL16_LO:
3720 case R_PPC_GOT_DTPREL16_HI:
3721 case R_PPC_GOT_DTPREL16_HA:
3722 case R_PPC_GOT16:
3723 case R_PPC_GOT16_LO:
3724 case R_PPC_GOT16_HI:
3725 case R_PPC_GOT16_HA:
3726 if (h != NULL)
3728 if (h->got.refcount > 0)
3729 h->got.refcount--;
3731 else if (local_got_refcounts != NULL)
3733 if (local_got_refcounts[r_symndx] > 0)
3734 local_got_refcounts[r_symndx]--;
3736 break;
3738 case R_PPC_REL24:
3739 case R_PPC_REL14:
3740 case R_PPC_REL14_BRTAKEN:
3741 case R_PPC_REL14_BRNTAKEN:
3742 case R_PPC_REL32:
3743 if (h == NULL
3744 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0
3745 || SYMBOL_REFERENCES_LOCAL (info, h))
3746 break;
3747 /* Fall thru */
3749 case R_PPC_ADDR32:
3750 case R_PPC_ADDR24:
3751 case R_PPC_ADDR16:
3752 case R_PPC_ADDR16_LO:
3753 case R_PPC_ADDR16_HI:
3754 case R_PPC_ADDR16_HA:
3755 case R_PPC_ADDR14:
3756 case R_PPC_ADDR14_BRTAKEN:
3757 case R_PPC_ADDR14_BRNTAKEN:
3758 case R_PPC_UADDR32:
3759 case R_PPC_UADDR16:
3760 case R_PPC_PLT32:
3761 case R_PPC_PLTREL24:
3762 case R_PPC_PLT16_LO:
3763 case R_PPC_PLT16_HI:
3764 case R_PPC_PLT16_HA:
3765 if (h != NULL)
3767 if (h->plt.refcount > 0)
3768 h->plt.refcount--;
3770 break;
3772 default:
3773 break;
3776 return TRUE;
3779 /* Set htab->tls_sec and htab->tls_get_addr. */
3781 bfd_boolean
3782 ppc_elf_tls_setup (obfd, info)
3783 bfd *obfd;
3784 struct bfd_link_info *info;
3786 asection *tls;
3787 struct ppc_elf_link_hash_table *htab;
3789 htab = ppc_elf_hash_table (info);
3790 htab->tls_get_addr = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
3791 FALSE, FALSE, TRUE);
3793 for (tls = obfd->sections; tls != NULL; tls = tls->next)
3794 if ((tls->flags & (SEC_THREAD_LOCAL | SEC_LOAD))
3795 == (SEC_THREAD_LOCAL | SEC_LOAD))
3796 break;
3797 htab->tls_sec = tls;
3799 return tls != NULL;
3802 /* Run through all the TLS relocs looking for optimization
3803 opportunities. */
3805 bfd_boolean
3806 ppc_elf_tls_optimize (obfd, info)
3807 bfd *obfd ATTRIBUTE_UNUSED;
3808 struct bfd_link_info *info;
3810 bfd *ibfd;
3811 asection *sec;
3812 struct ppc_elf_link_hash_table *htab;
3814 if (info->relocateable || info->shared)
3815 return TRUE;
3817 htab = ppc_elf_hash_table (info);
3818 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
3820 Elf_Internal_Sym *locsyms = NULL;
3821 Elf_Internal_Shdr *symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
3823 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
3824 if (sec->has_tls_reloc && !bfd_is_abs_section (sec->output_section))
3826 Elf_Internal_Rela *relstart, *rel, *relend;
3827 int expecting_tls_get_addr;
3829 /* Read the relocations. */
3830 relstart = _bfd_elf32_link_read_relocs (ibfd, sec, (PTR) NULL,
3831 (Elf_Internal_Rela *) NULL,
3832 info->keep_memory);
3833 if (relstart == NULL)
3834 return FALSE;
3836 expecting_tls_get_addr = 0;
3837 relend = relstart + sec->reloc_count;
3838 for (rel = relstart; rel < relend; rel++)
3840 enum elf_ppc_reloc_type r_type;
3841 unsigned long r_symndx;
3842 struct elf_link_hash_entry *h = NULL;
3843 char *tls_mask;
3844 char tls_set, tls_clear;
3845 bfd_boolean is_local;
3847 r_symndx = ELF32_R_SYM (rel->r_info);
3848 if (r_symndx >= symtab_hdr->sh_info)
3850 struct elf_link_hash_entry **sym_hashes;
3852 sym_hashes = elf_sym_hashes (ibfd);
3853 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
3854 while (h->root.type == bfd_link_hash_indirect
3855 || h->root.type == bfd_link_hash_warning)
3856 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3859 is_local = FALSE;
3860 if (h == NULL
3861 || !(h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC))
3862 is_local = TRUE;
3864 r_type = (enum elf_ppc_reloc_type) ELF32_R_TYPE (rel->r_info);
3865 switch (r_type)
3867 case R_PPC_GOT_TLSLD16:
3868 case R_PPC_GOT_TLSLD16_LO:
3869 case R_PPC_GOT_TLSLD16_HI:
3870 case R_PPC_GOT_TLSLD16_HA:
3871 /* These relocs should never be against a symbol
3872 defined in a shared lib. Leave them alone if
3873 that turns out to be the case. */
3874 expecting_tls_get_addr = 0;
3875 htab->tlsld_got.refcount -= 1;
3876 if (!is_local)
3877 continue;
3879 /* LD -> LE */
3880 tls_set = 0;
3881 tls_clear = TLS_LD;
3882 expecting_tls_get_addr = 1;
3883 break;
3885 case R_PPC_GOT_TLSGD16:
3886 case R_PPC_GOT_TLSGD16_LO:
3887 case R_PPC_GOT_TLSGD16_HI:
3888 case R_PPC_GOT_TLSGD16_HA:
3889 if (is_local)
3890 /* GD -> LE */
3891 tls_set = 0;
3892 else
3893 /* GD -> IE */
3894 tls_set = TLS_TLS | TLS_TPRELGD;
3895 tls_clear = TLS_GD;
3896 expecting_tls_get_addr = 1;
3897 break;
3899 case R_PPC_GOT_TPREL16:
3900 case R_PPC_GOT_TPREL16_LO:
3901 case R_PPC_GOT_TPREL16_HI:
3902 case R_PPC_GOT_TPREL16_HA:
3903 expecting_tls_get_addr = 0;
3904 if (is_local)
3906 /* IE -> LE */
3907 tls_set = 0;
3908 tls_clear = TLS_TPREL;
3909 break;
3911 else
3912 continue;
3914 case R_PPC_REL14:
3915 case R_PPC_REL14_BRTAKEN:
3916 case R_PPC_REL14_BRNTAKEN:
3917 case R_PPC_REL24:
3918 if (expecting_tls_get_addr
3919 && h != NULL
3920 && h == htab->tls_get_addr)
3922 if (h->plt.refcount > 0)
3923 h->plt.refcount -= 1;
3925 expecting_tls_get_addr = 0;
3926 continue;
3928 default:
3929 expecting_tls_get_addr = 0;
3930 continue;
3933 if (h != NULL)
3935 if (tls_set == 0)
3937 /* We managed to get rid of a got entry. */
3938 if (h->got.refcount > 0)
3939 h->got.refcount -= 1;
3941 tls_mask = &ppc_elf_hash_entry (h)->tls_mask;
3943 else
3945 Elf_Internal_Sym *sym;
3946 bfd_signed_vma *lgot_refs;
3947 char *lgot_masks;
3949 if (locsyms == NULL)
3951 locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
3952 if (locsyms == NULL)
3953 locsyms = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
3954 symtab_hdr->sh_info,
3955 0, NULL, NULL, NULL);
3956 if (locsyms == NULL)
3958 if (elf_section_data (sec)->relocs != relstart)
3959 free (relstart);
3960 return FALSE;
3963 sym = locsyms + r_symndx;
3964 lgot_refs = elf_local_got_refcounts (ibfd);
3965 if (lgot_refs == NULL)
3966 abort ();
3967 if (tls_set == 0)
3969 /* We managed to get rid of a got entry. */
3970 if (lgot_refs[r_symndx] > 0)
3971 lgot_refs[r_symndx] -= 1;
3973 lgot_masks = (char *) (lgot_refs + symtab_hdr->sh_info);
3974 tls_mask = &lgot_masks[r_symndx];
3977 *tls_mask |= tls_set;
3978 *tls_mask &= ~tls_clear;
3981 if (elf_section_data (sec)->relocs != relstart)
3982 free (relstart);
3985 if (locsyms != NULL
3986 && (symtab_hdr->contents != (unsigned char *) locsyms))
3988 if (!info->keep_memory)
3989 free (locsyms);
3990 else
3991 symtab_hdr->contents = (unsigned char *) locsyms;
3994 return TRUE;
3997 /* Hook called by the linker routine which adds symbols from an object
3998 file. We use it to put .comm items in .sbss, and not .bss. */
4000 static bfd_boolean
4001 ppc_elf_add_symbol_hook (abfd, info, sym, namep, flagsp, secp, valp)
4002 bfd *abfd;
4003 struct bfd_link_info *info;
4004 const Elf_Internal_Sym *sym;
4005 const char **namep ATTRIBUTE_UNUSED;
4006 flagword *flagsp ATTRIBUTE_UNUSED;
4007 asection **secp;
4008 bfd_vma *valp;
4010 if (sym->st_shndx == SHN_COMMON
4011 && !info->relocateable
4012 && sym->st_size <= elf_gp_size (abfd)
4013 && info->hash->creator->flavour == bfd_target_elf_flavour)
4015 /* Common symbols less than or equal to -G nn bytes are automatically
4016 put into .sdata. */
4017 elf_linker_section_t *sdata
4018 = ppc_elf_create_linker_section (abfd, info, LINKER_SECTION_SDATA);
4020 if (!sdata->bss_section)
4022 bfd_size_type amt;
4024 /* We don't go through bfd_make_section, because we don't
4025 want to attach this common section to DYNOBJ. The linker
4026 will move the symbols to the appropriate output section
4027 when it defines common symbols. */
4028 amt = sizeof (asection);
4029 sdata->bss_section = (asection *) bfd_zalloc (abfd, amt);
4030 if (sdata->bss_section == NULL)
4031 return FALSE;
4032 sdata->bss_section->name = sdata->bss_name;
4033 sdata->bss_section->flags = SEC_IS_COMMON;
4034 sdata->bss_section->output_section = sdata->bss_section;
4035 amt = sizeof (asymbol);
4036 sdata->bss_section->symbol = (asymbol *) bfd_zalloc (abfd, amt);
4037 amt = sizeof (asymbol *);
4038 sdata->bss_section->symbol_ptr_ptr =
4039 (asymbol **) bfd_zalloc (abfd, amt);
4040 if (sdata->bss_section->symbol == NULL
4041 || sdata->bss_section->symbol_ptr_ptr == NULL)
4042 return FALSE;
4043 sdata->bss_section->symbol->name = sdata->bss_name;
4044 sdata->bss_section->symbol->flags = BSF_SECTION_SYM;
4045 sdata->bss_section->symbol->section = sdata->bss_section;
4046 *sdata->bss_section->symbol_ptr_ptr = sdata->bss_section->symbol;
4049 *secp = sdata->bss_section;
4050 *valp = sym->st_size;
4053 return TRUE;
4056 /* Finish up dynamic symbol handling. We set the contents of various
4057 dynamic sections here. */
4059 static bfd_boolean
4060 ppc_elf_finish_dynamic_symbol (output_bfd, info, h, sym)
4061 bfd *output_bfd;
4062 struct bfd_link_info *info;
4063 struct elf_link_hash_entry *h;
4064 Elf_Internal_Sym *sym;
4066 struct ppc_elf_link_hash_table *htab;
4068 #ifdef DEBUG
4069 fprintf (stderr, "ppc_elf_finish_dynamic_symbol called for %s",
4070 h->root.root.string);
4071 #endif
4073 htab = ppc_elf_hash_table (info);
4074 BFD_ASSERT (htab->elf.dynobj != NULL);
4076 if (h->plt.offset != (bfd_vma) -1)
4078 Elf_Internal_Rela rela;
4079 bfd_byte *loc;
4080 bfd_vma reloc_index;
4082 #ifdef DEBUG
4083 fprintf (stderr, ", plt_offset = %d", h->plt.offset);
4084 #endif
4086 /* This symbol has an entry in the procedure linkage table. Set
4087 it up. */
4089 BFD_ASSERT (h->dynindx != -1);
4090 BFD_ASSERT (htab->plt != NULL && htab->relplt != NULL);
4092 /* We don't need to fill in the .plt. The ppc dynamic linker
4093 will fill it in. */
4095 /* Fill in the entry in the .rela.plt section. */
4096 rela.r_offset = (htab->plt->output_section->vma
4097 + htab->plt->output_offset
4098 + h->plt.offset);
4099 rela.r_info = ELF32_R_INFO (h->dynindx, R_PPC_JMP_SLOT);
4100 rela.r_addend = 0;
4102 reloc_index = (h->plt.offset - PLT_INITIAL_ENTRY_SIZE) / PLT_SLOT_SIZE;
4103 if (reloc_index > PLT_NUM_SINGLE_ENTRIES)
4104 reloc_index -= (reloc_index - PLT_NUM_SINGLE_ENTRIES) / 2;
4105 loc = (htab->relplt->contents
4106 + reloc_index * sizeof (Elf32_External_Rela));
4107 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
4109 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
4111 /* Mark the symbol as undefined, rather than as defined in
4112 the .plt section. Leave the value alone. */
4113 sym->st_shndx = SHN_UNDEF;
4114 /* If the symbol is weak, we do need to clear the value.
4115 Otherwise, the PLT entry would provide a definition for
4116 the symbol even if the symbol wasn't defined anywhere,
4117 and so the symbol would never be NULL. */
4118 if ((h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR_NONWEAK)
4119 == 0)
4120 sym->st_value = 0;
4124 if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_COPY) != 0)
4126 asection *s;
4127 Elf_Internal_Rela rela;
4128 bfd_byte *loc;
4130 /* This symbols needs a copy reloc. Set it up. */
4132 #ifdef DEBUG
4133 fprintf (stderr, ", copy");
4134 #endif
4136 BFD_ASSERT (h->dynindx != -1);
4138 if (h->size <= elf_gp_size (htab->elf.dynobj))
4139 s = htab->relsbss;
4140 else
4141 s = htab->relbss;
4142 BFD_ASSERT (s != NULL);
4144 rela.r_offset = (h->root.u.def.value
4145 + h->root.u.def.section->output_section->vma
4146 + h->root.u.def.section->output_offset);
4147 rela.r_info = ELF32_R_INFO (h->dynindx, R_PPC_COPY);
4148 rela.r_addend = 0;
4149 loc = s->contents + s->reloc_count++ * sizeof (Elf32_External_Rela);
4150 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
4153 #ifdef DEBUG
4154 fprintf (stderr, "\n");
4155 #endif
4157 /* Mark some specially defined symbols as absolute. */
4158 if (strcmp (h->root.root.string, "_DYNAMIC") == 0
4159 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0
4160 || strcmp (h->root.root.string, "_PROCEDURE_LINKAGE_TABLE_") == 0)
4161 sym->st_shndx = SHN_ABS;
4163 return TRUE;
4166 /* Finish up the dynamic sections. */
4168 static bfd_boolean
4169 ppc_elf_finish_dynamic_sections (output_bfd, info)
4170 bfd *output_bfd;
4171 struct bfd_link_info *info;
4173 asection *sdyn;
4174 struct ppc_elf_link_hash_table *htab;
4176 #ifdef DEBUG
4177 fprintf (stderr, "ppc_elf_finish_dynamic_sections called\n");
4178 #endif
4180 htab = ppc_elf_hash_table (info);
4181 sdyn = bfd_get_section_by_name (htab->elf.dynobj, ".dynamic");
4183 if (htab->elf.dynamic_sections_created)
4185 Elf32_External_Dyn *dyncon, *dynconend;
4187 BFD_ASSERT (htab->plt != NULL && sdyn != NULL);
4189 dyncon = (Elf32_External_Dyn *) sdyn->contents;
4190 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->_raw_size);
4191 for (; dyncon < dynconend; dyncon++)
4193 Elf_Internal_Dyn dyn;
4194 asection *s;
4196 bfd_elf32_swap_dyn_in (htab->elf.dynobj, dyncon, &dyn);
4198 switch (dyn.d_tag)
4200 case DT_PLTGOT:
4201 s = htab->plt;
4202 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
4203 break;
4205 case DT_PLTRELSZ:
4206 dyn.d_un.d_val = htab->relplt->_raw_size;
4207 break;
4209 case DT_JMPREL:
4210 s = htab->relplt;
4211 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
4212 break;
4214 default:
4215 continue;
4218 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
4222 /* Add a blrl instruction at _GLOBAL_OFFSET_TABLE_-4 so that a function can
4223 easily find the address of the _GLOBAL_OFFSET_TABLE_. */
4224 if (htab->got)
4226 unsigned char *contents = htab->got->contents;
4227 bfd_put_32 (output_bfd, (bfd_vma) 0x4e800021 /* blrl */, contents);
4229 if (sdyn == NULL)
4230 bfd_put_32 (output_bfd, (bfd_vma) 0, contents + 4);
4231 else
4232 bfd_put_32 (output_bfd,
4233 sdyn->output_section->vma + sdyn->output_offset,
4234 contents + 4);
4236 elf_section_data (htab->got->output_section)->this_hdr.sh_entsize = 4;
4239 return TRUE;
4242 /* The RELOCATE_SECTION function is called by the ELF backend linker
4243 to handle the relocations for a section.
4245 The relocs are always passed as Rela structures; if the section
4246 actually uses Rel structures, the r_addend field will always be
4247 zero.
4249 This function is responsible for adjust the section contents as
4250 necessary, and (if using Rela relocs and generating a
4251 relocateable output file) adjusting the reloc addend as
4252 necessary.
4254 This function does not have to worry about setting the reloc
4255 address or the reloc symbol index.
4257 LOCAL_SYMS is a pointer to the swapped in local symbols.
4259 LOCAL_SECTIONS is an array giving the section in the input file
4260 corresponding to the st_shndx field of each local symbol.
4262 The global hash table entry for the global symbols can be found
4263 via elf_sym_hashes (input_bfd).
4265 When generating relocateable output, this function must handle
4266 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
4267 going to be the section symbol corresponding to the output
4268 section, which means that the addend must be adjusted
4269 accordingly. */
4271 static bfd_boolean
4272 ppc_elf_relocate_section (output_bfd, info, input_bfd, input_section,
4273 contents, relocs, local_syms, local_sections)
4274 bfd *output_bfd;
4275 struct bfd_link_info *info;
4276 bfd *input_bfd;
4277 asection *input_section;
4278 bfd_byte *contents;
4279 Elf_Internal_Rela *relocs;
4280 Elf_Internal_Sym *local_syms;
4281 asection **local_sections;
4283 Elf_Internal_Shdr *symtab_hdr;
4284 struct elf_link_hash_entry **sym_hashes;
4285 struct ppc_elf_link_hash_table *htab;
4286 Elf_Internal_Rela *rel;
4287 Elf_Internal_Rela *relend;
4288 Elf_Internal_Rela outrel;
4289 bfd_byte *loc;
4290 asection *sreloc = NULL;
4291 bfd_vma *local_got_offsets;
4292 bfd_boolean ret = TRUE;
4294 #ifdef DEBUG
4295 fprintf (stderr, "ppc_elf_relocate_section called for %s section %s, %ld relocations%s\n",
4296 bfd_archive_filename (input_bfd),
4297 bfd_section_name(input_bfd, input_section),
4298 (long) input_section->reloc_count,
4299 (info->relocateable) ? " (relocatable)" : "");
4300 #endif
4302 if (info->relocateable)
4303 return TRUE;
4305 if (!ppc_elf_howto_table[R_PPC_ADDR32])
4306 /* Initialize howto table if needed. */
4307 ppc_elf_howto_init ();
4309 htab = ppc_elf_hash_table (info);
4310 local_got_offsets = elf_local_got_offsets (input_bfd);
4311 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
4312 sym_hashes = elf_sym_hashes (input_bfd);
4313 rel = relocs;
4314 relend = relocs + input_section->reloc_count;
4315 for (; rel < relend; rel++)
4317 enum elf_ppc_reloc_type r_type;
4318 bfd_vma addend;
4319 bfd_reloc_status_type r;
4320 Elf_Internal_Sym *sym;
4321 asection *sec;
4322 struct elf_link_hash_entry *h;
4323 const char *sym_name;
4324 reloc_howto_type *howto;
4325 unsigned long r_symndx;
4326 bfd_vma relocation;
4327 bfd_vma branch_bit, insn, from;
4328 bfd_boolean unresolved_reloc;
4329 bfd_boolean warned;
4330 unsigned int tls_type, tls_mask, tls_gd;
4332 r_type = (enum elf_ppc_reloc_type)ELF32_R_TYPE (rel->r_info);
4333 sym = (Elf_Internal_Sym *) 0;
4334 sec = (asection *) 0;
4335 h = (struct elf_link_hash_entry *) 0;
4336 unresolved_reloc = FALSE;
4337 warned = FALSE;
4338 r_symndx = ELF32_R_SYM (rel->r_info);
4339 if (r_symndx < symtab_hdr->sh_info)
4341 sym = local_syms + r_symndx;
4342 sec = local_sections[r_symndx];
4343 sym_name = bfd_elf_local_sym_name (input_bfd, sym);
4345 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, sec, rel);
4347 else
4349 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
4350 while (h->root.type == bfd_link_hash_indirect
4351 || h->root.type == bfd_link_hash_warning)
4352 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4353 sym_name = h->root.root.string;
4355 relocation = 0;
4356 if (h->root.type == bfd_link_hash_defined
4357 || h->root.type == bfd_link_hash_defweak)
4359 sec = h->root.u.def.section;
4360 /* Set a flag that will be cleared later if we find a
4361 relocation value for this symbol. output_section
4362 is typically NULL for symbols satisfied by a shared
4363 library. */
4364 if (sec->output_section == NULL)
4365 unresolved_reloc = TRUE;
4366 else
4367 relocation = (h->root.u.def.value
4368 + sec->output_section->vma
4369 + sec->output_offset);
4371 else if (h->root.type == bfd_link_hash_undefweak)
4373 else if (info->shared
4374 && !info->no_undefined
4375 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
4377 else
4379 if (! ((*info->callbacks->undefined_symbol)
4380 (info, h->root.root.string, input_bfd, input_section,
4381 rel->r_offset, (!info->shared
4382 || info->no_undefined
4383 || ELF_ST_VISIBILITY (h->other)))))
4384 return FALSE;
4385 warned = TRUE;
4389 /* TLS optimizations. Replace instruction sequences and relocs
4390 based on information we collected in tls_optimize. We edit
4391 RELOCS so that --emit-relocs will output something sensible
4392 for the final instruction stream. */
4393 tls_mask = 0;
4394 tls_gd = 0;
4395 if (IS_PPC_TLS_RELOC (r_type))
4397 if (h != NULL)
4398 tls_mask = ((struct ppc_elf_link_hash_entry *) h)->tls_mask;
4399 else if (local_got_offsets != NULL)
4401 char *lgot_masks;
4402 lgot_masks = (char *) (local_got_offsets + symtab_hdr->sh_info);
4403 tls_mask = lgot_masks[r_symndx];
4407 /* Ensure reloc mapping code below stays sane. */
4408 if ((R_PPC_GOT_TLSLD16 & 3) != (R_PPC_GOT_TLSGD16 & 3)
4409 || (R_PPC_GOT_TLSLD16_LO & 3) != (R_PPC_GOT_TLSGD16_LO & 3)
4410 || (R_PPC_GOT_TLSLD16_HI & 3) != (R_PPC_GOT_TLSGD16_HI & 3)
4411 || (R_PPC_GOT_TLSLD16_HA & 3) != (R_PPC_GOT_TLSGD16_HA & 3)
4412 || (R_PPC_GOT_TLSLD16 & 3) != (R_PPC_GOT_TPREL16 & 3)
4413 || (R_PPC_GOT_TLSLD16_LO & 3) != (R_PPC_GOT_TPREL16_LO & 3)
4414 || (R_PPC_GOT_TLSLD16_HI & 3) != (R_PPC_GOT_TPREL16_HI & 3)
4415 || (R_PPC_GOT_TLSLD16_HA & 3) != (R_PPC_GOT_TPREL16_HA & 3))
4416 abort ();
4417 switch (r_type)
4419 default:
4420 break;
4422 case R_PPC_GOT_TPREL16:
4423 case R_PPC_GOT_TPREL16_LO:
4424 if (tls_mask != 0
4425 && (tls_mask & TLS_TPREL) == 0)
4427 bfd_vma insn;
4428 insn = bfd_get_32 (output_bfd, contents + rel->r_offset - 2);
4429 insn &= 31 << 21;
4430 insn |= 0x3c020000; /* addis 0,2,0 */
4431 bfd_put_32 (output_bfd, insn, contents + rel->r_offset - 2);
4432 r_type = R_PPC_TPREL16_HA;
4433 rel->r_info = ELF32_R_INFO (r_symndx, r_type);
4435 break;
4437 case R_PPC_TLS:
4438 if (tls_mask != 0
4439 && (tls_mask & TLS_TPREL) == 0)
4441 bfd_vma insn, rtra;
4442 insn = bfd_get_32 (output_bfd, contents + rel->r_offset);
4443 if ((insn & ((31 << 26) | (31 << 11)))
4444 == ((31 << 26) | (2 << 11)))
4445 rtra = insn & ((1 << 26) - (1 << 16));
4446 else if ((insn & ((31 << 26) | (31 << 16)))
4447 == ((31 << 26) | (2 << 16)))
4448 rtra = (insn & (31 << 21)) | ((insn & (31 << 11)) << 5);
4449 else
4450 abort ();
4451 if ((insn & ((1 << 11) - (1 << 1))) == 266 << 1)
4452 /* add -> addi. */
4453 insn = 14 << 26;
4454 else if ((insn & (31 << 1)) == 23 << 1
4455 && ((insn & (31 << 6)) < 14 << 6
4456 || ((insn & (31 << 6)) >= 16 << 6
4457 && (insn & (31 << 6)) < 24 << 6)))
4458 /* load and store indexed -> dform. */
4459 insn = (32 | ((insn >> 6) & 31)) << 26;
4460 else if ((insn & (31 << 1)) == 21 << 1
4461 && (insn & (0x1a << 6)) == 0)
4462 /* ldx, ldux, stdx, stdux -> ld, ldu, std, stdu. */
4463 insn = (((58 | ((insn >> 6) & 4)) << 26)
4464 | ((insn >> 6) & 1));
4465 else if ((insn & (31 << 1)) == 21 << 1
4466 && (insn & ((1 << 11) - (1 << 1))) == 341 << 1)
4467 /* lwax -> lwa. */
4468 insn = (58 << 26) | 2;
4469 else
4470 abort ();
4471 insn |= rtra;
4472 bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
4473 r_type = R_PPC_TPREL16_LO;
4474 rel->r_info = ELF32_R_INFO (r_symndx, r_type);
4475 /* Was PPC_TLS which sits on insn boundary, now
4476 PPC_TPREL16_LO which is at insn+2. */
4477 rel->r_offset += 2;
4479 break;
4481 case R_PPC_GOT_TLSGD16_HI:
4482 case R_PPC_GOT_TLSGD16_HA:
4483 tls_gd = TLS_TPRELGD;
4484 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
4485 goto tls_gdld_hi;
4486 break;
4488 case R_PPC_GOT_TLSLD16_HI:
4489 case R_PPC_GOT_TLSLD16_HA:
4490 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
4492 tls_gdld_hi:
4493 if ((tls_mask & tls_gd) != 0)
4494 r_type = (((r_type - (R_PPC_GOT_TLSGD16 & 3)) & 3)
4495 + R_PPC_GOT_TPREL16);
4496 else
4498 bfd_put_32 (output_bfd, NOP, contents + rel->r_offset);
4499 rel->r_offset -= 2;
4500 r_type = R_PPC_NONE;
4502 rel->r_info = ELF32_R_INFO (r_symndx, r_type);
4504 break;
4506 case R_PPC_GOT_TLSGD16:
4507 case R_PPC_GOT_TLSGD16_LO:
4508 tls_gd = TLS_TPRELGD;
4509 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
4510 goto tls_get_addr_check;
4511 break;
4513 case R_PPC_GOT_TLSLD16:
4514 case R_PPC_GOT_TLSLD16_LO:
4515 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
4517 tls_get_addr_check:
4518 if (rel + 1 < relend)
4520 enum elf_ppc_reloc_type r_type2;
4521 unsigned long r_symndx2;
4522 struct elf_link_hash_entry *h2;
4523 bfd_vma insn1, insn2, insn3;
4524 bfd_vma offset;
4526 /* The next instruction should be a call to
4527 __tls_get_addr. Peek at the reloc to be sure. */
4528 r_type2
4529 = (enum elf_ppc_reloc_type) ELF32_R_TYPE (rel[1].r_info);
4530 r_symndx2 = ELF32_R_SYM (rel[1].r_info);
4531 if (r_symndx2 < symtab_hdr->sh_info
4532 || (r_type2 != R_PPC_REL14
4533 && r_type2 != R_PPC_REL14_BRTAKEN
4534 && r_type2 != R_PPC_REL14_BRNTAKEN
4535 && r_type2 != R_PPC_REL24
4536 && r_type2 != R_PPC_PLTREL24))
4537 break;
4539 h2 = sym_hashes[r_symndx2 - symtab_hdr->sh_info];
4540 while (h2->root.type == bfd_link_hash_indirect
4541 || h2->root.type == bfd_link_hash_warning)
4542 h2 = (struct elf_link_hash_entry *) h2->root.u.i.link;
4543 if (h2 == NULL || h2 != htab->tls_get_addr)
4544 break;
4546 /* OK, it checks out. Replace the call. */
4547 offset = rel[1].r_offset;
4548 insn1 = bfd_get_32 (output_bfd,
4549 contents + rel->r_offset - 2);
4550 insn3 = bfd_get_32 (output_bfd,
4551 contents + offset + 4);
4552 if ((tls_mask & tls_gd) != 0)
4554 /* IE */
4555 insn1 &= (1 << 26) - 1;
4556 insn1 |= 32 << 26; /* lwz */
4557 insn2 = 0x7c631214; /* add 3,3,2 */
4558 rel[1].r_info = ELF32_R_INFO (r_symndx2, R_PPC_NONE);
4559 r_type = (((r_type - (R_PPC_GOT_TLSGD16 & 3)) & 3)
4560 + R_PPC_GOT_TPREL16);
4561 rel->r_info = ELF32_R_INFO (r_symndx, r_type);
4563 else
4565 /* LE */
4566 insn1 = 0x3c620000; /* addis 3,2,0 */
4567 insn2 = 0x38630000; /* addi 3,3,0 */
4568 if (tls_gd == 0)
4570 /* Was an LD reloc. */
4571 r_symndx = 0;
4572 rel->r_addend = htab->tls_sec->vma + DTP_OFFSET;
4573 rel[1].r_addend = htab->tls_sec->vma + DTP_OFFSET;
4575 r_type = R_PPC_TPREL16_HA;
4576 rel->r_info = ELF32_R_INFO (r_symndx, r_type);
4577 rel[1].r_info = ELF32_R_INFO (r_symndx,
4578 R_PPC_TPREL16_LO);
4579 rel[1].r_offset += 2;
4581 if (insn3 == NOP
4582 || insn3 == CROR_151515 || insn3 == CROR_313131)
4584 insn3 = insn2;
4585 insn2 = NOP;
4586 rel[1].r_offset += 4;
4588 bfd_put_32 (output_bfd, insn1, contents + rel->r_offset - 2);
4589 bfd_put_32 (output_bfd, insn2, contents + offset);
4590 bfd_put_32 (output_bfd, insn3, contents + offset + 4);
4591 if (tls_gd == 0)
4593 /* We changed the symbol on an LD reloc. Start over
4594 in order to get h, sym, sec etc. right. */
4595 rel--;
4596 continue;
4600 break;
4603 /* Handle other relocations that tweak non-addend part of insn. */
4604 branch_bit = 0;
4605 switch (r_type)
4607 default:
4608 break;
4610 /* Branch taken prediction relocations. */
4611 case R_PPC_ADDR14_BRTAKEN:
4612 case R_PPC_REL14_BRTAKEN:
4613 branch_bit = BRANCH_PREDICT_BIT;
4614 /* Fall thru */
4616 /* Branch not taken predicition relocations. */
4617 case R_PPC_ADDR14_BRNTAKEN:
4618 case R_PPC_REL14_BRNTAKEN:
4619 insn = bfd_get_32 (output_bfd, contents + rel->r_offset);
4620 insn &= ~BRANCH_PREDICT_BIT;
4621 insn |= branch_bit;
4623 from = (rel->r_offset
4624 + input_section->output_offset
4625 + input_section->output_section->vma);
4627 /* Invert 'y' bit if not the default. */
4628 if ((bfd_signed_vma) (relocation + rel->r_addend - from) < 0)
4629 insn ^= BRANCH_PREDICT_BIT;
4631 bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
4632 break;
4635 addend = rel->r_addend;
4636 tls_type = 0;
4637 howto = NULL;
4638 if ((unsigned) r_type < (unsigned) R_PPC_max)
4639 howto = ppc_elf_howto_table[(int) r_type];
4640 switch (r_type)
4642 default:
4643 (*_bfd_error_handler)
4644 (_("%s: unknown relocation type %d for symbol %s"),
4645 bfd_archive_filename (input_bfd), (int) r_type, sym_name);
4647 bfd_set_error (bfd_error_bad_value);
4648 ret = FALSE;
4649 continue;
4651 case R_PPC_NONE:
4652 case R_PPC_TLS:
4653 case R_PPC_EMB_MRKREF:
4654 case R_PPC_GNU_VTINHERIT:
4655 case R_PPC_GNU_VTENTRY:
4656 continue;
4658 /* GOT16 relocations. Like an ADDR16 using the symbol's
4659 address in the GOT as relocation value instead of the
4660 symbol's value itself. Also, create a GOT entry for the
4661 symbol and put the symbol value there. */
4662 case R_PPC_GOT_TLSGD16:
4663 case R_PPC_GOT_TLSGD16_LO:
4664 case R_PPC_GOT_TLSGD16_HI:
4665 case R_PPC_GOT_TLSGD16_HA:
4666 tls_type = TLS_TLS | TLS_GD;
4667 goto dogot;
4669 case R_PPC_GOT_TLSLD16:
4670 case R_PPC_GOT_TLSLD16_LO:
4671 case R_PPC_GOT_TLSLD16_HI:
4672 case R_PPC_GOT_TLSLD16_HA:
4673 tls_type = TLS_TLS | TLS_LD;
4674 goto dogot;
4676 case R_PPC_GOT_TPREL16:
4677 case R_PPC_GOT_TPREL16_LO:
4678 case R_PPC_GOT_TPREL16_HI:
4679 case R_PPC_GOT_TPREL16_HA:
4680 tls_type = TLS_TLS | TLS_TPREL;
4681 goto dogot;
4683 case R_PPC_GOT_DTPREL16:
4684 case R_PPC_GOT_DTPREL16_LO:
4685 case R_PPC_GOT_DTPREL16_HI:
4686 case R_PPC_GOT_DTPREL16_HA:
4687 tls_type = TLS_TLS | TLS_DTPREL;
4688 goto dogot;
4690 case R_PPC_GOT16:
4691 case R_PPC_GOT16_LO:
4692 case R_PPC_GOT16_HI:
4693 case R_PPC_GOT16_HA:
4694 dogot:
4696 /* Relocation is to the entry for this symbol in the global
4697 offset table. */
4698 bfd_vma off;
4699 bfd_vma *offp;
4700 unsigned long indx;
4702 if (htab->got == NULL)
4703 abort ();
4705 indx = 0;
4706 if (tls_type == (TLS_TLS | TLS_LD)
4707 && (h == NULL
4708 || !(h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC)))
4709 offp = &htab->tlsld_got.offset;
4710 else if (h != NULL)
4712 bfd_boolean dyn;
4713 dyn = htab->elf.dynamic_sections_created;
4714 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
4715 || (info->shared
4716 && SYMBOL_REFERENCES_LOCAL (info, h)))
4717 /* This is actually a static link, or it is a
4718 -Bsymbolic link and the symbol is defined
4719 locally, or the symbol was forced to be local
4720 because of a version file. */
4722 else
4724 indx = h->dynindx;
4725 unresolved_reloc = FALSE;
4727 offp = &h->got.offset;
4729 else
4731 if (local_got_offsets == NULL)
4732 abort ();
4733 offp = &local_got_offsets[r_symndx];
4736 /* The offset must always be a multiple of 4. We use the
4737 least significant bit to record whether we have already
4738 processed this entry. */
4739 off = *offp;
4740 if ((off & 1) != 0)
4741 off &= ~1;
4742 else
4744 unsigned int tls_m = (tls_mask
4745 & (TLS_LD | TLS_GD | TLS_DTPREL
4746 | TLS_TPREL | TLS_TPRELGD));
4748 if (offp == &htab->tlsld_got.offset)
4749 tls_m = TLS_LD;
4750 else if (h == NULL
4751 || !(h->elf_link_hash_flags
4752 & ELF_LINK_HASH_DEF_DYNAMIC))
4753 tls_m &= ~TLS_LD;
4755 /* We might have multiple got entries for this sym.
4756 Initialize them all. */
4759 int tls_ty = 0;
4761 if ((tls_m & TLS_LD) != 0)
4763 tls_ty = TLS_TLS | TLS_LD;
4764 tls_m &= ~TLS_LD;
4766 else if ((tls_m & TLS_GD) != 0)
4768 tls_ty = TLS_TLS | TLS_GD;
4769 tls_m &= ~TLS_GD;
4771 else if ((tls_m & TLS_DTPREL) != 0)
4773 tls_ty = TLS_TLS | TLS_DTPREL;
4774 tls_m &= ~TLS_DTPREL;
4776 else if ((tls_m & (TLS_TPREL | TLS_TPRELGD)) != 0)
4778 tls_ty = TLS_TLS | TLS_TPREL;
4779 tls_m = 0;
4782 /* Generate relocs for the dynamic linker. */
4783 if (info->shared || indx != 0)
4785 outrel.r_offset = (htab->got->output_section->vma
4786 + htab->got->output_offset
4787 + off);
4788 if (tls_ty & (TLS_LD | TLS_GD))
4790 outrel.r_info = ELF32_R_INFO (indx, R_PPC_DTPMOD32);
4791 outrel.r_addend = 0;
4792 if (tls_ty == (TLS_TLS | TLS_GD))
4794 loc = htab->relgot->contents;
4795 loc += (htab->relgot->reloc_count++
4796 * sizeof (Elf32_External_Rela));
4797 bfd_elf32_swap_reloca_out (output_bfd,
4798 &outrel, loc);
4799 outrel.r_info
4800 = ELF32_R_INFO (indx, R_PPC_DTPREL32);
4801 outrel.r_offset += 4;
4804 else if (tls_ty == (TLS_TLS | TLS_DTPREL))
4805 outrel.r_info = ELF32_R_INFO (indx, R_PPC_DTPREL32);
4806 else if (tls_ty == (TLS_TLS | TLS_TPREL))
4807 outrel.r_info = ELF32_R_INFO (indx, R_PPC_TPREL32);
4808 else if (indx == 0)
4809 outrel.r_info = ELF32_R_INFO (indx, R_PPC_RELATIVE);
4810 else
4811 outrel.r_info = ELF32_R_INFO (indx, R_PPC_GLOB_DAT);
4812 outrel.r_addend = 0;
4813 if (indx == 0)
4814 outrel.r_addend += relocation;
4815 loc = htab->relgot->contents;
4816 loc += (htab->relgot->reloc_count++
4817 * sizeof (Elf32_External_Rela));
4818 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
4821 /* Init the .got section contents if we're not
4822 emitting a reloc. */
4823 else
4825 bfd_vma value = relocation;
4827 if ((tls_ty & (TLS_GD | TLS_TPREL | TLS_DTPREL
4828 | TLS_TPRELGD)) != 0)
4830 value -= htab->tls_sec->vma + DTP_OFFSET;
4831 if ((tls_ty & TLS_TPREL) != 0)
4832 value += DTP_OFFSET - TP_OFFSET;
4835 if (tls_ty == (TLS_TLS | TLS_GD))
4837 bfd_put_32 (output_bfd, value,
4838 htab->got->contents + off + 4);
4839 value = 1;
4841 else if (tls_ty == (TLS_TLS | TLS_LD))
4842 value = 1;
4843 bfd_put_32 (output_bfd, value,
4844 htab->got->contents + off);
4847 off += 4;
4848 if (tls_ty & (TLS_LD | TLS_GD))
4849 off += 4;
4851 while (tls_m != 0);
4853 off = *offp;
4854 *offp = off | 1;
4857 if (off >= (bfd_vma) -2)
4858 abort ();
4860 if ((tls_type & TLS_TLS) != 0)
4862 if (tls_type != (TLS_TLS | TLS_LD))
4864 if ((tls_mask & TLS_LD) != 0
4865 && !(h == NULL
4866 || !(h->elf_link_hash_flags
4867 & ELF_LINK_HASH_DEF_DYNAMIC)))
4868 off += 8;
4869 if (tls_type != (TLS_TLS | TLS_GD))
4871 if ((tls_mask & TLS_GD) != 0)
4872 off += 8;
4873 if (tls_type != (TLS_TLS | TLS_DTPREL))
4875 if ((tls_mask & TLS_DTPREL) != 0)
4876 off += 4;
4882 relocation = htab->got->output_offset + off - 4;
4884 /* Addends on got relocations don't make much sense.
4885 x+off@got is actually x@got+off, and since the got is
4886 generated by a hash table traversal, the value in the
4887 got at entry m+n bears little relation to the entry m. */
4888 if (addend != 0)
4889 (*_bfd_error_handler)
4890 (_("%s(%s+0x%lx): non-zero addend on got reloc against `%s'"),
4891 bfd_archive_filename (input_bfd),
4892 bfd_get_section_name (input_bfd, input_section),
4893 (long) rel->r_offset,
4894 sym_name);
4896 break;
4898 /* Relocations that need no special processing. */
4899 case R_PPC_LOCAL24PC:
4900 /* It makes no sense to point a local relocation
4901 at a symbol not in this object. */
4902 if (h != NULL
4903 && (h->root.type == bfd_link_hash_defined
4904 || h->root.type == bfd_link_hash_defweak)
4905 && sec->output_section == NULL)
4907 if (! (*info->callbacks->undefined_symbol) (info,
4908 h->root.root.string,
4909 input_bfd,
4910 input_section,
4911 rel->r_offset,
4912 TRUE))
4913 return FALSE;
4914 continue;
4916 break;
4918 case R_PPC_DTPREL16:
4919 case R_PPC_DTPREL16_LO:
4920 case R_PPC_DTPREL16_HI:
4921 case R_PPC_DTPREL16_HA:
4922 addend -= htab->tls_sec->vma + DTP_OFFSET;
4923 break;
4925 /* Relocations that may need to be propagated if this is a shared
4926 object. */
4927 case R_PPC_TPREL16:
4928 case R_PPC_TPREL16_LO:
4929 case R_PPC_TPREL16_HI:
4930 case R_PPC_TPREL16_HA:
4931 addend -= htab->tls_sec->vma + TP_OFFSET;
4932 /* The TPREL16 relocs shouldn't really be used in shared
4933 libs as they will result in DT_TEXTREL being set, but
4934 support them anyway. */
4935 goto dodyn;
4937 case R_PPC_TPREL32:
4938 addend -= htab->tls_sec->vma + TP_OFFSET;
4939 goto dodyn;
4941 case R_PPC_DTPREL32:
4942 addend -= htab->tls_sec->vma + DTP_OFFSET;
4943 goto dodyn;
4945 case R_PPC_REL24:
4946 case R_PPC_REL32:
4947 case R_PPC_REL14:
4948 case R_PPC_REL14_BRTAKEN:
4949 case R_PPC_REL14_BRNTAKEN:
4950 /* If these relocations are not to a named symbol, they can be
4951 handled right here, no need to bother the dynamic linker. */
4952 if (h == NULL
4953 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0
4954 || SYMBOL_REFERENCES_LOCAL (info, h))
4955 break;
4956 /* fall through */
4958 /* Relocations that always need to be propagated if this is a shared
4959 object. */
4960 case R_PPC_ADDR32:
4961 case R_PPC_ADDR24:
4962 case R_PPC_ADDR16:
4963 case R_PPC_ADDR16_LO:
4964 case R_PPC_ADDR16_HI:
4965 case R_PPC_ADDR16_HA:
4966 case R_PPC_ADDR14:
4967 case R_PPC_ADDR14_BRTAKEN:
4968 case R_PPC_ADDR14_BRNTAKEN:
4969 case R_PPC_UADDR32:
4970 case R_PPC_UADDR16:
4971 case R_PPC_DTPMOD32:
4972 /* r_symndx will be zero only for relocs against symbols
4973 from removed linkonce sections, or sections discarded by
4974 a linker script. */
4975 dodyn:
4976 if (r_symndx == 0)
4977 break;
4978 /* Fall thru. */
4980 if ((info->shared
4981 && (MUST_BE_DYN_RELOC (r_type)
4982 || (h != NULL
4983 && h->dynindx != -1
4984 && (!info->symbolic
4985 || (h->elf_link_hash_flags
4986 & ELF_LINK_HASH_DEF_REGULAR) == 0))))
4987 || (ELIMINATE_COPY_RELOCS
4988 && !info->shared
4989 && (input_section->flags & SEC_ALLOC) != 0
4990 && h != NULL
4991 && h->dynindx != -1
4992 && (h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0
4993 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
4994 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0))
4996 int skip;
4998 #ifdef DEBUG
4999 fprintf (stderr, "ppc_elf_relocate_section need to create relocation for %s\n",
5000 (h && h->root.root.string
5001 ? h->root.root.string : "<unknown>"));
5002 #endif
5004 /* When generating a shared object, these relocations
5005 are copied into the output file to be resolved at run
5006 time. */
5007 if (sreloc == NULL)
5009 const char *name;
5011 name = (bfd_elf_string_from_elf_section
5012 (input_bfd,
5013 elf_elfheader (input_bfd)->e_shstrndx,
5014 elf_section_data (input_section)->rel_hdr.sh_name));
5015 if (name == NULL)
5016 return FALSE;
5018 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
5019 && strcmp (bfd_get_section_name (input_bfd,
5020 input_section),
5021 name + 5) == 0);
5023 sreloc = bfd_get_section_by_name (htab->elf.dynobj, name);
5024 BFD_ASSERT (sreloc != NULL);
5027 skip = 0;
5029 outrel.r_offset =
5030 _bfd_elf_section_offset (output_bfd, info, input_section,
5031 rel->r_offset);
5032 if (outrel.r_offset == (bfd_vma) -1
5033 || outrel.r_offset == (bfd_vma) -2)
5034 skip = (int) outrel.r_offset;
5035 outrel.r_offset += (input_section->output_section->vma
5036 + input_section->output_offset);
5038 if (skip)
5039 memset (&outrel, 0, sizeof outrel);
5040 /* h->dynindx may be -1 if this symbol was marked to
5041 become local. */
5042 else if (h != NULL
5043 && !SYMBOL_REFERENCES_LOCAL (info, h))
5045 unresolved_reloc = FALSE;
5046 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
5047 outrel.r_addend = rel->r_addend;
5049 else
5051 outrel.r_addend = relocation + rel->r_addend;
5053 if (r_type == R_PPC_ADDR32)
5054 outrel.r_info = ELF32_R_INFO (0, R_PPC_RELATIVE);
5055 else
5057 long indx;
5059 if (bfd_is_abs_section (sec))
5060 indx = 0;
5061 else if (sec == NULL || sec->owner == NULL)
5063 bfd_set_error (bfd_error_bad_value);
5064 return FALSE;
5066 else
5068 asection *osec;
5070 /* We are turning this relocation into one
5071 against a section symbol. It would be
5072 proper to subtract the symbol's value,
5073 osec->vma, from the emitted reloc addend,
5074 but ld.so expects buggy relocs. */
5075 osec = sec->output_section;
5076 indx = elf_section_data (osec)->dynindx;
5077 BFD_ASSERT (indx > 0);
5078 #ifdef DEBUG
5079 if (indx <= 0)
5081 printf ("indx=%d section=%s flags=%08x name=%s\n",
5082 indx, osec->name, osec->flags,
5083 h->root.root.string);
5085 #endif
5088 outrel.r_info = ELF32_R_INFO (indx, r_type);
5092 loc = sreloc->contents;
5093 loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rela);
5094 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
5096 if (skip == -1)
5097 continue;
5099 /* This reloc will be computed at runtime. We clear the memory
5100 so that it contains predictable value. */
5101 if (! skip
5102 && ((input_section->flags & SEC_ALLOC) != 0
5103 || ELF32_R_TYPE (outrel.r_info) != R_PPC_RELATIVE))
5105 relocation = howto->pc_relative ? outrel.r_offset : 0;
5106 addend = 0;
5107 break;
5110 break;
5112 /* Indirect .sdata relocation. */
5113 case R_PPC_EMB_SDAI16:
5114 BFD_ASSERT (htab->sdata != NULL);
5115 relocation
5116 = bfd_elf32_finish_pointer_linker_section (output_bfd, input_bfd,
5117 info, htab->sdata, h,
5118 relocation, rel,
5119 R_PPC_RELATIVE);
5120 break;
5122 /* Indirect .sdata2 relocation. */
5123 case R_PPC_EMB_SDA2I16:
5124 BFD_ASSERT (htab->sdata2 != NULL);
5125 relocation
5126 = bfd_elf32_finish_pointer_linker_section (output_bfd, input_bfd,
5127 info, htab->sdata2, h,
5128 relocation, rel,
5129 R_PPC_RELATIVE);
5130 break;
5132 /* Handle the TOC16 reloc. We want to use the offset within the .got
5133 section, not the actual VMA. This is appropriate when generating
5134 an embedded ELF object, for which the .got section acts like the
5135 AIX .toc section. */
5136 case R_PPC_TOC16: /* phony GOT16 relocations */
5137 BFD_ASSERT (sec != (asection *) 0);
5138 BFD_ASSERT (bfd_is_und_section (sec)
5139 || strcmp (bfd_get_section_name (abfd, sec), ".got") == 0
5140 || strcmp (bfd_get_section_name (abfd, sec), ".cgot") == 0)
5142 addend -= sec->output_section->vma + sec->output_offset + 0x8000;
5143 break;
5145 case R_PPC_PLTREL24:
5146 /* Relocation is to the entry for this symbol in the
5147 procedure linkage table. */
5148 BFD_ASSERT (h != NULL);
5150 if (h->plt.offset == (bfd_vma) -1
5151 || htab->plt == NULL)
5153 /* We didn't make a PLT entry for this symbol. This
5154 happens when statically linking PIC code, or when
5155 using -Bsymbolic. */
5156 break;
5159 unresolved_reloc = FALSE;
5160 relocation = (htab->plt->output_section->vma
5161 + htab->plt->output_offset
5162 + h->plt.offset);
5163 break;
5165 /* Relocate against _SDA_BASE_. */
5166 case R_PPC_SDAREL16:
5168 const char *name;
5169 const struct elf_link_hash_entry *sh;
5171 BFD_ASSERT (sec != (asection *) 0);
5172 name = bfd_get_section_name (abfd, sec->output_section);
5173 if (! ((strncmp (name, ".sdata", 6) == 0
5174 && (name[6] == 0 || name[6] == '.'))
5175 || (strncmp (name, ".sbss", 5) == 0
5176 && (name[5] == 0 || name[5] == '.'))))
5178 (*_bfd_error_handler) (_("%s: The target (%s) of a %s relocation is in the wrong output section (%s)"),
5179 bfd_archive_filename (input_bfd),
5180 sym_name,
5181 howto->name,
5182 name);
5184 sh = htab->sdata->sym_hash;
5185 addend -= (sh->root.u.def.value
5186 + sh->root.u.def.section->output_section->vma
5187 + sh->root.u.def.section->output_offset);
5189 break;
5191 /* Relocate against _SDA2_BASE_. */
5192 case R_PPC_EMB_SDA2REL:
5194 const char *name;
5195 const struct elf_link_hash_entry *sh;
5197 BFD_ASSERT (sec != (asection *) 0);
5198 name = bfd_get_section_name (abfd, sec->output_section);
5199 if (! (strncmp (name, ".sdata2", 7) == 0
5200 || strncmp (name, ".sbss2", 6) == 0))
5202 (*_bfd_error_handler) (_("%s: The target (%s) of a %s relocation is in the wrong output section (%s)"),
5203 bfd_archive_filename (input_bfd),
5204 sym_name,
5205 howto->name,
5206 name);
5208 bfd_set_error (bfd_error_bad_value);
5209 ret = FALSE;
5210 continue;
5212 sh = htab->sdata2->sym_hash;
5213 addend -= (sh->root.u.def.value
5214 + sh->root.u.def.section->output_section->vma
5215 + sh->root.u.def.section->output_offset);
5217 break;
5219 /* Relocate against either _SDA_BASE_, _SDA2_BASE_, or 0. */
5220 case R_PPC_EMB_SDA21:
5221 case R_PPC_EMB_RELSDA:
5223 const char *name;
5224 const struct elf_link_hash_entry *sh;
5225 int reg;
5227 BFD_ASSERT (sec != (asection *) 0);
5228 name = bfd_get_section_name (abfd, sec->output_section);
5229 if (((strncmp (name, ".sdata", 6) == 0
5230 && (name[6] == 0 || name[6] == '.'))
5231 || (strncmp (name, ".sbss", 5) == 0
5232 && (name[5] == 0 || name[5] == '.'))))
5234 reg = 13;
5235 sh = htab->sdata->sym_hash;
5236 addend -= (sh->root.u.def.value
5237 + sh->root.u.def.section->output_section->vma
5238 + sh->root.u.def.section->output_offset);
5241 else if (strncmp (name, ".sdata2", 7) == 0
5242 || strncmp (name, ".sbss2", 6) == 0)
5244 reg = 2;
5245 sh = htab->sdata2->sym_hash;
5246 addend -= (sh->root.u.def.value
5247 + sh->root.u.def.section->output_section->vma
5248 + sh->root.u.def.section->output_offset);
5251 else if (strcmp (name, ".PPC.EMB.sdata0") == 0
5252 || strcmp (name, ".PPC.EMB.sbss0") == 0)
5254 reg = 0;
5257 else
5259 (*_bfd_error_handler) (_("%s: The target (%s) of a %s relocation is in the wrong output section (%s)"),
5260 bfd_archive_filename (input_bfd),
5261 sym_name,
5262 howto->name,
5263 name);
5265 bfd_set_error (bfd_error_bad_value);
5266 ret = FALSE;
5267 continue;
5270 if (r_type == R_PPC_EMB_SDA21)
5271 { /* fill in register field */
5272 insn = bfd_get_32 (output_bfd, contents + rel->r_offset);
5273 insn = (insn & ~RA_REGISTER_MASK) | (reg << RA_REGISTER_SHIFT);
5274 bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
5277 break;
5279 /* Relocate against the beginning of the section. */
5280 case R_PPC_SECTOFF:
5281 case R_PPC_SECTOFF_LO:
5282 case R_PPC_SECTOFF_HI:
5283 case R_PPC_SECTOFF_HA:
5284 BFD_ASSERT (sec != (asection *) 0);
5285 addend -= sec->output_section->vma;
5286 break;
5288 /* Negative relocations. */
5289 case R_PPC_EMB_NADDR32:
5290 case R_PPC_EMB_NADDR16:
5291 case R_PPC_EMB_NADDR16_LO:
5292 case R_PPC_EMB_NADDR16_HI:
5293 case R_PPC_EMB_NADDR16_HA:
5294 addend -= 2 * relocation;
5295 break;
5297 case R_PPC_COPY:
5298 case R_PPC_GLOB_DAT:
5299 case R_PPC_JMP_SLOT:
5300 case R_PPC_RELATIVE:
5301 case R_PPC_PLT32:
5302 case R_PPC_PLTREL32:
5303 case R_PPC_PLT16_LO:
5304 case R_PPC_PLT16_HI:
5305 case R_PPC_PLT16_HA:
5306 case R_PPC_ADDR30:
5307 case R_PPC_EMB_RELSEC16:
5308 case R_PPC_EMB_RELST_LO:
5309 case R_PPC_EMB_RELST_HI:
5310 case R_PPC_EMB_RELST_HA:
5311 case R_PPC_EMB_BIT_FLD:
5312 (*_bfd_error_handler)
5313 (_("%s: Relocation %s is not yet supported for symbol %s."),
5314 bfd_archive_filename (input_bfd),
5315 howto->name,
5316 sym_name);
5318 bfd_set_error (bfd_error_invalid_operation);
5319 ret = FALSE;
5320 continue;
5323 /* Do any further special processing. */
5324 switch (r_type)
5326 default:
5327 break;
5329 case R_PPC_ADDR16_HA:
5330 case R_PPC_GOT16_HA:
5331 case R_PPC_PLT16_HA:
5332 case R_PPC_SECTOFF_HA:
5333 case R_PPC_TPREL16_HA:
5334 case R_PPC_DTPREL16_HA:
5335 case R_PPC_GOT_TLSGD16_HA:
5336 case R_PPC_GOT_TLSLD16_HA:
5337 case R_PPC_GOT_TPREL16_HA:
5338 case R_PPC_GOT_DTPREL16_HA:
5339 case R_PPC_EMB_NADDR16_HA:
5340 case R_PPC_EMB_RELST_HA:
5341 /* It's just possible that this symbol is a weak symbol
5342 that's not actually defined anywhere. In that case,
5343 'sec' would be NULL, and we should leave the symbol
5344 alone (it will be set to zero elsewhere in the link). */
5345 if (sec != NULL)
5346 /* Add 0x10000 if sign bit in 0:15 is set. */
5347 addend += ((relocation + addend) & 0x8000) << 1;
5348 break;
5351 #ifdef DEBUG
5352 fprintf (stderr, "\ttype = %s (%d), name = %s, symbol index = %ld, offset = %ld, addend = %ld\n",
5353 howto->name,
5354 (int) r_type,
5355 sym_name,
5356 r_symndx,
5357 (long) rel->r_offset,
5358 (long) addend);
5359 #endif
5361 if (unresolved_reloc
5362 && !((input_section->flags & SEC_DEBUGGING) != 0
5363 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0))
5365 (*_bfd_error_handler)
5366 (_("%s(%s+0x%lx): unresolvable relocation against symbol `%s'"),
5367 bfd_archive_filename (input_bfd),
5368 bfd_get_section_name (input_bfd, input_section),
5369 (long) rel->r_offset,
5370 sym_name);
5371 ret = FALSE;
5374 r = _bfd_final_link_relocate (howto,
5375 input_bfd,
5376 input_section,
5377 contents,
5378 rel->r_offset,
5379 relocation,
5380 addend);
5382 if (r != bfd_reloc_ok)
5384 if (sym_name == NULL)
5385 sym_name = "(null)";
5386 if (r == bfd_reloc_overflow)
5388 if (warned)
5389 continue;
5390 if (h != NULL
5391 && h->root.type == bfd_link_hash_undefweak
5392 && howto->pc_relative)
5394 /* Assume this is a call protected by other code that
5395 detect the symbol is undefined. If this is the case,
5396 we can safely ignore the overflow. If not, the
5397 program is hosed anyway, and a little warning isn't
5398 going to help. */
5400 continue;
5403 if (! (*info->callbacks->reloc_overflow) (info,
5404 sym_name,
5405 howto->name,
5406 rel->r_addend,
5407 input_bfd,
5408 input_section,
5409 rel->r_offset))
5410 return FALSE;
5412 else
5414 (*_bfd_error_handler)
5415 (_("%s(%s+0x%lx): reloc against `%s': error %d"),
5416 bfd_archive_filename (input_bfd),
5417 bfd_get_section_name (input_bfd, input_section),
5418 (long) rel->r_offset, sym_name, (int) r);
5419 ret = FALSE;
5424 #ifdef DEBUG
5425 fprintf (stderr, "\n");
5426 #endif
5428 return ret;
5431 static enum elf_reloc_type_class
5432 ppc_elf_reloc_type_class (rela)
5433 const Elf_Internal_Rela *rela;
5435 switch ((int) ELF32_R_TYPE (rela->r_info))
5437 case R_PPC_RELATIVE:
5438 return reloc_class_relative;
5439 case R_PPC_REL24:
5440 case R_PPC_ADDR24:
5441 case R_PPC_JMP_SLOT:
5442 return reloc_class_plt;
5443 case R_PPC_COPY:
5444 return reloc_class_copy;
5445 default:
5446 return reloc_class_normal;
5450 /* Support for core dump NOTE sections. */
5452 static bfd_boolean
5453 ppc_elf_grok_prstatus (abfd, note)
5454 bfd *abfd;
5455 Elf_Internal_Note *note;
5457 int offset;
5458 unsigned int raw_size;
5460 switch (note->descsz)
5462 default:
5463 return FALSE;
5465 case 268: /* Linux/PPC. */
5466 /* pr_cursig */
5467 elf_tdata (abfd)->core_signal = bfd_get_16 (abfd, note->descdata + 12);
5469 /* pr_pid */
5470 elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, note->descdata + 24);
5472 /* pr_reg */
5473 offset = 72;
5474 raw_size = 192;
5476 break;
5479 /* Make a ".reg/999" section. */
5480 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
5481 raw_size, note->descpos + offset);
5484 static bfd_boolean
5485 ppc_elf_grok_psinfo (abfd, note)
5486 bfd *abfd;
5487 Elf_Internal_Note *note;
5489 switch (note->descsz)
5491 default:
5492 return FALSE;
5494 case 128: /* Linux/PPC elf_prpsinfo. */
5495 elf_tdata (abfd)->core_program
5496 = _bfd_elfcore_strndup (abfd, note->descdata + 32, 16);
5497 elf_tdata (abfd)->core_command
5498 = _bfd_elfcore_strndup (abfd, note->descdata + 48, 80);
5501 /* Note that for some reason, a spurious space is tacked
5502 onto the end of the args in some (at least one anyway)
5503 implementations, so strip it off if it exists. */
5506 char *command = elf_tdata (abfd)->core_command;
5507 int n = strlen (command);
5509 if (0 < n && command[n - 1] == ' ')
5510 command[n - 1] = '\0';
5513 return TRUE;
5516 /* Very simple linked list structure for recording apuinfo values. */
5517 typedef struct apuinfo_list
5519 struct apuinfo_list *next;
5520 unsigned long value;
5522 apuinfo_list;
5524 static apuinfo_list * head;
5526 static void apuinfo_list_init PARAMS ((void));
5527 static void apuinfo_list_add PARAMS ((unsigned long));
5528 static unsigned apuinfo_list_length PARAMS ((void));
5529 static unsigned long apuinfo_list_element PARAMS ((unsigned long));
5530 static void apuinfo_list_finish PARAMS ((void));
5532 extern void ppc_elf_begin_write_processing
5533 PARAMS ((bfd *, struct bfd_link_info *));
5534 extern void ppc_elf_final_write_processing
5535 PARAMS ((bfd *, bfd_boolean));
5536 extern bfd_boolean ppc_elf_write_section
5537 PARAMS ((bfd *, asection *, bfd_byte *));
5540 static void
5541 apuinfo_list_init PARAMS ((void))
5543 head = NULL;
5546 static void
5547 apuinfo_list_add (value)
5548 unsigned long value;
5550 apuinfo_list *entry = head;
5552 while (entry != NULL)
5554 if (entry->value == value)
5555 return;
5556 entry = entry->next;
5559 entry = bfd_malloc (sizeof (* entry));
5560 if (entry == NULL)
5561 return;
5563 entry->value = value;
5564 entry->next = head;
5565 head = entry;
5568 static unsigned
5569 apuinfo_list_length PARAMS ((void))
5571 apuinfo_list *entry;
5572 unsigned long count;
5574 for (entry = head, count = 0;
5575 entry;
5576 entry = entry->next)
5577 ++ count;
5579 return count;
5582 static inline unsigned long
5583 apuinfo_list_element (number)
5584 unsigned long number;
5586 apuinfo_list * entry;
5588 for (entry = head;
5589 entry && number --;
5590 entry = entry->next)
5593 return entry ? entry->value : 0;
5596 static void
5597 apuinfo_list_finish PARAMS ((void))
5599 apuinfo_list *entry;
5601 for (entry = head; entry;)
5603 apuinfo_list *next = entry->next;
5604 free (entry);
5605 entry = next;
5608 head = NULL;
5611 #define APUINFO_SECTION_NAME ".PPC.EMB.apuinfo"
5612 #define APUINFO_LABEL "APUinfo"
5614 /* Scan the input BFDs and create a linked list of
5615 the APUinfo values that will need to be emitted. */
5617 void
5618 ppc_elf_begin_write_processing (abfd, link_info)
5619 bfd *abfd;
5620 struct bfd_link_info *link_info;
5622 bfd *ibfd;
5623 asection *asec;
5624 char *buffer;
5625 unsigned num_input_sections;
5626 bfd_size_type output_section_size;
5627 unsigned i;
5628 unsigned num_entries;
5629 unsigned long offset;
5630 unsigned long length;
5631 const char *error_message = NULL;
5633 if (link_info == NULL)
5634 return;
5636 /* Scan the input bfds, looking for apuinfo sections. */
5637 num_input_sections = 0;
5638 output_section_size = 0;
5640 for (ibfd = link_info->input_bfds; ibfd; ibfd = ibfd->link_next)
5642 asec = bfd_get_section_by_name (ibfd, APUINFO_SECTION_NAME);
5643 if (asec)
5645 ++ num_input_sections;
5646 output_section_size += asec->_raw_size;
5650 /* We need at least one input sections
5651 in order to make merging worthwhile. */
5652 if (num_input_sections < 1)
5653 return;
5655 /* Just make sure that the output section exists as well. */
5656 asec = bfd_get_section_by_name (abfd, APUINFO_SECTION_NAME);
5657 if (asec == NULL)
5658 return;
5660 /* Allocate a buffer for the contents of the input sections. */
5661 buffer = bfd_malloc (output_section_size);
5662 if (buffer == NULL)
5663 return;
5665 offset = 0;
5666 apuinfo_list_init ();
5668 /* Read in the input sections contents. */
5669 for (ibfd = link_info->input_bfds; ibfd; ibfd = ibfd->link_next)
5671 unsigned long datum;
5672 char *ptr;
5674 asec = bfd_get_section_by_name (ibfd, APUINFO_SECTION_NAME);
5675 if (asec == NULL)
5676 continue;
5678 length = asec->_raw_size;
5679 if (length < 24)
5681 error_message = _("corrupt or empty %s section in %s");
5682 goto fail;
5685 if (bfd_seek (ibfd, asec->filepos, SEEK_SET) != 0
5686 || (bfd_bread (buffer + offset, length, ibfd) != length))
5688 error_message = _("unable to read in %s section from %s");
5689 goto fail;
5692 /* Process the contents of the section. */
5693 ptr = buffer + offset;
5694 error_message = _("corrupt %s section in %s");
5696 /* Verify the contents of the header. Note - we have to
5697 extract the values this way in order to allow for a
5698 host whose endian-ness is different from the target. */
5699 datum = bfd_get_32 (ibfd, ptr);
5700 if (datum != sizeof APUINFO_LABEL)
5701 goto fail;
5703 datum = bfd_get_32 (ibfd, ptr + 8);
5704 if (datum != 0x2)
5705 goto fail;
5707 if (strcmp (ptr + 12, APUINFO_LABEL) != 0)
5708 goto fail;
5710 /* Get the number of apuinfo entries. */
5711 datum = bfd_get_32 (ibfd, ptr + 4);
5712 if ((datum * 4 + 20) != length)
5713 goto fail;
5715 /* Make sure that we do not run off the end of the section. */
5716 if (offset + length > output_section_size)
5717 goto fail;
5719 /* Scan the apuinfo section, building a list of apuinfo numbers. */
5720 for (i = 0; i < datum; i++)
5721 apuinfo_list_add (bfd_get_32 (ibfd, ptr + 20 + (i * 4)));
5723 /* Update the offset. */
5724 offset += length;
5727 error_message = NULL;
5729 /* Compute the size of the output section. */
5730 num_entries = apuinfo_list_length ();
5731 output_section_size = 20 + num_entries * 4;
5733 asec = bfd_get_section_by_name (abfd, APUINFO_SECTION_NAME);
5735 if (! bfd_set_section_size (abfd, asec, output_section_size))
5736 ibfd = abfd,
5737 error_message = _("warning: unable to set size of %s section in %s");
5739 fail:
5740 free (buffer);
5742 if (error_message)
5743 (*_bfd_error_handler) (error_message, APUINFO_SECTION_NAME,
5744 bfd_archive_filename (ibfd));
5748 /* Prevent the output section from accumulating the input sections'
5749 contents. We have already stored this in our linked list structure. */
5751 bfd_boolean
5752 ppc_elf_write_section (abfd, asec, contents)
5753 bfd *abfd ATTRIBUTE_UNUSED;
5754 asection *asec;
5755 bfd_byte *contents ATTRIBUTE_UNUSED;
5757 return (apuinfo_list_length ()
5758 && strcmp (asec->name, APUINFO_SECTION_NAME) == 0);
5762 /* Finally we can generate the output section. */
5764 void
5765 ppc_elf_final_write_processing (abfd, linker)
5766 bfd *abfd;
5767 bfd_boolean linker ATTRIBUTE_UNUSED;
5769 bfd_byte *buffer;
5770 asection *asec;
5771 unsigned i;
5772 unsigned num_entries;
5773 bfd_size_type length;
5775 asec = bfd_get_section_by_name (abfd, APUINFO_SECTION_NAME);
5776 if (asec == NULL)
5777 return;
5779 if (apuinfo_list_length () == 0)
5780 return;
5782 length = asec->_raw_size;
5783 if (length < 20)
5784 return;
5786 buffer = bfd_malloc (length);
5787 if (buffer == NULL)
5789 (*_bfd_error_handler)
5790 (_("failed to allocate space for new APUinfo section."));
5791 return;
5794 /* Create the apuinfo header. */
5795 num_entries = apuinfo_list_length ();
5796 bfd_put_32 (abfd, sizeof APUINFO_LABEL, buffer);
5797 bfd_put_32 (abfd, num_entries, buffer + 4);
5798 bfd_put_32 (abfd, 0x2, buffer + 8);
5799 strcpy (buffer + 12, APUINFO_LABEL);
5801 length = 20;
5802 for (i = 0; i < num_entries; i++)
5804 bfd_put_32 (abfd, apuinfo_list_element (i), buffer + length);
5805 length += 4;
5808 if (length != asec->_raw_size)
5809 (*_bfd_error_handler) (_("failed to compute new APUinfo section."));
5811 if (! bfd_set_section_contents (abfd, asec, buffer, (file_ptr) 0, length))
5812 (*_bfd_error_handler) (_("failed to install new APUinfo section."));
5814 free (buffer);
5816 apuinfo_list_finish ();
5819 #define TARGET_LITTLE_SYM bfd_elf32_powerpcle_vec
5820 #define TARGET_LITTLE_NAME "elf32-powerpcle"
5821 #define TARGET_BIG_SYM bfd_elf32_powerpc_vec
5822 #define TARGET_BIG_NAME "elf32-powerpc"
5823 #define ELF_ARCH bfd_arch_powerpc
5824 #define ELF_MACHINE_CODE EM_PPC
5825 #define ELF_MAXPAGESIZE 0x10000
5826 #define elf_info_to_howto ppc_elf_info_to_howto
5828 #ifdef EM_CYGNUS_POWERPC
5829 #define ELF_MACHINE_ALT1 EM_CYGNUS_POWERPC
5830 #endif
5832 #ifdef EM_PPC_OLD
5833 #define ELF_MACHINE_ALT2 EM_PPC_OLD
5834 #endif
5836 #define elf_backend_plt_not_loaded 1
5837 #define elf_backend_got_symbol_offset 4
5838 #define elf_backend_can_gc_sections 1
5839 #define elf_backend_can_refcount 1
5840 #define elf_backend_got_header_size 12
5841 #define elf_backend_plt_header_size PLT_INITIAL_ENTRY_SIZE
5842 #define elf_backend_rela_normal 1
5844 #define bfd_elf32_bfd_merge_private_bfd_data ppc_elf_merge_private_bfd_data
5845 #define bfd_elf32_bfd_relax_section ppc_elf_relax_section
5846 #define bfd_elf32_bfd_reloc_type_lookup ppc_elf_reloc_type_lookup
5847 #define bfd_elf32_bfd_set_private_flags ppc_elf_set_private_flags
5848 #define bfd_elf32_bfd_link_hash_table_create ppc_elf_link_hash_table_create
5850 #define elf_backend_object_p ppc_elf_object_p
5851 #define elf_backend_gc_mark_hook ppc_elf_gc_mark_hook
5852 #define elf_backend_gc_sweep_hook ppc_elf_gc_sweep_hook
5853 #define elf_backend_section_from_shdr ppc_elf_section_from_shdr
5854 #define elf_backend_relocate_section ppc_elf_relocate_section
5855 #define elf_backend_create_dynamic_sections ppc_elf_create_dynamic_sections
5856 #define elf_backend_check_relocs ppc_elf_check_relocs
5857 #define elf_backend_copy_indirect_symbol ppc_elf_copy_indirect_symbol
5858 #define elf_backend_adjust_dynamic_symbol ppc_elf_adjust_dynamic_symbol
5859 #define elf_backend_add_symbol_hook ppc_elf_add_symbol_hook
5860 #define elf_backend_size_dynamic_sections ppc_elf_size_dynamic_sections
5861 #define elf_backend_finish_dynamic_symbol ppc_elf_finish_dynamic_symbol
5862 #define elf_backend_finish_dynamic_sections ppc_elf_finish_dynamic_sections
5863 #define elf_backend_fake_sections ppc_elf_fake_sections
5864 #define elf_backend_additional_program_headers ppc_elf_additional_program_headers
5865 #define elf_backend_modify_segment_map ppc_elf_modify_segment_map
5866 #define elf_backend_grok_prstatus ppc_elf_grok_prstatus
5867 #define elf_backend_grok_psinfo ppc_elf_grok_psinfo
5868 #define elf_backend_reloc_type_class ppc_elf_reloc_type_class
5869 #define elf_backend_begin_write_processing ppc_elf_begin_write_processing
5870 #define elf_backend_final_write_processing ppc_elf_final_write_processing
5871 #define elf_backend_write_section ppc_elf_write_section
5873 #include "elf32-target.h"