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