1 /* BFD back-end for PowerPC Microsoft Portable Executable files.
2 Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1996 Free Software Foundation, Inc.
4 Original version pieced together by Kim Knuttila (krk@cygnus.com)
6 There is nothing new under the sun. This file draws a lot on other
7 coff files, in particular, those for the rs/6000, alpha, mips, and
8 intel backends, and the PE work for the arm.
10 This file is part of BFD, the Binary File Descriptor library.
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 2 of the License, or
15 (at your option) any later version.
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to the Free Software
24 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
28 - relocs generated by gas
29 - ld will link files, but they do not run.
30 - dlltool will not produce correct output in some .reloc cases, and will
31 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 /* The toc is a set of bfd_vma fields. We use the fact that valid */
55 /* addresses are even (i.e. the bit representing "1" is off) to allow */
56 /* us to encode a little extra information in the field */
57 /* - Unallocated addresses are intialized to 1. */
58 /* - Allocated addresses are even numbers. */
59 /* The first time we actually write a reference to the toc in the bfd, */
60 /* we want to record that fact in a fixup file (if it is asked for), so */
61 /* we keep track of whether or not an address has been written by marking */
62 /* the low order bit with a "1" upon writing */
64 #define SET_UNALLOCATED(x) ((x) = 1)
65 #define IS_UNALLOCATED(x) ((x) == 1)
67 #define IS_WRITTEN(x) ((x) & 1)
68 #define MARK_AS_WRITTEN(x) ((x) |= 1)
69 #define MAKE_ADDR_AGAIN(x) ((x) &= ~1)
71 /* In order not to add an int to every hash table item for every coff
72 linker, we define our own hash table, derived from the coff one */
74 /* PE linker hash table entries. */
76 struct ppc_coff_link_hash_entry
78 struct coff_link_hash_entry root
; /* First entry, as required */
80 /* As we wonder around the relocs, we'll keep the assigned toc_offset
82 bfd_vma toc_offset
; /* Our addition, as required */
84 unsigned long int glue_insn
;
88 /* Need a 7 char string for an eye catcher */
91 #define CHECK_EYE(addr) \
92 if (strcmp(addr, EYE) != 0) \
95 "File %s, line %d, Hash check failure, bad eye %8s\n", \
96 __FILE__, __LINE__, addr); \
100 /* PE linker hash table. */
102 struct ppc_coff_link_hash_table
104 struct coff_link_hash_table root
; /* First entry, as required */
107 static struct bfd_hash_entry
*ppc_coff_link_hash_newfunc
108 PARAMS ((struct bfd_hash_entry
*, struct bfd_hash_table
*,
111 /* Routine to create an entry in the link hash table. */
113 static struct bfd_hash_entry
*
114 ppc_coff_link_hash_newfunc (entry
, table
, string
)
115 struct bfd_hash_entry
*entry
;
116 struct bfd_hash_table
*table
;
119 struct ppc_coff_link_hash_entry
*ret
=
120 (struct ppc_coff_link_hash_entry
*) entry
;
122 /* Allocate the structure if it has not already been allocated by a
124 if (ret
== (struct ppc_coff_link_hash_entry
*) NULL
)
125 ret
= (struct ppc_coff_link_hash_entry
*)
126 bfd_hash_allocate (table
,
127 sizeof (struct ppc_coff_link_hash_entry
));
129 if (ret
== (struct ppc_coff_link_hash_entry
*) NULL
)
132 /* Call the allocation method of the superclass. */
133 ret
= ((struct ppc_coff_link_hash_entry
*)
134 _bfd_coff_link_hash_newfunc ((struct bfd_hash_entry
*) ret
,
139 /* Initialize the local fields. */
140 SET_UNALLOCATED(ret
->toc_offset
);
141 ret
->symbol_is_glue
= 0;
143 strcpy(ret
->eye_catcher
, EYE
);
146 return (struct bfd_hash_entry
*) ret
;
149 /* Initialize a PE linker hash table. */
152 ppc_coff_link_hash_table_init (table
, abfd
, newfunc
)
153 struct ppc_coff_link_hash_table
*table
;
155 struct bfd_hash_entry
*(*newfunc
) PARAMS ((struct bfd_hash_entry
*,
156 struct bfd_hash_table
*,
159 return _bfd_coff_link_hash_table_init (&table
->root
, abfd
, newfunc
);
162 /* Create a PE linker hash table. */
164 static struct bfd_link_hash_table
*
165 ppc_coff_link_hash_table_create (abfd
)
168 struct ppc_coff_link_hash_table
*ret
;
170 ret
= ((struct ppc_coff_link_hash_table
*)
171 bfd_alloc (abfd
, sizeof (struct ppc_coff_link_hash_table
)));
174 if (! ppc_coff_link_hash_table_init (ret
, abfd
,
175 ppc_coff_link_hash_newfunc
))
177 bfd_release (abfd
, ret
);
178 return (struct bfd_link_hash_table
*) NULL
;
180 return &ret
->root
.root
;
183 /* Now, tailor coffcode.h to use our hash stuff */
185 #define coff_bfd_link_hash_table_create ppc_coff_link_hash_table_create
188 /* The nt loader points the toc register to &toc + 32768, in order to */
189 /* use the complete range of a 16-bit displacement (I guess). We have */
190 /* to adjust for this when we fix up loads displaced off the toc reg. */
191 #define TOC_LOAD_ADJUSTMENT (-32768)
192 #define TOC_SECTION_NAME ".private.toc"
194 /* The main body of code is in coffcode.h. */
196 #define COFF_DEFAULT_SECTION_ALIGNMENT_POWER (3)
198 /* In case we're on a 32-bit machine, construct a 64-bit "-1" value
199 from smaller values. Start with zero, widen, *then* decrement. */
200 #define MINUS_ONE (((bfd_vma)0) - 1)
202 /* these should definitely go in a header file somewhere... */
205 #define IMAGE_REL_PPC_ABSOLUTE 0x0000
208 #define IMAGE_REL_PPC_ADDR64 0x0001
211 #define IMAGE_REL_PPC_ADDR32 0x0002
213 /* 26-bit address, shifted left 2 (branch absolute) */
214 #define IMAGE_REL_PPC_ADDR24 0x0003
217 #define IMAGE_REL_PPC_ADDR16 0x0004
219 /* 16-bit address, shifted left 2 (load doubleword) */
220 #define IMAGE_REL_PPC_ADDR14 0x0005
222 /* 26-bit PC-relative offset, shifted left 2 (branch relative) */
223 #define IMAGE_REL_PPC_REL24 0x0006
225 /* 16-bit PC-relative offset, shifted left 2 (br cond relative) */
226 #define IMAGE_REL_PPC_REL14 0x0007
228 /* 16-bit offset from TOC base */
229 #define IMAGE_REL_PPC_TOCREL16 0x0008
231 /* 16-bit offset from TOC base, shifted left 2 (load doubleword) */
232 #define IMAGE_REL_PPC_TOCREL14 0x0009
234 /* 32-bit addr w/o image base */
235 #define IMAGE_REL_PPC_ADDR32NB 0x000A
237 /* va of containing section (as in an image sectionhdr) */
238 #define IMAGE_REL_PPC_SECREL 0x000B
240 /* sectionheader number */
241 #define IMAGE_REL_PPC_SECTION 0x000C
243 /* substitute TOC restore instruction iff symbol is glue code */
244 #define IMAGE_REL_PPC_IFGLUE 0x000D
246 /* symbol is glue code; virtual address is TOC restore instruction */
247 #define IMAGE_REL_PPC_IMGLUE 0x000E
249 /* va of containing section (limited to 16 bits) */
250 #define IMAGE_REL_PPC_SECREL16 0x000F
252 /* stuff to handle immediate data when the number of bits in the */
253 /* data is greater than the number of bits in the immediate field */
254 /* We need to do (usually) 32 bit arithmetic on 16 bit chunks */
255 #define IMAGE_REL_PPC_REFHI 0x0010
256 #define IMAGE_REL_PPC_REFLO 0x0011
257 #define IMAGE_REL_PPC_PAIR 0x0012
259 /* This is essentially the same as tocrel16, with TOCDEFN assumed */
260 #define IMAGE_REL_PPC_TOCREL16_DEFN 0x0013
262 /* Flag bits in IMAGE_RELOCATION.TYPE */
264 /* subtract reloc value rather than adding it */
265 #define IMAGE_REL_PPC_NEG 0x0100
267 /* fix branch prediction bit to predict branch taken */
268 #define IMAGE_REL_PPC_BRTAKEN 0x0200
270 /* fix branch prediction bit to predict branch not taken */
271 #define IMAGE_REL_PPC_BRNTAKEN 0x0400
273 /* toc slot defined in file (or, data in toc) */
274 #define IMAGE_REL_PPC_TOCDEFN 0x0800
276 /* masks to isolate above values in IMAGE_RELOCATION.Type */
277 #define IMAGE_REL_PPC_TYPEMASK 0x00FF
278 #define IMAGE_REL_PPC_FLAGMASK 0x0F00
280 #define EXTRACT_TYPE(x) ((x) & IMAGE_REL_PPC_TYPEMASK)
281 #define EXTRACT_FLAGS(x) ((x) & IMAGE_REL_PPC_FLAGMASK)
282 #define EXTRACT_JUNK(x) \
283 ((x) & ~(IMAGE_REL_PPC_TYPEMASK | IMAGE_REL_PPC_FLAGMASK))
286 /* static helper functions to make relocation work */
287 /* (Work In Progress) */
289 static bfd_reloc_status_type ppc_refhi_reloc
PARAMS ((bfd
*abfd
,
296 static bfd_reloc_status_type ppc_reflo_reloc
PARAMS ((bfd
*abfd
,
303 static bfd_reloc_status_type ppc_pair_reloc
PARAMS ((bfd
*abfd
,
312 static bfd_reloc_status_type ppc_toc16_reloc
PARAMS ((bfd
*abfd
,
320 static bfd_reloc_status_type ppc_addr32nb_reloc
PARAMS ((bfd
*abfd
,
328 static bfd_reloc_status_type ppc_section_reloc
PARAMS ((bfd
*abfd
,
336 static bfd_reloc_status_type ppc_secrel_reloc
PARAMS ((bfd
*abfd
,
344 static bfd_reloc_status_type ppc_imglue_reloc
PARAMS ((bfd
*abfd
,
354 static boolean in_reloc_p
PARAMS((bfd
*abfd
, reloc_howto_type
*howto
));
357 /* FIXME: It'll take a while to get through all of these. I only need a few to
358 get us started, so those I'll make sure work. Those marked FIXME are either
359 completely unverified or have a specific unknown marked in the comment */
361 /*---------------------------------------------------------------------------*/
363 /* Relocation entries for Windows/NT on PowerPC. */
365 /* From the document "" we find the following listed as used relocs: */
367 /* ABSOLUTE : The noop */
368 /* ADDR[64|32|16] : fields that hold addresses in data fields or the */
369 /* 16 bit displacement field on a load/store. */
370 /* ADDR[24|14] : fields that hold addresses in branch and cond */
371 /* branches. These represent [26|16] bit addresses. */
372 /* The low order 2 bits are preserved. */
373 /* REL[24|14] : branches relative to the Instruction Address */
374 /* register. These represent [26|16] bit addresses, */
375 /* as before. The instruction field will be zero, and */
376 /* the address of the SYM will be inserted at link time. */
377 /* TOCREL16 : 16 bit displacement field referring to a slot in */
379 /* TOCREL14 : 16 bit displacement field, similar to REL14 or ADDR14. */
380 /* ADDR32NB : 32 bit address relative to the virtual origin. */
381 /* (On the alpha, this is always a linker generated thunk)*/
382 /* (i.e. 32bit addr relative to the image base) */
383 /* SECREL : The value is relative to the start of the section */
384 /* containing the symbol. */
385 /* SECTION : access to the header containing the item. Supports the */
386 /* codeview debugger. */
388 /* In particular, note that the document does not indicate that the */
389 /* relocations listed in the header file are used. */
393 /*---------------------------------------------------------------------------*/
395 static reloc_howto_type ppc_coff_howto_table
[] =
397 /* IMAGE_REL_PPC_ABSOLUTE 0x0000 NOP */
399 HOWTO (IMAGE_REL_PPC_ABSOLUTE
, /* type */
401 0, /* size (0 = byte, 1 = short, 2 = long) */
403 false, /* pc_relative */
405 complain_overflow_dont
, /* dont complain_on_overflow */
406 0, /* special_function */
407 "ABSOLUTE", /* name */
408 false, /* partial_inplace */
411 false), /* pcrel_offset */
413 /* IMAGE_REL_PPC_ADDR64 0x0001 64-bit address */
415 HOWTO(IMAGE_REL_PPC_ADDR64
, /* type */
417 3, /* size (0 = byte, 1 = short, 2 = long) */
419 false, /* pc_relative */
421 complain_overflow_bitfield
, /* complain_on_overflow */
422 0, /* special_function */
424 true, /* partial_inplace */
425 MINUS_ONE
, /* src_mask */
426 MINUS_ONE
, /* dst_mask */
427 false), /* pcrel_offset */
429 /* IMAGE_REL_PPC_ADDR32 0x0002 32-bit address */
431 HOWTO (IMAGE_REL_PPC_ADDR32
, /* type */
433 2, /* size (0 = byte, 1 = short, 2 = long) */
435 false, /* pc_relative */
437 complain_overflow_bitfield
, /* complain_on_overflow */
438 0, /* special_function */
440 true, /* partial_inplace */
441 0xffffffff, /* src_mask */
442 0xffffffff, /* dst_mask */
443 false), /* pcrel_offset */
445 /* IMAGE_REL_PPC_ADDR24 0x0003 26-bit address, shifted left 2 (branch absolute) */
446 /* the LI field is in bit 6 through bit 29 is 24 bits, + 2 for the shift */
447 /* Of course, That's the IBM approved bit numbering, which is not what */
448 /* anyone else uses.... The li field is in bit 2 thru 25 */
450 HOWTO (IMAGE_REL_PPC_ADDR24
, /* type */
452 2, /* size (0 = byte, 1 = short, 2 = long) */
454 false, /* pc_relative */
456 complain_overflow_bitfield
, /* complain_on_overflow */
457 0, /* special_function */
459 true, /* partial_inplace */
460 0x07fffffc, /* src_mask */
461 0x07fffffc, /* dst_mask */
462 false), /* pcrel_offset */
464 /* IMAGE_REL_PPC_ADDR16 0x0004 16-bit address */
466 HOWTO (IMAGE_REL_PPC_ADDR16
, /* type */
468 1, /* size (0 = byte, 1 = short, 2 = long) */
470 false, /* pc_relative */
472 complain_overflow_signed
, /* complain_on_overflow */
473 0, /* special_function */
475 true, /* partial_inplace */
476 0xffff, /* src_mask */
477 0xffff, /* dst_mask */
478 false), /* pcrel_offset */
480 /* IMAGE_REL_PPC_ADDR14 0x0005 */
481 /* 16-bit address, shifted left 2 (load doubleword) */
482 /* FIXME: the mask is likely wrong, and the bit position may be as well */
484 HOWTO (IMAGE_REL_PPC_ADDR14
, /* type */
486 1, /* size (0 = byte, 1 = short, 2 = long) */
488 false, /* pc_relative */
490 complain_overflow_signed
, /* complain_on_overflow */
491 0, /* special_function */
493 true, /* partial_inplace */
494 0xffff, /* src_mask */
495 0xffff, /* dst_mask */
496 false), /* pcrel_offset */
498 /* IMAGE_REL_PPC_REL24 0x0006 */
499 /* 26-bit PC-relative offset, shifted left 2 (branch relative) */
501 HOWTO (IMAGE_REL_PPC_REL24
, /* type */
503 2, /* size (0 = byte, 1 = short, 2 = long) */
505 true, /* pc_relative */
507 complain_overflow_signed
, /* complain_on_overflow */
508 0, /* special_function */
510 true, /* partial_inplace */
511 0x3fffffc, /* src_mask */
512 0x3fffffc, /* dst_mask */
513 false), /* pcrel_offset */
515 /* IMAGE_REL_PPC_REL14 0x0007 */
516 /* 16-bit PC-relative offset, shifted left 2 (br cond relative) */
517 /* FIXME: the mask is likely wrong, and the bit position may be as well */
518 /* FIXME: how does it know how far to shift? */
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 true), /* pcrel_offset */
534 /* IMAGE_REL_PPC_TOCREL16 0x0008 */
535 /* 16-bit offset from TOC base */
537 HOWTO (IMAGE_REL_PPC_TOCREL16
,/* type */
539 1, /* size (0 = byte, 1 = short, 2 = long) */
541 false, /* pc_relative */
543 complain_overflow_dont
, /* complain_on_overflow */
544 ppc_toc16_reloc
, /* special_function */
545 "TOCREL16", /* name */
546 false, /* partial_inplace */
547 0xffff, /* src_mask */
548 0xffff, /* dst_mask */
549 false), /* pcrel_offset */
551 /* IMAGE_REL_PPC_TOCREL14 0x0009 */
552 /* 16-bit offset from TOC base, shifted left 2 (load doubleword) */
554 HOWTO (IMAGE_REL_PPC_TOCREL14
,/* type */
556 1, /* size (0 = byte, 1 = short, 2 = long) */
558 false, /* pc_relative */
560 complain_overflow_signed
, /* complain_on_overflow */
561 0, /* special_function */
562 "TOCREL14", /* name */
563 false, /* partial_inplace */
564 0xffff, /* src_mask */
565 0xffff, /* dst_mask */
566 false), /* pcrel_offset */
568 /* IMAGE_REL_PPC_ADDR32NB 0x000A */
569 /* 32-bit addr w/ image base */
571 HOWTO (IMAGE_REL_PPC_ADDR32NB
,/* type */
573 2, /* size (0 = byte, 1 = short, 2 = long) */
575 false, /* pc_relative */
577 complain_overflow_signed
, /* complain_on_overflow */
578 0, /* special_function */
579 "ADDR32NB", /* name */
580 true, /* partial_inplace */
581 0xffffffff, /* src_mask */
582 0xffffffff, /* dst_mask */
583 false), /* pcrel_offset */
585 /* IMAGE_REL_PPC_SECREL 0x000B */
586 /* va of containing section (as in an image sectionhdr) */
588 HOWTO (IMAGE_REL_PPC_SECREL
,/* type */
590 2, /* size (0 = byte, 1 = short, 2 = long) */
592 false, /* pc_relative */
594 complain_overflow_signed
, /* complain_on_overflow */
595 ppc_secrel_reloc
, /* special_function */
597 true, /* partial_inplace */
598 0xffffffff, /* src_mask */
599 0xffffffff, /* dst_mask */
600 true), /* pcrel_offset */
602 /* IMAGE_REL_PPC_SECTION 0x000C */
603 /* sectionheader number */
605 HOWTO (IMAGE_REL_PPC_SECTION
,/* type */
607 2, /* size (0 = byte, 1 = short, 2 = long) */
609 false, /* pc_relative */
611 complain_overflow_signed
, /* complain_on_overflow */
612 ppc_section_reloc
, /* special_function */
613 "SECTION", /* name */
614 true, /* partial_inplace */
615 0xffffffff, /* src_mask */
616 0xffffffff, /* dst_mask */
617 true), /* pcrel_offset */
619 /* IMAGE_REL_PPC_IFGLUE 0x000D */
620 /* substitute TOC restore instruction iff symbol is glue code */
622 HOWTO (IMAGE_REL_PPC_IFGLUE
,/* type */
624 2, /* size (0 = byte, 1 = short, 2 = long) */
626 false, /* pc_relative */
628 complain_overflow_signed
, /* complain_on_overflow */
629 0, /* special_function */
631 true, /* partial_inplace */
632 0xffffffff, /* src_mask */
633 0xffffffff, /* dst_mask */
634 false), /* pcrel_offset */
636 /* IMAGE_REL_PPC_IMGLUE 0x000E */
637 /* symbol is glue code; virtual address is TOC restore instruction */
639 HOWTO (IMAGE_REL_PPC_IMGLUE
,/* type */
641 2, /* size (0 = byte, 1 = short, 2 = long) */
643 false, /* pc_relative */
645 complain_overflow_dont
, /* complain_on_overflow */
646 ppc_imglue_reloc
, /* special_function */
648 false, /* partial_inplace */
649 0xffffffff, /* src_mask */
650 0xffffffff, /* dst_mask */
651 false), /* pcrel_offset */
653 /* IMAGE_REL_PPC_SECREL16 0x000F */
654 /* va of containing section (limited to 16 bits) */
656 HOWTO (IMAGE_REL_PPC_SECREL16
,/* type */
658 1, /* size (0 = byte, 1 = short, 2 = long) */
660 false, /* pc_relative */
662 complain_overflow_signed
, /* complain_on_overflow */
663 0, /* special_function */
664 "SECREL16", /* name */
665 true, /* partial_inplace */
666 0xffff, /* src_mask */
667 0xffff, /* dst_mask */
668 true), /* pcrel_offset */
670 /* IMAGE_REL_PPC_REFHI 0x0010 */
672 HOWTO (IMAGE_REL_PPC_REFHI
, /* type */
674 1, /* size (0 = byte, 1 = short, 2 = long) */
676 false, /* pc_relative */
678 complain_overflow_signed
, /* complain_on_overflow */
679 ppc_refhi_reloc
, /* special_function */
681 true, /* partial_inplace */
682 0xffffffff, /* src_mask */
683 0xffffffff, /* dst_mask */
684 false), /* pcrel_offset */
686 /* IMAGE_REL_PPC_REFLO 0x0011 */
688 HOWTO (IMAGE_REL_PPC_REFLO
, /* type */
690 1, /* size (0 = byte, 1 = short, 2 = long) */
692 false, /* pc_relative */
694 complain_overflow_signed
, /* complain_on_overflow */
695 ppc_refhi_reloc
, /* special_function */
697 true, /* partial_inplace */
698 0xffffffff, /* src_mask */
699 0xffffffff, /* dst_mask */
700 false), /* pcrel_offset */
702 /* IMAGE_REL_PPC_PAIR 0x0012 */
704 HOWTO (IMAGE_REL_PPC_PAIR
, /* type */
706 1, /* size (0 = byte, 1 = short, 2 = long) */
708 false, /* pc_relative */
710 complain_overflow_signed
, /* complain_on_overflow */
711 ppc_pair_reloc
, /* special_function */
713 true, /* partial_inplace */
714 0xffffffff, /* src_mask */
715 0xffffffff, /* dst_mask */
716 false), /* pcrel_offset */
718 /* IMAGE_REL_PPC_TOCREL16_DEFN 0x0013 */
719 /* 16-bit offset from TOC base, without causing a definition */
721 HOWTO ( (IMAGE_REL_PPC_TOCREL16
| IMAGE_REL_PPC_TOCDEFN
), /* type */
723 1, /* size (0 = byte, 1 = short, 2 = long) */
725 false, /* pc_relative */
727 complain_overflow_dont
, /* complain_on_overflow */
728 0, /* special_function */
729 "TOCREL16, TOCDEFN", /* name */
730 false, /* partial_inplace */
731 0xffff, /* src_mask */
732 0xffff, /* dst_mask */
733 false), /* pcrel_offset */
740 /* Some really cheezy macros that can be turned on to test stderr :-) */
749 fprintf(stderr,"Unimplemented Relocation -- %s\n",x); \
753 #define DUMP_RELOC(n,r) \
755 fprintf(stderr,"%s sym %d, addr %d, addend %d\n", \
756 n, (*(r->sym_ptr_ptr))->name, \
757 r->address, r->addend); \
760 /* Given a reloc name, n, and a pointer to an internal_reloc,
761 dump out interesting information on the contents
763 #define n_name _n._n_name
764 #define n_zeroes _n._n_n._n_zeroes
765 #define n_offset _n._n_n._n_offset
769 #define DUMP_RELOC2(n,r) \
771 fprintf(stderr,"%s sym %d, r_vaddr %d %s\n", \
772 n, r->r_symndx, r->r_vaddr,\
773 (((r->r_type) & IMAGE_REL_PPC_TOCDEFN) == 0) \
779 #define DUMP_RELOC(n,r)
780 #define DUMP_RELOC2(n,r)
785 /* toc construction and management routines */
786 extern bfd
* bfd_of_toc_owner
;
787 extern long int global_toc_size
;
789 extern long int import_table_size
;
790 extern long int first_thunk_address
;
791 extern long int thunk_size
;
809 struct list_ele
*next
;
811 enum ref_category cat
;
816 extern struct list_ele
*head
;
817 extern struct list_ele
*tail
;
820 record_toc(toc_section
, our_toc_offset
, cat
, name
)
821 asection
*toc_section
;
823 enum ref_category cat
;
826 /* add this entry to our toc addr-offset-name list */
828 t
= bfd_malloc (sizeof (struct list_ele
));
832 t
->offset
= our_toc_offset
;
835 t
->addr
= toc_section
->output_offset
+ our_toc_offset
;
849 /* record a toc offset against a symbol */
851 ppc_record_toc_entry(abfd
, info
, sec
, sym
, toc_kind
)
853 struct bfd_link_info
*info
;
856 enum toc_type toc_kind
;
859 bfd_byte
*old_contents
;
864 struct ppc_coff_link_hash_entry
*h
;
865 struct coff_symbol_struct
*target
;
873 h
= (struct ppc_coff_link_hash_entry
*) (obj_coff_sym_hashes (abfd
)[sym
]);
876 CHECK_EYE(h
->eye_catcher
);
881 local_syms
= obj_coff_local_toc_table(abfd
);
885 /* allocate a table */
887 (int *) bfd_zalloc (abfd
,
888 obj_raw_syment_count(abfd
) * sizeof(int));
891 obj_coff_local_toc_table(abfd
) = local_syms
;
892 for (i
= 0; i
< obj_raw_syment_count(abfd
); ++i
)
894 SET_UNALLOCATED(local_syms
[i
]);
898 if (IS_UNALLOCATED(local_syms
[sym
]))
900 local_syms
[sym
] = global_toc_size
;
901 ret_val
= global_toc_size
;
902 global_toc_size
+= 4;
904 /* The size must fit in a 16bit displacment */
905 if (global_toc_size
>= 65535)
908 "Exceeded toc size of 65535\n");
914 "Setting toc_offset for local sym %d to %d\n",
920 ret_val
= local_syms
[sym
];
923 "toc_offset already set for local sym %d to %d\n",
930 name
= h
->root
.root
.root
.string
;
932 /* check to see if there's a toc slot allocated. If not, do it
933 here. It will be used in relocate_section */
934 if (IS_UNALLOCATED(h
->toc_offset
))
936 h
->toc_offset
= global_toc_size
;
937 ret_val
= global_toc_size
;
938 global_toc_size
+= 4;
940 /* The size must fit in a 16bit displacment */
941 if (global_toc_size
>= 65535)
944 "Exceeded toc size of 65535\n");
950 "Setting toc_offset for sym %d (%s) [h=%p] to %d\n",
951 sym
, name
, h
, ret_val
);
956 ret_val
= h
->toc_offset
;
959 "toc_offset already set for sym %d (%s) [h=%p] to %d\n",
960 sym
, name
, h
, ret_val
);
967 /* FIXME: record a toc offset against a data-in-toc symbol */
968 /* Now, there is currenly some confusion on what this means. In some
969 compilers one sees the moral equivalent of:
973 refer to the data with a [tocv] qualifier
974 In general, one sees something to indicate that a tocd has been
975 seen, and that would trigger the allocation of data in toc. The IBM
976 docs seem to suggest that anything with the TOCDEFN qualifier should
977 never trigger storage allocation. However, in the kernel32.lib that
978 we've been using for our test bed, there are a couple of variables
979 referenced that fail that test.
981 So it can't work that way.
984 ppc_record_data_in_toc_entry(abfd
, info
, sec
, sym
, toc_kind
)
986 struct bfd_link_info
*info
;
989 enum toc_type toc_kind
;
992 bfd_byte
*old_contents
;
997 struct ppc_coff_link_hash_entry
*h
= 0;
998 struct coff_symbol_struct
*target
;
1004 h
= (struct ppc_coff_link_hash_entry
*) (obj_coff_sym_hashes (abfd
)[sym
]);
1008 local_syms
= obj_coff_local_toc_table(abfd
);
1009 if (local_syms
== 0)
1012 /* allocate a table */
1014 (int *) bfd_zalloc (abfd
,
1015 obj_raw_syment_count(abfd
) * sizeof(int));
1016 if (local_syms
== 0)
1018 obj_coff_local_toc_table(abfd
) = local_syms
;
1019 for (i
= 0; i
< obj_raw_syment_count(abfd
); ++i
)
1021 SET_UNALLOCATED(local_syms
[i
]);
1025 if (IS_UNALLOCATED(local_syms
[sym
]))
1027 local_syms
[sym
] = global_toc_size
;
1028 ret_val
= global_toc_size
;
1029 global_toc_size
+= 4;
1032 "Setting data_in_toc_offset for local sym %d to %d\n",
1038 ret_val
= local_syms
[sym
];
1041 "data_in_toc_offset already set for local sym %d to %d\n",
1048 CHECK_EYE(h
->eye_catcher
);
1050 name
= h
->root
.root
.root
.string
;
1052 /* check to see if there's a toc slot allocated. If not, do it
1053 here. It will be used in relocate_section */
1054 if (IS_UNALLOCATED(h
->toc_offset
))
1057 h
->toc_offset
= global_toc_size
;
1059 ret_val
= global_toc_size
;
1060 /* We're allocating a chunk of the toc, as opposed to a slot */
1061 /* FIXME: alignment? */
1063 global_toc_size
+= 4;
1066 "Setting data_in_toc_offset for sym %d (%s) [h=%p] to %d\n",
1067 sym
, name
, h
, ret_val
);
1072 ret_val
= h
->toc_offset
;
1075 "data_in_toc_offset already set for sym %d (%s) [h=%p] to %d\n",
1076 sym
, name
, h
, ret_val
);
1084 /* record a toc offset against a symbol */
1086 ppc_mark_symbol_as_glue(abfd
, sym
, rel
)
1089 struct internal_reloc
*rel
;
1091 struct ppc_coff_link_hash_entry
*h
;
1093 h
= (struct ppc_coff_link_hash_entry
*) (obj_coff_sym_hashes (abfd
)[sym
]);
1095 CHECK_EYE(h
->eye_catcher
);
1097 h
->symbol_is_glue
= 1;
1098 h
->glue_insn
= bfd_get_32 (abfd
, (bfd_byte
*) &rel
->r_vaddr
);
1104 /* Provided the symbol, returns the value reffed */
1105 static long get_symbol_value
PARAMS ((asymbol
*));
1108 get_symbol_value (symbol
)
1111 long relocation
= 0;
1113 if (bfd_is_com_section (symbol
->section
))
1119 relocation
= symbol
->value
+
1120 symbol
->section
->output_section
->vma
+
1121 symbol
->section
->output_offset
;
1127 /* Return true if this relocation should
1128 appear in the output .reloc section. */
1130 static boolean
in_reloc_p(abfd
, howto
)
1132 reloc_howto_type
*howto
;
1135 (! howto
->pc_relative
)
1136 && (howto
->type
!= IMAGE_REL_PPC_ADDR32NB
)
1137 && (howto
->type
!= IMAGE_REL_PPC_TOCREL16
)
1138 && (howto
->type
!= IMAGE_REL_PPC_IMGLUE
)
1139 && (howto
->type
!= IMAGE_REL_PPC_IFGLUE
)
1140 && (howto
->type
!= IMAGE_REL_PPC_SECREL
)
1141 && (howto
->type
!= IMAGE_REL_PPC_SECTION
)
1142 && (howto
->type
!= IMAGE_REL_PPC_SECREL16
)
1143 && (howto
->type
!= IMAGE_REL_PPC_REFHI
)
1144 && (howto
->type
!= IMAGE_REL_PPC_REFLO
)
1145 && (howto
->type
!= IMAGE_REL_PPC_PAIR
)
1146 && (howto
->type
!= IMAGE_REL_PPC_TOCREL16_DEFN
) ;
1149 /* this function is in charge of performing all the ppc PE relocations */
1150 /* Don't yet know if we want to do this this particular way ... (krk) */
1151 /* FIXME: (it is not yet enabled) */
1153 static bfd_reloc_status_type
1154 pe_ppc_reloc (abfd
, reloc_entry
, symbol_in
, data
, input_section
, output_bfd
,
1157 arelent
*reloc_entry
;
1160 asection
*input_section
;
1162 char **error_message
;
1164 /* the consth relocation comes in two parts, we have to remember
1165 the state between calls, in these variables */
1166 static boolean part1_consth_active
= false;
1167 static unsigned long part1_consth_value
;
1170 unsigned long sym_value
;
1171 unsigned long unsigned_value
;
1172 unsigned short r_type
;
1175 unsigned long addr
= reloc_entry
->address
; /*+ input_section->vma*/
1176 bfd_byte
*hit_data
=addr
+ (bfd_byte
*)(data
);
1178 fprintf(stderr
, "pe_ppc_reloc (%s)\n", TARGET_LITTLE_NAME
);
1180 r_type
= reloc_entry
->howto
->type
;
1184 /* Partial linking - do nothing */
1185 reloc_entry
->address
+= input_section
->output_offset
;
1186 return bfd_reloc_ok
;
1189 if (symbol_in
!= NULL
1190 && bfd_is_und_section (symbol_in
->section
))
1192 /* Keep the state machine happy in case we're called again */
1193 if (r_type
== IMAGE_REL_PPC_REFHI
)
1195 part1_consth_active
= true;
1196 part1_consth_value
= 0;
1198 return(bfd_reloc_undefined
);
1201 if ((part1_consth_active
) && (r_type
!= IMAGE_REL_PPC_PAIR
))
1203 part1_consth_active
= false;
1204 *error_message
= (char *) "Missing PAIR";
1205 return(bfd_reloc_dangerous
);
1209 sym_value
= get_symbol_value(symbol_in
);
1211 return(bfd_reloc_ok
);
1214 /* The reloc processing routine for the optimized COFF linker. */
1217 coff_ppc_relocate_section (output_bfd
, info
, input_bfd
, input_section
,
1218 contents
, relocs
, syms
, sections
)
1220 struct bfd_link_info
*info
;
1222 asection
*input_section
;
1224 struct internal_reloc
*relocs
;
1225 struct internal_syment
*syms
;
1226 asection
**sections
;
1228 struct internal_reloc
*rel
;
1229 struct internal_reloc
*relend
;
1232 asection
*toc_section
= 0;
1234 reloc_howto_type
*howto
= 0;
1238 "pe_ppc_relocate_section (%s) for %s in bfd %s\n",
1240 input_section
->name
,
1241 input_bfd
->filename
);
1245 /* If we are performing a relocateable link, we don't need to do a
1246 thing. The caller will take care of adjusting the reloc
1247 addresses and symbol indices. */
1248 if (info
->relocateable
)
1255 relend
= rel
+ input_section
->reloc_count
;
1256 for (; rel
< relend
; rel
++)
1259 struct ppc_coff_link_hash_entry
*h
;
1260 struct internal_syment
*sym
;
1264 bfd_reloc_status_type rstat
;
1267 unsigned short r_type
= EXTRACT_TYPE (rel
->r_type
);
1268 unsigned short r_flags
= EXTRACT_FLAGS(rel
->r_type
);
1269 unsigned short junk
= EXTRACT_JUNK (rel
->r_type
);
1272 /* now examine flags */
1275 fprintf (stderr
, "Reloc with flags found!");
1276 if ( r_flags
& IMAGE_REL_PPC_NEG
)
1277 fprintf (stderr
, " NEG");
1278 if ( r_flags
& IMAGE_REL_PPC_BRTAKEN
)
1279 fprintf (stderr
, " BRTAKEN");
1280 if ( r_flags
& IMAGE_REL_PPC_BRNTAKEN
)
1281 fprintf (stderr
, " BRNTAKEN");
1282 if ( r_flags
& IMAGE_REL_PPC_TOCDEFN
)
1283 fprintf (stderr
, " TOCDEFN");
1284 fprintf(stderr
, "\n");
1288 symndx
= rel
->r_symndx
;
1289 loc
= contents
+ rel
->r_vaddr
- input_section
->vma
;
1291 /* FIXME: check bounds on r_type */
1292 howto
= ppc_coff_howto_table
+ r_type
;
1301 h
= (struct ppc_coff_link_hash_entry
*)
1302 (obj_coff_sym_hashes (input_bfd
)[symndx
]);
1305 CHECK_EYE(h
->eye_catcher
);
1308 sym
= syms
+ symndx
;
1314 /* FIXME: PAIR unsupported in the following code */
1318 sec
= bfd_abs_section_ptr
;
1321 sec
= sections
[symndx
];
1322 val
= (sec
->output_section
->vma
1323 + sec
->output_offset
1330 CHECK_EYE(h
->eye_catcher
);
1332 if (h
->root
.root
.type
== bfd_link_hash_defined
1333 || h
->root
.root
.type
== bfd_link_hash_defweak
)
1335 sec
= h
->root
.root
.u
.def
.section
;
1336 val
= (h
->root
.root
.u
.def
.value
1337 + sec
->output_section
->vma
1338 + sec
->output_offset
);
1343 "missing %s\n",h
->root
.root
.root
.string
);
1344 if (! ((*info
->callbacks
->undefined_symbol
)
1345 (info
, h
->root
.root
.root
.string
, input_bfd
, input_section
,
1346 rel
->r_vaddr
- input_section
->vma
)))
1351 rstat
= bfd_reloc_ok
;
1353 /* Each case must do its own relocation, setting rstat appropriately */
1358 "ERROR: during reloc processing -- unsupported reloc %s\n",
1360 bfd_set_error (bfd_error_bad_value
);
1363 case IMAGE_REL_PPC_TOCREL16
:
1365 bfd_vma our_toc_offset
;
1368 DUMP_RELOC2(howto
->name
, rel
);
1370 if (toc_section
== 0)
1372 toc_section
= bfd_get_section_by_name (bfd_of_toc_owner
,
1377 "BFD of toc owner %p (%s), section addr of %s %p\n",
1378 bfd_of_toc_owner
, bfd_of_toc_owner
->filename
,
1379 TOC_SECTION_NAME
, toc_section
);
1382 if ( toc_section
== NULL
)
1384 fprintf(stderr
, "No Toc section!\n");
1390 * Amazing bit tricks present. As we may have seen earlier, we
1391 * use the 1 bit to tell us whether or not a toc offset has been
1392 * allocated. Now that they've all been allocated, we will use
1393 * the 1 bit to tell us if we've written this particular toc
1398 { /* it is a file local symbol */
1399 int *local_toc_table
;
1402 sym
= syms
+ symndx
;
1403 name
= sym
->_n
._n_name
;
1405 local_toc_table
= obj_coff_local_toc_table(input_bfd
);
1406 our_toc_offset
= local_toc_table
[symndx
];
1408 if (IS_WRITTEN(our_toc_offset
))
1410 /* if it has been written out, it is marked with the
1411 1 bit. Fix up our offset, but do not write it out
1414 MAKE_ADDR_AGAIN(our_toc_offset
);
1418 "Not writing out toc_offset of %d for %s\n",
1419 our_toc_offset
, name
);
1424 /* write out the toc entry */
1425 record_toc(toc_section
, our_toc_offset
, priv
, strdup(name
));
1428 "Writing out toc_offset "
1429 "toc_section (%p,%p)+%d val %d for %s\n",
1431 toc_section
->contents
,
1437 bfd_put_32(output_bfd
,
1439 toc_section
->contents
+ our_toc_offset
);
1441 MARK_AS_WRITTEN(local_toc_table
[symndx
]);
1447 const char *name
= h
->root
.root
.root
.string
;
1448 our_toc_offset
= h
->toc_offset
;
1450 if ((r_flags
& IMAGE_REL_PPC_TOCDEFN
)
1451 == IMAGE_REL_PPC_TOCDEFN
)
1453 /* This is wrong. If tocdefn is on, we must unconditionally
1454 assume the following path */
1455 && IS_UNALLOCATED(our_toc_offset
))
1458 /* This is unbelievable cheese. Some knowledgable asm
1459 hacker has decided to use r2 as a base for loading
1460 a value. He/She does this by setting the tocdefn bit,
1461 and not supplying a toc definition. The behaviour is
1462 then to use the difference between the value of the
1463 symbol and the actual location of the toc as the toc
1466 In fact, what is usually happening is, because the
1467 Import Address Table is mapped immediately following
1468 the toc, some trippy library code trying for speed on
1469 dll linkage, takes advantage of that and considers
1470 the IAT to be part of the toc, thus saving a load.
1474 "TOCDEFN is on, (%s) (%p) our_toc_offset = %x\n",
1475 name
, h
, our_toc_offset
);
1478 our_toc_offset
= val
-
1479 (toc_section
->output_section
->vma
+
1480 toc_section
->output_offset
);
1484 " our_toc_offset set to %x\n", our_toc_offset
);
1487 /* The size must still fit in a 16bit displacment */
1488 if (our_toc_offset
>= 65535)
1491 "TOCDEFN Relocation exceeded "
1492 "displacment of 65535\n");
1496 record_toc(toc_section
, our_toc_offset
, pub
, strdup(name
));
1498 else if (IS_WRITTEN(our_toc_offset
))
1500 /* if it has been written out, it is marked with the
1501 1 bit. Fix up our offset, but do not write it out
1504 MAKE_ADDR_AGAIN(our_toc_offset
);
1507 "Not writing out toc_offset of %d for %s\n",
1508 our_toc_offset
, name
);
1513 record_toc(toc_section
, our_toc_offset
, pub
, strdup(name
));
1516 /* write out the toc entry */
1518 "Writing out toc_offset "
1519 "toc_section (%p,%p)+%d val %d for %s\n",
1521 toc_section
->contents
,
1527 /* write out the toc entry */
1528 bfd_put_32(output_bfd
,
1530 toc_section
->contents
+ our_toc_offset
);
1532 MARK_AS_WRITTEN(h
->toc_offset
);
1533 /* The tricky part is that this is the address that */
1534 /* needs a .reloc entry for it */
1539 if (fixit
&& info
->base_file
)
1541 /* So if this is non pcrelative, and is referenced
1542 to a section or a common symbol, then it needs a reloc */
1544 /* relocation to a symbol in a section which
1545 isn't absolute - we output the address here
1548 bfd_vma addr
= toc_section
->output_section
->vma
1549 + toc_section
->output_offset
+ our_toc_offset
;
1551 if (coff_data(output_bfd
)->pe
)
1552 addr
-= pe_data(output_bfd
)->pe_opthdr
.ImageBase
;
1556 " Toc Section .reloc candidate addr = %x\n", addr
);
1558 fwrite (&addr
, 1,4, (FILE *) info
->base_file
);
1562 /* FIXME: this test is conservative */
1563 if ( (r_flags
& IMAGE_REL_PPC_TOCDEFN
) != IMAGE_REL_PPC_TOCDEFN
&&
1564 our_toc_offset
> toc_section
->_raw_size
)
1567 "reloc offset is bigger than the toc size!\n");
1571 /* Now we know the relocation for this toc reference */
1572 relocation
= our_toc_offset
+ TOC_LOAD_ADJUSTMENT
;
1573 rstat
= _bfd_relocate_contents (howto
,
1579 case IMAGE_REL_PPC_IFGLUE
:
1581 /* To solve this, we need to know whether or not the symbol */
1582 /* appearing on the call instruction is a glue function or not. */
1583 /* A glue function must announce itself via a IMGLUE reloc, and */
1584 /* the reloc contains the required toc restore instruction */
1587 const char *my_name
;
1588 DUMP_RELOC2(howto
->name
, rel
);
1592 my_name
= h
->root
.root
.root
.string
;
1593 if (h
->symbol_is_glue
== 1)
1595 x
= bfd_get_32(input_bfd
, loc
);
1596 bfd_put_32(input_bfd
, h
->glue_insn
, loc
);
1601 case IMAGE_REL_PPC_SECREL
:
1602 /* Unimplemented: codeview debugging information */
1603 /* For fast access to the header of the section
1604 containing the item. */
1606 case IMAGE_REL_PPC_SECTION
:
1607 /* Unimplemented: codeview debugging information */
1608 /* Is used to indicate that the value should be relative
1609 to the beginning of the section that contains the
1612 case IMAGE_REL_PPC_ABSOLUTE
:
1614 const char *my_name
;
1616 my_name
= (syms
+symndx
)->_n
._n_name
;
1619 my_name
= h
->root
.root
.root
.string
;
1623 "Warning: unsupported reloc %s <file %s, section %s>\n",
1625 bfd_get_filename(input_bfd
),
1626 input_section
->name
);
1628 fprintf(stderr
,"sym %d (%s), r_vaddr %d (%x)\n",
1629 rel
->r_symndx
, my_name
, rel
->r_vaddr
, rel
->r_vaddr
);
1632 case IMAGE_REL_PPC_IMGLUE
:
1634 /* There is nothing to do now. This reloc was noted in the first
1635 pass over the relocs, and the glue instruction extracted */
1636 const char *my_name
;
1637 if (h
->symbol_is_glue
== 1)
1639 my_name
= h
->root
.root
.root
.string
;
1641 "Warning: previously missed IMGLUE reloc %s <file %s, section %s>\n",
1643 bfd_get_filename(input_bfd
),
1644 input_section
->name
);
1650 case IMAGE_REL_PPC_ADDR32NB
:
1652 struct coff_link_hash_entry
*myh
= 0;
1653 const char *name
= 0;
1654 DUMP_RELOC2(howto
->name
, rel
);
1656 if (strncmp(".idata$2",input_section
->name
,8) == 0 && first_thunk_address
== 0)
1658 /* set magic values */
1660 struct coff_link_hash_entry
*myh
= 0;
1661 myh
= coff_link_hash_lookup (coff_hash_table (info
),
1663 false, false, true);
1664 first_thunk_address
= myh
->root
.u
.def
.value
+
1665 sec
->output_section
->vma
+
1666 sec
->output_offset
-
1667 pe_data(output_bfd
)->pe_opthdr
.ImageBase
;
1669 idata5offset
= myh
->root
.u
.def
.value
;
1670 myh
= coff_link_hash_lookup (coff_hash_table (info
),
1672 false, false, true);
1674 thunk_size
= myh
->root
.u
.def
.value
- idata5offset
;
1675 myh
= coff_link_hash_lookup (coff_hash_table (info
),
1677 false, false, true);
1678 import_table_size
= myh
->root
.u
.def
.value
;
1681 "first computation triggered fta %x, ts %d(%x), its %d(%x)\n",
1682 first_thunk_address
, thunk_size
, thunk_size
, import_table_size
,
1688 { /* it is a file local symbol */
1689 sym
= syms
+ symndx
;
1690 name
= sym
->_n
._n_name
;
1696 name
= h
->root
.root
.root
.string
;
1697 if (strcmp(".idata$2", name
) == 0)
1698 target
= "__idata2_magic__";
1699 else if (strcmp(".idata$4", name
) == 0)
1700 target
= "__idata4_magic__";
1701 else if (strcmp(".idata$5", name
) == 0)
1702 target
= "__idata5_magic__";
1708 myh
= coff_link_hash_lookup (coff_hash_table (info
),
1710 false, false, true);
1713 fprintf(stderr
, "Missing idata magic cookies, "
1714 "this cannot work anyway...\n");
1718 val
= myh
->root
.u
.def
.value
+
1719 sec
->output_section
->vma
+ sec
->output_offset
;
1720 if (first_thunk_address
== 0)
1723 myh
= coff_link_hash_lookup (coff_hash_table (info
),
1725 false, false, true);
1726 first_thunk_address
= myh
->root
.u
.def
.value
+
1727 sec
->output_section
->vma
+
1728 sec
->output_offset
-
1729 pe_data(output_bfd
)->pe_opthdr
.ImageBase
;
1731 idata5offset
= myh
->root
.u
.def
.value
;
1732 myh
= coff_link_hash_lookup (coff_hash_table (info
),
1734 false, false, true);
1736 thunk_size
= myh
->root
.u
.def
.value
- idata5offset
;
1737 myh
= coff_link_hash_lookup (coff_hash_table (info
),
1739 false, false, true);
1740 import_table_size
= myh
->root
.u
.def
.value
;
1744 "second computation triggered fta %x, ts %d(%x), its %d(%x)\n",
1745 first_thunk_address
, thunk_size
, thunk_size
, import_table_size
,
1752 rstat
= _bfd_relocate_contents (howto
,
1755 pe_data(output_bfd
)->pe_opthdr
.ImageBase
,
1760 case IMAGE_REL_PPC_REL24
:
1761 DUMP_RELOC2(howto
->name
, rel
);
1762 val
-= (input_section
->output_section
->vma
1763 + input_section
->output_offset
);
1765 rstat
= _bfd_relocate_contents (howto
,
1770 case IMAGE_REL_PPC_ADDR16
:
1771 case IMAGE_REL_PPC_ADDR24
:
1772 case IMAGE_REL_PPC_ADDR32
:
1773 DUMP_RELOC2(howto
->name
, rel
);
1774 rstat
= _bfd_relocate_contents (howto
,
1781 if ( info
->base_file
)
1783 /* So if this is non pcrelative, and is referenced
1784 to a section or a common symbol, then it needs a reloc */
1785 if (sym
&& pe_data(output_bfd
)->in_reloc_p(output_bfd
, howto
))
1787 /* relocation to a symbol in a section which
1788 isn't absolute - we output the address here
1790 bfd_vma addr
= rel
->r_vaddr
1791 - input_section
->vma
1792 + input_section
->output_offset
1793 + input_section
->output_section
->vma
;
1795 if (coff_data(output_bfd
)->pe
)
1797 bfd_vma before_addr
= addr
;
1798 addr
-= pe_data(output_bfd
)->pe_opthdr
.ImageBase
;
1801 " adjusted down from %x to %x", before_addr
, addr
);
1805 fprintf(stderr
, "\n");
1808 fwrite (&addr
, 1,4, (FILE *) info
->base_file
);
1818 case bfd_reloc_overflow
:
1821 char buf
[SYMNMLEN
+ 1];
1826 name
= h
->root
.root
.root
.string
;
1827 else if (sym
== NULL
)
1829 else if (sym
->_n
._n_n
._n_zeroes
== 0
1830 && sym
->_n
._n_n
._n_offset
!= 0)
1831 name
= obj_coff_strings (input_bfd
) + sym
->_n
._n_n
._n_offset
;
1834 strncpy (buf
, sym
->_n
._n_name
, SYMNMLEN
);
1835 buf
[SYMNMLEN
] = '\0';
1841 name
= _bfd_coff_internal_syment_name (input_bfd
, sym
, buf
);
1847 if (! ((*info
->callbacks
->reloc_overflow
)
1848 (info
, name
, howto
->name
,
1849 (bfd_vma
) 0, input_bfd
,
1850 input_section
, rel
->r_vaddr
- input_section
->vma
)))
1854 "pe_ppc_relocate_section (%s) for %s in bfd %s RETURNING TRUE\n",
1856 input_section
->name
,
1857 input_bfd
->filename
);
1869 "pe_ppc_relocate_section (%s) for %s in bfd %s RETURNING TRUE\n",
1871 input_section
->name
,
1872 input_bfd
->filename
);
1880 #ifdef COFF_IMAGE_WITH_PE
1882 long int global_toc_size
= 4;
1884 bfd
* bfd_of_toc_owner
= 0;
1886 long int import_table_size
;
1887 long int first_thunk_address
;
1888 long int thunk_size
;
1890 struct list_ele
*head
;
1891 struct list_ele
*tail
;
1894 h1
= "\n\t\t\tTOC MAPPING\n\n";
1896 h2
= " TOC disassembly Comments Name\n";
1898 h3
= " Offset spelling (if present)\n";
1911 for(t
= head
; t
!= 0; t
=t
->next
)
1917 else if (t
->cat
== pub
)
1919 else if (t
->cat
== data
)
1920 cat
= "data-in-toc ";
1922 if (t
->offset
> global_toc_size
)
1924 if (t
->offset
<= global_toc_size
+ thunk_size
)
1925 cat
= "IAT reference ";
1929 "**** global_toc_size %d(%x), thunk_size %d(%x)\n",
1930 global_toc_size
, global_toc_size
, thunk_size
, thunk_size
);
1931 cat
= "Out of bounds!";
1936 " %04lx (%d)", t
->offset
, t
->offset
- 32768);
1943 fprintf(file
, "\n");
1947 ppc_allocate_toc_section (info
)
1948 struct bfd_link_info
*info
;
1952 static char test_char
= '1';
1954 if ( global_toc_size
== 0 ) /* FIXME: does this get me in trouble? */
1957 if (bfd_of_toc_owner
== 0)
1960 "There is no bfd that owns the toc section!\n");
1964 s
= bfd_get_section_by_name ( bfd_of_toc_owner
, TOC_SECTION_NAME
);
1967 fprintf(stderr
, "No Toc section!\n");
1971 foo
= bfd_alloc(bfd_of_toc_owner
, global_toc_size
);
1972 memset(foo
, test_char
, global_toc_size
);
1974 s
->_raw_size
= s
->_cooked_size
= global_toc_size
;
1981 ppc_process_before_allocation (abfd
, info
)
1983 struct bfd_link_info
*info
;
1986 struct internal_reloc
*i
, *rel
;
1990 "ppc_process_before_allocation: BFD %s\n",
1991 bfd_get_filename(abfd
));
1994 /* here we have a bfd that is to be included on the link. We have a hook
1995 to do reloc rummaging, before section sizes are nailed down. */
1997 _bfd_coff_get_external_symbols(abfd
);
1999 /* rummage around all the relocs and map the toc */
2000 sec
= abfd
->sections
;
2007 for (; sec
!= 0; sec
= sec
->next
)
2013 " section %s reloc count %d\n",
2018 if (sec
->reloc_count
== 0)
2021 /* load the relocs */
2022 /* FIXME: there may be a storage leak here */
2023 i
=_bfd_coff_read_internal_relocs(abfd
,sec
,1,0,0,0);
2028 for (rel
=i
;rel
<i
+sec
->reloc_count
;++rel
)
2030 unsigned short r_type
= EXTRACT_TYPE (rel
->r_type
);
2031 unsigned short r_flags
= EXTRACT_FLAGS(rel
->r_type
);
2032 unsigned short junk
= EXTRACT_JUNK (rel
->r_type
);
2035 /* now examine flags */
2038 fprintf (stderr
, "Reloc with flags found!");
2039 if ( r_flags
& IMAGE_REL_PPC_NEG
)
2040 fprintf (stderr
, " NEG");
2041 if ( r_flags
& IMAGE_REL_PPC_BRTAKEN
)
2042 fprintf (stderr
, " BRTAKEN");
2043 if ( r_flags
& IMAGE_REL_PPC_BRNTAKEN
)
2044 fprintf (stderr
, " BRNTAKEN");
2045 if ( r_flags
& IMAGE_REL_PPC_TOCDEFN
)
2046 fprintf (stderr
, " TOCDEFN");
2047 fprintf(stderr
, "\n");
2051 DUMP_RELOC2(ppc_coff_howto_table
[r_type
].name
, rel
);
2055 case IMAGE_REL_PPC_TOCREL16
:
2058 This remains unimplemented for now, as it currently adds
2059 un-necessary elements to the toc. All we need to do today
2060 is not do anything if TOCDEFN is on.
2062 if ( r_flags
& IMAGE_REL_PPC_TOCDEFN
)
2063 toc_offset
= ppc_record_data_in_toc_entry(abfd
, info
, sec
,
2067 toc_offset
= ppc_record_toc_entry(abfd
, info
, sec
,
2068 rel
->r_symndx
, default_toc
);
2070 if ( (r_flags
& IMAGE_REL_PPC_TOCDEFN
) != IMAGE_REL_PPC_TOCDEFN
)
2071 toc_offset
= ppc_record_toc_entry(abfd
, info
, sec
,
2072 rel
->r_symndx
, default_toc
);
2074 case IMAGE_REL_PPC_IMGLUE
:
2075 ppc_mark_symbol_as_glue(abfd
, rel
->r_symndx
, rel
);
2087 static bfd_reloc_status_type
2088 ppc_refhi_reloc (abfd
,
2096 arelent
*reloc_entry
;
2099 asection
*input_section
;
2101 char **error_message
;
2104 DUMP_RELOC("REFHI",reloc_entry
);
2106 if (output_bfd
== (bfd
*) NULL
)
2107 return bfd_reloc_continue
;
2109 return bfd_reloc_undefined
;
2112 static bfd_reloc_status_type
2113 ppc_reflo_reloc (abfd
,
2121 arelent
*reloc_entry
;
2124 asection
*input_section
;
2126 char **error_message
;
2129 DUMP_RELOC("REFLO",reloc_entry
);
2131 if (output_bfd
== (bfd
*) NULL
)
2132 return bfd_reloc_continue
;
2134 return bfd_reloc_undefined
;
2137 static bfd_reloc_status_type
2138 ppc_pair_reloc (abfd
,
2146 arelent
*reloc_entry
;
2149 asection
*input_section
;
2151 char **error_message
;
2154 DUMP_RELOC("PAIR",reloc_entry
);
2156 if (output_bfd
== (bfd
*) NULL
)
2157 return bfd_reloc_continue
;
2159 return bfd_reloc_undefined
;
2163 static bfd_reloc_status_type
2164 ppc_toc16_reloc (abfd
,
2172 arelent
*reloc_entry
;
2175 asection
*input_section
;
2177 char **error_message
;
2179 UN_IMPL("TOCREL16");
2180 DUMP_RELOC("TOCREL16",reloc_entry
);
2182 if (output_bfd
== (bfd
*) NULL
)
2184 return bfd_reloc_continue
;
2187 return bfd_reloc_ok
;
2190 /* ADDR32NB : 32 bit address relative to the virtual origin. */
2191 /* (On the alpha, this is always a linker generated thunk)*/
2192 /* (i.e. 32bit addr relative to the image base) */
2196 static bfd_reloc_status_type
2197 ppc_addr32nb_reloc (abfd
,
2205 arelent
*reloc_entry
;
2208 asection
*input_section
;
2210 char **error_message
;
2212 UN_IMPL("ADDR32NB");
2213 DUMP_RELOC("ADDR32NB",reloc_entry
);
2215 return bfd_reloc_ok
;
2218 static bfd_reloc_status_type
2219 ppc_secrel_reloc (abfd
,
2227 arelent
*reloc_entry
;
2230 asection
*input_section
;
2232 char **error_message
;
2235 DUMP_RELOC("SECREL",reloc_entry
);
2237 if (output_bfd
== (bfd
*) NULL
)
2238 return bfd_reloc_continue
;
2240 return bfd_reloc_ok
;
2243 static bfd_reloc_status_type
2244 ppc_section_reloc (abfd
,
2252 arelent
*reloc_entry
;
2255 asection
*input_section
;
2257 char **error_message
;
2260 DUMP_RELOC("SECTION",reloc_entry
);
2262 if (output_bfd
== (bfd
*) NULL
)
2263 return bfd_reloc_continue
;
2265 return bfd_reloc_ok
;
2268 static bfd_reloc_status_type
2269 ppc_imglue_reloc (abfd
,
2277 arelent
*reloc_entry
;
2280 asection
*input_section
;
2282 char **error_message
;
2285 DUMP_RELOC("IMGLUE",reloc_entry
);
2287 if (output_bfd
== (bfd
*) NULL
)
2288 return bfd_reloc_continue
;
2290 return bfd_reloc_ok
;
2295 #define MAX_RELOC_INDEX \
2296 (sizeof(ppc_coff_howto_table) / sizeof(ppc_coff_howto_table[0]) - 1)
2299 /* FIXME: There is a possiblity that when we read in a reloc from a file,
2300 that there are some bits encoded in the upper portion of the
2301 type field. Not yet implemented.
2303 static void ppc_coff_rtype2howto
PARAMS ((arelent
*relent
,
2304 struct internal_reloc
*internal
));
2307 ppc_coff_rtype2howto (relent
, internal
)
2309 struct internal_reloc
*internal
;
2312 /* We can encode one of three things in the type field, aside from the
2314 1. IMAGE_REL_PPC_NEG - indicates the value field is a subtraction
2315 value, rather than an addition value
2316 2. IMAGE_REL_PPC_BRTAKEN, IMAGE_REL_PPC_BRNTAKEN - indicates that
2317 the branch is expected to be taken or not.
2318 3. IMAGE_REL_PPC_TOCDEFN - toc slot definition in the file
2319 For now, we just strip this stuff to find the type, and ignore it other
2322 reloc_howto_type
*howto
;
2323 unsigned short r_type
= EXTRACT_TYPE (internal
->r_type
);
2324 unsigned short r_flags
= EXTRACT_FLAGS(internal
->r_type
);
2325 unsigned short junk
= EXTRACT_JUNK (internal
->r_type
);
2327 /* the masking process only slices off the bottom byte for r_type. */
2328 if ( r_type
> MAX_RELOC_INDEX
)
2331 "ppc_coff_rtype2howto: reloc index %d out of range [%d, %d]\n",
2332 internal
->r_type
, 0, MAX_RELOC_INDEX
);
2336 /* check for absolute crap */
2340 "ppc_coff_rtype2howto: reloc index %d contains junk %d\n",
2341 internal
->r_type
, junk
);
2346 /* now examine flags */
2349 fprintf (stderr
, "Reloc with flags found!");
2350 if ( r_flags
& IMAGE_REL_PPC_NEG
)
2351 fprintf (stderr
, " NEG");
2352 if ( r_flags
& IMAGE_REL_PPC_BRTAKEN
)
2353 fprintf (stderr
, " BRTAKEN");
2354 if ( r_flags
& IMAGE_REL_PPC_BRNTAKEN
)
2355 fprintf (stderr
, " BRNTAKEN");
2356 if ( r_flags
& IMAGE_REL_PPC_TOCDEFN
)
2357 fprintf (stderr
, " TOCDEFN");
2358 fprintf(stderr
, "\n");
2364 case IMAGE_REL_PPC_ADDR16
:
2365 case IMAGE_REL_PPC_REL24
:
2366 case IMAGE_REL_PPC_ADDR24
:
2367 case IMAGE_REL_PPC_ADDR32
:
2368 case IMAGE_REL_PPC_IFGLUE
:
2369 case IMAGE_REL_PPC_ADDR32NB
:
2370 case IMAGE_REL_PPC_SECTION
:
2371 case IMAGE_REL_PPC_SECREL
:
2372 DUMP_RELOC2(ppc_coff_howto_table
[r_type
].name
, internal
);
2373 howto
= ppc_coff_howto_table
+ r_type
;
2375 case IMAGE_REL_PPC_IMGLUE
:
2376 DUMP_RELOC2(ppc_coff_howto_table
[r_type
].name
, internal
);
2377 howto
= ppc_coff_howto_table
+ r_type
;
2379 case IMAGE_REL_PPC_TOCREL16
:
2380 DUMP_RELOC2(ppc_coff_howto_table
[r_type
].name
, internal
);
2381 if (r_flags
& IMAGE_REL_PPC_TOCDEFN
)
2382 howto
= ppc_coff_howto_table
+ IMAGE_REL_PPC_TOCREL16_DEFN
;
2384 howto
= ppc_coff_howto_table
+ IMAGE_REL_PPC_TOCREL16
;
2388 "Warning: Unsupported reloc %s [%d] used -- it may not work.\n",
2389 ppc_coff_howto_table
[r_type
].name
,
2391 howto
= ppc_coff_howto_table
+ r_type
;
2395 relent
->howto
= howto
;
2399 static reloc_howto_type
*
2400 coff_ppc_rtype_to_howto (abfd
, sec
, rel
, h
, sym
, addendp
)
2403 struct internal_reloc
*rel
;
2404 struct coff_link_hash_entry
*h
;
2405 struct internal_syment
*sym
;
2408 reloc_howto_type
*howto
;
2410 /* We can encode one of three things in the type field, aside from the
2412 1. IMAGE_REL_PPC_NEG - indicates the value field is a subtraction
2413 value, rather than an addition value
2414 2. IMAGE_REL_PPC_BRTAKEN, IMAGE_REL_PPC_BRNTAKEN - indicates that
2415 the branch is expected to be taken or not.
2416 3. IMAGE_REL_PPC_TOCDEFN - toc slot definition in the file
2417 For now, we just strip this stuff to find the type, and ignore it other
2421 unsigned short r_type
= EXTRACT_TYPE (rel
->r_type
);
2422 unsigned short r_flags
= EXTRACT_FLAGS(rel
->r_type
);
2423 unsigned short junk
= EXTRACT_JUNK (rel
->r_type
);
2425 /* the masking process only slices off the bottom byte for r_type. */
2426 if ( r_type
> MAX_RELOC_INDEX
)
2429 "coff_ppc_rtype_to_howto: index %d out of range [%d, %d]\n",
2430 r_type
, 0, MAX_RELOC_INDEX
);
2434 /* check for absolute crap */
2438 "coff_ppc_rtype_to_howto: reloc index %d contains junk %d\n",
2444 /* now examine flags */
2447 fprintf (stderr
, "Reloc with flags found!");
2448 if ( r_flags
& IMAGE_REL_PPC_NEG
)
2449 fprintf (stderr
, " NEG");
2450 if ( r_flags
& IMAGE_REL_PPC_BRTAKEN
)
2451 fprintf (stderr
, " BRTAKEN");
2452 if ( r_flags
& IMAGE_REL_PPC_BRNTAKEN
)
2453 fprintf (stderr
, " BRNTAKEN");
2454 if ( r_flags
& IMAGE_REL_PPC_TOCDEFN
)
2455 fprintf (stderr
, " TOCDEFN");
2456 fprintf(stderr
, "\n");
2462 case IMAGE_REL_PPC_ADDR32NB
:
2463 DUMP_RELOC2(ppc_coff_howto_table
[r_type
].name
, rel
);
2464 *addendp
-= pe_data(sec
->output_section
->owner
)->pe_opthdr
.ImageBase
;
2465 howto
= ppc_coff_howto_table
+ r_type
;
2467 case IMAGE_REL_PPC_TOCREL16
:
2468 DUMP_RELOC2(ppc_coff_howto_table
[r_type
].name
, rel
);
2469 if (r_flags
& IMAGE_REL_PPC_TOCDEFN
)
2470 howto
= ppc_coff_howto_table
+ IMAGE_REL_PPC_TOCREL16_DEFN
;
2472 howto
= ppc_coff_howto_table
+ IMAGE_REL_PPC_TOCREL16
;
2474 case IMAGE_REL_PPC_ADDR16
:
2475 case IMAGE_REL_PPC_REL24
:
2476 case IMAGE_REL_PPC_ADDR24
:
2477 case IMAGE_REL_PPC_ADDR32
:
2478 case IMAGE_REL_PPC_IFGLUE
:
2479 case IMAGE_REL_PPC_SECTION
:
2480 case IMAGE_REL_PPC_SECREL
:
2481 DUMP_RELOC2(ppc_coff_howto_table
[r_type
].name
, rel
);
2482 howto
= ppc_coff_howto_table
+ r_type
;
2484 case IMAGE_REL_PPC_IMGLUE
:
2485 DUMP_RELOC2(ppc_coff_howto_table
[r_type
].name
, rel
);
2486 howto
= ppc_coff_howto_table
+ r_type
;
2490 "Warning: Unsupported reloc %s [%d] used -- it may not work.\n",
2491 ppc_coff_howto_table
[r_type
].name
,
2493 howto
= ppc_coff_howto_table
+ r_type
;
2501 /* a cheesy little macro to make the code a little more readable */
2502 #define HOW2MAP(bfd_rtype,ppc_rtype) \
2503 case bfd_rtype: return &ppc_coff_howto_table[ppc_rtype]
2505 static reloc_howto_type
*ppc_coff_reloc_type_lookup
2506 PARAMS ((bfd
*, bfd_reloc_code_real_type
));
2508 static reloc_howto_type
*
2509 ppc_coff_reloc_type_lookup (abfd
, code
)
2511 bfd_reloc_code_real_type code
;
2515 fprintf(stderr
, "ppc_coff_reloc_type_lookup for %s\n",
2516 bfd_get_reloc_code_name(code
));
2521 HOW2MAP(BFD_RELOC_32_GOTOFF
, IMAGE_REL_PPC_IMGLUE
);
2522 HOW2MAP(BFD_RELOC_16_GOT_PCREL
, IMAGE_REL_PPC_IFGLUE
);
2523 HOW2MAP(BFD_RELOC_16
, IMAGE_REL_PPC_ADDR16
);
2524 HOW2MAP(BFD_RELOC_PPC_B26
, IMAGE_REL_PPC_REL24
);
2525 HOW2MAP(BFD_RELOC_PPC_BA26
, IMAGE_REL_PPC_ADDR24
);
2526 HOW2MAP(BFD_RELOC_PPC_TOC16
, IMAGE_REL_PPC_TOCREL16
);
2527 HOW2MAP(BFD_RELOC_16_GOTOFF
, IMAGE_REL_PPC_TOCREL16_DEFN
);
2528 HOW2MAP(BFD_RELOC_32
, IMAGE_REL_PPC_ADDR32
);
2529 HOW2MAP(BFD_RELOC_RVA
, IMAGE_REL_PPC_ADDR32NB
);
2540 /* Tailor coffcode.h -- macro heaven. */
2542 #define RTYPE2HOWTO(cache_ptr, dst) ppc_coff_rtype2howto (cache_ptr, dst)
2544 #ifndef COFF_IMAGE_WITH_PE
2546 ppc_coff_swap_sym_in_hook ();
2549 /* We use the special COFF backend linker, with our own special touch. */
2551 #define coff_bfd_reloc_type_lookup ppc_coff_reloc_type_lookup
2552 #define coff_rtype_to_howto coff_ppc_rtype_to_howto
2553 #define coff_relocate_section coff_ppc_relocate_section
2554 #define coff_bfd_final_link ppc_bfd_coff_final_link
2556 #ifndef COFF_IMAGE_WITH_PE
2557 #define coff_swap_sym_in_hook ppc_coff_swap_sym_in_hook
2560 #define SELECT_RELOC(internal, howto) {internal.r_type=howto->type;}
2562 #define COFF_PAGE_SIZE 0x1000
2564 #define POWERPC_LE_PE
2566 #include "coffcode.h"
2570 #ifndef COFF_IMAGE_WITH_PE
2572 What we're trying to do here is allocate a toc section (early), and attach
2573 it to the last bfd to be processed. This avoids the problem of having a toc
2574 written out before all files have been processed. This code allocates
2575 a toc section for every file, and records the last one seen. There are
2576 at least two problems with this approach:
2577 1. We allocate whole bunches of toc sections that are ignored, but at
2578 at least we will not allocate a toc if no .toc is present.
2579 2. It's not clear to me that being the last bfd read necessarily means
2580 that you are the last bfd closed.
2581 3. Doing it on a "swap in" hook depends on when the "swap in" is called,
2582 and how often, etc. It's not clear to me that there isn't a hole here.
2586 ppc_coff_swap_sym_in_hook (abfd
, ext1
, in1
)
2591 SYMENT
*ext
= (SYMENT
*)ext1
;
2592 struct internal_syment
*in
= (struct internal_syment
*)in1
;
2594 if (bfd_of_toc_owner
!= 0) /* we already have a toc, so go home */
2597 if (strcmp(in
->_n
._n_name
, ".toc") == 0)
2600 register asection
*s
;
2603 s
= bfd_get_section_by_name ( abfd
, TOC_SECTION_NAME
);
2609 flags
= SEC_ALLOC
| SEC_LOAD
| SEC_HAS_CONTENTS
| SEC_IN_MEMORY
;
2613 "ppc_coff_swap_sym_in_hook: about to create the %s section\n",
2617 s
= bfd_make_section (abfd
, TOC_SECTION_NAME
);
2620 || !bfd_set_section_flags (abfd
, s
, flags
)
2621 || !bfd_set_section_alignment (abfd
, s
, 2))
2624 "toc section allocation failed!\n");
2628 /* save the bfd for later allocation */
2629 bfd_of_toc_owner
= abfd
;
2637 ppc_bfd_coff_final_link ();
2639 #ifndef COFF_IMAGE_WITH_PE
2645 if (abfd
== bfd_of_toc_owner
)
2654 return bfd_of_toc_owner
;
2657 /* this piece of machinery exists only to guarantee that the bfd that holds
2658 the toc section is written last.
2660 This does depend on bfd_make_section attaching a new section to the
2661 end of the section list for the bfd.
2663 This is otherwise intended to be functionally the same as
2664 cofflink.c:_bfd_coff_final_link(). It is specifically different only
2665 where the POWERPC_LE_PE macro modifies the code. It is left in as a
2666 precise form of comment. krk@cygnus.com
2668 #define POWERPC_LE_PE
2671 /* Do the final link step. */
2674 ppc_bfd_coff_final_link (abfd
, info
)
2676 struct bfd_link_info
*info
;
2678 bfd_size_type symesz
;
2679 struct coff_final_link_info finfo
;
2680 boolean debug_merge_allocated
;
2682 struct bfd_link_order
*p
;
2683 size_t max_sym_count
;
2684 size_t max_lineno_count
;
2685 size_t max_reloc_count
;
2686 size_t max_output_reloc_count
;
2687 size_t max_contents_size
;
2688 file_ptr rel_filepos
;
2690 file_ptr line_filepos
;
2691 unsigned int linesz
;
2693 bfd_byte
*external_relocs
= NULL
;
2694 char strbuf
[STRING_SIZE_SIZE
];
2696 symesz
= bfd_coff_symesz (abfd
);
2699 finfo
.output_bfd
= abfd
;
2700 finfo
.strtab
= NULL
;
2701 finfo
.section_info
= NULL
;
2702 finfo
.last_file_index
= -1;
2703 finfo
.internal_syms
= NULL
;
2704 finfo
.sec_ptrs
= NULL
;
2705 finfo
.sym_indices
= NULL
;
2706 finfo
.outsyms
= NULL
;
2707 finfo
.linenos
= NULL
;
2708 finfo
.contents
= NULL
;
2709 finfo
.external_relocs
= NULL
;
2710 finfo
.internal_relocs
= NULL
;
2711 debug_merge_allocated
= false;
2713 coff_data (abfd
)->link_info
= info
;
2715 finfo
.strtab
= _bfd_stringtab_init ();
2716 if (finfo
.strtab
== NULL
)
2719 if (! coff_debug_merge_hash_table_init (&finfo
.debug_merge
))
2721 debug_merge_allocated
= true;
2723 /* Compute the file positions for all the sections. */
2724 if (! abfd
->output_has_begun
)
2725 bfd_coff_compute_section_file_positions (abfd
);
2727 /* Count the line numbers and relocation entries required for the
2728 output file. Set the file positions for the relocs. */
2729 rel_filepos
= obj_relocbase (abfd
);
2730 relsz
= bfd_coff_relsz (abfd
);
2731 max_contents_size
= 0;
2732 max_lineno_count
= 0;
2733 max_reloc_count
= 0;
2735 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
2738 o
->lineno_count
= 0;
2739 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
2742 if (p
->type
== bfd_indirect_link_order
)
2746 sec
= p
->u
.indirect
.section
;
2748 if (info
->strip
== strip_none
2749 || info
->strip
== strip_some
)
2750 o
->lineno_count
+= sec
->lineno_count
;
2752 if (info
->relocateable
)
2753 o
->reloc_count
+= sec
->reloc_count
;
2755 if (sec
->_raw_size
> max_contents_size
)
2756 max_contents_size
= sec
->_raw_size
;
2757 if (sec
->lineno_count
> max_lineno_count
)
2758 max_lineno_count
= sec
->lineno_count
;
2759 if (sec
->reloc_count
> max_reloc_count
)
2760 max_reloc_count
= sec
->reloc_count
;
2762 else if (info
->relocateable
2763 && (p
->type
== bfd_section_reloc_link_order
2764 || p
->type
== bfd_symbol_reloc_link_order
))
2767 if (o
->reloc_count
== 0)
2771 o
->flags
|= SEC_RELOC
;
2772 o
->rel_filepos
= rel_filepos
;
2773 rel_filepos
+= o
->reloc_count
* relsz
;
2777 /* If doing a relocateable link, allocate space for the pointers we
2779 if (info
->relocateable
)
2783 /* We use section_count + 1, rather than section_count, because
2784 the target_index fields are 1 based. */
2785 finfo
.section_info
=
2786 ((struct coff_link_section_info
*)
2787 bfd_malloc ((abfd
->section_count
+ 1)
2788 * sizeof (struct coff_link_section_info
)));
2789 if (finfo
.section_info
== NULL
)
2791 for (i
= 0; i
<= abfd
->section_count
; i
++)
2793 finfo
.section_info
[i
].relocs
= NULL
;
2794 finfo
.section_info
[i
].rel_hashes
= NULL
;
2798 /* We now know the size of the relocs, so we can determine the file
2799 positions of the line numbers. */
2800 line_filepos
= rel_filepos
;
2801 linesz
= bfd_coff_linesz (abfd
);
2802 max_output_reloc_count
= 0;
2803 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
2805 if (o
->lineno_count
== 0)
2806 o
->line_filepos
= 0;
2809 o
->line_filepos
= line_filepos
;
2810 line_filepos
+= o
->lineno_count
* linesz
;
2813 if (o
->reloc_count
!= 0)
2815 /* We don't know the indices of global symbols until we have
2816 written out all the local symbols. For each section in
2817 the output file, we keep an array of pointers to hash
2818 table entries. Each entry in the array corresponds to a
2819 reloc. When we find a reloc against a global symbol, we
2820 set the corresponding entry in this array so that we can
2821 fix up the symbol index after we have written out all the
2824 Because of this problem, we also keep the relocs in
2825 memory until the end of the link. This wastes memory,
2826 but only when doing a relocateable link, which is not the
2828 BFD_ASSERT (info
->relocateable
);
2829 finfo
.section_info
[o
->target_index
].relocs
=
2830 ((struct internal_reloc
*)
2831 bfd_malloc (o
->reloc_count
* sizeof (struct internal_reloc
)));
2832 finfo
.section_info
[o
->target_index
].rel_hashes
=
2833 ((struct coff_link_hash_entry
**)
2834 bfd_malloc (o
->reloc_count
2835 * sizeof (struct coff_link_hash_entry
*)));
2836 if (finfo
.section_info
[o
->target_index
].relocs
== NULL
2837 || finfo
.section_info
[o
->target_index
].rel_hashes
== NULL
)
2840 if (o
->reloc_count
> max_output_reloc_count
)
2841 max_output_reloc_count
= o
->reloc_count
;
2844 /* Reset the reloc and lineno counts, so that we can use them to
2845 count the number of entries we have output so far. */
2847 o
->lineno_count
= 0;
2850 obj_sym_filepos (abfd
) = line_filepos
;
2852 /* Figure out the largest number of symbols in an input BFD. Take
2853 the opportunity to clear the output_has_begun fields of all the
2856 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
2860 sub
->output_has_begun
= false;
2861 sz
= obj_raw_syment_count (sub
);
2862 if (sz
> max_sym_count
)
2866 /* Allocate some buffers used while linking. */
2867 finfo
.internal_syms
= ((struct internal_syment
*)
2868 bfd_malloc (max_sym_count
2869 * sizeof (struct internal_syment
)));
2870 finfo
.sec_ptrs
= (asection
**) bfd_malloc (max_sym_count
2871 * sizeof (asection
*));
2872 finfo
.sym_indices
= (long *) bfd_malloc (max_sym_count
* sizeof (long));
2873 finfo
.outsyms
= ((bfd_byte
*)
2874 bfd_malloc ((size_t) ((max_sym_count
+ 1) * symesz
)));
2875 finfo
.linenos
= (bfd_byte
*) bfd_malloc (max_lineno_count
2876 * bfd_coff_linesz (abfd
));
2877 finfo
.contents
= (bfd_byte
*) bfd_malloc (max_contents_size
);
2878 finfo
.external_relocs
= (bfd_byte
*) bfd_malloc (max_reloc_count
* relsz
);
2879 if (! info
->relocateable
)
2880 finfo
.internal_relocs
= ((struct internal_reloc
*)
2881 bfd_malloc (max_reloc_count
2882 * sizeof (struct internal_reloc
)));
2883 if ((finfo
.internal_syms
== NULL
&& max_sym_count
> 0)
2884 || (finfo
.sec_ptrs
== NULL
&& max_sym_count
> 0)
2885 || (finfo
.sym_indices
== NULL
&& max_sym_count
> 0)
2886 || finfo
.outsyms
== NULL
2887 || (finfo
.linenos
== NULL
&& max_lineno_count
> 0)
2888 || (finfo
.contents
== NULL
&& max_contents_size
> 0)
2889 || (finfo
.external_relocs
== NULL
&& max_reloc_count
> 0)
2890 || (! info
->relocateable
2891 && finfo
.internal_relocs
== NULL
2892 && max_reloc_count
> 0))
2895 /* We now know the position of everything in the file, except that
2896 we don't know the size of the symbol table and therefore we don't
2897 know where the string table starts. We just build the string
2898 table in memory as we go along. We process all the relocations
2899 for a single input file at once. */
2900 obj_raw_syment_count (abfd
) = 0;
2902 if (coff_backend_info (abfd
)->_bfd_coff_start_final_link
)
2904 if (! bfd_coff_start_final_link (abfd
, info
))
2908 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
2910 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
2912 if (p
->type
== bfd_indirect_link_order
2913 && (bfd_get_flavour (p
->u
.indirect
.section
->owner
)
2914 == bfd_target_coff_flavour
))
2916 sub
= p
->u
.indirect
.section
->owner
;
2917 #ifdef POWERPC_LE_PE
2918 if (! sub
->output_has_begun
&& !ppc_do_last(sub
))
2920 if (! sub
->output_has_begun
)
2923 if (! coff_link_input_bfd (&finfo
, sub
))
2925 sub
->output_has_begun
= true;
2928 else if (p
->type
== bfd_section_reloc_link_order
2929 || p
->type
== bfd_symbol_reloc_link_order
)
2931 if (! coff_reloc_link_order (abfd
, &finfo
, o
, p
))
2936 if (! _bfd_default_link_order (abfd
, info
, o
, p
))
2942 #ifdef POWERPC_LE_PE
2944 extern bfd
* ppc_get_last();
2945 bfd
* last_one
= ppc_get_last();
2948 if (! coff_link_input_bfd (&finfo
, last_one
))
2951 last_one
->output_has_begun
= true;
2955 /* Free up the buffers used by coff_link_input_bfd. */
2957 coff_debug_merge_hash_table_free (&finfo
.debug_merge
);
2958 debug_merge_allocated
= false;
2960 if (finfo
.internal_syms
!= NULL
)
2962 free (finfo
.internal_syms
);
2963 finfo
.internal_syms
= NULL
;
2965 if (finfo
.sec_ptrs
!= NULL
)
2967 free (finfo
.sec_ptrs
);
2968 finfo
.sec_ptrs
= NULL
;
2970 if (finfo
.sym_indices
!= NULL
)
2972 free (finfo
.sym_indices
);
2973 finfo
.sym_indices
= NULL
;
2975 if (finfo
.linenos
!= NULL
)
2977 free (finfo
.linenos
);
2978 finfo
.linenos
= NULL
;
2980 if (finfo
.contents
!= NULL
)
2982 free (finfo
.contents
);
2983 finfo
.contents
= NULL
;
2985 if (finfo
.external_relocs
!= NULL
)
2987 free (finfo
.external_relocs
);
2988 finfo
.external_relocs
= NULL
;
2990 if (finfo
.internal_relocs
!= NULL
)
2992 free (finfo
.internal_relocs
);
2993 finfo
.internal_relocs
= NULL
;
2996 /* The value of the last C_FILE symbol is supposed to be the symbol
2997 index of the first external symbol. Write it out again if
2999 if (finfo
.last_file_index
!= -1
3000 && (unsigned int) finfo
.last_file
.n_value
!= obj_raw_syment_count (abfd
))
3002 finfo
.last_file
.n_value
= obj_raw_syment_count (abfd
);
3003 bfd_coff_swap_sym_out (abfd
, (PTR
) &finfo
.last_file
,
3004 (PTR
) finfo
.outsyms
);
3006 (obj_sym_filepos (abfd
)
3007 + finfo
.last_file_index
* symesz
),
3009 || bfd_write (finfo
.outsyms
, symesz
, 1, abfd
) != symesz
)
3013 /* Write out the global symbols. */
3014 finfo
.failed
= false;
3015 coff_link_hash_traverse (coff_hash_table (info
), coff_write_global_sym
,
3020 /* The outsyms buffer is used by coff_write_global_sym. */
3021 if (finfo
.outsyms
!= NULL
)
3023 free (finfo
.outsyms
);
3024 finfo
.outsyms
= NULL
;
3027 if (info
->relocateable
)
3029 /* Now that we have written out all the global symbols, we know
3030 the symbol indices to use for relocs against them, and we can
3031 finally write out the relocs. */
3032 external_relocs
= ((bfd_byte
*)
3033 bfd_malloc (max_output_reloc_count
* relsz
));
3034 if (external_relocs
== NULL
)
3037 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
3039 struct internal_reloc
*irel
;
3040 struct internal_reloc
*irelend
;
3041 struct coff_link_hash_entry
**rel_hash
;
3044 if (o
->reloc_count
== 0)
3047 irel
= finfo
.section_info
[o
->target_index
].relocs
;
3048 irelend
= irel
+ o
->reloc_count
;
3049 rel_hash
= finfo
.section_info
[o
->target_index
].rel_hashes
;
3050 erel
= external_relocs
;
3051 for (; irel
< irelend
; irel
++, rel_hash
++, erel
+= relsz
)
3053 if (*rel_hash
!= NULL
)
3055 BFD_ASSERT ((*rel_hash
)->indx
>= 0);
3056 irel
->r_symndx
= (*rel_hash
)->indx
;
3058 bfd_coff_swap_reloc_out (abfd
, (PTR
) irel
, (PTR
) erel
);
3061 if (bfd_seek (abfd
, o
->rel_filepos
, SEEK_SET
) != 0
3062 || bfd_write ((PTR
) external_relocs
, relsz
, o
->reloc_count
,
3063 abfd
) != relsz
* o
->reloc_count
)
3067 free (external_relocs
);
3068 external_relocs
= NULL
;
3071 /* Free up the section information. */
3072 if (finfo
.section_info
!= NULL
)
3076 for (i
= 0; i
< abfd
->section_count
; i
++)
3078 if (finfo
.section_info
[i
].relocs
!= NULL
)
3079 free (finfo
.section_info
[i
].relocs
);
3080 if (finfo
.section_info
[i
].rel_hashes
!= NULL
)
3081 free (finfo
.section_info
[i
].rel_hashes
);
3083 free (finfo
.section_info
);
3084 finfo
.section_info
= NULL
;
3087 /* Write out the string table. */
3088 if (obj_raw_syment_count (abfd
) != 0)
3091 (obj_sym_filepos (abfd
)
3092 + obj_raw_syment_count (abfd
) * symesz
),
3096 #if STRING_SIZE_SIZE == 4
3098 _bfd_stringtab_size (finfo
.strtab
) + STRING_SIZE_SIZE
,
3099 (bfd_byte
*) strbuf
);
3101 #error Change bfd_h_put_32
3104 if (bfd_write (strbuf
, 1, STRING_SIZE_SIZE
, abfd
) != STRING_SIZE_SIZE
)
3107 if (! _bfd_stringtab_emit (abfd
, finfo
.strtab
))
3111 _bfd_stringtab_free (finfo
.strtab
);
3113 /* Setting bfd_get_symcount to 0 will cause write_object_contents to
3114 not try to write out the symbols. */
3115 bfd_get_symcount (abfd
) = 0;
3120 if (debug_merge_allocated
)
3121 coff_debug_merge_hash_table_free (&finfo
.debug_merge
);
3122 if (finfo
.strtab
!= NULL
)
3123 _bfd_stringtab_free (finfo
.strtab
);
3124 if (finfo
.section_info
!= NULL
)
3128 for (i
= 0; i
< abfd
->section_count
; i
++)
3130 if (finfo
.section_info
[i
].relocs
!= NULL
)
3131 free (finfo
.section_info
[i
].relocs
);
3132 if (finfo
.section_info
[i
].rel_hashes
!= NULL
)
3133 free (finfo
.section_info
[i
].rel_hashes
);
3135 free (finfo
.section_info
);
3137 if (finfo
.internal_syms
!= NULL
)
3138 free (finfo
.internal_syms
);
3139 if (finfo
.sec_ptrs
!= NULL
)
3140 free (finfo
.sec_ptrs
);
3141 if (finfo
.sym_indices
!= NULL
)
3142 free (finfo
.sym_indices
);
3143 if (finfo
.outsyms
!= NULL
)
3144 free (finfo
.outsyms
);
3145 if (finfo
.linenos
!= NULL
)
3146 free (finfo
.linenos
);
3147 if (finfo
.contents
!= NULL
)
3148 free (finfo
.contents
);
3149 if (finfo
.external_relocs
!= NULL
)
3150 free (finfo
.external_relocs
);
3151 if (finfo
.internal_relocs
!= NULL
)
3152 free (finfo
.internal_relocs
);
3153 if (external_relocs
!= NULL
)
3154 free (external_relocs
);
3160 /* The transfer vectors that lead the outside world to all of the above. */
3162 #ifdef TARGET_LITTLE_SYM
3166 TARGET_LITTLE_NAME
, /* name or coff-arm-little */
3167 bfd_target_coff_flavour
,
3168 BFD_ENDIAN_LITTLE
, /* data byte order is little */
3169 BFD_ENDIAN_LITTLE
, /* header byte order is little */
3171 (HAS_RELOC
| EXEC_P
| /* FIXME: object flags */
3172 HAS_LINENO
| HAS_DEBUG
|
3173 HAS_SYMS
| HAS_LOCALS
| WP_TEXT
),
3175 (SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
), /* section flags */
3176 0, /* leading char */
3177 '/', /* ar_pad_char */
3178 15, /* ar_max_namelen??? FIXMEmgo */
3180 bfd_getl64
, bfd_getl_signed_64
, bfd_putl64
,
3181 bfd_getl32
, bfd_getl_signed_32
, bfd_putl32
,
3182 bfd_getl16
, bfd_getl_signed_16
, bfd_putl16
, /* data */
3184 bfd_getl64
, bfd_getl_signed_64
, bfd_putl64
,
3185 bfd_getl32
, bfd_getl_signed_32
, bfd_putl32
,
3186 bfd_getl16
, bfd_getl_signed_16
, bfd_putl16
, /* hdrs */
3188 {_bfd_dummy_target
, coff_object_p
, /* bfd_check_format */
3189 bfd_generic_archive_p
, /* _bfd_dummy_target */ coff_object_p
},
3190 {bfd_false
, coff_mkobject
, _bfd_generic_mkarchive
, /* bfd_set_format */
3192 {bfd_false
, coff_write_object_contents
, /* bfd_write_contents */
3193 _bfd_write_archive_contents
, bfd_false
},
3195 BFD_JUMP_TABLE_GENERIC (coff
),
3196 BFD_JUMP_TABLE_COPY (coff
),
3197 BFD_JUMP_TABLE_CORE (_bfd_nocore
),
3198 BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_coff
),
3199 BFD_JUMP_TABLE_SYMBOLS (coff
),
3200 BFD_JUMP_TABLE_RELOCS (coff
),
3201 BFD_JUMP_TABLE_WRITE (coff
),
3202 BFD_JUMP_TABLE_LINK (coff
),
3203 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic
),
3209 #ifdef TARGET_BIG_SYM
3214 bfd_target_coff_flavour
,
3215 BFD_ENDIAN_BIG
, /* data byte order is big */
3216 BFD_ENDIAN_BIG
, /* header byte order is big */
3218 (HAS_RELOC
| EXEC_P
| /* FIXME: object flags */
3219 HAS_LINENO
| HAS_DEBUG
|
3220 HAS_SYMS
| HAS_LOCALS
| WP_TEXT
),
3222 (SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
), /* section flags */
3223 0, /* leading char */
3224 '/', /* ar_pad_char */
3225 15, /* ar_max_namelen??? FIXMEmgo */
3227 bfd_getb64
, bfd_getb_signed_64
, bfd_putb64
,
3228 bfd_getb32
, bfd_getb_signed_32
, bfd_putb32
,
3229 bfd_getb16
, bfd_getb_signed_16
, bfd_putb16
, /* data */
3231 bfd_getb64
, bfd_getb_signed_64
, bfd_putb64
,
3232 bfd_getb32
, bfd_getb_signed_32
, bfd_putb32
,
3233 bfd_getb16
, bfd_getb_signed_16
, bfd_putb16
, /* hdrs */
3235 {_bfd_dummy_target
, coff_object_p
, /* bfd_check_format */
3236 bfd_generic_archive_p
, /* _bfd_dummy_target */ coff_object_p
},
3237 {bfd_false
, coff_mkobject
, _bfd_generic_mkarchive
, /* bfd_set_format */
3239 {bfd_false
, coff_write_object_contents
, /* bfd_write_contents */
3240 _bfd_write_archive_contents
, bfd_false
},
3242 BFD_JUMP_TABLE_GENERIC (coff
),
3243 BFD_JUMP_TABLE_COPY (coff
),
3244 BFD_JUMP_TABLE_CORE (_bfd_nocore
),
3245 BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_coff
),
3246 BFD_JUMP_TABLE_SYMBOLS (coff
),
3247 BFD_JUMP_TABLE_RELOCS (coff
),
3248 BFD_JUMP_TABLE_WRITE (coff
),
3249 BFD_JUMP_TABLE_LINK (coff
),
3250 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic
),