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
223 /* The nt loader points the toc register to &toc + 32768, in order to */
224 /* use the complete range of a 16-bit displacement. We have to adjust */
225 /* for this when we fix up loads displaced off the toc reg. */
226 #define TOC_LOAD_ADJUSTMENT (-32768)
227 #define TOC_SECTION_NAME ".private.toc"
229 /* The main body of code is in coffcode.h. */
231 #define COFF_DEFAULT_SECTION_ALIGNMENT_POWER (3)
233 /* In case we're on a 32-bit machine, construct a 64-bit "-1" value
234 from smaller values. Start with zero, widen, *then* decrement. */
235 #define MINUS_ONE (((bfd_vma)0) - 1)
237 /* these should definitely go in a header file somewhere... */
240 #define IMAGE_REL_PPC_ABSOLUTE 0x0000
243 #define IMAGE_REL_PPC_ADDR64 0x0001
246 #define IMAGE_REL_PPC_ADDR32 0x0002
248 /* 26-bit address, shifted left 2 (branch absolute) */
249 #define IMAGE_REL_PPC_ADDR24 0x0003
252 #define IMAGE_REL_PPC_ADDR16 0x0004
254 /* 16-bit address, shifted left 2 (load doubleword) */
255 #define IMAGE_REL_PPC_ADDR14 0x0005
257 /* 26-bit PC-relative offset, shifted left 2 (branch relative) */
258 #define IMAGE_REL_PPC_REL24 0x0006
260 /* 16-bit PC-relative offset, shifted left 2 (br cond relative) */
261 #define IMAGE_REL_PPC_REL14 0x0007
263 /* 16-bit offset from TOC base */
264 #define IMAGE_REL_PPC_TOCREL16 0x0008
266 /* 16-bit offset from TOC base, shifted left 2 (load doubleword) */
267 #define IMAGE_REL_PPC_TOCREL14 0x0009
269 /* 32-bit addr w/o image base */
270 #define IMAGE_REL_PPC_ADDR32NB 0x000A
272 /* va of containing section (as in an image sectionhdr) */
273 #define IMAGE_REL_PPC_SECREL 0x000B
275 /* sectionheader number */
276 #define IMAGE_REL_PPC_SECTION 0x000C
278 /* substitute TOC restore instruction iff symbol is glue code */
279 #define IMAGE_REL_PPC_IFGLUE 0x000D
281 /* symbol is glue code; virtual address is TOC restore instruction */
282 #define IMAGE_REL_PPC_IMGLUE 0x000E
284 /* va of containing section (limited to 16 bits) */
285 #define IMAGE_REL_PPC_SECREL16 0x000F
287 /* stuff to handle immediate data when the number of bits in the */
288 /* data is greater than the number of bits in the immediate field */
289 /* We need to do (usually) 32 bit arithmetic on 16 bit chunks */
290 #define IMAGE_REL_PPC_REFHI 0x0010
291 #define IMAGE_REL_PPC_REFLO 0x0011
292 #define IMAGE_REL_PPC_PAIR 0x0012
294 /* This is essentially the same as tocrel16, with TOCDEFN assumed */
295 #define IMAGE_REL_PPC_TOCREL16_DEFN 0x0013
297 /* Flag bits in IMAGE_RELOCATION.TYPE */
299 /* subtract reloc value rather than adding it */
300 #define IMAGE_REL_PPC_NEG 0x0100
302 /* fix branch prediction bit to predict branch taken */
303 #define IMAGE_REL_PPC_BRTAKEN 0x0200
305 /* fix branch prediction bit to predict branch not taken */
306 #define IMAGE_REL_PPC_BRNTAKEN 0x0400
308 /* toc slot defined in file (or, data in toc) */
309 #define IMAGE_REL_PPC_TOCDEFN 0x0800
311 /* masks to isolate above values in IMAGE_RELOCATION.Type */
312 #define IMAGE_REL_PPC_TYPEMASK 0x00FF
313 #define IMAGE_REL_PPC_FLAGMASK 0x0F00
315 #define EXTRACT_TYPE(x) ((x) & IMAGE_REL_PPC_TYPEMASK)
316 #define EXTRACT_FLAGS(x) ((x) & IMAGE_REL_PPC_FLAGMASK)
317 #define EXTRACT_JUNK(x) \
318 ((x) & ~(IMAGE_REL_PPC_TYPEMASK | IMAGE_REL_PPC_FLAGMASK))
321 /* static helper functions to make relocation work */
322 /* (Work In Progress) */
324 static bfd_reloc_status_type ppc_refhi_reloc
PARAMS ((bfd
*abfd
,
332 static bfd_reloc_status_type ppc_reflo_reloc
PARAMS ((bfd
*abfd
,
340 static bfd_reloc_status_type ppc_pair_reloc
PARAMS ((bfd
*abfd
,
349 static bfd_reloc_status_type ppc_toc16_reloc
PARAMS ((bfd
*abfd
,
358 static bfd_reloc_status_type ppc_addr32nb_reloc
PARAMS ((bfd
*abfd
,
366 static bfd_reloc_status_type ppc_section_reloc
PARAMS ((bfd
*abfd
,
374 static bfd_reloc_status_type ppc_secrel_reloc
PARAMS ((bfd
*abfd
,
382 static bfd_reloc_status_type ppc_imglue_reloc
PARAMS ((bfd
*abfd
,
390 static boolean in_reloc_p
PARAMS((bfd
*abfd
, reloc_howto_type
*howto
));
393 /* FIXME: It'll take a while to get through all of these. I only need a few to
394 get us started, so those I'll make sure work. Those marked FIXME are either
395 completely unverified or have a specific unknown marked in the comment */
397 /*---------------------------------------------------------------------------*/
399 /* Relocation entries for Windows/NT on PowerPC. */
401 /* From the document "" we find the following listed as used relocs: */
403 /* ABSOLUTE : The noop */
404 /* ADDR[64|32|16] : fields that hold addresses in data fields or the */
405 /* 16 bit displacement field on a load/store. */
406 /* ADDR[24|14] : fields that hold addresses in branch and cond */
407 /* branches. These represent [26|16] bit addresses. */
408 /* The low order 2 bits are preserved. */
409 /* REL[24|14] : branches relative to the Instruction Address */
410 /* register. These represent [26|16] bit addresses, */
411 /* as before. The instruction field will be zero, and */
412 /* the address of the SYM will be inserted at link time. */
413 /* TOCREL16 : 16 bit displacement field referring to a slot in */
415 /* TOCREL14 : 16 bit displacement field, similar to REL14 or ADDR14. */
416 /* ADDR32NB : 32 bit address relative to the virtual origin. */
417 /* (On the alpha, this is always a linker generated thunk)*/
418 /* (i.e. 32bit addr relative to the image base) */
419 /* SECREL : The value is relative to the start of the section */
420 /* containing the symbol. */
421 /* SECTION : access to the header containing the item. Supports the */
422 /* codeview debugger. */
424 /* In particular, note that the document does not indicate that the */
425 /* relocations listed in the header file are used. */
429 /*---------------------------------------------------------------------------*/
431 static reloc_howto_type ppc_coff_howto_table
[] =
433 /* IMAGE_REL_PPC_ABSOLUTE 0x0000 NOP */
435 HOWTO (IMAGE_REL_PPC_ABSOLUTE
, /* type */
437 0, /* size (0 = byte, 1 = short, 2 = long) */
439 false, /* pc_relative */
441 complain_overflow_dont
, /* dont complain_on_overflow */
442 0, /* special_function */
443 "ABSOLUTE", /* name */
444 false, /* partial_inplace */
447 false), /* pcrel_offset */
449 /* IMAGE_REL_PPC_ADDR64 0x0001 64-bit address */
451 HOWTO(IMAGE_REL_PPC_ADDR64
, /* type */
453 3, /* size (0 = byte, 1 = short, 2 = long) */
455 false, /* pc_relative */
457 complain_overflow_bitfield
, /* complain_on_overflow */
458 0, /* special_function */
460 true, /* partial_inplace */
461 MINUS_ONE
, /* src_mask */
462 MINUS_ONE
, /* dst_mask */
463 false), /* pcrel_offset */
465 /* IMAGE_REL_PPC_ADDR32 0x0002 32-bit address */
467 HOWTO (IMAGE_REL_PPC_ADDR32
, /* type */
469 2, /* size (0 = byte, 1 = short, 2 = long) */
471 false, /* pc_relative */
473 complain_overflow_bitfield
, /* complain_on_overflow */
474 0, /* special_function */
476 true, /* partial_inplace */
477 0xffffffff, /* src_mask */
478 0xffffffff, /* dst_mask */
479 false), /* pcrel_offset */
481 /* IMAGE_REL_PPC_ADDR24 0x0003 26-bit address, shifted left 2 (branch absolute) */
482 /* the LI field is in bit 6 through bit 29 is 24 bits, + 2 for the shift */
483 /* Of course, That's the IBM approved bit numbering, which is not what */
484 /* anyone else uses.... The li field is in bit 2 thru 25 */
486 HOWTO (IMAGE_REL_PPC_ADDR24
, /* type */
488 2, /* size (0 = byte, 1 = short, 2 = long) */
490 false, /* pc_relative */
492 complain_overflow_bitfield
, /* complain_on_overflow */
493 0, /* special_function */
495 true, /* partial_inplace */
496 0x07fffffc, /* src_mask */
497 0x07fffffc, /* dst_mask */
498 false), /* pcrel_offset */
500 /* IMAGE_REL_PPC_ADDR16 0x0004 16-bit address */
502 HOWTO (IMAGE_REL_PPC_ADDR16
, /* type */
504 1, /* size (0 = byte, 1 = short, 2 = long) */
506 false, /* pc_relative */
508 complain_overflow_signed
, /* complain_on_overflow */
509 0, /* special_function */
511 true, /* partial_inplace */
512 0xffff, /* src_mask */
513 0xffff, /* dst_mask */
514 false), /* pcrel_offset */
516 /* IMAGE_REL_PPC_ADDR14 0x0005 */
517 /* 16-bit address, shifted left 2 (load doubleword) */
518 /* FIXME: the mask is likely wrong, and the bit position may be as well */
520 HOWTO (IMAGE_REL_PPC_ADDR14
, /* type */
522 1, /* size (0 = byte, 1 = short, 2 = long) */
524 false, /* pc_relative */
526 complain_overflow_signed
, /* complain_on_overflow */
527 0, /* special_function */
529 true, /* partial_inplace */
530 0xffff, /* src_mask */
531 0xffff, /* dst_mask */
532 false), /* pcrel_offset */
534 /* IMAGE_REL_PPC_REL24 0x0006 */
535 /* 26-bit PC-relative offset, shifted left 2 (branch relative) */
537 HOWTO (IMAGE_REL_PPC_REL24
, /* type */
539 2, /* size (0 = byte, 1 = short, 2 = long) */
541 true, /* pc_relative */
543 complain_overflow_signed
, /* complain_on_overflow */
544 0, /* special_function */
546 true, /* partial_inplace */
547 0x3fffffc, /* src_mask */
548 0x3fffffc, /* dst_mask */
549 false), /* pcrel_offset */
551 /* IMAGE_REL_PPC_REL14 0x0007 */
552 /* 16-bit PC-relative offset, shifted left 2 (br cond relative) */
553 /* FIXME: the mask is likely wrong, and the bit position may be as well */
554 /* FIXME: how does it know how far to shift? */
556 HOWTO (IMAGE_REL_PPC_ADDR14
, /* type */
558 1, /* size (0 = byte, 1 = short, 2 = long) */
560 false, /* pc_relative */
562 complain_overflow_signed
, /* complain_on_overflow */
563 0, /* special_function */
565 true, /* partial_inplace */
566 0xffff, /* src_mask */
567 0xffff, /* dst_mask */
568 true), /* pcrel_offset */
570 /* IMAGE_REL_PPC_TOCREL16 0x0008 */
571 /* 16-bit offset from TOC base */
573 HOWTO (IMAGE_REL_PPC_TOCREL16
,/* type */
575 1, /* size (0 = byte, 1 = short, 2 = long) */
577 false, /* pc_relative */
579 complain_overflow_dont
, /* complain_on_overflow */
580 ppc_toc16_reloc
, /* special_function */
581 "TOCREL16", /* name */
582 false, /* partial_inplace */
583 0xffff, /* src_mask */
584 0xffff, /* dst_mask */
585 false), /* pcrel_offset */
587 /* IMAGE_REL_PPC_TOCREL14 0x0009 */
588 /* 16-bit offset from TOC base, shifted left 2 (load doubleword) */
590 HOWTO (IMAGE_REL_PPC_TOCREL14
,/* type */
592 1, /* size (0 = byte, 1 = short, 2 = long) */
594 false, /* pc_relative */
596 complain_overflow_signed
, /* complain_on_overflow */
597 0, /* special_function */
598 "TOCREL14", /* name */
599 false, /* partial_inplace */
600 0xffff, /* src_mask */
601 0xffff, /* dst_mask */
602 false), /* pcrel_offset */
604 /* IMAGE_REL_PPC_ADDR32NB 0x000A */
605 /* 32-bit addr w/ image base */
607 HOWTO (IMAGE_REL_PPC_ADDR32NB
,/* type */
609 2, /* size (0 = byte, 1 = short, 2 = long) */
611 false, /* pc_relative */
613 complain_overflow_signed
, /* complain_on_overflow */
614 0, /* special_function */
615 "ADDR32NB", /* name */
616 true, /* partial_inplace */
617 0xffffffff, /* src_mask */
618 0xffffffff, /* dst_mask */
619 false), /* pcrel_offset */
621 /* IMAGE_REL_PPC_SECREL 0x000B */
622 /* va of containing section (as in an image sectionhdr) */
624 HOWTO (IMAGE_REL_PPC_SECREL
,/* type */
626 2, /* size (0 = byte, 1 = short, 2 = long) */
628 false, /* pc_relative */
630 complain_overflow_signed
, /* complain_on_overflow */
631 ppc_secrel_reloc
, /* special_function */
633 true, /* partial_inplace */
634 0xffffffff, /* src_mask */
635 0xffffffff, /* dst_mask */
636 true), /* pcrel_offset */
638 /* IMAGE_REL_PPC_SECTION 0x000C */
639 /* sectionheader number */
641 HOWTO (IMAGE_REL_PPC_SECTION
,/* type */
643 2, /* size (0 = byte, 1 = short, 2 = long) */
645 false, /* pc_relative */
647 complain_overflow_signed
, /* complain_on_overflow */
648 ppc_section_reloc
, /* special_function */
649 "SECTION", /* name */
650 true, /* partial_inplace */
651 0xffffffff, /* src_mask */
652 0xffffffff, /* dst_mask */
653 true), /* pcrel_offset */
655 /* IMAGE_REL_PPC_IFGLUE 0x000D */
656 /* substitute TOC restore instruction iff symbol is glue code */
658 HOWTO (IMAGE_REL_PPC_IFGLUE
,/* type */
660 2, /* size (0 = byte, 1 = short, 2 = long) */
662 false, /* pc_relative */
664 complain_overflow_signed
, /* complain_on_overflow */
665 0, /* special_function */
667 true, /* partial_inplace */
668 0xffffffff, /* src_mask */
669 0xffffffff, /* dst_mask */
670 false), /* pcrel_offset */
672 /* IMAGE_REL_PPC_IMGLUE 0x000E */
673 /* symbol is glue code; virtual address is TOC restore instruction */
675 HOWTO (IMAGE_REL_PPC_IMGLUE
,/* type */
677 2, /* size (0 = byte, 1 = short, 2 = long) */
679 false, /* pc_relative */
681 complain_overflow_dont
, /* complain_on_overflow */
682 ppc_imglue_reloc
, /* special_function */
684 false, /* partial_inplace */
685 0xffffffff, /* src_mask */
686 0xffffffff, /* dst_mask */
687 false), /* pcrel_offset */
689 /* IMAGE_REL_PPC_SECREL16 0x000F */
690 /* va of containing section (limited to 16 bits) */
692 HOWTO (IMAGE_REL_PPC_SECREL16
,/* type */
694 1, /* size (0 = byte, 1 = short, 2 = long) */
696 false, /* pc_relative */
698 complain_overflow_signed
, /* complain_on_overflow */
699 0, /* special_function */
700 "SECREL16", /* name */
701 true, /* partial_inplace */
702 0xffff, /* src_mask */
703 0xffff, /* dst_mask */
704 true), /* pcrel_offset */
706 /* IMAGE_REL_PPC_REFHI 0x0010 */
708 HOWTO (IMAGE_REL_PPC_REFHI
, /* type */
710 1, /* size (0 = byte, 1 = short, 2 = long) */
712 false, /* pc_relative */
714 complain_overflow_signed
, /* complain_on_overflow */
715 ppc_refhi_reloc
, /* special_function */
717 true, /* partial_inplace */
718 0xffffffff, /* src_mask */
719 0xffffffff, /* dst_mask */
720 false), /* pcrel_offset */
722 /* IMAGE_REL_PPC_REFLO 0x0011 */
724 HOWTO (IMAGE_REL_PPC_REFLO
, /* type */
726 1, /* size (0 = byte, 1 = short, 2 = long) */
728 false, /* pc_relative */
730 complain_overflow_signed
, /* complain_on_overflow */
731 ppc_refhi_reloc
, /* special_function */
733 true, /* partial_inplace */
734 0xffffffff, /* src_mask */
735 0xffffffff, /* dst_mask */
736 false), /* pcrel_offset */
738 /* IMAGE_REL_PPC_PAIR 0x0012 */
740 HOWTO (IMAGE_REL_PPC_PAIR
, /* type */
742 1, /* size (0 = byte, 1 = short, 2 = long) */
744 false, /* pc_relative */
746 complain_overflow_signed
, /* complain_on_overflow */
747 ppc_pair_reloc
, /* special_function */
749 true, /* partial_inplace */
750 0xffffffff, /* src_mask */
751 0xffffffff, /* dst_mask */
752 false), /* pcrel_offset */
754 /* IMAGE_REL_PPC_TOCREL16_DEFN 0x0013 */
755 /* 16-bit offset from TOC base, without causing a definition */
757 HOWTO ( (IMAGE_REL_PPC_TOCREL16
| IMAGE_REL_PPC_TOCDEFN
), /* type */
759 1, /* size (0 = byte, 1 = short, 2 = long) */
761 false, /* pc_relative */
763 complain_overflow_dont
, /* complain_on_overflow */
764 0, /* special_function */
765 "TOCREL16, TOCDEFN", /* name */
766 false, /* partial_inplace */
767 0xffff, /* src_mask */
768 0xffff, /* dst_mask */
769 false), /* pcrel_offset */
775 /* Some really cheezy macros that can be turned on to test stderr :-) */
784 fprintf (stderr,_("Unimplemented Relocation -- %s\n"),x); \
788 #define DUMP_RELOC(n,r) \
790 fprintf (stderr,"%s sym %d, addr %d, addend %d\n", \
791 n, (*(r->sym_ptr_ptr))->name, \
792 r->address, r->addend); \
795 /* Given a reloc name, n, and a pointer to an internal_reloc,
796 dump out interesting information on the contents
798 #define n_name _n._n_name
799 #define n_zeroes _n._n_n._n_zeroes
800 #define n_offset _n._n_n._n_offset
804 #define DUMP_RELOC2(n,r) \
806 fprintf (stderr,"%s sym %d, r_vaddr %d %s\n", \
807 n, r->r_symndx, r->r_vaddr,\
808 (((r->r_type) & IMAGE_REL_PPC_TOCDEFN) == 0) \
814 #define DUMP_RELOC(n,r)
815 #define DUMP_RELOC2(n,r)
819 /* toc construction and management routines */
821 /* This file is compiled twice, and these variables are defined in one
822 of the compilations. FIXME: This is confusing and weird. Also,
823 BFD should not use global variables. */
824 extern bfd
* bfd_of_toc_owner
;
825 extern long int global_toc_size
;
827 extern long int import_table_size
;
828 extern long int first_thunk_address
;
829 extern long int thunk_size
;
847 struct list_ele
*next
;
849 enum ref_category cat
;
854 extern struct list_ele
*head
;
855 extern struct list_ele
*tail
;
857 static void record_toc
858 PARAMS ((asection
*, int, enum ref_category
, const char *));
861 record_toc (toc_section
, our_toc_offset
, cat
, name
)
862 asection
*toc_section
;
864 enum ref_category cat
;
867 /* add this entry to our toc addr-offset-name list */
869 t
= (struct list_ele
*) bfd_malloc (sizeof (struct list_ele
));
873 t
->offset
= our_toc_offset
;
876 t
->addr
= toc_section
->output_offset
+ our_toc_offset
;
890 #ifdef COFF_IMAGE_WITH_PE
892 static boolean ppc_record_toc_entry
893 PARAMS ((bfd
*, struct bfd_link_info
*, asection
*, int, enum toc_type
));
894 static void ppc_mark_symbol_as_glue
895 PARAMS ((bfd
*, int, struct internal_reloc
*));
897 /* record a toc offset against a symbol */
899 ppc_record_toc_entry(abfd
, info
, sec
, sym
, toc_kind
)
901 struct bfd_link_info
*info ATTRIBUTE_UNUSED
;
902 asection
*sec ATTRIBUTE_UNUSED
;
904 enum toc_type toc_kind ATTRIBUTE_UNUSED
;
906 struct ppc_coff_link_hash_entry
*h
;
913 h
= (struct ppc_coff_link_hash_entry
*) (obj_coff_sym_hashes (abfd
)[sym
]);
921 local_syms
= obj_coff_local_toc_table(abfd
);
925 /* allocate a table */
927 (int *) bfd_zalloc (abfd
,
928 obj_raw_syment_count(abfd
) * sizeof (int));
931 obj_coff_local_toc_table(abfd
) = local_syms
;
932 for (i
= 0; i
< obj_raw_syment_count(abfd
); ++i
)
934 SET_UNALLOCATED(local_syms
[i
]);
938 if (IS_UNALLOCATED(local_syms
[sym
]))
940 local_syms
[sym
] = global_toc_size
;
941 global_toc_size
+= 4;
943 /* The size must fit in a 16bit displacment */
944 if (global_toc_size
> 65535)
946 (*_bfd_error_handler
) (_("TOC overflow"));
947 bfd_set_error (bfd_error_file_too_big
);
954 name
= h
->root
.root
.root
.string
;
956 /* check to see if there's a toc slot allocated. If not, do it
957 here. It will be used in relocate_section */
958 if (IS_UNALLOCATED(h
->toc_offset
))
960 h
->toc_offset
= global_toc_size
;
961 global_toc_size
+= 4;
963 /* The size must fit in a 16bit displacment */
964 if (global_toc_size
>= 65535)
966 (*_bfd_error_handler
) (_("TOC overflow"));
967 bfd_set_error (bfd_error_file_too_big
);
976 /* record a toc offset against a symbol */
978 ppc_mark_symbol_as_glue(abfd
, sym
, rel
)
981 struct internal_reloc
*rel
;
983 struct ppc_coff_link_hash_entry
*h
;
985 h
= (struct ppc_coff_link_hash_entry
*) (obj_coff_sym_hashes (abfd
)[sym
]);
989 h
->symbol_is_glue
= 1;
990 h
->glue_insn
= bfd_get_32 (abfd
, (bfd_byte
*) &rel
->r_vaddr
);
995 #endif /* COFF_IMAGE_WITH_PE */
998 /* Return true if this relocation should
999 appear in the output .reloc section. */
1001 static boolean
in_reloc_p(abfd
, howto
)
1002 bfd
* abfd ATTRIBUTE_UNUSED
;
1003 reloc_howto_type
*howto
;
1006 (! howto
->pc_relative
)
1007 && (howto
->type
!= IMAGE_REL_PPC_ADDR32NB
)
1008 && (howto
->type
!= IMAGE_REL_PPC_TOCREL16
)
1009 && (howto
->type
!= IMAGE_REL_PPC_IMGLUE
)
1010 && (howto
->type
!= IMAGE_REL_PPC_IFGLUE
)
1011 && (howto
->type
!= IMAGE_REL_PPC_SECREL
)
1012 && (howto
->type
!= IMAGE_REL_PPC_SECTION
)
1013 && (howto
->type
!= IMAGE_REL_PPC_SECREL16
)
1014 && (howto
->type
!= IMAGE_REL_PPC_REFHI
)
1015 && (howto
->type
!= IMAGE_REL_PPC_REFLO
)
1016 && (howto
->type
!= IMAGE_REL_PPC_PAIR
)
1017 && (howto
->type
!= IMAGE_REL_PPC_TOCREL16_DEFN
) ;
1022 /* this function is in charge of performing all the ppc PE relocations */
1023 /* Don't yet know if we want to do this this particular way ... (krk) */
1024 /* FIXME: (it is not yet enabled) */
1026 static bfd_reloc_status_type
1027 pe_ppc_reloc (abfd
, reloc_entry
, symbol_in
, data
, input_section
, output_bfd
,
1030 arelent
*reloc_entry
;
1033 asection
*input_section
;
1035 char **error_message
;
1037 /* the consth relocation comes in two parts, we have to remember
1038 the state between calls, in these variables */
1039 static boolean part1_consth_active
= false;
1040 static unsigned long part1_consth_value
;
1042 unsigned long sym_value
;
1043 unsigned short r_type
;
1044 unsigned long addr
= reloc_entry
->address
; /*+ input_section->vma*/
1046 r_type
= reloc_entry
->howto
->type
;
1050 /* Partial linking - do nothing */
1051 reloc_entry
->address
+= input_section
->output_offset
;
1052 return bfd_reloc_ok
;
1055 if (symbol_in
!= NULL
1056 && bfd_is_und_section (symbol_in
->section
))
1058 /* Keep the state machine happy in case we're called again */
1059 if (r_type
== IMAGE_REL_PPC_REFHI
)
1061 part1_consth_active
= true;
1062 part1_consth_value
= 0;
1064 return(bfd_reloc_undefined
);
1067 if ((part1_consth_active
) && (r_type
!= IMAGE_REL_PPC_PAIR
))
1069 part1_consth_active
= false;
1070 *error_message
= (char *) _("Missing PAIR");
1071 return(bfd_reloc_dangerous
);
1074 sym_value
= get_symbol_value(symbol_in
);
1076 return(bfd_reloc_ok
);
1081 /* The reloc processing routine for the optimized COFF linker. */
1084 coff_ppc_relocate_section (output_bfd
, info
, input_bfd
, input_section
,
1085 contents
, relocs
, syms
, sections
)
1087 struct bfd_link_info
*info
;
1089 asection
*input_section
;
1091 struct internal_reloc
*relocs
;
1092 struct internal_syment
*syms
;
1093 asection
**sections
;
1095 struct internal_reloc
*rel
;
1096 struct internal_reloc
*relend
;
1099 asection
*toc_section
= 0;
1101 reloc_howto_type
*howto
= 0;
1103 /* If we are performing a relocateable link, we don't need to do a
1104 thing. The caller will take care of adjusting the reloc
1105 addresses and symbol indices. */
1106 if (info
->relocateable
)
1113 relend
= rel
+ input_section
->reloc_count
;
1114 for (; rel
< relend
; rel
++)
1117 struct ppc_coff_link_hash_entry
*h
;
1118 struct internal_syment
*sym
;
1122 bfd_reloc_status_type rstat
;
1125 unsigned short r_type
= EXTRACT_TYPE (rel
->r_type
);
1126 unsigned short r_flags
= EXTRACT_FLAGS(rel
->r_type
);
1128 symndx
= rel
->r_symndx
;
1129 loc
= contents
+ rel
->r_vaddr
- input_section
->vma
;
1131 /* FIXME: check bounds on r_type */
1132 howto
= ppc_coff_howto_table
+ r_type
;
1141 h
= (struct ppc_coff_link_hash_entry
*)
1142 (obj_coff_sym_hashes (input_bfd
)[symndx
]);
1148 sym
= syms
+ symndx
;
1151 if (r_type
== IMAGE_REL_PPC_IMGLUE
&& h
== 0)
1153 /* An IMGLUE reloc must have a name. Something is very wrong. */
1160 /* FIXME: PAIR unsupported in the following code */
1164 sec
= bfd_abs_section_ptr
;
1167 sec
= sections
[symndx
];
1168 val
= (sec
->output_section
->vma
1169 + sec
->output_offset
1171 if (! obj_pe (output_bfd
))
1179 if (h
->root
.root
.type
== bfd_link_hash_defined
1180 || h
->root
.root
.type
== bfd_link_hash_defweak
)
1182 sec
= h
->root
.root
.u
.def
.section
;
1183 val
= (h
->root
.root
.u
.def
.value
1184 + sec
->output_section
->vma
1185 + sec
->output_offset
);
1189 if (! ((*info
->callbacks
->undefined_symbol
)
1190 (info
, h
->root
.root
.root
.string
, input_bfd
, input_section
,
1191 rel
->r_vaddr
- input_section
->vma
, true)))
1196 rstat
= bfd_reloc_ok
;
1198 /* Each case must do its own relocation, setting rstat appropriately */
1202 (*_bfd_error_handler
)
1203 (_("%s: unsupported relocation type 0x%02x"),
1204 bfd_get_filename (input_bfd
), r_type
);
1205 bfd_set_error (bfd_error_bad_value
);
1207 case IMAGE_REL_PPC_TOCREL16
:
1209 bfd_vma our_toc_offset
;
1212 DUMP_RELOC2(howto
->name
, rel
);
1214 if (toc_section
== 0)
1216 toc_section
= bfd_get_section_by_name (bfd_of_toc_owner
,
1219 if ( toc_section
== NULL
)
1221 /* There is no toc section. Something is very wrong. */
1227 * Amazing bit tricks present. As we may have seen earlier, we
1228 * use the 1 bit to tell us whether or not a toc offset has been
1229 * allocated. Now that they've all been allocated, we will use
1230 * the 1 bit to tell us if we've written this particular toc
1235 { /* it is a file local symbol */
1236 int *local_toc_table
;
1239 sym
= syms
+ symndx
;
1240 name
= sym
->_n
._n_name
;
1242 local_toc_table
= obj_coff_local_toc_table(input_bfd
);
1243 our_toc_offset
= local_toc_table
[symndx
];
1245 if (IS_WRITTEN(our_toc_offset
))
1247 /* if it has been written out, it is marked with the
1248 1 bit. Fix up our offset, but do not write it out
1251 MAKE_ADDR_AGAIN(our_toc_offset
);
1255 /* write out the toc entry */
1256 record_toc(toc_section
,
1261 bfd_put_32 (output_bfd
,
1263 toc_section
->contents
+ our_toc_offset
);
1265 MARK_AS_WRITTEN(local_toc_table
[symndx
]);
1271 const char *name
= h
->root
.root
.root
.string
;
1272 our_toc_offset
= h
->toc_offset
;
1274 if ((r_flags
& IMAGE_REL_PPC_TOCDEFN
)
1275 == IMAGE_REL_PPC_TOCDEFN
)
1277 /* This is unbelievable cheese. Some knowledgable asm
1278 hacker has decided to use r2 as a base for loading
1279 a value. He/She does this by setting the tocdefn bit,
1280 and not supplying a toc definition. The behaviour is
1281 then to use the difference between the value of the
1282 symbol and the actual location of the toc as the toc
1285 In fact, what is usually happening is, because the
1286 Import Address Table is mapped immediately following
1287 the toc, some trippy library code trying for speed on
1288 dll linkage, takes advantage of that and considers
1289 the IAT to be part of the toc, thus saving a load.
1292 our_toc_offset
= val
-
1293 (toc_section
->output_section
->vma
+
1294 toc_section
->output_offset
);
1296 /* The size must still fit in a 16bit displacment */
1297 if (our_toc_offset
>= 65535)
1299 (*_bfd_error_handler
)
1300 (_("%s: Relocation for %s of %x exceeds Toc size limit"),
1301 bfd_get_filename (input_bfd
), name
, our_toc_offset
);
1302 bfd_set_error (bfd_error_bad_value
);
1306 record_toc(toc_section
, our_toc_offset
, pub
, strdup(name
));
1308 else if (IS_WRITTEN(our_toc_offset
))
1310 /* if it has been written out, it is marked with the
1311 1 bit. Fix up our offset, but do not write it out
1314 MAKE_ADDR_AGAIN(our_toc_offset
);
1318 record_toc(toc_section
, our_toc_offset
, pub
, strdup(name
));
1320 /* write out the toc entry */
1321 bfd_put_32 (output_bfd
,
1323 toc_section
->contents
+ our_toc_offset
);
1325 MARK_AS_WRITTEN(h
->toc_offset
);
1326 /* The tricky part is that this is the address that */
1327 /* needs a .reloc entry for it */
1332 if (fixit
&& info
->base_file
)
1334 /* So if this is non pcrelative, and is referenced
1335 to a section or a common symbol, then it needs a reloc */
1337 /* relocation to a symbol in a section which
1338 isn't absolute - we output the address here
1341 bfd_vma addr
= toc_section
->output_section
->vma
1342 + toc_section
->output_offset
+ our_toc_offset
;
1344 if (coff_data(output_bfd
)->pe
)
1345 addr
-= pe_data(output_bfd
)->pe_opthdr
.ImageBase
;
1347 fwrite (&addr
, 1,4, (FILE *) info
->base_file
);
1350 /* FIXME: this test is conservative */
1351 if ( (r_flags
& IMAGE_REL_PPC_TOCDEFN
) != IMAGE_REL_PPC_TOCDEFN
&&
1352 our_toc_offset
> toc_section
->_raw_size
)
1354 (*_bfd_error_handler
)
1355 (_("%s: Relocation exceeds allocated TOC (%x)"),
1356 bfd_get_filename (input_bfd
),
1357 toc_section
->_raw_size
);
1358 bfd_set_error (bfd_error_bad_value
);
1362 /* Now we know the relocation for this toc reference */
1363 relocation
= our_toc_offset
+ TOC_LOAD_ADJUSTMENT
;
1364 rstat
= _bfd_relocate_contents (howto
,
1370 case IMAGE_REL_PPC_IFGLUE
:
1372 /* To solve this, we need to know whether or not the symbol */
1373 /* appearing on the call instruction is a glue function or not. */
1374 /* A glue function must announce itself via a IMGLUE reloc, and */
1375 /* the reloc contains the required toc restore instruction */
1378 const char *my_name
;
1379 DUMP_RELOC2(howto
->name
, rel
);
1383 my_name
= h
->root
.root
.root
.string
;
1384 if (h
->symbol_is_glue
== 1)
1386 x
= bfd_get_32 (input_bfd
, loc
);
1387 bfd_put_32 (input_bfd
, h
->glue_insn
, loc
);
1392 case IMAGE_REL_PPC_SECREL
:
1393 /* Unimplemented: codeview debugging information */
1394 /* For fast access to the header of the section
1395 containing the item. */
1397 case IMAGE_REL_PPC_SECTION
:
1398 /* Unimplemented: codeview debugging information */
1399 /* Is used to indicate that the value should be relative
1400 to the beginning of the section that contains the
1403 case IMAGE_REL_PPC_ABSOLUTE
:
1405 const char *my_name
;
1407 my_name
= (syms
+symndx
)->_n
._n_name
;
1410 my_name
= h
->root
.root
.root
.string
;
1414 _("Warning: unsupported reloc %s <file %s, section %s>\n"),
1416 bfd_get_filename(input_bfd
),
1417 input_section
->name
);
1419 fprintf (stderr
,"sym %ld (%s), r_vaddr %ld (%lx)\n",
1420 rel
->r_symndx
, my_name
, (long) rel
->r_vaddr
,
1421 (unsigned long) rel
->r_vaddr
);
1424 case IMAGE_REL_PPC_IMGLUE
:
1426 /* There is nothing to do now. This reloc was noted in the first
1427 pass over the relocs, and the glue instruction extracted */
1428 const char *my_name
;
1429 if (h
->symbol_is_glue
== 1)
1431 my_name
= h
->root
.root
.root
.string
;
1433 (*_bfd_error_handler
)
1434 (_("%s: Out of order IMGLUE reloc for %s"),
1435 bfd_get_filename (input_bfd
), my_name
);
1436 bfd_set_error (bfd_error_bad_value
);
1440 case IMAGE_REL_PPC_ADDR32NB
:
1442 struct coff_link_hash_entry
*myh
= 0;
1443 const char *name
= 0;
1444 DUMP_RELOC2(howto
->name
, rel
);
1446 if (strncmp(".idata$2",input_section
->name
,8) == 0 && first_thunk_address
== 0)
1448 /* set magic values */
1450 struct coff_link_hash_entry
*myh
= 0;
1451 myh
= coff_link_hash_lookup (coff_hash_table (info
),
1453 false, false, true);
1454 first_thunk_address
= myh
->root
.u
.def
.value
+
1455 sec
->output_section
->vma
+
1456 sec
->output_offset
-
1457 pe_data(output_bfd
)->pe_opthdr
.ImageBase
;
1459 idata5offset
= myh
->root
.u
.def
.value
;
1460 myh
= coff_link_hash_lookup (coff_hash_table (info
),
1462 false, false, true);
1464 thunk_size
= myh
->root
.u
.def
.value
- idata5offset
;
1465 myh
= coff_link_hash_lookup (coff_hash_table (info
),
1467 false, false, true);
1468 import_table_size
= myh
->root
.u
.def
.value
;
1472 { /* it is a file local symbol */
1473 sym
= syms
+ symndx
;
1474 name
= sym
->_n
._n_name
;
1480 name
= h
->root
.root
.root
.string
;
1481 if (strcmp(".idata$2", name
) == 0)
1482 target
= "__idata2_magic__";
1483 else if (strcmp(".idata$4", name
) == 0)
1484 target
= "__idata4_magic__";
1485 else if (strcmp(".idata$5", name
) == 0)
1486 target
= "__idata5_magic__";
1492 myh
= coff_link_hash_lookup (coff_hash_table (info
),
1494 false, false, true);
1497 /* Missing magic cookies. Something is very wrong. */
1501 val
= myh
->root
.u
.def
.value
+
1502 sec
->output_section
->vma
+ sec
->output_offset
;
1503 if (first_thunk_address
== 0)
1506 myh
= coff_link_hash_lookup (coff_hash_table (info
),
1508 false, false, true);
1509 first_thunk_address
= myh
->root
.u
.def
.value
+
1510 sec
->output_section
->vma
+
1511 sec
->output_offset
-
1512 pe_data(output_bfd
)->pe_opthdr
.ImageBase
;
1514 idata5offset
= myh
->root
.u
.def
.value
;
1515 myh
= coff_link_hash_lookup (coff_hash_table (info
),
1517 false, false, true);
1519 thunk_size
= myh
->root
.u
.def
.value
- idata5offset
;
1520 myh
= coff_link_hash_lookup (coff_hash_table (info
),
1522 false, false, true);
1523 import_table_size
= myh
->root
.u
.def
.value
;
1528 rstat
= _bfd_relocate_contents (howto
,
1531 pe_data(output_bfd
)->pe_opthdr
.ImageBase
,
1536 case IMAGE_REL_PPC_REL24
:
1537 DUMP_RELOC2(howto
->name
, rel
);
1538 val
-= (input_section
->output_section
->vma
1539 + input_section
->output_offset
);
1541 rstat
= _bfd_relocate_contents (howto
,
1546 case IMAGE_REL_PPC_ADDR16
:
1547 case IMAGE_REL_PPC_ADDR24
:
1548 case IMAGE_REL_PPC_ADDR32
:
1549 DUMP_RELOC2(howto
->name
, rel
);
1550 rstat
= _bfd_relocate_contents (howto
,
1557 if ( info
->base_file
)
1559 /* So if this is non pcrelative, and is referenced
1560 to a section or a common symbol, then it needs a reloc */
1561 if (sym
&& pe_data(output_bfd
)->in_reloc_p(output_bfd
, howto
))
1563 /* relocation to a symbol in a section which
1564 isn't absolute - we output the address here
1566 bfd_vma addr
= rel
->r_vaddr
1567 - input_section
->vma
1568 + input_section
->output_offset
1569 + input_section
->output_section
->vma
;
1571 if (coff_data(output_bfd
)->pe
)
1573 addr
-= pe_data(output_bfd
)->pe_opthdr
.ImageBase
;
1575 fwrite (&addr
, 1,4, (FILE *) info
->base_file
);
1585 case bfd_reloc_overflow
:
1588 char buf
[SYMNMLEN
+ 1];
1593 name
= h
->root
.root
.root
.string
;
1594 else if (sym
== NULL
)
1596 else if (sym
->_n
._n_n
._n_zeroes
== 0
1597 && sym
->_n
._n_n
._n_offset
!= 0)
1598 name
= obj_coff_strings (input_bfd
) + sym
->_n
._n_n
._n_offset
;
1601 strncpy (buf
, sym
->_n
._n_name
, SYMNMLEN
);
1602 buf
[SYMNMLEN
] = '\0';
1606 if (! ((*info
->callbacks
->reloc_overflow
)
1607 (info
, name
, howto
->name
,
1608 (bfd_vma
) 0, input_bfd
,
1609 input_section
, rel
->r_vaddr
- input_section
->vma
)))
1621 #ifdef COFF_IMAGE_WITH_PE
1623 /* FIXME: BFD should not use global variables. This file is compiled
1624 twice, and these variables are shared. This is confusing and
1627 long int global_toc_size
= 4;
1629 bfd
* bfd_of_toc_owner
= 0;
1631 long int import_table_size
;
1632 long int first_thunk_address
;
1633 long int thunk_size
;
1635 struct list_ele
*head
;
1636 struct list_ele
*tail
;
1639 h1
= N_("\n\t\t\tTOC MAPPING\n\n");
1641 h2
= N_(" TOC disassembly Comments Name\n");
1643 h3
= N_(" Offset spelling (if present)\n");
1649 FILE *file
= (FILE *) vfile
;
1652 fprintf (file
, _(h1
));
1653 fprintf (file
, _(h2
));
1654 fprintf (file
, _(h3
));
1656 for (t
= head
; t
!= 0; t
=t
->next
)
1658 const char *cat
= "";
1661 cat
= _("private ");
1662 else if (t
->cat
== pub
)
1664 else if (t
->cat
== data
)
1665 cat
= _("data-in-toc ");
1667 if (t
->offset
> global_toc_size
)
1669 if (t
->offset
<= global_toc_size
+ thunk_size
)
1670 cat
= _("IAT reference ");
1674 _("**** global_toc_size %ld(%lx), thunk_size %ld(%lx)\n"),
1675 global_toc_size
, global_toc_size
, thunk_size
, thunk_size
);
1676 cat
= _("Out of bounds!");
1681 " %04lx (%d)", (unsigned long) t
->offset
, t
->offset
- 32768);
1688 fprintf (file
, "\n");
1692 ppc_allocate_toc_section (info
)
1693 struct bfd_link_info
*info ATTRIBUTE_UNUSED
;
1697 static char test_char
= '1';
1699 if ( global_toc_size
== 0 ) /* FIXME: does this get me in trouble? */
1702 if (bfd_of_toc_owner
== 0)
1704 /* No toc owner? Something is very wrong. */
1708 s
= bfd_get_section_by_name ( bfd_of_toc_owner
, TOC_SECTION_NAME
);
1711 /* No toc section? Something is very wrong. */
1715 foo
= (bfd_byte
*) bfd_alloc(bfd_of_toc_owner
, global_toc_size
);
1716 memset(foo
, test_char
, global_toc_size
);
1718 s
->_raw_size
= s
->_cooked_size
= global_toc_size
;
1725 ppc_process_before_allocation (abfd
, info
)
1727 struct bfd_link_info
*info
;
1730 struct internal_reloc
*i
, *rel
;
1732 /* here we have a bfd that is to be included on the link. We have a hook
1733 to do reloc rummaging, before section sizes are nailed down. */
1735 _bfd_coff_get_external_symbols(abfd
);
1737 /* rummage around all the relocs and map the toc */
1738 sec
= abfd
->sections
;
1745 for (; sec
!= 0; sec
= sec
->next
)
1747 if (sec
->reloc_count
== 0)
1750 /* load the relocs */
1751 /* FIXME: there may be a storage leak here */
1752 i
=_bfd_coff_read_internal_relocs(abfd
,sec
,1,0,0,0);
1757 for (rel
=i
;rel
<i
+sec
->reloc_count
;++rel
)
1759 unsigned short r_type
= EXTRACT_TYPE (rel
->r_type
);
1760 unsigned short r_flags
= EXTRACT_FLAGS(rel
->r_type
);
1763 DUMP_RELOC2(ppc_coff_howto_table
[r_type
].name
, rel
);
1767 case IMAGE_REL_PPC_TOCREL16
:
1768 /* if TOCDEFN is on, ignore as someone else has allocated the
1770 if ( (r_flags
& IMAGE_REL_PPC_TOCDEFN
) != IMAGE_REL_PPC_TOCDEFN
)
1771 ok
= ppc_record_toc_entry(abfd
, info
, sec
,
1772 rel
->r_symndx
, default_toc
);
1776 case IMAGE_REL_PPC_IMGLUE
:
1777 ppc_mark_symbol_as_glue(abfd
, rel
->r_symndx
, rel
);
1790 static bfd_reloc_status_type
1791 ppc_refhi_reloc (abfd
,
1798 bfd
*abfd ATTRIBUTE_UNUSED
;
1799 arelent
*reloc_entry ATTRIBUTE_UNUSED
;
1800 asymbol
*symbol ATTRIBUTE_UNUSED
;
1801 PTR data ATTRIBUTE_UNUSED
;
1802 asection
*input_section ATTRIBUTE_UNUSED
;
1804 char **error_message ATTRIBUTE_UNUSED
;
1807 DUMP_RELOC("REFHI",reloc_entry
);
1809 if (output_bfd
== (bfd
*) NULL
)
1810 return bfd_reloc_continue
;
1812 return bfd_reloc_undefined
;
1817 static bfd_reloc_status_type
1818 ppc_reflo_reloc (abfd
,
1826 arelent
*reloc_entry
;
1829 asection
*input_section
;
1831 char **error_message
;
1834 DUMP_RELOC("REFLO",reloc_entry
);
1836 if (output_bfd
== (bfd
*) NULL
)
1837 return bfd_reloc_continue
;
1839 return bfd_reloc_undefined
;
1844 static bfd_reloc_status_type
1845 ppc_pair_reloc (abfd
,
1852 bfd
*abfd ATTRIBUTE_UNUSED
;
1853 arelent
*reloc_entry ATTRIBUTE_UNUSED
;
1854 asymbol
*symbol ATTRIBUTE_UNUSED
;
1855 PTR data ATTRIBUTE_UNUSED
;
1856 asection
*input_section ATTRIBUTE_UNUSED
;
1858 char **error_message ATTRIBUTE_UNUSED
;
1861 DUMP_RELOC("PAIR",reloc_entry
);
1863 if (output_bfd
== (bfd
*) NULL
)
1864 return bfd_reloc_continue
;
1866 return bfd_reloc_undefined
;
1870 static bfd_reloc_status_type
1871 ppc_toc16_reloc (abfd
,
1878 bfd
*abfd ATTRIBUTE_UNUSED
;
1879 arelent
*reloc_entry ATTRIBUTE_UNUSED
;
1880 asymbol
*symbol ATTRIBUTE_UNUSED
;
1881 PTR data ATTRIBUTE_UNUSED
;
1882 asection
*input_section ATTRIBUTE_UNUSED
;
1884 char **error_message ATTRIBUTE_UNUSED
;
1886 UN_IMPL("TOCREL16");
1887 DUMP_RELOC("TOCREL16",reloc_entry
);
1889 if (output_bfd
== (bfd
*) NULL
)
1891 return bfd_reloc_continue
;
1894 return bfd_reloc_ok
;
1899 /* ADDR32NB : 32 bit address relative to the virtual origin. */
1900 /* (On the alpha, this is always a linker generated thunk)*/
1901 /* (i.e. 32bit addr relative to the image base) */
1905 static bfd_reloc_status_type
1906 ppc_addr32nb_reloc (abfd
,
1914 arelent
*reloc_entry
;
1917 asection
*input_section
;
1919 char **error_message
;
1921 UN_IMPL("ADDR32NB");
1922 DUMP_RELOC("ADDR32NB",reloc_entry
);
1924 return bfd_reloc_ok
;
1929 static bfd_reloc_status_type
1930 ppc_secrel_reloc (abfd
,
1937 bfd
*abfd ATTRIBUTE_UNUSED
;
1938 arelent
*reloc_entry ATTRIBUTE_UNUSED
;
1939 asymbol
*symbol ATTRIBUTE_UNUSED
;
1940 PTR data ATTRIBUTE_UNUSED
;
1941 asection
*input_section ATTRIBUTE_UNUSED
;
1943 char **error_message ATTRIBUTE_UNUSED
;
1946 DUMP_RELOC("SECREL",reloc_entry
);
1948 if (output_bfd
== (bfd
*) NULL
)
1949 return bfd_reloc_continue
;
1951 return bfd_reloc_ok
;
1954 static bfd_reloc_status_type
1955 ppc_section_reloc (abfd
,
1962 bfd
*abfd ATTRIBUTE_UNUSED
;
1963 arelent
*reloc_entry ATTRIBUTE_UNUSED
;
1964 asymbol
*symbol ATTRIBUTE_UNUSED
;
1965 PTR data ATTRIBUTE_UNUSED
;
1966 asection
*input_section ATTRIBUTE_UNUSED
;
1968 char **error_message ATTRIBUTE_UNUSED
;
1971 DUMP_RELOC("SECTION",reloc_entry
);
1973 if (output_bfd
== (bfd
*) NULL
)
1974 return bfd_reloc_continue
;
1976 return bfd_reloc_ok
;
1979 static bfd_reloc_status_type
1980 ppc_imglue_reloc (abfd
,
1987 bfd
*abfd ATTRIBUTE_UNUSED
;
1988 arelent
*reloc_entry ATTRIBUTE_UNUSED
;
1989 asymbol
*symbol ATTRIBUTE_UNUSED
;
1990 PTR data ATTRIBUTE_UNUSED
;
1991 asection
*input_section ATTRIBUTE_UNUSED
;
1993 char **error_message ATTRIBUTE_UNUSED
;
1996 DUMP_RELOC("IMGLUE",reloc_entry
);
1998 if (output_bfd
== (bfd
*) NULL
)
1999 return bfd_reloc_continue
;
2001 return bfd_reloc_ok
;
2006 #define MAX_RELOC_INDEX \
2007 (sizeof (ppc_coff_howto_table) / sizeof (ppc_coff_howto_table[0]) - 1)
2009 /* FIXME: There is a possiblity that when we read in a reloc from a file,
2010 that there are some bits encoded in the upper portion of the
2011 type field. Not yet implemented.
2013 static void ppc_coff_rtype2howto
PARAMS ((arelent
*relent
,
2014 struct internal_reloc
*internal
));
2017 ppc_coff_rtype2howto (relent
, internal
)
2019 struct internal_reloc
*internal
;
2022 /* We can encode one of three things in the type field, aside from the
2024 1. IMAGE_REL_PPC_NEG - indicates the value field is a subtraction
2025 value, rather than an addition value
2026 2. IMAGE_REL_PPC_BRTAKEN, IMAGE_REL_PPC_BRNTAKEN - indicates that
2027 the branch is expected to be taken or not.
2028 3. IMAGE_REL_PPC_TOCDEFN - toc slot definition in the file
2029 For now, we just strip this stuff to find the type, and ignore it other
2032 reloc_howto_type
*howto
;
2033 unsigned short r_type
= EXTRACT_TYPE (internal
->r_type
);
2034 unsigned short r_flags
= EXTRACT_FLAGS(internal
->r_type
);
2035 unsigned short junk
= EXTRACT_JUNK (internal
->r_type
);
2037 /* the masking process only slices off the bottom byte for r_type. */
2038 if ( r_type
> MAX_RELOC_INDEX
)
2041 /* check for absolute crap */
2047 case IMAGE_REL_PPC_ADDR16
:
2048 case IMAGE_REL_PPC_REL24
:
2049 case IMAGE_REL_PPC_ADDR24
:
2050 case IMAGE_REL_PPC_ADDR32
:
2051 case IMAGE_REL_PPC_IFGLUE
:
2052 case IMAGE_REL_PPC_ADDR32NB
:
2053 case IMAGE_REL_PPC_SECTION
:
2054 case IMAGE_REL_PPC_SECREL
:
2055 DUMP_RELOC2(ppc_coff_howto_table
[r_type
].name
, internal
);
2056 howto
= ppc_coff_howto_table
+ r_type
;
2058 case IMAGE_REL_PPC_IMGLUE
:
2059 DUMP_RELOC2(ppc_coff_howto_table
[r_type
].name
, internal
);
2060 howto
= ppc_coff_howto_table
+ r_type
;
2062 case IMAGE_REL_PPC_TOCREL16
:
2063 DUMP_RELOC2(ppc_coff_howto_table
[r_type
].name
, internal
);
2064 if (r_flags
& IMAGE_REL_PPC_TOCDEFN
)
2065 howto
= ppc_coff_howto_table
+ IMAGE_REL_PPC_TOCREL16_DEFN
;
2067 howto
= ppc_coff_howto_table
+ IMAGE_REL_PPC_TOCREL16
;
2071 _("Warning: Unsupported reloc %s [%d] used -- it may not work.\n"),
2072 ppc_coff_howto_table
[r_type
].name
,
2074 howto
= ppc_coff_howto_table
+ r_type
;
2078 relent
->howto
= howto
;
2082 static reloc_howto_type
*
2083 coff_ppc_rtype_to_howto (abfd
, sec
, rel
, h
, sym
, addendp
)
2084 bfd
*abfd ATTRIBUTE_UNUSED
;
2086 struct internal_reloc
*rel
;
2087 struct coff_link_hash_entry
*h ATTRIBUTE_UNUSED
;
2088 struct internal_syment
*sym ATTRIBUTE_UNUSED
;
2091 reloc_howto_type
*howto
;
2093 /* We can encode one of three things in the type field, aside from the
2095 1. IMAGE_REL_PPC_NEG - indicates the value field is a subtraction
2096 value, rather than an addition value
2097 2. IMAGE_REL_PPC_BRTAKEN, IMAGE_REL_PPC_BRNTAKEN - indicates that
2098 the branch is expected to be taken or not.
2099 3. IMAGE_REL_PPC_TOCDEFN - toc slot definition in the file
2100 For now, we just strip this stuff to find the type, and ignore it other
2104 unsigned short r_type
= EXTRACT_TYPE (rel
->r_type
);
2105 unsigned short r_flags
= EXTRACT_FLAGS(rel
->r_type
);
2106 unsigned short junk
= EXTRACT_JUNK (rel
->r_type
);
2108 /* the masking process only slices off the bottom byte for r_type. */
2109 if ( r_type
> MAX_RELOC_INDEX
)
2112 /* check for absolute crap */
2118 case IMAGE_REL_PPC_ADDR32NB
:
2119 DUMP_RELOC2(ppc_coff_howto_table
[r_type
].name
, rel
);
2120 *addendp
-= pe_data(sec
->output_section
->owner
)->pe_opthdr
.ImageBase
;
2121 howto
= ppc_coff_howto_table
+ r_type
;
2123 case IMAGE_REL_PPC_TOCREL16
:
2124 DUMP_RELOC2(ppc_coff_howto_table
[r_type
].name
, rel
);
2125 if (r_flags
& IMAGE_REL_PPC_TOCDEFN
)
2126 howto
= ppc_coff_howto_table
+ IMAGE_REL_PPC_TOCREL16_DEFN
;
2128 howto
= ppc_coff_howto_table
+ IMAGE_REL_PPC_TOCREL16
;
2130 case IMAGE_REL_PPC_ADDR16
:
2131 case IMAGE_REL_PPC_REL24
:
2132 case IMAGE_REL_PPC_ADDR24
:
2133 case IMAGE_REL_PPC_ADDR32
:
2134 case IMAGE_REL_PPC_IFGLUE
:
2135 case IMAGE_REL_PPC_SECTION
:
2136 case IMAGE_REL_PPC_SECREL
:
2137 DUMP_RELOC2(ppc_coff_howto_table
[r_type
].name
, rel
);
2138 howto
= ppc_coff_howto_table
+ r_type
;
2140 case IMAGE_REL_PPC_IMGLUE
:
2141 DUMP_RELOC2(ppc_coff_howto_table
[r_type
].name
, rel
);
2142 howto
= ppc_coff_howto_table
+ r_type
;
2146 _("Warning: Unsupported reloc %s [%d] used -- it may not work.\n"),
2147 ppc_coff_howto_table
[r_type
].name
,
2149 howto
= ppc_coff_howto_table
+ r_type
;
2156 /* a cheesy little macro to make the code a little more readable */
2157 #define HOW2MAP(bfd_rtype,ppc_rtype) \
2158 case bfd_rtype: return &ppc_coff_howto_table[ppc_rtype]
2160 static reloc_howto_type
*ppc_coff_reloc_type_lookup
2161 PARAMS ((bfd
*, bfd_reloc_code_real_type
));
2163 static reloc_howto_type
*
2164 ppc_coff_reloc_type_lookup (abfd
, code
)
2165 bfd
*abfd ATTRIBUTE_UNUSED
;
2166 bfd_reloc_code_real_type code
;
2170 HOW2MAP(BFD_RELOC_32_GOTOFF
, IMAGE_REL_PPC_IMGLUE
);
2171 HOW2MAP(BFD_RELOC_16_GOT_PCREL
, IMAGE_REL_PPC_IFGLUE
);
2172 HOW2MAP(BFD_RELOC_16
, IMAGE_REL_PPC_ADDR16
);
2173 HOW2MAP(BFD_RELOC_PPC_B26
, IMAGE_REL_PPC_REL24
);
2174 HOW2MAP(BFD_RELOC_PPC_BA26
, IMAGE_REL_PPC_ADDR24
);
2175 HOW2MAP(BFD_RELOC_PPC_TOC16
, IMAGE_REL_PPC_TOCREL16
);
2176 HOW2MAP(BFD_RELOC_16_GOTOFF
, IMAGE_REL_PPC_TOCREL16_DEFN
);
2177 HOW2MAP(BFD_RELOC_32
, IMAGE_REL_PPC_ADDR32
);
2178 HOW2MAP(BFD_RELOC_RVA
, IMAGE_REL_PPC_ADDR32NB
);
2188 /* Tailor coffcode.h -- macro heaven. */
2190 #define RTYPE2HOWTO(cache_ptr, dst) ppc_coff_rtype2howto (cache_ptr, dst)
2192 #ifndef COFF_IMAGE_WITH_PE
2193 static void ppc_coff_swap_sym_in_hook
PARAMS ((bfd
*, PTR
, PTR
));
2196 /* We use the special COFF backend linker, with our own special touch. */
2198 #define coff_bfd_reloc_type_lookup ppc_coff_reloc_type_lookup
2199 #define coff_rtype_to_howto coff_ppc_rtype_to_howto
2200 #define coff_relocate_section coff_ppc_relocate_section
2201 #define coff_bfd_final_link ppc_bfd_coff_final_link
2203 #ifndef COFF_IMAGE_WITH_PE
2204 /* FIXME: This no longer works. */
2205 #define coff_swap_sym_in_hook ppc_coff_swap_sym_in_hook
2208 #define SELECT_RELOC(internal, howto) {internal.r_type=howto->type;}
2210 #define COFF_PAGE_SIZE 0x1000
2212 /* FIXME: This controls some code that used to be in peicode.h and is
2213 now in peigen.c. It will not control the code in peigen.c. If
2214 anybody wants to get this working, you will need to fix that. */
2215 #define POWERPC_LE_PE
2217 #define COFF_SECTION_ALIGNMENT_ENTRIES \
2218 { COFF_SECTION_NAME_EXACT_MATCH (".idata$2"), \
2219 COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 0 }, \
2220 { COFF_SECTION_NAME_EXACT_MATCH (".idata$3"), \
2221 COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 0 }, \
2222 { COFF_SECTION_NAME_EXACT_MATCH (".idata$4"), \
2223 COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 2 }, \
2224 { COFF_SECTION_NAME_EXACT_MATCH (".idata$5"), \
2225 COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 2 }, \
2226 { COFF_SECTION_NAME_EXACT_MATCH (".idata$6"), \
2227 COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 1 }, \
2228 { COFF_SECTION_NAME_EXACT_MATCH (".reloc"), \
2229 COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 1 }
2231 #include "coffcode.h"
2235 #ifndef COFF_IMAGE_WITH_PE
2237 What we're trying to do here is allocate a toc section (early), and attach
2238 it to the last bfd to be processed. This avoids the problem of having a toc
2239 written out before all files have been processed. This code allocates
2240 a toc section for every file, and records the last one seen. There are
2241 at least two problems with this approach:
2242 1. We allocate whole bunches of toc sections that are ignored, but at
2243 at least we will not allocate a toc if no .toc is present.
2244 2. It's not clear to me that being the last bfd read necessarily means
2245 that you are the last bfd closed.
2246 3. Doing it on a "swap in" hook depends on when the "swap in" is called,
2247 and how often, etc. It's not clear to me that there isn't a hole here.
2251 ppc_coff_swap_sym_in_hook (abfd
, ext1
, in1
)
2253 PTR ext1 ATTRIBUTE_UNUSED
;
2256 struct internal_syment
*in
= (struct internal_syment
*)in1
;
2258 if (bfd_of_toc_owner
!= 0) /* we already have a toc, so go home */
2261 if (strcmp(in
->_n
._n_name
, ".toc") == 0)
2264 register asection
*s
;
2266 s
= bfd_get_section_by_name ( abfd
, TOC_SECTION_NAME
);
2272 flags
= SEC_ALLOC
| SEC_LOAD
| SEC_HAS_CONTENTS
| SEC_IN_MEMORY
;
2274 s
= bfd_make_section (abfd
, TOC_SECTION_NAME
);
2277 || !bfd_set_section_flags (abfd
, s
, flags
)
2278 || !bfd_set_section_alignment (abfd
, s
, 2))
2280 /* FIXME: set appropriate bfd error */
2284 /* save the bfd for later allocation */
2285 bfd_of_toc_owner
= abfd
;
2292 #ifndef COFF_IMAGE_WITH_PE
2294 static boolean ppc_do_last
PARAMS ((bfd
*));
2295 static bfd
*ppc_get_last
PARAMS ((void));
2301 if (abfd
== bfd_of_toc_owner
)
2310 return bfd_of_toc_owner
;
2313 /* this piece of machinery exists only to guarantee that the bfd that holds
2314 the toc section is written last.
2316 This does depend on bfd_make_section attaching a new section to the
2317 end of the section list for the bfd.
2319 This is otherwise intended to be functionally the same as
2320 cofflink.c:_bfd_coff_final_link(). It is specifically different only
2321 where the POWERPC_LE_PE macro modifies the code. It is left in as a
2322 precise form of comment. krk@cygnus.com
2325 /* Do the final link step. */
2328 ppc_bfd_coff_final_link (abfd
, info
)
2330 struct bfd_link_info
*info
;
2332 bfd_size_type symesz
;
2333 struct coff_final_link_info finfo
;
2334 boolean debug_merge_allocated
;
2336 struct bfd_link_order
*p
;
2337 size_t max_sym_count
;
2338 size_t max_lineno_count
;
2339 size_t max_reloc_count
;
2340 size_t max_output_reloc_count
;
2341 size_t max_contents_size
;
2342 file_ptr rel_filepos
;
2344 file_ptr line_filepos
;
2345 unsigned int linesz
;
2347 bfd_byte
*external_relocs
= NULL
;
2348 char strbuf
[STRING_SIZE_SIZE
];
2350 symesz
= bfd_coff_symesz (abfd
);
2353 finfo
.output_bfd
= abfd
;
2354 finfo
.strtab
= NULL
;
2355 finfo
.section_info
= NULL
;
2356 finfo
.last_file_index
= -1;
2357 finfo
.last_bf_index
= -1;
2358 finfo
.internal_syms
= NULL
;
2359 finfo
.sec_ptrs
= NULL
;
2360 finfo
.sym_indices
= NULL
;
2361 finfo
.outsyms
= NULL
;
2362 finfo
.linenos
= NULL
;
2363 finfo
.contents
= NULL
;
2364 finfo
.external_relocs
= NULL
;
2365 finfo
.internal_relocs
= NULL
;
2366 debug_merge_allocated
= false;
2368 coff_data (abfd
)->link_info
= info
;
2370 finfo
.strtab
= _bfd_stringtab_init ();
2371 if (finfo
.strtab
== NULL
)
2374 if (! coff_debug_merge_hash_table_init (&finfo
.debug_merge
))
2376 debug_merge_allocated
= true;
2378 /* Compute the file positions for all the sections. */
2379 if (! abfd
->output_has_begun
)
2381 if (! bfd_coff_compute_section_file_positions (abfd
))
2385 /* Count the line numbers and relocation entries required for the
2386 output file. Set the file positions for the relocs. */
2387 rel_filepos
= obj_relocbase (abfd
);
2388 relsz
= bfd_coff_relsz (abfd
);
2389 max_contents_size
= 0;
2390 max_lineno_count
= 0;
2391 max_reloc_count
= 0;
2393 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
2396 o
->lineno_count
= 0;
2397 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
2400 if (p
->type
== bfd_indirect_link_order
)
2404 sec
= p
->u
.indirect
.section
;
2406 /* Mark all sections which are to be included in the
2407 link. This will normally be every section. We need
2408 to do this so that we can identify any sections which
2409 the linker has decided to not include. */
2410 sec
->linker_mark
= true;
2412 if (info
->strip
== strip_none
2413 || info
->strip
== strip_some
)
2414 o
->lineno_count
+= sec
->lineno_count
;
2416 if (info
->relocateable
)
2417 o
->reloc_count
+= sec
->reloc_count
;
2419 if (sec
->_raw_size
> max_contents_size
)
2420 max_contents_size
= sec
->_raw_size
;
2421 if (sec
->lineno_count
> max_lineno_count
)
2422 max_lineno_count
= sec
->lineno_count
;
2423 if (sec
->reloc_count
> max_reloc_count
)
2424 max_reloc_count
= sec
->reloc_count
;
2426 else if (info
->relocateable
2427 && (p
->type
== bfd_section_reloc_link_order
2428 || p
->type
== bfd_symbol_reloc_link_order
))
2431 if (o
->reloc_count
== 0)
2435 o
->flags
|= SEC_RELOC
;
2436 o
->rel_filepos
= rel_filepos
;
2437 rel_filepos
+= o
->reloc_count
* relsz
;
2441 /* If doing a relocateable link, allocate space for the pointers we
2443 if (info
->relocateable
)
2447 /* We use section_count + 1, rather than section_count, because
2448 the target_index fields are 1 based. */
2449 finfo
.section_info
=
2450 ((struct coff_link_section_info
*)
2451 bfd_malloc ((abfd
->section_count
+ 1)
2452 * sizeof (struct coff_link_section_info
)));
2453 if (finfo
.section_info
== NULL
)
2455 for (i
= 0; i
<= abfd
->section_count
; i
++)
2457 finfo
.section_info
[i
].relocs
= NULL
;
2458 finfo
.section_info
[i
].rel_hashes
= NULL
;
2462 /* We now know the size of the relocs, so we can determine the file
2463 positions of the line numbers. */
2464 line_filepos
= rel_filepos
;
2465 linesz
= bfd_coff_linesz (abfd
);
2466 max_output_reloc_count
= 0;
2467 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
2469 if (o
->lineno_count
== 0)
2470 o
->line_filepos
= 0;
2473 o
->line_filepos
= line_filepos
;
2474 line_filepos
+= o
->lineno_count
* linesz
;
2477 if (o
->reloc_count
!= 0)
2479 /* We don't know the indices of global symbols until we have
2480 written out all the local symbols. For each section in
2481 the output file, we keep an array of pointers to hash
2482 table entries. Each entry in the array corresponds to a
2483 reloc. When we find a reloc against a global symbol, we
2484 set the corresponding entry in this array so that we can
2485 fix up the symbol index after we have written out all the
2488 Because of this problem, we also keep the relocs in
2489 memory until the end of the link. This wastes memory,
2490 but only when doing a relocateable link, which is not the
2492 BFD_ASSERT (info
->relocateable
);
2493 finfo
.section_info
[o
->target_index
].relocs
=
2494 ((struct internal_reloc
*)
2495 bfd_malloc (o
->reloc_count
* sizeof (struct internal_reloc
)));
2496 finfo
.section_info
[o
->target_index
].rel_hashes
=
2497 ((struct coff_link_hash_entry
**)
2498 bfd_malloc (o
->reloc_count
2499 * sizeof (struct coff_link_hash_entry
*)));
2500 if (finfo
.section_info
[o
->target_index
].relocs
== NULL
2501 || finfo
.section_info
[o
->target_index
].rel_hashes
== NULL
)
2504 if (o
->reloc_count
> max_output_reloc_count
)
2505 max_output_reloc_count
= o
->reloc_count
;
2508 /* Reset the reloc and lineno counts, so that we can use them to
2509 count the number of entries we have output so far. */
2511 o
->lineno_count
= 0;
2514 obj_sym_filepos (abfd
) = line_filepos
;
2516 /* Figure out the largest number of symbols in an input BFD. Take
2517 the opportunity to clear the output_has_begun fields of all the
2520 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
2524 sub
->output_has_begun
= false;
2525 sz
= obj_raw_syment_count (sub
);
2526 if (sz
> max_sym_count
)
2530 /* Allocate some buffers used while linking. */
2531 finfo
.internal_syms
= ((struct internal_syment
*)
2532 bfd_malloc (max_sym_count
2533 * sizeof (struct internal_syment
)));
2534 finfo
.sec_ptrs
= (asection
**) bfd_malloc (max_sym_count
2535 * sizeof (asection
*));
2536 finfo
.sym_indices
= (long *) bfd_malloc (max_sym_count
* sizeof (long));
2537 finfo
.outsyms
= ((bfd_byte
*)
2538 bfd_malloc ((size_t) ((max_sym_count
+ 1) * symesz
)));
2539 finfo
.linenos
= (bfd_byte
*) bfd_malloc (max_lineno_count
2540 * bfd_coff_linesz (abfd
));
2541 finfo
.contents
= (bfd_byte
*) bfd_malloc (max_contents_size
);
2542 finfo
.external_relocs
= (bfd_byte
*) bfd_malloc (max_reloc_count
* relsz
);
2543 if (! info
->relocateable
)
2544 finfo
.internal_relocs
= ((struct internal_reloc
*)
2545 bfd_malloc (max_reloc_count
2546 * sizeof (struct internal_reloc
)));
2547 if ((finfo
.internal_syms
== NULL
&& max_sym_count
> 0)
2548 || (finfo
.sec_ptrs
== NULL
&& max_sym_count
> 0)
2549 || (finfo
.sym_indices
== NULL
&& max_sym_count
> 0)
2550 || finfo
.outsyms
== NULL
2551 || (finfo
.linenos
== NULL
&& max_lineno_count
> 0)
2552 || (finfo
.contents
== NULL
&& max_contents_size
> 0)
2553 || (finfo
.external_relocs
== NULL
&& max_reloc_count
> 0)
2554 || (! info
->relocateable
2555 && finfo
.internal_relocs
== NULL
2556 && max_reloc_count
> 0))
2559 /* We now know the position of everything in the file, except that
2560 we don't know the size of the symbol table and therefore we don't
2561 know where the string table starts. We just build the string
2562 table in memory as we go along. We process all the relocations
2563 for a single input file at once. */
2564 obj_raw_syment_count (abfd
) = 0;
2566 if (coff_backend_info (abfd
)->_bfd_coff_start_final_link
)
2568 if (! bfd_coff_start_final_link (abfd
, info
))
2572 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
2574 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
2576 if (p
->type
== bfd_indirect_link_order
2577 && (bfd_get_flavour (p
->u
.indirect
.section
->owner
)
2578 == bfd_target_coff_flavour
))
2580 sub
= p
->u
.indirect
.section
->owner
;
2581 #ifdef POWERPC_LE_PE
2582 if (! sub
->output_has_begun
&& !ppc_do_last(sub
))
2584 if (! sub
->output_has_begun
)
2587 if (! _bfd_coff_link_input_bfd (&finfo
, sub
))
2589 sub
->output_has_begun
= true;
2592 else if (p
->type
== bfd_section_reloc_link_order
2593 || p
->type
== bfd_symbol_reloc_link_order
)
2595 if (! _bfd_coff_reloc_link_order (abfd
, &finfo
, o
, p
))
2600 if (! _bfd_default_link_order (abfd
, info
, o
, p
))
2606 #ifdef POWERPC_LE_PE
2608 bfd
* last_one
= ppc_get_last();
2611 if (! _bfd_coff_link_input_bfd (&finfo
, last_one
))
2614 last_one
->output_has_begun
= true;
2618 /* Free up the buffers used by _bfd_coff_link_input_bfd. */
2620 coff_debug_merge_hash_table_free (&finfo
.debug_merge
);
2621 debug_merge_allocated
= false;
2623 if (finfo
.internal_syms
!= NULL
)
2625 free (finfo
.internal_syms
);
2626 finfo
.internal_syms
= NULL
;
2628 if (finfo
.sec_ptrs
!= NULL
)
2630 free (finfo
.sec_ptrs
);
2631 finfo
.sec_ptrs
= NULL
;
2633 if (finfo
.sym_indices
!= NULL
)
2635 free (finfo
.sym_indices
);
2636 finfo
.sym_indices
= NULL
;
2638 if (finfo
.linenos
!= NULL
)
2640 free (finfo
.linenos
);
2641 finfo
.linenos
= NULL
;
2643 if (finfo
.contents
!= NULL
)
2645 free (finfo
.contents
);
2646 finfo
.contents
= NULL
;
2648 if (finfo
.external_relocs
!= NULL
)
2650 free (finfo
.external_relocs
);
2651 finfo
.external_relocs
= NULL
;
2653 if (finfo
.internal_relocs
!= NULL
)
2655 free (finfo
.internal_relocs
);
2656 finfo
.internal_relocs
= NULL
;
2659 /* The value of the last C_FILE symbol is supposed to be the symbol
2660 index of the first external symbol. Write it out again if
2662 if (finfo
.last_file_index
!= -1
2663 && (unsigned int) finfo
.last_file
.n_value
!= obj_raw_syment_count (abfd
))
2665 finfo
.last_file
.n_value
= obj_raw_syment_count (abfd
);
2666 bfd_coff_swap_sym_out (abfd
, (PTR
) &finfo
.last_file
,
2667 (PTR
) finfo
.outsyms
);
2669 (obj_sym_filepos (abfd
)
2670 + finfo
.last_file_index
* symesz
),
2672 || bfd_write (finfo
.outsyms
, symesz
, 1, abfd
) != symesz
)
2676 /* Write out the global symbols. */
2677 finfo
.failed
= false;
2678 coff_link_hash_traverse (coff_hash_table (info
), _bfd_coff_write_global_sym
,
2683 /* The outsyms buffer is used by _bfd_coff_write_global_sym. */
2684 if (finfo
.outsyms
!= NULL
)
2686 free (finfo
.outsyms
);
2687 finfo
.outsyms
= NULL
;
2690 if (info
->relocateable
)
2692 /* Now that we have written out all the global symbols, we know
2693 the symbol indices to use for relocs against them, and we can
2694 finally write out the relocs. */
2695 external_relocs
= ((bfd_byte
*)
2696 bfd_malloc (max_output_reloc_count
* relsz
));
2697 if (external_relocs
== NULL
)
2700 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
2702 struct internal_reloc
*irel
;
2703 struct internal_reloc
*irelend
;
2704 struct coff_link_hash_entry
**rel_hash
;
2707 if (o
->reloc_count
== 0)
2710 irel
= finfo
.section_info
[o
->target_index
].relocs
;
2711 irelend
= irel
+ o
->reloc_count
;
2712 rel_hash
= finfo
.section_info
[o
->target_index
].rel_hashes
;
2713 erel
= external_relocs
;
2714 for (; irel
< irelend
; irel
++, rel_hash
++, erel
+= relsz
)
2716 if (*rel_hash
!= NULL
)
2718 BFD_ASSERT ((*rel_hash
)->indx
>= 0);
2719 irel
->r_symndx
= (*rel_hash
)->indx
;
2721 bfd_coff_swap_reloc_out (abfd
, (PTR
) irel
, (PTR
) erel
);
2724 if (bfd_seek (abfd
, o
->rel_filepos
, SEEK_SET
) != 0
2725 || bfd_write ((PTR
) external_relocs
, relsz
, o
->reloc_count
,
2726 abfd
) != relsz
* o
->reloc_count
)
2730 free (external_relocs
);
2731 external_relocs
= NULL
;
2734 /* Free up the section information. */
2735 if (finfo
.section_info
!= NULL
)
2739 for (i
= 0; i
< abfd
->section_count
; i
++)
2741 if (finfo
.section_info
[i
].relocs
!= NULL
)
2742 free (finfo
.section_info
[i
].relocs
);
2743 if (finfo
.section_info
[i
].rel_hashes
!= NULL
)
2744 free (finfo
.section_info
[i
].rel_hashes
);
2746 free (finfo
.section_info
);
2747 finfo
.section_info
= NULL
;
2750 /* If we have optimized stabs strings, output them. */
2751 if (coff_hash_table (info
)->stab_info
!= NULL
)
2753 if (! _bfd_write_stab_strings (abfd
, &coff_hash_table (info
)->stab_info
))
2757 /* Write out the string table. */
2758 if (obj_raw_syment_count (abfd
) != 0)
2761 (obj_sym_filepos (abfd
)
2762 + obj_raw_syment_count (abfd
) * symesz
),
2766 #if STRING_SIZE_SIZE == 4
2768 _bfd_stringtab_size (finfo
.strtab
) + STRING_SIZE_SIZE
,
2769 (bfd_byte
*) strbuf
);
2771 #error Change bfd_h_put_32
2774 if (bfd_write (strbuf
, 1, STRING_SIZE_SIZE
, abfd
) != STRING_SIZE_SIZE
)
2777 if (! _bfd_stringtab_emit (abfd
, finfo
.strtab
))
2781 _bfd_stringtab_free (finfo
.strtab
);
2783 /* Setting bfd_get_symcount to 0 will cause write_object_contents to
2784 not try to write out the symbols. */
2785 bfd_get_symcount (abfd
) = 0;
2790 if (debug_merge_allocated
)
2791 coff_debug_merge_hash_table_free (&finfo
.debug_merge
);
2792 if (finfo
.strtab
!= NULL
)
2793 _bfd_stringtab_free (finfo
.strtab
);
2794 if (finfo
.section_info
!= NULL
)
2798 for (i
= 0; i
< abfd
->section_count
; i
++)
2800 if (finfo
.section_info
[i
].relocs
!= NULL
)
2801 free (finfo
.section_info
[i
].relocs
);
2802 if (finfo
.section_info
[i
].rel_hashes
!= NULL
)
2803 free (finfo
.section_info
[i
].rel_hashes
);
2805 free (finfo
.section_info
);
2807 if (finfo
.internal_syms
!= NULL
)
2808 free (finfo
.internal_syms
);
2809 if (finfo
.sec_ptrs
!= NULL
)
2810 free (finfo
.sec_ptrs
);
2811 if (finfo
.sym_indices
!= NULL
)
2812 free (finfo
.sym_indices
);
2813 if (finfo
.outsyms
!= NULL
)
2814 free (finfo
.outsyms
);
2815 if (finfo
.linenos
!= NULL
)
2816 free (finfo
.linenos
);
2817 if (finfo
.contents
!= NULL
)
2818 free (finfo
.contents
);
2819 if (finfo
.external_relocs
!= NULL
)
2820 free (finfo
.external_relocs
);
2821 if (finfo
.internal_relocs
!= NULL
)
2822 free (finfo
.internal_relocs
);
2823 if (external_relocs
!= NULL
)
2824 free (external_relocs
);
2830 /* Forward declaration for use by alternative_target field. */
2831 #ifdef TARGET_BIG_SYM
2832 extern const bfd_target TARGET_BIG_SYM
;
2835 /* The transfer vectors that lead the outside world to all of the above. */
2837 #ifdef TARGET_LITTLE_SYM
2838 const bfd_target TARGET_LITTLE_SYM
=
2840 TARGET_LITTLE_NAME
, /* name or coff-arm-little */
2841 bfd_target_coff_flavour
,
2842 BFD_ENDIAN_LITTLE
, /* data byte order is little */
2843 BFD_ENDIAN_LITTLE
, /* header byte order is little */
2845 (HAS_RELOC
| EXEC_P
| /* FIXME: object flags */
2846 HAS_LINENO
| HAS_DEBUG
|
2847 HAS_SYMS
| HAS_LOCALS
| WP_TEXT
| D_PAGED
),
2849 #ifndef COFF_WITH_PE
2850 (SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
), /* section flags */
2852 (SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
/* section flags */
2853 | SEC_LINK_ONCE
| SEC_LINK_DUPLICATES
),
2856 0, /* leading char */
2857 '/', /* ar_pad_char */
2858 15, /* ar_max_namelen??? FIXMEmgo */
2860 bfd_getl64
, bfd_getl_signed_64
, bfd_putl64
,
2861 bfd_getl32
, bfd_getl_signed_32
, bfd_putl32
,
2862 bfd_getl16
, bfd_getl_signed_16
, bfd_putl16
, /* data */
2864 bfd_getl64
, bfd_getl_signed_64
, bfd_putl64
,
2865 bfd_getl32
, bfd_getl_signed_32
, bfd_putl32
,
2866 bfd_getl16
, bfd_getl_signed_16
, bfd_putl16
, /* hdrs */
2868 {_bfd_dummy_target
, coff_object_p
, /* bfd_check_format */
2869 bfd_generic_archive_p
, /* _bfd_dummy_target */ coff_object_p
},
2870 {bfd_false
, coff_mkobject
, _bfd_generic_mkarchive
, /* bfd_set_format */
2872 {bfd_false
, coff_write_object_contents
, /* bfd_write_contents */
2873 _bfd_write_archive_contents
, bfd_false
},
2875 BFD_JUMP_TABLE_GENERIC (coff
),
2876 BFD_JUMP_TABLE_COPY (coff
),
2877 BFD_JUMP_TABLE_CORE (_bfd_nocore
),
2878 BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_coff
),
2879 BFD_JUMP_TABLE_SYMBOLS (coff
),
2880 BFD_JUMP_TABLE_RELOCS (coff
),
2881 BFD_JUMP_TABLE_WRITE (coff
),
2882 BFD_JUMP_TABLE_LINK (coff
),
2883 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic
),
2885 /* Alternative_target. */
2886 #ifdef TARGET_BIG_SYM
2896 #ifdef TARGET_BIG_SYM
2897 const bfd_target TARGET_BIG_SYM
=
2900 bfd_target_coff_flavour
,
2901 BFD_ENDIAN_BIG
, /* data byte order is big */
2902 BFD_ENDIAN_BIG
, /* header byte order is big */
2904 (HAS_RELOC
| EXEC_P
| /* FIXME: object flags */
2905 HAS_LINENO
| HAS_DEBUG
|
2906 HAS_SYMS
| HAS_LOCALS
| WP_TEXT
| D_PAGED
),
2908 #ifndef COFF_WITH_PE
2909 (SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
), /* section flags */
2911 (SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
/* section flags */
2912 | SEC_LINK_ONCE
| SEC_LINK_DUPLICATES
),
2915 0, /* leading char */
2916 '/', /* ar_pad_char */
2917 15, /* ar_max_namelen??? FIXMEmgo */
2919 bfd_getb64
, bfd_getb_signed_64
, bfd_putb64
,
2920 bfd_getb32
, bfd_getb_signed_32
, bfd_putb32
,
2921 bfd_getb16
, bfd_getb_signed_16
, bfd_putb16
, /* data */
2923 bfd_getb64
, bfd_getb_signed_64
, bfd_putb64
,
2924 bfd_getb32
, bfd_getb_signed_32
, bfd_putb32
,
2925 bfd_getb16
, bfd_getb_signed_16
, bfd_putb16
, /* hdrs */
2927 {_bfd_dummy_target
, coff_object_p
, /* bfd_check_format */
2928 bfd_generic_archive_p
, /* _bfd_dummy_target */ coff_object_p
},
2929 {bfd_false
, coff_mkobject
, _bfd_generic_mkarchive
, /* bfd_set_format */
2931 {bfd_false
, coff_write_object_contents
, /* bfd_write_contents */
2932 _bfd_write_archive_contents
, bfd_false
},
2934 BFD_JUMP_TABLE_GENERIC (coff
),
2935 BFD_JUMP_TABLE_COPY (coff
),
2936 BFD_JUMP_TABLE_CORE (_bfd_nocore
),
2937 BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_coff
),
2938 BFD_JUMP_TABLE_SYMBOLS (coff
),
2939 BFD_JUMP_TABLE_RELOCS (coff
),
2940 BFD_JUMP_TABLE_WRITE (coff
),
2941 BFD_JUMP_TABLE_LINK (coff
),
2942 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic
),
2944 /* Alternative_target. */
2945 #ifdef TARGET_LITTLE_SYM
2946 & TARGET_LITTLE_SYM
,