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