1 /* BFD back-end for PowerPC Microsoft Portable Executable files.
2 Copyright (C) 1990-2017 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 3 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, 51 Franklin Street - Fifth Floor,
25 Boston, MA 02110-1301, USA. */
29 - relocs generated by gas
30 - ld will link files, but they do not run.
31 - dlltool will not produce correct output in some .reloc cases, and will
32 not produce the right glue code for dll function calls. */
38 #include "coff/powerpc.h"
39 #include "coff/internal.h"
47 #define BADMAG(x) PPCBADMAG(x)
51 /* This file is compiled more than once, but we only compile the
52 final_link routine once. */
53 extern bfd_boolean ppc_bfd_coff_final_link (bfd *, struct bfd_link_info *);
54 extern void dump_toc (void *);
56 /* The toc is a set of bfd_vma fields. We use the fact that valid
57 addresses are even (i.e. the bit representing "1" is off) to allow
58 us to encode a little extra information in the field
59 - Unallocated addresses are initialized to 1.
60 - Allocated addresses are even numbers.
61 The first time we actually write a reference to the toc in the bfd,
62 we want to record that fact in a fixup file (if it is asked for), so
63 we keep track of whether or not an address has been written by marking
64 the low order bit with a "1" upon writing. */
66 #define SET_UNALLOCATED(x) ((x) = 1)
67 #define IS_UNALLOCATED(x) ((x) == 1)
69 #define IS_WRITTEN(x) ((x) & 1)
70 #define MARK_AS_WRITTEN(x) ((x) |= 1)
71 #define MAKE_ADDR_AGAIN(x) ((x) &= ~1)
73 /* Turn on this check if you suspect something amiss in the hash tables. */
76 /* Need a 7 char string for an eye catcher. */
79 #define HASH_CHECK_DCL char eye_catcher[8];
80 #define HASH_CHECK_INIT(ret) strcpy(ret->eye_catcher, EYE)
81 #define HASH_CHECK(addr) \
82 if (strcmp (addr->eye_catcher, EYE) != 0) \
85 /* xgettext: c-format */ \
86 _("File %s, line %d, Hash check failure, bad eye %8s\n"), \
87 __FILE__, __LINE__, addr->eye_catcher); \
93 #define HASH_CHECK_DCL
94 #define HASH_CHECK_INIT(ret)
95 #define HASH_CHECK(addr)
99 /* In order not to add an int to every hash table item for every coff
100 linker, we define our own hash table, derived from the coff one. */
102 /* PE linker hash table entries. */
104 struct ppc_coff_link_hash_entry
106 struct coff_link_hash_entry root; /* First entry, as required. */
108 /* As we wonder around the relocs, we'll keep the assigned toc_offset
110 bfd_vma toc_offset; /* Our addition, as required. */
112 unsigned long int glue_insn;
117 /* PE linker hash table. */
119 struct ppc_coff_link_hash_table
121 struct coff_link_hash_table root; /* First entry, as required. */
124 /* Routine to create an entry in the link hash table. */
126 static struct bfd_hash_entry *
127 ppc_coff_link_hash_newfunc (struct bfd_hash_entry * entry,
128 struct bfd_hash_table * table,
131 struct ppc_coff_link_hash_entry *ret =
132 (struct ppc_coff_link_hash_entry *) entry;
134 /* Allocate the structure if it has not already been allocated by a
136 if (ret == (struct ppc_coff_link_hash_entry *) NULL)
137 ret = (struct ppc_coff_link_hash_entry *)
138 bfd_hash_allocate (table,
139 sizeof (struct ppc_coff_link_hash_entry));
141 if (ret == (struct ppc_coff_link_hash_entry *) NULL)
144 /* Call the allocation method of the superclass. */
145 ret = ((struct ppc_coff_link_hash_entry *)
146 _bfd_coff_link_hash_newfunc ((struct bfd_hash_entry *) ret,
151 /* Initialize the local fields. */
152 SET_UNALLOCATED (ret->toc_offset);
153 ret->symbol_is_glue = 0;
156 HASH_CHECK_INIT (ret);
159 return (struct bfd_hash_entry *) ret;
162 /* Initialize a PE linker hash table. */
165 ppc_coff_link_hash_table_init (struct ppc_coff_link_hash_table *table,
167 struct bfd_hash_entry *(*newfunc)
168 (struct bfd_hash_entry *,
169 struct bfd_hash_table *,
171 unsigned int entsize)
173 return _bfd_coff_link_hash_table_init (&table->root, abfd, newfunc, entsize);
176 /* Create a PE linker hash table. */
178 static struct bfd_link_hash_table *
179 ppc_coff_link_hash_table_create (bfd *abfd)
181 struct ppc_coff_link_hash_table *ret;
182 bfd_size_type amt = sizeof (struct ppc_coff_link_hash_table);
184 ret = (struct ppc_coff_link_hash_table *) bfd_malloc (amt);
187 if (!ppc_coff_link_hash_table_init (ret, abfd,
188 ppc_coff_link_hash_newfunc,
189 sizeof (struct ppc_coff_link_hash_entry)))
192 return (struct bfd_link_hash_table *) NULL;
194 return &ret->root.root;
197 /* Now, tailor coffcode.h to use our hash stuff. */
199 #define coff_bfd_link_hash_table_create ppc_coff_link_hash_table_create
201 /* The nt loader points the toc register to &toc + 32768, in order to
202 use the complete range of a 16-bit displacement. We have to adjust
203 for this when we fix up loads displaced off the toc reg. */
204 #define TOC_LOAD_ADJUSTMENT (-32768)
205 #define TOC_SECTION_NAME ".private.toc"
207 /* The main body of code is in coffcode.h. */
209 #define COFF_DEFAULT_SECTION_ALIGNMENT_POWER (3)
211 /* In case we're on a 32-bit machine, construct a 64-bit "-1" value
212 from smaller values. Start with zero, widen, *then* decrement. */
213 #define MINUS_ONE (((bfd_vma)0) - 1)
215 /* These should definitely go in a header file somewhere... */
218 #define IMAGE_REL_PPC_ABSOLUTE 0x0000
221 #define IMAGE_REL_PPC_ADDR64 0x0001
224 #define IMAGE_REL_PPC_ADDR32 0x0002
226 /* 26-bit address, shifted left 2 (branch absolute) */
227 #define IMAGE_REL_PPC_ADDR24 0x0003
230 #define IMAGE_REL_PPC_ADDR16 0x0004
232 /* 16-bit address, shifted left 2 (load doubleword) */
233 #define IMAGE_REL_PPC_ADDR14 0x0005
235 /* 26-bit PC-relative offset, shifted left 2 (branch relative) */
236 #define IMAGE_REL_PPC_REL24 0x0006
238 /* 16-bit PC-relative offset, shifted left 2 (br cond relative) */
239 #define IMAGE_REL_PPC_REL14 0x0007
241 /* 16-bit offset from TOC base */
242 #define IMAGE_REL_PPC_TOCREL16 0x0008
244 /* 16-bit offset from TOC base, shifted left 2 (load doubleword) */
245 #define IMAGE_REL_PPC_TOCREL14 0x0009
247 /* 32-bit addr w/o image base */
248 #define IMAGE_REL_PPC_ADDR32NB 0x000A
250 /* va of containing section (as in an image sectionhdr) */
251 #define IMAGE_REL_PPC_SECREL 0x000B
253 /* sectionheader number */
254 #define IMAGE_REL_PPC_SECTION 0x000C
256 /* substitute TOC restore instruction iff symbol is glue code */
257 #define IMAGE_REL_PPC_IFGLUE 0x000D
259 /* symbol is glue code; virtual address is TOC restore instruction */
260 #define IMAGE_REL_PPC_IMGLUE 0x000E
262 /* va of containing section (limited to 16 bits) */
263 #define IMAGE_REL_PPC_SECREL16 0x000F
265 /* Stuff to handle immediate data when the number of bits in the
266 data is greater than the number of bits in the immediate field
267 We need to do (usually) 32 bit arithmetic on 16 bit chunks. */
268 #define IMAGE_REL_PPC_REFHI 0x0010
269 #define IMAGE_REL_PPC_REFLO 0x0011
270 #define IMAGE_REL_PPC_PAIR 0x0012
272 /* This is essentially the same as tocrel16, with TOCDEFN assumed. */
273 #define IMAGE_REL_PPC_TOCREL16_DEFN 0x0013
275 /* Flag bits in IMAGE_RELOCATION.TYPE. */
277 /* Subtract reloc value rather than adding it. */
278 #define IMAGE_REL_PPC_NEG 0x0100
280 /* Fix branch prediction bit to predict branch taken. */
281 #define IMAGE_REL_PPC_BRTAKEN 0x0200
283 /* Fix branch prediction bit to predict branch not taken. */
284 #define IMAGE_REL_PPC_BRNTAKEN 0x0400
286 /* TOC slot defined in file (or, data in toc). */
287 #define IMAGE_REL_PPC_TOCDEFN 0x0800
289 /* Masks to isolate above values in IMAGE_RELOCATION.Type. */
290 #define IMAGE_REL_PPC_TYPEMASK 0x00FF
291 #define IMAGE_REL_PPC_FLAGMASK 0x0F00
293 #define EXTRACT_TYPE(x) ((x) & IMAGE_REL_PPC_TYPEMASK)
294 #define EXTRACT_FLAGS(x) ((x) & IMAGE_REL_PPC_FLAGMASK)
295 #define EXTRACT_JUNK(x) \
296 ((x) & ~(IMAGE_REL_PPC_TYPEMASK | IMAGE_REL_PPC_FLAGMASK))
298 /* Static helper functions to make relocation work. */
299 /* (Work In Progress) */
301 static bfd_reloc_status_type ppc_refhi_reloc
302 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
303 static bfd_reloc_status_type ppc_pair_reloc
304 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
305 static bfd_reloc_status_type ppc_toc16_reloc
306 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
307 static bfd_reloc_status_type ppc_section_reloc
308 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
309 static bfd_reloc_status_type ppc_secrel_reloc
310 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
311 static bfd_reloc_status_type ppc_imglue_reloc
312 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
314 /* FIXME: It'll take a while to get through all of these. I only need a few to
315 get us started, so those I'll make sure work. Those marked FIXME are either
316 completely unverified or have a specific unknown marked in the comment. */
318 /* Relocation entries for Windows/NT on PowerPC.
320 From the document "" we find the following listed as used relocs:
323 ADDR[64|32|16] : fields that hold addresses in data fields or the
324 16 bit displacement field on a load/store.
325 ADDR[24|14] : fields that hold addresses in branch and cond
326 branches. These represent [26|16] bit addresses.
327 The low order 2 bits are preserved.
328 REL[24|14] : branches relative to the Instruction Address
329 register. These represent [26|16] bit addresses,
330 as before. The instruction field will be zero, and
331 the address of the SYM will be inserted at link time.
332 TOCREL16 : 16 bit displacement field referring to a slot in
334 TOCREL14 : 16 bit displacement field, similar to REL14 or ADDR14.
335 ADDR32NB : 32 bit address relative to the virtual origin.
336 (On the alpha, this is always a linker generated thunk)
337 (i.e. 32bit addr relative to the image base)
338 SECREL : The value is relative to the start of the section
339 containing the symbol.
340 SECTION : access to the header containing the item. Supports the
343 In particular, note that the document does not indicate that the
344 relocations listed in the header file are used. */
347 static reloc_howto_type ppc_coff_howto_table[] =
349 /* IMAGE_REL_PPC_ABSOLUTE 0x0000 NOP */
351 HOWTO (IMAGE_REL_PPC_ABSOLUTE, /* type */
353 0, /* size (0 = byte, 1 = short, 2 = long) */
355 FALSE, /* pc_relative */
357 complain_overflow_dont, /* dont complain_on_overflow */
358 0, /* special_function */
359 "ABSOLUTE", /* name */
360 FALSE, /* partial_inplace */
363 FALSE), /* pcrel_offset */
365 /* IMAGE_REL_PPC_ADDR64 0x0001 64-bit address */
367 HOWTO(IMAGE_REL_PPC_ADDR64, /* type */
369 3, /* size (0 = byte, 1 = short, 2 = long) */
371 FALSE, /* pc_relative */
373 complain_overflow_bitfield, /* complain_on_overflow */
374 0, /* special_function */
376 TRUE, /* partial_inplace */
377 MINUS_ONE, /* src_mask */
378 MINUS_ONE, /* dst_mask */
379 FALSE), /* pcrel_offset */
381 /* IMAGE_REL_PPC_ADDR32 0x0002 32-bit address */
383 HOWTO (IMAGE_REL_PPC_ADDR32, /* type */
385 2, /* size (0 = byte, 1 = short, 2 = long) */
387 FALSE, /* pc_relative */
389 complain_overflow_bitfield, /* complain_on_overflow */
390 0, /* special_function */
392 TRUE, /* partial_inplace */
393 0xffffffff, /* src_mask */
394 0xffffffff, /* dst_mask */
395 FALSE), /* pcrel_offset */
397 /* IMAGE_REL_PPC_ADDR24 0x0003 26-bit address, shifted left 2 (branch absolute) */
398 /* the LI field is in bit 6 through bit 29 is 24 bits, + 2 for the shift */
399 /* Of course, That's the IBM approved bit numbering, which is not what */
400 /* anyone else uses.... The li field is in bit 2 thru 25 */
402 HOWTO (IMAGE_REL_PPC_ADDR24, /* type */
404 2, /* size (0 = byte, 1 = short, 2 = long) */
406 FALSE, /* pc_relative */
408 complain_overflow_bitfield, /* complain_on_overflow */
409 0, /* special_function */
411 TRUE, /* partial_inplace */
412 0x07fffffc, /* src_mask */
413 0x07fffffc, /* dst_mask */
414 FALSE), /* pcrel_offset */
416 /* IMAGE_REL_PPC_ADDR16 0x0004 16-bit address */
418 HOWTO (IMAGE_REL_PPC_ADDR16, /* type */
420 1, /* size (0 = byte, 1 = short, 2 = long) */
422 FALSE, /* pc_relative */
424 complain_overflow_signed, /* complain_on_overflow */
425 0, /* special_function */
427 TRUE, /* partial_inplace */
428 0xffff, /* src_mask */
429 0xffff, /* dst_mask */
430 FALSE), /* pcrel_offset */
432 /* IMAGE_REL_PPC_ADDR14 0x0005 */
433 /* 16-bit address, shifted left 2 (load doubleword) */
434 /* FIXME: the mask is likely wrong, and the bit position may be as well */
436 HOWTO (IMAGE_REL_PPC_ADDR14, /* type */
438 1, /* size (0 = byte, 1 = short, 2 = long) */
440 FALSE, /* pc_relative */
442 complain_overflow_signed, /* complain_on_overflow */
443 0, /* special_function */
445 TRUE, /* partial_inplace */
446 0xffff, /* src_mask */
447 0xffff, /* dst_mask */
448 FALSE), /* pcrel_offset */
450 /* IMAGE_REL_PPC_REL24 0x0006 */
451 /* 26-bit PC-relative offset, shifted left 2 (branch relative) */
453 HOWTO (IMAGE_REL_PPC_REL24, /* type */
455 2, /* size (0 = byte, 1 = short, 2 = long) */
457 TRUE, /* pc_relative */
459 complain_overflow_signed, /* complain_on_overflow */
460 0, /* special_function */
462 TRUE, /* partial_inplace */
463 0x3fffffc, /* src_mask */
464 0x3fffffc, /* dst_mask */
465 FALSE), /* pcrel_offset */
467 /* IMAGE_REL_PPC_REL14 0x0007 */
468 /* 16-bit PC-relative offset, shifted left 2 (br cond relative) */
469 /* FIXME: the mask is likely wrong, and the bit position may be as well */
470 /* FIXME: how does it know how far to shift? */
472 HOWTO (IMAGE_REL_PPC_ADDR14, /* type */
474 1, /* size (0 = byte, 1 = short, 2 = long) */
476 FALSE, /* pc_relative */
478 complain_overflow_signed, /* complain_on_overflow */
479 0, /* special_function */
481 TRUE, /* partial_inplace */
482 0xffff, /* src_mask */
483 0xffff, /* dst_mask */
484 TRUE), /* pcrel_offset */
486 /* IMAGE_REL_PPC_TOCREL16 0x0008 */
487 /* 16-bit offset from TOC base */
489 HOWTO (IMAGE_REL_PPC_TOCREL16,/* type */
491 1, /* size (0 = byte, 1 = short, 2 = long) */
493 FALSE, /* pc_relative */
495 complain_overflow_dont, /* complain_on_overflow */
496 ppc_toc16_reloc, /* special_function */
497 "TOCREL16", /* name */
498 FALSE, /* partial_inplace */
499 0xffff, /* src_mask */
500 0xffff, /* dst_mask */
501 FALSE), /* pcrel_offset */
503 /* IMAGE_REL_PPC_TOCREL14 0x0009 */
504 /* 16-bit offset from TOC base, shifted left 2 (load doubleword) */
506 HOWTO (IMAGE_REL_PPC_TOCREL14,/* type */
508 1, /* size (0 = byte, 1 = short, 2 = long) */
510 FALSE, /* pc_relative */
512 complain_overflow_signed, /* complain_on_overflow */
513 0, /* special_function */
514 "TOCREL14", /* name */
515 FALSE, /* partial_inplace */
516 0xffff, /* src_mask */
517 0xffff, /* dst_mask */
518 FALSE), /* pcrel_offset */
520 /* IMAGE_REL_PPC_ADDR32NB 0x000A */
521 /* 32-bit addr w/ image base */
523 HOWTO (IMAGE_REL_PPC_ADDR32NB,/* type */
525 2, /* size (0 = byte, 1 = short, 2 = long) */
527 FALSE, /* pc_relative */
529 complain_overflow_signed, /* complain_on_overflow */
530 0, /* special_function */
531 "ADDR32NB", /* name */
532 TRUE, /* partial_inplace */
533 0xffffffff, /* src_mask */
534 0xffffffff, /* dst_mask */
535 FALSE), /* pcrel_offset */
537 /* IMAGE_REL_PPC_SECREL 0x000B */
538 /* va of containing section (as in an image sectionhdr) */
540 HOWTO (IMAGE_REL_PPC_SECREL,/* type */
542 2, /* size (0 = byte, 1 = short, 2 = long) */
544 FALSE, /* pc_relative */
546 complain_overflow_signed, /* complain_on_overflow */
547 ppc_secrel_reloc, /* special_function */
549 TRUE, /* partial_inplace */
550 0xffffffff, /* src_mask */
551 0xffffffff, /* dst_mask */
552 TRUE), /* pcrel_offset */
554 /* IMAGE_REL_PPC_SECTION 0x000C */
555 /* sectionheader number */
557 HOWTO (IMAGE_REL_PPC_SECTION,/* type */
559 2, /* size (0 = byte, 1 = short, 2 = long) */
561 FALSE, /* pc_relative */
563 complain_overflow_signed, /* complain_on_overflow */
564 ppc_section_reloc, /* special_function */
565 "SECTION", /* name */
566 TRUE, /* partial_inplace */
567 0xffffffff, /* src_mask */
568 0xffffffff, /* dst_mask */
569 TRUE), /* pcrel_offset */
571 /* IMAGE_REL_PPC_IFGLUE 0x000D */
572 /* substitute TOC restore instruction iff symbol is glue code */
574 HOWTO (IMAGE_REL_PPC_IFGLUE,/* type */
576 2, /* size (0 = byte, 1 = short, 2 = long) */
578 FALSE, /* pc_relative */
580 complain_overflow_signed, /* complain_on_overflow */
581 0, /* special_function */
583 TRUE, /* partial_inplace */
584 0xffffffff, /* src_mask */
585 0xffffffff, /* dst_mask */
586 FALSE), /* pcrel_offset */
588 /* IMAGE_REL_PPC_IMGLUE 0x000E */
589 /* symbol is glue code; virtual address is TOC restore instruction */
591 HOWTO (IMAGE_REL_PPC_IMGLUE,/* type */
593 2, /* size (0 = byte, 1 = short, 2 = long) */
595 FALSE, /* pc_relative */
597 complain_overflow_dont, /* complain_on_overflow */
598 ppc_imglue_reloc, /* special_function */
600 FALSE, /* partial_inplace */
601 0xffffffff, /* src_mask */
602 0xffffffff, /* dst_mask */
603 FALSE), /* pcrel_offset */
605 /* IMAGE_REL_PPC_SECREL16 0x000F */
606 /* va of containing section (limited to 16 bits) */
608 HOWTO (IMAGE_REL_PPC_SECREL16,/* type */
610 1, /* size (0 = byte, 1 = short, 2 = long) */
612 FALSE, /* pc_relative */
614 complain_overflow_signed, /* complain_on_overflow */
615 0, /* special_function */
616 "SECREL16", /* name */
617 TRUE, /* partial_inplace */
618 0xffff, /* src_mask */
619 0xffff, /* dst_mask */
620 TRUE), /* pcrel_offset */
622 /* IMAGE_REL_PPC_REFHI 0x0010 */
624 HOWTO (IMAGE_REL_PPC_REFHI, /* type */
626 1, /* size (0 = byte, 1 = short, 2 = long) */
628 FALSE, /* pc_relative */
630 complain_overflow_signed, /* complain_on_overflow */
631 ppc_refhi_reloc, /* special_function */
633 TRUE, /* partial_inplace */
634 0xffffffff, /* src_mask */
635 0xffffffff, /* dst_mask */
636 FALSE), /* pcrel_offset */
638 /* IMAGE_REL_PPC_REFLO 0x0011 */
640 HOWTO (IMAGE_REL_PPC_REFLO, /* type */
642 1, /* size (0 = byte, 1 = short, 2 = long) */
644 FALSE, /* pc_relative */
646 complain_overflow_signed, /* complain_on_overflow */
647 ppc_refhi_reloc, /* special_function */
649 TRUE, /* partial_inplace */
650 0xffffffff, /* src_mask */
651 0xffffffff, /* dst_mask */
652 FALSE), /* pcrel_offset */
654 /* IMAGE_REL_PPC_PAIR 0x0012 */
656 HOWTO (IMAGE_REL_PPC_PAIR, /* type */
658 1, /* size (0 = byte, 1 = short, 2 = long) */
660 FALSE, /* pc_relative */
662 complain_overflow_signed, /* complain_on_overflow */
663 ppc_pair_reloc, /* special_function */
665 TRUE, /* partial_inplace */
666 0xffffffff, /* src_mask */
667 0xffffffff, /* dst_mask */
668 FALSE), /* pcrel_offset */
670 /* IMAGE_REL_PPC_TOCREL16_DEFN 0x0013 */
671 /* 16-bit offset from TOC base, without causing a definition */
673 HOWTO ( (IMAGE_REL_PPC_TOCREL16 | IMAGE_REL_PPC_TOCDEFN), /* type */
675 1, /* size (0 = byte, 1 = short, 2 = long) */
677 FALSE, /* pc_relative */
679 complain_overflow_dont, /* complain_on_overflow */
680 0, /* special_function */
681 "TOCREL16, TOCDEFN", /* name */
682 FALSE, /* partial_inplace */
683 0xffff, /* src_mask */
684 0xffff, /* dst_mask */
685 FALSE), /* pcrel_offset */
689 /* Some really cheezy macros that can be turned on to test stderr :-) */
698 fprintf (stderr,_("Unimplemented Relocation -- %s\n"),x); \
702 #define DUMP_RELOC(n,r) \
704 fprintf (stderr,"%s sym %d, addr %d, addend %d\n", \
705 n, (*(r->sym_ptr_ptr))->name, \
706 r->address, r->addend); \
709 /* Given a reloc name, n, and a pointer to an internal_reloc,
710 dump out interesting information on the contents
712 #define n_name _n._n_name
713 #define n_zeroes _n._n_n._n_zeroes
714 #define n_offset _n._n_n._n_offset */
716 #define DUMP_RELOC2(n,r) \
718 fprintf (stderr,"%s sym %d, r_vaddr %d %s\n", \
719 n, r->r_symndx, r->r_vaddr, \
720 (((r->r_type) & IMAGE_REL_PPC_TOCDEFN) == 0) \
726 #define DUMP_RELOC(n,r)
727 #define DUMP_RELOC2(n,r)
730 /* TOC construction and management routines. */
732 /* This file is compiled twice, and these variables are defined in one
733 of the compilations. FIXME: This is confusing and weird. Also,
734 BFD should not use global variables. */
735 extern bfd * bfd_of_toc_owner;
736 extern long int global_toc_size;
737 extern long int import_table_size;
738 extern long int first_thunk_address;
739 extern long int thunk_size;
757 struct list_ele *next;
759 enum ref_category cat;
764 extern struct list_ele *head;
765 extern struct list_ele *tail;
768 record_toc (asection *toc_section,
769 bfd_signed_vma our_toc_offset,
770 enum ref_category cat,
773 /* Add this entry to our toc addr-offset-name list. */
774 bfd_size_type amt = sizeof (struct list_ele);
775 struct list_ele *t = (struct list_ele *) bfd_malloc (amt);
780 t->offset = our_toc_offset;
783 t->addr = toc_section->output_offset + our_toc_offset;
797 #ifdef COFF_IMAGE_WITH_PE
799 /* Record a toc offset against a symbol. */
801 ppc_record_toc_entry (bfd *abfd,
802 struct bfd_link_info *info ATTRIBUTE_UNUSED,
803 asection *sec ATTRIBUTE_UNUSED,
805 enum toc_type toc_kind ATTRIBUTE_UNUSED)
807 struct ppc_coff_link_hash_entry *h;
812 h = (struct ppc_coff_link_hash_entry *) (obj_coff_sym_hashes (abfd)[sym]);
820 local_syms = obj_coff_local_toc_table(abfd);
827 /* allocate a table */
828 amt = (bfd_size_type) obj_raw_syment_count (abfd) * sizeof (int);
829 local_syms = (int *) bfd_zalloc (abfd, amt);
832 obj_coff_local_toc_table (abfd) = local_syms;
834 for (i = 0; i < obj_raw_syment_count (abfd); ++i)
836 SET_UNALLOCATED (local_syms[i]);
840 if (IS_UNALLOCATED(local_syms[sym]))
842 local_syms[sym] = global_toc_size;
843 global_toc_size += 4;
845 /* The size must fit in a 16-bit displacement. */
846 if (global_toc_size > 65535)
848 _bfd_error_handler (_("TOC overflow"));
849 bfd_set_error (bfd_error_file_too_big);
856 /* Check to see if there's a toc slot allocated. If not, do it
857 here. It will be used in relocate_section. */
858 if (IS_UNALLOCATED(h->toc_offset))
860 h->toc_offset = global_toc_size;
861 global_toc_size += 4;
863 /* The size must fit in a 16-bit displacement. */
864 if (global_toc_size >= 65535)
866 _bfd_error_handler (_("TOC overflow"));
867 bfd_set_error (bfd_error_file_too_big);
876 /* Record a toc offset against a symbol. */
878 ppc_mark_symbol_as_glue (bfd *abfd,
880 struct internal_reloc *rel)
882 struct ppc_coff_link_hash_entry *h;
884 h = (struct ppc_coff_link_hash_entry *) (obj_coff_sym_hashes (abfd)[sym]);
888 h->symbol_is_glue = 1;
889 h->glue_insn = bfd_get_32 (abfd, (bfd_byte *) &rel->r_vaddr);
894 #endif /* COFF_IMAGE_WITH_PE */
896 /* Return TRUE if this relocation should
897 appear in the output .reloc section. */
900 in_reloc_p (bfd * abfd ATTRIBUTE_UNUSED,
901 reloc_howto_type *howto)
904 (! howto->pc_relative)
905 && (howto->type != IMAGE_REL_PPC_ADDR32NB)
906 && (howto->type != IMAGE_REL_PPC_TOCREL16)
907 && (howto->type != IMAGE_REL_PPC_IMGLUE)
908 && (howto->type != IMAGE_REL_PPC_IFGLUE)
909 && (howto->type != IMAGE_REL_PPC_SECREL)
910 && (howto->type != IMAGE_REL_PPC_SECTION)
911 && (howto->type != IMAGE_REL_PPC_SECREL16)
912 && (howto->type != IMAGE_REL_PPC_REFHI)
913 && (howto->type != IMAGE_REL_PPC_REFLO)
914 && (howto->type != IMAGE_REL_PPC_PAIR)
915 && (howto->type != IMAGE_REL_PPC_TOCREL16_DEFN) ;
919 write_base_file_entry (bfd *obfd, struct bfd_link_info *info, bfd_vma addr)
921 if (coff_data (obfd)->pe)
922 addr -= pe_data (obfd)->pe_opthdr.ImageBase;
923 if (fwrite (&addr, sizeof (addr), 1, (FILE *) info->base_file) == 1)
926 bfd_set_error (bfd_error_system_call);
930 /* The reloc processing routine for the optimized COFF linker. */
933 coff_ppc_relocate_section (bfd *output_bfd,
934 struct bfd_link_info *info,
936 asection *input_section,
938 struct internal_reloc *relocs,
939 struct internal_syment *syms,
942 struct internal_reloc *rel;
943 struct internal_reloc *relend;
944 asection *toc_section = 0;
946 reloc_howto_type *howto = 0;
948 /* If we are performing a relocatable link, we don't need to do a
949 thing. The caller will take care of adjusting the reloc
950 addresses and symbol indices. */
951 if (bfd_link_relocatable (info))
955 relend = rel + input_section->reloc_count;
956 for (; rel < relend; rel++)
959 struct ppc_coff_link_hash_entry *h;
960 struct internal_syment *sym;
964 bfd_reloc_status_type rstat;
967 unsigned short r_type = EXTRACT_TYPE (rel->r_type);
968 unsigned short r_flags = EXTRACT_FLAGS(rel->r_type);
970 symndx = rel->r_symndx;
971 loc = contents + rel->r_vaddr - input_section->vma;
973 /* FIXME: check bounds on r_type */
974 howto = ppc_coff_howto_table + r_type;
983 h = (struct ppc_coff_link_hash_entry *)
984 (obj_coff_sym_hashes (input_bfd)[symndx]);
993 if (r_type == IMAGE_REL_PPC_IMGLUE && h == 0)
995 /* An IMGLUE reloc must have a name. Something is very wrong. */
1002 /* FIXME: PAIR unsupported in the following code. */
1006 sec = bfd_abs_section_ptr;
1009 sec = sections[symndx];
1010 val = (sec->output_section->vma
1011 + sec->output_offset
1013 if (! obj_pe (output_bfd))
1021 if (h->root.root.type == bfd_link_hash_defined
1022 || h->root.root.type == bfd_link_hash_defweak)
1024 sec = h->root.root.u.def.section;
1025 val = (h->root.root.u.def.value
1026 + sec->output_section->vma
1027 + sec->output_offset);
1030 (*info->callbacks->undefined_symbol)
1031 (info, h->root.root.root.string, input_bfd, input_section,
1032 rel->r_vaddr - input_section->vma, TRUE);
1035 rstat = bfd_reloc_ok;
1037 /* Each case must do its own relocation, setting rstat appropriately. */
1042 /* xgettext: c-format */
1043 (_("%B: unsupported relocation type 0x%02x"), input_bfd, r_type);
1044 bfd_set_error (bfd_error_bad_value);
1046 case IMAGE_REL_PPC_TOCREL16:
1048 bfd_signed_vma our_toc_offset;
1051 DUMP_RELOC2(howto->name, rel);
1053 if (toc_section == 0)
1055 toc_section = bfd_get_section_by_name (bfd_of_toc_owner,
1058 if ( toc_section == NULL )
1060 /* There is no toc section. Something is very wrong. */
1065 /* Amazing bit tricks present. As we may have seen earlier, we
1066 use the 1 bit to tell us whether or not a toc offset has been
1067 allocated. Now that they've all been allocated, we will use
1068 the 1 bit to tell us if we've written this particular toc
1073 /* It is a file local symbol. */
1074 int *local_toc_table;
1075 char name[SYMNMLEN + 1];
1077 sym = syms + symndx;
1078 strncpy (name, sym->_n._n_name, SYMNMLEN);
1079 name[SYMNMLEN] = '\0';
1081 local_toc_table = obj_coff_local_toc_table(input_bfd);
1082 our_toc_offset = local_toc_table[symndx];
1084 if (IS_WRITTEN(our_toc_offset))
1086 /* If it has been written out, it is marked with the
1087 1 bit. Fix up our offset, but do not write it out
1089 MAKE_ADDR_AGAIN(our_toc_offset);
1093 /* Write out the toc entry. */
1094 record_toc (toc_section, our_toc_offset, priv,
1097 bfd_put_32 (output_bfd, val,
1098 toc_section->contents + our_toc_offset);
1100 MARK_AS_WRITTEN(local_toc_table[symndx]);
1106 const char *name = h->root.root.root.string;
1107 our_toc_offset = h->toc_offset;
1109 if ((r_flags & IMAGE_REL_PPC_TOCDEFN)
1110 == IMAGE_REL_PPC_TOCDEFN )
1112 /* This is unbelievable cheese. Some knowledgable asm
1113 hacker has decided to use r2 as a base for loading
1114 a value. He/She does this by setting the tocdefn bit,
1115 and not supplying a toc definition. The behaviour is
1116 then to use the difference between the value of the
1117 symbol and the actual location of the toc as the toc
1120 In fact, what is usually happening is, because the
1121 Import Address Table is mapped immediately following
1122 the toc, some trippy library code trying for speed on
1123 dll linkage, takes advantage of that and considers
1124 the IAT to be part of the toc, thus saving a load. */
1126 our_toc_offset = val - (toc_section->output_section->vma
1127 + toc_section->output_offset);
1129 /* The size must still fit in a 16-bit displacement. */
1130 if ((bfd_vma) our_toc_offset >= 65535)
1133 /* xgettext: c-format */
1134 (_("%B: Relocation for %s of %lx exceeds "
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)
1188 /* xgettext: c-format */
1189 (_("%B: Relocation exceeds allocated TOC (%lx)"),
1190 input_bfd, (unsigned long) toc_section->size);
1191 bfd_set_error (bfd_error_bad_value);
1195 /* Now we know the relocation for this toc reference. */
1196 relocation = our_toc_offset + TOC_LOAD_ADJUSTMENT;
1197 rstat = _bfd_relocate_contents (howto, input_bfd, relocation, loc);
1200 case IMAGE_REL_PPC_IFGLUE:
1202 /* To solve this, we need to know whether or not the symbol
1203 appearing on the call instruction is a glue function or not.
1204 A glue function must announce itself via a IMGLUE reloc, and
1205 the reloc contains the required toc restore instruction. */
1206 DUMP_RELOC2 (howto->name, rel);
1210 if (h->symbol_is_glue == 1)
1212 bfd_put_32 (input_bfd, (bfd_vma) h->glue_insn, loc);
1217 case IMAGE_REL_PPC_SECREL:
1218 /* Unimplemented: codeview debugging information. */
1219 /* For fast access to the header of the section
1220 containing the item. */
1222 case IMAGE_REL_PPC_SECTION:
1223 /* Unimplemented: codeview debugging information. */
1224 /* Is used to indicate that the value should be relative
1225 to the beginning of the section that contains the
1228 case IMAGE_REL_PPC_ABSOLUTE:
1230 const char *my_name;
1231 char buf[SYMNMLEN + 1];
1235 strncpy (buf, (syms+symndx)->_n._n_name, SYMNMLEN);
1236 buf[SYMNMLEN] = '\0';
1240 my_name = h->root.root.root.string;
1243 /* xgettext: c-format */
1244 (_("Warning: unsupported reloc %s <file %B, section %A>\n"
1245 "sym %ld (%s), r_vaddr %ld (%lx)"),
1246 input_bfd, input_section, howto->name,
1247 rel->r_symndx, my_name, (long) rel->r_vaddr,
1248 (unsigned long) rel->r_vaddr);
1251 case IMAGE_REL_PPC_IMGLUE:
1253 /* There is nothing to do now. This reloc was noted in the first
1254 pass over the relocs, and the glue instruction extracted. */
1255 const char *my_name;
1257 if (h->symbol_is_glue == 1)
1259 my_name = h->root.root.root.string;
1262 /* xgettext: c-format */
1263 (_("%B: Out of order IMGLUE reloc for %s"), input_bfd, my_name);
1264 bfd_set_error (bfd_error_bad_value);
1268 case IMAGE_REL_PPC_ADDR32NB:
1270 const char *name = 0;
1272 DUMP_RELOC2 (howto->name, rel);
1274 if (CONST_STRNEQ (input_section->name, ".idata$2") && first_thunk_address == 0)
1276 /* Set magic values. */
1278 struct coff_link_hash_entry *myh;
1280 myh = coff_link_hash_lookup (coff_hash_table (info),
1282 FALSE, FALSE, TRUE);
1283 first_thunk_address = myh->root.u.def.value +
1284 sec->output_section->vma +
1285 sec->output_offset -
1286 pe_data(output_bfd)->pe_opthdr.ImageBase;
1288 idata5offset = myh->root.u.def.value;
1289 myh = coff_link_hash_lookup (coff_hash_table (info),
1291 FALSE, FALSE, TRUE);
1293 thunk_size = myh->root.u.def.value - idata5offset;
1294 myh = coff_link_hash_lookup (coff_hash_table (info),
1296 FALSE, FALSE, TRUE);
1297 import_table_size = myh->root.u.def.value;
1301 /* It is a file local symbol. */
1302 sym = syms + symndx;
1307 name = h->root.root.root.string;
1308 if (strcmp (".idata$2", name) == 0)
1309 target = "__idata2_magic__";
1310 else if (strcmp (".idata$4", name) == 0)
1311 target = "__idata4_magic__";
1312 else if (strcmp (".idata$5", name) == 0)
1313 target = "__idata5_magic__";
1317 struct coff_link_hash_entry *myh;
1319 myh = coff_link_hash_lookup (coff_hash_table (info),
1321 FALSE, FALSE, TRUE);
1324 /* Missing magic cookies. Something is very wrong. */
1328 val = myh->root.u.def.value +
1329 sec->output_section->vma + sec->output_offset;
1330 if (first_thunk_address == 0)
1333 myh = coff_link_hash_lookup (coff_hash_table (info),
1335 FALSE, FALSE, TRUE);
1336 first_thunk_address = myh->root.u.def.value +
1337 sec->output_section->vma +
1338 sec->output_offset -
1339 pe_data(output_bfd)->pe_opthdr.ImageBase;
1341 idata5offset = myh->root.u.def.value;
1342 myh = coff_link_hash_lookup (coff_hash_table (info),
1344 FALSE, FALSE, TRUE);
1346 thunk_size = myh->root.u.def.value - idata5offset;
1347 myh = coff_link_hash_lookup (coff_hash_table (info),
1349 FALSE, FALSE, TRUE);
1350 import_table_size = myh->root.u.def.value;
1355 rstat = _bfd_relocate_contents (howto,
1358 pe_data (output_bfd)->pe_opthdr.ImageBase,
1363 case IMAGE_REL_PPC_REL24:
1364 DUMP_RELOC2(howto->name, rel);
1365 val -= (input_section->output_section->vma
1366 + input_section->output_offset);
1368 rstat = _bfd_relocate_contents (howto,
1373 case IMAGE_REL_PPC_ADDR16:
1374 case IMAGE_REL_PPC_ADDR24:
1375 case IMAGE_REL_PPC_ADDR32:
1376 DUMP_RELOC2(howto->name, rel);
1377 rstat = _bfd_relocate_contents (howto,
1384 if (info->base_file)
1386 /* So if this is non pcrelative, and is referenced
1387 to a section or a common symbol, then it needs a reloc. */
1388 if (sym && pe_data(output_bfd)->in_reloc_p (output_bfd, howto))
1390 /* Relocation to a symbol in a section which
1391 isn't absolute - we output the address here
1393 bfd_vma addr = (rel->r_vaddr
1394 - input_section->vma
1395 + input_section->output_offset
1396 + input_section->output_section->vma);
1398 if (!write_base_file_entry (output_bfd, info, addr))
1409 case bfd_reloc_overflow:
1412 char buf[SYMNMLEN + 1];
1418 else if (sym == NULL)
1420 else if (sym->_n._n_n._n_zeroes == 0
1421 && sym->_n._n_n._n_offset != 0)
1422 name = obj_coff_strings (input_bfd) + sym->_n._n_n._n_offset;
1425 strncpy (buf, sym->_n._n_name, SYMNMLEN);
1426 buf[SYMNMLEN] = '\0';
1430 (*info->callbacks->reloc_overflow)
1431 (info, (h ? &h->root.root : NULL), name, howto->name,
1432 (bfd_vma) 0, input_bfd, input_section,
1433 rel->r_vaddr - input_section->vma);
1441 #ifdef COFF_IMAGE_WITH_PE
1443 /* FIXME: BFD should not use global variables. This file is compiled
1444 twice, and these variables are shared. This is confusing and
1447 long int global_toc_size = 4;
1449 bfd* bfd_of_toc_owner = 0;
1451 long int import_table_size;
1452 long int first_thunk_address;
1453 long int thunk_size;
1455 struct list_ele *head;
1456 struct list_ele *tail;
1459 h1 = N_("\n\t\t\tTOC MAPPING\n\n");
1461 h2 = N_(" TOC disassembly Comments Name\n");
1463 h3 = N_(" Offset spelling (if present)\n");
1466 dump_toc (void * vfile)
1468 FILE *file = (FILE *) vfile;
1471 fputs (_(h1), file);
1472 fputs (_(h2), file);
1473 fputs (_(h3), file);
1475 for (t = head; t != 0; t=t->next)
1477 const char *cat = "";
1480 cat = _("private ");
1481 else if (t->cat == pub)
1483 else if (t->cat == tocdata)
1484 cat = _("data-in-toc ");
1486 if (t->offset > global_toc_size)
1488 if (t->offset <= global_toc_size + thunk_size)
1489 cat = _("IAT reference ");
1493 /* xgettext: c-format */
1494 _("**** global_toc_size %ld(%lx), thunk_size %ld(%lx)\n"),
1495 global_toc_size, (unsigned long) global_toc_size,
1496 thunk_size, (unsigned long) thunk_size);
1497 cat = _("Out of bounds!");
1502 " %04lx (%d)", (unsigned long) t->offset, t->offset - 32768);
1509 fprintf (file, "\n");
1513 ppc_allocate_toc_section (struct bfd_link_info *info ATTRIBUTE_UNUSED)
1518 static char test_char = '1';
1520 if ( global_toc_size == 0 ) /* FIXME: does this get me in trouble? */
1523 if (bfd_of_toc_owner == 0)
1524 /* No toc owner? Something is very wrong. */
1527 s = bfd_get_section_by_name ( bfd_of_toc_owner , TOC_SECTION_NAME);
1529 /* No toc section? Something is very wrong. */
1532 amt = global_toc_size;
1533 foo = (bfd_byte *) bfd_alloc (bfd_of_toc_owner, amt);
1534 memset(foo, test_char, (size_t) global_toc_size);
1536 s->size = global_toc_size;
1543 ppc_process_before_allocation (bfd *abfd,
1544 struct bfd_link_info *info)
1547 struct internal_reloc *i, *rel;
1549 /* Here we have a bfd that is to be included on the link. We have a hook
1550 to do reloc rummaging, before section sizes are nailed down. */
1551 _bfd_coff_get_external_symbols (abfd);
1553 /* Rummage around all the relocs and map the toc. */
1554 sec = abfd->sections;
1559 for (; sec != 0; sec = sec->next)
1561 if (sec->reloc_count == 0)
1564 /* load the relocs */
1565 /* FIXME: there may be a storage leak here */
1566 i=_bfd_coff_read_internal_relocs(abfd,sec,1,0,0,0);
1571 for (rel = i; rel < i + sec->reloc_count; ++rel)
1573 unsigned short r_type = EXTRACT_TYPE (rel->r_type);
1574 unsigned short r_flags = EXTRACT_FLAGS (rel->r_type);
1575 bfd_boolean ok = TRUE;
1577 DUMP_RELOC2 (ppc_coff_howto_table[r_type].name, rel);
1581 case IMAGE_REL_PPC_TOCREL16:
1582 /* If TOCDEFN is on, ignore as someone else has allocated the
1584 if ((r_flags & IMAGE_REL_PPC_TOCDEFN) != IMAGE_REL_PPC_TOCDEFN)
1585 ok = ppc_record_toc_entry(abfd, info, sec,
1586 rel->r_symndx, default_toc);
1590 case IMAGE_REL_PPC_IMGLUE:
1591 ppc_mark_symbol_as_glue (abfd, rel->r_symndx, rel);
1604 static bfd_reloc_status_type
1605 ppc_refhi_reloc (bfd *abfd ATTRIBUTE_UNUSED,
1606 arelent *reloc_entry ATTRIBUTE_UNUSED,
1607 asymbol *symbol ATTRIBUTE_UNUSED,
1608 void * data ATTRIBUTE_UNUSED,
1609 asection *input_section ATTRIBUTE_UNUSED,
1611 char **error_message ATTRIBUTE_UNUSED)
1614 DUMP_RELOC("REFHI",reloc_entry);
1616 if (output_bfd == (bfd *) NULL)
1617 return bfd_reloc_continue;
1619 return bfd_reloc_undefined;
1622 static bfd_reloc_status_type
1623 ppc_pair_reloc (bfd *abfd ATTRIBUTE_UNUSED,
1624 arelent *reloc_entry ATTRIBUTE_UNUSED,
1625 asymbol *symbol ATTRIBUTE_UNUSED,
1626 void * data ATTRIBUTE_UNUSED,
1627 asection *input_section ATTRIBUTE_UNUSED,
1629 char **error_message ATTRIBUTE_UNUSED)
1632 DUMP_RELOC("PAIR",reloc_entry);
1634 if (output_bfd == (bfd *) NULL)
1635 return bfd_reloc_continue;
1637 return bfd_reloc_undefined;
1640 static bfd_reloc_status_type
1641 ppc_toc16_reloc (bfd *abfd ATTRIBUTE_UNUSED,
1642 arelent *reloc_entry ATTRIBUTE_UNUSED,
1643 asymbol *symbol ATTRIBUTE_UNUSED,
1644 void * data ATTRIBUTE_UNUSED,
1645 asection *input_section ATTRIBUTE_UNUSED,
1647 char **error_message ATTRIBUTE_UNUSED)
1649 UN_IMPL ("TOCREL16");
1650 DUMP_RELOC ("TOCREL16",reloc_entry);
1652 if (output_bfd == (bfd *) NULL)
1653 return bfd_reloc_continue;
1655 return bfd_reloc_ok;
1658 static bfd_reloc_status_type
1659 ppc_secrel_reloc (bfd *abfd ATTRIBUTE_UNUSED,
1660 arelent *reloc_entry ATTRIBUTE_UNUSED,
1661 asymbol *symbol ATTRIBUTE_UNUSED,
1662 void * data ATTRIBUTE_UNUSED,
1663 asection *input_section ATTRIBUTE_UNUSED,
1665 char **error_message ATTRIBUTE_UNUSED)
1668 DUMP_RELOC("SECREL",reloc_entry);
1670 if (output_bfd == (bfd *) NULL)
1671 return bfd_reloc_continue;
1673 return bfd_reloc_ok;
1676 static bfd_reloc_status_type
1677 ppc_section_reloc (bfd *abfd ATTRIBUTE_UNUSED,
1678 arelent *reloc_entry ATTRIBUTE_UNUSED,
1679 asymbol *symbol ATTRIBUTE_UNUSED,
1680 void * data ATTRIBUTE_UNUSED,
1681 asection *input_section ATTRIBUTE_UNUSED,
1683 char **error_message ATTRIBUTE_UNUSED)
1686 DUMP_RELOC("SECTION",reloc_entry);
1688 if (output_bfd == (bfd *) NULL)
1689 return bfd_reloc_continue;
1691 return bfd_reloc_ok;
1694 static bfd_reloc_status_type
1695 ppc_imglue_reloc (bfd *abfd ATTRIBUTE_UNUSED,
1696 arelent *reloc_entry ATTRIBUTE_UNUSED,
1697 asymbol *symbol ATTRIBUTE_UNUSED,
1698 void * data ATTRIBUTE_UNUSED,
1699 asection *input_section ATTRIBUTE_UNUSED,
1701 char **error_message ATTRIBUTE_UNUSED)
1705 DUMP_RELOC("IMGLUE",reloc_entry);
1707 if (output_bfd == (bfd *) NULL)
1708 return bfd_reloc_continue;
1710 return bfd_reloc_ok;
1713 #define MAX_RELOC_INDEX \
1714 (sizeof (ppc_coff_howto_table) / sizeof (ppc_coff_howto_table[0]) - 1)
1716 /* FIXME: There is a possibility that when we read in a reloc from a file,
1717 that there are some bits encoded in the upper portion of the
1718 type field. Not yet implemented. */
1721 ppc_coff_rtype2howto (arelent *relent, struct internal_reloc *internal)
1723 /* We can encode one of three things in the type field, aside from the
1725 1. IMAGE_REL_PPC_NEG - indicates the value field is a subtraction
1726 value, rather than an addition value
1727 2. IMAGE_REL_PPC_BRTAKEN, IMAGE_REL_PPC_BRNTAKEN - indicates that
1728 the branch is expected to be taken or not.
1729 3. IMAGE_REL_PPC_TOCDEFN - toc slot definition in the file
1730 For now, we just strip this stuff to find the type, and ignore it other
1732 reloc_howto_type *howto;
1733 unsigned short r_type = EXTRACT_TYPE (internal->r_type);
1734 unsigned short r_flags = EXTRACT_FLAGS(internal->r_type);
1735 unsigned short junk = EXTRACT_JUNK (internal->r_type);
1737 /* The masking process only slices off the bottom byte for r_type. */
1738 if ( r_type > MAX_RELOC_INDEX )
1741 /* Check for absolute crap. */
1747 case IMAGE_REL_PPC_ADDR16:
1748 case IMAGE_REL_PPC_REL24:
1749 case IMAGE_REL_PPC_ADDR24:
1750 case IMAGE_REL_PPC_ADDR32:
1751 case IMAGE_REL_PPC_IFGLUE:
1752 case IMAGE_REL_PPC_ADDR32NB:
1753 case IMAGE_REL_PPC_SECTION:
1754 case IMAGE_REL_PPC_SECREL:
1755 DUMP_RELOC2 (ppc_coff_howto_table[r_type].name, internal);
1756 howto = ppc_coff_howto_table + r_type;
1758 case IMAGE_REL_PPC_IMGLUE:
1759 DUMP_RELOC2 (ppc_coff_howto_table[r_type].name, internal);
1760 howto = ppc_coff_howto_table + r_type;
1762 case IMAGE_REL_PPC_TOCREL16:
1763 DUMP_RELOC2 (ppc_coff_howto_table[r_type].name, internal);
1764 if (r_flags & IMAGE_REL_PPC_TOCDEFN)
1765 howto = ppc_coff_howto_table + IMAGE_REL_PPC_TOCREL16_DEFN;
1767 howto = ppc_coff_howto_table + IMAGE_REL_PPC_TOCREL16;
1771 /* xgettext: c-format */
1772 (_("warning: unsupported reloc %s [%d] used -- it may not work"),
1773 ppc_coff_howto_table[r_type].name, r_type);
1774 howto = ppc_coff_howto_table + r_type;
1778 relent->howto = howto;
1781 static reloc_howto_type *
1782 coff_ppc_rtype_to_howto (bfd *abfd ATTRIBUTE_UNUSED,
1784 struct internal_reloc *rel,
1785 struct coff_link_hash_entry *h ATTRIBUTE_UNUSED,
1786 struct internal_syment *sym ATTRIBUTE_UNUSED,
1789 reloc_howto_type *howto;
1791 /* We can encode one of three things in the type field, aside from the
1793 1. IMAGE_REL_PPC_NEG - indicates the value field is a subtraction
1794 value, rather than an addition value
1795 2. IMAGE_REL_PPC_BRTAKEN, IMAGE_REL_PPC_BRNTAKEN - indicates that
1796 the branch is expected to be taken or not.
1797 3. IMAGE_REL_PPC_TOCDEFN - toc slot definition in the file
1798 For now, we just strip this stuff to find the type, and ignore it other
1801 unsigned short r_type = EXTRACT_TYPE (rel->r_type);
1802 unsigned short r_flags = EXTRACT_FLAGS (rel->r_type);
1803 unsigned short junk = EXTRACT_JUNK (rel->r_type);
1805 /* The masking process only slices off the bottom byte for r_type. */
1806 if (r_type > MAX_RELOC_INDEX)
1809 /* Check for absolute crap. */
1815 case IMAGE_REL_PPC_ADDR32NB:
1816 DUMP_RELOC2(ppc_coff_howto_table[r_type].name, rel);
1817 *addendp -= pe_data(sec->output_section->owner)->pe_opthdr.ImageBase;
1818 howto = ppc_coff_howto_table + r_type;
1820 case IMAGE_REL_PPC_TOCREL16:
1821 DUMP_RELOC2(ppc_coff_howto_table[r_type].name, rel);
1822 if (r_flags & IMAGE_REL_PPC_TOCDEFN)
1823 howto = ppc_coff_howto_table + IMAGE_REL_PPC_TOCREL16_DEFN;
1825 howto = ppc_coff_howto_table + IMAGE_REL_PPC_TOCREL16;
1827 case IMAGE_REL_PPC_ADDR16:
1828 case IMAGE_REL_PPC_REL24:
1829 case IMAGE_REL_PPC_ADDR24:
1830 case IMAGE_REL_PPC_ADDR32:
1831 case IMAGE_REL_PPC_IFGLUE:
1832 case IMAGE_REL_PPC_SECTION:
1833 case IMAGE_REL_PPC_SECREL:
1834 DUMP_RELOC2(ppc_coff_howto_table[r_type].name, rel);
1835 howto = ppc_coff_howto_table + r_type;
1837 case IMAGE_REL_PPC_IMGLUE:
1838 DUMP_RELOC2(ppc_coff_howto_table[r_type].name, rel);
1839 howto = ppc_coff_howto_table + r_type;
1843 /* xgettext: c-format */
1844 (_("warning: unsupported reloc %s [%d] used -- it may not work"),
1845 ppc_coff_howto_table[r_type].name, r_type);
1846 howto = ppc_coff_howto_table + r_type;
1853 /* A cheesy little macro to make the code a little more readable. */
1854 #define HOW2MAP(bfd_rtype,ppc_rtype) \
1855 case bfd_rtype: return &ppc_coff_howto_table[ppc_rtype]
1857 static reloc_howto_type *
1858 ppc_coff_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
1859 bfd_reloc_code_real_type code)
1863 HOW2MAP(BFD_RELOC_32_GOTOFF, IMAGE_REL_PPC_IMGLUE);
1864 HOW2MAP(BFD_RELOC_16_GOT_PCREL, IMAGE_REL_PPC_IFGLUE);
1865 HOW2MAP(BFD_RELOC_16, IMAGE_REL_PPC_ADDR16);
1866 HOW2MAP(BFD_RELOC_PPC_B26, IMAGE_REL_PPC_REL24);
1867 HOW2MAP(BFD_RELOC_PPC_BA26, IMAGE_REL_PPC_ADDR24);
1868 HOW2MAP(BFD_RELOC_PPC_TOC16, IMAGE_REL_PPC_TOCREL16);
1869 HOW2MAP(BFD_RELOC_16_GOTOFF, IMAGE_REL_PPC_TOCREL16_DEFN);
1870 HOW2MAP(BFD_RELOC_32, IMAGE_REL_PPC_ADDR32);
1871 HOW2MAP(BFD_RELOC_RVA, IMAGE_REL_PPC_ADDR32NB);
1878 static reloc_howto_type *
1879 ppc_coff_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
1885 i < sizeof (ppc_coff_howto_table) / sizeof (ppc_coff_howto_table[0]);
1887 if (ppc_coff_howto_table[i].name != NULL
1888 && strcasecmp (ppc_coff_howto_table[i].name, r_name) == 0)
1889 return &ppc_coff_howto_table[i];
1894 /* Tailor coffcode.h -- macro heaven. */
1896 #define RTYPE2HOWTO(cache_ptr, dst) ppc_coff_rtype2howto (cache_ptr, dst)
1898 /* We use the special COFF backend linker, with our own special touch. */
1900 #define coff_bfd_reloc_type_lookup ppc_coff_reloc_type_lookup
1901 #define coff_bfd_reloc_name_lookup ppc_coff_reloc_name_lookup
1902 #define coff_rtype_to_howto coff_ppc_rtype_to_howto
1903 #define coff_relocate_section coff_ppc_relocate_section
1904 #define coff_bfd_final_link ppc_bfd_coff_final_link
1906 #ifndef COFF_IMAGE_WITH_PE
1909 #define SELECT_RELOC(internal, howto) {internal.r_type=howto->type;}
1911 #define COFF_PAGE_SIZE 0x1000
1913 /* FIXME: This controls some code that used to be in peicode.h and is
1914 now in peigen.c. It will not control the code in peigen.c. If
1915 anybody wants to get this working, you will need to fix that. */
1916 #define POWERPC_LE_PE
1918 #define COFF_SECTION_ALIGNMENT_ENTRIES \
1919 { COFF_SECTION_NAME_EXACT_MATCH (".idata$2"), \
1920 COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 0 }, \
1921 { COFF_SECTION_NAME_EXACT_MATCH (".idata$3"), \
1922 COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 0 }, \
1923 { COFF_SECTION_NAME_EXACT_MATCH (".idata$4"), \
1924 COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 2 }, \
1925 { COFF_SECTION_NAME_EXACT_MATCH (".idata$5"), \
1926 COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 2 }, \
1927 { COFF_SECTION_NAME_EXACT_MATCH (".idata$6"), \
1928 COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 1 }, \
1929 { COFF_SECTION_NAME_EXACT_MATCH (".reloc"), \
1930 COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 1 }
1932 #include "coffcode.h"
1934 #ifndef COFF_IMAGE_WITH_PE
1937 ppc_do_last (bfd *abfd)
1939 if (abfd == bfd_of_toc_owner)
1948 return bfd_of_toc_owner;
1951 /* This piece of machinery exists only to guarantee that the bfd that holds
1952 the toc section is written last.
1954 This does depend on bfd_make_section attaching a new section to the
1955 end of the section list for the bfd.
1957 This is otherwise intended to be functionally the same as
1958 cofflink.c:_bfd_coff_final_link(). It is specifically different only
1959 where the POWERPC_LE_PE macro modifies the code. It is left in as a
1960 precise form of comment. krk@cygnus.com */
1962 /* Do the final link step. */
1965 ppc_bfd_coff_final_link (bfd *abfd, struct bfd_link_info *info)
1967 bfd_size_type symesz;
1968 struct coff_final_link_info flaginfo;
1969 bfd_boolean debug_merge_allocated;
1971 struct bfd_link_order *p;
1972 bfd_size_type max_sym_count;
1973 bfd_size_type max_lineno_count;
1974 bfd_size_type max_reloc_count;
1975 bfd_size_type max_output_reloc_count;
1976 bfd_size_type max_contents_size;
1977 file_ptr rel_filepos;
1979 file_ptr line_filepos;
1980 unsigned int linesz;
1982 bfd_byte *external_relocs = NULL;
1983 char strbuf[STRING_SIZE_SIZE];
1986 symesz = bfd_coff_symesz (abfd);
1988 flaginfo.info = info;
1989 flaginfo.output_bfd = abfd;
1990 flaginfo.strtab = NULL;
1991 flaginfo.section_info = NULL;
1992 flaginfo.last_file_index = -1;
1993 flaginfo.last_bf_index = -1;
1994 flaginfo.internal_syms = NULL;
1995 flaginfo.sec_ptrs = NULL;
1996 flaginfo.sym_indices = NULL;
1997 flaginfo.outsyms = NULL;
1998 flaginfo.linenos = NULL;
1999 flaginfo.contents = NULL;
2000 flaginfo.external_relocs = NULL;
2001 flaginfo.internal_relocs = NULL;
2002 debug_merge_allocated = FALSE;
2004 coff_data (abfd)->link_info = info;
2006 flaginfo.strtab = _bfd_stringtab_init ();
2007 if (flaginfo.strtab == NULL)
2010 if (! coff_debug_merge_hash_table_init (&flaginfo.debug_merge))
2012 debug_merge_allocated = TRUE;
2014 /* Compute the file positions for all the sections. */
2015 if (! abfd->output_has_begun)
2017 if (! bfd_coff_compute_section_file_positions (abfd))
2021 /* Count the line numbers and relocation entries required for the
2022 output file. Set the file positions for the relocs. */
2023 rel_filepos = obj_relocbase (abfd);
2024 relsz = bfd_coff_relsz (abfd);
2025 max_contents_size = 0;
2026 max_lineno_count = 0;
2027 max_reloc_count = 0;
2029 for (o = abfd->sections; o != NULL; o = o->next)
2032 o->lineno_count = 0;
2034 for (p = o->map_head.link_order; p != NULL; p = p->next)
2036 if (p->type == bfd_indirect_link_order)
2040 sec = p->u.indirect.section;
2042 /* Mark all sections which are to be included in the
2043 link. This will normally be every section. We need
2044 to do this so that we can identify any sections which
2045 the linker has decided to not include. */
2046 sec->linker_mark = TRUE;
2048 if (info->strip == strip_none
2049 || info->strip == strip_some)
2050 o->lineno_count += sec->lineno_count;
2052 if (bfd_link_relocatable (info))
2053 o->reloc_count += sec->reloc_count;
2055 if (sec->rawsize > max_contents_size)
2056 max_contents_size = sec->rawsize;
2057 if (sec->size > max_contents_size)
2058 max_contents_size = sec->size;
2059 if (sec->lineno_count > max_lineno_count)
2060 max_lineno_count = sec->lineno_count;
2061 if (sec->reloc_count > max_reloc_count)
2062 max_reloc_count = sec->reloc_count;
2064 else if (bfd_link_relocatable (info)
2065 && (p->type == bfd_section_reloc_link_order
2066 || p->type == bfd_symbol_reloc_link_order))
2069 if (o->reloc_count == 0)
2073 o->flags |= SEC_RELOC;
2074 o->rel_filepos = rel_filepos;
2075 rel_filepos += o->reloc_count * relsz;
2079 /* If doing a relocatable link, allocate space for the pointers we
2081 if (bfd_link_relocatable (info))
2085 /* We use section_count + 1, rather than section_count, because
2086 the target_index fields are 1 based. */
2087 amt = abfd->section_count + 1;
2088 amt *= sizeof (struct coff_link_section_info);
2089 flaginfo.section_info = (struct coff_link_section_info *) bfd_malloc (amt);
2091 if (flaginfo.section_info == NULL)
2094 for (i = 0; i <= abfd->section_count; i++)
2096 flaginfo.section_info[i].relocs = NULL;
2097 flaginfo.section_info[i].rel_hashes = NULL;
2101 /* We now know the size of the relocs, so we can determine the file
2102 positions of the line numbers. */
2103 line_filepos = rel_filepos;
2104 linesz = bfd_coff_linesz (abfd);
2105 max_output_reloc_count = 0;
2107 for (o = abfd->sections; o != NULL; o = o->next)
2109 if (o->lineno_count == 0)
2110 o->line_filepos = 0;
2113 o->line_filepos = line_filepos;
2114 line_filepos += o->lineno_count * linesz;
2117 if (o->reloc_count != 0)
2119 /* We don't know the indices of global symbols until we have
2120 written out all the local symbols. For each section in
2121 the output file, we keep an array of pointers to hash
2122 table entries. Each entry in the array corresponds to a
2123 reloc. When we find a reloc against a global symbol, we
2124 set the corresponding entry in this array so that we can
2125 fix up the symbol index after we have written out all the
2128 Because of this problem, we also keep the relocs in
2129 memory until the end of the link. This wastes memory,
2130 but only when doing a relocatable link, which is not the
2132 BFD_ASSERT (bfd_link_relocatable (info));
2133 amt = o->reloc_count;
2134 amt *= sizeof (struct internal_reloc);
2135 flaginfo.section_info[o->target_index].relocs =
2136 (struct internal_reloc *) bfd_malloc (amt);
2137 amt = o->reloc_count;
2138 amt *= sizeof (struct coff_link_hash_entry *);
2139 flaginfo.section_info[o->target_index].rel_hashes =
2140 (struct coff_link_hash_entry **) bfd_malloc (amt);
2141 if (flaginfo.section_info[o->target_index].relocs == NULL
2142 || flaginfo.section_info[o->target_index].rel_hashes == NULL)
2145 if (o->reloc_count > max_output_reloc_count)
2146 max_output_reloc_count = o->reloc_count;
2149 /* Reset the reloc and lineno counts, so that we can use them to
2150 count the number of entries we have output so far. */
2152 o->lineno_count = 0;
2155 obj_sym_filepos (abfd) = line_filepos;
2157 /* Figure out the largest number of symbols in an input BFD. Take
2158 the opportunity to clear the output_has_begun fields of all the
2161 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
2165 sub->output_has_begun = FALSE;
2166 sz = obj_raw_syment_count (sub);
2167 if (sz > max_sym_count)
2171 /* Allocate some buffers used while linking. */
2172 amt = max_sym_count * sizeof (struct internal_syment);
2173 flaginfo.internal_syms = (struct internal_syment *) bfd_malloc (amt);
2174 amt = max_sym_count * sizeof (asection *);
2175 flaginfo.sec_ptrs = (asection **) bfd_malloc (amt);
2176 amt = max_sym_count * sizeof (long);
2177 flaginfo.sym_indices = (long *) bfd_malloc (amt);
2178 amt = (max_sym_count + 1) * symesz;
2179 flaginfo.outsyms = (bfd_byte *) bfd_malloc (amt);
2180 amt = max_lineno_count * bfd_coff_linesz (abfd);
2181 flaginfo.linenos = (bfd_byte *) bfd_malloc (amt);
2182 flaginfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
2183 flaginfo.external_relocs = (bfd_byte *) bfd_malloc (max_reloc_count * relsz);
2184 if (! bfd_link_relocatable (info))
2186 amt = max_reloc_count * sizeof (struct internal_reloc);
2187 flaginfo.internal_relocs = (struct internal_reloc *) bfd_malloc (amt);
2189 if ((flaginfo.internal_syms == NULL && max_sym_count > 0)
2190 || (flaginfo.sec_ptrs == NULL && max_sym_count > 0)
2191 || (flaginfo.sym_indices == NULL && max_sym_count > 0)
2192 || flaginfo.outsyms == NULL
2193 || (flaginfo.linenos == NULL && max_lineno_count > 0)
2194 || (flaginfo.contents == NULL && max_contents_size > 0)
2195 || (flaginfo.external_relocs == NULL && max_reloc_count > 0)
2196 || (! bfd_link_relocatable (info)
2197 && flaginfo.internal_relocs == NULL
2198 && max_reloc_count > 0))
2201 /* We now know the position of everything in the file, except that
2202 we don't know the size of the symbol table and therefore we don't
2203 know where the string table starts. We just build the string
2204 table in memory as we go along. We process all the relocations
2205 for a single input file at once. */
2206 obj_raw_syment_count (abfd) = 0;
2208 if (coff_backend_info (abfd)->_bfd_coff_start_final_link)
2210 if (! bfd_coff_start_final_link (abfd, info))
2214 for (o = abfd->sections; o != NULL; o = o->next)
2216 for (p = o->map_head.link_order; p != NULL; p = p->next)
2218 if (p->type == bfd_indirect_link_order
2219 && (bfd_get_flavour (p->u.indirect.section->owner)
2220 == bfd_target_coff_flavour))
2222 sub = p->u.indirect.section->owner;
2223 #ifdef POWERPC_LE_PE
2224 if (! sub->output_has_begun && !ppc_do_last(sub))
2226 if (! sub->output_has_begun)
2229 if (! _bfd_coff_link_input_bfd (&flaginfo, sub))
2231 sub->output_has_begun = TRUE;
2234 else if (p->type == bfd_section_reloc_link_order
2235 || p->type == bfd_symbol_reloc_link_order)
2237 if (! _bfd_coff_reloc_link_order (abfd, &flaginfo, o, p))
2242 if (! _bfd_default_link_order (abfd, info, o, p))
2248 #ifdef POWERPC_LE_PE
2250 bfd* last_one = ppc_get_last();
2253 if (! _bfd_coff_link_input_bfd (&flaginfo, last_one))
2256 last_one->output_has_begun = TRUE;
2260 /* Free up the buffers used by _bfd_coff_link_input_bfd. */
2261 coff_debug_merge_hash_table_free (&flaginfo.debug_merge);
2262 debug_merge_allocated = FALSE;
2264 if (flaginfo.internal_syms != NULL)
2266 free (flaginfo.internal_syms);
2267 flaginfo.internal_syms = NULL;
2269 if (flaginfo.sec_ptrs != NULL)
2271 free (flaginfo.sec_ptrs);
2272 flaginfo.sec_ptrs = NULL;
2274 if (flaginfo.sym_indices != NULL)
2276 free (flaginfo.sym_indices);
2277 flaginfo.sym_indices = NULL;
2279 if (flaginfo.linenos != NULL)
2281 free (flaginfo.linenos);
2282 flaginfo.linenos = NULL;
2284 if (flaginfo.contents != NULL)
2286 free (flaginfo.contents);
2287 flaginfo.contents = NULL;
2289 if (flaginfo.external_relocs != NULL)
2291 free (flaginfo.external_relocs);
2292 flaginfo.external_relocs = NULL;
2294 if (flaginfo.internal_relocs != NULL)
2296 free (flaginfo.internal_relocs);
2297 flaginfo.internal_relocs = NULL;
2300 /* The value of the last C_FILE symbol is supposed to be the symbol
2301 index of the first external symbol. Write it out again if
2303 if (flaginfo.last_file_index != -1
2304 && (unsigned int) flaginfo.last_file.n_value != obj_raw_syment_count (abfd))
2308 flaginfo.last_file.n_value = obj_raw_syment_count (abfd);
2309 bfd_coff_swap_sym_out (abfd, &flaginfo.last_file,
2311 pos = obj_sym_filepos (abfd) + flaginfo.last_file_index * symesz;
2312 if (bfd_seek (abfd, pos, SEEK_SET) != 0
2313 || bfd_bwrite (flaginfo.outsyms, symesz, abfd) != symesz)
2317 /* Write out the global symbols. */
2318 flaginfo.failed = FALSE;
2319 bfd_hash_traverse (&info->hash->table, _bfd_coff_write_global_sym, &flaginfo);
2320 if (flaginfo.failed)
2323 /* The outsyms buffer is used by _bfd_coff_write_global_sym. */
2324 if (flaginfo.outsyms != NULL)
2326 free (flaginfo.outsyms);
2327 flaginfo.outsyms = NULL;
2330 if (bfd_link_relocatable (info))
2332 /* Now that we have written out all the global symbols, we know
2333 the symbol indices to use for relocs against them, and we can
2334 finally write out the relocs. */
2335 amt = max_output_reloc_count * relsz;
2336 external_relocs = (bfd_byte *) bfd_malloc (amt);
2337 if (external_relocs == NULL)
2340 for (o = abfd->sections; o != NULL; o = o->next)
2342 struct internal_reloc *irel;
2343 struct internal_reloc *irelend;
2344 struct coff_link_hash_entry **rel_hash;
2347 if (o->reloc_count == 0)
2350 irel = flaginfo.section_info[o->target_index].relocs;
2351 irelend = irel + o->reloc_count;
2352 rel_hash = flaginfo.section_info[o->target_index].rel_hashes;
2353 erel = external_relocs;
2354 for (; irel < irelend; irel++, rel_hash++, erel += relsz)
2356 if (*rel_hash != NULL)
2358 BFD_ASSERT ((*rel_hash)->indx >= 0);
2359 irel->r_symndx = (*rel_hash)->indx;
2361 bfd_coff_swap_reloc_out (abfd, irel, erel);
2364 amt = relsz * o->reloc_count;
2365 if (bfd_seek (abfd, o->rel_filepos, SEEK_SET) != 0
2366 || bfd_bwrite (external_relocs, amt, abfd) != amt)
2370 free (external_relocs);
2371 external_relocs = NULL;
2374 /* Free up the section information. */
2375 if (flaginfo.section_info != NULL)
2379 for (i = 0; i < abfd->section_count; i++)
2381 if (flaginfo.section_info[i].relocs != NULL)
2382 free (flaginfo.section_info[i].relocs);
2383 if (flaginfo.section_info[i].rel_hashes != NULL)
2384 free (flaginfo.section_info[i].rel_hashes);
2386 free (flaginfo.section_info);
2387 flaginfo.section_info = NULL;
2390 /* If we have optimized stabs strings, output them. */
2391 if (coff_hash_table (info)->stab_info.stabstr != NULL)
2393 if (! _bfd_write_stab_strings (abfd, &coff_hash_table (info)->stab_info))
2397 /* Write out the string table. */
2398 if (obj_raw_syment_count (abfd) != 0)
2402 pos = obj_sym_filepos (abfd) + obj_raw_syment_count (abfd) * symesz;
2403 if (bfd_seek (abfd, pos, SEEK_SET) != 0)
2406 #if STRING_SIZE_SIZE == 4
2408 _bfd_stringtab_size (flaginfo.strtab) + STRING_SIZE_SIZE,
2411 #error Change H_PUT_32 above
2414 if (bfd_bwrite (strbuf, (bfd_size_type) STRING_SIZE_SIZE, abfd)
2415 != STRING_SIZE_SIZE)
2418 if (! _bfd_stringtab_emit (abfd, flaginfo.strtab))
2422 _bfd_stringtab_free (flaginfo.strtab);
2424 /* Setting bfd_get_symcount to 0 will cause write_object_contents to
2425 not try to write out the symbols. */
2426 bfd_get_symcount (abfd) = 0;
2431 if (debug_merge_allocated)
2432 coff_debug_merge_hash_table_free (&flaginfo.debug_merge);
2433 if (flaginfo.strtab != NULL)
2434 _bfd_stringtab_free (flaginfo.strtab);
2435 if (flaginfo.section_info != NULL)
2439 for (i = 0; i < abfd->section_count; i++)
2441 if (flaginfo.section_info[i].relocs != NULL)
2442 free (flaginfo.section_info[i].relocs);
2443 if (flaginfo.section_info[i].rel_hashes != NULL)
2444 free (flaginfo.section_info[i].rel_hashes);
2446 free (flaginfo.section_info);
2448 if (flaginfo.internal_syms != NULL)
2449 free (flaginfo.internal_syms);
2450 if (flaginfo.sec_ptrs != NULL)
2451 free (flaginfo.sec_ptrs);
2452 if (flaginfo.sym_indices != NULL)
2453 free (flaginfo.sym_indices);
2454 if (flaginfo.outsyms != NULL)
2455 free (flaginfo.outsyms);
2456 if (flaginfo.linenos != NULL)
2457 free (flaginfo.linenos);
2458 if (flaginfo.contents != NULL)
2459 free (flaginfo.contents);
2460 if (flaginfo.external_relocs != NULL)
2461 free (flaginfo.external_relocs);
2462 if (flaginfo.internal_relocs != NULL)
2463 free (flaginfo.internal_relocs);
2464 if (external_relocs != NULL)
2465 free (external_relocs);
2470 /* Forward declaration for use by alternative_target field. */
2471 #ifdef TARGET_BIG_SYM
2472 extern const bfd_target TARGET_BIG_SYM;
2475 /* The transfer vectors that lead the outside world to all of the above. */
2477 #ifdef TARGET_LITTLE_SYM
2478 const bfd_target TARGET_LITTLE_SYM =
2480 TARGET_LITTLE_NAME, /* name or coff-arm-little */
2481 bfd_target_coff_flavour,
2482 BFD_ENDIAN_LITTLE, /* data byte order is little */
2483 BFD_ENDIAN_LITTLE, /* header byte order is little */
2485 (HAS_RELOC | EXEC_P | /* FIXME: object flags */
2486 HAS_LINENO | HAS_DEBUG |
2487 HAS_SYMS | HAS_LOCALS | WP_TEXT | D_PAGED),
2489 #ifndef COFF_WITH_PE
2490 (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY
2491 | SEC_RELOC), /* section flags */
2493 (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY
2494 | SEC_RELOC | SEC_LINK_ONCE | SEC_LINK_DUPLICATES),
2497 0, /* leading char */
2498 '/', /* ar_pad_char */
2499 15, /* ar_max_namelen??? FIXMEmgo */
2500 0, /* match priority. */
2502 bfd_getl64, bfd_getl_signed_64, bfd_putl64,
2503 bfd_getl32, bfd_getl_signed_32, bfd_putl32,
2504 bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* data */
2506 bfd_getl64, bfd_getl_signed_64, bfd_putl64,
2507 bfd_getl32, bfd_getl_signed_32, bfd_putl32,
2508 bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* hdrs */
2510 {_bfd_dummy_target, coff_object_p, /* bfd_check_format */
2511 bfd_generic_archive_p, /* _bfd_dummy_target */ coff_object_p },
2512 {bfd_false, coff_mkobject, _bfd_generic_mkarchive, /* bfd_set_format */
2514 {bfd_false, coff_write_object_contents, /* bfd_write_contents */
2515 _bfd_write_archive_contents, bfd_false},
2517 BFD_JUMP_TABLE_GENERIC (coff),
2518 BFD_JUMP_TABLE_COPY (coff),
2519 BFD_JUMP_TABLE_CORE (_bfd_nocore),
2520 BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_coff),
2521 BFD_JUMP_TABLE_SYMBOLS (coff),
2522 BFD_JUMP_TABLE_RELOCS (coff),
2523 BFD_JUMP_TABLE_WRITE (coff),
2524 BFD_JUMP_TABLE_LINK (coff),
2525 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
2527 /* Alternative_target. */
2528 #ifdef TARGET_BIG_SYM
2538 #ifdef TARGET_BIG_SYM
2539 const bfd_target TARGET_BIG_SYM =
2542 bfd_target_coff_flavour,
2543 BFD_ENDIAN_BIG, /* data byte order is big */
2544 BFD_ENDIAN_BIG, /* header byte order is big */
2546 (HAS_RELOC | EXEC_P | /* FIXME: object flags */
2547 HAS_LINENO | HAS_DEBUG |
2548 HAS_SYMS | HAS_LOCALS | WP_TEXT | D_PAGED),
2550 #ifndef COFF_WITH_PE
2551 (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY
2552 | SEC_RELOC), /* section flags */
2554 (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY
2555 | SEC_RELOC | SEC_LINK_ONCE | SEC_LINK_DUPLICATES),
2558 0, /* leading char */
2559 '/', /* ar_pad_char */
2560 15, /* ar_max_namelen??? FIXMEmgo */
2561 0, /* match priority. */
2563 bfd_getb64, bfd_getb_signed_64, bfd_putb64,
2564 bfd_getb32, bfd_getb_signed_32, bfd_putb32,
2565 bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* data */
2567 bfd_getb64, bfd_getb_signed_64, bfd_putb64,
2568 bfd_getb32, bfd_getb_signed_32, bfd_putb32,
2569 bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* hdrs */
2571 {_bfd_dummy_target, coff_object_p, /* bfd_check_format */
2572 bfd_generic_archive_p, /* _bfd_dummy_target */ coff_object_p },
2573 {bfd_false, coff_mkobject, _bfd_generic_mkarchive, /* bfd_set_format */
2575 {bfd_false, coff_write_object_contents, /* bfd_write_contents */
2576 _bfd_write_archive_contents, bfd_false},
2578 BFD_JUMP_TABLE_GENERIC (coff),
2579 BFD_JUMP_TABLE_COPY (coff),
2580 BFD_JUMP_TABLE_CORE (_bfd_nocore),
2581 BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_coff),
2582 BFD_JUMP_TABLE_SYMBOLS (coff),
2583 BFD_JUMP_TABLE_RELOCS (coff),
2584 BFD_JUMP_TABLE_WRITE (coff),
2585 BFD_JUMP_TABLE_LINK (coff),
2586 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
2588 /* Alternative_target. */
2589 #ifdef TARGET_LITTLE_SYM
2590 & TARGET_LITTLE_SYM,