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
,
297 static bfd_reloc_status_type ppc_reflo_reloc
PARAMS ((bfd
*abfd
,
305 static bfd_reloc_status_type ppc_pair_reloc
PARAMS ((bfd
*abfd
,
314 static bfd_reloc_status_type ppc_toc16_reloc
PARAMS ((bfd
*abfd
,
323 static bfd_reloc_status_type ppc_addr32nb_reloc
PARAMS ((bfd
*abfd
,
331 static bfd_reloc_status_type ppc_section_reloc
PARAMS ((bfd
*abfd
,
339 static bfd_reloc_status_type ppc_secrel_reloc
PARAMS ((bfd
*abfd
,
347 static bfd_reloc_status_type ppc_imglue_reloc
PARAMS ((bfd
*abfd
,
357 static boolean in_reloc_p
PARAMS((bfd
*abfd
, reloc_howto_type
*howto
));
360 /* FIXME: It'll take a while to get through all of these. I only need a few to
361 get us started, so those I'll make sure work. Those marked FIXME are either
362 completely unverified or have a specific unknown marked in the comment */
364 /*---------------------------------------------------------------------------*/
366 /* Relocation entries for Windows/NT on PowerPC. */
368 /* From the document "" we find the following listed as used relocs: */
370 /* ABSOLUTE : The noop */
371 /* ADDR[64|32|16] : fields that hold addresses in data fields or the */
372 /* 16 bit displacement field on a load/store. */
373 /* ADDR[24|14] : fields that hold addresses in branch and cond */
374 /* branches. These represent [26|16] bit addresses. */
375 /* The low order 2 bits are preserved. */
376 /* REL[24|14] : branches relative to the Instruction Address */
377 /* register. These represent [26|16] bit addresses, */
378 /* as before. The instruction field will be zero, and */
379 /* the address of the SYM will be inserted at link time. */
380 /* TOCREL16 : 16 bit displacement field referring to a slot in */
382 /* TOCREL14 : 16 bit displacement field, similar to REL14 or ADDR14. */
383 /* ADDR32NB : 32 bit address relative to the virtual origin. */
384 /* (On the alpha, this is always a linker generated thunk)*/
385 /* (i.e. 32bit addr relative to the image base) */
386 /* SECREL : The value is relative to the start of the section */
387 /* containing the symbol. */
388 /* SECTION : access to the header containing the item. Supports the */
389 /* codeview debugger. */
391 /* In particular, note that the document does not indicate that the */
392 /* relocations listed in the header file are used. */
396 /*---------------------------------------------------------------------------*/
398 static reloc_howto_type ppc_coff_howto_table
[] =
400 /* IMAGE_REL_PPC_ABSOLUTE 0x0000 NOP */
402 HOWTO (IMAGE_REL_PPC_ABSOLUTE
, /* type */
404 0, /* size (0 = byte, 1 = short, 2 = long) */
406 false, /* pc_relative */
408 complain_overflow_dont
, /* dont complain_on_overflow */
409 0, /* special_function */
410 "ABSOLUTE", /* name */
411 false, /* partial_inplace */
414 false), /* pcrel_offset */
416 /* IMAGE_REL_PPC_ADDR64 0x0001 64-bit address */
418 HOWTO(IMAGE_REL_PPC_ADDR64
, /* type */
420 3, /* size (0 = byte, 1 = short, 2 = long) */
422 false, /* pc_relative */
424 complain_overflow_bitfield
, /* complain_on_overflow */
425 0, /* special_function */
427 true, /* partial_inplace */
428 MINUS_ONE
, /* src_mask */
429 MINUS_ONE
, /* dst_mask */
430 false), /* pcrel_offset */
432 /* IMAGE_REL_PPC_ADDR32 0x0002 32-bit address */
434 HOWTO (IMAGE_REL_PPC_ADDR32
, /* type */
436 2, /* size (0 = byte, 1 = short, 2 = long) */
438 false, /* pc_relative */
440 complain_overflow_bitfield
, /* complain_on_overflow */
441 0, /* special_function */
443 true, /* partial_inplace */
444 0xffffffff, /* src_mask */
445 0xffffffff, /* dst_mask */
446 false), /* pcrel_offset */
448 /* IMAGE_REL_PPC_ADDR24 0x0003 26-bit address, shifted left 2 (branch absolute) */
449 /* the LI field is in bit 6 through bit 29 is 24 bits, + 2 for the shift */
450 /* Of course, That's the IBM approved bit numbering, which is not what */
451 /* anyone else uses.... The li field is in bit 2 thru 25 */
453 HOWTO (IMAGE_REL_PPC_ADDR24
, /* type */
455 2, /* size (0 = byte, 1 = short, 2 = long) */
457 false, /* pc_relative */
459 complain_overflow_bitfield
, /* complain_on_overflow */
460 0, /* special_function */
462 true, /* partial_inplace */
463 0x07fffffc, /* src_mask */
464 0x07fffffc, /* dst_mask */
465 false), /* pcrel_offset */
467 /* IMAGE_REL_PPC_ADDR16 0x0004 16-bit address */
469 HOWTO (IMAGE_REL_PPC_ADDR16
, /* type */
471 1, /* size (0 = byte, 1 = short, 2 = long) */
473 false, /* pc_relative */
475 complain_overflow_signed
, /* complain_on_overflow */
476 0, /* special_function */
478 true, /* partial_inplace */
479 0xffff, /* src_mask */
480 0xffff, /* dst_mask */
481 false), /* pcrel_offset */
483 /* IMAGE_REL_PPC_ADDR14 0x0005 */
484 /* 16-bit address, shifted left 2 (load doubleword) */
485 /* FIXME: the mask is likely wrong, and the bit position may be as well */
487 HOWTO (IMAGE_REL_PPC_ADDR14
, /* type */
489 1, /* size (0 = byte, 1 = short, 2 = long) */
491 false, /* pc_relative */
493 complain_overflow_signed
, /* complain_on_overflow */
494 0, /* special_function */
496 true, /* partial_inplace */
497 0xffff, /* src_mask */
498 0xffff, /* dst_mask */
499 false), /* pcrel_offset */
501 /* IMAGE_REL_PPC_REL24 0x0006 */
502 /* 26-bit PC-relative offset, shifted left 2 (branch relative) */
504 HOWTO (IMAGE_REL_PPC_REL24
, /* type */
506 2, /* size (0 = byte, 1 = short, 2 = long) */
508 true, /* pc_relative */
510 complain_overflow_signed
, /* complain_on_overflow */
511 0, /* special_function */
513 true, /* partial_inplace */
514 0x3fffffc, /* src_mask */
515 0x3fffffc, /* dst_mask */
516 false), /* pcrel_offset */
518 /* IMAGE_REL_PPC_REL14 0x0007 */
519 /* 16-bit PC-relative offset, shifted left 2 (br cond relative) */
520 /* FIXME: the mask is likely wrong, and the bit position may be as well */
521 /* FIXME: how does it know how far to shift? */
523 HOWTO (IMAGE_REL_PPC_ADDR14
, /* type */
525 1, /* size (0 = byte, 1 = short, 2 = long) */
527 false, /* pc_relative */
529 complain_overflow_signed
, /* complain_on_overflow */
530 0, /* special_function */
532 true, /* partial_inplace */
533 0xffff, /* src_mask */
534 0xffff, /* dst_mask */
535 true), /* pcrel_offset */
537 /* IMAGE_REL_PPC_TOCREL16 0x0008 */
538 /* 16-bit offset from TOC base */
540 HOWTO (IMAGE_REL_PPC_TOCREL16
,/* type */
542 1, /* size (0 = byte, 1 = short, 2 = long) */
544 false, /* pc_relative */
546 complain_overflow_dont
, /* complain_on_overflow */
547 ppc_toc16_reloc
, /* special_function */
548 "TOCREL16", /* name */
549 false, /* partial_inplace */
550 0xffff, /* src_mask */
551 0xffff, /* dst_mask */
552 false), /* pcrel_offset */
554 /* IMAGE_REL_PPC_TOCREL14 0x0009 */
555 /* 16-bit offset from TOC base, shifted left 2 (load doubleword) */
557 HOWTO (IMAGE_REL_PPC_TOCREL14
,/* type */
559 1, /* size (0 = byte, 1 = short, 2 = long) */
561 false, /* pc_relative */
563 complain_overflow_signed
, /* complain_on_overflow */
564 0, /* special_function */
565 "TOCREL14", /* name */
566 false, /* partial_inplace */
567 0xffff, /* src_mask */
568 0xffff, /* dst_mask */
569 false), /* pcrel_offset */
571 /* IMAGE_REL_PPC_ADDR32NB 0x000A */
572 /* 32-bit addr w/ image base */
574 HOWTO (IMAGE_REL_PPC_ADDR32NB
,/* type */
576 2, /* size (0 = byte, 1 = short, 2 = long) */
578 false, /* pc_relative */
580 complain_overflow_signed
, /* complain_on_overflow */
581 0, /* special_function */
582 "ADDR32NB", /* name */
583 true, /* partial_inplace */
584 0xffffffff, /* src_mask */
585 0xffffffff, /* dst_mask */
586 false), /* pcrel_offset */
588 /* IMAGE_REL_PPC_SECREL 0x000B */
589 /* va of containing section (as in an image sectionhdr) */
591 HOWTO (IMAGE_REL_PPC_SECREL
,/* type */
593 2, /* size (0 = byte, 1 = short, 2 = long) */
595 false, /* pc_relative */
597 complain_overflow_signed
, /* complain_on_overflow */
598 ppc_secrel_reloc
, /* special_function */
600 true, /* partial_inplace */
601 0xffffffff, /* src_mask */
602 0xffffffff, /* dst_mask */
603 true), /* pcrel_offset */
605 /* IMAGE_REL_PPC_SECTION 0x000C */
606 /* sectionheader number */
608 HOWTO (IMAGE_REL_PPC_SECTION
,/* type */
610 2, /* size (0 = byte, 1 = short, 2 = long) */
612 false, /* pc_relative */
614 complain_overflow_signed
, /* complain_on_overflow */
615 ppc_section_reloc
, /* special_function */
616 "SECTION", /* name */
617 true, /* partial_inplace */
618 0xffffffff, /* src_mask */
619 0xffffffff, /* dst_mask */
620 true), /* pcrel_offset */
622 /* IMAGE_REL_PPC_IFGLUE 0x000D */
623 /* substitute TOC restore instruction iff symbol is glue code */
625 HOWTO (IMAGE_REL_PPC_IFGLUE
,/* type */
627 2, /* size (0 = byte, 1 = short, 2 = long) */
629 false, /* pc_relative */
631 complain_overflow_signed
, /* complain_on_overflow */
632 0, /* special_function */
634 true, /* partial_inplace */
635 0xffffffff, /* src_mask */
636 0xffffffff, /* dst_mask */
637 false), /* pcrel_offset */
639 /* IMAGE_REL_PPC_IMGLUE 0x000E */
640 /* symbol is glue code; virtual address is TOC restore instruction */
642 HOWTO (IMAGE_REL_PPC_IMGLUE
,/* type */
644 2, /* size (0 = byte, 1 = short, 2 = long) */
646 false, /* pc_relative */
648 complain_overflow_dont
, /* complain_on_overflow */
649 ppc_imglue_reloc
, /* special_function */
651 false, /* partial_inplace */
652 0xffffffff, /* src_mask */
653 0xffffffff, /* dst_mask */
654 false), /* pcrel_offset */
656 /* IMAGE_REL_PPC_SECREL16 0x000F */
657 /* va of containing section (limited to 16 bits) */
659 HOWTO (IMAGE_REL_PPC_SECREL16
,/* type */
661 1, /* size (0 = byte, 1 = short, 2 = long) */
663 false, /* pc_relative */
665 complain_overflow_signed
, /* complain_on_overflow */
666 0, /* special_function */
667 "SECREL16", /* name */
668 true, /* partial_inplace */
669 0xffff, /* src_mask */
670 0xffff, /* dst_mask */
671 true), /* pcrel_offset */
673 /* IMAGE_REL_PPC_REFHI 0x0010 */
675 HOWTO (IMAGE_REL_PPC_REFHI
, /* type */
677 1, /* size (0 = byte, 1 = short, 2 = long) */
679 false, /* pc_relative */
681 complain_overflow_signed
, /* complain_on_overflow */
682 ppc_refhi_reloc
, /* special_function */
684 true, /* partial_inplace */
685 0xffffffff, /* src_mask */
686 0xffffffff, /* dst_mask */
687 false), /* pcrel_offset */
689 /* IMAGE_REL_PPC_REFLO 0x0011 */
691 HOWTO (IMAGE_REL_PPC_REFLO
, /* type */
693 1, /* size (0 = byte, 1 = short, 2 = long) */
695 false, /* pc_relative */
697 complain_overflow_signed
, /* complain_on_overflow */
698 ppc_refhi_reloc
, /* special_function */
700 true, /* partial_inplace */
701 0xffffffff, /* src_mask */
702 0xffffffff, /* dst_mask */
703 false), /* pcrel_offset */
705 /* IMAGE_REL_PPC_PAIR 0x0012 */
707 HOWTO (IMAGE_REL_PPC_PAIR
, /* type */
709 1, /* size (0 = byte, 1 = short, 2 = long) */
711 false, /* pc_relative */
713 complain_overflow_signed
, /* complain_on_overflow */
714 ppc_pair_reloc
, /* special_function */
716 true, /* partial_inplace */
717 0xffffffff, /* src_mask */
718 0xffffffff, /* dst_mask */
719 false), /* pcrel_offset */
721 /* IMAGE_REL_PPC_TOCREL16_DEFN 0x0013 */
722 /* 16-bit offset from TOC base, without causing a definition */
724 HOWTO ( (IMAGE_REL_PPC_TOCREL16
| IMAGE_REL_PPC_TOCDEFN
), /* type */
726 1, /* size (0 = byte, 1 = short, 2 = long) */
728 false, /* pc_relative */
730 complain_overflow_dont
, /* complain_on_overflow */
731 0, /* special_function */
732 "TOCREL16, TOCDEFN", /* name */
733 false, /* partial_inplace */
734 0xffff, /* src_mask */
735 0xffff, /* dst_mask */
736 false), /* pcrel_offset */
743 /* Some really cheezy macros that can be turned on to test stderr :-) */
752 fprintf(stderr,"Unimplemented Relocation -- %s\n",x); \
756 #define DUMP_RELOC(n,r) \
758 fprintf(stderr,"%s sym %d, addr %d, addend %d\n", \
759 n, (*(r->sym_ptr_ptr))->name, \
760 r->address, r->addend); \
763 /* Given a reloc name, n, and a pointer to an internal_reloc,
764 dump out interesting information on the contents
766 #define n_name _n._n_name
767 #define n_zeroes _n._n_n._n_zeroes
768 #define n_offset _n._n_n._n_offset
772 #define DUMP_RELOC2(n,r) \
774 fprintf(stderr,"%s sym %d, r_vaddr %d %s\n", \
775 n, r->r_symndx, r->r_vaddr,\
776 (((r->r_type) & IMAGE_REL_PPC_TOCDEFN) == 0) \
782 #define DUMP_RELOC(n,r)
783 #define DUMP_RELOC2(n,r)
788 /* toc construction and management routines */
789 extern bfd
* bfd_of_toc_owner
;
790 extern long int global_toc_size
;
792 extern long int import_table_size
;
793 extern long int first_thunk_address
;
794 extern long int thunk_size
;
812 struct list_ele
*next
;
814 enum ref_category cat
;
819 extern struct list_ele
*head
;
820 extern struct list_ele
*tail
;
823 record_toc(toc_section
, our_toc_offset
, cat
, name
)
824 asection
*toc_section
;
826 enum ref_category cat
;
829 /* add this entry to our toc addr-offset-name list */
831 t
= bfd_malloc (sizeof (struct list_ele
));
835 t
->offset
= our_toc_offset
;
838 t
->addr
= toc_section
->output_offset
+ our_toc_offset
;
852 #ifdef COFF_IMAGE_WITH_PE
854 /* record a toc offset against a symbol */
856 ppc_record_toc_entry(abfd
, info
, sec
, sym
, toc_kind
)
858 struct bfd_link_info
*info
;
861 enum toc_type toc_kind
;
863 struct ppc_coff_link_hash_entry
*h
;
871 h
= (struct ppc_coff_link_hash_entry
*) (obj_coff_sym_hashes (abfd
)[sym
]);
874 CHECK_EYE(h
->eye_catcher
);
879 local_syms
= obj_coff_local_toc_table(abfd
);
883 /* allocate a table */
885 (int *) bfd_zalloc (abfd
,
886 obj_raw_syment_count(abfd
) * sizeof(int));
889 obj_coff_local_toc_table(abfd
) = local_syms
;
890 for (i
= 0; i
< obj_raw_syment_count(abfd
); ++i
)
892 SET_UNALLOCATED(local_syms
[i
]);
896 if (IS_UNALLOCATED(local_syms
[sym
]))
898 local_syms
[sym
] = global_toc_size
;
899 ret_val
= global_toc_size
;
900 global_toc_size
+= 4;
902 /* The size must fit in a 16bit displacment */
903 if (global_toc_size
>= 65535)
906 "Exceeded toc size of 65535\n");
912 "Setting toc_offset for local sym %d to %d\n",
918 ret_val
= local_syms
[sym
];
921 "toc_offset already set for local sym %d to %d\n",
928 name
= h
->root
.root
.root
.string
;
930 /* check to see if there's a toc slot allocated. If not, do it
931 here. It will be used in relocate_section */
932 if (IS_UNALLOCATED(h
->toc_offset
))
934 h
->toc_offset
= global_toc_size
;
935 ret_val
= global_toc_size
;
936 global_toc_size
+= 4;
938 /* The size must fit in a 16bit displacment */
939 if (global_toc_size
>= 65535)
942 "Exceeded toc size of 65535\n");
948 "Setting toc_offset for sym %d (%s) [h=%p] to %d\n",
949 sym
, name
, h
, ret_val
);
954 ret_val
= h
->toc_offset
;
957 "toc_offset already set for sym %d (%s) [h=%p] to %d\n",
958 sym
, name
, h
, ret_val
);
966 #endif /* COFF_IMAGE_WITH_PE */
970 /* FIXME: record a toc offset against a data-in-toc symbol */
971 /* Now, there is currenly some confusion on what this means. In some
972 compilers one sees the moral equivalent of:
976 refer to the data with a [tocv] qualifier
977 In general, one sees something to indicate that a tocd has been
978 seen, and that would trigger the allocation of data in toc. The IBM
979 docs seem to suggest that anything with the TOCDEFN qualifier should
980 never trigger storage allocation. However, in the kernel32.lib that
981 we've been using for our test bed, there are a couple of variables
982 referenced that fail that test.
984 So it can't work that way.
987 ppc_record_data_in_toc_entry(abfd
, info
, sec
, sym
, toc_kind
)
989 struct bfd_link_info
*info
;
992 enum toc_type toc_kind
;
994 struct ppc_coff_link_hash_entry
*h
= 0;
1000 h
= (struct ppc_coff_link_hash_entry
*) (obj_coff_sym_hashes (abfd
)[sym
]);
1004 local_syms
= obj_coff_local_toc_table(abfd
);
1005 if (local_syms
== 0)
1008 /* allocate a table */
1010 (int *) bfd_zalloc (abfd
,
1011 obj_raw_syment_count(abfd
) * sizeof(int));
1012 if (local_syms
== 0)
1014 obj_coff_local_toc_table(abfd
) = local_syms
;
1015 for (i
= 0; i
< obj_raw_syment_count(abfd
); ++i
)
1017 SET_UNALLOCATED(local_syms
[i
]);
1021 if (IS_UNALLOCATED(local_syms
[sym
]))
1023 local_syms
[sym
] = global_toc_size
;
1024 ret_val
= global_toc_size
;
1025 global_toc_size
+= 4;
1028 "Setting data_in_toc_offset for local sym %d to %d\n",
1034 ret_val
= local_syms
[sym
];
1037 "data_in_toc_offset already set for local sym %d to %d\n",
1044 CHECK_EYE(h
->eye_catcher
);
1046 name
= h
->root
.root
.root
.string
;
1048 /* check to see if there's a toc slot allocated. If not, do it
1049 here. It will be used in relocate_section */
1050 if (IS_UNALLOCATED(h
->toc_offset
))
1053 h
->toc_offset
= global_toc_size
;
1055 ret_val
= global_toc_size
;
1056 /* We're allocating a chunk of the toc, as opposed to a slot */
1057 /* FIXME: alignment? */
1059 global_toc_size
+= 4;
1062 "Setting data_in_toc_offset for sym %d (%s) [h=%p] to %d\n",
1063 sym
, name
, h
, ret_val
);
1068 ret_val
= h
->toc_offset
;
1071 "data_in_toc_offset already set for sym %d (%s) [h=%p] to %d\n",
1072 sym
, name
, h
, ret_val
);
1082 #ifdef COFF_IMAGE_WITH_PE
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
);
1103 #endif /* COFF_IMAGE_WITH_PE */
1107 /* Provided the symbol, returns the value reffed */
1108 static long get_symbol_value
PARAMS ((asymbol
*));
1111 get_symbol_value (symbol
)
1114 long relocation
= 0;
1116 if (bfd_is_com_section (symbol
->section
))
1122 relocation
= symbol
->value
+
1123 symbol
->section
->output_section
->vma
+
1124 symbol
->section
->output_offset
;
1132 /* Return true if this relocation should
1133 appear in the output .reloc section. */
1135 static boolean
in_reloc_p(abfd
, howto
)
1137 reloc_howto_type
*howto
;
1140 (! howto
->pc_relative
)
1141 && (howto
->type
!= IMAGE_REL_PPC_ADDR32NB
)
1142 && (howto
->type
!= IMAGE_REL_PPC_TOCREL16
)
1143 && (howto
->type
!= IMAGE_REL_PPC_IMGLUE
)
1144 && (howto
->type
!= IMAGE_REL_PPC_IFGLUE
)
1145 && (howto
->type
!= IMAGE_REL_PPC_SECREL
)
1146 && (howto
->type
!= IMAGE_REL_PPC_SECTION
)
1147 && (howto
->type
!= IMAGE_REL_PPC_SECREL16
)
1148 && (howto
->type
!= IMAGE_REL_PPC_REFHI
)
1149 && (howto
->type
!= IMAGE_REL_PPC_REFLO
)
1150 && (howto
->type
!= IMAGE_REL_PPC_PAIR
)
1151 && (howto
->type
!= IMAGE_REL_PPC_TOCREL16_DEFN
) ;
1156 /* this function is in charge of performing all the ppc PE relocations */
1157 /* Don't yet know if we want to do this this particular way ... (krk) */
1158 /* FIXME: (it is not yet enabled) */
1160 static bfd_reloc_status_type
1161 pe_ppc_reloc (abfd
, reloc_entry
, symbol_in
, data
, input_section
, output_bfd
,
1164 arelent
*reloc_entry
;
1167 asection
*input_section
;
1169 char **error_message
;
1171 /* the consth relocation comes in two parts, we have to remember
1172 the state between calls, in these variables */
1173 static boolean part1_consth_active
= false;
1174 static unsigned long part1_consth_value
;
1176 unsigned long sym_value
;
1177 unsigned short r_type
;
1178 unsigned long addr
= reloc_entry
->address
; /*+ input_section->vma*/
1180 fprintf(stderr
, "pe_ppc_reloc (%s)\n", TARGET_LITTLE_NAME
);
1182 r_type
= reloc_entry
->howto
->type
;
1186 /* Partial linking - do nothing */
1187 reloc_entry
->address
+= input_section
->output_offset
;
1188 return bfd_reloc_ok
;
1191 if (symbol_in
!= NULL
1192 && bfd_is_und_section (symbol_in
->section
))
1194 /* Keep the state machine happy in case we're called again */
1195 if (r_type
== IMAGE_REL_PPC_REFHI
)
1197 part1_consth_active
= true;
1198 part1_consth_value
= 0;
1200 return(bfd_reloc_undefined
);
1203 if ((part1_consth_active
) && (r_type
!= IMAGE_REL_PPC_PAIR
))
1205 part1_consth_active
= false;
1206 *error_message
= (char *) "Missing PAIR";
1207 return(bfd_reloc_dangerous
);
1211 sym_value
= get_symbol_value(symbol_in
);
1213 return(bfd_reloc_ok
);
1218 /* The reloc processing routine for the optimized COFF linker. */
1221 coff_ppc_relocate_section (output_bfd
, info
, input_bfd
, input_section
,
1222 contents
, relocs
, syms
, sections
)
1224 struct bfd_link_info
*info
;
1226 asection
*input_section
;
1228 struct internal_reloc
*relocs
;
1229 struct internal_syment
*syms
;
1230 asection
**sections
;
1232 struct internal_reloc
*rel
;
1233 struct internal_reloc
*relend
;
1236 asection
*toc_section
= 0;
1238 reloc_howto_type
*howto
= 0;
1242 "pe_ppc_relocate_section (%s) for %s in bfd %s\n",
1244 input_section
->name
,
1245 input_bfd
->filename
);
1249 /* If we are performing a relocateable link, we don't need to do a
1250 thing. The caller will take care of adjusting the reloc
1251 addresses and symbol indices. */
1252 if (info
->relocateable
)
1259 relend
= rel
+ input_section
->reloc_count
;
1260 for (; rel
< relend
; rel
++)
1263 struct ppc_coff_link_hash_entry
*h
;
1264 struct internal_syment
*sym
;
1268 bfd_reloc_status_type rstat
;
1271 unsigned short r_type
= EXTRACT_TYPE (rel
->r_type
);
1272 unsigned short r_flags
= EXTRACT_FLAGS(rel
->r_type
);
1275 /* now examine flags */
1278 fprintf (stderr
, "Reloc with flags found!");
1279 if ( r_flags
& IMAGE_REL_PPC_NEG
)
1280 fprintf (stderr
, " NEG");
1281 if ( r_flags
& IMAGE_REL_PPC_BRTAKEN
)
1282 fprintf (stderr
, " BRTAKEN");
1283 if ( r_flags
& IMAGE_REL_PPC_BRNTAKEN
)
1284 fprintf (stderr
, " BRNTAKEN");
1285 if ( r_flags
& IMAGE_REL_PPC_TOCDEFN
)
1286 fprintf (stderr
, " TOCDEFN");
1287 fprintf(stderr
, "\n");
1291 symndx
= rel
->r_symndx
;
1292 loc
= contents
+ rel
->r_vaddr
- input_section
->vma
;
1294 /* FIXME: check bounds on r_type */
1295 howto
= ppc_coff_howto_table
+ r_type
;
1304 h
= (struct ppc_coff_link_hash_entry
*)
1305 (obj_coff_sym_hashes (input_bfd
)[symndx
]);
1308 CHECK_EYE(h
->eye_catcher
);
1311 sym
= syms
+ symndx
;
1317 /* FIXME: PAIR unsupported in the following code */
1321 sec
= bfd_abs_section_ptr
;
1324 sec
= sections
[symndx
];
1325 val
= (sec
->output_section
->vma
1326 + sec
->output_offset
1333 CHECK_EYE(h
->eye_catcher
);
1335 if (h
->root
.root
.type
== bfd_link_hash_defined
1336 || h
->root
.root
.type
== bfd_link_hash_defweak
)
1338 sec
= h
->root
.root
.u
.def
.section
;
1339 val
= (h
->root
.root
.u
.def
.value
1340 + sec
->output_section
->vma
1341 + sec
->output_offset
);
1346 "missing %s\n",h
->root
.root
.root
.string
);
1347 if (! ((*info
->callbacks
->undefined_symbol
)
1348 (info
, h
->root
.root
.root
.string
, input_bfd
, input_section
,
1349 rel
->r_vaddr
- input_section
->vma
)))
1354 rstat
= bfd_reloc_ok
;
1356 /* Each case must do its own relocation, setting rstat appropriately */
1361 "ERROR: during reloc processing -- unsupported reloc %s\n",
1363 bfd_set_error (bfd_error_bad_value
);
1366 case IMAGE_REL_PPC_TOCREL16
:
1368 bfd_vma our_toc_offset
;
1371 DUMP_RELOC2(howto
->name
, rel
);
1373 if (toc_section
== 0)
1375 toc_section
= bfd_get_section_by_name (bfd_of_toc_owner
,
1380 "BFD of toc owner %p (%s), section addr of %s %p\n",
1381 bfd_of_toc_owner
, bfd_of_toc_owner
->filename
,
1382 TOC_SECTION_NAME
, toc_section
);
1385 if ( toc_section
== NULL
)
1387 fprintf(stderr
, "No Toc section!\n");
1393 * Amazing bit tricks present. As we may have seen earlier, we
1394 * use the 1 bit to tell us whether or not a toc offset has been
1395 * allocated. Now that they've all been allocated, we will use
1396 * the 1 bit to tell us if we've written this particular toc
1401 { /* it is a file local symbol */
1402 int *local_toc_table
;
1405 sym
= syms
+ symndx
;
1406 name
= sym
->_n
._n_name
;
1408 local_toc_table
= obj_coff_local_toc_table(input_bfd
);
1409 our_toc_offset
= local_toc_table
[symndx
];
1411 if (IS_WRITTEN(our_toc_offset
))
1413 /* if it has been written out, it is marked with the
1414 1 bit. Fix up our offset, but do not write it out
1417 MAKE_ADDR_AGAIN(our_toc_offset
);
1421 "Not writing out toc_offset of %d for %s\n",
1422 our_toc_offset
, name
);
1427 /* write out the toc entry */
1428 record_toc(toc_section
, our_toc_offset
, priv
, strdup(name
));
1431 "Writing out toc_offset "
1432 "toc_section (%p,%p)+%d val %d for %s\n",
1434 toc_section
->contents
,
1440 bfd_put_32(output_bfd
,
1442 toc_section
->contents
+ our_toc_offset
);
1444 MARK_AS_WRITTEN(local_toc_table
[symndx
]);
1450 const char *name
= h
->root
.root
.root
.string
;
1451 our_toc_offset
= h
->toc_offset
;
1453 if ((r_flags
& IMAGE_REL_PPC_TOCDEFN
)
1454 == IMAGE_REL_PPC_TOCDEFN
)
1456 /* This is wrong. If tocdefn is on, we must unconditionally
1457 assume the following path */
1458 && IS_UNALLOCATED(our_toc_offset
))
1461 /* This is unbelievable cheese. Some knowledgable asm
1462 hacker has decided to use r2 as a base for loading
1463 a value. He/She does this by setting the tocdefn bit,
1464 and not supplying a toc definition. The behaviour is
1465 then to use the difference between the value of the
1466 symbol and the actual location of the toc as the toc
1469 In fact, what is usually happening is, because the
1470 Import Address Table is mapped immediately following
1471 the toc, some trippy library code trying for speed on
1472 dll linkage, takes advantage of that and considers
1473 the IAT to be part of the toc, thus saving a load.
1477 "TOCDEFN is on, (%s) (%p) our_toc_offset = %x\n",
1478 name
, h
, our_toc_offset
);
1481 our_toc_offset
= val
-
1482 (toc_section
->output_section
->vma
+
1483 toc_section
->output_offset
);
1487 " our_toc_offset set to %x\n", our_toc_offset
);
1490 /* The size must still fit in a 16bit displacment */
1491 if (our_toc_offset
>= 65535)
1494 "TOCDEFN Relocation exceeded "
1495 "displacment of 65535\n");
1499 record_toc(toc_section
, our_toc_offset
, pub
, strdup(name
));
1501 else if (IS_WRITTEN(our_toc_offset
))
1503 /* if it has been written out, it is marked with the
1504 1 bit. Fix up our offset, but do not write it out
1507 MAKE_ADDR_AGAIN(our_toc_offset
);
1510 "Not writing out toc_offset of %d for %s\n",
1511 our_toc_offset
, name
);
1516 record_toc(toc_section
, our_toc_offset
, pub
, strdup(name
));
1519 /* write out the toc entry */
1521 "Writing out toc_offset "
1522 "toc_section (%p,%p)+%d val %d for %s\n",
1524 toc_section
->contents
,
1530 /* write out the toc entry */
1531 bfd_put_32(output_bfd
,
1533 toc_section
->contents
+ our_toc_offset
);
1535 MARK_AS_WRITTEN(h
->toc_offset
);
1536 /* The tricky part is that this is the address that */
1537 /* needs a .reloc entry for it */
1542 if (fixit
&& info
->base_file
)
1544 /* So if this is non pcrelative, and is referenced
1545 to a section or a common symbol, then it needs a reloc */
1547 /* relocation to a symbol in a section which
1548 isn't absolute - we output the address here
1551 bfd_vma addr
= toc_section
->output_section
->vma
1552 + toc_section
->output_offset
+ our_toc_offset
;
1554 if (coff_data(output_bfd
)->pe
)
1555 addr
-= pe_data(output_bfd
)->pe_opthdr
.ImageBase
;
1559 " Toc Section .reloc candidate addr = %x\n", addr
);
1561 fwrite (&addr
, 1,4, (FILE *) info
->base_file
);
1565 /* FIXME: this test is conservative */
1566 if ( (r_flags
& IMAGE_REL_PPC_TOCDEFN
) != IMAGE_REL_PPC_TOCDEFN
&&
1567 our_toc_offset
> toc_section
->_raw_size
)
1570 "reloc offset is bigger than the toc size!\n");
1574 /* Now we know the relocation for this toc reference */
1575 relocation
= our_toc_offset
+ TOC_LOAD_ADJUSTMENT
;
1576 rstat
= _bfd_relocate_contents (howto
,
1582 case IMAGE_REL_PPC_IFGLUE
:
1584 /* To solve this, we need to know whether or not the symbol */
1585 /* appearing on the call instruction is a glue function or not. */
1586 /* A glue function must announce itself via a IMGLUE reloc, and */
1587 /* the reloc contains the required toc restore instruction */
1590 const char *my_name
;
1591 DUMP_RELOC2(howto
->name
, rel
);
1595 my_name
= h
->root
.root
.root
.string
;
1596 if (h
->symbol_is_glue
== 1)
1598 x
= bfd_get_32(input_bfd
, loc
);
1599 bfd_put_32(input_bfd
, h
->glue_insn
, loc
);
1604 case IMAGE_REL_PPC_SECREL
:
1605 /* Unimplemented: codeview debugging information */
1606 /* For fast access to the header of the section
1607 containing the item. */
1609 case IMAGE_REL_PPC_SECTION
:
1610 /* Unimplemented: codeview debugging information */
1611 /* Is used to indicate that the value should be relative
1612 to the beginning of the section that contains the
1615 case IMAGE_REL_PPC_ABSOLUTE
:
1617 const char *my_name
;
1619 my_name
= (syms
+symndx
)->_n
._n_name
;
1622 my_name
= h
->root
.root
.root
.string
;
1626 "Warning: unsupported reloc %s <file %s, section %s>\n",
1628 bfd_get_filename(input_bfd
),
1629 input_section
->name
);
1631 fprintf(stderr
,"sym %ld (%s), r_vaddr %ld (%lx)\n",
1632 rel
->r_symndx
, my_name
, (long) rel
->r_vaddr
,
1633 (unsigned long) rel
->r_vaddr
);
1636 case IMAGE_REL_PPC_IMGLUE
:
1638 /* There is nothing to do now. This reloc was noted in the first
1639 pass over the relocs, and the glue instruction extracted */
1640 const char *my_name
;
1641 if (h
->symbol_is_glue
== 1)
1643 my_name
= h
->root
.root
.root
.string
;
1645 "Warning: previously missed IMGLUE reloc %s <file %s, section %s>\n",
1647 bfd_get_filename(input_bfd
),
1648 input_section
->name
);
1654 case IMAGE_REL_PPC_ADDR32NB
:
1656 struct coff_link_hash_entry
*myh
= 0;
1657 const char *name
= 0;
1658 DUMP_RELOC2(howto
->name
, rel
);
1660 if (strncmp(".idata$2",input_section
->name
,8) == 0 && first_thunk_address
== 0)
1662 /* set magic values */
1664 struct coff_link_hash_entry
*myh
= 0;
1665 myh
= coff_link_hash_lookup (coff_hash_table (info
),
1667 false, false, true);
1668 first_thunk_address
= myh
->root
.u
.def
.value
+
1669 sec
->output_section
->vma
+
1670 sec
->output_offset
-
1671 pe_data(output_bfd
)->pe_opthdr
.ImageBase
;
1673 idata5offset
= myh
->root
.u
.def
.value
;
1674 myh
= coff_link_hash_lookup (coff_hash_table (info
),
1676 false, false, true);
1678 thunk_size
= myh
->root
.u
.def
.value
- idata5offset
;
1679 myh
= coff_link_hash_lookup (coff_hash_table (info
),
1681 false, false, true);
1682 import_table_size
= myh
->root
.u
.def
.value
;
1685 "first computation triggered fta %x, ts %d(%x), its %d(%x)\n",
1686 first_thunk_address
, thunk_size
, thunk_size
, import_table_size
,
1692 { /* it is a file local symbol */
1693 sym
= syms
+ symndx
;
1694 name
= sym
->_n
._n_name
;
1700 name
= h
->root
.root
.root
.string
;
1701 if (strcmp(".idata$2", name
) == 0)
1702 target
= "__idata2_magic__";
1703 else if (strcmp(".idata$4", name
) == 0)
1704 target
= "__idata4_magic__";
1705 else if (strcmp(".idata$5", name
) == 0)
1706 target
= "__idata5_magic__";
1712 myh
= coff_link_hash_lookup (coff_hash_table (info
),
1714 false, false, true);
1717 fprintf(stderr
, "Missing idata magic cookies, "
1718 "this cannot work anyway...\n");
1722 val
= myh
->root
.u
.def
.value
+
1723 sec
->output_section
->vma
+ sec
->output_offset
;
1724 if (first_thunk_address
== 0)
1727 myh
= coff_link_hash_lookup (coff_hash_table (info
),
1729 false, false, true);
1730 first_thunk_address
= myh
->root
.u
.def
.value
+
1731 sec
->output_section
->vma
+
1732 sec
->output_offset
-
1733 pe_data(output_bfd
)->pe_opthdr
.ImageBase
;
1735 idata5offset
= myh
->root
.u
.def
.value
;
1736 myh
= coff_link_hash_lookup (coff_hash_table (info
),
1738 false, false, true);
1740 thunk_size
= myh
->root
.u
.def
.value
- idata5offset
;
1741 myh
= coff_link_hash_lookup (coff_hash_table (info
),
1743 false, false, true);
1744 import_table_size
= myh
->root
.u
.def
.value
;
1748 "second computation triggered fta %x, ts %d(%x), its %d(%x)\n",
1749 first_thunk_address
, thunk_size
, thunk_size
, import_table_size
,
1756 rstat
= _bfd_relocate_contents (howto
,
1759 pe_data(output_bfd
)->pe_opthdr
.ImageBase
,
1764 case IMAGE_REL_PPC_REL24
:
1765 DUMP_RELOC2(howto
->name
, rel
);
1766 val
-= (input_section
->output_section
->vma
1767 + input_section
->output_offset
);
1769 rstat
= _bfd_relocate_contents (howto
,
1774 case IMAGE_REL_PPC_ADDR16
:
1775 case IMAGE_REL_PPC_ADDR24
:
1776 case IMAGE_REL_PPC_ADDR32
:
1777 DUMP_RELOC2(howto
->name
, rel
);
1778 rstat
= _bfd_relocate_contents (howto
,
1785 if ( info
->base_file
)
1787 /* So if this is non pcrelative, and is referenced
1788 to a section or a common symbol, then it needs a reloc */
1789 if (sym
&& pe_data(output_bfd
)->in_reloc_p(output_bfd
, howto
))
1791 /* relocation to a symbol in a section which
1792 isn't absolute - we output the address here
1794 bfd_vma addr
= rel
->r_vaddr
1795 - input_section
->vma
1796 + input_section
->output_offset
1797 + input_section
->output_section
->vma
;
1799 if (coff_data(output_bfd
)->pe
)
1802 bfd_vma before_addr
= addr
;
1804 addr
-= pe_data(output_bfd
)->pe_opthdr
.ImageBase
;
1807 " adjusted down from %x to %x", before_addr
, addr
);
1811 fprintf(stderr
, "\n");
1814 fwrite (&addr
, 1,4, (FILE *) info
->base_file
);
1824 case bfd_reloc_overflow
:
1827 char buf
[SYMNMLEN
+ 1];
1832 name
= h
->root
.root
.root
.string
;
1833 else if (sym
== NULL
)
1835 else if (sym
->_n
._n_n
._n_zeroes
== 0
1836 && sym
->_n
._n_n
._n_offset
!= 0)
1837 name
= obj_coff_strings (input_bfd
) + sym
->_n
._n_n
._n_offset
;
1840 strncpy (buf
, sym
->_n
._n_name
, SYMNMLEN
);
1841 buf
[SYMNMLEN
] = '\0';
1847 name
= _bfd_coff_internal_syment_name (input_bfd
, sym
, buf
);
1853 if (! ((*info
->callbacks
->reloc_overflow
)
1854 (info
, name
, howto
->name
,
1855 (bfd_vma
) 0, input_bfd
,
1856 input_section
, rel
->r_vaddr
- input_section
->vma
)))
1860 "pe_ppc_relocate_section (%s) for %s in bfd %s RETURNING TRUE\n",
1862 input_section
->name
,
1863 input_bfd
->filename
);
1875 "pe_ppc_relocate_section (%s) for %s in bfd %s RETURNING TRUE\n",
1877 input_section
->name
,
1878 input_bfd
->filename
);
1886 #ifdef COFF_IMAGE_WITH_PE
1888 long int global_toc_size
= 4;
1890 bfd
* bfd_of_toc_owner
= 0;
1892 long int import_table_size
;
1893 long int first_thunk_address
;
1894 long int thunk_size
;
1896 struct list_ele
*head
;
1897 struct list_ele
*tail
;
1900 h1
= "\n\t\t\tTOC MAPPING\n\n";
1902 h2
= " TOC disassembly Comments Name\n";
1904 h3
= " Offset spelling (if present)\n";
1917 for(t
= head
; t
!= 0; t
=t
->next
)
1923 else if (t
->cat
== pub
)
1925 else if (t
->cat
== data
)
1926 cat
= "data-in-toc ";
1928 if (t
->offset
> global_toc_size
)
1930 if (t
->offset
<= global_toc_size
+ thunk_size
)
1931 cat
= "IAT reference ";
1935 "**** global_toc_size %ld(%lx), thunk_size %ld(%lx)\n",
1936 global_toc_size
, global_toc_size
, thunk_size
, thunk_size
);
1937 cat
= "Out of bounds!";
1942 " %04lx (%d)", (unsigned long) t
->offset
, t
->offset
- 32768);
1949 fprintf(file
, "\n");
1953 ppc_allocate_toc_section (info
)
1954 struct bfd_link_info
*info
;
1958 static char test_char
= '1';
1960 if ( global_toc_size
== 0 ) /* FIXME: does this get me in trouble? */
1963 if (bfd_of_toc_owner
== 0)
1966 "There is no bfd that owns the toc section!\n");
1970 s
= bfd_get_section_by_name ( bfd_of_toc_owner
, TOC_SECTION_NAME
);
1973 fprintf(stderr
, "No Toc section!\n");
1977 foo
= bfd_alloc(bfd_of_toc_owner
, global_toc_size
);
1978 memset(foo
, test_char
, global_toc_size
);
1980 s
->_raw_size
= s
->_cooked_size
= global_toc_size
;
1987 ppc_process_before_allocation (abfd
, info
)
1989 struct bfd_link_info
*info
;
1992 struct internal_reloc
*i
, *rel
;
1996 "ppc_process_before_allocation: BFD %s\n",
1997 bfd_get_filename(abfd
));
2000 /* here we have a bfd that is to be included on the link. We have a hook
2001 to do reloc rummaging, before section sizes are nailed down. */
2003 _bfd_coff_get_external_symbols(abfd
);
2005 /* rummage around all the relocs and map the toc */
2006 sec
= abfd
->sections
;
2013 for (; sec
!= 0; sec
= sec
->next
)
2019 " section %s reloc count %d\n",
2024 if (sec
->reloc_count
== 0)
2027 /* load the relocs */
2028 /* FIXME: there may be a storage leak here */
2029 i
=_bfd_coff_read_internal_relocs(abfd
,sec
,1,0,0,0);
2034 for (rel
=i
;rel
<i
+sec
->reloc_count
;++rel
)
2036 unsigned short r_type
= EXTRACT_TYPE (rel
->r_type
);
2037 unsigned short r_flags
= EXTRACT_FLAGS(rel
->r_type
);
2040 /* now examine flags */
2043 fprintf (stderr
, "Reloc with flags found!");
2044 if ( r_flags
& IMAGE_REL_PPC_NEG
)
2045 fprintf (stderr
, " NEG");
2046 if ( r_flags
& IMAGE_REL_PPC_BRTAKEN
)
2047 fprintf (stderr
, " BRTAKEN");
2048 if ( r_flags
& IMAGE_REL_PPC_BRNTAKEN
)
2049 fprintf (stderr
, " BRNTAKEN");
2050 if ( r_flags
& IMAGE_REL_PPC_TOCDEFN
)
2051 fprintf (stderr
, " TOCDEFN");
2052 fprintf(stderr
, "\n");
2056 DUMP_RELOC2(ppc_coff_howto_table
[r_type
].name
, rel
);
2060 case IMAGE_REL_PPC_TOCREL16
:
2063 This remains unimplemented for now, as it currently adds
2064 un-necessary elements to the toc. All we need to do today
2065 is not do anything if TOCDEFN is on.
2067 if ( r_flags
& IMAGE_REL_PPC_TOCDEFN
)
2068 toc_offset
= ppc_record_data_in_toc_entry(abfd
, info
, sec
,
2072 toc_offset
= ppc_record_toc_entry(abfd
, info
, sec
,
2073 rel
->r_symndx
, default_toc
);
2075 if ( (r_flags
& IMAGE_REL_PPC_TOCDEFN
) != IMAGE_REL_PPC_TOCDEFN
)
2076 toc_offset
= ppc_record_toc_entry(abfd
, info
, sec
,
2077 rel
->r_symndx
, default_toc
);
2079 case IMAGE_REL_PPC_IMGLUE
:
2080 ppc_mark_symbol_as_glue(abfd
, rel
->r_symndx
, rel
);
2094 static bfd_reloc_status_type
2095 ppc_refhi_reloc (abfd
,
2103 arelent
*reloc_entry
;
2106 asection
*input_section
;
2108 char **error_message
;
2111 DUMP_RELOC("REFHI",reloc_entry
);
2113 if (output_bfd
== (bfd
*) NULL
)
2114 return bfd_reloc_continue
;
2116 return bfd_reloc_undefined
;
2121 static bfd_reloc_status_type
2122 ppc_reflo_reloc (abfd
,
2130 arelent
*reloc_entry
;
2133 asection
*input_section
;
2135 char **error_message
;
2138 DUMP_RELOC("REFLO",reloc_entry
);
2140 if (output_bfd
== (bfd
*) NULL
)
2141 return bfd_reloc_continue
;
2143 return bfd_reloc_undefined
;
2148 static bfd_reloc_status_type
2149 ppc_pair_reloc (abfd
,
2157 arelent
*reloc_entry
;
2160 asection
*input_section
;
2162 char **error_message
;
2165 DUMP_RELOC("PAIR",reloc_entry
);
2167 if (output_bfd
== (bfd
*) NULL
)
2168 return bfd_reloc_continue
;
2170 return bfd_reloc_undefined
;
2174 static bfd_reloc_status_type
2175 ppc_toc16_reloc (abfd
,
2183 arelent
*reloc_entry
;
2186 asection
*input_section
;
2188 char **error_message
;
2190 UN_IMPL("TOCREL16");
2191 DUMP_RELOC("TOCREL16",reloc_entry
);
2193 if (output_bfd
== (bfd
*) NULL
)
2195 return bfd_reloc_continue
;
2198 return bfd_reloc_ok
;
2203 /* ADDR32NB : 32 bit address relative to the virtual origin. */
2204 /* (On the alpha, this is always a linker generated thunk)*/
2205 /* (i.e. 32bit addr relative to the image base) */
2209 static bfd_reloc_status_type
2210 ppc_addr32nb_reloc (abfd
,
2218 arelent
*reloc_entry
;
2221 asection
*input_section
;
2223 char **error_message
;
2225 UN_IMPL("ADDR32NB");
2226 DUMP_RELOC("ADDR32NB",reloc_entry
);
2228 return bfd_reloc_ok
;
2233 static bfd_reloc_status_type
2234 ppc_secrel_reloc (abfd
,
2242 arelent
*reloc_entry
;
2245 asection
*input_section
;
2247 char **error_message
;
2250 DUMP_RELOC("SECREL",reloc_entry
);
2252 if (output_bfd
== (bfd
*) NULL
)
2253 return bfd_reloc_continue
;
2255 return bfd_reloc_ok
;
2258 static bfd_reloc_status_type
2259 ppc_section_reloc (abfd
,
2267 arelent
*reloc_entry
;
2270 asection
*input_section
;
2272 char **error_message
;
2275 DUMP_RELOC("SECTION",reloc_entry
);
2277 if (output_bfd
== (bfd
*) NULL
)
2278 return bfd_reloc_continue
;
2280 return bfd_reloc_ok
;
2283 static bfd_reloc_status_type
2284 ppc_imglue_reloc (abfd
,
2292 arelent
*reloc_entry
;
2295 asection
*input_section
;
2297 char **error_message
;
2300 DUMP_RELOC("IMGLUE",reloc_entry
);
2302 if (output_bfd
== (bfd
*) NULL
)
2303 return bfd_reloc_continue
;
2305 return bfd_reloc_ok
;
2310 #define MAX_RELOC_INDEX \
2311 (sizeof(ppc_coff_howto_table) / sizeof(ppc_coff_howto_table[0]) - 1)
2314 /* FIXME: There is a possiblity that when we read in a reloc from a file,
2315 that there are some bits encoded in the upper portion of the
2316 type field. Not yet implemented.
2318 static void ppc_coff_rtype2howto
PARAMS ((arelent
*relent
,
2319 struct internal_reloc
*internal
));
2322 ppc_coff_rtype2howto (relent
, internal
)
2324 struct internal_reloc
*internal
;
2327 /* We can encode one of three things in the type field, aside from the
2329 1. IMAGE_REL_PPC_NEG - indicates the value field is a subtraction
2330 value, rather than an addition value
2331 2. IMAGE_REL_PPC_BRTAKEN, IMAGE_REL_PPC_BRNTAKEN - indicates that
2332 the branch is expected to be taken or not.
2333 3. IMAGE_REL_PPC_TOCDEFN - toc slot definition in the file
2334 For now, we just strip this stuff to find the type, and ignore it other
2337 reloc_howto_type
*howto
;
2338 unsigned short r_type
= EXTRACT_TYPE (internal
->r_type
);
2339 unsigned short r_flags
= EXTRACT_FLAGS(internal
->r_type
);
2340 unsigned short junk
= EXTRACT_JUNK (internal
->r_type
);
2342 /* the masking process only slices off the bottom byte for r_type. */
2343 if ( r_type
> MAX_RELOC_INDEX
)
2346 "ppc_coff_rtype2howto: reloc index %d out of range [%d, %ld]\n",
2347 internal
->r_type
, 0, (long) MAX_RELOC_INDEX
);
2351 /* check for absolute crap */
2355 "ppc_coff_rtype2howto: reloc index %d contains junk %d\n",
2356 internal
->r_type
, junk
);
2361 /* now examine flags */
2364 fprintf (stderr
, "Reloc with flags found!");
2365 if ( r_flags
& IMAGE_REL_PPC_NEG
)
2366 fprintf (stderr
, " NEG");
2367 if ( r_flags
& IMAGE_REL_PPC_BRTAKEN
)
2368 fprintf (stderr
, " BRTAKEN");
2369 if ( r_flags
& IMAGE_REL_PPC_BRNTAKEN
)
2370 fprintf (stderr
, " BRNTAKEN");
2371 if ( r_flags
& IMAGE_REL_PPC_TOCDEFN
)
2372 fprintf (stderr
, " TOCDEFN");
2373 fprintf(stderr
, "\n");
2379 case IMAGE_REL_PPC_ADDR16
:
2380 case IMAGE_REL_PPC_REL24
:
2381 case IMAGE_REL_PPC_ADDR24
:
2382 case IMAGE_REL_PPC_ADDR32
:
2383 case IMAGE_REL_PPC_IFGLUE
:
2384 case IMAGE_REL_PPC_ADDR32NB
:
2385 case IMAGE_REL_PPC_SECTION
:
2386 case IMAGE_REL_PPC_SECREL
:
2387 DUMP_RELOC2(ppc_coff_howto_table
[r_type
].name
, internal
);
2388 howto
= ppc_coff_howto_table
+ r_type
;
2390 case IMAGE_REL_PPC_IMGLUE
:
2391 DUMP_RELOC2(ppc_coff_howto_table
[r_type
].name
, internal
);
2392 howto
= ppc_coff_howto_table
+ r_type
;
2394 case IMAGE_REL_PPC_TOCREL16
:
2395 DUMP_RELOC2(ppc_coff_howto_table
[r_type
].name
, internal
);
2396 if (r_flags
& IMAGE_REL_PPC_TOCDEFN
)
2397 howto
= ppc_coff_howto_table
+ IMAGE_REL_PPC_TOCREL16_DEFN
;
2399 howto
= ppc_coff_howto_table
+ IMAGE_REL_PPC_TOCREL16
;
2403 "Warning: Unsupported reloc %s [%d] used -- it may not work.\n",
2404 ppc_coff_howto_table
[r_type
].name
,
2406 howto
= ppc_coff_howto_table
+ r_type
;
2410 relent
->howto
= howto
;
2414 static reloc_howto_type
*
2415 coff_ppc_rtype_to_howto (abfd
, sec
, rel
, h
, sym
, addendp
)
2418 struct internal_reloc
*rel
;
2419 struct coff_link_hash_entry
*h
;
2420 struct internal_syment
*sym
;
2423 reloc_howto_type
*howto
;
2425 /* We can encode one of three things in the type field, aside from the
2427 1. IMAGE_REL_PPC_NEG - indicates the value field is a subtraction
2428 value, rather than an addition value
2429 2. IMAGE_REL_PPC_BRTAKEN, IMAGE_REL_PPC_BRNTAKEN - indicates that
2430 the branch is expected to be taken or not.
2431 3. IMAGE_REL_PPC_TOCDEFN - toc slot definition in the file
2432 For now, we just strip this stuff to find the type, and ignore it other
2436 unsigned short r_type
= EXTRACT_TYPE (rel
->r_type
);
2437 unsigned short r_flags
= EXTRACT_FLAGS(rel
->r_type
);
2438 unsigned short junk
= EXTRACT_JUNK (rel
->r_type
);
2440 /* the masking process only slices off the bottom byte for r_type. */
2441 if ( r_type
> MAX_RELOC_INDEX
)
2444 "coff_ppc_rtype_to_howto: index %d out of range [%d, %ld]\n",
2445 r_type
, 0, (long) MAX_RELOC_INDEX
);
2449 /* check for absolute crap */
2453 "coff_ppc_rtype_to_howto: reloc index %d contains junk %d\n",
2459 /* now examine flags */
2462 fprintf (stderr
, "Reloc with flags found!");
2463 if ( r_flags
& IMAGE_REL_PPC_NEG
)
2464 fprintf (stderr
, " NEG");
2465 if ( r_flags
& IMAGE_REL_PPC_BRTAKEN
)
2466 fprintf (stderr
, " BRTAKEN");
2467 if ( r_flags
& IMAGE_REL_PPC_BRNTAKEN
)
2468 fprintf (stderr
, " BRNTAKEN");
2469 if ( r_flags
& IMAGE_REL_PPC_TOCDEFN
)
2470 fprintf (stderr
, " TOCDEFN");
2471 fprintf(stderr
, "\n");
2477 case IMAGE_REL_PPC_ADDR32NB
:
2478 DUMP_RELOC2(ppc_coff_howto_table
[r_type
].name
, rel
);
2479 *addendp
-= pe_data(sec
->output_section
->owner
)->pe_opthdr
.ImageBase
;
2480 howto
= ppc_coff_howto_table
+ r_type
;
2482 case IMAGE_REL_PPC_TOCREL16
:
2483 DUMP_RELOC2(ppc_coff_howto_table
[r_type
].name
, rel
);
2484 if (r_flags
& IMAGE_REL_PPC_TOCDEFN
)
2485 howto
= ppc_coff_howto_table
+ IMAGE_REL_PPC_TOCREL16_DEFN
;
2487 howto
= ppc_coff_howto_table
+ IMAGE_REL_PPC_TOCREL16
;
2489 case IMAGE_REL_PPC_ADDR16
:
2490 case IMAGE_REL_PPC_REL24
:
2491 case IMAGE_REL_PPC_ADDR24
:
2492 case IMAGE_REL_PPC_ADDR32
:
2493 case IMAGE_REL_PPC_IFGLUE
:
2494 case IMAGE_REL_PPC_SECTION
:
2495 case IMAGE_REL_PPC_SECREL
:
2496 DUMP_RELOC2(ppc_coff_howto_table
[r_type
].name
, rel
);
2497 howto
= ppc_coff_howto_table
+ r_type
;
2499 case IMAGE_REL_PPC_IMGLUE
:
2500 DUMP_RELOC2(ppc_coff_howto_table
[r_type
].name
, rel
);
2501 howto
= ppc_coff_howto_table
+ r_type
;
2505 "Warning: Unsupported reloc %s [%d] used -- it may not work.\n",
2506 ppc_coff_howto_table
[r_type
].name
,
2508 howto
= ppc_coff_howto_table
+ r_type
;
2516 /* a cheesy little macro to make the code a little more readable */
2517 #define HOW2MAP(bfd_rtype,ppc_rtype) \
2518 case bfd_rtype: return &ppc_coff_howto_table[ppc_rtype]
2520 static reloc_howto_type
*ppc_coff_reloc_type_lookup
2521 PARAMS ((bfd
*, bfd_reloc_code_real_type
));
2523 static reloc_howto_type
*
2524 ppc_coff_reloc_type_lookup (abfd
, code
)
2526 bfd_reloc_code_real_type code
;
2530 fprintf(stderr
, "ppc_coff_reloc_type_lookup for %s\n",
2531 bfd_get_reloc_code_name(code
));
2536 HOW2MAP(BFD_RELOC_32_GOTOFF
, IMAGE_REL_PPC_IMGLUE
);
2537 HOW2MAP(BFD_RELOC_16_GOT_PCREL
, IMAGE_REL_PPC_IFGLUE
);
2538 HOW2MAP(BFD_RELOC_16
, IMAGE_REL_PPC_ADDR16
);
2539 HOW2MAP(BFD_RELOC_PPC_B26
, IMAGE_REL_PPC_REL24
);
2540 HOW2MAP(BFD_RELOC_PPC_BA26
, IMAGE_REL_PPC_ADDR24
);
2541 HOW2MAP(BFD_RELOC_PPC_TOC16
, IMAGE_REL_PPC_TOCREL16
);
2542 HOW2MAP(BFD_RELOC_16_GOTOFF
, IMAGE_REL_PPC_TOCREL16_DEFN
);
2543 HOW2MAP(BFD_RELOC_32
, IMAGE_REL_PPC_ADDR32
);
2544 HOW2MAP(BFD_RELOC_RVA
, IMAGE_REL_PPC_ADDR32NB
);
2555 /* Tailor coffcode.h -- macro heaven. */
2557 #define RTYPE2HOWTO(cache_ptr, dst) ppc_coff_rtype2howto (cache_ptr, dst)
2559 #ifndef COFF_IMAGE_WITH_PE
2561 ppc_coff_swap_sym_in_hook ();
2564 /* We use the special COFF backend linker, with our own special touch. */
2566 #define coff_bfd_reloc_type_lookup ppc_coff_reloc_type_lookup
2567 #define coff_rtype_to_howto coff_ppc_rtype_to_howto
2568 #define coff_relocate_section coff_ppc_relocate_section
2569 #define coff_bfd_final_link ppc_bfd_coff_final_link
2571 #ifndef COFF_IMAGE_WITH_PE
2572 #define coff_swap_sym_in_hook ppc_coff_swap_sym_in_hook
2575 #define SELECT_RELOC(internal, howto) {internal.r_type=howto->type;}
2577 #define COFF_PAGE_SIZE 0x1000
2579 #define POWERPC_LE_PE
2581 #include "coffcode.h"
2585 #ifndef COFF_IMAGE_WITH_PE
2587 What we're trying to do here is allocate a toc section (early), and attach
2588 it to the last bfd to be processed. This avoids the problem of having a toc
2589 written out before all files have been processed. This code allocates
2590 a toc section for every file, and records the last one seen. There are
2591 at least two problems with this approach:
2592 1. We allocate whole bunches of toc sections that are ignored, but at
2593 at least we will not allocate a toc if no .toc is present.
2594 2. It's not clear to me that being the last bfd read necessarily means
2595 that you are the last bfd closed.
2596 3. Doing it on a "swap in" hook depends on when the "swap in" is called,
2597 and how often, etc. It's not clear to me that there isn't a hole here.
2601 ppc_coff_swap_sym_in_hook (abfd
, ext1
, in1
)
2606 struct internal_syment
*in
= (struct internal_syment
*)in1
;
2608 if (bfd_of_toc_owner
!= 0) /* we already have a toc, so go home */
2611 if (strcmp(in
->_n
._n_name
, ".toc") == 0)
2614 register asection
*s
;
2616 s
= bfd_get_section_by_name ( abfd
, TOC_SECTION_NAME
);
2622 flags
= SEC_ALLOC
| SEC_LOAD
| SEC_HAS_CONTENTS
| SEC_IN_MEMORY
;
2626 "ppc_coff_swap_sym_in_hook: about to create the %s section\n",
2630 s
= bfd_make_section (abfd
, TOC_SECTION_NAME
);
2633 || !bfd_set_section_flags (abfd
, s
, flags
)
2634 || !bfd_set_section_alignment (abfd
, s
, 2))
2637 "toc section allocation failed!\n");
2641 /* save the bfd for later allocation */
2642 bfd_of_toc_owner
= abfd
;
2650 ppc_bfd_coff_final_link ();
2652 #ifndef COFF_IMAGE_WITH_PE
2658 if (abfd
== bfd_of_toc_owner
)
2667 return bfd_of_toc_owner
;
2670 /* this piece of machinery exists only to guarantee that the bfd that holds
2671 the toc section is written last.
2673 This does depend on bfd_make_section attaching a new section to the
2674 end of the section list for the bfd.
2676 This is otherwise intended to be functionally the same as
2677 cofflink.c:_bfd_coff_final_link(). It is specifically different only
2678 where the POWERPC_LE_PE macro modifies the code. It is left in as a
2679 precise form of comment. krk@cygnus.com
2681 #define POWERPC_LE_PE
2684 /* Do the final link step. */
2687 ppc_bfd_coff_final_link (abfd
, info
)
2689 struct bfd_link_info
*info
;
2691 bfd_size_type symesz
;
2692 struct coff_final_link_info finfo
;
2693 boolean debug_merge_allocated
;
2695 struct bfd_link_order
*p
;
2696 size_t max_sym_count
;
2697 size_t max_lineno_count
;
2698 size_t max_reloc_count
;
2699 size_t max_output_reloc_count
;
2700 size_t max_contents_size
;
2701 file_ptr rel_filepos
;
2703 file_ptr line_filepos
;
2704 unsigned int linesz
;
2706 bfd_byte
*external_relocs
= NULL
;
2707 char strbuf
[STRING_SIZE_SIZE
];
2709 symesz
= bfd_coff_symesz (abfd
);
2712 finfo
.output_bfd
= abfd
;
2713 finfo
.strtab
= NULL
;
2714 finfo
.section_info
= NULL
;
2715 finfo
.last_file_index
= -1;
2716 finfo
.internal_syms
= NULL
;
2717 finfo
.sec_ptrs
= NULL
;
2718 finfo
.sym_indices
= NULL
;
2719 finfo
.outsyms
= NULL
;
2720 finfo
.linenos
= NULL
;
2721 finfo
.contents
= NULL
;
2722 finfo
.external_relocs
= NULL
;
2723 finfo
.internal_relocs
= NULL
;
2724 debug_merge_allocated
= false;
2726 coff_data (abfd
)->link_info
= info
;
2728 finfo
.strtab
= _bfd_stringtab_init ();
2729 if (finfo
.strtab
== NULL
)
2732 if (! coff_debug_merge_hash_table_init (&finfo
.debug_merge
))
2734 debug_merge_allocated
= true;
2736 /* Compute the file positions for all the sections. */
2737 if (! abfd
->output_has_begun
)
2738 bfd_coff_compute_section_file_positions (abfd
);
2740 /* Count the line numbers and relocation entries required for the
2741 output file. Set the file positions for the relocs. */
2742 rel_filepos
= obj_relocbase (abfd
);
2743 relsz
= bfd_coff_relsz (abfd
);
2744 max_contents_size
= 0;
2745 max_lineno_count
= 0;
2746 max_reloc_count
= 0;
2748 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
2751 o
->lineno_count
= 0;
2752 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
2755 if (p
->type
== bfd_indirect_link_order
)
2759 sec
= p
->u
.indirect
.section
;
2761 /* Mark all sections which are to be included in the
2762 link. This will normally be every section. We need
2763 to do this so that we can identify any sections which
2764 the linker has decided to not include. */
2765 sec
->linker_mark
= true;
2767 if (info
->strip
== strip_none
2768 || info
->strip
== strip_some
)
2769 o
->lineno_count
+= sec
->lineno_count
;
2771 if (info
->relocateable
)
2772 o
->reloc_count
+= sec
->reloc_count
;
2774 if (sec
->_raw_size
> max_contents_size
)
2775 max_contents_size
= sec
->_raw_size
;
2776 if (sec
->lineno_count
> max_lineno_count
)
2777 max_lineno_count
= sec
->lineno_count
;
2778 if (sec
->reloc_count
> max_reloc_count
)
2779 max_reloc_count
= sec
->reloc_count
;
2781 else if (info
->relocateable
2782 && (p
->type
== bfd_section_reloc_link_order
2783 || p
->type
== bfd_symbol_reloc_link_order
))
2786 if (o
->reloc_count
== 0)
2790 o
->flags
|= SEC_RELOC
;
2791 o
->rel_filepos
= rel_filepos
;
2792 rel_filepos
+= o
->reloc_count
* relsz
;
2796 /* If doing a relocateable link, allocate space for the pointers we
2798 if (info
->relocateable
)
2802 /* We use section_count + 1, rather than section_count, because
2803 the target_index fields are 1 based. */
2804 finfo
.section_info
=
2805 ((struct coff_link_section_info
*)
2806 bfd_malloc ((abfd
->section_count
+ 1)
2807 * sizeof (struct coff_link_section_info
)));
2808 if (finfo
.section_info
== NULL
)
2810 for (i
= 0; i
<= abfd
->section_count
; i
++)
2812 finfo
.section_info
[i
].relocs
= NULL
;
2813 finfo
.section_info
[i
].rel_hashes
= NULL
;
2817 /* We now know the size of the relocs, so we can determine the file
2818 positions of the line numbers. */
2819 line_filepos
= rel_filepos
;
2820 linesz
= bfd_coff_linesz (abfd
);
2821 max_output_reloc_count
= 0;
2822 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
2824 if (o
->lineno_count
== 0)
2825 o
->line_filepos
= 0;
2828 o
->line_filepos
= line_filepos
;
2829 line_filepos
+= o
->lineno_count
* linesz
;
2832 if (o
->reloc_count
!= 0)
2834 /* We don't know the indices of global symbols until we have
2835 written out all the local symbols. For each section in
2836 the output file, we keep an array of pointers to hash
2837 table entries. Each entry in the array corresponds to a
2838 reloc. When we find a reloc against a global symbol, we
2839 set the corresponding entry in this array so that we can
2840 fix up the symbol index after we have written out all the
2843 Because of this problem, we also keep the relocs in
2844 memory until the end of the link. This wastes memory,
2845 but only when doing a relocateable link, which is not the
2847 BFD_ASSERT (info
->relocateable
);
2848 finfo
.section_info
[o
->target_index
].relocs
=
2849 ((struct internal_reloc
*)
2850 bfd_malloc (o
->reloc_count
* sizeof (struct internal_reloc
)));
2851 finfo
.section_info
[o
->target_index
].rel_hashes
=
2852 ((struct coff_link_hash_entry
**)
2853 bfd_malloc (o
->reloc_count
2854 * sizeof (struct coff_link_hash_entry
*)));
2855 if (finfo
.section_info
[o
->target_index
].relocs
== NULL
2856 || finfo
.section_info
[o
->target_index
].rel_hashes
== NULL
)
2859 if (o
->reloc_count
> max_output_reloc_count
)
2860 max_output_reloc_count
= o
->reloc_count
;
2863 /* Reset the reloc and lineno counts, so that we can use them to
2864 count the number of entries we have output so far. */
2866 o
->lineno_count
= 0;
2869 obj_sym_filepos (abfd
) = line_filepos
;
2871 /* Figure out the largest number of symbols in an input BFD. Take
2872 the opportunity to clear the output_has_begun fields of all the
2875 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link_next
)
2879 sub
->output_has_begun
= false;
2880 sz
= obj_raw_syment_count (sub
);
2881 if (sz
> max_sym_count
)
2885 /* Allocate some buffers used while linking. */
2886 finfo
.internal_syms
= ((struct internal_syment
*)
2887 bfd_malloc (max_sym_count
2888 * sizeof (struct internal_syment
)));
2889 finfo
.sec_ptrs
= (asection
**) bfd_malloc (max_sym_count
2890 * sizeof (asection
*));
2891 finfo
.sym_indices
= (long *) bfd_malloc (max_sym_count
* sizeof (long));
2892 finfo
.outsyms
= ((bfd_byte
*)
2893 bfd_malloc ((size_t) ((max_sym_count
+ 1) * symesz
)));
2894 finfo
.linenos
= (bfd_byte
*) bfd_malloc (max_lineno_count
2895 * bfd_coff_linesz (abfd
));
2896 finfo
.contents
= (bfd_byte
*) bfd_malloc (max_contents_size
);
2897 finfo
.external_relocs
= (bfd_byte
*) bfd_malloc (max_reloc_count
* relsz
);
2898 if (! info
->relocateable
)
2899 finfo
.internal_relocs
= ((struct internal_reloc
*)
2900 bfd_malloc (max_reloc_count
2901 * sizeof (struct internal_reloc
)));
2902 if ((finfo
.internal_syms
== NULL
&& max_sym_count
> 0)
2903 || (finfo
.sec_ptrs
== NULL
&& max_sym_count
> 0)
2904 || (finfo
.sym_indices
== NULL
&& max_sym_count
> 0)
2905 || finfo
.outsyms
== NULL
2906 || (finfo
.linenos
== NULL
&& max_lineno_count
> 0)
2907 || (finfo
.contents
== NULL
&& max_contents_size
> 0)
2908 || (finfo
.external_relocs
== NULL
&& max_reloc_count
> 0)
2909 || (! info
->relocateable
2910 && finfo
.internal_relocs
== NULL
2911 && max_reloc_count
> 0))
2914 /* We now know the position of everything in the file, except that
2915 we don't know the size of the symbol table and therefore we don't
2916 know where the string table starts. We just build the string
2917 table in memory as we go along. We process all the relocations
2918 for a single input file at once. */
2919 obj_raw_syment_count (abfd
) = 0;
2921 if (coff_backend_info (abfd
)->_bfd_coff_start_final_link
)
2923 if (! bfd_coff_start_final_link (abfd
, info
))
2927 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
2929 for (p
= o
->link_order_head
; p
!= NULL
; p
= p
->next
)
2931 if (p
->type
== bfd_indirect_link_order
2932 && (bfd_get_flavour (p
->u
.indirect
.section
->owner
)
2933 == bfd_target_coff_flavour
))
2935 sub
= p
->u
.indirect
.section
->owner
;
2936 #ifdef POWERPC_LE_PE
2937 if (! sub
->output_has_begun
&& !ppc_do_last(sub
))
2939 if (! sub
->output_has_begun
)
2942 if (! _bfd_coff_link_input_bfd (&finfo
, sub
))
2944 sub
->output_has_begun
= true;
2947 else if (p
->type
== bfd_section_reloc_link_order
2948 || p
->type
== bfd_symbol_reloc_link_order
)
2950 if (! _bfd_coff_reloc_link_order (abfd
, &finfo
, o
, p
))
2955 if (! _bfd_default_link_order (abfd
, info
, o
, p
))
2961 #ifdef POWERPC_LE_PE
2963 extern bfd
* ppc_get_last();
2964 bfd
* last_one
= ppc_get_last();
2967 if (! _bfd_coff_link_input_bfd (&finfo
, last_one
))
2970 last_one
->output_has_begun
= true;
2974 /* Free up the buffers used by _bfd_coff_link_input_bfd. */
2976 coff_debug_merge_hash_table_free (&finfo
.debug_merge
);
2977 debug_merge_allocated
= false;
2979 if (finfo
.internal_syms
!= NULL
)
2981 free (finfo
.internal_syms
);
2982 finfo
.internal_syms
= NULL
;
2984 if (finfo
.sec_ptrs
!= NULL
)
2986 free (finfo
.sec_ptrs
);
2987 finfo
.sec_ptrs
= NULL
;
2989 if (finfo
.sym_indices
!= NULL
)
2991 free (finfo
.sym_indices
);
2992 finfo
.sym_indices
= NULL
;
2994 if (finfo
.linenos
!= NULL
)
2996 free (finfo
.linenos
);
2997 finfo
.linenos
= NULL
;
2999 if (finfo
.contents
!= NULL
)
3001 free (finfo
.contents
);
3002 finfo
.contents
= NULL
;
3004 if (finfo
.external_relocs
!= NULL
)
3006 free (finfo
.external_relocs
);
3007 finfo
.external_relocs
= NULL
;
3009 if (finfo
.internal_relocs
!= NULL
)
3011 free (finfo
.internal_relocs
);
3012 finfo
.internal_relocs
= NULL
;
3015 /* The value of the last C_FILE symbol is supposed to be the symbol
3016 index of the first external symbol. Write it out again if
3018 if (finfo
.last_file_index
!= -1
3019 && (unsigned int) finfo
.last_file
.n_value
!= obj_raw_syment_count (abfd
))
3021 finfo
.last_file
.n_value
= obj_raw_syment_count (abfd
);
3022 bfd_coff_swap_sym_out (abfd
, (PTR
) &finfo
.last_file
,
3023 (PTR
) finfo
.outsyms
);
3025 (obj_sym_filepos (abfd
)
3026 + finfo
.last_file_index
* symesz
),
3028 || bfd_write (finfo
.outsyms
, symesz
, 1, abfd
) != symesz
)
3032 /* Write out the global symbols. */
3033 finfo
.failed
= false;
3034 coff_link_hash_traverse (coff_hash_table (info
), _bfd_coff_write_global_sym
,
3039 /* The outsyms buffer is used by _bfd_coff_write_global_sym. */
3040 if (finfo
.outsyms
!= NULL
)
3042 free (finfo
.outsyms
);
3043 finfo
.outsyms
= NULL
;
3046 if (info
->relocateable
)
3048 /* Now that we have written out all the global symbols, we know
3049 the symbol indices to use for relocs against them, and we can
3050 finally write out the relocs. */
3051 external_relocs
= ((bfd_byte
*)
3052 bfd_malloc (max_output_reloc_count
* relsz
));
3053 if (external_relocs
== NULL
)
3056 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
3058 struct internal_reloc
*irel
;
3059 struct internal_reloc
*irelend
;
3060 struct coff_link_hash_entry
**rel_hash
;
3063 if (o
->reloc_count
== 0)
3066 irel
= finfo
.section_info
[o
->target_index
].relocs
;
3067 irelend
= irel
+ o
->reloc_count
;
3068 rel_hash
= finfo
.section_info
[o
->target_index
].rel_hashes
;
3069 erel
= external_relocs
;
3070 for (; irel
< irelend
; irel
++, rel_hash
++, erel
+= relsz
)
3072 if (*rel_hash
!= NULL
)
3074 BFD_ASSERT ((*rel_hash
)->indx
>= 0);
3075 irel
->r_symndx
= (*rel_hash
)->indx
;
3077 bfd_coff_swap_reloc_out (abfd
, (PTR
) irel
, (PTR
) erel
);
3080 if (bfd_seek (abfd
, o
->rel_filepos
, SEEK_SET
) != 0
3081 || bfd_write ((PTR
) external_relocs
, relsz
, o
->reloc_count
,
3082 abfd
) != relsz
* o
->reloc_count
)
3086 free (external_relocs
);
3087 external_relocs
= NULL
;
3090 /* Free up the section information. */
3091 if (finfo
.section_info
!= NULL
)
3095 for (i
= 0; i
< abfd
->section_count
; i
++)
3097 if (finfo
.section_info
[i
].relocs
!= NULL
)
3098 free (finfo
.section_info
[i
].relocs
);
3099 if (finfo
.section_info
[i
].rel_hashes
!= NULL
)
3100 free (finfo
.section_info
[i
].rel_hashes
);
3102 free (finfo
.section_info
);
3103 finfo
.section_info
= NULL
;
3106 /* Write out the string table. */
3107 if (obj_raw_syment_count (abfd
) != 0)
3110 (obj_sym_filepos (abfd
)
3111 + obj_raw_syment_count (abfd
) * symesz
),
3115 #if STRING_SIZE_SIZE == 4
3117 _bfd_stringtab_size (finfo
.strtab
) + STRING_SIZE_SIZE
,
3118 (bfd_byte
*) strbuf
);
3120 #error Change bfd_h_put_32
3123 if (bfd_write (strbuf
, 1, STRING_SIZE_SIZE
, abfd
) != STRING_SIZE_SIZE
)
3126 if (! _bfd_stringtab_emit (abfd
, finfo
.strtab
))
3130 _bfd_stringtab_free (finfo
.strtab
);
3132 /* Setting bfd_get_symcount to 0 will cause write_object_contents to
3133 not try to write out the symbols. */
3134 bfd_get_symcount (abfd
) = 0;
3139 if (debug_merge_allocated
)
3140 coff_debug_merge_hash_table_free (&finfo
.debug_merge
);
3141 if (finfo
.strtab
!= NULL
)
3142 _bfd_stringtab_free (finfo
.strtab
);
3143 if (finfo
.section_info
!= NULL
)
3147 for (i
= 0; i
< abfd
->section_count
; i
++)
3149 if (finfo
.section_info
[i
].relocs
!= NULL
)
3150 free (finfo
.section_info
[i
].relocs
);
3151 if (finfo
.section_info
[i
].rel_hashes
!= NULL
)
3152 free (finfo
.section_info
[i
].rel_hashes
);
3154 free (finfo
.section_info
);
3156 if (finfo
.internal_syms
!= NULL
)
3157 free (finfo
.internal_syms
);
3158 if (finfo
.sec_ptrs
!= NULL
)
3159 free (finfo
.sec_ptrs
);
3160 if (finfo
.sym_indices
!= NULL
)
3161 free (finfo
.sym_indices
);
3162 if (finfo
.outsyms
!= NULL
)
3163 free (finfo
.outsyms
);
3164 if (finfo
.linenos
!= NULL
)
3165 free (finfo
.linenos
);
3166 if (finfo
.contents
!= NULL
)
3167 free (finfo
.contents
);
3168 if (finfo
.external_relocs
!= NULL
)
3169 free (finfo
.external_relocs
);
3170 if (finfo
.internal_relocs
!= NULL
)
3171 free (finfo
.internal_relocs
);
3172 if (external_relocs
!= NULL
)
3173 free (external_relocs
);
3179 /* The transfer vectors that lead the outside world to all of the above. */
3181 #ifdef TARGET_LITTLE_SYM
3185 TARGET_LITTLE_NAME
, /* name or coff-arm-little */
3186 bfd_target_coff_flavour
,
3187 BFD_ENDIAN_LITTLE
, /* data byte order is little */
3188 BFD_ENDIAN_LITTLE
, /* header byte order is little */
3190 (HAS_RELOC
| EXEC_P
| /* FIXME: object flags */
3191 HAS_LINENO
| HAS_DEBUG
|
3192 HAS_SYMS
| HAS_LOCALS
| WP_TEXT
),
3194 (SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
), /* section flags */
3195 0, /* leading char */
3196 '/', /* ar_pad_char */
3197 15, /* ar_max_namelen??? FIXMEmgo */
3199 bfd_getl64
, bfd_getl_signed_64
, bfd_putl64
,
3200 bfd_getl32
, bfd_getl_signed_32
, bfd_putl32
,
3201 bfd_getl16
, bfd_getl_signed_16
, bfd_putl16
, /* data */
3203 bfd_getl64
, bfd_getl_signed_64
, bfd_putl64
,
3204 bfd_getl32
, bfd_getl_signed_32
, bfd_putl32
,
3205 bfd_getl16
, bfd_getl_signed_16
, bfd_putl16
, /* hdrs */
3207 {_bfd_dummy_target
, coff_object_p
, /* bfd_check_format */
3208 bfd_generic_archive_p
, /* _bfd_dummy_target */ coff_object_p
},
3209 {bfd_false
, coff_mkobject
, _bfd_generic_mkarchive
, /* bfd_set_format */
3211 {bfd_false
, coff_write_object_contents
, /* bfd_write_contents */
3212 _bfd_write_archive_contents
, bfd_false
},
3214 BFD_JUMP_TABLE_GENERIC (coff
),
3215 BFD_JUMP_TABLE_COPY (coff
),
3216 BFD_JUMP_TABLE_CORE (_bfd_nocore
),
3217 BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_coff
),
3218 BFD_JUMP_TABLE_SYMBOLS (coff
),
3219 BFD_JUMP_TABLE_RELOCS (coff
),
3220 BFD_JUMP_TABLE_WRITE (coff
),
3221 BFD_JUMP_TABLE_LINK (coff
),
3222 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic
),
3228 #ifdef TARGET_BIG_SYM
3233 bfd_target_coff_flavour
,
3234 BFD_ENDIAN_BIG
, /* data byte order is big */
3235 BFD_ENDIAN_BIG
, /* header byte order is big */
3237 (HAS_RELOC
| EXEC_P
| /* FIXME: object flags */
3238 HAS_LINENO
| HAS_DEBUG
|
3239 HAS_SYMS
| HAS_LOCALS
| WP_TEXT
),
3241 (SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
), /* section flags */
3242 0, /* leading char */
3243 '/', /* ar_pad_char */
3244 15, /* ar_max_namelen??? FIXMEmgo */
3246 bfd_getb64
, bfd_getb_signed_64
, bfd_putb64
,
3247 bfd_getb32
, bfd_getb_signed_32
, bfd_putb32
,
3248 bfd_getb16
, bfd_getb_signed_16
, bfd_putb16
, /* data */
3250 bfd_getb64
, bfd_getb_signed_64
, bfd_putb64
,
3251 bfd_getb32
, bfd_getb_signed_32
, bfd_putb32
,
3252 bfd_getb16
, bfd_getb_signed_16
, bfd_putb16
, /* hdrs */
3254 {_bfd_dummy_target
, coff_object_p
, /* bfd_check_format */
3255 bfd_generic_archive_p
, /* _bfd_dummy_target */ coff_object_p
},
3256 {bfd_false
, coff_mkobject
, _bfd_generic_mkarchive
, /* bfd_set_format */
3258 {bfd_false
, coff_write_object_contents
, /* bfd_write_contents */
3259 _bfd_write_archive_contents
, bfd_false
},
3261 BFD_JUMP_TABLE_GENERIC (coff
),
3262 BFD_JUMP_TABLE_COPY (coff
),
3263 BFD_JUMP_TABLE_CORE (_bfd_nocore
),
3264 BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_coff
),
3265 BFD_JUMP_TABLE_SYMBOLS (coff
),
3266 BFD_JUMP_TABLE_RELOCS (coff
),
3267 BFD_JUMP_TABLE_WRITE (coff
),
3268 BFD_JUMP_TABLE_LINK (coff
),
3269 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic
),