Home | History | Annotate | Download | only in bfd
      1 /* BFD back-end for PowerPC Microsoft Portable Executable files.
      2    Copyright (C) 1990-2014 Free Software Foundation, Inc.
      3 
      4    Original version pieced together by Kim Knuttila (krk (at) cygnus.com)
      5 
      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.
      9 
     10    This file is part of BFD, the Binary File Descriptor library.
     11 
     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.
     16 
     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.
     21 
     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.  */
     26 
     27 /* Current State:
     28    - objdump works
     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.  */
     33 
     34 #include "sysdep.h"
     35 #include "bfd.h"
     36 #include "libbfd.h"
     37 
     38 #include "coff/powerpc.h"
     39 #include "coff/internal.h"
     40 
     41 #include "coff/pe.h"
     42 
     43 #ifdef BADMAG
     44 #undef BADMAG
     45 #endif
     46 
     47 #define BADMAG(x) PPCBADMAG(x)
     48 
     49 #include "libcoff.h"
     50 
     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 *);
     55 
     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.  */
     65 
     66 #define SET_UNALLOCATED(x)  ((x) = 1)
     67 #define IS_UNALLOCATED(x)   ((x) == 1)
     68 
     69 #define IS_WRITTEN(x)       ((x) & 1)
     70 #define MARK_AS_WRITTEN(x)  ((x) |= 1)
     71 #define MAKE_ADDR_AGAIN(x)  ((x) &= ~1)
     72 
     73 /* Turn on this check if you suspect something amiss in the hash tables.  */
     74 #ifdef DEBUG_HASH
     75 
     76 /* Need a 7 char string for an eye catcher.  */
     77 #define EYE "krkjunk"
     78 
     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) \
     83   { \
     84     fprintf (stderr,\
     85     _("File %s, line %d, Hash check failure, bad eye %8s\n"), \
     86     __FILE__, __LINE__, addr->eye_catcher); \
     87     abort (); \
     88  }
     89 
     90 #else
     91 
     92 #define HASH_CHECK_DCL
     93 #define HASH_CHECK_INIT(ret)
     94 #define HASH_CHECK(addr)
     95 
     96 #endif
     97 
     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.  */
    100 
    101 /* PE linker hash table entries.  */
    102 
    103 struct ppc_coff_link_hash_entry
    104 {
    105   struct coff_link_hash_entry root; /* First entry, as required.  */
    106 
    107   /* As we wonder around the relocs, we'll keep the assigned toc_offset
    108      here.  */
    109   bfd_vma toc_offset;               /* Our addition, as required.  */
    110   int symbol_is_glue;
    111   unsigned long int glue_insn;
    112 
    113   HASH_CHECK_DCL
    114 };
    115 
    116 /* PE linker hash table.  */
    117 
    118 struct ppc_coff_link_hash_table
    119 {
    120   struct coff_link_hash_table root; /* First entry, as required.  */
    121 };
    122 
    123 /* Routine to create an entry in the link hash table.  */
    124 
    125 static struct bfd_hash_entry *
    126 ppc_coff_link_hash_newfunc (struct bfd_hash_entry * entry,
    127 			    struct bfd_hash_table * table,
    128 			    const char * string)
    129 {
    130   struct ppc_coff_link_hash_entry *ret =
    131     (struct ppc_coff_link_hash_entry *) entry;
    132 
    133   /* Allocate the structure if it has not already been allocated by a
    134      subclass.  */
    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));
    139 
    140   if (ret == (struct ppc_coff_link_hash_entry *) NULL)
    141     return NULL;
    142 
    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,
    146 				      table, string));
    147 
    148   if (ret)
    149     {
    150       /* Initialize the local fields.  */
    151       SET_UNALLOCATED (ret->toc_offset);
    152       ret->symbol_is_glue = 0;
    153       ret->glue_insn = 0;
    154 
    155       HASH_CHECK_INIT (ret);
    156     }
    157 
    158   return (struct bfd_hash_entry *) ret;
    159 }
    160 
    161 /* Initialize a PE linker hash table.  */
    162 
    163 static bfd_boolean
    164 ppc_coff_link_hash_table_init (struct ppc_coff_link_hash_table *table,
    165 			       bfd *abfd,
    166 			       struct bfd_hash_entry *(*newfunc)
    167 			         (struct bfd_hash_entry *,
    168 				  struct bfd_hash_table *,
    169 				  const char *),
    170 			       unsigned int entsize)
    171 {
    172   return _bfd_coff_link_hash_table_init (&table->root, abfd, newfunc, entsize);
    173 }
    174 
    175 /* Create a PE linker hash table.  */
    176 
    177 static struct bfd_link_hash_table *
    178 ppc_coff_link_hash_table_create (bfd *abfd)
    179 {
    180   struct ppc_coff_link_hash_table *ret;
    181   bfd_size_type amt = sizeof (struct ppc_coff_link_hash_table);
    182 
    183   ret = (struct ppc_coff_link_hash_table *) bfd_malloc (amt);
    184   if (ret == NULL)
    185     return NULL;
    186   if (!ppc_coff_link_hash_table_init (ret, abfd,
    187 				      ppc_coff_link_hash_newfunc,
    188 				      sizeof (struct ppc_coff_link_hash_entry)))
    189     {
    190       free (ret);
    191       return (struct bfd_link_hash_table *) NULL;
    192     }
    193   return &ret->root.root;
    194 }
    195 
    196 /* Now, tailor coffcode.h to use our hash stuff.  */
    197 
    198 #define coff_bfd_link_hash_table_create ppc_coff_link_hash_table_create
    199 
    200 /* The nt loader points the toc register to &toc + 32768, in order to
    202    use the complete range of a 16-bit displacement. We have to adjust
    203    for this when we fix up loads displaced off the toc reg.  */
    204 #define TOC_LOAD_ADJUSTMENT (-32768)
    205 #define TOC_SECTION_NAME ".private.toc"
    206 
    207 /* The main body of code is in coffcode.h.  */
    208 
    209 #define COFF_DEFAULT_SECTION_ALIGNMENT_POWER (3)
    210 
    211 /* In case we're on a 32-bit machine, construct a 64-bit "-1" value
    212    from smaller values.  Start with zero, widen, *then* decrement.  */
    213 #define MINUS_ONE	(((bfd_vma)0) - 1)
    214 
    215 /* These should definitely go in a header file somewhere...  */
    216 
    217 /* NOP */
    218 #define IMAGE_REL_PPC_ABSOLUTE          0x0000
    219 
    220 /* 64-bit address */
    221 #define IMAGE_REL_PPC_ADDR64            0x0001
    222 
    223 /* 32-bit address */
    224 #define IMAGE_REL_PPC_ADDR32            0x0002
    225 
    226 /* 26-bit address, shifted left 2 (branch absolute) */
    227 #define IMAGE_REL_PPC_ADDR24            0x0003
    228 
    229 /* 16-bit address */
    230 #define IMAGE_REL_PPC_ADDR16            0x0004
    231 
    232 /* 16-bit address, shifted left 2 (load doubleword) */
    233 #define IMAGE_REL_PPC_ADDR14            0x0005
    234 
    235 /* 26-bit PC-relative offset, shifted left 2 (branch relative) */
    236 #define IMAGE_REL_PPC_REL24             0x0006
    237 
    238 /* 16-bit PC-relative offset, shifted left 2 (br cond relative) */
    239 #define IMAGE_REL_PPC_REL14             0x0007
    240 
    241 /* 16-bit offset from TOC base */
    242 #define IMAGE_REL_PPC_TOCREL16          0x0008
    243 
    244 /* 16-bit offset from TOC base, shifted left 2 (load doubleword) */
    245 #define IMAGE_REL_PPC_TOCREL14          0x0009
    246 
    247 /* 32-bit addr w/o image base */
    248 #define IMAGE_REL_PPC_ADDR32NB          0x000A
    249 
    250 /* va of containing section (as in an image sectionhdr) */
    251 #define IMAGE_REL_PPC_SECREL            0x000B
    252 
    253 /* sectionheader number */
    254 #define IMAGE_REL_PPC_SECTION           0x000C
    255 
    256 /* substitute TOC restore instruction iff symbol is glue code */
    257 #define IMAGE_REL_PPC_IFGLUE            0x000D
    258 
    259 /* symbol is glue code; virtual address is TOC restore instruction */
    260 #define IMAGE_REL_PPC_IMGLUE            0x000E
    261 
    262 /* va of containing section (limited to 16 bits) */
    263 #define IMAGE_REL_PPC_SECREL16          0x000F
    264 
    265 /* Stuff to handle immediate data when the number of bits in the
    266    data is greater than the number of bits in the immediate field
    267    We need to do (usually) 32 bit arithmetic on 16 bit chunks.  */
    268 #define IMAGE_REL_PPC_REFHI             0x0010
    269 #define IMAGE_REL_PPC_REFLO             0x0011
    270 #define IMAGE_REL_PPC_PAIR              0x0012
    271 
    272 /* This is essentially the same as tocrel16, with TOCDEFN assumed.  */
    273 #define IMAGE_REL_PPC_TOCREL16_DEFN     0x0013
    274 
    275 /* Flag bits in IMAGE_RELOCATION.TYPE.  */
    276 
    277 /* Subtract reloc value rather than adding it.  */
    278 #define IMAGE_REL_PPC_NEG               0x0100
    279 
    280 /* Fix branch prediction bit to predict branch taken.  */
    281 #define IMAGE_REL_PPC_BRTAKEN           0x0200
    282 
    283 /* Fix branch prediction bit to predict branch not taken.  */
    284 #define IMAGE_REL_PPC_BRNTAKEN          0x0400
    285 
    286 /* TOC slot defined in file (or, data in toc).  */
    287 #define IMAGE_REL_PPC_TOCDEFN           0x0800
    288 
    289 /* Masks to isolate above values in IMAGE_RELOCATION.Type.  */
    290 #define IMAGE_REL_PPC_TYPEMASK          0x00FF
    291 #define IMAGE_REL_PPC_FLAGMASK          0x0F00
    292 
    293 #define EXTRACT_TYPE(x)                 ((x) & IMAGE_REL_PPC_TYPEMASK)
    294 #define EXTRACT_FLAGS(x) ((x) & IMAGE_REL_PPC_FLAGMASK)
    295 #define EXTRACT_JUNK(x)  \
    296            ((x) & ~(IMAGE_REL_PPC_TYPEMASK | IMAGE_REL_PPC_FLAGMASK))
    297 
    298 /* Static helper functions to make relocation work.  */
    300 /* (Work In Progress) */
    301 
    302 static bfd_reloc_status_type ppc_refhi_reloc
    303   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
    304 static bfd_reloc_status_type ppc_pair_reloc
    305   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
    306 static bfd_reloc_status_type ppc_toc16_reloc
    307   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
    308 static bfd_reloc_status_type ppc_section_reloc
    309   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
    310 static bfd_reloc_status_type ppc_secrel_reloc
    311   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
    312 static bfd_reloc_status_type ppc_imglue_reloc
    313   (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
    314 
    315 /* FIXME: It'll take a while to get through all of these. I only need a few to
    316    get us started, so those I'll make sure work. Those marked FIXME are either
    317    completely unverified or have a specific unknown marked in the comment.  */
    318 
    319 /* Relocation entries for Windows/NT on PowerPC.
    320 
    321    From the document "" we find the following listed as used relocs:
    322 
    323      ABSOLUTE       : The noop
    324      ADDR[64|32|16] : fields that hold addresses in data fields or the
    325                       16 bit displacement field on a load/store.
    326      ADDR[24|14]    : fields that hold addresses in branch and cond
    327                       branches. These represent [26|16] bit addresses.
    328                       The low order 2 bits are preserved.
    329      REL[24|14]     : branches relative to the Instruction Address
    330                       register. These represent [26|16] bit addresses,
    331                       as before. The instruction field will be zero, and
    332                       the address of the SYM will be inserted at link time.
    333      TOCREL16       : 16 bit displacement field referring to a slot in
    334                       toc.
    335      TOCREL14       : 16 bit displacement field, similar to REL14 or ADDR14.
    336      ADDR32NB       : 32 bit address relative to the virtual origin.
    337                       (On the alpha, this is always a linker generated thunk)
    338                       (i.e. 32bit addr relative to the image base)
    339      SECREL         : The value is relative to the start of the section
    340                       containing the symbol.
    341      SECTION        : access to the header containing the item. Supports the
    342                       codeview debugger.
    343 
    344    In particular, note that the document does not indicate that the
    345    relocations listed in the header file are used.  */
    346 
    347 
    348 static reloc_howto_type ppc_coff_howto_table[] =
    349 {
    350   /* IMAGE_REL_PPC_ABSOLUTE 0x0000   NOP */
    351   /* Unused: */
    352   HOWTO (IMAGE_REL_PPC_ABSOLUTE, /* type */
    353 	 0,	                 /* rightshift */
    354 	 0,	                 /* size (0 = byte, 1 = short, 2 = long) */
    355 	 0,	                 /* bitsize */
    356 	 FALSE,	                 /* pc_relative */
    357 	 0,	                 /* bitpos */
    358 	 complain_overflow_dont, /* dont complain_on_overflow */
    359 	 0,		         /* special_function */
    360 	 "ABSOLUTE",             /* name */
    361 	 FALSE,	                 /* partial_inplace */
    362 	 0x00,	 	         /* src_mask */
    363 	 0x00,        		 /* dst_mask */
    364 	 FALSE),                 /* pcrel_offset */
    365 
    366   /* IMAGE_REL_PPC_ADDR64 0x0001  64-bit address */
    367   /* Unused: */
    368   HOWTO(IMAGE_REL_PPC_ADDR64,    /* type */
    369 	0,	                 /* rightshift */
    370 	3,	                 /* size (0 = byte, 1 = short, 2 = long) */
    371 	64,	                 /* bitsize */
    372 	FALSE,	                 /* pc_relative */
    373 	0,	                 /* bitpos */
    374 	complain_overflow_bitfield, 	 /* complain_on_overflow */
    375 	0,		         /* special_function */
    376 	"ADDR64",               /* name */
    377 	TRUE,	                 /* partial_inplace */
    378 	MINUS_ONE,	 	 /* src_mask */
    379 	MINUS_ONE,        	 /* dst_mask */
    380 	FALSE),                 /* pcrel_offset */
    381 
    382   /* IMAGE_REL_PPC_ADDR32 0x0002  32-bit address */
    383   /* Used: */
    384   HOWTO (IMAGE_REL_PPC_ADDR32,	/* type */
    385 	 0,	                /* rightshift */
    386 	 2,	                /* size (0 = byte, 1 = short, 2 = long) */
    387 	 32,	                /* bitsize */
    388 	 FALSE,	                /* pc_relative */
    389 	 0,	                /* bitpos */
    390 	 complain_overflow_bitfield, /* complain_on_overflow */
    391 	 0,		        /* special_function */
    392 	 "ADDR32",              /* name */
    393 	 TRUE,	                /* partial_inplace */
    394 	 0xffffffff,            /* src_mask */
    395 	 0xffffffff,            /* dst_mask */
    396 	 FALSE),                /* pcrel_offset */
    397 
    398   /* IMAGE_REL_PPC_ADDR24 0x0003  26-bit address, shifted left 2 (branch absolute) */
    399   /* the LI field is in bit 6 through bit 29 is 24 bits, + 2 for the shift */
    400   /* Of course, That's the IBM approved bit numbering, which is not what */
    401   /* anyone else uses.... The li field is in bit 2 thru 25 */
    402   /* Used: */
    403   HOWTO (IMAGE_REL_PPC_ADDR24,  /* type */
    404 	 0,	                /* rightshift */
    405 	 2,	                /* size (0 = byte, 1 = short, 2 = long) */
    406 	 26,	                /* bitsize */
    407 	 FALSE,	                /* pc_relative */
    408 	 0,	                /* bitpos */
    409 	 complain_overflow_bitfield, /* complain_on_overflow */
    410 	 0,		        /* special_function */
    411 	 "ADDR24",              /* name */
    412 	 TRUE,	                /* partial_inplace */
    413 	 0x07fffffc,	        /* src_mask */
    414 	 0x07fffffc,        	/* dst_mask */
    415 	 FALSE),                /* pcrel_offset */
    416 
    417   /* IMAGE_REL_PPC_ADDR16 0x0004  16-bit address */
    418   /* Used: */
    419   HOWTO (IMAGE_REL_PPC_ADDR16,  /* type */
    420 	 0,	                /* rightshift */
    421 	 1,	                /* size (0 = byte, 1 = short, 2 = long) */
    422 	 16,	                /* bitsize */
    423 	 FALSE,	                /* pc_relative */
    424 	 0,	                /* bitpos */
    425 	 complain_overflow_signed, /* complain_on_overflow */
    426 	 0,		        /* special_function */
    427 	 "ADDR16",              /* name */
    428 	 TRUE,	                /* partial_inplace */
    429 	 0xffff,	        /* src_mask */
    430 	 0xffff,        	/* dst_mask */
    431 	 FALSE),                /* pcrel_offset */
    432 
    433   /* IMAGE_REL_PPC_ADDR14 0x0005 */
    434   /*  16-bit address, shifted left 2 (load doubleword) */
    435   /* FIXME: the mask is likely wrong, and the bit position may be as well */
    436   /* Unused: */
    437   HOWTO (IMAGE_REL_PPC_ADDR14,  /* type */
    438 	 1,	                /* rightshift */
    439 	 1,	                /* size (0 = byte, 1 = short, 2 = long) */
    440 	 16,	                /* bitsize */
    441 	 FALSE,	                /* pc_relative */
    442 	 0,	                /* bitpos */
    443 	 complain_overflow_signed, /* complain_on_overflow */
    444 	 0,		        /* special_function */
    445 	 "ADDR16",              /* name */
    446 	 TRUE,	                /* partial_inplace */
    447 	 0xffff,	        /* src_mask */
    448 	 0xffff,        	/* dst_mask */
    449 	 FALSE),                /* pcrel_offset */
    450 
    451   /* IMAGE_REL_PPC_REL24 0x0006 */
    452   /*   26-bit PC-relative offset, shifted left 2 (branch relative) */
    453   /* Used: */
    454   HOWTO (IMAGE_REL_PPC_REL24,   /* type */
    455 	 0,	                /* rightshift */
    456 	 2,	                /* size (0 = byte, 1 = short, 2 = long) */
    457 	 26,	                /* bitsize */
    458 	 TRUE,	                /* pc_relative */
    459 	 0,	                /* bitpos */
    460 	 complain_overflow_signed, /* complain_on_overflow */
    461 	 0,		        /* special_function */
    462 	 "REL24",               /* name */
    463 	 TRUE,	                /* partial_inplace */
    464 	 0x3fffffc,	        /* src_mask */
    465 	 0x3fffffc,        	/* dst_mask */
    466 	 FALSE),                /* pcrel_offset */
    467 
    468   /* IMAGE_REL_PPC_REL14 0x0007 */
    469   /*   16-bit PC-relative offset, shifted left 2 (br cond relative) */
    470   /* FIXME: the mask is likely wrong, and the bit position may be as well */
    471   /* FIXME: how does it know how far to shift? */
    472   /* Unused: */
    473   HOWTO (IMAGE_REL_PPC_ADDR14,  /* type */
    474 	 1,	                /* rightshift */
    475 	 1,	                /* size (0 = byte, 1 = short, 2 = long) */
    476 	 16,	                /* bitsize */
    477 	 FALSE,	                /* pc_relative */
    478 	 0,	                /* bitpos */
    479 	 complain_overflow_signed, /* complain_on_overflow */
    480 	 0,		        /* special_function */
    481 	 "ADDR16",              /* name */
    482 	 TRUE,	                /* partial_inplace */
    483 	 0xffff,	        /* src_mask */
    484 	 0xffff,        	/* dst_mask */
    485 	 TRUE),                 /* pcrel_offset */
    486 
    487   /* IMAGE_REL_PPC_TOCREL16 0x0008 */
    488   /*   16-bit offset from TOC base */
    489   /* Used: */
    490   HOWTO (IMAGE_REL_PPC_TOCREL16,/* type */
    491 	 0,	                /* rightshift */
    492 	 1,	                /* size (0 = byte, 1 = short, 2 = long) */
    493 	 16,	                /* bitsize */
    494 	 FALSE,	                /* pc_relative */
    495 	 0,	                /* bitpos */
    496 	 complain_overflow_dont, /* complain_on_overflow */
    497 	 ppc_toc16_reloc,       /* special_function */
    498 	 "TOCREL16",            /* name */
    499 	 FALSE,	                /* partial_inplace */
    500 	 0xffff,	        /* src_mask */
    501 	 0xffff,        	/* dst_mask */
    502 	 FALSE),                /* pcrel_offset */
    503 
    504   /* IMAGE_REL_PPC_TOCREL14 0x0009 */
    505   /*   16-bit offset from TOC base, shifted left 2 (load doubleword) */
    506   /* Unused: */
    507   HOWTO (IMAGE_REL_PPC_TOCREL14,/* type */
    508 	 1,	                /* rightshift */
    509 	 1,	                /* size (0 = byte, 1 = short, 2 = long) */
    510 	 16,	                /* bitsize */
    511 	 FALSE,	                /* pc_relative */
    512 	 0,	                /* bitpos */
    513 	 complain_overflow_signed, /* complain_on_overflow */
    514 	 0,		        /* special_function */
    515 	 "TOCREL14",            /* name */
    516 	 FALSE,	                /* partial_inplace */
    517 	 0xffff,	        /* src_mask */
    518 	 0xffff,        	/* dst_mask */
    519 	 FALSE),                /* pcrel_offset */
    520 
    521   /* IMAGE_REL_PPC_ADDR32NB 0x000A */
    522   /*   32-bit addr w/ image base */
    523   /* Unused: */
    524   HOWTO (IMAGE_REL_PPC_ADDR32NB,/* type */
    525 	 0,	                /* rightshift */
    526 	 2,	                /* size (0 = byte, 1 = short, 2 = long) */
    527 	 32,	                /* bitsize */
    528 	 FALSE,	                /* pc_relative */
    529 	 0,	                /* bitpos */
    530 	 complain_overflow_signed, /* complain_on_overflow */
    531 	 0,                     /* special_function */
    532 	 "ADDR32NB",            /* name */
    533 	 TRUE,	                /* partial_inplace */
    534 	 0xffffffff,	        /* src_mask */
    535 	 0xffffffff,        	/* dst_mask */
    536 	 FALSE),                 /* pcrel_offset */
    537 
    538   /* IMAGE_REL_PPC_SECREL 0x000B */
    539   /*   va of containing section (as in an image sectionhdr) */
    540   /* Unused: */
    541   HOWTO (IMAGE_REL_PPC_SECREL,/* type */
    542 	 0,	                /* rightshift */
    543 	 2,	                /* size (0 = byte, 1 = short, 2 = long) */
    544 	 32,	                /* bitsize */
    545 	 FALSE,	                /* pc_relative */
    546 	 0,	                /* bitpos */
    547 	 complain_overflow_signed, /* complain_on_overflow */
    548 	 ppc_secrel_reloc,      /* special_function */
    549 	 "SECREL",              /* name */
    550 	 TRUE,	                /* partial_inplace */
    551 	 0xffffffff,	        /* src_mask */
    552 	 0xffffffff,        	/* dst_mask */
    553 	 TRUE),                 /* pcrel_offset */
    554 
    555   /* IMAGE_REL_PPC_SECTION 0x000C */
    556   /*   sectionheader number */
    557   /* Unused: */
    558   HOWTO (IMAGE_REL_PPC_SECTION,/* type */
    559 	 0,	                /* rightshift */
    560 	 2,	                /* size (0 = byte, 1 = short, 2 = long) */
    561 	 32,	                /* bitsize */
    562 	 FALSE,	                /* pc_relative */
    563 	 0,	                /* bitpos */
    564 	 complain_overflow_signed, /* complain_on_overflow */
    565 	 ppc_section_reloc,     /* special_function */
    566 	 "SECTION",             /* name */
    567 	 TRUE,	                /* partial_inplace */
    568 	 0xffffffff,	        /* src_mask */
    569 	 0xffffffff,        	/* dst_mask */
    570 	 TRUE),                 /* pcrel_offset */
    571 
    572   /* IMAGE_REL_PPC_IFGLUE 0x000D */
    573   /*   substitute TOC restore instruction iff symbol is glue code */
    574   /* Used: */
    575   HOWTO (IMAGE_REL_PPC_IFGLUE,/* type */
    576 	 0,	                /* rightshift */
    577 	 2,	                /* size (0 = byte, 1 = short, 2 = long) */
    578 	 32,	                /* bitsize */
    579 	 FALSE,	                /* pc_relative */
    580 	 0,	                /* bitpos */
    581 	 complain_overflow_signed, /* complain_on_overflow */
    582 	 0,		        /* special_function */
    583 	 "IFGLUE",              /* name */
    584 	 TRUE,	                /* partial_inplace */
    585 	 0xffffffff,	        /* src_mask */
    586 	 0xffffffff,        	/* dst_mask */
    587 	 FALSE),                /* pcrel_offset */
    588 
    589   /* IMAGE_REL_PPC_IMGLUE 0x000E */
    590   /*   symbol is glue code; virtual address is TOC restore instruction */
    591   /* Unused: */
    592   HOWTO (IMAGE_REL_PPC_IMGLUE,/* type */
    593 	 0,	                /* rightshift */
    594 	 2,	                /* size (0 = byte, 1 = short, 2 = long) */
    595 	 32,	                /* bitsize */
    596 	 FALSE,	                /* pc_relative */
    597 	 0,	                /* bitpos */
    598 	 complain_overflow_dont, /* complain_on_overflow */
    599 	 ppc_imglue_reloc,      /* special_function */
    600 	 "IMGLUE",              /* name */
    601 	 FALSE,	                /* partial_inplace */
    602 	 0xffffffff,	        /* src_mask */
    603 	 0xffffffff,        	/* dst_mask */
    604 	 FALSE),                 /* pcrel_offset */
    605 
    606   /* IMAGE_REL_PPC_SECREL16 0x000F */
    607   /*   va of containing section (limited to 16 bits) */
    608   /* Unused: */
    609   HOWTO (IMAGE_REL_PPC_SECREL16,/* type */
    610 	 0,	                /* rightshift */
    611 	 1,	                /* size (0 = byte, 1 = short, 2 = long) */
    612 	 16,	                /* bitsize */
    613 	 FALSE,	                /* pc_relative */
    614 	 0,	                /* bitpos */
    615 	 complain_overflow_signed, /* complain_on_overflow */
    616 	 0,		        /* special_function */
    617 	 "SECREL16",            /* name */
    618 	 TRUE,	                /* partial_inplace */
    619 	 0xffff,	        /* src_mask */
    620 	 0xffff,        	/* dst_mask */
    621 	 TRUE),                 /* pcrel_offset */
    622 
    623   /* IMAGE_REL_PPC_REFHI             0x0010 */
    624   /* Unused: */
    625   HOWTO (IMAGE_REL_PPC_REFHI,   /* type */
    626 	 0,	                /* rightshift */
    627 	 1,	                /* size (0 = byte, 1 = short, 2 = long) */
    628 	 16,	                /* bitsize */
    629 	 FALSE,	                /* pc_relative */
    630 	 0,	                /* bitpos */
    631 	 complain_overflow_signed, /* complain_on_overflow */
    632 	 ppc_refhi_reloc,	/* special_function */
    633 	 "REFHI",               /* name */
    634 	 TRUE,	                /* partial_inplace */
    635 	 0xffffffff,	        /* src_mask */
    636 	 0xffffffff,        	/* dst_mask */
    637 	 FALSE),                 /* pcrel_offset */
    638 
    639   /* IMAGE_REL_PPC_REFLO             0x0011 */
    640   /* Unused: */
    641   HOWTO (IMAGE_REL_PPC_REFLO,   /* type */
    642 	 0,	                /* rightshift */
    643 	 1,	                /* size (0 = byte, 1 = short, 2 = long) */
    644 	 16,	                /* bitsize */
    645 	 FALSE,	                /* pc_relative */
    646 	 0,	                /* bitpos */
    647 	 complain_overflow_signed, /* complain_on_overflow */
    648 	 ppc_refhi_reloc,	/* special_function */
    649 	 "REFLO",               /* name */
    650 	 TRUE,	                /* partial_inplace */
    651 	 0xffffffff,	        /* src_mask */
    652 	 0xffffffff,        	/* dst_mask */
    653 	 FALSE),                /* pcrel_offset */
    654 
    655   /* IMAGE_REL_PPC_PAIR              0x0012 */
    656   /* Unused: */
    657   HOWTO (IMAGE_REL_PPC_PAIR,    /* type */
    658 	 0,	                /* rightshift */
    659 	 1,	                /* size (0 = byte, 1 = short, 2 = long) */
    660 	 16,	                /* bitsize */
    661 	 FALSE,	                /* pc_relative */
    662 	 0,	                /* bitpos */
    663 	 complain_overflow_signed, /* complain_on_overflow */
    664 	 ppc_pair_reloc,        /* special_function */
    665 	 "PAIR",                /* name */
    666 	 TRUE,	                /* partial_inplace */
    667 	 0xffffffff,	        /* src_mask */
    668 	 0xffffffff,        	/* dst_mask */
    669 	 FALSE),                /* pcrel_offset */
    670 
    671   /* IMAGE_REL_PPC_TOCREL16_DEFN 0x0013 */
    672   /*   16-bit offset from TOC base, without causing a definition */
    673   /* Used: */
    674   HOWTO ( (IMAGE_REL_PPC_TOCREL16 | IMAGE_REL_PPC_TOCDEFN), /* type */
    675 	 0,	                /* rightshift */
    676 	 1,	                /* size (0 = byte, 1 = short, 2 = long) */
    677 	 16,	                /* bitsize */
    678 	 FALSE,	                /* pc_relative */
    679 	 0,	                /* bitpos */
    680 	 complain_overflow_dont, /* complain_on_overflow */
    681 	 0,                     /* special_function */
    682 	 "TOCREL16, TOCDEFN",   /* name */
    683 	 FALSE,	                /* partial_inplace */
    684 	 0xffff,	        /* src_mask */
    685 	 0xffff,        	/* dst_mask */
    686 	 FALSE),                /* pcrel_offset */
    687 
    688 };
    689 
    690 /* Some really cheezy macros that can be turned on to test stderr :-)  */
    692 
    693 #ifdef DEBUG_RELOC
    694 #define UN_IMPL(x)                                           \
    695 {                                                            \
    696    static int i;                                             \
    697    if (i == 0)                                               \
    698      {                                                       \
    699        i = 1;                                                \
    700        fprintf (stderr,_("Unimplemented Relocation -- %s\n"),x); \
    701      }                                                       \
    702 }
    703 
    704 #define DUMP_RELOC(n,r)                              \
    705 {                                                    \
    706    fprintf (stderr,"%s sym %d, addr %d, addend %d\n", \
    707 	   n, (*(r->sym_ptr_ptr))->name,             \
    708 	   r->address, r->addend);                   \
    709 }
    710 
    711 /* Given a reloc name, n, and a pointer to an internal_reloc,
    712    dump out interesting information on the contents
    713 
    714 #define n_name		_n._n_name
    715 #define n_zeroes	_n._n_n._n_zeroes
    716 #define n_offset	_n._n_n._n_offset  */
    717 
    718 #define DUMP_RELOC2(n,r)                     		\
    719 {                                            		\
    720    fprintf (stderr,"%s sym %d, r_vaddr %d %s\n", 	\
    721 	   n, r->r_symndx, r->r_vaddr,			\
    722 	   (((r->r_type) & IMAGE_REL_PPC_TOCDEFN) == 0) \
    723 	   ?" ":" TOCDEFN"  );      			\
    724 }
    725 
    726 #else
    727 #define UN_IMPL(x)
    728 #define DUMP_RELOC(n,r)
    729 #define DUMP_RELOC2(n,r)
    730 #endif
    731 
    732 /* TOC construction and management routines.  */
    734 
    735 /* This file is compiled twice, and these variables are defined in one
    736    of the compilations.  FIXME: This is confusing and weird.  Also,
    737    BFD should not use global variables.  */
    738 extern bfd *    bfd_of_toc_owner;
    739 extern long int global_toc_size;
    740 extern long int import_table_size;
    741 extern long int first_thunk_address;
    742 extern long int thunk_size;
    743 
    744 enum toc_type
    745 {
    746   default_toc,
    747   toc_32,
    748   toc_64
    749 };
    750 
    751 enum ref_category
    752 {
    753   priv,
    754   pub,
    755   tocdata
    756 };
    757 
    758 struct list_ele
    759 {
    760   struct list_ele *next;
    761   bfd_vma addr;
    762   enum ref_category cat;
    763   int offset;
    764   const char *name;
    765 };
    766 
    767 extern struct list_ele *head;
    768 extern struct list_ele *tail;
    769 
    770 static void
    771 record_toc (asection *toc_section,
    772 	    bfd_signed_vma our_toc_offset,
    773 	    enum ref_category cat,
    774 	    const char *name)
    775 {
    776   /* Add this entry to our toc addr-offset-name list.  */
    777   bfd_size_type amt = sizeof (struct list_ele);
    778   struct list_ele *t = (struct list_ele *) bfd_malloc (amt);
    779 
    780   if (t == NULL)
    781     abort ();
    782   t->next = 0;
    783   t->offset = our_toc_offset;
    784   t->name = name;
    785   t->cat = cat;
    786   t->addr = toc_section->output_offset + our_toc_offset;
    787 
    788   if (head == 0)
    789     {
    790       head = t;
    791       tail = t;
    792     }
    793   else
    794     {
    795       tail->next = t;
    796       tail = t;
    797     }
    798 }
    799 
    800 #ifdef COFF_IMAGE_WITH_PE
    801 
    802 /* Record a toc offset against a symbol.  */
    803 static bfd_boolean
    804 ppc_record_toc_entry (bfd *abfd,
    805 		      struct bfd_link_info *info ATTRIBUTE_UNUSED,
    806 		      asection *sec ATTRIBUTE_UNUSED,
    807 		      int sym,
    808 		      enum toc_type toc_kind ATTRIBUTE_UNUSED)
    809 {
    810   struct ppc_coff_link_hash_entry *h;
    811   int *local_syms;
    812 
    813   h = 0;
    814 
    815   h = (struct ppc_coff_link_hash_entry *) (obj_coff_sym_hashes (abfd)[sym]);
    816   if (h != 0)
    817     {
    818       HASH_CHECK(h);
    819     }
    820 
    821   if (h == 0)
    822     {
    823       local_syms = obj_coff_local_toc_table(abfd);
    824 
    825       if (local_syms == 0)
    826 	{
    827 	  unsigned int i;
    828 	  bfd_size_type amt;
    829 
    830 	  /* allocate a table */
    831 	  amt = (bfd_size_type) obj_raw_syment_count (abfd) * sizeof (int);
    832 	  local_syms = (int *) bfd_zalloc (abfd, amt);
    833 	  if (local_syms == 0)
    834 	    return FALSE;
    835 	  obj_coff_local_toc_table (abfd) = local_syms;
    836 
    837 	  for (i = 0; i < obj_raw_syment_count (abfd); ++i)
    838 	    {
    839 	      SET_UNALLOCATED (local_syms[i]);
    840 	    }
    841 	}
    842 
    843       if (IS_UNALLOCATED(local_syms[sym]))
    844 	{
    845 	  local_syms[sym] = global_toc_size;
    846 	  global_toc_size += 4;
    847 
    848 	  /* The size must fit in a 16-bit displacement.  */
    849 	  if (global_toc_size > 65535)
    850 	    {
    851 	      (*_bfd_error_handler) (_("TOC overflow"));
    852 	      bfd_set_error (bfd_error_file_too_big);
    853 	      return FALSE;
    854 	    }
    855 	}
    856     }
    857   else
    858     {
    859       /* Check to see if there's a toc slot allocated. If not, do it
    860 	 here. It will be used in relocate_section.  */
    861       if (IS_UNALLOCATED(h->toc_offset))
    862 	{
    863 	  h->toc_offset = global_toc_size;
    864 	  global_toc_size += 4;
    865 
    866 	  /* The size must fit in a 16-bit displacement.  */
    867 	  if (global_toc_size >= 65535)
    868 	    {
    869 	      (*_bfd_error_handler) (_("TOC overflow"));
    870 	      bfd_set_error (bfd_error_file_too_big);
    871 	      return FALSE;
    872 	    }
    873 	}
    874     }
    875 
    876   return TRUE;
    877 }
    878 
    879 /* Record a toc offset against a symbol.  */
    880 static void
    881 ppc_mark_symbol_as_glue (bfd *abfd,
    882 			 int sym,
    883 			 struct internal_reloc *rel)
    884 {
    885   struct ppc_coff_link_hash_entry *h;
    886 
    887   h = (struct ppc_coff_link_hash_entry *) (obj_coff_sym_hashes (abfd)[sym]);
    888 
    889   HASH_CHECK(h);
    890 
    891   h->symbol_is_glue = 1;
    892   h->glue_insn = bfd_get_32 (abfd, (bfd_byte *) &rel->r_vaddr);
    893 
    894   return;
    895 }
    896 
    897 #endif /* COFF_IMAGE_WITH_PE */
    898 
    899 /* Return TRUE if this relocation should
    901    appear in the output .reloc section.  */
    902 
    903 static bfd_boolean
    904 in_reloc_p (bfd * abfd ATTRIBUTE_UNUSED,
    905 	    reloc_howto_type *howto)
    906 {
    907   return
    908     (! howto->pc_relative)
    909       && (howto->type != IMAGE_REL_PPC_ADDR32NB)
    910       && (howto->type != IMAGE_REL_PPC_TOCREL16)
    911       && (howto->type != IMAGE_REL_PPC_IMGLUE)
    912       && (howto->type != IMAGE_REL_PPC_IFGLUE)
    913       && (howto->type != IMAGE_REL_PPC_SECREL)
    914       && (howto->type != IMAGE_REL_PPC_SECTION)
    915       && (howto->type != IMAGE_REL_PPC_SECREL16)
    916       && (howto->type != IMAGE_REL_PPC_REFHI)
    917       && (howto->type != IMAGE_REL_PPC_REFLO)
    918       && (howto->type != IMAGE_REL_PPC_PAIR)
    919       && (howto->type != IMAGE_REL_PPC_TOCREL16_DEFN) ;
    920 }
    921 
    922 static bfd_boolean
    923 write_base_file_entry (bfd *obfd, struct bfd_link_info *info, bfd_vma addr)
    924 {
    925   if (coff_data (obfd)->pe)
    926      addr -= pe_data (obfd)->pe_opthdr.ImageBase;
    927   if (fwrite (&addr, sizeof (addr), 1, (FILE *) info->base_file) == 1)
    928     return TRUE;
    929 
    930   bfd_set_error (bfd_error_system_call);
    931   return FALSE;
    932 }
    933 
    934 /* The reloc processing routine for the optimized COFF linker.  */
    935 
    936 static bfd_boolean
    937 coff_ppc_relocate_section (bfd *output_bfd,
    938 			   struct bfd_link_info *info,
    939 			   bfd *input_bfd,
    940 			   asection *input_section,
    941 			   bfd_byte *contents,
    942 			   struct internal_reloc *relocs,
    943 			   struct internal_syment *syms,
    944 			   asection **sections)
    945 {
    946   struct internal_reloc *rel;
    947   struct internal_reloc *relend;
    948   asection *toc_section = 0;
    949   bfd_vma relocation;
    950   reloc_howto_type *howto = 0;
    951 
    952   /* If we are performing a relocatable link, we don't need to do a
    953      thing.  The caller will take care of adjusting the reloc
    954      addresses and symbol indices.  */
    955   if (info->relocatable)
    956     return TRUE;
    957 
    958   rel = relocs;
    959   relend = rel + input_section->reloc_count;
    960   for (; rel < relend; rel++)
    961     {
    962       long symndx;
    963       struct ppc_coff_link_hash_entry *h;
    964       struct internal_syment *sym;
    965       bfd_vma val;
    966 
    967       asection *sec;
    968       bfd_reloc_status_type rstat;
    969       bfd_byte *loc;
    970 
    971       unsigned short r_type  = EXTRACT_TYPE (rel->r_type);
    972       unsigned short r_flags = EXTRACT_FLAGS(rel->r_type);
    973 
    974       symndx = rel->r_symndx;
    975       loc = contents + rel->r_vaddr - input_section->vma;
    976 
    977       /* FIXME: check bounds on r_type */
    978       howto = ppc_coff_howto_table + r_type;
    979 
    980       if (symndx == -1)
    981 	{
    982 	  h = NULL;
    983 	  sym = NULL;
    984 	}
    985       else
    986 	{
    987 	  h = (struct ppc_coff_link_hash_entry *)
    988 	    (obj_coff_sym_hashes (input_bfd)[symndx]);
    989 	  if (h != 0)
    990 	    {
    991 	      HASH_CHECK(h);
    992 	    }
    993 
    994 	  sym = syms + symndx;
    995 	}
    996 
    997       if (r_type == IMAGE_REL_PPC_IMGLUE && h == 0)
    998 	{
    999 	  /* An IMGLUE reloc must have a name. Something is very wrong.  */
   1000 	  abort ();
   1001 	}
   1002 
   1003       sec = NULL;
   1004       val = 0;
   1005 
   1006       /* FIXME: PAIR unsupported in the following code.  */
   1007       if (h == NULL)
   1008 	{
   1009 	  if (symndx == -1)
   1010 	    sec = bfd_abs_section_ptr;
   1011 	  else
   1012 	    {
   1013 	      sec = sections[symndx];
   1014 	      val = (sec->output_section->vma
   1015 		     + sec->output_offset
   1016 		     + sym->n_value);
   1017 	      if (! obj_pe (output_bfd))
   1018 		val -= sec->vma;
   1019 	    }
   1020 	}
   1021       else
   1022 	{
   1023 	  HASH_CHECK(h);
   1024 
   1025 	  if (h->root.root.type == bfd_link_hash_defined
   1026 	      || h->root.root.type == bfd_link_hash_defweak)
   1027 	    {
   1028 	      sec = h->root.root.u.def.section;
   1029 	      val = (h->root.root.u.def.value
   1030 		     + sec->output_section->vma
   1031 		     + sec->output_offset);
   1032 	    }
   1033 	  else
   1034 	    {
   1035 	      if (! ((*info->callbacks->undefined_symbol)
   1036 		     (info, h->root.root.root.string, input_bfd, input_section,
   1037 		      rel->r_vaddr - input_section->vma, TRUE)))
   1038 		return FALSE;
   1039 	    }
   1040 	}
   1041 
   1042       rstat = bfd_reloc_ok;
   1043 
   1044       /* Each case must do its own relocation, setting rstat appropriately.  */
   1045       switch (r_type)
   1046 	{
   1047 	default:
   1048 	  (*_bfd_error_handler)
   1049 	    (_("%B: unsupported relocation type 0x%02x"), input_bfd, r_type);
   1050 	  bfd_set_error (bfd_error_bad_value);
   1051 	  return FALSE;
   1052 	case IMAGE_REL_PPC_TOCREL16:
   1053 	  {
   1054 	    bfd_signed_vma our_toc_offset;
   1055 	    int fixit;
   1056 
   1057 	    DUMP_RELOC2(howto->name, rel);
   1058 
   1059 	    if (toc_section == 0)
   1060 	      {
   1061 		toc_section = bfd_get_section_by_name (bfd_of_toc_owner,
   1062 						       TOC_SECTION_NAME);
   1063 
   1064 		if ( toc_section == NULL )
   1065 		  {
   1066 		    /* There is no toc section. Something is very wrong.  */
   1067 		    abort ();
   1068 		  }
   1069 	      }
   1070 
   1071 	    /* Amazing bit tricks present. As we may have seen earlier, we
   1072 	       use the 1 bit to tell us whether or not a toc offset has been
   1073 	       allocated. Now that they've all been allocated, we will use
   1074 	       the 1 bit to tell us if we've written this particular toc
   1075 	       entry out.  */
   1076 	    fixit = FALSE;
   1077 	    if (h == 0)
   1078 	      {
   1079 		/* It is a file local symbol.  */
   1080 		int *local_toc_table;
   1081 		char name[SYMNMLEN + 1];
   1082 
   1083 		sym = syms + symndx;
   1084 		strncpy (name, sym->_n._n_name, SYMNMLEN);
   1085 		name[SYMNMLEN] = '\0';
   1086 
   1087 		local_toc_table = obj_coff_local_toc_table(input_bfd);
   1088 		our_toc_offset = local_toc_table[symndx];
   1089 
   1090 		if (IS_WRITTEN(our_toc_offset))
   1091 		  {
   1092 		    /* If it has been written out, it is marked with the
   1093 		       1 bit. Fix up our offset, but do not write it out
   1094 		       again.  */
   1095 		    MAKE_ADDR_AGAIN(our_toc_offset);
   1096 		  }
   1097 		else
   1098 		  {
   1099 		    /* Write out the toc entry.  */
   1100 		    record_toc (toc_section, our_toc_offset, priv,
   1101 				strdup (name));
   1102 
   1103 		    bfd_put_32 (output_bfd, val,
   1104 			       toc_section->contents + our_toc_offset);
   1105 
   1106 		    MARK_AS_WRITTEN(local_toc_table[symndx]);
   1107 		    fixit = TRUE;
   1108 		  }
   1109 	      }
   1110 	    else
   1111 	      {
   1112 		const char *name = h->root.root.root.string;
   1113 		our_toc_offset = h->toc_offset;
   1114 
   1115 		if ((r_flags & IMAGE_REL_PPC_TOCDEFN)
   1116 		    == IMAGE_REL_PPC_TOCDEFN )
   1117 		  {
   1118 		    /* This is unbelievable cheese. Some knowledgable asm
   1119 		       hacker has decided to use r2 as a base for loading
   1120 		       a value. He/She does this by setting the tocdefn bit,
   1121 		       and not supplying a toc definition. The behaviour is
   1122 		       then to use the difference between the value of the
   1123 		       symbol and the actual location of the toc as the toc
   1124 		       index.
   1125 
   1126 		       In fact, what is usually happening is, because the
   1127 		       Import Address Table is mapped immediately following
   1128 		       the toc, some trippy library code trying for speed on
   1129 		       dll linkage, takes advantage of that and considers
   1130 		       the IAT to be part of the toc, thus saving a load.  */
   1131 
   1132 		    our_toc_offset = val - (toc_section->output_section->vma
   1133 					    + toc_section->output_offset);
   1134 
   1135 		    /* The size must still fit in a 16-bit displacement.  */
   1136 		    if ((bfd_vma) our_toc_offset >= 65535)
   1137 		      {
   1138 			(*_bfd_error_handler)
   1139 			  (_("%B: Relocation for %s of %lx exceeds Toc size limit"),
   1140 			   input_bfd, name,
   1141 			   (unsigned long) our_toc_offset);
   1142 			bfd_set_error (bfd_error_bad_value);
   1143 			return FALSE;
   1144 		      }
   1145 
   1146 		    record_toc (toc_section, our_toc_offset, pub,
   1147 				strdup (name));
   1148 		  }
   1149 		else if (IS_WRITTEN (our_toc_offset))
   1150 		  {
   1151 		    /* If it has been written out, it is marked with the
   1152 		       1 bit. Fix up our offset, but do not write it out
   1153 		       again.  */
   1154 		    MAKE_ADDR_AGAIN(our_toc_offset);
   1155 		  }
   1156 		else
   1157 		  {
   1158 		    record_toc(toc_section, our_toc_offset, pub,
   1159 			       strdup (name));
   1160 
   1161 		    /* Write out the toc entry.  */
   1162 		    bfd_put_32 (output_bfd, val,
   1163 			       toc_section->contents + our_toc_offset);
   1164 
   1165 		    MARK_AS_WRITTEN(h->toc_offset);
   1166 		    /* The tricky part is that this is the address that
   1167 		       needs a .reloc entry for it.  */
   1168 		    fixit = TRUE;
   1169 		  }
   1170 	      }
   1171 
   1172 	    if (fixit && info->base_file)
   1173 	      {
   1174 		/* So if this is non pcrelative, and is referenced
   1175 		   to a section or a common symbol, then it needs a reloc.  */
   1176 
   1177 		/* Relocation to a symbol in a section which
   1178 		   isn't absolute - we output the address here
   1179 		   to a file.  */
   1180 		bfd_vma addr = (toc_section->output_section->vma
   1181 				+ toc_section->output_offset + our_toc_offset);
   1182 
   1183 		if (!write_base_file_entry (output_bfd, info, addr))
   1184 		  return FALSE;
   1185 	      }
   1186 
   1187 	    /* FIXME: this test is conservative.  */
   1188 	    if ((r_flags & IMAGE_REL_PPC_TOCDEFN) != IMAGE_REL_PPC_TOCDEFN
   1189 		&& (bfd_vma) our_toc_offset > toc_section->size)
   1190 	      {
   1191 		(*_bfd_error_handler)
   1192 		  (_("%B: Relocation exceeds allocated TOC (%lx)"),
   1193 		   input_bfd, (unsigned long) toc_section->size);
   1194 		bfd_set_error (bfd_error_bad_value);
   1195 		return FALSE;
   1196 	      }
   1197 
   1198 	    /* Now we know the relocation for this toc reference.  */
   1199 	    relocation =  our_toc_offset + TOC_LOAD_ADJUSTMENT;
   1200 	    rstat = _bfd_relocate_contents (howto, input_bfd, relocation, loc);
   1201 	  }
   1202 	  break;
   1203 	case IMAGE_REL_PPC_IFGLUE:
   1204 	  {
   1205 	    /* To solve this, we need to know whether or not the symbol
   1206 	       appearing on the call instruction is a glue function or not.
   1207 	       A glue function must announce itself via a IMGLUE reloc, and
   1208 	       the reloc contains the required toc restore instruction.  */
   1209 	    DUMP_RELOC2 (howto->name, rel);
   1210 
   1211 	    if (h != 0)
   1212 	      {
   1213 		if (h->symbol_is_glue == 1)
   1214 		  {
   1215 		    bfd_put_32 (input_bfd, (bfd_vma) h->glue_insn, loc);
   1216 		  }
   1217 	      }
   1218 	  }
   1219 	  break;
   1220 	case IMAGE_REL_PPC_SECREL:
   1221 	  /* Unimplemented: codeview debugging information.  */
   1222 	  /* For fast access to the header of the section
   1223 	     containing the item.  */
   1224 	  break;
   1225 	case IMAGE_REL_PPC_SECTION:
   1226 	  /* Unimplemented: codeview debugging information.  */
   1227 	  /* Is used to indicate that the value should be relative
   1228 	     to the beginning of the section that contains the
   1229 	     symbol.  */
   1230 	  break;
   1231 	case IMAGE_REL_PPC_ABSOLUTE:
   1232 	  {
   1233 	    const char *my_name;
   1234 	    char buf[SYMNMLEN + 1];
   1235 
   1236 	    if (h == 0)
   1237 	      {
   1238 		strncpy (buf, (syms+symndx)->_n._n_name, SYMNMLEN);
   1239 		buf[SYMNMLEN] = '\0';
   1240 		my_name = buf;
   1241 	      }
   1242 	    else
   1243 	      my_name = h->root.root.root.string;
   1244 
   1245 	    (*_bfd_error_handler)
   1246 	      (_("Warning: unsupported reloc %s <file %B, section %A>\n"
   1247 		 "sym %ld (%s), r_vaddr %ld (%lx)"),
   1248 	       input_bfd, input_section, howto->name,
   1249 	       rel->r_symndx, my_name, (long) rel->r_vaddr,
   1250 	       (unsigned long) rel->r_vaddr);
   1251 	  }
   1252 	  break;
   1253 	case IMAGE_REL_PPC_IMGLUE:
   1254 	  {
   1255 	    /* There is nothing to do now. This reloc was noted in the first
   1256 	       pass over the relocs, and the glue instruction extracted.  */
   1257 	    const char *my_name;
   1258 
   1259 	    if (h->symbol_is_glue == 1)
   1260 	      break;
   1261 	    my_name = h->root.root.root.string;
   1262 
   1263 	    (*_bfd_error_handler)
   1264 	      (_("%B: Out of order IMGLUE reloc for %s"), input_bfd, my_name);
   1265 	    bfd_set_error (bfd_error_bad_value);
   1266 	    return FALSE;
   1267 	  }
   1268 
   1269 	case IMAGE_REL_PPC_ADDR32NB:
   1270 	  {
   1271 	    const char *name = 0;
   1272 
   1273 	    DUMP_RELOC2 (howto->name, rel);
   1274 
   1275 	    if (CONST_STRNEQ (input_section->name, ".idata$2") && first_thunk_address == 0)
   1276 	      {
   1277 		/* Set magic values.  */
   1278 		int idata5offset;
   1279 		struct coff_link_hash_entry *myh;
   1280 
   1281 		myh = coff_link_hash_lookup (coff_hash_table (info),
   1282 					     "__idata5_magic__",
   1283 					     FALSE, FALSE, TRUE);
   1284 		first_thunk_address = myh->root.u.def.value +
   1285 		  sec->output_section->vma +
   1286 		    sec->output_offset -
   1287 		      pe_data(output_bfd)->pe_opthdr.ImageBase;
   1288 
   1289 		idata5offset = myh->root.u.def.value;
   1290 		myh = coff_link_hash_lookup (coff_hash_table (info),
   1291 					     "__idata6_magic__",
   1292 					     FALSE, FALSE, TRUE);
   1293 
   1294 		thunk_size = myh->root.u.def.value - idata5offset;
   1295 		myh = coff_link_hash_lookup (coff_hash_table (info),
   1296 					     "__idata4_magic__",
   1297 					     FALSE, FALSE, TRUE);
   1298 		import_table_size = myh->root.u.def.value;
   1299 	      }
   1300 
   1301 	    if (h == 0)
   1302 	      /* It is a file local symbol.  */
   1303 	      sym = syms + symndx;
   1304 	    else
   1305 	      {
   1306 		char *target = 0;
   1307 
   1308 		name = h->root.root.root.string;
   1309 		if (strcmp (".idata$2", name) == 0)
   1310 		  target = "__idata2_magic__";
   1311 		else if (strcmp (".idata$4", name) == 0)
   1312 		  target = "__idata4_magic__";
   1313 		else if (strcmp (".idata$5", name) == 0)
   1314 		  target = "__idata5_magic__";
   1315 
   1316 		if (target != 0)
   1317 		  {
   1318 		    struct coff_link_hash_entry *myh;
   1319 
   1320 		    myh = coff_link_hash_lookup (coff_hash_table (info),
   1321 						 target,
   1322 						 FALSE, FALSE, TRUE);
   1323 		    if (myh == 0)
   1324 		      {
   1325 			/* Missing magic cookies. Something is very wrong.  */
   1326 			abort ();
   1327 		      }
   1328 
   1329 		    val = myh->root.u.def.value +
   1330 		      sec->output_section->vma + sec->output_offset;
   1331 		    if (first_thunk_address == 0)
   1332 		      {
   1333 			int idata5offset;
   1334 			myh = coff_link_hash_lookup (coff_hash_table (info),
   1335 						     "__idata5_magic__",
   1336 						     FALSE, FALSE, TRUE);
   1337 			first_thunk_address = myh->root.u.def.value +
   1338 			  sec->output_section->vma +
   1339 			    sec->output_offset -
   1340 			      pe_data(output_bfd)->pe_opthdr.ImageBase;
   1341 
   1342 			idata5offset = myh->root.u.def.value;
   1343 			myh = coff_link_hash_lookup (coff_hash_table (info),
   1344 						     "__idata6_magic__",
   1345 						     FALSE, FALSE, TRUE);
   1346 
   1347 			thunk_size = myh->root.u.def.value - idata5offset;
   1348 			myh = coff_link_hash_lookup (coff_hash_table (info),
   1349 						     "__idata4_magic__",
   1350 						     FALSE, FALSE, TRUE);
   1351 			import_table_size = myh->root.u.def.value;
   1352 		      }
   1353 		  }
   1354 	      }
   1355 
   1356 	    rstat = _bfd_relocate_contents (howto,
   1357 					    input_bfd,
   1358 					    val -
   1359 					    pe_data (output_bfd)->pe_opthdr.ImageBase,
   1360 					    loc);
   1361 	  }
   1362 	  break;
   1363 
   1364 	case IMAGE_REL_PPC_REL24:
   1365 	  DUMP_RELOC2(howto->name, rel);
   1366 	  val -= (input_section->output_section->vma
   1367 		  + input_section->output_offset);
   1368 
   1369 	  rstat = _bfd_relocate_contents (howto,
   1370 					  input_bfd,
   1371 					  val,
   1372 					  loc);
   1373 	  break;
   1374 	case IMAGE_REL_PPC_ADDR16:
   1375 	case IMAGE_REL_PPC_ADDR24:
   1376 	case IMAGE_REL_PPC_ADDR32:
   1377 	  DUMP_RELOC2(howto->name, rel);
   1378 	  rstat = _bfd_relocate_contents (howto,
   1379 					  input_bfd,
   1380 					  val,
   1381 					  loc);
   1382 	  break;
   1383 	}
   1384 
   1385       if (info->base_file)
   1386 	{
   1387 	  /* So if this is non pcrelative, and is referenced
   1388 	     to a section or a common symbol, then it needs a reloc.  */
   1389 	  if (sym && pe_data(output_bfd)->in_reloc_p (output_bfd, howto))
   1390 	    {
   1391 	      /* Relocation to a symbol in a section which
   1392 		 isn't absolute - we output the address here
   1393 		 to a file.  */
   1394 	      bfd_vma addr = (rel->r_vaddr
   1395 			      - input_section->vma
   1396 			      + input_section->output_offset
   1397 			      + input_section->output_section->vma);
   1398 
   1399 	      if (!write_base_file_entry (output_bfd, info, addr))
   1400 		return FALSE;
   1401 	    }
   1402 	}
   1403 
   1404       switch (rstat)
   1405 	{
   1406 	default:
   1407 	  abort ();
   1408 	case bfd_reloc_ok:
   1409 	  break;
   1410 	case bfd_reloc_overflow:
   1411 	  {
   1412 	    const char *name;
   1413 	    char buf[SYMNMLEN + 1];
   1414 
   1415 	    if (symndx == -1)
   1416 	      name = "*ABS*";
   1417 	    else if (h != NULL)
   1418 	      name = NULL;
   1419 	    else if (sym == NULL)
   1420 	      name = "*unknown*";
   1421 	    else if (sym->_n._n_n._n_zeroes == 0
   1422 		     && sym->_n._n_n._n_offset != 0)
   1423 	      name = obj_coff_strings (input_bfd) + sym->_n._n_n._n_offset;
   1424 	    else
   1425 	      {
   1426 		strncpy (buf, sym->_n._n_name, SYMNMLEN);
   1427 		buf[SYMNMLEN] = '\0';
   1428 		name = buf;
   1429 	      }
   1430 
   1431 	    if (! ((*info->callbacks->reloc_overflow)
   1432 		   (info, (h ? &h->root.root : NULL), name, howto->name,
   1433 		    (bfd_vma) 0, input_bfd,
   1434 		    input_section, rel->r_vaddr - input_section->vma)))
   1435 	      return FALSE;
   1436 	  }
   1437 	}
   1438     }
   1439 
   1440   return TRUE;
   1441 }
   1442 
   1443 #ifdef COFF_IMAGE_WITH_PE
   1444 
   1445 /* FIXME: BFD should not use global variables.  This file is compiled
   1446    twice, and these variables are shared.  This is confusing and
   1447    weird.  */
   1448 
   1449 long int global_toc_size = 4;
   1450 
   1451 bfd* bfd_of_toc_owner = 0;
   1452 
   1453 long int import_table_size;
   1454 long int first_thunk_address;
   1455 long int thunk_size;
   1456 
   1457 struct list_ele *head;
   1458 struct list_ele *tail;
   1459 
   1460 static char *
   1461 h1 = N_("\n\t\t\tTOC MAPPING\n\n");
   1462 static char *
   1463 h2 = N_(" TOC    disassembly  Comments       Name\n");
   1464 static char *
   1465 h3 = N_(" Offset  spelling                   (if present)\n");
   1466 
   1467 void
   1468 dump_toc (void * vfile)
   1469 {
   1470   FILE *file = (FILE *) vfile;
   1471   struct list_ele *t;
   1472 
   1473   fputs (_(h1), file);
   1474   fputs (_(h2), file);
   1475   fputs (_(h3), file);
   1476 
   1477   for (t = head; t != 0; t=t->next)
   1478     {
   1479       const char *cat = "";
   1480 
   1481       if (t->cat == priv)
   1482 	cat = _("private       ");
   1483       else if (t->cat == pub)
   1484 	cat = _("public        ");
   1485       else if (t->cat == tocdata)
   1486 	cat = _("data-in-toc   ");
   1487 
   1488       if (t->offset > global_toc_size)
   1489 	{
   1490 	  if (t->offset <= global_toc_size + thunk_size)
   1491 	    cat = _("IAT reference ");
   1492 	  else
   1493 	    {
   1494 	      fprintf (file,
   1495 		      _("**** global_toc_size %ld(%lx), thunk_size %ld(%lx)\n"),
   1496 		       global_toc_size, (unsigned long) global_toc_size,
   1497 		       thunk_size, (unsigned long) thunk_size);
   1498 	      cat = _("Out of bounds!");
   1499 	    }
   1500 	}
   1501 
   1502       fprintf (file,
   1503 	      " %04lx    (%d)", (unsigned long) t->offset, t->offset - 32768);
   1504       fprintf (file,
   1505 	      "    %s %s\n",
   1506 	      cat, t->name);
   1507 
   1508     }
   1509 
   1510   fprintf (file, "\n");
   1511 }
   1512 
   1513 bfd_boolean
   1514 ppc_allocate_toc_section (struct bfd_link_info *info ATTRIBUTE_UNUSED)
   1515 {
   1516   asection *s;
   1517   bfd_byte *foo;
   1518   bfd_size_type amt;
   1519   static char test_char = '1';
   1520 
   1521   if ( global_toc_size == 0 ) /* FIXME: does this get me in trouble?  */
   1522     return TRUE;
   1523 
   1524   if (bfd_of_toc_owner == 0)
   1525     /* No toc owner? Something is very wrong.  */
   1526     abort ();
   1527 
   1528   s = bfd_get_section_by_name ( bfd_of_toc_owner , TOC_SECTION_NAME);
   1529   if (s == NULL)
   1530     /* No toc section? Something is very wrong.  */
   1531     abort ();
   1532 
   1533   amt = global_toc_size;
   1534   foo = (bfd_byte *) bfd_alloc (bfd_of_toc_owner, amt);
   1535   memset(foo, test_char, (size_t) global_toc_size);
   1536 
   1537   s->size = global_toc_size;
   1538   s->contents = foo;
   1539 
   1540   return TRUE;
   1541 }
   1542 
   1543 bfd_boolean
   1544 ppc_process_before_allocation (bfd *abfd,
   1545 			       struct bfd_link_info *info)
   1546 {
   1547   asection *sec;
   1548   struct internal_reloc *i, *rel;
   1549 
   1550   /* Here we have a bfd that is to be included on the link. We have a hook
   1551      to do reloc rummaging, before section sizes are nailed down.  */
   1552   _bfd_coff_get_external_symbols (abfd);
   1553 
   1554   /* Rummage around all the relocs and map the toc.  */
   1555   sec = abfd->sections;
   1556 
   1557   if (sec == 0)
   1558     return TRUE;
   1559 
   1560   for (; sec != 0; sec = sec->next)
   1561     {
   1562       if (sec->reloc_count == 0)
   1563 	continue;
   1564 
   1565       /* load the relocs */
   1566       /* FIXME: there may be a storage leak here */
   1567       i=_bfd_coff_read_internal_relocs(abfd,sec,1,0,0,0);
   1568 
   1569       if (i == 0)
   1570 	abort ();
   1571 
   1572       for (rel = i; rel < i + sec->reloc_count; ++rel)
   1573 	{
   1574 	  unsigned short r_type  = EXTRACT_TYPE  (rel->r_type);
   1575 	  unsigned short r_flags = EXTRACT_FLAGS (rel->r_type);
   1576 	  bfd_boolean ok = TRUE;
   1577 
   1578 	  DUMP_RELOC2 (ppc_coff_howto_table[r_type].name, rel);
   1579 
   1580 	  switch(r_type)
   1581 	    {
   1582 	    case IMAGE_REL_PPC_TOCREL16:
   1583 	      /* If TOCDEFN is on, ignore as someone else has allocated the
   1584 		 toc entry.  */
   1585 	      if ((r_flags & IMAGE_REL_PPC_TOCDEFN) != IMAGE_REL_PPC_TOCDEFN)
   1586 		ok = ppc_record_toc_entry(abfd, info, sec,
   1587 					  rel->r_symndx, default_toc);
   1588 	      if (!ok)
   1589 		return FALSE;
   1590 	      break;
   1591 	    case IMAGE_REL_PPC_IMGLUE:
   1592 	      ppc_mark_symbol_as_glue (abfd, rel->r_symndx, rel);
   1593 	      break;
   1594 	    default:
   1595 	      break;
   1596 	    }
   1597 	}
   1598     }
   1599 
   1600   return TRUE;
   1601 }
   1602 
   1603 #endif
   1604 
   1605 static bfd_reloc_status_type
   1606 ppc_refhi_reloc (bfd *abfd ATTRIBUTE_UNUSED,
   1607 		 arelent *reloc_entry ATTRIBUTE_UNUSED,
   1608 		 asymbol *symbol ATTRIBUTE_UNUSED,
   1609 		 void * data ATTRIBUTE_UNUSED,
   1610 		 asection *input_section ATTRIBUTE_UNUSED,
   1611 		 bfd *output_bfd,
   1612 		 char **error_message ATTRIBUTE_UNUSED)
   1613 {
   1614   UN_IMPL("REFHI");
   1615   DUMP_RELOC("REFHI",reloc_entry);
   1616 
   1617   if (output_bfd == (bfd *) NULL)
   1618     return bfd_reloc_continue;
   1619 
   1620   return bfd_reloc_undefined;
   1621 }
   1622 
   1623 static bfd_reloc_status_type
   1624 ppc_pair_reloc (bfd *abfd ATTRIBUTE_UNUSED,
   1625 		arelent *reloc_entry ATTRIBUTE_UNUSED,
   1626 		asymbol *symbol ATTRIBUTE_UNUSED,
   1627 		void * data ATTRIBUTE_UNUSED,
   1628 		asection *input_section ATTRIBUTE_UNUSED,
   1629 		bfd *output_bfd,
   1630 		char **error_message ATTRIBUTE_UNUSED)
   1631 {
   1632   UN_IMPL("PAIR");
   1633   DUMP_RELOC("PAIR",reloc_entry);
   1634 
   1635   if (output_bfd == (bfd *) NULL)
   1636     return bfd_reloc_continue;
   1637 
   1638   return bfd_reloc_undefined;
   1639 }
   1640 
   1641 static bfd_reloc_status_type
   1642 ppc_toc16_reloc (bfd *abfd ATTRIBUTE_UNUSED,
   1643 		 arelent *reloc_entry ATTRIBUTE_UNUSED,
   1644 		 asymbol *symbol ATTRIBUTE_UNUSED,
   1645 		 void * data ATTRIBUTE_UNUSED,
   1646 		 asection *input_section ATTRIBUTE_UNUSED,
   1647 		 bfd *output_bfd,
   1648 		 char **error_message ATTRIBUTE_UNUSED)
   1649 {
   1650   UN_IMPL ("TOCREL16");
   1651   DUMP_RELOC ("TOCREL16",reloc_entry);
   1652 
   1653   if (output_bfd == (bfd *) NULL)
   1654     return bfd_reloc_continue;
   1655 
   1656   return bfd_reloc_ok;
   1657 }
   1658 
   1659 static bfd_reloc_status_type
   1660 ppc_secrel_reloc (bfd *abfd ATTRIBUTE_UNUSED,
   1661 		  arelent *reloc_entry ATTRIBUTE_UNUSED,
   1662 		  asymbol *symbol ATTRIBUTE_UNUSED,
   1663 		  void * data ATTRIBUTE_UNUSED,
   1664 		  asection *input_section ATTRIBUTE_UNUSED,
   1665 		  bfd *output_bfd,
   1666 		  char **error_message ATTRIBUTE_UNUSED)
   1667 {
   1668   UN_IMPL("SECREL");
   1669   DUMP_RELOC("SECREL",reloc_entry);
   1670 
   1671   if (output_bfd == (bfd *) NULL)
   1672     return bfd_reloc_continue;
   1673 
   1674   return bfd_reloc_ok;
   1675 }
   1676 
   1677 static bfd_reloc_status_type
   1678 ppc_section_reloc (bfd *abfd ATTRIBUTE_UNUSED,
   1679 		   arelent *reloc_entry ATTRIBUTE_UNUSED,
   1680 		   asymbol *symbol ATTRIBUTE_UNUSED,
   1681 		   void * data ATTRIBUTE_UNUSED,
   1682 		   asection *input_section ATTRIBUTE_UNUSED,
   1683 		   bfd *output_bfd,
   1684 		   char **error_message ATTRIBUTE_UNUSED)
   1685 {
   1686   UN_IMPL("SECTION");
   1687   DUMP_RELOC("SECTION",reloc_entry);
   1688 
   1689   if (output_bfd == (bfd *) NULL)
   1690     return bfd_reloc_continue;
   1691 
   1692   return bfd_reloc_ok;
   1693 }
   1694 
   1695 static bfd_reloc_status_type
   1696 ppc_imglue_reloc (bfd *abfd ATTRIBUTE_UNUSED,
   1697 		  arelent *reloc_entry ATTRIBUTE_UNUSED,
   1698 		  asymbol *symbol ATTRIBUTE_UNUSED,
   1699 		  void * data ATTRIBUTE_UNUSED,
   1700 		  asection *input_section ATTRIBUTE_UNUSED,
   1701 		  bfd *output_bfd,
   1702 		  char **error_message ATTRIBUTE_UNUSED)
   1703 
   1704 {
   1705   UN_IMPL("IMGLUE");
   1706   DUMP_RELOC("IMGLUE",reloc_entry);
   1707 
   1708   if (output_bfd == (bfd *) NULL)
   1709     return bfd_reloc_continue;
   1710 
   1711   return bfd_reloc_ok;
   1712 }
   1713 
   1714 #define MAX_RELOC_INDEX  \
   1716       (sizeof (ppc_coff_howto_table) / sizeof (ppc_coff_howto_table[0]) - 1)
   1717 
   1718 /* FIXME: There is a possibility that when we read in a reloc from a file,
   1719           that there are some bits encoded in the upper portion of the
   1720 	  type field. Not yet implemented.  */
   1721 
   1722 static void
   1723 ppc_coff_rtype2howto (arelent *relent, struct internal_reloc *internal)
   1724 {
   1725   /* We can encode one of three things in the type field, aside from the
   1726      type:
   1727      1. IMAGE_REL_PPC_NEG - indicates the value field is a subtraction
   1728         value, rather than an addition value
   1729      2. IMAGE_REL_PPC_BRTAKEN, IMAGE_REL_PPC_BRNTAKEN - indicates that
   1730         the branch is expected to be taken or not.
   1731      3. IMAGE_REL_PPC_TOCDEFN - toc slot definition in the file
   1732      For now, we just strip this stuff to find the type, and ignore it other
   1733      than that.  */
   1734   reloc_howto_type *howto;
   1735   unsigned short r_type  = EXTRACT_TYPE (internal->r_type);
   1736   unsigned short r_flags = EXTRACT_FLAGS(internal->r_type);
   1737   unsigned short junk    = EXTRACT_JUNK (internal->r_type);
   1738 
   1739   /* The masking process only slices off the bottom byte for r_type.  */
   1740   if ( r_type > MAX_RELOC_INDEX )
   1741     abort ();
   1742 
   1743   /* Check for absolute crap.  */
   1744   if (junk != 0)
   1745     abort ();
   1746 
   1747   switch(r_type)
   1748     {
   1749     case IMAGE_REL_PPC_ADDR16:
   1750     case IMAGE_REL_PPC_REL24:
   1751     case IMAGE_REL_PPC_ADDR24:
   1752     case IMAGE_REL_PPC_ADDR32:
   1753     case IMAGE_REL_PPC_IFGLUE:
   1754     case IMAGE_REL_PPC_ADDR32NB:
   1755     case IMAGE_REL_PPC_SECTION:
   1756     case IMAGE_REL_PPC_SECREL:
   1757       DUMP_RELOC2 (ppc_coff_howto_table[r_type].name, internal);
   1758       howto = ppc_coff_howto_table + r_type;
   1759       break;
   1760     case IMAGE_REL_PPC_IMGLUE:
   1761       DUMP_RELOC2 (ppc_coff_howto_table[r_type].name, internal);
   1762       howto = ppc_coff_howto_table + r_type;
   1763       break;
   1764     case IMAGE_REL_PPC_TOCREL16:
   1765       DUMP_RELOC2 (ppc_coff_howto_table[r_type].name, internal);
   1766       if (r_flags & IMAGE_REL_PPC_TOCDEFN)
   1767 	howto = ppc_coff_howto_table + IMAGE_REL_PPC_TOCREL16_DEFN;
   1768       else
   1769 	howto = ppc_coff_howto_table + IMAGE_REL_PPC_TOCREL16;
   1770       break;
   1771     default:
   1772       (*_bfd_error_handler) (_("warning: unsupported reloc %s [%d] used -- it may not work"),
   1773 			     ppc_coff_howto_table[r_type].name,
   1774 			     r_type);
   1775       howto = ppc_coff_howto_table + r_type;
   1776       break;
   1777     }
   1778 
   1779   relent->howto = howto;
   1780 }
   1781 
   1782 static reloc_howto_type *
   1783 coff_ppc_rtype_to_howto (bfd *abfd ATTRIBUTE_UNUSED,
   1784 			 asection *sec,
   1785 			 struct internal_reloc *rel,
   1786 			 struct coff_link_hash_entry *h ATTRIBUTE_UNUSED,
   1787 			 struct internal_syment *sym ATTRIBUTE_UNUSED,
   1788 			 bfd_vma *addendp)
   1789 {
   1790   reloc_howto_type *howto;
   1791 
   1792   /* We can encode one of three things in the type field, aside from the
   1793      type:
   1794      1. IMAGE_REL_PPC_NEG - indicates the value field is a subtraction
   1795         value, rather than an addition value
   1796      2. IMAGE_REL_PPC_BRTAKEN, IMAGE_REL_PPC_BRNTAKEN - indicates that
   1797         the branch is expected to be taken or not.
   1798      3. IMAGE_REL_PPC_TOCDEFN - toc slot definition in the file
   1799      For now, we just strip this stuff to find the type, and ignore it other
   1800      than that.  */
   1801 
   1802   unsigned short r_type  = EXTRACT_TYPE  (rel->r_type);
   1803   unsigned short r_flags = EXTRACT_FLAGS (rel->r_type);
   1804   unsigned short junk    = EXTRACT_JUNK  (rel->r_type);
   1805 
   1806   /* The masking process only slices off the bottom byte for r_type.  */
   1807   if (r_type > MAX_RELOC_INDEX)
   1808     abort ();
   1809 
   1810   /* Check for absolute crap.  */
   1811   if (junk != 0)
   1812     abort ();
   1813 
   1814   switch(r_type)
   1815     {
   1816     case IMAGE_REL_PPC_ADDR32NB:
   1817       DUMP_RELOC2(ppc_coff_howto_table[r_type].name, rel);
   1818       *addendp -= pe_data(sec->output_section->owner)->pe_opthdr.ImageBase;
   1819       howto = ppc_coff_howto_table + r_type;
   1820       break;
   1821     case IMAGE_REL_PPC_TOCREL16:
   1822       DUMP_RELOC2(ppc_coff_howto_table[r_type].name, rel);
   1823       if (r_flags & IMAGE_REL_PPC_TOCDEFN)
   1824 	howto = ppc_coff_howto_table + IMAGE_REL_PPC_TOCREL16_DEFN;
   1825       else
   1826 	howto = ppc_coff_howto_table + IMAGE_REL_PPC_TOCREL16;
   1827       break;
   1828     case IMAGE_REL_PPC_ADDR16:
   1829     case IMAGE_REL_PPC_REL24:
   1830     case IMAGE_REL_PPC_ADDR24:
   1831     case IMAGE_REL_PPC_ADDR32:
   1832     case IMAGE_REL_PPC_IFGLUE:
   1833     case IMAGE_REL_PPC_SECTION:
   1834     case IMAGE_REL_PPC_SECREL:
   1835       DUMP_RELOC2(ppc_coff_howto_table[r_type].name, rel);
   1836       howto = ppc_coff_howto_table + r_type;
   1837       break;
   1838     case IMAGE_REL_PPC_IMGLUE:
   1839       DUMP_RELOC2(ppc_coff_howto_table[r_type].name, rel);
   1840       howto = ppc_coff_howto_table + r_type;
   1841       break;
   1842     default:
   1843       (*_bfd_error_handler) (_("warning: unsupported reloc %s [%d] used -- it may not work"),
   1844 			     ppc_coff_howto_table[r_type].name,
   1845 			     r_type);
   1846       howto = ppc_coff_howto_table + r_type;
   1847       break;
   1848     }
   1849 
   1850   return howto;
   1851 }
   1852 
   1853 /* A cheesy little macro to make the code a little more readable.  */
   1854 #define HOW2MAP(bfd_rtype,ppc_rtype)  \
   1855  case bfd_rtype: return &ppc_coff_howto_table[ppc_rtype]
   1856 
   1857 static reloc_howto_type *
   1858 ppc_coff_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
   1859 			    bfd_reloc_code_real_type code)
   1860 {
   1861   switch (code)
   1862     {
   1863       HOW2MAP(BFD_RELOC_32_GOTOFF,    IMAGE_REL_PPC_IMGLUE);
   1864       HOW2MAP(BFD_RELOC_16_GOT_PCREL, IMAGE_REL_PPC_IFGLUE);
   1865       HOW2MAP(BFD_RELOC_16,           IMAGE_REL_PPC_ADDR16);
   1866       HOW2MAP(BFD_RELOC_PPC_B26,      IMAGE_REL_PPC_REL24);
   1867       HOW2MAP(BFD_RELOC_PPC_BA26,     IMAGE_REL_PPC_ADDR24);
   1868       HOW2MAP(BFD_RELOC_PPC_TOC16,    IMAGE_REL_PPC_TOCREL16);
   1869       HOW2MAP(BFD_RELOC_16_GOTOFF,    IMAGE_REL_PPC_TOCREL16_DEFN);
   1870       HOW2MAP(BFD_RELOC_32,           IMAGE_REL_PPC_ADDR32);
   1871       HOW2MAP(BFD_RELOC_RVA,          IMAGE_REL_PPC_ADDR32NB);
   1872     default:
   1873       return NULL;
   1874     }
   1875 }
   1876 #undef HOW2MAP
   1877 
   1878 static reloc_howto_type *
   1879 ppc_coff_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
   1880 			    const char *r_name)
   1881 {
   1882   unsigned int i;
   1883 
   1884   for (i = 0;
   1885        i < sizeof (ppc_coff_howto_table) / sizeof (ppc_coff_howto_table[0]);
   1886        i++)
   1887     if (ppc_coff_howto_table[i].name != NULL
   1888 	&& strcasecmp (ppc_coff_howto_table[i].name, r_name) == 0)
   1889       return &ppc_coff_howto_table[i];
   1890 
   1891   return NULL;
   1892 }
   1893 
   1894 /* Tailor coffcode.h -- macro heaven.  */
   1896 
   1897 #define RTYPE2HOWTO(cache_ptr, dst)  ppc_coff_rtype2howto (cache_ptr, dst)
   1898 
   1899 /* We use the special COFF backend linker, with our own special touch.  */
   1900 
   1901 #define coff_bfd_reloc_type_lookup   ppc_coff_reloc_type_lookup
   1902 #define coff_bfd_reloc_name_lookup ppc_coff_reloc_name_lookup
   1903 #define coff_rtype_to_howto          coff_ppc_rtype_to_howto
   1904 #define coff_relocate_section        coff_ppc_relocate_section
   1905 #define coff_bfd_final_link          ppc_bfd_coff_final_link
   1906 
   1907 #ifndef COFF_IMAGE_WITH_PE
   1908 #endif
   1909 
   1910 #define SELECT_RELOC(internal, howto) {internal.r_type=howto->type;}
   1911 
   1912 #define COFF_PAGE_SIZE                       0x1000
   1913 
   1914 /* FIXME: This controls some code that used to be in peicode.h and is
   1915    now in peigen.c.  It will not control the code in peigen.c.  If
   1916    anybody wants to get this working, you will need to fix that.  */
   1917 #define POWERPC_LE_PE
   1918 
   1919 #define COFF_SECTION_ALIGNMENT_ENTRIES \
   1920 { COFF_SECTION_NAME_EXACT_MATCH (".idata$2"), \
   1921   COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 0 }, \
   1922 { COFF_SECTION_NAME_EXACT_MATCH (".idata$3"), \
   1923   COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 0 }, \
   1924 { COFF_SECTION_NAME_EXACT_MATCH (".idata$4"), \
   1925   COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 2 }, \
   1926 { COFF_SECTION_NAME_EXACT_MATCH (".idata$5"), \
   1927   COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 2 }, \
   1928 { COFF_SECTION_NAME_EXACT_MATCH (".idata$6"), \
   1929   COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 1 }, \
   1930 { COFF_SECTION_NAME_EXACT_MATCH (".reloc"), \
   1931   COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 1 }
   1932 
   1933 #include "coffcode.h"
   1934 
   1935 #ifndef COFF_IMAGE_WITH_PE
   1937 
   1938 static bfd_boolean
   1939 ppc_do_last (bfd *abfd)
   1940 {
   1941   if (abfd == bfd_of_toc_owner)
   1942     return TRUE;
   1943   else
   1944     return FALSE;
   1945 }
   1946 
   1947 static bfd *
   1948 ppc_get_last (void)
   1949 {
   1950   return bfd_of_toc_owner;
   1951 }
   1952 
   1953 /* This piece of machinery exists only to guarantee that the bfd that holds
   1954    the toc section is written last.
   1955 
   1956    This does depend on bfd_make_section attaching a new section to the
   1957    end of the section list for the bfd.
   1958 
   1959    This is otherwise intended to be functionally the same as
   1960    cofflink.c:_bfd_coff_final_link(). It is specifically different only
   1961    where the POWERPC_LE_PE macro modifies the code. It is left in as a
   1962    precise form of comment. krk (at) cygnus.com  */
   1963 
   1964 /* Do the final link step.  */
   1965 
   1966 bfd_boolean
   1967 ppc_bfd_coff_final_link (bfd *abfd, struct bfd_link_info *info)
   1968 {
   1969   bfd_size_type symesz;
   1970   struct coff_final_link_info flaginfo;
   1971   bfd_boolean debug_merge_allocated;
   1972   asection *o;
   1973   struct bfd_link_order *p;
   1974   bfd_size_type max_sym_count;
   1975   bfd_size_type max_lineno_count;
   1976   bfd_size_type max_reloc_count;
   1977   bfd_size_type max_output_reloc_count;
   1978   bfd_size_type max_contents_size;
   1979   file_ptr rel_filepos;
   1980   unsigned int relsz;
   1981   file_ptr line_filepos;
   1982   unsigned int linesz;
   1983   bfd *sub;
   1984   bfd_byte *external_relocs = NULL;
   1985   char strbuf[STRING_SIZE_SIZE];
   1986   bfd_size_type amt;
   1987 
   1988   symesz = bfd_coff_symesz (abfd);
   1989 
   1990   flaginfo.info = info;
   1991   flaginfo.output_bfd = abfd;
   1992   flaginfo.strtab = NULL;
   1993   flaginfo.section_info = NULL;
   1994   flaginfo.last_file_index = -1;
   1995   flaginfo.last_bf_index = -1;
   1996   flaginfo.internal_syms = NULL;
   1997   flaginfo.sec_ptrs = NULL;
   1998   flaginfo.sym_indices = NULL;
   1999   flaginfo.outsyms = NULL;
   2000   flaginfo.linenos = NULL;
   2001   flaginfo.contents = NULL;
   2002   flaginfo.external_relocs = NULL;
   2003   flaginfo.internal_relocs = NULL;
   2004   debug_merge_allocated = FALSE;
   2005 
   2006   coff_data (abfd)->link_info = info;
   2007 
   2008   flaginfo.strtab = _bfd_stringtab_init ();
   2009   if (flaginfo.strtab == NULL)
   2010     goto error_return;
   2011 
   2012   if (! coff_debug_merge_hash_table_init (&flaginfo.debug_merge))
   2013     goto error_return;
   2014   debug_merge_allocated = TRUE;
   2015 
   2016   /* Compute the file positions for all the sections.  */
   2017   if (! abfd->output_has_begun)
   2018     {
   2019       if (! bfd_coff_compute_section_file_positions (abfd))
   2020 	return FALSE;
   2021     }
   2022 
   2023   /* Count the line numbers and relocation entries required for the
   2024      output file.  Set the file positions for the relocs.  */
   2025   rel_filepos = obj_relocbase (abfd);
   2026   relsz = bfd_coff_relsz (abfd);
   2027   max_contents_size = 0;
   2028   max_lineno_count = 0;
   2029   max_reloc_count = 0;
   2030 
   2031   for (o = abfd->sections; o != NULL; o = o->next)
   2032     {
   2033       o->reloc_count = 0;
   2034       o->lineno_count = 0;
   2035 
   2036       for (p = o->map_head.link_order; p != NULL; p = p->next)
   2037 	{
   2038 	  if (p->type == bfd_indirect_link_order)
   2039 	    {
   2040 	      asection *sec;
   2041 
   2042 	      sec = p->u.indirect.section;
   2043 
   2044 	      /* Mark all sections which are to be included in the
   2045 		 link.  This will normally be every section.  We need
   2046 		 to do this so that we can identify any sections which
   2047 		 the linker has decided to not include.  */
   2048 	      sec->linker_mark = TRUE;
   2049 
   2050 	      if (info->strip == strip_none
   2051 		  || info->strip == strip_some)
   2052 		o->lineno_count += sec->lineno_count;
   2053 
   2054 	      if (info->relocatable)
   2055 		o->reloc_count += sec->reloc_count;
   2056 
   2057 	      if (sec->rawsize > max_contents_size)
   2058 		max_contents_size = sec->rawsize;
   2059 	      if (sec->size > max_contents_size)
   2060 		max_contents_size = sec->size;
   2061 	      if (sec->lineno_count > max_lineno_count)
   2062 		max_lineno_count = sec->lineno_count;
   2063 	      if (sec->reloc_count > max_reloc_count)
   2064 		max_reloc_count = sec->reloc_count;
   2065 	    }
   2066 	  else if (info->relocatable
   2067 		   && (p->type == bfd_section_reloc_link_order
   2068 		       || p->type == bfd_symbol_reloc_link_order))
   2069 	    ++o->reloc_count;
   2070 	}
   2071       if (o->reloc_count == 0)
   2072 	o->rel_filepos = 0;
   2073       else
   2074 	{
   2075 	  o->flags |= SEC_RELOC;
   2076 	  o->rel_filepos = rel_filepos;
   2077 	  rel_filepos += o->reloc_count * relsz;
   2078 	}
   2079     }
   2080 
   2081   /* If doing a relocatable link, allocate space for the pointers we
   2082      need to keep.  */
   2083   if (info->relocatable)
   2084     {
   2085       unsigned int i;
   2086 
   2087       /* We use section_count + 1, rather than section_count, because
   2088          the target_index fields are 1 based.  */
   2089       amt = abfd->section_count + 1;
   2090       amt *= sizeof (struct coff_link_section_info);
   2091       flaginfo.section_info = (struct coff_link_section_info *) bfd_malloc (amt);
   2092 
   2093       if (flaginfo.section_info == NULL)
   2094 	goto error_return;
   2095 
   2096       for (i = 0; i <= abfd->section_count; i++)
   2097 	{
   2098 	  flaginfo.section_info[i].relocs = NULL;
   2099 	  flaginfo.section_info[i].rel_hashes = NULL;
   2100 	}
   2101     }
   2102 
   2103   /* We now know the size of the relocs, so we can determine the file
   2104      positions of the line numbers.  */
   2105   line_filepos = rel_filepos;
   2106   linesz = bfd_coff_linesz (abfd);
   2107   max_output_reloc_count = 0;
   2108 
   2109   for (o = abfd->sections; o != NULL; o = o->next)
   2110     {
   2111       if (o->lineno_count == 0)
   2112 	o->line_filepos = 0;
   2113       else
   2114 	{
   2115 	  o->line_filepos = line_filepos;
   2116 	  line_filepos += o->lineno_count * linesz;
   2117 	}
   2118 
   2119       if (o->reloc_count != 0)
   2120 	{
   2121 	  /* We don't know the indices of global symbols until we have
   2122              written out all the local symbols.  For each section in
   2123              the output file, we keep an array of pointers to hash
   2124              table entries.  Each entry in the array corresponds to a
   2125              reloc.  When we find a reloc against a global symbol, we
   2126              set the corresponding entry in this array so that we can
   2127              fix up the symbol index after we have written out all the
   2128              local symbols.
   2129 
   2130 	     Because of this problem, we also keep the relocs in
   2131 	     memory until the end of the link.  This wastes memory,
   2132 	     but only when doing a relocatable link, which is not the
   2133 	     common case.  */
   2134 	  BFD_ASSERT (info->relocatable);
   2135 	  amt = o->reloc_count;
   2136 	  amt *= sizeof (struct internal_reloc);
   2137 	  flaginfo.section_info[o->target_index].relocs =
   2138 	    (struct internal_reloc *) bfd_malloc (amt);
   2139 	  amt = o->reloc_count;
   2140 	  amt *= sizeof (struct coff_link_hash_entry *);
   2141 	  flaginfo.section_info[o->target_index].rel_hashes =
   2142 	    (struct coff_link_hash_entry **) bfd_malloc (amt);
   2143 	  if (flaginfo.section_info[o->target_index].relocs == NULL
   2144 	      || flaginfo.section_info[o->target_index].rel_hashes == NULL)
   2145 	    goto error_return;
   2146 
   2147 	  if (o->reloc_count > max_output_reloc_count)
   2148 	    max_output_reloc_count = o->reloc_count;
   2149 	}
   2150 
   2151       /* Reset the reloc and lineno counts, so that we can use them to
   2152 	 count the number of entries we have output so far.  */
   2153       o->reloc_count = 0;
   2154       o->lineno_count = 0;
   2155     }
   2156 
   2157   obj_sym_filepos (abfd) = line_filepos;
   2158 
   2159   /* Figure out the largest number of symbols in an input BFD.  Take
   2160      the opportunity to clear the output_has_begun fields of all the
   2161      input BFD's.  */
   2162   max_sym_count = 0;
   2163   for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
   2164     {
   2165       bfd_size_type sz;
   2166 
   2167       sub->output_has_begun = FALSE;
   2168       sz = obj_raw_syment_count (sub);
   2169       if (sz > max_sym_count)
   2170 	max_sym_count = sz;
   2171     }
   2172 
   2173   /* Allocate some buffers used while linking.  */
   2174   amt = max_sym_count * sizeof (struct internal_syment);
   2175   flaginfo.internal_syms = (struct internal_syment *) bfd_malloc (amt);
   2176   amt = max_sym_count * sizeof (asection *);
   2177   flaginfo.sec_ptrs = (asection **) bfd_malloc (amt);
   2178   amt = max_sym_count * sizeof (long);
   2179   flaginfo.sym_indices = (long *) bfd_malloc (amt);
   2180   amt = (max_sym_count + 1) * symesz;
   2181   flaginfo.outsyms = (bfd_byte *) bfd_malloc (amt);
   2182   amt = max_lineno_count * bfd_coff_linesz (abfd);
   2183   flaginfo.linenos = (bfd_byte *) bfd_malloc (amt);
   2184   flaginfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
   2185   flaginfo.external_relocs = (bfd_byte *) bfd_malloc (max_reloc_count * relsz);
   2186   if (! info->relocatable)
   2187     {
   2188       amt = max_reloc_count * sizeof (struct internal_reloc);
   2189       flaginfo.internal_relocs = (struct internal_reloc *) bfd_malloc (amt);
   2190     }
   2191   if ((flaginfo.internal_syms == NULL && max_sym_count > 0)
   2192       || (flaginfo.sec_ptrs == NULL && max_sym_count > 0)
   2193       || (flaginfo.sym_indices == NULL && max_sym_count > 0)
   2194       || flaginfo.outsyms == NULL
   2195       || (flaginfo.linenos == NULL && max_lineno_count > 0)
   2196       || (flaginfo.contents == NULL && max_contents_size > 0)
   2197       || (flaginfo.external_relocs == NULL && max_reloc_count > 0)
   2198       || (! info->relocatable
   2199 	  && flaginfo.internal_relocs == NULL
   2200 	  && max_reloc_count > 0))
   2201     goto error_return;
   2202 
   2203   /* We now know the position of everything in the file, except that
   2204      we don't know the size of the symbol table and therefore we don't
   2205      know where the string table starts.  We just build the string
   2206      table in memory as we go along.  We process all the relocations
   2207      for a single input file at once.  */
   2208   obj_raw_syment_count (abfd) = 0;
   2209 
   2210   if (coff_backend_info (abfd)->_bfd_coff_start_final_link)
   2211     {
   2212       if (! bfd_coff_start_final_link (abfd, info))
   2213 	goto error_return;
   2214     }
   2215 
   2216   for (o = abfd->sections; o != NULL; o = o->next)
   2217     {
   2218       for (p = o->map_head.link_order; p != NULL; p = p->next)
   2219 	{
   2220 	  if (p->type == bfd_indirect_link_order
   2221 	      && (bfd_get_flavour (p->u.indirect.section->owner)
   2222 		  == bfd_target_coff_flavour))
   2223 	    {
   2224 	      sub = p->u.indirect.section->owner;
   2225 #ifdef POWERPC_LE_PE
   2226 	      if (! sub->output_has_begun && !ppc_do_last(sub))
   2227 #else
   2228 	      if (! sub->output_has_begun)
   2229 #endif
   2230 		{
   2231 		  if (! _bfd_coff_link_input_bfd (&flaginfo, sub))
   2232 		    goto error_return;
   2233 		  sub->output_has_begun = TRUE;
   2234 		}
   2235 	    }
   2236 	  else if (p->type == bfd_section_reloc_link_order
   2237 		   || p->type == bfd_symbol_reloc_link_order)
   2238 	    {
   2239 	      if (! _bfd_coff_reloc_link_order (abfd, &flaginfo, o, p))
   2240 		goto error_return;
   2241 	    }
   2242 	  else
   2243 	    {
   2244 	      if (! _bfd_default_link_order (abfd, info, o, p))
   2245 		goto error_return;
   2246 	    }
   2247 	}
   2248     }
   2249 
   2250 #ifdef POWERPC_LE_PE
   2251   {
   2252     bfd* last_one = ppc_get_last();
   2253     if (last_one)
   2254       {
   2255 	if (! _bfd_coff_link_input_bfd (&flaginfo, last_one))
   2256 	  goto error_return;
   2257       }
   2258     last_one->output_has_begun = TRUE;
   2259   }
   2260 #endif
   2261 
   2262   /* Free up the buffers used by _bfd_coff_link_input_bfd.  */
   2263   coff_debug_merge_hash_table_free (&flaginfo.debug_merge);
   2264   debug_merge_allocated = FALSE;
   2265 
   2266   if (flaginfo.internal_syms != NULL)
   2267     {
   2268       free (flaginfo.internal_syms);
   2269       flaginfo.internal_syms = NULL;
   2270     }
   2271   if (flaginfo.sec_ptrs != NULL)
   2272     {
   2273       free (flaginfo.sec_ptrs);
   2274       flaginfo.sec_ptrs = NULL;
   2275     }
   2276   if (flaginfo.sym_indices != NULL)
   2277     {
   2278       free (flaginfo.sym_indices);
   2279       flaginfo.sym_indices = NULL;
   2280     }
   2281   if (flaginfo.linenos != NULL)
   2282     {
   2283       free (flaginfo.linenos);
   2284       flaginfo.linenos = NULL;
   2285     }
   2286   if (flaginfo.contents != NULL)
   2287     {
   2288       free (flaginfo.contents);
   2289       flaginfo.contents = NULL;
   2290     }
   2291   if (flaginfo.external_relocs != NULL)
   2292     {
   2293       free (flaginfo.external_relocs);
   2294       flaginfo.external_relocs = NULL;
   2295     }
   2296   if (flaginfo.internal_relocs != NULL)
   2297     {
   2298       free (flaginfo.internal_relocs);
   2299       flaginfo.internal_relocs = NULL;
   2300     }
   2301 
   2302   /* The value of the last C_FILE symbol is supposed to be the symbol
   2303      index of the first external symbol.  Write it out again if
   2304      necessary.  */
   2305   if (flaginfo.last_file_index != -1
   2306       && (unsigned int) flaginfo.last_file.n_value != obj_raw_syment_count (abfd))
   2307     {
   2308       file_ptr pos;
   2309 
   2310       flaginfo.last_file.n_value = obj_raw_syment_count (abfd);
   2311       bfd_coff_swap_sym_out (abfd, &flaginfo.last_file,
   2312 			     flaginfo.outsyms);
   2313       pos = obj_sym_filepos (abfd) + flaginfo.last_file_index * symesz;
   2314       if (bfd_seek (abfd, pos, SEEK_SET) != 0
   2315 	  || bfd_bwrite (flaginfo.outsyms, symesz, abfd) != symesz)
   2316 	return FALSE;
   2317     }
   2318 
   2319   /* Write out the global symbols.  */
   2320   flaginfo.failed = FALSE;
   2321   bfd_hash_traverse (&info->hash->table, _bfd_coff_write_global_sym, &flaginfo);
   2322   if (flaginfo.failed)
   2323     goto error_return;
   2324 
   2325   /* The outsyms buffer is used by _bfd_coff_write_global_sym.  */
   2326   if (flaginfo.outsyms != NULL)
   2327     {
   2328       free (flaginfo.outsyms);
   2329       flaginfo.outsyms = NULL;
   2330     }
   2331 
   2332   if (info->relocatable)
   2333     {
   2334       /* Now that we have written out all the global symbols, we know
   2335 	 the symbol indices to use for relocs against them, and we can
   2336 	 finally write out the relocs.  */
   2337       amt = max_output_reloc_count * relsz;
   2338       external_relocs = (bfd_byte *) bfd_malloc (amt);
   2339       if (external_relocs == NULL)
   2340 	goto error_return;
   2341 
   2342       for (o = abfd->sections; o != NULL; o = o->next)
   2343 	{
   2344 	  struct internal_reloc *irel;
   2345 	  struct internal_reloc *irelend;
   2346 	  struct coff_link_hash_entry **rel_hash;
   2347 	  bfd_byte *erel;
   2348 
   2349 	  if (o->reloc_count == 0)
   2350 	    continue;
   2351 
   2352 	  irel = flaginfo.section_info[o->target_index].relocs;
   2353 	  irelend = irel + o->reloc_count;
   2354 	  rel_hash = flaginfo.section_info[o->target_index].rel_hashes;
   2355 	  erel = external_relocs;
   2356 	  for (; irel < irelend; irel++, rel_hash++, erel += relsz)
   2357 	    {
   2358 	      if (*rel_hash != NULL)
   2359 		{
   2360 		  BFD_ASSERT ((*rel_hash)->indx >= 0);
   2361 		  irel->r_symndx = (*rel_hash)->indx;
   2362 		}
   2363 	      bfd_coff_swap_reloc_out (abfd, irel, erel);
   2364 	    }
   2365 
   2366 	  amt = relsz * o->reloc_count;
   2367 	  if (bfd_seek (abfd, o->rel_filepos, SEEK_SET) != 0
   2368 	      || bfd_bwrite (external_relocs, amt, abfd) != amt)
   2369 	    goto error_return;
   2370 	}
   2371 
   2372       free (external_relocs);
   2373       external_relocs = NULL;
   2374     }
   2375 
   2376   /* Free up the section information.  */
   2377   if (flaginfo.section_info != NULL)
   2378     {
   2379       unsigned int i;
   2380 
   2381       for (i = 0; i < abfd->section_count; i++)
   2382 	{
   2383 	  if (flaginfo.section_info[i].relocs != NULL)
   2384 	    free (flaginfo.section_info[i].relocs);
   2385 	  if (flaginfo.section_info[i].rel_hashes != NULL)
   2386 	    free (flaginfo.section_info[i].rel_hashes);
   2387 	}
   2388       free (flaginfo.section_info);
   2389       flaginfo.section_info = NULL;
   2390     }
   2391 
   2392   /* If we have optimized stabs strings, output them.  */
   2393   if (coff_hash_table (info)->stab_info.stabstr != NULL)
   2394     {
   2395       if (! _bfd_write_stab_strings (abfd, &coff_hash_table (info)->stab_info))
   2396 	return FALSE;
   2397     }
   2398 
   2399   /* Write out the string table.  */
   2400   if (obj_raw_syment_count (abfd) != 0)
   2401     {
   2402       file_ptr pos;
   2403 
   2404       pos = obj_sym_filepos (abfd) + obj_raw_syment_count (abfd) * symesz;
   2405       if (bfd_seek (abfd, pos, SEEK_SET) != 0)
   2406 	return FALSE;
   2407 
   2408 #if STRING_SIZE_SIZE == 4
   2409       H_PUT_32 (abfd,
   2410 		_bfd_stringtab_size (flaginfo.strtab) + STRING_SIZE_SIZE,
   2411 		strbuf);
   2412 #else
   2413  #error Change H_PUT_32 above
   2414 #endif
   2415 
   2416       if (bfd_bwrite (strbuf, (bfd_size_type) STRING_SIZE_SIZE, abfd)
   2417 	  != STRING_SIZE_SIZE)
   2418 	return FALSE;
   2419 
   2420       if (! _bfd_stringtab_emit (abfd, flaginfo.strtab))
   2421 	return FALSE;
   2422     }
   2423 
   2424   _bfd_stringtab_free (flaginfo.strtab);
   2425 
   2426   /* Setting bfd_get_symcount to 0 will cause write_object_contents to
   2427      not try to write out the symbols.  */
   2428   bfd_get_symcount (abfd) = 0;
   2429 
   2430   return TRUE;
   2431 
   2432  error_return:
   2433   if (debug_merge_allocated)
   2434     coff_debug_merge_hash_table_free (&flaginfo.debug_merge);
   2435   if (flaginfo.strtab != NULL)
   2436     _bfd_stringtab_free (flaginfo.strtab);
   2437   if (flaginfo.section_info != NULL)
   2438     {
   2439       unsigned int i;
   2440 
   2441       for (i = 0; i < abfd->section_count; i++)
   2442 	{
   2443 	  if (flaginfo.section_info[i].relocs != NULL)
   2444 	    free (flaginfo.section_info[i].relocs);
   2445 	  if (flaginfo.section_info[i].rel_hashes != NULL)
   2446 	    free (flaginfo.section_info[i].rel_hashes);
   2447 	}
   2448       free (flaginfo.section_info);
   2449     }
   2450   if (flaginfo.internal_syms != NULL)
   2451     free (flaginfo.internal_syms);
   2452   if (flaginfo.sec_ptrs != NULL)
   2453     free (flaginfo.sec_ptrs);
   2454   if (flaginfo.sym_indices != NULL)
   2455     free (flaginfo.sym_indices);
   2456   if (flaginfo.outsyms != NULL)
   2457     free (flaginfo.outsyms);
   2458   if (flaginfo.linenos != NULL)
   2459     free (flaginfo.linenos);
   2460   if (flaginfo.contents != NULL)
   2461     free (flaginfo.contents);
   2462   if (flaginfo.external_relocs != NULL)
   2463     free (flaginfo.external_relocs);
   2464   if (flaginfo.internal_relocs != NULL)
   2465     free (flaginfo.internal_relocs);
   2466   if (external_relocs != NULL)
   2467     free (external_relocs);
   2468   return FALSE;
   2469 }
   2470 #endif
   2471 
   2472 /* Forward declaration for use by alternative_target field.  */
   2474 #ifdef TARGET_BIG_SYM
   2475 extern const bfd_target TARGET_BIG_SYM;
   2476 #endif
   2477 
   2478 /* The transfer vectors that lead the outside world to all of the above.  */
   2479 
   2480 #ifdef TARGET_LITTLE_SYM
   2481 const bfd_target TARGET_LITTLE_SYM =
   2482 {
   2483   TARGET_LITTLE_NAME,		/* name or coff-arm-little */
   2484   bfd_target_coff_flavour,
   2485   BFD_ENDIAN_LITTLE,		/* data byte order is little */
   2486   BFD_ENDIAN_LITTLE,		/* header byte order is little */
   2487 
   2488   (HAS_RELOC | EXEC_P |		/* FIXME: object flags */
   2489    HAS_LINENO | HAS_DEBUG |
   2490    HAS_SYMS | HAS_LOCALS | WP_TEXT | D_PAGED),
   2491 
   2492 #ifndef COFF_WITH_PE
   2493   (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY
   2494    | SEC_RELOC),		/* section flags */
   2495 #else
   2496   (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY
   2497    | SEC_RELOC | SEC_LINK_ONCE | SEC_LINK_DUPLICATES),
   2498 #endif
   2499 
   2500   0,				/* leading char */
   2501   '/',				/* ar_pad_char */
   2502   15,				/* ar_max_namelen??? FIXMEmgo */
   2503   0,				/* match priority.  */
   2504 
   2505   bfd_getl64, bfd_getl_signed_64, bfd_putl64,
   2506   bfd_getl32, bfd_getl_signed_32, bfd_putl32,
   2507   bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* data */
   2508 
   2509   bfd_getl64, bfd_getl_signed_64, bfd_putl64,
   2510   bfd_getl32, bfd_getl_signed_32, bfd_putl32,
   2511   bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* hdrs */
   2512 
   2513   {_bfd_dummy_target, coff_object_p, 	/* bfd_check_format */
   2514      bfd_generic_archive_p, /* _bfd_dummy_target */ coff_object_p },
   2515   {bfd_false, coff_mkobject, _bfd_generic_mkarchive, /* bfd_set_format */
   2516      bfd_false},
   2517   {bfd_false, coff_write_object_contents,	/* bfd_write_contents */
   2518      _bfd_write_archive_contents, bfd_false},
   2519 
   2520   BFD_JUMP_TABLE_GENERIC (coff),
   2521   BFD_JUMP_TABLE_COPY (coff),
   2522   BFD_JUMP_TABLE_CORE (_bfd_nocore),
   2523   BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_coff),
   2524   BFD_JUMP_TABLE_SYMBOLS (coff),
   2525   BFD_JUMP_TABLE_RELOCS (coff),
   2526   BFD_JUMP_TABLE_WRITE (coff),
   2527   BFD_JUMP_TABLE_LINK (coff),
   2528   BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
   2529 
   2530   /* Alternative_target.  */
   2531 #ifdef TARGET_BIG_SYM
   2532   & TARGET_BIG_SYM,
   2533 #else
   2534   NULL,
   2535 #endif
   2536 
   2537   COFF_SWAP_TABLE
   2538 };
   2539 #endif
   2540 
   2541 #ifdef TARGET_BIG_SYM
   2542 const bfd_target TARGET_BIG_SYM =
   2543 {
   2544   TARGET_BIG_NAME,
   2545   bfd_target_coff_flavour,
   2546   BFD_ENDIAN_BIG,		/* data byte order is big */
   2547   BFD_ENDIAN_BIG,		/* header byte order is big */
   2548 
   2549   (HAS_RELOC | EXEC_P |		/* FIXME: object flags */
   2550    HAS_LINENO | HAS_DEBUG |
   2551    HAS_SYMS | HAS_LOCALS | WP_TEXT | D_PAGED),
   2552 
   2553 #ifndef COFF_WITH_PE
   2554   (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY
   2555    | SEC_RELOC),		/* section flags */
   2556 #else
   2557   (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY
   2558    | SEC_RELOC | SEC_LINK_ONCE | SEC_LINK_DUPLICATES),
   2559 #endif
   2560 
   2561   0,				/* leading char */
   2562   '/',				/* ar_pad_char */
   2563   15,				/* ar_max_namelen??? FIXMEmgo */
   2564   0,				/* match priority.  */
   2565 
   2566   bfd_getb64, bfd_getb_signed_64, bfd_putb64,
   2567   bfd_getb32, bfd_getb_signed_32, bfd_putb32,
   2568   bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* data */
   2569 
   2570   bfd_getb64, bfd_getb_signed_64, bfd_putb64,
   2571   bfd_getb32, bfd_getb_signed_32, bfd_putb32,
   2572   bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* hdrs */
   2573 
   2574   {_bfd_dummy_target, coff_object_p, 	/* bfd_check_format */
   2575      bfd_generic_archive_p, /* _bfd_dummy_target */ coff_object_p },
   2576   {bfd_false, coff_mkobject, _bfd_generic_mkarchive, /* bfd_set_format */
   2577      bfd_false},
   2578   {bfd_false, coff_write_object_contents,	/* bfd_write_contents */
   2579      _bfd_write_archive_contents, bfd_false},
   2580 
   2581   BFD_JUMP_TABLE_GENERIC (coff),
   2582   BFD_JUMP_TABLE_COPY (coff),
   2583   BFD_JUMP_TABLE_CORE (_bfd_nocore),
   2584   BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_coff),
   2585   BFD_JUMP_TABLE_SYMBOLS (coff),
   2586   BFD_JUMP_TABLE_RELOCS (coff),
   2587   BFD_JUMP_TABLE_WRITE (coff),
   2588   BFD_JUMP_TABLE_LINK (coff),
   2589   BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
   2590 
   2591   /* Alternative_target.  */
   2592 #ifdef TARGET_LITTLE_SYM
   2593   & TARGET_LITTLE_SYM,
   2594 #else
   2595   NULL,
   2596 #endif
   2597 
   2598   COFF_SWAP_TABLE
   2599 };
   2600 
   2601 #endif
   2602