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