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