bfd/
[deliverable/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,
5e5e11bd 3 2005, 2006 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
ILT
59#include "bfd.h"
60#include "sysdep.h"
61#include "libbfd.h"
62#include "coff/internal.h"
63
64/* NOTE: it's strange to be including an architecture specific header
65 in what's supposed to be general (to PE/PEI) code. However, that's
66 where the definitions are, and they don't vary per architecture
67 within PE/PEI, so we get them from there. FIXME: The lack of
68 variance is an assumption which may prove to be incorrect if new
69 PE/PEI targets are created. */
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
648 fix, strip munges the file. */
98a96df7
CF
649 if (coff_section_data (abfd, sec) != NULL
650 && pei_section_data (abfd, sec) != NULL)
651 isize += SA (FA (pei_section_data (abfd, sec)->virt_size));
277d1b5e
ILT
652 }
653
654 aouthdr_in->dsize = dsize;
655 aouthdr_in->tsize = tsize;
d48bdb99 656 extra->SizeOfHeaders = hsize;
7920ce38 657 extra->SizeOfImage = SA (hsize) + isize;
277d1b5e
ILT
658 }
659
dc810e39 660 H_PUT_16 (abfd, aouthdr_in->magic, aouthdr_out->standard.magic);
277d1b5e 661
5933bdc9
ILT
662#define LINKER_VERSION 256 /* That is, 2.56 */
663
664 /* This piece of magic sets the "linker version" field to
665 LINKER_VERSION. */
dc810e39
AM
666 H_PUT_16 (abfd, (LINKER_VERSION / 100 + (LINKER_VERSION % 100) * 256),
667 aouthdr_out->standard.vstamp);
668
669 PUT_AOUTHDR_TSIZE (abfd, aouthdr_in->tsize, aouthdr_out->standard.tsize);
670 PUT_AOUTHDR_DSIZE (abfd, aouthdr_in->dsize, aouthdr_out->standard.dsize);
671 PUT_AOUTHDR_BSIZE (abfd, aouthdr_in->bsize, aouthdr_out->standard.bsize);
672 PUT_AOUTHDR_ENTRY (abfd, aouthdr_in->entry, aouthdr_out->standard.entry);
277d1b5e 673 PUT_AOUTHDR_TEXT_START (abfd, aouthdr_in->text_start,
dc810e39 674 aouthdr_out->standard.text_start);
277d1b5e 675
99ad8390 676#if !defined(COFF_WITH_pep) && !defined(COFF_WITH_pex64)
c25cfdf8 677 /* PE32+ does not have data_start member! */
277d1b5e 678 PUT_AOUTHDR_DATA_START (abfd, aouthdr_in->data_start,
dc810e39 679 aouthdr_out->standard.data_start);
fac41780 680#endif
277d1b5e 681
dc810e39
AM
682 PUT_OPTHDR_IMAGE_BASE (abfd, extra->ImageBase, aouthdr_out->ImageBase);
683 H_PUT_32 (abfd, extra->SectionAlignment, aouthdr_out->SectionAlignment);
684 H_PUT_32 (abfd, extra->FileAlignment, aouthdr_out->FileAlignment);
685 H_PUT_16 (abfd, extra->MajorOperatingSystemVersion,
686 aouthdr_out->MajorOperatingSystemVersion);
687 H_PUT_16 (abfd, extra->MinorOperatingSystemVersion,
688 aouthdr_out->MinorOperatingSystemVersion);
689 H_PUT_16 (abfd, extra->MajorImageVersion, aouthdr_out->MajorImageVersion);
690 H_PUT_16 (abfd, extra->MinorImageVersion, aouthdr_out->MinorImageVersion);
691 H_PUT_16 (abfd, extra->MajorSubsystemVersion,
692 aouthdr_out->MajorSubsystemVersion);
693 H_PUT_16 (abfd, extra->MinorSubsystemVersion,
694 aouthdr_out->MinorSubsystemVersion);
695 H_PUT_32 (abfd, extra->Reserved1, aouthdr_out->Reserved1);
696 H_PUT_32 (abfd, extra->SizeOfImage, aouthdr_out->SizeOfImage);
697 H_PUT_32 (abfd, extra->SizeOfHeaders, aouthdr_out->SizeOfHeaders);
698 H_PUT_32 (abfd, extra->CheckSum, aouthdr_out->CheckSum);
699 H_PUT_16 (abfd, extra->Subsystem, aouthdr_out->Subsystem);
700 H_PUT_16 (abfd, extra->DllCharacteristics, aouthdr_out->DllCharacteristics);
fac41780 701 PUT_OPTHDR_SIZE_OF_STACK_RESERVE (abfd, extra->SizeOfStackReserve,
dc810e39 702 aouthdr_out->SizeOfStackReserve);
fac41780 703 PUT_OPTHDR_SIZE_OF_STACK_COMMIT (abfd, extra->SizeOfStackCommit,
dc810e39 704 aouthdr_out->SizeOfStackCommit);
fac41780 705 PUT_OPTHDR_SIZE_OF_HEAP_RESERVE (abfd, extra->SizeOfHeapReserve,
dc810e39 706 aouthdr_out->SizeOfHeapReserve);
fac41780 707 PUT_OPTHDR_SIZE_OF_HEAP_COMMIT (abfd, extra->SizeOfHeapCommit,
dc810e39
AM
708 aouthdr_out->SizeOfHeapCommit);
709 H_PUT_32 (abfd, extra->LoaderFlags, aouthdr_out->LoaderFlags);
710 H_PUT_32 (abfd, extra->NumberOfRvaAndSizes,
711 aouthdr_out->NumberOfRvaAndSizes);
277d1b5e
ILT
712 {
713 int idx;
1725a96e 714
6fa957a9 715 for (idx = 0; idx < 16; idx++)
277d1b5e 716 {
dc810e39
AM
717 H_PUT_32 (abfd, extra->DataDirectory[idx].VirtualAddress,
718 aouthdr_out->DataDirectory[idx][0]);
719 H_PUT_32 (abfd, extra->DataDirectory[idx].Size,
720 aouthdr_out->DataDirectory[idx][1]);
277d1b5e
ILT
721 }
722 }
723
724 return AOUTSZ;
725}
726
727unsigned int
7920ce38 728_bfd_XXi_only_swap_filehdr_out (bfd * abfd, void * in, void * out)
277d1b5e
ILT
729{
730 int idx;
6fa957a9
KH
731 struct internal_filehdr *filehdr_in = (struct internal_filehdr *) in;
732 struct external_PEI_filehdr *filehdr_out = (struct external_PEI_filehdr *) out;
277d1b5e
ILT
733
734 if (pe_data (abfd)->has_reloc_section)
735 filehdr_in->f_flags &= ~F_RELFLG;
736
737 if (pe_data (abfd)->dll)
738 filehdr_in->f_flags |= F_DLL;
739
740 filehdr_in->pe.e_magic = DOSMAGIC;
741 filehdr_in->pe.e_cblp = 0x90;
742 filehdr_in->pe.e_cp = 0x3;
743 filehdr_in->pe.e_crlc = 0x0;
744 filehdr_in->pe.e_cparhdr = 0x4;
745 filehdr_in->pe.e_minalloc = 0x0;
746 filehdr_in->pe.e_maxalloc = 0xffff;
747 filehdr_in->pe.e_ss = 0x0;
748 filehdr_in->pe.e_sp = 0xb8;
749 filehdr_in->pe.e_csum = 0x0;
750 filehdr_in->pe.e_ip = 0x0;
751 filehdr_in->pe.e_cs = 0x0;
752 filehdr_in->pe.e_lfarlc = 0x40;
753 filehdr_in->pe.e_ovno = 0x0;
754
6fa957a9 755 for (idx = 0; idx < 4; idx++)
277d1b5e
ILT
756 filehdr_in->pe.e_res[idx] = 0x0;
757
758 filehdr_in->pe.e_oemid = 0x0;
759 filehdr_in->pe.e_oeminfo = 0x0;
760
6fa957a9 761 for (idx = 0; idx < 10; idx++)
277d1b5e
ILT
762 filehdr_in->pe.e_res2[idx] = 0x0;
763
764 filehdr_in->pe.e_lfanew = 0x80;
765
6fa957a9
KH
766 /* This next collection of data are mostly just characters. It
767 appears to be constant within the headers put on NT exes. */
277d1b5e
ILT
768 filehdr_in->pe.dos_message[0] = 0x0eba1f0e;
769 filehdr_in->pe.dos_message[1] = 0xcd09b400;
770 filehdr_in->pe.dos_message[2] = 0x4c01b821;
771 filehdr_in->pe.dos_message[3] = 0x685421cd;
772 filehdr_in->pe.dos_message[4] = 0x70207369;
773 filehdr_in->pe.dos_message[5] = 0x72676f72;
774 filehdr_in->pe.dos_message[6] = 0x63206d61;
775 filehdr_in->pe.dos_message[7] = 0x6f6e6e61;
776 filehdr_in->pe.dos_message[8] = 0x65622074;
777 filehdr_in->pe.dos_message[9] = 0x6e757220;
778 filehdr_in->pe.dos_message[10] = 0x206e6920;
779 filehdr_in->pe.dos_message[11] = 0x20534f44;
780 filehdr_in->pe.dos_message[12] = 0x65646f6d;
781 filehdr_in->pe.dos_message[13] = 0x0a0d0d2e;
782 filehdr_in->pe.dos_message[14] = 0x24;
783 filehdr_in->pe.dos_message[15] = 0x0;
784 filehdr_in->pe.nt_signature = NT_SIGNATURE;
785
dc810e39
AM
786 H_PUT_16 (abfd, filehdr_in->f_magic, filehdr_out->f_magic);
787 H_PUT_16 (abfd, filehdr_in->f_nscns, filehdr_out->f_nscns);
277d1b5e 788
dc810e39
AM
789 H_PUT_32 (abfd, time (0), filehdr_out->f_timdat);
790 PUT_FILEHDR_SYMPTR (abfd, filehdr_in->f_symptr,
791 filehdr_out->f_symptr);
792 H_PUT_32 (abfd, filehdr_in->f_nsyms, filehdr_out->f_nsyms);
793 H_PUT_16 (abfd, filehdr_in->f_opthdr, filehdr_out->f_opthdr);
794 H_PUT_16 (abfd, filehdr_in->f_flags, filehdr_out->f_flags);
277d1b5e 795
1725a96e 796 /* Put in extra dos header stuff. This data remains essentially
277d1b5e 797 constant, it just has to be tacked on to the beginning of all exes
1725a96e 798 for NT. */
dc810e39
AM
799 H_PUT_16 (abfd, filehdr_in->pe.e_magic, filehdr_out->e_magic);
800 H_PUT_16 (abfd, filehdr_in->pe.e_cblp, filehdr_out->e_cblp);
801 H_PUT_16 (abfd, filehdr_in->pe.e_cp, filehdr_out->e_cp);
802 H_PUT_16 (abfd, filehdr_in->pe.e_crlc, filehdr_out->e_crlc);
803 H_PUT_16 (abfd, filehdr_in->pe.e_cparhdr, filehdr_out->e_cparhdr);
804 H_PUT_16 (abfd, filehdr_in->pe.e_minalloc, filehdr_out->e_minalloc);
805 H_PUT_16 (abfd, filehdr_in->pe.e_maxalloc, filehdr_out->e_maxalloc);
806 H_PUT_16 (abfd, filehdr_in->pe.e_ss, filehdr_out->e_ss);
807 H_PUT_16 (abfd, filehdr_in->pe.e_sp, filehdr_out->e_sp);
808 H_PUT_16 (abfd, filehdr_in->pe.e_csum, filehdr_out->e_csum);
809 H_PUT_16 (abfd, filehdr_in->pe.e_ip, filehdr_out->e_ip);
810 H_PUT_16 (abfd, filehdr_in->pe.e_cs, filehdr_out->e_cs);
811 H_PUT_16 (abfd, filehdr_in->pe.e_lfarlc, filehdr_out->e_lfarlc);
812 H_PUT_16 (abfd, filehdr_in->pe.e_ovno, filehdr_out->e_ovno);
1725a96e
NC
813
814 for (idx = 0; idx < 4; idx++)
dc810e39 815 H_PUT_16 (abfd, filehdr_in->pe.e_res[idx], filehdr_out->e_res[idx]);
1725a96e 816
dc810e39
AM
817 H_PUT_16 (abfd, filehdr_in->pe.e_oemid, filehdr_out->e_oemid);
818 H_PUT_16 (abfd, filehdr_in->pe.e_oeminfo, filehdr_out->e_oeminfo);
1725a96e
NC
819
820 for (idx = 0; idx < 10; idx++)
dc810e39 821 H_PUT_16 (abfd, filehdr_in->pe.e_res2[idx], filehdr_out->e_res2[idx]);
1725a96e 822
dc810e39 823 H_PUT_32 (abfd, filehdr_in->pe.e_lfanew, filehdr_out->e_lfanew);
277d1b5e 824
1725a96e 825 for (idx = 0; idx < 16; idx++)
dc810e39
AM
826 H_PUT_32 (abfd, filehdr_in->pe.dos_message[idx],
827 filehdr_out->dos_message[idx]);
277d1b5e 828
6fa957a9 829 /* Also put in the NT signature. */
dc810e39 830 H_PUT_32 (abfd, filehdr_in->pe.nt_signature, filehdr_out->nt_signature);
277d1b5e 831
277d1b5e
ILT
832 return FILHSZ;
833}
834
835unsigned int
7920ce38 836_bfd_XX_only_swap_filehdr_out (bfd * abfd, void * in, void * out)
277d1b5e 837{
6fa957a9
KH
838 struct internal_filehdr *filehdr_in = (struct internal_filehdr *) in;
839 FILHDR *filehdr_out = (FILHDR *) out;
277d1b5e 840
dc810e39
AM
841 H_PUT_16 (abfd, filehdr_in->f_magic, filehdr_out->f_magic);
842 H_PUT_16 (abfd, filehdr_in->f_nscns, filehdr_out->f_nscns);
843 H_PUT_32 (abfd, filehdr_in->f_timdat, filehdr_out->f_timdat);
844 PUT_FILEHDR_SYMPTR (abfd, filehdr_in->f_symptr, filehdr_out->f_symptr);
845 H_PUT_32 (abfd, filehdr_in->f_nsyms, filehdr_out->f_nsyms);
846 H_PUT_16 (abfd, filehdr_in->f_opthdr, filehdr_out->f_opthdr);
847 H_PUT_16 (abfd, filehdr_in->f_flags, filehdr_out->f_flags);
277d1b5e
ILT
848
849 return FILHSZ;
850}
851
852unsigned int
7920ce38 853_bfd_XXi_swap_scnhdr_out (bfd * abfd, void * in, void * out)
277d1b5e 854{
6fa957a9
KH
855 struct internal_scnhdr *scnhdr_int = (struct internal_scnhdr *) in;
856 SCNHDR *scnhdr_ext = (SCNHDR *) out;
277d1b5e
ILT
857 unsigned int ret = SCNHSZ;
858 bfd_vma ps;
859 bfd_vma ss;
860
6fa957a9 861 memcpy (scnhdr_ext->s_name, scnhdr_int->s_name, sizeof (scnhdr_int->s_name));
277d1b5e
ILT
862
863 PUT_SCNHDR_VADDR (abfd,
9602af51 864 ((scnhdr_int->s_vaddr
6fa957a9 865 - pe_data (abfd)->pe_opthdr.ImageBase)
277d1b5e 866 & 0xffffffff),
dc810e39 867 scnhdr_ext->s_vaddr);
277d1b5e 868
5933bdc9
ILT
869 /* NT wants the size data to be rounded up to the next
870 NT_FILE_ALIGNMENT, but zero if it has no content (as in .bss,
871 sometimes). */
5933bdc9 872 if ((scnhdr_int->s_flags & IMAGE_SCN_CNT_UNINITIALIZED_DATA) != 0)
277d1b5e 873 {
ff0c9faf
NC
874 if (bfd_pe_executable_p (abfd))
875 {
876 ps = scnhdr_int->s_size;
877 ss = 0;
878 }
879 else
880 {
881 ps = 0;
882 ss = scnhdr_int->s_size;
883 }
277d1b5e
ILT
884 }
885 else
886 {
ff0c9faf
NC
887 if (bfd_pe_executable_p (abfd))
888 ps = scnhdr_int->s_paddr;
889 else
890 ps = 0;
891
277d1b5e
ILT
892 ss = scnhdr_int->s_size;
893 }
894
895 PUT_SCNHDR_SIZE (abfd, ss,
dc810e39 896 scnhdr_ext->s_size);
277d1b5e 897
5933bdc9 898 /* s_paddr in PE is really the virtual size. */
dc810e39 899 PUT_SCNHDR_PADDR (abfd, ps, scnhdr_ext->s_paddr);
277d1b5e
ILT
900
901 PUT_SCNHDR_SCNPTR (abfd, scnhdr_int->s_scnptr,
dc810e39 902 scnhdr_ext->s_scnptr);
277d1b5e 903 PUT_SCNHDR_RELPTR (abfd, scnhdr_int->s_relptr,
dc810e39 904 scnhdr_ext->s_relptr);
277d1b5e 905 PUT_SCNHDR_LNNOPTR (abfd, scnhdr_int->s_lnnoptr,
dc810e39 906 scnhdr_ext->s_lnnoptr);
277d1b5e 907
277d1b5e 908 {
25c80428
NC
909 /* Extra flags must be set when dealing with PE. All sections should also
910 have the IMAGE_SCN_MEM_READ (0x40000000) flag set. In addition, the
911 .text section must have IMAGE_SCN_MEM_EXECUTE (0x20000000) and the data
912 sections (.idata, .data, .bss, .CRT) must have IMAGE_SCN_MEM_WRITE set
913 (this is especially important when dealing with the .idata section since
914 the addresses for routines from .dlls must be overwritten). If .reloc
915 section data is ever generated, we must add IMAGE_SCN_MEM_DISCARDABLE
916 (0x02000000). Also, the resource data should also be read and
917 writable. */
918
919 /* FIXME: Alignment is also encoded in this field, at least on PPC and
920 ARM-WINCE. Although - how do we get the original alignment field
921 back ? */
922
923 typedef struct
924 {
925 const char * section_name;
926 unsigned long must_have;
927 }
928 pe_required_section_flags;
929
930 pe_required_section_flags known_sections [] =
931 {
932 { ".arch", IMAGE_SCN_MEM_READ | IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_DISCARDABLE | IMAGE_SCN_ALIGN_8BYTES },
933 { ".bss", IMAGE_SCN_MEM_READ | IMAGE_SCN_CNT_UNINITIALIZED_DATA | IMAGE_SCN_MEM_WRITE },
934 { ".data", IMAGE_SCN_MEM_READ | IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_WRITE },
935 { ".edata", IMAGE_SCN_MEM_READ | IMAGE_SCN_CNT_INITIALIZED_DATA },
936 { ".idata", IMAGE_SCN_MEM_READ | IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_WRITE },
937 { ".pdata", IMAGE_SCN_MEM_READ | IMAGE_SCN_CNT_INITIALIZED_DATA },
938 { ".rdata", IMAGE_SCN_MEM_READ | IMAGE_SCN_CNT_INITIALIZED_DATA },
939 { ".reloc", IMAGE_SCN_MEM_READ | IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_DISCARDABLE },
940 { ".rsrc", IMAGE_SCN_MEM_READ | IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_WRITE },
941 { ".text" , IMAGE_SCN_MEM_READ | IMAGE_SCN_CNT_CODE | IMAGE_SCN_MEM_EXECUTE },
942 { ".tls", IMAGE_SCN_MEM_READ | IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_WRITE },
943 { ".xdata", IMAGE_SCN_MEM_READ | IMAGE_SCN_CNT_INITIALIZED_DATA },
944 { NULL, 0}
945 };
946
947 pe_required_section_flags * p;
1725a96e 948
66bed356
DS
949 /* We have defaulted to adding the IMAGE_SCN_MEM_WRITE flag, but now
950 we know exactly what this specific section wants so we remove it
951 and then allow the must_have field to add it back in if necessary.
952 However, we don't remove IMAGE_SCN_MEM_WRITE flag from .text if the
953 default WP_TEXT file flag has been cleared. WP_TEXT may be cleared
954 by ld --enable-auto-import (if auto-import is actually needed),
955 by ld --omagic, or by obcopy --writable-text. */
66bed356 956
25c80428
NC
957 for (p = known_sections; p->section_name; p++)
958 if (strcmp (scnhdr_int->s_name, p->section_name) == 0)
959 {
3c9d0484
DS
960 if (strcmp (scnhdr_int->s_name, ".text")
961 || (bfd_get_file_flags (abfd) & WP_TEXT))
d48bdb99
AM
962 scnhdr_int->s_flags &= ~IMAGE_SCN_MEM_WRITE;
963 scnhdr_int->s_flags |= p->must_have;
25c80428
NC
964 break;
965 }
966
d48bdb99 967 H_PUT_32 (abfd, scnhdr_int->s_flags, scnhdr_ext->s_flags);
277d1b5e
ILT
968 }
969
cb43721d 970 if (coff_data (abfd)->link_info
1049f94e 971 && ! coff_data (abfd)->link_info->relocatable
cb43721d
ILT
972 && ! coff_data (abfd)->link_info->shared
973 && strcmp (scnhdr_int->s_name, ".text") == 0)
277d1b5e 974 {
cb43721d 975 /* By inference from looking at MS output, the 32 bit field
7dee875e 976 which is the combination of the number_of_relocs and
cb43721d
ILT
977 number_of_linenos is used for the line number count in
978 executables. A 16-bit field won't do for cc1. The MS
979 document says that the number of relocs is zero for
980 executables, but the 17-th bit has been observed to be there.
981 Overflow is not an issue: a 4G-line program will overflow a
982 bunch of other fields long before this! */
dc810e39
AM
983 H_PUT_16 (abfd, (scnhdr_int->s_nlnno & 0xffff), scnhdr_ext->s_nlnno);
984 H_PUT_16 (abfd, (scnhdr_int->s_nlnno >> 16), scnhdr_ext->s_nreloc);
277d1b5e 985 }
277d1b5e
ILT
986 else
987 {
cb43721d 988 if (scnhdr_int->s_nlnno <= 0xffff)
dc810e39 989 H_PUT_16 (abfd, scnhdr_int->s_nlnno, scnhdr_ext->s_nlnno);
cb43721d
ILT
990 else
991 {
992 (*_bfd_error_handler) (_("%s: line number overflow: 0x%lx > 0xffff"),
993 bfd_get_filename (abfd),
994 scnhdr_int->s_nlnno);
995 bfd_set_error (bfd_error_file_truncated);
dc810e39 996 H_PUT_16 (abfd, 0xffff, scnhdr_ext->s_nlnno);
cb43721d
ILT
997 ret = 0;
998 }
1725a96e 999
cd339148
NS
1000 /* Although we could encode 0xffff relocs here, we do not, to be
1001 consistent with other parts of bfd. Also it lets us warn, as
1002 we should never see 0xffff here w/o having the overflow flag
1003 set. */
1004 if (scnhdr_int->s_nreloc < 0xffff)
dc810e39 1005 H_PUT_16 (abfd, scnhdr_int->s_nreloc, scnhdr_ext->s_nreloc);
cb43721d
ILT
1006 else
1007 {
1725a96e 1008 /* PE can deal with large #s of relocs, but not here. */
dc810e39 1009 H_PUT_16 (abfd, 0xffff, scnhdr_ext->s_nreloc);
3e4554a2 1010 scnhdr_int->s_flags |= IMAGE_SCN_LNK_NRELOC_OVFL;
dc810e39 1011 H_PUT_32 (abfd, scnhdr_int->s_flags, scnhdr_ext->s_flags);
cb43721d 1012 }
277d1b5e
ILT
1013 }
1014 return ret;
1015}
1016
1725a96e 1017static char * dir_names[IMAGE_NUMBEROF_DIRECTORY_ENTRIES] =
7920ce38
NC
1018{
1019 N_("Export Directory [.edata (or where ever we found it)]"),
1020 N_("Import Directory [parts of .idata]"),
1021 N_("Resource Directory [.rsrc]"),
1022 N_("Exception Directory [.pdata]"),
1023 N_("Security Directory"),
1024 N_("Base Relocation Directory [.reloc]"),
1025 N_("Debug Directory"),
1026 N_("Description Directory"),
1027 N_("Special Directory"),
1028 N_("Thread Storage Directory [.tls]"),
1029 N_("Load Configuration Directory"),
1030 N_("Bound Import Directory"),
1031 N_("Import Address Table Directory"),
1032 N_("Delay Import Directory"),
6c73cbb1 1033 N_("CLR Runtime Header"),
7920ce38
NC
1034 N_("Reserved")
1035};
1725a96e 1036
277d1b5e
ILT
1037#ifdef POWERPC_LE_PE
1038/* The code for the PPC really falls in the "architecture dependent"
1039 category. However, it's not clear that anyone will ever care, so
1040 we're ignoring the issue for now; if/when PPC matters, some of this
1041 may need to go into peicode.h, or arguments passed to enable the
1042 PPC- specific code. */
1043#endif
1044
b34976b6 1045static bfd_boolean
7920ce38 1046pe_print_idata (bfd * abfd, void * vfile)
277d1b5e
ILT
1047{
1048 FILE *file = (FILE *) vfile;
a76b448c 1049 bfd_byte *data;
8181c403
AM
1050 asection *section;
1051 bfd_signed_vma adj;
277d1b5e
ILT
1052
1053#ifdef POWERPC_LE_PE
1054 asection *rel_section = bfd_get_section_by_name (abfd, ".reldata");
1055#endif
1056
a76b448c 1057 bfd_size_type datasize = 0;
277d1b5e 1058 bfd_size_type dataoff;
277d1b5e 1059 bfd_size_type i;
277d1b5e
ILT
1060 int onaline = 20;
1061
1062 pe_data_type *pe = pe_data (abfd);
1063 struct internal_extra_pe_aouthdr *extra = &pe->pe_opthdr;
1064
8181c403 1065 bfd_vma addr;
277d1b5e 1066
6c73cbb1 1067 addr = extra->DataDirectory[PE_IMPORT_TABLE].VirtualAddress;
277d1b5e 1068
6c73cbb1 1069 if (addr == 0 && extra->DataDirectory[PE_IMPORT_TABLE].Size == 0)
8181c403 1070 {
a76b448c
AM
1071 /* Maybe the extra header isn't there. Look for the section. */
1072 section = bfd_get_section_by_name (abfd, ".idata");
1073 if (section == NULL)
b34976b6 1074 return TRUE;
a76b448c
AM
1075
1076 addr = section->vma;
eea6121a 1077 datasize = section->size;
a76b448c 1078 if (datasize == 0)
b34976b6 1079 return TRUE;
8181c403 1080 }
a76b448c 1081 else
8181c403 1082 {
a76b448c
AM
1083 addr += extra->ImageBase;
1084 for (section = abfd->sections; section != NULL; section = section->next)
1085 {
eea6121a 1086 datasize = section->size;
a76b448c
AM
1087 if (addr >= section->vma && addr < section->vma + datasize)
1088 break;
1089 }
1090
1091 if (section == NULL)
1092 {
1093 fprintf (file,
1094 _("\nThere is an import table, but the section containing it could not be found\n"));
b34976b6 1095 return TRUE;
a76b448c 1096 }
8181c403 1097 }
5933bdc9 1098
8181c403
AM
1099 fprintf (file, _("\nThere is an import table in %s at 0x%lx\n"),
1100 section->name, (unsigned long) addr);
277d1b5e 1101
8181c403 1102 dataoff = addr - section->vma;
a76b448c 1103 datasize -= dataoff;
277d1b5e
ILT
1104
1105#ifdef POWERPC_LE_PE
eea6121a 1106 if (rel_section != 0 && rel_section->size != 0)
277d1b5e
ILT
1107 {
1108 /* The toc address can be found by taking the starting address,
1109 which on the PPC locates a function descriptor. The
1110 descriptor consists of the function code starting address
1111 followed by the address of the toc. The starting address we
1112 get from the bfd, and the descriptor is supposed to be in the
1113 .reldata section. */
1114
1115 bfd_vma loadable_toc_address;
1116 bfd_vma toc_address;
1117 bfd_vma start_address;
eea6121a 1118 bfd_byte *data;
a50b2160 1119 bfd_vma offset;
8181c403 1120
eea6121a
AM
1121 if (!bfd_malloc_and_get_section (abfd, rel_section, &data))
1122 {
1123 if (data != NULL)
1124 free (data);
1125 return FALSE;
1126 }
277d1b5e
ILT
1127
1128 offset = abfd->start_address - rel_section->vma;
1129
a50b2160
JJ
1130 if (offset >= rel_section->size || offset + 8 > rel_section->size)
1131 {
1132 if (data != NULL)
1133 free (data);
1134 return FALSE;
1135 }
1136
db8503c4
AM
1137 start_address = bfd_get_32 (abfd, data + offset);
1138 loadable_toc_address = bfd_get_32 (abfd, data + offset + 4);
277d1b5e
ILT
1139 toc_address = loadable_toc_address - 32768;
1140
9602af51 1141 fprintf (file,
6fa957a9
KH
1142 _("\nFunction descriptor located at the start address: %04lx\n"),
1143 (unsigned long int) (abfd->start_address));
277d1b5e
ILT
1144 fprintf (file,
1145 _("\tcode-base %08lx toc (loadable/actual) %08lx/%08lx\n"),
1146 start_address, loadable_toc_address, toc_address);
eea6121a
AM
1147 if (data != NULL)
1148 free (data);
277d1b5e
ILT
1149 }
1150 else
1151 {
9602af51 1152 fprintf (file,
6fa957a9 1153 _("\nNo reldata section! Function descriptor not decoded.\n"));
277d1b5e
ILT
1154 }
1155#endif
1156
9602af51 1157 fprintf (file,
6fa957a9
KH
1158 _("\nThe Import Tables (interpreted %s section contents)\n"),
1159 section->name);
9602af51 1160 fprintf (file,
ca09e32b
NC
1161 _("\
1162 vma: Hint Time Forward DLL First\n\
1163 Table Stamp Chain Name Thunk\n"));
277d1b5e 1164
db8503c4 1165 /* Read the whole section. Some of the fields might be before dataoff. */
eea6121a
AM
1166 if (!bfd_malloc_and_get_section (abfd, section, &data))
1167 {
1168 if (data != NULL)
1169 free (data);
1170 return FALSE;
1171 }
277d1b5e 1172
db8503c4 1173 adj = section->vma - extra->ImageBase;
277d1b5e 1174
5e226794 1175 /* Print all image import descriptors. */
5933bdc9 1176 for (i = 0; i < datasize; i += onaline)
277d1b5e
ILT
1177 {
1178 bfd_vma hint_addr;
1179 bfd_vma time_stamp;
1180 bfd_vma forward_chain;
1181 bfd_vma dll_name;
1182 bfd_vma first_thunk;
1183 int idx = 0;
1184 bfd_size_type j;
1185 char *dll;
1186
6c73cbb1 1187 /* Print (i + extra->DataDirectory[PE_IMPORT_TABLE].VirtualAddress). */
db8503c4 1188 fprintf (file, " %08lx\t", (unsigned long) (i + adj + dataoff));
db8503c4
AM
1189 hint_addr = bfd_get_32 (abfd, data + i + dataoff);
1190 time_stamp = bfd_get_32 (abfd, data + i + 4 + dataoff);
1191 forward_chain = bfd_get_32 (abfd, data + i + 8 + dataoff);
1192 dll_name = bfd_get_32 (abfd, data + i + 12 + dataoff);
1193 first_thunk = bfd_get_32 (abfd, data + i + 16 + dataoff);
5933bdc9
ILT
1194
1195 fprintf (file, "%08lx %08lx %08lx %08lx %08lx\n",
a76b448c
AM
1196 (unsigned long) hint_addr,
1197 (unsigned long) time_stamp,
1198 (unsigned long) forward_chain,
1199 (unsigned long) dll_name,
1200 (unsigned long) first_thunk);
277d1b5e
ILT
1201
1202 if (hint_addr == 0 && first_thunk == 0)
1203 break;
1204
a50b2160
JJ
1205 if (dll_name - adj >= section->size)
1206 break;
1207
8181c403 1208 dll = (char *) data + dll_name - adj;
9602af51 1209 fprintf (file, _("\n\tDLL Name: %s\n"), dll);
277d1b5e
ILT
1210
1211 if (hint_addr != 0)
1212 {
6e7c73dd
CF
1213 bfd_byte *ft_data;
1214 asection *ft_section;
1215 bfd_vma ft_addr;
1216 bfd_size_type ft_datasize;
1217 int ft_idx;
6e7c73dd
CF
1218 int ft_allocated = 0;
1219
5e226794 1220 fprintf (file, _("\tvma: Hint/Ord Member-Name Bound-To\n"));
277d1b5e 1221
8181c403 1222 idx = hint_addr - adj;
5e226794
NC
1223
1224 ft_addr = first_thunk + extra->ImageBase;
6e7c73dd
CF
1225 ft_data = data;
1226 ft_idx = first_thunk - adj;
1227 ft_allocated = 0;
6c73cbb1
NC
1228
1229 if (first_thunk != hint_addr)
6e7c73dd
CF
1230 {
1231 /* Find the section which contains the first thunk. */
1232 for (ft_section = abfd->sections;
1233 ft_section != NULL;
1234 ft_section = ft_section->next)
1235 {
eea6121a 1236 ft_datasize = ft_section->size;
6e7c73dd
CF
1237 if (ft_addr >= ft_section->vma
1238 && ft_addr < ft_section->vma + ft_datasize)
1239 break;
1240 }
1241
1242 if (ft_section == NULL)
1243 {
1244 fprintf (file,
1245 _("\nThere is a first thunk, but the section containing it could not be found\n"));
1246 continue;
1247 }
1248
1249 /* Now check to see if this section is the same as our current
1250 section. If it is not then we will have to load its data in. */
1251 if (ft_section == section)
1252 {
1253 ft_data = data;
1254 ft_idx = first_thunk - adj;
1255 }
1256 else
1257 {
1258 ft_idx = first_thunk - (ft_section->vma - extra->ImageBase);
7920ce38 1259 ft_data = bfd_malloc (datasize);
6e7c73dd
CF
1260 if (ft_data == NULL)
1261 continue;
1262
1263 /* Read datasize bfd_bytes starting at offset ft_idx. */
7920ce38
NC
1264 if (! bfd_get_section_contents
1265 (abfd, ft_section, ft_data, (bfd_vma) ft_idx, datasize))
6e7c73dd
CF
1266 {
1267 free (ft_data);
1268 continue;
1269 }
1270
1271 ft_idx = 0;
1272 ft_allocated = 1;
1273 }
1274 }
5e226794
NC
1275
1276 /* Print HintName vector entries. */
99ad8390
NC
1277#ifdef COFF_WITH_pex64
1278 for (j = 0; j < datasize; j += 8)
1279 {
1280 unsigned long member = bfd_get_32 (abfd, data + idx + j);
1281 unsigned long member_high = bfd_get_32 (abfd, data + idx + j + 4);
1282
1283 if (!member && !member_high)
1284 break;
1285
1286 if (member_high & 0x80000000)
1287 fprintf (file, "\t%lx%08lx\t %4lx%08lx <none>",
1288 member_high,member, member_high & 0x7fffffff, member);
1289 else
1290 {
1291 int ordinal;
1292 char *member_name;
1293
1294 ordinal = bfd_get_16 (abfd, data + member - adj);
1295 member_name = (char *) data + member - adj + 2;
1296 fprintf (file, "\t%04lx\t %4d %s",member, ordinal, member_name);
1297 }
1298
1299 /* If the time stamp is not zero, the import address
1300 table holds actual addresses. */
1301 if (time_stamp != 0
1302 && first_thunk != 0
1303 && first_thunk != hint_addr)
1304 fprintf (file, "\t%04lx",
1305 (long) bfd_get_32 (abfd, ft_data + ft_idx + j));
1306 fprintf (file, "\n");
1307 }
1308#else
5933bdc9 1309 for (j = 0; j < datasize; j += 4)
277d1b5e
ILT
1310 {
1311 unsigned long member = bfd_get_32 (abfd, data + idx + j);
1312
5e226794 1313 /* Print single IMAGE_IMPORT_BY_NAME vector. */
277d1b5e
ILT
1314 if (member == 0)
1315 break;
5e226794 1316
277d1b5e 1317 if (member & 0x80000000)
5e226794
NC
1318 fprintf (file, "\t%04lx\t %4lu <none>",
1319 member, member & 0x7fffffff);
277d1b5e
ILT
1320 else
1321 {
1322 int ordinal;
1323 char *member_name;
1324
8181c403
AM
1325 ordinal = bfd_get_16 (abfd, data + member - adj);
1326 member_name = (char *) data + member - adj + 2;
277d1b5e
ILT
1327 fprintf (file, "\t%04lx\t %4d %s",
1328 member, ordinal, member_name);
1329 }
5e226794 1330
277d1b5e 1331 /* If the time stamp is not zero, the import address
5e226794
NC
1332 table holds actual addresses. */
1333 if (time_stamp != 0
1334 && first_thunk != 0
1335 && first_thunk != hint_addr)
277d1b5e 1336 fprintf (file, "\t%04lx",
6e7c73dd 1337 (long) bfd_get_32 (abfd, ft_data + ft_idx + j));
277d1b5e
ILT
1338
1339 fprintf (file, "\n");
1340 }
99ad8390 1341#endif
e4cf60a8
NC
1342 if (ft_allocated)
1343 free (ft_data);
277d1b5e
ILT
1344 }
1345
9602af51 1346 fprintf (file, "\n");
277d1b5e
ILT
1347 }
1348
1349 free (data);
1350
b34976b6 1351 return TRUE;
277d1b5e
ILT
1352}
1353
b34976b6 1354static bfd_boolean
7920ce38 1355pe_print_edata (bfd * abfd, void * vfile)
277d1b5e
ILT
1356{
1357 FILE *file = (FILE *) vfile;
a76b448c 1358 bfd_byte *data;
8181c403 1359 asection *section;
a76b448c 1360 bfd_size_type datasize = 0;
277d1b5e
ILT
1361 bfd_size_type dataoff;
1362 bfd_size_type i;
8181c403 1363 bfd_signed_vma adj;
1725a96e
NC
1364 struct EDT_type
1365 {
7920ce38 1366 long export_flags; /* Reserved - should be zero. */
6fa957a9
KH
1367 long time_stamp;
1368 short major_ver;
1369 short minor_ver;
7920ce38
NC
1370 bfd_vma name; /* RVA - relative to image base. */
1371 long base; /* Ordinal base. */
1372 unsigned long num_functions;/* Number in the export address table. */
1373 unsigned long num_names; /* Number in the name pointer table. */
1374 bfd_vma eat_addr; /* RVA to the export address table. */
1375 bfd_vma npt_addr; /* RVA to the Export Name Pointer Table. */
1376 bfd_vma ot_addr; /* RVA to the Ordinal Table. */
6fa957a9 1377 } edt;
277d1b5e
ILT
1378
1379 pe_data_type *pe = pe_data (abfd);
1380 struct internal_extra_pe_aouthdr *extra = &pe->pe_opthdr;
1381
8181c403 1382 bfd_vma addr;
277d1b5e 1383
6c73cbb1 1384 addr = extra->DataDirectory[PE_EXPORT_TABLE].VirtualAddress;
277d1b5e 1385
6c73cbb1 1386 if (addr == 0 && extra->DataDirectory[PE_EXPORT_TABLE].Size == 0)
8181c403 1387 {
a76b448c
AM
1388 /* Maybe the extra header isn't there. Look for the section. */
1389 section = bfd_get_section_by_name (abfd, ".edata");
1390 if (section == NULL)
b34976b6 1391 return TRUE;
a76b448c
AM
1392
1393 addr = section->vma;
0facbdf5 1394 dataoff = 0;
eea6121a 1395 datasize = section->size;
a76b448c 1396 if (datasize == 0)
b34976b6 1397 return TRUE;
8181c403 1398 }
a76b448c 1399 else
8181c403 1400 {
a76b448c 1401 addr += extra->ImageBase;
1725a96e 1402
a76b448c 1403 for (section = abfd->sections; section != NULL; section = section->next)
0facbdf5
NC
1404 if (addr >= section->vma && addr < section->vma + section->size)
1405 break;
a76b448c
AM
1406
1407 if (section == NULL)
1408 {
1409 fprintf (file,
1410 _("\nThere is an export table, but the section containing it could not be found\n"));
b34976b6 1411 return TRUE;
a76b448c 1412 }
0facbdf5
NC
1413
1414 dataoff = addr - section->vma;
6c73cbb1 1415 datasize = extra->DataDirectory[PE_EXPORT_TABLE].Size;
0facbdf5
NC
1416 if (datasize > section->size - dataoff)
1417 {
1418 fprintf (file,
1419 _("\nThere is an export table in %s, but it does not fit into that section\n"),
1420 section->name);
1421 return TRUE;
1422 }
277d1b5e
ILT
1423 }
1424
8181c403
AM
1425 fprintf (file, _("\nThere is an export table in %s at 0x%lx\n"),
1426 section->name, (unsigned long) addr);
1427
7920ce38 1428 data = bfd_malloc (datasize);
8181c403 1429 if (data == NULL)
b34976b6 1430 return FALSE;
277d1b5e 1431
7920ce38 1432 if (! bfd_get_section_contents (abfd, section, data,
dc810e39 1433 (file_ptr) dataoff, datasize))
b34976b6 1434 return FALSE;
277d1b5e 1435
6fa957a9
KH
1436 /* Go get Export Directory Table. */
1437 edt.export_flags = bfd_get_32 (abfd, data + 0);
1438 edt.time_stamp = bfd_get_32 (abfd, data + 4);
1439 edt.major_ver = bfd_get_16 (abfd, data + 8);
1440 edt.minor_ver = bfd_get_16 (abfd, data + 10);
1441 edt.name = bfd_get_32 (abfd, data + 12);
1442 edt.base = bfd_get_32 (abfd, data + 16);
1443 edt.num_functions = bfd_get_32 (abfd, data + 20);
1444 edt.num_names = bfd_get_32 (abfd, data + 24);
1445 edt.eat_addr = bfd_get_32 (abfd, data + 28);
1446 edt.npt_addr = bfd_get_32 (abfd, data + 32);
1447 edt.ot_addr = bfd_get_32 (abfd, data + 36);
277d1b5e 1448
8181c403 1449 adj = section->vma - extra->ImageBase + dataoff;
277d1b5e 1450
1725a96e 1451 /* Dump the EDT first. */
9602af51 1452 fprintf (file,
6fa957a9
KH
1453 _("\nThe Export Tables (interpreted %s section contents)\n\n"),
1454 section->name);
277d1b5e 1455
9602af51 1456 fprintf (file,
6fa957a9 1457 _("Export Flags \t\t\t%lx\n"), (unsigned long) edt.export_flags);
277d1b5e 1458
9602af51 1459 fprintf (file,
6fa957a9 1460 _("Time/Date stamp \t\t%lx\n"), (unsigned long) edt.time_stamp);
277d1b5e 1461
9602af51 1462 fprintf (file,
6fa957a9 1463 _("Major/Minor \t\t\t%d/%d\n"), edt.major_ver, edt.minor_ver);
277d1b5e
ILT
1464
1465 fprintf (file,
1466 _("Name \t\t\t\t"));
1467 fprintf_vma (file, edt.name);
1468 fprintf (file,
8181c403 1469 " %s\n", data + edt.name - adj);
277d1b5e 1470
9602af51 1471 fprintf (file,
6fa957a9 1472 _("Ordinal Base \t\t\t%ld\n"), edt.base);
277d1b5e 1473
9602af51 1474 fprintf (file,
6fa957a9 1475 _("Number in:\n"));
277d1b5e 1476
9602af51 1477 fprintf (file,
6fa957a9
KH
1478 _("\tExport Address Table \t\t%08lx\n"),
1479 edt.num_functions);
277d1b5e 1480
9602af51 1481 fprintf (file,
6fa957a9 1482 _("\t[Name Pointer/Ordinal] Table\t%08lx\n"), edt.num_names);
277d1b5e 1483
9602af51 1484 fprintf (file,
6fa957a9 1485 _("Table Addresses\n"));
277d1b5e
ILT
1486
1487 fprintf (file,
1488 _("\tExport Address Table \t\t"));
1489 fprintf_vma (file, edt.eat_addr);
1490 fprintf (file, "\n");
1491
1492 fprintf (file,
6fa957a9 1493 _("\tName Pointer Table \t\t"));
277d1b5e
ILT
1494 fprintf_vma (file, edt.npt_addr);
1495 fprintf (file, "\n");
1496
1497 fprintf (file,
1498 _("\tOrdinal Table \t\t\t"));
1499 fprintf_vma (file, edt.ot_addr);
1500 fprintf (file, "\n");
1501
5933bdc9 1502 /* The next table to find is the Export Address Table. It's basically
277d1b5e
ILT
1503 a list of pointers that either locate a function in this dll, or
1504 forward the call to another dll. Something like:
1725a96e
NC
1505 typedef union
1506 {
277d1b5e
ILT
1507 long export_rva;
1508 long forwarder_rva;
7920ce38 1509 } export_address_table_entry; */
277d1b5e 1510
9602af51 1511 fprintf (file,
277d1b5e
ILT
1512 _("\nExport Address Table -- Ordinal Base %ld\n"),
1513 edt.base);
1514
1515 for (i = 0; i < edt.num_functions; ++i)
1516 {
1517 bfd_vma eat_member = bfd_get_32 (abfd,
8181c403 1518 data + edt.eat_addr + (i * 4) - adj);
277d1b5e
ILT
1519 if (eat_member == 0)
1520 continue;
1521
db8503c4 1522 if (eat_member - adj <= datasize)
277d1b5e 1523 {
db8503c4 1524 /* This rva is to a name (forwarding function) in our section. */
6fa957a9 1525 /* Should locate a function descriptor. */
5933bdc9
ILT
1526 fprintf (file,
1527 "\t[%4ld] +base[%4ld] %04lx %s -- %s\n",
a76b448c
AM
1528 (long) i,
1529 (long) (i + edt.base),
1530 (unsigned long) eat_member,
1531 _("Forwarder RVA"),
1532 data + eat_member - adj);
277d1b5e
ILT
1533 }
1534 else
1535 {
6fa957a9 1536 /* Should locate a function descriptor in the reldata section. */
5933bdc9
ILT
1537 fprintf (file,
1538 "\t[%4ld] +base[%4ld] %04lx %s\n",
a76b448c
AM
1539 (long) i,
1540 (long) (i + edt.base),
1541 (unsigned long) eat_member,
5933bdc9 1542 _("Export RVA"));
277d1b5e
ILT
1543 }
1544 }
1545
6fa957a9
KH
1546 /* The Export Name Pointer Table is paired with the Export Ordinal Table. */
1547 /* Dump them in parallel for clarity. */
9602af51 1548 fprintf (file,
6fa957a9 1549 _("\n[Ordinal/Name Pointer] Table\n"));
277d1b5e
ILT
1550
1551 for (i = 0; i < edt.num_names; ++i)
1552 {
9602af51 1553 bfd_vma name_ptr = bfd_get_32 (abfd,
277d1b5e
ILT
1554 data +
1555 edt.npt_addr
8181c403 1556 + (i*4) - adj);
9602af51 1557
8181c403 1558 char *name = (char *) data + name_ptr - adj;
277d1b5e 1559
9602af51 1560 bfd_vma ord = bfd_get_16 (abfd,
277d1b5e
ILT
1561 data +
1562 edt.ot_addr
8181c403 1563 + (i*2) - adj);
9602af51 1564 fprintf (file,
277d1b5e 1565 "\t[%4ld] %s\n", (long) ord, name);
277d1b5e
ILT
1566 }
1567
1568 free (data);
1569
b34976b6 1570 return TRUE;
277d1b5e
ILT
1571}
1572
fac41780
JW
1573/* This really is architecture dependent. On IA-64, a .pdata entry
1574 consists of three dwords containing relative virtual addresses that
1575 specify the start and end address of the code range the entry
1576 covers and the address of the corresponding unwind info data. */
6fa957a9 1577
b34976b6 1578static bfd_boolean
7920ce38 1579pe_print_pdata (bfd * abfd, void * vfile)
277d1b5e 1580{
99ad8390
NC
1581#if defined(COFF_WITH_pep) && !defined(COFF_WITH_pex64)
1582# define PDATA_ROW_SIZE (3 * 8)
fac41780 1583#else
99ad8390 1584# define PDATA_ROW_SIZE (5 * 4)
fac41780 1585#endif
277d1b5e
ILT
1586 FILE *file = (FILE *) vfile;
1587 bfd_byte *data = 0;
1588 asection *section = bfd_get_section_by_name (abfd, ".pdata");
1589 bfd_size_type datasize = 0;
1590 bfd_size_type i;
1591 bfd_size_type start, stop;
fac41780 1592 int onaline = PDATA_ROW_SIZE;
277d1b5e 1593
5933bdc9
ILT
1594 if (section == NULL
1595 || coff_section_data (abfd, section) == NULL
1596 || pei_section_data (abfd, section) == NULL)
b34976b6 1597 return TRUE;
277d1b5e 1598
5933bdc9 1599 stop = pei_section_data (abfd, section)->virt_size;
277d1b5e 1600 if ((stop % onaline) != 0)
6fa957a9
KH
1601 fprintf (file,
1602 _("Warning, .pdata section size (%ld) is not a multiple of %d\n"),
1603 (long) stop, onaline);
277d1b5e 1604
5933bdc9
ILT
1605 fprintf (file,
1606 _("\nThe Function Table (interpreted .pdata section contents)\n"));
99ad8390 1607#if defined(COFF_WITH_pep) && !defined(COFF_WITH_pex64)
9602af51 1608 fprintf (file,
6fa957a9 1609 _(" vma:\t\t\tBegin Address End Address Unwind Info\n"));
fac41780 1610#else
ca09e32b
NC
1611 fprintf (file, _("\
1612 vma:\t\tBegin End EH EH PrologEnd Exception\n\
1613 \t\tAddress Address Handler Data Address Mask\n"));
fac41780 1614#endif
277d1b5e 1615
eea6121a 1616 datasize = section->size;
dc810e39 1617 if (datasize == 0)
b34976b6 1618 return TRUE;
277d1b5e 1619
7920ce38 1620 if (! bfd_malloc_and_get_section (abfd, section, &data))
eea6121a
AM
1621 {
1622 if (data != NULL)
1623 free (data);
1624 return FALSE;
1625 }
277d1b5e
ILT
1626
1627 start = 0;
1628
1629 for (i = start; i < stop; i += onaline)
1630 {
1631 bfd_vma begin_addr;
1632 bfd_vma end_addr;
1633 bfd_vma eh_handler;
1634 bfd_vma eh_data;
1635 bfd_vma prolog_end_addr;
5933bdc9 1636 int em_data;
277d1b5e 1637
fac41780 1638 if (i + PDATA_ROW_SIZE > stop)
277d1b5e 1639 break;
5933bdc9 1640
6fa957a9
KH
1641 begin_addr = GET_PDATA_ENTRY (abfd, data + i );
1642 end_addr = GET_PDATA_ENTRY (abfd, data + i + 4);
1643 eh_handler = GET_PDATA_ENTRY (abfd, data + i + 8);
1644 eh_data = GET_PDATA_ENTRY (abfd, data + i + 12);
1645 prolog_end_addr = GET_PDATA_ENTRY (abfd, data + i + 16);
9602af51 1646
277d1b5e
ILT
1647 if (begin_addr == 0 && end_addr == 0 && eh_handler == 0
1648 && eh_data == 0 && prolog_end_addr == 0)
1725a96e
NC
1649 /* We are probably into the padding of the section now. */
1650 break;
277d1b5e 1651
5933bdc9 1652 em_data = ((eh_handler & 0x1) << 2) | (prolog_end_addr & 0x3);
6fa957a9
KH
1653 eh_handler &= ~(bfd_vma) 0x3;
1654 prolog_end_addr &= ~(bfd_vma) 0x3;
fac41780
JW
1655
1656 fputc (' ', file);
1657 fprintf_vma (file, i + section->vma); fputc ('\t', file);
1658 fprintf_vma (file, begin_addr); fputc (' ', file);
1659 fprintf_vma (file, end_addr); fputc (' ', file);
1660 fprintf_vma (file, eh_handler);
99ad8390 1661#if !defined(COFF_WITH_pep) || defined(COFF_WITH_pex64)
fac41780
JW
1662 fputc (' ', file);
1663 fprintf_vma (file, eh_data); fputc (' ', file);
1664 fprintf_vma (file, prolog_end_addr);
1665 fprintf (file, " %x", em_data);
1666#endif
277d1b5e
ILT
1667
1668#ifdef POWERPC_LE_PE
1669 if (eh_handler == 0 && eh_data != 0)
1670 {
6fa957a9 1671 /* Special bits here, although the meaning may be a little
7920ce38
NC
1672 mysterious. The only one I know for sure is 0x03
1673 Code Significance
1674 0x00 None
1675 0x01 Register Save Millicode
1676 0x02 Register Restore Millicode
1677 0x03 Glue Code Sequence. */
277d1b5e
ILT
1678 switch (eh_data)
1679 {
1680 case 0x01:
9602af51 1681 fprintf (file, _(" Register save millicode"));
277d1b5e
ILT
1682 break;
1683 case 0x02:
9602af51 1684 fprintf (file, _(" Register restore millicode"));
277d1b5e
ILT
1685 break;
1686 case 0x03:
9602af51 1687 fprintf (file, _(" Glue code sequence"));
277d1b5e
ILT
1688 break;
1689 default:
1690 break;
1691 }
1692 }
1693#endif
9602af51 1694 fprintf (file, "\n");
277d1b5e
ILT
1695 }
1696
1697 free (data);
1698
b34976b6 1699 return TRUE;
277d1b5e
ILT
1700}
1701
5933bdc9 1702#define IMAGE_REL_BASED_HIGHADJ 4
1725a96e 1703static const char * const tbl[] =
7920ce38
NC
1704{
1705 "ABSOLUTE",
1706 "HIGH",
1707 "LOW",
1708 "HIGHLOW",
1709 "HIGHADJ",
1710 "MIPS_JMPADDR",
1711 "SECTION",
1712 "REL32",
1713 "RESERVED1",
1714 "MIPS_JMPADDR16",
1715 "DIR64",
2bfd55ca 1716 "HIGH3ADJ",
7920ce38
NC
1717 "UNKNOWN", /* MUST be last. */
1718};
277d1b5e 1719
b34976b6 1720static bfd_boolean
7920ce38 1721pe_print_reloc (bfd * abfd, void * vfile)
277d1b5e
ILT
1722{
1723 FILE *file = (FILE *) vfile;
1724 bfd_byte *data = 0;
1725 asection *section = bfd_get_section_by_name (abfd, ".reloc");
dc810e39 1726 bfd_size_type datasize;
277d1b5e
ILT
1727 bfd_size_type i;
1728 bfd_size_type start, stop;
1729
5933bdc9 1730 if (section == NULL)
b34976b6 1731 return TRUE;
277d1b5e 1732
eea6121a 1733 if (section->size == 0)
b34976b6 1734 return TRUE;
277d1b5e 1735
5933bdc9
ILT
1736 fprintf (file,
1737 _("\n\nPE File Base Relocations (interpreted .reloc section contents)\n"));
277d1b5e 1738
eea6121a 1739 datasize = section->size;
7920ce38 1740 if (! bfd_malloc_and_get_section (abfd, section, &data))
eea6121a
AM
1741 {
1742 if (data != NULL)
1743 free (data);
1744 return FALSE;
1745 }
277d1b5e
ILT
1746
1747 start = 0;
1748
eea6121a 1749 stop = section->size;
277d1b5e
ILT
1750
1751 for (i = start; i < stop;)
1752 {
1753 int j;
1754 bfd_vma virtual_address;
1755 long number, size;
1756
1757 /* The .reloc section is a sequence of blocks, with a header consisting
1725a96e 1758 of two 32 bit quantities, followed by a number of 16 bit entries. */
9602af51
KH
1759 virtual_address = bfd_get_32 (abfd, data+i);
1760 size = bfd_get_32 (abfd, data+i+4);
277d1b5e
ILT
1761 number = (size - 8) / 2;
1762
1763 if (size == 0)
1725a96e 1764 break;
277d1b5e
ILT
1765
1766 fprintf (file,
1767 _("\nVirtual Address: %08lx Chunk size %ld (0x%lx) Number of fixups %ld\n"),
a76b448c 1768 (unsigned long) virtual_address, size, size, number);
277d1b5e
ILT
1769
1770 for (j = 0; j < number; ++j)
1771 {
5933bdc9
ILT
1772 unsigned short e = bfd_get_16 (abfd, data + i + 8 + j * 2);
1773 unsigned int t = (e & 0xF000) >> 12;
277d1b5e
ILT
1774 int off = e & 0x0FFF;
1775
5933bdc9
ILT
1776 if (t >= sizeof (tbl) / sizeof (tbl[0]))
1777 t = (sizeof (tbl) / sizeof (tbl[0])) - 1;
277d1b5e 1778
5933bdc9
ILT
1779 fprintf (file,
1780 _("\treloc %4d offset %4x [%4lx] %s"),
1781 j, off, (long) (off + virtual_address), tbl[t]);
277d1b5e 1782
17505c5c 1783 /* HIGHADJ takes an argument, - the next record *is* the
9602af51 1784 low 16 bits of addend. */
5933bdc9
ILT
1785 if (t == IMAGE_REL_BASED_HIGHADJ)
1786 {
6fa957a9
KH
1787 fprintf (file, " (%4x)",
1788 ((unsigned int)
1789 bfd_get_16 (abfd, data + i + 8 + j * 2 + 2)));
1790 j++;
5933bdc9 1791 }
9602af51 1792
17505c5c 1793 fprintf (file, "\n");
277d1b5e 1794 }
1725a96e 1795
277d1b5e
ILT
1796 i += size;
1797 }
1798
1799 free (data);
1800
b34976b6 1801 return TRUE;
277d1b5e
ILT
1802}
1803
1804/* Print out the program headers. */
1805
b34976b6 1806bfd_boolean
7920ce38 1807_bfd_XX_print_private_bfd_data_common (bfd * abfd, void * vfile)
277d1b5e
ILT
1808{
1809 FILE *file = (FILE *) vfile;
1810 int j;
1811 pe_data_type *pe = pe_data (abfd);
1812 struct internal_extra_pe_aouthdr *i = &pe->pe_opthdr;
fac41780 1813 const char *subsystem_name = NULL;
d13c9dc6 1814 const char *name;
277d1b5e
ILT
1815
1816 /* The MS dumpbin program reportedly ands with 0xff0f before
1817 printing the characteristics field. Not sure why. No reason to
1818 emulate it here. */
1819 fprintf (file, _("\nCharacteristics 0x%x\n"), pe->real_flags);
1820#undef PF
6fa957a9 1821#define PF(x, y) if (pe->real_flags & x) { fprintf (file, "\t%s\n", y); }
d70270c5
BF
1822 PF (IMAGE_FILE_RELOCS_STRIPPED, "relocations stripped");
1823 PF (IMAGE_FILE_EXECUTABLE_IMAGE, "executable");
1824 PF (IMAGE_FILE_LINE_NUMS_STRIPPED, "line numbers stripped");
1825 PF (IMAGE_FILE_LOCAL_SYMS_STRIPPED, "symbols stripped");
1826 PF (IMAGE_FILE_LARGE_ADDRESS_AWARE, "large address aware");
1827 PF (IMAGE_FILE_BYTES_REVERSED_LO, "little endian");
1828 PF (IMAGE_FILE_32BIT_MACHINE, "32 bit words");
1829 PF (IMAGE_FILE_DEBUG_STRIPPED, "debugging information removed");
1830 PF (IMAGE_FILE_SYSTEM, "system file");
1831 PF (IMAGE_FILE_DLL, "DLL");
1832 PF (IMAGE_FILE_BYTES_REVERSED_HI, "big endian");
277d1b5e
ILT
1833#undef PF
1834
5933bdc9 1835 /* ctime implies '\n'. */
0b6488e2
RH
1836 {
1837 time_t t = pe->coff.timestamp;
1838 fprintf (file, "\nTime/Date\t\t%s", ctime (&t));
1839 }
d13c9dc6
L
1840
1841#ifndef IMAGE_NT_OPTIONAL_HDR_MAGIC
1842# define IMAGE_NT_OPTIONAL_HDR_MAGIC 0x10b
1843#endif
1844#ifndef IMAGE_NT_OPTIONAL_HDR64_MAGIC
1845# define IMAGE_NT_OPTIONAL_HDR64_MAGIC 0x20b
1846#endif
1847#ifndef IMAGE_NT_OPTIONAL_HDRROM_MAGIC
1848# define IMAGE_NT_OPTIONAL_HDRROM_MAGIC 0x107
1849#endif
1850
1851 switch (i->Magic)
1852 {
1853 case IMAGE_NT_OPTIONAL_HDR_MAGIC:
1854 name = "PE32";
1855 break;
1856 case IMAGE_NT_OPTIONAL_HDR64_MAGIC:
1857 name = "PE32+";
1858 break;
1859 case IMAGE_NT_OPTIONAL_HDRROM_MAGIC:
1860 name = "ROM";
1861 break;
1862 default:
1863 name = NULL;
1864 break;
1865 }
1866 fprintf (file, "Magic\t\t\t%04x", i->Magic);
1867 if (name)
1868 fprintf (file, "\t(%s)",name);
1869 fprintf (file, "\nMajorLinkerVersion\t%d\n", i->MajorLinkerVersion);
1870 fprintf (file, "MinorLinkerVersion\t%d\n", i->MinorLinkerVersion);
1871 fprintf (file, "SizeOfCode\t\t%08lx\n", i->SizeOfCode);
1872 fprintf (file, "SizeOfInitializedData\t%08lx\n",
1873 i->SizeOfInitializedData);
1874 fprintf (file, "SizeOfUninitializedData\t%08lx\n",
1875 i->SizeOfUninitializedData);
1876 fprintf (file, "AddressOfEntryPoint\t");
1877 fprintf_vma (file, i->AddressOfEntryPoint);
1878 fprintf (file, "\nBaseOfCode\t\t");
1879 fprintf_vma (file, i->BaseOfCode);
1880#if !defined(COFF_WITH_pep) && !defined(COFF_WITH_pex64)
1881 /* PE32+ does not have BaseOfData member! */
1882 fprintf (file, "\nBaseOfData\t\t");
1883 fprintf_vma (file, i->BaseOfData);
1884#endif
1885
9602af51 1886 fprintf (file, "\nImageBase\t\t");
277d1b5e 1887 fprintf_vma (file, i->ImageBase);
9602af51 1888 fprintf (file, "\nSectionAlignment\t");
277d1b5e 1889 fprintf_vma (file, i->SectionAlignment);
9602af51 1890 fprintf (file, "\nFileAlignment\t\t");
277d1b5e 1891 fprintf_vma (file, i->FileAlignment);
9602af51
KH
1892 fprintf (file, "\nMajorOSystemVersion\t%d\n", i->MajorOperatingSystemVersion);
1893 fprintf (file, "MinorOSystemVersion\t%d\n", i->MinorOperatingSystemVersion);
1894 fprintf (file, "MajorImageVersion\t%d\n", i->MajorImageVersion);
1895 fprintf (file, "MinorImageVersion\t%d\n", i->MinorImageVersion);
1896 fprintf (file, "MajorSubsystemVersion\t%d\n", i->MajorSubsystemVersion);
1897 fprintf (file, "MinorSubsystemVersion\t%d\n", i->MinorSubsystemVersion);
1898 fprintf (file, "Win32Version\t\t%08lx\n", i->Reserved1);
1899 fprintf (file, "SizeOfImage\t\t%08lx\n", i->SizeOfImage);
1900 fprintf (file, "SizeOfHeaders\t\t%08lx\n", i->SizeOfHeaders);
1901 fprintf (file, "CheckSum\t\t%08lx\n", i->CheckSum);
1725a96e 1902
fac41780
JW
1903 switch (i->Subsystem)
1904 {
1905 case IMAGE_SUBSYSTEM_UNKNOWN:
1906 subsystem_name = "unspecified";
1907 break;
1908 case IMAGE_SUBSYSTEM_NATIVE:
1909 subsystem_name = "NT native";
1910 break;
1911 case IMAGE_SUBSYSTEM_WINDOWS_GUI:
1912 subsystem_name = "Windows GUI";
1913 break;
1914 case IMAGE_SUBSYSTEM_WINDOWS_CUI:
1915 subsystem_name = "Windows CUI";
1916 break;
1917 case IMAGE_SUBSYSTEM_POSIX_CUI:
1918 subsystem_name = "POSIX CUI";
1919 break;
1920 case IMAGE_SUBSYSTEM_WINDOWS_CE_GUI:
1921 subsystem_name = "Wince CUI";
1922 break;
1923 case IMAGE_SUBSYSTEM_EFI_APPLICATION:
1924 subsystem_name = "EFI application";
1925 break;
1926 case IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER:
1927 subsystem_name = "EFI boot service driver";
1928 break;
1929 case IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER:
9602af51 1930 subsystem_name = "EFI runtime driver";
fac41780 1931 break;
6c73cbb1
NC
1932 // These are from revision 8.0 of the MS PE/COFF spec
1933 case IMAGE_SUBSYSTEM_EFI_ROM:
1934 subsystem_name = "EFI ROM";
1935 break;
1936 case IMAGE_SUBSYSTEM_XBOX:
1937 subsystem_name = "XBOX";
1938 break;
1939 // Added default case for clarity - subsystem_name is NULL anyway.
1940 default:
1941 subsystem_name = NULL;
fac41780 1942 }
1725a96e 1943
9602af51 1944 fprintf (file, "Subsystem\t\t%08x", i->Subsystem);
fac41780
JW
1945 if (subsystem_name)
1946 fprintf (file, "\t(%s)", subsystem_name);
9602af51
KH
1947 fprintf (file, "\nDllCharacteristics\t%08x\n", i->DllCharacteristics);
1948 fprintf (file, "SizeOfStackReserve\t");
277d1b5e 1949 fprintf_vma (file, i->SizeOfStackReserve);
9602af51 1950 fprintf (file, "\nSizeOfStackCommit\t");
277d1b5e 1951 fprintf_vma (file, i->SizeOfStackCommit);
9602af51 1952 fprintf (file, "\nSizeOfHeapReserve\t");
277d1b5e 1953 fprintf_vma (file, i->SizeOfHeapReserve);
9602af51 1954 fprintf (file, "\nSizeOfHeapCommit\t");
277d1b5e 1955 fprintf_vma (file, i->SizeOfHeapCommit);
9602af51
KH
1956 fprintf (file, "\nLoaderFlags\t\t%08lx\n", i->LoaderFlags);
1957 fprintf (file, "NumberOfRvaAndSizes\t%08lx\n", i->NumberOfRvaAndSizes);
277d1b5e 1958
9602af51 1959 fprintf (file, "\nThe Data Directory\n");
277d1b5e
ILT
1960 for (j = 0; j < IMAGE_NUMBEROF_DIRECTORY_ENTRIES; j++)
1961 {
1962 fprintf (file, "Entry %1x ", j);
1963 fprintf_vma (file, i->DataDirectory[j].VirtualAddress);
1964 fprintf (file, " %08lx ", i->DataDirectory[j].Size);
1965 fprintf (file, "%s\n", dir_names[j]);
1966 }
1967
1968 pe_print_idata (abfd, vfile);
1969 pe_print_edata (abfd, vfile);
1970 pe_print_pdata (abfd, vfile);
1971 pe_print_reloc (abfd, vfile);
1972
b34976b6 1973 return TRUE;
277d1b5e
ILT
1974}
1975
1976/* Copy any private info we understand from the input bfd
1977 to the output bfd. */
1978
b34976b6 1979bfd_boolean
7920ce38 1980_bfd_XX_bfd_copy_private_bfd_data_common (bfd * ibfd, bfd * obfd)
277d1b5e
ILT
1981{
1982 /* One day we may try to grok other private data. */
1983 if (ibfd->xvec->flavour != bfd_target_coff_flavour
1984 || obfd->xvec->flavour != bfd_target_coff_flavour)
b34976b6 1985 return TRUE;
277d1b5e
ILT
1986
1987 pe_data (obfd)->pe_opthdr = pe_data (ibfd)->pe_opthdr;
1988 pe_data (obfd)->dll = pe_data (ibfd)->dll;
1989
1725a96e 1990 /* For strip: if we removed .reloc, we'll make a real mess of things
5933bdc9
ILT
1991 if we don't remove this entry as well. */
1992 if (! pe_data (obfd)->has_reloc_section)
1993 {
6c73cbb1
NC
1994 pe_data (obfd)->pe_opthdr.DataDirectory[PE_BASE_RELOCATION_TABLE].VirtualAddress = 0;
1995 pe_data (obfd)->pe_opthdr.DataDirectory[PE_BASE_RELOCATION_TABLE].Size = 0;
5933bdc9 1996 }
b34976b6 1997 return TRUE;
277d1b5e
ILT
1998}
1999
9602af51 2000/* Copy private section data. */
1725a96e 2001
b34976b6 2002bfd_boolean
7920ce38
NC
2003_bfd_XX_bfd_copy_private_section_data (bfd *ibfd,
2004 asection *isec,
2005 bfd *obfd,
2006 asection *osec)
277d1b5e
ILT
2007{
2008 if (bfd_get_flavour (ibfd) != bfd_target_coff_flavour
2009 || bfd_get_flavour (obfd) != bfd_target_coff_flavour)
b34976b6 2010 return TRUE;
277d1b5e
ILT
2011
2012 if (coff_section_data (ibfd, isec) != NULL
2013 && pei_section_data (ibfd, isec) != NULL)
2014 {
2015 if (coff_section_data (obfd, osec) == NULL)
2016 {
dc810e39 2017 bfd_size_type amt = sizeof (struct coff_section_tdata);
7920ce38 2018 osec->used_by_bfd = bfd_zalloc (obfd, amt);
277d1b5e 2019 if (osec->used_by_bfd == NULL)
b34976b6 2020 return FALSE;
277d1b5e 2021 }
1725a96e 2022
277d1b5e
ILT
2023 if (pei_section_data (obfd, osec) == NULL)
2024 {
dc810e39 2025 bfd_size_type amt = sizeof (struct pei_section_tdata);
7920ce38 2026 coff_section_data (obfd, osec)->tdata = bfd_zalloc (obfd, amt);
277d1b5e 2027 if (coff_section_data (obfd, osec)->tdata == NULL)
b34976b6 2028 return FALSE;
277d1b5e 2029 }
1725a96e 2030
277d1b5e
ILT
2031 pei_section_data (obfd, osec)->virt_size =
2032 pei_section_data (ibfd, isec)->virt_size;
5933bdc9 2033 pei_section_data (obfd, osec)->pe_flags =
6fa957a9 2034 pei_section_data (ibfd, isec)->pe_flags;
277d1b5e
ILT
2035 }
2036
b34976b6 2037 return TRUE;
277d1b5e 2038}
7d2b58d6
ILT
2039
2040void
7920ce38 2041_bfd_XX_get_symbol_info (bfd * abfd, asymbol *symbol, symbol_info *ret)
7d2b58d6
ILT
2042{
2043 coff_get_symbol_info (abfd, symbol, ret);
7d2b58d6 2044}
2fbadf2c
ILT
2045
2046/* Handle the .idata section and other things that need symbol table
2047 access. */
2048
b34976b6 2049bfd_boolean
7920ce38 2050_bfd_XXi_final_link_postscript (bfd * abfd, struct coff_final_link_info *pfinfo)
2fbadf2c
ILT
2051{
2052 struct coff_link_hash_entry *h1;
2053 struct bfd_link_info *info = pfinfo->info;
4e22f78d 2054 bfd_boolean result = TRUE;
2fbadf2c
ILT
2055
2056 /* There are a few fields that need to be filled in now while we
2057 have symbol table access.
2058
2059 The .idata subsections aren't directly available as sections, but
2060 they are in the symbol table, so get them from there. */
2061
2062 /* The import directory. This is the address of .idata$2, with size
2063 of .idata$2 + .idata$3. */
2064 h1 = coff_link_hash_lookup (coff_hash_table (info),
b34976b6 2065 ".idata$2", FALSE, FALSE, TRUE);
2fbadf2c
ILT
2066 if (h1 != NULL)
2067 {
4e22f78d
NC
2068 /* PR ld/2729: We cannot rely upon all the output sections having been
2069 created properly, so check before referencing them. Issue a warning
2070 message for any sections tht could not be found. */
2071 if (h1->root.u.def.section != NULL
2072 && h1->root.u.def.section->output_section != NULL)
6c73cbb1 2073 pe_data (abfd)->pe_opthdr.DataDirectory[PE_IMPORT_TABLE].VirtualAddress =
4e22f78d
NC
2074 (h1->root.u.def.value
2075 + h1->root.u.def.section->output_section->vma
2076 + h1->root.u.def.section->output_offset);
2077 else
2078 {
2079 _bfd_error_handler
2080 (_("%B: unable to fill in DataDictionary[1] because .idata$2 is missing"),
2081 abfd);
2082 result = FALSE;
2083 }
2084
2fbadf2c 2085 h1 = coff_link_hash_lookup (coff_hash_table (info),
b34976b6 2086 ".idata$4", FALSE, FALSE, TRUE);
4e22f78d
NC
2087 if (h1 != NULL
2088 && h1->root.u.def.section != NULL
2089 && h1->root.u.def.section->output_section != NULL)
6c73cbb1 2090 pe_data (abfd)->pe_opthdr.DataDirectory[PE_IMPORT_TABLE].Size =
4e22f78d
NC
2091 ((h1->root.u.def.value
2092 + h1->root.u.def.section->output_section->vma
2093 + h1->root.u.def.section->output_offset)
6c73cbb1 2094 - pe_data (abfd)->pe_opthdr.DataDirectory[PE_IMPORT_TABLE].VirtualAddress);
4e22f78d
NC
2095 else
2096 {
2097 _bfd_error_handler
2098 (_("%B: unable to fill in DataDictionary[1] because .idata$4 is missing"),
2099 abfd);
2100 result = FALSE;
2101 }
2fbadf2c
ILT
2102
2103 /* The import address table. This is the size/address of
2104 .idata$5. */
2105 h1 = coff_link_hash_lookup (coff_hash_table (info),
b34976b6 2106 ".idata$5", FALSE, FALSE, TRUE);
4e22f78d
NC
2107 if (h1 != NULL
2108 && h1->root.u.def.section != NULL
2109 && h1->root.u.def.section->output_section != NULL)
6c73cbb1 2110 pe_data (abfd)->pe_opthdr.DataDirectory[PE_IMPORT_ADDRESS_TABLE].VirtualAddress =
4e22f78d
NC
2111 (h1->root.u.def.value
2112 + h1->root.u.def.section->output_section->vma
2113 + h1->root.u.def.section->output_offset);
2114 else
2115 {
2116 _bfd_error_handler
2117 (_("%B: unable to fill in DataDictionary[12] because .idata$5 is missing"),
2118 abfd);
2119 result = FALSE;
2120 }
2121
2fbadf2c 2122 h1 = coff_link_hash_lookup (coff_hash_table (info),
b34976b6 2123 ".idata$6", FALSE, FALSE, TRUE);
4e22f78d
NC
2124 if (h1 != NULL
2125 && h1->root.u.def.section != NULL
2126 && h1->root.u.def.section->output_section != NULL)
6c73cbb1 2127 pe_data (abfd)->pe_opthdr.DataDirectory[PE_IMPORT_ADDRESS_TABLE].Size =
4e22f78d
NC
2128 ((h1->root.u.def.value
2129 + h1->root.u.def.section->output_section->vma
2130 + h1->root.u.def.section->output_offset)
6c73cbb1 2131 - pe_data (abfd)->pe_opthdr.DataDirectory[PE_IMPORT_ADDRESS_TABLE].VirtualAddress);
4e22f78d
NC
2132 else
2133 {
2134 _bfd_error_handler
6c73cbb1 2135 (_("%B: unable to fill in DataDictionary[PE_IMPORT_ADDRESS_TABLE (12)] because .idata$6 is missing"),
4e22f78d
NC
2136 abfd);
2137 result = FALSE;
2138 }
2fbadf2c 2139 }
ca6dee30
NC
2140
2141 h1 = coff_link_hash_lookup (coff_hash_table (info),
2142 "__tls_used", FALSE, FALSE, TRUE);
2143 if (h1 != NULL)
2144 {
4e22f78d
NC
2145 if (h1->root.u.def.section != NULL
2146 && h1->root.u.def.section->output_section != NULL)
6c73cbb1 2147 pe_data (abfd)->pe_opthdr.DataDirectory[PE_TLS_TABLE].VirtualAddress =
4e22f78d
NC
2148 (h1->root.u.def.value
2149 + h1->root.u.def.section->output_section->vma
2150 + h1->root.u.def.section->output_offset
2151 - pe_data (abfd)->pe_opthdr.ImageBase);
2152 else
2153 {
2154 _bfd_error_handler
2155 (_("%B: unable to fill in DataDictionary[9] because __tls_used is missing"),
2156 abfd);
2157 result = FALSE;
2158 }
2159
6c73cbb1 2160 pe_data (abfd)->pe_opthdr.DataDirectory[PE_TLS_TABLE].Size = 0x18;
ca6dee30
NC
2161 }
2162
2fbadf2c
ILT
2163 /* If we couldn't find idata$2, we either have an excessively
2164 trivial program or are in DEEP trouble; we have to assume trivial
2165 program.... */
4e22f78d 2166 return result;
2fbadf2c 2167}
This page took 0.565358 seconds and 4 git commands to generate.