1 /* BFD back-end for PowerPC Microsoft Portable Executable files.
2 Copyright 1990, 91, 92, 93, 94, 95, 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 = (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]);
1096 "ppc_mark_symbol_as_glue:\n");
1098 CHECK_EYE(h->eye_catcher);
1100 h->symbol_is_glue = 1;
1101 h->glue_insn = bfd_get_32 (abfd, (bfd_byte *) &rel->r_vaddr);
1106 #endif /* COFF_IMAGE_WITH_PE */
1110 /* Provided the symbol, returns the value reffed */
1111 static long get_symbol_value PARAMS ((asymbol *));
1114 get_symbol_value (symbol)
1117 long relocation = 0;
1119 if (bfd_is_com_section (symbol->section))
1125 relocation = symbol->value +
1126 symbol->section->output_section->vma +
1127 symbol->section->output_offset;
1135 /* Return true if this relocation should
1136 appear in the output .reloc section. */
1138 static boolean in_reloc_p(abfd, howto)
1140 reloc_howto_type *howto;
1143 (! howto->pc_relative)
1144 && (howto->type != IMAGE_REL_PPC_ADDR32NB)
1145 && (howto->type != IMAGE_REL_PPC_TOCREL16)
1146 && (howto->type != IMAGE_REL_PPC_IMGLUE)
1147 && (howto->type != IMAGE_REL_PPC_IFGLUE)
1148 && (howto->type != IMAGE_REL_PPC_SECREL)
1149 && (howto->type != IMAGE_REL_PPC_SECTION)
1150 && (howto->type != IMAGE_REL_PPC_SECREL16)
1151 && (howto->type != IMAGE_REL_PPC_REFHI)
1152 && (howto->type != IMAGE_REL_PPC_REFLO)
1153 && (howto->type != IMAGE_REL_PPC_PAIR)
1154 && (howto->type != IMAGE_REL_PPC_TOCREL16_DEFN) ;
1159 /* this function is in charge of performing all the ppc PE relocations */
1160 /* Don't yet know if we want to do this this particular way ... (krk) */
1161 /* FIXME: (it is not yet enabled) */
1163 static bfd_reloc_status_type
1164 pe_ppc_reloc (abfd, reloc_entry, symbol_in, data, input_section, output_bfd,
1167 arelent *reloc_entry;
1170 asection *input_section;
1172 char **error_message;
1174 /* the consth relocation comes in two parts, we have to remember
1175 the state between calls, in these variables */
1176 static boolean part1_consth_active = false;
1177 static unsigned long part1_consth_value;
1179 unsigned long sym_value;
1180 unsigned short r_type;
1181 unsigned long addr = reloc_entry->address ; /*+ input_section->vma*/
1183 fprintf(stderr, "pe_ppc_reloc (%s)\n", TARGET_LITTLE_NAME);
1185 r_type = reloc_entry->howto->type;
1189 /* Partial linking - do nothing */
1190 reloc_entry->address += input_section->output_offset;
1191 return bfd_reloc_ok;
1194 if (symbol_in != NULL
1195 && bfd_is_und_section (symbol_in->section))
1197 /* Keep the state machine happy in case we're called again */
1198 if (r_type == IMAGE_REL_PPC_REFHI)
1200 part1_consth_active = true;
1201 part1_consth_value = 0;
1203 return(bfd_reloc_undefined);
1206 if ((part1_consth_active) && (r_type != IMAGE_REL_PPC_PAIR))
1208 part1_consth_active = false;
1209 *error_message = (char *) "Missing PAIR";
1210 return(bfd_reloc_dangerous);
1214 sym_value = get_symbol_value(symbol_in);
1216 return(bfd_reloc_ok);
1221 /* The reloc processing routine for the optimized COFF linker. */
1224 coff_ppc_relocate_section (output_bfd, info, input_bfd, input_section,
1225 contents, relocs, syms, sections)
1227 struct bfd_link_info *info;
1229 asection *input_section;
1231 struct internal_reloc *relocs;
1232 struct internal_syment *syms;
1233 asection **sections;
1235 struct internal_reloc *rel;
1236 struct internal_reloc *relend;
1239 asection *toc_section = 0;
1241 reloc_howto_type *howto = 0;
1245 "pe_ppc_relocate_section (%s) for %s in bfd %s\n",
1247 input_section->name,
1248 input_bfd->filename);
1252 /* If we are performing a relocateable link, we don't need to do a
1253 thing. The caller will take care of adjusting the reloc
1254 addresses and symbol indices. */
1255 if (info->relocateable)
1262 relend = rel + input_section->reloc_count;
1263 for (; rel < relend; rel++)
1266 struct ppc_coff_link_hash_entry *h;
1267 struct internal_syment *sym;
1271 bfd_reloc_status_type rstat;
1274 unsigned short r_type = EXTRACT_TYPE (rel->r_type);
1275 unsigned short r_flags = EXTRACT_FLAGS(rel->r_type);
1278 /* now examine flags */
1281 fprintf (stderr, "Reloc with flags found!");
1282 if ( r_flags & IMAGE_REL_PPC_NEG )
1283 fprintf (stderr, " NEG");
1284 if ( r_flags & IMAGE_REL_PPC_BRTAKEN )
1285 fprintf (stderr, " BRTAKEN");
1286 if ( r_flags & IMAGE_REL_PPC_BRNTAKEN )
1287 fprintf (stderr, " BRNTAKEN");
1288 if ( r_flags & IMAGE_REL_PPC_TOCDEFN )
1289 fprintf (stderr, " TOCDEFN");
1290 fprintf(stderr, "\n");
1294 symndx = rel->r_symndx;
1295 loc = contents + rel->r_vaddr - input_section->vma;
1297 /* FIXME: check bounds on r_type */
1298 howto = ppc_coff_howto_table + r_type;
1307 h = (struct ppc_coff_link_hash_entry *)
1308 (obj_coff_sym_hashes (input_bfd)[symndx]);
1311 CHECK_EYE(h->eye_catcher);
1314 sym = syms + symndx;
1317 if (r_type == IMAGE_REL_PPC_IMGLUE && h == 0)
1320 "relocate_section: IMGLUE reloc has no name!\n");
1327 /* FIXME: PAIR unsupported in the following code */
1331 sec = bfd_abs_section_ptr;
1334 sec = sections[symndx];
1335 val = (sec->output_section->vma
1336 + sec->output_offset
1343 CHECK_EYE(h->eye_catcher);
1345 if (h->root.root.type == bfd_link_hash_defined
1346 || h->root.root.type == bfd_link_hash_defweak)
1348 sec = h->root.root.u.def.section;
1349 val = (h->root.root.u.def.value
1350 + sec->output_section->vma
1351 + sec->output_offset);
1355 if (! ((*info->callbacks->undefined_symbol)
1356 (info, h->root.root.root.string, input_bfd, input_section,
1357 rel->r_vaddr - input_section->vma)))
1362 rstat = bfd_reloc_ok;
1364 /* Each case must do its own relocation, setting rstat appropriately */
1369 "ERROR: during reloc processing -- unsupported reloc %s\n",
1371 bfd_set_error (bfd_error_bad_value);
1374 case IMAGE_REL_PPC_TOCREL16:
1376 bfd_vma our_toc_offset;
1379 DUMP_RELOC2(howto->name, rel);
1381 if (toc_section == 0)
1383 toc_section = bfd_get_section_by_name (bfd_of_toc_owner,
1388 "BFD of toc owner %p (%s), section addr of %s %p\n",
1389 bfd_of_toc_owner, bfd_of_toc_owner->filename,
1390 TOC_SECTION_NAME, toc_section);
1393 if ( toc_section == NULL )
1395 fprintf(stderr, "No Toc section!\n");
1401 * Amazing bit tricks present. As we may have seen earlier, we
1402 * use the 1 bit to tell us whether or not a toc offset has been
1403 * allocated. Now that they've all been allocated, we will use
1404 * the 1 bit to tell us if we've written this particular toc
1409 { /* it is a file local symbol */
1410 int *local_toc_table;
1413 sym = syms + symndx;
1414 name = sym->_n._n_name;
1416 local_toc_table = obj_coff_local_toc_table(input_bfd);
1417 our_toc_offset = local_toc_table[symndx];
1419 if (IS_WRITTEN(our_toc_offset))
1421 /* if it has been written out, it is marked with the
1422 1 bit. Fix up our offset, but do not write it out
1425 MAKE_ADDR_AGAIN(our_toc_offset);
1429 "Not writing out toc_offset of %d for %s\n",
1430 our_toc_offset, name);
1435 /* write out the toc entry */
1436 record_toc(toc_section, our_toc_offset, priv, strdup(name));
1439 "Writing out toc_offset "
1440 "toc_section (%p,%p)+%d val %d for %s\n",
1442 toc_section->contents,
1448 bfd_put_32(output_bfd,
1450 toc_section->contents + our_toc_offset);
1452 MARK_AS_WRITTEN(local_toc_table[symndx]);
1458 const char *name = h->root.root.root.string;
1459 our_toc_offset = h->toc_offset;
1461 if ((r_flags & IMAGE_REL_PPC_TOCDEFN)
1462 == IMAGE_REL_PPC_TOCDEFN )
1464 /* This is unbelievable cheese. Some knowledgable asm
1465 hacker has decided to use r2 as a base for loading
1466 a value. He/She does this by setting the tocdefn bit,
1467 and not supplying a toc definition. The behaviour is
1468 then to use the difference between the value of the
1469 symbol and the actual location of the toc as the toc
1472 In fact, what is usually happening is, because the
1473 Import Address Table is mapped immediately following
1474 the toc, some trippy library code trying for speed on
1475 dll linkage, takes advantage of that and considers
1476 the IAT to be part of the toc, thus saving a load.
1480 "TOCDEFN is on, (%s) (%p) our_toc_offset = %x, val (%x) vma (%x) off (%x)\n",
1481 name, h, our_toc_offset,
1482 val, toc_section->output_section->vma,
1483 toc_section->output_offset);
1486 our_toc_offset = val -
1487 (toc_section->output_section->vma +
1488 toc_section->output_offset);
1492 " our_toc_offset set to %x\n", our_toc_offset);
1495 /* The size must still fit in a 16bit displacment */
1496 if (our_toc_offset >= 65535)
1499 "Error: TOCDEFN Relocation of %d for %s exceeded displacement of 65535\n", our_toc_offset, name);
1503 record_toc(toc_section, our_toc_offset, pub, strdup(name));
1505 else if (IS_WRITTEN(our_toc_offset))
1507 /* if it has been written out, it is marked with the
1508 1 bit. Fix up our offset, but do not write it out
1511 MAKE_ADDR_AGAIN(our_toc_offset);
1514 "Not writing out toc_offset of %d for %s\n",
1515 our_toc_offset, name);
1520 record_toc(toc_section, our_toc_offset, pub, strdup(name));
1523 /* write out the toc entry */
1525 "Writing out toc_offset "
1526 "toc_section (%p,%p)+%d val %d for %s\n",
1528 toc_section->contents,
1534 /* write out the toc entry */
1535 bfd_put_32(output_bfd,
1537 toc_section->contents + our_toc_offset);
1539 MARK_AS_WRITTEN(h->toc_offset);
1540 /* The tricky part is that this is the address that */
1541 /* needs a .reloc entry for it */
1546 if (fixit && info->base_file)
1548 /* So if this is non pcrelative, and is referenced
1549 to a section or a common symbol, then it needs a reloc */
1551 /* relocation to a symbol in a section which
1552 isn't absolute - we output the address here
1555 bfd_vma addr = toc_section->output_section->vma
1556 + toc_section->output_offset + our_toc_offset;
1558 if (coff_data(output_bfd)->pe)
1559 addr -= pe_data(output_bfd)->pe_opthdr.ImageBase;
1563 " Toc Section .reloc candidate addr = %x\n", addr);
1565 fwrite (&addr, 1,4, (FILE *) info->base_file);
1569 /* FIXME: this test is conservative */
1570 if ( (r_flags & IMAGE_REL_PPC_TOCDEFN) != IMAGE_REL_PPC_TOCDEFN &&
1571 our_toc_offset > toc_section->_raw_size)
1574 "reloc offset is bigger than the toc size!\n");
1578 /* Now we know the relocation for this toc reference */
1579 relocation = our_toc_offset + TOC_LOAD_ADJUSTMENT;
1580 rstat = _bfd_relocate_contents (howto,
1586 case IMAGE_REL_PPC_IFGLUE:
1588 /* To solve this, we need to know whether or not the symbol */
1589 /* appearing on the call instruction is a glue function or not. */
1590 /* A glue function must announce itself via a IMGLUE reloc, and */
1591 /* the reloc contains the required toc restore instruction */
1594 const char *my_name;
1595 DUMP_RELOC2(howto->name, rel);
1599 my_name = h->root.root.root.string;
1600 if (h->symbol_is_glue == 1)
1602 x = bfd_get_32(input_bfd, loc);
1603 bfd_put_32(input_bfd, h->glue_insn, loc);
1608 case IMAGE_REL_PPC_SECREL:
1609 /* Unimplemented: codeview debugging information */
1610 /* For fast access to the header of the section
1611 containing the item. */
1613 case IMAGE_REL_PPC_SECTION:
1614 /* Unimplemented: codeview debugging information */
1615 /* Is used to indicate that the value should be relative
1616 to the beginning of the section that contains the
1619 case IMAGE_REL_PPC_ABSOLUTE:
1621 const char *my_name;
1623 my_name = (syms+symndx)->_n._n_name;
1626 my_name = h->root.root.root.string;
1630 "Warning: unsupported reloc %s <file %s, section %s>\n",
1632 bfd_get_filename(input_bfd),
1633 input_section->name);
1635 fprintf(stderr,"sym %ld (%s), r_vaddr %ld (%lx)\n",
1636 rel->r_symndx, my_name, (long) rel->r_vaddr,
1637 (unsigned long) rel->r_vaddr);
1640 case IMAGE_REL_PPC_IMGLUE:
1642 /* There is nothing to do now. This reloc was noted in the first
1643 pass over the relocs, and the glue instruction extracted */
1644 const char *my_name;
1645 if (h->symbol_is_glue == 1)
1647 my_name = h->root.root.root.string;
1649 "Warning: previously missed IMGLUE reloc %s <file %s, section %s>\n",
1651 bfd_get_filename(input_bfd),
1652 input_section->name);
1658 case IMAGE_REL_PPC_ADDR32NB:
1660 struct coff_link_hash_entry *myh = 0;
1661 const char *name = 0;
1662 DUMP_RELOC2(howto->name, rel);
1664 if (strncmp(".idata$2",input_section->name,8) == 0 && first_thunk_address == 0)
1666 /* set magic values */
1668 struct coff_link_hash_entry *myh = 0;
1669 myh = coff_link_hash_lookup (coff_hash_table (info),
1671 false, false, true);
1672 first_thunk_address = myh->root.u.def.value +
1673 sec->output_section->vma +
1674 sec->output_offset -
1675 pe_data(output_bfd)->pe_opthdr.ImageBase;
1677 idata5offset = myh->root.u.def.value;
1678 myh = coff_link_hash_lookup (coff_hash_table (info),
1680 false, false, true);
1682 thunk_size = myh->root.u.def.value - idata5offset;
1683 myh = coff_link_hash_lookup (coff_hash_table (info),
1685 false, false, true);
1686 import_table_size = myh->root.u.def.value;
1689 "first computation triggered fta %x, ts %d(%x), its %d(%x)\n",
1690 first_thunk_address, thunk_size, thunk_size, import_table_size,
1696 { /* it is a file local symbol */
1697 sym = syms + symndx;
1698 name = sym->_n._n_name;
1704 name = h->root.root.root.string;
1705 if (strcmp(".idata$2", name) == 0)
1706 target = "__idata2_magic__";
1707 else if (strcmp(".idata$4", name) == 0)
1708 target = "__idata4_magic__";
1709 else if (strcmp(".idata$5", name) == 0)
1710 target = "__idata5_magic__";
1716 myh = coff_link_hash_lookup (coff_hash_table (info),
1718 false, false, true);
1721 fprintf(stderr, "Missing idata magic cookies, this cannot work anyway...\n");
1725 val = myh->root.u.def.value +
1726 sec->output_section->vma + sec->output_offset;
1727 if (first_thunk_address == 0)
1730 myh = coff_link_hash_lookup (coff_hash_table (info),
1732 false, false, true);
1733 first_thunk_address = myh->root.u.def.value +
1734 sec->output_section->vma +
1735 sec->output_offset -
1736 pe_data(output_bfd)->pe_opthdr.ImageBase;
1738 idata5offset = myh->root.u.def.value;
1739 myh = coff_link_hash_lookup (coff_hash_table (info),
1741 false, false, true);
1743 thunk_size = myh->root.u.def.value - idata5offset;
1744 myh = coff_link_hash_lookup (coff_hash_table (info),
1746 false, false, true);
1747 import_table_size = myh->root.u.def.value;
1751 "second computation triggered fta %x, ts %d(%x), its %d(%x)\n",
1752 first_thunk_address, thunk_size, thunk_size, import_table_size,
1759 rstat = _bfd_relocate_contents (howto,
1762 pe_data(output_bfd)->pe_opthdr.ImageBase,
1767 case IMAGE_REL_PPC_REL24:
1768 DUMP_RELOC2(howto->name, rel);
1769 val -= (input_section->output_section->vma
1770 + input_section->output_offset);
1772 rstat = _bfd_relocate_contents (howto,
1777 case IMAGE_REL_PPC_ADDR16:
1778 case IMAGE_REL_PPC_ADDR24:
1779 case IMAGE_REL_PPC_ADDR32:
1780 DUMP_RELOC2(howto->name, rel);
1781 rstat = _bfd_relocate_contents (howto,
1788 if ( info->base_file )
1790 /* So if this is non pcrelative, and is referenced
1791 to a section or a common symbol, then it needs a reloc */
1792 if (sym && pe_data(output_bfd)->in_reloc_p(output_bfd, howto))
1794 /* relocation to a symbol in a section which
1795 isn't absolute - we output the address here
1797 bfd_vma addr = rel->r_vaddr
1798 - input_section->vma
1799 + input_section->output_offset
1800 + input_section->output_section->vma;
1802 if (coff_data(output_bfd)->pe)
1805 bfd_vma before_addr = addr;
1807 addr -= pe_data(output_bfd)->pe_opthdr.ImageBase;
1810 " adjusted down from %x to %x", before_addr, addr);
1814 fprintf(stderr, "\n");
1817 fwrite (&addr, 1,4, (FILE *) info->base_file);
1827 case bfd_reloc_overflow:
1830 char buf[SYMNMLEN + 1];
1835 name = h->root.root.root.string;
1836 else if (sym == NULL)
1838 else if (sym->_n._n_n._n_zeroes == 0
1839 && sym->_n._n_n._n_offset != 0)
1840 name = obj_coff_strings (input_bfd) + sym->_n._n_n._n_offset;
1843 strncpy (buf, sym->_n._n_name, SYMNMLEN);
1844 buf[SYMNMLEN] = '\0';
1850 name = _bfd_coff_internal_syment_name (input_bfd, sym, buf);
1856 if (! ((*info->callbacks->reloc_overflow)
1857 (info, name, howto->name,
1858 (bfd_vma) 0, input_bfd,
1859 input_section, rel->r_vaddr - input_section->vma)))
1863 "pe_ppc_relocate_section (%s) for %s in bfd %s RETURNING TRUE\n",
1865 input_section->name,
1866 input_bfd->filename);
1878 "pe_ppc_relocate_section (%s) for %s in bfd %s RETURNING TRUE\n",
1880 input_section->name,
1881 input_bfd->filename);
1889 #ifdef COFF_IMAGE_WITH_PE
1891 long int global_toc_size = 4;
1893 bfd* bfd_of_toc_owner = 0;
1895 long int import_table_size;
1896 long int first_thunk_address;
1897 long int thunk_size;
1899 struct list_ele *head;
1900 struct list_ele *tail;
1903 h1 = "\n\t\t\tTOC MAPPING\n\n";
1905 h2 = " TOC disassembly Comments Name\n";
1907 h3 = " Offset spelling (if present)\n";
1920 for(t = head; t != 0; t=t->next)
1926 else if (t->cat == pub)
1928 else if (t->cat == data)
1929 cat = "data-in-toc ";
1931 if (t->offset > global_toc_size)
1933 if (t->offset <= global_toc_size + thunk_size)
1934 cat = "IAT reference ";
1938 "**** global_toc_size %ld(%lx), thunk_size %ld(%lx)\n",
1939 global_toc_size, global_toc_size, thunk_size, thunk_size);
1940 cat = "Out of bounds!";
1945 " %04lx (%d)", (unsigned long) t->offset, t->offset - 32768);
1952 fprintf(file, "\n");
1956 ppc_allocate_toc_section (info)
1957 struct bfd_link_info *info;
1961 static char test_char = '1';
1963 if ( global_toc_size == 0 ) /* FIXME: does this get me in trouble? */
1966 if (bfd_of_toc_owner == 0)
1969 "There is no bfd that owns the toc section!\n");
1973 s = bfd_get_section_by_name ( bfd_of_toc_owner , TOC_SECTION_NAME);
1976 fprintf(stderr, "No Toc section!\n");
1980 foo = (bfd_byte *) bfd_alloc(bfd_of_toc_owner, global_toc_size);
1981 memset(foo, test_char, global_toc_size);
1983 s->_raw_size = s->_cooked_size = global_toc_size;
1990 ppc_process_before_allocation (abfd, info)
1992 struct bfd_link_info *info;
1995 struct internal_reloc *i, *rel;
1999 "ppc_process_before_allocation: BFD %s\n",
2000 bfd_get_filename(abfd));
2003 /* here we have a bfd that is to be included on the link. We have a hook
2004 to do reloc rummaging, before section sizes are nailed down. */
2006 _bfd_coff_get_external_symbols(abfd);
2008 /* rummage around all the relocs and map the toc */
2009 sec = abfd->sections;
2016 for (; sec != 0; sec = sec->next)
2022 " section %s reloc count %d\n",
2027 if (sec->reloc_count == 0)
2030 /* load the relocs */
2031 /* FIXME: there may be a storage leak here */
2032 i=_bfd_coff_read_internal_relocs(abfd,sec,1,0,0,0);
2037 for (rel=i;rel<i+sec->reloc_count;++rel)
2039 unsigned short r_type = EXTRACT_TYPE (rel->r_type);
2040 unsigned short r_flags = EXTRACT_FLAGS(rel->r_type);
2043 /* now examine flags */
2046 fprintf (stderr, "Reloc with flags found!");
2047 if ( r_flags & IMAGE_REL_PPC_NEG )
2048 fprintf (stderr, " NEG");
2049 if ( r_flags & IMAGE_REL_PPC_BRTAKEN )
2050 fprintf (stderr, " BRTAKEN");
2051 if ( r_flags & IMAGE_REL_PPC_BRNTAKEN )
2052 fprintf (stderr, " BRNTAKEN");
2053 if ( r_flags & IMAGE_REL_PPC_TOCDEFN )
2054 fprintf (stderr, " TOCDEFN");
2055 fprintf(stderr, "\n");
2059 DUMP_RELOC2(ppc_coff_howto_table[r_type].name, rel);
2063 case IMAGE_REL_PPC_TOCREL16:
2066 This remains unimplemented for now, as it currently adds
2067 un-necessary elements to the toc. All we need to do today
2068 is not do anything if TOCDEFN is on.
2070 if ( r_flags & IMAGE_REL_PPC_TOCDEFN )
2071 toc_offset = ppc_record_data_in_toc_entry(abfd, info, sec,
2075 toc_offset = ppc_record_toc_entry(abfd, info, sec,
2076 rel->r_symndx, default_toc);
2078 if ( (r_flags & IMAGE_REL_PPC_TOCDEFN) != IMAGE_REL_PPC_TOCDEFN )
2079 toc_offset = ppc_record_toc_entry(abfd, info, sec,
2080 rel->r_symndx, default_toc);
2082 case IMAGE_REL_PPC_IMGLUE:
2083 ppc_mark_symbol_as_glue(abfd, rel->r_symndx, rel);
2097 static bfd_reloc_status_type
2098 ppc_refhi_reloc (abfd,
2106 arelent *reloc_entry;
2109 asection *input_section;
2111 char **error_message;
2114 DUMP_RELOC("REFHI",reloc_entry);
2116 if (output_bfd == (bfd *) NULL)
2117 return bfd_reloc_continue;
2119 return bfd_reloc_undefined;
2124 static bfd_reloc_status_type
2125 ppc_reflo_reloc (abfd,
2133 arelent *reloc_entry;
2136 asection *input_section;
2138 char **error_message;
2141 DUMP_RELOC("REFLO",reloc_entry);
2143 if (output_bfd == (bfd *) NULL)
2144 return bfd_reloc_continue;
2146 return bfd_reloc_undefined;
2151 static bfd_reloc_status_type
2152 ppc_pair_reloc (abfd,
2160 arelent *reloc_entry;
2163 asection *input_section;
2165 char **error_message;
2168 DUMP_RELOC("PAIR",reloc_entry);
2170 if (output_bfd == (bfd *) NULL)
2171 return bfd_reloc_continue;
2173 return bfd_reloc_undefined;
2177 static bfd_reloc_status_type
2178 ppc_toc16_reloc (abfd,
2186 arelent *reloc_entry;
2189 asection *input_section;
2191 char **error_message;
2193 UN_IMPL("TOCREL16");
2194 DUMP_RELOC("TOCREL16",reloc_entry);
2196 if (output_bfd == (bfd *) NULL)
2198 return bfd_reloc_continue;
2201 return bfd_reloc_ok;
2206 /* ADDR32NB : 32 bit address relative to the virtual origin. */
2207 /* (On the alpha, this is always a linker generated thunk)*/
2208 /* (i.e. 32bit addr relative to the image base) */
2212 static bfd_reloc_status_type
2213 ppc_addr32nb_reloc (abfd,
2221 arelent *reloc_entry;
2224 asection *input_section;
2226 char **error_message;
2228 UN_IMPL("ADDR32NB");
2229 DUMP_RELOC("ADDR32NB",reloc_entry);
2231 return bfd_reloc_ok;
2236 static bfd_reloc_status_type
2237 ppc_secrel_reloc (abfd,
2245 arelent *reloc_entry;
2248 asection *input_section;
2250 char **error_message;
2253 DUMP_RELOC("SECREL",reloc_entry);
2255 if (output_bfd == (bfd *) NULL)
2256 return bfd_reloc_continue;
2258 return bfd_reloc_ok;
2261 static bfd_reloc_status_type
2262 ppc_section_reloc (abfd,
2270 arelent *reloc_entry;
2273 asection *input_section;
2275 char **error_message;
2278 DUMP_RELOC("SECTION",reloc_entry);
2280 if (output_bfd == (bfd *) NULL)
2281 return bfd_reloc_continue;
2283 return bfd_reloc_ok;
2286 static bfd_reloc_status_type
2287 ppc_imglue_reloc (abfd,
2295 arelent *reloc_entry;
2298 asection *input_section;
2300 char **error_message;
2303 DUMP_RELOC("IMGLUE",reloc_entry);
2305 if (output_bfd == (bfd *) NULL)
2306 return bfd_reloc_continue;
2308 return bfd_reloc_ok;
2313 #define MAX_RELOC_INDEX \
2314 (sizeof(ppc_coff_howto_table) / sizeof(ppc_coff_howto_table[0]) - 1)
2317 /* FIXME: There is a possiblity that when we read in a reloc from a file,
2318 that there are some bits encoded in the upper portion of the
2319 type field. Not yet implemented.
2321 static void ppc_coff_rtype2howto PARAMS ((arelent *relent,
2322 struct internal_reloc *internal));
2325 ppc_coff_rtype2howto (relent, internal)
2327 struct internal_reloc *internal;
2330 /* We can encode one of three things in the type field, aside from the
2332 1. IMAGE_REL_PPC_NEG - indicates the value field is a subtraction
2333 value, rather than an addition value
2334 2. IMAGE_REL_PPC_BRTAKEN, IMAGE_REL_PPC_BRNTAKEN - indicates that
2335 the branch is expected to be taken or not.
2336 3. IMAGE_REL_PPC_TOCDEFN - toc slot definition in the file
2337 For now, we just strip this stuff to find the type, and ignore it other
2340 reloc_howto_type *howto;
2341 unsigned short r_type = EXTRACT_TYPE (internal->r_type);
2342 unsigned short r_flags = EXTRACT_FLAGS(internal->r_type);
2343 unsigned short junk = EXTRACT_JUNK (internal->r_type);
2345 /* the masking process only slices off the bottom byte for r_type. */
2346 if ( r_type > MAX_RELOC_INDEX )
2349 "ppc_coff_rtype2howto: reloc index %d out of range [%d, %ld]\n",
2350 internal->r_type, 0, (long) MAX_RELOC_INDEX);
2354 /* check for absolute crap */
2358 "ppc_coff_rtype2howto: reloc index %d contains junk %d\n",
2359 internal->r_type, junk);
2364 /* now examine flags */
2367 fprintf (stderr, "Reloc with flags found!");
2368 if ( r_flags & IMAGE_REL_PPC_NEG )
2369 fprintf (stderr, " NEG");
2370 if ( r_flags & IMAGE_REL_PPC_BRTAKEN )
2371 fprintf (stderr, " BRTAKEN");
2372 if ( r_flags & IMAGE_REL_PPC_BRNTAKEN )
2373 fprintf (stderr, " BRNTAKEN");
2374 if ( r_flags & IMAGE_REL_PPC_TOCDEFN )
2375 fprintf (stderr, " TOCDEFN");
2376 fprintf(stderr, "\n");
2382 case IMAGE_REL_PPC_ADDR16:
2383 case IMAGE_REL_PPC_REL24:
2384 case IMAGE_REL_PPC_ADDR24:
2385 case IMAGE_REL_PPC_ADDR32:
2386 case IMAGE_REL_PPC_IFGLUE:
2387 case IMAGE_REL_PPC_ADDR32NB:
2388 case IMAGE_REL_PPC_SECTION:
2389 case IMAGE_REL_PPC_SECREL:
2390 DUMP_RELOC2(ppc_coff_howto_table[r_type].name, internal);
2391 howto = ppc_coff_howto_table + r_type;
2393 case IMAGE_REL_PPC_IMGLUE:
2394 DUMP_RELOC2(ppc_coff_howto_table[r_type].name, internal);
2395 howto = ppc_coff_howto_table + r_type;
2397 case IMAGE_REL_PPC_TOCREL16:
2398 DUMP_RELOC2(ppc_coff_howto_table[r_type].name, internal);
2399 if (r_flags & IMAGE_REL_PPC_TOCDEFN)
2400 howto = ppc_coff_howto_table + IMAGE_REL_PPC_TOCREL16_DEFN;
2402 howto = ppc_coff_howto_table + IMAGE_REL_PPC_TOCREL16;
2406 "Warning: Unsupported reloc %s [%d] used -- it may not work.\n",
2407 ppc_coff_howto_table[r_type].name,
2409 howto = ppc_coff_howto_table + r_type;
2413 relent->howto = howto;
2417 static reloc_howto_type *
2418 coff_ppc_rtype_to_howto (abfd, sec, rel, h, sym, addendp)
2421 struct internal_reloc *rel;
2422 struct coff_link_hash_entry *h;
2423 struct internal_syment *sym;
2426 reloc_howto_type *howto;
2428 /* We can encode one of three things in the type field, aside from the
2430 1. IMAGE_REL_PPC_NEG - indicates the value field is a subtraction
2431 value, rather than an addition value
2432 2. IMAGE_REL_PPC_BRTAKEN, IMAGE_REL_PPC_BRNTAKEN - indicates that
2433 the branch is expected to be taken or not.
2434 3. IMAGE_REL_PPC_TOCDEFN - toc slot definition in the file
2435 For now, we just strip this stuff to find the type, and ignore it other
2439 unsigned short r_type = EXTRACT_TYPE (rel->r_type);
2440 unsigned short r_flags = EXTRACT_FLAGS(rel->r_type);
2441 unsigned short junk = EXTRACT_JUNK (rel->r_type);
2443 /* the masking process only slices off the bottom byte for r_type. */
2444 if ( r_type > MAX_RELOC_INDEX )
2447 "coff_ppc_rtype_to_howto: index %d out of range [%d, %ld]\n",
2448 r_type, 0, (long) MAX_RELOC_INDEX);
2452 /* check for absolute crap */
2456 "coff_ppc_rtype_to_howto: reloc index %d contains junk %d\n",
2462 /* now examine flags */
2465 fprintf (stderr, "Reloc with flags found!");
2466 if ( r_flags & IMAGE_REL_PPC_NEG )
2467 fprintf (stderr, " NEG");
2468 if ( r_flags & IMAGE_REL_PPC_BRTAKEN )
2469 fprintf (stderr, " BRTAKEN");
2470 if ( r_flags & IMAGE_REL_PPC_BRNTAKEN )
2471 fprintf (stderr, " BRNTAKEN");
2472 if ( r_flags & IMAGE_REL_PPC_TOCDEFN )
2473 fprintf (stderr, " TOCDEFN");
2474 fprintf(stderr, "\n");
2480 case IMAGE_REL_PPC_ADDR32NB:
2481 DUMP_RELOC2(ppc_coff_howto_table[r_type].name, rel);
2482 *addendp -= pe_data(sec->output_section->owner)->pe_opthdr.ImageBase;
2483 howto = ppc_coff_howto_table + r_type;
2485 case IMAGE_REL_PPC_TOCREL16:
2486 DUMP_RELOC2(ppc_coff_howto_table[r_type].name, rel);
2487 if (r_flags & IMAGE_REL_PPC_TOCDEFN)
2488 howto = ppc_coff_howto_table + IMAGE_REL_PPC_TOCREL16_DEFN;
2490 howto = ppc_coff_howto_table + IMAGE_REL_PPC_TOCREL16;
2492 case IMAGE_REL_PPC_ADDR16:
2493 case IMAGE_REL_PPC_REL24:
2494 case IMAGE_REL_PPC_ADDR24:
2495 case IMAGE_REL_PPC_ADDR32:
2496 case IMAGE_REL_PPC_IFGLUE:
2497 case IMAGE_REL_PPC_SECTION:
2498 case IMAGE_REL_PPC_SECREL:
2499 DUMP_RELOC2(ppc_coff_howto_table[r_type].name, rel);
2500 howto = ppc_coff_howto_table + r_type;
2502 case IMAGE_REL_PPC_IMGLUE:
2503 DUMP_RELOC2(ppc_coff_howto_table[r_type].name, rel);
2504 howto = ppc_coff_howto_table + r_type;
2508 "Warning: Unsupported reloc %s [%d] used -- it may not work.\n",
2509 ppc_coff_howto_table[r_type].name,
2511 howto = ppc_coff_howto_table + r_type;
2519 /* a cheesy little macro to make the code a little more readable */
2520 #define HOW2MAP(bfd_rtype,ppc_rtype) \
2521 case bfd_rtype: return &ppc_coff_howto_table[ppc_rtype]
2523 static reloc_howto_type *ppc_coff_reloc_type_lookup
2524 PARAMS ((bfd *, bfd_reloc_code_real_type));
2526 static reloc_howto_type *
2527 ppc_coff_reloc_type_lookup (abfd, code)
2529 bfd_reloc_code_real_type code;
2533 fprintf(stderr, "ppc_coff_reloc_type_lookup for %s\n",
2534 bfd_get_reloc_code_name(code));
2539 HOW2MAP(BFD_RELOC_32_GOTOFF, IMAGE_REL_PPC_IMGLUE);
2540 HOW2MAP(BFD_RELOC_16_GOT_PCREL, IMAGE_REL_PPC_IFGLUE);
2541 HOW2MAP(BFD_RELOC_16, IMAGE_REL_PPC_ADDR16);
2542 HOW2MAP(BFD_RELOC_PPC_B26, IMAGE_REL_PPC_REL24);
2543 HOW2MAP(BFD_RELOC_PPC_BA26, IMAGE_REL_PPC_ADDR24);
2544 HOW2MAP(BFD_RELOC_PPC_TOC16, IMAGE_REL_PPC_TOCREL16);
2545 HOW2MAP(BFD_RELOC_16_GOTOFF, IMAGE_REL_PPC_TOCREL16_DEFN);
2546 HOW2MAP(BFD_RELOC_32, IMAGE_REL_PPC_ADDR32);
2547 HOW2MAP(BFD_RELOC_RVA, IMAGE_REL_PPC_ADDR32NB);
2557 /* Tailor coffcode.h -- macro heaven. */
2559 #define RTYPE2HOWTO(cache_ptr, dst) ppc_coff_rtype2howto (cache_ptr, dst)
2561 #ifndef COFF_IMAGE_WITH_PE
2563 ppc_coff_swap_sym_in_hook ();
2566 /* We use the special COFF backend linker, with our own special touch. */
2568 #define coff_bfd_reloc_type_lookup ppc_coff_reloc_type_lookup
2569 #define coff_rtype_to_howto coff_ppc_rtype_to_howto
2570 #define coff_relocate_section coff_ppc_relocate_section
2571 #define coff_bfd_final_link ppc_bfd_coff_final_link
2573 #ifndef COFF_IMAGE_WITH_PE
2574 #define coff_swap_sym_in_hook ppc_coff_swap_sym_in_hook
2577 #define SELECT_RELOC(internal, howto) {internal.r_type=howto->type;}
2579 #define COFF_PAGE_SIZE 0x1000
2581 #define POWERPC_LE_PE
2583 #include "coffcode.h"
2587 #ifndef COFF_IMAGE_WITH_PE
2589 What we're trying to do here is allocate a toc section (early), and attach
2590 it to the last bfd to be processed. This avoids the problem of having a toc
2591 written out before all files have been processed. This code allocates
2592 a toc section for every file, and records the last one seen. There are
2593 at least two problems with this approach:
2594 1. We allocate whole bunches of toc sections that are ignored, but at
2595 at least we will not allocate a toc if no .toc is present.
2596 2. It's not clear to me that being the last bfd read necessarily means
2597 that you are the last bfd closed.
2598 3. Doing it on a "swap in" hook depends on when the "swap in" is called,
2599 and how often, etc. It's not clear to me that there isn't a hole here.
2603 ppc_coff_swap_sym_in_hook (abfd, ext1, in1)
2608 struct internal_syment *in = (struct internal_syment *)in1;
2610 if (bfd_of_toc_owner != 0) /* we already have a toc, so go home */
2613 if (strcmp(in->_n._n_name, ".toc") == 0)
2616 register asection *s;
2618 s = bfd_get_section_by_name ( abfd , TOC_SECTION_NAME);
2624 flags = SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY ;
2628 "ppc_coff_swap_sym_in_hook: about to create the %s section\n",
2632 s = bfd_make_section (abfd, TOC_SECTION_NAME);
2635 || !bfd_set_section_flags (abfd, s, flags)
2636 || !bfd_set_section_alignment (abfd, s, 2))
2639 "toc section allocation failed!\n");
2643 /* save the bfd for later allocation */
2644 bfd_of_toc_owner = abfd;
2652 ppc_bfd_coff_final_link ();
2654 #ifndef COFF_IMAGE_WITH_PE
2660 if (abfd == bfd_of_toc_owner)
2669 return bfd_of_toc_owner;
2672 /* this piece of machinery exists only to guarantee that the bfd that holds
2673 the toc section is written last.
2675 This does depend on bfd_make_section attaching a new section to the
2676 end of the section list for the bfd.
2678 This is otherwise intended to be functionally the same as
2679 cofflink.c:_bfd_coff_final_link(). It is specifically different only
2680 where the POWERPC_LE_PE macro modifies the code. It is left in as a
2681 precise form of comment. krk@cygnus.com
2683 #define POWERPC_LE_PE
2686 /* Do the final link step. */
2689 ppc_bfd_coff_final_link (abfd, info)
2691 struct bfd_link_info *info;
2693 bfd_size_type symesz;
2694 struct coff_final_link_info finfo;
2695 boolean debug_merge_allocated;
2697 struct bfd_link_order *p;
2698 size_t max_sym_count;
2699 size_t max_lineno_count;
2700 size_t max_reloc_count;
2701 size_t max_output_reloc_count;
2702 size_t max_contents_size;
2703 file_ptr rel_filepos;
2705 file_ptr line_filepos;
2706 unsigned int linesz;
2708 bfd_byte *external_relocs = NULL;
2709 char strbuf[STRING_SIZE_SIZE];
2711 symesz = bfd_coff_symesz (abfd);
2714 finfo.output_bfd = abfd;
2715 finfo.strtab = NULL;
2716 finfo.section_info = NULL;
2717 finfo.last_file_index = -1;
2718 finfo.last_bf_index = -1;
2719 finfo.internal_syms = NULL;
2720 finfo.sec_ptrs = NULL;
2721 finfo.sym_indices = NULL;
2722 finfo.outsyms = NULL;
2723 finfo.linenos = NULL;
2724 finfo.contents = NULL;
2725 finfo.external_relocs = NULL;
2726 finfo.internal_relocs = NULL;
2727 debug_merge_allocated = false;
2729 coff_data (abfd)->link_info = info;
2731 finfo.strtab = _bfd_stringtab_init ();
2732 if (finfo.strtab == NULL)
2735 if (! coff_debug_merge_hash_table_init (&finfo.debug_merge))
2737 debug_merge_allocated = true;
2739 /* Compute the file positions for all the sections. */
2740 if (! abfd->output_has_begun)
2741 bfd_coff_compute_section_file_positions (abfd);
2743 /* Count the line numbers and relocation entries required for the
2744 output file. Set the file positions for the relocs. */
2745 rel_filepos = obj_relocbase (abfd);
2746 relsz = bfd_coff_relsz (abfd);
2747 max_contents_size = 0;
2748 max_lineno_count = 0;
2749 max_reloc_count = 0;
2751 for (o = abfd->sections; o != NULL; o = o->next)
2754 o->lineno_count = 0;
2755 for (p = o->link_order_head; p != NULL; p = p->next)
2758 if (p->type == bfd_indirect_link_order)
2762 sec = p->u.indirect.section;
2764 /* Mark all sections which are to be included in the
2765 link. This will normally be every section. We need
2766 to do this so that we can identify any sections which
2767 the linker has decided to not include. */
2768 sec->linker_mark = true;
2770 if (info->strip == strip_none
2771 || info->strip == strip_some)
2772 o->lineno_count += sec->lineno_count;
2774 if (info->relocateable)
2775 o->reloc_count += sec->reloc_count;
2777 if (sec->_raw_size > max_contents_size)
2778 max_contents_size = sec->_raw_size;
2779 if (sec->lineno_count > max_lineno_count)
2780 max_lineno_count = sec->lineno_count;
2781 if (sec->reloc_count > max_reloc_count)
2782 max_reloc_count = sec->reloc_count;
2784 else if (info->relocateable
2785 && (p->type == bfd_section_reloc_link_order
2786 || p->type == bfd_symbol_reloc_link_order))
2789 if (o->reloc_count == 0)
2793 o->flags |= SEC_RELOC;
2794 o->rel_filepos = rel_filepos;
2795 rel_filepos += o->reloc_count * relsz;
2799 /* If doing a relocateable link, allocate space for the pointers we
2801 if (info->relocateable)
2805 /* We use section_count + 1, rather than section_count, because
2806 the target_index fields are 1 based. */
2807 finfo.section_info =
2808 ((struct coff_link_section_info *)
2809 bfd_malloc ((abfd->section_count + 1)
2810 * sizeof (struct coff_link_section_info)));
2811 if (finfo.section_info == NULL)
2813 for (i = 0; i <= abfd->section_count; i++)
2815 finfo.section_info[i].relocs = NULL;
2816 finfo.section_info[i].rel_hashes = NULL;
2820 /* We now know the size of the relocs, so we can determine the file
2821 positions of the line numbers. */
2822 line_filepos = rel_filepos;
2823 linesz = bfd_coff_linesz (abfd);
2824 max_output_reloc_count = 0;
2825 for (o = abfd->sections; o != NULL; o = o->next)
2827 if (o->lineno_count == 0)
2828 o->line_filepos = 0;
2831 o->line_filepos = line_filepos;
2832 line_filepos += o->lineno_count * linesz;
2835 if (o->reloc_count != 0)
2837 /* We don't know the indices of global symbols until we have
2838 written out all the local symbols. For each section in
2839 the output file, we keep an array of pointers to hash
2840 table entries. Each entry in the array corresponds to a
2841 reloc. When we find a reloc against a global symbol, we
2842 set the corresponding entry in this array so that we can
2843 fix up the symbol index after we have written out all the
2846 Because of this problem, we also keep the relocs in
2847 memory until the end of the link. This wastes memory,
2848 but only when doing a relocateable link, which is not the
2850 BFD_ASSERT (info->relocateable);
2851 finfo.section_info[o->target_index].relocs =
2852 ((struct internal_reloc *)
2853 bfd_malloc (o->reloc_count * sizeof (struct internal_reloc)));
2854 finfo.section_info[o->target_index].rel_hashes =
2855 ((struct coff_link_hash_entry **)
2856 bfd_malloc (o->reloc_count
2857 * sizeof (struct coff_link_hash_entry *)));
2858 if (finfo.section_info[o->target_index].relocs == NULL
2859 || finfo.section_info[o->target_index].rel_hashes == NULL)
2862 if (o->reloc_count > max_output_reloc_count)
2863 max_output_reloc_count = o->reloc_count;
2866 /* Reset the reloc and lineno counts, so that we can use them to
2867 count the number of entries we have output so far. */
2869 o->lineno_count = 0;
2872 obj_sym_filepos (abfd) = line_filepos;
2874 /* Figure out the largest number of symbols in an input BFD. Take
2875 the opportunity to clear the output_has_begun fields of all the
2878 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
2882 sub->output_has_begun = false;
2883 sz = obj_raw_syment_count (sub);
2884 if (sz > max_sym_count)
2888 /* Allocate some buffers used while linking. */
2889 finfo.internal_syms = ((struct internal_syment *)
2890 bfd_malloc (max_sym_count
2891 * sizeof (struct internal_syment)));
2892 finfo.sec_ptrs = (asection **) bfd_malloc (max_sym_count
2893 * sizeof (asection *));
2894 finfo.sym_indices = (long *) bfd_malloc (max_sym_count * sizeof (long));
2895 finfo.outsyms = ((bfd_byte *)
2896 bfd_malloc ((size_t) ((max_sym_count + 1) * symesz)));
2897 finfo.linenos = (bfd_byte *) bfd_malloc (max_lineno_count
2898 * bfd_coff_linesz (abfd));
2899 finfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
2900 finfo.external_relocs = (bfd_byte *) bfd_malloc (max_reloc_count * relsz);
2901 if (! info->relocateable)
2902 finfo.internal_relocs = ((struct internal_reloc *)
2903 bfd_malloc (max_reloc_count
2904 * sizeof (struct internal_reloc)));
2905 if ((finfo.internal_syms == NULL && max_sym_count > 0)
2906 || (finfo.sec_ptrs == NULL && max_sym_count > 0)
2907 || (finfo.sym_indices == NULL && max_sym_count > 0)
2908 || finfo.outsyms == NULL
2909 || (finfo.linenos == NULL && max_lineno_count > 0)
2910 || (finfo.contents == NULL && max_contents_size > 0)
2911 || (finfo.external_relocs == NULL && max_reloc_count > 0)
2912 || (! info->relocateable
2913 && finfo.internal_relocs == NULL
2914 && max_reloc_count > 0))
2917 /* We now know the position of everything in the file, except that
2918 we don't know the size of the symbol table and therefore we don't
2919 know where the string table starts. We just build the string
2920 table in memory as we go along. We process all the relocations
2921 for a single input file at once. */
2922 obj_raw_syment_count (abfd) = 0;
2924 if (coff_backend_info (abfd)->_bfd_coff_start_final_link)
2926 if (! bfd_coff_start_final_link (abfd, info))
2930 for (o = abfd->sections; o != NULL; o = o->next)
2932 for (p = o->link_order_head; p != NULL; p = p->next)
2934 if (p->type == bfd_indirect_link_order
2935 && (bfd_get_flavour (p->u.indirect.section->owner)
2936 == bfd_target_coff_flavour))
2938 sub = p->u.indirect.section->owner;
2939 #ifdef POWERPC_LE_PE
2940 if (! sub->output_has_begun && !ppc_do_last(sub))
2942 if (! sub->output_has_begun)
2945 if (! _bfd_coff_link_input_bfd (&finfo, sub))
2947 sub->output_has_begun = true;
2950 else if (p->type == bfd_section_reloc_link_order
2951 || p->type == bfd_symbol_reloc_link_order)
2953 if (! _bfd_coff_reloc_link_order (abfd, &finfo, o, p))
2958 if (! _bfd_default_link_order (abfd, info, o, p))
2964 #ifdef POWERPC_LE_PE
2966 extern bfd* ppc_get_last();
2967 bfd* last_one = ppc_get_last();
2970 if (! _bfd_coff_link_input_bfd (&finfo, last_one))
2973 last_one->output_has_begun = true;
2977 /* Free up the buffers used by _bfd_coff_link_input_bfd. */
2979 coff_debug_merge_hash_table_free (&finfo.debug_merge);
2980 debug_merge_allocated = false;
2982 if (finfo.internal_syms != NULL)
2984 free (finfo.internal_syms);
2985 finfo.internal_syms = NULL;
2987 if (finfo.sec_ptrs != NULL)
2989 free (finfo.sec_ptrs);
2990 finfo.sec_ptrs = NULL;
2992 if (finfo.sym_indices != NULL)
2994 free (finfo.sym_indices);
2995 finfo.sym_indices = NULL;
2997 if (finfo.linenos != NULL)
2999 free (finfo.linenos);
3000 finfo.linenos = NULL;
3002 if (finfo.contents != NULL)
3004 free (finfo.contents);
3005 finfo.contents = NULL;
3007 if (finfo.external_relocs != NULL)
3009 free (finfo.external_relocs);
3010 finfo.external_relocs = NULL;
3012 if (finfo.internal_relocs != NULL)
3014 free (finfo.internal_relocs);
3015 finfo.internal_relocs = NULL;
3018 /* The value of the last C_FILE symbol is supposed to be the symbol
3019 index of the first external symbol. Write it out again if
3021 if (finfo.last_file_index != -1
3022 && (unsigned int) finfo.last_file.n_value != obj_raw_syment_count (abfd))
3024 finfo.last_file.n_value = obj_raw_syment_count (abfd);
3025 bfd_coff_swap_sym_out (abfd, (PTR) &finfo.last_file,
3026 (PTR) finfo.outsyms);
3028 (obj_sym_filepos (abfd)
3029 + finfo.last_file_index * symesz),
3031 || bfd_write (finfo.outsyms, symesz, 1, abfd) != symesz)
3035 /* Write out the global symbols. */
3036 finfo.failed = false;
3037 coff_link_hash_traverse (coff_hash_table (info), _bfd_coff_write_global_sym,
3042 /* The outsyms buffer is used by _bfd_coff_write_global_sym. */
3043 if (finfo.outsyms != NULL)
3045 free (finfo.outsyms);
3046 finfo.outsyms = NULL;
3049 if (info->relocateable)
3051 /* Now that we have written out all the global symbols, we know
3052 the symbol indices to use for relocs against them, and we can
3053 finally write out the relocs. */
3054 external_relocs = ((bfd_byte *)
3055 bfd_malloc (max_output_reloc_count * relsz));
3056 if (external_relocs == NULL)
3059 for (o = abfd->sections; o != NULL; o = o->next)
3061 struct internal_reloc *irel;
3062 struct internal_reloc *irelend;
3063 struct coff_link_hash_entry **rel_hash;
3066 if (o->reloc_count == 0)
3069 irel = finfo.section_info[o->target_index].relocs;
3070 irelend = irel + o->reloc_count;
3071 rel_hash = finfo.section_info[o->target_index].rel_hashes;
3072 erel = external_relocs;
3073 for (; irel < irelend; irel++, rel_hash++, erel += relsz)
3075 if (*rel_hash != NULL)
3077 BFD_ASSERT ((*rel_hash)->indx >= 0);
3078 irel->r_symndx = (*rel_hash)->indx;
3080 bfd_coff_swap_reloc_out (abfd, (PTR) irel, (PTR) erel);
3083 if (bfd_seek (abfd, o->rel_filepos, SEEK_SET) != 0
3084 || bfd_write ((PTR) external_relocs, relsz, o->reloc_count,
3085 abfd) != relsz * o->reloc_count)
3089 free (external_relocs);
3090 external_relocs = NULL;
3093 /* Free up the section information. */
3094 if (finfo.section_info != NULL)
3098 for (i = 0; i < abfd->section_count; i++)
3100 if (finfo.section_info[i].relocs != NULL)
3101 free (finfo.section_info[i].relocs);
3102 if (finfo.section_info[i].rel_hashes != NULL)
3103 free (finfo.section_info[i].rel_hashes);
3105 free (finfo.section_info);
3106 finfo.section_info = NULL;
3109 /* If we have optimized stabs strings, output them. */
3110 if (coff_hash_table (info)->stab_info != NULL)
3112 if (! _bfd_write_stab_strings (abfd, &coff_hash_table (info)->stab_info))
3116 /* Write out the string table. */
3117 if (obj_raw_syment_count (abfd) != 0)
3120 (obj_sym_filepos (abfd)
3121 + obj_raw_syment_count (abfd) * symesz),
3125 #if STRING_SIZE_SIZE == 4
3127 _bfd_stringtab_size (finfo.strtab) + STRING_SIZE_SIZE,
3128 (bfd_byte *) strbuf);
3130 #error Change bfd_h_put_32
3133 if (bfd_write (strbuf, 1, STRING_SIZE_SIZE, abfd) != STRING_SIZE_SIZE)
3136 if (! _bfd_stringtab_emit (abfd, finfo.strtab))
3140 _bfd_stringtab_free (finfo.strtab);
3142 /* Setting bfd_get_symcount to 0 will cause write_object_contents to
3143 not try to write out the symbols. */
3144 bfd_get_symcount (abfd) = 0;
3149 if (debug_merge_allocated)
3150 coff_debug_merge_hash_table_free (&finfo.debug_merge);
3151 if (finfo.strtab != NULL)
3152 _bfd_stringtab_free (finfo.strtab);
3153 if (finfo.section_info != NULL)
3157 for (i = 0; i < abfd->section_count; i++)
3159 if (finfo.section_info[i].relocs != NULL)
3160 free (finfo.section_info[i].relocs);
3161 if (finfo.section_info[i].rel_hashes != NULL)
3162 free (finfo.section_info[i].rel_hashes);
3164 free (finfo.section_info);
3166 if (finfo.internal_syms != NULL)
3167 free (finfo.internal_syms);
3168 if (finfo.sec_ptrs != NULL)
3169 free (finfo.sec_ptrs);
3170 if (finfo.sym_indices != NULL)
3171 free (finfo.sym_indices);
3172 if (finfo.outsyms != NULL)
3173 free (finfo.outsyms);
3174 if (finfo.linenos != NULL)
3175 free (finfo.linenos);
3176 if (finfo.contents != NULL)
3177 free (finfo.contents);
3178 if (finfo.external_relocs != NULL)
3179 free (finfo.external_relocs);
3180 if (finfo.internal_relocs != NULL)
3181 free (finfo.internal_relocs);
3182 if (external_relocs != NULL)
3183 free (external_relocs);
3189 /* The transfer vectors that lead the outside world to all of the above. */
3191 #ifdef TARGET_LITTLE_SYM
3195 TARGET_LITTLE_NAME, /* name or coff-arm-little */
3196 bfd_target_coff_flavour,
3197 BFD_ENDIAN_LITTLE, /* data byte order is little */
3198 BFD_ENDIAN_LITTLE, /* header byte order is little */
3200 (HAS_RELOC | EXEC_P | /* FIXME: object flags */
3201 HAS_LINENO | HAS_DEBUG |
3202 HAS_SYMS | HAS_LOCALS | WP_TEXT | D_PAGED),
3204 #ifndef COFF_WITH_PE
3205 (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* section flags */
3207 (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC /* section flags */
3208 | SEC_LINK_ONCE | SEC_LINK_DUPLICATES),
3211 0, /* leading char */
3212 '/', /* ar_pad_char */
3213 15, /* ar_max_namelen??? FIXMEmgo */
3215 bfd_getl64, bfd_getl_signed_64, bfd_putl64,
3216 bfd_getl32, bfd_getl_signed_32, bfd_putl32,
3217 bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* data */
3219 bfd_getl64, bfd_getl_signed_64, bfd_putl64,
3220 bfd_getl32, bfd_getl_signed_32, bfd_putl32,
3221 bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* hdrs */
3223 {_bfd_dummy_target, coff_object_p, /* bfd_check_format */
3224 bfd_generic_archive_p, /* _bfd_dummy_target */ coff_object_p },
3225 {bfd_false, coff_mkobject, _bfd_generic_mkarchive, /* bfd_set_format */
3227 {bfd_false, coff_write_object_contents, /* bfd_write_contents */
3228 _bfd_write_archive_contents, bfd_false},
3230 BFD_JUMP_TABLE_GENERIC (coff),
3231 BFD_JUMP_TABLE_COPY (coff),
3232 BFD_JUMP_TABLE_CORE (_bfd_nocore),
3233 BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_coff),
3234 BFD_JUMP_TABLE_SYMBOLS (coff),
3235 BFD_JUMP_TABLE_RELOCS (coff),
3236 BFD_JUMP_TABLE_WRITE (coff),
3237 BFD_JUMP_TABLE_LINK (coff),
3238 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
3244 #ifdef TARGET_BIG_SYM
3249 bfd_target_coff_flavour,
3250 BFD_ENDIAN_BIG, /* data byte order is big */
3251 BFD_ENDIAN_BIG, /* header byte order is big */
3253 (HAS_RELOC | EXEC_P | /* FIXME: object flags */
3254 HAS_LINENO | HAS_DEBUG |
3255 HAS_SYMS | HAS_LOCALS | WP_TEXT | D_PAGED),
3257 #ifndef COFF_WITH_PE
3258 (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* section flags */
3260 (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC /* section flags */
3261 | SEC_LINK_ONCE | SEC_LINK_DUPLICATES),
3264 0, /* leading char */
3265 '/', /* ar_pad_char */
3266 15, /* ar_max_namelen??? FIXMEmgo */
3268 bfd_getb64, bfd_getb_signed_64, bfd_putb64,
3269 bfd_getb32, bfd_getb_signed_32, bfd_putb32,
3270 bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* data */
3272 bfd_getb64, bfd_getb_signed_64, bfd_putb64,
3273 bfd_getb32, bfd_getb_signed_32, bfd_putb32,
3274 bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* hdrs */
3276 {_bfd_dummy_target, coff_object_p, /* bfd_check_format */
3277 bfd_generic_archive_p, /* _bfd_dummy_target */ coff_object_p },
3278 {bfd_false, coff_mkobject, _bfd_generic_mkarchive, /* bfd_set_format */
3280 {bfd_false, coff_write_object_contents, /* bfd_write_contents */
3281 _bfd_write_archive_contents, bfd_false},
3283 BFD_JUMP_TABLE_GENERIC (coff),
3284 BFD_JUMP_TABLE_COPY (coff),
3285 BFD_JUMP_TABLE_CORE (_bfd_nocore),
3286 BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_coff),
3287 BFD_JUMP_TABLE_SYMBOLS (coff),
3288 BFD_JUMP_TABLE_RELOCS (coff),
3289 BFD_JUMP_TABLE_WRITE (coff),
3290 BFD_JUMP_TABLE_LINK (coff),
3291 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),