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