Home | History | Annotate | Download | only in bfd
      1 /* BFD back-end for PowerPC Microsoft Portable Executable files.
      2    Copyright (C) 1990-2016 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 (bfd_link_relocatable (info))
    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 	    (*info->callbacks->undefined_symbol)
   1035 	      (info, h->root.root.root.string, input_bfd, input_section,
   1036 	       rel->r_vaddr - input_section->vma, TRUE);
   1037 	}
   1038 
   1039       rstat = bfd_reloc_ok;
   1040 
   1041       /* Each case must do its own relocation, setting rstat appropriately.  */
   1042       switch (r_type)
   1043 	{
   1044 	default:
   1045 	  (*_bfd_error_handler)
   1046 	    (_("%B: unsupported relocation type 0x%02x"), input_bfd, r_type);
   1047 	  bfd_set_error (bfd_error_bad_value);
   1048 	  return FALSE;
   1049 	case IMAGE_REL_PPC_TOCREL16:
   1050 	  {
   1051 	    bfd_signed_vma our_toc_offset;
   1052 	    int fixit;
   1053 
   1054 	    DUMP_RELOC2(howto->name, rel);
   1055 
   1056 	    if (toc_section == 0)
   1057 	      {
   1058 		toc_section = bfd_get_section_by_name (bfd_of_toc_owner,
   1059 						       TOC_SECTION_NAME);
   1060 
   1061 		if ( toc_section == NULL )
   1062 		  {
   1063 		    /* There is no toc section. Something is very wrong.  */
   1064 		    abort ();
   1065 		  }
   1066 	      }
   1067 
   1068 	    /* Amazing bit tricks present. As we may have seen earlier, we
   1069 	       use the 1 bit to tell us whether or not a toc offset has been
   1070 	       allocated. Now that they've all been allocated, we will use
   1071 	       the 1 bit to tell us if we've written this particular toc
   1072 	       entry out.  */
   1073 	    fixit = FALSE;
   1074 	    if (h == 0)
   1075 	      {
   1076 		/* It is a file local symbol.  */
   1077 		int *local_toc_table;
   1078 		char name[SYMNMLEN + 1];
   1079 
   1080 		sym = syms + symndx;
   1081 		strncpy (name, sym->_n._n_name, SYMNMLEN);
   1082 		name[SYMNMLEN] = '\0';
   1083 
   1084 		local_toc_table = obj_coff_local_toc_table(input_bfd);
   1085 		our_toc_offset = local_toc_table[symndx];
   1086 
   1087 		if (IS_WRITTEN(our_toc_offset))
   1088 		  {
   1089 		    /* If it has been written out, it is marked with the
   1090 		       1 bit. Fix up our offset, but do not write it out
   1091 		       again.  */
   1092 		    MAKE_ADDR_AGAIN(our_toc_offset);
   1093 		  }
   1094 		else
   1095 		  {
   1096 		    /* Write out the toc entry.  */
   1097 		    record_toc (toc_section, our_toc_offset, priv,
   1098 				strdup (name));
   1099 
   1100 		    bfd_put_32 (output_bfd, val,
   1101 			       toc_section->contents + our_toc_offset);
   1102 
   1103 		    MARK_AS_WRITTEN(local_toc_table[symndx]);
   1104 		    fixit = TRUE;
   1105 		  }
   1106 	      }
   1107 	    else
   1108 	      {
   1109 		const char *name = h->root.root.root.string;
   1110 		our_toc_offset = h->toc_offset;
   1111 
   1112 		if ((r_flags & IMAGE_REL_PPC_TOCDEFN)
   1113 		    == IMAGE_REL_PPC_TOCDEFN )
   1114 		  {
   1115 		    /* This is unbelievable cheese. Some knowledgable asm
   1116 		       hacker has decided to use r2 as a base for loading
   1117 		       a value. He/She does this by setting the tocdefn bit,
   1118 		       and not supplying a toc definition. The behaviour is
   1119 		       then to use the difference between the value of the
   1120 		       symbol and the actual location of the toc as the toc
   1121 		       index.
   1122 
   1123 		       In fact, what is usually happening is, because the
   1124 		       Import Address Table is mapped immediately following
   1125 		       the toc, some trippy library code trying for speed on
   1126 		       dll linkage, takes advantage of that and considers
   1127 		       the IAT to be part of the toc, thus saving a load.  */
   1128 
   1129 		    our_toc_offset = val - (toc_section->output_section->vma
   1130 					    + toc_section->output_offset);
   1131 
   1132 		    /* The size must still fit in a 16-bit displacement.  */
   1133 		    if ((bfd_vma) our_toc_offset >= 65535)
   1134 		      {
   1135 			(*_bfd_error_handler)
   1136 			  (_("%B: Relocation for %s of %lx exceeds Toc size limit"),
   1137 			   input_bfd, name,
   1138 			   (unsigned long) our_toc_offset);
   1139 			bfd_set_error (bfd_error_bad_value);
   1140 			return FALSE;
   1141 		      }
   1142 
   1143 		    record_toc (toc_section, our_toc_offset, pub,
   1144 				strdup (name));
   1145 		  }
   1146 		else if (IS_WRITTEN (our_toc_offset))
   1147 		  {
   1148 		    /* If it has been written out, it is marked with the
   1149 		       1 bit. Fix up our offset, but do not write it out
   1150 		       again.  */
   1151 		    MAKE_ADDR_AGAIN(our_toc_offset);
   1152 		  }
   1153 		else
   1154 		  {
   1155 		    record_toc(toc_section, our_toc_offset, pub,
   1156 			       strdup (name));
   1157 
   1158 		    /* Write out the toc entry.  */
   1159 		    bfd_put_32 (output_bfd, val,
   1160 			       toc_section->contents + our_toc_offset);
   1161 
   1162 		    MARK_AS_WRITTEN(h->toc_offset);
   1163 		    /* The tricky part is that this is the address that
   1164 		       needs a .reloc entry for it.  */
   1165 		    fixit = TRUE;
   1166 		  }
   1167 	      }
   1168 
   1169 	    if (fixit && info->base_file)
   1170 	      {
   1171 		/* So if this is non pcrelative, and is referenced
   1172 		   to a section or a common symbol, then it needs a reloc.  */
   1173 
   1174 		/* Relocation to a symbol in a section which
   1175 		   isn't absolute - we output the address here
   1176 		   to a file.  */
   1177 		bfd_vma addr = (toc_section->output_section->vma
   1178 				+ toc_section->output_offset + our_toc_offset);
   1179 
   1180 		if (!write_base_file_entry (output_bfd, info, addr))
   1181 		  return FALSE;
   1182 	      }
   1183 
   1184 	    /* FIXME: this test is conservative.  */
   1185 	    if ((r_flags & IMAGE_REL_PPC_TOCDEFN) != IMAGE_REL_PPC_TOCDEFN
   1186 		&& (bfd_vma) our_toc_offset > toc_section->size)
   1187 	      {
   1188 		(*_bfd_error_handler)
   1189 		  (_("%B: Relocation exceeds allocated TOC (%lx)"),
   1190 		   input_bfd, (unsigned long) toc_section->size);
   1191 		bfd_set_error (bfd_error_bad_value);
   1192 		return FALSE;
   1193 	      }
   1194 
   1195 	    /* Now we know the relocation for this toc reference.  */
   1196 	    relocation =  our_toc_offset + TOC_LOAD_ADJUSTMENT;
   1197 	    rstat = _bfd_relocate_contents (howto, input_bfd, relocation, loc);
   1198 	  }
   1199 	  break;
   1200 	case IMAGE_REL_PPC_IFGLUE:
   1201 	  {
   1202 	    /* To solve this, we need to know whether or not the symbol
   1203 	       appearing on the call instruction is a glue function or not.
   1204 	       A glue function must announce itself via a IMGLUE reloc, and
   1205 	       the reloc contains the required toc restore instruction.  */
   1206 	    DUMP_RELOC2 (howto->name, rel);
   1207 
   1208 	    if (h != 0)
   1209 	      {
   1210 		if (h->symbol_is_glue == 1)
   1211 		  {
   1212 		    bfd_put_32 (input_bfd, (bfd_vma) h->glue_insn, loc);
   1213 		  }
   1214 	      }
   1215 	  }
   1216 	  break;
   1217 	case IMAGE_REL_PPC_SECREL:
   1218 	  /* Unimplemented: codeview debugging information.  */
   1219 	  /* For fast access to the header of the section
   1220 	     containing the item.  */
   1221 	  break;
   1222 	case IMAGE_REL_PPC_SECTION:
   1223 	  /* Unimplemented: codeview debugging information.  */
   1224 	  /* Is used to indicate that the value should be relative
   1225 	     to the beginning of the section that contains the
   1226 	     symbol.  */
   1227 	  break;
   1228 	case IMAGE_REL_PPC_ABSOLUTE:
   1229 	  {
   1230 	    const char *my_name;
   1231 	    char buf[SYMNMLEN + 1];
   1232 
   1233 	    if (h == 0)
   1234 	      {
   1235 		strncpy (buf, (syms+symndx)->_n._n_name, SYMNMLEN);
   1236 		buf[SYMNMLEN] = '\0';
   1237 		my_name = buf;
   1238 	      }
   1239 	    else
   1240 	      my_name = h->root.root.root.string;
   1241 
   1242 	    (*_bfd_error_handler)
   1243 	      (_("Warning: unsupported reloc %s <file %B, section %A>\n"
   1244 		 "sym %ld (%s), r_vaddr %ld (%lx)"),
   1245 	       input_bfd, input_section, howto->name,
   1246 	       rel->r_symndx, my_name, (long) rel->r_vaddr,
   1247 	       (unsigned long) rel->r_vaddr);
   1248 	  }
   1249 	  break;
   1250 	case IMAGE_REL_PPC_IMGLUE:
   1251 	  {
   1252 	    /* There is nothing to do now. This reloc was noted in the first
   1253 	       pass over the relocs, and the glue instruction extracted.  */
   1254 	    const char *my_name;
   1255 
   1256 	    if (h->symbol_is_glue == 1)
   1257 	      break;
   1258 	    my_name = h->root.root.root.string;
   1259 
   1260 	    (*_bfd_error_handler)
   1261 	      (_("%B: Out of order IMGLUE reloc for %s"), input_bfd, my_name);
   1262 	    bfd_set_error (bfd_error_bad_value);
   1263 	    return FALSE;
   1264 	  }
   1265 
   1266 	case IMAGE_REL_PPC_ADDR32NB:
   1267 	  {
   1268 	    const char *name = 0;
   1269 
   1270 	    DUMP_RELOC2 (howto->name, rel);
   1271 
   1272 	    if (CONST_STRNEQ (input_section->name, ".idata$2") && first_thunk_address == 0)
   1273 	      {
   1274 		/* Set magic values.  */
   1275 		int idata5offset;
   1276 		struct coff_link_hash_entry *myh;
   1277 
   1278 		myh = coff_link_hash_lookup (coff_hash_table (info),
   1279 					     "__idata5_magic__",
   1280 					     FALSE, FALSE, TRUE);
   1281 		first_thunk_address = myh->root.u.def.value +
   1282 		  sec->output_section->vma +
   1283 		    sec->output_offset -
   1284 		      pe_data(output_bfd)->pe_opthdr.ImageBase;
   1285 
   1286 		idata5offset = myh->root.u.def.value;
   1287 		myh = coff_link_hash_lookup (coff_hash_table (info),
   1288 					     "__idata6_magic__",
   1289 					     FALSE, FALSE, TRUE);
   1290 
   1291 		thunk_size = myh->root.u.def.value - idata5offset;
   1292 		myh = coff_link_hash_lookup (coff_hash_table (info),
   1293 					     "__idata4_magic__",
   1294 					     FALSE, FALSE, TRUE);
   1295 		import_table_size = myh->root.u.def.value;
   1296 	      }
   1297 
   1298 	    if (h == 0)
   1299 	      /* It is a file local symbol.  */
   1300 	      sym = syms + symndx;
   1301 	    else
   1302 	      {
   1303 		char *target = 0;
   1304 
   1305 		name = h->root.root.root.string;
   1306 		if (strcmp (".idata$2", name) == 0)
   1307 		  target = "__idata2_magic__";
   1308 		else if (strcmp (".idata$4", name) == 0)
   1309 		  target = "__idata4_magic__";
   1310 		else if (strcmp (".idata$5", name) == 0)
   1311 		  target = "__idata5_magic__";
   1312 
   1313 		if (target != 0)
   1314 		  {
   1315 		    struct coff_link_hash_entry *myh;
   1316 
   1317 		    myh = coff_link_hash_lookup (coff_hash_table (info),
   1318 						 target,
   1319 						 FALSE, FALSE, TRUE);
   1320 		    if (myh == 0)
   1321 		      {
   1322 			/* Missing magic cookies. Something is very wrong.  */
   1323 			abort ();
   1324 		      }
   1325 
   1326 		    val = myh->root.u.def.value +
   1327 		      sec->output_section->vma + sec->output_offset;
   1328 		    if (first_thunk_address == 0)
   1329 		      {
   1330 			int idata5offset;
   1331 			myh = coff_link_hash_lookup (coff_hash_table (info),
   1332 						     "__idata5_magic__",
   1333 						     FALSE, FALSE, TRUE);
   1334 			first_thunk_address = myh->root.u.def.value +
   1335 			  sec->output_section->vma +
   1336 			    sec->output_offset -
   1337 			      pe_data(output_bfd)->pe_opthdr.ImageBase;
   1338 
   1339 			idata5offset = myh->root.u.def.value;
   1340 			myh = coff_link_hash_lookup (coff_hash_table (info),
   1341 						     "__idata6_magic__",
   1342 						     FALSE, FALSE, TRUE);
   1343 
   1344 			thunk_size = myh->root.u.def.value - idata5offset;
   1345 			myh = coff_link_hash_lookup (coff_hash_table (info),
   1346 						     "__idata4_magic__",
   1347 						     FALSE, FALSE, TRUE);
   1348 			import_table_size = myh->root.u.def.value;
   1349 		      }
   1350 		  }
   1351 	      }
   1352 
   1353 	    rstat = _bfd_relocate_contents (howto,
   1354 					    input_bfd,
   1355 					    val -
   1356 					    pe_data (output_bfd)->pe_opthdr.ImageBase,
   1357 					    loc);
   1358 	  }
   1359 	  break;
   1360 
   1361 	case IMAGE_REL_PPC_REL24:
   1362 	  DUMP_RELOC2(howto->name, rel);
   1363 	  val -= (input_section->output_section->vma
   1364 		  + input_section->output_offset);
   1365 
   1366 	  rstat = _bfd_relocate_contents (howto,
   1367 					  input_bfd,
   1368 					  val,
   1369 					  loc);
   1370 	  break;
   1371 	case IMAGE_REL_PPC_ADDR16:
   1372 	case IMAGE_REL_PPC_ADDR24:
   1373 	case IMAGE_REL_PPC_ADDR32:
   1374 	  DUMP_RELOC2(howto->name, rel);
   1375 	  rstat = _bfd_relocate_contents (howto,
   1376 					  input_bfd,
   1377 					  val,
   1378 					  loc);
   1379 	  break;
   1380 	}
   1381 
   1382       if (info->base_file)
   1383 	{
   1384 	  /* So if this is non pcrelative, and is referenced
   1385 	     to a section or a common symbol, then it needs a reloc.  */
   1386 	  if (sym && pe_data(output_bfd)->in_reloc_p (output_bfd, howto))
   1387 	    {
   1388 	      /* Relocation to a symbol in a section which
   1389 		 isn't absolute - we output the address here
   1390 		 to a file.  */
   1391 	      bfd_vma addr = (rel->r_vaddr
   1392 			      - input_section->vma
   1393 			      + input_section->output_offset
   1394 			      + input_section->output_section->vma);
   1395 
   1396 	      if (!write_base_file_entry (output_bfd, info, addr))
   1397 		return FALSE;
   1398 	    }
   1399 	}
   1400 
   1401       switch (rstat)
   1402 	{
   1403 	default:
   1404 	  abort ();
   1405 	case bfd_reloc_ok:
   1406 	  break;
   1407 	case bfd_reloc_overflow:
   1408 	  {
   1409 	    const char *name;
   1410 	    char buf[SYMNMLEN + 1];
   1411 
   1412 	    if (symndx == -1)
   1413 	      name = "*ABS*";
   1414 	    else if (h != NULL)
   1415 	      name = NULL;
   1416 	    else if (sym == NULL)
   1417 	      name = "*unknown*";
   1418 	    else if (sym->_n._n_n._n_zeroes == 0
   1419 		     && sym->_n._n_n._n_offset != 0)
   1420 	      name = obj_coff_strings (input_bfd) + sym->_n._n_n._n_offset;
   1421 	    else
   1422 	      {
   1423 		strncpy (buf, sym->_n._n_name, SYMNMLEN);
   1424 		buf[SYMNMLEN] = '\0';
   1425 		name = buf;
   1426 	      }
   1427 
   1428 	    (*info->callbacks->reloc_overflow)
   1429 	      (info, (h ? &h->root.root : NULL), name, howto->name,
   1430 	       (bfd_vma) 0, input_bfd, input_section,
   1431 	       rel->r_vaddr - input_section->vma);
   1432 	  }
   1433 	}
   1434     }
   1435 
   1436   return TRUE;
   1437 }
   1438 
   1439 #ifdef COFF_IMAGE_WITH_PE
   1440 
   1441 /* FIXME: BFD should not use global variables.  This file is compiled
   1442    twice, and these variables are shared.  This is confusing and
   1443    weird.  */
   1444 
   1445 long int global_toc_size = 4;
   1446 
   1447 bfd* bfd_of_toc_owner = 0;
   1448 
   1449 long int import_table_size;
   1450 long int first_thunk_address;
   1451 long int thunk_size;
   1452 
   1453 struct list_ele *head;
   1454 struct list_ele *tail;
   1455 
   1456 static char *
   1457 h1 = N_("\n\t\t\tTOC MAPPING\n\n");
   1458 static char *
   1459 h2 = N_(" TOC    disassembly  Comments       Name\n");
   1460 static char *
   1461 h3 = N_(" Offset  spelling                   (if present)\n");
   1462 
   1463 void
   1464 dump_toc (void * vfile)
   1465 {
   1466   FILE *file = (FILE *) vfile;
   1467   struct list_ele *t;
   1468 
   1469   fputs (_(h1), file);
   1470   fputs (_(h2), file);
   1471   fputs (_(h3), file);
   1472 
   1473   for (t = head; t != 0; t=t->next)
   1474     {
   1475       const char *cat = "";
   1476 
   1477       if (t->cat == priv)
   1478 	cat = _("private       ");
   1479       else if (t->cat == pub)
   1480 	cat = _("public        ");
   1481       else if (t->cat == tocdata)
   1482 	cat = _("data-in-toc   ");
   1483 
   1484       if (t->offset > global_toc_size)
   1485 	{
   1486 	  if (t->offset <= global_toc_size + thunk_size)
   1487 	    cat = _("IAT reference ");
   1488 	  else
   1489 	    {
   1490 	      fprintf (file,
   1491 		      _("**** global_toc_size %ld(%lx), thunk_size %ld(%lx)\n"),
   1492 		       global_toc_size, (unsigned long) global_toc_size,
   1493 		       thunk_size, (unsigned long) thunk_size);
   1494 	      cat = _("Out of bounds!");
   1495 	    }
   1496 	}
   1497 
   1498       fprintf (file,
   1499 	      " %04lx    (%d)", (unsigned long) t->offset, t->offset - 32768);
   1500       fprintf (file,
   1501 	      "    %s %s\n",
   1502 	      cat, t->name);
   1503 
   1504     }
   1505 
   1506   fprintf (file, "\n");
   1507 }
   1508 
   1509 bfd_boolean
   1510 ppc_allocate_toc_section (struct bfd_link_info *info ATTRIBUTE_UNUSED)
   1511 {
   1512   asection *s;
   1513   bfd_byte *foo;
   1514   bfd_size_type amt;
   1515   static char test_char = '1';
   1516 
   1517   if ( global_toc_size == 0 ) /* FIXME: does this get me in trouble?  */
   1518     return TRUE;
   1519 
   1520   if (bfd_of_toc_owner == 0)
   1521     /* No toc owner? Something is very wrong.  */
   1522     abort ();
   1523 
   1524   s = bfd_get_section_by_name ( bfd_of_toc_owner , TOC_SECTION_NAME);
   1525   if (s == NULL)
   1526     /* No toc section? Something is very wrong.  */
   1527     abort ();
   1528 
   1529   amt = global_toc_size;
   1530   foo = (bfd_byte *) bfd_alloc (bfd_of_toc_owner, amt);
   1531   memset(foo, test_char, (size_t) global_toc_size);
   1532 
   1533   s->size = global_toc_size;
   1534   s->contents = foo;
   1535 
   1536   return TRUE;
   1537 }
   1538 
   1539 bfd_boolean
   1540 ppc_process_before_allocation (bfd *abfd,
   1541 			       struct bfd_link_info *info)
   1542 {
   1543   asection *sec;
   1544   struct internal_reloc *i, *rel;
   1545 
   1546   /* Here we have a bfd that is to be included on the link. We have a hook
   1547      to do reloc rummaging, before section sizes are nailed down.  */
   1548   _bfd_coff_get_external_symbols (abfd);
   1549 
   1550   /* Rummage around all the relocs and map the toc.  */
   1551   sec = abfd->sections;
   1552 
   1553   if (sec == 0)
   1554     return TRUE;
   1555 
   1556   for (; sec != 0; sec = sec->next)
   1557     {
   1558       if (sec->reloc_count == 0)
   1559 	continue;
   1560 
   1561       /* load the relocs */
   1562       /* FIXME: there may be a storage leak here */
   1563       i=_bfd_coff_read_internal_relocs(abfd,sec,1,0,0,0);
   1564 
   1565       if (i == 0)
   1566 	abort ();
   1567 
   1568       for (rel = i; rel < i + sec->reloc_count; ++rel)
   1569 	{
   1570 	  unsigned short r_type  = EXTRACT_TYPE  (rel->r_type);
   1571 	  unsigned short r_flags = EXTRACT_FLAGS (rel->r_type);
   1572 	  bfd_boolean ok = TRUE;
   1573 
   1574 	  DUMP_RELOC2 (ppc_coff_howto_table[r_type].name, rel);
   1575 
   1576 	  switch(r_type)
   1577 	    {
   1578 	    case IMAGE_REL_PPC_TOCREL16:
   1579 	      /* If TOCDEFN is on, ignore as someone else has allocated the
   1580 		 toc entry.  */
   1581 	      if ((r_flags & IMAGE_REL_PPC_TOCDEFN) != IMAGE_REL_PPC_TOCDEFN)
   1582 		ok = ppc_record_toc_entry(abfd, info, sec,
   1583 					  rel->r_symndx, default_toc);
   1584 	      if (!ok)
   1585 		return FALSE;
   1586 	      break;
   1587 	    case IMAGE_REL_PPC_IMGLUE:
   1588 	      ppc_mark_symbol_as_glue (abfd, rel->r_symndx, rel);
   1589 	      break;
   1590 	    default:
   1591 	      break;
   1592 	    }
   1593 	}
   1594     }
   1595 
   1596   return TRUE;
   1597 }
   1598 
   1599 #endif
   1600 
   1601 static bfd_reloc_status_type
   1602 ppc_refhi_reloc (bfd *abfd ATTRIBUTE_UNUSED,
   1603 		 arelent *reloc_entry ATTRIBUTE_UNUSED,
   1604 		 asymbol *symbol ATTRIBUTE_UNUSED,
   1605 		 void * data ATTRIBUTE_UNUSED,
   1606 		 asection *input_section ATTRIBUTE_UNUSED,
   1607 		 bfd *output_bfd,
   1608 		 char **error_message ATTRIBUTE_UNUSED)
   1609 {
   1610   UN_IMPL("REFHI");
   1611   DUMP_RELOC("REFHI",reloc_entry);
   1612 
   1613   if (output_bfd == (bfd *) NULL)
   1614     return bfd_reloc_continue;
   1615 
   1616   return bfd_reloc_undefined;
   1617 }
   1618 
   1619 static bfd_reloc_status_type
   1620 ppc_pair_reloc (bfd *abfd ATTRIBUTE_UNUSED,
   1621 		arelent *reloc_entry ATTRIBUTE_UNUSED,
   1622 		asymbol *symbol ATTRIBUTE_UNUSED,
   1623 		void * data ATTRIBUTE_UNUSED,
   1624 		asection *input_section ATTRIBUTE_UNUSED,
   1625 		bfd *output_bfd,
   1626 		char **error_message ATTRIBUTE_UNUSED)
   1627 {
   1628   UN_IMPL("PAIR");
   1629   DUMP_RELOC("PAIR",reloc_entry);
   1630 
   1631   if (output_bfd == (bfd *) NULL)
   1632     return bfd_reloc_continue;
   1633 
   1634   return bfd_reloc_undefined;
   1635 }
   1636 
   1637 static bfd_reloc_status_type
   1638 ppc_toc16_reloc (bfd *abfd ATTRIBUTE_UNUSED,
   1639 		 arelent *reloc_entry ATTRIBUTE_UNUSED,
   1640 		 asymbol *symbol ATTRIBUTE_UNUSED,
   1641 		 void * data ATTRIBUTE_UNUSED,
   1642 		 asection *input_section ATTRIBUTE_UNUSED,
   1643 		 bfd *output_bfd,
   1644 		 char **error_message ATTRIBUTE_UNUSED)
   1645 {
   1646   UN_IMPL ("TOCREL16");
   1647   DUMP_RELOC ("TOCREL16",reloc_entry);
   1648 
   1649   if (output_bfd == (bfd *) NULL)
   1650     return bfd_reloc_continue;
   1651 
   1652   return bfd_reloc_ok;
   1653 }
   1654 
   1655 static bfd_reloc_status_type
   1656 ppc_secrel_reloc (bfd *abfd ATTRIBUTE_UNUSED,
   1657 		  arelent *reloc_entry ATTRIBUTE_UNUSED,
   1658 		  asymbol *symbol ATTRIBUTE_UNUSED,
   1659 		  void * data ATTRIBUTE_UNUSED,
   1660 		  asection *input_section ATTRIBUTE_UNUSED,
   1661 		  bfd *output_bfd,
   1662 		  char **error_message ATTRIBUTE_UNUSED)
   1663 {
   1664   UN_IMPL("SECREL");
   1665   DUMP_RELOC("SECREL",reloc_entry);
   1666 
   1667   if (output_bfd == (bfd *) NULL)
   1668     return bfd_reloc_continue;
   1669 
   1670   return bfd_reloc_ok;
   1671 }
   1672 
   1673 static bfd_reloc_status_type
   1674 ppc_section_reloc (bfd *abfd ATTRIBUTE_UNUSED,
   1675 		   arelent *reloc_entry ATTRIBUTE_UNUSED,
   1676 		   asymbol *symbol ATTRIBUTE_UNUSED,
   1677 		   void * data ATTRIBUTE_UNUSED,
   1678 		   asection *input_section ATTRIBUTE_UNUSED,
   1679 		   bfd *output_bfd,
   1680 		   char **error_message ATTRIBUTE_UNUSED)
   1681 {
   1682   UN_IMPL("SECTION");
   1683   DUMP_RELOC("SECTION",reloc_entry);
   1684 
   1685   if (output_bfd == (bfd *) NULL)
   1686     return bfd_reloc_continue;
   1687 
   1688   return bfd_reloc_ok;
   1689 }
   1690 
   1691 static bfd_reloc_status_type
   1692 ppc_imglue_reloc (bfd *abfd ATTRIBUTE_UNUSED,
   1693 		  arelent *reloc_entry ATTRIBUTE_UNUSED,
   1694 		  asymbol *symbol ATTRIBUTE_UNUSED,
   1695 		  void * data ATTRIBUTE_UNUSED,
   1696 		  asection *input_section ATTRIBUTE_UNUSED,
   1697 		  bfd *output_bfd,
   1698 		  char **error_message ATTRIBUTE_UNUSED)
   1699 
   1700 {
   1701   UN_IMPL("IMGLUE");
   1702   DUMP_RELOC("IMGLUE",reloc_entry);
   1703 
   1704   if (output_bfd == (bfd *) NULL)
   1705     return bfd_reloc_continue;
   1706 
   1707   return bfd_reloc_ok;
   1708 }
   1709 
   1710 #define MAX_RELOC_INDEX  \
   1712       (sizeof (ppc_coff_howto_table) / sizeof (ppc_coff_howto_table[0]) - 1)
   1713 
   1714 /* FIXME: There is a possibility that when we read in a reloc from a file,
   1715           that there are some bits encoded in the upper portion of the
   1716 	  type field. Not yet implemented.  */
   1717 
   1718 static void
   1719 ppc_coff_rtype2howto (arelent *relent, struct internal_reloc *internal)
   1720 {
   1721   /* We can encode one of three things in the type field, aside from the
   1722      type:
   1723      1. IMAGE_REL_PPC_NEG - indicates the value field is a subtraction
   1724         value, rather than an addition value
   1725      2. IMAGE_REL_PPC_BRTAKEN, IMAGE_REL_PPC_BRNTAKEN - indicates that
   1726         the branch is expected to be taken or not.
   1727      3. IMAGE_REL_PPC_TOCDEFN - toc slot definition in the file
   1728      For now, we just strip this stuff to find the type, and ignore it other
   1729      than that.  */
   1730   reloc_howto_type *howto;
   1731   unsigned short r_type  = EXTRACT_TYPE (internal->r_type);
   1732   unsigned short r_flags = EXTRACT_FLAGS(internal->r_type);
   1733   unsigned short junk    = EXTRACT_JUNK (internal->r_type);
   1734 
   1735   /* The masking process only slices off the bottom byte for r_type.  */
   1736   if ( r_type > MAX_RELOC_INDEX )
   1737     abort ();
   1738 
   1739   /* Check for absolute crap.  */
   1740   if (junk != 0)
   1741     abort ();
   1742 
   1743   switch(r_type)
   1744     {
   1745     case IMAGE_REL_PPC_ADDR16:
   1746     case IMAGE_REL_PPC_REL24:
   1747     case IMAGE_REL_PPC_ADDR24:
   1748     case IMAGE_REL_PPC_ADDR32:
   1749     case IMAGE_REL_PPC_IFGLUE:
   1750     case IMAGE_REL_PPC_ADDR32NB:
   1751     case IMAGE_REL_PPC_SECTION:
   1752     case IMAGE_REL_PPC_SECREL:
   1753       DUMP_RELOC2 (ppc_coff_howto_table[r_type].name, internal);
   1754       howto = ppc_coff_howto_table + r_type;
   1755       break;
   1756     case IMAGE_REL_PPC_IMGLUE:
   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_TOCREL16:
   1761       DUMP_RELOC2 (ppc_coff_howto_table[r_type].name, internal);
   1762       if (r_flags & IMAGE_REL_PPC_TOCDEFN)
   1763 	howto = ppc_coff_howto_table + IMAGE_REL_PPC_TOCREL16_DEFN;
   1764       else
   1765 	howto = ppc_coff_howto_table + IMAGE_REL_PPC_TOCREL16;
   1766       break;
   1767     default:
   1768       (*_bfd_error_handler) (_("warning: unsupported reloc %s [%d] used -- it may not work"),
   1769 			     ppc_coff_howto_table[r_type].name,
   1770 			     r_type);
   1771       howto = ppc_coff_howto_table + r_type;
   1772       break;
   1773     }
   1774 
   1775   relent->howto = howto;
   1776 }
   1777 
   1778 static reloc_howto_type *
   1779 coff_ppc_rtype_to_howto (bfd *abfd ATTRIBUTE_UNUSED,
   1780 			 asection *sec,
   1781 			 struct internal_reloc *rel,
   1782 			 struct coff_link_hash_entry *h ATTRIBUTE_UNUSED,
   1783 			 struct internal_syment *sym ATTRIBUTE_UNUSED,
   1784 			 bfd_vma *addendp)
   1785 {
   1786   reloc_howto_type *howto;
   1787 
   1788   /* We can encode one of three things in the type field, aside from the
   1789      type:
   1790      1. IMAGE_REL_PPC_NEG - indicates the value field is a subtraction
   1791         value, rather than an addition value
   1792      2. IMAGE_REL_PPC_BRTAKEN, IMAGE_REL_PPC_BRNTAKEN - indicates that
   1793         the branch is expected to be taken or not.
   1794      3. IMAGE_REL_PPC_TOCDEFN - toc slot definition in the file
   1795      For now, we just strip this stuff to find the type, and ignore it other
   1796      than that.  */
   1797 
   1798   unsigned short r_type  = EXTRACT_TYPE  (rel->r_type);
   1799   unsigned short r_flags = EXTRACT_FLAGS (rel->r_type);
   1800   unsigned short junk    = EXTRACT_JUNK  (rel->r_type);
   1801 
   1802   /* The masking process only slices off the bottom byte for r_type.  */
   1803   if (r_type > MAX_RELOC_INDEX)
   1804     abort ();
   1805 
   1806   /* Check for absolute crap.  */
   1807   if (junk != 0)
   1808     abort ();
   1809 
   1810   switch(r_type)
   1811     {
   1812     case IMAGE_REL_PPC_ADDR32NB:
   1813       DUMP_RELOC2(ppc_coff_howto_table[r_type].name, rel);
   1814       *addendp -= pe_data(sec->output_section->owner)->pe_opthdr.ImageBase;
   1815       howto = ppc_coff_howto_table + r_type;
   1816       break;
   1817     case IMAGE_REL_PPC_TOCREL16:
   1818       DUMP_RELOC2(ppc_coff_howto_table[r_type].name, rel);
   1819       if (r_flags & IMAGE_REL_PPC_TOCDEFN)
   1820 	howto = ppc_coff_howto_table + IMAGE_REL_PPC_TOCREL16_DEFN;
   1821       else
   1822 	howto = ppc_coff_howto_table + IMAGE_REL_PPC_TOCREL16;
   1823       break;
   1824     case IMAGE_REL_PPC_ADDR16:
   1825     case IMAGE_REL_PPC_REL24:
   1826     case IMAGE_REL_PPC_ADDR24:
   1827     case IMAGE_REL_PPC_ADDR32:
   1828     case IMAGE_REL_PPC_IFGLUE:
   1829     case IMAGE_REL_PPC_SECTION:
   1830     case IMAGE_REL_PPC_SECREL:
   1831       DUMP_RELOC2(ppc_coff_howto_table[r_type].name, rel);
   1832       howto = ppc_coff_howto_table + r_type;
   1833       break;
   1834     case IMAGE_REL_PPC_IMGLUE:
   1835       DUMP_RELOC2(ppc_coff_howto_table[r_type].name, rel);
   1836       howto = ppc_coff_howto_table + r_type;
   1837       break;
   1838     default:
   1839       (*_bfd_error_handler) (_("warning: unsupported reloc %s [%d] used -- it may not work"),
   1840 			     ppc_coff_howto_table[r_type].name,
   1841 			     r_type);
   1842       howto = ppc_coff_howto_table + r_type;
   1843       break;
   1844     }
   1845 
   1846   return howto;
   1847 }
   1848 
   1849 /* A cheesy little macro to make the code a little more readable.  */
   1850 #define HOW2MAP(bfd_rtype,ppc_rtype)  \
   1851  case bfd_rtype: return &ppc_coff_howto_table[ppc_rtype]
   1852 
   1853 static reloc_howto_type *
   1854 ppc_coff_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
   1855 			    bfd_reloc_code_real_type code)
   1856 {
   1857   switch (code)
   1858     {
   1859       HOW2MAP(BFD_RELOC_32_GOTOFF,    IMAGE_REL_PPC_IMGLUE);
   1860       HOW2MAP(BFD_RELOC_16_GOT_PCREL, IMAGE_REL_PPC_IFGLUE);
   1861       HOW2MAP(BFD_RELOC_16,           IMAGE_REL_PPC_ADDR16);
   1862       HOW2MAP(BFD_RELOC_PPC_B26,      IMAGE_REL_PPC_REL24);
   1863       HOW2MAP(BFD_RELOC_PPC_BA26,     IMAGE_REL_PPC_ADDR24);
   1864       HOW2MAP(BFD_RELOC_PPC_TOC16,    IMAGE_REL_PPC_TOCREL16);
   1865       HOW2MAP(BFD_RELOC_16_GOTOFF,    IMAGE_REL_PPC_TOCREL16_DEFN);
   1866       HOW2MAP(BFD_RELOC_32,           IMAGE_REL_PPC_ADDR32);
   1867       HOW2MAP(BFD_RELOC_RVA,          IMAGE_REL_PPC_ADDR32NB);
   1868     default:
   1869       return NULL;
   1870     }
   1871 }
   1872 #undef HOW2MAP
   1873 
   1874 static reloc_howto_type *
   1875 ppc_coff_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
   1876 			    const char *r_name)
   1877 {
   1878   unsigned int i;
   1879 
   1880   for (i = 0;
   1881        i < sizeof (ppc_coff_howto_table) / sizeof (ppc_coff_howto_table[0]);
   1882        i++)
   1883     if (ppc_coff_howto_table[i].name != NULL
   1884 	&& strcasecmp (ppc_coff_howto_table[i].name, r_name) == 0)
   1885       return &ppc_coff_howto_table[i];
   1886 
   1887   return NULL;
   1888 }
   1889 
   1890 /* Tailor coffcode.h -- macro heaven.  */
   1892 
   1893 #define RTYPE2HOWTO(cache_ptr, dst)  ppc_coff_rtype2howto (cache_ptr, dst)
   1894 
   1895 /* We use the special COFF backend linker, with our own special touch.  */
   1896 
   1897 #define coff_bfd_reloc_type_lookup   ppc_coff_reloc_type_lookup
   1898 #define coff_bfd_reloc_name_lookup ppc_coff_reloc_name_lookup
   1899 #define coff_rtype_to_howto          coff_ppc_rtype_to_howto
   1900 #define coff_relocate_section        coff_ppc_relocate_section
   1901 #define coff_bfd_final_link          ppc_bfd_coff_final_link
   1902 
   1903 #ifndef COFF_IMAGE_WITH_PE
   1904 #endif
   1905 
   1906 #define SELECT_RELOC(internal, howto) {internal.r_type=howto->type;}
   1907 
   1908 #define COFF_PAGE_SIZE                       0x1000
   1909 
   1910 /* FIXME: This controls some code that used to be in peicode.h and is
   1911    now in peigen.c.  It will not control the code in peigen.c.  If
   1912    anybody wants to get this working, you will need to fix that.  */
   1913 #define POWERPC_LE_PE
   1914 
   1915 #define COFF_SECTION_ALIGNMENT_ENTRIES \
   1916 { COFF_SECTION_NAME_EXACT_MATCH (".idata$2"), \
   1917   COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 0 }, \
   1918 { COFF_SECTION_NAME_EXACT_MATCH (".idata$3"), \
   1919   COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 0 }, \
   1920 { COFF_SECTION_NAME_EXACT_MATCH (".idata$4"), \
   1921   COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 2 }, \
   1922 { COFF_SECTION_NAME_EXACT_MATCH (".idata$5"), \
   1923   COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 2 }, \
   1924 { COFF_SECTION_NAME_EXACT_MATCH (".idata$6"), \
   1925   COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 1 }, \
   1926 { COFF_SECTION_NAME_EXACT_MATCH (".reloc"), \
   1927   COFF_ALIGNMENT_FIELD_EMPTY, COFF_ALIGNMENT_FIELD_EMPTY, 1 }
   1928 
   1929 #include "coffcode.h"
   1930 
   1931 #ifndef COFF_IMAGE_WITH_PE
   1933 
   1934 static bfd_boolean
   1935 ppc_do_last (bfd *abfd)
   1936 {
   1937   if (abfd == bfd_of_toc_owner)
   1938     return TRUE;
   1939   else
   1940     return FALSE;
   1941 }
   1942 
   1943 static bfd *
   1944 ppc_get_last (void)
   1945 {
   1946   return bfd_of_toc_owner;
   1947 }
   1948 
   1949 /* This piece of machinery exists only to guarantee that the bfd that holds
   1950    the toc section is written last.
   1951 
   1952    This does depend on bfd_make_section attaching a new section to the
   1953    end of the section list for the bfd.
   1954 
   1955    This is otherwise intended to be functionally the same as
   1956    cofflink.c:_bfd_coff_final_link(). It is specifically different only
   1957    where the POWERPC_LE_PE macro modifies the code. It is left in as a
   1958    precise form of comment. krk (at) cygnus.com  */
   1959 
   1960 /* Do the final link step.  */
   1961 
   1962 bfd_boolean
   1963 ppc_bfd_coff_final_link (bfd *abfd, struct bfd_link_info *info)
   1964 {
   1965   bfd_size_type symesz;
   1966   struct coff_final_link_info flaginfo;
   1967   bfd_boolean debug_merge_allocated;
   1968   asection *o;
   1969   struct bfd_link_order *p;
   1970   bfd_size_type max_sym_count;
   1971   bfd_size_type max_lineno_count;
   1972   bfd_size_type max_reloc_count;
   1973   bfd_size_type max_output_reloc_count;
   1974   bfd_size_type max_contents_size;
   1975   file_ptr rel_filepos;
   1976   unsigned int relsz;
   1977   file_ptr line_filepos;
   1978   unsigned int linesz;
   1979   bfd *sub;
   1980   bfd_byte *external_relocs = NULL;
   1981   char strbuf[STRING_SIZE_SIZE];
   1982   bfd_size_type amt;
   1983 
   1984   symesz = bfd_coff_symesz (abfd);
   1985 
   1986   flaginfo.info = info;
   1987   flaginfo.output_bfd = abfd;
   1988   flaginfo.strtab = NULL;
   1989   flaginfo.section_info = NULL;
   1990   flaginfo.last_file_index = -1;
   1991   flaginfo.last_bf_index = -1;
   1992   flaginfo.internal_syms = NULL;
   1993   flaginfo.sec_ptrs = NULL;
   1994   flaginfo.sym_indices = NULL;
   1995   flaginfo.outsyms = NULL;
   1996   flaginfo.linenos = NULL;
   1997   flaginfo.contents = NULL;
   1998   flaginfo.external_relocs = NULL;
   1999   flaginfo.internal_relocs = NULL;
   2000   debug_merge_allocated = FALSE;
   2001 
   2002   coff_data (abfd)->link_info = info;
   2003 
   2004   flaginfo.strtab = _bfd_stringtab_init ();
   2005   if (flaginfo.strtab == NULL)
   2006     goto error_return;
   2007 
   2008   if (! coff_debug_merge_hash_table_init (&flaginfo.debug_merge))
   2009     goto error_return;
   2010   debug_merge_allocated = TRUE;
   2011 
   2012   /* Compute the file positions for all the sections.  */
   2013   if (! abfd->output_has_begun)
   2014     {
   2015       if (! bfd_coff_compute_section_file_positions (abfd))
   2016 	return FALSE;
   2017     }
   2018 
   2019   /* Count the line numbers and relocation entries required for the
   2020      output file.  Set the file positions for the relocs.  */
   2021   rel_filepos = obj_relocbase (abfd);
   2022   relsz = bfd_coff_relsz (abfd);
   2023   max_contents_size = 0;
   2024   max_lineno_count = 0;
   2025   max_reloc_count = 0;
   2026 
   2027   for (o = abfd->sections; o != NULL; o = o->next)
   2028     {
   2029       o->reloc_count = 0;
   2030       o->lineno_count = 0;
   2031 
   2032       for (p = o->map_head.link_order; p != NULL; p = p->next)
   2033 	{
   2034 	  if (p->type == bfd_indirect_link_order)
   2035 	    {
   2036 	      asection *sec;
   2037 
   2038 	      sec = p->u.indirect.section;
   2039 
   2040 	      /* Mark all sections which are to be included in the
   2041 		 link.  This will normally be every section.  We need
   2042 		 to do this so that we can identify any sections which
   2043 		 the linker has decided to not include.  */
   2044 	      sec->linker_mark = TRUE;
   2045 
   2046 	      if (info->strip == strip_none
   2047 		  || info->strip == strip_some)
   2048 		o->lineno_count += sec->lineno_count;
   2049 
   2050 	      if (bfd_link_relocatable (info))
   2051 		o->reloc_count += sec->reloc_count;
   2052 
   2053 	      if (sec->rawsize > max_contents_size)
   2054 		max_contents_size = sec->rawsize;
   2055 	      if (sec->size > max_contents_size)
   2056 		max_contents_size = sec->size;
   2057 	      if (sec->lineno_count > max_lineno_count)
   2058 		max_lineno_count = sec->lineno_count;
   2059 	      if (sec->reloc_count > max_reloc_count)
   2060 		max_reloc_count = sec->reloc_count;
   2061 	    }
   2062 	  else if (bfd_link_relocatable (info)
   2063 		   && (p->type == bfd_section_reloc_link_order
   2064 		       || p->type == bfd_symbol_reloc_link_order))
   2065 	    ++o->reloc_count;
   2066 	}
   2067       if (o->reloc_count == 0)
   2068 	o->rel_filepos = 0;
   2069       else
   2070 	{
   2071 	  o->flags |= SEC_RELOC;
   2072 	  o->rel_filepos = rel_filepos;
   2073 	  rel_filepos += o->reloc_count * relsz;
   2074 	}
   2075     }
   2076 
   2077   /* If doing a relocatable link, allocate space for the pointers we
   2078      need to keep.  */
   2079   if (bfd_link_relocatable (info))
   2080     {
   2081       unsigned int i;
   2082 
   2083       /* We use section_count + 1, rather than section_count, because
   2084          the target_index fields are 1 based.  */
   2085       amt = abfd->section_count + 1;
   2086       amt *= sizeof (struct coff_link_section_info);
   2087       flaginfo.section_info = (struct coff_link_section_info *) bfd_malloc (amt);
   2088 
   2089       if (flaginfo.section_info == NULL)
   2090 	goto error_return;
   2091 
   2092       for (i = 0; i <= abfd->section_count; i++)
   2093 	{
   2094 	  flaginfo.section_info[i].relocs = NULL;
   2095 	  flaginfo.section_info[i].rel_hashes = NULL;
   2096 	}
   2097     }
   2098 
   2099   /* We now know the size of the relocs, so we can determine the file
   2100      positions of the line numbers.  */
   2101   line_filepos = rel_filepos;
   2102   linesz = bfd_coff_linesz (abfd);
   2103   max_output_reloc_count = 0;
   2104 
   2105   for (o = abfd->sections; o != NULL; o = o->next)
   2106     {
   2107       if (o->lineno_count == 0)
   2108 	o->line_filepos = 0;
   2109       else
   2110 	{
   2111 	  o->line_filepos = line_filepos;
   2112 	  line_filepos += o->lineno_count * linesz;
   2113 	}
   2114 
   2115       if (o->reloc_count != 0)
   2116 	{
   2117 	  /* We don't know the indices of global symbols until we have
   2118              written out all the local symbols.  For each section in
   2119              the output file, we keep an array of pointers to hash
   2120              table entries.  Each entry in the array corresponds to a
   2121              reloc.  When we find a reloc against a global symbol, we
   2122              set the corresponding entry in this array so that we can
   2123              fix up the symbol index after we have written out all the
   2124              local symbols.
   2125 
   2126 	     Because of this problem, we also keep the relocs in
   2127 	     memory until the end of the link.  This wastes memory,
   2128 	     but only when doing a relocatable link, which is not the
   2129 	     common case.  */
   2130 	  BFD_ASSERT (bfd_link_relocatable (info));
   2131 	  amt = o->reloc_count;
   2132 	  amt *= sizeof (struct internal_reloc);
   2133 	  flaginfo.section_info[o->target_index].relocs =
   2134 	    (struct internal_reloc *) bfd_malloc (amt);
   2135 	  amt = o->reloc_count;
   2136 	  amt *= sizeof (struct coff_link_hash_entry *);
   2137 	  flaginfo.section_info[o->target_index].rel_hashes =
   2138 	    (struct coff_link_hash_entry **) bfd_malloc (amt);
   2139 	  if (flaginfo.section_info[o->target_index].relocs == NULL
   2140 	      || flaginfo.section_info[o->target_index].rel_hashes == NULL)
   2141 	    goto error_return;
   2142 
   2143 	  if (o->reloc_count > max_output_reloc_count)
   2144 	    max_output_reloc_count = o->reloc_count;
   2145 	}
   2146 
   2147       /* Reset the reloc and lineno counts, so that we can use them to
   2148 	 count the number of entries we have output so far.  */
   2149       o->reloc_count = 0;
   2150       o->lineno_count = 0;
   2151     }
   2152 
   2153   obj_sym_filepos (abfd) = line_filepos;
   2154 
   2155   /* Figure out the largest number of symbols in an input BFD.  Take
   2156      the opportunity to clear the output_has_begun fields of all the
   2157      input BFD's.  */
   2158   max_sym_count = 0;
   2159   for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
   2160     {
   2161       bfd_size_type sz;
   2162 
   2163       sub->output_has_begun = FALSE;
   2164       sz = obj_raw_syment_count (sub);
   2165       if (sz > max_sym_count)
   2166 	max_sym_count = sz;
   2167     }
   2168 
   2169   /* Allocate some buffers used while linking.  */
   2170   amt = max_sym_count * sizeof (struct internal_syment);
   2171   flaginfo.internal_syms = (struct internal_syment *) bfd_malloc (amt);
   2172   amt = max_sym_count * sizeof (asection *);
   2173   flaginfo.sec_ptrs = (asection **) bfd_malloc (amt);
   2174   amt = max_sym_count * sizeof (long);
   2175   flaginfo.sym_indices = (long *) bfd_malloc (amt);
   2176   amt = (max_sym_count + 1) * symesz;
   2177   flaginfo.outsyms = (bfd_byte *) bfd_malloc (amt);
   2178   amt = max_lineno_count * bfd_coff_linesz (abfd);
   2179   flaginfo.linenos = (bfd_byte *) bfd_malloc (amt);
   2180   flaginfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
   2181   flaginfo.external_relocs = (bfd_byte *) bfd_malloc (max_reloc_count * relsz);
   2182   if (! bfd_link_relocatable (info))
   2183     {
   2184       amt = max_reloc_count * sizeof (struct internal_reloc);
   2185       flaginfo.internal_relocs = (struct internal_reloc *) bfd_malloc (amt);
   2186     }
   2187   if ((flaginfo.internal_syms == NULL && max_sym_count > 0)
   2188       || (flaginfo.sec_ptrs == NULL && max_sym_count > 0)
   2189       || (flaginfo.sym_indices == NULL && max_sym_count > 0)
   2190       || flaginfo.outsyms == NULL
   2191       || (flaginfo.linenos == NULL && max_lineno_count > 0)
   2192       || (flaginfo.contents == NULL && max_contents_size > 0)
   2193       || (flaginfo.external_relocs == NULL && max_reloc_count > 0)
   2194       || (! bfd_link_relocatable (info)
   2195 	  && flaginfo.internal_relocs == NULL
   2196 	  && max_reloc_count > 0))
   2197     goto error_return;
   2198 
   2199   /* We now know the position of everything in the file, except that
   2200      we don't know the size of the symbol table and therefore we don't
   2201      know where the string table starts.  We just build the string
   2202      table in memory as we go along.  We process all the relocations
   2203      for a single input file at once.  */
   2204   obj_raw_syment_count (abfd) = 0;
   2205 
   2206   if (coff_backend_info (abfd)->_bfd_coff_start_final_link)
   2207     {
   2208       if (! bfd_coff_start_final_link (abfd, info))
   2209 	goto error_return;
   2210     }
   2211 
   2212   for (o = abfd->sections; o != NULL; o = o->next)
   2213     {
   2214       for (p = o->map_head.link_order; p != NULL; p = p->next)
   2215 	{
   2216 	  if (p->type == bfd_indirect_link_order
   2217 	      && (bfd_get_flavour (p->u.indirect.section->owner)
   2218 		  == bfd_target_coff_flavour))
   2219 	    {
   2220 	      sub = p->u.indirect.section->owner;
   2221 #ifdef POWERPC_LE_PE
   2222 	      if (! sub->output_has_begun && !ppc_do_last(sub))
   2223 #else
   2224 	      if (! sub->output_has_begun)
   2225 #endif
   2226 		{
   2227 		  if (! _bfd_coff_link_input_bfd (&flaginfo, sub))
   2228 		    goto error_return;
   2229 		  sub->output_has_begun = TRUE;
   2230 		}
   2231 	    }
   2232 	  else if (p->type == bfd_section_reloc_link_order
   2233 		   || p->type == bfd_symbol_reloc_link_order)
   2234 	    {
   2235 	      if (! _bfd_coff_reloc_link_order (abfd, &flaginfo, o, p))
   2236 		goto error_return;
   2237 	    }
   2238 	  else
   2239 	    {
   2240 	      if (! _bfd_default_link_order (abfd, info, o, p))
   2241 		goto error_return;
   2242 	    }
   2243 	}
   2244     }
   2245 
   2246 #ifdef POWERPC_LE_PE
   2247   {
   2248     bfd* last_one = ppc_get_last();
   2249     if (last_one)
   2250       {
   2251 	if (! _bfd_coff_link_input_bfd (&flaginfo, last_one))
   2252 	  goto error_return;
   2253       }
   2254     last_one->output_has_begun = TRUE;
   2255   }
   2256 #endif
   2257 
   2258   /* Free up the buffers used by _bfd_coff_link_input_bfd.  */
   2259   coff_debug_merge_hash_table_free (&flaginfo.debug_merge);
   2260   debug_merge_allocated = FALSE;
   2261 
   2262   if (flaginfo.internal_syms != NULL)
   2263     {
   2264       free (flaginfo.internal_syms);
   2265       flaginfo.internal_syms = NULL;
   2266     }
   2267   if (flaginfo.sec_ptrs != NULL)
   2268     {
   2269       free (flaginfo.sec_ptrs);
   2270       flaginfo.sec_ptrs = NULL;
   2271     }
   2272   if (flaginfo.sym_indices != NULL)
   2273     {
   2274       free (flaginfo.sym_indices);
   2275       flaginfo.sym_indices = NULL;
   2276     }
   2277   if (flaginfo.linenos != NULL)
   2278     {
   2279       free (flaginfo.linenos);
   2280       flaginfo.linenos = NULL;
   2281     }
   2282   if (flaginfo.contents != NULL)
   2283     {
   2284       free (flaginfo.contents);
   2285       flaginfo.contents = NULL;
   2286     }
   2287   if (flaginfo.external_relocs != NULL)
   2288     {
   2289       free (flaginfo.external_relocs);
   2290       flaginfo.external_relocs = NULL;
   2291     }
   2292   if (flaginfo.internal_relocs != NULL)
   2293     {
   2294       free (flaginfo.internal_relocs);
   2295       flaginfo.internal_relocs = NULL;
   2296     }
   2297 
   2298   /* The value of the last C_FILE symbol is supposed to be the symbol
   2299      index of the first external symbol.  Write it out again if
   2300      necessary.  */
   2301   if (flaginfo.last_file_index != -1
   2302       && (unsigned int) flaginfo.last_file.n_value != obj_raw_syment_count (abfd))
   2303     {
   2304       file_ptr pos;
   2305 
   2306       flaginfo.last_file.n_value = obj_raw_syment_count (abfd);
   2307       bfd_coff_swap_sym_out (abfd, &flaginfo.last_file,
   2308 			     flaginfo.outsyms);
   2309       pos = obj_sym_filepos (abfd) + flaginfo.last_file_index * symesz;
   2310       if (bfd_seek (abfd, pos, SEEK_SET) != 0
   2311 	  || bfd_bwrite (flaginfo.outsyms, symesz, abfd) != symesz)
   2312 	return FALSE;
   2313     }
   2314 
   2315   /* Write out the global symbols.  */
   2316   flaginfo.failed = FALSE;
   2317   bfd_hash_traverse (&info->hash->table, _bfd_coff_write_global_sym, &flaginfo);
   2318   if (flaginfo.failed)
   2319     goto error_return;
   2320 
   2321   /* The outsyms buffer is used by _bfd_coff_write_global_sym.  */
   2322   if (flaginfo.outsyms != NULL)
   2323     {
   2324       free (flaginfo.outsyms);
   2325       flaginfo.outsyms = NULL;
   2326     }
   2327 
   2328   if (bfd_link_relocatable (info))
   2329     {
   2330       /* Now that we have written out all the global symbols, we know
   2331 	 the symbol indices to use for relocs against them, and we can
   2332 	 finally write out the relocs.  */
   2333       amt = max_output_reloc_count * relsz;
   2334       external_relocs = (bfd_byte *) bfd_malloc (amt);
   2335       if (external_relocs == NULL)
   2336 	goto error_return;
   2337 
   2338       for (o = abfd->sections; o != NULL; o = o->next)
   2339 	{
   2340 	  struct internal_reloc *irel;
   2341 	  struct internal_reloc *irelend;
   2342 	  struct coff_link_hash_entry **rel_hash;
   2343 	  bfd_byte *erel;
   2344 
   2345 	  if (o->reloc_count == 0)
   2346 	    continue;
   2347 
   2348 	  irel = flaginfo.section_info[o->target_index].relocs;
   2349 	  irelend = irel + o->reloc_count;
   2350 	  rel_hash = flaginfo.section_info[o->target_index].rel_hashes;
   2351 	  erel = external_relocs;
   2352 	  for (; irel < irelend; irel++, rel_hash++, erel += relsz)
   2353 	    {
   2354 	      if (*rel_hash != NULL)
   2355 		{
   2356 		  BFD_ASSERT ((*rel_hash)->indx >= 0);
   2357 		  irel->r_symndx = (*rel_hash)->indx;
   2358 		}
   2359 	      bfd_coff_swap_reloc_out (abfd, irel, erel);
   2360 	    }
   2361 
   2362 	  amt = relsz * o->reloc_count;
   2363 	  if (bfd_seek (abfd, o->rel_filepos, SEEK_SET) != 0
   2364 	      || bfd_bwrite (external_relocs, amt, abfd) != amt)
   2365 	    goto error_return;
   2366 	}
   2367 
   2368       free (external_relocs);
   2369       external_relocs = NULL;
   2370     }
   2371 
   2372   /* Free up the section information.  */
   2373   if (flaginfo.section_info != NULL)
   2374     {
   2375       unsigned int i;
   2376 
   2377       for (i = 0; i < abfd->section_count; i++)
   2378 	{
   2379 	  if (flaginfo.section_info[i].relocs != NULL)
   2380 	    free (flaginfo.section_info[i].relocs);
   2381 	  if (flaginfo.section_info[i].rel_hashes != NULL)
   2382 	    free (flaginfo.section_info[i].rel_hashes);
   2383 	}
   2384       free (flaginfo.section_info);
   2385       flaginfo.section_info = NULL;
   2386     }
   2387 
   2388   /* If we have optimized stabs strings, output them.  */
   2389   if (coff_hash_table (info)->stab_info.stabstr != NULL)
   2390     {
   2391       if (! _bfd_write_stab_strings (abfd, &coff_hash_table (info)->stab_info))
   2392 	return FALSE;
   2393     }
   2394 
   2395   /* Write out the string table.  */
   2396   if (obj_raw_syment_count (abfd) != 0)
   2397     {
   2398       file_ptr pos;
   2399 
   2400       pos = obj_sym_filepos (abfd) + obj_raw_syment_count (abfd) * symesz;
   2401       if (bfd_seek (abfd, pos, SEEK_SET) != 0)
   2402 	return FALSE;
   2403 
   2404 #if STRING_SIZE_SIZE == 4
   2405       H_PUT_32 (abfd,
   2406 		_bfd_stringtab_size (flaginfo.strtab) + STRING_SIZE_SIZE,
   2407 		strbuf);
   2408 #else
   2409  #error Change H_PUT_32 above
   2410 #endif
   2411 
   2412       if (bfd_bwrite (strbuf, (bfd_size_type) STRING_SIZE_SIZE, abfd)
   2413 	  != STRING_SIZE_SIZE)
   2414 	return FALSE;
   2415 
   2416       if (! _bfd_stringtab_emit (abfd, flaginfo.strtab))
   2417 	return FALSE;
   2418     }
   2419 
   2420   _bfd_stringtab_free (flaginfo.strtab);
   2421 
   2422   /* Setting bfd_get_symcount to 0 will cause write_object_contents to
   2423      not try to write out the symbols.  */
   2424   bfd_get_symcount (abfd) = 0;
   2425 
   2426   return TRUE;
   2427 
   2428  error_return:
   2429   if (debug_merge_allocated)
   2430     coff_debug_merge_hash_table_free (&flaginfo.debug_merge);
   2431   if (flaginfo.strtab != NULL)
   2432     _bfd_stringtab_free (flaginfo.strtab);
   2433   if (flaginfo.section_info != NULL)
   2434     {
   2435       unsigned int i;
   2436 
   2437       for (i = 0; i < abfd->section_count; i++)
   2438 	{
   2439 	  if (flaginfo.section_info[i].relocs != NULL)
   2440 	    free (flaginfo.section_info[i].relocs);
   2441 	  if (flaginfo.section_info[i].rel_hashes != NULL)
   2442 	    free (flaginfo.section_info[i].rel_hashes);
   2443 	}
   2444       free (flaginfo.section_info);
   2445     }
   2446   if (flaginfo.internal_syms != NULL)
   2447     free (flaginfo.internal_syms);
   2448   if (flaginfo.sec_ptrs != NULL)
   2449     free (flaginfo.sec_ptrs);
   2450   if (flaginfo.sym_indices != NULL)
   2451     free (flaginfo.sym_indices);
   2452   if (flaginfo.outsyms != NULL)
   2453     free (flaginfo.outsyms);
   2454   if (flaginfo.linenos != NULL)
   2455     free (flaginfo.linenos);
   2456   if (flaginfo.contents != NULL)
   2457     free (flaginfo.contents);
   2458   if (flaginfo.external_relocs != NULL)
   2459     free (flaginfo.external_relocs);
   2460   if (flaginfo.internal_relocs != NULL)
   2461     free (flaginfo.internal_relocs);
   2462   if (external_relocs != NULL)
   2463     free (external_relocs);
   2464   return FALSE;
   2465 }
   2466 #endif
   2467 
   2468 /* Forward declaration for use by alternative_target field.  */
   2470 #ifdef TARGET_BIG_SYM
   2471 extern const bfd_target TARGET_BIG_SYM;
   2472 #endif
   2473 
   2474 /* The transfer vectors that lead the outside world to all of the above.  */
   2475 
   2476 #ifdef TARGET_LITTLE_SYM
   2477 const bfd_target TARGET_LITTLE_SYM =
   2478 {
   2479   TARGET_LITTLE_NAME,		/* name or coff-arm-little */
   2480   bfd_target_coff_flavour,
   2481   BFD_ENDIAN_LITTLE,		/* data byte order is little */
   2482   BFD_ENDIAN_LITTLE,		/* header byte order is little */
   2483 
   2484   (HAS_RELOC | EXEC_P |		/* FIXME: object flags */
   2485    HAS_LINENO | HAS_DEBUG |
   2486    HAS_SYMS | HAS_LOCALS | WP_TEXT | D_PAGED),
   2487 
   2488 #ifndef COFF_WITH_PE
   2489   (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY
   2490    | SEC_RELOC),		/* section flags */
   2491 #else
   2492   (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY
   2493    | SEC_RELOC | SEC_LINK_ONCE | SEC_LINK_DUPLICATES),
   2494 #endif
   2495 
   2496   0,				/* leading char */
   2497   '/',				/* ar_pad_char */
   2498   15,				/* ar_max_namelen??? FIXMEmgo */
   2499   0,				/* match priority.  */
   2500 
   2501   bfd_getl64, bfd_getl_signed_64, bfd_putl64,
   2502   bfd_getl32, bfd_getl_signed_32, bfd_putl32,
   2503   bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* data */
   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, /* hdrs */
   2508 
   2509   {_bfd_dummy_target, coff_object_p, 	/* bfd_check_format */
   2510      bfd_generic_archive_p, /* _bfd_dummy_target */ coff_object_p },
   2511   {bfd_false, coff_mkobject, _bfd_generic_mkarchive, /* bfd_set_format */
   2512      bfd_false},
   2513   {bfd_false, coff_write_object_contents,	/* bfd_write_contents */
   2514      _bfd_write_archive_contents, bfd_false},
   2515 
   2516   BFD_JUMP_TABLE_GENERIC (coff),
   2517   BFD_JUMP_TABLE_COPY (coff),
   2518   BFD_JUMP_TABLE_CORE (_bfd_nocore),
   2519   BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_coff),
   2520   BFD_JUMP_TABLE_SYMBOLS (coff),
   2521   BFD_JUMP_TABLE_RELOCS (coff),
   2522   BFD_JUMP_TABLE_WRITE (coff),
   2523   BFD_JUMP_TABLE_LINK (coff),
   2524   BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
   2525 
   2526   /* Alternative_target.  */
   2527 #ifdef TARGET_BIG_SYM
   2528   & TARGET_BIG_SYM,
   2529 #else
   2530   NULL,
   2531 #endif
   2532 
   2533   COFF_SWAP_TABLE
   2534 };
   2535 #endif
   2536 
   2537 #ifdef TARGET_BIG_SYM
   2538 const bfd_target TARGET_BIG_SYM =
   2539 {
   2540   TARGET_BIG_NAME,
   2541   bfd_target_coff_flavour,
   2542   BFD_ENDIAN_BIG,		/* data byte order is big */
   2543   BFD_ENDIAN_BIG,		/* header byte order is big */
   2544 
   2545   (HAS_RELOC | EXEC_P |		/* FIXME: object flags */
   2546    HAS_LINENO | HAS_DEBUG |
   2547    HAS_SYMS | HAS_LOCALS | WP_TEXT | D_PAGED),
   2548 
   2549 #ifndef COFF_WITH_PE
   2550   (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY
   2551    | SEC_RELOC),		/* section flags */
   2552 #else
   2553   (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY
   2554    | SEC_RELOC | SEC_LINK_ONCE | SEC_LINK_DUPLICATES),
   2555 #endif
   2556 
   2557   0,				/* leading char */
   2558   '/',				/* ar_pad_char */
   2559   15,				/* ar_max_namelen??? FIXMEmgo */
   2560   0,				/* match priority.  */
   2561 
   2562   bfd_getb64, bfd_getb_signed_64, bfd_putb64,
   2563   bfd_getb32, bfd_getb_signed_32, bfd_putb32,
   2564   bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* data */
   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, /* hdrs */
   2569 
   2570   {_bfd_dummy_target, coff_object_p, 	/* bfd_check_format */
   2571      bfd_generic_archive_p, /* _bfd_dummy_target */ coff_object_p },
   2572   {bfd_false, coff_mkobject, _bfd_generic_mkarchive, /* bfd_set_format */
   2573      bfd_false},
   2574   {bfd_false, coff_write_object_contents,	/* bfd_write_contents */
   2575      _bfd_write_archive_contents, bfd_false},
   2576 
   2577   BFD_JUMP_TABLE_GENERIC (coff),
   2578   BFD_JUMP_TABLE_COPY (coff),
   2579   BFD_JUMP_TABLE_CORE (_bfd_nocore),
   2580   BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_coff),
   2581   BFD_JUMP_TABLE_SYMBOLS (coff),
   2582   BFD_JUMP_TABLE_RELOCS (coff),
   2583   BFD_JUMP_TABLE_WRITE (coff),
   2584   BFD_JUMP_TABLE_LINK (coff),
   2585   BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
   2586 
   2587   /* Alternative_target.  */
   2588 #ifdef TARGET_LITTLE_SYM
   2589   & TARGET_LITTLE_SYM,
   2590 #else
   2591   NULL,
   2592 #endif
   2593 
   2594   COFF_SWAP_TABLE
   2595 };
   2596 
   2597 #endif
   2598