OSDN Git Service

* peXXigen.c (_bfd_XX_print_private_bfd_data_common): Copy
[pf3gnuchains/pf3gnuchains3x.git] / bfd / peXXigen.c
1 /* Support for the generic parts of PE/PEI; the common executable parts.
2    Copyright 1995, 1996, 1997, 1998, 1999, 2000, 2001
3    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 <sac@cygnus.com>.
23
24    PE/PEI rearrangement (and code added): Donn Terry
25                                           Softway Systems, Inc.
26 */
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
57 /* This expands into COFF_WITH_pe or COFF_WITH_pep depending on whether
58    we're compiling for straight PE or PE+.  */
59 #define COFF_WITH_XX
60
61 #include "bfd.h"
62 #include "sysdep.h"
63 #include "libbfd.h"
64 #include "coff/internal.h"
65
66 /* NOTE: it's strange to be including an architecture specific header
67    in what's supposed to be general (to PE/PEI) code.  However, that's
68    where the definitions are, and they don't vary per architecture
69    within PE/PEI, so we get them from there.  FIXME: The lack of
70    variance is an assumption which may prove to be incorrect if new
71    PE/PEI targets are created.  */
72 #ifdef COFF_WITH_pep
73 # include "coff/ia64.h"
74 #else
75 # include "coff/i386.h"
76 #endif
77
78 #include "coff/pe.h"
79 #include "libcoff.h"
80 #include "libpei.h"
81
82 #ifdef COFF_WITH_pep
83 # undef AOUTSZ
84 # define AOUTSZ         PEPAOUTSZ
85 # define PEAOUTHDR      PEPAOUTHDR
86 #endif
87
88 /* FIXME: This file has various tests of POWERPC_LE_PE.  Those tests
89    worked when the code was in peicode.h, but no longer work now that
90    the code is in peigen.c.  PowerPC NT is said to be dead.  If
91    anybody wants to revive the code, you will have to figure out how
92    to handle those issues.  */
93
94 static void add_data_entry
95   PARAMS ((bfd *, struct internal_extra_pe_aouthdr *, int, char *, bfd_vma));
96 static boolean pe_print_pdata PARAMS ((bfd *, PTR));
97 static boolean pe_print_reloc PARAMS ((bfd *, PTR));
98 static boolean pe_print_idata PARAMS ((bfd *, PTR));
99 static boolean pe_print_edata PARAMS ((bfd *, PTR));
100 \f
101
102 void
103 _bfd_XXi_swap_sym_in (abfd, ext1, in1)
104      bfd *abfd;
105      PTR ext1;
106      PTR in1;
107 {
108   SYMENT *ext = (SYMENT *) ext1;
109   struct internal_syment *in = (struct internal_syment *) in1;
110
111   if (ext->e.e_name[0] == 0)
112     {
113       in->_n._n_n._n_zeroes = 0;
114       in->_n._n_n._n_offset = H_GET_32 (abfd, ext->e.e.e_offset);
115     }
116   else
117     memcpy (in->_n._n_name, ext->e.e_name, SYMNMLEN);
118
119   in->n_value = H_GET_32 (abfd, ext->e_value);
120   in->n_scnum = H_GET_16 (abfd, ext->e_scnum);
121
122   if (sizeof (ext->e_type) == 2)
123     in->n_type = H_GET_16 (abfd, ext->e_type);
124   else
125     in->n_type = H_GET_32 (abfd, ext->e_type);
126
127   in->n_sclass = H_GET_8 (abfd, ext->e_sclass);
128   in->n_numaux = H_GET_8 (abfd, ext->e_numaux);
129
130 #ifndef STRICT_PE_FORMAT
131   /* This is for Gnu-created DLLs.  */
132
133   /* The section symbols for the .idata$ sections have class 0x68
134      (C_SECTION), which MS documentation indicates is a section
135      symbol.  Unfortunately, the value field in the symbol is simply a
136      copy of the .idata section's flags rather than something useful.
137      When these symbols are encountered, change the value to 0 so that
138      they will be handled somewhat correctly in the bfd code.  */
139   if (in->n_sclass == C_SECTION)
140     {
141       in->n_value = 0x0;
142
143 #if 0
144       /* FIXME: This is clearly wrong.  The problem seems to be that
145          undefined C_SECTION symbols appear in the first object of a
146          MS generated .lib file, and the symbols are not defined
147          anywhere.  */
148       in->n_scnum = 1;
149
150       /* I have tried setting the class to 3 and using the following
151          to set the section number.  This will put the address of the
152          pointer to the string kernel32.dll at addresses 0 and 0x10
153          off start of idata section which is not correct.  */
154 #if 0
155       if (strcmp (in->_n._n_name, ".idata$4") == 0)
156         in->n_scnum = 3;
157       else
158         in->n_scnum = 2;
159 #endif
160 #else
161       /* Create synthetic empty sections as needed.  DJ */
162       if (in->n_scnum == 0)
163         {
164           asection *sec;
165
166           for (sec = abfd->sections; sec; sec = sec->next)
167             {
168               if (strcmp (sec->name, in->n_name) == 0)
169                 {
170                   in->n_scnum = sec->target_index;
171                   break;
172                 }
173             }
174         }
175
176       if (in->n_scnum == 0)
177         {
178           int unused_section_number = 0;
179           asection *sec;
180           char *name;
181
182           for (sec = abfd->sections; sec; sec = sec->next)
183             if (unused_section_number <= sec->target_index)
184               unused_section_number = sec->target_index + 1;
185
186           name = bfd_alloc (abfd, (bfd_size_type) strlen (in->n_name) + 10);
187           if (name == NULL)
188             return;
189           strcpy (name, in->n_name);
190           sec = bfd_make_section_anyway (abfd, name);
191
192           sec->vma = 0;
193           sec->lma = 0;
194           sec->_cooked_size = 0;
195           sec->_raw_size = 0;
196           sec->filepos = 0;
197           sec->rel_filepos = 0;
198           sec->reloc_count = 0;
199           sec->line_filepos = 0;
200           sec->lineno_count = 0;
201           sec->userdata = NULL;
202           sec->next = (asection *) NULL;
203           sec->flags = 0;
204           sec->alignment_power = 2;
205           sec->flags = SEC_HAS_CONTENTS | SEC_ALLOC | SEC_DATA | SEC_LOAD;
206
207           sec->target_index = unused_section_number;
208
209           in->n_scnum = unused_section_number;
210         }
211       in->n_sclass = C_STAT;
212 #endif
213     }
214 #endif
215
216 #ifdef coff_swap_sym_in_hook
217   /* This won't work in peigen.c, but since it's for PPC PE, it's not
218      worth fixing.  */
219   coff_swap_sym_in_hook (abfd, ext1, in1);
220 #endif
221 }
222
223 unsigned int
224 _bfd_XXi_swap_sym_out (abfd, inp, extp)
225      bfd       *abfd;
226      PTR        inp;
227      PTR        extp;
228 {
229   struct internal_syment *in = (struct internal_syment *) inp;
230   SYMENT *ext = (SYMENT *) extp;
231
232   if (in->_n._n_name[0] == 0)
233     {
234       H_PUT_32 (abfd, 0, ext->e.e.e_zeroes);
235       H_PUT_32 (abfd, in->_n._n_n._n_offset, ext->e.e.e_offset);
236     }
237   else
238     memcpy (ext->e.e_name, in->_n._n_name, SYMNMLEN);
239
240   H_PUT_32 (abfd, in->n_value, ext->e_value);
241   H_PUT_16 (abfd, in->n_scnum, ext->e_scnum);
242
243   if (sizeof (ext->e_type) == 2)
244     H_PUT_16 (abfd, in->n_type, ext->e_type);
245   else
246     H_PUT_32 (abfd, in->n_type, ext->e_type);
247
248   H_PUT_8 (abfd, in->n_sclass, ext->e_sclass);
249   H_PUT_8 (abfd, in->n_numaux, ext->e_numaux);
250
251   return SYMESZ;
252 }
253
254 void
255 _bfd_XXi_swap_aux_in (abfd, ext1, type, class, indx, numaux, in1)
256      bfd            *abfd;
257      PTR             ext1;
258      int             type;
259      int             class;
260      int             indx ATTRIBUTE_UNUSED;
261      int             numaux ATTRIBUTE_UNUSED;
262      PTR             in1;
263 {
264   AUXENT *ext = (AUXENT *) ext1;
265   union internal_auxent *in = (union internal_auxent *) in1;
266
267   switch (class)
268     {
269     case C_FILE:
270       if (ext->x_file.x_fname[0] == 0)
271         {
272           in->x_file.x_n.x_zeroes = 0;
273           in->x_file.x_n.x_offset = H_GET_32 (abfd, ext->x_file.x_n.x_offset);
274         }
275       else
276         memcpy (in->x_file.x_fname, ext->x_file.x_fname, FILNMLEN);
277       return;
278
279     case C_STAT:
280     case C_LEAFSTAT:
281     case C_HIDDEN:
282       if (type == T_NULL)
283         {
284           in->x_scn.x_scnlen = GET_SCN_SCNLEN (abfd, ext);
285           in->x_scn.x_nreloc = GET_SCN_NRELOC (abfd, ext);
286           in->x_scn.x_nlinno = GET_SCN_NLINNO (abfd, ext);
287           in->x_scn.x_checksum = H_GET_32 (abfd, ext->x_scn.x_checksum);
288           in->x_scn.x_associated = H_GET_16 (abfd, ext->x_scn.x_associated);
289           in->x_scn.x_comdat = H_GET_8 (abfd, ext->x_scn.x_comdat);
290           return;
291         }
292       break;
293     }
294
295   in->x_sym.x_tagndx.l = H_GET_32 (abfd, ext->x_sym.x_tagndx);
296   in->x_sym.x_tvndx = H_GET_16 (abfd, ext->x_sym.x_tvndx);
297
298   if (class == C_BLOCK || class == C_FCN || ISFCN (type) || ISTAG (class))
299     {
300       in->x_sym.x_fcnary.x_fcn.x_lnnoptr = GET_FCN_LNNOPTR (abfd, ext);
301       in->x_sym.x_fcnary.x_fcn.x_endndx.l = GET_FCN_ENDNDX (abfd, ext);
302     }
303   else
304     {
305       in->x_sym.x_fcnary.x_ary.x_dimen[0] =
306         H_GET_16 (abfd, ext->x_sym.x_fcnary.x_ary.x_dimen[0]);
307       in->x_sym.x_fcnary.x_ary.x_dimen[1] =
308         H_GET_16 (abfd, ext->x_sym.x_fcnary.x_ary.x_dimen[1]);
309       in->x_sym.x_fcnary.x_ary.x_dimen[2] =
310         H_GET_16 (abfd, ext->x_sym.x_fcnary.x_ary.x_dimen[2]);
311       in->x_sym.x_fcnary.x_ary.x_dimen[3] =
312         H_GET_16 (abfd, ext->x_sym.x_fcnary.x_ary.x_dimen[3]);
313     }
314
315   if (ISFCN (type))
316     {
317       in->x_sym.x_misc.x_fsize = H_GET_32 (abfd, ext->x_sym.x_misc.x_fsize);
318     }
319   else
320     {
321       in->x_sym.x_misc.x_lnsz.x_lnno = GET_LNSZ_LNNO (abfd, ext);
322       in->x_sym.x_misc.x_lnsz.x_size = GET_LNSZ_SIZE (abfd, ext);
323     }
324 }
325
326 unsigned int
327 _bfd_XXi_swap_aux_out (abfd, inp, type, class, indx, numaux, extp)
328      bfd  *abfd;
329      PTR   inp;
330      int   type;
331      int   class;
332      int   indx ATTRIBUTE_UNUSED;
333      int   numaux ATTRIBUTE_UNUSED;
334      PTR   extp;
335 {
336   union internal_auxent *in = (union internal_auxent *) inp;
337   AUXENT *ext = (AUXENT *) extp;
338
339   memset ((PTR) ext, 0, AUXESZ);
340   switch (class)
341     {
342     case C_FILE:
343       if (in->x_file.x_fname[0] == 0)
344         {
345           H_PUT_32 (abfd, 0, ext->x_file.x_n.x_zeroes);
346           H_PUT_32 (abfd, in->x_file.x_n.x_offset, ext->x_file.x_n.x_offset);
347         }
348       else
349         memcpy (ext->x_file.x_fname, in->x_file.x_fname, FILNMLEN);
350
351       return AUXESZ;
352
353     case C_STAT:
354     case C_LEAFSTAT:
355     case C_HIDDEN:
356       if (type == T_NULL)
357         {
358           PUT_SCN_SCNLEN (abfd, in->x_scn.x_scnlen, ext);
359           PUT_SCN_NRELOC (abfd, in->x_scn.x_nreloc, ext);
360           PUT_SCN_NLINNO (abfd, in->x_scn.x_nlinno, ext);
361           H_PUT_32 (abfd, in->x_scn.x_checksum, ext->x_scn.x_checksum);
362           H_PUT_16 (abfd, in->x_scn.x_associated, ext->x_scn.x_associated);
363           H_PUT_8 (abfd, in->x_scn.x_comdat, ext->x_scn.x_comdat);
364           return AUXESZ;
365         }
366       break;
367     }
368
369   H_PUT_32 (abfd, in->x_sym.x_tagndx.l, ext->x_sym.x_tagndx);
370   H_PUT_16 (abfd, in->x_sym.x_tvndx, ext->x_sym.x_tvndx);
371
372   if (class == C_BLOCK || class == C_FCN || ISFCN (type) || ISTAG (class))
373     {
374       PUT_FCN_LNNOPTR (abfd, in->x_sym.x_fcnary.x_fcn.x_lnnoptr,  ext);
375       PUT_FCN_ENDNDX  (abfd, in->x_sym.x_fcnary.x_fcn.x_endndx.l, ext);
376     }
377   else
378     {
379       H_PUT_16 (abfd, in->x_sym.x_fcnary.x_ary.x_dimen[0],
380                 ext->x_sym.x_fcnary.x_ary.x_dimen[0]);
381       H_PUT_16 (abfd, in->x_sym.x_fcnary.x_ary.x_dimen[1],
382                 ext->x_sym.x_fcnary.x_ary.x_dimen[1]);
383       H_PUT_16 (abfd, in->x_sym.x_fcnary.x_ary.x_dimen[2],
384                 ext->x_sym.x_fcnary.x_ary.x_dimen[2]);
385       H_PUT_16 (abfd, in->x_sym.x_fcnary.x_ary.x_dimen[3],
386                 ext->x_sym.x_fcnary.x_ary.x_dimen[3]);
387     }
388
389   if (ISFCN (type))
390     H_PUT_32 (abfd, in->x_sym.x_misc.x_fsize, ext->x_sym.x_misc.x_fsize);
391   else
392     {
393       PUT_LNSZ_LNNO (abfd, in->x_sym.x_misc.x_lnsz.x_lnno, ext);
394       PUT_LNSZ_SIZE (abfd, in->x_sym.x_misc.x_lnsz.x_size, ext);
395     }
396
397   return AUXESZ;
398 }
399
400 void
401 _bfd_XXi_swap_lineno_in (abfd, ext1, in1)
402      bfd *abfd;
403      PTR ext1;
404      PTR in1;
405 {
406   LINENO *ext = (LINENO *) ext1;
407   struct internal_lineno *in = (struct internal_lineno *) in1;
408
409   in->l_addr.l_symndx = H_GET_32 (abfd, ext->l_addr.l_symndx);
410   in->l_lnno = GET_LINENO_LNNO (abfd, ext);
411 }
412
413 unsigned int
414 _bfd_XXi_swap_lineno_out (abfd, inp, outp)
415      bfd       *abfd;
416      PTR        inp;
417      PTR        outp;
418 {
419   struct internal_lineno *in = (struct internal_lineno *) inp;
420   struct external_lineno *ext = (struct external_lineno *) outp;
421   H_PUT_32 (abfd, in->l_addr.l_symndx, ext->l_addr.l_symndx);
422
423   PUT_LINENO_LNNO (abfd, in->l_lnno, ext);
424   return LINESZ;
425 }
426
427 void
428 _bfd_XXi_swap_aouthdr_in (abfd, aouthdr_ext1, aouthdr_int1)
429      bfd *abfd;
430      PTR aouthdr_ext1;
431      PTR aouthdr_int1;
432 {
433   struct internal_extra_pe_aouthdr *a;
434   PEAOUTHDR *src = (PEAOUTHDR *) (aouthdr_ext1);
435   AOUTHDR        *aouthdr_ext = (AOUTHDR *) aouthdr_ext1;
436   struct internal_aouthdr *aouthdr_int = (struct internal_aouthdr *)aouthdr_int1;
437
438   aouthdr_int->magic = H_GET_16 (abfd, aouthdr_ext->magic);
439   aouthdr_int->vstamp = H_GET_16 (abfd, aouthdr_ext->vstamp);
440   aouthdr_int->tsize = GET_AOUTHDR_TSIZE (abfd, aouthdr_ext->tsize);
441   aouthdr_int->dsize = GET_AOUTHDR_DSIZE (abfd, aouthdr_ext->dsize);
442   aouthdr_int->bsize = GET_AOUTHDR_BSIZE (abfd, aouthdr_ext->bsize);
443   aouthdr_int->entry = GET_AOUTHDR_ENTRY (abfd, aouthdr_ext->entry);
444   aouthdr_int->text_start =
445     GET_AOUTHDR_TEXT_START (abfd, aouthdr_ext->text_start);
446 #ifndef COFF_WITH_pep
447   /* PE32+ does not have data_start member! */
448   aouthdr_int->data_start =
449     GET_AOUTHDR_DATA_START (abfd, aouthdr_ext->data_start);
450 #endif
451
452   a = &aouthdr_int->pe;
453   a->ImageBase = GET_OPTHDR_IMAGE_BASE (abfd, src->ImageBase);
454   a->SectionAlignment = H_GET_32 (abfd, src->SectionAlignment);
455   a->FileAlignment = H_GET_32 (abfd, src->FileAlignment);
456   a->MajorOperatingSystemVersion =
457     H_GET_16 (abfd, src->MajorOperatingSystemVersion);
458   a->MinorOperatingSystemVersion =
459     H_GET_16 (abfd, src->MinorOperatingSystemVersion);
460   a->MajorImageVersion = H_GET_16 (abfd, src->MajorImageVersion);
461   a->MinorImageVersion = H_GET_16 (abfd, src->MinorImageVersion);
462   a->MajorSubsystemVersion = H_GET_16 (abfd, src->MajorSubsystemVersion);
463   a->MinorSubsystemVersion = H_GET_16 (abfd, src->MinorSubsystemVersion);
464   a->Reserved1 = H_GET_32 (abfd, src->Reserved1);
465   a->SizeOfImage = H_GET_32 (abfd, src->SizeOfImage);
466   a->SizeOfHeaders = H_GET_32 (abfd, src->SizeOfHeaders);
467   a->CheckSum = H_GET_32 (abfd, src->CheckSum);
468   a->Subsystem = H_GET_16 (abfd, src->Subsystem);
469   a->DllCharacteristics = H_GET_16 (abfd, src->DllCharacteristics);
470   a->SizeOfStackReserve =
471     GET_OPTHDR_SIZE_OF_STACK_RESERVE (abfd, src->SizeOfStackReserve);
472   a->SizeOfStackCommit =
473     GET_OPTHDR_SIZE_OF_STACK_COMMIT (abfd, src->SizeOfStackCommit);
474   a->SizeOfHeapReserve =
475     GET_OPTHDR_SIZE_OF_HEAP_RESERVE (abfd, src->SizeOfHeapReserve);
476   a->SizeOfHeapCommit =
477     GET_OPTHDR_SIZE_OF_HEAP_COMMIT (abfd, src->SizeOfHeapCommit);
478   a->LoaderFlags = H_GET_32 (abfd, src->LoaderFlags);
479   a->NumberOfRvaAndSizes = H_GET_32 (abfd, src->NumberOfRvaAndSizes);
480
481   {
482     int idx;
483
484     for (idx = 0; idx < 16; idx++)
485       {
486         /* If data directory is empty, rva also should be 0.  */
487         int size =
488           H_GET_32 (abfd, src->DataDirectory[idx][1]);
489         a->DataDirectory[idx].Size = size;
490
491         if (size)
492           a->DataDirectory[idx].VirtualAddress =
493             H_GET_32 (abfd, src->DataDirectory[idx][0]);
494         else
495           a->DataDirectory[idx].VirtualAddress = 0;
496       }
497   }
498
499   if (aouthdr_int->entry)
500     {
501       aouthdr_int->entry += a->ImageBase;
502 #ifndef COFF_WITH_pep
503       aouthdr_int->entry &= 0xffffffff;
504 #endif
505     }
506
507   if (aouthdr_int->tsize)
508     {
509       aouthdr_int->text_start += a->ImageBase;
510 #ifndef COFF_WITH_pep
511       aouthdr_int->text_start &= 0xffffffff;
512 #endif
513     }
514
515 #ifndef COFF_WITH_pep
516   /* PE32+ does not have data_start member! */
517   if (aouthdr_int->dsize)
518     {
519       aouthdr_int->data_start += a->ImageBase;
520       aouthdr_int->data_start &= 0xffffffff;
521     }
522 #endif
523
524 #ifdef POWERPC_LE_PE
525   /* These three fields are normally set up by ppc_relocate_section.
526      In the case of reading a file in, we can pick them up from the
527      DataDirectory.  */
528   first_thunk_address = a->DataDirectory[12].VirtualAddress;
529   thunk_size = a->DataDirectory[12].Size;
530   import_table_size = a->DataDirectory[1].Size;
531 #endif
532 }
533
534 /* A support function for below.  */
535
536 static void
537 add_data_entry (abfd, aout, idx, name, base)
538      bfd *abfd;
539      struct internal_extra_pe_aouthdr *aout;
540      int idx;
541      char *name;
542      bfd_vma base;
543 {
544   asection *sec = bfd_get_section_by_name (abfd, name);
545
546   /* Add import directory information if it exists.  */
547   if ((sec != NULL)
548       && (coff_section_data (abfd, sec) != NULL)
549       && (pei_section_data (abfd, sec) != NULL))
550     {
551       /* If data directory is empty, rva also should be 0.  */
552       int size = pei_section_data (abfd, sec)->virt_size;
553       aout->DataDirectory[idx].Size = size;
554
555       if (size)
556         {
557           aout->DataDirectory[idx].VirtualAddress =
558             (sec->vma - base) & 0xffffffff;
559           sec->flags |= SEC_DATA;
560         }
561     }
562 }
563
564 unsigned int
565 _bfd_XXi_swap_aouthdr_out (abfd, in, out)
566      bfd       *abfd;
567      PTR        in;
568      PTR        out;
569 {
570   struct internal_aouthdr *aouthdr_in = (struct internal_aouthdr *) in;
571   pe_data_type *pe = pe_data (abfd);
572   struct internal_extra_pe_aouthdr *extra = &pe->pe_opthdr;
573   PEAOUTHDR *aouthdr_out = (PEAOUTHDR *) out;
574   bfd_vma sa, fa, ib;
575
576   if (pe->force_minimum_alignment)
577     {
578       if (!extra->FileAlignment)
579         extra->FileAlignment = PE_DEF_FILE_ALIGNMENT;
580       if (!extra->SectionAlignment)
581         extra->SectionAlignment = PE_DEF_SECTION_ALIGNMENT;
582     }
583
584   if (extra->Subsystem == IMAGE_SUBSYSTEM_UNKNOWN)
585     extra->Subsystem = pe->target_subsystem;
586
587   sa = extra->SectionAlignment;
588   fa = extra->FileAlignment;
589   ib = extra->ImageBase;
590
591   if (aouthdr_in->tsize)
592     {
593       aouthdr_in->text_start -= ib;
594 #ifndef COFF_WITH_pep
595       aouthdr_in->text_start &= 0xffffffff;
596 #endif
597     }
598
599   if (aouthdr_in->dsize)
600     {
601       aouthdr_in->data_start -= ib;
602 #ifndef COFF_WITH_pep
603       aouthdr_in->data_start &= 0xffffffff;
604 #endif
605     }
606
607   if (aouthdr_in->entry)
608     {
609       aouthdr_in->entry -= ib;
610 #ifndef COFF_WITH_pep
611       aouthdr_in->entry &= 0xffffffff;
612 #endif
613     }
614
615 #define FA(x) (((x) + fa -1 ) & (- fa))
616 #define SA(x) (((x) + sa -1 ) & (- sa))
617
618   /* We like to have the sizes aligned.  */
619
620   aouthdr_in->bsize = FA (aouthdr_in->bsize);
621
622   extra->NumberOfRvaAndSizes = IMAGE_NUMBEROF_DIRECTORY_ENTRIES;
623
624   /* first null out all data directory entries ..  */
625   memset (extra->DataDirectory, sizeof (extra->DataDirectory), 0);
626
627   add_data_entry (abfd, extra, 0, ".edata", ib);
628
629   /* Don't call add_data_entry for .idata$2 or .idata$5.  It's done in
630      bfd_coff_final_link where all the required information is
631      available.  */
632
633   /* However, until other .idata fixes are made (pending patch), the
634      entry for .idata is needed for backwards compatability.  FIXME.  */
635   add_data_entry (abfd, extra, 1, ".idata", ib);
636
637   add_data_entry (abfd, extra, 2, ".rsrc", ib);
638
639   add_data_entry (abfd, extra, 3, ".pdata", ib);
640
641   /* For some reason, the virtual size (which is what's set by
642      add_data_entry) for .reloc is not the same as the size recorded
643      in this slot by MSVC; it doesn't seem to cause problems (so far),
644      but since it's the best we've got, use it.  It does do the right
645      thing for .pdata.  */
646   if (pe->has_reloc_section)
647     add_data_entry (abfd, extra, 5, ".reloc", ib);
648
649   {
650     asection *sec;
651     bfd_vma dsize = 0;
652     bfd_vma isize = SA(abfd->sections->filepos);
653     bfd_vma tsize = 0;
654
655     for (sec = abfd->sections; sec; sec = sec->next)
656       {
657         int rounded = FA(sec->_raw_size);
658
659         if (sec->flags & SEC_DATA)
660           dsize += rounded;
661         if (sec->flags & SEC_CODE)
662           tsize += rounded;
663         /* The image size is the total VIRTUAL size (which is what is
664            in the virt_size field).  Files have been seen (from MSVC
665            5.0 link.exe) where the file size of the .data segment is
666            quite small compared to the virtual size.  Without this
667            fix, strip munges the file.  */
668         isize += SA (FA (pei_section_data (abfd, sec)->virt_size));
669       }
670
671     aouthdr_in->dsize = dsize;
672     aouthdr_in->tsize = tsize;
673     extra->SizeOfImage = isize;
674   }
675
676   extra->SizeOfHeaders = abfd->sections->filepos;
677   H_PUT_16 (abfd, aouthdr_in->magic, aouthdr_out->standard.magic);
678
679 #define LINKER_VERSION 256 /* That is, 2.56 */
680
681   /* This piece of magic sets the "linker version" field to
682      LINKER_VERSION.  */
683   H_PUT_16 (abfd, (LINKER_VERSION / 100 + (LINKER_VERSION % 100) * 256),
684             aouthdr_out->standard.vstamp);
685
686   PUT_AOUTHDR_TSIZE (abfd, aouthdr_in->tsize, aouthdr_out->standard.tsize);
687   PUT_AOUTHDR_DSIZE (abfd, aouthdr_in->dsize, aouthdr_out->standard.dsize);
688   PUT_AOUTHDR_BSIZE (abfd, aouthdr_in->bsize, aouthdr_out->standard.bsize);
689   PUT_AOUTHDR_ENTRY (abfd, aouthdr_in->entry, aouthdr_out->standard.entry);
690   PUT_AOUTHDR_TEXT_START (abfd, aouthdr_in->text_start,
691                           aouthdr_out->standard.text_start);
692
693 #ifndef COFF_WITH_pep
694   /* PE32+ does not have data_start member! */
695   PUT_AOUTHDR_DATA_START (abfd, aouthdr_in->data_start,
696                           aouthdr_out->standard.data_start);
697 #endif
698
699   PUT_OPTHDR_IMAGE_BASE (abfd, extra->ImageBase, aouthdr_out->ImageBase);
700   H_PUT_32 (abfd, extra->SectionAlignment, aouthdr_out->SectionAlignment);
701   H_PUT_32 (abfd, extra->FileAlignment, aouthdr_out->FileAlignment);
702   H_PUT_16 (abfd, extra->MajorOperatingSystemVersion,
703             aouthdr_out->MajorOperatingSystemVersion);
704   H_PUT_16 (abfd, extra->MinorOperatingSystemVersion,
705             aouthdr_out->MinorOperatingSystemVersion);
706   H_PUT_16 (abfd, extra->MajorImageVersion, aouthdr_out->MajorImageVersion);
707   H_PUT_16 (abfd, extra->MinorImageVersion, aouthdr_out->MinorImageVersion);
708   H_PUT_16 (abfd, extra->MajorSubsystemVersion,
709             aouthdr_out->MajorSubsystemVersion);
710   H_PUT_16 (abfd, extra->MinorSubsystemVersion,
711             aouthdr_out->MinorSubsystemVersion);
712   H_PUT_32 (abfd, extra->Reserved1, aouthdr_out->Reserved1);
713   H_PUT_32 (abfd, extra->SizeOfImage, aouthdr_out->SizeOfImage);
714   H_PUT_32 (abfd, extra->SizeOfHeaders, aouthdr_out->SizeOfHeaders);
715   H_PUT_32 (abfd, extra->CheckSum, aouthdr_out->CheckSum);
716   H_PUT_16 (abfd, extra->Subsystem, aouthdr_out->Subsystem);
717   H_PUT_16 (abfd, extra->DllCharacteristics, aouthdr_out->DllCharacteristics);
718   PUT_OPTHDR_SIZE_OF_STACK_RESERVE (abfd, extra->SizeOfStackReserve,
719                                     aouthdr_out->SizeOfStackReserve);
720   PUT_OPTHDR_SIZE_OF_STACK_COMMIT (abfd, extra->SizeOfStackCommit,
721                                    aouthdr_out->SizeOfStackCommit);
722   PUT_OPTHDR_SIZE_OF_HEAP_RESERVE (abfd, extra->SizeOfHeapReserve,
723                                    aouthdr_out->SizeOfHeapReserve);
724   PUT_OPTHDR_SIZE_OF_HEAP_COMMIT (abfd, extra->SizeOfHeapCommit,
725                                   aouthdr_out->SizeOfHeapCommit);
726   H_PUT_32 (abfd, extra->LoaderFlags, aouthdr_out->LoaderFlags);
727   H_PUT_32 (abfd, extra->NumberOfRvaAndSizes,
728             aouthdr_out->NumberOfRvaAndSizes);
729   {
730     int idx;
731
732     for (idx = 0; idx < 16; idx++)
733       {
734         H_PUT_32 (abfd, extra->DataDirectory[idx].VirtualAddress,
735                   aouthdr_out->DataDirectory[idx][0]);
736         H_PUT_32 (abfd, extra->DataDirectory[idx].Size,
737                   aouthdr_out->DataDirectory[idx][1]);
738       }
739   }
740
741   return AOUTSZ;
742 }
743
744 unsigned int
745 _bfd_XXi_only_swap_filehdr_out (abfd, in, out)
746      bfd       *abfd;
747      PTR        in;
748      PTR        out;
749 {
750   int idx;
751   struct internal_filehdr *filehdr_in = (struct internal_filehdr *) in;
752   struct external_PEI_filehdr *filehdr_out = (struct external_PEI_filehdr *) out;
753
754   if (pe_data (abfd)->has_reloc_section)
755     filehdr_in->f_flags &= ~F_RELFLG;
756
757   if (pe_data (abfd)->dll)
758     filehdr_in->f_flags |= F_DLL;
759
760   filehdr_in->pe.e_magic    = DOSMAGIC;
761   filehdr_in->pe.e_cblp     = 0x90;
762   filehdr_in->pe.e_cp       = 0x3;
763   filehdr_in->pe.e_crlc     = 0x0;
764   filehdr_in->pe.e_cparhdr  = 0x4;
765   filehdr_in->pe.e_minalloc = 0x0;
766   filehdr_in->pe.e_maxalloc = 0xffff;
767   filehdr_in->pe.e_ss       = 0x0;
768   filehdr_in->pe.e_sp       = 0xb8;
769   filehdr_in->pe.e_csum     = 0x0;
770   filehdr_in->pe.e_ip       = 0x0;
771   filehdr_in->pe.e_cs       = 0x0;
772   filehdr_in->pe.e_lfarlc   = 0x40;
773   filehdr_in->pe.e_ovno     = 0x0;
774
775   for (idx = 0; idx < 4; idx++)
776     filehdr_in->pe.e_res[idx] = 0x0;
777
778   filehdr_in->pe.e_oemid   = 0x0;
779   filehdr_in->pe.e_oeminfo = 0x0;
780
781   for (idx = 0; idx < 10; idx++)
782     filehdr_in->pe.e_res2[idx] = 0x0;
783
784   filehdr_in->pe.e_lfanew = 0x80;
785
786   /* This next collection of data are mostly just characters.  It
787      appears to be constant within the headers put on NT exes.  */
788   filehdr_in->pe.dos_message[0]  = 0x0eba1f0e;
789   filehdr_in->pe.dos_message[1]  = 0xcd09b400;
790   filehdr_in->pe.dos_message[2]  = 0x4c01b821;
791   filehdr_in->pe.dos_message[3]  = 0x685421cd;
792   filehdr_in->pe.dos_message[4]  = 0x70207369;
793   filehdr_in->pe.dos_message[5]  = 0x72676f72;
794   filehdr_in->pe.dos_message[6]  = 0x63206d61;
795   filehdr_in->pe.dos_message[7]  = 0x6f6e6e61;
796   filehdr_in->pe.dos_message[8]  = 0x65622074;
797   filehdr_in->pe.dos_message[9]  = 0x6e757220;
798   filehdr_in->pe.dos_message[10] = 0x206e6920;
799   filehdr_in->pe.dos_message[11] = 0x20534f44;
800   filehdr_in->pe.dos_message[12] = 0x65646f6d;
801   filehdr_in->pe.dos_message[13] = 0x0a0d0d2e;
802   filehdr_in->pe.dos_message[14] = 0x24;
803   filehdr_in->pe.dos_message[15] = 0x0;
804   filehdr_in->pe.nt_signature = NT_SIGNATURE;
805
806   H_PUT_16 (abfd, filehdr_in->f_magic, filehdr_out->f_magic);
807   H_PUT_16 (abfd, filehdr_in->f_nscns, filehdr_out->f_nscns);
808
809   H_PUT_32 (abfd, time (0), filehdr_out->f_timdat);
810   PUT_FILEHDR_SYMPTR (abfd, filehdr_in->f_symptr,
811                       filehdr_out->f_symptr);
812   H_PUT_32 (abfd, filehdr_in->f_nsyms, filehdr_out->f_nsyms);
813   H_PUT_16 (abfd, filehdr_in->f_opthdr, filehdr_out->f_opthdr);
814   H_PUT_16 (abfd, filehdr_in->f_flags, filehdr_out->f_flags);
815
816   /* Put in extra dos header stuff.  This data remains essentially
817      constant, it just has to be tacked on to the beginning of all exes
818      for NT.  */
819   H_PUT_16 (abfd, filehdr_in->pe.e_magic, filehdr_out->e_magic);
820   H_PUT_16 (abfd, filehdr_in->pe.e_cblp, filehdr_out->e_cblp);
821   H_PUT_16 (abfd, filehdr_in->pe.e_cp, filehdr_out->e_cp);
822   H_PUT_16 (abfd, filehdr_in->pe.e_crlc, filehdr_out->e_crlc);
823   H_PUT_16 (abfd, filehdr_in->pe.e_cparhdr, filehdr_out->e_cparhdr);
824   H_PUT_16 (abfd, filehdr_in->pe.e_minalloc, filehdr_out->e_minalloc);
825   H_PUT_16 (abfd, filehdr_in->pe.e_maxalloc, filehdr_out->e_maxalloc);
826   H_PUT_16 (abfd, filehdr_in->pe.e_ss, filehdr_out->e_ss);
827   H_PUT_16 (abfd, filehdr_in->pe.e_sp, filehdr_out->e_sp);
828   H_PUT_16 (abfd, filehdr_in->pe.e_csum, filehdr_out->e_csum);
829   H_PUT_16 (abfd, filehdr_in->pe.e_ip, filehdr_out->e_ip);
830   H_PUT_16 (abfd, filehdr_in->pe.e_cs, filehdr_out->e_cs);
831   H_PUT_16 (abfd, filehdr_in->pe.e_lfarlc, filehdr_out->e_lfarlc);
832   H_PUT_16 (abfd, filehdr_in->pe.e_ovno, filehdr_out->e_ovno);
833
834   for (idx = 0; idx < 4; idx++)
835     H_PUT_16 (abfd, filehdr_in->pe.e_res[idx], filehdr_out->e_res[idx]);
836
837   H_PUT_16 (abfd, filehdr_in->pe.e_oemid, filehdr_out->e_oemid);
838   H_PUT_16 (abfd, filehdr_in->pe.e_oeminfo, filehdr_out->e_oeminfo);
839
840   for (idx = 0; idx < 10; idx++)
841     H_PUT_16 (abfd, filehdr_in->pe.e_res2[idx], filehdr_out->e_res2[idx]);
842
843   H_PUT_32 (abfd, filehdr_in->pe.e_lfanew, filehdr_out->e_lfanew);
844
845   for (idx = 0; idx < 16; idx++)
846     H_PUT_32 (abfd, filehdr_in->pe.dos_message[idx],
847               filehdr_out->dos_message[idx]);
848
849   /* Also put in the NT signature.  */
850   H_PUT_32 (abfd, filehdr_in->pe.nt_signature, filehdr_out->nt_signature);
851
852   return FILHSZ;
853 }
854
855 unsigned int
856 _bfd_XX_only_swap_filehdr_out (abfd, in, out)
857      bfd       *abfd;
858      PTR        in;
859      PTR        out;
860 {
861   struct internal_filehdr *filehdr_in = (struct internal_filehdr *) in;
862   FILHDR *filehdr_out = (FILHDR *) out;
863
864   H_PUT_16 (abfd, filehdr_in->f_magic, filehdr_out->f_magic);
865   H_PUT_16 (abfd, filehdr_in->f_nscns, filehdr_out->f_nscns);
866   H_PUT_32 (abfd, filehdr_in->f_timdat, filehdr_out->f_timdat);
867   PUT_FILEHDR_SYMPTR (abfd, filehdr_in->f_symptr, filehdr_out->f_symptr);
868   H_PUT_32 (abfd, filehdr_in->f_nsyms, filehdr_out->f_nsyms);
869   H_PUT_16 (abfd, filehdr_in->f_opthdr, filehdr_out->f_opthdr);
870   H_PUT_16 (abfd, filehdr_in->f_flags, filehdr_out->f_flags);
871
872   return FILHSZ;
873 }
874
875 unsigned int
876 _bfd_XXi_swap_scnhdr_out (abfd, in, out)
877      bfd       *abfd;
878      PTR        in;
879      PTR        out;
880 {
881   struct internal_scnhdr *scnhdr_int = (struct internal_scnhdr *) in;
882   SCNHDR *scnhdr_ext = (SCNHDR *) out;
883   unsigned int ret = SCNHSZ;
884   bfd_vma ps;
885   bfd_vma ss;
886
887   memcpy (scnhdr_ext->s_name, scnhdr_int->s_name, sizeof (scnhdr_int->s_name));
888
889   PUT_SCNHDR_VADDR (abfd,
890                     ((scnhdr_int->s_vaddr
891                       - pe_data (abfd)->pe_opthdr.ImageBase)
892                      & 0xffffffff),
893                     scnhdr_ext->s_vaddr);
894
895   /* NT wants the size data to be rounded up to the next
896      NT_FILE_ALIGNMENT, but zero if it has no content (as in .bss,
897      sometimes).  */
898   if ((scnhdr_int->s_flags & IMAGE_SCN_CNT_UNINITIALIZED_DATA) != 0)
899     {
900       ps = scnhdr_int->s_size;
901       ss = 0;
902     }
903   else
904     {
905       ps = scnhdr_int->s_paddr;
906       ss = scnhdr_int->s_size;
907     }
908
909   PUT_SCNHDR_SIZE (abfd, ss,
910                    scnhdr_ext->s_size);
911
912   /* s_paddr in PE is really the virtual size.  */
913   PUT_SCNHDR_PADDR (abfd, ps, scnhdr_ext->s_paddr);
914
915   PUT_SCNHDR_SCNPTR (abfd, scnhdr_int->s_scnptr,
916                      scnhdr_ext->s_scnptr);
917   PUT_SCNHDR_RELPTR (abfd, scnhdr_int->s_relptr,
918                      scnhdr_ext->s_relptr);
919   PUT_SCNHDR_LNNOPTR (abfd, scnhdr_int->s_lnnoptr,
920                       scnhdr_ext->s_lnnoptr);
921
922   /* Extra flags must be set when dealing with NT.  All sections should also
923      have the IMAGE_SCN_MEM_READ (0x40000000) flag set.  In addition, the
924      .text section must have IMAGE_SCN_MEM_EXECUTE (0x20000000) and the data
925      sections (.idata, .data, .bss, .CRT) must have IMAGE_SCN_MEM_WRITE set
926      (this is especially important when dealing with the .idata section since
927      the addresses for routines from .dlls must be overwritten).  If .reloc
928      section data is ever generated, we must add IMAGE_SCN_MEM_DISCARDABLE
929      (0x02000000).  Also, the resource data should also be read and
930      writable.  */
931
932   /* FIXME: alignment is also encoded in this field, at least on ppc (krk) */
933   /* FIXME: even worse, I don't see how to get the original alignment field*/
934   /*        back...                                                        */
935
936   {
937     int flags = scnhdr_int->s_flags;
938
939     H_PUT_32 (abfd, flags, scnhdr_ext->s_flags);
940   }
941
942   if (coff_data (abfd)->link_info
943       && ! coff_data (abfd)->link_info->relocateable
944       && ! coff_data (abfd)->link_info->shared
945       && strcmp (scnhdr_int->s_name, ".text") == 0)
946     {
947       /* By inference from looking at MS output, the 32 bit field
948          which is the combintion of the number_of_relocs and
949          number_of_linenos is used for the line number count in
950          executables.  A 16-bit field won't do for cc1.  The MS
951          document says that the number of relocs is zero for
952          executables, but the 17-th bit has been observed to be there.
953          Overflow is not an issue: a 4G-line program will overflow a
954          bunch of other fields long before this!  */
955       H_PUT_16 (abfd, (scnhdr_int->s_nlnno & 0xffff), scnhdr_ext->s_nlnno);
956       H_PUT_16 (abfd, (scnhdr_int->s_nlnno >> 16), scnhdr_ext->s_nreloc);
957     }
958   else
959     {
960       if (scnhdr_int->s_nlnno <= 0xffff)
961         H_PUT_16 (abfd, scnhdr_int->s_nlnno, scnhdr_ext->s_nlnno);
962       else
963         {
964           (*_bfd_error_handler) (_("%s: line number overflow: 0x%lx > 0xffff"),
965                                  bfd_get_filename (abfd),
966                                  scnhdr_int->s_nlnno);
967           bfd_set_error (bfd_error_file_truncated);
968           H_PUT_16 (abfd, 0xffff, scnhdr_ext->s_nlnno);
969           ret = 0;
970         }
971
972       if (scnhdr_int->s_nreloc <= 0xffff)
973         H_PUT_16 (abfd, scnhdr_int->s_nreloc, scnhdr_ext->s_nreloc);
974       else
975         {
976           /* PE can deal with large #s of relocs, but not here.  */
977           H_PUT_16 (abfd, 0xffff, scnhdr_ext->s_nreloc);
978           scnhdr_int->s_flags |= IMAGE_SCN_LNK_NRELOC_OVFL;
979           H_PUT_32 (abfd, scnhdr_int->s_flags, scnhdr_ext->s_flags);
980 #if 0
981           (*_bfd_error_handler) (_("%s: reloc overflow 1: 0x%lx > 0xffff"),
982                                  bfd_get_filename (abfd),
983                                  scnhdr_int->s_nreloc);
984           bfd_set_error (bfd_error_file_truncated);
985           H_PUT_16 (abfd, 0xffff, scnhdr_ext->s_nreloc);
986           ret = 0;
987 #endif
988         }
989     }
990   return ret;
991 }
992
993 static char * dir_names[IMAGE_NUMBEROF_DIRECTORY_ENTRIES] =
994   {
995     N_("Export Directory [.edata (or where ever we found it)]"),
996     N_("Import Directory [parts of .idata]"),
997     N_("Resource Directory [.rsrc]"),
998     N_("Exception Directory [.pdata]"),
999     N_("Security Directory"),
1000     N_("Base Relocation Directory [.reloc]"),
1001     N_("Debug Directory"),
1002     N_("Description Directory"),
1003     N_("Special Directory"),
1004     N_("Thread Storage Directory [.tls]"),
1005     N_("Load Configuration Directory"),
1006     N_("Bound Import Directory"),
1007     N_("Import Address Table Directory"),
1008     N_("Delay Import Directory"),
1009     N_("Reserved"),
1010     N_("Reserved")
1011   };
1012
1013 #ifdef POWERPC_LE_PE
1014 /* The code for the PPC really falls in the "architecture dependent"
1015    category.  However, it's not clear that anyone will ever care, so
1016    we're ignoring the issue for now; if/when PPC matters, some of this
1017    may need to go into peicode.h, or arguments passed to enable the
1018    PPC- specific code.  */
1019 #endif
1020
1021 static boolean
1022 pe_print_idata (abfd, vfile)
1023      bfd *abfd;
1024      PTR vfile;
1025 {
1026   FILE *file = (FILE *) vfile;
1027   bfd_byte *data;
1028   asection *section;
1029   bfd_signed_vma adj;
1030
1031 #ifdef POWERPC_LE_PE
1032   asection *rel_section = bfd_get_section_by_name (abfd, ".reldata");
1033 #endif
1034
1035   bfd_size_type datasize = 0;
1036   bfd_size_type dataoff;
1037   bfd_size_type i;
1038   bfd_size_type amt;
1039   int onaline = 20;
1040
1041   pe_data_type *pe = pe_data (abfd);
1042   struct internal_extra_pe_aouthdr *extra = &pe->pe_opthdr;
1043
1044   bfd_vma addr;
1045
1046   addr = extra->DataDirectory[1].VirtualAddress;
1047
1048   if (addr == 0 && extra->DataDirectory[1].Size == 0)
1049     {
1050       /* Maybe the extra header isn't there.  Look for the section.  */
1051       section = bfd_get_section_by_name (abfd, ".idata");
1052       if (section == NULL)
1053         return true;
1054
1055       addr = section->vma;
1056       datasize = bfd_section_size (abfd, section);
1057       if (datasize == 0)
1058         return true;
1059     }
1060   else
1061     {
1062       addr += extra->ImageBase;
1063       for (section = abfd->sections; section != NULL; section = section->next)
1064         {
1065           datasize = bfd_section_size (abfd, section);
1066           if (addr >= section->vma && addr < section->vma + datasize)
1067             break;
1068         }
1069
1070       if (section == NULL)
1071         {
1072           fprintf (file,
1073                    _("\nThere is an import table, but the section containing it could not be found\n"));
1074           return true;
1075         }
1076     }
1077
1078   fprintf (file, _("\nThere is an import table in %s at 0x%lx\n"),
1079            section->name, (unsigned long) addr);
1080
1081   dataoff = addr - section->vma;
1082   datasize -= dataoff;
1083
1084 #ifdef POWERPC_LE_PE
1085   if (rel_section != 0 && bfd_section_size (abfd, rel_section) != 0)
1086     {
1087       /* The toc address can be found by taking the starting address,
1088          which on the PPC locates a function descriptor. The
1089          descriptor consists of the function code starting address
1090          followed by the address of the toc. The starting address we
1091          get from the bfd, and the descriptor is supposed to be in the
1092          .reldata section.  */
1093
1094       bfd_vma loadable_toc_address;
1095       bfd_vma toc_address;
1096       bfd_vma start_address;
1097       bfd_byte *data = 0;
1098       int offset;
1099
1100       amt = bfd_section_size (abfd, rel_section);
1101       data = (bfd_byte *) bfd_malloc (amt);
1102       if (data == NULL && amt != 0)
1103         return false;
1104
1105       bfd_get_section_contents (abfd, rel_section, (PTR) data, (bfd_vma) 0,
1106                                 amt);
1107
1108       offset = abfd->start_address - rel_section->vma;
1109
1110       start_address = bfd_get_32 (abfd, data + offset);
1111       loadable_toc_address = bfd_get_32 (abfd, data + offset + 4);
1112       toc_address = loadable_toc_address - 32768;
1113
1114       fprintf (file,
1115                _("\nFunction descriptor located at the start address: %04lx\n"),
1116                (unsigned long int) (abfd->start_address));
1117       fprintf (file,
1118                _("\tcode-base %08lx toc (loadable/actual) %08lx/%08lx\n"),
1119                start_address, loadable_toc_address, toc_address);
1120     }
1121   else
1122     {
1123       fprintf (file,
1124                _("\nNo reldata section! Function descriptor not decoded.\n"));
1125     }
1126 #endif
1127
1128   fprintf (file,
1129            _("\nThe Import Tables (interpreted %s section contents)\n"),
1130            section->name);
1131   fprintf (file,
1132            _(" vma:            Hint    Time      Forward  DLL       First\n"));
1133   fprintf (file,
1134            _("                 Table   Stamp     Chain    Name      Thunk\n"));
1135
1136   amt = dataoff + datasize;
1137   data = (bfd_byte *) bfd_malloc (amt);
1138   if (data == NULL)
1139     return false;
1140
1141   /* Read the whole section.  Some of the fields might be before dataoff.  */
1142   if (! bfd_get_section_contents (abfd, section, (PTR) data, (bfd_vma) 0, amt))
1143     return false;
1144
1145   adj = section->vma - extra->ImageBase;
1146
1147   for (i = 0; i < datasize; i += onaline)
1148     {
1149       bfd_vma hint_addr;
1150       bfd_vma time_stamp;
1151       bfd_vma forward_chain;
1152       bfd_vma dll_name;
1153       bfd_vma first_thunk;
1154       int idx = 0;
1155       bfd_size_type j;
1156       char *dll;
1157
1158       /* print (i + extra->DataDirectory[1].VirtualAddress)  */
1159       fprintf (file, " %08lx\t", (unsigned long) (i + adj + dataoff));
1160 #if 0
1161       if (i + 20 > datasize)
1162         /* Check stuff.  */
1163         ;
1164 #endif
1165       hint_addr = bfd_get_32 (abfd, data + i + dataoff);
1166       time_stamp = bfd_get_32 (abfd, data + i + 4 + dataoff);
1167       forward_chain = bfd_get_32 (abfd, data + i + 8 + dataoff);
1168       dll_name = bfd_get_32 (abfd, data + i + 12 + dataoff);
1169       first_thunk = bfd_get_32 (abfd, data + i + 16 + dataoff);
1170
1171       fprintf (file, "%08lx %08lx %08lx %08lx %08lx\n",
1172                (unsigned long) hint_addr,
1173                (unsigned long) time_stamp,
1174                (unsigned long) forward_chain,
1175                (unsigned long) dll_name,
1176                (unsigned long) first_thunk);
1177
1178       if (hint_addr == 0 && first_thunk == 0)
1179         break;
1180
1181       dll = (char *) data + dll_name - adj;
1182       fprintf (file, _("\n\tDLL Name: %s\n"), dll);
1183
1184       if (hint_addr != 0)
1185         {
1186           fprintf (file, _("\tvma:  Hint/Ord Member-Name\n"));
1187
1188           idx = hint_addr - adj;
1189
1190           for (j = 0; j < datasize; j += 4)
1191             {
1192               unsigned long member = bfd_get_32 (abfd, data + idx + j);
1193
1194               if (member == 0)
1195                 break;
1196               if (member & 0x80000000)
1197                 fprintf (file, "\t%04lx\t %4lu", member,
1198                          member & 0x7fffffff);
1199               else
1200                 {
1201                   int ordinal;
1202                   char *member_name;
1203
1204                   ordinal = bfd_get_16 (abfd, data + member - adj);
1205                   member_name = (char *) data + member - adj + 2;
1206                   fprintf (file, "\t%04lx\t %4d  %s",
1207                            member, ordinal, member_name);
1208                 }
1209
1210               /* If the time stamp is not zero, the import address
1211                  table holds actual addresses.  */
1212               if (time_stamp != 0
1213                   && first_thunk != 0
1214                   && first_thunk != hint_addr)
1215                 fprintf (file, "\t%04lx",
1216                          (long) bfd_get_32 (abfd, data + first_thunk - adj + j));
1217
1218               fprintf (file, "\n");
1219             }
1220         }
1221
1222       if (hint_addr != first_thunk && time_stamp == 0)
1223         {
1224           int differ = 0;
1225           int idx2;
1226
1227           idx2 = first_thunk - adj;
1228
1229           for (j = 0; j < datasize; j += 4)
1230             {
1231               int ordinal;
1232               char *member_name;
1233               bfd_vma hint_member = 0;
1234               bfd_vma iat_member;
1235
1236               if (hint_addr != 0)
1237                 hint_member = bfd_get_32 (abfd, data + idx + j);
1238               iat_member = bfd_get_32 (abfd, data + idx2 + j);
1239
1240               if (hint_addr == 0 && iat_member == 0)
1241                 break;
1242
1243               if (hint_addr == 0 || hint_member != iat_member)
1244                 {
1245                   if (differ == 0)
1246                     {
1247                       fprintf (file,
1248                                _("\tThe Import Address Table (difference found)\n"));
1249                       fprintf (file, _("\tvma:  Hint/Ord Member-Name\n"));
1250                       differ = 1;
1251                     }
1252
1253                   if (iat_member == 0)
1254                     fprintf (file,
1255                              _("\t>>> Ran out of IAT members!\n"));
1256                   else
1257                     {
1258                       ordinal = bfd_get_16 (abfd, data + iat_member - adj);
1259                       member_name = (char *) data + iat_member - adj + 2;
1260                       fprintf (file, "\t%04lx\t %4d  %s\n",
1261                               (unsigned long) iat_member,
1262                               ordinal,
1263                               member_name);
1264                     }
1265                 }
1266
1267               if (hint_addr != 0 && hint_member == 0)
1268                 break;
1269             }
1270
1271           if (differ == 0)
1272             fprintf (file,
1273                      _("\tThe Import Address Table is identical\n"));
1274         }
1275
1276       fprintf (file, "\n");
1277     }
1278
1279   free (data);
1280
1281   return true;
1282 }
1283
1284 static boolean
1285 pe_print_edata (abfd, vfile)
1286      bfd *abfd;
1287      PTR vfile;
1288 {
1289   FILE *file = (FILE *) vfile;
1290   bfd_byte *data;
1291   asection *section;
1292   bfd_size_type datasize = 0;
1293   bfd_size_type dataoff;
1294   bfd_size_type i;
1295   bfd_signed_vma adj;
1296   struct EDT_type
1297   {
1298     long export_flags;          /* reserved - should be zero */
1299     long time_stamp;
1300     short major_ver;
1301     short minor_ver;
1302     bfd_vma name;               /* rva - relative to image base */
1303     long base;                  /* ordinal base */
1304     unsigned long num_functions;/* Number in the export address table */
1305     unsigned long num_names;    /* Number in the name pointer table */
1306     bfd_vma eat_addr;           /* rva to the export address table */
1307     bfd_vma npt_addr;           /* rva to the Export Name Pointer Table */
1308     bfd_vma ot_addr;            /* rva to the Ordinal Table */
1309   } edt;
1310
1311   pe_data_type *pe = pe_data (abfd);
1312   struct internal_extra_pe_aouthdr *extra = &pe->pe_opthdr;
1313
1314   bfd_vma addr;
1315
1316   addr = extra->DataDirectory[0].VirtualAddress;
1317
1318   if (addr == 0 && extra->DataDirectory[0].Size == 0)
1319     {
1320       /* Maybe the extra header isn't there.  Look for the section.  */
1321       section = bfd_get_section_by_name (abfd, ".edata");
1322       if (section == NULL)
1323         return true;
1324
1325       addr = section->vma;
1326       datasize = bfd_section_size (abfd, section);
1327       if (datasize == 0)
1328         return true;
1329     }
1330   else
1331     {
1332       addr += extra->ImageBase;
1333
1334       for (section = abfd->sections; section != NULL; section = section->next)
1335         {
1336           datasize = bfd_section_size (abfd, section);
1337
1338           if (addr >= section->vma && addr < section->vma + datasize)
1339             break;
1340         }
1341
1342       if (section == NULL)
1343         {
1344           fprintf (file,
1345                    _("\nThere is an export table, but the section containing it could not be found\n"));
1346           return true;
1347         }
1348     }
1349
1350   fprintf (file, _("\nThere is an export table in %s at 0x%lx\n"),
1351            section->name, (unsigned long) addr);
1352
1353   dataoff = addr - section->vma;
1354   datasize -= dataoff;
1355
1356   data = (bfd_byte *) bfd_malloc (datasize);
1357   if (data == NULL)
1358     return false;
1359
1360   if (! bfd_get_section_contents (abfd, section, (PTR) data,
1361                                   (file_ptr) dataoff, datasize))
1362     return false;
1363
1364   /* Go get Export Directory Table.  */
1365   edt.export_flags   = bfd_get_32 (abfd, data +  0);
1366   edt.time_stamp     = bfd_get_32 (abfd, data +  4);
1367   edt.major_ver      = bfd_get_16 (abfd, data +  8);
1368   edt.minor_ver      = bfd_get_16 (abfd, data + 10);
1369   edt.name           = bfd_get_32 (abfd, data + 12);
1370   edt.base           = bfd_get_32 (abfd, data + 16);
1371   edt.num_functions  = bfd_get_32 (abfd, data + 20);
1372   edt.num_names      = bfd_get_32 (abfd, data + 24);
1373   edt.eat_addr       = bfd_get_32 (abfd, data + 28);
1374   edt.npt_addr       = bfd_get_32 (abfd, data + 32);
1375   edt.ot_addr        = bfd_get_32 (abfd, data + 36);
1376
1377   adj = section->vma - extra->ImageBase + dataoff;
1378
1379   /* Dump the EDT first.  */
1380   fprintf (file,
1381            _("\nThe Export Tables (interpreted %s section contents)\n\n"),
1382            section->name);
1383
1384   fprintf (file,
1385            _("Export Flags \t\t\t%lx\n"), (unsigned long) edt.export_flags);
1386
1387   fprintf (file,
1388            _("Time/Date stamp \t\t%lx\n"), (unsigned long) edt.time_stamp);
1389
1390   fprintf (file,
1391            _("Major/Minor \t\t\t%d/%d\n"), edt.major_ver, edt.minor_ver);
1392
1393   fprintf (file,
1394            _("Name \t\t\t\t"));
1395   fprintf_vma (file, edt.name);
1396   fprintf (file,
1397            " %s\n", data + edt.name - adj);
1398
1399   fprintf (file,
1400            _("Ordinal Base \t\t\t%ld\n"), edt.base);
1401
1402   fprintf (file,
1403            _("Number in:\n"));
1404
1405   fprintf (file,
1406            _("\tExport Address Table \t\t%08lx\n"),
1407            edt.num_functions);
1408
1409   fprintf (file,
1410            _("\t[Name Pointer/Ordinal] Table\t%08lx\n"), edt.num_names);
1411
1412   fprintf (file,
1413            _("Table Addresses\n"));
1414
1415   fprintf (file,
1416            _("\tExport Address Table \t\t"));
1417   fprintf_vma (file, edt.eat_addr);
1418   fprintf (file, "\n");
1419
1420   fprintf (file,
1421            _("\tName Pointer Table \t\t"));
1422   fprintf_vma (file, edt.npt_addr);
1423   fprintf (file, "\n");
1424
1425   fprintf (file,
1426            _("\tOrdinal Table \t\t\t"));
1427   fprintf_vma (file, edt.ot_addr);
1428   fprintf (file, "\n");
1429
1430   /* The next table to find is the Export Address Table. It's basically
1431      a list of pointers that either locate a function in this dll, or
1432      forward the call to another dll. Something like:
1433       typedef union
1434       {
1435         long export_rva;
1436         long forwarder_rva;
1437       } export_address_table_entry;
1438   */
1439
1440   fprintf (file,
1441           _("\nExport Address Table -- Ordinal Base %ld\n"),
1442           edt.base);
1443
1444   for (i = 0; i < edt.num_functions; ++i)
1445     {
1446       bfd_vma eat_member = bfd_get_32 (abfd,
1447                                        data + edt.eat_addr + (i * 4) - adj);
1448       if (eat_member == 0)
1449         continue;
1450
1451       if (eat_member - adj <= datasize)
1452         {
1453           /* This rva is to a name (forwarding function) in our section.  */
1454           /* Should locate a function descriptor.  */
1455           fprintf (file,
1456                    "\t[%4ld] +base[%4ld] %04lx %s -- %s\n",
1457                    (long) i,
1458                    (long) (i + edt.base),
1459                    (unsigned long) eat_member,
1460                    _("Forwarder RVA"),
1461                    data + eat_member - adj);
1462         }
1463       else
1464         {
1465           /* Should locate a function descriptor in the reldata section.  */
1466           fprintf (file,
1467                    "\t[%4ld] +base[%4ld] %04lx %s\n",
1468                    (long) i,
1469                    (long) (i + edt.base),
1470                    (unsigned long) eat_member,
1471                    _("Export RVA"));
1472         }
1473     }
1474
1475   /* The Export Name Pointer Table is paired with the Export Ordinal Table.  */
1476   /* Dump them in parallel for clarity.  */
1477   fprintf (file,
1478            _("\n[Ordinal/Name Pointer] Table\n"));
1479
1480   for (i = 0; i < edt.num_names; ++i)
1481     {
1482       bfd_vma name_ptr = bfd_get_32 (abfd,
1483                                     data +
1484                                     edt.npt_addr
1485                                     + (i*4) - adj);
1486
1487       char *name = (char *) data + name_ptr - adj;
1488
1489       bfd_vma ord = bfd_get_16 (abfd,
1490                                     data +
1491                                     edt.ot_addr
1492                                     + (i*2) - adj);
1493       fprintf (file,
1494               "\t[%4ld] %s\n", (long) ord, name);
1495     }
1496
1497   free (data);
1498
1499   return true;
1500 }
1501
1502 /* This really is architecture dependent.  On IA-64, a .pdata entry
1503    consists of three dwords containing relative virtual addresses that
1504    specify the start and end address of the code range the entry
1505    covers and the address of the corresponding unwind info data.  */
1506
1507 static boolean
1508 pe_print_pdata (abfd, vfile)
1509      bfd *abfd;
1510      PTR vfile;
1511 {
1512 #ifdef COFF_WITH_pep
1513 # define PDATA_ROW_SIZE (3*8)
1514 #else
1515 # define PDATA_ROW_SIZE (5*4)
1516 #endif
1517   FILE *file = (FILE *) vfile;
1518   bfd_byte *data = 0;
1519   asection *section = bfd_get_section_by_name (abfd, ".pdata");
1520   bfd_size_type datasize = 0;
1521   bfd_size_type i;
1522   bfd_size_type start, stop;
1523   int onaline = PDATA_ROW_SIZE;
1524
1525   if (section == NULL
1526       || coff_section_data (abfd, section) == NULL
1527       || pei_section_data (abfd, section) == NULL)
1528     return true;
1529
1530   stop = pei_section_data (abfd, section)->virt_size;
1531   if ((stop % onaline) != 0)
1532     fprintf (file,
1533              _("Warning, .pdata section size (%ld) is not a multiple of %d\n"),
1534              (long) stop, onaline);
1535
1536   fprintf (file,
1537            _("\nThe Function Table (interpreted .pdata section contents)\n"));
1538 #ifdef COFF_WITH_pep
1539   fprintf (file,
1540            _(" vma:\t\t\tBegin Address    End Address      Unwind Info\n"));
1541 #else
1542   fprintf (file,
1543            _(" vma:\t\tBegin    End      EH       EH       PrologEnd  Exception\n"));
1544   fprintf (file,
1545            _("     \t\tAddress  Address  Handler  Data     Address    Mask\n"));
1546 #endif
1547
1548   datasize = bfd_section_size (abfd, section);
1549   if (datasize == 0)
1550     return true;
1551
1552   data = (bfd_byte *) bfd_malloc (datasize);
1553   if (data == NULL && datasize != 0)
1554     return false;
1555
1556   bfd_get_section_contents (abfd, section, (PTR) data, (bfd_vma) 0,
1557                             datasize);
1558
1559   start = 0;
1560
1561   for (i = start; i < stop; i += onaline)
1562     {
1563       bfd_vma begin_addr;
1564       bfd_vma end_addr;
1565       bfd_vma eh_handler;
1566       bfd_vma eh_data;
1567       bfd_vma prolog_end_addr;
1568       int em_data;
1569
1570       if (i + PDATA_ROW_SIZE > stop)
1571         break;
1572
1573       begin_addr      = GET_PDATA_ENTRY (abfd, data + i     );
1574       end_addr        = GET_PDATA_ENTRY (abfd, data + i +  4);
1575       eh_handler      = GET_PDATA_ENTRY (abfd, data + i +  8);
1576       eh_data         = GET_PDATA_ENTRY (abfd, data + i + 12);
1577       prolog_end_addr = GET_PDATA_ENTRY (abfd, data + i + 16);
1578
1579       if (begin_addr == 0 && end_addr == 0 && eh_handler == 0
1580           && eh_data == 0 && prolog_end_addr == 0)
1581         /* We are probably into the padding of the section now.  */
1582         break;
1583
1584       em_data = ((eh_handler & 0x1) << 2) | (prolog_end_addr & 0x3);
1585       eh_handler &= ~(bfd_vma) 0x3;
1586       prolog_end_addr &= ~(bfd_vma) 0x3;
1587
1588       fputc (' ', file);
1589       fprintf_vma (file, i + section->vma); fputc ('\t', file);
1590       fprintf_vma (file, begin_addr); fputc (' ', file);
1591       fprintf_vma (file, end_addr); fputc (' ', file);
1592       fprintf_vma (file, eh_handler);
1593 #ifndef COFF_WITH_pep
1594       fputc (' ', file);
1595       fprintf_vma (file, eh_data); fputc (' ', file);
1596       fprintf_vma (file, prolog_end_addr);
1597       fprintf (file, "   %x", em_data);
1598 #endif
1599
1600 #ifdef POWERPC_LE_PE
1601       if (eh_handler == 0 && eh_data != 0)
1602         {
1603           /* Special bits here, although the meaning may be a little
1604              mysterious. The only one I know for sure is 0x03.  */
1605           /* Code Significance                           */
1606           /* 0x00 None                                   */
1607           /* 0x01 Register Save Millicode                */
1608           /* 0x02 Register Restore Millicode             */
1609           /* 0x03 Glue Code Sequence                     */
1610           switch (eh_data)
1611             {
1612             case 0x01:
1613               fprintf (file, _(" Register save millicode"));
1614               break;
1615             case 0x02:
1616               fprintf (file, _(" Register restore millicode"));
1617               break;
1618             case 0x03:
1619               fprintf (file, _(" Glue code sequence"));
1620               break;
1621             default:
1622               break;
1623             }
1624         }
1625 #endif
1626       fprintf (file, "\n");
1627     }
1628
1629   free (data);
1630
1631   return true;
1632 }
1633
1634 #define IMAGE_REL_BASED_HIGHADJ 4
1635 static const char * const tbl[] =
1636   {
1637     "ABSOLUTE",
1638     "HIGH",
1639     "LOW",
1640     "HIGHLOW",
1641     "HIGHADJ",
1642     "MIPS_JMPADDR",
1643     "SECTION",
1644     "REL32",
1645     "RESERVED1",
1646     "MIPS_JMPADDR16",
1647     "DIR64",
1648     "HIGH3ADJ"
1649     "UNKNOWN",   /* MUST be last */
1650   };
1651
1652 static boolean
1653 pe_print_reloc (abfd, vfile)
1654      bfd *abfd;
1655      PTR vfile;
1656 {
1657   FILE *file = (FILE *) vfile;
1658   bfd_byte *data = 0;
1659   asection *section = bfd_get_section_by_name (abfd, ".reloc");
1660   bfd_size_type datasize;
1661   bfd_size_type i;
1662   bfd_size_type start, stop;
1663
1664   if (section == NULL)
1665     return true;
1666
1667   if (bfd_section_size (abfd, section) == 0)
1668     return true;
1669
1670   fprintf (file,
1671            _("\n\nPE File Base Relocations (interpreted .reloc section contents)\n"));
1672
1673   datasize = bfd_section_size (abfd, section);
1674   data = (bfd_byte *) bfd_malloc (datasize);
1675   if (data == NULL && datasize != 0)
1676     return false;
1677
1678   bfd_get_section_contents (abfd, section, (PTR) data, (bfd_vma) 0,
1679                             datasize);
1680
1681   start = 0;
1682
1683   stop = bfd_section_size (abfd, section);
1684
1685   for (i = start; i < stop;)
1686     {
1687       int j;
1688       bfd_vma virtual_address;
1689       long number, size;
1690
1691       /* The .reloc section is a sequence of blocks, with a header consisting
1692          of two 32 bit quantities, followed by a number of 16 bit entries.  */
1693       virtual_address = bfd_get_32 (abfd, data+i);
1694       size = bfd_get_32 (abfd, data+i+4);
1695       number = (size - 8) / 2;
1696
1697       if (size == 0)
1698         break;
1699
1700       fprintf (file,
1701                _("\nVirtual Address: %08lx Chunk size %ld (0x%lx) Number of fixups %ld\n"),
1702                (unsigned long) virtual_address, size, size, number);
1703
1704       for (j = 0; j < number; ++j)
1705         {
1706           unsigned short e = bfd_get_16 (abfd, data + i + 8 + j * 2);
1707           unsigned int t = (e & 0xF000) >> 12;
1708           int off = e & 0x0FFF;
1709
1710           if (t >= sizeof (tbl) / sizeof (tbl[0]))
1711             t = (sizeof (tbl) / sizeof (tbl[0])) - 1;
1712
1713           fprintf (file,
1714                    _("\treloc %4d offset %4x [%4lx] %s"),
1715                    j, off, (long) (off + virtual_address), tbl[t]);
1716
1717           /* HIGHADJ takes an argument, - the next record *is* the
1718              low 16 bits of addend.  */
1719           if (t == IMAGE_REL_BASED_HIGHADJ)
1720             {
1721               fprintf (file, " (%4x)",
1722                        ((unsigned int)
1723                         bfd_get_16 (abfd, data + i + 8 + j * 2 + 2)));
1724               j++;
1725             }
1726
1727           fprintf (file, "\n");
1728         }
1729
1730       i += size;
1731     }
1732
1733   free (data);
1734
1735   return true;
1736 }
1737
1738 /* Print out the program headers.  */
1739
1740 boolean
1741 _bfd_XX_print_private_bfd_data_common (abfd, vfile)
1742      bfd *abfd;
1743      PTR vfile;
1744 {
1745   FILE *file = (FILE *) vfile;
1746   int j;
1747   pe_data_type *pe = pe_data (abfd);
1748   struct internal_extra_pe_aouthdr *i = &pe->pe_opthdr;
1749   const char *subsystem_name = NULL;
1750
1751   /* The MS dumpbin program reportedly ands with 0xff0f before
1752      printing the characteristics field.  Not sure why.  No reason to
1753      emulate it here.  */
1754   fprintf (file, _("\nCharacteristics 0x%x\n"), pe->real_flags);
1755 #undef PF
1756 #define PF(x, y) if (pe->real_flags & x) { fprintf (file, "\t%s\n", y); }
1757   PF (F_RELFLG, "relocations stripped");
1758   PF (F_EXEC, "executable");
1759   PF (F_LNNO, "line numbers stripped");
1760   PF (F_LSYMS, "symbols stripped");
1761   PF (0x80, "little endian");
1762   PF (F_AR32WR, "32 bit words");
1763   PF (0x200, "debugging information removed");
1764   PF (0x1000, "system file");
1765   PF (F_DLL, "DLL");
1766   PF (0x8000, "big endian");
1767 #undef PF
1768
1769   /* ctime implies '\n'.  */
1770   {
1771     time_t t = pe->coff.timestamp;
1772     fprintf (file, "\nTime/Date\t\t%s", ctime (&t));
1773   }
1774   fprintf (file, "\nImageBase\t\t");
1775   fprintf_vma (file, i->ImageBase);
1776   fprintf (file, "\nSectionAlignment\t");
1777   fprintf_vma (file, i->SectionAlignment);
1778   fprintf (file, "\nFileAlignment\t\t");
1779   fprintf_vma (file, i->FileAlignment);
1780   fprintf (file, "\nMajorOSystemVersion\t%d\n", i->MajorOperatingSystemVersion);
1781   fprintf (file, "MinorOSystemVersion\t%d\n", i->MinorOperatingSystemVersion);
1782   fprintf (file, "MajorImageVersion\t%d\n", i->MajorImageVersion);
1783   fprintf (file, "MinorImageVersion\t%d\n", i->MinorImageVersion);
1784   fprintf (file, "MajorSubsystemVersion\t%d\n", i->MajorSubsystemVersion);
1785   fprintf (file, "MinorSubsystemVersion\t%d\n", i->MinorSubsystemVersion);
1786   fprintf (file, "Win32Version\t\t%08lx\n", i->Reserved1);
1787   fprintf (file, "SizeOfImage\t\t%08lx\n", i->SizeOfImage);
1788   fprintf (file, "SizeOfHeaders\t\t%08lx\n", i->SizeOfHeaders);
1789   fprintf (file, "CheckSum\t\t%08lx\n", i->CheckSum);
1790
1791   switch (i->Subsystem)
1792     {
1793     case IMAGE_SUBSYSTEM_UNKNOWN:
1794       subsystem_name = "unspecified";
1795       break;
1796     case IMAGE_SUBSYSTEM_NATIVE:
1797       subsystem_name = "NT native";
1798       break;
1799     case IMAGE_SUBSYSTEM_WINDOWS_GUI:
1800       subsystem_name = "Windows GUI";
1801       break;
1802     case IMAGE_SUBSYSTEM_WINDOWS_CUI:
1803       subsystem_name = "Windows CUI";
1804       break;
1805     case IMAGE_SUBSYSTEM_POSIX_CUI:
1806       subsystem_name = "POSIX CUI";
1807       break;
1808     case IMAGE_SUBSYSTEM_WINDOWS_CE_GUI:
1809       subsystem_name = "Wince CUI";
1810       break;
1811     case IMAGE_SUBSYSTEM_EFI_APPLICATION:
1812       subsystem_name = "EFI application";
1813       break;
1814     case IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER:
1815       subsystem_name = "EFI boot service driver";
1816       break;
1817     case IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER:
1818       subsystem_name = "EFI runtime driver";
1819       break;
1820     }
1821
1822   fprintf (file, "Subsystem\t\t%08x", i->Subsystem);
1823   if (subsystem_name)
1824     fprintf (file, "\t(%s)", subsystem_name);
1825   fprintf (file, "\nDllCharacteristics\t%08x\n", i->DllCharacteristics);
1826   fprintf (file, "SizeOfStackReserve\t");
1827   fprintf_vma (file, i->SizeOfStackReserve);
1828   fprintf (file, "\nSizeOfStackCommit\t");
1829   fprintf_vma (file, i->SizeOfStackCommit);
1830   fprintf (file, "\nSizeOfHeapReserve\t");
1831   fprintf_vma (file, i->SizeOfHeapReserve);
1832   fprintf (file, "\nSizeOfHeapCommit\t");
1833   fprintf_vma (file, i->SizeOfHeapCommit);
1834   fprintf (file, "\nLoaderFlags\t\t%08lx\n", i->LoaderFlags);
1835   fprintf (file, "NumberOfRvaAndSizes\t%08lx\n", i->NumberOfRvaAndSizes);
1836
1837   fprintf (file, "\nThe Data Directory\n");
1838   for (j = 0; j < IMAGE_NUMBEROF_DIRECTORY_ENTRIES; j++)
1839     {
1840       fprintf (file, "Entry %1x ", j);
1841       fprintf_vma (file, i->DataDirectory[j].VirtualAddress);
1842       fprintf (file, " %08lx ", i->DataDirectory[j].Size);
1843       fprintf (file, "%s\n", dir_names[j]);
1844     }
1845
1846   pe_print_idata (abfd, vfile);
1847   pe_print_edata (abfd, vfile);
1848   pe_print_pdata (abfd, vfile);
1849   pe_print_reloc (abfd, vfile);
1850
1851   return true;
1852 }
1853
1854 /* Copy any private info we understand from the input bfd
1855    to the output bfd.  */
1856
1857 boolean
1858 _bfd_XX_bfd_copy_private_bfd_data_common (ibfd, obfd)
1859      bfd *ibfd, *obfd;
1860 {
1861   /* One day we may try to grok other private data.  */
1862   if (ibfd->xvec->flavour != bfd_target_coff_flavour
1863       || obfd->xvec->flavour != bfd_target_coff_flavour)
1864     return true;
1865
1866   pe_data (obfd)->pe_opthdr = pe_data (ibfd)->pe_opthdr;
1867   pe_data (obfd)->dll = pe_data (ibfd)->dll;
1868
1869   /* For strip: if we removed .reloc, we'll make a real mess of things
1870      if we don't remove this entry as well.  */
1871   if (! pe_data (obfd)->has_reloc_section)
1872     {
1873       pe_data (obfd)->pe_opthdr.DataDirectory[5].VirtualAddress = 0;
1874       pe_data (obfd)->pe_opthdr.DataDirectory[5].Size = 0;
1875     }
1876   return true;
1877 }
1878
1879 /* Copy private section data.  */
1880
1881 boolean
1882 _bfd_XX_bfd_copy_private_section_data (ibfd, isec, obfd, osec)
1883      bfd *ibfd;
1884      asection *isec;
1885      bfd *obfd;
1886      asection *osec;
1887 {
1888   if (bfd_get_flavour (ibfd) != bfd_target_coff_flavour
1889       || bfd_get_flavour (obfd) != bfd_target_coff_flavour)
1890     return true;
1891
1892   if (coff_section_data (ibfd, isec) != NULL
1893       && pei_section_data (ibfd, isec) != NULL)
1894     {
1895       if (coff_section_data (obfd, osec) == NULL)
1896         {
1897           bfd_size_type amt = sizeof (struct coff_section_tdata);
1898           osec->used_by_bfd = (PTR) bfd_zalloc (obfd, amt);
1899           if (osec->used_by_bfd == NULL)
1900             return false;
1901         }
1902
1903       if (pei_section_data (obfd, osec) == NULL)
1904         {
1905           bfd_size_type amt = sizeof (struct pei_section_tdata);
1906           coff_section_data (obfd, osec)->tdata = (PTR) bfd_zalloc (obfd, amt);
1907           if (coff_section_data (obfd, osec)->tdata == NULL)
1908             return false;
1909         }
1910
1911       pei_section_data (obfd, osec)->virt_size =
1912         pei_section_data (ibfd, isec)->virt_size;
1913       pei_section_data (obfd, osec)->pe_flags =
1914         pei_section_data (ibfd, isec)->pe_flags;
1915     }
1916
1917   return true;
1918 }
1919
1920 void
1921 _bfd_XX_get_symbol_info (abfd, symbol, ret)
1922      bfd *abfd;
1923      asymbol *symbol;
1924      symbol_info *ret;
1925 {
1926   coff_get_symbol_info (abfd, symbol, ret);
1927 #if 0 /* This code no longer appears to be necessary.
1928          ImageBase has already been added in by coff_swap_scnhdr_in.  */
1929   if (pe_data (abfd) != NULL
1930       && ((symbol->flags & BSF_DEBUGGING) == 0
1931           || (symbol->flags & BSF_DEBUGGING_RELOC) != 0)
1932       && ! bfd_is_abs_section (symbol->section))
1933     ret->value += pe_data (abfd)->pe_opthdr.ImageBase;
1934 #endif
1935 }
1936
1937 /* Handle the .idata section and other things that need symbol table
1938    access.  */
1939
1940 boolean
1941 _bfd_XXi_final_link_postscript (abfd, pfinfo)
1942      bfd *abfd;
1943      struct coff_final_link_info *pfinfo;
1944 {
1945   struct coff_link_hash_entry *h1;
1946   struct bfd_link_info *info = pfinfo->info;
1947
1948   /* There are a few fields that need to be filled in now while we
1949      have symbol table access.
1950
1951      The .idata subsections aren't directly available as sections, but
1952      they are in the symbol table, so get them from there.  */
1953
1954   /* The import directory.  This is the address of .idata$2, with size
1955      of .idata$2 + .idata$3.  */
1956   h1 = coff_link_hash_lookup (coff_hash_table (info),
1957                               ".idata$2", false, false, true);
1958   if (h1 != NULL)
1959     {
1960       pe_data (abfd)->pe_opthdr.DataDirectory[1].VirtualAddress =
1961         (h1->root.u.def.value
1962          + h1->root.u.def.section->output_section->vma
1963          + h1->root.u.def.section->output_offset);
1964       h1 = coff_link_hash_lookup (coff_hash_table (info),
1965                                   ".idata$4", false, false, true);
1966       pe_data (abfd)->pe_opthdr.DataDirectory[1].Size =
1967         ((h1->root.u.def.value
1968           + h1->root.u.def.section->output_section->vma
1969           + h1->root.u.def.section->output_offset)
1970          - pe_data (abfd)->pe_opthdr.DataDirectory[1].VirtualAddress);
1971
1972       /* The import address table.  This is the size/address of
1973          .idata$5.  */
1974       h1 = coff_link_hash_lookup (coff_hash_table (info),
1975                                   ".idata$5", false, false, true);
1976       pe_data (abfd)->pe_opthdr.DataDirectory[12].VirtualAddress =
1977         (h1->root.u.def.value
1978          + h1->root.u.def.section->output_section->vma
1979          + h1->root.u.def.section->output_offset);
1980       h1 = coff_link_hash_lookup (coff_hash_table (info),
1981                                   ".idata$6", false, false, true);
1982       pe_data (abfd)->pe_opthdr.DataDirectory[12].Size =
1983         ((h1->root.u.def.value
1984           + h1->root.u.def.section->output_section->vma
1985           + h1->root.u.def.section->output_offset)
1986          - pe_data (abfd)->pe_opthdr.DataDirectory[12].VirtualAddress);
1987     }
1988
1989   /* If we couldn't find idata$2, we either have an excessively
1990      trivial program or are in DEEP trouble; we have to assume trivial
1991      program....  */
1992   return true;
1993 }