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 /* In order not to add an int to every hash table item for every coff
54 linker, we define our own hash table, derived from the coff one */
56 /* PE linker hash table entries. */
58 struct ppc_coff_link_hash_entry
60 struct coff_link_hash_entry root
; /* First entry, as required */
62 /* As we wonder around the relocs, we'll keep the assigned toc_offset
64 bfd_vma toc_offset
; /* Our addition, as required */
66 unsigned long int glue_insn
;
70 /* Need a 7 char string for an eye catcher */
73 #define CHECK_EYE(addr) \
74 if (strcmp(addr, EYE) != 0) \
77 "File %s, line %d, Hash check failure, bad eye %8s\n", \
78 __FILE__, __LINE__, addr); \
82 /* PE linker hash table. */
84 struct ppc_coff_link_hash_table
86 struct coff_link_hash_table root
; /* First entry, as required */
89 static struct bfd_hash_entry
*ppc_coff_link_hash_newfunc
90 PARAMS ((struct bfd_hash_entry
*, struct bfd_hash_table
*,
93 /* Routine to create an entry in the link hash table. */
95 static struct bfd_hash_entry
*
96 ppc_coff_link_hash_newfunc (entry
, table
, string
)
97 struct bfd_hash_entry
*entry
;
98 struct bfd_hash_table
*table
;
101 struct ppc_coff_link_hash_entry
*ret
=
102 (struct ppc_coff_link_hash_entry
*) entry
;
104 /* Allocate the structure if it has not already been allocated by a
106 if (ret
== (struct ppc_coff_link_hash_entry
*) NULL
)
107 ret
= (struct ppc_coff_link_hash_entry
*)
108 bfd_hash_allocate (table
,
109 sizeof (struct ppc_coff_link_hash_entry
));
111 if (ret
== (struct ppc_coff_link_hash_entry
*) NULL
)
114 /* Call the allocation method of the superclass. */
115 ret
= ((struct ppc_coff_link_hash_entry
*)
116 _bfd_coff_link_hash_newfunc ((struct bfd_hash_entry
*) ret
,
121 /* Initialize the local fields. */
123 ret
->symbol_is_glue
= 0;
125 strcpy(ret
->eye_catcher
, EYE
);
128 return (struct bfd_hash_entry
*) ret
;
131 /* Initialize a PE linker hash table. */
134 ppc_coff_link_hash_table_init (table
, abfd
, newfunc
)
135 struct ppc_coff_link_hash_table
*table
;
137 struct bfd_hash_entry
*(*newfunc
) PARAMS ((struct bfd_hash_entry
*,
138 struct bfd_hash_table
*,
141 return _bfd_coff_link_hash_table_init (&table
->root
, abfd
, newfunc
);
144 /* Create a PE linker hash table. */
146 static struct bfd_link_hash_table
*
147 ppc_coff_link_hash_table_create (abfd
)
150 struct ppc_coff_link_hash_table
*ret
;
152 ret
= ((struct ppc_coff_link_hash_table
*)
153 bfd_alloc (abfd
, sizeof (struct ppc_coff_link_hash_table
)));
156 if (! ppc_coff_link_hash_table_init (ret
, abfd
,
157 ppc_coff_link_hash_newfunc
))
159 bfd_release (abfd
, ret
);
160 return (struct bfd_link_hash_table
*) NULL
;
162 return &ret
->root
.root
;
165 /* Now, tailor coffcode.h to use our hash stuff */
167 #define coff_bfd_link_hash_table_create ppc_coff_link_hash_table_create
170 /* The nt loader points the toc register to &toc + 32768, in order to */
171 /* use the complete range of a 16-bit displacement (I guess). We have */
172 /* to adjust for this when we fix up loads displaced off the toc reg. */
173 #define TOC_LOAD_ADJUSTMENT (-32768)
174 #define TOC_SECTION_NAME ".private.toc"
176 /* The main body of code is in coffcode.h. */
178 #define COFF_DEFAULT_SECTION_ALIGNMENT_POWER (3)
180 /* In case we're on a 32-bit machine, construct a 64-bit "-1" value
181 from smaller values. Start with zero, widen, *then* decrement. */
182 #define MINUS_ONE (((bfd_vma)0) - 1)
184 /* these should definitely go in a header file somewhere... */
187 #define IMAGE_REL_PPC_ABSOLUTE 0x0000
190 #define IMAGE_REL_PPC_ADDR64 0x0001
193 #define IMAGE_REL_PPC_ADDR32 0x0002
195 /* 26-bit address, shifted left 2 (branch absolute) */
196 #define IMAGE_REL_PPC_ADDR24 0x0003
199 #define IMAGE_REL_PPC_ADDR16 0x0004
201 /* 16-bit address, shifted left 2 (load doubleword) */
202 #define IMAGE_REL_PPC_ADDR14 0x0005
204 /* 26-bit PC-relative offset, shifted left 2 (branch relative) */
205 #define IMAGE_REL_PPC_REL24 0x0006
207 /* 16-bit PC-relative offset, shifted left 2 (br cond relative) */
208 #define IMAGE_REL_PPC_REL14 0x0007
210 /* 16-bit offset from TOC base */
211 #define IMAGE_REL_PPC_TOCREL16 0x0008
213 /* 16-bit offset from TOC base, shifted left 2 (load doubleword) */
214 #define IMAGE_REL_PPC_TOCREL14 0x0009
216 /* 32-bit addr w/o image base */
217 #define IMAGE_REL_PPC_ADDR32NB 0x000A
219 /* va of containing section (as in an image sectionhdr) */
220 #define IMAGE_REL_PPC_SECREL 0x000B
222 /* sectionheader number */
223 #define IMAGE_REL_PPC_SECTION 0x000C
225 /* substitute TOC restore instruction iff symbol is glue code */
226 #define IMAGE_REL_PPC_IFGLUE 0x000D
228 /* symbol is glue code; virtual address is TOC restore instruction */
229 #define IMAGE_REL_PPC_IMGLUE 0x000E
231 /* va of containing section (limited to 16 bits) */
232 #define IMAGE_REL_PPC_SECREL16 0x000F
234 /* stuff to handle immediate data when the number of bits in the */
235 /* data is greater than the number of bits in the immediate field */
236 /* We need to do (usually) 32 bit arithmetic on 16 bit chunks */
237 #define IMAGE_REL_PPC_REFHI 0x0010
238 #define IMAGE_REL_PPC_REFLO 0x0011
239 #define IMAGE_REL_PPC_PAIR 0x0012
242 /* Flag bits in IMAGE_RELOCATION.TYPE */
244 /* subtract reloc value rather than adding it */
245 #define IMAGE_REL_PPC_NEG 0x0100
247 /* fix branch prediction bit to predict branch taken */
248 #define IMAGE_REL_PPC_BRTAKEN 0x0200
250 /* fix branch prediction bit to predict branch not taken */
251 #define IMAGE_REL_PPC_BRNTAKEN 0x0400
253 /* toc slot defined in file (or, data in toc) */
254 #define IMAGE_REL_PPC_TOCDEFN 0x0800
256 /* masks to isolate above values in IMAGE_RELOCATION.Type */
257 #define IMAGE_REL_PPC_TYPEMASK 0x00FF
258 #define IMAGE_REL_PPC_FLAGMASK 0x0F00
260 #define EXTRACT_TYPE(x) ((x) & IMAGE_REL_PPC_TYPEMASK)
261 #define EXTRACT_FLAGS(x) ((x) & IMAGE_REL_PPC_FLAGMASK)
262 #define EXTRACT_JUNK(x) \
263 ((x) & ~(IMAGE_REL_PPC_TYPEMASK | IMAGE_REL_PPC_FLAGMASK))
266 /* static helper functions to make relocation work */
267 /* (Work In Progress) */
269 static bfd_reloc_status_type ppc_refhi_reloc
PARAMS ((bfd
*abfd
,
276 static bfd_reloc_status_type ppc_reflo_reloc
PARAMS ((bfd
*abfd
,
283 static bfd_reloc_status_type ppc_pair_reloc
PARAMS ((bfd
*abfd
,
292 static bfd_reloc_status_type ppc_toc16_reloc
PARAMS ((bfd
*abfd
,
300 static bfd_reloc_status_type ppc_addr32nb_reloc
PARAMS ((bfd
*abfd
,
308 static bfd_reloc_status_type ppc_section_reloc
PARAMS ((bfd
*abfd
,
316 static bfd_reloc_status_type ppc_secrel_reloc
PARAMS ((bfd
*abfd
,
324 static bfd_reloc_status_type ppc_imglue_reloc
PARAMS ((bfd
*abfd
,
334 static boolean in_reloc_p
PARAMS((bfd
*abfd
, reloc_howto_type
*howto
));
337 /* FIXME: It'll take a while to get through all of these. I only need a few to
338 get us started, so those I'll make sure work. Those marked FIXME are either
339 completely unverified or have a specific unknown marked in the comment */
341 /*---------------------------------------------------------------------------*/
343 /* Relocation entries for Windows/NT on PowerPC. */
345 /* From the document "" we find the following listed as used relocs: */
347 /* ABSOLUTE : The noop */
348 /* ADDR[64|32|16] : fields that hold addresses in data fields or the */
349 /* 16 bit displacement field on a load/store. */
350 /* ADDR[24|14] : fields that hold addresses in branch and cond */
351 /* branches. These represent [26|16] bit addresses. */
352 /* The low order 2 bits are preserved. */
353 /* REL[24|14] : branches relative to the Instruction Address */
354 /* register. These represent [26|16] bit addresses, */
355 /* as before. The instruction field will be zero, and */
356 /* the address of the SYM will be inserted at link time. */
357 /* TOCREL16 : 16 bit displacement field referring to a slot in */
359 /* TOCREL14 : 16 bit displacement field, similar to REL14 or ADDR14. */
360 /* ADDR32NB : 32 bit address relative to the virtual origin. */
361 /* (On the alpha, this is always a linker generated thunk)*/
362 /* (i.e. 32bit addr relative to the image base) */
363 /* SECREL : The value is relative to the start of the section */
364 /* containing the symbol. */
365 /* SECTION : access to the header containing the item. Supports the */
366 /* codeview debugger. */
368 /* In particular, note that the document does not indicate that the */
369 /* relocations listed in the header file are used. */
373 /*---------------------------------------------------------------------------*/
375 static reloc_howto_type ppc_coff_howto_table
[] =
377 /* IMAGE_REL_PPC_ABSOLUTE 0x0000 NOP */
379 HOWTO (IMAGE_REL_PPC_ABSOLUTE
, /* type */
381 0, /* size (0 = byte, 1 = short, 2 = long) */
383 false, /* pc_relative */
385 complain_overflow_dont
, /* dont complain_on_overflow */
386 0, /* special_function */
387 "ABSOLUTE", /* name */
388 false, /* partial_inplace */
391 false), /* pcrel_offset */
393 /* IMAGE_REL_PPC_ADDR64 0x0001 64-bit address */
395 HOWTO(IMAGE_REL_PPC_ADDR64
, /* type */
397 3, /* size (0 = byte, 1 = short, 2 = long) */
399 false, /* pc_relative */
401 complain_overflow_bitfield
, /* complain_on_overflow */
402 0, /* special_function */
404 true, /* partial_inplace */
405 MINUS_ONE
, /* src_mask */
406 MINUS_ONE
, /* dst_mask */
407 false), /* pcrel_offset */
409 /* IMAGE_REL_PPC_ADDR32 0x0002 32-bit address */
411 HOWTO (IMAGE_REL_PPC_ADDR32
, /* type */
413 2, /* size (0 = byte, 1 = short, 2 = long) */
415 false, /* pc_relative */
417 complain_overflow_bitfield
, /* complain_on_overflow */
418 0, /* special_function */
420 true, /* partial_inplace */
421 0xffffffff, /* src_mask */
422 0xffffffff, /* dst_mask */
423 false), /* pcrel_offset */
425 /* IMAGE_REL_PPC_ADDR24 0x0003 26-bit address, shifted left 2 (branch absolute) */
426 /* the LI field is in bit 6 through bit 29 is 24 bits, + 2 for the shift */
427 /* Of course, That's the IBM approved bit numbering, which is not what */
428 /* anyone else uses.... The li field is in bit 2 thru 25 */
430 HOWTO (IMAGE_REL_PPC_ADDR24
, /* 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 0x07fffffc, /* src_mask */
441 0x07fffffc, /* dst_mask */
442 false), /* pcrel_offset */
444 /* IMAGE_REL_PPC_ADDR16 0x0004 16-bit address */
446 HOWTO (IMAGE_REL_PPC_ADDR16
, /* type */
448 1, /* size (0 = byte, 1 = short, 2 = long) */
450 false, /* pc_relative */
452 complain_overflow_signed
, /* complain_on_overflow */
453 0, /* special_function */
455 true, /* partial_inplace */
456 0xffff, /* src_mask */
457 0xffff, /* dst_mask */
458 false), /* pcrel_offset */
460 /* IMAGE_REL_PPC_ADDR14 0x0005 */
461 /* 16-bit address, shifted left 2 (load doubleword) */
462 /* FIXME: the mask is likely wrong, and the bit position may be as well */
464 HOWTO (IMAGE_REL_PPC_ADDR14
, /* type */
466 1, /* size (0 = byte, 1 = short, 2 = long) */
468 false, /* pc_relative */
470 complain_overflow_signed
, /* complain_on_overflow */
471 0, /* special_function */
473 true, /* partial_inplace */
474 0xffff, /* src_mask */
475 0xffff, /* dst_mask */
476 false), /* pcrel_offset */
478 /* IMAGE_REL_PPC_REL24 0x0006 */
479 /* 26-bit PC-relative offset, shifted left 2 (branch relative) */
481 HOWTO (IMAGE_REL_PPC_REL24
, /* type */
483 2, /* size (0 = byte, 1 = short, 2 = long) */
485 true, /* pc_relative */
487 complain_overflow_signed
, /* complain_on_overflow */
488 0, /* special_function */
490 true, /* partial_inplace */
491 0x3fffffc, /* src_mask */
492 0x3fffffc, /* dst_mask */
493 false), /* pcrel_offset */
495 /* IMAGE_REL_PPC_REL14 0x0007 */
496 /* 16-bit PC-relative offset, shifted left 2 (br cond relative) */
497 /* FIXME: the mask is likely wrong, and the bit position may be as well */
498 /* FIXME: how does it know how far to shift? */
500 HOWTO (IMAGE_REL_PPC_ADDR14
, /* type */
502 1, /* size (0 = byte, 1 = short, 2 = long) */
504 false, /* pc_relative */
506 complain_overflow_signed
, /* complain_on_overflow */
507 0, /* special_function */
509 true, /* partial_inplace */
510 0xffff, /* src_mask */
511 0xffff, /* dst_mask */
512 true), /* pcrel_offset */
514 /* IMAGE_REL_PPC_TOCREL16 0x0008 */
515 /* 16-bit offset from TOC base */
517 HOWTO (IMAGE_REL_PPC_TOCREL16
,/* type */
519 1, /* size (0 = byte, 1 = short, 2 = long) */
521 false, /* pc_relative */
523 complain_overflow_dont
, /* complain_on_overflow */
524 ppc_toc16_reloc
, /* special_function */
525 "TOCREL16", /* name */
526 false, /* partial_inplace */
527 0xffff, /* src_mask */
528 0xffff, /* dst_mask */
529 false), /* pcrel_offset */
531 /* IMAGE_REL_PPC_TOCREL14 0x0009 */
532 /* 16-bit offset from TOC base, shifted left 2 (load doubleword) */
534 HOWTO (IMAGE_REL_PPC_TOCREL14
,/* type */
536 1, /* size (0 = byte, 1 = short, 2 = long) */
538 false, /* pc_relative */
540 complain_overflow_signed
, /* complain_on_overflow */
541 0, /* special_function */
542 "TOCREL14", /* name */
543 false, /* partial_inplace */
544 0xffff, /* src_mask */
545 0xffff, /* dst_mask */
546 false), /* pcrel_offset */
548 /* IMAGE_REL_PPC_ADDR32NB 0x000A */
549 /* 32-bit addr w/ image base */
551 HOWTO (IMAGE_REL_PPC_ADDR32NB
,/* type */
553 2, /* size (0 = byte, 1 = short, 2 = long) */
555 false, /* pc_relative */
557 complain_overflow_signed
, /* complain_on_overflow */
558 0, /* special_function */
559 "ADDR32NB", /* name */
560 true, /* partial_inplace */
561 0xffffffff, /* src_mask */
562 0xffffffff, /* dst_mask */
563 false), /* pcrel_offset */
565 /* IMAGE_REL_PPC_SECREL 0x000B */
566 /* va of containing section (as in an image sectionhdr) */
568 HOWTO (IMAGE_REL_PPC_SECREL
,/* type */
570 2, /* size (0 = byte, 1 = short, 2 = long) */
572 false, /* pc_relative */
574 complain_overflow_signed
, /* complain_on_overflow */
575 ppc_secrel_reloc
, /* special_function */
577 true, /* partial_inplace */
578 0xffffffff, /* src_mask */
579 0xffffffff, /* dst_mask */
580 true), /* pcrel_offset */
582 /* IMAGE_REL_PPC_SECTION 0x000C */
583 /* sectionheader number */
585 HOWTO (IMAGE_REL_PPC_SECTION
,/* type */
587 2, /* size (0 = byte, 1 = short, 2 = long) */
589 false, /* pc_relative */
591 complain_overflow_signed
, /* complain_on_overflow */
592 ppc_section_reloc
, /* special_function */
593 "SECTION", /* name */
594 true, /* partial_inplace */
595 0xffffffff, /* src_mask */
596 0xffffffff, /* dst_mask */
597 true), /* pcrel_offset */
599 /* IMAGE_REL_PPC_IFGLUE 0x000D */
600 /* substitute TOC restore instruction iff symbol is glue code */
602 HOWTO (IMAGE_REL_PPC_IFGLUE
,/* type */
604 2, /* size (0 = byte, 1 = short, 2 = long) */
606 false, /* pc_relative */
608 complain_overflow_signed
, /* complain_on_overflow */
609 0, /* special_function */
611 true, /* partial_inplace */
612 0xffffffff, /* src_mask */
613 0xffffffff, /* dst_mask */
614 false), /* pcrel_offset */
616 /* IMAGE_REL_PPC_IMGLUE 0x000E */
617 /* symbol is glue code; virtual address is TOC restore instruction */
619 HOWTO (IMAGE_REL_PPC_IMGLUE
,/* type */
621 2, /* size (0 = byte, 1 = short, 2 = long) */
623 false, /* pc_relative */
625 complain_overflow_dont
, /* complain_on_overflow */
626 ppc_imglue_reloc
, /* special_function */
628 false, /* partial_inplace */
629 0xffffffff, /* src_mask */
630 0xffffffff, /* dst_mask */
631 false), /* pcrel_offset */
633 /* IMAGE_REL_PPC_SECREL16 0x000F */
634 /* va of containing section (limited to 16 bits) */
636 HOWTO (IMAGE_REL_PPC_SECREL16
,/* type */
638 1, /* size (0 = byte, 1 = short, 2 = long) */
640 false, /* pc_relative */
642 complain_overflow_signed
, /* complain_on_overflow */
643 0, /* special_function */
644 "SECREL16", /* name */
645 true, /* partial_inplace */
646 0xffff, /* src_mask */
647 0xffff, /* dst_mask */
648 true), /* pcrel_offset */
650 /* IMAGE_REL_PPC_REFHI 0x0010 */
652 HOWTO (IMAGE_REL_PPC_REFHI
, /* type */
654 1, /* size (0 = byte, 1 = short, 2 = long) */
656 false, /* pc_relative */
658 complain_overflow_signed
, /* complain_on_overflow */
659 ppc_refhi_reloc
, /* special_function */
661 true, /* partial_inplace */
662 0xffffffff, /* src_mask */
663 0xffffffff, /* dst_mask */
664 false), /* pcrel_offset */
666 /* IMAGE_REL_PPC_REFLO 0x0011 */
668 HOWTO (IMAGE_REL_PPC_REFLO
, /* type */
670 1, /* size (0 = byte, 1 = short, 2 = long) */
672 false, /* pc_relative */
674 complain_overflow_signed
, /* complain_on_overflow */
675 ppc_refhi_reloc
, /* special_function */
677 true, /* partial_inplace */
678 0xffffffff, /* src_mask */
679 0xffffffff, /* dst_mask */
680 false), /* pcrel_offset */
682 /* IMAGE_REL_PPC_PAIR 0x0012 */
684 HOWTO (IMAGE_REL_PPC_PAIR
, /* type */
686 1, /* size (0 = byte, 1 = short, 2 = long) */
688 false, /* pc_relative */
690 complain_overflow_signed
, /* complain_on_overflow */
691 ppc_pair_reloc
, /* special_function */
693 true, /* partial_inplace */
694 0xffffffff, /* src_mask */
695 0xffffffff, /* dst_mask */
696 false) /* pcrel_offset */
702 /* Some really cheezy macros that can be turned on to test stderr :-) */
711 fprintf(stderr,"Unimplemented Relocation -- %s\n",x); \
715 #define DUMP_RELOC(n,r) \
717 fprintf(stderr,"%s sym %d, addr %d, addend %d\n", \
718 n, (*(r->sym_ptr_ptr))->name, \
719 r->address, r->addend); \
722 /* Given a reloc name, n, and a pointer to an internal_reloc,
723 dump out interesting information on the contents
725 #define n_name _n._n_name
726 #define n_zeroes _n._n_n._n_zeroes
727 #define n_offset _n._n_n._n_offset
731 #define DUMP_RELOC2(n,r) \
733 fprintf(stderr,"%s sym %d, r_vaddr %d %s\n", \
734 n, r->r_symndx, r->r_vaddr,\
735 (((r->r_type) & IMAGE_REL_PPC_TOCDEFN) == 0) \
741 #define DUMP_RELOC(n,r)
742 #define DUMP_RELOC2(n,r)
747 /* toc construction and management routines */
748 extern bfd
* bfd_of_toc_owner
;
749 extern long int global_toc_size
;
751 extern long int import_table_size
;
752 extern long int first_thunk_address
;
753 extern long int thunk_size
;
771 struct list_ele
*next
;
773 enum ref_category cat
;
778 extern struct list_ele
*head
;
779 extern struct list_ele
*tail
;
782 record_toc(toc_section
, our_toc_offset
, cat
, name
)
783 asection
*toc_section
;
785 enum ref_category cat
;
788 /* add this entry to our toc addr-offset-name list */
790 t
= malloc(sizeof(struct list_ele
));
792 t
->offset
= our_toc_offset
;
795 t
->addr
= toc_section
->output_offset
+ our_toc_offset
;
809 /* record a toc offset against a symbol */
811 ppc_record_toc_entry(abfd
, info
, sec
, sym
, toc_kind
)
813 struct bfd_link_info
*info
;
816 enum toc_type toc_kind
;
819 bfd_byte
*old_contents
;
824 struct ppc_coff_link_hash_entry
*h
;
825 struct coff_symbol_struct
*target
;
833 h
= (struct ppc_coff_link_hash_entry
*) (obj_coff_sym_hashes (abfd
)[sym
]);
836 CHECK_EYE(h
->eye_catcher
);
841 local_syms
= obj_coff_local_toc_table(abfd
);
845 /* allocate a table */
847 (int *) bfd_zalloc (abfd
,
848 obj_raw_syment_count(abfd
) * sizeof(int));
851 obj_coff_local_toc_table(abfd
) = local_syms
;
852 for (i
= 0; i
< obj_raw_syment_count(abfd
); ++i
)
856 if (local_syms
[sym
] == 1)
858 local_syms
[sym
] = global_toc_size
;
859 ret_val
= global_toc_size
;
860 global_toc_size
+= 4;
862 /* The size must fit in a 16bit displacment */
863 if (global_toc_size
>= 65535)
866 "Exceeded toc size of 65535\n");
872 "Setting toc_offset for local sym %d to %d\n",
878 ret_val
= local_syms
[sym
];
881 "toc_offset already set for local sym %d to %d\n",
888 name
= h
->root
.root
.root
.string
;
890 /* check to see if there's a toc slot allocated. If not, do it
891 here. It will be used in relocate_section */
892 if (h
->toc_offset
== 1)
894 h
->toc_offset
= global_toc_size
;
895 ret_val
= global_toc_size
;
896 global_toc_size
+= 4;
898 /* The size must fit in a 16bit displacment */
899 if (global_toc_size
>= 65535)
902 "Exceeded toc size of 65535\n");
908 "Setting toc_offset for sym %d (%s) [h=%p] to %d\n",
909 sym
, name
, h
, ret_val
);
914 ret_val
= h
->toc_offset
;
917 "toc_offset already set for sym %d (%s) [h=%p] to %d\n",
918 sym
, name
, h
, ret_val
);
925 /* FIXME: record a toc offset against a data-in-toc symbol */
926 /* Now, there is currenly some confusion on what this means. In some
927 compilers one sees the moral equivalent of:
931 refer to the data with a [tocv] qualifier
932 In general, one sees something to indicate that a tocd has been
933 seen, and that would trigger the allocation of data in toc. The IBM
934 docs seem to suggest that anything with the TOCDEFN qualifier should
935 never trigger storage allocation. However, in the kernel32.lib that
936 we've been using for our test bed, there are a couple of variables
937 referenced that fail that test.
939 So it can't work that way.
942 ppc_record_data_in_toc_entry(abfd
, info
, sec
, sym
, toc_kind
)
944 struct bfd_link_info
*info
;
947 enum toc_type toc_kind
;
950 bfd_byte
*old_contents
;
955 struct ppc_coff_link_hash_entry
*h
= 0;
956 struct coff_symbol_struct
*target
;
962 h
= (struct ppc_coff_link_hash_entry
*) (obj_coff_sym_hashes (abfd
)[sym
]);
966 local_syms
= obj_coff_local_toc_table(abfd
);
970 /* allocate a table */
972 (int *) bfd_zalloc (abfd
,
973 obj_raw_syment_count(abfd
) * sizeof(int));
976 obj_coff_local_toc_table(abfd
) = local_syms
;
977 for (i
= 0; i
< obj_raw_syment_count(abfd
); ++i
)
981 if (local_syms
[sym
] == 1)
983 local_syms
[sym
] = global_toc_size
;
984 ret_val
= global_toc_size
;
985 global_toc_size
+= 4;
988 "Setting data_in_toc_offset for local sym %d to %d\n",
994 ret_val
= local_syms
[sym
];
997 "data_in_toc_offset already set for local sym %d to %d\n",
1004 CHECK_EYE(h
->eye_catcher
);
1006 name
= h
->root
.root
.root
.string
;
1008 /* check to see if there's a toc slot allocated. If not, do it
1009 here. It will be used in relocate_section */
1010 if (h
->toc_offset
== 1)
1013 h
->toc_offset
= global_toc_size
;
1015 ret_val
= global_toc_size
;
1016 /* We're allocating a chunk of the toc, as opposed to a slot */
1017 /* FIXME: alignment? */
1019 global_toc_size
+= 4;
1022 "Setting data_in_toc_offset for sym %d (%s) [h=%p] to %d\n",
1023 sym
, name
, h
, ret_val
);
1028 ret_val
= h
->toc_offset
;
1031 "data_in_toc_offset already set for sym %d (%s) [h=%p] to %d\n",
1032 sym
, name
, h
, ret_val
);
1040 /* record a toc offset against a symbol */
1042 ppc_mark_symbol_as_glue(abfd
, sym
, rel
)
1045 struct internal_reloc
*rel
;
1047 struct ppc_coff_link_hash_entry
*h
;
1049 h
= (struct ppc_coff_link_hash_entry
*) (obj_coff_sym_hashes (abfd
)[sym
]);
1051 CHECK_EYE(h
->eye_catcher
);
1053 h
->symbol_is_glue
= 1;
1054 h
->glue_insn
= bfd_get_32 (abfd
, (bfd_byte
*) &rel
->r_vaddr
);
1060 /* Provided the symbol, returns the value reffed */
1061 static long get_symbol_value
PARAMS ((asymbol
*));
1064 get_symbol_value (symbol
)
1067 long relocation
= 0;
1069 if (bfd_is_com_section (symbol
->section
))
1075 relocation
= symbol
->value
+
1076 symbol
->section
->output_section
->vma
+
1077 symbol
->section
->output_offset
;
1083 /* Return true if this relocation should
1084 appear in the output .reloc section. */
1086 static boolean
in_reloc_p(abfd
, howto
)
1088 reloc_howto_type
*howto
;
1091 (! howto
->pc_relative
)
1092 && (howto
->type
!= IMAGE_REL_PPC_TOCREL16
)
1093 && (howto
->type
!= IMAGE_REL_PPC_IMGLUE
);
1096 /* this function is in charge of performing all the ppc PE relocations */
1097 /* Don't yet know if we want to do this this particular way ... (krk) */
1098 /* FIXME: (it is not yet enabled) */
1100 static bfd_reloc_status_type
1101 pe_ppc_reloc (abfd
, reloc_entry
, symbol_in
, data
, input_section
, output_bfd
,
1104 arelent
*reloc_entry
;
1107 asection
*input_section
;
1109 char **error_message
;
1111 /* the consth relocation comes in two parts, we have to remember
1112 the state between calls, in these variables */
1113 static boolean part1_consth_active
= false;
1114 static unsigned long part1_consth_value
;
1117 unsigned long sym_value
;
1118 unsigned long unsigned_value
;
1119 unsigned short r_type
;
1122 unsigned long addr
= reloc_entry
->address
; /*+ input_section->vma*/
1123 bfd_byte
*hit_data
=addr
+ (bfd_byte
*)(data
);
1125 fprintf(stderr
, "pe_ppc_reloc (%s)\n", TARGET_LITTLE_NAME
);
1127 r_type
= reloc_entry
->howto
->type
;
1131 /* Partial linking - do nothing */
1132 reloc_entry
->address
+= input_section
->output_offset
;
1133 return bfd_reloc_ok
;
1136 if (symbol_in
!= NULL
1137 && bfd_is_und_section (symbol_in
->section
))
1139 /* Keep the state machine happy in case we're called again */
1140 if (r_type
== IMAGE_REL_PPC_REFHI
)
1142 part1_consth_active
= true;
1143 part1_consth_value
= 0;
1145 return(bfd_reloc_undefined
);
1148 if ((part1_consth_active
) && (r_type
!= IMAGE_REL_PPC_PAIR
))
1150 part1_consth_active
= false;
1151 *error_message
= (char *) "Missing PAIR";
1152 return(bfd_reloc_dangerous
);
1156 sym_value
= get_symbol_value(symbol_in
);
1158 return(bfd_reloc_ok
);
1161 /* The reloc processing routine for the optimized COFF linker. */
1164 coff_ppc_relocate_section (output_bfd
, info
, input_bfd
, input_section
,
1165 contents
, relocs
, syms
, sections
)
1167 struct bfd_link_info
*info
;
1169 asection
*input_section
;
1171 struct internal_reloc
*relocs
;
1172 struct internal_syment
*syms
;
1173 asection
**sections
;
1175 struct internal_reloc
*rel
;
1176 struct internal_reloc
*relend
;
1179 asection
*toc_section
= 0;
1181 reloc_howto_type
*howto
= 0;
1185 "pe_ppc_relocate_section (%s) for %s \n",
1187 input_section
->name
);
1191 /* If we are performing a relocateable link, we don't need to do a
1192 thing. The caller will take care of adjusting the reloc
1193 addresses and symbol indices. */
1194 if (info
->relocateable
)
1201 relend
= rel
+ input_section
->reloc_count
;
1202 for (; rel
< relend
; rel
++)
1205 struct ppc_coff_link_hash_entry
*h
;
1206 struct internal_syment
*sym
;
1210 bfd_reloc_status_type rstat
;
1213 unsigned short r_type
= EXTRACT_TYPE (rel
->r_type
);
1214 unsigned short r_flags
= EXTRACT_FLAGS(rel
->r_type
);
1215 unsigned short junk
= EXTRACT_JUNK (rel
->r_type
);
1218 /* now examine flags */
1221 fprintf (stderr
, "Reloc with flags found!");
1222 if ( r_flags
& IMAGE_REL_PPC_NEG
)
1223 fprintf (stderr
, " NEG");
1224 if ( r_flags
& IMAGE_REL_PPC_BRTAKEN
)
1225 fprintf (stderr
, " BRTAKEN");
1226 if ( r_flags
& IMAGE_REL_PPC_BRNTAKEN
)
1227 fprintf (stderr
, " BRNTAKEN");
1228 if ( r_flags
& IMAGE_REL_PPC_TOCDEFN
)
1229 fprintf (stderr
, " TOCDEFN");
1230 fprintf(stderr
, "\n");
1234 symndx
= rel
->r_symndx
;
1235 loc
= contents
+ rel
->r_vaddr
- input_section
->vma
;
1237 /* FIXME: check bounds on r_type */
1238 howto
= ppc_coff_howto_table
+ r_type
;
1247 h
= (struct ppc_coff_link_hash_entry
*)
1248 (obj_coff_sym_hashes (input_bfd
)[symndx
]);
1251 CHECK_EYE(h
->eye_catcher
);
1254 sym
= syms
+ symndx
;
1260 /* FIXME: PAIR unsupported in the following code */
1264 sec
= bfd_abs_section_ptr
;
1267 sec
= sections
[symndx
];
1268 val
= (sec
->output_section
->vma
1269 + sec
->output_offset
1276 CHECK_EYE(h
->eye_catcher
);
1278 if (h
->root
.root
.type
== bfd_link_hash_defined
1279 || h
->root
.root
.type
== bfd_link_hash_defweak
)
1281 sec
= h
->root
.root
.u
.def
.section
;
1282 val
= (h
->root
.root
.u
.def
.value
1283 + sec
->output_section
->vma
1284 + sec
->output_offset
);
1288 if (! ((*info
->callbacks
->undefined_symbol
)
1289 (info
, h
->root
.root
.root
.string
, input_bfd
, input_section
,
1290 rel
->r_vaddr
- input_section
->vma
)))
1295 rstat
= bfd_reloc_ok
;
1297 /* Each case must do its own relocation, setting rstat appropriately */
1302 "ERROR: during reloc processing -- unsupported reloc %s\n",
1304 bfd_set_error (bfd_error_bad_value
);
1307 case IMAGE_REL_PPC_TOCREL16
:
1309 bfd_vma our_toc_offset
;
1312 DUMP_RELOC2(howto
->name
, rel
);
1314 if (toc_section
== 0)
1316 toc_section
= bfd_get_section_by_name (bfd_of_toc_owner
,
1321 "BFD of toc owner %p, section addr of %s %p\n",
1322 bfd_of_toc_owner
, TOC_SECTION_NAME
, toc_section
);
1325 if ( toc_section
== NULL
)
1327 fprintf(stderr
, "No Toc section!\n");
1333 * Amazing bit tricks present. As we may have seen earlier, we
1334 * use the 1 bit to tell us whether or not a toc offset has been
1335 * allocated. Now that they've all been allocated, we will use
1336 * the 1 bit to tell us if we've written this particular toc
1341 { /* it is a file local symbol */
1342 int *local_toc_table
;
1345 sym
= syms
+ symndx
;
1346 name
= sym
->_n
._n_name
;
1348 local_toc_table
= obj_coff_local_toc_table(input_bfd
);
1349 our_toc_offset
= local_toc_table
[symndx
];
1351 if ((our_toc_offset
& 1) != 0)
1353 /* if it has been written out, it is marked with the
1354 1 bit. Fix up our offset, but do not write it out
1357 our_toc_offset
&= ~1;
1361 "Not writing out toc_offset of %d for %s\n",
1362 our_toc_offset
, name
);
1367 /* write out the toc entry */
1368 record_toc(toc_section
, our_toc_offset
, priv
, strdup(name
));
1371 "Writing out toc_offset "
1372 "toc_section (%p,%p)+%d val %d for %s\n",
1374 toc_section
->contents
,
1380 bfd_put_32(output_bfd
,
1382 toc_section
->contents
+ our_toc_offset
);
1384 local_toc_table
[symndx
] |= 1;
1390 const char *name
= h
->root
.root
.root
.string
;
1391 our_toc_offset
= h
->toc_offset
;
1393 if ((r_flags
& IMAGE_REL_PPC_TOCDEFN
)
1394 == IMAGE_REL_PPC_TOCDEFN
1395 && our_toc_offset
== 1)
1397 /* This is unbelievable cheese. Some knowledgable asm
1398 hacker has decided to use r2 as a base for loading
1399 a value. He/She does this by setting the tocdefn bit,
1400 and not supplying a toc definition. The behaviour is
1401 then to use the difference between the value of the
1402 symbol and the actual location of the toc as the toc
1405 In fact, what is usually happening is, because the
1406 Import Address Table is mapped immediately following
1407 the toc, some trippy library code trying for speed on
1408 dll linkage, takes advantage of that and considers
1409 the IAT to be part of the toc, thus saving a load.
1411 our_toc_offset
= val
-
1412 (toc_section
->output_section
->vma
+
1413 toc_section
->output_offset
);
1415 /* The size must still fit in a 16bit displacment */
1416 if (our_toc_offset
>= 65535)
1419 "TOCDEFN Relocation exceeded "
1420 "displacment of 65535\n");
1424 record_toc(toc_section
, our_toc_offset
, pub
, strdup(name
));
1426 else if ((our_toc_offset
& 1) != 0)
1428 /* if it has been written out, it is marked with the
1429 1 bit. Fix up our offset, but do not write it out
1432 our_toc_offset
&= ~1;
1435 "Not writing out toc_offset of %d for %s\n",
1436 our_toc_offset
, name
);
1441 record_toc(toc_section
, our_toc_offset
, pub
, strdup(name
));
1444 /* write out the toc entry */
1446 "Writing out toc_offset "
1447 "toc_section (%p,%p)+%d val %d for %s\n",
1449 toc_section
->contents
,
1455 /* write out the toc entry */
1456 bfd_put_32(output_bfd
,
1458 toc_section
->contents
+ our_toc_offset
);
1461 /* The tricky part is that this is the address that */
1462 /* needs a .reloc entry for it */
1467 if (fixit
&& info
->base_file
)
1469 /* So if this is non pcrelative, and is referenced
1470 to a section or a common symbol, then it needs a reloc */
1472 /* relocation to a symbol in a section which
1473 isn't absolute - we output the address here
1476 bfd_vma addr
= toc_section
->output_section
->vma
1477 + toc_section
->output_offset
+ our_toc_offset
;
1480 " Toc Section reloc candidate\n");
1482 if (coff_data(output_bfd
)->pe
)
1483 addr
-= pe_data(output_bfd
)->pe_opthdr
.ImageBase
;
1484 fwrite (&addr
, 1,4, (FILE *) info
->base_file
);
1488 /* FIXME: this test is conservative */
1489 if ( (r_flags
& IMAGE_REL_PPC_TOCDEFN
) != IMAGE_REL_PPC_TOCDEFN
&&
1490 our_toc_offset
> toc_section
->_raw_size
)
1493 "reloc offset is bigger than the toc size!\n");
1497 /* Now we know the relocation for this toc reference */
1498 relocation
= our_toc_offset
+ TOC_LOAD_ADJUSTMENT
;
1499 rstat
= _bfd_relocate_contents (howto
,
1505 case IMAGE_REL_PPC_IFGLUE
:
1507 /* To solve this, we need to know whether or not the symbol */
1508 /* appearing on the call instruction is a glue function or not. */
1509 /* A glue function must announce itself via a IMGLUE reloc, and */
1510 /* the reloc contains the required toc restore instruction */
1513 const char *my_name
;
1514 DUMP_RELOC2(howto
->name
, rel
);
1518 my_name
= h
->root
.root
.root
.string
;
1519 if (h
->symbol_is_glue
== 1)
1521 x
= bfd_get_32(input_bfd
, loc
);
1522 bfd_put_32(input_bfd
, h
->glue_insn
, loc
);
1527 case IMAGE_REL_PPC_SECREL
:
1528 /* Unimplemented: codeview debugging information */
1529 /* For fast access to the header of the section
1530 containing the item. */
1532 case IMAGE_REL_PPC_SECTION
:
1533 /* Unimplemented: codeview debugging information */
1534 /* Is used to indicate that the value should be relative
1535 to the beginning of the section that contains the
1538 case IMAGE_REL_PPC_ABSOLUTE
:
1540 const char *my_name
;
1542 my_name
= (syms
+symndx
)->_n
._n_name
;
1545 my_name
= h
->root
.root
.root
.string
;
1549 "Warning: unsupported reloc %s <file %s, section %s>\n",
1551 bfd_get_filename(input_bfd
),
1552 input_section
->name
);
1554 fprintf(stderr
,"sym %d (%s), r_vaddr %d (%x)\n",
1555 rel
->r_symndx
, my_name
, rel
->r_vaddr
, rel
->r_vaddr
);
1558 case IMAGE_REL_PPC_IMGLUE
:
1560 /* There is nothing to do now. This reloc was noted in the first
1561 pass over the relocs, and the glue instruction extracted */
1562 const char *my_name
;
1563 if (h
->symbol_is_glue
== 1)
1565 my_name
= h
->root
.root
.root
.string
;
1567 "Warning: previously missed IMGLUE reloc %s <file %s, section %s>\n",
1569 bfd_get_filename(input_bfd
),
1570 input_section
->name
);
1576 case IMAGE_REL_PPC_ADDR32NB
:
1578 struct coff_link_hash_entry
*myh
= 0;
1579 const char *name
= 0;
1580 DUMP_RELOC2(howto
->name
, rel
);
1582 { /* it is a file local symbol */
1583 sym
= syms
+ symndx
;
1584 name
= sym
->_n
._n_name
;
1590 name
= h
->root
.root
.root
.string
;
1591 if (strcmp(".idata$2", name
) == 0)
1592 target
= "__idata2_magic__";
1593 else if (strcmp(".idata$4", name
) == 0)
1594 target
= "__idata4_magic__";
1595 else if (strcmp(".idata$5", name
) == 0)
1596 target
= "__idata5_magic__";
1602 myh
= coff_link_hash_lookup (coff_hash_table (info
),
1604 false, false, true);
1607 fprintf(stderr
, "Missing idata magic cookies, "
1608 "this cannot work anyway...\n");
1612 val
= myh
->root
.u
.def
.value
+
1613 sec
->output_section
->vma
+ sec
->output_offset
;
1614 if (first_thunk_address
== 0)
1617 myh
= coff_link_hash_lookup (coff_hash_table (info
),
1619 false, false, true);
1620 first_thunk_address
= myh
->root
.u
.def
.value
+
1621 sec
->output_section
->vma
+
1622 sec
->output_offset
-
1623 pe_data(output_bfd
)->pe_opthdr
.ImageBase
;
1625 idata5offset
= myh
->root
.u
.def
.value
;
1626 myh
= coff_link_hash_lookup (coff_hash_table (info
),
1628 false, false, true);
1630 thunk_size
= myh
->root
.u
.def
.value
- idata5offset
;
1631 myh
= coff_link_hash_lookup (coff_hash_table (info
),
1633 false, false, true);
1634 import_table_size
= myh
->root
.u
.def
.value
;
1638 rstat
= _bfd_relocate_contents (howto
,
1641 pe_data(output_bfd
)->pe_opthdr
.ImageBase
,
1646 case IMAGE_REL_PPC_REL24
:
1647 DUMP_RELOC2(howto
->name
, rel
);
1648 val
-= (input_section
->output_section
->vma
1649 + input_section
->output_offset
);
1651 rstat
= _bfd_relocate_contents (howto
,
1656 case IMAGE_REL_PPC_ADDR16
:
1657 case IMAGE_REL_PPC_ADDR24
:
1658 case IMAGE_REL_PPC_ADDR32
:
1659 DUMP_RELOC2(howto
->name
, rel
);
1660 rstat
= _bfd_relocate_contents (howto
,
1667 if ( info
->base_file
)
1669 /* So if this is non pcrelative, and is referenced
1670 to a section or a common symbol, then it needs a reloc */
1671 if (sym
&& pe_data(output_bfd
)->in_reloc_p(output_bfd
, howto
))
1673 /* relocation to a symbol in a section which
1674 isn't absolute - we output the address here
1676 bfd_vma addr
= rel
->r_vaddr
1677 - input_section
->vma
1678 + input_section
->output_offset
1679 + input_section
->output_section
->vma
;
1681 if (coff_data(output_bfd
)->pe
)
1683 addr
-= pe_data(output_bfd
)->pe_opthdr
.ImageBase
;
1685 " adjusted down to %d", addr
);
1687 fprintf(stderr
, "\n");
1689 fwrite (&addr
, 1,4, (FILE *) info
->base_file
);
1699 case bfd_reloc_overflow
:
1702 char buf
[SYMNMLEN
+ 1];
1707 name
= h
->root
.root
.root
.string
;
1708 else if (sym
== NULL
)
1710 else if (sym
->_n
._n_n
._n_zeroes
== 0
1711 && sym
->_n
._n_n
._n_offset
!= 0)
1712 name
= obj_coff_strings (input_bfd
) + sym
->_n
._n_n
._n_offset
;
1715 strncpy (buf
, sym
->_n
._n_name
, SYMNMLEN
);
1716 buf
[SYMNMLEN
] = '\0';
1722 name
= _bfd_coff_internal_syment_name (input_bfd
, sym
, buf
);
1728 if (! ((*info
->callbacks
->reloc_overflow
)
1729 (info
, name
, howto
->name
,
1730 (bfd_vma
) 0, input_bfd
,
1731 input_section
, rel
->r_vaddr
- input_section
->vma
)))
1741 #ifdef COFF_IMAGE_WITH_PE
1743 long int global_toc_size
= 0;
1745 bfd
* bfd_of_toc_owner
= 0;
1747 long int import_table_size
;
1748 long int first_thunk_address
;
1749 long int thunk_size
;
1751 struct list_ele
*head
;
1752 struct list_ele
*tail
;
1755 h1
= "\n\t\t\tTOC MAPPING\n\n";
1757 h2
= " TOC disassembly Comments Name\n";
1759 h3
= " Offset spelling (if present)\n";
1772 for(t
= head
; t
!= 0; t
=t
->next
)
1778 else if (t
->cat
== pub
)
1780 else if (t
->cat
== data
)
1781 cat
= "data-in-toc ";
1783 if (t
->offset
> global_toc_size
)
1785 if (t
->offset
<= global_toc_size
+ thunk_size
)
1786 cat
= "IAT reference ";
1788 cat
= "Out of bounds!";
1792 " %04lx (%d)", t
->offset
, t
->offset
- 32768);
1799 fprintf(file
, "\n");
1803 ppc_allocate_toc_section (info
)
1804 struct bfd_link_info
*info
;
1808 static char test_char
= '1';
1810 if ( global_toc_size
== 0 ) /* FIXME: does this get me in trouble? */
1813 if (bfd_of_toc_owner
== 0)
1816 "There is no bfd that owns the toc section!\n");
1820 s
= bfd_get_section_by_name ( bfd_of_toc_owner
, TOC_SECTION_NAME
);
1823 fprintf(stderr
, "No Toc section!\n");
1827 foo
= bfd_alloc(bfd_of_toc_owner
, global_toc_size
);
1828 memset(foo
, test_char
, global_toc_size
);
1830 s
->_raw_size
= s
->_cooked_size
= global_toc_size
;
1837 ppc_process_before_allocation (abfd
, info
)
1839 struct bfd_link_info
*info
;
1842 struct internal_reloc
*i
, *rel
;
1846 "ppc_process_before_allocation: BFD %s\n",
1847 bfd_get_filename(abfd
));
1850 /* here we have a bfd that is to be included on the link. We have a hook
1851 to do reloc rummaging, before section sizes are nailed down. */
1853 _bfd_coff_get_external_symbols(abfd
);
1855 /* rummage around all the relocs and map the toc */
1856 sec
= abfd
->sections
;
1863 for (; sec
!= 0; sec
= sec
->next
)
1869 " section %s reloc count %d\n",
1874 if (sec
->reloc_count
== 0)
1877 /* load the relocs */
1878 /* FIXME: there may be a storage leak here */
1879 i
=_bfd_coff_read_internal_relocs(abfd
,sec
,1,0,0,0);
1884 for (rel
=i
;rel
<i
+sec
->reloc_count
;++rel
)
1886 unsigned short r_type
= EXTRACT_TYPE (rel
->r_type
);
1887 unsigned short r_flags
= EXTRACT_FLAGS(rel
->r_type
);
1888 unsigned short junk
= EXTRACT_JUNK (rel
->r_type
);
1891 /* now examine flags */
1894 fprintf (stderr
, "Reloc with flags found!");
1895 if ( r_flags
& IMAGE_REL_PPC_NEG
)
1896 fprintf (stderr
, " NEG");
1897 if ( r_flags
& IMAGE_REL_PPC_BRTAKEN
)
1898 fprintf (stderr
, " BRTAKEN");
1899 if ( r_flags
& IMAGE_REL_PPC_BRNTAKEN
)
1900 fprintf (stderr
, " BRNTAKEN");
1901 if ( r_flags
& IMAGE_REL_PPC_TOCDEFN
)
1902 fprintf (stderr
, " TOCDEFN");
1903 fprintf(stderr
, "\n");
1907 DUMP_RELOC2(ppc_coff_howto_table
[r_type
].name
, rel
);
1911 case IMAGE_REL_PPC_TOCREL16
:
1914 This remains unimplemented for now, as it currently adds
1915 un-necessary elements to the toc. All we need to do today
1916 is not do anything if TOCDEFN is on.
1918 if ( r_flags
& IMAGE_REL_PPC_TOCDEFN
)
1919 toc_offset
= ppc_record_data_in_toc_entry(abfd
, info
, sec
,
1923 toc_offset
= ppc_record_toc_entry(abfd
, info
, sec
,
1924 rel
->r_symndx
, default_toc
);
1926 if ( (r_flags
& IMAGE_REL_PPC_TOCDEFN
) != IMAGE_REL_PPC_TOCDEFN
)
1927 toc_offset
= ppc_record_toc_entry(abfd
, info
, sec
,
1928 rel
->r_symndx
, default_toc
);
1930 case IMAGE_REL_PPC_IMGLUE
:
1931 ppc_mark_symbol_as_glue(abfd
, rel
->r_symndx
, rel
);
1943 static bfd_reloc_status_type
1944 ppc_refhi_reloc (abfd
,
1952 arelent
*reloc_entry
;
1955 asection
*input_section
;
1957 char **error_message
;
1960 DUMP_RELOC("REFHI",reloc_entry
);
1962 if (output_bfd
== (bfd
*) NULL
)
1963 return bfd_reloc_continue
;
1965 return bfd_reloc_undefined
;
1968 static bfd_reloc_status_type
1969 ppc_reflo_reloc (abfd
,
1977 arelent
*reloc_entry
;
1980 asection
*input_section
;
1982 char **error_message
;
1985 DUMP_RELOC("REFLO",reloc_entry
);
1987 if (output_bfd
== (bfd
*) NULL
)
1988 return bfd_reloc_continue
;
1990 return bfd_reloc_undefined
;
1993 static bfd_reloc_status_type
1994 ppc_pair_reloc (abfd
,
2002 arelent
*reloc_entry
;
2005 asection
*input_section
;
2007 char **error_message
;
2010 DUMP_RELOC("PAIR",reloc_entry
);
2012 if (output_bfd
== (bfd
*) NULL
)
2013 return bfd_reloc_continue
;
2015 return bfd_reloc_undefined
;
2019 static bfd_reloc_status_type
2020 ppc_toc16_reloc (abfd
,
2028 arelent
*reloc_entry
;
2031 asection
*input_section
;
2033 char **error_message
;
2035 UN_IMPL("TOCREL16");
2036 DUMP_RELOC("TOCREL16",reloc_entry
);
2038 if (output_bfd
== (bfd
*) NULL
)
2040 return bfd_reloc_continue
;
2043 return bfd_reloc_ok
;
2046 /* ADDR32NB : 32 bit address relative to the virtual origin. */
2047 /* (On the alpha, this is always a linker generated thunk)*/
2048 /* (i.e. 32bit addr relative to the image base) */
2052 static bfd_reloc_status_type
2053 ppc_addr32nb_reloc (abfd
,
2061 arelent
*reloc_entry
;
2064 asection
*input_section
;
2066 char **error_message
;
2068 UN_IMPL("ADDR32NB");
2069 DUMP_RELOC("ADDR32NB",reloc_entry
);
2071 return bfd_reloc_ok
;
2074 static bfd_reloc_status_type
2075 ppc_secrel_reloc (abfd
,
2083 arelent
*reloc_entry
;
2086 asection
*input_section
;
2088 char **error_message
;
2091 DUMP_RELOC("SECREL",reloc_entry
);
2093 if (output_bfd
== (bfd
*) NULL
)
2094 return bfd_reloc_continue
;
2096 return bfd_reloc_ok
;
2099 static bfd_reloc_status_type
2100 ppc_section_reloc (abfd
,
2108 arelent
*reloc_entry
;
2111 asection
*input_section
;
2113 char **error_message
;
2116 DUMP_RELOC("SECTION",reloc_entry
);
2118 if (output_bfd
== (bfd
*) NULL
)
2119 return bfd_reloc_continue
;
2121 return bfd_reloc_ok
;
2124 static bfd_reloc_status_type
2125 ppc_imglue_reloc (abfd
,
2133 arelent
*reloc_entry
;
2136 asection
*input_section
;
2138 char **error_message
;
2141 DUMP_RELOC("IMGLUE",reloc_entry
);
2143 if (output_bfd
== (bfd
*) NULL
)
2144 return bfd_reloc_continue
;
2146 return bfd_reloc_ok
;
2151 #define MAX_RELOC_INDEX \
2152 (sizeof(ppc_coff_howto_table) / sizeof(ppc_coff_howto_table[0]) - 1)
2155 /* FIXME: There is a possiblity that when we read in a reloc from a file,
2156 that there are some bits encoded in the upper portion of the
2157 type field. Not yet implemented.
2159 static void ppc_coff_rtype2howto
PARAMS ((arelent
*relent
,
2160 struct internal_reloc
*internal
));
2163 ppc_coff_rtype2howto (relent
, internal
)
2165 struct internal_reloc
*internal
;
2168 /* We can encode one of three things in the type field, aside from the
2170 1. IMAGE_REL_PPC_NEG - indicates the value field is a subtraction
2171 value, rather than an addition value
2172 2. IMAGE_REL_PPC_BRTAKEN, IMAGE_REL_PPC_BRNTAKEN - indicates that
2173 the branch is expected to be taken or not.
2174 3. IMAGE_REL_PPC_TOCDEFN - toc slot definition in the file
2175 For now, we just strip this stuff to find the type, and ignore it other
2179 unsigned short r_type
= EXTRACT_TYPE (internal
->r_type
);
2180 unsigned short r_flags
= EXTRACT_FLAGS(internal
->r_type
);
2181 unsigned short junk
= EXTRACT_JUNK (internal
->r_type
);
2183 /* the masking process only slices off the bottom byte for r_type. */
2184 if ( r_type
> MAX_RELOC_INDEX
)
2187 "ppc_coff_rtype2howto: reloc index %d out of range [%d, %d]\n",
2188 internal
->r_type
, 0, MAX_RELOC_INDEX
);
2192 /* check for absolute crap */
2196 "ppc_coff_rtype2howto: reloc index %d contains junk %d\n",
2197 internal
->r_type
, junk
);
2202 /* now examine flags */
2205 fprintf (stderr
, "Reloc with flags found!");
2206 if ( r_flags
& IMAGE_REL_PPC_NEG
)
2207 fprintf (stderr
, " NEG");
2208 if ( r_flags
& IMAGE_REL_PPC_BRTAKEN
)
2209 fprintf (stderr
, " BRTAKEN");
2210 if ( r_flags
& IMAGE_REL_PPC_BRNTAKEN
)
2211 fprintf (stderr
, " BRNTAKEN");
2212 if ( r_flags
& IMAGE_REL_PPC_TOCDEFN
)
2213 fprintf (stderr
, " TOCDEFN");
2214 fprintf(stderr
, "\n");
2220 case IMAGE_REL_PPC_ADDR16
:
2221 case IMAGE_REL_PPC_REL24
:
2222 case IMAGE_REL_PPC_ADDR24
:
2223 case IMAGE_REL_PPC_TOCREL16
:
2224 case IMAGE_REL_PPC_ADDR32
:
2225 case IMAGE_REL_PPC_IFGLUE
:
2226 case IMAGE_REL_PPC_ADDR32NB
:
2227 case IMAGE_REL_PPC_SECTION
:
2228 case IMAGE_REL_PPC_SECREL
:
2229 DUMP_RELOC2(ppc_coff_howto_table
[r_type
].name
, internal
);
2231 case IMAGE_REL_PPC_IMGLUE
:
2232 DUMP_RELOC2(ppc_coff_howto_table
[r_type
].name
, internal
);
2236 "Warning: Unsupported reloc %s [%d] used -- it may not work.\n",
2237 ppc_coff_howto_table
[r_type
].name
,
2242 relent
->howto
= ppc_coff_howto_table
+ r_type
;
2246 static reloc_howto_type
*
2247 coff_ppc_rtype_to_howto (abfd
, sec
, rel
, h
, sym
, addendp
)
2250 struct internal_reloc
*rel
;
2251 struct coff_link_hash_entry
*h
;
2252 struct internal_syment
*sym
;
2255 reloc_howto_type
*howto
;
2257 /* We can encode one of three things in the type field, aside from the
2259 1. IMAGE_REL_PPC_NEG - indicates the value field is a subtraction
2260 value, rather than an addition value
2261 2. IMAGE_REL_PPC_BRTAKEN, IMAGE_REL_PPC_BRNTAKEN - indicates that
2262 the branch is expected to be taken or not.
2263 3. IMAGE_REL_PPC_TOCDEFN - toc slot definition in the file
2264 For now, we just strip this stuff to find the type, and ignore it other
2268 unsigned short r_type
= EXTRACT_TYPE (rel
->r_type
);
2269 unsigned short r_flags
= EXTRACT_FLAGS(rel
->r_type
);
2270 unsigned short junk
= EXTRACT_JUNK (rel
->r_type
);
2272 /* the masking process only slices off the bottom byte for r_type. */
2273 if ( r_type
> MAX_RELOC_INDEX
)
2276 "coff_ppc_rtype_to_howto: index %d out of range [%d, %d]\n",
2277 r_type
, 0, MAX_RELOC_INDEX
);
2281 /* check for absolute crap */
2285 "coff_ppc_rtype_to_howto: reloc index %d contains junk %d\n",
2291 /* now examine flags */
2294 fprintf (stderr
, "Reloc with flags found!");
2295 if ( r_flags
& IMAGE_REL_PPC_NEG
)
2296 fprintf (stderr
, " NEG");
2297 if ( r_flags
& IMAGE_REL_PPC_BRTAKEN
)
2298 fprintf (stderr
, " BRTAKEN");
2299 if ( r_flags
& IMAGE_REL_PPC_BRNTAKEN
)
2300 fprintf (stderr
, " BRNTAKEN");
2301 if ( r_flags
& IMAGE_REL_PPC_TOCDEFN
)
2302 fprintf (stderr
, " TOCDEFN");
2303 fprintf(stderr
, "\n");
2309 case IMAGE_REL_PPC_ADDR32NB
:
2310 DUMP_RELOC2(ppc_coff_howto_table
[r_type
].name
, rel
);
2311 *addendp
-= pe_data(sec
->output_section
->owner
)->pe_opthdr
.ImageBase
;
2313 case IMAGE_REL_PPC_ADDR16
:
2314 case IMAGE_REL_PPC_REL24
:
2315 case IMAGE_REL_PPC_ADDR24
:
2316 case IMAGE_REL_PPC_TOCREL16
:
2317 case IMAGE_REL_PPC_ADDR32
:
2318 case IMAGE_REL_PPC_IFGLUE
:
2319 case IMAGE_REL_PPC_SECTION
:
2320 case IMAGE_REL_PPC_SECREL
:
2321 DUMP_RELOC2(ppc_coff_howto_table
[r_type
].name
, rel
);
2323 case IMAGE_REL_PPC_IMGLUE
:
2324 DUMP_RELOC2(ppc_coff_howto_table
[r_type
].name
, rel
);
2328 "Warning: Unsupported reloc %s [%d] used -- it may not work.\n",
2329 ppc_coff_howto_table
[r_type
].name
,
2334 howto
= ppc_coff_howto_table
+ r_type
;
2339 /* a cheesy little macro to make the code a little more readable */
2340 #define HOW2MAP(bfd_rtype,ppc_rtype) \
2341 case bfd_rtype: return &ppc_coff_howto_table[ppc_rtype]
2343 static reloc_howto_type
*ppc_coff_reloc_type_lookup
2344 PARAMS ((bfd
*, bfd_reloc_code_real_type
));
2346 static reloc_howto_type
*
2347 ppc_coff_reloc_type_lookup (abfd
, code
)
2349 bfd_reloc_code_real_type code
;
2353 fprintf(stderr
, "ppc_coff_reloc_type_lookup for %s\n",
2354 bfd_get_reloc_code_name(code
));
2359 HOW2MAP(BFD_RELOC_16_GOT_PCREL
, IMAGE_REL_PPC_IFGLUE
);
2360 HOW2MAP(BFD_RELOC_16
, IMAGE_REL_PPC_ADDR16
);
2361 HOW2MAP(BFD_RELOC_PPC_B26
, IMAGE_REL_PPC_REL24
);
2362 HOW2MAP(BFD_RELOC_PPC_BA26
, IMAGE_REL_PPC_ADDR24
);
2363 HOW2MAP(BFD_RELOC_PPC_TOC16
, IMAGE_REL_PPC_TOCREL16
);
2364 HOW2MAP(BFD_RELOC_32
, IMAGE_REL_PPC_ADDR32
);
2365 HOW2MAP(BFD_RELOC_RVA
, IMAGE_REL_PPC_ADDR32NB
);
2368 "\treturning NULL\n");
2378 /* Tailor coffcode.h -- macro heaven. */
2380 #define RTYPE2HOWTO(cache_ptr, dst) ppc_coff_rtype2howto (cache_ptr, dst)
2382 #ifndef COFF_IMAGE_WITH_PE
2384 ppc_coff_swap_sym_in_hook ();
2387 /* We use the special COFF backend linker, with our own special touch. */
2389 #define coff_bfd_reloc_type_lookup ppc_coff_reloc_type_lookup
2390 #define coff_rtype_to_howto coff_ppc_rtype_to_howto
2391 #define coff_relocate_section coff_ppc_relocate_section
2393 #ifndef COFF_IMAGE_WITH_PE
2394 #define coff_swap_sym_in_hook ppc_coff_swap_sym_in_hook
2397 #define SELECT_RELOC(internal, howto) {internal.r_type=howto->type;}
2399 #define COFF_PAGE_SIZE 0x1000
2401 #define POWERPC_LE_PE
2403 #include "coffcode.h"
2407 #ifndef COFF_IMAGE_WITH_PE
2409 What we're trying to do here is allocate a toc section (early), and attach
2410 it to the last bfd to be processed. This avoids the problem of having a toc
2411 written out before all files have been processed. This code allocates
2412 a toc section for every file, and records the last one seen. There are
2413 at least two problems with this approach:
2414 1. We allocate whole bunches of toc sections that are ignored, but at
2415 at least we will not allocate a toc if no .toc is present.
2416 2. It's not clear to me that being the last bfd read necessarily means
2417 that you are the last bfd closed.
2418 3. Doing it on a "swap in" hook depends on when the "swap in" is called,
2419 and how often, etc. It's not clear to me that there isn't a hole here.
2423 ppc_coff_swap_sym_in_hook (abfd
, ext1
, in1
)
2428 SYMENT
*ext
= (SYMENT
*)ext1
;
2429 struct internal_syment
*in
= (struct internal_syment
*)in1
;
2432 if (bfd_of_toc_owner
!= 0) /* we already have a toc, so go home */
2436 if (strcmp(in
->_n
._n_name
, ".toc") == 0)
2439 register asection
*s
;
2442 s
= bfd_get_section_by_name ( abfd
, TOC_SECTION_NAME
);
2448 flags
= SEC_ALLOC
| SEC_LOAD
| SEC_HAS_CONTENTS
| SEC_IN_MEMORY
;
2452 "ppc_coff_swap_sym_in_hook: about to create the %s section\n",
2456 s
= bfd_make_section (abfd
, TOC_SECTION_NAME
);
2459 || !bfd_set_section_flags (abfd
, s
, flags
)
2460 || !bfd_set_section_alignment (abfd
, s
, 2))
2463 "toc section allocation failed!\n");
2467 /* save the bfd for later allocation */
2468 bfd_of_toc_owner
= abfd
;
2477 /* The transfer vectors that lead the outside world to all of the above. */
2479 #ifdef TARGET_LITTLE_SYM
2483 TARGET_LITTLE_NAME
, /* name or coff-arm-little */
2484 bfd_target_coff_flavour
,
2485 false, /* data byte order is little */
2486 false, /* header byte order is little */
2488 (HAS_RELOC
| EXEC_P
| /* FIXME: object flags */
2489 HAS_LINENO
| HAS_DEBUG
|
2490 HAS_SYMS
| HAS_LOCALS
| WP_TEXT
),
2492 (SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
), /* section flags */
2493 0, /* leading char */
2494 '/', /* ar_pad_char */
2495 15, /* ar_max_namelen??? FIXMEmgo */
2497 bfd_getl64
, bfd_getl_signed_64
, bfd_putl64
,
2498 bfd_getl32
, bfd_getl_signed_32
, bfd_putl32
,
2499 bfd_getl16
, bfd_getl_signed_16
, bfd_putl16
, /* data */
2501 bfd_getl64
, bfd_getl_signed_64
, bfd_putl64
,
2502 bfd_getl32
, bfd_getl_signed_32
, bfd_putl32
,
2503 bfd_getl16
, bfd_getl_signed_16
, bfd_putl16
, /* hdrs */
2505 {_bfd_dummy_target
, coff_object_p
, /* bfd_check_format */
2506 bfd_generic_archive_p
, /* _bfd_dummy_target */ coff_object_p
},
2507 {bfd_false
, coff_mkobject
, _bfd_generic_mkarchive
, /* bfd_set_format */
2509 {bfd_false
, coff_write_object_contents
, /* bfd_write_contents */
2510 _bfd_write_archive_contents
, bfd_false
},
2512 BFD_JUMP_TABLE_GENERIC (coff
),
2513 BFD_JUMP_TABLE_COPY (coff
),
2514 BFD_JUMP_TABLE_CORE (_bfd_nocore
),
2515 BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_coff
),
2516 BFD_JUMP_TABLE_SYMBOLS (coff
),
2517 BFD_JUMP_TABLE_RELOCS (coff
),
2518 BFD_JUMP_TABLE_WRITE (coff
),
2519 BFD_JUMP_TABLE_LINK (coff
),
2520 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic
),
2526 #ifdef TARGET_BIG_SYM
2531 bfd_target_coff_flavour
,
2532 true, /* data byte order is big */
2533 true, /* header byte order is big */
2535 (HAS_RELOC
| EXEC_P
| /* FIXME: object flags */
2536 HAS_LINENO
| HAS_DEBUG
|
2537 HAS_SYMS
| HAS_LOCALS
| WP_TEXT
),
2539 (SEC_HAS_CONTENTS
| SEC_ALLOC
| SEC_LOAD
| SEC_RELOC
), /* section flags */
2540 0, /* leading char */
2541 '/', /* ar_pad_char */
2542 15, /* ar_max_namelen??? FIXMEmgo */
2544 bfd_getb64
, bfd_getb_signed_64
, bfd_putb64
,
2545 bfd_getb32
, bfd_getb_signed_32
, bfd_putb32
,
2546 bfd_getb16
, bfd_getb_signed_16
, bfd_putb16
, /* data */
2548 bfd_getb64
, bfd_getb_signed_64
, bfd_putb64
,
2549 bfd_getb32
, bfd_getb_signed_32
, bfd_putb32
,
2550 bfd_getb16
, bfd_getb_signed_16
, bfd_putb16
, /* hdrs */
2552 {_bfd_dummy_target
, coff_object_p
, /* bfd_check_format */
2553 bfd_generic_archive_p
, /* _bfd_dummy_target */ coff_object_p
},
2554 {bfd_false
, coff_mkobject
, _bfd_generic_mkarchive
, /* bfd_set_format */
2556 {bfd_false
, coff_write_object_contents
, /* bfd_write_contents */
2557 _bfd_write_archive_contents
, bfd_false
},
2559 BFD_JUMP_TABLE_GENERIC (coff
),
2560 BFD_JUMP_TABLE_COPY (coff
),
2561 BFD_JUMP_TABLE_CORE (_bfd_nocore
),
2562 BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_coff
),
2563 BFD_JUMP_TABLE_SYMBOLS (coff
),
2564 BFD_JUMP_TABLE_RELOCS (coff
),
2565 BFD_JUMP_TABLE_WRITE (coff
),
2566 BFD_JUMP_TABLE_LINK (coff
),
2567 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic
),