OSDN Git Service

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