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