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