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