OSDN Git Service

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