1 /* BFD back-end for PowerPC Microsoft Portable Executable files.
2 Copyright (C) 1990-2015 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 _("File %s, line %d, Hash check failure, bad eye %8s\n"), \
86 __FILE__, __LINE__, addr->eye_catcher); \
92 #define HASH_CHECK_DCL
93 #define HASH_CHECK_INIT(ret)
94 #define HASH_CHECK(addr)
98 /* In order not to add an int to every hash table item for every coff
99 linker, we define our own hash table, derived from the coff one. */
101 /* PE linker hash table entries. */
103 struct ppc_coff_link_hash_entry
105 struct coff_link_hash_entry root; /* First entry, as required. */
107 /* As we wonder around the relocs, we'll keep the assigned toc_offset
109 bfd_vma toc_offset; /* Our addition, as required. */
111 unsigned long int glue_insn;
116 /* PE linker hash table. */
118 struct ppc_coff_link_hash_table
120 struct coff_link_hash_table root; /* First entry, as required. */
123 /* Routine to create an entry in the link hash table. */
125 static struct bfd_hash_entry *
126 ppc_coff_link_hash_newfunc (struct bfd_hash_entry * entry,
127 struct bfd_hash_table * table,
130 struct ppc_coff_link_hash_entry *ret =
131 (struct ppc_coff_link_hash_entry *) entry;
133 /* Allocate the structure if it has not already been allocated by a
135 if (ret == (struct ppc_coff_link_hash_entry *) NULL)
136 ret = (struct ppc_coff_link_hash_entry *)
137 bfd_hash_allocate (table,
138 sizeof (struct ppc_coff_link_hash_entry));
140 if (ret == (struct ppc_coff_link_hash_entry *) NULL)
143 /* Call the allocation method of the superclass. */
144 ret = ((struct ppc_coff_link_hash_entry *)
145 _bfd_coff_link_hash_newfunc ((struct bfd_hash_entry *) ret,
150 /* Initialize the local fields. */
151 SET_UNALLOCATED (ret->toc_offset);
152 ret->symbol_is_glue = 0;
155 HASH_CHECK_INIT (ret);
158 return (struct bfd_hash_entry *) ret;
161 /* Initialize a PE linker hash table. */
164 ppc_coff_link_hash_table_init (struct ppc_coff_link_hash_table *table,
166 struct bfd_hash_entry *(*newfunc)
167 (struct bfd_hash_entry *,
168 struct bfd_hash_table *,
170 unsigned int entsize)
172 return _bfd_coff_link_hash_table_init (&table->root, abfd, newfunc, entsize);
175 /* Create a PE linker hash table. */
177 static struct bfd_link_hash_table *
178 ppc_coff_link_hash_table_create (bfd *abfd)
180 struct ppc_coff_link_hash_table *ret;
181 bfd_size_type amt = sizeof (struct ppc_coff_link_hash_table);
183 ret = (struct ppc_coff_link_hash_table *) bfd_malloc (amt);
186 if (!ppc_coff_link_hash_table_init (ret, abfd,
187 ppc_coff_link_hash_newfunc,
188 sizeof (struct ppc_coff_link_hash_entry)))
191 return (struct bfd_link_hash_table *) NULL;
193 return &ret->root.root;
196 /* Now, tailor coffcode.h to use our hash stuff. */
198 #define coff_bfd_link_hash_table_create ppc_coff_link_hash_table_create
200 /* The nt loader points the toc register to &toc + 32768, in order to
201 use the complete range of a 16-bit displacement. We have to adjust
202 for this when we fix up loads displaced off the toc reg. */
203 #define TOC_LOAD_ADJUSTMENT (-32768)
204 #define TOC_SECTION_NAME ".private.toc"
206 /* The main body of code is in coffcode.h. */
208 #define COFF_DEFAULT_SECTION_ALIGNMENT_POWER (3)
210 /* In case we're on a 32-bit machine, construct a 64-bit "-1" value
211 from smaller values. Start with zero, widen, *then* decrement. */
212 #define MINUS_ONE (((bfd_vma)0) - 1)
214 /* These should definitely go in a header file somewhere... */
217 #define IMAGE_REL_PPC_ABSOLUTE 0x0000
220 #define IMAGE_REL_PPC_ADDR64 0x0001
223 #define IMAGE_REL_PPC_ADDR32 0x0002
225 /* 26-bit address, shifted left 2 (branch absolute) */
226 #define IMAGE_REL_PPC_ADDR24 0x0003
229 #define IMAGE_REL_PPC_ADDR16 0x0004
231 /* 16-bit address, shifted left 2 (load doubleword) */
232 #define IMAGE_REL_PPC_ADDR14 0x0005
234 /* 26-bit PC-relative offset, shifted left 2 (branch relative) */
235 #define IMAGE_REL_PPC_REL24 0x0006
237 /* 16-bit PC-relative offset, shifted left 2 (br cond relative) */
238 #define IMAGE_REL_PPC_REL14 0x0007
240 /* 16-bit offset from TOC base */
241 #define IMAGE_REL_PPC_TOCREL16 0x0008
243 /* 16-bit offset from TOC base, shifted left 2 (load doubleword) */
244 #define IMAGE_REL_PPC_TOCREL14 0x0009
246 /* 32-bit addr w/o image base */
247 #define IMAGE_REL_PPC_ADDR32NB 0x000A
249 /* va of containing section (as in an image sectionhdr) */
250 #define IMAGE_REL_PPC_SECREL 0x000B
252 /* sectionheader number */
253 #define IMAGE_REL_PPC_SECTION 0x000C
255 /* substitute TOC restore instruction iff symbol is glue code */
256 #define IMAGE_REL_PPC_IFGLUE 0x000D
258 /* symbol is glue code; virtual address is TOC restore instruction */
259 #define IMAGE_REL_PPC_IMGLUE 0x000E
261 /* va of containing section (limited to 16 bits) */
262 #define IMAGE_REL_PPC_SECREL16 0x000F
264 /* Stuff to handle immediate data when the number of bits in the
265 data is greater than the number of bits in the immediate field
266 We need to do (usually) 32 bit arithmetic on 16 bit chunks. */
267 #define IMAGE_REL_PPC_REFHI 0x0010
268 #define IMAGE_REL_PPC_REFLO 0x0011
269 #define IMAGE_REL_PPC_PAIR 0x0012
271 /* This is essentially the same as tocrel16, with TOCDEFN assumed. */
272 #define IMAGE_REL_PPC_TOCREL16_DEFN 0x0013
274 /* Flag bits in IMAGE_RELOCATION.TYPE. */
276 /* Subtract reloc value rather than adding it. */
277 #define IMAGE_REL_PPC_NEG 0x0100
279 /* Fix branch prediction bit to predict branch taken. */
280 #define IMAGE_REL_PPC_BRTAKEN 0x0200
282 /* Fix branch prediction bit to predict branch not taken. */
283 #define IMAGE_REL_PPC_BRNTAKEN 0x0400
285 /* TOC slot defined in file (or, data in toc). */
286 #define IMAGE_REL_PPC_TOCDEFN 0x0800
288 /* Masks to isolate above values in IMAGE_RELOCATION.Type. */
289 #define IMAGE_REL_PPC_TYPEMASK 0x00FF
290 #define IMAGE_REL_PPC_FLAGMASK 0x0F00
292 #define EXTRACT_TYPE(x) ((x) & IMAGE_REL_PPC_TYPEMASK)
293 #define EXTRACT_FLAGS(x) ((x) & IMAGE_REL_PPC_FLAGMASK)
294 #define EXTRACT_JUNK(x) \
295 ((x) & ~(IMAGE_REL_PPC_TYPEMASK | IMAGE_REL_PPC_FLAGMASK))
297 /* Static helper functions to make relocation work. */
298 /* (Work In Progress) */
300 static bfd_reloc_status_type ppc_refhi_reloc
301 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
302 static bfd_reloc_status_type ppc_pair_reloc
303 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
304 static bfd_reloc_status_type ppc_toc16_reloc
305 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
306 static bfd_reloc_status_type ppc_section_reloc
307 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
308 static bfd_reloc_status_type ppc_secrel_reloc
309 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
310 static bfd_reloc_status_type ppc_imglue_reloc
311 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
313 /* FIXME: It'll take a while to get through all of these. I only need a few to
314 get us started, so those I'll make sure work. Those marked FIXME are either
315 completely unverified or have a specific unknown marked in the comment. */
317 /* Relocation entries for Windows/NT on PowerPC.
319 From the document "" we find the following listed as used relocs:
322 ADDR[64|32|16] : fields that hold addresses in data fields or the
323 16 bit displacement field on a load/store.
324 ADDR[24|14] : fields that hold addresses in branch and cond
325 branches. These represent [26|16] bit addresses.
326 The low order 2 bits are preserved.
327 REL[24|14] : branches relative to the Instruction Address
328 register. These represent [26|16] bit addresses,
329 as before. The instruction field will be zero, and
330 the address of the SYM will be inserted at link time.
331 TOCREL16 : 16 bit displacement field referring to a slot in
333 TOCREL14 : 16 bit displacement field, similar to REL14 or ADDR14.
334 ADDR32NB : 32 bit address relative to the virtual origin.
335 (On the alpha, this is always a linker generated thunk)
336 (i.e. 32bit addr relative to the image base)
337 SECREL : The value is relative to the start of the section
338 containing the symbol.
339 SECTION : access to the header containing the item. Supports the
342 In particular, note that the document does not indicate that the
343 relocations listed in the header file are used. */
346 static reloc_howto_type ppc_coff_howto_table[] =
348 /* IMAGE_REL_PPC_ABSOLUTE 0x0000 NOP */
350 HOWTO (IMAGE_REL_PPC_ABSOLUTE, /* type */
352 0, /* size (0 = byte, 1 = short, 2 = long) */
354 FALSE, /* pc_relative */
356 complain_overflow_dont, /* dont complain_on_overflow */
357 0, /* special_function */
358 "ABSOLUTE", /* name */
359 FALSE, /* partial_inplace */
362 FALSE), /* pcrel_offset */
364 /* IMAGE_REL_PPC_ADDR64 0x0001 64-bit address */
366 HOWTO(IMAGE_REL_PPC_ADDR64, /* type */
368 3, /* size (0 = byte, 1 = short, 2 = long) */
370 FALSE, /* pc_relative */
372 complain_overflow_bitfield, /* complain_on_overflow */
373 0, /* special_function */
375 TRUE, /* partial_inplace */
376 MINUS_ONE, /* src_mask */
377 MINUS_ONE, /* dst_mask */
378 FALSE), /* pcrel_offset */
380 /* IMAGE_REL_PPC_ADDR32 0x0002 32-bit address */
382 HOWTO (IMAGE_REL_PPC_ADDR32, /* type */
384 2, /* size (0 = byte, 1 = short, 2 = long) */
386 FALSE, /* pc_relative */
388 complain_overflow_bitfield, /* complain_on_overflow */
389 0, /* special_function */
391 TRUE, /* partial_inplace */
392 0xffffffff, /* src_mask */
393 0xffffffff, /* dst_mask */
394 FALSE), /* pcrel_offset */
396 /* IMAGE_REL_PPC_ADDR24 0x0003 26-bit address, shifted left 2 (branch absolute) */
397 /* the LI field is in bit 6 through bit 29 is 24 bits, + 2 for the shift */
398 /* Of course, That's the IBM approved bit numbering, which is not what */
399 /* anyone else uses.... The li field is in bit 2 thru 25 */
401 HOWTO (IMAGE_REL_PPC_ADDR24, /* type */
403 2, /* size (0 = byte, 1 = short, 2 = long) */
405 FALSE, /* pc_relative */
407 complain_overflow_bitfield, /* complain_on_overflow */
408 0, /* special_function */
410 TRUE, /* partial_inplace */
411 0x07fffffc, /* src_mask */
412 0x07fffffc, /* dst_mask */
413 FALSE), /* pcrel_offset */
415 /* IMAGE_REL_PPC_ADDR16 0x0004 16-bit address */
417 HOWTO (IMAGE_REL_PPC_ADDR16, /* type */
419 1, /* size (0 = byte, 1 = short, 2 = long) */
421 FALSE, /* pc_relative */
423 complain_overflow_signed, /* complain_on_overflow */
424 0, /* special_function */
426 TRUE, /* partial_inplace */
427 0xffff, /* src_mask */
428 0xffff, /* dst_mask */
429 FALSE), /* pcrel_offset */
431 /* IMAGE_REL_PPC_ADDR14 0x0005 */
432 /* 16-bit address, shifted left 2 (load doubleword) */
433 /* FIXME: the mask is likely wrong, and the bit position may be as well */
435 HOWTO (IMAGE_REL_PPC_ADDR14, /* type */
437 1, /* size (0 = byte, 1 = short, 2 = long) */
439 FALSE, /* pc_relative */
441 complain_overflow_signed, /* complain_on_overflow */
442 0, /* special_function */
444 TRUE, /* partial_inplace */
445 0xffff, /* src_mask */
446 0xffff, /* dst_mask */
447 FALSE), /* pcrel_offset */
449 /* IMAGE_REL_PPC_REL24 0x0006 */
450 /* 26-bit PC-relative offset, shifted left 2 (branch relative) */
452 HOWTO (IMAGE_REL_PPC_REL24, /* type */
454 2, /* size (0 = byte, 1 = short, 2 = long) */
456 TRUE, /* pc_relative */
458 complain_overflow_signed, /* complain_on_overflow */
459 0, /* special_function */
461 TRUE, /* partial_inplace */
462 0x3fffffc, /* src_mask */
463 0x3fffffc, /* dst_mask */
464 FALSE), /* pcrel_offset */
466 /* IMAGE_REL_PPC_REL14 0x0007 */
467 /* 16-bit PC-relative offset, shifted left 2 (br cond relative) */
468 /* FIXME: the mask is likely wrong, and the bit position may be as well */
469 /* FIXME: how does it know how far to shift? */
471 HOWTO (IMAGE_REL_PPC_ADDR14, /* type */
473 1, /* size (0 = byte, 1 = short, 2 = long) */
475 FALSE, /* pc_relative */
477 complain_overflow_signed, /* complain_on_overflow */
478 0, /* special_function */
480 TRUE, /* partial_inplace */
481 0xffff, /* src_mask */
482 0xffff, /* dst_mask */
483 TRUE), /* pcrel_offset */
485 /* IMAGE_REL_PPC_TOCREL16 0x0008 */
486 /* 16-bit offset from TOC base */
488 HOWTO (IMAGE_REL_PPC_TOCREL16,/* type */
490 1, /* size (0 = byte, 1 = short, 2 = long) */
492 FALSE, /* pc_relative */
494 complain_overflow_dont, /* complain_on_overflow */
495 ppc_toc16_reloc, /* special_function */
496 "TOCREL16", /* name */
497 FALSE, /* partial_inplace */
498 0xffff, /* src_mask */
499 0xffff, /* dst_mask */
500 FALSE), /* pcrel_offset */
502 /* IMAGE_REL_PPC_TOCREL14 0x0009 */
503 /* 16-bit offset from TOC base, shifted left 2 (load doubleword) */
505 HOWTO (IMAGE_REL_PPC_TOCREL14,/* type */
507 1, /* size (0 = byte, 1 = short, 2 = long) */
509 FALSE, /* pc_relative */
511 complain_overflow_signed, /* complain_on_overflow */
512 0, /* special_function */
513 "TOCREL14", /* name */
514 FALSE, /* partial_inplace */
515 0xffff, /* src_mask */
516 0xffff, /* dst_mask */
517 FALSE), /* pcrel_offset */
519 /* IMAGE_REL_PPC_ADDR32NB 0x000A */
520 /* 32-bit addr w/ image base */
522 HOWTO (IMAGE_REL_PPC_ADDR32NB,/* type */
524 2, /* size (0 = byte, 1 = short, 2 = long) */
526 FALSE, /* pc_relative */
528 complain_overflow_signed, /* complain_on_overflow */
529 0, /* special_function */
530 "ADDR32NB", /* name */
531 TRUE, /* partial_inplace */
532 0xffffffff, /* src_mask */
533 0xffffffff, /* dst_mask */
534 FALSE), /* pcrel_offset */
536 /* IMAGE_REL_PPC_SECREL 0x000B */
537 /* va of containing section (as in an image sectionhdr) */
539 HOWTO (IMAGE_REL_PPC_SECREL,/* type */
541 2, /* size (0 = byte, 1 = short, 2 = long) */
543 FALSE, /* pc_relative */
545 complain_overflow_signed, /* complain_on_overflow */
546 ppc_secrel_reloc, /* special_function */
548 TRUE, /* partial_inplace */
549 0xffffffff, /* src_mask */
550 0xffffffff, /* dst_mask */
551 TRUE), /* pcrel_offset */
553 /* IMAGE_REL_PPC_SECTION 0x000C */
554 /* sectionheader number */
556 HOWTO (IMAGE_REL_PPC_SECTION,/* type */
558 2, /* size (0 = byte, 1 = short, 2 = long) */
560 FALSE, /* pc_relative */
562 complain_overflow_signed, /* complain_on_overflow */
563 ppc_section_reloc, /* special_function */
564 "SECTION", /* name */
565 TRUE, /* partial_inplace */
566 0xffffffff, /* src_mask */
567 0xffffffff, /* dst_mask */
568 TRUE), /* pcrel_offset */
570 /* IMAGE_REL_PPC_IFGLUE 0x000D */
571 /* substitute TOC restore instruction iff symbol is glue code */
573 HOWTO (IMAGE_REL_PPC_IFGLUE,/* type */
575 2, /* size (0 = byte, 1 = short, 2 = long) */
577 FALSE, /* pc_relative */
579 complain_overflow_signed, /* complain_on_overflow */
580 0, /* special_function */
582 TRUE, /* partial_inplace */
583 0xffffffff, /* src_mask */
584 0xffffffff, /* dst_mask */
585 FALSE), /* pcrel_offset */
587 /* IMAGE_REL_PPC_IMGLUE 0x000E */
588 /* symbol is glue code; virtual address is TOC restore instruction */
590 HOWTO (IMAGE_REL_PPC_IMGLUE,/* type */
592 2, /* size (0 = byte, 1 = short, 2 = long) */
594 FALSE, /* pc_relative */
596 complain_overflow_dont, /* complain_on_overflow */
597 ppc_imglue_reloc, /* special_function */
599 FALSE, /* partial_inplace */
600 0xffffffff, /* src_mask */
601 0xffffffff, /* dst_mask */
602 FALSE), /* pcrel_offset */
604 /* IMAGE_REL_PPC_SECREL16 0x000F */
605 /* va of containing section (limited to 16 bits) */
607 HOWTO (IMAGE_REL_PPC_SECREL16,/* type */
609 1, /* size (0 = byte, 1 = short, 2 = long) */
611 FALSE, /* pc_relative */
613 complain_overflow_signed, /* complain_on_overflow */
614 0, /* special_function */
615 "SECREL16", /* name */
616 TRUE, /* partial_inplace */
617 0xffff, /* src_mask */
618 0xffff, /* dst_mask */
619 TRUE), /* pcrel_offset */
621 /* IMAGE_REL_PPC_REFHI 0x0010 */
623 HOWTO (IMAGE_REL_PPC_REFHI, /* type */
625 1, /* size (0 = byte, 1 = short, 2 = long) */
627 FALSE, /* pc_relative */
629 complain_overflow_signed, /* complain_on_overflow */
630 ppc_refhi_reloc, /* special_function */
632 TRUE, /* partial_inplace */
633 0xffffffff, /* src_mask */
634 0xffffffff, /* dst_mask */
635 FALSE), /* pcrel_offset */
637 /* IMAGE_REL_PPC_REFLO 0x0011 */
639 HOWTO (IMAGE_REL_PPC_REFLO, /* type */
641 1, /* size (0 = byte, 1 = short, 2 = long) */
643 FALSE, /* pc_relative */
645 complain_overflow_signed, /* complain_on_overflow */
646 ppc_refhi_reloc, /* special_function */
648 TRUE, /* partial_inplace */
649 0xffffffff, /* src_mask */
650 0xffffffff, /* dst_mask */
651 FALSE), /* pcrel_offset */
653 /* IMAGE_REL_PPC_PAIR 0x0012 */
655 HOWTO (IMAGE_REL_PPC_PAIR, /* type */
657 1, /* size (0 = byte, 1 = short, 2 = long) */
659 FALSE, /* pc_relative */
661 complain_overflow_signed, /* complain_on_overflow */
662 ppc_pair_reloc, /* special_function */
664 TRUE, /* partial_inplace */
665 0xffffffff, /* src_mask */
666 0xffffffff, /* dst_mask */
667 FALSE), /* pcrel_offset */
669 /* IMAGE_REL_PPC_TOCREL16_DEFN 0x0013 */
670 /* 16-bit offset from TOC base, without causing a definition */
672 HOWTO ( (IMAGE_REL_PPC_TOCREL16 | IMAGE_REL_PPC_TOCDEFN), /* type */
674 1, /* size (0 = byte, 1 = short, 2 = long) */
676 FALSE, /* pc_relative */
678 complain_overflow_dont, /* complain_on_overflow */
679 0, /* special_function */
680 "TOCREL16, TOCDEFN", /* name */
681 FALSE, /* partial_inplace */
682 0xffff, /* src_mask */
683 0xffff, /* dst_mask */
684 FALSE), /* pcrel_offset */
688 /* Some really cheezy macros that can be turned on to test stderr :-) */
697 fprintf (stderr,_("Unimplemented Relocation -- %s\n"),x); \
701 #define DUMP_RELOC(n,r) \
703 fprintf (stderr,"%s sym %d, addr %d, addend %d\n", \
704 n, (*(r->sym_ptr_ptr))->name, \
705 r->address, r->addend); \
708 /* Given a reloc name, n, and a pointer to an internal_reloc,
709 dump out interesting information on the contents
711 #define n_name _n._n_name
712 #define n_zeroes _n._n_n._n_zeroes
713 #define n_offset _n._n_n._n_offset */
715 #define DUMP_RELOC2(n,r) \
717 fprintf (stderr,"%s sym %d, r_vaddr %d %s\n", \
718 n, r->r_symndx, r->r_vaddr, \
719 (((r->r_type) & IMAGE_REL_PPC_TOCDEFN) == 0) \
725 #define DUMP_RELOC(n,r)
726 #define DUMP_RELOC2(n,r)
729 /* TOC construction and management routines. */
731 /* This file is compiled twice, and these variables are defined in one
732 of the compilations. FIXME: This is confusing and weird. Also,
733 BFD should not use global variables. */
734 extern bfd * bfd_of_toc_owner;
735 extern long int global_toc_size;
736 extern long int import_table_size;
737 extern long int first_thunk_address;
738 extern long int thunk_size;
756 struct list_ele *next;
758 enum ref_category cat;
763 extern struct list_ele *head;
764 extern struct list_ele *tail;
767 record_toc (asection *toc_section,
768 bfd_signed_vma our_toc_offset,
769 enum ref_category cat,
772 /* Add this entry to our toc addr-offset-name list. */
773 bfd_size_type amt = sizeof (struct list_ele);
774 struct list_ele *t = (struct list_ele *) bfd_malloc (amt);
779 t->offset = our_toc_offset;
782 t->addr = toc_section->output_offset + our_toc_offset;
796 #ifdef COFF_IMAGE_WITH_PE
798 /* Record a toc offset against a symbol. */
800 ppc_record_toc_entry (bfd *abfd,
801 struct bfd_link_info *info ATTRIBUTE_UNUSED,
802 asection *sec ATTRIBUTE_UNUSED,
804 enum toc_type toc_kind ATTRIBUTE_UNUSED)
806 struct ppc_coff_link_hash_entry *h;
811 h = (struct ppc_coff_link_hash_entry *) (obj_coff_sym_hashes (abfd)[sym]);
819 local_syms = obj_coff_local_toc_table(abfd);
826 /* allocate a table */
827 amt = (bfd_size_type) obj_raw_syment_count (abfd) * sizeof (int);
828 local_syms = (int *) bfd_zalloc (abfd, amt);
831 obj_coff_local_toc_table (abfd) = local_syms;
833 for (i = 0; i < obj_raw_syment_count (abfd); ++i)
835 SET_UNALLOCATED (local_syms[i]);
839 if (IS_UNALLOCATED(local_syms[sym]))
841 local_syms[sym] = global_toc_size;
842 global_toc_size += 4;
844 /* The size must fit in a 16-bit displacement. */
845 if (global_toc_size > 65535)
847 (*_bfd_error_handler) (_("TOC overflow"));
848 bfd_set_error (bfd_error_file_too_big);
855 /* Check to see if there's a toc slot allocated. If not, do it
856 here. It will be used in relocate_section. */
857 if (IS_UNALLOCATED(h->toc_offset))
859 h->toc_offset = global_toc_size;
860 global_toc_size += 4;
862 /* The size must fit in a 16-bit displacement. */
863 if (global_toc_size >= 65535)
865 (*_bfd_error_handler) (_("TOC overflow"));
866 bfd_set_error (bfd_error_file_too_big);
875 /* Record a toc offset against a symbol. */
877 ppc_mark_symbol_as_glue (bfd *abfd,
879 struct internal_reloc *rel)
881 struct ppc_coff_link_hash_entry *h;
883 h = (struct ppc_coff_link_hash_entry *) (obj_coff_sym_hashes (abfd)[sym]);
887 h->symbol_is_glue = 1;
888 h->glue_insn = bfd_get_32 (abfd, (bfd_byte *) &rel->r_vaddr);
893 #endif /* COFF_IMAGE_WITH_PE */
895 /* Return TRUE if this relocation should
896 appear in the output .reloc section. */
899 in_reloc_p (bfd * abfd ATTRIBUTE_UNUSED,
900 reloc_howto_type *howto)
903 (! howto->pc_relative)
904 && (howto->type != IMAGE_REL_PPC_ADDR32NB)
905 && (howto->type != IMAGE_REL_PPC_TOCREL16)
906 && (howto->type != IMAGE_REL_PPC_IMGLUE)
907 && (howto->type != IMAGE_REL_PPC_IFGLUE)
908 && (howto->type != IMAGE_REL_PPC_SECREL)
909 && (howto->type != IMAGE_REL_PPC_SECTION)
910 && (howto->type != IMAGE_REL_PPC_SECREL16)
911 && (howto->type != IMAGE_REL_PPC_REFHI)
912 && (howto->type != IMAGE_REL_PPC_REFLO)
913 && (howto->type != IMAGE_REL_PPC_PAIR)
914 && (howto->type != IMAGE_REL_PPC_TOCREL16_DEFN) ;
918 write_base_file_entry (bfd *obfd, struct bfd_link_info *info, bfd_vma addr)
920 if (coff_data (obfd)->pe)
921 addr -= pe_data (obfd)->pe_opthdr.ImageBase;
922 if (fwrite (&addr, sizeof (addr), 1, (FILE *) info->base_file) == 1)
925 bfd_set_error (bfd_error_system_call);
929 /* The reloc processing routine for the optimized COFF linker. */
932 coff_ppc_relocate_section (bfd *output_bfd,
933 struct bfd_link_info *info,
935 asection *input_section,
937 struct internal_reloc *relocs,
938 struct internal_syment *syms,
941 struct internal_reloc *rel;
942 struct internal_reloc *relend;
943 asection *toc_section = 0;
945 reloc_howto_type *howto = 0;
947 /* If we are performing a relocatable link, we don't need to do a
948 thing. The caller will take care of adjusting the reloc
949 addresses and symbol indices. */
950 if (info->relocatable)
954 relend = rel + input_section->reloc_count;
955 for (; rel < relend; rel++)
958 struct ppc_coff_link_hash_entry *h;
959 struct internal_syment *sym;
963 bfd_reloc_status_type rstat;
966 unsigned short r_type = EXTRACT_TYPE (rel->r_type);
967 unsigned short r_flags = EXTRACT_FLAGS(rel->r_type);
969 symndx = rel->r_symndx;
970 loc = contents + rel->r_vaddr - input_section->vma;
972 /* FIXME: check bounds on r_type */
973 howto = ppc_coff_howto_table + r_type;
982 h = (struct ppc_coff_link_hash_entry *)
983 (obj_coff_sym_hashes (input_bfd)[symndx]);
992 if (r_type == IMAGE_REL_PPC_IMGLUE && h == 0)
994 /* An IMGLUE reloc must have a name. Something is very wrong. */
1001 /* FIXME: PAIR unsupported in the following code. */
1005 sec = bfd_abs_section_ptr;
1008 sec = sections[symndx];
1009 val = (sec->output_section->vma
1010 + sec->output_offset
1012 if (! obj_pe (output_bfd))
1020 if (h->root.root.type == bfd_link_hash_defined
1021 || h->root.root.type == bfd_link_hash_defweak)
1023 sec = h->root.root.u.def.section;
1024 val = (h->root.root.u.def.value
1025 + sec->output_section->vma
1026 + sec->output_offset);
1030 if (! ((*info->callbacks->undefined_symbol)
1031 (info, h->root.root.root.string, input_bfd, input_section,
1032 rel->r_vaddr - input_section->vma, TRUE)))
1037 rstat = bfd_reloc_ok;
1039 /* Each case must do its own relocation, setting rstat appropriately. */
1043 (*_bfd_error_handler)
1044 (_("%B: unsupported relocation type 0x%02x"), input_bfd, r_type);
1045 bfd_set_error (bfd_error_bad_value);
1047 case IMAGE_REL_PPC_TOCREL16:
1049 bfd_signed_vma our_toc_offset;
1052 DUMP_RELOC2(howto->name, rel);
1054 if (toc_section == 0)
1056 toc_section = bfd_get_section_by_name (bfd_of_toc_owner,
1059 if ( toc_section == NULL )
1061 /* There is no toc section. Something is very wrong. */
1066 /* Amazing bit tricks present. As we may have seen earlier, we
1067 use the 1 bit to tell us whether or not a toc offset has been
1068 allocated. Now that they've all been allocated, we will use
1069 the 1 bit to tell us if we've written this particular toc
1074 /* It is a file local symbol. */
1075 int *local_toc_table;
1076 char name[SYMNMLEN + 1];
1078 sym = syms + symndx;
1079 strncpy (name, sym->_n._n_name, SYMNMLEN);
1080 name[SYMNMLEN] = '\0';
1082 local_toc_table = obj_coff_local_toc_table(input_bfd);
1083 our_toc_offset = local_toc_table[symndx];
1085 if (IS_WRITTEN(our_toc_offset))
1087 /* If it has been written out, it is marked with the
1088 1 bit. Fix up our offset, but do not write it out
1090 MAKE_ADDR_AGAIN(our_toc_offset);
1094 /* Write out the toc entry. */
1095 record_toc (toc_section, our_toc_offset, priv,
1098 bfd_put_32 (output_bfd, val,
1099 toc_section->contents + our_toc_offset);
1101 MARK_AS_WRITTEN(local_toc_table[symndx]);
1107 const char *name = h->root.root.root.string;
1108 our_toc_offset = h->toc_offset;
1110 if ((r_flags & IMAGE_REL_PPC_TOCDEFN)
1111 == IMAGE_REL_PPC_TOCDEFN )
1113 /* This is unbelievable cheese. Some knowledgable asm
1114 hacker has decided to use r2 as a base for loading
1115 a value. He/She does this by setting the tocdefn bit,
1116 and not supplying a toc definition. The behaviour is
1117 then to use the difference between the value of the
1118 symbol and the actual location of the toc as the toc
1121 In fact, what is usually happening is, because the
1122 Import Address Table is mapped immediately following
1123 the toc, some trippy library code trying for speed on
1124 dll linkage, takes advantage of that and considers
1125 the IAT to be part of the toc, thus saving a load. */
1127 our_toc_offset = val - (toc_section->output_section->vma
1128 + toc_section->output_offset);
1130 /* The size must still fit in a 16-bit displacement. */
1131 if ((bfd_vma) our_toc_offset >= 65535)
1133 (*_bfd_error_handler)
1134 (_("%B: Relocation for %s of %lx exceeds Toc size limit"),
1136 (unsigned long) our_toc_offset);
1137 bfd_set_error (bfd_error_bad_value);
1141 record_toc (toc_section, our_toc_offset, pub,
1144 else if (IS_WRITTEN (our_toc_offset))
1146 /* If it has been written out, it is marked with the
1147 1 bit. Fix up our offset, but do not write it out
1149 MAKE_ADDR_AGAIN(our_toc_offset);
1153 record_toc(toc_section, our_toc_offset, pub,
1156 /* Write out the toc entry. */
1157 bfd_put_32 (output_bfd, val,
1158 toc_section->contents + our_toc_offset);
1160 MARK_AS_WRITTEN(h->toc_offset);
1161 /* The tricky part is that this is the address that
1162 needs a .reloc entry for it. */
1167 if (fixit && info->base_file)
1169 /* So if this is non pcrelative, and is referenced
1170 to a section or a common symbol, then it needs a reloc. */
1172 /* Relocation to a symbol in a section which
1173 isn't absolute - we output the address here
1175 bfd_vma addr = (toc_section->output_section->vma
1176 + toc_section->output_offset + our_toc_offset);
1178 if (!write_base_file_entry (output_bfd, info, addr))
1182 /* FIXME: this test is conservative. */
1183 if ((r_flags & IMAGE_REL_PPC_TOCDEFN) != IMAGE_REL_PPC_TOCDEFN
1184 && (bfd_vma) our_toc_offset > toc_section->size)
1186 (*_bfd_error_handler)
1187 (_("%B: Relocation exceeds allocated TOC (%lx)"),
1188 input_bfd, (unsigned long) toc_section->size);
1189 bfd_set_error (bfd_error_bad_value);
1193 /* Now we know the relocation for this toc reference. */
1194 relocation = our_toc_offset + TOC_LOAD_ADJUSTMENT;
1195 rstat = _bfd_relocate_contents (howto, input_bfd, relocation, loc);
1198 case IMAGE_REL_PPC_IFGLUE:
1200 /* To solve this, we need to know whether or not the symbol
1201 appearing on the call instruction is a glue function or not.
1202 A glue function must announce itself via a IMGLUE reloc, and
1203 the reloc contains the required toc restore instruction. */
1204 DUMP_RELOC2 (howto->name, rel);
1208 if (h->symbol_is_glue == 1)
1210 bfd_put_32 (input_bfd, (bfd_vma) h->glue_insn, loc);
1215 case IMAGE_REL_PPC_SECREL:
1216 /* Unimplemented: codeview debugging information. */
1217 /* For fast access to the header of the section
1218 containing the item. */
1220 case IMAGE_REL_PPC_SECTION:
1221 /* Unimplemented: codeview debugging information. */
1222 /* Is used to indicate that the value should be relative
1223 to the beginning of the section that contains the
1226 case IMAGE_REL_PPC_ABSOLUTE:
1228 const char *my_name;
1229 char buf[SYMNMLEN + 1];
1233 strncpy (buf, (syms+symndx)->_n._n_name, SYMNMLEN);
1234 buf[SYMNMLEN] = '\0';
1238 my_name = h->root.root.root.string;
1240 (*_bfd_error_handler)
1241 (_("Warning: unsupported reloc %s <file %B, section %A>\n"
1242 "sym %ld (%s), r_vaddr %ld (%lx)"),
1243 input_bfd, input_section, howto->name,
1244 rel->r_symndx, my_name, (long) rel->r_vaddr,
1245 (unsigned long) rel->r_vaddr);
1248 case IMAGE_REL_PPC_IMGLUE:
1250 /* There is nothing to do now. This reloc was noted in the first
1251 pass over the relocs, and the glue instruction extracted. */
1252 const char *my_name;
1254 if (h->symbol_is_glue == 1)
1256 my_name = h->root.root.root.string;
1258 (*_bfd_error_handler)
1259 (_("%B: Out of order IMGLUE reloc for %s"), input_bfd, my_name);
1260 bfd_set_error (bfd_error_bad_value);
1264 case IMAGE_REL_PPC_ADDR32NB:
1266 const char *name = 0;
1268 DUMP_RELOC2 (howto->name, rel);
1270 if (CONST_STRNEQ (input_section->name, ".idata$2") && first_thunk_address == 0)
1272 /* Set magic values. */
1274 struct coff_link_hash_entry *myh;
1276 myh = coff_link_hash_lookup (coff_hash_table (info),
1278 FALSE, FALSE, TRUE);
1279 first_thunk_address = myh->root.u.def.value +
1280 sec->output_section->vma +
1281 sec->output_offset -
1282 pe_data(output_bfd)->pe_opthdr.ImageBase;
1284 idata5offset = myh->root.u.def.value;
1285 myh = coff_link_hash_lookup (coff_hash_table (info),
1287 FALSE, FALSE, TRUE);
1289 thunk_size = myh->root.u.def.value - idata5offset;
1290 myh = coff_link_hash_lookup (coff_hash_table (info),
1292 FALSE, FALSE, TRUE);
1293 import_table_size = myh->root.u.def.value;
1297 /* It is a file local symbol. */
1298 sym = syms + symndx;
1303 name = h->root.root.root.string;
1304 if (strcmp (".idata$2", name) == 0)
1305 target = "__idata2_magic__";
1306 else if (strcmp (".idata$4", name) == 0)
1307 target = "__idata4_magic__";
1308 else if (strcmp (".idata$5", name) == 0)
1309 target = "__idata5_magic__";
1313 struct coff_link_hash_entry *myh;
1315 myh = coff_link_hash_lookup (coff_hash_table (info),
1317 FALSE, FALSE, TRUE);
1320 /* Missing magic cookies. Something is very wrong. */
1324 val = myh->root.u.def.value +
1325 sec->output_section->vma + sec->output_offset;
1326 if (first_thunk_address == 0)
1329 myh = coff_link_hash_lookup (coff_hash_table (info),
1331 FALSE, FALSE, TRUE);
1332 first_thunk_address = myh->root.u.def.value +
1333 sec->output_section->vma +
1334 sec->output_offset -
1335 pe_data(output_bfd)->pe_opthdr.ImageBase;
1337 idata5offset = myh->root.u.def.value;
1338 myh = coff_link_hash_lookup (coff_hash_table (info),
1340 FALSE, FALSE, TRUE);
1342 thunk_size = myh->root.u.def.value - idata5offset;
1343 myh = coff_link_hash_lookup (coff_hash_table (info),
1345 FALSE, FALSE, TRUE);
1346 import_table_size = myh->root.u.def.value;
1351 rstat = _bfd_relocate_contents (howto,
1354 pe_data (output_bfd)->pe_opthdr.ImageBase,
1359 case IMAGE_REL_PPC_REL24:
1360 DUMP_RELOC2(howto->name, rel);
1361 val -= (input_section->output_section->vma
1362 + input_section->output_offset);
1364 rstat = _bfd_relocate_contents (howto,
1369 case IMAGE_REL_PPC_ADDR16:
1370 case IMAGE_REL_PPC_ADDR24:
1371 case IMAGE_REL_PPC_ADDR32:
1372 DUMP_RELOC2(howto->name, rel);
1373 rstat = _bfd_relocate_contents (howto,
1380 if (info->base_file)
1382 /* So if this is non pcrelative, and is referenced
1383 to a section or a common symbol, then it needs a reloc. */
1384 if (sym && pe_data(output_bfd)->in_reloc_p (output_bfd, howto))
1386 /* Relocation to a symbol in a section which
1387 isn't absolute - we output the address here
1389 bfd_vma addr = (rel->r_vaddr
1390 - input_section->vma
1391 + input_section->output_offset
1392 + input_section->output_section->vma);
1394 if (!write_base_file_entry (output_bfd, info, addr))
1405 case bfd_reloc_overflow:
1408 char buf[SYMNMLEN + 1];
1414 else if (sym == NULL)
1416 else if (sym->_n._n_n._n_zeroes == 0
1417 && sym->_n._n_n._n_offset != 0)
1418 name = obj_coff_strings (input_bfd) + sym->_n._n_n._n_offset;
1421 strncpy (buf, sym->_n._n_name, SYMNMLEN);
1422 buf[SYMNMLEN] = '\0';
1426 if (! ((*info->callbacks->reloc_overflow)
1427 (info, (h ? &h->root.root : NULL), name, howto->name,
1428 (bfd_vma) 0, input_bfd,
1429 input_section, rel->r_vaddr - input_section->vma)))
1438 #ifdef COFF_IMAGE_WITH_PE
1440 /* FIXME: BFD should not use global variables. This file is compiled
1441 twice, and these variables are shared. This is confusing and
1444 long int global_toc_size = 4;
1446 bfd* bfd_of_toc_owner = 0;
1448 long int import_table_size;
1449 long int first_thunk_address;
1450 long int thunk_size;
1452 struct list_ele *head;
1453 struct list_ele *tail;
1456 h1 = N_("\n\t\t\tTOC MAPPING\n\n");
1458 h2 = N_(" TOC disassembly Comments Name\n");
1460 h3 = N_(" Offset spelling (if present)\n");
1463 dump_toc (void * vfile)
1465 FILE *file = (FILE *) vfile;
1468 fputs (_(h1), file);
1469 fputs (_(h2), file);
1470 fputs (_(h3), file);
1472 for (t = head; t != 0; t=t->next)
1474 const char *cat = "";
1477 cat = _("private ");
1478 else if (t->cat == pub)
1480 else if (t->cat == tocdata)
1481 cat = _("data-in-toc ");
1483 if (t->offset > global_toc_size)
1485 if (t->offset <= global_toc_size + thunk_size)
1486 cat = _("IAT reference ");
1490 _("**** global_toc_size %ld(%lx), thunk_size %ld(%lx)\n"),
1491 global_toc_size, (unsigned long) global_toc_size,
1492 thunk_size, (unsigned long) thunk_size);
1493 cat = _("Out of bounds!");
1498 " %04lx (%d)", (unsigned long) t->offset, t->offset - 32768);
1505 fprintf (file, "\n");
1509 ppc_allocate_toc_section (struct bfd_link_info *info ATTRIBUTE_UNUSED)
1514 static char test_char = '1';
1516 if ( global_toc_size == 0 ) /* FIXME: does this get me in trouble? */
1519 if (bfd_of_toc_owner == 0)
1520 /* No toc owner? Something is very wrong. */
1523 s = bfd_get_section_by_name ( bfd_of_toc_owner , TOC_SECTION_NAME);
1525 /* No toc section? Something is very wrong. */
1528 amt = global_toc_size;
1529 foo = (bfd_byte *) bfd_alloc (bfd_of_toc_owner, amt);
1530 memset(foo, test_char, (size_t) global_toc_size);
1532 s->size = global_toc_size;
1539 ppc_process_before_allocation (bfd *abfd,
1540 struct bfd_link_info *info)
1543 struct internal_reloc *i, *rel;
1545 /* Here we have a bfd that is to be included on the link. We have a hook
1546 to do reloc rummaging, before section sizes are nailed down. */
1547 _bfd_coff_get_external_symbols (abfd);
1549 /* Rummage around all the relocs and map the toc. */
1550 sec = abfd->sections;
1555 for (; sec != 0; sec = sec->next)
1557 if (sec->reloc_count == 0)
1560 /* load the relocs */
1561 /* FIXME: there may be a storage leak here */
1562 i=_bfd_coff_read_internal_relocs(abfd,sec,1,0,0,0);
1567 for (rel = i; rel < i + sec->reloc_count; ++rel)
1569 unsigned short r_type = EXTRACT_TYPE (rel->r_type);
1570 unsigned short r_flags = EXTRACT_FLAGS (rel->r_type);
1571 bfd_boolean ok = TRUE;
1573 DUMP_RELOC2 (ppc_coff_howto_table[r_type].name, rel);
1577 case IMAGE_REL_PPC_TOCREL16:
1578 /* If TOCDEFN is on, ignore as someone else has allocated the
1580 if ((r_flags & IMAGE_REL_PPC_TOCDEFN) != IMAGE_REL_PPC_TOCDEFN)
1581 ok = ppc_record_toc_entry(abfd, info, sec,
1582 rel->r_symndx, default_toc);
1586 case IMAGE_REL_PPC_IMGLUE:
1587 ppc_mark_symbol_as_glue (abfd, rel->r_symndx, rel);
1600 static bfd_reloc_status_type
1601 ppc_refhi_reloc (bfd *abfd ATTRIBUTE_UNUSED,
1602 arelent *reloc_entry ATTRIBUTE_UNUSED,
1603 asymbol *symbol ATTRIBUTE_UNUSED,
1604 void * data ATTRIBUTE_UNUSED,
1605 asection *input_section ATTRIBUTE_UNUSED,
1607 char **error_message ATTRIBUTE_UNUSED)
1610 DUMP_RELOC("REFHI",reloc_entry);
1612 if (output_bfd == (bfd *) NULL)
1613 return bfd_reloc_continue;
1615 return bfd_reloc_undefined;
1618 static bfd_reloc_status_type
1619 ppc_pair_reloc (bfd *abfd ATTRIBUTE_UNUSED,
1620 arelent *reloc_entry ATTRIBUTE_UNUSED,
1621 asymbol *symbol ATTRIBUTE_UNUSED,
1622 void * data ATTRIBUTE_UNUSED,
1623 asection *input_section ATTRIBUTE_UNUSED,
1625 char **error_message ATTRIBUTE_UNUSED)
1628 DUMP_RELOC("PAIR",reloc_entry);
1630 if (output_bfd == (bfd *) NULL)
1631 return bfd_reloc_continue;
1633 return bfd_reloc_undefined;
1636 static bfd_reloc_status_type
1637 ppc_toc16_reloc (bfd *abfd ATTRIBUTE_UNUSED,
1638 arelent *reloc_entry ATTRIBUTE_UNUSED,
1639 asymbol *symbol ATTRIBUTE_UNUSED,
1640 void * data ATTRIBUTE_UNUSED,
1641 asection *input_section ATTRIBUTE_UNUSED,
1643 char **error_message ATTRIBUTE_UNUSED)
1645 UN_IMPL ("TOCREL16");
1646 DUMP_RELOC ("TOCREL16",reloc_entry);
1648 if (output_bfd == (bfd *) NULL)
1649 return bfd_reloc_continue;
1651 return bfd_reloc_ok;
1654 static bfd_reloc_status_type
1655 ppc_secrel_reloc (bfd *abfd ATTRIBUTE_UNUSED,
1656 arelent *reloc_entry ATTRIBUTE_UNUSED,
1657 asymbol *symbol ATTRIBUTE_UNUSED,
1658 void * data ATTRIBUTE_UNUSED,
1659 asection *input_section ATTRIBUTE_UNUSED,
1661 char **error_message ATTRIBUTE_UNUSED)
1664 DUMP_RELOC("SECREL",reloc_entry);
1666 if (output_bfd == (bfd *) NULL)
1667 return bfd_reloc_continue;
1669 return bfd_reloc_ok;
1672 static bfd_reloc_status_type
1673 ppc_section_reloc (bfd *abfd ATTRIBUTE_UNUSED,
1674 arelent *reloc_entry ATTRIBUTE_UNUSED,
1675 asymbol *symbol ATTRIBUTE_UNUSED,
1676 void * data ATTRIBUTE_UNUSED,
1677 asection *input_section ATTRIBUTE_UNUSED,
1679 char **error_message ATTRIBUTE_UNUSED)
1682 DUMP_RELOC("SECTION",reloc_entry);
1684 if (output_bfd == (bfd *) NULL)
1685 return bfd_reloc_continue;
1687 return bfd_reloc_ok;
1690 static bfd_reloc_status_type
1691 ppc_imglue_reloc (bfd *abfd ATTRIBUTE_UNUSED,
1692 arelent *reloc_entry ATTRIBUTE_UNUSED,
1693 asymbol *symbol ATTRIBUTE_UNUSED,
1694 void * data ATTRIBUTE_UNUSED,
1695 asection *input_section ATTRIBUTE_UNUSED,
1697 char **error_message ATTRIBUTE_UNUSED)
1701 DUMP_RELOC("IMGLUE",reloc_entry);
1703 if (output_bfd == (bfd *) NULL)
1704 return bfd_reloc_continue;
1706 return bfd_reloc_ok;
1709 #define MAX_RELOC_INDEX \
1710 (sizeof (ppc_coff_howto_table) / sizeof (ppc_coff_howto_table[0]) - 1)
1712 /* FIXME: There is a possibility that when we read in a reloc from a file,
1713 that there are some bits encoded in the upper portion of the
1714 type field. Not yet implemented. */
1717 ppc_coff_rtype2howto (arelent *relent, struct internal_reloc *internal)
1719 /* We can encode one of three things in the type field, aside from the
1721 1. IMAGE_REL_PPC_NEG - indicates the value field is a subtraction
1722 value, rather than an addition value
1723 2. IMAGE_REL_PPC_BRTAKEN, IMAGE_REL_PPC_BRNTAKEN - indicates that
1724 the branch is expected to be taken or not.
1725 3. IMAGE_REL_PPC_TOCDEFN - toc slot definition in the file
1726 For now, we just strip this stuff to find the type, and ignore it other
1728 reloc_howto_type *howto;
1729 unsigned short r_type = EXTRACT_TYPE (internal->r_type);
1730 unsigned short r_flags = EXTRACT_FLAGS(internal->r_type);
1731 unsigned short junk = EXTRACT_JUNK (internal->r_type);
1733 /* The masking process only slices off the bottom byte for r_type. */
1734 if ( r_type > MAX_RELOC_INDEX )
1737 /* Check for absolute crap. */
1743 case IMAGE_REL_PPC_ADDR16:
1744 case IMAGE_REL_PPC_REL24:
1745 case IMAGE_REL_PPC_ADDR24:
1746 case IMAGE_REL_PPC_ADDR32:
1747 case IMAGE_REL_PPC_IFGLUE:
1748 case IMAGE_REL_PPC_ADDR32NB:
1749 case IMAGE_REL_PPC_SECTION:
1750 case IMAGE_REL_PPC_SECREL:
1751 DUMP_RELOC2 (ppc_coff_howto_table[r_type].name, internal);
1752 howto = ppc_coff_howto_table + r_type;
1754 case IMAGE_REL_PPC_IMGLUE:
1755 DUMP_RELOC2 (ppc_coff_howto_table[r_type].name, internal);
1756 howto = ppc_coff_howto_table + r_type;
1758 case IMAGE_REL_PPC_TOCREL16:
1759 DUMP_RELOC2 (ppc_coff_howto_table[r_type].name, internal);
1760 if (r_flags & IMAGE_REL_PPC_TOCDEFN)
1761 howto = ppc_coff_howto_table + IMAGE_REL_PPC_TOCREL16_DEFN;
1763 howto = ppc_coff_howto_table + IMAGE_REL_PPC_TOCREL16;
1766 (*_bfd_error_handler) (_("warning: unsupported reloc %s [%d] used -- it may not work"),
1767 ppc_coff_howto_table[r_type].name,
1769 howto = ppc_coff_howto_table + r_type;
1773 relent->howto = howto;
1776 static reloc_howto_type *
1777 coff_ppc_rtype_to_howto (bfd *abfd ATTRIBUTE_UNUSED,
1779 struct internal_reloc *rel,
1780 struct coff_link_hash_entry *h ATTRIBUTE_UNUSED,
1781 struct internal_syment *sym ATTRIBUTE_UNUSED,
1784 reloc_howto_type *howto;
1786 /* We can encode one of three things in the type field, aside from the
1788 1. IMAGE_REL_PPC_NEG - indicates the value field is a subtraction
1789 value, rather than an addition value
1790 2. IMAGE_REL_PPC_BRTAKEN, IMAGE_REL_PPC_BRNTAKEN - indicates that
1791 the branch is expected to be taken or not.
1792 3. IMAGE_REL_PPC_TOCDEFN - toc slot definition in the file
1793 For now, we just strip this stuff to find the type, and ignore it other
1796 unsigned short r_type = EXTRACT_TYPE (rel->r_type);
1797 unsigned short r_flags = EXTRACT_FLAGS (rel->r_type);
1798 unsigned short junk = EXTRACT_JUNK (rel->r_type);
1800 /* The masking process only slices off the bottom byte for r_type. */
1801 if (r_type > MAX_RELOC_INDEX)
1804 /* Check for absolute crap. */
1810 case IMAGE_REL_PPC_ADDR32NB:
1811 DUMP_RELOC2(ppc_coff_howto_table[r_type].name, rel);
1812 *addendp -= pe_data(sec->output_section->owner)->pe_opthdr.ImageBase;
1813 howto = ppc_coff_howto_table + r_type;
1815 case IMAGE_REL_PPC_TOCREL16:
1816 DUMP_RELOC2(ppc_coff_howto_table[r_type].name, rel);
1817 if (r_flags & IMAGE_REL_PPC_TOCDEFN)
1818 howto = ppc_coff_howto_table + IMAGE_REL_PPC_TOCREL16_DEFN;
1820 howto = ppc_coff_howto_table + IMAGE_REL_PPC_TOCREL16;
1822 case IMAGE_REL_PPC_ADDR16:
1823 case IMAGE_REL_PPC_REL24:
1824 case IMAGE_REL_PPC_ADDR24:
1825 case IMAGE_REL_PPC_ADDR32:
1826 case IMAGE_REL_PPC_IFGLUE:
1827 case IMAGE_REL_PPC_SECTION:
1828 case IMAGE_REL_PPC_SECREL:
1829 DUMP_RELOC2(ppc_coff_howto_table[r_type].name, rel);
1830 howto = ppc_coff_howto_table + r_type;
1832 case IMAGE_REL_PPC_IMGLUE:
1833 DUMP_RELOC2(ppc_coff_howto_table[r_type].name, rel);
1834 howto = ppc_coff_howto_table + r_type;
1837 (*_bfd_error_handler) (_("warning: unsupported reloc %s [%d] used -- it may not work"),
1838 ppc_coff_howto_table[r_type].name,
1840 howto = ppc_coff_howto_table + r_type;
1847 /* A cheesy little macro to make the code a little more readable. */
1848 #define HOW2MAP(bfd_rtype,ppc_rtype) \
1849 case bfd_rtype: return &ppc_coff_howto_table[ppc_rtype]
1851 static reloc_howto_type *
1852 ppc_coff_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
1853 bfd_reloc_code_real_type code)
1857 HOW2MAP(BFD_RELOC_32_GOTOFF, IMAGE_REL_PPC_IMGLUE);
1858 HOW2MAP(BFD_RELOC_16_GOT_PCREL, IMAGE_REL_PPC_IFGLUE);
1859 HOW2MAP(BFD_RELOC_16, IMAGE_REL_PPC_ADDR16);
1860 HOW2MAP(BFD_RELOC_PPC_B26, IMAGE_REL_PPC_REL24);
1861 HOW2MAP(BFD_RELOC_PPC_BA26, IMAGE_REL_PPC_ADDR24);
1862 HOW2MAP(BFD_RELOC_PPC_TOC16, IMAGE_REL_PPC_TOCREL16);
1863 HOW2MAP(BFD_RELOC_16_GOTOFF, IMAGE_REL_PPC_TOCREL16_DEFN);
1864 HOW2MAP(BFD_RELOC_32, IMAGE_REL_PPC_ADDR32);
1865 HOW2MAP(BFD_RELOC_RVA, IMAGE_REL_PPC_ADDR32NB);
1872 static reloc_howto_type *
1873 ppc_coff_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
1879 i < sizeof (ppc_coff_howto_table) / sizeof (ppc_coff_howto_table[0]);
1881 if (ppc_coff_howto_table[i].name != NULL
1882 && strcasecmp (ppc_coff_howto_table[i].name, r_name) == 0)
1883 return &ppc_coff_howto_table[i];
1888 /* Tailor coffcode.h -- macro heaven. */
1890 #define RTYPE2HOWTO(cache_ptr, dst) ppc_coff_rtype2howto (cache_ptr, dst)
1892 /* We use the special COFF backend linker, with our own special touch. */
1894 #define coff_bfd_reloc_type_lookup ppc_coff_reloc_type_lookup
1895 #define coff_bfd_reloc_name_lookup ppc_coff_reloc_name_lookup
1896 #define coff_rtype_to_howto coff_ppc_rtype_to_howto
1897 #define coff_relocate_section coff_ppc_relocate_section
1898 #define coff_bfd_final_link ppc_bfd_coff_final_link
1900 #ifndef COFF_IMAGE_WITH_PE
1903 #define SELECT_RELOC(internal, howto) {internal.r_type=howto->type;}
1905 #define COFF_PAGE_SIZE 0x1000
1907 /* FIXME: This controls some code that used to be in peicode.h and is
1908 now in peigen.c. It will not control the code in peigen.c. If
1909 anybody wants to get this working, you will need to fix that. */
1910 #define POWERPC_LE_PE
1912 #define COFF_SECTION_ALIGNMENT_ENTRIES \
1913 { COFF_SECTION_NAME_EXACT_MATCH (".idata$2"), \
1914 COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 0 }, \
1915 { COFF_SECTION_NAME_EXACT_MATCH (".idata$3"), \
1916 COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 0 }, \
1917 { COFF_SECTION_NAME_EXACT_MATCH (".idata$4"), \
1918 COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 2 }, \
1919 { COFF_SECTION_NAME_EXACT_MATCH (".idata$5"), \
1920 COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 2 }, \
1921 { COFF_SECTION_NAME_EXACT_MATCH (".idata$6"), \
1922 COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 1 }, \
1923 { COFF_SECTION_NAME_EXACT_MATCH (".reloc"), \
1924 COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 1 }
1926 #include "coffcode.h"
1928 #ifndef COFF_IMAGE_WITH_PE
1931 ppc_do_last (bfd *abfd)
1933 if (abfd == bfd_of_toc_owner)
1942 return bfd_of_toc_owner;
1945 /* This piece of machinery exists only to guarantee that the bfd that holds
1946 the toc section is written last.
1948 This does depend on bfd_make_section attaching a new section to the
1949 end of the section list for the bfd.
1951 This is otherwise intended to be functionally the same as
1952 cofflink.c:_bfd_coff_final_link(). It is specifically different only
1953 where the POWERPC_LE_PE macro modifies the code. It is left in as a
1954 precise form of comment. krk@cygnus.com */
1956 /* Do the final link step. */
1959 ppc_bfd_coff_final_link (bfd *abfd, struct bfd_link_info *info)
1961 bfd_size_type symesz;
1962 struct coff_final_link_info flaginfo;
1963 bfd_boolean debug_merge_allocated;
1965 struct bfd_link_order *p;
1966 bfd_size_type max_sym_count;
1967 bfd_size_type max_lineno_count;
1968 bfd_size_type max_reloc_count;
1969 bfd_size_type max_output_reloc_count;
1970 bfd_size_type max_contents_size;
1971 file_ptr rel_filepos;
1973 file_ptr line_filepos;
1974 unsigned int linesz;
1976 bfd_byte *external_relocs = NULL;
1977 char strbuf[STRING_SIZE_SIZE];
1980 symesz = bfd_coff_symesz (abfd);
1982 flaginfo.info = info;
1983 flaginfo.output_bfd = abfd;
1984 flaginfo.strtab = NULL;
1985 flaginfo.section_info = NULL;
1986 flaginfo.last_file_index = -1;
1987 flaginfo.last_bf_index = -1;
1988 flaginfo.internal_syms = NULL;
1989 flaginfo.sec_ptrs = NULL;
1990 flaginfo.sym_indices = NULL;
1991 flaginfo.outsyms = NULL;
1992 flaginfo.linenos = NULL;
1993 flaginfo.contents = NULL;
1994 flaginfo.external_relocs = NULL;
1995 flaginfo.internal_relocs = NULL;
1996 debug_merge_allocated = FALSE;
1998 coff_data (abfd)->link_info = info;
2000 flaginfo.strtab = _bfd_stringtab_init ();
2001 if (flaginfo.strtab == NULL)
2004 if (! coff_debug_merge_hash_table_init (&flaginfo.debug_merge))
2006 debug_merge_allocated = TRUE;
2008 /* Compute the file positions for all the sections. */
2009 if (! abfd->output_has_begun)
2011 if (! bfd_coff_compute_section_file_positions (abfd))
2015 /* Count the line numbers and relocation entries required for the
2016 output file. Set the file positions for the relocs. */
2017 rel_filepos = obj_relocbase (abfd);
2018 relsz = bfd_coff_relsz (abfd);
2019 max_contents_size = 0;
2020 max_lineno_count = 0;
2021 max_reloc_count = 0;
2023 for (o = abfd->sections; o != NULL; o = o->next)
2026 o->lineno_count = 0;
2028 for (p = o->map_head.link_order; p != NULL; p = p->next)
2030 if (p->type == bfd_indirect_link_order)
2034 sec = p->u.indirect.section;
2036 /* Mark all sections which are to be included in the
2037 link. This will normally be every section. We need
2038 to do this so that we can identify any sections which
2039 the linker has decided to not include. */
2040 sec->linker_mark = TRUE;
2042 if (info->strip == strip_none
2043 || info->strip == strip_some)
2044 o->lineno_count += sec->lineno_count;
2046 if (info->relocatable)
2047 o->reloc_count += sec->reloc_count;
2049 if (sec->rawsize > max_contents_size)
2050 max_contents_size = sec->rawsize;
2051 if (sec->size > max_contents_size)
2052 max_contents_size = sec->size;
2053 if (sec->lineno_count > max_lineno_count)
2054 max_lineno_count = sec->lineno_count;
2055 if (sec->reloc_count > max_reloc_count)
2056 max_reloc_count = sec->reloc_count;
2058 else if (info->relocatable
2059 && (p->type == bfd_section_reloc_link_order
2060 || p->type == bfd_symbol_reloc_link_order))
2063 if (o->reloc_count == 0)
2067 o->flags |= SEC_RELOC;
2068 o->rel_filepos = rel_filepos;
2069 rel_filepos += o->reloc_count * relsz;
2073 /* If doing a relocatable link, allocate space for the pointers we
2075 if (info->relocatable)
2079 /* We use section_count + 1, rather than section_count, because
2080 the target_index fields are 1 based. */
2081 amt = abfd->section_count + 1;
2082 amt *= sizeof (struct coff_link_section_info);
2083 flaginfo.section_info = (struct coff_link_section_info *) bfd_malloc (amt);
2085 if (flaginfo.section_info == NULL)
2088 for (i = 0; i <= abfd->section_count; i++)
2090 flaginfo.section_info[i].relocs = NULL;
2091 flaginfo.section_info[i].rel_hashes = NULL;
2095 /* We now know the size of the relocs, so we can determine the file
2096 positions of the line numbers. */
2097 line_filepos = rel_filepos;
2098 linesz = bfd_coff_linesz (abfd);
2099 max_output_reloc_count = 0;
2101 for (o = abfd->sections; o != NULL; o = o->next)
2103 if (o->lineno_count == 0)
2104 o->line_filepos = 0;
2107 o->line_filepos = line_filepos;
2108 line_filepos += o->lineno_count * linesz;
2111 if (o->reloc_count != 0)
2113 /* We don't know the indices of global symbols until we have
2114 written out all the local symbols. For each section in
2115 the output file, we keep an array of pointers to hash
2116 table entries. Each entry in the array corresponds to a
2117 reloc. When we find a reloc against a global symbol, we
2118 set the corresponding entry in this array so that we can
2119 fix up the symbol index after we have written out all the
2122 Because of this problem, we also keep the relocs in
2123 memory until the end of the link. This wastes memory,
2124 but only when doing a relocatable link, which is not the
2126 BFD_ASSERT (info->relocatable);
2127 amt = o->reloc_count;
2128 amt *= sizeof (struct internal_reloc);
2129 flaginfo.section_info[o->target_index].relocs =
2130 (struct internal_reloc *) bfd_malloc (amt);
2131 amt = o->reloc_count;
2132 amt *= sizeof (struct coff_link_hash_entry *);
2133 flaginfo.section_info[o->target_index].rel_hashes =
2134 (struct coff_link_hash_entry **) bfd_malloc (amt);
2135 if (flaginfo.section_info[o->target_index].relocs == NULL
2136 || flaginfo.section_info[o->target_index].rel_hashes == NULL)
2139 if (o->reloc_count > max_output_reloc_count)
2140 max_output_reloc_count = o->reloc_count;
2143 /* Reset the reloc and lineno counts, so that we can use them to
2144 count the number of entries we have output so far. */
2146 o->lineno_count = 0;
2149 obj_sym_filepos (abfd) = line_filepos;
2151 /* Figure out the largest number of symbols in an input BFD. Take
2152 the opportunity to clear the output_has_begun fields of all the
2155 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
2159 sub->output_has_begun = FALSE;
2160 sz = obj_raw_syment_count (sub);
2161 if (sz > max_sym_count)
2165 /* Allocate some buffers used while linking. */
2166 amt = max_sym_count * sizeof (struct internal_syment);
2167 flaginfo.internal_syms = (struct internal_syment *) bfd_malloc (amt);
2168 amt = max_sym_count * sizeof (asection *);
2169 flaginfo.sec_ptrs = (asection **) bfd_malloc (amt);
2170 amt = max_sym_count * sizeof (long);
2171 flaginfo.sym_indices = (long *) bfd_malloc (amt);
2172 amt = (max_sym_count + 1) * symesz;
2173 flaginfo.outsyms = (bfd_byte *) bfd_malloc (amt);
2174 amt = max_lineno_count * bfd_coff_linesz (abfd);
2175 flaginfo.linenos = (bfd_byte *) bfd_malloc (amt);
2176 flaginfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
2177 flaginfo.external_relocs = (bfd_byte *) bfd_malloc (max_reloc_count * relsz);
2178 if (! info->relocatable)
2180 amt = max_reloc_count * sizeof (struct internal_reloc);
2181 flaginfo.internal_relocs = (struct internal_reloc *) bfd_malloc (amt);
2183 if ((flaginfo.internal_syms == NULL && max_sym_count > 0)
2184 || (flaginfo.sec_ptrs == NULL && max_sym_count > 0)
2185 || (flaginfo.sym_indices == NULL && max_sym_count > 0)
2186 || flaginfo.outsyms == NULL
2187 || (flaginfo.linenos == NULL && max_lineno_count > 0)
2188 || (flaginfo.contents == NULL && max_contents_size > 0)
2189 || (flaginfo.external_relocs == NULL && max_reloc_count > 0)
2190 || (! info->relocatable
2191 && flaginfo.internal_relocs == NULL
2192 && max_reloc_count > 0))
2195 /* We now know the position of everything in the file, except that
2196 we don't know the size of the symbol table and therefore we don't
2197 know where the string table starts. We just build the string
2198 table in memory as we go along. We process all the relocations
2199 for a single input file at once. */
2200 obj_raw_syment_count (abfd) = 0;
2202 if (coff_backend_info (abfd)->_bfd_coff_start_final_link)
2204 if (! bfd_coff_start_final_link (abfd, info))
2208 for (o = abfd->sections; o != NULL; o = o->next)
2210 for (p = o->map_head.link_order; p != NULL; p = p->next)
2212 if (p->type == bfd_indirect_link_order
2213 && (bfd_get_flavour (p->u.indirect.section->owner)
2214 == bfd_target_coff_flavour))
2216 sub = p->u.indirect.section->owner;
2217 #ifdef POWERPC_LE_PE
2218 if (! sub->output_has_begun && !ppc_do_last(sub))
2220 if (! sub->output_has_begun)
2223 if (! _bfd_coff_link_input_bfd (&flaginfo, sub))
2225 sub->output_has_begun = TRUE;
2228 else if (p->type == bfd_section_reloc_link_order
2229 || p->type == bfd_symbol_reloc_link_order)
2231 if (! _bfd_coff_reloc_link_order (abfd, &flaginfo, o, p))
2236 if (! _bfd_default_link_order (abfd, info, o, p))
2242 #ifdef POWERPC_LE_PE
2244 bfd* last_one = ppc_get_last();
2247 if (! _bfd_coff_link_input_bfd (&flaginfo, last_one))
2250 last_one->output_has_begun = TRUE;
2254 /* Free up the buffers used by _bfd_coff_link_input_bfd. */
2255 coff_debug_merge_hash_table_free (&flaginfo.debug_merge);
2256 debug_merge_allocated = FALSE;
2258 if (flaginfo.internal_syms != NULL)
2260 free (flaginfo.internal_syms);
2261 flaginfo.internal_syms = NULL;
2263 if (flaginfo.sec_ptrs != NULL)
2265 free (flaginfo.sec_ptrs);
2266 flaginfo.sec_ptrs = NULL;
2268 if (flaginfo.sym_indices != NULL)
2270 free (flaginfo.sym_indices);
2271 flaginfo.sym_indices = NULL;
2273 if (flaginfo.linenos != NULL)
2275 free (flaginfo.linenos);
2276 flaginfo.linenos = NULL;
2278 if (flaginfo.contents != NULL)
2280 free (flaginfo.contents);
2281 flaginfo.contents = NULL;
2283 if (flaginfo.external_relocs != NULL)
2285 free (flaginfo.external_relocs);
2286 flaginfo.external_relocs = NULL;
2288 if (flaginfo.internal_relocs != NULL)
2290 free (flaginfo.internal_relocs);
2291 flaginfo.internal_relocs = NULL;
2294 /* The value of the last C_FILE symbol is supposed to be the symbol
2295 index of the first external symbol. Write it out again if
2297 if (flaginfo.last_file_index != -1
2298 && (unsigned int) flaginfo.last_file.n_value != obj_raw_syment_count (abfd))
2302 flaginfo.last_file.n_value = obj_raw_syment_count (abfd);
2303 bfd_coff_swap_sym_out (abfd, &flaginfo.last_file,
2305 pos = obj_sym_filepos (abfd) + flaginfo.last_file_index * symesz;
2306 if (bfd_seek (abfd, pos, SEEK_SET) != 0
2307 || bfd_bwrite (flaginfo.outsyms, symesz, abfd) != symesz)
2311 /* Write out the global symbols. */
2312 flaginfo.failed = FALSE;
2313 bfd_hash_traverse (&info->hash->table, _bfd_coff_write_global_sym, &flaginfo);
2314 if (flaginfo.failed)
2317 /* The outsyms buffer is used by _bfd_coff_write_global_sym. */
2318 if (flaginfo.outsyms != NULL)
2320 free (flaginfo.outsyms);
2321 flaginfo.outsyms = NULL;
2324 if (info->relocatable)
2326 /* Now that we have written out all the global symbols, we know
2327 the symbol indices to use for relocs against them, and we can
2328 finally write out the relocs. */
2329 amt = max_output_reloc_count * relsz;
2330 external_relocs = (bfd_byte *) bfd_malloc (amt);
2331 if (external_relocs == NULL)
2334 for (o = abfd->sections; o != NULL; o = o->next)
2336 struct internal_reloc *irel;
2337 struct internal_reloc *irelend;
2338 struct coff_link_hash_entry **rel_hash;
2341 if (o->reloc_count == 0)
2344 irel = flaginfo.section_info[o->target_index].relocs;
2345 irelend = irel + o->reloc_count;
2346 rel_hash = flaginfo.section_info[o->target_index].rel_hashes;
2347 erel = external_relocs;
2348 for (; irel < irelend; irel++, rel_hash++, erel += relsz)
2350 if (*rel_hash != NULL)
2352 BFD_ASSERT ((*rel_hash)->indx >= 0);
2353 irel->r_symndx = (*rel_hash)->indx;
2355 bfd_coff_swap_reloc_out (abfd, irel, erel);
2358 amt = relsz * o->reloc_count;
2359 if (bfd_seek (abfd, o->rel_filepos, SEEK_SET) != 0
2360 || bfd_bwrite (external_relocs, amt, abfd) != amt)
2364 free (external_relocs);
2365 external_relocs = NULL;
2368 /* Free up the section information. */
2369 if (flaginfo.section_info != NULL)
2373 for (i = 0; i < abfd->section_count; i++)
2375 if (flaginfo.section_info[i].relocs != NULL)
2376 free (flaginfo.section_info[i].relocs);
2377 if (flaginfo.section_info[i].rel_hashes != NULL)
2378 free (flaginfo.section_info[i].rel_hashes);
2380 free (flaginfo.section_info);
2381 flaginfo.section_info = NULL;
2384 /* If we have optimized stabs strings, output them. */
2385 if (coff_hash_table (info)->stab_info.stabstr != NULL)
2387 if (! _bfd_write_stab_strings (abfd, &coff_hash_table (info)->stab_info))
2391 /* Write out the string table. */
2392 if (obj_raw_syment_count (abfd) != 0)
2396 pos = obj_sym_filepos (abfd) + obj_raw_syment_count (abfd) * symesz;
2397 if (bfd_seek (abfd, pos, SEEK_SET) != 0)
2400 #if STRING_SIZE_SIZE == 4
2402 _bfd_stringtab_size (flaginfo.strtab) + STRING_SIZE_SIZE,
2405 #error Change H_PUT_32 above
2408 if (bfd_bwrite (strbuf, (bfd_size_type) STRING_SIZE_SIZE, abfd)
2409 != STRING_SIZE_SIZE)
2412 if (! _bfd_stringtab_emit (abfd, flaginfo.strtab))
2416 _bfd_stringtab_free (flaginfo.strtab);
2418 /* Setting bfd_get_symcount to 0 will cause write_object_contents to
2419 not try to write out the symbols. */
2420 bfd_get_symcount (abfd) = 0;
2425 if (debug_merge_allocated)
2426 coff_debug_merge_hash_table_free (&flaginfo.debug_merge);
2427 if (flaginfo.strtab != NULL)
2428 _bfd_stringtab_free (flaginfo.strtab);
2429 if (flaginfo.section_info != NULL)
2433 for (i = 0; i < abfd->section_count; i++)
2435 if (flaginfo.section_info[i].relocs != NULL)
2436 free (flaginfo.section_info[i].relocs);
2437 if (flaginfo.section_info[i].rel_hashes != NULL)
2438 free (flaginfo.section_info[i].rel_hashes);
2440 free (flaginfo.section_info);
2442 if (flaginfo.internal_syms != NULL)
2443 free (flaginfo.internal_syms);
2444 if (flaginfo.sec_ptrs != NULL)
2445 free (flaginfo.sec_ptrs);
2446 if (flaginfo.sym_indices != NULL)
2447 free (flaginfo.sym_indices);
2448 if (flaginfo.outsyms != NULL)
2449 free (flaginfo.outsyms);
2450 if (flaginfo.linenos != NULL)
2451 free (flaginfo.linenos);
2452 if (flaginfo.contents != NULL)
2453 free (flaginfo.contents);
2454 if (flaginfo.external_relocs != NULL)
2455 free (flaginfo.external_relocs);
2456 if (flaginfo.internal_relocs != NULL)
2457 free (flaginfo.internal_relocs);
2458 if (external_relocs != NULL)
2459 free (external_relocs);
2464 /* Forward declaration for use by alternative_target field. */
2465 #ifdef TARGET_BIG_SYM
2466 extern const bfd_target TARGET_BIG_SYM;
2469 /* The transfer vectors that lead the outside world to all of the above. */
2471 #ifdef TARGET_LITTLE_SYM
2472 const bfd_target TARGET_LITTLE_SYM =
2474 TARGET_LITTLE_NAME, /* name or coff-arm-little */
2475 bfd_target_coff_flavour,
2476 BFD_ENDIAN_LITTLE, /* data byte order is little */
2477 BFD_ENDIAN_LITTLE, /* header byte order is little */
2479 (HAS_RELOC | EXEC_P | /* FIXME: object flags */
2480 HAS_LINENO | HAS_DEBUG |
2481 HAS_SYMS | HAS_LOCALS | WP_TEXT | D_PAGED),
2483 #ifndef COFF_WITH_PE
2484 (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY
2485 | SEC_RELOC), /* section flags */
2487 (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY
2488 | SEC_RELOC | SEC_LINK_ONCE | SEC_LINK_DUPLICATES),
2491 0, /* leading char */
2492 '/', /* ar_pad_char */
2493 15, /* ar_max_namelen??? FIXMEmgo */
2494 0, /* match priority. */
2496 bfd_getl64, bfd_getl_signed_64, bfd_putl64,
2497 bfd_getl32, bfd_getl_signed_32, bfd_putl32,
2498 bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* data */
2500 bfd_getl64, bfd_getl_signed_64, bfd_putl64,
2501 bfd_getl32, bfd_getl_signed_32, bfd_putl32,
2502 bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* hdrs */
2504 {_bfd_dummy_target, coff_object_p, /* bfd_check_format */
2505 bfd_generic_archive_p, /* _bfd_dummy_target */ coff_object_p },
2506 {bfd_false, coff_mkobject, _bfd_generic_mkarchive, /* bfd_set_format */
2508 {bfd_false, coff_write_object_contents, /* bfd_write_contents */
2509 _bfd_write_archive_contents, bfd_false},
2511 BFD_JUMP_TABLE_GENERIC (coff),
2512 BFD_JUMP_TABLE_COPY (coff),
2513 BFD_JUMP_TABLE_CORE (_bfd_nocore),
2514 BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_coff),
2515 BFD_JUMP_TABLE_SYMBOLS (coff),
2516 BFD_JUMP_TABLE_RELOCS (coff),
2517 BFD_JUMP_TABLE_WRITE (coff),
2518 BFD_JUMP_TABLE_LINK (coff),
2519 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
2521 /* Alternative_target. */
2522 #ifdef TARGET_BIG_SYM
2532 #ifdef TARGET_BIG_SYM
2533 const bfd_target TARGET_BIG_SYM =
2536 bfd_target_coff_flavour,
2537 BFD_ENDIAN_BIG, /* data byte order is big */
2538 BFD_ENDIAN_BIG, /* header byte order is big */
2540 (HAS_RELOC | EXEC_P | /* FIXME: object flags */
2541 HAS_LINENO | HAS_DEBUG |
2542 HAS_SYMS | HAS_LOCALS | WP_TEXT | D_PAGED),
2544 #ifndef COFF_WITH_PE
2545 (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY
2546 | SEC_RELOC), /* section flags */
2548 (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY
2549 | SEC_RELOC | SEC_LINK_ONCE | SEC_LINK_DUPLICATES),
2552 0, /* leading char */
2553 '/', /* ar_pad_char */
2554 15, /* ar_max_namelen??? FIXMEmgo */
2555 0, /* match priority. */
2557 bfd_getb64, bfd_getb_signed_64, bfd_putb64,
2558 bfd_getb32, bfd_getb_signed_32, bfd_putb32,
2559 bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* data */
2561 bfd_getb64, bfd_getb_signed_64, bfd_putb64,
2562 bfd_getb32, bfd_getb_signed_32, bfd_putb32,
2563 bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* hdrs */
2565 {_bfd_dummy_target, coff_object_p, /* bfd_check_format */
2566 bfd_generic_archive_p, /* _bfd_dummy_target */ coff_object_p },
2567 {bfd_false, coff_mkobject, _bfd_generic_mkarchive, /* bfd_set_format */
2569 {bfd_false, coff_write_object_contents, /* bfd_write_contents */
2570 _bfd_write_archive_contents, bfd_false},
2572 BFD_JUMP_TABLE_GENERIC (coff),
2573 BFD_JUMP_TABLE_COPY (coff),
2574 BFD_JUMP_TABLE_CORE (_bfd_nocore),
2575 BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_coff),
2576 BFD_JUMP_TABLE_SYMBOLS (coff),
2577 BFD_JUMP_TABLE_RELOCS (coff),
2578 BFD_JUMP_TABLE_WRITE (coff),
2579 BFD_JUMP_TABLE_LINK (coff),
2580 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
2582 /* Alternative_target. */
2583 #ifdef TARGET_LITTLE_SYM
2584 & TARGET_LITTLE_SYM,