OSDN Git Service

2009-04-19 Peter O'Gorman <binutils@mlists.thewrittenword.com>
[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, 2002, 2003, 2004,
3    2005, 2006, 2007, 2008, 2009  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 3 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., 51 Franklin Street - Fifth Floor, Boston,
21    MA 02110-1301, USA.  */
22
23
24 /* Most of this hacked by Steve Chamberlain <sac@cygnus.com>.
25
26    PE/PEI rearrangement (and code added): Donn Terry
27                                           Softway Systems, Inc.  */
28
29 /* Hey look, some documentation [and in a place you expect to find it]!
30
31    The main reference for the pei format is "Microsoft Portable Executable
32    and Common Object File Format Specification 4.1".  Get it if you need to
33    do some serious hacking on this code.
34
35    Another reference:
36    "Peering Inside the PE: A Tour of the Win32 Portable Executable
37    File Format", MSJ 1994, Volume 9.
38
39    The *sole* difference between the pe format and the pei format is that the
40    latter has an MSDOS 2.0 .exe header on the front that prints the message
41    "This app must be run under Windows." (or some such).
42    (FIXME: Whether that statement is *really* true or not is unknown.
43    Are there more subtle differences between pe and pei formats?
44    For now assume there aren't.  If you find one, then for God sakes
45    document it here!)
46
47    The Microsoft docs use the word "image" instead of "executable" because
48    the former can also refer to a DLL (shared library).  Confusion can arise
49    because the `i' in `pei' also refers to "image".  The `pe' format can
50    also create images (i.e. executables), it's just that to run on a win32
51    system you need to use the pei format.
52
53    FIXME: Please add more docs here so the next poor fool that has to hack
54    on this code has a chance of getting something accomplished without
55    wasting too much time.  */
56
57 /* This expands into COFF_WITH_pe, COFF_WITH_pep, or COFF_WITH_pex64
58    depending on whether we're compiling for straight PE or PE+.  */
59 #define COFF_WITH_XX
60
61 #include "sysdep.h"
62 #include "bfd.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 #if defined COFF_WITH_pex64
73 # include "coff/x86_64.h"
74 #elif defined COFF_WITH_pep
75 # include "coff/ia64.h"
76 #else
77 # include "coff/i386.h"
78 #endif
79
80 #include "coff/pe.h"
81 #include "libcoff.h"
82 #include "libpei.h"
83
84 #if defined COFF_WITH_pep || defined COFF_WITH_pex64
85 # undef AOUTSZ
86 # define AOUTSZ         PEPAOUTSZ
87 # define PEAOUTHDR      PEPAOUTHDR
88 #endif
89
90 /* FIXME: This file has various tests of POWERPC_LE_PE.  Those tests
91    worked when the code was in peicode.h, but no longer work now that
92    the code is in peigen.c.  PowerPC NT is said to be dead.  If
93    anybody wants to revive the code, you will have to figure out how
94    to handle those issues.  */
95 \f
96 void
97 _bfd_XXi_swap_sym_in (bfd * abfd, void * ext1, void * in1)
98 {
99   SYMENT *ext = (SYMENT *) ext1;
100   struct internal_syment *in = (struct internal_syment *) in1;
101
102   if (ext->e.e_name[0] == 0)
103     {
104       in->_n._n_n._n_zeroes = 0;
105       in->_n._n_n._n_offset = H_GET_32 (abfd, ext->e.e.e_offset);
106     }
107   else
108     memcpy (in->_n._n_name, ext->e.e_name, SYMNMLEN);
109
110   in->n_value = H_GET_32 (abfd, ext->e_value);
111   in->n_scnum = H_GET_16 (abfd, ext->e_scnum);
112
113   if (sizeof (ext->e_type) == 2)
114     in->n_type = H_GET_16 (abfd, ext->e_type);
115   else
116     in->n_type = H_GET_32 (abfd, ext->e_type);
117
118   in->n_sclass = H_GET_8 (abfd, ext->e_sclass);
119   in->n_numaux = H_GET_8 (abfd, ext->e_numaux);
120
121 #ifndef STRICT_PE_FORMAT
122   /* This is for Gnu-created DLLs.  */
123
124   /* The section symbols for the .idata$ sections have class 0x68
125      (C_SECTION), which MS documentation indicates is a section
126      symbol.  Unfortunately, the value field in the symbol is simply a
127      copy of the .idata section's flags rather than something useful.
128      When these symbols are encountered, change the value to 0 so that
129      they will be handled somewhat correctly in the bfd code.  */
130   if (in->n_sclass == C_SECTION)
131     {
132       char namebuf[SYMNMLEN + 1];
133       const char *name;
134
135       in->n_value = 0x0;
136
137       /* Create synthetic empty sections as needed.  DJ */
138       if (in->n_scnum == 0)
139         {
140           asection *sec;
141
142           name = _bfd_coff_internal_syment_name (abfd, in, namebuf);
143           if (name == NULL)
144             /* FIXME: Return error.  */
145             abort ();
146           sec = bfd_get_section_by_name (abfd, name);
147           if (sec != NULL)
148             in->n_scnum = sec->target_index;
149         }
150
151       if (in->n_scnum == 0)
152         {
153           int unused_section_number = 0;
154           asection *sec;
155           flagword flags;
156
157           for (sec = abfd->sections; sec; sec = sec->next)
158             if (unused_section_number <= sec->target_index)
159               unused_section_number = sec->target_index + 1;
160
161           if (name == namebuf)
162             {
163               name = bfd_alloc (abfd, strlen (namebuf) + 1);
164               if (name == NULL)
165                 /* FIXME: Return error.  */
166                 abort ();
167               strcpy ((char *) name, namebuf);
168             }
169           flags = SEC_HAS_CONTENTS | SEC_ALLOC | SEC_DATA | SEC_LOAD;
170           sec = bfd_make_section_anyway_with_flags (abfd, name, flags);
171           if (sec == NULL)
172             /* FIXME: Return error.  */
173             abort ();
174
175           sec->vma = 0;
176           sec->lma = 0;
177           sec->size = 0;
178           sec->filepos = 0;
179           sec->rel_filepos = 0;
180           sec->reloc_count = 0;
181           sec->line_filepos = 0;
182           sec->lineno_count = 0;
183           sec->userdata = NULL;
184           sec->next = NULL;
185           sec->alignment_power = 2;
186
187           sec->target_index = unused_section_number;
188
189           in->n_scnum = unused_section_number;
190         }
191       in->n_sclass = C_STAT;
192     }
193 #endif
194
195 #ifdef coff_swap_sym_in_hook
196   /* This won't work in peigen.c, but since it's for PPC PE, it's not
197      worth fixing.  */
198   coff_swap_sym_in_hook (abfd, ext1, in1);
199 #endif
200 }
201
202 unsigned int
203 _bfd_XXi_swap_sym_out (bfd * abfd, void * inp, void * extp)
204 {
205   struct internal_syment *in = (struct internal_syment *) inp;
206   SYMENT *ext = (SYMENT *) extp;
207
208   if (in->_n._n_name[0] == 0)
209     {
210       H_PUT_32 (abfd, 0, ext->e.e.e_zeroes);
211       H_PUT_32 (abfd, in->_n._n_n._n_offset, ext->e.e.e_offset);
212     }
213   else
214     memcpy (ext->e.e_name, in->_n._n_name, SYMNMLEN);
215
216   H_PUT_32 (abfd, in->n_value, ext->e_value);
217   H_PUT_16 (abfd, in->n_scnum, ext->e_scnum);
218
219   if (sizeof (ext->e_type) == 2)
220     H_PUT_16 (abfd, in->n_type, ext->e_type);
221   else
222     H_PUT_32 (abfd, in->n_type, ext->e_type);
223
224   H_PUT_8 (abfd, in->n_sclass, ext->e_sclass);
225   H_PUT_8 (abfd, in->n_numaux, ext->e_numaux);
226
227   return SYMESZ;
228 }
229
230 void
231 _bfd_XXi_swap_aux_in (bfd *     abfd,
232                       void *    ext1,
233                       int       type,
234                       int       class,
235                       int       indx ATTRIBUTE_UNUSED,
236                       int       numaux ATTRIBUTE_UNUSED,
237                       void *    in1)
238 {
239   AUXENT *ext = (AUXENT *) ext1;
240   union internal_auxent *in = (union internal_auxent *) in1;
241
242   switch (class)
243     {
244     case C_FILE:
245       if (ext->x_file.x_fname[0] == 0)
246         {
247           in->x_file.x_n.x_zeroes = 0;
248           in->x_file.x_n.x_offset = H_GET_32 (abfd, ext->x_file.x_n.x_offset);
249         }
250       else
251         memcpy (in->x_file.x_fname, ext->x_file.x_fname, FILNMLEN);
252       return;
253
254     case C_STAT:
255     case C_LEAFSTAT:
256     case C_HIDDEN:
257       if (type == T_NULL)
258         {
259           in->x_scn.x_scnlen = GET_SCN_SCNLEN (abfd, ext);
260           in->x_scn.x_nreloc = GET_SCN_NRELOC (abfd, ext);
261           in->x_scn.x_nlinno = GET_SCN_NLINNO (abfd, ext);
262           in->x_scn.x_checksum = H_GET_32 (abfd, ext->x_scn.x_checksum);
263           in->x_scn.x_associated = H_GET_16 (abfd, ext->x_scn.x_associated);
264           in->x_scn.x_comdat = H_GET_8 (abfd, ext->x_scn.x_comdat);
265           return;
266         }
267       break;
268     }
269
270   in->x_sym.x_tagndx.l = H_GET_32 (abfd, ext->x_sym.x_tagndx);
271   in->x_sym.x_tvndx = H_GET_16 (abfd, ext->x_sym.x_tvndx);
272
273   if (class == C_BLOCK || class == C_FCN || ISFCN (type) || ISTAG (class))
274     {
275       in->x_sym.x_fcnary.x_fcn.x_lnnoptr = GET_FCN_LNNOPTR (abfd, ext);
276       in->x_sym.x_fcnary.x_fcn.x_endndx.l = GET_FCN_ENDNDX (abfd, ext);
277     }
278   else
279     {
280       in->x_sym.x_fcnary.x_ary.x_dimen[0] =
281         H_GET_16 (abfd, ext->x_sym.x_fcnary.x_ary.x_dimen[0]);
282       in->x_sym.x_fcnary.x_ary.x_dimen[1] =
283         H_GET_16 (abfd, ext->x_sym.x_fcnary.x_ary.x_dimen[1]);
284       in->x_sym.x_fcnary.x_ary.x_dimen[2] =
285         H_GET_16 (abfd, ext->x_sym.x_fcnary.x_ary.x_dimen[2]);
286       in->x_sym.x_fcnary.x_ary.x_dimen[3] =
287         H_GET_16 (abfd, ext->x_sym.x_fcnary.x_ary.x_dimen[3]);
288     }
289
290   if (ISFCN (type))
291     {
292       in->x_sym.x_misc.x_fsize = H_GET_32 (abfd, ext->x_sym.x_misc.x_fsize);
293     }
294   else
295     {
296       in->x_sym.x_misc.x_lnsz.x_lnno = GET_LNSZ_LNNO (abfd, ext);
297       in->x_sym.x_misc.x_lnsz.x_size = GET_LNSZ_SIZE (abfd, ext);
298     }
299 }
300
301 unsigned int
302 _bfd_XXi_swap_aux_out (bfd *  abfd,
303                        void * inp,
304                        int    type,
305                        int    class,
306                        int    indx ATTRIBUTE_UNUSED,
307                        int    numaux ATTRIBUTE_UNUSED,
308                        void * extp)
309 {
310   union internal_auxent *in = (union internal_auxent *) inp;
311   AUXENT *ext = (AUXENT *) extp;
312
313   memset (ext, 0, AUXESZ);
314
315   switch (class)
316     {
317     case C_FILE:
318       if (in->x_file.x_fname[0] == 0)
319         {
320           H_PUT_32 (abfd, 0, ext->x_file.x_n.x_zeroes);
321           H_PUT_32 (abfd, in->x_file.x_n.x_offset, ext->x_file.x_n.x_offset);
322         }
323       else
324         memcpy (ext->x_file.x_fname, in->x_file.x_fname, FILNMLEN);
325
326       return AUXESZ;
327
328     case C_STAT:
329     case C_LEAFSTAT:
330     case C_HIDDEN:
331       if (type == T_NULL)
332         {
333           PUT_SCN_SCNLEN (abfd, in->x_scn.x_scnlen, ext);
334           PUT_SCN_NRELOC (abfd, in->x_scn.x_nreloc, ext);
335           PUT_SCN_NLINNO (abfd, in->x_scn.x_nlinno, ext);
336           H_PUT_32 (abfd, in->x_scn.x_checksum, ext->x_scn.x_checksum);
337           H_PUT_16 (abfd, in->x_scn.x_associated, ext->x_scn.x_associated);
338           H_PUT_8 (abfd, in->x_scn.x_comdat, ext->x_scn.x_comdat);
339           return AUXESZ;
340         }
341       break;
342     }
343
344   H_PUT_32 (abfd, in->x_sym.x_tagndx.l, ext->x_sym.x_tagndx);
345   H_PUT_16 (abfd, in->x_sym.x_tvndx, ext->x_sym.x_tvndx);
346
347   if (class == C_BLOCK || class == C_FCN || ISFCN (type) || ISTAG (class))
348     {
349       PUT_FCN_LNNOPTR (abfd, in->x_sym.x_fcnary.x_fcn.x_lnnoptr,  ext);
350       PUT_FCN_ENDNDX  (abfd, in->x_sym.x_fcnary.x_fcn.x_endndx.l, ext);
351     }
352   else
353     {
354       H_PUT_16 (abfd, in->x_sym.x_fcnary.x_ary.x_dimen[0],
355                 ext->x_sym.x_fcnary.x_ary.x_dimen[0]);
356       H_PUT_16 (abfd, in->x_sym.x_fcnary.x_ary.x_dimen[1],
357                 ext->x_sym.x_fcnary.x_ary.x_dimen[1]);
358       H_PUT_16 (abfd, in->x_sym.x_fcnary.x_ary.x_dimen[2],
359                 ext->x_sym.x_fcnary.x_ary.x_dimen[2]);
360       H_PUT_16 (abfd, in->x_sym.x_fcnary.x_ary.x_dimen[3],
361                 ext->x_sym.x_fcnary.x_ary.x_dimen[3]);
362     }
363
364   if (ISFCN (type))
365     H_PUT_32 (abfd, in->x_sym.x_misc.x_fsize, ext->x_sym.x_misc.x_fsize);
366   else
367     {
368       PUT_LNSZ_LNNO (abfd, in->x_sym.x_misc.x_lnsz.x_lnno, ext);
369       PUT_LNSZ_SIZE (abfd, in->x_sym.x_misc.x_lnsz.x_size, ext);
370     }
371
372   return AUXESZ;
373 }
374
375 void
376 _bfd_XXi_swap_lineno_in (bfd * abfd, void * ext1, void * in1)
377 {
378   LINENO *ext = (LINENO *) ext1;
379   struct internal_lineno *in = (struct internal_lineno *) in1;
380
381   in->l_addr.l_symndx = H_GET_32 (abfd, ext->l_addr.l_symndx);
382   in->l_lnno = GET_LINENO_LNNO (abfd, ext);
383 }
384
385 unsigned int
386 _bfd_XXi_swap_lineno_out (bfd * abfd, void * inp, void * outp)
387 {
388   struct internal_lineno *in = (struct internal_lineno *) inp;
389   struct external_lineno *ext = (struct external_lineno *) outp;
390   H_PUT_32 (abfd, in->l_addr.l_symndx, ext->l_addr.l_symndx);
391
392   PUT_LINENO_LNNO (abfd, in->l_lnno, ext);
393   return LINESZ;
394 }
395
396 void
397 _bfd_XXi_swap_aouthdr_in (bfd * abfd,
398                           void * aouthdr_ext1,
399                           void * aouthdr_int1)
400 {
401   PEAOUTHDR * src = (PEAOUTHDR *) aouthdr_ext1;
402   AOUTHDR * aouthdr_ext = (AOUTHDR *) aouthdr_ext1;
403   struct internal_aouthdr *aouthdr_int
404     = (struct internal_aouthdr *) aouthdr_int1;
405   struct internal_extra_pe_aouthdr *a = &aouthdr_int->pe;
406
407   aouthdr_int->magic = H_GET_16 (abfd, aouthdr_ext->magic);
408   aouthdr_int->vstamp = H_GET_16 (abfd, aouthdr_ext->vstamp);
409   aouthdr_int->tsize = GET_AOUTHDR_TSIZE (abfd, aouthdr_ext->tsize);
410   aouthdr_int->dsize = GET_AOUTHDR_DSIZE (abfd, aouthdr_ext->dsize);
411   aouthdr_int->bsize = GET_AOUTHDR_BSIZE (abfd, aouthdr_ext->bsize);
412   aouthdr_int->entry = GET_AOUTHDR_ENTRY (abfd, aouthdr_ext->entry);
413   aouthdr_int->text_start =
414     GET_AOUTHDR_TEXT_START (abfd, aouthdr_ext->text_start);
415 #if !defined(COFF_WITH_pep) && !defined(COFF_WITH_pex64)
416   /* PE32+ does not have data_start member!  */
417   aouthdr_int->data_start =
418     GET_AOUTHDR_DATA_START (abfd, aouthdr_ext->data_start);
419   a->BaseOfData = aouthdr_int->data_start;
420 #endif
421
422   a->Magic = aouthdr_int->magic;
423   a->MajorLinkerVersion = H_GET_8 (abfd, aouthdr_ext->vstamp);
424   a->MinorLinkerVersion = H_GET_8 (abfd, aouthdr_ext->vstamp + 1);
425   a->SizeOfCode = aouthdr_int->tsize ;
426   a->SizeOfInitializedData = aouthdr_int->dsize ;
427   a->SizeOfUninitializedData = aouthdr_int->bsize ;
428   a->AddressOfEntryPoint = aouthdr_int->entry;
429   a->BaseOfCode = aouthdr_int->text_start;
430   a->ImageBase = GET_OPTHDR_IMAGE_BASE (abfd, src->ImageBase);
431   a->SectionAlignment = H_GET_32 (abfd, src->SectionAlignment);
432   a->FileAlignment = H_GET_32 (abfd, src->FileAlignment);
433   a->MajorOperatingSystemVersion =
434     H_GET_16 (abfd, src->MajorOperatingSystemVersion);
435   a->MinorOperatingSystemVersion =
436     H_GET_16 (abfd, src->MinorOperatingSystemVersion);
437   a->MajorImageVersion = H_GET_16 (abfd, src->MajorImageVersion);
438   a->MinorImageVersion = H_GET_16 (abfd, src->MinorImageVersion);
439   a->MajorSubsystemVersion = H_GET_16 (abfd, src->MajorSubsystemVersion);
440   a->MinorSubsystemVersion = H_GET_16 (abfd, src->MinorSubsystemVersion);
441   a->Reserved1 = H_GET_32 (abfd, src->Reserved1);
442   a->SizeOfImage = H_GET_32 (abfd, src->SizeOfImage);
443   a->SizeOfHeaders = H_GET_32 (abfd, src->SizeOfHeaders);
444   a->CheckSum = H_GET_32 (abfd, src->CheckSum);
445   a->Subsystem = H_GET_16 (abfd, src->Subsystem);
446   a->DllCharacteristics = H_GET_16 (abfd, src->DllCharacteristics);
447   a->SizeOfStackReserve =
448     GET_OPTHDR_SIZE_OF_STACK_RESERVE (abfd, src->SizeOfStackReserve);
449   a->SizeOfStackCommit =
450     GET_OPTHDR_SIZE_OF_STACK_COMMIT (abfd, src->SizeOfStackCommit);
451   a->SizeOfHeapReserve =
452     GET_OPTHDR_SIZE_OF_HEAP_RESERVE (abfd, src->SizeOfHeapReserve);
453   a->SizeOfHeapCommit =
454     GET_OPTHDR_SIZE_OF_HEAP_COMMIT (abfd, src->SizeOfHeapCommit);
455   a->LoaderFlags = H_GET_32 (abfd, src->LoaderFlags);
456   a->NumberOfRvaAndSizes = H_GET_32 (abfd, src->NumberOfRvaAndSizes);
457
458   {
459     int idx;
460
461     for (idx = 0; idx < 16; idx++)
462       {
463         /* If data directory is empty, rva also should be 0.  */
464         int size =
465           H_GET_32 (abfd, src->DataDirectory[idx][1]);
466
467         a->DataDirectory[idx].Size = size;
468
469         if (size)
470           a->DataDirectory[idx].VirtualAddress =
471             H_GET_32 (abfd, src->DataDirectory[idx][0]);
472         else
473           a->DataDirectory[idx].VirtualAddress = 0;
474       }
475   }
476
477   if (aouthdr_int->entry)
478     {
479       aouthdr_int->entry += a->ImageBase;
480 #if !defined(COFF_WITH_pep) && !defined(COFF_WITH_pex64)
481       aouthdr_int->entry &= 0xffffffff;
482 #endif
483     }
484
485   if (aouthdr_int->tsize)
486     {
487       aouthdr_int->text_start += a->ImageBase;
488 #if !defined(COFF_WITH_pep) && !defined(COFF_WITH_pex64)
489       aouthdr_int->text_start &= 0xffffffff;
490 #endif
491     }
492
493 #if !defined(COFF_WITH_pep) && !defined(COFF_WITH_pex64)
494   /* PE32+ does not have data_start member!  */
495   if (aouthdr_int->dsize)
496     {
497       aouthdr_int->data_start += a->ImageBase;
498       aouthdr_int->data_start &= 0xffffffff;
499     }
500 #endif
501
502 #ifdef POWERPC_LE_PE
503   /* These three fields are normally set up by ppc_relocate_section.
504      In the case of reading a file in, we can pick them up from the
505      DataDirectory.  */
506   first_thunk_address = a->DataDirectory[PE_IMPORT_ADDRESS_TABLE].VirtualAddress;
507   thunk_size = a->DataDirectory[PE_IMPORT_ADDRESS_TABLE].Size;
508   import_table_size = a->DataDirectory[PE_IMPORT_TABLE].Size;
509 #endif
510 }
511
512 /* A support function for below.  */
513
514 static void
515 add_data_entry (bfd * abfd,
516                 struct internal_extra_pe_aouthdr *aout,
517                 int idx,
518                 char *name,
519                 bfd_vma base)
520 {
521   asection *sec = bfd_get_section_by_name (abfd, name);
522
523   /* Add import directory information if it exists.  */
524   if ((sec != NULL)
525       && (coff_section_data (abfd, sec) != NULL)
526       && (pei_section_data (abfd, sec) != NULL))
527     {
528       /* If data directory is empty, rva also should be 0.  */
529       int size = pei_section_data (abfd, sec)->virt_size;
530       aout->DataDirectory[idx].Size = size;
531
532       if (size)
533         {
534           aout->DataDirectory[idx].VirtualAddress =
535             (sec->vma - base) & 0xffffffff;
536           sec->flags |= SEC_DATA;
537         }
538     }
539 }
540
541 unsigned int
542 _bfd_XXi_swap_aouthdr_out (bfd * abfd, void * in, void * out)
543 {
544   struct internal_aouthdr *aouthdr_in = (struct internal_aouthdr *) in;
545   pe_data_type *pe = pe_data (abfd);
546   struct internal_extra_pe_aouthdr *extra = &pe->pe_opthdr;
547   PEAOUTHDR *aouthdr_out = (PEAOUTHDR *) out;
548   bfd_vma sa, fa, ib;
549   IMAGE_DATA_DIRECTORY idata2, idata5, tls;
550   
551   if (pe->force_minimum_alignment)
552     {
553       if (!extra->FileAlignment)
554         extra->FileAlignment = PE_DEF_FILE_ALIGNMENT;
555       if (!extra->SectionAlignment)
556         extra->SectionAlignment = PE_DEF_SECTION_ALIGNMENT;
557     }
558
559   if (extra->Subsystem == IMAGE_SUBSYSTEM_UNKNOWN)
560     extra->Subsystem = pe->target_subsystem;
561
562   sa = extra->SectionAlignment;
563   fa = extra->FileAlignment;
564   ib = extra->ImageBase;
565
566   idata2 = pe->pe_opthdr.DataDirectory[PE_IMPORT_TABLE];
567   idata5 = pe->pe_opthdr.DataDirectory[PE_IMPORT_ADDRESS_TABLE];
568   tls = pe->pe_opthdr.DataDirectory[PE_TLS_TABLE];
569   
570   if (aouthdr_in->tsize)
571     {
572       aouthdr_in->text_start -= ib;
573 #if !defined(COFF_WITH_pep) && !defined(COFF_WITH_pex64)
574       aouthdr_in->text_start &= 0xffffffff;
575 #endif
576     }
577
578   if (aouthdr_in->dsize)
579     {
580       aouthdr_in->data_start -= ib;
581 #if !defined(COFF_WITH_pep) && !defined(COFF_WITH_pex64)
582       aouthdr_in->data_start &= 0xffffffff;
583 #endif
584     }
585
586   if (aouthdr_in->entry)
587     {
588       aouthdr_in->entry -= ib;
589 #if !defined(COFF_WITH_pep) && !defined(COFF_WITH_pex64)
590       aouthdr_in->entry &= 0xffffffff;
591 #endif
592     }
593
594 #define FA(x) (((x) + fa -1 ) & (- fa))
595 #define SA(x) (((x) + sa -1 ) & (- sa))
596
597   /* We like to have the sizes aligned.  */
598   aouthdr_in->bsize = FA (aouthdr_in->bsize);
599
600   extra->NumberOfRvaAndSizes = IMAGE_NUMBEROF_DIRECTORY_ENTRIES;
601
602   /* First null out all data directory entries.  */
603   memset (extra->DataDirectory, 0, sizeof (extra->DataDirectory));
604
605   add_data_entry (abfd, extra, 0, ".edata", ib);
606   add_data_entry (abfd, extra, 2, ".rsrc", ib);
607   add_data_entry (abfd, extra, 3, ".pdata", ib);
608
609   /* In theory we do not need to call add_data_entry for .idata$2 or
610      .idata$5.  It will be done in bfd_coff_final_link where all the
611      required information is available.  If however, we are not going
612      to perform a final link, eg because we have been invoked by objcopy
613      or strip, then we need to make sure that these Data Directory
614      entries are initialised properly.
615
616      So - we copy the input values into the output values, and then, if
617      a final link is going to be performed, it can overwrite them.  */
618   extra->DataDirectory[PE_IMPORT_TABLE]  = idata2;
619   extra->DataDirectory[PE_IMPORT_ADDRESS_TABLE] = idata5;
620   extra->DataDirectory[PE_TLS_TABLE] = tls;
621
622   if (extra->DataDirectory[PE_IMPORT_TABLE].VirtualAddress == 0)
623     /* Until other .idata fixes are made (pending patch), the entry for
624        .idata is needed for backwards compatibility.  FIXME.  */
625     add_data_entry (abfd, extra, 1, ".idata", ib);
626     
627   /* For some reason, the virtual size (which is what's set by
628      add_data_entry) for .reloc is not the same as the size recorded
629      in this slot by MSVC; it doesn't seem to cause problems (so far),
630      but since it's the best we've got, use it.  It does do the right
631      thing for .pdata.  */
632   if (pe->has_reloc_section)
633     add_data_entry (abfd, extra, 5, ".reloc", ib);
634
635   {
636     asection *sec;
637     bfd_vma hsize = 0;
638     bfd_vma dsize = 0;
639     bfd_vma isize = 0;
640     bfd_vma tsize = 0;
641
642     for (sec = abfd->sections; sec; sec = sec->next)
643       {
644         int rounded = FA (sec->size);
645
646         /* The first non-zero section filepos is the header size.
647            Sections without contents will have a filepos of 0.  */
648         if (hsize == 0)
649           hsize = sec->filepos;
650         if (sec->flags & SEC_DATA)
651           dsize += rounded;
652         if (sec->flags & SEC_CODE)
653           tsize += rounded;
654         /* The image size is the total VIRTUAL size (which is what is
655            in the virt_size field).  Files have been seen (from MSVC
656            5.0 link.exe) where the file size of the .data segment is
657            quite small compared to the virtual size.  Without this
658            fix, strip munges the file.
659
660            FIXME: We need to handle holes between sections, which may
661            happpen when we covert from another format.  We just use
662            the virtual address and virtual size of the last section
663            for the image size.  */
664         if (coff_section_data (abfd, sec) != NULL
665             && pei_section_data (abfd, sec) != NULL)
666           isize = (sec->vma - extra->ImageBase
667                    + SA (FA (pei_section_data (abfd, sec)->virt_size)));
668       }
669
670     aouthdr_in->dsize = dsize;
671     aouthdr_in->tsize = tsize;
672     extra->SizeOfHeaders = hsize;
673     extra->SizeOfImage = isize;
674   }
675
676   H_PUT_16 (abfd, aouthdr_in->magic, aouthdr_out->standard.magic);
677
678 #define LINKER_VERSION 256 /* That is, 2.56 */
679
680   /* This piece of magic sets the "linker version" field to
681      LINKER_VERSION.  */
682   H_PUT_16 (abfd, (LINKER_VERSION / 100 + (LINKER_VERSION % 100) * 256),
683             aouthdr_out->standard.vstamp);
684
685   PUT_AOUTHDR_TSIZE (abfd, aouthdr_in->tsize, aouthdr_out->standard.tsize);
686   PUT_AOUTHDR_DSIZE (abfd, aouthdr_in->dsize, aouthdr_out->standard.dsize);
687   PUT_AOUTHDR_BSIZE (abfd, aouthdr_in->bsize, aouthdr_out->standard.bsize);
688   PUT_AOUTHDR_ENTRY (abfd, aouthdr_in->entry, aouthdr_out->standard.entry);
689   PUT_AOUTHDR_TEXT_START (abfd, aouthdr_in->text_start,
690                           aouthdr_out->standard.text_start);
691
692 #if !defined(COFF_WITH_pep) && !defined(COFF_WITH_pex64)
693   /* PE32+ does not have data_start member!  */
694   PUT_AOUTHDR_DATA_START (abfd, aouthdr_in->data_start,
695                           aouthdr_out->standard.data_start);
696 #endif
697
698   PUT_OPTHDR_IMAGE_BASE (abfd, extra->ImageBase, aouthdr_out->ImageBase);
699   H_PUT_32 (abfd, extra->SectionAlignment, aouthdr_out->SectionAlignment);
700   H_PUT_32 (abfd, extra->FileAlignment, aouthdr_out->FileAlignment);
701   H_PUT_16 (abfd, extra->MajorOperatingSystemVersion,
702             aouthdr_out->MajorOperatingSystemVersion);
703   H_PUT_16 (abfd, extra->MinorOperatingSystemVersion,
704             aouthdr_out->MinorOperatingSystemVersion);
705   H_PUT_16 (abfd, extra->MajorImageVersion, aouthdr_out->MajorImageVersion);
706   H_PUT_16 (abfd, extra->MinorImageVersion, aouthdr_out->MinorImageVersion);
707   H_PUT_16 (abfd, extra->MajorSubsystemVersion,
708             aouthdr_out->MajorSubsystemVersion);
709   H_PUT_16 (abfd, extra->MinorSubsystemVersion,
710             aouthdr_out->MinorSubsystemVersion);
711   H_PUT_32 (abfd, extra->Reserved1, aouthdr_out->Reserved1);
712   H_PUT_32 (abfd, extra->SizeOfImage, aouthdr_out->SizeOfImage);
713   H_PUT_32 (abfd, extra->SizeOfHeaders, aouthdr_out->SizeOfHeaders);
714   H_PUT_32 (abfd, extra->CheckSum, aouthdr_out->CheckSum);
715   H_PUT_16 (abfd, extra->Subsystem, aouthdr_out->Subsystem);
716   H_PUT_16 (abfd, extra->DllCharacteristics, aouthdr_out->DllCharacteristics);
717   PUT_OPTHDR_SIZE_OF_STACK_RESERVE (abfd, extra->SizeOfStackReserve,
718                                     aouthdr_out->SizeOfStackReserve);
719   PUT_OPTHDR_SIZE_OF_STACK_COMMIT (abfd, extra->SizeOfStackCommit,
720                                    aouthdr_out->SizeOfStackCommit);
721   PUT_OPTHDR_SIZE_OF_HEAP_RESERVE (abfd, extra->SizeOfHeapReserve,
722                                    aouthdr_out->SizeOfHeapReserve);
723   PUT_OPTHDR_SIZE_OF_HEAP_COMMIT (abfd, extra->SizeOfHeapCommit,
724                                   aouthdr_out->SizeOfHeapCommit);
725   H_PUT_32 (abfd, extra->LoaderFlags, aouthdr_out->LoaderFlags);
726   H_PUT_32 (abfd, extra->NumberOfRvaAndSizes,
727             aouthdr_out->NumberOfRvaAndSizes);
728   {
729     int idx;
730
731     for (idx = 0; idx < 16; idx++)
732       {
733         H_PUT_32 (abfd, extra->DataDirectory[idx].VirtualAddress,
734                   aouthdr_out->DataDirectory[idx][0]);
735         H_PUT_32 (abfd, extra->DataDirectory[idx].Size,
736                   aouthdr_out->DataDirectory[idx][1]);
737       }
738   }
739
740   return AOUTSZ;
741 }
742
743 unsigned int
744 _bfd_XXi_only_swap_filehdr_out (bfd * abfd, void * in, void * out)
745 {
746   int idx;
747   struct internal_filehdr *filehdr_in = (struct internal_filehdr *) in;
748   struct external_PEI_filehdr *filehdr_out = (struct external_PEI_filehdr *) out;
749
750   if (pe_data (abfd)->has_reloc_section)
751     filehdr_in->f_flags &= ~F_RELFLG;
752
753   if (pe_data (abfd)->dll)
754     filehdr_in->f_flags |= F_DLL;
755
756   filehdr_in->pe.e_magic    = DOSMAGIC;
757   filehdr_in->pe.e_cblp     = 0x90;
758   filehdr_in->pe.e_cp       = 0x3;
759   filehdr_in->pe.e_crlc     = 0x0;
760   filehdr_in->pe.e_cparhdr  = 0x4;
761   filehdr_in->pe.e_minalloc = 0x0;
762   filehdr_in->pe.e_maxalloc = 0xffff;
763   filehdr_in->pe.e_ss       = 0x0;
764   filehdr_in->pe.e_sp       = 0xb8;
765   filehdr_in->pe.e_csum     = 0x0;
766   filehdr_in->pe.e_ip       = 0x0;
767   filehdr_in->pe.e_cs       = 0x0;
768   filehdr_in->pe.e_lfarlc   = 0x40;
769   filehdr_in->pe.e_ovno     = 0x0;
770
771   for (idx = 0; idx < 4; idx++)
772     filehdr_in->pe.e_res[idx] = 0x0;
773
774   filehdr_in->pe.e_oemid   = 0x0;
775   filehdr_in->pe.e_oeminfo = 0x0;
776
777   for (idx = 0; idx < 10; idx++)
778     filehdr_in->pe.e_res2[idx] = 0x0;
779
780   filehdr_in->pe.e_lfanew = 0x80;
781
782   /* This next collection of data are mostly just characters.  It
783      appears to be constant within the headers put on NT exes.  */
784   filehdr_in->pe.dos_message[0]  = 0x0eba1f0e;
785   filehdr_in->pe.dos_message[1]  = 0xcd09b400;
786   filehdr_in->pe.dos_message[2]  = 0x4c01b821;
787   filehdr_in->pe.dos_message[3]  = 0x685421cd;
788   filehdr_in->pe.dos_message[4]  = 0x70207369;
789   filehdr_in->pe.dos_message[5]  = 0x72676f72;
790   filehdr_in->pe.dos_message[6]  = 0x63206d61;
791   filehdr_in->pe.dos_message[7]  = 0x6f6e6e61;
792   filehdr_in->pe.dos_message[8]  = 0x65622074;
793   filehdr_in->pe.dos_message[9]  = 0x6e757220;
794   filehdr_in->pe.dos_message[10] = 0x206e6920;
795   filehdr_in->pe.dos_message[11] = 0x20534f44;
796   filehdr_in->pe.dos_message[12] = 0x65646f6d;
797   filehdr_in->pe.dos_message[13] = 0x0a0d0d2e;
798   filehdr_in->pe.dos_message[14] = 0x24;
799   filehdr_in->pe.dos_message[15] = 0x0;
800   filehdr_in->pe.nt_signature = NT_SIGNATURE;
801
802   H_PUT_16 (abfd, filehdr_in->f_magic, filehdr_out->f_magic);
803   H_PUT_16 (abfd, filehdr_in->f_nscns, filehdr_out->f_nscns);
804
805   H_PUT_32 (abfd, time (0), filehdr_out->f_timdat);
806   PUT_FILEHDR_SYMPTR (abfd, filehdr_in->f_symptr,
807                       filehdr_out->f_symptr);
808   H_PUT_32 (abfd, filehdr_in->f_nsyms, filehdr_out->f_nsyms);
809   H_PUT_16 (abfd, filehdr_in->f_opthdr, filehdr_out->f_opthdr);
810   H_PUT_16 (abfd, filehdr_in->f_flags, filehdr_out->f_flags);
811
812   /* Put in extra dos header stuff.  This data remains essentially
813      constant, it just has to be tacked on to the beginning of all exes
814      for NT.  */
815   H_PUT_16 (abfd, filehdr_in->pe.e_magic, filehdr_out->e_magic);
816   H_PUT_16 (abfd, filehdr_in->pe.e_cblp, filehdr_out->e_cblp);
817   H_PUT_16 (abfd, filehdr_in->pe.e_cp, filehdr_out->e_cp);
818   H_PUT_16 (abfd, filehdr_in->pe.e_crlc, filehdr_out->e_crlc);
819   H_PUT_16 (abfd, filehdr_in->pe.e_cparhdr, filehdr_out->e_cparhdr);
820   H_PUT_16 (abfd, filehdr_in->pe.e_minalloc, filehdr_out->e_minalloc);
821   H_PUT_16 (abfd, filehdr_in->pe.e_maxalloc, filehdr_out->e_maxalloc);
822   H_PUT_16 (abfd, filehdr_in->pe.e_ss, filehdr_out->e_ss);
823   H_PUT_16 (abfd, filehdr_in->pe.e_sp, filehdr_out->e_sp);
824   H_PUT_16 (abfd, filehdr_in->pe.e_csum, filehdr_out->e_csum);
825   H_PUT_16 (abfd, filehdr_in->pe.e_ip, filehdr_out->e_ip);
826   H_PUT_16 (abfd, filehdr_in->pe.e_cs, filehdr_out->e_cs);
827   H_PUT_16 (abfd, filehdr_in->pe.e_lfarlc, filehdr_out->e_lfarlc);
828   H_PUT_16 (abfd, filehdr_in->pe.e_ovno, filehdr_out->e_ovno);
829
830   for (idx = 0; idx < 4; idx++)
831     H_PUT_16 (abfd, filehdr_in->pe.e_res[idx], filehdr_out->e_res[idx]);
832
833   H_PUT_16 (abfd, filehdr_in->pe.e_oemid, filehdr_out->e_oemid);
834   H_PUT_16 (abfd, filehdr_in->pe.e_oeminfo, filehdr_out->e_oeminfo);
835
836   for (idx = 0; idx < 10; idx++)
837     H_PUT_16 (abfd, filehdr_in->pe.e_res2[idx], filehdr_out->e_res2[idx]);
838
839   H_PUT_32 (abfd, filehdr_in->pe.e_lfanew, filehdr_out->e_lfanew);
840
841   for (idx = 0; idx < 16; idx++)
842     H_PUT_32 (abfd, filehdr_in->pe.dos_message[idx],
843               filehdr_out->dos_message[idx]);
844
845   /* Also put in the NT signature.  */
846   H_PUT_32 (abfd, filehdr_in->pe.nt_signature, filehdr_out->nt_signature);
847
848   return FILHSZ;
849 }
850
851 unsigned int
852 _bfd_XX_only_swap_filehdr_out (bfd * abfd, void * in, void * out)
853 {
854   struct internal_filehdr *filehdr_in = (struct internal_filehdr *) in;
855   FILHDR *filehdr_out = (FILHDR *) out;
856
857   H_PUT_16 (abfd, filehdr_in->f_magic, filehdr_out->f_magic);
858   H_PUT_16 (abfd, filehdr_in->f_nscns, filehdr_out->f_nscns);
859   H_PUT_32 (abfd, filehdr_in->f_timdat, filehdr_out->f_timdat);
860   PUT_FILEHDR_SYMPTR (abfd, filehdr_in->f_symptr, filehdr_out->f_symptr);
861   H_PUT_32 (abfd, filehdr_in->f_nsyms, filehdr_out->f_nsyms);
862   H_PUT_16 (abfd, filehdr_in->f_opthdr, filehdr_out->f_opthdr);
863   H_PUT_16 (abfd, filehdr_in->f_flags, filehdr_out->f_flags);
864
865   return FILHSZ;
866 }
867
868 unsigned int
869 _bfd_XXi_swap_scnhdr_out (bfd * abfd, void * in, void * out)
870 {
871   struct internal_scnhdr *scnhdr_int = (struct internal_scnhdr *) in;
872   SCNHDR *scnhdr_ext = (SCNHDR *) out;
873   unsigned int ret = SCNHSZ;
874   bfd_vma ps;
875   bfd_vma ss;
876
877   memcpy (scnhdr_ext->s_name, scnhdr_int->s_name, sizeof (scnhdr_int->s_name));
878
879   PUT_SCNHDR_VADDR (abfd,
880                     ((scnhdr_int->s_vaddr
881                       - pe_data (abfd)->pe_opthdr.ImageBase)
882                      & 0xffffffff),
883                     scnhdr_ext->s_vaddr);
884
885   /* NT wants the size data to be rounded up to the next
886      NT_FILE_ALIGNMENT, but zero if it has no content (as in .bss,
887      sometimes).  */
888   if ((scnhdr_int->s_flags & IMAGE_SCN_CNT_UNINITIALIZED_DATA) != 0)
889     {
890       if (bfd_pei_p (abfd))
891         {
892           ps = scnhdr_int->s_size;
893           ss = 0;
894         }
895       else
896        {
897          ps = 0;
898          ss = scnhdr_int->s_size;
899        }
900     }
901   else
902     {
903       if (bfd_pei_p (abfd))
904         ps = scnhdr_int->s_paddr;
905       else
906         ps = 0;
907
908       ss = scnhdr_int->s_size;
909     }
910
911   PUT_SCNHDR_SIZE (abfd, ss,
912                    scnhdr_ext->s_size);
913
914   /* s_paddr in PE is really the virtual size.  */
915   PUT_SCNHDR_PADDR (abfd, ps, scnhdr_ext->s_paddr);
916
917   PUT_SCNHDR_SCNPTR (abfd, scnhdr_int->s_scnptr,
918                      scnhdr_ext->s_scnptr);
919   PUT_SCNHDR_RELPTR (abfd, scnhdr_int->s_relptr,
920                      scnhdr_ext->s_relptr);
921   PUT_SCNHDR_LNNOPTR (abfd, scnhdr_int->s_lnnoptr,
922                       scnhdr_ext->s_lnnoptr);
923
924   {
925     /* Extra flags must be set when dealing with PE.  All sections should also
926        have the IMAGE_SCN_MEM_READ (0x40000000) flag set.  In addition, the
927        .text section must have IMAGE_SCN_MEM_EXECUTE (0x20000000) and the data
928        sections (.idata, .data, .bss, .CRT) must have IMAGE_SCN_MEM_WRITE set
929        (this is especially important when dealing with the .idata section since
930        the addresses for routines from .dlls must be overwritten).  If .reloc
931        section data is ever generated, we must add IMAGE_SCN_MEM_DISCARDABLE
932        (0x02000000).  Also, the resource data should also be read and
933        writable.  */
934
935     /* FIXME: Alignment is also encoded in this field, at least on PPC and 
936        ARM-WINCE.  Although - how do we get the original alignment field
937        back ?  */
938
939     typedef struct
940     {
941       const char *      section_name;
942       unsigned long     must_have;
943     }
944     pe_required_section_flags;
945     
946     pe_required_section_flags known_sections [] =
947       {
948         { ".arch",  IMAGE_SCN_MEM_READ | IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_DISCARDABLE | IMAGE_SCN_ALIGN_8BYTES },
949         { ".bss",   IMAGE_SCN_MEM_READ | IMAGE_SCN_CNT_UNINITIALIZED_DATA | IMAGE_SCN_MEM_WRITE },
950         { ".data",  IMAGE_SCN_MEM_READ | IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_WRITE },
951         { ".edata", IMAGE_SCN_MEM_READ | IMAGE_SCN_CNT_INITIALIZED_DATA },
952         { ".idata", IMAGE_SCN_MEM_READ | IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_WRITE },
953         { ".pdata", IMAGE_SCN_MEM_READ | IMAGE_SCN_CNT_INITIALIZED_DATA },
954         { ".rdata", IMAGE_SCN_MEM_READ | IMAGE_SCN_CNT_INITIALIZED_DATA },
955         { ".reloc", IMAGE_SCN_MEM_READ | IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_DISCARDABLE },
956         { ".rsrc",  IMAGE_SCN_MEM_READ | IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_WRITE },
957         { ".text" , IMAGE_SCN_MEM_READ | IMAGE_SCN_CNT_CODE | IMAGE_SCN_MEM_EXECUTE },
958         { ".tls",   IMAGE_SCN_MEM_READ | IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_WRITE },
959         { ".xdata", IMAGE_SCN_MEM_READ | IMAGE_SCN_CNT_INITIALIZED_DATA },
960         { NULL, 0}
961       };
962
963     pe_required_section_flags * p;
964
965     /* We have defaulted to adding the IMAGE_SCN_MEM_WRITE flag, but now
966        we know exactly what this specific section wants so we remove it
967        and then allow the must_have field to add it back in if necessary.
968        However, we don't remove IMAGE_SCN_MEM_WRITE flag from .text if the
969        default WP_TEXT file flag has been cleared.  WP_TEXT may be cleared
970        by ld --enable-auto-import (if auto-import is actually needed),
971        by ld --omagic, or by obcopy --writable-text.  */
972
973     for (p = known_sections; p->section_name; p++)
974       if (strcmp (scnhdr_int->s_name, p->section_name) == 0)
975         {
976           if (strcmp (scnhdr_int->s_name, ".text")
977               || (bfd_get_file_flags (abfd) & WP_TEXT))
978             scnhdr_int->s_flags &= ~IMAGE_SCN_MEM_WRITE;
979           scnhdr_int->s_flags |= p->must_have;
980           break;
981         }
982
983     H_PUT_32 (abfd, scnhdr_int->s_flags, scnhdr_ext->s_flags);
984   }
985
986   if (coff_data (abfd)->link_info
987       && ! coff_data (abfd)->link_info->relocatable
988       && ! coff_data (abfd)->link_info->shared
989       && strcmp (scnhdr_int->s_name, ".text") == 0)
990     {
991       /* By inference from looking at MS output, the 32 bit field
992          which is the combination of the number_of_relocs and
993          number_of_linenos is used for the line number count in
994          executables.  A 16-bit field won't do for cc1.  The MS
995          document says that the number of relocs is zero for
996          executables, but the 17-th bit has been observed to be there.
997          Overflow is not an issue: a 4G-line program will overflow a
998          bunch of other fields long before this!  */
999       H_PUT_16 (abfd, (scnhdr_int->s_nlnno & 0xffff), scnhdr_ext->s_nlnno);
1000       H_PUT_16 (abfd, (scnhdr_int->s_nlnno >> 16), scnhdr_ext->s_nreloc);
1001     }
1002   else
1003     {
1004       if (scnhdr_int->s_nlnno <= 0xffff)
1005         H_PUT_16 (abfd, scnhdr_int->s_nlnno, scnhdr_ext->s_nlnno);
1006       else
1007         {
1008           (*_bfd_error_handler) (_("%s: line number overflow: 0x%lx > 0xffff"),
1009                                  bfd_get_filename (abfd),
1010                                  scnhdr_int->s_nlnno);
1011           bfd_set_error (bfd_error_file_truncated);
1012           H_PUT_16 (abfd, 0xffff, scnhdr_ext->s_nlnno);
1013           ret = 0;
1014         }
1015
1016       /* Although we could encode 0xffff relocs here, we do not, to be
1017          consistent with other parts of bfd. Also it lets us warn, as
1018          we should never see 0xffff here w/o having the overflow flag
1019          set.  */
1020       if (scnhdr_int->s_nreloc < 0xffff)
1021         H_PUT_16 (abfd, scnhdr_int->s_nreloc, scnhdr_ext->s_nreloc);
1022       else
1023         {
1024           /* PE can deal with large #s of relocs, but not here.  */
1025           H_PUT_16 (abfd, 0xffff, scnhdr_ext->s_nreloc);
1026           scnhdr_int->s_flags |= IMAGE_SCN_LNK_NRELOC_OVFL;
1027           H_PUT_32 (abfd, scnhdr_int->s_flags, scnhdr_ext->s_flags);
1028         }
1029     }
1030   return ret;
1031 }
1032
1033 static char * dir_names[IMAGE_NUMBEROF_DIRECTORY_ENTRIES] =
1034 {
1035   N_("Export Directory [.edata (or where ever we found it)]"),
1036   N_("Import Directory [parts of .idata]"),
1037   N_("Resource Directory [.rsrc]"),
1038   N_("Exception Directory [.pdata]"),
1039   N_("Security Directory"),
1040   N_("Base Relocation Directory [.reloc]"),
1041   N_("Debug Directory"),
1042   N_("Description Directory"),
1043   N_("Special Directory"),
1044   N_("Thread Storage Directory [.tls]"),
1045   N_("Load Configuration Directory"),
1046   N_("Bound Import Directory"),
1047   N_("Import Address Table Directory"),
1048   N_("Delay Import Directory"),
1049   N_("CLR Runtime Header"),
1050   N_("Reserved")
1051 };
1052
1053 #ifdef POWERPC_LE_PE
1054 /* The code for the PPC really falls in the "architecture dependent"
1055    category.  However, it's not clear that anyone will ever care, so
1056    we're ignoring the issue for now; if/when PPC matters, some of this
1057    may need to go into peicode.h, or arguments passed to enable the
1058    PPC- specific code.  */
1059 #endif
1060
1061 static bfd_boolean
1062 pe_print_idata (bfd * abfd, void * vfile)
1063 {
1064   FILE *file = (FILE *) vfile;
1065   bfd_byte *data;
1066   asection *section;
1067   bfd_signed_vma adj;
1068
1069 #ifdef POWERPC_LE_PE
1070   asection *rel_section = bfd_get_section_by_name (abfd, ".reldata");
1071 #endif
1072
1073   bfd_size_type datasize = 0;
1074   bfd_size_type dataoff;
1075   bfd_size_type i;
1076   int onaline = 20;
1077
1078   pe_data_type *pe = pe_data (abfd);
1079   struct internal_extra_pe_aouthdr *extra = &pe->pe_opthdr;
1080
1081   bfd_vma addr;
1082
1083   addr = extra->DataDirectory[PE_IMPORT_TABLE].VirtualAddress;
1084
1085   if (addr == 0 && extra->DataDirectory[PE_IMPORT_TABLE].Size == 0)
1086     {
1087       /* Maybe the extra header isn't there.  Look for the section.  */
1088       section = bfd_get_section_by_name (abfd, ".idata");
1089       if (section == NULL)
1090         return TRUE;
1091
1092       addr = section->vma;
1093       datasize = section->size;
1094       if (datasize == 0)
1095         return TRUE;
1096     }
1097   else
1098     {
1099       addr += extra->ImageBase;
1100       for (section = abfd->sections; section != NULL; section = section->next)
1101         {
1102           datasize = section->size;
1103           if (addr >= section->vma && addr < section->vma + datasize)
1104             break;
1105         }
1106
1107       if (section == NULL)
1108         {
1109           fprintf (file,
1110                    _("\nThere is an import table, but the section containing it could not be found\n"));
1111           return TRUE;
1112         }
1113     }
1114
1115   fprintf (file, _("\nThere is an import table in %s at 0x%lx\n"),
1116            section->name, (unsigned long) addr);
1117
1118   dataoff = addr - section->vma;
1119   datasize -= dataoff;
1120
1121 #ifdef POWERPC_LE_PE
1122   if (rel_section != 0 && rel_section->size != 0)
1123     {
1124       /* The toc address can be found by taking the starting address,
1125          which on the PPC locates a function descriptor. The
1126          descriptor consists of the function code starting address
1127          followed by the address of the toc. The starting address we
1128          get from the bfd, and the descriptor is supposed to be in the
1129          .reldata section.  */
1130
1131       bfd_vma loadable_toc_address;
1132       bfd_vma toc_address;
1133       bfd_vma start_address;
1134       bfd_byte *data;
1135       bfd_vma offset;
1136
1137       if (!bfd_malloc_and_get_section (abfd, rel_section, &data))
1138         {
1139           if (data != NULL)
1140             free (data);
1141           return FALSE;
1142         }
1143
1144       offset = abfd->start_address - rel_section->vma;
1145
1146       if (offset >= rel_section->size || offset + 8 > rel_section->size)
1147         {
1148           if (data != NULL)
1149             free (data);
1150           return FALSE;
1151         }
1152
1153       start_address = bfd_get_32 (abfd, data + offset);
1154       loadable_toc_address = bfd_get_32 (abfd, data + offset + 4);
1155       toc_address = loadable_toc_address - 32768;
1156
1157       fprintf (file,
1158                _("\nFunction descriptor located at the start address: %04lx\n"),
1159                (unsigned long int) (abfd->start_address));
1160       fprintf (file,
1161                _("\tcode-base %08lx toc (loadable/actual) %08lx/%08lx\n"),
1162                start_address, loadable_toc_address, toc_address);
1163       if (data != NULL)
1164         free (data);
1165     }
1166   else
1167     {
1168       fprintf (file,
1169                _("\nNo reldata section! Function descriptor not decoded.\n"));
1170     }
1171 #endif
1172
1173   fprintf (file,
1174            _("\nThe Import Tables (interpreted %s section contents)\n"),
1175            section->name);
1176   fprintf (file,
1177            _("\
1178  vma:            Hint    Time      Forward  DLL       First\n\
1179                  Table   Stamp     Chain    Name      Thunk\n"));
1180
1181   /* Read the whole section.  Some of the fields might be before dataoff.  */
1182   if (!bfd_malloc_and_get_section (abfd, section, &data))
1183     {
1184       if (data != NULL)
1185         free (data);
1186       return FALSE;
1187     }
1188
1189   adj = section->vma - extra->ImageBase;
1190
1191   /* Print all image import descriptors.  */
1192   for (i = 0; i < datasize; i += onaline)
1193     {
1194       bfd_vma hint_addr;
1195       bfd_vma time_stamp;
1196       bfd_vma forward_chain;
1197       bfd_vma dll_name;
1198       bfd_vma first_thunk;
1199       int idx = 0;
1200       bfd_size_type j;
1201       char *dll;
1202
1203       /* Print (i + extra->DataDirectory[PE_IMPORT_TABLE].VirtualAddress).  */
1204       fprintf (file, " %08lx\t", (unsigned long) (i + adj + dataoff));
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       if (dll_name - adj >= section->size)
1222         break;
1223
1224       dll = (char *) data + dll_name - adj;
1225       fprintf (file, _("\n\tDLL Name: %s\n"), dll);
1226
1227       if (hint_addr != 0)
1228         {
1229           bfd_byte *ft_data;
1230           asection *ft_section;
1231           bfd_vma ft_addr;
1232           bfd_size_type ft_datasize;
1233           int ft_idx;
1234           int ft_allocated = 0;
1235
1236           fprintf (file, _("\tvma:  Hint/Ord Member-Name Bound-To\n"));
1237
1238           idx = hint_addr - adj;
1239           
1240           ft_addr = first_thunk + extra->ImageBase;
1241           ft_data = data;
1242           ft_idx = first_thunk - adj;
1243           ft_allocated = 0; 
1244
1245           if (first_thunk != hint_addr)
1246             {
1247               /* Find the section which contains the first thunk.  */
1248               for (ft_section = abfd->sections;
1249                    ft_section != NULL;
1250                    ft_section = ft_section->next)
1251                 {
1252                   ft_datasize = ft_section->size;
1253                   if (ft_addr >= ft_section->vma
1254                       && ft_addr < ft_section->vma + ft_datasize)
1255                     break;
1256                 }
1257
1258               if (ft_section == NULL)
1259                 {
1260                   fprintf (file,
1261                        _("\nThere is a first thunk, but the section containing it could not be found\n"));
1262                   continue;
1263                 }
1264
1265               /* Now check to see if this section is the same as our current
1266                  section.  If it is not then we will have to load its data in.  */
1267               if (ft_section == section)
1268                 {
1269                   ft_data = data;
1270                   ft_idx = first_thunk - adj;
1271                 }
1272               else
1273                 {
1274                   ft_idx = first_thunk - (ft_section->vma - extra->ImageBase);
1275                   ft_data = bfd_malloc (datasize);
1276                   if (ft_data == NULL)
1277                     continue;
1278
1279                   /* Read datasize bfd_bytes starting at offset ft_idx.  */
1280                   if (! bfd_get_section_contents
1281                       (abfd, ft_section, ft_data, (bfd_vma) ft_idx, datasize))
1282                     {
1283                       free (ft_data);
1284                       continue;
1285                     }
1286
1287                   ft_idx = 0;
1288                   ft_allocated = 1;
1289                 }
1290             }
1291
1292           /* Print HintName vector entries.  */
1293 #ifdef COFF_WITH_pex64
1294           for (j = 0; j < datasize; j += 8)
1295             {
1296               unsigned long member = bfd_get_32 (abfd, data + idx + j);
1297               unsigned long member_high = bfd_get_32 (abfd, data + idx + j + 4);
1298
1299               if (!member && !member_high)
1300                 break;
1301
1302               if (member_high & 0x80000000)
1303                 fprintf (file, "\t%lx%08lx\t %4lx%08lx  <none>",
1304                          member_high,member, member_high & 0x7fffffff, member);
1305               else
1306                 {
1307                   int ordinal;
1308                   char *member_name;
1309
1310                   ordinal = bfd_get_16 (abfd, data + member - adj);
1311                   member_name = (char *) data + member - adj + 2;
1312                   fprintf (file, "\t%04lx\t %4d  %s",member, ordinal, member_name);
1313                 }
1314
1315               /* If the time stamp is not zero, the import address
1316                  table holds actual addresses.  */
1317               if (time_stamp != 0
1318                   && first_thunk != 0
1319                   && first_thunk != hint_addr)
1320                 fprintf (file, "\t%04lx",
1321                          (unsigned long) bfd_get_32 (abfd, ft_data + ft_idx + j));
1322               fprintf (file, "\n");
1323             }
1324 #else
1325           for (j = 0; j < datasize; j += 4)
1326             {
1327               unsigned long member = bfd_get_32 (abfd, data + idx + j);
1328
1329               /* Print single IMAGE_IMPORT_BY_NAME vector.  */ 
1330               if (member == 0)
1331                 break;
1332
1333               if (member & 0x80000000)
1334                 fprintf (file, "\t%04lx\t %4lu  <none>",
1335                          member, member & 0x7fffffff);
1336               else
1337                 {
1338                   int ordinal;
1339                   char *member_name;
1340
1341                   ordinal = bfd_get_16 (abfd, data + member - adj);
1342                   member_name = (char *) data + member - adj + 2;
1343                   fprintf (file, "\t%04lx\t %4d  %s",
1344                            member, ordinal, member_name);
1345                 }
1346
1347               /* If the time stamp is not zero, the import address
1348                  table holds actual addresses.  */
1349               if (time_stamp != 0
1350                   && first_thunk != 0
1351                   && first_thunk != hint_addr)
1352                 fprintf (file, "\t%04lx",
1353                          (unsigned long) bfd_get_32 (abfd, ft_data + ft_idx + j));
1354
1355               fprintf (file, "\n");
1356             }
1357 #endif
1358           if (ft_allocated)
1359             free (ft_data);
1360         }
1361
1362       fprintf (file, "\n");
1363     }
1364
1365   free (data);
1366
1367   return TRUE;
1368 }
1369
1370 static bfd_boolean
1371 pe_print_edata (bfd * abfd, void * vfile)
1372 {
1373   FILE *file = (FILE *) vfile;
1374   bfd_byte *data;
1375   asection *section;
1376   bfd_size_type datasize = 0;
1377   bfd_size_type dataoff;
1378   bfd_size_type i;
1379   bfd_signed_vma adj;
1380   struct EDT_type
1381   {
1382     long export_flags;          /* Reserved - should be zero.  */
1383     long time_stamp;
1384     short major_ver;
1385     short minor_ver;
1386     bfd_vma name;               /* RVA - relative to image base.  */
1387     long base;                  /* Ordinal base.  */
1388     unsigned long num_functions;/* Number in the export address table.  */
1389     unsigned long num_names;    /* Number in the name pointer table.  */
1390     bfd_vma eat_addr;           /* RVA to the export address table.  */
1391     bfd_vma npt_addr;           /* RVA to the Export Name Pointer Table.  */
1392     bfd_vma ot_addr;            /* RVA to the Ordinal Table.  */
1393   } edt;
1394
1395   pe_data_type *pe = pe_data (abfd);
1396   struct internal_extra_pe_aouthdr *extra = &pe->pe_opthdr;
1397
1398   bfd_vma addr;
1399
1400   addr = extra->DataDirectory[PE_EXPORT_TABLE].VirtualAddress;
1401
1402   if (addr == 0 && extra->DataDirectory[PE_EXPORT_TABLE].Size == 0)
1403     {
1404       /* Maybe the extra header isn't there.  Look for the section.  */
1405       section = bfd_get_section_by_name (abfd, ".edata");
1406       if (section == NULL)
1407         return TRUE;
1408
1409       addr = section->vma;
1410       dataoff = 0;
1411       datasize = section->size;
1412       if (datasize == 0)
1413         return TRUE;
1414     }
1415   else
1416     {
1417       addr += extra->ImageBase;
1418
1419       for (section = abfd->sections; section != NULL; section = section->next)
1420         if (addr >= section->vma && addr < section->vma + section->size)
1421           break;
1422
1423       if (section == NULL)
1424         {
1425           fprintf (file,
1426                    _("\nThere is an export table, but the section containing it could not be found\n"));
1427           return TRUE;
1428         }
1429
1430       dataoff = addr - section->vma;
1431       datasize = extra->DataDirectory[PE_EXPORT_TABLE].Size;
1432       if (datasize > section->size - dataoff)
1433         {
1434           fprintf (file,
1435                    _("\nThere is an export table in %s, but it does not fit into that section\n"),
1436                    section->name);
1437           return TRUE;
1438         }
1439     }
1440
1441   fprintf (file, _("\nThere is an export table in %s at 0x%lx\n"),
1442            section->name, (unsigned long) addr);
1443
1444   data = bfd_malloc (datasize);
1445   if (data == NULL)
1446     return FALSE;
1447
1448   if (! bfd_get_section_contents (abfd, section, data,
1449                                   (file_ptr) dataoff, datasize))
1450     return FALSE;
1451
1452   /* Go get Export Directory Table.  */
1453   edt.export_flags   = bfd_get_32 (abfd, data +  0);
1454   edt.time_stamp     = bfd_get_32 (abfd, data +  4);
1455   edt.major_ver      = bfd_get_16 (abfd, data +  8);
1456   edt.minor_ver      = bfd_get_16 (abfd, data + 10);
1457   edt.name           = bfd_get_32 (abfd, data + 12);
1458   edt.base           = bfd_get_32 (abfd, data + 16);
1459   edt.num_functions  = bfd_get_32 (abfd, data + 20);
1460   edt.num_names      = bfd_get_32 (abfd, data + 24);
1461   edt.eat_addr       = bfd_get_32 (abfd, data + 28);
1462   edt.npt_addr       = bfd_get_32 (abfd, data + 32);
1463   edt.ot_addr        = bfd_get_32 (abfd, data + 36);
1464
1465   adj = section->vma - extra->ImageBase + dataoff;
1466
1467   /* Dump the EDT first.  */
1468   fprintf (file,
1469            _("\nThe Export Tables (interpreted %s section contents)\n\n"),
1470            section->name);
1471
1472   fprintf (file,
1473            _("Export Flags \t\t\t%lx\n"), (unsigned long) edt.export_flags);
1474
1475   fprintf (file,
1476            _("Time/Date stamp \t\t%lx\n"), (unsigned long) edt.time_stamp);
1477
1478   fprintf (file,
1479            _("Major/Minor \t\t\t%d/%d\n"), edt.major_ver, edt.minor_ver);
1480
1481   fprintf (file,
1482            _("Name \t\t\t\t"));
1483   fprintf_vma (file, edt.name);
1484   fprintf (file,
1485            " %s\n", data + edt.name - adj);
1486
1487   fprintf (file,
1488            _("Ordinal Base \t\t\t%ld\n"), edt.base);
1489
1490   fprintf (file,
1491            _("Number in:\n"));
1492
1493   fprintf (file,
1494            _("\tExport Address Table \t\t%08lx\n"),
1495            edt.num_functions);
1496
1497   fprintf (file,
1498            _("\t[Name Pointer/Ordinal] Table\t%08lx\n"), edt.num_names);
1499
1500   fprintf (file,
1501            _("Table Addresses\n"));
1502
1503   fprintf (file,
1504            _("\tExport Address Table \t\t"));
1505   fprintf_vma (file, edt.eat_addr);
1506   fprintf (file, "\n");
1507
1508   fprintf (file,
1509            _("\tName Pointer Table \t\t"));
1510   fprintf_vma (file, edt.npt_addr);
1511   fprintf (file, "\n");
1512
1513   fprintf (file,
1514            _("\tOrdinal Table \t\t\t"));
1515   fprintf_vma (file, edt.ot_addr);
1516   fprintf (file, "\n");
1517
1518   /* The next table to find is the Export Address Table. It's basically
1519      a list of pointers that either locate a function in this dll, or
1520      forward the call to another dll. Something like:
1521       typedef union
1522       {
1523         long export_rva;
1524         long forwarder_rva;
1525       } export_address_table_entry;  */
1526
1527   fprintf (file,
1528           _("\nExport Address Table -- Ordinal Base %ld\n"),
1529           edt.base);
1530
1531   for (i = 0; i < edt.num_functions; ++i)
1532     {
1533       bfd_vma eat_member = bfd_get_32 (abfd,
1534                                        data + edt.eat_addr + (i * 4) - adj);
1535       if (eat_member == 0)
1536         continue;
1537
1538       if (eat_member - adj <= datasize)
1539         {
1540           /* This rva is to a name (forwarding function) in our section.  */
1541           /* Should locate a function descriptor.  */
1542           fprintf (file,
1543                    "\t[%4ld] +base[%4ld] %04lx %s -- %s\n",
1544                    (long) i,
1545                    (long) (i + edt.base),
1546                    (unsigned long) eat_member,
1547                    _("Forwarder RVA"),
1548                    data + eat_member - adj);
1549         }
1550       else
1551         {
1552           /* Should locate a function descriptor in the reldata section.  */
1553           fprintf (file,
1554                    "\t[%4ld] +base[%4ld] %04lx %s\n",
1555                    (long) i,
1556                    (long) (i + edt.base),
1557                    (unsigned long) eat_member,
1558                    _("Export RVA"));
1559         }
1560     }
1561
1562   /* The Export Name Pointer Table is paired with the Export Ordinal Table.  */
1563   /* Dump them in parallel for clarity.  */
1564   fprintf (file,
1565            _("\n[Ordinal/Name Pointer] Table\n"));
1566
1567   for (i = 0; i < edt.num_names; ++i)
1568     {
1569       bfd_vma name_ptr = bfd_get_32 (abfd,
1570                                     data +
1571                                     edt.npt_addr
1572                                     + (i*4) - adj);
1573
1574       char *name = (char *) data + name_ptr - adj;
1575
1576       bfd_vma ord = bfd_get_16 (abfd,
1577                                     data +
1578                                     edt.ot_addr
1579                                     + (i*2) - adj);
1580       fprintf (file,
1581               "\t[%4ld] %s\n", (long) ord, name);
1582     }
1583
1584   free (data);
1585
1586   return TRUE;
1587 }
1588
1589 /* This really is architecture dependent.  On IA-64, a .pdata entry
1590    consists of three dwords containing relative virtual addresses that
1591    specify the start and end address of the code range the entry
1592    covers and the address of the corresponding unwind info data. 
1593
1594    On ARM and SH-4, a compressed PDATA structure is used :
1595    _IMAGE_CE_RUNTIME_FUNCTION_ENTRY, whereas MIPS is documented to use
1596    _IMAGE_ALPHA_RUNTIME_FUNCTION_ENTRY.
1597    See http://msdn2.microsoft.com/en-us/library/ms253988(VS.80).aspx .
1598
1599    This is the version for uncompressed data.  */
1600
1601 static bfd_boolean
1602 pe_print_pdata (bfd * abfd, void * vfile)
1603 {
1604 #if defined(COFF_WITH_pep) && !defined(COFF_WITH_pex64)
1605 # define PDATA_ROW_SIZE (3 * 8)
1606 #else
1607 # define PDATA_ROW_SIZE (5 * 4)
1608 #endif
1609   FILE *file = (FILE *) vfile;
1610   bfd_byte *data = 0;
1611   asection *section = bfd_get_section_by_name (abfd, ".pdata");
1612   bfd_size_type datasize = 0;
1613   bfd_size_type i;
1614   bfd_size_type start, stop;
1615   int onaline = PDATA_ROW_SIZE;
1616
1617   if (section == NULL
1618       || coff_section_data (abfd, section) == NULL
1619       || pei_section_data (abfd, section) == NULL)
1620     return TRUE;
1621
1622   stop = pei_section_data (abfd, section)->virt_size;
1623   if ((stop % onaline) != 0)
1624     fprintf (file,
1625              _("Warning, .pdata section size (%ld) is not a multiple of %d\n"),
1626              (long) stop, onaline);
1627
1628   fprintf (file,
1629            _("\nThe Function Table (interpreted .pdata section contents)\n"));
1630 #if defined(COFF_WITH_pep) && !defined(COFF_WITH_pex64)
1631   fprintf (file,
1632            _(" vma:\t\t\tBegin Address    End Address      Unwind Info\n"));
1633 #else
1634   fprintf (file, _("\
1635  vma:\t\tBegin    End      EH       EH       PrologEnd  Exception\n\
1636      \t\tAddress  Address  Handler  Data     Address    Mask\n"));
1637 #endif
1638
1639   datasize = section->size;
1640   if (datasize == 0)
1641     return TRUE;
1642
1643   if (! bfd_malloc_and_get_section (abfd, section, &data))
1644     {
1645       if (data != NULL)
1646         free (data);
1647       return FALSE;
1648     }
1649
1650   start = 0;
1651
1652   for (i = start; i < stop; i += onaline)
1653     {
1654       bfd_vma begin_addr;
1655       bfd_vma end_addr;
1656       bfd_vma eh_handler;
1657       bfd_vma eh_data;
1658       bfd_vma prolog_end_addr;
1659       int em_data;
1660
1661       if (i + PDATA_ROW_SIZE > stop)
1662         break;
1663
1664       begin_addr      = GET_PDATA_ENTRY (abfd, data + i     );
1665       end_addr        = GET_PDATA_ENTRY (abfd, data + i +  4);
1666       eh_handler      = GET_PDATA_ENTRY (abfd, data + i +  8);
1667       eh_data         = GET_PDATA_ENTRY (abfd, data + i + 12);
1668       prolog_end_addr = GET_PDATA_ENTRY (abfd, data + i + 16);
1669
1670       if (begin_addr == 0 && end_addr == 0 && eh_handler == 0
1671           && eh_data == 0 && prolog_end_addr == 0)
1672         /* We are probably into the padding of the section now.  */
1673         break;
1674
1675       em_data = ((eh_handler & 0x1) << 2) | (prolog_end_addr & 0x3);
1676       eh_handler &= ~(bfd_vma) 0x3;
1677       prolog_end_addr &= ~(bfd_vma) 0x3;
1678
1679       fputc (' ', file);
1680       fprintf_vma (file, i + section->vma); fputc ('\t', file);
1681       fprintf_vma (file, begin_addr); fputc (' ', file);
1682       fprintf_vma (file, end_addr); fputc (' ', file);
1683       fprintf_vma (file, eh_handler);
1684 #if !defined(COFF_WITH_pep) || defined(COFF_WITH_pex64)
1685       fputc (' ', file);
1686       fprintf_vma (file, eh_data); fputc (' ', file);
1687       fprintf_vma (file, prolog_end_addr);
1688       fprintf (file, "   %x", em_data);
1689 #endif
1690
1691 #ifdef POWERPC_LE_PE
1692       if (eh_handler == 0 && eh_data != 0)
1693         {
1694           /* Special bits here, although the meaning may be a little
1695              mysterious. The only one I know for sure is 0x03
1696              Code Significance
1697              0x00 None
1698              0x01 Register Save Millicode
1699              0x02 Register Restore Millicode
1700              0x03 Glue Code Sequence.  */
1701           switch (eh_data)
1702             {
1703             case 0x01:
1704               fprintf (file, _(" Register save millicode"));
1705               break;
1706             case 0x02:
1707               fprintf (file, _(" Register restore millicode"));
1708               break;
1709             case 0x03:
1710               fprintf (file, _(" Glue code sequence"));
1711               break;
1712             default:
1713               break;
1714             }
1715         }
1716 #endif
1717       fprintf (file, "\n");
1718     }
1719
1720   free (data);
1721
1722   return TRUE;
1723 #undef PDATA_ROW_SIZE
1724 }
1725
1726 typedef struct sym_cache
1727 {
1728   int        symcount;
1729   asymbol ** syms;
1730 } sym_cache;
1731
1732 static asymbol **
1733 slurp_symtab (bfd *abfd, sym_cache *psc)
1734 {
1735   asymbol ** sy = NULL;
1736   long storage;
1737
1738   if (!(bfd_get_file_flags (abfd) & HAS_SYMS))
1739     {
1740       psc->symcount = 0;
1741       return NULL;
1742     }
1743
1744   storage = bfd_get_symtab_upper_bound (abfd);
1745   if (storage < 0)
1746     return NULL;
1747   if (storage)
1748     sy = bfd_malloc (storage);
1749
1750   psc->symcount = bfd_canonicalize_symtab (abfd, sy);
1751   if (psc->symcount < 0)
1752     return NULL;
1753   return sy;
1754 }
1755
1756 static const char *
1757 my_symbol_for_address (bfd *abfd, bfd_vma func, sym_cache *psc)
1758 {
1759   int i;
1760
1761   if (psc->syms == 0)
1762     psc->syms = slurp_symtab (abfd, psc);
1763
1764   for (i = 0; i < psc->symcount; i++)
1765     {
1766       if (psc->syms[i]->section->vma + psc->syms[i]->value == func)
1767         return psc->syms[i]->name;
1768     }
1769
1770   return NULL;
1771 }
1772
1773 static void
1774 cleanup_syms (sym_cache *psc)
1775 {
1776   psc->symcount = 0;
1777   free (psc->syms);
1778   psc->syms = NULL;
1779 }
1780
1781 /* This is the version for "compressed" pdata.  */
1782
1783 bfd_boolean
1784 _bfd_XX_print_ce_compressed_pdata (bfd * abfd, void * vfile)
1785 {
1786 # define PDATA_ROW_SIZE (2 * 4)
1787   FILE *file = (FILE *) vfile;
1788   bfd_byte *data = NULL;
1789   asection *section = bfd_get_section_by_name (abfd, ".pdata");
1790   bfd_size_type datasize = 0;
1791   bfd_size_type i;
1792   bfd_size_type start, stop;
1793   int onaline = PDATA_ROW_SIZE;
1794   struct sym_cache sym_cache = {0, 0} ;
1795
1796   if (section == NULL
1797       || coff_section_data (abfd, section) == NULL
1798       || pei_section_data (abfd, section) == NULL)
1799     return TRUE;
1800
1801   stop = pei_section_data (abfd, section)->virt_size;
1802   if ((stop % onaline) != 0)
1803     fprintf (file,
1804              _("Warning, .pdata section size (%ld) is not a multiple of %d\n"),
1805              (long) stop, onaline);
1806
1807   fprintf (file,
1808            _("\nThe Function Table (interpreted .pdata section contents)\n"));
1809
1810   fprintf (file, _("\
1811  vma:\t\tBegin    Prolog   Function Flags    Exception EH\n\
1812      \t\tAddress  Length   Length   32b exc  Handler   Data\n"));
1813
1814   datasize = section->size;
1815   if (datasize == 0)
1816     return TRUE;
1817
1818   if (! bfd_malloc_and_get_section (abfd, section, &data))
1819     {
1820       if (data != NULL)
1821         free (data);
1822       return FALSE;
1823     }
1824
1825   start = 0;
1826
1827   for (i = start; i < stop; i += onaline)
1828     {
1829       bfd_vma begin_addr;
1830       bfd_vma other_data;
1831       bfd_vma prolog_length, function_length;
1832       int flag32bit, exception_flag;
1833       bfd_byte *tdata = 0;
1834       asection *tsection;
1835
1836       if (i + PDATA_ROW_SIZE > stop)
1837         break;
1838
1839       begin_addr = GET_PDATA_ENTRY (abfd, data + i     );
1840       other_data = GET_PDATA_ENTRY (abfd, data + i +  4);
1841
1842       if (begin_addr == 0 && other_data == 0)
1843         /* We are probably into the padding of the section now.  */
1844         break;
1845
1846       prolog_length = (other_data & 0x000000FF);
1847       function_length = (other_data & 0x3FFFFF00) >> 8;
1848       flag32bit = (int)((other_data & 0x40000000) >> 30);
1849       exception_flag = (int)((other_data & 0x80000000) >> 31);
1850
1851       fputc (' ', file);
1852       fprintf_vma (file, i + section->vma); fputc ('\t', file);
1853       fprintf_vma (file, begin_addr); fputc (' ', file);
1854       fprintf_vma (file, prolog_length); fputc (' ', file);
1855       fprintf_vma (file, function_length); fputc (' ', file);
1856       fprintf (file, "%2d  %2d   ", flag32bit, exception_flag);
1857
1858       /* Get the exception handler's address and the data passed from the
1859          .text section. This is really the data that belongs with the .pdata
1860          but got "compressed" out for the ARM and SH4 architectures.  */
1861       tsection = bfd_get_section_by_name (abfd, ".text");
1862       if (tsection && coff_section_data (abfd, tsection)
1863           && pei_section_data (abfd, tsection))
1864         {
1865           if (bfd_malloc_and_get_section (abfd, tsection, & tdata))
1866             {
1867               int xx = (begin_addr - 8) - tsection->vma;
1868
1869               tdata = bfd_malloc (8);
1870               if (bfd_get_section_contents (abfd, tsection, tdata, (bfd_vma) xx, 8))
1871                 {
1872                   bfd_vma eh, eh_data;
1873
1874                   eh = bfd_get_32 (abfd, tdata);
1875                   eh_data = bfd_get_32 (abfd, tdata + 4);
1876                   fprintf (file, "%08x  ", (unsigned int) eh);
1877                   fprintf (file, "%08x", (unsigned int) eh_data);
1878                   if (eh != 0)
1879                     {
1880                       const char *s = my_symbol_for_address (abfd, eh, &sym_cache);
1881
1882                       if (s)
1883                         fprintf (file, " (%s) ", s);
1884                     }
1885                 }
1886               free (tdata);
1887             }
1888           else
1889             {
1890               if (tdata)
1891                 free (tdata);
1892             }
1893         }
1894
1895       fprintf (file, "\n");
1896     }
1897
1898   free (data);
1899
1900   cleanup_syms (& sym_cache);
1901
1902   return TRUE;
1903 #undef PDATA_ROW_SIZE
1904 }
1905
1906 #ifdef COFF_WITH_pex64
1907 /* The PE+ x64 variant.  */
1908 bfd_boolean
1909 _bfd_pex64_print_pdata (bfd *abfd, void *vfile)
1910 {
1911 # define PDATA_ROW_SIZE (3 * 4)
1912   FILE *file = (FILE *) vfile;
1913   bfd_byte *data = NULL;
1914   asection *section = bfd_get_section_by_name (abfd, ".pdata");
1915   bfd_size_type datasize = 0;
1916   bfd_size_type i;
1917   bfd_size_type start, stop;
1918   int onaline = PDATA_ROW_SIZE;
1919   struct sym_cache sym_cache = {0, 0};
1920
1921   if (section == NULL
1922       || coff_section_data (abfd, section) == NULL
1923       || pei_section_data (abfd, section) == NULL)
1924     return TRUE;
1925
1926   stop = pei_section_data (abfd, section)->virt_size;
1927   if ((stop % onaline) != 0)
1928     fprintf (file,
1929              _("warning: .pdata section size (%ld) is not a multiple of %d\n"),
1930              (long) stop, onaline);
1931
1932   fprintf (file,
1933            _("\nThe Function Table (interpreted .pdata section contents)\n"));
1934
1935   fprintf (file, _("vma:\t\t\tBeginAddress\t EndAddress\t  UnwindData\n"));
1936
1937   datasize = section->size;
1938   if (datasize == 0)
1939     return TRUE;
1940
1941   if (!bfd_malloc_and_get_section (abfd, section, &data))
1942     {
1943       if (data != NULL)
1944         free (data);
1945       return FALSE;
1946     }
1947
1948   start = 0;
1949
1950   for (i = start; i < stop; i += onaline)
1951     {
1952       bfd_vma begin_addr;
1953       bfd_vma end_addr;
1954       bfd_vma unwind_data_addr;
1955
1956       if (i + PDATA_ROW_SIZE > stop)
1957         break;
1958
1959       begin_addr = bfd_get_32 (abfd, data + i);
1960       end_addr = bfd_get_32 (abfd, data + i + 4);
1961       unwind_data_addr = bfd_get_32 (abfd, data + i +  8);
1962
1963       if (begin_addr == 0 && end_addr == 0 && unwind_data_addr == 0)
1964         /* We are probably into the padding of the section now.  */
1965         break;
1966
1967       fputc (' ', file);
1968       fprintf_vma (file, i + section->vma);
1969       fprintf (file, ":\t");
1970       fprintf_vma (file, begin_addr);
1971       fputc (' ', file);
1972       fprintf_vma (file, end_addr);
1973       fputc (' ', file);
1974       fprintf_vma (file, unwind_data_addr);
1975
1976       fprintf (file, "\n");
1977     }
1978
1979   free (data);
1980
1981   cleanup_syms (&sym_cache);
1982
1983   return TRUE;
1984 #undef PDATA_ROW_SIZE
1985 }
1986 #endif
1987 \f
1988 #define IMAGE_REL_BASED_HIGHADJ 4
1989 static const char * const tbl[] =
1990 {
1991   "ABSOLUTE",
1992   "HIGH",
1993   "LOW",
1994   "HIGHLOW",
1995   "HIGHADJ",
1996   "MIPS_JMPADDR",
1997   "SECTION",
1998   "REL32",
1999   "RESERVED1",
2000   "MIPS_JMPADDR16",
2001   "DIR64",
2002   "HIGH3ADJ",
2003   "UNKNOWN",   /* MUST be last.  */
2004 };
2005
2006 static bfd_boolean
2007 pe_print_reloc (bfd * abfd, void * vfile)
2008 {
2009   FILE *file = (FILE *) vfile;
2010   bfd_byte *data = 0;
2011   asection *section = bfd_get_section_by_name (abfd, ".reloc");
2012   bfd_size_type datasize;
2013   bfd_size_type i;
2014   bfd_size_type start, stop;
2015
2016   if (section == NULL)
2017     return TRUE;
2018
2019   if (section->size == 0)
2020     return TRUE;
2021
2022   fprintf (file,
2023            _("\n\nPE File Base Relocations (interpreted .reloc section contents)\n"));
2024
2025   datasize = section->size;
2026   if (! bfd_malloc_and_get_section (abfd, section, &data))
2027     {
2028       if (data != NULL)
2029         free (data);
2030       return FALSE;
2031     }
2032
2033   start = 0;
2034
2035   stop = section->size;
2036
2037   for (i = start; i < stop;)
2038     {
2039       int j;
2040       bfd_vma virtual_address;
2041       long number, size;
2042
2043       /* The .reloc section is a sequence of blocks, with a header consisting
2044          of two 32 bit quantities, followed by a number of 16 bit entries.  */
2045       virtual_address = bfd_get_32 (abfd, data+i);
2046       size = bfd_get_32 (abfd, data+i+4);
2047       number = (size - 8) / 2;
2048
2049       if (size == 0)
2050         break;
2051
2052       fprintf (file,
2053                _("\nVirtual Address: %08lx Chunk size %ld (0x%lx) Number of fixups %ld\n"),
2054                (unsigned long) virtual_address, size, (unsigned long) size, number);
2055
2056       for (j = 0; j < number; ++j)
2057         {
2058           unsigned short e = bfd_get_16 (abfd, data + i + 8 + j * 2);
2059           unsigned int t = (e & 0xF000) >> 12;
2060           int off = e & 0x0FFF;
2061
2062           if (t >= sizeof (tbl) / sizeof (tbl[0]))
2063             t = (sizeof (tbl) / sizeof (tbl[0])) - 1;
2064
2065           fprintf (file,
2066                    _("\treloc %4d offset %4x [%4lx] %s"),
2067                    j, off, (unsigned long) (off + virtual_address), tbl[t]);
2068
2069           /* HIGHADJ takes an argument, - the next record *is* the
2070              low 16 bits of addend.  */
2071           if (t == IMAGE_REL_BASED_HIGHADJ)
2072             {
2073               fprintf (file, " (%4x)",
2074                        ((unsigned int)
2075                         bfd_get_16 (abfd, data + i + 8 + j * 2 + 2)));
2076               j++;
2077             }
2078
2079           fprintf (file, "\n");
2080         }
2081
2082       i += size;
2083     }
2084
2085   free (data);
2086
2087   return TRUE;
2088 }
2089
2090 /* Print out the program headers.  */
2091
2092 bfd_boolean
2093 _bfd_XX_print_private_bfd_data_common (bfd * abfd, void * vfile)
2094 {
2095   FILE *file = (FILE *) vfile;
2096   int j;
2097   pe_data_type *pe = pe_data (abfd);
2098   struct internal_extra_pe_aouthdr *i = &pe->pe_opthdr;
2099   const char *subsystem_name = NULL;
2100   const char *name;
2101
2102   /* The MS dumpbin program reportedly ands with 0xff0f before
2103      printing the characteristics field.  Not sure why.  No reason to
2104      emulate it here.  */
2105   fprintf (file, _("\nCharacteristics 0x%x\n"), pe->real_flags);
2106 #undef PF
2107 #define PF(x, y) if (pe->real_flags & x) { fprintf (file, "\t%s\n", y); }
2108   PF (IMAGE_FILE_RELOCS_STRIPPED, "relocations stripped");
2109   PF (IMAGE_FILE_EXECUTABLE_IMAGE, "executable");
2110   PF (IMAGE_FILE_LINE_NUMS_STRIPPED, "line numbers stripped");
2111   PF (IMAGE_FILE_LOCAL_SYMS_STRIPPED, "symbols stripped");
2112   PF (IMAGE_FILE_LARGE_ADDRESS_AWARE, "large address aware");
2113   PF (IMAGE_FILE_BYTES_REVERSED_LO, "little endian");
2114   PF (IMAGE_FILE_32BIT_MACHINE, "32 bit words");
2115   PF (IMAGE_FILE_DEBUG_STRIPPED, "debugging information removed");
2116   PF (IMAGE_FILE_SYSTEM, "system file");
2117   PF (IMAGE_FILE_DLL, "DLL");
2118   PF (IMAGE_FILE_BYTES_REVERSED_HI, "big endian");
2119 #undef PF
2120
2121   /* ctime implies '\n'.  */
2122   {
2123     time_t t = pe->coff.timestamp;
2124     fprintf (file, "\nTime/Date\t\t%s", ctime (&t));
2125   }
2126
2127 #ifndef IMAGE_NT_OPTIONAL_HDR_MAGIC
2128 # define IMAGE_NT_OPTIONAL_HDR_MAGIC 0x10b
2129 #endif
2130 #ifndef IMAGE_NT_OPTIONAL_HDR64_MAGIC
2131 # define IMAGE_NT_OPTIONAL_HDR64_MAGIC 0x20b
2132 #endif
2133 #ifndef IMAGE_NT_OPTIONAL_HDRROM_MAGIC
2134 # define IMAGE_NT_OPTIONAL_HDRROM_MAGIC 0x107
2135 #endif
2136
2137   switch (i->Magic)
2138     {
2139     case IMAGE_NT_OPTIONAL_HDR_MAGIC:
2140       name = "PE32";
2141       break;
2142     case IMAGE_NT_OPTIONAL_HDR64_MAGIC:
2143       name = "PE32+";
2144       break;
2145     case IMAGE_NT_OPTIONAL_HDRROM_MAGIC:
2146       name = "ROM";
2147       break;
2148     default:
2149       name = NULL;
2150       break;
2151     }
2152   fprintf (file, "Magic\t\t\t%04x", i->Magic);
2153   if (name)
2154     fprintf (file, "\t(%s)",name);
2155   fprintf (file, "\nMajorLinkerVersion\t%d\n", i->MajorLinkerVersion);
2156   fprintf (file, "MinorLinkerVersion\t%d\n", i->MinorLinkerVersion);
2157   fprintf (file, "SizeOfCode\t\t%08lx\n", (unsigned long) i->SizeOfCode);
2158   fprintf (file, "SizeOfInitializedData\t%08lx\n",
2159            (unsigned long) i->SizeOfInitializedData);
2160   fprintf (file, "SizeOfUninitializedData\t%08lx\n",
2161            (unsigned long) i->SizeOfUninitializedData);
2162   fprintf (file, "AddressOfEntryPoint\t");
2163   fprintf_vma (file, i->AddressOfEntryPoint);
2164   fprintf (file, "\nBaseOfCode\t\t");
2165   fprintf_vma (file, i->BaseOfCode);
2166 #if !defined(COFF_WITH_pep) && !defined(COFF_WITH_pex64)
2167   /* PE32+ does not have BaseOfData member!  */
2168   fprintf (file, "\nBaseOfData\t\t");
2169   fprintf_vma (file, i->BaseOfData);
2170 #endif
2171
2172   fprintf (file, "\nImageBase\t\t");
2173   fprintf_vma (file, i->ImageBase);
2174   fprintf (file, "\nSectionAlignment\t");
2175   fprintf_vma (file, i->SectionAlignment);
2176   fprintf (file, "\nFileAlignment\t\t");
2177   fprintf_vma (file, i->FileAlignment);
2178   fprintf (file, "\nMajorOSystemVersion\t%d\n", i->MajorOperatingSystemVersion);
2179   fprintf (file, "MinorOSystemVersion\t%d\n", i->MinorOperatingSystemVersion);
2180   fprintf (file, "MajorImageVersion\t%d\n", i->MajorImageVersion);
2181   fprintf (file, "MinorImageVersion\t%d\n", i->MinorImageVersion);
2182   fprintf (file, "MajorSubsystemVersion\t%d\n", i->MajorSubsystemVersion);
2183   fprintf (file, "MinorSubsystemVersion\t%d\n", i->MinorSubsystemVersion);
2184   fprintf (file, "Win32Version\t\t%08lx\n", (unsigned long) i->Reserved1);
2185   fprintf (file, "SizeOfImage\t\t%08lx\n", (unsigned long) i->SizeOfImage);
2186   fprintf (file, "SizeOfHeaders\t\t%08lx\n", (unsigned long) i->SizeOfHeaders);
2187   fprintf (file, "CheckSum\t\t%08lx\n", (unsigned long) i->CheckSum);
2188
2189   switch (i->Subsystem)
2190     {
2191     case IMAGE_SUBSYSTEM_UNKNOWN:
2192       subsystem_name = "unspecified";
2193       break;
2194     case IMAGE_SUBSYSTEM_NATIVE:
2195       subsystem_name = "NT native";
2196       break;
2197     case IMAGE_SUBSYSTEM_WINDOWS_GUI:
2198       subsystem_name = "Windows GUI";
2199       break;
2200     case IMAGE_SUBSYSTEM_WINDOWS_CUI:
2201       subsystem_name = "Windows CUI";
2202       break;
2203     case IMAGE_SUBSYSTEM_POSIX_CUI:
2204       subsystem_name = "POSIX CUI";
2205       break;
2206     case IMAGE_SUBSYSTEM_WINDOWS_CE_GUI:
2207       subsystem_name = "Wince CUI";
2208       break;
2209     // These are from UEFI Platform Initialization Specification 1.1.
2210     case IMAGE_SUBSYSTEM_EFI_APPLICATION:
2211       subsystem_name = "EFI application";
2212       break;
2213     case IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER:
2214       subsystem_name = "EFI boot service driver";
2215       break;
2216     case IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER:
2217       subsystem_name = "EFI runtime driver";
2218       break;
2219     case IMAGE_SUBSYSTEM_SAL_RUNTIME_DRIVER:
2220       subsystem_name = "SAL runtime driver";
2221       break;
2222     // This is from revision 8.0 of the MS PE/COFF spec
2223     case IMAGE_SUBSYSTEM_XBOX:
2224       subsystem_name = "XBOX";
2225       break;
2226     // Added default case for clarity - subsystem_name is NULL anyway.
2227     default:
2228       subsystem_name = NULL;
2229     }
2230
2231   fprintf (file, "Subsystem\t\t%08x", i->Subsystem);
2232   if (subsystem_name)
2233     fprintf (file, "\t(%s)", subsystem_name);
2234   fprintf (file, "\nDllCharacteristics\t%08x\n", i->DllCharacteristics);
2235   fprintf (file, "SizeOfStackReserve\t");
2236   fprintf_vma (file, i->SizeOfStackReserve);
2237   fprintf (file, "\nSizeOfStackCommit\t");
2238   fprintf_vma (file, i->SizeOfStackCommit);
2239   fprintf (file, "\nSizeOfHeapReserve\t");
2240   fprintf_vma (file, i->SizeOfHeapReserve);
2241   fprintf (file, "\nSizeOfHeapCommit\t");
2242   fprintf_vma (file, i->SizeOfHeapCommit);
2243   fprintf (file, "\nLoaderFlags\t\t%08lx\n", (unsigned long) i->LoaderFlags);
2244   fprintf (file, "NumberOfRvaAndSizes\t%08lx\n",
2245            (unsigned long) i->NumberOfRvaAndSizes);
2246
2247   fprintf (file, "\nThe Data Directory\n");
2248   for (j = 0; j < IMAGE_NUMBEROF_DIRECTORY_ENTRIES; j++)
2249     {
2250       fprintf (file, "Entry %1x ", j);
2251       fprintf_vma (file, i->DataDirectory[j].VirtualAddress);
2252       fprintf (file, " %08lx ", (unsigned long) i->DataDirectory[j].Size);
2253       fprintf (file, "%s\n", dir_names[j]);
2254     }
2255
2256   pe_print_idata (abfd, vfile);
2257   pe_print_edata (abfd, vfile);
2258   if (bfd_coff_have_print_pdata (abfd))
2259     bfd_coff_print_pdata (abfd, vfile);
2260   else
2261     pe_print_pdata (abfd, vfile);
2262   pe_print_reloc (abfd, vfile);
2263
2264   return TRUE;
2265 }
2266
2267 /* Copy any private info we understand from the input bfd
2268    to the output bfd.  */
2269
2270 bfd_boolean
2271 _bfd_XX_bfd_copy_private_bfd_data_common (bfd * ibfd, bfd * obfd)
2272 {
2273   pe_data_type *ipe, *ope;
2274
2275   /* One day we may try to grok other private data.  */
2276   if (ibfd->xvec->flavour != bfd_target_coff_flavour
2277       || obfd->xvec->flavour != bfd_target_coff_flavour)
2278     return TRUE;
2279
2280   ipe = pe_data (ibfd);
2281   ope = pe_data (obfd);
2282  
2283   ope->pe_opthdr = ipe->pe_opthdr;
2284   ope->dll = ipe->dll;
2285
2286   /* Don't copy input subsystem if output is different from input.  */
2287   if (obfd->xvec != ibfd->xvec)
2288     ope->pe_opthdr.Subsystem = IMAGE_SUBSYSTEM_UNKNOWN;
2289
2290   /* For strip: if we removed .reloc, we'll make a real mess of things
2291      if we don't remove this entry as well.  */
2292   if (! pe_data (obfd)->has_reloc_section)
2293     {
2294       pe_data (obfd)->pe_opthdr.DataDirectory[PE_BASE_RELOCATION_TABLE].VirtualAddress = 0;
2295       pe_data (obfd)->pe_opthdr.DataDirectory[PE_BASE_RELOCATION_TABLE].Size = 0;
2296     }
2297   return TRUE;
2298 }
2299
2300 /* Copy private section data.  */
2301
2302 bfd_boolean
2303 _bfd_XX_bfd_copy_private_section_data (bfd *ibfd,
2304                                        asection *isec,
2305                                        bfd *obfd,
2306                                        asection *osec)
2307 {
2308   if (bfd_get_flavour (ibfd) != bfd_target_coff_flavour
2309       || bfd_get_flavour (obfd) != bfd_target_coff_flavour)
2310     return TRUE;
2311
2312   if (coff_section_data (ibfd, isec) != NULL
2313       && pei_section_data (ibfd, isec) != NULL)
2314     {
2315       if (coff_section_data (obfd, osec) == NULL)
2316         {
2317           bfd_size_type amt = sizeof (struct coff_section_tdata);
2318           osec->used_by_bfd = bfd_zalloc (obfd, amt);
2319           if (osec->used_by_bfd == NULL)
2320             return FALSE;
2321         }
2322
2323       if (pei_section_data (obfd, osec) == NULL)
2324         {
2325           bfd_size_type amt = sizeof (struct pei_section_tdata);
2326           coff_section_data (obfd, osec)->tdata = bfd_zalloc (obfd, amt);
2327           if (coff_section_data (obfd, osec)->tdata == NULL)
2328             return FALSE;
2329         }
2330
2331       pei_section_data (obfd, osec)->virt_size =
2332         pei_section_data (ibfd, isec)->virt_size;
2333       pei_section_data (obfd, osec)->pe_flags =
2334         pei_section_data (ibfd, isec)->pe_flags;
2335     }
2336
2337   return TRUE;
2338 }
2339
2340 void
2341 _bfd_XX_get_symbol_info (bfd * abfd, asymbol *symbol, symbol_info *ret)
2342 {
2343   coff_get_symbol_info (abfd, symbol, ret);
2344 }
2345
2346 /* Handle the .idata section and other things that need symbol table
2347    access.  */
2348
2349 bfd_boolean
2350 _bfd_XXi_final_link_postscript (bfd * abfd, struct coff_final_link_info *pfinfo)
2351 {
2352   struct coff_link_hash_entry *h1;
2353   struct bfd_link_info *info = pfinfo->info;
2354   bfd_boolean result = TRUE;
2355
2356   /* There are a few fields that need to be filled in now while we
2357      have symbol table access.
2358
2359      The .idata subsections aren't directly available as sections, but
2360      they are in the symbol table, so get them from there.  */
2361
2362   /* The import directory.  This is the address of .idata$2, with size
2363      of .idata$2 + .idata$3.  */
2364   h1 = coff_link_hash_lookup (coff_hash_table (info),
2365                               ".idata$2", FALSE, FALSE, TRUE);
2366   if (h1 != NULL)
2367     {
2368       /* PR ld/2729: We cannot rely upon all the output sections having been 
2369          created properly, so check before referencing them.  Issue a warning
2370          message for any sections tht could not be found.  */
2371       if ((h1->root.type == bfd_link_hash_defined
2372            || h1->root.type == bfd_link_hash_defweak)
2373           && h1->root.u.def.section != NULL
2374           && h1->root.u.def.section->output_section != NULL)
2375         pe_data (abfd)->pe_opthdr.DataDirectory[PE_IMPORT_TABLE].VirtualAddress =
2376           (h1->root.u.def.value
2377            + h1->root.u.def.section->output_section->vma
2378            + h1->root.u.def.section->output_offset);
2379       else
2380         {
2381           _bfd_error_handler
2382             (_("%B: unable to fill in DataDictionary[1] because .idata$2 is missing"), 
2383              abfd);
2384           result = FALSE;
2385         }
2386
2387       h1 = coff_link_hash_lookup (coff_hash_table (info),
2388                                   ".idata$4", FALSE, FALSE, TRUE);
2389       if (h1 != NULL
2390           && (h1->root.type == bfd_link_hash_defined
2391            || h1->root.type == bfd_link_hash_defweak)
2392           && h1->root.u.def.section != NULL
2393           && h1->root.u.def.section->output_section != NULL)
2394         pe_data (abfd)->pe_opthdr.DataDirectory[PE_IMPORT_TABLE].Size =
2395           ((h1->root.u.def.value
2396             + h1->root.u.def.section->output_section->vma
2397             + h1->root.u.def.section->output_offset)
2398            - pe_data (abfd)->pe_opthdr.DataDirectory[PE_IMPORT_TABLE].VirtualAddress);
2399       else
2400         {
2401           _bfd_error_handler
2402             (_("%B: unable to fill in DataDictionary[1] because .idata$4 is missing"), 
2403              abfd);
2404           result = FALSE;
2405         }
2406
2407       /* The import address table.  This is the size/address of
2408          .idata$5.  */
2409       h1 = coff_link_hash_lookup (coff_hash_table (info),
2410                                   ".idata$5", FALSE, FALSE, TRUE);
2411       if (h1 != NULL
2412           && (h1->root.type == bfd_link_hash_defined
2413            || h1->root.type == bfd_link_hash_defweak)
2414           && h1->root.u.def.section != NULL
2415           && h1->root.u.def.section->output_section != NULL)
2416         pe_data (abfd)->pe_opthdr.DataDirectory[PE_IMPORT_ADDRESS_TABLE].VirtualAddress =
2417           (h1->root.u.def.value
2418            + h1->root.u.def.section->output_section->vma
2419            + h1->root.u.def.section->output_offset);
2420       else
2421         {
2422           _bfd_error_handler
2423             (_("%B: unable to fill in DataDictionary[12] because .idata$5 is missing"), 
2424              abfd);
2425           result = FALSE;
2426         }
2427
2428       h1 = coff_link_hash_lookup (coff_hash_table (info),
2429                                   ".idata$6", FALSE, FALSE, TRUE);
2430       if (h1 != NULL
2431           && (h1->root.type == bfd_link_hash_defined
2432            || h1->root.type == bfd_link_hash_defweak)
2433           && h1->root.u.def.section != NULL
2434           && h1->root.u.def.section->output_section != NULL)
2435         pe_data (abfd)->pe_opthdr.DataDirectory[PE_IMPORT_ADDRESS_TABLE].Size =
2436           ((h1->root.u.def.value
2437             + h1->root.u.def.section->output_section->vma
2438             + h1->root.u.def.section->output_offset)
2439            - pe_data (abfd)->pe_opthdr.DataDirectory[PE_IMPORT_ADDRESS_TABLE].VirtualAddress);      
2440       else
2441         {
2442           _bfd_error_handler
2443             (_("%B: unable to fill in DataDictionary[PE_IMPORT_ADDRESS_TABLE (12)] because .idata$6 is missing"), 
2444              abfd);
2445           result = FALSE;
2446         }
2447     }
2448
2449   h1 = coff_link_hash_lookup (coff_hash_table (info),
2450                               "__tls_used", FALSE, FALSE, TRUE);
2451   if (h1 != NULL)
2452     {
2453       if ((h1->root.type == bfd_link_hash_defined
2454            || h1->root.type == bfd_link_hash_defweak)
2455           && h1->root.u.def.section != NULL
2456           && h1->root.u.def.section->output_section != NULL)
2457         pe_data (abfd)->pe_opthdr.DataDirectory[PE_TLS_TABLE].VirtualAddress =
2458           (h1->root.u.def.value
2459            + h1->root.u.def.section->output_section->vma
2460            + h1->root.u.def.section->output_offset
2461            - pe_data (abfd)->pe_opthdr.ImageBase);
2462       else
2463         {
2464           _bfd_error_handler
2465             (_("%B: unable to fill in DataDictionary[9] because __tls_used is missing"), 
2466              abfd);
2467           result = FALSE;
2468         }
2469
2470       pe_data (abfd)->pe_opthdr.DataDirectory[PE_TLS_TABLE].Size = 0x18;
2471     }
2472
2473   /* If we couldn't find idata$2, we either have an excessively
2474      trivial program or are in DEEP trouble; we have to assume trivial
2475      program....  */
2476   return result;
2477 }