* elf32-xtensa.c (xtensa_elf_dynamic_symbol_p): Renamed to...
[binutils.git] / bfd / coff-ppc.c
blob0ef3785234532de44a1443d09b67f8a9b9d01129
1 /* BFD back-end for PowerPC Microsoft Portable Executable files.
2 Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
3 2000, 2001, 2002, 2003, 2004, 2005, 2006
4 Free Software Foundation, Inc.
6 Original version pieced together by Kim Knuttila (krk@cygnus.com)
8 There is nothing new under the sun. This file draws a lot on other
9 coff files, in particular, those for the rs/6000, alpha, mips, and
10 intel backends, and the PE work for the arm.
12 This file is part of BFD, the Binary File Descriptor library.
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 2 of the License, or
17 (at your option) any later version.
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
24 You should have received a copy of the GNU General Public License
25 along with this program; if not, write to the Free Software
26 Foundation, 51 Franklin Street - Fifth Floor,
27 Boston, MA 02110-1301, USA. */
29 /* Current State:
30 - objdump works
31 - relocs generated by gas
32 - ld will link files, but they do not run.
33 - dlltool will not produce correct output in some .reloc cases, and will
34 not produce the right glue code for dll function calls. */
36 #include "bfd.h"
37 #include "sysdep.h"
39 #include "libbfd.h"
41 #include "coff/powerpc.h"
42 #include "coff/internal.h"
44 #include "coff/pe.h"
46 #ifdef BADMAG
47 #undef BADMAG
48 #endif
50 #define BADMAG(x) PPCBADMAG(x)
52 #include "libcoff.h"
54 /* This file is compiled more than once, but we only compile the
55 final_link routine once. */
56 extern bfd_boolean ppc_bfd_coff_final_link
57 PARAMS ((bfd *, struct bfd_link_info *));
58 extern void dump_toc PARAMS ((PTR));
60 /* The toc is a set of bfd_vma fields. We use the fact that valid
61 addresses are even (i.e. the bit representing "1" is off) to allow
62 us to encode a little extra information in the field
63 - Unallocated addresses are initialized to 1.
64 - Allocated addresses are even numbers.
65 The first time we actually write a reference to the toc in the bfd,
66 we want to record that fact in a fixup file (if it is asked for), so
67 we keep track of whether or not an address has been written by marking
68 the low order bit with a "1" upon writing. */
70 #define SET_UNALLOCATED(x) ((x) = 1)
71 #define IS_UNALLOCATED(x) ((x) == 1)
73 #define IS_WRITTEN(x) ((x) & 1)
74 #define MARK_AS_WRITTEN(x) ((x) |= 1)
75 #define MAKE_ADDR_AGAIN(x) ((x) &= ~1)
77 /* Turn on this check if you suspect something amiss in the hash tables. */
78 #ifdef DEBUG_HASH
80 /* Need a 7 char string for an eye catcher. */
81 #define EYE "krkjunk"
83 #define HASH_CHECK_DCL char eye_catcher[8];
84 #define HASH_CHECK_INIT(ret) strcpy(ret->eye_catcher, EYE)
85 #define HASH_CHECK(addr) \
86 if (strcmp(addr->eye_catcher, EYE) != 0) \
87 { \
88 fprintf (stderr,\
89 _("File %s, line %d, Hash check failure, bad eye %8s\n"), \
90 __FILE__, __LINE__, addr->eye_catcher); \
91 abort (); \
94 #else
96 #define HASH_CHECK_DCL
97 #define HASH_CHECK_INIT(ret)
98 #define HASH_CHECK(addr)
100 #endif
102 /* In order not to add an int to every hash table item for every coff
103 linker, we define our own hash table, derived from the coff one. */
105 /* PE linker hash table entries. */
107 struct ppc_coff_link_hash_entry
109 struct coff_link_hash_entry root; /* First entry, as required. */
111 /* As we wonder around the relocs, we'll keep the assigned toc_offset
112 here. */
113 bfd_vma toc_offset; /* Our addition, as required. */
114 int symbol_is_glue;
115 unsigned long int glue_insn;
117 HASH_CHECK_DCL
120 /* PE linker hash table. */
122 struct ppc_coff_link_hash_table
124 struct coff_link_hash_table root; /* First entry, as required. */
127 static struct bfd_hash_entry *ppc_coff_link_hash_newfunc
128 PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *,
129 const char *));
130 static struct bfd_link_hash_table *ppc_coff_link_hash_table_create
131 PARAMS ((bfd *));
132 static bfd_boolean coff_ppc_relocate_section
133 PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
134 struct internal_reloc *, struct internal_syment *, asection **));
135 static reloc_howto_type *coff_ppc_rtype_to_howto
136 PARAMS ((bfd *, asection *, struct internal_reloc *,
137 struct coff_link_hash_entry *, struct internal_syment *,
138 bfd_vma *));
140 /* Routine to create an entry in the link hash table. */
142 static struct bfd_hash_entry *
143 ppc_coff_link_hash_newfunc (entry, table, string)
144 struct bfd_hash_entry *entry;
145 struct bfd_hash_table *table;
146 const char *string;
148 struct ppc_coff_link_hash_entry *ret =
149 (struct ppc_coff_link_hash_entry *) entry;
151 /* Allocate the structure if it has not already been allocated by a
152 subclass. */
153 if (ret == (struct ppc_coff_link_hash_entry *) NULL)
154 ret = (struct ppc_coff_link_hash_entry *)
155 bfd_hash_allocate (table,
156 sizeof (struct ppc_coff_link_hash_entry));
158 if (ret == (struct ppc_coff_link_hash_entry *) NULL)
159 return NULL;
161 /* Call the allocation method of the superclass. */
162 ret = ((struct ppc_coff_link_hash_entry *)
163 _bfd_coff_link_hash_newfunc ((struct bfd_hash_entry *) ret,
164 table, string));
166 if (ret)
168 /* Initialize the local fields. */
169 SET_UNALLOCATED (ret->toc_offset);
170 ret->symbol_is_glue = 0;
171 ret->glue_insn = 0;
173 HASH_CHECK_INIT (ret);
176 return (struct bfd_hash_entry *) ret;
179 /* Initialize a PE linker hash table. */
181 static bfd_boolean
182 ppc_coff_link_hash_table_init (struct ppc_coff_link_hash_table *table,
183 bfd *abfd,
184 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
185 struct bfd_hash_table *,
186 const char *),
187 unsigned int entsize)
189 return _bfd_coff_link_hash_table_init (&table->root, abfd, newfunc, entsize);
192 /* Create a PE linker hash table. */
194 static struct bfd_link_hash_table *
195 ppc_coff_link_hash_table_create (abfd)
196 bfd *abfd;
198 struct ppc_coff_link_hash_table *ret;
199 bfd_size_type amt = sizeof (struct ppc_coff_link_hash_table);
201 ret = (struct ppc_coff_link_hash_table *) bfd_malloc (amt);
202 if (ret == NULL)
203 return NULL;
204 if (!ppc_coff_link_hash_table_init (ret, abfd,
205 ppc_coff_link_hash_newfunc,
206 sizeof (struct ppc_coff_link_hash_entry)))
208 free (ret);
209 return (struct bfd_link_hash_table *) NULL;
211 return &ret->root.root;
214 /* Now, tailor coffcode.h to use our hash stuff. */
216 #define coff_bfd_link_hash_table_create ppc_coff_link_hash_table_create
218 /* The nt loader points the toc register to &toc + 32768, in order to
219 use the complete range of a 16-bit displacement. We have to adjust
220 for this when we fix up loads displaced off the toc reg. */
221 #define TOC_LOAD_ADJUSTMENT (-32768)
222 #define TOC_SECTION_NAME ".private.toc"
224 /* The main body of code is in coffcode.h. */
226 #define COFF_DEFAULT_SECTION_ALIGNMENT_POWER (3)
228 /* In case we're on a 32-bit machine, construct a 64-bit "-1" value
229 from smaller values. Start with zero, widen, *then* decrement. */
230 #define MINUS_ONE (((bfd_vma)0) - 1)
232 /* These should definitely go in a header file somewhere... */
234 /* NOP */
235 #define IMAGE_REL_PPC_ABSOLUTE 0x0000
237 /* 64-bit address */
238 #define IMAGE_REL_PPC_ADDR64 0x0001
240 /* 32-bit address */
241 #define IMAGE_REL_PPC_ADDR32 0x0002
243 /* 26-bit address, shifted left 2 (branch absolute) */
244 #define IMAGE_REL_PPC_ADDR24 0x0003
246 /* 16-bit address */
247 #define IMAGE_REL_PPC_ADDR16 0x0004
249 /* 16-bit address, shifted left 2 (load doubleword) */
250 #define IMAGE_REL_PPC_ADDR14 0x0005
252 /* 26-bit PC-relative offset, shifted left 2 (branch relative) */
253 #define IMAGE_REL_PPC_REL24 0x0006
255 /* 16-bit PC-relative offset, shifted left 2 (br cond relative) */
256 #define IMAGE_REL_PPC_REL14 0x0007
258 /* 16-bit offset from TOC base */
259 #define IMAGE_REL_PPC_TOCREL16 0x0008
261 /* 16-bit offset from TOC base, shifted left 2 (load doubleword) */
262 #define IMAGE_REL_PPC_TOCREL14 0x0009
264 /* 32-bit addr w/o image base */
265 #define IMAGE_REL_PPC_ADDR32NB 0x000A
267 /* va of containing section (as in an image sectionhdr) */
268 #define IMAGE_REL_PPC_SECREL 0x000B
270 /* sectionheader number */
271 #define IMAGE_REL_PPC_SECTION 0x000C
273 /* substitute TOC restore instruction iff symbol is glue code */
274 #define IMAGE_REL_PPC_IFGLUE 0x000D
276 /* symbol is glue code; virtual address is TOC restore instruction */
277 #define IMAGE_REL_PPC_IMGLUE 0x000E
279 /* va of containing section (limited to 16 bits) */
280 #define IMAGE_REL_PPC_SECREL16 0x000F
282 /* Stuff to handle immediate data when the number of bits in the
283 data is greater than the number of bits in the immediate field
284 We need to do (usually) 32 bit arithmetic on 16 bit chunks. */
285 #define IMAGE_REL_PPC_REFHI 0x0010
286 #define IMAGE_REL_PPC_REFLO 0x0011
287 #define IMAGE_REL_PPC_PAIR 0x0012
289 /* This is essentially the same as tocrel16, with TOCDEFN assumed. */
290 #define IMAGE_REL_PPC_TOCREL16_DEFN 0x0013
292 /* Flag bits in IMAGE_RELOCATION.TYPE. */
294 /* Subtract reloc value rather than adding it. */
295 #define IMAGE_REL_PPC_NEG 0x0100
297 /* Fix branch prediction bit to predict branch taken. */
298 #define IMAGE_REL_PPC_BRTAKEN 0x0200
300 /* Fix branch prediction bit to predict branch not taken. */
301 #define IMAGE_REL_PPC_BRNTAKEN 0x0400
303 /* TOC slot defined in file (or, data in toc). */
304 #define IMAGE_REL_PPC_TOCDEFN 0x0800
306 /* Masks to isolate above values in IMAGE_RELOCATION.Type. */
307 #define IMAGE_REL_PPC_TYPEMASK 0x00FF
308 #define IMAGE_REL_PPC_FLAGMASK 0x0F00
310 #define EXTRACT_TYPE(x) ((x) & IMAGE_REL_PPC_TYPEMASK)
311 #define EXTRACT_FLAGS(x) ((x) & IMAGE_REL_PPC_FLAGMASK)
312 #define EXTRACT_JUNK(x) \
313 ((x) & ~(IMAGE_REL_PPC_TYPEMASK | IMAGE_REL_PPC_FLAGMASK))
315 /* Static helper functions to make relocation work. */
316 /* (Work In Progress) */
318 static bfd_reloc_status_type ppc_refhi_reloc PARAMS ((bfd *abfd,
319 arelent *reloc,
320 asymbol *symbol,
321 PTR data,
322 asection *section,
323 bfd *output_bfd,
324 char **error));
325 static bfd_reloc_status_type ppc_pair_reloc PARAMS ((bfd *abfd,
326 arelent *reloc,
327 asymbol *symbol,
328 PTR data,
329 asection *section,
330 bfd *output_bfd,
331 char **error));
333 static bfd_reloc_status_type ppc_toc16_reloc PARAMS ((bfd *abfd,
334 arelent *reloc,
335 asymbol *symbol,
336 PTR data,
337 asection *section,
338 bfd *output_bfd,
339 char **error));
341 static bfd_reloc_status_type ppc_section_reloc PARAMS ((bfd *abfd,
342 arelent *reloc,
343 asymbol *symbol,
344 PTR data,
345 asection *section,
346 bfd *output_bfd,
347 char **error));
349 static bfd_reloc_status_type ppc_secrel_reloc PARAMS ((bfd *abfd,
350 arelent *reloc,
351 asymbol *symbol,
352 PTR data,
353 asection *section,
354 bfd *output_bfd,
355 char **error));
357 static bfd_reloc_status_type ppc_imglue_reloc PARAMS ((bfd *abfd,
358 arelent *reloc,
359 asymbol *symbol,
360 PTR data,
361 asection *section,
362 bfd *output_bfd,
363 char **error));
365 static bfd_boolean in_reloc_p PARAMS((bfd *abfd, reloc_howto_type *howto));
367 /* FIXME: It'll take a while to get through all of these. I only need a few to
368 get us started, so those I'll make sure work. Those marked FIXME are either
369 completely unverified or have a specific unknown marked in the comment. */
371 /* Relocation entries for Windows/NT on PowerPC.
373 From the document "" we find the following listed as used relocs:
375 ABSOLUTE : The noop
376 ADDR[64|32|16] : fields that hold addresses in data fields or the
377 16 bit displacement field on a load/store.
378 ADDR[24|14] : fields that hold addresses in branch and cond
379 branches. These represent [26|16] bit addresses.
380 The low order 2 bits are preserved.
381 REL[24|14] : branches relative to the Instruction Address
382 register. These represent [26|16] bit addresses,
383 as before. The instruction field will be zero, and
384 the address of the SYM will be inserted at link time.
385 TOCREL16 : 16 bit displacement field referring to a slot in
386 toc.
387 TOCREL14 : 16 bit displacement field, similar to REL14 or ADDR14.
388 ADDR32NB : 32 bit address relative to the virtual origin.
389 (On the alpha, this is always a linker generated thunk)
390 (i.e. 32bit addr relative to the image base)
391 SECREL : The value is relative to the start of the section
392 containing the symbol.
393 SECTION : access to the header containing the item. Supports the
394 codeview debugger.
396 In particular, note that the document does not indicate that the
397 relocations listed in the header file are used. */
400 static reloc_howto_type ppc_coff_howto_table[] =
402 /* IMAGE_REL_PPC_ABSOLUTE 0x0000 NOP */
403 /* Unused: */
404 HOWTO (IMAGE_REL_PPC_ABSOLUTE, /* type */
405 0, /* rightshift */
406 0, /* size (0 = byte, 1 = short, 2 = long) */
407 0, /* bitsize */
408 FALSE, /* pc_relative */
409 0, /* bitpos */
410 complain_overflow_dont, /* dont complain_on_overflow */
411 0, /* special_function */
412 "ABSOLUTE", /* name */
413 FALSE, /* partial_inplace */
414 0x00, /* src_mask */
415 0x00, /* dst_mask */
416 FALSE), /* pcrel_offset */
418 /* IMAGE_REL_PPC_ADDR64 0x0001 64-bit address */
419 /* Unused: */
420 HOWTO(IMAGE_REL_PPC_ADDR64, /* type */
421 0, /* rightshift */
422 3, /* size (0 = byte, 1 = short, 2 = long) */
423 64, /* bitsize */
424 FALSE, /* pc_relative */
425 0, /* bitpos */
426 complain_overflow_bitfield, /* complain_on_overflow */
427 0, /* special_function */
428 "ADDR64", /* name */
429 TRUE, /* partial_inplace */
430 MINUS_ONE, /* src_mask */
431 MINUS_ONE, /* dst_mask */
432 FALSE), /* pcrel_offset */
434 /* IMAGE_REL_PPC_ADDR32 0x0002 32-bit address */
435 /* Used: */
436 HOWTO (IMAGE_REL_PPC_ADDR32, /* type */
437 0, /* rightshift */
438 2, /* size (0 = byte, 1 = short, 2 = long) */
439 32, /* bitsize */
440 FALSE, /* pc_relative */
441 0, /* bitpos */
442 complain_overflow_bitfield, /* complain_on_overflow */
443 0, /* special_function */
444 "ADDR32", /* name */
445 TRUE, /* partial_inplace */
446 0xffffffff, /* src_mask */
447 0xffffffff, /* dst_mask */
448 FALSE), /* pcrel_offset */
450 /* IMAGE_REL_PPC_ADDR24 0x0003 26-bit address, shifted left 2 (branch absolute) */
451 /* the LI field is in bit 6 through bit 29 is 24 bits, + 2 for the shift */
452 /* Of course, That's the IBM approved bit numbering, which is not what */
453 /* anyone else uses.... The li field is in bit 2 thru 25 */
454 /* Used: */
455 HOWTO (IMAGE_REL_PPC_ADDR24, /* type */
456 0, /* rightshift */
457 2, /* size (0 = byte, 1 = short, 2 = long) */
458 26, /* bitsize */
459 FALSE, /* pc_relative */
460 0, /* bitpos */
461 complain_overflow_bitfield, /* complain_on_overflow */
462 0, /* special_function */
463 "ADDR24", /* name */
464 TRUE, /* partial_inplace */
465 0x07fffffc, /* src_mask */
466 0x07fffffc, /* dst_mask */
467 FALSE), /* pcrel_offset */
469 /* IMAGE_REL_PPC_ADDR16 0x0004 16-bit address */
470 /* Used: */
471 HOWTO (IMAGE_REL_PPC_ADDR16, /* type */
472 0, /* rightshift */
473 1, /* size (0 = byte, 1 = short, 2 = long) */
474 16, /* bitsize */
475 FALSE, /* pc_relative */
476 0, /* bitpos */
477 complain_overflow_signed, /* complain_on_overflow */
478 0, /* special_function */
479 "ADDR16", /* name */
480 TRUE, /* partial_inplace */
481 0xffff, /* src_mask */
482 0xffff, /* dst_mask */
483 FALSE), /* pcrel_offset */
485 /* IMAGE_REL_PPC_ADDR14 0x0005 */
486 /* 16-bit address, shifted left 2 (load doubleword) */
487 /* FIXME: the mask is likely wrong, and the bit position may be as well */
488 /* Unused: */
489 HOWTO (IMAGE_REL_PPC_ADDR14, /* type */
490 1, /* rightshift */
491 1, /* size (0 = byte, 1 = short, 2 = long) */
492 16, /* bitsize */
493 FALSE, /* pc_relative */
494 0, /* bitpos */
495 complain_overflow_signed, /* complain_on_overflow */
496 0, /* special_function */
497 "ADDR16", /* name */
498 TRUE, /* partial_inplace */
499 0xffff, /* src_mask */
500 0xffff, /* dst_mask */
501 FALSE), /* pcrel_offset */
503 /* IMAGE_REL_PPC_REL24 0x0006 */
504 /* 26-bit PC-relative offset, shifted left 2 (branch relative) */
505 /* Used: */
506 HOWTO (IMAGE_REL_PPC_REL24, /* type */
507 0, /* rightshift */
508 2, /* size (0 = byte, 1 = short, 2 = long) */
509 26, /* bitsize */
510 TRUE, /* pc_relative */
511 0, /* bitpos */
512 complain_overflow_signed, /* complain_on_overflow */
513 0, /* special_function */
514 "REL24", /* name */
515 TRUE, /* partial_inplace */
516 0x3fffffc, /* src_mask */
517 0x3fffffc, /* dst_mask */
518 FALSE), /* pcrel_offset */
520 /* IMAGE_REL_PPC_REL14 0x0007 */
521 /* 16-bit PC-relative offset, shifted left 2 (br cond relative) */
522 /* FIXME: the mask is likely wrong, and the bit position may be as well */
523 /* FIXME: how does it know how far to shift? */
524 /* Unused: */
525 HOWTO (IMAGE_REL_PPC_ADDR14, /* type */
526 1, /* rightshift */
527 1, /* size (0 = byte, 1 = short, 2 = long) */
528 16, /* bitsize */
529 FALSE, /* pc_relative */
530 0, /* bitpos */
531 complain_overflow_signed, /* complain_on_overflow */
532 0, /* special_function */
533 "ADDR16", /* name */
534 TRUE, /* partial_inplace */
535 0xffff, /* src_mask */
536 0xffff, /* dst_mask */
537 TRUE), /* pcrel_offset */
539 /* IMAGE_REL_PPC_TOCREL16 0x0008 */
540 /* 16-bit offset from TOC base */
541 /* Used: */
542 HOWTO (IMAGE_REL_PPC_TOCREL16,/* type */
543 0, /* rightshift */
544 1, /* size (0 = byte, 1 = short, 2 = long) */
545 16, /* bitsize */
546 FALSE, /* pc_relative */
547 0, /* bitpos */
548 complain_overflow_dont, /* complain_on_overflow */
549 ppc_toc16_reloc, /* special_function */
550 "TOCREL16", /* name */
551 FALSE, /* partial_inplace */
552 0xffff, /* src_mask */
553 0xffff, /* dst_mask */
554 FALSE), /* pcrel_offset */
556 /* IMAGE_REL_PPC_TOCREL14 0x0009 */
557 /* 16-bit offset from TOC base, shifted left 2 (load doubleword) */
558 /* Unused: */
559 HOWTO (IMAGE_REL_PPC_TOCREL14,/* type */
560 1, /* rightshift */
561 1, /* size (0 = byte, 1 = short, 2 = long) */
562 16, /* bitsize */
563 FALSE, /* pc_relative */
564 0, /* bitpos */
565 complain_overflow_signed, /* complain_on_overflow */
566 0, /* special_function */
567 "TOCREL14", /* name */
568 FALSE, /* partial_inplace */
569 0xffff, /* src_mask */
570 0xffff, /* dst_mask */
571 FALSE), /* pcrel_offset */
573 /* IMAGE_REL_PPC_ADDR32NB 0x000A */
574 /* 32-bit addr w/ image base */
575 /* Unused: */
576 HOWTO (IMAGE_REL_PPC_ADDR32NB,/* type */
577 0, /* rightshift */
578 2, /* size (0 = byte, 1 = short, 2 = long) */
579 32, /* bitsize */
580 FALSE, /* pc_relative */
581 0, /* bitpos */
582 complain_overflow_signed, /* complain_on_overflow */
583 0, /* special_function */
584 "ADDR32NB", /* name */
585 TRUE, /* partial_inplace */
586 0xffffffff, /* src_mask */
587 0xffffffff, /* dst_mask */
588 FALSE), /* pcrel_offset */
590 /* IMAGE_REL_PPC_SECREL 0x000B */
591 /* va of containing section (as in an image sectionhdr) */
592 /* Unused: */
593 HOWTO (IMAGE_REL_PPC_SECREL,/* type */
594 0, /* rightshift */
595 2, /* size (0 = byte, 1 = short, 2 = long) */
596 32, /* bitsize */
597 FALSE, /* pc_relative */
598 0, /* bitpos */
599 complain_overflow_signed, /* complain_on_overflow */
600 ppc_secrel_reloc, /* special_function */
601 "SECREL", /* name */
602 TRUE, /* partial_inplace */
603 0xffffffff, /* src_mask */
604 0xffffffff, /* dst_mask */
605 TRUE), /* pcrel_offset */
607 /* IMAGE_REL_PPC_SECTION 0x000C */
608 /* sectionheader number */
609 /* Unused: */
610 HOWTO (IMAGE_REL_PPC_SECTION,/* type */
611 0, /* rightshift */
612 2, /* size (0 = byte, 1 = short, 2 = long) */
613 32, /* bitsize */
614 FALSE, /* pc_relative */
615 0, /* bitpos */
616 complain_overflow_signed, /* complain_on_overflow */
617 ppc_section_reloc, /* special_function */
618 "SECTION", /* name */
619 TRUE, /* partial_inplace */
620 0xffffffff, /* src_mask */
621 0xffffffff, /* dst_mask */
622 TRUE), /* pcrel_offset */
624 /* IMAGE_REL_PPC_IFGLUE 0x000D */
625 /* substitute TOC restore instruction iff symbol is glue code */
626 /* Used: */
627 HOWTO (IMAGE_REL_PPC_IFGLUE,/* type */
628 0, /* rightshift */
629 2, /* size (0 = byte, 1 = short, 2 = long) */
630 32, /* bitsize */
631 FALSE, /* pc_relative */
632 0, /* bitpos */
633 complain_overflow_signed, /* complain_on_overflow */
634 0, /* special_function */
635 "IFGLUE", /* name */
636 TRUE, /* partial_inplace */
637 0xffffffff, /* src_mask */
638 0xffffffff, /* dst_mask */
639 FALSE), /* pcrel_offset */
641 /* IMAGE_REL_PPC_IMGLUE 0x000E */
642 /* symbol is glue code; virtual address is TOC restore instruction */
643 /* Unused: */
644 HOWTO (IMAGE_REL_PPC_IMGLUE,/* type */
645 0, /* rightshift */
646 2, /* size (0 = byte, 1 = short, 2 = long) */
647 32, /* bitsize */
648 FALSE, /* pc_relative */
649 0, /* bitpos */
650 complain_overflow_dont, /* complain_on_overflow */
651 ppc_imglue_reloc, /* special_function */
652 "IMGLUE", /* name */
653 FALSE, /* partial_inplace */
654 0xffffffff, /* src_mask */
655 0xffffffff, /* dst_mask */
656 FALSE), /* pcrel_offset */
658 /* IMAGE_REL_PPC_SECREL16 0x000F */
659 /* va of containing section (limited to 16 bits) */
660 /* Unused: */
661 HOWTO (IMAGE_REL_PPC_SECREL16,/* type */
662 0, /* rightshift */
663 1, /* size (0 = byte, 1 = short, 2 = long) */
664 16, /* bitsize */
665 FALSE, /* pc_relative */
666 0, /* bitpos */
667 complain_overflow_signed, /* complain_on_overflow */
668 0, /* special_function */
669 "SECREL16", /* name */
670 TRUE, /* partial_inplace */
671 0xffff, /* src_mask */
672 0xffff, /* dst_mask */
673 TRUE), /* pcrel_offset */
675 /* IMAGE_REL_PPC_REFHI 0x0010 */
676 /* Unused: */
677 HOWTO (IMAGE_REL_PPC_REFHI, /* type */
678 0, /* rightshift */
679 1, /* size (0 = byte, 1 = short, 2 = long) */
680 16, /* bitsize */
681 FALSE, /* pc_relative */
682 0, /* bitpos */
683 complain_overflow_signed, /* complain_on_overflow */
684 ppc_refhi_reloc, /* special_function */
685 "REFHI", /* name */
686 TRUE, /* partial_inplace */
687 0xffffffff, /* src_mask */
688 0xffffffff, /* dst_mask */
689 FALSE), /* pcrel_offset */
691 /* IMAGE_REL_PPC_REFLO 0x0011 */
692 /* Unused: */
693 HOWTO (IMAGE_REL_PPC_REFLO, /* type */
694 0, /* rightshift */
695 1, /* size (0 = byte, 1 = short, 2 = long) */
696 16, /* bitsize */
697 FALSE, /* pc_relative */
698 0, /* bitpos */
699 complain_overflow_signed, /* complain_on_overflow */
700 ppc_refhi_reloc, /* special_function */
701 "REFLO", /* name */
702 TRUE, /* partial_inplace */
703 0xffffffff, /* src_mask */
704 0xffffffff, /* dst_mask */
705 FALSE), /* pcrel_offset */
707 /* IMAGE_REL_PPC_PAIR 0x0012 */
708 /* Unused: */
709 HOWTO (IMAGE_REL_PPC_PAIR, /* type */
710 0, /* rightshift */
711 1, /* size (0 = byte, 1 = short, 2 = long) */
712 16, /* bitsize */
713 FALSE, /* pc_relative */
714 0, /* bitpos */
715 complain_overflow_signed, /* complain_on_overflow */
716 ppc_pair_reloc, /* special_function */
717 "PAIR", /* name */
718 TRUE, /* partial_inplace */
719 0xffffffff, /* src_mask */
720 0xffffffff, /* dst_mask */
721 FALSE), /* pcrel_offset */
723 /* IMAGE_REL_PPC_TOCREL16_DEFN 0x0013 */
724 /* 16-bit offset from TOC base, without causing a definition */
725 /* Used: */
726 HOWTO ( (IMAGE_REL_PPC_TOCREL16 | IMAGE_REL_PPC_TOCDEFN), /* type */
727 0, /* rightshift */
728 1, /* size (0 = byte, 1 = short, 2 = long) */
729 16, /* bitsize */
730 FALSE, /* pc_relative */
731 0, /* bitpos */
732 complain_overflow_dont, /* complain_on_overflow */
733 0, /* special_function */
734 "TOCREL16, TOCDEFN", /* name */
735 FALSE, /* partial_inplace */
736 0xffff, /* src_mask */
737 0xffff, /* dst_mask */
738 FALSE), /* pcrel_offset */
742 /* Some really cheezy macros that can be turned on to test stderr :-) */
744 #ifdef DEBUG_RELOC
745 #define UN_IMPL(x) \
747 static int i; \
748 if (i == 0) \
750 i = 1; \
751 fprintf (stderr,_("Unimplemented Relocation -- %s\n"),x); \
755 #define DUMP_RELOC(n,r) \
757 fprintf (stderr,"%s sym %d, addr %d, addend %d\n", \
758 n, (*(r->sym_ptr_ptr))->name, \
759 r->address, r->addend); \
762 /* Given a reloc name, n, and a pointer to an internal_reloc,
763 dump out interesting information on the contents
765 #define n_name _n._n_name
766 #define n_zeroes _n._n_n._n_zeroes
767 #define n_offset _n._n_n._n_offset */
769 #define DUMP_RELOC2(n,r) \
771 fprintf (stderr,"%s sym %d, r_vaddr %d %s\n", \
772 n, r->r_symndx, r->r_vaddr, \
773 (((r->r_type) & IMAGE_REL_PPC_TOCDEFN) == 0) \
774 ?" ":" TOCDEFN" ); \
777 #else
778 #define UN_IMPL(x)
779 #define DUMP_RELOC(n,r)
780 #define DUMP_RELOC2(n,r)
781 #endif
783 /* TOC construction and management routines. */
785 /* This file is compiled twice, and these variables are defined in one
786 of the compilations. FIXME: This is confusing and weird. Also,
787 BFD should not use global variables. */
788 extern bfd * bfd_of_toc_owner;
789 extern long int global_toc_size;
790 extern long int import_table_size;
791 extern long int first_thunk_address;
792 extern long int thunk_size;
794 enum toc_type
796 default_toc,
797 toc_32,
798 toc_64
801 enum ref_category
803 priv,
804 pub,
805 tocdata
808 struct list_ele
810 struct list_ele *next;
811 bfd_vma addr;
812 enum ref_category cat;
813 int offset;
814 const char *name;
817 extern struct list_ele *head;
818 extern struct list_ele *tail;
820 static void record_toc
821 PARAMS ((asection *, bfd_signed_vma, enum ref_category, const char *));
823 static void
824 record_toc (toc_section, our_toc_offset, cat, name)
825 asection *toc_section;
826 bfd_signed_vma our_toc_offset;
827 enum ref_category cat;
828 const char *name;
830 /* Add this entry to our toc addr-offset-name list. */
831 bfd_size_type amt = sizeof (struct list_ele);
832 struct list_ele *t = (struct list_ele *) bfd_malloc (amt);
834 if (t == NULL)
835 abort ();
836 t->next = 0;
837 t->offset = our_toc_offset;
838 t->name = name;
839 t->cat = cat;
840 t->addr = toc_section->output_offset + our_toc_offset;
842 if (head == 0)
844 head = t;
845 tail = t;
847 else
849 tail->next = t;
850 tail = t;
854 #ifdef COFF_IMAGE_WITH_PE
856 static bfd_boolean ppc_record_toc_entry
857 PARAMS ((bfd *, struct bfd_link_info *, asection *, int, enum toc_type));
858 static void ppc_mark_symbol_as_glue
859 PARAMS ((bfd *, int, struct internal_reloc *));
861 /* Record a toc offset against a symbol. */
862 static bfd_boolean
863 ppc_record_toc_entry(abfd, info, sec, sym, toc_kind)
864 bfd *abfd;
865 struct bfd_link_info *info ATTRIBUTE_UNUSED;
866 asection *sec ATTRIBUTE_UNUSED;
867 int sym;
868 enum toc_type toc_kind ATTRIBUTE_UNUSED;
870 struct ppc_coff_link_hash_entry *h;
871 const char *name;
873 int *local_syms;
875 h = 0;
877 h = (struct ppc_coff_link_hash_entry *) (obj_coff_sym_hashes (abfd)[sym]);
878 if (h != 0)
880 HASH_CHECK(h);
883 if (h == 0)
885 local_syms = obj_coff_local_toc_table(abfd);
887 if (local_syms == 0)
889 unsigned int i;
890 bfd_size_type amt;
892 /* allocate a table */
893 amt = (bfd_size_type) obj_raw_syment_count (abfd) * sizeof (int);
894 local_syms = (int *) bfd_zalloc (abfd, amt);
895 if (local_syms == 0)
896 return FALSE;
897 obj_coff_local_toc_table (abfd) = local_syms;
899 for (i = 0; i < obj_raw_syment_count (abfd); ++i)
901 SET_UNALLOCATED (local_syms[i]);
905 if (IS_UNALLOCATED(local_syms[sym]))
907 local_syms[sym] = global_toc_size;
908 global_toc_size += 4;
910 /* The size must fit in a 16-bit displacement. */
911 if (global_toc_size > 65535)
913 (*_bfd_error_handler) (_("TOC overflow"));
914 bfd_set_error (bfd_error_file_too_big);
915 return FALSE;
919 else
921 name = h->root.root.root.string;
923 /* Check to see if there's a toc slot allocated. If not, do it
924 here. It will be used in relocate_section. */
925 if (IS_UNALLOCATED(h->toc_offset))
927 h->toc_offset = global_toc_size;
928 global_toc_size += 4;
930 /* The size must fit in a 16-bit displacement. */
931 if (global_toc_size >= 65535)
933 (*_bfd_error_handler) (_("TOC overflow"));
934 bfd_set_error (bfd_error_file_too_big);
935 return FALSE;
940 return TRUE;
943 /* Record a toc offset against a symbol. */
944 static void
945 ppc_mark_symbol_as_glue(abfd, sym, rel)
946 bfd *abfd;
947 int sym;
948 struct internal_reloc *rel;
950 struct ppc_coff_link_hash_entry *h;
952 h = (struct ppc_coff_link_hash_entry *) (obj_coff_sym_hashes (abfd)[sym]);
954 HASH_CHECK(h);
956 h->symbol_is_glue = 1;
957 h->glue_insn = bfd_get_32 (abfd, (bfd_byte *) &rel->r_vaddr);
959 return;
962 #endif /* COFF_IMAGE_WITH_PE */
964 /* Return TRUE if this relocation should
965 appear in the output .reloc section. */
967 static bfd_boolean in_reloc_p(abfd, howto)
968 bfd * abfd ATTRIBUTE_UNUSED;
969 reloc_howto_type *howto;
971 return
972 (! howto->pc_relative)
973 && (howto->type != IMAGE_REL_PPC_ADDR32NB)
974 && (howto->type != IMAGE_REL_PPC_TOCREL16)
975 && (howto->type != IMAGE_REL_PPC_IMGLUE)
976 && (howto->type != IMAGE_REL_PPC_IFGLUE)
977 && (howto->type != IMAGE_REL_PPC_SECREL)
978 && (howto->type != IMAGE_REL_PPC_SECTION)
979 && (howto->type != IMAGE_REL_PPC_SECREL16)
980 && (howto->type != IMAGE_REL_PPC_REFHI)
981 && (howto->type != IMAGE_REL_PPC_REFLO)
982 && (howto->type != IMAGE_REL_PPC_PAIR)
983 && (howto->type != IMAGE_REL_PPC_TOCREL16_DEFN) ;
986 /* The reloc processing routine for the optimized COFF linker. */
988 static bfd_boolean
989 coff_ppc_relocate_section (output_bfd, info, input_bfd, input_section,
990 contents, relocs, syms, sections)
991 bfd *output_bfd;
992 struct bfd_link_info *info;
993 bfd *input_bfd;
994 asection *input_section;
995 bfd_byte *contents;
996 struct internal_reloc *relocs;
997 struct internal_syment *syms;
998 asection **sections;
1000 struct internal_reloc *rel;
1001 struct internal_reloc *relend;
1002 bfd_boolean hihalf;
1003 bfd_vma hihalf_val;
1004 asection *toc_section = 0;
1005 bfd_vma relocation;
1006 reloc_howto_type *howto = 0;
1008 /* If we are performing a relocatable link, we don't need to do a
1009 thing. The caller will take care of adjusting the reloc
1010 addresses and symbol indices. */
1011 if (info->relocatable)
1012 return TRUE;
1014 hihalf = FALSE;
1015 hihalf_val = 0;
1017 rel = relocs;
1018 relend = rel + input_section->reloc_count;
1019 for (; rel < relend; rel++)
1021 long symndx;
1022 struct ppc_coff_link_hash_entry *h;
1023 struct internal_syment *sym;
1024 bfd_vma val;
1026 asection *sec;
1027 bfd_reloc_status_type rstat;
1028 bfd_byte *loc;
1030 unsigned short r_type = EXTRACT_TYPE (rel->r_type);
1031 unsigned short r_flags = EXTRACT_FLAGS(rel->r_type);
1033 symndx = rel->r_symndx;
1034 loc = contents + rel->r_vaddr - input_section->vma;
1036 /* FIXME: check bounds on r_type */
1037 howto = ppc_coff_howto_table + r_type;
1039 if (symndx == -1)
1041 h = NULL;
1042 sym = NULL;
1044 else
1046 h = (struct ppc_coff_link_hash_entry *)
1047 (obj_coff_sym_hashes (input_bfd)[symndx]);
1048 if (h != 0)
1050 HASH_CHECK(h);
1053 sym = syms + symndx;
1056 if (r_type == IMAGE_REL_PPC_IMGLUE && h == 0)
1058 /* An IMGLUE reloc must have a name. Something is very wrong. */
1059 abort ();
1062 sec = NULL;
1063 val = 0;
1065 /* FIXME: PAIR unsupported in the following code. */
1066 if (h == NULL)
1068 if (symndx == -1)
1069 sec = bfd_abs_section_ptr;
1070 else
1072 sec = sections[symndx];
1073 val = (sec->output_section->vma
1074 + sec->output_offset
1075 + sym->n_value);
1076 if (! obj_pe (output_bfd))
1077 val -= sec->vma;
1080 else
1082 HASH_CHECK(h);
1084 if (h->root.root.type == bfd_link_hash_defined
1085 || h->root.root.type == bfd_link_hash_defweak)
1087 sec = h->root.root.u.def.section;
1088 val = (h->root.root.u.def.value
1089 + sec->output_section->vma
1090 + sec->output_offset);
1092 else
1094 if (! ((*info->callbacks->undefined_symbol)
1095 (info, h->root.root.root.string, input_bfd, input_section,
1096 rel->r_vaddr - input_section->vma, TRUE)))
1097 return FALSE;
1101 rstat = bfd_reloc_ok;
1103 /* Each case must do its own relocation, setting rstat appropriately. */
1104 switch (r_type)
1106 default:
1107 (*_bfd_error_handler)
1108 (_("%B: unsupported relocation type 0x%02x"), input_bfd, r_type);
1109 bfd_set_error (bfd_error_bad_value);
1110 return FALSE;
1111 case IMAGE_REL_PPC_TOCREL16:
1113 bfd_signed_vma our_toc_offset;
1114 int fixit;
1116 DUMP_RELOC2(howto->name, rel);
1118 if (toc_section == 0)
1120 toc_section = bfd_get_section_by_name (bfd_of_toc_owner,
1121 TOC_SECTION_NAME);
1123 if ( toc_section == NULL )
1125 /* There is no toc section. Something is very wrong. */
1126 abort ();
1130 /* Amazing bit tricks present. As we may have seen earlier, we
1131 use the 1 bit to tell us whether or not a toc offset has been
1132 allocated. Now that they've all been allocated, we will use
1133 the 1 bit to tell us if we've written this particular toc
1134 entry out. */
1135 fixit = FALSE;
1136 if (h == 0)
1138 /* It is a file local symbol. */
1139 int *local_toc_table;
1140 const char *name;
1142 sym = syms + symndx;
1143 name = sym->_n._n_name;
1145 local_toc_table = obj_coff_local_toc_table(input_bfd);
1146 our_toc_offset = local_toc_table[symndx];
1148 if (IS_WRITTEN(our_toc_offset))
1150 /* If it has been written out, it is marked with the
1151 1 bit. Fix up our offset, but do not write it out
1152 again. */
1153 MAKE_ADDR_AGAIN(our_toc_offset);
1155 else
1157 /* Write out the toc entry. */
1158 record_toc (toc_section, our_toc_offset, priv,
1159 strdup (name));
1161 bfd_put_32 (output_bfd, val,
1162 toc_section->contents + our_toc_offset);
1164 MARK_AS_WRITTEN(local_toc_table[symndx]);
1165 fixit = TRUE;
1168 else
1170 const char *name = h->root.root.root.string;
1171 our_toc_offset = h->toc_offset;
1173 if ((r_flags & IMAGE_REL_PPC_TOCDEFN)
1174 == IMAGE_REL_PPC_TOCDEFN )
1176 /* This is unbelievable cheese. Some knowledgable asm
1177 hacker has decided to use r2 as a base for loading
1178 a value. He/She does this by setting the tocdefn bit,
1179 and not supplying a toc definition. The behaviour is
1180 then to use the difference between the value of the
1181 symbol and the actual location of the toc as the toc
1182 index.
1184 In fact, what is usually happening is, because the
1185 Import Address Table is mapped immediately following
1186 the toc, some trippy library code trying for speed on
1187 dll linkage, takes advantage of that and considers
1188 the IAT to be part of the toc, thus saving a load. */
1190 our_toc_offset = val - (toc_section->output_section->vma
1191 + toc_section->output_offset);
1193 /* The size must still fit in a 16-bit displacement. */
1194 if ((bfd_vma) our_toc_offset >= 65535)
1196 (*_bfd_error_handler)
1197 (_("%B: Relocation for %s of %lx exceeds Toc size limit"),
1198 input_bfd, name,
1199 (unsigned long) our_toc_offset);
1200 bfd_set_error (bfd_error_bad_value);
1201 return FALSE;
1204 record_toc (toc_section, our_toc_offset, pub,
1205 strdup (name));
1207 else if (IS_WRITTEN (our_toc_offset))
1209 /* If it has been written out, it is marked with the
1210 1 bit. Fix up our offset, but do not write it out
1211 again. */
1212 MAKE_ADDR_AGAIN(our_toc_offset);
1214 else
1216 record_toc(toc_section, our_toc_offset, pub,
1217 strdup (name));
1219 /* Write out the toc entry. */
1220 bfd_put_32 (output_bfd, val,
1221 toc_section->contents + our_toc_offset);
1223 MARK_AS_WRITTEN(h->toc_offset);
1224 /* The tricky part is that this is the address that
1225 needs a .reloc entry for it. */
1226 fixit = TRUE;
1230 if (fixit && info->base_file)
1232 /* So if this is non pcrelative, and is referenced
1233 to a section or a common symbol, then it needs a reloc. */
1235 /* Relocation to a symbol in a section which
1236 isn't absolute - we output the address here
1237 to a file. */
1238 bfd_vma addr = (toc_section->output_section->vma
1239 + toc_section->output_offset + our_toc_offset);
1241 if (coff_data (output_bfd)->pe)
1242 addr -= pe_data(output_bfd)->pe_opthdr.ImageBase;
1244 fwrite (&addr, 1,4, (FILE *) info->base_file);
1247 /* FIXME: this test is conservative. */
1248 if ((r_flags & IMAGE_REL_PPC_TOCDEFN) != IMAGE_REL_PPC_TOCDEFN
1249 && (bfd_vma) our_toc_offset > toc_section->size)
1251 (*_bfd_error_handler)
1252 (_("%B: Relocation exceeds allocated TOC (%lx)"),
1253 input_bfd, (unsigned long) toc_section->size);
1254 bfd_set_error (bfd_error_bad_value);
1255 return FALSE;
1258 /* Now we know the relocation for this toc reference. */
1259 relocation = our_toc_offset + TOC_LOAD_ADJUSTMENT;
1260 rstat = _bfd_relocate_contents (howto, input_bfd, relocation, loc);
1262 break;
1263 case IMAGE_REL_PPC_IFGLUE:
1265 /* To solve this, we need to know whether or not the symbol
1266 appearing on the call instruction is a glue function or not.
1267 A glue function must announce itself via a IMGLUE reloc, and
1268 the reloc contains the required toc restore instruction. */
1269 bfd_vma x;
1270 const char *my_name;
1272 DUMP_RELOC2 (howto->name, rel);
1274 if (h != 0)
1276 my_name = h->root.root.root.string;
1277 if (h->symbol_is_glue == 1)
1279 x = bfd_get_32 (input_bfd, loc);
1280 bfd_put_32 (input_bfd, (bfd_vma) h->glue_insn, loc);
1284 break;
1285 case IMAGE_REL_PPC_SECREL:
1286 /* Unimplemented: codeview debugging information. */
1287 /* For fast access to the header of the section
1288 containing the item. */
1289 break;
1290 case IMAGE_REL_PPC_SECTION:
1291 /* Unimplemented: codeview debugging information. */
1292 /* Is used to indicate that the value should be relative
1293 to the beginning of the section that contains the
1294 symbol. */
1295 break;
1296 case IMAGE_REL_PPC_ABSOLUTE:
1298 const char *my_name;
1300 if (h == 0)
1301 my_name = (syms+symndx)->_n._n_name;
1302 else
1303 my_name = h->root.root.root.string;
1305 (*_bfd_error_handler)
1306 (_("Warning: unsupported reloc %s <file %B, section %A>\n"
1307 "sym %ld (%s), r_vaddr %ld (%lx)"),
1308 input_bfd, input_section, howto->name,
1309 rel->r_symndx, my_name, (long) rel->r_vaddr,
1310 (unsigned long) rel->r_vaddr);
1312 break;
1313 case IMAGE_REL_PPC_IMGLUE:
1315 /* There is nothing to do now. This reloc was noted in the first
1316 pass over the relocs, and the glue instruction extracted. */
1317 const char *my_name;
1319 if (h->symbol_is_glue == 1)
1320 break;
1321 my_name = h->root.root.root.string;
1323 (*_bfd_error_handler)
1324 (_("%B: Out of order IMGLUE reloc for %s"), input_bfd, my_name);
1325 bfd_set_error (bfd_error_bad_value);
1326 return FALSE;
1329 case IMAGE_REL_PPC_ADDR32NB:
1331 const char *name = 0;
1333 DUMP_RELOC2 (howto->name, rel);
1335 if (CONST_STRNEQ (input_section->name, ".idata$2") && first_thunk_address == 0)
1337 /* Set magic values. */
1338 int idata5offset;
1339 struct coff_link_hash_entry *myh;
1341 myh = coff_link_hash_lookup (coff_hash_table (info),
1342 "__idata5_magic__",
1343 FALSE, FALSE, TRUE);
1344 first_thunk_address = myh->root.u.def.value +
1345 sec->output_section->vma +
1346 sec->output_offset -
1347 pe_data(output_bfd)->pe_opthdr.ImageBase;
1349 idata5offset = myh->root.u.def.value;
1350 myh = coff_link_hash_lookup (coff_hash_table (info),
1351 "__idata6_magic__",
1352 FALSE, FALSE, TRUE);
1354 thunk_size = myh->root.u.def.value - idata5offset;
1355 myh = coff_link_hash_lookup (coff_hash_table (info),
1356 "__idata4_magic__",
1357 FALSE, FALSE, TRUE);
1358 import_table_size = myh->root.u.def.value;
1361 if (h == 0)
1363 /* It is a file local symbol. */
1364 sym = syms + symndx;
1365 name = sym->_n._n_name;
1367 else
1369 char *target = 0;
1371 name = h->root.root.root.string;
1372 if (strcmp (".idata$2", name) == 0)
1373 target = "__idata2_magic__";
1374 else if (strcmp (".idata$4", name) == 0)
1375 target = "__idata4_magic__";
1376 else if (strcmp (".idata$5", name) == 0)
1377 target = "__idata5_magic__";
1379 if (target != 0)
1381 struct coff_link_hash_entry *myh;
1383 myh = coff_link_hash_lookup (coff_hash_table (info),
1384 target,
1385 FALSE, FALSE, TRUE);
1386 if (myh == 0)
1388 /* Missing magic cookies. Something is very wrong. */
1389 abort ();
1392 val = myh->root.u.def.value +
1393 sec->output_section->vma + sec->output_offset;
1394 if (first_thunk_address == 0)
1396 int idata5offset;
1397 myh = coff_link_hash_lookup (coff_hash_table (info),
1398 "__idata5_magic__",
1399 FALSE, FALSE, TRUE);
1400 first_thunk_address = myh->root.u.def.value +
1401 sec->output_section->vma +
1402 sec->output_offset -
1403 pe_data(output_bfd)->pe_opthdr.ImageBase;
1405 idata5offset = myh->root.u.def.value;
1406 myh = coff_link_hash_lookup (coff_hash_table (info),
1407 "__idata6_magic__",
1408 FALSE, FALSE, TRUE);
1410 thunk_size = myh->root.u.def.value - idata5offset;
1411 myh = coff_link_hash_lookup (coff_hash_table (info),
1412 "__idata4_magic__",
1413 FALSE, FALSE, TRUE);
1414 import_table_size = myh->root.u.def.value;
1419 rstat = _bfd_relocate_contents (howto,
1420 input_bfd,
1421 val -
1422 pe_data (output_bfd)->pe_opthdr.ImageBase,
1423 loc);
1425 break;
1427 case IMAGE_REL_PPC_REL24:
1428 DUMP_RELOC2(howto->name, rel);
1429 val -= (input_section->output_section->vma
1430 + input_section->output_offset);
1432 rstat = _bfd_relocate_contents (howto,
1433 input_bfd,
1434 val,
1435 loc);
1436 break;
1437 case IMAGE_REL_PPC_ADDR16:
1438 case IMAGE_REL_PPC_ADDR24:
1439 case IMAGE_REL_PPC_ADDR32:
1440 DUMP_RELOC2(howto->name, rel);
1441 rstat = _bfd_relocate_contents (howto,
1442 input_bfd,
1443 val,
1444 loc);
1445 break;
1448 if (info->base_file)
1450 /* So if this is non pcrelative, and is referenced
1451 to a section or a common symbol, then it needs a reloc. */
1452 if (sym && pe_data(output_bfd)->in_reloc_p (output_bfd, howto))
1454 /* Relocation to a symbol in a section which
1455 isn't absolute - we output the address here
1456 to a file. */
1457 bfd_vma addr = rel->r_vaddr
1458 - input_section->vma
1459 + input_section->output_offset
1460 + input_section->output_section->vma;
1462 if (coff_data (output_bfd)->pe)
1463 addr -= pe_data (output_bfd)->pe_opthdr.ImageBase;
1465 fwrite (&addr, 1,4, (FILE *) info->base_file);
1469 switch (rstat)
1471 default:
1472 abort ();
1473 case bfd_reloc_ok:
1474 break;
1475 case bfd_reloc_overflow:
1477 const char *name;
1478 char buf[SYMNMLEN + 1];
1480 if (symndx == -1)
1481 name = "*ABS*";
1482 else if (h != NULL)
1483 name = NULL;
1484 else if (sym == NULL)
1485 name = "*unknown*";
1486 else if (sym->_n._n_n._n_zeroes == 0
1487 && sym->_n._n_n._n_offset != 0)
1488 name = obj_coff_strings (input_bfd) + sym->_n._n_n._n_offset;
1489 else
1491 strncpy (buf, sym->_n._n_name, SYMNMLEN);
1492 buf[SYMNMLEN] = '\0';
1493 name = buf;
1496 if (! ((*info->callbacks->reloc_overflow)
1497 (info, (h ? &h->root.root : NULL), name, howto->name,
1498 (bfd_vma) 0, input_bfd,
1499 input_section, rel->r_vaddr - input_section->vma)))
1500 return FALSE;
1505 return TRUE;
1508 #ifdef COFF_IMAGE_WITH_PE
1510 /* FIXME: BFD should not use global variables. This file is compiled
1511 twice, and these variables are shared. This is confusing and
1512 weird. */
1514 long int global_toc_size = 4;
1516 bfd* bfd_of_toc_owner = 0;
1518 long int import_table_size;
1519 long int first_thunk_address;
1520 long int thunk_size;
1522 struct list_ele *head;
1523 struct list_ele *tail;
1525 static char *
1526 h1 = N_("\n\t\t\tTOC MAPPING\n\n");
1527 static char *
1528 h2 = N_(" TOC disassembly Comments Name\n");
1529 static char *
1530 h3 = N_(" Offset spelling (if present)\n");
1532 void
1533 dump_toc (vfile)
1534 PTR vfile;
1536 FILE *file = (FILE *) vfile;
1537 struct list_ele *t;
1539 fprintf (file, _(h1));
1540 fprintf (file, _(h2));
1541 fprintf (file, _(h3));
1543 for (t = head; t != 0; t=t->next)
1545 const char *cat = "";
1547 if (t->cat == priv)
1548 cat = _("private ");
1549 else if (t->cat == pub)
1550 cat = _("public ");
1551 else if (t->cat == tocdata)
1552 cat = _("data-in-toc ");
1554 if (t->offset > global_toc_size)
1556 if (t->offset <= global_toc_size + thunk_size)
1557 cat = _("IAT reference ");
1558 else
1560 fprintf (file,
1561 _("**** global_toc_size %ld(%lx), thunk_size %ld(%lx)\n"),
1562 global_toc_size, global_toc_size,
1563 thunk_size, thunk_size);
1564 cat = _("Out of bounds!");
1568 fprintf (file,
1569 " %04lx (%d)", (unsigned long) t->offset, t->offset - 32768);
1570 fprintf (file,
1571 " %s %s\n",
1572 cat, t->name);
1576 fprintf (file, "\n");
1579 bfd_boolean
1580 ppc_allocate_toc_section (info)
1581 struct bfd_link_info *info ATTRIBUTE_UNUSED;
1583 asection *s;
1584 bfd_byte *foo;
1585 bfd_size_type amt;
1586 static char test_char = '1';
1588 if ( global_toc_size == 0 ) /* FIXME: does this get me in trouble? */
1589 return TRUE;
1591 if (bfd_of_toc_owner == 0)
1592 /* No toc owner? Something is very wrong. */
1593 abort ();
1595 s = bfd_get_section_by_name ( bfd_of_toc_owner , TOC_SECTION_NAME);
1596 if (s == NULL)
1597 /* No toc section? Something is very wrong. */
1598 abort ();
1600 amt = global_toc_size;
1601 foo = (bfd_byte *) bfd_alloc (bfd_of_toc_owner, amt);
1602 memset(foo, test_char, (size_t) global_toc_size);
1604 s->size = global_toc_size;
1605 s->contents = foo;
1607 return TRUE;
1610 bfd_boolean
1611 ppc_process_before_allocation (abfd, info)
1612 bfd *abfd;
1613 struct bfd_link_info *info;
1615 asection *sec;
1616 struct internal_reloc *i, *rel;
1618 /* Here we have a bfd that is to be included on the link. We have a hook
1619 to do reloc rummaging, before section sizes are nailed down. */
1620 _bfd_coff_get_external_symbols (abfd);
1622 /* Rummage around all the relocs and map the toc. */
1623 sec = abfd->sections;
1625 if (sec == 0)
1626 return TRUE;
1628 for (; sec != 0; sec = sec->next)
1630 if (sec->reloc_count == 0)
1631 continue;
1633 /* load the relocs */
1634 /* FIXME: there may be a storage leak here */
1635 i=_bfd_coff_read_internal_relocs(abfd,sec,1,0,0,0);
1637 if (i == 0)
1638 abort ();
1640 for (rel = i; rel < i + sec->reloc_count; ++rel)
1642 unsigned short r_type = EXTRACT_TYPE (rel->r_type);
1643 unsigned short r_flags = EXTRACT_FLAGS (rel->r_type);
1644 bfd_boolean ok = TRUE;
1646 DUMP_RELOC2 (ppc_coff_howto_table[r_type].name, rel);
1648 switch(r_type)
1650 case IMAGE_REL_PPC_TOCREL16:
1651 /* If TOCDEFN is on, ignore as someone else has allocated the
1652 toc entry. */
1653 if ((r_flags & IMAGE_REL_PPC_TOCDEFN) != IMAGE_REL_PPC_TOCDEFN)
1654 ok = ppc_record_toc_entry(abfd, info, sec,
1655 rel->r_symndx, default_toc);
1656 if (!ok)
1657 return FALSE;
1658 break;
1659 case IMAGE_REL_PPC_IMGLUE:
1660 ppc_mark_symbol_as_glue (abfd, rel->r_symndx, rel);
1661 break;
1662 default:
1663 break;
1668 return TRUE;
1671 #endif
1673 static bfd_reloc_status_type
1674 ppc_refhi_reloc (abfd, reloc_entry, symbol, data,
1675 input_section, output_bfd, error_message)
1676 bfd *abfd ATTRIBUTE_UNUSED;
1677 arelent *reloc_entry ATTRIBUTE_UNUSED;
1678 asymbol *symbol ATTRIBUTE_UNUSED;
1679 PTR data ATTRIBUTE_UNUSED;
1680 asection *input_section ATTRIBUTE_UNUSED;
1681 bfd *output_bfd;
1682 char **error_message ATTRIBUTE_UNUSED;
1684 UN_IMPL("REFHI");
1685 DUMP_RELOC("REFHI",reloc_entry);
1687 if (output_bfd == (bfd *) NULL)
1688 return bfd_reloc_continue;
1690 return bfd_reloc_undefined;
1693 static bfd_reloc_status_type
1694 ppc_pair_reloc (abfd, reloc_entry, symbol, data,
1695 input_section, output_bfd, error_message)
1696 bfd *abfd ATTRIBUTE_UNUSED;
1697 arelent *reloc_entry ATTRIBUTE_UNUSED;
1698 asymbol *symbol ATTRIBUTE_UNUSED;
1699 PTR data ATTRIBUTE_UNUSED;
1700 asection *input_section ATTRIBUTE_UNUSED;
1701 bfd *output_bfd;
1702 char **error_message ATTRIBUTE_UNUSED;
1704 UN_IMPL("PAIR");
1705 DUMP_RELOC("PAIR",reloc_entry);
1707 if (output_bfd == (bfd *) NULL)
1708 return bfd_reloc_continue;
1710 return bfd_reloc_undefined;
1713 static bfd_reloc_status_type
1714 ppc_toc16_reloc (abfd, reloc_entry, symbol, data,
1715 input_section, output_bfd, error_message)
1716 bfd *abfd ATTRIBUTE_UNUSED;
1717 arelent *reloc_entry ATTRIBUTE_UNUSED;
1718 asymbol *symbol ATTRIBUTE_UNUSED;
1719 PTR data ATTRIBUTE_UNUSED;
1720 asection *input_section ATTRIBUTE_UNUSED;
1721 bfd *output_bfd;
1722 char **error_message ATTRIBUTE_UNUSED;
1724 UN_IMPL ("TOCREL16");
1725 DUMP_RELOC ("TOCREL16",reloc_entry);
1727 if (output_bfd == (bfd *) NULL)
1728 return bfd_reloc_continue;
1730 return bfd_reloc_ok;
1733 static bfd_reloc_status_type
1734 ppc_secrel_reloc (abfd, reloc_entry, symbol, data,
1735 input_section, output_bfd, error_message)
1736 bfd *abfd ATTRIBUTE_UNUSED;
1737 arelent *reloc_entry ATTRIBUTE_UNUSED;
1738 asymbol *symbol ATTRIBUTE_UNUSED;
1739 PTR data ATTRIBUTE_UNUSED;
1740 asection *input_section ATTRIBUTE_UNUSED;
1741 bfd *output_bfd;
1742 char **error_message ATTRIBUTE_UNUSED;
1744 UN_IMPL("SECREL");
1745 DUMP_RELOC("SECREL",reloc_entry);
1747 if (output_bfd == (bfd *) NULL)
1748 return bfd_reloc_continue;
1750 return bfd_reloc_ok;
1753 static bfd_reloc_status_type
1754 ppc_section_reloc (abfd, reloc_entry, symbol, data,
1755 input_section, output_bfd, error_message)
1756 bfd *abfd ATTRIBUTE_UNUSED;
1757 arelent *reloc_entry ATTRIBUTE_UNUSED;
1758 asymbol *symbol ATTRIBUTE_UNUSED;
1759 PTR data ATTRIBUTE_UNUSED;
1760 asection *input_section ATTRIBUTE_UNUSED;
1761 bfd *output_bfd;
1762 char **error_message ATTRIBUTE_UNUSED;
1764 UN_IMPL("SECTION");
1765 DUMP_RELOC("SECTION",reloc_entry);
1767 if (output_bfd == (bfd *) NULL)
1768 return bfd_reloc_continue;
1770 return bfd_reloc_ok;
1773 static bfd_reloc_status_type
1774 ppc_imglue_reloc (abfd, reloc_entry, symbol, data,
1775 input_section, output_bfd, error_message)
1776 bfd *abfd ATTRIBUTE_UNUSED;
1777 arelent *reloc_entry ATTRIBUTE_UNUSED;
1778 asymbol *symbol ATTRIBUTE_UNUSED;
1779 PTR data ATTRIBUTE_UNUSED;
1780 asection *input_section ATTRIBUTE_UNUSED;
1781 bfd *output_bfd;
1782 char **error_message ATTRIBUTE_UNUSED;
1784 UN_IMPL("IMGLUE");
1785 DUMP_RELOC("IMGLUE",reloc_entry);
1787 if (output_bfd == (bfd *) NULL)
1788 return bfd_reloc_continue;
1790 return bfd_reloc_ok;
1793 #define MAX_RELOC_INDEX \
1794 (sizeof (ppc_coff_howto_table) / sizeof (ppc_coff_howto_table[0]) - 1)
1796 /* FIXME: There is a possibility that when we read in a reloc from a file,
1797 that there are some bits encoded in the upper portion of the
1798 type field. Not yet implemented. */
1799 static void ppc_coff_rtype2howto PARAMS ((arelent *, struct internal_reloc *));
1801 static void
1802 ppc_coff_rtype2howto (relent, internal)
1803 arelent *relent;
1804 struct internal_reloc *internal;
1806 /* We can encode one of three things in the type field, aside from the
1807 type:
1808 1. IMAGE_REL_PPC_NEG - indicates the value field is a subtraction
1809 value, rather than an addition value
1810 2. IMAGE_REL_PPC_BRTAKEN, IMAGE_REL_PPC_BRNTAKEN - indicates that
1811 the branch is expected to be taken or not.
1812 3. IMAGE_REL_PPC_TOCDEFN - toc slot definition in the file
1813 For now, we just strip this stuff to find the type, and ignore it other
1814 than that. */
1815 reloc_howto_type *howto;
1816 unsigned short r_type = EXTRACT_TYPE (internal->r_type);
1817 unsigned short r_flags = EXTRACT_FLAGS(internal->r_type);
1818 unsigned short junk = EXTRACT_JUNK (internal->r_type);
1820 /* The masking process only slices off the bottom byte for r_type. */
1821 if ( r_type > MAX_RELOC_INDEX )
1822 abort ();
1824 /* Check for absolute crap. */
1825 if (junk != 0)
1826 abort ();
1828 switch(r_type)
1830 case IMAGE_REL_PPC_ADDR16:
1831 case IMAGE_REL_PPC_REL24:
1832 case IMAGE_REL_PPC_ADDR24:
1833 case IMAGE_REL_PPC_ADDR32:
1834 case IMAGE_REL_PPC_IFGLUE:
1835 case IMAGE_REL_PPC_ADDR32NB:
1836 case IMAGE_REL_PPC_SECTION:
1837 case IMAGE_REL_PPC_SECREL:
1838 DUMP_RELOC2 (ppc_coff_howto_table[r_type].name, internal);
1839 howto = ppc_coff_howto_table + r_type;
1840 break;
1841 case IMAGE_REL_PPC_IMGLUE:
1842 DUMP_RELOC2 (ppc_coff_howto_table[r_type].name, internal);
1843 howto = ppc_coff_howto_table + r_type;
1844 break;
1845 case IMAGE_REL_PPC_TOCREL16:
1846 DUMP_RELOC2 (ppc_coff_howto_table[r_type].name, internal);
1847 if (r_flags & IMAGE_REL_PPC_TOCDEFN)
1848 howto = ppc_coff_howto_table + IMAGE_REL_PPC_TOCREL16_DEFN;
1849 else
1850 howto = ppc_coff_howto_table + IMAGE_REL_PPC_TOCREL16;
1851 break;
1852 default:
1853 fprintf (stderr,
1854 _("Warning: Unsupported reloc %s [%d] used -- it may not work.\n"),
1855 ppc_coff_howto_table[r_type].name,
1856 r_type);
1857 howto = ppc_coff_howto_table + r_type;
1858 break;
1861 relent->howto = howto;
1864 static reloc_howto_type *
1865 coff_ppc_rtype_to_howto (abfd, sec, rel, h, sym, addendp)
1866 bfd *abfd ATTRIBUTE_UNUSED;
1867 asection *sec;
1868 struct internal_reloc *rel;
1869 struct coff_link_hash_entry *h ATTRIBUTE_UNUSED;
1870 struct internal_syment *sym ATTRIBUTE_UNUSED;
1871 bfd_vma *addendp;
1873 reloc_howto_type *howto;
1875 /* We can encode one of three things in the type field, aside from the
1876 type:
1877 1. IMAGE_REL_PPC_NEG - indicates the value field is a subtraction
1878 value, rather than an addition value
1879 2. IMAGE_REL_PPC_BRTAKEN, IMAGE_REL_PPC_BRNTAKEN - indicates that
1880 the branch is expected to be taken or not.
1881 3. IMAGE_REL_PPC_TOCDEFN - toc slot definition in the file
1882 For now, we just strip this stuff to find the type, and ignore it other
1883 than that. */
1885 unsigned short r_type = EXTRACT_TYPE (rel->r_type);
1886 unsigned short r_flags = EXTRACT_FLAGS (rel->r_type);
1887 unsigned short junk = EXTRACT_JUNK (rel->r_type);
1889 /* The masking process only slices off the bottom byte for r_type. */
1890 if (r_type > MAX_RELOC_INDEX)
1891 abort ();
1893 /* Check for absolute crap. */
1894 if (junk != 0)
1895 abort ();
1897 switch(r_type)
1899 case IMAGE_REL_PPC_ADDR32NB:
1900 DUMP_RELOC2(ppc_coff_howto_table[r_type].name, rel);
1901 *addendp -= pe_data(sec->output_section->owner)->pe_opthdr.ImageBase;
1902 howto = ppc_coff_howto_table + r_type;
1903 break;
1904 case IMAGE_REL_PPC_TOCREL16:
1905 DUMP_RELOC2(ppc_coff_howto_table[r_type].name, rel);
1906 if (r_flags & IMAGE_REL_PPC_TOCDEFN)
1907 howto = ppc_coff_howto_table + IMAGE_REL_PPC_TOCREL16_DEFN;
1908 else
1909 howto = ppc_coff_howto_table + IMAGE_REL_PPC_TOCREL16;
1910 break;
1911 case IMAGE_REL_PPC_ADDR16:
1912 case IMAGE_REL_PPC_REL24:
1913 case IMAGE_REL_PPC_ADDR24:
1914 case IMAGE_REL_PPC_ADDR32:
1915 case IMAGE_REL_PPC_IFGLUE:
1916 case IMAGE_REL_PPC_SECTION:
1917 case IMAGE_REL_PPC_SECREL:
1918 DUMP_RELOC2(ppc_coff_howto_table[r_type].name, rel);
1919 howto = ppc_coff_howto_table + r_type;
1920 break;
1921 case IMAGE_REL_PPC_IMGLUE:
1922 DUMP_RELOC2(ppc_coff_howto_table[r_type].name, rel);
1923 howto = ppc_coff_howto_table + r_type;
1924 break;
1925 default:
1926 fprintf (stderr,
1927 _("Warning: Unsupported reloc %s [%d] used -- it may not work.\n"),
1928 ppc_coff_howto_table[r_type].name,
1929 r_type);
1930 howto = ppc_coff_howto_table + r_type;
1931 break;
1934 return howto;
1937 /* A cheesy little macro to make the code a little more readable. */
1938 #define HOW2MAP(bfd_rtype,ppc_rtype) \
1939 case bfd_rtype: return &ppc_coff_howto_table[ppc_rtype]
1941 static reloc_howto_type *ppc_coff_reloc_type_lookup
1942 PARAMS ((bfd *, bfd_reloc_code_real_type));
1944 static reloc_howto_type *
1945 ppc_coff_reloc_type_lookup (abfd, code)
1946 bfd *abfd ATTRIBUTE_UNUSED;
1947 bfd_reloc_code_real_type code;
1949 switch (code)
1951 HOW2MAP(BFD_RELOC_32_GOTOFF, IMAGE_REL_PPC_IMGLUE);
1952 HOW2MAP(BFD_RELOC_16_GOT_PCREL, IMAGE_REL_PPC_IFGLUE);
1953 HOW2MAP(BFD_RELOC_16, IMAGE_REL_PPC_ADDR16);
1954 HOW2MAP(BFD_RELOC_PPC_B26, IMAGE_REL_PPC_REL24);
1955 HOW2MAP(BFD_RELOC_PPC_BA26, IMAGE_REL_PPC_ADDR24);
1956 HOW2MAP(BFD_RELOC_PPC_TOC16, IMAGE_REL_PPC_TOCREL16);
1957 HOW2MAP(BFD_RELOC_16_GOTOFF, IMAGE_REL_PPC_TOCREL16_DEFN);
1958 HOW2MAP(BFD_RELOC_32, IMAGE_REL_PPC_ADDR32);
1959 HOW2MAP(BFD_RELOC_RVA, IMAGE_REL_PPC_ADDR32NB);
1960 default:
1961 return NULL;
1965 #undef HOW2MAP
1967 /* Tailor coffcode.h -- macro heaven. */
1969 #define RTYPE2HOWTO(cache_ptr, dst) ppc_coff_rtype2howto (cache_ptr, dst)
1971 /* We use the special COFF backend linker, with our own special touch. */
1973 #define coff_bfd_reloc_type_lookup ppc_coff_reloc_type_lookup
1974 #define coff_rtype_to_howto coff_ppc_rtype_to_howto
1975 #define coff_relocate_section coff_ppc_relocate_section
1976 #define coff_bfd_final_link ppc_bfd_coff_final_link
1978 #ifndef COFF_IMAGE_WITH_PE
1979 #endif
1981 #define SELECT_RELOC(internal, howto) {internal.r_type=howto->type;}
1983 #define COFF_PAGE_SIZE 0x1000
1985 /* FIXME: This controls some code that used to be in peicode.h and is
1986 now in peigen.c. It will not control the code in peigen.c. If
1987 anybody wants to get this working, you will need to fix that. */
1988 #define POWERPC_LE_PE
1990 #define COFF_SECTION_ALIGNMENT_ENTRIES \
1991 { COFF_SECTION_NAME_EXACT_MATCH (".idata$2"), \
1992 COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 0 }, \
1993 { COFF_SECTION_NAME_EXACT_MATCH (".idata$3"), \
1994 COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 0 }, \
1995 { COFF_SECTION_NAME_EXACT_MATCH (".idata$4"), \
1996 COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 2 }, \
1997 { COFF_SECTION_NAME_EXACT_MATCH (".idata$5"), \
1998 COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 2 }, \
1999 { COFF_SECTION_NAME_EXACT_MATCH (".idata$6"), \
2000 COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 1 }, \
2001 { COFF_SECTION_NAME_EXACT_MATCH (".reloc"), \
2002 COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 1 }
2004 #include "coffcode.h"
2006 #ifndef COFF_IMAGE_WITH_PE
2008 static bfd_boolean ppc_do_last PARAMS ((bfd *));
2009 static bfd *ppc_get_last PARAMS ((void));
2011 static bfd_boolean
2012 ppc_do_last (abfd)
2013 bfd *abfd;
2015 if (abfd == bfd_of_toc_owner)
2016 return TRUE;
2017 else
2018 return FALSE;
2021 static bfd *
2022 ppc_get_last()
2024 return bfd_of_toc_owner;
2027 /* This piece of machinery exists only to guarantee that the bfd that holds
2028 the toc section is written last.
2030 This does depend on bfd_make_section attaching a new section to the
2031 end of the section list for the bfd.
2033 This is otherwise intended to be functionally the same as
2034 cofflink.c:_bfd_coff_final_link(). It is specifically different only
2035 where the POWERPC_LE_PE macro modifies the code. It is left in as a
2036 precise form of comment. krk@cygnus.com */
2038 /* Do the final link step. */
2040 bfd_boolean
2041 ppc_bfd_coff_final_link (abfd, info)
2042 bfd *abfd;
2043 struct bfd_link_info *info;
2045 bfd_size_type symesz;
2046 struct coff_final_link_info finfo;
2047 bfd_boolean debug_merge_allocated;
2048 asection *o;
2049 struct bfd_link_order *p;
2050 bfd_size_type max_sym_count;
2051 bfd_size_type max_lineno_count;
2052 bfd_size_type max_reloc_count;
2053 bfd_size_type max_output_reloc_count;
2054 bfd_size_type max_contents_size;
2055 file_ptr rel_filepos;
2056 unsigned int relsz;
2057 file_ptr line_filepos;
2058 unsigned int linesz;
2059 bfd *sub;
2060 bfd_byte *external_relocs = NULL;
2061 char strbuf[STRING_SIZE_SIZE];
2062 bfd_size_type amt;
2064 symesz = bfd_coff_symesz (abfd);
2066 finfo.info = info;
2067 finfo.output_bfd = abfd;
2068 finfo.strtab = NULL;
2069 finfo.section_info = NULL;
2070 finfo.last_file_index = -1;
2071 finfo.last_bf_index = -1;
2072 finfo.internal_syms = NULL;
2073 finfo.sec_ptrs = NULL;
2074 finfo.sym_indices = NULL;
2075 finfo.outsyms = NULL;
2076 finfo.linenos = NULL;
2077 finfo.contents = NULL;
2078 finfo.external_relocs = NULL;
2079 finfo.internal_relocs = NULL;
2080 debug_merge_allocated = FALSE;
2082 coff_data (abfd)->link_info = info;
2084 finfo.strtab = _bfd_stringtab_init ();
2085 if (finfo.strtab == NULL)
2086 goto error_return;
2088 if (! coff_debug_merge_hash_table_init (&finfo.debug_merge))
2089 goto error_return;
2090 debug_merge_allocated = TRUE;
2092 /* Compute the file positions for all the sections. */
2093 if (! abfd->output_has_begun)
2095 if (! bfd_coff_compute_section_file_positions (abfd))
2096 return FALSE;
2099 /* Count the line numbers and relocation entries required for the
2100 output file. Set the file positions for the relocs. */
2101 rel_filepos = obj_relocbase (abfd);
2102 relsz = bfd_coff_relsz (abfd);
2103 max_contents_size = 0;
2104 max_lineno_count = 0;
2105 max_reloc_count = 0;
2107 for (o = abfd->sections; o != NULL; o = o->next)
2109 o->reloc_count = 0;
2110 o->lineno_count = 0;
2112 for (p = o->map_head.link_order; p != NULL; p = p->next)
2114 if (p->type == bfd_indirect_link_order)
2116 asection *sec;
2118 sec = p->u.indirect.section;
2120 /* Mark all sections which are to be included in the
2121 link. This will normally be every section. We need
2122 to do this so that we can identify any sections which
2123 the linker has decided to not include. */
2124 sec->linker_mark = TRUE;
2126 if (info->strip == strip_none
2127 || info->strip == strip_some)
2128 o->lineno_count += sec->lineno_count;
2130 if (info->relocatable)
2131 o->reloc_count += sec->reloc_count;
2133 if (sec->rawsize > max_contents_size)
2134 max_contents_size = sec->rawsize;
2135 if (sec->size > max_contents_size)
2136 max_contents_size = sec->size;
2137 if (sec->lineno_count > max_lineno_count)
2138 max_lineno_count = sec->lineno_count;
2139 if (sec->reloc_count > max_reloc_count)
2140 max_reloc_count = sec->reloc_count;
2142 else if (info->relocatable
2143 && (p->type == bfd_section_reloc_link_order
2144 || p->type == bfd_symbol_reloc_link_order))
2145 ++o->reloc_count;
2147 if (o->reloc_count == 0)
2148 o->rel_filepos = 0;
2149 else
2151 o->flags |= SEC_RELOC;
2152 o->rel_filepos = rel_filepos;
2153 rel_filepos += o->reloc_count * relsz;
2157 /* If doing a relocatable link, allocate space for the pointers we
2158 need to keep. */
2159 if (info->relocatable)
2161 unsigned int i;
2163 /* We use section_count + 1, rather than section_count, because
2164 the target_index fields are 1 based. */
2165 amt = abfd->section_count + 1;
2166 amt *= sizeof (struct coff_link_section_info);
2167 finfo.section_info = (struct coff_link_section_info *) bfd_malloc (amt);
2169 if (finfo.section_info == NULL)
2170 goto error_return;
2172 for (i = 0; i <= abfd->section_count; i++)
2174 finfo.section_info[i].relocs = NULL;
2175 finfo.section_info[i].rel_hashes = NULL;
2179 /* We now know the size of the relocs, so we can determine the file
2180 positions of the line numbers. */
2181 line_filepos = rel_filepos;
2182 linesz = bfd_coff_linesz (abfd);
2183 max_output_reloc_count = 0;
2185 for (o = abfd->sections; o != NULL; o = o->next)
2187 if (o->lineno_count == 0)
2188 o->line_filepos = 0;
2189 else
2191 o->line_filepos = line_filepos;
2192 line_filepos += o->lineno_count * linesz;
2195 if (o->reloc_count != 0)
2197 /* We don't know the indices of global symbols until we have
2198 written out all the local symbols. For each section in
2199 the output file, we keep an array of pointers to hash
2200 table entries. Each entry in the array corresponds to a
2201 reloc. When we find a reloc against a global symbol, we
2202 set the corresponding entry in this array so that we can
2203 fix up the symbol index after we have written out all the
2204 local symbols.
2206 Because of this problem, we also keep the relocs in
2207 memory until the end of the link. This wastes memory,
2208 but only when doing a relocatable link, which is not the
2209 common case. */
2210 BFD_ASSERT (info->relocatable);
2211 amt = o->reloc_count;
2212 amt *= sizeof (struct internal_reloc);
2213 finfo.section_info[o->target_index].relocs =
2214 (struct internal_reloc *) bfd_malloc (amt);
2215 amt = o->reloc_count;
2216 amt *= sizeof (struct coff_link_hash_entry *);
2217 finfo.section_info[o->target_index].rel_hashes =
2218 (struct coff_link_hash_entry **) bfd_malloc (amt);
2219 if (finfo.section_info[o->target_index].relocs == NULL
2220 || finfo.section_info[o->target_index].rel_hashes == NULL)
2221 goto error_return;
2223 if (o->reloc_count > max_output_reloc_count)
2224 max_output_reloc_count = o->reloc_count;
2227 /* Reset the reloc and lineno counts, so that we can use them to
2228 count the number of entries we have output so far. */
2229 o->reloc_count = 0;
2230 o->lineno_count = 0;
2233 obj_sym_filepos (abfd) = line_filepos;
2235 /* Figure out the largest number of symbols in an input BFD. Take
2236 the opportunity to clear the output_has_begun fields of all the
2237 input BFD's. */
2238 max_sym_count = 0;
2239 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
2241 bfd_size_type sz;
2243 sub->output_has_begun = FALSE;
2244 sz = obj_raw_syment_count (sub);
2245 if (sz > max_sym_count)
2246 max_sym_count = sz;
2249 /* Allocate some buffers used while linking. */
2250 amt = max_sym_count * sizeof (struct internal_syment);
2251 finfo.internal_syms = (struct internal_syment *) bfd_malloc (amt);
2252 amt = max_sym_count * sizeof (asection *);
2253 finfo.sec_ptrs = (asection **) bfd_malloc (amt);
2254 amt = max_sym_count * sizeof (long);
2255 finfo.sym_indices = (long *) bfd_malloc (amt);
2256 amt = (max_sym_count + 1) * symesz;
2257 finfo.outsyms = (bfd_byte *) bfd_malloc (amt);
2258 amt = max_lineno_count * bfd_coff_linesz (abfd);
2259 finfo.linenos = (bfd_byte *) bfd_malloc (amt);
2260 finfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
2261 finfo.external_relocs = (bfd_byte *) bfd_malloc (max_reloc_count * relsz);
2262 if (! info->relocatable)
2264 amt = max_reloc_count * sizeof (struct internal_reloc);
2265 finfo.internal_relocs = (struct internal_reloc *) bfd_malloc (amt);
2267 if ((finfo.internal_syms == NULL && max_sym_count > 0)
2268 || (finfo.sec_ptrs == NULL && max_sym_count > 0)
2269 || (finfo.sym_indices == NULL && max_sym_count > 0)
2270 || finfo.outsyms == NULL
2271 || (finfo.linenos == NULL && max_lineno_count > 0)
2272 || (finfo.contents == NULL && max_contents_size > 0)
2273 || (finfo.external_relocs == NULL && max_reloc_count > 0)
2274 || (! info->relocatable
2275 && finfo.internal_relocs == NULL
2276 && max_reloc_count > 0))
2277 goto error_return;
2279 /* We now know the position of everything in the file, except that
2280 we don't know the size of the symbol table and therefore we don't
2281 know where the string table starts. We just build the string
2282 table in memory as we go along. We process all the relocations
2283 for a single input file at once. */
2284 obj_raw_syment_count (abfd) = 0;
2286 if (coff_backend_info (abfd)->_bfd_coff_start_final_link)
2288 if (! bfd_coff_start_final_link (abfd, info))
2289 goto error_return;
2292 for (o = abfd->sections; o != NULL; o = o->next)
2294 for (p = o->map_head.link_order; p != NULL; p = p->next)
2296 if (p->type == bfd_indirect_link_order
2297 && (bfd_get_flavour (p->u.indirect.section->owner)
2298 == bfd_target_coff_flavour))
2300 sub = p->u.indirect.section->owner;
2301 #ifdef POWERPC_LE_PE
2302 if (! sub->output_has_begun && !ppc_do_last(sub))
2303 #else
2304 if (! sub->output_has_begun)
2305 #endif
2307 if (! _bfd_coff_link_input_bfd (&finfo, sub))
2308 goto error_return;
2309 sub->output_has_begun = TRUE;
2312 else if (p->type == bfd_section_reloc_link_order
2313 || p->type == bfd_symbol_reloc_link_order)
2315 if (! _bfd_coff_reloc_link_order (abfd, &finfo, o, p))
2316 goto error_return;
2318 else
2320 if (! _bfd_default_link_order (abfd, info, o, p))
2321 goto error_return;
2326 #ifdef POWERPC_LE_PE
2328 bfd* last_one = ppc_get_last();
2329 if (last_one)
2331 if (! _bfd_coff_link_input_bfd (&finfo, last_one))
2332 goto error_return;
2334 last_one->output_has_begun = TRUE;
2336 #endif
2338 /* Free up the buffers used by _bfd_coff_link_input_bfd. */
2339 coff_debug_merge_hash_table_free (&finfo.debug_merge);
2340 debug_merge_allocated = FALSE;
2342 if (finfo.internal_syms != NULL)
2344 free (finfo.internal_syms);
2345 finfo.internal_syms = NULL;
2347 if (finfo.sec_ptrs != NULL)
2349 free (finfo.sec_ptrs);
2350 finfo.sec_ptrs = NULL;
2352 if (finfo.sym_indices != NULL)
2354 free (finfo.sym_indices);
2355 finfo.sym_indices = NULL;
2357 if (finfo.linenos != NULL)
2359 free (finfo.linenos);
2360 finfo.linenos = NULL;
2362 if (finfo.contents != NULL)
2364 free (finfo.contents);
2365 finfo.contents = NULL;
2367 if (finfo.external_relocs != NULL)
2369 free (finfo.external_relocs);
2370 finfo.external_relocs = NULL;
2372 if (finfo.internal_relocs != NULL)
2374 free (finfo.internal_relocs);
2375 finfo.internal_relocs = NULL;
2378 /* The value of the last C_FILE symbol is supposed to be the symbol
2379 index of the first external symbol. Write it out again if
2380 necessary. */
2381 if (finfo.last_file_index != -1
2382 && (unsigned int) finfo.last_file.n_value != obj_raw_syment_count (abfd))
2384 file_ptr pos;
2386 finfo.last_file.n_value = obj_raw_syment_count (abfd);
2387 bfd_coff_swap_sym_out (abfd, (PTR) &finfo.last_file,
2388 (PTR) finfo.outsyms);
2389 pos = obj_sym_filepos (abfd) + finfo.last_file_index * symesz;
2390 if (bfd_seek (abfd, pos, SEEK_SET) != 0
2391 || bfd_bwrite (finfo.outsyms, symesz, abfd) != symesz)
2392 return FALSE;
2395 /* Write out the global symbols. */
2396 finfo.failed = FALSE;
2397 coff_link_hash_traverse (coff_hash_table (info), _bfd_coff_write_global_sym,
2398 (PTR) &finfo);
2399 if (finfo.failed)
2400 goto error_return;
2402 /* The outsyms buffer is used by _bfd_coff_write_global_sym. */
2403 if (finfo.outsyms != NULL)
2405 free (finfo.outsyms);
2406 finfo.outsyms = NULL;
2409 if (info->relocatable)
2411 /* Now that we have written out all the global symbols, we know
2412 the symbol indices to use for relocs against them, and we can
2413 finally write out the relocs. */
2414 amt = max_output_reloc_count * relsz;
2415 external_relocs = (bfd_byte *) bfd_malloc (amt);
2416 if (external_relocs == NULL)
2417 goto error_return;
2419 for (o = abfd->sections; o != NULL; o = o->next)
2421 struct internal_reloc *irel;
2422 struct internal_reloc *irelend;
2423 struct coff_link_hash_entry **rel_hash;
2424 bfd_byte *erel;
2426 if (o->reloc_count == 0)
2427 continue;
2429 irel = finfo.section_info[o->target_index].relocs;
2430 irelend = irel + o->reloc_count;
2431 rel_hash = finfo.section_info[o->target_index].rel_hashes;
2432 erel = external_relocs;
2433 for (; irel < irelend; irel++, rel_hash++, erel += relsz)
2435 if (*rel_hash != NULL)
2437 BFD_ASSERT ((*rel_hash)->indx >= 0);
2438 irel->r_symndx = (*rel_hash)->indx;
2440 bfd_coff_swap_reloc_out (abfd, (PTR) irel, (PTR) erel);
2443 amt = relsz * o->reloc_count;
2444 if (bfd_seek (abfd, o->rel_filepos, SEEK_SET) != 0
2445 || bfd_bwrite ((PTR) external_relocs, amt, abfd) != amt)
2446 goto error_return;
2449 free (external_relocs);
2450 external_relocs = NULL;
2453 /* Free up the section information. */
2454 if (finfo.section_info != NULL)
2456 unsigned int i;
2458 for (i = 0; i < abfd->section_count; i++)
2460 if (finfo.section_info[i].relocs != NULL)
2461 free (finfo.section_info[i].relocs);
2462 if (finfo.section_info[i].rel_hashes != NULL)
2463 free (finfo.section_info[i].rel_hashes);
2465 free (finfo.section_info);
2466 finfo.section_info = NULL;
2469 /* If we have optimized stabs strings, output them. */
2470 if (coff_hash_table (info)->stab_info.stabstr != NULL)
2472 if (! _bfd_write_stab_strings (abfd, &coff_hash_table (info)->stab_info))
2473 return FALSE;
2476 /* Write out the string table. */
2477 if (obj_raw_syment_count (abfd) != 0)
2479 file_ptr pos;
2481 pos = obj_sym_filepos (abfd) + obj_raw_syment_count (abfd) * symesz;
2482 if (bfd_seek (abfd, pos, SEEK_SET) != 0)
2483 return FALSE;
2485 #if STRING_SIZE_SIZE == 4
2486 H_PUT_32 (abfd,
2487 _bfd_stringtab_size (finfo.strtab) + STRING_SIZE_SIZE,
2488 strbuf);
2489 #else
2490 #error Change H_PUT_32 above
2491 #endif
2493 if (bfd_bwrite (strbuf, (bfd_size_type) STRING_SIZE_SIZE, abfd)
2494 != STRING_SIZE_SIZE)
2495 return FALSE;
2497 if (! _bfd_stringtab_emit (abfd, finfo.strtab))
2498 return FALSE;
2501 _bfd_stringtab_free (finfo.strtab);
2503 /* Setting bfd_get_symcount to 0 will cause write_object_contents to
2504 not try to write out the symbols. */
2505 bfd_get_symcount (abfd) = 0;
2507 return TRUE;
2509 error_return:
2510 if (debug_merge_allocated)
2511 coff_debug_merge_hash_table_free (&finfo.debug_merge);
2512 if (finfo.strtab != NULL)
2513 _bfd_stringtab_free (finfo.strtab);
2514 if (finfo.section_info != NULL)
2516 unsigned int i;
2518 for (i = 0; i < abfd->section_count; i++)
2520 if (finfo.section_info[i].relocs != NULL)
2521 free (finfo.section_info[i].relocs);
2522 if (finfo.section_info[i].rel_hashes != NULL)
2523 free (finfo.section_info[i].rel_hashes);
2525 free (finfo.section_info);
2527 if (finfo.internal_syms != NULL)
2528 free (finfo.internal_syms);
2529 if (finfo.sec_ptrs != NULL)
2530 free (finfo.sec_ptrs);
2531 if (finfo.sym_indices != NULL)
2532 free (finfo.sym_indices);
2533 if (finfo.outsyms != NULL)
2534 free (finfo.outsyms);
2535 if (finfo.linenos != NULL)
2536 free (finfo.linenos);
2537 if (finfo.contents != NULL)
2538 free (finfo.contents);
2539 if (finfo.external_relocs != NULL)
2540 free (finfo.external_relocs);
2541 if (finfo.internal_relocs != NULL)
2542 free (finfo.internal_relocs);
2543 if (external_relocs != NULL)
2544 free (external_relocs);
2545 return FALSE;
2547 #endif
2549 /* Forward declaration for use by alternative_target field. */
2550 #ifdef TARGET_BIG_SYM
2551 extern const bfd_target TARGET_BIG_SYM;
2552 #endif
2554 /* The transfer vectors that lead the outside world to all of the above. */
2556 #ifdef TARGET_LITTLE_SYM
2557 const bfd_target TARGET_LITTLE_SYM =
2559 TARGET_LITTLE_NAME, /* name or coff-arm-little */
2560 bfd_target_coff_flavour,
2561 BFD_ENDIAN_LITTLE, /* data byte order is little */
2562 BFD_ENDIAN_LITTLE, /* header byte order is little */
2564 (HAS_RELOC | EXEC_P | /* FIXME: object flags */
2565 HAS_LINENO | HAS_DEBUG |
2566 HAS_SYMS | HAS_LOCALS | WP_TEXT | D_PAGED),
2568 #ifndef COFF_WITH_PE
2569 (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* section flags */
2570 #else
2571 (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC /* section flags */
2572 | SEC_LINK_ONCE | SEC_LINK_DUPLICATES),
2573 #endif
2575 0, /* leading char */
2576 '/', /* ar_pad_char */
2577 15, /* ar_max_namelen??? FIXMEmgo */
2579 bfd_getl64, bfd_getl_signed_64, bfd_putl64,
2580 bfd_getl32, bfd_getl_signed_32, bfd_putl32,
2581 bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* data */
2583 bfd_getl64, bfd_getl_signed_64, bfd_putl64,
2584 bfd_getl32, bfd_getl_signed_32, bfd_putl32,
2585 bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* hdrs */
2587 {_bfd_dummy_target, coff_object_p, /* bfd_check_format */
2588 bfd_generic_archive_p, /* _bfd_dummy_target */ coff_object_p },
2589 {bfd_false, coff_mkobject, _bfd_generic_mkarchive, /* bfd_set_format */
2590 bfd_false},
2591 {bfd_false, coff_write_object_contents, /* bfd_write_contents */
2592 _bfd_write_archive_contents, bfd_false},
2594 BFD_JUMP_TABLE_GENERIC (coff),
2595 BFD_JUMP_TABLE_COPY (coff),
2596 BFD_JUMP_TABLE_CORE (_bfd_nocore),
2597 BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_coff),
2598 BFD_JUMP_TABLE_SYMBOLS (coff),
2599 BFD_JUMP_TABLE_RELOCS (coff),
2600 BFD_JUMP_TABLE_WRITE (coff),
2601 BFD_JUMP_TABLE_LINK (coff),
2602 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
2604 /* Alternative_target. */
2605 #ifdef TARGET_BIG_SYM
2606 & TARGET_BIG_SYM,
2607 #else
2608 NULL,
2609 #endif
2611 COFF_SWAP_TABLE
2613 #endif
2615 #ifdef TARGET_BIG_SYM
2616 const bfd_target TARGET_BIG_SYM =
2618 TARGET_BIG_NAME,
2619 bfd_target_coff_flavour,
2620 BFD_ENDIAN_BIG, /* data byte order is big */
2621 BFD_ENDIAN_BIG, /* header byte order is big */
2623 (HAS_RELOC | EXEC_P | /* FIXME: object flags */
2624 HAS_LINENO | HAS_DEBUG |
2625 HAS_SYMS | HAS_LOCALS | WP_TEXT | D_PAGED),
2627 #ifndef COFF_WITH_PE
2628 (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* section flags */
2629 #else
2630 (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC /* section flags */
2631 | SEC_LINK_ONCE | SEC_LINK_DUPLICATES),
2632 #endif
2634 0, /* leading char */
2635 '/', /* ar_pad_char */
2636 15, /* ar_max_namelen??? FIXMEmgo */
2638 bfd_getb64, bfd_getb_signed_64, bfd_putb64,
2639 bfd_getb32, bfd_getb_signed_32, bfd_putb32,
2640 bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* data */
2642 bfd_getb64, bfd_getb_signed_64, bfd_putb64,
2643 bfd_getb32, bfd_getb_signed_32, bfd_putb32,
2644 bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* hdrs */
2646 {_bfd_dummy_target, coff_object_p, /* bfd_check_format */
2647 bfd_generic_archive_p, /* _bfd_dummy_target */ coff_object_p },
2648 {bfd_false, coff_mkobject, _bfd_generic_mkarchive, /* bfd_set_format */
2649 bfd_false},
2650 {bfd_false, coff_write_object_contents, /* bfd_write_contents */
2651 _bfd_write_archive_contents, bfd_false},
2653 BFD_JUMP_TABLE_GENERIC (coff),
2654 BFD_JUMP_TABLE_COPY (coff),
2655 BFD_JUMP_TABLE_CORE (_bfd_nocore),
2656 BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_coff),
2657 BFD_JUMP_TABLE_SYMBOLS (coff),
2658 BFD_JUMP_TABLE_RELOCS (coff),
2659 BFD_JUMP_TABLE_WRITE (coff),
2660 BFD_JUMP_TABLE_LINK (coff),
2661 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
2663 /* Alternative_target. */
2664 #ifdef TARGET_LITTLE_SYM
2665 & TARGET_LITTLE_SYM,
2666 #else
2667 NULL,
2668 #endif
2670 COFF_SWAP_TABLE
2673 #endif