Skip CVS directories when creating tar ball.
[deliverable/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 {
472 a->DataDirectory[idx].VirtualAddress =
473 bfd_h_get_32 (abfd, (bfd_byte *)src->DataDirectory[idx][0]);
474 a->DataDirectory[idx].Size =
475 bfd_h_get_32 (abfd, (bfd_byte *)src->DataDirectory[idx][1]);
476 }
477 }
478
479 if (aouthdr_int->entry)
480 {
481 aouthdr_int->entry += a->ImageBase;
fac41780 482#ifndef COFF_WITH_PEP64
277d1b5e 483 aouthdr_int->entry &= 0xffffffff;
fac41780 484#endif
277d1b5e
ILT
485 }
486 if (aouthdr_int->tsize)
487 {
488 aouthdr_int->text_start += a->ImageBase;
fac41780 489#ifndef COFF_WITH_PEP64
277d1b5e 490 aouthdr_int->text_start &= 0xffffffff;
fac41780 491#endif
277d1b5e 492 }
fac41780
JW
493#ifndef COFF_WITH_PEP64
494 /* PE32+ does not have data_start member! */
277d1b5e
ILT
495 if (aouthdr_int->dsize)
496 {
497 aouthdr_int->data_start += a->ImageBase;
498 aouthdr_int->data_start &= 0xffffffff;
499 }
fac41780 500#endif
277d1b5e
ILT
501
502#ifdef POWERPC_LE_PE
503 /* These three fields are normally set up by ppc_relocate_section.
504 In the case of reading a file in, we can pick them up from the
505 DataDirectory. */
506 first_thunk_address = a->DataDirectory[12].VirtualAddress ;
507 thunk_size = a->DataDirectory[12].Size;
508 import_table_size = a->DataDirectory[1].Size;
509#endif
510
511}
512
5933bdc9
ILT
513/* A support function for below. */
514
515static void
516add_data_entry (abfd, aout, idx, name, base)
277d1b5e
ILT
517 bfd *abfd;
518 struct internal_extra_pe_aouthdr *aout;
519 int idx;
520 char *name;
521 bfd_vma base;
522{
523 asection *sec = bfd_get_section_by_name (abfd, name);
524
525 /* add import directory information if it exists */
526 if ((sec != NULL)
527 && (coff_section_data (abfd, sec) != NULL)
528 && (pei_section_data (abfd, sec) != NULL))
529 {
530 aout->DataDirectory[idx].VirtualAddress = (sec->vma - base) & 0xffffffff;
531 aout->DataDirectory[idx].Size = pei_section_data (abfd, sec)->virt_size;
532 sec->flags |= SEC_DATA;
533 }
534}
535
536unsigned int
537_bfd_pei_swap_aouthdr_out (abfd, in, out)
538 bfd *abfd;
539 PTR in;
540 PTR out;
541{
542 struct internal_aouthdr *aouthdr_in = (struct internal_aouthdr *)in;
543 struct internal_extra_pe_aouthdr *extra = &pe_data (abfd)->pe_opthdr;
544 PEAOUTHDR *aouthdr_out = (PEAOUTHDR *)out;
fac41780
JW
545 bfd_vma sa, fa, ib;
546
547 /* The following definitely is required for EFI applications.
548 Perhaps it's needed for other PEI targets as well, but I don't
549 know that for a fact, so we play it safe here and tweak the
550 alignments only if PEI_FORCE_MINIMUM_ALIGNMENT is
551 defined. --davidm */
552#ifdef PEI_FORCE_MINIMUM_ALIGNMENT
553 if (!extra->FileAlignment)
554 extra->FileAlignment = PE_DEF_FILE_ALIGNMENT;
555 if (!extra->SectionAlignment)
556 extra->SectionAlignment = PE_DEF_SECTION_ALIGNMENT;
557#endif
277d1b5e 558
fac41780
JW
559#ifdef PEI_DEFAULT_TARGET_SUBSYSTEM
560 if (extra->Subsystem == IMAGE_SUBSYSTEM_UNKNOWN)
561 extra->Subsystem = PEI_DEFAULT_TARGET_SUBSYSTEM;
562#endif
563
564 sa = extra->SectionAlignment;
565 fa = extra->FileAlignment;
566 ib = extra->ImageBase;
277d1b5e
ILT
567
568 if (aouthdr_in->tsize)
569 {
570 aouthdr_in->text_start -= ib;
571 aouthdr_in->text_start &= 0xffffffff;
572 }
573 if (aouthdr_in->dsize)
574 {
575 aouthdr_in->data_start -= ib;
576 aouthdr_in->data_start &= 0xffffffff;
577 }
578 if (aouthdr_in->entry)
579 {
580 aouthdr_in->entry -= ib;
581 aouthdr_in->entry &= 0xffffffff;
582 }
583
584#define FA(x) (((x) + fa -1 ) & (- fa))
585#define SA(x) (((x) + sa -1 ) & (- sa))
586
587 /* We like to have the sizes aligned */
588
589 aouthdr_in->bsize = FA (aouthdr_in->bsize);
590
591
592 extra->NumberOfRvaAndSizes = IMAGE_NUMBEROF_DIRECTORY_ENTRIES;
593
594 /* first null out all data directory entries .. */
595 memset (extra->DataDirectory, sizeof (extra->DataDirectory), 0);
596
8181c403 597 add_data_entry (abfd, extra, 0, ".edata", ib);
277d1b5e 598
2fbadf2c
ILT
599 /* Don't call add_data_entry for .idata$2 or .idata$5. It's done in
600 bfd_coff_final_link where all the required information is
601 available. */
277d1b5e 602
2fbadf2c
ILT
603 /* However, until other .idata fixes are made (pending patch), the
604 entry for .idata is needed for backwards compatability. FIXME. */
8181c403 605 add_data_entry (abfd, extra, 1, ".idata" , ib);
277d1b5e 606
8181c403 607 add_data_entry (abfd, extra, 2, ".rsrc" , ib);
2fbadf2c 608
8181c403 609 add_data_entry (abfd, extra, 3, ".pdata", ib);
2fbadf2c
ILT
610
611 /* For some reason, the virtual size (which is what's set by
612 add_data_entry) for .reloc is not the same as the size recorded
613 in this slot by MSVC; it doesn't seem to cause problems (so far),
614 but since it's the best we've got, use it. It does do the right
615 thing for .pdata. */
616 if (pe_data (abfd)->has_reloc_section)
8181c403 617 add_data_entry (abfd, extra, 5, ".reloc", ib);
277d1b5e
ILT
618
619 {
620 asection *sec;
621 bfd_vma dsize= 0;
622 bfd_vma isize = SA(abfd->sections->filepos);
623 bfd_vma tsize= 0;
624
625 for (sec = abfd->sections; sec; sec = sec->next)
626 {
627 int rounded = FA(sec->_raw_size);
628
629 if (sec->flags & SEC_DATA)
630 dsize += rounded;
631 if (sec->flags & SEC_CODE)
632 tsize += rounded;
5933bdc9
ILT
633 /* The image size is the total VIRTUAL size (which is what is
634 in the virt_size field). Files have been seen (from MSVC
635 5.0 link.exe) where the file size of the .data segment is
636 quite small compared to the virtual size. Without this
637 fix, strip munges the file. */
638 isize += SA (FA (pei_section_data (abfd, sec)->virt_size));
277d1b5e
ILT
639 }
640
641 aouthdr_in->dsize = dsize;
642 aouthdr_in->tsize = tsize;
643 extra->SizeOfImage = isize;
644 }
645
646 extra->SizeOfHeaders = abfd->sections->filepos;
647 bfd_h_put_16(abfd, aouthdr_in->magic, (bfd_byte *) aouthdr_out->standard.magic);
648
5933bdc9
ILT
649#define LINKER_VERSION 256 /* That is, 2.56 */
650
651 /* This piece of magic sets the "linker version" field to
652 LINKER_VERSION. */
653 bfd_h_put_16 (abfd,
654 LINKER_VERSION / 100 + (LINKER_VERSION % 100) * 256,
655 (bfd_byte *) aouthdr_out->standard.vstamp);
277d1b5e
ILT
656
657 PUT_AOUTHDR_TSIZE (abfd, aouthdr_in->tsize, (bfd_byte *) aouthdr_out->standard.tsize);
658 PUT_AOUTHDR_DSIZE (abfd, aouthdr_in->dsize, (bfd_byte *) aouthdr_out->standard.dsize);
659 PUT_AOUTHDR_BSIZE (abfd, aouthdr_in->bsize, (bfd_byte *) aouthdr_out->standard.bsize);
660 PUT_AOUTHDR_ENTRY (abfd, aouthdr_in->entry, (bfd_byte *) aouthdr_out->standard.entry);
661 PUT_AOUTHDR_TEXT_START (abfd, aouthdr_in->text_start,
662 (bfd_byte *) aouthdr_out->standard.text_start);
663
fac41780
JW
664#ifndef COFF_WITH_PEP64
665 /* PE32+ does not have data_start member! */
277d1b5e
ILT
666 PUT_AOUTHDR_DATA_START (abfd, aouthdr_in->data_start,
667 (bfd_byte *) aouthdr_out->standard.data_start);
fac41780 668#endif
277d1b5e 669
fac41780
JW
670 PUT_OPTHDR_IMAGE_BASE (abfd, extra->ImageBase,
671 (bfd_byte *) aouthdr_out->ImageBase);
277d1b5e
ILT
672 bfd_h_put_32 (abfd, extra->SectionAlignment,
673 (bfd_byte *) aouthdr_out->SectionAlignment);
674 bfd_h_put_32 (abfd, extra->FileAlignment,
675 (bfd_byte *) aouthdr_out->FileAlignment);
676 bfd_h_put_16 (abfd, extra->MajorOperatingSystemVersion,
677 (bfd_byte *) aouthdr_out->MajorOperatingSystemVersion);
678 bfd_h_put_16 (abfd, extra->MinorOperatingSystemVersion,
679 (bfd_byte *) aouthdr_out->MinorOperatingSystemVersion);
680 bfd_h_put_16 (abfd, extra->MajorImageVersion,
681 (bfd_byte *) aouthdr_out->MajorImageVersion);
682 bfd_h_put_16 (abfd, extra->MinorImageVersion,
683 (bfd_byte *) aouthdr_out->MinorImageVersion);
684 bfd_h_put_16 (abfd, extra->MajorSubsystemVersion,
685 (bfd_byte *) aouthdr_out->MajorSubsystemVersion);
686 bfd_h_put_16 (abfd, extra->MinorSubsystemVersion,
687 (bfd_byte *) aouthdr_out->MinorSubsystemVersion);
688 bfd_h_put_32 (abfd, extra->Reserved1,
689 (bfd_byte *) aouthdr_out->Reserved1);
690 bfd_h_put_32 (abfd, extra->SizeOfImage,
691 (bfd_byte *) aouthdr_out->SizeOfImage);
692 bfd_h_put_32 (abfd, extra->SizeOfHeaders,
693 (bfd_byte *) aouthdr_out->SizeOfHeaders);
694 bfd_h_put_32 (abfd, extra->CheckSum,
695 (bfd_byte *) aouthdr_out->CheckSum);
696 bfd_h_put_16 (abfd, extra->Subsystem,
697 (bfd_byte *) aouthdr_out->Subsystem);
698 bfd_h_put_16 (abfd, extra->DllCharacteristics,
699 (bfd_byte *) aouthdr_out->DllCharacteristics);
fac41780
JW
700 PUT_OPTHDR_SIZE_OF_STACK_RESERVE (abfd, extra->SizeOfStackReserve,
701 (bfd_byte *) aouthdr_out->SizeOfStackReserve);
702 PUT_OPTHDR_SIZE_OF_STACK_COMMIT (abfd, extra->SizeOfStackCommit,
703 (bfd_byte *) aouthdr_out->SizeOfStackCommit);
704 PUT_OPTHDR_SIZE_OF_HEAP_RESERVE (abfd, extra->SizeOfHeapReserve,
705 (bfd_byte *) aouthdr_out->SizeOfHeapReserve);
706 PUT_OPTHDR_SIZE_OF_HEAP_COMMIT (abfd, extra->SizeOfHeapCommit,
707 (bfd_byte *) aouthdr_out->SizeOfHeapCommit);
277d1b5e
ILT
708 bfd_h_put_32 (abfd, extra->LoaderFlags,
709 (bfd_byte *) aouthdr_out->LoaderFlags);
710 bfd_h_put_32 (abfd, extra->NumberOfRvaAndSizes,
711 (bfd_byte *) aouthdr_out->NumberOfRvaAndSizes);
712 {
713 int idx;
714 for (idx=0; idx < 16; idx++)
715 {
716 bfd_h_put_32 (abfd, extra->DataDirectory[idx].VirtualAddress,
717 (bfd_byte *) aouthdr_out->DataDirectory[idx][0]);
718 bfd_h_put_32 (abfd, extra->DataDirectory[idx].Size,
719 (bfd_byte *) aouthdr_out->DataDirectory[idx][1]);
720 }
721 }
722
723 return AOUTSZ;
724}
725
726unsigned int
727_bfd_pei_only_swap_filehdr_out (abfd, in, out)
728 bfd *abfd;
729 PTR in;
730 PTR out;
731{
732 int idx;
733 struct internal_filehdr *filehdr_in = (struct internal_filehdr *)in;
734 struct external_PEI_filehdr *filehdr_out = (struct external_PEI_filehdr *)out;
735
736 if (pe_data (abfd)->has_reloc_section)
737 filehdr_in->f_flags &= ~F_RELFLG;
738
739 if (pe_data (abfd)->dll)
740 filehdr_in->f_flags |= F_DLL;
741
742 filehdr_in->pe.e_magic = DOSMAGIC;
743 filehdr_in->pe.e_cblp = 0x90;
744 filehdr_in->pe.e_cp = 0x3;
745 filehdr_in->pe.e_crlc = 0x0;
746 filehdr_in->pe.e_cparhdr = 0x4;
747 filehdr_in->pe.e_minalloc = 0x0;
748 filehdr_in->pe.e_maxalloc = 0xffff;
749 filehdr_in->pe.e_ss = 0x0;
750 filehdr_in->pe.e_sp = 0xb8;
751 filehdr_in->pe.e_csum = 0x0;
752 filehdr_in->pe.e_ip = 0x0;
753 filehdr_in->pe.e_cs = 0x0;
754 filehdr_in->pe.e_lfarlc = 0x40;
755 filehdr_in->pe.e_ovno = 0x0;
756
757 for (idx=0; idx < 4; idx++)
758 filehdr_in->pe.e_res[idx] = 0x0;
759
760 filehdr_in->pe.e_oemid = 0x0;
761 filehdr_in->pe.e_oeminfo = 0x0;
762
763 for (idx=0; idx < 10; idx++)
764 filehdr_in->pe.e_res2[idx] = 0x0;
765
766 filehdr_in->pe.e_lfanew = 0x80;
767
768 /* this next collection of data are mostly just characters. It appears
769 to be constant within the headers put on NT exes */
770 filehdr_in->pe.dos_message[0] = 0x0eba1f0e;
771 filehdr_in->pe.dos_message[1] = 0xcd09b400;
772 filehdr_in->pe.dos_message[2] = 0x4c01b821;
773 filehdr_in->pe.dos_message[3] = 0x685421cd;
774 filehdr_in->pe.dos_message[4] = 0x70207369;
775 filehdr_in->pe.dos_message[5] = 0x72676f72;
776 filehdr_in->pe.dos_message[6] = 0x63206d61;
777 filehdr_in->pe.dos_message[7] = 0x6f6e6e61;
778 filehdr_in->pe.dos_message[8] = 0x65622074;
779 filehdr_in->pe.dos_message[9] = 0x6e757220;
780 filehdr_in->pe.dos_message[10] = 0x206e6920;
781 filehdr_in->pe.dos_message[11] = 0x20534f44;
782 filehdr_in->pe.dos_message[12] = 0x65646f6d;
783 filehdr_in->pe.dos_message[13] = 0x0a0d0d2e;
784 filehdr_in->pe.dos_message[14] = 0x24;
785 filehdr_in->pe.dos_message[15] = 0x0;
786 filehdr_in->pe.nt_signature = NT_SIGNATURE;
787
788
789
790 bfd_h_put_16(abfd, filehdr_in->f_magic, (bfd_byte *) filehdr_out->f_magic);
791 bfd_h_put_16(abfd, filehdr_in->f_nscns, (bfd_byte *) filehdr_out->f_nscns);
792
793 bfd_h_put_32(abfd, time (0), (bfd_byte *) filehdr_out->f_timdat);
794 PUT_FILEHDR_SYMPTR (abfd, (bfd_vma) filehdr_in->f_symptr,
795 (bfd_byte *) filehdr_out->f_symptr);
796 bfd_h_put_32(abfd, filehdr_in->f_nsyms, (bfd_byte *) filehdr_out->f_nsyms);
797 bfd_h_put_16(abfd, filehdr_in->f_opthdr, (bfd_byte *) filehdr_out->f_opthdr);
798 bfd_h_put_16(abfd, filehdr_in->f_flags, (bfd_byte *) filehdr_out->f_flags);
799
800 /* put in extra dos header stuff. This data remains essentially
801 constant, it just has to be tacked on to the beginning of all exes
802 for NT */
803 bfd_h_put_16(abfd, filehdr_in->pe.e_magic, (bfd_byte *) filehdr_out->e_magic);
804 bfd_h_put_16(abfd, filehdr_in->pe.e_cblp, (bfd_byte *) filehdr_out->e_cblp);
805 bfd_h_put_16(abfd, filehdr_in->pe.e_cp, (bfd_byte *) filehdr_out->e_cp);
806 bfd_h_put_16(abfd, filehdr_in->pe.e_crlc, (bfd_byte *) filehdr_out->e_crlc);
807 bfd_h_put_16(abfd, filehdr_in->pe.e_cparhdr,
808 (bfd_byte *) filehdr_out->e_cparhdr);
809 bfd_h_put_16(abfd, filehdr_in->pe.e_minalloc,
810 (bfd_byte *) filehdr_out->e_minalloc);
811 bfd_h_put_16(abfd, filehdr_in->pe.e_maxalloc,
812 (bfd_byte *) filehdr_out->e_maxalloc);
813 bfd_h_put_16(abfd, filehdr_in->pe.e_ss, (bfd_byte *) filehdr_out->e_ss);
814 bfd_h_put_16(abfd, filehdr_in->pe.e_sp, (bfd_byte *) filehdr_out->e_sp);
815 bfd_h_put_16(abfd, filehdr_in->pe.e_csum, (bfd_byte *) filehdr_out->e_csum);
816 bfd_h_put_16(abfd, filehdr_in->pe.e_ip, (bfd_byte *) filehdr_out->e_ip);
817 bfd_h_put_16(abfd, filehdr_in->pe.e_cs, (bfd_byte *) filehdr_out->e_cs);
818 bfd_h_put_16(abfd, filehdr_in->pe.e_lfarlc, (bfd_byte *) filehdr_out->e_lfarlc);
819 bfd_h_put_16(abfd, filehdr_in->pe.e_ovno, (bfd_byte *) filehdr_out->e_ovno);
820 {
821 int idx;
822 for (idx=0; idx < 4; idx++)
823 bfd_h_put_16(abfd, filehdr_in->pe.e_res[idx],
824 (bfd_byte *) filehdr_out->e_res[idx]);
825 }
826 bfd_h_put_16(abfd, filehdr_in->pe.e_oemid, (bfd_byte *) filehdr_out->e_oemid);
827 bfd_h_put_16(abfd, filehdr_in->pe.e_oeminfo,
828 (bfd_byte *) filehdr_out->e_oeminfo);
829 {
830 int idx;
831 for (idx=0; idx < 10; idx++)
832 bfd_h_put_16(abfd, filehdr_in->pe.e_res2[idx],
833 (bfd_byte *) filehdr_out->e_res2[idx]);
834 }
835 bfd_h_put_32(abfd, filehdr_in->pe.e_lfanew, (bfd_byte *) filehdr_out->e_lfanew);
836
837 {
838 int idx;
839 for (idx=0; idx < 16; idx++)
840 bfd_h_put_32(abfd, filehdr_in->pe.dos_message[idx],
841 (bfd_byte *) filehdr_out->dos_message[idx]);
842 }
843
844 /* also put in the NT signature */
845 bfd_h_put_32(abfd, filehdr_in->pe.nt_signature,
846 (bfd_byte *) filehdr_out->nt_signature);
847
848
849
850
851 return FILHSZ;
852}
853
854unsigned int
855_bfd_pe_only_swap_filehdr_out (abfd, in, out)
856 bfd *abfd;
857 PTR in;
858 PTR out;
859{
860 struct internal_filehdr *filehdr_in = (struct internal_filehdr *)in;
861 FILHDR *filehdr_out = (FILHDR *)out;
862
863 bfd_h_put_16(abfd, filehdr_in->f_magic, (bfd_byte *) filehdr_out->f_magic);
864 bfd_h_put_16(abfd, filehdr_in->f_nscns, (bfd_byte *) filehdr_out->f_nscns);
865 bfd_h_put_32(abfd, filehdr_in->f_timdat, (bfd_byte *) filehdr_out->f_timdat);
866 PUT_FILEHDR_SYMPTR (abfd, (bfd_vma) filehdr_in->f_symptr,
867 (bfd_byte *) filehdr_out->f_symptr);
868 bfd_h_put_32(abfd, filehdr_in->f_nsyms, (bfd_byte *) filehdr_out->f_nsyms);
869 bfd_h_put_16(abfd, filehdr_in->f_opthdr, (bfd_byte *) filehdr_out->f_opthdr);
870 bfd_h_put_16(abfd, filehdr_in->f_flags, (bfd_byte *) filehdr_out->f_flags);
871
872 return FILHSZ;
873}
874
875unsigned int
876_bfd_pei_swap_scnhdr_out (abfd, in, out)
877 bfd *abfd;
878 PTR in;
879 PTR out;
880{
881 struct internal_scnhdr *scnhdr_int = (struct internal_scnhdr *)in;
882 SCNHDR *scnhdr_ext = (SCNHDR *)out;
883 unsigned int ret = SCNHSZ;
884 bfd_vma ps;
885 bfd_vma ss;
886
887 memcpy(scnhdr_ext->s_name, scnhdr_int->s_name, sizeof(scnhdr_int->s_name));
888
889 PUT_SCNHDR_VADDR (abfd,
890 ((scnhdr_int->s_vaddr
891 - pe_data(abfd)->pe_opthdr.ImageBase)
892 & 0xffffffff),
893 (bfd_byte *) scnhdr_ext->s_vaddr);
894
5933bdc9
ILT
895 /* NT wants the size data to be rounded up to the next
896 NT_FILE_ALIGNMENT, but zero if it has no content (as in .bss,
897 sometimes). */
277d1b5e 898
5933bdc9 899 if ((scnhdr_int->s_flags & IMAGE_SCN_CNT_UNINITIALIZED_DATA) != 0)
277d1b5e
ILT
900 {
901 ps = scnhdr_int->s_size;
902 ss = 0;
903 }
904 else
905 {
906 ps = scnhdr_int->s_paddr;
907 ss = scnhdr_int->s_size;
908 }
909
910 PUT_SCNHDR_SIZE (abfd, ss,
911 (bfd_byte *) scnhdr_ext->s_size);
912
913
5933bdc9 914 /* s_paddr in PE is really the virtual size. */
277d1b5e
ILT
915 PUT_SCNHDR_PADDR (abfd, ps, (bfd_byte *) scnhdr_ext->s_paddr);
916
917 PUT_SCNHDR_SCNPTR (abfd, scnhdr_int->s_scnptr,
918 (bfd_byte *) scnhdr_ext->s_scnptr);
919 PUT_SCNHDR_RELPTR (abfd, scnhdr_int->s_relptr,
920 (bfd_byte *) scnhdr_ext->s_relptr);
921 PUT_SCNHDR_LNNOPTR (abfd, scnhdr_int->s_lnnoptr,
922 (bfd_byte *) scnhdr_ext->s_lnnoptr);
923
924 /* Extra flags must be set when dealing with NT. All sections should also
925 have the IMAGE_SCN_MEM_READ (0x40000000) flag set. In addition, the
926 .text section must have IMAGE_SCN_MEM_EXECUTE (0x20000000) and the data
927 sections (.idata, .data, .bss, .CRT) must have IMAGE_SCN_MEM_WRITE set
928 (this is especially important when dealing with the .idata section since
929 the addresses for routines from .dlls must be overwritten). If .reloc
930 section data is ever generated, we must add IMAGE_SCN_MEM_DISCARDABLE
931 (0x02000000). Also, the resource data should also be read and
932 writable. */
933
934 /* FIXME: alignment is also encoded in this field, at least on ppc (krk) */
935 /* FIXME: even worse, I don't see how to get the original alignment field*/
936 /* back... */
937
277d1b5e
ILT
938 {
939 int flags = scnhdr_int->s_flags;
277d1b5e
ILT
940 bfd_h_put_32(abfd, flags, (bfd_byte *) scnhdr_ext->s_flags);
941 }
942
cb43721d
ILT
943 if (coff_data (abfd)->link_info
944 && ! coff_data (abfd)->link_info->relocateable
945 && ! coff_data (abfd)->link_info->shared
946 && strcmp (scnhdr_int->s_name, ".text") == 0)
277d1b5e 947 {
cb43721d
ILT
948 /* By inference from looking at MS output, the 32 bit field
949 which is the combintion of the number_of_relocs and
950 number_of_linenos is used for the line number count in
951 executables. A 16-bit field won't do for cc1. The MS
952 document says that the number of relocs is zero for
953 executables, but the 17-th bit has been observed to be there.
954 Overflow is not an issue: a 4G-line program will overflow a
955 bunch of other fields long before this! */
956 bfd_h_put_16 (abfd, scnhdr_int->s_nlnno & 0xffff,
957 (bfd_byte *) scnhdr_ext->s_nlnno);
958 bfd_h_put_16 (abfd, scnhdr_int->s_nlnno >> 16,
959 (bfd_byte *) scnhdr_ext->s_nreloc);
277d1b5e 960 }
277d1b5e
ILT
961 else
962 {
cb43721d
ILT
963 if (scnhdr_int->s_nlnno <= 0xffff)
964 bfd_h_put_16 (abfd, scnhdr_int->s_nlnno,
965 (bfd_byte *) scnhdr_ext->s_nlnno);
966 else
967 {
968 (*_bfd_error_handler) (_("%s: line number overflow: 0x%lx > 0xffff"),
969 bfd_get_filename (abfd),
970 scnhdr_int->s_nlnno);
971 bfd_set_error (bfd_error_file_truncated);
972 bfd_h_put_16 (abfd, 0xffff, (bfd_byte *) scnhdr_ext->s_nlnno);
973 ret = 0;
974 }
975 if (scnhdr_int->s_nreloc <= 0xffff)
976 bfd_h_put_16 (abfd, scnhdr_int->s_nreloc,
977 (bfd_byte *) scnhdr_ext->s_nreloc);
978 else
979 {
980 (*_bfd_error_handler) (_("%s: reloc overflow: 0x%lx > 0xffff"),
981 bfd_get_filename (abfd),
982 scnhdr_int->s_nreloc);
983 bfd_set_error (bfd_error_file_truncated);
984 bfd_h_put_16 (abfd, 0xffff, (bfd_byte *) scnhdr_ext->s_nreloc);
985 ret = 0;
986 }
277d1b5e
ILT
987 }
988 return ret;
989}
990
991static char * dir_names[IMAGE_NUMBEROF_DIRECTORY_ENTRIES] =
992{
993 N_ ("Export Directory [.edata (or where ever we found it)]"),
994 N_ ("Import Directory [parts of .idata]"),
995 N_ ("Resource Directory [.rsrc]"),
996 N_ ("Exception Directory [.pdata]"),
997 N_ ("Security Directory"),
998 N_ ("Base Relocation Directory [.reloc]"),
999 N_ ("Debug Directory"),
1000 N_ ("Description Directory"),
1001 N_ ("Special Directory"),
1002 N_ ("Thread Storage Directory [.tls]"),
1003 N_ ("Load Configuration Directory"),
1004 N_ ("Bound Import Directory"),
1005 N_ ("Import Address Table Directory"),
8181c403 1006 N_ ("Delay Import Directory"),
277d1b5e
ILT
1007 N_ ("Reserved"),
1008 N_ ("Reserved")
1009};
1010
1011/**********************************************************************/
1012#ifdef POWERPC_LE_PE
1013/* The code for the PPC really falls in the "architecture dependent"
1014 category. However, it's not clear that anyone will ever care, so
1015 we're ignoring the issue for now; if/when PPC matters, some of this
1016 may need to go into peicode.h, or arguments passed to enable the
1017 PPC- specific code. */
1018#endif
1019
1020/**********************************************************************/
1021static boolean
5933bdc9 1022pe_print_idata (abfd, vfile)
277d1b5e
ILT
1023 bfd *abfd;
1024 PTR vfile;
1025{
1026 FILE *file = (FILE *) vfile;
a76b448c 1027 bfd_byte *data;
8181c403
AM
1028 asection *section;
1029 bfd_signed_vma adj;
277d1b5e
ILT
1030
1031#ifdef POWERPC_LE_PE
1032 asection *rel_section = bfd_get_section_by_name (abfd, ".reldata");
1033#endif
1034
a76b448c 1035 bfd_size_type datasize = 0;
277d1b5e 1036 bfd_size_type dataoff;
277d1b5e 1037 bfd_size_type i;
277d1b5e
ILT
1038 int onaline = 20;
1039
1040 pe_data_type *pe = pe_data (abfd);
1041 struct internal_extra_pe_aouthdr *extra = &pe->pe_opthdr;
1042
8181c403 1043 bfd_vma addr;
277d1b5e 1044
8181c403 1045 addr = extra->DataDirectory[1].VirtualAddress;
277d1b5e 1046
a76b448c 1047 if (addr == 0 && extra->DataDirectory[1].Size == 0)
8181c403 1048 {
a76b448c
AM
1049 /* Maybe the extra header isn't there. Look for the section. */
1050 section = bfd_get_section_by_name (abfd, ".idata");
1051 if (section == NULL)
1052 return true;
1053
1054 addr = section->vma;
1055 datasize = bfd_section_size (abfd, section);
1056 if (datasize == 0)
1057 return true;
8181c403 1058 }
a76b448c 1059 else
8181c403 1060 {
a76b448c
AM
1061 addr += extra->ImageBase;
1062 for (section = abfd->sections; section != NULL; section = section->next)
1063 {
1064 datasize = bfd_section_size (abfd, section);
1065 if (addr >= section->vma && addr < section->vma + datasize)
1066 break;
1067 }
1068
1069 if (section == NULL)
1070 {
1071 fprintf (file,
1072 _("\nThere is an import table, but the section containing it could not be found\n"));
1073 return true;
1074 }
8181c403 1075 }
5933bdc9 1076
8181c403
AM
1077 fprintf (file, _("\nThere is an import table in %s at 0x%lx\n"),
1078 section->name, (unsigned long) addr);
277d1b5e 1079
8181c403 1080 dataoff = addr - section->vma;
a76b448c 1081 datasize -= dataoff;
277d1b5e
ILT
1082
1083#ifdef POWERPC_LE_PE
1084 if (rel_section != 0 && bfd_section_size (abfd, rel_section) != 0)
1085 {
1086 /* The toc address can be found by taking the starting address,
1087 which on the PPC locates a function descriptor. The
1088 descriptor consists of the function code starting address
1089 followed by the address of the toc. The starting address we
1090 get from the bfd, and the descriptor is supposed to be in the
1091 .reldata section. */
1092
1093 bfd_vma loadable_toc_address;
1094 bfd_vma toc_address;
1095 bfd_vma start_address;
1096 bfd_byte *data = 0;
1097 int offset;
8181c403 1098
277d1b5e
ILT
1099 data = (bfd_byte *) bfd_malloc ((size_t) bfd_section_size (abfd,
1100 rel_section));
1101 if (data == NULL && bfd_section_size (abfd, rel_section) != 0)
1102 return false;
1103
277d1b5e
ILT
1104 bfd_get_section_contents (abfd,
1105 rel_section,
1106 (PTR) data, 0,
1107 bfd_section_size (abfd, rel_section));
1108
1109 offset = abfd->start_address - rel_section->vma;
1110
1111 start_address = bfd_get_32(abfd, data+offset);
1112 loadable_toc_address = bfd_get_32(abfd, data+offset+4);
1113 toc_address = loadable_toc_address - 32768;
1114
1115 fprintf(file,
1116 _("\nFunction descriptor located at the start address: %04lx\n"),
1117 (unsigned long int) (abfd->start_address));
1118 fprintf (file,
1119 _("\tcode-base %08lx toc (loadable/actual) %08lx/%08lx\n"),
1120 start_address, loadable_toc_address, toc_address);
1121 }
1122 else
1123 {
1124 fprintf(file,
1125 _("\nNo reldata section! Function descriptor not decoded.\n"));
1126 }
1127#endif
1128
1129 fprintf(file,
8181c403
AM
1130 _("\nThe Import Tables (interpreted %s section contents)\n"),
1131 section->name);
277d1b5e
ILT
1132 fprintf(file,
1133 _(" vma: Hint Time Forward DLL First\n"));
1134 fprintf(file,
1135 _(" Table Stamp Chain Name Thunk\n"));
1136
a76b448c
AM
1137 data = (bfd_byte *) bfd_malloc (datasize);
1138 if (data == NULL)
277d1b5e
ILT
1139 return false;
1140
a76b448c 1141 if (! bfd_get_section_contents (abfd, section, (PTR) data, dataoff, datasize))
277d1b5e
ILT
1142 return false;
1143
a76b448c 1144 adj = section->vma - extra->ImageBase + dataoff;
277d1b5e 1145
5933bdc9 1146 for (i = 0; i < datasize; i += onaline)
277d1b5e
ILT
1147 {
1148 bfd_vma hint_addr;
1149 bfd_vma time_stamp;
1150 bfd_vma forward_chain;
1151 bfd_vma dll_name;
1152 bfd_vma first_thunk;
1153 int idx = 0;
1154 bfd_size_type j;
1155 char *dll;
1156
8181c403 1157 /* print (i + extra->DataDirectory[1].VirtualAddress) */
a76b448c 1158 fprintf (file, " %08lx\t", (unsigned long) (i + adj));
8181c403 1159
5933bdc9 1160 if (i + 20 > datasize)
277d1b5e
ILT
1161 {
1162 /* check stuff */
1163 ;
1164 }
5933bdc9 1165
a76b448c
AM
1166 hint_addr = bfd_get_32 (abfd, data + i);
1167 time_stamp = bfd_get_32 (abfd, data + i + 4);
1168 forward_chain = bfd_get_32 (abfd, data + i + 8);
1169 dll_name = bfd_get_32 (abfd, data + i + 12);
1170 first_thunk = bfd_get_32 (abfd, data + i + 16);
5933bdc9
ILT
1171
1172 fprintf (file, "%08lx %08lx %08lx %08lx %08lx\n",
a76b448c
AM
1173 (unsigned long) hint_addr,
1174 (unsigned long) time_stamp,
1175 (unsigned long) forward_chain,
1176 (unsigned long) dll_name,
1177 (unsigned long) first_thunk);
277d1b5e
ILT
1178
1179 if (hint_addr == 0 && first_thunk == 0)
1180 break;
1181
8181c403 1182 dll = (char *) data + dll_name - adj;
277d1b5e
ILT
1183 fprintf(file, _("\n\tDLL Name: %s\n"), dll);
1184
1185 if (hint_addr != 0)
1186 {
1187 fprintf (file, _("\tvma: Hint/Ord Member-Name\n"));
1188
8181c403 1189 idx = hint_addr - adj;
277d1b5e 1190
5933bdc9 1191 for (j = 0; j < datasize; j += 4)
277d1b5e
ILT
1192 {
1193 unsigned long member = bfd_get_32 (abfd, data + idx + j);
1194
1195 if (member == 0)
1196 break;
1197 if (member & 0x80000000)
1198 fprintf (file, "\t%04lx\t %4lu", member,
1199 member & 0x7fffffff);
1200 else
1201 {
1202 int ordinal;
1203 char *member_name;
1204
8181c403
AM
1205 ordinal = bfd_get_16 (abfd, data + member - adj);
1206 member_name = (char *) data + member - adj + 2;
277d1b5e
ILT
1207 fprintf (file, "\t%04lx\t %4d %s",
1208 member, ordinal, member_name);
1209 }
1210
1211 /* If the time stamp is not zero, the import address
1212 table holds actual addresses. */
1213 if (time_stamp != 0
1214 && first_thunk != 0
1215 && first_thunk != hint_addr)
1216 fprintf (file, "\t%04lx",
a76b448c 1217 (long) bfd_get_32 (abfd, data + first_thunk - adj + j));
277d1b5e
ILT
1218
1219 fprintf (file, "\n");
1220 }
1221 }
1222
1223 if (hint_addr != first_thunk && time_stamp == 0)
1224 {
1225 int differ = 0;
1226 int idx2;
1227
8181c403 1228 idx2 = first_thunk - adj;
277d1b5e 1229
5933bdc9 1230 for (j = 0; j < datasize; j += 4)
277d1b5e
ILT
1231 {
1232 int ordinal;
1233 char *member_name;
1234 bfd_vma hint_member = 0;
1235 bfd_vma iat_member;
1236
1237 if (hint_addr != 0)
1238 hint_member = bfd_get_32 (abfd, data + idx + j);
1239 iat_member = bfd_get_32 (abfd, data + idx2 + j);
1240
1241 if (hint_addr == 0 && iat_member == 0)
1242 break;
1243
1244 if (hint_addr == 0 || hint_member != iat_member)
1245 {
1246 if (differ == 0)
1247 {
1248 fprintf (file,
1249 _("\tThe Import Address Table (difference found)\n"));
1250 fprintf(file, _("\tvma: Hint/Ord Member-Name\n"));
1251 differ = 1;
1252 }
1253 if (iat_member == 0)
1254 {
1255 fprintf(file,
1256 _("\t>>> Ran out of IAT members!\n"));
1257 }
1258 else
1259 {
1260 ordinal = bfd_get_16(abfd,
8181c403
AM
1261 data + iat_member - adj);
1262 member_name = (char *) data + iat_member - adj + 2;
277d1b5e 1263 fprintf(file, "\t%04lx\t %4d %s\n",
a76b448c
AM
1264 (unsigned long) iat_member,
1265 ordinal,
1266 member_name);
277d1b5e
ILT
1267 }
1268 }
1269
1270 if (hint_addr != 0 && hint_member == 0)
1271 break;
1272 }
1273 if (differ == 0)
1274 {
1275 fprintf(file,
1276 _("\tThe Import Address Table is identical\n"));
1277 }
1278 }
1279
1280 fprintf(file, "\n");
1281
1282 }
1283
1284 free (data);
1285
1286 return true;
1287}
1288
1289static boolean
1290pe_print_edata (abfd, vfile)
1291 bfd *abfd;
1292 PTR vfile;
1293{
1294 FILE *file = (FILE *) vfile;
a76b448c 1295 bfd_byte *data;
8181c403 1296 asection *section;
277d1b5e 1297
a76b448c 1298 bfd_size_type datasize = 0;
277d1b5e
ILT
1299 bfd_size_type dataoff;
1300 bfd_size_type i;
1301
8181c403 1302 bfd_signed_vma adj;
277d1b5e
ILT
1303 struct EDT_type
1304 {
1305 long export_flags; /* reserved - should be zero */
1306 long time_stamp;
1307 short major_ver;
1308 short minor_ver;
1309 bfd_vma name; /* rva - relative to image base */
1310 long base; /* ordinal base */
1311 unsigned long num_functions; /* Number in the export address table */
1312 unsigned long num_names; /* Number in the name pointer table */
1313 bfd_vma eat_addr; /* rva to the export address table */
1314 bfd_vma npt_addr; /* rva to the Export Name Pointer Table */
1315 bfd_vma ot_addr; /* rva to the Ordinal Table */
1316 } edt;
1317
1318 pe_data_type *pe = pe_data (abfd);
1319 struct internal_extra_pe_aouthdr *extra = &pe->pe_opthdr;
1320
8181c403 1321 bfd_vma addr;
277d1b5e 1322
8181c403 1323 addr = extra->DataDirectory[0].VirtualAddress;
277d1b5e 1324
a76b448c 1325 if (addr == 0 && extra->DataDirectory[0].Size == 0)
8181c403 1326 {
a76b448c
AM
1327 /* Maybe the extra header isn't there. Look for the section. */
1328 section = bfd_get_section_by_name (abfd, ".edata");
1329 if (section == NULL)
1330 return true;
1331
1332 addr = section->vma;
1333 datasize = bfd_section_size (abfd, section);
1334 if (datasize == 0)
1335 return true;
8181c403 1336 }
a76b448c 1337 else
8181c403 1338 {
a76b448c
AM
1339 addr += extra->ImageBase;
1340 for (section = abfd->sections; section != NULL; section = section->next)
1341 {
1342 datasize = bfd_section_size (abfd, section);
1343 if (addr >= section->vma && addr < section->vma + datasize)
1344 break;
1345 }
1346
1347 if (section == NULL)
1348 {
1349 fprintf (file,
1350 _("\nThere is an export table, but the section containing it could not be found\n"));
1351 return true;
1352 }
277d1b5e
ILT
1353 }
1354
8181c403
AM
1355 fprintf (file, _("\nThere is an export table in %s at 0x%lx\n"),
1356 section->name, (unsigned long) addr);
1357
1358 dataoff = addr - section->vma;
a76b448c 1359 datasize -= dataoff;
8181c403 1360
277d1b5e 1361 data = (bfd_byte *) bfd_malloc (datasize);
8181c403 1362 if (data == NULL)
277d1b5e
ILT
1363 return false;
1364
1365 if (! bfd_get_section_contents (abfd, section, (PTR) data, dataoff,
1366 datasize))
1367 return false;
1368
1369 /* Go get Export Directory Table */
1370 edt.export_flags = bfd_get_32(abfd, data+0);
1371 edt.time_stamp = bfd_get_32(abfd, data+4);
1372 edt.major_ver = bfd_get_16(abfd, data+8);
1373 edt.minor_ver = bfd_get_16(abfd, data+10);
1374 edt.name = bfd_get_32(abfd, data+12);
1375 edt.base = bfd_get_32(abfd, data+16);
1376 edt.num_functions = bfd_get_32(abfd, data+20);
1377 edt.num_names = bfd_get_32(abfd, data+24);
1378 edt.eat_addr = bfd_get_32(abfd, data+28);
1379 edt.npt_addr = bfd_get_32(abfd, data+32);
1380 edt.ot_addr = bfd_get_32(abfd, data+36);
1381
8181c403 1382 adj = section->vma - extra->ImageBase + dataoff;
277d1b5e
ILT
1383
1384 /* Dump the EDT first first */
1385 fprintf(file,
8181c403
AM
1386 _("\nThe Export Tables (interpreted %s section contents)\n\n"),
1387 section->name);
277d1b5e
ILT
1388
1389 fprintf(file,
1390 _("Export Flags \t\t\t%lx\n"), (unsigned long) edt.export_flags);
1391
1392 fprintf(file,
1393 _("Time/Date stamp \t\t%lx\n"), (unsigned long) edt.time_stamp);
1394
1395 fprintf(file,
1396 _("Major/Minor \t\t\t%d/%d\n"), edt.major_ver, edt.minor_ver);
1397
1398 fprintf (file,
1399 _("Name \t\t\t\t"));
1400 fprintf_vma (file, edt.name);
1401 fprintf (file,
8181c403 1402 " %s\n", data + edt.name - adj);
277d1b5e
ILT
1403
1404 fprintf(file,
1405 _("Ordinal Base \t\t\t%ld\n"), edt.base);
1406
1407 fprintf(file,
1408 _("Number in:\n"));
1409
1410 fprintf(file,
8181c403 1411 _("\tExport Address Table \t\t%08lx\n"),
277d1b5e
ILT
1412 edt.num_functions);
1413
1414 fprintf(file,
8181c403 1415 _("\t[Name Pointer/Ordinal] Table\t%08lx\n"), edt.num_names);
277d1b5e
ILT
1416
1417 fprintf(file,
1418 _("Table Addresses\n"));
1419
1420 fprintf (file,
1421 _("\tExport Address Table \t\t"));
1422 fprintf_vma (file, edt.eat_addr);
1423 fprintf (file, "\n");
1424
1425 fprintf (file,
1426 _("\tName Pointer Table \t\t"));
1427 fprintf_vma (file, edt.npt_addr);
1428 fprintf (file, "\n");
1429
1430 fprintf (file,
1431 _("\tOrdinal Table \t\t\t"));
1432 fprintf_vma (file, edt.ot_addr);
1433 fprintf (file, "\n");
1434
1435
5933bdc9 1436 /* The next table to find is the Export Address Table. It's basically
277d1b5e
ILT
1437 a list of pointers that either locate a function in this dll, or
1438 forward the call to another dll. Something like:
1439 typedef union
1440 {
1441 long export_rva;
1442 long forwarder_rva;
1443 } export_address_table_entry;
1444 */
1445
1446 fprintf(file,
1447 _("\nExport Address Table -- Ordinal Base %ld\n"),
1448 edt.base);
1449
1450 for (i = 0; i < edt.num_functions; ++i)
1451 {
1452 bfd_vma eat_member = bfd_get_32 (abfd,
8181c403 1453 data + edt.eat_addr + (i * 4) - adj);
5933bdc9
ILT
1454 bfd_vma eat_actual = eat_member;
1455 bfd_vma edata_start = bfd_get_section_vma (abfd, section);
277d1b5e
ILT
1456 bfd_vma edata_end = edata_start + datasize;
1457
1458 if (eat_member == 0)
1459 continue;
1460
1461 if (edata_start < eat_actual && eat_actual < edata_end)
1462 {
1463 /* this rva is to a name (forwarding function) in our section */
1464 /* Should locate a function descriptor */
5933bdc9
ILT
1465 fprintf (file,
1466 "\t[%4ld] +base[%4ld] %04lx %s -- %s\n",
a76b448c
AM
1467 (long) i,
1468 (long) (i + edt.base),
1469 (unsigned long) eat_member,
1470 _("Forwarder RVA"),
1471 data + eat_member - adj);
277d1b5e
ILT
1472 }
1473 else
1474 {
1475 /* Should locate a function descriptor in the reldata section */
5933bdc9
ILT
1476 fprintf (file,
1477 "\t[%4ld] +base[%4ld] %04lx %s\n",
a76b448c
AM
1478 (long) i,
1479 (long) (i + edt.base),
1480 (unsigned long) eat_member,
5933bdc9 1481 _("Export RVA"));
277d1b5e
ILT
1482 }
1483 }
1484
1485 /* The Export Name Pointer Table is paired with the Export Ordinal Table */
1486 /* Dump them in parallel for clarity */
1487 fprintf(file,
1488 _("\n[Ordinal/Name Pointer] Table\n"));
1489
1490 for (i = 0; i < edt.num_names; ++i)
1491 {
1492 bfd_vma name_ptr = bfd_get_32(abfd,
1493 data +
1494 edt.npt_addr
8181c403 1495 + (i*4) - adj);
277d1b5e 1496
8181c403 1497 char *name = (char *) data + name_ptr - adj;
277d1b5e
ILT
1498
1499 bfd_vma ord = bfd_get_16(abfd,
1500 data +
1501 edt.ot_addr
8181c403 1502 + (i*2) - adj);
277d1b5e
ILT
1503 fprintf(file,
1504 "\t[%4ld] %s\n", (long) ord, name);
1505
1506 }
1507
1508 free (data);
1509
1510 return true;
1511}
1512
fac41780
JW
1513/* This really is architecture dependent. On IA-64, a .pdata entry
1514 consists of three dwords containing relative virtual addresses that
1515 specify the start and end address of the code range the entry
1516 covers and the address of the corresponding unwind info data. */
277d1b5e
ILT
1517static boolean
1518pe_print_pdata (abfd, vfile)
1519 bfd *abfd;
1520 PTR vfile;
1521{
fac41780
JW
1522#ifdef COFF_WITH_PEP64
1523# define PDATA_ROW_SIZE (3*8)
1524#else
1525# define PDATA_ROW_SIZE (5*4)
1526#endif
277d1b5e
ILT
1527 FILE *file = (FILE *) vfile;
1528 bfd_byte *data = 0;
1529 asection *section = bfd_get_section_by_name (abfd, ".pdata");
1530 bfd_size_type datasize = 0;
1531 bfd_size_type i;
1532 bfd_size_type start, stop;
fac41780 1533 int onaline = PDATA_ROW_SIZE;
277d1b5e 1534
5933bdc9
ILT
1535 if (section == NULL
1536 || coff_section_data (abfd, section) == NULL
1537 || pei_section_data (abfd, section) == NULL)
1538 return true;
277d1b5e 1539
5933bdc9 1540 stop = pei_section_data (abfd, section)->virt_size;
277d1b5e
ILT
1541 if ((stop % onaline) != 0)
1542 fprintf (file, _("Warning, .pdata section size (%ld) is not a multiple of %d\n"),
1543 (long)stop, onaline);
1544
5933bdc9
ILT
1545 fprintf (file,
1546 _("\nThe Function Table (interpreted .pdata section contents)\n"));
fac41780
JW
1547#ifdef COFF_WITH_PEP64
1548 fprintf(file,
1549 _(" vma:\t\t\tBegin Address End Address Unwind Info\n"));
1550#else
5933bdc9
ILT
1551 fprintf (file,
1552 _(" vma:\t\tBegin End EH EH PrologEnd Exception\n"));
1553 fprintf (file,
1554 _(" \t\tAddress Address Handler Data Address Mask\n"));
fac41780 1555#endif
277d1b5e
ILT
1556
1557 if (bfd_section_size (abfd, section) == 0)
1558 return true;
1559
1560 data = (bfd_byte *) bfd_malloc ((size_t) bfd_section_size (abfd, section));
1561 datasize = bfd_section_size (abfd, section);
1562 if (data == NULL && datasize != 0)
1563 return false;
1564
1565 bfd_get_section_contents (abfd,
1566 section,
1567 (PTR) data, 0,
1568 bfd_section_size (abfd, section));
1569
1570 start = 0;
1571
1572 for (i = start; i < stop; i += onaline)
1573 {
1574 bfd_vma begin_addr;
1575 bfd_vma end_addr;
1576 bfd_vma eh_handler;
1577 bfd_vma eh_data;
1578 bfd_vma prolog_end_addr;
5933bdc9 1579 int em_data;
277d1b5e 1580
fac41780 1581 if (i + PDATA_ROW_SIZE > stop)
277d1b5e 1582 break;
5933bdc9 1583
fac41780
JW
1584 begin_addr = GET_PDATA_ENTRY(abfd, data+i);
1585 end_addr = GET_PDATA_ENTRY(abfd, data+i+4);
1586 eh_handler = GET_PDATA_ENTRY(abfd, data+i+8);
1587 eh_data = GET_PDATA_ENTRY(abfd, data+i+12);
1588 prolog_end_addr = GET_PDATA_ENTRY(abfd, data+i+16);
277d1b5e
ILT
1589
1590 if (begin_addr == 0 && end_addr == 0 && eh_handler == 0
1591 && eh_data == 0 && prolog_end_addr == 0)
1592 {
1593 /* We are probably into the padding of the section now. */
1594 break;
1595 }
1596
5933bdc9 1597 em_data = ((eh_handler & 0x1) << 2) | (prolog_end_addr & 0x3);
fac41780
JW
1598 eh_handler &= ~ (bfd_vma) 0x3;
1599 prolog_end_addr &= ~ (bfd_vma) 0x3;
1600
1601 fputc (' ', file);
1602 fprintf_vma (file, i + section->vma); fputc ('\t', file);
1603 fprintf_vma (file, begin_addr); fputc (' ', file);
1604 fprintf_vma (file, end_addr); fputc (' ', file);
1605 fprintf_vma (file, eh_handler);
1606#ifndef COFF_WITH_PEP64
1607 fputc (' ', file);
1608 fprintf_vma (file, eh_data); fputc (' ', file);
1609 fprintf_vma (file, prolog_end_addr);
1610 fprintf (file, " %x", em_data);
1611#endif
277d1b5e
ILT
1612
1613#ifdef POWERPC_LE_PE
1614 if (eh_handler == 0 && eh_data != 0)
1615 {
1616 /* Special bits here, although the meaning may */
1617 /* be a little mysterious. The only one I know */
1618 /* for sure is 0x03. */
1619 /* Code Significance */
1620 /* 0x00 None */
1621 /* 0x01 Register Save Millicode */
1622 /* 0x02 Register Restore Millicode */
1623 /* 0x03 Glue Code Sequence */
1624 switch (eh_data)
1625 {
1626 case 0x01:
1627 fprintf(file, _(" Register save millicode"));
1628 break;
1629 case 0x02:
1630 fprintf(file, _(" Register restore millicode"));
1631 break;
1632 case 0x03:
1633 fprintf(file, _(" Glue code sequence"));
1634 break;
1635 default:
1636 break;
1637 }
1638 }
1639#endif
1640 fprintf(file, "\n");
1641 }
1642
1643 free (data);
1644
1645 return true;
1646}
1647
5933bdc9
ILT
1648#define IMAGE_REL_BASED_HIGHADJ 4
1649static const char * const tbl[] =
277d1b5e
ILT
1650{
1651"ABSOLUTE",
1652"HIGH",
1653"LOW",
1654"HIGHLOW",
1655"HIGHADJ",
5933bdc9 1656"MIPS_JMPADDR",
fac41780
JW
1657"SECTION",
1658"REL32",
1659"RESERVED1",
1660"MIPS_JMPADDR16",
1661"DIR64",
1662"HIGH3ADJ"
5933bdc9 1663"UNKNOWN", /* MUST be last */
277d1b5e
ILT
1664};
1665
1666static boolean
1667pe_print_reloc (abfd, vfile)
1668 bfd *abfd;
1669 PTR vfile;
1670{
1671 FILE *file = (FILE *) vfile;
1672 bfd_byte *data = 0;
1673 asection *section = bfd_get_section_by_name (abfd, ".reloc");
1674 bfd_size_type datasize = 0;
1675 bfd_size_type i;
1676 bfd_size_type start, stop;
1677
5933bdc9 1678 if (section == NULL)
277d1b5e
ILT
1679 return true;
1680
1681 if (bfd_section_size (abfd, section) == 0)
1682 return true;
1683
5933bdc9
ILT
1684 fprintf (file,
1685 _("\n\nPE File Base Relocations (interpreted .reloc section contents)\n"));
277d1b5e
ILT
1686
1687 data = (bfd_byte *) bfd_malloc ((size_t) bfd_section_size (abfd, section));
1688 datasize = bfd_section_size (abfd, section);
1689 if (data == NULL && datasize != 0)
1690 return false;
1691
1692 bfd_get_section_contents (abfd,
1693 section,
1694 (PTR) data, 0,
1695 bfd_section_size (abfd, section));
1696
1697 start = 0;
1698
1699 stop = bfd_section_size (abfd, section);
1700
1701 for (i = start; i < stop;)
1702 {
1703 int j;
1704 bfd_vma virtual_address;
1705 long number, size;
1706
1707 /* The .reloc section is a sequence of blocks, with a header consisting
1708 of two 32 bit quantities, followed by a number of 16 bit entries */
1709
1710 virtual_address = bfd_get_32(abfd, data+i);
1711 size = bfd_get_32(abfd, data+i+4);
1712 number = (size - 8) / 2;
1713
1714 if (size == 0)
1715 {
1716 break;
1717 }
1718
1719 fprintf (file,
1720 _("\nVirtual Address: %08lx Chunk size %ld (0x%lx) Number of fixups %ld\n"),
a76b448c 1721 (unsigned long) virtual_address, size, size, number);
277d1b5e
ILT
1722
1723 for (j = 0; j < number; ++j)
1724 {
5933bdc9
ILT
1725 unsigned short e = bfd_get_16 (abfd, data + i + 8 + j * 2);
1726 unsigned int t = (e & 0xF000) >> 12;
277d1b5e
ILT
1727 int off = e & 0x0FFF;
1728
5933bdc9
ILT
1729 if (t >= sizeof (tbl) / sizeof (tbl[0]))
1730 t = (sizeof (tbl) / sizeof (tbl[0])) - 1;
277d1b5e 1731
5933bdc9
ILT
1732 fprintf (file,
1733 _("\treloc %4d offset %4x [%4lx] %s"),
1734 j, off, (long) (off + virtual_address), tbl[t]);
277d1b5e 1735
17505c5c
NC
1736 /* HIGHADJ takes an argument, - the next record *is* the
1737 low 16 bits of addend. */
5933bdc9
ILT
1738 if (t == IMAGE_REL_BASED_HIGHADJ)
1739 {
17505c5c 1740 fprintf (file, " (%4x)",
5933bdc9
ILT
1741 ((unsigned int)
1742 bfd_get_16 (abfd, data + i + 8 + j * 2 + 2)));
1743 j++;
1744 }
17505c5c
NC
1745
1746 fprintf (file, "\n");
277d1b5e
ILT
1747 }
1748 i += size;
1749 }
1750
1751 free (data);
1752
1753 return true;
1754}
1755
1756/* Print out the program headers. */
1757
1758boolean
1759_bfd_pe_print_private_bfd_data_common (abfd, vfile)
1760 bfd *abfd;
1761 PTR vfile;
1762{
1763 FILE *file = (FILE *) vfile;
1764 int j;
1765 pe_data_type *pe = pe_data (abfd);
1766 struct internal_extra_pe_aouthdr *i = &pe->pe_opthdr;
fac41780 1767 const char *subsystem_name = NULL;
277d1b5e
ILT
1768
1769 /* The MS dumpbin program reportedly ands with 0xff0f before
1770 printing the characteristics field. Not sure why. No reason to
1771 emulate it here. */
1772 fprintf (file, _("\nCharacteristics 0x%x\n"), pe->real_flags);
1773#undef PF
1774#define PF(x, y) if (pe->real_flags & x) { fprintf (file, "\t%s\n", y); }
1775 PF (F_RELFLG, "relocations stripped");
1776 PF (F_EXEC, "executable");
1777 PF (F_LNNO, "line numbers stripped");
1778 PF (F_LSYMS, "symbols stripped");
1779 PF (0x80, "little endian");
1780 PF (F_AR32WR, "32 bit words");
1781 PF (0x200, "debugging information removed");
1782 PF (0x1000, "system file");
1783 PF (F_DLL, "DLL");
1784 PF (0x8000, "big endian");
1785#undef PF
1786
5933bdc9
ILT
1787 /* ctime implies '\n'. */
1788 fprintf (file, "\nTime/Date\t\t%s", ctime (&pe->coff.timestamp));
277d1b5e
ILT
1789 fprintf (file,"\nImageBase\t\t");
1790 fprintf_vma (file, i->ImageBase);
1791 fprintf (file,"\nSectionAlignment\t");
1792 fprintf_vma (file, i->SectionAlignment);
1793 fprintf (file,"\nFileAlignment\t\t");
1794 fprintf_vma (file, i->FileAlignment);
1795 fprintf (file,"\nMajorOSystemVersion\t%d\n", i->MajorOperatingSystemVersion);
1796 fprintf (file,"MinorOSystemVersion\t%d\n", i->MinorOperatingSystemVersion);
1797 fprintf (file,"MajorImageVersion\t%d\n", i->MajorImageVersion);
1798 fprintf (file,"MinorImageVersion\t%d\n", i->MinorImageVersion);
1799 fprintf (file,"MajorSubsystemVersion\t%d\n", i->MajorSubsystemVersion);
1800 fprintf (file,"MinorSubsystemVersion\t%d\n", i->MinorSubsystemVersion);
8181c403 1801 fprintf (file,"Win32Version\t\t%08lx\n", i->Reserved1);
277d1b5e
ILT
1802 fprintf (file,"SizeOfImage\t\t%08lx\n", i->SizeOfImage);
1803 fprintf (file,"SizeOfHeaders\t\t%08lx\n", i->SizeOfHeaders);
1804 fprintf (file,"CheckSum\t\t%08lx\n", i->CheckSum);
fac41780
JW
1805 switch (i->Subsystem)
1806 {
1807 case IMAGE_SUBSYSTEM_UNKNOWN:
1808 subsystem_name = "unspecified";
1809 break;
1810 case IMAGE_SUBSYSTEM_NATIVE:
1811 subsystem_name = "NT native";
1812 break;
1813 case IMAGE_SUBSYSTEM_WINDOWS_GUI:
1814 subsystem_name = "Windows GUI";
1815 break;
1816 case IMAGE_SUBSYSTEM_WINDOWS_CUI:
1817 subsystem_name = "Windows CUI";
1818 break;
1819 case IMAGE_SUBSYSTEM_POSIX_CUI:
1820 subsystem_name = "POSIX CUI";
1821 break;
1822 case IMAGE_SUBSYSTEM_WINDOWS_CE_GUI:
1823 subsystem_name = "Wince CUI";
1824 break;
1825 case IMAGE_SUBSYSTEM_EFI_APPLICATION:
1826 subsystem_name = "EFI application";
1827 break;
1828 case IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER:
1829 subsystem_name = "EFI boot service driver";
1830 break;
1831 case IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER:
1832 subsystem_name = "EFI runtime driver";
1833 break;
1834 }
1835 fprintf (file,"Subsystem\t\t%08x", i->Subsystem);
1836 if (subsystem_name)
1837 fprintf (file, "\t(%s)", subsystem_name);
1838 fprintf (file,"\nDllCharacteristics\t%08x\n", i->DllCharacteristics);
277d1b5e
ILT
1839 fprintf (file,"SizeOfStackReserve\t");
1840 fprintf_vma (file, i->SizeOfStackReserve);
1841 fprintf (file,"\nSizeOfStackCommit\t");
1842 fprintf_vma (file, i->SizeOfStackCommit);
1843 fprintf (file,"\nSizeOfHeapReserve\t");
1844 fprintf_vma (file, i->SizeOfHeapReserve);
1845 fprintf (file,"\nSizeOfHeapCommit\t");
1846 fprintf_vma (file, i->SizeOfHeapCommit);
1847 fprintf (file,"\nLoaderFlags\t\t%08lx\n", i->LoaderFlags);
1848 fprintf (file,"NumberOfRvaAndSizes\t%08lx\n", i->NumberOfRvaAndSizes);
1849
1850 fprintf (file,"\nThe Data Directory\n");
1851 for (j = 0; j < IMAGE_NUMBEROF_DIRECTORY_ENTRIES; j++)
1852 {
1853 fprintf (file, "Entry %1x ", j);
1854 fprintf_vma (file, i->DataDirectory[j].VirtualAddress);
1855 fprintf (file, " %08lx ", i->DataDirectory[j].Size);
1856 fprintf (file, "%s\n", dir_names[j]);
1857 }
1858
1859 pe_print_idata (abfd, vfile);
1860 pe_print_edata (abfd, vfile);
1861 pe_print_pdata (abfd, vfile);
1862 pe_print_reloc (abfd, vfile);
1863
1864 return true;
1865}
1866
1867/* Copy any private info we understand from the input bfd
1868 to the output bfd. */
1869
1870boolean
1871_bfd_pe_bfd_copy_private_bfd_data_common (ibfd, obfd)
1872 bfd *ibfd, *obfd;
1873{
1874 /* One day we may try to grok other private data. */
1875 if (ibfd->xvec->flavour != bfd_target_coff_flavour
1876 || obfd->xvec->flavour != bfd_target_coff_flavour)
1877 return true;
1878
1879 pe_data (obfd)->pe_opthdr = pe_data (ibfd)->pe_opthdr;
1880 pe_data (obfd)->dll = pe_data (ibfd)->dll;
1881
5933bdc9
ILT
1882 /* for strip: if we removed .reloc, we'll make a real mess of things
1883 if we don't remove this entry as well. */
1884 if (! pe_data (obfd)->has_reloc_section)
1885 {
1886 pe_data(obfd)->pe_opthdr.DataDirectory[5].VirtualAddress = 0;
1887 pe_data(obfd)->pe_opthdr.DataDirectory[5].Size = 0;
1888 }
277d1b5e
ILT
1889 return true;
1890}
1891
1892/* Copy private section data. */
1893boolean
1894_bfd_pe_bfd_copy_private_section_data (ibfd, isec, obfd, osec)
1895 bfd *ibfd;
1896 asection *isec;
1897 bfd *obfd;
1898 asection *osec;
1899{
1900 if (bfd_get_flavour (ibfd) != bfd_target_coff_flavour
1901 || bfd_get_flavour (obfd) != bfd_target_coff_flavour)
1902 return true;
1903
1904 if (coff_section_data (ibfd, isec) != NULL
1905 && pei_section_data (ibfd, isec) != NULL)
1906 {
1907 if (coff_section_data (obfd, osec) == NULL)
1908 {
1909 osec->used_by_bfd =
1910 (PTR) bfd_zalloc (obfd, sizeof (struct coff_section_tdata));
1911 if (osec->used_by_bfd == NULL)
1912 return false;
1913 }
1914 if (pei_section_data (obfd, osec) == NULL)
1915 {
1916 coff_section_data (obfd, osec)->tdata =
1917 (PTR) bfd_zalloc (obfd, sizeof (struct pei_section_tdata));
1918 if (coff_section_data (obfd, osec)->tdata == NULL)
1919 return false;
1920 }
1921 pei_section_data (obfd, osec)->virt_size =
1922 pei_section_data (ibfd, isec)->virt_size;
5933bdc9
ILT
1923 pei_section_data (obfd, osec)->pe_flags =
1924 pei_section_data (ibfd, isec)->pe_flags;
277d1b5e
ILT
1925 }
1926
1927 return true;
1928}
7d2b58d6
ILT
1929
1930void
1931_bfd_pe_get_symbol_info (abfd, symbol, ret)
1932 bfd *abfd;
1933 asymbol *symbol;
1934 symbol_info *ret;
1935{
1936 coff_get_symbol_info (abfd, symbol, ret);
1937
1938 if (pe_data (abfd) != NULL
1939 && ((symbol->flags & BSF_DEBUGGING) == 0
1940 || (symbol->flags & BSF_DEBUGGING_RELOC) != 0)
1941 && ! bfd_is_abs_section (symbol->section))
1942 ret->value += pe_data (abfd)->pe_opthdr.ImageBase;
1943}
2fbadf2c
ILT
1944
1945/* Handle the .idata section and other things that need symbol table
1946 access. */
1947
1948boolean
1949_bfd_pei_final_link_postscript (abfd, pfinfo)
1950 bfd *abfd;
1951 struct coff_final_link_info *pfinfo;
1952{
1953 struct coff_link_hash_entry *h1;
1954 struct bfd_link_info *info = pfinfo->info;
1955
1956 /* There are a few fields that need to be filled in now while we
1957 have symbol table access.
1958
1959 The .idata subsections aren't directly available as sections, but
1960 they are in the symbol table, so get them from there. */
1961
1962 /* The import directory. This is the address of .idata$2, with size
1963 of .idata$2 + .idata$3. */
1964 h1 = coff_link_hash_lookup (coff_hash_table (info),
1965 ".idata$2", false, false, true);
1966 if (h1 != NULL)
1967 {
1968 pe_data(abfd)->pe_opthdr.DataDirectory[1].VirtualAddress =
1969 (h1->root.u.def.value
1970 + h1->root.u.def.section->output_section->vma
1971 + h1->root.u.def.section->output_offset);
1972 h1 = coff_link_hash_lookup (coff_hash_table (info),
1973 ".idata$4", false, false, true);
1974 pe_data (abfd)->pe_opthdr.DataDirectory[1].Size =
1975 ((h1->root.u.def.value
1976 + h1->root.u.def.section->output_section->vma
1977 + h1->root.u.def.section->output_offset)
1978 - pe_data(abfd)->pe_opthdr.DataDirectory[1].VirtualAddress);
1979
1980 /* The import address table. This is the size/address of
1981 .idata$5. */
1982 h1 = coff_link_hash_lookup (coff_hash_table (info),
1983 ".idata$5", false, false, true);
1984 pe_data (abfd)->pe_opthdr.DataDirectory[12].VirtualAddress =
1985 (h1->root.u.def.value
1986 + h1->root.u.def.section->output_section->vma
1987 + h1->root.u.def.section->output_offset);
1988 h1 = coff_link_hash_lookup (coff_hash_table (info),
1989 ".idata$6", false, false, true);
1990 pe_data (abfd)->pe_opthdr.DataDirectory[12].Size =
1991 ((h1->root.u.def.value
1992 + h1->root.u.def.section->output_section->vma
1993 + h1->root.u.def.section->output_offset)
1994 - pe_data(abfd)->pe_opthdr.DataDirectory[12].VirtualAddress);
1995 }
1996
1997 /* If we couldn't find idata$2, we either have an excessively
1998 trivial program or are in DEEP trouble; we have to assume trivial
1999 program.... */
2000 return true;
2001}
This page took 0.120106 seconds and 4 git commands to generate.