2003-03-21 H.J. Lu <hongjiu.lu@intel.com>
[binutils.git] / bfd / peXXigen.c
blob2454436308d74a31f16c773539fdc982a7c88591
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 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 2 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, MA 02110-1301, USA. */
22 /* Most of this hacked by Steve Chamberlain <sac@cygnus.com>.
24 PE/PEI rearrangement (and code added): Donn Terry
25 Softway Systems, Inc. */
27 /* Hey look, some documentation [and in a place you expect to find it]!
29 The main reference for the pei format is "Microsoft Portable Executable
30 and Common Object File Format Specification 4.1". Get it if you need to
31 do some serious hacking on this code.
33 Another reference:
34 "Peering Inside the PE: A Tour of the Win32 Portable Executable
35 File Format", MSJ 1994, Volume 9.
37 The *sole* difference between the pe format and the pei format is that the
38 latter has an MSDOS 2.0 .exe header on the front that prints the message
39 "This app must be run under Windows." (or some such).
40 (FIXME: Whether that statement is *really* true or not is unknown.
41 Are there more subtle differences between pe and pei formats?
42 For now assume there aren't. If you find one, then for God sakes
43 document it here!)
45 The Microsoft docs use the word "image" instead of "executable" because
46 the former can also refer to a DLL (shared library). Confusion can arise
47 because the `i' in `pei' also refers to "image". The `pe' format can
48 also create images (i.e. executables), it's just that to run on a win32
49 system you need to use the pei format.
51 FIXME: Please add more docs here so the next poor fool that has to hack
52 on this code has a chance of getting something accomplished without
53 wasting too much time. */
55 /* This expands into COFF_WITH_pe, COFF_WITH_pep, or COFF_WITH_pex64
56 depending on whether we're compiling for straight PE or PE+. */
57 #define COFF_WITH_XX
59 #include "bfd.h"
60 #include "sysdep.h"
61 #include "libbfd.h"
62 #include "coff/internal.h"
64 /* NOTE: it's strange to be including an architecture specific header
65 in what's supposed to be general (to PE/PEI) code. However, that's
66 where the definitions are, and they don't vary per architecture
67 within PE/PEI, so we get them from there. FIXME: The lack of
68 variance is an assumption which may prove to be incorrect if new
69 PE/PEI targets are created. */
70 #if defined COFF_WITH_pex64
71 # include "coff/x86_64.h"
72 #elif defined COFF_WITH_pep
73 # include "coff/ia64.h"
74 #else
75 # include "coff/i386.h"
76 #endif
78 #include "coff/pe.h"
79 #include "libcoff.h"
80 #include "libpei.h"
82 #if defined COFF_WITH_pep || defined COFF_WITH_pex64
83 # undef AOUTSZ
84 # define AOUTSZ PEPAOUTSZ
85 # define PEAOUTHDR PEPAOUTHDR
86 #endif
88 /* FIXME: This file has various tests of POWERPC_LE_PE. Those tests
89 worked when the code was in peicode.h, but no longer work now that
90 the code is in peigen.c. PowerPC NT is said to be dead. If
91 anybody wants to revive the code, you will have to figure out how
92 to handle those issues. */
94 void
95 _bfd_XXi_swap_sym_in (bfd * abfd, void * ext1, void * in1)
97 SYMENT *ext = (SYMENT *) ext1;
98 struct internal_syment *in = (struct internal_syment *) in1;
100 if (ext->e.e_name[0] == 0)
102 in->_n._n_n._n_zeroes = 0;
103 in->_n._n_n._n_offset = H_GET_32 (abfd, ext->e.e.e_offset);
105 else
106 memcpy (in->_n._n_name, ext->e.e_name, SYMNMLEN);
108 in->n_value = H_GET_32 (abfd, ext->e_value);
109 in->n_scnum = H_GET_16 (abfd, ext->e_scnum);
111 if (sizeof (ext->e_type) == 2)
112 in->n_type = H_GET_16 (abfd, ext->e_type);
113 else
114 in->n_type = H_GET_32 (abfd, ext->e_type);
116 in->n_sclass = H_GET_8 (abfd, ext->e_sclass);
117 in->n_numaux = H_GET_8 (abfd, ext->e_numaux);
119 #ifndef STRICT_PE_FORMAT
120 /* This is for Gnu-created DLLs. */
122 /* The section symbols for the .idata$ sections have class 0x68
123 (C_SECTION), which MS documentation indicates is a section
124 symbol. Unfortunately, the value field in the symbol is simply a
125 copy of the .idata section's flags rather than something useful.
126 When these symbols are encountered, change the value to 0 so that
127 they will be handled somewhat correctly in the bfd code. */
128 if (in->n_sclass == C_SECTION)
130 in->n_value = 0x0;
132 /* Create synthetic empty sections as needed. DJ */
133 if (in->n_scnum == 0)
135 asection *sec;
137 for (sec = abfd->sections; sec; sec = sec->next)
139 if (strcmp (sec->name, in->n_name) == 0)
141 in->n_scnum = sec->target_index;
142 break;
147 if (in->n_scnum == 0)
149 int unused_section_number = 0;
150 asection *sec;
151 char *name;
152 flagword flags;
154 for (sec = abfd->sections; sec; sec = sec->next)
155 if (unused_section_number <= sec->target_index)
156 unused_section_number = sec->target_index + 1;
158 name = bfd_alloc (abfd, (bfd_size_type) strlen (in->n_name) + 10);
159 if (name == NULL)
160 return;
161 strcpy (name, in->n_name);
162 flags = SEC_HAS_CONTENTS | SEC_ALLOC | SEC_DATA | SEC_LOAD;
163 sec = bfd_make_section_anyway_with_flags (abfd, name, flags);
165 sec->vma = 0;
166 sec->lma = 0;
167 sec->size = 0;
168 sec->filepos = 0;
169 sec->rel_filepos = 0;
170 sec->reloc_count = 0;
171 sec->line_filepos = 0;
172 sec->lineno_count = 0;
173 sec->userdata = NULL;
174 sec->next = NULL;
175 sec->alignment_power = 2;
177 sec->target_index = unused_section_number;
179 in->n_scnum = unused_section_number;
181 in->n_sclass = C_STAT;
183 #endif
185 #ifdef coff_swap_sym_in_hook
186 /* This won't work in peigen.c, but since it's for PPC PE, it's not
187 worth fixing. */
188 coff_swap_sym_in_hook (abfd, ext1, in1);
189 #endif
192 unsigned int
193 _bfd_XXi_swap_sym_out (bfd * abfd, void * inp, void * extp)
195 struct internal_syment *in = (struct internal_syment *) inp;
196 SYMENT *ext = (SYMENT *) extp;
198 if (in->_n._n_name[0] == 0)
200 H_PUT_32 (abfd, 0, ext->e.e.e_zeroes);
201 H_PUT_32 (abfd, in->_n._n_n._n_offset, ext->e.e.e_offset);
203 else
204 memcpy (ext->e.e_name, in->_n._n_name, SYMNMLEN);
206 H_PUT_32 (abfd, in->n_value, ext->e_value);
207 H_PUT_16 (abfd, in->n_scnum, ext->e_scnum);
209 if (sizeof (ext->e_type) == 2)
210 H_PUT_16 (abfd, in->n_type, ext->e_type);
211 else
212 H_PUT_32 (abfd, in->n_type, ext->e_type);
214 H_PUT_8 (abfd, in->n_sclass, ext->e_sclass);
215 H_PUT_8 (abfd, in->n_numaux, ext->e_numaux);
217 return SYMESZ;
220 void
221 _bfd_XXi_swap_aux_in (bfd * abfd,
222 void * ext1,
223 int type,
224 int class,
225 int indx ATTRIBUTE_UNUSED,
226 int numaux ATTRIBUTE_UNUSED,
227 void * in1)
229 AUXENT *ext = (AUXENT *) ext1;
230 union internal_auxent *in = (union internal_auxent *) in1;
232 switch (class)
234 case C_FILE:
235 if (ext->x_file.x_fname[0] == 0)
237 in->x_file.x_n.x_zeroes = 0;
238 in->x_file.x_n.x_offset = H_GET_32 (abfd, ext->x_file.x_n.x_offset);
240 else
241 memcpy (in->x_file.x_fname, ext->x_file.x_fname, FILNMLEN);
242 return;
244 case C_STAT:
245 case C_LEAFSTAT:
246 case C_HIDDEN:
247 if (type == T_NULL)
249 in->x_scn.x_scnlen = GET_SCN_SCNLEN (abfd, ext);
250 in->x_scn.x_nreloc = GET_SCN_NRELOC (abfd, ext);
251 in->x_scn.x_nlinno = GET_SCN_NLINNO (abfd, ext);
252 in->x_scn.x_checksum = H_GET_32 (abfd, ext->x_scn.x_checksum);
253 in->x_scn.x_associated = H_GET_16 (abfd, ext->x_scn.x_associated);
254 in->x_scn.x_comdat = H_GET_8 (abfd, ext->x_scn.x_comdat);
255 return;
257 break;
260 in->x_sym.x_tagndx.l = H_GET_32 (abfd, ext->x_sym.x_tagndx);
261 in->x_sym.x_tvndx = H_GET_16 (abfd, ext->x_sym.x_tvndx);
263 if (class == C_BLOCK || class == C_FCN || ISFCN (type) || ISTAG (class))
265 in->x_sym.x_fcnary.x_fcn.x_lnnoptr = GET_FCN_LNNOPTR (abfd, ext);
266 in->x_sym.x_fcnary.x_fcn.x_endndx.l = GET_FCN_ENDNDX (abfd, ext);
268 else
270 in->x_sym.x_fcnary.x_ary.x_dimen[0] =
271 H_GET_16 (abfd, ext->x_sym.x_fcnary.x_ary.x_dimen[0]);
272 in->x_sym.x_fcnary.x_ary.x_dimen[1] =
273 H_GET_16 (abfd, ext->x_sym.x_fcnary.x_ary.x_dimen[1]);
274 in->x_sym.x_fcnary.x_ary.x_dimen[2] =
275 H_GET_16 (abfd, ext->x_sym.x_fcnary.x_ary.x_dimen[2]);
276 in->x_sym.x_fcnary.x_ary.x_dimen[3] =
277 H_GET_16 (abfd, ext->x_sym.x_fcnary.x_ary.x_dimen[3]);
280 if (ISFCN (type))
282 in->x_sym.x_misc.x_fsize = H_GET_32 (abfd, ext->x_sym.x_misc.x_fsize);
284 else
286 in->x_sym.x_misc.x_lnsz.x_lnno = GET_LNSZ_LNNO (abfd, ext);
287 in->x_sym.x_misc.x_lnsz.x_size = GET_LNSZ_SIZE (abfd, ext);
291 unsigned int
292 _bfd_XXi_swap_aux_out (bfd * abfd,
293 void * inp,
294 int type,
295 int class,
296 int indx ATTRIBUTE_UNUSED,
297 int numaux ATTRIBUTE_UNUSED,
298 void * extp)
300 union internal_auxent *in = (union internal_auxent *) inp;
301 AUXENT *ext = (AUXENT *) extp;
303 memset (ext, 0, AUXESZ);
305 switch (class)
307 case C_FILE:
308 if (in->x_file.x_fname[0] == 0)
310 H_PUT_32 (abfd, 0, ext->x_file.x_n.x_zeroes);
311 H_PUT_32 (abfd, in->x_file.x_n.x_offset, ext->x_file.x_n.x_offset);
313 else
314 memcpy (ext->x_file.x_fname, in->x_file.x_fname, FILNMLEN);
316 return AUXESZ;
318 case C_STAT:
319 case C_LEAFSTAT:
320 case C_HIDDEN:
321 if (type == T_NULL)
323 PUT_SCN_SCNLEN (abfd, in->x_scn.x_scnlen, ext);
324 PUT_SCN_NRELOC (abfd, in->x_scn.x_nreloc, ext);
325 PUT_SCN_NLINNO (abfd, in->x_scn.x_nlinno, ext);
326 H_PUT_32 (abfd, in->x_scn.x_checksum, ext->x_scn.x_checksum);
327 H_PUT_16 (abfd, in->x_scn.x_associated, ext->x_scn.x_associated);
328 H_PUT_8 (abfd, in->x_scn.x_comdat, ext->x_scn.x_comdat);
329 return AUXESZ;
331 break;
334 H_PUT_32 (abfd, in->x_sym.x_tagndx.l, ext->x_sym.x_tagndx);
335 H_PUT_16 (abfd, in->x_sym.x_tvndx, ext->x_sym.x_tvndx);
337 if (class == C_BLOCK || class == C_FCN || ISFCN (type) || ISTAG (class))
339 PUT_FCN_LNNOPTR (abfd, in->x_sym.x_fcnary.x_fcn.x_lnnoptr, ext);
340 PUT_FCN_ENDNDX (abfd, in->x_sym.x_fcnary.x_fcn.x_endndx.l, ext);
342 else
344 H_PUT_16 (abfd, in->x_sym.x_fcnary.x_ary.x_dimen[0],
345 ext->x_sym.x_fcnary.x_ary.x_dimen[0]);
346 H_PUT_16 (abfd, in->x_sym.x_fcnary.x_ary.x_dimen[1],
347 ext->x_sym.x_fcnary.x_ary.x_dimen[1]);
348 H_PUT_16 (abfd, in->x_sym.x_fcnary.x_ary.x_dimen[2],
349 ext->x_sym.x_fcnary.x_ary.x_dimen[2]);
350 H_PUT_16 (abfd, in->x_sym.x_fcnary.x_ary.x_dimen[3],
351 ext->x_sym.x_fcnary.x_ary.x_dimen[3]);
354 if (ISFCN (type))
355 H_PUT_32 (abfd, in->x_sym.x_misc.x_fsize, ext->x_sym.x_misc.x_fsize);
356 else
358 PUT_LNSZ_LNNO (abfd, in->x_sym.x_misc.x_lnsz.x_lnno, ext);
359 PUT_LNSZ_SIZE (abfd, in->x_sym.x_misc.x_lnsz.x_size, ext);
362 return AUXESZ;
365 void
366 _bfd_XXi_swap_lineno_in (bfd * abfd, void * ext1, void * in1)
368 LINENO *ext = (LINENO *) ext1;
369 struct internal_lineno *in = (struct internal_lineno *) in1;
371 in->l_addr.l_symndx = H_GET_32 (abfd, ext->l_addr.l_symndx);
372 in->l_lnno = GET_LINENO_LNNO (abfd, ext);
375 unsigned int
376 _bfd_XXi_swap_lineno_out (bfd * abfd, void * inp, void * outp)
378 struct internal_lineno *in = (struct internal_lineno *) inp;
379 struct external_lineno *ext = (struct external_lineno *) outp;
380 H_PUT_32 (abfd, in->l_addr.l_symndx, ext->l_addr.l_symndx);
382 PUT_LINENO_LNNO (abfd, in->l_lnno, ext);
383 return LINESZ;
386 void
387 _bfd_XXi_swap_aouthdr_in (bfd * abfd,
388 void * aouthdr_ext1,
389 void * aouthdr_int1)
391 PEAOUTHDR * src = (PEAOUTHDR *) aouthdr_ext1;
392 AOUTHDR * aouthdr_ext = (AOUTHDR *) aouthdr_ext1;
393 struct internal_aouthdr *aouthdr_int
394 = (struct internal_aouthdr *) aouthdr_int1;
395 struct internal_extra_pe_aouthdr *a = &aouthdr_int->pe;
397 aouthdr_int->magic = H_GET_16 (abfd, aouthdr_ext->magic);
398 aouthdr_int->vstamp = H_GET_16 (abfd, aouthdr_ext->vstamp);
399 aouthdr_int->tsize = GET_AOUTHDR_TSIZE (abfd, aouthdr_ext->tsize);
400 aouthdr_int->dsize = GET_AOUTHDR_DSIZE (abfd, aouthdr_ext->dsize);
401 aouthdr_int->bsize = GET_AOUTHDR_BSIZE (abfd, aouthdr_ext->bsize);
402 aouthdr_int->entry = GET_AOUTHDR_ENTRY (abfd, aouthdr_ext->entry);
403 aouthdr_int->text_start =
404 GET_AOUTHDR_TEXT_START (abfd, aouthdr_ext->text_start);
405 #if !defined(COFF_WITH_pep) && !defined(COFF_WITH_pex64)
406 /* PE32+ does not have data_start member! */
407 aouthdr_int->data_start =
408 GET_AOUTHDR_DATA_START (abfd, aouthdr_ext->data_start);
409 a->BaseOfData = aouthdr_int->data_start;
410 #endif
412 a->Magic = aouthdr_int->magic;
413 a->MajorLinkerVersion = H_GET_8 (abfd, aouthdr_ext->vstamp);
414 a->MinorLinkerVersion = H_GET_8 (abfd, aouthdr_ext->vstamp + 1);
415 a->SizeOfCode = aouthdr_int->tsize ;
416 a->SizeOfInitializedData = aouthdr_int->dsize ;
417 a->SizeOfUninitializedData = aouthdr_int->bsize ;
418 a->AddressOfEntryPoint = aouthdr_int->entry;
419 a->BaseOfCode = aouthdr_int->text_start;
420 a->ImageBase = GET_OPTHDR_IMAGE_BASE (abfd, src->ImageBase);
421 a->SectionAlignment = H_GET_32 (abfd, src->SectionAlignment);
422 a->FileAlignment = H_GET_32 (abfd, src->FileAlignment);
423 a->MajorOperatingSystemVersion =
424 H_GET_16 (abfd, src->MajorOperatingSystemVersion);
425 a->MinorOperatingSystemVersion =
426 H_GET_16 (abfd, src->MinorOperatingSystemVersion);
427 a->MajorImageVersion = H_GET_16 (abfd, src->MajorImageVersion);
428 a->MinorImageVersion = H_GET_16 (abfd, src->MinorImageVersion);
429 a->MajorSubsystemVersion = H_GET_16 (abfd, src->MajorSubsystemVersion);
430 a->MinorSubsystemVersion = H_GET_16 (abfd, src->MinorSubsystemVersion);
431 a->Reserved1 = H_GET_32 (abfd, src->Reserved1);
432 a->SizeOfImage = H_GET_32 (abfd, src->SizeOfImage);
433 a->SizeOfHeaders = H_GET_32 (abfd, src->SizeOfHeaders);
434 a->CheckSum = H_GET_32 (abfd, src->CheckSum);
435 a->Subsystem = H_GET_16 (abfd, src->Subsystem);
436 a->DllCharacteristics = H_GET_16 (abfd, src->DllCharacteristics);
437 a->SizeOfStackReserve =
438 GET_OPTHDR_SIZE_OF_STACK_RESERVE (abfd, src->SizeOfStackReserve);
439 a->SizeOfStackCommit =
440 GET_OPTHDR_SIZE_OF_STACK_COMMIT (abfd, src->SizeOfStackCommit);
441 a->SizeOfHeapReserve =
442 GET_OPTHDR_SIZE_OF_HEAP_RESERVE (abfd, src->SizeOfHeapReserve);
443 a->SizeOfHeapCommit =
444 GET_OPTHDR_SIZE_OF_HEAP_COMMIT (abfd, src->SizeOfHeapCommit);
445 a->LoaderFlags = H_GET_32 (abfd, src->LoaderFlags);
446 a->NumberOfRvaAndSizes = H_GET_32 (abfd, src->NumberOfRvaAndSizes);
449 int idx;
451 for (idx = 0; idx < 16; idx++)
453 /* If data directory is empty, rva also should be 0. */
454 int size =
455 H_GET_32 (abfd, src->DataDirectory[idx][1]);
457 a->DataDirectory[idx].Size = size;
459 if (size)
460 a->DataDirectory[idx].VirtualAddress =
461 H_GET_32 (abfd, src->DataDirectory[idx][0]);
462 else
463 a->DataDirectory[idx].VirtualAddress = 0;
467 if (aouthdr_int->entry)
469 aouthdr_int->entry += a->ImageBase;
470 #if !defined(COFF_WITH_pep) && !defined(COFF_WITH_pex64)
471 aouthdr_int->entry &= 0xffffffff;
472 #endif
475 if (aouthdr_int->tsize)
477 aouthdr_int->text_start += a->ImageBase;
478 #if !defined(COFF_WITH_pep) && !defined(COFF_WITH_pex64)
479 aouthdr_int->text_start &= 0xffffffff;
480 #endif
483 #if !defined(COFF_WITH_pep) && !defined(COFF_WITH_pex64)
484 /* PE32+ does not have data_start member! */
485 if (aouthdr_int->dsize)
487 aouthdr_int->data_start += a->ImageBase;
488 aouthdr_int->data_start &= 0xffffffff;
490 #endif
492 #ifdef POWERPC_LE_PE
493 /* These three fields are normally set up by ppc_relocate_section.
494 In the case of reading a file in, we can pick them up from the
495 DataDirectory. */
496 first_thunk_address = a->DataDirectory[PE_IMPORT_ADDRESS_TABLE].VirtualAddress;
497 thunk_size = a->DataDirectory[PE_IMPORT_ADDRESS_TABLE].Size;
498 import_table_size = a->DataDirectory[PE_IMPORT_TABLE].Size;
499 #endif
502 /* A support function for below. */
504 static void
505 add_data_entry (bfd * abfd,
506 struct internal_extra_pe_aouthdr *aout,
507 int idx,
508 char *name,
509 bfd_vma base)
511 asection *sec = bfd_get_section_by_name (abfd, name);
513 /* Add import directory information if it exists. */
514 if ((sec != NULL)
515 && (coff_section_data (abfd, sec) != NULL)
516 && (pei_section_data (abfd, sec) != NULL))
518 /* If data directory is empty, rva also should be 0. */
519 int size = pei_section_data (abfd, sec)->virt_size;
520 aout->DataDirectory[idx].Size = size;
522 if (size)
524 aout->DataDirectory[idx].VirtualAddress =
525 (sec->vma - base) & 0xffffffff;
526 sec->flags |= SEC_DATA;
531 unsigned int
532 _bfd_XXi_swap_aouthdr_out (bfd * abfd, void * in, void * out)
534 struct internal_aouthdr *aouthdr_in = (struct internal_aouthdr *) in;
535 pe_data_type *pe = pe_data (abfd);
536 struct internal_extra_pe_aouthdr *extra = &pe->pe_opthdr;
537 PEAOUTHDR *aouthdr_out = (PEAOUTHDR *) out;
538 bfd_vma sa, fa, ib;
539 IMAGE_DATA_DIRECTORY idata2, idata5, tls;
541 if (pe->force_minimum_alignment)
543 if (!extra->FileAlignment)
544 extra->FileAlignment = PE_DEF_FILE_ALIGNMENT;
545 if (!extra->SectionAlignment)
546 extra->SectionAlignment = PE_DEF_SECTION_ALIGNMENT;
549 if (extra->Subsystem == IMAGE_SUBSYSTEM_UNKNOWN)
550 extra->Subsystem = pe->target_subsystem;
552 sa = extra->SectionAlignment;
553 fa = extra->FileAlignment;
554 ib = extra->ImageBase;
556 idata2 = pe->pe_opthdr.DataDirectory[PE_IMPORT_TABLE];
557 idata5 = pe->pe_opthdr.DataDirectory[PE_IMPORT_ADDRESS_TABLE];
558 tls = pe->pe_opthdr.DataDirectory[PE_TLS_TABLE];
560 if (aouthdr_in->tsize)
562 aouthdr_in->text_start -= ib;
563 #if !defined(COFF_WITH_pep) && !defined(COFF_WITH_pex64)
564 aouthdr_in->text_start &= 0xffffffff;
565 #endif
568 if (aouthdr_in->dsize)
570 aouthdr_in->data_start -= ib;
571 #if !defined(COFF_WITH_pep) && !defined(COFF_WITH_pex64)
572 aouthdr_in->data_start &= 0xffffffff;
573 #endif
576 if (aouthdr_in->entry)
578 aouthdr_in->entry -= ib;
579 #if !defined(COFF_WITH_pep) && !defined(COFF_WITH_pex64)
580 aouthdr_in->entry &= 0xffffffff;
581 #endif
584 #define FA(x) (((x) + fa -1 ) & (- fa))
585 #define SA(x) (((x) + sa -1 ) & (- sa))
587 /* We like to have the sizes aligned. */
588 aouthdr_in->bsize = FA (aouthdr_in->bsize);
590 extra->NumberOfRvaAndSizes = IMAGE_NUMBEROF_DIRECTORY_ENTRIES;
592 /* First null out all data directory entries. */
593 memset (extra->DataDirectory, 0, sizeof (extra->DataDirectory));
595 add_data_entry (abfd, extra, 0, ".edata", ib);
596 add_data_entry (abfd, extra, 2, ".rsrc", ib);
597 add_data_entry (abfd, extra, 3, ".pdata", ib);
599 /* In theory we do not need to call add_data_entry for .idata$2 or
600 .idata$5. It will be done in bfd_coff_final_link where all the
601 required information is available. If however, we are not going
602 to perform a final link, eg because we have been invoked by objcopy
603 or strip, then we need to make sure that these Data Directory
604 entries are initialised properly.
606 So - we copy the input values into the output values, and then, if
607 a final link is going to be performed, it can overwrite them. */
608 extra->DataDirectory[PE_IMPORT_TABLE] = idata2;
609 extra->DataDirectory[PE_IMPORT_ADDRESS_TABLE] = idata5;
610 extra->DataDirectory[PE_TLS_TABLE] = tls;
612 if (extra->DataDirectory[PE_IMPORT_TABLE].VirtualAddress == 0)
613 /* Until other .idata fixes are made (pending patch), the entry for
614 .idata is needed for backwards compatibility. FIXME. */
615 add_data_entry (abfd, extra, 1, ".idata", ib);
617 /* For some reason, the virtual size (which is what's set by
618 add_data_entry) for .reloc is not the same as the size recorded
619 in this slot by MSVC; it doesn't seem to cause problems (so far),
620 but since it's the best we've got, use it. It does do the right
621 thing for .pdata. */
622 if (pe->has_reloc_section)
623 add_data_entry (abfd, extra, 5, ".reloc", ib);
626 asection *sec;
627 bfd_vma hsize = 0;
628 bfd_vma dsize = 0;
629 bfd_vma isize = 0;
630 bfd_vma tsize = 0;
632 for (sec = abfd->sections; sec; sec = sec->next)
634 int rounded = FA (sec->size);
636 /* The first non-zero section filepos is the header size.
637 Sections without contents will have a filepos of 0. */
638 if (hsize == 0)
639 hsize = sec->filepos;
640 if (sec->flags & SEC_DATA)
641 dsize += rounded;
642 if (sec->flags & SEC_CODE)
643 tsize += rounded;
644 /* The image size is the total VIRTUAL size (which is what is
645 in the virt_size field). Files have been seen (from MSVC
646 5.0 link.exe) where the file size of the .data segment is
647 quite small compared to the virtual size. Without this
648 fix, strip munges the file. */
649 if (coff_section_data (abfd, sec) != NULL
650 && pei_section_data (abfd, sec) != NULL)
651 isize += SA (FA (pei_section_data (abfd, sec)->virt_size));
654 aouthdr_in->dsize = dsize;
655 aouthdr_in->tsize = tsize;
656 extra->SizeOfHeaders = hsize;
657 extra->SizeOfImage = SA (hsize) + isize;
660 H_PUT_16 (abfd, aouthdr_in->magic, aouthdr_out->standard.magic);
662 #define LINKER_VERSION 256 /* That is, 2.56 */
664 /* This piece of magic sets the "linker version" field to
665 LINKER_VERSION. */
666 H_PUT_16 (abfd, (LINKER_VERSION / 100 + (LINKER_VERSION % 100) * 256),
667 aouthdr_out->standard.vstamp);
669 PUT_AOUTHDR_TSIZE (abfd, aouthdr_in->tsize, aouthdr_out->standard.tsize);
670 PUT_AOUTHDR_DSIZE (abfd, aouthdr_in->dsize, aouthdr_out->standard.dsize);
671 PUT_AOUTHDR_BSIZE (abfd, aouthdr_in->bsize, aouthdr_out->standard.bsize);
672 PUT_AOUTHDR_ENTRY (abfd, aouthdr_in->entry, aouthdr_out->standard.entry);
673 PUT_AOUTHDR_TEXT_START (abfd, aouthdr_in->text_start,
674 aouthdr_out->standard.text_start);
676 #if !defined(COFF_WITH_pep) && !defined(COFF_WITH_pex64)
677 /* PE32+ does not have data_start member! */
678 PUT_AOUTHDR_DATA_START (abfd, aouthdr_in->data_start,
679 aouthdr_out->standard.data_start);
680 #endif
682 PUT_OPTHDR_IMAGE_BASE (abfd, extra->ImageBase, aouthdr_out->ImageBase);
683 H_PUT_32 (abfd, extra->SectionAlignment, aouthdr_out->SectionAlignment);
684 H_PUT_32 (abfd, extra->FileAlignment, aouthdr_out->FileAlignment);
685 H_PUT_16 (abfd, extra->MajorOperatingSystemVersion,
686 aouthdr_out->MajorOperatingSystemVersion);
687 H_PUT_16 (abfd, extra->MinorOperatingSystemVersion,
688 aouthdr_out->MinorOperatingSystemVersion);
689 H_PUT_16 (abfd, extra->MajorImageVersion, aouthdr_out->MajorImageVersion);
690 H_PUT_16 (abfd, extra->MinorImageVersion, aouthdr_out->MinorImageVersion);
691 H_PUT_16 (abfd, extra->MajorSubsystemVersion,
692 aouthdr_out->MajorSubsystemVersion);
693 H_PUT_16 (abfd, extra->MinorSubsystemVersion,
694 aouthdr_out->MinorSubsystemVersion);
695 H_PUT_32 (abfd, extra->Reserved1, aouthdr_out->Reserved1);
696 H_PUT_32 (abfd, extra->SizeOfImage, aouthdr_out->SizeOfImage);
697 H_PUT_32 (abfd, extra->SizeOfHeaders, aouthdr_out->SizeOfHeaders);
698 H_PUT_32 (abfd, extra->CheckSum, aouthdr_out->CheckSum);
699 H_PUT_16 (abfd, extra->Subsystem, aouthdr_out->Subsystem);
700 H_PUT_16 (abfd, extra->DllCharacteristics, aouthdr_out->DllCharacteristics);
701 PUT_OPTHDR_SIZE_OF_STACK_RESERVE (abfd, extra->SizeOfStackReserve,
702 aouthdr_out->SizeOfStackReserve);
703 PUT_OPTHDR_SIZE_OF_STACK_COMMIT (abfd, extra->SizeOfStackCommit,
704 aouthdr_out->SizeOfStackCommit);
705 PUT_OPTHDR_SIZE_OF_HEAP_RESERVE (abfd, extra->SizeOfHeapReserve,
706 aouthdr_out->SizeOfHeapReserve);
707 PUT_OPTHDR_SIZE_OF_HEAP_COMMIT (abfd, extra->SizeOfHeapCommit,
708 aouthdr_out->SizeOfHeapCommit);
709 H_PUT_32 (abfd, extra->LoaderFlags, aouthdr_out->LoaderFlags);
710 H_PUT_32 (abfd, extra->NumberOfRvaAndSizes,
711 aouthdr_out->NumberOfRvaAndSizes);
713 int idx;
715 for (idx = 0; idx < 16; idx++)
717 H_PUT_32 (abfd, extra->DataDirectory[idx].VirtualAddress,
718 aouthdr_out->DataDirectory[idx][0]);
719 H_PUT_32 (abfd, extra->DataDirectory[idx].Size,
720 aouthdr_out->DataDirectory[idx][1]);
724 return AOUTSZ;
727 unsigned int
728 _bfd_XXi_only_swap_filehdr_out (bfd * abfd, void * in, void * out)
730 int idx;
731 struct internal_filehdr *filehdr_in = (struct internal_filehdr *) in;
732 struct external_PEI_filehdr *filehdr_out = (struct external_PEI_filehdr *) out;
734 if (pe_data (abfd)->has_reloc_section)
735 filehdr_in->f_flags &= ~F_RELFLG;
737 if (pe_data (abfd)->dll)
738 filehdr_in->f_flags |= F_DLL;
740 filehdr_in->pe.e_magic = DOSMAGIC;
741 filehdr_in->pe.e_cblp = 0x90;
742 filehdr_in->pe.e_cp = 0x3;
743 filehdr_in->pe.e_crlc = 0x0;
744 filehdr_in->pe.e_cparhdr = 0x4;
745 filehdr_in->pe.e_minalloc = 0x0;
746 filehdr_in->pe.e_maxalloc = 0xffff;
747 filehdr_in->pe.e_ss = 0x0;
748 filehdr_in->pe.e_sp = 0xb8;
749 filehdr_in->pe.e_csum = 0x0;
750 filehdr_in->pe.e_ip = 0x0;
751 filehdr_in->pe.e_cs = 0x0;
752 filehdr_in->pe.e_lfarlc = 0x40;
753 filehdr_in->pe.e_ovno = 0x0;
755 for (idx = 0; idx < 4; idx++)
756 filehdr_in->pe.e_res[idx] = 0x0;
758 filehdr_in->pe.e_oemid = 0x0;
759 filehdr_in->pe.e_oeminfo = 0x0;
761 for (idx = 0; idx < 10; idx++)
762 filehdr_in->pe.e_res2[idx] = 0x0;
764 filehdr_in->pe.e_lfanew = 0x80;
766 /* This next collection of data are mostly just characters. It
767 appears to be constant within the headers put on NT exes. */
768 filehdr_in->pe.dos_message[0] = 0x0eba1f0e;
769 filehdr_in->pe.dos_message[1] = 0xcd09b400;
770 filehdr_in->pe.dos_message[2] = 0x4c01b821;
771 filehdr_in->pe.dos_message[3] = 0x685421cd;
772 filehdr_in->pe.dos_message[4] = 0x70207369;
773 filehdr_in->pe.dos_message[5] = 0x72676f72;
774 filehdr_in->pe.dos_message[6] = 0x63206d61;
775 filehdr_in->pe.dos_message[7] = 0x6f6e6e61;
776 filehdr_in->pe.dos_message[8] = 0x65622074;
777 filehdr_in->pe.dos_message[9] = 0x6e757220;
778 filehdr_in->pe.dos_message[10] = 0x206e6920;
779 filehdr_in->pe.dos_message[11] = 0x20534f44;
780 filehdr_in->pe.dos_message[12] = 0x65646f6d;
781 filehdr_in->pe.dos_message[13] = 0x0a0d0d2e;
782 filehdr_in->pe.dos_message[14] = 0x24;
783 filehdr_in->pe.dos_message[15] = 0x0;
784 filehdr_in->pe.nt_signature = NT_SIGNATURE;
786 H_PUT_16 (abfd, filehdr_in->f_magic, filehdr_out->f_magic);
787 H_PUT_16 (abfd, filehdr_in->f_nscns, filehdr_out->f_nscns);
789 H_PUT_32 (abfd, time (0), filehdr_out->f_timdat);
790 PUT_FILEHDR_SYMPTR (abfd, filehdr_in->f_symptr,
791 filehdr_out->f_symptr);
792 H_PUT_32 (abfd, filehdr_in->f_nsyms, filehdr_out->f_nsyms);
793 H_PUT_16 (abfd, filehdr_in->f_opthdr, filehdr_out->f_opthdr);
794 H_PUT_16 (abfd, filehdr_in->f_flags, filehdr_out->f_flags);
796 /* Put in extra dos header stuff. This data remains essentially
797 constant, it just has to be tacked on to the beginning of all exes
798 for NT. */
799 H_PUT_16 (abfd, filehdr_in->pe.e_magic, filehdr_out->e_magic);
800 H_PUT_16 (abfd, filehdr_in->pe.e_cblp, filehdr_out->e_cblp);
801 H_PUT_16 (abfd, filehdr_in->pe.e_cp, filehdr_out->e_cp);
802 H_PUT_16 (abfd, filehdr_in->pe.e_crlc, filehdr_out->e_crlc);
803 H_PUT_16 (abfd, filehdr_in->pe.e_cparhdr, filehdr_out->e_cparhdr);
804 H_PUT_16 (abfd, filehdr_in->pe.e_minalloc, filehdr_out->e_minalloc);
805 H_PUT_16 (abfd, filehdr_in->pe.e_maxalloc, filehdr_out->e_maxalloc);
806 H_PUT_16 (abfd, filehdr_in->pe.e_ss, filehdr_out->e_ss);
807 H_PUT_16 (abfd, filehdr_in->pe.e_sp, filehdr_out->e_sp);
808 H_PUT_16 (abfd, filehdr_in->pe.e_csum, filehdr_out->e_csum);
809 H_PUT_16 (abfd, filehdr_in->pe.e_ip, filehdr_out->e_ip);
810 H_PUT_16 (abfd, filehdr_in->pe.e_cs, filehdr_out->e_cs);
811 H_PUT_16 (abfd, filehdr_in->pe.e_lfarlc, filehdr_out->e_lfarlc);
812 H_PUT_16 (abfd, filehdr_in->pe.e_ovno, filehdr_out->e_ovno);
814 for (idx = 0; idx < 4; idx++)
815 H_PUT_16 (abfd, filehdr_in->pe.e_res[idx], filehdr_out->e_res[idx]);
817 H_PUT_16 (abfd, filehdr_in->pe.e_oemid, filehdr_out->e_oemid);
818 H_PUT_16 (abfd, filehdr_in->pe.e_oeminfo, filehdr_out->e_oeminfo);
820 for (idx = 0; idx < 10; idx++)
821 H_PUT_16 (abfd, filehdr_in->pe.e_res2[idx], filehdr_out->e_res2[idx]);
823 H_PUT_32 (abfd, filehdr_in->pe.e_lfanew, filehdr_out->e_lfanew);
825 for (idx = 0; idx < 16; idx++)
826 H_PUT_32 (abfd, filehdr_in->pe.dos_message[idx],
827 filehdr_out->dos_message[idx]);
829 /* Also put in the NT signature. */
830 H_PUT_32 (abfd, filehdr_in->pe.nt_signature, filehdr_out->nt_signature);
832 return FILHSZ;
835 unsigned int
836 _bfd_XX_only_swap_filehdr_out (bfd * abfd, void * in, void * out)
838 struct internal_filehdr *filehdr_in = (struct internal_filehdr *) in;
839 FILHDR *filehdr_out = (FILHDR *) out;
841 H_PUT_16 (abfd, filehdr_in->f_magic, filehdr_out->f_magic);
842 H_PUT_16 (abfd, filehdr_in->f_nscns, filehdr_out->f_nscns);
843 H_PUT_32 (abfd, filehdr_in->f_timdat, filehdr_out->f_timdat);
844 PUT_FILEHDR_SYMPTR (abfd, filehdr_in->f_symptr, filehdr_out->f_symptr);
845 H_PUT_32 (abfd, filehdr_in->f_nsyms, filehdr_out->f_nsyms);
846 H_PUT_16 (abfd, filehdr_in->f_opthdr, filehdr_out->f_opthdr);
847 H_PUT_16 (abfd, filehdr_in->f_flags, filehdr_out->f_flags);
849 return FILHSZ;
852 unsigned int
853 _bfd_XXi_swap_scnhdr_out (bfd * abfd, void * in, void * out)
855 struct internal_scnhdr *scnhdr_int = (struct internal_scnhdr *) in;
856 SCNHDR *scnhdr_ext = (SCNHDR *) out;
857 unsigned int ret = SCNHSZ;
858 bfd_vma ps;
859 bfd_vma ss;
861 memcpy (scnhdr_ext->s_name, scnhdr_int->s_name, sizeof (scnhdr_int->s_name));
863 PUT_SCNHDR_VADDR (abfd,
864 ((scnhdr_int->s_vaddr
865 - pe_data (abfd)->pe_opthdr.ImageBase)
866 & 0xffffffff),
867 scnhdr_ext->s_vaddr);
869 /* NT wants the size data to be rounded up to the next
870 NT_FILE_ALIGNMENT, but zero if it has no content (as in .bss,
871 sometimes). */
872 if ((scnhdr_int->s_flags & IMAGE_SCN_CNT_UNINITIALIZED_DATA) != 0)
874 if (bfd_pe_executable_p (abfd))
876 ps = scnhdr_int->s_size;
877 ss = 0;
879 else
881 ps = 0;
882 ss = scnhdr_int->s_size;
885 else
887 if (bfd_pe_executable_p (abfd))
888 ps = scnhdr_int->s_paddr;
889 else
890 ps = 0;
892 ss = scnhdr_int->s_size;
895 PUT_SCNHDR_SIZE (abfd, ss,
896 scnhdr_ext->s_size);
898 /* s_paddr in PE is really the virtual size. */
899 PUT_SCNHDR_PADDR (abfd, ps, scnhdr_ext->s_paddr);
901 PUT_SCNHDR_SCNPTR (abfd, scnhdr_int->s_scnptr,
902 scnhdr_ext->s_scnptr);
903 PUT_SCNHDR_RELPTR (abfd, scnhdr_int->s_relptr,
904 scnhdr_ext->s_relptr);
905 PUT_SCNHDR_LNNOPTR (abfd, scnhdr_int->s_lnnoptr,
906 scnhdr_ext->s_lnnoptr);
909 /* Extra flags must be set when dealing with PE. All sections should also
910 have the IMAGE_SCN_MEM_READ (0x40000000) flag set. In addition, the
911 .text section must have IMAGE_SCN_MEM_EXECUTE (0x20000000) and the data
912 sections (.idata, .data, .bss, .CRT) must have IMAGE_SCN_MEM_WRITE set
913 (this is especially important when dealing with the .idata section since
914 the addresses for routines from .dlls must be overwritten). If .reloc
915 section data is ever generated, we must add IMAGE_SCN_MEM_DISCARDABLE
916 (0x02000000). Also, the resource data should also be read and
917 writable. */
919 /* FIXME: Alignment is also encoded in this field, at least on PPC and
920 ARM-WINCE. Although - how do we get the original alignment field
921 back ? */
923 typedef struct
925 const char * section_name;
926 unsigned long must_have;
928 pe_required_section_flags;
930 pe_required_section_flags known_sections [] =
932 { ".arch", IMAGE_SCN_MEM_READ | IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_DISCARDABLE | IMAGE_SCN_ALIGN_8BYTES },
933 { ".bss", IMAGE_SCN_MEM_READ | IMAGE_SCN_CNT_UNINITIALIZED_DATA | IMAGE_SCN_MEM_WRITE },
934 { ".data", IMAGE_SCN_MEM_READ | IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_WRITE },
935 { ".edata", IMAGE_SCN_MEM_READ | IMAGE_SCN_CNT_INITIALIZED_DATA },
936 { ".idata", IMAGE_SCN_MEM_READ | IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_WRITE },
937 { ".pdata", IMAGE_SCN_MEM_READ | IMAGE_SCN_CNT_INITIALIZED_DATA },
938 { ".rdata", IMAGE_SCN_MEM_READ | IMAGE_SCN_CNT_INITIALIZED_DATA },
939 { ".reloc", IMAGE_SCN_MEM_READ | IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_DISCARDABLE },
940 { ".rsrc", IMAGE_SCN_MEM_READ | IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_WRITE },
941 { ".text" , IMAGE_SCN_MEM_READ | IMAGE_SCN_CNT_CODE | IMAGE_SCN_MEM_EXECUTE },
942 { ".tls", IMAGE_SCN_MEM_READ | IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_WRITE },
943 { ".xdata", IMAGE_SCN_MEM_READ | IMAGE_SCN_CNT_INITIALIZED_DATA },
944 { NULL, 0}
947 pe_required_section_flags * p;
949 /* We have defaulted to adding the IMAGE_SCN_MEM_WRITE flag, but now
950 we know exactly what this specific section wants so we remove it
951 and then allow the must_have field to add it back in if necessary.
952 However, we don't remove IMAGE_SCN_MEM_WRITE flag from .text if the
953 default WP_TEXT file flag has been cleared. WP_TEXT may be cleared
954 by ld --enable-auto-import (if auto-import is actually needed),
955 by ld --omagic, or by obcopy --writable-text. */
957 for (p = known_sections; p->section_name; p++)
958 if (strcmp (scnhdr_int->s_name, p->section_name) == 0)
960 if (strcmp (scnhdr_int->s_name, ".text")
961 || (bfd_get_file_flags (abfd) & WP_TEXT))
962 scnhdr_int->s_flags &= ~IMAGE_SCN_MEM_WRITE;
963 scnhdr_int->s_flags |= p->must_have;
964 break;
967 H_PUT_32 (abfd, scnhdr_int->s_flags, scnhdr_ext->s_flags);
970 if (coff_data (abfd)->link_info
971 && ! coff_data (abfd)->link_info->relocatable
972 && ! coff_data (abfd)->link_info->shared
973 && strcmp (scnhdr_int->s_name, ".text") == 0)
975 /* By inference from looking at MS output, the 32 bit field
976 which is the combination of the number_of_relocs and
977 number_of_linenos is used for the line number count in
978 executables. A 16-bit field won't do for cc1. The MS
979 document says that the number of relocs is zero for
980 executables, but the 17-th bit has been observed to be there.
981 Overflow is not an issue: a 4G-line program will overflow a
982 bunch of other fields long before this! */
983 H_PUT_16 (abfd, (scnhdr_int->s_nlnno & 0xffff), scnhdr_ext->s_nlnno);
984 H_PUT_16 (abfd, (scnhdr_int->s_nlnno >> 16), scnhdr_ext->s_nreloc);
986 else
988 if (scnhdr_int->s_nlnno <= 0xffff)
989 H_PUT_16 (abfd, scnhdr_int->s_nlnno, scnhdr_ext->s_nlnno);
990 else
992 (*_bfd_error_handler) (_("%s: line number overflow: 0x%lx > 0xffff"),
993 bfd_get_filename (abfd),
994 scnhdr_int->s_nlnno);
995 bfd_set_error (bfd_error_file_truncated);
996 H_PUT_16 (abfd, 0xffff, scnhdr_ext->s_nlnno);
997 ret = 0;
1000 /* Although we could encode 0xffff relocs here, we do not, to be
1001 consistent with other parts of bfd. Also it lets us warn, as
1002 we should never see 0xffff here w/o having the overflow flag
1003 set. */
1004 if (scnhdr_int->s_nreloc < 0xffff)
1005 H_PUT_16 (abfd, scnhdr_int->s_nreloc, scnhdr_ext->s_nreloc);
1006 else
1008 /* PE can deal with large #s of relocs, but not here. */
1009 H_PUT_16 (abfd, 0xffff, scnhdr_ext->s_nreloc);
1010 scnhdr_int->s_flags |= IMAGE_SCN_LNK_NRELOC_OVFL;
1011 H_PUT_32 (abfd, scnhdr_int->s_flags, scnhdr_ext->s_flags);
1014 return ret;
1017 static char * dir_names[IMAGE_NUMBEROF_DIRECTORY_ENTRIES] =
1019 N_("Export Directory [.edata (or where ever we found it)]"),
1020 N_("Import Directory [parts of .idata]"),
1021 N_("Resource Directory [.rsrc]"),
1022 N_("Exception Directory [.pdata]"),
1023 N_("Security Directory"),
1024 N_("Base Relocation Directory [.reloc]"),
1025 N_("Debug Directory"),
1026 N_("Description Directory"),
1027 N_("Special Directory"),
1028 N_("Thread Storage Directory [.tls]"),
1029 N_("Load Configuration Directory"),
1030 N_("Bound Import Directory"),
1031 N_("Import Address Table Directory"),
1032 N_("Delay Import Directory"),
1033 N_("CLR Runtime Header"),
1034 N_("Reserved")
1037 #ifdef POWERPC_LE_PE
1038 /* The code for the PPC really falls in the "architecture dependent"
1039 category. However, it's not clear that anyone will ever care, so
1040 we're ignoring the issue for now; if/when PPC matters, some of this
1041 may need to go into peicode.h, or arguments passed to enable the
1042 PPC- specific code. */
1043 #endif
1045 static bfd_boolean
1046 pe_print_idata (bfd * abfd, void * vfile)
1048 FILE *file = (FILE *) vfile;
1049 bfd_byte *data;
1050 asection *section;
1051 bfd_signed_vma adj;
1053 #ifdef POWERPC_LE_PE
1054 asection *rel_section = bfd_get_section_by_name (abfd, ".reldata");
1055 #endif
1057 bfd_size_type datasize = 0;
1058 bfd_size_type dataoff;
1059 bfd_size_type i;
1060 int onaline = 20;
1062 pe_data_type *pe = pe_data (abfd);
1063 struct internal_extra_pe_aouthdr *extra = &pe->pe_opthdr;
1065 bfd_vma addr;
1067 addr = extra->DataDirectory[PE_IMPORT_TABLE].VirtualAddress;
1069 if (addr == 0 && extra->DataDirectory[PE_IMPORT_TABLE].Size == 0)
1071 /* Maybe the extra header isn't there. Look for the section. */
1072 section = bfd_get_section_by_name (abfd, ".idata");
1073 if (section == NULL)
1074 return TRUE;
1076 addr = section->vma;
1077 datasize = section->size;
1078 if (datasize == 0)
1079 return TRUE;
1081 else
1083 addr += extra->ImageBase;
1084 for (section = abfd->sections; section != NULL; section = section->next)
1086 datasize = section->size;
1087 if (addr >= section->vma && addr < section->vma + datasize)
1088 break;
1091 if (section == NULL)
1093 fprintf (file,
1094 _("\nThere is an import table, but the section containing it could not be found\n"));
1095 return TRUE;
1099 fprintf (file, _("\nThere is an import table in %s at 0x%lx\n"),
1100 section->name, (unsigned long) addr);
1102 dataoff = addr - section->vma;
1103 datasize -= dataoff;
1105 #ifdef POWERPC_LE_PE
1106 if (rel_section != 0 && rel_section->size != 0)
1108 /* The toc address can be found by taking the starting address,
1109 which on the PPC locates a function descriptor. The
1110 descriptor consists of the function code starting address
1111 followed by the address of the toc. The starting address we
1112 get from the bfd, and the descriptor is supposed to be in the
1113 .reldata section. */
1115 bfd_vma loadable_toc_address;
1116 bfd_vma toc_address;
1117 bfd_vma start_address;
1118 bfd_byte *data;
1119 bfd_vma offset;
1121 if (!bfd_malloc_and_get_section (abfd, rel_section, &data))
1123 if (data != NULL)
1124 free (data);
1125 return FALSE;
1128 offset = abfd->start_address - rel_section->vma;
1130 if (offset >= rel_section->size || offset + 8 > rel_section->size)
1132 if (data != NULL)
1133 free (data);
1134 return FALSE;
1137 start_address = bfd_get_32 (abfd, data + offset);
1138 loadable_toc_address = bfd_get_32 (abfd, data + offset + 4);
1139 toc_address = loadable_toc_address - 32768;
1141 fprintf (file,
1142 _("\nFunction descriptor located at the start address: %04lx\n"),
1143 (unsigned long int) (abfd->start_address));
1144 fprintf (file,
1145 _("\tcode-base %08lx toc (loadable/actual) %08lx/%08lx\n"),
1146 start_address, loadable_toc_address, toc_address);
1147 if (data != NULL)
1148 free (data);
1150 else
1152 fprintf (file,
1153 _("\nNo reldata section! Function descriptor not decoded.\n"));
1155 #endif
1157 fprintf (file,
1158 _("\nThe Import Tables (interpreted %s section contents)\n"),
1159 section->name);
1160 fprintf (file,
1161 _("\
1162 vma: Hint Time Forward DLL First\n\
1163 Table Stamp Chain Name Thunk\n"));
1165 /* Read the whole section. Some of the fields might be before dataoff. */
1166 if (!bfd_malloc_and_get_section (abfd, section, &data))
1168 if (data != NULL)
1169 free (data);
1170 return FALSE;
1173 adj = section->vma - extra->ImageBase;
1175 /* Print all image import descriptors. */
1176 for (i = 0; i < datasize; i += onaline)
1178 bfd_vma hint_addr;
1179 bfd_vma time_stamp;
1180 bfd_vma forward_chain;
1181 bfd_vma dll_name;
1182 bfd_vma first_thunk;
1183 int idx = 0;
1184 bfd_size_type j;
1185 char *dll;
1187 /* Print (i + extra->DataDirectory[PE_IMPORT_TABLE].VirtualAddress). */
1188 fprintf (file, " %08lx\t", (unsigned long) (i + adj + dataoff));
1189 hint_addr = bfd_get_32 (abfd, data + i + dataoff);
1190 time_stamp = bfd_get_32 (abfd, data + i + 4 + dataoff);
1191 forward_chain = bfd_get_32 (abfd, data + i + 8 + dataoff);
1192 dll_name = bfd_get_32 (abfd, data + i + 12 + dataoff);
1193 first_thunk = bfd_get_32 (abfd, data + i + 16 + dataoff);
1195 fprintf (file, "%08lx %08lx %08lx %08lx %08lx\n",
1196 (unsigned long) hint_addr,
1197 (unsigned long) time_stamp,
1198 (unsigned long) forward_chain,
1199 (unsigned long) dll_name,
1200 (unsigned long) first_thunk);
1202 if (hint_addr == 0 && first_thunk == 0)
1203 break;
1205 if (dll_name - adj >= section->size)
1206 break;
1208 dll = (char *) data + dll_name - adj;
1209 fprintf (file, _("\n\tDLL Name: %s\n"), dll);
1211 if (hint_addr != 0)
1213 bfd_byte *ft_data;
1214 asection *ft_section;
1215 bfd_vma ft_addr;
1216 bfd_size_type ft_datasize;
1217 int ft_idx;
1218 int ft_allocated = 0;
1220 fprintf (file, _("\tvma: Hint/Ord Member-Name Bound-To\n"));
1222 idx = hint_addr - adj;
1224 ft_addr = first_thunk + extra->ImageBase;
1225 ft_data = data;
1226 ft_idx = first_thunk - adj;
1227 ft_allocated = 0;
1229 if (first_thunk != hint_addr)
1231 /* Find the section which contains the first thunk. */
1232 for (ft_section = abfd->sections;
1233 ft_section != NULL;
1234 ft_section = ft_section->next)
1236 ft_datasize = ft_section->size;
1237 if (ft_addr >= ft_section->vma
1238 && ft_addr < ft_section->vma + ft_datasize)
1239 break;
1242 if (ft_section == NULL)
1244 fprintf (file,
1245 _("\nThere is a first thunk, but the section containing it could not be found\n"));
1246 continue;
1249 /* Now check to see if this section is the same as our current
1250 section. If it is not then we will have to load its data in. */
1251 if (ft_section == section)
1253 ft_data = data;
1254 ft_idx = first_thunk - adj;
1256 else
1258 ft_idx = first_thunk - (ft_section->vma - extra->ImageBase);
1259 ft_data = bfd_malloc (datasize);
1260 if (ft_data == NULL)
1261 continue;
1263 /* Read datasize bfd_bytes starting at offset ft_idx. */
1264 if (! bfd_get_section_contents
1265 (abfd, ft_section, ft_data, (bfd_vma) ft_idx, datasize))
1267 free (ft_data);
1268 continue;
1271 ft_idx = 0;
1272 ft_allocated = 1;
1276 /* Print HintName vector entries. */
1277 #ifdef COFF_WITH_pex64
1278 for (j = 0; j < datasize; j += 8)
1280 unsigned long member = bfd_get_32 (abfd, data + idx + j);
1281 unsigned long member_high = bfd_get_32 (abfd, data + idx + j + 4);
1283 if (!member && !member_high)
1284 break;
1286 if (member_high & 0x80000000)
1287 fprintf (file, "\t%lx%08lx\t %4lx%08lx <none>",
1288 member_high,member, member_high & 0x7fffffff, member);
1289 else
1291 int ordinal;
1292 char *member_name;
1294 ordinal = bfd_get_16 (abfd, data + member - adj);
1295 member_name = (char *) data + member - adj + 2;
1296 fprintf (file, "\t%04lx\t %4d %s",member, ordinal, member_name);
1299 /* If the time stamp is not zero, the import address
1300 table holds actual addresses. */
1301 if (time_stamp != 0
1302 && first_thunk != 0
1303 && first_thunk != hint_addr)
1304 fprintf (file, "\t%04lx",
1305 (long) bfd_get_32 (abfd, ft_data + ft_idx + j));
1306 fprintf (file, "\n");
1308 #else
1309 for (j = 0; j < datasize; j += 4)
1311 unsigned long member = bfd_get_32 (abfd, data + idx + j);
1313 /* Print single IMAGE_IMPORT_BY_NAME vector. */
1314 if (member == 0)
1315 break;
1317 if (member & 0x80000000)
1318 fprintf (file, "\t%04lx\t %4lu <none>",
1319 member, member & 0x7fffffff);
1320 else
1322 int ordinal;
1323 char *member_name;
1325 ordinal = bfd_get_16 (abfd, data + member - adj);
1326 member_name = (char *) data + member - adj + 2;
1327 fprintf (file, "\t%04lx\t %4d %s",
1328 member, ordinal, member_name);
1331 /* If the time stamp is not zero, the import address
1332 table holds actual addresses. */
1333 if (time_stamp != 0
1334 && first_thunk != 0
1335 && first_thunk != hint_addr)
1336 fprintf (file, "\t%04lx",
1337 (long) bfd_get_32 (abfd, ft_data + ft_idx + j));
1339 fprintf (file, "\n");
1341 #endif
1342 if (ft_allocated)
1343 free (ft_data);
1346 fprintf (file, "\n");
1349 free (data);
1351 return TRUE;
1354 static bfd_boolean
1355 pe_print_edata (bfd * abfd, void * vfile)
1357 FILE *file = (FILE *) vfile;
1358 bfd_byte *data;
1359 asection *section;
1360 bfd_size_type datasize = 0;
1361 bfd_size_type dataoff;
1362 bfd_size_type i;
1363 bfd_signed_vma adj;
1364 struct EDT_type
1366 long export_flags; /* Reserved - should be zero. */
1367 long time_stamp;
1368 short major_ver;
1369 short minor_ver;
1370 bfd_vma name; /* RVA - relative to image base. */
1371 long base; /* Ordinal base. */
1372 unsigned long num_functions;/* Number in the export address table. */
1373 unsigned long num_names; /* Number in the name pointer table. */
1374 bfd_vma eat_addr; /* RVA to the export address table. */
1375 bfd_vma npt_addr; /* RVA to the Export Name Pointer Table. */
1376 bfd_vma ot_addr; /* RVA to the Ordinal Table. */
1377 } edt;
1379 pe_data_type *pe = pe_data (abfd);
1380 struct internal_extra_pe_aouthdr *extra = &pe->pe_opthdr;
1382 bfd_vma addr;
1384 addr = extra->DataDirectory[PE_EXPORT_TABLE].VirtualAddress;
1386 if (addr == 0 && extra->DataDirectory[PE_EXPORT_TABLE].Size == 0)
1388 /* Maybe the extra header isn't there. Look for the section. */
1389 section = bfd_get_section_by_name (abfd, ".edata");
1390 if (section == NULL)
1391 return TRUE;
1393 addr = section->vma;
1394 dataoff = 0;
1395 datasize = section->size;
1396 if (datasize == 0)
1397 return TRUE;
1399 else
1401 addr += extra->ImageBase;
1403 for (section = abfd->sections; section != NULL; section = section->next)
1404 if (addr >= section->vma && addr < section->vma + section->size)
1405 break;
1407 if (section == NULL)
1409 fprintf (file,
1410 _("\nThere is an export table, but the section containing it could not be found\n"));
1411 return TRUE;
1414 dataoff = addr - section->vma;
1415 datasize = extra->DataDirectory[PE_EXPORT_TABLE].Size;
1416 if (datasize > section->size - dataoff)
1418 fprintf (file,
1419 _("\nThere is an export table in %s, but it does not fit into that section\n"),
1420 section->name);
1421 return TRUE;
1425 fprintf (file, _("\nThere is an export table in %s at 0x%lx\n"),
1426 section->name, (unsigned long) addr);
1428 data = bfd_malloc (datasize);
1429 if (data == NULL)
1430 return FALSE;
1432 if (! bfd_get_section_contents (abfd, section, data,
1433 (file_ptr) dataoff, datasize))
1434 return FALSE;
1436 /* Go get Export Directory Table. */
1437 edt.export_flags = bfd_get_32 (abfd, data + 0);
1438 edt.time_stamp = bfd_get_32 (abfd, data + 4);
1439 edt.major_ver = bfd_get_16 (abfd, data + 8);
1440 edt.minor_ver = bfd_get_16 (abfd, data + 10);
1441 edt.name = bfd_get_32 (abfd, data + 12);
1442 edt.base = bfd_get_32 (abfd, data + 16);
1443 edt.num_functions = bfd_get_32 (abfd, data + 20);
1444 edt.num_names = bfd_get_32 (abfd, data + 24);
1445 edt.eat_addr = bfd_get_32 (abfd, data + 28);
1446 edt.npt_addr = bfd_get_32 (abfd, data + 32);
1447 edt.ot_addr = bfd_get_32 (abfd, data + 36);
1449 adj = section->vma - extra->ImageBase + dataoff;
1451 /* Dump the EDT first. */
1452 fprintf (file,
1453 _("\nThe Export Tables (interpreted %s section contents)\n\n"),
1454 section->name);
1456 fprintf (file,
1457 _("Export Flags \t\t\t%lx\n"), (unsigned long) edt.export_flags);
1459 fprintf (file,
1460 _("Time/Date stamp \t\t%lx\n"), (unsigned long) edt.time_stamp);
1462 fprintf (file,
1463 _("Major/Minor \t\t\t%d/%d\n"), edt.major_ver, edt.minor_ver);
1465 fprintf (file,
1466 _("Name \t\t\t\t"));
1467 fprintf_vma (file, edt.name);
1468 fprintf (file,
1469 " %s\n", data + edt.name - adj);
1471 fprintf (file,
1472 _("Ordinal Base \t\t\t%ld\n"), edt.base);
1474 fprintf (file,
1475 _("Number in:\n"));
1477 fprintf (file,
1478 _("\tExport Address Table \t\t%08lx\n"),
1479 edt.num_functions);
1481 fprintf (file,
1482 _("\t[Name Pointer/Ordinal] Table\t%08lx\n"), edt.num_names);
1484 fprintf (file,
1485 _("Table Addresses\n"));
1487 fprintf (file,
1488 _("\tExport Address Table \t\t"));
1489 fprintf_vma (file, edt.eat_addr);
1490 fprintf (file, "\n");
1492 fprintf (file,
1493 _("\tName Pointer Table \t\t"));
1494 fprintf_vma (file, edt.npt_addr);
1495 fprintf (file, "\n");
1497 fprintf (file,
1498 _("\tOrdinal Table \t\t\t"));
1499 fprintf_vma (file, edt.ot_addr);
1500 fprintf (file, "\n");
1502 /* The next table to find is the Export Address Table. It's basically
1503 a list of pointers that either locate a function in this dll, or
1504 forward the call to another dll. Something like:
1505 typedef union
1507 long export_rva;
1508 long forwarder_rva;
1509 } export_address_table_entry; */
1511 fprintf (file,
1512 _("\nExport Address Table -- Ordinal Base %ld\n"),
1513 edt.base);
1515 for (i = 0; i < edt.num_functions; ++i)
1517 bfd_vma eat_member = bfd_get_32 (abfd,
1518 data + edt.eat_addr + (i * 4) - adj);
1519 if (eat_member == 0)
1520 continue;
1522 if (eat_member - adj <= datasize)
1524 /* This rva is to a name (forwarding function) in our section. */
1525 /* Should locate a function descriptor. */
1526 fprintf (file,
1527 "\t[%4ld] +base[%4ld] %04lx %s -- %s\n",
1528 (long) i,
1529 (long) (i + edt.base),
1530 (unsigned long) eat_member,
1531 _("Forwarder RVA"),
1532 data + eat_member - adj);
1534 else
1536 /* Should locate a function descriptor in the reldata section. */
1537 fprintf (file,
1538 "\t[%4ld] +base[%4ld] %04lx %s\n",
1539 (long) i,
1540 (long) (i + edt.base),
1541 (unsigned long) eat_member,
1542 _("Export RVA"));
1546 /* The Export Name Pointer Table is paired with the Export Ordinal Table. */
1547 /* Dump them in parallel for clarity. */
1548 fprintf (file,
1549 _("\n[Ordinal/Name Pointer] Table\n"));
1551 for (i = 0; i < edt.num_names; ++i)
1553 bfd_vma name_ptr = bfd_get_32 (abfd,
1554 data +
1555 edt.npt_addr
1556 + (i*4) - adj);
1558 char *name = (char *) data + name_ptr - adj;
1560 bfd_vma ord = bfd_get_16 (abfd,
1561 data +
1562 edt.ot_addr
1563 + (i*2) - adj);
1564 fprintf (file,
1565 "\t[%4ld] %s\n", (long) ord, name);
1568 free (data);
1570 return TRUE;
1573 /* This really is architecture dependent. On IA-64, a .pdata entry
1574 consists of three dwords containing relative virtual addresses that
1575 specify the start and end address of the code range the entry
1576 covers and the address of the corresponding unwind info data. */
1578 static bfd_boolean
1579 pe_print_pdata (bfd * abfd, void * vfile)
1581 #if defined(COFF_WITH_pep) && !defined(COFF_WITH_pex64)
1582 # define PDATA_ROW_SIZE (3 * 8)
1583 #else
1584 # define PDATA_ROW_SIZE (5 * 4)
1585 #endif
1586 FILE *file = (FILE *) vfile;
1587 bfd_byte *data = 0;
1588 asection *section = bfd_get_section_by_name (abfd, ".pdata");
1589 bfd_size_type datasize = 0;
1590 bfd_size_type i;
1591 bfd_size_type start, stop;
1592 int onaline = PDATA_ROW_SIZE;
1594 if (section == NULL
1595 || coff_section_data (abfd, section) == NULL
1596 || pei_section_data (abfd, section) == NULL)
1597 return TRUE;
1599 stop = pei_section_data (abfd, section)->virt_size;
1600 if ((stop % onaline) != 0)
1601 fprintf (file,
1602 _("Warning, .pdata section size (%ld) is not a multiple of %d\n"),
1603 (long) stop, onaline);
1605 fprintf (file,
1606 _("\nThe Function Table (interpreted .pdata section contents)\n"));
1607 #if defined(COFF_WITH_pep) && !defined(COFF_WITH_pex64)
1608 fprintf (file,
1609 _(" vma:\t\t\tBegin Address End Address Unwind Info\n"));
1610 #else
1611 fprintf (file, _("\
1612 vma:\t\tBegin End EH EH PrologEnd Exception\n\
1613 \t\tAddress Address Handler Data Address Mask\n"));
1614 #endif
1616 datasize = section->size;
1617 if (datasize == 0)
1618 return TRUE;
1620 if (! bfd_malloc_and_get_section (abfd, section, &data))
1622 if (data != NULL)
1623 free (data);
1624 return FALSE;
1627 start = 0;
1629 for (i = start; i < stop; i += onaline)
1631 bfd_vma begin_addr;
1632 bfd_vma end_addr;
1633 bfd_vma eh_handler;
1634 bfd_vma eh_data;
1635 bfd_vma prolog_end_addr;
1636 int em_data;
1638 if (i + PDATA_ROW_SIZE > stop)
1639 break;
1641 begin_addr = GET_PDATA_ENTRY (abfd, data + i );
1642 end_addr = GET_PDATA_ENTRY (abfd, data + i + 4);
1643 eh_handler = GET_PDATA_ENTRY (abfd, data + i + 8);
1644 eh_data = GET_PDATA_ENTRY (abfd, data + i + 12);
1645 prolog_end_addr = GET_PDATA_ENTRY (abfd, data + i + 16);
1647 if (begin_addr == 0 && end_addr == 0 && eh_handler == 0
1648 && eh_data == 0 && prolog_end_addr == 0)
1649 /* We are probably into the padding of the section now. */
1650 break;
1652 em_data = ((eh_handler & 0x1) << 2) | (prolog_end_addr & 0x3);
1653 eh_handler &= ~(bfd_vma) 0x3;
1654 prolog_end_addr &= ~(bfd_vma) 0x3;
1656 fputc (' ', file);
1657 fprintf_vma (file, i + section->vma); fputc ('\t', file);
1658 fprintf_vma (file, begin_addr); fputc (' ', file);
1659 fprintf_vma (file, end_addr); fputc (' ', file);
1660 fprintf_vma (file, eh_handler);
1661 #if !defined(COFF_WITH_pep) || defined(COFF_WITH_pex64)
1662 fputc (' ', file);
1663 fprintf_vma (file, eh_data); fputc (' ', file);
1664 fprintf_vma (file, prolog_end_addr);
1665 fprintf (file, " %x", em_data);
1666 #endif
1668 #ifdef POWERPC_LE_PE
1669 if (eh_handler == 0 && eh_data != 0)
1671 /* Special bits here, although the meaning may be a little
1672 mysterious. The only one I know for sure is 0x03
1673 Code Significance
1674 0x00 None
1675 0x01 Register Save Millicode
1676 0x02 Register Restore Millicode
1677 0x03 Glue Code Sequence. */
1678 switch (eh_data)
1680 case 0x01:
1681 fprintf (file, _(" Register save millicode"));
1682 break;
1683 case 0x02:
1684 fprintf (file, _(" Register restore millicode"));
1685 break;
1686 case 0x03:
1687 fprintf (file, _(" Glue code sequence"));
1688 break;
1689 default:
1690 break;
1693 #endif
1694 fprintf (file, "\n");
1697 free (data);
1699 return TRUE;
1702 #define IMAGE_REL_BASED_HIGHADJ 4
1703 static const char * const tbl[] =
1705 "ABSOLUTE",
1706 "HIGH",
1707 "LOW",
1708 "HIGHLOW",
1709 "HIGHADJ",
1710 "MIPS_JMPADDR",
1711 "SECTION",
1712 "REL32",
1713 "RESERVED1",
1714 "MIPS_JMPADDR16",
1715 "DIR64",
1716 "HIGH3ADJ",
1717 "UNKNOWN", /* MUST be last. */
1720 static bfd_boolean
1721 pe_print_reloc (bfd * abfd, void * vfile)
1723 FILE *file = (FILE *) vfile;
1724 bfd_byte *data = 0;
1725 asection *section = bfd_get_section_by_name (abfd, ".reloc");
1726 bfd_size_type datasize;
1727 bfd_size_type i;
1728 bfd_size_type start, stop;
1730 if (section == NULL)
1731 return TRUE;
1733 if (section->size == 0)
1734 return TRUE;
1736 fprintf (file,
1737 _("\n\nPE File Base Relocations (interpreted .reloc section contents)\n"));
1739 datasize = section->size;
1740 if (! bfd_malloc_and_get_section (abfd, section, &data))
1742 if (data != NULL)
1743 free (data);
1744 return FALSE;
1747 start = 0;
1749 stop = section->size;
1751 for (i = start; i < stop;)
1753 int j;
1754 bfd_vma virtual_address;
1755 long number, size;
1757 /* The .reloc section is a sequence of blocks, with a header consisting
1758 of two 32 bit quantities, followed by a number of 16 bit entries. */
1759 virtual_address = bfd_get_32 (abfd, data+i);
1760 size = bfd_get_32 (abfd, data+i+4);
1761 number = (size - 8) / 2;
1763 if (size == 0)
1764 break;
1766 fprintf (file,
1767 _("\nVirtual Address: %08lx Chunk size %ld (0x%lx) Number of fixups %ld\n"),
1768 (unsigned long) virtual_address, size, size, number);
1770 for (j = 0; j < number; ++j)
1772 unsigned short e = bfd_get_16 (abfd, data + i + 8 + j * 2);
1773 unsigned int t = (e & 0xF000) >> 12;
1774 int off = e & 0x0FFF;
1776 if (t >= sizeof (tbl) / sizeof (tbl[0]))
1777 t = (sizeof (tbl) / sizeof (tbl[0])) - 1;
1779 fprintf (file,
1780 _("\treloc %4d offset %4x [%4lx] %s"),
1781 j, off, (long) (off + virtual_address), tbl[t]);
1783 /* HIGHADJ takes an argument, - the next record *is* the
1784 low 16 bits of addend. */
1785 if (t == IMAGE_REL_BASED_HIGHADJ)
1787 fprintf (file, " (%4x)",
1788 ((unsigned int)
1789 bfd_get_16 (abfd, data + i + 8 + j * 2 + 2)));
1790 j++;
1793 fprintf (file, "\n");
1796 i += size;
1799 free (data);
1801 return TRUE;
1804 /* Print out the program headers. */
1806 bfd_boolean
1807 _bfd_XX_print_private_bfd_data_common (bfd * abfd, void * vfile)
1809 FILE *file = (FILE *) vfile;
1810 int j;
1811 pe_data_type *pe = pe_data (abfd);
1812 struct internal_extra_pe_aouthdr *i = &pe->pe_opthdr;
1813 const char *subsystem_name = NULL;
1814 const char *name;
1816 /* The MS dumpbin program reportedly ands with 0xff0f before
1817 printing the characteristics field. Not sure why. No reason to
1818 emulate it here. */
1819 fprintf (file, _("\nCharacteristics 0x%x\n"), pe->real_flags);
1820 #undef PF
1821 #define PF(x, y) if (pe->real_flags & x) { fprintf (file, "\t%s\n", y); }
1822 PF (IMAGE_FILE_RELOCS_STRIPPED, "relocations stripped");
1823 PF (IMAGE_FILE_EXECUTABLE_IMAGE, "executable");
1824 PF (IMAGE_FILE_LINE_NUMS_STRIPPED, "line numbers stripped");
1825 PF (IMAGE_FILE_LOCAL_SYMS_STRIPPED, "symbols stripped");
1826 PF (IMAGE_FILE_LARGE_ADDRESS_AWARE, "large address aware");
1827 PF (IMAGE_FILE_BYTES_REVERSED_LO, "little endian");
1828 PF (IMAGE_FILE_32BIT_MACHINE, "32 bit words");
1829 PF (IMAGE_FILE_DEBUG_STRIPPED, "debugging information removed");
1830 PF (IMAGE_FILE_SYSTEM, "system file");
1831 PF (IMAGE_FILE_DLL, "DLL");
1832 PF (IMAGE_FILE_BYTES_REVERSED_HI, "big endian");
1833 #undef PF
1835 /* ctime implies '\n'. */
1837 time_t t = pe->coff.timestamp;
1838 fprintf (file, "\nTime/Date\t\t%s", ctime (&t));
1841 #ifndef IMAGE_NT_OPTIONAL_HDR_MAGIC
1842 # define IMAGE_NT_OPTIONAL_HDR_MAGIC 0x10b
1843 #endif
1844 #ifndef IMAGE_NT_OPTIONAL_HDR64_MAGIC
1845 # define IMAGE_NT_OPTIONAL_HDR64_MAGIC 0x20b
1846 #endif
1847 #ifndef IMAGE_NT_OPTIONAL_HDRROM_MAGIC
1848 # define IMAGE_NT_OPTIONAL_HDRROM_MAGIC 0x107
1849 #endif
1851 switch (i->Magic)
1853 case IMAGE_NT_OPTIONAL_HDR_MAGIC:
1854 name = "PE32";
1855 break;
1856 case IMAGE_NT_OPTIONAL_HDR64_MAGIC:
1857 name = "PE32+";
1858 break;
1859 case IMAGE_NT_OPTIONAL_HDRROM_MAGIC:
1860 name = "ROM";
1861 break;
1862 default:
1863 name = NULL;
1864 break;
1866 fprintf (file, "Magic\t\t\t%04x", i->Magic);
1867 if (name)
1868 fprintf (file, "\t(%s)",name);
1869 fprintf (file, "\nMajorLinkerVersion\t%d\n", i->MajorLinkerVersion);
1870 fprintf (file, "MinorLinkerVersion\t%d\n", i->MinorLinkerVersion);
1871 fprintf (file, "SizeOfCode\t\t%08lx\n", i->SizeOfCode);
1872 fprintf (file, "SizeOfInitializedData\t%08lx\n",
1873 i->SizeOfInitializedData);
1874 fprintf (file, "SizeOfUninitializedData\t%08lx\n",
1875 i->SizeOfUninitializedData);
1876 fprintf (file, "AddressOfEntryPoint\t");
1877 fprintf_vma (file, i->AddressOfEntryPoint);
1878 fprintf (file, "\nBaseOfCode\t\t");
1879 fprintf_vma (file, i->BaseOfCode);
1880 #if !defined(COFF_WITH_pep) && !defined(COFF_WITH_pex64)
1881 /* PE32+ does not have BaseOfData member! */
1882 fprintf (file, "\nBaseOfData\t\t");
1883 fprintf_vma (file, i->BaseOfData);
1884 #endif
1886 fprintf (file, "\nImageBase\t\t");
1887 fprintf_vma (file, i->ImageBase);
1888 fprintf (file, "\nSectionAlignment\t");
1889 fprintf_vma (file, i->SectionAlignment);
1890 fprintf (file, "\nFileAlignment\t\t");
1891 fprintf_vma (file, i->FileAlignment);
1892 fprintf (file, "\nMajorOSystemVersion\t%d\n", i->MajorOperatingSystemVersion);
1893 fprintf (file, "MinorOSystemVersion\t%d\n", i->MinorOperatingSystemVersion);
1894 fprintf (file, "MajorImageVersion\t%d\n", i->MajorImageVersion);
1895 fprintf (file, "MinorImageVersion\t%d\n", i->MinorImageVersion);
1896 fprintf (file, "MajorSubsystemVersion\t%d\n", i->MajorSubsystemVersion);
1897 fprintf (file, "MinorSubsystemVersion\t%d\n", i->MinorSubsystemVersion);
1898 fprintf (file, "Win32Version\t\t%08lx\n", i->Reserved1);
1899 fprintf (file, "SizeOfImage\t\t%08lx\n", i->SizeOfImage);
1900 fprintf (file, "SizeOfHeaders\t\t%08lx\n", i->SizeOfHeaders);
1901 fprintf (file, "CheckSum\t\t%08lx\n", i->CheckSum);
1903 switch (i->Subsystem)
1905 case IMAGE_SUBSYSTEM_UNKNOWN:
1906 subsystem_name = "unspecified";
1907 break;
1908 case IMAGE_SUBSYSTEM_NATIVE:
1909 subsystem_name = "NT native";
1910 break;
1911 case IMAGE_SUBSYSTEM_WINDOWS_GUI:
1912 subsystem_name = "Windows GUI";
1913 break;
1914 case IMAGE_SUBSYSTEM_WINDOWS_CUI:
1915 subsystem_name = "Windows CUI";
1916 break;
1917 case IMAGE_SUBSYSTEM_POSIX_CUI:
1918 subsystem_name = "POSIX CUI";
1919 break;
1920 case IMAGE_SUBSYSTEM_WINDOWS_CE_GUI:
1921 subsystem_name = "Wince CUI";
1922 break;
1923 case IMAGE_SUBSYSTEM_EFI_APPLICATION:
1924 subsystem_name = "EFI application";
1925 break;
1926 case IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER:
1927 subsystem_name = "EFI boot service driver";
1928 break;
1929 case IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER:
1930 subsystem_name = "EFI runtime driver";
1931 break;
1932 // These are from revision 8.0 of the MS PE/COFF spec
1933 case IMAGE_SUBSYSTEM_EFI_ROM:
1934 subsystem_name = "EFI ROM";
1935 break;
1936 case IMAGE_SUBSYSTEM_XBOX:
1937 subsystem_name = "XBOX";
1938 break;
1939 // Added default case for clarity - subsystem_name is NULL anyway.
1940 default:
1941 subsystem_name = NULL;
1944 fprintf (file, "Subsystem\t\t%08x", i->Subsystem);
1945 if (subsystem_name)
1946 fprintf (file, "\t(%s)", subsystem_name);
1947 fprintf (file, "\nDllCharacteristics\t%08x\n", i->DllCharacteristics);
1948 fprintf (file, "SizeOfStackReserve\t");
1949 fprintf_vma (file, i->SizeOfStackReserve);
1950 fprintf (file, "\nSizeOfStackCommit\t");
1951 fprintf_vma (file, i->SizeOfStackCommit);
1952 fprintf (file, "\nSizeOfHeapReserve\t");
1953 fprintf_vma (file, i->SizeOfHeapReserve);
1954 fprintf (file, "\nSizeOfHeapCommit\t");
1955 fprintf_vma (file, i->SizeOfHeapCommit);
1956 fprintf (file, "\nLoaderFlags\t\t%08lx\n", i->LoaderFlags);
1957 fprintf (file, "NumberOfRvaAndSizes\t%08lx\n", i->NumberOfRvaAndSizes);
1959 fprintf (file, "\nThe Data Directory\n");
1960 for (j = 0; j < IMAGE_NUMBEROF_DIRECTORY_ENTRIES; j++)
1962 fprintf (file, "Entry %1x ", j);
1963 fprintf_vma (file, i->DataDirectory[j].VirtualAddress);
1964 fprintf (file, " %08lx ", i->DataDirectory[j].Size);
1965 fprintf (file, "%s\n", dir_names[j]);
1968 pe_print_idata (abfd, vfile);
1969 pe_print_edata (abfd, vfile);
1970 pe_print_pdata (abfd, vfile);
1971 pe_print_reloc (abfd, vfile);
1973 return TRUE;
1976 /* Copy any private info we understand from the input bfd
1977 to the output bfd. */
1979 bfd_boolean
1980 _bfd_XX_bfd_copy_private_bfd_data_common (bfd * ibfd, bfd * obfd)
1982 /* One day we may try to grok other private data. */
1983 if (ibfd->xvec->flavour != bfd_target_coff_flavour
1984 || obfd->xvec->flavour != bfd_target_coff_flavour)
1985 return TRUE;
1987 pe_data (obfd)->pe_opthdr = pe_data (ibfd)->pe_opthdr;
1988 pe_data (obfd)->dll = pe_data (ibfd)->dll;
1990 /* For strip: if we removed .reloc, we'll make a real mess of things
1991 if we don't remove this entry as well. */
1992 if (! pe_data (obfd)->has_reloc_section)
1994 pe_data (obfd)->pe_opthdr.DataDirectory[PE_BASE_RELOCATION_TABLE].VirtualAddress = 0;
1995 pe_data (obfd)->pe_opthdr.DataDirectory[PE_BASE_RELOCATION_TABLE].Size = 0;
1997 return TRUE;
2000 /* Copy private section data. */
2002 bfd_boolean
2003 _bfd_XX_bfd_copy_private_section_data (bfd *ibfd,
2004 asection *isec,
2005 bfd *obfd,
2006 asection *osec)
2008 if (bfd_get_flavour (ibfd) != bfd_target_coff_flavour
2009 || bfd_get_flavour (obfd) != bfd_target_coff_flavour)
2010 return TRUE;
2012 if (coff_section_data (ibfd, isec) != NULL
2013 && pei_section_data (ibfd, isec) != NULL)
2015 if (coff_section_data (obfd, osec) == NULL)
2017 bfd_size_type amt = sizeof (struct coff_section_tdata);
2018 osec->used_by_bfd = bfd_zalloc (obfd, amt);
2019 if (osec->used_by_bfd == NULL)
2020 return FALSE;
2023 if (pei_section_data (obfd, osec) == NULL)
2025 bfd_size_type amt = sizeof (struct pei_section_tdata);
2026 coff_section_data (obfd, osec)->tdata = bfd_zalloc (obfd, amt);
2027 if (coff_section_data (obfd, osec)->tdata == NULL)
2028 return FALSE;
2031 pei_section_data (obfd, osec)->virt_size =
2032 pei_section_data (ibfd, isec)->virt_size;
2033 pei_section_data (obfd, osec)->pe_flags =
2034 pei_section_data (ibfd, isec)->pe_flags;
2037 return TRUE;
2040 void
2041 _bfd_XX_get_symbol_info (bfd * abfd, asymbol *symbol, symbol_info *ret)
2043 coff_get_symbol_info (abfd, symbol, ret);
2046 /* Handle the .idata section and other things that need symbol table
2047 access. */
2049 bfd_boolean
2050 _bfd_XXi_final_link_postscript (bfd * abfd, struct coff_final_link_info *pfinfo)
2052 struct coff_link_hash_entry *h1;
2053 struct bfd_link_info *info = pfinfo->info;
2054 bfd_boolean result = TRUE;
2056 /* There are a few fields that need to be filled in now while we
2057 have symbol table access.
2059 The .idata subsections aren't directly available as sections, but
2060 they are in the symbol table, so get them from there. */
2062 /* The import directory. This is the address of .idata$2, with size
2063 of .idata$2 + .idata$3. */
2064 h1 = coff_link_hash_lookup (coff_hash_table (info),
2065 ".idata$2", FALSE, FALSE, TRUE);
2066 if (h1 != NULL)
2068 /* PR ld/2729: We cannot rely upon all the output sections having been
2069 created properly, so check before referencing them. Issue a warning
2070 message for any sections tht could not be found. */
2071 if (h1->root.u.def.section != NULL
2072 && h1->root.u.def.section->output_section != NULL)
2073 pe_data (abfd)->pe_opthdr.DataDirectory[PE_IMPORT_TABLE].VirtualAddress =
2074 (h1->root.u.def.value
2075 + h1->root.u.def.section->output_section->vma
2076 + h1->root.u.def.section->output_offset);
2077 else
2079 _bfd_error_handler
2080 (_("%B: unable to fill in DataDictionary[1] because .idata$2 is missing"),
2081 abfd);
2082 result = FALSE;
2085 h1 = coff_link_hash_lookup (coff_hash_table (info),
2086 ".idata$4", FALSE, FALSE, TRUE);
2087 if (h1 != NULL
2088 && h1->root.u.def.section != NULL
2089 && h1->root.u.def.section->output_section != NULL)
2090 pe_data (abfd)->pe_opthdr.DataDirectory[PE_IMPORT_TABLE].Size =
2091 ((h1->root.u.def.value
2092 + h1->root.u.def.section->output_section->vma
2093 + h1->root.u.def.section->output_offset)
2094 - pe_data (abfd)->pe_opthdr.DataDirectory[PE_IMPORT_TABLE].VirtualAddress);
2095 else
2097 _bfd_error_handler
2098 (_("%B: unable to fill in DataDictionary[1] because .idata$4 is missing"),
2099 abfd);
2100 result = FALSE;
2103 /* The import address table. This is the size/address of
2104 .idata$5. */
2105 h1 = coff_link_hash_lookup (coff_hash_table (info),
2106 ".idata$5", FALSE, FALSE, TRUE);
2107 if (h1 != NULL
2108 && h1->root.u.def.section != NULL
2109 && h1->root.u.def.section->output_section != NULL)
2110 pe_data (abfd)->pe_opthdr.DataDirectory[PE_IMPORT_ADDRESS_TABLE].VirtualAddress =
2111 (h1->root.u.def.value
2112 + h1->root.u.def.section->output_section->vma
2113 + h1->root.u.def.section->output_offset);
2114 else
2116 _bfd_error_handler
2117 (_("%B: unable to fill in DataDictionary[12] because .idata$5 is missing"),
2118 abfd);
2119 result = FALSE;
2122 h1 = coff_link_hash_lookup (coff_hash_table (info),
2123 ".idata$6", FALSE, FALSE, TRUE);
2124 if (h1 != NULL
2125 && h1->root.u.def.section != NULL
2126 && h1->root.u.def.section->output_section != NULL)
2127 pe_data (abfd)->pe_opthdr.DataDirectory[PE_IMPORT_ADDRESS_TABLE].Size =
2128 ((h1->root.u.def.value
2129 + h1->root.u.def.section->output_section->vma
2130 + h1->root.u.def.section->output_offset)
2131 - pe_data (abfd)->pe_opthdr.DataDirectory[PE_IMPORT_ADDRESS_TABLE].VirtualAddress);
2132 else
2134 _bfd_error_handler
2135 (_("%B: unable to fill in DataDictionary[PE_IMPORT_ADDRESS_TABLE (12)] because .idata$6 is missing"),
2136 abfd);
2137 result = FALSE;
2141 h1 = coff_link_hash_lookup (coff_hash_table (info),
2142 "__tls_used", FALSE, FALSE, TRUE);
2143 if (h1 != NULL)
2145 if (h1->root.u.def.section != NULL
2146 && h1->root.u.def.section->output_section != NULL)
2147 pe_data (abfd)->pe_opthdr.DataDirectory[PE_TLS_TABLE].VirtualAddress =
2148 (h1->root.u.def.value
2149 + h1->root.u.def.section->output_section->vma
2150 + h1->root.u.def.section->output_offset
2151 - pe_data (abfd)->pe_opthdr.ImageBase);
2152 else
2154 _bfd_error_handler
2155 (_("%B: unable to fill in DataDictionary[9] because __tls_used is missing"),
2156 abfd);
2157 result = FALSE;
2160 pe_data (abfd)->pe_opthdr.DataDirectory[PE_TLS_TABLE].Size = 0x18;
2163 /* If we couldn't find idata$2, we either have an excessively
2164 trivial program or are in DEEP trouble; we have to assume trivial
2165 program.... */
2166 return result;