bfd/
[deliverable/binutils-gdb.git] / bfd / peXXigen.c
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.
5
6 This file is part of BFD, the Binary File Descriptor library.
7
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.
12
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.
17
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. */
21
22 /* Most of this hacked by Steve Chamberlain <sac@cygnus.com>.
23
24 PE/PEI rearrangement (and code added): Donn Terry
25 Softway Systems, Inc. */
26
27 /* Hey look, some documentation [and in a place you expect to find it]!
28
29 The main reference for the pei format is "Microsoft Portable Executable
30 and Common Object File Format Specification 4.1". Get it if you need to
31 do some serious hacking on this code.
32
33 Another reference:
34 "Peering Inside the PE: A Tour of the Win32 Portable Executable
35 File Format", MSJ 1994, Volume 9.
36
37 The *sole* difference between the pe format and the pei format is that the
38 latter has an MSDOS 2.0 .exe header on the front that prints the message
39 "This app must be run under Windows." (or some such).
40 (FIXME: Whether that statement is *really* true or not is unknown.
41 Are there more subtle differences between pe and pei formats?
42 For now assume there aren't. If you find one, then for God sakes
43 document it here!)
44
45 The Microsoft docs use the word "image" instead of "executable" because
46 the former can also refer to a DLL (shared library). Confusion can arise
47 because the `i' in `pei' also refers to "image". The `pe' format can
48 also create images (i.e. executables), it's just that to run on a win32
49 system you need to use the pei format.
50
51 FIXME: Please add more docs here so the next poor fool that has to hack
52 on this code has a chance of getting something accomplished without
53 wasting too much time. */
54
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
58
59 #include "bfd.h"
60 #include "sysdep.h"
61 #include "libbfd.h"
62 #include "coff/internal.h"
63
64 /* NOTE: it's strange to be including an architecture specific header
65 in what's supposed to be general (to PE/PEI) code. However, that's
66 where the definitions are, and they don't vary per architecture
67 within PE/PEI, so we get them from there. FIXME: The lack of
68 variance is an assumption which may prove to be incorrect if new
69 PE/PEI targets are created. */
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
77
78 #include "coff/pe.h"
79 #include "libcoff.h"
80 #include "libpei.h"
81
82 #if defined COFF_WITH_pep || defined COFF_WITH_pex64
83 # undef AOUTSZ
84 # define AOUTSZ PEPAOUTSZ
85 # define PEAOUTHDR PEPAOUTHDR
86 #endif
87
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. */
93 \f
94 void
95 _bfd_XXi_swap_sym_in (bfd * abfd, void * ext1, void * in1)
96 {
97 SYMENT *ext = (SYMENT *) ext1;
98 struct internal_syment *in = (struct internal_syment *) in1;
99
100 if (ext->e.e_name[0] == 0)
101 {
102 in->_n._n_n._n_zeroes = 0;
103 in->_n._n_n._n_offset = H_GET_32 (abfd, ext->e.e.e_offset);
104 }
105 else
106 memcpy (in->_n._n_name, ext->e.e_name, SYMNMLEN);
107
108 in->n_value = H_GET_32 (abfd, ext->e_value);
109 in->n_scnum = H_GET_16 (abfd, ext->e_scnum);
110
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);
115
116 in->n_sclass = H_GET_8 (abfd, ext->e_sclass);
117 in->n_numaux = H_GET_8 (abfd, ext->e_numaux);
118
119 #ifndef STRICT_PE_FORMAT
120 /* This is for Gnu-created DLLs. */
121
122 /* The section symbols for the .idata$ sections have class 0x68
123 (C_SECTION), which MS documentation indicates is a section
124 symbol. Unfortunately, the value field in the symbol is simply a
125 copy of the .idata section's flags rather than something useful.
126 When these symbols are encountered, change the value to 0 so that
127 they will be handled somewhat correctly in the bfd code. */
128 if (in->n_sclass == C_SECTION)
129 {
130 in->n_value = 0x0;
131
132 /* Create synthetic empty sections as needed. DJ */
133 if (in->n_scnum == 0)
134 {
135 asection *sec;
136
137 for (sec = abfd->sections; sec; sec = sec->next)
138 {
139 if (strcmp (sec->name, in->n_name) == 0)
140 {
141 in->n_scnum = sec->target_index;
142 break;
143 }
144 }
145 }
146
147 if (in->n_scnum == 0)
148 {
149 int unused_section_number = 0;
150 asection *sec;
151 char *name;
152 flagword flags;
153
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;
157
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);
164
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;
176
177 sec->target_index = unused_section_number;
178
179 in->n_scnum = unused_section_number;
180 }
181 in->n_sclass = C_STAT;
182 }
183 #endif
184
185 #ifdef coff_swap_sym_in_hook
186 /* This won't work in peigen.c, but since it's for PPC PE, it's not
187 worth fixing. */
188 coff_swap_sym_in_hook (abfd, ext1, in1);
189 #endif
190 }
191
192 unsigned int
193 _bfd_XXi_swap_sym_out (bfd * abfd, void * inp, void * extp)
194 {
195 struct internal_syment *in = (struct internal_syment *) inp;
196 SYMENT *ext = (SYMENT *) extp;
197
198 if (in->_n._n_name[0] == 0)
199 {
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);
202 }
203 else
204 memcpy (ext->e.e_name, in->_n._n_name, SYMNMLEN);
205
206 H_PUT_32 (abfd, in->n_value, ext->e_value);
207 H_PUT_16 (abfd, in->n_scnum, ext->e_scnum);
208
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);
213
214 H_PUT_8 (abfd, in->n_sclass, ext->e_sclass);
215 H_PUT_8 (abfd, in->n_numaux, ext->e_numaux);
216
217 return SYMESZ;
218 }
219
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)
228 {
229 AUXENT *ext = (AUXENT *) ext1;
230 union internal_auxent *in = (union internal_auxent *) in1;
231
232 switch (class)
233 {
234 case C_FILE:
235 if (ext->x_file.x_fname[0] == 0)
236 {
237 in->x_file.x_n.x_zeroes = 0;
238 in->x_file.x_n.x_offset = H_GET_32 (abfd, ext->x_file.x_n.x_offset);
239 }
240 else
241 memcpy (in->x_file.x_fname, ext->x_file.x_fname, FILNMLEN);
242 return;
243
244 case C_STAT:
245 case C_LEAFSTAT:
246 case C_HIDDEN:
247 if (type == T_NULL)
248 {
249 in->x_scn.x_scnlen = GET_SCN_SCNLEN (abfd, ext);
250 in->x_scn.x_nreloc = GET_SCN_NRELOC (abfd, ext);
251 in->x_scn.x_nlinno = GET_SCN_NLINNO (abfd, ext);
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;
256 }
257 break;
258 }
259
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);
262
263 if (class == C_BLOCK || class == C_FCN || ISFCN (type) || ISTAG (class))
264 {
265 in->x_sym.x_fcnary.x_fcn.x_lnnoptr = GET_FCN_LNNOPTR (abfd, ext);
266 in->x_sym.x_fcnary.x_fcn.x_endndx.l = GET_FCN_ENDNDX (abfd, ext);
267 }
268 else
269 {
270 in->x_sym.x_fcnary.x_ary.x_dimen[0] =
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]);
278 }
279
280 if (ISFCN (type))
281 {
282 in->x_sym.x_misc.x_fsize = H_GET_32 (abfd, ext->x_sym.x_misc.x_fsize);
283 }
284 else
285 {
286 in->x_sym.x_misc.x_lnsz.x_lnno = GET_LNSZ_LNNO (abfd, ext);
287 in->x_sym.x_misc.x_lnsz.x_size = GET_LNSZ_SIZE (abfd, ext);
288 }
289 }
290
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)
299 {
300 union internal_auxent *in = (union internal_auxent *) inp;
301 AUXENT *ext = (AUXENT *) extp;
302
303 memset (ext, 0, AUXESZ);
304
305 switch (class)
306 {
307 case C_FILE:
308 if (in->x_file.x_fname[0] == 0)
309 {
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);
312 }
313 else
314 memcpy (ext->x_file.x_fname, in->x_file.x_fname, FILNMLEN);
315
316 return AUXESZ;
317
318 case C_STAT:
319 case C_LEAFSTAT:
320 case C_HIDDEN:
321 if (type == T_NULL)
322 {
323 PUT_SCN_SCNLEN (abfd, in->x_scn.x_scnlen, ext);
324 PUT_SCN_NRELOC (abfd, in->x_scn.x_nreloc, ext);
325 PUT_SCN_NLINNO (abfd, in->x_scn.x_nlinno, ext);
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;
330 }
331 break;
332 }
333
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);
336
337 if (class == C_BLOCK || class == C_FCN || ISFCN (type) || ISTAG (class))
338 {
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);
341 }
342 else
343 {
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]);
352 }
353
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
357 {
358 PUT_LNSZ_LNNO (abfd, in->x_sym.x_misc.x_lnsz.x_lnno, ext);
359 PUT_LNSZ_SIZE (abfd, in->x_sym.x_misc.x_lnsz.x_size, ext);
360 }
361
362 return AUXESZ;
363 }
364
365 void
366 _bfd_XXi_swap_lineno_in (bfd * abfd, void * ext1, void * in1)
367 {
368 LINENO *ext = (LINENO *) ext1;
369 struct internal_lineno *in = (struct internal_lineno *) in1;
370
371 in->l_addr.l_symndx = H_GET_32 (abfd, ext->l_addr.l_symndx);
372 in->l_lnno = GET_LINENO_LNNO (abfd, ext);
373 }
374
375 unsigned int
376 _bfd_XXi_swap_lineno_out (bfd * abfd, void * inp, void * outp)
377 {
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);
381
382 PUT_LINENO_LNNO (abfd, in->l_lnno, ext);
383 return LINESZ;
384 }
385
386 void
387 _bfd_XXi_swap_aouthdr_in (bfd * abfd,
388 void * aouthdr_ext1,
389 void * aouthdr_int1)
390 {
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;
396
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
411
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);
447
448 {
449 int idx;
450
451 for (idx = 0; idx < 16; idx++)
452 {
453 /* If data directory is empty, rva also should be 0. */
454 int size =
455 H_GET_32 (abfd, src->DataDirectory[idx][1]);
456
457 a->DataDirectory[idx].Size = size;
458
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;
464 }
465 }
466
467 if (aouthdr_int->entry)
468 {
469 aouthdr_int->entry += a->ImageBase;
470 #if !defined(COFF_WITH_pep) && !defined(COFF_WITH_pex64)
471 aouthdr_int->entry &= 0xffffffff;
472 #endif
473 }
474
475 if (aouthdr_int->tsize)
476 {
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
481 }
482
483 #if !defined(COFF_WITH_pep) && !defined(COFF_WITH_pex64)
484 /* PE32+ does not have data_start member! */
485 if (aouthdr_int->dsize)
486 {
487 aouthdr_int->data_start += a->ImageBase;
488 aouthdr_int->data_start &= 0xffffffff;
489 }
490 #endif
491
492 #ifdef POWERPC_LE_PE
493 /* These three fields are normally set up by ppc_relocate_section.
494 In the case of reading a file in, we can pick them up from the
495 DataDirectory. */
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
500 }
501
502 /* A support function for below. */
503
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)
510 {
511 asection *sec = bfd_get_section_by_name (abfd, name);
512
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))
517 {
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;
521
522 if (size)
523 {
524 aout->DataDirectory[idx].VirtualAddress =
525 (sec->vma - base) & 0xffffffff;
526 sec->flags |= SEC_DATA;
527 }
528 }
529 }
530
531 unsigned int
532 _bfd_XXi_swap_aouthdr_out (bfd * abfd, void * in, void * out)
533 {
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;
540
541 if (pe->force_minimum_alignment)
542 {
543 if (!extra->FileAlignment)
544 extra->FileAlignment = PE_DEF_FILE_ALIGNMENT;
545 if (!extra->SectionAlignment)
546 extra->SectionAlignment = PE_DEF_SECTION_ALIGNMENT;
547 }
548
549 if (extra->Subsystem == IMAGE_SUBSYSTEM_UNKNOWN)
550 extra->Subsystem = pe->target_subsystem;
551
552 sa = extra->SectionAlignment;
553 fa = extra->FileAlignment;
554 ib = extra->ImageBase;
555
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];
559
560 if (aouthdr_in->tsize)
561 {
562 aouthdr_in->text_start -= ib;
563 #if !defined(COFF_WITH_pep) && !defined(COFF_WITH_pex64)
564 aouthdr_in->text_start &= 0xffffffff;
565 #endif
566 }
567
568 if (aouthdr_in->dsize)
569 {
570 aouthdr_in->data_start -= ib;
571 #if !defined(COFF_WITH_pep) && !defined(COFF_WITH_pex64)
572 aouthdr_in->data_start &= 0xffffffff;
573 #endif
574 }
575
576 if (aouthdr_in->entry)
577 {
578 aouthdr_in->entry -= ib;
579 #if !defined(COFF_WITH_pep) && !defined(COFF_WITH_pex64)
580 aouthdr_in->entry &= 0xffffffff;
581 #endif
582 }
583
584 #define FA(x) (((x) + fa -1 ) & (- fa))
585 #define SA(x) (((x) + sa -1 ) & (- sa))
586
587 /* We like to have the sizes aligned. */
588 aouthdr_in->bsize = FA (aouthdr_in->bsize);
589
590 extra->NumberOfRvaAndSizes = IMAGE_NUMBEROF_DIRECTORY_ENTRIES;
591
592 /* First null out all data directory entries. */
593 memset (extra->DataDirectory, 0, sizeof (extra->DataDirectory));
594
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);
598
599 /* In theory we do not need to call add_data_entry for .idata$2 or
600 .idata$5. It will be done in bfd_coff_final_link where all the
601 required information is available. If however, we are not going
602 to perform a final link, eg because we have been invoked by objcopy
603 or strip, then we need to make sure that these Data Directory
604 entries are initialised properly.
605
606 So - we copy the input values into the output values, and then, if
607 a final link is going to be performed, it can overwrite them. */
608 extra->DataDirectory[PE_IMPORT_TABLE] = idata2;
609 extra->DataDirectory[PE_IMPORT_ADDRESS_TABLE] = idata5;
610 extra->DataDirectory[PE_TLS_TABLE] = tls;
611
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);
616
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);
624
625 {
626 asection *sec;
627 bfd_vma hsize = 0;
628 bfd_vma dsize = 0;
629 bfd_vma isize = 0;
630 bfd_vma tsize = 0;
631
632 for (sec = abfd->sections; sec; sec = sec->next)
633 {
634 int rounded = FA (sec->size);
635
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
650 FIXME: We need to handle holes between sections, which may
651 happpen when we covert from another format. We just use
652 the virtual address and virtual size of the last section
653 for the image size. */
654 if (coff_section_data (abfd, sec) != NULL
655 && pei_section_data (abfd, sec) != NULL)
656 isize = (sec->vma - extra->ImageBase
657 + SA (FA (pei_section_data (abfd, sec)->virt_size)));
658 }
659
660 aouthdr_in->dsize = dsize;
661 aouthdr_in->tsize = tsize;
662 extra->SizeOfHeaders = hsize;
663 extra->SizeOfImage = isize;
664 }
665
666 H_PUT_16 (abfd, aouthdr_in->magic, aouthdr_out->standard.magic);
667
668 #define LINKER_VERSION 256 /* That is, 2.56 */
669
670 /* This piece of magic sets the "linker version" field to
671 LINKER_VERSION. */
672 H_PUT_16 (abfd, (LINKER_VERSION / 100 + (LINKER_VERSION % 100) * 256),
673 aouthdr_out->standard.vstamp);
674
675 PUT_AOUTHDR_TSIZE (abfd, aouthdr_in->tsize, aouthdr_out->standard.tsize);
676 PUT_AOUTHDR_DSIZE (abfd, aouthdr_in->dsize, aouthdr_out->standard.dsize);
677 PUT_AOUTHDR_BSIZE (abfd, aouthdr_in->bsize, aouthdr_out->standard.bsize);
678 PUT_AOUTHDR_ENTRY (abfd, aouthdr_in->entry, aouthdr_out->standard.entry);
679 PUT_AOUTHDR_TEXT_START (abfd, aouthdr_in->text_start,
680 aouthdr_out->standard.text_start);
681
682 #if !defined(COFF_WITH_pep) && !defined(COFF_WITH_pex64)
683 /* PE32+ does not have data_start member! */
684 PUT_AOUTHDR_DATA_START (abfd, aouthdr_in->data_start,
685 aouthdr_out->standard.data_start);
686 #endif
687
688 PUT_OPTHDR_IMAGE_BASE (abfd, extra->ImageBase, aouthdr_out->ImageBase);
689 H_PUT_32 (abfd, extra->SectionAlignment, aouthdr_out->SectionAlignment);
690 H_PUT_32 (abfd, extra->FileAlignment, aouthdr_out->FileAlignment);
691 H_PUT_16 (abfd, extra->MajorOperatingSystemVersion,
692 aouthdr_out->MajorOperatingSystemVersion);
693 H_PUT_16 (abfd, extra->MinorOperatingSystemVersion,
694 aouthdr_out->MinorOperatingSystemVersion);
695 H_PUT_16 (abfd, extra->MajorImageVersion, aouthdr_out->MajorImageVersion);
696 H_PUT_16 (abfd, extra->MinorImageVersion, aouthdr_out->MinorImageVersion);
697 H_PUT_16 (abfd, extra->MajorSubsystemVersion,
698 aouthdr_out->MajorSubsystemVersion);
699 H_PUT_16 (abfd, extra->MinorSubsystemVersion,
700 aouthdr_out->MinorSubsystemVersion);
701 H_PUT_32 (abfd, extra->Reserved1, aouthdr_out->Reserved1);
702 H_PUT_32 (abfd, extra->SizeOfImage, aouthdr_out->SizeOfImage);
703 H_PUT_32 (abfd, extra->SizeOfHeaders, aouthdr_out->SizeOfHeaders);
704 H_PUT_32 (abfd, extra->CheckSum, aouthdr_out->CheckSum);
705 H_PUT_16 (abfd, extra->Subsystem, aouthdr_out->Subsystem);
706 H_PUT_16 (abfd, extra->DllCharacteristics, aouthdr_out->DllCharacteristics);
707 PUT_OPTHDR_SIZE_OF_STACK_RESERVE (abfd, extra->SizeOfStackReserve,
708 aouthdr_out->SizeOfStackReserve);
709 PUT_OPTHDR_SIZE_OF_STACK_COMMIT (abfd, extra->SizeOfStackCommit,
710 aouthdr_out->SizeOfStackCommit);
711 PUT_OPTHDR_SIZE_OF_HEAP_RESERVE (abfd, extra->SizeOfHeapReserve,
712 aouthdr_out->SizeOfHeapReserve);
713 PUT_OPTHDR_SIZE_OF_HEAP_COMMIT (abfd, extra->SizeOfHeapCommit,
714 aouthdr_out->SizeOfHeapCommit);
715 H_PUT_32 (abfd, extra->LoaderFlags, aouthdr_out->LoaderFlags);
716 H_PUT_32 (abfd, extra->NumberOfRvaAndSizes,
717 aouthdr_out->NumberOfRvaAndSizes);
718 {
719 int idx;
720
721 for (idx = 0; idx < 16; idx++)
722 {
723 H_PUT_32 (abfd, extra->DataDirectory[idx].VirtualAddress,
724 aouthdr_out->DataDirectory[idx][0]);
725 H_PUT_32 (abfd, extra->DataDirectory[idx].Size,
726 aouthdr_out->DataDirectory[idx][1]);
727 }
728 }
729
730 return AOUTSZ;
731 }
732
733 unsigned int
734 _bfd_XXi_only_swap_filehdr_out (bfd * abfd, void * in, void * out)
735 {
736 int idx;
737 struct internal_filehdr *filehdr_in = (struct internal_filehdr *) in;
738 struct external_PEI_filehdr *filehdr_out = (struct external_PEI_filehdr *) out;
739
740 if (pe_data (abfd)->has_reloc_section)
741 filehdr_in->f_flags &= ~F_RELFLG;
742
743 if (pe_data (abfd)->dll)
744 filehdr_in->f_flags |= F_DLL;
745
746 filehdr_in->pe.e_magic = DOSMAGIC;
747 filehdr_in->pe.e_cblp = 0x90;
748 filehdr_in->pe.e_cp = 0x3;
749 filehdr_in->pe.e_crlc = 0x0;
750 filehdr_in->pe.e_cparhdr = 0x4;
751 filehdr_in->pe.e_minalloc = 0x0;
752 filehdr_in->pe.e_maxalloc = 0xffff;
753 filehdr_in->pe.e_ss = 0x0;
754 filehdr_in->pe.e_sp = 0xb8;
755 filehdr_in->pe.e_csum = 0x0;
756 filehdr_in->pe.e_ip = 0x0;
757 filehdr_in->pe.e_cs = 0x0;
758 filehdr_in->pe.e_lfarlc = 0x40;
759 filehdr_in->pe.e_ovno = 0x0;
760
761 for (idx = 0; idx < 4; idx++)
762 filehdr_in->pe.e_res[idx] = 0x0;
763
764 filehdr_in->pe.e_oemid = 0x0;
765 filehdr_in->pe.e_oeminfo = 0x0;
766
767 for (idx = 0; idx < 10; idx++)
768 filehdr_in->pe.e_res2[idx] = 0x0;
769
770 filehdr_in->pe.e_lfanew = 0x80;
771
772 /* This next collection of data are mostly just characters. It
773 appears to be constant within the headers put on NT exes. */
774 filehdr_in->pe.dos_message[0] = 0x0eba1f0e;
775 filehdr_in->pe.dos_message[1] = 0xcd09b400;
776 filehdr_in->pe.dos_message[2] = 0x4c01b821;
777 filehdr_in->pe.dos_message[3] = 0x685421cd;
778 filehdr_in->pe.dos_message[4] = 0x70207369;
779 filehdr_in->pe.dos_message[5] = 0x72676f72;
780 filehdr_in->pe.dos_message[6] = 0x63206d61;
781 filehdr_in->pe.dos_message[7] = 0x6f6e6e61;
782 filehdr_in->pe.dos_message[8] = 0x65622074;
783 filehdr_in->pe.dos_message[9] = 0x6e757220;
784 filehdr_in->pe.dos_message[10] = 0x206e6920;
785 filehdr_in->pe.dos_message[11] = 0x20534f44;
786 filehdr_in->pe.dos_message[12] = 0x65646f6d;
787 filehdr_in->pe.dos_message[13] = 0x0a0d0d2e;
788 filehdr_in->pe.dos_message[14] = 0x24;
789 filehdr_in->pe.dos_message[15] = 0x0;
790 filehdr_in->pe.nt_signature = NT_SIGNATURE;
791
792 H_PUT_16 (abfd, filehdr_in->f_magic, filehdr_out->f_magic);
793 H_PUT_16 (abfd, filehdr_in->f_nscns, filehdr_out->f_nscns);
794
795 H_PUT_32 (abfd, time (0), filehdr_out->f_timdat);
796 PUT_FILEHDR_SYMPTR (abfd, filehdr_in->f_symptr,
797 filehdr_out->f_symptr);
798 H_PUT_32 (abfd, filehdr_in->f_nsyms, filehdr_out->f_nsyms);
799 H_PUT_16 (abfd, filehdr_in->f_opthdr, filehdr_out->f_opthdr);
800 H_PUT_16 (abfd, filehdr_in->f_flags, filehdr_out->f_flags);
801
802 /* Put in extra dos header stuff. This data remains essentially
803 constant, it just has to be tacked on to the beginning of all exes
804 for NT. */
805 H_PUT_16 (abfd, filehdr_in->pe.e_magic, filehdr_out->e_magic);
806 H_PUT_16 (abfd, filehdr_in->pe.e_cblp, filehdr_out->e_cblp);
807 H_PUT_16 (abfd, filehdr_in->pe.e_cp, filehdr_out->e_cp);
808 H_PUT_16 (abfd, filehdr_in->pe.e_crlc, filehdr_out->e_crlc);
809 H_PUT_16 (abfd, filehdr_in->pe.e_cparhdr, filehdr_out->e_cparhdr);
810 H_PUT_16 (abfd, filehdr_in->pe.e_minalloc, filehdr_out->e_minalloc);
811 H_PUT_16 (abfd, filehdr_in->pe.e_maxalloc, filehdr_out->e_maxalloc);
812 H_PUT_16 (abfd, filehdr_in->pe.e_ss, filehdr_out->e_ss);
813 H_PUT_16 (abfd, filehdr_in->pe.e_sp, filehdr_out->e_sp);
814 H_PUT_16 (abfd, filehdr_in->pe.e_csum, filehdr_out->e_csum);
815 H_PUT_16 (abfd, filehdr_in->pe.e_ip, filehdr_out->e_ip);
816 H_PUT_16 (abfd, filehdr_in->pe.e_cs, filehdr_out->e_cs);
817 H_PUT_16 (abfd, filehdr_in->pe.e_lfarlc, filehdr_out->e_lfarlc);
818 H_PUT_16 (abfd, filehdr_in->pe.e_ovno, filehdr_out->e_ovno);
819
820 for (idx = 0; idx < 4; idx++)
821 H_PUT_16 (abfd, filehdr_in->pe.e_res[idx], filehdr_out->e_res[idx]);
822
823 H_PUT_16 (abfd, filehdr_in->pe.e_oemid, filehdr_out->e_oemid);
824 H_PUT_16 (abfd, filehdr_in->pe.e_oeminfo, filehdr_out->e_oeminfo);
825
826 for (idx = 0; idx < 10; idx++)
827 H_PUT_16 (abfd, filehdr_in->pe.e_res2[idx], filehdr_out->e_res2[idx]);
828
829 H_PUT_32 (abfd, filehdr_in->pe.e_lfanew, filehdr_out->e_lfanew);
830
831 for (idx = 0; idx < 16; idx++)
832 H_PUT_32 (abfd, filehdr_in->pe.dos_message[idx],
833 filehdr_out->dos_message[idx]);
834
835 /* Also put in the NT signature. */
836 H_PUT_32 (abfd, filehdr_in->pe.nt_signature, filehdr_out->nt_signature);
837
838 return FILHSZ;
839 }
840
841 unsigned int
842 _bfd_XX_only_swap_filehdr_out (bfd * abfd, void * in, void * out)
843 {
844 struct internal_filehdr *filehdr_in = (struct internal_filehdr *) in;
845 FILHDR *filehdr_out = (FILHDR *) out;
846
847 H_PUT_16 (abfd, filehdr_in->f_magic, filehdr_out->f_magic);
848 H_PUT_16 (abfd, filehdr_in->f_nscns, filehdr_out->f_nscns);
849 H_PUT_32 (abfd, filehdr_in->f_timdat, filehdr_out->f_timdat);
850 PUT_FILEHDR_SYMPTR (abfd, filehdr_in->f_symptr, filehdr_out->f_symptr);
851 H_PUT_32 (abfd, filehdr_in->f_nsyms, filehdr_out->f_nsyms);
852 H_PUT_16 (abfd, filehdr_in->f_opthdr, filehdr_out->f_opthdr);
853 H_PUT_16 (abfd, filehdr_in->f_flags, filehdr_out->f_flags);
854
855 return FILHSZ;
856 }
857
858 unsigned int
859 _bfd_XXi_swap_scnhdr_out (bfd * abfd, void * in, void * out)
860 {
861 struct internal_scnhdr *scnhdr_int = (struct internal_scnhdr *) in;
862 SCNHDR *scnhdr_ext = (SCNHDR *) out;
863 unsigned int ret = SCNHSZ;
864 bfd_vma ps;
865 bfd_vma ss;
866
867 memcpy (scnhdr_ext->s_name, scnhdr_int->s_name, sizeof (scnhdr_int->s_name));
868
869 PUT_SCNHDR_VADDR (abfd,
870 ((scnhdr_int->s_vaddr
871 - pe_data (abfd)->pe_opthdr.ImageBase)
872 & 0xffffffff),
873 scnhdr_ext->s_vaddr);
874
875 /* NT wants the size data to be rounded up to the next
876 NT_FILE_ALIGNMENT, but zero if it has no content (as in .bss,
877 sometimes). */
878 if ((scnhdr_int->s_flags & IMAGE_SCN_CNT_UNINITIALIZED_DATA) != 0)
879 {
880 if (bfd_pe_executable_p (abfd))
881 {
882 ps = scnhdr_int->s_size;
883 ss = 0;
884 }
885 else
886 {
887 ps = 0;
888 ss = scnhdr_int->s_size;
889 }
890 }
891 else
892 {
893 if (bfd_pe_executable_p (abfd))
894 ps = scnhdr_int->s_paddr;
895 else
896 ps = 0;
897
898 ss = scnhdr_int->s_size;
899 }
900
901 PUT_SCNHDR_SIZE (abfd, ss,
902 scnhdr_ext->s_size);
903
904 /* s_paddr in PE is really the virtual size. */
905 PUT_SCNHDR_PADDR (abfd, ps, scnhdr_ext->s_paddr);
906
907 PUT_SCNHDR_SCNPTR (abfd, scnhdr_int->s_scnptr,
908 scnhdr_ext->s_scnptr);
909 PUT_SCNHDR_RELPTR (abfd, scnhdr_int->s_relptr,
910 scnhdr_ext->s_relptr);
911 PUT_SCNHDR_LNNOPTR (abfd, scnhdr_int->s_lnnoptr,
912 scnhdr_ext->s_lnnoptr);
913
914 {
915 /* Extra flags must be set when dealing with PE. All sections should also
916 have the IMAGE_SCN_MEM_READ (0x40000000) flag set. In addition, the
917 .text section must have IMAGE_SCN_MEM_EXECUTE (0x20000000) and the data
918 sections (.idata, .data, .bss, .CRT) must have IMAGE_SCN_MEM_WRITE set
919 (this is especially important when dealing with the .idata section since
920 the addresses for routines from .dlls must be overwritten). If .reloc
921 section data is ever generated, we must add IMAGE_SCN_MEM_DISCARDABLE
922 (0x02000000). Also, the resource data should also be read and
923 writable. */
924
925 /* FIXME: Alignment is also encoded in this field, at least on PPC and
926 ARM-WINCE. Although - how do we get the original alignment field
927 back ? */
928
929 typedef struct
930 {
931 const char * section_name;
932 unsigned long must_have;
933 }
934 pe_required_section_flags;
935
936 pe_required_section_flags known_sections [] =
937 {
938 { ".arch", IMAGE_SCN_MEM_READ | IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_DISCARDABLE | IMAGE_SCN_ALIGN_8BYTES },
939 { ".bss", IMAGE_SCN_MEM_READ | IMAGE_SCN_CNT_UNINITIALIZED_DATA | IMAGE_SCN_MEM_WRITE },
940 { ".data", IMAGE_SCN_MEM_READ | IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_WRITE },
941 { ".edata", IMAGE_SCN_MEM_READ | IMAGE_SCN_CNT_INITIALIZED_DATA },
942 { ".idata", IMAGE_SCN_MEM_READ | IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_WRITE },
943 { ".pdata", IMAGE_SCN_MEM_READ | IMAGE_SCN_CNT_INITIALIZED_DATA },
944 { ".rdata", IMAGE_SCN_MEM_READ | IMAGE_SCN_CNT_INITIALIZED_DATA },
945 { ".reloc", IMAGE_SCN_MEM_READ | IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_DISCARDABLE },
946 { ".rsrc", IMAGE_SCN_MEM_READ | IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_WRITE },
947 { ".text" , IMAGE_SCN_MEM_READ | IMAGE_SCN_CNT_CODE | IMAGE_SCN_MEM_EXECUTE },
948 { ".tls", IMAGE_SCN_MEM_READ | IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_WRITE },
949 { ".xdata", IMAGE_SCN_MEM_READ | IMAGE_SCN_CNT_INITIALIZED_DATA },
950 { NULL, 0}
951 };
952
953 pe_required_section_flags * p;
954
955 /* We have defaulted to adding the IMAGE_SCN_MEM_WRITE flag, but now
956 we know exactly what this specific section wants so we remove it
957 and then allow the must_have field to add it back in if necessary.
958 However, we don't remove IMAGE_SCN_MEM_WRITE flag from .text if the
959 default WP_TEXT file flag has been cleared. WP_TEXT may be cleared
960 by ld --enable-auto-import (if auto-import is actually needed),
961 by ld --omagic, or by obcopy --writable-text. */
962
963 for (p = known_sections; p->section_name; p++)
964 if (strcmp (scnhdr_int->s_name, p->section_name) == 0)
965 {
966 if (strcmp (scnhdr_int->s_name, ".text")
967 || (bfd_get_file_flags (abfd) & WP_TEXT))
968 scnhdr_int->s_flags &= ~IMAGE_SCN_MEM_WRITE;
969 scnhdr_int->s_flags |= p->must_have;
970 break;
971 }
972
973 H_PUT_32 (abfd, scnhdr_int->s_flags, scnhdr_ext->s_flags);
974 }
975
976 if (coff_data (abfd)->link_info
977 && ! coff_data (abfd)->link_info->relocatable
978 && ! coff_data (abfd)->link_info->shared
979 && strcmp (scnhdr_int->s_name, ".text") == 0)
980 {
981 /* By inference from looking at MS output, the 32 bit field
982 which is the combination of the number_of_relocs and
983 number_of_linenos is used for the line number count in
984 executables. A 16-bit field won't do for cc1. The MS
985 document says that the number of relocs is zero for
986 executables, but the 17-th bit has been observed to be there.
987 Overflow is not an issue: a 4G-line program will overflow a
988 bunch of other fields long before this! */
989 H_PUT_16 (abfd, (scnhdr_int->s_nlnno & 0xffff), scnhdr_ext->s_nlnno);
990 H_PUT_16 (abfd, (scnhdr_int->s_nlnno >> 16), scnhdr_ext->s_nreloc);
991 }
992 else
993 {
994 if (scnhdr_int->s_nlnno <= 0xffff)
995 H_PUT_16 (abfd, scnhdr_int->s_nlnno, scnhdr_ext->s_nlnno);
996 else
997 {
998 (*_bfd_error_handler) (_("%s: line number overflow: 0x%lx > 0xffff"),
999 bfd_get_filename (abfd),
1000 scnhdr_int->s_nlnno);
1001 bfd_set_error (bfd_error_file_truncated);
1002 H_PUT_16 (abfd, 0xffff, scnhdr_ext->s_nlnno);
1003 ret = 0;
1004 }
1005
1006 /* Although we could encode 0xffff relocs here, we do not, to be
1007 consistent with other parts of bfd. Also it lets us warn, as
1008 we should never see 0xffff here w/o having the overflow flag
1009 set. */
1010 if (scnhdr_int->s_nreloc < 0xffff)
1011 H_PUT_16 (abfd, scnhdr_int->s_nreloc, scnhdr_ext->s_nreloc);
1012 else
1013 {
1014 /* PE can deal with large #s of relocs, but not here. */
1015 H_PUT_16 (abfd, 0xffff, scnhdr_ext->s_nreloc);
1016 scnhdr_int->s_flags |= IMAGE_SCN_LNK_NRELOC_OVFL;
1017 H_PUT_32 (abfd, scnhdr_int->s_flags, scnhdr_ext->s_flags);
1018 }
1019 }
1020 return ret;
1021 }
1022
1023 static char * dir_names[IMAGE_NUMBEROF_DIRECTORY_ENTRIES] =
1024 {
1025 N_("Export Directory [.edata (or where ever we found it)]"),
1026 N_("Import Directory [parts of .idata]"),
1027 N_("Resource Directory [.rsrc]"),
1028 N_("Exception Directory [.pdata]"),
1029 N_("Security Directory"),
1030 N_("Base Relocation Directory [.reloc]"),
1031 N_("Debug Directory"),
1032 N_("Description Directory"),
1033 N_("Special Directory"),
1034 N_("Thread Storage Directory [.tls]"),
1035 N_("Load Configuration Directory"),
1036 N_("Bound Import Directory"),
1037 N_("Import Address Table Directory"),
1038 N_("Delay Import Directory"),
1039 N_("CLR Runtime Header"),
1040 N_("Reserved")
1041 };
1042
1043 #ifdef POWERPC_LE_PE
1044 /* The code for the PPC really falls in the "architecture dependent"
1045 category. However, it's not clear that anyone will ever care, so
1046 we're ignoring the issue for now; if/when PPC matters, some of this
1047 may need to go into peicode.h, or arguments passed to enable the
1048 PPC- specific code. */
1049 #endif
1050
1051 static bfd_boolean
1052 pe_print_idata (bfd * abfd, void * vfile)
1053 {
1054 FILE *file = (FILE *) vfile;
1055 bfd_byte *data;
1056 asection *section;
1057 bfd_signed_vma adj;
1058
1059 #ifdef POWERPC_LE_PE
1060 asection *rel_section = bfd_get_section_by_name (abfd, ".reldata");
1061 #endif
1062
1063 bfd_size_type datasize = 0;
1064 bfd_size_type dataoff;
1065 bfd_size_type i;
1066 int onaline = 20;
1067
1068 pe_data_type *pe = pe_data (abfd);
1069 struct internal_extra_pe_aouthdr *extra = &pe->pe_opthdr;
1070
1071 bfd_vma addr;
1072
1073 addr = extra->DataDirectory[PE_IMPORT_TABLE].VirtualAddress;
1074
1075 if (addr == 0 && extra->DataDirectory[PE_IMPORT_TABLE].Size == 0)
1076 {
1077 /* Maybe the extra header isn't there. Look for the section. */
1078 section = bfd_get_section_by_name (abfd, ".idata");
1079 if (section == NULL)
1080 return TRUE;
1081
1082 addr = section->vma;
1083 datasize = section->size;
1084 if (datasize == 0)
1085 return TRUE;
1086 }
1087 else
1088 {
1089 addr += extra->ImageBase;
1090 for (section = abfd->sections; section != NULL; section = section->next)
1091 {
1092 datasize = section->size;
1093 if (addr >= section->vma && addr < section->vma + datasize)
1094 break;
1095 }
1096
1097 if (section == NULL)
1098 {
1099 fprintf (file,
1100 _("\nThere is an import table, but the section containing it could not be found\n"));
1101 return TRUE;
1102 }
1103 }
1104
1105 fprintf (file, _("\nThere is an import table in %s at 0x%lx\n"),
1106 section->name, (unsigned long) addr);
1107
1108 dataoff = addr - section->vma;
1109 datasize -= dataoff;
1110
1111 #ifdef POWERPC_LE_PE
1112 if (rel_section != 0 && rel_section->size != 0)
1113 {
1114 /* The toc address can be found by taking the starting address,
1115 which on the PPC locates a function descriptor. The
1116 descriptor consists of the function code starting address
1117 followed by the address of the toc. The starting address we
1118 get from the bfd, and the descriptor is supposed to be in the
1119 .reldata section. */
1120
1121 bfd_vma loadable_toc_address;
1122 bfd_vma toc_address;
1123 bfd_vma start_address;
1124 bfd_byte *data;
1125 bfd_vma offset;
1126
1127 if (!bfd_malloc_and_get_section (abfd, rel_section, &data))
1128 {
1129 if (data != NULL)
1130 free (data);
1131 return FALSE;
1132 }
1133
1134 offset = abfd->start_address - rel_section->vma;
1135
1136 if (offset >= rel_section->size || offset + 8 > rel_section->size)
1137 {
1138 if (data != NULL)
1139 free (data);
1140 return FALSE;
1141 }
1142
1143 start_address = bfd_get_32 (abfd, data + offset);
1144 loadable_toc_address = bfd_get_32 (abfd, data + offset + 4);
1145 toc_address = loadable_toc_address - 32768;
1146
1147 fprintf (file,
1148 _("\nFunction descriptor located at the start address: %04lx\n"),
1149 (unsigned long int) (abfd->start_address));
1150 fprintf (file,
1151 _("\tcode-base %08lx toc (loadable/actual) %08lx/%08lx\n"),
1152 start_address, loadable_toc_address, toc_address);
1153 if (data != NULL)
1154 free (data);
1155 }
1156 else
1157 {
1158 fprintf (file,
1159 _("\nNo reldata section! Function descriptor not decoded.\n"));
1160 }
1161 #endif
1162
1163 fprintf (file,
1164 _("\nThe Import Tables (interpreted %s section contents)\n"),
1165 section->name);
1166 fprintf (file,
1167 _("\
1168 vma: Hint Time Forward DLL First\n\
1169 Table Stamp Chain Name Thunk\n"));
1170
1171 /* Read the whole section. Some of the fields might be before dataoff. */
1172 if (!bfd_malloc_and_get_section (abfd, section, &data))
1173 {
1174 if (data != NULL)
1175 free (data);
1176 return FALSE;
1177 }
1178
1179 adj = section->vma - extra->ImageBase;
1180
1181 /* Print all image import descriptors. */
1182 for (i = 0; i < datasize; i += onaline)
1183 {
1184 bfd_vma hint_addr;
1185 bfd_vma time_stamp;
1186 bfd_vma forward_chain;
1187 bfd_vma dll_name;
1188 bfd_vma first_thunk;
1189 int idx = 0;
1190 bfd_size_type j;
1191 char *dll;
1192
1193 /* Print (i + extra->DataDirectory[PE_IMPORT_TABLE].VirtualAddress). */
1194 fprintf (file, " %08lx\t", (unsigned long) (i + adj + dataoff));
1195 hint_addr = bfd_get_32 (abfd, data + i + dataoff);
1196 time_stamp = bfd_get_32 (abfd, data + i + 4 + dataoff);
1197 forward_chain = bfd_get_32 (abfd, data + i + 8 + dataoff);
1198 dll_name = bfd_get_32 (abfd, data + i + 12 + dataoff);
1199 first_thunk = bfd_get_32 (abfd, data + i + 16 + dataoff);
1200
1201 fprintf (file, "%08lx %08lx %08lx %08lx %08lx\n",
1202 (unsigned long) hint_addr,
1203 (unsigned long) time_stamp,
1204 (unsigned long) forward_chain,
1205 (unsigned long) dll_name,
1206 (unsigned long) first_thunk);
1207
1208 if (hint_addr == 0 && first_thunk == 0)
1209 break;
1210
1211 if (dll_name - adj >= section->size)
1212 break;
1213
1214 dll = (char *) data + dll_name - adj;
1215 fprintf (file, _("\n\tDLL Name: %s\n"), dll);
1216
1217 if (hint_addr != 0)
1218 {
1219 bfd_byte *ft_data;
1220 asection *ft_section;
1221 bfd_vma ft_addr;
1222 bfd_size_type ft_datasize;
1223 int ft_idx;
1224 int ft_allocated = 0;
1225
1226 fprintf (file, _("\tvma: Hint/Ord Member-Name Bound-To\n"));
1227
1228 idx = hint_addr - adj;
1229
1230 ft_addr = first_thunk + extra->ImageBase;
1231 ft_data = data;
1232 ft_idx = first_thunk - adj;
1233 ft_allocated = 0;
1234
1235 if (first_thunk != hint_addr)
1236 {
1237 /* Find the section which contains the first thunk. */
1238 for (ft_section = abfd->sections;
1239 ft_section != NULL;
1240 ft_section = ft_section->next)
1241 {
1242 ft_datasize = ft_section->size;
1243 if (ft_addr >= ft_section->vma
1244 && ft_addr < ft_section->vma + ft_datasize)
1245 break;
1246 }
1247
1248 if (ft_section == NULL)
1249 {
1250 fprintf (file,
1251 _("\nThere is a first thunk, but the section containing it could not be found\n"));
1252 continue;
1253 }
1254
1255 /* Now check to see if this section is the same as our current
1256 section. If it is not then we will have to load its data in. */
1257 if (ft_section == section)
1258 {
1259 ft_data = data;
1260 ft_idx = first_thunk - adj;
1261 }
1262 else
1263 {
1264 ft_idx = first_thunk - (ft_section->vma - extra->ImageBase);
1265 ft_data = bfd_malloc (datasize);
1266 if (ft_data == NULL)
1267 continue;
1268
1269 /* Read datasize bfd_bytes starting at offset ft_idx. */
1270 if (! bfd_get_section_contents
1271 (abfd, ft_section, ft_data, (bfd_vma) ft_idx, datasize))
1272 {
1273 free (ft_data);
1274 continue;
1275 }
1276
1277 ft_idx = 0;
1278 ft_allocated = 1;
1279 }
1280 }
1281
1282 /* Print HintName vector entries. */
1283 #ifdef COFF_WITH_pex64
1284 for (j = 0; j < datasize; j += 8)
1285 {
1286 unsigned long member = bfd_get_32 (abfd, data + idx + j);
1287 unsigned long member_high = bfd_get_32 (abfd, data + idx + j + 4);
1288
1289 if (!member && !member_high)
1290 break;
1291
1292 if (member_high & 0x80000000)
1293 fprintf (file, "\t%lx%08lx\t %4lx%08lx <none>",
1294 member_high,member, member_high & 0x7fffffff, member);
1295 else
1296 {
1297 int ordinal;
1298 char *member_name;
1299
1300 ordinal = bfd_get_16 (abfd, data + member - adj);
1301 member_name = (char *) data + member - adj + 2;
1302 fprintf (file, "\t%04lx\t %4d %s",member, ordinal, member_name);
1303 }
1304
1305 /* If the time stamp is not zero, the import address
1306 table holds actual addresses. */
1307 if (time_stamp != 0
1308 && first_thunk != 0
1309 && first_thunk != hint_addr)
1310 fprintf (file, "\t%04lx",
1311 (long) bfd_get_32 (abfd, ft_data + ft_idx + j));
1312 fprintf (file, "\n");
1313 }
1314 #else
1315 for (j = 0; j < datasize; j += 4)
1316 {
1317 unsigned long member = bfd_get_32 (abfd, data + idx + j);
1318
1319 /* Print single IMAGE_IMPORT_BY_NAME vector. */
1320 if (member == 0)
1321 break;
1322
1323 if (member & 0x80000000)
1324 fprintf (file, "\t%04lx\t %4lu <none>",
1325 member, member & 0x7fffffff);
1326 else
1327 {
1328 int ordinal;
1329 char *member_name;
1330
1331 ordinal = bfd_get_16 (abfd, data + member - adj);
1332 member_name = (char *) data + member - adj + 2;
1333 fprintf (file, "\t%04lx\t %4d %s",
1334 member, ordinal, member_name);
1335 }
1336
1337 /* If the time stamp is not zero, the import address
1338 table holds actual addresses. */
1339 if (time_stamp != 0
1340 && first_thunk != 0
1341 && first_thunk != hint_addr)
1342 fprintf (file, "\t%04lx",
1343 (long) bfd_get_32 (abfd, ft_data + ft_idx + j));
1344
1345 fprintf (file, "\n");
1346 }
1347 #endif
1348 if (ft_allocated)
1349 free (ft_data);
1350 }
1351
1352 fprintf (file, "\n");
1353 }
1354
1355 free (data);
1356
1357 return TRUE;
1358 }
1359
1360 static bfd_boolean
1361 pe_print_edata (bfd * abfd, void * vfile)
1362 {
1363 FILE *file = (FILE *) vfile;
1364 bfd_byte *data;
1365 asection *section;
1366 bfd_size_type datasize = 0;
1367 bfd_size_type dataoff;
1368 bfd_size_type i;
1369 bfd_signed_vma adj;
1370 struct EDT_type
1371 {
1372 long export_flags; /* Reserved - should be zero. */
1373 long time_stamp;
1374 short major_ver;
1375 short minor_ver;
1376 bfd_vma name; /* RVA - relative to image base. */
1377 long base; /* Ordinal base. */
1378 unsigned long num_functions;/* Number in the export address table. */
1379 unsigned long num_names; /* Number in the name pointer table. */
1380 bfd_vma eat_addr; /* RVA to the export address table. */
1381 bfd_vma npt_addr; /* RVA to the Export Name Pointer Table. */
1382 bfd_vma ot_addr; /* RVA to the Ordinal Table. */
1383 } edt;
1384
1385 pe_data_type *pe = pe_data (abfd);
1386 struct internal_extra_pe_aouthdr *extra = &pe->pe_opthdr;
1387
1388 bfd_vma addr;
1389
1390 addr = extra->DataDirectory[PE_EXPORT_TABLE].VirtualAddress;
1391
1392 if (addr == 0 && extra->DataDirectory[PE_EXPORT_TABLE].Size == 0)
1393 {
1394 /* Maybe the extra header isn't there. Look for the section. */
1395 section = bfd_get_section_by_name (abfd, ".edata");
1396 if (section == NULL)
1397 return TRUE;
1398
1399 addr = section->vma;
1400 dataoff = 0;
1401 datasize = section->size;
1402 if (datasize == 0)
1403 return TRUE;
1404 }
1405 else
1406 {
1407 addr += extra->ImageBase;
1408
1409 for (section = abfd->sections; section != NULL; section = section->next)
1410 if (addr >= section->vma && addr < section->vma + section->size)
1411 break;
1412
1413 if (section == NULL)
1414 {
1415 fprintf (file,
1416 _("\nThere is an export table, but the section containing it could not be found\n"));
1417 return TRUE;
1418 }
1419
1420 dataoff = addr - section->vma;
1421 datasize = extra->DataDirectory[PE_EXPORT_TABLE].Size;
1422 if (datasize > section->size - dataoff)
1423 {
1424 fprintf (file,
1425 _("\nThere is an export table in %s, but it does not fit into that section\n"),
1426 section->name);
1427 return TRUE;
1428 }
1429 }
1430
1431 fprintf (file, _("\nThere is an export table in %s at 0x%lx\n"),
1432 section->name, (unsigned long) addr);
1433
1434 data = bfd_malloc (datasize);
1435 if (data == NULL)
1436 return FALSE;
1437
1438 if (! bfd_get_section_contents (abfd, section, data,
1439 (file_ptr) dataoff, datasize))
1440 return FALSE;
1441
1442 /* Go get Export Directory Table. */
1443 edt.export_flags = bfd_get_32 (abfd, data + 0);
1444 edt.time_stamp = bfd_get_32 (abfd, data + 4);
1445 edt.major_ver = bfd_get_16 (abfd, data + 8);
1446 edt.minor_ver = bfd_get_16 (abfd, data + 10);
1447 edt.name = bfd_get_32 (abfd, data + 12);
1448 edt.base = bfd_get_32 (abfd, data + 16);
1449 edt.num_functions = bfd_get_32 (abfd, data + 20);
1450 edt.num_names = bfd_get_32 (abfd, data + 24);
1451 edt.eat_addr = bfd_get_32 (abfd, data + 28);
1452 edt.npt_addr = bfd_get_32 (abfd, data + 32);
1453 edt.ot_addr = bfd_get_32 (abfd, data + 36);
1454
1455 adj = section->vma - extra->ImageBase + dataoff;
1456
1457 /* Dump the EDT first. */
1458 fprintf (file,
1459 _("\nThe Export Tables (interpreted %s section contents)\n\n"),
1460 section->name);
1461
1462 fprintf (file,
1463 _("Export Flags \t\t\t%lx\n"), (unsigned long) edt.export_flags);
1464
1465 fprintf (file,
1466 _("Time/Date stamp \t\t%lx\n"), (unsigned long) edt.time_stamp);
1467
1468 fprintf (file,
1469 _("Major/Minor \t\t\t%d/%d\n"), edt.major_ver, edt.minor_ver);
1470
1471 fprintf (file,
1472 _("Name \t\t\t\t"));
1473 fprintf_vma (file, edt.name);
1474 fprintf (file,
1475 " %s\n", data + edt.name - adj);
1476
1477 fprintf (file,
1478 _("Ordinal Base \t\t\t%ld\n"), edt.base);
1479
1480 fprintf (file,
1481 _("Number in:\n"));
1482
1483 fprintf (file,
1484 _("\tExport Address Table \t\t%08lx\n"),
1485 edt.num_functions);
1486
1487 fprintf (file,
1488 _("\t[Name Pointer/Ordinal] Table\t%08lx\n"), edt.num_names);
1489
1490 fprintf (file,
1491 _("Table Addresses\n"));
1492
1493 fprintf (file,
1494 _("\tExport Address Table \t\t"));
1495 fprintf_vma (file, edt.eat_addr);
1496 fprintf (file, "\n");
1497
1498 fprintf (file,
1499 _("\tName Pointer Table \t\t"));
1500 fprintf_vma (file, edt.npt_addr);
1501 fprintf (file, "\n");
1502
1503 fprintf (file,
1504 _("\tOrdinal Table \t\t\t"));
1505 fprintf_vma (file, edt.ot_addr);
1506 fprintf (file, "\n");
1507
1508 /* The next table to find is the Export Address Table. It's basically
1509 a list of pointers that either locate a function in this dll, or
1510 forward the call to another dll. Something like:
1511 typedef union
1512 {
1513 long export_rva;
1514 long forwarder_rva;
1515 } export_address_table_entry; */
1516
1517 fprintf (file,
1518 _("\nExport Address Table -- Ordinal Base %ld\n"),
1519 edt.base);
1520
1521 for (i = 0; i < edt.num_functions; ++i)
1522 {
1523 bfd_vma eat_member = bfd_get_32 (abfd,
1524 data + edt.eat_addr + (i * 4) - adj);
1525 if (eat_member == 0)
1526 continue;
1527
1528 if (eat_member - adj <= datasize)
1529 {
1530 /* This rva is to a name (forwarding function) in our section. */
1531 /* Should locate a function descriptor. */
1532 fprintf (file,
1533 "\t[%4ld] +base[%4ld] %04lx %s -- %s\n",
1534 (long) i,
1535 (long) (i + edt.base),
1536 (unsigned long) eat_member,
1537 _("Forwarder RVA"),
1538 data + eat_member - adj);
1539 }
1540 else
1541 {
1542 /* Should locate a function descriptor in the reldata section. */
1543 fprintf (file,
1544 "\t[%4ld] +base[%4ld] %04lx %s\n",
1545 (long) i,
1546 (long) (i + edt.base),
1547 (unsigned long) eat_member,
1548 _("Export RVA"));
1549 }
1550 }
1551
1552 /* The Export Name Pointer Table is paired with the Export Ordinal Table. */
1553 /* Dump them in parallel for clarity. */
1554 fprintf (file,
1555 _("\n[Ordinal/Name Pointer] Table\n"));
1556
1557 for (i = 0; i < edt.num_names; ++i)
1558 {
1559 bfd_vma name_ptr = bfd_get_32 (abfd,
1560 data +
1561 edt.npt_addr
1562 + (i*4) - adj);
1563
1564 char *name = (char *) data + name_ptr - adj;
1565
1566 bfd_vma ord = bfd_get_16 (abfd,
1567 data +
1568 edt.ot_addr
1569 + (i*2) - adj);
1570 fprintf (file,
1571 "\t[%4ld] %s\n", (long) ord, name);
1572 }
1573
1574 free (data);
1575
1576 return TRUE;
1577 }
1578
1579 /* This really is architecture dependent. On IA-64, a .pdata entry
1580 consists of three dwords containing relative virtual addresses that
1581 specify the start and end address of the code range the entry
1582 covers and the address of the corresponding unwind info data. */
1583
1584 static bfd_boolean
1585 pe_print_pdata (bfd * abfd, void * vfile)
1586 {
1587 #if defined(COFF_WITH_pep) && !defined(COFF_WITH_pex64)
1588 # define PDATA_ROW_SIZE (3 * 8)
1589 #else
1590 # define PDATA_ROW_SIZE (5 * 4)
1591 #endif
1592 FILE *file = (FILE *) vfile;
1593 bfd_byte *data = 0;
1594 asection *section = bfd_get_section_by_name (abfd, ".pdata");
1595 bfd_size_type datasize = 0;
1596 bfd_size_type i;
1597 bfd_size_type start, stop;
1598 int onaline = PDATA_ROW_SIZE;
1599
1600 if (section == NULL
1601 || coff_section_data (abfd, section) == NULL
1602 || pei_section_data (abfd, section) == NULL)
1603 return TRUE;
1604
1605 stop = pei_section_data (abfd, section)->virt_size;
1606 if ((stop % onaline) != 0)
1607 fprintf (file,
1608 _("Warning, .pdata section size (%ld) is not a multiple of %d\n"),
1609 (long) stop, onaline);
1610
1611 fprintf (file,
1612 _("\nThe Function Table (interpreted .pdata section contents)\n"));
1613 #if defined(COFF_WITH_pep) && !defined(COFF_WITH_pex64)
1614 fprintf (file,
1615 _(" vma:\t\t\tBegin Address End Address Unwind Info\n"));
1616 #else
1617 fprintf (file, _("\
1618 vma:\t\tBegin End EH EH PrologEnd Exception\n\
1619 \t\tAddress Address Handler Data Address Mask\n"));
1620 #endif
1621
1622 datasize = section->size;
1623 if (datasize == 0)
1624 return TRUE;
1625
1626 if (! bfd_malloc_and_get_section (abfd, section, &data))
1627 {
1628 if (data != NULL)
1629 free (data);
1630 return FALSE;
1631 }
1632
1633 start = 0;
1634
1635 for (i = start; i < stop; i += onaline)
1636 {
1637 bfd_vma begin_addr;
1638 bfd_vma end_addr;
1639 bfd_vma eh_handler;
1640 bfd_vma eh_data;
1641 bfd_vma prolog_end_addr;
1642 int em_data;
1643
1644 if (i + PDATA_ROW_SIZE > stop)
1645 break;
1646
1647 begin_addr = GET_PDATA_ENTRY (abfd, data + i );
1648 end_addr = GET_PDATA_ENTRY (abfd, data + i + 4);
1649 eh_handler = GET_PDATA_ENTRY (abfd, data + i + 8);
1650 eh_data = GET_PDATA_ENTRY (abfd, data + i + 12);
1651 prolog_end_addr = GET_PDATA_ENTRY (abfd, data + i + 16);
1652
1653 if (begin_addr == 0 && end_addr == 0 && eh_handler == 0
1654 && eh_data == 0 && prolog_end_addr == 0)
1655 /* We are probably into the padding of the section now. */
1656 break;
1657
1658 em_data = ((eh_handler & 0x1) << 2) | (prolog_end_addr & 0x3);
1659 eh_handler &= ~(bfd_vma) 0x3;
1660 prolog_end_addr &= ~(bfd_vma) 0x3;
1661
1662 fputc (' ', file);
1663 fprintf_vma (file, i + section->vma); fputc ('\t', file);
1664 fprintf_vma (file, begin_addr); fputc (' ', file);
1665 fprintf_vma (file, end_addr); fputc (' ', file);
1666 fprintf_vma (file, eh_handler);
1667 #if !defined(COFF_WITH_pep) || defined(COFF_WITH_pex64)
1668 fputc (' ', file);
1669 fprintf_vma (file, eh_data); fputc (' ', file);
1670 fprintf_vma (file, prolog_end_addr);
1671 fprintf (file, " %x", em_data);
1672 #endif
1673
1674 #ifdef POWERPC_LE_PE
1675 if (eh_handler == 0 && eh_data != 0)
1676 {
1677 /* Special bits here, although the meaning may be a little
1678 mysterious. The only one I know for sure is 0x03
1679 Code Significance
1680 0x00 None
1681 0x01 Register Save Millicode
1682 0x02 Register Restore Millicode
1683 0x03 Glue Code Sequence. */
1684 switch (eh_data)
1685 {
1686 case 0x01:
1687 fprintf (file, _(" Register save millicode"));
1688 break;
1689 case 0x02:
1690 fprintf (file, _(" Register restore millicode"));
1691 break;
1692 case 0x03:
1693 fprintf (file, _(" Glue code sequence"));
1694 break;
1695 default:
1696 break;
1697 }
1698 }
1699 #endif
1700 fprintf (file, "\n");
1701 }
1702
1703 free (data);
1704
1705 return TRUE;
1706 }
1707
1708 #define IMAGE_REL_BASED_HIGHADJ 4
1709 static const char * const tbl[] =
1710 {
1711 "ABSOLUTE",
1712 "HIGH",
1713 "LOW",
1714 "HIGHLOW",
1715 "HIGHADJ",
1716 "MIPS_JMPADDR",
1717 "SECTION",
1718 "REL32",
1719 "RESERVED1",
1720 "MIPS_JMPADDR16",
1721 "DIR64",
1722 "HIGH3ADJ",
1723 "UNKNOWN", /* MUST be last. */
1724 };
1725
1726 static bfd_boolean
1727 pe_print_reloc (bfd * abfd, void * vfile)
1728 {
1729 FILE *file = (FILE *) vfile;
1730 bfd_byte *data = 0;
1731 asection *section = bfd_get_section_by_name (abfd, ".reloc");
1732 bfd_size_type datasize;
1733 bfd_size_type i;
1734 bfd_size_type start, stop;
1735
1736 if (section == NULL)
1737 return TRUE;
1738
1739 if (section->size == 0)
1740 return TRUE;
1741
1742 fprintf (file,
1743 _("\n\nPE File Base Relocations (interpreted .reloc section contents)\n"));
1744
1745 datasize = section->size;
1746 if (! bfd_malloc_and_get_section (abfd, section, &data))
1747 {
1748 if (data != NULL)
1749 free (data);
1750 return FALSE;
1751 }
1752
1753 start = 0;
1754
1755 stop = section->size;
1756
1757 for (i = start; i < stop;)
1758 {
1759 int j;
1760 bfd_vma virtual_address;
1761 long number, size;
1762
1763 /* The .reloc section is a sequence of blocks, with a header consisting
1764 of two 32 bit quantities, followed by a number of 16 bit entries. */
1765 virtual_address = bfd_get_32 (abfd, data+i);
1766 size = bfd_get_32 (abfd, data+i+4);
1767 number = (size - 8) / 2;
1768
1769 if (size == 0)
1770 break;
1771
1772 fprintf (file,
1773 _("\nVirtual Address: %08lx Chunk size %ld (0x%lx) Number of fixups %ld\n"),
1774 (unsigned long) virtual_address, size, size, number);
1775
1776 for (j = 0; j < number; ++j)
1777 {
1778 unsigned short e = bfd_get_16 (abfd, data + i + 8 + j * 2);
1779 unsigned int t = (e & 0xF000) >> 12;
1780 int off = e & 0x0FFF;
1781
1782 if (t >= sizeof (tbl) / sizeof (tbl[0]))
1783 t = (sizeof (tbl) / sizeof (tbl[0])) - 1;
1784
1785 fprintf (file,
1786 _("\treloc %4d offset %4x [%4lx] %s"),
1787 j, off, (long) (off + virtual_address), tbl[t]);
1788
1789 /* HIGHADJ takes an argument, - the next record *is* the
1790 low 16 bits of addend. */
1791 if (t == IMAGE_REL_BASED_HIGHADJ)
1792 {
1793 fprintf (file, " (%4x)",
1794 ((unsigned int)
1795 bfd_get_16 (abfd, data + i + 8 + j * 2 + 2)));
1796 j++;
1797 }
1798
1799 fprintf (file, "\n");
1800 }
1801
1802 i += size;
1803 }
1804
1805 free (data);
1806
1807 return TRUE;
1808 }
1809
1810 /* Print out the program headers. */
1811
1812 bfd_boolean
1813 _bfd_XX_print_private_bfd_data_common (bfd * abfd, void * vfile)
1814 {
1815 FILE *file = (FILE *) vfile;
1816 int j;
1817 pe_data_type *pe = pe_data (abfd);
1818 struct internal_extra_pe_aouthdr *i = &pe->pe_opthdr;
1819 const char *subsystem_name = NULL;
1820 const char *name;
1821
1822 /* The MS dumpbin program reportedly ands with 0xff0f before
1823 printing the characteristics field. Not sure why. No reason to
1824 emulate it here. */
1825 fprintf (file, _("\nCharacteristics 0x%x\n"), pe->real_flags);
1826 #undef PF
1827 #define PF(x, y) if (pe->real_flags & x) { fprintf (file, "\t%s\n", y); }
1828 PF (IMAGE_FILE_RELOCS_STRIPPED, "relocations stripped");
1829 PF (IMAGE_FILE_EXECUTABLE_IMAGE, "executable");
1830 PF (IMAGE_FILE_LINE_NUMS_STRIPPED, "line numbers stripped");
1831 PF (IMAGE_FILE_LOCAL_SYMS_STRIPPED, "symbols stripped");
1832 PF (IMAGE_FILE_LARGE_ADDRESS_AWARE, "large address aware");
1833 PF (IMAGE_FILE_BYTES_REVERSED_LO, "little endian");
1834 PF (IMAGE_FILE_32BIT_MACHINE, "32 bit words");
1835 PF (IMAGE_FILE_DEBUG_STRIPPED, "debugging information removed");
1836 PF (IMAGE_FILE_SYSTEM, "system file");
1837 PF (IMAGE_FILE_DLL, "DLL");
1838 PF (IMAGE_FILE_BYTES_REVERSED_HI, "big endian");
1839 #undef PF
1840
1841 /* ctime implies '\n'. */
1842 {
1843 time_t t = pe->coff.timestamp;
1844 fprintf (file, "\nTime/Date\t\t%s", ctime (&t));
1845 }
1846
1847 #ifndef IMAGE_NT_OPTIONAL_HDR_MAGIC
1848 # define IMAGE_NT_OPTIONAL_HDR_MAGIC 0x10b
1849 #endif
1850 #ifndef IMAGE_NT_OPTIONAL_HDR64_MAGIC
1851 # define IMAGE_NT_OPTIONAL_HDR64_MAGIC 0x20b
1852 #endif
1853 #ifndef IMAGE_NT_OPTIONAL_HDRROM_MAGIC
1854 # define IMAGE_NT_OPTIONAL_HDRROM_MAGIC 0x107
1855 #endif
1856
1857 switch (i->Magic)
1858 {
1859 case IMAGE_NT_OPTIONAL_HDR_MAGIC:
1860 name = "PE32";
1861 break;
1862 case IMAGE_NT_OPTIONAL_HDR64_MAGIC:
1863 name = "PE32+";
1864 break;
1865 case IMAGE_NT_OPTIONAL_HDRROM_MAGIC:
1866 name = "ROM";
1867 break;
1868 default:
1869 name = NULL;
1870 break;
1871 }
1872 fprintf (file, "Magic\t\t\t%04x", i->Magic);
1873 if (name)
1874 fprintf (file, "\t(%s)",name);
1875 fprintf (file, "\nMajorLinkerVersion\t%d\n", i->MajorLinkerVersion);
1876 fprintf (file, "MinorLinkerVersion\t%d\n", i->MinorLinkerVersion);
1877 fprintf (file, "SizeOfCode\t\t%08lx\n", i->SizeOfCode);
1878 fprintf (file, "SizeOfInitializedData\t%08lx\n",
1879 i->SizeOfInitializedData);
1880 fprintf (file, "SizeOfUninitializedData\t%08lx\n",
1881 i->SizeOfUninitializedData);
1882 fprintf (file, "AddressOfEntryPoint\t");
1883 fprintf_vma (file, i->AddressOfEntryPoint);
1884 fprintf (file, "\nBaseOfCode\t\t");
1885 fprintf_vma (file, i->BaseOfCode);
1886 #if !defined(COFF_WITH_pep) && !defined(COFF_WITH_pex64)
1887 /* PE32+ does not have BaseOfData member! */
1888 fprintf (file, "\nBaseOfData\t\t");
1889 fprintf_vma (file, i->BaseOfData);
1890 #endif
1891
1892 fprintf (file, "\nImageBase\t\t");
1893 fprintf_vma (file, i->ImageBase);
1894 fprintf (file, "\nSectionAlignment\t");
1895 fprintf_vma (file, i->SectionAlignment);
1896 fprintf (file, "\nFileAlignment\t\t");
1897 fprintf_vma (file, i->FileAlignment);
1898 fprintf (file, "\nMajorOSystemVersion\t%d\n", i->MajorOperatingSystemVersion);
1899 fprintf (file, "MinorOSystemVersion\t%d\n", i->MinorOperatingSystemVersion);
1900 fprintf (file, "MajorImageVersion\t%d\n", i->MajorImageVersion);
1901 fprintf (file, "MinorImageVersion\t%d\n", i->MinorImageVersion);
1902 fprintf (file, "MajorSubsystemVersion\t%d\n", i->MajorSubsystemVersion);
1903 fprintf (file, "MinorSubsystemVersion\t%d\n", i->MinorSubsystemVersion);
1904 fprintf (file, "Win32Version\t\t%08lx\n", i->Reserved1);
1905 fprintf (file, "SizeOfImage\t\t%08lx\n", i->SizeOfImage);
1906 fprintf (file, "SizeOfHeaders\t\t%08lx\n", i->SizeOfHeaders);
1907 fprintf (file, "CheckSum\t\t%08lx\n", i->CheckSum);
1908
1909 switch (i->Subsystem)
1910 {
1911 case IMAGE_SUBSYSTEM_UNKNOWN:
1912 subsystem_name = "unspecified";
1913 break;
1914 case IMAGE_SUBSYSTEM_NATIVE:
1915 subsystem_name = "NT native";
1916 break;
1917 case IMAGE_SUBSYSTEM_WINDOWS_GUI:
1918 subsystem_name = "Windows GUI";
1919 break;
1920 case IMAGE_SUBSYSTEM_WINDOWS_CUI:
1921 subsystem_name = "Windows CUI";
1922 break;
1923 case IMAGE_SUBSYSTEM_POSIX_CUI:
1924 subsystem_name = "POSIX CUI";
1925 break;
1926 case IMAGE_SUBSYSTEM_WINDOWS_CE_GUI:
1927 subsystem_name = "Wince CUI";
1928 break;
1929 case IMAGE_SUBSYSTEM_EFI_APPLICATION:
1930 subsystem_name = "EFI application";
1931 break;
1932 case IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER:
1933 subsystem_name = "EFI boot service driver";
1934 break;
1935 case IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER:
1936 subsystem_name = "EFI runtime driver";
1937 break;
1938 // These are from revision 8.0 of the MS PE/COFF spec
1939 case IMAGE_SUBSYSTEM_EFI_ROM:
1940 subsystem_name = "EFI ROM";
1941 break;
1942 case IMAGE_SUBSYSTEM_XBOX:
1943 subsystem_name = "XBOX";
1944 break;
1945 // Added default case for clarity - subsystem_name is NULL anyway.
1946 default:
1947 subsystem_name = NULL;
1948 }
1949
1950 fprintf (file, "Subsystem\t\t%08x", i->Subsystem);
1951 if (subsystem_name)
1952 fprintf (file, "\t(%s)", subsystem_name);
1953 fprintf (file, "\nDllCharacteristics\t%08x\n", i->DllCharacteristics);
1954 fprintf (file, "SizeOfStackReserve\t");
1955 fprintf_vma (file, i->SizeOfStackReserve);
1956 fprintf (file, "\nSizeOfStackCommit\t");
1957 fprintf_vma (file, i->SizeOfStackCommit);
1958 fprintf (file, "\nSizeOfHeapReserve\t");
1959 fprintf_vma (file, i->SizeOfHeapReserve);
1960 fprintf (file, "\nSizeOfHeapCommit\t");
1961 fprintf_vma (file, i->SizeOfHeapCommit);
1962 fprintf (file, "\nLoaderFlags\t\t%08lx\n", i->LoaderFlags);
1963 fprintf (file, "NumberOfRvaAndSizes\t%08lx\n", i->NumberOfRvaAndSizes);
1964
1965 fprintf (file, "\nThe Data Directory\n");
1966 for (j = 0; j < IMAGE_NUMBEROF_DIRECTORY_ENTRIES; j++)
1967 {
1968 fprintf (file, "Entry %1x ", j);
1969 fprintf_vma (file, i->DataDirectory[j].VirtualAddress);
1970 fprintf (file, " %08lx ", i->DataDirectory[j].Size);
1971 fprintf (file, "%s\n", dir_names[j]);
1972 }
1973
1974 pe_print_idata (abfd, vfile);
1975 pe_print_edata (abfd, vfile);
1976 pe_print_pdata (abfd, vfile);
1977 pe_print_reloc (abfd, vfile);
1978
1979 return TRUE;
1980 }
1981
1982 /* Copy any private info we understand from the input bfd
1983 to the output bfd. */
1984
1985 bfd_boolean
1986 _bfd_XX_bfd_copy_private_bfd_data_common (bfd * ibfd, bfd * obfd)
1987 {
1988 /* One day we may try to grok other private data. */
1989 if (ibfd->xvec->flavour != bfd_target_coff_flavour
1990 || obfd->xvec->flavour != bfd_target_coff_flavour)
1991 return TRUE;
1992
1993 pe_data (obfd)->pe_opthdr = pe_data (ibfd)->pe_opthdr;
1994 pe_data (obfd)->dll = pe_data (ibfd)->dll;
1995
1996 /* For strip: if we removed .reloc, we'll make a real mess of things
1997 if we don't remove this entry as well. */
1998 if (! pe_data (obfd)->has_reloc_section)
1999 {
2000 pe_data (obfd)->pe_opthdr.DataDirectory[PE_BASE_RELOCATION_TABLE].VirtualAddress = 0;
2001 pe_data (obfd)->pe_opthdr.DataDirectory[PE_BASE_RELOCATION_TABLE].Size = 0;
2002 }
2003 return TRUE;
2004 }
2005
2006 /* Copy private section data. */
2007
2008 bfd_boolean
2009 _bfd_XX_bfd_copy_private_section_data (bfd *ibfd,
2010 asection *isec,
2011 bfd *obfd,
2012 asection *osec)
2013 {
2014 if (bfd_get_flavour (ibfd) != bfd_target_coff_flavour
2015 || bfd_get_flavour (obfd) != bfd_target_coff_flavour)
2016 return TRUE;
2017
2018 if (coff_section_data (ibfd, isec) != NULL
2019 && pei_section_data (ibfd, isec) != NULL)
2020 {
2021 if (coff_section_data (obfd, osec) == NULL)
2022 {
2023 bfd_size_type amt = sizeof (struct coff_section_tdata);
2024 osec->used_by_bfd = bfd_zalloc (obfd, amt);
2025 if (osec->used_by_bfd == NULL)
2026 return FALSE;
2027 }
2028
2029 if (pei_section_data (obfd, osec) == NULL)
2030 {
2031 bfd_size_type amt = sizeof (struct pei_section_tdata);
2032 coff_section_data (obfd, osec)->tdata = bfd_zalloc (obfd, amt);
2033 if (coff_section_data (obfd, osec)->tdata == NULL)
2034 return FALSE;
2035 }
2036
2037 pei_section_data (obfd, osec)->virt_size =
2038 pei_section_data (ibfd, isec)->virt_size;
2039 pei_section_data (obfd, osec)->pe_flags =
2040 pei_section_data (ibfd, isec)->pe_flags;
2041 }
2042
2043 return TRUE;
2044 }
2045
2046 void
2047 _bfd_XX_get_symbol_info (bfd * abfd, asymbol *symbol, symbol_info *ret)
2048 {
2049 coff_get_symbol_info (abfd, symbol, ret);
2050 }
2051
2052 /* Handle the .idata section and other things that need symbol table
2053 access. */
2054
2055 bfd_boolean
2056 _bfd_XXi_final_link_postscript (bfd * abfd, struct coff_final_link_info *pfinfo)
2057 {
2058 struct coff_link_hash_entry *h1;
2059 struct bfd_link_info *info = pfinfo->info;
2060 bfd_boolean result = TRUE;
2061
2062 /* There are a few fields that need to be filled in now while we
2063 have symbol table access.
2064
2065 The .idata subsections aren't directly available as sections, but
2066 they are in the symbol table, so get them from there. */
2067
2068 /* The import directory. This is the address of .idata$2, with size
2069 of .idata$2 + .idata$3. */
2070 h1 = coff_link_hash_lookup (coff_hash_table (info),
2071 ".idata$2", FALSE, FALSE, TRUE);
2072 if (h1 != NULL)
2073 {
2074 /* PR ld/2729: We cannot rely upon all the output sections having been
2075 created properly, so check before referencing them. Issue a warning
2076 message for any sections tht could not be found. */
2077 if (h1->root.u.def.section != NULL
2078 && h1->root.u.def.section->output_section != NULL)
2079 pe_data (abfd)->pe_opthdr.DataDirectory[PE_IMPORT_TABLE].VirtualAddress =
2080 (h1->root.u.def.value
2081 + h1->root.u.def.section->output_section->vma
2082 + h1->root.u.def.section->output_offset);
2083 else
2084 {
2085 _bfd_error_handler
2086 (_("%B: unable to fill in DataDictionary[1] because .idata$2 is missing"),
2087 abfd);
2088 result = FALSE;
2089 }
2090
2091 h1 = coff_link_hash_lookup (coff_hash_table (info),
2092 ".idata$4", FALSE, FALSE, TRUE);
2093 if (h1 != NULL
2094 && h1->root.u.def.section != NULL
2095 && h1->root.u.def.section->output_section != NULL)
2096 pe_data (abfd)->pe_opthdr.DataDirectory[PE_IMPORT_TABLE].Size =
2097 ((h1->root.u.def.value
2098 + h1->root.u.def.section->output_section->vma
2099 + h1->root.u.def.section->output_offset)
2100 - pe_data (abfd)->pe_opthdr.DataDirectory[PE_IMPORT_TABLE].VirtualAddress);
2101 else
2102 {
2103 _bfd_error_handler
2104 (_("%B: unable to fill in DataDictionary[1] because .idata$4 is missing"),
2105 abfd);
2106 result = FALSE;
2107 }
2108
2109 /* The import address table. This is the size/address of
2110 .idata$5. */
2111 h1 = coff_link_hash_lookup (coff_hash_table (info),
2112 ".idata$5", FALSE, FALSE, TRUE);
2113 if (h1 != NULL
2114 && h1->root.u.def.section != NULL
2115 && h1->root.u.def.section->output_section != NULL)
2116 pe_data (abfd)->pe_opthdr.DataDirectory[PE_IMPORT_ADDRESS_TABLE].VirtualAddress =
2117 (h1->root.u.def.value
2118 + h1->root.u.def.section->output_section->vma
2119 + h1->root.u.def.section->output_offset);
2120 else
2121 {
2122 _bfd_error_handler
2123 (_("%B: unable to fill in DataDictionary[12] because .idata$5 is missing"),
2124 abfd);
2125 result = FALSE;
2126 }
2127
2128 h1 = coff_link_hash_lookup (coff_hash_table (info),
2129 ".idata$6", FALSE, FALSE, TRUE);
2130 if (h1 != NULL
2131 && h1->root.u.def.section != NULL
2132 && h1->root.u.def.section->output_section != NULL)
2133 pe_data (abfd)->pe_opthdr.DataDirectory[PE_IMPORT_ADDRESS_TABLE].Size =
2134 ((h1->root.u.def.value
2135 + h1->root.u.def.section->output_section->vma
2136 + h1->root.u.def.section->output_offset)
2137 - pe_data (abfd)->pe_opthdr.DataDirectory[PE_IMPORT_ADDRESS_TABLE].VirtualAddress);
2138 else
2139 {
2140 _bfd_error_handler
2141 (_("%B: unable to fill in DataDictionary[PE_IMPORT_ADDRESS_TABLE (12)] because .idata$6 is missing"),
2142 abfd);
2143 result = FALSE;
2144 }
2145 }
2146
2147 h1 = coff_link_hash_lookup (coff_hash_table (info),
2148 "__tls_used", FALSE, FALSE, TRUE);
2149 if (h1 != NULL)
2150 {
2151 if (h1->root.u.def.section != NULL
2152 && h1->root.u.def.section->output_section != NULL)
2153 pe_data (abfd)->pe_opthdr.DataDirectory[PE_TLS_TABLE].VirtualAddress =
2154 (h1->root.u.def.value
2155 + h1->root.u.def.section->output_section->vma
2156 + h1->root.u.def.section->output_offset
2157 - pe_data (abfd)->pe_opthdr.ImageBase);
2158 else
2159 {
2160 _bfd_error_handler
2161 (_("%B: unable to fill in DataDictionary[9] because __tls_used is missing"),
2162 abfd);
2163 result = FALSE;
2164 }
2165
2166 pe_data (abfd)->pe_opthdr.DataDirectory[PE_TLS_TABLE].Size = 0x18;
2167 }
2168
2169 /* If we couldn't find idata$2, we either have an excessively
2170 trivial program or are in DEEP trouble; we have to assume trivial
2171 program.... */
2172 return result;
2173 }
This page took 0.076304 seconds and 4 git commands to generate.