1 /* BFD back-end for PowerPC Microsoft Portable Executable files.
2 Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
3 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007
4 Free Software Foundation, Inc.
6 Original version pieced together by Kim Knuttila (krk@cygnus.com)
8 There is nothing new under the sun. This file draws a lot on other
9 coff files, in particular, those for the rs/6000, alpha, mips, and
10 intel backends, and the PE work for the arm.
12 This file is part of BFD, the Binary File Descriptor library.
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 3 of the License, or
17 (at your option) any later version.
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
24 You should have received a copy of the GNU General Public License
25 along with this program; if not, write to the Free Software
26 Foundation, 51 Franklin Street - Fifth Floor,
27 Boston, MA 02110-1301, USA. */
31 - relocs generated by gas
32 - ld will link files, but they do not run.
33 - dlltool will not produce correct output in some .reloc cases, and will
34 not produce the right glue code for dll function calls. */
40 #include "coff/powerpc.h"
41 #include "coff/internal.h"
49 #define BADMAG(x) PPCBADMAG(x)
53 /* This file is compiled more than once, but we only compile the
54 final_link routine once. */
55 extern bfd_boolean ppc_bfd_coff_final_link
56 PARAMS ((bfd
*, struct bfd_link_info
*));
57 extern void dump_toc
PARAMS ((PTR
));
59 /* The toc is a set of bfd_vma fields. We use the fact that valid
60 addresses are even (i.e. the bit representing "1" is off) to allow
61 us to encode a little extra information in the field
62 - Unallocated addresses are initialized to 1.
63 - Allocated addresses are even numbers.
64 The first time we actually write a reference to the toc in the bfd,
65 we want to record that fact in a fixup file (if it is asked for), so
66 we keep track of whether or not an address has been written by marking
67 the low order bit with a "1" upon writing. */
69 #define SET_UNALLOCATED(x) ((x) = 1)
70 #define IS_UNALLOCATED(x) ((x) == 1)
72 #define IS_WRITTEN(x) ((x) & 1)
73 #define MARK_AS_WRITTEN(x) ((x) |= 1)
74 #define MAKE_ADDR_AGAIN(x) ((x) &= ~1)
76 /* Turn on this check if you suspect something amiss in the hash tables. */
79 /* Need a 7 char string for an eye catcher. */
82 #define HASH_CHECK_DCL char eye_catcher[8];
83 #define HASH_CHECK_INIT(ret) strcpy(ret->eye_catcher, EYE)
84 #define HASH_CHECK(addr) \
85 if (strcmp(addr->eye_catcher, EYE) != 0) \
88 _("File %s, line %d, Hash check failure, bad eye %8s\n"), \
89 __FILE__, __LINE__, addr->eye_catcher); \
95 #define HASH_CHECK_DCL
96 #define HASH_CHECK_INIT(ret)
97 #define HASH_CHECK(addr)
101 /* In order not to add an int to every hash table item for every coff
102 linker, we define our own hash table, derived from the coff one. */
104 /* PE linker hash table entries. */
106 struct ppc_coff_link_hash_entry
108 struct coff_link_hash_entry root
; /* First entry, as required. */
110 /* As we wonder around the relocs, we'll keep the assigned toc_offset
112 bfd_vma toc_offset
; /* Our addition, as required. */
114 unsigned long int glue_insn
;
119 /* PE linker hash table. */
121 struct ppc_coff_link_hash_table
123 struct coff_link_hash_table root
; /* First entry, as required. */
126 static struct bfd_hash_entry
*ppc_coff_link_hash_newfunc
127 PARAMS ((struct bfd_hash_entry
*, struct bfd_hash_table
*,
129 static struct bfd_link_hash_table
*ppc_coff_link_hash_table_create
131 static bfd_boolean coff_ppc_relocate_section
132 PARAMS ((bfd
*, struct bfd_link_info
*, bfd
*, asection
*, bfd_byte
*,
133 struct internal_reloc
*, struct internal_syment
*, asection
**));
134 static reloc_howto_type
*coff_ppc_rtype_to_howto
135 PARAMS ((bfd
*, asection
*, struct internal_reloc
*,
136 struct coff_link_hash_entry
*, struct internal_syment
*,
139 /* Routine to create an entry in the link hash table. */
141 static struct bfd_hash_entry
*
142 ppc_coff_link_hash_newfunc (entry
, table
, string
)
143 struct bfd_hash_entry
*entry
;
144 struct bfd_hash_table
*table
;
147 struct ppc_coff_link_hash_entry
*ret
=
148 (struct ppc_coff_link_hash_entry
*) entry
;
150 /* Allocate the structure if it has not already been allocated by a
152 if (ret
== (struct ppc_coff_link_hash_entry
*) NULL
)
153 ret
= (struct ppc_coff_link_hash_entry
*)
154 bfd_hash_allocate (table
,
155 sizeof (struct ppc_coff_link_hash_entry
));
157 if (ret
== (struct ppc_coff_link_hash_entry
*) NULL
)
160 /* Call the allocation method of the superclass. */
161 ret
= ((struct ppc_coff_link_hash_entry
*)
162 _bfd_coff_link_hash_newfunc ((struct bfd_hash_entry
*) ret
,
167 /* Initialize the local fields. */
168 SET_UNALLOCATED (ret
->toc_offset
);
169 ret
->symbol_is_glue
= 0;
172 HASH_CHECK_INIT (ret
);
175 return (struct bfd_hash_entry
*) ret
;
178 /* Initialize a PE linker hash table. */
181 ppc_coff_link_hash_table_init (struct ppc_coff_link_hash_table
*table
,
183 struct bfd_hash_entry
*(*newfunc
) (struct bfd_hash_entry
*,
184 struct bfd_hash_table
*,
186 unsigned int entsize
)
188 return _bfd_coff_link_hash_table_init (&table
->root
, abfd
, newfunc
, entsize
);
191 /* Create a PE linker hash table. */
193 static struct bfd_link_hash_table
*
194 ppc_coff_link_hash_table_create (abfd
)
197 struct ppc_coff_link_hash_table
*ret
;
198 bfd_size_type amt
= sizeof (struct ppc_coff_link_hash_table
);
200 ret
= (struct ppc_coff_link_hash_table
*) bfd_malloc (amt
);
203 if (!ppc_coff_link_hash_table_init (ret
, abfd
,
204 ppc_coff_link_hash_newfunc
,
205 sizeof (struct ppc_coff_link_hash_entry
)))
208 return (struct bfd_link_hash_table
*) NULL
;
210 return &ret
->root
.root
;
213 /* Now, tailor coffcode.h to use our hash stuff. */
215 #define coff_bfd_link_hash_table_create ppc_coff_link_hash_table_create
217 /* The nt loader points the toc register to &toc + 32768, in order to
218 use the complete range of a 16-bit displacement. We have to adjust
219 for this when we fix up loads displaced off the toc reg. */
220 #define TOC_LOAD_ADJUSTMENT (-32768)
221 #define TOC_SECTION_NAME ".private.toc"
223 /* The main body of code is in coffcode.h. */
225 #define COFF_DEFAULT_SECTION_ALIGNMENT_POWER (3)
227 /* In case we're on a 32-bit machine, construct a 64-bit "-1" value
228 from smaller values. Start with zero, widen, *then* decrement. */
229 #define MINUS_ONE (((bfd_vma)0) - 1)
231 /* These should definitely go in a header file somewhere... */
234 #define IMAGE_REL_PPC_ABSOLUTE 0x0000
237 #define IMAGE_REL_PPC_ADDR64 0x0001
240 #define IMAGE_REL_PPC_ADDR32 0x0002
242 /* 26-bit address, shifted left 2 (branch absolute) */
243 #define IMAGE_REL_PPC_ADDR24 0x0003
246 #define IMAGE_REL_PPC_ADDR16 0x0004
248 /* 16-bit address, shifted left 2 (load doubleword) */
249 #define IMAGE_REL_PPC_ADDR14 0x0005
251 /* 26-bit PC-relative offset, shifted left 2 (branch relative) */
252 #define IMAGE_REL_PPC_REL24 0x0006
254 /* 16-bit PC-relative offset, shifted left 2 (br cond relative) */
255 #define IMAGE_REL_PPC_REL14 0x0007
257 /* 16-bit offset from TOC base */
258 #define IMAGE_REL_PPC_TOCREL16 0x0008
260 /* 16-bit offset from TOC base, shifted left 2 (load doubleword) */
261 #define IMAGE_REL_PPC_TOCREL14 0x0009
263 /* 32-bit addr w/o image base */
264 #define IMAGE_REL_PPC_ADDR32NB 0x000A
266 /* va of containing section (as in an image sectionhdr) */
267 #define IMAGE_REL_PPC_SECREL 0x000B
269 /* sectionheader number */
270 #define IMAGE_REL_PPC_SECTION 0x000C
272 /* substitute TOC restore instruction iff symbol is glue code */
273 #define IMAGE_REL_PPC_IFGLUE 0x000D
275 /* symbol is glue code; virtual address is TOC restore instruction */
276 #define IMAGE_REL_PPC_IMGLUE 0x000E
278 /* va of containing section (limited to 16 bits) */
279 #define IMAGE_REL_PPC_SECREL16 0x000F
281 /* Stuff to handle immediate data when the number of bits in the
282 data is greater than the number of bits in the immediate field
283 We need to do (usually) 32 bit arithmetic on 16 bit chunks. */
284 #define IMAGE_REL_PPC_REFHI 0x0010
285 #define IMAGE_REL_PPC_REFLO 0x0011
286 #define IMAGE_REL_PPC_PAIR 0x0012
288 /* This is essentially the same as tocrel16, with TOCDEFN assumed. */
289 #define IMAGE_REL_PPC_TOCREL16_DEFN 0x0013
291 /* Flag bits in IMAGE_RELOCATION.TYPE. */
293 /* Subtract reloc value rather than adding it. */
294 #define IMAGE_REL_PPC_NEG 0x0100
296 /* Fix branch prediction bit to predict branch taken. */
297 #define IMAGE_REL_PPC_BRTAKEN 0x0200
299 /* Fix branch prediction bit to predict branch not taken. */
300 #define IMAGE_REL_PPC_BRNTAKEN 0x0400
302 /* TOC slot defined in file (or, data in toc). */
303 #define IMAGE_REL_PPC_TOCDEFN 0x0800
305 /* Masks to isolate above values in IMAGE_RELOCATION.Type. */
306 #define IMAGE_REL_PPC_TYPEMASK 0x00FF
307 #define IMAGE_REL_PPC_FLAGMASK 0x0F00
309 #define EXTRACT_TYPE(x) ((x) & IMAGE_REL_PPC_TYPEMASK)
310 #define EXTRACT_FLAGS(x) ((x) & IMAGE_REL_PPC_FLAGMASK)
311 #define EXTRACT_JUNK(x) \
312 ((x) & ~(IMAGE_REL_PPC_TYPEMASK | IMAGE_REL_PPC_FLAGMASK))
314 /* Static helper functions to make relocation work. */
315 /* (Work In Progress) */
317 static bfd_reloc_status_type ppc_refhi_reloc
PARAMS ((bfd
*abfd
,
324 static bfd_reloc_status_type ppc_pair_reloc
PARAMS ((bfd
*abfd
,
332 static bfd_reloc_status_type ppc_toc16_reloc
PARAMS ((bfd
*abfd
,
340 static bfd_reloc_status_type ppc_section_reloc
PARAMS ((bfd
*abfd
,
348 static bfd_reloc_status_type ppc_secrel_reloc
PARAMS ((bfd
*abfd
,
356 static bfd_reloc_status_type ppc_imglue_reloc
PARAMS ((bfd
*abfd
,
364 static bfd_boolean in_reloc_p
PARAMS((bfd
*abfd
, reloc_howto_type
*howto
));
366 /* FIXME: It'll take a while to get through all of these. I only need a few to
367 get us started, so those I'll make sure work. Those marked FIXME are either
368 completely unverified or have a specific unknown marked in the comment. */
370 /* Relocation entries for Windows/NT on PowerPC.
372 From the document "" we find the following listed as used relocs:
375 ADDR[64|32|16] : fields that hold addresses in data fields or the
376 16 bit displacement field on a load/store.
377 ADDR[24|14] : fields that hold addresses in branch and cond
378 branches. These represent [26|16] bit addresses.
379 The low order 2 bits are preserved.
380 REL[24|14] : branches relative to the Instruction Address
381 register. These represent [26|16] bit addresses,
382 as before. The instruction field will be zero, and
383 the address of the SYM will be inserted at link time.
384 TOCREL16 : 16 bit displacement field referring to a slot in
386 TOCREL14 : 16 bit displacement field, similar to REL14 or ADDR14.
387 ADDR32NB : 32 bit address relative to the virtual origin.
388 (On the alpha, this is always a linker generated thunk)
389 (i.e. 32bit addr relative to the image base)
390 SECREL : The value is relative to the start of the section
391 containing the symbol.
392 SECTION : access to the header containing the item. Supports the
395 In particular, note that the document does not indicate that the
396 relocations listed in the header file are used. */
399 static reloc_howto_type ppc_coff_howto_table
[] =
401 /* IMAGE_REL_PPC_ABSOLUTE 0x0000 NOP */
403 HOWTO (IMAGE_REL_PPC_ABSOLUTE
, /* type */
405 0, /* size (0 = byte, 1 = short, 2 = long) */
407 FALSE
, /* pc_relative */
409 complain_overflow_dont
, /* dont complain_on_overflow */
410 0, /* special_function */
411 "ABSOLUTE", /* name */
412 FALSE
, /* partial_inplace */
415 FALSE
), /* pcrel_offset */
417 /* IMAGE_REL_PPC_ADDR64 0x0001 64-bit address */
419 HOWTO(IMAGE_REL_PPC_ADDR64
, /* type */
421 3, /* size (0 = byte, 1 = short, 2 = long) */
423 FALSE
, /* pc_relative */
425 complain_overflow_bitfield
, /* complain_on_overflow */
426 0, /* special_function */
428 TRUE
, /* partial_inplace */
429 MINUS_ONE
, /* src_mask */
430 MINUS_ONE
, /* dst_mask */
431 FALSE
), /* pcrel_offset */
433 /* IMAGE_REL_PPC_ADDR32 0x0002 32-bit address */
435 HOWTO (IMAGE_REL_PPC_ADDR32
, /* type */
437 2, /* size (0 = byte, 1 = short, 2 = long) */
439 FALSE
, /* pc_relative */
441 complain_overflow_bitfield
, /* complain_on_overflow */
442 0, /* special_function */
444 TRUE
, /* partial_inplace */
445 0xffffffff, /* src_mask */
446 0xffffffff, /* dst_mask */
447 FALSE
), /* pcrel_offset */
449 /* IMAGE_REL_PPC_ADDR24 0x0003 26-bit address, shifted left 2 (branch absolute) */
450 /* the LI field is in bit 6 through bit 29 is 24 bits, + 2 for the shift */
451 /* Of course, That's the IBM approved bit numbering, which is not what */
452 /* anyone else uses.... The li field is in bit 2 thru 25 */
454 HOWTO (IMAGE_REL_PPC_ADDR24
, /* type */
456 2, /* size (0 = byte, 1 = short, 2 = long) */
458 FALSE
, /* pc_relative */
460 complain_overflow_bitfield
, /* complain_on_overflow */
461 0, /* special_function */
463 TRUE
, /* partial_inplace */
464 0x07fffffc, /* src_mask */
465 0x07fffffc, /* dst_mask */
466 FALSE
), /* pcrel_offset */
468 /* IMAGE_REL_PPC_ADDR16 0x0004 16-bit address */
470 HOWTO (IMAGE_REL_PPC_ADDR16
, /* type */
472 1, /* size (0 = byte, 1 = short, 2 = long) */
474 FALSE
, /* pc_relative */
476 complain_overflow_signed
, /* complain_on_overflow */
477 0, /* special_function */
479 TRUE
, /* partial_inplace */
480 0xffff, /* src_mask */
481 0xffff, /* dst_mask */
482 FALSE
), /* pcrel_offset */
484 /* IMAGE_REL_PPC_ADDR14 0x0005 */
485 /* 16-bit address, shifted left 2 (load doubleword) */
486 /* FIXME: the mask is likely wrong, and the bit position may be as well */
488 HOWTO (IMAGE_REL_PPC_ADDR14
, /* type */
490 1, /* size (0 = byte, 1 = short, 2 = long) */
492 FALSE
, /* pc_relative */
494 complain_overflow_signed
, /* complain_on_overflow */
495 0, /* special_function */
497 TRUE
, /* partial_inplace */
498 0xffff, /* src_mask */
499 0xffff, /* dst_mask */
500 FALSE
), /* pcrel_offset */
502 /* IMAGE_REL_PPC_REL24 0x0006 */
503 /* 26-bit PC-relative offset, shifted left 2 (branch relative) */
505 HOWTO (IMAGE_REL_PPC_REL24
, /* type */
507 2, /* size (0 = byte, 1 = short, 2 = long) */
509 TRUE
, /* pc_relative */
511 complain_overflow_signed
, /* complain_on_overflow */
512 0, /* special_function */
514 TRUE
, /* partial_inplace */
515 0x3fffffc, /* src_mask */
516 0x3fffffc, /* dst_mask */
517 FALSE
), /* pcrel_offset */
519 /* IMAGE_REL_PPC_REL14 0x0007 */
520 /* 16-bit PC-relative offset, shifted left 2 (br cond relative) */
521 /* FIXME: the mask is likely wrong, and the bit position may be as well */
522 /* FIXME: how does it know how far to shift? */
524 HOWTO (IMAGE_REL_PPC_ADDR14
, /* type */
526 1, /* size (0 = byte, 1 = short, 2 = long) */
528 FALSE
, /* pc_relative */
530 complain_overflow_signed
, /* complain_on_overflow */
531 0, /* special_function */
533 TRUE
, /* partial_inplace */
534 0xffff, /* src_mask */
535 0xffff, /* dst_mask */
536 TRUE
), /* pcrel_offset */
538 /* IMAGE_REL_PPC_TOCREL16 0x0008 */
539 /* 16-bit offset from TOC base */
541 HOWTO (IMAGE_REL_PPC_TOCREL16
,/* type */
543 1, /* size (0 = byte, 1 = short, 2 = long) */
545 FALSE
, /* pc_relative */
547 complain_overflow_dont
, /* complain_on_overflow */
548 ppc_toc16_reloc
, /* special_function */
549 "TOCREL16", /* name */
550 FALSE
, /* partial_inplace */
551 0xffff, /* src_mask */
552 0xffff, /* dst_mask */
553 FALSE
), /* pcrel_offset */
555 /* IMAGE_REL_PPC_TOCREL14 0x0009 */
556 /* 16-bit offset from TOC base, shifted left 2 (load doubleword) */
558 HOWTO (IMAGE_REL_PPC_TOCREL14
,/* type */
560 1, /* size (0 = byte, 1 = short, 2 = long) */
562 FALSE
, /* pc_relative */
564 complain_overflow_signed
, /* complain_on_overflow */
565 0, /* special_function */
566 "TOCREL14", /* name */
567 FALSE
, /* partial_inplace */
568 0xffff, /* src_mask */
569 0xffff, /* dst_mask */
570 FALSE
), /* pcrel_offset */
572 /* IMAGE_REL_PPC_ADDR32NB 0x000A */
573 /* 32-bit addr w/ image base */
575 HOWTO (IMAGE_REL_PPC_ADDR32NB
,/* type */
577 2, /* size (0 = byte, 1 = short, 2 = long) */
579 FALSE
, /* pc_relative */
581 complain_overflow_signed
, /* complain_on_overflow */
582 0, /* special_function */
583 "ADDR32NB", /* name */
584 TRUE
, /* partial_inplace */
585 0xffffffff, /* src_mask */
586 0xffffffff, /* dst_mask */
587 FALSE
), /* pcrel_offset */
589 /* IMAGE_REL_PPC_SECREL 0x000B */
590 /* va of containing section (as in an image sectionhdr) */
592 HOWTO (IMAGE_REL_PPC_SECREL
,/* type */
594 2, /* size (0 = byte, 1 = short, 2 = long) */
596 FALSE
, /* pc_relative */
598 complain_overflow_signed
, /* complain_on_overflow */
599 ppc_secrel_reloc
, /* special_function */
601 TRUE
, /* partial_inplace */
602 0xffffffff, /* src_mask */
603 0xffffffff, /* dst_mask */
604 TRUE
), /* pcrel_offset */
606 /* IMAGE_REL_PPC_SECTION 0x000C */
607 /* sectionheader number */
609 HOWTO (IMAGE_REL_PPC_SECTION
,/* type */
611 2, /* size (0 = byte, 1 = short, 2 = long) */
613 FALSE
, /* pc_relative */
615 complain_overflow_signed
, /* complain_on_overflow */
616 ppc_section_reloc
, /* special_function */
617 "SECTION", /* name */
618 TRUE
, /* partial_inplace */
619 0xffffffff, /* src_mask */
620 0xffffffff, /* dst_mask */
621 TRUE
), /* pcrel_offset */
623 /* IMAGE_REL_PPC_IFGLUE 0x000D */
624 /* substitute TOC restore instruction iff symbol is glue code */
626 HOWTO (IMAGE_REL_PPC_IFGLUE
,/* type */
628 2, /* size (0 = byte, 1 = short, 2 = long) */
630 FALSE
, /* pc_relative */
632 complain_overflow_signed
, /* complain_on_overflow */
633 0, /* special_function */
635 TRUE
, /* partial_inplace */
636 0xffffffff, /* src_mask */
637 0xffffffff, /* dst_mask */
638 FALSE
), /* pcrel_offset */
640 /* IMAGE_REL_PPC_IMGLUE 0x000E */
641 /* symbol is glue code; virtual address is TOC restore instruction */
643 HOWTO (IMAGE_REL_PPC_IMGLUE
,/* type */
645 2, /* size (0 = byte, 1 = short, 2 = long) */
647 FALSE
, /* pc_relative */
649 complain_overflow_dont
, /* complain_on_overflow */
650 ppc_imglue_reloc
, /* special_function */
652 FALSE
, /* partial_inplace */
653 0xffffffff, /* src_mask */
654 0xffffffff, /* dst_mask */
655 FALSE
), /* pcrel_offset */
657 /* IMAGE_REL_PPC_SECREL16 0x000F */
658 /* va of containing section (limited to 16 bits) */
660 HOWTO (IMAGE_REL_PPC_SECREL16
,/* type */
662 1, /* size (0 = byte, 1 = short, 2 = long) */
664 FALSE
, /* pc_relative */
666 complain_overflow_signed
, /* complain_on_overflow */
667 0, /* special_function */
668 "SECREL16", /* name */
669 TRUE
, /* partial_inplace */
670 0xffff, /* src_mask */
671 0xffff, /* dst_mask */
672 TRUE
), /* pcrel_offset */
674 /* IMAGE_REL_PPC_REFHI 0x0010 */
676 HOWTO (IMAGE_REL_PPC_REFHI
, /* type */
678 1, /* size (0 = byte, 1 = short, 2 = long) */
680 FALSE
, /* pc_relative */
682 complain_overflow_signed
, /* complain_on_overflow */
683 ppc_refhi_reloc
, /* special_function */
685 TRUE
, /* partial_inplace */
686 0xffffffff, /* src_mask */
687 0xffffffff, /* dst_mask */
688 FALSE
), /* pcrel_offset */
690 /* IMAGE_REL_PPC_REFLO 0x0011 */
692 HOWTO (IMAGE_REL_PPC_REFLO
, /* type */
694 1, /* size (0 = byte, 1 = short, 2 = long) */
696 FALSE
, /* pc_relative */
698 complain_overflow_signed
, /* complain_on_overflow */
699 ppc_refhi_reloc
, /* special_function */
701 TRUE
, /* partial_inplace */
702 0xffffffff, /* src_mask */
703 0xffffffff, /* dst_mask */
704 FALSE
), /* pcrel_offset */
706 /* IMAGE_REL_PPC_PAIR 0x0012 */
708 HOWTO (IMAGE_REL_PPC_PAIR
, /* type */
710 1, /* size (0 = byte, 1 = short, 2 = long) */
712 FALSE
, /* pc_relative */
714 complain_overflow_signed
, /* complain_on_overflow */
715 ppc_pair_reloc
, /* special_function */
717 TRUE
, /* partial_inplace */
718 0xffffffff, /* src_mask */
719 0xffffffff, /* dst_mask */
720 FALSE
), /* pcrel_offset */
722 /* IMAGE_REL_PPC_TOCREL16_DEFN 0x0013 */
723 /* 16-bit offset from TOC base, without causing a definition */
725 HOWTO ( (IMAGE_REL_PPC_TOCREL16
| IMAGE_REL_PPC_TOCDEFN
), /* type */
727 1, /* size (0 = byte, 1 = short, 2 = long) */
729 FALSE
, /* pc_relative */
731 complain_overflow_dont
, /* complain_on_overflow */
732 0, /* special_function */
733 "TOCREL16, TOCDEFN", /* name */
734 FALSE
, /* partial_inplace */
735 0xffff, /* src_mask */
736 0xffff, /* dst_mask */
737 FALSE
), /* pcrel_offset */
741 /* Some really cheezy macros that can be turned on to test stderr :-) */
750 fprintf (stderr,_("Unimplemented Relocation -- %s\n"),x); \
754 #define DUMP_RELOC(n,r) \
756 fprintf (stderr,"%s sym %d, addr %d, addend %d\n", \
757 n, (*(r->sym_ptr_ptr))->name, \
758 r->address, r->addend); \
761 /* Given a reloc name, n, and a pointer to an internal_reloc,
762 dump out interesting information on the contents
764 #define n_name _n._n_name
765 #define n_zeroes _n._n_n._n_zeroes
766 #define n_offset _n._n_n._n_offset */
768 #define DUMP_RELOC2(n,r) \
770 fprintf (stderr,"%s sym %d, r_vaddr %d %s\n", \
771 n, r->r_symndx, r->r_vaddr, \
772 (((r->r_type) & IMAGE_REL_PPC_TOCDEFN) == 0) \
778 #define DUMP_RELOC(n,r)
779 #define DUMP_RELOC2(n,r)
782 /* TOC construction and management routines. */
784 /* This file is compiled twice, and these variables are defined in one
785 of the compilations. FIXME: This is confusing and weird. Also,
786 BFD should not use global variables. */
787 extern bfd
* bfd_of_toc_owner
;
788 extern long int global_toc_size
;
789 extern long int import_table_size
;
790 extern long int first_thunk_address
;
791 extern long int thunk_size
;
809 struct list_ele
*next
;
811 enum ref_category cat
;
816 extern struct list_ele
*head
;
817 extern struct list_ele
*tail
;
819 static void record_toc
820 PARAMS ((asection
*, bfd_signed_vma
, enum ref_category
, const char *));
823 record_toc (toc_section
, our_toc_offset
, cat
, name
)
824 asection
*toc_section
;
825 bfd_signed_vma our_toc_offset
;
826 enum ref_category cat
;
829 /* Add this entry to our toc addr-offset-name list. */
830 bfd_size_type amt
= sizeof (struct list_ele
);
831 struct list_ele
*t
= (struct list_ele
*) bfd_malloc (amt
);
836 t
->offset
= our_toc_offset
;
839 t
->addr
= toc_section
->output_offset
+ our_toc_offset
;
853 #ifdef COFF_IMAGE_WITH_PE
855 static bfd_boolean ppc_record_toc_entry
856 PARAMS ((bfd
*, struct bfd_link_info
*, asection
*, int, enum toc_type
));
857 static void ppc_mark_symbol_as_glue
858 PARAMS ((bfd
*, int, struct internal_reloc
*));
860 /* Record a toc offset against a symbol. */
862 ppc_record_toc_entry(abfd
, info
, sec
, sym
, toc_kind
)
864 struct bfd_link_info
*info ATTRIBUTE_UNUSED
;
865 asection
*sec ATTRIBUTE_UNUSED
;
867 enum toc_type toc_kind ATTRIBUTE_UNUSED
;
869 struct ppc_coff_link_hash_entry
*h
;
876 h
= (struct ppc_coff_link_hash_entry
*) (obj_coff_sym_hashes (abfd
)[sym
]);
884 local_syms
= obj_coff_local_toc_table(abfd
);
891 /* allocate a table */
892 amt
= (bfd_size_type
) obj_raw_syment_count (abfd
) * sizeof (int);
893 local_syms
= (int *) bfd_zalloc (abfd
, amt
);
896 obj_coff_local_toc_table (abfd
) = local_syms
;
898 for (i
= 0; i
< obj_raw_syment_count (abfd
); ++i
)
900 SET_UNALLOCATED (local_syms
[i
]);
904 if (IS_UNALLOCATED(local_syms
[sym
]))
906 local_syms
[sym
] = global_toc_size
;
907 global_toc_size
+= 4;
909 /* The size must fit in a 16-bit displacement. */
910 if (global_toc_size
> 65535)
912 (*_bfd_error_handler
) (_("TOC overflow"));
913 bfd_set_error (bfd_error_file_too_big
);
920 name
= h
->root
.root
.root
.string
;
922 /* Check to see if there's a toc slot allocated. If not, do it
923 here. It will be used in relocate_section. */
924 if (IS_UNALLOCATED(h
->toc_offset
))
926 h
->toc_offset
= global_toc_size
;
927 global_toc_size
+= 4;
929 /* The size must fit in a 16-bit displacement. */
930 if (global_toc_size
>= 65535)
932 (*_bfd_error_handler
) (_("TOC overflow"));
933 bfd_set_error (bfd_error_file_too_big
);
942 /* Record a toc offset against a symbol. */
944 ppc_mark_symbol_as_glue(abfd
, sym
, rel
)
947 struct internal_reloc
*rel
;
949 struct ppc_coff_link_hash_entry
*h
;
951 h
= (struct ppc_coff_link_hash_entry
*) (obj_coff_sym_hashes (abfd
)[sym
]);
955 h
->symbol_is_glue
= 1;
956 h
->glue_insn
= bfd_get_32 (abfd
, (bfd_byte
*) &rel
->r_vaddr
);
961 #endif /* COFF_IMAGE_WITH_PE */
963 /* Return TRUE if this relocation should
964 appear in the output .reloc section. */
966 static bfd_boolean
in_reloc_p(abfd
, howto
)
967 bfd
* abfd ATTRIBUTE_UNUSED
;
968 reloc_howto_type
*howto
;
971 (! howto
->pc_relative
)
972 && (howto
->type
!= IMAGE_REL_PPC_ADDR32NB
)
973 && (howto
->type
!= IMAGE_REL_PPC_TOCREL16
)
974 && (howto
->type
!= IMAGE_REL_PPC_IMGLUE
)
975 && (howto
->type
!= IMAGE_REL_PPC_IFGLUE
)
976 && (howto
->type
!= IMAGE_REL_PPC_SECREL
)
977 && (howto
->type
!= IMAGE_REL_PPC_SECTION
)
978 && (howto
->type
!= IMAGE_REL_PPC_SECREL16
)
979 && (howto
->type
!= IMAGE_REL_PPC_REFHI
)
980 && (howto
->type
!= IMAGE_REL_PPC_REFLO
)
981 && (howto
->type
!= IMAGE_REL_PPC_PAIR
)
982 && (howto
->type
!= IMAGE_REL_PPC_TOCREL16_DEFN
) ;
985 /* The reloc processing routine for the optimized COFF linker. */
988 coff_ppc_relocate_section (output_bfd
, info
, input_bfd
, input_section
,
989 contents
, relocs
, syms
, sections
)
991 struct bfd_link_info
*info
;
993 asection
*input_section
;
995 struct internal_reloc
*relocs
;
996 struct internal_syment
*syms
;
999 struct internal_reloc
*rel
;
1000 struct internal_reloc
*relend
;
1003 asection
*toc_section
= 0;
1005 reloc_howto_type
*howto
= 0;
1007 /* If we are performing a relocatable link, we don't need to do a
1008 thing. The caller will take care of adjusting the reloc
1009 addresses and symbol indices. */
1010 if (info
->relocatable
)
1017 relend
= rel
+ input_section
->reloc_count
;
1018 for (; rel
< relend
; rel
++)
1021 struct ppc_coff_link_hash_entry
*h
;
1022 struct internal_syment
*sym
;
1026 bfd_reloc_status_type rstat
;
1029 unsigned short r_type
= EXTRACT_TYPE (rel
->r_type
);
1030 unsigned short r_flags
= EXTRACT_FLAGS(rel
->r_type
);
1032 symndx
= rel
->r_symndx
;
1033 loc
= contents
+ rel
->r_vaddr
- input_section
->vma
;
1035 /* FIXME: check bounds on r_type */
1036 howto
= ppc_coff_howto_table
+ r_type
;
1045 h
= (struct ppc_coff_link_hash_entry
*)
1046 (obj_coff_sym_hashes (input_bfd
)[symndx
]);
1052 sym
= syms
+ symndx
;
1055 if (r_type
== IMAGE_REL_PPC_IMGLUE
&& h
== 0)
1057 /* An IMGLUE reloc must have a name. Something is very wrong. */
1064 /* FIXME: PAIR unsupported in the following code. */
1068 sec
= bfd_abs_section_ptr
;
1071 sec
= sections
[symndx
];
1072 val
= (sec
->output_section
->vma
1073 + sec
->output_offset
1075 if (! obj_pe (output_bfd
))
1083 if (h
->root
.root
.type
== bfd_link_hash_defined
1084 || h
->root
.root
.type
== bfd_link_hash_defweak
)
1086 sec
= h
->root
.root
.u
.def
.section
;
1087 val
= (h
->root
.root
.u
.def
.value
1088 + sec
->output_section
->vma
1089 + sec
->output_offset
);
1093 if (! ((*info
->callbacks
->undefined_symbol
)
1094 (info
, h
->root
.root
.root
.string
, input_bfd
, input_section
,
1095 rel
->r_vaddr
- input_section
->vma
, TRUE
)))
1100 rstat
= bfd_reloc_ok
;
1102 /* Each case must do its own relocation, setting rstat appropriately. */
1106 (*_bfd_error_handler
)
1107 (_("%B: unsupported relocation type 0x%02x"), input_bfd
, r_type
);
1108 bfd_set_error (bfd_error_bad_value
);
1110 case IMAGE_REL_PPC_TOCREL16
:
1112 bfd_signed_vma our_toc_offset
;
1115 DUMP_RELOC2(howto
->name
, rel
);
1117 if (toc_section
== 0)
1119 toc_section
= bfd_get_section_by_name (bfd_of_toc_owner
,
1122 if ( toc_section
== NULL
)
1124 /* There is no toc section. Something is very wrong. */
1129 /* Amazing bit tricks present. As we may have seen earlier, we
1130 use the 1 bit to tell us whether or not a toc offset has been
1131 allocated. Now that they've all been allocated, we will use
1132 the 1 bit to tell us if we've written this particular toc
1137 /* It is a file local symbol. */
1138 int *local_toc_table
;
1141 sym
= syms
+ symndx
;
1142 name
= sym
->_n
._n_name
;
1144 local_toc_table
= obj_coff_local_toc_table(input_bfd
);
1145 our_toc_offset
= local_toc_table
[symndx
];
1147 if (IS_WRITTEN(our_toc_offset
))
1149 /* If it has been written out, it is marked with the
1150 1 bit. Fix up our offset, but do not write it out
1152 MAKE_ADDR_AGAIN(our_toc_offset
);
1156 /* Write out the toc entry. */
1157 record_toc (toc_section
, our_toc_offset
, priv
,
1160 bfd_put_32 (output_bfd
, val
,
1161 toc_section
->contents
+ our_toc_offset
);
1163 MARK_AS_WRITTEN(local_toc_table
[symndx
]);
1169 const char *name
= h
->root
.root
.root
.string
;
1170 our_toc_offset
= h
->toc_offset
;
1172 if ((r_flags
& IMAGE_REL_PPC_TOCDEFN
)
1173 == IMAGE_REL_PPC_TOCDEFN
)
1175 /* This is unbelievable cheese. Some knowledgable asm
1176 hacker has decided to use r2 as a base for loading
1177 a value. He/She does this by setting the tocdefn bit,
1178 and not supplying a toc definition. The behaviour is
1179 then to use the difference between the value of the
1180 symbol and the actual location of the toc as the toc
1183 In fact, what is usually happening is, because the
1184 Import Address Table is mapped immediately following
1185 the toc, some trippy library code trying for speed on
1186 dll linkage, takes advantage of that and considers
1187 the IAT to be part of the toc, thus saving a load. */
1189 our_toc_offset
= val
- (toc_section
->output_section
->vma
1190 + toc_section
->output_offset
);
1192 /* The size must still fit in a 16-bit displacement. */
1193 if ((bfd_vma
) our_toc_offset
>= 65535)
1195 (*_bfd_error_handler
)
1196 (_("%B: Relocation for %s of %lx exceeds Toc size limit"),
1198 (unsigned long) our_toc_offset
);
1199 bfd_set_error (bfd_error_bad_value
);
1203 record_toc (toc_section
, our_toc_offset
, pub
,
1206 else if (IS_WRITTEN (our_toc_offset
))
1208 /* If it has been written out, it is marked with the
1209 1 bit. Fix up our offset, but do not write it out
1211 MAKE_ADDR_AGAIN(our_toc_offset
);
1215 record_toc(toc_section
, our_toc_offset
, pub
,
1218 /* Write out the toc entry. */
1219 bfd_put_32 (output_bfd
, val
,
1220 toc_section
->contents
+ our_toc_offset
);
1222 MARK_AS_WRITTEN(h
->toc_offset
);
1223 /* The tricky part is that this is the address that
1224 needs a .reloc entry for it. */
1229 if (fixit
&& info
->base_file
)
1231 /* So if this is non pcrelative, and is referenced
1232 to a section or a common symbol, then it needs a reloc. */
1234 /* Relocation to a symbol in a section which
1235 isn't absolute - we output the address here
1237 bfd_vma addr
= (toc_section
->output_section
->vma
1238 + toc_section
->output_offset
+ our_toc_offset
);
1240 if (coff_data (output_bfd
)->pe
)
1241 addr
-= pe_data(output_bfd
)->pe_opthdr
.ImageBase
;
1243 fwrite (&addr
, 1,4, (FILE *) info
->base_file
);
1246 /* FIXME: this test is conservative. */
1247 if ((r_flags
& IMAGE_REL_PPC_TOCDEFN
) != IMAGE_REL_PPC_TOCDEFN
1248 && (bfd_vma
) our_toc_offset
> toc_section
->size
)
1250 (*_bfd_error_handler
)
1251 (_("%B: Relocation exceeds allocated TOC (%lx)"),
1252 input_bfd
, (unsigned long) toc_section
->size
);
1253 bfd_set_error (bfd_error_bad_value
);
1257 /* Now we know the relocation for this toc reference. */
1258 relocation
= our_toc_offset
+ TOC_LOAD_ADJUSTMENT
;
1259 rstat
= _bfd_relocate_contents (howto
, input_bfd
, relocation
, loc
);
1262 case IMAGE_REL_PPC_IFGLUE
:
1264 /* To solve this, we need to know whether or not the symbol
1265 appearing on the call instruction is a glue function or not.
1266 A glue function must announce itself via a IMGLUE reloc, and
1267 the reloc contains the required toc restore instruction. */
1269 const char *my_name
;
1271 DUMP_RELOC2 (howto
->name
, rel
);
1275 my_name
= h
->root
.root
.root
.string
;
1276 if (h
->symbol_is_glue
== 1)
1278 x
= bfd_get_32 (input_bfd
, loc
);
1279 bfd_put_32 (input_bfd
, (bfd_vma
) h
->glue_insn
, loc
);
1284 case IMAGE_REL_PPC_SECREL
:
1285 /* Unimplemented: codeview debugging information. */
1286 /* For fast access to the header of the section
1287 containing the item. */
1289 case IMAGE_REL_PPC_SECTION
:
1290 /* Unimplemented: codeview debugging information. */
1291 /* Is used to indicate that the value should be relative
1292 to the beginning of the section that contains the
1295 case IMAGE_REL_PPC_ABSOLUTE
:
1297 const char *my_name
;
1300 my_name
= (syms
+symndx
)->_n
._n_name
;
1302 my_name
= h
->root
.root
.root
.string
;
1304 (*_bfd_error_handler
)
1305 (_("Warning: unsupported reloc %s <file %B, section %A>\n"
1306 "sym %ld (%s), r_vaddr %ld (%lx)"),
1307 input_bfd
, input_section
, howto
->name
,
1308 rel
->r_symndx
, my_name
, (long) rel
->r_vaddr
,
1309 (unsigned long) rel
->r_vaddr
);
1312 case IMAGE_REL_PPC_IMGLUE
:
1314 /* There is nothing to do now. This reloc was noted in the first
1315 pass over the relocs, and the glue instruction extracted. */
1316 const char *my_name
;
1318 if (h
->symbol_is_glue
== 1)
1320 my_name
= h
->root
.root
.root
.string
;
1322 (*_bfd_error_handler
)
1323 (_("%B: Out of order IMGLUE reloc for %s"), input_bfd
, my_name
);
1324 bfd_set_error (bfd_error_bad_value
);
1328 case IMAGE_REL_PPC_ADDR32NB
:
1330 const char *name
= 0;
1332 DUMP_RELOC2 (howto
->name
, rel
);
1334 if (CONST_STRNEQ (input_section
->name
, ".idata$2") && first_thunk_address
== 0)
1336 /* Set magic values. */
1338 struct coff_link_hash_entry
*myh
;
1340 myh
= coff_link_hash_lookup (coff_hash_table (info
),
1342 FALSE
, FALSE
, TRUE
);
1343 first_thunk_address
= myh
->root
.u
.def
.value
+
1344 sec
->output_section
->vma
+
1345 sec
->output_offset
-
1346 pe_data(output_bfd
)->pe_opthdr
.ImageBase
;
1348 idata5offset
= myh
->root
.u
.def
.value
;
1349 myh
= coff_link_hash_lookup (coff_hash_table (info
),
1351 FALSE
, FALSE
, TRUE
);
1353 thunk_size
= myh
->root
.u
.def
.value
- idata5offset
;
1354 myh
= coff_link_hash_lookup (coff_hash_table (info
),
1356 FALSE
, FALSE
, TRUE
);
1357 import_table_size
= myh
->root
.u
.def
.value
;
1362 /* It is a file local symbol. */
1363 sym
= syms
+ symndx
;
1364 name
= sym
->_n
._n_name
;
1370 name
= h
->root
.root
.root
.string
;
1371 if (strcmp (".idata$2", name
) == 0)
1372 target
= "__idata2_magic__";
1373 else if (strcmp (".idata$4", name
) == 0)
1374 target
= "__idata4_magic__";
1375 else if (strcmp (".idata$5", name
) == 0)
1376 target
= "__idata5_magic__";
1380 struct coff_link_hash_entry
*myh
;
1382 myh
= coff_link_hash_lookup (coff_hash_table (info
),
1384 FALSE
, FALSE
, TRUE
);
1387 /* Missing magic cookies. Something is very wrong. */
1391 val
= myh
->root
.u
.def
.value
+
1392 sec
->output_section
->vma
+ sec
->output_offset
;
1393 if (first_thunk_address
== 0)
1396 myh
= coff_link_hash_lookup (coff_hash_table (info
),
1398 FALSE
, FALSE
, TRUE
);
1399 first_thunk_address
= myh
->root
.u
.def
.value
+
1400 sec
->output_section
->vma
+
1401 sec
->output_offset
-
1402 pe_data(output_bfd
)->pe_opthdr
.ImageBase
;
1404 idata5offset
= myh
->root
.u
.def
.value
;
1405 myh
= coff_link_hash_lookup (coff_hash_table (info
),
1407 FALSE
, FALSE
, TRUE
);
1409 thunk_size
= myh
->root
.u
.def
.value
- idata5offset
;
1410 myh
= coff_link_hash_lookup (coff_hash_table (info
),
1412 FALSE
, FALSE
, TRUE
);
1413 import_table_size
= myh
->root
.u
.def
.value
;
1418 rstat
= _bfd_relocate_contents (howto
,
1421 pe_data (output_bfd
)->pe_opthdr
.ImageBase
,
1426 case IMAGE_REL_PPC_REL24
:
1427 DUMP_RELOC2(howto
->name
, rel
);
1428 val
-= (input_section
->output_section
->vma
1429 + input_section
->output_offset
);
1431 rstat
= _bfd_relocate_contents (howto
,
1436 case IMAGE_REL_PPC_ADDR16
:
1437 case IMAGE_REL_PPC_ADDR24
:
1438 case IMAGE_REL_PPC_ADDR32
:
1439 DUMP_RELOC2(howto
->name
, rel
);
1440 rstat
= _bfd_relocate_contents (howto
,
1447 if (info
->base_file
)
1449 /* So if this is non pcrelative, and is referenced
1450 to a section or a common symbol, then it needs a reloc. */
1451 if (sym
&& pe_data(output_bfd
)->in_reloc_p (output_bfd
, howto
))
1453 /* Relocation to a symbol in a section which
1454 isn't absolute - we output the address here
1456 bfd_vma addr
= rel
->r_vaddr
1457 - input_section
->vma
1458 + input_section
->output_offset
1459 + input_section
->output_section
->vma
;
1461 if (coff_data (output_bfd
)->pe
)
1462 addr
-= pe_data (output_bfd
)->pe_opthdr
.ImageBase
;
1464 fwrite (&addr
, 1,4, (FILE *) info
->base_file
);
1474 case bfd_reloc_overflow
:
1477 char buf
[SYMNMLEN
+ 1];
1483 else if (sym
== NULL
)
1485 else if (sym
->_n
._n_n
._n_zeroes
== 0
1486 && sym
->_n
._n_n
._n_offset
!= 0)
1487 name
= obj_coff_strings (input_bfd
) + sym
->_n
._n_n
._n_offset
;
1490 strncpy (buf
, sym
->_n
._n_name
, SYMNMLEN
);
1491 buf
[SYMNMLEN
] = '\0';
1495 if (! ((*info
->callbacks
->reloc_overflow
)
1496 (info
, (h
? &h
->root
.root
: NULL
), name
, howto
->name
,
1497 (bfd_vma
) 0, input_bfd
,
1498 input_section
, rel
->r_vaddr
- input_section
->vma
)))
1507 #ifdef COFF_IMAGE_WITH_PE
1509 /* FIXME: BFD should not use global variables. This file is compiled
1510 twice, and these variables are shared. This is confusing and
1513 long int global_toc_size
= 4;
1515 bfd
* bfd_of_toc_owner
= 0;
1517 long int import_table_size
;
1518 long int first_thunk_address
;
1519 long int thunk_size
;
1521 struct list_ele
*head
;
1522 struct list_ele
*tail
;
1525 h1
= N_("\n\t\t\tTOC MAPPING\n\n");
1527 h2
= N_(" TOC disassembly Comments Name\n");
1529 h3
= N_(" Offset spelling (if present)\n");
1535 FILE *file
= (FILE *) vfile
;
1538 fprintf (file
, _(h1
));
1539 fprintf (file
, _(h2
));
1540 fprintf (file
, _(h3
));
1542 for (t
= head
; t
!= 0; t
=t
->next
)
1544 const char *cat
= "";
1547 cat
= _("private ");
1548 else if (t
->cat
== pub
)
1550 else if (t
->cat
== tocdata
)
1551 cat
= _("data-in-toc ");
1553 if (t
->offset
> global_toc_size
)
1555 if (t
->offset
<= global_toc_size
+ thunk_size
)
1556 cat
= _("IAT reference ");
1560 _("**** global_toc_size %ld(%lx), thunk_size %ld(%lx)\n"),
1561 global_toc_size
, global_toc_size
,
1562 thunk_size
, thunk_size
);
1563 cat
= _("Out of bounds!");
1568 " %04lx (%d)", (unsigned long) t
->offset
, t
->offset
- 32768);
1575 fprintf (file
, "\n");
1579 ppc_allocate_toc_section (info
)
1580 struct bfd_link_info
*info ATTRIBUTE_UNUSED
;
1585 static char test_char
= '1';
1587 if ( global_toc_size
== 0 ) /* FIXME: does this get me in trouble? */
1590 if (bfd_of_toc_owner
== 0)
1591 /* No toc owner? Something is very wrong. */
1594 s
= bfd_get_section_by_name ( bfd_of_toc_owner
, TOC_SECTION_NAME
);
1596 /* No toc section? Something is very wrong. */
1599 amt
= global_toc_size
;
1600 foo
= (bfd_byte
*) bfd_alloc (bfd_of_toc_owner
, amt
);
1601 memset(foo
, test_char
, (size_t) global_toc_size
);
1603 s
->size
= global_toc_size
;
1610 ppc_process_before_allocation (abfd
, info
)
1612 struct bfd_link_info
*info
;
1615 struct internal_reloc
*i
, *rel
;
1617 /* Here we have a bfd that is to be included on the link. We have a hook
1618 to do reloc rummaging, before section sizes are nailed down. */
1619 _bfd_coff_get_external_symbols (abfd
);
1621 /* Rummage around all the relocs and map the toc. */
1622 sec
= abfd
->sections
;
1627 for (; sec
!= 0; sec
= sec
->next
)
1629 if (sec
->reloc_count
== 0)
1632 /* load the relocs */
1633 /* FIXME: there may be a storage leak here */
1634 i
=_bfd_coff_read_internal_relocs(abfd
,sec
,1,0,0,0);
1639 for (rel
= i
; rel
< i
+ sec
->reloc_count
; ++rel
)
1641 unsigned short r_type
= EXTRACT_TYPE (rel
->r_type
);
1642 unsigned short r_flags
= EXTRACT_FLAGS (rel
->r_type
);
1643 bfd_boolean ok
= TRUE
;
1645 DUMP_RELOC2 (ppc_coff_howto_table
[r_type
].name
, rel
);
1649 case IMAGE_REL_PPC_TOCREL16
:
1650 /* If TOCDEFN is on, ignore as someone else has allocated the
1652 if ((r_flags
& IMAGE_REL_PPC_TOCDEFN
) != IMAGE_REL_PPC_TOCDEFN
)
1653 ok
= ppc_record_toc_entry(abfd
, info
, sec
,
1654 rel
->r_symndx
, default_toc
);
1658 case IMAGE_REL_PPC_IMGLUE
:
1659 ppc_mark_symbol_as_glue (abfd
, rel
->r_symndx
, rel
);
1672 static bfd_reloc_status_type
1673 ppc_refhi_reloc (abfd
, reloc_entry
, symbol
, data
,
1674 input_section
, output_bfd
, error_message
)
1675 bfd
*abfd ATTRIBUTE_UNUSED
;
1676 arelent
*reloc_entry ATTRIBUTE_UNUSED
;
1677 asymbol
*symbol ATTRIBUTE_UNUSED
;
1678 PTR data ATTRIBUTE_UNUSED
;
1679 asection
*input_section ATTRIBUTE_UNUSED
;
1681 char **error_message ATTRIBUTE_UNUSED
;
1684 DUMP_RELOC("REFHI",reloc_entry
);
1686 if (output_bfd
== (bfd
*) NULL
)
1687 return bfd_reloc_continue
;
1689 return bfd_reloc_undefined
;
1692 static bfd_reloc_status_type
1693 ppc_pair_reloc (abfd
, reloc_entry
, symbol
, data
,
1694 input_section
, output_bfd
, error_message
)
1695 bfd
*abfd ATTRIBUTE_UNUSED
;
1696 arelent
*reloc_entry ATTRIBUTE_UNUSED
;
1697 asymbol
*symbol ATTRIBUTE_UNUSED
;
1698 PTR data ATTRIBUTE_UNUSED
;
1699 asection
*input_section ATTRIBUTE_UNUSED
;
1701 char **error_message ATTRIBUTE_UNUSED
;
1704 DUMP_RELOC("PAIR",reloc_entry
);
1706 if (output_bfd
== (bfd
*) NULL
)
1707 return bfd_reloc_continue
;
1709 return bfd_reloc_undefined
;
1712 static bfd_reloc_status_type
1713 ppc_toc16_reloc (abfd
, reloc_entry
, symbol
, data
,
1714 input_section
, output_bfd
, error_message
)
1715 bfd
*abfd ATTRIBUTE_UNUSED
;
1716 arelent
*reloc_entry ATTRIBUTE_UNUSED
;
1717 asymbol
*symbol ATTRIBUTE_UNUSED
;
1718 PTR data ATTRIBUTE_UNUSED
;
1719 asection
*input_section ATTRIBUTE_UNUSED
;
1721 char **error_message ATTRIBUTE_UNUSED
;
1723 UN_IMPL ("TOCREL16");
1724 DUMP_RELOC ("TOCREL16",reloc_entry
);
1726 if (output_bfd
== (bfd
*) NULL
)
1727 return bfd_reloc_continue
;
1729 return bfd_reloc_ok
;
1732 static bfd_reloc_status_type
1733 ppc_secrel_reloc (abfd
, reloc_entry
, symbol
, data
,
1734 input_section
, output_bfd
, error_message
)
1735 bfd
*abfd ATTRIBUTE_UNUSED
;
1736 arelent
*reloc_entry ATTRIBUTE_UNUSED
;
1737 asymbol
*symbol ATTRIBUTE_UNUSED
;
1738 PTR data ATTRIBUTE_UNUSED
;
1739 asection
*input_section ATTRIBUTE_UNUSED
;
1741 char **error_message ATTRIBUTE_UNUSED
;
1744 DUMP_RELOC("SECREL",reloc_entry
);
1746 if (output_bfd
== (bfd
*) NULL
)
1747 return bfd_reloc_continue
;
1749 return bfd_reloc_ok
;
1752 static bfd_reloc_status_type
1753 ppc_section_reloc (abfd
, reloc_entry
, symbol
, data
,
1754 input_section
, output_bfd
, error_message
)
1755 bfd
*abfd ATTRIBUTE_UNUSED
;
1756 arelent
*reloc_entry ATTRIBUTE_UNUSED
;
1757 asymbol
*symbol ATTRIBUTE_UNUSED
;
1758 PTR data ATTRIBUTE_UNUSED
;
1759 asection
*input_section ATTRIBUTE_UNUSED
;
1761 char **error_message ATTRIBUTE_UNUSED
;
1764 DUMP_RELOC("SECTION",reloc_entry
);
1766 if (output_bfd
== (bfd
*) NULL
)
1767 return bfd_reloc_continue
;
1769 return bfd_reloc_ok
;
1772 static bfd_reloc_status_type
1773 ppc_imglue_reloc (abfd
, reloc_entry
, symbol
, data
,
1774 input_section
, output_bfd
, error_message
)
1775 bfd
*abfd ATTRIBUTE_UNUSED
;
1776 arelent
*reloc_entry ATTRIBUTE_UNUSED
;
1777 asymbol
*symbol ATTRIBUTE_UNUSED
;
1778 PTR data ATTRIBUTE_UNUSED
;
1779 asection
*input_section ATTRIBUTE_UNUSED
;
1781 char **error_message ATTRIBUTE_UNUSED
;
1784 DUMP_RELOC("IMGLUE",reloc_entry
);
1786 if (output_bfd
== (bfd
*) NULL
)
1787 return bfd_reloc_continue
;
1789 return bfd_reloc_ok
;
1792 #define MAX_RELOC_INDEX \
1793 (sizeof (ppc_coff_howto_table) / sizeof (ppc_coff_howto_table[0]) - 1)
1795 /* FIXME: There is a possibility that when we read in a reloc from a file,
1796 that there are some bits encoded in the upper portion of the
1797 type field. Not yet implemented. */
1798 static void ppc_coff_rtype2howto
PARAMS ((arelent
*, struct internal_reloc
*));
1801 ppc_coff_rtype2howto (relent
, internal
)
1803 struct internal_reloc
*internal
;
1805 /* We can encode one of three things in the type field, aside from the
1807 1. IMAGE_REL_PPC_NEG - indicates the value field is a subtraction
1808 value, rather than an addition value
1809 2. IMAGE_REL_PPC_BRTAKEN, IMAGE_REL_PPC_BRNTAKEN - indicates that
1810 the branch is expected to be taken or not.
1811 3. IMAGE_REL_PPC_TOCDEFN - toc slot definition in the file
1812 For now, we just strip this stuff to find the type, and ignore it other
1814 reloc_howto_type
*howto
;
1815 unsigned short r_type
= EXTRACT_TYPE (internal
->r_type
);
1816 unsigned short r_flags
= EXTRACT_FLAGS(internal
->r_type
);
1817 unsigned short junk
= EXTRACT_JUNK (internal
->r_type
);
1819 /* The masking process only slices off the bottom byte for r_type. */
1820 if ( r_type
> MAX_RELOC_INDEX
)
1823 /* Check for absolute crap. */
1829 case IMAGE_REL_PPC_ADDR16
:
1830 case IMAGE_REL_PPC_REL24
:
1831 case IMAGE_REL_PPC_ADDR24
:
1832 case IMAGE_REL_PPC_ADDR32
:
1833 case IMAGE_REL_PPC_IFGLUE
:
1834 case IMAGE_REL_PPC_ADDR32NB
:
1835 case IMAGE_REL_PPC_SECTION
:
1836 case IMAGE_REL_PPC_SECREL
:
1837 DUMP_RELOC2 (ppc_coff_howto_table
[r_type
].name
, internal
);
1838 howto
= ppc_coff_howto_table
+ r_type
;
1840 case IMAGE_REL_PPC_IMGLUE
:
1841 DUMP_RELOC2 (ppc_coff_howto_table
[r_type
].name
, internal
);
1842 howto
= ppc_coff_howto_table
+ r_type
;
1844 case IMAGE_REL_PPC_TOCREL16
:
1845 DUMP_RELOC2 (ppc_coff_howto_table
[r_type
].name
, internal
);
1846 if (r_flags
& IMAGE_REL_PPC_TOCDEFN
)
1847 howto
= ppc_coff_howto_table
+ IMAGE_REL_PPC_TOCREL16_DEFN
;
1849 howto
= ppc_coff_howto_table
+ IMAGE_REL_PPC_TOCREL16
;
1853 _("Warning: Unsupported reloc %s [%d] used -- it may not work.\n"),
1854 ppc_coff_howto_table
[r_type
].name
,
1856 howto
= ppc_coff_howto_table
+ r_type
;
1860 relent
->howto
= howto
;
1863 static reloc_howto_type
*
1864 coff_ppc_rtype_to_howto (abfd
, sec
, rel
, h
, sym
, addendp
)
1865 bfd
*abfd ATTRIBUTE_UNUSED
;
1867 struct internal_reloc
*rel
;
1868 struct coff_link_hash_entry
*h ATTRIBUTE_UNUSED
;
1869 struct internal_syment
*sym ATTRIBUTE_UNUSED
;
1872 reloc_howto_type
*howto
;
1874 /* We can encode one of three things in the type field, aside from the
1876 1. IMAGE_REL_PPC_NEG - indicates the value field is a subtraction
1877 value, rather than an addition value
1878 2. IMAGE_REL_PPC_BRTAKEN, IMAGE_REL_PPC_BRNTAKEN - indicates that
1879 the branch is expected to be taken or not.
1880 3. IMAGE_REL_PPC_TOCDEFN - toc slot definition in the file
1881 For now, we just strip this stuff to find the type, and ignore it other
1884 unsigned short r_type
= EXTRACT_TYPE (rel
->r_type
);
1885 unsigned short r_flags
= EXTRACT_FLAGS (rel
->r_type
);
1886 unsigned short junk
= EXTRACT_JUNK (rel
->r_type
);
1888 /* The masking process only slices off the bottom byte for r_type. */
1889 if (r_type
> MAX_RELOC_INDEX
)
1892 /* Check for absolute crap. */
1898 case IMAGE_REL_PPC_ADDR32NB
:
1899 DUMP_RELOC2(ppc_coff_howto_table
[r_type
].name
, rel
);
1900 *addendp
-= pe_data(sec
->output_section
->owner
)->pe_opthdr
.ImageBase
;
1901 howto
= ppc_coff_howto_table
+ r_type
;
1903 case IMAGE_REL_PPC_TOCREL16
:
1904 DUMP_RELOC2(ppc_coff_howto_table
[r_type
].name
, rel
);
1905 if (r_flags
& IMAGE_REL_PPC_TOCDEFN
)
1906 howto
= ppc_coff_howto_table
+ IMAGE_REL_PPC_TOCREL16_DEFN
;
1908 howto
= ppc_coff_howto_table
+ IMAGE_REL_PPC_TOCREL16
;
1910 case IMAGE_REL_PPC_ADDR16
:
1911 case IMAGE_REL_PPC_REL24
:
1912 case IMAGE_REL_PPC_ADDR24
:
1913 case IMAGE_REL_PPC_ADDR32
:
1914 case IMAGE_REL_PPC_IFGLUE
:
1915 case IMAGE_REL_PPC_SECTION
:
1916 case IMAGE_REL_PPC_SECREL
:
1917 DUMP_RELOC2(ppc_coff_howto_table
[r_type
].name
, rel
);
1918 howto
= ppc_coff_howto_table
+ r_type
;
1920 case IMAGE_REL_PPC_IMGLUE
:
1921 DUMP_RELOC2(ppc_coff_howto_table
[r_type
].name
, rel
);
1922 howto
= ppc_coff_howto_table
+ r_type
;
1926 _("Warning: Unsupported reloc %s [%d] used -- it may not work.\n"),
1927 ppc_coff_howto_table
[r_type
].name
,
1929 howto
= ppc_coff_howto_table
+ r_type
;
1936 /* A cheesy little macro to make the code a little more readable. */
1937 #define HOW2MAP(bfd_rtype,ppc_rtype) \
1938 case bfd_rtype: return &ppc_coff_howto_table[ppc_rtype]
1940 static reloc_howto_type
*ppc_coff_reloc_type_lookup
1941 PARAMS ((bfd
*, bfd_reloc_code_real_type
));
1943 static reloc_howto_type
*
1944 ppc_coff_reloc_type_lookup (abfd
, code
)
1945 bfd
*abfd ATTRIBUTE_UNUSED
;
1946 bfd_reloc_code_real_type code
;
1950 HOW2MAP(BFD_RELOC_32_GOTOFF
, IMAGE_REL_PPC_IMGLUE
);
1951 HOW2MAP(BFD_RELOC_16_GOT_PCREL
, IMAGE_REL_PPC_IFGLUE
);
1952 HOW2MAP(BFD_RELOC_16
, IMAGE_REL_PPC_ADDR16
);
1953 HOW2MAP(BFD_RELOC_PPC_B26
, IMAGE_REL_PPC_REL24
);
1954 HOW2MAP(BFD_RELOC_PPC_BA26
, IMAGE_REL_PPC_ADDR24
);
1955 HOW2MAP(BFD_RELOC_PPC_TOC16
, IMAGE_REL_PPC_TOCREL16
);
1956 HOW2MAP(BFD_RELOC_16_GOTOFF
, IMAGE_REL_PPC_TOCREL16_DEFN
);
1957 HOW2MAP(BFD_RELOC_32
, IMAGE_REL_PPC_ADDR32
);
1958 HOW2MAP(BFD_RELOC_RVA
, IMAGE_REL_PPC_ADDR32NB
);
1965 static reloc_howto_type
*
1966 ppc_coff_reloc_name_lookup (bfd
*abfd ATTRIBUTE_UNUSED
,
1972 i
< sizeof (ppc_coff_howto_table
) / sizeof (ppc_coff_howto_table
[0]);
1974 if (ppc_coff_howto_table
[i
].name
!= NULL
1975 && strcasecmp (ppc_coff_howto_table
[i
].name
, r_name
) == 0)
1976 return &ppc_coff_howto_table
[i
];
1981 /* Tailor coffcode.h -- macro heaven. */
1983 #define RTYPE2HOWTO(cache_ptr, dst) ppc_coff_rtype2howto (cache_ptr, dst)
1985 /* We use the special COFF backend linker, with our own special touch. */
1987 #define coff_bfd_reloc_type_lookup ppc_coff_reloc_type_lookup
1988 #define coff_bfd_reloc_name_lookup ppc_coff_reloc_name_lookup
1989 #define coff_rtype_to_howto coff_ppc_rtype_to_howto
1990 #define coff_relocate_section coff_ppc_relocate_section
1991 #define coff_bfd_final_link ppc_bfd_coff_final_link
1993 #ifndef COFF_IMAGE_WITH_PE
1996 #define SELECT_RELOC(internal, howto) {internal.r_type=howto->type;}
1998 #define COFF_PAGE_SIZE 0x1000
2000 /* FIXME: This controls some code that used to be in peicode.h and is
2001 now in peigen.c. It will not control the code in peigen.c. If
2002 anybody wants to get this working, you will need to fix that. */
2003 #define POWERPC_LE_PE
2005 #define COFF_SECTION_ALIGNMENT_ENTRIES \
2006 { COFF_SECTION_NAME_EXACT_MATCH (".idata$2"), \
2007 COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 0 }, \
2008 { COFF_SECTION_NAME_EXACT_MATCH (".idata$3"), \
2009 COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 0 }, \
2010 { COFF_SECTION_NAME_EXACT_MATCH (".idata$4"), \
2011 COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 2 }, \
2012 { COFF_SECTION_NAME_EXACT_MATCH (".idata$5"), \
2013 COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 2 }, \
2014 { COFF_SECTION_NAME_EXACT_MATCH (".idata$6"), \
2015 COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 1 }, \
2016 { COFF_SECTION_NAME_EXACT_MATCH (".reloc"), \
2017 COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 1 }
2019 #include "coffcode.h"
2021 #ifndef COFF_IMAGE_WITH_PE
2023 static bfd_boolean ppc_do_last
PARAMS ((bfd
*));
2024 static bfd
*ppc_get_last
PARAMS ((void));
2030 if (abfd
== bfd_of_toc_owner
)
2039 return bfd_of_toc_owner
;
2042 /* This piece of machinery exists only to guarantee that the bfd that holds
2043 the toc section is written last.
2045 This does depend on bfd_make_section attaching a new section to the
2046 end of the section list for the bfd.
2048 This is otherwise intended to be functionally the same as
2049 cofflink.c:_bfd_coff_final_link(). It is specifically different only
2050 where the POWERPC_LE_PE macro modifies the code. It is left in as a
2051 precise form of comment. krk@cygnus.com */
2053 /* Do the final link step. */
2056 ppc_bfd_coff_final_link (abfd
, info
)
2058 struct bfd_link_info
*info
;
2060 bfd_size_type symesz
;
2061 struct coff_final_link_info finfo
;
2062 bfd_boolean debug_merge_allocated
;
2064 struct bfd_link_order
*p
;
2065 bfd_size_type max_sym_count
;
2066 bfd_size_type max_lineno_count
;
2067 bfd_size_type max_reloc_count
;
2068 bfd_size_type max_output_reloc_count
;
2069 bfd_size_type max_contents_size
;
2070 file_ptr rel_filepos
;
2072 file_ptr line_filepos
;
2073 unsigned int linesz
;
2075 bfd_byte
*external_relocs
= NULL
;
2076 char strbuf
[STRING_SIZE_SIZE
];
2079 symesz
= bfd_coff_symesz (abfd
);
2082 finfo
.output_bfd
= abfd
;
2083 finfo
.strtab
= NULL
;
2084 finfo
.section_info
= NULL
;
2085 finfo
.last_file_index
= -1;
2086 finfo
.last_bf_index
= -1;
2087 finfo
.internal_syms
= NULL
;
2088 finfo
.sec_ptrs
= NULL
;
2089 finfo
.sym_indices
= NULL
;
2090 finfo
.outsyms
= NULL
;
2091 finfo
.linenos
= NULL
;
2092 finfo
.contents
= NULL
;
2093 finfo
.external_relocs
= NULL
;
2094 finfo
.internal_relocs
= NULL
;
2095 debug_merge_allocated
= FALSE
;
2097 coff_data (abfd
)->link_info
= info
;
2099 finfo
.strtab
= _bfd_stringtab_init ();
2100 if (finfo
.strtab
== NULL
)
2103 if (! coff_debug_merge_hash_table_init (&finfo
.debug_merge
))
2105 debug_merge_allocated
= TRUE
;
2107 /* Compute the file positions for all the sections. */
2108 if (! abfd
->output_has_begun
)
2110 if (! bfd_coff_compute_section_file_positions (abfd
))
2114 /* Count the line numbers and relocation entries required for the
2115 output file. Set the file positions for the relocs. */
2116 rel_filepos
= obj_relocbase (abfd
);
2117 relsz
= bfd_coff_relsz (abfd
);
2118 max_contents_size
= 0;
2119 max_lineno_count
= 0;
2120 max_reloc_count
= 0;
2122 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
2125 o
->lineno_count
= 0;
2127 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
2129 if (p
->type
== bfd_indirect_link_order
)
2133 sec
= p
->u
.indirect
.section
;
2135 /* Mark all sections which are to be included in the
2136 link. This will normally be every section. We need
2137 to do this so that we can identify any sections which
2138 the linker has decided to not include. */
2139 sec
->linker_mark
= TRUE
;
2141 if (info
->strip
== strip_none
2142 || info
->strip
== strip_some
)
2143 o
->lineno_count
+= sec
->lineno_count
;
2145 if (info
->relocatable
)
2146 o
->reloc_count
+= sec
->reloc_count
;
2148 if (sec
->rawsize
> max_contents_size
)
2149 max_contents_size
= sec
->rawsize
;
2150 if (sec
->size
> max_contents_size
)
2151 max_contents_size
= sec
->size
;
2152 if (sec
->lineno_count
> max_lineno_count
)
2153 max_lineno_count
= sec
->lineno_count
;
2154 if (sec
->reloc_count
> max_reloc_count
)
2155 max_reloc_count
= sec
->reloc_count
;
2157 else if (info
->relocatable
2158 && (p
->type
== bfd_section_reloc_link_order
2159 || p
->type
== bfd_symbol_reloc_link_order
))
2162 if (o
->reloc_count
== 0)
2166 o
->flags
|= SEC_RELOC
;
2167 o
->rel_filepos
= rel_filepos
;
2168 rel_filepos
+= o
->reloc_count
* relsz
;
2172 /* If doing a relocatable link, allocate space for the pointers we
2174 if (info
->relocatable
)
2178 /* We use section_count + 1, rather than section_count, because
2179 the target_index fields are 1 based. */
2180 amt
= abfd
->section_count
+ 1;
2181 amt
*= sizeof (struct coff_link_section_info
);
2182 finfo
.section_info
= (struct coff_link_section_info
*) bfd_malloc (amt
);
2184 if (finfo
.section_info
== NULL
)
2187 for (i
= 0; i
<= abfd
->section_count
; i
++)
2189 finfo
.section_info
[i
].relocs
= NULL
;
2190 finfo
.section_info
[i
].rel_hashes
= NULL
;
2194 /* We now know the size of the relocs, so we can determine the file
2195 positions of the line numbers. */
2196 line_filepos
= rel_filepos
;
2197 linesz
= bfd_coff_linesz (abfd
);
2198 max_output_reloc_count
= 0;
2200 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
2202 if (o
->lineno_count
== 0)
2203 o
->line_filepos
= 0;
2206 o
->line_filepos
= line_filepos
;
2207 line_filepos
+= o
->lineno_count
* linesz
;
2210 if (o
->reloc_count
!= 0)
2212 /* We don't know the indices of global symbols until we have
2213 written out all the local symbols. For each section in
2214 the output file, we keep an array of pointers to hash
2215 table entries. Each entry in the array corresponds to a
2216 reloc. When we find a reloc against a global symbol, we
2217 set the corresponding entry in this array so that we can
2218 fix up the symbol index after we have written out all the
2221 Because of this problem, we also keep the relocs in
2222 memory until the end of the link. This wastes memory,
2223 but only when doing a relocatable link, which is not the
2225 BFD_ASSERT (info
->relocatable
);
2226 amt
= o
->reloc_count
;
2227 amt
*= sizeof (struct internal_reloc
);
2228 finfo
.section_info
[o
->target_index
].relocs
=
2229 (struct internal_reloc
*) bfd_malloc (amt
);
2230 amt
= o
->reloc_count
;
2231 amt
*= sizeof (struct coff_link_hash_entry
*);
2232 finfo
.section_info
[o
->target_index
].rel_hashes
=
2233 (struct coff_link_hash_entry
**) bfd_malloc (amt
);
2234 if (finfo
.section_info
[o
->target_index
].relocs
== NULL
2235 || finfo
.section_info
[o
->target_index
].rel_hashes
== NULL
)
2238 if (o
->reloc_count
> max_output_reloc_count
)
2239 max_output_reloc_count
= o
->reloc_count
;
2242 /* Reset the reloc and lineno counts, so that we can use them to
2243 count the number of entries we have output so far. */
2245 o
->lineno_count
= 0;
2248 obj_sym_filepos (abfd
) = line_filepos
;
2250 /* Figure out the largest number of symbols in an input BFD. Take
2251 the opportunity to clear the output_has_begun fields of all the
2254 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
2258 sub
->output_has_begun
= FALSE
;
2259 sz
= obj_raw_syment_count (sub
);
2260 if (sz
> max_sym_count
)
2264 /* Allocate some buffers used while linking. */
2265 amt
= max_sym_count
* sizeof (struct internal_syment
);
2266 finfo
.internal_syms
= (struct internal_syment
*) bfd_malloc (amt
);
2267 amt
= max_sym_count
* sizeof (asection
*);
2268 finfo
.sec_ptrs
= (asection
**) bfd_malloc (amt
);
2269 amt
= max_sym_count
* sizeof (long);
2270 finfo
.sym_indices
= (long *) bfd_malloc (amt
);
2271 amt
= (max_sym_count
+ 1) * symesz
;
2272 finfo
.outsyms
= (bfd_byte
*) bfd_malloc (amt
);
2273 amt
= max_lineno_count
* bfd_coff_linesz (abfd
);
2274 finfo
.linenos
= (bfd_byte
*) bfd_malloc (amt
);
2275 finfo
.contents
= (bfd_byte
*) bfd_malloc (max_contents_size
);
2276 finfo
.external_relocs
= (bfd_byte
*) bfd_malloc (max_reloc_count
* relsz
);
2277 if (! info
->relocatable
)
2279 amt
= max_reloc_count
* sizeof (struct internal_reloc
);
2280 finfo
.internal_relocs
= (struct internal_reloc
*) bfd_malloc (amt
);
2282 if ((finfo
.internal_syms
== NULL
&& max_sym_count
> 0)
2283 || (finfo
.sec_ptrs
== NULL
&& max_sym_count
> 0)
2284 || (finfo
.sym_indices
== NULL
&& max_sym_count
> 0)
2285 || finfo
.outsyms
== NULL
2286 || (finfo
.linenos
== NULL
&& max_lineno_count
> 0)
2287 || (finfo
.contents
== NULL
&& max_contents_size
> 0)
2288 || (finfo
.external_relocs
== NULL
&& max_reloc_count
> 0)
2289 || (! info
->relocatable
2290 && finfo
.internal_relocs
== NULL
2291 && max_reloc_count
> 0))
2294 /* We now know the position of everything in the file, except that
2295 we don't know the size of the symbol table and therefore we don't
2296 know where the string table starts. We just build the string
2297 table in memory as we go along. We process all the relocations
2298 for a single input file at once. */
2299 obj_raw_syment_count (abfd
) = 0;
2301 if (coff_backend_info (abfd
)->_bfd_coff_start_final_link
)
2303 if (! bfd_coff_start_final_link (abfd
, info
))
2307 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
2309 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
2311 if (p
->type
== bfd_indirect_link_order
2312 && (bfd_get_flavour (p
->u
.indirect
.section
->owner
)
2313 == bfd_target_coff_flavour
))
2315 sub
= p
->u
.indirect
.section
->owner
;
2316 #ifdef POWERPC_LE_PE
2317 if (! sub
->output_has_begun
&& !ppc_do_last(sub
))
2319 if (! sub
->output_has_begun
)
2322 if (! _bfd_coff_link_input_bfd (&finfo
, sub
))
2324 sub
->output_has_begun
= TRUE
;
2327 else if (p
->type
== bfd_section_reloc_link_order
2328 || p
->type
== bfd_symbol_reloc_link_order
)
2330 if (! _bfd_coff_reloc_link_order (abfd
, &finfo
, o
, p
))
2335 if (! _bfd_default_link_order (abfd
, info
, o
, p
))
2341 #ifdef POWERPC_LE_PE
2343 bfd
* last_one
= ppc_get_last();
2346 if (! _bfd_coff_link_input_bfd (&finfo
, last_one
))
2349 last_one
->output_has_begun
= TRUE
;
2353 /* Free up the buffers used by _bfd_coff_link_input_bfd. */
2354 coff_debug_merge_hash_table_free (&finfo
.debug_merge
);
2355 debug_merge_allocated
= FALSE
;
2357 if (finfo
.internal_syms
!= NULL
)
2359 free (finfo
.internal_syms
);
2360 finfo
.internal_syms
= NULL
;
2362 if (finfo
.sec_ptrs
!= NULL
)
2364 free (finfo
.sec_ptrs
);
2365 finfo
.sec_ptrs
= NULL
;
2367 if (finfo
.sym_indices
!= NULL
)
2369 free (finfo
.sym_indices
);
2370 finfo
.sym_indices
= NULL
;
2372 if (finfo
.linenos
!= NULL
)
2374 free (finfo
.linenos
);
2375 finfo
.linenos
= NULL
;
2377 if (finfo
.contents
!= NULL
)
2379 free (finfo
.contents
);
2380 finfo
.contents
= NULL
;
2382 if (finfo
.external_relocs
!= NULL
)
2384 free (finfo
.external_relocs
);
2385 finfo
.external_relocs
= NULL
;
2387 if (finfo
.internal_relocs
!= NULL
)
2389 free (finfo
.internal_relocs
);
2390 finfo
.internal_relocs
= NULL
;
2393 /* The value of the last C_FILE symbol is supposed to be the symbol
2394 index of the first external symbol. Write it out again if
2396 if (finfo
.last_file_index
!= -1
2397 && (unsigned int) finfo
.last_file
.n_value
!= obj_raw_syment_count (abfd
))
2401 finfo
.last_file
.n_value
= obj_raw_syment_count (abfd
);
2402 bfd_coff_swap_sym_out (abfd
, (PTR
) &finfo
.last_file
,
2403 (PTR
) finfo
.outsyms
);
2404 pos
= obj_sym_filepos (abfd
) + finfo
.last_file_index
* symesz
;
2405 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0
2406 || bfd_bwrite (finfo
.outsyms
, symesz
, abfd
) != symesz
)
2410 /* Write out the global symbols. */
2411 finfo
.failed
= FALSE
;
2412 coff_link_hash_traverse (coff_hash_table (info
), _bfd_coff_write_global_sym
,
2417 /* The outsyms buffer is used by _bfd_coff_write_global_sym. */
2418 if (finfo
.outsyms
!= NULL
)
2420 free (finfo
.outsyms
);
2421 finfo
.outsyms
= NULL
;
2424 if (info
->relocatable
)
2426 /* Now that we have written out all the global symbols, we know
2427 the symbol indices to use for relocs against them, and we can
2428 finally write out the relocs. */
2429 amt
= max_output_reloc_count
* relsz
;
2430 external_relocs
= (bfd_byte
*) bfd_malloc (amt
);
2431 if (external_relocs
== NULL
)
2434 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
2436 struct internal_reloc
*irel
;
2437 struct internal_reloc
*irelend
;
2438 struct coff_link_hash_entry
**rel_hash
;
2441 if (o
->reloc_count
== 0)
2444 irel
= finfo
.section_info
[o
->target_index
].relocs
;
2445 irelend
= irel
+ o
->reloc_count
;
2446 rel_hash
= finfo
.section_info
[o
->target_index
].rel_hashes
;
2447 erel
= external_relocs
;
2448 for (; irel
< irelend
; irel
++, rel_hash
++, erel
+= relsz
)
2450 if (*rel_hash
!= NULL
)
2452 BFD_ASSERT ((*rel_hash
)->indx
>= 0);
2453 irel
->r_symndx
= (*rel_hash
)->indx
;
2455 bfd_coff_swap_reloc_out (abfd
, (PTR
) irel
, (PTR
) erel
);
2458 amt
= relsz
* o
->reloc_count
;
2459 if (bfd_seek (abfd
, o
->rel_filepos
, SEEK_SET
) != 0
2460 || bfd_bwrite ((PTR
) external_relocs
, amt
, abfd
) != amt
)
2464 free (external_relocs
);
2465 external_relocs
= NULL
;
2468 /* Free up the section information. */
2469 if (finfo
.section_info
!= NULL
)
2473 for (i
= 0; i
< abfd
->section_count
; i
++)
2475 if (finfo
.section_info
[i
].relocs
!= NULL
)
2476 free (finfo
.section_info
[i
].relocs
);
2477 if (finfo
.section_info
[i
].rel_hashes
!= NULL
)
2478 free (finfo
.section_info
[i
].rel_hashes
);
2480 free (finfo
.section_info
);
2481 finfo
.section_info
= NULL
;
2484 /* If we have optimized stabs strings, output them. */
2485 if (coff_hash_table (info
)->stab_info
.stabstr
!= NULL
)
2487 if (! _bfd_write_stab_strings (abfd
, &coff_hash_table (info
)->stab_info
))
2491 /* Write out the string table. */
2492 if (obj_raw_syment_count (abfd
) != 0)
2496 pos
= obj_sym_filepos (abfd
) + obj_raw_syment_count (abfd
) * symesz
;
2497 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0)
2500 #if STRING_SIZE_SIZE == 4
2502 _bfd_stringtab_size (finfo
.strtab
) + STRING_SIZE_SIZE
,
2505 #error Change H_PUT_32 above
2508 if (bfd_bwrite (strbuf
, (bfd_size_type
) STRING_SIZE_SIZE
, abfd
)
2509 != STRING_SIZE_SIZE
)
2512 if (! _bfd_stringtab_emit (abfd
, finfo
.strtab
))
2516 _bfd_stringtab_free (finfo
.strtab
);
2518 /* Setting bfd_get_symcount to 0 will cause write_object_contents to
2519 not try to write out the symbols. */
2520 bfd_get_symcount (abfd
) = 0;
2525 if (debug_merge_allocated
)
2526 coff_debug_merge_hash_table_free (&finfo
.debug_merge
);
2527 if (finfo
.strtab
!= NULL
)
2528 _bfd_stringtab_free (finfo
.strtab
);
2529 if (finfo
.section_info
!= NULL
)
2533 for (i
= 0; i
< abfd
->section_count
; i
++)
2535 if (finfo
.section_info
[i
].relocs
!= NULL
)
2536 free (finfo
.section_info
[i
].relocs
);
2537 if (finfo
.section_info
[i
].rel_hashes
!= NULL
)
2538 free (finfo
.section_info
[i
].rel_hashes
);
2540 free (finfo
.section_info
);
2542 if (finfo
.internal_syms
!= NULL
)
2543 free (finfo
.internal_syms
);
2544 if (finfo
.sec_ptrs
!= NULL
)
2545 free (finfo
.sec_ptrs
);
2546 if (finfo
.sym_indices
!= NULL
)
2547 free (finfo
.sym_indices
);
2548 if (finfo
.outsyms
!= NULL
)
2549 free (finfo
.outsyms
);
2550 if (finfo
.linenos
!= NULL
)
2551 free (finfo
.linenos
);
2552 if (finfo
.contents
!= NULL
)
2553 free (finfo
.contents
);
2554 if (finfo
.external_relocs
!= NULL
)
2555 free (finfo
.external_relocs
);
2556 if (finfo
.internal_relocs
!= NULL
)
2557 free (finfo
.internal_relocs
);
2558 if (external_relocs
!= NULL
)
2559 free (external_relocs
);
2564 /* Forward declaration for use by alternative_target field. */
2565 #ifdef TARGET_BIG_SYM
2566 extern const bfd_target TARGET_BIG_SYM
;
2569 /* The transfer vectors that lead the outside world to all of the above. */
2571 #ifdef TARGET_LITTLE_SYM
2572 const bfd_target TARGET_LITTLE_SYM
=
2574 TARGET_LITTLE_NAME
, /* name or coff-arm-little */
2575 bfd_target_coff_flavour
,
2576 BFD_ENDIAN_LITTLE
, /* data byte order is little */
2577 BFD_ENDIAN_LITTLE
, /* header byte order is little */
2579 (HAS_RELOC
| EXEC_P
| /* FIXME: object flags */
2580 HAS_LINENO
| HAS_DEBUG
|
2581 HAS_SYMS
| HAS_LOCALS
| WP_TEXT
| D_PAGED
),
2583 #ifndef COFF_WITH_PE
2584 (SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
), /* section flags */
2586 (SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
/* section flags */
2587 | SEC_LINK_ONCE
| SEC_LINK_DUPLICATES
),
2590 0, /* leading char */
2591 '/', /* ar_pad_char */
2592 15, /* ar_max_namelen??? FIXMEmgo */
2594 bfd_getl64
, bfd_getl_signed_64
, bfd_putl64
,
2595 bfd_getl32
, bfd_getl_signed_32
, bfd_putl32
,
2596 bfd_getl16
, bfd_getl_signed_16
, bfd_putl16
, /* data */
2598 bfd_getl64
, bfd_getl_signed_64
, bfd_putl64
,
2599 bfd_getl32
, bfd_getl_signed_32
, bfd_putl32
,
2600 bfd_getl16
, bfd_getl_signed_16
, bfd_putl16
, /* hdrs */
2602 {_bfd_dummy_target
, coff_object_p
, /* bfd_check_format */
2603 bfd_generic_archive_p
, /* _bfd_dummy_target */ coff_object_p
},
2604 {bfd_false
, coff_mkobject
, _bfd_generic_mkarchive
, /* bfd_set_format */
2606 {bfd_false
, coff_write_object_contents
, /* bfd_write_contents */
2607 _bfd_write_archive_contents
, bfd_false
},
2609 BFD_JUMP_TABLE_GENERIC (coff
),
2610 BFD_JUMP_TABLE_COPY (coff
),
2611 BFD_JUMP_TABLE_CORE (_bfd_nocore
),
2612 BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_coff
),
2613 BFD_JUMP_TABLE_SYMBOLS (coff
),
2614 BFD_JUMP_TABLE_RELOCS (coff
),
2615 BFD_JUMP_TABLE_WRITE (coff
),
2616 BFD_JUMP_TABLE_LINK (coff
),
2617 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic
),
2619 /* Alternative_target. */
2620 #ifdef TARGET_BIG_SYM
2630 #ifdef TARGET_BIG_SYM
2631 const bfd_target TARGET_BIG_SYM
=
2634 bfd_target_coff_flavour
,
2635 BFD_ENDIAN_BIG
, /* data byte order is big */
2636 BFD_ENDIAN_BIG
, /* header byte order is big */
2638 (HAS_RELOC
| EXEC_P
| /* FIXME: object flags */
2639 HAS_LINENO
| HAS_DEBUG
|
2640 HAS_SYMS
| HAS_LOCALS
| WP_TEXT
| D_PAGED
),
2642 #ifndef COFF_WITH_PE
2643 (SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
), /* section flags */
2645 (SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
/* section flags */
2646 | SEC_LINK_ONCE
| SEC_LINK_DUPLICATES
),
2649 0, /* leading char */
2650 '/', /* ar_pad_char */
2651 15, /* ar_max_namelen??? FIXMEmgo */
2653 bfd_getb64
, bfd_getb_signed_64
, bfd_putb64
,
2654 bfd_getb32
, bfd_getb_signed_32
, bfd_putb32
,
2655 bfd_getb16
, bfd_getb_signed_16
, bfd_putb16
, /* data */
2657 bfd_getb64
, bfd_getb_signed_64
, bfd_putb64
,
2658 bfd_getb32
, bfd_getb_signed_32
, bfd_putb32
,
2659 bfd_getb16
, bfd_getb_signed_16
, bfd_putb16
, /* hdrs */
2661 {_bfd_dummy_target
, coff_object_p
, /* bfd_check_format */
2662 bfd_generic_archive_p
, /* _bfd_dummy_target */ coff_object_p
},
2663 {bfd_false
, coff_mkobject
, _bfd_generic_mkarchive
, /* bfd_set_format */
2665 {bfd_false
, coff_write_object_contents
, /* bfd_write_contents */
2666 _bfd_write_archive_contents
, bfd_false
},
2668 BFD_JUMP_TABLE_GENERIC (coff
),
2669 BFD_JUMP_TABLE_COPY (coff
),
2670 BFD_JUMP_TABLE_CORE (_bfd_nocore
),
2671 BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_coff
),
2672 BFD_JUMP_TABLE_SYMBOLS (coff
),
2673 BFD_JUMP_TABLE_RELOCS (coff
),
2674 BFD_JUMP_TABLE_WRITE (coff
),
2675 BFD_JUMP_TABLE_LINK (coff
),
2676 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic
),
2678 /* Alternative_target. */
2679 #ifdef TARGET_LITTLE_SYM
2680 & TARGET_LITTLE_SYM
,