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