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