cddb38f1b60844047605e39b3e9f6cc0398fefed
[deliverable/binutils-gdb.git] / bfd / peXXigen.c
1 /* Support for the generic parts of PE/PEI; the common executable parts.
2 Copyright 1995-2013 Free Software Foundation, Inc.
3 Written by Cygnus Solutions.
4
5 This file is part of BFD, the Binary File Descriptor library.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
20 MA 02110-1301, USA. */
21
22
23 /* Most of this hacked by Steve Chamberlain <sac@cygnus.com>.
24
25 PE/PEI rearrangement (and code added): Donn Terry
26 Softway Systems, Inc. */
27
28 /* Hey look, some documentation [and in a place you expect to find it]!
29
30 The main reference for the pei format is "Microsoft Portable Executable
31 and Common Object File Format Specification 4.1". Get it if you need to
32 do some serious hacking on this code.
33
34 Another reference:
35 "Peering Inside the PE: A Tour of the Win32 Portable Executable
36 File Format", MSJ 1994, Volume 9.
37
38 The *sole* difference between the pe format and the pei format is that the
39 latter has an MSDOS 2.0 .exe header on the front that prints the message
40 "This app must be run under Windows." (or some such).
41 (FIXME: Whether that statement is *really* true or not is unknown.
42 Are there more subtle differences between pe and pei formats?
43 For now assume there aren't. If you find one, then for God sakes
44 document it here!)
45
46 The Microsoft docs use the word "image" instead of "executable" because
47 the former can also refer to a DLL (shared library). Confusion can arise
48 because the `i' in `pei' also refers to "image". The `pe' format can
49 also create images (i.e. executables), it's just that to run on a win32
50 system you need to use the pei format.
51
52 FIXME: Please add more docs here so the next poor fool that has to hack
53 on this code has a chance of getting something accomplished without
54 wasting too much time. */
55
56 /* This expands into COFF_WITH_pe, COFF_WITH_pep, or COFF_WITH_pex64
57 depending on whether we're compiling for straight PE or PE+. */
58 #define COFF_WITH_XX
59
60 #include "sysdep.h"
61 #include "bfd.h"
62 #include "libbfd.h"
63 #include "coff/internal.h"
64 #include "bfdver.h"
65 #ifdef HAVE_WCHAR_H
66 #include <wchar.h>
67 #endif
68
69 /* NOTE: it's strange to be including an architecture specific header
70 in what's supposed to be general (to PE/PEI) code. However, that's
71 where the definitions are, and they don't vary per architecture
72 within PE/PEI, so we get them from there. FIXME: The lack of
73 variance is an assumption which may prove to be incorrect if new
74 PE/PEI targets are created. */
75 #if defined COFF_WITH_pex64
76 # include "coff/x86_64.h"
77 #elif defined COFF_WITH_pep
78 # include "coff/ia64.h"
79 #else
80 # include "coff/i386.h"
81 #endif
82
83 #include "coff/pe.h"
84 #include "libcoff.h"
85 #include "libpei.h"
86 #include "safe-ctype.h"
87
88 #if defined COFF_WITH_pep || defined COFF_WITH_pex64
89 # undef AOUTSZ
90 # define AOUTSZ PEPAOUTSZ
91 # define PEAOUTHDR PEPAOUTHDR
92 #endif
93
94 #define HighBitSet(val) ((val) & 0x80000000)
95 #define SetHighBit(val) ((val) | 0x80000000)
96 #define WithoutHighBit(val) ((val) & 0x7fffffff)
97
98 /* FIXME: This file has various tests of POWERPC_LE_PE. Those tests
99 worked when the code was in peicode.h, but no longer work now that
100 the code is in peigen.c. PowerPC NT is said to be dead. If
101 anybody wants to revive the code, you will have to figure out how
102 to handle those issues. */
103 \f
104 void
105 _bfd_XXi_swap_sym_in (bfd * abfd, void * ext1, void * in1)
106 {
107 SYMENT *ext = (SYMENT *) ext1;
108 struct internal_syment *in = (struct internal_syment *) in1;
109
110 if (ext->e.e_name[0] == 0)
111 {
112 in->_n._n_n._n_zeroes = 0;
113 in->_n._n_n._n_offset = H_GET_32 (abfd, ext->e.e.e_offset);
114 }
115 else
116 memcpy (in->_n._n_name, ext->e.e_name, SYMNMLEN);
117
118 in->n_value = H_GET_32 (abfd, ext->e_value);
119 in->n_scnum = H_GET_16 (abfd, ext->e_scnum);
120
121 if (sizeof (ext->e_type) == 2)
122 in->n_type = H_GET_16 (abfd, ext->e_type);
123 else
124 in->n_type = H_GET_32 (abfd, ext->e_type);
125
126 in->n_sclass = H_GET_8 (abfd, ext->e_sclass);
127 in->n_numaux = H_GET_8 (abfd, ext->e_numaux);
128
129 #ifndef STRICT_PE_FORMAT
130 /* This is for Gnu-created DLLs. */
131
132 /* The section symbols for the .idata$ sections have class 0x68
133 (C_SECTION), which MS documentation indicates is a section
134 symbol. Unfortunately, the value field in the symbol is simply a
135 copy of the .idata section's flags rather than something useful.
136 When these symbols are encountered, change the value to 0 so that
137 they will be handled somewhat correctly in the bfd code. */
138 if (in->n_sclass == C_SECTION)
139 {
140 char namebuf[SYMNMLEN + 1];
141 const char *name = NULL;
142
143 in->n_value = 0x0;
144
145 /* Create synthetic empty sections as needed. DJ */
146 if (in->n_scnum == 0)
147 {
148 asection *sec;
149
150 name = _bfd_coff_internal_syment_name (abfd, in, namebuf);
151 if (name == NULL)
152 /* FIXME: Return error. */
153 abort ();
154 sec = bfd_get_section_by_name (abfd, name);
155 if (sec != NULL)
156 in->n_scnum = sec->target_index;
157 }
158
159 if (in->n_scnum == 0)
160 {
161 int unused_section_number = 0;
162 asection *sec;
163 flagword flags;
164
165 for (sec = abfd->sections; sec; sec = sec->next)
166 if (unused_section_number <= sec->target_index)
167 unused_section_number = sec->target_index + 1;
168
169 if (name == namebuf)
170 {
171 name = (const char *) bfd_alloc (abfd, strlen (namebuf) + 1);
172 if (name == NULL)
173 /* FIXME: Return error. */
174 abort ();
175 strcpy ((char *) name, namebuf);
176 }
177 flags = SEC_HAS_CONTENTS | SEC_ALLOC | SEC_DATA | SEC_LOAD;
178 sec = bfd_make_section_anyway_with_flags (abfd, name, flags);
179 if (sec == NULL)
180 /* FIXME: Return error. */
181 abort ();
182
183 sec->vma = 0;
184 sec->lma = 0;
185 sec->size = 0;
186 sec->filepos = 0;
187 sec->rel_filepos = 0;
188 sec->reloc_count = 0;
189 sec->line_filepos = 0;
190 sec->lineno_count = 0;
191 sec->userdata = NULL;
192 sec->next = NULL;
193 sec->alignment_power = 2;
194
195 sec->target_index = unused_section_number;
196
197 in->n_scnum = unused_section_number;
198 }
199 in->n_sclass = C_STAT;
200 }
201 #endif
202
203 #ifdef coff_swap_sym_in_hook
204 /* This won't work in peigen.c, but since it's for PPC PE, it's not
205 worth fixing. */
206 coff_swap_sym_in_hook (abfd, ext1, in1);
207 #endif
208 }
209
210 unsigned int
211 _bfd_XXi_swap_sym_out (bfd * abfd, void * inp, void * extp)
212 {
213 struct internal_syment *in = (struct internal_syment *) inp;
214 SYMENT *ext = (SYMENT *) extp;
215
216 if (in->_n._n_name[0] == 0)
217 {
218 H_PUT_32 (abfd, 0, ext->e.e.e_zeroes);
219 H_PUT_32 (abfd, in->_n._n_n._n_offset, ext->e.e.e_offset);
220 }
221 else
222 memcpy (ext->e.e_name, in->_n._n_name, SYMNMLEN);
223
224 H_PUT_32 (abfd, in->n_value, ext->e_value);
225 H_PUT_16 (abfd, in->n_scnum, ext->e_scnum);
226
227 if (sizeof (ext->e_type) == 2)
228 H_PUT_16 (abfd, in->n_type, ext->e_type);
229 else
230 H_PUT_32 (abfd, in->n_type, ext->e_type);
231
232 H_PUT_8 (abfd, in->n_sclass, ext->e_sclass);
233 H_PUT_8 (abfd, in->n_numaux, ext->e_numaux);
234
235 return SYMESZ;
236 }
237
238 void
239 _bfd_XXi_swap_aux_in (bfd * abfd,
240 void * ext1,
241 int type,
242 int in_class,
243 int indx ATTRIBUTE_UNUSED,
244 int numaux ATTRIBUTE_UNUSED,
245 void * in1)
246 {
247 AUXENT *ext = (AUXENT *) ext1;
248 union internal_auxent *in = (union internal_auxent *) in1;
249
250 switch (in_class)
251 {
252 case C_FILE:
253 if (ext->x_file.x_fname[0] == 0)
254 {
255 in->x_file.x_n.x_zeroes = 0;
256 in->x_file.x_n.x_offset = H_GET_32 (abfd, ext->x_file.x_n.x_offset);
257 }
258 else
259 memcpy (in->x_file.x_fname, ext->x_file.x_fname, FILNMLEN);
260 return;
261
262 case C_STAT:
263 case C_LEAFSTAT:
264 case C_HIDDEN:
265 if (type == T_NULL)
266 {
267 in->x_scn.x_scnlen = GET_SCN_SCNLEN (abfd, ext);
268 in->x_scn.x_nreloc = GET_SCN_NRELOC (abfd, ext);
269 in->x_scn.x_nlinno = GET_SCN_NLINNO (abfd, ext);
270 in->x_scn.x_checksum = H_GET_32 (abfd, ext->x_scn.x_checksum);
271 in->x_scn.x_associated = H_GET_16 (abfd, ext->x_scn.x_associated);
272 in->x_scn.x_comdat = H_GET_8 (abfd, ext->x_scn.x_comdat);
273 return;
274 }
275 break;
276 }
277
278 in->x_sym.x_tagndx.l = H_GET_32 (abfd, ext->x_sym.x_tagndx);
279 in->x_sym.x_tvndx = H_GET_16 (abfd, ext->x_sym.x_tvndx);
280
281 if (in_class == C_BLOCK || in_class == C_FCN || ISFCN (type)
282 || ISTAG (in_class))
283 {
284 in->x_sym.x_fcnary.x_fcn.x_lnnoptr = GET_FCN_LNNOPTR (abfd, ext);
285 in->x_sym.x_fcnary.x_fcn.x_endndx.l = GET_FCN_ENDNDX (abfd, ext);
286 }
287 else
288 {
289 in->x_sym.x_fcnary.x_ary.x_dimen[0] =
290 H_GET_16 (abfd, ext->x_sym.x_fcnary.x_ary.x_dimen[0]);
291 in->x_sym.x_fcnary.x_ary.x_dimen[1] =
292 H_GET_16 (abfd, ext->x_sym.x_fcnary.x_ary.x_dimen[1]);
293 in->x_sym.x_fcnary.x_ary.x_dimen[2] =
294 H_GET_16 (abfd, ext->x_sym.x_fcnary.x_ary.x_dimen[2]);
295 in->x_sym.x_fcnary.x_ary.x_dimen[3] =
296 H_GET_16 (abfd, ext->x_sym.x_fcnary.x_ary.x_dimen[3]);
297 }
298
299 if (ISFCN (type))
300 {
301 in->x_sym.x_misc.x_fsize = H_GET_32 (abfd, ext->x_sym.x_misc.x_fsize);
302 }
303 else
304 {
305 in->x_sym.x_misc.x_lnsz.x_lnno = GET_LNSZ_LNNO (abfd, ext);
306 in->x_sym.x_misc.x_lnsz.x_size = GET_LNSZ_SIZE (abfd, ext);
307 }
308 }
309
310 unsigned int
311 _bfd_XXi_swap_aux_out (bfd * abfd,
312 void * inp,
313 int type,
314 int in_class,
315 int indx ATTRIBUTE_UNUSED,
316 int numaux ATTRIBUTE_UNUSED,
317 void * extp)
318 {
319 union internal_auxent *in = (union internal_auxent *) inp;
320 AUXENT *ext = (AUXENT *) extp;
321
322 memset (ext, 0, AUXESZ);
323
324 switch (in_class)
325 {
326 case C_FILE:
327 if (in->x_file.x_fname[0] == 0)
328 {
329 H_PUT_32 (abfd, 0, ext->x_file.x_n.x_zeroes);
330 H_PUT_32 (abfd, in->x_file.x_n.x_offset, ext->x_file.x_n.x_offset);
331 }
332 else
333 memcpy (ext->x_file.x_fname, in->x_file.x_fname, FILNMLEN);
334
335 return AUXESZ;
336
337 case C_STAT:
338 case C_LEAFSTAT:
339 case C_HIDDEN:
340 if (type == T_NULL)
341 {
342 PUT_SCN_SCNLEN (abfd, in->x_scn.x_scnlen, ext);
343 PUT_SCN_NRELOC (abfd, in->x_scn.x_nreloc, ext);
344 PUT_SCN_NLINNO (abfd, in->x_scn.x_nlinno, ext);
345 H_PUT_32 (abfd, in->x_scn.x_checksum, ext->x_scn.x_checksum);
346 H_PUT_16 (abfd, in->x_scn.x_associated, ext->x_scn.x_associated);
347 H_PUT_8 (abfd, in->x_scn.x_comdat, ext->x_scn.x_comdat);
348 return AUXESZ;
349 }
350 break;
351 }
352
353 H_PUT_32 (abfd, in->x_sym.x_tagndx.l, ext->x_sym.x_tagndx);
354 H_PUT_16 (abfd, in->x_sym.x_tvndx, ext->x_sym.x_tvndx);
355
356 if (in_class == C_BLOCK || in_class == C_FCN || ISFCN (type)
357 || ISTAG (in_class))
358 {
359 PUT_FCN_LNNOPTR (abfd, in->x_sym.x_fcnary.x_fcn.x_lnnoptr, ext);
360 PUT_FCN_ENDNDX (abfd, in->x_sym.x_fcnary.x_fcn.x_endndx.l, ext);
361 }
362 else
363 {
364 H_PUT_16 (abfd, in->x_sym.x_fcnary.x_ary.x_dimen[0],
365 ext->x_sym.x_fcnary.x_ary.x_dimen[0]);
366 H_PUT_16 (abfd, in->x_sym.x_fcnary.x_ary.x_dimen[1],
367 ext->x_sym.x_fcnary.x_ary.x_dimen[1]);
368 H_PUT_16 (abfd, in->x_sym.x_fcnary.x_ary.x_dimen[2],
369 ext->x_sym.x_fcnary.x_ary.x_dimen[2]);
370 H_PUT_16 (abfd, in->x_sym.x_fcnary.x_ary.x_dimen[3],
371 ext->x_sym.x_fcnary.x_ary.x_dimen[3]);
372 }
373
374 if (ISFCN (type))
375 H_PUT_32 (abfd, in->x_sym.x_misc.x_fsize, ext->x_sym.x_misc.x_fsize);
376 else
377 {
378 PUT_LNSZ_LNNO (abfd, in->x_sym.x_misc.x_lnsz.x_lnno, ext);
379 PUT_LNSZ_SIZE (abfd, in->x_sym.x_misc.x_lnsz.x_size, ext);
380 }
381
382 return AUXESZ;
383 }
384
385 void
386 _bfd_XXi_swap_lineno_in (bfd * abfd, void * ext1, void * in1)
387 {
388 LINENO *ext = (LINENO *) ext1;
389 struct internal_lineno *in = (struct internal_lineno *) in1;
390
391 in->l_addr.l_symndx = H_GET_32 (abfd, ext->l_addr.l_symndx);
392 in->l_lnno = GET_LINENO_LNNO (abfd, ext);
393 }
394
395 unsigned int
396 _bfd_XXi_swap_lineno_out (bfd * abfd, void * inp, void * outp)
397 {
398 struct internal_lineno *in = (struct internal_lineno *) inp;
399 struct external_lineno *ext = (struct external_lineno *) outp;
400 H_PUT_32 (abfd, in->l_addr.l_symndx, ext->l_addr.l_symndx);
401
402 PUT_LINENO_LNNO (abfd, in->l_lnno, ext);
403 return LINESZ;
404 }
405
406 void
407 _bfd_XXi_swap_aouthdr_in (bfd * abfd,
408 void * aouthdr_ext1,
409 void * aouthdr_int1)
410 {
411 PEAOUTHDR * src = (PEAOUTHDR *) aouthdr_ext1;
412 AOUTHDR * aouthdr_ext = (AOUTHDR *) aouthdr_ext1;
413 struct internal_aouthdr *aouthdr_int
414 = (struct internal_aouthdr *) aouthdr_int1;
415 struct internal_extra_pe_aouthdr *a = &aouthdr_int->pe;
416
417 aouthdr_int->magic = H_GET_16 (abfd, aouthdr_ext->magic);
418 aouthdr_int->vstamp = H_GET_16 (abfd, aouthdr_ext->vstamp);
419 aouthdr_int->tsize = GET_AOUTHDR_TSIZE (abfd, aouthdr_ext->tsize);
420 aouthdr_int->dsize = GET_AOUTHDR_DSIZE (abfd, aouthdr_ext->dsize);
421 aouthdr_int->bsize = GET_AOUTHDR_BSIZE (abfd, aouthdr_ext->bsize);
422 aouthdr_int->entry = GET_AOUTHDR_ENTRY (abfd, aouthdr_ext->entry);
423 aouthdr_int->text_start =
424 GET_AOUTHDR_TEXT_START (abfd, aouthdr_ext->text_start);
425 #if !defined(COFF_WITH_pep) && !defined(COFF_WITH_pex64)
426 /* PE32+ does not have data_start member! */
427 aouthdr_int->data_start =
428 GET_AOUTHDR_DATA_START (abfd, aouthdr_ext->data_start);
429 a->BaseOfData = aouthdr_int->data_start;
430 #endif
431
432 a->Magic = aouthdr_int->magic;
433 a->MajorLinkerVersion = H_GET_8 (abfd, aouthdr_ext->vstamp);
434 a->MinorLinkerVersion = H_GET_8 (abfd, aouthdr_ext->vstamp + 1);
435 a->SizeOfCode = aouthdr_int->tsize ;
436 a->SizeOfInitializedData = aouthdr_int->dsize ;
437 a->SizeOfUninitializedData = aouthdr_int->bsize ;
438 a->AddressOfEntryPoint = aouthdr_int->entry;
439 a->BaseOfCode = aouthdr_int->text_start;
440 a->ImageBase = GET_OPTHDR_IMAGE_BASE (abfd, src->ImageBase);
441 a->SectionAlignment = H_GET_32 (abfd, src->SectionAlignment);
442 a->FileAlignment = H_GET_32 (abfd, src->FileAlignment);
443 a->MajorOperatingSystemVersion =
444 H_GET_16 (abfd, src->MajorOperatingSystemVersion);
445 a->MinorOperatingSystemVersion =
446 H_GET_16 (abfd, src->MinorOperatingSystemVersion);
447 a->MajorImageVersion = H_GET_16 (abfd, src->MajorImageVersion);
448 a->MinorImageVersion = H_GET_16 (abfd, src->MinorImageVersion);
449 a->MajorSubsystemVersion = H_GET_16 (abfd, src->MajorSubsystemVersion);
450 a->MinorSubsystemVersion = H_GET_16 (abfd, src->MinorSubsystemVersion);
451 a->Reserved1 = H_GET_32 (abfd, src->Reserved1);
452 a->SizeOfImage = H_GET_32 (abfd, src->SizeOfImage);
453 a->SizeOfHeaders = H_GET_32 (abfd, src->SizeOfHeaders);
454 a->CheckSum = H_GET_32 (abfd, src->CheckSum);
455 a->Subsystem = H_GET_16 (abfd, src->Subsystem);
456 a->DllCharacteristics = H_GET_16 (abfd, src->DllCharacteristics);
457 a->SizeOfStackReserve =
458 GET_OPTHDR_SIZE_OF_STACK_RESERVE (abfd, src->SizeOfStackReserve);
459 a->SizeOfStackCommit =
460 GET_OPTHDR_SIZE_OF_STACK_COMMIT (abfd, src->SizeOfStackCommit);
461 a->SizeOfHeapReserve =
462 GET_OPTHDR_SIZE_OF_HEAP_RESERVE (abfd, src->SizeOfHeapReserve);
463 a->SizeOfHeapCommit =
464 GET_OPTHDR_SIZE_OF_HEAP_COMMIT (abfd, src->SizeOfHeapCommit);
465 a->LoaderFlags = H_GET_32 (abfd, src->LoaderFlags);
466 a->NumberOfRvaAndSizes = H_GET_32 (abfd, src->NumberOfRvaAndSizes);
467
468 {
469 int idx;
470
471 for (idx = 0; idx < a->NumberOfRvaAndSizes; idx++)
472 {
473 /* If data directory is empty, rva also should be 0. */
474 int size =
475 H_GET_32 (abfd, src->DataDirectory[idx][1]);
476
477 a->DataDirectory[idx].Size = size;
478
479 if (size)
480 a->DataDirectory[idx].VirtualAddress =
481 H_GET_32 (abfd, src->DataDirectory[idx][0]);
482 else
483 a->DataDirectory[idx].VirtualAddress = 0;
484 }
485 }
486
487 if (aouthdr_int->entry)
488 {
489 aouthdr_int->entry += a->ImageBase;
490 #if !defined(COFF_WITH_pep) && !defined(COFF_WITH_pex64)
491 aouthdr_int->entry &= 0xffffffff;
492 #endif
493 }
494
495 if (aouthdr_int->tsize)
496 {
497 aouthdr_int->text_start += a->ImageBase;
498 #if !defined(COFF_WITH_pep) && !defined(COFF_WITH_pex64)
499 aouthdr_int->text_start &= 0xffffffff;
500 #endif
501 }
502
503 #if !defined(COFF_WITH_pep) && !defined(COFF_WITH_pex64)
504 /* PE32+ does not have data_start member! */
505 if (aouthdr_int->dsize)
506 {
507 aouthdr_int->data_start += a->ImageBase;
508 aouthdr_int->data_start &= 0xffffffff;
509 }
510 #endif
511
512 #ifdef POWERPC_LE_PE
513 /* These three fields are normally set up by ppc_relocate_section.
514 In the case of reading a file in, we can pick them up from the
515 DataDirectory. */
516 first_thunk_address = a->DataDirectory[PE_IMPORT_ADDRESS_TABLE].VirtualAddress;
517 thunk_size = a->DataDirectory[PE_IMPORT_ADDRESS_TABLE].Size;
518 import_table_size = a->DataDirectory[PE_IMPORT_TABLE].Size;
519 #endif
520 }
521
522 /* A support function for below. */
523
524 static void
525 add_data_entry (bfd * abfd,
526 struct internal_extra_pe_aouthdr *aout,
527 int idx,
528 char *name,
529 bfd_vma base)
530 {
531 asection *sec = bfd_get_section_by_name (abfd, name);
532
533 /* Add import directory information if it exists. */
534 if ((sec != NULL)
535 && (coff_section_data (abfd, sec) != NULL)
536 && (pei_section_data (abfd, sec) != NULL))
537 {
538 /* If data directory is empty, rva also should be 0. */
539 int size = pei_section_data (abfd, sec)->virt_size;
540 aout->DataDirectory[idx].Size = size;
541
542 if (size)
543 {
544 aout->DataDirectory[idx].VirtualAddress =
545 (sec->vma - base) & 0xffffffff;
546 sec->flags |= SEC_DATA;
547 }
548 }
549 }
550
551 unsigned int
552 _bfd_XXi_swap_aouthdr_out (bfd * abfd, void * in, void * out)
553 {
554 struct internal_aouthdr *aouthdr_in = (struct internal_aouthdr *) in;
555 pe_data_type *pe = pe_data (abfd);
556 struct internal_extra_pe_aouthdr *extra = &pe->pe_opthdr;
557 PEAOUTHDR *aouthdr_out = (PEAOUTHDR *) out;
558 bfd_vma sa, fa, ib;
559 IMAGE_DATA_DIRECTORY idata2, idata5, tls;
560
561 sa = extra->SectionAlignment;
562 fa = extra->FileAlignment;
563 ib = extra->ImageBase;
564
565 idata2 = pe->pe_opthdr.DataDirectory[PE_IMPORT_TABLE];
566 idata5 = pe->pe_opthdr.DataDirectory[PE_IMPORT_ADDRESS_TABLE];
567 tls = pe->pe_opthdr.DataDirectory[PE_TLS_TABLE];
568
569 if (aouthdr_in->tsize)
570 {
571 aouthdr_in->text_start -= ib;
572 #if !defined(COFF_WITH_pep) && !defined(COFF_WITH_pex64)
573 aouthdr_in->text_start &= 0xffffffff;
574 #endif
575 }
576
577 if (aouthdr_in->dsize)
578 {
579 aouthdr_in->data_start -= ib;
580 #if !defined(COFF_WITH_pep) && !defined(COFF_WITH_pex64)
581 aouthdr_in->data_start &= 0xffffffff;
582 #endif
583 }
584
585 if (aouthdr_in->entry)
586 {
587 aouthdr_in->entry -= ib;
588 #if !defined(COFF_WITH_pep) && !defined(COFF_WITH_pex64)
589 aouthdr_in->entry &= 0xffffffff;
590 #endif
591 }
592
593 #define FA(x) (((x) + fa -1 ) & (- fa))
594 #define SA(x) (((x) + sa -1 ) & (- sa))
595
596 /* We like to have the sizes aligned. */
597 aouthdr_in->bsize = FA (aouthdr_in->bsize);
598
599 extra->NumberOfRvaAndSizes = IMAGE_NUMBEROF_DIRECTORY_ENTRIES;
600
601 add_data_entry (abfd, extra, 0, ".edata", ib);
602 add_data_entry (abfd, extra, 2, ".rsrc", ib);
603 add_data_entry (abfd, extra, 3, ".pdata", ib);
604
605 /* In theory we do not need to call add_data_entry for .idata$2 or
606 .idata$5. It will be done in bfd_coff_final_link where all the
607 required information is available. If however, we are not going
608 to perform a final link, eg because we have been invoked by objcopy
609 or strip, then we need to make sure that these Data Directory
610 entries are initialised properly.
611
612 So - we copy the input values into the output values, and then, if
613 a final link is going to be performed, it can overwrite them. */
614 extra->DataDirectory[PE_IMPORT_TABLE] = idata2;
615 extra->DataDirectory[PE_IMPORT_ADDRESS_TABLE] = idata5;
616 extra->DataDirectory[PE_TLS_TABLE] = tls;
617
618 if (extra->DataDirectory[PE_IMPORT_TABLE].VirtualAddress == 0)
619 /* Until other .idata fixes are made (pending patch), the entry for
620 .idata is needed for backwards compatibility. FIXME. */
621 add_data_entry (abfd, extra, 1, ".idata", ib);
622
623 /* For some reason, the virtual size (which is what's set by
624 add_data_entry) for .reloc is not the same as the size recorded
625 in this slot by MSVC; it doesn't seem to cause problems (so far),
626 but since it's the best we've got, use it. It does do the right
627 thing for .pdata. */
628 if (pe->has_reloc_section)
629 add_data_entry (abfd, extra, 5, ".reloc", ib);
630
631 {
632 asection *sec;
633 bfd_vma hsize = 0;
634 bfd_vma dsize = 0;
635 bfd_vma isize = 0;
636 bfd_vma tsize = 0;
637
638 for (sec = abfd->sections; sec; sec = sec->next)
639 {
640 int rounded = FA (sec->size);
641
642 /* The first non-zero section filepos is the header size.
643 Sections without contents will have a filepos of 0. */
644 if (hsize == 0)
645 hsize = sec->filepos;
646 if (sec->flags & SEC_DATA)
647 dsize += rounded;
648 if (sec->flags & SEC_CODE)
649 tsize += rounded;
650 /* The image size is the total VIRTUAL size (which is what is
651 in the virt_size field). Files have been seen (from MSVC
652 5.0 link.exe) where the file size of the .data segment is
653 quite small compared to the virtual size. Without this
654 fix, strip munges the file.
655
656 FIXME: We need to handle holes between sections, which may
657 happpen when we covert from another format. We just use
658 the virtual address and virtual size of the last section
659 for the image size. */
660 if (coff_section_data (abfd, sec) != NULL
661 && pei_section_data (abfd, sec) != NULL)
662 isize = (sec->vma - extra->ImageBase
663 + SA (FA (pei_section_data (abfd, sec)->virt_size)));
664 }
665
666 aouthdr_in->dsize = dsize;
667 aouthdr_in->tsize = tsize;
668 extra->SizeOfHeaders = hsize;
669 extra->SizeOfImage = isize;
670 }
671
672 H_PUT_16 (abfd, aouthdr_in->magic, aouthdr_out->standard.magic);
673
674 /* e.g. 219510000 is linker version 2.19 */
675 #define LINKER_VERSION ((short) (BFD_VERSION / 1000000))
676
677 /* This piece of magic sets the "linker version" field to
678 LINKER_VERSION. */
679 H_PUT_16 (abfd, (LINKER_VERSION / 100 + (LINKER_VERSION % 100) * 256),
680 aouthdr_out->standard.vstamp);
681
682 PUT_AOUTHDR_TSIZE (abfd, aouthdr_in->tsize, aouthdr_out->standard.tsize);
683 PUT_AOUTHDR_DSIZE (abfd, aouthdr_in->dsize, aouthdr_out->standard.dsize);
684 PUT_AOUTHDR_BSIZE (abfd, aouthdr_in->bsize, aouthdr_out->standard.bsize);
685 PUT_AOUTHDR_ENTRY (abfd, aouthdr_in->entry, aouthdr_out->standard.entry);
686 PUT_AOUTHDR_TEXT_START (abfd, aouthdr_in->text_start,
687 aouthdr_out->standard.text_start);
688
689 #if !defined(COFF_WITH_pep) && !defined(COFF_WITH_pex64)
690 /* PE32+ does not have data_start member! */
691 PUT_AOUTHDR_DATA_START (abfd, aouthdr_in->data_start,
692 aouthdr_out->standard.data_start);
693 #endif
694
695 PUT_OPTHDR_IMAGE_BASE (abfd, extra->ImageBase, aouthdr_out->ImageBase);
696 H_PUT_32 (abfd, extra->SectionAlignment, aouthdr_out->SectionAlignment);
697 H_PUT_32 (abfd, extra->FileAlignment, aouthdr_out->FileAlignment);
698 H_PUT_16 (abfd, extra->MajorOperatingSystemVersion,
699 aouthdr_out->MajorOperatingSystemVersion);
700 H_PUT_16 (abfd, extra->MinorOperatingSystemVersion,
701 aouthdr_out->MinorOperatingSystemVersion);
702 H_PUT_16 (abfd, extra->MajorImageVersion, aouthdr_out->MajorImageVersion);
703 H_PUT_16 (abfd, extra->MinorImageVersion, aouthdr_out->MinorImageVersion);
704 H_PUT_16 (abfd, extra->MajorSubsystemVersion,
705 aouthdr_out->MajorSubsystemVersion);
706 H_PUT_16 (abfd, extra->MinorSubsystemVersion,
707 aouthdr_out->MinorSubsystemVersion);
708 H_PUT_32 (abfd, extra->Reserved1, aouthdr_out->Reserved1);
709 H_PUT_32 (abfd, extra->SizeOfImage, aouthdr_out->SizeOfImage);
710 H_PUT_32 (abfd, extra->SizeOfHeaders, aouthdr_out->SizeOfHeaders);
711 H_PUT_32 (abfd, extra->CheckSum, aouthdr_out->CheckSum);
712 H_PUT_16 (abfd, extra->Subsystem, aouthdr_out->Subsystem);
713 H_PUT_16 (abfd, extra->DllCharacteristics, aouthdr_out->DllCharacteristics);
714 PUT_OPTHDR_SIZE_OF_STACK_RESERVE (abfd, extra->SizeOfStackReserve,
715 aouthdr_out->SizeOfStackReserve);
716 PUT_OPTHDR_SIZE_OF_STACK_COMMIT (abfd, extra->SizeOfStackCommit,
717 aouthdr_out->SizeOfStackCommit);
718 PUT_OPTHDR_SIZE_OF_HEAP_RESERVE (abfd, extra->SizeOfHeapReserve,
719 aouthdr_out->SizeOfHeapReserve);
720 PUT_OPTHDR_SIZE_OF_HEAP_COMMIT (abfd, extra->SizeOfHeapCommit,
721 aouthdr_out->SizeOfHeapCommit);
722 H_PUT_32 (abfd, extra->LoaderFlags, aouthdr_out->LoaderFlags);
723 H_PUT_32 (abfd, extra->NumberOfRvaAndSizes,
724 aouthdr_out->NumberOfRvaAndSizes);
725 {
726 int idx;
727
728 for (idx = 0; idx < 16; idx++)
729 {
730 H_PUT_32 (abfd, extra->DataDirectory[idx].VirtualAddress,
731 aouthdr_out->DataDirectory[idx][0]);
732 H_PUT_32 (abfd, extra->DataDirectory[idx].Size,
733 aouthdr_out->DataDirectory[idx][1]);
734 }
735 }
736
737 return AOUTSZ;
738 }
739
740 unsigned int
741 _bfd_XXi_only_swap_filehdr_out (bfd * abfd, void * in, void * out)
742 {
743 int idx;
744 struct internal_filehdr *filehdr_in = (struct internal_filehdr *) in;
745 struct external_PEI_filehdr *filehdr_out = (struct external_PEI_filehdr *) out;
746
747 if (pe_data (abfd)->has_reloc_section
748 || pe_data (abfd)->dont_strip_reloc)
749 filehdr_in->f_flags &= ~F_RELFLG;
750
751 if (pe_data (abfd)->dll)
752 filehdr_in->f_flags |= F_DLL;
753
754 filehdr_in->pe.e_magic = DOSMAGIC;
755 filehdr_in->pe.e_cblp = 0x90;
756 filehdr_in->pe.e_cp = 0x3;
757 filehdr_in->pe.e_crlc = 0x0;
758 filehdr_in->pe.e_cparhdr = 0x4;
759 filehdr_in->pe.e_minalloc = 0x0;
760 filehdr_in->pe.e_maxalloc = 0xffff;
761 filehdr_in->pe.e_ss = 0x0;
762 filehdr_in->pe.e_sp = 0xb8;
763 filehdr_in->pe.e_csum = 0x0;
764 filehdr_in->pe.e_ip = 0x0;
765 filehdr_in->pe.e_cs = 0x0;
766 filehdr_in->pe.e_lfarlc = 0x40;
767 filehdr_in->pe.e_ovno = 0x0;
768
769 for (idx = 0; idx < 4; idx++)
770 filehdr_in->pe.e_res[idx] = 0x0;
771
772 filehdr_in->pe.e_oemid = 0x0;
773 filehdr_in->pe.e_oeminfo = 0x0;
774
775 for (idx = 0; idx < 10; idx++)
776 filehdr_in->pe.e_res2[idx] = 0x0;
777
778 filehdr_in->pe.e_lfanew = 0x80;
779
780 /* This next collection of data are mostly just characters. It
781 appears to be constant within the headers put on NT exes. */
782 filehdr_in->pe.dos_message[0] = 0x0eba1f0e;
783 filehdr_in->pe.dos_message[1] = 0xcd09b400;
784 filehdr_in->pe.dos_message[2] = 0x4c01b821;
785 filehdr_in->pe.dos_message[3] = 0x685421cd;
786 filehdr_in->pe.dos_message[4] = 0x70207369;
787 filehdr_in->pe.dos_message[5] = 0x72676f72;
788 filehdr_in->pe.dos_message[6] = 0x63206d61;
789 filehdr_in->pe.dos_message[7] = 0x6f6e6e61;
790 filehdr_in->pe.dos_message[8] = 0x65622074;
791 filehdr_in->pe.dos_message[9] = 0x6e757220;
792 filehdr_in->pe.dos_message[10] = 0x206e6920;
793 filehdr_in->pe.dos_message[11] = 0x20534f44;
794 filehdr_in->pe.dos_message[12] = 0x65646f6d;
795 filehdr_in->pe.dos_message[13] = 0x0a0d0d2e;
796 filehdr_in->pe.dos_message[14] = 0x24;
797 filehdr_in->pe.dos_message[15] = 0x0;
798 filehdr_in->pe.nt_signature = NT_SIGNATURE;
799
800 H_PUT_16 (abfd, filehdr_in->f_magic, filehdr_out->f_magic);
801 H_PUT_16 (abfd, filehdr_in->f_nscns, filehdr_out->f_nscns);
802
803 /* Only use a real timestamp if the option was chosen. */
804 if ((pe_data (abfd)->insert_timestamp))
805 H_PUT_32 (abfd, time(0), filehdr_out->f_timdat);
806
807 PUT_FILEHDR_SYMPTR (abfd, filehdr_in->f_symptr,
808 filehdr_out->f_symptr);
809 H_PUT_32 (abfd, filehdr_in->f_nsyms, filehdr_out->f_nsyms);
810 H_PUT_16 (abfd, filehdr_in->f_opthdr, filehdr_out->f_opthdr);
811 H_PUT_16 (abfd, filehdr_in->f_flags, filehdr_out->f_flags);
812
813 /* Put in extra dos header stuff. This data remains essentially
814 constant, it just has to be tacked on to the beginning of all exes
815 for NT. */
816 H_PUT_16 (abfd, filehdr_in->pe.e_magic, filehdr_out->e_magic);
817 H_PUT_16 (abfd, filehdr_in->pe.e_cblp, filehdr_out->e_cblp);
818 H_PUT_16 (abfd, filehdr_in->pe.e_cp, filehdr_out->e_cp);
819 H_PUT_16 (abfd, filehdr_in->pe.e_crlc, filehdr_out->e_crlc);
820 H_PUT_16 (abfd, filehdr_in->pe.e_cparhdr, filehdr_out->e_cparhdr);
821 H_PUT_16 (abfd, filehdr_in->pe.e_minalloc, filehdr_out->e_minalloc);
822 H_PUT_16 (abfd, filehdr_in->pe.e_maxalloc, filehdr_out->e_maxalloc);
823 H_PUT_16 (abfd, filehdr_in->pe.e_ss, filehdr_out->e_ss);
824 H_PUT_16 (abfd, filehdr_in->pe.e_sp, filehdr_out->e_sp);
825 H_PUT_16 (abfd, filehdr_in->pe.e_csum, filehdr_out->e_csum);
826 H_PUT_16 (abfd, filehdr_in->pe.e_ip, filehdr_out->e_ip);
827 H_PUT_16 (abfd, filehdr_in->pe.e_cs, filehdr_out->e_cs);
828 H_PUT_16 (abfd, filehdr_in->pe.e_lfarlc, filehdr_out->e_lfarlc);
829 H_PUT_16 (abfd, filehdr_in->pe.e_ovno, filehdr_out->e_ovno);
830
831 for (idx = 0; idx < 4; idx++)
832 H_PUT_16 (abfd, filehdr_in->pe.e_res[idx], filehdr_out->e_res[idx]);
833
834 H_PUT_16 (abfd, filehdr_in->pe.e_oemid, filehdr_out->e_oemid);
835 H_PUT_16 (abfd, filehdr_in->pe.e_oeminfo, filehdr_out->e_oeminfo);
836
837 for (idx = 0; idx < 10; idx++)
838 H_PUT_16 (abfd, filehdr_in->pe.e_res2[idx], filehdr_out->e_res2[idx]);
839
840 H_PUT_32 (abfd, filehdr_in->pe.e_lfanew, filehdr_out->e_lfanew);
841
842 for (idx = 0; idx < 16; idx++)
843 H_PUT_32 (abfd, filehdr_in->pe.dos_message[idx],
844 filehdr_out->dos_message[idx]);
845
846 /* Also put in the NT signature. */
847 H_PUT_32 (abfd, filehdr_in->pe.nt_signature, filehdr_out->nt_signature);
848
849 return FILHSZ;
850 }
851
852 unsigned int
853 _bfd_XX_only_swap_filehdr_out (bfd * abfd, void * in, void * out)
854 {
855 struct internal_filehdr *filehdr_in = (struct internal_filehdr *) in;
856 FILHDR *filehdr_out = (FILHDR *) out;
857
858 H_PUT_16 (abfd, filehdr_in->f_magic, filehdr_out->f_magic);
859 H_PUT_16 (abfd, filehdr_in->f_nscns, filehdr_out->f_nscns);
860 H_PUT_32 (abfd, filehdr_in->f_timdat, filehdr_out->f_timdat);
861 PUT_FILEHDR_SYMPTR (abfd, filehdr_in->f_symptr, filehdr_out->f_symptr);
862 H_PUT_32 (abfd, filehdr_in->f_nsyms, filehdr_out->f_nsyms);
863 H_PUT_16 (abfd, filehdr_in->f_opthdr, filehdr_out->f_opthdr);
864 H_PUT_16 (abfd, filehdr_in->f_flags, filehdr_out->f_flags);
865
866 return FILHSZ;
867 }
868
869 unsigned int
870 _bfd_XXi_swap_scnhdr_out (bfd * abfd, void * in, void * out)
871 {
872 struct internal_scnhdr *scnhdr_int = (struct internal_scnhdr *) in;
873 SCNHDR *scnhdr_ext = (SCNHDR *) out;
874 unsigned int ret = SCNHSZ;
875 bfd_vma ps;
876 bfd_vma ss;
877
878 memcpy (scnhdr_ext->s_name, scnhdr_int->s_name, sizeof (scnhdr_int->s_name));
879
880 PUT_SCNHDR_VADDR (abfd,
881 ((scnhdr_int->s_vaddr
882 - pe_data (abfd)->pe_opthdr.ImageBase)
883 & 0xffffffff),
884 scnhdr_ext->s_vaddr);
885
886 /* NT wants the size data to be rounded up to the next
887 NT_FILE_ALIGNMENT, but zero if it has no content (as in .bss,
888 sometimes). */
889 if ((scnhdr_int->s_flags & IMAGE_SCN_CNT_UNINITIALIZED_DATA) != 0)
890 {
891 if (bfd_pei_p (abfd))
892 {
893 ps = scnhdr_int->s_size;
894 ss = 0;
895 }
896 else
897 {
898 ps = 0;
899 ss = scnhdr_int->s_size;
900 }
901 }
902 else
903 {
904 if (bfd_pei_p (abfd))
905 ps = scnhdr_int->s_paddr;
906 else
907 ps = 0;
908
909 ss = scnhdr_int->s_size;
910 }
911
912 PUT_SCNHDR_SIZE (abfd, ss,
913 scnhdr_ext->s_size);
914
915 /* s_paddr in PE is really the virtual size. */
916 PUT_SCNHDR_PADDR (abfd, ps, scnhdr_ext->s_paddr);
917
918 PUT_SCNHDR_SCNPTR (abfd, scnhdr_int->s_scnptr,
919 scnhdr_ext->s_scnptr);
920 PUT_SCNHDR_RELPTR (abfd, scnhdr_int->s_relptr,
921 scnhdr_ext->s_relptr);
922 PUT_SCNHDR_LNNOPTR (abfd, scnhdr_int->s_lnnoptr,
923 scnhdr_ext->s_lnnoptr);
924
925 {
926 /* Extra flags must be set when dealing with PE. All sections should also
927 have the IMAGE_SCN_MEM_READ (0x40000000) flag set. In addition, the
928 .text section must have IMAGE_SCN_MEM_EXECUTE (0x20000000) and the data
929 sections (.idata, .data, .bss, .CRT) must have IMAGE_SCN_MEM_WRITE set
930 (this is especially important when dealing with the .idata section since
931 the addresses for routines from .dlls must be overwritten). If .reloc
932 section data is ever generated, we must add IMAGE_SCN_MEM_DISCARDABLE
933 (0x02000000). Also, the resource data should also be read and
934 writable. */
935
936 /* FIXME: Alignment is also encoded in this field, at least on PPC and
937 ARM-WINCE. Although - how do we get the original alignment field
938 back ? */
939
940 typedef struct
941 {
942 const char * section_name;
943 unsigned long must_have;
944 }
945 pe_required_section_flags;
946
947 pe_required_section_flags known_sections [] =
948 {
949 { ".arch", IMAGE_SCN_MEM_READ | IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_DISCARDABLE | IMAGE_SCN_ALIGN_8BYTES },
950 { ".bss", IMAGE_SCN_MEM_READ | IMAGE_SCN_CNT_UNINITIALIZED_DATA | IMAGE_SCN_MEM_WRITE },
951 { ".data", IMAGE_SCN_MEM_READ | IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_WRITE },
952 { ".edata", IMAGE_SCN_MEM_READ | IMAGE_SCN_CNT_INITIALIZED_DATA },
953 { ".idata", IMAGE_SCN_MEM_READ | IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_WRITE },
954 { ".pdata", IMAGE_SCN_MEM_READ | IMAGE_SCN_CNT_INITIALIZED_DATA },
955 { ".rdata", IMAGE_SCN_MEM_READ | IMAGE_SCN_CNT_INITIALIZED_DATA },
956 { ".reloc", IMAGE_SCN_MEM_READ | IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_DISCARDABLE },
957 { ".rsrc", IMAGE_SCN_MEM_READ | IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_WRITE },
958 { ".text" , IMAGE_SCN_MEM_READ | IMAGE_SCN_CNT_CODE | IMAGE_SCN_MEM_EXECUTE },
959 { ".tls", IMAGE_SCN_MEM_READ | IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_WRITE },
960 { ".xdata", IMAGE_SCN_MEM_READ | IMAGE_SCN_CNT_INITIALIZED_DATA },
961 { NULL, 0}
962 };
963
964 pe_required_section_flags * p;
965
966 /* We have defaulted to adding the IMAGE_SCN_MEM_WRITE flag, but now
967 we know exactly what this specific section wants so we remove it
968 and then allow the must_have field to add it back in if necessary.
969 However, we don't remove IMAGE_SCN_MEM_WRITE flag from .text if the
970 default WP_TEXT file flag has been cleared. WP_TEXT may be cleared
971 by ld --enable-auto-import (if auto-import is actually needed),
972 by ld --omagic, or by obcopy --writable-text. */
973
974 for (p = known_sections; p->section_name; p++)
975 if (strcmp (scnhdr_int->s_name, p->section_name) == 0)
976 {
977 if (strcmp (scnhdr_int->s_name, ".text")
978 || (bfd_get_file_flags (abfd) & WP_TEXT))
979 scnhdr_int->s_flags &= ~IMAGE_SCN_MEM_WRITE;
980 scnhdr_int->s_flags |= p->must_have;
981 break;
982 }
983
984 H_PUT_32 (abfd, scnhdr_int->s_flags, scnhdr_ext->s_flags);
985 }
986
987 if (coff_data (abfd)->link_info
988 && ! coff_data (abfd)->link_info->relocatable
989 && ! coff_data (abfd)->link_info->shared
990 && strcmp (scnhdr_int->s_name, ".text") == 0)
991 {
992 /* By inference from looking at MS output, the 32 bit field
993 which is the combination of the number_of_relocs and
994 number_of_linenos is used for the line number count in
995 executables. A 16-bit field won't do for cc1. The MS
996 document says that the number of relocs is zero for
997 executables, but the 17-th bit has been observed to be there.
998 Overflow is not an issue: a 4G-line program will overflow a
999 bunch of other fields long before this! */
1000 H_PUT_16 (abfd, (scnhdr_int->s_nlnno & 0xffff), scnhdr_ext->s_nlnno);
1001 H_PUT_16 (abfd, (scnhdr_int->s_nlnno >> 16), scnhdr_ext->s_nreloc);
1002 }
1003 else
1004 {
1005 if (scnhdr_int->s_nlnno <= 0xffff)
1006 H_PUT_16 (abfd, scnhdr_int->s_nlnno, scnhdr_ext->s_nlnno);
1007 else
1008 {
1009 (*_bfd_error_handler) (_("%s: line number overflow: 0x%lx > 0xffff"),
1010 bfd_get_filename (abfd),
1011 scnhdr_int->s_nlnno);
1012 bfd_set_error (bfd_error_file_truncated);
1013 H_PUT_16 (abfd, 0xffff, scnhdr_ext->s_nlnno);
1014 ret = 0;
1015 }
1016
1017 /* Although we could encode 0xffff relocs here, we do not, to be
1018 consistent with other parts of bfd. Also it lets us warn, as
1019 we should never see 0xffff here w/o having the overflow flag
1020 set. */
1021 if (scnhdr_int->s_nreloc < 0xffff)
1022 H_PUT_16 (abfd, scnhdr_int->s_nreloc, scnhdr_ext->s_nreloc);
1023 else
1024 {
1025 /* PE can deal with large #s of relocs, but not here. */
1026 H_PUT_16 (abfd, 0xffff, scnhdr_ext->s_nreloc);
1027 scnhdr_int->s_flags |= IMAGE_SCN_LNK_NRELOC_OVFL;
1028 H_PUT_32 (abfd, scnhdr_int->s_flags, scnhdr_ext->s_flags);
1029 }
1030 }
1031 return ret;
1032 }
1033
1034 static char * dir_names[IMAGE_NUMBEROF_DIRECTORY_ENTRIES] =
1035 {
1036 N_("Export Directory [.edata (or where ever we found it)]"),
1037 N_("Import Directory [parts of .idata]"),
1038 N_("Resource Directory [.rsrc]"),
1039 N_("Exception Directory [.pdata]"),
1040 N_("Security Directory"),
1041 N_("Base Relocation Directory [.reloc]"),
1042 N_("Debug Directory"),
1043 N_("Description Directory"),
1044 N_("Special Directory"),
1045 N_("Thread Storage Directory [.tls]"),
1046 N_("Load Configuration Directory"),
1047 N_("Bound Import Directory"),
1048 N_("Import Address Table Directory"),
1049 N_("Delay Import Directory"),
1050 N_("CLR Runtime Header"),
1051 N_("Reserved")
1052 };
1053
1054 #ifdef POWERPC_LE_PE
1055 /* The code for the PPC really falls in the "architecture dependent"
1056 category. However, it's not clear that anyone will ever care, so
1057 we're ignoring the issue for now; if/when PPC matters, some of this
1058 may need to go into peicode.h, or arguments passed to enable the
1059 PPC- specific code. */
1060 #endif
1061
1062 static bfd_boolean
1063 pe_print_idata (bfd * abfd, void * vfile)
1064 {
1065 FILE *file = (FILE *) vfile;
1066 bfd_byte *data;
1067 asection *section;
1068 bfd_signed_vma adj;
1069
1070 #ifdef POWERPC_LE_PE
1071 asection *rel_section = bfd_get_section_by_name (abfd, ".reldata");
1072 #endif
1073
1074 bfd_size_type datasize = 0;
1075 bfd_size_type dataoff;
1076 bfd_size_type i;
1077 int onaline = 20;
1078
1079 pe_data_type *pe = pe_data (abfd);
1080 struct internal_extra_pe_aouthdr *extra = &pe->pe_opthdr;
1081
1082 bfd_vma addr;
1083
1084 addr = extra->DataDirectory[PE_IMPORT_TABLE].VirtualAddress;
1085
1086 if (addr == 0 && extra->DataDirectory[PE_IMPORT_TABLE].Size == 0)
1087 {
1088 /* Maybe the extra header isn't there. Look for the section. */
1089 section = bfd_get_section_by_name (abfd, ".idata");
1090 if (section == NULL)
1091 return TRUE;
1092
1093 addr = section->vma;
1094 datasize = section->size;
1095 if (datasize == 0)
1096 return TRUE;
1097 }
1098 else
1099 {
1100 addr += extra->ImageBase;
1101 for (section = abfd->sections; section != NULL; section = section->next)
1102 {
1103 datasize = section->size;
1104 if (addr >= section->vma && addr < section->vma + datasize)
1105 break;
1106 }
1107
1108 if (section == NULL)
1109 {
1110 fprintf (file,
1111 _("\nThere is an import table, but the section containing it could not be found\n"));
1112 return TRUE;
1113 }
1114 }
1115
1116 fprintf (file, _("\nThere is an import table in %s at 0x%lx\n"),
1117 section->name, (unsigned long) addr);
1118
1119 dataoff = addr - section->vma;
1120
1121 #ifdef POWERPC_LE_PE
1122 if (rel_section != 0 && rel_section->size != 0)
1123 {
1124 /* The toc address can be found by taking the starting address,
1125 which on the PPC locates a function descriptor. The
1126 descriptor consists of the function code starting address
1127 followed by the address of the toc. The starting address we
1128 get from the bfd, and the descriptor is supposed to be in the
1129 .reldata section. */
1130
1131 bfd_vma loadable_toc_address;
1132 bfd_vma toc_address;
1133 bfd_vma start_address;
1134 bfd_byte *data;
1135 bfd_vma offset;
1136
1137 if (!bfd_malloc_and_get_section (abfd, rel_section, &data))
1138 {
1139 if (data != NULL)
1140 free (data);
1141 return FALSE;
1142 }
1143
1144 offset = abfd->start_address - rel_section->vma;
1145
1146 if (offset >= rel_section->size || offset + 8 > rel_section->size)
1147 {
1148 if (data != NULL)
1149 free (data);
1150 return FALSE;
1151 }
1152
1153 start_address = bfd_get_32 (abfd, data + offset);
1154 loadable_toc_address = bfd_get_32 (abfd, data + offset + 4);
1155 toc_address = loadable_toc_address - 32768;
1156
1157 fprintf (file,
1158 _("\nFunction descriptor located at the start address: %04lx\n"),
1159 (unsigned long int) (abfd->start_address));
1160 fprintf (file,
1161 _("\tcode-base %08lx toc (loadable/actual) %08lx/%08lx\n"),
1162 start_address, loadable_toc_address, toc_address);
1163 if (data != NULL)
1164 free (data);
1165 }
1166 else
1167 {
1168 fprintf (file,
1169 _("\nNo reldata section! Function descriptor not decoded.\n"));
1170 }
1171 #endif
1172
1173 fprintf (file,
1174 _("\nThe Import Tables (interpreted %s section contents)\n"),
1175 section->name);
1176 fprintf (file,
1177 _("\
1178 vma: Hint Time Forward DLL First\n\
1179 Table Stamp Chain Name Thunk\n"));
1180
1181 /* Read the whole section. Some of the fields might be before dataoff. */
1182 if (!bfd_malloc_and_get_section (abfd, section, &data))
1183 {
1184 if (data != NULL)
1185 free (data);
1186 return FALSE;
1187 }
1188
1189 adj = section->vma - extra->ImageBase;
1190
1191 /* Print all image import descriptors. */
1192 for (i = dataoff; i + onaline <= datasize; i += onaline)
1193 {
1194 bfd_vma hint_addr;
1195 bfd_vma time_stamp;
1196 bfd_vma forward_chain;
1197 bfd_vma dll_name;
1198 bfd_vma first_thunk;
1199 int idx = 0;
1200 bfd_size_type j;
1201 char *dll;
1202
1203 /* Print (i + extra->DataDirectory[PE_IMPORT_TABLE].VirtualAddress). */
1204 fprintf (file, " %08lx\t", (unsigned long) (i + adj));
1205 hint_addr = bfd_get_32 (abfd, data + i);
1206 time_stamp = bfd_get_32 (abfd, data + i + 4);
1207 forward_chain = bfd_get_32 (abfd, data + i + 8);
1208 dll_name = bfd_get_32 (abfd, data + i + 12);
1209 first_thunk = bfd_get_32 (abfd, data + i + 16);
1210
1211 fprintf (file, "%08lx %08lx %08lx %08lx %08lx\n",
1212 (unsigned long) hint_addr,
1213 (unsigned long) time_stamp,
1214 (unsigned long) forward_chain,
1215 (unsigned long) dll_name,
1216 (unsigned long) first_thunk);
1217
1218 if (hint_addr == 0 && first_thunk == 0)
1219 break;
1220
1221 if (dll_name - adj >= section->size)
1222 break;
1223
1224 dll = (char *) data + dll_name - adj;
1225 fprintf (file, _("\n\tDLL Name: %s\n"), dll);
1226
1227 if (hint_addr != 0)
1228 {
1229 bfd_byte *ft_data;
1230 asection *ft_section;
1231 bfd_vma ft_addr;
1232 bfd_size_type ft_datasize;
1233 int ft_idx;
1234 int ft_allocated;
1235
1236 fprintf (file, _("\tvma: Hint/Ord Member-Name Bound-To\n"));
1237
1238 idx = hint_addr - adj;
1239
1240 ft_addr = first_thunk + extra->ImageBase;
1241 ft_idx = first_thunk - adj;
1242 ft_data = data + ft_idx;
1243 ft_datasize = datasize - ft_idx;
1244 ft_allocated = 0;
1245
1246 if (first_thunk != hint_addr)
1247 {
1248 /* Find the section which contains the first thunk. */
1249 for (ft_section = abfd->sections;
1250 ft_section != NULL;
1251 ft_section = ft_section->next)
1252 {
1253 if (ft_addr >= ft_section->vma
1254 && ft_addr < ft_section->vma + ft_section->size)
1255 break;
1256 }
1257
1258 if (ft_section == NULL)
1259 {
1260 fprintf (file,
1261 _("\nThere is a first thunk, but the section containing it could not be found\n"));
1262 continue;
1263 }
1264
1265 /* Now check to see if this section is the same as our current
1266 section. If it is not then we will have to load its data in. */
1267 if (ft_section != section)
1268 {
1269 ft_idx = first_thunk - (ft_section->vma - extra->ImageBase);
1270 ft_datasize = ft_section->size - ft_idx;
1271 ft_data = (bfd_byte *) bfd_malloc (ft_datasize);
1272 if (ft_data == NULL)
1273 continue;
1274
1275 /* Read ft_datasize bytes starting at offset ft_idx. */
1276 if (!bfd_get_section_contents (abfd, ft_section, ft_data,
1277 (bfd_vma) ft_idx, ft_datasize))
1278 {
1279 free (ft_data);
1280 continue;
1281 }
1282 ft_allocated = 1;
1283 }
1284 }
1285
1286 /* Print HintName vector entries. */
1287 #ifdef COFF_WITH_pex64
1288 for (j = 0; idx + j + 8 <= datasize; j += 8)
1289 {
1290 unsigned long member = bfd_get_32 (abfd, data + idx + j);
1291 unsigned long member_high = bfd_get_32 (abfd, data + idx + j + 4);
1292
1293 if (!member && !member_high)
1294 break;
1295
1296 if (HighBitSet (member_high))
1297 fprintf (file, "\t%lx%08lx\t %4lx%08lx <none>",
1298 member_high, member,
1299 WithoutHighBit (member_high), member);
1300 else
1301 {
1302 int ordinal;
1303 char *member_name;
1304
1305 ordinal = bfd_get_16 (abfd, data + member - adj);
1306 member_name = (char *) data + member - adj + 2;
1307 fprintf (file, "\t%04lx\t %4d %s",member, ordinal, member_name);
1308 }
1309
1310 /* If the time stamp is not zero, the import address
1311 table holds actual addresses. */
1312 if (time_stamp != 0
1313 && first_thunk != 0
1314 && first_thunk != hint_addr
1315 && j + 4 <= ft_datasize)
1316 fprintf (file, "\t%04lx",
1317 (unsigned long) bfd_get_32 (abfd, ft_data + j));
1318 fprintf (file, "\n");
1319 }
1320 #else
1321 for (j = 0; idx + j + 4 <= datasize; j += 4)
1322 {
1323 unsigned long member = bfd_get_32 (abfd, data + idx + j);
1324
1325 /* Print single IMAGE_IMPORT_BY_NAME vector. */
1326 if (member == 0)
1327 break;
1328
1329 if (HighBitSet (member))
1330 fprintf (file, "\t%04lx\t %4lu <none>",
1331 member, WithoutHighBit (member));
1332 else
1333 {
1334 int ordinal;
1335 char *member_name;
1336
1337 ordinal = bfd_get_16 (abfd, data + member - adj);
1338 member_name = (char *) data + member - adj + 2;
1339 fprintf (file, "\t%04lx\t %4d %s",
1340 member, ordinal, member_name);
1341 }
1342
1343 /* If the time stamp is not zero, the import address
1344 table holds actual addresses. */
1345 if (time_stamp != 0
1346 && first_thunk != 0
1347 && first_thunk != hint_addr
1348 && j + 4 <= ft_datasize)
1349 fprintf (file, "\t%04lx",
1350 (unsigned long) bfd_get_32 (abfd, ft_data + j));
1351
1352 fprintf (file, "\n");
1353 }
1354 #endif
1355 if (ft_allocated)
1356 free (ft_data);
1357 }
1358
1359 fprintf (file, "\n");
1360 }
1361
1362 free (data);
1363
1364 return TRUE;
1365 }
1366
1367 static bfd_boolean
1368 pe_print_edata (bfd * abfd, void * vfile)
1369 {
1370 FILE *file = (FILE *) vfile;
1371 bfd_byte *data;
1372 asection *section;
1373 bfd_size_type datasize = 0;
1374 bfd_size_type dataoff;
1375 bfd_size_type i;
1376 bfd_signed_vma adj;
1377 struct EDT_type
1378 {
1379 long export_flags; /* Reserved - should be zero. */
1380 long time_stamp;
1381 short major_ver;
1382 short minor_ver;
1383 bfd_vma name; /* RVA - relative to image base. */
1384 long base; /* Ordinal base. */
1385 unsigned long num_functions;/* Number in the export address table. */
1386 unsigned long num_names; /* Number in the name pointer table. */
1387 bfd_vma eat_addr; /* RVA to the export address table. */
1388 bfd_vma npt_addr; /* RVA to the Export Name Pointer Table. */
1389 bfd_vma ot_addr; /* RVA to the Ordinal Table. */
1390 } edt;
1391
1392 pe_data_type *pe = pe_data (abfd);
1393 struct internal_extra_pe_aouthdr *extra = &pe->pe_opthdr;
1394
1395 bfd_vma addr;
1396
1397 addr = extra->DataDirectory[PE_EXPORT_TABLE].VirtualAddress;
1398
1399 if (addr == 0 && extra->DataDirectory[PE_EXPORT_TABLE].Size == 0)
1400 {
1401 /* Maybe the extra header isn't there. Look for the section. */
1402 section = bfd_get_section_by_name (abfd, ".edata");
1403 if (section == NULL)
1404 return TRUE;
1405
1406 addr = section->vma;
1407 dataoff = 0;
1408 datasize = section->size;
1409 if (datasize == 0)
1410 return TRUE;
1411 }
1412 else
1413 {
1414 addr += extra->ImageBase;
1415
1416 for (section = abfd->sections; section != NULL; section = section->next)
1417 if (addr >= section->vma && addr < section->vma + section->size)
1418 break;
1419
1420 if (section == NULL)
1421 {
1422 fprintf (file,
1423 _("\nThere is an export table, but the section containing it could not be found\n"));
1424 return TRUE;
1425 }
1426
1427 dataoff = addr - section->vma;
1428 datasize = extra->DataDirectory[PE_EXPORT_TABLE].Size;
1429 if (datasize > section->size - dataoff)
1430 {
1431 fprintf (file,
1432 _("\nThere is an export table in %s, but it does not fit into that section\n"),
1433 section->name);
1434 return TRUE;
1435 }
1436 }
1437
1438 fprintf (file, _("\nThere is an export table in %s at 0x%lx\n"),
1439 section->name, (unsigned long) addr);
1440
1441 data = (bfd_byte *) bfd_malloc (datasize);
1442 if (data == NULL)
1443 return FALSE;
1444
1445 if (! bfd_get_section_contents (abfd, section, data,
1446 (file_ptr) dataoff, datasize))
1447 return FALSE;
1448
1449 /* Go get Export Directory Table. */
1450 edt.export_flags = bfd_get_32 (abfd, data + 0);
1451 edt.time_stamp = bfd_get_32 (abfd, data + 4);
1452 edt.major_ver = bfd_get_16 (abfd, data + 8);
1453 edt.minor_ver = bfd_get_16 (abfd, data + 10);
1454 edt.name = bfd_get_32 (abfd, data + 12);
1455 edt.base = bfd_get_32 (abfd, data + 16);
1456 edt.num_functions = bfd_get_32 (abfd, data + 20);
1457 edt.num_names = bfd_get_32 (abfd, data + 24);
1458 edt.eat_addr = bfd_get_32 (abfd, data + 28);
1459 edt.npt_addr = bfd_get_32 (abfd, data + 32);
1460 edt.ot_addr = bfd_get_32 (abfd, data + 36);
1461
1462 adj = section->vma - extra->ImageBase + dataoff;
1463
1464 /* Dump the EDT first. */
1465 fprintf (file,
1466 _("\nThe Export Tables (interpreted %s section contents)\n\n"),
1467 section->name);
1468
1469 fprintf (file,
1470 _("Export Flags \t\t\t%lx\n"), (unsigned long) edt.export_flags);
1471
1472 fprintf (file,
1473 _("Time/Date stamp \t\t%lx\n"), (unsigned long) edt.time_stamp);
1474
1475 fprintf (file,
1476 _("Major/Minor \t\t\t%d/%d\n"), edt.major_ver, edt.minor_ver);
1477
1478 fprintf (file,
1479 _("Name \t\t\t\t"));
1480 bfd_fprintf_vma (abfd, file, edt.name);
1481 fprintf (file,
1482 " %s\n", data + edt.name - adj);
1483
1484 fprintf (file,
1485 _("Ordinal Base \t\t\t%ld\n"), edt.base);
1486
1487 fprintf (file,
1488 _("Number in:\n"));
1489
1490 fprintf (file,
1491 _("\tExport Address Table \t\t%08lx\n"),
1492 edt.num_functions);
1493
1494 fprintf (file,
1495 _("\t[Name Pointer/Ordinal] Table\t%08lx\n"), edt.num_names);
1496
1497 fprintf (file,
1498 _("Table Addresses\n"));
1499
1500 fprintf (file,
1501 _("\tExport Address Table \t\t"));
1502 bfd_fprintf_vma (abfd, file, edt.eat_addr);
1503 fprintf (file, "\n");
1504
1505 fprintf (file,
1506 _("\tName Pointer Table \t\t"));
1507 bfd_fprintf_vma (abfd, file, edt.npt_addr);
1508 fprintf (file, "\n");
1509
1510 fprintf (file,
1511 _("\tOrdinal Table \t\t\t"));
1512 bfd_fprintf_vma (abfd, file, edt.ot_addr);
1513 fprintf (file, "\n");
1514
1515 /* The next table to find is the Export Address Table. It's basically
1516 a list of pointers that either locate a function in this dll, or
1517 forward the call to another dll. Something like:
1518 typedef union
1519 {
1520 long export_rva;
1521 long forwarder_rva;
1522 } export_address_table_entry; */
1523
1524 fprintf (file,
1525 _("\nExport Address Table -- Ordinal Base %ld\n"),
1526 edt.base);
1527
1528 for (i = 0; i < edt.num_functions; ++i)
1529 {
1530 bfd_vma eat_member = bfd_get_32 (abfd,
1531 data + edt.eat_addr + (i * 4) - adj);
1532 if (eat_member == 0)
1533 continue;
1534
1535 if (eat_member - adj <= datasize)
1536 {
1537 /* This rva is to a name (forwarding function) in our section. */
1538 /* Should locate a function descriptor. */
1539 fprintf (file,
1540 "\t[%4ld] +base[%4ld] %04lx %s -- %s\n",
1541 (long) i,
1542 (long) (i + edt.base),
1543 (unsigned long) eat_member,
1544 _("Forwarder RVA"),
1545 data + eat_member - adj);
1546 }
1547 else
1548 {
1549 /* Should locate a function descriptor in the reldata section. */
1550 fprintf (file,
1551 "\t[%4ld] +base[%4ld] %04lx %s\n",
1552 (long) i,
1553 (long) (i + edt.base),
1554 (unsigned long) eat_member,
1555 _("Export RVA"));
1556 }
1557 }
1558
1559 /* The Export Name Pointer Table is paired with the Export Ordinal Table. */
1560 /* Dump them in parallel for clarity. */
1561 fprintf (file,
1562 _("\n[Ordinal/Name Pointer] Table\n"));
1563
1564 for (i = 0; i < edt.num_names; ++i)
1565 {
1566 bfd_vma name_ptr = bfd_get_32 (abfd,
1567 data +
1568 edt.npt_addr
1569 + (i*4) - adj);
1570
1571 char *name = (char *) data + name_ptr - adj;
1572
1573 bfd_vma ord = bfd_get_16 (abfd,
1574 data +
1575 edt.ot_addr
1576 + (i*2) - adj);
1577 fprintf (file,
1578 "\t[%4ld] %s\n", (long) ord, name);
1579 }
1580
1581 free (data);
1582
1583 return TRUE;
1584 }
1585
1586 /* This really is architecture dependent. On IA-64, a .pdata entry
1587 consists of three dwords containing relative virtual addresses that
1588 specify the start and end address of the code range the entry
1589 covers and the address of the corresponding unwind info data.
1590
1591 On ARM and SH-4, a compressed PDATA structure is used :
1592 _IMAGE_CE_RUNTIME_FUNCTION_ENTRY, whereas MIPS is documented to use
1593 _IMAGE_ALPHA_RUNTIME_FUNCTION_ENTRY.
1594 See http://msdn2.microsoft.com/en-us/library/ms253988(VS.80).aspx .
1595
1596 This is the version for uncompressed data. */
1597
1598 static bfd_boolean
1599 pe_print_pdata (bfd * abfd, void * vfile)
1600 {
1601 #if defined(COFF_WITH_pep) && !defined(COFF_WITH_pex64)
1602 # define PDATA_ROW_SIZE (3 * 8)
1603 #else
1604 # define PDATA_ROW_SIZE (5 * 4)
1605 #endif
1606 FILE *file = (FILE *) vfile;
1607 bfd_byte *data = 0;
1608 asection *section = bfd_get_section_by_name (abfd, ".pdata");
1609 bfd_size_type datasize = 0;
1610 bfd_size_type i;
1611 bfd_size_type start, stop;
1612 int onaline = PDATA_ROW_SIZE;
1613
1614 if (section == NULL
1615 || coff_section_data (abfd, section) == NULL
1616 || pei_section_data (abfd, section) == NULL)
1617 return TRUE;
1618
1619 stop = pei_section_data (abfd, section)->virt_size;
1620 if ((stop % onaline) != 0)
1621 fprintf (file,
1622 _("Warning, .pdata section size (%ld) is not a multiple of %d\n"),
1623 (long) stop, onaline);
1624
1625 fprintf (file,
1626 _("\nThe Function Table (interpreted .pdata section contents)\n"));
1627 #if defined(COFF_WITH_pep) && !defined(COFF_WITH_pex64)
1628 fprintf (file,
1629 _(" vma:\t\t\tBegin Address End Address Unwind Info\n"));
1630 #else
1631 fprintf (file, _("\
1632 vma:\t\tBegin End EH EH PrologEnd Exception\n\
1633 \t\tAddress Address Handler Data Address Mask\n"));
1634 #endif
1635
1636 datasize = section->size;
1637 if (datasize == 0)
1638 return TRUE;
1639
1640 if (! bfd_malloc_and_get_section (abfd, section, &data))
1641 {
1642 if (data != NULL)
1643 free (data);
1644 return FALSE;
1645 }
1646
1647 start = 0;
1648
1649 for (i = start; i < stop; i += onaline)
1650 {
1651 bfd_vma begin_addr;
1652 bfd_vma end_addr;
1653 bfd_vma eh_handler;
1654 bfd_vma eh_data;
1655 bfd_vma prolog_end_addr;
1656 #if !defined(COFF_WITH_pep) || defined(COFF_WITH_pex64)
1657 int em_data;
1658 #endif
1659
1660 if (i + PDATA_ROW_SIZE > stop)
1661 break;
1662
1663 begin_addr = GET_PDATA_ENTRY (abfd, data + i );
1664 end_addr = GET_PDATA_ENTRY (abfd, data + i + 4);
1665 eh_handler = GET_PDATA_ENTRY (abfd, data + i + 8);
1666 eh_data = GET_PDATA_ENTRY (abfd, data + i + 12);
1667 prolog_end_addr = GET_PDATA_ENTRY (abfd, data + i + 16);
1668
1669 if (begin_addr == 0 && end_addr == 0 && eh_handler == 0
1670 && eh_data == 0 && prolog_end_addr == 0)
1671 /* We are probably into the padding of the section now. */
1672 break;
1673
1674 #if !defined(COFF_WITH_pep) || defined(COFF_WITH_pex64)
1675 em_data = ((eh_handler & 0x1) << 2) | (prolog_end_addr & 0x3);
1676 #endif
1677 eh_handler &= ~(bfd_vma) 0x3;
1678 prolog_end_addr &= ~(bfd_vma) 0x3;
1679
1680 fputc (' ', file);
1681 bfd_fprintf_vma (abfd, file, i + section->vma); fputc ('\t', file);
1682 bfd_fprintf_vma (abfd, file, begin_addr); fputc (' ', file);
1683 bfd_fprintf_vma (abfd, file, end_addr); fputc (' ', file);
1684 bfd_fprintf_vma (abfd, file, eh_handler);
1685 #if !defined(COFF_WITH_pep) || defined(COFF_WITH_pex64)
1686 fputc (' ', file);
1687 bfd_fprintf_vma (abfd, file, eh_data); fputc (' ', file);
1688 bfd_fprintf_vma (abfd, file, prolog_end_addr);
1689 fprintf (file, " %x", em_data);
1690 #endif
1691
1692 #ifdef POWERPC_LE_PE
1693 if (eh_handler == 0 && eh_data != 0)
1694 {
1695 /* Special bits here, although the meaning may be a little
1696 mysterious. The only one I know for sure is 0x03
1697 Code Significance
1698 0x00 None
1699 0x01 Register Save Millicode
1700 0x02 Register Restore Millicode
1701 0x03 Glue Code Sequence. */
1702 switch (eh_data)
1703 {
1704 case 0x01:
1705 fprintf (file, _(" Register save millicode"));
1706 break;
1707 case 0x02:
1708 fprintf (file, _(" Register restore millicode"));
1709 break;
1710 case 0x03:
1711 fprintf (file, _(" Glue code sequence"));
1712 break;
1713 default:
1714 break;
1715 }
1716 }
1717 #endif
1718 fprintf (file, "\n");
1719 }
1720
1721 free (data);
1722
1723 return TRUE;
1724 #undef PDATA_ROW_SIZE
1725 }
1726
1727 typedef struct sym_cache
1728 {
1729 int symcount;
1730 asymbol ** syms;
1731 } sym_cache;
1732
1733 static asymbol **
1734 slurp_symtab (bfd *abfd, sym_cache *psc)
1735 {
1736 asymbol ** sy = NULL;
1737 long storage;
1738
1739 if (!(bfd_get_file_flags (abfd) & HAS_SYMS))
1740 {
1741 psc->symcount = 0;
1742 return NULL;
1743 }
1744
1745 storage = bfd_get_symtab_upper_bound (abfd);
1746 if (storage < 0)
1747 return NULL;
1748 if (storage)
1749 sy = (asymbol **) bfd_malloc (storage);
1750
1751 psc->symcount = bfd_canonicalize_symtab (abfd, sy);
1752 if (psc->symcount < 0)
1753 return NULL;
1754 return sy;
1755 }
1756
1757 static const char *
1758 my_symbol_for_address (bfd *abfd, bfd_vma func, sym_cache *psc)
1759 {
1760 int i;
1761
1762 if (psc->syms == 0)
1763 psc->syms = slurp_symtab (abfd, psc);
1764
1765 for (i = 0; i < psc->symcount; i++)
1766 {
1767 if (psc->syms[i]->section->vma + psc->syms[i]->value == func)
1768 return psc->syms[i]->name;
1769 }
1770
1771 return NULL;
1772 }
1773
1774 static void
1775 cleanup_syms (sym_cache *psc)
1776 {
1777 psc->symcount = 0;
1778 free (psc->syms);
1779 psc->syms = NULL;
1780 }
1781
1782 /* This is the version for "compressed" pdata. */
1783
1784 bfd_boolean
1785 _bfd_XX_print_ce_compressed_pdata (bfd * abfd, void * vfile)
1786 {
1787 # define PDATA_ROW_SIZE (2 * 4)
1788 FILE *file = (FILE *) vfile;
1789 bfd_byte *data = NULL;
1790 asection *section = bfd_get_section_by_name (abfd, ".pdata");
1791 bfd_size_type datasize = 0;
1792 bfd_size_type i;
1793 bfd_size_type start, stop;
1794 int onaline = PDATA_ROW_SIZE;
1795 struct sym_cache cache = {0, 0} ;
1796
1797 if (section == NULL
1798 || coff_section_data (abfd, section) == NULL
1799 || pei_section_data (abfd, section) == NULL)
1800 return TRUE;
1801
1802 stop = pei_section_data (abfd, section)->virt_size;
1803 if ((stop % onaline) != 0)
1804 fprintf (file,
1805 _("Warning, .pdata section size (%ld) is not a multiple of %d\n"),
1806 (long) stop, onaline);
1807
1808 fprintf (file,
1809 _("\nThe Function Table (interpreted .pdata section contents)\n"));
1810
1811 fprintf (file, _("\
1812 vma:\t\tBegin Prolog Function Flags Exception EH\n\
1813 \t\tAddress Length Length 32b exc Handler Data\n"));
1814
1815 datasize = section->size;
1816 if (datasize == 0)
1817 return TRUE;
1818
1819 if (! bfd_malloc_and_get_section (abfd, section, &data))
1820 {
1821 if (data != NULL)
1822 free (data);
1823 return FALSE;
1824 }
1825
1826 start = 0;
1827
1828 for (i = start; i < stop; i += onaline)
1829 {
1830 bfd_vma begin_addr;
1831 bfd_vma other_data;
1832 bfd_vma prolog_length, function_length;
1833 int flag32bit, exception_flag;
1834 asection *tsection;
1835
1836 if (i + PDATA_ROW_SIZE > stop)
1837 break;
1838
1839 begin_addr = GET_PDATA_ENTRY (abfd, data + i );
1840 other_data = GET_PDATA_ENTRY (abfd, data + i + 4);
1841
1842 if (begin_addr == 0 && other_data == 0)
1843 /* We are probably into the padding of the section now. */
1844 break;
1845
1846 prolog_length = (other_data & 0x000000FF);
1847 function_length = (other_data & 0x3FFFFF00) >> 8;
1848 flag32bit = (int)((other_data & 0x40000000) >> 30);
1849 exception_flag = (int)((other_data & 0x80000000) >> 31);
1850
1851 fputc (' ', file);
1852 bfd_fprintf_vma (abfd, file, i + section->vma); fputc ('\t', file);
1853 bfd_fprintf_vma (abfd, file, begin_addr); fputc (' ', file);
1854 bfd_fprintf_vma (abfd, file, prolog_length); fputc (' ', file);
1855 bfd_fprintf_vma (abfd, file, function_length); fputc (' ', file);
1856 fprintf (file, "%2d %2d ", flag32bit, exception_flag);
1857
1858 /* Get the exception handler's address and the data passed from the
1859 .text section. This is really the data that belongs with the .pdata
1860 but got "compressed" out for the ARM and SH4 architectures. */
1861 tsection = bfd_get_section_by_name (abfd, ".text");
1862 if (tsection && coff_section_data (abfd, tsection)
1863 && pei_section_data (abfd, tsection))
1864 {
1865 bfd_vma eh_off = (begin_addr - 8) - tsection->vma;
1866 bfd_byte *tdata;
1867
1868 tdata = (bfd_byte *) bfd_malloc (8);
1869 if (tdata)
1870 {
1871 if (bfd_get_section_contents (abfd, tsection, tdata, eh_off, 8))
1872 {
1873 bfd_vma eh, eh_data;
1874
1875 eh = bfd_get_32 (abfd, tdata);
1876 eh_data = bfd_get_32 (abfd, tdata + 4);
1877 fprintf (file, "%08x ", (unsigned int) eh);
1878 fprintf (file, "%08x", (unsigned int) eh_data);
1879 if (eh != 0)
1880 {
1881 const char *s = my_symbol_for_address (abfd, eh, &cache);
1882
1883 if (s)
1884 fprintf (file, " (%s) ", s);
1885 }
1886 }
1887 free (tdata);
1888 }
1889 }
1890
1891 fprintf (file, "\n");
1892 }
1893
1894 free (data);
1895
1896 cleanup_syms (& cache);
1897
1898 return TRUE;
1899 #undef PDATA_ROW_SIZE
1900 }
1901
1902 \f
1903 #define IMAGE_REL_BASED_HIGHADJ 4
1904 static const char * const tbl[] =
1905 {
1906 "ABSOLUTE",
1907 "HIGH",
1908 "LOW",
1909 "HIGHLOW",
1910 "HIGHADJ",
1911 "MIPS_JMPADDR",
1912 "SECTION",
1913 "REL32",
1914 "RESERVED1",
1915 "MIPS_JMPADDR16",
1916 "DIR64",
1917 "HIGH3ADJ",
1918 "UNKNOWN", /* MUST be last. */
1919 };
1920
1921 static bfd_boolean
1922 pe_print_reloc (bfd * abfd, void * vfile)
1923 {
1924 FILE *file = (FILE *) vfile;
1925 bfd_byte *data = 0;
1926 asection *section = bfd_get_section_by_name (abfd, ".reloc");
1927 bfd_size_type i;
1928 bfd_size_type start, stop;
1929
1930 if (section == NULL)
1931 return TRUE;
1932
1933 if (section->size == 0)
1934 return TRUE;
1935
1936 fprintf (file,
1937 _("\n\nPE File Base Relocations (interpreted .reloc section contents)\n"));
1938
1939 if (! bfd_malloc_and_get_section (abfd, section, &data))
1940 {
1941 if (data != NULL)
1942 free (data);
1943 return FALSE;
1944 }
1945
1946 start = 0;
1947
1948 stop = section->size;
1949
1950 for (i = start; i < stop;)
1951 {
1952 int j;
1953 bfd_vma virtual_address;
1954 long number, size;
1955
1956 /* The .reloc section is a sequence of blocks, with a header consisting
1957 of two 32 bit quantities, followed by a number of 16 bit entries. */
1958 virtual_address = bfd_get_32 (abfd, data+i);
1959 size = bfd_get_32 (abfd, data+i+4);
1960 number = (size - 8) / 2;
1961
1962 if (size == 0)
1963 break;
1964
1965 fprintf (file,
1966 _("\nVirtual Address: %08lx Chunk size %ld (0x%lx) Number of fixups %ld\n"),
1967 (unsigned long) virtual_address, size, (unsigned long) size, number);
1968
1969 for (j = 0; j < number; ++j)
1970 {
1971 unsigned short e = bfd_get_16 (abfd, data + i + 8 + j * 2);
1972 unsigned int t = (e & 0xF000) >> 12;
1973 int off = e & 0x0FFF;
1974
1975 if (t >= sizeof (tbl) / sizeof (tbl[0]))
1976 t = (sizeof (tbl) / sizeof (tbl[0])) - 1;
1977
1978 fprintf (file,
1979 _("\treloc %4d offset %4x [%4lx] %s"),
1980 j, off, (unsigned long) (off + virtual_address), tbl[t]);
1981
1982 /* HIGHADJ takes an argument, - the next record *is* the
1983 low 16 bits of addend. */
1984 if (t == IMAGE_REL_BASED_HIGHADJ)
1985 {
1986 fprintf (file, " (%4x)",
1987 ((unsigned int)
1988 bfd_get_16 (abfd, data + i + 8 + j * 2 + 2)));
1989 j++;
1990 }
1991
1992 fprintf (file, "\n");
1993 }
1994
1995 i += size;
1996 }
1997
1998 free (data);
1999
2000 return TRUE;
2001 }
2002 \f
2003
2004 static bfd_byte *
2005 rsrc_print_resource_directory (FILE * , bfd *, unsigned int,
2006 bfd_byte *, bfd_byte *, bfd_byte *, bfd_vma);
2007
2008 static bfd_byte *
2009 rsrc_print_resource_entries (FILE * file,
2010 bfd * abfd,
2011 unsigned int indent,
2012 bfd_boolean is_name,
2013 bfd_byte * datastart,
2014 bfd_byte * data,
2015 bfd_byte * dataend,
2016 bfd_vma rva_bias)
2017 {
2018 unsigned long entry, addr, size;
2019
2020 if (data + 8 >= dataend)
2021 return dataend + 1;
2022
2023 fprintf (file, _("%*.s Entry: "), indent, " ");
2024
2025 entry = (long) bfd_get_32 (abfd, data);
2026 if (is_name)
2027 {
2028 bfd_byte * name;
2029
2030 /* Note - the documenation says that this field is an RVA value
2031 but windres appears to produce a section relative offset with
2032 the top bit set. Support both styles for now. */
2033 if (HighBitSet (entry))
2034 name = datastart + WithoutHighBit (entry);
2035 else
2036 name = datastart + entry - rva_bias;
2037
2038 if (name + 2 < dataend)
2039 {
2040 unsigned int len;
2041 len = bfd_get_16 (abfd, name);
2042
2043 fprintf (file, _("name: [val: %08lx len %d]: "), entry, len);
2044 if (name + 2 + len * 2 < dataend)
2045 {
2046 /* This strange loop is to cope with multibyte characters. */
2047 while (len --)
2048 {
2049 name += 2;
2050 fprintf (file, "%.1s", name);
2051 }
2052 }
2053 else
2054 fprintf (file, _("<corrupt string length: %#x>"), len);
2055 }
2056 else
2057 fprintf (file, _("<corrupt string offset: %#lx>"), entry);
2058 }
2059 else
2060 fprintf (file, _("ID: %#08lx"), entry);
2061
2062 entry = (long) bfd_get_32 (abfd, data + 4);
2063 fprintf (file, _(", Value: %#08lx\n"), entry);
2064
2065 if (HighBitSet (entry))
2066 return rsrc_print_resource_directory (file, abfd, indent + 1,
2067 datastart,
2068 datastart + WithoutHighBit (entry),
2069 dataend, rva_bias);
2070
2071 if (datastart + entry + 16 >= dataend)
2072 return dataend + 1;
2073
2074 fprintf (file, _("%*.s Leaf: Addr: %#08lx, Size: %#08lx, Codepage: %d\n"),
2075 indent, " ",
2076 addr = (long) bfd_get_32 (abfd, datastart + entry),
2077 size = (long) bfd_get_32 (abfd, datastart + entry + 4),
2078 (int) bfd_get_32 (abfd, datastart + entry + 8));
2079
2080 /* Check that the reserved entry is 0. */
2081 if (bfd_get_32 (abfd, datastart + entry + 12) != 0
2082 /* And that the data address/size is valid too. */
2083 || (datastart + (addr - rva_bias) + size > dataend))
2084 return dataend + 1;
2085
2086 return datastart + (addr - rva_bias) + size;
2087 }
2088
2089 #define max(a,b) ((a) > (b) ? (a) : (b))
2090 #define min(a,b) ((a) < (b) ? (a) : (b))
2091
2092 static bfd_byte *
2093 rsrc_print_resource_directory (FILE * file,
2094 bfd * abfd,
2095 unsigned int indent,
2096 bfd_byte * datastart,
2097 bfd_byte * data,
2098 bfd_byte * dataend,
2099 bfd_vma rva_bias)
2100 {
2101 unsigned int num_names, num_ids;
2102 bfd_byte * highest_data = data;
2103
2104 if (data + 16 >= dataend)
2105 return dataend + 1;
2106
2107 fprintf (file, "%*.s ", indent, " ");
2108 switch (indent)
2109 {
2110 case 0: fprintf (file, "Type"); break;
2111 case 2: fprintf (file, "Name"); break;
2112 case 4: fprintf (file, "Language"); break;
2113 default: fprintf (file, "<unknown>"); break;
2114 }
2115
2116 fprintf (file, _(" Table: Char: %d, Time: %08lx, Ver: %d/%d, Num Names: %d, IDs: %d\n"),
2117 (int) bfd_get_32 (abfd, data),
2118 (long) bfd_get_32 (abfd, data + 4),
2119 (int) bfd_get_16 (abfd, data + 8),
2120 (int) bfd_get_16 (abfd, data + 10),
2121 num_names = (int) bfd_get_16 (abfd, data + 12),
2122 num_ids = (int) bfd_get_16 (abfd, data + 14));
2123 data += 16;
2124
2125 while (num_names --)
2126 {
2127 bfd_byte * entry_end;
2128
2129 entry_end = rsrc_print_resource_entries (file, abfd, indent + 1, TRUE,
2130 datastart, data, dataend, rva_bias);
2131 data += 8;
2132 highest_data = max (highest_data, entry_end);
2133 if (entry_end >= dataend)
2134 return entry_end;
2135 }
2136
2137 while (num_ids --)
2138 {
2139 bfd_byte * entry_end;
2140
2141 entry_end = rsrc_print_resource_entries (file, abfd, indent + 1, FALSE,
2142 datastart, data, dataend, rva_bias);
2143 data += 8;
2144 highest_data = max (highest_data, entry_end);
2145 if (entry_end >= dataend)
2146 return entry_end;
2147 }
2148
2149 return max (highest_data, data);
2150 }
2151
2152 /* Display the contents of a .rsrc section. We do not try to
2153 reproduce the resources, windres does that. Instead we dump
2154 the tables in a human readable format. */
2155
2156 static bfd_boolean
2157 rsrc_print_section (bfd * abfd, void * vfile)
2158 {
2159 bfd_vma rva_bias;
2160 pe_data_type * pe;
2161 FILE * file = (FILE *) vfile;
2162 bfd_size_type datasize;
2163 asection * section;
2164 bfd_byte * data;
2165 bfd_byte * dataend;
2166 bfd_byte * datastart;
2167
2168
2169 pe = pe_data (abfd);
2170 if (pe == NULL)
2171 return TRUE;
2172
2173 section = bfd_get_section_by_name (abfd, ".rsrc");
2174 if (section == NULL)
2175 return TRUE;
2176
2177 rva_bias = section->vma - pe->pe_opthdr.ImageBase;
2178
2179 datasize = section->size;
2180 if (datasize == 0)
2181 return TRUE;
2182
2183 if (! bfd_malloc_and_get_section (abfd, section, & data))
2184 {
2185 if (data != NULL)
2186 free (data);
2187 return FALSE;
2188 }
2189 datastart = data;
2190 dataend = data + datasize;
2191
2192 fflush (file);
2193 fprintf (file, "\nThe .rsrc Resource Directory section:\n");
2194
2195 while (data < dataend)
2196 {
2197 bfd_byte * p = data;
2198
2199 data = rsrc_print_resource_directory (file, abfd, 0, data, data, dataend, rva_bias);
2200
2201 if (data == dataend + 1)
2202 fprintf (file, _("Corrupt .rsrc section detected!\n"));
2203 else
2204 {
2205 /* Align data before continuing. */
2206 int align = (1 << section->alignment_power) - 1;
2207
2208 data = (bfd_byte *) (((long) (data + align)) & ~ align);
2209 rva_bias += data - p;
2210
2211 /* For reasons that are unclear .rsrc sections are sometimes created
2212 aligned to a 1^3 boundary even when their alignment is set at
2213 1^2. Catch that case here before we issue a spurious warning
2214 message. */
2215 if (data == (dataend - 4))
2216 data = dataend;
2217 else if (data < dataend)
2218 fprintf (file, _("\nWARNING: Extra data in .rsrc section - it will be ignored by Windows:\n"));
2219 }
2220 }
2221
2222 free (datastart);
2223 return TRUE;
2224 }
2225
2226 /* Print out the program headers. */
2227
2228 bfd_boolean
2229 _bfd_XX_print_private_bfd_data_common (bfd * abfd, void * vfile)
2230 {
2231 FILE *file = (FILE *) vfile;
2232 int j;
2233 pe_data_type *pe = pe_data (abfd);
2234 struct internal_extra_pe_aouthdr *i = &pe->pe_opthdr;
2235 const char *subsystem_name = NULL;
2236 const char *name;
2237
2238 /* The MS dumpbin program reportedly ands with 0xff0f before
2239 printing the characteristics field. Not sure why. No reason to
2240 emulate it here. */
2241 fprintf (file, _("\nCharacteristics 0x%x\n"), pe->real_flags);
2242 #undef PF
2243 #define PF(x, y) if (pe->real_flags & x) { fprintf (file, "\t%s\n", y); }
2244 PF (IMAGE_FILE_RELOCS_STRIPPED, "relocations stripped");
2245 PF (IMAGE_FILE_EXECUTABLE_IMAGE, "executable");
2246 PF (IMAGE_FILE_LINE_NUMS_STRIPPED, "line numbers stripped");
2247 PF (IMAGE_FILE_LOCAL_SYMS_STRIPPED, "symbols stripped");
2248 PF (IMAGE_FILE_LARGE_ADDRESS_AWARE, "large address aware");
2249 PF (IMAGE_FILE_BYTES_REVERSED_LO, "little endian");
2250 PF (IMAGE_FILE_32BIT_MACHINE, "32 bit words");
2251 PF (IMAGE_FILE_DEBUG_STRIPPED, "debugging information removed");
2252 PF (IMAGE_FILE_SYSTEM, "system file");
2253 PF (IMAGE_FILE_DLL, "DLL");
2254 PF (IMAGE_FILE_BYTES_REVERSED_HI, "big endian");
2255 #undef PF
2256
2257 /* ctime implies '\n'. */
2258 {
2259 time_t t = pe->coff.timestamp;
2260 fprintf (file, "\nTime/Date\t\t%s", ctime (&t));
2261 }
2262
2263 #ifndef IMAGE_NT_OPTIONAL_HDR_MAGIC
2264 # define IMAGE_NT_OPTIONAL_HDR_MAGIC 0x10b
2265 #endif
2266 #ifndef IMAGE_NT_OPTIONAL_HDR64_MAGIC
2267 # define IMAGE_NT_OPTIONAL_HDR64_MAGIC 0x20b
2268 #endif
2269 #ifndef IMAGE_NT_OPTIONAL_HDRROM_MAGIC
2270 # define IMAGE_NT_OPTIONAL_HDRROM_MAGIC 0x107
2271 #endif
2272
2273 switch (i->Magic)
2274 {
2275 case IMAGE_NT_OPTIONAL_HDR_MAGIC:
2276 name = "PE32";
2277 break;
2278 case IMAGE_NT_OPTIONAL_HDR64_MAGIC:
2279 name = "PE32+";
2280 break;
2281 case IMAGE_NT_OPTIONAL_HDRROM_MAGIC:
2282 name = "ROM";
2283 break;
2284 default:
2285 name = NULL;
2286 break;
2287 }
2288 fprintf (file, "Magic\t\t\t%04x", i->Magic);
2289 if (name)
2290 fprintf (file, "\t(%s)",name);
2291 fprintf (file, "\nMajorLinkerVersion\t%d\n", i->MajorLinkerVersion);
2292 fprintf (file, "MinorLinkerVersion\t%d\n", i->MinorLinkerVersion);
2293 fprintf (file, "SizeOfCode\t\t%08lx\n", (unsigned long) i->SizeOfCode);
2294 fprintf (file, "SizeOfInitializedData\t%08lx\n",
2295 (unsigned long) i->SizeOfInitializedData);
2296 fprintf (file, "SizeOfUninitializedData\t%08lx\n",
2297 (unsigned long) i->SizeOfUninitializedData);
2298 fprintf (file, "AddressOfEntryPoint\t");
2299 bfd_fprintf_vma (abfd, file, i->AddressOfEntryPoint);
2300 fprintf (file, "\nBaseOfCode\t\t");
2301 bfd_fprintf_vma (abfd, file, i->BaseOfCode);
2302 #if !defined(COFF_WITH_pep) && !defined(COFF_WITH_pex64)
2303 /* PE32+ does not have BaseOfData member! */
2304 fprintf (file, "\nBaseOfData\t\t");
2305 bfd_fprintf_vma (abfd, file, i->BaseOfData);
2306 #endif
2307
2308 fprintf (file, "\nImageBase\t\t");
2309 bfd_fprintf_vma (abfd, file, i->ImageBase);
2310 fprintf (file, "\nSectionAlignment\t");
2311 bfd_fprintf_vma (abfd, file, i->SectionAlignment);
2312 fprintf (file, "\nFileAlignment\t\t");
2313 bfd_fprintf_vma (abfd, file, i->FileAlignment);
2314 fprintf (file, "\nMajorOSystemVersion\t%d\n", i->MajorOperatingSystemVersion);
2315 fprintf (file, "MinorOSystemVersion\t%d\n", i->MinorOperatingSystemVersion);
2316 fprintf (file, "MajorImageVersion\t%d\n", i->MajorImageVersion);
2317 fprintf (file, "MinorImageVersion\t%d\n", i->MinorImageVersion);
2318 fprintf (file, "MajorSubsystemVersion\t%d\n", i->MajorSubsystemVersion);
2319 fprintf (file, "MinorSubsystemVersion\t%d\n", i->MinorSubsystemVersion);
2320 fprintf (file, "Win32Version\t\t%08lx\n", (unsigned long) i->Reserved1);
2321 fprintf (file, "SizeOfImage\t\t%08lx\n", (unsigned long) i->SizeOfImage);
2322 fprintf (file, "SizeOfHeaders\t\t%08lx\n", (unsigned long) i->SizeOfHeaders);
2323 fprintf (file, "CheckSum\t\t%08lx\n", (unsigned long) i->CheckSum);
2324
2325 switch (i->Subsystem)
2326 {
2327 case IMAGE_SUBSYSTEM_UNKNOWN:
2328 subsystem_name = "unspecified";
2329 break;
2330 case IMAGE_SUBSYSTEM_NATIVE:
2331 subsystem_name = "NT native";
2332 break;
2333 case IMAGE_SUBSYSTEM_WINDOWS_GUI:
2334 subsystem_name = "Windows GUI";
2335 break;
2336 case IMAGE_SUBSYSTEM_WINDOWS_CUI:
2337 subsystem_name = "Windows CUI";
2338 break;
2339 case IMAGE_SUBSYSTEM_POSIX_CUI:
2340 subsystem_name = "POSIX CUI";
2341 break;
2342 case IMAGE_SUBSYSTEM_WINDOWS_CE_GUI:
2343 subsystem_name = "Wince CUI";
2344 break;
2345 // These are from UEFI Platform Initialization Specification 1.1.
2346 case IMAGE_SUBSYSTEM_EFI_APPLICATION:
2347 subsystem_name = "EFI application";
2348 break;
2349 case IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER:
2350 subsystem_name = "EFI boot service driver";
2351 break;
2352 case IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER:
2353 subsystem_name = "EFI runtime driver";
2354 break;
2355 case IMAGE_SUBSYSTEM_SAL_RUNTIME_DRIVER:
2356 subsystem_name = "SAL runtime driver";
2357 break;
2358 // This is from revision 8.0 of the MS PE/COFF spec
2359 case IMAGE_SUBSYSTEM_XBOX:
2360 subsystem_name = "XBOX";
2361 break;
2362 // Added default case for clarity - subsystem_name is NULL anyway.
2363 default:
2364 subsystem_name = NULL;
2365 }
2366
2367 fprintf (file, "Subsystem\t\t%08x", i->Subsystem);
2368 if (subsystem_name)
2369 fprintf (file, "\t(%s)", subsystem_name);
2370 fprintf (file, "\nDllCharacteristics\t%08x\n", i->DllCharacteristics);
2371 fprintf (file, "SizeOfStackReserve\t");
2372 bfd_fprintf_vma (abfd, file, i->SizeOfStackReserve);
2373 fprintf (file, "\nSizeOfStackCommit\t");
2374 bfd_fprintf_vma (abfd, file, i->SizeOfStackCommit);
2375 fprintf (file, "\nSizeOfHeapReserve\t");
2376 bfd_fprintf_vma (abfd, file, i->SizeOfHeapReserve);
2377 fprintf (file, "\nSizeOfHeapCommit\t");
2378 bfd_fprintf_vma (abfd, file, i->SizeOfHeapCommit);
2379 fprintf (file, "\nLoaderFlags\t\t%08lx\n", (unsigned long) i->LoaderFlags);
2380 fprintf (file, "NumberOfRvaAndSizes\t%08lx\n",
2381 (unsigned long) i->NumberOfRvaAndSizes);
2382
2383 fprintf (file, "\nThe Data Directory\n");
2384 for (j = 0; j < IMAGE_NUMBEROF_DIRECTORY_ENTRIES; j++)
2385 {
2386 fprintf (file, "Entry %1x ", j);
2387 bfd_fprintf_vma (abfd, file, i->DataDirectory[j].VirtualAddress);
2388 fprintf (file, " %08lx ", (unsigned long) i->DataDirectory[j].Size);
2389 fprintf (file, "%s\n", dir_names[j]);
2390 }
2391
2392 pe_print_idata (abfd, vfile);
2393 pe_print_edata (abfd, vfile);
2394 if (bfd_coff_have_print_pdata (abfd))
2395 bfd_coff_print_pdata (abfd, vfile);
2396 else
2397 pe_print_pdata (abfd, vfile);
2398 pe_print_reloc (abfd, vfile);
2399
2400 rsrc_print_section (abfd, vfile);
2401
2402 return TRUE;
2403 }
2404
2405 /* Copy any private info we understand from the input bfd
2406 to the output bfd. */
2407
2408 bfd_boolean
2409 _bfd_XX_bfd_copy_private_bfd_data_common (bfd * ibfd, bfd * obfd)
2410 {
2411 pe_data_type *ipe, *ope;
2412
2413 /* One day we may try to grok other private data. */
2414 if (ibfd->xvec->flavour != bfd_target_coff_flavour
2415 || obfd->xvec->flavour != bfd_target_coff_flavour)
2416 return TRUE;
2417
2418 ipe = pe_data (ibfd);
2419 ope = pe_data (obfd);
2420
2421 /* pe_opthdr is copied in copy_object. */
2422 ope->dll = ipe->dll;
2423
2424 /* Don't copy input subsystem if output is different from input. */
2425 if (obfd->xvec != ibfd->xvec)
2426 ope->pe_opthdr.Subsystem = IMAGE_SUBSYSTEM_UNKNOWN;
2427
2428 /* For strip: if we removed .reloc, we'll make a real mess of things
2429 if we don't remove this entry as well. */
2430 if (! pe_data (obfd)->has_reloc_section)
2431 {
2432 pe_data (obfd)->pe_opthdr.DataDirectory[PE_BASE_RELOCATION_TABLE].VirtualAddress = 0;
2433 pe_data (obfd)->pe_opthdr.DataDirectory[PE_BASE_RELOCATION_TABLE].Size = 0;
2434 }
2435
2436 /* For PIE, if there is .reloc, we won't add IMAGE_FILE_RELOCS_STRIPPED.
2437 But there is no .reloc, we make sure that IMAGE_FILE_RELOCS_STRIPPED
2438 won't be added. */
2439 if (! pe_data (ibfd)->has_reloc_section
2440 && ! (pe_data (ibfd)->real_flags & IMAGE_FILE_RELOCS_STRIPPED))
2441 pe_data (obfd)->dont_strip_reloc = 1;
2442
2443 return TRUE;
2444 }
2445
2446 /* Copy private section data. */
2447
2448 bfd_boolean
2449 _bfd_XX_bfd_copy_private_section_data (bfd *ibfd,
2450 asection *isec,
2451 bfd *obfd,
2452 asection *osec)
2453 {
2454 if (bfd_get_flavour (ibfd) != bfd_target_coff_flavour
2455 || bfd_get_flavour (obfd) != bfd_target_coff_flavour)
2456 return TRUE;
2457
2458 if (coff_section_data (ibfd, isec) != NULL
2459 && pei_section_data (ibfd, isec) != NULL)
2460 {
2461 if (coff_section_data (obfd, osec) == NULL)
2462 {
2463 bfd_size_type amt = sizeof (struct coff_section_tdata);
2464 osec->used_by_bfd = bfd_zalloc (obfd, amt);
2465 if (osec->used_by_bfd == NULL)
2466 return FALSE;
2467 }
2468
2469 if (pei_section_data (obfd, osec) == NULL)
2470 {
2471 bfd_size_type amt = sizeof (struct pei_section_tdata);
2472 coff_section_data (obfd, osec)->tdata = bfd_zalloc (obfd, amt);
2473 if (coff_section_data (obfd, osec)->tdata == NULL)
2474 return FALSE;
2475 }
2476
2477 pei_section_data (obfd, osec)->virt_size =
2478 pei_section_data (ibfd, isec)->virt_size;
2479 pei_section_data (obfd, osec)->pe_flags =
2480 pei_section_data (ibfd, isec)->pe_flags;
2481 }
2482
2483 return TRUE;
2484 }
2485
2486 void
2487 _bfd_XX_get_symbol_info (bfd * abfd, asymbol *symbol, symbol_info *ret)
2488 {
2489 coff_get_symbol_info (abfd, symbol, ret);
2490 }
2491
2492 #if !defined(COFF_WITH_pep) && defined(COFF_WITH_pex64)
2493 static int
2494 sort_x64_pdata (const void *l, const void *r)
2495 {
2496 const char *lp = (const char *) l;
2497 const char *rp = (const char *) r;
2498 bfd_vma vl, vr;
2499 vl = bfd_getl32 (lp); vr = bfd_getl32 (rp);
2500 if (vl != vr)
2501 return (vl < vr ? -1 : 1);
2502 /* We compare just begin address. */
2503 return 0;
2504 }
2505 #endif
2506 \f
2507 /* Functions to process a .rsrc section. */
2508
2509 static unsigned int sizeof_leaves;
2510 static unsigned int sizeof_strings;
2511 static unsigned int sizeof_tables_and_entries;
2512
2513 static bfd_byte *
2514 rsrc_count_directory (bfd *, bfd_byte *, bfd_byte *, bfd_byte *, bfd_vma);
2515
2516 static bfd_byte *
2517 rsrc_count_entries (bfd * abfd,
2518 bfd_boolean is_name,
2519 bfd_byte * datastart,
2520 bfd_byte * data,
2521 bfd_byte * dataend,
2522 bfd_vma rva_bias)
2523 {
2524 unsigned long entry, addr, size;
2525
2526 if (data + 8 >= dataend)
2527 return dataend + 1;
2528
2529 if (is_name)
2530 {
2531 bfd_byte * name;
2532
2533 entry = (long) bfd_get_32 (abfd, data);
2534
2535 if (HighBitSet (entry))
2536 name = datastart + WithoutHighBit (entry);
2537 else
2538 name = datastart + entry - rva_bias;
2539
2540 if (name + 2 >= dataend)
2541 return dataend + 1;
2542
2543 unsigned int len = bfd_get_16 (abfd, name);
2544 if (len == 0 || len > 256)
2545 return dataend + 1;
2546
2547 sizeof_strings += (len + 1) * 2;
2548 }
2549
2550 entry = (long) bfd_get_32 (abfd, data + 4);
2551
2552 if (HighBitSet (entry))
2553 return rsrc_count_directory (abfd,
2554 datastart,
2555 datastart + WithoutHighBit (entry),
2556 dataend, rva_bias);
2557
2558 if (datastart + entry + 16 >= dataend)
2559 return dataend + 1;
2560
2561 addr = (long) bfd_get_32 (abfd, datastart + entry);
2562 size = (long) bfd_get_32 (abfd, datastart + entry + 4);
2563
2564 sizeof_leaves += 16;
2565
2566 return datastart + addr - rva_bias + size;
2567 }
2568
2569 static bfd_byte *
2570 rsrc_count_directory (bfd * abfd,
2571 bfd_byte * datastart,
2572 bfd_byte * data,
2573 bfd_byte * dataend,
2574 bfd_vma rva_bias)
2575 {
2576 unsigned int num_entries, num_ids;
2577 bfd_byte * highest_data = data;
2578
2579 if (data + 16 >= dataend)
2580 return dataend + 1;
2581
2582 num_entries = (int) bfd_get_16 (abfd, data + 12);
2583 num_ids = (int) bfd_get_16 (abfd, data + 14);
2584
2585 num_entries += num_ids;
2586
2587 data += 16;
2588 sizeof_tables_and_entries += 16;
2589
2590 while (num_entries --)
2591 {
2592 bfd_byte * entry_end;
2593
2594 entry_end = rsrc_count_entries (abfd, num_entries >= num_ids,
2595 datastart, data, dataend, rva_bias);
2596 data += 8;
2597 sizeof_tables_and_entries += 8;
2598 highest_data = max (highest_data, entry_end);
2599 if (entry_end >= dataend)
2600 break;
2601 }
2602
2603 return max (highest_data, data);
2604 }
2605
2606 typedef struct rsrc_dir_chain
2607 {
2608 unsigned int num_entries;
2609 struct rsrc_entry * first_entry;
2610 struct rsrc_entry * last_entry;
2611 } rsrc_dir_chain;
2612
2613 typedef struct rsrc_directory
2614 {
2615 unsigned int characteristics;
2616 unsigned int time;
2617 unsigned int major;
2618 unsigned int minor;
2619
2620 rsrc_dir_chain names;
2621 rsrc_dir_chain ids;
2622
2623 struct rsrc_entry * entry;
2624 } rsrc_directory;
2625
2626 typedef struct rsrc_string
2627 {
2628 unsigned int len;
2629 bfd_byte * string;
2630 } rsrc_string;
2631
2632 typedef struct rsrc_leaf
2633 {
2634 unsigned int size;
2635 unsigned int codepage;
2636 bfd_byte * data;
2637 } rsrc_leaf;
2638
2639 typedef struct rsrc_entry
2640 {
2641 bfd_boolean is_name;
2642 union
2643 {
2644 unsigned int id;
2645 struct rsrc_string name;
2646 } name_id;
2647
2648 bfd_boolean is_dir;
2649 union
2650 {
2651 struct rsrc_directory * directory;
2652 struct rsrc_leaf * leaf;
2653 } value;
2654
2655 struct rsrc_entry * next_entry;
2656 struct rsrc_directory * parent;
2657 } rsrc_entry;
2658
2659 static bfd_byte *
2660 rsrc_parse_directory (bfd *, rsrc_directory *, bfd_byte *,
2661 bfd_byte *, bfd_byte *, bfd_vma, rsrc_entry *);
2662
2663 static bfd_byte *
2664 rsrc_parse_entry (bfd * abfd,
2665 bfd_boolean is_name,
2666 rsrc_entry * entry,
2667 bfd_byte * datastart,
2668 bfd_byte * data,
2669 bfd_byte * dataend,
2670 bfd_vma rva_bias,
2671 rsrc_directory * parent)
2672 {
2673 unsigned long val, addr, size;
2674
2675 val = bfd_get_32 (abfd, data);
2676
2677 entry->parent = parent;
2678 entry->is_name = is_name;
2679
2680 if (is_name)
2681 {
2682 /* FIXME: Add range checking ? */
2683 if (HighBitSet (val))
2684 {
2685 val = WithoutHighBit (val);
2686
2687 entry->name_id.name.len = bfd_get_16 (abfd, datastart + val);
2688 entry->name_id.name.string = datastart + val + 2;
2689 }
2690 else
2691 {
2692 entry->name_id.name.len = bfd_get_16 (abfd, datastart + val - rva_bias);
2693 entry->name_id.name.string = datastart + val - rva_bias + 2;
2694 }
2695 }
2696 else
2697 entry->name_id.id = val;
2698
2699 val = bfd_get_32 (abfd, data + 4);
2700
2701 if (HighBitSet (val))
2702 {
2703 entry->is_dir = TRUE;
2704 entry->value.directory = bfd_malloc (sizeof * entry->value.directory);
2705 if (entry->value.directory == NULL)
2706 return dataend;
2707
2708 return rsrc_parse_directory (abfd, entry->value.directory,
2709 datastart,
2710 datastart + WithoutHighBit (val),
2711 dataend, rva_bias, entry);
2712 }
2713
2714 entry->is_dir = FALSE;
2715 entry->value.leaf = bfd_malloc (sizeof * entry->value.leaf);
2716 if (entry->value.leaf == NULL)
2717 return dataend;
2718
2719 addr = bfd_get_32 (abfd, datastart + val);
2720 size = entry->value.leaf->size = bfd_get_32 (abfd, datastart + val + 4);
2721 entry->value.leaf->codepage = bfd_get_32 (abfd, datastart + val + 8);
2722
2723 entry->value.leaf->data = bfd_malloc (size);
2724 if (entry->value.leaf->data == NULL)
2725 return dataend;
2726
2727 memcpy (entry->value.leaf->data, datastart + addr - rva_bias, size);
2728 return datastart + (addr - rva_bias) + size;
2729 }
2730
2731 static bfd_byte *
2732 rsrc_parse_entries (bfd * abfd,
2733 rsrc_dir_chain * chain,
2734 bfd_boolean is_name,
2735 bfd_byte * highest_data,
2736 bfd_byte * datastart,
2737 bfd_byte * data,
2738 bfd_byte * dataend,
2739 bfd_vma rva_bias,
2740 rsrc_directory * parent)
2741 {
2742 rsrc_entry * entry;
2743
2744 if (chain->num_entries == 0)
2745 {
2746 chain->first_entry = chain->last_entry = NULL;
2747 return highest_data;
2748 }
2749
2750 entry = bfd_malloc (sizeof * entry);
2751 if (entry == NULL)
2752 return dataend;
2753
2754 chain->first_entry = entry;
2755
2756 unsigned int i;
2757 for (i = chain->num_entries; i--;)
2758 {
2759 bfd_byte * entry_end;
2760
2761 entry_end = rsrc_parse_entry (abfd, is_name, entry, datastart,
2762 data, dataend, rva_bias, parent);
2763 data += 8;
2764 highest_data = max (entry_end, highest_data);
2765 if (entry_end > dataend)
2766 return dataend;
2767
2768 if (i)
2769 {
2770 entry->next_entry = bfd_malloc (sizeof * entry);
2771 entry = entry->next_entry;
2772 if (entry == NULL)
2773 return dataend;
2774 }
2775 else
2776 entry->next_entry = NULL;
2777 }
2778
2779 chain->last_entry = entry;
2780
2781 return highest_data;
2782 }
2783
2784 static bfd_byte *
2785 rsrc_parse_directory (bfd * abfd,
2786 rsrc_directory * table,
2787 bfd_byte * datastart,
2788 bfd_byte * data,
2789 bfd_byte * dataend,
2790 bfd_vma rva_bias,
2791 rsrc_entry * entry)
2792 {
2793 bfd_byte * highest_data = data;
2794
2795 if (table == NULL)
2796 return dataend;
2797
2798 table->characteristics = bfd_get_32 (abfd, data);
2799 table->time = bfd_get_32 (abfd, data + 4);
2800 table->major = bfd_get_16 (abfd, data + 8);
2801 table->minor = bfd_get_16 (abfd, data + 10);
2802 table->names.num_entries = bfd_get_16 (abfd, data + 12);
2803 table->ids.num_entries = bfd_get_16 (abfd, data + 14);
2804 table->entry = entry;
2805
2806 data += 16;
2807
2808 highest_data = rsrc_parse_entries (abfd, & table->names, TRUE, data,
2809 datastart, data, dataend, rva_bias, table);
2810 data += table->names.num_entries * 8;
2811
2812 highest_data = rsrc_parse_entries (abfd, & table->ids, FALSE, highest_data,
2813 datastart, data, dataend, rva_bias, table);
2814 data += table->ids.num_entries * 8;
2815
2816 return max (highest_data, data);
2817 }
2818
2819 typedef struct rsrc_write_data
2820 {
2821 bfd * abfd;
2822 bfd_byte * datastart;
2823 bfd_byte * next_table;
2824 bfd_byte * next_leaf;
2825 bfd_byte * next_string;
2826 bfd_byte * next_data;
2827 bfd_vma rva_bias;
2828 } rsrc_write_data;
2829
2830 static void
2831 rsrc_write_string (rsrc_write_data * data,
2832 rsrc_string * string)
2833 {
2834 bfd_put_16 (data->abfd, string->len, data->next_string);
2835 memcpy (data->next_string + 2, string->string, string->len * 2);
2836 data->next_string += (string->len + 1) * 2;
2837 }
2838
2839 static inline unsigned int
2840 rsrc_compute_rva (rsrc_write_data * data,
2841 bfd_byte * addr)
2842 {
2843 return (addr - data->datastart) + data->rva_bias;
2844 }
2845
2846 static void
2847 rsrc_write_leaf (rsrc_write_data * data,
2848 rsrc_leaf * leaf)
2849 {
2850 bfd_put_32 (data->abfd, rsrc_compute_rva (data, data->next_data), data->next_leaf);
2851 bfd_put_32 (data->abfd, leaf->size, data->next_leaf + 4);
2852 bfd_put_32 (data->abfd, leaf->codepage, data->next_leaf + 8);
2853 bfd_put_32 (data->abfd, 0 /*reserved*/, data->next_leaf + 12);
2854 data->next_leaf += 16;
2855
2856 memcpy (data->next_data, leaf->data, leaf->size);
2857 data->next_data += leaf->size;
2858 }
2859
2860 static void rsrc_write_directory (rsrc_write_data *, rsrc_directory *);
2861
2862 static void
2863 rsrc_write_entry (rsrc_write_data * data,
2864 bfd_byte * where,
2865 rsrc_entry * entry)
2866 {
2867 if (entry->is_name)
2868 {
2869 bfd_put_32 (data->abfd,
2870 SetHighBit (data->next_string - data->datastart),
2871 where);
2872 rsrc_write_string (data, & entry->name_id.name);
2873 }
2874 else
2875 bfd_put_32 (data->abfd, entry->name_id.id, where);
2876
2877 if (entry->is_dir)
2878 {
2879 bfd_put_32 (data->abfd,
2880 SetHighBit (data->next_table - data->datastart),
2881 where + 4);
2882 rsrc_write_directory (data, entry->value.directory);
2883 }
2884 else
2885 {
2886 bfd_put_32 (data->abfd, data->next_leaf - data->datastart, where + 4);
2887 rsrc_write_leaf (data, entry->value.leaf);
2888 }
2889 }
2890
2891 static void
2892 rsrc_write_directory (rsrc_write_data * data,
2893 rsrc_directory * dir)
2894 {
2895 rsrc_entry * entry;
2896 unsigned int i;
2897
2898 bfd_put_32 (data->abfd, dir->characteristics, data->next_table);
2899 bfd_put_32 (data->abfd, 0 /*dir->time*/, data->next_table + 4);
2900 bfd_put_16 (data->abfd, dir->major, data->next_table + 8);
2901 bfd_put_16 (data->abfd, dir->minor, data->next_table + 10);
2902 bfd_put_16 (data->abfd, dir->names.num_entries, data->next_table + 12);
2903 bfd_put_16 (data->abfd, dir->ids.num_entries, data->next_table + 14);
2904
2905 /* Compute where the entries and the next table will be placed. */
2906 bfd_byte * next_entry = data->next_table + 16;
2907 data->next_table = next_entry + (dir->names.num_entries * 8) + (dir->ids.num_entries * 8);
2908 bfd_byte * nt = data->next_table;
2909
2910 /* Write the entries. */
2911 for (i = dir->names.num_entries, entry = dir->names.first_entry;
2912 i > 0 && entry != NULL;
2913 i--, entry = entry->next_entry)
2914 {
2915 rsrc_write_entry (data, next_entry, entry);
2916 next_entry += 8;
2917 }
2918 BFD_ASSERT (i == 0);
2919 BFD_ASSERT (entry == NULL);
2920
2921 for (i = dir->ids.num_entries, entry = dir->ids.first_entry;
2922 i > 0 && entry != NULL;
2923 i--, entry = entry->next_entry)
2924 {
2925 rsrc_write_entry (data, next_entry, entry);
2926 next_entry += 8;
2927 }
2928 BFD_ASSERT (i == 0);
2929 BFD_ASSERT (entry == NULL);
2930 BFD_ASSERT (nt == next_entry);
2931 }
2932
2933 #ifdef HAVE_WCHAR_H
2934 /* Return the length (number of units) of the first character in S,
2935 putting its 'ucs4_t' representation in *PUC. */
2936
2937 static unsigned int
2938 u16_mbtouc (wchar_t * puc, const unsigned short * s, unsigned int n)
2939 {
2940 unsigned short c = * s;
2941
2942 if (c < 0xd800 || c >= 0xe000)
2943 {
2944 *puc = c;
2945 return 1;
2946 }
2947
2948 if (c < 0xdc00)
2949 {
2950 if (n >= 2)
2951 {
2952 if (s[1] >= 0xdc00 && s[1] < 0xe000)
2953 {
2954 *puc = 0x10000 + ((c - 0xd800) << 10) + (s[1] - 0xdc00);
2955 return 2;
2956 }
2957 }
2958 else
2959 {
2960 /* Incomplete multibyte character. */
2961 *puc = 0xfffd;
2962 return n;
2963 }
2964 }
2965
2966 /* Invalid multibyte character. */
2967 *puc = 0xfffd;
2968 return 1;
2969 }
2970 #endif /* HAVE_WCHAR_H */
2971
2972 /* Perform a comparison of two entries. */
2973 static signed int
2974 rsrc_cmp (bfd_boolean is_name, rsrc_entry * a, rsrc_entry * b)
2975 {
2976 if (! is_name)
2977 return a->name_id.id - b->name_id.id;
2978
2979 /* We have to perform a case insenstive, unicode string comparison... */
2980 int res;
2981
2982 #ifdef __CYGWIN__
2983 /* Under Cygwin unicode == UTF-16 == wchar_t.
2984 FIXME: The same is true for MingGW - we should test for that too. */
2985 res = wcsncasecmp ((const wchar_t *) astring + 2, (const wchar_t *) bstring + 2, min (alen, blen));
2986 #elif defined HAVE_WCHAR_H
2987 unsigned int i;
2988 bfd_byte * astring = a->name_id.name.string;
2989 unsigned int alen = a->name_id.name.len;
2990 bfd_byte * bstring = b->name_id.name.string;
2991 unsigned int blen = b->name_id.name.len;
2992
2993 res = 0;
2994 for (i = min (alen, blen); i--; astring += 2, bstring += 2)
2995 {
2996 wchar_t awc;
2997 wchar_t bwc;
2998
2999 /* Convert UTF-16 unicode characters into wchar_t characters so
3000 that we can then perform a case insensitive comparison. */
3001 int Alen = u16_mbtouc (& awc, (const unsigned short *) astring, 2);
3002 int Blen = u16_mbtouc (& bwc, (const unsigned short *) bstring, 2);
3003
3004 if (Alen != Blen)
3005 return Alen - Blen;
3006 res = wcsncasecmp (& awc, & bwc, 1);
3007 if (res)
3008 break;
3009 }
3010 #else
3011 res = memcmp (astring + 2, bstring + 2, min (alen, blen) * 2);
3012 #endif
3013
3014 if (res == 0)
3015 res = alen - blen;
3016
3017 return res;
3018 }
3019
3020 static void
3021 rsrc_print_name (char * buffer, rsrc_string string)
3022 {
3023 unsigned int i;
3024 bfd_byte * name = string.string;
3025
3026 for (i = string.len; i--; name += 2)
3027 sprintf (buffer + strlen (buffer), "%.1s", name);
3028 }
3029
3030 static const char *
3031 rsrc_resource_name (rsrc_entry * entry, rsrc_directory * dir)
3032 {
3033 static char buffer [256];
3034 bfd_boolean is_string = FALSE;
3035
3036 buffer[0] = 0;
3037
3038 if (dir != NULL && dir->entry != NULL && dir->entry->parent != NULL && dir->entry->parent->entry != NULL)
3039 {
3040 strcpy (buffer, "type: ");
3041 if (dir->entry->parent->entry->is_name)
3042 rsrc_print_name (buffer + strlen (buffer), dir->entry->parent->entry->name_id.name);
3043 else
3044 {
3045 unsigned int id = dir->entry->parent->entry->name_id.id;
3046
3047 sprintf (buffer + strlen (buffer), "%x", id);
3048 switch (id)
3049 {
3050 case 1: strcat (buffer, " (CURSOR)"); break;
3051 case 2: strcat (buffer, " (BITMAP)"); break;
3052 case 3: strcat (buffer, " (ICON)"); break;
3053 case 4: strcat (buffer, " (MENU)"); break;
3054 case 5: strcat (buffer, " (DIALOG)"); break;
3055 case 6: strcat (buffer, " (STRING)"); is_string = TRUE; break;
3056 case 7: strcat (buffer, " (FONTDIR)"); break;
3057 case 8: strcat (buffer, " (FONT)"); break;
3058 case 9: strcat (buffer, " (ACCELERATOR)"); break;
3059 case 10: strcat (buffer, " (RCDATA)"); break;
3060 case 11: strcat (buffer, " (MESSAGETABLE)"); break;
3061 case 12: strcat (buffer, " (GROUP_CURSOR)"); break;
3062 case 14: strcat (buffer, " (GROUP_ICON)"); break;
3063 case 16: strcat (buffer, " (VERSION)"); break;
3064 case 17: strcat (buffer, " (DLGINCLUDE)"); break;
3065 case 19: strcat (buffer, " (PLUGPLAY)"); break;
3066 case 20: strcat (buffer, " (VXD)"); break;
3067 case 21: strcat (buffer, " (ANICURSOR)"); break;
3068 case 22: strcat (buffer, " (ANIICON)"); break;
3069 case 23: strcat (buffer, " (HTML)"); break;
3070 case 24: strcat (buffer, " (MANIFEST)"); break;
3071 case 240: strcat (buffer, " (DLGINIT)"); break;
3072 case 241: strcat (buffer, " (TOOLBAR)"); break;
3073 }
3074 }
3075 }
3076
3077 if (dir != NULL && dir->entry != NULL)
3078 {
3079 strcat (buffer, " name: ");
3080 if (dir->entry->is_name)
3081 rsrc_print_name (buffer + strlen (buffer), dir->entry->name_id.name);
3082 else
3083 {
3084 unsigned int id = dir->entry->name_id.id;
3085
3086 sprintf (buffer + strlen (buffer), "%x", id);
3087
3088 if (is_string)
3089 sprintf (buffer + strlen (buffer), " (resource id range: %d - %d)",
3090 (id - 1) << 4, (id << 4) - 1);
3091 }
3092 }
3093
3094 if (entry != NULL)
3095 {
3096 strcat (buffer, " lang: ");
3097
3098 if (entry->is_name)
3099 rsrc_print_name (buffer + strlen (buffer), entry->name_id.name);
3100 else
3101 sprintf (buffer + strlen (buffer), "%x", entry->name_id.id);
3102 }
3103
3104 return buffer;
3105 }
3106
3107 /* *sigh* Windows resource strings are special. Only the top 28-bits of
3108 their ID is stored in the NAME entry. The bottom four bits are used as
3109 an index into unicode string table that makes up the data of the leaf.
3110 So identical type-name-lang string resources may not actually be
3111 identical at all.
3112
3113 This function is called when we have detected two string resources with
3114 match top-28-bit IDs. We have to scan the string tables inside the leaves
3115 and discover if there are any real collisions. If there are then we report
3116 them and return FALSE. Otherwise we copy any strings from B into A and then
3117 return TRUE. */
3118
3119 static bfd_boolean
3120 rsrc_merge_string_entries (rsrc_entry * a ATTRIBUTE_UNUSED,
3121 rsrc_entry * b ATTRIBUTE_UNUSED)
3122 {
3123 unsigned int copy_needed = 0;
3124 unsigned int i;
3125
3126 /* Step one: Find out what we have to do. */
3127 BFD_ASSERT (! a->is_dir);
3128 bfd_byte * astring = a->value.leaf->data;
3129
3130 BFD_ASSERT (! b->is_dir);
3131 bfd_byte * bstring = b->value.leaf->data;
3132
3133 for (i = 0; i < 16; i++)
3134 {
3135 unsigned int alen = astring[0] + (astring[1] << 8);
3136 unsigned int blen = bstring[0] + (bstring[1] << 8);
3137
3138 if (alen == 0)
3139 {
3140 copy_needed += blen * 2;
3141 }
3142 else if (blen == 0)
3143 ;
3144 else if (alen != blen)
3145 /* FIXME: Should we continue the loop in order to report other duplicates ? */
3146 break;
3147 /* alen == blen != 0. We might have two identical strings. If so we
3148 can ignore the second one. There is no need for wchar_t vs UTF-16
3149 theatrics here - we are only interested in (case sensitive) equality. */
3150 else if (memcmp (astring + 2, bstring + 2, alen * 2) != 0)
3151 break;
3152
3153 astring += (alen + 1) * 2;
3154 bstring += (blen + 1) * 2;
3155 }
3156
3157 if (i != 16)
3158 {
3159 if (a->parent != NULL
3160 && a->parent->entry != NULL
3161 && a->parent->entry->is_name == FALSE)
3162 _bfd_error_handler (_(".rsrc merge failure: duplicate string resource: %d"),
3163 ((a->parent->entry->name_id.id - 1) << 4) + i);
3164 return FALSE;
3165 }
3166
3167 if (copy_needed == 0)
3168 return TRUE;
3169
3170 /* If we reach here then A and B must both have non-colliding strings.
3171 (We never get string resources with fully empty string tables).
3172 We need to allocate an extra COPY_NEEDED bytes in A and then bring
3173 in B's strings. */
3174 bfd_byte * new_data = bfd_malloc (a->value.leaf->size + copy_needed);
3175 if (new_data == NULL)
3176 return FALSE;
3177
3178 bfd_byte * nstring = new_data;
3179 astring = a->value.leaf->data;
3180 bstring = b->value.leaf->data;
3181
3182 for (i = 0; i < 16; i++)
3183 {
3184 unsigned int alen = astring[0] + (astring[1] << 8);
3185 unsigned int blen = bstring[0] + (bstring[1] << 8);
3186
3187 if (alen != 0)
3188 {
3189 memcpy (nstring, astring, (alen + 1) * 2);
3190 nstring += (alen + 1) * 2;
3191 }
3192 else if (blen != 0)
3193 {
3194 memcpy (nstring, bstring, (blen + 1) * 2);
3195 nstring += (blen + 1) * 2;
3196 }
3197 else
3198 {
3199 * nstring++ = 0;
3200 * nstring++ = 0;
3201 }
3202
3203 astring += (alen + 1) * 2;
3204 bstring += (blen + 1) * 2;
3205 }
3206
3207 BFD_ASSERT (nstring - new_data == (signed) (a->value.leaf->size + copy_needed));
3208
3209 free (a->value.leaf->data);
3210 a->value.leaf->data = new_data;
3211 a->value.leaf->size += copy_needed;
3212
3213 return TRUE;
3214 }
3215
3216 static void rsrc_merge (rsrc_entry *, rsrc_entry *);
3217
3218 /* Sort the entries in given part of the directory.
3219 We use an old fashioned bubble sort because we are dealing
3220 with lists and we want to handle matches specially. */
3221
3222 static void
3223 rsrc_sort_entries (rsrc_dir_chain * chain,
3224 bfd_boolean is_name,
3225 rsrc_directory * dir)
3226 {
3227 rsrc_entry * entry;
3228 rsrc_entry * next;
3229 rsrc_entry ** points_to_entry;
3230 bfd_boolean swapped;
3231
3232 if (chain->num_entries < 2)
3233 return;
3234
3235 do
3236 {
3237 swapped = FALSE;
3238 points_to_entry = & chain->first_entry;
3239 entry = * points_to_entry;
3240 next = entry->next_entry;
3241
3242 do
3243 {
3244 signed int cmp = rsrc_cmp (is_name, entry, next);
3245
3246 if (cmp > 0)
3247 {
3248 entry->next_entry = next->next_entry;
3249 next->next_entry = entry;
3250 * points_to_entry = next;
3251 points_to_entry = & next->next_entry;
3252 next = entry->next_entry;
3253 swapped = TRUE;
3254 }
3255 else if (cmp == 0)
3256 {
3257 if (entry->is_dir && next->is_dir)
3258 {
3259 /* When we encounter identical directory entries we have to
3260 merge them together. The exception to this rule is for
3261 resource manifests - there can only be one of these,
3262 even if they differ in language. Zero-language manifests
3263 are assumed to be default manifests (provided by the
3264 cygwin build system) and these can be silently dropped,
3265 unless that would reduce the number of manifests to zero.
3266 There should only ever be one non-zero lang manifest -
3267 if there are more it is an error. A non-zero lang
3268 manifest takes precedence over a default manifest. */
3269 if (entry->is_name == FALSE
3270 && entry->name_id.id == 1
3271 && dir != NULL
3272 && dir->entry != NULL
3273 && dir->entry->is_name == FALSE
3274 && dir->entry->name_id.id == 0x18)
3275 {
3276 if (next->value.directory->names.num_entries == 0
3277 && next->value.directory->ids.num_entries == 1
3278 && next->value.directory->ids.first_entry->is_name == FALSE
3279 && next->value.directory->ids.first_entry->name_id.id == 0)
3280 /* Fall through so that NEXT is dropped. */
3281 ;
3282 else if (entry->value.directory->names.num_entries == 0
3283 && entry->value.directory->ids.num_entries == 1
3284 && entry->value.directory->ids.first_entry->is_name == FALSE
3285 && entry->value.directory->ids.first_entry->name_id.id == 0)
3286 {
3287 /* Swap ENTRY and NEXT. Then fall through so that the old ENTRY is dropped. */
3288 entry->next_entry = next->next_entry;
3289 next->next_entry = entry;
3290 * points_to_entry = next;
3291 points_to_entry = & next->next_entry;
3292 next = entry->next_entry;
3293 swapped = TRUE;
3294 }
3295 else
3296 {
3297 _bfd_error_handler (_(".rsrc merge failure: multiple non-default manifests"));
3298 bfd_set_error (bfd_error_file_truncated);
3299 return;
3300 }
3301
3302 /* Unhook NEXT from the chain. */
3303 /* FIXME: memory loss here. */
3304 entry->next_entry = next->next_entry;
3305 chain->num_entries --;
3306 if (chain->num_entries < 2)
3307 return;
3308 next = next->next_entry;
3309 }
3310 else
3311 rsrc_merge (entry, next);
3312 }
3313 else if (entry->is_dir != next->is_dir)
3314 {
3315 _bfd_error_handler (_(".rsrc merge failure: a directory matches a leaf"));
3316 bfd_set_error (bfd_error_file_truncated);
3317 return;
3318 }
3319 else
3320 {
3321 /* Otherwise with identical leaves we issue an error
3322 message - because there should never be duplicates.
3323 The exception is Type 18/Name 1/Lang 0 which is the
3324 defaul manifest - this can just be dropped. */
3325 if (entry->is_name == FALSE
3326 && entry->name_id.id == 0
3327 && dir != NULL
3328 && dir->entry != NULL
3329 && dir->entry->is_name == FALSE
3330 && dir->entry->name_id.id == 1
3331 && dir->entry->parent != NULL
3332 && dir->entry->parent->entry != NULL
3333 && dir->entry->parent->entry->is_name == FALSE
3334 && dir->entry->parent->entry->name_id.id == 0x18 /* RT_MANIFEST */)
3335 ;
3336 else if (dir != NULL
3337 && dir->entry != NULL
3338 && dir->entry->parent != NULL
3339 && dir->entry->parent->entry != NULL
3340 && dir->entry->parent->entry->is_name == FALSE
3341 && dir->entry->parent->entry->name_id.id == 0x6 /* RT_STRING */)
3342 {
3343 /* Strings need special handling. */
3344 if (! rsrc_merge_string_entries (entry, next))
3345 {
3346 /* _bfd_error_handler should have been called inside merge_strings. */
3347 bfd_set_error (bfd_error_file_truncated);
3348 return;
3349 }
3350 }
3351 else
3352 {
3353 if (dir == NULL
3354 || dir->entry == NULL
3355 || dir->entry->parent == NULL
3356 || dir->entry->parent->entry == NULL)
3357 _bfd_error_handler (_(".rsrc merge failure: duplicate leaf"));
3358 else
3359 _bfd_error_handler (_(".rsrc merge failure: duplicate leaf: %s"),
3360 rsrc_resource_name (entry, dir));
3361 bfd_set_error (bfd_error_file_truncated);
3362 return;
3363 }
3364 }
3365
3366 /* Unhook NEXT from the chain. */
3367 entry->next_entry = next->next_entry;
3368 chain->num_entries --;
3369 if (chain->num_entries < 2)
3370 return;
3371 next = next->next_entry;
3372 }
3373 else
3374 {
3375 points_to_entry = & entry->next_entry;
3376 entry = next;
3377 next = next->next_entry;
3378 }
3379 }
3380 while (next);
3381
3382 chain->last_entry = entry;
3383 }
3384 while (swapped);
3385 }
3386
3387 /* Attach B's chain onto A. */
3388 static void
3389 rsrc_attach_chain (struct rsrc_dir_chain * achain, struct rsrc_dir_chain * bchain)
3390 {
3391 if (bchain->num_entries == 0)
3392 return;
3393
3394 achain->num_entries += bchain->num_entries;
3395
3396 if (achain->first_entry == NULL)
3397 {
3398 achain->first_entry = bchain->first_entry;
3399 achain->last_entry = bchain->last_entry;
3400 }
3401 else
3402 {
3403 achain->last_entry->next_entry = bchain->first_entry;
3404 achain->last_entry = bchain->last_entry;
3405 }
3406
3407 bchain->num_entries = 0;
3408 bchain->first_entry = bchain->last_entry = NULL;
3409 }
3410
3411 static void
3412 rsrc_merge (struct rsrc_entry * a, struct rsrc_entry * b)
3413 {
3414 BFD_ASSERT (a->is_dir);
3415 BFD_ASSERT (b->is_dir);
3416
3417 rsrc_directory * adir = a->value.directory;
3418 rsrc_directory * bdir = b->value.directory;
3419
3420 if (adir->characteristics != bdir->characteristics)
3421 {
3422 _bfd_error_handler (_(".rsrc merge failure: dirs with differing characteristics\n"));
3423 bfd_set_error (bfd_error_file_truncated);
3424 return;
3425 }
3426
3427 if (adir->major != bdir->major || adir->minor != bdir->minor)
3428 {
3429 _bfd_error_handler (_(".rsrc merge failure: differing directory versions\n"));
3430 bfd_set_error (bfd_error_file_truncated);
3431 return;
3432 }
3433
3434 /* Attach B's name chain to A. */
3435 rsrc_attach_chain (& adir->names, & bdir->names);
3436
3437 /* Attach B's ID chain to A. */
3438 rsrc_attach_chain (& adir->ids, & bdir->ids);
3439
3440 /* Now sort A's entries. */
3441 rsrc_sort_entries (& adir->names, TRUE, adir);
3442 rsrc_sort_entries (& adir->ids, FALSE, adir);
3443 }
3444
3445 /* Check the .rsrc section. If it contains multiple concatenated
3446 resources then we must merge them properly. Otherwise Windows
3447 will ignore all but the first set. */
3448
3449 static void
3450 rsrc_process_section (bfd * abfd,
3451 struct coff_final_link_info * pfinfo)
3452 {
3453 rsrc_directory new_table;
3454 bfd_size_type size;
3455 asection * sec;
3456
3457 new_table.names.num_entries = 0;
3458 new_table.ids.num_entries = 0;
3459
3460 sec = bfd_get_section_by_name (abfd, ".rsrc");
3461 if (sec == NULL || (size = sec->rawsize) == 0)
3462 return;
3463
3464 pe_data_type * pe = pe_data (abfd);
3465 if (pe == NULL)
3466 return;
3467
3468 bfd_vma rva_bias;
3469 rva_bias = sec->vma - pe->pe_opthdr.ImageBase;
3470
3471 bfd_byte * data = bfd_malloc (size);
3472 if (data == NULL)
3473 return;
3474
3475 bfd_byte * datastart = data;
3476
3477 if (! bfd_get_section_contents (abfd, sec, data, 0, size))
3478 goto end;
3479
3480 /* Step one: Walk the section, computing the size of the tables,
3481 leaves and data and decide if we need to do anything. */
3482 bfd_byte * dataend = data + size;
3483 unsigned int num_resource_sets = 0;
3484 sizeof_leaves = sizeof_strings = sizeof_tables_and_entries = 0;
3485
3486 while (data < dataend)
3487 {
3488 bfd_byte * p = data;
3489
3490 data = rsrc_count_directory (abfd, data, data, dataend, rva_bias);
3491 if (data > dataend)
3492 {
3493 /* Corrupted .rsrc section - cannot merge. */
3494 _bfd_error_handler (_("%s: .rsrc merge failure: corrupt .rsrc section"),
3495 bfd_get_filename (abfd));
3496 bfd_set_error (bfd_error_file_truncated);
3497 goto end;
3498 }
3499
3500 /* Align the data pointer - we assume 1^2 alignment. */
3501 data = (bfd_byte *) (((long) (data + 3)) & ~ 3);
3502 rva_bias += data - p;
3503
3504 if (data == (dataend - 4))
3505 data = dataend;
3506
3507 ++ num_resource_sets;
3508 }
3509
3510 if (num_resource_sets < 2)
3511 /* No merging necessary. */
3512 goto end;
3513
3514 /* Step two: Walk the data again, building trees of the resources. */
3515 data = datastart;
3516 rva_bias = sec->vma - pe->pe_opthdr.ImageBase;
3517
3518 rsrc_directory * type_tables = bfd_malloc (num_resource_sets * sizeof * type_tables);
3519 if (type_tables == NULL)
3520 goto end;
3521
3522 unsigned int index = 0;
3523 while (data < dataend)
3524 {
3525 bfd_byte * p = data;
3526
3527 data = rsrc_parse_directory (abfd, type_tables + index, data, data, dataend,
3528 rva_bias, NULL);
3529 data = (bfd_byte *) (((long) (data + 3)) & ~ 3);
3530 rva_bias += data - p;
3531 if (data == (dataend - 4))
3532 data = dataend;
3533 index ++;
3534 }
3535 BFD_ASSERT (index == num_resource_sets);
3536
3537 /* Step three: Merge the top level tables (there can be only one).
3538
3539 We must ensure that the merged entries are in ascending order.
3540
3541 We also thread the top level table entries from the old tree onto
3542 the new table, so that they can be pulled off later. */
3543
3544 /* FIXME: Should we verify that all type tables are the same ? */
3545 new_table.characteristics = type_tables[0].characteristics;
3546 new_table.time = type_tables[0].time;
3547 new_table.major = type_tables[0].major;
3548 new_table.minor = type_tables[0].minor;
3549
3550 /* Chain the NAME entries onto the table. */
3551 new_table.names.first_entry = NULL;
3552 new_table.names.last_entry = NULL;
3553
3554 for (index = 0; index < num_resource_sets; index++)
3555 rsrc_attach_chain (& new_table.names, & type_tables[index].names);
3556
3557 rsrc_sort_entries (& new_table.names, TRUE, & new_table);
3558
3559 /* Chain the ID entries onto the table. */
3560 new_table.ids.first_entry = NULL;
3561 new_table.ids.last_entry = NULL;
3562
3563 for (index = 0; index < num_resource_sets; index++)
3564 rsrc_attach_chain (& new_table.ids, & type_tables[index].ids);
3565
3566 rsrc_sort_entries (& new_table.ids, FALSE, & new_table);
3567
3568 /* Step four: Create new contents for the .rsrc section. */
3569 bfd_byte * new_data = bfd_malloc (size);
3570 if (new_data == NULL)
3571 goto end;
3572
3573 rsrc_write_data write_data;
3574
3575 write_data.abfd = abfd;
3576 write_data.datastart = new_data;
3577 write_data.next_table = new_data;
3578 write_data.next_leaf = new_data + sizeof_tables_and_entries;
3579 write_data.next_string = write_data.next_leaf + sizeof_leaves;
3580 write_data.next_data = write_data.next_string + sizeof_strings;
3581
3582 write_data.rva_bias = sec->vma - pe->pe_opthdr.ImageBase;
3583
3584 rsrc_write_directory (& write_data, & new_table);
3585
3586 /* Step five: Replace the old contents with the new.
3587 We recompute the size as we may have lost entries due to mergeing. */
3588 size = ((write_data.next_data - new_data) + 3) & ~3;
3589 bfd_set_section_contents (pfinfo->output_bfd, sec, new_data, 0, size);
3590 sec->size = sec->rawsize = size;
3591
3592 end:
3593 /* FIXME: Free the resource tree, if we have one. */
3594 free (datastart);
3595 }
3596
3597 /* Handle the .idata section and other things that need symbol table
3598 access. */
3599
3600 bfd_boolean
3601 _bfd_XXi_final_link_postscript (bfd * abfd, struct coff_final_link_info *pfinfo)
3602 {
3603 struct coff_link_hash_entry *h1;
3604 struct bfd_link_info *info = pfinfo->info;
3605 bfd_boolean result = TRUE;
3606
3607 /* There are a few fields that need to be filled in now while we
3608 have symbol table access.
3609
3610 The .idata subsections aren't directly available as sections, but
3611 they are in the symbol table, so get them from there. */
3612
3613 /* The import directory. This is the address of .idata$2, with size
3614 of .idata$2 + .idata$3. */
3615 h1 = coff_link_hash_lookup (coff_hash_table (info),
3616 ".idata$2", FALSE, FALSE, TRUE);
3617 if (h1 != NULL)
3618 {
3619 /* PR ld/2729: We cannot rely upon all the output sections having been
3620 created properly, so check before referencing them. Issue a warning
3621 message for any sections tht could not be found. */
3622 if ((h1->root.type == bfd_link_hash_defined
3623 || h1->root.type == bfd_link_hash_defweak)
3624 && h1->root.u.def.section != NULL
3625 && h1->root.u.def.section->output_section != NULL)
3626 pe_data (abfd)->pe_opthdr.DataDirectory[PE_IMPORT_TABLE].VirtualAddress =
3627 (h1->root.u.def.value
3628 + h1->root.u.def.section->output_section->vma
3629 + h1->root.u.def.section->output_offset);
3630 else
3631 {
3632 _bfd_error_handler
3633 (_("%B: unable to fill in DataDictionary[1] because .idata$2 is missing"),
3634 abfd);
3635 result = FALSE;
3636 }
3637
3638 h1 = coff_link_hash_lookup (coff_hash_table (info),
3639 ".idata$4", FALSE, FALSE, TRUE);
3640 if (h1 != NULL
3641 && (h1->root.type == bfd_link_hash_defined
3642 || h1->root.type == bfd_link_hash_defweak)
3643 && h1->root.u.def.section != NULL
3644 && h1->root.u.def.section->output_section != NULL)
3645 pe_data (abfd)->pe_opthdr.DataDirectory[PE_IMPORT_TABLE].Size =
3646 ((h1->root.u.def.value
3647 + h1->root.u.def.section->output_section->vma
3648 + h1->root.u.def.section->output_offset)
3649 - pe_data (abfd)->pe_opthdr.DataDirectory[PE_IMPORT_TABLE].VirtualAddress);
3650 else
3651 {
3652 _bfd_error_handler
3653 (_("%B: unable to fill in DataDictionary[1] because .idata$4 is missing"),
3654 abfd);
3655 result = FALSE;
3656 }
3657
3658 /* The import address table. This is the size/address of
3659 .idata$5. */
3660 h1 = coff_link_hash_lookup (coff_hash_table (info),
3661 ".idata$5", FALSE, FALSE, TRUE);
3662 if (h1 != NULL
3663 && (h1->root.type == bfd_link_hash_defined
3664 || h1->root.type == bfd_link_hash_defweak)
3665 && h1->root.u.def.section != NULL
3666 && h1->root.u.def.section->output_section != NULL)
3667 pe_data (abfd)->pe_opthdr.DataDirectory[PE_IMPORT_ADDRESS_TABLE].VirtualAddress =
3668 (h1->root.u.def.value
3669 + h1->root.u.def.section->output_section->vma
3670 + h1->root.u.def.section->output_offset);
3671 else
3672 {
3673 _bfd_error_handler
3674 (_("%B: unable to fill in DataDictionary[12] because .idata$5 is missing"),
3675 abfd);
3676 result = FALSE;
3677 }
3678
3679 h1 = coff_link_hash_lookup (coff_hash_table (info),
3680 ".idata$6", FALSE, FALSE, TRUE);
3681 if (h1 != NULL
3682 && (h1->root.type == bfd_link_hash_defined
3683 || h1->root.type == bfd_link_hash_defweak)
3684 && h1->root.u.def.section != NULL
3685 && h1->root.u.def.section->output_section != NULL)
3686 pe_data (abfd)->pe_opthdr.DataDirectory[PE_IMPORT_ADDRESS_TABLE].Size =
3687 ((h1->root.u.def.value
3688 + h1->root.u.def.section->output_section->vma
3689 + h1->root.u.def.section->output_offset)
3690 - pe_data (abfd)->pe_opthdr.DataDirectory[PE_IMPORT_ADDRESS_TABLE].VirtualAddress);
3691 else
3692 {
3693 _bfd_error_handler
3694 (_("%B: unable to fill in DataDictionary[PE_IMPORT_ADDRESS_TABLE (12)] because .idata$6 is missing"),
3695 abfd);
3696 result = FALSE;
3697 }
3698 }
3699 else
3700 {
3701 h1 = coff_link_hash_lookup (coff_hash_table (info),
3702 "__IAT_start__", FALSE, FALSE, TRUE);
3703 if (h1 != NULL
3704 && (h1->root.type == bfd_link_hash_defined
3705 || h1->root.type == bfd_link_hash_defweak)
3706 && h1->root.u.def.section != NULL
3707 && h1->root.u.def.section->output_section != NULL)
3708 {
3709 bfd_vma iat_va;
3710
3711 iat_va =
3712 (h1->root.u.def.value
3713 + h1->root.u.def.section->output_section->vma
3714 + h1->root.u.def.section->output_offset);
3715
3716 h1 = coff_link_hash_lookup (coff_hash_table (info),
3717 "__IAT_end__", FALSE, FALSE, TRUE);
3718 if (h1 != NULL
3719 && (h1->root.type == bfd_link_hash_defined
3720 || h1->root.type == bfd_link_hash_defweak)
3721 && h1->root.u.def.section != NULL
3722 && h1->root.u.def.section->output_section != NULL)
3723 {
3724 pe_data (abfd)->pe_opthdr.DataDirectory[PE_IMPORT_ADDRESS_TABLE].Size =
3725 ((h1->root.u.def.value
3726 + h1->root.u.def.section->output_section->vma
3727 + h1->root.u.def.section->output_offset)
3728 - iat_va);
3729 if (pe_data (abfd)->pe_opthdr.DataDirectory[PE_IMPORT_ADDRESS_TABLE].Size != 0)
3730 pe_data (abfd)->pe_opthdr.DataDirectory[PE_IMPORT_ADDRESS_TABLE].VirtualAddress =
3731 iat_va - pe_data (abfd)->pe_opthdr.ImageBase;
3732 }
3733 else
3734 {
3735 _bfd_error_handler
3736 (_("%B: unable to fill in DataDictionary[PE_IMPORT_ADDRESS_TABLE(12)]"
3737 " because .idata$6 is missing"), abfd);
3738 result = FALSE;
3739 }
3740 }
3741 }
3742
3743 h1 = coff_link_hash_lookup (coff_hash_table (info),
3744 (bfd_get_symbol_leading_char(abfd) != 0
3745 ? "__tls_used" : "_tls_used"),
3746 FALSE, FALSE, TRUE);
3747 if (h1 != NULL)
3748 {
3749 if ((h1->root.type == bfd_link_hash_defined
3750 || h1->root.type == bfd_link_hash_defweak)
3751 && h1->root.u.def.section != NULL
3752 && h1->root.u.def.section->output_section != NULL)
3753 pe_data (abfd)->pe_opthdr.DataDirectory[PE_TLS_TABLE].VirtualAddress =
3754 (h1->root.u.def.value
3755 + h1->root.u.def.section->output_section->vma
3756 + h1->root.u.def.section->output_offset
3757 - pe_data (abfd)->pe_opthdr.ImageBase);
3758 else
3759 {
3760 _bfd_error_handler
3761 (_("%B: unable to fill in DataDictionary[9] because __tls_used is missing"),
3762 abfd);
3763 result = FALSE;
3764 }
3765 /* According to PECOFF sepcifications by Microsoft version 8.2
3766 the TLS data directory consists of 4 pointers, followed
3767 by two 4-byte integer. This implies that the total size
3768 is different for 32-bit and 64-bit executables. */
3769 #if !defined(COFF_WITH_pep) && !defined(COFF_WITH_pex64)
3770 pe_data (abfd)->pe_opthdr.DataDirectory[PE_TLS_TABLE].Size = 0x18;
3771 #else
3772 pe_data (abfd)->pe_opthdr.DataDirectory[PE_TLS_TABLE].Size = 0x28;
3773 #endif
3774 }
3775
3776 /* If there is a .pdata section and we have linked pdata finally, we
3777 need to sort the entries ascending. */
3778 #if !defined(COFF_WITH_pep) && defined(COFF_WITH_pex64)
3779 {
3780 asection *sec = bfd_get_section_by_name (abfd, ".pdata");
3781
3782 if (sec)
3783 {
3784 bfd_size_type x = sec->rawsize;
3785 bfd_byte *tmp_data = NULL;
3786
3787 if (x)
3788 tmp_data = bfd_malloc (x);
3789
3790 if (tmp_data != NULL)
3791 {
3792 if (bfd_get_section_contents (abfd, sec, tmp_data, 0, x))
3793 {
3794 qsort (tmp_data,
3795 (size_t) (x / 12),
3796 12, sort_x64_pdata);
3797 bfd_set_section_contents (pfinfo->output_bfd, sec,
3798 tmp_data, 0, x);
3799 }
3800 free (tmp_data);
3801 }
3802 }
3803 }
3804 #endif
3805
3806 rsrc_process_section (abfd, pfinfo);
3807
3808 /* If we couldn't find idata$2, we either have an excessively
3809 trivial program or are in DEEP trouble; we have to assume trivial
3810 program.... */
3811 return result;
3812 }
This page took 0.161238 seconds and 4 git commands to generate.