OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [tags/] [gnu-src/] [gdb-7.2/] [gdb-7.2-or32-1.0rc3/] [bfd/] [peXXigen.c] - Blame information for rev 581

Go to most recent revision | Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.