1 /* BFD back-end for PowerPC Microsoft Portable Executable files.
2 Copyright 1990, 1991, 1992, 1993, 1994 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.
40 #include "coff/powerpc.h"
41 #include "coff/internal.h"
49 #define BADMAG(x) PPCBADMAG(x)
53 /* The toc is a set of bfd_vma fields. We use the fact that valid */
54 /* addresses are even (i.e. the bit representing "1" is off) to allow */
55 /* us to encode a little extra information in the field */
56 /* - Unallocated addresses are intialized to 1. */
57 /* - Allocated addresses are even numbers. */
58 /* The first time we actually write a reference to the toc in the bfd, */
59 /* we want to record that fact in a fixup file (if it is asked for), so */
60 /* we keep track of whether or not an address has been written by marking */
61 /* the low order bit with a "1" upon writing */
63 #define SET_UNALLOCATED(x) ((x) = 1)
64 #define IS_UNALLOCATED(x) ((x) == 1)
66 #define IS_WRITTEN(x) ((x) & 1)
67 #define MARK_AS_WRITTEN(x) ((x) |= 1)
68 #define MAKE_ADDR_AGAIN(x) ((x) &= ~1)
70 /* In order not to add an int to every hash table item for every coff
71 linker, we define our own hash table, derived from the coff one */
73 /* PE linker hash table entries. */
75 struct ppc_coff_link_hash_entry
77 struct coff_link_hash_entry root; /* First entry, as required */
79 /* As we wonder around the relocs, we'll keep the assigned toc_offset
81 bfd_vma toc_offset; /* Our addition, as required */
83 unsigned long int glue_insn;
87 /* Need a 7 char string for an eye catcher */
90 #define CHECK_EYE(addr) \
91 if (strcmp(addr, EYE) != 0) \
94 "File %s, line %d, Hash check failure, bad eye %8s\n", \
95 __FILE__, __LINE__, addr); \
99 /* PE linker hash table. */
101 struct ppc_coff_link_hash_table
103 struct coff_link_hash_table root; /* First entry, as required */
106 static struct bfd_hash_entry *ppc_coff_link_hash_newfunc
107 PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *,
110 /* Routine to create an entry in the link hash table. */
112 static struct bfd_hash_entry *
113 ppc_coff_link_hash_newfunc (entry, table, string)
114 struct bfd_hash_entry *entry;
115 struct bfd_hash_table *table;
118 struct ppc_coff_link_hash_entry *ret =
119 (struct ppc_coff_link_hash_entry *) entry;
121 /* Allocate the structure if it has not already been allocated by a
123 if (ret == (struct ppc_coff_link_hash_entry *) NULL)
124 ret = (struct ppc_coff_link_hash_entry *)
125 bfd_hash_allocate (table,
126 sizeof (struct ppc_coff_link_hash_entry));
128 if (ret == (struct ppc_coff_link_hash_entry *) NULL)
131 /* Call the allocation method of the superclass. */
132 ret = ((struct ppc_coff_link_hash_entry *)
133 _bfd_coff_link_hash_newfunc ((struct bfd_hash_entry *) ret,
138 /* Initialize the local fields. */
139 SET_UNALLOCATED(ret->toc_offset);
140 ret->symbol_is_glue = 0;
142 strcpy(ret->eye_catcher, EYE);
145 return (struct bfd_hash_entry *) ret;
148 /* Initialize a PE linker hash table. */
151 ppc_coff_link_hash_table_init (table, abfd, newfunc)
152 struct ppc_coff_link_hash_table *table;
154 struct bfd_hash_entry *(*newfunc) PARAMS ((struct bfd_hash_entry *,
155 struct bfd_hash_table *,
158 return _bfd_coff_link_hash_table_init (&table->root, abfd, newfunc);
161 /* Create a PE linker hash table. */
163 static struct bfd_link_hash_table *
164 ppc_coff_link_hash_table_create (abfd)
167 struct ppc_coff_link_hash_table *ret;
169 ret = ((struct ppc_coff_link_hash_table *)
170 bfd_alloc (abfd, sizeof (struct ppc_coff_link_hash_table)));
173 if (! ppc_coff_link_hash_table_init (ret, abfd,
174 ppc_coff_link_hash_newfunc))
176 bfd_release (abfd, ret);
177 return (struct bfd_link_hash_table *) NULL;
179 return &ret->root.root;
182 /* Now, tailor coffcode.h to use our hash stuff */
184 #define coff_bfd_link_hash_table_create ppc_coff_link_hash_table_create
187 /* The nt loader points the toc register to &toc + 32768, in order to */
188 /* use the complete range of a 16-bit displacement (I guess). We have */
189 /* to adjust for this when we fix up loads displaced off the toc reg. */
190 #define TOC_LOAD_ADJUSTMENT (-32768)
191 #define TOC_SECTION_NAME ".private.toc"
193 /* The main body of code is in coffcode.h. */
195 #define COFF_DEFAULT_SECTION_ALIGNMENT_POWER (3)
197 /* In case we're on a 32-bit machine, construct a 64-bit "-1" value
198 from smaller values. Start with zero, widen, *then* decrement. */
199 #define MINUS_ONE (((bfd_vma)0) - 1)
201 /* these should definitely go in a header file somewhere... */
204 #define IMAGE_REL_PPC_ABSOLUTE 0x0000
207 #define IMAGE_REL_PPC_ADDR64 0x0001
210 #define IMAGE_REL_PPC_ADDR32 0x0002
212 /* 26-bit address, shifted left 2 (branch absolute) */
213 #define IMAGE_REL_PPC_ADDR24 0x0003
216 #define IMAGE_REL_PPC_ADDR16 0x0004
218 /* 16-bit address, shifted left 2 (load doubleword) */
219 #define IMAGE_REL_PPC_ADDR14 0x0005
221 /* 26-bit PC-relative offset, shifted left 2 (branch relative) */
222 #define IMAGE_REL_PPC_REL24 0x0006
224 /* 16-bit PC-relative offset, shifted left 2 (br cond relative) */
225 #define IMAGE_REL_PPC_REL14 0x0007
227 /* 16-bit offset from TOC base */
228 #define IMAGE_REL_PPC_TOCREL16 0x0008
230 /* 16-bit offset from TOC base, shifted left 2 (load doubleword) */
231 #define IMAGE_REL_PPC_TOCREL14 0x0009
233 /* 32-bit addr w/o image base */
234 #define IMAGE_REL_PPC_ADDR32NB 0x000A
236 /* va of containing section (as in an image sectionhdr) */
237 #define IMAGE_REL_PPC_SECREL 0x000B
239 /* sectionheader number */
240 #define IMAGE_REL_PPC_SECTION 0x000C
242 /* substitute TOC restore instruction iff symbol is glue code */
243 #define IMAGE_REL_PPC_IFGLUE 0x000D
245 /* symbol is glue code; virtual address is TOC restore instruction */
246 #define IMAGE_REL_PPC_IMGLUE 0x000E
248 /* va of containing section (limited to 16 bits) */
249 #define IMAGE_REL_PPC_SECREL16 0x000F
251 /* stuff to handle immediate data when the number of bits in the */
252 /* data is greater than the number of bits in the immediate field */
253 /* We need to do (usually) 32 bit arithmetic on 16 bit chunks */
254 #define IMAGE_REL_PPC_REFHI 0x0010
255 #define IMAGE_REL_PPC_REFLO 0x0011
256 #define IMAGE_REL_PPC_PAIR 0x0012
258 /* This is essentially the same as tocrel16, with TOCDEFN assumed */
259 #define IMAGE_REL_PPC_TOCREL16_DEFN 0x0013
261 /* Flag bits in IMAGE_RELOCATION.TYPE */
263 /* subtract reloc value rather than adding it */
264 #define IMAGE_REL_PPC_NEG 0x0100
266 /* fix branch prediction bit to predict branch taken */
267 #define IMAGE_REL_PPC_BRTAKEN 0x0200
269 /* fix branch prediction bit to predict branch not taken */
270 #define IMAGE_REL_PPC_BRNTAKEN 0x0400
272 /* toc slot defined in file (or, data in toc) */
273 #define IMAGE_REL_PPC_TOCDEFN 0x0800
275 /* masks to isolate above values in IMAGE_RELOCATION.Type */
276 #define IMAGE_REL_PPC_TYPEMASK 0x00FF
277 #define IMAGE_REL_PPC_FLAGMASK 0x0F00
279 #define EXTRACT_TYPE(x) ((x) & IMAGE_REL_PPC_TYPEMASK)
280 #define EXTRACT_FLAGS(x) ((x) & IMAGE_REL_PPC_FLAGMASK)
281 #define EXTRACT_JUNK(x) \
282 ((x) & ~(IMAGE_REL_PPC_TYPEMASK | IMAGE_REL_PPC_FLAGMASK))
285 /* static helper functions to make relocation work */
286 /* (Work In Progress) */
288 static bfd_reloc_status_type ppc_refhi_reloc PARAMS ((bfd *abfd,
295 static bfd_reloc_status_type ppc_reflo_reloc PARAMS ((bfd *abfd,
302 static bfd_reloc_status_type ppc_pair_reloc PARAMS ((bfd *abfd,
311 static bfd_reloc_status_type ppc_toc16_reloc PARAMS ((bfd *abfd,
319 static bfd_reloc_status_type ppc_addr32nb_reloc PARAMS ((bfd *abfd,
327 static bfd_reloc_status_type ppc_section_reloc PARAMS ((bfd *abfd,
335 static bfd_reloc_status_type ppc_secrel_reloc PARAMS ((bfd *abfd,
343 static bfd_reloc_status_type ppc_imglue_reloc PARAMS ((bfd *abfd,
353 static boolean in_reloc_p PARAMS((bfd *abfd, reloc_howto_type *howto));
356 /* FIXME: It'll take a while to get through all of these. I only need a few to
357 get us started, so those I'll make sure work. Those marked FIXME are either
358 completely unverified or have a specific unknown marked in the comment */
360 /*---------------------------------------------------------------------------*/
362 /* Relocation entries for Windows/NT on PowerPC. */
364 /* From the document "" we find the following listed as used relocs: */
366 /* ABSOLUTE : The noop */
367 /* ADDR[64|32|16] : fields that hold addresses in data fields or the */
368 /* 16 bit displacement field on a load/store. */
369 /* ADDR[24|14] : fields that hold addresses in branch and cond */
370 /* branches. These represent [26|16] bit addresses. */
371 /* The low order 2 bits are preserved. */
372 /* REL[24|14] : branches relative to the Instruction Address */
373 /* register. These represent [26|16] bit addresses, */
374 /* as before. The instruction field will be zero, and */
375 /* the address of the SYM will be inserted at link time. */
376 /* TOCREL16 : 16 bit displacement field referring to a slot in */
378 /* TOCREL14 : 16 bit displacement field, similar to REL14 or ADDR14. */
379 /* ADDR32NB : 32 bit address relative to the virtual origin. */
380 /* (On the alpha, this is always a linker generated thunk)*/
381 /* (i.e. 32bit addr relative to the image base) */
382 /* SECREL : The value is relative to the start of the section */
383 /* containing the symbol. */
384 /* SECTION : access to the header containing the item. Supports the */
385 /* codeview debugger. */
387 /* In particular, note that the document does not indicate that the */
388 /* relocations listed in the header file are used. */
392 /*---------------------------------------------------------------------------*/
394 static reloc_howto_type ppc_coff_howto_table[] =
396 /* IMAGE_REL_PPC_ABSOLUTE 0x0000 NOP */
398 HOWTO (IMAGE_REL_PPC_ABSOLUTE, /* type */
400 0, /* size (0 = byte, 1 = short, 2 = long) */
402 false, /* pc_relative */
404 complain_overflow_dont, /* dont complain_on_overflow */
405 0, /* special_function */
406 "ABSOLUTE", /* name */
407 false, /* partial_inplace */
410 false), /* pcrel_offset */
412 /* IMAGE_REL_PPC_ADDR64 0x0001 64-bit address */
414 HOWTO(IMAGE_REL_PPC_ADDR64, /* type */
416 3, /* size (0 = byte, 1 = short, 2 = long) */
418 false, /* pc_relative */
420 complain_overflow_bitfield, /* complain_on_overflow */
421 0, /* special_function */
423 true, /* partial_inplace */
424 MINUS_ONE, /* src_mask */
425 MINUS_ONE, /* dst_mask */
426 false), /* pcrel_offset */
428 /* IMAGE_REL_PPC_ADDR32 0x0002 32-bit address */
430 HOWTO (IMAGE_REL_PPC_ADDR32, /* type */
432 2, /* size (0 = byte, 1 = short, 2 = long) */
434 false, /* pc_relative */
436 complain_overflow_bitfield, /* complain_on_overflow */
437 0, /* special_function */
439 true, /* partial_inplace */
440 0xffffffff, /* src_mask */
441 0xffffffff, /* dst_mask */
442 false), /* pcrel_offset */
444 /* IMAGE_REL_PPC_ADDR24 0x0003 26-bit address, shifted left 2 (branch absolute) */
445 /* the LI field is in bit 6 through bit 29 is 24 bits, + 2 for the shift */
446 /* Of course, That's the IBM approved bit numbering, which is not what */
447 /* anyone else uses.... The li field is in bit 2 thru 25 */
449 HOWTO (IMAGE_REL_PPC_ADDR24, /* type */
451 2, /* size (0 = byte, 1 = short, 2 = long) */
453 false, /* pc_relative */
455 complain_overflow_bitfield, /* complain_on_overflow */
456 0, /* special_function */
458 true, /* partial_inplace */
459 0x07fffffc, /* src_mask */
460 0x07fffffc, /* dst_mask */
461 false), /* pcrel_offset */
463 /* IMAGE_REL_PPC_ADDR16 0x0004 16-bit address */
465 HOWTO (IMAGE_REL_PPC_ADDR16, /* type */
467 1, /* size (0 = byte, 1 = short, 2 = long) */
469 false, /* pc_relative */
471 complain_overflow_signed, /* complain_on_overflow */
472 0, /* special_function */
474 true, /* partial_inplace */
475 0xffff, /* src_mask */
476 0xffff, /* dst_mask */
477 false), /* pcrel_offset */
479 /* IMAGE_REL_PPC_ADDR14 0x0005 */
480 /* 16-bit address, shifted left 2 (load doubleword) */
481 /* FIXME: the mask is likely wrong, and the bit position may be as well */
483 HOWTO (IMAGE_REL_PPC_ADDR14, /* type */
485 1, /* size (0 = byte, 1 = short, 2 = long) */
487 false, /* pc_relative */
489 complain_overflow_signed, /* complain_on_overflow */
490 0, /* special_function */
492 true, /* partial_inplace */
493 0xffff, /* src_mask */
494 0xffff, /* dst_mask */
495 false), /* pcrel_offset */
497 /* IMAGE_REL_PPC_REL24 0x0006 */
498 /* 26-bit PC-relative offset, shifted left 2 (branch relative) */
500 HOWTO (IMAGE_REL_PPC_REL24, /* type */
502 2, /* size (0 = byte, 1 = short, 2 = long) */
504 true, /* pc_relative */
506 complain_overflow_signed, /* complain_on_overflow */
507 0, /* special_function */
509 true, /* partial_inplace */
510 0x3fffffc, /* src_mask */
511 0x3fffffc, /* dst_mask */
512 false), /* pcrel_offset */
514 /* IMAGE_REL_PPC_REL14 0x0007 */
515 /* 16-bit PC-relative offset, shifted left 2 (br cond relative) */
516 /* FIXME: the mask is likely wrong, and the bit position may be as well */
517 /* FIXME: how does it know how far to shift? */
519 HOWTO (IMAGE_REL_PPC_ADDR14, /* type */
521 1, /* size (0 = byte, 1 = short, 2 = long) */
523 false, /* pc_relative */
525 complain_overflow_signed, /* complain_on_overflow */
526 0, /* special_function */
528 true, /* partial_inplace */
529 0xffff, /* src_mask */
530 0xffff, /* dst_mask */
531 true), /* pcrel_offset */
533 /* IMAGE_REL_PPC_TOCREL16 0x0008 */
534 /* 16-bit offset from TOC base */
536 HOWTO (IMAGE_REL_PPC_TOCREL16,/* type */
538 1, /* size (0 = byte, 1 = short, 2 = long) */
540 false, /* pc_relative */
542 complain_overflow_dont, /* complain_on_overflow */
543 ppc_toc16_reloc, /* special_function */
544 "TOCREL16", /* name */
545 false, /* partial_inplace */
546 0xffff, /* src_mask */
547 0xffff, /* dst_mask */
548 false), /* pcrel_offset */
550 /* IMAGE_REL_PPC_TOCREL14 0x0009 */
551 /* 16-bit offset from TOC base, shifted left 2 (load doubleword) */
553 HOWTO (IMAGE_REL_PPC_TOCREL14,/* type */
555 1, /* size (0 = byte, 1 = short, 2 = long) */
557 false, /* pc_relative */
559 complain_overflow_signed, /* complain_on_overflow */
560 0, /* special_function */
561 "TOCREL14", /* name */
562 false, /* partial_inplace */
563 0xffff, /* src_mask */
564 0xffff, /* dst_mask */
565 false), /* pcrel_offset */
567 /* IMAGE_REL_PPC_ADDR32NB 0x000A */
568 /* 32-bit addr w/ image base */
570 HOWTO (IMAGE_REL_PPC_ADDR32NB,/* type */
572 2, /* size (0 = byte, 1 = short, 2 = long) */
574 false, /* pc_relative */
576 complain_overflow_signed, /* complain_on_overflow */
577 0, /* special_function */
578 "ADDR32NB", /* name */
579 true, /* partial_inplace */
580 0xffffffff, /* src_mask */
581 0xffffffff, /* dst_mask */
582 false), /* pcrel_offset */
584 /* IMAGE_REL_PPC_SECREL 0x000B */
585 /* va of containing section (as in an image sectionhdr) */
587 HOWTO (IMAGE_REL_PPC_SECREL,/* type */
589 2, /* size (0 = byte, 1 = short, 2 = long) */
591 false, /* pc_relative */
593 complain_overflow_signed, /* complain_on_overflow */
594 ppc_secrel_reloc, /* special_function */
596 true, /* partial_inplace */
597 0xffffffff, /* src_mask */
598 0xffffffff, /* dst_mask */
599 true), /* pcrel_offset */
601 /* IMAGE_REL_PPC_SECTION 0x000C */
602 /* sectionheader number */
604 HOWTO (IMAGE_REL_PPC_SECTION,/* type */
606 2, /* size (0 = byte, 1 = short, 2 = long) */
608 false, /* pc_relative */
610 complain_overflow_signed, /* complain_on_overflow */
611 ppc_section_reloc, /* special_function */
612 "SECTION", /* name */
613 true, /* partial_inplace */
614 0xffffffff, /* src_mask */
615 0xffffffff, /* dst_mask */
616 true), /* pcrel_offset */
618 /* IMAGE_REL_PPC_IFGLUE 0x000D */
619 /* substitute TOC restore instruction iff symbol is glue code */
621 HOWTO (IMAGE_REL_PPC_IFGLUE,/* type */
623 2, /* size (0 = byte, 1 = short, 2 = long) */
625 false, /* pc_relative */
627 complain_overflow_signed, /* complain_on_overflow */
628 0, /* special_function */
630 true, /* partial_inplace */
631 0xffffffff, /* src_mask */
632 0xffffffff, /* dst_mask */
633 false), /* pcrel_offset */
635 /* IMAGE_REL_PPC_IMGLUE 0x000E */
636 /* symbol is glue code; virtual address is TOC restore instruction */
638 HOWTO (IMAGE_REL_PPC_IMGLUE,/* type */
640 2, /* size (0 = byte, 1 = short, 2 = long) */
642 false, /* pc_relative */
644 complain_overflow_dont, /* complain_on_overflow */
645 ppc_imglue_reloc, /* special_function */
647 false, /* partial_inplace */
648 0xffffffff, /* src_mask */
649 0xffffffff, /* dst_mask */
650 false), /* pcrel_offset */
652 /* IMAGE_REL_PPC_SECREL16 0x000F */
653 /* va of containing section (limited to 16 bits) */
655 HOWTO (IMAGE_REL_PPC_SECREL16,/* type */
657 1, /* size (0 = byte, 1 = short, 2 = long) */
659 false, /* pc_relative */
661 complain_overflow_signed, /* complain_on_overflow */
662 0, /* special_function */
663 "SECREL16", /* name */
664 true, /* partial_inplace */
665 0xffff, /* src_mask */
666 0xffff, /* dst_mask */
667 true), /* pcrel_offset */
669 /* IMAGE_REL_PPC_REFHI 0x0010 */
671 HOWTO (IMAGE_REL_PPC_REFHI, /* type */
673 1, /* size (0 = byte, 1 = short, 2 = long) */
675 false, /* pc_relative */
677 complain_overflow_signed, /* complain_on_overflow */
678 ppc_refhi_reloc, /* special_function */
680 true, /* partial_inplace */
681 0xffffffff, /* src_mask */
682 0xffffffff, /* dst_mask */
683 false), /* pcrel_offset */
685 /* IMAGE_REL_PPC_REFLO 0x0011 */
687 HOWTO (IMAGE_REL_PPC_REFLO, /* type */
689 1, /* size (0 = byte, 1 = short, 2 = long) */
691 false, /* pc_relative */
693 complain_overflow_signed, /* complain_on_overflow */
694 ppc_refhi_reloc, /* special_function */
696 true, /* partial_inplace */
697 0xffffffff, /* src_mask */
698 0xffffffff, /* dst_mask */
699 false), /* pcrel_offset */
701 /* IMAGE_REL_PPC_PAIR 0x0012 */
703 HOWTO (IMAGE_REL_PPC_PAIR, /* type */
705 1, /* size (0 = byte, 1 = short, 2 = long) */
707 false, /* pc_relative */
709 complain_overflow_signed, /* complain_on_overflow */
710 ppc_pair_reloc, /* special_function */
712 true, /* partial_inplace */
713 0xffffffff, /* src_mask */
714 0xffffffff, /* dst_mask */
715 false), /* pcrel_offset */
717 /* IMAGE_REL_PPC_TOCREL16_DEFN 0x0013 */
718 /* 16-bit offset from TOC base, without causing a definition */
720 HOWTO ( (IMAGE_REL_PPC_TOCREL16 | IMAGE_REL_PPC_TOCDEFN), /* type */
722 1, /* size (0 = byte, 1 = short, 2 = long) */
724 false, /* pc_relative */
726 complain_overflow_dont, /* complain_on_overflow */
727 0, /* special_function */
728 "TOCREL16, TOCDEFN", /* name */
729 false, /* partial_inplace */
730 0xffff, /* src_mask */
731 0xffff, /* dst_mask */
732 false), /* pcrel_offset */
739 /* Some really cheezy macros that can be turned on to test stderr :-) */
748 fprintf(stderr,"Unimplemented Relocation -- %s\n",x); \
752 #define DUMP_RELOC(n,r) \
754 fprintf(stderr,"%s sym %d, addr %d, addend %d\n", \
755 n, (*(r->sym_ptr_ptr))->name, \
756 r->address, r->addend); \
759 /* Given a reloc name, n, and a pointer to an internal_reloc,
760 dump out interesting information on the contents
762 #define n_name _n._n_name
763 #define n_zeroes _n._n_n._n_zeroes
764 #define n_offset _n._n_n._n_offset
768 #define DUMP_RELOC2(n,r) \
770 fprintf(stderr,"%s sym %d, r_vaddr %d %s\n", \
771 n, r->r_symndx, r->r_vaddr,\
772 (((r->r_type) & IMAGE_REL_PPC_TOCDEFN) == 0) \
778 #define DUMP_RELOC(n,r)
779 #define DUMP_RELOC2(n,r)
784 /* toc construction and management routines */
785 extern bfd* bfd_of_toc_owner;
786 extern long int global_toc_size;
788 extern long int import_table_size;
789 extern long int first_thunk_address;
790 extern long int thunk_size;
808 struct list_ele *next;
810 enum ref_category cat;
815 extern struct list_ele *head;
816 extern struct list_ele *tail;
819 record_toc(toc_section, our_toc_offset, cat, name)
820 asection *toc_section;
822 enum ref_category cat;
825 /* add this entry to our toc addr-offset-name list */
827 t = bfd_malloc (sizeof (struct list_ele));
831 t->offset = our_toc_offset;
834 t->addr = toc_section->output_offset + our_toc_offset;
848 /* record a toc offset against a symbol */
850 ppc_record_toc_entry(abfd, info, sec, sym, toc_kind)
852 struct bfd_link_info *info;
855 enum toc_type toc_kind;
858 bfd_byte *old_contents;
863 struct ppc_coff_link_hash_entry *h;
864 struct coff_symbol_struct *target;
872 h = (struct ppc_coff_link_hash_entry *) (obj_coff_sym_hashes (abfd)[sym]);
875 CHECK_EYE(h->eye_catcher);
880 local_syms = obj_coff_local_toc_table(abfd);
884 /* allocate a table */
886 (int *) bfd_zalloc (abfd,
887 obj_raw_syment_count(abfd) * sizeof(int));
890 obj_coff_local_toc_table(abfd) = local_syms;
891 for (i = 0; i < obj_raw_syment_count(abfd); ++i)
893 SET_UNALLOCATED(local_syms[i]);
897 if (IS_UNALLOCATED(local_syms[sym]))
899 local_syms[sym] = global_toc_size;
900 ret_val = global_toc_size;
901 global_toc_size += 4;
903 /* The size must fit in a 16bit displacment */
904 if (global_toc_size >= 65535)
907 "Exceeded toc size of 65535\n");
913 "Setting toc_offset for local sym %d to %d\n",
919 ret_val = local_syms[sym];
922 "toc_offset already set for local sym %d to %d\n",
929 name = h->root.root.root.string;
931 /* check to see if there's a toc slot allocated. If not, do it
932 here. It will be used in relocate_section */
933 if (IS_UNALLOCATED(h->toc_offset))
935 h->toc_offset = global_toc_size;
936 ret_val = global_toc_size;
937 global_toc_size += 4;
939 /* The size must fit in a 16bit displacment */
940 if (global_toc_size >= 65535)
943 "Exceeded toc size of 65535\n");
949 "Setting toc_offset for sym %d (%s) [h=%p] to %d\n",
950 sym, name, h, ret_val);
955 ret_val = h->toc_offset;
958 "toc_offset already set for sym %d (%s) [h=%p] to %d\n",
959 sym, name, h, ret_val);
966 /* FIXME: record a toc offset against a data-in-toc symbol */
967 /* Now, there is currenly some confusion on what this means. In some
968 compilers one sees the moral equivalent of:
972 refer to the data with a [tocv] qualifier
973 In general, one sees something to indicate that a tocd has been
974 seen, and that would trigger the allocation of data in toc. The IBM
975 docs seem to suggest that anything with the TOCDEFN qualifier should
976 never trigger storage allocation. However, in the kernel32.lib that
977 we've been using for our test bed, there are a couple of variables
978 referenced that fail that test.
980 So it can't work that way.
983 ppc_record_data_in_toc_entry(abfd, info, sec, sym, toc_kind)
985 struct bfd_link_info *info;
988 enum toc_type toc_kind;
991 bfd_byte *old_contents;
996 struct ppc_coff_link_hash_entry *h = 0;
997 struct coff_symbol_struct *target;
1003 h = (struct ppc_coff_link_hash_entry *) (obj_coff_sym_hashes (abfd)[sym]);
1007 local_syms = obj_coff_local_toc_table(abfd);
1008 if (local_syms == 0)
1011 /* allocate a table */
1013 (int *) bfd_zalloc (abfd,
1014 obj_raw_syment_count(abfd) * sizeof(int));
1015 if (local_syms == 0)
1017 obj_coff_local_toc_table(abfd) = local_syms;
1018 for (i = 0; i < obj_raw_syment_count(abfd); ++i)
1020 SET_UNALLOCATED(local_syms[i]);
1024 if (IS_UNALLOCATED(local_syms[sym]))
1026 local_syms[sym] = global_toc_size;
1027 ret_val = global_toc_size;
1028 global_toc_size += 4;
1031 "Setting data_in_toc_offset for local sym %d to %d\n",
1037 ret_val = local_syms[sym];
1040 "data_in_toc_offset already set for local sym %d to %d\n",
1047 CHECK_EYE(h->eye_catcher);
1049 name = h->root.root.root.string;
1051 /* check to see if there's a toc slot allocated. If not, do it
1052 here. It will be used in relocate_section */
1053 if (IS_UNALLOCATED(h->toc_offset))
1056 h->toc_offset = global_toc_size;
1058 ret_val = global_toc_size;
1059 /* We're allocating a chunk of the toc, as opposed to a slot */
1060 /* FIXME: alignment? */
1062 global_toc_size += 4;
1065 "Setting data_in_toc_offset for sym %d (%s) [h=%p] to %d\n",
1066 sym, name, h, ret_val);
1071 ret_val = h->toc_offset;
1074 "data_in_toc_offset already set for sym %d (%s) [h=%p] to %d\n",
1075 sym, name, h, ret_val);
1083 /* record a toc offset against a symbol */
1085 ppc_mark_symbol_as_glue(abfd, sym, rel)
1088 struct internal_reloc *rel;
1090 struct ppc_coff_link_hash_entry *h;
1092 h = (struct ppc_coff_link_hash_entry *) (obj_coff_sym_hashes (abfd)[sym]);
1094 CHECK_EYE(h->eye_catcher);
1096 h->symbol_is_glue = 1;
1097 h->glue_insn = bfd_get_32 (abfd, (bfd_byte *) &rel->r_vaddr);
1103 /* Provided the symbol, returns the value reffed */
1104 static long get_symbol_value PARAMS ((asymbol *));
1107 get_symbol_value (symbol)
1110 long relocation = 0;
1112 if (bfd_is_com_section (symbol->section))
1118 relocation = symbol->value +
1119 symbol->section->output_section->vma +
1120 symbol->section->output_offset;
1126 /* Return true if this relocation should
1127 appear in the output .reloc section. */
1129 static boolean in_reloc_p(abfd, howto)
1131 reloc_howto_type *howto;
1134 (! howto->pc_relative)
1135 && (howto->type != IMAGE_REL_PPC_ADDR32NB)
1136 && (howto->type != IMAGE_REL_PPC_TOCREL16)
1137 && (howto->type != IMAGE_REL_PPC_IMGLUE)
1138 && (howto->type != IMAGE_REL_PPC_IFGLUE)
1139 && (howto->type != IMAGE_REL_PPC_SECREL)
1140 && (howto->type != IMAGE_REL_PPC_SECTION)
1141 && (howto->type != IMAGE_REL_PPC_SECREL16)
1142 && (howto->type != IMAGE_REL_PPC_REFHI)
1143 && (howto->type != IMAGE_REL_PPC_REFLO)
1144 && (howto->type != IMAGE_REL_PPC_PAIR)
1145 && (howto->type != IMAGE_REL_PPC_TOCREL16_DEFN) ;
1148 /* this function is in charge of performing all the ppc PE relocations */
1149 /* Don't yet know if we want to do this this particular way ... (krk) */
1150 /* FIXME: (it is not yet enabled) */
1152 static bfd_reloc_status_type
1153 pe_ppc_reloc (abfd, reloc_entry, symbol_in, data, input_section, output_bfd,
1156 arelent *reloc_entry;
1159 asection *input_section;
1161 char **error_message;
1163 /* the consth relocation comes in two parts, we have to remember
1164 the state between calls, in these variables */
1165 static boolean part1_consth_active = false;
1166 static unsigned long part1_consth_value;
1169 unsigned long sym_value;
1170 unsigned long unsigned_value;
1171 unsigned short r_type;
1174 unsigned long addr = reloc_entry->address ; /*+ input_section->vma*/
1175 bfd_byte *hit_data =addr + (bfd_byte *)(data);
1177 fprintf(stderr, "pe_ppc_reloc (%s)\n", TARGET_LITTLE_NAME);
1179 r_type = reloc_entry->howto->type;
1183 /* Partial linking - do nothing */
1184 reloc_entry->address += input_section->output_offset;
1185 return bfd_reloc_ok;
1188 if (symbol_in != NULL
1189 && bfd_is_und_section (symbol_in->section))
1191 /* Keep the state machine happy in case we're called again */
1192 if (r_type == IMAGE_REL_PPC_REFHI)
1194 part1_consth_active = true;
1195 part1_consth_value = 0;
1197 return(bfd_reloc_undefined);
1200 if ((part1_consth_active) && (r_type != IMAGE_REL_PPC_PAIR))
1202 part1_consth_active = false;
1203 *error_message = (char *) "Missing PAIR";
1204 return(bfd_reloc_dangerous);
1208 sym_value = get_symbol_value(symbol_in);
1210 return(bfd_reloc_ok);
1213 /* The reloc processing routine for the optimized COFF linker. */
1216 coff_ppc_relocate_section (output_bfd, info, input_bfd, input_section,
1217 contents, relocs, syms, sections)
1219 struct bfd_link_info *info;
1221 asection *input_section;
1223 struct internal_reloc *relocs;
1224 struct internal_syment *syms;
1225 asection **sections;
1227 struct internal_reloc *rel;
1228 struct internal_reloc *relend;
1231 asection *toc_section = 0;
1233 reloc_howto_type *howto = 0;
1237 "pe_ppc_relocate_section (%s) for %s \n",
1239 input_section->name);
1243 /* If we are performing a relocateable link, we don't need to do a
1244 thing. The caller will take care of adjusting the reloc
1245 addresses and symbol indices. */
1246 if (info->relocateable)
1253 relend = rel + input_section->reloc_count;
1254 for (; rel < relend; rel++)
1257 struct ppc_coff_link_hash_entry *h;
1258 struct internal_syment *sym;
1262 bfd_reloc_status_type rstat;
1265 unsigned short r_type = EXTRACT_TYPE (rel->r_type);
1266 unsigned short r_flags = EXTRACT_FLAGS(rel->r_type);
1267 unsigned short junk = EXTRACT_JUNK (rel->r_type);
1270 /* now examine flags */
1273 fprintf (stderr, "Reloc with flags found!");
1274 if ( r_flags & IMAGE_REL_PPC_NEG )
1275 fprintf (stderr, " NEG");
1276 if ( r_flags & IMAGE_REL_PPC_BRTAKEN )
1277 fprintf (stderr, " BRTAKEN");
1278 if ( r_flags & IMAGE_REL_PPC_BRNTAKEN )
1279 fprintf (stderr, " BRNTAKEN");
1280 if ( r_flags & IMAGE_REL_PPC_TOCDEFN )
1281 fprintf (stderr, " TOCDEFN");
1282 fprintf(stderr, "\n");
1286 symndx = rel->r_symndx;
1287 loc = contents + rel->r_vaddr - input_section->vma;
1289 /* FIXME: check bounds on r_type */
1290 howto = ppc_coff_howto_table + r_type;
1299 h = (struct ppc_coff_link_hash_entry *)
1300 (obj_coff_sym_hashes (input_bfd)[symndx]);
1303 CHECK_EYE(h->eye_catcher);
1306 sym = syms + symndx;
1312 /* FIXME: PAIR unsupported in the following code */
1316 sec = bfd_abs_section_ptr;
1319 sec = sections[symndx];
1320 val = (sec->output_section->vma
1321 + sec->output_offset
1328 CHECK_EYE(h->eye_catcher);
1330 if (h->root.root.type == bfd_link_hash_defined
1331 || h->root.root.type == bfd_link_hash_defweak)
1333 sec = h->root.root.u.def.section;
1334 val = (h->root.root.u.def.value
1335 + sec->output_section->vma
1336 + sec->output_offset);
1340 if (! ((*info->callbacks->undefined_symbol)
1341 (info, h->root.root.root.string, input_bfd, input_section,
1342 rel->r_vaddr - input_section->vma)))
1347 rstat = bfd_reloc_ok;
1349 /* Each case must do its own relocation, setting rstat appropriately */
1354 "ERROR: during reloc processing -- unsupported reloc %s\n",
1356 bfd_set_error (bfd_error_bad_value);
1359 case IMAGE_REL_PPC_TOCREL16:
1361 bfd_vma our_toc_offset;
1364 DUMP_RELOC2(howto->name, rel);
1366 if (toc_section == 0)
1368 toc_section = bfd_get_section_by_name (bfd_of_toc_owner,
1373 "BFD of toc owner %p, section addr of %s %p\n",
1374 bfd_of_toc_owner, TOC_SECTION_NAME, toc_section);
1377 if ( toc_section == NULL )
1379 fprintf(stderr, "No Toc section!\n");
1385 * Amazing bit tricks present. As we may have seen earlier, we
1386 * use the 1 bit to tell us whether or not a toc offset has been
1387 * allocated. Now that they've all been allocated, we will use
1388 * the 1 bit to tell us if we've written this particular toc
1393 { /* it is a file local symbol */
1394 int *local_toc_table;
1397 sym = syms + symndx;
1398 name = sym->_n._n_name;
1400 local_toc_table = obj_coff_local_toc_table(input_bfd);
1401 our_toc_offset = local_toc_table[symndx];
1403 if (IS_WRITTEN(our_toc_offset))
1405 /* if it has been written out, it is marked with the
1406 1 bit. Fix up our offset, but do not write it out
1409 MAKE_ADDR_AGAIN(our_toc_offset);
1413 "Not writing out toc_offset of %d for %s\n",
1414 our_toc_offset, name);
1419 /* write out the toc entry */
1420 record_toc(toc_section, our_toc_offset, priv, strdup(name));
1423 "Writing out toc_offset "
1424 "toc_section (%p,%p)+%d val %d for %s\n",
1426 toc_section->contents,
1432 bfd_put_32(output_bfd,
1434 toc_section->contents + our_toc_offset);
1436 MARK_AS_WRITTEN(local_toc_table[symndx]);
1442 const char *name = h->root.root.root.string;
1443 our_toc_offset = h->toc_offset;
1445 if ((r_flags & IMAGE_REL_PPC_TOCDEFN)
1446 == IMAGE_REL_PPC_TOCDEFN
1447 && IS_UNALLOCATED(our_toc_offset))
1449 /* This is unbelievable cheese. Some knowledgable asm
1450 hacker has decided to use r2 as a base for loading
1451 a value. He/She does this by setting the tocdefn bit,
1452 and not supplying a toc definition. The behaviour is
1453 then to use the difference between the value of the
1454 symbol and the actual location of the toc as the toc
1457 In fact, what is usually happening is, because the
1458 Import Address Table is mapped immediately following
1459 the toc, some trippy library code trying for speed on
1460 dll linkage, takes advantage of that and considers
1461 the IAT to be part of the toc, thus saving a load.
1463 our_toc_offset = val -
1464 (toc_section->output_section->vma +
1465 toc_section->output_offset);
1467 /* The size must still fit in a 16bit displacment */
1468 if (our_toc_offset >= 65535)
1471 "TOCDEFN Relocation exceeded "
1472 "displacment of 65535\n");
1476 record_toc(toc_section, our_toc_offset, pub, strdup(name));
1478 else if (IS_WRITTEN(our_toc_offset))
1480 /* if it has been written out, it is marked with the
1481 1 bit. Fix up our offset, but do not write it out
1484 MAKE_ADDR_AGAIN(our_toc_offset);
1487 "Not writing out toc_offset of %d for %s\n",
1488 our_toc_offset, name);
1493 record_toc(toc_section, our_toc_offset, pub, strdup(name));
1496 /* write out the toc entry */
1498 "Writing out toc_offset "
1499 "toc_section (%p,%p)+%d val %d for %s\n",
1501 toc_section->contents,
1507 /* write out the toc entry */
1508 bfd_put_32(output_bfd,
1510 toc_section->contents + our_toc_offset);
1512 MARK_AS_WRITTEN(h->toc_offset);
1513 /* The tricky part is that this is the address that */
1514 /* needs a .reloc entry for it */
1519 if (fixit && info->base_file)
1521 /* So if this is non pcrelative, and is referenced
1522 to a section or a common symbol, then it needs a reloc */
1524 /* relocation to a symbol in a section which
1525 isn't absolute - we output the address here
1528 bfd_vma addr = toc_section->output_section->vma
1529 + toc_section->output_offset + our_toc_offset;
1531 if (coff_data(output_bfd)->pe)
1532 addr -= pe_data(output_bfd)->pe_opthdr.ImageBase;
1536 " Toc Section .reloc candidate addr = %x\n", addr);
1538 fwrite (&addr, 1,4, (FILE *) info->base_file);
1542 /* FIXME: this test is conservative */
1543 if ( (r_flags & IMAGE_REL_PPC_TOCDEFN) != IMAGE_REL_PPC_TOCDEFN &&
1544 our_toc_offset > toc_section->_raw_size)
1547 "reloc offset is bigger than the toc size!\n");
1551 /* Now we know the relocation for this toc reference */
1552 relocation = our_toc_offset + TOC_LOAD_ADJUSTMENT;
1553 rstat = _bfd_relocate_contents (howto,
1559 case IMAGE_REL_PPC_IFGLUE:
1561 /* To solve this, we need to know whether or not the symbol */
1562 /* appearing on the call instruction is a glue function or not. */
1563 /* A glue function must announce itself via a IMGLUE reloc, and */
1564 /* the reloc contains the required toc restore instruction */
1567 const char *my_name;
1568 DUMP_RELOC2(howto->name, rel);
1572 my_name = h->root.root.root.string;
1573 if (h->symbol_is_glue == 1)
1575 x = bfd_get_32(input_bfd, loc);
1576 bfd_put_32(input_bfd, h->glue_insn, loc);
1581 case IMAGE_REL_PPC_SECREL:
1582 /* Unimplemented: codeview debugging information */
1583 /* For fast access to the header of the section
1584 containing the item. */
1586 case IMAGE_REL_PPC_SECTION:
1587 /* Unimplemented: codeview debugging information */
1588 /* Is used to indicate that the value should be relative
1589 to the beginning of the section that contains the
1592 case IMAGE_REL_PPC_ABSOLUTE:
1594 const char *my_name;
1596 my_name = (syms+symndx)->_n._n_name;
1599 my_name = h->root.root.root.string;
1603 "Warning: unsupported reloc %s <file %s, section %s>\n",
1605 bfd_get_filename(input_bfd),
1606 input_section->name);
1608 fprintf(stderr,"sym %d (%s), r_vaddr %d (%x)\n",
1609 rel->r_symndx, my_name, rel->r_vaddr, rel->r_vaddr);
1612 case IMAGE_REL_PPC_IMGLUE:
1614 /* There is nothing to do now. This reloc was noted in the first
1615 pass over the relocs, and the glue instruction extracted */
1616 const char *my_name;
1617 if (h->symbol_is_glue == 1)
1619 my_name = h->root.root.root.string;
1621 "Warning: previously missed IMGLUE reloc %s <file %s, section %s>\n",
1623 bfd_get_filename(input_bfd),
1624 input_section->name);
1630 case IMAGE_REL_PPC_ADDR32NB:
1632 struct coff_link_hash_entry *myh = 0;
1633 const char *name = 0;
1634 DUMP_RELOC2(howto->name, rel);
1636 { /* it is a file local symbol */
1637 sym = syms + symndx;
1638 name = sym->_n._n_name;
1644 name = h->root.root.root.string;
1645 if (strcmp(".idata$2", name) == 0)
1646 target = "__idata2_magic__";
1647 else if (strcmp(".idata$4", name) == 0)
1648 target = "__idata4_magic__";
1649 else if (strcmp(".idata$5", name) == 0)
1650 target = "__idata5_magic__";
1656 myh = coff_link_hash_lookup (coff_hash_table (info),
1658 false, false, true);
1661 fprintf(stderr, "Missing idata magic cookies, "
1662 "this cannot work anyway...\n");
1666 val = myh->root.u.def.value +
1667 sec->output_section->vma + sec->output_offset;
1668 if (first_thunk_address == 0)
1671 myh = coff_link_hash_lookup (coff_hash_table (info),
1673 false, false, true);
1674 first_thunk_address = myh->root.u.def.value +
1675 sec->output_section->vma +
1676 sec->output_offset -
1677 pe_data(output_bfd)->pe_opthdr.ImageBase;
1679 idata5offset = myh->root.u.def.value;
1680 myh = coff_link_hash_lookup (coff_hash_table (info),
1682 false, false, true);
1684 thunk_size = myh->root.u.def.value - idata5offset;
1685 myh = coff_link_hash_lookup (coff_hash_table (info),
1687 false, false, true);
1688 import_table_size = myh->root.u.def.value;
1693 rstat = _bfd_relocate_contents (howto,
1696 pe_data(output_bfd)->pe_opthdr.ImageBase,
1701 case IMAGE_REL_PPC_REL24:
1702 DUMP_RELOC2(howto->name, rel);
1703 val -= (input_section->output_section->vma
1704 + input_section->output_offset);
1706 rstat = _bfd_relocate_contents (howto,
1711 case IMAGE_REL_PPC_ADDR16:
1712 case IMAGE_REL_PPC_ADDR24:
1713 case IMAGE_REL_PPC_ADDR32:
1714 DUMP_RELOC2(howto->name, rel);
1715 rstat = _bfd_relocate_contents (howto,
1722 if ( info->base_file )
1724 /* So if this is non pcrelative, and is referenced
1725 to a section or a common symbol, then it needs a reloc */
1726 if (sym && pe_data(output_bfd)->in_reloc_p(output_bfd, howto))
1728 /* relocation to a symbol in a section which
1729 isn't absolute - we output the address here
1731 bfd_vma addr = rel->r_vaddr
1732 - input_section->vma
1733 + input_section->output_offset
1734 + input_section->output_section->vma;
1736 if (coff_data(output_bfd)->pe)
1738 bfd_vma before_addr = addr;
1739 addr -= pe_data(output_bfd)->pe_opthdr.ImageBase;
1742 " adjusted down from %x to %x", before_addr, addr);
1746 fprintf(stderr, "\n");
1749 fwrite (&addr, 1,4, (FILE *) info->base_file);
1759 case bfd_reloc_overflow:
1762 char buf[SYMNMLEN + 1];
1767 name = h->root.root.root.string;
1768 else if (sym == NULL)
1770 else if (sym->_n._n_n._n_zeroes == 0
1771 && sym->_n._n_n._n_offset != 0)
1772 name = obj_coff_strings (input_bfd) + sym->_n._n_n._n_offset;
1775 strncpy (buf, sym->_n._n_name, SYMNMLEN);
1776 buf[SYMNMLEN] = '\0';
1782 name = _bfd_coff_internal_syment_name (input_bfd, sym, buf);
1788 if (! ((*info->callbacks->reloc_overflow)
1789 (info, name, howto->name,
1790 (bfd_vma) 0, input_bfd,
1791 input_section, rel->r_vaddr - input_section->vma)))
1801 #ifdef COFF_IMAGE_WITH_PE
1803 long int global_toc_size = 0;
1805 bfd* bfd_of_toc_owner = 0;
1807 long int import_table_size;
1808 long int first_thunk_address;
1809 long int thunk_size;
1811 struct list_ele *head;
1812 struct list_ele *tail;
1815 h1 = "\n\t\t\tTOC MAPPING\n\n";
1817 h2 = " TOC disassembly Comments Name\n";
1819 h3 = " Offset spelling (if present)\n";
1832 for(t = head; t != 0; t=t->next)
1838 else if (t->cat == pub)
1840 else if (t->cat == data)
1841 cat = "data-in-toc ";
1843 if (t->offset > global_toc_size)
1845 if (t->offset <= global_toc_size + thunk_size)
1846 cat = "IAT reference ";
1848 cat = "Out of bounds!";
1852 " %04lx (%d)", t->offset, t->offset - 32768);
1859 fprintf(file, "\n");
1863 ppc_allocate_toc_section (info)
1864 struct bfd_link_info *info;
1868 static char test_char = '1';
1870 if ( global_toc_size == 0 ) /* FIXME: does this get me in trouble? */
1873 if (bfd_of_toc_owner == 0)
1876 "There is no bfd that owns the toc section!\n");
1880 s = bfd_get_section_by_name ( bfd_of_toc_owner , TOC_SECTION_NAME);
1883 fprintf(stderr, "No Toc section!\n");
1887 foo = bfd_alloc(bfd_of_toc_owner, global_toc_size);
1888 memset(foo, test_char, global_toc_size);
1890 s->_raw_size = s->_cooked_size = global_toc_size;
1897 ppc_process_before_allocation (abfd, info)
1899 struct bfd_link_info *info;
1902 struct internal_reloc *i, *rel;
1906 "ppc_process_before_allocation: BFD %s\n",
1907 bfd_get_filename(abfd));
1910 /* here we have a bfd that is to be included on the link. We have a hook
1911 to do reloc rummaging, before section sizes are nailed down. */
1913 _bfd_coff_get_external_symbols(abfd);
1915 /* rummage around all the relocs and map the toc */
1916 sec = abfd->sections;
1923 for (; sec != 0; sec = sec->next)
1929 " section %s reloc count %d\n",
1934 if (sec->reloc_count == 0)
1937 /* load the relocs */
1938 /* FIXME: there may be a storage leak here */
1939 i=_bfd_coff_read_internal_relocs(abfd,sec,1,0,0,0);
1944 for (rel=i;rel<i+sec->reloc_count;++rel)
1946 unsigned short r_type = EXTRACT_TYPE (rel->r_type);
1947 unsigned short r_flags = EXTRACT_FLAGS(rel->r_type);
1948 unsigned short junk = EXTRACT_JUNK (rel->r_type);
1951 /* now examine flags */
1954 fprintf (stderr, "Reloc with flags found!");
1955 if ( r_flags & IMAGE_REL_PPC_NEG )
1956 fprintf (stderr, " NEG");
1957 if ( r_flags & IMAGE_REL_PPC_BRTAKEN )
1958 fprintf (stderr, " BRTAKEN");
1959 if ( r_flags & IMAGE_REL_PPC_BRNTAKEN )
1960 fprintf (stderr, " BRNTAKEN");
1961 if ( r_flags & IMAGE_REL_PPC_TOCDEFN )
1962 fprintf (stderr, " TOCDEFN");
1963 fprintf(stderr, "\n");
1967 DUMP_RELOC2(ppc_coff_howto_table[r_type].name, rel);
1971 case IMAGE_REL_PPC_TOCREL16:
1974 This remains unimplemented for now, as it currently adds
1975 un-necessary elements to the toc. All we need to do today
1976 is not do anything if TOCDEFN is on.
1978 if ( r_flags & IMAGE_REL_PPC_TOCDEFN )
1979 toc_offset = ppc_record_data_in_toc_entry(abfd, info, sec,
1983 toc_offset = ppc_record_toc_entry(abfd, info, sec,
1984 rel->r_symndx, default_toc);
1986 if ( (r_flags & IMAGE_REL_PPC_TOCDEFN) != IMAGE_REL_PPC_TOCDEFN )
1987 toc_offset = ppc_record_toc_entry(abfd, info, sec,
1988 rel->r_symndx, default_toc);
1990 case IMAGE_REL_PPC_IMGLUE:
1991 ppc_mark_symbol_as_glue(abfd, rel->r_symndx, rel);
2003 static bfd_reloc_status_type
2004 ppc_refhi_reloc (abfd,
2012 arelent *reloc_entry;
2015 asection *input_section;
2017 char **error_message;
2020 DUMP_RELOC("REFHI",reloc_entry);
2022 if (output_bfd == (bfd *) NULL)
2023 return bfd_reloc_continue;
2025 return bfd_reloc_undefined;
2028 static bfd_reloc_status_type
2029 ppc_reflo_reloc (abfd,
2037 arelent *reloc_entry;
2040 asection *input_section;
2042 char **error_message;
2045 DUMP_RELOC("REFLO",reloc_entry);
2047 if (output_bfd == (bfd *) NULL)
2048 return bfd_reloc_continue;
2050 return bfd_reloc_undefined;
2053 static bfd_reloc_status_type
2054 ppc_pair_reloc (abfd,
2062 arelent *reloc_entry;
2065 asection *input_section;
2067 char **error_message;
2070 DUMP_RELOC("PAIR",reloc_entry);
2072 if (output_bfd == (bfd *) NULL)
2073 return bfd_reloc_continue;
2075 return bfd_reloc_undefined;
2079 static bfd_reloc_status_type
2080 ppc_toc16_reloc (abfd,
2088 arelent *reloc_entry;
2091 asection *input_section;
2093 char **error_message;
2095 UN_IMPL("TOCREL16");
2096 DUMP_RELOC("TOCREL16",reloc_entry);
2098 if (output_bfd == (bfd *) NULL)
2100 return bfd_reloc_continue;
2103 return bfd_reloc_ok;
2106 /* ADDR32NB : 32 bit address relative to the virtual origin. */
2107 /* (On the alpha, this is always a linker generated thunk)*/
2108 /* (i.e. 32bit addr relative to the image base) */
2112 static bfd_reloc_status_type
2113 ppc_addr32nb_reloc (abfd,
2121 arelent *reloc_entry;
2124 asection *input_section;
2126 char **error_message;
2128 UN_IMPL("ADDR32NB");
2129 DUMP_RELOC("ADDR32NB",reloc_entry);
2131 return bfd_reloc_ok;
2134 static bfd_reloc_status_type
2135 ppc_secrel_reloc (abfd,
2143 arelent *reloc_entry;
2146 asection *input_section;
2148 char **error_message;
2151 DUMP_RELOC("SECREL",reloc_entry);
2153 if (output_bfd == (bfd *) NULL)
2154 return bfd_reloc_continue;
2156 return bfd_reloc_ok;
2159 static bfd_reloc_status_type
2160 ppc_section_reloc (abfd,
2168 arelent *reloc_entry;
2171 asection *input_section;
2173 char **error_message;
2176 DUMP_RELOC("SECTION",reloc_entry);
2178 if (output_bfd == (bfd *) NULL)
2179 return bfd_reloc_continue;
2181 return bfd_reloc_ok;
2184 static bfd_reloc_status_type
2185 ppc_imglue_reloc (abfd,
2193 arelent *reloc_entry;
2196 asection *input_section;
2198 char **error_message;
2201 DUMP_RELOC("IMGLUE",reloc_entry);
2203 if (output_bfd == (bfd *) NULL)
2204 return bfd_reloc_continue;
2206 return bfd_reloc_ok;
2211 #define MAX_RELOC_INDEX \
2212 (sizeof(ppc_coff_howto_table) / sizeof(ppc_coff_howto_table[0]) - 1)
2215 /* FIXME: There is a possiblity that when we read in a reloc from a file,
2216 that there are some bits encoded in the upper portion of the
2217 type field. Not yet implemented.
2219 static void ppc_coff_rtype2howto PARAMS ((arelent *relent,
2220 struct internal_reloc *internal));
2223 ppc_coff_rtype2howto (relent, internal)
2225 struct internal_reloc *internal;
2228 /* We can encode one of three things in the type field, aside from the
2230 1. IMAGE_REL_PPC_NEG - indicates the value field is a subtraction
2231 value, rather than an addition value
2232 2. IMAGE_REL_PPC_BRTAKEN, IMAGE_REL_PPC_BRNTAKEN - indicates that
2233 the branch is expected to be taken or not.
2234 3. IMAGE_REL_PPC_TOCDEFN - toc slot definition in the file
2235 For now, we just strip this stuff to find the type, and ignore it other
2238 reloc_howto_type *howto;
2239 unsigned short r_type = EXTRACT_TYPE (internal->r_type);
2240 unsigned short r_flags = EXTRACT_FLAGS(internal->r_type);
2241 unsigned short junk = EXTRACT_JUNK (internal->r_type);
2243 /* the masking process only slices off the bottom byte for r_type. */
2244 if ( r_type > MAX_RELOC_INDEX )
2247 "ppc_coff_rtype2howto: reloc index %d out of range [%d, %d]\n",
2248 internal->r_type, 0, MAX_RELOC_INDEX);
2252 /* check for absolute crap */
2256 "ppc_coff_rtype2howto: reloc index %d contains junk %d\n",
2257 internal->r_type, junk);
2262 /* now examine flags */
2265 fprintf (stderr, "Reloc with flags found!");
2266 if ( r_flags & IMAGE_REL_PPC_NEG )
2267 fprintf (stderr, " NEG");
2268 if ( r_flags & IMAGE_REL_PPC_BRTAKEN )
2269 fprintf (stderr, " BRTAKEN");
2270 if ( r_flags & IMAGE_REL_PPC_BRNTAKEN )
2271 fprintf (stderr, " BRNTAKEN");
2272 if ( r_flags & IMAGE_REL_PPC_TOCDEFN )
2273 fprintf (stderr, " TOCDEFN");
2274 fprintf(stderr, "\n");
2280 case IMAGE_REL_PPC_ADDR16:
2281 case IMAGE_REL_PPC_REL24:
2282 case IMAGE_REL_PPC_ADDR24:
2283 case IMAGE_REL_PPC_ADDR32:
2284 case IMAGE_REL_PPC_IFGLUE:
2285 case IMAGE_REL_PPC_ADDR32NB:
2286 case IMAGE_REL_PPC_SECTION:
2287 case IMAGE_REL_PPC_SECREL:
2288 DUMP_RELOC2(ppc_coff_howto_table[r_type].name, internal);
2289 howto = ppc_coff_howto_table + r_type;
2291 case IMAGE_REL_PPC_IMGLUE:
2292 DUMP_RELOC2(ppc_coff_howto_table[r_type].name, internal);
2293 howto = ppc_coff_howto_table + r_type;
2295 case IMAGE_REL_PPC_TOCREL16:
2296 DUMP_RELOC2(ppc_coff_howto_table[r_type].name, internal);
2297 if (r_flags & IMAGE_REL_PPC_TOCDEFN)
2298 howto = ppc_coff_howto_table + IMAGE_REL_PPC_TOCREL16_DEFN;
2300 howto = ppc_coff_howto_table + IMAGE_REL_PPC_TOCREL16;
2304 "Warning: Unsupported reloc %s [%d] used -- it may not work.\n",
2305 ppc_coff_howto_table[r_type].name,
2307 howto = ppc_coff_howto_table + r_type;
2311 relent->howto = howto;
2315 static reloc_howto_type *
2316 coff_ppc_rtype_to_howto (abfd, sec, rel, h, sym, addendp)
2319 struct internal_reloc *rel;
2320 struct coff_link_hash_entry *h;
2321 struct internal_syment *sym;
2324 reloc_howto_type *howto;
2326 /* We can encode one of three things in the type field, aside from the
2328 1. IMAGE_REL_PPC_NEG - indicates the value field is a subtraction
2329 value, rather than an addition value
2330 2. IMAGE_REL_PPC_BRTAKEN, IMAGE_REL_PPC_BRNTAKEN - indicates that
2331 the branch is expected to be taken or not.
2332 3. IMAGE_REL_PPC_TOCDEFN - toc slot definition in the file
2333 For now, we just strip this stuff to find the type, and ignore it other
2337 unsigned short r_type = EXTRACT_TYPE (rel->r_type);
2338 unsigned short r_flags = EXTRACT_FLAGS(rel->r_type);
2339 unsigned short junk = EXTRACT_JUNK (rel->r_type);
2341 /* the masking process only slices off the bottom byte for r_type. */
2342 if ( r_type > MAX_RELOC_INDEX )
2345 "coff_ppc_rtype_to_howto: index %d out of range [%d, %d]\n",
2346 r_type, 0, MAX_RELOC_INDEX);
2350 /* check for absolute crap */
2354 "coff_ppc_rtype_to_howto: reloc index %d contains junk %d\n",
2360 /* now examine flags */
2363 fprintf (stderr, "Reloc with flags found!");
2364 if ( r_flags & IMAGE_REL_PPC_NEG )
2365 fprintf (stderr, " NEG");
2366 if ( r_flags & IMAGE_REL_PPC_BRTAKEN )
2367 fprintf (stderr, " BRTAKEN");
2368 if ( r_flags & IMAGE_REL_PPC_BRNTAKEN )
2369 fprintf (stderr, " BRNTAKEN");
2370 if ( r_flags & IMAGE_REL_PPC_TOCDEFN )
2371 fprintf (stderr, " TOCDEFN");
2372 fprintf(stderr, "\n");
2378 case IMAGE_REL_PPC_ADDR32NB:
2379 DUMP_RELOC2(ppc_coff_howto_table[r_type].name, rel);
2380 *addendp -= pe_data(sec->output_section->owner)->pe_opthdr.ImageBase;
2381 howto = ppc_coff_howto_table + r_type;
2383 case IMAGE_REL_PPC_TOCREL16:
2384 DUMP_RELOC2(ppc_coff_howto_table[r_type].name, rel);
2385 if (r_flags & IMAGE_REL_PPC_TOCDEFN)
2386 howto = ppc_coff_howto_table + IMAGE_REL_PPC_TOCREL16_DEFN;
2388 howto = ppc_coff_howto_table + IMAGE_REL_PPC_TOCREL16;
2390 case IMAGE_REL_PPC_ADDR16:
2391 case IMAGE_REL_PPC_REL24:
2392 case IMAGE_REL_PPC_ADDR24:
2393 case IMAGE_REL_PPC_ADDR32:
2394 case IMAGE_REL_PPC_IFGLUE:
2395 case IMAGE_REL_PPC_SECTION:
2396 case IMAGE_REL_PPC_SECREL:
2397 DUMP_RELOC2(ppc_coff_howto_table[r_type].name, rel);
2398 howto = ppc_coff_howto_table + r_type;
2400 case IMAGE_REL_PPC_IMGLUE:
2401 DUMP_RELOC2(ppc_coff_howto_table[r_type].name, rel);
2402 howto = ppc_coff_howto_table + r_type;
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;
2417 /* a cheesy little macro to make the code a little more readable */
2418 #define HOW2MAP(bfd_rtype,ppc_rtype) \
2419 case bfd_rtype: return &ppc_coff_howto_table[ppc_rtype]
2421 static reloc_howto_type *ppc_coff_reloc_type_lookup
2422 PARAMS ((bfd *, bfd_reloc_code_real_type));
2424 static reloc_howto_type *
2425 ppc_coff_reloc_type_lookup (abfd, code)
2427 bfd_reloc_code_real_type code;
2431 fprintf(stderr, "ppc_coff_reloc_type_lookup for %s\n",
2432 bfd_get_reloc_code_name(code));
2437 HOW2MAP(BFD_RELOC_32_GOTOFF, IMAGE_REL_PPC_IMGLUE);
2438 HOW2MAP(BFD_RELOC_16_GOT_PCREL, IMAGE_REL_PPC_IFGLUE);
2439 HOW2MAP(BFD_RELOC_16, IMAGE_REL_PPC_ADDR16);
2440 HOW2MAP(BFD_RELOC_PPC_B26, IMAGE_REL_PPC_REL24);
2441 HOW2MAP(BFD_RELOC_PPC_BA26, IMAGE_REL_PPC_ADDR24);
2442 HOW2MAP(BFD_RELOC_PPC_TOC16, IMAGE_REL_PPC_TOCREL16);
2443 HOW2MAP(BFD_RELOC_16_GOTOFF, IMAGE_REL_PPC_TOCREL16_DEFN);
2444 HOW2MAP(BFD_RELOC_32, IMAGE_REL_PPC_ADDR32);
2445 HOW2MAP(BFD_RELOC_RVA, IMAGE_REL_PPC_ADDR32NB);
2456 /* Tailor coffcode.h -- macro heaven. */
2458 #define RTYPE2HOWTO(cache_ptr, dst) ppc_coff_rtype2howto (cache_ptr, dst)
2460 #ifndef COFF_IMAGE_WITH_PE
2462 ppc_coff_swap_sym_in_hook ();
2465 /* We use the special COFF backend linker, with our own special touch. */
2467 #define coff_bfd_reloc_type_lookup ppc_coff_reloc_type_lookup
2468 #define coff_rtype_to_howto coff_ppc_rtype_to_howto
2469 #define coff_relocate_section coff_ppc_relocate_section
2471 #ifndef COFF_IMAGE_WITH_PE
2472 #define coff_swap_sym_in_hook ppc_coff_swap_sym_in_hook
2475 #define SELECT_RELOC(internal, howto) {internal.r_type=howto->type;}
2477 #define COFF_PAGE_SIZE 0x1000
2479 #define POWERPC_LE_PE
2481 #include "coffcode.h"
2485 #ifndef COFF_IMAGE_WITH_PE
2487 What we're trying to do here is allocate a toc section (early), and attach
2488 it to the last bfd to be processed. This avoids the problem of having a toc
2489 written out before all files have been processed. This code allocates
2490 a toc section for every file, and records the last one seen. There are
2491 at least two problems with this approach:
2492 1. We allocate whole bunches of toc sections that are ignored, but at
2493 at least we will not allocate a toc if no .toc is present.
2494 2. It's not clear to me that being the last bfd read necessarily means
2495 that you are the last bfd closed.
2496 3. Doing it on a "swap in" hook depends on when the "swap in" is called,
2497 and how often, etc. It's not clear to me that there isn't a hole here.
2501 ppc_coff_swap_sym_in_hook (abfd, ext1, in1)
2506 SYMENT *ext = (SYMENT *)ext1;
2507 struct internal_syment *in = (struct internal_syment *)in1;
2510 if (bfd_of_toc_owner != 0) /* we already have a toc, so go home */
2514 if (strcmp(in->_n._n_name, ".toc") == 0)
2517 register asection *s;
2520 s = bfd_get_section_by_name ( abfd , TOC_SECTION_NAME);
2526 flags = SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY ;
2530 "ppc_coff_swap_sym_in_hook: about to create the %s section\n",
2534 s = bfd_make_section (abfd, TOC_SECTION_NAME);
2537 || !bfd_set_section_flags (abfd, s, flags)
2538 || !bfd_set_section_alignment (abfd, s, 2))
2541 "toc section allocation failed!\n");
2545 /* save the bfd for later allocation */
2546 bfd_of_toc_owner = abfd;
2555 /* The transfer vectors that lead the outside world to all of the above. */
2557 #ifdef TARGET_LITTLE_SYM
2561 TARGET_LITTLE_NAME, /* name or coff-arm-little */
2562 bfd_target_coff_flavour,
2563 false, /* data byte order is little */
2564 false, /* header byte order is little */
2566 (HAS_RELOC | EXEC_P | /* FIXME: object flags */
2567 HAS_LINENO | HAS_DEBUG |
2568 HAS_SYMS | HAS_LOCALS | WP_TEXT),
2570 (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* section flags */
2571 0, /* leading char */
2572 '/', /* ar_pad_char */
2573 15, /* ar_max_namelen??? FIXMEmgo */
2575 bfd_getl64, bfd_getl_signed_64, bfd_putl64,
2576 bfd_getl32, bfd_getl_signed_32, bfd_putl32,
2577 bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* data */
2579 bfd_getl64, bfd_getl_signed_64, bfd_putl64,
2580 bfd_getl32, bfd_getl_signed_32, bfd_putl32,
2581 bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* hdrs */
2583 {_bfd_dummy_target, coff_object_p, /* bfd_check_format */
2584 bfd_generic_archive_p, /* _bfd_dummy_target */ coff_object_p },
2585 {bfd_false, coff_mkobject, _bfd_generic_mkarchive, /* bfd_set_format */
2587 {bfd_false, coff_write_object_contents, /* bfd_write_contents */
2588 _bfd_write_archive_contents, bfd_false},
2590 BFD_JUMP_TABLE_GENERIC (coff),
2591 BFD_JUMP_TABLE_COPY (coff),
2592 BFD_JUMP_TABLE_CORE (_bfd_nocore),
2593 BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_coff),
2594 BFD_JUMP_TABLE_SYMBOLS (coff),
2595 BFD_JUMP_TABLE_RELOCS (coff),
2596 BFD_JUMP_TABLE_WRITE (coff),
2597 BFD_JUMP_TABLE_LINK (coff),
2598 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
2604 #ifdef TARGET_BIG_SYM
2609 bfd_target_coff_flavour,
2610 true, /* data byte order is big */
2611 true, /* header byte order is big */
2613 (HAS_RELOC | EXEC_P | /* FIXME: object flags */
2614 HAS_LINENO | HAS_DEBUG |
2615 HAS_SYMS | HAS_LOCALS | WP_TEXT),
2617 (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* section flags */
2618 0, /* leading char */
2619 '/', /* ar_pad_char */
2620 15, /* ar_max_namelen??? FIXMEmgo */
2622 bfd_getb64, bfd_getb_signed_64, bfd_putb64,
2623 bfd_getb32, bfd_getb_signed_32, bfd_putb32,
2624 bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* data */
2626 bfd_getb64, bfd_getb_signed_64, bfd_putb64,
2627 bfd_getb32, bfd_getb_signed_32, bfd_putb32,
2628 bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* hdrs */
2630 {_bfd_dummy_target, coff_object_p, /* bfd_check_format */
2631 bfd_generic_archive_p, /* _bfd_dummy_target */ coff_object_p },
2632 {bfd_false, coff_mkobject, _bfd_generic_mkarchive, /* bfd_set_format */
2634 {bfd_false, coff_write_object_contents, /* bfd_write_contents */
2635 _bfd_write_archive_contents, bfd_false},
2637 BFD_JUMP_TABLE_GENERIC (coff),
2638 BFD_JUMP_TABLE_COPY (coff),
2639 BFD_JUMP_TABLE_CORE (_bfd_nocore),
2640 BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_coff),
2641 BFD_JUMP_TABLE_SYMBOLS (coff),
2642 BFD_JUMP_TABLE_RELOCS (coff),
2643 BFD_JUMP_TABLE_WRITE (coff),
2644 BFD_JUMP_TABLE_LINK (coff),
2645 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),