bfd/
[binutils.git] / bfd / elf32-ppc.c
blobacd9d841b7a4405f276f6c6af904224fba2765e6
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"
34 /* RELA relocations are used here. */
36 static struct bfd_hash_entry *ppc_elf_link_hash_newfunc
37 PARAMS ((struct bfd_hash_entry *entry, struct bfd_hash_table *table,
38 const char *string));
39 static struct bfd_link_hash_table *ppc_elf_link_hash_table_create
40 PARAMS ((bfd *abfd));
41 static void ppc_elf_copy_indirect_symbol
42 PARAMS ((struct elf_backend_data *bed, struct elf_link_hash_entry *dir,
43 struct elf_link_hash_entry *ind));
44 static reloc_howto_type *ppc_elf_reloc_type_lookup
45 PARAMS ((bfd *abfd, bfd_reloc_code_real_type code));
46 static void ppc_elf_info_to_howto
47 PARAMS ((bfd *abfd, arelent *cache_ptr, Elf_Internal_Rela *dst));
48 static void ppc_elf_howto_init
49 PARAMS ((void));
50 static int ppc_elf_sort_rela
51 PARAMS ((const PTR, const PTR));
52 static bfd_boolean ppc_elf_relax_section
53 PARAMS ((bfd *, asection *, struct bfd_link_info *, bfd_boolean *));
54 static bfd_reloc_status_type ppc_elf_addr16_ha_reloc
55 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
56 static bfd_boolean ppc_elf_object_p
57 PARAMS ((bfd *));
58 static bfd_boolean ppc_elf_set_private_flags
59 PARAMS ((bfd *, flagword));
60 static bfd_boolean ppc_elf_merge_private_bfd_data
61 PARAMS ((bfd *, bfd *));
62 static int ppc_elf_additional_program_headers
63 PARAMS ((bfd *));
64 static bfd_boolean ppc_elf_modify_segment_map
65 PARAMS ((bfd *));
66 static asection *ppc_elf_create_got
67 PARAMS ((bfd *, struct bfd_link_info *));
68 static bfd_boolean ppc_elf_create_dynamic_sections
69 PARAMS ((bfd *, struct bfd_link_info *));
70 static bfd_boolean ppc_elf_section_from_shdr
71 PARAMS ((bfd *, Elf_Internal_Shdr *, const char *));
72 static bfd_boolean ppc_elf_fake_sections
73 PARAMS ((bfd *, Elf_Internal_Shdr *, asection *));
74 static elf_linker_section_t *ppc_elf_create_linker_section
75 PARAMS ((bfd *abfd, struct bfd_link_info *info,
76 enum elf_linker_section_enum));
77 static bfd_boolean ppc_elf_check_relocs
78 PARAMS ((bfd *, struct bfd_link_info *, asection *,
79 const Elf_Internal_Rela *));
80 static asection * ppc_elf_gc_mark_hook
81 PARAMS ((asection *sec, struct bfd_link_info *info, Elf_Internal_Rela *rel,
82 struct elf_link_hash_entry *h, Elf_Internal_Sym *sym));
83 static bfd_boolean ppc_elf_gc_sweep_hook
84 PARAMS ((bfd *abfd, struct bfd_link_info *info, asection *sec,
85 const Elf_Internal_Rela *relocs));
86 static bfd_boolean ppc_elf_adjust_dynamic_symbol
87 PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *));
88 static bfd_boolean allocate_dynrelocs
89 PARAMS ((struct elf_link_hash_entry *, PTR));
90 static bfd_boolean readonly_dynrelocs
91 PARAMS ((struct elf_link_hash_entry *, PTR));
92 static bfd_boolean ppc_elf_size_dynamic_sections
93 PARAMS ((bfd *, struct bfd_link_info *));
94 static bfd_boolean ppc_elf_relocate_section
95 PARAMS ((bfd *, struct bfd_link_info *info, bfd *, asection *, bfd_byte *,
96 Elf_Internal_Rela *relocs, Elf_Internal_Sym *local_syms,
97 asection **));
98 static bfd_boolean ppc_elf_add_symbol_hook
99 PARAMS ((bfd *, struct bfd_link_info *, const Elf_Internal_Sym *,
100 const char **, flagword *, asection **, bfd_vma *));
101 static bfd_boolean ppc_elf_finish_dynamic_symbol
102 PARAMS ((bfd *, struct bfd_link_info *, struct elf_link_hash_entry *,
103 Elf_Internal_Sym *));
104 static bfd_boolean ppc_elf_finish_dynamic_sections
105 PARAMS ((bfd *, struct bfd_link_info *));
106 static enum elf_reloc_type_class ppc_elf_reloc_type_class
107 PARAMS ((const Elf_Internal_Rela *));
108 static bfd_boolean ppc_elf_grok_prstatus
109 PARAMS ((bfd *abfd, Elf_Internal_Note *note));
110 static bfd_boolean ppc_elf_grok_psinfo
111 PARAMS ((bfd *abfd, Elf_Internal_Note *note));
113 #define BRANCH_PREDICT_BIT 0x200000 /* branch prediction bit for branch taken relocs */
114 #define RA_REGISTER_MASK 0x001f0000 /* mask to set RA in memory instructions */
115 #define RA_REGISTER_SHIFT 16 /* value to shift register by to insert RA */
117 /* The name of the dynamic interpreter. This is put in the .interp
118 section. */
120 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
122 /* The size in bytes of an entry in the procedure linkage table. */
123 #define PLT_ENTRY_SIZE 12
124 /* The initial size of the plt reserved for the dynamic linker. */
125 #define PLT_INITIAL_ENTRY_SIZE 72
126 /* The size of the gap between entries in the PLT. */
127 #define PLT_SLOT_SIZE 8
128 /* The number of single-slot PLT entries (the rest use two slots). */
129 #define PLT_NUM_SINGLE_ENTRIES 8192
131 /* Will references to this symbol always reference the symbol
132 in this object? */
133 #define SYMBOL_REFERENCES_LOCAL(INFO, H) \
134 ((! INFO->shared \
135 || INFO->symbolic \
136 || H->dynindx == -1 \
137 || ELF_ST_VISIBILITY (H->other) == STV_INTERNAL \
138 || ELF_ST_VISIBILITY (H->other) == STV_HIDDEN) \
139 && (H->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0)
141 /* Will _calls_ to this symbol always call the version in this object? */
142 #define SYMBOL_CALLS_LOCAL(INFO, H) \
143 ((! INFO->shared \
144 || INFO->symbolic \
145 || H->dynindx == -1 \
146 || ELF_ST_VISIBILITY (H->other) != STV_DEFAULT) \
147 && (H->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0)
149 /* The PPC linker needs to keep track of the number of relocs that it
150 decides to copy as dynamic relocs in check_relocs for each symbol.
151 This is so that it can later discard them if they are found to be
152 unnecessary. We store the information in a field extending the
153 regular ELF linker hash table. */
155 struct ppc_elf_dyn_relocs
157 struct ppc_elf_dyn_relocs *next;
159 /* The input section of the reloc. */
160 asection *sec;
162 /* Total number of relocs copied for the input section. */
163 bfd_size_type count;
166 /* PPC ELF linker hash entry. */
168 struct ppc_elf_link_hash_entry
170 struct elf_link_hash_entry root;
172 /* Track dynamic relocs copied for this symbol. */
173 struct ppc_elf_dyn_relocs *dyn_relocs;
176 #define ppc_elf_hash_entry(ent) ((struct ppc_elf_link_hash_entry *) (ent))
178 /* PPC ELF linker hash table. */
180 struct ppc_elf_link_hash_table
182 struct elf_link_hash_table root;
184 /* Small local sym to section mapping cache. */
185 struct sym_sec_cache sym_sec;
188 /* Get the PPC ELF linker hash table from a link_info structure. */
190 #define ppc_elf_hash_table(p) \
191 ((struct ppc_elf_link_hash_table *) (p)->hash)
193 /* Create an entry in a PPC ELF linker hash table. */
195 static struct bfd_hash_entry *
196 ppc_elf_link_hash_newfunc (entry, table, string)
197 struct bfd_hash_entry *entry;
198 struct bfd_hash_table *table;
199 const char *string;
201 /* Allocate the structure if it has not already been allocated by a
202 subclass. */
203 if (entry == NULL)
205 entry = bfd_hash_allocate (table,
206 sizeof (struct ppc_elf_link_hash_entry));
207 if (entry == NULL)
208 return entry;
211 /* Call the allocation method of the superclass. */
212 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
213 if (entry != NULL)
214 ppc_elf_hash_entry (entry)->dyn_relocs = NULL;
216 return entry;
219 /* Create a PPC ELF linker hash table. */
221 static struct bfd_link_hash_table *
222 ppc_elf_link_hash_table_create (abfd)
223 bfd *abfd;
225 struct ppc_elf_link_hash_table *ret;
227 ret = ((struct ppc_elf_link_hash_table *)
228 bfd_malloc (sizeof (struct ppc_elf_link_hash_table)));
229 if (ret == NULL)
230 return NULL;
232 if (! _bfd_elf_link_hash_table_init (&ret->root, abfd,
233 ppc_elf_link_hash_newfunc))
235 free (ret);
236 return NULL;
239 ret->sym_sec.abfd = NULL;
241 return &ret->root.root;
244 /* Copy the extra info we tack onto an elf_link_hash_entry. */
246 static void
247 ppc_elf_copy_indirect_symbol (bed, dir, ind)
248 struct elf_backend_data *bed;
249 struct elf_link_hash_entry *dir, *ind;
251 struct ppc_elf_link_hash_entry *edir, *eind;
253 edir = (struct ppc_elf_link_hash_entry *) dir;
254 eind = (struct ppc_elf_link_hash_entry *) ind;
256 if (eind->dyn_relocs != NULL)
258 if (edir->dyn_relocs != NULL)
260 struct ppc_elf_dyn_relocs **pp;
261 struct ppc_elf_dyn_relocs *p;
263 if (ind->root.type == bfd_link_hash_indirect)
264 abort ();
266 /* Add reloc counts against the weak sym to the strong sym
267 list. Merge any entries against the same section. */
268 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
270 struct ppc_elf_dyn_relocs *q;
272 for (q = edir->dyn_relocs; q != NULL; q = q->next)
273 if (q->sec == p->sec)
275 q->count += p->count;
276 *pp = p->next;
277 break;
279 if (q == NULL)
280 pp = &p->next;
282 *pp = edir->dyn_relocs;
285 edir->dyn_relocs = eind->dyn_relocs;
286 eind->dyn_relocs = NULL;
289 _bfd_elf_link_hash_copy_indirect (bed, dir, ind);
292 static reloc_howto_type *ppc_elf_howto_table[(int) R_PPC_max];
294 static reloc_howto_type ppc_elf_howto_raw[] = {
295 /* This reloc does nothing. */
296 HOWTO (R_PPC_NONE, /* type */
297 0, /* rightshift */
298 2, /* size (0 = byte, 1 = short, 2 = long) */
299 32, /* bitsize */
300 FALSE, /* pc_relative */
301 0, /* bitpos */
302 complain_overflow_bitfield, /* complain_on_overflow */
303 bfd_elf_generic_reloc, /* special_function */
304 "R_PPC_NONE", /* name */
305 FALSE, /* partial_inplace */
306 0, /* src_mask */
307 0, /* dst_mask */
308 FALSE), /* pcrel_offset */
310 /* A standard 32 bit relocation. */
311 HOWTO (R_PPC_ADDR32, /* type */
312 0, /* rightshift */
313 2, /* size (0 = byte, 1 = short, 2 = long) */
314 32, /* bitsize */
315 FALSE, /* pc_relative */
316 0, /* bitpos */
317 complain_overflow_bitfield, /* complain_on_overflow */
318 bfd_elf_generic_reloc, /* special_function */
319 "R_PPC_ADDR32", /* name */
320 FALSE, /* partial_inplace */
321 0, /* src_mask */
322 0xffffffff, /* dst_mask */
323 FALSE), /* pcrel_offset */
325 /* An absolute 26 bit branch; the lower two bits must be zero.
326 FIXME: we don't check that, we just clear them. */
327 HOWTO (R_PPC_ADDR24, /* type */
328 0, /* rightshift */
329 2, /* size (0 = byte, 1 = short, 2 = long) */
330 26, /* bitsize */
331 FALSE, /* pc_relative */
332 0, /* bitpos */
333 complain_overflow_bitfield, /* complain_on_overflow */
334 bfd_elf_generic_reloc, /* special_function */
335 "R_PPC_ADDR24", /* name */
336 FALSE, /* partial_inplace */
337 0, /* src_mask */
338 0x3fffffc, /* dst_mask */
339 FALSE), /* pcrel_offset */
341 /* A standard 16 bit relocation. */
342 HOWTO (R_PPC_ADDR16, /* type */
343 0, /* rightshift */
344 1, /* size (0 = byte, 1 = short, 2 = long) */
345 16, /* bitsize */
346 FALSE, /* pc_relative */
347 0, /* bitpos */
348 complain_overflow_bitfield, /* complain_on_overflow */
349 bfd_elf_generic_reloc, /* special_function */
350 "R_PPC_ADDR16", /* name */
351 FALSE, /* partial_inplace */
352 0, /* src_mask */
353 0xffff, /* dst_mask */
354 FALSE), /* pcrel_offset */
356 /* A 16 bit relocation without overflow. */
357 HOWTO (R_PPC_ADDR16_LO, /* type */
358 0, /* rightshift */
359 1, /* size (0 = byte, 1 = short, 2 = long) */
360 16, /* bitsize */
361 FALSE, /* pc_relative */
362 0, /* bitpos */
363 complain_overflow_dont,/* complain_on_overflow */
364 bfd_elf_generic_reloc, /* special_function */
365 "R_PPC_ADDR16_LO", /* name */
366 FALSE, /* partial_inplace */
367 0, /* src_mask */
368 0xffff, /* dst_mask */
369 FALSE), /* pcrel_offset */
371 /* The high order 16 bits of an address. */
372 HOWTO (R_PPC_ADDR16_HI, /* type */
373 16, /* rightshift */
374 1, /* size (0 = byte, 1 = short, 2 = long) */
375 16, /* bitsize */
376 FALSE, /* pc_relative */
377 0, /* bitpos */
378 complain_overflow_dont, /* complain_on_overflow */
379 bfd_elf_generic_reloc, /* special_function */
380 "R_PPC_ADDR16_HI", /* name */
381 FALSE, /* partial_inplace */
382 0, /* src_mask */
383 0xffff, /* dst_mask */
384 FALSE), /* pcrel_offset */
386 /* The high order 16 bits of an address, plus 1 if the contents of
387 the low 16 bits, treated as a signed number, is negative. */
388 HOWTO (R_PPC_ADDR16_HA, /* type */
389 16, /* rightshift */
390 1, /* size (0 = byte, 1 = short, 2 = long) */
391 16, /* bitsize */
392 FALSE, /* pc_relative */
393 0, /* bitpos */
394 complain_overflow_dont, /* complain_on_overflow */
395 ppc_elf_addr16_ha_reloc, /* special_function */
396 "R_PPC_ADDR16_HA", /* name */
397 FALSE, /* partial_inplace */
398 0, /* src_mask */
399 0xffff, /* dst_mask */
400 FALSE), /* pcrel_offset */
402 /* An absolute 16 bit branch; the lower two bits must be zero.
403 FIXME: we don't check that, we just clear them. */
404 HOWTO (R_PPC_ADDR14, /* type */
405 0, /* rightshift */
406 2, /* size (0 = byte, 1 = short, 2 = long) */
407 16, /* 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_ADDR14", /* name */
413 FALSE, /* partial_inplace */
414 0, /* src_mask */
415 0xfffc, /* dst_mask */
416 FALSE), /* pcrel_offset */
418 /* An absolute 16 bit branch, for which bit 10 should be set to
419 indicate that the branch is expected to be taken. The lower two
420 bits must be zero. */
421 HOWTO (R_PPC_ADDR14_BRTAKEN, /* type */
422 0, /* rightshift */
423 2, /* size (0 = byte, 1 = short, 2 = long) */
424 16, /* bitsize */
425 FALSE, /* pc_relative */
426 0, /* bitpos */
427 complain_overflow_bitfield, /* complain_on_overflow */
428 bfd_elf_generic_reloc, /* special_function */
429 "R_PPC_ADDR14_BRTAKEN",/* name */
430 FALSE, /* partial_inplace */
431 0, /* src_mask */
432 0xfffc, /* dst_mask */
433 FALSE), /* pcrel_offset */
435 /* An absolute 16 bit branch, for which bit 10 should be set to
436 indicate that the branch is not expected to be taken. The lower
437 two bits must be zero. */
438 HOWTO (R_PPC_ADDR14_BRNTAKEN, /* type */
439 0, /* rightshift */
440 2, /* size (0 = byte, 1 = short, 2 = long) */
441 16, /* bitsize */
442 FALSE, /* pc_relative */
443 0, /* bitpos */
444 complain_overflow_bitfield, /* complain_on_overflow */
445 bfd_elf_generic_reloc, /* special_function */
446 "R_PPC_ADDR14_BRNTAKEN",/* name */
447 FALSE, /* partial_inplace */
448 0, /* src_mask */
449 0xfffc, /* dst_mask */
450 FALSE), /* pcrel_offset */
452 /* A relative 26 bit branch; the lower two bits must be zero. */
453 HOWTO (R_PPC_REL24, /* type */
454 0, /* rightshift */
455 2, /* size (0 = byte, 1 = short, 2 = long) */
456 26, /* bitsize */
457 TRUE, /* pc_relative */
458 0, /* bitpos */
459 complain_overflow_signed, /* complain_on_overflow */
460 bfd_elf_generic_reloc, /* special_function */
461 "R_PPC_REL24", /* name */
462 FALSE, /* partial_inplace */
463 0, /* src_mask */
464 0x3fffffc, /* dst_mask */
465 TRUE), /* pcrel_offset */
467 /* A relative 16 bit branch; the lower two bits must be zero. */
468 HOWTO (R_PPC_REL14, /* type */
469 0, /* rightshift */
470 2, /* size (0 = byte, 1 = short, 2 = long) */
471 16, /* bitsize */
472 TRUE, /* pc_relative */
473 0, /* bitpos */
474 complain_overflow_signed, /* complain_on_overflow */
475 bfd_elf_generic_reloc, /* special_function */
476 "R_PPC_REL14", /* name */
477 FALSE, /* partial_inplace */
478 0, /* src_mask */
479 0xfffc, /* dst_mask */
480 TRUE), /* pcrel_offset */
482 /* A relative 16 bit branch. Bit 10 should be set to indicate that
483 the branch is expected to be taken. The lower two bits must be
484 zero. */
485 HOWTO (R_PPC_REL14_BRTAKEN, /* type */
486 0, /* rightshift */
487 2, /* size (0 = byte, 1 = short, 2 = long) */
488 16, /* bitsize */
489 TRUE, /* pc_relative */
490 0, /* bitpos */
491 complain_overflow_signed, /* complain_on_overflow */
492 bfd_elf_generic_reloc, /* special_function */
493 "R_PPC_REL14_BRTAKEN", /* name */
494 FALSE, /* partial_inplace */
495 0, /* src_mask */
496 0xfffc, /* dst_mask */
497 TRUE), /* pcrel_offset */
499 /* A relative 16 bit branch. Bit 10 should be set to indicate that
500 the branch is not expected to be taken. The lower two bits must
501 be zero. */
502 HOWTO (R_PPC_REL14_BRNTAKEN, /* type */
503 0, /* rightshift */
504 2, /* size (0 = byte, 1 = short, 2 = long) */
505 16, /* bitsize */
506 TRUE, /* pc_relative */
507 0, /* bitpos */
508 complain_overflow_signed, /* complain_on_overflow */
509 bfd_elf_generic_reloc, /* special_function */
510 "R_PPC_REL14_BRNTAKEN",/* name */
511 FALSE, /* partial_inplace */
512 0, /* src_mask */
513 0xfffc, /* dst_mask */
514 TRUE), /* pcrel_offset */
516 /* Like R_PPC_ADDR16, but referring to the GOT table entry for the
517 symbol. */
518 HOWTO (R_PPC_GOT16, /* type */
519 0, /* rightshift */
520 1, /* size (0 = byte, 1 = short, 2 = long) */
521 16, /* bitsize */
522 FALSE, /* pc_relative */
523 0, /* bitpos */
524 complain_overflow_signed, /* complain_on_overflow */
525 bfd_elf_generic_reloc, /* special_function */
526 "R_PPC_GOT16", /* name */
527 FALSE, /* partial_inplace */
528 0, /* src_mask */
529 0xffff, /* dst_mask */
530 FALSE), /* pcrel_offset */
532 /* Like R_PPC_ADDR16_LO, but referring to the GOT table entry for
533 the symbol. */
534 HOWTO (R_PPC_GOT16_LO, /* type */
535 0, /* rightshift */
536 1, /* size (0 = byte, 1 = short, 2 = long) */
537 16, /* bitsize */
538 FALSE, /* pc_relative */
539 0, /* bitpos */
540 complain_overflow_dont, /* complain_on_overflow */
541 bfd_elf_generic_reloc, /* special_function */
542 "R_PPC_GOT16_LO", /* name */
543 FALSE, /* partial_inplace */
544 0, /* src_mask */
545 0xffff, /* dst_mask */
546 FALSE), /* pcrel_offset */
548 /* Like R_PPC_ADDR16_HI, but referring to the GOT table entry for
549 the symbol. */
550 HOWTO (R_PPC_GOT16_HI, /* type */
551 16, /* rightshift */
552 1, /* size (0 = byte, 1 = short, 2 = long) */
553 16, /* bitsize */
554 FALSE, /* pc_relative */
555 0, /* bitpos */
556 complain_overflow_bitfield, /* complain_on_overflow */
557 bfd_elf_generic_reloc, /* special_function */
558 "R_PPC_GOT16_HI", /* name */
559 FALSE, /* partial_inplace */
560 0, /* src_mask */
561 0xffff, /* dst_mask */
562 FALSE), /* pcrel_offset */
564 /* Like R_PPC_ADDR16_HA, but referring to the GOT table entry for
565 the symbol. */
566 HOWTO (R_PPC_GOT16_HA, /* type */
567 16, /* rightshift */
568 1, /* size (0 = byte, 1 = short, 2 = long) */
569 16, /* bitsize */
570 FALSE, /* pc_relative */
571 0, /* bitpos */
572 complain_overflow_bitfield, /* complain_on_overflow */
573 ppc_elf_addr16_ha_reloc, /* special_function */
574 "R_PPC_GOT16_HA", /* name */
575 FALSE, /* partial_inplace */
576 0, /* src_mask */
577 0xffff, /* dst_mask */
578 FALSE), /* pcrel_offset */
580 /* Like R_PPC_REL24, but referring to the procedure linkage table
581 entry for the symbol. */
582 HOWTO (R_PPC_PLTREL24, /* type */
583 0, /* rightshift */
584 2, /* size (0 = byte, 1 = short, 2 = long) */
585 26, /* bitsize */
586 TRUE, /* pc_relative */
587 0, /* bitpos */
588 complain_overflow_signed, /* complain_on_overflow */
589 bfd_elf_generic_reloc, /* special_function */
590 "R_PPC_PLTREL24", /* name */
591 FALSE, /* partial_inplace */
592 0, /* src_mask */
593 0x3fffffc, /* dst_mask */
594 TRUE), /* pcrel_offset */
596 /* This is used only by the dynamic linker. The symbol should exist
597 both in the object being run and in some shared library. The
598 dynamic linker copies the data addressed by the symbol from the
599 shared library into the object, because the object being
600 run has to have the data at some particular address. */
601 HOWTO (R_PPC_COPY, /* type */
602 0, /* rightshift */
603 2, /* size (0 = byte, 1 = short, 2 = long) */
604 32, /* bitsize */
605 FALSE, /* pc_relative */
606 0, /* bitpos */
607 complain_overflow_bitfield, /* complain_on_overflow */
608 bfd_elf_generic_reloc, /* special_function */
609 "R_PPC_COPY", /* name */
610 FALSE, /* partial_inplace */
611 0, /* src_mask */
612 0, /* dst_mask */
613 FALSE), /* pcrel_offset */
615 /* Like R_PPC_ADDR32, but used when setting global offset table
616 entries. */
617 HOWTO (R_PPC_GLOB_DAT, /* type */
618 0, /* rightshift */
619 2, /* size (0 = byte, 1 = short, 2 = long) */
620 32, /* bitsize */
621 FALSE, /* pc_relative */
622 0, /* bitpos */
623 complain_overflow_bitfield, /* complain_on_overflow */
624 bfd_elf_generic_reloc, /* special_function */
625 "R_PPC_GLOB_DAT", /* name */
626 FALSE, /* partial_inplace */
627 0, /* src_mask */
628 0xffffffff, /* dst_mask */
629 FALSE), /* pcrel_offset */
631 /* Marks a procedure linkage table entry for a symbol. */
632 HOWTO (R_PPC_JMP_SLOT, /* type */
633 0, /* rightshift */
634 2, /* size (0 = byte, 1 = short, 2 = long) */
635 32, /* bitsize */
636 FALSE, /* pc_relative */
637 0, /* bitpos */
638 complain_overflow_bitfield, /* complain_on_overflow */
639 bfd_elf_generic_reloc, /* special_function */
640 "R_PPC_JMP_SLOT", /* name */
641 FALSE, /* partial_inplace */
642 0, /* src_mask */
643 0, /* dst_mask */
644 FALSE), /* pcrel_offset */
646 /* Used only by the dynamic linker. When the object is run, this
647 longword is set to the load address of the object, plus the
648 addend. */
649 HOWTO (R_PPC_RELATIVE, /* type */
650 0, /* rightshift */
651 2, /* size (0 = byte, 1 = short, 2 = long) */
652 32, /* bitsize */
653 FALSE, /* pc_relative */
654 0, /* bitpos */
655 complain_overflow_bitfield, /* complain_on_overflow */
656 bfd_elf_generic_reloc, /* special_function */
657 "R_PPC_RELATIVE", /* name */
658 FALSE, /* partial_inplace */
659 0, /* src_mask */
660 0xffffffff, /* dst_mask */
661 FALSE), /* pcrel_offset */
663 /* Like R_PPC_REL24, but uses the value of the symbol within the
664 object rather than the final value. Normally used for
665 _GLOBAL_OFFSET_TABLE_. */
666 HOWTO (R_PPC_LOCAL24PC, /* type */
667 0, /* rightshift */
668 2, /* size (0 = byte, 1 = short, 2 = long) */
669 26, /* bitsize */
670 TRUE, /* pc_relative */
671 0, /* bitpos */
672 complain_overflow_signed, /* complain_on_overflow */
673 bfd_elf_generic_reloc, /* special_function */
674 "R_PPC_LOCAL24PC", /* name */
675 FALSE, /* partial_inplace */
676 0, /* src_mask */
677 0x3fffffc, /* dst_mask */
678 TRUE), /* pcrel_offset */
680 /* Like R_PPC_ADDR32, but may be unaligned. */
681 HOWTO (R_PPC_UADDR32, /* type */
682 0, /* rightshift */
683 2, /* size (0 = byte, 1 = short, 2 = long) */
684 32, /* bitsize */
685 FALSE, /* pc_relative */
686 0, /* bitpos */
687 complain_overflow_bitfield, /* complain_on_overflow */
688 bfd_elf_generic_reloc, /* special_function */
689 "R_PPC_UADDR32", /* name */
690 FALSE, /* partial_inplace */
691 0, /* src_mask */
692 0xffffffff, /* dst_mask */
693 FALSE), /* pcrel_offset */
695 /* Like R_PPC_ADDR16, but may be unaligned. */
696 HOWTO (R_PPC_UADDR16, /* type */
697 0, /* rightshift */
698 1, /* size (0 = byte, 1 = short, 2 = long) */
699 16, /* bitsize */
700 FALSE, /* pc_relative */
701 0, /* bitpos */
702 complain_overflow_bitfield, /* complain_on_overflow */
703 bfd_elf_generic_reloc, /* special_function */
704 "R_PPC_UADDR16", /* name */
705 FALSE, /* partial_inplace */
706 0, /* src_mask */
707 0xffff, /* dst_mask */
708 FALSE), /* pcrel_offset */
710 /* 32-bit PC relative */
711 HOWTO (R_PPC_REL32, /* type */
712 0, /* rightshift */
713 2, /* size (0 = byte, 1 = short, 2 = long) */
714 32, /* bitsize */
715 TRUE, /* pc_relative */
716 0, /* bitpos */
717 complain_overflow_bitfield, /* complain_on_overflow */
718 bfd_elf_generic_reloc, /* special_function */
719 "R_PPC_REL32", /* name */
720 FALSE, /* partial_inplace */
721 0, /* src_mask */
722 0xffffffff, /* dst_mask */
723 TRUE), /* pcrel_offset */
725 /* 32-bit relocation to the symbol's procedure linkage table.
726 FIXME: not supported. */
727 HOWTO (R_PPC_PLT32, /* type */
728 0, /* rightshift */
729 2, /* size (0 = byte, 1 = short, 2 = long) */
730 32, /* bitsize */
731 FALSE, /* pc_relative */
732 0, /* bitpos */
733 complain_overflow_bitfield, /* complain_on_overflow */
734 bfd_elf_generic_reloc, /* special_function */
735 "R_PPC_PLT32", /* name */
736 FALSE, /* partial_inplace */
737 0, /* src_mask */
738 0, /* dst_mask */
739 FALSE), /* pcrel_offset */
741 /* 32-bit PC relative relocation to the symbol's procedure linkage table.
742 FIXME: not supported. */
743 HOWTO (R_PPC_PLTREL32, /* type */
744 0, /* rightshift */
745 2, /* size (0 = byte, 1 = short, 2 = long) */
746 32, /* bitsize */
747 TRUE, /* pc_relative */
748 0, /* bitpos */
749 complain_overflow_bitfield, /* complain_on_overflow */
750 bfd_elf_generic_reloc, /* special_function */
751 "R_PPC_PLTREL32", /* name */
752 FALSE, /* partial_inplace */
753 0, /* src_mask */
754 0, /* dst_mask */
755 TRUE), /* pcrel_offset */
757 /* Like R_PPC_ADDR16_LO, but referring to the PLT table entry for
758 the symbol. */
759 HOWTO (R_PPC_PLT16_LO, /* type */
760 0, /* rightshift */
761 1, /* size (0 = byte, 1 = short, 2 = long) */
762 16, /* bitsize */
763 FALSE, /* pc_relative */
764 0, /* bitpos */
765 complain_overflow_dont, /* complain_on_overflow */
766 bfd_elf_generic_reloc, /* special_function */
767 "R_PPC_PLT16_LO", /* name */
768 FALSE, /* partial_inplace */
769 0, /* src_mask */
770 0xffff, /* dst_mask */
771 FALSE), /* pcrel_offset */
773 /* Like R_PPC_ADDR16_HI, but referring to the PLT table entry for
774 the symbol. */
775 HOWTO (R_PPC_PLT16_HI, /* type */
776 16, /* rightshift */
777 1, /* size (0 = byte, 1 = short, 2 = long) */
778 16, /* bitsize */
779 FALSE, /* pc_relative */
780 0, /* bitpos */
781 complain_overflow_bitfield, /* complain_on_overflow */
782 bfd_elf_generic_reloc, /* special_function */
783 "R_PPC_PLT16_HI", /* name */
784 FALSE, /* partial_inplace */
785 0, /* src_mask */
786 0xffff, /* dst_mask */
787 FALSE), /* pcrel_offset */
789 /* Like R_PPC_ADDR16_HA, but referring to the PLT table entry for
790 the symbol. */
791 HOWTO (R_PPC_PLT16_HA, /* type */
792 16, /* rightshift */
793 1, /* size (0 = byte, 1 = short, 2 = long) */
794 16, /* bitsize */
795 FALSE, /* pc_relative */
796 0, /* bitpos */
797 complain_overflow_bitfield, /* complain_on_overflow */
798 ppc_elf_addr16_ha_reloc, /* special_function */
799 "R_PPC_PLT16_HA", /* name */
800 FALSE, /* partial_inplace */
801 0, /* src_mask */
802 0xffff, /* dst_mask */
803 FALSE), /* pcrel_offset */
805 /* A sign-extended 16 bit value relative to _SDA_BASE_, for use with
806 small data items. */
807 HOWTO (R_PPC_SDAREL16, /* type */
808 0, /* rightshift */
809 1, /* size (0 = byte, 1 = short, 2 = long) */
810 16, /* bitsize */
811 FALSE, /* pc_relative */
812 0, /* bitpos */
813 complain_overflow_signed, /* complain_on_overflow */
814 bfd_elf_generic_reloc, /* special_function */
815 "R_PPC_SDAREL16", /* name */
816 FALSE, /* partial_inplace */
817 0, /* src_mask */
818 0xffff, /* dst_mask */
819 FALSE), /* pcrel_offset */
821 /* 16-bit section relative relocation. */
822 HOWTO (R_PPC_SECTOFF, /* type */
823 0, /* rightshift */
824 1, /* size (0 = byte, 1 = short, 2 = long) */
825 16, /* bitsize */
826 FALSE, /* pc_relative */
827 0, /* bitpos */
828 complain_overflow_bitfield, /* complain_on_overflow */
829 bfd_elf_generic_reloc, /* special_function */
830 "R_PPC_SECTOFF", /* name */
831 FALSE, /* partial_inplace */
832 0, /* src_mask */
833 0xffff, /* dst_mask */
834 FALSE), /* pcrel_offset */
836 /* 16-bit lower half section relative relocation. */
837 HOWTO (R_PPC_SECTOFF_LO, /* type */
838 0, /* rightshift */
839 1, /* size (0 = byte, 1 = short, 2 = long) */
840 16, /* bitsize */
841 FALSE, /* pc_relative */
842 0, /* bitpos */
843 complain_overflow_dont, /* complain_on_overflow */
844 bfd_elf_generic_reloc, /* special_function */
845 "R_PPC_SECTOFF_LO", /* name */
846 FALSE, /* partial_inplace */
847 0, /* src_mask */
848 0xffff, /* dst_mask */
849 FALSE), /* pcrel_offset */
851 /* 16-bit upper half section relative relocation. */
852 HOWTO (R_PPC_SECTOFF_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_SECTOFF_HI", /* name */
861 FALSE, /* partial_inplace */
862 0, /* src_mask */
863 0xffff, /* dst_mask */
864 FALSE), /* pcrel_offset */
866 /* 16-bit upper half adjusted section relative relocation. */
867 HOWTO (R_PPC_SECTOFF_HA, /* type */
868 16, /* rightshift */
869 1, /* size (0 = byte, 1 = short, 2 = long) */
870 16, /* bitsize */
871 FALSE, /* pc_relative */
872 0, /* bitpos */
873 complain_overflow_bitfield, /* complain_on_overflow */
874 ppc_elf_addr16_ha_reloc, /* special_function */
875 "R_PPC_SECTOFF_HA", /* name */
876 FALSE, /* partial_inplace */
877 0, /* src_mask */
878 0xffff, /* dst_mask */
879 FALSE), /* pcrel_offset */
881 /* The remaining relocs are from the Embedded ELF ABI, and are not
882 in the SVR4 ELF ABI. */
884 /* 32 bit value resulting from the addend minus the symbol */
885 HOWTO (R_PPC_EMB_NADDR32, /* type */
886 0, /* rightshift */
887 2, /* size (0 = byte, 1 = short, 2 = long) */
888 32, /* bitsize */
889 FALSE, /* pc_relative */
890 0, /* bitpos */
891 complain_overflow_bitfield, /* complain_on_overflow */
892 bfd_elf_generic_reloc, /* special_function */
893 "R_PPC_EMB_NADDR32", /* name */
894 FALSE, /* partial_inplace */
895 0, /* src_mask */
896 0xffffffff, /* dst_mask */
897 FALSE), /* pcrel_offset */
899 /* 16 bit value resulting from the addend minus the symbol */
900 HOWTO (R_PPC_EMB_NADDR16, /* type */
901 0, /* rightshift */
902 1, /* size (0 = byte, 1 = short, 2 = long) */
903 16, /* bitsize */
904 FALSE, /* pc_relative */
905 0, /* bitpos */
906 complain_overflow_bitfield, /* complain_on_overflow */
907 bfd_elf_generic_reloc, /* special_function */
908 "R_PPC_EMB_NADDR16", /* name */
909 FALSE, /* partial_inplace */
910 0, /* src_mask */
911 0xffff, /* dst_mask */
912 FALSE), /* pcrel_offset */
914 /* 16 bit value resulting from the addend minus the symbol */
915 HOWTO (R_PPC_EMB_NADDR16_LO, /* type */
916 0, /* rightshift */
917 1, /* size (0 = byte, 1 = short, 2 = long) */
918 16, /* bitsize */
919 FALSE, /* pc_relative */
920 0, /* bitpos */
921 complain_overflow_dont,/* complain_on_overflow */
922 bfd_elf_generic_reloc, /* special_function */
923 "R_PPC_EMB_ADDR16_LO", /* name */
924 FALSE, /* partial_inplace */
925 0, /* src_mask */
926 0xffff, /* dst_mask */
927 FALSE), /* pcrel_offset */
929 /* The high order 16 bits of the addend minus the symbol */
930 HOWTO (R_PPC_EMB_NADDR16_HI, /* type */
931 16, /* rightshift */
932 1, /* size (0 = byte, 1 = short, 2 = long) */
933 16, /* bitsize */
934 FALSE, /* pc_relative */
935 0, /* bitpos */
936 complain_overflow_dont, /* complain_on_overflow */
937 bfd_elf_generic_reloc, /* special_function */
938 "R_PPC_EMB_NADDR16_HI", /* name */
939 FALSE, /* partial_inplace */
940 0, /* src_mask */
941 0xffff, /* dst_mask */
942 FALSE), /* pcrel_offset */
944 /* The high order 16 bits of the result of the addend minus the address,
945 plus 1 if the contents of the low 16 bits, treated as a signed number,
946 is negative. */
947 HOWTO (R_PPC_EMB_NADDR16_HA, /* type */
948 16, /* rightshift */
949 1, /* size (0 = byte, 1 = short, 2 = long) */
950 16, /* bitsize */
951 FALSE, /* pc_relative */
952 0, /* bitpos */
953 complain_overflow_dont, /* complain_on_overflow */
954 ppc_elf_addr16_ha_reloc, /* special_function */
955 "R_PPC_EMB_NADDR16_HA", /* name */
956 FALSE, /* partial_inplace */
957 0, /* src_mask */
958 0xffff, /* dst_mask */
959 FALSE), /* pcrel_offset */
961 /* 16 bit value resulting from allocating a 4 byte word to hold an
962 address in the .sdata section, and returning the offset from
963 _SDA_BASE_ for that relocation */
964 HOWTO (R_PPC_EMB_SDAI16, /* type */
965 0, /* rightshift */
966 1, /* size (0 = byte, 1 = short, 2 = long) */
967 16, /* bitsize */
968 FALSE, /* pc_relative */
969 0, /* bitpos */
970 complain_overflow_bitfield, /* complain_on_overflow */
971 bfd_elf_generic_reloc, /* special_function */
972 "R_PPC_EMB_SDAI16", /* name */
973 FALSE, /* partial_inplace */
974 0, /* src_mask */
975 0xffff, /* dst_mask */
976 FALSE), /* pcrel_offset */
978 /* 16 bit value resulting from allocating a 4 byte word to hold an
979 address in the .sdata2 section, and returning the offset from
980 _SDA2_BASE_ for that relocation */
981 HOWTO (R_PPC_EMB_SDA2I16, /* type */
982 0, /* rightshift */
983 1, /* size (0 = byte, 1 = short, 2 = long) */
984 16, /* bitsize */
985 FALSE, /* pc_relative */
986 0, /* bitpos */
987 complain_overflow_bitfield, /* complain_on_overflow */
988 bfd_elf_generic_reloc, /* special_function */
989 "R_PPC_EMB_SDA2I16", /* name */
990 FALSE, /* partial_inplace */
991 0, /* src_mask */
992 0xffff, /* dst_mask */
993 FALSE), /* pcrel_offset */
995 /* A sign-extended 16 bit value relative to _SDA2_BASE_, for use with
996 small data items. */
997 HOWTO (R_PPC_EMB_SDA2REL, /* type */
998 0, /* rightshift */
999 1, /* size (0 = byte, 1 = short, 2 = long) */
1000 16, /* bitsize */
1001 FALSE, /* pc_relative */
1002 0, /* bitpos */
1003 complain_overflow_signed, /* complain_on_overflow */
1004 bfd_elf_generic_reloc, /* special_function */
1005 "R_PPC_EMB_SDA2REL", /* name */
1006 FALSE, /* partial_inplace */
1007 0, /* src_mask */
1008 0xffff, /* dst_mask */
1009 FALSE), /* pcrel_offset */
1011 /* Relocate against either _SDA_BASE_ or _SDA2_BASE_, filling in the 16 bit
1012 signed offset from the appropriate base, and filling in the register
1013 field with the appropriate register (0, 2, or 13). */
1014 HOWTO (R_PPC_EMB_SDA21, /* type */
1015 0, /* rightshift */
1016 2, /* size (0 = byte, 1 = short, 2 = long) */
1017 16, /* bitsize */
1018 FALSE, /* pc_relative */
1019 0, /* bitpos */
1020 complain_overflow_signed, /* complain_on_overflow */
1021 bfd_elf_generic_reloc, /* special_function */
1022 "R_PPC_EMB_SDA21", /* name */
1023 FALSE, /* partial_inplace */
1024 0, /* src_mask */
1025 0xffff, /* dst_mask */
1026 FALSE), /* pcrel_offset */
1028 /* Relocation not handled: R_PPC_EMB_MRKREF */
1029 /* Relocation not handled: R_PPC_EMB_RELSEC16 */
1030 /* Relocation not handled: R_PPC_EMB_RELST_LO */
1031 /* Relocation not handled: R_PPC_EMB_RELST_HI */
1032 /* Relocation not handled: R_PPC_EMB_RELST_HA */
1033 /* Relocation not handled: R_PPC_EMB_BIT_FLD */
1035 /* PC relative relocation against either _SDA_BASE_ or _SDA2_BASE_, filling
1036 in the 16 bit signed offset from the appropriate base, and filling in the
1037 register field with the appropriate register (0, 2, or 13). */
1038 HOWTO (R_PPC_EMB_RELSDA, /* type */
1039 0, /* rightshift */
1040 1, /* size (0 = byte, 1 = short, 2 = long) */
1041 16, /* bitsize */
1042 TRUE, /* pc_relative */
1043 0, /* bitpos */
1044 complain_overflow_signed, /* complain_on_overflow */
1045 bfd_elf_generic_reloc, /* special_function */
1046 "R_PPC_EMB_RELSDA", /* name */
1047 FALSE, /* partial_inplace */
1048 0, /* src_mask */
1049 0xffff, /* dst_mask */
1050 FALSE), /* pcrel_offset */
1052 /* GNU extension to record C++ vtable hierarchy */
1053 HOWTO (R_PPC_GNU_VTINHERIT, /* type */
1054 0, /* rightshift */
1055 0, /* size (0 = byte, 1 = short, 2 = long) */
1056 0, /* bitsize */
1057 FALSE, /* pc_relative */
1058 0, /* bitpos */
1059 complain_overflow_dont, /* complain_on_overflow */
1060 NULL, /* special_function */
1061 "R_PPC_GNU_VTINHERIT", /* name */
1062 FALSE, /* partial_inplace */
1063 0, /* src_mask */
1064 0, /* dst_mask */
1065 FALSE), /* pcrel_offset */
1067 /* GNU extension to record C++ vtable member usage */
1068 HOWTO (R_PPC_GNU_VTENTRY, /* type */
1069 0, /* rightshift */
1070 0, /* size (0 = byte, 1 = short, 2 = long) */
1071 0, /* bitsize */
1072 FALSE, /* pc_relative */
1073 0, /* bitpos */
1074 complain_overflow_dont, /* complain_on_overflow */
1075 NULL, /* special_function */
1076 "R_PPC_GNU_VTENTRY", /* name */
1077 FALSE, /* partial_inplace */
1078 0, /* src_mask */
1079 0, /* dst_mask */
1080 FALSE), /* pcrel_offset */
1082 /* Phony reloc to handle AIX style TOC entries */
1083 HOWTO (R_PPC_TOC16, /* type */
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 bfd_elf_generic_reloc, /* special_function */
1091 "R_PPC_TOC16", /* name */
1092 FALSE, /* partial_inplace */
1093 0, /* src_mask */
1094 0xffff, /* dst_mask */
1095 FALSE), /* pcrel_offset */
1098 /* Initialize the ppc_elf_howto_table, so that linear accesses can be done. */
1100 static void
1101 ppc_elf_howto_init ()
1103 unsigned int i, type;
1105 for (i = 0; i < sizeof (ppc_elf_howto_raw) / sizeof (ppc_elf_howto_raw[0]); i++)
1107 type = ppc_elf_howto_raw[i].type;
1108 BFD_ASSERT (type < sizeof (ppc_elf_howto_table) / sizeof (ppc_elf_howto_table[0]));
1109 ppc_elf_howto_table[type] = &ppc_elf_howto_raw[i];
1113 /* This function handles relaxing for the PPC with option --mpc860c0[=<n>].
1115 The MPC860, revision C0 or earlier contains a bug in the die.
1116 If all of the following conditions are true, the next instruction
1117 to be executed *may* be treated as a no-op.
1118 1/ A forward branch is executed.
1119 2/ The branch is predicted as not taken.
1120 3/ The branch is taken.
1121 4/ The branch is located in the last 5 words of a page.
1122 (The EOP limit is 5 by default but may be specified as any value from 1-10.)
1124 Our software solution is to detect these problematic branches in a
1125 linker pass and modify them as follows:
1126 1/ Unconditional branches - Since these are always predicted taken,
1127 there is no problem and no action is required.
1128 2/ Conditional backward branches - No problem, no action required.
1129 3/ Conditional forward branches - Ensure that the "inverse prediction
1130 bit" is set (ensure it is predicted taken).
1131 4/ Conditional register branches - Ensure that the "y bit" is set
1132 (ensure it is predicted taken).
1135 /* Sort sections by address. */
1137 static int
1138 ppc_elf_sort_rela (arg1, arg2)
1139 const PTR arg1;
1140 const PTR arg2;
1142 const Elf_Internal_Rela **rela1 = (const Elf_Internal_Rela**) arg1;
1143 const Elf_Internal_Rela **rela2 = (const Elf_Internal_Rela**) arg2;
1145 /* Sort by offset. */
1146 return ((*rela1)->r_offset - (*rela2)->r_offset);
1149 static bfd_boolean
1150 ppc_elf_relax_section (abfd, isec, link_info, again)
1151 bfd *abfd;
1152 asection *isec;
1153 struct bfd_link_info *link_info;
1154 bfd_boolean *again;
1156 #define PAGESIZE 0x1000
1158 bfd_byte *contents = NULL;
1159 bfd_byte *free_contents = NULL;
1160 Elf_Internal_Rela *internal_relocs = NULL;
1161 Elf_Internal_Rela *free_relocs = NULL;
1162 Elf_Internal_Rela **rela_comb = NULL;
1163 int comb_curr, comb_count;
1165 /* We never have to do this more than once per input section. */
1166 *again = FALSE;
1168 /* If needed, initialize this section's cooked size. */
1169 if (isec->_cooked_size == 0)
1170 isec->_cooked_size = isec->_raw_size;
1172 /* We're only interested in text sections which overlap the
1173 troublesome area at the end of a page. */
1174 if (link_info->mpc860c0 && (isec->flags & SEC_CODE) && isec->_cooked_size)
1176 bfd_vma dot, end_page, end_section;
1177 bfd_boolean section_modified;
1179 /* Get the section contents. */
1180 /* Get cached copy if it exists. */
1181 if (elf_section_data (isec)->this_hdr.contents != NULL)
1182 contents = elf_section_data (isec)->this_hdr.contents;
1183 else
1185 /* Go get them off disk. */
1186 contents = (bfd_byte *) bfd_malloc (isec->_raw_size);
1187 if (contents == NULL)
1188 goto error_return;
1189 free_contents = contents;
1191 if (! bfd_get_section_contents (abfd, isec, contents,
1192 (file_ptr) 0, isec->_raw_size))
1193 goto error_return;
1196 comb_curr = 0;
1197 comb_count = 0;
1198 if (isec->reloc_count)
1200 unsigned n;
1201 bfd_size_type amt;
1203 /* Get a copy of the native relocations. */
1204 internal_relocs = _bfd_elf32_link_read_relocs (
1205 abfd, isec, (PTR) NULL, (Elf_Internal_Rela *) NULL,
1206 link_info->keep_memory);
1207 if (internal_relocs == NULL)
1208 goto error_return;
1209 if (! link_info->keep_memory)
1210 free_relocs = internal_relocs;
1212 /* Setup a faster access method for the reloc info we need. */
1213 amt = isec->reloc_count;
1214 amt *= sizeof (Elf_Internal_Rela*);
1215 rela_comb = (Elf_Internal_Rela**) bfd_malloc (amt);
1216 if (rela_comb == NULL)
1217 goto error_return;
1218 for (n = 0; n < isec->reloc_count; ++n)
1220 long r_type;
1222 r_type = ELF32_R_TYPE (internal_relocs[n].r_info);
1223 if (r_type < 0 || r_type >= (int) R_PPC_max)
1224 goto error_return;
1226 /* Prologue constants are sometimes present in the ".text"
1227 sections and they can be identified by their associated relocation.
1228 We don't want to process those words and some others which
1229 can also be identified by their relocations. However, not all
1230 conditional branches will have a relocation so we will
1231 only ignore words that 1) have a reloc, and 2) the reloc
1232 is not applicable to a conditional branch.
1233 The array rela_comb is built here for use in the EOP scan loop. */
1234 switch (r_type)
1236 case R_PPC_ADDR14_BRNTAKEN: /* absolute, predicted not taken */
1237 case R_PPC_REL14: /* relative cond. br. */
1238 case R_PPC_REL14_BRNTAKEN: /* rel. cond. br., predicted not taken */
1239 /* We should check the instruction. */
1240 break;
1241 default:
1242 /* The word is not a conditional branch - ignore it. */
1243 rela_comb[comb_count++] = &internal_relocs[n];
1244 break;
1247 if (comb_count > 1)
1248 qsort (rela_comb, (size_t) comb_count, sizeof (int), ppc_elf_sort_rela);
1251 /* Enumerate each EOP region that overlaps this section. */
1252 end_section = isec->vma + isec->_cooked_size;
1253 dot = end_page = (isec->vma | (PAGESIZE - 1)) + 1;
1254 dot -= link_info->mpc860c0;
1255 section_modified = FALSE;
1256 if (dot < isec->vma) /* Increment the start position if this section */
1257 dot = isec->vma; /* begins in the middle of its first EOP region. */
1258 for (;
1259 dot < end_section;
1260 dot += PAGESIZE, end_page += PAGESIZE)
1263 /* Check each word in this EOP region. */
1264 for (; dot < end_page; dot += 4)
1266 bfd_vma isec_offset;
1267 unsigned long insn;
1268 bfd_boolean skip, modified;
1270 /* Don't process this word if there is a relocation for it and
1271 the relocation indicates the word is not a conditional branch. */
1272 skip = FALSE;
1273 isec_offset = dot - isec->vma;
1274 for (; comb_curr<comb_count; ++comb_curr)
1276 bfd_vma r_offset;
1278 r_offset = rela_comb[comb_curr]->r_offset;
1279 if (r_offset >= isec_offset)
1281 if (r_offset == isec_offset) skip = TRUE;
1282 break;
1285 if (skip) continue;
1287 /* Check the current word for a problematic conditional branch. */
1288 #define BO0(insn) ((insn) & 0x02000000)
1289 #define BO2(insn) ((insn) & 0x00800000)
1290 #define BO4(insn) ((insn) & 0x00200000)
1291 insn = (unsigned long) bfd_get_32 (abfd, contents + isec_offset);
1292 modified = FALSE;
1293 if ((insn & 0xFc000000) == 0x40000000)
1295 /* Instruction is BCx */
1296 if ((!BO0(insn) || !BO2(insn)) && !BO4(insn))
1298 bfd_vma target;
1299 /* This branch is predicted as "normal".
1300 If this is a forward branch, it is problematic. */
1302 target = insn & 0x0000Fffc; /*extract*/
1303 target = (target ^ 0x8000) - 0x8000; /*sign extend*/
1304 if ((insn & 0x00000002) == 0)
1305 target += dot; /*convert to abs*/
1306 if (target > dot)
1308 insn |= 0x00200000; /* set the prediction bit */
1309 modified = TRUE;
1313 else if ((insn & 0xFc00Fffe) == 0x4c000420)
1315 /* Instruction is BCCTRx */
1316 if ((!BO0(insn) || !BO2(insn)) && !BO4(insn))
1318 /* This branch is predicted as not-taken.
1319 If this is a forward branch, it is problematic.
1320 Since we can't tell statically if it will branch forward,
1321 always set the prediction bit. */
1322 insn |= 0x00200000; /* set the prediction bit */
1323 modified = TRUE;
1326 else if ((insn & 0xFc00Fffe) == 0x4c000020)
1328 /* Instruction is BCLRx */
1329 if ((!BO0(insn) || !BO2(insn)) && !BO4(insn))
1331 /* This branch is predicted as not-taken.
1332 If this is a forward branch, it is problematic.
1333 Since we can't tell statically if it will branch forward,
1334 always set the prediction bit. */
1335 insn |= 0x00200000; /* set the prediction bit */
1336 modified = TRUE;
1339 #undef BO0
1340 #undef BO2
1341 #undef BO4
1342 if (modified)
1344 bfd_put_32 (abfd, (bfd_vma) insn, contents + isec_offset);
1345 section_modified = TRUE;
1349 if (section_modified)
1351 elf_section_data (isec)->this_hdr.contents = contents;
1352 free_contents = NULL;
1356 if (rela_comb != NULL)
1358 free (rela_comb);
1359 rela_comb = NULL;
1362 if (free_relocs != NULL)
1364 free (free_relocs);
1365 free_relocs = NULL;
1368 if (free_contents != NULL)
1370 if (! link_info->keep_memory)
1371 free (free_contents);
1372 else
1374 /* Cache the section contents for elf_link_input_bfd. */
1375 elf_section_data (isec)->this_hdr.contents = contents;
1377 free_contents = NULL;
1380 return TRUE;
1382 error_return:
1383 if (rela_comb != NULL)
1384 free (rela_comb);
1385 if (free_relocs != NULL)
1386 free (free_relocs);
1387 if (free_contents != NULL)
1388 free (free_contents);
1389 return FALSE;
1392 static reloc_howto_type *
1393 ppc_elf_reloc_type_lookup (abfd, code)
1394 bfd *abfd ATTRIBUTE_UNUSED;
1395 bfd_reloc_code_real_type code;
1397 enum elf_ppc_reloc_type ppc_reloc = R_PPC_NONE;
1399 if (!ppc_elf_howto_table[R_PPC_ADDR32])
1400 /* Initialize howto table if needed. */
1401 ppc_elf_howto_init ();
1403 switch ((int) code)
1405 default:
1406 return (reloc_howto_type *) NULL;
1408 case BFD_RELOC_NONE: ppc_reloc = R_PPC_NONE; break;
1409 case BFD_RELOC_32: ppc_reloc = R_PPC_ADDR32; break;
1410 case BFD_RELOC_PPC_BA26: ppc_reloc = R_PPC_ADDR24; break;
1411 case BFD_RELOC_16: ppc_reloc = R_PPC_ADDR16; break;
1412 case BFD_RELOC_LO16: ppc_reloc = R_PPC_ADDR16_LO; break;
1413 case BFD_RELOC_HI16: ppc_reloc = R_PPC_ADDR16_HI; break;
1414 case BFD_RELOC_HI16_S: ppc_reloc = R_PPC_ADDR16_HA; break;
1415 case BFD_RELOC_PPC_BA16: ppc_reloc = R_PPC_ADDR14; break;
1416 case BFD_RELOC_PPC_BA16_BRTAKEN: ppc_reloc = R_PPC_ADDR14_BRTAKEN; break;
1417 case BFD_RELOC_PPC_BA16_BRNTAKEN: ppc_reloc = R_PPC_ADDR14_BRNTAKEN; break;
1418 case BFD_RELOC_PPC_B26: ppc_reloc = R_PPC_REL24; break;
1419 case BFD_RELOC_PPC_B16: ppc_reloc = R_PPC_REL14; break;
1420 case BFD_RELOC_PPC_B16_BRTAKEN: ppc_reloc = R_PPC_REL14_BRTAKEN; break;
1421 case BFD_RELOC_PPC_B16_BRNTAKEN: ppc_reloc = R_PPC_REL14_BRNTAKEN; break;
1422 case BFD_RELOC_16_GOTOFF: ppc_reloc = R_PPC_GOT16; break;
1423 case BFD_RELOC_LO16_GOTOFF: ppc_reloc = R_PPC_GOT16_LO; break;
1424 case BFD_RELOC_HI16_GOTOFF: ppc_reloc = R_PPC_GOT16_HI; break;
1425 case BFD_RELOC_HI16_S_GOTOFF: ppc_reloc = R_PPC_GOT16_HA; break;
1426 case BFD_RELOC_24_PLT_PCREL: ppc_reloc = R_PPC_PLTREL24; break;
1427 case BFD_RELOC_PPC_COPY: ppc_reloc = R_PPC_COPY; break;
1428 case BFD_RELOC_PPC_GLOB_DAT: ppc_reloc = R_PPC_GLOB_DAT; break;
1429 case BFD_RELOC_PPC_LOCAL24PC: ppc_reloc = R_PPC_LOCAL24PC; break;
1430 case BFD_RELOC_32_PCREL: ppc_reloc = R_PPC_REL32; break;
1431 case BFD_RELOC_32_PLTOFF: ppc_reloc = R_PPC_PLT32; break;
1432 case BFD_RELOC_32_PLT_PCREL: ppc_reloc = R_PPC_PLTREL32; break;
1433 case BFD_RELOC_LO16_PLTOFF: ppc_reloc = R_PPC_PLT16_LO; break;
1434 case BFD_RELOC_HI16_PLTOFF: ppc_reloc = R_PPC_PLT16_HI; break;
1435 case BFD_RELOC_HI16_S_PLTOFF: ppc_reloc = R_PPC_PLT16_HA; break;
1436 case BFD_RELOC_GPREL16: ppc_reloc = R_PPC_SDAREL16; break;
1437 case BFD_RELOC_16_BASEREL: ppc_reloc = R_PPC_SECTOFF; break;
1438 case BFD_RELOC_LO16_BASEREL: ppc_reloc = R_PPC_SECTOFF_LO; break;
1439 case BFD_RELOC_HI16_BASEREL: ppc_reloc = R_PPC_SECTOFF_HI; break;
1440 case BFD_RELOC_HI16_S_BASEREL: ppc_reloc = R_PPC_SECTOFF_HA; break;
1441 case BFD_RELOC_CTOR: ppc_reloc = R_PPC_ADDR32; break;
1442 case BFD_RELOC_PPC_TOC16: ppc_reloc = R_PPC_TOC16; break;
1443 case BFD_RELOC_PPC_EMB_NADDR32: ppc_reloc = R_PPC_EMB_NADDR32; break;
1444 case BFD_RELOC_PPC_EMB_NADDR16: ppc_reloc = R_PPC_EMB_NADDR16; break;
1445 case BFD_RELOC_PPC_EMB_NADDR16_LO: ppc_reloc = R_PPC_EMB_NADDR16_LO; break;
1446 case BFD_RELOC_PPC_EMB_NADDR16_HI: ppc_reloc = R_PPC_EMB_NADDR16_HI; break;
1447 case BFD_RELOC_PPC_EMB_NADDR16_HA: ppc_reloc = R_PPC_EMB_NADDR16_HA; break;
1448 case BFD_RELOC_PPC_EMB_SDAI16: ppc_reloc = R_PPC_EMB_SDAI16; break;
1449 case BFD_RELOC_PPC_EMB_SDA2I16: ppc_reloc = R_PPC_EMB_SDA2I16; break;
1450 case BFD_RELOC_PPC_EMB_SDA2REL: ppc_reloc = R_PPC_EMB_SDA2REL; break;
1451 case BFD_RELOC_PPC_EMB_SDA21: ppc_reloc = R_PPC_EMB_SDA21; break;
1452 case BFD_RELOC_PPC_EMB_MRKREF: ppc_reloc = R_PPC_EMB_MRKREF; break;
1453 case BFD_RELOC_PPC_EMB_RELSEC16: ppc_reloc = R_PPC_EMB_RELSEC16; break;
1454 case BFD_RELOC_PPC_EMB_RELST_LO: ppc_reloc = R_PPC_EMB_RELST_LO; break;
1455 case BFD_RELOC_PPC_EMB_RELST_HI: ppc_reloc = R_PPC_EMB_RELST_HI; break;
1456 case BFD_RELOC_PPC_EMB_RELST_HA: ppc_reloc = R_PPC_EMB_RELST_HA; break;
1457 case BFD_RELOC_PPC_EMB_BIT_FLD: ppc_reloc = R_PPC_EMB_BIT_FLD; break;
1458 case BFD_RELOC_PPC_EMB_RELSDA: ppc_reloc = R_PPC_EMB_RELSDA; break;
1459 case BFD_RELOC_VTABLE_INHERIT: ppc_reloc = R_PPC_GNU_VTINHERIT; break;
1460 case BFD_RELOC_VTABLE_ENTRY: ppc_reloc = R_PPC_GNU_VTENTRY; break;
1463 return ppc_elf_howto_table[(int) ppc_reloc];
1466 /* Set the howto pointer for a PowerPC ELF reloc. */
1468 static void
1469 ppc_elf_info_to_howto (abfd, cache_ptr, dst)
1470 bfd *abfd ATTRIBUTE_UNUSED;
1471 arelent *cache_ptr;
1472 Elf_Internal_Rela *dst;
1474 if (!ppc_elf_howto_table[R_PPC_ADDR32])
1475 /* Initialize howto table if needed. */
1476 ppc_elf_howto_init ();
1478 BFD_ASSERT (ELF32_R_TYPE (dst->r_info) < (unsigned int) R_PPC_max);
1479 cache_ptr->howto = ppc_elf_howto_table[ELF32_R_TYPE (dst->r_info)];
1482 /* Handle the R_PPC_ADDR16_HA reloc. */
1484 static bfd_reloc_status_type
1485 ppc_elf_addr16_ha_reloc (abfd, reloc_entry, symbol, data, input_section,
1486 output_bfd, error_message)
1487 bfd *abfd ATTRIBUTE_UNUSED;
1488 arelent *reloc_entry;
1489 asymbol *symbol;
1490 PTR data ATTRIBUTE_UNUSED;
1491 asection *input_section;
1492 bfd *output_bfd;
1493 char **error_message ATTRIBUTE_UNUSED;
1495 bfd_vma relocation;
1497 if (output_bfd != NULL)
1499 reloc_entry->address += input_section->output_offset;
1500 return bfd_reloc_ok;
1503 if (reloc_entry->address > input_section->_cooked_size)
1504 return bfd_reloc_outofrange;
1506 if (bfd_is_com_section (symbol->section))
1507 relocation = 0;
1508 else
1509 relocation = symbol->value;
1511 relocation += symbol->section->output_section->vma;
1512 relocation += symbol->section->output_offset;
1513 relocation += reloc_entry->addend;
1515 reloc_entry->addend += (relocation & 0x8000) << 1;
1517 return bfd_reloc_continue;
1520 /* Fix bad default arch selected for a 32 bit input bfd when the
1521 default is 64 bit. */
1523 static bfd_boolean
1524 ppc_elf_object_p (abfd)
1525 bfd *abfd;
1527 if (abfd->arch_info->the_default && abfd->arch_info->bits_per_word == 64)
1529 Elf_Internal_Ehdr *i_ehdr = elf_elfheader (abfd);
1531 if (i_ehdr->e_ident[EI_CLASS] == ELFCLASS32)
1533 /* Relies on arch after 64 bit default being 32 bit default. */
1534 abfd->arch_info = abfd->arch_info->next;
1535 BFD_ASSERT (abfd->arch_info->bits_per_word == 32);
1538 return TRUE;
1541 /* Function to set whether a module needs the -mrelocatable bit set. */
1543 static bfd_boolean
1544 ppc_elf_set_private_flags (abfd, flags)
1545 bfd *abfd;
1546 flagword flags;
1548 BFD_ASSERT (!elf_flags_init (abfd)
1549 || elf_elfheader (abfd)->e_flags == flags);
1551 elf_elfheader (abfd)->e_flags = flags;
1552 elf_flags_init (abfd) = TRUE;
1553 return TRUE;
1556 /* Merge backend specific data from an object file to the output
1557 object file when linking */
1558 static bfd_boolean
1559 ppc_elf_merge_private_bfd_data (ibfd, obfd)
1560 bfd *ibfd;
1561 bfd *obfd;
1563 flagword old_flags;
1564 flagword new_flags;
1565 bfd_boolean error;
1567 /* Check if we have the same endianess */
1568 if (! _bfd_generic_verify_endian_match (ibfd, obfd))
1569 return FALSE;
1571 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
1572 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
1573 return TRUE;
1575 new_flags = elf_elfheader (ibfd)->e_flags;
1576 old_flags = elf_elfheader (obfd)->e_flags;
1577 if (!elf_flags_init (obfd)) /* First call, no flags set */
1579 elf_flags_init (obfd) = TRUE;
1580 elf_elfheader (obfd)->e_flags = new_flags;
1583 else if (new_flags == old_flags) /* Compatible flags are ok */
1586 else /* Incompatible flags */
1588 /* Warn about -mrelocatable mismatch. Allow -mrelocatable-lib to be linked
1589 with either. */
1590 error = FALSE;
1591 if ((new_flags & EF_PPC_RELOCATABLE) != 0
1592 && (old_flags & (EF_PPC_RELOCATABLE | EF_PPC_RELOCATABLE_LIB)) == 0)
1594 error = TRUE;
1595 (*_bfd_error_handler)
1596 (_("%s: compiled with -mrelocatable and linked with modules compiled normally"),
1597 bfd_archive_filename (ibfd));
1599 else if ((new_flags & (EF_PPC_RELOCATABLE | EF_PPC_RELOCATABLE_LIB)) == 0
1600 && (old_flags & EF_PPC_RELOCATABLE) != 0)
1602 error = TRUE;
1603 (*_bfd_error_handler)
1604 (_("%s: compiled normally and linked with modules compiled with -mrelocatable"),
1605 bfd_archive_filename (ibfd));
1608 /* The output is -mrelocatable-lib iff both the input files are. */
1609 if (! (new_flags & EF_PPC_RELOCATABLE_LIB))
1610 elf_elfheader (obfd)->e_flags &= ~EF_PPC_RELOCATABLE_LIB;
1612 /* The output is -mrelocatable iff it can't be -mrelocatable-lib,
1613 but each input file is either -mrelocatable or -mrelocatable-lib. */
1614 if (! (elf_elfheader (obfd)->e_flags & EF_PPC_RELOCATABLE_LIB)
1615 && (new_flags & (EF_PPC_RELOCATABLE_LIB | EF_PPC_RELOCATABLE))
1616 && (old_flags & (EF_PPC_RELOCATABLE_LIB | EF_PPC_RELOCATABLE)))
1617 elf_elfheader (obfd)->e_flags |= EF_PPC_RELOCATABLE;
1619 /* Do not warn about eabi vs. V.4 mismatch, just or in the bit if any module uses it */
1620 elf_elfheader (obfd)->e_flags |= (new_flags & EF_PPC_EMB);
1622 new_flags &= ~ (EF_PPC_RELOCATABLE | EF_PPC_RELOCATABLE_LIB | EF_PPC_EMB);
1623 old_flags &= ~ (EF_PPC_RELOCATABLE | EF_PPC_RELOCATABLE_LIB | EF_PPC_EMB);
1625 /* Warn about any other mismatches */
1626 if (new_flags != old_flags)
1628 error = TRUE;
1629 (*_bfd_error_handler)
1630 (_("%s: uses different e_flags (0x%lx) fields than previous modules (0x%lx)"),
1631 bfd_archive_filename (ibfd), (long) new_flags, (long) old_flags);
1634 if (error)
1636 bfd_set_error (bfd_error_bad_value);
1637 return FALSE;
1641 return TRUE;
1644 /* Handle a PowerPC specific section when reading an object file. This
1645 is called when elfcode.h finds a section with an unknown type. */
1647 static bfd_boolean
1648 ppc_elf_section_from_shdr (abfd, hdr, name)
1649 bfd *abfd;
1650 Elf_Internal_Shdr *hdr;
1651 const char *name;
1653 asection *newsect;
1654 flagword flags;
1656 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name))
1657 return FALSE;
1659 newsect = hdr->bfd_section;
1660 flags = bfd_get_section_flags (abfd, newsect);
1661 if (hdr->sh_flags & SHF_EXCLUDE)
1662 flags |= SEC_EXCLUDE;
1664 if (hdr->sh_type == SHT_ORDERED)
1665 flags |= SEC_SORT_ENTRIES;
1667 bfd_set_section_flags (abfd, newsect, flags);
1668 return TRUE;
1671 /* Set up any other section flags and such that may be necessary. */
1673 static bfd_boolean
1674 ppc_elf_fake_sections (abfd, shdr, asect)
1675 bfd *abfd ATTRIBUTE_UNUSED;
1676 Elf_Internal_Shdr *shdr;
1677 asection *asect;
1679 if ((asect->flags & SEC_EXCLUDE) != 0)
1680 shdr->sh_flags |= SHF_EXCLUDE;
1682 if ((asect->flags & SEC_SORT_ENTRIES) != 0)
1683 shdr->sh_type = SHT_ORDERED;
1685 return TRUE;
1688 /* Create a special linker section */
1689 static elf_linker_section_t *
1690 ppc_elf_create_linker_section (abfd, info, which)
1691 bfd *abfd;
1692 struct bfd_link_info *info;
1693 enum elf_linker_section_enum which;
1695 bfd *dynobj = elf_hash_table (info)->dynobj;
1696 elf_linker_section_t *lsect;
1698 /* Record the first bfd section that needs the special section */
1699 if (!dynobj)
1700 dynobj = elf_hash_table (info)->dynobj = abfd;
1702 /* If this is the first time, create the section */
1703 lsect = elf_linker_section (dynobj, which);
1704 if (!lsect)
1706 elf_linker_section_t defaults;
1707 static elf_linker_section_t zero_section;
1709 defaults = zero_section;
1710 defaults.which = which;
1711 defaults.hole_written_p = FALSE;
1712 defaults.alignment = 2;
1714 /* Both of these sections are (technically) created by the user
1715 putting data in them, so they shouldn't be marked
1716 SEC_LINKER_CREATED.
1718 The linker creates them so it has somewhere to attach their
1719 respective symbols. In fact, if they were empty it would
1720 be OK to leave the symbol set to 0 (or any random number), because
1721 the appropriate register should never be used. */
1722 defaults.flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
1723 | SEC_IN_MEMORY);
1725 switch (which)
1727 default:
1728 (*_bfd_error_handler) (_("%s: Unknown special linker type %d"),
1729 bfd_get_filename (abfd),
1730 (int) which);
1732 bfd_set_error (bfd_error_bad_value);
1733 return (elf_linker_section_t *) 0;
1735 case LINKER_SECTION_SDATA: /* .sdata/.sbss section */
1736 defaults.name = ".sdata";
1737 defaults.rel_name = ".rela.sdata";
1738 defaults.bss_name = ".sbss";
1739 defaults.sym_name = "_SDA_BASE_";
1740 defaults.sym_offset = 32768;
1741 break;
1743 case LINKER_SECTION_SDATA2: /* .sdata2/.sbss2 section */
1744 defaults.name = ".sdata2";
1745 defaults.rel_name = ".rela.sdata2";
1746 defaults.bss_name = ".sbss2";
1747 defaults.sym_name = "_SDA2_BASE_";
1748 defaults.sym_offset = 32768;
1749 defaults.flags |= SEC_READONLY;
1750 break;
1753 lsect = _bfd_elf_create_linker_section (abfd, info, which, &defaults);
1756 return lsect;
1759 /* If we have a non-zero sized .sbss2 or .PPC.EMB.sbss0 sections, we
1760 need to bump up the number of section headers. */
1762 static int
1763 ppc_elf_additional_program_headers (abfd)
1764 bfd *abfd;
1766 asection *s;
1767 int ret;
1769 ret = 0;
1771 s = bfd_get_section_by_name (abfd, ".interp");
1772 if (s != NULL)
1773 ++ret;
1775 s = bfd_get_section_by_name (abfd, ".sbss2");
1776 if (s != NULL && (s->flags & SEC_LOAD) != 0 && s->_raw_size > 0)
1777 ++ret;
1779 s = bfd_get_section_by_name (abfd, ".PPC.EMB.sbss0");
1780 if (s != NULL && (s->flags & SEC_LOAD) != 0 && s->_raw_size > 0)
1781 ++ret;
1783 return ret;
1786 /* Modify the segment map if needed. */
1788 static bfd_boolean
1789 ppc_elf_modify_segment_map (abfd)
1790 bfd *abfd ATTRIBUTE_UNUSED;
1792 return TRUE;
1795 /* The powerpc .got has a blrl instruction in it. Mark it executable. */
1797 static asection *
1798 ppc_elf_create_got (abfd, info)
1799 bfd *abfd;
1800 struct bfd_link_info *info;
1802 register asection *s;
1803 flagword flags;
1805 if (!_bfd_elf_create_got_section (abfd, info))
1806 return NULL;
1808 s = bfd_get_section_by_name (abfd, ".got");
1809 if (s == NULL)
1810 abort ();
1812 flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_HAS_CONTENTS | SEC_IN_MEMORY
1813 | SEC_LINKER_CREATED);
1814 if (!bfd_set_section_flags (abfd, s, flags))
1815 return NULL;
1816 return s;
1819 /* We have to create .dynsbss and .rela.sbss here so that they get mapped
1820 to output sections (just like _bfd_elf_create_dynamic_sections has
1821 to create .dynbss and .rela.bss). */
1823 static bfd_boolean
1824 ppc_elf_create_dynamic_sections (abfd, info)
1825 bfd *abfd;
1826 struct bfd_link_info *info;
1828 register asection *s;
1829 flagword flags;
1831 if (!ppc_elf_create_got (abfd, info))
1832 return FALSE;
1834 if (!_bfd_elf_create_dynamic_sections (abfd, info))
1835 return FALSE;
1837 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
1838 | SEC_LINKER_CREATED);
1840 s = bfd_make_section (abfd, ".dynsbss");
1841 if (s == NULL
1842 || ! bfd_set_section_flags (abfd, s, SEC_ALLOC))
1843 return FALSE;
1845 if (! info->shared)
1847 s = bfd_make_section (abfd, ".rela.sbss");
1848 if (s == NULL
1849 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
1850 || ! bfd_set_section_alignment (abfd, s, 2))
1851 return FALSE;
1854 s = bfd_get_section_by_name (abfd, ".plt");
1855 if (s == NULL)
1856 abort ();
1858 flags = SEC_ALLOC | SEC_CODE | SEC_IN_MEMORY | SEC_LINKER_CREATED;
1859 return bfd_set_section_flags (abfd, s, flags);
1862 /* Adjust a symbol defined by a dynamic object and referenced by a
1863 regular object. The current definition is in some section of the
1864 dynamic object, but we're not including those sections. We have to
1865 change the definition to something the rest of the link can
1866 understand. */
1868 static bfd_boolean
1869 ppc_elf_adjust_dynamic_symbol (info, h)
1870 struct bfd_link_info *info;
1871 struct elf_link_hash_entry *h;
1873 bfd *dynobj = elf_hash_table (info)->dynobj;
1874 asection *s;
1875 unsigned int power_of_two;
1876 bfd_vma plt_offset;
1878 #ifdef DEBUG
1879 fprintf (stderr, "ppc_elf_adjust_dynamic_symbol called for %s\n", h->root.root.string);
1880 #endif
1882 /* Make sure we know what is going on here. */
1883 BFD_ASSERT (dynobj != NULL
1884 && ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT)
1885 || h->weakdef != NULL
1886 || ((h->elf_link_hash_flags
1887 & ELF_LINK_HASH_DEF_DYNAMIC) != 0
1888 && (h->elf_link_hash_flags
1889 & ELF_LINK_HASH_REF_REGULAR) != 0
1890 && (h->elf_link_hash_flags
1891 & ELF_LINK_HASH_DEF_REGULAR) == 0)));
1893 /* If this is a function, put it in the procedure linkage table. We
1894 will fill in the contents of the procedure linkage table later,
1895 when we know the address of the .got section. */
1896 if (h->type == STT_FUNC
1897 || (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0)
1899 if (! elf_hash_table (info)->dynamic_sections_created
1900 || SYMBOL_CALLS_LOCAL (info, h)
1901 || (info->shared && h->plt.refcount <= 0))
1903 /* A PLT entry is not required/allowed when:
1905 1. We are not using ld.so; because then the PLT entry
1906 can't be set up, so we can't use one.
1908 2. We know for certain that a call to this symbol
1909 will go to this object.
1911 3. GC has rendered the entry unused.
1912 Note, however, that in an executable all references to the
1913 symbol go to the PLT, so we can't turn it off in that case.
1914 ??? The correct thing to do here is to reference count
1915 all uses of the symbol, not just those to the GOT or PLT. */
1916 h->plt.offset = (bfd_vma) -1;
1917 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
1918 return TRUE;
1921 /* Make sure this symbol is output as a dynamic symbol. */
1922 if (h->dynindx == -1)
1924 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
1925 return FALSE;
1927 BFD_ASSERT (h->dynindx != -1);
1929 s = bfd_get_section_by_name (dynobj, ".plt");
1930 BFD_ASSERT (s != NULL);
1932 /* If this is the first .plt entry, make room for the special
1933 first entry. */
1934 if (s->_raw_size == 0)
1935 s->_raw_size += PLT_INITIAL_ENTRY_SIZE;
1937 /* The PowerPC PLT is actually composed of two parts, the first part
1938 is 2 words (for a load and a jump), and then there is a remaining
1939 word available at the end. */
1940 plt_offset = (PLT_INITIAL_ENTRY_SIZE
1941 + (PLT_SLOT_SIZE
1942 * ((s->_raw_size - PLT_INITIAL_ENTRY_SIZE)
1943 / PLT_ENTRY_SIZE)));
1945 /* If this symbol is not defined in a regular file, and we are
1946 not generating a shared library, then set the symbol to this
1947 location in the .plt. This is required to make function
1948 pointers compare as equal between the normal executable and
1949 the shared library. */
1950 if (! info->shared
1951 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
1953 h->root.u.def.section = s;
1954 h->root.u.def.value = plt_offset;
1957 h->plt.offset = plt_offset;
1959 /* Make room for this entry. After the 8192nd entry, room
1960 for two entries is allocated. */
1961 if ((s->_raw_size - PLT_INITIAL_ENTRY_SIZE) / PLT_ENTRY_SIZE
1962 >= PLT_NUM_SINGLE_ENTRIES)
1963 s->_raw_size += 2 * PLT_ENTRY_SIZE;
1964 else
1965 s->_raw_size += PLT_ENTRY_SIZE;
1967 /* We also need to make an entry in the .rela.plt section. */
1968 s = bfd_get_section_by_name (dynobj, ".rela.plt");
1969 BFD_ASSERT (s != NULL);
1970 s->_raw_size += sizeof (Elf32_External_Rela);
1972 return TRUE;
1974 else
1975 h->plt.offset = (bfd_vma) -1;
1977 /* If this is a weak symbol, and there is a real definition, the
1978 processor independent code will have arranged for us to see the
1979 real definition first, and we can just use the same value. */
1980 if (h->weakdef != NULL)
1982 BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined
1983 || h->weakdef->root.type == bfd_link_hash_defweak);
1984 h->root.u.def.section = h->weakdef->root.u.def.section;
1985 h->root.u.def.value = h->weakdef->root.u.def.value;
1986 return TRUE;
1989 /* This is a reference to a symbol defined by a dynamic object which
1990 is not a function. */
1992 /* If we are creating a shared library, we must presume that the
1993 only references to the symbol are via the global offset table.
1994 For such cases we need not do anything here; the relocations will
1995 be handled correctly by relocate_section. */
1996 if (info->shared)
1997 return TRUE;
1999 /* We must allocate the symbol in our .dynbss section, which will
2000 become part of the .bss section of the executable. There will be
2001 an entry for this symbol in the .dynsym section. The dynamic
2002 object will contain position independent code, so all references
2003 from the dynamic object to this symbol will go through the global
2004 offset table. The dynamic linker will use the .dynsym entry to
2005 determine the address it must put in the global offset table, so
2006 both the dynamic object and the regular object will refer to the
2007 same memory location for the variable.
2009 Of course, if the symbol is sufficiently small, we must instead
2010 allocate it in .sbss. FIXME: It would be better to do this if and
2011 only if there were actually SDAREL relocs for that symbol. */
2013 if (h->size <= elf_gp_size (dynobj))
2014 s = bfd_get_section_by_name (dynobj, ".dynsbss");
2015 else
2016 s = bfd_get_section_by_name (dynobj, ".dynbss");
2017 BFD_ASSERT (s != NULL);
2019 /* We must generate a R_PPC_COPY reloc to tell the dynamic linker to
2020 copy the initial value out of the dynamic object and into the
2021 runtime process image. We need to remember the offset into the
2022 .rela.bss section we are going to use. */
2023 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
2025 asection *srel;
2027 if (h->size <= elf_gp_size (dynobj))
2028 srel = bfd_get_section_by_name (dynobj, ".rela.sbss");
2029 else
2030 srel = bfd_get_section_by_name (dynobj, ".rela.bss");
2031 BFD_ASSERT (srel != NULL);
2032 srel->_raw_size += sizeof (Elf32_External_Rela);
2033 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_COPY;
2036 /* We need to figure out the alignment required for this symbol. I
2037 have no idea how ELF linkers handle this. */
2038 power_of_two = bfd_log2 (h->size);
2039 if (power_of_two > 4)
2040 power_of_two = 4;
2042 /* Apply the required alignment. */
2043 s->_raw_size = BFD_ALIGN (s->_raw_size,
2044 (bfd_size_type) (1 << power_of_two));
2045 if (power_of_two > bfd_get_section_alignment (dynobj, s))
2047 if (! bfd_set_section_alignment (dynobj, s, power_of_two))
2048 return FALSE;
2051 /* Define the symbol as being at this point in the section. */
2052 h->root.u.def.section = s;
2053 h->root.u.def.value = s->_raw_size;
2055 /* Increment the section size to make room for the symbol. */
2056 s->_raw_size += h->size;
2058 return TRUE;
2061 /* Allocate space in associated reloc sections for dynamic relocs. */
2063 static bfd_boolean
2064 allocate_dynrelocs (h, info)
2065 struct elf_link_hash_entry *h;
2066 PTR info ATTRIBUTE_UNUSED;
2068 struct ppc_elf_dyn_relocs *p;
2070 if (h->root.type == bfd_link_hash_indirect)
2071 return TRUE;
2073 if (h->root.type == bfd_link_hash_warning)
2074 /* When warning symbols are created, they **replace** the "real"
2075 entry in the hash table, thus we never get to see the real
2076 symbol in a hash traversal. So look at it now. */
2077 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2079 for (p = ppc_elf_hash_entry (h)->dyn_relocs; p != NULL; p = p->next)
2081 asection *sreloc = elf_section_data (p->sec)->sreloc;
2082 sreloc->_raw_size += p->count * sizeof (Elf32_External_Rela);
2085 return TRUE;
2088 /* Find any dynamic relocs that apply to read-only sections. */
2090 static bfd_boolean
2091 readonly_dynrelocs (h, info)
2092 struct elf_link_hash_entry *h;
2093 PTR info;
2095 struct ppc_elf_dyn_relocs *p;
2097 if (h->root.type == bfd_link_hash_indirect)
2098 return TRUE;
2100 if (h->root.type == bfd_link_hash_warning)
2101 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2103 for (p = ppc_elf_hash_entry (h)->dyn_relocs; p != NULL; p = p->next)
2105 asection *s = p->sec->output_section;
2107 if (s != NULL
2108 && ((s->flags & (SEC_READONLY | SEC_ALLOC))
2109 == (SEC_READONLY | SEC_ALLOC)))
2111 ((struct bfd_link_info *) info)->flags |= DF_TEXTREL;
2113 /* Not an error, just cut short the traversal. */
2114 return FALSE;
2117 return TRUE;
2120 /* Set the sizes of the dynamic sections. */
2122 static bfd_boolean
2123 ppc_elf_size_dynamic_sections (output_bfd, info)
2124 bfd *output_bfd ATTRIBUTE_UNUSED;
2125 struct bfd_link_info *info;
2127 bfd *dynobj;
2128 asection *s;
2129 bfd_boolean plt;
2130 bfd_boolean relocs;
2131 bfd *ibfd;
2133 #ifdef DEBUG
2134 fprintf (stderr, "ppc_elf_size_dynamic_sections called\n");
2135 #endif
2137 dynobj = elf_hash_table (info)->dynobj;
2138 BFD_ASSERT (dynobj != NULL);
2140 if (elf_hash_table (info)->dynamic_sections_created)
2142 /* Set the contents of the .interp section to the interpreter. */
2143 if (! info->shared)
2145 s = bfd_get_section_by_name (dynobj, ".interp");
2146 BFD_ASSERT (s != NULL);
2147 s->_raw_size = sizeof ELF_DYNAMIC_INTERPRETER;
2148 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
2151 else
2153 /* We may have created entries in the .rela.got, .rela.sdata, and
2154 .rela.sdata2 sections. However, if we are not creating the
2155 dynamic sections, we will not actually use these entries. Reset
2156 the size of .rela.got, et al, which will cause it to get
2157 stripped from the output file below. */
2158 static char *rela_sections[] = { ".rela.got", ".rela.sdata",
2159 ".rela.sdata2", ".rela.sbss",
2160 (char *) 0 };
2161 char **p;
2163 for (p = rela_sections; *p != (char *) 0; p++)
2165 s = bfd_get_section_by_name (dynobj, *p);
2166 if (s != NULL)
2167 s->_raw_size = 0;
2171 /* Allocate space for local sym dynamic relocs. */
2172 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
2174 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
2175 continue;
2177 for (s = ibfd->sections; s != NULL; s = s->next)
2179 struct ppc_elf_dyn_relocs *p;
2181 for (p = ((struct ppc_elf_dyn_relocs *)
2182 elf_section_data (s)->local_dynrel);
2183 p != NULL;
2184 p = p->next)
2186 if (!bfd_is_abs_section (p->sec)
2187 && bfd_is_abs_section (p->sec->output_section))
2189 /* Input section has been discarded, either because
2190 it is a copy of a linkonce section or due to
2191 linker script /DISCARD/, so we'll be discarding
2192 the relocs too. */
2194 else if (p->count != 0)
2196 elf_section_data (p->sec)->sreloc->_raw_size
2197 += p->count * sizeof (Elf32_External_Rela);
2198 if ((p->sec->output_section->flags
2199 & (SEC_READONLY | SEC_ALLOC))
2200 == (SEC_READONLY | SEC_ALLOC))
2201 info->flags |= DF_TEXTREL;
2207 /* Allocate space for global sym dynamic relocs. */
2208 elf_link_hash_traverse (elf_hash_table (info), allocate_dynrelocs, NULL);
2210 /* The check_relocs and adjust_dynamic_symbol entry points have
2211 determined the sizes of the various dynamic sections. Allocate
2212 memory for them. */
2213 plt = FALSE;
2214 relocs = FALSE;
2215 for (s = dynobj->sections; s != NULL; s = s->next)
2217 const char *name;
2218 bfd_boolean strip;
2220 if ((s->flags & SEC_LINKER_CREATED) == 0)
2221 continue;
2223 /* It's OK to base decisions on the section name, because none
2224 of the dynobj section names depend upon the input files. */
2225 name = bfd_get_section_name (dynobj, s);
2227 strip = FALSE;
2229 if (strcmp (name, ".plt") == 0)
2231 if (s->_raw_size == 0)
2233 /* Strip this section if we don't need it; see the
2234 comment below. */
2235 strip = TRUE;
2237 else
2239 /* Remember whether there is a PLT. */
2240 plt = TRUE;
2243 else if (strncmp (name, ".rela", 5) == 0)
2245 if (s->_raw_size == 0)
2247 /* If we don't need this section, strip it from the
2248 output file. This is mostly to handle .rela.bss and
2249 .rela.plt. We must create both sections in
2250 create_dynamic_sections, because they must be created
2251 before the linker maps input sections to output
2252 sections. The linker does that before
2253 adjust_dynamic_symbol is called, and it is that
2254 function which decides whether anything needs to go
2255 into these sections. */
2256 strip = TRUE;
2258 else
2260 /* Remember whether there are any relocation sections. */
2261 relocs = TRUE;
2263 /* We use the reloc_count field as a counter if we need
2264 to copy relocs into the output file. */
2265 s->reloc_count = 0;
2268 else if (strcmp (name, ".got") != 0
2269 && strcmp (name, ".sdata") != 0
2270 && strcmp (name, ".sdata2") != 0)
2272 /* It's not one of our sections, so don't allocate space. */
2273 continue;
2276 if (strip)
2278 _bfd_strip_section_from_output (info, s);
2279 continue;
2282 /* Allocate memory for the section contents. */
2283 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->_raw_size);
2284 if (s->contents == NULL && s->_raw_size != 0)
2285 return FALSE;
2288 if (elf_hash_table (info)->dynamic_sections_created)
2290 /* Add some entries to the .dynamic section. We fill in the
2291 values later, in ppc_elf_finish_dynamic_sections, but we
2292 must add the entries now so that we get the correct size for
2293 the .dynamic section. The DT_DEBUG entry is filled in by the
2294 dynamic linker and used by the debugger. */
2295 #define add_dynamic_entry(TAG, VAL) \
2296 bfd_elf32_add_dynamic_entry (info, (bfd_vma) (TAG), (bfd_vma) (VAL))
2298 if (!info->shared)
2300 if (!add_dynamic_entry (DT_DEBUG, 0))
2301 return FALSE;
2304 if (plt)
2306 if (!add_dynamic_entry (DT_PLTGOT, 0)
2307 || !add_dynamic_entry (DT_PLTRELSZ, 0)
2308 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
2309 || !add_dynamic_entry (DT_JMPREL, 0))
2310 return FALSE;
2313 if (relocs)
2315 if (!add_dynamic_entry (DT_RELA, 0)
2316 || !add_dynamic_entry (DT_RELASZ, 0)
2317 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf32_External_Rela)))
2318 return FALSE;
2321 /* If any dynamic relocs apply to a read-only section, then we
2322 need a DT_TEXTREL entry. */
2323 if ((info->flags & DF_TEXTREL) == 0)
2324 elf_link_hash_traverse (elf_hash_table (info), readonly_dynrelocs,
2325 (PTR) info);
2327 if ((info->flags & DF_TEXTREL) != 0)
2329 if (!add_dynamic_entry (DT_TEXTREL, 0))
2330 return FALSE;
2333 #undef add_dynamic_entry
2335 return TRUE;
2338 /* Look through the relocs for a section during the first phase, and
2339 allocate space in the global offset table or procedure linkage
2340 table. */
2342 static bfd_boolean
2343 ppc_elf_check_relocs (abfd, info, sec, relocs)
2344 bfd *abfd;
2345 struct bfd_link_info *info;
2346 asection *sec;
2347 const Elf_Internal_Rela *relocs;
2349 bfd *dynobj;
2350 Elf_Internal_Shdr *symtab_hdr;
2351 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
2352 const Elf_Internal_Rela *rel;
2353 const Elf_Internal_Rela *rel_end;
2354 bfd_signed_vma *local_got_refcounts;
2355 elf_linker_section_t *sdata;
2356 elf_linker_section_t *sdata2;
2357 asection *sreloc;
2358 asection *sgot = NULL;
2359 asection *srelgot = NULL;
2361 if (info->relocateable)
2362 return TRUE;
2364 #ifdef DEBUG
2365 fprintf (stderr, "ppc_elf_check_relocs called for section %s in %s\n",
2366 bfd_get_section_name (abfd, sec),
2367 bfd_archive_filename (abfd));
2368 #endif
2370 /* Create the linker generated sections all the time so that the
2371 special symbols are created. */
2373 if ((sdata = elf_linker_section (abfd, LINKER_SECTION_SDATA)) == NULL)
2375 sdata = ppc_elf_create_linker_section (abfd, info, LINKER_SECTION_SDATA);
2376 if (!sdata)
2377 return FALSE;
2380 if ((sdata2 = elf_linker_section (abfd, LINKER_SECTION_SDATA2)) == NULL)
2382 sdata2 = ppc_elf_create_linker_section (abfd, info, LINKER_SECTION_SDATA2);
2383 if (!sdata2)
2384 return FALSE;
2387 dynobj = elf_hash_table (info)->dynobj;
2388 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2389 local_got_refcounts = elf_local_got_refcounts (abfd);
2391 sym_hashes = elf_sym_hashes (abfd);
2392 sym_hashes_end = sym_hashes + symtab_hdr->sh_size/sizeof (Elf32_External_Sym);
2393 if (!elf_bad_symtab (abfd))
2394 sym_hashes_end -= symtab_hdr->sh_info;
2396 sreloc = NULL;
2398 rel_end = relocs + sec->reloc_count;
2399 for (rel = relocs; rel < rel_end; rel++)
2401 unsigned long r_symndx;
2402 struct elf_link_hash_entry *h;
2404 r_symndx = ELF32_R_SYM (rel->r_info);
2405 if (r_symndx < symtab_hdr->sh_info)
2406 h = NULL;
2407 else
2408 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
2410 /* If a relocation refers to _GLOBAL_OFFSET_TABLE_, create the .got.
2411 This shows up in particular in an R_PPC_ADDR32 in the eabi
2412 startup code. */
2413 if (h && strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
2415 if (sgot == NULL)
2417 if (dynobj == NULL)
2418 elf_hash_table (info)->dynobj = dynobj = abfd;
2419 sgot = ppc_elf_create_got (dynobj, info);
2420 if (sgot == NULL)
2421 return FALSE;
2425 switch (ELF32_R_TYPE (rel->r_info))
2427 /* GOT16 relocations */
2428 case R_PPC_GOT16:
2429 case R_PPC_GOT16_LO:
2430 case R_PPC_GOT16_HI:
2431 case R_PPC_GOT16_HA:
2432 /* This symbol requires a global offset table entry. */
2434 if (sgot == NULL)
2436 if (dynobj == NULL)
2437 elf_hash_table (info)->dynobj = dynobj = abfd;
2438 sgot = ppc_elf_create_got (dynobj, info);
2439 if (sgot == NULL)
2440 return FALSE;
2443 if (srelgot == NULL
2444 && (h != NULL || info->shared))
2446 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
2447 if (srelgot == NULL)
2449 srelgot = bfd_make_section (dynobj, ".rela.got");
2450 if (srelgot == NULL
2451 || ! bfd_set_section_flags (dynobj, srelgot,
2452 (SEC_ALLOC
2453 | SEC_LOAD
2454 | SEC_HAS_CONTENTS
2455 | SEC_IN_MEMORY
2456 | SEC_LINKER_CREATED
2457 | SEC_READONLY))
2458 || ! bfd_set_section_alignment (dynobj, srelgot, 2))
2459 return FALSE;
2463 if (h != NULL)
2465 if (h->got.refcount == 0)
2467 /* Make sure this symbol is output as a dynamic symbol. */
2468 if (h->dynindx == -1)
2469 if (!bfd_elf32_link_record_dynamic_symbol (info, h))
2470 return FALSE;
2472 /* Allocate space in the .got. */
2473 sgot->_raw_size += 4;
2474 /* Allocate relocation space. */
2475 srelgot->_raw_size += sizeof (Elf32_External_Rela);
2477 h->got.refcount++;
2479 else
2481 /* This is a global offset table entry for a local symbol. */
2482 if (local_got_refcounts == NULL)
2484 bfd_size_type size;
2486 size = symtab_hdr->sh_info;
2487 size *= sizeof (bfd_signed_vma);
2488 local_got_refcounts
2489 = (bfd_signed_vma *) bfd_zalloc (abfd, size);
2490 if (local_got_refcounts == NULL)
2491 return FALSE;
2492 elf_local_got_refcounts (abfd) = local_got_refcounts;
2494 if (local_got_refcounts[r_symndx] == 0)
2496 sgot->_raw_size += 4;
2498 /* If we are generating a shared object, we need to
2499 output a R_PPC_RELATIVE reloc so that the
2500 dynamic linker can adjust this GOT entry. */
2501 if (info->shared)
2502 srelgot->_raw_size += sizeof (Elf32_External_Rela);
2504 local_got_refcounts[r_symndx]++;
2506 break;
2508 /* Indirect .sdata relocation */
2509 case R_PPC_EMB_SDAI16:
2510 if (info->shared)
2512 ((*_bfd_error_handler)
2513 (_("%s: relocation %s cannot be used when making a shared object"),
2514 bfd_archive_filename (abfd), "R_PPC_EMB_SDAI16"));
2515 return FALSE;
2518 if (srelgot == NULL && (h != NULL || info->shared))
2520 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
2521 if (srelgot == NULL)
2523 srelgot = bfd_make_section (dynobj, ".rela.got");
2524 if (srelgot == NULL
2525 || ! bfd_set_section_flags (dynobj, srelgot,
2526 (SEC_ALLOC
2527 | SEC_LOAD
2528 | SEC_HAS_CONTENTS
2529 | SEC_IN_MEMORY
2530 | SEC_LINKER_CREATED
2531 | SEC_READONLY))
2532 || ! bfd_set_section_alignment (dynobj, srelgot, 2))
2533 return FALSE;
2537 if (!bfd_elf32_create_pointer_linker_section (abfd, info, sdata, h, rel))
2538 return FALSE;
2540 break;
2542 /* Indirect .sdata2 relocation */
2543 case R_PPC_EMB_SDA2I16:
2544 if (info->shared)
2546 ((*_bfd_error_handler)
2547 (_("%s: relocation %s cannot be used when making a shared object"),
2548 bfd_archive_filename (abfd), "R_PPC_EMB_SDA2I16"));
2549 return FALSE;
2552 if (srelgot == NULL && (h != NULL || info->shared))
2554 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
2555 if (srelgot == NULL)
2557 srelgot = bfd_make_section (dynobj, ".rela.got");
2558 if (srelgot == NULL
2559 || ! bfd_set_section_flags (dynobj, srelgot,
2560 (SEC_ALLOC
2561 | SEC_LOAD
2562 | SEC_HAS_CONTENTS
2563 | SEC_IN_MEMORY
2564 | SEC_LINKER_CREATED
2565 | SEC_READONLY))
2566 || ! bfd_set_section_alignment (dynobj, srelgot, 2))
2567 return FALSE;
2571 if (!bfd_elf32_create_pointer_linker_section (abfd, info, sdata2, h, rel))
2572 return FALSE;
2574 break;
2576 case R_PPC_SDAREL16:
2577 case R_PPC_EMB_SDA2REL:
2578 case R_PPC_EMB_SDA21:
2579 if (info->shared)
2581 ((*_bfd_error_handler)
2582 (_("%s: relocation %s cannot be used when making a shared object"),
2583 bfd_archive_filename (abfd),
2584 ppc_elf_howto_table[(int) ELF32_R_TYPE (rel->r_info)]->name));
2585 return FALSE;
2587 break;
2589 case R_PPC_PLT32:
2590 case R_PPC_PLTREL24:
2591 case R_PPC_PLT16_LO:
2592 case R_PPC_PLT16_HI:
2593 case R_PPC_PLT16_HA:
2594 #ifdef DEBUG
2595 fprintf (stderr, "Reloc requires a PLT entry\n");
2596 #endif
2597 /* This symbol requires a procedure linkage table entry. We
2598 actually build the entry in adjust_dynamic_symbol,
2599 because this might be a case of linking PIC code without
2600 linking in any dynamic objects, in which case we don't
2601 need to generate a procedure linkage table after all. */
2603 if (h == NULL)
2605 /* It does not make sense to have a procedure linkage
2606 table entry for a local symbol. */
2607 bfd_set_error (bfd_error_bad_value);
2608 return FALSE;
2611 /* Make sure this symbol is output as a dynamic symbol. */
2612 if (h->dynindx == -1)
2614 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
2615 return FALSE;
2617 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
2618 h->plt.refcount++;
2619 break;
2621 /* The following relocations don't need to propagate the
2622 relocation if linking a shared object since they are
2623 section relative. */
2624 case R_PPC_SECTOFF:
2625 case R_PPC_SECTOFF_LO:
2626 case R_PPC_SECTOFF_HI:
2627 case R_PPC_SECTOFF_HA:
2628 break;
2630 /* This refers only to functions defined in the shared library */
2631 case R_PPC_LOCAL24PC:
2632 break;
2634 /* This relocation describes the C++ object vtable hierarchy.
2635 Reconstruct it for later use during GC. */
2636 case R_PPC_GNU_VTINHERIT:
2637 if (!_bfd_elf32_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
2638 return FALSE;
2639 break;
2641 /* This relocation describes which C++ vtable entries are actually
2642 used. Record for later use during GC. */
2643 case R_PPC_GNU_VTENTRY:
2644 if (!_bfd_elf32_gc_record_vtentry (abfd, sec, h, rel->r_addend))
2645 return FALSE;
2646 break;
2648 /* When creating a shared object, we must copy these
2649 relocs into the output file. We create a reloc
2650 section in dynobj and make room for the reloc. */
2651 case R_PPC_REL24:
2652 case R_PPC_REL14:
2653 case R_PPC_REL14_BRTAKEN:
2654 case R_PPC_REL14_BRNTAKEN:
2655 case R_PPC_REL32:
2656 if (h == NULL
2657 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0
2658 || SYMBOL_REFERENCES_LOCAL (info, h))
2659 break;
2660 /* fall through */
2662 default:
2663 if (info->shared)
2665 struct ppc_elf_dyn_relocs *p;
2666 struct ppc_elf_dyn_relocs **head;
2668 #ifdef DEBUG
2669 fprintf (stderr, "ppc_elf_check_relocs need to create relocation for %s\n",
2670 (h && h->root.root.string) ? h->root.root.string : "<unknown>");
2671 #endif
2672 if (sreloc == NULL)
2674 const char *name;
2676 name = (bfd_elf_string_from_elf_section
2677 (abfd,
2678 elf_elfheader (abfd)->e_shstrndx,
2679 elf_section_data (sec)->rel_hdr.sh_name));
2680 if (name == NULL)
2681 return FALSE;
2683 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
2684 && strcmp (bfd_get_section_name (abfd, sec),
2685 name + 5) == 0);
2687 sreloc = bfd_get_section_by_name (dynobj, name);
2688 if (sreloc == NULL)
2690 flagword flags;
2692 sreloc = bfd_make_section (dynobj, name);
2693 flags = (SEC_HAS_CONTENTS | SEC_READONLY
2694 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
2695 if ((sec->flags & SEC_ALLOC) != 0)
2696 flags |= SEC_ALLOC | SEC_LOAD;
2697 if (sreloc == NULL
2698 || ! bfd_set_section_flags (dynobj, sreloc, flags)
2699 || ! bfd_set_section_alignment (dynobj, sreloc, 2))
2700 return FALSE;
2702 elf_section_data (sec)->sreloc = sreloc;
2705 /* If this is a global symbol, we count the number of
2706 relocations we need for this symbol. */
2707 if (h != NULL)
2709 head = &ppc_elf_hash_entry (h)->dyn_relocs;
2711 else
2713 /* Track dynamic relocs needed for local syms too.
2714 We really need local syms available to do this
2715 easily. Oh well. */
2717 asection *s;
2718 s = (bfd_section_from_r_symndx
2719 (abfd, &ppc_elf_hash_table (info)->sym_sec,
2720 sec, r_symndx));
2721 if (s == NULL)
2722 return FALSE;
2724 head = ((struct ppc_elf_dyn_relocs **)
2725 &elf_section_data (s)->local_dynrel);
2728 p = *head;
2729 if (p == NULL || p->sec != sec)
2731 p = ((struct ppc_elf_dyn_relocs *)
2732 bfd_alloc (elf_hash_table (info)->dynobj, sizeof *p));
2733 if (p == NULL)
2734 return FALSE;
2735 p->next = *head;
2736 *head = p;
2737 p->sec = sec;
2738 p->count = 0;
2741 p->count++;
2744 break;
2748 return TRUE;
2751 /* Return the section that should be marked against GC for a given
2752 relocation. */
2754 static asection *
2755 ppc_elf_gc_mark_hook (sec, info, rel, h, sym)
2756 asection *sec;
2757 struct bfd_link_info *info ATTRIBUTE_UNUSED;
2758 Elf_Internal_Rela *rel;
2759 struct elf_link_hash_entry *h;
2760 Elf_Internal_Sym *sym;
2762 if (h != NULL)
2764 switch (ELF32_R_TYPE (rel->r_info))
2766 case R_PPC_GNU_VTINHERIT:
2767 case R_PPC_GNU_VTENTRY:
2768 break;
2770 default:
2771 switch (h->root.type)
2773 case bfd_link_hash_defined:
2774 case bfd_link_hash_defweak:
2775 return h->root.u.def.section;
2777 case bfd_link_hash_common:
2778 return h->root.u.c.p->section;
2780 default:
2781 break;
2785 else
2786 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
2788 return NULL;
2791 /* Update the got entry reference counts for the section being removed. */
2793 static bfd_boolean
2794 ppc_elf_gc_sweep_hook (abfd, info, sec, relocs)
2795 bfd *abfd;
2796 struct bfd_link_info *info ATTRIBUTE_UNUSED;
2797 asection *sec;
2798 const Elf_Internal_Rela *relocs;
2800 Elf_Internal_Shdr *symtab_hdr;
2801 struct elf_link_hash_entry **sym_hashes;
2802 bfd_signed_vma *local_got_refcounts;
2803 const Elf_Internal_Rela *rel, *relend;
2804 unsigned long r_symndx;
2805 struct elf_link_hash_entry *h;
2807 elf_section_data (sec)->local_dynrel = NULL;
2809 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2810 sym_hashes = elf_sym_hashes (abfd);
2811 local_got_refcounts = elf_local_got_refcounts (abfd);
2813 relend = relocs + sec->reloc_count;
2814 for (rel = relocs; rel < relend; rel++)
2815 switch (ELF32_R_TYPE (rel->r_info))
2817 case R_PPC_GOT16:
2818 case R_PPC_GOT16_LO:
2819 case R_PPC_GOT16_HI:
2820 case R_PPC_GOT16_HA:
2821 r_symndx = ELF32_R_SYM (rel->r_info);
2822 if (r_symndx >= symtab_hdr->sh_info)
2824 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
2825 if (h->got.refcount > 0)
2826 h->got.refcount--;
2828 else if (local_got_refcounts != NULL)
2830 if (local_got_refcounts[r_symndx] > 0)
2831 local_got_refcounts[r_symndx]--;
2833 break;
2835 case R_PPC_PLT32:
2836 case R_PPC_PLTREL24:
2837 case R_PPC_PLT16_LO:
2838 case R_PPC_PLT16_HI:
2839 case R_PPC_PLT16_HA:
2840 r_symndx = ELF32_R_SYM (rel->r_info);
2841 if (r_symndx >= symtab_hdr->sh_info)
2843 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
2844 if (h->plt.refcount > 0)
2845 h->plt.refcount--;
2847 /* Fall through */
2849 default:
2850 r_symndx = ELF32_R_SYM (rel->r_info);
2851 if (r_symndx >= symtab_hdr->sh_info)
2853 struct ppc_elf_dyn_relocs **pp, *p;
2855 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
2857 for (pp = &ppc_elf_hash_entry (h)->dyn_relocs;
2858 (p = *pp) != NULL;
2859 pp = &p->next)
2860 if (p->sec == sec)
2862 if (--p->count == 0)
2863 *pp = p->next;
2864 break;
2867 break;
2870 return TRUE;
2873 /* Hook called by the linker routine which adds symbols from an object
2874 file. We use it to put .comm items in .sbss, and not .bss. */
2876 static bfd_boolean
2877 ppc_elf_add_symbol_hook (abfd, info, sym, namep, flagsp, secp, valp)
2878 bfd *abfd;
2879 struct bfd_link_info *info;
2880 const Elf_Internal_Sym *sym;
2881 const char **namep ATTRIBUTE_UNUSED;
2882 flagword *flagsp ATTRIBUTE_UNUSED;
2883 asection **secp;
2884 bfd_vma *valp;
2886 if (sym->st_shndx == SHN_COMMON
2887 && !info->relocateable
2888 && sym->st_size <= elf_gp_size (abfd)
2889 && info->hash->creator->flavour == bfd_target_elf_flavour)
2891 /* Common symbols less than or equal to -G nn bytes are automatically
2892 put into .sdata. */
2893 elf_linker_section_t *sdata
2894 = ppc_elf_create_linker_section (abfd, info, LINKER_SECTION_SDATA);
2896 if (!sdata->bss_section)
2898 bfd_size_type amt;
2900 /* We don't go through bfd_make_section, because we don't
2901 want to attach this common section to DYNOBJ. The linker
2902 will move the symbols to the appropriate output section
2903 when it defines common symbols. */
2904 amt = sizeof (asection);
2905 sdata->bss_section = (asection *) bfd_zalloc (abfd, amt);
2906 if (sdata->bss_section == NULL)
2907 return FALSE;
2908 sdata->bss_section->name = sdata->bss_name;
2909 sdata->bss_section->flags = SEC_IS_COMMON;
2910 sdata->bss_section->output_section = sdata->bss_section;
2911 amt = sizeof (asymbol);
2912 sdata->bss_section->symbol = (asymbol *) bfd_zalloc (abfd, amt);
2913 amt = sizeof (asymbol *);
2914 sdata->bss_section->symbol_ptr_ptr =
2915 (asymbol **) bfd_zalloc (abfd, amt);
2916 if (sdata->bss_section->symbol == NULL
2917 || sdata->bss_section->symbol_ptr_ptr == NULL)
2918 return FALSE;
2919 sdata->bss_section->symbol->name = sdata->bss_name;
2920 sdata->bss_section->symbol->flags = BSF_SECTION_SYM;
2921 sdata->bss_section->symbol->section = sdata->bss_section;
2922 *sdata->bss_section->symbol_ptr_ptr = sdata->bss_section->symbol;
2925 *secp = sdata->bss_section;
2926 *valp = sym->st_size;
2929 return TRUE;
2932 /* Finish up dynamic symbol handling. We set the contents of various
2933 dynamic sections here. */
2935 static bfd_boolean
2936 ppc_elf_finish_dynamic_symbol (output_bfd, info, h, sym)
2937 bfd *output_bfd;
2938 struct bfd_link_info *info;
2939 struct elf_link_hash_entry *h;
2940 Elf_Internal_Sym *sym;
2942 bfd *dynobj;
2944 #ifdef DEBUG
2945 fprintf (stderr, "ppc_elf_finish_dynamic_symbol called for %s",
2946 h->root.root.string);
2947 #endif
2949 dynobj = elf_hash_table (info)->dynobj;
2950 BFD_ASSERT (dynobj != NULL);
2952 if (h->plt.offset != (bfd_vma) -1)
2954 asection *splt;
2955 asection *srela;
2956 Elf_Internal_Rela rela;
2957 bfd_byte *loc;
2958 bfd_vma reloc_index;
2960 #ifdef DEBUG
2961 fprintf (stderr, ", plt_offset = %d", h->plt.offset);
2962 #endif
2964 /* This symbol has an entry in the procedure linkage table. Set
2965 it up. */
2967 BFD_ASSERT (h->dynindx != -1);
2969 splt = bfd_get_section_by_name (dynobj, ".plt");
2970 srela = bfd_get_section_by_name (dynobj, ".rela.plt");
2971 BFD_ASSERT (splt != NULL && srela != NULL);
2973 /* We don't need to fill in the .plt. The ppc dynamic linker
2974 will fill it in. */
2976 /* Fill in the entry in the .rela.plt section. */
2977 rela.r_offset = (splt->output_section->vma
2978 + splt->output_offset
2979 + h->plt.offset);
2980 rela.r_info = ELF32_R_INFO (h->dynindx, R_PPC_JMP_SLOT);
2981 rela.r_addend = 0;
2983 reloc_index = (h->plt.offset - PLT_INITIAL_ENTRY_SIZE) / PLT_SLOT_SIZE;
2984 if (reloc_index > PLT_NUM_SINGLE_ENTRIES)
2985 reloc_index -= (reloc_index - PLT_NUM_SINGLE_ENTRIES) / 2;
2986 loc = srela->contents + reloc_index * sizeof (Elf32_External_Rela);
2987 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
2989 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
2991 /* Mark the symbol as undefined, rather than as defined in
2992 the .plt section. Leave the value alone. */
2993 sym->st_shndx = SHN_UNDEF;
2994 /* If the symbol is weak, we do need to clear the value.
2995 Otherwise, the PLT entry would provide a definition for
2996 the symbol even if the symbol wasn't defined anywhere,
2997 and so the symbol would never be NULL. */
2998 if ((h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR_NONWEAK)
2999 == 0)
3000 sym->st_value = 0;
3004 if (h->got.offset != (bfd_vma) -1)
3006 asection *sgot;
3007 asection *srela;
3008 Elf_Internal_Rela rela;
3009 bfd_byte *loc;
3011 /* This symbol has an entry in the global offset table. Set it
3012 up. */
3014 sgot = bfd_get_section_by_name (dynobj, ".got");
3015 srela = bfd_get_section_by_name (dynobj, ".rela.got");
3016 BFD_ASSERT (sgot != NULL && srela != NULL);
3018 rela.r_offset = (sgot->output_section->vma
3019 + sgot->output_offset
3020 + (h->got.offset &~ (bfd_vma) 1));
3022 /* If this is a -Bsymbolic link, and the symbol is defined
3023 locally, we just want to emit a RELATIVE reloc. The entry in
3024 the global offset table will already have been initialized in
3025 the relocate_section function. */
3026 if (info->shared
3027 && SYMBOL_REFERENCES_LOCAL (info, h))
3029 rela.r_info = ELF32_R_INFO (0, R_PPC_RELATIVE);
3030 rela.r_addend = (h->root.u.def.value
3031 + h->root.u.def.section->output_section->vma
3032 + h->root.u.def.section->output_offset);
3034 else
3036 BFD_ASSERT ((h->got.offset & 1) == 0);
3037 rela.r_info = ELF32_R_INFO (h->dynindx, R_PPC_GLOB_DAT);
3038 rela.r_addend = 0;
3041 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + h->got.offset);
3042 loc = srela->contents;
3043 loc += srela->reloc_count++ * sizeof (Elf32_External_Rela);
3044 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
3047 if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_COPY) != 0)
3049 asection *s;
3050 Elf_Internal_Rela rela;
3051 bfd_byte *loc;
3053 /* This symbols needs a copy reloc. Set it up. */
3055 #ifdef DEBUG
3056 fprintf (stderr, ", copy");
3057 #endif
3059 BFD_ASSERT (h->dynindx != -1);
3061 if (h->size <= elf_gp_size (dynobj))
3062 s = bfd_get_section_by_name (h->root.u.def.section->owner,
3063 ".rela.sbss");
3064 else
3065 s = bfd_get_section_by_name (h->root.u.def.section->owner,
3066 ".rela.bss");
3067 BFD_ASSERT (s != NULL);
3069 rela.r_offset = (h->root.u.def.value
3070 + h->root.u.def.section->output_section->vma
3071 + h->root.u.def.section->output_offset);
3072 rela.r_info = ELF32_R_INFO (h->dynindx, R_PPC_COPY);
3073 rela.r_addend = 0;
3074 loc = s->contents + s->reloc_count++ * sizeof (Elf32_External_Rela);
3075 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
3078 #ifdef DEBUG
3079 fprintf (stderr, "\n");
3080 #endif
3082 /* Mark some specially defined symbols as absolute. */
3083 if (strcmp (h->root.root.string, "_DYNAMIC") == 0
3084 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0
3085 || strcmp (h->root.root.string, "_PROCEDURE_LINKAGE_TABLE_") == 0)
3086 sym->st_shndx = SHN_ABS;
3088 return TRUE;
3091 /* Finish up the dynamic sections. */
3093 static bfd_boolean
3094 ppc_elf_finish_dynamic_sections (output_bfd, info)
3095 bfd *output_bfd;
3096 struct bfd_link_info *info;
3098 asection *sdyn;
3099 bfd *dynobj = elf_hash_table (info)->dynobj;
3100 asection *sgot = bfd_get_section_by_name (dynobj, ".got");
3102 #ifdef DEBUG
3103 fprintf (stderr, "ppc_elf_finish_dynamic_sections called\n");
3104 #endif
3106 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
3108 if (elf_hash_table (info)->dynamic_sections_created)
3110 asection *splt;
3111 Elf32_External_Dyn *dyncon, *dynconend;
3113 splt = bfd_get_section_by_name (dynobj, ".plt");
3114 BFD_ASSERT (splt != NULL && sdyn != NULL);
3116 dyncon = (Elf32_External_Dyn *) sdyn->contents;
3117 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->_raw_size);
3118 for (; dyncon < dynconend; dyncon++)
3120 Elf_Internal_Dyn dyn;
3121 const char *name;
3122 bfd_boolean size;
3124 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
3126 switch (dyn.d_tag)
3128 case DT_PLTGOT: name = ".plt"; size = FALSE; break;
3129 case DT_PLTRELSZ: name = ".rela.plt"; size = TRUE; break;
3130 case DT_JMPREL: name = ".rela.plt"; size = FALSE; break;
3131 default: name = NULL; size = FALSE; break;
3134 if (name != NULL)
3136 asection *s;
3138 s = bfd_get_section_by_name (output_bfd, name);
3139 if (s == NULL)
3140 dyn.d_un.d_val = 0;
3141 else
3143 if (! size)
3144 dyn.d_un.d_ptr = s->vma;
3145 else
3147 if (s->_cooked_size != 0)
3148 dyn.d_un.d_val = s->_cooked_size;
3149 else
3150 dyn.d_un.d_val = s->_raw_size;
3153 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
3158 /* Add a blrl instruction at _GLOBAL_OFFSET_TABLE_-4 so that a function can
3159 easily find the address of the _GLOBAL_OFFSET_TABLE_. */
3160 if (sgot)
3162 unsigned char *contents = sgot->contents;
3163 bfd_put_32 (output_bfd, (bfd_vma) 0x4e800021 /* blrl */, contents);
3165 if (sdyn == NULL)
3166 bfd_put_32 (output_bfd, (bfd_vma) 0, contents+4);
3167 else
3168 bfd_put_32 (output_bfd,
3169 sdyn->output_section->vma + sdyn->output_offset,
3170 contents+4);
3172 elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
3175 return TRUE;
3178 /* The RELOCATE_SECTION function is called by the ELF backend linker
3179 to handle the relocations for a section.
3181 The relocs are always passed as Rela structures; if the section
3182 actually uses Rel structures, the r_addend field will always be
3183 zero.
3185 This function is responsible for adjust the section contents as
3186 necessary, and (if using Rela relocs and generating a
3187 relocateable output file) adjusting the reloc addend as
3188 necessary.
3190 This function does not have to worry about setting the reloc
3191 address or the reloc symbol index.
3193 LOCAL_SYMS is a pointer to the swapped in local symbols.
3195 LOCAL_SECTIONS is an array giving the section in the input file
3196 corresponding to the st_shndx field of each local symbol.
3198 The global hash table entry for the global symbols can be found
3199 via elf_sym_hashes (input_bfd).
3201 When generating relocateable output, this function must handle
3202 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
3203 going to be the section symbol corresponding to the output
3204 section, which means that the addend must be adjusted
3205 accordingly. */
3207 static bfd_boolean
3208 ppc_elf_relocate_section (output_bfd, info, input_bfd, input_section,
3209 contents, relocs, local_syms, local_sections)
3210 bfd *output_bfd;
3211 struct bfd_link_info *info;
3212 bfd *input_bfd;
3213 asection *input_section;
3214 bfd_byte *contents;
3215 Elf_Internal_Rela *relocs;
3216 Elf_Internal_Sym *local_syms;
3217 asection **local_sections;
3219 Elf_Internal_Shdr *symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
3220 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (input_bfd);
3221 bfd *dynobj = elf_hash_table (info)->dynobj;
3222 elf_linker_section_t *sdata = NULL;
3223 elf_linker_section_t *sdata2 = NULL;
3224 Elf_Internal_Rela *rel = relocs;
3225 Elf_Internal_Rela *relend = relocs + input_section->reloc_count;
3226 asection *sreloc = NULL;
3227 asection *splt;
3228 asection *sgot;
3229 bfd_vma *local_got_offsets;
3230 bfd_boolean ret = TRUE;
3231 long insn;
3233 if (dynobj)
3235 sdata = elf_linker_section (dynobj, LINKER_SECTION_SDATA);
3236 sdata2 = elf_linker_section (dynobj, LINKER_SECTION_SDATA2);
3239 #ifdef DEBUG
3240 fprintf (stderr, "ppc_elf_relocate_section called for %s section %s, %ld relocations%s\n",
3241 bfd_archive_filename (input_bfd),
3242 bfd_section_name(input_bfd, input_section),
3243 (long) input_section->reloc_count,
3244 (info->relocateable) ? " (relocatable)" : "");
3245 #endif
3247 if (info->relocateable)
3248 return TRUE;
3250 if (!ppc_elf_howto_table[R_PPC_ADDR32])
3251 /* Initialize howto table if needed. */
3252 ppc_elf_howto_init ();
3254 local_got_offsets = elf_local_got_offsets (input_bfd);
3256 splt = sgot = NULL;
3257 if (dynobj != NULL)
3259 splt = bfd_get_section_by_name (dynobj, ".plt");
3260 sgot = bfd_get_section_by_name (dynobj, ".got");
3263 for (; rel < relend; rel++)
3265 enum elf_ppc_reloc_type r_type = (enum elf_ppc_reloc_type)ELF32_R_TYPE (rel->r_info);
3266 bfd_vma offset = rel->r_offset;
3267 bfd_vma addend = rel->r_addend;
3268 bfd_reloc_status_type r = bfd_reloc_other;
3269 Elf_Internal_Sym *sym = (Elf_Internal_Sym *) 0;
3270 asection *sec = (asection *) 0;
3271 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) 0;
3272 const char *sym_name = (const char *) 0;
3273 reloc_howto_type *howto;
3274 unsigned long r_symndx;
3275 bfd_vma relocation;
3276 int will_become_local;
3278 /* Unknown relocation handling */
3279 if ((unsigned) r_type >= (unsigned) R_PPC_max
3280 || !ppc_elf_howto_table[(int) r_type])
3282 (*_bfd_error_handler) (_("%s: unknown relocation type %d"),
3283 bfd_archive_filename (input_bfd),
3284 (int) r_type);
3286 bfd_set_error (bfd_error_bad_value);
3287 ret = FALSE;
3288 continue;
3291 howto = ppc_elf_howto_table[(int) r_type];
3292 r_symndx = ELF32_R_SYM (rel->r_info);
3294 if (r_symndx < symtab_hdr->sh_info)
3296 sym = local_syms + r_symndx;
3297 sec = local_sections[r_symndx];
3298 sym_name = "<local symbol>";
3300 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, sec, rel);
3301 addend = rel->r_addend;
3302 /* Relocs to local symbols are always resolved. */
3303 will_become_local = 1;
3305 else
3307 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
3308 while (h->root.type == bfd_link_hash_indirect
3309 || h->root.type == bfd_link_hash_warning)
3310 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3311 sym_name = h->root.root.string;
3313 /* Can this relocation be resolved immediately? */
3314 will_become_local = SYMBOL_REFERENCES_LOCAL (info, h);
3316 if (h->root.type == bfd_link_hash_defined
3317 || h->root.type == bfd_link_hash_defweak)
3319 sec = h->root.u.def.section;
3320 if (((r_type == R_PPC_PLT32
3321 || r_type == R_PPC_PLTREL24)
3322 && splt != NULL
3323 && h->plt.offset != (bfd_vma) -1)
3324 || (r_type == R_PPC_LOCAL24PC
3325 && sec->output_section == NULL)
3326 || ((r_type == R_PPC_GOT16
3327 || r_type == R_PPC_GOT16_LO
3328 || r_type == R_PPC_GOT16_HI
3329 || r_type == R_PPC_GOT16_HA)
3330 && elf_hash_table (info)->dynamic_sections_created
3331 && (! info->shared || ! will_become_local))
3332 || (info->shared
3333 && ! will_become_local
3334 && ((input_section->flags & SEC_ALLOC) != 0
3335 /* Testing SEC_DEBUGGING here may be wrong.
3336 It's here to avoid a crash when
3337 generating a shared library with DWARF
3338 debugging information. */
3339 || ((input_section->flags & SEC_DEBUGGING) != 0
3340 && (h->elf_link_hash_flags
3341 & ELF_LINK_HASH_DEF_DYNAMIC) != 0))
3342 && (r_type == R_PPC_ADDR32
3343 || r_type == R_PPC_ADDR24
3344 || r_type == R_PPC_ADDR16
3345 || r_type == R_PPC_ADDR16_LO
3346 || r_type == R_PPC_ADDR16_HI
3347 || r_type == R_PPC_ADDR16_HA
3348 || r_type == R_PPC_ADDR14
3349 || r_type == R_PPC_ADDR14_BRTAKEN
3350 || r_type == R_PPC_ADDR14_BRNTAKEN
3351 || r_type == R_PPC_COPY
3352 || r_type == R_PPC_GLOB_DAT
3353 || r_type == R_PPC_JMP_SLOT
3354 || r_type == R_PPC_UADDR32
3355 || r_type == R_PPC_UADDR16
3356 || r_type == R_PPC_SDAREL16
3357 || r_type == R_PPC_EMB_NADDR32
3358 || r_type == R_PPC_EMB_NADDR16
3359 || r_type == R_PPC_EMB_NADDR16_LO
3360 || r_type == R_PPC_EMB_NADDR16_HI
3361 || r_type == R_PPC_EMB_NADDR16_HA
3362 || r_type == R_PPC_EMB_SDAI16
3363 || r_type == R_PPC_EMB_SDA2I16
3364 || r_type == R_PPC_EMB_SDA2REL
3365 || r_type == R_PPC_EMB_SDA21
3366 || r_type == R_PPC_EMB_MRKREF
3367 || r_type == R_PPC_EMB_BIT_FLD
3368 || r_type == R_PPC_EMB_RELSDA
3369 || ((r_type == R_PPC_REL24
3370 || r_type == R_PPC_REL32
3371 || r_type == R_PPC_REL14
3372 || r_type == R_PPC_REL14_BRTAKEN
3373 || r_type == R_PPC_REL14_BRNTAKEN
3374 || r_type == R_PPC_RELATIVE)
3375 && strcmp (h->root.root.string,
3376 "_GLOBAL_OFFSET_TABLE_") != 0))))
3378 /* In these cases, we don't need the relocation
3379 value. We check specially because in some
3380 obscure cases sec->output_section will be NULL. */
3381 relocation = 0;
3383 else if (sec->output_section == NULL)
3385 (*_bfd_error_handler)
3386 (_("%s: warning: unresolvable relocation against symbol `%s' from %s section"),
3387 bfd_archive_filename (input_bfd), h->root.root.string,
3388 bfd_get_section_name (input_bfd, input_section));
3389 relocation = 0;
3391 else
3392 relocation = (h->root.u.def.value
3393 + sec->output_section->vma
3394 + sec->output_offset);
3396 else if (h->root.type == bfd_link_hash_undefweak)
3397 relocation = 0;
3398 else if (info->shared
3399 && (!info->symbolic || info->allow_shlib_undefined)
3400 && !info->no_undefined
3401 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
3402 relocation = 0;
3403 else
3405 if (! (*info->callbacks->undefined_symbol) (info,
3406 h->root.root.string,
3407 input_bfd,
3408 input_section,
3409 rel->r_offset,
3410 (!info->shared
3411 || info->no_undefined
3412 || ELF_ST_VISIBILITY (h->other))))
3413 return FALSE;
3414 relocation = 0;
3418 switch ((int) r_type)
3420 default:
3421 (*_bfd_error_handler) (_("%s: unknown relocation type %d for symbol %s"),
3422 bfd_archive_filename (input_bfd),
3423 (int) r_type, sym_name);
3425 bfd_set_error (bfd_error_bad_value);
3426 ret = FALSE;
3427 continue;
3429 case (int) R_PPC_NONE:
3430 continue;
3432 /* Relocations that need no special processing. */
3433 case (int) R_PPC_LOCAL24PC:
3434 /* It makes no sense to point a local relocation
3435 at a symbol not in this object. */
3436 if (h != NULL
3437 && (h->root.type == bfd_link_hash_defined
3438 || h->root.type == bfd_link_hash_defweak)
3439 && sec->output_section == NULL)
3441 if (! (*info->callbacks->undefined_symbol) (info,
3442 h->root.root.string,
3443 input_bfd,
3444 input_section,
3445 rel->r_offset,
3446 TRUE))
3447 return FALSE;
3448 continue;
3450 break;
3452 /* Relocations that may need to be propagated if this is a shared
3453 object. */
3454 case (int) R_PPC_REL24:
3455 case (int) R_PPC_REL32:
3456 case (int) R_PPC_REL14:
3457 /* If these relocations are not to a named symbol, they can be
3458 handled right here, no need to bother the dynamic linker. */
3459 if (h == NULL
3460 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0
3461 || SYMBOL_REFERENCES_LOCAL (info, h))
3462 break;
3463 /* fall through */
3465 /* Relocations that always need to be propagated if this is a shared
3466 object. */
3467 case (int) R_PPC_ADDR32:
3468 case (int) R_PPC_ADDR24:
3469 case (int) R_PPC_ADDR16:
3470 case (int) R_PPC_ADDR16_LO:
3471 case (int) R_PPC_ADDR16_HI:
3472 case (int) R_PPC_ADDR16_HA:
3473 case (int) R_PPC_ADDR14:
3474 case (int) R_PPC_UADDR32:
3475 case (int) R_PPC_UADDR16:
3476 if (info->shared && r_symndx != 0)
3478 Elf_Internal_Rela outrel;
3479 bfd_byte *loc;
3480 int skip;
3482 #ifdef DEBUG
3483 fprintf (stderr, "ppc_elf_relocate_section need to create relocation for %s\n",
3484 (h && h->root.root.string) ? h->root.root.string : "<unknown>");
3485 #endif
3487 /* When generating a shared object, these relocations
3488 are copied into the output file to be resolved at run
3489 time. */
3491 if (sreloc == NULL)
3493 const char *name;
3495 name = (bfd_elf_string_from_elf_section
3496 (input_bfd,
3497 elf_elfheader (input_bfd)->e_shstrndx,
3498 elf_section_data (input_section)->rel_hdr.sh_name));
3499 if (name == NULL)
3500 return FALSE;
3502 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
3503 && strcmp (bfd_get_section_name (input_bfd,
3504 input_section),
3505 name + 5) == 0);
3507 sreloc = bfd_get_section_by_name (dynobj, name);
3508 BFD_ASSERT (sreloc != NULL);
3511 skip = 0;
3513 outrel.r_offset =
3514 _bfd_elf_section_offset (output_bfd, info, input_section,
3515 rel->r_offset);
3516 if (outrel.r_offset == (bfd_vma) -1
3517 || outrel.r_offset == (bfd_vma) -2)
3518 skip = (int) outrel.r_offset;
3519 outrel.r_offset += (input_section->output_section->vma
3520 + input_section->output_offset);
3522 if (skip)
3523 memset (&outrel, 0, sizeof outrel);
3524 /* h->dynindx may be -1 if this symbol was marked to
3525 become local. */
3526 else if (! will_become_local)
3528 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
3529 outrel.r_addend = rel->r_addend;
3531 else
3533 outrel.r_addend = relocation + rel->r_addend;
3535 if (r_type == R_PPC_ADDR32)
3536 outrel.r_info = ELF32_R_INFO (0, R_PPC_RELATIVE);
3537 else
3539 long indx;
3541 if (h == NULL)
3542 sec = local_sections[r_symndx];
3543 else
3545 BFD_ASSERT (h->root.type == bfd_link_hash_defined
3546 || (h->root.type
3547 == bfd_link_hash_defweak));
3548 sec = h->root.u.def.section;
3551 if (bfd_is_abs_section (sec))
3552 indx = 0;
3553 else if (sec == NULL || sec->owner == NULL)
3555 bfd_set_error (bfd_error_bad_value);
3556 return FALSE;
3558 else
3560 asection *osec;
3562 /* We are turning this relocation into one
3563 against a section symbol. It would be
3564 proper to subtract the symbol's value,
3565 osec->vma, from the emitted reloc addend,
3566 but ld.so expects buggy relocs. */
3567 osec = sec->output_section;
3568 indx = elf_section_data (osec)->dynindx;
3569 BFD_ASSERT (indx > 0);
3570 #ifdef DEBUG
3571 if (indx <= 0)
3573 printf ("indx=%d section=%s flags=%08x name=%s\n",
3574 indx, osec->name, osec->flags,
3575 h->root.root.string);
3577 #endif
3580 outrel.r_info = ELF32_R_INFO (indx, r_type);
3584 loc = sreloc->contents;
3585 loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rela);
3586 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
3588 if (skip == -1)
3589 continue;
3591 /* This reloc will be computed at runtime. We clear the memory
3592 so that it contains predictable value. */
3593 if (! skip
3594 && ((input_section->flags & SEC_ALLOC) != 0
3595 || ELF32_R_TYPE (outrel.r_info) != R_PPC_RELATIVE))
3597 relocation = howto->pc_relative ? outrel.r_offset : 0;
3598 addend = 0;
3599 break;
3603 /* Arithmetic adjust relocations that aren't going into a
3604 shared object. */
3605 if (r_type == R_PPC_ADDR16_HA
3606 /* It's just possible that this symbol is a weak symbol
3607 that's not actually defined anywhere. In that case,
3608 'sec' would be NULL, and we should leave the symbol
3609 alone (it will be set to zero elsewhere in the link). */
3610 && sec != NULL)
3612 addend += ((relocation + addend) & 0x8000) << 1;
3614 break;
3616 /* branch taken prediction relocations */
3617 case (int) R_PPC_ADDR14_BRTAKEN:
3618 case (int) R_PPC_REL14_BRTAKEN:
3619 insn = bfd_get_32 (output_bfd, contents + offset);
3620 if ((relocation - offset) & 0x8000)
3621 insn &= ~BRANCH_PREDICT_BIT;
3622 else
3623 insn |= BRANCH_PREDICT_BIT;
3624 bfd_put_32 (output_bfd, (bfd_vma) insn, contents + offset);
3625 break;
3627 /* branch not taken predicition relocations */
3628 case (int) R_PPC_ADDR14_BRNTAKEN:
3629 case (int) R_PPC_REL14_BRNTAKEN:
3630 insn = bfd_get_32 (output_bfd, contents + offset);
3631 if ((relocation - offset) & 0x8000)
3632 insn |= BRANCH_PREDICT_BIT;
3633 else
3634 insn &= ~BRANCH_PREDICT_BIT;
3635 bfd_put_32 (output_bfd, (bfd_vma) insn, contents + offset);
3636 break;
3638 /* GOT16 relocations */
3639 case (int) R_PPC_GOT16:
3640 case (int) R_PPC_GOT16_LO:
3641 case (int) R_PPC_GOT16_HI:
3642 case (int) R_PPC_GOT16_HA:
3643 /* Relocation is to the entry for this symbol in the global
3644 offset table. */
3645 BFD_ASSERT (sgot != NULL);
3647 if (h != NULL)
3649 bfd_vma off;
3651 off = h->got.offset;
3652 BFD_ASSERT (off != (bfd_vma) -1);
3654 if (! elf_hash_table (info)->dynamic_sections_created
3655 || (info->shared
3656 && SYMBOL_REFERENCES_LOCAL (info, h)))
3658 /* This is actually a static link, or it is a
3659 -Bsymbolic link and the symbol is defined
3660 locally. We must initialize this entry in the
3661 global offset table. Since the offset must
3662 always be a multiple of 4, we use the least
3663 significant bit to record whether we have
3664 initialized it already.
3666 When doing a dynamic link, we create a .rela.got
3667 relocation entry to initialize the value. This
3668 is done in the finish_dynamic_symbol routine. */
3669 if ((off & 1) != 0)
3670 off &= ~1;
3671 else
3673 bfd_put_32 (output_bfd, relocation,
3674 sgot->contents + off);
3675 h->got.offset |= 1;
3679 relocation = sgot->output_offset + off - 4;
3681 else
3683 bfd_vma off;
3685 BFD_ASSERT (local_got_offsets != NULL
3686 && local_got_offsets[r_symndx] != (bfd_vma) -1);
3688 off = local_got_offsets[r_symndx];
3690 /* The offset must always be a multiple of 4. We use
3691 the least significant bit to record whether we have
3692 already processed this entry. */
3693 if ((off & 1) != 0)
3694 off &= ~1;
3695 else
3698 if (info->shared)
3700 asection *srelgot;
3701 Elf_Internal_Rela outrel;
3702 bfd_byte *loc;
3704 /* We need to generate a R_PPC_RELATIVE reloc
3705 for the dynamic linker. */
3706 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
3707 BFD_ASSERT (srelgot != NULL);
3709 outrel.r_offset = (sgot->output_section->vma
3710 + sgot->output_offset
3711 + off);
3712 outrel.r_info = ELF32_R_INFO (0, R_PPC_RELATIVE);
3713 outrel.r_addend = relocation;
3714 loc = srelgot->contents;
3715 loc += srelgot->reloc_count++ * sizeof (Elf32_External_Rela);
3716 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
3717 relocation = 0;
3720 bfd_put_32 (output_bfd, relocation, sgot->contents + off);
3721 local_got_offsets[r_symndx] |= 1;
3724 relocation = sgot->output_offset + off - 4;
3726 if (r_type == R_PPC_GOT16_HA)
3727 addend += ((relocation + addend) & 0x8000) << 1;
3728 break;
3730 /* Indirect .sdata relocation */
3731 case (int) R_PPC_EMB_SDAI16:
3732 BFD_ASSERT (sdata != NULL);
3733 relocation = bfd_elf32_finish_pointer_linker_section (output_bfd, input_bfd, info,
3734 sdata, h, relocation, rel,
3735 R_PPC_RELATIVE);
3736 break;
3738 /* Indirect .sdata2 relocation */
3739 case (int) R_PPC_EMB_SDA2I16:
3740 BFD_ASSERT (sdata2 != NULL);
3741 relocation = bfd_elf32_finish_pointer_linker_section (output_bfd, input_bfd, info,
3742 sdata2, h, relocation, rel,
3743 R_PPC_RELATIVE);
3744 break;
3746 /* Handle the TOC16 reloc. We want to use the offset within the .got
3747 section, not the actual VMA. This is appropriate when generating
3748 an embedded ELF object, for which the .got section acts like the
3749 AIX .toc section. */
3750 case (int) R_PPC_TOC16: /* phony GOT16 relocations */
3751 BFD_ASSERT (sec != (asection *) 0);
3752 BFD_ASSERT (bfd_is_und_section (sec)
3753 || strcmp (bfd_get_section_name (abfd, sec), ".got") == 0
3754 || strcmp (bfd_get_section_name (abfd, sec), ".cgot") == 0)
3756 addend -= sec->output_section->vma + sec->output_offset + 0x8000;
3757 break;
3759 case (int) R_PPC_PLTREL24:
3760 /* Relocation is to the entry for this symbol in the
3761 procedure linkage table. */
3762 BFD_ASSERT (h != NULL);
3764 if (h->plt.offset == (bfd_vma) -1
3765 || splt == NULL)
3767 /* We didn't make a PLT entry for this symbol. This
3768 happens when statically linking PIC code, or when
3769 using -Bsymbolic. */
3770 break;
3773 relocation = (splt->output_section->vma
3774 + splt->output_offset
3775 + h->plt.offset);
3776 break;
3778 /* relocate against _SDA_BASE_ */
3779 case (int) R_PPC_SDAREL16:
3781 const char *name;
3783 BFD_ASSERT (sec != (asection *) 0);
3784 name = bfd_get_section_name (abfd, sec->output_section);
3785 if (! ((strncmp (name, ".sdata", 6) == 0
3786 && (name[6] == 0 || name[6] == '.'))
3787 || (strncmp (name, ".sbss", 5) == 0
3788 && (name[5] == 0 || name[5] == '.'))))
3790 (*_bfd_error_handler) (_("%s: The target (%s) of a %s relocation is in the wrong output section (%s)"),
3791 bfd_archive_filename (input_bfd),
3792 sym_name,
3793 ppc_elf_howto_table[(int) r_type]->name,
3794 name);
3796 addend -= (sdata->sym_hash->root.u.def.value
3797 + sdata->sym_hash->root.u.def.section->output_section->vma
3798 + sdata->sym_hash->root.u.def.section->output_offset);
3800 break;
3802 /* relocate against _SDA2_BASE_ */
3803 case (int) R_PPC_EMB_SDA2REL:
3805 const char *name;
3807 BFD_ASSERT (sec != (asection *) 0);
3808 name = bfd_get_section_name (abfd, sec->output_section);
3809 if (! (strncmp (name, ".sdata2", 7) == 0
3810 || strncmp (name, ".sbss2", 6) == 0))
3812 (*_bfd_error_handler) (_("%s: The target (%s) of a %s relocation is in the wrong output section (%s)"),
3813 bfd_archive_filename (input_bfd),
3814 sym_name,
3815 ppc_elf_howto_table[(int) r_type]->name,
3816 name);
3818 bfd_set_error (bfd_error_bad_value);
3819 ret = FALSE;
3820 continue;
3822 addend -= (sdata2->sym_hash->root.u.def.value
3823 + sdata2->sym_hash->root.u.def.section->output_section->vma
3824 + sdata2->sym_hash->root.u.def.section->output_offset);
3826 break;
3828 /* relocate against either _SDA_BASE_, _SDA2_BASE_, or 0 */
3829 case (int) R_PPC_EMB_SDA21:
3830 case (int) R_PPC_EMB_RELSDA:
3832 const char *name;
3833 int reg;
3835 BFD_ASSERT (sec != (asection *) 0);
3836 name = bfd_get_section_name (abfd, sec->output_section);
3837 if (((strncmp (name, ".sdata", 6) == 0
3838 && (name[6] == 0 || name[6] == '.'))
3839 || (strncmp (name, ".sbss", 5) == 0
3840 && (name[5] == 0 || name[5] == '.'))))
3842 reg = 13;
3843 addend -= (sdata->sym_hash->root.u.def.value
3844 + sdata->sym_hash->root.u.def.section->output_section->vma
3845 + sdata->sym_hash->root.u.def.section->output_offset);
3848 else if (strncmp (name, ".sdata2", 7) == 0
3849 || strncmp (name, ".sbss2", 6) == 0)
3851 reg = 2;
3852 addend -= (sdata2->sym_hash->root.u.def.value
3853 + sdata2->sym_hash->root.u.def.section->output_section->vma
3854 + sdata2->sym_hash->root.u.def.section->output_offset);
3857 else if (strcmp (name, ".PPC.EMB.sdata0") == 0
3858 || strcmp (name, ".PPC.EMB.sbss0") == 0)
3860 reg = 0;
3863 else
3865 (*_bfd_error_handler) (_("%s: The target (%s) of a %s relocation is in the wrong output section (%s)"),
3866 bfd_archive_filename (input_bfd),
3867 sym_name,
3868 ppc_elf_howto_table[(int) r_type]->name,
3869 name);
3871 bfd_set_error (bfd_error_bad_value);
3872 ret = FALSE;
3873 continue;
3876 if (r_type == R_PPC_EMB_SDA21)
3877 { /* fill in register field */
3878 insn = bfd_get_32 (output_bfd, contents + offset);
3879 insn = (insn & ~RA_REGISTER_MASK) | (reg << RA_REGISTER_SHIFT);
3880 bfd_put_32 (output_bfd, (bfd_vma) insn, contents + offset);
3883 break;
3885 /* Relocate against the beginning of the section */
3886 case (int) R_PPC_SECTOFF:
3887 case (int) R_PPC_SECTOFF_LO:
3888 case (int) R_PPC_SECTOFF_HI:
3889 BFD_ASSERT (sec != (asection *) 0);
3890 addend -= sec->output_section->vma;
3891 break;
3893 case (int) R_PPC_SECTOFF_HA:
3894 BFD_ASSERT (sec != (asection *) 0);
3895 addend -= sec->output_section->vma;
3896 addend += ((relocation + addend) & 0x8000) << 1;
3897 break;
3899 /* Negative relocations */
3900 case (int) R_PPC_EMB_NADDR32:
3901 case (int) R_PPC_EMB_NADDR16:
3902 case (int) R_PPC_EMB_NADDR16_LO:
3903 case (int) R_PPC_EMB_NADDR16_HI:
3904 addend -= 2 * relocation;
3905 break;
3907 case (int) R_PPC_EMB_NADDR16_HA:
3908 addend -= 2 * relocation;
3909 addend += ((relocation + addend) & 0x8000) << 1;
3910 break;
3912 /* NOP relocation that prevents garbage collecting linkers from omitting a
3913 reference. */
3914 case (int) R_PPC_EMB_MRKREF:
3915 continue;
3917 case (int) R_PPC_COPY:
3918 case (int) R_PPC_GLOB_DAT:
3919 case (int) R_PPC_JMP_SLOT:
3920 case (int) R_PPC_RELATIVE:
3921 case (int) R_PPC_PLT32:
3922 case (int) R_PPC_PLTREL32:
3923 case (int) R_PPC_PLT16_LO:
3924 case (int) R_PPC_PLT16_HI:
3925 case (int) R_PPC_PLT16_HA:
3926 case (int) R_PPC_EMB_RELSEC16:
3927 case (int) R_PPC_EMB_RELST_LO:
3928 case (int) R_PPC_EMB_RELST_HI:
3929 case (int) R_PPC_EMB_RELST_HA:
3930 case (int) R_PPC_EMB_BIT_FLD:
3931 (*_bfd_error_handler) (_("%s: Relocation %s is not yet supported for symbol %s."),
3932 bfd_archive_filename (input_bfd),
3933 ppc_elf_howto_table[(int) r_type]->name,
3934 sym_name);
3936 bfd_set_error (bfd_error_invalid_operation);
3937 ret = FALSE;
3938 continue;
3940 case (int) R_PPC_GNU_VTINHERIT:
3941 case (int) R_PPC_GNU_VTENTRY:
3942 /* These are no-ops in the end. */
3943 continue;
3946 #ifdef DEBUG
3947 fprintf (stderr, "\ttype = %s (%d), name = %s, symbol index = %ld, offset = %ld, addend = %ld\n",
3948 howto->name,
3949 (int) r_type,
3950 sym_name,
3951 r_symndx,
3952 (long) offset,
3953 (long) addend);
3954 #endif
3956 r = _bfd_final_link_relocate (howto,
3957 input_bfd,
3958 input_section,
3959 contents,
3960 offset,
3961 relocation,
3962 addend);
3964 if (r == bfd_reloc_ok)
3966 else if (r == bfd_reloc_overflow)
3968 const char *name;
3970 if (h != NULL)
3972 if (h->root.type == bfd_link_hash_undefweak
3973 && howto->pc_relative)
3975 /* Assume this is a call protected by other code that
3976 detect the symbol is undefined. If this is the case,
3977 we can safely ignore the overflow. If not, the
3978 program is hosed anyway, and a little warning isn't
3979 going to help. */
3981 continue;
3984 name = h->root.root.string;
3986 else
3988 name = bfd_elf_string_from_elf_section (input_bfd,
3989 symtab_hdr->sh_link,
3990 sym->st_name);
3991 if (name == NULL)
3992 continue;
3993 if (*name == '\0')
3994 name = bfd_section_name (input_bfd, sec);
3997 if (! (*info->callbacks->reloc_overflow) (info,
3998 name,
3999 howto->name,
4000 (bfd_vma) 0,
4001 input_bfd,
4002 input_section,
4003 offset))
4004 return FALSE;
4006 else
4007 ret = FALSE;
4010 #ifdef DEBUG
4011 fprintf (stderr, "\n");
4012 #endif
4014 return ret;
4017 static enum elf_reloc_type_class
4018 ppc_elf_reloc_type_class (rela)
4019 const Elf_Internal_Rela *rela;
4021 switch ((int) ELF32_R_TYPE (rela->r_info))
4023 case R_PPC_RELATIVE:
4024 return reloc_class_relative;
4025 case R_PPC_REL24:
4026 case R_PPC_ADDR24:
4027 case R_PPC_JMP_SLOT:
4028 return reloc_class_plt;
4029 case R_PPC_COPY:
4030 return reloc_class_copy;
4031 default:
4032 return reloc_class_normal;
4036 /* Support for core dump NOTE sections */
4037 static bfd_boolean
4038 ppc_elf_grok_prstatus (abfd, note)
4039 bfd *abfd;
4040 Elf_Internal_Note *note;
4042 int offset;
4043 unsigned int raw_size;
4045 switch (note->descsz)
4047 default:
4048 return FALSE;
4050 case 268: /* Linux/PPC */
4051 /* pr_cursig */
4052 elf_tdata (abfd)->core_signal = bfd_get_16 (abfd, note->descdata + 12);
4054 /* pr_pid */
4055 elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, note->descdata + 24);
4057 /* pr_reg */
4058 offset = 72;
4059 raw_size = 192;
4061 break;
4064 /* Make a ".reg/999" section. */
4065 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
4066 raw_size, note->descpos + offset);
4069 static bfd_boolean
4070 ppc_elf_grok_psinfo (abfd, note)
4071 bfd *abfd;
4072 Elf_Internal_Note *note;
4074 switch (note->descsz)
4076 default:
4077 return FALSE;
4079 case 128: /* Linux/PPC elf_prpsinfo */
4080 elf_tdata (abfd)->core_program
4081 = _bfd_elfcore_strndup (abfd, note->descdata + 32, 16);
4082 elf_tdata (abfd)->core_command
4083 = _bfd_elfcore_strndup (abfd, note->descdata + 48, 80);
4086 /* Note that for some reason, a spurious space is tacked
4087 onto the end of the args in some (at least one anyway)
4088 implementations, so strip it off if it exists. */
4091 char *command = elf_tdata (abfd)->core_command;
4092 int n = strlen (command);
4094 if (0 < n && command[n - 1] == ' ')
4095 command[n - 1] = '\0';
4098 return TRUE;
4101 /* Very simple linked list structure for recording apuinfo values. */
4102 typedef struct apuinfo_list
4104 struct apuinfo_list * next;
4105 unsigned long value;
4107 apuinfo_list;
4109 static apuinfo_list * head;
4111 static void apuinfo_list_init PARAMS ((void));
4112 static void apuinfo_list_add PARAMS ((unsigned long));
4113 static unsigned apuinfo_list_length PARAMS ((void));
4114 static unsigned long apuinfo_list_element PARAMS ((unsigned long));
4115 static void apuinfo_list_finish PARAMS ((void));
4117 extern void ppc_elf_begin_write_processing
4118 PARAMS ((bfd *, struct bfd_link_info *));
4119 extern void ppc_elf_final_write_processing
4120 PARAMS ((bfd *, bfd_boolean));
4121 extern bfd_boolean ppc_elf_write_section
4122 PARAMS ((bfd *, asection *, bfd_byte *));
4126 static void
4127 apuinfo_list_init PARAMS ((void))
4129 head = NULL;
4132 static void
4133 apuinfo_list_add (value)
4134 unsigned long value;
4136 apuinfo_list * entry = head;
4138 while (entry != NULL)
4140 if (entry->value == value)
4141 return;
4142 entry = entry->next;
4145 entry = bfd_malloc (sizeof (* entry));
4146 if (entry == NULL)
4147 return;
4149 entry->value = value;
4150 entry->next = head;
4151 head = entry;
4154 static unsigned
4155 apuinfo_list_length PARAMS ((void))
4157 apuinfo_list * entry;
4158 unsigned long count;
4160 for (entry = head, count = 0;
4161 entry;
4162 entry = entry->next)
4163 ++ count;
4165 return count;
4168 static inline unsigned long
4169 apuinfo_list_element (number)
4170 unsigned long number;
4172 apuinfo_list * entry;
4174 for (entry = head;
4175 entry && number --;
4176 entry = entry->next)
4179 return entry ? entry->value : 0;
4182 static void
4183 apuinfo_list_finish PARAMS ((void))
4185 apuinfo_list * entry;
4187 for (entry = head; entry;)
4189 apuinfo_list * next = entry->next;
4190 free (entry);
4191 entry = next;
4194 head = NULL;
4197 #define APUINFO_SECTION_NAME ".PPC.EMB.apuinfo"
4198 #define APUINFO_LABEL "APUinfo"
4200 /* Scan the input BFDs and create a linked list of
4201 the APUinfo values that will need to be emitted. */
4203 void
4204 ppc_elf_begin_write_processing (abfd, link_info)
4205 bfd *abfd;
4206 struct bfd_link_info *link_info;
4208 bfd * ibfd;
4209 asection * asec;
4210 char * buffer;
4211 unsigned num_input_sections;
4212 bfd_size_type output_section_size;
4213 unsigned i;
4214 unsigned num_entries;
4215 unsigned long offset;
4216 unsigned long length;
4217 const char * error_message = NULL;
4219 if (link_info == NULL)
4220 return;
4222 /* Scan the input bfds, looking for apuinfo sections. */
4223 num_input_sections = 0;
4224 output_section_size = 0;
4226 for (ibfd = link_info->input_bfds; ibfd; ibfd = ibfd->link_next)
4228 asec = bfd_get_section_by_name (ibfd, APUINFO_SECTION_NAME);
4229 if (asec)
4231 ++ num_input_sections;
4232 output_section_size += asec->_raw_size;
4236 /* We need at least one input sections
4237 in order to make merging worthwhile. */
4238 if (num_input_sections < 1)
4239 return;
4241 /* Just make sure that the output section exists as well. */
4242 asec = bfd_get_section_by_name (abfd, APUINFO_SECTION_NAME);
4243 if (asec == NULL)
4244 return;
4246 /* Allocate a buffer for the contents of the input sections. */
4247 buffer = bfd_malloc (output_section_size);
4248 if (buffer == NULL)
4249 return;
4251 offset = 0;
4252 apuinfo_list_init ();
4254 /* Read in the input sections contents. */
4255 for (ibfd = link_info->input_bfds; ibfd; ibfd = ibfd->link_next)
4257 unsigned long datum;
4258 char * ptr;
4261 asec = bfd_get_section_by_name (ibfd, APUINFO_SECTION_NAME);
4262 if (asec == NULL)
4263 continue;
4265 length = asec->_raw_size;
4266 if (length < 24)
4268 error_message = _("corrupt or empty %s section in %s");
4269 goto fail;
4272 if (bfd_seek (ibfd, asec->filepos, SEEK_SET) != 0
4273 || (bfd_bread (buffer + offset, length, ibfd) != length))
4275 error_message = _("unable to read in %s section from %s");
4276 goto fail;
4279 /* Process the contents of the section. */
4280 ptr = buffer + offset;
4281 error_message = _("corrupt %s section in %s");
4283 /* Verify the contents of the header. Note - we have to
4284 extract the values this way in order to allow for a
4285 host whose endian-ness is different from the target. */
4286 datum = bfd_get_32 (ibfd, ptr);
4287 if (datum != sizeof APUINFO_LABEL)
4288 goto fail;
4290 datum = bfd_get_32 (ibfd, ptr + 8);
4291 if (datum != 0x2)
4292 goto fail;
4294 if (strcmp (ptr + 12, APUINFO_LABEL) != 0)
4295 goto fail;
4297 /* Get the number of apuinfo entries. */
4298 datum = bfd_get_32 (ibfd, ptr + 4);
4299 if ((datum * 4 + 20) != length)
4300 goto fail;
4302 /* Make sure that we do not run off the end of the section. */
4303 if (offset + length > output_section_size)
4304 goto fail;
4306 /* Scan the apuinfo section, building a list of apuinfo numbers. */
4307 for (i = 0; i < datum; i++)
4308 apuinfo_list_add (bfd_get_32 (ibfd, ptr + 20 + (i * 4)));
4310 /* Update the offset. */
4311 offset += length;
4314 error_message = NULL;
4316 /* Compute the size of the output section. */
4317 num_entries = apuinfo_list_length ();
4318 output_section_size = 20 + num_entries * 4;
4320 asec = bfd_get_section_by_name (abfd, APUINFO_SECTION_NAME);
4322 if (! bfd_set_section_size (abfd, asec, output_section_size))
4323 ibfd = abfd,
4324 error_message = _("warning: unable to set size of %s section in %s");
4326 fail:
4327 free (buffer);
4329 if (error_message)
4330 _bfd_error_handler (error_message, APUINFO_SECTION_NAME,
4331 bfd_archive_filename (ibfd));
4335 /* Prevent the output section from accumulating the input sections'
4336 contents. We have already stored this in our linked list structure. */
4338 bfd_boolean
4339 ppc_elf_write_section (abfd, asec, contents)
4340 bfd * abfd ATTRIBUTE_UNUSED;
4341 asection * asec;
4342 bfd_byte * contents ATTRIBUTE_UNUSED;
4344 return apuinfo_list_length () && strcmp (asec->name, APUINFO_SECTION_NAME) == 0;
4348 /* Finally we can generate the output section. */
4350 void
4351 ppc_elf_final_write_processing (abfd, linker)
4352 bfd * abfd;
4353 bfd_boolean linker ATTRIBUTE_UNUSED;
4355 bfd_byte * buffer;
4356 asection * asec;
4357 unsigned i;
4358 unsigned num_entries;
4359 bfd_size_type length;
4361 asec = bfd_get_section_by_name (abfd, APUINFO_SECTION_NAME);
4362 if (asec == NULL)
4363 return;
4365 if (apuinfo_list_length () == 0)
4366 return;
4368 length = asec->_raw_size;
4369 if (length < 20)
4370 return;
4372 buffer = bfd_malloc (length);
4373 if (buffer == NULL)
4375 _bfd_error_handler (_("failed to allocate space for new APUinfo section."));
4376 return;
4379 /* Create the apuinfo header. */
4380 num_entries = apuinfo_list_length ();
4381 bfd_put_32 (abfd, sizeof APUINFO_LABEL, buffer);
4382 bfd_put_32 (abfd, num_entries, buffer + 4);
4383 bfd_put_32 (abfd, 0x2, buffer + 8);
4384 strcpy (buffer + 12, APUINFO_LABEL);
4386 length = 20;
4387 for (i = 0; i < num_entries; i++)
4389 bfd_put_32 (abfd, apuinfo_list_element (i), buffer + length);
4390 length += 4;
4393 if (length != asec->_raw_size)
4394 _bfd_error_handler (_("failed to compute new APUinfo section."));
4396 if (! bfd_set_section_contents (abfd, asec, buffer, (file_ptr) 0, length))
4397 _bfd_error_handler (_("failed to install new APUinfo section."));
4399 free (buffer);
4401 apuinfo_list_finish ();
4404 #define TARGET_LITTLE_SYM bfd_elf32_powerpcle_vec
4405 #define TARGET_LITTLE_NAME "elf32-powerpcle"
4406 #define TARGET_BIG_SYM bfd_elf32_powerpc_vec
4407 #define TARGET_BIG_NAME "elf32-powerpc"
4408 #define ELF_ARCH bfd_arch_powerpc
4409 #define ELF_MACHINE_CODE EM_PPC
4410 #define ELF_MAXPAGESIZE 0x10000
4411 #define elf_info_to_howto ppc_elf_info_to_howto
4413 #ifdef EM_CYGNUS_POWERPC
4414 #define ELF_MACHINE_ALT1 EM_CYGNUS_POWERPC
4415 #endif
4417 #ifdef EM_PPC_OLD
4418 #define ELF_MACHINE_ALT2 EM_PPC_OLD
4419 #endif
4421 #define elf_backend_plt_not_loaded 1
4422 #define elf_backend_got_symbol_offset 4
4423 #define elf_backend_can_gc_sections 1
4424 #define elf_backend_can_refcount 1
4425 #define elf_backend_got_header_size 12
4426 #define elf_backend_plt_header_size PLT_INITIAL_ENTRY_SIZE
4427 #define elf_backend_rela_normal 1
4429 #define bfd_elf32_bfd_merge_private_bfd_data ppc_elf_merge_private_bfd_data
4430 #define bfd_elf32_bfd_relax_section ppc_elf_relax_section
4431 #define bfd_elf32_bfd_reloc_type_lookup ppc_elf_reloc_type_lookup
4432 #define bfd_elf32_bfd_set_private_flags ppc_elf_set_private_flags
4433 #define bfd_elf32_bfd_final_link _bfd_elf32_gc_common_final_link
4434 #define bfd_elf32_bfd_link_hash_table_create ppc_elf_link_hash_table_create
4436 #define elf_backend_object_p ppc_elf_object_p
4437 #define elf_backend_gc_mark_hook ppc_elf_gc_mark_hook
4438 #define elf_backend_gc_sweep_hook ppc_elf_gc_sweep_hook
4439 #define elf_backend_section_from_shdr ppc_elf_section_from_shdr
4440 #define elf_backend_relocate_section ppc_elf_relocate_section
4441 #define elf_backend_create_dynamic_sections ppc_elf_create_dynamic_sections
4442 #define elf_backend_check_relocs ppc_elf_check_relocs
4443 #define elf_backend_copy_indirect_symbol ppc_elf_copy_indirect_symbol
4444 #define elf_backend_adjust_dynamic_symbol ppc_elf_adjust_dynamic_symbol
4445 #define elf_backend_add_symbol_hook ppc_elf_add_symbol_hook
4446 #define elf_backend_size_dynamic_sections ppc_elf_size_dynamic_sections
4447 #define elf_backend_finish_dynamic_symbol ppc_elf_finish_dynamic_symbol
4448 #define elf_backend_finish_dynamic_sections ppc_elf_finish_dynamic_sections
4449 #define elf_backend_fake_sections ppc_elf_fake_sections
4450 #define elf_backend_additional_program_headers ppc_elf_additional_program_headers
4451 #define elf_backend_modify_segment_map ppc_elf_modify_segment_map
4452 #define elf_backend_grok_prstatus ppc_elf_grok_prstatus
4453 #define elf_backend_grok_psinfo ppc_elf_grok_psinfo
4454 #define elf_backend_reloc_type_class ppc_elf_reloc_type_class
4455 #define elf_backend_begin_write_processing ppc_elf_begin_write_processing
4456 #define elf_backend_final_write_processing ppc_elf_final_write_processing
4457 #define elf_backend_write_section ppc_elf_write_section
4459 #include "elf32-target.h"