OSDN Git Service

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