1 /* BFD back-end for PowerPC Microsoft Portable Executable files.
2 Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
3 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011,
4 2012 Free Software Foundation, Inc.
6 Original version pieced together by Kim Knuttila (krk@cygnus.com)
8 There is nothing new under the sun. This file draws a lot on other
9 coff files, in particular, those for the rs/6000, alpha, mips, and
10 intel backends, and the PE work for the arm.
12 This file is part of BFD, the Binary File Descriptor library.
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 3 of the License, or
17 (at your option) any later version.
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
24 You should have received a copy of the GNU General Public License
25 along with this program; if not, write to the Free Software
26 Foundation, 51 Franklin Street - Fifth Floor,
27 Boston, MA 02110-1301, USA. */
31 - relocs generated by gas
32 - ld will link files, but they do not run.
33 - dlltool will not produce correct output in some .reloc cases, and will
34 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 /* This file is compiled more than once, but we only compile the
54 final_link routine once. */
55 extern bfd_boolean ppc_bfd_coff_final_link (bfd *, struct bfd_link_info *);
56 extern void dump_toc (void *);
58 /* The toc is a set of bfd_vma fields. We use the fact that valid
59 addresses are even (i.e. the bit representing "1" is off) to allow
60 us to encode a little extra information in the field
61 - Unallocated addresses are initialized to 1.
62 - Allocated addresses are even numbers.
63 The first time we actually write a reference to the toc in the bfd,
64 we want to record that fact in a fixup file (if it is asked for), so
65 we keep track of whether or not an address has been written by marking
66 the low order bit with a "1" upon writing. */
68 #define SET_UNALLOCATED(x) ((x) = 1)
69 #define IS_UNALLOCATED(x) ((x) == 1)
71 #define IS_WRITTEN(x) ((x) & 1)
72 #define MARK_AS_WRITTEN(x) ((x) |= 1)
73 #define MAKE_ADDR_AGAIN(x) ((x) &= ~1)
75 /* Turn on this check if you suspect something amiss in the hash tables. */
78 /* Need a 7 char string for an eye catcher. */
81 #define HASH_CHECK_DCL char eye_catcher[8];
82 #define HASH_CHECK_INIT(ret) strcpy(ret->eye_catcher, EYE)
83 #define HASH_CHECK(addr) \
84 if (strcmp(addr->eye_catcher, EYE) != 0) \
87 _("File %s, line %d, Hash check failure, bad eye %8s\n"), \
88 __FILE__, __LINE__, addr->eye_catcher); \
94 #define HASH_CHECK_DCL
95 #define HASH_CHECK_INIT(ret)
96 #define HASH_CHECK(addr)
100 /* In order not to add an int to every hash table item for every coff
101 linker, we define our own hash table, derived from the coff one. */
103 /* PE linker hash table entries. */
105 struct ppc_coff_link_hash_entry
107 struct coff_link_hash_entry root; /* First entry, as required. */
109 /* As we wonder around the relocs, we'll keep the assigned toc_offset
111 bfd_vma toc_offset; /* Our addition, as required. */
113 unsigned long int glue_insn;
118 /* PE linker hash table. */
120 struct ppc_coff_link_hash_table
122 struct coff_link_hash_table root; /* First entry, as required. */
125 /* Routine to create an entry in the link hash table. */
127 static struct bfd_hash_entry *
128 ppc_coff_link_hash_newfunc (struct bfd_hash_entry * entry,
129 struct bfd_hash_table * table,
132 struct ppc_coff_link_hash_entry *ret =
133 (struct ppc_coff_link_hash_entry *) entry;
135 /* Allocate the structure if it has not already been allocated by a
137 if (ret == (struct ppc_coff_link_hash_entry *) NULL)
138 ret = (struct ppc_coff_link_hash_entry *)
139 bfd_hash_allocate (table,
140 sizeof (struct ppc_coff_link_hash_entry));
142 if (ret == (struct ppc_coff_link_hash_entry *) NULL)
145 /* Call the allocation method of the superclass. */
146 ret = ((struct ppc_coff_link_hash_entry *)
147 _bfd_coff_link_hash_newfunc ((struct bfd_hash_entry *) ret,
152 /* Initialize the local fields. */
153 SET_UNALLOCATED (ret->toc_offset);
154 ret->symbol_is_glue = 0;
157 HASH_CHECK_INIT (ret);
160 return (struct bfd_hash_entry *) ret;
163 /* Initialize a PE linker hash table. */
166 ppc_coff_link_hash_table_init (struct ppc_coff_link_hash_table *table,
168 struct bfd_hash_entry *(*newfunc)
169 (struct bfd_hash_entry *,
170 struct bfd_hash_table *,
172 unsigned int entsize)
174 return _bfd_coff_link_hash_table_init (&table->root, abfd, newfunc, entsize);
177 /* Create a PE linker hash table. */
179 static struct bfd_link_hash_table *
180 ppc_coff_link_hash_table_create (bfd *abfd)
182 struct ppc_coff_link_hash_table *ret;
183 bfd_size_type amt = sizeof (struct ppc_coff_link_hash_table);
185 ret = (struct ppc_coff_link_hash_table *) bfd_malloc (amt);
188 if (!ppc_coff_link_hash_table_init (ret, abfd,
189 ppc_coff_link_hash_newfunc,
190 sizeof (struct ppc_coff_link_hash_entry)))
193 return (struct bfd_link_hash_table *) NULL;
195 return &ret->root.root;
198 /* Now, tailor coffcode.h to use our hash stuff. */
200 #define coff_bfd_link_hash_table_create ppc_coff_link_hash_table_create
202 /* The nt loader points the toc register to &toc + 32768, in order to
203 use the complete range of a 16-bit displacement. We have to adjust
204 for this when we fix up loads displaced off the toc reg. */
205 #define TOC_LOAD_ADJUSTMENT (-32768)
206 #define TOC_SECTION_NAME ".private.toc"
208 /* The main body of code is in coffcode.h. */
210 #define COFF_DEFAULT_SECTION_ALIGNMENT_POWER (3)
212 /* In case we're on a 32-bit machine, construct a 64-bit "-1" value
213 from smaller values. Start with zero, widen, *then* decrement. */
214 #define MINUS_ONE (((bfd_vma)0) - 1)
216 /* These should definitely go in a header file somewhere... */
219 #define IMAGE_REL_PPC_ABSOLUTE 0x0000
222 #define IMAGE_REL_PPC_ADDR64 0x0001
225 #define IMAGE_REL_PPC_ADDR32 0x0002
227 /* 26-bit address, shifted left 2 (branch absolute) */
228 #define IMAGE_REL_PPC_ADDR24 0x0003
231 #define IMAGE_REL_PPC_ADDR16 0x0004
233 /* 16-bit address, shifted left 2 (load doubleword) */
234 #define IMAGE_REL_PPC_ADDR14 0x0005
236 /* 26-bit PC-relative offset, shifted left 2 (branch relative) */
237 #define IMAGE_REL_PPC_REL24 0x0006
239 /* 16-bit PC-relative offset, shifted left 2 (br cond relative) */
240 #define IMAGE_REL_PPC_REL14 0x0007
242 /* 16-bit offset from TOC base */
243 #define IMAGE_REL_PPC_TOCREL16 0x0008
245 /* 16-bit offset from TOC base, shifted left 2 (load doubleword) */
246 #define IMAGE_REL_PPC_TOCREL14 0x0009
248 /* 32-bit addr w/o image base */
249 #define IMAGE_REL_PPC_ADDR32NB 0x000A
251 /* va of containing section (as in an image sectionhdr) */
252 #define IMAGE_REL_PPC_SECREL 0x000B
254 /* sectionheader number */
255 #define IMAGE_REL_PPC_SECTION 0x000C
257 /* substitute TOC restore instruction iff symbol is glue code */
258 #define IMAGE_REL_PPC_IFGLUE 0x000D
260 /* symbol is glue code; virtual address is TOC restore instruction */
261 #define IMAGE_REL_PPC_IMGLUE 0x000E
263 /* va of containing section (limited to 16 bits) */
264 #define IMAGE_REL_PPC_SECREL16 0x000F
266 /* Stuff to handle immediate data when the number of bits in the
267 data is greater than the number of bits in the immediate field
268 We need to do (usually) 32 bit arithmetic on 16 bit chunks. */
269 #define IMAGE_REL_PPC_REFHI 0x0010
270 #define IMAGE_REL_PPC_REFLO 0x0011
271 #define IMAGE_REL_PPC_PAIR 0x0012
273 /* This is essentially the same as tocrel16, with TOCDEFN assumed. */
274 #define IMAGE_REL_PPC_TOCREL16_DEFN 0x0013
276 /* Flag bits in IMAGE_RELOCATION.TYPE. */
278 /* Subtract reloc value rather than adding it. */
279 #define IMAGE_REL_PPC_NEG 0x0100
281 /* Fix branch prediction bit to predict branch taken. */
282 #define IMAGE_REL_PPC_BRTAKEN 0x0200
284 /* Fix branch prediction bit to predict branch not taken. */
285 #define IMAGE_REL_PPC_BRNTAKEN 0x0400
287 /* TOC slot defined in file (or, data in toc). */
288 #define IMAGE_REL_PPC_TOCDEFN 0x0800
290 /* Masks to isolate above values in IMAGE_RELOCATION.Type. */
291 #define IMAGE_REL_PPC_TYPEMASK 0x00FF
292 #define IMAGE_REL_PPC_FLAGMASK 0x0F00
294 #define EXTRACT_TYPE(x) ((x) & IMAGE_REL_PPC_TYPEMASK)
295 #define EXTRACT_FLAGS(x) ((x) & IMAGE_REL_PPC_FLAGMASK)
296 #define EXTRACT_JUNK(x) \
297 ((x) & ~(IMAGE_REL_PPC_TYPEMASK | IMAGE_REL_PPC_FLAGMASK))
299 /* Static helper functions to make relocation work. */
300 /* (Work In Progress) */
302 static bfd_reloc_status_type ppc_refhi_reloc
303 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
304 static bfd_reloc_status_type ppc_pair_reloc
305 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
306 static bfd_reloc_status_type ppc_toc16_reloc
307 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
308 static bfd_reloc_status_type ppc_section_reloc
309 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
310 static bfd_reloc_status_type ppc_secrel_reloc
311 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
312 static bfd_reloc_status_type ppc_imglue_reloc
313 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
315 /* FIXME: It'll take a while to get through all of these. I only need a few to
316 get us started, so those I'll make sure work. Those marked FIXME are either
317 completely unverified or have a specific unknown marked in the comment. */
319 /* Relocation entries for Windows/NT on PowerPC.
321 From the document "" we find the following listed as used relocs:
324 ADDR[64|32|16] : fields that hold addresses in data fields or the
325 16 bit displacement field on a load/store.
326 ADDR[24|14] : fields that hold addresses in branch and cond
327 branches. These represent [26|16] bit addresses.
328 The low order 2 bits are preserved.
329 REL[24|14] : branches relative to the Instruction Address
330 register. These represent [26|16] bit addresses,
331 as before. The instruction field will be zero, and
332 the address of the SYM will be inserted at link time.
333 TOCREL16 : 16 bit displacement field referring to a slot in
335 TOCREL14 : 16 bit displacement field, similar to REL14 or ADDR14.
336 ADDR32NB : 32 bit address relative to the virtual origin.
337 (On the alpha, this is always a linker generated thunk)
338 (i.e. 32bit addr relative to the image base)
339 SECREL : The value is relative to the start of the section
340 containing the symbol.
341 SECTION : access to the header containing the item. Supports the
344 In particular, note that the document does not indicate that the
345 relocations listed in the header file are used. */
348 static reloc_howto_type ppc_coff_howto_table[] =
350 /* IMAGE_REL_PPC_ABSOLUTE 0x0000 NOP */
352 HOWTO (IMAGE_REL_PPC_ABSOLUTE, /* type */
354 0, /* size (0 = byte, 1 = short, 2 = long) */
356 FALSE, /* pc_relative */
358 complain_overflow_dont, /* dont complain_on_overflow */
359 0, /* special_function */
360 "ABSOLUTE", /* name */
361 FALSE, /* partial_inplace */
364 FALSE), /* pcrel_offset */
366 /* IMAGE_REL_PPC_ADDR64 0x0001 64-bit address */
368 HOWTO(IMAGE_REL_PPC_ADDR64, /* type */
370 3, /* size (0 = byte, 1 = short, 2 = long) */
372 FALSE, /* pc_relative */
374 complain_overflow_bitfield, /* complain_on_overflow */
375 0, /* special_function */
377 TRUE, /* partial_inplace */
378 MINUS_ONE, /* src_mask */
379 MINUS_ONE, /* dst_mask */
380 FALSE), /* pcrel_offset */
382 /* IMAGE_REL_PPC_ADDR32 0x0002 32-bit address */
384 HOWTO (IMAGE_REL_PPC_ADDR32, /* type */
386 2, /* size (0 = byte, 1 = short, 2 = long) */
388 FALSE, /* pc_relative */
390 complain_overflow_bitfield, /* complain_on_overflow */
391 0, /* special_function */
393 TRUE, /* partial_inplace */
394 0xffffffff, /* src_mask */
395 0xffffffff, /* dst_mask */
396 FALSE), /* pcrel_offset */
398 /* IMAGE_REL_PPC_ADDR24 0x0003 26-bit address, shifted left 2 (branch absolute) */
399 /* the LI field is in bit 6 through bit 29 is 24 bits, + 2 for the shift */
400 /* Of course, That's the IBM approved bit numbering, which is not what */
401 /* anyone else uses.... The li field is in bit 2 thru 25 */
403 HOWTO (IMAGE_REL_PPC_ADDR24, /* type */
405 2, /* size (0 = byte, 1 = short, 2 = long) */
407 FALSE, /* pc_relative */
409 complain_overflow_bitfield, /* complain_on_overflow */
410 0, /* special_function */
412 TRUE, /* partial_inplace */
413 0x07fffffc, /* src_mask */
414 0x07fffffc, /* dst_mask */
415 FALSE), /* pcrel_offset */
417 /* IMAGE_REL_PPC_ADDR16 0x0004 16-bit address */
419 HOWTO (IMAGE_REL_PPC_ADDR16, /* type */
421 1, /* size (0 = byte, 1 = short, 2 = long) */
423 FALSE, /* pc_relative */
425 complain_overflow_signed, /* complain_on_overflow */
426 0, /* special_function */
428 TRUE, /* partial_inplace */
429 0xffff, /* src_mask */
430 0xffff, /* dst_mask */
431 FALSE), /* pcrel_offset */
433 /* IMAGE_REL_PPC_ADDR14 0x0005 */
434 /* 16-bit address, shifted left 2 (load doubleword) */
435 /* FIXME: the mask is likely wrong, and the bit position may be as well */
437 HOWTO (IMAGE_REL_PPC_ADDR14, /* type */
439 1, /* size (0 = byte, 1 = short, 2 = long) */
441 FALSE, /* pc_relative */
443 complain_overflow_signed, /* complain_on_overflow */
444 0, /* special_function */
446 TRUE, /* partial_inplace */
447 0xffff, /* src_mask */
448 0xffff, /* dst_mask */
449 FALSE), /* pcrel_offset */
451 /* IMAGE_REL_PPC_REL24 0x0006 */
452 /* 26-bit PC-relative offset, shifted left 2 (branch relative) */
454 HOWTO (IMAGE_REL_PPC_REL24, /* type */
456 2, /* size (0 = byte, 1 = short, 2 = long) */
458 TRUE, /* pc_relative */
460 complain_overflow_signed, /* complain_on_overflow */
461 0, /* special_function */
463 TRUE, /* partial_inplace */
464 0x3fffffc, /* src_mask */
465 0x3fffffc, /* dst_mask */
466 FALSE), /* pcrel_offset */
468 /* IMAGE_REL_PPC_REL14 0x0007 */
469 /* 16-bit PC-relative offset, shifted left 2 (br cond relative) */
470 /* FIXME: the mask is likely wrong, and the bit position may be as well */
471 /* FIXME: how does it know how far to shift? */
473 HOWTO (IMAGE_REL_PPC_ADDR14, /* type */
475 1, /* size (0 = byte, 1 = short, 2 = long) */
477 FALSE, /* pc_relative */
479 complain_overflow_signed, /* complain_on_overflow */
480 0, /* special_function */
482 TRUE, /* partial_inplace */
483 0xffff, /* src_mask */
484 0xffff, /* dst_mask */
485 TRUE), /* pcrel_offset */
487 /* IMAGE_REL_PPC_TOCREL16 0x0008 */
488 /* 16-bit offset from TOC base */
490 HOWTO (IMAGE_REL_PPC_TOCREL16,/* type */
492 1, /* size (0 = byte, 1 = short, 2 = long) */
494 FALSE, /* pc_relative */
496 complain_overflow_dont, /* complain_on_overflow */
497 ppc_toc16_reloc, /* special_function */
498 "TOCREL16", /* name */
499 FALSE, /* partial_inplace */
500 0xffff, /* src_mask */
501 0xffff, /* dst_mask */
502 FALSE), /* pcrel_offset */
504 /* IMAGE_REL_PPC_TOCREL14 0x0009 */
505 /* 16-bit offset from TOC base, shifted left 2 (load doubleword) */
507 HOWTO (IMAGE_REL_PPC_TOCREL14,/* type */
509 1, /* size (0 = byte, 1 = short, 2 = long) */
511 FALSE, /* pc_relative */
513 complain_overflow_signed, /* complain_on_overflow */
514 0, /* special_function */
515 "TOCREL14", /* name */
516 FALSE, /* partial_inplace */
517 0xffff, /* src_mask */
518 0xffff, /* dst_mask */
519 FALSE), /* pcrel_offset */
521 /* IMAGE_REL_PPC_ADDR32NB 0x000A */
522 /* 32-bit addr w/ image base */
524 HOWTO (IMAGE_REL_PPC_ADDR32NB,/* type */
526 2, /* size (0 = byte, 1 = short, 2 = long) */
528 FALSE, /* pc_relative */
530 complain_overflow_signed, /* complain_on_overflow */
531 0, /* special_function */
532 "ADDR32NB", /* name */
533 TRUE, /* partial_inplace */
534 0xffffffff, /* src_mask */
535 0xffffffff, /* dst_mask */
536 FALSE), /* pcrel_offset */
538 /* IMAGE_REL_PPC_SECREL 0x000B */
539 /* va of containing section (as in an image sectionhdr) */
541 HOWTO (IMAGE_REL_PPC_SECREL,/* type */
543 2, /* size (0 = byte, 1 = short, 2 = long) */
545 FALSE, /* pc_relative */
547 complain_overflow_signed, /* complain_on_overflow */
548 ppc_secrel_reloc, /* special_function */
550 TRUE, /* partial_inplace */
551 0xffffffff, /* src_mask */
552 0xffffffff, /* dst_mask */
553 TRUE), /* pcrel_offset */
555 /* IMAGE_REL_PPC_SECTION 0x000C */
556 /* sectionheader number */
558 HOWTO (IMAGE_REL_PPC_SECTION,/* type */
560 2, /* size (0 = byte, 1 = short, 2 = long) */
562 FALSE, /* pc_relative */
564 complain_overflow_signed, /* complain_on_overflow */
565 ppc_section_reloc, /* special_function */
566 "SECTION", /* name */
567 TRUE, /* partial_inplace */
568 0xffffffff, /* src_mask */
569 0xffffffff, /* dst_mask */
570 TRUE), /* pcrel_offset */
572 /* IMAGE_REL_PPC_IFGLUE 0x000D */
573 /* substitute TOC restore instruction iff symbol is glue code */
575 HOWTO (IMAGE_REL_PPC_IFGLUE,/* type */
577 2, /* size (0 = byte, 1 = short, 2 = long) */
579 FALSE, /* pc_relative */
581 complain_overflow_signed, /* complain_on_overflow */
582 0, /* special_function */
584 TRUE, /* partial_inplace */
585 0xffffffff, /* src_mask */
586 0xffffffff, /* dst_mask */
587 FALSE), /* pcrel_offset */
589 /* IMAGE_REL_PPC_IMGLUE 0x000E */
590 /* symbol is glue code; virtual address is TOC restore instruction */
592 HOWTO (IMAGE_REL_PPC_IMGLUE,/* type */
594 2, /* size (0 = byte, 1 = short, 2 = long) */
596 FALSE, /* pc_relative */
598 complain_overflow_dont, /* complain_on_overflow */
599 ppc_imglue_reloc, /* special_function */
601 FALSE, /* partial_inplace */
602 0xffffffff, /* src_mask */
603 0xffffffff, /* dst_mask */
604 FALSE), /* pcrel_offset */
606 /* IMAGE_REL_PPC_SECREL16 0x000F */
607 /* va of containing section (limited to 16 bits) */
609 HOWTO (IMAGE_REL_PPC_SECREL16,/* type */
611 1, /* size (0 = byte, 1 = short, 2 = long) */
613 FALSE, /* pc_relative */
615 complain_overflow_signed, /* complain_on_overflow */
616 0, /* special_function */
617 "SECREL16", /* name */
618 TRUE, /* partial_inplace */
619 0xffff, /* src_mask */
620 0xffff, /* dst_mask */
621 TRUE), /* pcrel_offset */
623 /* IMAGE_REL_PPC_REFHI 0x0010 */
625 HOWTO (IMAGE_REL_PPC_REFHI, /* type */
627 1, /* size (0 = byte, 1 = short, 2 = long) */
629 FALSE, /* pc_relative */
631 complain_overflow_signed, /* complain_on_overflow */
632 ppc_refhi_reloc, /* special_function */
634 TRUE, /* partial_inplace */
635 0xffffffff, /* src_mask */
636 0xffffffff, /* dst_mask */
637 FALSE), /* pcrel_offset */
639 /* IMAGE_REL_PPC_REFLO 0x0011 */
641 HOWTO (IMAGE_REL_PPC_REFLO, /* type */
643 1, /* size (0 = byte, 1 = short, 2 = long) */
645 FALSE, /* pc_relative */
647 complain_overflow_signed, /* complain_on_overflow */
648 ppc_refhi_reloc, /* special_function */
650 TRUE, /* partial_inplace */
651 0xffffffff, /* src_mask */
652 0xffffffff, /* dst_mask */
653 FALSE), /* pcrel_offset */
655 /* IMAGE_REL_PPC_PAIR 0x0012 */
657 HOWTO (IMAGE_REL_PPC_PAIR, /* type */
659 1, /* size (0 = byte, 1 = short, 2 = long) */
661 FALSE, /* pc_relative */
663 complain_overflow_signed, /* complain_on_overflow */
664 ppc_pair_reloc, /* special_function */
666 TRUE, /* partial_inplace */
667 0xffffffff, /* src_mask */
668 0xffffffff, /* dst_mask */
669 FALSE), /* pcrel_offset */
671 /* IMAGE_REL_PPC_TOCREL16_DEFN 0x0013 */
672 /* 16-bit offset from TOC base, without causing a definition */
674 HOWTO ( (IMAGE_REL_PPC_TOCREL16 | IMAGE_REL_PPC_TOCDEFN), /* type */
676 1, /* size (0 = byte, 1 = short, 2 = long) */
678 FALSE, /* pc_relative */
680 complain_overflow_dont, /* complain_on_overflow */
681 0, /* special_function */
682 "TOCREL16, TOCDEFN", /* name */
683 FALSE, /* partial_inplace */
684 0xffff, /* src_mask */
685 0xffff, /* dst_mask */
686 FALSE), /* pcrel_offset */
690 /* Some really cheezy macros that can be turned on to test stderr :-) */
699 fprintf (stderr,_("Unimplemented Relocation -- %s\n"),x); \
703 #define DUMP_RELOC(n,r) \
705 fprintf (stderr,"%s sym %d, addr %d, addend %d\n", \
706 n, (*(r->sym_ptr_ptr))->name, \
707 r->address, r->addend); \
710 /* Given a reloc name, n, and a pointer to an internal_reloc,
711 dump out interesting information on the contents
713 #define n_name _n._n_name
714 #define n_zeroes _n._n_n._n_zeroes
715 #define n_offset _n._n_n._n_offset */
717 #define DUMP_RELOC2(n,r) \
719 fprintf (stderr,"%s sym %d, r_vaddr %d %s\n", \
720 n, r->r_symndx, r->r_vaddr, \
721 (((r->r_type) & IMAGE_REL_PPC_TOCDEFN) == 0) \
727 #define DUMP_RELOC(n,r)
728 #define DUMP_RELOC2(n,r)
731 /* TOC construction and management routines. */
733 /* This file is compiled twice, and these variables are defined in one
734 of the compilations. FIXME: This is confusing and weird. Also,
735 BFD should not use global variables. */
736 extern bfd * bfd_of_toc_owner;
737 extern long int global_toc_size;
738 extern long int import_table_size;
739 extern long int first_thunk_address;
740 extern long int thunk_size;
758 struct list_ele *next;
760 enum ref_category cat;
765 extern struct list_ele *head;
766 extern struct list_ele *tail;
769 record_toc (asection *toc_section,
770 bfd_signed_vma our_toc_offset,
771 enum ref_category cat,
774 /* Add this entry to our toc addr-offset-name list. */
775 bfd_size_type amt = sizeof (struct list_ele);
776 struct list_ele *t = (struct list_ele *) bfd_malloc (amt);
781 t->offset = our_toc_offset;
784 t->addr = toc_section->output_offset + our_toc_offset;
798 #ifdef COFF_IMAGE_WITH_PE
800 /* Record a toc offset against a symbol. */
802 ppc_record_toc_entry (bfd *abfd,
803 struct bfd_link_info *info ATTRIBUTE_UNUSED,
804 asection *sec ATTRIBUTE_UNUSED,
806 enum toc_type toc_kind ATTRIBUTE_UNUSED)
808 struct ppc_coff_link_hash_entry *h;
813 h = (struct ppc_coff_link_hash_entry *) (obj_coff_sym_hashes (abfd)[sym]);
821 local_syms = obj_coff_local_toc_table(abfd);
828 /* allocate a table */
829 amt = (bfd_size_type) obj_raw_syment_count (abfd) * sizeof (int);
830 local_syms = (int *) bfd_zalloc (abfd, amt);
833 obj_coff_local_toc_table (abfd) = local_syms;
835 for (i = 0; i < obj_raw_syment_count (abfd); ++i)
837 SET_UNALLOCATED (local_syms[i]);
841 if (IS_UNALLOCATED(local_syms[sym]))
843 local_syms[sym] = global_toc_size;
844 global_toc_size += 4;
846 /* The size must fit in a 16-bit displacement. */
847 if (global_toc_size > 65535)
849 (*_bfd_error_handler) (_("TOC overflow"));
850 bfd_set_error (bfd_error_file_too_big);
857 /* Check to see if there's a toc slot allocated. If not, do it
858 here. It will be used in relocate_section. */
859 if (IS_UNALLOCATED(h->toc_offset))
861 h->toc_offset = global_toc_size;
862 global_toc_size += 4;
864 /* The size must fit in a 16-bit displacement. */
865 if (global_toc_size >= 65535)
867 (*_bfd_error_handler) (_("TOC overflow"));
868 bfd_set_error (bfd_error_file_too_big);
877 /* Record a toc offset against a symbol. */
879 ppc_mark_symbol_as_glue (bfd *abfd,
881 struct internal_reloc *rel)
883 struct ppc_coff_link_hash_entry *h;
885 h = (struct ppc_coff_link_hash_entry *) (obj_coff_sym_hashes (abfd)[sym]);
889 h->symbol_is_glue = 1;
890 h->glue_insn = bfd_get_32 (abfd, (bfd_byte *) &rel->r_vaddr);
895 #endif /* COFF_IMAGE_WITH_PE */
897 /* Return TRUE if this relocation should
898 appear in the output .reloc section. */
901 in_reloc_p (bfd * abfd ATTRIBUTE_UNUSED,
902 reloc_howto_type *howto)
905 (! howto->pc_relative)
906 && (howto->type != IMAGE_REL_PPC_ADDR32NB)
907 && (howto->type != IMAGE_REL_PPC_TOCREL16)
908 && (howto->type != IMAGE_REL_PPC_IMGLUE)
909 && (howto->type != IMAGE_REL_PPC_IFGLUE)
910 && (howto->type != IMAGE_REL_PPC_SECREL)
911 && (howto->type != IMAGE_REL_PPC_SECTION)
912 && (howto->type != IMAGE_REL_PPC_SECREL16)
913 && (howto->type != IMAGE_REL_PPC_REFHI)
914 && (howto->type != IMAGE_REL_PPC_REFLO)
915 && (howto->type != IMAGE_REL_PPC_PAIR)
916 && (howto->type != IMAGE_REL_PPC_TOCREL16_DEFN) ;
920 write_base_file_entry (bfd *obfd, struct bfd_link_info *info, bfd_vma addr)
922 if (coff_data (obfd)->pe)
923 addr -= pe_data (obfd)->pe_opthdr.ImageBase;
924 if (fwrite (&addr, sizeof (addr), 1, (FILE *) info->base_file) == 1)
927 bfd_set_error (bfd_error_system_call);
931 /* The reloc processing routine for the optimized COFF linker. */
934 coff_ppc_relocate_section (bfd *output_bfd,
935 struct bfd_link_info *info,
937 asection *input_section,
939 struct internal_reloc *relocs,
940 struct internal_syment *syms,
943 struct internal_reloc *rel;
944 struct internal_reloc *relend;
945 asection *toc_section = 0;
947 reloc_howto_type *howto = 0;
949 /* If we are performing a relocatable link, we don't need to do a
950 thing. The caller will take care of adjusting the reloc
951 addresses and symbol indices. */
952 if (info->relocatable)
956 relend = rel + input_section->reloc_count;
957 for (; rel < relend; rel++)
960 struct ppc_coff_link_hash_entry *h;
961 struct internal_syment *sym;
965 bfd_reloc_status_type rstat;
968 unsigned short r_type = EXTRACT_TYPE (rel->r_type);
969 unsigned short r_flags = EXTRACT_FLAGS(rel->r_type);
971 symndx = rel->r_symndx;
972 loc = contents + rel->r_vaddr - input_section->vma;
974 /* FIXME: check bounds on r_type */
975 howto = ppc_coff_howto_table + r_type;
984 h = (struct ppc_coff_link_hash_entry *)
985 (obj_coff_sym_hashes (input_bfd)[symndx]);
994 if (r_type == IMAGE_REL_PPC_IMGLUE && h == 0)
996 /* An IMGLUE reloc must have a name. Something is very wrong. */
1003 /* FIXME: PAIR unsupported in the following code. */
1007 sec = bfd_abs_section_ptr;
1010 sec = sections[symndx];
1011 val = (sec->output_section->vma
1012 + sec->output_offset
1014 if (! obj_pe (output_bfd))
1022 if (h->root.root.type == bfd_link_hash_defined
1023 || h->root.root.type == bfd_link_hash_defweak)
1025 sec = h->root.root.u.def.section;
1026 val = (h->root.root.u.def.value
1027 + sec->output_section->vma
1028 + sec->output_offset);
1032 if (! ((*info->callbacks->undefined_symbol)
1033 (info, h->root.root.root.string, input_bfd, input_section,
1034 rel->r_vaddr - input_section->vma, TRUE)))
1039 rstat = bfd_reloc_ok;
1041 /* Each case must do its own relocation, setting rstat appropriately. */
1045 (*_bfd_error_handler)
1046 (_("%B: unsupported relocation type 0x%02x"), input_bfd, r_type);
1047 bfd_set_error (bfd_error_bad_value);
1049 case IMAGE_REL_PPC_TOCREL16:
1051 bfd_signed_vma our_toc_offset;
1054 DUMP_RELOC2(howto->name, rel);
1056 if (toc_section == 0)
1058 toc_section = bfd_get_section_by_name (bfd_of_toc_owner,
1061 if ( toc_section == NULL )
1063 /* There is no toc section. Something is very wrong. */
1068 /* Amazing bit tricks present. As we may have seen earlier, we
1069 use the 1 bit to tell us whether or not a toc offset has been
1070 allocated. Now that they've all been allocated, we will use
1071 the 1 bit to tell us if we've written this particular toc
1076 /* It is a file local symbol. */
1077 int *local_toc_table;
1080 sym = syms + symndx;
1081 name = sym->_n._n_name;
1083 local_toc_table = obj_coff_local_toc_table(input_bfd);
1084 our_toc_offset = local_toc_table[symndx];
1086 if (IS_WRITTEN(our_toc_offset))
1088 /* If it has been written out, it is marked with the
1089 1 bit. Fix up our offset, but do not write it out
1091 MAKE_ADDR_AGAIN(our_toc_offset);
1095 /* Write out the toc entry. */
1096 record_toc (toc_section, our_toc_offset, priv,
1099 bfd_put_32 (output_bfd, val,
1100 toc_section->contents + our_toc_offset);
1102 MARK_AS_WRITTEN(local_toc_table[symndx]);
1108 const char *name = h->root.root.root.string;
1109 our_toc_offset = h->toc_offset;
1111 if ((r_flags & IMAGE_REL_PPC_TOCDEFN)
1112 == IMAGE_REL_PPC_TOCDEFN )
1114 /* This is unbelievable cheese. Some knowledgable asm
1115 hacker has decided to use r2 as a base for loading
1116 a value. He/She does this by setting the tocdefn bit,
1117 and not supplying a toc definition. The behaviour is
1118 then to use the difference between the value of the
1119 symbol and the actual location of the toc as the toc
1122 In fact, what is usually happening is, because the
1123 Import Address Table is mapped immediately following
1124 the toc, some trippy library code trying for speed on
1125 dll linkage, takes advantage of that and considers
1126 the IAT to be part of the toc, thus saving a load. */
1128 our_toc_offset = val - (toc_section->output_section->vma
1129 + toc_section->output_offset);
1131 /* The size must still fit in a 16-bit displacement. */
1132 if ((bfd_vma) our_toc_offset >= 65535)
1134 (*_bfd_error_handler)
1135 (_("%B: Relocation for %s of %lx exceeds Toc size limit"),
1137 (unsigned long) our_toc_offset);
1138 bfd_set_error (bfd_error_bad_value);
1142 record_toc (toc_section, our_toc_offset, pub,
1145 else if (IS_WRITTEN (our_toc_offset))
1147 /* If it has been written out, it is marked with the
1148 1 bit. Fix up our offset, but do not write it out
1150 MAKE_ADDR_AGAIN(our_toc_offset);
1154 record_toc(toc_section, our_toc_offset, pub,
1157 /* Write out the toc entry. */
1158 bfd_put_32 (output_bfd, val,
1159 toc_section->contents + our_toc_offset);
1161 MARK_AS_WRITTEN(h->toc_offset);
1162 /* The tricky part is that this is the address that
1163 needs a .reloc entry for it. */
1168 if (fixit && info->base_file)
1170 /* So if this is non pcrelative, and is referenced
1171 to a section or a common symbol, then it needs a reloc. */
1173 /* Relocation to a symbol in a section which
1174 isn't absolute - we output the address here
1176 bfd_vma addr = (toc_section->output_section->vma
1177 + toc_section->output_offset + our_toc_offset);
1179 if (!write_base_file_entry (output_bfd, info, addr))
1183 /* FIXME: this test is conservative. */
1184 if ((r_flags & IMAGE_REL_PPC_TOCDEFN) != IMAGE_REL_PPC_TOCDEFN
1185 && (bfd_vma) our_toc_offset > toc_section->size)
1187 (*_bfd_error_handler)
1188 (_("%B: Relocation exceeds allocated TOC (%lx)"),
1189 input_bfd, (unsigned long) toc_section->size);
1190 bfd_set_error (bfd_error_bad_value);
1194 /* Now we know the relocation for this toc reference. */
1195 relocation = our_toc_offset + TOC_LOAD_ADJUSTMENT;
1196 rstat = _bfd_relocate_contents (howto, input_bfd, relocation, loc);
1199 case IMAGE_REL_PPC_IFGLUE:
1201 /* To solve this, we need to know whether or not the symbol
1202 appearing on the call instruction is a glue function or not.
1203 A glue function must announce itself via a IMGLUE reloc, and
1204 the reloc contains the required toc restore instruction. */
1205 DUMP_RELOC2 (howto->name, rel);
1209 if (h->symbol_is_glue == 1)
1211 bfd_put_32 (input_bfd, (bfd_vma) h->glue_insn, loc);
1216 case IMAGE_REL_PPC_SECREL:
1217 /* Unimplemented: codeview debugging information. */
1218 /* For fast access to the header of the section
1219 containing the item. */
1221 case IMAGE_REL_PPC_SECTION:
1222 /* Unimplemented: codeview debugging information. */
1223 /* Is used to indicate that the value should be relative
1224 to the beginning of the section that contains the
1227 case IMAGE_REL_PPC_ABSOLUTE:
1229 const char *my_name;
1232 my_name = (syms+symndx)->_n._n_name;
1234 my_name = h->root.root.root.string;
1236 (*_bfd_error_handler)
1237 (_("Warning: unsupported reloc %s <file %B, section %A>\n"
1238 "sym %ld (%s), r_vaddr %ld (%lx)"),
1239 input_bfd, input_section, howto->name,
1240 rel->r_symndx, my_name, (long) rel->r_vaddr,
1241 (unsigned long) rel->r_vaddr);
1244 case IMAGE_REL_PPC_IMGLUE:
1246 /* There is nothing to do now. This reloc was noted in the first
1247 pass over the relocs, and the glue instruction extracted. */
1248 const char *my_name;
1250 if (h->symbol_is_glue == 1)
1252 my_name = h->root.root.root.string;
1254 (*_bfd_error_handler)
1255 (_("%B: Out of order IMGLUE reloc for %s"), input_bfd, my_name);
1256 bfd_set_error (bfd_error_bad_value);
1260 case IMAGE_REL_PPC_ADDR32NB:
1262 const char *name = 0;
1264 DUMP_RELOC2 (howto->name, rel);
1266 if (CONST_STRNEQ (input_section->name, ".idata$2") && first_thunk_address == 0)
1268 /* Set magic values. */
1270 struct coff_link_hash_entry *myh;
1272 myh = coff_link_hash_lookup (coff_hash_table (info),
1274 FALSE, FALSE, TRUE);
1275 first_thunk_address = myh->root.u.def.value +
1276 sec->output_section->vma +
1277 sec->output_offset -
1278 pe_data(output_bfd)->pe_opthdr.ImageBase;
1280 idata5offset = myh->root.u.def.value;
1281 myh = coff_link_hash_lookup (coff_hash_table (info),
1283 FALSE, FALSE, TRUE);
1285 thunk_size = myh->root.u.def.value - idata5offset;
1286 myh = coff_link_hash_lookup (coff_hash_table (info),
1288 FALSE, FALSE, TRUE);
1289 import_table_size = myh->root.u.def.value;
1294 /* It is a file local symbol. */
1295 sym = syms + symndx;
1296 name = sym->_n._n_name;
1302 name = h->root.root.root.string;
1303 if (strcmp (".idata$2", name) == 0)
1304 target = "__idata2_magic__";
1305 else if (strcmp (".idata$4", name) == 0)
1306 target = "__idata4_magic__";
1307 else if (strcmp (".idata$5", name) == 0)
1308 target = "__idata5_magic__";
1312 struct coff_link_hash_entry *myh;
1314 myh = coff_link_hash_lookup (coff_hash_table (info),
1316 FALSE, FALSE, TRUE);
1319 /* Missing magic cookies. Something is very wrong. */
1323 val = myh->root.u.def.value +
1324 sec->output_section->vma + sec->output_offset;
1325 if (first_thunk_address == 0)
1328 myh = coff_link_hash_lookup (coff_hash_table (info),
1330 FALSE, FALSE, TRUE);
1331 first_thunk_address = myh->root.u.def.value +
1332 sec->output_section->vma +
1333 sec->output_offset -
1334 pe_data(output_bfd)->pe_opthdr.ImageBase;
1336 idata5offset = myh->root.u.def.value;
1337 myh = coff_link_hash_lookup (coff_hash_table (info),
1339 FALSE, FALSE, TRUE);
1341 thunk_size = myh->root.u.def.value - idata5offset;
1342 myh = coff_link_hash_lookup (coff_hash_table (info),
1344 FALSE, FALSE, TRUE);
1345 import_table_size = myh->root.u.def.value;
1350 rstat = _bfd_relocate_contents (howto,
1353 pe_data (output_bfd)->pe_opthdr.ImageBase,
1358 case IMAGE_REL_PPC_REL24:
1359 DUMP_RELOC2(howto->name, rel);
1360 val -= (input_section->output_section->vma
1361 + input_section->output_offset);
1363 rstat = _bfd_relocate_contents (howto,
1368 case IMAGE_REL_PPC_ADDR16:
1369 case IMAGE_REL_PPC_ADDR24:
1370 case IMAGE_REL_PPC_ADDR32:
1371 DUMP_RELOC2(howto->name, rel);
1372 rstat = _bfd_relocate_contents (howto,
1379 if (info->base_file)
1381 /* So if this is non pcrelative, and is referenced
1382 to a section or a common symbol, then it needs a reloc. */
1383 if (sym && pe_data(output_bfd)->in_reloc_p (output_bfd, howto))
1385 /* Relocation to a symbol in a section which
1386 isn't absolute - we output the address here
1388 bfd_vma addr = (rel->r_vaddr
1389 - input_section->vma
1390 + input_section->output_offset
1391 + input_section->output_section->vma);
1393 if (!write_base_file_entry (output_bfd, info, addr))
1404 case bfd_reloc_overflow:
1407 char buf[SYMNMLEN + 1];
1413 else if (sym == NULL)
1415 else if (sym->_n._n_n._n_zeroes == 0
1416 && sym->_n._n_n._n_offset != 0)
1417 name = obj_coff_strings (input_bfd) + sym->_n._n_n._n_offset;
1420 strncpy (buf, sym->_n._n_name, SYMNMLEN);
1421 buf[SYMNMLEN] = '\0';
1425 if (! ((*info->callbacks->reloc_overflow)
1426 (info, (h ? &h->root.root : NULL), name, howto->name,
1427 (bfd_vma) 0, input_bfd,
1428 input_section, rel->r_vaddr - input_section->vma)))
1437 #ifdef COFF_IMAGE_WITH_PE
1439 /* FIXME: BFD should not use global variables. This file is compiled
1440 twice, and these variables are shared. This is confusing and
1443 long int global_toc_size = 4;
1445 bfd* bfd_of_toc_owner = 0;
1447 long int import_table_size;
1448 long int first_thunk_address;
1449 long int thunk_size;
1451 struct list_ele *head;
1452 struct list_ele *tail;
1455 h1 = N_("\n\t\t\tTOC MAPPING\n\n");
1457 h2 = N_(" TOC disassembly Comments Name\n");
1459 h3 = N_(" Offset spelling (if present)\n");
1462 dump_toc (void * vfile)
1464 FILE *file = (FILE *) vfile;
1467 fputs (_(h1), file);
1468 fputs (_(h2), file);
1469 fputs (_(h3), file);
1471 for (t = head; t != 0; t=t->next)
1473 const char *cat = "";
1476 cat = _("private ");
1477 else if (t->cat == pub)
1479 else if (t->cat == tocdata)
1480 cat = _("data-in-toc ");
1482 if (t->offset > global_toc_size)
1484 if (t->offset <= global_toc_size + thunk_size)
1485 cat = _("IAT reference ");
1489 _("**** global_toc_size %ld(%lx), thunk_size %ld(%lx)\n"),
1490 global_toc_size, (unsigned long) global_toc_size,
1491 thunk_size, (unsigned long) thunk_size);
1492 cat = _("Out of bounds!");
1497 " %04lx (%d)", (unsigned long) t->offset, t->offset - 32768);
1504 fprintf (file, "\n");
1508 ppc_allocate_toc_section (struct bfd_link_info *info ATTRIBUTE_UNUSED)
1513 static char test_char = '1';
1515 if ( global_toc_size == 0 ) /* FIXME: does this get me in trouble? */
1518 if (bfd_of_toc_owner == 0)
1519 /* No toc owner? Something is very wrong. */
1522 s = bfd_get_section_by_name ( bfd_of_toc_owner , TOC_SECTION_NAME);
1524 /* No toc section? Something is very wrong. */
1527 amt = global_toc_size;
1528 foo = (bfd_byte *) bfd_alloc (bfd_of_toc_owner, amt);
1529 memset(foo, test_char, (size_t) global_toc_size);
1531 s->size = global_toc_size;
1538 ppc_process_before_allocation (bfd *abfd,
1539 struct bfd_link_info *info)
1542 struct internal_reloc *i, *rel;
1544 /* Here we have a bfd that is to be included on the link. We have a hook
1545 to do reloc rummaging, before section sizes are nailed down. */
1546 _bfd_coff_get_external_symbols (abfd);
1548 /* Rummage around all the relocs and map the toc. */
1549 sec = abfd->sections;
1554 for (; sec != 0; sec = sec->next)
1556 if (sec->reloc_count == 0)
1559 /* load the relocs */
1560 /* FIXME: there may be a storage leak here */
1561 i=_bfd_coff_read_internal_relocs(abfd,sec,1,0,0,0);
1566 for (rel = i; rel < i + sec->reloc_count; ++rel)
1568 unsigned short r_type = EXTRACT_TYPE (rel->r_type);
1569 unsigned short r_flags = EXTRACT_FLAGS (rel->r_type);
1570 bfd_boolean ok = TRUE;
1572 DUMP_RELOC2 (ppc_coff_howto_table[r_type].name, rel);
1576 case IMAGE_REL_PPC_TOCREL16:
1577 /* If TOCDEFN is on, ignore as someone else has allocated the
1579 if ((r_flags & IMAGE_REL_PPC_TOCDEFN) != IMAGE_REL_PPC_TOCDEFN)
1580 ok = ppc_record_toc_entry(abfd, info, sec,
1581 rel->r_symndx, default_toc);
1585 case IMAGE_REL_PPC_IMGLUE:
1586 ppc_mark_symbol_as_glue (abfd, rel->r_symndx, rel);
1599 static bfd_reloc_status_type
1600 ppc_refhi_reloc (bfd *abfd ATTRIBUTE_UNUSED,
1601 arelent *reloc_entry ATTRIBUTE_UNUSED,
1602 asymbol *symbol ATTRIBUTE_UNUSED,
1603 void * data ATTRIBUTE_UNUSED,
1604 asection *input_section ATTRIBUTE_UNUSED,
1606 char **error_message ATTRIBUTE_UNUSED)
1609 DUMP_RELOC("REFHI",reloc_entry);
1611 if (output_bfd == (bfd *) NULL)
1612 return bfd_reloc_continue;
1614 return bfd_reloc_undefined;
1617 static bfd_reloc_status_type
1618 ppc_pair_reloc (bfd *abfd ATTRIBUTE_UNUSED,
1619 arelent *reloc_entry ATTRIBUTE_UNUSED,
1620 asymbol *symbol ATTRIBUTE_UNUSED,
1621 void * data ATTRIBUTE_UNUSED,
1622 asection *input_section ATTRIBUTE_UNUSED,
1624 char **error_message ATTRIBUTE_UNUSED)
1627 DUMP_RELOC("PAIR",reloc_entry);
1629 if (output_bfd == (bfd *) NULL)
1630 return bfd_reloc_continue;
1632 return bfd_reloc_undefined;
1635 static bfd_reloc_status_type
1636 ppc_toc16_reloc (bfd *abfd ATTRIBUTE_UNUSED,
1637 arelent *reloc_entry ATTRIBUTE_UNUSED,
1638 asymbol *symbol ATTRIBUTE_UNUSED,
1639 void * data ATTRIBUTE_UNUSED,
1640 asection *input_section ATTRIBUTE_UNUSED,
1642 char **error_message ATTRIBUTE_UNUSED)
1644 UN_IMPL ("TOCREL16");
1645 DUMP_RELOC ("TOCREL16",reloc_entry);
1647 if (output_bfd == (bfd *) NULL)
1648 return bfd_reloc_continue;
1650 return bfd_reloc_ok;
1653 static bfd_reloc_status_type
1654 ppc_secrel_reloc (bfd *abfd ATTRIBUTE_UNUSED,
1655 arelent *reloc_entry ATTRIBUTE_UNUSED,
1656 asymbol *symbol ATTRIBUTE_UNUSED,
1657 void * data ATTRIBUTE_UNUSED,
1658 asection *input_section ATTRIBUTE_UNUSED,
1660 char **error_message ATTRIBUTE_UNUSED)
1663 DUMP_RELOC("SECREL",reloc_entry);
1665 if (output_bfd == (bfd *) NULL)
1666 return bfd_reloc_continue;
1668 return bfd_reloc_ok;
1671 static bfd_reloc_status_type
1672 ppc_section_reloc (bfd *abfd ATTRIBUTE_UNUSED,
1673 arelent *reloc_entry ATTRIBUTE_UNUSED,
1674 asymbol *symbol ATTRIBUTE_UNUSED,
1675 void * data ATTRIBUTE_UNUSED,
1676 asection *input_section ATTRIBUTE_UNUSED,
1678 char **error_message ATTRIBUTE_UNUSED)
1681 DUMP_RELOC("SECTION",reloc_entry);
1683 if (output_bfd == (bfd *) NULL)
1684 return bfd_reloc_continue;
1686 return bfd_reloc_ok;
1689 static bfd_reloc_status_type
1690 ppc_imglue_reloc (bfd *abfd ATTRIBUTE_UNUSED,
1691 arelent *reloc_entry ATTRIBUTE_UNUSED,
1692 asymbol *symbol ATTRIBUTE_UNUSED,
1693 void * data ATTRIBUTE_UNUSED,
1694 asection *input_section ATTRIBUTE_UNUSED,
1696 char **error_message ATTRIBUTE_UNUSED)
1700 DUMP_RELOC("IMGLUE",reloc_entry);
1702 if (output_bfd == (bfd *) NULL)
1703 return bfd_reloc_continue;
1705 return bfd_reloc_ok;
1708 #define MAX_RELOC_INDEX \
1709 (sizeof (ppc_coff_howto_table) / sizeof (ppc_coff_howto_table[0]) - 1)
1711 /* FIXME: There is a possibility that when we read in a reloc from a file,
1712 that there are some bits encoded in the upper portion of the
1713 type field. Not yet implemented. */
1716 ppc_coff_rtype2howto (arelent *relent, struct internal_reloc *internal)
1718 /* We can encode one of three things in the type field, aside from the
1720 1. IMAGE_REL_PPC_NEG - indicates the value field is a subtraction
1721 value, rather than an addition value
1722 2. IMAGE_REL_PPC_BRTAKEN, IMAGE_REL_PPC_BRNTAKEN - indicates that
1723 the branch is expected to be taken or not.
1724 3. IMAGE_REL_PPC_TOCDEFN - toc slot definition in the file
1725 For now, we just strip this stuff to find the type, and ignore it other
1727 reloc_howto_type *howto;
1728 unsigned short r_type = EXTRACT_TYPE (internal->r_type);
1729 unsigned short r_flags = EXTRACT_FLAGS(internal->r_type);
1730 unsigned short junk = EXTRACT_JUNK (internal->r_type);
1732 /* The masking process only slices off the bottom byte for r_type. */
1733 if ( r_type > MAX_RELOC_INDEX )
1736 /* Check for absolute crap. */
1742 case IMAGE_REL_PPC_ADDR16:
1743 case IMAGE_REL_PPC_REL24:
1744 case IMAGE_REL_PPC_ADDR24:
1745 case IMAGE_REL_PPC_ADDR32:
1746 case IMAGE_REL_PPC_IFGLUE:
1747 case IMAGE_REL_PPC_ADDR32NB:
1748 case IMAGE_REL_PPC_SECTION:
1749 case IMAGE_REL_PPC_SECREL:
1750 DUMP_RELOC2 (ppc_coff_howto_table[r_type].name, internal);
1751 howto = ppc_coff_howto_table + r_type;
1753 case IMAGE_REL_PPC_IMGLUE:
1754 DUMP_RELOC2 (ppc_coff_howto_table[r_type].name, internal);
1755 howto = ppc_coff_howto_table + r_type;
1757 case IMAGE_REL_PPC_TOCREL16:
1758 DUMP_RELOC2 (ppc_coff_howto_table[r_type].name, internal);
1759 if (r_flags & IMAGE_REL_PPC_TOCDEFN)
1760 howto = ppc_coff_howto_table + IMAGE_REL_PPC_TOCREL16_DEFN;
1762 howto = ppc_coff_howto_table + IMAGE_REL_PPC_TOCREL16;
1765 (*_bfd_error_handler) (_("warning: unsupported reloc %s [%d] used -- it may not work"),
1766 ppc_coff_howto_table[r_type].name,
1768 howto = ppc_coff_howto_table + r_type;
1772 relent->howto = howto;
1775 static reloc_howto_type *
1776 coff_ppc_rtype_to_howto (bfd *abfd ATTRIBUTE_UNUSED,
1778 struct internal_reloc *rel,
1779 struct coff_link_hash_entry *h ATTRIBUTE_UNUSED,
1780 struct internal_syment *sym ATTRIBUTE_UNUSED,
1783 reloc_howto_type *howto;
1785 /* We can encode one of three things in the type field, aside from the
1787 1. IMAGE_REL_PPC_NEG - indicates the value field is a subtraction
1788 value, rather than an addition value
1789 2. IMAGE_REL_PPC_BRTAKEN, IMAGE_REL_PPC_BRNTAKEN - indicates that
1790 the branch is expected to be taken or not.
1791 3. IMAGE_REL_PPC_TOCDEFN - toc slot definition in the file
1792 For now, we just strip this stuff to find the type, and ignore it other
1795 unsigned short r_type = EXTRACT_TYPE (rel->r_type);
1796 unsigned short r_flags = EXTRACT_FLAGS (rel->r_type);
1797 unsigned short junk = EXTRACT_JUNK (rel->r_type);
1799 /* The masking process only slices off the bottom byte for r_type. */
1800 if (r_type > MAX_RELOC_INDEX)
1803 /* Check for absolute crap. */
1809 case IMAGE_REL_PPC_ADDR32NB:
1810 DUMP_RELOC2(ppc_coff_howto_table[r_type].name, rel);
1811 *addendp -= pe_data(sec->output_section->owner)->pe_opthdr.ImageBase;
1812 howto = ppc_coff_howto_table + r_type;
1814 case IMAGE_REL_PPC_TOCREL16:
1815 DUMP_RELOC2(ppc_coff_howto_table[r_type].name, rel);
1816 if (r_flags & IMAGE_REL_PPC_TOCDEFN)
1817 howto = ppc_coff_howto_table + IMAGE_REL_PPC_TOCREL16_DEFN;
1819 howto = ppc_coff_howto_table + IMAGE_REL_PPC_TOCREL16;
1821 case IMAGE_REL_PPC_ADDR16:
1822 case IMAGE_REL_PPC_REL24:
1823 case IMAGE_REL_PPC_ADDR24:
1824 case IMAGE_REL_PPC_ADDR32:
1825 case IMAGE_REL_PPC_IFGLUE:
1826 case IMAGE_REL_PPC_SECTION:
1827 case IMAGE_REL_PPC_SECREL:
1828 DUMP_RELOC2(ppc_coff_howto_table[r_type].name, rel);
1829 howto = ppc_coff_howto_table + r_type;
1831 case IMAGE_REL_PPC_IMGLUE:
1832 DUMP_RELOC2(ppc_coff_howto_table[r_type].name, rel);
1833 howto = ppc_coff_howto_table + r_type;
1836 (*_bfd_error_handler) (_("warning: unsupported reloc %s [%d] used -- it may not work"),
1837 ppc_coff_howto_table[r_type].name,
1839 howto = ppc_coff_howto_table + r_type;
1846 /* A cheesy little macro to make the code a little more readable. */
1847 #define HOW2MAP(bfd_rtype,ppc_rtype) \
1848 case bfd_rtype: return &ppc_coff_howto_table[ppc_rtype]
1850 static reloc_howto_type *
1851 ppc_coff_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
1852 bfd_reloc_code_real_type code)
1856 HOW2MAP(BFD_RELOC_32_GOTOFF, IMAGE_REL_PPC_IMGLUE);
1857 HOW2MAP(BFD_RELOC_16_GOT_PCREL, IMAGE_REL_PPC_IFGLUE);
1858 HOW2MAP(BFD_RELOC_16, IMAGE_REL_PPC_ADDR16);
1859 HOW2MAP(BFD_RELOC_PPC_B26, IMAGE_REL_PPC_REL24);
1860 HOW2MAP(BFD_RELOC_PPC_BA26, IMAGE_REL_PPC_ADDR24);
1861 HOW2MAP(BFD_RELOC_PPC_TOC16, IMAGE_REL_PPC_TOCREL16);
1862 HOW2MAP(BFD_RELOC_16_GOTOFF, IMAGE_REL_PPC_TOCREL16_DEFN);
1863 HOW2MAP(BFD_RELOC_32, IMAGE_REL_PPC_ADDR32);
1864 HOW2MAP(BFD_RELOC_RVA, IMAGE_REL_PPC_ADDR32NB);
1871 static reloc_howto_type *
1872 ppc_coff_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
1878 i < sizeof (ppc_coff_howto_table) / sizeof (ppc_coff_howto_table[0]);
1880 if (ppc_coff_howto_table[i].name != NULL
1881 && strcasecmp (ppc_coff_howto_table[i].name, r_name) == 0)
1882 return &ppc_coff_howto_table[i];
1887 /* Tailor coffcode.h -- macro heaven. */
1889 #define RTYPE2HOWTO(cache_ptr, dst) ppc_coff_rtype2howto (cache_ptr, dst)
1891 /* We use the special COFF backend linker, with our own special touch. */
1893 #define coff_bfd_reloc_type_lookup ppc_coff_reloc_type_lookup
1894 #define coff_bfd_reloc_name_lookup ppc_coff_reloc_name_lookup
1895 #define coff_rtype_to_howto coff_ppc_rtype_to_howto
1896 #define coff_relocate_section coff_ppc_relocate_section
1897 #define coff_bfd_final_link ppc_bfd_coff_final_link
1899 #ifndef COFF_IMAGE_WITH_PE
1902 #define SELECT_RELOC(internal, howto) {internal.r_type=howto->type;}
1904 #define COFF_PAGE_SIZE 0x1000
1906 /* FIXME: This controls some code that used to be in peicode.h and is
1907 now in peigen.c. It will not control the code in peigen.c. If
1908 anybody wants to get this working, you will need to fix that. */
1909 #define POWERPC_LE_PE
1911 #define COFF_SECTION_ALIGNMENT_ENTRIES \
1912 { COFF_SECTION_NAME_EXACT_MATCH (".idata$2"), \
1913 COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 0 }, \
1914 { COFF_SECTION_NAME_EXACT_MATCH (".idata$3"), \
1915 COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 0 }, \
1916 { COFF_SECTION_NAME_EXACT_MATCH (".idata$4"), \
1917 COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 2 }, \
1918 { COFF_SECTION_NAME_EXACT_MATCH (".idata$5"), \
1919 COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 2 }, \
1920 { COFF_SECTION_NAME_EXACT_MATCH (".idata$6"), \
1921 COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 1 }, \
1922 { COFF_SECTION_NAME_EXACT_MATCH (".reloc"), \
1923 COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 1 }
1925 #include "coffcode.h"
1927 #ifndef COFF_IMAGE_WITH_PE
1930 ppc_do_last (bfd *abfd)
1932 if (abfd == bfd_of_toc_owner)
1941 return bfd_of_toc_owner;
1944 /* This piece of machinery exists only to guarantee that the bfd that holds
1945 the toc section is written last.
1947 This does depend on bfd_make_section attaching a new section to the
1948 end of the section list for the bfd.
1950 This is otherwise intended to be functionally the same as
1951 cofflink.c:_bfd_coff_final_link(). It is specifically different only
1952 where the POWERPC_LE_PE macro modifies the code. It is left in as a
1953 precise form of comment. krk@cygnus.com */
1955 /* Do the final link step. */
1958 ppc_bfd_coff_final_link (bfd *abfd, struct bfd_link_info *info)
1960 bfd_size_type symesz;
1961 struct coff_final_link_info flaginfo;
1962 bfd_boolean debug_merge_allocated;
1964 struct bfd_link_order *p;
1965 bfd_size_type max_sym_count;
1966 bfd_size_type max_lineno_count;
1967 bfd_size_type max_reloc_count;
1968 bfd_size_type max_output_reloc_count;
1969 bfd_size_type max_contents_size;
1970 file_ptr rel_filepos;
1972 file_ptr line_filepos;
1973 unsigned int linesz;
1975 bfd_byte *external_relocs = NULL;
1976 char strbuf[STRING_SIZE_SIZE];
1979 symesz = bfd_coff_symesz (abfd);
1981 flaginfo.info = info;
1982 flaginfo.output_bfd = abfd;
1983 flaginfo.strtab = NULL;
1984 flaginfo.section_info = NULL;
1985 flaginfo.last_file_index = -1;
1986 flaginfo.last_bf_index = -1;
1987 flaginfo.internal_syms = NULL;
1988 flaginfo.sec_ptrs = NULL;
1989 flaginfo.sym_indices = NULL;
1990 flaginfo.outsyms = NULL;
1991 flaginfo.linenos = NULL;
1992 flaginfo.contents = NULL;
1993 flaginfo.external_relocs = NULL;
1994 flaginfo.internal_relocs = NULL;
1995 debug_merge_allocated = FALSE;
1997 coff_data (abfd)->link_info = info;
1999 flaginfo.strtab = _bfd_stringtab_init ();
2000 if (flaginfo.strtab == NULL)
2003 if (! coff_debug_merge_hash_table_init (&flaginfo.debug_merge))
2005 debug_merge_allocated = TRUE;
2007 /* Compute the file positions for all the sections. */
2008 if (! abfd->output_has_begun)
2010 if (! bfd_coff_compute_section_file_positions (abfd))
2014 /* Count the line numbers and relocation entries required for the
2015 output file. Set the file positions for the relocs. */
2016 rel_filepos = obj_relocbase (abfd);
2017 relsz = bfd_coff_relsz (abfd);
2018 max_contents_size = 0;
2019 max_lineno_count = 0;
2020 max_reloc_count = 0;
2022 for (o = abfd->sections; o != NULL; o = o->next)
2025 o->lineno_count = 0;
2027 for (p = o->map_head.link_order; p != NULL; p = p->next)
2029 if (p->type == bfd_indirect_link_order)
2033 sec = p->u.indirect.section;
2035 /* Mark all sections which are to be included in the
2036 link. This will normally be every section. We need
2037 to do this so that we can identify any sections which
2038 the linker has decided to not include. */
2039 sec->linker_mark = TRUE;
2041 if (info->strip == strip_none
2042 || info->strip == strip_some)
2043 o->lineno_count += sec->lineno_count;
2045 if (info->relocatable)
2046 o->reloc_count += sec->reloc_count;
2048 if (sec->rawsize > max_contents_size)
2049 max_contents_size = sec->rawsize;
2050 if (sec->size > max_contents_size)
2051 max_contents_size = sec->size;
2052 if (sec->lineno_count > max_lineno_count)
2053 max_lineno_count = sec->lineno_count;
2054 if (sec->reloc_count > max_reloc_count)
2055 max_reloc_count = sec->reloc_count;
2057 else if (info->relocatable
2058 && (p->type == bfd_section_reloc_link_order
2059 || p->type == bfd_symbol_reloc_link_order))
2062 if (o->reloc_count == 0)
2066 o->flags |= SEC_RELOC;
2067 o->rel_filepos = rel_filepos;
2068 rel_filepos += o->reloc_count * relsz;
2072 /* If doing a relocatable link, allocate space for the pointers we
2074 if (info->relocatable)
2078 /* We use section_count + 1, rather than section_count, because
2079 the target_index fields are 1 based. */
2080 amt = abfd->section_count + 1;
2081 amt *= sizeof (struct coff_link_section_info);
2082 flaginfo.section_info = (struct coff_link_section_info *) bfd_malloc (amt);
2084 if (flaginfo.section_info == NULL)
2087 for (i = 0; i <= abfd->section_count; i++)
2089 flaginfo.section_info[i].relocs = NULL;
2090 flaginfo.section_info[i].rel_hashes = NULL;
2094 /* We now know the size of the relocs, so we can determine the file
2095 positions of the line numbers. */
2096 line_filepos = rel_filepos;
2097 linesz = bfd_coff_linesz (abfd);
2098 max_output_reloc_count = 0;
2100 for (o = abfd->sections; o != NULL; o = o->next)
2102 if (o->lineno_count == 0)
2103 o->line_filepos = 0;
2106 o->line_filepos = line_filepos;
2107 line_filepos += o->lineno_count * linesz;
2110 if (o->reloc_count != 0)
2112 /* We don't know the indices of global symbols until we have
2113 written out all the local symbols. For each section in
2114 the output file, we keep an array of pointers to hash
2115 table entries. Each entry in the array corresponds to a
2116 reloc. When we find a reloc against a global symbol, we
2117 set the corresponding entry in this array so that we can
2118 fix up the symbol index after we have written out all the
2121 Because of this problem, we also keep the relocs in
2122 memory until the end of the link. This wastes memory,
2123 but only when doing a relocatable link, which is not the
2125 BFD_ASSERT (info->relocatable);
2126 amt = o->reloc_count;
2127 amt *= sizeof (struct internal_reloc);
2128 flaginfo.section_info[o->target_index].relocs =
2129 (struct internal_reloc *) bfd_malloc (amt);
2130 amt = o->reloc_count;
2131 amt *= sizeof (struct coff_link_hash_entry *);
2132 flaginfo.section_info[o->target_index].rel_hashes =
2133 (struct coff_link_hash_entry **) bfd_malloc (amt);
2134 if (flaginfo.section_info[o->target_index].relocs == NULL
2135 || flaginfo.section_info[o->target_index].rel_hashes == NULL)
2138 if (o->reloc_count > max_output_reloc_count)
2139 max_output_reloc_count = o->reloc_count;
2142 /* Reset the reloc and lineno counts, so that we can use them to
2143 count the number of entries we have output so far. */
2145 o->lineno_count = 0;
2148 obj_sym_filepos (abfd) = line_filepos;
2150 /* Figure out the largest number of symbols in an input BFD. Take
2151 the opportunity to clear the output_has_begun fields of all the
2154 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
2158 sub->output_has_begun = FALSE;
2159 sz = obj_raw_syment_count (sub);
2160 if (sz > max_sym_count)
2164 /* Allocate some buffers used while linking. */
2165 amt = max_sym_count * sizeof (struct internal_syment);
2166 flaginfo.internal_syms = (struct internal_syment *) bfd_malloc (amt);
2167 amt = max_sym_count * sizeof (asection *);
2168 flaginfo.sec_ptrs = (asection **) bfd_malloc (amt);
2169 amt = max_sym_count * sizeof (long);
2170 flaginfo.sym_indices = (long *) bfd_malloc (amt);
2171 amt = (max_sym_count + 1) * symesz;
2172 flaginfo.outsyms = (bfd_byte *) bfd_malloc (amt);
2173 amt = max_lineno_count * bfd_coff_linesz (abfd);
2174 flaginfo.linenos = (bfd_byte *) bfd_malloc (amt);
2175 flaginfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
2176 flaginfo.external_relocs = (bfd_byte *) bfd_malloc (max_reloc_count * relsz);
2177 if (! info->relocatable)
2179 amt = max_reloc_count * sizeof (struct internal_reloc);
2180 flaginfo.internal_relocs = (struct internal_reloc *) bfd_malloc (amt);
2182 if ((flaginfo.internal_syms == NULL && max_sym_count > 0)
2183 || (flaginfo.sec_ptrs == NULL && max_sym_count > 0)
2184 || (flaginfo.sym_indices == NULL && max_sym_count > 0)
2185 || flaginfo.outsyms == NULL
2186 || (flaginfo.linenos == NULL && max_lineno_count > 0)
2187 || (flaginfo.contents == NULL && max_contents_size > 0)
2188 || (flaginfo.external_relocs == NULL && max_reloc_count > 0)
2189 || (! info->relocatable
2190 && flaginfo.internal_relocs == NULL
2191 && max_reloc_count > 0))
2194 /* We now know the position of everything in the file, except that
2195 we don't know the size of the symbol table and therefore we don't
2196 know where the string table starts. We just build the string
2197 table in memory as we go along. We process all the relocations
2198 for a single input file at once. */
2199 obj_raw_syment_count (abfd) = 0;
2201 if (coff_backend_info (abfd)->_bfd_coff_start_final_link)
2203 if (! bfd_coff_start_final_link (abfd, info))
2207 for (o = abfd->sections; o != NULL; o = o->next)
2209 for (p = o->map_head.link_order; p != NULL; p = p->next)
2211 if (p->type == bfd_indirect_link_order
2212 && (bfd_get_flavour (p->u.indirect.section->owner)
2213 == bfd_target_coff_flavour))
2215 sub = p->u.indirect.section->owner;
2216 #ifdef POWERPC_LE_PE
2217 if (! sub->output_has_begun && !ppc_do_last(sub))
2219 if (! sub->output_has_begun)
2222 if (! _bfd_coff_link_input_bfd (&flaginfo, sub))
2224 sub->output_has_begun = TRUE;
2227 else if (p->type == bfd_section_reloc_link_order
2228 || p->type == bfd_symbol_reloc_link_order)
2230 if (! _bfd_coff_reloc_link_order (abfd, &flaginfo, o, p))
2235 if (! _bfd_default_link_order (abfd, info, o, p))
2241 #ifdef POWERPC_LE_PE
2243 bfd* last_one = ppc_get_last();
2246 if (! _bfd_coff_link_input_bfd (&flaginfo, last_one))
2249 last_one->output_has_begun = TRUE;
2253 /* Free up the buffers used by _bfd_coff_link_input_bfd. */
2254 coff_debug_merge_hash_table_free (&flaginfo.debug_merge);
2255 debug_merge_allocated = FALSE;
2257 if (flaginfo.internal_syms != NULL)
2259 free (flaginfo.internal_syms);
2260 flaginfo.internal_syms = NULL;
2262 if (flaginfo.sec_ptrs != NULL)
2264 free (flaginfo.sec_ptrs);
2265 flaginfo.sec_ptrs = NULL;
2267 if (flaginfo.sym_indices != NULL)
2269 free (flaginfo.sym_indices);
2270 flaginfo.sym_indices = NULL;
2272 if (flaginfo.linenos != NULL)
2274 free (flaginfo.linenos);
2275 flaginfo.linenos = NULL;
2277 if (flaginfo.contents != NULL)
2279 free (flaginfo.contents);
2280 flaginfo.contents = NULL;
2282 if (flaginfo.external_relocs != NULL)
2284 free (flaginfo.external_relocs);
2285 flaginfo.external_relocs = NULL;
2287 if (flaginfo.internal_relocs != NULL)
2289 free (flaginfo.internal_relocs);
2290 flaginfo.internal_relocs = NULL;
2293 /* The value of the last C_FILE symbol is supposed to be the symbol
2294 index of the first external symbol. Write it out again if
2296 if (flaginfo.last_file_index != -1
2297 && (unsigned int) flaginfo.last_file.n_value != obj_raw_syment_count (abfd))
2301 flaginfo.last_file.n_value = obj_raw_syment_count (abfd);
2302 bfd_coff_swap_sym_out (abfd, &flaginfo.last_file,
2304 pos = obj_sym_filepos (abfd) + flaginfo.last_file_index * symesz;
2305 if (bfd_seek (abfd, pos, SEEK_SET) != 0
2306 || bfd_bwrite (flaginfo.outsyms, symesz, abfd) != symesz)
2310 /* Write out the global symbols. */
2311 flaginfo.failed = FALSE;
2312 bfd_hash_traverse (&info->hash->table, _bfd_coff_write_global_sym, &flaginfo);
2313 if (flaginfo.failed)
2316 /* The outsyms buffer is used by _bfd_coff_write_global_sym. */
2317 if (flaginfo.outsyms != NULL)
2319 free (flaginfo.outsyms);
2320 flaginfo.outsyms = NULL;
2323 if (info->relocatable)
2325 /* Now that we have written out all the global symbols, we know
2326 the symbol indices to use for relocs against them, and we can
2327 finally write out the relocs. */
2328 amt = max_output_reloc_count * relsz;
2329 external_relocs = (bfd_byte *) bfd_malloc (amt);
2330 if (external_relocs == NULL)
2333 for (o = abfd->sections; o != NULL; o = o->next)
2335 struct internal_reloc *irel;
2336 struct internal_reloc *irelend;
2337 struct coff_link_hash_entry **rel_hash;
2340 if (o->reloc_count == 0)
2343 irel = flaginfo.section_info[o->target_index].relocs;
2344 irelend = irel + o->reloc_count;
2345 rel_hash = flaginfo.section_info[o->target_index].rel_hashes;
2346 erel = external_relocs;
2347 for (; irel < irelend; irel++, rel_hash++, erel += relsz)
2349 if (*rel_hash != NULL)
2351 BFD_ASSERT ((*rel_hash)->indx >= 0);
2352 irel->r_symndx = (*rel_hash)->indx;
2354 bfd_coff_swap_reloc_out (abfd, irel, erel);
2357 amt = relsz * o->reloc_count;
2358 if (bfd_seek (abfd, o->rel_filepos, SEEK_SET) != 0
2359 || bfd_bwrite (external_relocs, amt, abfd) != amt)
2363 free (external_relocs);
2364 external_relocs = NULL;
2367 /* Free up the section information. */
2368 if (flaginfo.section_info != NULL)
2372 for (i = 0; i < abfd->section_count; i++)
2374 if (flaginfo.section_info[i].relocs != NULL)
2375 free (flaginfo.section_info[i].relocs);
2376 if (flaginfo.section_info[i].rel_hashes != NULL)
2377 free (flaginfo.section_info[i].rel_hashes);
2379 free (flaginfo.section_info);
2380 flaginfo.section_info = NULL;
2383 /* If we have optimized stabs strings, output them. */
2384 if (coff_hash_table (info)->stab_info.stabstr != NULL)
2386 if (! _bfd_write_stab_strings (abfd, &coff_hash_table (info)->stab_info))
2390 /* Write out the string table. */
2391 if (obj_raw_syment_count (abfd) != 0)
2395 pos = obj_sym_filepos (abfd) + obj_raw_syment_count (abfd) * symesz;
2396 if (bfd_seek (abfd, pos, SEEK_SET) != 0)
2399 #if STRING_SIZE_SIZE == 4
2401 _bfd_stringtab_size (flaginfo.strtab) + STRING_SIZE_SIZE,
2404 #error Change H_PUT_32 above
2407 if (bfd_bwrite (strbuf, (bfd_size_type) STRING_SIZE_SIZE, abfd)
2408 != STRING_SIZE_SIZE)
2411 if (! _bfd_stringtab_emit (abfd, flaginfo.strtab))
2415 _bfd_stringtab_free (flaginfo.strtab);
2417 /* Setting bfd_get_symcount to 0 will cause write_object_contents to
2418 not try to write out the symbols. */
2419 bfd_get_symcount (abfd) = 0;
2424 if (debug_merge_allocated)
2425 coff_debug_merge_hash_table_free (&flaginfo.debug_merge);
2426 if (flaginfo.strtab != NULL)
2427 _bfd_stringtab_free (flaginfo.strtab);
2428 if (flaginfo.section_info != NULL)
2432 for (i = 0; i < abfd->section_count; i++)
2434 if (flaginfo.section_info[i].relocs != NULL)
2435 free (flaginfo.section_info[i].relocs);
2436 if (flaginfo.section_info[i].rel_hashes != NULL)
2437 free (flaginfo.section_info[i].rel_hashes);
2439 free (flaginfo.section_info);
2441 if (flaginfo.internal_syms != NULL)
2442 free (flaginfo.internal_syms);
2443 if (flaginfo.sec_ptrs != NULL)
2444 free (flaginfo.sec_ptrs);
2445 if (flaginfo.sym_indices != NULL)
2446 free (flaginfo.sym_indices);
2447 if (flaginfo.outsyms != NULL)
2448 free (flaginfo.outsyms);
2449 if (flaginfo.linenos != NULL)
2450 free (flaginfo.linenos);
2451 if (flaginfo.contents != NULL)
2452 free (flaginfo.contents);
2453 if (flaginfo.external_relocs != NULL)
2454 free (flaginfo.external_relocs);
2455 if (flaginfo.internal_relocs != NULL)
2456 free (flaginfo.internal_relocs);
2457 if (external_relocs != NULL)
2458 free (external_relocs);
2463 /* Forward declaration for use by alternative_target field. */
2464 #ifdef TARGET_BIG_SYM
2465 extern const bfd_target TARGET_BIG_SYM;
2468 /* The transfer vectors that lead the outside world to all of the above. */
2470 #ifdef TARGET_LITTLE_SYM
2471 const bfd_target TARGET_LITTLE_SYM =
2473 TARGET_LITTLE_NAME, /* name or coff-arm-little */
2474 bfd_target_coff_flavour,
2475 BFD_ENDIAN_LITTLE, /* data byte order is little */
2476 BFD_ENDIAN_LITTLE, /* header byte order is little */
2478 (HAS_RELOC | EXEC_P | /* FIXME: object flags */
2479 HAS_LINENO | HAS_DEBUG |
2480 HAS_SYMS | HAS_LOCALS | WP_TEXT | D_PAGED),
2482 #ifndef COFF_WITH_PE
2483 (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY
2484 | SEC_RELOC), /* section flags */
2486 (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY
2487 | SEC_RELOC | SEC_LINK_ONCE | SEC_LINK_DUPLICATES),
2490 0, /* leading char */
2491 '/', /* ar_pad_char */
2492 15, /* ar_max_namelen??? FIXMEmgo */
2493 0, /* match priority. */
2495 bfd_getl64, bfd_getl_signed_64, bfd_putl64,
2496 bfd_getl32, bfd_getl_signed_32, bfd_putl32,
2497 bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* data */
2499 bfd_getl64, bfd_getl_signed_64, bfd_putl64,
2500 bfd_getl32, bfd_getl_signed_32, bfd_putl32,
2501 bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* hdrs */
2503 {_bfd_dummy_target, coff_object_p, /* bfd_check_format */
2504 bfd_generic_archive_p, /* _bfd_dummy_target */ coff_object_p },
2505 {bfd_false, coff_mkobject, _bfd_generic_mkarchive, /* bfd_set_format */
2507 {bfd_false, coff_write_object_contents, /* bfd_write_contents */
2508 _bfd_write_archive_contents, bfd_false},
2510 BFD_JUMP_TABLE_GENERIC (coff),
2511 BFD_JUMP_TABLE_COPY (coff),
2512 BFD_JUMP_TABLE_CORE (_bfd_nocore),
2513 BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_coff),
2514 BFD_JUMP_TABLE_SYMBOLS (coff),
2515 BFD_JUMP_TABLE_RELOCS (coff),
2516 BFD_JUMP_TABLE_WRITE (coff),
2517 BFD_JUMP_TABLE_LINK (coff),
2518 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
2520 /* Alternative_target. */
2521 #ifdef TARGET_BIG_SYM
2531 #ifdef TARGET_BIG_SYM
2532 const bfd_target TARGET_BIG_SYM =
2535 bfd_target_coff_flavour,
2536 BFD_ENDIAN_BIG, /* data byte order is big */
2537 BFD_ENDIAN_BIG, /* header byte order is big */
2539 (HAS_RELOC | EXEC_P | /* FIXME: object flags */
2540 HAS_LINENO | HAS_DEBUG |
2541 HAS_SYMS | HAS_LOCALS | WP_TEXT | D_PAGED),
2543 #ifndef COFF_WITH_PE
2544 (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY
2545 | SEC_RELOC), /* section flags */
2547 (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY
2548 | SEC_RELOC | SEC_LINK_ONCE | SEC_LINK_DUPLICATES),
2551 0, /* leading char */
2552 '/', /* ar_pad_char */
2553 15, /* ar_max_namelen??? FIXMEmgo */
2554 0, /* match priority. */
2556 bfd_getb64, bfd_getb_signed_64, bfd_putb64,
2557 bfd_getb32, bfd_getb_signed_32, bfd_putb32,
2558 bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* data */
2560 bfd_getb64, bfd_getb_signed_64, bfd_putb64,
2561 bfd_getb32, bfd_getb_signed_32, bfd_putb32,
2562 bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* hdrs */
2564 {_bfd_dummy_target, coff_object_p, /* bfd_check_format */
2565 bfd_generic_archive_p, /* _bfd_dummy_target */ coff_object_p },
2566 {bfd_false, coff_mkobject, _bfd_generic_mkarchive, /* bfd_set_format */
2568 {bfd_false, coff_write_object_contents, /* bfd_write_contents */
2569 _bfd_write_archive_contents, bfd_false},
2571 BFD_JUMP_TABLE_GENERIC (coff),
2572 BFD_JUMP_TABLE_COPY (coff),
2573 BFD_JUMP_TABLE_CORE (_bfd_nocore),
2574 BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_coff),
2575 BFD_JUMP_TABLE_SYMBOLS (coff),
2576 BFD_JUMP_TABLE_RELOCS (coff),
2577 BFD_JUMP_TABLE_WRITE (coff),
2578 BFD_JUMP_TABLE_LINK (coff),
2579 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
2581 /* Alternative_target. */
2582 #ifdef TARGET_LITTLE_SYM
2583 & TARGET_LITTLE_SYM,