OSDN Git Service

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