Home | History | Annotate | Download | only in bfd
      1 /* Support for the generic parts of PE/PEI, for BFD.
      2    Copyright (C) 1995-2014 Free Software Foundation, Inc.
      3    Written by Cygnus Solutions.
      4 
      5    This file is part of BFD, the Binary File Descriptor library.
      6 
      7    This program is free software; you can redistribute it and/or modify
      8    it under the terms of the GNU General Public License as published by
      9    the Free Software Foundation; either version 3 of the License, or
     10    (at your option) any later version.
     11 
     12    This program is distributed in the hope that it will be useful,
     13    but WITHOUT ANY WARRANTY; without even the implied warranty of
     14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     15    GNU General Public License for more details.
     16 
     17    You should have received a copy of the GNU General Public License
     18    along with this program; if not, write to the Free Software
     19    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
     20    MA 02110-1301, USA.  */
     21 
     22 
     23 /* Most of this hacked by  Steve Chamberlain,
     24 			sac (at) cygnus.com
     25 
     26    PE/PEI rearrangement (and code added): Donn Terry
     27                                        Softway Systems, Inc.  */
     28 
     29 /* Hey look, some documentation [and in a place you expect to find it]!
     30 
     31    The main reference for the pei format is "Microsoft Portable Executable
     32    and Common Object File Format Specification 4.1".  Get it if you need to
     33    do some serious hacking on this code.
     34 
     35    Another reference:
     36    "Peering Inside the PE: A Tour of the Win32 Portable Executable
     37    File Format", MSJ 1994, Volume 9.
     38 
     39    The *sole* difference between the pe format and the pei format is that the
     40    latter has an MSDOS 2.0 .exe header on the front that prints the message
     41    "This app must be run under Windows." (or some such).
     42    (FIXME: Whether that statement is *really* true or not is unknown.
     43    Are there more subtle differences between pe and pei formats?
     44    For now assume there aren't.  If you find one, then for God sakes
     45    document it here!)
     46 
     47    The Microsoft docs use the word "image" instead of "executable" because
     48    the former can also refer to a DLL (shared library).  Confusion can arise
     49    because the `i' in `pei' also refers to "image".  The `pe' format can
     50    also create images (i.e. executables), it's just that to run on a win32
     51    system you need to use the pei format.
     52 
     53    FIXME: Please add more docs here so the next poor fool that has to hack
     54    on this code has a chance of getting something accomplished without
     55    wasting too much time.  */
     56 
     57 #include "libpei.h"
     58 
     59 static bfd_boolean (*pe_saved_coff_bfd_print_private_bfd_data) (bfd *, void *) =
     60 #ifndef coff_bfd_print_private_bfd_data
     61      NULL;
     62 #else
     63      coff_bfd_print_private_bfd_data;
     64 #undef coff_bfd_print_private_bfd_data
     65 #endif
     66 
     67 static bfd_boolean                      pe_print_private_bfd_data (bfd *, void *);
     68 #define coff_bfd_print_private_bfd_data pe_print_private_bfd_data
     69 
     70 static bfd_boolean (*pe_saved_coff_bfd_copy_private_bfd_data) (bfd *, bfd *) =
     71 #ifndef coff_bfd_copy_private_bfd_data
     72      NULL;
     73 #else
     74      coff_bfd_copy_private_bfd_data;
     75 #undef coff_bfd_copy_private_bfd_data
     76 #endif
     77 
     78 static bfd_boolean                     pe_bfd_copy_private_bfd_data (bfd *, bfd *);
     79 #define coff_bfd_copy_private_bfd_data pe_bfd_copy_private_bfd_data
     80 
     81 #define coff_mkobject      pe_mkobject
     82 #define coff_mkobject_hook pe_mkobject_hook
     83 
     84 #ifdef COFF_IMAGE_WITH_PE
     85 /* This structure contains static variables used by the ILF code.  */
     86 typedef asection * asection_ptr;
     87 
     88 typedef struct
     89 {
     90   bfd *			abfd;
     91   bfd_byte *		data;
     92   struct bfd_in_memory * bim;
     93   unsigned short        magic;
     94 
     95   arelent *		reltab;
     96   unsigned int 		relcount;
     97 
     98   coff_symbol_type * 	sym_cache;
     99   coff_symbol_type * 	sym_ptr;
    100   unsigned int       	sym_index;
    101 
    102   unsigned int * 	sym_table;
    103   unsigned int * 	table_ptr;
    104 
    105   combined_entry_type * native_syms;
    106   combined_entry_type * native_ptr;
    107 
    108   coff_symbol_type **	sym_ptr_table;
    109   coff_symbol_type **	sym_ptr_ptr;
    110 
    111   unsigned int		sec_index;
    112 
    113   char *                string_table;
    114   char *                string_ptr;
    115   char *		end_string_ptr;
    116 
    117   SYMENT *              esym_table;
    118   SYMENT *              esym_ptr;
    119 
    120   struct internal_reloc * int_reltab;
    121 }
    122 pe_ILF_vars;
    123 #endif /* COFF_IMAGE_WITH_PE */
    124 
    125 const bfd_target *coff_real_object_p
    126   (bfd *, unsigned, struct internal_filehdr *, struct internal_aouthdr *);
    127 
    128 #ifndef NO_COFF_RELOCS
    130 static void
    131 coff_swap_reloc_in (bfd * abfd, void * src, void * dst)
    132 {
    133   RELOC *reloc_src = (RELOC *) src;
    134   struct internal_reloc *reloc_dst = (struct internal_reloc *) dst;
    135 
    136   reloc_dst->r_vaddr  = H_GET_32 (abfd, reloc_src->r_vaddr);
    137   reloc_dst->r_symndx = H_GET_S32 (abfd, reloc_src->r_symndx);
    138   reloc_dst->r_type   = H_GET_16 (abfd, reloc_src->r_type);
    139 #ifdef SWAP_IN_RELOC_OFFSET
    140   reloc_dst->r_offset = SWAP_IN_RELOC_OFFSET (abfd, reloc_src->r_offset);
    141 #endif
    142 }
    143 
    144 static unsigned int
    145 coff_swap_reloc_out (bfd * abfd, void * src, void * dst)
    146 {
    147   struct internal_reloc *reloc_src = (struct internal_reloc *) src;
    148   struct external_reloc *reloc_dst = (struct external_reloc *) dst;
    149 
    150   H_PUT_32 (abfd, reloc_src->r_vaddr, reloc_dst->r_vaddr);
    151   H_PUT_32 (abfd, reloc_src->r_symndx, reloc_dst->r_symndx);
    152   H_PUT_16 (abfd, reloc_src->r_type, reloc_dst->r_type);
    153 
    154 #ifdef SWAP_OUT_RELOC_OFFSET
    155   SWAP_OUT_RELOC_OFFSET (abfd, reloc_src->r_offset, reloc_dst->r_offset);
    156 #endif
    157 #ifdef SWAP_OUT_RELOC_EXTRA
    158   SWAP_OUT_RELOC_EXTRA (abfd, reloc_src, reloc_dst);
    159 #endif
    160   return RELSZ;
    161 }
    162 #endif /* not NO_COFF_RELOCS */
    163 
    164 #ifdef COFF_IMAGE_WITH_PE
    165 #undef FILHDR
    166 #define FILHDR struct external_PEI_IMAGE_hdr
    167 #endif
    168 
    169 static void
    170 coff_swap_filehdr_in (bfd * abfd, void * src, void * dst)
    171 {
    172   FILHDR *filehdr_src = (FILHDR *) src;
    173   struct internal_filehdr *filehdr_dst = (struct internal_filehdr *) dst;
    174 
    175   filehdr_dst->f_magic  = H_GET_16 (abfd, filehdr_src->f_magic);
    176   filehdr_dst->f_nscns  = H_GET_16 (abfd, filehdr_src->f_nscns);
    177   filehdr_dst->f_timdat = H_GET_32 (abfd, filehdr_src->f_timdat);
    178   filehdr_dst->f_nsyms  = H_GET_32 (abfd, filehdr_src->f_nsyms);
    179   filehdr_dst->f_flags  = H_GET_16 (abfd, filehdr_src->f_flags);
    180   filehdr_dst->f_symptr = H_GET_32 (abfd, filehdr_src->f_symptr);
    181 
    182   /* Other people's tools sometimes generate headers with an nsyms but
    183      a zero symptr.  */
    184   if (filehdr_dst->f_nsyms != 0 && filehdr_dst->f_symptr == 0)
    185     {
    186       filehdr_dst->f_nsyms = 0;
    187       filehdr_dst->f_flags |= F_LSYMS;
    188     }
    189 
    190   filehdr_dst->f_opthdr = H_GET_16 (abfd, filehdr_src-> f_opthdr);
    191 }
    192 
    193 #ifdef COFF_IMAGE_WITH_PE
    194 # define coff_swap_filehdr_out _bfd_XXi_only_swap_filehdr_out
    195 #elif defined COFF_WITH_pex64
    196 # define coff_swap_filehdr_out _bfd_pex64_only_swap_filehdr_out
    197 #elif defined COFF_WITH_pep
    198 # define coff_swap_filehdr_out _bfd_pep_only_swap_filehdr_out
    199 #else
    200 # define coff_swap_filehdr_out _bfd_pe_only_swap_filehdr_out
    201 #endif
    202 
    203 static void
    204 coff_swap_scnhdr_in (bfd * abfd, void * ext, void * in)
    205 {
    206   SCNHDR *scnhdr_ext = (SCNHDR *) ext;
    207   struct internal_scnhdr *scnhdr_int = (struct internal_scnhdr *) in;
    208 
    209   memcpy (scnhdr_int->s_name, scnhdr_ext->s_name, sizeof (scnhdr_int->s_name));
    210 
    211   scnhdr_int->s_vaddr   = GET_SCNHDR_VADDR (abfd, scnhdr_ext->s_vaddr);
    212   scnhdr_int->s_paddr   = GET_SCNHDR_PADDR (abfd, scnhdr_ext->s_paddr);
    213   scnhdr_int->s_size    = GET_SCNHDR_SIZE (abfd, scnhdr_ext->s_size);
    214   scnhdr_int->s_scnptr  = GET_SCNHDR_SCNPTR (abfd, scnhdr_ext->s_scnptr);
    215   scnhdr_int->s_relptr  = GET_SCNHDR_RELPTR (abfd, scnhdr_ext->s_relptr);
    216   scnhdr_int->s_lnnoptr = GET_SCNHDR_LNNOPTR (abfd, scnhdr_ext->s_lnnoptr);
    217   scnhdr_int->s_flags   = H_GET_32 (abfd, scnhdr_ext->s_flags);
    218 
    219   /* MS handles overflow of line numbers by carrying into the reloc
    220      field (it appears).  Since it's supposed to be zero for PE
    221      *IMAGE* format, that's safe.  This is still a bit iffy.  */
    222 #ifdef COFF_IMAGE_WITH_PE
    223   scnhdr_int->s_nlnno = (H_GET_16 (abfd, scnhdr_ext->s_nlnno)
    224 			 + (H_GET_16 (abfd, scnhdr_ext->s_nreloc) << 16));
    225   scnhdr_int->s_nreloc = 0;
    226 #else
    227   scnhdr_int->s_nreloc = H_GET_16 (abfd, scnhdr_ext->s_nreloc);
    228   scnhdr_int->s_nlnno = H_GET_16 (abfd, scnhdr_ext->s_nlnno);
    229 #endif
    230 
    231   if (scnhdr_int->s_vaddr != 0)
    232     {
    233       scnhdr_int->s_vaddr += pe_data (abfd)->pe_opthdr.ImageBase;
    234       /* Do not cut upper 32-bits for 64-bit vma.  */
    235 #ifndef COFF_WITH_pex64
    236       scnhdr_int->s_vaddr &= 0xffffffff;
    237 #endif
    238     }
    239 
    240 #ifndef COFF_NO_HACK_SCNHDR_SIZE
    241   /* If this section holds uninitialized data and is from an object file
    242      or from an executable image that has not initialized the field,
    243      or if the image is an executable file and the physical size is padded,
    244      use the virtual size (stored in s_paddr) instead.  */
    245   if (scnhdr_int->s_paddr > 0
    246       && (((scnhdr_int->s_flags & IMAGE_SCN_CNT_UNINITIALIZED_DATA) != 0
    247 	   && (! bfd_pei_p (abfd) || scnhdr_int->s_size == 0))
    248           || (bfd_pei_p (abfd) && (scnhdr_int->s_size > scnhdr_int->s_paddr))))
    249   /* This code used to set scnhdr_int->s_paddr to 0.  However,
    250      coff_set_alignment_hook stores s_paddr in virt_size, which
    251      only works if it correctly holds the virtual size of the
    252      section.  */
    253     scnhdr_int->s_size = scnhdr_int->s_paddr;
    254 #endif
    255 }
    256 
    257 static bfd_boolean
    258 pe_mkobject (bfd * abfd)
    259 {
    260   pe_data_type *pe;
    261   bfd_size_type amt = sizeof (pe_data_type);
    262 
    263   abfd->tdata.pe_obj_data = (struct pe_tdata *) bfd_zalloc (abfd, amt);
    264 
    265   if (abfd->tdata.pe_obj_data == 0)
    266     return FALSE;
    267 
    268   pe = pe_data (abfd);
    269 
    270   pe->coff.pe = 1;
    271 
    272   /* in_reloc_p is architecture dependent.  */
    273   pe->in_reloc_p = in_reloc_p;
    274 
    275   memset (& pe->pe_opthdr, 0, sizeof pe->pe_opthdr);
    276   return TRUE;
    277 }
    278 
    279 /* Create the COFF backend specific information.  */
    280 
    281 static void *
    282 pe_mkobject_hook (bfd * abfd,
    283 		  void * filehdr,
    284 		  void * aouthdr ATTRIBUTE_UNUSED)
    285 {
    286   struct internal_filehdr *internal_f = (struct internal_filehdr *) filehdr;
    287   pe_data_type *pe;
    288 
    289   if (! pe_mkobject (abfd))
    290     return NULL;
    291 
    292   pe = pe_data (abfd);
    293   pe->coff.sym_filepos = internal_f->f_symptr;
    294   /* These members communicate important constants about the symbol
    295      table to GDB's symbol-reading code.  These `constants'
    296      unfortunately vary among coff implementations...  */
    297   pe->coff.local_n_btmask = N_BTMASK;
    298   pe->coff.local_n_btshft = N_BTSHFT;
    299   pe->coff.local_n_tmask = N_TMASK;
    300   pe->coff.local_n_tshift = N_TSHIFT;
    301   pe->coff.local_symesz = SYMESZ;
    302   pe->coff.local_auxesz = AUXESZ;
    303   pe->coff.local_linesz = LINESZ;
    304 
    305   pe->coff.timestamp = internal_f->f_timdat;
    306 
    307   obj_raw_syment_count (abfd) =
    308     obj_conv_table_size (abfd) =
    309       internal_f->f_nsyms;
    310 
    311   pe->real_flags = internal_f->f_flags;
    312 
    313   if ((internal_f->f_flags & F_DLL) != 0)
    314     pe->dll = 1;
    315 
    316   if ((internal_f->f_flags & IMAGE_FILE_DEBUG_STRIPPED) == 0)
    317     abfd->flags |= HAS_DEBUG;
    318 
    319 #ifdef COFF_IMAGE_WITH_PE
    320   if (aouthdr)
    321     pe->pe_opthdr = ((struct internal_aouthdr *) aouthdr)->pe;
    322 #endif
    323 
    324 #ifdef ARM
    325   if (! _bfd_coff_arm_set_private_flags (abfd, internal_f->f_flags))
    326     coff_data (abfd) ->flags = 0;
    327 #endif
    328 
    329   return (void *) pe;
    330 }
    331 
    332 static bfd_boolean
    333 pe_print_private_bfd_data (bfd *abfd, void * vfile)
    334 {
    335   FILE *file = (FILE *) vfile;
    336 
    337   if (!_bfd_XX_print_private_bfd_data_common (abfd, vfile))
    338     return FALSE;
    339 
    340   if (pe_saved_coff_bfd_print_private_bfd_data == NULL)
    341     return TRUE;
    342 
    343   fputc ('\n', file);
    344 
    345   return pe_saved_coff_bfd_print_private_bfd_data (abfd, vfile);
    346 }
    347 
    348 /* Copy any private info we understand from the input bfd
    349    to the output bfd.  */
    350 
    351 static bfd_boolean
    352 pe_bfd_copy_private_bfd_data (bfd *ibfd, bfd *obfd)
    353 {
    354   /* PR binutils/716: Copy the large address aware flag.
    355      XXX: Should we be copying other flags or other fields in the pe_data()
    356      structure ?  */
    357   if (pe_data (obfd) != NULL
    358       && pe_data (ibfd) != NULL
    359       && pe_data (ibfd)->real_flags & IMAGE_FILE_LARGE_ADDRESS_AWARE)
    360     pe_data (obfd)->real_flags |= IMAGE_FILE_LARGE_ADDRESS_AWARE;
    361 
    362   if (!_bfd_XX_bfd_copy_private_bfd_data_common (ibfd, obfd))
    363     return FALSE;
    364 
    365   if (pe_saved_coff_bfd_copy_private_bfd_data)
    366     return pe_saved_coff_bfd_copy_private_bfd_data (ibfd, obfd);
    367 
    368   return TRUE;
    369 }
    370 
    371 #define coff_bfd_copy_private_section_data \
    372   _bfd_XX_bfd_copy_private_section_data
    373 
    374 #define coff_get_symbol_info _bfd_XX_get_symbol_info
    375 
    376 #ifdef COFF_IMAGE_WITH_PE
    377 
    378 /* Code to handle Microsoft's Image Library Format.
    380    Also known as LINK6 format.
    381    Documentation about this format can be found at:
    382 
    383    http://msdn.microsoft.com/library/specs/pecoff_section8.htm  */
    384 
    385 /* The following constants specify the sizes of the various data
    386    structures that we have to create in order to build a bfd describing
    387    an ILF object file.  The final "+ 1" in the definitions of SIZEOF_IDATA6
    388    and SIZEOF_IDATA7 below is to allow for the possibility that we might
    389    need a padding byte in order to ensure 16 bit alignment for the section's
    390    contents.
    391 
    392    The value for SIZEOF_ILF_STRINGS is computed as follows:
    393 
    394       There will be NUM_ILF_SECTIONS section symbols.  Allow 9 characters
    395       per symbol for their names (longest section name is .idata$x).
    396 
    397       There will be two symbols for the imported value, one the symbol name
    398       and one with _imp__ prefixed.  Allowing for the terminating nul's this
    399       is strlen (symbol_name) * 2 + 8 + 21 + strlen (source_dll).
    400 
    401       The strings in the string table must start STRING__SIZE_SIZE bytes into
    402       the table in order to for the string lookup code in coffgen/coffcode to
    403       work.  */
    404 #define NUM_ILF_RELOCS		8
    405 #define NUM_ILF_SECTIONS        6
    406 #define NUM_ILF_SYMS 		(2 + NUM_ILF_SECTIONS)
    407 
    408 #define SIZEOF_ILF_SYMS		 (NUM_ILF_SYMS * sizeof (* vars.sym_cache))
    409 #define SIZEOF_ILF_SYM_TABLE	 (NUM_ILF_SYMS * sizeof (* vars.sym_table))
    410 #define SIZEOF_ILF_NATIVE_SYMS	 (NUM_ILF_SYMS * sizeof (* vars.native_syms))
    411 #define SIZEOF_ILF_SYM_PTR_TABLE (NUM_ILF_SYMS * sizeof (* vars.sym_ptr_table))
    412 #define SIZEOF_ILF_EXT_SYMS	 (NUM_ILF_SYMS * sizeof (* vars.esym_table))
    413 #define SIZEOF_ILF_RELOCS	 (NUM_ILF_RELOCS * sizeof (* vars.reltab))
    414 #define SIZEOF_ILF_INT_RELOCS	 (NUM_ILF_RELOCS * sizeof (* vars.int_reltab))
    415 #define SIZEOF_ILF_STRINGS	 (strlen (symbol_name) * 2 + 8 \
    416 					+ 21 + strlen (source_dll) \
    417 					+ NUM_ILF_SECTIONS * 9 \
    418 					+ STRING_SIZE_SIZE)
    419 #define SIZEOF_IDATA2		(5 * 4)
    420 
    421 /* For PEx64 idata4 & 5 have thumb size of 8 bytes.  */
    422 #ifdef COFF_WITH_pex64
    423 #define SIZEOF_IDATA4		(2 * 4)
    424 #define SIZEOF_IDATA5		(2 * 4)
    425 #else
    426 #define SIZEOF_IDATA4		(1 * 4)
    427 #define SIZEOF_IDATA5		(1 * 4)
    428 #endif
    429 
    430 #define SIZEOF_IDATA6		(2 + strlen (symbol_name) + 1 + 1)
    431 #define SIZEOF_IDATA7		(strlen (source_dll) + 1 + 1)
    432 #define SIZEOF_ILF_SECTIONS     (NUM_ILF_SECTIONS * sizeof (struct coff_section_tdata))
    433 
    434 #define ILF_DATA_SIZE				\
    435     + SIZEOF_ILF_SYMS				\
    436     + SIZEOF_ILF_SYM_TABLE			\
    437     + SIZEOF_ILF_NATIVE_SYMS			\
    438     + SIZEOF_ILF_SYM_PTR_TABLE			\
    439     + SIZEOF_ILF_EXT_SYMS			\
    440     + SIZEOF_ILF_RELOCS				\
    441     + SIZEOF_ILF_INT_RELOCS			\
    442     + SIZEOF_ILF_STRINGS			\
    443     + SIZEOF_IDATA2				\
    444     + SIZEOF_IDATA4				\
    445     + SIZEOF_IDATA5				\
    446     + SIZEOF_IDATA6				\
    447     + SIZEOF_IDATA7				\
    448     + SIZEOF_ILF_SECTIONS			\
    449     + MAX_TEXT_SECTION_SIZE
    450 
    451 /* Create an empty relocation against the given symbol.  */
    452 
    453 static void
    454 pe_ILF_make_a_symbol_reloc (pe_ILF_vars *               vars,
    455 			    bfd_vma                     address,
    456 			    bfd_reloc_code_real_type    reloc,
    457 			    struct bfd_symbol **  	sym,
    458 			    unsigned int                sym_index)
    459 {
    460   arelent * entry;
    461   struct internal_reloc * internal;
    462 
    463   entry = vars->reltab + vars->relcount;
    464   internal = vars->int_reltab + vars->relcount;
    465 
    466   entry->address     = address;
    467   entry->addend      = 0;
    468   entry->howto       = bfd_reloc_type_lookup (vars->abfd, reloc);
    469   entry->sym_ptr_ptr = sym;
    470 
    471   internal->r_vaddr  = address;
    472   internal->r_symndx = sym_index;
    473   internal->r_type   = entry->howto->type;
    474 
    475   vars->relcount ++;
    476 
    477   BFD_ASSERT (vars->relcount <= NUM_ILF_RELOCS);
    478 }
    479 
    480 /* Create an empty relocation against the given section.  */
    481 
    482 static void
    483 pe_ILF_make_a_reloc (pe_ILF_vars *             vars,
    484 		     bfd_vma                   address,
    485 		     bfd_reloc_code_real_type  reloc,
    486 		     asection_ptr              sec)
    487 {
    488   pe_ILF_make_a_symbol_reloc (vars, address, reloc, sec->symbol_ptr_ptr,
    489 			      coff_section_data (vars->abfd, sec)->i);
    490 }
    491 
    492 /* Move the queued relocs into the given section.  */
    493 
    494 static void
    495 pe_ILF_save_relocs (pe_ILF_vars * vars,
    496 		    asection_ptr  sec)
    497 {
    498   /* Make sure that there is somewhere to store the internal relocs.  */
    499   if (coff_section_data (vars->abfd, sec) == NULL)
    500     /* We should probably return an error indication here.  */
    501     abort ();
    502 
    503   coff_section_data (vars->abfd, sec)->relocs = vars->int_reltab;
    504   coff_section_data (vars->abfd, sec)->keep_relocs = TRUE;
    505 
    506   sec->relocation  = vars->reltab;
    507   sec->reloc_count = vars->relcount;
    508   sec->flags      |= SEC_RELOC;
    509 
    510   vars->reltab     += vars->relcount;
    511   vars->int_reltab += vars->relcount;
    512   vars->relcount   = 0;
    513 
    514   BFD_ASSERT ((bfd_byte *) vars->int_reltab < (bfd_byte *) vars->string_table);
    515 }
    516 
    517 /* Create a global symbol and add it to the relevant tables.  */
    518 
    519 static void
    520 pe_ILF_make_a_symbol (pe_ILF_vars *  vars,
    521 		      const char *   prefix,
    522 		      const char *   symbol_name,
    523 		      asection_ptr   section,
    524 		      flagword       extra_flags)
    525 {
    526   coff_symbol_type * sym;
    527   combined_entry_type * ent;
    528   SYMENT * esym;
    529   unsigned short sclass;
    530 
    531   if (extra_flags & BSF_LOCAL)
    532     sclass = C_STAT;
    533   else
    534     sclass = C_EXT;
    535 
    536 #ifdef THUMBPEMAGIC
    537   if (vars->magic == THUMBPEMAGIC)
    538     {
    539       if (extra_flags & BSF_FUNCTION)
    540 	sclass = C_THUMBEXTFUNC;
    541       else if (extra_flags & BSF_LOCAL)
    542 	sclass = C_THUMBSTAT;
    543       else
    544 	sclass = C_THUMBEXT;
    545     }
    546 #endif
    547 
    548   BFD_ASSERT (vars->sym_index < NUM_ILF_SYMS);
    549 
    550   sym = vars->sym_ptr;
    551   ent = vars->native_ptr;
    552   esym = vars->esym_ptr;
    553 
    554   /* Copy the symbol's name into the string table.  */
    555   sprintf (vars->string_ptr, "%s%s", prefix, symbol_name);
    556 
    557   if (section == NULL)
    558     section = bfd_und_section_ptr;
    559 
    560   /* Initialise the external symbol.  */
    561   H_PUT_32 (vars->abfd, vars->string_ptr - vars->string_table,
    562 	    esym->e.e.e_offset);
    563   H_PUT_16 (vars->abfd, section->target_index, esym->e_scnum);
    564   esym->e_sclass[0] = sclass;
    565 
    566   /* The following initialisations are unnecessary - the memory is
    567      zero initialised.  They are just kept here as reminders.  */
    568 
    569   /* Initialise the internal symbol structure.  */
    570   ent->u.syment.n_sclass          = sclass;
    571   ent->u.syment.n_scnum           = section->target_index;
    572   ent->u.syment._n._n_n._n_offset = (bfd_hostptr_t) sym;
    573   ent->is_sym = TRUE;
    574 
    575   sym->symbol.the_bfd = vars->abfd;
    576   sym->symbol.name    = vars->string_ptr;
    577   sym->symbol.flags   = BSF_EXPORT | BSF_GLOBAL | extra_flags;
    578   sym->symbol.section = section;
    579   sym->native         = ent;
    580 
    581   * vars->table_ptr = vars->sym_index;
    582   * vars->sym_ptr_ptr = sym;
    583 
    584   /* Adjust pointers for the next symbol.  */
    585   vars->sym_index ++;
    586   vars->sym_ptr ++;
    587   vars->sym_ptr_ptr ++;
    588   vars->table_ptr ++;
    589   vars->native_ptr ++;
    590   vars->esym_ptr ++;
    591   vars->string_ptr += strlen (symbol_name) + strlen (prefix) + 1;
    592 
    593   BFD_ASSERT (vars->string_ptr < vars->end_string_ptr);
    594 }
    595 
    596 /* Create a section.  */
    597 
    598 static asection_ptr
    599 pe_ILF_make_a_section (pe_ILF_vars * vars,
    600 		       const char *  name,
    601 		       unsigned int  size,
    602 		       flagword      extra_flags)
    603 {
    604   asection_ptr sec;
    605   flagword     flags;
    606 
    607   sec = bfd_make_section_old_way (vars->abfd, name);
    608   if (sec == NULL)
    609     return NULL;
    610 
    611   flags = SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_KEEP | SEC_IN_MEMORY;
    612 
    613   bfd_set_section_flags (vars->abfd, sec, flags | extra_flags);
    614 
    615   (void) bfd_set_section_alignment (vars->abfd, sec, 2);
    616 
    617   /* Check that we will not run out of space.  */
    618   BFD_ASSERT (vars->data + size < vars->bim->buffer + vars->bim->size);
    619 
    620   /* Set the section size and contents.  The actual
    621      contents are filled in by our parent.  */
    622   bfd_set_section_size (vars->abfd, sec, (bfd_size_type) size);
    623   sec->contents = vars->data;
    624   sec->target_index = vars->sec_index ++;
    625 
    626   /* Advance data pointer in the vars structure.  */
    627   vars->data += size;
    628 
    629   /* Skip the padding byte if it was not needed.
    630      The logic here is that if the string length is odd,
    631      then the entire string length, including the null byte,
    632      is even and so the extra, padding byte, is not needed.  */
    633   if (size & 1)
    634     vars->data --;
    635 
    636   /* Create a coff_section_tdata structure for our use.  */
    637   sec->used_by_bfd = (struct coff_section_tdata *) vars->data;
    638   vars->data += sizeof (struct coff_section_tdata);
    639 
    640   BFD_ASSERT (vars->data <= vars->bim->buffer + vars->bim->size);
    641 
    642   /* Create a symbol to refer to this section.  */
    643   pe_ILF_make_a_symbol (vars, "", name, sec, BSF_LOCAL);
    644 
    645   /* Cache the index to the symbol in the coff_section_data structure.  */
    646   coff_section_data (vars->abfd, sec)->i = vars->sym_index - 1;
    647 
    648   return sec;
    649 }
    650 
    651 /* This structure contains the code that goes into the .text section
    652    in order to perform a jump into the DLL lookup table.  The entries
    653    in the table are index by the magic number used to represent the
    654    machine type in the PE file.  The contents of the data[] arrays in
    655    these entries are stolen from the jtab[] arrays in ld/pe-dll.c.
    656    The SIZE field says how many bytes in the DATA array are actually
    657    used.  The OFFSET field says where in the data array the address
    658    of the .idata$5 section should be placed.  */
    659 #define MAX_TEXT_SECTION_SIZE 32
    660 
    661 typedef struct
    662 {
    663   unsigned short magic;
    664   unsigned char  data[MAX_TEXT_SECTION_SIZE];
    665   unsigned int   size;
    666   unsigned int   offset;
    667 }
    668 jump_table;
    669 
    670 static jump_table jtab[] =
    671 {
    672 #ifdef I386MAGIC
    673   { I386MAGIC,
    674     { 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, 0x90, 0x90 },
    675     8, 2
    676   },
    677 #endif
    678 
    679 #ifdef AMD64MAGIC
    680   { AMD64MAGIC,
    681     { 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, 0x90, 0x90 },
    682     8, 2
    683   },
    684 #endif
    685 
    686 #ifdef  MC68MAGIC
    687   { MC68MAGIC,
    688     { /* XXX fill me in */ },
    689     0, 0
    690   },
    691 #endif
    692 
    693 #ifdef  MIPS_ARCH_MAGIC_WINCE
    694   { MIPS_ARCH_MAGIC_WINCE,
    695     { 0x00, 0x00, 0x08, 0x3c, 0x00, 0x00, 0x08, 0x8d,
    696       0x08, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00 },
    697     16, 0
    698   },
    699 #endif
    700 
    701 #ifdef  SH_ARCH_MAGIC_WINCE
    702   { SH_ARCH_MAGIC_WINCE,
    703     { 0x01, 0xd0, 0x02, 0x60, 0x2b, 0x40,
    704       0x09, 0x00, 0x00, 0x00, 0x00, 0x00 },
    705     12, 8
    706   },
    707 #endif
    708 
    709 #ifdef  ARMPEMAGIC
    710   { ARMPEMAGIC,
    711     { 0x00, 0xc0, 0x9f, 0xe5, 0x00, 0xf0,
    712       0x9c, 0xe5, 0x00, 0x00, 0x00, 0x00},
    713     12, 8
    714   },
    715 #endif
    716 
    717 #ifdef  THUMBPEMAGIC
    718   { THUMBPEMAGIC,
    719     { 0x40, 0xb4, 0x02, 0x4e, 0x36, 0x68, 0xb4, 0x46,
    720       0x40, 0xbc, 0x60, 0x47, 0x00, 0x00, 0x00, 0x00 },
    721     16, 12
    722   },
    723 #endif
    724   { 0, { 0 }, 0, 0 }
    725 };
    726 
    727 #ifndef NUM_ENTRIES
    728 #define NUM_ENTRIES(a) (sizeof (a) / sizeof (a)[0])
    729 #endif
    730 
    731 /* Build a full BFD from the information supplied in a ILF object.  */
    732 
    733 static bfd_boolean
    734 pe_ILF_build_a_bfd (bfd *           abfd,
    735 		    unsigned int    magic,
    736 		    char *          symbol_name,
    737 		    char *          source_dll,
    738 		    unsigned int    ordinal,
    739 		    unsigned int    types)
    740 {
    741   bfd_byte *               ptr;
    742   pe_ILF_vars              vars;
    743   struct internal_filehdr  internal_f;
    744   unsigned int             import_type;
    745   unsigned int             import_name_type;
    746   asection_ptr             id4, id5, id6 = NULL, text = NULL;
    747   coff_symbol_type **      imp_sym;
    748   unsigned int             imp_index;
    749 
    750   /* Decode and verify the types field of the ILF structure.  */
    751   import_type = types & 0x3;
    752   import_name_type = (types & 0x1c) >> 2;
    753 
    754   switch (import_type)
    755     {
    756     case IMPORT_CODE:
    757     case IMPORT_DATA:
    758       break;
    759 
    760     case IMPORT_CONST:
    761       /* XXX code yet to be written.  */
    762       _bfd_error_handler (_("%B: Unhandled import type; %x"),
    763 			  abfd, import_type);
    764       return FALSE;
    765 
    766     default:
    767       _bfd_error_handler (_("%B: Unrecognised import type; %x"),
    768 			  abfd, import_type);
    769       return FALSE;
    770     }
    771 
    772   switch (import_name_type)
    773     {
    774     case IMPORT_ORDINAL:
    775     case IMPORT_NAME:
    776     case IMPORT_NAME_NOPREFIX:
    777     case IMPORT_NAME_UNDECORATE:
    778       break;
    779 
    780     default:
    781       _bfd_error_handler (_("%B: Unrecognised import name type; %x"),
    782 			  abfd, import_name_type);
    783       return FALSE;
    784     }
    785 
    786   /* Initialise local variables.
    787 
    788      Note these are kept in a structure rather than being
    789      declared as statics since bfd frowns on global variables.
    790 
    791      We are going to construct the contents of the BFD in memory,
    792      so allocate all the space that we will need right now.  */
    793   vars.bim
    794     = (struct bfd_in_memory *) bfd_malloc ((bfd_size_type) sizeof (*vars.bim));
    795   if (vars.bim == NULL)
    796     return FALSE;
    797 
    798   ptr = (bfd_byte *) bfd_zmalloc ((bfd_size_type) ILF_DATA_SIZE);
    799   vars.bim->buffer = ptr;
    800   vars.bim->size   = ILF_DATA_SIZE;
    801   if (ptr == NULL)
    802     goto error_return;
    803 
    804   /* Initialise the pointers to regions of the memory and the
    805      other contents of the pe_ILF_vars structure as well.  */
    806   vars.sym_cache = (coff_symbol_type *) ptr;
    807   vars.sym_ptr   = (coff_symbol_type *) ptr;
    808   vars.sym_index = 0;
    809   ptr += SIZEOF_ILF_SYMS;
    810 
    811   vars.sym_table = (unsigned int *) ptr;
    812   vars.table_ptr = (unsigned int *) ptr;
    813   ptr += SIZEOF_ILF_SYM_TABLE;
    814 
    815   vars.native_syms = (combined_entry_type *) ptr;
    816   vars.native_ptr  = (combined_entry_type *) ptr;
    817   ptr += SIZEOF_ILF_NATIVE_SYMS;
    818 
    819   vars.sym_ptr_table = (coff_symbol_type **) ptr;
    820   vars.sym_ptr_ptr   = (coff_symbol_type **) ptr;
    821   ptr += SIZEOF_ILF_SYM_PTR_TABLE;
    822 
    823   vars.esym_table = (SYMENT *) ptr;
    824   vars.esym_ptr   = (SYMENT *) ptr;
    825   ptr += SIZEOF_ILF_EXT_SYMS;
    826 
    827   vars.reltab   = (arelent *) ptr;
    828   vars.relcount = 0;
    829   ptr += SIZEOF_ILF_RELOCS;
    830 
    831   vars.int_reltab  = (struct internal_reloc *) ptr;
    832   ptr += SIZEOF_ILF_INT_RELOCS;
    833 
    834   vars.string_table = (char *) ptr;
    835   vars.string_ptr   = (char *) ptr + STRING_SIZE_SIZE;
    836   ptr += SIZEOF_ILF_STRINGS;
    837   vars.end_string_ptr = (char *) ptr;
    838 
    839   /* The remaining space in bim->buffer is used
    840      by the pe_ILF_make_a_section() function.  */
    841   vars.data = ptr;
    842   vars.abfd = abfd;
    843   vars.sec_index = 0;
    844   vars.magic = magic;
    845 
    846   /* Create the initial .idata$<n> sections:
    847      [.idata$2:  Import Directory Table -- not needed]
    848      .idata$4:  Import Lookup Table
    849      .idata$5:  Import Address Table
    850 
    851      Note we do not create a .idata$3 section as this is
    852      created for us by the linker script.  */
    853   id4 = pe_ILF_make_a_section (& vars, ".idata$4", SIZEOF_IDATA4, 0);
    854   id5 = pe_ILF_make_a_section (& vars, ".idata$5", SIZEOF_IDATA5, 0);
    855   if (id4 == NULL || id5 == NULL)
    856     goto error_return;
    857 
    858   /* Fill in the contents of these sections.  */
    859   if (import_name_type == IMPORT_ORDINAL)
    860     {
    861       if (ordinal == 0)
    862 	/* XXX - treat as IMPORT_NAME ??? */
    863 	abort ();
    864 
    865 #ifdef COFF_WITH_pex64
    866       ((unsigned int *) id4->contents)[0] = ordinal;
    867       ((unsigned int *) id4->contents)[1] = 0x80000000;
    868       ((unsigned int *) id5->contents)[0] = ordinal;
    869       ((unsigned int *) id5->contents)[1] = 0x80000000;
    870 #else
    871       * (unsigned int *) id4->contents = ordinal | 0x80000000;
    872       * (unsigned int *) id5->contents = ordinal | 0x80000000;
    873 #endif
    874     }
    875   else
    876     {
    877       char * symbol;
    878       unsigned int len;
    879 
    880       /* Create .idata$6 - the Hint Name Table.  */
    881       id6 = pe_ILF_make_a_section (& vars, ".idata$6", SIZEOF_IDATA6, 0);
    882       if (id6 == NULL)
    883 	goto error_return;
    884 
    885       /* If necessary, trim the import symbol name.  */
    886       symbol = symbol_name;
    887 
    888       /* As used by MS compiler, '_', '@', and '?' are alternative
    889 	 forms of USER_LABEL_PREFIX, with '?' for c++ mangled names,
    890 	 '@' used for fastcall (in C),  '_' everywhere else.  Only one
    891 	 of these is used for a symbol.  We strip this leading char for
    892 	 IMPORT_NAME_NOPREFIX and IMPORT_NAME_UNDECORATE as per the
    893 	 PE COFF 6.0 spec (section 8.3, Import Name Type).  */
    894 
    895       if (import_name_type != IMPORT_NAME)
    896 	{
    897 	  char c = symbol[0];
    898 
    899 	  /* Check that we don't remove for targets with empty
    900 	     USER_LABEL_PREFIX the leading underscore.  */
    901 	  if ((c == '_' && abfd->xvec->symbol_leading_char != 0)
    902 	      || c == '@' || c == '?')
    903 	    symbol++;
    904 	}
    905 
    906       len = strlen (symbol);
    907       if (import_name_type == IMPORT_NAME_UNDECORATE)
    908 	{
    909 	  /* Truncate at the first '@'.  */
    910 	  char *at = strchr (symbol, '@');
    911 
    912 	  if (at != NULL)
    913 	    len = at - symbol;
    914 	}
    915 
    916       id6->contents[0] = ordinal & 0xff;
    917       id6->contents[1] = ordinal >> 8;
    918 
    919       memcpy ((char *) id6->contents + 2, symbol, len);
    920       id6->contents[len + 2] = '\0';
    921     }
    922 
    923   if (import_name_type != IMPORT_ORDINAL)
    924     {
    925       pe_ILF_make_a_reloc (&vars, (bfd_vma) 0, BFD_RELOC_RVA, id6);
    926       pe_ILF_save_relocs (&vars, id4);
    927 
    928       pe_ILF_make_a_reloc (&vars, (bfd_vma) 0, BFD_RELOC_RVA, id6);
    929       pe_ILF_save_relocs (&vars, id5);
    930     }
    931 
    932   /* Create extra sections depending upon the type of import we are dealing with.  */
    933   switch (import_type)
    934     {
    935       int i;
    936 
    937     case IMPORT_CODE:
    938       /* Create a .text section.
    939 	 First we need to look up its contents in the jump table.  */
    940       for (i = NUM_ENTRIES (jtab); i--;)
    941 	{
    942 	  if (jtab[i].size == 0)
    943 	    continue;
    944 	  if (jtab[i].magic == magic)
    945 	    break;
    946 	}
    947       /* If we did not find a matching entry something is wrong.  */
    948       if (i < 0)
    949 	abort ();
    950 
    951       /* Create the .text section.  */
    952       text = pe_ILF_make_a_section (& vars, ".text", jtab[i].size, SEC_CODE);
    953       if (text == NULL)
    954 	goto error_return;
    955 
    956       /* Copy in the jump code.  */
    957       memcpy (text->contents, jtab[i].data, jtab[i].size);
    958 
    959       /* Create an import symbol.  */
    960       pe_ILF_make_a_symbol (& vars, "__imp_", symbol_name, id5, 0);
    961       imp_sym   = vars.sym_ptr_ptr - 1;
    962       imp_index = vars.sym_index - 1;
    963 
    964       /* Create a reloc for the data in the text section.  */
    965 #ifdef MIPS_ARCH_MAGIC_WINCE
    966       if (magic == MIPS_ARCH_MAGIC_WINCE)
    967 	{
    968 	  pe_ILF_make_a_symbol_reloc (&vars, (bfd_vma) 0, BFD_RELOC_HI16_S,
    969 				      (struct bfd_symbol **) imp_sym,
    970 				      imp_index);
    971 	  pe_ILF_make_a_reloc (&vars, (bfd_vma) 0, BFD_RELOC_LO16, text);
    972 	  pe_ILF_make_a_symbol_reloc (&vars, (bfd_vma) 4, BFD_RELOC_LO16,
    973 				      (struct bfd_symbol **) imp_sym,
    974 				      imp_index);
    975 	}
    976       else
    977 #endif
    978 	pe_ILF_make_a_symbol_reloc (&vars, (bfd_vma) jtab[i].offset,
    979 				    BFD_RELOC_32, (asymbol **) imp_sym,
    980 				    imp_index);
    981 
    982       pe_ILF_save_relocs (& vars, text);
    983       break;
    984 
    985     case IMPORT_DATA:
    986       break;
    987 
    988     default:
    989       /* XXX code not yet written.  */
    990       abort ();
    991     }
    992 
    993   /* Initialise the bfd.  */
    994   memset (& internal_f, 0, sizeof (internal_f));
    995 
    996   internal_f.f_magic  = magic;
    997   internal_f.f_symptr = 0;
    998   internal_f.f_nsyms  = 0;
    999   internal_f.f_flags  = F_AR32WR | F_LNNO; /* XXX is this correct ?  */
   1000 
   1001   if (   ! bfd_set_start_address (abfd, (bfd_vma) 0)
   1002       || ! bfd_coff_set_arch_mach_hook (abfd, & internal_f))
   1003     goto error_return;
   1004 
   1005   if (bfd_coff_mkobject_hook (abfd, (void *) & internal_f, NULL) == NULL)
   1006     goto error_return;
   1007 
   1008   coff_data (abfd)->pe = 1;
   1009 #ifdef THUMBPEMAGIC
   1010   if (vars.magic == THUMBPEMAGIC)
   1011     /* Stop some linker warnings about thumb code not supporting interworking.  */
   1012     coff_data (abfd)->flags |= F_INTERWORK | F_INTERWORK_SET;
   1013 #endif
   1014 
   1015   /* Switch from file contents to memory contents.  */
   1016   bfd_cache_close (abfd);
   1017 
   1018   abfd->iostream = (void *) vars.bim;
   1019   abfd->flags |= BFD_IN_MEMORY /* | HAS_LOCALS */;
   1020   abfd->iovec = &_bfd_memory_iovec;
   1021   abfd->where = 0;
   1022   abfd->origin = 0;
   1023   obj_sym_filepos (abfd) = 0;
   1024 
   1025   /* Now create a symbol describing the imported value.  */
   1026   switch (import_type)
   1027     {
   1028     case IMPORT_CODE:
   1029       pe_ILF_make_a_symbol (& vars, "", symbol_name, text,
   1030 			    BSF_NOT_AT_END | BSF_FUNCTION);
   1031 
   1032       /* Create an import symbol for the DLL, without the
   1033        .dll suffix.  */
   1034       ptr = (bfd_byte *) strrchr (source_dll, '.');
   1035       if (ptr)
   1036 	* ptr = 0;
   1037       pe_ILF_make_a_symbol (& vars, "__IMPORT_DESCRIPTOR_", source_dll, NULL, 0);
   1038       if (ptr)
   1039 	* ptr = '.';
   1040       break;
   1041 
   1042     case IMPORT_DATA:
   1043       /* Nothing to do here.  */
   1044       break;
   1045 
   1046     default:
   1047       /* XXX code not yet written.  */
   1048       abort ();
   1049     }
   1050 
   1051   /* Point the bfd at the symbol table.  */
   1052   obj_symbols (abfd) = vars.sym_cache;
   1053   bfd_get_symcount (abfd) = vars.sym_index;
   1054 
   1055   obj_raw_syments (abfd) = vars.native_syms;
   1056   obj_raw_syment_count (abfd) = vars.sym_index;
   1057 
   1058   obj_coff_external_syms (abfd) = (void *) vars.esym_table;
   1059   obj_coff_keep_syms (abfd) = TRUE;
   1060 
   1061   obj_convert (abfd) = vars.sym_table;
   1062   obj_conv_table_size (abfd) = vars.sym_index;
   1063 
   1064   obj_coff_strings (abfd) = vars.string_table;
   1065   obj_coff_keep_strings (abfd) = TRUE;
   1066 
   1067   abfd->flags |= HAS_SYMS;
   1068 
   1069   return TRUE;
   1070 
   1071  error_return:
   1072   if (vars.bim->buffer != NULL)
   1073     free (vars.bim->buffer);
   1074   free (vars.bim);
   1075   return FALSE;
   1076 }
   1077 
   1078 /* We have detected a Image Library Format archive element.
   1079    Decode the element and return the appropriate target.  */
   1080 
   1081 static const bfd_target *
   1082 pe_ILF_object_p (bfd * abfd)
   1083 {
   1084   bfd_byte        buffer[14];
   1085   bfd_byte *      ptr;
   1086   char *          symbol_name;
   1087   char *          source_dll;
   1088   unsigned int    machine;
   1089   bfd_size_type   size;
   1090   unsigned int    ordinal;
   1091   unsigned int    types;
   1092   unsigned int    magic;
   1093 
   1094   /* Upon entry the first six bytes of the ILF header have
   1095       already been read.  Now read the rest of the header.  */
   1096   if (bfd_bread (buffer, (bfd_size_type) 14, abfd) != 14)
   1097     return NULL;
   1098 
   1099   ptr = buffer;
   1100 
   1101   machine = H_GET_16 (abfd, ptr);
   1102   ptr += 2;
   1103 
   1104   /* Check that the machine type is recognised.  */
   1105   magic = 0;
   1106 
   1107   switch (machine)
   1108     {
   1109     case IMAGE_FILE_MACHINE_UNKNOWN:
   1110     case IMAGE_FILE_MACHINE_ALPHA:
   1111     case IMAGE_FILE_MACHINE_ALPHA64:
   1112     case IMAGE_FILE_MACHINE_IA64:
   1113       break;
   1114 
   1115     case IMAGE_FILE_MACHINE_I386:
   1116 #ifdef I386MAGIC
   1117       magic = I386MAGIC;
   1118 #endif
   1119       break;
   1120 
   1121     case IMAGE_FILE_MACHINE_AMD64:
   1122 #ifdef AMD64MAGIC
   1123       magic = AMD64MAGIC;
   1124 #endif
   1125       break;
   1126 
   1127     case IMAGE_FILE_MACHINE_M68K:
   1128 #ifdef MC68AGIC
   1129       magic = MC68MAGIC;
   1130 #endif
   1131       break;
   1132 
   1133     case IMAGE_FILE_MACHINE_R3000:
   1134     case IMAGE_FILE_MACHINE_R4000:
   1135     case IMAGE_FILE_MACHINE_R10000:
   1136 
   1137     case IMAGE_FILE_MACHINE_MIPS16:
   1138     case IMAGE_FILE_MACHINE_MIPSFPU:
   1139     case IMAGE_FILE_MACHINE_MIPSFPU16:
   1140 #ifdef MIPS_ARCH_MAGIC_WINCE
   1141       magic = MIPS_ARCH_MAGIC_WINCE;
   1142 #endif
   1143       break;
   1144 
   1145     case IMAGE_FILE_MACHINE_SH3:
   1146     case IMAGE_FILE_MACHINE_SH4:
   1147 #ifdef SH_ARCH_MAGIC_WINCE
   1148       magic = SH_ARCH_MAGIC_WINCE;
   1149 #endif
   1150       break;
   1151 
   1152     case IMAGE_FILE_MACHINE_ARM:
   1153 #ifdef ARMPEMAGIC
   1154       magic = ARMPEMAGIC;
   1155 #endif
   1156       break;
   1157 
   1158     case IMAGE_FILE_MACHINE_THUMB:
   1159 #ifdef THUMBPEMAGIC
   1160       {
   1161 	extern const bfd_target TARGET_LITTLE_SYM;
   1162 
   1163 	if (abfd->xvec == & TARGET_LITTLE_SYM)
   1164 	  magic = THUMBPEMAGIC;
   1165       }
   1166 #endif
   1167       break;
   1168 
   1169     case IMAGE_FILE_MACHINE_POWERPC:
   1170       /* We no longer support PowerPC.  */
   1171     default:
   1172       _bfd_error_handler
   1173 	(_("%B: Unrecognised machine type (0x%x)"
   1174 	   " in Import Library Format archive"),
   1175 	 abfd, machine);
   1176       bfd_set_error (bfd_error_malformed_archive);
   1177 
   1178       return NULL;
   1179       break;
   1180     }
   1181 
   1182   if (magic == 0)
   1183     {
   1184       _bfd_error_handler
   1185 	(_("%B: Recognised but unhandled machine type (0x%x)"
   1186 	   " in Import Library Format archive"),
   1187 	 abfd, machine);
   1188       bfd_set_error (bfd_error_wrong_format);
   1189 
   1190       return NULL;
   1191     }
   1192 
   1193   /* We do not bother to check the date.
   1194      date = H_GET_32 (abfd, ptr);  */
   1195   ptr += 4;
   1196 
   1197   size = H_GET_32 (abfd, ptr);
   1198   ptr += 4;
   1199 
   1200   if (size == 0)
   1201     {
   1202       _bfd_error_handler
   1203 	(_("%B: size field is zero in Import Library Format header"), abfd);
   1204       bfd_set_error (bfd_error_malformed_archive);
   1205 
   1206       return NULL;
   1207     }
   1208 
   1209   ordinal = H_GET_16 (abfd, ptr);
   1210   ptr += 2;
   1211 
   1212   types = H_GET_16 (abfd, ptr);
   1213   /* ptr += 2; */
   1214 
   1215   /* Now read in the two strings that follow.  */
   1216   ptr = (bfd_byte *) bfd_alloc (abfd, size);
   1217   if (ptr == NULL)
   1218     return NULL;
   1219 
   1220   if (bfd_bread (ptr, size, abfd) != size)
   1221     {
   1222       bfd_release (abfd, ptr);
   1223       return NULL;
   1224     }
   1225 
   1226   symbol_name = (char *) ptr;
   1227   source_dll  = symbol_name + strlen (symbol_name) + 1;
   1228 
   1229   /* Verify that the strings are null terminated.  */
   1230   if (ptr[size - 1] != 0
   1231       || (bfd_size_type) ((bfd_byte *) source_dll - ptr) >= size)
   1232     {
   1233       _bfd_error_handler
   1234 	(_("%B: string not null terminated in ILF object file."), abfd);
   1235       bfd_set_error (bfd_error_malformed_archive);
   1236       bfd_release (abfd, ptr);
   1237       return NULL;
   1238     }
   1239 
   1240   /* Now construct the bfd.  */
   1241   if (! pe_ILF_build_a_bfd (abfd, magic, symbol_name,
   1242 			    source_dll, ordinal, types))
   1243     {
   1244       bfd_release (abfd, ptr);
   1245       return NULL;
   1246     }
   1247 
   1248   return abfd->xvec;
   1249 }
   1250 
   1251 static const bfd_target *
   1252 pe_bfd_object_p (bfd * abfd)
   1253 {
   1254   bfd_byte buffer[6];
   1255   struct external_PEI_DOS_hdr dos_hdr;
   1256   struct external_PEI_IMAGE_hdr image_hdr;
   1257   struct internal_filehdr internal_f;
   1258   struct internal_aouthdr internal_a;
   1259   file_ptr opt_hdr_size;
   1260   file_ptr offset;
   1261 
   1262   /* Detect if this a Microsoft Import Library Format element.  */
   1263   /* First read the beginning of the header.  */
   1264   if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0
   1265       || bfd_bread (buffer, (bfd_size_type) 6, abfd) != 6)
   1266     {
   1267       if (bfd_get_error () != bfd_error_system_call)
   1268 	bfd_set_error (bfd_error_wrong_format);
   1269       return NULL;
   1270     }
   1271 
   1272   /* Then check the magic and the version (only 0 is supported).  */
   1273   if (H_GET_32 (abfd, buffer) == 0xffff0000
   1274       && H_GET_16 (abfd, buffer + 4) == 0)
   1275     return pe_ILF_object_p (abfd);
   1276 
   1277   if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0
   1278       || bfd_bread (&dos_hdr, (bfd_size_type) sizeof (dos_hdr), abfd)
   1279 	 != sizeof (dos_hdr))
   1280     {
   1281       if (bfd_get_error () != bfd_error_system_call)
   1282 	bfd_set_error (bfd_error_wrong_format);
   1283       return NULL;
   1284     }
   1285 
   1286   /* There are really two magic numbers involved; the magic number
   1287      that says this is a NT executable (PEI) and the magic number that
   1288      determines the architecture.  The former is DOSMAGIC, stored in
   1289      the e_magic field.  The latter is stored in the f_magic field.
   1290      If the NT magic number isn't valid, the architecture magic number
   1291      could be mimicked by some other field (specifically, the number
   1292      of relocs in section 3).  Since this routine can only be called
   1293      correctly for a PEI file, check the e_magic number here, and, if
   1294      it doesn't match, clobber the f_magic number so that we don't get
   1295      a false match.  */
   1296   if (H_GET_16 (abfd, dos_hdr.e_magic) != DOSMAGIC)
   1297     {
   1298       bfd_set_error (bfd_error_wrong_format);
   1299       return NULL;
   1300     }
   1301 
   1302   offset = H_GET_32 (abfd, dos_hdr.e_lfanew);
   1303   if (bfd_seek (abfd, offset, SEEK_SET) != 0
   1304       || (bfd_bread (&image_hdr, (bfd_size_type) sizeof (image_hdr), abfd)
   1305 	  != sizeof (image_hdr)))
   1306     {
   1307       if (bfd_get_error () != bfd_error_system_call)
   1308 	bfd_set_error (bfd_error_wrong_format);
   1309       return NULL;
   1310     }
   1311 
   1312   if (H_GET_32 (abfd, image_hdr.nt_signature) != 0x4550)
   1313     {
   1314       bfd_set_error (bfd_error_wrong_format);
   1315       return NULL;
   1316     }
   1317 
   1318   /* Swap file header, so that we get the location for calling
   1319      real_object_p.  */
   1320   bfd_coff_swap_filehdr_in (abfd, &image_hdr, &internal_f);
   1321 
   1322   if (! bfd_coff_bad_format_hook (abfd, &internal_f)
   1323       || internal_f.f_opthdr > bfd_coff_aoutsz (abfd))
   1324     {
   1325       bfd_set_error (bfd_error_wrong_format);
   1326       return NULL;
   1327     }
   1328 
   1329   /* Read the optional header, which has variable size.  */
   1330   opt_hdr_size = internal_f.f_opthdr;
   1331 
   1332   if (opt_hdr_size != 0)
   1333     {
   1334       bfd_size_type amt = opt_hdr_size;
   1335       void * opthdr;
   1336 
   1337       /* PR 17521 file: 230-131433-0.004.  */
   1338       if (amt < sizeof (PEAOUTHDR))
   1339 	amt = sizeof (PEAOUTHDR);
   1340 
   1341       opthdr = bfd_zalloc (abfd, amt);
   1342       if (opthdr == NULL)
   1343 	return NULL;
   1344       if (bfd_bread (opthdr, opt_hdr_size, abfd)
   1345 	  != (bfd_size_type) opt_hdr_size)
   1346 	return NULL;
   1347 
   1348       bfd_coff_swap_aouthdr_in (abfd, opthdr, & internal_a);
   1349     }
   1350 
   1351   return coff_real_object_p (abfd, internal_f.f_nscns, &internal_f,
   1352                             (opt_hdr_size != 0
   1353                              ? &internal_a
   1354                              : (struct internal_aouthdr *) NULL));
   1355 }
   1356 
   1357 #define coff_object_p pe_bfd_object_p
   1358 #endif /* COFF_IMAGE_WITH_PE */
   1359