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.
41 #include "coff/powerpc.h"
42 #include "coff/internal.h"
50 #define BADMAG(x) PPCBADMAG(x)
54 /* This file is compiled more than once, but we only compile the
55 final_link routine once. */
56 extern boolean ppc_bfd_coff_final_link
57 PARAMS ((bfd
*, struct bfd_link_info
*));
58 extern void dump_toc
PARAMS ((PTR
));
60 /* The toc is a set of bfd_vma fields. We use the fact that valid */
61 /* addresses are even (i.e. the bit representing "1" is off) to allow */
62 /* us to encode a little extra information in the field */
63 /* - Unallocated addresses are intialized to 1. */
64 /* - Allocated addresses are even numbers. */
65 /* The first time we actually write a reference to the toc in the bfd, */
66 /* we want to record that fact in a fixup file (if it is asked for), so */
67 /* we keep track of whether or not an address has been written by marking */
68 /* the low order bit with a "1" upon writing */
70 #define SET_UNALLOCATED(x) ((x) = 1)
71 #define IS_UNALLOCATED(x) ((x) == 1)
73 #define IS_WRITTEN(x) ((x) & 1)
74 #define MARK_AS_WRITTEN(x) ((x) |= 1)
75 #define MAKE_ADDR_AGAIN(x) ((x) &= ~1)
77 /* Turn on this check if you suspect something amiss in the hash tables */
80 /* Need a 7 char string for an eye catcher */
83 #define HASH_CHECK_DCL char eye_catcher[8];
84 #define HASH_CHECK_INIT(ret) strcpy(ret->eye_catcher, EYE)
85 #define HASH_CHECK(addr) \
86 if (strcmp(addr->eye_catcher, EYE) != 0) \
89 _("File %s, line %d, Hash check failure, bad eye %8s\n"), \
90 __FILE__, __LINE__, addr->eye_catcher); \
96 #define HASH_CHECK_DCL
97 #define HASH_CHECK_INIT(ret)
98 #define HASH_CHECK(addr)
102 /* In order not to add an int to every hash table item for every coff
103 linker, we define our own hash table, derived from the coff one */
105 /* PE linker hash table entries. */
107 struct ppc_coff_link_hash_entry
109 struct coff_link_hash_entry root
; /* First entry, as required */
111 /* As we wonder around the relocs, we'll keep the assigned toc_offset
113 bfd_vma toc_offset
; /* Our addition, as required */
115 unsigned long int glue_insn
;
120 /* PE linker hash table. */
122 struct ppc_coff_link_hash_table
124 struct coff_link_hash_table root
; /* First entry, as required */
127 static struct bfd_hash_entry
*ppc_coff_link_hash_newfunc
128 PARAMS ((struct bfd_hash_entry
*, struct bfd_hash_table
*,
130 static boolean ppc_coff_link_hash_table_init
131 PARAMS ((struct ppc_coff_link_hash_table
*, bfd
*,
132 struct bfd_hash_entry
*(*) (struct bfd_hash_entry
*,
133 struct bfd_hash_table
*,
135 static struct bfd_link_hash_table
*ppc_coff_link_hash_table_create
137 static boolean coff_ppc_relocate_section
138 PARAMS ((bfd
*, struct bfd_link_info
*, bfd
*, asection
*, bfd_byte
*,
139 struct internal_reloc
*, struct internal_syment
*, asection
**));
140 static reloc_howto_type
*coff_ppc_rtype_to_howto
141 PARAMS ((bfd
*, asection
*, struct internal_reloc
*,
142 struct coff_link_hash_entry
*, struct internal_syment
*,
145 /* Routine to create an entry in the link hash table. */
147 static struct bfd_hash_entry
*
148 ppc_coff_link_hash_newfunc (entry
, table
, string
)
149 struct bfd_hash_entry
*entry
;
150 struct bfd_hash_table
*table
;
153 struct ppc_coff_link_hash_entry
*ret
=
154 (struct ppc_coff_link_hash_entry
*) entry
;
156 /* Allocate the structure if it has not already been allocated by a
158 if (ret
== (struct ppc_coff_link_hash_entry
*) NULL
)
159 ret
= (struct ppc_coff_link_hash_entry
*)
160 bfd_hash_allocate (table
,
161 sizeof (struct ppc_coff_link_hash_entry
));
163 if (ret
== (struct ppc_coff_link_hash_entry
*) NULL
)
166 /* Call the allocation method of the superclass. */
167 ret
= ((struct ppc_coff_link_hash_entry
*)
168 _bfd_coff_link_hash_newfunc ((struct bfd_hash_entry
*) ret
,
173 /* Initialize the local fields. */
174 SET_UNALLOCATED(ret
->toc_offset
);
175 ret
->symbol_is_glue
= 0;
178 HASH_CHECK_INIT(ret
);
181 return (struct bfd_hash_entry
*) ret
;
184 /* Initialize a PE linker hash table. */
187 ppc_coff_link_hash_table_init (table
, abfd
, newfunc
)
188 struct ppc_coff_link_hash_table
*table
;
190 struct bfd_hash_entry
*(*newfunc
) PARAMS ((struct bfd_hash_entry
*,
191 struct bfd_hash_table
*,
194 return _bfd_coff_link_hash_table_init (&table
->root
, abfd
, newfunc
);
197 /* Create a PE linker hash table. */
199 static struct bfd_link_hash_table
*
200 ppc_coff_link_hash_table_create (abfd
)
203 struct ppc_coff_link_hash_table
*ret
;
205 ret
= ((struct ppc_coff_link_hash_table
*)
206 bfd_alloc (abfd
, sizeof (struct ppc_coff_link_hash_table
)));
209 if (! ppc_coff_link_hash_table_init (ret
, abfd
,
210 ppc_coff_link_hash_newfunc
))
212 bfd_release (abfd
, ret
);
213 return (struct bfd_link_hash_table
*) NULL
;
215 return &ret
->root
.root
;
218 /* Now, tailor coffcode.h to use our hash stuff */
220 #define coff_bfd_link_hash_table_create ppc_coff_link_hash_table_create
222 /* The nt loader points the toc register to &toc + 32768, in order to */
223 /* use the complete range of a 16-bit displacement. We have to adjust */
224 /* for this when we fix up loads displaced off the toc reg. */
225 #define TOC_LOAD_ADJUSTMENT (-32768)
226 #define TOC_SECTION_NAME ".private.toc"
228 /* The main body of code is in coffcode.h. */
230 #define COFF_DEFAULT_SECTION_ALIGNMENT_POWER (3)
232 /* In case we're on a 32-bit machine, construct a 64-bit "-1" value
233 from smaller values. Start with zero, widen, *then* decrement. */
234 #define MINUS_ONE (((bfd_vma)0) - 1)
236 /* these should definitely go in a header file somewhere... */
239 #define IMAGE_REL_PPC_ABSOLUTE 0x0000
242 #define IMAGE_REL_PPC_ADDR64 0x0001
245 #define IMAGE_REL_PPC_ADDR32 0x0002
247 /* 26-bit address, shifted left 2 (branch absolute) */
248 #define IMAGE_REL_PPC_ADDR24 0x0003
251 #define IMAGE_REL_PPC_ADDR16 0x0004
253 /* 16-bit address, shifted left 2 (load doubleword) */
254 #define IMAGE_REL_PPC_ADDR14 0x0005
256 /* 26-bit PC-relative offset, shifted left 2 (branch relative) */
257 #define IMAGE_REL_PPC_REL24 0x0006
259 /* 16-bit PC-relative offset, shifted left 2 (br cond relative) */
260 #define IMAGE_REL_PPC_REL14 0x0007
262 /* 16-bit offset from TOC base */
263 #define IMAGE_REL_PPC_TOCREL16 0x0008
265 /* 16-bit offset from TOC base, shifted left 2 (load doubleword) */
266 #define IMAGE_REL_PPC_TOCREL14 0x0009
268 /* 32-bit addr w/o image base */
269 #define IMAGE_REL_PPC_ADDR32NB 0x000A
271 /* va of containing section (as in an image sectionhdr) */
272 #define IMAGE_REL_PPC_SECREL 0x000B
274 /* sectionheader number */
275 #define IMAGE_REL_PPC_SECTION 0x000C
277 /* substitute TOC restore instruction iff symbol is glue code */
278 #define IMAGE_REL_PPC_IFGLUE 0x000D
280 /* symbol is glue code; virtual address is TOC restore instruction */
281 #define IMAGE_REL_PPC_IMGLUE 0x000E
283 /* va of containing section (limited to 16 bits) */
284 #define IMAGE_REL_PPC_SECREL16 0x000F
286 /* stuff to handle immediate data when the number of bits in the */
287 /* data is greater than the number of bits in the immediate field */
288 /* We need to do (usually) 32 bit arithmetic on 16 bit chunks */
289 #define IMAGE_REL_PPC_REFHI 0x0010
290 #define IMAGE_REL_PPC_REFLO 0x0011
291 #define IMAGE_REL_PPC_PAIR 0x0012
293 /* This is essentially the same as tocrel16, with TOCDEFN assumed */
294 #define IMAGE_REL_PPC_TOCREL16_DEFN 0x0013
296 /* Flag bits in IMAGE_RELOCATION.TYPE */
298 /* subtract reloc value rather than adding it */
299 #define IMAGE_REL_PPC_NEG 0x0100
301 /* fix branch prediction bit to predict branch taken */
302 #define IMAGE_REL_PPC_BRTAKEN 0x0200
304 /* fix branch prediction bit to predict branch not taken */
305 #define IMAGE_REL_PPC_BRNTAKEN 0x0400
307 /* toc slot defined in file (or, data in toc) */
308 #define IMAGE_REL_PPC_TOCDEFN 0x0800
310 /* masks to isolate above values in IMAGE_RELOCATION.Type */
311 #define IMAGE_REL_PPC_TYPEMASK 0x00FF
312 #define IMAGE_REL_PPC_FLAGMASK 0x0F00
314 #define EXTRACT_TYPE(x) ((x) & IMAGE_REL_PPC_TYPEMASK)
315 #define EXTRACT_FLAGS(x) ((x) & IMAGE_REL_PPC_FLAGMASK)
316 #define EXTRACT_JUNK(x) \
317 ((x) & ~(IMAGE_REL_PPC_TYPEMASK | IMAGE_REL_PPC_FLAGMASK))
319 /* static helper functions to make relocation work */
320 /* (Work In Progress) */
322 static bfd_reloc_status_type ppc_refhi_reloc
PARAMS ((bfd
*abfd
,
330 static bfd_reloc_status_type ppc_reflo_reloc
PARAMS ((bfd
*abfd
,
338 static bfd_reloc_status_type ppc_pair_reloc
PARAMS ((bfd
*abfd
,
346 static bfd_reloc_status_type ppc_toc16_reloc
PARAMS ((bfd
*abfd
,
355 static bfd_reloc_status_type ppc_addr32nb_reloc
PARAMS ((bfd
*abfd
,
363 static bfd_reloc_status_type ppc_section_reloc
PARAMS ((bfd
*abfd
,
371 static bfd_reloc_status_type ppc_secrel_reloc
PARAMS ((bfd
*abfd
,
379 static bfd_reloc_status_type ppc_imglue_reloc
PARAMS ((bfd
*abfd
,
387 static boolean in_reloc_p
PARAMS((bfd
*abfd
, reloc_howto_type
*howto
));
389 /* FIXME: It'll take a while to get through all of these. I only need a few to
390 get us started, so those I'll make sure work. Those marked FIXME are either
391 completely unverified or have a specific unknown marked in the comment */
393 /*---------------------------------------------------------------------------*/
395 /* Relocation entries for Windows/NT on PowerPC. */
397 /* From the document "" we find the following listed as used relocs: */
399 /* ABSOLUTE : The noop */
400 /* ADDR[64|32|16] : fields that hold addresses in data fields or the */
401 /* 16 bit displacement field on a load/store. */
402 /* ADDR[24|14] : fields that hold addresses in branch and cond */
403 /* branches. These represent [26|16] bit addresses. */
404 /* The low order 2 bits are preserved. */
405 /* REL[24|14] : branches relative to the Instruction Address */
406 /* register. These represent [26|16] bit addresses, */
407 /* as before. The instruction field will be zero, and */
408 /* the address of the SYM will be inserted at link time. */
409 /* TOCREL16 : 16 bit displacement field referring to a slot in */
411 /* TOCREL14 : 16 bit displacement field, similar to REL14 or ADDR14. */
412 /* ADDR32NB : 32 bit address relative to the virtual origin. */
413 /* (On the alpha, this is always a linker generated thunk)*/
414 /* (i.e. 32bit addr relative to the image base) */
415 /* SECREL : The value is relative to the start of the section */
416 /* containing the symbol. */
417 /* SECTION : access to the header containing the item. Supports the */
418 /* codeview debugger. */
420 /* In particular, note that the document does not indicate that the */
421 /* relocations listed in the header file are used. */
425 /*---------------------------------------------------------------------------*/
427 static reloc_howto_type ppc_coff_howto_table
[] =
429 /* IMAGE_REL_PPC_ABSOLUTE 0x0000 NOP */
431 HOWTO (IMAGE_REL_PPC_ABSOLUTE
, /* type */
433 0, /* size (0 = byte, 1 = short, 2 = long) */
435 false, /* pc_relative */
437 complain_overflow_dont
, /* dont complain_on_overflow */
438 0, /* special_function */
439 "ABSOLUTE", /* name */
440 false, /* partial_inplace */
443 false), /* pcrel_offset */
445 /* IMAGE_REL_PPC_ADDR64 0x0001 64-bit address */
447 HOWTO(IMAGE_REL_PPC_ADDR64
, /* type */
449 3, /* size (0 = byte, 1 = short, 2 = long) */
451 false, /* pc_relative */
453 complain_overflow_bitfield
, /* complain_on_overflow */
454 0, /* special_function */
456 true, /* partial_inplace */
457 MINUS_ONE
, /* src_mask */
458 MINUS_ONE
, /* dst_mask */
459 false), /* pcrel_offset */
461 /* IMAGE_REL_PPC_ADDR32 0x0002 32-bit address */
463 HOWTO (IMAGE_REL_PPC_ADDR32
, /* type */
465 2, /* size (0 = byte, 1 = short, 2 = long) */
467 false, /* pc_relative */
469 complain_overflow_bitfield
, /* complain_on_overflow */
470 0, /* special_function */
472 true, /* partial_inplace */
473 0xffffffff, /* src_mask */
474 0xffffffff, /* dst_mask */
475 false), /* pcrel_offset */
477 /* IMAGE_REL_PPC_ADDR24 0x0003 26-bit address, shifted left 2 (branch absolute) */
478 /* the LI field is in bit 6 through bit 29 is 24 bits, + 2 for the shift */
479 /* Of course, That's the IBM approved bit numbering, which is not what */
480 /* anyone else uses.... The li field is in bit 2 thru 25 */
482 HOWTO (IMAGE_REL_PPC_ADDR24
, /* type */
484 2, /* size (0 = byte, 1 = short, 2 = long) */
486 false, /* pc_relative */
488 complain_overflow_bitfield
, /* complain_on_overflow */
489 0, /* special_function */
491 true, /* partial_inplace */
492 0x07fffffc, /* src_mask */
493 0x07fffffc, /* dst_mask */
494 false), /* pcrel_offset */
496 /* IMAGE_REL_PPC_ADDR16 0x0004 16-bit address */
498 HOWTO (IMAGE_REL_PPC_ADDR16
, /* type */
500 1, /* size (0 = byte, 1 = short, 2 = long) */
502 false, /* pc_relative */
504 complain_overflow_signed
, /* complain_on_overflow */
505 0, /* special_function */
507 true, /* partial_inplace */
508 0xffff, /* src_mask */
509 0xffff, /* dst_mask */
510 false), /* pcrel_offset */
512 /* IMAGE_REL_PPC_ADDR14 0x0005 */
513 /* 16-bit address, shifted left 2 (load doubleword) */
514 /* FIXME: the mask is likely wrong, and the bit position may be as well */
516 HOWTO (IMAGE_REL_PPC_ADDR14
, /* type */
518 1, /* size (0 = byte, 1 = short, 2 = long) */
520 false, /* pc_relative */
522 complain_overflow_signed
, /* complain_on_overflow */
523 0, /* special_function */
525 true, /* partial_inplace */
526 0xffff, /* src_mask */
527 0xffff, /* dst_mask */
528 false), /* pcrel_offset */
530 /* IMAGE_REL_PPC_REL24 0x0006 */
531 /* 26-bit PC-relative offset, shifted left 2 (branch relative) */
533 HOWTO (IMAGE_REL_PPC_REL24
, /* type */
535 2, /* size (0 = byte, 1 = short, 2 = long) */
537 true, /* pc_relative */
539 complain_overflow_signed
, /* complain_on_overflow */
540 0, /* special_function */
542 true, /* partial_inplace */
543 0x3fffffc, /* src_mask */
544 0x3fffffc, /* dst_mask */
545 false), /* pcrel_offset */
547 /* IMAGE_REL_PPC_REL14 0x0007 */
548 /* 16-bit PC-relative offset, shifted left 2 (br cond relative) */
549 /* FIXME: the mask is likely wrong, and the bit position may be as well */
550 /* FIXME: how does it know how far to shift? */
552 HOWTO (IMAGE_REL_PPC_ADDR14
, /* type */
554 1, /* size (0 = byte, 1 = short, 2 = long) */
556 false, /* pc_relative */
558 complain_overflow_signed
, /* complain_on_overflow */
559 0, /* special_function */
561 true, /* partial_inplace */
562 0xffff, /* src_mask */
563 0xffff, /* dst_mask */
564 true), /* pcrel_offset */
566 /* IMAGE_REL_PPC_TOCREL16 0x0008 */
567 /* 16-bit offset from TOC base */
569 HOWTO (IMAGE_REL_PPC_TOCREL16
,/* type */
571 1, /* size (0 = byte, 1 = short, 2 = long) */
573 false, /* pc_relative */
575 complain_overflow_dont
, /* complain_on_overflow */
576 ppc_toc16_reloc
, /* special_function */
577 "TOCREL16", /* name */
578 false, /* partial_inplace */
579 0xffff, /* src_mask */
580 0xffff, /* dst_mask */
581 false), /* pcrel_offset */
583 /* IMAGE_REL_PPC_TOCREL14 0x0009 */
584 /* 16-bit offset from TOC base, shifted left 2 (load doubleword) */
586 HOWTO (IMAGE_REL_PPC_TOCREL14
,/* type */
588 1, /* size (0 = byte, 1 = short, 2 = long) */
590 false, /* pc_relative */
592 complain_overflow_signed
, /* complain_on_overflow */
593 0, /* special_function */
594 "TOCREL14", /* name */
595 false, /* partial_inplace */
596 0xffff, /* src_mask */
597 0xffff, /* dst_mask */
598 false), /* pcrel_offset */
600 /* IMAGE_REL_PPC_ADDR32NB 0x000A */
601 /* 32-bit addr w/ image base */
603 HOWTO (IMAGE_REL_PPC_ADDR32NB
,/* type */
605 2, /* size (0 = byte, 1 = short, 2 = long) */
607 false, /* pc_relative */
609 complain_overflow_signed
, /* complain_on_overflow */
610 0, /* special_function */
611 "ADDR32NB", /* name */
612 true, /* partial_inplace */
613 0xffffffff, /* src_mask */
614 0xffffffff, /* dst_mask */
615 false), /* pcrel_offset */
617 /* IMAGE_REL_PPC_SECREL 0x000B */
618 /* va of containing section (as in an image sectionhdr) */
620 HOWTO (IMAGE_REL_PPC_SECREL
,/* type */
622 2, /* size (0 = byte, 1 = short, 2 = long) */
624 false, /* pc_relative */
626 complain_overflow_signed
, /* complain_on_overflow */
627 ppc_secrel_reloc
, /* special_function */
629 true, /* partial_inplace */
630 0xffffffff, /* src_mask */
631 0xffffffff, /* dst_mask */
632 true), /* pcrel_offset */
634 /* IMAGE_REL_PPC_SECTION 0x000C */
635 /* sectionheader number */
637 HOWTO (IMAGE_REL_PPC_SECTION
,/* type */
639 2, /* size (0 = byte, 1 = short, 2 = long) */
641 false, /* pc_relative */
643 complain_overflow_signed
, /* complain_on_overflow */
644 ppc_section_reloc
, /* special_function */
645 "SECTION", /* name */
646 true, /* partial_inplace */
647 0xffffffff, /* src_mask */
648 0xffffffff, /* dst_mask */
649 true), /* pcrel_offset */
651 /* IMAGE_REL_PPC_IFGLUE 0x000D */
652 /* substitute TOC restore instruction iff symbol is glue code */
654 HOWTO (IMAGE_REL_PPC_IFGLUE
,/* type */
656 2, /* size (0 = byte, 1 = short, 2 = long) */
658 false, /* pc_relative */
660 complain_overflow_signed
, /* complain_on_overflow */
661 0, /* special_function */
663 true, /* partial_inplace */
664 0xffffffff, /* src_mask */
665 0xffffffff, /* dst_mask */
666 false), /* pcrel_offset */
668 /* IMAGE_REL_PPC_IMGLUE 0x000E */
669 /* symbol is glue code; virtual address is TOC restore instruction */
671 HOWTO (IMAGE_REL_PPC_IMGLUE
,/* type */
673 2, /* size (0 = byte, 1 = short, 2 = long) */
675 false, /* pc_relative */
677 complain_overflow_dont
, /* complain_on_overflow */
678 ppc_imglue_reloc
, /* special_function */
680 false, /* partial_inplace */
681 0xffffffff, /* src_mask */
682 0xffffffff, /* dst_mask */
683 false), /* pcrel_offset */
685 /* IMAGE_REL_PPC_SECREL16 0x000F */
686 /* va of containing section (limited to 16 bits) */
688 HOWTO (IMAGE_REL_PPC_SECREL16
,/* type */
690 1, /* size (0 = byte, 1 = short, 2 = long) */
692 false, /* pc_relative */
694 complain_overflow_signed
, /* complain_on_overflow */
695 0, /* special_function */
696 "SECREL16", /* name */
697 true, /* partial_inplace */
698 0xffff, /* src_mask */
699 0xffff, /* dst_mask */
700 true), /* pcrel_offset */
702 /* IMAGE_REL_PPC_REFHI 0x0010 */
704 HOWTO (IMAGE_REL_PPC_REFHI
, /* type */
706 1, /* size (0 = byte, 1 = short, 2 = long) */
708 false, /* pc_relative */
710 complain_overflow_signed
, /* complain_on_overflow */
711 ppc_refhi_reloc
, /* special_function */
713 true, /* partial_inplace */
714 0xffffffff, /* src_mask */
715 0xffffffff, /* dst_mask */
716 false), /* pcrel_offset */
718 /* IMAGE_REL_PPC_REFLO 0x0011 */
720 HOWTO (IMAGE_REL_PPC_REFLO
, /* type */
722 1, /* size (0 = byte, 1 = short, 2 = long) */
724 false, /* pc_relative */
726 complain_overflow_signed
, /* complain_on_overflow */
727 ppc_refhi_reloc
, /* special_function */
729 true, /* partial_inplace */
730 0xffffffff, /* src_mask */
731 0xffffffff, /* dst_mask */
732 false), /* pcrel_offset */
734 /* IMAGE_REL_PPC_PAIR 0x0012 */
736 HOWTO (IMAGE_REL_PPC_PAIR
, /* type */
738 1, /* size (0 = byte, 1 = short, 2 = long) */
740 false, /* pc_relative */
742 complain_overflow_signed
, /* complain_on_overflow */
743 ppc_pair_reloc
, /* special_function */
745 true, /* partial_inplace */
746 0xffffffff, /* src_mask */
747 0xffffffff, /* dst_mask */
748 false), /* pcrel_offset */
750 /* IMAGE_REL_PPC_TOCREL16_DEFN 0x0013 */
751 /* 16-bit offset from TOC base, without causing a definition */
753 HOWTO ( (IMAGE_REL_PPC_TOCREL16
| IMAGE_REL_PPC_TOCDEFN
), /* type */
755 1, /* size (0 = byte, 1 = short, 2 = long) */
757 false, /* pc_relative */
759 complain_overflow_dont
, /* complain_on_overflow */
760 0, /* special_function */
761 "TOCREL16, TOCDEFN", /* name */
762 false, /* partial_inplace */
763 0xffff, /* src_mask */
764 0xffff, /* dst_mask */
765 false), /* pcrel_offset */
769 /* Some really cheezy macros that can be turned on to test stderr :-) */
778 fprintf (stderr,_("Unimplemented Relocation -- %s\n"),x); \
782 #define DUMP_RELOC(n,r) \
784 fprintf (stderr,"%s sym %d, addr %d, addend %d\n", \
785 n, (*(r->sym_ptr_ptr))->name, \
786 r->address, r->addend); \
789 /* Given a reloc name, n, and a pointer to an internal_reloc,
790 dump out interesting information on the contents
792 #define n_name _n._n_name
793 #define n_zeroes _n._n_n._n_zeroes
794 #define n_offset _n._n_n._n_offset
798 #define DUMP_RELOC2(n,r) \
800 fprintf (stderr,"%s sym %d, r_vaddr %d %s\n", \
801 n, r->r_symndx, r->r_vaddr,\
802 (((r->r_type) & IMAGE_REL_PPC_TOCDEFN) == 0) \
808 #define DUMP_RELOC(n,r)
809 #define DUMP_RELOC2(n,r)
812 /* toc construction and management routines */
814 /* This file is compiled twice, and these variables are defined in one
815 of the compilations. FIXME: This is confusing and weird. Also,
816 BFD should not use global variables. */
817 extern bfd
* bfd_of_toc_owner
;
818 extern long int global_toc_size
;
820 extern long int import_table_size
;
821 extern long int first_thunk_address
;
822 extern long int thunk_size
;
840 struct list_ele
*next
;
842 enum ref_category cat
;
847 extern struct list_ele
*head
;
848 extern struct list_ele
*tail
;
850 static void record_toc
851 PARAMS ((asection
*, int, enum ref_category
, const char *));
854 record_toc (toc_section
, our_toc_offset
, cat
, name
)
855 asection
*toc_section
;
857 enum ref_category cat
;
860 /* add this entry to our toc addr-offset-name list */
862 t
= (struct list_ele
*) bfd_malloc (sizeof (struct list_ele
));
866 t
->offset
= our_toc_offset
;
869 t
->addr
= toc_section
->output_offset
+ our_toc_offset
;
883 #ifdef COFF_IMAGE_WITH_PE
885 static boolean ppc_record_toc_entry
886 PARAMS ((bfd
*, struct bfd_link_info
*, asection
*, int, enum toc_type
));
887 static void ppc_mark_symbol_as_glue
888 PARAMS ((bfd
*, int, struct internal_reloc
*));
890 /* record a toc offset against a symbol */
892 ppc_record_toc_entry(abfd
, info
, sec
, sym
, toc_kind
)
894 struct bfd_link_info
*info ATTRIBUTE_UNUSED
;
895 asection
*sec ATTRIBUTE_UNUSED
;
897 enum toc_type toc_kind ATTRIBUTE_UNUSED
;
899 struct ppc_coff_link_hash_entry
*h
;
906 h
= (struct ppc_coff_link_hash_entry
*) (obj_coff_sym_hashes (abfd
)[sym
]);
914 local_syms
= obj_coff_local_toc_table(abfd
);
918 /* allocate a table */
920 (int *) bfd_zalloc (abfd
,
921 obj_raw_syment_count(abfd
) * sizeof (int));
924 obj_coff_local_toc_table(abfd
) = local_syms
;
925 for (i
= 0; i
< obj_raw_syment_count(abfd
); ++i
)
927 SET_UNALLOCATED(local_syms
[i
]);
931 if (IS_UNALLOCATED(local_syms
[sym
]))
933 local_syms
[sym
] = global_toc_size
;
934 global_toc_size
+= 4;
936 /* The size must fit in a 16bit displacment */
937 if (global_toc_size
> 65535)
939 (*_bfd_error_handler
) (_("TOC overflow"));
940 bfd_set_error (bfd_error_file_too_big
);
947 name
= h
->root
.root
.root
.string
;
949 /* check to see if there's a toc slot allocated. If not, do it
950 here. It will be used in relocate_section */
951 if (IS_UNALLOCATED(h
->toc_offset
))
953 h
->toc_offset
= global_toc_size
;
954 global_toc_size
+= 4;
956 /* The size must fit in a 16bit displacment */
957 if (global_toc_size
>= 65535)
959 (*_bfd_error_handler
) (_("TOC overflow"));
960 bfd_set_error (bfd_error_file_too_big
);
969 /* record a toc offset against a symbol */
971 ppc_mark_symbol_as_glue(abfd
, sym
, rel
)
974 struct internal_reloc
*rel
;
976 struct ppc_coff_link_hash_entry
*h
;
978 h
= (struct ppc_coff_link_hash_entry
*) (obj_coff_sym_hashes (abfd
)[sym
]);
982 h
->symbol_is_glue
= 1;
983 h
->glue_insn
= bfd_get_32 (abfd
, (bfd_byte
*) &rel
->r_vaddr
);
988 #endif /* COFF_IMAGE_WITH_PE */
990 /* Return true if this relocation should
991 appear in the output .reloc section. */
993 static boolean
in_reloc_p(abfd
, howto
)
994 bfd
* abfd ATTRIBUTE_UNUSED
;
995 reloc_howto_type
*howto
;
998 (! howto
->pc_relative
)
999 && (howto
->type
!= IMAGE_REL_PPC_ADDR32NB
)
1000 && (howto
->type
!= IMAGE_REL_PPC_TOCREL16
)
1001 && (howto
->type
!= IMAGE_REL_PPC_IMGLUE
)
1002 && (howto
->type
!= IMAGE_REL_PPC_IFGLUE
)
1003 && (howto
->type
!= IMAGE_REL_PPC_SECREL
)
1004 && (howto
->type
!= IMAGE_REL_PPC_SECTION
)
1005 && (howto
->type
!= IMAGE_REL_PPC_SECREL16
)
1006 && (howto
->type
!= IMAGE_REL_PPC_REFHI
)
1007 && (howto
->type
!= IMAGE_REL_PPC_REFLO
)
1008 && (howto
->type
!= IMAGE_REL_PPC_PAIR
)
1009 && (howto
->type
!= IMAGE_REL_PPC_TOCREL16_DEFN
) ;
1014 /* this function is in charge of performing all the ppc PE relocations */
1015 /* Don't yet know if we want to do this this particular way ... (krk) */
1016 /* FIXME: (it is not yet enabled) */
1018 static bfd_reloc_status_type
1019 pe_ppc_reloc (abfd
, reloc_entry
, symbol_in
, data
, input_section
, output_bfd
,
1022 arelent
*reloc_entry
;
1025 asection
*input_section
;
1027 char **error_message
;
1029 /* the consth relocation comes in two parts, we have to remember
1030 the state between calls, in these variables */
1031 static boolean part1_consth_active
= false;
1032 static unsigned long part1_consth_value
;
1034 unsigned long sym_value
;
1035 unsigned short r_type
;
1036 unsigned long addr
= reloc_entry
->address
; /*+ input_section->vma*/
1038 r_type
= reloc_entry
->howto
->type
;
1042 /* Partial linking - do nothing */
1043 reloc_entry
->address
+= input_section
->output_offset
;
1044 return bfd_reloc_ok
;
1047 if (symbol_in
!= NULL
1048 && bfd_is_und_section (symbol_in
->section
))
1050 /* Keep the state machine happy in case we're called again */
1051 if (r_type
== IMAGE_REL_PPC_REFHI
)
1053 part1_consth_active
= true;
1054 part1_consth_value
= 0;
1056 return(bfd_reloc_undefined
);
1059 if ((part1_consth_active
) && (r_type
!= IMAGE_REL_PPC_PAIR
))
1061 part1_consth_active
= false;
1062 *error_message
= (char *) _("Missing PAIR");
1063 return(bfd_reloc_dangerous
);
1066 sym_value
= get_symbol_value(symbol_in
);
1068 return(bfd_reloc_ok
);
1073 /* The reloc processing routine for the optimized COFF linker. */
1076 coff_ppc_relocate_section (output_bfd
, info
, input_bfd
, input_section
,
1077 contents
, relocs
, syms
, sections
)
1079 struct bfd_link_info
*info
;
1081 asection
*input_section
;
1083 struct internal_reloc
*relocs
;
1084 struct internal_syment
*syms
;
1085 asection
**sections
;
1087 struct internal_reloc
*rel
;
1088 struct internal_reloc
*relend
;
1091 asection
*toc_section
= 0;
1093 reloc_howto_type
*howto
= 0;
1095 /* If we are performing a relocateable link, we don't need to do a
1096 thing. The caller will take care of adjusting the reloc
1097 addresses and symbol indices. */
1098 if (info
->relocateable
)
1105 relend
= rel
+ input_section
->reloc_count
;
1106 for (; rel
< relend
; rel
++)
1109 struct ppc_coff_link_hash_entry
*h
;
1110 struct internal_syment
*sym
;
1114 bfd_reloc_status_type rstat
;
1117 unsigned short r_type
= EXTRACT_TYPE (rel
->r_type
);
1118 unsigned short r_flags
= EXTRACT_FLAGS(rel
->r_type
);
1120 symndx
= rel
->r_symndx
;
1121 loc
= contents
+ rel
->r_vaddr
- input_section
->vma
;
1123 /* FIXME: check bounds on r_type */
1124 howto
= ppc_coff_howto_table
+ r_type
;
1133 h
= (struct ppc_coff_link_hash_entry
*)
1134 (obj_coff_sym_hashes (input_bfd
)[symndx
]);
1140 sym
= syms
+ symndx
;
1143 if (r_type
== IMAGE_REL_PPC_IMGLUE
&& h
== 0)
1145 /* An IMGLUE reloc must have a name. Something is very wrong. */
1152 /* FIXME: PAIR unsupported in the following code */
1156 sec
= bfd_abs_section_ptr
;
1159 sec
= sections
[symndx
];
1160 val
= (sec
->output_section
->vma
1161 + sec
->output_offset
1163 if (! obj_pe (output_bfd
))
1171 if (h
->root
.root
.type
== bfd_link_hash_defined
1172 || h
->root
.root
.type
== bfd_link_hash_defweak
)
1174 sec
= h
->root
.root
.u
.def
.section
;
1175 val
= (h
->root
.root
.u
.def
.value
1176 + sec
->output_section
->vma
1177 + sec
->output_offset
);
1181 if (! ((*info
->callbacks
->undefined_symbol
)
1182 (info
, h
->root
.root
.root
.string
, input_bfd
, input_section
,
1183 rel
->r_vaddr
- input_section
->vma
, true)))
1188 rstat
= bfd_reloc_ok
;
1190 /* Each case must do its own relocation, setting rstat appropriately */
1194 (*_bfd_error_handler
)
1195 (_("%s: unsupported relocation type 0x%02x"),
1196 bfd_get_filename (input_bfd
), r_type
);
1197 bfd_set_error (bfd_error_bad_value
);
1199 case IMAGE_REL_PPC_TOCREL16
:
1201 bfd_vma our_toc_offset
;
1204 DUMP_RELOC2(howto
->name
, rel
);
1206 if (toc_section
== 0)
1208 toc_section
= bfd_get_section_by_name (bfd_of_toc_owner
,
1211 if ( toc_section
== NULL
)
1213 /* There is no toc section. Something is very wrong. */
1219 * Amazing bit tricks present. As we may have seen earlier, we
1220 * use the 1 bit to tell us whether or not a toc offset has been
1221 * allocated. Now that they've all been allocated, we will use
1222 * the 1 bit to tell us if we've written this particular toc
1227 { /* it is a file local symbol */
1228 int *local_toc_table
;
1231 sym
= syms
+ symndx
;
1232 name
= sym
->_n
._n_name
;
1234 local_toc_table
= obj_coff_local_toc_table(input_bfd
);
1235 our_toc_offset
= local_toc_table
[symndx
];
1237 if (IS_WRITTEN(our_toc_offset
))
1239 /* if it has been written out, it is marked with the
1240 1 bit. Fix up our offset, but do not write it out
1243 MAKE_ADDR_AGAIN(our_toc_offset
);
1247 /* write out the toc entry */
1248 record_toc(toc_section
,
1253 bfd_put_32 (output_bfd
,
1255 toc_section
->contents
+ our_toc_offset
);
1257 MARK_AS_WRITTEN(local_toc_table
[symndx
]);
1263 const char *name
= h
->root
.root
.root
.string
;
1264 our_toc_offset
= h
->toc_offset
;
1266 if ((r_flags
& IMAGE_REL_PPC_TOCDEFN
)
1267 == IMAGE_REL_PPC_TOCDEFN
)
1269 /* This is unbelievable cheese. Some knowledgable asm
1270 hacker has decided to use r2 as a base for loading
1271 a value. He/She does this by setting the tocdefn bit,
1272 and not supplying a toc definition. The behaviour is
1273 then to use the difference between the value of the
1274 symbol and the actual location of the toc as the toc
1277 In fact, what is usually happening is, because the
1278 Import Address Table is mapped immediately following
1279 the toc, some trippy library code trying for speed on
1280 dll linkage, takes advantage of that and considers
1281 the IAT to be part of the toc, thus saving a load.
1284 our_toc_offset
= val
-
1285 (toc_section
->output_section
->vma
+
1286 toc_section
->output_offset
);
1288 /* The size must still fit in a 16bit displacment */
1289 if (our_toc_offset
>= 65535)
1291 (*_bfd_error_handler
)
1292 (_("%s: Relocation for %s of %x exceeds Toc size limit"),
1293 bfd_get_filename (input_bfd
), name
, our_toc_offset
);
1294 bfd_set_error (bfd_error_bad_value
);
1298 record_toc(toc_section
, our_toc_offset
, pub
, strdup(name
));
1300 else if (IS_WRITTEN(our_toc_offset
))
1302 /* if it has been written out, it is marked with the
1303 1 bit. Fix up our offset, but do not write it out
1306 MAKE_ADDR_AGAIN(our_toc_offset
);
1310 record_toc(toc_section
, our_toc_offset
, pub
, strdup(name
));
1312 /* write out the toc entry */
1313 bfd_put_32 (output_bfd
,
1315 toc_section
->contents
+ our_toc_offset
);
1317 MARK_AS_WRITTEN(h
->toc_offset
);
1318 /* The tricky part is that this is the address that */
1319 /* needs a .reloc entry for it */
1324 if (fixit
&& info
->base_file
)
1326 /* So if this is non pcrelative, and is referenced
1327 to a section or a common symbol, then it needs a reloc */
1329 /* relocation to a symbol in a section which
1330 isn't absolute - we output the address here
1333 bfd_vma addr
= toc_section
->output_section
->vma
1334 + toc_section
->output_offset
+ our_toc_offset
;
1336 if (coff_data(output_bfd
)->pe
)
1337 addr
-= pe_data(output_bfd
)->pe_opthdr
.ImageBase
;
1339 fwrite (&addr
, 1,4, (FILE *) info
->base_file
);
1342 /* FIXME: this test is conservative */
1343 if ( (r_flags
& IMAGE_REL_PPC_TOCDEFN
) != IMAGE_REL_PPC_TOCDEFN
&&
1344 our_toc_offset
> toc_section
->_raw_size
)
1346 (*_bfd_error_handler
)
1347 (_("%s: Relocation exceeds allocated TOC (%x)"),
1348 bfd_get_filename (input_bfd
),
1349 toc_section
->_raw_size
);
1350 bfd_set_error (bfd_error_bad_value
);
1354 /* Now we know the relocation for this toc reference */
1355 relocation
= our_toc_offset
+ TOC_LOAD_ADJUSTMENT
;
1356 rstat
= _bfd_relocate_contents (howto
,
1362 case IMAGE_REL_PPC_IFGLUE
:
1364 /* To solve this, we need to know whether or not the symbol */
1365 /* appearing on the call instruction is a glue function or not. */
1366 /* A glue function must announce itself via a IMGLUE reloc, and */
1367 /* the reloc contains the required toc restore instruction */
1370 const char *my_name
;
1371 DUMP_RELOC2(howto
->name
, rel
);
1375 my_name
= h
->root
.root
.root
.string
;
1376 if (h
->symbol_is_glue
== 1)
1378 x
= bfd_get_32 (input_bfd
, loc
);
1379 bfd_put_32 (input_bfd
, h
->glue_insn
, loc
);
1384 case IMAGE_REL_PPC_SECREL
:
1385 /* Unimplemented: codeview debugging information */
1386 /* For fast access to the header of the section
1387 containing the item. */
1389 case IMAGE_REL_PPC_SECTION
:
1390 /* Unimplemented: codeview debugging information */
1391 /* Is used to indicate that the value should be relative
1392 to the beginning of the section that contains the
1395 case IMAGE_REL_PPC_ABSOLUTE
:
1397 const char *my_name
;
1399 my_name
= (syms
+symndx
)->_n
._n_name
;
1402 my_name
= h
->root
.root
.root
.string
;
1406 _("Warning: unsupported reloc %s <file %s, section %s>\n"),
1408 bfd_get_filename(input_bfd
),
1409 input_section
->name
);
1411 fprintf (stderr
,"sym %ld (%s), r_vaddr %ld (%lx)\n",
1412 rel
->r_symndx
, my_name
, (long) rel
->r_vaddr
,
1413 (unsigned long) rel
->r_vaddr
);
1416 case IMAGE_REL_PPC_IMGLUE
:
1418 /* There is nothing to do now. This reloc was noted in the first
1419 pass over the relocs, and the glue instruction extracted */
1420 const char *my_name
;
1421 if (h
->symbol_is_glue
== 1)
1423 my_name
= h
->root
.root
.root
.string
;
1425 (*_bfd_error_handler
)
1426 (_("%s: Out of order IMGLUE reloc for %s"),
1427 bfd_get_filename (input_bfd
), my_name
);
1428 bfd_set_error (bfd_error_bad_value
);
1432 case IMAGE_REL_PPC_ADDR32NB
:
1434 struct coff_link_hash_entry
*myh
= 0;
1435 const char *name
= 0;
1436 DUMP_RELOC2(howto
->name
, rel
);
1438 if (strncmp(".idata$2",input_section
->name
,8) == 0 && first_thunk_address
== 0)
1440 /* set magic values */
1442 struct coff_link_hash_entry
*myh
= 0;
1443 myh
= coff_link_hash_lookup (coff_hash_table (info
),
1445 false, false, true);
1446 first_thunk_address
= myh
->root
.u
.def
.value
+
1447 sec
->output_section
->vma
+
1448 sec
->output_offset
-
1449 pe_data(output_bfd
)->pe_opthdr
.ImageBase
;
1451 idata5offset
= myh
->root
.u
.def
.value
;
1452 myh
= coff_link_hash_lookup (coff_hash_table (info
),
1454 false, false, true);
1456 thunk_size
= myh
->root
.u
.def
.value
- idata5offset
;
1457 myh
= coff_link_hash_lookup (coff_hash_table (info
),
1459 false, false, true);
1460 import_table_size
= myh
->root
.u
.def
.value
;
1464 { /* it is a file local symbol */
1465 sym
= syms
+ symndx
;
1466 name
= sym
->_n
._n_name
;
1472 name
= h
->root
.root
.root
.string
;
1473 if (strcmp(".idata$2", name
) == 0)
1474 target
= "__idata2_magic__";
1475 else if (strcmp(".idata$4", name
) == 0)
1476 target
= "__idata4_magic__";
1477 else if (strcmp(".idata$5", name
) == 0)
1478 target
= "__idata5_magic__";
1484 myh
= coff_link_hash_lookup (coff_hash_table (info
),
1486 false, false, true);
1489 /* Missing magic cookies. Something is very wrong. */
1493 val
= myh
->root
.u
.def
.value
+
1494 sec
->output_section
->vma
+ sec
->output_offset
;
1495 if (first_thunk_address
== 0)
1498 myh
= coff_link_hash_lookup (coff_hash_table (info
),
1500 false, false, true);
1501 first_thunk_address
= myh
->root
.u
.def
.value
+
1502 sec
->output_section
->vma
+
1503 sec
->output_offset
-
1504 pe_data(output_bfd
)->pe_opthdr
.ImageBase
;
1506 idata5offset
= myh
->root
.u
.def
.value
;
1507 myh
= coff_link_hash_lookup (coff_hash_table (info
),
1509 false, false, true);
1511 thunk_size
= myh
->root
.u
.def
.value
- idata5offset
;
1512 myh
= coff_link_hash_lookup (coff_hash_table (info
),
1514 false, false, true);
1515 import_table_size
= myh
->root
.u
.def
.value
;
1520 rstat
= _bfd_relocate_contents (howto
,
1523 pe_data(output_bfd
)->pe_opthdr
.ImageBase
,
1528 case IMAGE_REL_PPC_REL24
:
1529 DUMP_RELOC2(howto
->name
, rel
);
1530 val
-= (input_section
->output_section
->vma
1531 + input_section
->output_offset
);
1533 rstat
= _bfd_relocate_contents (howto
,
1538 case IMAGE_REL_PPC_ADDR16
:
1539 case IMAGE_REL_PPC_ADDR24
:
1540 case IMAGE_REL_PPC_ADDR32
:
1541 DUMP_RELOC2(howto
->name
, rel
);
1542 rstat
= _bfd_relocate_contents (howto
,
1549 if ( info
->base_file
)
1551 /* So if this is non pcrelative, and is referenced
1552 to a section or a common symbol, then it needs a reloc */
1553 if (sym
&& pe_data(output_bfd
)->in_reloc_p(output_bfd
, howto
))
1555 /* relocation to a symbol in a section which
1556 isn't absolute - we output the address here
1558 bfd_vma addr
= rel
->r_vaddr
1559 - input_section
->vma
1560 + input_section
->output_offset
1561 + input_section
->output_section
->vma
;
1563 if (coff_data(output_bfd
)->pe
)
1565 addr
-= pe_data(output_bfd
)->pe_opthdr
.ImageBase
;
1567 fwrite (&addr
, 1,4, (FILE *) info
->base_file
);
1577 case bfd_reloc_overflow
:
1580 char buf
[SYMNMLEN
+ 1];
1585 name
= h
->root
.root
.root
.string
;
1586 else if (sym
== NULL
)
1588 else if (sym
->_n
._n_n
._n_zeroes
== 0
1589 && sym
->_n
._n_n
._n_offset
!= 0)
1590 name
= obj_coff_strings (input_bfd
) + sym
->_n
._n_n
._n_offset
;
1593 strncpy (buf
, sym
->_n
._n_name
, SYMNMLEN
);
1594 buf
[SYMNMLEN
] = '\0';
1598 if (! ((*info
->callbacks
->reloc_overflow
)
1599 (info
, name
, howto
->name
,
1600 (bfd_vma
) 0, input_bfd
,
1601 input_section
, rel
->r_vaddr
- input_section
->vma
)))
1613 #ifdef COFF_IMAGE_WITH_PE
1615 /* FIXME: BFD should not use global variables. This file is compiled
1616 twice, and these variables are shared. This is confusing and
1619 long int global_toc_size
= 4;
1621 bfd
* bfd_of_toc_owner
= 0;
1623 long int import_table_size
;
1624 long int first_thunk_address
;
1625 long int thunk_size
;
1627 struct list_ele
*head
;
1628 struct list_ele
*tail
;
1631 h1
= N_("\n\t\t\tTOC MAPPING\n\n");
1633 h2
= N_(" TOC disassembly Comments Name\n");
1635 h3
= N_(" Offset spelling (if present)\n");
1641 FILE *file
= (FILE *) vfile
;
1644 fprintf (file
, _(h1
));
1645 fprintf (file
, _(h2
));
1646 fprintf (file
, _(h3
));
1648 for (t
= head
; t
!= 0; t
=t
->next
)
1650 const char *cat
= "";
1653 cat
= _("private ");
1654 else if (t
->cat
== pub
)
1656 else if (t
->cat
== data
)
1657 cat
= _("data-in-toc ");
1659 if (t
->offset
> global_toc_size
)
1661 if (t
->offset
<= global_toc_size
+ thunk_size
)
1662 cat
= _("IAT reference ");
1666 _("**** global_toc_size %ld(%lx), thunk_size %ld(%lx)\n"),
1667 global_toc_size
, global_toc_size
, thunk_size
, thunk_size
);
1668 cat
= _("Out of bounds!");
1673 " %04lx (%d)", (unsigned long) t
->offset
, t
->offset
- 32768);
1680 fprintf (file
, "\n");
1684 ppc_allocate_toc_section (info
)
1685 struct bfd_link_info
*info ATTRIBUTE_UNUSED
;
1689 static char test_char
= '1';
1691 if ( global_toc_size
== 0 ) /* FIXME: does this get me in trouble? */
1694 if (bfd_of_toc_owner
== 0)
1696 /* No toc owner? Something is very wrong. */
1700 s
= bfd_get_section_by_name ( bfd_of_toc_owner
, TOC_SECTION_NAME
);
1703 /* No toc section? Something is very wrong. */
1707 foo
= (bfd_byte
*) bfd_alloc(bfd_of_toc_owner
, global_toc_size
);
1708 memset(foo
, test_char
, global_toc_size
);
1710 s
->_raw_size
= s
->_cooked_size
= global_toc_size
;
1717 ppc_process_before_allocation (abfd
, info
)
1719 struct bfd_link_info
*info
;
1722 struct internal_reloc
*i
, *rel
;
1724 /* here we have a bfd that is to be included on the link. We have a hook
1725 to do reloc rummaging, before section sizes are nailed down. */
1727 _bfd_coff_get_external_symbols(abfd
);
1729 /* rummage around all the relocs and map the toc */
1730 sec
= abfd
->sections
;
1737 for (; sec
!= 0; sec
= sec
->next
)
1739 if (sec
->reloc_count
== 0)
1742 /* load the relocs */
1743 /* FIXME: there may be a storage leak here */
1744 i
=_bfd_coff_read_internal_relocs(abfd
,sec
,1,0,0,0);
1749 for (rel
=i
;rel
<i
+sec
->reloc_count
;++rel
)
1751 unsigned short r_type
= EXTRACT_TYPE (rel
->r_type
);
1752 unsigned short r_flags
= EXTRACT_FLAGS(rel
->r_type
);
1755 DUMP_RELOC2(ppc_coff_howto_table
[r_type
].name
, rel
);
1759 case IMAGE_REL_PPC_TOCREL16
:
1760 /* if TOCDEFN is on, ignore as someone else has allocated the
1762 if ( (r_flags
& IMAGE_REL_PPC_TOCDEFN
) != IMAGE_REL_PPC_TOCDEFN
)
1763 ok
= ppc_record_toc_entry(abfd
, info
, sec
,
1764 rel
->r_symndx
, default_toc
);
1768 case IMAGE_REL_PPC_IMGLUE
:
1769 ppc_mark_symbol_as_glue(abfd
, rel
->r_symndx
, rel
);
1782 static bfd_reloc_status_type
1783 ppc_refhi_reloc (abfd
,
1790 bfd
*abfd ATTRIBUTE_UNUSED
;
1791 arelent
*reloc_entry ATTRIBUTE_UNUSED
;
1792 asymbol
*symbol ATTRIBUTE_UNUSED
;
1793 PTR data ATTRIBUTE_UNUSED
;
1794 asection
*input_section ATTRIBUTE_UNUSED
;
1796 char **error_message ATTRIBUTE_UNUSED
;
1799 DUMP_RELOC("REFHI",reloc_entry
);
1801 if (output_bfd
== (bfd
*) NULL
)
1802 return bfd_reloc_continue
;
1804 return bfd_reloc_undefined
;
1809 static bfd_reloc_status_type
1810 ppc_reflo_reloc (abfd
,
1818 arelent
*reloc_entry
;
1821 asection
*input_section
;
1823 char **error_message
;
1826 DUMP_RELOC("REFLO",reloc_entry
);
1828 if (output_bfd
== (bfd
*) NULL
)
1829 return bfd_reloc_continue
;
1831 return bfd_reloc_undefined
;
1836 static bfd_reloc_status_type
1837 ppc_pair_reloc (abfd
,
1844 bfd
*abfd ATTRIBUTE_UNUSED
;
1845 arelent
*reloc_entry ATTRIBUTE_UNUSED
;
1846 asymbol
*symbol ATTRIBUTE_UNUSED
;
1847 PTR data ATTRIBUTE_UNUSED
;
1848 asection
*input_section ATTRIBUTE_UNUSED
;
1850 char **error_message ATTRIBUTE_UNUSED
;
1853 DUMP_RELOC("PAIR",reloc_entry
);
1855 if (output_bfd
== (bfd
*) NULL
)
1856 return bfd_reloc_continue
;
1858 return bfd_reloc_undefined
;
1861 static bfd_reloc_status_type
1862 ppc_toc16_reloc (abfd
,
1869 bfd
*abfd ATTRIBUTE_UNUSED
;
1870 arelent
*reloc_entry ATTRIBUTE_UNUSED
;
1871 asymbol
*symbol ATTRIBUTE_UNUSED
;
1872 PTR data ATTRIBUTE_UNUSED
;
1873 asection
*input_section ATTRIBUTE_UNUSED
;
1875 char **error_message ATTRIBUTE_UNUSED
;
1877 UN_IMPL("TOCREL16");
1878 DUMP_RELOC("TOCREL16",reloc_entry
);
1880 if (output_bfd
== (bfd
*) NULL
)
1882 return bfd_reloc_continue
;
1885 return bfd_reloc_ok
;
1890 /* ADDR32NB : 32 bit address relative to the virtual origin. */
1891 /* (On the alpha, this is always a linker generated thunk)*/
1892 /* (i.e. 32bit addr relative to the image base) */
1896 static bfd_reloc_status_type
1897 ppc_addr32nb_reloc (abfd
,
1905 arelent
*reloc_entry
;
1908 asection
*input_section
;
1910 char **error_message
;
1912 UN_IMPL("ADDR32NB");
1913 DUMP_RELOC("ADDR32NB",reloc_entry
);
1915 return bfd_reloc_ok
;
1920 static bfd_reloc_status_type
1921 ppc_secrel_reloc (abfd
,
1928 bfd
*abfd ATTRIBUTE_UNUSED
;
1929 arelent
*reloc_entry ATTRIBUTE_UNUSED
;
1930 asymbol
*symbol ATTRIBUTE_UNUSED
;
1931 PTR data ATTRIBUTE_UNUSED
;
1932 asection
*input_section ATTRIBUTE_UNUSED
;
1934 char **error_message ATTRIBUTE_UNUSED
;
1937 DUMP_RELOC("SECREL",reloc_entry
);
1939 if (output_bfd
== (bfd
*) NULL
)
1940 return bfd_reloc_continue
;
1942 return bfd_reloc_ok
;
1945 static bfd_reloc_status_type
1946 ppc_section_reloc (abfd
,
1953 bfd
*abfd ATTRIBUTE_UNUSED
;
1954 arelent
*reloc_entry ATTRIBUTE_UNUSED
;
1955 asymbol
*symbol ATTRIBUTE_UNUSED
;
1956 PTR data ATTRIBUTE_UNUSED
;
1957 asection
*input_section ATTRIBUTE_UNUSED
;
1959 char **error_message ATTRIBUTE_UNUSED
;
1962 DUMP_RELOC("SECTION",reloc_entry
);
1964 if (output_bfd
== (bfd
*) NULL
)
1965 return bfd_reloc_continue
;
1967 return bfd_reloc_ok
;
1970 static bfd_reloc_status_type
1971 ppc_imglue_reloc (abfd
,
1978 bfd
*abfd ATTRIBUTE_UNUSED
;
1979 arelent
*reloc_entry ATTRIBUTE_UNUSED
;
1980 asymbol
*symbol ATTRIBUTE_UNUSED
;
1981 PTR data ATTRIBUTE_UNUSED
;
1982 asection
*input_section ATTRIBUTE_UNUSED
;
1984 char **error_message ATTRIBUTE_UNUSED
;
1987 DUMP_RELOC("IMGLUE",reloc_entry
);
1989 if (output_bfd
== (bfd
*) NULL
)
1990 return bfd_reloc_continue
;
1992 return bfd_reloc_ok
;
1995 #define MAX_RELOC_INDEX \
1996 (sizeof (ppc_coff_howto_table) / sizeof (ppc_coff_howto_table[0]) - 1)
1998 /* FIXME: There is a possiblity that when we read in a reloc from a file,
1999 that there are some bits encoded in the upper portion of the
2000 type field. Not yet implemented.
2002 static void ppc_coff_rtype2howto
PARAMS ((arelent
*relent
,
2003 struct internal_reloc
*internal
));
2006 ppc_coff_rtype2howto (relent
, internal
)
2008 struct internal_reloc
*internal
;
2011 /* We can encode one of three things in the type field, aside from the
2013 1. IMAGE_REL_PPC_NEG - indicates the value field is a subtraction
2014 value, rather than an addition value
2015 2. IMAGE_REL_PPC_BRTAKEN, IMAGE_REL_PPC_BRNTAKEN - indicates that
2016 the branch is expected to be taken or not.
2017 3. IMAGE_REL_PPC_TOCDEFN - toc slot definition in the file
2018 For now, we just strip this stuff to find the type, and ignore it other
2021 reloc_howto_type
*howto
;
2022 unsigned short r_type
= EXTRACT_TYPE (internal
->r_type
);
2023 unsigned short r_flags
= EXTRACT_FLAGS(internal
->r_type
);
2024 unsigned short junk
= EXTRACT_JUNK (internal
->r_type
);
2026 /* the masking process only slices off the bottom byte for r_type. */
2027 if ( r_type
> MAX_RELOC_INDEX
)
2030 /* check for absolute crap */
2036 case IMAGE_REL_PPC_ADDR16
:
2037 case IMAGE_REL_PPC_REL24
:
2038 case IMAGE_REL_PPC_ADDR24
:
2039 case IMAGE_REL_PPC_ADDR32
:
2040 case IMAGE_REL_PPC_IFGLUE
:
2041 case IMAGE_REL_PPC_ADDR32NB
:
2042 case IMAGE_REL_PPC_SECTION
:
2043 case IMAGE_REL_PPC_SECREL
:
2044 DUMP_RELOC2(ppc_coff_howto_table
[r_type
].name
, internal
);
2045 howto
= ppc_coff_howto_table
+ r_type
;
2047 case IMAGE_REL_PPC_IMGLUE
:
2048 DUMP_RELOC2(ppc_coff_howto_table
[r_type
].name
, internal
);
2049 howto
= ppc_coff_howto_table
+ r_type
;
2051 case IMAGE_REL_PPC_TOCREL16
:
2052 DUMP_RELOC2(ppc_coff_howto_table
[r_type
].name
, internal
);
2053 if (r_flags
& IMAGE_REL_PPC_TOCDEFN
)
2054 howto
= ppc_coff_howto_table
+ IMAGE_REL_PPC_TOCREL16_DEFN
;
2056 howto
= ppc_coff_howto_table
+ IMAGE_REL_PPC_TOCREL16
;
2060 _("Warning: Unsupported reloc %s [%d] used -- it may not work.\n"),
2061 ppc_coff_howto_table
[r_type
].name
,
2063 howto
= ppc_coff_howto_table
+ r_type
;
2067 relent
->howto
= howto
;
2071 static reloc_howto_type
*
2072 coff_ppc_rtype_to_howto (abfd
, sec
, rel
, h
, sym
, addendp
)
2073 bfd
*abfd ATTRIBUTE_UNUSED
;
2075 struct internal_reloc
*rel
;
2076 struct coff_link_hash_entry
*h ATTRIBUTE_UNUSED
;
2077 struct internal_syment
*sym ATTRIBUTE_UNUSED
;
2080 reloc_howto_type
*howto
;
2082 /* We can encode one of three things in the type field, aside from the
2084 1. IMAGE_REL_PPC_NEG - indicates the value field is a subtraction
2085 value, rather than an addition value
2086 2. IMAGE_REL_PPC_BRTAKEN, IMAGE_REL_PPC_BRNTAKEN - indicates that
2087 the branch is expected to be taken or not.
2088 3. IMAGE_REL_PPC_TOCDEFN - toc slot definition in the file
2089 For now, we just strip this stuff to find the type, and ignore it other
2093 unsigned short r_type
= EXTRACT_TYPE (rel
->r_type
);
2094 unsigned short r_flags
= EXTRACT_FLAGS(rel
->r_type
);
2095 unsigned short junk
= EXTRACT_JUNK (rel
->r_type
);
2097 /* the masking process only slices off the bottom byte for r_type. */
2098 if ( r_type
> MAX_RELOC_INDEX
)
2101 /* check for absolute crap */
2107 case IMAGE_REL_PPC_ADDR32NB
:
2108 DUMP_RELOC2(ppc_coff_howto_table
[r_type
].name
, rel
);
2109 *addendp
-= pe_data(sec
->output_section
->owner
)->pe_opthdr
.ImageBase
;
2110 howto
= ppc_coff_howto_table
+ r_type
;
2112 case IMAGE_REL_PPC_TOCREL16
:
2113 DUMP_RELOC2(ppc_coff_howto_table
[r_type
].name
, rel
);
2114 if (r_flags
& IMAGE_REL_PPC_TOCDEFN
)
2115 howto
= ppc_coff_howto_table
+ IMAGE_REL_PPC_TOCREL16_DEFN
;
2117 howto
= ppc_coff_howto_table
+ IMAGE_REL_PPC_TOCREL16
;
2119 case IMAGE_REL_PPC_ADDR16
:
2120 case IMAGE_REL_PPC_REL24
:
2121 case IMAGE_REL_PPC_ADDR24
:
2122 case IMAGE_REL_PPC_ADDR32
:
2123 case IMAGE_REL_PPC_IFGLUE
:
2124 case IMAGE_REL_PPC_SECTION
:
2125 case IMAGE_REL_PPC_SECREL
:
2126 DUMP_RELOC2(ppc_coff_howto_table
[r_type
].name
, rel
);
2127 howto
= ppc_coff_howto_table
+ r_type
;
2129 case IMAGE_REL_PPC_IMGLUE
:
2130 DUMP_RELOC2(ppc_coff_howto_table
[r_type
].name
, rel
);
2131 howto
= ppc_coff_howto_table
+ r_type
;
2135 _("Warning: Unsupported reloc %s [%d] used -- it may not work.\n"),
2136 ppc_coff_howto_table
[r_type
].name
,
2138 howto
= ppc_coff_howto_table
+ r_type
;
2145 /* a cheesy little macro to make the code a little more readable */
2146 #define HOW2MAP(bfd_rtype,ppc_rtype) \
2147 case bfd_rtype: return &ppc_coff_howto_table[ppc_rtype]
2149 static reloc_howto_type
*ppc_coff_reloc_type_lookup
2150 PARAMS ((bfd
*, bfd_reloc_code_real_type
));
2152 static reloc_howto_type
*
2153 ppc_coff_reloc_type_lookup (abfd
, code
)
2154 bfd
*abfd ATTRIBUTE_UNUSED
;
2155 bfd_reloc_code_real_type code
;
2159 HOW2MAP(BFD_RELOC_32_GOTOFF
, IMAGE_REL_PPC_IMGLUE
);
2160 HOW2MAP(BFD_RELOC_16_GOT_PCREL
, IMAGE_REL_PPC_IFGLUE
);
2161 HOW2MAP(BFD_RELOC_16
, IMAGE_REL_PPC_ADDR16
);
2162 HOW2MAP(BFD_RELOC_PPC_B26
, IMAGE_REL_PPC_REL24
);
2163 HOW2MAP(BFD_RELOC_PPC_BA26
, IMAGE_REL_PPC_ADDR24
);
2164 HOW2MAP(BFD_RELOC_PPC_TOC16
, IMAGE_REL_PPC_TOCREL16
);
2165 HOW2MAP(BFD_RELOC_16_GOTOFF
, IMAGE_REL_PPC_TOCREL16_DEFN
);
2166 HOW2MAP(BFD_RELOC_32
, IMAGE_REL_PPC_ADDR32
);
2167 HOW2MAP(BFD_RELOC_RVA
, IMAGE_REL_PPC_ADDR32NB
);
2176 /* Tailor coffcode.h -- macro heaven. */
2178 #define RTYPE2HOWTO(cache_ptr, dst) ppc_coff_rtype2howto (cache_ptr, dst)
2180 #ifndef COFF_IMAGE_WITH_PE
2181 static void ppc_coff_swap_sym_in_hook
PARAMS ((bfd
*, PTR
, PTR
));
2184 /* We use the special COFF backend linker, with our own special touch. */
2186 #define coff_bfd_reloc_type_lookup ppc_coff_reloc_type_lookup
2187 #define coff_rtype_to_howto coff_ppc_rtype_to_howto
2188 #define coff_relocate_section coff_ppc_relocate_section
2189 #define coff_bfd_final_link ppc_bfd_coff_final_link
2191 #ifndef COFF_IMAGE_WITH_PE
2192 /* FIXME: This no longer works. */
2193 #define coff_swap_sym_in_hook ppc_coff_swap_sym_in_hook
2196 #define SELECT_RELOC(internal, howto) {internal.r_type=howto->type;}
2198 #define COFF_PAGE_SIZE 0x1000
2200 /* FIXME: This controls some code that used to be in peicode.h and is
2201 now in peigen.c. It will not control the code in peigen.c. If
2202 anybody wants to get this working, you will need to fix that. */
2203 #define POWERPC_LE_PE
2205 #define COFF_SECTION_ALIGNMENT_ENTRIES \
2206 { COFF_SECTION_NAME_EXACT_MATCH (".idata$2"), \
2207 COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 0 }, \
2208 { COFF_SECTION_NAME_EXACT_MATCH (".idata$3"), \
2209 COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 0 }, \
2210 { COFF_SECTION_NAME_EXACT_MATCH (".idata$4"), \
2211 COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 2 }, \
2212 { COFF_SECTION_NAME_EXACT_MATCH (".idata$5"), \
2213 COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 2 }, \
2214 { COFF_SECTION_NAME_EXACT_MATCH (".idata$6"), \
2215 COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 1 }, \
2216 { COFF_SECTION_NAME_EXACT_MATCH (".reloc"), \
2217 COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 1 }
2219 #include "coffcode.h"
2221 #ifndef COFF_IMAGE_WITH_PE
2223 What we're trying to do here is allocate a toc section (early), and attach
2224 it to the last bfd to be processed. This avoids the problem of having a toc
2225 written out before all files have been processed. This code allocates
2226 a toc section for every file, and records the last one seen. There are
2227 at least two problems with this approach:
2228 1. We allocate whole bunches of toc sections that are ignored, but at
2229 at least we will not allocate a toc if no .toc is present.
2230 2. It's not clear to me that being the last bfd read necessarily means
2231 that you are the last bfd closed.
2232 3. Doing it on a "swap in" hook depends on when the "swap in" is called,
2233 and how often, etc. It's not clear to me that there isn't a hole here.
2237 ppc_coff_swap_sym_in_hook (abfd
, ext1
, in1
)
2239 PTR ext1 ATTRIBUTE_UNUSED
;
2242 struct internal_syment
*in
= (struct internal_syment
*)in1
;
2244 if (bfd_of_toc_owner
!= 0) /* we already have a toc, so go home */
2247 if (strcmp(in
->_n
._n_name
, ".toc") == 0)
2250 register asection
*s
;
2252 s
= bfd_get_section_by_name ( abfd
, TOC_SECTION_NAME
);
2258 flags
= SEC_ALLOC
| SEC_LOAD
| SEC_HAS_CONTENTS
| SEC_IN_MEMORY
;
2260 s
= bfd_make_section (abfd
, TOC_SECTION_NAME
);
2263 || !bfd_set_section_flags (abfd
, s
, flags
)
2264 || !bfd_set_section_alignment (abfd
, s
, 2))
2266 /* FIXME: set appropriate bfd error */
2270 /* save the bfd for later allocation */
2271 bfd_of_toc_owner
= abfd
;
2278 #ifndef COFF_IMAGE_WITH_PE
2280 static boolean ppc_do_last
PARAMS ((bfd
*));
2281 static bfd
*ppc_get_last
PARAMS ((void));
2287 if (abfd
== bfd_of_toc_owner
)
2296 return bfd_of_toc_owner
;
2299 /* this piece of machinery exists only to guarantee that the bfd that holds
2300 the toc section is written last.
2302 This does depend on bfd_make_section attaching a new section to the
2303 end of the section list for the bfd.
2305 This is otherwise intended to be functionally the same as
2306 cofflink.c:_bfd_coff_final_link(). It is specifically different only
2307 where the POWERPC_LE_PE macro modifies the code. It is left in as a
2308 precise form of comment. krk@cygnus.com
2311 /* Do the final link step. */
2314 ppc_bfd_coff_final_link (abfd
, info
)
2316 struct bfd_link_info
*info
;
2318 bfd_size_type symesz
;
2319 struct coff_final_link_info finfo
;
2320 boolean debug_merge_allocated
;
2322 struct bfd_link_order
*p
;
2323 size_t max_sym_count
;
2324 size_t max_lineno_count
;
2325 size_t max_reloc_count
;
2326 size_t max_output_reloc_count
;
2327 size_t max_contents_size
;
2328 file_ptr rel_filepos
;
2330 file_ptr line_filepos
;
2331 unsigned int linesz
;
2333 bfd_byte
*external_relocs
= NULL
;
2334 char strbuf
[STRING_SIZE_SIZE
];
2336 symesz
= bfd_coff_symesz (abfd
);
2339 finfo
.output_bfd
= abfd
;
2340 finfo
.strtab
= NULL
;
2341 finfo
.section_info
= NULL
;
2342 finfo
.last_file_index
= -1;
2343 finfo
.last_bf_index
= -1;
2344 finfo
.internal_syms
= NULL
;
2345 finfo
.sec_ptrs
= NULL
;
2346 finfo
.sym_indices
= NULL
;
2347 finfo
.outsyms
= NULL
;
2348 finfo
.linenos
= NULL
;
2349 finfo
.contents
= NULL
;
2350 finfo
.external_relocs
= NULL
;
2351 finfo
.internal_relocs
= NULL
;
2352 debug_merge_allocated
= false;
2354 coff_data (abfd
)->link_info
= info
;
2356 finfo
.strtab
= _bfd_stringtab_init ();
2357 if (finfo
.strtab
== NULL
)
2360 if (! coff_debug_merge_hash_table_init (&finfo
.debug_merge
))
2362 debug_merge_allocated
= true;
2364 /* Compute the file positions for all the sections. */
2365 if (! abfd
->output_has_begun
)
2367 if (! bfd_coff_compute_section_file_positions (abfd
))
2371 /* Count the line numbers and relocation entries required for the
2372 output file. Set the file positions for the relocs. */
2373 rel_filepos
= obj_relocbase (abfd
);
2374 relsz
= bfd_coff_relsz (abfd
);
2375 max_contents_size
= 0;
2376 max_lineno_count
= 0;
2377 max_reloc_count
= 0;
2379 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
2382 o
->lineno_count
= 0;
2383 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
2386 if (p
->type
== bfd_indirect_link_order
)
2390 sec
= p
->u
.indirect
.section
;
2392 /* Mark all sections which are to be included in the
2393 link. This will normally be every section. We need
2394 to do this so that we can identify any sections which
2395 the linker has decided to not include. */
2396 sec
->linker_mark
= true;
2398 if (info
->strip
== strip_none
2399 || info
->strip
== strip_some
)
2400 o
->lineno_count
+= sec
->lineno_count
;
2402 if (info
->relocateable
)
2403 o
->reloc_count
+= sec
->reloc_count
;
2405 if (sec
->_raw_size
> max_contents_size
)
2406 max_contents_size
= sec
->_raw_size
;
2407 if (sec
->lineno_count
> max_lineno_count
)
2408 max_lineno_count
= sec
->lineno_count
;
2409 if (sec
->reloc_count
> max_reloc_count
)
2410 max_reloc_count
= sec
->reloc_count
;
2412 else if (info
->relocateable
2413 && (p
->type
== bfd_section_reloc_link_order
2414 || p
->type
== bfd_symbol_reloc_link_order
))
2417 if (o
->reloc_count
== 0)
2421 o
->flags
|= SEC_RELOC
;
2422 o
->rel_filepos
= rel_filepos
;
2423 rel_filepos
+= o
->reloc_count
* relsz
;
2427 /* If doing a relocateable link, allocate space for the pointers we
2429 if (info
->relocateable
)
2433 /* We use section_count + 1, rather than section_count, because
2434 the target_index fields are 1 based. */
2435 finfo
.section_info
=
2436 ((struct coff_link_section_info
*)
2437 bfd_malloc ((abfd
->section_count
+ 1)
2438 * sizeof (struct coff_link_section_info
)));
2439 if (finfo
.section_info
== NULL
)
2441 for (i
= 0; i
<= abfd
->section_count
; i
++)
2443 finfo
.section_info
[i
].relocs
= NULL
;
2444 finfo
.section_info
[i
].rel_hashes
= NULL
;
2448 /* We now know the size of the relocs, so we can determine the file
2449 positions of the line numbers. */
2450 line_filepos
= rel_filepos
;
2451 linesz
= bfd_coff_linesz (abfd
);
2452 max_output_reloc_count
= 0;
2453 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
2455 if (o
->lineno_count
== 0)
2456 o
->line_filepos
= 0;
2459 o
->line_filepos
= line_filepos
;
2460 line_filepos
+= o
->lineno_count
* linesz
;
2463 if (o
->reloc_count
!= 0)
2465 /* We don't know the indices of global symbols until we have
2466 written out all the local symbols. For each section in
2467 the output file, we keep an array of pointers to hash
2468 table entries. Each entry in the array corresponds to a
2469 reloc. When we find a reloc against a global symbol, we
2470 set the corresponding entry in this array so that we can
2471 fix up the symbol index after we have written out all the
2474 Because of this problem, we also keep the relocs in
2475 memory until the end of the link. This wastes memory,
2476 but only when doing a relocateable link, which is not the
2478 BFD_ASSERT (info
->relocateable
);
2479 finfo
.section_info
[o
->target_index
].relocs
=
2480 ((struct internal_reloc
*)
2481 bfd_malloc (o
->reloc_count
* sizeof (struct internal_reloc
)));
2482 finfo
.section_info
[o
->target_index
].rel_hashes
=
2483 ((struct coff_link_hash_entry
**)
2484 bfd_malloc (o
->reloc_count
2485 * sizeof (struct coff_link_hash_entry
*)));
2486 if (finfo
.section_info
[o
->target_index
].relocs
== NULL
2487 || finfo
.section_info
[o
->target_index
].rel_hashes
== NULL
)
2490 if (o
->reloc_count
> max_output_reloc_count
)
2491 max_output_reloc_count
= o
->reloc_count
;
2494 /* Reset the reloc and lineno counts, so that we can use them to
2495 count the number of entries we have output so far. */
2497 o
->lineno_count
= 0;
2500 obj_sym_filepos (abfd
) = line_filepos
;
2502 /* Figure out the largest number of symbols in an input BFD. Take
2503 the opportunity to clear the output_has_begun fields of all the
2506 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
2510 sub
->output_has_begun
= false;
2511 sz
= obj_raw_syment_count (sub
);
2512 if (sz
> max_sym_count
)
2516 /* Allocate some buffers used while linking. */
2517 finfo
.internal_syms
= ((struct internal_syment
*)
2518 bfd_malloc (max_sym_count
2519 * sizeof (struct internal_syment
)));
2520 finfo
.sec_ptrs
= (asection
**) bfd_malloc (max_sym_count
2521 * sizeof (asection
*));
2522 finfo
.sym_indices
= (long *) bfd_malloc (max_sym_count
* sizeof (long));
2523 finfo
.outsyms
= ((bfd_byte
*)
2524 bfd_malloc ((size_t) ((max_sym_count
+ 1) * symesz
)));
2525 finfo
.linenos
= (bfd_byte
*) bfd_malloc (max_lineno_count
2526 * bfd_coff_linesz (abfd
));
2527 finfo
.contents
= (bfd_byte
*) bfd_malloc (max_contents_size
);
2528 finfo
.external_relocs
= (bfd_byte
*) bfd_malloc (max_reloc_count
* relsz
);
2529 if (! info
->relocateable
)
2530 finfo
.internal_relocs
= ((struct internal_reloc
*)
2531 bfd_malloc (max_reloc_count
2532 * sizeof (struct internal_reloc
)));
2533 if ((finfo
.internal_syms
== NULL
&& max_sym_count
> 0)
2534 || (finfo
.sec_ptrs
== NULL
&& max_sym_count
> 0)
2535 || (finfo
.sym_indices
== NULL
&& max_sym_count
> 0)
2536 || finfo
.outsyms
== NULL
2537 || (finfo
.linenos
== NULL
&& max_lineno_count
> 0)
2538 || (finfo
.contents
== NULL
&& max_contents_size
> 0)
2539 || (finfo
.external_relocs
== NULL
&& max_reloc_count
> 0)
2540 || (! info
->relocateable
2541 && finfo
.internal_relocs
== NULL
2542 && max_reloc_count
> 0))
2545 /* We now know the position of everything in the file, except that
2546 we don't know the size of the symbol table and therefore we don't
2547 know where the string table starts. We just build the string
2548 table in memory as we go along. We process all the relocations
2549 for a single input file at once. */
2550 obj_raw_syment_count (abfd
) = 0;
2552 if (coff_backend_info (abfd
)->_bfd_coff_start_final_link
)
2554 if (! bfd_coff_start_final_link (abfd
, info
))
2558 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
2560 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
2562 if (p
->type
== bfd_indirect_link_order
2563 && (bfd_get_flavour (p
->u
.indirect
.section
->owner
)
2564 == bfd_target_coff_flavour
))
2566 sub
= p
->u
.indirect
.section
->owner
;
2567 #ifdef POWERPC_LE_PE
2568 if (! sub
->output_has_begun
&& !ppc_do_last(sub
))
2570 if (! sub
->output_has_begun
)
2573 if (! _bfd_coff_link_input_bfd (&finfo
, sub
))
2575 sub
->output_has_begun
= true;
2578 else if (p
->type
== bfd_section_reloc_link_order
2579 || p
->type
== bfd_symbol_reloc_link_order
)
2581 if (! _bfd_coff_reloc_link_order (abfd
, &finfo
, o
, p
))
2586 if (! _bfd_default_link_order (abfd
, info
, o
, p
))
2592 #ifdef POWERPC_LE_PE
2594 bfd
* last_one
= ppc_get_last();
2597 if (! _bfd_coff_link_input_bfd (&finfo
, last_one
))
2600 last_one
->output_has_begun
= true;
2604 /* Free up the buffers used by _bfd_coff_link_input_bfd. */
2606 coff_debug_merge_hash_table_free (&finfo
.debug_merge
);
2607 debug_merge_allocated
= false;
2609 if (finfo
.internal_syms
!= NULL
)
2611 free (finfo
.internal_syms
);
2612 finfo
.internal_syms
= NULL
;
2614 if (finfo
.sec_ptrs
!= NULL
)
2616 free (finfo
.sec_ptrs
);
2617 finfo
.sec_ptrs
= NULL
;
2619 if (finfo
.sym_indices
!= NULL
)
2621 free (finfo
.sym_indices
);
2622 finfo
.sym_indices
= NULL
;
2624 if (finfo
.linenos
!= NULL
)
2626 free (finfo
.linenos
);
2627 finfo
.linenos
= NULL
;
2629 if (finfo
.contents
!= NULL
)
2631 free (finfo
.contents
);
2632 finfo
.contents
= NULL
;
2634 if (finfo
.external_relocs
!= NULL
)
2636 free (finfo
.external_relocs
);
2637 finfo
.external_relocs
= NULL
;
2639 if (finfo
.internal_relocs
!= NULL
)
2641 free (finfo
.internal_relocs
);
2642 finfo
.internal_relocs
= NULL
;
2645 /* The value of the last C_FILE symbol is supposed to be the symbol
2646 index of the first external symbol. Write it out again if
2648 if (finfo
.last_file_index
!= -1
2649 && (unsigned int) finfo
.last_file
.n_value
!= obj_raw_syment_count (abfd
))
2651 finfo
.last_file
.n_value
= obj_raw_syment_count (abfd
);
2652 bfd_coff_swap_sym_out (abfd
, (PTR
) &finfo
.last_file
,
2653 (PTR
) finfo
.outsyms
);
2655 (obj_sym_filepos (abfd
)
2656 + finfo
.last_file_index
* symesz
),
2658 || bfd_write (finfo
.outsyms
, symesz
, 1, abfd
) != symesz
)
2662 /* Write out the global symbols. */
2663 finfo
.failed
= false;
2664 coff_link_hash_traverse (coff_hash_table (info
), _bfd_coff_write_global_sym
,
2669 /* The outsyms buffer is used by _bfd_coff_write_global_sym. */
2670 if (finfo
.outsyms
!= NULL
)
2672 free (finfo
.outsyms
);
2673 finfo
.outsyms
= NULL
;
2676 if (info
->relocateable
)
2678 /* Now that we have written out all the global symbols, we know
2679 the symbol indices to use for relocs against them, and we can
2680 finally write out the relocs. */
2681 external_relocs
= ((bfd_byte
*)
2682 bfd_malloc (max_output_reloc_count
* relsz
));
2683 if (external_relocs
== NULL
)
2686 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
2688 struct internal_reloc
*irel
;
2689 struct internal_reloc
*irelend
;
2690 struct coff_link_hash_entry
**rel_hash
;
2693 if (o
->reloc_count
== 0)
2696 irel
= finfo
.section_info
[o
->target_index
].relocs
;
2697 irelend
= irel
+ o
->reloc_count
;
2698 rel_hash
= finfo
.section_info
[o
->target_index
].rel_hashes
;
2699 erel
= external_relocs
;
2700 for (; irel
< irelend
; irel
++, rel_hash
++, erel
+= relsz
)
2702 if (*rel_hash
!= NULL
)
2704 BFD_ASSERT ((*rel_hash
)->indx
>= 0);
2705 irel
->r_symndx
= (*rel_hash
)->indx
;
2707 bfd_coff_swap_reloc_out (abfd
, (PTR
) irel
, (PTR
) erel
);
2710 if (bfd_seek (abfd
, o
->rel_filepos
, SEEK_SET
) != 0
2711 || bfd_write ((PTR
) external_relocs
, relsz
, o
->reloc_count
,
2712 abfd
) != relsz
* o
->reloc_count
)
2716 free (external_relocs
);
2717 external_relocs
= NULL
;
2720 /* Free up the section information. */
2721 if (finfo
.section_info
!= NULL
)
2725 for (i
= 0; i
< abfd
->section_count
; i
++)
2727 if (finfo
.section_info
[i
].relocs
!= NULL
)
2728 free (finfo
.section_info
[i
].relocs
);
2729 if (finfo
.section_info
[i
].rel_hashes
!= NULL
)
2730 free (finfo
.section_info
[i
].rel_hashes
);
2732 free (finfo
.section_info
);
2733 finfo
.section_info
= NULL
;
2736 /* If we have optimized stabs strings, output them. */
2737 if (coff_hash_table (info
)->stab_info
!= NULL
)
2739 if (! _bfd_write_stab_strings (abfd
, &coff_hash_table (info
)->stab_info
))
2743 /* Write out the string table. */
2744 if (obj_raw_syment_count (abfd
) != 0)
2747 (obj_sym_filepos (abfd
)
2748 + obj_raw_syment_count (abfd
) * symesz
),
2752 #if STRING_SIZE_SIZE == 4
2754 _bfd_stringtab_size (finfo
.strtab
) + STRING_SIZE_SIZE
,
2755 (bfd_byte
*) strbuf
);
2757 #error Change bfd_h_put_32
2760 if (bfd_write (strbuf
, 1, STRING_SIZE_SIZE
, abfd
) != STRING_SIZE_SIZE
)
2763 if (! _bfd_stringtab_emit (abfd
, finfo
.strtab
))
2767 _bfd_stringtab_free (finfo
.strtab
);
2769 /* Setting bfd_get_symcount to 0 will cause write_object_contents to
2770 not try to write out the symbols. */
2771 bfd_get_symcount (abfd
) = 0;
2776 if (debug_merge_allocated
)
2777 coff_debug_merge_hash_table_free (&finfo
.debug_merge
);
2778 if (finfo
.strtab
!= NULL
)
2779 _bfd_stringtab_free (finfo
.strtab
);
2780 if (finfo
.section_info
!= NULL
)
2784 for (i
= 0; i
< abfd
->section_count
; i
++)
2786 if (finfo
.section_info
[i
].relocs
!= NULL
)
2787 free (finfo
.section_info
[i
].relocs
);
2788 if (finfo
.section_info
[i
].rel_hashes
!= NULL
)
2789 free (finfo
.section_info
[i
].rel_hashes
);
2791 free (finfo
.section_info
);
2793 if (finfo
.internal_syms
!= NULL
)
2794 free (finfo
.internal_syms
);
2795 if (finfo
.sec_ptrs
!= NULL
)
2796 free (finfo
.sec_ptrs
);
2797 if (finfo
.sym_indices
!= NULL
)
2798 free (finfo
.sym_indices
);
2799 if (finfo
.outsyms
!= NULL
)
2800 free (finfo
.outsyms
);
2801 if (finfo
.linenos
!= NULL
)
2802 free (finfo
.linenos
);
2803 if (finfo
.contents
!= NULL
)
2804 free (finfo
.contents
);
2805 if (finfo
.external_relocs
!= NULL
)
2806 free (finfo
.external_relocs
);
2807 if (finfo
.internal_relocs
!= NULL
)
2808 free (finfo
.internal_relocs
);
2809 if (external_relocs
!= NULL
)
2810 free (external_relocs
);
2815 /* Forward declaration for use by alternative_target field. */
2816 #ifdef TARGET_BIG_SYM
2817 extern const bfd_target TARGET_BIG_SYM
;
2820 /* The transfer vectors that lead the outside world to all of the above. */
2822 #ifdef TARGET_LITTLE_SYM
2823 const bfd_target TARGET_LITTLE_SYM
=
2825 TARGET_LITTLE_NAME
, /* name or coff-arm-little */
2826 bfd_target_coff_flavour
,
2827 BFD_ENDIAN_LITTLE
, /* data byte order is little */
2828 BFD_ENDIAN_LITTLE
, /* header byte order is little */
2830 (HAS_RELOC
| EXEC_P
| /* FIXME: object flags */
2831 HAS_LINENO
| HAS_DEBUG
|
2832 HAS_SYMS
| HAS_LOCALS
| WP_TEXT
| D_PAGED
),
2834 #ifndef COFF_WITH_PE
2835 (SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
), /* section flags */
2837 (SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
/* section flags */
2838 | SEC_LINK_ONCE
| SEC_LINK_DUPLICATES
),
2841 0, /* leading char */
2842 '/', /* ar_pad_char */
2843 15, /* ar_max_namelen??? FIXMEmgo */
2845 bfd_getl64
, bfd_getl_signed_64
, bfd_putl64
,
2846 bfd_getl32
, bfd_getl_signed_32
, bfd_putl32
,
2847 bfd_getl16
, bfd_getl_signed_16
, bfd_putl16
, /* data */
2849 bfd_getl64
, bfd_getl_signed_64
, bfd_putl64
,
2850 bfd_getl32
, bfd_getl_signed_32
, bfd_putl32
,
2851 bfd_getl16
, bfd_getl_signed_16
, bfd_putl16
, /* hdrs */
2853 {_bfd_dummy_target
, coff_object_p
, /* bfd_check_format */
2854 bfd_generic_archive_p
, /* _bfd_dummy_target */ coff_object_p
},
2855 {bfd_false
, coff_mkobject
, _bfd_generic_mkarchive
, /* bfd_set_format */
2857 {bfd_false
, coff_write_object_contents
, /* bfd_write_contents */
2858 _bfd_write_archive_contents
, bfd_false
},
2860 BFD_JUMP_TABLE_GENERIC (coff
),
2861 BFD_JUMP_TABLE_COPY (coff
),
2862 BFD_JUMP_TABLE_CORE (_bfd_nocore
),
2863 BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_coff
),
2864 BFD_JUMP_TABLE_SYMBOLS (coff
),
2865 BFD_JUMP_TABLE_RELOCS (coff
),
2866 BFD_JUMP_TABLE_WRITE (coff
),
2867 BFD_JUMP_TABLE_LINK (coff
),
2868 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic
),
2870 /* Alternative_target. */
2871 #ifdef TARGET_BIG_SYM
2881 #ifdef TARGET_BIG_SYM
2882 const bfd_target TARGET_BIG_SYM
=
2885 bfd_target_coff_flavour
,
2886 BFD_ENDIAN_BIG
, /* data byte order is big */
2887 BFD_ENDIAN_BIG
, /* header byte order is big */
2889 (HAS_RELOC
| EXEC_P
| /* FIXME: object flags */
2890 HAS_LINENO
| HAS_DEBUG
|
2891 HAS_SYMS
| HAS_LOCALS
| WP_TEXT
| D_PAGED
),
2893 #ifndef COFF_WITH_PE
2894 (SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
), /* section flags */
2896 (SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
/* section flags */
2897 | SEC_LINK_ONCE
| SEC_LINK_DUPLICATES
),
2900 0, /* leading char */
2901 '/', /* ar_pad_char */
2902 15, /* ar_max_namelen??? FIXMEmgo */
2904 bfd_getb64
, bfd_getb_signed_64
, bfd_putb64
,
2905 bfd_getb32
, bfd_getb_signed_32
, bfd_putb32
,
2906 bfd_getb16
, bfd_getb_signed_16
, bfd_putb16
, /* data */
2908 bfd_getb64
, bfd_getb_signed_64
, bfd_putb64
,
2909 bfd_getb32
, bfd_getb_signed_32
, bfd_putb32
,
2910 bfd_getb16
, bfd_getb_signed_16
, bfd_putb16
, /* hdrs */
2912 {_bfd_dummy_target
, coff_object_p
, /* bfd_check_format */
2913 bfd_generic_archive_p
, /* _bfd_dummy_target */ coff_object_p
},
2914 {bfd_false
, coff_mkobject
, _bfd_generic_mkarchive
, /* bfd_set_format */
2916 {bfd_false
, coff_write_object_contents
, /* bfd_write_contents */
2917 _bfd_write_archive_contents
, bfd_false
},
2919 BFD_JUMP_TABLE_GENERIC (coff
),
2920 BFD_JUMP_TABLE_COPY (coff
),
2921 BFD_JUMP_TABLE_CORE (_bfd_nocore
),
2922 BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_coff
),
2923 BFD_JUMP_TABLE_SYMBOLS (coff
),
2924 BFD_JUMP_TABLE_RELOCS (coff
),
2925 BFD_JUMP_TABLE_WRITE (coff
),
2926 BFD_JUMP_TABLE_LINK (coff
),
2927 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic
),
2929 /* Alternative_target. */
2930 #ifdef TARGET_LITTLE_SYM
2931 & TARGET_LITTLE_SYM
,