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