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