[testsuite] Disable Ctrl-V use for mingw hosts.
[deliverable/binutils-gdb.git] / bfd / peXXigen.c
CommitLineData
277d1b5e 1/* Support for the generic parts of PE/PEI; the common executable parts.
4b95cf5c 2 Copyright (C) 1995-2014 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"
5879bb8f
NC
65#ifdef HAVE_WCHAR_H
66#include <wchar.h>
67#endif
277d1b5e
ILT
68
69/* NOTE: it's strange to be including an architecture specific header
70 in what's supposed to be general (to PE/PEI) code. However, that's
71 where the definitions are, and they don't vary per architecture
72 within PE/PEI, so we get them from there. FIXME: The lack of
73 variance is an assumption which may prove to be incorrect if new
74 PE/PEI targets are created. */
99ad8390
NC
75#if defined COFF_WITH_pex64
76# include "coff/x86_64.h"
77#elif defined COFF_WITH_pep
cbff5e0d
DD
78# include "coff/ia64.h"
79#else
80# include "coff/i386.h"
81#endif
277d1b5e
ILT
82
83#include "coff/pe.h"
84#include "libcoff.h"
85#include "libpei.h"
5879bb8f 86#include "safe-ctype.h"
277d1b5e 87
99ad8390 88#if defined COFF_WITH_pep || defined COFF_WITH_pex64
cbff5e0d
DD
89# undef AOUTSZ
90# define AOUTSZ PEPAOUTSZ
91# define PEAOUTHDR PEPAOUTHDR
92#endif
93
5879bb8f
NC
94#define HighBitSet(val) ((val) & 0x80000000)
95#define SetHighBit(val) ((val) | 0x80000000)
96#define WithoutHighBit(val) ((val) & 0x7fffffff)
97
277d1b5e
ILT
98/* FIXME: This file has various tests of POWERPC_LE_PE. Those tests
99 worked when the code was in peicode.h, but no longer work now that
100 the code is in peigen.c. PowerPC NT is said to be dead. If
101 anybody wants to revive the code, you will have to figure out how
102 to handle those issues. */
1725a96e 103\f
277d1b5e 104void
7920ce38 105_bfd_XXi_swap_sym_in (bfd * abfd, void * ext1, void * in1)
277d1b5e 106{
6fa957a9
KH
107 SYMENT *ext = (SYMENT *) ext1;
108 struct internal_syment *in = (struct internal_syment *) in1;
277d1b5e 109
6fa957a9
KH
110 if (ext->e.e_name[0] == 0)
111 {
112 in->_n._n_n._n_zeroes = 0;
dc810e39 113 in->_n._n_n._n_offset = H_GET_32 (abfd, ext->e.e.e_offset);
6fa957a9
KH
114 }
115 else
1725a96e 116 memcpy (in->_n._n_name, ext->e.e_name, SYMNMLEN);
277d1b5e 117
dc810e39
AM
118 in->n_value = H_GET_32 (abfd, ext->e_value);
119 in->n_scnum = H_GET_16 (abfd, ext->e_scnum);
1725a96e 120
6fa957a9 121 if (sizeof (ext->e_type) == 2)
dc810e39 122 in->n_type = H_GET_16 (abfd, ext->e_type);
6fa957a9 123 else
dc810e39 124 in->n_type = H_GET_32 (abfd, ext->e_type);
1725a96e 125
dc810e39
AM
126 in->n_sclass = H_GET_8 (abfd, ext->e_sclass);
127 in->n_numaux = H_GET_8 (abfd, ext->e_numaux);
277d1b5e
ILT
128
129#ifndef STRICT_PE_FORMAT
6fa957a9 130 /* This is for Gnu-created DLLs. */
277d1b5e
ILT
131
132 /* The section symbols for the .idata$ sections have class 0x68
133 (C_SECTION), which MS documentation indicates is a section
134 symbol. Unfortunately, the value field in the symbol is simply a
135 copy of the .idata section's flags rather than something useful.
136 When these symbols are encountered, change the value to 0 so that
137 they will be handled somewhat correctly in the bfd code. */
138 if (in->n_sclass == C_SECTION)
139 {
383c383f 140 char namebuf[SYMNMLEN + 1];
ba775898 141 const char *name = NULL;
383c383f 142
277d1b5e
ILT
143 in->n_value = 0x0;
144
277d1b5e
ILT
145 /* Create synthetic empty sections as needed. DJ */
146 if (in->n_scnum == 0)
147 {
148 asection *sec;
1725a96e 149
383c383f
AM
150 name = _bfd_coff_internal_syment_name (abfd, in, namebuf);
151 if (name == NULL)
152 /* FIXME: Return error. */
153 abort ();
154 sec = bfd_get_section_by_name (abfd, name);
155 if (sec != NULL)
156 in->n_scnum = sec->target_index;
277d1b5e 157 }
1725a96e 158
277d1b5e
ILT
159 if (in->n_scnum == 0)
160 {
161 int unused_section_number = 0;
162 asection *sec;
117ed4f8 163 flagword flags;
1725a96e 164
6fa957a9 165 for (sec = abfd->sections; sec; sec = sec->next)
277d1b5e 166 if (unused_section_number <= sec->target_index)
6fa957a9 167 unused_section_number = sec->target_index + 1;
277d1b5e 168
383c383f
AM
169 if (name == namebuf)
170 {
a50b1753 171 name = (const char *) bfd_alloc (abfd, strlen (namebuf) + 1);
383c383f
AM
172 if (name == NULL)
173 /* FIXME: Return error. */
174 abort ();
175 strcpy ((char *) name, namebuf);
176 }
117ed4f8
AM
177 flags = SEC_HAS_CONTENTS | SEC_ALLOC | SEC_DATA | SEC_LOAD;
178 sec = bfd_make_section_anyway_with_flags (abfd, name, flags);
383c383f
AM
179 if (sec == NULL)
180 /* FIXME: Return error. */
181 abort ();
277d1b5e
ILT
182
183 sec->vma = 0;
184 sec->lma = 0;
eea6121a 185 sec->size = 0;
277d1b5e
ILT
186 sec->filepos = 0;
187 sec->rel_filepos = 0;
188 sec->reloc_count = 0;
189 sec->line_filepos = 0;
190 sec->lineno_count = 0;
191 sec->userdata = NULL;
7920ce38 192 sec->next = NULL;
277d1b5e 193 sec->alignment_power = 2;
277d1b5e
ILT
194
195 sec->target_index = unused_section_number;
196
197 in->n_scnum = unused_section_number;
198 }
199 in->n_sclass = C_STAT;
277d1b5e
ILT
200 }
201#endif
202
203#ifdef coff_swap_sym_in_hook
204 /* This won't work in peigen.c, but since it's for PPC PE, it's not
9602af51 205 worth fixing. */
6fa957a9 206 coff_swap_sym_in_hook (abfd, ext1, in1);
277d1b5e
ILT
207#endif
208}
209
210unsigned int
7920ce38 211_bfd_XXi_swap_sym_out (bfd * abfd, void * inp, void * extp)
277d1b5e 212{
6fa957a9
KH
213 struct internal_syment *in = (struct internal_syment *) inp;
214 SYMENT *ext = (SYMENT *) extp;
1725a96e 215
6fa957a9
KH
216 if (in->_n._n_name[0] == 0)
217 {
dc810e39
AM
218 H_PUT_32 (abfd, 0, ext->e.e.e_zeroes);
219 H_PUT_32 (abfd, in->_n._n_n._n_offset, ext->e.e.e_offset);
6fa957a9
KH
220 }
221 else
1725a96e 222 memcpy (ext->e.e_name, in->_n._n_name, SYMNMLEN);
277d1b5e 223
dc810e39
AM
224 H_PUT_32 (abfd, in->n_value, ext->e_value);
225 H_PUT_16 (abfd, in->n_scnum, ext->e_scnum);
1725a96e 226
9602af51 227 if (sizeof (ext->e_type) == 2)
dc810e39 228 H_PUT_16 (abfd, in->n_type, ext->e_type);
277d1b5e 229 else
dc810e39 230 H_PUT_32 (abfd, in->n_type, ext->e_type);
1725a96e 231
dc810e39
AM
232 H_PUT_8 (abfd, in->n_sclass, ext->e_sclass);
233 H_PUT_8 (abfd, in->n_numaux, ext->e_numaux);
277d1b5e
ILT
234
235 return SYMESZ;
236}
237
238void
7920ce38
NC
239_bfd_XXi_swap_aux_in (bfd * abfd,
240 void * ext1,
241 int type,
96d56e9f 242 int in_class,
7920ce38
NC
243 int indx ATTRIBUTE_UNUSED,
244 int numaux ATTRIBUTE_UNUSED,
245 void * in1)
277d1b5e 246{
6fa957a9
KH
247 AUXENT *ext = (AUXENT *) ext1;
248 union internal_auxent *in = (union internal_auxent *) in1;
249
96d56e9f 250 switch (in_class)
6fa957a9
KH
251 {
252 case C_FILE:
253 if (ext->x_file.x_fname[0] == 0)
254 {
255 in->x_file.x_n.x_zeroes = 0;
dc810e39 256 in->x_file.x_n.x_offset = H_GET_32 (abfd, ext->x_file.x_n.x_offset);
6fa957a9
KH
257 }
258 else
1725a96e 259 memcpy (in->x_file.x_fname, ext->x_file.x_fname, FILNMLEN);
277d1b5e 260 return;
6fa957a9
KH
261
262 case C_STAT:
263 case C_LEAFSTAT:
264 case C_HIDDEN:
265 if (type == T_NULL)
266 {
267 in->x_scn.x_scnlen = GET_SCN_SCNLEN (abfd, ext);
268 in->x_scn.x_nreloc = GET_SCN_NRELOC (abfd, ext);
269 in->x_scn.x_nlinno = GET_SCN_NLINNO (abfd, ext);
dc810e39
AM
270 in->x_scn.x_checksum = H_GET_32 (abfd, ext->x_scn.x_checksum);
271 in->x_scn.x_associated = H_GET_16 (abfd, ext->x_scn.x_associated);
272 in->x_scn.x_comdat = H_GET_8 (abfd, ext->x_scn.x_comdat);
6fa957a9
KH
273 return;
274 }
275 break;
277d1b5e 276 }
277d1b5e 277
dc810e39
AM
278 in->x_sym.x_tagndx.l = H_GET_32 (abfd, ext->x_sym.x_tagndx);
279 in->x_sym.x_tvndx = H_GET_16 (abfd, ext->x_sym.x_tvndx);
277d1b5e 280
96d56e9f
NC
281 if (in_class == C_BLOCK || in_class == C_FCN || ISFCN (type)
282 || ISTAG (in_class))
277d1b5e
ILT
283 {
284 in->x_sym.x_fcnary.x_fcn.x_lnnoptr = GET_FCN_LNNOPTR (abfd, ext);
285 in->x_sym.x_fcnary.x_fcn.x_endndx.l = GET_FCN_ENDNDX (abfd, ext);
286 }
287 else
288 {
289 in->x_sym.x_fcnary.x_ary.x_dimen[0] =
dc810e39 290 H_GET_16 (abfd, ext->x_sym.x_fcnary.x_ary.x_dimen[0]);
277d1b5e 291 in->x_sym.x_fcnary.x_ary.x_dimen[1] =
dc810e39 292 H_GET_16 (abfd, ext->x_sym.x_fcnary.x_ary.x_dimen[1]);
277d1b5e 293 in->x_sym.x_fcnary.x_ary.x_dimen[2] =
dc810e39 294 H_GET_16 (abfd, ext->x_sym.x_fcnary.x_ary.x_dimen[2]);
277d1b5e 295 in->x_sym.x_fcnary.x_ary.x_dimen[3] =
dc810e39 296 H_GET_16 (abfd, ext->x_sym.x_fcnary.x_ary.x_dimen[3]);
277d1b5e
ILT
297 }
298
6fa957a9
KH
299 if (ISFCN (type))
300 {
dc810e39 301 in->x_sym.x_misc.x_fsize = H_GET_32 (abfd, ext->x_sym.x_misc.x_fsize);
6fa957a9
KH
302 }
303 else
304 {
305 in->x_sym.x_misc.x_lnsz.x_lnno = GET_LNSZ_LNNO (abfd, ext);
306 in->x_sym.x_misc.x_lnsz.x_size = GET_LNSZ_SIZE (abfd, ext);
307 }
277d1b5e
ILT
308}
309
310unsigned int
7920ce38
NC
311_bfd_XXi_swap_aux_out (bfd * abfd,
312 void * inp,
313 int type,
96d56e9f 314 int in_class,
7920ce38
NC
315 int indx ATTRIBUTE_UNUSED,
316 int numaux ATTRIBUTE_UNUSED,
317 void * extp)
277d1b5e 318{
6fa957a9
KH
319 union internal_auxent *in = (union internal_auxent *) inp;
320 AUXENT *ext = (AUXENT *) extp;
321
7920ce38
NC
322 memset (ext, 0, AUXESZ);
323
96d56e9f 324 switch (in_class)
6fa957a9
KH
325 {
326 case C_FILE:
327 if (in->x_file.x_fname[0] == 0)
328 {
dc810e39
AM
329 H_PUT_32 (abfd, 0, ext->x_file.x_n.x_zeroes);
330 H_PUT_32 (abfd, in->x_file.x_n.x_offset, ext->x_file.x_n.x_offset);
6fa957a9
KH
331 }
332 else
1725a96e
NC
333 memcpy (ext->x_file.x_fname, in->x_file.x_fname, FILNMLEN);
334
277d1b5e 335 return AUXESZ;
6fa957a9
KH
336
337 case C_STAT:
338 case C_LEAFSTAT:
339 case C_HIDDEN:
340 if (type == T_NULL)
341 {
342 PUT_SCN_SCNLEN (abfd, in->x_scn.x_scnlen, ext);
343 PUT_SCN_NRELOC (abfd, in->x_scn.x_nreloc, ext);
344 PUT_SCN_NLINNO (abfd, in->x_scn.x_nlinno, ext);
dc810e39
AM
345 H_PUT_32 (abfd, in->x_scn.x_checksum, ext->x_scn.x_checksum);
346 H_PUT_16 (abfd, in->x_scn.x_associated, ext->x_scn.x_associated);
347 H_PUT_8 (abfd, in->x_scn.x_comdat, ext->x_scn.x_comdat);
6fa957a9
KH
348 return AUXESZ;
349 }
350 break;
277d1b5e 351 }
277d1b5e 352
dc810e39
AM
353 H_PUT_32 (abfd, in->x_sym.x_tagndx.l, ext->x_sym.x_tagndx);
354 H_PUT_16 (abfd, in->x_sym.x_tvndx, ext->x_sym.x_tvndx);
277d1b5e 355
96d56e9f
NC
356 if (in_class == C_BLOCK || in_class == C_FCN || ISFCN (type)
357 || ISTAG (in_class))
277d1b5e 358 {
6fa957a9
KH
359 PUT_FCN_LNNOPTR (abfd, in->x_sym.x_fcnary.x_fcn.x_lnnoptr, ext);
360 PUT_FCN_ENDNDX (abfd, in->x_sym.x_fcnary.x_fcn.x_endndx.l, ext);
277d1b5e
ILT
361 }
362 else
363 {
dc810e39
AM
364 H_PUT_16 (abfd, in->x_sym.x_fcnary.x_ary.x_dimen[0],
365 ext->x_sym.x_fcnary.x_ary.x_dimen[0]);
366 H_PUT_16 (abfd, in->x_sym.x_fcnary.x_ary.x_dimen[1],
367 ext->x_sym.x_fcnary.x_ary.x_dimen[1]);
368 H_PUT_16 (abfd, in->x_sym.x_fcnary.x_ary.x_dimen[2],
369 ext->x_sym.x_fcnary.x_ary.x_dimen[2]);
370 H_PUT_16 (abfd, in->x_sym.x_fcnary.x_ary.x_dimen[3],
371 ext->x_sym.x_fcnary.x_ary.x_dimen[3]);
277d1b5e
ILT
372 }
373
374 if (ISFCN (type))
dc810e39 375 H_PUT_32 (abfd, in->x_sym.x_misc.x_fsize, ext->x_sym.x_misc.x_fsize);
277d1b5e
ILT
376 else
377 {
378 PUT_LNSZ_LNNO (abfd, in->x_sym.x_misc.x_lnsz.x_lnno, ext);
379 PUT_LNSZ_SIZE (abfd, in->x_sym.x_misc.x_lnsz.x_size, ext);
380 }
381
382 return AUXESZ;
383}
384
385void
7920ce38 386_bfd_XXi_swap_lineno_in (bfd * abfd, void * ext1, void * in1)
277d1b5e 387{
6fa957a9
KH
388 LINENO *ext = (LINENO *) ext1;
389 struct internal_lineno *in = (struct internal_lineno *) in1;
277d1b5e 390
dc810e39 391 in->l_addr.l_symndx = H_GET_32 (abfd, ext->l_addr.l_symndx);
6fa957a9 392 in->l_lnno = GET_LINENO_LNNO (abfd, ext);
277d1b5e
ILT
393}
394
395unsigned int
7920ce38 396_bfd_XXi_swap_lineno_out (bfd * abfd, void * inp, void * outp)
277d1b5e 397{
6fa957a9
KH
398 struct internal_lineno *in = (struct internal_lineno *) inp;
399 struct external_lineno *ext = (struct external_lineno *) outp;
dc810e39 400 H_PUT_32 (abfd, in->l_addr.l_symndx, ext->l_addr.l_symndx);
277d1b5e
ILT
401
402 PUT_LINENO_LNNO (abfd, in->l_lnno, ext);
403 return LINESZ;
404}
405
406void
7920ce38
NC
407_bfd_XXi_swap_aouthdr_in (bfd * abfd,
408 void * aouthdr_ext1,
409 void * aouthdr_int1)
277d1b5e 410{
d13c9dc6 411 PEAOUTHDR * src = (PEAOUTHDR *) aouthdr_ext1;
7920ce38 412 AOUTHDR * aouthdr_ext = (AOUTHDR *) aouthdr_ext1;
d13c9dc6
L
413 struct internal_aouthdr *aouthdr_int
414 = (struct internal_aouthdr *) aouthdr_int1;
415 struct internal_extra_pe_aouthdr *a = &aouthdr_int->pe;
277d1b5e 416
dc810e39
AM
417 aouthdr_int->magic = H_GET_16 (abfd, aouthdr_ext->magic);
418 aouthdr_int->vstamp = H_GET_16 (abfd, aouthdr_ext->vstamp);
419 aouthdr_int->tsize = GET_AOUTHDR_TSIZE (abfd, aouthdr_ext->tsize);
420 aouthdr_int->dsize = GET_AOUTHDR_DSIZE (abfd, aouthdr_ext->dsize);
421 aouthdr_int->bsize = GET_AOUTHDR_BSIZE (abfd, aouthdr_ext->bsize);
422 aouthdr_int->entry = GET_AOUTHDR_ENTRY (abfd, aouthdr_ext->entry);
277d1b5e 423 aouthdr_int->text_start =
dc810e39 424 GET_AOUTHDR_TEXT_START (abfd, aouthdr_ext->text_start);
99ad8390 425#if !defined(COFF_WITH_pep) && !defined(COFF_WITH_pex64)
7920ce38 426 /* PE32+ does not have data_start member! */
277d1b5e 427 aouthdr_int->data_start =
dc810e39 428 GET_AOUTHDR_DATA_START (abfd, aouthdr_ext->data_start);
d13c9dc6 429 a->BaseOfData = aouthdr_int->data_start;
fac41780 430#endif
277d1b5e 431
d13c9dc6
L
432 a->Magic = aouthdr_int->magic;
433 a->MajorLinkerVersion = H_GET_8 (abfd, aouthdr_ext->vstamp);
434 a->MinorLinkerVersion = H_GET_8 (abfd, aouthdr_ext->vstamp + 1);
435 a->SizeOfCode = aouthdr_int->tsize ;
436 a->SizeOfInitializedData = aouthdr_int->dsize ;
437 a->SizeOfUninitializedData = aouthdr_int->bsize ;
438 a->AddressOfEntryPoint = aouthdr_int->entry;
439 a->BaseOfCode = aouthdr_int->text_start;
dc810e39
AM
440 a->ImageBase = GET_OPTHDR_IMAGE_BASE (abfd, src->ImageBase);
441 a->SectionAlignment = H_GET_32 (abfd, src->SectionAlignment);
442 a->FileAlignment = H_GET_32 (abfd, src->FileAlignment);
277d1b5e 443 a->MajorOperatingSystemVersion =
dc810e39 444 H_GET_16 (abfd, src->MajorOperatingSystemVersion);
277d1b5e 445 a->MinorOperatingSystemVersion =
dc810e39
AM
446 H_GET_16 (abfd, src->MinorOperatingSystemVersion);
447 a->MajorImageVersion = H_GET_16 (abfd, src->MajorImageVersion);
448 a->MinorImageVersion = H_GET_16 (abfd, src->MinorImageVersion);
449 a->MajorSubsystemVersion = H_GET_16 (abfd, src->MajorSubsystemVersion);
450 a->MinorSubsystemVersion = H_GET_16 (abfd, src->MinorSubsystemVersion);
451 a->Reserved1 = H_GET_32 (abfd, src->Reserved1);
452 a->SizeOfImage = H_GET_32 (abfd, src->SizeOfImage);
453 a->SizeOfHeaders = H_GET_32 (abfd, src->SizeOfHeaders);
454 a->CheckSum = H_GET_32 (abfd, src->CheckSum);
455 a->Subsystem = H_GET_16 (abfd, src->Subsystem);
456 a->DllCharacteristics = H_GET_16 (abfd, src->DllCharacteristics);
457 a->SizeOfStackReserve =
458 GET_OPTHDR_SIZE_OF_STACK_RESERVE (abfd, src->SizeOfStackReserve);
459 a->SizeOfStackCommit =
460 GET_OPTHDR_SIZE_OF_STACK_COMMIT (abfd, src->SizeOfStackCommit);
461 a->SizeOfHeapReserve =
462 GET_OPTHDR_SIZE_OF_HEAP_RESERVE (abfd, src->SizeOfHeapReserve);
463 a->SizeOfHeapCommit =
464 GET_OPTHDR_SIZE_OF_HEAP_COMMIT (abfd, src->SizeOfHeapCommit);
465 a->LoaderFlags = H_GET_32 (abfd, src->LoaderFlags);
466 a->NumberOfRvaAndSizes = H_GET_32 (abfd, src->NumberOfRvaAndSizes);
277d1b5e
ILT
467
468 {
469 int idx;
1725a96e 470
ce63b7b3 471 for (idx = 0; idx < a->NumberOfRvaAndSizes; idx++)
277d1b5e 472 {
6fa957a9
KH
473 /* If data directory is empty, rva also should be 0. */
474 int size =
dc810e39 475 H_GET_32 (abfd, src->DataDirectory[idx][1]);
99ad8390 476
3028b4c0
DD
477 a->DataDirectory[idx].Size = size;
478
479 if (size)
1725a96e 480 a->DataDirectory[idx].VirtualAddress =
dc810e39 481 H_GET_32 (abfd, src->DataDirectory[idx][0]);
6fa957a9 482 else
3028b4c0 483 a->DataDirectory[idx].VirtualAddress = 0;
277d1b5e
ILT
484 }
485 }
486
487 if (aouthdr_int->entry)
488 {
489 aouthdr_int->entry += a->ImageBase;
99ad8390 490#if !defined(COFF_WITH_pep) && !defined(COFF_WITH_pex64)
277d1b5e 491 aouthdr_int->entry &= 0xffffffff;
fac41780 492#endif
277d1b5e 493 }
1725a96e 494
9602af51 495 if (aouthdr_int->tsize)
277d1b5e
ILT
496 {
497 aouthdr_int->text_start += a->ImageBase;
99ad8390 498#if !defined(COFF_WITH_pep) && !defined(COFF_WITH_pex64)
277d1b5e 499 aouthdr_int->text_start &= 0xffffffff;
fac41780 500#endif
277d1b5e 501 }
1725a96e 502
99ad8390 503#if !defined(COFF_WITH_pep) && !defined(COFF_WITH_pex64)
7920ce38 504 /* PE32+ does not have data_start member! */
9602af51 505 if (aouthdr_int->dsize)
277d1b5e
ILT
506 {
507 aouthdr_int->data_start += a->ImageBase;
508 aouthdr_int->data_start &= 0xffffffff;
509 }
fac41780 510#endif
277d1b5e
ILT
511
512#ifdef POWERPC_LE_PE
513 /* These three fields are normally set up by ppc_relocate_section.
514 In the case of reading a file in, we can pick them up from the
515 DataDirectory. */
6c73cbb1
NC
516 first_thunk_address = a->DataDirectory[PE_IMPORT_ADDRESS_TABLE].VirtualAddress;
517 thunk_size = a->DataDirectory[PE_IMPORT_ADDRESS_TABLE].Size;
518 import_table_size = a->DataDirectory[PE_IMPORT_TABLE].Size;
277d1b5e 519#endif
277d1b5e
ILT
520}
521
5933bdc9
ILT
522/* A support function for below. */
523
524static void
7920ce38
NC
525add_data_entry (bfd * abfd,
526 struct internal_extra_pe_aouthdr *aout,
527 int idx,
528 char *name,
529 bfd_vma base)
277d1b5e
ILT
530{
531 asection *sec = bfd_get_section_by_name (abfd, name);
532
1725a96e 533 /* Add import directory information if it exists. */
277d1b5e
ILT
534 if ((sec != NULL)
535 && (coff_section_data (abfd, sec) != NULL)
536 && (pei_section_data (abfd, sec) != NULL))
537 {
1725a96e 538 /* If data directory is empty, rva also should be 0. */
3028b4c0
DD
539 int size = pei_section_data (abfd, sec)->virt_size;
540 aout->DataDirectory[idx].Size = size;
541
542 if (size)
6fa957a9
KH
543 {
544 aout->DataDirectory[idx].VirtualAddress =
545 (sec->vma - base) & 0xffffffff;
546 sec->flags |= SEC_DATA;
547 }
277d1b5e
ILT
548 }
549}
550
551unsigned int
7920ce38 552_bfd_XXi_swap_aouthdr_out (bfd * abfd, void * in, void * out)
277d1b5e 553{
6fa957a9 554 struct internal_aouthdr *aouthdr_in = (struct internal_aouthdr *) in;
cbff5e0d
DD
555 pe_data_type *pe = pe_data (abfd);
556 struct internal_extra_pe_aouthdr *extra = &pe->pe_opthdr;
6fa957a9 557 PEAOUTHDR *aouthdr_out = (PEAOUTHDR *) out;
fac41780 558 bfd_vma sa, fa, ib;
ca6dee30 559 IMAGE_DATA_DIRECTORY idata2, idata5, tls;
4e1fc599 560
fac41780
JW
561 sa = extra->SectionAlignment;
562 fa = extra->FileAlignment;
563 ib = extra->ImageBase;
277d1b5e 564
6c73cbb1
NC
565 idata2 = pe->pe_opthdr.DataDirectory[PE_IMPORT_TABLE];
566 idata5 = pe->pe_opthdr.DataDirectory[PE_IMPORT_ADDRESS_TABLE];
567 tls = pe->pe_opthdr.DataDirectory[PE_TLS_TABLE];
4e1fc599 568
9602af51 569 if (aouthdr_in->tsize)
277d1b5e
ILT
570 {
571 aouthdr_in->text_start -= ib;
99ad8390 572#if !defined(COFF_WITH_pep) && !defined(COFF_WITH_pex64)
277d1b5e 573 aouthdr_in->text_start &= 0xffffffff;
cbff5e0d 574#endif
277d1b5e 575 }
1725a96e 576
9602af51 577 if (aouthdr_in->dsize)
277d1b5e
ILT
578 {
579 aouthdr_in->data_start -= ib;
99ad8390 580#if !defined(COFF_WITH_pep) && !defined(COFF_WITH_pex64)
277d1b5e 581 aouthdr_in->data_start &= 0xffffffff;
cbff5e0d 582#endif
277d1b5e 583 }
1725a96e 584
9602af51 585 if (aouthdr_in->entry)
277d1b5e
ILT
586 {
587 aouthdr_in->entry -= ib;
99ad8390 588#if !defined(COFF_WITH_pep) && !defined(COFF_WITH_pex64)
277d1b5e 589 aouthdr_in->entry &= 0xffffffff;
cbff5e0d 590#endif
277d1b5e
ILT
591 }
592
6fa957a9
KH
593#define FA(x) (((x) + fa -1 ) & (- fa))
594#define SA(x) (((x) + sa -1 ) & (- sa))
277d1b5e 595
6fa957a9 596 /* We like to have the sizes aligned. */
277d1b5e
ILT
597 aouthdr_in->bsize = FA (aouthdr_in->bsize);
598
277d1b5e
ILT
599 extra->NumberOfRvaAndSizes = IMAGE_NUMBEROF_DIRECTORY_ENTRIES;
600
8181c403 601 add_data_entry (abfd, extra, 0, ".edata", ib);
9602af51 602 add_data_entry (abfd, extra, 2, ".rsrc", ib);
8181c403 603 add_data_entry (abfd, extra, 3, ".pdata", ib);
2fbadf2c 604
c25cfdf8
NC
605 /* In theory we do not need to call add_data_entry for .idata$2 or
606 .idata$5. It will be done in bfd_coff_final_link where all the
607 required information is available. If however, we are not going
608 to perform a final link, eg because we have been invoked by objcopy
609 or strip, then we need to make sure that these Data Directory
610 entries are initialised properly.
611
612 So - we copy the input values into the output values, and then, if
613 a final link is going to be performed, it can overwrite them. */
6c73cbb1
NC
614 extra->DataDirectory[PE_IMPORT_TABLE] = idata2;
615 extra->DataDirectory[PE_IMPORT_ADDRESS_TABLE] = idata5;
616 extra->DataDirectory[PE_TLS_TABLE] = tls;
c25cfdf8 617
6c73cbb1 618 if (extra->DataDirectory[PE_IMPORT_TABLE].VirtualAddress == 0)
c25cfdf8 619 /* Until other .idata fixes are made (pending patch), the entry for
7dee875e 620 .idata is needed for backwards compatibility. FIXME. */
c25cfdf8 621 add_data_entry (abfd, extra, 1, ".idata", ib);
4e1fc599 622
2fbadf2c
ILT
623 /* For some reason, the virtual size (which is what's set by
624 add_data_entry) for .reloc is not the same as the size recorded
625 in this slot by MSVC; it doesn't seem to cause problems (so far),
626 but since it's the best we've got, use it. It does do the right
627 thing for .pdata. */
cbff5e0d 628 if (pe->has_reloc_section)
8181c403 629 add_data_entry (abfd, extra, 5, ".reloc", ib);
277d1b5e
ILT
630
631 {
632 asection *sec;
d48bdb99 633 bfd_vma hsize = 0;
6fa957a9 634 bfd_vma dsize = 0;
d48bdb99 635 bfd_vma isize = 0;
6fa957a9 636 bfd_vma tsize = 0;
277d1b5e
ILT
637
638 for (sec = abfd->sections; sec; sec = sec->next)
639 {
7920ce38 640 int rounded = FA (sec->size);
277d1b5e 641
d48bdb99
AM
642 /* The first non-zero section filepos is the header size.
643 Sections without contents will have a filepos of 0. */
644 if (hsize == 0)
645 hsize = sec->filepos;
277d1b5e
ILT
646 if (sec->flags & SEC_DATA)
647 dsize += rounded;
648 if (sec->flags & SEC_CODE)
649 tsize += rounded;
5933bdc9
ILT
650 /* The image size is the total VIRTUAL size (which is what is
651 in the virt_size field). Files have been seen (from MSVC
652 5.0 link.exe) where the file size of the .data segment is
653 quite small compared to the virtual size. Without this
50572669
L
654 fix, strip munges the file.
655
656 FIXME: We need to handle holes between sections, which may
657 happpen when we covert from another format. We just use
658 the virtual address and virtual size of the last section
659 for the image size. */
98a96df7
CF
660 if (coff_section_data (abfd, sec) != NULL
661 && pei_section_data (abfd, sec) != NULL)
50572669
L
662 isize = (sec->vma - extra->ImageBase
663 + SA (FA (pei_section_data (abfd, sec)->virt_size)));
277d1b5e
ILT
664 }
665
666 aouthdr_in->dsize = dsize;
667 aouthdr_in->tsize = tsize;
d48bdb99 668 extra->SizeOfHeaders = hsize;
50572669 669 extra->SizeOfImage = isize;
277d1b5e
ILT
670 }
671
dc810e39 672 H_PUT_16 (abfd, aouthdr_in->magic, aouthdr_out->standard.magic);
277d1b5e 673
5fdcb63c
KT
674/* e.g. 219510000 is linker version 2.19 */
675#define LINKER_VERSION ((short) (BFD_VERSION / 1000000))
5933bdc9
ILT
676
677 /* This piece of magic sets the "linker version" field to
678 LINKER_VERSION. */
dc810e39
AM
679 H_PUT_16 (abfd, (LINKER_VERSION / 100 + (LINKER_VERSION % 100) * 256),
680 aouthdr_out->standard.vstamp);
681
682 PUT_AOUTHDR_TSIZE (abfd, aouthdr_in->tsize, aouthdr_out->standard.tsize);
683 PUT_AOUTHDR_DSIZE (abfd, aouthdr_in->dsize, aouthdr_out->standard.dsize);
684 PUT_AOUTHDR_BSIZE (abfd, aouthdr_in->bsize, aouthdr_out->standard.bsize);
685 PUT_AOUTHDR_ENTRY (abfd, aouthdr_in->entry, aouthdr_out->standard.entry);
277d1b5e 686 PUT_AOUTHDR_TEXT_START (abfd, aouthdr_in->text_start,
dc810e39 687 aouthdr_out->standard.text_start);
277d1b5e 688
99ad8390 689#if !defined(COFF_WITH_pep) && !defined(COFF_WITH_pex64)
c25cfdf8 690 /* PE32+ does not have data_start member! */
277d1b5e 691 PUT_AOUTHDR_DATA_START (abfd, aouthdr_in->data_start,
dc810e39 692 aouthdr_out->standard.data_start);
fac41780 693#endif
277d1b5e 694
dc810e39
AM
695 PUT_OPTHDR_IMAGE_BASE (abfd, extra->ImageBase, aouthdr_out->ImageBase);
696 H_PUT_32 (abfd, extra->SectionAlignment, aouthdr_out->SectionAlignment);
697 H_PUT_32 (abfd, extra->FileAlignment, aouthdr_out->FileAlignment);
698 H_PUT_16 (abfd, extra->MajorOperatingSystemVersion,
699 aouthdr_out->MajorOperatingSystemVersion);
700 H_PUT_16 (abfd, extra->MinorOperatingSystemVersion,
701 aouthdr_out->MinorOperatingSystemVersion);
702 H_PUT_16 (abfd, extra->MajorImageVersion, aouthdr_out->MajorImageVersion);
703 H_PUT_16 (abfd, extra->MinorImageVersion, aouthdr_out->MinorImageVersion);
704 H_PUT_16 (abfd, extra->MajorSubsystemVersion,
705 aouthdr_out->MajorSubsystemVersion);
706 H_PUT_16 (abfd, extra->MinorSubsystemVersion,
707 aouthdr_out->MinorSubsystemVersion);
708 H_PUT_32 (abfd, extra->Reserved1, aouthdr_out->Reserved1);
709 H_PUT_32 (abfd, extra->SizeOfImage, aouthdr_out->SizeOfImage);
710 H_PUT_32 (abfd, extra->SizeOfHeaders, aouthdr_out->SizeOfHeaders);
711 H_PUT_32 (abfd, extra->CheckSum, aouthdr_out->CheckSum);
712 H_PUT_16 (abfd, extra->Subsystem, aouthdr_out->Subsystem);
713 H_PUT_16 (abfd, extra->DllCharacteristics, aouthdr_out->DllCharacteristics);
fac41780 714 PUT_OPTHDR_SIZE_OF_STACK_RESERVE (abfd, extra->SizeOfStackReserve,
dc810e39 715 aouthdr_out->SizeOfStackReserve);
fac41780 716 PUT_OPTHDR_SIZE_OF_STACK_COMMIT (abfd, extra->SizeOfStackCommit,
dc810e39 717 aouthdr_out->SizeOfStackCommit);
fac41780 718 PUT_OPTHDR_SIZE_OF_HEAP_RESERVE (abfd, extra->SizeOfHeapReserve,
dc810e39 719 aouthdr_out->SizeOfHeapReserve);
fac41780 720 PUT_OPTHDR_SIZE_OF_HEAP_COMMIT (abfd, extra->SizeOfHeapCommit,
dc810e39
AM
721 aouthdr_out->SizeOfHeapCommit);
722 H_PUT_32 (abfd, extra->LoaderFlags, aouthdr_out->LoaderFlags);
723 H_PUT_32 (abfd, extra->NumberOfRvaAndSizes,
724 aouthdr_out->NumberOfRvaAndSizes);
277d1b5e
ILT
725 {
726 int idx;
1725a96e 727
6fa957a9 728 for (idx = 0; idx < 16; idx++)
277d1b5e 729 {
dc810e39
AM
730 H_PUT_32 (abfd, extra->DataDirectory[idx].VirtualAddress,
731 aouthdr_out->DataDirectory[idx][0]);
732 H_PUT_32 (abfd, extra->DataDirectory[idx].Size,
733 aouthdr_out->DataDirectory[idx][1]);
277d1b5e
ILT
734 }
735 }
736
737 return AOUTSZ;
738}
739
740unsigned int
7920ce38 741_bfd_XXi_only_swap_filehdr_out (bfd * abfd, void * in, void * out)
277d1b5e
ILT
742{
743 int idx;
6fa957a9
KH
744 struct internal_filehdr *filehdr_in = (struct internal_filehdr *) in;
745 struct external_PEI_filehdr *filehdr_out = (struct external_PEI_filehdr *) out;
277d1b5e 746
441f34fa
L
747 if (pe_data (abfd)->has_reloc_section
748 || pe_data (abfd)->dont_strip_reloc)
277d1b5e
ILT
749 filehdr_in->f_flags &= ~F_RELFLG;
750
751 if (pe_data (abfd)->dll)
752 filehdr_in->f_flags |= F_DLL;
753
754 filehdr_in->pe.e_magic = DOSMAGIC;
755 filehdr_in->pe.e_cblp = 0x90;
756 filehdr_in->pe.e_cp = 0x3;
757 filehdr_in->pe.e_crlc = 0x0;
758 filehdr_in->pe.e_cparhdr = 0x4;
759 filehdr_in->pe.e_minalloc = 0x0;
760 filehdr_in->pe.e_maxalloc = 0xffff;
761 filehdr_in->pe.e_ss = 0x0;
762 filehdr_in->pe.e_sp = 0xb8;
763 filehdr_in->pe.e_csum = 0x0;
764 filehdr_in->pe.e_ip = 0x0;
765 filehdr_in->pe.e_cs = 0x0;
766 filehdr_in->pe.e_lfarlc = 0x40;
767 filehdr_in->pe.e_ovno = 0x0;
768
6fa957a9 769 for (idx = 0; idx < 4; idx++)
277d1b5e
ILT
770 filehdr_in->pe.e_res[idx] = 0x0;
771
772 filehdr_in->pe.e_oemid = 0x0;
773 filehdr_in->pe.e_oeminfo = 0x0;
774
6fa957a9 775 for (idx = 0; idx < 10; idx++)
277d1b5e
ILT
776 filehdr_in->pe.e_res2[idx] = 0x0;
777
778 filehdr_in->pe.e_lfanew = 0x80;
779
6fa957a9
KH
780 /* This next collection of data are mostly just characters. It
781 appears to be constant within the headers put on NT exes. */
277d1b5e
ILT
782 filehdr_in->pe.dos_message[0] = 0x0eba1f0e;
783 filehdr_in->pe.dos_message[1] = 0xcd09b400;
784 filehdr_in->pe.dos_message[2] = 0x4c01b821;
785 filehdr_in->pe.dos_message[3] = 0x685421cd;
786 filehdr_in->pe.dos_message[4] = 0x70207369;
787 filehdr_in->pe.dos_message[5] = 0x72676f72;
788 filehdr_in->pe.dos_message[6] = 0x63206d61;
789 filehdr_in->pe.dos_message[7] = 0x6f6e6e61;
790 filehdr_in->pe.dos_message[8] = 0x65622074;
791 filehdr_in->pe.dos_message[9] = 0x6e757220;
792 filehdr_in->pe.dos_message[10] = 0x206e6920;
793 filehdr_in->pe.dos_message[11] = 0x20534f44;
794 filehdr_in->pe.dos_message[12] = 0x65646f6d;
795 filehdr_in->pe.dos_message[13] = 0x0a0d0d2e;
796 filehdr_in->pe.dos_message[14] = 0x24;
797 filehdr_in->pe.dos_message[15] = 0x0;
798 filehdr_in->pe.nt_signature = NT_SIGNATURE;
799
dc810e39
AM
800 H_PUT_16 (abfd, filehdr_in->f_magic, filehdr_out->f_magic);
801 H_PUT_16 (abfd, filehdr_in->f_nscns, filehdr_out->f_nscns);
277d1b5e 802
0cb112f7
CF
803 /* Only use a real timestamp if the option was chosen. */
804 if ((pe_data (abfd)->insert_timestamp))
805 H_PUT_32 (abfd, time(0), filehdr_out->f_timdat);
806
dc810e39
AM
807 PUT_FILEHDR_SYMPTR (abfd, filehdr_in->f_symptr,
808 filehdr_out->f_symptr);
809 H_PUT_32 (abfd, filehdr_in->f_nsyms, filehdr_out->f_nsyms);
810 H_PUT_16 (abfd, filehdr_in->f_opthdr, filehdr_out->f_opthdr);
811 H_PUT_16 (abfd, filehdr_in->f_flags, filehdr_out->f_flags);
277d1b5e 812
1725a96e 813 /* Put in extra dos header stuff. This data remains essentially
277d1b5e 814 constant, it just has to be tacked on to the beginning of all exes
1725a96e 815 for NT. */
dc810e39
AM
816 H_PUT_16 (abfd, filehdr_in->pe.e_magic, filehdr_out->e_magic);
817 H_PUT_16 (abfd, filehdr_in->pe.e_cblp, filehdr_out->e_cblp);
818 H_PUT_16 (abfd, filehdr_in->pe.e_cp, filehdr_out->e_cp);
819 H_PUT_16 (abfd, filehdr_in->pe.e_crlc, filehdr_out->e_crlc);
820 H_PUT_16 (abfd, filehdr_in->pe.e_cparhdr, filehdr_out->e_cparhdr);
821 H_PUT_16 (abfd, filehdr_in->pe.e_minalloc, filehdr_out->e_minalloc);
822 H_PUT_16 (abfd, filehdr_in->pe.e_maxalloc, filehdr_out->e_maxalloc);
823 H_PUT_16 (abfd, filehdr_in->pe.e_ss, filehdr_out->e_ss);
824 H_PUT_16 (abfd, filehdr_in->pe.e_sp, filehdr_out->e_sp);
825 H_PUT_16 (abfd, filehdr_in->pe.e_csum, filehdr_out->e_csum);
826 H_PUT_16 (abfd, filehdr_in->pe.e_ip, filehdr_out->e_ip);
827 H_PUT_16 (abfd, filehdr_in->pe.e_cs, filehdr_out->e_cs);
828 H_PUT_16 (abfd, filehdr_in->pe.e_lfarlc, filehdr_out->e_lfarlc);
829 H_PUT_16 (abfd, filehdr_in->pe.e_ovno, filehdr_out->e_ovno);
1725a96e
NC
830
831 for (idx = 0; idx < 4; idx++)
dc810e39 832 H_PUT_16 (abfd, filehdr_in->pe.e_res[idx], filehdr_out->e_res[idx]);
1725a96e 833
dc810e39
AM
834 H_PUT_16 (abfd, filehdr_in->pe.e_oemid, filehdr_out->e_oemid);
835 H_PUT_16 (abfd, filehdr_in->pe.e_oeminfo, filehdr_out->e_oeminfo);
1725a96e
NC
836
837 for (idx = 0; idx < 10; idx++)
dc810e39 838 H_PUT_16 (abfd, filehdr_in->pe.e_res2[idx], filehdr_out->e_res2[idx]);
1725a96e 839
dc810e39 840 H_PUT_32 (abfd, filehdr_in->pe.e_lfanew, filehdr_out->e_lfanew);
277d1b5e 841
1725a96e 842 for (idx = 0; idx < 16; idx++)
dc810e39
AM
843 H_PUT_32 (abfd, filehdr_in->pe.dos_message[idx],
844 filehdr_out->dos_message[idx]);
277d1b5e 845
6fa957a9 846 /* Also put in the NT signature. */
dc810e39 847 H_PUT_32 (abfd, filehdr_in->pe.nt_signature, filehdr_out->nt_signature);
277d1b5e 848
277d1b5e
ILT
849 return FILHSZ;
850}
851
852unsigned int
7920ce38 853_bfd_XX_only_swap_filehdr_out (bfd * abfd, void * in, void * out)
277d1b5e 854{
6fa957a9
KH
855 struct internal_filehdr *filehdr_in = (struct internal_filehdr *) in;
856 FILHDR *filehdr_out = (FILHDR *) out;
277d1b5e 857
dc810e39
AM
858 H_PUT_16 (abfd, filehdr_in->f_magic, filehdr_out->f_magic);
859 H_PUT_16 (abfd, filehdr_in->f_nscns, filehdr_out->f_nscns);
860 H_PUT_32 (abfd, filehdr_in->f_timdat, filehdr_out->f_timdat);
861 PUT_FILEHDR_SYMPTR (abfd, filehdr_in->f_symptr, filehdr_out->f_symptr);
862 H_PUT_32 (abfd, filehdr_in->f_nsyms, filehdr_out->f_nsyms);
863 H_PUT_16 (abfd, filehdr_in->f_opthdr, filehdr_out->f_opthdr);
864 H_PUT_16 (abfd, filehdr_in->f_flags, filehdr_out->f_flags);
277d1b5e
ILT
865
866 return FILHSZ;
867}
868
869unsigned int
7920ce38 870_bfd_XXi_swap_scnhdr_out (bfd * abfd, void * in, void * out)
277d1b5e 871{
6fa957a9
KH
872 struct internal_scnhdr *scnhdr_int = (struct internal_scnhdr *) in;
873 SCNHDR *scnhdr_ext = (SCNHDR *) out;
277d1b5e
ILT
874 unsigned int ret = SCNHSZ;
875 bfd_vma ps;
876 bfd_vma ss;
877
6fa957a9 878 memcpy (scnhdr_ext->s_name, scnhdr_int->s_name, sizeof (scnhdr_int->s_name));
277d1b5e
ILT
879
880 PUT_SCNHDR_VADDR (abfd,
9602af51 881 ((scnhdr_int->s_vaddr
6fa957a9 882 - pe_data (abfd)->pe_opthdr.ImageBase)
277d1b5e 883 & 0xffffffff),
dc810e39 884 scnhdr_ext->s_vaddr);
277d1b5e 885
5933bdc9
ILT
886 /* NT wants the size data to be rounded up to the next
887 NT_FILE_ALIGNMENT, but zero if it has no content (as in .bss,
888 sometimes). */
5933bdc9 889 if ((scnhdr_int->s_flags & IMAGE_SCN_CNT_UNINITIALIZED_DATA) != 0)
277d1b5e 890 {
92dd4511 891 if (bfd_pei_p (abfd))
ff0c9faf
NC
892 {
893 ps = scnhdr_int->s_size;
894 ss = 0;
895 }
896 else
897 {
898 ps = 0;
899 ss = scnhdr_int->s_size;
900 }
277d1b5e
ILT
901 }
902 else
903 {
92dd4511 904 if (bfd_pei_p (abfd))
ff0c9faf
NC
905 ps = scnhdr_int->s_paddr;
906 else
907 ps = 0;
908
277d1b5e
ILT
909 ss = scnhdr_int->s_size;
910 }
911
912 PUT_SCNHDR_SIZE (abfd, ss,
dc810e39 913 scnhdr_ext->s_size);
277d1b5e 914
5933bdc9 915 /* s_paddr in PE is really the virtual size. */
dc810e39 916 PUT_SCNHDR_PADDR (abfd, ps, scnhdr_ext->s_paddr);
277d1b5e
ILT
917
918 PUT_SCNHDR_SCNPTR (abfd, scnhdr_int->s_scnptr,
dc810e39 919 scnhdr_ext->s_scnptr);
277d1b5e 920 PUT_SCNHDR_RELPTR (abfd, scnhdr_int->s_relptr,
dc810e39 921 scnhdr_ext->s_relptr);
277d1b5e 922 PUT_SCNHDR_LNNOPTR (abfd, scnhdr_int->s_lnnoptr,
dc810e39 923 scnhdr_ext->s_lnnoptr);
277d1b5e 924
277d1b5e 925 {
25c80428
NC
926 /* Extra flags must be set when dealing with PE. All sections should also
927 have the IMAGE_SCN_MEM_READ (0x40000000) flag set. In addition, the
928 .text section must have IMAGE_SCN_MEM_EXECUTE (0x20000000) and the data
929 sections (.idata, .data, .bss, .CRT) must have IMAGE_SCN_MEM_WRITE set
930 (this is especially important when dealing with the .idata section since
931 the addresses for routines from .dlls must be overwritten). If .reloc
932 section data is ever generated, we must add IMAGE_SCN_MEM_DISCARDABLE
933 (0x02000000). Also, the resource data should also be read and
934 writable. */
935
4e1fc599 936 /* FIXME: Alignment is also encoded in this field, at least on PPC and
25c80428
NC
937 ARM-WINCE. Although - how do we get the original alignment field
938 back ? */
939
940 typedef struct
941 {
942 const char * section_name;
943 unsigned long must_have;
944 }
945 pe_required_section_flags;
4e1fc599 946
25c80428
NC
947 pe_required_section_flags known_sections [] =
948 {
949 { ".arch", IMAGE_SCN_MEM_READ | IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_DISCARDABLE | IMAGE_SCN_ALIGN_8BYTES },
950 { ".bss", IMAGE_SCN_MEM_READ | IMAGE_SCN_CNT_UNINITIALIZED_DATA | IMAGE_SCN_MEM_WRITE },
951 { ".data", IMAGE_SCN_MEM_READ | IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_WRITE },
952 { ".edata", IMAGE_SCN_MEM_READ | IMAGE_SCN_CNT_INITIALIZED_DATA },
953 { ".idata", IMAGE_SCN_MEM_READ | IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_WRITE },
954 { ".pdata", IMAGE_SCN_MEM_READ | IMAGE_SCN_CNT_INITIALIZED_DATA },
955 { ".rdata", IMAGE_SCN_MEM_READ | IMAGE_SCN_CNT_INITIALIZED_DATA },
956 { ".reloc", IMAGE_SCN_MEM_READ | IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_DISCARDABLE },
957 { ".rsrc", IMAGE_SCN_MEM_READ | IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_WRITE },
958 { ".text" , IMAGE_SCN_MEM_READ | IMAGE_SCN_CNT_CODE | IMAGE_SCN_MEM_EXECUTE },
959 { ".tls", IMAGE_SCN_MEM_READ | IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_WRITE },
960 { ".xdata", IMAGE_SCN_MEM_READ | IMAGE_SCN_CNT_INITIALIZED_DATA },
961 { NULL, 0}
962 };
963
964 pe_required_section_flags * p;
1725a96e 965
66bed356
DS
966 /* We have defaulted to adding the IMAGE_SCN_MEM_WRITE flag, but now
967 we know exactly what this specific section wants so we remove it
968 and then allow the must_have field to add it back in if necessary.
969 However, we don't remove IMAGE_SCN_MEM_WRITE flag from .text if the
970 default WP_TEXT file flag has been cleared. WP_TEXT may be cleared
971 by ld --enable-auto-import (if auto-import is actually needed),
972 by ld --omagic, or by obcopy --writable-text. */
66bed356 973
25c80428
NC
974 for (p = known_sections; p->section_name; p++)
975 if (strcmp (scnhdr_int->s_name, p->section_name) == 0)
976 {
3c9d0484
DS
977 if (strcmp (scnhdr_int->s_name, ".text")
978 || (bfd_get_file_flags (abfd) & WP_TEXT))
d48bdb99
AM
979 scnhdr_int->s_flags &= ~IMAGE_SCN_MEM_WRITE;
980 scnhdr_int->s_flags |= p->must_have;
25c80428
NC
981 break;
982 }
983
d48bdb99 984 H_PUT_32 (abfd, scnhdr_int->s_flags, scnhdr_ext->s_flags);
277d1b5e
ILT
985 }
986
cb43721d 987 if (coff_data (abfd)->link_info
1049f94e 988 && ! coff_data (abfd)->link_info->relocatable
cb43721d
ILT
989 && ! coff_data (abfd)->link_info->shared
990 && strcmp (scnhdr_int->s_name, ".text") == 0)
277d1b5e 991 {
cb43721d 992 /* By inference from looking at MS output, the 32 bit field
7dee875e 993 which is the combination of the number_of_relocs and
cb43721d
ILT
994 number_of_linenos is used for the line number count in
995 executables. A 16-bit field won't do for cc1. The MS
996 document says that the number of relocs is zero for
997 executables, but the 17-th bit has been observed to be there.
998 Overflow is not an issue: a 4G-line program will overflow a
999 bunch of other fields long before this! */
dc810e39
AM
1000 H_PUT_16 (abfd, (scnhdr_int->s_nlnno & 0xffff), scnhdr_ext->s_nlnno);
1001 H_PUT_16 (abfd, (scnhdr_int->s_nlnno >> 16), scnhdr_ext->s_nreloc);
277d1b5e 1002 }
277d1b5e
ILT
1003 else
1004 {
cb43721d 1005 if (scnhdr_int->s_nlnno <= 0xffff)
dc810e39 1006 H_PUT_16 (abfd, scnhdr_int->s_nlnno, scnhdr_ext->s_nlnno);
cb43721d
ILT
1007 else
1008 {
1009 (*_bfd_error_handler) (_("%s: line number overflow: 0x%lx > 0xffff"),
1010 bfd_get_filename (abfd),
1011 scnhdr_int->s_nlnno);
1012 bfd_set_error (bfd_error_file_truncated);
dc810e39 1013 H_PUT_16 (abfd, 0xffff, scnhdr_ext->s_nlnno);
cb43721d
ILT
1014 ret = 0;
1015 }
1725a96e 1016
cd339148
NS
1017 /* Although we could encode 0xffff relocs here, we do not, to be
1018 consistent with other parts of bfd. Also it lets us warn, as
1019 we should never see 0xffff here w/o having the overflow flag
1020 set. */
1021 if (scnhdr_int->s_nreloc < 0xffff)
dc810e39 1022 H_PUT_16 (abfd, scnhdr_int->s_nreloc, scnhdr_ext->s_nreloc);
cb43721d
ILT
1023 else
1024 {
1725a96e 1025 /* PE can deal with large #s of relocs, but not here. */
dc810e39 1026 H_PUT_16 (abfd, 0xffff, scnhdr_ext->s_nreloc);
3e4554a2 1027 scnhdr_int->s_flags |= IMAGE_SCN_LNK_NRELOC_OVFL;
dc810e39 1028 H_PUT_32 (abfd, scnhdr_int->s_flags, scnhdr_ext->s_flags);
cb43721d 1029 }
277d1b5e
ILT
1030 }
1031 return ret;
1032}
1033
1725a96e 1034static char * dir_names[IMAGE_NUMBEROF_DIRECTORY_ENTRIES] =
7920ce38
NC
1035{
1036 N_("Export Directory [.edata (or where ever we found it)]"),
1037 N_("Import Directory [parts of .idata]"),
1038 N_("Resource Directory [.rsrc]"),
1039 N_("Exception Directory [.pdata]"),
1040 N_("Security Directory"),
1041 N_("Base Relocation Directory [.reloc]"),
1042 N_("Debug Directory"),
1043 N_("Description Directory"),
1044 N_("Special Directory"),
1045 N_("Thread Storage Directory [.tls]"),
1046 N_("Load Configuration Directory"),
1047 N_("Bound Import Directory"),
1048 N_("Import Address Table Directory"),
1049 N_("Delay Import Directory"),
6c73cbb1 1050 N_("CLR Runtime Header"),
7920ce38
NC
1051 N_("Reserved")
1052};
1725a96e 1053
277d1b5e
ILT
1054#ifdef POWERPC_LE_PE
1055/* The code for the PPC really falls in the "architecture dependent"
1056 category. However, it's not clear that anyone will ever care, so
1057 we're ignoring the issue for now; if/when PPC matters, some of this
1058 may need to go into peicode.h, or arguments passed to enable the
1059 PPC- specific code. */
1060#endif
1061
b34976b6 1062static bfd_boolean
7920ce38 1063pe_print_idata (bfd * abfd, void * vfile)
277d1b5e
ILT
1064{
1065 FILE *file = (FILE *) vfile;
a76b448c 1066 bfd_byte *data;
8181c403
AM
1067 asection *section;
1068 bfd_signed_vma adj;
277d1b5e
ILT
1069
1070#ifdef POWERPC_LE_PE
1071 asection *rel_section = bfd_get_section_by_name (abfd, ".reldata");
1072#endif
1073
a76b448c 1074 bfd_size_type datasize = 0;
277d1b5e 1075 bfd_size_type dataoff;
277d1b5e 1076 bfd_size_type i;
277d1b5e
ILT
1077 int onaline = 20;
1078
1079 pe_data_type *pe = pe_data (abfd);
1080 struct internal_extra_pe_aouthdr *extra = &pe->pe_opthdr;
1081
8181c403 1082 bfd_vma addr;
277d1b5e 1083
6c73cbb1 1084 addr = extra->DataDirectory[PE_IMPORT_TABLE].VirtualAddress;
277d1b5e 1085
6c73cbb1 1086 if (addr == 0 && extra->DataDirectory[PE_IMPORT_TABLE].Size == 0)
8181c403 1087 {
a76b448c
AM
1088 /* Maybe the extra header isn't there. Look for the section. */
1089 section = bfd_get_section_by_name (abfd, ".idata");
1090 if (section == NULL)
b34976b6 1091 return TRUE;
a76b448c
AM
1092
1093 addr = section->vma;
eea6121a 1094 datasize = section->size;
a76b448c 1095 if (datasize == 0)
b34976b6 1096 return TRUE;
8181c403 1097 }
a76b448c 1098 else
8181c403 1099 {
a76b448c
AM
1100 addr += extra->ImageBase;
1101 for (section = abfd->sections; section != NULL; section = section->next)
1102 {
eea6121a 1103 datasize = section->size;
a76b448c
AM
1104 if (addr >= section->vma && addr < section->vma + datasize)
1105 break;
1106 }
1107
1108 if (section == NULL)
1109 {
1110 fprintf (file,
1111 _("\nThere is an import table, but the section containing it could not be found\n"));
b34976b6 1112 return TRUE;
a76b448c 1113 }
b69c8728
JT
1114 else if (!(section->flags & SEC_HAS_CONTENTS))
1115 {
1116 fprintf (file,
1117 _("\nThere is an import table in %s, but that section has no contents\n"),
1118 section->name);
1119 return TRUE;
1120 }
8181c403 1121 }
5933bdc9 1122
8181c403
AM
1123 fprintf (file, _("\nThere is an import table in %s at 0x%lx\n"),
1124 section->name, (unsigned long) addr);
277d1b5e 1125
8181c403 1126 dataoff = addr - section->vma;
277d1b5e
ILT
1127
1128#ifdef POWERPC_LE_PE
eea6121a 1129 if (rel_section != 0 && rel_section->size != 0)
277d1b5e
ILT
1130 {
1131 /* The toc address can be found by taking the starting address,
1132 which on the PPC locates a function descriptor. The
1133 descriptor consists of the function code starting address
1134 followed by the address of the toc. The starting address we
1135 get from the bfd, and the descriptor is supposed to be in the
1136 .reldata section. */
1137
1138 bfd_vma loadable_toc_address;
1139 bfd_vma toc_address;
1140 bfd_vma start_address;
eea6121a 1141 bfd_byte *data;
a50b2160 1142 bfd_vma offset;
8181c403 1143
eea6121a
AM
1144 if (!bfd_malloc_and_get_section (abfd, rel_section, &data))
1145 {
1146 if (data != NULL)
1147 free (data);
1148 return FALSE;
1149 }
277d1b5e
ILT
1150
1151 offset = abfd->start_address - rel_section->vma;
1152
a50b2160
JJ
1153 if (offset >= rel_section->size || offset + 8 > rel_section->size)
1154 {
1155 if (data != NULL)
1156 free (data);
1157 return FALSE;
1158 }
1159
db8503c4
AM
1160 start_address = bfd_get_32 (abfd, data + offset);
1161 loadable_toc_address = bfd_get_32 (abfd, data + offset + 4);
277d1b5e
ILT
1162 toc_address = loadable_toc_address - 32768;
1163
9602af51 1164 fprintf (file,
6fa957a9
KH
1165 _("\nFunction descriptor located at the start address: %04lx\n"),
1166 (unsigned long int) (abfd->start_address));
277d1b5e
ILT
1167 fprintf (file,
1168 _("\tcode-base %08lx toc (loadable/actual) %08lx/%08lx\n"),
1169 start_address, loadable_toc_address, toc_address);
eea6121a
AM
1170 if (data != NULL)
1171 free (data);
277d1b5e
ILT
1172 }
1173 else
1174 {
9602af51 1175 fprintf (file,
6fa957a9 1176 _("\nNo reldata section! Function descriptor not decoded.\n"));
277d1b5e
ILT
1177 }
1178#endif
1179
9602af51 1180 fprintf (file,
6fa957a9
KH
1181 _("\nThe Import Tables (interpreted %s section contents)\n"),
1182 section->name);
9602af51 1183 fprintf (file,
ca09e32b
NC
1184 _("\
1185 vma: Hint Time Forward DLL First\n\
1186 Table Stamp Chain Name Thunk\n"));
277d1b5e 1187
db8503c4 1188 /* Read the whole section. Some of the fields might be before dataoff. */
eea6121a
AM
1189 if (!bfd_malloc_and_get_section (abfd, section, &data))
1190 {
1191 if (data != NULL)
1192 free (data);
1193 return FALSE;
1194 }
277d1b5e 1195
db8503c4 1196 adj = section->vma - extra->ImageBase;
277d1b5e 1197
5e226794 1198 /* Print all image import descriptors. */
4e1fc599 1199 for (i = dataoff; i + onaline <= datasize; i += onaline)
277d1b5e
ILT
1200 {
1201 bfd_vma hint_addr;
1202 bfd_vma time_stamp;
1203 bfd_vma forward_chain;
1204 bfd_vma dll_name;
1205 bfd_vma first_thunk;
1206 int idx = 0;
1207 bfd_size_type j;
1208 char *dll;
1209
6c73cbb1 1210 /* Print (i + extra->DataDirectory[PE_IMPORT_TABLE].VirtualAddress). */
4e1fc599
AM
1211 fprintf (file, " %08lx\t", (unsigned long) (i + adj));
1212 hint_addr = bfd_get_32 (abfd, data + i);
1213 time_stamp = bfd_get_32 (abfd, data + i + 4);
1214 forward_chain = bfd_get_32 (abfd, data + i + 8);
1215 dll_name = bfd_get_32 (abfd, data + i + 12);
1216 first_thunk = bfd_get_32 (abfd, data + i + 16);
5933bdc9
ILT
1217
1218 fprintf (file, "%08lx %08lx %08lx %08lx %08lx\n",
a76b448c
AM
1219 (unsigned long) hint_addr,
1220 (unsigned long) time_stamp,
1221 (unsigned long) forward_chain,
1222 (unsigned long) dll_name,
1223 (unsigned long) first_thunk);
277d1b5e
ILT
1224
1225 if (hint_addr == 0 && first_thunk == 0)
1226 break;
1227
a50b2160
JJ
1228 if (dll_name - adj >= section->size)
1229 break;
1230
8181c403 1231 dll = (char *) data + dll_name - adj;
9602af51 1232 fprintf (file, _("\n\tDLL Name: %s\n"), dll);
277d1b5e
ILT
1233
1234 if (hint_addr != 0)
1235 {
6e7c73dd
CF
1236 bfd_byte *ft_data;
1237 asection *ft_section;
1238 bfd_vma ft_addr;
1239 bfd_size_type ft_datasize;
1240 int ft_idx;
4e1fc599 1241 int ft_allocated;
6e7c73dd 1242
5e226794 1243 fprintf (file, _("\tvma: Hint/Ord Member-Name Bound-To\n"));
277d1b5e 1244
8181c403 1245 idx = hint_addr - adj;
4e1fc599 1246
5e226794 1247 ft_addr = first_thunk + extra->ImageBase;
6e7c73dd 1248 ft_idx = first_thunk - adj;
4e1fc599
AM
1249 ft_data = data + ft_idx;
1250 ft_datasize = datasize - ft_idx;
1251 ft_allocated = 0;
6c73cbb1
NC
1252
1253 if (first_thunk != hint_addr)
6e7c73dd
CF
1254 {
1255 /* Find the section which contains the first thunk. */
1256 for (ft_section = abfd->sections;
1257 ft_section != NULL;
1258 ft_section = ft_section->next)
1259 {
6e7c73dd 1260 if (ft_addr >= ft_section->vma
4e1fc599 1261 && ft_addr < ft_section->vma + ft_section->size)
6e7c73dd
CF
1262 break;
1263 }
1264
1265 if (ft_section == NULL)
1266 {
1267 fprintf (file,
1268 _("\nThere is a first thunk, but the section containing it could not be found\n"));
1269 continue;
1270 }
1271
1272 /* Now check to see if this section is the same as our current
1273 section. If it is not then we will have to load its data in. */
4e1fc599 1274 if (ft_section != section)
6e7c73dd
CF
1275 {
1276 ft_idx = first_thunk - (ft_section->vma - extra->ImageBase);
4e1fc599
AM
1277 ft_datasize = ft_section->size - ft_idx;
1278 ft_data = (bfd_byte *) bfd_malloc (ft_datasize);
6e7c73dd
CF
1279 if (ft_data == NULL)
1280 continue;
1281
4e1fc599
AM
1282 /* Read ft_datasize bytes starting at offset ft_idx. */
1283 if (!bfd_get_section_contents (abfd, ft_section, ft_data,
1284 (bfd_vma) ft_idx, ft_datasize))
6e7c73dd
CF
1285 {
1286 free (ft_data);
1287 continue;
1288 }
6e7c73dd
CF
1289 ft_allocated = 1;
1290 }
1291 }
5e226794
NC
1292
1293 /* Print HintName vector entries. */
99ad8390 1294#ifdef COFF_WITH_pex64
4e1fc599 1295 for (j = 0; idx + j + 8 <= datasize; j += 8)
99ad8390
NC
1296 {
1297 unsigned long member = bfd_get_32 (abfd, data + idx + j);
1298 unsigned long member_high = bfd_get_32 (abfd, data + idx + j + 4);
1299
1300 if (!member && !member_high)
1301 break;
1302
5879bb8f 1303 if (HighBitSet (member_high))
99ad8390 1304 fprintf (file, "\t%lx%08lx\t %4lx%08lx <none>",
5879bb8f
NC
1305 member_high, member,
1306 WithoutHighBit (member_high), member);
99ad8390
NC
1307 else
1308 {
1309 int ordinal;
1310 char *member_name;
1311
1312 ordinal = bfd_get_16 (abfd, data + member - adj);
1313 member_name = (char *) data + member - adj + 2;
1314 fprintf (file, "\t%04lx\t %4d %s",member, ordinal, member_name);
1315 }
1316
1317 /* If the time stamp is not zero, the import address
1318 table holds actual addresses. */
1319 if (time_stamp != 0
1320 && first_thunk != 0
4e1fc599
AM
1321 && first_thunk != hint_addr
1322 && j + 4 <= ft_datasize)
99ad8390 1323 fprintf (file, "\t%04lx",
4e1fc599 1324 (unsigned long) bfd_get_32 (abfd, ft_data + j));
99ad8390
NC
1325 fprintf (file, "\n");
1326 }
1327#else
4e1fc599 1328 for (j = 0; idx + j + 4 <= datasize; j += 4)
277d1b5e
ILT
1329 {
1330 unsigned long member = bfd_get_32 (abfd, data + idx + j);
1331
4e1fc599 1332 /* Print single IMAGE_IMPORT_BY_NAME vector. */
277d1b5e
ILT
1333 if (member == 0)
1334 break;
5e226794 1335
5879bb8f 1336 if (HighBitSet (member))
5e226794 1337 fprintf (file, "\t%04lx\t %4lu <none>",
5879bb8f 1338 member, WithoutHighBit (member));
277d1b5e
ILT
1339 else
1340 {
1341 int ordinal;
1342 char *member_name;
1343
8181c403
AM
1344 ordinal = bfd_get_16 (abfd, data + member - adj);
1345 member_name = (char *) data + member - adj + 2;
277d1b5e
ILT
1346 fprintf (file, "\t%04lx\t %4d %s",
1347 member, ordinal, member_name);
1348 }
5e226794 1349
277d1b5e 1350 /* If the time stamp is not zero, the import address
5e226794
NC
1351 table holds actual addresses. */
1352 if (time_stamp != 0
1353 && first_thunk != 0
4e1fc599
AM
1354 && first_thunk != hint_addr
1355 && j + 4 <= ft_datasize)
277d1b5e 1356 fprintf (file, "\t%04lx",
4e1fc599 1357 (unsigned long) bfd_get_32 (abfd, ft_data + j));
277d1b5e
ILT
1358
1359 fprintf (file, "\n");
1360 }
99ad8390 1361#endif
e4cf60a8
NC
1362 if (ft_allocated)
1363 free (ft_data);
277d1b5e
ILT
1364 }
1365
9602af51 1366 fprintf (file, "\n");
277d1b5e
ILT
1367 }
1368
1369 free (data);
1370
b34976b6 1371 return TRUE;
277d1b5e
ILT
1372}
1373
b34976b6 1374static bfd_boolean
7920ce38 1375pe_print_edata (bfd * abfd, void * vfile)
277d1b5e
ILT
1376{
1377 FILE *file = (FILE *) vfile;
a76b448c 1378 bfd_byte *data;
8181c403 1379 asection *section;
a76b448c 1380 bfd_size_type datasize = 0;
277d1b5e
ILT
1381 bfd_size_type dataoff;
1382 bfd_size_type i;
b69c8728 1383 bfd_vma adj;
1725a96e
NC
1384 struct EDT_type
1385 {
7920ce38 1386 long export_flags; /* Reserved - should be zero. */
6fa957a9
KH
1387 long time_stamp;
1388 short major_ver;
1389 short minor_ver;
7920ce38
NC
1390 bfd_vma name; /* RVA - relative to image base. */
1391 long base; /* Ordinal base. */
1392 unsigned long num_functions;/* Number in the export address table. */
1393 unsigned long num_names; /* Number in the name pointer table. */
1394 bfd_vma eat_addr; /* RVA to the export address table. */
1395 bfd_vma npt_addr; /* RVA to the Export Name Pointer Table. */
1396 bfd_vma ot_addr; /* RVA to the Ordinal Table. */
6fa957a9 1397 } edt;
277d1b5e
ILT
1398
1399 pe_data_type *pe = pe_data (abfd);
1400 struct internal_extra_pe_aouthdr *extra = &pe->pe_opthdr;
1401
8181c403 1402 bfd_vma addr;
277d1b5e 1403
6c73cbb1 1404 addr = extra->DataDirectory[PE_EXPORT_TABLE].VirtualAddress;
277d1b5e 1405
6c73cbb1 1406 if (addr == 0 && extra->DataDirectory[PE_EXPORT_TABLE].Size == 0)
8181c403 1407 {
a76b448c
AM
1408 /* Maybe the extra header isn't there. Look for the section. */
1409 section = bfd_get_section_by_name (abfd, ".edata");
1410 if (section == NULL)
b34976b6 1411 return TRUE;
a76b448c
AM
1412
1413 addr = section->vma;
0facbdf5 1414 dataoff = 0;
eea6121a 1415 datasize = section->size;
a76b448c 1416 if (datasize == 0)
b34976b6 1417 return TRUE;
8181c403 1418 }
a76b448c 1419 else
8181c403 1420 {
a76b448c 1421 addr += extra->ImageBase;
1725a96e 1422
a76b448c 1423 for (section = abfd->sections; section != NULL; section = section->next)
0facbdf5
NC
1424 if (addr >= section->vma && addr < section->vma + section->size)
1425 break;
a76b448c
AM
1426
1427 if (section == NULL)
1428 {
1429 fprintf (file,
1430 _("\nThere is an export table, but the section containing it could not be found\n"));
b34976b6 1431 return TRUE;
a76b448c 1432 }
b69c8728
JT
1433 else if (!(section->flags & SEC_HAS_CONTENTS))
1434 {
1435 fprintf (file,
1436 _("\nThere is an export table in %s, but that section has no contents\n"),
1437 section->name);
1438 return TRUE;
1439 }
0facbdf5
NC
1440
1441 dataoff = addr - section->vma;
6c73cbb1 1442 datasize = extra->DataDirectory[PE_EXPORT_TABLE].Size;
0facbdf5
NC
1443 if (datasize > section->size - dataoff)
1444 {
1445 fprintf (file,
1446 _("\nThere is an export table in %s, but it does not fit into that section\n"),
1447 section->name);
1448 return TRUE;
1449 }
277d1b5e
ILT
1450 }
1451
8181c403
AM
1452 fprintf (file, _("\nThere is an export table in %s at 0x%lx\n"),
1453 section->name, (unsigned long) addr);
1454
a50b1753 1455 data = (bfd_byte *) bfd_malloc (datasize);
8181c403 1456 if (data == NULL)
b34976b6 1457 return FALSE;
277d1b5e 1458
7920ce38 1459 if (! bfd_get_section_contents (abfd, section, data,
dc810e39 1460 (file_ptr) dataoff, datasize))
b34976b6 1461 return FALSE;
277d1b5e 1462
6fa957a9
KH
1463 /* Go get Export Directory Table. */
1464 edt.export_flags = bfd_get_32 (abfd, data + 0);
1465 edt.time_stamp = bfd_get_32 (abfd, data + 4);
1466 edt.major_ver = bfd_get_16 (abfd, data + 8);
1467 edt.minor_ver = bfd_get_16 (abfd, data + 10);
1468 edt.name = bfd_get_32 (abfd, data + 12);
1469 edt.base = bfd_get_32 (abfd, data + 16);
1470 edt.num_functions = bfd_get_32 (abfd, data + 20);
1471 edt.num_names = bfd_get_32 (abfd, data + 24);
1472 edt.eat_addr = bfd_get_32 (abfd, data + 28);
1473 edt.npt_addr = bfd_get_32 (abfd, data + 32);
1474 edt.ot_addr = bfd_get_32 (abfd, data + 36);
277d1b5e 1475
8181c403 1476 adj = section->vma - extra->ImageBase + dataoff;
277d1b5e 1477
1725a96e 1478 /* Dump the EDT first. */
9602af51 1479 fprintf (file,
6fa957a9
KH
1480 _("\nThe Export Tables (interpreted %s section contents)\n\n"),
1481 section->name);
277d1b5e 1482
9602af51 1483 fprintf (file,
6fa957a9 1484 _("Export Flags \t\t\t%lx\n"), (unsigned long) edt.export_flags);
277d1b5e 1485
9602af51 1486 fprintf (file,
6fa957a9 1487 _("Time/Date stamp \t\t%lx\n"), (unsigned long) edt.time_stamp);
277d1b5e 1488
9602af51 1489 fprintf (file,
6fa957a9 1490 _("Major/Minor \t\t\t%d/%d\n"), edt.major_ver, edt.minor_ver);
277d1b5e
ILT
1491
1492 fprintf (file,
1493 _("Name \t\t\t\t"));
ebf12fbe 1494 bfd_fprintf_vma (abfd, file, edt.name);
b69c8728
JT
1495
1496 if ((edt.name >= adj) && (edt.name < adj + datasize))
1497 fprintf (file, " %s\n", data + edt.name - adj);
1498 else
1499 fprintf (file, "(outside .edata section)\n");
277d1b5e 1500
9602af51 1501 fprintf (file,
6fa957a9 1502 _("Ordinal Base \t\t\t%ld\n"), edt.base);
277d1b5e 1503
9602af51 1504 fprintf (file,
6fa957a9 1505 _("Number in:\n"));
277d1b5e 1506
9602af51 1507 fprintf (file,
6fa957a9
KH
1508 _("\tExport Address Table \t\t%08lx\n"),
1509 edt.num_functions);
277d1b5e 1510
9602af51 1511 fprintf (file,
6fa957a9 1512 _("\t[Name Pointer/Ordinal] Table\t%08lx\n"), edt.num_names);
277d1b5e 1513
9602af51 1514 fprintf (file,
6fa957a9 1515 _("Table Addresses\n"));
277d1b5e
ILT
1516
1517 fprintf (file,
1518 _("\tExport Address Table \t\t"));
ebf12fbe 1519 bfd_fprintf_vma (abfd, file, edt.eat_addr);
277d1b5e
ILT
1520 fprintf (file, "\n");
1521
1522 fprintf (file,
6fa957a9 1523 _("\tName Pointer Table \t\t"));
ebf12fbe 1524 bfd_fprintf_vma (abfd, file, edt.npt_addr);
277d1b5e
ILT
1525 fprintf (file, "\n");
1526
1527 fprintf (file,
1528 _("\tOrdinal Table \t\t\t"));
ebf12fbe 1529 bfd_fprintf_vma (abfd, file, edt.ot_addr);
277d1b5e
ILT
1530 fprintf (file, "\n");
1531
5933bdc9 1532 /* The next table to find is the Export Address Table. It's basically
277d1b5e
ILT
1533 a list of pointers that either locate a function in this dll, or
1534 forward the call to another dll. Something like:
1725a96e
NC
1535 typedef union
1536 {
277d1b5e
ILT
1537 long export_rva;
1538 long forwarder_rva;
7920ce38 1539 } export_address_table_entry; */
277d1b5e 1540
9602af51 1541 fprintf (file,
277d1b5e
ILT
1542 _("\nExport Address Table -- Ordinal Base %ld\n"),
1543 edt.base);
1544
1545 for (i = 0; i < edt.num_functions; ++i)
1546 {
1547 bfd_vma eat_member = bfd_get_32 (abfd,
8181c403 1548 data + edt.eat_addr + (i * 4) - adj);
277d1b5e
ILT
1549 if (eat_member == 0)
1550 continue;
1551
db8503c4 1552 if (eat_member - adj <= datasize)
277d1b5e 1553 {
db8503c4 1554 /* This rva is to a name (forwarding function) in our section. */
6fa957a9 1555 /* Should locate a function descriptor. */
5933bdc9
ILT
1556 fprintf (file,
1557 "\t[%4ld] +base[%4ld] %04lx %s -- %s\n",
a76b448c
AM
1558 (long) i,
1559 (long) (i + edt.base),
1560 (unsigned long) eat_member,
1561 _("Forwarder RVA"),
1562 data + eat_member - adj);
277d1b5e
ILT
1563 }
1564 else
1565 {
6fa957a9 1566 /* Should locate a function descriptor in the reldata section. */
5933bdc9
ILT
1567 fprintf (file,
1568 "\t[%4ld] +base[%4ld] %04lx %s\n",
a76b448c
AM
1569 (long) i,
1570 (long) (i + edt.base),
1571 (unsigned long) eat_member,
5933bdc9 1572 _("Export RVA"));
277d1b5e
ILT
1573 }
1574 }
1575
6fa957a9
KH
1576 /* The Export Name Pointer Table is paired with the Export Ordinal Table. */
1577 /* Dump them in parallel for clarity. */
9602af51 1578 fprintf (file,
6fa957a9 1579 _("\n[Ordinal/Name Pointer] Table\n"));
277d1b5e
ILT
1580
1581 for (i = 0; i < edt.num_names; ++i)
1582 {
9602af51 1583 bfd_vma name_ptr = bfd_get_32 (abfd,
277d1b5e
ILT
1584 data +
1585 edt.npt_addr
8181c403 1586 + (i*4) - adj);
9602af51 1587
8181c403 1588 char *name = (char *) data + name_ptr - adj;
277d1b5e 1589
9602af51 1590 bfd_vma ord = bfd_get_16 (abfd,
277d1b5e
ILT
1591 data +
1592 edt.ot_addr
8181c403 1593 + (i*2) - adj);
9602af51 1594 fprintf (file,
277d1b5e 1595 "\t[%4ld] %s\n", (long) ord, name);
277d1b5e
ILT
1596 }
1597
1598 free (data);
1599
b34976b6 1600 return TRUE;
277d1b5e
ILT
1601}
1602
fac41780
JW
1603/* This really is architecture dependent. On IA-64, a .pdata entry
1604 consists of three dwords containing relative virtual addresses that
1605 specify the start and end address of the code range the entry
4e1fc599 1606 covers and the address of the corresponding unwind info data.
2b5c217d
NC
1607
1608 On ARM and SH-4, a compressed PDATA structure is used :
1609 _IMAGE_CE_RUNTIME_FUNCTION_ENTRY, whereas MIPS is documented to use
1610 _IMAGE_ALPHA_RUNTIME_FUNCTION_ENTRY.
1611 See http://msdn2.microsoft.com/en-us/library/ms253988(VS.80).aspx .
1612
799c00e0 1613 This is the version for uncompressed data. */
6fa957a9 1614
b34976b6 1615static bfd_boolean
7920ce38 1616pe_print_pdata (bfd * abfd, void * vfile)
277d1b5e 1617{
99ad8390
NC
1618#if defined(COFF_WITH_pep) && !defined(COFF_WITH_pex64)
1619# define PDATA_ROW_SIZE (3 * 8)
fac41780 1620#else
99ad8390 1621# define PDATA_ROW_SIZE (5 * 4)
fac41780 1622#endif
277d1b5e
ILT
1623 FILE *file = (FILE *) vfile;
1624 bfd_byte *data = 0;
1625 asection *section = bfd_get_section_by_name (abfd, ".pdata");
1626 bfd_size_type datasize = 0;
1627 bfd_size_type i;
1628 bfd_size_type start, stop;
fac41780 1629 int onaline = PDATA_ROW_SIZE;
277d1b5e 1630
5933bdc9
ILT
1631 if (section == NULL
1632 || coff_section_data (abfd, section) == NULL
1633 || pei_section_data (abfd, section) == NULL)
b34976b6 1634 return TRUE;
277d1b5e 1635
5933bdc9 1636 stop = pei_section_data (abfd, section)->virt_size;
277d1b5e 1637 if ((stop % onaline) != 0)
6fa957a9
KH
1638 fprintf (file,
1639 _("Warning, .pdata section size (%ld) is not a multiple of %d\n"),
1640 (long) stop, onaline);
277d1b5e 1641
5933bdc9
ILT
1642 fprintf (file,
1643 _("\nThe Function Table (interpreted .pdata section contents)\n"));
99ad8390 1644#if defined(COFF_WITH_pep) && !defined(COFF_WITH_pex64)
9602af51 1645 fprintf (file,
6fa957a9 1646 _(" vma:\t\t\tBegin Address End Address Unwind Info\n"));
fac41780 1647#else
ca09e32b
NC
1648 fprintf (file, _("\
1649 vma:\t\tBegin End EH EH PrologEnd Exception\n\
1650 \t\tAddress Address Handler Data Address Mask\n"));
fac41780 1651#endif
277d1b5e 1652
eea6121a 1653 datasize = section->size;
dc810e39 1654 if (datasize == 0)
b34976b6 1655 return TRUE;
277d1b5e 1656
7920ce38 1657 if (! bfd_malloc_and_get_section (abfd, section, &data))
eea6121a
AM
1658 {
1659 if (data != NULL)
1660 free (data);
1661 return FALSE;
1662 }
277d1b5e
ILT
1663
1664 start = 0;
1665
1666 for (i = start; i < stop; i += onaline)
1667 {
1668 bfd_vma begin_addr;
1669 bfd_vma end_addr;
1670 bfd_vma eh_handler;
1671 bfd_vma eh_data;
1672 bfd_vma prolog_end_addr;
c7e2358a 1673#if !defined(COFF_WITH_pep) || defined(COFF_WITH_pex64)
5933bdc9 1674 int em_data;
c7e2358a 1675#endif
277d1b5e 1676
fac41780 1677 if (i + PDATA_ROW_SIZE > stop)
277d1b5e 1678 break;
5933bdc9 1679
6fa957a9
KH
1680 begin_addr = GET_PDATA_ENTRY (abfd, data + i );
1681 end_addr = GET_PDATA_ENTRY (abfd, data + i + 4);
1682 eh_handler = GET_PDATA_ENTRY (abfd, data + i + 8);
1683 eh_data = GET_PDATA_ENTRY (abfd, data + i + 12);
1684 prolog_end_addr = GET_PDATA_ENTRY (abfd, data + i + 16);
9602af51 1685
277d1b5e
ILT
1686 if (begin_addr == 0 && end_addr == 0 && eh_handler == 0
1687 && eh_data == 0 && prolog_end_addr == 0)
1725a96e
NC
1688 /* We are probably into the padding of the section now. */
1689 break;
277d1b5e 1690
c7e2358a 1691#if !defined(COFF_WITH_pep) || defined(COFF_WITH_pex64)
5933bdc9 1692 em_data = ((eh_handler & 0x1) << 2) | (prolog_end_addr & 0x3);
c7e2358a 1693#endif
6fa957a9
KH
1694 eh_handler &= ~(bfd_vma) 0x3;
1695 prolog_end_addr &= ~(bfd_vma) 0x3;
fac41780
JW
1696
1697 fputc (' ', file);
ebf12fbe
DK
1698 bfd_fprintf_vma (abfd, file, i + section->vma); fputc ('\t', file);
1699 bfd_fprintf_vma (abfd, file, begin_addr); fputc (' ', file);
1700 bfd_fprintf_vma (abfd, file, end_addr); fputc (' ', file);
1701 bfd_fprintf_vma (abfd, file, eh_handler);
99ad8390 1702#if !defined(COFF_WITH_pep) || defined(COFF_WITH_pex64)
fac41780 1703 fputc (' ', file);
ebf12fbe
DK
1704 bfd_fprintf_vma (abfd, file, eh_data); fputc (' ', file);
1705 bfd_fprintf_vma (abfd, file, prolog_end_addr);
fac41780
JW
1706 fprintf (file, " %x", em_data);
1707#endif
277d1b5e
ILT
1708
1709#ifdef POWERPC_LE_PE
1710 if (eh_handler == 0 && eh_data != 0)
1711 {
6fa957a9 1712 /* Special bits here, although the meaning may be a little
7920ce38
NC
1713 mysterious. The only one I know for sure is 0x03
1714 Code Significance
1715 0x00 None
1716 0x01 Register Save Millicode
1717 0x02 Register Restore Millicode
1718 0x03 Glue Code Sequence. */
277d1b5e
ILT
1719 switch (eh_data)
1720 {
1721 case 0x01:
9602af51 1722 fprintf (file, _(" Register save millicode"));
277d1b5e
ILT
1723 break;
1724 case 0x02:
9602af51 1725 fprintf (file, _(" Register restore millicode"));
277d1b5e
ILT
1726 break;
1727 case 0x03:
9602af51 1728 fprintf (file, _(" Glue code sequence"));
277d1b5e
ILT
1729 break;
1730 default:
1731 break;
1732 }
1733 }
1734#endif
9602af51 1735 fprintf (file, "\n");
277d1b5e
ILT
1736 }
1737
1738 free (data);
1739
b34976b6 1740 return TRUE;
2b5c217d 1741#undef PDATA_ROW_SIZE
277d1b5e
ILT
1742}
1743
799c00e0
NC
1744typedef struct sym_cache
1745{
1746 int symcount;
1747 asymbol ** syms;
1748} sym_cache;
1749
1750static asymbol **
1751slurp_symtab (bfd *abfd, sym_cache *psc)
1752{
1753 asymbol ** sy = NULL;
1754 long storage;
1755
1756 if (!(bfd_get_file_flags (abfd) & HAS_SYMS))
1757 {
1758 psc->symcount = 0;
1759 return NULL;
1760 }
1761
1762 storage = bfd_get_symtab_upper_bound (abfd);
1763 if (storage < 0)
1764 return NULL;
1765 if (storage)
a50b1753 1766 sy = (asymbol **) bfd_malloc (storage);
799c00e0
NC
1767
1768 psc->symcount = bfd_canonicalize_symtab (abfd, sy);
1769 if (psc->symcount < 0)
1770 return NULL;
1771 return sy;
1772}
1773
1774static const char *
1775my_symbol_for_address (bfd *abfd, bfd_vma func, sym_cache *psc)
1776{
1777 int i;
1778
1779 if (psc->syms == 0)
1780 psc->syms = slurp_symtab (abfd, psc);
1781
1782 for (i = 0; i < psc->symcount; i++)
1783 {
1784 if (psc->syms[i]->section->vma + psc->syms[i]->value == func)
1785 return psc->syms[i]->name;
1786 }
1787
1788 return NULL;
1789}
1790
1791static void
1792cleanup_syms (sym_cache *psc)
1793{
1794 psc->symcount = 0;
1795 free (psc->syms);
1796 psc->syms = NULL;
1797}
1798
1799/* This is the version for "compressed" pdata. */
1800
1801bfd_boolean
1802_bfd_XX_print_ce_compressed_pdata (bfd * abfd, void * vfile)
1803{
1804# define PDATA_ROW_SIZE (2 * 4)
1805 FILE *file = (FILE *) vfile;
1806 bfd_byte *data = NULL;
1807 asection *section = bfd_get_section_by_name (abfd, ".pdata");
1808 bfd_size_type datasize = 0;
1809 bfd_size_type i;
1810 bfd_size_type start, stop;
1811 int onaline = PDATA_ROW_SIZE;
91d6fa6a 1812 struct sym_cache cache = {0, 0} ;
799c00e0
NC
1813
1814 if (section == NULL
1815 || coff_section_data (abfd, section) == NULL
1816 || pei_section_data (abfd, section) == NULL)
1817 return TRUE;
1818
1819 stop = pei_section_data (abfd, section)->virt_size;
1820 if ((stop % onaline) != 0)
1821 fprintf (file,
1822 _("Warning, .pdata section size (%ld) is not a multiple of %d\n"),
1823 (long) stop, onaline);
1824
1825 fprintf (file,
1826 _("\nThe Function Table (interpreted .pdata section contents)\n"));
1827
1828 fprintf (file, _("\
1829 vma:\t\tBegin Prolog Function Flags Exception EH\n\
1830 \t\tAddress Length Length 32b exc Handler Data\n"));
1831
1832 datasize = section->size;
1833 if (datasize == 0)
1834 return TRUE;
1835
1836 if (! bfd_malloc_and_get_section (abfd, section, &data))
1837 {
1838 if (data != NULL)
1839 free (data);
1840 return FALSE;
1841 }
1842
1843 start = 0;
1844
1845 for (i = start; i < stop; i += onaline)
1846 {
1847 bfd_vma begin_addr;
1848 bfd_vma other_data;
1849 bfd_vma prolog_length, function_length;
1850 int flag32bit, exception_flag;
799c00e0
NC
1851 asection *tsection;
1852
1853 if (i + PDATA_ROW_SIZE > stop)
1854 break;
1855
1856 begin_addr = GET_PDATA_ENTRY (abfd, data + i );
1857 other_data = GET_PDATA_ENTRY (abfd, data + i + 4);
1858
1859 if (begin_addr == 0 && other_data == 0)
1860 /* We are probably into the padding of the section now. */
1861 break;
1862
1863 prolog_length = (other_data & 0x000000FF);
1864 function_length = (other_data & 0x3FFFFF00) >> 8;
1865 flag32bit = (int)((other_data & 0x40000000) >> 30);
1866 exception_flag = (int)((other_data & 0x80000000) >> 31);
1867
1868 fputc (' ', file);
ebf12fbe
DK
1869 bfd_fprintf_vma (abfd, file, i + section->vma); fputc ('\t', file);
1870 bfd_fprintf_vma (abfd, file, begin_addr); fputc (' ', file);
1871 bfd_fprintf_vma (abfd, file, prolog_length); fputc (' ', file);
1872 bfd_fprintf_vma (abfd, file, function_length); fputc (' ', file);
799c00e0
NC
1873 fprintf (file, "%2d %2d ", flag32bit, exception_flag);
1874
1875 /* Get the exception handler's address and the data passed from the
1876 .text section. This is really the data that belongs with the .pdata
1877 but got "compressed" out for the ARM and SH4 architectures. */
1878 tsection = bfd_get_section_by_name (abfd, ".text");
1879 if (tsection && coff_section_data (abfd, tsection)
1880 && pei_section_data (abfd, tsection))
1881 {
4e1fc599
AM
1882 bfd_vma eh_off = (begin_addr - 8) - tsection->vma;
1883 bfd_byte *tdata;
799c00e0 1884
4e1fc599
AM
1885 tdata = (bfd_byte *) bfd_malloc (8);
1886 if (tdata)
1887 {
1888 if (bfd_get_section_contents (abfd, tsection, tdata, eh_off, 8))
799c00e0
NC
1889 {
1890 bfd_vma eh, eh_data;
1891
1892 eh = bfd_get_32 (abfd, tdata);
1893 eh_data = bfd_get_32 (abfd, tdata + 4);
1894 fprintf (file, "%08x ", (unsigned int) eh);
1895 fprintf (file, "%08x", (unsigned int) eh_data);
1896 if (eh != 0)
1897 {
91d6fa6a 1898 const char *s = my_symbol_for_address (abfd, eh, &cache);
799c00e0
NC
1899
1900 if (s)
1901 fprintf (file, " (%s) ", s);
1902 }
1903 }
1904 free (tdata);
1905 }
799c00e0
NC
1906 }
1907
1908 fprintf (file, "\n");
1909 }
1910
1911 free (data);
1912
91d6fa6a 1913 cleanup_syms (& cache);
799c00e0
NC
1914
1915 return TRUE;
1916#undef PDATA_ROW_SIZE
1917}
c7c7219d 1918
799c00e0 1919\f
5933bdc9 1920#define IMAGE_REL_BASED_HIGHADJ 4
1725a96e 1921static const char * const tbl[] =
7920ce38
NC
1922{
1923 "ABSOLUTE",
1924 "HIGH",
1925 "LOW",
1926 "HIGHLOW",
1927 "HIGHADJ",
1928 "MIPS_JMPADDR",
1929 "SECTION",
1930 "REL32",
1931 "RESERVED1",
1932 "MIPS_JMPADDR16",
1933 "DIR64",
2bfd55ca 1934 "HIGH3ADJ",
7920ce38
NC
1935 "UNKNOWN", /* MUST be last. */
1936};
277d1b5e 1937
b34976b6 1938static bfd_boolean
7920ce38 1939pe_print_reloc (bfd * abfd, void * vfile)
277d1b5e
ILT
1940{
1941 FILE *file = (FILE *) vfile;
1942 bfd_byte *data = 0;
1943 asection *section = bfd_get_section_by_name (abfd, ".reloc");
277d1b5e
ILT
1944 bfd_size_type i;
1945 bfd_size_type start, stop;
1946
b69c8728 1947 if (section == NULL || section->size == 0 || !(section->flags & SEC_HAS_CONTENTS))
b34976b6 1948 return TRUE;
277d1b5e 1949
5933bdc9
ILT
1950 fprintf (file,
1951 _("\n\nPE File Base Relocations (interpreted .reloc section contents)\n"));
277d1b5e 1952
7920ce38 1953 if (! bfd_malloc_and_get_section (abfd, section, &data))
eea6121a
AM
1954 {
1955 if (data != NULL)
1956 free (data);
1957 return FALSE;
1958 }
277d1b5e
ILT
1959
1960 start = 0;
1961
eea6121a 1962 stop = section->size;
277d1b5e
ILT
1963
1964 for (i = start; i < stop;)
1965 {
1966 int j;
1967 bfd_vma virtual_address;
1968 long number, size;
1969
1970 /* The .reloc section is a sequence of blocks, with a header consisting
1725a96e 1971 of two 32 bit quantities, followed by a number of 16 bit entries. */
9602af51
KH
1972 virtual_address = bfd_get_32 (abfd, data+i);
1973 size = bfd_get_32 (abfd, data+i+4);
277d1b5e
ILT
1974 number = (size - 8) / 2;
1975
1976 if (size == 0)
1725a96e 1977 break;
277d1b5e
ILT
1978
1979 fprintf (file,
1980 _("\nVirtual Address: %08lx Chunk size %ld (0x%lx) Number of fixups %ld\n"),
0af1713e 1981 (unsigned long) virtual_address, size, (unsigned long) size, number);
277d1b5e
ILT
1982
1983 for (j = 0; j < number; ++j)
1984 {
5933bdc9
ILT
1985 unsigned short e = bfd_get_16 (abfd, data + i + 8 + j * 2);
1986 unsigned int t = (e & 0xF000) >> 12;
277d1b5e
ILT
1987 int off = e & 0x0FFF;
1988
5933bdc9
ILT
1989 if (t >= sizeof (tbl) / sizeof (tbl[0]))
1990 t = (sizeof (tbl) / sizeof (tbl[0])) - 1;
277d1b5e 1991
5933bdc9
ILT
1992 fprintf (file,
1993 _("\treloc %4d offset %4x [%4lx] %s"),
0af1713e 1994 j, off, (unsigned long) (off + virtual_address), tbl[t]);
277d1b5e 1995
17505c5c 1996 /* HIGHADJ takes an argument, - the next record *is* the
9602af51 1997 low 16 bits of addend. */
5933bdc9
ILT
1998 if (t == IMAGE_REL_BASED_HIGHADJ)
1999 {
6fa957a9
KH
2000 fprintf (file, " (%4x)",
2001 ((unsigned int)
2002 bfd_get_16 (abfd, data + i + 8 + j * 2 + 2)));
2003 j++;
5933bdc9 2004 }
9602af51 2005
17505c5c 2006 fprintf (file, "\n");
277d1b5e 2007 }
1725a96e 2008
277d1b5e
ILT
2009 i += size;
2010 }
2011
2012 free (data);
2013
b34976b6 2014 return TRUE;
277d1b5e 2015}
5879bb8f 2016\f
277d1b5e 2017
11a6da56 2018static bfd_byte *
5879bb8f
NC
2019rsrc_print_resource_directory (FILE * , bfd *, unsigned int,
2020 bfd_byte *, bfd_byte *, bfd_byte *, bfd_vma);
11a6da56
NC
2021
2022static bfd_byte *
5879bb8f
NC
2023rsrc_print_resource_entries (FILE * file,
2024 bfd * abfd,
2025 unsigned int indent,
2026 bfd_boolean is_name,
2027 bfd_byte * datastart,
2028 bfd_byte * data,
2029 bfd_byte * dataend,
2030 bfd_vma rva_bias)
11a6da56
NC
2031{
2032 unsigned long entry, addr, size;
2033
2034 if (data + 8 >= dataend)
2035 return dataend + 1;
2036
2037 fprintf (file, _("%*.s Entry: "), indent, " ");
2038
2039 entry = (long) bfd_get_32 (abfd, data);
2040 if (is_name)
2041 {
5879bb8f
NC
2042 bfd_byte * name;
2043
2044 /* Note - the documenation says that this field is an RVA value
2045 but windres appears to produce a section relative offset with
2046 the top bit set. Support both styles for now. */
2047 if (HighBitSet (entry))
2048 name = datastart + WithoutHighBit (entry);
2049 else
2050 name = datastart + entry - rva_bias;
2051
2052 if (name + 2 < dataend)
11a6da56
NC
2053 {
2054 unsigned int len;
5879bb8f 2055 len = bfd_get_16 (abfd, name);
9373215c 2056
5879bb8f
NC
2057 fprintf (file, _("name: [val: %08lx len %d]: "), entry, len);
2058 if (name + 2 + len * 2 < dataend)
2059 {
2060 /* This strange loop is to cope with multibyte characters. */
2061 while (len --)
2062 {
2063 name += 2;
2064 fprintf (file, "%.1s", name);
2065 }
2066 }
11a6da56 2067 else
5879bb8f 2068 fprintf (file, _("<corrupt string length: %#x>"), len);
11a6da56
NC
2069 }
2070 else
5879bb8f 2071 fprintf (file, _("<corrupt string offset: %#lx>"), entry);
11a6da56
NC
2072 }
2073 else
2074 fprintf (file, _("ID: %#08lx"), entry);
9373215c 2075
11a6da56 2076 entry = (long) bfd_get_32 (abfd, data + 4);
5879bb8f 2077 fprintf (file, _(", Value: %#08lx\n"), entry);
11a6da56 2078
5879bb8f
NC
2079 if (HighBitSet (entry))
2080 return rsrc_print_resource_directory (file, abfd, indent + 1,
2081 datastart,
2082 datastart + WithoutHighBit (entry),
2083 dataend, rva_bias);
11a6da56
NC
2084
2085 if (datastart + entry + 16 >= dataend)
5879bb8f 2086 return dataend + 1;
11a6da56 2087
5879bb8f
NC
2088 fprintf (file, _("%*.s Leaf: Addr: %#08lx, Size: %#08lx, Codepage: %d\n"),
2089 indent, " ",
11a6da56
NC
2090 addr = (long) bfd_get_32 (abfd, datastart + entry),
2091 size = (long) bfd_get_32 (abfd, datastart + entry + 4),
2092 (int) bfd_get_32 (abfd, datastart + entry + 8));
9373215c 2093
11a6da56
NC
2094 /* Check that the reserved entry is 0. */
2095 if (bfd_get_32 (abfd, datastart + entry + 12) != 0
2096 /* And that the data address/size is valid too. */
5879bb8f 2097 || (datastart + (addr - rva_bias) + size > dataend))
11a6da56
NC
2098 return dataend + 1;
2099
5879bb8f 2100 return datastart + (addr - rva_bias) + size;
11a6da56
NC
2101}
2102
5879bb8f
NC
2103#define max(a,b) ((a) > (b) ? (a) : (b))
2104#define min(a,b) ((a) < (b) ? (a) : (b))
2105
11a6da56 2106static bfd_byte *
5879bb8f
NC
2107rsrc_print_resource_directory (FILE * file,
2108 bfd * abfd,
2109 unsigned int indent,
2110 bfd_byte * datastart,
2111 bfd_byte * data,
2112 bfd_byte * dataend,
2113 bfd_vma rva_bias)
11a6da56
NC
2114{
2115 unsigned int num_names, num_ids;
5879bb8f 2116 bfd_byte * highest_data = data;
11a6da56
NC
2117
2118 if (data + 16 >= dataend)
2119 return dataend + 1;
2120
2121 fprintf (file, "%*.s ", indent, " ");
2122 switch (indent)
2123 {
2124 case 0: fprintf (file, "Type"); break;
2125 case 2: fprintf (file, "Name"); break;
2126 case 4: fprintf (file, "Language"); break;
2127 default: fprintf (file, "<unknown>"); break;
2128 }
2129
2130 fprintf (file, _(" Table: Char: %d, Time: %08lx, Ver: %d/%d, Num Names: %d, IDs: %d\n"),
2131 (int) bfd_get_32 (abfd, data),
2132 (long) bfd_get_32 (abfd, data + 4),
2133 (int) bfd_get_16 (abfd, data + 8),
2134 (int) bfd_get_16 (abfd, data + 10),
2135 num_names = (int) bfd_get_16 (abfd, data + 12),
2136 num_ids = (int) bfd_get_16 (abfd, data + 14));
2137 data += 16;
2138
5879bb8f 2139 while (num_names --)
11a6da56 2140 {
5879bb8f
NC
2141 bfd_byte * entry_end;
2142
2143 entry_end = rsrc_print_resource_entries (file, abfd, indent + 1, TRUE,
2144 datastart, data, dataend, rva_bias);
2145 data += 8;
2146 highest_data = max (highest_data, entry_end);
2147 if (entry_end >= dataend)
2148 return entry_end;
11a6da56
NC
2149 }
2150
5879bb8f 2151 while (num_ids --)
11a6da56 2152 {
5879bb8f
NC
2153 bfd_byte * entry_end;
2154
2155 entry_end = rsrc_print_resource_entries (file, abfd, indent + 1, FALSE,
9373215c
PM
2156 datastart, data, dataend,
2157 rva_bias);
5879bb8f
NC
2158 data += 8;
2159 highest_data = max (highest_data, entry_end);
2160 if (entry_end >= dataend)
2161 return entry_end;
11a6da56
NC
2162 }
2163
5879bb8f 2164 return max (highest_data, data);
11a6da56
NC
2165}
2166
2167/* Display the contents of a .rsrc section. We do not try to
2168 reproduce the resources, windres does that. Instead we dump
2169 the tables in a human readable format. */
2170
2171static bfd_boolean
5879bb8f 2172rsrc_print_section (bfd * abfd, void * vfile)
11a6da56
NC
2173{
2174 bfd_vma rva_bias;
2175 pe_data_type * pe;
2176 FILE * file = (FILE *) vfile;
2177 bfd_size_type datasize;
2178 asection * section;
2179 bfd_byte * data;
2180 bfd_byte * dataend;
2181 bfd_byte * datastart;
2182
11a6da56 2183 pe = pe_data (abfd);
5879bb8f 2184 if (pe == NULL)
11a6da56
NC
2185 return TRUE;
2186
5879bb8f
NC
2187 section = bfd_get_section_by_name (abfd, ".rsrc");
2188 if (section == NULL)
11a6da56 2189 return TRUE;
b69c8728
JT
2190 if (!(section->flags & SEC_HAS_CONTENTS))
2191 return TRUE;
5879bb8f 2192
11a6da56
NC
2193 datasize = section->size;
2194 if (datasize == 0)
2195 return TRUE;
2196
b69c8728
JT
2197 rva_bias = section->vma - pe->pe_opthdr.ImageBase;
2198
5879bb8f 2199 if (! bfd_malloc_and_get_section (abfd, section, & data))
11a6da56
NC
2200 {
2201 if (data != NULL)
2202 free (data);
2203 return FALSE;
2204 }
2205 datastart = data;
2206 dataend = data + datasize;
2207
2208 fflush (file);
2209 fprintf (file, "\nThe .rsrc Resource Directory section:\n");
2210
2211 while (data < dataend)
2212 {
5879bb8f
NC
2213 bfd_byte * p = data;
2214
9373215c
PM
2215 data = rsrc_print_resource_directory (file, abfd, 0, data, data,
2216 dataend, rva_bias);
11a6da56
NC
2217
2218 if (data == dataend + 1)
2219 fprintf (file, _("Corrupt .rsrc section detected!\n"));
2220 else
2221 {
2222 /* Align data before continuing. */
2223 int align = (1 << section->alignment_power) - 1;
5879bb8f 2224
b9e95fa2 2225 data = (bfd_byte *) (((ptrdiff_t) (data + align)) & ~ align);
5879bb8f 2226 rva_bias += data - p;
11a6da56
NC
2227
2228 /* For reasons that are unclear .rsrc sections are sometimes created
2229 aligned to a 1^3 boundary even when their alignment is set at
2230 1^2. Catch that case here before we issue a spurious warning
2231 message. */
2232 if (data == (dataend - 4))
2233 data = dataend;
2234 else if (data < dataend)
2235 fprintf (file, _("\nWARNING: Extra data in .rsrc section - it will be ignored by Windows:\n"));
2236 }
2237 }
2238
5879bb8f 2239 free (datastart);
11a6da56
NC
2240 return TRUE;
2241}
2242
277d1b5e
ILT
2243/* Print out the program headers. */
2244
b34976b6 2245bfd_boolean
7920ce38 2246_bfd_XX_print_private_bfd_data_common (bfd * abfd, void * vfile)
277d1b5e
ILT
2247{
2248 FILE *file = (FILE *) vfile;
2249 int j;
2250 pe_data_type *pe = pe_data (abfd);
2251 struct internal_extra_pe_aouthdr *i = &pe->pe_opthdr;
fac41780 2252 const char *subsystem_name = NULL;
d13c9dc6 2253 const char *name;
277d1b5e
ILT
2254
2255 /* The MS dumpbin program reportedly ands with 0xff0f before
2256 printing the characteristics field. Not sure why. No reason to
2257 emulate it here. */
2258 fprintf (file, _("\nCharacteristics 0x%x\n"), pe->real_flags);
2259#undef PF
6fa957a9 2260#define PF(x, y) if (pe->real_flags & x) { fprintf (file, "\t%s\n", y); }
d70270c5
BF
2261 PF (IMAGE_FILE_RELOCS_STRIPPED, "relocations stripped");
2262 PF (IMAGE_FILE_EXECUTABLE_IMAGE, "executable");
2263 PF (IMAGE_FILE_LINE_NUMS_STRIPPED, "line numbers stripped");
2264 PF (IMAGE_FILE_LOCAL_SYMS_STRIPPED, "symbols stripped");
2265 PF (IMAGE_FILE_LARGE_ADDRESS_AWARE, "large address aware");
2266 PF (IMAGE_FILE_BYTES_REVERSED_LO, "little endian");
2267 PF (IMAGE_FILE_32BIT_MACHINE, "32 bit words");
2268 PF (IMAGE_FILE_DEBUG_STRIPPED, "debugging information removed");
2269 PF (IMAGE_FILE_SYSTEM, "system file");
2270 PF (IMAGE_FILE_DLL, "DLL");
2271 PF (IMAGE_FILE_BYTES_REVERSED_HI, "big endian");
277d1b5e
ILT
2272#undef PF
2273
5933bdc9 2274 /* ctime implies '\n'. */
0b6488e2
RH
2275 {
2276 time_t t = pe->coff.timestamp;
2277 fprintf (file, "\nTime/Date\t\t%s", ctime (&t));
2278 }
d13c9dc6
L
2279
2280#ifndef IMAGE_NT_OPTIONAL_HDR_MAGIC
2281# define IMAGE_NT_OPTIONAL_HDR_MAGIC 0x10b
2282#endif
2283#ifndef IMAGE_NT_OPTIONAL_HDR64_MAGIC
2284# define IMAGE_NT_OPTIONAL_HDR64_MAGIC 0x20b
2285#endif
2286#ifndef IMAGE_NT_OPTIONAL_HDRROM_MAGIC
2287# define IMAGE_NT_OPTIONAL_HDRROM_MAGIC 0x107
2288#endif
2289
2290 switch (i->Magic)
2291 {
2292 case IMAGE_NT_OPTIONAL_HDR_MAGIC:
2293 name = "PE32";
2294 break;
2295 case IMAGE_NT_OPTIONAL_HDR64_MAGIC:
2296 name = "PE32+";
2297 break;
2298 case IMAGE_NT_OPTIONAL_HDRROM_MAGIC:
2299 name = "ROM";
2300 break;
2301 default:
2302 name = NULL;
2303 break;
2304 }
2305 fprintf (file, "Magic\t\t\t%04x", i->Magic);
2306 if (name)
2307 fprintf (file, "\t(%s)",name);
2308 fprintf (file, "\nMajorLinkerVersion\t%d\n", i->MajorLinkerVersion);
2309 fprintf (file, "MinorLinkerVersion\t%d\n", i->MinorLinkerVersion);
0af1713e 2310 fprintf (file, "SizeOfCode\t\t%08lx\n", (unsigned long) i->SizeOfCode);
d13c9dc6 2311 fprintf (file, "SizeOfInitializedData\t%08lx\n",
0af1713e 2312 (unsigned long) i->SizeOfInitializedData);
d13c9dc6 2313 fprintf (file, "SizeOfUninitializedData\t%08lx\n",
0af1713e 2314 (unsigned long) i->SizeOfUninitializedData);
d13c9dc6 2315 fprintf (file, "AddressOfEntryPoint\t");
ebf12fbe 2316 bfd_fprintf_vma (abfd, file, i->AddressOfEntryPoint);
d13c9dc6 2317 fprintf (file, "\nBaseOfCode\t\t");
ebf12fbe 2318 bfd_fprintf_vma (abfd, file, i->BaseOfCode);
d13c9dc6
L
2319#if !defined(COFF_WITH_pep) && !defined(COFF_WITH_pex64)
2320 /* PE32+ does not have BaseOfData member! */
2321 fprintf (file, "\nBaseOfData\t\t");
ebf12fbe 2322 bfd_fprintf_vma (abfd, file, i->BaseOfData);
d13c9dc6
L
2323#endif
2324
9602af51 2325 fprintf (file, "\nImageBase\t\t");
ebf12fbe 2326 bfd_fprintf_vma (abfd, file, i->ImageBase);
9602af51 2327 fprintf (file, "\nSectionAlignment\t");
ebf12fbe 2328 bfd_fprintf_vma (abfd, file, i->SectionAlignment);
9602af51 2329 fprintf (file, "\nFileAlignment\t\t");
ebf12fbe 2330 bfd_fprintf_vma (abfd, file, i->FileAlignment);
9602af51
KH
2331 fprintf (file, "\nMajorOSystemVersion\t%d\n", i->MajorOperatingSystemVersion);
2332 fprintf (file, "MinorOSystemVersion\t%d\n", i->MinorOperatingSystemVersion);
2333 fprintf (file, "MajorImageVersion\t%d\n", i->MajorImageVersion);
2334 fprintf (file, "MinorImageVersion\t%d\n", i->MinorImageVersion);
2335 fprintf (file, "MajorSubsystemVersion\t%d\n", i->MajorSubsystemVersion);
2336 fprintf (file, "MinorSubsystemVersion\t%d\n", i->MinorSubsystemVersion);
0af1713e
AM
2337 fprintf (file, "Win32Version\t\t%08lx\n", (unsigned long) i->Reserved1);
2338 fprintf (file, "SizeOfImage\t\t%08lx\n", (unsigned long) i->SizeOfImage);
2339 fprintf (file, "SizeOfHeaders\t\t%08lx\n", (unsigned long) i->SizeOfHeaders);
2340 fprintf (file, "CheckSum\t\t%08lx\n", (unsigned long) i->CheckSum);
1725a96e 2341
fac41780
JW
2342 switch (i->Subsystem)
2343 {
2344 case IMAGE_SUBSYSTEM_UNKNOWN:
2345 subsystem_name = "unspecified";
2346 break;
2347 case IMAGE_SUBSYSTEM_NATIVE:
2348 subsystem_name = "NT native";
2349 break;
2350 case IMAGE_SUBSYSTEM_WINDOWS_GUI:
2351 subsystem_name = "Windows GUI";
2352 break;
2353 case IMAGE_SUBSYSTEM_WINDOWS_CUI:
2354 subsystem_name = "Windows CUI";
2355 break;
2356 case IMAGE_SUBSYSTEM_POSIX_CUI:
2357 subsystem_name = "POSIX CUI";
2358 break;
2359 case IMAGE_SUBSYSTEM_WINDOWS_CE_GUI:
2360 subsystem_name = "Wince CUI";
2361 break;
d9118602 2362 // These are from UEFI Platform Initialization Specification 1.1.
fac41780
JW
2363 case IMAGE_SUBSYSTEM_EFI_APPLICATION:
2364 subsystem_name = "EFI application";
2365 break;
2366 case IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER:
2367 subsystem_name = "EFI boot service driver";
2368 break;
2369 case IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER:
9602af51 2370 subsystem_name = "EFI runtime driver";
fac41780 2371 break;
d9118602
L
2372 case IMAGE_SUBSYSTEM_SAL_RUNTIME_DRIVER:
2373 subsystem_name = "SAL runtime driver";
6c73cbb1 2374 break;
d9118602 2375 // This is from revision 8.0 of the MS PE/COFF spec
6c73cbb1
NC
2376 case IMAGE_SUBSYSTEM_XBOX:
2377 subsystem_name = "XBOX";
2378 break;
2379 // Added default case for clarity - subsystem_name is NULL anyway.
2380 default:
2381 subsystem_name = NULL;
fac41780 2382 }
1725a96e 2383
9602af51 2384 fprintf (file, "Subsystem\t\t%08x", i->Subsystem);
fac41780
JW
2385 if (subsystem_name)
2386 fprintf (file, "\t(%s)", subsystem_name);
9602af51
KH
2387 fprintf (file, "\nDllCharacteristics\t%08x\n", i->DllCharacteristics);
2388 fprintf (file, "SizeOfStackReserve\t");
ebf12fbe 2389 bfd_fprintf_vma (abfd, file, i->SizeOfStackReserve);
9602af51 2390 fprintf (file, "\nSizeOfStackCommit\t");
ebf12fbe 2391 bfd_fprintf_vma (abfd, file, i->SizeOfStackCommit);
9602af51 2392 fprintf (file, "\nSizeOfHeapReserve\t");
ebf12fbe 2393 bfd_fprintf_vma (abfd, file, i->SizeOfHeapReserve);
9602af51 2394 fprintf (file, "\nSizeOfHeapCommit\t");
ebf12fbe 2395 bfd_fprintf_vma (abfd, file, i->SizeOfHeapCommit);
0af1713e
AM
2396 fprintf (file, "\nLoaderFlags\t\t%08lx\n", (unsigned long) i->LoaderFlags);
2397 fprintf (file, "NumberOfRvaAndSizes\t%08lx\n",
2398 (unsigned long) i->NumberOfRvaAndSizes);
277d1b5e 2399
9602af51 2400 fprintf (file, "\nThe Data Directory\n");
277d1b5e
ILT
2401 for (j = 0; j < IMAGE_NUMBEROF_DIRECTORY_ENTRIES; j++)
2402 {
2403 fprintf (file, "Entry %1x ", j);
ebf12fbe 2404 bfd_fprintf_vma (abfd, file, i->DataDirectory[j].VirtualAddress);
0af1713e 2405 fprintf (file, " %08lx ", (unsigned long) i->DataDirectory[j].Size);
277d1b5e
ILT
2406 fprintf (file, "%s\n", dir_names[j]);
2407 }
2408
2409 pe_print_idata (abfd, vfile);
2410 pe_print_edata (abfd, vfile);
2b5c217d
NC
2411 if (bfd_coff_have_print_pdata (abfd))
2412 bfd_coff_print_pdata (abfd, vfile);
2413 else
2414 pe_print_pdata (abfd, vfile);
277d1b5e
ILT
2415 pe_print_reloc (abfd, vfile);
2416
5879bb8f 2417 rsrc_print_section (abfd, vfile);
9373215c 2418
b34976b6 2419 return TRUE;
277d1b5e
ILT
2420}
2421
2422/* Copy any private info we understand from the input bfd
2423 to the output bfd. */
2424
b34976b6 2425bfd_boolean
7920ce38 2426_bfd_XX_bfd_copy_private_bfd_data_common (bfd * ibfd, bfd * obfd)
277d1b5e 2427{
4be8cddc
L
2428 pe_data_type *ipe, *ope;
2429
277d1b5e
ILT
2430 /* One day we may try to grok other private data. */
2431 if (ibfd->xvec->flavour != bfd_target_coff_flavour
2432 || obfd->xvec->flavour != bfd_target_coff_flavour)
b34976b6 2433 return TRUE;
277d1b5e 2434
4be8cddc
L
2435 ipe = pe_data (ibfd);
2436 ope = pe_data (obfd);
4e1fc599 2437
325c681d 2438 /* pe_opthdr is copied in copy_object. */
4be8cddc
L
2439 ope->dll = ipe->dll;
2440
2441 /* Don't copy input subsystem if output is different from input. */
2442 if (obfd->xvec != ibfd->xvec)
2443 ope->pe_opthdr.Subsystem = IMAGE_SUBSYSTEM_UNKNOWN;
277d1b5e 2444
1725a96e 2445 /* For strip: if we removed .reloc, we'll make a real mess of things
5933bdc9
ILT
2446 if we don't remove this entry as well. */
2447 if (! pe_data (obfd)->has_reloc_section)
2448 {
6c73cbb1
NC
2449 pe_data (obfd)->pe_opthdr.DataDirectory[PE_BASE_RELOCATION_TABLE].VirtualAddress = 0;
2450 pe_data (obfd)->pe_opthdr.DataDirectory[PE_BASE_RELOCATION_TABLE].Size = 0;
5933bdc9 2451 }
441f34fa
L
2452
2453 /* For PIE, if there is .reloc, we won't add IMAGE_FILE_RELOCS_STRIPPED.
2454 But there is no .reloc, we make sure that IMAGE_FILE_RELOCS_STRIPPED
2455 won't be added. */
2456 if (! pe_data (ibfd)->has_reloc_section
2457 && ! (pe_data (ibfd)->real_flags & IMAGE_FILE_RELOCS_STRIPPED))
2458 pe_data (obfd)->dont_strip_reloc = 1;
2459
b34976b6 2460 return TRUE;
277d1b5e
ILT
2461}
2462
9602af51 2463/* Copy private section data. */
1725a96e 2464
b34976b6 2465bfd_boolean
7920ce38
NC
2466_bfd_XX_bfd_copy_private_section_data (bfd *ibfd,
2467 asection *isec,
2468 bfd *obfd,
2469 asection *osec)
277d1b5e
ILT
2470{
2471 if (bfd_get_flavour (ibfd) != bfd_target_coff_flavour
2472 || bfd_get_flavour (obfd) != bfd_target_coff_flavour)
b34976b6 2473 return TRUE;
277d1b5e
ILT
2474
2475 if (coff_section_data (ibfd, isec) != NULL
2476 && pei_section_data (ibfd, isec) != NULL)
2477 {
2478 if (coff_section_data (obfd, osec) == NULL)
2479 {
dc810e39 2480 bfd_size_type amt = sizeof (struct coff_section_tdata);
7920ce38 2481 osec->used_by_bfd = bfd_zalloc (obfd, amt);
277d1b5e 2482 if (osec->used_by_bfd == NULL)
b34976b6 2483 return FALSE;
277d1b5e 2484 }
1725a96e 2485
277d1b5e
ILT
2486 if (pei_section_data (obfd, osec) == NULL)
2487 {
dc810e39 2488 bfd_size_type amt = sizeof (struct pei_section_tdata);
7920ce38 2489 coff_section_data (obfd, osec)->tdata = bfd_zalloc (obfd, amt);
277d1b5e 2490 if (coff_section_data (obfd, osec)->tdata == NULL)
b34976b6 2491 return FALSE;
277d1b5e 2492 }
1725a96e 2493
277d1b5e
ILT
2494 pei_section_data (obfd, osec)->virt_size =
2495 pei_section_data (ibfd, isec)->virt_size;
5933bdc9 2496 pei_section_data (obfd, osec)->pe_flags =
6fa957a9 2497 pei_section_data (ibfd, isec)->pe_flags;
277d1b5e
ILT
2498 }
2499
b34976b6 2500 return TRUE;
277d1b5e 2501}
7d2b58d6
ILT
2502
2503void
7920ce38 2504_bfd_XX_get_symbol_info (bfd * abfd, asymbol *symbol, symbol_info *ret)
7d2b58d6
ILT
2505{
2506 coff_get_symbol_info (abfd, symbol, ret);
7d2b58d6 2507}
2fbadf2c 2508
5174d0fb
KT
2509#if !defined(COFF_WITH_pep) && defined(COFF_WITH_pex64)
2510static int
2511sort_x64_pdata (const void *l, const void *r)
2512{
2513 const char *lp = (const char *) l;
2514 const char *rp = (const char *) r;
2515 bfd_vma vl, vr;
2516 vl = bfd_getl32 (lp); vr = bfd_getl32 (rp);
2517 if (vl != vr)
2518 return (vl < vr ? -1 : 1);
2519 /* We compare just begin address. */
2520 return 0;
2521}
2522#endif
5879bb8f
NC
2523\f
2524/* Functions to process a .rsrc section. */
2525
2526static unsigned int sizeof_leaves;
2527static unsigned int sizeof_strings;
2528static unsigned int sizeof_tables_and_entries;
2529
2530static bfd_byte *
2531rsrc_count_directory (bfd *, bfd_byte *, bfd_byte *, bfd_byte *, bfd_vma);
2532
2533static bfd_byte *
2534rsrc_count_entries (bfd * abfd,
2535 bfd_boolean is_name,
2536 bfd_byte * datastart,
2537 bfd_byte * data,
2538 bfd_byte * dataend,
2539 bfd_vma rva_bias)
2540{
2541 unsigned long entry, addr, size;
2542
2543 if (data + 8 >= dataend)
2544 return dataend + 1;
2545
2546 if (is_name)
2547 {
2548 bfd_byte * name;
2549
2550 entry = (long) bfd_get_32 (abfd, data);
2551
2552 if (HighBitSet (entry))
2553 name = datastart + WithoutHighBit (entry);
2554 else
2555 name = datastart + entry - rva_bias;
2556
2557 if (name + 2 >= dataend)
2558 return dataend + 1;
2559
2560 unsigned int len = bfd_get_16 (abfd, name);
2561 if (len == 0 || len > 256)
2562 return dataend + 1;
2563
2564 sizeof_strings += (len + 1) * 2;
2565 }
2566
2567 entry = (long) bfd_get_32 (abfd, data + 4);
2568
2569 if (HighBitSet (entry))
2570 return rsrc_count_directory (abfd,
2571 datastart,
2572 datastart + WithoutHighBit (entry),
2573 dataend, rva_bias);
2574
2575 if (datastart + entry + 16 >= dataend)
2576 return dataend + 1;
2577
2578 addr = (long) bfd_get_32 (abfd, datastart + entry);
2579 size = (long) bfd_get_32 (abfd, datastart + entry + 4);
2580
2581 sizeof_leaves += 16;
2582
2583 return datastart + addr - rva_bias + size;
2584}
9373215c 2585
5879bb8f
NC
2586static bfd_byte *
2587rsrc_count_directory (bfd * abfd,
2588 bfd_byte * datastart,
2589 bfd_byte * data,
2590 bfd_byte * dataend,
2591 bfd_vma rva_bias)
2592{
2593 unsigned int num_entries, num_ids;
2594 bfd_byte * highest_data = data;
2595
2596 if (data + 16 >= dataend)
2597 return dataend + 1;
2598
2599 num_entries = (int) bfd_get_16 (abfd, data + 12);
2600 num_ids = (int) bfd_get_16 (abfd, data + 14);
2601
2602 num_entries += num_ids;
2603
2604 data += 16;
2605 sizeof_tables_and_entries += 16;
2606
2607 while (num_entries --)
2608 {
2609 bfd_byte * entry_end;
2610
2611 entry_end = rsrc_count_entries (abfd, num_entries >= num_ids,
2612 datastart, data, dataend, rva_bias);
2613 data += 8;
2614 sizeof_tables_and_entries += 8;
2615 highest_data = max (highest_data, entry_end);
2616 if (entry_end >= dataend)
2617 break;
2618 }
2619
2620 return max (highest_data, data);
2621}
2622
2623typedef struct rsrc_dir_chain
2624{
2625 unsigned int num_entries;
2626 struct rsrc_entry * first_entry;
2627 struct rsrc_entry * last_entry;
2628} rsrc_dir_chain;
2629
2630typedef struct rsrc_directory
2631{
2632 unsigned int characteristics;
2633 unsigned int time;
2634 unsigned int major;
2635 unsigned int minor;
2636
2637 rsrc_dir_chain names;
2638 rsrc_dir_chain ids;
2639
2640 struct rsrc_entry * entry;
2641} rsrc_directory;
2642
2643typedef struct rsrc_string
2644{
2645 unsigned int len;
2646 bfd_byte * string;
2647} rsrc_string;
9373215c 2648
5879bb8f
NC
2649typedef struct rsrc_leaf
2650{
2651 unsigned int size;
2652 unsigned int codepage;
2653 bfd_byte * data;
2654} rsrc_leaf;
2655
2656typedef struct rsrc_entry
2657{
2658 bfd_boolean is_name;
2659 union
2660 {
2661 unsigned int id;
2662 struct rsrc_string name;
2663 } name_id;
2664
2665 bfd_boolean is_dir;
2666 union
2667 {
2668 struct rsrc_directory * directory;
2669 struct rsrc_leaf * leaf;
2670 } value;
2671
2672 struct rsrc_entry * next_entry;
2673 struct rsrc_directory * parent;
2674} rsrc_entry;
2675
2676static bfd_byte *
2677rsrc_parse_directory (bfd *, rsrc_directory *, bfd_byte *,
2678 bfd_byte *, bfd_byte *, bfd_vma, rsrc_entry *);
2679
2680static bfd_byte *
2681rsrc_parse_entry (bfd * abfd,
2682 bfd_boolean is_name,
2683 rsrc_entry * entry,
2684 bfd_byte * datastart,
2685 bfd_byte * data,
2686 bfd_byte * dataend,
2687 bfd_vma rva_bias,
2688 rsrc_directory * parent)
2689{
2690 unsigned long val, addr, size;
2691
2692 val = bfd_get_32 (abfd, data);
2693
2694 entry->parent = parent;
2695 entry->is_name = is_name;
2696
2697 if (is_name)
2698 {
2699 /* FIXME: Add range checking ? */
2700 if (HighBitSet (val))
2701 {
2702 val = WithoutHighBit (val);
2703
2704 entry->name_id.name.len = bfd_get_16 (abfd, datastart + val);
2705 entry->name_id.name.string = datastart + val + 2;
2706 }
2707 else
2708 {
9373215c
PM
2709 entry->name_id.name.len = bfd_get_16 (abfd, datastart + val
2710 - rva_bias);
5879bb8f
NC
2711 entry->name_id.name.string = datastart + val - rva_bias + 2;
2712 }
2713 }
2714 else
2715 entry->name_id.id = val;
2716
2717 val = bfd_get_32 (abfd, data + 4);
2718
2719 if (HighBitSet (val))
2720 {
2721 entry->is_dir = TRUE;
2722 entry->value.directory = bfd_malloc (sizeof * entry->value.directory);
2723 if (entry->value.directory == NULL)
2724 return dataend;
2725
2726 return rsrc_parse_directory (abfd, entry->value.directory,
2727 datastart,
2728 datastart + WithoutHighBit (val),
2729 dataend, rva_bias, entry);
2730 }
2731
2732 entry->is_dir = FALSE;
2733 entry->value.leaf = bfd_malloc (sizeof * entry->value.leaf);
2734 if (entry->value.leaf == NULL)
2735 return dataend;
2736
2737 addr = bfd_get_32 (abfd, datastart + val);
2738 size = entry->value.leaf->size = bfd_get_32 (abfd, datastart + val + 4);
2739 entry->value.leaf->codepage = bfd_get_32 (abfd, datastart + val + 8);
2740
2741 entry->value.leaf->data = bfd_malloc (size);
2742 if (entry->value.leaf->data == NULL)
2743 return dataend;
2744
2745 memcpy (entry->value.leaf->data, datastart + addr - rva_bias, size);
2746 return datastart + (addr - rva_bias) + size;
2747}
2748
2749static bfd_byte *
2750rsrc_parse_entries (bfd * abfd,
2751 rsrc_dir_chain * chain,
2752 bfd_boolean is_name,
2753 bfd_byte * highest_data,
2754 bfd_byte * datastart,
2755 bfd_byte * data,
2756 bfd_byte * dataend,
2757 bfd_vma rva_bias,
2758 rsrc_directory * parent)
2759{
9373215c 2760 unsigned int i;
5879bb8f
NC
2761 rsrc_entry * entry;
2762
2763 if (chain->num_entries == 0)
2764 {
2765 chain->first_entry = chain->last_entry = NULL;
2766 return highest_data;
2767 }
2768
2769 entry = bfd_malloc (sizeof * entry);
2770 if (entry == NULL)
2771 return dataend;
2772
2773 chain->first_entry = entry;
2774
5879bb8f
NC
2775 for (i = chain->num_entries; i--;)
2776 {
2777 bfd_byte * entry_end;
2778
2779 entry_end = rsrc_parse_entry (abfd, is_name, entry, datastart,
2780 data, dataend, rva_bias, parent);
2781 data += 8;
2782 highest_data = max (entry_end, highest_data);
2783 if (entry_end > dataend)
2784 return dataend;
2785
2786 if (i)
2787 {
2788 entry->next_entry = bfd_malloc (sizeof * entry);
2789 entry = entry->next_entry;
2790 if (entry == NULL)
2791 return dataend;
2792 }
2793 else
2794 entry->next_entry = NULL;
2795 }
2796
2797 chain->last_entry = entry;
2798
2799 return highest_data;
2800}
2801
2802static bfd_byte *
2803rsrc_parse_directory (bfd * abfd,
2804 rsrc_directory * table,
2805 bfd_byte * datastart,
2806 bfd_byte * data,
2807 bfd_byte * dataend,
2808 bfd_vma rva_bias,
2809 rsrc_entry * entry)
2810{
2811 bfd_byte * highest_data = data;
2812
2813 if (table == NULL)
2814 return dataend;
2815
2816 table->characteristics = bfd_get_32 (abfd, data);
2817 table->time = bfd_get_32 (abfd, data + 4);
2818 table->major = bfd_get_16 (abfd, data + 8);
2819 table->minor = bfd_get_16 (abfd, data + 10);
2820 table->names.num_entries = bfd_get_16 (abfd, data + 12);
2821 table->ids.num_entries = bfd_get_16 (abfd, data + 14);
2822 table->entry = entry;
2823
2824 data += 16;
2825
2826 highest_data = rsrc_parse_entries (abfd, & table->names, TRUE, data,
2827 datastart, data, dataend, rva_bias, table);
2828 data += table->names.num_entries * 8;
2829
2830 highest_data = rsrc_parse_entries (abfd, & table->ids, FALSE, highest_data,
2831 datastart, data, dataend, rva_bias, table);
2832 data += table->ids.num_entries * 8;
2833
2834 return max (highest_data, data);
2835}
2836
2837typedef struct rsrc_write_data
2838{
2839 bfd * abfd;
2840 bfd_byte * datastart;
2841 bfd_byte * next_table;
2842 bfd_byte * next_leaf;
2843 bfd_byte * next_string;
2844 bfd_byte * next_data;
2845 bfd_vma rva_bias;
9373215c
PM
2846} rsrc_write_data;
2847
5879bb8f
NC
2848static void
2849rsrc_write_string (rsrc_write_data * data,
2850 rsrc_string * string)
2851{
2852 bfd_put_16 (data->abfd, string->len, data->next_string);
2853 memcpy (data->next_string + 2, string->string, string->len * 2);
2854 data->next_string += (string->len + 1) * 2;
2855}
2856
2857static inline unsigned int
2858rsrc_compute_rva (rsrc_write_data * data,
2859 bfd_byte * addr)
2860{
2861 return (addr - data->datastart) + data->rva_bias;
2862}
2863
2864static void
2865rsrc_write_leaf (rsrc_write_data * data,
2866 rsrc_leaf * leaf)
2867{
9373215c
PM
2868 bfd_put_32 (data->abfd, rsrc_compute_rva (data, data->next_data),
2869 data->next_leaf);
5879bb8f
NC
2870 bfd_put_32 (data->abfd, leaf->size, data->next_leaf + 4);
2871 bfd_put_32 (data->abfd, leaf->codepage, data->next_leaf + 8);
2872 bfd_put_32 (data->abfd, 0 /*reserved*/, data->next_leaf + 12);
2873 data->next_leaf += 16;
2874
2875 memcpy (data->next_data, leaf->data, leaf->size);
2876 data->next_data += leaf->size;
2877}
2878
2879static void rsrc_write_directory (rsrc_write_data *, rsrc_directory *);
2880
2881static void
2882rsrc_write_entry (rsrc_write_data * data,
2883 bfd_byte * where,
2884 rsrc_entry * entry)
2885{
2886 if (entry->is_name)
2887 {
2888 bfd_put_32 (data->abfd,
2889 SetHighBit (data->next_string - data->datastart),
2890 where);
2891 rsrc_write_string (data, & entry->name_id.name);
2892 }
2893 else
2894 bfd_put_32 (data->abfd, entry->name_id.id, where);
2895
2896 if (entry->is_dir)
2897 {
2898 bfd_put_32 (data->abfd,
2899 SetHighBit (data->next_table - data->datastart),
2900 where + 4);
2901 rsrc_write_directory (data, entry->value.directory);
2902 }
2903 else
2904 {
2905 bfd_put_32 (data->abfd, data->next_leaf - data->datastart, where + 4);
2906 rsrc_write_leaf (data, entry->value.leaf);
2907 }
2908}
2909
2910static void
2911rsrc_write_directory (rsrc_write_data * data,
2912 rsrc_directory * dir)
2913{
2914 rsrc_entry * entry;
2915 unsigned int i;
9373215c
PM
2916 bfd_byte * next_entry;
2917 bfd_byte * nt;
5879bb8f
NC
2918
2919 bfd_put_32 (data->abfd, dir->characteristics, data->next_table);
2920 bfd_put_32 (data->abfd, 0 /*dir->time*/, data->next_table + 4);
2921 bfd_put_16 (data->abfd, dir->major, data->next_table + 8);
2922 bfd_put_16 (data->abfd, dir->minor, data->next_table + 10);
2923 bfd_put_16 (data->abfd, dir->names.num_entries, data->next_table + 12);
2924 bfd_put_16 (data->abfd, dir->ids.num_entries, data->next_table + 14);
2925
2926 /* Compute where the entries and the next table will be placed. */
9373215c
PM
2927 next_entry = data->next_table + 16;
2928 data->next_table = next_entry + (dir->names.num_entries * 8)
2929 + (dir->ids.num_entries * 8);
2930 nt = data->next_table;
2931
5879bb8f
NC
2932 /* Write the entries. */
2933 for (i = dir->names.num_entries, entry = dir->names.first_entry;
2934 i > 0 && entry != NULL;
2935 i--, entry = entry->next_entry)
2936 {
2937 rsrc_write_entry (data, next_entry, entry);
2938 next_entry += 8;
2939 }
2940 BFD_ASSERT (i == 0);
2941 BFD_ASSERT (entry == NULL);
2942
2943 for (i = dir->ids.num_entries, entry = dir->ids.first_entry;
2944 i > 0 && entry != NULL;
2945 i--, entry = entry->next_entry)
2946 {
2947 rsrc_write_entry (data, next_entry, entry);
2948 next_entry += 8;
2949 }
2950 BFD_ASSERT (i == 0);
2951 BFD_ASSERT (entry == NULL);
2952 BFD_ASSERT (nt == next_entry);
2953}
2954
9373215c 2955#if defined HAVE_WCHAR_H && ! defined __CYGWIN__ && ! defined __MINGW32__
5879bb8f
NC
2956/* Return the length (number of units) of the first character in S,
2957 putting its 'ucs4_t' representation in *PUC. */
2958
2959static unsigned int
2960u16_mbtouc (wchar_t * puc, const unsigned short * s, unsigned int n)
2961{
2962 unsigned short c = * s;
2963
2964 if (c < 0xd800 || c >= 0xe000)
2965 {
2966 *puc = c;
2967 return 1;
2968 }
2969
2970 if (c < 0xdc00)
2971 {
2972 if (n >= 2)
2973 {
2974 if (s[1] >= 0xdc00 && s[1] < 0xe000)
2975 {
2976 *puc = 0x10000 + ((c - 0xd800) << 10) + (s[1] - 0xdc00);
2977 return 2;
2978 }
2979 }
2980 else
2981 {
2982 /* Incomplete multibyte character. */
2983 *puc = 0xfffd;
2984 return n;
2985 }
2986 }
2987
2988 /* Invalid multibyte character. */
2989 *puc = 0xfffd;
2990 return 1;
2991}
9373215c 2992#endif /* HAVE_WCHAR_H and not Cygwin/Mingw */
5879bb8f
NC
2993
2994/* Perform a comparison of two entries. */
2995static signed int
2996rsrc_cmp (bfd_boolean is_name, rsrc_entry * a, rsrc_entry * b)
2997{
9373215c 2998 signed int res;
9373215c
PM
2999 bfd_byte * astring;
3000 unsigned int alen;
3001 bfd_byte * bstring;
3002 unsigned int blen;
3003
5879bb8f 3004 if (! is_name)
9373215c 3005 return a->name_id.id - b->name_id.id;
5879bb8f
NC
3006
3007 /* We have to perform a case insenstive, unicode string comparison... */
9373215c
PM
3008 astring = a->name_id.name.string;
3009 alen = a->name_id.name.len;
3010 bstring = b->name_id.name.string;
3011 blen = b->name_id.name.len;
5879bb8f 3012
9373215c
PM
3013#if defined __CYGWIN__ || defined __MINGW32__
3014 /* Under Windows hosts (both Cygwin and Mingw types),
3015 unicode == UTF-16 == wchar_t. The case insensitive string comparison
3016 function however goes by different names in the two environments... */
3017
3018#undef rscpcmp
5879bb8f 3019#ifdef __CYGWIN__
9373215c
PM
3020#define rscpcmp wcsncasecmp
3021#endif
3022#ifdef __MINGW32__
3023#define rscpcmp wcsnicmp
3024#endif
3025
3026 res = rscpcmp ((const wchar_t *) astring, (const wchar_t *) bstring,
3027 min (alen, blen));
5879bb8f 3028
9373215c 3029#elif defined HAVE_WCHAR_H
3f10b67a
PM
3030 {
3031 unsigned int i;
3032 res = 0;
3033 for (i = min (alen, blen); i--; astring += 2, bstring += 2)
3034 {
3035 wchar_t awc;
3036 wchar_t bwc;
3037
3038 /* Convert UTF-16 unicode characters into wchar_t characters so
3039 that we can then perform a case insensitive comparison. */
3040 int Alen = u16_mbtouc (& awc, (const unsigned short *) astring, 2);
3041 int Blen = u16_mbtouc (& bwc, (const unsigned short *) bstring, 2);
3042
3043 if (Alen != Blen)
3044 return Alen - Blen;
3045 res = wcsncasecmp (& awc, & bwc, 1);
3046 if (res)
3047 break;
3048 }
3049 }
5879bb8f 3050#else
9373215c
PM
3051 /* Do the best we can - a case sensitive, untranslated comparison. */
3052 res = memcmp (astring, bstring, min (alen, blen) * 2);
5879bb8f
NC
3053#endif
3054
3055 if (res == 0)
3056 res = alen - blen;
3057
3058 return res;
3059}
3060
3061static void
3062rsrc_print_name (char * buffer, rsrc_string string)
3063{
3064 unsigned int i;
3065 bfd_byte * name = string.string;
3066
3067 for (i = string.len; i--; name += 2)
3068 sprintf (buffer + strlen (buffer), "%.1s", name);
3069}
3070
3071static const char *
3072rsrc_resource_name (rsrc_entry * entry, rsrc_directory * dir)
3073{
3074 static char buffer [256];
3075 bfd_boolean is_string = FALSE;
3076
3077 buffer[0] = 0;
3078
9373215c
PM
3079 if (dir != NULL && dir->entry != NULL && dir->entry->parent != NULL
3080 && dir->entry->parent->entry != NULL)
5879bb8f
NC
3081 {
3082 strcpy (buffer, "type: ");
3083 if (dir->entry->parent->entry->is_name)
9373215c
PM
3084 rsrc_print_name (buffer + strlen (buffer),
3085 dir->entry->parent->entry->name_id.name);
5879bb8f
NC
3086 else
3087 {
3088 unsigned int id = dir->entry->parent->entry->name_id.id;
3089
3090 sprintf (buffer + strlen (buffer), "%x", id);
3091 switch (id)
3092 {
3093 case 1: strcat (buffer, " (CURSOR)"); break;
3094 case 2: strcat (buffer, " (BITMAP)"); break;
3095 case 3: strcat (buffer, " (ICON)"); break;
3096 case 4: strcat (buffer, " (MENU)"); break;
3097 case 5: strcat (buffer, " (DIALOG)"); break;
3098 case 6: strcat (buffer, " (STRING)"); is_string = TRUE; break;
3099 case 7: strcat (buffer, " (FONTDIR)"); break;
3100 case 8: strcat (buffer, " (FONT)"); break;
3101 case 9: strcat (buffer, " (ACCELERATOR)"); break;
3102 case 10: strcat (buffer, " (RCDATA)"); break;
3103 case 11: strcat (buffer, " (MESSAGETABLE)"); break;
3104 case 12: strcat (buffer, " (GROUP_CURSOR)"); break;
3105 case 14: strcat (buffer, " (GROUP_ICON)"); break;
3106 case 16: strcat (buffer, " (VERSION)"); break;
3107 case 17: strcat (buffer, " (DLGINCLUDE)"); break;
3108 case 19: strcat (buffer, " (PLUGPLAY)"); break;
3109 case 20: strcat (buffer, " (VXD)"); break;
3110 case 21: strcat (buffer, " (ANICURSOR)"); break;
3111 case 22: strcat (buffer, " (ANIICON)"); break;
3112 case 23: strcat (buffer, " (HTML)"); break;
3113 case 24: strcat (buffer, " (MANIFEST)"); break;
3114 case 240: strcat (buffer, " (DLGINIT)"); break;
3115 case 241: strcat (buffer, " (TOOLBAR)"); break;
3116 }
3117 }
3118 }
3119
3120 if (dir != NULL && dir->entry != NULL)
3121 {
3122 strcat (buffer, " name: ");
3123 if (dir->entry->is_name)
3124 rsrc_print_name (buffer + strlen (buffer), dir->entry->name_id.name);
3125 else
3126 {
3127 unsigned int id = dir->entry->name_id.id;
3128
3129 sprintf (buffer + strlen (buffer), "%x", id);
3130
3131 if (is_string)
3132 sprintf (buffer + strlen (buffer), " (resource id range: %d - %d)",
3133 (id - 1) << 4, (id << 4) - 1);
3134 }
3135 }
3136
3137 if (entry != NULL)
3138 {
3139 strcat (buffer, " lang: ");
3140
3141 if (entry->is_name)
3142 rsrc_print_name (buffer + strlen (buffer), entry->name_id.name);
3143 else
3144 sprintf (buffer + strlen (buffer), "%x", entry->name_id.id);
3145 }
3146
3147 return buffer;
3148}
3149
3150/* *sigh* Windows resource strings are special. Only the top 28-bits of
3151 their ID is stored in the NAME entry. The bottom four bits are used as
3152 an index into unicode string table that makes up the data of the leaf.
3153 So identical type-name-lang string resources may not actually be
3154 identical at all.
3155
3156 This function is called when we have detected two string resources with
3157 match top-28-bit IDs. We have to scan the string tables inside the leaves
3158 and discover if there are any real collisions. If there are then we report
9373215c
PM
3159 them and return FALSE. Otherwise we copy any strings from B into A and
3160 then return TRUE. */
5879bb8f
NC
3161
3162static bfd_boolean
3163rsrc_merge_string_entries (rsrc_entry * a ATTRIBUTE_UNUSED,
3164 rsrc_entry * b ATTRIBUTE_UNUSED)
3165{
3166 unsigned int copy_needed = 0;
3167 unsigned int i;
9373215c
PM
3168 bfd_byte * astring;
3169 bfd_byte * bstring;
3170 bfd_byte * new_data;
3171 bfd_byte * nstring;
5879bb8f
NC
3172
3173 /* Step one: Find out what we have to do. */
3174 BFD_ASSERT (! a->is_dir);
9373215c 3175 astring = a->value.leaf->data;
5879bb8f
NC
3176
3177 BFD_ASSERT (! b->is_dir);
9373215c 3178 bstring = b->value.leaf->data;
5879bb8f
NC
3179
3180 for (i = 0; i < 16; i++)
3181 {
3182 unsigned int alen = astring[0] + (astring[1] << 8);
3183 unsigned int blen = bstring[0] + (bstring[1] << 8);
3184
3185 if (alen == 0)
3186 {
3187 copy_needed += blen * 2;
3188 }
3189 else if (blen == 0)
3190 ;
3191 else if (alen != blen)
3192 /* FIXME: Should we continue the loop in order to report other duplicates ? */
3193 break;
3194 /* alen == blen != 0. We might have two identical strings. If so we
3195 can ignore the second one. There is no need for wchar_t vs UTF-16
3196 theatrics here - we are only interested in (case sensitive) equality. */
3197 else if (memcmp (astring + 2, bstring + 2, alen * 2) != 0)
3198 break;
3199
3200 astring += (alen + 1) * 2;
3201 bstring += (blen + 1) * 2;
3202 }
3203
3204 if (i != 16)
3205 {
3206 if (a->parent != NULL
3207 && a->parent->entry != NULL
3208 && a->parent->entry->is_name == FALSE)
3209 _bfd_error_handler (_(".rsrc merge failure: duplicate string resource: %d"),
3210 ((a->parent->entry->name_id.id - 1) << 4) + i);
3211 return FALSE;
3212 }
3213
3214 if (copy_needed == 0)
3215 return TRUE;
3216
3217 /* If we reach here then A and B must both have non-colliding strings.
3218 (We never get string resources with fully empty string tables).
3219 We need to allocate an extra COPY_NEEDED bytes in A and then bring
3220 in B's strings. */
9373215c 3221 new_data = bfd_malloc (a->value.leaf->size + copy_needed);
5879bb8f
NC
3222 if (new_data == NULL)
3223 return FALSE;
3224
9373215c 3225 nstring = new_data;
5879bb8f
NC
3226 astring = a->value.leaf->data;
3227 bstring = b->value.leaf->data;
3228
3229 for (i = 0; i < 16; i++)
3230 {
3231 unsigned int alen = astring[0] + (astring[1] << 8);
3232 unsigned int blen = bstring[0] + (bstring[1] << 8);
3233
3234 if (alen != 0)
3235 {
3236 memcpy (nstring, astring, (alen + 1) * 2);
3237 nstring += (alen + 1) * 2;
3238 }
3239 else if (blen != 0)
3240 {
3241 memcpy (nstring, bstring, (blen + 1) * 2);
3242 nstring += (blen + 1) * 2;
3243 }
3244 else
3245 {
3246 * nstring++ = 0;
3247 * nstring++ = 0;
3248 }
9373215c 3249
5879bb8f
NC
3250 astring += (alen + 1) * 2;
3251 bstring += (blen + 1) * 2;
3252 }
3253
3254 BFD_ASSERT (nstring - new_data == (signed) (a->value.leaf->size + copy_needed));
9373215c 3255
5879bb8f
NC
3256 free (a->value.leaf->data);
3257 a->value.leaf->data = new_data;
3258 a->value.leaf->size += copy_needed;
3259
3260 return TRUE;
3261}
3262
3263static void rsrc_merge (rsrc_entry *, rsrc_entry *);
3264
3265/* Sort the entries in given part of the directory.
3266 We use an old fashioned bubble sort because we are dealing
9373215c 3267 with lists and we want to handle matches specially. */
5879bb8f
NC
3268
3269static void
3270rsrc_sort_entries (rsrc_dir_chain * chain,
3271 bfd_boolean is_name,
3272 rsrc_directory * dir)
3273{
3274 rsrc_entry * entry;
3275 rsrc_entry * next;
3276 rsrc_entry ** points_to_entry;
3277 bfd_boolean swapped;
3278
3279 if (chain->num_entries < 2)
3280 return;
3281
3282 do
3283 {
3284 swapped = FALSE;
3285 points_to_entry = & chain->first_entry;
3286 entry = * points_to_entry;
3287 next = entry->next_entry;
3288
3289 do
3290 {
3291 signed int cmp = rsrc_cmp (is_name, entry, next);
3292
3293 if (cmp > 0)
3294 {
3295 entry->next_entry = next->next_entry;
3296 next->next_entry = entry;
3297 * points_to_entry = next;
3298 points_to_entry = & next->next_entry;
3299 next = entry->next_entry;
3300 swapped = TRUE;
3301 }
3302 else if (cmp == 0)
3303 {
3304 if (entry->is_dir && next->is_dir)
3305 {
3306 /* When we encounter identical directory entries we have to
3307 merge them together. The exception to this rule is for
3308 resource manifests - there can only be one of these,
3309 even if they differ in language. Zero-language manifests
3310 are assumed to be default manifests (provided by the
3311 cygwin build system) and these can be silently dropped,
3312 unless that would reduce the number of manifests to zero.
3313 There should only ever be one non-zero lang manifest -
3314 if there are more it is an error. A non-zero lang
3315 manifest takes precedence over a default manifest. */
3316 if (entry->is_name == FALSE
3317 && entry->name_id.id == 1
3318 && dir != NULL
3319 && dir->entry != NULL
3320 && dir->entry->is_name == FALSE
3321 && dir->entry->name_id.id == 0x18)
3322 {
3323 if (next->value.directory->names.num_entries == 0
3324 && next->value.directory->ids.num_entries == 1
3325 && next->value.directory->ids.first_entry->is_name == FALSE
3326 && next->value.directory->ids.first_entry->name_id.id == 0)
3327 /* Fall through so that NEXT is dropped. */
3328 ;
3329 else if (entry->value.directory->names.num_entries == 0
3330 && entry->value.directory->ids.num_entries == 1
3331 && entry->value.directory->ids.first_entry->is_name == FALSE
3332 && entry->value.directory->ids.first_entry->name_id.id == 0)
3333 {
3334 /* Swap ENTRY and NEXT. Then fall through so that the old ENTRY is dropped. */
3335 entry->next_entry = next->next_entry;
3336 next->next_entry = entry;
3337 * points_to_entry = next;
3338 points_to_entry = & next->next_entry;
3339 next = entry->next_entry;
3340 swapped = TRUE;
3341 }
3342 else
3343 {
3344 _bfd_error_handler (_(".rsrc merge failure: multiple non-default manifests"));
3345 bfd_set_error (bfd_error_file_truncated);
3346 return;
3347 }
9373215c 3348
5879bb8f
NC
3349 /* Unhook NEXT from the chain. */
3350 /* FIXME: memory loss here. */
3351 entry->next_entry = next->next_entry;
3352 chain->num_entries --;
3353 if (chain->num_entries < 2)
3354 return;
3355 next = next->next_entry;
3356 }
3357 else
3358 rsrc_merge (entry, next);
3359 }
3360 else if (entry->is_dir != next->is_dir)
3361 {
3362 _bfd_error_handler (_(".rsrc merge failure: a directory matches a leaf"));
3363 bfd_set_error (bfd_error_file_truncated);
3364 return;
3365 }
3366 else
3367 {
3368 /* Otherwise with identical leaves we issue an error
3369 message - because there should never be duplicates.
3370 The exception is Type 18/Name 1/Lang 0 which is the
3371 defaul manifest - this can just be dropped. */
3372 if (entry->is_name == FALSE
3373 && entry->name_id.id == 0
3374 && dir != NULL
3375 && dir->entry != NULL
3376 && dir->entry->is_name == FALSE
3377 && dir->entry->name_id.id == 1
3378 && dir->entry->parent != NULL
3379 && dir->entry->parent->entry != NULL
3380 && dir->entry->parent->entry->is_name == FALSE
3381 && dir->entry->parent->entry->name_id.id == 0x18 /* RT_MANIFEST */)
3382 ;
3383 else if (dir != NULL
3384 && dir->entry != NULL
3385 && dir->entry->parent != NULL
3386 && dir->entry->parent->entry != NULL
3387 && dir->entry->parent->entry->is_name == FALSE
3388 && dir->entry->parent->entry->name_id.id == 0x6 /* RT_STRING */)
3389 {
3390 /* Strings need special handling. */
3391 if (! rsrc_merge_string_entries (entry, next))
3392 {
3393 /* _bfd_error_handler should have been called inside merge_strings. */
3394 bfd_set_error (bfd_error_file_truncated);
3395 return;
3396 }
3397 }
3398 else
3399 {
3400 if (dir == NULL
3401 || dir->entry == NULL
3402 || dir->entry->parent == NULL
3403 || dir->entry->parent->entry == NULL)
3404 _bfd_error_handler (_(".rsrc merge failure: duplicate leaf"));
3405 else
3406 _bfd_error_handler (_(".rsrc merge failure: duplicate leaf: %s"),
3407 rsrc_resource_name (entry, dir));
3408 bfd_set_error (bfd_error_file_truncated);
3409 return;
3410 }
3411 }
3412
3413 /* Unhook NEXT from the chain. */
3414 entry->next_entry = next->next_entry;
3415 chain->num_entries --;
3416 if (chain->num_entries < 2)
3417 return;
3418 next = next->next_entry;
3419 }
3420 else
3421 {
3422 points_to_entry = & entry->next_entry;
3423 entry = next;
3424 next = next->next_entry;
3425 }
3426 }
3427 while (next);
3428
3429 chain->last_entry = entry;
3430 }
3431 while (swapped);
3432}
3433
3434/* Attach B's chain onto A. */
3435static void
9373215c 3436rsrc_attach_chain (rsrc_dir_chain * achain, rsrc_dir_chain * bchain)
5879bb8f
NC
3437{
3438 if (bchain->num_entries == 0)
3439 return;
3440
3441 achain->num_entries += bchain->num_entries;
3442
3443 if (achain->first_entry == NULL)
3444 {
3445 achain->first_entry = bchain->first_entry;
3446 achain->last_entry = bchain->last_entry;
3447 }
3448 else
3449 {
3450 achain->last_entry->next_entry = bchain->first_entry;
3451 achain->last_entry = bchain->last_entry;
3452 }
9373215c 3453
5879bb8f
NC
3454 bchain->num_entries = 0;
3455 bchain->first_entry = bchain->last_entry = NULL;
3456}
3457
3458static void
3459rsrc_merge (struct rsrc_entry * a, struct rsrc_entry * b)
3460{
9373215c
PM
3461 rsrc_directory * adir;
3462 rsrc_directory * bdir;
3463
5879bb8f
NC
3464 BFD_ASSERT (a->is_dir);
3465 BFD_ASSERT (b->is_dir);
3466
9373215c
PM
3467 adir = a->value.directory;
3468 bdir = b->value.directory;
3469
5879bb8f
NC
3470 if (adir->characteristics != bdir->characteristics)
3471 {
3472 _bfd_error_handler (_(".rsrc merge failure: dirs with differing characteristics\n"));
3473 bfd_set_error (bfd_error_file_truncated);
3474 return;
3475 }
9373215c 3476
5879bb8f
NC
3477 if (adir->major != bdir->major || adir->minor != bdir->minor)
3478 {
3479 _bfd_error_handler (_(".rsrc merge failure: differing directory versions\n"));
3480 bfd_set_error (bfd_error_file_truncated);
3481 return;
3482 }
3483
3484 /* Attach B's name chain to A. */
3485 rsrc_attach_chain (& adir->names, & bdir->names);
3486
3487 /* Attach B's ID chain to A. */
3488 rsrc_attach_chain (& adir->ids, & bdir->ids);
3489
3490 /* Now sort A's entries. */
3491 rsrc_sort_entries (& adir->names, TRUE, adir);
3492 rsrc_sort_entries (& adir->ids, FALSE, adir);
3493}
3494
3495/* Check the .rsrc section. If it contains multiple concatenated
3496 resources then we must merge them properly. Otherwise Windows
3497 will ignore all but the first set. */
3498
3499static void
3500rsrc_process_section (bfd * abfd,
3501 struct coff_final_link_info * pfinfo)
3502{
9373215c
PM
3503 rsrc_directory new_table;
3504 bfd_size_type size;
3505 asection * sec;
3506 pe_data_type * pe;
3507 bfd_vma rva_bias;
3508 bfd_byte * data;
3509 bfd_byte * datastart;
3510 bfd_byte * dataend;
3511 bfd_byte * new_data;
3512 unsigned int num_resource_sets;
3513 rsrc_directory * type_tables;
3514 rsrc_write_data write_data;
3515 unsigned int indx;
6caf7111
NC
3516 bfd * input;
3517 unsigned int num_input_rsrc = 0;
3518 unsigned int max_num_input_rsrc = 4;
3519 ptrdiff_t * rsrc_sizes = NULL;
5879bb8f
NC
3520
3521 new_table.names.num_entries = 0;
3522 new_table.ids.num_entries = 0;
9373215c 3523
5879bb8f
NC
3524 sec = bfd_get_section_by_name (abfd, ".rsrc");
3525 if (sec == NULL || (size = sec->rawsize) == 0)
3526 return;
3527
9373215c 3528 pe = pe_data (abfd);
5879bb8f
NC
3529 if (pe == NULL)
3530 return;
3531
5879bb8f
NC
3532 rva_bias = sec->vma - pe->pe_opthdr.ImageBase;
3533
9373215c 3534 data = bfd_malloc (size);
5879bb8f
NC
3535 if (data == NULL)
3536 return;
9373215c 3537 datastart = data;
5879bb8f
NC
3538
3539 if (! bfd_get_section_contents (abfd, sec, data, 0, size))
3540 goto end;
3541
6caf7111
NC
3542 /* Step zero: Scan the input bfds looking for .rsrc sections and record
3543 their lengths. Note - we rely upon the fact that the linker script
3544 does *not* sort the input .rsrc sections, so that the order in the
3545 linkinfo list matches the order in the output .rsrc section.
3546
3547 We need to know the lengths because each input .rsrc section has padding
3548 at the end of a variable amount. (It does not appear to be based upon
3549 the section alignment or the file alignment). We need to skip any
3550 padding bytes when parsing the input .rsrc sections. */
3551 rsrc_sizes = bfd_malloc (max_num_input_rsrc * sizeof * rsrc_sizes);
3552 if (rsrc_sizes == NULL)
3553 goto end;
3554
3555 for (input = pfinfo->info->input_bfds;
3556 input != NULL;
3557 input = input->link_next)
3558 {
3559 asection * rsrc_sec = bfd_get_section_by_name (input, ".rsrc");
3560
3561 if (rsrc_sec != NULL)
3562 {
3563 if (num_input_rsrc == max_num_input_rsrc)
3564 {
3565 max_num_input_rsrc += 10;
3566 rsrc_sizes = bfd_realloc (rsrc_sizes, max_num_input_rsrc
3567 * sizeof * rsrc_sizes);
3568 if (rsrc_sizes == NULL)
3569 goto end;
3570 }
3571
3572 BFD_ASSERT (rsrc_sec->size > 0);
3573 rsrc_sizes [num_input_rsrc ++] = rsrc_sec->size;
3574 }
3575 }
3576
3577 if (num_input_rsrc < 2)
3578 goto end;
3579
5879bb8f
NC
3580 /* Step one: Walk the section, computing the size of the tables,
3581 leaves and data and decide if we need to do anything. */
1d63324c 3582 dataend = data + size;
9373215c 3583 num_resource_sets = 0;
5879bb8f
NC
3584 sizeof_leaves = sizeof_strings = sizeof_tables_and_entries = 0;
3585
3586 while (data < dataend)
3587 {
3588 bfd_byte * p = data;
3589
3590 data = rsrc_count_directory (abfd, data, data, dataend, rva_bias);
1d63324c 3591
5879bb8f
NC
3592 if (data > dataend)
3593 {
3594 /* Corrupted .rsrc section - cannot merge. */
3595 _bfd_error_handler (_("%s: .rsrc merge failure: corrupt .rsrc section"),
3596 bfd_get_filename (abfd));
3597 bfd_set_error (bfd_error_file_truncated);
3598 goto end;
3599 }
3600
6caf7111
NC
3601 if ((data - p) > rsrc_sizes [num_resource_sets])
3602 {
3603 _bfd_error_handler (_("%s: .rsrc merge failure: unexpected .rsrc size"),
3604 bfd_get_filename (abfd));
3605 bfd_set_error (bfd_error_file_truncated);
3606 goto end;
3607 }
3608 /* FIXME: Should we add a check for "data - p" being much smaller
3609 than rsrc_sizes[num_resource_sets] ? */
3610
3611 data = p + rsrc_sizes[num_resource_sets];
5879bb8f 3612 rva_bias += data - p;
5879bb8f
NC
3613 ++ num_resource_sets;
3614 }
6caf7111 3615 BFD_ASSERT (num_resource_sets == num_input_rsrc);
5879bb8f
NC
3616
3617 /* Step two: Walk the data again, building trees of the resources. */
3618 data = datastart;
3619 rva_bias = sec->vma - pe->pe_opthdr.ImageBase;
3620
9373215c 3621 type_tables = bfd_malloc (num_resource_sets * sizeof * type_tables);
5879bb8f
NC
3622 if (type_tables == NULL)
3623 goto end;
3624
9373215c 3625 indx = 0;
5879bb8f
NC
3626 while (data < dataend)
3627 {
3628 bfd_byte * p = data;
3629
6caf7111 3630 (void) rsrc_parse_directory (abfd, type_tables + indx, data, data,
9373215c 3631 dataend, rva_bias, NULL);
6caf7111 3632 data = p + rsrc_sizes[indx];
5879bb8f 3633 rva_bias += data - p;
6caf7111 3634 ++ indx;
5879bb8f 3635 }
337e86d7 3636 BFD_ASSERT (indx == num_resource_sets);
9373215c 3637
5879bb8f 3638 /* Step three: Merge the top level tables (there can be only one).
9373215c 3639
5879bb8f 3640 We must ensure that the merged entries are in ascending order.
9373215c 3641
5879bb8f
NC
3642 We also thread the top level table entries from the old tree onto
3643 the new table, so that they can be pulled off later. */
3644
3645 /* FIXME: Should we verify that all type tables are the same ? */
3646 new_table.characteristics = type_tables[0].characteristics;
3647 new_table.time = type_tables[0].time;
3648 new_table.major = type_tables[0].major;
3649 new_table.minor = type_tables[0].minor;
3650
3651 /* Chain the NAME entries onto the table. */
3652 new_table.names.first_entry = NULL;
3653 new_table.names.last_entry = NULL;
3654
337e86d7
L
3655 for (indx = 0; indx < num_resource_sets; indx++)
3656 rsrc_attach_chain (& new_table.names, & type_tables[indx].names);
5879bb8f
NC
3657
3658 rsrc_sort_entries (& new_table.names, TRUE, & new_table);
9373215c 3659
5879bb8f
NC
3660 /* Chain the ID entries onto the table. */
3661 new_table.ids.first_entry = NULL;
3662 new_table.ids.last_entry = NULL;
3663
337e86d7
L
3664 for (indx = 0; indx < num_resource_sets; indx++)
3665 rsrc_attach_chain (& new_table.ids, & type_tables[indx].ids);
5879bb8f
NC
3666
3667 rsrc_sort_entries (& new_table.ids, FALSE, & new_table);
3668
3669 /* Step four: Create new contents for the .rsrc section. */
9373215c 3670 new_data = bfd_malloc (size);
5879bb8f
NC
3671 if (new_data == NULL)
3672 goto end;
3673
5879bb8f
NC
3674 write_data.abfd = abfd;
3675 write_data.datastart = new_data;
3676 write_data.next_table = new_data;
3677 write_data.next_leaf = new_data + sizeof_tables_and_entries;
3678 write_data.next_string = write_data.next_leaf + sizeof_leaves;
3679 write_data.next_data = write_data.next_string + sizeof_strings;
9373215c 3680 write_data.rva_bias = sec->vma - pe->pe_opthdr.ImageBase;
5879bb8f
NC
3681
3682 rsrc_write_directory (& write_data, & new_table);
3683
3684 /* Step five: Replace the old contents with the new.
3685 We recompute the size as we may have lost entries due to mergeing. */
9373215c 3686 size = ((write_data.next_data - new_data) + 3) & ~ 3;
5879bb8f
NC
3687 bfd_set_section_contents (pfinfo->output_bfd, sec, new_data, 0, size);
3688 sec->size = sec->rawsize = size;
9373215c 3689
5879bb8f 3690 end:
6caf7111 3691 /* Step size: Free all the memory that we have used. */
5879bb8f
NC
3692 /* FIXME: Free the resource tree, if we have one. */
3693 free (datastart);
6caf7111 3694 free (rsrc_sizes);
5879bb8f 3695}
5174d0fb 3696
2fbadf2c
ILT
3697/* Handle the .idata section and other things that need symbol table
3698 access. */
3699
b34976b6 3700bfd_boolean
7920ce38 3701_bfd_XXi_final_link_postscript (bfd * abfd, struct coff_final_link_info *pfinfo)
2fbadf2c
ILT
3702{
3703 struct coff_link_hash_entry *h1;
3704 struct bfd_link_info *info = pfinfo->info;
4e22f78d 3705 bfd_boolean result = TRUE;
2fbadf2c
ILT
3706
3707 /* There are a few fields that need to be filled in now while we
3708 have symbol table access.
3709
3710 The .idata subsections aren't directly available as sections, but
3711 they are in the symbol table, so get them from there. */
3712
3713 /* The import directory. This is the address of .idata$2, with size
3714 of .idata$2 + .idata$3. */
3715 h1 = coff_link_hash_lookup (coff_hash_table (info),
b34976b6 3716 ".idata$2", FALSE, FALSE, TRUE);
2fbadf2c
ILT
3717 if (h1 != NULL)
3718 {
4e1fc599 3719 /* PR ld/2729: We cannot rely upon all the output sections having been
4e22f78d
NC
3720 created properly, so check before referencing them. Issue a warning
3721 message for any sections tht could not be found. */
b92997d6
AM
3722 if ((h1->root.type == bfd_link_hash_defined
3723 || h1->root.type == bfd_link_hash_defweak)
3724 && h1->root.u.def.section != NULL
4e22f78d 3725 && h1->root.u.def.section->output_section != NULL)
6c73cbb1 3726 pe_data (abfd)->pe_opthdr.DataDirectory[PE_IMPORT_TABLE].VirtualAddress =
4e22f78d
NC
3727 (h1->root.u.def.value
3728 + h1->root.u.def.section->output_section->vma
3729 + h1->root.u.def.section->output_offset);
3730 else
3731 {
3732 _bfd_error_handler
4e1fc599 3733 (_("%B: unable to fill in DataDictionary[1] because .idata$2 is missing"),
4e22f78d
NC
3734 abfd);
3735 result = FALSE;
3736 }
3737
2fbadf2c 3738 h1 = coff_link_hash_lookup (coff_hash_table (info),
b34976b6 3739 ".idata$4", FALSE, FALSE, TRUE);
4e22f78d 3740 if (h1 != NULL
b92997d6
AM
3741 && (h1->root.type == bfd_link_hash_defined
3742 || h1->root.type == bfd_link_hash_defweak)
4e22f78d
NC
3743 && h1->root.u.def.section != NULL
3744 && h1->root.u.def.section->output_section != NULL)
6c73cbb1 3745 pe_data (abfd)->pe_opthdr.DataDirectory[PE_IMPORT_TABLE].Size =
4e22f78d
NC
3746 ((h1->root.u.def.value
3747 + h1->root.u.def.section->output_section->vma
3748 + h1->root.u.def.section->output_offset)
6c73cbb1 3749 - pe_data (abfd)->pe_opthdr.DataDirectory[PE_IMPORT_TABLE].VirtualAddress);
4e22f78d
NC
3750 else
3751 {
3752 _bfd_error_handler
4e1fc599 3753 (_("%B: unable to fill in DataDictionary[1] because .idata$4 is missing"),
4e22f78d
NC
3754 abfd);
3755 result = FALSE;
3756 }
2fbadf2c
ILT
3757
3758 /* The import address table. This is the size/address of
3759 .idata$5. */
3760 h1 = coff_link_hash_lookup (coff_hash_table (info),
b34976b6 3761 ".idata$5", FALSE, FALSE, TRUE);
4e22f78d 3762 if (h1 != NULL
b92997d6
AM
3763 && (h1->root.type == bfd_link_hash_defined
3764 || h1->root.type == bfd_link_hash_defweak)
4e22f78d
NC
3765 && h1->root.u.def.section != NULL
3766 && h1->root.u.def.section->output_section != NULL)
6c73cbb1 3767 pe_data (abfd)->pe_opthdr.DataDirectory[PE_IMPORT_ADDRESS_TABLE].VirtualAddress =
4e22f78d
NC
3768 (h1->root.u.def.value
3769 + h1->root.u.def.section->output_section->vma
3770 + h1->root.u.def.section->output_offset);
3771 else
3772 {
3773 _bfd_error_handler
4e1fc599 3774 (_("%B: unable to fill in DataDictionary[12] because .idata$5 is missing"),
4e22f78d
NC
3775 abfd);
3776 result = FALSE;
3777 }
3778
2fbadf2c 3779 h1 = coff_link_hash_lookup (coff_hash_table (info),
b34976b6 3780 ".idata$6", FALSE, FALSE, TRUE);
4e22f78d 3781 if (h1 != NULL
b92997d6
AM
3782 && (h1->root.type == bfd_link_hash_defined
3783 || h1->root.type == bfd_link_hash_defweak)
4e22f78d
NC
3784 && h1->root.u.def.section != NULL
3785 && h1->root.u.def.section->output_section != NULL)
6c73cbb1 3786 pe_data (abfd)->pe_opthdr.DataDirectory[PE_IMPORT_ADDRESS_TABLE].Size =
4e22f78d
NC
3787 ((h1->root.u.def.value
3788 + h1->root.u.def.section->output_section->vma
3789 + h1->root.u.def.section->output_offset)
4e1fc599 3790 - pe_data (abfd)->pe_opthdr.DataDirectory[PE_IMPORT_ADDRESS_TABLE].VirtualAddress);
4e22f78d
NC
3791 else
3792 {
3793 _bfd_error_handler
4e1fc599 3794 (_("%B: unable to fill in DataDictionary[PE_IMPORT_ADDRESS_TABLE (12)] because .idata$6 is missing"),
4e22f78d
NC
3795 abfd);
3796 result = FALSE;
3797 }
2fbadf2c 3798 }
cb2f80e6
KT
3799 else
3800 {
3801 h1 = coff_link_hash_lookup (coff_hash_table (info),
3802 "__IAT_start__", FALSE, FALSE, TRUE);
3803 if (h1 != NULL
3804 && (h1->root.type == bfd_link_hash_defined
3805 || h1->root.type == bfd_link_hash_defweak)
3806 && h1->root.u.def.section != NULL
3807 && h1->root.u.def.section->output_section != NULL)
3808 {
3809 bfd_vma iat_va;
3810
3811 iat_va =
3812 (h1->root.u.def.value
3813 + h1->root.u.def.section->output_section->vma
3814 + h1->root.u.def.section->output_offset);
3815
3816 h1 = coff_link_hash_lookup (coff_hash_table (info),
3817 "__IAT_end__", FALSE, FALSE, TRUE);
3818 if (h1 != NULL
3819 && (h1->root.type == bfd_link_hash_defined
3820 || h1->root.type == bfd_link_hash_defweak)
3821 && h1->root.u.def.section != NULL
3822 && h1->root.u.def.section->output_section != NULL)
3823 {
3824 pe_data (abfd)->pe_opthdr.DataDirectory[PE_IMPORT_ADDRESS_TABLE].Size =
3825 ((h1->root.u.def.value
3826 + h1->root.u.def.section->output_section->vma
3827 + h1->root.u.def.section->output_offset)
3828 - iat_va);
3829 if (pe_data (abfd)->pe_opthdr.DataDirectory[PE_IMPORT_ADDRESS_TABLE].Size != 0)
3830 pe_data (abfd)->pe_opthdr.DataDirectory[PE_IMPORT_ADDRESS_TABLE].VirtualAddress =
3831 iat_va - pe_data (abfd)->pe_opthdr.ImageBase;
3832 }
3833 else
3834 {
3835 _bfd_error_handler
3836 (_("%B: unable to fill in DataDictionary[PE_IMPORT_ADDRESS_TABLE(12)]"
3837 " because .idata$6 is missing"), abfd);
3838 result = FALSE;
3839 }
3840 }
3841 }
ca6dee30
NC
3842
3843 h1 = coff_link_hash_lookup (coff_hash_table (info),
c91a930c
KT
3844 (bfd_get_symbol_leading_char(abfd) != 0
3845 ? "__tls_used" : "_tls_used"),
3846 FALSE, FALSE, TRUE);
ca6dee30
NC
3847 if (h1 != NULL)
3848 {
b92997d6
AM
3849 if ((h1->root.type == bfd_link_hash_defined
3850 || h1->root.type == bfd_link_hash_defweak)
3851 && h1->root.u.def.section != NULL
4e22f78d 3852 && h1->root.u.def.section->output_section != NULL)
6c73cbb1 3853 pe_data (abfd)->pe_opthdr.DataDirectory[PE_TLS_TABLE].VirtualAddress =
4e22f78d
NC
3854 (h1->root.u.def.value
3855 + h1->root.u.def.section->output_section->vma
3856 + h1->root.u.def.section->output_offset
3857 - pe_data (abfd)->pe_opthdr.ImageBase);
3858 else
3859 {
3860 _bfd_error_handler
4e1fc599 3861 (_("%B: unable to fill in DataDictionary[9] because __tls_used is missing"),
4e22f78d
NC
3862 abfd);
3863 result = FALSE;
3864 }
bc2b2990
PM
3865 /* According to PECOFF sepcifications by Microsoft version 8.2
3866 the TLS data directory consists of 4 pointers, followed
3867 by two 4-byte integer. This implies that the total size
68ffbac6 3868 is different for 32-bit and 64-bit executables. */
bc2b2990 3869#if !defined(COFF_WITH_pep) && !defined(COFF_WITH_pex64)
6c73cbb1 3870 pe_data (abfd)->pe_opthdr.DataDirectory[PE_TLS_TABLE].Size = 0x18;
bc2b2990
PM
3871#else
3872 pe_data (abfd)->pe_opthdr.DataDirectory[PE_TLS_TABLE].Size = 0x28;
3873#endif
ca6dee30
NC
3874 }
3875
5174d0fb
KT
3876/* If there is a .pdata section and we have linked pdata finally, we
3877 need to sort the entries ascending. */
3878#if !defined(COFF_WITH_pep) && defined(COFF_WITH_pex64)
3879 {
3880 asection *sec = bfd_get_section_by_name (abfd, ".pdata");
3881
3882 if (sec)
3883 {
21e68916
KT
3884 bfd_size_type x = sec->rawsize;
3885 bfd_byte *tmp_data = NULL;
5174d0fb 3886
21e68916
KT
3887 if (x)
3888 tmp_data = bfd_malloc (x);
3889
3890 if (tmp_data != NULL)
5174d0fb 3891 {
21e68916
KT
3892 if (bfd_get_section_contents (abfd, sec, tmp_data, 0, x))
3893 {
3894 qsort (tmp_data,
3895 (size_t) (x / 12),
3896 12, sort_x64_pdata);
3897 bfd_set_section_contents (pfinfo->output_bfd, sec,
3898 tmp_data, 0, x);
3899 }
3900 free (tmp_data);
5174d0fb
KT
3901 }
3902 }
3903 }
3904#endif
3905
5879bb8f
NC
3906 rsrc_process_section (abfd, pfinfo);
3907
2fbadf2c
ILT
3908 /* If we couldn't find idata$2, we either have an excessively
3909 trivial program or are in DEEP trouble; we have to assume trivial
3910 program.... */
4e22f78d 3911 return result;
2fbadf2c 3912}
This page took 1.082197 seconds and 4 git commands to generate.