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