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