1 /* BFD back-end for PowerPC Microsoft Portable Executable files.
2 Copyright 1990, 91, 92, 93, 94, 95, 1996 Free Software Foundation, Inc.
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 = (struct list_ele *) 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 displacement of 65535\n");
1498 record_toc(toc_section, our_toc_offset, pub, strdup(name));
1500 else if (IS_WRITTEN(our_toc_offset))
1502 /* if it has been written out, it is marked with the
1503 1 bit. Fix up our offset, but do not write it out
1506 MAKE_ADDR_AGAIN(our_toc_offset);
1509 "Not writing out toc_offset of %d for %s\n",
1510 our_toc_offset, name);
1515 record_toc(toc_section, our_toc_offset, pub, strdup(name));
1518 /* write out the toc entry */
1520 "Writing out toc_offset "
1521 "toc_section (%p,%p)+%d val %d for %s\n",
1523 toc_section->contents,
1529 /* write out the toc entry */
1530 bfd_put_32(output_bfd,
1532 toc_section->contents + our_toc_offset);
1534 MARK_AS_WRITTEN(h->toc_offset);
1535 /* The tricky part is that this is the address that */
1536 /* needs a .reloc entry for it */
1541 if (fixit && info->base_file)
1543 /* So if this is non pcrelative, and is referenced
1544 to a section or a common symbol, then it needs a reloc */
1546 /* relocation to a symbol in a section which
1547 isn't absolute - we output the address here
1550 bfd_vma addr = toc_section->output_section->vma
1551 + toc_section->output_offset + our_toc_offset;
1553 if (coff_data(output_bfd)->pe)
1554 addr -= pe_data(output_bfd)->pe_opthdr.ImageBase;
1558 " Toc Section .reloc candidate addr = %x\n", addr);
1560 fwrite (&addr, 1,4, (FILE *) info->base_file);
1564 /* FIXME: this test is conservative */
1565 if ( (r_flags & IMAGE_REL_PPC_TOCDEFN) != IMAGE_REL_PPC_TOCDEFN &&
1566 our_toc_offset > toc_section->_raw_size)
1569 "reloc offset is bigger than the toc size!\n");
1573 /* Now we know the relocation for this toc reference */
1574 relocation = our_toc_offset + TOC_LOAD_ADJUSTMENT;
1575 rstat = _bfd_relocate_contents (howto,
1581 case IMAGE_REL_PPC_IFGLUE:
1583 /* To solve this, we need to know whether or not the symbol */
1584 /* appearing on the call instruction is a glue function or not. */
1585 /* A glue function must announce itself via a IMGLUE reloc, and */
1586 /* the reloc contains the required toc restore instruction */
1589 const char *my_name;
1590 DUMP_RELOC2(howto->name, rel);
1594 my_name = h->root.root.root.string;
1595 if (h->symbol_is_glue == 1)
1597 x = bfd_get_32(input_bfd, loc);
1598 bfd_put_32(input_bfd, h->glue_insn, loc);
1603 case IMAGE_REL_PPC_SECREL:
1604 /* Unimplemented: codeview debugging information */
1605 /* For fast access to the header of the section
1606 containing the item. */
1608 case IMAGE_REL_PPC_SECTION:
1609 /* Unimplemented: codeview debugging information */
1610 /* Is used to indicate that the value should be relative
1611 to the beginning of the section that contains the
1614 case IMAGE_REL_PPC_ABSOLUTE:
1616 const char *my_name;
1618 my_name = (syms+symndx)->_n._n_name;
1621 my_name = h->root.root.root.string;
1625 "Warning: unsupported reloc %s <file %s, section %s>\n",
1627 bfd_get_filename(input_bfd),
1628 input_section->name);
1630 fprintf(stderr,"sym %ld (%s), r_vaddr %ld (%lx)\n",
1631 rel->r_symndx, my_name, (long) rel->r_vaddr,
1632 (unsigned long) rel->r_vaddr);
1635 case IMAGE_REL_PPC_IMGLUE:
1637 /* There is nothing to do now. This reloc was noted in the first
1638 pass over the relocs, and the glue instruction extracted */
1639 const char *my_name;
1640 if (h->symbol_is_glue == 1)
1642 my_name = h->root.root.root.string;
1644 "Warning: previously missed IMGLUE reloc %s <file %s, section %s>\n",
1646 bfd_get_filename(input_bfd),
1647 input_section->name);
1653 case IMAGE_REL_PPC_ADDR32NB:
1655 struct coff_link_hash_entry *myh = 0;
1656 const char *name = 0;
1657 DUMP_RELOC2(howto->name, rel);
1659 if (strncmp(".idata$2",input_section->name,8) == 0 && first_thunk_address == 0)
1661 /* set magic values */
1663 struct coff_link_hash_entry *myh = 0;
1664 myh = coff_link_hash_lookup (coff_hash_table (info),
1666 false, false, true);
1667 first_thunk_address = myh->root.u.def.value +
1668 sec->output_section->vma +
1669 sec->output_offset -
1670 pe_data(output_bfd)->pe_opthdr.ImageBase;
1672 idata5offset = myh->root.u.def.value;
1673 myh = coff_link_hash_lookup (coff_hash_table (info),
1675 false, false, true);
1677 thunk_size = myh->root.u.def.value - idata5offset;
1678 myh = coff_link_hash_lookup (coff_hash_table (info),
1680 false, false, true);
1681 import_table_size = myh->root.u.def.value;
1684 "first computation triggered fta %x, ts %d(%x), its %d(%x)\n",
1685 first_thunk_address, thunk_size, thunk_size, import_table_size,
1691 { /* it is a file local symbol */
1692 sym = syms + symndx;
1693 name = sym->_n._n_name;
1699 name = h->root.root.root.string;
1700 if (strcmp(".idata$2", name) == 0)
1701 target = "__idata2_magic__";
1702 else if (strcmp(".idata$4", name) == 0)
1703 target = "__idata4_magic__";
1704 else if (strcmp(".idata$5", name) == 0)
1705 target = "__idata5_magic__";
1711 myh = coff_link_hash_lookup (coff_hash_table (info),
1713 false, false, true);
1716 fprintf(stderr, "Missing idata magic cookies, this cannot work anyway...\n");
1720 val = myh->root.u.def.value +
1721 sec->output_section->vma + sec->output_offset;
1722 if (first_thunk_address == 0)
1725 myh = coff_link_hash_lookup (coff_hash_table (info),
1727 false, false, true);
1728 first_thunk_address = myh->root.u.def.value +
1729 sec->output_section->vma +
1730 sec->output_offset -
1731 pe_data(output_bfd)->pe_opthdr.ImageBase;
1733 idata5offset = myh->root.u.def.value;
1734 myh = coff_link_hash_lookup (coff_hash_table (info),
1736 false, false, true);
1738 thunk_size = myh->root.u.def.value - idata5offset;
1739 myh = coff_link_hash_lookup (coff_hash_table (info),
1741 false, false, true);
1742 import_table_size = myh->root.u.def.value;
1746 "second computation triggered fta %x, ts %d(%x), its %d(%x)\n",
1747 first_thunk_address, thunk_size, thunk_size, import_table_size,
1754 rstat = _bfd_relocate_contents (howto,
1757 pe_data(output_bfd)->pe_opthdr.ImageBase,
1762 case IMAGE_REL_PPC_REL24:
1763 DUMP_RELOC2(howto->name, rel);
1764 val -= (input_section->output_section->vma
1765 + input_section->output_offset);
1767 rstat = _bfd_relocate_contents (howto,
1772 case IMAGE_REL_PPC_ADDR16:
1773 case IMAGE_REL_PPC_ADDR24:
1774 case IMAGE_REL_PPC_ADDR32:
1775 DUMP_RELOC2(howto->name, rel);
1776 rstat = _bfd_relocate_contents (howto,
1783 if ( info->base_file )
1785 /* So if this is non pcrelative, and is referenced
1786 to a section or a common symbol, then it needs a reloc */
1787 if (sym && pe_data(output_bfd)->in_reloc_p(output_bfd, howto))
1789 /* relocation to a symbol in a section which
1790 isn't absolute - we output the address here
1792 bfd_vma addr = rel->r_vaddr
1793 - input_section->vma
1794 + input_section->output_offset
1795 + input_section->output_section->vma;
1797 if (coff_data(output_bfd)->pe)
1800 bfd_vma before_addr = addr;
1802 addr -= pe_data(output_bfd)->pe_opthdr.ImageBase;
1805 " adjusted down from %x to %x", before_addr, addr);
1809 fprintf(stderr, "\n");
1812 fwrite (&addr, 1,4, (FILE *) info->base_file);
1822 case bfd_reloc_overflow:
1825 char buf[SYMNMLEN + 1];
1830 name = h->root.root.root.string;
1831 else if (sym == NULL)
1833 else if (sym->_n._n_n._n_zeroes == 0
1834 && sym->_n._n_n._n_offset != 0)
1835 name = obj_coff_strings (input_bfd) + sym->_n._n_n._n_offset;
1838 strncpy (buf, sym->_n._n_name, SYMNMLEN);
1839 buf[SYMNMLEN] = '\0';
1845 name = _bfd_coff_internal_syment_name (input_bfd, sym, buf);
1851 if (! ((*info->callbacks->reloc_overflow)
1852 (info, name, howto->name,
1853 (bfd_vma) 0, input_bfd,
1854 input_section, rel->r_vaddr - input_section->vma)))
1858 "pe_ppc_relocate_section (%s) for %s in bfd %s RETURNING TRUE\n",
1860 input_section->name,
1861 input_bfd->filename);
1873 "pe_ppc_relocate_section (%s) for %s in bfd %s RETURNING TRUE\n",
1875 input_section->name,
1876 input_bfd->filename);
1884 #ifdef COFF_IMAGE_WITH_PE
1886 long int global_toc_size = 4;
1888 bfd* bfd_of_toc_owner = 0;
1890 long int import_table_size;
1891 long int first_thunk_address;
1892 long int thunk_size;
1894 struct list_ele *head;
1895 struct list_ele *tail;
1898 h1 = "\n\t\t\tTOC MAPPING\n\n";
1900 h2 = " TOC disassembly Comments Name\n";
1902 h3 = " Offset spelling (if present)\n";
1915 for(t = head; t != 0; t=t->next)
1921 else if (t->cat == pub)
1923 else if (t->cat == data)
1924 cat = "data-in-toc ";
1926 if (t->offset > global_toc_size)
1928 if (t->offset <= global_toc_size + thunk_size)
1929 cat = "IAT reference ";
1933 "**** global_toc_size %ld(%lx), thunk_size %ld(%lx)\n",
1934 global_toc_size, global_toc_size, thunk_size, thunk_size);
1935 cat = "Out of bounds!";
1940 " %04lx (%d)", (unsigned long) t->offset, t->offset - 32768);
1947 fprintf(file, "\n");
1951 ppc_allocate_toc_section (info)
1952 struct bfd_link_info *info;
1956 static char test_char = '1';
1958 if ( global_toc_size == 0 ) /* FIXME: does this get me in trouble? */
1961 if (bfd_of_toc_owner == 0)
1964 "There is no bfd that owns the toc section!\n");
1968 s = bfd_get_section_by_name ( bfd_of_toc_owner , TOC_SECTION_NAME);
1971 fprintf(stderr, "No Toc section!\n");
1975 foo = (bfd_byte *) bfd_alloc(bfd_of_toc_owner, global_toc_size);
1976 memset(foo, test_char, global_toc_size);
1978 s->_raw_size = s->_cooked_size = global_toc_size;
1985 ppc_process_before_allocation (abfd, info)
1987 struct bfd_link_info *info;
1990 struct internal_reloc *i, *rel;
1994 "ppc_process_before_allocation: BFD %s\n",
1995 bfd_get_filename(abfd));
1998 /* here we have a bfd that is to be included on the link. We have a hook
1999 to do reloc rummaging, before section sizes are nailed down. */
2001 _bfd_coff_get_external_symbols(abfd);
2003 /* rummage around all the relocs and map the toc */
2004 sec = abfd->sections;
2011 for (; sec != 0; sec = sec->next)
2017 " section %s reloc count %d\n",
2022 if (sec->reloc_count == 0)
2025 /* load the relocs */
2026 /* FIXME: there may be a storage leak here */
2027 i=_bfd_coff_read_internal_relocs(abfd,sec,1,0,0,0);
2032 for (rel=i;rel<i+sec->reloc_count;++rel)
2034 unsigned short r_type = EXTRACT_TYPE (rel->r_type);
2035 unsigned short r_flags = EXTRACT_FLAGS(rel->r_type);
2038 /* now examine flags */
2041 fprintf (stderr, "Reloc with flags found!");
2042 if ( r_flags & IMAGE_REL_PPC_NEG )
2043 fprintf (stderr, " NEG");
2044 if ( r_flags & IMAGE_REL_PPC_BRTAKEN )
2045 fprintf (stderr, " BRTAKEN");
2046 if ( r_flags & IMAGE_REL_PPC_BRNTAKEN )
2047 fprintf (stderr, " BRNTAKEN");
2048 if ( r_flags & IMAGE_REL_PPC_TOCDEFN )
2049 fprintf (stderr, " TOCDEFN");
2050 fprintf(stderr, "\n");
2054 DUMP_RELOC2(ppc_coff_howto_table[r_type].name, rel);
2058 case IMAGE_REL_PPC_TOCREL16:
2061 This remains unimplemented for now, as it currently adds
2062 un-necessary elements to the toc. All we need to do today
2063 is not do anything if TOCDEFN is on.
2065 if ( r_flags & IMAGE_REL_PPC_TOCDEFN )
2066 toc_offset = ppc_record_data_in_toc_entry(abfd, info, sec,
2070 toc_offset = ppc_record_toc_entry(abfd, info, sec,
2071 rel->r_symndx, default_toc);
2073 if ( (r_flags & IMAGE_REL_PPC_TOCDEFN) != IMAGE_REL_PPC_TOCDEFN )
2074 toc_offset = ppc_record_toc_entry(abfd, info, sec,
2075 rel->r_symndx, default_toc);
2077 case IMAGE_REL_PPC_IMGLUE:
2078 ppc_mark_symbol_as_glue(abfd, rel->r_symndx, rel);
2092 static bfd_reloc_status_type
2093 ppc_refhi_reloc (abfd,
2101 arelent *reloc_entry;
2104 asection *input_section;
2106 char **error_message;
2109 DUMP_RELOC("REFHI",reloc_entry);
2111 if (output_bfd == (bfd *) NULL)
2112 return bfd_reloc_continue;
2114 return bfd_reloc_undefined;
2119 static bfd_reloc_status_type
2120 ppc_reflo_reloc (abfd,
2128 arelent *reloc_entry;
2131 asection *input_section;
2133 char **error_message;
2136 DUMP_RELOC("REFLO",reloc_entry);
2138 if (output_bfd == (bfd *) NULL)
2139 return bfd_reloc_continue;
2141 return bfd_reloc_undefined;
2146 static bfd_reloc_status_type
2147 ppc_pair_reloc (abfd,
2155 arelent *reloc_entry;
2158 asection *input_section;
2160 char **error_message;
2163 DUMP_RELOC("PAIR",reloc_entry);
2165 if (output_bfd == (bfd *) NULL)
2166 return bfd_reloc_continue;
2168 return bfd_reloc_undefined;
2172 static bfd_reloc_status_type
2173 ppc_toc16_reloc (abfd,
2181 arelent *reloc_entry;
2184 asection *input_section;
2186 char **error_message;
2188 UN_IMPL("TOCREL16");
2189 DUMP_RELOC("TOCREL16",reloc_entry);
2191 if (output_bfd == (bfd *) NULL)
2193 return bfd_reloc_continue;
2196 return bfd_reloc_ok;
2201 /* ADDR32NB : 32 bit address relative to the virtual origin. */
2202 /* (On the alpha, this is always a linker generated thunk)*/
2203 /* (i.e. 32bit addr relative to the image base) */
2207 static bfd_reloc_status_type
2208 ppc_addr32nb_reloc (abfd,
2216 arelent *reloc_entry;
2219 asection *input_section;
2221 char **error_message;
2223 UN_IMPL("ADDR32NB");
2224 DUMP_RELOC("ADDR32NB",reloc_entry);
2226 return bfd_reloc_ok;
2231 static bfd_reloc_status_type
2232 ppc_secrel_reloc (abfd,
2240 arelent *reloc_entry;
2243 asection *input_section;
2245 char **error_message;
2248 DUMP_RELOC("SECREL",reloc_entry);
2250 if (output_bfd == (bfd *) NULL)
2251 return bfd_reloc_continue;
2253 return bfd_reloc_ok;
2256 static bfd_reloc_status_type
2257 ppc_section_reloc (abfd,
2265 arelent *reloc_entry;
2268 asection *input_section;
2270 char **error_message;
2273 DUMP_RELOC("SECTION",reloc_entry);
2275 if (output_bfd == (bfd *) NULL)
2276 return bfd_reloc_continue;
2278 return bfd_reloc_ok;
2281 static bfd_reloc_status_type
2282 ppc_imglue_reloc (abfd,
2290 arelent *reloc_entry;
2293 asection *input_section;
2295 char **error_message;
2298 DUMP_RELOC("IMGLUE",reloc_entry);
2300 if (output_bfd == (bfd *) NULL)
2301 return bfd_reloc_continue;
2303 return bfd_reloc_ok;
2308 #define MAX_RELOC_INDEX \
2309 (sizeof(ppc_coff_howto_table) / sizeof(ppc_coff_howto_table[0]) - 1)
2312 /* FIXME: There is a possiblity that when we read in a reloc from a file,
2313 that there are some bits encoded in the upper portion of the
2314 type field. Not yet implemented.
2316 static void ppc_coff_rtype2howto PARAMS ((arelent *relent,
2317 struct internal_reloc *internal));
2320 ppc_coff_rtype2howto (relent, internal)
2322 struct internal_reloc *internal;
2325 /* We can encode one of three things in the type field, aside from the
2327 1. IMAGE_REL_PPC_NEG - indicates the value field is a subtraction
2328 value, rather than an addition value
2329 2. IMAGE_REL_PPC_BRTAKEN, IMAGE_REL_PPC_BRNTAKEN - indicates that
2330 the branch is expected to be taken or not.
2331 3. IMAGE_REL_PPC_TOCDEFN - toc slot definition in the file
2332 For now, we just strip this stuff to find the type, and ignore it other
2335 reloc_howto_type *howto;
2336 unsigned short r_type = EXTRACT_TYPE (internal->r_type);
2337 unsigned short r_flags = EXTRACT_FLAGS(internal->r_type);
2338 unsigned short junk = EXTRACT_JUNK (internal->r_type);
2340 /* the masking process only slices off the bottom byte for r_type. */
2341 if ( r_type > MAX_RELOC_INDEX )
2344 "ppc_coff_rtype2howto: reloc index %d out of range [%d, %ld]\n",
2345 internal->r_type, 0, (long) MAX_RELOC_INDEX);
2349 /* check for absolute crap */
2353 "ppc_coff_rtype2howto: reloc index %d contains junk %d\n",
2354 internal->r_type, junk);
2359 /* now examine flags */
2362 fprintf (stderr, "Reloc with flags found!");
2363 if ( r_flags & IMAGE_REL_PPC_NEG )
2364 fprintf (stderr, " NEG");
2365 if ( r_flags & IMAGE_REL_PPC_BRTAKEN )
2366 fprintf (stderr, " BRTAKEN");
2367 if ( r_flags & IMAGE_REL_PPC_BRNTAKEN )
2368 fprintf (stderr, " BRNTAKEN");
2369 if ( r_flags & IMAGE_REL_PPC_TOCDEFN )
2370 fprintf (stderr, " TOCDEFN");
2371 fprintf(stderr, "\n");
2377 case IMAGE_REL_PPC_ADDR16:
2378 case IMAGE_REL_PPC_REL24:
2379 case IMAGE_REL_PPC_ADDR24:
2380 case IMAGE_REL_PPC_ADDR32:
2381 case IMAGE_REL_PPC_IFGLUE:
2382 case IMAGE_REL_PPC_ADDR32NB:
2383 case IMAGE_REL_PPC_SECTION:
2384 case IMAGE_REL_PPC_SECREL:
2385 DUMP_RELOC2(ppc_coff_howto_table[r_type].name, internal);
2386 howto = ppc_coff_howto_table + r_type;
2388 case IMAGE_REL_PPC_IMGLUE:
2389 DUMP_RELOC2(ppc_coff_howto_table[r_type].name, internal);
2390 howto = ppc_coff_howto_table + r_type;
2392 case IMAGE_REL_PPC_TOCREL16:
2393 DUMP_RELOC2(ppc_coff_howto_table[r_type].name, internal);
2394 if (r_flags & IMAGE_REL_PPC_TOCDEFN)
2395 howto = ppc_coff_howto_table + IMAGE_REL_PPC_TOCREL16_DEFN;
2397 howto = ppc_coff_howto_table + IMAGE_REL_PPC_TOCREL16;
2401 "Warning: Unsupported reloc %s [%d] used -- it may not work.\n",
2402 ppc_coff_howto_table[r_type].name,
2404 howto = ppc_coff_howto_table + r_type;
2408 relent->howto = howto;
2412 static reloc_howto_type *
2413 coff_ppc_rtype_to_howto (abfd, sec, rel, h, sym, addendp)
2416 struct internal_reloc *rel;
2417 struct coff_link_hash_entry *h;
2418 struct internal_syment *sym;
2421 reloc_howto_type *howto;
2423 /* We can encode one of three things in the type field, aside from the
2425 1. IMAGE_REL_PPC_NEG - indicates the value field is a subtraction
2426 value, rather than an addition value
2427 2. IMAGE_REL_PPC_BRTAKEN, IMAGE_REL_PPC_BRNTAKEN - indicates that
2428 the branch is expected to be taken or not.
2429 3. IMAGE_REL_PPC_TOCDEFN - toc slot definition in the file
2430 For now, we just strip this stuff to find the type, and ignore it other
2434 unsigned short r_type = EXTRACT_TYPE (rel->r_type);
2435 unsigned short r_flags = EXTRACT_FLAGS(rel->r_type);
2436 unsigned short junk = EXTRACT_JUNK (rel->r_type);
2438 /* the masking process only slices off the bottom byte for r_type. */
2439 if ( r_type > MAX_RELOC_INDEX )
2442 "coff_ppc_rtype_to_howto: index %d out of range [%d, %ld]\n",
2443 r_type, 0, (long) MAX_RELOC_INDEX);
2447 /* check for absolute crap */
2451 "coff_ppc_rtype_to_howto: reloc index %d contains junk %d\n",
2457 /* now examine flags */
2460 fprintf (stderr, "Reloc with flags found!");
2461 if ( r_flags & IMAGE_REL_PPC_NEG )
2462 fprintf (stderr, " NEG");
2463 if ( r_flags & IMAGE_REL_PPC_BRTAKEN )
2464 fprintf (stderr, " BRTAKEN");
2465 if ( r_flags & IMAGE_REL_PPC_BRNTAKEN )
2466 fprintf (stderr, " BRNTAKEN");
2467 if ( r_flags & IMAGE_REL_PPC_TOCDEFN )
2468 fprintf (stderr, " TOCDEFN");
2469 fprintf(stderr, "\n");
2475 case IMAGE_REL_PPC_ADDR32NB:
2476 DUMP_RELOC2(ppc_coff_howto_table[r_type].name, rel);
2477 *addendp -= pe_data(sec->output_section->owner)->pe_opthdr.ImageBase;
2478 howto = ppc_coff_howto_table + r_type;
2480 case IMAGE_REL_PPC_TOCREL16:
2481 DUMP_RELOC2(ppc_coff_howto_table[r_type].name, rel);
2482 if (r_flags & IMAGE_REL_PPC_TOCDEFN)
2483 howto = ppc_coff_howto_table + IMAGE_REL_PPC_TOCREL16_DEFN;
2485 howto = ppc_coff_howto_table + IMAGE_REL_PPC_TOCREL16;
2487 case IMAGE_REL_PPC_ADDR16:
2488 case IMAGE_REL_PPC_REL24:
2489 case IMAGE_REL_PPC_ADDR24:
2490 case IMAGE_REL_PPC_ADDR32:
2491 case IMAGE_REL_PPC_IFGLUE:
2492 case IMAGE_REL_PPC_SECTION:
2493 case IMAGE_REL_PPC_SECREL:
2494 DUMP_RELOC2(ppc_coff_howto_table[r_type].name, rel);
2495 howto = ppc_coff_howto_table + r_type;
2497 case IMAGE_REL_PPC_IMGLUE:
2498 DUMP_RELOC2(ppc_coff_howto_table[r_type].name, rel);
2499 howto = ppc_coff_howto_table + r_type;
2503 "Warning: Unsupported reloc %s [%d] used -- it may not work.\n",
2504 ppc_coff_howto_table[r_type].name,
2506 howto = ppc_coff_howto_table + r_type;
2514 /* a cheesy little macro to make the code a little more readable */
2515 #define HOW2MAP(bfd_rtype,ppc_rtype) \
2516 case bfd_rtype: return &ppc_coff_howto_table[ppc_rtype]
2518 static reloc_howto_type *ppc_coff_reloc_type_lookup
2519 PARAMS ((bfd *, bfd_reloc_code_real_type));
2521 static reloc_howto_type *
2522 ppc_coff_reloc_type_lookup (abfd, code)
2524 bfd_reloc_code_real_type code;
2528 fprintf(stderr, "ppc_coff_reloc_type_lookup for %s\n",
2529 bfd_get_reloc_code_name(code));
2534 HOW2MAP(BFD_RELOC_32_GOTOFF, IMAGE_REL_PPC_IMGLUE);
2535 HOW2MAP(BFD_RELOC_16_GOT_PCREL, IMAGE_REL_PPC_IFGLUE);
2536 HOW2MAP(BFD_RELOC_16, IMAGE_REL_PPC_ADDR16);
2537 HOW2MAP(BFD_RELOC_PPC_B26, IMAGE_REL_PPC_REL24);
2538 HOW2MAP(BFD_RELOC_PPC_BA26, IMAGE_REL_PPC_ADDR24);
2539 HOW2MAP(BFD_RELOC_PPC_TOC16, IMAGE_REL_PPC_TOCREL16);
2540 HOW2MAP(BFD_RELOC_16_GOTOFF, IMAGE_REL_PPC_TOCREL16_DEFN);
2541 HOW2MAP(BFD_RELOC_32, IMAGE_REL_PPC_ADDR32);
2542 HOW2MAP(BFD_RELOC_RVA, IMAGE_REL_PPC_ADDR32NB);
2552 /* Tailor coffcode.h -- macro heaven. */
2554 #define RTYPE2HOWTO(cache_ptr, dst) ppc_coff_rtype2howto (cache_ptr, dst)
2556 #ifndef COFF_IMAGE_WITH_PE
2558 ppc_coff_swap_sym_in_hook ();
2561 /* We use the special COFF backend linker, with our own special touch. */
2563 #define coff_bfd_reloc_type_lookup ppc_coff_reloc_type_lookup
2564 #define coff_rtype_to_howto coff_ppc_rtype_to_howto
2565 #define coff_relocate_section coff_ppc_relocate_section
2566 #define coff_bfd_final_link ppc_bfd_coff_final_link
2568 #ifndef COFF_IMAGE_WITH_PE
2569 #define coff_swap_sym_in_hook ppc_coff_swap_sym_in_hook
2572 #define SELECT_RELOC(internal, howto) {internal.r_type=howto->type;}
2574 #define COFF_PAGE_SIZE 0x1000
2576 #define POWERPC_LE_PE
2578 #include "coffcode.h"
2582 #ifndef COFF_IMAGE_WITH_PE
2584 What we're trying to do here is allocate a toc section (early), and attach
2585 it to the last bfd to be processed. This avoids the problem of having a toc
2586 written out before all files have been processed. This code allocates
2587 a toc section for every file, and records the last one seen. There are
2588 at least two problems with this approach:
2589 1. We allocate whole bunches of toc sections that are ignored, but at
2590 at least we will not allocate a toc if no .toc is present.
2591 2. It's not clear to me that being the last bfd read necessarily means
2592 that you are the last bfd closed.
2593 3. Doing it on a "swap in" hook depends on when the "swap in" is called,
2594 and how often, etc. It's not clear to me that there isn't a hole here.
2598 ppc_coff_swap_sym_in_hook (abfd, ext1, in1)
2603 struct internal_syment *in = (struct internal_syment *)in1;
2605 if (bfd_of_toc_owner != 0) /* we already have a toc, so go home */
2608 if (strcmp(in->_n._n_name, ".toc") == 0)
2611 register asection *s;
2613 s = bfd_get_section_by_name ( abfd , TOC_SECTION_NAME);
2619 flags = SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY ;
2623 "ppc_coff_swap_sym_in_hook: about to create the %s section\n",
2627 s = bfd_make_section (abfd, TOC_SECTION_NAME);
2630 || !bfd_set_section_flags (abfd, s, flags)
2631 || !bfd_set_section_alignment (abfd, s, 2))
2634 "toc section allocation failed!\n");
2638 /* save the bfd for later allocation */
2639 bfd_of_toc_owner = abfd;
2647 ppc_bfd_coff_final_link ();
2649 #ifndef COFF_IMAGE_WITH_PE
2655 if (abfd == bfd_of_toc_owner)
2664 return bfd_of_toc_owner;
2667 /* this piece of machinery exists only to guarantee that the bfd that holds
2668 the toc section is written last.
2670 This does depend on bfd_make_section attaching a new section to the
2671 end of the section list for the bfd.
2673 This is otherwise intended to be functionally the same as
2674 cofflink.c:_bfd_coff_final_link(). It is specifically different only
2675 where the POWERPC_LE_PE macro modifies the code. It is left in as a
2678 #define POWERPC_LE_PE
2681 /* Do the final link step. */
2684 ppc_bfd_coff_final_link (abfd, info)
2686 struct bfd_link_info *info;
2688 bfd_size_type symesz;
2689 struct coff_final_link_info finfo;
2690 boolean debug_merge_allocated;
2692 struct bfd_link_order *p;
2693 size_t max_sym_count;
2694 size_t max_lineno_count;
2695 size_t max_reloc_count;
2696 size_t max_output_reloc_count;
2697 size_t max_contents_size;
2698 file_ptr rel_filepos;
2700 file_ptr line_filepos;
2701 unsigned int linesz;
2703 bfd_byte *external_relocs = NULL;
2704 char strbuf[STRING_SIZE_SIZE];
2706 symesz = bfd_coff_symesz (abfd);
2709 finfo.output_bfd = abfd;
2710 finfo.strtab = NULL;
2711 finfo.section_info = NULL;
2712 finfo.last_file_index = -1;
2713 finfo.last_bf_index = -1;
2714 finfo.internal_syms = NULL;
2715 finfo.sec_ptrs = NULL;
2716 finfo.sym_indices = NULL;
2717 finfo.outsyms = NULL;
2718 finfo.linenos = NULL;
2719 finfo.contents = NULL;
2720 finfo.external_relocs = NULL;
2721 finfo.internal_relocs = NULL;
2722 debug_merge_allocated = false;
2724 coff_data (abfd)->link_info = info;
2726 finfo.strtab = _bfd_stringtab_init ();
2727 if (finfo.strtab == NULL)
2730 if (! coff_debug_merge_hash_table_init (&finfo.debug_merge))
2732 debug_merge_allocated = true;
2734 /* Compute the file positions for all the sections. */
2735 if (! abfd->output_has_begun)
2736 bfd_coff_compute_section_file_positions (abfd);
2738 /* Count the line numbers and relocation entries required for the
2739 output file. Set the file positions for the relocs. */
2740 rel_filepos = obj_relocbase (abfd);
2741 relsz = bfd_coff_relsz (abfd);
2742 max_contents_size = 0;
2743 max_lineno_count = 0;
2744 max_reloc_count = 0;
2746 for (o = abfd->sections; o != NULL; o = o->next)
2749 o->lineno_count = 0;
2750 for (p = o->link_order_head; p != NULL; p = p->next)
2753 if (p->type == bfd_indirect_link_order)
2757 sec = p->u.indirect.section;
2759 /* Mark all sections which are to be included in the
2760 link. This will normally be every section. We need
2761 to do this so that we can identify any sections which
2762 the linker has decided to not include. */
2763 sec->linker_mark = true;
2765 if (info->strip == strip_none
2766 || info->strip == strip_some)
2767 o->lineno_count += sec->lineno_count;
2769 if (info->relocateable)
2770 o->reloc_count += sec->reloc_count;
2772 if (sec->_raw_size > max_contents_size)
2773 max_contents_size = sec->_raw_size;
2774 if (sec->lineno_count > max_lineno_count)
2775 max_lineno_count = sec->lineno_count;
2776 if (sec->reloc_count > max_reloc_count)
2777 max_reloc_count = sec->reloc_count;
2779 else if (info->relocateable
2780 && (p->type == bfd_section_reloc_link_order
2781 || p->type == bfd_symbol_reloc_link_order))
2784 if (o->reloc_count == 0)
2788 o->flags |= SEC_RELOC;
2789 o->rel_filepos = rel_filepos;
2790 rel_filepos += o->reloc_count * relsz;
2794 /* If doing a relocateable link, allocate space for the pointers we
2796 if (info->relocateable)
2800 /* We use section_count + 1, rather than section_count, because
2801 the target_index fields are 1 based. */
2802 finfo.section_info =
2803 ((struct coff_link_section_info *)
2804 bfd_malloc ((abfd->section_count + 1)
2805 * sizeof (struct coff_link_section_info)));
2806 if (finfo.section_info == NULL)
2808 for (i = 0; i <= abfd->section_count; i++)
2810 finfo.section_info[i].relocs = NULL;
2811 finfo.section_info[i].rel_hashes = NULL;
2815 /* We now know the size of the relocs, so we can determine the file
2816 positions of the line numbers. */
2817 line_filepos = rel_filepos;
2818 linesz = bfd_coff_linesz (abfd);
2819 max_output_reloc_count = 0;
2820 for (o = abfd->sections; o != NULL; o = o->next)
2822 if (o->lineno_count == 0)
2823 o->line_filepos = 0;
2826 o->line_filepos = line_filepos;
2827 line_filepos += o->lineno_count * linesz;
2830 if (o->reloc_count != 0)
2832 /* We don't know the indices of global symbols until we have
2833 written out all the local symbols. For each section in
2834 the output file, we keep an array of pointers to hash
2835 table entries. Each entry in the array corresponds to a
2836 reloc. When we find a reloc against a global symbol, we
2837 set the corresponding entry in this array so that we can
2838 fix up the symbol index after we have written out all the
2841 Because of this problem, we also keep the relocs in
2842 memory until the end of the link. This wastes memory,
2843 but only when doing a relocateable link, which is not the
2845 BFD_ASSERT (info->relocateable);
2846 finfo.section_info[o->target_index].relocs =
2847 ((struct internal_reloc *)
2848 bfd_malloc (o->reloc_count * sizeof (struct internal_reloc)));
2849 finfo.section_info[o->target_index].rel_hashes =
2850 ((struct coff_link_hash_entry **)
2851 bfd_malloc (o->reloc_count
2852 * sizeof (struct coff_link_hash_entry *)));
2853 if (finfo.section_info[o->target_index].relocs == NULL
2854 || finfo.section_info[o->target_index].rel_hashes == NULL)
2857 if (o->reloc_count > max_output_reloc_count)
2858 max_output_reloc_count = o->reloc_count;
2861 /* Reset the reloc and lineno counts, so that we can use them to
2862 count the number of entries we have output so far. */
2864 o->lineno_count = 0;
2867 obj_sym_filepos (abfd) = line_filepos;
2869 /* Figure out the largest number of symbols in an input BFD. Take
2870 the opportunity to clear the output_has_begun fields of all the
2873 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
2877 sub->output_has_begun = false;
2878 sz = obj_raw_syment_count (sub);
2879 if (sz > max_sym_count)
2883 /* Allocate some buffers used while linking. */
2884 finfo.internal_syms = ((struct internal_syment *)
2885 bfd_malloc (max_sym_count
2886 * sizeof (struct internal_syment)));
2887 finfo.sec_ptrs = (asection **) bfd_malloc (max_sym_count
2888 * sizeof (asection *));
2889 finfo.sym_indices = (long *) bfd_malloc (max_sym_count * sizeof (long));
2890 finfo.outsyms = ((bfd_byte *)
2891 bfd_malloc ((size_t) ((max_sym_count + 1) * symesz)));
2892 finfo.linenos = (bfd_byte *) bfd_malloc (max_lineno_count
2893 * bfd_coff_linesz (abfd));
2894 finfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
2895 finfo.external_relocs = (bfd_byte *) bfd_malloc (max_reloc_count * relsz);
2896 if (! info->relocateable)
2897 finfo.internal_relocs = ((struct internal_reloc *)
2898 bfd_malloc (max_reloc_count
2899 * sizeof (struct internal_reloc)));
2900 if ((finfo.internal_syms == NULL && max_sym_count > 0)
2901 || (finfo.sec_ptrs == NULL && max_sym_count > 0)
2902 || (finfo.sym_indices == NULL && max_sym_count > 0)
2903 || finfo.outsyms == NULL
2904 || (finfo.linenos == NULL && max_lineno_count > 0)
2905 || (finfo.contents == NULL && max_contents_size > 0)
2906 || (finfo.external_relocs == NULL && max_reloc_count > 0)
2907 || (! info->relocateable
2908 && finfo.internal_relocs == NULL
2909 && max_reloc_count > 0))
2912 /* We now know the position of everything in the file, except that
2913 we don't know the size of the symbol table and therefore we don't
2914 know where the string table starts. We just build the string
2915 table in memory as we go along. We process all the relocations
2916 for a single input file at once. */
2917 obj_raw_syment_count (abfd) = 0;
2919 if (coff_backend_info (abfd)->_bfd_coff_start_final_link)
2921 if (! bfd_coff_start_final_link (abfd, info))
2925 for (o = abfd->sections; o != NULL; o = o->next)
2927 for (p = o->link_order_head; p != NULL; p = p->next)
2929 if (p->type == bfd_indirect_link_order
2930 && (bfd_get_flavour (p->u.indirect.section->owner)
2931 == bfd_target_coff_flavour))
2933 sub = p->u.indirect.section->owner;
2934 #ifdef POWERPC_LE_PE
2935 if (! sub->output_has_begun && !ppc_do_last(sub))
2937 if (! sub->output_has_begun)
2940 if (! _bfd_coff_link_input_bfd (&finfo, sub))
2942 sub->output_has_begun = true;
2945 else if (p->type == bfd_section_reloc_link_order
2946 || p->type == bfd_symbol_reloc_link_order)
2948 if (! _bfd_coff_reloc_link_order (abfd, &finfo, o, p))
2953 if (! _bfd_default_link_order (abfd, info, o, p))
2959 #ifdef POWERPC_LE_PE
2961 extern bfd* ppc_get_last();
2962 bfd* last_one = ppc_get_last();
2965 if (! _bfd_coff_link_input_bfd (&finfo, last_one))
2968 last_one->output_has_begun = true;
2972 /* Free up the buffers used by _bfd_coff_link_input_bfd. */
2974 coff_debug_merge_hash_table_free (&finfo.debug_merge);
2975 debug_merge_allocated = false;
2977 if (finfo.internal_syms != NULL)
2979 free (finfo.internal_syms);
2980 finfo.internal_syms = NULL;
2982 if (finfo.sec_ptrs != NULL)
2984 free (finfo.sec_ptrs);
2985 finfo.sec_ptrs = NULL;
2987 if (finfo.sym_indices != NULL)
2989 free (finfo.sym_indices);
2990 finfo.sym_indices = NULL;
2992 if (finfo.linenos != NULL)
2994 free (finfo.linenos);
2995 finfo.linenos = NULL;
2997 if (finfo.contents != NULL)
2999 free (finfo.contents);
3000 finfo.contents = NULL;
3002 if (finfo.external_relocs != NULL)
3004 free (finfo.external_relocs);
3005 finfo.external_relocs = NULL;
3007 if (finfo.internal_relocs != NULL)
3009 free (finfo.internal_relocs);
3010 finfo.internal_relocs = NULL;
3013 /* The value of the last C_FILE symbol is supposed to be the symbol
3014 index of the first external symbol. Write it out again if
3016 if (finfo.last_file_index != -1
3017 && (unsigned int) finfo.last_file.n_value != obj_raw_syment_count (abfd))
3019 finfo.last_file.n_value = obj_raw_syment_count (abfd);
3020 bfd_coff_swap_sym_out (abfd, (PTR) &finfo.last_file,
3021 (PTR) finfo.outsyms);
3023 (obj_sym_filepos (abfd)
3024 + finfo.last_file_index * symesz),
3026 || bfd_write (finfo.outsyms, symesz, 1, abfd) != symesz)
3030 /* Write out the global symbols. */
3031 finfo.failed = false;
3032 coff_link_hash_traverse (coff_hash_table (info), _bfd_coff_write_global_sym,
3037 /* The outsyms buffer is used by _bfd_coff_write_global_sym. */
3038 if (finfo.outsyms != NULL)
3040 free (finfo.outsyms);
3041 finfo.outsyms = NULL;
3044 if (info->relocateable)
3046 /* Now that we have written out all the global symbols, we know
3047 the symbol indices to use for relocs against them, and we can
3048 finally write out the relocs. */
3049 external_relocs = ((bfd_byte *)
3050 bfd_malloc (max_output_reloc_count * relsz));
3051 if (external_relocs == NULL)
3054 for (o = abfd->sections; o != NULL; o = o->next)
3056 struct internal_reloc *irel;
3057 struct internal_reloc *irelend;
3058 struct coff_link_hash_entry **rel_hash;
3061 if (o->reloc_count == 0)
3064 irel = finfo.section_info[o->target_index].relocs;
3065 irelend = irel + o->reloc_count;
3066 rel_hash = finfo.section_info[o->target_index].rel_hashes;
3067 erel = external_relocs;
3068 for (; irel < irelend; irel++, rel_hash++, erel += relsz)
3070 if (*rel_hash != NULL)
3072 BFD_ASSERT ((*rel_hash)->indx >= 0);
3073 irel->r_symndx = (*rel_hash)->indx;
3075 bfd_coff_swap_reloc_out (abfd, (PTR) irel, (PTR) erel);
3078 if (bfd_seek (abfd, o->rel_filepos, SEEK_SET) != 0
3079 || bfd_write ((PTR) external_relocs, relsz, o->reloc_count,
3080 abfd) != relsz * o->reloc_count)
3084 free (external_relocs);
3085 external_relocs = NULL;
3088 /* Free up the section information. */
3089 if (finfo.section_info != NULL)
3093 for (i = 0; i < abfd->section_count; i++)
3095 if (finfo.section_info[i].relocs != NULL)
3096 free (finfo.section_info[i].relocs);
3097 if (finfo.section_info[i].rel_hashes != NULL)
3098 free (finfo.section_info[i].rel_hashes);
3100 free (finfo.section_info);
3101 finfo.section_info = NULL;
3104 /* Write out the string table. */
3105 if (obj_raw_syment_count (abfd) != 0)
3108 (obj_sym_filepos (abfd)
3109 + obj_raw_syment_count (abfd) * symesz),
3113 #if STRING_SIZE_SIZE == 4
3115 _bfd_stringtab_size (finfo.strtab) + STRING_SIZE_SIZE,
3116 (bfd_byte *) strbuf);
3118 #error Change bfd_h_put_32
3121 if (bfd_write (strbuf, 1, STRING_SIZE_SIZE, abfd) != STRING_SIZE_SIZE)
3124 if (! _bfd_stringtab_emit (abfd, finfo.strtab))
3128 _bfd_stringtab_free (finfo.strtab);
3130 /* Setting bfd_get_symcount to 0 will cause write_object_contents to
3131 not try to write out the symbols. */
3132 bfd_get_symcount (abfd) = 0;
3137 if (debug_merge_allocated)
3138 coff_debug_merge_hash_table_free (&finfo.debug_merge);
3139 if (finfo.strtab != NULL)
3140 _bfd_stringtab_free (finfo.strtab);
3141 if (finfo.section_info != NULL)
3145 for (i = 0; i < abfd->section_count; i++)
3147 if (finfo.section_info[i].relocs != NULL)
3148 free (finfo.section_info[i].relocs);
3149 if (finfo.section_info[i].rel_hashes != NULL)
3150 free (finfo.section_info[i].rel_hashes);
3152 free (finfo.section_info);
3154 if (finfo.internal_syms != NULL)
3155 free (finfo.internal_syms);
3156 if (finfo.sec_ptrs != NULL)
3157 free (finfo.sec_ptrs);
3158 if (finfo.sym_indices != NULL)
3159 free (finfo.sym_indices);
3160 if (finfo.outsyms != NULL)
3161 free (finfo.outsyms);
3162 if (finfo.linenos != NULL)
3163 free (finfo.linenos);
3164 if (finfo.contents != NULL)
3165 free (finfo.contents);
3166 if (finfo.external_relocs != NULL)
3167 free (finfo.external_relocs);
3168 if (finfo.internal_relocs != NULL)
3169 free (finfo.internal_relocs);
3170 if (external_relocs != NULL)
3171 free (external_relocs);
3177 /* The transfer vectors that lead the outside world to all of the above. */
3179 #ifdef TARGET_LITTLE_SYM
3183 TARGET_LITTLE_NAME, /* name or coff-arm-little */
3184 bfd_target_coff_flavour,
3185 BFD_ENDIAN_LITTLE, /* data byte order is little */
3186 BFD_ENDIAN_LITTLE, /* header byte order is little */
3188 (HAS_RELOC | EXEC_P | /* FIXME: object flags */
3189 HAS_LINENO | HAS_DEBUG |
3190 HAS_SYMS | HAS_LOCALS | WP_TEXT),
3192 #ifndef COFF_WITH_PE
3193 (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* section flags */
3195 (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC /* section flags */
3196 | SEC_LINK_ONCE | SEC_LINK_DUPLICATES),
3199 0, /* leading char */
3200 '/', /* ar_pad_char */
3201 15, /* ar_max_namelen??? FIXMEmgo */
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, /* data */
3207 bfd_getl64, bfd_getl_signed_64, bfd_putl64,
3208 bfd_getl32, bfd_getl_signed_32, bfd_putl32,
3209 bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* hdrs */
3211 {_bfd_dummy_target, coff_object_p, /* bfd_check_format */
3212 bfd_generic_archive_p, /* _bfd_dummy_target */ coff_object_p },
3213 {bfd_false, coff_mkobject, _bfd_generic_mkarchive, /* bfd_set_format */
3215 {bfd_false, coff_write_object_contents, /* bfd_write_contents */
3216 _bfd_write_archive_contents, bfd_false},
3218 BFD_JUMP_TABLE_GENERIC (coff),
3219 BFD_JUMP_TABLE_COPY (coff),
3220 BFD_JUMP_TABLE_CORE (_bfd_nocore),
3221 BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_coff),
3222 BFD_JUMP_TABLE_SYMBOLS (coff),
3223 BFD_JUMP_TABLE_RELOCS (coff),
3224 BFD_JUMP_TABLE_WRITE (coff),
3225 BFD_JUMP_TABLE_LINK (coff),
3226 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
3232 #ifdef TARGET_BIG_SYM
3237 bfd_target_coff_flavour,
3238 BFD_ENDIAN_BIG, /* data byte order is big */
3239 BFD_ENDIAN_BIG, /* header byte order is big */
3241 (HAS_RELOC | EXEC_P | /* FIXME: object flags */
3242 HAS_LINENO | HAS_DEBUG |
3243 HAS_SYMS | HAS_LOCALS | WP_TEXT),
3245 #ifndef COFF_WITH_PE
3246 (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* section flags */
3248 (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC /* section flags */
3249 | SEC_LINK_ONCE | SEC_LINK_DUPLICATES),
3252 0, /* leading char */
3253 '/', /* ar_pad_char */
3254 15, /* ar_max_namelen??? FIXMEmgo */
3256 bfd_getb64, bfd_getb_signed_64, bfd_putb64,
3257 bfd_getb32, bfd_getb_signed_32, bfd_putb32,
3258 bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* data */
3260 bfd_getb64, bfd_getb_signed_64, bfd_putb64,
3261 bfd_getb32, bfd_getb_signed_32, bfd_putb32,
3262 bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* hdrs */
3264 {_bfd_dummy_target, coff_object_p, /* bfd_check_format */
3265 bfd_generic_archive_p, /* _bfd_dummy_target */ coff_object_p },
3266 {bfd_false, coff_mkobject, _bfd_generic_mkarchive, /* bfd_set_format */
3268 {bfd_false, coff_write_object_contents, /* bfd_write_contents */
3269 _bfd_write_archive_contents, bfd_false},
3271 BFD_JUMP_TABLE_GENERIC (coff),
3272 BFD_JUMP_TABLE_COPY (coff),
3273 BFD_JUMP_TABLE_CORE (_bfd_nocore),
3274 BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_coff),
3275 BFD_JUMP_TABLE_SYMBOLS (coff),
3276 BFD_JUMP_TABLE_RELOCS (coff),
3277 BFD_JUMP_TABLE_WRITE (coff),
3278 BFD_JUMP_TABLE_LINK (coff),
3279 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),