]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - bfd/peigen.c
* configure.in (i386-*-msdosdjgpp): Set bfd_gas to yes.
[thirdparty/binutils-gdb.git] / bfd / peigen.c
CommitLineData
277d1b5e 1/* Support for the generic parts of PE/PEI; the common executable parts.
a76b448c 2 Copyright 1995, 96, 97, 98, 99, 2000 Free Software Foundation, Inc.
277d1b5e
ILT
3 Written by Cygnus Solutions.
4
5This file is part of BFD, the Binary File Descriptor library.
6
7This program is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 2 of the License, or
10(at your option) any later version.
11
12This program is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with this program; if not, write to the Free Software
19Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
20
21/*
22Most of this hacked by Steve Chamberlain,
23 sac@cygnus.com
24
25PE/PEI rearrangement (and code added): Donn Terry
26 Softway Systems, Inc.
27*/
28
29/* Hey look, some documentation [and in a place you expect to find it]!
30
31 The main reference for the pei format is "Microsoft Portable Executable
32 and Common Object File Format Specification 4.1". Get it if you need to
33 do some serious hacking on this code.
34
35 Another reference:
36 "Peering Inside the PE: A Tour of the Win32 Portable Executable
37 File Format", MSJ 1994, Volume 9.
38
39 The *sole* difference between the pe format and the pei format is that the
40 latter has an MSDOS 2.0 .exe header on the front that prints the message
41 "This app must be run under Windows." (or some such).
42 (FIXME: Whether that statement is *really* true or not is unknown.
43 Are there more subtle differences between pe and pei formats?
44 For now assume there aren't. If you find one, then for God sakes
45 document it here!)
46
47 The Microsoft docs use the word "image" instead of "executable" because
48 the former can also refer to a DLL (shared library). Confusion can arise
49 because the `i' in `pei' also refers to "image". The `pe' format can
50 also create images (i.e. executables), it's just that to run on a win32
51 system you need to use the pei format.
52
53 FIXME: Please add more docs here so the next poor fool that has to hack
54 on this code has a chance of getting something accomplished without
55 wasting too much time.
56*/
57
58#include "bfd.h"
59#include "sysdep.h"
60#include "libbfd.h"
61#include "coff/internal.h"
62
63/* NOTE: it's strange to be including an architecture specific header
64 in what's supposed to be general (to PE/PEI) code. However, that's
65 where the definitions are, and they don't vary per architecture
66 within PE/PEI, so we get them from there. FIXME: The lack of
67 variance is an assumption which may prove to be incorrect if new
68 PE/PEI targets are created. */
69#include "coff/i386.h"
70
71#include "coff/pe.h"
72#include "libcoff.h"
73#include "libpei.h"
74
75/* FIXME: This file has various tests of POWERPC_LE_PE. Those tests
76 worked when the code was in peicode.h, but no longer work now that
77 the code is in peigen.c. PowerPC NT is said to be dead. If
78 anybody wants to revive the code, you will have to figure out how
79 to handle those issues. */
80
5933bdc9
ILT
81static void add_data_entry
82 PARAMS ((bfd *, struct internal_extra_pe_aouthdr *, int, char *, bfd_vma));
83static boolean pe_print_pdata PARAMS ((bfd *, PTR));
84static boolean pe_print_reloc PARAMS ((bfd *, PTR));
277d1b5e
ILT
85
86/**********************************************************************/
87
88void
89_bfd_pei_swap_sym_in (abfd, ext1, in1)
90 bfd *abfd;
91 PTR ext1;
92 PTR in1;
93{
94 SYMENT *ext = (SYMENT *)ext1;
95 struct internal_syment *in = (struct internal_syment *)in1;
96
97 if( ext->e.e_name[0] == 0) {
98 in->_n._n_n._n_zeroes = 0;
99 in->_n._n_n._n_offset = bfd_h_get_32(abfd, (bfd_byte *) ext->e.e.e_offset);
100 }
101 else {
102 memcpy(in->_n._n_name, ext->e.e_name, SYMNMLEN);
103 }
104
105 in->n_value = bfd_h_get_32(abfd, (bfd_byte *) ext->e_value);
106 in->n_scnum = bfd_h_get_16(abfd, (bfd_byte *) ext->e_scnum);
107 if (sizeof(ext->e_type) == 2){
108 in->n_type = bfd_h_get_16(abfd, (bfd_byte *) ext->e_type);
109 }
110 else {
111 in->n_type = bfd_h_get_32(abfd, (bfd_byte *) ext->e_type);
112 }
113 in->n_sclass = bfd_h_get_8(abfd, ext->e_sclass);
114 in->n_numaux = bfd_h_get_8(abfd, ext->e_numaux);
115
116#ifndef STRICT_PE_FORMAT
117 /* This is for Gnu-created DLLs */
118
119 /* The section symbols for the .idata$ sections have class 0x68
120 (C_SECTION), which MS documentation indicates is a section
121 symbol. Unfortunately, the value field in the symbol is simply a
122 copy of the .idata section's flags rather than something useful.
123 When these symbols are encountered, change the value to 0 so that
124 they will be handled somewhat correctly in the bfd code. */
125 if (in->n_sclass == C_SECTION)
126 {
127 in->n_value = 0x0;
128
129#if 0
130 /* FIXME: This is clearly wrong. The problem seems to be that
131 undefined C_SECTION symbols appear in the first object of a
132 MS generated .lib file, and the symbols are not defined
133 anywhere. */
134 in->n_scnum = 1;
135
136 /* I have tried setting the class to 3 and using the following
137 to set the section number. This will put the address of the
138 pointer to the string kernel32.dll at addresses 0 and 0x10
139 off start of idata section which is not correct */
140 /* if (strcmp (in->_n._n_name, ".idata$4") == 0) */
141 /* in->n_scnum = 3; */
142 /* else */
143 /* in->n_scnum = 2; */
144#else
145 /* Create synthetic empty sections as needed. DJ */
146 if (in->n_scnum == 0)
147 {
148 asection *sec;
149 for (sec=abfd->sections; sec; sec=sec->next)
150 {
151 if (strcmp (sec->name, in->n_name) == 0)
152 {
153 in->n_scnum = sec->target_index;
154 break;
155 }
156 }
157 }
158 if (in->n_scnum == 0)
159 {
160 int unused_section_number = 0;
161 asection *sec;
162 char *name;
163 for (sec=abfd->sections; sec; sec=sec->next)
164 if (unused_section_number <= sec->target_index)
165 unused_section_number = sec->target_index+1;
166
167 name = bfd_alloc (abfd, strlen (in->n_name) + 10);
168 if (name == NULL)
169 return;
170 strcpy (name, in->n_name);
171 sec = bfd_make_section_anyway (abfd, name);
172
173 sec->vma = 0;
174 sec->lma = 0;
175 sec->_cooked_size = 0;
176 sec->_raw_size = 0;
177 sec->filepos = 0;
178 sec->rel_filepos = 0;
179 sec->reloc_count = 0;
180 sec->line_filepos = 0;
181 sec->lineno_count = 0;
182 sec->userdata = NULL;
183 sec->next = (asection *) NULL;
184 sec->flags = 0;
185 sec->alignment_power = 2;
186 sec->flags = SEC_HAS_CONTENTS | SEC_ALLOC | SEC_DATA | SEC_LOAD;
187
188 sec->target_index = unused_section_number;
189
190 in->n_scnum = unused_section_number;
191 }
192 in->n_sclass = C_STAT;
193#endif
194 }
195#endif
196
197#ifdef coff_swap_sym_in_hook
198 /* This won't work in peigen.c, but since it's for PPC PE, it's not
199 worth fixing. */
200 coff_swap_sym_in_hook(abfd, ext1, in1);
201#endif
202}
203
204unsigned int
205_bfd_pei_swap_sym_out (abfd, inp, extp)
206 bfd *abfd;
207 PTR inp;
208 PTR extp;
209{
210 struct internal_syment *in = (struct internal_syment *)inp;
211 SYMENT *ext =(SYMENT *)extp;
212 if(in->_n._n_name[0] == 0) {
213 bfd_h_put_32(abfd, 0, (bfd_byte *) ext->e.e.e_zeroes);
214 bfd_h_put_32(abfd, in->_n._n_n._n_offset, (bfd_byte *) ext->e.e.e_offset);
215 }
216 else {
217 memcpy(ext->e.e_name, in->_n._n_name, SYMNMLEN);
218 }
219
220 bfd_h_put_32(abfd, in->n_value , (bfd_byte *) ext->e_value);
221 bfd_h_put_16(abfd, in->n_scnum , (bfd_byte *) ext->e_scnum);
222 if (sizeof(ext->e_type) == 2)
223 {
224 bfd_h_put_16(abfd, in->n_type , (bfd_byte *) ext->e_type);
225 }
226 else
227 {
228 bfd_h_put_32(abfd, in->n_type , (bfd_byte *) ext->e_type);
229 }
230 bfd_h_put_8(abfd, in->n_sclass , ext->e_sclass);
231 bfd_h_put_8(abfd, in->n_numaux , ext->e_numaux);
232
233 return SYMESZ;
234}
235
236void
237_bfd_pei_swap_aux_in (abfd, ext1, type, class, indx, numaux, in1)
238 bfd *abfd;
239 PTR ext1;
240 int type;
241 int class;
242 int indx ATTRIBUTE_UNUSED;
243 int numaux ATTRIBUTE_UNUSED;
244 PTR in1;
245{
246 AUXENT *ext = (AUXENT *)ext1;
247 union internal_auxent *in = (union internal_auxent *)in1;
248
249 switch (class) {
250 case C_FILE:
251 if (ext->x_file.x_fname[0] == 0) {
252 in->x_file.x_n.x_zeroes = 0;
253 in->x_file.x_n.x_offset =
254 bfd_h_get_32(abfd, (bfd_byte *) ext->x_file.x_n.x_offset);
255 } else {
256 memcpy (in->x_file.x_fname, ext->x_file.x_fname, FILNMLEN);
257 }
258 return;
259
260
261 case C_STAT:
262 case C_LEAFSTAT:
263 case C_HIDDEN:
264 if (type == T_NULL) {
265 in->x_scn.x_scnlen = GET_SCN_SCNLEN(abfd, ext);
266 in->x_scn.x_nreloc = GET_SCN_NRELOC(abfd, ext);
267 in->x_scn.x_nlinno = GET_SCN_NLINNO(abfd, ext);
268 in->x_scn.x_checksum = bfd_h_get_32 (abfd,
269 (bfd_byte *) ext->x_scn.x_checksum);
270 in->x_scn.x_associated =
271 bfd_h_get_16 (abfd, (bfd_byte *) ext->x_scn.x_associated);
272 in->x_scn.x_comdat = bfd_h_get_8 (abfd,
273 (bfd_byte *) ext->x_scn.x_comdat);
274 return;
275 }
276 break;
277 }
278
279 in->x_sym.x_tagndx.l = bfd_h_get_32(abfd, (bfd_byte *) ext->x_sym.x_tagndx);
280 in->x_sym.x_tvndx = bfd_h_get_16(abfd, (bfd_byte *) ext->x_sym.x_tvndx);
281
282 if (class == C_BLOCK || class == C_FCN || ISFCN (type) || ISTAG (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 bfd_h_get_16 (abfd, (bfd_byte *) ext->x_sym.x_fcnary.x_ary.x_dimen[0]);
291 in->x_sym.x_fcnary.x_ary.x_dimen[1] =
292 bfd_h_get_16 (abfd, (bfd_byte *) ext->x_sym.x_fcnary.x_ary.x_dimen[1]);
293 in->x_sym.x_fcnary.x_ary.x_dimen[2] =
294 bfd_h_get_16 (abfd, (bfd_byte *) ext->x_sym.x_fcnary.x_ary.x_dimen[2]);
295 in->x_sym.x_fcnary.x_ary.x_dimen[3] =
296 bfd_h_get_16 (abfd, (bfd_byte *) ext->x_sym.x_fcnary.x_ary.x_dimen[3]);
297 }
298
299 if (ISFCN(type)) {
300 in->x_sym.x_misc.x_fsize = bfd_h_get_32(abfd, (bfd_byte *) ext->x_sym.x_misc.x_fsize);
301 }
302 else {
303 in->x_sym.x_misc.x_lnsz.x_lnno = GET_LNSZ_LNNO(abfd, ext);
304 in->x_sym.x_misc.x_lnsz.x_size = GET_LNSZ_SIZE(abfd, ext);
305 }
306}
307
308unsigned int
309_bfd_pei_swap_aux_out (abfd, inp, type, class, indx, numaux, extp)
310 bfd *abfd;
311 PTR inp;
312 int type;
313 int class;
314 int indx ATTRIBUTE_UNUSED;
315 int numaux ATTRIBUTE_UNUSED;
316 PTR extp;
317{
318 union internal_auxent *in = (union internal_auxent *)inp;
319 AUXENT *ext = (AUXENT *)extp;
320
321 memset((PTR)ext, 0, AUXESZ);
322 switch (class) {
323 case C_FILE:
324 if (in->x_file.x_fname[0] == 0) {
325 bfd_h_put_32(abfd, 0, (bfd_byte *) ext->x_file.x_n.x_zeroes);
326 bfd_h_put_32(abfd,
327 in->x_file.x_n.x_offset,
328 (bfd_byte *) ext->x_file.x_n.x_offset);
329 }
330 else {
331 memcpy (ext->x_file.x_fname, in->x_file.x_fname, FILNMLEN);
332 }
333 return AUXESZ;
334
335
336 case C_STAT:
337 case C_LEAFSTAT:
338 case C_HIDDEN:
339 if (type == T_NULL) {
340 PUT_SCN_SCNLEN(abfd, in->x_scn.x_scnlen, ext);
341 PUT_SCN_NRELOC(abfd, in->x_scn.x_nreloc, ext);
342 PUT_SCN_NLINNO(abfd, in->x_scn.x_nlinno, ext);
343 bfd_h_put_32 (abfd, in->x_scn.x_checksum,
344 (bfd_byte *) ext->x_scn.x_checksum);
345 bfd_h_put_16 (abfd, in->x_scn.x_associated,
346 (bfd_byte *) ext->x_scn.x_associated);
347 bfd_h_put_8 (abfd, in->x_scn.x_comdat,
348 (bfd_byte *) ext->x_scn.x_comdat);
349 return AUXESZ;
350 }
351 break;
352 }
353
354 bfd_h_put_32(abfd, in->x_sym.x_tagndx.l, (bfd_byte *) ext->x_sym.x_tagndx);
355 bfd_h_put_16(abfd, in->x_sym.x_tvndx , (bfd_byte *) ext->x_sym.x_tvndx);
356
357 if (class == C_BLOCK || class == C_FCN || ISFCN (type) || ISTAG (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 bfd_h_put_16 (abfd, in->x_sym.x_fcnary.x_ary.x_dimen[0],
365 (bfd_byte *) ext->x_sym.x_fcnary.x_ary.x_dimen[0]);
366 bfd_h_put_16 (abfd, in->x_sym.x_fcnary.x_ary.x_dimen[1],
367 (bfd_byte *) ext->x_sym.x_fcnary.x_ary.x_dimen[1]);
368 bfd_h_put_16 (abfd, in->x_sym.x_fcnary.x_ary.x_dimen[2],
369 (bfd_byte *) ext->x_sym.x_fcnary.x_ary.x_dimen[2]);
370 bfd_h_put_16 (abfd, in->x_sym.x_fcnary.x_ary.x_dimen[3],
371 (bfd_byte *) ext->x_sym.x_fcnary.x_ary.x_dimen[3]);
372 }
373
374 if (ISFCN (type))
375 bfd_h_put_32 (abfd, in->x_sym.x_misc.x_fsize,
376 (bfd_byte *) ext->x_sym.x_misc.x_fsize);
377 else
378 {
379 PUT_LNSZ_LNNO (abfd, in->x_sym.x_misc.x_lnsz.x_lnno, ext);
380 PUT_LNSZ_SIZE (abfd, in->x_sym.x_misc.x_lnsz.x_size, ext);
381 }
382
383 return AUXESZ;
384}
385
386void
387_bfd_pei_swap_lineno_in (abfd, ext1, in1)
388 bfd *abfd;
389 PTR ext1;
390 PTR in1;
391{
392 LINENO *ext = (LINENO *)ext1;
393 struct internal_lineno *in = (struct internal_lineno *)in1;
394
395 in->l_addr.l_symndx = bfd_h_get_32(abfd, (bfd_byte *) ext->l_addr.l_symndx);
396 in->l_lnno = GET_LINENO_LNNO(abfd, ext);
397}
398
399unsigned int
400_bfd_pei_swap_lineno_out (abfd, inp, outp)
401 bfd *abfd;
402 PTR inp;
403 PTR outp;
404{
405 struct internal_lineno *in = (struct internal_lineno *)inp;
406 struct external_lineno *ext = (struct external_lineno *)outp;
407 bfd_h_put_32(abfd, in->l_addr.l_symndx, (bfd_byte *)
408 ext->l_addr.l_symndx);
409
410 PUT_LINENO_LNNO (abfd, in->l_lnno, ext);
411 return LINESZ;
412}
413
414void
415_bfd_pei_swap_aouthdr_in (abfd, aouthdr_ext1, aouthdr_int1)
416 bfd *abfd;
417 PTR aouthdr_ext1;
418 PTR aouthdr_int1;
419{
420 struct internal_extra_pe_aouthdr *a;
421 PEAOUTHDR *src = (PEAOUTHDR *)(aouthdr_ext1);
422 AOUTHDR *aouthdr_ext = (AOUTHDR *) aouthdr_ext1;
423 struct internal_aouthdr *aouthdr_int = (struct internal_aouthdr *)aouthdr_int1;
424
425 aouthdr_int->magic = bfd_h_get_16(abfd, (bfd_byte *) aouthdr_ext->magic);
426 aouthdr_int->vstamp = bfd_h_get_16(abfd, (bfd_byte *) aouthdr_ext->vstamp);
427 aouthdr_int->tsize =
428 GET_AOUTHDR_TSIZE (abfd, (bfd_byte *) aouthdr_ext->tsize);
429 aouthdr_int->dsize =
430 GET_AOUTHDR_DSIZE (abfd, (bfd_byte *) aouthdr_ext->dsize);
431 aouthdr_int->bsize =
432 GET_AOUTHDR_BSIZE (abfd, (bfd_byte *) aouthdr_ext->bsize);
433 aouthdr_int->entry =
434 GET_AOUTHDR_ENTRY (abfd, (bfd_byte *) aouthdr_ext->entry);
435 aouthdr_int->text_start =
436 GET_AOUTHDR_TEXT_START (abfd, (bfd_byte *) aouthdr_ext->text_start);
fac41780
JW
437#ifndef COFF_WITH_PEP64
438 /* PE32+ does not have data_start member! */
277d1b5e
ILT
439 aouthdr_int->data_start =
440 GET_AOUTHDR_DATA_START (abfd, (bfd_byte *) aouthdr_ext->data_start);
fac41780 441#endif
277d1b5e
ILT
442
443 a = &aouthdr_int->pe;
fac41780 444 a->ImageBase = GET_OPTHDR_IMAGE_BASE (abfd, (bfd_byte *)src->ImageBase);
277d1b5e
ILT
445 a->SectionAlignment = bfd_h_get_32 (abfd, (bfd_byte *)src->SectionAlignment);
446 a->FileAlignment = bfd_h_get_32 (abfd, (bfd_byte *)src->FileAlignment);
447 a->MajorOperatingSystemVersion =
448 bfd_h_get_16 (abfd, (bfd_byte *)src->MajorOperatingSystemVersion);
449 a->MinorOperatingSystemVersion =
450 bfd_h_get_16 (abfd, (bfd_byte *)src->MinorOperatingSystemVersion);
451 a->MajorImageVersion = bfd_h_get_16 (abfd, (bfd_byte *)src->MajorImageVersion);
452 a->MinorImageVersion = bfd_h_get_16 (abfd, (bfd_byte *)src->MinorImageVersion);
453 a->MajorSubsystemVersion = bfd_h_get_16 (abfd, (bfd_byte *)src->MajorSubsystemVersion);
454 a->MinorSubsystemVersion = bfd_h_get_16 (abfd, (bfd_byte *)src->MinorSubsystemVersion);
455 a->Reserved1 = bfd_h_get_32 (abfd, (bfd_byte *)src->Reserved1);
456 a->SizeOfImage = bfd_h_get_32 (abfd, (bfd_byte *)src->SizeOfImage);
457 a->SizeOfHeaders = bfd_h_get_32 (abfd, (bfd_byte *)src->SizeOfHeaders);
458 a->CheckSum = bfd_h_get_32 (abfd, (bfd_byte *)src->CheckSum);
459 a->Subsystem = bfd_h_get_16 (abfd, (bfd_byte *)src->Subsystem);
460 a->DllCharacteristics = bfd_h_get_16 (abfd, (bfd_byte *)src->DllCharacteristics);
fac41780
JW
461 a->SizeOfStackReserve = GET_OPTHDR_SIZE_OF_STACK_RESERVE (abfd, (bfd_byte *)src->SizeOfStackReserve);
462 a->SizeOfStackCommit = GET_OPTHDR_SIZE_OF_STACK_COMMIT (abfd, (bfd_byte *)src->SizeOfStackCommit);
463 a->SizeOfHeapReserve = GET_OPTHDR_SIZE_OF_HEAP_RESERVE (abfd, (bfd_byte *)src->SizeOfHeapReserve);
464 a->SizeOfHeapCommit = GET_OPTHDR_SIZE_OF_HEAP_COMMIT (abfd, (bfd_byte *)src->SizeOfHeapCommit);
277d1b5e
ILT
465 a->LoaderFlags = bfd_h_get_32 (abfd, (bfd_byte *)src->LoaderFlags);
466 a->NumberOfRvaAndSizes = bfd_h_get_32 (abfd, (bfd_byte *)src->NumberOfRvaAndSizes);
467
468 {
469 int idx;
470 for (idx=0; idx < 16; idx++)
471 {
3028b4c0
DD
472 /* If data directory is empty, rva also should be 0 */
473 int size = bfd_h_get_32 (abfd, (bfd_byte *) src->DataDirectory[idx][1]);
474 a->DataDirectory[idx].Size = size;
475
476 if (size)
477 {
478 a->DataDirectory[idx].VirtualAddress =
479 bfd_h_get_32 (abfd, (bfd_byte *) src->DataDirectory[idx][0]);
480 }
481 else
482 a->DataDirectory[idx].VirtualAddress = 0;
277d1b5e
ILT
483 }
484 }
485
486 if (aouthdr_int->entry)
487 {
488 aouthdr_int->entry += a->ImageBase;
fac41780 489#ifndef COFF_WITH_PEP64
277d1b5e 490 aouthdr_int->entry &= 0xffffffff;
fac41780 491#endif
277d1b5e
ILT
492 }
493 if (aouthdr_int->tsize)
494 {
495 aouthdr_int->text_start += a->ImageBase;
fac41780 496#ifndef COFF_WITH_PEP64
277d1b5e 497 aouthdr_int->text_start &= 0xffffffff;
fac41780 498#endif
277d1b5e 499 }
fac41780
JW
500#ifndef COFF_WITH_PEP64
501 /* PE32+ does not have data_start member! */
277d1b5e
ILT
502 if (aouthdr_int->dsize)
503 {
504 aouthdr_int->data_start += a->ImageBase;
505 aouthdr_int->data_start &= 0xffffffff;
506 }
fac41780 507#endif
277d1b5e
ILT
508
509#ifdef POWERPC_LE_PE
510 /* These three fields are normally set up by ppc_relocate_section.
511 In the case of reading a file in, we can pick them up from the
512 DataDirectory. */
513 first_thunk_address = a->DataDirectory[12].VirtualAddress ;
514 thunk_size = a->DataDirectory[12].Size;
515 import_table_size = a->DataDirectory[1].Size;
516#endif
517
518}
519
5933bdc9
ILT
520/* A support function for below. */
521
522static void
523add_data_entry (abfd, aout, idx, name, base)
277d1b5e
ILT
524 bfd *abfd;
525 struct internal_extra_pe_aouthdr *aout;
526 int idx;
527 char *name;
528 bfd_vma base;
529{
530 asection *sec = bfd_get_section_by_name (abfd, name);
531
532 /* add import directory information if it exists */
533 if ((sec != NULL)
534 && (coff_section_data (abfd, sec) != NULL)
535 && (pei_section_data (abfd, sec) != NULL))
536 {
3028b4c0
DD
537 /* If data directory is empty, rva also should be 0 */
538 int size = pei_section_data (abfd, sec)->virt_size;
539 aout->DataDirectory[idx].Size = size;
540
541 if (size)
542 {
543 aout->DataDirectory[idx].VirtualAddress =
544 (sec->vma - base) & 0xffffffff;
545 sec->flags |= SEC_DATA;
546 }
277d1b5e
ILT
547 }
548}
549
550unsigned int
551_bfd_pei_swap_aouthdr_out (abfd, in, out)
552 bfd *abfd;
553 PTR in;
554 PTR out;
555{
556 struct internal_aouthdr *aouthdr_in = (struct internal_aouthdr *)in;
557 struct internal_extra_pe_aouthdr *extra = &pe_data (abfd)->pe_opthdr;
558 PEAOUTHDR *aouthdr_out = (PEAOUTHDR *)out;
fac41780
JW
559 bfd_vma sa, fa, ib;
560
561 /* The following definitely is required for EFI applications.
562 Perhaps it's needed for other PEI targets as well, but I don't
563 know that for a fact, so we play it safe here and tweak the
564 alignments only if PEI_FORCE_MINIMUM_ALIGNMENT is
565 defined. --davidm */
566#ifdef PEI_FORCE_MINIMUM_ALIGNMENT
567 if (!extra->FileAlignment)
568 extra->FileAlignment = PE_DEF_FILE_ALIGNMENT;
569 if (!extra->SectionAlignment)
570 extra->SectionAlignment = PE_DEF_SECTION_ALIGNMENT;
571#endif
277d1b5e 572
fac41780
JW
573#ifdef PEI_DEFAULT_TARGET_SUBSYSTEM
574 if (extra->Subsystem == IMAGE_SUBSYSTEM_UNKNOWN)
575 extra->Subsystem = PEI_DEFAULT_TARGET_SUBSYSTEM;
576#endif
577
578 sa = extra->SectionAlignment;
579 fa = extra->FileAlignment;
580 ib = extra->ImageBase;
277d1b5e
ILT
581
582 if (aouthdr_in->tsize)
583 {
584 aouthdr_in->text_start -= ib;
585 aouthdr_in->text_start &= 0xffffffff;
586 }
587 if (aouthdr_in->dsize)
588 {
589 aouthdr_in->data_start -= ib;
590 aouthdr_in->data_start &= 0xffffffff;
591 }
592 if (aouthdr_in->entry)
593 {
594 aouthdr_in->entry -= ib;
595 aouthdr_in->entry &= 0xffffffff;
596 }
597
598#define FA(x) (((x) + fa -1 ) & (- fa))
599#define SA(x) (((x) + sa -1 ) & (- sa))
600
601 /* We like to have the sizes aligned */
602
603 aouthdr_in->bsize = FA (aouthdr_in->bsize);
604
605
606 extra->NumberOfRvaAndSizes = IMAGE_NUMBEROF_DIRECTORY_ENTRIES;
607
608 /* first null out all data directory entries .. */
609 memset (extra->DataDirectory, sizeof (extra->DataDirectory), 0);
610
8181c403 611 add_data_entry (abfd, extra, 0, ".edata", ib);
277d1b5e 612
2fbadf2c
ILT
613 /* Don't call add_data_entry for .idata$2 or .idata$5. It's done in
614 bfd_coff_final_link where all the required information is
615 available. */
277d1b5e 616
2fbadf2c
ILT
617 /* However, until other .idata fixes are made (pending patch), the
618 entry for .idata is needed for backwards compatability. FIXME. */
8181c403 619 add_data_entry (abfd, extra, 1, ".idata" , ib);
277d1b5e 620
8181c403 621 add_data_entry (abfd, extra, 2, ".rsrc" , ib);
2fbadf2c 622
8181c403 623 add_data_entry (abfd, extra, 3, ".pdata", ib);
2fbadf2c
ILT
624
625 /* For some reason, the virtual size (which is what's set by
626 add_data_entry) for .reloc is not the same as the size recorded
627 in this slot by MSVC; it doesn't seem to cause problems (so far),
628 but since it's the best we've got, use it. It does do the right
629 thing for .pdata. */
630 if (pe_data (abfd)->has_reloc_section)
8181c403 631 add_data_entry (abfd, extra, 5, ".reloc", ib);
277d1b5e
ILT
632
633 {
634 asection *sec;
635 bfd_vma dsize= 0;
636 bfd_vma isize = SA(abfd->sections->filepos);
637 bfd_vma tsize= 0;
638
639 for (sec = abfd->sections; sec; sec = sec->next)
640 {
641 int rounded = FA(sec->_raw_size);
642
643 if (sec->flags & SEC_DATA)
644 dsize += rounded;
645 if (sec->flags & SEC_CODE)
646 tsize += rounded;
5933bdc9
ILT
647 /* The image size is the total VIRTUAL size (which is what is
648 in the virt_size field). Files have been seen (from MSVC
649 5.0 link.exe) where the file size of the .data segment is
650 quite small compared to the virtual size. Without this
651 fix, strip munges the file. */
652 isize += SA (FA (pei_section_data (abfd, sec)->virt_size));
277d1b5e
ILT
653 }
654
655 aouthdr_in->dsize = dsize;
656 aouthdr_in->tsize = tsize;
657 extra->SizeOfImage = isize;
658 }
659
660 extra->SizeOfHeaders = abfd->sections->filepos;
661 bfd_h_put_16(abfd, aouthdr_in->magic, (bfd_byte *) aouthdr_out->standard.magic);
662
5933bdc9
ILT
663#define LINKER_VERSION 256 /* That is, 2.56 */
664
665 /* This piece of magic sets the "linker version" field to
666 LINKER_VERSION. */
667 bfd_h_put_16 (abfd,
668 LINKER_VERSION / 100 + (LINKER_VERSION % 100) * 256,
669 (bfd_byte *) aouthdr_out->standard.vstamp);
277d1b5e
ILT
670
671 PUT_AOUTHDR_TSIZE (abfd, aouthdr_in->tsize, (bfd_byte *) aouthdr_out->standard.tsize);
672 PUT_AOUTHDR_DSIZE (abfd, aouthdr_in->dsize, (bfd_byte *) aouthdr_out->standard.dsize);
673 PUT_AOUTHDR_BSIZE (abfd, aouthdr_in->bsize, (bfd_byte *) aouthdr_out->standard.bsize);
674 PUT_AOUTHDR_ENTRY (abfd, aouthdr_in->entry, (bfd_byte *) aouthdr_out->standard.entry);
675 PUT_AOUTHDR_TEXT_START (abfd, aouthdr_in->text_start,
676 (bfd_byte *) aouthdr_out->standard.text_start);
677
fac41780
JW
678#ifndef COFF_WITH_PEP64
679 /* PE32+ does not have data_start member! */
277d1b5e
ILT
680 PUT_AOUTHDR_DATA_START (abfd, aouthdr_in->data_start,
681 (bfd_byte *) aouthdr_out->standard.data_start);
fac41780 682#endif
277d1b5e 683
fac41780
JW
684 PUT_OPTHDR_IMAGE_BASE (abfd, extra->ImageBase,
685 (bfd_byte *) aouthdr_out->ImageBase);
277d1b5e
ILT
686 bfd_h_put_32 (abfd, extra->SectionAlignment,
687 (bfd_byte *) aouthdr_out->SectionAlignment);
688 bfd_h_put_32 (abfd, extra->FileAlignment,
689 (bfd_byte *) aouthdr_out->FileAlignment);
690 bfd_h_put_16 (abfd, extra->MajorOperatingSystemVersion,
691 (bfd_byte *) aouthdr_out->MajorOperatingSystemVersion);
692 bfd_h_put_16 (abfd, extra->MinorOperatingSystemVersion,
693 (bfd_byte *) aouthdr_out->MinorOperatingSystemVersion);
694 bfd_h_put_16 (abfd, extra->MajorImageVersion,
695 (bfd_byte *) aouthdr_out->MajorImageVersion);
696 bfd_h_put_16 (abfd, extra->MinorImageVersion,
697 (bfd_byte *) aouthdr_out->MinorImageVersion);
698 bfd_h_put_16 (abfd, extra->MajorSubsystemVersion,
699 (bfd_byte *) aouthdr_out->MajorSubsystemVersion);
700 bfd_h_put_16 (abfd, extra->MinorSubsystemVersion,
701 (bfd_byte *) aouthdr_out->MinorSubsystemVersion);
702 bfd_h_put_32 (abfd, extra->Reserved1,
703 (bfd_byte *) aouthdr_out->Reserved1);
704 bfd_h_put_32 (abfd, extra->SizeOfImage,
705 (bfd_byte *) aouthdr_out->SizeOfImage);
706 bfd_h_put_32 (abfd, extra->SizeOfHeaders,
707 (bfd_byte *) aouthdr_out->SizeOfHeaders);
708 bfd_h_put_32 (abfd, extra->CheckSum,
709 (bfd_byte *) aouthdr_out->CheckSum);
710 bfd_h_put_16 (abfd, extra->Subsystem,
711 (bfd_byte *) aouthdr_out->Subsystem);
712 bfd_h_put_16 (abfd, extra->DllCharacteristics,
713 (bfd_byte *) aouthdr_out->DllCharacteristics);
fac41780
JW
714 PUT_OPTHDR_SIZE_OF_STACK_RESERVE (abfd, extra->SizeOfStackReserve,
715 (bfd_byte *) aouthdr_out->SizeOfStackReserve);
716 PUT_OPTHDR_SIZE_OF_STACK_COMMIT (abfd, extra->SizeOfStackCommit,
717 (bfd_byte *) aouthdr_out->SizeOfStackCommit);
718 PUT_OPTHDR_SIZE_OF_HEAP_RESERVE (abfd, extra->SizeOfHeapReserve,
719 (bfd_byte *) aouthdr_out->SizeOfHeapReserve);
720 PUT_OPTHDR_SIZE_OF_HEAP_COMMIT (abfd, extra->SizeOfHeapCommit,
721 (bfd_byte *) aouthdr_out->SizeOfHeapCommit);
277d1b5e
ILT
722 bfd_h_put_32 (abfd, extra->LoaderFlags,
723 (bfd_byte *) aouthdr_out->LoaderFlags);
724 bfd_h_put_32 (abfd, extra->NumberOfRvaAndSizes,
725 (bfd_byte *) aouthdr_out->NumberOfRvaAndSizes);
726 {
727 int idx;
728 for (idx=0; idx < 16; idx++)
729 {
730 bfd_h_put_32 (abfd, extra->DataDirectory[idx].VirtualAddress,
731 (bfd_byte *) aouthdr_out->DataDirectory[idx][0]);
732 bfd_h_put_32 (abfd, extra->DataDirectory[idx].Size,
733 (bfd_byte *) aouthdr_out->DataDirectory[idx][1]);
734 }
735 }
736
737 return AOUTSZ;
738}
739
740unsigned int
741_bfd_pei_only_swap_filehdr_out (abfd, in, out)
742 bfd *abfd;
743 PTR in;
744 PTR out;
745{
746 int idx;
747 struct internal_filehdr *filehdr_in = (struct internal_filehdr *)in;
748 struct external_PEI_filehdr *filehdr_out = (struct external_PEI_filehdr *)out;
749
750 if (pe_data (abfd)->has_reloc_section)
751 filehdr_in->f_flags &= ~F_RELFLG;
752
753 if (pe_data (abfd)->dll)
754 filehdr_in->f_flags |= F_DLL;
755
756 filehdr_in->pe.e_magic = DOSMAGIC;
757 filehdr_in->pe.e_cblp = 0x90;
758 filehdr_in->pe.e_cp = 0x3;
759 filehdr_in->pe.e_crlc = 0x0;
760 filehdr_in->pe.e_cparhdr = 0x4;
761 filehdr_in->pe.e_minalloc = 0x0;
762 filehdr_in->pe.e_maxalloc = 0xffff;
763 filehdr_in->pe.e_ss = 0x0;
764 filehdr_in->pe.e_sp = 0xb8;
765 filehdr_in->pe.e_csum = 0x0;
766 filehdr_in->pe.e_ip = 0x0;
767 filehdr_in->pe.e_cs = 0x0;
768 filehdr_in->pe.e_lfarlc = 0x40;
769 filehdr_in->pe.e_ovno = 0x0;
770
771 for (idx=0; idx < 4; idx++)
772 filehdr_in->pe.e_res[idx] = 0x0;
773
774 filehdr_in->pe.e_oemid = 0x0;
775 filehdr_in->pe.e_oeminfo = 0x0;
776
777 for (idx=0; idx < 10; idx++)
778 filehdr_in->pe.e_res2[idx] = 0x0;
779
780 filehdr_in->pe.e_lfanew = 0x80;
781
782 /* this next collection of data are mostly just characters. It appears
783 to be constant within the headers put on NT exes */
784 filehdr_in->pe.dos_message[0] = 0x0eba1f0e;
785 filehdr_in->pe.dos_message[1] = 0xcd09b400;
786 filehdr_in->pe.dos_message[2] = 0x4c01b821;
787 filehdr_in->pe.dos_message[3] = 0x685421cd;
788 filehdr_in->pe.dos_message[4] = 0x70207369;
789 filehdr_in->pe.dos_message[5] = 0x72676f72;
790 filehdr_in->pe.dos_message[6] = 0x63206d61;
791 filehdr_in->pe.dos_message[7] = 0x6f6e6e61;
792 filehdr_in->pe.dos_message[8] = 0x65622074;
793 filehdr_in->pe.dos_message[9] = 0x6e757220;
794 filehdr_in->pe.dos_message[10] = 0x206e6920;
795 filehdr_in->pe.dos_message[11] = 0x20534f44;
796 filehdr_in->pe.dos_message[12] = 0x65646f6d;
797 filehdr_in->pe.dos_message[13] = 0x0a0d0d2e;
798 filehdr_in->pe.dos_message[14] = 0x24;
799 filehdr_in->pe.dos_message[15] = 0x0;
800 filehdr_in->pe.nt_signature = NT_SIGNATURE;
801
802
803
804 bfd_h_put_16(abfd, filehdr_in->f_magic, (bfd_byte *) filehdr_out->f_magic);
805 bfd_h_put_16(abfd, filehdr_in->f_nscns, (bfd_byte *) filehdr_out->f_nscns);
806
807 bfd_h_put_32(abfd, time (0), (bfd_byte *) filehdr_out->f_timdat);
808 PUT_FILEHDR_SYMPTR (abfd, (bfd_vma) filehdr_in->f_symptr,
809 (bfd_byte *) filehdr_out->f_symptr);
810 bfd_h_put_32(abfd, filehdr_in->f_nsyms, (bfd_byte *) filehdr_out->f_nsyms);
811 bfd_h_put_16(abfd, filehdr_in->f_opthdr, (bfd_byte *) filehdr_out->f_opthdr);
812 bfd_h_put_16(abfd, filehdr_in->f_flags, (bfd_byte *) filehdr_out->f_flags);
813
814 /* put in extra dos header stuff. This data remains essentially
815 constant, it just has to be tacked on to the beginning of all exes
816 for NT */
817 bfd_h_put_16(abfd, filehdr_in->pe.e_magic, (bfd_byte *) filehdr_out->e_magic);
818 bfd_h_put_16(abfd, filehdr_in->pe.e_cblp, (bfd_byte *) filehdr_out->e_cblp);
819 bfd_h_put_16(abfd, filehdr_in->pe.e_cp, (bfd_byte *) filehdr_out->e_cp);
820 bfd_h_put_16(abfd, filehdr_in->pe.e_crlc, (bfd_byte *) filehdr_out->e_crlc);
821 bfd_h_put_16(abfd, filehdr_in->pe.e_cparhdr,
822 (bfd_byte *) filehdr_out->e_cparhdr);
823 bfd_h_put_16(abfd, filehdr_in->pe.e_minalloc,
824 (bfd_byte *) filehdr_out->e_minalloc);
825 bfd_h_put_16(abfd, filehdr_in->pe.e_maxalloc,
826 (bfd_byte *) filehdr_out->e_maxalloc);
827 bfd_h_put_16(abfd, filehdr_in->pe.e_ss, (bfd_byte *) filehdr_out->e_ss);
828 bfd_h_put_16(abfd, filehdr_in->pe.e_sp, (bfd_byte *) filehdr_out->e_sp);
829 bfd_h_put_16(abfd, filehdr_in->pe.e_csum, (bfd_byte *) filehdr_out->e_csum);
830 bfd_h_put_16(abfd, filehdr_in->pe.e_ip, (bfd_byte *) filehdr_out->e_ip);
831 bfd_h_put_16(abfd, filehdr_in->pe.e_cs, (bfd_byte *) filehdr_out->e_cs);
832 bfd_h_put_16(abfd, filehdr_in->pe.e_lfarlc, (bfd_byte *) filehdr_out->e_lfarlc);
833 bfd_h_put_16(abfd, filehdr_in->pe.e_ovno, (bfd_byte *) filehdr_out->e_ovno);
834 {
835 int idx;
836 for (idx=0; idx < 4; idx++)
837 bfd_h_put_16(abfd, filehdr_in->pe.e_res[idx],
838 (bfd_byte *) filehdr_out->e_res[idx]);
839 }
840 bfd_h_put_16(abfd, filehdr_in->pe.e_oemid, (bfd_byte *) filehdr_out->e_oemid);
841 bfd_h_put_16(abfd, filehdr_in->pe.e_oeminfo,
842 (bfd_byte *) filehdr_out->e_oeminfo);
843 {
844 int idx;
845 for (idx=0; idx < 10; idx++)
846 bfd_h_put_16(abfd, filehdr_in->pe.e_res2[idx],
847 (bfd_byte *) filehdr_out->e_res2[idx]);
848 }
849 bfd_h_put_32(abfd, filehdr_in->pe.e_lfanew, (bfd_byte *) filehdr_out->e_lfanew);
850
851 {
852 int idx;
853 for (idx=0; idx < 16; idx++)
854 bfd_h_put_32(abfd, filehdr_in->pe.dos_message[idx],
855 (bfd_byte *) filehdr_out->dos_message[idx]);
856 }
857
858 /* also put in the NT signature */
859 bfd_h_put_32(abfd, filehdr_in->pe.nt_signature,
860 (bfd_byte *) filehdr_out->nt_signature);
861
862
863
864
865 return FILHSZ;
866}
867
868unsigned int
869_bfd_pe_only_swap_filehdr_out (abfd, in, out)
870 bfd *abfd;
871 PTR in;
872 PTR out;
873{
874 struct internal_filehdr *filehdr_in = (struct internal_filehdr *)in;
875 FILHDR *filehdr_out = (FILHDR *)out;
876
877 bfd_h_put_16(abfd, filehdr_in->f_magic, (bfd_byte *) filehdr_out->f_magic);
878 bfd_h_put_16(abfd, filehdr_in->f_nscns, (bfd_byte *) filehdr_out->f_nscns);
879 bfd_h_put_32(abfd, filehdr_in->f_timdat, (bfd_byte *) filehdr_out->f_timdat);
880 PUT_FILEHDR_SYMPTR (abfd, (bfd_vma) filehdr_in->f_symptr,
881 (bfd_byte *) filehdr_out->f_symptr);
882 bfd_h_put_32(abfd, filehdr_in->f_nsyms, (bfd_byte *) filehdr_out->f_nsyms);
883 bfd_h_put_16(abfd, filehdr_in->f_opthdr, (bfd_byte *) filehdr_out->f_opthdr);
884 bfd_h_put_16(abfd, filehdr_in->f_flags, (bfd_byte *) filehdr_out->f_flags);
885
886 return FILHSZ;
887}
888
889unsigned int
890_bfd_pei_swap_scnhdr_out (abfd, in, out)
891 bfd *abfd;
892 PTR in;
893 PTR out;
894{
895 struct internal_scnhdr *scnhdr_int = (struct internal_scnhdr *)in;
896 SCNHDR *scnhdr_ext = (SCNHDR *)out;
897 unsigned int ret = SCNHSZ;
898 bfd_vma ps;
899 bfd_vma ss;
900
901 memcpy(scnhdr_ext->s_name, scnhdr_int->s_name, sizeof(scnhdr_int->s_name));
902
903 PUT_SCNHDR_VADDR (abfd,
904 ((scnhdr_int->s_vaddr
905 - pe_data(abfd)->pe_opthdr.ImageBase)
906 & 0xffffffff),
907 (bfd_byte *) scnhdr_ext->s_vaddr);
908
5933bdc9
ILT
909 /* NT wants the size data to be rounded up to the next
910 NT_FILE_ALIGNMENT, but zero if it has no content (as in .bss,
911 sometimes). */
277d1b5e 912
5933bdc9 913 if ((scnhdr_int->s_flags & IMAGE_SCN_CNT_UNINITIALIZED_DATA) != 0)
277d1b5e
ILT
914 {
915 ps = scnhdr_int->s_size;
916 ss = 0;
917 }
918 else
919 {
920 ps = scnhdr_int->s_paddr;
921 ss = scnhdr_int->s_size;
922 }
923
924 PUT_SCNHDR_SIZE (abfd, ss,
925 (bfd_byte *) scnhdr_ext->s_size);
926
927
5933bdc9 928 /* s_paddr in PE is really the virtual size. */
277d1b5e
ILT
929 PUT_SCNHDR_PADDR (abfd, ps, (bfd_byte *) scnhdr_ext->s_paddr);
930
931 PUT_SCNHDR_SCNPTR (abfd, scnhdr_int->s_scnptr,
932 (bfd_byte *) scnhdr_ext->s_scnptr);
933 PUT_SCNHDR_RELPTR (abfd, scnhdr_int->s_relptr,
934 (bfd_byte *) scnhdr_ext->s_relptr);
935 PUT_SCNHDR_LNNOPTR (abfd, scnhdr_int->s_lnnoptr,
936 (bfd_byte *) scnhdr_ext->s_lnnoptr);
937
938 /* Extra flags must be set when dealing with NT. All sections should also
939 have the IMAGE_SCN_MEM_READ (0x40000000) flag set. In addition, the
940 .text section must have IMAGE_SCN_MEM_EXECUTE (0x20000000) and the data
941 sections (.idata, .data, .bss, .CRT) must have IMAGE_SCN_MEM_WRITE set
942 (this is especially important when dealing with the .idata section since
943 the addresses for routines from .dlls must be overwritten). If .reloc
944 section data is ever generated, we must add IMAGE_SCN_MEM_DISCARDABLE
945 (0x02000000). Also, the resource data should also be read and
946 writable. */
947
948 /* FIXME: alignment is also encoded in this field, at least on ppc (krk) */
949 /* FIXME: even worse, I don't see how to get the original alignment field*/
950 /* back... */
951
277d1b5e
ILT
952 {
953 int flags = scnhdr_int->s_flags;
277d1b5e
ILT
954 bfd_h_put_32(abfd, flags, (bfd_byte *) scnhdr_ext->s_flags);
955 }
956
cb43721d
ILT
957 if (coff_data (abfd)->link_info
958 && ! coff_data (abfd)->link_info->relocateable
959 && ! coff_data (abfd)->link_info->shared
960 && strcmp (scnhdr_int->s_name, ".text") == 0)
277d1b5e 961 {
cb43721d
ILT
962 /* By inference from looking at MS output, the 32 bit field
963 which is the combintion of the number_of_relocs and
964 number_of_linenos is used for the line number count in
965 executables. A 16-bit field won't do for cc1. The MS
966 document says that the number of relocs is zero for
967 executables, but the 17-th bit has been observed to be there.
968 Overflow is not an issue: a 4G-line program will overflow a
969 bunch of other fields long before this! */
970 bfd_h_put_16 (abfd, scnhdr_int->s_nlnno & 0xffff,
971 (bfd_byte *) scnhdr_ext->s_nlnno);
972 bfd_h_put_16 (abfd, scnhdr_int->s_nlnno >> 16,
973 (bfd_byte *) scnhdr_ext->s_nreloc);
277d1b5e 974 }
277d1b5e
ILT
975 else
976 {
cb43721d
ILT
977 if (scnhdr_int->s_nlnno <= 0xffff)
978 bfd_h_put_16 (abfd, scnhdr_int->s_nlnno,
979 (bfd_byte *) scnhdr_ext->s_nlnno);
980 else
981 {
982 (*_bfd_error_handler) (_("%s: line number overflow: 0x%lx > 0xffff"),
983 bfd_get_filename (abfd),
984 scnhdr_int->s_nlnno);
985 bfd_set_error (bfd_error_file_truncated);
986 bfd_h_put_16 (abfd, 0xffff, (bfd_byte *) scnhdr_ext->s_nlnno);
987 ret = 0;
988 }
989 if (scnhdr_int->s_nreloc <= 0xffff)
990 bfd_h_put_16 (abfd, scnhdr_int->s_nreloc,
991 (bfd_byte *) scnhdr_ext->s_nreloc);
992 else
993 {
3e4554a2
DD
994 /* PE can deal with large #s of relocs, but not here */
995 bfd_h_put_16 (abfd, 0xffff, (bfd_byte *) scnhdr_ext->s_nreloc);
996 scnhdr_int->s_flags |= IMAGE_SCN_LNK_NRELOC_OVFL;
997 bfd_h_put_32(abfd, scnhdr_int->s_flags,
998 (bfd_byte *) scnhdr_ext->s_flags);
999#if 0
1000 (*_bfd_error_handler) (_("%s: reloc overflow 1: 0x%lx > 0xffff"),
cb43721d
ILT
1001 bfd_get_filename (abfd),
1002 scnhdr_int->s_nreloc);
1003 bfd_set_error (bfd_error_file_truncated);
1004 bfd_h_put_16 (abfd, 0xffff, (bfd_byte *) scnhdr_ext->s_nreloc);
1005 ret = 0;
3e4554a2 1006#endif
cb43721d 1007 }
277d1b5e
ILT
1008 }
1009 return ret;
1010}
1011
1012static char * dir_names[IMAGE_NUMBEROF_DIRECTORY_ENTRIES] =
1013{
1014 N_ ("Export Directory [.edata (or where ever we found it)]"),
1015 N_ ("Import Directory [parts of .idata]"),
1016 N_ ("Resource Directory [.rsrc]"),
1017 N_ ("Exception Directory [.pdata]"),
1018 N_ ("Security Directory"),
1019 N_ ("Base Relocation Directory [.reloc]"),
1020 N_ ("Debug Directory"),
1021 N_ ("Description Directory"),
1022 N_ ("Special Directory"),
1023 N_ ("Thread Storage Directory [.tls]"),
1024 N_ ("Load Configuration Directory"),
1025 N_ ("Bound Import Directory"),
1026 N_ ("Import Address Table Directory"),
8181c403 1027 N_ ("Delay Import Directory"),
277d1b5e
ILT
1028 N_ ("Reserved"),
1029 N_ ("Reserved")
1030};
1031
1032/**********************************************************************/
1033#ifdef POWERPC_LE_PE
1034/* The code for the PPC really falls in the "architecture dependent"
1035 category. However, it's not clear that anyone will ever care, so
1036 we're ignoring the issue for now; if/when PPC matters, some of this
1037 may need to go into peicode.h, or arguments passed to enable the
1038 PPC- specific code. */
1039#endif
1040
1041/**********************************************************************/
1042static boolean
5933bdc9 1043pe_print_idata (abfd, vfile)
277d1b5e
ILT
1044 bfd *abfd;
1045 PTR vfile;
1046{
1047 FILE *file = (FILE *) vfile;
a76b448c 1048 bfd_byte *data;
8181c403
AM
1049 asection *section;
1050 bfd_signed_vma adj;
277d1b5e
ILT
1051
1052#ifdef POWERPC_LE_PE
1053 asection *rel_section = bfd_get_section_by_name (abfd, ".reldata");
1054#endif
1055
a76b448c 1056 bfd_size_type datasize = 0;
277d1b5e 1057 bfd_size_type dataoff;
277d1b5e 1058 bfd_size_type i;
277d1b5e
ILT
1059 int onaline = 20;
1060
1061 pe_data_type *pe = pe_data (abfd);
1062 struct internal_extra_pe_aouthdr *extra = &pe->pe_opthdr;
1063
8181c403 1064 bfd_vma addr;
277d1b5e 1065
8181c403 1066 addr = extra->DataDirectory[1].VirtualAddress;
277d1b5e 1067
a76b448c 1068 if (addr == 0 && extra->DataDirectory[1].Size == 0)
8181c403 1069 {
a76b448c
AM
1070 /* Maybe the extra header isn't there. Look for the section. */
1071 section = bfd_get_section_by_name (abfd, ".idata");
1072 if (section == NULL)
1073 return true;
1074
1075 addr = section->vma;
1076 datasize = bfd_section_size (abfd, section);
1077 if (datasize == 0)
1078 return true;
8181c403 1079 }
a76b448c 1080 else
8181c403 1081 {
a76b448c
AM
1082 addr += extra->ImageBase;
1083 for (section = abfd->sections; section != NULL; section = section->next)
1084 {
1085 datasize = bfd_section_size (abfd, section);
1086 if (addr >= section->vma && addr < section->vma + datasize)
1087 break;
1088 }
1089
1090 if (section == NULL)
1091 {
1092 fprintf (file,
1093 _("\nThere is an import table, but the section containing it could not be found\n"));
1094 return true;
1095 }
8181c403 1096 }
5933bdc9 1097
8181c403
AM
1098 fprintf (file, _("\nThere is an import table in %s at 0x%lx\n"),
1099 section->name, (unsigned long) addr);
277d1b5e 1100
8181c403 1101 dataoff = addr - section->vma;
a76b448c 1102 datasize -= dataoff;
277d1b5e
ILT
1103
1104#ifdef POWERPC_LE_PE
1105 if (rel_section != 0 && bfd_section_size (abfd, rel_section) != 0)
1106 {
1107 /* The toc address can be found by taking the starting address,
1108 which on the PPC locates a function descriptor. The
1109 descriptor consists of the function code starting address
1110 followed by the address of the toc. The starting address we
1111 get from the bfd, and the descriptor is supposed to be in the
1112 .reldata section. */
1113
1114 bfd_vma loadable_toc_address;
1115 bfd_vma toc_address;
1116 bfd_vma start_address;
1117 bfd_byte *data = 0;
1118 int offset;
8181c403 1119
277d1b5e
ILT
1120 data = (bfd_byte *) bfd_malloc ((size_t) bfd_section_size (abfd,
1121 rel_section));
1122 if (data == NULL && bfd_section_size (abfd, rel_section) != 0)
1123 return false;
1124
277d1b5e
ILT
1125 bfd_get_section_contents (abfd,
1126 rel_section,
1127 (PTR) data, 0,
1128 bfd_section_size (abfd, rel_section));
1129
1130 offset = abfd->start_address - rel_section->vma;
1131
db8503c4
AM
1132 start_address = bfd_get_32 (abfd, data + offset);
1133 loadable_toc_address = bfd_get_32 (abfd, data + offset + 4);
277d1b5e
ILT
1134 toc_address = loadable_toc_address - 32768;
1135
1136 fprintf(file,
1137 _("\nFunction descriptor located at the start address: %04lx\n"),
1138 (unsigned long int) (abfd->start_address));
1139 fprintf (file,
1140 _("\tcode-base %08lx toc (loadable/actual) %08lx/%08lx\n"),
1141 start_address, loadable_toc_address, toc_address);
1142 }
1143 else
1144 {
1145 fprintf(file,
1146 _("\nNo reldata section! Function descriptor not decoded.\n"));
1147 }
1148#endif
1149
1150 fprintf(file,
8181c403
AM
1151 _("\nThe Import Tables (interpreted %s section contents)\n"),
1152 section->name);
277d1b5e
ILT
1153 fprintf(file,
1154 _(" vma: Hint Time Forward DLL First\n"));
1155 fprintf(file,
1156 _(" Table Stamp Chain Name Thunk\n"));
1157
db8503c4 1158 data = (bfd_byte *) bfd_malloc (dataoff + datasize);
a76b448c 1159 if (data == NULL)
277d1b5e
ILT
1160 return false;
1161
db8503c4
AM
1162 /* Read the whole section. Some of the fields might be before dataoff. */
1163 if (! bfd_get_section_contents (abfd, section, (PTR) data,
1164 0, dataoff + datasize))
277d1b5e
ILT
1165 return false;
1166
db8503c4 1167 adj = section->vma - extra->ImageBase;
277d1b5e 1168
5933bdc9 1169 for (i = 0; i < datasize; i += onaline)
277d1b5e
ILT
1170 {
1171 bfd_vma hint_addr;
1172 bfd_vma time_stamp;
1173 bfd_vma forward_chain;
1174 bfd_vma dll_name;
1175 bfd_vma first_thunk;
1176 int idx = 0;
1177 bfd_size_type j;
1178 char *dll;
1179
8181c403 1180 /* print (i + extra->DataDirectory[1].VirtualAddress) */
db8503c4 1181 fprintf (file, " %08lx\t", (unsigned long) (i + adj + dataoff));
8181c403 1182
5933bdc9 1183 if (i + 20 > datasize)
277d1b5e
ILT
1184 {
1185 /* check stuff */
1186 ;
1187 }
5933bdc9 1188
db8503c4
AM
1189 hint_addr = bfd_get_32 (abfd, data + i + dataoff);
1190 time_stamp = bfd_get_32 (abfd, data + i + 4 + dataoff);
1191 forward_chain = bfd_get_32 (abfd, data + i + 8 + dataoff);
1192 dll_name = bfd_get_32 (abfd, data + i + 12 + dataoff);
1193 first_thunk = bfd_get_32 (abfd, data + i + 16 + dataoff);
5933bdc9
ILT
1194
1195 fprintf (file, "%08lx %08lx %08lx %08lx %08lx\n",
a76b448c
AM
1196 (unsigned long) hint_addr,
1197 (unsigned long) time_stamp,
1198 (unsigned long) forward_chain,
1199 (unsigned long) dll_name,
1200 (unsigned long) first_thunk);
277d1b5e
ILT
1201
1202 if (hint_addr == 0 && first_thunk == 0)
1203 break;
1204
8181c403 1205 dll = (char *) data + dll_name - adj;
277d1b5e
ILT
1206 fprintf(file, _("\n\tDLL Name: %s\n"), dll);
1207
1208 if (hint_addr != 0)
1209 {
1210 fprintf (file, _("\tvma: Hint/Ord Member-Name\n"));
1211
8181c403 1212 idx = hint_addr - adj;
277d1b5e 1213
5933bdc9 1214 for (j = 0; j < datasize; j += 4)
277d1b5e
ILT
1215 {
1216 unsigned long member = bfd_get_32 (abfd, data + idx + j);
1217
1218 if (member == 0)
1219 break;
1220 if (member & 0x80000000)
1221 fprintf (file, "\t%04lx\t %4lu", member,
1222 member & 0x7fffffff);
1223 else
1224 {
1225 int ordinal;
1226 char *member_name;
1227
8181c403
AM
1228 ordinal = bfd_get_16 (abfd, data + member - adj);
1229 member_name = (char *) data + member - adj + 2;
277d1b5e
ILT
1230 fprintf (file, "\t%04lx\t %4d %s",
1231 member, ordinal, member_name);
1232 }
1233
1234 /* If the time stamp is not zero, the import address
1235 table holds actual addresses. */
1236 if (time_stamp != 0
1237 && first_thunk != 0
1238 && first_thunk != hint_addr)
1239 fprintf (file, "\t%04lx",
a76b448c 1240 (long) bfd_get_32 (abfd, data + first_thunk - adj + j));
277d1b5e
ILT
1241
1242 fprintf (file, "\n");
1243 }
1244 }
1245
1246 if (hint_addr != first_thunk && time_stamp == 0)
1247 {
1248 int differ = 0;
1249 int idx2;
1250
8181c403 1251 idx2 = first_thunk - adj;
277d1b5e 1252
5933bdc9 1253 for (j = 0; j < datasize; j += 4)
277d1b5e
ILT
1254 {
1255 int ordinal;
1256 char *member_name;
1257 bfd_vma hint_member = 0;
1258 bfd_vma iat_member;
1259
1260 if (hint_addr != 0)
1261 hint_member = bfd_get_32 (abfd, data + idx + j);
1262 iat_member = bfd_get_32 (abfd, data + idx2 + j);
1263
1264 if (hint_addr == 0 && iat_member == 0)
1265 break;
1266
1267 if (hint_addr == 0 || hint_member != iat_member)
1268 {
1269 if (differ == 0)
1270 {
1271 fprintf (file,
1272 _("\tThe Import Address Table (difference found)\n"));
1273 fprintf(file, _("\tvma: Hint/Ord Member-Name\n"));
1274 differ = 1;
1275 }
1276 if (iat_member == 0)
1277 {
1278 fprintf(file,
1279 _("\t>>> Ran out of IAT members!\n"));
1280 }
1281 else
1282 {
db8503c4 1283 ordinal = bfd_get_16 (abfd, data + iat_member - adj);
8181c403 1284 member_name = (char *) data + iat_member - adj + 2;
277d1b5e 1285 fprintf(file, "\t%04lx\t %4d %s\n",
a76b448c
AM
1286 (unsigned long) iat_member,
1287 ordinal,
1288 member_name);
277d1b5e
ILT
1289 }
1290 }
1291
1292 if (hint_addr != 0 && hint_member == 0)
1293 break;
1294 }
1295 if (differ == 0)
1296 {
1297 fprintf(file,
1298 _("\tThe Import Address Table is identical\n"));
1299 }
1300 }
1301
1302 fprintf(file, "\n");
1303
1304 }
1305
1306 free (data);
1307
1308 return true;
1309}
1310
1311static boolean
1312pe_print_edata (abfd, vfile)
1313 bfd *abfd;
1314 PTR vfile;
1315{
1316 FILE *file = (FILE *) vfile;
a76b448c 1317 bfd_byte *data;
8181c403 1318 asection *section;
277d1b5e 1319
a76b448c 1320 bfd_size_type datasize = 0;
277d1b5e
ILT
1321 bfd_size_type dataoff;
1322 bfd_size_type i;
1323
8181c403 1324 bfd_signed_vma adj;
277d1b5e
ILT
1325 struct EDT_type
1326 {
1327 long export_flags; /* reserved - should be zero */
1328 long time_stamp;
1329 short major_ver;
1330 short minor_ver;
1331 bfd_vma name; /* rva - relative to image base */
1332 long base; /* ordinal base */
1333 unsigned long num_functions; /* Number in the export address table */
1334 unsigned long num_names; /* Number in the name pointer table */
1335 bfd_vma eat_addr; /* rva to the export address table */
1336 bfd_vma npt_addr; /* rva to the Export Name Pointer Table */
1337 bfd_vma ot_addr; /* rva to the Ordinal Table */
1338 } edt;
1339
1340 pe_data_type *pe = pe_data (abfd);
1341 struct internal_extra_pe_aouthdr *extra = &pe->pe_opthdr;
1342
8181c403 1343 bfd_vma addr;
277d1b5e 1344
8181c403 1345 addr = extra->DataDirectory[0].VirtualAddress;
277d1b5e 1346
a76b448c 1347 if (addr == 0 && extra->DataDirectory[0].Size == 0)
8181c403 1348 {
a76b448c
AM
1349 /* Maybe the extra header isn't there. Look for the section. */
1350 section = bfd_get_section_by_name (abfd, ".edata");
1351 if (section == NULL)
1352 return true;
1353
1354 addr = section->vma;
1355 datasize = bfd_section_size (abfd, section);
1356 if (datasize == 0)
1357 return true;
8181c403 1358 }
a76b448c 1359 else
8181c403 1360 {
a76b448c
AM
1361 addr += extra->ImageBase;
1362 for (section = abfd->sections; section != NULL; section = section->next)
1363 {
1364 datasize = bfd_section_size (abfd, section);
1365 if (addr >= section->vma && addr < section->vma + datasize)
1366 break;
1367 }
1368
1369 if (section == NULL)
1370 {
1371 fprintf (file,
1372 _("\nThere is an export table, but the section containing it could not be found\n"));
1373 return true;
1374 }
277d1b5e
ILT
1375 }
1376
8181c403
AM
1377 fprintf (file, _("\nThere is an export table in %s at 0x%lx\n"),
1378 section->name, (unsigned long) addr);
1379
1380 dataoff = addr - section->vma;
a76b448c 1381 datasize -= dataoff;
8181c403 1382
277d1b5e 1383 data = (bfd_byte *) bfd_malloc (datasize);
8181c403 1384 if (data == NULL)
277d1b5e
ILT
1385 return false;
1386
1387 if (! bfd_get_section_contents (abfd, section, (PTR) data, dataoff,
1388 datasize))
1389 return false;
1390
1391 /* Go get Export Directory Table */
1392 edt.export_flags = bfd_get_32(abfd, data+0);
1393 edt.time_stamp = bfd_get_32(abfd, data+4);
1394 edt.major_ver = bfd_get_16(abfd, data+8);
1395 edt.minor_ver = bfd_get_16(abfd, data+10);
1396 edt.name = bfd_get_32(abfd, data+12);
1397 edt.base = bfd_get_32(abfd, data+16);
1398 edt.num_functions = bfd_get_32(abfd, data+20);
1399 edt.num_names = bfd_get_32(abfd, data+24);
1400 edt.eat_addr = bfd_get_32(abfd, data+28);
1401 edt.npt_addr = bfd_get_32(abfd, data+32);
1402 edt.ot_addr = bfd_get_32(abfd, data+36);
1403
8181c403 1404 adj = section->vma - extra->ImageBase + dataoff;
277d1b5e
ILT
1405
1406 /* Dump the EDT first first */
1407 fprintf(file,
8181c403
AM
1408 _("\nThe Export Tables (interpreted %s section contents)\n\n"),
1409 section->name);
277d1b5e
ILT
1410
1411 fprintf(file,
1412 _("Export Flags \t\t\t%lx\n"), (unsigned long) edt.export_flags);
1413
1414 fprintf(file,
1415 _("Time/Date stamp \t\t%lx\n"), (unsigned long) edt.time_stamp);
1416
1417 fprintf(file,
1418 _("Major/Minor \t\t\t%d/%d\n"), edt.major_ver, edt.minor_ver);
1419
1420 fprintf (file,
1421 _("Name \t\t\t\t"));
1422 fprintf_vma (file, edt.name);
1423 fprintf (file,
8181c403 1424 " %s\n", data + edt.name - adj);
277d1b5e
ILT
1425
1426 fprintf(file,
1427 _("Ordinal Base \t\t\t%ld\n"), edt.base);
1428
1429 fprintf(file,
1430 _("Number in:\n"));
1431
1432 fprintf(file,
8181c403 1433 _("\tExport Address Table \t\t%08lx\n"),
277d1b5e
ILT
1434 edt.num_functions);
1435
1436 fprintf(file,
8181c403 1437 _("\t[Name Pointer/Ordinal] Table\t%08lx\n"), edt.num_names);
277d1b5e
ILT
1438
1439 fprintf(file,
1440 _("Table Addresses\n"));
1441
1442 fprintf (file,
1443 _("\tExport Address Table \t\t"));
1444 fprintf_vma (file, edt.eat_addr);
1445 fprintf (file, "\n");
1446
1447 fprintf (file,
1448 _("\tName Pointer Table \t\t"));
1449 fprintf_vma (file, edt.npt_addr);
1450 fprintf (file, "\n");
1451
1452 fprintf (file,
1453 _("\tOrdinal Table \t\t\t"));
1454 fprintf_vma (file, edt.ot_addr);
1455 fprintf (file, "\n");
1456
1457
5933bdc9 1458 /* The next table to find is the Export Address Table. It's basically
277d1b5e
ILT
1459 a list of pointers that either locate a function in this dll, or
1460 forward the call to another dll. Something like:
1461 typedef union
1462 {
1463 long export_rva;
1464 long forwarder_rva;
1465 } export_address_table_entry;
1466 */
1467
1468 fprintf(file,
1469 _("\nExport Address Table -- Ordinal Base %ld\n"),
1470 edt.base);
1471
1472 for (i = 0; i < edt.num_functions; ++i)
1473 {
1474 bfd_vma eat_member = bfd_get_32 (abfd,
8181c403 1475 data + edt.eat_addr + (i * 4) - adj);
277d1b5e
ILT
1476 if (eat_member == 0)
1477 continue;
1478
db8503c4 1479 if (eat_member - adj <= datasize)
277d1b5e 1480 {
db8503c4 1481 /* This rva is to a name (forwarding function) in our section. */
277d1b5e 1482 /* Should locate a function descriptor */
5933bdc9
ILT
1483 fprintf (file,
1484 "\t[%4ld] +base[%4ld] %04lx %s -- %s\n",
a76b448c
AM
1485 (long) i,
1486 (long) (i + edt.base),
1487 (unsigned long) eat_member,
1488 _("Forwarder RVA"),
1489 data + eat_member - adj);
277d1b5e
ILT
1490 }
1491 else
1492 {
1493 /* Should locate a function descriptor in the reldata section */
5933bdc9
ILT
1494 fprintf (file,
1495 "\t[%4ld] +base[%4ld] %04lx %s\n",
a76b448c
AM
1496 (long) i,
1497 (long) (i + edt.base),
1498 (unsigned long) eat_member,
5933bdc9 1499 _("Export RVA"));
277d1b5e
ILT
1500 }
1501 }
1502
1503 /* The Export Name Pointer Table is paired with the Export Ordinal Table */
1504 /* Dump them in parallel for clarity */
1505 fprintf(file,
1506 _("\n[Ordinal/Name Pointer] Table\n"));
1507
1508 for (i = 0; i < edt.num_names; ++i)
1509 {
1510 bfd_vma name_ptr = bfd_get_32(abfd,
1511 data +
1512 edt.npt_addr
8181c403 1513 + (i*4) - adj);
277d1b5e 1514
8181c403 1515 char *name = (char *) data + name_ptr - adj;
277d1b5e
ILT
1516
1517 bfd_vma ord = bfd_get_16(abfd,
1518 data +
1519 edt.ot_addr
8181c403 1520 + (i*2) - adj);
277d1b5e
ILT
1521 fprintf(file,
1522 "\t[%4ld] %s\n", (long) ord, name);
1523
1524 }
1525
1526 free (data);
1527
1528 return true;
1529}
1530
fac41780
JW
1531/* This really is architecture dependent. On IA-64, a .pdata entry
1532 consists of three dwords containing relative virtual addresses that
1533 specify the start and end address of the code range the entry
1534 covers and the address of the corresponding unwind info data. */
277d1b5e
ILT
1535static boolean
1536pe_print_pdata (abfd, vfile)
1537 bfd *abfd;
1538 PTR vfile;
1539{
fac41780
JW
1540#ifdef COFF_WITH_PEP64
1541# define PDATA_ROW_SIZE (3*8)
1542#else
1543# define PDATA_ROW_SIZE (5*4)
1544#endif
277d1b5e
ILT
1545 FILE *file = (FILE *) vfile;
1546 bfd_byte *data = 0;
1547 asection *section = bfd_get_section_by_name (abfd, ".pdata");
1548 bfd_size_type datasize = 0;
1549 bfd_size_type i;
1550 bfd_size_type start, stop;
fac41780 1551 int onaline = PDATA_ROW_SIZE;
277d1b5e 1552
5933bdc9
ILT
1553 if (section == NULL
1554 || coff_section_data (abfd, section) == NULL
1555 || pei_section_data (abfd, section) == NULL)
1556 return true;
277d1b5e 1557
5933bdc9 1558 stop = pei_section_data (abfd, section)->virt_size;
277d1b5e
ILT
1559 if ((stop % onaline) != 0)
1560 fprintf (file, _("Warning, .pdata section size (%ld) is not a multiple of %d\n"),
1561 (long)stop, onaline);
1562
5933bdc9
ILT
1563 fprintf (file,
1564 _("\nThe Function Table (interpreted .pdata section contents)\n"));
fac41780
JW
1565#ifdef COFF_WITH_PEP64
1566 fprintf(file,
1567 _(" vma:\t\t\tBegin Address End Address Unwind Info\n"));
1568#else
5933bdc9
ILT
1569 fprintf (file,
1570 _(" vma:\t\tBegin End EH EH PrologEnd Exception\n"));
1571 fprintf (file,
1572 _(" \t\tAddress Address Handler Data Address Mask\n"));
fac41780 1573#endif
277d1b5e
ILT
1574
1575 if (bfd_section_size (abfd, section) == 0)
1576 return true;
1577
1578 data = (bfd_byte *) bfd_malloc ((size_t) bfd_section_size (abfd, section));
1579 datasize = bfd_section_size (abfd, section);
1580 if (data == NULL && datasize != 0)
1581 return false;
1582
1583 bfd_get_section_contents (abfd,
1584 section,
1585 (PTR) data, 0,
1586 bfd_section_size (abfd, section));
1587
1588 start = 0;
1589
1590 for (i = start; i < stop; i += onaline)
1591 {
1592 bfd_vma begin_addr;
1593 bfd_vma end_addr;
1594 bfd_vma eh_handler;
1595 bfd_vma eh_data;
1596 bfd_vma prolog_end_addr;
5933bdc9 1597 int em_data;
277d1b5e 1598
fac41780 1599 if (i + PDATA_ROW_SIZE > stop)
277d1b5e 1600 break;
5933bdc9 1601
fac41780
JW
1602 begin_addr = GET_PDATA_ENTRY(abfd, data+i);
1603 end_addr = GET_PDATA_ENTRY(abfd, data+i+4);
1604 eh_handler = GET_PDATA_ENTRY(abfd, data+i+8);
1605 eh_data = GET_PDATA_ENTRY(abfd, data+i+12);
1606 prolog_end_addr = GET_PDATA_ENTRY(abfd, data+i+16);
277d1b5e
ILT
1607
1608 if (begin_addr == 0 && end_addr == 0 && eh_handler == 0
1609 && eh_data == 0 && prolog_end_addr == 0)
1610 {
1611 /* We are probably into the padding of the section now. */
1612 break;
1613 }
1614
5933bdc9 1615 em_data = ((eh_handler & 0x1) << 2) | (prolog_end_addr & 0x3);
fac41780
JW
1616 eh_handler &= ~ (bfd_vma) 0x3;
1617 prolog_end_addr &= ~ (bfd_vma) 0x3;
1618
1619 fputc (' ', file);
1620 fprintf_vma (file, i + section->vma); fputc ('\t', file);
1621 fprintf_vma (file, begin_addr); fputc (' ', file);
1622 fprintf_vma (file, end_addr); fputc (' ', file);
1623 fprintf_vma (file, eh_handler);
1624#ifndef COFF_WITH_PEP64
1625 fputc (' ', file);
1626 fprintf_vma (file, eh_data); fputc (' ', file);
1627 fprintf_vma (file, prolog_end_addr);
1628 fprintf (file, " %x", em_data);
1629#endif
277d1b5e
ILT
1630
1631#ifdef POWERPC_LE_PE
1632 if (eh_handler == 0 && eh_data != 0)
1633 {
1634 /* Special bits here, although the meaning may */
1635 /* be a little mysterious. The only one I know */
1636 /* for sure is 0x03. */
1637 /* Code Significance */
1638 /* 0x00 None */
1639 /* 0x01 Register Save Millicode */
1640 /* 0x02 Register Restore Millicode */
1641 /* 0x03 Glue Code Sequence */
1642 switch (eh_data)
1643 {
1644 case 0x01:
1645 fprintf(file, _(" Register save millicode"));
1646 break;
1647 case 0x02:
1648 fprintf(file, _(" Register restore millicode"));
1649 break;
1650 case 0x03:
1651 fprintf(file, _(" Glue code sequence"));
1652 break;
1653 default:
1654 break;
1655 }
1656 }
1657#endif
1658 fprintf(file, "\n");
1659 }
1660
1661 free (data);
1662
1663 return true;
1664}
1665
5933bdc9
ILT
1666#define IMAGE_REL_BASED_HIGHADJ 4
1667static const char * const tbl[] =
277d1b5e
ILT
1668{
1669"ABSOLUTE",
1670"HIGH",
1671"LOW",
1672"HIGHLOW",
1673"HIGHADJ",
5933bdc9 1674"MIPS_JMPADDR",
fac41780
JW
1675"SECTION",
1676"REL32",
1677"RESERVED1",
1678"MIPS_JMPADDR16",
1679"DIR64",
1680"HIGH3ADJ"
5933bdc9 1681"UNKNOWN", /* MUST be last */
277d1b5e
ILT
1682};
1683
1684static boolean
1685pe_print_reloc (abfd, vfile)
1686 bfd *abfd;
1687 PTR vfile;
1688{
1689 FILE *file = (FILE *) vfile;
1690 bfd_byte *data = 0;
1691 asection *section = bfd_get_section_by_name (abfd, ".reloc");
1692 bfd_size_type datasize = 0;
1693 bfd_size_type i;
1694 bfd_size_type start, stop;
1695
5933bdc9 1696 if (section == NULL)
277d1b5e
ILT
1697 return true;
1698
1699 if (bfd_section_size (abfd, section) == 0)
1700 return true;
1701
5933bdc9
ILT
1702 fprintf (file,
1703 _("\n\nPE File Base Relocations (interpreted .reloc section contents)\n"));
277d1b5e
ILT
1704
1705 data = (bfd_byte *) bfd_malloc ((size_t) bfd_section_size (abfd, section));
1706 datasize = bfd_section_size (abfd, section);
1707 if (data == NULL && datasize != 0)
1708 return false;
1709
1710 bfd_get_section_contents (abfd,
1711 section,
1712 (PTR) data, 0,
1713 bfd_section_size (abfd, section));
1714
1715 start = 0;
1716
1717 stop = bfd_section_size (abfd, section);
1718
1719 for (i = start; i < stop;)
1720 {
1721 int j;
1722 bfd_vma virtual_address;
1723 long number, size;
1724
1725 /* The .reloc section is a sequence of blocks, with a header consisting
1726 of two 32 bit quantities, followed by a number of 16 bit entries */
1727
1728 virtual_address = bfd_get_32(abfd, data+i);
1729 size = bfd_get_32(abfd, data+i+4);
1730 number = (size - 8) / 2;
1731
1732 if (size == 0)
1733 {
1734 break;
1735 }
1736
1737 fprintf (file,
1738 _("\nVirtual Address: %08lx Chunk size %ld (0x%lx) Number of fixups %ld\n"),
a76b448c 1739 (unsigned long) virtual_address, size, size, number);
277d1b5e
ILT
1740
1741 for (j = 0; j < number; ++j)
1742 {
5933bdc9
ILT
1743 unsigned short e = bfd_get_16 (abfd, data + i + 8 + j * 2);
1744 unsigned int t = (e & 0xF000) >> 12;
277d1b5e
ILT
1745 int off = e & 0x0FFF;
1746
5933bdc9
ILT
1747 if (t >= sizeof (tbl) / sizeof (tbl[0]))
1748 t = (sizeof (tbl) / sizeof (tbl[0])) - 1;
277d1b5e 1749
5933bdc9
ILT
1750 fprintf (file,
1751 _("\treloc %4d offset %4x [%4lx] %s"),
1752 j, off, (long) (off + virtual_address), tbl[t]);
277d1b5e 1753
17505c5c
NC
1754 /* HIGHADJ takes an argument, - the next record *is* the
1755 low 16 bits of addend. */
5933bdc9
ILT
1756 if (t == IMAGE_REL_BASED_HIGHADJ)
1757 {
17505c5c 1758 fprintf (file, " (%4x)",
5933bdc9
ILT
1759 ((unsigned int)
1760 bfd_get_16 (abfd, data + i + 8 + j * 2 + 2)));
1761 j++;
1762 }
17505c5c
NC
1763
1764 fprintf (file, "\n");
277d1b5e
ILT
1765 }
1766 i += size;
1767 }
1768
1769 free (data);
1770
1771 return true;
1772}
1773
1774/* Print out the program headers. */
1775
1776boolean
1777_bfd_pe_print_private_bfd_data_common (abfd, vfile)
1778 bfd *abfd;
1779 PTR vfile;
1780{
1781 FILE *file = (FILE *) vfile;
1782 int j;
1783 pe_data_type *pe = pe_data (abfd);
1784 struct internal_extra_pe_aouthdr *i = &pe->pe_opthdr;
fac41780 1785 const char *subsystem_name = NULL;
277d1b5e
ILT
1786
1787 /* The MS dumpbin program reportedly ands with 0xff0f before
1788 printing the characteristics field. Not sure why. No reason to
1789 emulate it here. */
1790 fprintf (file, _("\nCharacteristics 0x%x\n"), pe->real_flags);
1791#undef PF
1792#define PF(x, y) if (pe->real_flags & x) { fprintf (file, "\t%s\n", y); }
1793 PF (F_RELFLG, "relocations stripped");
1794 PF (F_EXEC, "executable");
1795 PF (F_LNNO, "line numbers stripped");
1796 PF (F_LSYMS, "symbols stripped");
1797 PF (0x80, "little endian");
1798 PF (F_AR32WR, "32 bit words");
1799 PF (0x200, "debugging information removed");
1800 PF (0x1000, "system file");
1801 PF (F_DLL, "DLL");
1802 PF (0x8000, "big endian");
1803#undef PF
1804
5933bdc9
ILT
1805 /* ctime implies '\n'. */
1806 fprintf (file, "\nTime/Date\t\t%s", ctime (&pe->coff.timestamp));
277d1b5e
ILT
1807 fprintf (file,"\nImageBase\t\t");
1808 fprintf_vma (file, i->ImageBase);
1809 fprintf (file,"\nSectionAlignment\t");
1810 fprintf_vma (file, i->SectionAlignment);
1811 fprintf (file,"\nFileAlignment\t\t");
1812 fprintf_vma (file, i->FileAlignment);
1813 fprintf (file,"\nMajorOSystemVersion\t%d\n", i->MajorOperatingSystemVersion);
1814 fprintf (file,"MinorOSystemVersion\t%d\n", i->MinorOperatingSystemVersion);
1815 fprintf (file,"MajorImageVersion\t%d\n", i->MajorImageVersion);
1816 fprintf (file,"MinorImageVersion\t%d\n", i->MinorImageVersion);
1817 fprintf (file,"MajorSubsystemVersion\t%d\n", i->MajorSubsystemVersion);
1818 fprintf (file,"MinorSubsystemVersion\t%d\n", i->MinorSubsystemVersion);
8181c403 1819 fprintf (file,"Win32Version\t\t%08lx\n", i->Reserved1);
277d1b5e
ILT
1820 fprintf (file,"SizeOfImage\t\t%08lx\n", i->SizeOfImage);
1821 fprintf (file,"SizeOfHeaders\t\t%08lx\n", i->SizeOfHeaders);
1822 fprintf (file,"CheckSum\t\t%08lx\n", i->CheckSum);
fac41780
JW
1823 switch (i->Subsystem)
1824 {
1825 case IMAGE_SUBSYSTEM_UNKNOWN:
1826 subsystem_name = "unspecified";
1827 break;
1828 case IMAGE_SUBSYSTEM_NATIVE:
1829 subsystem_name = "NT native";
1830 break;
1831 case IMAGE_SUBSYSTEM_WINDOWS_GUI:
1832 subsystem_name = "Windows GUI";
1833 break;
1834 case IMAGE_SUBSYSTEM_WINDOWS_CUI:
1835 subsystem_name = "Windows CUI";
1836 break;
1837 case IMAGE_SUBSYSTEM_POSIX_CUI:
1838 subsystem_name = "POSIX CUI";
1839 break;
1840 case IMAGE_SUBSYSTEM_WINDOWS_CE_GUI:
1841 subsystem_name = "Wince CUI";
1842 break;
1843 case IMAGE_SUBSYSTEM_EFI_APPLICATION:
1844 subsystem_name = "EFI application";
1845 break;
1846 case IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER:
1847 subsystem_name = "EFI boot service driver";
1848 break;
1849 case IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER:
1850 subsystem_name = "EFI runtime driver";
1851 break;
1852 }
1853 fprintf (file,"Subsystem\t\t%08x", i->Subsystem);
1854 if (subsystem_name)
1855 fprintf (file, "\t(%s)", subsystem_name);
1856 fprintf (file,"\nDllCharacteristics\t%08x\n", i->DllCharacteristics);
277d1b5e
ILT
1857 fprintf (file,"SizeOfStackReserve\t");
1858 fprintf_vma (file, i->SizeOfStackReserve);
1859 fprintf (file,"\nSizeOfStackCommit\t");
1860 fprintf_vma (file, i->SizeOfStackCommit);
1861 fprintf (file,"\nSizeOfHeapReserve\t");
1862 fprintf_vma (file, i->SizeOfHeapReserve);
1863 fprintf (file,"\nSizeOfHeapCommit\t");
1864 fprintf_vma (file, i->SizeOfHeapCommit);
1865 fprintf (file,"\nLoaderFlags\t\t%08lx\n", i->LoaderFlags);
1866 fprintf (file,"NumberOfRvaAndSizes\t%08lx\n", i->NumberOfRvaAndSizes);
1867
1868 fprintf (file,"\nThe Data Directory\n");
1869 for (j = 0; j < IMAGE_NUMBEROF_DIRECTORY_ENTRIES; j++)
1870 {
1871 fprintf (file, "Entry %1x ", j);
1872 fprintf_vma (file, i->DataDirectory[j].VirtualAddress);
1873 fprintf (file, " %08lx ", i->DataDirectory[j].Size);
1874 fprintf (file, "%s\n", dir_names[j]);
1875 }
1876
1877 pe_print_idata (abfd, vfile);
1878 pe_print_edata (abfd, vfile);
1879 pe_print_pdata (abfd, vfile);
1880 pe_print_reloc (abfd, vfile);
1881
1882 return true;
1883}
1884
1885/* Copy any private info we understand from the input bfd
1886 to the output bfd. */
1887
1888boolean
1889_bfd_pe_bfd_copy_private_bfd_data_common (ibfd, obfd)
1890 bfd *ibfd, *obfd;
1891{
1892 /* One day we may try to grok other private data. */
1893 if (ibfd->xvec->flavour != bfd_target_coff_flavour
1894 || obfd->xvec->flavour != bfd_target_coff_flavour)
1895 return true;
1896
1897 pe_data (obfd)->pe_opthdr = pe_data (ibfd)->pe_opthdr;
1898 pe_data (obfd)->dll = pe_data (ibfd)->dll;
1899
5933bdc9
ILT
1900 /* for strip: if we removed .reloc, we'll make a real mess of things
1901 if we don't remove this entry as well. */
1902 if (! pe_data (obfd)->has_reloc_section)
1903 {
1904 pe_data(obfd)->pe_opthdr.DataDirectory[5].VirtualAddress = 0;
1905 pe_data(obfd)->pe_opthdr.DataDirectory[5].Size = 0;
1906 }
277d1b5e
ILT
1907 return true;
1908}
1909
1910/* Copy private section data. */
1911boolean
1912_bfd_pe_bfd_copy_private_section_data (ibfd, isec, obfd, osec)
1913 bfd *ibfd;
1914 asection *isec;
1915 bfd *obfd;
1916 asection *osec;
1917{
1918 if (bfd_get_flavour (ibfd) != bfd_target_coff_flavour
1919 || bfd_get_flavour (obfd) != bfd_target_coff_flavour)
1920 return true;
1921
1922 if (coff_section_data (ibfd, isec) != NULL
1923 && pei_section_data (ibfd, isec) != NULL)
1924 {
1925 if (coff_section_data (obfd, osec) == NULL)
1926 {
1927 osec->used_by_bfd =
1928 (PTR) bfd_zalloc (obfd, sizeof (struct coff_section_tdata));
1929 if (osec->used_by_bfd == NULL)
1930 return false;
1931 }
1932 if (pei_section_data (obfd, osec) == NULL)
1933 {
1934 coff_section_data (obfd, osec)->tdata =
1935 (PTR) bfd_zalloc (obfd, sizeof (struct pei_section_tdata));
1936 if (coff_section_data (obfd, osec)->tdata == NULL)
1937 return false;
1938 }
1939 pei_section_data (obfd, osec)->virt_size =
1940 pei_section_data (ibfd, isec)->virt_size;
5933bdc9
ILT
1941 pei_section_data (obfd, osec)->pe_flags =
1942 pei_section_data (ibfd, isec)->pe_flags;
277d1b5e
ILT
1943 }
1944
1945 return true;
1946}
7d2b58d6
ILT
1947
1948void
1949_bfd_pe_get_symbol_info (abfd, symbol, ret)
1950 bfd *abfd;
1951 asymbol *symbol;
1952 symbol_info *ret;
1953{
1954 coff_get_symbol_info (abfd, symbol, ret);
1955
1956 if (pe_data (abfd) != NULL
1957 && ((symbol->flags & BSF_DEBUGGING) == 0
1958 || (symbol->flags & BSF_DEBUGGING_RELOC) != 0)
1959 && ! bfd_is_abs_section (symbol->section))
1960 ret->value += pe_data (abfd)->pe_opthdr.ImageBase;
1961}
2fbadf2c
ILT
1962
1963/* Handle the .idata section and other things that need symbol table
1964 access. */
1965
1966boolean
1967_bfd_pei_final_link_postscript (abfd, pfinfo)
1968 bfd *abfd;
1969 struct coff_final_link_info *pfinfo;
1970{
1971 struct coff_link_hash_entry *h1;
1972 struct bfd_link_info *info = pfinfo->info;
1973
1974 /* There are a few fields that need to be filled in now while we
1975 have symbol table access.
1976
1977 The .idata subsections aren't directly available as sections, but
1978 they are in the symbol table, so get them from there. */
1979
1980 /* The import directory. This is the address of .idata$2, with size
1981 of .idata$2 + .idata$3. */
1982 h1 = coff_link_hash_lookup (coff_hash_table (info),
1983 ".idata$2", false, false, true);
1984 if (h1 != NULL)
1985 {
1986 pe_data(abfd)->pe_opthdr.DataDirectory[1].VirtualAddress =
1987 (h1->root.u.def.value
1988 + h1->root.u.def.section->output_section->vma
1989 + h1->root.u.def.section->output_offset);
1990 h1 = coff_link_hash_lookup (coff_hash_table (info),
1991 ".idata$4", false, false, true);
1992 pe_data (abfd)->pe_opthdr.DataDirectory[1].Size =
1993 ((h1->root.u.def.value
1994 + h1->root.u.def.section->output_section->vma
1995 + h1->root.u.def.section->output_offset)
1996 - pe_data(abfd)->pe_opthdr.DataDirectory[1].VirtualAddress);
1997
1998 /* The import address table. This is the size/address of
1999 .idata$5. */
2000 h1 = coff_link_hash_lookup (coff_hash_table (info),
2001 ".idata$5", false, false, true);
2002 pe_data (abfd)->pe_opthdr.DataDirectory[12].VirtualAddress =
2003 (h1->root.u.def.value
2004 + h1->root.u.def.section->output_section->vma
2005 + h1->root.u.def.section->output_offset);
2006 h1 = coff_link_hash_lookup (coff_hash_table (info),
2007 ".idata$6", false, false, true);
2008 pe_data (abfd)->pe_opthdr.DataDirectory[12].Size =
2009 ((h1->root.u.def.value
2010 + h1->root.u.def.section->output_section->vma
2011 + h1->root.u.def.section->output_offset)
2012 - pe_data(abfd)->pe_opthdr.DataDirectory[12].VirtualAddress);
2013 }
2014
2015 /* If we couldn't find idata$2, we either have an excessively
2016 trivial program or are in DEEP trouble; we have to assume trivial
2017 program.... */
2018 return true;
2019}