1 /* BFD back-end for PowerPC Microsoft Portable Executable files.
2 Copyright 1990, 91, 92, 93, 94, 95, 96, 97, 98, 1999
3 Free Software Foundation, Inc.
5 Original version pieced together by Kim Knuttila (krk@cygnus.com)
7 There is nothing new under the sun. This file draws a lot on other
8 coff files, in particular, those for the rs/6000, alpha, mips, and
9 intel backends, and the PE work for the arm.
11 This file is part of BFD, the Binary File Descriptor library.
13 This program is free software; you can redistribute it and/or modify
14 it under the terms of the GNU General Public License as published by
15 the Free Software Foundation; either version 2 of the License, or
16 (at your option) any later version.
18 This program is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, write to the Free Software
25 Foundation, 59 Temple Place - Suite 330,
26 Boston, MA 02111-1307, USA. */
30 - relocs generated by gas
31 - ld will link files, but they do not run.
32 - dlltool will not produce correct output in some .reloc cases, and will
33 not produce the right glue code for dll function calls.
42 #include "coff/powerpc.h"
43 #include "coff/internal.h"
51 #define BADMAG(x) PPCBADMAG(x)
55 /* This file is compiled more than once, but we only compile the
56 final_link routine once. */
57 extern boolean ppc_bfd_coff_final_link
58 PARAMS ((bfd
*, struct bfd_link_info
*));
59 extern void dump_toc
PARAMS ((PTR
));
61 /* The toc is a set of bfd_vma fields. We use the fact that valid */
62 /* addresses are even (i.e. the bit representing "1" is off) to allow */
63 /* us to encode a little extra information in the field */
64 /* - Unallocated addresses are intialized to 1. */
65 /* - Allocated addresses are even numbers. */
66 /* The first time we actually write a reference to the toc in the bfd, */
67 /* we want to record that fact in a fixup file (if it is asked for), so */
68 /* we keep track of whether or not an address has been written by marking */
69 /* the low order bit with a "1" upon writing */
71 #define SET_UNALLOCATED(x) ((x) = 1)
72 #define IS_UNALLOCATED(x) ((x) == 1)
74 #define IS_WRITTEN(x) ((x) & 1)
75 #define MARK_AS_WRITTEN(x) ((x) |= 1)
76 #define MAKE_ADDR_AGAIN(x) ((x) &= ~1)
79 /* Turn on this check if you suspect something amiss in the hash tables */
82 /* Need a 7 char string for an eye catcher */
85 #define HASH_CHECK_DCL char eye_catcher[8];
86 #define HASH_CHECK_INIT(ret) strcpy(ret->eye_catcher, EYE)
87 #define HASH_CHECK(addr) \
88 if (strcmp(addr->eye_catcher, EYE) != 0) \
91 _("File %s, line %d, Hash check failure, bad eye %8s\n"), \
92 __FILE__, __LINE__, addr->eye_catcher); \
99 #define HASH_CHECK_DCL
100 #define HASH_CHECK_INIT(ret)
101 #define HASH_CHECK(addr)
105 /* In order not to add an int to every hash table item for every coff
106 linker, we define our own hash table, derived from the coff one */
108 /* PE linker hash table entries. */
110 struct ppc_coff_link_hash_entry
112 struct coff_link_hash_entry root
; /* First entry, as required */
114 /* As we wonder around the relocs, we'll keep the assigned toc_offset
116 bfd_vma toc_offset
; /* Our addition, as required */
118 unsigned long int glue_insn
;
124 /* PE linker hash table. */
126 struct ppc_coff_link_hash_table
128 struct coff_link_hash_table root
; /* First entry, as required */
131 static struct bfd_hash_entry
*ppc_coff_link_hash_newfunc
132 PARAMS ((struct bfd_hash_entry
*, struct bfd_hash_table
*,
134 static boolean ppc_coff_link_hash_table_init
135 PARAMS ((struct ppc_coff_link_hash_table
*, bfd
*,
136 struct bfd_hash_entry
*(*) (struct bfd_hash_entry
*,
137 struct bfd_hash_table
*,
139 static struct bfd_link_hash_table
*ppc_coff_link_hash_table_create
141 static boolean coff_ppc_relocate_section
142 PARAMS ((bfd
*, struct bfd_link_info
*, bfd
*, asection
*, bfd_byte
*,
143 struct internal_reloc
*, struct internal_syment
*, asection
**));
144 static reloc_howto_type
*coff_ppc_rtype_to_howto
145 PARAMS ((bfd
*, asection
*, struct internal_reloc
*,
146 struct coff_link_hash_entry
*, struct internal_syment
*,
149 /* Routine to create an entry in the link hash table. */
151 static struct bfd_hash_entry
*
152 ppc_coff_link_hash_newfunc (entry
, table
, string
)
153 struct bfd_hash_entry
*entry
;
154 struct bfd_hash_table
*table
;
157 struct ppc_coff_link_hash_entry
*ret
=
158 (struct ppc_coff_link_hash_entry
*) entry
;
160 /* Allocate the structure if it has not already been allocated by a
162 if (ret
== (struct ppc_coff_link_hash_entry
*) NULL
)
163 ret
= (struct ppc_coff_link_hash_entry
*)
164 bfd_hash_allocate (table
,
165 sizeof (struct ppc_coff_link_hash_entry
));
167 if (ret
== (struct ppc_coff_link_hash_entry
*) NULL
)
170 /* Call the allocation method of the superclass. */
171 ret
= ((struct ppc_coff_link_hash_entry
*)
172 _bfd_coff_link_hash_newfunc ((struct bfd_hash_entry
*) ret
,
177 /* Initialize the local fields. */
178 SET_UNALLOCATED(ret
->toc_offset
);
179 ret
->symbol_is_glue
= 0;
182 HASH_CHECK_INIT(ret
);
185 return (struct bfd_hash_entry
*) ret
;
188 /* Initialize a PE linker hash table. */
191 ppc_coff_link_hash_table_init (table
, abfd
, newfunc
)
192 struct ppc_coff_link_hash_table
*table
;
194 struct bfd_hash_entry
*(*newfunc
) PARAMS ((struct bfd_hash_entry
*,
195 struct bfd_hash_table
*,
198 return _bfd_coff_link_hash_table_init (&table
->root
, abfd
, newfunc
);
201 /* Create a PE linker hash table. */
203 static struct bfd_link_hash_table
*
204 ppc_coff_link_hash_table_create (abfd
)
207 struct ppc_coff_link_hash_table
*ret
;
209 ret
= ((struct ppc_coff_link_hash_table
*)
210 bfd_alloc (abfd
, sizeof (struct ppc_coff_link_hash_table
)));
213 if (! ppc_coff_link_hash_table_init (ret
, abfd
,
214 ppc_coff_link_hash_newfunc
))
216 bfd_release (abfd
, ret
);
217 return (struct bfd_link_hash_table
*) NULL
;
219 return &ret
->root
.root
;
222 /* Now, tailor coffcode.h to use our hash stuff */
224 #define coff_bfd_link_hash_table_create ppc_coff_link_hash_table_create
227 /* The nt loader points the toc register to &toc + 32768, in order to */
228 /* use the complete range of a 16-bit displacement. We have to adjust */
229 /* for this when we fix up loads displaced off the toc reg. */
230 #define TOC_LOAD_ADJUSTMENT (-32768)
231 #define TOC_SECTION_NAME ".private.toc"
233 /* The main body of code is in coffcode.h. */
235 #define COFF_DEFAULT_SECTION_ALIGNMENT_POWER (3)
237 /* In case we're on a 32-bit machine, construct a 64-bit "-1" value
238 from smaller values. Start with zero, widen, *then* decrement. */
239 #define MINUS_ONE (((bfd_vma)0) - 1)
241 /* these should definitely go in a header file somewhere... */
244 #define IMAGE_REL_PPC_ABSOLUTE 0x0000
247 #define IMAGE_REL_PPC_ADDR64 0x0001
250 #define IMAGE_REL_PPC_ADDR32 0x0002
252 /* 26-bit address, shifted left 2 (branch absolute) */
253 #define IMAGE_REL_PPC_ADDR24 0x0003
256 #define IMAGE_REL_PPC_ADDR16 0x0004
258 /* 16-bit address, shifted left 2 (load doubleword) */
259 #define IMAGE_REL_PPC_ADDR14 0x0005
261 /* 26-bit PC-relative offset, shifted left 2 (branch relative) */
262 #define IMAGE_REL_PPC_REL24 0x0006
264 /* 16-bit PC-relative offset, shifted left 2 (br cond relative) */
265 #define IMAGE_REL_PPC_REL14 0x0007
267 /* 16-bit offset from TOC base */
268 #define IMAGE_REL_PPC_TOCREL16 0x0008
270 /* 16-bit offset from TOC base, shifted left 2 (load doubleword) */
271 #define IMAGE_REL_PPC_TOCREL14 0x0009
273 /* 32-bit addr w/o image base */
274 #define IMAGE_REL_PPC_ADDR32NB 0x000A
276 /* va of containing section (as in an image sectionhdr) */
277 #define IMAGE_REL_PPC_SECREL 0x000B
279 /* sectionheader number */
280 #define IMAGE_REL_PPC_SECTION 0x000C
282 /* substitute TOC restore instruction iff symbol is glue code */
283 #define IMAGE_REL_PPC_IFGLUE 0x000D
285 /* symbol is glue code; virtual address is TOC restore instruction */
286 #define IMAGE_REL_PPC_IMGLUE 0x000E
288 /* va of containing section (limited to 16 bits) */
289 #define IMAGE_REL_PPC_SECREL16 0x000F
291 /* stuff to handle immediate data when the number of bits in the */
292 /* data is greater than the number of bits in the immediate field */
293 /* We need to do (usually) 32 bit arithmetic on 16 bit chunks */
294 #define IMAGE_REL_PPC_REFHI 0x0010
295 #define IMAGE_REL_PPC_REFLO 0x0011
296 #define IMAGE_REL_PPC_PAIR 0x0012
298 /* This is essentially the same as tocrel16, with TOCDEFN assumed */
299 #define IMAGE_REL_PPC_TOCREL16_DEFN 0x0013
301 /* Flag bits in IMAGE_RELOCATION.TYPE */
303 /* subtract reloc value rather than adding it */
304 #define IMAGE_REL_PPC_NEG 0x0100
306 /* fix branch prediction bit to predict branch taken */
307 #define IMAGE_REL_PPC_BRTAKEN 0x0200
309 /* fix branch prediction bit to predict branch not taken */
310 #define IMAGE_REL_PPC_BRNTAKEN 0x0400
312 /* toc slot defined in file (or, data in toc) */
313 #define IMAGE_REL_PPC_TOCDEFN 0x0800
315 /* masks to isolate above values in IMAGE_RELOCATION.Type */
316 #define IMAGE_REL_PPC_TYPEMASK 0x00FF
317 #define IMAGE_REL_PPC_FLAGMASK 0x0F00
319 #define EXTRACT_TYPE(x) ((x) & IMAGE_REL_PPC_TYPEMASK)
320 #define EXTRACT_FLAGS(x) ((x) & IMAGE_REL_PPC_FLAGMASK)
321 #define EXTRACT_JUNK(x) \
322 ((x) & ~(IMAGE_REL_PPC_TYPEMASK | IMAGE_REL_PPC_FLAGMASK))
325 /* static helper functions to make relocation work */
326 /* (Work In Progress) */
328 static bfd_reloc_status_type ppc_refhi_reloc
PARAMS ((bfd
*abfd
,
336 static bfd_reloc_status_type ppc_reflo_reloc
PARAMS ((bfd
*abfd
,
344 static bfd_reloc_status_type ppc_pair_reloc
PARAMS ((bfd
*abfd
,
353 static bfd_reloc_status_type ppc_toc16_reloc
PARAMS ((bfd
*abfd
,
362 static bfd_reloc_status_type ppc_addr32nb_reloc
PARAMS ((bfd
*abfd
,
370 static bfd_reloc_status_type ppc_section_reloc
PARAMS ((bfd
*abfd
,
378 static bfd_reloc_status_type ppc_secrel_reloc
PARAMS ((bfd
*abfd
,
386 static bfd_reloc_status_type ppc_imglue_reloc
PARAMS ((bfd
*abfd
,
396 static boolean in_reloc_p
PARAMS((bfd
*abfd
, reloc_howto_type
*howto
));
399 /* FIXME: It'll take a while to get through all of these. I only need a few to
400 get us started, so those I'll make sure work. Those marked FIXME are either
401 completely unverified or have a specific unknown marked in the comment */
403 /*---------------------------------------------------------------------------*/
405 /* Relocation entries for Windows/NT on PowerPC. */
407 /* From the document "" we find the following listed as used relocs: */
409 /* ABSOLUTE : The noop */
410 /* ADDR[64|32|16] : fields that hold addresses in data fields or the */
411 /* 16 bit displacement field on a load/store. */
412 /* ADDR[24|14] : fields that hold addresses in branch and cond */
413 /* branches. These represent [26|16] bit addresses. */
414 /* The low order 2 bits are preserved. */
415 /* REL[24|14] : branches relative to the Instruction Address */
416 /* register. These represent [26|16] bit addresses, */
417 /* as before. The instruction field will be zero, and */
418 /* the address of the SYM will be inserted at link time. */
419 /* TOCREL16 : 16 bit displacement field referring to a slot in */
421 /* TOCREL14 : 16 bit displacement field, similar to REL14 or ADDR14. */
422 /* ADDR32NB : 32 bit address relative to the virtual origin. */
423 /* (On the alpha, this is always a linker generated thunk)*/
424 /* (i.e. 32bit addr relative to the image base) */
425 /* SECREL : The value is relative to the start of the section */
426 /* containing the symbol. */
427 /* SECTION : access to the header containing the item. Supports the */
428 /* codeview debugger. */
430 /* In particular, note that the document does not indicate that the */
431 /* relocations listed in the header file are used. */
435 /*---------------------------------------------------------------------------*/
437 static reloc_howto_type ppc_coff_howto_table
[] =
439 /* IMAGE_REL_PPC_ABSOLUTE 0x0000 NOP */
441 HOWTO (IMAGE_REL_PPC_ABSOLUTE
, /* type */
443 0, /* size (0 = byte, 1 = short, 2 = long) */
445 false, /* pc_relative */
447 complain_overflow_dont
, /* dont complain_on_overflow */
448 0, /* special_function */
449 "ABSOLUTE", /* name */
450 false, /* partial_inplace */
453 false), /* pcrel_offset */
455 /* IMAGE_REL_PPC_ADDR64 0x0001 64-bit address */
457 HOWTO(IMAGE_REL_PPC_ADDR64
, /* type */
459 3, /* size (0 = byte, 1 = short, 2 = long) */
461 false, /* pc_relative */
463 complain_overflow_bitfield
, /* complain_on_overflow */
464 0, /* special_function */
466 true, /* partial_inplace */
467 MINUS_ONE
, /* src_mask */
468 MINUS_ONE
, /* dst_mask */
469 false), /* pcrel_offset */
471 /* IMAGE_REL_PPC_ADDR32 0x0002 32-bit address */
473 HOWTO (IMAGE_REL_PPC_ADDR32
, /* type */
475 2, /* size (0 = byte, 1 = short, 2 = long) */
477 false, /* pc_relative */
479 complain_overflow_bitfield
, /* complain_on_overflow */
480 0, /* special_function */
482 true, /* partial_inplace */
483 0xffffffff, /* src_mask */
484 0xffffffff, /* dst_mask */
485 false), /* pcrel_offset */
487 /* IMAGE_REL_PPC_ADDR24 0x0003 26-bit address, shifted left 2 (branch absolute) */
488 /* the LI field is in bit 6 through bit 29 is 24 bits, + 2 for the shift */
489 /* Of course, That's the IBM approved bit numbering, which is not what */
490 /* anyone else uses.... The li field is in bit 2 thru 25 */
492 HOWTO (IMAGE_REL_PPC_ADDR24
, /* type */
494 2, /* size (0 = byte, 1 = short, 2 = long) */
496 false, /* pc_relative */
498 complain_overflow_bitfield
, /* complain_on_overflow */
499 0, /* special_function */
501 true, /* partial_inplace */
502 0x07fffffc, /* src_mask */
503 0x07fffffc, /* dst_mask */
504 false), /* pcrel_offset */
506 /* IMAGE_REL_PPC_ADDR16 0x0004 16-bit address */
508 HOWTO (IMAGE_REL_PPC_ADDR16
, /* type */
510 1, /* size (0 = byte, 1 = short, 2 = long) */
512 false, /* pc_relative */
514 complain_overflow_signed
, /* complain_on_overflow */
515 0, /* special_function */
517 true, /* partial_inplace */
518 0xffff, /* src_mask */
519 0xffff, /* dst_mask */
520 false), /* pcrel_offset */
522 /* IMAGE_REL_PPC_ADDR14 0x0005 */
523 /* 16-bit address, shifted left 2 (load doubleword) */
524 /* FIXME: the mask is likely wrong, and the bit position may be as well */
526 HOWTO (IMAGE_REL_PPC_ADDR14
, /* type */
528 1, /* size (0 = byte, 1 = short, 2 = long) */
530 false, /* pc_relative */
532 complain_overflow_signed
, /* complain_on_overflow */
533 0, /* special_function */
535 true, /* partial_inplace */
536 0xffff, /* src_mask */
537 0xffff, /* dst_mask */
538 false), /* pcrel_offset */
540 /* IMAGE_REL_PPC_REL24 0x0006 */
541 /* 26-bit PC-relative offset, shifted left 2 (branch relative) */
543 HOWTO (IMAGE_REL_PPC_REL24
, /* type */
545 2, /* size (0 = byte, 1 = short, 2 = long) */
547 true, /* pc_relative */
549 complain_overflow_signed
, /* complain_on_overflow */
550 0, /* special_function */
552 true, /* partial_inplace */
553 0x3fffffc, /* src_mask */
554 0x3fffffc, /* dst_mask */
555 false), /* pcrel_offset */
557 /* IMAGE_REL_PPC_REL14 0x0007 */
558 /* 16-bit PC-relative offset, shifted left 2 (br cond relative) */
559 /* FIXME: the mask is likely wrong, and the bit position may be as well */
560 /* FIXME: how does it know how far to shift? */
562 HOWTO (IMAGE_REL_PPC_ADDR14
, /* type */
564 1, /* size (0 = byte, 1 = short, 2 = long) */
566 false, /* pc_relative */
568 complain_overflow_signed
, /* complain_on_overflow */
569 0, /* special_function */
571 true, /* partial_inplace */
572 0xffff, /* src_mask */
573 0xffff, /* dst_mask */
574 true), /* pcrel_offset */
576 /* IMAGE_REL_PPC_TOCREL16 0x0008 */
577 /* 16-bit offset from TOC base */
579 HOWTO (IMAGE_REL_PPC_TOCREL16
,/* type */
581 1, /* size (0 = byte, 1 = short, 2 = long) */
583 false, /* pc_relative */
585 complain_overflow_dont
, /* complain_on_overflow */
586 ppc_toc16_reloc
, /* special_function */
587 "TOCREL16", /* name */
588 false, /* partial_inplace */
589 0xffff, /* src_mask */
590 0xffff, /* dst_mask */
591 false), /* pcrel_offset */
593 /* IMAGE_REL_PPC_TOCREL14 0x0009 */
594 /* 16-bit offset from TOC base, shifted left 2 (load doubleword) */
596 HOWTO (IMAGE_REL_PPC_TOCREL14
,/* type */
598 1, /* size (0 = byte, 1 = short, 2 = long) */
600 false, /* pc_relative */
602 complain_overflow_signed
, /* complain_on_overflow */
603 0, /* special_function */
604 "TOCREL14", /* name */
605 false, /* partial_inplace */
606 0xffff, /* src_mask */
607 0xffff, /* dst_mask */
608 false), /* pcrel_offset */
610 /* IMAGE_REL_PPC_ADDR32NB 0x000A */
611 /* 32-bit addr w/ image base */
613 HOWTO (IMAGE_REL_PPC_ADDR32NB
,/* type */
615 2, /* size (0 = byte, 1 = short, 2 = long) */
617 false, /* pc_relative */
619 complain_overflow_signed
, /* complain_on_overflow */
620 0, /* special_function */
621 "ADDR32NB", /* name */
622 true, /* partial_inplace */
623 0xffffffff, /* src_mask */
624 0xffffffff, /* dst_mask */
625 false), /* pcrel_offset */
627 /* IMAGE_REL_PPC_SECREL 0x000B */
628 /* va of containing section (as in an image sectionhdr) */
630 HOWTO (IMAGE_REL_PPC_SECREL
,/* type */
632 2, /* size (0 = byte, 1 = short, 2 = long) */
634 false, /* pc_relative */
636 complain_overflow_signed
, /* complain_on_overflow */
637 ppc_secrel_reloc
, /* special_function */
639 true, /* partial_inplace */
640 0xffffffff, /* src_mask */
641 0xffffffff, /* dst_mask */
642 true), /* pcrel_offset */
644 /* IMAGE_REL_PPC_SECTION 0x000C */
645 /* sectionheader number */
647 HOWTO (IMAGE_REL_PPC_SECTION
,/* type */
649 2, /* size (0 = byte, 1 = short, 2 = long) */
651 false, /* pc_relative */
653 complain_overflow_signed
, /* complain_on_overflow */
654 ppc_section_reloc
, /* special_function */
655 "SECTION", /* name */
656 true, /* partial_inplace */
657 0xffffffff, /* src_mask */
658 0xffffffff, /* dst_mask */
659 true), /* pcrel_offset */
661 /* IMAGE_REL_PPC_IFGLUE 0x000D */
662 /* substitute TOC restore instruction iff symbol is glue code */
664 HOWTO (IMAGE_REL_PPC_IFGLUE
,/* type */
666 2, /* size (0 = byte, 1 = short, 2 = long) */
668 false, /* pc_relative */
670 complain_overflow_signed
, /* complain_on_overflow */
671 0, /* special_function */
673 true, /* partial_inplace */
674 0xffffffff, /* src_mask */
675 0xffffffff, /* dst_mask */
676 false), /* pcrel_offset */
678 /* IMAGE_REL_PPC_IMGLUE 0x000E */
679 /* symbol is glue code; virtual address is TOC restore instruction */
681 HOWTO (IMAGE_REL_PPC_IMGLUE
,/* type */
683 2, /* size (0 = byte, 1 = short, 2 = long) */
685 false, /* pc_relative */
687 complain_overflow_dont
, /* complain_on_overflow */
688 ppc_imglue_reloc
, /* special_function */
690 false, /* partial_inplace */
691 0xffffffff, /* src_mask */
692 0xffffffff, /* dst_mask */
693 false), /* pcrel_offset */
695 /* IMAGE_REL_PPC_SECREL16 0x000F */
696 /* va of containing section (limited to 16 bits) */
698 HOWTO (IMAGE_REL_PPC_SECREL16
,/* type */
700 1, /* size (0 = byte, 1 = short, 2 = long) */
702 false, /* pc_relative */
704 complain_overflow_signed
, /* complain_on_overflow */
705 0, /* special_function */
706 "SECREL16", /* name */
707 true, /* partial_inplace */
708 0xffff, /* src_mask */
709 0xffff, /* dst_mask */
710 true), /* pcrel_offset */
712 /* IMAGE_REL_PPC_REFHI 0x0010 */
714 HOWTO (IMAGE_REL_PPC_REFHI
, /* type */
716 1, /* size (0 = byte, 1 = short, 2 = long) */
718 false, /* pc_relative */
720 complain_overflow_signed
, /* complain_on_overflow */
721 ppc_refhi_reloc
, /* special_function */
723 true, /* partial_inplace */
724 0xffffffff, /* src_mask */
725 0xffffffff, /* dst_mask */
726 false), /* pcrel_offset */
728 /* IMAGE_REL_PPC_REFLO 0x0011 */
730 HOWTO (IMAGE_REL_PPC_REFLO
, /* type */
732 1, /* size (0 = byte, 1 = short, 2 = long) */
734 false, /* pc_relative */
736 complain_overflow_signed
, /* complain_on_overflow */
737 ppc_refhi_reloc
, /* special_function */
739 true, /* partial_inplace */
740 0xffffffff, /* src_mask */
741 0xffffffff, /* dst_mask */
742 false), /* pcrel_offset */
744 /* IMAGE_REL_PPC_PAIR 0x0012 */
746 HOWTO (IMAGE_REL_PPC_PAIR
, /* type */
748 1, /* size (0 = byte, 1 = short, 2 = long) */
750 false, /* pc_relative */
752 complain_overflow_signed
, /* complain_on_overflow */
753 ppc_pair_reloc
, /* special_function */
755 true, /* partial_inplace */
756 0xffffffff, /* src_mask */
757 0xffffffff, /* dst_mask */
758 false), /* pcrel_offset */
760 /* IMAGE_REL_PPC_TOCREL16_DEFN 0x0013 */
761 /* 16-bit offset from TOC base, without causing a definition */
763 HOWTO ( (IMAGE_REL_PPC_TOCREL16
| IMAGE_REL_PPC_TOCDEFN
), /* type */
765 1, /* size (0 = byte, 1 = short, 2 = long) */
767 false, /* pc_relative */
769 complain_overflow_dont
, /* complain_on_overflow */
770 0, /* special_function */
771 "TOCREL16, TOCDEFN", /* name */
772 false, /* partial_inplace */
773 0xffff, /* src_mask */
774 0xffff, /* dst_mask */
775 false), /* pcrel_offset */
782 /* Some really cheezy macros that can be turned on to test stderr :-) */
791 fprintf(stderr,_("Unimplemented Relocation -- %s\n"),x); \
795 #define DUMP_RELOC(n,r) \
797 fprintf(stderr,"%s sym %d, addr %d, addend %d\n", \
798 n, (*(r->sym_ptr_ptr))->name, \
799 r->address, r->addend); \
802 /* Given a reloc name, n, and a pointer to an internal_reloc,
803 dump out interesting information on the contents
805 #define n_name _n._n_name
806 #define n_zeroes _n._n_n._n_zeroes
807 #define n_offset _n._n_n._n_offset
811 #define DUMP_RELOC2(n,r) \
813 fprintf(stderr,"%s sym %d, r_vaddr %d %s\n", \
814 n, r->r_symndx, r->r_vaddr,\
815 (((r->r_type) & IMAGE_REL_PPC_TOCDEFN) == 0) \
821 #define DUMP_RELOC(n,r)
822 #define DUMP_RELOC2(n,r)
827 /* toc construction and management routines */
829 /* This file is compiled twice, and these variables are defined in one
830 of the compilations. FIXME: This is confusing and weird. Also,
831 BFD should not use global variables. */
832 extern bfd
* bfd_of_toc_owner
;
833 extern long int global_toc_size
;
835 extern long int import_table_size
;
836 extern long int first_thunk_address
;
837 extern long int thunk_size
;
855 struct list_ele
*next
;
857 enum ref_category cat
;
862 extern struct list_ele
*head
;
863 extern struct list_ele
*tail
;
865 static void record_toc
866 PARAMS ((asection
*, int, enum ref_category
, const char *));
869 record_toc (toc_section
, our_toc_offset
, cat
, name
)
870 asection
*toc_section
;
872 enum ref_category cat
;
875 /* add this entry to our toc addr-offset-name list */
877 t
= (struct list_ele
*) bfd_malloc (sizeof (struct list_ele
));
881 t
->offset
= our_toc_offset
;
884 t
->addr
= toc_section
->output_offset
+ our_toc_offset
;
898 #ifdef COFF_IMAGE_WITH_PE
900 static boolean ppc_record_toc_entry
901 PARAMS ((bfd
*, struct bfd_link_info
*, asection
*, int, enum toc_type
));
902 static void ppc_mark_symbol_as_glue
903 PARAMS ((bfd
*, int, struct internal_reloc
*));
905 /* record a toc offset against a symbol */
907 ppc_record_toc_entry(abfd
, info
, sec
, sym
, toc_kind
)
909 struct bfd_link_info
*info ATTRIBUTE_UNUSED
;
910 asection
*sec ATTRIBUTE_UNUSED
;
912 enum toc_type toc_kind ATTRIBUTE_UNUSED
;
914 struct ppc_coff_link_hash_entry
*h
;
921 h
= (struct ppc_coff_link_hash_entry
*) (obj_coff_sym_hashes (abfd
)[sym
]);
929 local_syms
= obj_coff_local_toc_table(abfd
);
933 /* allocate a table */
935 (int *) bfd_zalloc (abfd
,
936 obj_raw_syment_count(abfd
) * sizeof(int));
939 obj_coff_local_toc_table(abfd
) = local_syms
;
940 for (i
= 0; i
< obj_raw_syment_count(abfd
); ++i
)
942 SET_UNALLOCATED(local_syms
[i
]);
946 if (IS_UNALLOCATED(local_syms
[sym
]))
948 local_syms
[sym
] = global_toc_size
;
949 global_toc_size
+= 4;
951 /* The size must fit in a 16bit displacment */
952 if (global_toc_size
> 65535)
954 (*_bfd_error_handler
) (_("TOC overflow"));
955 bfd_set_error (bfd_error_file_too_big
);
962 name
= h
->root
.root
.root
.string
;
964 /* check to see if there's a toc slot allocated. If not, do it
965 here. It will be used in relocate_section */
966 if (IS_UNALLOCATED(h
->toc_offset
))
968 h
->toc_offset
= global_toc_size
;
969 global_toc_size
+= 4;
971 /* The size must fit in a 16bit displacment */
972 if (global_toc_size
>= 65535)
974 (*_bfd_error_handler
) (_("TOC overflow"));
975 bfd_set_error (bfd_error_file_too_big
);
984 /* record a toc offset against a symbol */
986 ppc_mark_symbol_as_glue(abfd
, sym
, rel
)
989 struct internal_reloc
*rel
;
991 struct ppc_coff_link_hash_entry
*h
;
993 h
= (struct ppc_coff_link_hash_entry
*) (obj_coff_sym_hashes (abfd
)[sym
]);
997 h
->symbol_is_glue
= 1;
998 h
->glue_insn
= bfd_get_32 (abfd
, (bfd_byte
*) &rel
->r_vaddr
);
1003 #endif /* COFF_IMAGE_WITH_PE */
1006 /* Return true if this relocation should
1007 appear in the output .reloc section. */
1009 static boolean
in_reloc_p(abfd
, howto
)
1010 bfd
* abfd ATTRIBUTE_UNUSED
;
1011 reloc_howto_type
*howto
;
1014 (! howto
->pc_relative
)
1015 && (howto
->type
!= IMAGE_REL_PPC_ADDR32NB
)
1016 && (howto
->type
!= IMAGE_REL_PPC_TOCREL16
)
1017 && (howto
->type
!= IMAGE_REL_PPC_IMGLUE
)
1018 && (howto
->type
!= IMAGE_REL_PPC_IFGLUE
)
1019 && (howto
->type
!= IMAGE_REL_PPC_SECREL
)
1020 && (howto
->type
!= IMAGE_REL_PPC_SECTION
)
1021 && (howto
->type
!= IMAGE_REL_PPC_SECREL16
)
1022 && (howto
->type
!= IMAGE_REL_PPC_REFHI
)
1023 && (howto
->type
!= IMAGE_REL_PPC_REFLO
)
1024 && (howto
->type
!= IMAGE_REL_PPC_PAIR
)
1025 && (howto
->type
!= IMAGE_REL_PPC_TOCREL16_DEFN
) ;
1030 /* this function is in charge of performing all the ppc PE relocations */
1031 /* Don't yet know if we want to do this this particular way ... (krk) */
1032 /* FIXME: (it is not yet enabled) */
1034 static bfd_reloc_status_type
1035 pe_ppc_reloc (abfd
, reloc_entry
, symbol_in
, data
, input_section
, output_bfd
,
1038 arelent
*reloc_entry
;
1041 asection
*input_section
;
1043 char **error_message
;
1045 /* the consth relocation comes in two parts, we have to remember
1046 the state between calls, in these variables */
1047 static boolean part1_consth_active
= false;
1048 static unsigned long part1_consth_value
;
1050 unsigned long sym_value
;
1051 unsigned short r_type
;
1052 unsigned long addr
= reloc_entry
->address
; /*+ input_section->vma*/
1054 r_type
= reloc_entry
->howto
->type
;
1058 /* Partial linking - do nothing */
1059 reloc_entry
->address
+= input_section
->output_offset
;
1060 return bfd_reloc_ok
;
1063 if (symbol_in
!= NULL
1064 && bfd_is_und_section (symbol_in
->section
))
1066 /* Keep the state machine happy in case we're called again */
1067 if (r_type
== IMAGE_REL_PPC_REFHI
)
1069 part1_consth_active
= true;
1070 part1_consth_value
= 0;
1072 return(bfd_reloc_undefined
);
1075 if ((part1_consth_active
) && (r_type
!= IMAGE_REL_PPC_PAIR
))
1077 part1_consth_active
= false;
1078 *error_message
= (char *) _("Missing PAIR");
1079 return(bfd_reloc_dangerous
);
1083 sym_value
= get_symbol_value(symbol_in
);
1085 return(bfd_reloc_ok
);
1090 /* The reloc processing routine for the optimized COFF linker. */
1093 coff_ppc_relocate_section (output_bfd
, info
, input_bfd
, input_section
,
1094 contents
, relocs
, syms
, sections
)
1096 struct bfd_link_info
*info
;
1098 asection
*input_section
;
1100 struct internal_reloc
*relocs
;
1101 struct internal_syment
*syms
;
1102 asection
**sections
;
1104 struct internal_reloc
*rel
;
1105 struct internal_reloc
*relend
;
1108 asection
*toc_section
= 0;
1110 reloc_howto_type
*howto
= 0;
1112 /* If we are performing a relocateable link, we don't need to do a
1113 thing. The caller will take care of adjusting the reloc
1114 addresses and symbol indices. */
1115 if (info
->relocateable
)
1122 relend
= rel
+ input_section
->reloc_count
;
1123 for (; rel
< relend
; rel
++)
1126 struct ppc_coff_link_hash_entry
*h
;
1127 struct internal_syment
*sym
;
1131 bfd_reloc_status_type rstat
;
1134 unsigned short r_type
= EXTRACT_TYPE (rel
->r_type
);
1135 unsigned short r_flags
= EXTRACT_FLAGS(rel
->r_type
);
1137 symndx
= rel
->r_symndx
;
1138 loc
= contents
+ rel
->r_vaddr
- input_section
->vma
;
1140 /* FIXME: check bounds on r_type */
1141 howto
= ppc_coff_howto_table
+ r_type
;
1150 h
= (struct ppc_coff_link_hash_entry
*)
1151 (obj_coff_sym_hashes (input_bfd
)[symndx
]);
1157 sym
= syms
+ symndx
;
1160 if (r_type
== IMAGE_REL_PPC_IMGLUE
&& h
== 0)
1162 /* An IMGLUE reloc must have a name. Something is very wrong. */
1169 /* FIXME: PAIR unsupported in the following code */
1173 sec
= bfd_abs_section_ptr
;
1176 sec
= sections
[symndx
];
1177 val
= (sec
->output_section
->vma
1178 + sec
->output_offset
1180 if (! obj_pe (output_bfd
))
1188 if (h
->root
.root
.type
== bfd_link_hash_defined
1189 || h
->root
.root
.type
== bfd_link_hash_defweak
)
1191 sec
= h
->root
.root
.u
.def
.section
;
1192 val
= (h
->root
.root
.u
.def
.value
1193 + sec
->output_section
->vma
1194 + sec
->output_offset
);
1198 if (! ((*info
->callbacks
->undefined_symbol
)
1199 (info
, h
->root
.root
.root
.string
, input_bfd
, input_section
,
1200 rel
->r_vaddr
- input_section
->vma
)))
1205 rstat
= bfd_reloc_ok
;
1207 /* Each case must do its own relocation, setting rstat appropriately */
1211 (*_bfd_error_handler
)
1212 (_("%s: unsupported relocation type 0x%02x"),
1213 bfd_get_filename (input_bfd
), r_type
);
1214 bfd_set_error (bfd_error_bad_value
);
1216 case IMAGE_REL_PPC_TOCREL16
:
1218 bfd_vma our_toc_offset
;
1221 DUMP_RELOC2(howto
->name
, rel
);
1223 if (toc_section
== 0)
1225 toc_section
= bfd_get_section_by_name (bfd_of_toc_owner
,
1228 if ( toc_section
== NULL
)
1230 /* There is no toc section. Something is very wrong. */
1236 * Amazing bit tricks present. As we may have seen earlier, we
1237 * use the 1 bit to tell us whether or not a toc offset has been
1238 * allocated. Now that they've all been allocated, we will use
1239 * the 1 bit to tell us if we've written this particular toc
1244 { /* it is a file local symbol */
1245 int *local_toc_table
;
1248 sym
= syms
+ symndx
;
1249 name
= sym
->_n
._n_name
;
1251 local_toc_table
= obj_coff_local_toc_table(input_bfd
);
1252 our_toc_offset
= local_toc_table
[symndx
];
1254 if (IS_WRITTEN(our_toc_offset
))
1256 /* if it has been written out, it is marked with the
1257 1 bit. Fix up our offset, but do not write it out
1260 MAKE_ADDR_AGAIN(our_toc_offset
);
1264 /* write out the toc entry */
1265 record_toc(toc_section
,
1270 bfd_put_32(output_bfd
,
1272 toc_section
->contents
+ our_toc_offset
);
1274 MARK_AS_WRITTEN(local_toc_table
[symndx
]);
1280 const char *name
= h
->root
.root
.root
.string
;
1281 our_toc_offset
= h
->toc_offset
;
1283 if ((r_flags
& IMAGE_REL_PPC_TOCDEFN
)
1284 == IMAGE_REL_PPC_TOCDEFN
)
1286 /* This is unbelievable cheese. Some knowledgable asm
1287 hacker has decided to use r2 as a base for loading
1288 a value. He/She does this by setting the tocdefn bit,
1289 and not supplying a toc definition. The behaviour is
1290 then to use the difference between the value of the
1291 symbol and the actual location of the toc as the toc
1294 In fact, what is usually happening is, because the
1295 Import Address Table is mapped immediately following
1296 the toc, some trippy library code trying for speed on
1297 dll linkage, takes advantage of that and considers
1298 the IAT to be part of the toc, thus saving a load.
1301 our_toc_offset
= val
-
1302 (toc_section
->output_section
->vma
+
1303 toc_section
->output_offset
);
1305 /* The size must still fit in a 16bit displacment */
1306 if (our_toc_offset
>= 65535)
1308 (*_bfd_error_handler
)
1309 (_("%s: Relocation for %s of %x exceeds Toc size limit"),
1310 bfd_get_filename (input_bfd
), name
, our_toc_offset
);
1311 bfd_set_error (bfd_error_bad_value
);
1315 record_toc(toc_section
, our_toc_offset
, pub
, strdup(name
));
1317 else if (IS_WRITTEN(our_toc_offset
))
1319 /* if it has been written out, it is marked with the
1320 1 bit. Fix up our offset, but do not write it out
1323 MAKE_ADDR_AGAIN(our_toc_offset
);
1327 record_toc(toc_section
, our_toc_offset
, pub
, strdup(name
));
1329 /* write out the toc entry */
1330 bfd_put_32(output_bfd
,
1332 toc_section
->contents
+ our_toc_offset
);
1334 MARK_AS_WRITTEN(h
->toc_offset
);
1335 /* The tricky part is that this is the address that */
1336 /* needs a .reloc entry for it */
1341 if (fixit
&& info
->base_file
)
1343 /* So if this is non pcrelative, and is referenced
1344 to a section or a common symbol, then it needs a reloc */
1346 /* relocation to a symbol in a section which
1347 isn't absolute - we output the address here
1350 bfd_vma addr
= toc_section
->output_section
->vma
1351 + toc_section
->output_offset
+ our_toc_offset
;
1353 if (coff_data(output_bfd
)->pe
)
1354 addr
-= pe_data(output_bfd
)->pe_opthdr
.ImageBase
;
1356 fwrite (&addr
, 1,4, (FILE *) info
->base_file
);
1360 /* FIXME: this test is conservative */
1361 if ( (r_flags
& IMAGE_REL_PPC_TOCDEFN
) != IMAGE_REL_PPC_TOCDEFN
&&
1362 our_toc_offset
> toc_section
->_raw_size
)
1364 (*_bfd_error_handler
)
1365 (_("%s: Relocation exceeds allocated TOC (%x)"),
1366 bfd_get_filename (input_bfd
),
1367 toc_section
->_raw_size
);
1368 bfd_set_error (bfd_error_bad_value
);
1372 /* Now we know the relocation for this toc reference */
1373 relocation
= our_toc_offset
+ TOC_LOAD_ADJUSTMENT
;
1374 rstat
= _bfd_relocate_contents (howto
,
1380 case IMAGE_REL_PPC_IFGLUE
:
1382 /* To solve this, we need to know whether or not the symbol */
1383 /* appearing on the call instruction is a glue function or not. */
1384 /* A glue function must announce itself via a IMGLUE reloc, and */
1385 /* the reloc contains the required toc restore instruction */
1388 const char *my_name
;
1389 DUMP_RELOC2(howto
->name
, rel
);
1393 my_name
= h
->root
.root
.root
.string
;
1394 if (h
->symbol_is_glue
== 1)
1396 x
= bfd_get_32(input_bfd
, loc
);
1397 bfd_put_32(input_bfd
, h
->glue_insn
, loc
);
1402 case IMAGE_REL_PPC_SECREL
:
1403 /* Unimplemented: codeview debugging information */
1404 /* For fast access to the header of the section
1405 containing the item. */
1407 case IMAGE_REL_PPC_SECTION
:
1408 /* Unimplemented: codeview debugging information */
1409 /* Is used to indicate that the value should be relative
1410 to the beginning of the section that contains the
1413 case IMAGE_REL_PPC_ABSOLUTE
:
1415 const char *my_name
;
1417 my_name
= (syms
+symndx
)->_n
._n_name
;
1420 my_name
= h
->root
.root
.root
.string
;
1424 _("Warning: unsupported reloc %s <file %s, section %s>\n"),
1426 bfd_get_filename(input_bfd
),
1427 input_section
->name
);
1429 fprintf(stderr
,"sym %ld (%s), r_vaddr %ld (%lx)\n",
1430 rel
->r_symndx
, my_name
, (long) rel
->r_vaddr
,
1431 (unsigned long) rel
->r_vaddr
);
1434 case IMAGE_REL_PPC_IMGLUE
:
1436 /* There is nothing to do now. This reloc was noted in the first
1437 pass over the relocs, and the glue instruction extracted */
1438 const char *my_name
;
1439 if (h
->symbol_is_glue
== 1)
1441 my_name
= h
->root
.root
.root
.string
;
1443 (*_bfd_error_handler
)
1444 (_("%s: Out of order IMGLUE reloc for %s"),
1445 bfd_get_filename (input_bfd
), my_name
);
1446 bfd_set_error (bfd_error_bad_value
);
1450 case IMAGE_REL_PPC_ADDR32NB
:
1452 struct coff_link_hash_entry
*myh
= 0;
1453 const char *name
= 0;
1454 DUMP_RELOC2(howto
->name
, rel
);
1456 if (strncmp(".idata$2",input_section
->name
,8) == 0 && first_thunk_address
== 0)
1458 /* set magic values */
1460 struct coff_link_hash_entry
*myh
= 0;
1461 myh
= coff_link_hash_lookup (coff_hash_table (info
),
1463 false, false, true);
1464 first_thunk_address
= myh
->root
.u
.def
.value
+
1465 sec
->output_section
->vma
+
1466 sec
->output_offset
-
1467 pe_data(output_bfd
)->pe_opthdr
.ImageBase
;
1469 idata5offset
= myh
->root
.u
.def
.value
;
1470 myh
= coff_link_hash_lookup (coff_hash_table (info
),
1472 false, false, true);
1474 thunk_size
= myh
->root
.u
.def
.value
- idata5offset
;
1475 myh
= coff_link_hash_lookup (coff_hash_table (info
),
1477 false, false, true);
1478 import_table_size
= myh
->root
.u
.def
.value
;
1482 { /* it is a file local symbol */
1483 sym
= syms
+ symndx
;
1484 name
= sym
->_n
._n_name
;
1490 name
= h
->root
.root
.root
.string
;
1491 if (strcmp(".idata$2", name
) == 0)
1492 target
= "__idata2_magic__";
1493 else if (strcmp(".idata$4", name
) == 0)
1494 target
= "__idata4_magic__";
1495 else if (strcmp(".idata$5", name
) == 0)
1496 target
= "__idata5_magic__";
1502 myh
= coff_link_hash_lookup (coff_hash_table (info
),
1504 false, false, true);
1507 /* Missing magic cookies. Something is very wrong. */
1511 val
= myh
->root
.u
.def
.value
+
1512 sec
->output_section
->vma
+ sec
->output_offset
;
1513 if (first_thunk_address
== 0)
1516 myh
= coff_link_hash_lookup (coff_hash_table (info
),
1518 false, false, true);
1519 first_thunk_address
= myh
->root
.u
.def
.value
+
1520 sec
->output_section
->vma
+
1521 sec
->output_offset
-
1522 pe_data(output_bfd
)->pe_opthdr
.ImageBase
;
1524 idata5offset
= myh
->root
.u
.def
.value
;
1525 myh
= coff_link_hash_lookup (coff_hash_table (info
),
1527 false, false, true);
1529 thunk_size
= myh
->root
.u
.def
.value
- idata5offset
;
1530 myh
= coff_link_hash_lookup (coff_hash_table (info
),
1532 false, false, true);
1533 import_table_size
= myh
->root
.u
.def
.value
;
1538 rstat
= _bfd_relocate_contents (howto
,
1541 pe_data(output_bfd
)->pe_opthdr
.ImageBase
,
1546 case IMAGE_REL_PPC_REL24
:
1547 DUMP_RELOC2(howto
->name
, rel
);
1548 val
-= (input_section
->output_section
->vma
1549 + input_section
->output_offset
);
1551 rstat
= _bfd_relocate_contents (howto
,
1556 case IMAGE_REL_PPC_ADDR16
:
1557 case IMAGE_REL_PPC_ADDR24
:
1558 case IMAGE_REL_PPC_ADDR32
:
1559 DUMP_RELOC2(howto
->name
, rel
);
1560 rstat
= _bfd_relocate_contents (howto
,
1567 if ( info
->base_file
)
1569 /* So if this is non pcrelative, and is referenced
1570 to a section or a common symbol, then it needs a reloc */
1571 if (sym
&& pe_data(output_bfd
)->in_reloc_p(output_bfd
, howto
))
1573 /* relocation to a symbol in a section which
1574 isn't absolute - we output the address here
1576 bfd_vma addr
= rel
->r_vaddr
1577 - input_section
->vma
1578 + input_section
->output_offset
1579 + input_section
->output_section
->vma
;
1581 if (coff_data(output_bfd
)->pe
)
1583 addr
-= pe_data(output_bfd
)->pe_opthdr
.ImageBase
;
1585 fwrite (&addr
, 1,4, (FILE *) info
->base_file
);
1595 case bfd_reloc_overflow
:
1598 char buf
[SYMNMLEN
+ 1];
1603 name
= h
->root
.root
.root
.string
;
1604 else if (sym
== NULL
)
1606 else if (sym
->_n
._n_n
._n_zeroes
== 0
1607 && sym
->_n
._n_n
._n_offset
!= 0)
1608 name
= obj_coff_strings (input_bfd
) + sym
->_n
._n_n
._n_offset
;
1611 strncpy (buf
, sym
->_n
._n_name
, SYMNMLEN
);
1612 buf
[SYMNMLEN
] = '\0';
1616 if (! ((*info
->callbacks
->reloc_overflow
)
1617 (info
, name
, howto
->name
,
1618 (bfd_vma
) 0, input_bfd
,
1619 input_section
, rel
->r_vaddr
- input_section
->vma
)))
1631 #ifdef COFF_IMAGE_WITH_PE
1633 /* FIXME: BFD should not use global variables. This file is compiled
1634 twice, and these variables are shared. This is confusing and
1637 long int global_toc_size
= 4;
1639 bfd
* bfd_of_toc_owner
= 0;
1641 long int import_table_size
;
1642 long int first_thunk_address
;
1643 long int thunk_size
;
1645 struct list_ele
*head
;
1646 struct list_ele
*tail
;
1649 h1
= N_("\n\t\t\tTOC MAPPING\n\n");
1651 h2
= N_(" TOC disassembly Comments Name\n");
1653 h3
= N_(" Offset spelling (if present)\n");
1659 FILE *file
= (FILE *) vfile
;
1662 fprintf(file
, _(h1
));
1663 fprintf(file
, _(h2
));
1664 fprintf(file
, _(h3
));
1666 for(t
= head
; t
!= 0; t
=t
->next
)
1668 const char *cat
= "";
1671 cat
= _("private ");
1672 else if (t
->cat
== pub
)
1674 else if (t
->cat
== data
)
1675 cat
= _("data-in-toc ");
1677 if (t
->offset
> global_toc_size
)
1679 if (t
->offset
<= global_toc_size
+ thunk_size
)
1680 cat
= _("IAT reference ");
1684 _("**** global_toc_size %ld(%lx), thunk_size %ld(%lx)\n"),
1685 global_toc_size
, global_toc_size
, thunk_size
, thunk_size
);
1686 cat
= _("Out of bounds!");
1691 " %04lx (%d)", (unsigned long) t
->offset
, t
->offset
- 32768);
1698 fprintf(file
, "\n");
1702 ppc_allocate_toc_section (info
)
1703 struct bfd_link_info
*info ATTRIBUTE_UNUSED
;
1707 static char test_char
= '1';
1709 if ( global_toc_size
== 0 ) /* FIXME: does this get me in trouble? */
1712 if (bfd_of_toc_owner
== 0)
1714 /* No toc owner? Something is very wrong. */
1718 s
= bfd_get_section_by_name ( bfd_of_toc_owner
, TOC_SECTION_NAME
);
1721 /* No toc section? Something is very wrong. */
1725 foo
= (bfd_byte
*) bfd_alloc(bfd_of_toc_owner
, global_toc_size
);
1726 memset(foo
, test_char
, global_toc_size
);
1728 s
->_raw_size
= s
->_cooked_size
= global_toc_size
;
1735 ppc_process_before_allocation (abfd
, info
)
1737 struct bfd_link_info
*info
;
1740 struct internal_reloc
*i
, *rel
;
1742 /* here we have a bfd that is to be included on the link. We have a hook
1743 to do reloc rummaging, before section sizes are nailed down. */
1745 _bfd_coff_get_external_symbols(abfd
);
1747 /* rummage around all the relocs and map the toc */
1748 sec
= abfd
->sections
;
1755 for (; sec
!= 0; sec
= sec
->next
)
1757 if (sec
->reloc_count
== 0)
1760 /* load the relocs */
1761 /* FIXME: there may be a storage leak here */
1762 i
=_bfd_coff_read_internal_relocs(abfd
,sec
,1,0,0,0);
1767 for (rel
=i
;rel
<i
+sec
->reloc_count
;++rel
)
1769 unsigned short r_type
= EXTRACT_TYPE (rel
->r_type
);
1770 unsigned short r_flags
= EXTRACT_FLAGS(rel
->r_type
);
1773 DUMP_RELOC2(ppc_coff_howto_table
[r_type
].name
, rel
);
1777 case IMAGE_REL_PPC_TOCREL16
:
1778 /* if TOCDEFN is on, ignore as someone else has allocated the
1780 if ( (r_flags
& IMAGE_REL_PPC_TOCDEFN
) != IMAGE_REL_PPC_TOCDEFN
)
1781 ok
= ppc_record_toc_entry(abfd
, info
, sec
,
1782 rel
->r_symndx
, default_toc
);
1786 case IMAGE_REL_PPC_IMGLUE
:
1787 ppc_mark_symbol_as_glue(abfd
, rel
->r_symndx
, rel
);
1801 static bfd_reloc_status_type
1802 ppc_refhi_reloc (abfd
,
1809 bfd
*abfd ATTRIBUTE_UNUSED
;
1810 arelent
*reloc_entry ATTRIBUTE_UNUSED
;
1811 asymbol
*symbol ATTRIBUTE_UNUSED
;
1812 PTR data ATTRIBUTE_UNUSED
;
1813 asection
*input_section ATTRIBUTE_UNUSED
;
1815 char **error_message ATTRIBUTE_UNUSED
;
1818 DUMP_RELOC("REFHI",reloc_entry
);
1820 if (output_bfd
== (bfd
*) NULL
)
1821 return bfd_reloc_continue
;
1823 return bfd_reloc_undefined
;
1828 static bfd_reloc_status_type
1829 ppc_reflo_reloc (abfd
,
1837 arelent
*reloc_entry
;
1840 asection
*input_section
;
1842 char **error_message
;
1845 DUMP_RELOC("REFLO",reloc_entry
);
1847 if (output_bfd
== (bfd
*) NULL
)
1848 return bfd_reloc_continue
;
1850 return bfd_reloc_undefined
;
1855 static bfd_reloc_status_type
1856 ppc_pair_reloc (abfd
,
1863 bfd
*abfd ATTRIBUTE_UNUSED
;
1864 arelent
*reloc_entry ATTRIBUTE_UNUSED
;
1865 asymbol
*symbol ATTRIBUTE_UNUSED
;
1866 PTR data ATTRIBUTE_UNUSED
;
1867 asection
*input_section ATTRIBUTE_UNUSED
;
1869 char **error_message ATTRIBUTE_UNUSED
;
1872 DUMP_RELOC("PAIR",reloc_entry
);
1874 if (output_bfd
== (bfd
*) NULL
)
1875 return bfd_reloc_continue
;
1877 return bfd_reloc_undefined
;
1881 static bfd_reloc_status_type
1882 ppc_toc16_reloc (abfd
,
1889 bfd
*abfd ATTRIBUTE_UNUSED
;
1890 arelent
*reloc_entry ATTRIBUTE_UNUSED
;
1891 asymbol
*symbol ATTRIBUTE_UNUSED
;
1892 PTR data ATTRIBUTE_UNUSED
;
1893 asection
*input_section ATTRIBUTE_UNUSED
;
1895 char **error_message ATTRIBUTE_UNUSED
;
1897 UN_IMPL("TOCREL16");
1898 DUMP_RELOC("TOCREL16",reloc_entry
);
1900 if (output_bfd
== (bfd
*) NULL
)
1902 return bfd_reloc_continue
;
1905 return bfd_reloc_ok
;
1910 /* ADDR32NB : 32 bit address relative to the virtual origin. */
1911 /* (On the alpha, this is always a linker generated thunk)*/
1912 /* (i.e. 32bit addr relative to the image base) */
1916 static bfd_reloc_status_type
1917 ppc_addr32nb_reloc (abfd
,
1925 arelent
*reloc_entry
;
1928 asection
*input_section
;
1930 char **error_message
;
1932 UN_IMPL("ADDR32NB");
1933 DUMP_RELOC("ADDR32NB",reloc_entry
);
1935 return bfd_reloc_ok
;
1940 static bfd_reloc_status_type
1941 ppc_secrel_reloc (abfd
,
1948 bfd
*abfd ATTRIBUTE_UNUSED
;
1949 arelent
*reloc_entry ATTRIBUTE_UNUSED
;
1950 asymbol
*symbol ATTRIBUTE_UNUSED
;
1951 PTR data ATTRIBUTE_UNUSED
;
1952 asection
*input_section ATTRIBUTE_UNUSED
;
1954 char **error_message ATTRIBUTE_UNUSED
;
1957 DUMP_RELOC("SECREL",reloc_entry
);
1959 if (output_bfd
== (bfd
*) NULL
)
1960 return bfd_reloc_continue
;
1962 return bfd_reloc_ok
;
1965 static bfd_reloc_status_type
1966 ppc_section_reloc (abfd
,
1973 bfd
*abfd ATTRIBUTE_UNUSED
;
1974 arelent
*reloc_entry ATTRIBUTE_UNUSED
;
1975 asymbol
*symbol ATTRIBUTE_UNUSED
;
1976 PTR data ATTRIBUTE_UNUSED
;
1977 asection
*input_section ATTRIBUTE_UNUSED
;
1979 char **error_message ATTRIBUTE_UNUSED
;
1982 DUMP_RELOC("SECTION",reloc_entry
);
1984 if (output_bfd
== (bfd
*) NULL
)
1985 return bfd_reloc_continue
;
1987 return bfd_reloc_ok
;
1990 static bfd_reloc_status_type
1991 ppc_imglue_reloc (abfd
,
1998 bfd
*abfd ATTRIBUTE_UNUSED
;
1999 arelent
*reloc_entry ATTRIBUTE_UNUSED
;
2000 asymbol
*symbol ATTRIBUTE_UNUSED
;
2001 PTR data ATTRIBUTE_UNUSED
;
2002 asection
*input_section ATTRIBUTE_UNUSED
;
2004 char **error_message ATTRIBUTE_UNUSED
;
2007 DUMP_RELOC("IMGLUE",reloc_entry
);
2009 if (output_bfd
== (bfd
*) NULL
)
2010 return bfd_reloc_continue
;
2012 return bfd_reloc_ok
;
2017 #define MAX_RELOC_INDEX \
2018 (sizeof(ppc_coff_howto_table) / sizeof(ppc_coff_howto_table[0]) - 1)
2021 /* FIXME: There is a possiblity that when we read in a reloc from a file,
2022 that there are some bits encoded in the upper portion of the
2023 type field. Not yet implemented.
2025 static void ppc_coff_rtype2howto
PARAMS ((arelent
*relent
,
2026 struct internal_reloc
*internal
));
2029 ppc_coff_rtype2howto (relent
, internal
)
2031 struct internal_reloc
*internal
;
2034 /* We can encode one of three things in the type field, aside from the
2036 1. IMAGE_REL_PPC_NEG - indicates the value field is a subtraction
2037 value, rather than an addition value
2038 2. IMAGE_REL_PPC_BRTAKEN, IMAGE_REL_PPC_BRNTAKEN - indicates that
2039 the branch is expected to be taken or not.
2040 3. IMAGE_REL_PPC_TOCDEFN - toc slot definition in the file
2041 For now, we just strip this stuff to find the type, and ignore it other
2044 reloc_howto_type
*howto
;
2045 unsigned short r_type
= EXTRACT_TYPE (internal
->r_type
);
2046 unsigned short r_flags
= EXTRACT_FLAGS(internal
->r_type
);
2047 unsigned short junk
= EXTRACT_JUNK (internal
->r_type
);
2049 /* the masking process only slices off the bottom byte for r_type. */
2050 if ( r_type
> MAX_RELOC_INDEX
)
2053 /* check for absolute crap */
2059 case IMAGE_REL_PPC_ADDR16
:
2060 case IMAGE_REL_PPC_REL24
:
2061 case IMAGE_REL_PPC_ADDR24
:
2062 case IMAGE_REL_PPC_ADDR32
:
2063 case IMAGE_REL_PPC_IFGLUE
:
2064 case IMAGE_REL_PPC_ADDR32NB
:
2065 case IMAGE_REL_PPC_SECTION
:
2066 case IMAGE_REL_PPC_SECREL
:
2067 DUMP_RELOC2(ppc_coff_howto_table
[r_type
].name
, internal
);
2068 howto
= ppc_coff_howto_table
+ r_type
;
2070 case IMAGE_REL_PPC_IMGLUE
:
2071 DUMP_RELOC2(ppc_coff_howto_table
[r_type
].name
, internal
);
2072 howto
= ppc_coff_howto_table
+ r_type
;
2074 case IMAGE_REL_PPC_TOCREL16
:
2075 DUMP_RELOC2(ppc_coff_howto_table
[r_type
].name
, internal
);
2076 if (r_flags
& IMAGE_REL_PPC_TOCDEFN
)
2077 howto
= ppc_coff_howto_table
+ IMAGE_REL_PPC_TOCREL16_DEFN
;
2079 howto
= ppc_coff_howto_table
+ IMAGE_REL_PPC_TOCREL16
;
2083 _("Warning: Unsupported reloc %s [%d] used -- it may not work.\n"),
2084 ppc_coff_howto_table
[r_type
].name
,
2086 howto
= ppc_coff_howto_table
+ r_type
;
2090 relent
->howto
= howto
;
2094 static reloc_howto_type
*
2095 coff_ppc_rtype_to_howto (abfd
, sec
, rel
, h
, sym
, addendp
)
2096 bfd
*abfd ATTRIBUTE_UNUSED
;
2098 struct internal_reloc
*rel
;
2099 struct coff_link_hash_entry
*h ATTRIBUTE_UNUSED
;
2100 struct internal_syment
*sym ATTRIBUTE_UNUSED
;
2103 reloc_howto_type
*howto
;
2105 /* We can encode one of three things in the type field, aside from the
2107 1. IMAGE_REL_PPC_NEG - indicates the value field is a subtraction
2108 value, rather than an addition value
2109 2. IMAGE_REL_PPC_BRTAKEN, IMAGE_REL_PPC_BRNTAKEN - indicates that
2110 the branch is expected to be taken or not.
2111 3. IMAGE_REL_PPC_TOCDEFN - toc slot definition in the file
2112 For now, we just strip this stuff to find the type, and ignore it other
2116 unsigned short r_type
= EXTRACT_TYPE (rel
->r_type
);
2117 unsigned short r_flags
= EXTRACT_FLAGS(rel
->r_type
);
2118 unsigned short junk
= EXTRACT_JUNK (rel
->r_type
);
2120 /* the masking process only slices off the bottom byte for r_type. */
2121 if ( r_type
> MAX_RELOC_INDEX
)
2124 /* check for absolute crap */
2130 case IMAGE_REL_PPC_ADDR32NB
:
2131 DUMP_RELOC2(ppc_coff_howto_table
[r_type
].name
, rel
);
2132 *addendp
-= pe_data(sec
->output_section
->owner
)->pe_opthdr
.ImageBase
;
2133 howto
= ppc_coff_howto_table
+ r_type
;
2135 case IMAGE_REL_PPC_TOCREL16
:
2136 DUMP_RELOC2(ppc_coff_howto_table
[r_type
].name
, rel
);
2137 if (r_flags
& IMAGE_REL_PPC_TOCDEFN
)
2138 howto
= ppc_coff_howto_table
+ IMAGE_REL_PPC_TOCREL16_DEFN
;
2140 howto
= ppc_coff_howto_table
+ IMAGE_REL_PPC_TOCREL16
;
2142 case IMAGE_REL_PPC_ADDR16
:
2143 case IMAGE_REL_PPC_REL24
:
2144 case IMAGE_REL_PPC_ADDR24
:
2145 case IMAGE_REL_PPC_ADDR32
:
2146 case IMAGE_REL_PPC_IFGLUE
:
2147 case IMAGE_REL_PPC_SECTION
:
2148 case IMAGE_REL_PPC_SECREL
:
2149 DUMP_RELOC2(ppc_coff_howto_table
[r_type
].name
, rel
);
2150 howto
= ppc_coff_howto_table
+ r_type
;
2152 case IMAGE_REL_PPC_IMGLUE
:
2153 DUMP_RELOC2(ppc_coff_howto_table
[r_type
].name
, rel
);
2154 howto
= ppc_coff_howto_table
+ r_type
;
2158 _("Warning: Unsupported reloc %s [%d] used -- it may not work.\n"),
2159 ppc_coff_howto_table
[r_type
].name
,
2161 howto
= ppc_coff_howto_table
+ r_type
;
2169 /* a cheesy little macro to make the code a little more readable */
2170 #define HOW2MAP(bfd_rtype,ppc_rtype) \
2171 case bfd_rtype: return &ppc_coff_howto_table[ppc_rtype]
2173 static reloc_howto_type
*ppc_coff_reloc_type_lookup
2174 PARAMS ((bfd
*, bfd_reloc_code_real_type
));
2176 static reloc_howto_type
*
2177 ppc_coff_reloc_type_lookup (abfd
, code
)
2178 bfd
*abfd ATTRIBUTE_UNUSED
;
2179 bfd_reloc_code_real_type code
;
2183 HOW2MAP(BFD_RELOC_32_GOTOFF
, IMAGE_REL_PPC_IMGLUE
);
2184 HOW2MAP(BFD_RELOC_16_GOT_PCREL
, IMAGE_REL_PPC_IFGLUE
);
2185 HOW2MAP(BFD_RELOC_16
, IMAGE_REL_PPC_ADDR16
);
2186 HOW2MAP(BFD_RELOC_PPC_B26
, IMAGE_REL_PPC_REL24
);
2187 HOW2MAP(BFD_RELOC_PPC_BA26
, IMAGE_REL_PPC_ADDR24
);
2188 HOW2MAP(BFD_RELOC_PPC_TOC16
, IMAGE_REL_PPC_TOCREL16
);
2189 HOW2MAP(BFD_RELOC_16_GOTOFF
, IMAGE_REL_PPC_TOCREL16_DEFN
);
2190 HOW2MAP(BFD_RELOC_32
, IMAGE_REL_PPC_ADDR32
);
2191 HOW2MAP(BFD_RELOC_RVA
, IMAGE_REL_PPC_ADDR32NB
);
2201 /* Tailor coffcode.h -- macro heaven. */
2203 #define RTYPE2HOWTO(cache_ptr, dst) ppc_coff_rtype2howto (cache_ptr, dst)
2205 #ifndef COFF_IMAGE_WITH_PE
2206 static void ppc_coff_swap_sym_in_hook
PARAMS ((bfd
*, PTR
, PTR
));
2209 /* We use the special COFF backend linker, with our own special touch. */
2211 #define coff_bfd_reloc_type_lookup ppc_coff_reloc_type_lookup
2212 #define coff_rtype_to_howto coff_ppc_rtype_to_howto
2213 #define coff_relocate_section coff_ppc_relocate_section
2214 #define coff_bfd_final_link ppc_bfd_coff_final_link
2216 #ifndef COFF_IMAGE_WITH_PE
2217 /* FIXME: This no longer works. */
2218 #define coff_swap_sym_in_hook ppc_coff_swap_sym_in_hook
2221 #define SELECT_RELOC(internal, howto) {internal.r_type=howto->type;}
2223 #define COFF_PAGE_SIZE 0x1000
2225 /* FIXME: This controls some code that used to be in peicode.h and is
2226 now in peigen.c. It will not control the code in peigen.c. If
2227 anybody wants to get this working, you will need to fix that. */
2228 #define POWERPC_LE_PE
2230 #define COFF_SECTION_ALIGNMENT_ENTRIES \
2231 { COFF_SECTION_NAME_EXACT_MATCH (".idata$2"), \
2232 COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 0 }, \
2233 { COFF_SECTION_NAME_EXACT_MATCH (".idata$3"), \
2234 COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 0 }, \
2235 { COFF_SECTION_NAME_EXACT_MATCH (".idata$4"), \
2236 COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 2 }, \
2237 { COFF_SECTION_NAME_EXACT_MATCH (".idata$5"), \
2238 COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 2 }, \
2239 { COFF_SECTION_NAME_EXACT_MATCH (".idata$6"), \
2240 COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 1 }, \
2241 { COFF_SECTION_NAME_EXACT_MATCH (".reloc"), \
2242 COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 1 }
2244 #include "coffcode.h"
2248 #ifndef COFF_IMAGE_WITH_PE
2250 What we're trying to do here is allocate a toc section (early), and attach
2251 it to the last bfd to be processed. This avoids the problem of having a toc
2252 written out before all files have been processed. This code allocates
2253 a toc section for every file, and records the last one seen. There are
2254 at least two problems with this approach:
2255 1. We allocate whole bunches of toc sections that are ignored, but at
2256 at least we will not allocate a toc if no .toc is present.
2257 2. It's not clear to me that being the last bfd read necessarily means
2258 that you are the last bfd closed.
2259 3. Doing it on a "swap in" hook depends on when the "swap in" is called,
2260 and how often, etc. It's not clear to me that there isn't a hole here.
2264 ppc_coff_swap_sym_in_hook (abfd
, ext1
, in1
)
2266 PTR ext1 ATTRIBUTE_UNUSED
;
2269 struct internal_syment
*in
= (struct internal_syment
*)in1
;
2271 if (bfd_of_toc_owner
!= 0) /* we already have a toc, so go home */
2274 if (strcmp(in
->_n
._n_name
, ".toc") == 0)
2277 register asection
*s
;
2279 s
= bfd_get_section_by_name ( abfd
, TOC_SECTION_NAME
);
2285 flags
= SEC_ALLOC
| SEC_LOAD
| SEC_HAS_CONTENTS
| SEC_IN_MEMORY
;
2287 s
= bfd_make_section (abfd
, TOC_SECTION_NAME
);
2290 || !bfd_set_section_flags (abfd
, s
, flags
)
2291 || !bfd_set_section_alignment (abfd
, s
, 2))
2293 /* FIXME: set appropriate bfd error */
2297 /* save the bfd for later allocation */
2298 bfd_of_toc_owner
= abfd
;
2305 #ifndef COFF_IMAGE_WITH_PE
2307 static boolean ppc_do_last
PARAMS ((bfd
*));
2308 static bfd
*ppc_get_last
PARAMS ((void));
2314 if (abfd
== bfd_of_toc_owner
)
2323 return bfd_of_toc_owner
;
2326 /* this piece of machinery exists only to guarantee that the bfd that holds
2327 the toc section is written last.
2329 This does depend on bfd_make_section attaching a new section to the
2330 end of the section list for the bfd.
2332 This is otherwise intended to be functionally the same as
2333 cofflink.c:_bfd_coff_final_link(). It is specifically different only
2334 where the POWERPC_LE_PE macro modifies the code. It is left in as a
2335 precise form of comment. krk@cygnus.com
2339 /* Do the final link step. */
2342 ppc_bfd_coff_final_link (abfd
, info
)
2344 struct bfd_link_info
*info
;
2346 bfd_size_type symesz
;
2347 struct coff_final_link_info finfo
;
2348 boolean debug_merge_allocated
;
2350 struct bfd_link_order
*p
;
2351 size_t max_sym_count
;
2352 size_t max_lineno_count
;
2353 size_t max_reloc_count
;
2354 size_t max_output_reloc_count
;
2355 size_t max_contents_size
;
2356 file_ptr rel_filepos
;
2358 file_ptr line_filepos
;
2359 unsigned int linesz
;
2361 bfd_byte
*external_relocs
= NULL
;
2362 char strbuf
[STRING_SIZE_SIZE
];
2364 symesz
= bfd_coff_symesz (abfd
);
2367 finfo
.output_bfd
= abfd
;
2368 finfo
.strtab
= NULL
;
2369 finfo
.section_info
= NULL
;
2370 finfo
.last_file_index
= -1;
2371 finfo
.last_bf_index
= -1;
2372 finfo
.internal_syms
= NULL
;
2373 finfo
.sec_ptrs
= NULL
;
2374 finfo
.sym_indices
= NULL
;
2375 finfo
.outsyms
= NULL
;
2376 finfo
.linenos
= NULL
;
2377 finfo
.contents
= NULL
;
2378 finfo
.external_relocs
= NULL
;
2379 finfo
.internal_relocs
= NULL
;
2380 debug_merge_allocated
= false;
2382 coff_data (abfd
)->link_info
= info
;
2384 finfo
.strtab
= _bfd_stringtab_init ();
2385 if (finfo
.strtab
== NULL
)
2388 if (! coff_debug_merge_hash_table_init (&finfo
.debug_merge
))
2390 debug_merge_allocated
= true;
2392 /* Compute the file positions for all the sections. */
2393 if (! abfd
->output_has_begun
)
2395 if (! bfd_coff_compute_section_file_positions (abfd
))
2399 /* Count the line numbers and relocation entries required for the
2400 output file. Set the file positions for the relocs. */
2401 rel_filepos
= obj_relocbase (abfd
);
2402 relsz
= bfd_coff_relsz (abfd
);
2403 max_contents_size
= 0;
2404 max_lineno_count
= 0;
2405 max_reloc_count
= 0;
2407 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
2410 o
->lineno_count
= 0;
2411 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
2414 if (p
->type
== bfd_indirect_link_order
)
2418 sec
= p
->u
.indirect
.section
;
2420 /* Mark all sections which are to be included in the
2421 link. This will normally be every section. We need
2422 to do this so that we can identify any sections which
2423 the linker has decided to not include. */
2424 sec
->linker_mark
= true;
2426 if (info
->strip
== strip_none
2427 || info
->strip
== strip_some
)
2428 o
->lineno_count
+= sec
->lineno_count
;
2430 if (info
->relocateable
)
2431 o
->reloc_count
+= sec
->reloc_count
;
2433 if (sec
->_raw_size
> max_contents_size
)
2434 max_contents_size
= sec
->_raw_size
;
2435 if (sec
->lineno_count
> max_lineno_count
)
2436 max_lineno_count
= sec
->lineno_count
;
2437 if (sec
->reloc_count
> max_reloc_count
)
2438 max_reloc_count
= sec
->reloc_count
;
2440 else if (info
->relocateable
2441 && (p
->type
== bfd_section_reloc_link_order
2442 || p
->type
== bfd_symbol_reloc_link_order
))
2445 if (o
->reloc_count
== 0)
2449 o
->flags
|= SEC_RELOC
;
2450 o
->rel_filepos
= rel_filepos
;
2451 rel_filepos
+= o
->reloc_count
* relsz
;
2455 /* If doing a relocateable link, allocate space for the pointers we
2457 if (info
->relocateable
)
2461 /* We use section_count + 1, rather than section_count, because
2462 the target_index fields are 1 based. */
2463 finfo
.section_info
=
2464 ((struct coff_link_section_info
*)
2465 bfd_malloc ((abfd
->section_count
+ 1)
2466 * sizeof (struct coff_link_section_info
)));
2467 if (finfo
.section_info
== NULL
)
2469 for (i
= 0; i
<= abfd
->section_count
; i
++)
2471 finfo
.section_info
[i
].relocs
= NULL
;
2472 finfo
.section_info
[i
].rel_hashes
= NULL
;
2476 /* We now know the size of the relocs, so we can determine the file
2477 positions of the line numbers. */
2478 line_filepos
= rel_filepos
;
2479 linesz
= bfd_coff_linesz (abfd
);
2480 max_output_reloc_count
= 0;
2481 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
2483 if (o
->lineno_count
== 0)
2484 o
->line_filepos
= 0;
2487 o
->line_filepos
= line_filepos
;
2488 line_filepos
+= o
->lineno_count
* linesz
;
2491 if (o
->reloc_count
!= 0)
2493 /* We don't know the indices of global symbols until we have
2494 written out all the local symbols. For each section in
2495 the output file, we keep an array of pointers to hash
2496 table entries. Each entry in the array corresponds to a
2497 reloc. When we find a reloc against a global symbol, we
2498 set the corresponding entry in this array so that we can
2499 fix up the symbol index after we have written out all the
2502 Because of this problem, we also keep the relocs in
2503 memory until the end of the link. This wastes memory,
2504 but only when doing a relocateable link, which is not the
2506 BFD_ASSERT (info
->relocateable
);
2507 finfo
.section_info
[o
->target_index
].relocs
=
2508 ((struct internal_reloc
*)
2509 bfd_malloc (o
->reloc_count
* sizeof (struct internal_reloc
)));
2510 finfo
.section_info
[o
->target_index
].rel_hashes
=
2511 ((struct coff_link_hash_entry
**)
2512 bfd_malloc (o
->reloc_count
2513 * sizeof (struct coff_link_hash_entry
*)));
2514 if (finfo
.section_info
[o
->target_index
].relocs
== NULL
2515 || finfo
.section_info
[o
->target_index
].rel_hashes
== NULL
)
2518 if (o
->reloc_count
> max_output_reloc_count
)
2519 max_output_reloc_count
= o
->reloc_count
;
2522 /* Reset the reloc and lineno counts, so that we can use them to
2523 count the number of entries we have output so far. */
2525 o
->lineno_count
= 0;
2528 obj_sym_filepos (abfd
) = line_filepos
;
2530 /* Figure out the largest number of symbols in an input BFD. Take
2531 the opportunity to clear the output_has_begun fields of all the
2534 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
2538 sub
->output_has_begun
= false;
2539 sz
= obj_raw_syment_count (sub
);
2540 if (sz
> max_sym_count
)
2544 /* Allocate some buffers used while linking. */
2545 finfo
.internal_syms
= ((struct internal_syment
*)
2546 bfd_malloc (max_sym_count
2547 * sizeof (struct internal_syment
)));
2548 finfo
.sec_ptrs
= (asection
**) bfd_malloc (max_sym_count
2549 * sizeof (asection
*));
2550 finfo
.sym_indices
= (long *) bfd_malloc (max_sym_count
* sizeof (long));
2551 finfo
.outsyms
= ((bfd_byte
*)
2552 bfd_malloc ((size_t) ((max_sym_count
+ 1) * symesz
)));
2553 finfo
.linenos
= (bfd_byte
*) bfd_malloc (max_lineno_count
2554 * bfd_coff_linesz (abfd
));
2555 finfo
.contents
= (bfd_byte
*) bfd_malloc (max_contents_size
);
2556 finfo
.external_relocs
= (bfd_byte
*) bfd_malloc (max_reloc_count
* relsz
);
2557 if (! info
->relocateable
)
2558 finfo
.internal_relocs
= ((struct internal_reloc
*)
2559 bfd_malloc (max_reloc_count
2560 * sizeof (struct internal_reloc
)));
2561 if ((finfo
.internal_syms
== NULL
&& max_sym_count
> 0)
2562 || (finfo
.sec_ptrs
== NULL
&& max_sym_count
> 0)
2563 || (finfo
.sym_indices
== NULL
&& max_sym_count
> 0)
2564 || finfo
.outsyms
== NULL
2565 || (finfo
.linenos
== NULL
&& max_lineno_count
> 0)
2566 || (finfo
.contents
== NULL
&& max_contents_size
> 0)
2567 || (finfo
.external_relocs
== NULL
&& max_reloc_count
> 0)
2568 || (! info
->relocateable
2569 && finfo
.internal_relocs
== NULL
2570 && max_reloc_count
> 0))
2573 /* We now know the position of everything in the file, except that
2574 we don't know the size of the symbol table and therefore we don't
2575 know where the string table starts. We just build the string
2576 table in memory as we go along. We process all the relocations
2577 for a single input file at once. */
2578 obj_raw_syment_count (abfd
) = 0;
2580 if (coff_backend_info (abfd
)->_bfd_coff_start_final_link
)
2582 if (! bfd_coff_start_final_link (abfd
, info
))
2586 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
2588 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
2590 if (p
->type
== bfd_indirect_link_order
2591 && (bfd_get_flavour (p
->u
.indirect
.section
->owner
)
2592 == bfd_target_coff_flavour
))
2594 sub
= p
->u
.indirect
.section
->owner
;
2595 #ifdef POWERPC_LE_PE
2596 if (! sub
->output_has_begun
&& !ppc_do_last(sub
))
2598 if (! sub
->output_has_begun
)
2601 if (! _bfd_coff_link_input_bfd (&finfo
, sub
))
2603 sub
->output_has_begun
= true;
2606 else if (p
->type
== bfd_section_reloc_link_order
2607 || p
->type
== bfd_symbol_reloc_link_order
)
2609 if (! _bfd_coff_reloc_link_order (abfd
, &finfo
, o
, p
))
2614 if (! _bfd_default_link_order (abfd
, info
, o
, p
))
2620 #ifdef POWERPC_LE_PE
2622 bfd
* last_one
= ppc_get_last();
2625 if (! _bfd_coff_link_input_bfd (&finfo
, last_one
))
2628 last_one
->output_has_begun
= true;
2632 /* Free up the buffers used by _bfd_coff_link_input_bfd. */
2634 coff_debug_merge_hash_table_free (&finfo
.debug_merge
);
2635 debug_merge_allocated
= false;
2637 if (finfo
.internal_syms
!= NULL
)
2639 free (finfo
.internal_syms
);
2640 finfo
.internal_syms
= NULL
;
2642 if (finfo
.sec_ptrs
!= NULL
)
2644 free (finfo
.sec_ptrs
);
2645 finfo
.sec_ptrs
= NULL
;
2647 if (finfo
.sym_indices
!= NULL
)
2649 free (finfo
.sym_indices
);
2650 finfo
.sym_indices
= NULL
;
2652 if (finfo
.linenos
!= NULL
)
2654 free (finfo
.linenos
);
2655 finfo
.linenos
= NULL
;
2657 if (finfo
.contents
!= NULL
)
2659 free (finfo
.contents
);
2660 finfo
.contents
= NULL
;
2662 if (finfo
.external_relocs
!= NULL
)
2664 free (finfo
.external_relocs
);
2665 finfo
.external_relocs
= NULL
;
2667 if (finfo
.internal_relocs
!= NULL
)
2669 free (finfo
.internal_relocs
);
2670 finfo
.internal_relocs
= NULL
;
2673 /* The value of the last C_FILE symbol is supposed to be the symbol
2674 index of the first external symbol. Write it out again if
2676 if (finfo
.last_file_index
!= -1
2677 && (unsigned int) finfo
.last_file
.n_value
!= obj_raw_syment_count (abfd
))
2679 finfo
.last_file
.n_value
= obj_raw_syment_count (abfd
);
2680 bfd_coff_swap_sym_out (abfd
, (PTR
) &finfo
.last_file
,
2681 (PTR
) finfo
.outsyms
);
2683 (obj_sym_filepos (abfd
)
2684 + finfo
.last_file_index
* symesz
),
2686 || bfd_write (finfo
.outsyms
, symesz
, 1, abfd
) != symesz
)
2690 /* Write out the global symbols. */
2691 finfo
.failed
= false;
2692 coff_link_hash_traverse (coff_hash_table (info
), _bfd_coff_write_global_sym
,
2697 /* The outsyms buffer is used by _bfd_coff_write_global_sym. */
2698 if (finfo
.outsyms
!= NULL
)
2700 free (finfo
.outsyms
);
2701 finfo
.outsyms
= NULL
;
2704 if (info
->relocateable
)
2706 /* Now that we have written out all the global symbols, we know
2707 the symbol indices to use for relocs against them, and we can
2708 finally write out the relocs. */
2709 external_relocs
= ((bfd_byte
*)
2710 bfd_malloc (max_output_reloc_count
* relsz
));
2711 if (external_relocs
== NULL
)
2714 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
2716 struct internal_reloc
*irel
;
2717 struct internal_reloc
*irelend
;
2718 struct coff_link_hash_entry
**rel_hash
;
2721 if (o
->reloc_count
== 0)
2724 irel
= finfo
.section_info
[o
->target_index
].relocs
;
2725 irelend
= irel
+ o
->reloc_count
;
2726 rel_hash
= finfo
.section_info
[o
->target_index
].rel_hashes
;
2727 erel
= external_relocs
;
2728 for (; irel
< irelend
; irel
++, rel_hash
++, erel
+= relsz
)
2730 if (*rel_hash
!= NULL
)
2732 BFD_ASSERT ((*rel_hash
)->indx
>= 0);
2733 irel
->r_symndx
= (*rel_hash
)->indx
;
2735 bfd_coff_swap_reloc_out (abfd
, (PTR
) irel
, (PTR
) erel
);
2738 if (bfd_seek (abfd
, o
->rel_filepos
, SEEK_SET
) != 0
2739 || bfd_write ((PTR
) external_relocs
, relsz
, o
->reloc_count
,
2740 abfd
) != relsz
* o
->reloc_count
)
2744 free (external_relocs
);
2745 external_relocs
= NULL
;
2748 /* Free up the section information. */
2749 if (finfo
.section_info
!= NULL
)
2753 for (i
= 0; i
< abfd
->section_count
; i
++)
2755 if (finfo
.section_info
[i
].relocs
!= NULL
)
2756 free (finfo
.section_info
[i
].relocs
);
2757 if (finfo
.section_info
[i
].rel_hashes
!= NULL
)
2758 free (finfo
.section_info
[i
].rel_hashes
);
2760 free (finfo
.section_info
);
2761 finfo
.section_info
= NULL
;
2764 /* If we have optimized stabs strings, output them. */
2765 if (coff_hash_table (info
)->stab_info
!= NULL
)
2767 if (! _bfd_write_stab_strings (abfd
, &coff_hash_table (info
)->stab_info
))
2771 /* Write out the string table. */
2772 if (obj_raw_syment_count (abfd
) != 0)
2775 (obj_sym_filepos (abfd
)
2776 + obj_raw_syment_count (abfd
) * symesz
),
2780 #if STRING_SIZE_SIZE == 4
2782 _bfd_stringtab_size (finfo
.strtab
) + STRING_SIZE_SIZE
,
2783 (bfd_byte
*) strbuf
);
2785 #error Change bfd_h_put_32
2788 if (bfd_write (strbuf
, 1, STRING_SIZE_SIZE
, abfd
) != STRING_SIZE_SIZE
)
2791 if (! _bfd_stringtab_emit (abfd
, finfo
.strtab
))
2795 _bfd_stringtab_free (finfo
.strtab
);
2797 /* Setting bfd_get_symcount to 0 will cause write_object_contents to
2798 not try to write out the symbols. */
2799 bfd_get_symcount (abfd
) = 0;
2804 if (debug_merge_allocated
)
2805 coff_debug_merge_hash_table_free (&finfo
.debug_merge
);
2806 if (finfo
.strtab
!= NULL
)
2807 _bfd_stringtab_free (finfo
.strtab
);
2808 if (finfo
.section_info
!= NULL
)
2812 for (i
= 0; i
< abfd
->section_count
; i
++)
2814 if (finfo
.section_info
[i
].relocs
!= NULL
)
2815 free (finfo
.section_info
[i
].relocs
);
2816 if (finfo
.section_info
[i
].rel_hashes
!= NULL
)
2817 free (finfo
.section_info
[i
].rel_hashes
);
2819 free (finfo
.section_info
);
2821 if (finfo
.internal_syms
!= NULL
)
2822 free (finfo
.internal_syms
);
2823 if (finfo
.sec_ptrs
!= NULL
)
2824 free (finfo
.sec_ptrs
);
2825 if (finfo
.sym_indices
!= NULL
)
2826 free (finfo
.sym_indices
);
2827 if (finfo
.outsyms
!= NULL
)
2828 free (finfo
.outsyms
);
2829 if (finfo
.linenos
!= NULL
)
2830 free (finfo
.linenos
);
2831 if (finfo
.contents
!= NULL
)
2832 free (finfo
.contents
);
2833 if (finfo
.external_relocs
!= NULL
)
2834 free (finfo
.external_relocs
);
2835 if (finfo
.internal_relocs
!= NULL
)
2836 free (finfo
.internal_relocs
);
2837 if (external_relocs
!= NULL
)
2838 free (external_relocs
);
2844 /* Forward declaration for use by alternative_target field. */
2845 #ifdef TARGET_BIG_SYM
2846 extern const bfd_target TARGET_BIG_SYM
;
2849 /* The transfer vectors that lead the outside world to all of the above. */
2851 #ifdef TARGET_LITTLE_SYM
2852 const bfd_target TARGET_LITTLE_SYM
=
2854 TARGET_LITTLE_NAME
, /* name or coff-arm-little */
2855 bfd_target_coff_flavour
,
2856 BFD_ENDIAN_LITTLE
, /* data byte order is little */
2857 BFD_ENDIAN_LITTLE
, /* header byte order is little */
2859 (HAS_RELOC
| EXEC_P
| /* FIXME: object flags */
2860 HAS_LINENO
| HAS_DEBUG
|
2861 HAS_SYMS
| HAS_LOCALS
| WP_TEXT
| D_PAGED
),
2863 #ifndef COFF_WITH_PE
2864 (SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
), /* section flags */
2866 (SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
/* section flags */
2867 | SEC_LINK_ONCE
| SEC_LINK_DUPLICATES
),
2870 0, /* leading char */
2871 '/', /* ar_pad_char */
2872 15, /* ar_max_namelen??? FIXMEmgo */
2874 bfd_getl64
, bfd_getl_signed_64
, bfd_putl64
,
2875 bfd_getl32
, bfd_getl_signed_32
, bfd_putl32
,
2876 bfd_getl16
, bfd_getl_signed_16
, bfd_putl16
, /* data */
2878 bfd_getl64
, bfd_getl_signed_64
, bfd_putl64
,
2879 bfd_getl32
, bfd_getl_signed_32
, bfd_putl32
,
2880 bfd_getl16
, bfd_getl_signed_16
, bfd_putl16
, /* hdrs */
2882 {_bfd_dummy_target
, coff_object_p
, /* bfd_check_format */
2883 bfd_generic_archive_p
, /* _bfd_dummy_target */ coff_object_p
},
2884 {bfd_false
, coff_mkobject
, _bfd_generic_mkarchive
, /* bfd_set_format */
2886 {bfd_false
, coff_write_object_contents
, /* bfd_write_contents */
2887 _bfd_write_archive_contents
, bfd_false
},
2889 BFD_JUMP_TABLE_GENERIC (coff
),
2890 BFD_JUMP_TABLE_COPY (coff
),
2891 BFD_JUMP_TABLE_CORE (_bfd_nocore
),
2892 BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_coff
),
2893 BFD_JUMP_TABLE_SYMBOLS (coff
),
2894 BFD_JUMP_TABLE_RELOCS (coff
),
2895 BFD_JUMP_TABLE_WRITE (coff
),
2896 BFD_JUMP_TABLE_LINK (coff
),
2897 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic
),
2899 /* Alternative_target. */
2900 #ifdef TARGET_BIG_SYM
2910 #ifdef TARGET_BIG_SYM
2911 const bfd_target TARGET_BIG_SYM
=
2914 bfd_target_coff_flavour
,
2915 BFD_ENDIAN_BIG
, /* data byte order is big */
2916 BFD_ENDIAN_BIG
, /* header byte order is big */
2918 (HAS_RELOC
| EXEC_P
| /* FIXME: object flags */
2919 HAS_LINENO
| HAS_DEBUG
|
2920 HAS_SYMS
| HAS_LOCALS
| WP_TEXT
| D_PAGED
),
2922 #ifndef COFF_WITH_PE
2923 (SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
), /* section flags */
2925 (SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
/* section flags */
2926 | SEC_LINK_ONCE
| SEC_LINK_DUPLICATES
),
2929 0, /* leading char */
2930 '/', /* ar_pad_char */
2931 15, /* ar_max_namelen??? FIXMEmgo */
2933 bfd_getb64
, bfd_getb_signed_64
, bfd_putb64
,
2934 bfd_getb32
, bfd_getb_signed_32
, bfd_putb32
,
2935 bfd_getb16
, bfd_getb_signed_16
, bfd_putb16
, /* data */
2937 bfd_getb64
, bfd_getb_signed_64
, bfd_putb64
,
2938 bfd_getb32
, bfd_getb_signed_32
, bfd_putb32
,
2939 bfd_getb16
, bfd_getb_signed_16
, bfd_putb16
, /* hdrs */
2941 {_bfd_dummy_target
, coff_object_p
, /* bfd_check_format */
2942 bfd_generic_archive_p
, /* _bfd_dummy_target */ coff_object_p
},
2943 {bfd_false
, coff_mkobject
, _bfd_generic_mkarchive
, /* bfd_set_format */
2945 {bfd_false
, coff_write_object_contents
, /* bfd_write_contents */
2946 _bfd_write_archive_contents
, bfd_false
},
2948 BFD_JUMP_TABLE_GENERIC (coff
),
2949 BFD_JUMP_TABLE_COPY (coff
),
2950 BFD_JUMP_TABLE_CORE (_bfd_nocore
),
2951 BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_coff
),
2952 BFD_JUMP_TABLE_SYMBOLS (coff
),
2953 BFD_JUMP_TABLE_RELOCS (coff
),
2954 BFD_JUMP_TABLE_WRITE (coff
),
2955 BFD_JUMP_TABLE_LINK (coff
),
2956 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic
),
2959 /* Alternative_target. */
2960 #ifdef TARGET_LITTLE_SYM
2961 & TARGET_LITTLE_SYM
,