1 /* Support for the generic parts of PE/PEI; the common executable parts.
2 Copyright 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
3 2005, 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
4 Written by Cygnus Solutions.
6 This file is part of BFD, the Binary File Descriptor library.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
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.
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
20 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 MA 02110-1301, USA. */
24 /* Most of this hacked by Steve Chamberlain <sac@cygnus.com>.
26 PE/PEI rearrangement (and code added): Donn Terry
27 Softway Systems, Inc. */
29 /* Hey look, some documentation [and in a place you expect to find it]!
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.
36 "Peering Inside the PE: A Tour of the Win32 Portable Executable
37 File Format", MSJ 1994, Volume 9.
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
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.
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
55 wasting too much time. */
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+. */
64 #include "coff/internal.h"
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. */
72 #if defined COFF_WITH_pex64
73 # include "coff/x86_64.h"
74 #elif defined COFF_WITH_pep
75 # include "coff/ia64.h"
77 # include "coff/i386.h"
84 #if defined COFF_WITH_pep || defined COFF_WITH_pex64
86 # define AOUTSZ PEPAOUTSZ
87 # define PEAOUTHDR PEPAOUTHDR
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. */
97 _bfd_XXi_swap_sym_in (bfd * abfd, void * ext1, void * in1)
99 SYMENT *ext = (SYMENT *) ext1;
100 struct internal_syment *in = (struct internal_syment *) in1;
102 if (ext->e.e_name[0] == 0)
104 in->_n._n_n._n_zeroes = 0;
105 in->_n._n_n._n_offset = H_GET_32 (abfd, ext->e.e.e_offset);
108 memcpy (in->_n._n_name, ext->e.e_name, SYMNMLEN);
110 in->n_value = H_GET_32 (abfd, ext->e_value);
111 in->n_scnum = H_GET_16 (abfd, ext->e_scnum);
113 if (sizeof (ext->e_type) == 2)
114 in->n_type = H_GET_16 (abfd, ext->e_type);
116 in->n_type = H_GET_32 (abfd, ext->e_type);
118 in->n_sclass = H_GET_8 (abfd, ext->e_sclass);
119 in->n_numaux = H_GET_8 (abfd, ext->e_numaux);
121 #ifndef STRICT_PE_FORMAT
122 /* This is for Gnu-created DLLs. */
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)
134 /* Create synthetic empty sections as needed. DJ */
135 if (in->n_scnum == 0)
139 for (sec = abfd->sections; sec; sec = sec->next)
141 if (strcmp (sec->name, in->n_name) == 0)
143 in->n_scnum = sec->target_index;
149 if (in->n_scnum == 0)
151 int unused_section_number = 0;
156 for (sec = abfd->sections; sec; sec = sec->next)
157 if (unused_section_number <= sec->target_index)
158 unused_section_number = sec->target_index + 1;
160 name = bfd_alloc (abfd, (bfd_size_type) strlen (in->n_name) + 10);
163 strcpy (name, in->n_name);
164 flags = SEC_HAS_CONTENTS | SEC_ALLOC | SEC_DATA | SEC_LOAD;
165 sec = bfd_make_section_anyway_with_flags (abfd, name, flags);
171 sec->rel_filepos = 0;
172 sec->reloc_count = 0;
173 sec->line_filepos = 0;
174 sec->lineno_count = 0;
175 sec->userdata = NULL;
177 sec->alignment_power = 2;
179 sec->target_index = unused_section_number;
181 in->n_scnum = unused_section_number;
183 in->n_sclass = C_STAT;
187 #ifdef coff_swap_sym_in_hook
188 /* This won't work in peigen.c, but since it's for PPC PE, it's not
190 coff_swap_sym_in_hook (abfd, ext1, in1);
195 _bfd_XXi_swap_sym_out (bfd * abfd, void * inp, void * extp)
197 struct internal_syment *in = (struct internal_syment *) inp;
198 SYMENT *ext = (SYMENT *) extp;
200 if (in->_n._n_name[0] == 0)
202 H_PUT_32 (abfd, 0, ext->e.e.e_zeroes);
203 H_PUT_32 (abfd, in->_n._n_n._n_offset, ext->e.e.e_offset);
206 memcpy (ext->e.e_name, in->_n._n_name, SYMNMLEN);
208 H_PUT_32 (abfd, in->n_value, ext->e_value);
209 H_PUT_16 (abfd, in->n_scnum, ext->e_scnum);
211 if (sizeof (ext->e_type) == 2)
212 H_PUT_16 (abfd, in->n_type, ext->e_type);
214 H_PUT_32 (abfd, in->n_type, ext->e_type);
216 H_PUT_8 (abfd, in->n_sclass, ext->e_sclass);
217 H_PUT_8 (abfd, in->n_numaux, ext->e_numaux);
223 _bfd_XXi_swap_aux_in (bfd * abfd,
227 int indx ATTRIBUTE_UNUSED,
228 int numaux ATTRIBUTE_UNUSED,
231 AUXENT *ext = (AUXENT *) ext1;
232 union internal_auxent *in = (union internal_auxent *) in1;
237 if (ext->x_file.x_fname[0] == 0)
239 in->x_file.x_n.x_zeroes = 0;
240 in->x_file.x_n.x_offset = H_GET_32 (abfd, ext->x_file.x_n.x_offset);
243 memcpy (in->x_file.x_fname, ext->x_file.x_fname, FILNMLEN);
251 in->x_scn.x_scnlen = GET_SCN_SCNLEN (abfd, ext);
252 in->x_scn.x_nreloc = GET_SCN_NRELOC (abfd, ext);
253 in->x_scn.x_nlinno = GET_SCN_NLINNO (abfd, ext);
254 in->x_scn.x_checksum = H_GET_32 (abfd, ext->x_scn.x_checksum);
255 in->x_scn.x_associated = H_GET_16 (abfd, ext->x_scn.x_associated);
256 in->x_scn.x_comdat = H_GET_8 (abfd, ext->x_scn.x_comdat);
262 in->x_sym.x_tagndx.l = H_GET_32 (abfd, ext->x_sym.x_tagndx);
263 in->x_sym.x_tvndx = H_GET_16 (abfd, ext->x_sym.x_tvndx);
265 if (class == C_BLOCK || class == C_FCN || ISFCN (type) || ISTAG (class))
267 in->x_sym.x_fcnary.x_fcn.x_lnnoptr = GET_FCN_LNNOPTR (abfd, ext);
268 in->x_sym.x_fcnary.x_fcn.x_endndx.l = GET_FCN_ENDNDX (abfd, ext);
272 in->x_sym.x_fcnary.x_ary.x_dimen[0] =
273 H_GET_16 (abfd, ext->x_sym.x_fcnary.x_ary.x_dimen[0]);
274 in->x_sym.x_fcnary.x_ary.x_dimen[1] =
275 H_GET_16 (abfd, ext->x_sym.x_fcnary.x_ary.x_dimen[1]);
276 in->x_sym.x_fcnary.x_ary.x_dimen[2] =
277 H_GET_16 (abfd, ext->x_sym.x_fcnary.x_ary.x_dimen[2]);
278 in->x_sym.x_fcnary.x_ary.x_dimen[3] =
279 H_GET_16 (abfd, ext->x_sym.x_fcnary.x_ary.x_dimen[3]);
284 in->x_sym.x_misc.x_fsize = H_GET_32 (abfd, ext->x_sym.x_misc.x_fsize);
288 in->x_sym.x_misc.x_lnsz.x_lnno = GET_LNSZ_LNNO (abfd, ext);
289 in->x_sym.x_misc.x_lnsz.x_size = GET_LNSZ_SIZE (abfd, ext);
294 _bfd_XXi_swap_aux_out (bfd * abfd,
298 int indx ATTRIBUTE_UNUSED,
299 int numaux ATTRIBUTE_UNUSED,
302 union internal_auxent *in = (union internal_auxent *) inp;
303 AUXENT *ext = (AUXENT *) extp;
305 memset (ext, 0, AUXESZ);
310 if (in->x_file.x_fname[0] == 0)
312 H_PUT_32 (abfd, 0, ext->x_file.x_n.x_zeroes);
313 H_PUT_32 (abfd, in->x_file.x_n.x_offset, ext->x_file.x_n.x_offset);
316 memcpy (ext->x_file.x_fname, in->x_file.x_fname, FILNMLEN);
325 PUT_SCN_SCNLEN (abfd, in->x_scn.x_scnlen, ext);
326 PUT_SCN_NRELOC (abfd, in->x_scn.x_nreloc, ext);
327 PUT_SCN_NLINNO (abfd, in->x_scn.x_nlinno, ext);
328 H_PUT_32 (abfd, in->x_scn.x_checksum, ext->x_scn.x_checksum);
329 H_PUT_16 (abfd, in->x_scn.x_associated, ext->x_scn.x_associated);
330 H_PUT_8 (abfd, in->x_scn.x_comdat, ext->x_scn.x_comdat);
336 H_PUT_32 (abfd, in->x_sym.x_tagndx.l, ext->x_sym.x_tagndx);
337 H_PUT_16 (abfd, in->x_sym.x_tvndx, ext->x_sym.x_tvndx);
339 if (class == C_BLOCK || class == C_FCN || ISFCN (type) || ISTAG (class))
341 PUT_FCN_LNNOPTR (abfd, in->x_sym.x_fcnary.x_fcn.x_lnnoptr, ext);
342 PUT_FCN_ENDNDX (abfd, in->x_sym.x_fcnary.x_fcn.x_endndx.l, ext);
346 H_PUT_16 (abfd, in->x_sym.x_fcnary.x_ary.x_dimen[0],
347 ext->x_sym.x_fcnary.x_ary.x_dimen[0]);
348 H_PUT_16 (abfd, in->x_sym.x_fcnary.x_ary.x_dimen[1],
349 ext->x_sym.x_fcnary.x_ary.x_dimen[1]);
350 H_PUT_16 (abfd, in->x_sym.x_fcnary.x_ary.x_dimen[2],
351 ext->x_sym.x_fcnary.x_ary.x_dimen[2]);
352 H_PUT_16 (abfd, in->x_sym.x_fcnary.x_ary.x_dimen[3],
353 ext->x_sym.x_fcnary.x_ary.x_dimen[3]);
357 H_PUT_32 (abfd, in->x_sym.x_misc.x_fsize, ext->x_sym.x_misc.x_fsize);
360 PUT_LNSZ_LNNO (abfd, in->x_sym.x_misc.x_lnsz.x_lnno, ext);
361 PUT_LNSZ_SIZE (abfd, in->x_sym.x_misc.x_lnsz.x_size, ext);
368 _bfd_XXi_swap_lineno_in (bfd * abfd, void * ext1, void * in1)
370 LINENO *ext = (LINENO *) ext1;
371 struct internal_lineno *in = (struct internal_lineno *) in1;
373 in->l_addr.l_symndx = H_GET_32 (abfd, ext->l_addr.l_symndx);
374 in->l_lnno = GET_LINENO_LNNO (abfd, ext);
378 _bfd_XXi_swap_lineno_out (bfd * abfd, void * inp, void * outp)
380 struct internal_lineno *in = (struct internal_lineno *) inp;
381 struct external_lineno *ext = (struct external_lineno *) outp;
382 H_PUT_32 (abfd, in->l_addr.l_symndx, ext->l_addr.l_symndx);
384 PUT_LINENO_LNNO (abfd, in->l_lnno, ext);
389 _bfd_XXi_swap_aouthdr_in (bfd * abfd,
393 PEAOUTHDR * src = (PEAOUTHDR *) aouthdr_ext1;
394 AOUTHDR * aouthdr_ext = (AOUTHDR *) aouthdr_ext1;
395 struct internal_aouthdr *aouthdr_int
396 = (struct internal_aouthdr *) aouthdr_int1;
397 struct internal_extra_pe_aouthdr *a = &aouthdr_int->pe;
399 aouthdr_int->magic = H_GET_16 (abfd, aouthdr_ext->magic);
400 aouthdr_int->vstamp = H_GET_16 (abfd, aouthdr_ext->vstamp);
401 aouthdr_int->tsize = GET_AOUTHDR_TSIZE (abfd, aouthdr_ext->tsize);
402 aouthdr_int->dsize = GET_AOUTHDR_DSIZE (abfd, aouthdr_ext->dsize);
403 aouthdr_int->bsize = GET_AOUTHDR_BSIZE (abfd, aouthdr_ext->bsize);
404 aouthdr_int->entry = GET_AOUTHDR_ENTRY (abfd, aouthdr_ext->entry);
405 aouthdr_int->text_start =
406 GET_AOUTHDR_TEXT_START (abfd, aouthdr_ext->text_start);
407 #if !defined(COFF_WITH_pep) && !defined(COFF_WITH_pex64)
408 /* PE32+ does not have data_start member! */
409 aouthdr_int->data_start =
410 GET_AOUTHDR_DATA_START (abfd, aouthdr_ext->data_start);
411 a->BaseOfData = aouthdr_int->data_start;
414 a->Magic = aouthdr_int->magic;
415 a->MajorLinkerVersion = H_GET_8 (abfd, aouthdr_ext->vstamp);
416 a->MinorLinkerVersion = H_GET_8 (abfd, aouthdr_ext->vstamp + 1);
417 a->SizeOfCode = aouthdr_int->tsize ;
418 a->SizeOfInitializedData = aouthdr_int->dsize ;
419 a->SizeOfUninitializedData = aouthdr_int->bsize ;
420 a->AddressOfEntryPoint = aouthdr_int->entry;
421 a->BaseOfCode = aouthdr_int->text_start;
422 a->ImageBase = GET_OPTHDR_IMAGE_BASE (abfd, src->ImageBase);
423 a->SectionAlignment = H_GET_32 (abfd, src->SectionAlignment);
424 a->FileAlignment = H_GET_32 (abfd, src->FileAlignment);
425 a->MajorOperatingSystemVersion =
426 H_GET_16 (abfd, src->MajorOperatingSystemVersion);
427 a->MinorOperatingSystemVersion =
428 H_GET_16 (abfd, src->MinorOperatingSystemVersion);
429 a->MajorImageVersion = H_GET_16 (abfd, src->MajorImageVersion);
430 a->MinorImageVersion = H_GET_16 (abfd, src->MinorImageVersion);
431 a->MajorSubsystemVersion = H_GET_16 (abfd, src->MajorSubsystemVersion);
432 a->MinorSubsystemVersion = H_GET_16 (abfd, src->MinorSubsystemVersion);
433 a->Reserved1 = H_GET_32 (abfd, src->Reserved1);
434 a->SizeOfImage = H_GET_32 (abfd, src->SizeOfImage);
435 a->SizeOfHeaders = H_GET_32 (abfd, src->SizeOfHeaders);
436 a->CheckSum = H_GET_32 (abfd, src->CheckSum);
437 a->Subsystem = H_GET_16 (abfd, src->Subsystem);
438 a->DllCharacteristics = H_GET_16 (abfd, src->DllCharacteristics);
439 a->SizeOfStackReserve =
440 GET_OPTHDR_SIZE_OF_STACK_RESERVE (abfd, src->SizeOfStackReserve);
441 a->SizeOfStackCommit =
442 GET_OPTHDR_SIZE_OF_STACK_COMMIT (abfd, src->SizeOfStackCommit);
443 a->SizeOfHeapReserve =
444 GET_OPTHDR_SIZE_OF_HEAP_RESERVE (abfd, src->SizeOfHeapReserve);
445 a->SizeOfHeapCommit =
446 GET_OPTHDR_SIZE_OF_HEAP_COMMIT (abfd, src->SizeOfHeapCommit);
447 a->LoaderFlags = H_GET_32 (abfd, src->LoaderFlags);
448 a->NumberOfRvaAndSizes = H_GET_32 (abfd, src->NumberOfRvaAndSizes);
453 for (idx = 0; idx < 16; idx++)
455 /* If data directory is empty, rva also should be 0. */
457 H_GET_32 (abfd, src->DataDirectory[idx][1]);
459 a->DataDirectory[idx].Size = size;
462 a->DataDirectory[idx].VirtualAddress =
463 H_GET_32 (abfd, src->DataDirectory[idx][0]);
465 a->DataDirectory[idx].VirtualAddress = 0;
469 if (aouthdr_int->entry)
471 aouthdr_int->entry += a->ImageBase;
472 #if !defined(COFF_WITH_pep) && !defined(COFF_WITH_pex64)
473 aouthdr_int->entry &= 0xffffffff;
477 if (aouthdr_int->tsize)
479 aouthdr_int->text_start += a->ImageBase;
480 #if !defined(COFF_WITH_pep) && !defined(COFF_WITH_pex64)
481 aouthdr_int->text_start &= 0xffffffff;
485 #if !defined(COFF_WITH_pep) && !defined(COFF_WITH_pex64)
486 /* PE32+ does not have data_start member! */
487 if (aouthdr_int->dsize)
489 aouthdr_int->data_start += a->ImageBase;
490 aouthdr_int->data_start &= 0xffffffff;
495 /* These three fields are normally set up by ppc_relocate_section.
496 In the case of reading a file in, we can pick them up from the
498 first_thunk_address = a->DataDirectory[PE_IMPORT_ADDRESS_TABLE].VirtualAddress;
499 thunk_size = a->DataDirectory[PE_IMPORT_ADDRESS_TABLE].Size;
500 import_table_size = a->DataDirectory[PE_IMPORT_TABLE].Size;
504 /* A support function for below. */
507 add_data_entry (bfd * abfd,
508 struct internal_extra_pe_aouthdr *aout,
513 asection *sec = bfd_get_section_by_name (abfd, name);
515 /* Add import directory information if it exists. */
517 && (coff_section_data (abfd, sec) != NULL)
518 && (pei_section_data (abfd, sec) != NULL))
520 /* If data directory is empty, rva also should be 0. */
521 int size = pei_section_data (abfd, sec)->virt_size;
522 aout->DataDirectory[idx].Size = size;
526 aout->DataDirectory[idx].VirtualAddress =
527 (sec->vma - base) & 0xffffffff;
528 sec->flags |= SEC_DATA;
534 _bfd_XXi_swap_aouthdr_out (bfd * abfd, void * in, void * out)
536 struct internal_aouthdr *aouthdr_in = (struct internal_aouthdr *) in;
537 pe_data_type *pe = pe_data (abfd);
538 struct internal_extra_pe_aouthdr *extra = &pe->pe_opthdr;
539 PEAOUTHDR *aouthdr_out = (PEAOUTHDR *) out;
541 IMAGE_DATA_DIRECTORY idata2, idata5, tls;
543 if (pe->force_minimum_alignment)
545 if (!extra->FileAlignment)
546 extra->FileAlignment = PE_DEF_FILE_ALIGNMENT;
547 if (!extra->SectionAlignment)
548 extra->SectionAlignment = PE_DEF_SECTION_ALIGNMENT;
551 if (extra->Subsystem == IMAGE_SUBSYSTEM_UNKNOWN)
552 extra->Subsystem = pe->target_subsystem;
554 sa = extra->SectionAlignment;
555 fa = extra->FileAlignment;
556 ib = extra->ImageBase;
558 idata2 = pe->pe_opthdr.DataDirectory[PE_IMPORT_TABLE];
559 idata5 = pe->pe_opthdr.DataDirectory[PE_IMPORT_ADDRESS_TABLE];
560 tls = pe->pe_opthdr.DataDirectory[PE_TLS_TABLE];
562 if (aouthdr_in->tsize)
564 aouthdr_in->text_start -= ib;
565 #if !defined(COFF_WITH_pep) && !defined(COFF_WITH_pex64)
566 aouthdr_in->text_start &= 0xffffffff;
570 if (aouthdr_in->dsize)
572 aouthdr_in->data_start -= ib;
573 #if !defined(COFF_WITH_pep) && !defined(COFF_WITH_pex64)
574 aouthdr_in->data_start &= 0xffffffff;
578 if (aouthdr_in->entry)
580 aouthdr_in->entry -= ib;
581 #if !defined(COFF_WITH_pep) && !defined(COFF_WITH_pex64)
582 aouthdr_in->entry &= 0xffffffff;
586 #define FA(x) (((x) + fa -1 ) & (- fa))
587 #define SA(x) (((x) + sa -1 ) & (- sa))
589 /* We like to have the sizes aligned. */
590 aouthdr_in->bsize = FA (aouthdr_in->bsize);
592 extra->NumberOfRvaAndSizes = IMAGE_NUMBEROF_DIRECTORY_ENTRIES;
594 /* First null out all data directory entries. */
595 memset (extra->DataDirectory, 0, sizeof (extra->DataDirectory));
597 add_data_entry (abfd, extra, 0, ".edata", ib);
598 add_data_entry (abfd, extra, 2, ".rsrc", ib);
599 add_data_entry (abfd, extra, 3, ".pdata", ib);
601 /* In theory we do not need to call add_data_entry for .idata$2 or
602 .idata$5. It will be done in bfd_coff_final_link where all the
603 required information is available. If however, we are not going
604 to perform a final link, eg because we have been invoked by objcopy
605 or strip, then we need to make sure that these Data Directory
606 entries are initialised properly.
608 So - we copy the input values into the output values, and then, if
609 a final link is going to be performed, it can overwrite them. */
610 extra->DataDirectory[PE_IMPORT_TABLE] = idata2;
611 extra->DataDirectory[PE_IMPORT_ADDRESS_TABLE] = idata5;
612 extra->DataDirectory[PE_TLS_TABLE] = tls;
614 if (extra->DataDirectory[PE_IMPORT_TABLE].VirtualAddress == 0)
615 /* Until other .idata fixes are made (pending patch), the entry for
616 .idata is needed for backwards compatibility. FIXME. */
617 add_data_entry (abfd, extra, 1, ".idata", ib);
619 /* For some reason, the virtual size (which is what's set by
620 add_data_entry) for .reloc is not the same as the size recorded
621 in this slot by MSVC; it doesn't seem to cause problems (so far),
622 but since it's the best we've got, use it. It does do the right
624 if (pe->has_reloc_section)
625 add_data_entry (abfd, extra, 5, ".reloc", ib);
634 for (sec = abfd->sections; sec; sec = sec->next)
636 int rounded = FA (sec->size);
638 /* The first non-zero section filepos is the header size.
639 Sections without contents will have a filepos of 0. */
641 hsize = sec->filepos;
642 if (sec->flags & SEC_DATA)
644 if (sec->flags & SEC_CODE)
646 /* The image size is the total VIRTUAL size (which is what is
647 in the virt_size field). Files have been seen (from MSVC
648 5.0 link.exe) where the file size of the .data segment is
649 quite small compared to the virtual size. Without this
650 fix, strip munges the file.
652 FIXME: We need to handle holes between sections, which may
653 happpen when we covert from another format. We just use
654 the virtual address and virtual size of the last section
655 for the image size. */
656 if (coff_section_data (abfd, sec) != NULL
657 && pei_section_data (abfd, sec) != NULL)
658 isize = (sec->vma - extra->ImageBase
659 + SA (FA (pei_section_data (abfd, sec)->virt_size)));
662 aouthdr_in->dsize = dsize;
663 aouthdr_in->tsize = tsize;
664 extra->SizeOfHeaders = hsize;
665 extra->SizeOfImage = isize;
668 H_PUT_16 (abfd, aouthdr_in->magic, aouthdr_out->standard.magic);
670 #define LINKER_VERSION 256 /* That is, 2.56 */
672 /* This piece of magic sets the "linker version" field to
674 H_PUT_16 (abfd, (LINKER_VERSION / 100 + (LINKER_VERSION % 100) * 256),
675 aouthdr_out->standard.vstamp);
677 PUT_AOUTHDR_TSIZE (abfd, aouthdr_in->tsize, aouthdr_out->standard.tsize);
678 PUT_AOUTHDR_DSIZE (abfd, aouthdr_in->dsize, aouthdr_out->standard.dsize);
679 PUT_AOUTHDR_BSIZE (abfd, aouthdr_in->bsize, aouthdr_out->standard.bsize);
680 PUT_AOUTHDR_ENTRY (abfd, aouthdr_in->entry, aouthdr_out->standard.entry);
681 PUT_AOUTHDR_TEXT_START (abfd, aouthdr_in->text_start,
682 aouthdr_out->standard.text_start);
684 #if !defined(COFF_WITH_pep) && !defined(COFF_WITH_pex64)
685 /* PE32+ does not have data_start member! */
686 PUT_AOUTHDR_DATA_START (abfd, aouthdr_in->data_start,
687 aouthdr_out->standard.data_start);
690 PUT_OPTHDR_IMAGE_BASE (abfd, extra->ImageBase, aouthdr_out->ImageBase);
691 H_PUT_32 (abfd, extra->SectionAlignment, aouthdr_out->SectionAlignment);
692 H_PUT_32 (abfd, extra->FileAlignment, aouthdr_out->FileAlignment);
693 H_PUT_16 (abfd, extra->MajorOperatingSystemVersion,
694 aouthdr_out->MajorOperatingSystemVersion);
695 H_PUT_16 (abfd, extra->MinorOperatingSystemVersion,
696 aouthdr_out->MinorOperatingSystemVersion);
697 H_PUT_16 (abfd, extra->MajorImageVersion, aouthdr_out->MajorImageVersion);
698 H_PUT_16 (abfd, extra->MinorImageVersion, aouthdr_out->MinorImageVersion);
699 H_PUT_16 (abfd, extra->MajorSubsystemVersion,
700 aouthdr_out->MajorSubsystemVersion);
701 H_PUT_16 (abfd, extra->MinorSubsystemVersion,
702 aouthdr_out->MinorSubsystemVersion);
703 H_PUT_32 (abfd, extra->Reserved1, aouthdr_out->Reserved1);
704 H_PUT_32 (abfd, extra->SizeOfImage, aouthdr_out->SizeOfImage);
705 H_PUT_32 (abfd, extra->SizeOfHeaders, aouthdr_out->SizeOfHeaders);
706 H_PUT_32 (abfd, extra->CheckSum, aouthdr_out->CheckSum);
707 H_PUT_16 (abfd, extra->Subsystem, aouthdr_out->Subsystem);
708 H_PUT_16 (abfd, extra->DllCharacteristics, aouthdr_out->DllCharacteristics);
709 PUT_OPTHDR_SIZE_OF_STACK_RESERVE (abfd, extra->SizeOfStackReserve,
710 aouthdr_out->SizeOfStackReserve);
711 PUT_OPTHDR_SIZE_OF_STACK_COMMIT (abfd, extra->SizeOfStackCommit,
712 aouthdr_out->SizeOfStackCommit);
713 PUT_OPTHDR_SIZE_OF_HEAP_RESERVE (abfd, extra->SizeOfHeapReserve,
714 aouthdr_out->SizeOfHeapReserve);
715 PUT_OPTHDR_SIZE_OF_HEAP_COMMIT (abfd, extra->SizeOfHeapCommit,
716 aouthdr_out->SizeOfHeapCommit);
717 H_PUT_32 (abfd, extra->LoaderFlags, aouthdr_out->LoaderFlags);
718 H_PUT_32 (abfd, extra->NumberOfRvaAndSizes,
719 aouthdr_out->NumberOfRvaAndSizes);
723 for (idx = 0; idx < 16; idx++)
725 H_PUT_32 (abfd, extra->DataDirectory[idx].VirtualAddress,
726 aouthdr_out->DataDirectory[idx][0]);
727 H_PUT_32 (abfd, extra->DataDirectory[idx].Size,
728 aouthdr_out->DataDirectory[idx][1]);
736 _bfd_XXi_only_swap_filehdr_out (bfd * abfd, void * in, void * out)
739 struct internal_filehdr *filehdr_in = (struct internal_filehdr *) in;
740 struct external_PEI_filehdr *filehdr_out = (struct external_PEI_filehdr *) out;
742 if (pe_data (abfd)->has_reloc_section)
743 filehdr_in->f_flags &= ~F_RELFLG;
745 if (pe_data (abfd)->dll)
746 filehdr_in->f_flags |= F_DLL;
748 filehdr_in->pe.e_magic = DOSMAGIC;
749 filehdr_in->pe.e_cblp = 0x90;
750 filehdr_in->pe.e_cp = 0x3;
751 filehdr_in->pe.e_crlc = 0x0;
752 filehdr_in->pe.e_cparhdr = 0x4;
753 filehdr_in->pe.e_minalloc = 0x0;
754 filehdr_in->pe.e_maxalloc = 0xffff;
755 filehdr_in->pe.e_ss = 0x0;
756 filehdr_in->pe.e_sp = 0xb8;
757 filehdr_in->pe.e_csum = 0x0;
758 filehdr_in->pe.e_ip = 0x0;
759 filehdr_in->pe.e_cs = 0x0;
760 filehdr_in->pe.e_lfarlc = 0x40;
761 filehdr_in->pe.e_ovno = 0x0;
763 for (idx = 0; idx < 4; idx++)
764 filehdr_in->pe.e_res[idx] = 0x0;
766 filehdr_in->pe.e_oemid = 0x0;
767 filehdr_in->pe.e_oeminfo = 0x0;
769 for (idx = 0; idx < 10; idx++)
770 filehdr_in->pe.e_res2[idx] = 0x0;
772 filehdr_in->pe.e_lfanew = 0x80;
774 /* This next collection of data are mostly just characters. It
775 appears to be constant within the headers put on NT exes. */
776 filehdr_in->pe.dos_message[0] = 0x0eba1f0e;
777 filehdr_in->pe.dos_message[1] = 0xcd09b400;
778 filehdr_in->pe.dos_message[2] = 0x4c01b821;
779 filehdr_in->pe.dos_message[3] = 0x685421cd;
780 filehdr_in->pe.dos_message[4] = 0x70207369;
781 filehdr_in->pe.dos_message[5] = 0x72676f72;
782 filehdr_in->pe.dos_message[6] = 0x63206d61;
783 filehdr_in->pe.dos_message[7] = 0x6f6e6e61;
784 filehdr_in->pe.dos_message[8] = 0x65622074;
785 filehdr_in->pe.dos_message[9] = 0x6e757220;
786 filehdr_in->pe.dos_message[10] = 0x206e6920;
787 filehdr_in->pe.dos_message[11] = 0x20534f44;
788 filehdr_in->pe.dos_message[12] = 0x65646f6d;
789 filehdr_in->pe.dos_message[13] = 0x0a0d0d2e;
790 filehdr_in->pe.dos_message[14] = 0x24;
791 filehdr_in->pe.dos_message[15] = 0x0;
792 filehdr_in->pe.nt_signature = NT_SIGNATURE;
794 H_PUT_16 (abfd, filehdr_in->f_magic, filehdr_out->f_magic);
795 H_PUT_16 (abfd, filehdr_in->f_nscns, filehdr_out->f_nscns);
797 H_PUT_32 (abfd, time (0), filehdr_out->f_timdat);
798 PUT_FILEHDR_SYMPTR (abfd, filehdr_in->f_symptr,
799 filehdr_out->f_symptr);
800 H_PUT_32 (abfd, filehdr_in->f_nsyms, filehdr_out->f_nsyms);
801 H_PUT_16 (abfd, filehdr_in->f_opthdr, filehdr_out->f_opthdr);
802 H_PUT_16 (abfd, filehdr_in->f_flags, filehdr_out->f_flags);
804 /* Put in extra dos header stuff. This data remains essentially
805 constant, it just has to be tacked on to the beginning of all exes
807 H_PUT_16 (abfd, filehdr_in->pe.e_magic, filehdr_out->e_magic);
808 H_PUT_16 (abfd, filehdr_in->pe.e_cblp, filehdr_out->e_cblp);
809 H_PUT_16 (abfd, filehdr_in->pe.e_cp, filehdr_out->e_cp);
810 H_PUT_16 (abfd, filehdr_in->pe.e_crlc, filehdr_out->e_crlc);
811 H_PUT_16 (abfd, filehdr_in->pe.e_cparhdr, filehdr_out->e_cparhdr);
812 H_PUT_16 (abfd, filehdr_in->pe.e_minalloc, filehdr_out->e_minalloc);
813 H_PUT_16 (abfd, filehdr_in->pe.e_maxalloc, filehdr_out->e_maxalloc);
814 H_PUT_16 (abfd, filehdr_in->pe.e_ss, filehdr_out->e_ss);
815 H_PUT_16 (abfd, filehdr_in->pe.e_sp, filehdr_out->e_sp);
816 H_PUT_16 (abfd, filehdr_in->pe.e_csum, filehdr_out->e_csum);
817 H_PUT_16 (abfd, filehdr_in->pe.e_ip, filehdr_out->e_ip);
818 H_PUT_16 (abfd, filehdr_in->pe.e_cs, filehdr_out->e_cs);
819 H_PUT_16 (abfd, filehdr_in->pe.e_lfarlc, filehdr_out->e_lfarlc);
820 H_PUT_16 (abfd, filehdr_in->pe.e_ovno, filehdr_out->e_ovno);
822 for (idx = 0; idx < 4; idx++)
823 H_PUT_16 (abfd, filehdr_in->pe.e_res[idx], filehdr_out->e_res[idx]);
825 H_PUT_16 (abfd, filehdr_in->pe.e_oemid, filehdr_out->e_oemid);
826 H_PUT_16 (abfd, filehdr_in->pe.e_oeminfo, filehdr_out->e_oeminfo);
828 for (idx = 0; idx < 10; idx++)
829 H_PUT_16 (abfd, filehdr_in->pe.e_res2[idx], filehdr_out->e_res2[idx]);
831 H_PUT_32 (abfd, filehdr_in->pe.e_lfanew, filehdr_out->e_lfanew);
833 for (idx = 0; idx < 16; idx++)
834 H_PUT_32 (abfd, filehdr_in->pe.dos_message[idx],
835 filehdr_out->dos_message[idx]);
837 /* Also put in the NT signature. */
838 H_PUT_32 (abfd, filehdr_in->pe.nt_signature, filehdr_out->nt_signature);
844 _bfd_XX_only_swap_filehdr_out (bfd * abfd, void * in, void * out)
846 struct internal_filehdr *filehdr_in = (struct internal_filehdr *) in;
847 FILHDR *filehdr_out = (FILHDR *) out;
849 H_PUT_16 (abfd, filehdr_in->f_magic, filehdr_out->f_magic);
850 H_PUT_16 (abfd, filehdr_in->f_nscns, filehdr_out->f_nscns);
851 H_PUT_32 (abfd, filehdr_in->f_timdat, filehdr_out->f_timdat);
852 PUT_FILEHDR_SYMPTR (abfd, filehdr_in->f_symptr, filehdr_out->f_symptr);
853 H_PUT_32 (abfd, filehdr_in->f_nsyms, filehdr_out->f_nsyms);
854 H_PUT_16 (abfd, filehdr_in->f_opthdr, filehdr_out->f_opthdr);
855 H_PUT_16 (abfd, filehdr_in->f_flags, filehdr_out->f_flags);
861 _bfd_XXi_swap_scnhdr_out (bfd * abfd, void * in, void * out)
863 struct internal_scnhdr *scnhdr_int = (struct internal_scnhdr *) in;
864 SCNHDR *scnhdr_ext = (SCNHDR *) out;
865 unsigned int ret = SCNHSZ;
869 memcpy (scnhdr_ext->s_name, scnhdr_int->s_name, sizeof (scnhdr_int->s_name));
871 PUT_SCNHDR_VADDR (abfd,
872 ((scnhdr_int->s_vaddr
873 - pe_data (abfd)->pe_opthdr.ImageBase)
875 scnhdr_ext->s_vaddr);
877 /* NT wants the size data to be rounded up to the next
878 NT_FILE_ALIGNMENT, but zero if it has no content (as in .bss,
880 if ((scnhdr_int->s_flags & IMAGE_SCN_CNT_UNINITIALIZED_DATA) != 0)
882 if (bfd_pei_p (abfd))
884 ps = scnhdr_int->s_size;
890 ss = scnhdr_int->s_size;
895 if (bfd_pei_p (abfd))
896 ps = scnhdr_int->s_paddr;
900 ss = scnhdr_int->s_size;
903 PUT_SCNHDR_SIZE (abfd, ss,
906 /* s_paddr in PE is really the virtual size. */
907 PUT_SCNHDR_PADDR (abfd, ps, scnhdr_ext->s_paddr);
909 PUT_SCNHDR_SCNPTR (abfd, scnhdr_int->s_scnptr,
910 scnhdr_ext->s_scnptr);
911 PUT_SCNHDR_RELPTR (abfd, scnhdr_int->s_relptr,
912 scnhdr_ext->s_relptr);
913 PUT_SCNHDR_LNNOPTR (abfd, scnhdr_int->s_lnnoptr,
914 scnhdr_ext->s_lnnoptr);
917 /* Extra flags must be set when dealing with PE. All sections should also
918 have the IMAGE_SCN_MEM_READ (0x40000000) flag set. In addition, the
919 .text section must have IMAGE_SCN_MEM_EXECUTE (0x20000000) and the data
920 sections (.idata, .data, .bss, .CRT) must have IMAGE_SCN_MEM_WRITE set
921 (this is especially important when dealing with the .idata section since
922 the addresses for routines from .dlls must be overwritten). If .reloc
923 section data is ever generated, we must add IMAGE_SCN_MEM_DISCARDABLE
924 (0x02000000). Also, the resource data should also be read and
927 /* FIXME: Alignment is also encoded in this field, at least on PPC and
928 ARM-WINCE. Although - how do we get the original alignment field
933 const char * section_name;
934 unsigned long must_have;
936 pe_required_section_flags;
938 pe_required_section_flags known_sections [] =
940 { ".arch", IMAGE_SCN_MEM_READ | IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_DISCARDABLE | IMAGE_SCN_ALIGN_8BYTES },
941 { ".bss", IMAGE_SCN_MEM_READ | IMAGE_SCN_CNT_UNINITIALIZED_DATA | IMAGE_SCN_MEM_WRITE },
942 { ".data", IMAGE_SCN_MEM_READ | IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_WRITE },
943 { ".edata", IMAGE_SCN_MEM_READ | IMAGE_SCN_CNT_INITIALIZED_DATA },
944 { ".idata", IMAGE_SCN_MEM_READ | IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_WRITE },
945 { ".pdata", IMAGE_SCN_MEM_READ | IMAGE_SCN_CNT_INITIALIZED_DATA },
946 { ".rdata", IMAGE_SCN_MEM_READ | IMAGE_SCN_CNT_INITIALIZED_DATA },
947 { ".reloc", IMAGE_SCN_MEM_READ | IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_DISCARDABLE },
948 { ".rsrc", IMAGE_SCN_MEM_READ | IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_WRITE },
949 { ".text" , IMAGE_SCN_MEM_READ | IMAGE_SCN_CNT_CODE | IMAGE_SCN_MEM_EXECUTE },
950 { ".tls", IMAGE_SCN_MEM_READ | IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_WRITE },
951 { ".xdata", IMAGE_SCN_MEM_READ | IMAGE_SCN_CNT_INITIALIZED_DATA },
955 pe_required_section_flags * p;
957 /* We have defaulted to adding the IMAGE_SCN_MEM_WRITE flag, but now
958 we know exactly what this specific section wants so we remove it
959 and then allow the must_have field to add it back in if necessary.
960 However, we don't remove IMAGE_SCN_MEM_WRITE flag from .text if the
961 default WP_TEXT file flag has been cleared. WP_TEXT may be cleared
962 by ld --enable-auto-import (if auto-import is actually needed),
963 by ld --omagic, or by obcopy --writable-text. */
965 for (p = known_sections; p->section_name; p++)
966 if (strcmp (scnhdr_int->s_name, p->section_name) == 0)
968 if (strcmp (scnhdr_int->s_name, ".text")
969 || (bfd_get_file_flags (abfd) & WP_TEXT))
970 scnhdr_int->s_flags &= ~IMAGE_SCN_MEM_WRITE;
971 scnhdr_int->s_flags |= p->must_have;
975 H_PUT_32 (abfd, scnhdr_int->s_flags, scnhdr_ext->s_flags);
978 if (coff_data (abfd)->link_info
979 && ! coff_data (abfd)->link_info->relocatable
980 && ! coff_data (abfd)->link_info->shared
981 && strcmp (scnhdr_int->s_name, ".text") == 0)
983 /* By inference from looking at MS output, the 32 bit field
984 which is the combination of the number_of_relocs and
985 number_of_linenos is used for the line number count in
986 executables. A 16-bit field won't do for cc1. The MS
987 document says that the number of relocs is zero for
988 executables, but the 17-th bit has been observed to be there.
989 Overflow is not an issue: a 4G-line program will overflow a
990 bunch of other fields long before this! */
991 H_PUT_16 (abfd, (scnhdr_int->s_nlnno & 0xffff), scnhdr_ext->s_nlnno);
992 H_PUT_16 (abfd, (scnhdr_int->s_nlnno >> 16), scnhdr_ext->s_nreloc);
996 if (scnhdr_int->s_nlnno <= 0xffff)
997 H_PUT_16 (abfd, scnhdr_int->s_nlnno, scnhdr_ext->s_nlnno);
1000 (*_bfd_error_handler) (_("%s: line number overflow: 0x%lx > 0xffff"),
1001 bfd_get_filename (abfd),
1002 scnhdr_int->s_nlnno);
1003 bfd_set_error (bfd_error_file_truncated);
1004 H_PUT_16 (abfd, 0xffff, scnhdr_ext->s_nlnno);
1008 /* Although we could encode 0xffff relocs here, we do not, to be
1009 consistent with other parts of bfd. Also it lets us warn, as
1010 we should never see 0xffff here w/o having the overflow flag
1012 if (scnhdr_int->s_nreloc < 0xffff)
1013 H_PUT_16 (abfd, scnhdr_int->s_nreloc, scnhdr_ext->s_nreloc);
1016 /* PE can deal with large #s of relocs, but not here. */
1017 H_PUT_16 (abfd, 0xffff, scnhdr_ext->s_nreloc);
1018 scnhdr_int->s_flags |= IMAGE_SCN_LNK_NRELOC_OVFL;
1019 H_PUT_32 (abfd, scnhdr_int->s_flags, scnhdr_ext->s_flags);
1025 static char * dir_names[IMAGE_NUMBEROF_DIRECTORY_ENTRIES] =
1027 N_("Export Directory [.edata (or where ever we found it)]"),
1028 N_("Import Directory [parts of .idata]"),
1029 N_("Resource Directory [.rsrc]"),
1030 N_("Exception Directory [.pdata]"),
1031 N_("Security Directory"),
1032 N_("Base Relocation Directory [.reloc]"),
1033 N_("Debug Directory"),
1034 N_("Description Directory"),
1035 N_("Special Directory"),
1036 N_("Thread Storage Directory [.tls]"),
1037 N_("Load Configuration Directory"),
1038 N_("Bound Import Directory"),
1039 N_("Import Address Table Directory"),
1040 N_("Delay Import Directory"),
1041 N_("CLR Runtime Header"),
1045 #ifdef POWERPC_LE_PE
1046 /* The code for the PPC really falls in the "architecture dependent"
1047 category. However, it's not clear that anyone will ever care, so
1048 we're ignoring the issue for now; if/when PPC matters, some of this
1049 may need to go into peicode.h, or arguments passed to enable the
1050 PPC- specific code. */
1054 pe_print_idata (bfd * abfd, void * vfile)
1056 FILE *file = (FILE *) vfile;
1061 #ifdef POWERPC_LE_PE
1062 asection *rel_section = bfd_get_section_by_name (abfd, ".reldata");
1065 bfd_size_type datasize = 0;
1066 bfd_size_type dataoff;
1070 pe_data_type *pe = pe_data (abfd);
1071 struct internal_extra_pe_aouthdr *extra = &pe->pe_opthdr;
1075 addr = extra->DataDirectory[PE_IMPORT_TABLE].VirtualAddress;
1077 if (addr == 0 && extra->DataDirectory[PE_IMPORT_TABLE].Size == 0)
1079 /* Maybe the extra header isn't there. Look for the section. */
1080 section = bfd_get_section_by_name (abfd, ".idata");
1081 if (section == NULL)
1084 addr = section->vma;
1085 datasize = section->size;
1091 addr += extra->ImageBase;
1092 for (section = abfd->sections; section != NULL; section = section->next)
1094 datasize = section->size;
1095 if (addr >= section->vma && addr < section->vma + datasize)
1099 if (section == NULL)
1102 _("\nThere is an import table, but the section containing it could not be found\n"));
1107 fprintf (file, _("\nThere is an import table in %s at 0x%lx\n"),
1108 section->name, (unsigned long) addr);
1110 dataoff = addr - section->vma;
1111 datasize -= dataoff;
1113 #ifdef POWERPC_LE_PE
1114 if (rel_section != 0 && rel_section->size != 0)
1116 /* The toc address can be found by taking the starting address,
1117 which on the PPC locates a function descriptor. The
1118 descriptor consists of the function code starting address
1119 followed by the address of the toc. The starting address we
1120 get from the bfd, and the descriptor is supposed to be in the
1121 .reldata section. */
1123 bfd_vma loadable_toc_address;
1124 bfd_vma toc_address;
1125 bfd_vma start_address;
1129 if (!bfd_malloc_and_get_section (abfd, rel_section, &data))
1136 offset = abfd->start_address - rel_section->vma;
1138 if (offset >= rel_section->size || offset + 8 > rel_section->size)
1145 start_address = bfd_get_32 (abfd, data + offset);
1146 loadable_toc_address = bfd_get_32 (abfd, data + offset + 4);
1147 toc_address = loadable_toc_address - 32768;
1150 _("\nFunction descriptor located at the start address: %04lx\n"),
1151 (unsigned long int) (abfd->start_address));
1153 _("\tcode-base %08lx toc (loadable/actual) %08lx/%08lx\n"),
1154 start_address, loadable_toc_address, toc_address);
1161 _("\nNo reldata section! Function descriptor not decoded.\n"));
1166 _("\nThe Import Tables (interpreted %s section contents)\n"),
1170 vma: Hint Time Forward DLL First\n\
1171 Table Stamp Chain Name Thunk\n"));
1173 /* Read the whole section. Some of the fields might be before dataoff. */
1174 if (!bfd_malloc_and_get_section (abfd, section, &data))
1181 adj = section->vma - extra->ImageBase;
1183 /* Print all image import descriptors. */
1184 for (i = 0; i < datasize; i += onaline)
1188 bfd_vma forward_chain;
1190 bfd_vma first_thunk;
1195 /* Print (i + extra->DataDirectory[PE_IMPORT_TABLE].VirtualAddress). */
1196 fprintf (file, " %08lx\t", (unsigned long) (i + adj + dataoff));
1197 hint_addr = bfd_get_32 (abfd, data + i + dataoff);
1198 time_stamp = bfd_get_32 (abfd, data + i + 4 + dataoff);
1199 forward_chain = bfd_get_32 (abfd, data + i + 8 + dataoff);
1200 dll_name = bfd_get_32 (abfd, data + i + 12 + dataoff);
1201 first_thunk = bfd_get_32 (abfd, data + i + 16 + dataoff);
1203 fprintf (file, "%08lx %08lx %08lx %08lx %08lx\n",
1204 (unsigned long) hint_addr,
1205 (unsigned long) time_stamp,
1206 (unsigned long) forward_chain,
1207 (unsigned long) dll_name,
1208 (unsigned long) first_thunk);
1210 if (hint_addr == 0 && first_thunk == 0)
1213 if (dll_name - adj >= section->size)
1216 dll = (char *) data + dll_name - adj;
1217 fprintf (file, _("\n\tDLL Name: %s\n"), dll);
1222 asection *ft_section;
1224 bfd_size_type ft_datasize;
1226 int ft_allocated = 0;
1228 fprintf (file, _("\tvma: Hint/Ord Member-Name Bound-To\n"));
1230 idx = hint_addr - adj;
1232 ft_addr = first_thunk + extra->ImageBase;
1234 ft_idx = first_thunk - adj;
1237 if (first_thunk != hint_addr)
1239 /* Find the section which contains the first thunk. */
1240 for (ft_section = abfd->sections;
1242 ft_section = ft_section->next)
1244 ft_datasize = ft_section->size;
1245 if (ft_addr >= ft_section->vma
1246 && ft_addr < ft_section->vma + ft_datasize)
1250 if (ft_section == NULL)
1253 _("\nThere is a first thunk, but the section containing it could not be found\n"));
1257 /* Now check to see if this section is the same as our current
1258 section. If it is not then we will have to load its data in. */
1259 if (ft_section == section)
1262 ft_idx = first_thunk - adj;
1266 ft_idx = first_thunk - (ft_section->vma - extra->ImageBase);
1267 ft_data = bfd_malloc (datasize);
1268 if (ft_data == NULL)
1271 /* Read datasize bfd_bytes starting at offset ft_idx. */
1272 if (! bfd_get_section_contents
1273 (abfd, ft_section, ft_data, (bfd_vma) ft_idx, datasize))
1284 /* Print HintName vector entries. */
1285 #ifdef COFF_WITH_pex64
1286 for (j = 0; j < datasize; j += 8)
1288 unsigned long member = bfd_get_32 (abfd, data + idx + j);
1289 unsigned long member_high = bfd_get_32 (abfd, data + idx + j + 4);
1291 if (!member && !member_high)
1294 if (member_high & 0x80000000)
1295 fprintf (file, "\t%lx%08lx\t %4lx%08lx <none>",
1296 member_high,member, member_high & 0x7fffffff, member);
1302 ordinal = bfd_get_16 (abfd, data + member - adj);
1303 member_name = (char *) data + member - adj + 2;
1304 fprintf (file, "\t%04lx\t %4d %s",member, ordinal, member_name);
1307 /* If the time stamp is not zero, the import address
1308 table holds actual addresses. */
1311 && first_thunk != hint_addr)
1312 fprintf (file, "\t%04lx",
1313 (unsigned long) bfd_get_32 (abfd, ft_data + ft_idx + j));
1314 fprintf (file, "\n");
1317 for (j = 0; j < datasize; j += 4)
1319 unsigned long member = bfd_get_32 (abfd, data + idx + j);
1321 /* Print single IMAGE_IMPORT_BY_NAME vector. */
1325 if (member & 0x80000000)
1326 fprintf (file, "\t%04lx\t %4lu <none>",
1327 member, member & 0x7fffffff);
1333 ordinal = bfd_get_16 (abfd, data + member - adj);
1334 member_name = (char *) data + member - adj + 2;
1335 fprintf (file, "\t%04lx\t %4d %s",
1336 member, ordinal, member_name);
1339 /* If the time stamp is not zero, the import address
1340 table holds actual addresses. */
1343 && first_thunk != hint_addr)
1344 fprintf (file, "\t%04lx",
1345 (unsigned long) bfd_get_32 (abfd, ft_data + ft_idx + j));
1347 fprintf (file, "\n");
1354 fprintf (file, "\n");
1363 pe_print_edata (bfd * abfd, void * vfile)
1365 FILE *file = (FILE *) vfile;
1368 bfd_size_type datasize = 0;
1369 bfd_size_type dataoff;
1374 long export_flags; /* Reserved - should be zero. */
1378 bfd_vma name; /* RVA - relative to image base. */
1379 long base; /* Ordinal base. */
1380 unsigned long num_functions;/* Number in the export address table. */
1381 unsigned long num_names; /* Number in the name pointer table. */
1382 bfd_vma eat_addr; /* RVA to the export address table. */
1383 bfd_vma npt_addr; /* RVA to the Export Name Pointer Table. */
1384 bfd_vma ot_addr; /* RVA to the Ordinal Table. */
1387 pe_data_type *pe = pe_data (abfd);
1388 struct internal_extra_pe_aouthdr *extra = &pe->pe_opthdr;
1392 addr = extra->DataDirectory[PE_EXPORT_TABLE].VirtualAddress;
1394 if (addr == 0 && extra->DataDirectory[PE_EXPORT_TABLE].Size == 0)
1396 /* Maybe the extra header isn't there. Look for the section. */
1397 section = bfd_get_section_by_name (abfd, ".edata");
1398 if (section == NULL)
1401 addr = section->vma;
1403 datasize = section->size;
1409 addr += extra->ImageBase;
1411 for (section = abfd->sections; section != NULL; section = section->next)
1412 if (addr >= section->vma && addr < section->vma + section->size)
1415 if (section == NULL)
1418 _("\nThere is an export table, but the section containing it could not be found\n"));
1422 dataoff = addr - section->vma;
1423 datasize = extra->DataDirectory[PE_EXPORT_TABLE].Size;
1424 if (datasize > section->size - dataoff)
1427 _("\nThere is an export table in %s, but it does not fit into that section\n"),
1433 fprintf (file, _("\nThere is an export table in %s at 0x%lx\n"),
1434 section->name, (unsigned long) addr);
1436 data = bfd_malloc (datasize);
1440 if (! bfd_get_section_contents (abfd, section, data,
1441 (file_ptr) dataoff, datasize))
1444 /* Go get Export Directory Table. */
1445 edt.export_flags = bfd_get_32 (abfd, data + 0);
1446 edt.time_stamp = bfd_get_32 (abfd, data + 4);
1447 edt.major_ver = bfd_get_16 (abfd, data + 8);
1448 edt.minor_ver = bfd_get_16 (abfd, data + 10);
1449 edt.name = bfd_get_32 (abfd, data + 12);
1450 edt.base = bfd_get_32 (abfd, data + 16);
1451 edt.num_functions = bfd_get_32 (abfd, data + 20);
1452 edt.num_names = bfd_get_32 (abfd, data + 24);
1453 edt.eat_addr = bfd_get_32 (abfd, data + 28);
1454 edt.npt_addr = bfd_get_32 (abfd, data + 32);
1455 edt.ot_addr = bfd_get_32 (abfd, data + 36);
1457 adj = section->vma - extra->ImageBase + dataoff;
1459 /* Dump the EDT first. */
1461 _("\nThe Export Tables (interpreted %s section contents)\n\n"),
1465 _("Export Flags \t\t\t%lx\n"), (unsigned long) edt.export_flags);
1468 _("Time/Date stamp \t\t%lx\n"), (unsigned long) edt.time_stamp);
1471 _("Major/Minor \t\t\t%d/%d\n"), edt.major_ver, edt.minor_ver);
1474 _("Name \t\t\t\t"));
1475 fprintf_vma (file, edt.name);
1477 " %s\n", data + edt.name - adj);
1480 _("Ordinal Base \t\t\t%ld\n"), edt.base);
1486 _("\tExport Address Table \t\t%08lx\n"),
1490 _("\t[Name Pointer/Ordinal] Table\t%08lx\n"), edt.num_names);
1493 _("Table Addresses\n"));
1496 _("\tExport Address Table \t\t"));
1497 fprintf_vma (file, edt.eat_addr);
1498 fprintf (file, "\n");
1501 _("\tName Pointer Table \t\t"));
1502 fprintf_vma (file, edt.npt_addr);
1503 fprintf (file, "\n");
1506 _("\tOrdinal Table \t\t\t"));
1507 fprintf_vma (file, edt.ot_addr);
1508 fprintf (file, "\n");
1510 /* The next table to find is the Export Address Table. It's basically
1511 a list of pointers that either locate a function in this dll, or
1512 forward the call to another dll. Something like:
1517 } export_address_table_entry; */
1520 _("\nExport Address Table -- Ordinal Base %ld\n"),
1523 for (i = 0; i < edt.num_functions; ++i)
1525 bfd_vma eat_member = bfd_get_32 (abfd,
1526 data + edt.eat_addr + (i * 4) - adj);
1527 if (eat_member == 0)
1530 if (eat_member - adj <= datasize)
1532 /* This rva is to a name (forwarding function) in our section. */
1533 /* Should locate a function descriptor. */
1535 "\t[%4ld] +base[%4ld] %04lx %s -- %s\n",
1537 (long) (i + edt.base),
1538 (unsigned long) eat_member,
1540 data + eat_member - adj);
1544 /* Should locate a function descriptor in the reldata section. */
1546 "\t[%4ld] +base[%4ld] %04lx %s\n",
1548 (long) (i + edt.base),
1549 (unsigned long) eat_member,
1554 /* The Export Name Pointer Table is paired with the Export Ordinal Table. */
1555 /* Dump them in parallel for clarity. */
1557 _("\n[Ordinal/Name Pointer] Table\n"));
1559 for (i = 0; i < edt.num_names; ++i)
1561 bfd_vma name_ptr = bfd_get_32 (abfd,
1566 char *name = (char *) data + name_ptr - adj;
1568 bfd_vma ord = bfd_get_16 (abfd,
1573 "\t[%4ld] %s\n", (long) ord, name);
1581 /* This really is architecture dependent. On IA-64, a .pdata entry
1582 consists of three dwords containing relative virtual addresses that
1583 specify the start and end address of the code range the entry
1584 covers and the address of the corresponding unwind info data.
1586 On ARM and SH-4, a compressed PDATA structure is used :
1587 _IMAGE_CE_RUNTIME_FUNCTION_ENTRY, whereas MIPS is documented to use
1588 _IMAGE_ALPHA_RUNTIME_FUNCTION_ENTRY.
1589 See http://msdn2.microsoft.com/en-us/library/ms253988(VS.80).aspx .
1591 This is the version for uncompressed data. */
1594 pe_print_pdata (bfd * abfd, void * vfile)
1596 #if defined(COFF_WITH_pep) && !defined(COFF_WITH_pex64)
1597 # define PDATA_ROW_SIZE (3 * 8)
1599 # define PDATA_ROW_SIZE (5 * 4)
1601 FILE *file = (FILE *) vfile;
1603 asection *section = bfd_get_section_by_name (abfd, ".pdata");
1604 bfd_size_type datasize = 0;
1606 bfd_size_type start, stop;
1607 int onaline = PDATA_ROW_SIZE;
1610 || coff_section_data (abfd, section) == NULL
1611 || pei_section_data (abfd, section) == NULL)
1614 stop = pei_section_data (abfd, section)->virt_size;
1615 if ((stop % onaline) != 0)
1617 _("Warning, .pdata section size (%ld) is not a multiple of %d\n"),
1618 (long) stop, onaline);
1621 _("\nThe Function Table (interpreted .pdata section contents)\n"));
1622 #if defined(COFF_WITH_pep) && !defined(COFF_WITH_pex64)
1624 _(" vma:\t\t\tBegin Address End Address Unwind Info\n"));
1627 vma:\t\tBegin End EH EH PrologEnd Exception\n\
1628 \t\tAddress Address Handler Data Address Mask\n"));
1631 datasize = section->size;
1635 if (! bfd_malloc_and_get_section (abfd, section, &data))
1644 for (i = start; i < stop; i += onaline)
1650 bfd_vma prolog_end_addr;
1653 if (i + PDATA_ROW_SIZE > stop)
1656 begin_addr = GET_PDATA_ENTRY (abfd, data + i );
1657 end_addr = GET_PDATA_ENTRY (abfd, data + i + 4);
1658 eh_handler = GET_PDATA_ENTRY (abfd, data + i + 8);
1659 eh_data = GET_PDATA_ENTRY (abfd, data + i + 12);
1660 prolog_end_addr = GET_PDATA_ENTRY (abfd, data + i + 16);
1662 if (begin_addr == 0 && end_addr == 0 && eh_handler == 0
1663 && eh_data == 0 && prolog_end_addr == 0)
1664 /* We are probably into the padding of the section now. */
1667 em_data = ((eh_handler & 0x1) << 2) | (prolog_end_addr & 0x3);
1668 eh_handler &= ~(bfd_vma) 0x3;
1669 prolog_end_addr &= ~(bfd_vma) 0x3;
1672 fprintf_vma (file, i + section->vma); fputc ('\t', file);
1673 fprintf_vma (file, begin_addr); fputc (' ', file);
1674 fprintf_vma (file, end_addr); fputc (' ', file);
1675 fprintf_vma (file, eh_handler);
1676 #if !defined(COFF_WITH_pep) || defined(COFF_WITH_pex64)
1678 fprintf_vma (file, eh_data); fputc (' ', file);
1679 fprintf_vma (file, prolog_end_addr);
1680 fprintf (file, " %x", em_data);
1683 #ifdef POWERPC_LE_PE
1684 if (eh_handler == 0 && eh_data != 0)
1686 /* Special bits here, although the meaning may be a little
1687 mysterious. The only one I know for sure is 0x03
1690 0x01 Register Save Millicode
1691 0x02 Register Restore Millicode
1692 0x03 Glue Code Sequence. */
1696 fprintf (file, _(" Register save millicode"));
1699 fprintf (file, _(" Register restore millicode"));
1702 fprintf (file, _(" Glue code sequence"));
1709 fprintf (file, "\n");
1715 #undef PDATA_ROW_SIZE
1718 typedef struct sym_cache
1725 slurp_symtab (bfd *abfd, sym_cache *psc)
1727 asymbol ** sy = NULL;
1730 if (!(bfd_get_file_flags (abfd) & HAS_SYMS))
1736 storage = bfd_get_symtab_upper_bound (abfd);
1740 sy = bfd_malloc (storage);
1742 psc->symcount = bfd_canonicalize_symtab (abfd, sy);
1743 if (psc->symcount < 0)
1749 my_symbol_for_address (bfd *abfd, bfd_vma func, sym_cache *psc)
1754 psc->syms = slurp_symtab (abfd, psc);
1756 for (i = 0; i < psc->symcount; i++)
1758 if (psc->syms[i]->section->vma + psc->syms[i]->value == func)
1759 return psc->syms[i]->name;
1766 cleanup_syms (sym_cache *psc)
1773 /* This is the version for "compressed" pdata. */
1776 _bfd_XX_print_ce_compressed_pdata (bfd * abfd, void * vfile)
1778 # define PDATA_ROW_SIZE (2 * 4)
1779 FILE *file = (FILE *) vfile;
1780 bfd_byte *data = NULL;
1781 asection *section = bfd_get_section_by_name (abfd, ".pdata");
1782 bfd_size_type datasize = 0;
1784 bfd_size_type start, stop;
1785 int onaline = PDATA_ROW_SIZE;
1786 struct sym_cache sym_cache = {0, 0} ;
1789 || coff_section_data (abfd, section) == NULL
1790 || pei_section_data (abfd, section) == NULL)
1793 stop = pei_section_data (abfd, section)->virt_size;
1794 if ((stop % onaline) != 0)
1796 _("Warning, .pdata section size (%ld) is not a multiple of %d\n"),
1797 (long) stop, onaline);
1800 _("\nThe Function Table (interpreted .pdata section contents)\n"));
1803 vma:\t\tBegin Prolog Function Flags Exception EH\n\
1804 \t\tAddress Length Length 32b exc Handler Data\n"));
1806 datasize = section->size;
1810 if (! bfd_malloc_and_get_section (abfd, section, &data))
1819 for (i = start; i < stop; i += onaline)
1823 bfd_vma prolog_length, function_length;
1824 int flag32bit, exception_flag;
1825 bfd_byte *tdata = 0;
1828 if (i + PDATA_ROW_SIZE > stop)
1831 begin_addr = GET_PDATA_ENTRY (abfd, data + i );
1832 other_data = GET_PDATA_ENTRY (abfd, data + i + 4);
1834 if (begin_addr == 0 && other_data == 0)
1835 /* We are probably into the padding of the section now. */
1838 prolog_length = (other_data & 0x000000FF);
1839 function_length = (other_data & 0x3FFFFF00) >> 8;
1840 flag32bit = (int)((other_data & 0x40000000) >> 30);
1841 exception_flag = (int)((other_data & 0x80000000) >> 31);
1844 fprintf_vma (file, i + section->vma); fputc ('\t', file);
1845 fprintf_vma (file, begin_addr); fputc (' ', file);
1846 fprintf_vma (file, prolog_length); fputc (' ', file);
1847 fprintf_vma (file, function_length); fputc (' ', file);
1848 fprintf (file, "%2d %2d ", flag32bit, exception_flag);
1850 /* Get the exception handler's address and the data passed from the
1851 .text section. This is really the data that belongs with the .pdata
1852 but got "compressed" out for the ARM and SH4 architectures. */
1853 tsection = bfd_get_section_by_name (abfd, ".text");
1854 if (tsection && coff_section_data (abfd, tsection)
1855 && pei_section_data (abfd, tsection))
1857 if (bfd_malloc_and_get_section (abfd, tsection, & tdata))
1859 int xx = (begin_addr - 8) - tsection->vma;
1861 tdata = bfd_malloc (8);
1862 if (bfd_get_section_contents (abfd, tsection, tdata, (bfd_vma) xx, 8))
1864 bfd_vma eh, eh_data;
1866 eh = bfd_get_32 (abfd, tdata);
1867 eh_data = bfd_get_32 (abfd, tdata + 4);
1868 fprintf (file, "%08x ", (unsigned int) eh);
1869 fprintf (file, "%08x", (unsigned int) eh_data);
1872 const char *s = my_symbol_for_address (abfd, eh, &sym_cache);
1875 fprintf (file, " (%s) ", s);
1887 fprintf (file, "\n");
1892 cleanup_syms (& sym_cache);
1895 #undef PDATA_ROW_SIZE
1898 #ifdef COFF_WITH_pex64
1899 /* The PE+ x64 variant. */
1901 _bfd_pex64_print_pdata (bfd *abfd, void *vfile)
1903 # define PDATA_ROW_SIZE (3 * 4)
1904 FILE *file = (FILE *) vfile;
1905 bfd_byte *data = NULL;
1906 asection *section = bfd_get_section_by_name (abfd, ".pdata");
1907 bfd_size_type datasize = 0;
1909 bfd_size_type start, stop;
1910 int onaline = PDATA_ROW_SIZE;
1911 struct sym_cache sym_cache = {0, 0};
1914 || coff_section_data (abfd, section) == NULL
1915 || pei_section_data (abfd, section) == NULL)
1918 stop = pei_section_data (abfd, section)->virt_size;
1919 if ((stop % onaline) != 0)
1921 _("warning: .pdata section size (%ld) is not a multiple of %d\n"),
1922 (long) stop, onaline);
1925 _("\nThe Function Table (interpreted .pdata section contents)\n"));
1927 fprintf (file, _("vma:\t\t\tBeginAddress\t EndAddress\t UnwindData\n"));
1929 datasize = section->size;
1933 if (!bfd_malloc_and_get_section (abfd, section, &data))
1942 for (i = start; i < stop; i += onaline)
1946 bfd_vma unwind_data_addr;
1948 if (i + PDATA_ROW_SIZE > stop)
1951 begin_addr = bfd_get_32 (abfd, data + i);
1952 end_addr = bfd_get_32 (abfd, data + i + 4);
1953 unwind_data_addr = bfd_get_32 (abfd, data + i + 8);
1955 if (begin_addr == 0 && end_addr == 0 && unwind_data_addr == 0)
1956 /* We are probably into the padding of the section now. */
1960 fprintf_vma (file, i + section->vma);
1961 fprintf (file, ":\t");
1962 fprintf_vma (file, begin_addr);
1964 fprintf_vma (file, end_addr);
1966 fprintf_vma (file, unwind_data_addr);
1968 fprintf (file, "\n");
1973 cleanup_syms (&sym_cache);
1976 #undef PDATA_ROW_SIZE
1980 #define IMAGE_REL_BASED_HIGHADJ 4
1981 static const char * const tbl[] =
1995 "UNKNOWN", /* MUST be last. */
1999 pe_print_reloc (bfd * abfd, void * vfile)
2001 FILE *file = (FILE *) vfile;
2003 asection *section = bfd_get_section_by_name (abfd, ".reloc");
2004 bfd_size_type datasize;
2006 bfd_size_type start, stop;
2008 if (section == NULL)
2011 if (section->size == 0)
2015 _("\n\nPE File Base Relocations (interpreted .reloc section contents)\n"));
2017 datasize = section->size;
2018 if (! bfd_malloc_and_get_section (abfd, section, &data))
2027 stop = section->size;
2029 for (i = start; i < stop;)
2032 bfd_vma virtual_address;
2035 /* The .reloc section is a sequence of blocks, with a header consisting
2036 of two 32 bit quantities, followed by a number of 16 bit entries. */
2037 virtual_address = bfd_get_32 (abfd, data+i);
2038 size = bfd_get_32 (abfd, data+i+4);
2039 number = (size - 8) / 2;
2045 _("\nVirtual Address: %08lx Chunk size %ld (0x%lx) Number of fixups %ld\n"),
2046 (unsigned long) virtual_address, size, (unsigned long) size, number);
2048 for (j = 0; j < number; ++j)
2050 unsigned short e = bfd_get_16 (abfd, data + i + 8 + j * 2);
2051 unsigned int t = (e & 0xF000) >> 12;
2052 int off = e & 0x0FFF;
2054 if (t >= sizeof (tbl) / sizeof (tbl[0]))
2055 t = (sizeof (tbl) / sizeof (tbl[0])) - 1;
2058 _("\treloc %4d offset %4x [%4lx] %s"),
2059 j, off, (unsigned long) (off + virtual_address), tbl[t]);
2061 /* HIGHADJ takes an argument, - the next record *is* the
2062 low 16 bits of addend. */
2063 if (t == IMAGE_REL_BASED_HIGHADJ)
2065 fprintf (file, " (%4x)",
2067 bfd_get_16 (abfd, data + i + 8 + j * 2 + 2)));
2071 fprintf (file, "\n");
2082 /* Print out the program headers. */
2085 _bfd_XX_print_private_bfd_data_common (bfd * abfd, void * vfile)
2087 FILE *file = (FILE *) vfile;
2089 pe_data_type *pe = pe_data (abfd);
2090 struct internal_extra_pe_aouthdr *i = &pe->pe_opthdr;
2091 const char *subsystem_name = NULL;
2094 /* The MS dumpbin program reportedly ands with 0xff0f before
2095 printing the characteristics field. Not sure why. No reason to
2097 fprintf (file, _("\nCharacteristics 0x%x\n"), pe->real_flags);
2099 #define PF(x, y) if (pe->real_flags & x) { fprintf (file, "\t%s\n", y); }
2100 PF (IMAGE_FILE_RELOCS_STRIPPED, "relocations stripped");
2101 PF (IMAGE_FILE_EXECUTABLE_IMAGE, "executable");
2102 PF (IMAGE_FILE_LINE_NUMS_STRIPPED, "line numbers stripped");
2103 PF (IMAGE_FILE_LOCAL_SYMS_STRIPPED, "symbols stripped");
2104 PF (IMAGE_FILE_LARGE_ADDRESS_AWARE, "large address aware");
2105 PF (IMAGE_FILE_BYTES_REVERSED_LO, "little endian");
2106 PF (IMAGE_FILE_32BIT_MACHINE, "32 bit words");
2107 PF (IMAGE_FILE_DEBUG_STRIPPED, "debugging information removed");
2108 PF (IMAGE_FILE_SYSTEM, "system file");
2109 PF (IMAGE_FILE_DLL, "DLL");
2110 PF (IMAGE_FILE_BYTES_REVERSED_HI, "big endian");
2113 /* ctime implies '\n'. */
2115 time_t t = pe->coff.timestamp;
2116 fprintf (file, "\nTime/Date\t\t%s", ctime (&t));
2119 #ifndef IMAGE_NT_OPTIONAL_HDR_MAGIC
2120 # define IMAGE_NT_OPTIONAL_HDR_MAGIC 0x10b
2122 #ifndef IMAGE_NT_OPTIONAL_HDR64_MAGIC
2123 # define IMAGE_NT_OPTIONAL_HDR64_MAGIC 0x20b
2125 #ifndef IMAGE_NT_OPTIONAL_HDRROM_MAGIC
2126 # define IMAGE_NT_OPTIONAL_HDRROM_MAGIC 0x107
2131 case IMAGE_NT_OPTIONAL_HDR_MAGIC:
2134 case IMAGE_NT_OPTIONAL_HDR64_MAGIC:
2137 case IMAGE_NT_OPTIONAL_HDRROM_MAGIC:
2144 fprintf (file, "Magic\t\t\t%04x", i->Magic);
2146 fprintf (file, "\t(%s)",name);
2147 fprintf (file, "\nMajorLinkerVersion\t%d\n", i->MajorLinkerVersion);
2148 fprintf (file, "MinorLinkerVersion\t%d\n", i->MinorLinkerVersion);
2149 fprintf (file, "SizeOfCode\t\t%08lx\n", (unsigned long) i->SizeOfCode);
2150 fprintf (file, "SizeOfInitializedData\t%08lx\n",
2151 (unsigned long) i->SizeOfInitializedData);
2152 fprintf (file, "SizeOfUninitializedData\t%08lx\n",
2153 (unsigned long) i->SizeOfUninitializedData);
2154 fprintf (file, "AddressOfEntryPoint\t");
2155 fprintf_vma (file, i->AddressOfEntryPoint);
2156 fprintf (file, "\nBaseOfCode\t\t");
2157 fprintf_vma (file, i->BaseOfCode);
2158 #if !defined(COFF_WITH_pep) && !defined(COFF_WITH_pex64)
2159 /* PE32+ does not have BaseOfData member! */
2160 fprintf (file, "\nBaseOfData\t\t");
2161 fprintf_vma (file, i->BaseOfData);
2164 fprintf (file, "\nImageBase\t\t");
2165 fprintf_vma (file, i->ImageBase);
2166 fprintf (file, "\nSectionAlignment\t");
2167 fprintf_vma (file, i->SectionAlignment);
2168 fprintf (file, "\nFileAlignment\t\t");
2169 fprintf_vma (file, i->FileAlignment);
2170 fprintf (file, "\nMajorOSystemVersion\t%d\n", i->MajorOperatingSystemVersion);
2171 fprintf (file, "MinorOSystemVersion\t%d\n", i->MinorOperatingSystemVersion);
2172 fprintf (file, "MajorImageVersion\t%d\n", i->MajorImageVersion);
2173 fprintf (file, "MinorImageVersion\t%d\n", i->MinorImageVersion);
2174 fprintf (file, "MajorSubsystemVersion\t%d\n", i->MajorSubsystemVersion);
2175 fprintf (file, "MinorSubsystemVersion\t%d\n", i->MinorSubsystemVersion);
2176 fprintf (file, "Win32Version\t\t%08lx\n", (unsigned long) i->Reserved1);
2177 fprintf (file, "SizeOfImage\t\t%08lx\n", (unsigned long) i->SizeOfImage);
2178 fprintf (file, "SizeOfHeaders\t\t%08lx\n", (unsigned long) i->SizeOfHeaders);
2179 fprintf (file, "CheckSum\t\t%08lx\n", (unsigned long) i->CheckSum);
2181 switch (i->Subsystem)
2183 case IMAGE_SUBSYSTEM_UNKNOWN:
2184 subsystem_name = "unspecified";
2186 case IMAGE_SUBSYSTEM_NATIVE:
2187 subsystem_name = "NT native";
2189 case IMAGE_SUBSYSTEM_WINDOWS_GUI:
2190 subsystem_name = "Windows GUI";
2192 case IMAGE_SUBSYSTEM_WINDOWS_CUI:
2193 subsystem_name = "Windows CUI";
2195 case IMAGE_SUBSYSTEM_POSIX_CUI:
2196 subsystem_name = "POSIX CUI";
2198 case IMAGE_SUBSYSTEM_WINDOWS_CE_GUI:
2199 subsystem_name = "Wince CUI";
2201 case IMAGE_SUBSYSTEM_EFI_APPLICATION:
2202 subsystem_name = "EFI application";
2204 case IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER:
2205 subsystem_name = "EFI boot service driver";
2207 case IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER:
2208 subsystem_name = "EFI runtime driver";
2210 // These are from revision 8.0 of the MS PE/COFF spec
2211 case IMAGE_SUBSYSTEM_EFI_ROM:
2212 subsystem_name = "EFI ROM";
2214 case IMAGE_SUBSYSTEM_XBOX:
2215 subsystem_name = "XBOX";
2217 // Added default case for clarity - subsystem_name is NULL anyway.
2219 subsystem_name = NULL;
2222 fprintf (file, "Subsystem\t\t%08x", i->Subsystem);
2224 fprintf (file, "\t(%s)", subsystem_name);
2225 fprintf (file, "\nDllCharacteristics\t%08x\n", i->DllCharacteristics);
2226 fprintf (file, "SizeOfStackReserve\t");
2227 fprintf_vma (file, i->SizeOfStackReserve);
2228 fprintf (file, "\nSizeOfStackCommit\t");
2229 fprintf_vma (file, i->SizeOfStackCommit);
2230 fprintf (file, "\nSizeOfHeapReserve\t");
2231 fprintf_vma (file, i->SizeOfHeapReserve);
2232 fprintf (file, "\nSizeOfHeapCommit\t");
2233 fprintf_vma (file, i->SizeOfHeapCommit);
2234 fprintf (file, "\nLoaderFlags\t\t%08lx\n", (unsigned long) i->LoaderFlags);
2235 fprintf (file, "NumberOfRvaAndSizes\t%08lx\n",
2236 (unsigned long) i->NumberOfRvaAndSizes);
2238 fprintf (file, "\nThe Data Directory\n");
2239 for (j = 0; j < IMAGE_NUMBEROF_DIRECTORY_ENTRIES; j++)
2241 fprintf (file, "Entry %1x ", j);
2242 fprintf_vma (file, i->DataDirectory[j].VirtualAddress);
2243 fprintf (file, " %08lx ", (unsigned long) i->DataDirectory[j].Size);
2244 fprintf (file, "%s\n", dir_names[j]);
2247 pe_print_idata (abfd, vfile);
2248 pe_print_edata (abfd, vfile);
2249 if (bfd_coff_have_print_pdata (abfd))
2250 bfd_coff_print_pdata (abfd, vfile);
2252 pe_print_pdata (abfd, vfile);
2253 pe_print_reloc (abfd, vfile);
2258 /* Copy any private info we understand from the input bfd
2259 to the output bfd. */
2262 _bfd_XX_bfd_copy_private_bfd_data_common (bfd * ibfd, bfd * obfd)
2264 pe_data_type *ipe, *ope;
2266 /* One day we may try to grok other private data. */
2267 if (ibfd->xvec->flavour != bfd_target_coff_flavour
2268 || obfd->xvec->flavour != bfd_target_coff_flavour)
2271 ipe = pe_data (ibfd);
2272 ope = pe_data (obfd);
2274 ope->pe_opthdr = ipe->pe_opthdr;
2275 ope->dll = ipe->dll;
2277 /* Don't copy input subsystem if output is different from input. */
2278 if (obfd->xvec != ibfd->xvec)
2279 ope->pe_opthdr.Subsystem = IMAGE_SUBSYSTEM_UNKNOWN;
2281 /* For strip: if we removed .reloc, we'll make a real mess of things
2282 if we don't remove this entry as well. */
2283 if (! pe_data (obfd)->has_reloc_section)
2285 pe_data (obfd)->pe_opthdr.DataDirectory[PE_BASE_RELOCATION_TABLE].VirtualAddress = 0;
2286 pe_data (obfd)->pe_opthdr.DataDirectory[PE_BASE_RELOCATION_TABLE].Size = 0;
2291 /* Copy private section data. */
2294 _bfd_XX_bfd_copy_private_section_data (bfd *ibfd,
2299 if (bfd_get_flavour (ibfd) != bfd_target_coff_flavour
2300 || bfd_get_flavour (obfd) != bfd_target_coff_flavour)
2303 if (coff_section_data (ibfd, isec) != NULL
2304 && pei_section_data (ibfd, isec) != NULL)
2306 if (coff_section_data (obfd, osec) == NULL)
2308 bfd_size_type amt = sizeof (struct coff_section_tdata);
2309 osec->used_by_bfd = bfd_zalloc (obfd, amt);
2310 if (osec->used_by_bfd == NULL)
2314 if (pei_section_data (obfd, osec) == NULL)
2316 bfd_size_type amt = sizeof (struct pei_section_tdata);
2317 coff_section_data (obfd, osec)->tdata = bfd_zalloc (obfd, amt);
2318 if (coff_section_data (obfd, osec)->tdata == NULL)
2322 pei_section_data (obfd, osec)->virt_size =
2323 pei_section_data (ibfd, isec)->virt_size;
2324 pei_section_data (obfd, osec)->pe_flags =
2325 pei_section_data (ibfd, isec)->pe_flags;
2332 _bfd_XX_get_symbol_info (bfd * abfd, asymbol *symbol, symbol_info *ret)
2334 coff_get_symbol_info (abfd, symbol, ret);
2337 /* Handle the .idata section and other things that need symbol table
2341 _bfd_XXi_final_link_postscript (bfd * abfd, struct coff_final_link_info *pfinfo)
2343 struct coff_link_hash_entry *h1;
2344 struct bfd_link_info *info = pfinfo->info;
2345 bfd_boolean result = TRUE;
2347 /* There are a few fields that need to be filled in now while we
2348 have symbol table access.
2350 The .idata subsections aren't directly available as sections, but
2351 they are in the symbol table, so get them from there. */
2353 /* The import directory. This is the address of .idata$2, with size
2354 of .idata$2 + .idata$3. */
2355 h1 = coff_link_hash_lookup (coff_hash_table (info),
2356 ".idata$2", FALSE, FALSE, TRUE);
2359 /* PR ld/2729: We cannot rely upon all the output sections having been
2360 created properly, so check before referencing them. Issue a warning
2361 message for any sections tht could not be found. */
2362 if ((h1->root.type == bfd_link_hash_defined
2363 || h1->root.type == bfd_link_hash_defweak)
2364 && h1->root.u.def.section != NULL
2365 && h1->root.u.def.section->output_section != NULL)
2366 pe_data (abfd)->pe_opthdr.DataDirectory[PE_IMPORT_TABLE].VirtualAddress =
2367 (h1->root.u.def.value
2368 + h1->root.u.def.section->output_section->vma
2369 + h1->root.u.def.section->output_offset);
2373 (_("%B: unable to fill in DataDictionary[1] because .idata$2 is missing"),
2378 h1 = coff_link_hash_lookup (coff_hash_table (info),
2379 ".idata$4", FALSE, FALSE, TRUE);
2381 && (h1->root.type == bfd_link_hash_defined
2382 || h1->root.type == bfd_link_hash_defweak)
2383 && h1->root.u.def.section != NULL
2384 && h1->root.u.def.section->output_section != NULL)
2385 pe_data (abfd)->pe_opthdr.DataDirectory[PE_IMPORT_TABLE].Size =
2386 ((h1->root.u.def.value
2387 + h1->root.u.def.section->output_section->vma
2388 + h1->root.u.def.section->output_offset)
2389 - pe_data (abfd)->pe_opthdr.DataDirectory[PE_IMPORT_TABLE].VirtualAddress);
2393 (_("%B: unable to fill in DataDictionary[1] because .idata$4 is missing"),
2398 /* The import address table. This is the size/address of
2400 h1 = coff_link_hash_lookup (coff_hash_table (info),
2401 ".idata$5", FALSE, FALSE, TRUE);
2403 && (h1->root.type == bfd_link_hash_defined
2404 || h1->root.type == bfd_link_hash_defweak)
2405 && h1->root.u.def.section != NULL
2406 && h1->root.u.def.section->output_section != NULL)
2407 pe_data (abfd)->pe_opthdr.DataDirectory[PE_IMPORT_ADDRESS_TABLE].VirtualAddress =
2408 (h1->root.u.def.value
2409 + h1->root.u.def.section->output_section->vma
2410 + h1->root.u.def.section->output_offset);
2414 (_("%B: unable to fill in DataDictionary[12] because .idata$5 is missing"),
2419 h1 = coff_link_hash_lookup (coff_hash_table (info),
2420 ".idata$6", FALSE, FALSE, TRUE);
2422 && (h1->root.type == bfd_link_hash_defined
2423 || h1->root.type == bfd_link_hash_defweak)
2424 && h1->root.u.def.section != NULL
2425 && h1->root.u.def.section->output_section != NULL)
2426 pe_data (abfd)->pe_opthdr.DataDirectory[PE_IMPORT_ADDRESS_TABLE].Size =
2427 ((h1->root.u.def.value
2428 + h1->root.u.def.section->output_section->vma
2429 + h1->root.u.def.section->output_offset)
2430 - pe_data (abfd)->pe_opthdr.DataDirectory[PE_IMPORT_ADDRESS_TABLE].VirtualAddress);
2434 (_("%B: unable to fill in DataDictionary[PE_IMPORT_ADDRESS_TABLE (12)] because .idata$6 is missing"),
2440 h1 = coff_link_hash_lookup (coff_hash_table (info),
2441 "__tls_used", FALSE, FALSE, TRUE);
2444 if ((h1->root.type == bfd_link_hash_defined
2445 || h1->root.type == bfd_link_hash_defweak)
2446 && h1->root.u.def.section != NULL
2447 && h1->root.u.def.section->output_section != NULL)
2448 pe_data (abfd)->pe_opthdr.DataDirectory[PE_TLS_TABLE].VirtualAddress =
2449 (h1->root.u.def.value
2450 + h1->root.u.def.section->output_section->vma
2451 + h1->root.u.def.section->output_offset
2452 - pe_data (abfd)->pe_opthdr.ImageBase);
2456 (_("%B: unable to fill in DataDictionary[9] because __tls_used is missing"),
2461 pe_data (abfd)->pe_opthdr.DataDirectory[PE_TLS_TABLE].Size = 0x18;
2464 /* If we couldn't find idata$2, we either have an excessively
2465 trivial program or are in DEEP trouble; we have to assume trivial