]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - bfd/peXXigen.c
Another PE SEC_HAS_CONTENTS test
[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");
81ff113f 1291 if (section == NULL || (section->flags & SEC_HAS_CONTENTS) == 0)
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 1847 if (section == NULL
81ff113f 1848 || (section->flags & SEC_HAS_CONTENTS) == 0
5933bdc9
ILT
1849 || coff_section_data (abfd, section) == NULL
1850 || pei_section_data (abfd, section) == NULL)
0a1b45a2 1851 return true;
277d1b5e 1852
5933bdc9 1853 stop = pei_section_data (abfd, section)->virt_size;
277d1b5e 1854 if ((stop % onaline) != 0)
6fa957a9 1855 fprintf (file,
695344c0 1856 /* xgettext:c-format */
59d08d6c 1857 _("warning, .pdata section size (%ld) is not a multiple of %d\n"),
6fa957a9 1858 (long) stop, onaline);
277d1b5e 1859
5933bdc9
ILT
1860 fprintf (file,
1861 _("\nThe Function Table (interpreted .pdata section contents)\n"));
31f60095 1862#if defined(COFF_WITH_pep) && !defined(COFF_WITH_pex64) && !defined(COFF_WITH_peAArch64) && !defined(COFF_WITH_peLoongArch64)
9602af51 1863 fprintf (file,
6fa957a9 1864 _(" vma:\t\t\tBegin Address End Address Unwind Info\n"));
fac41780 1865#else
ca09e32b
NC
1866 fprintf (file, _("\
1867 vma:\t\tBegin End EH EH PrologEnd Exception\n\
1868 \t\tAddress Address Handler Data Address Mask\n"));
fac41780 1869#endif
277d1b5e 1870
eea6121a 1871 datasize = section->size;
dc810e39 1872 if (datasize == 0)
0a1b45a2 1873 return true;
277d1b5e 1874
6937bb54
NC
1875 /* PR 17512: file: 002-193900-0.004. */
1876 if (datasize < stop)
1877 {
695344c0 1878 /* xgettext:c-format */
6937bb54
NC
1879 fprintf (file, _("Virtual size of .pdata section (%ld) larger than real size (%ld)\n"),
1880 (long) stop, (long) datasize);
0a1b45a2 1881 return false;
6937bb54
NC
1882 }
1883
7920ce38 1884 if (! bfd_malloc_and_get_section (abfd, section, &data))
eea6121a 1885 {
c9594989 1886 free (data);
0a1b45a2 1887 return false;
eea6121a 1888 }
277d1b5e
ILT
1889
1890 start = 0;
1891
1892 for (i = start; i < stop; i += onaline)
1893 {
1894 bfd_vma begin_addr;
1895 bfd_vma end_addr;
1896 bfd_vma eh_handler;
1897 bfd_vma eh_data;
1898 bfd_vma prolog_end_addr;
31f60095 1899#if !defined(COFF_WITH_pep) || defined(COFF_WITH_pex64) || defined(COFF_WITH_peAArch64) || defined(COFF_WITH_peLoongArch64)
5933bdc9 1900 int em_data;
c7e2358a 1901#endif
277d1b5e 1902
fac41780 1903 if (i + PDATA_ROW_SIZE > stop)
277d1b5e 1904 break;
5933bdc9 1905
07d6d2b8
AM
1906 begin_addr = GET_PDATA_ENTRY (abfd, data + i );
1907 end_addr = GET_PDATA_ENTRY (abfd, data + i + 4);
6fa957a9 1908 eh_handler = GET_PDATA_ENTRY (abfd, data + i + 8);
07d6d2b8 1909 eh_data = GET_PDATA_ENTRY (abfd, data + i + 12);
6fa957a9 1910 prolog_end_addr = GET_PDATA_ENTRY (abfd, data + i + 16);
9602af51 1911
277d1b5e
ILT
1912 if (begin_addr == 0 && end_addr == 0 && eh_handler == 0
1913 && eh_data == 0 && prolog_end_addr == 0)
1725a96e
NC
1914 /* We are probably into the padding of the section now. */
1915 break;
277d1b5e 1916
31f60095 1917#if !defined(COFF_WITH_pep) || defined(COFF_WITH_pex64) || defined(COFF_WITH_peAArch64) || defined(COFF_WITH_peLoongArch64)
5933bdc9 1918 em_data = ((eh_handler & 0x1) << 2) | (prolog_end_addr & 0x3);
c7e2358a 1919#endif
6fa957a9
KH
1920 eh_handler &= ~(bfd_vma) 0x3;
1921 prolog_end_addr &= ~(bfd_vma) 0x3;
fac41780
JW
1922
1923 fputc (' ', file);
ebf12fbe
DK
1924 bfd_fprintf_vma (abfd, file, i + section->vma); fputc ('\t', file);
1925 bfd_fprintf_vma (abfd, file, begin_addr); fputc (' ', file);
1926 bfd_fprintf_vma (abfd, file, end_addr); fputc (' ', file);
1927 bfd_fprintf_vma (abfd, file, eh_handler);
31f60095 1928#if !defined(COFF_WITH_pep) || defined(COFF_WITH_pex64) || defined(COFF_WITH_peAArch64) || defined(COFF_WITH_peLoongArch64)
fac41780 1929 fputc (' ', file);
ebf12fbe
DK
1930 bfd_fprintf_vma (abfd, file, eh_data); fputc (' ', file);
1931 bfd_fprintf_vma (abfd, file, prolog_end_addr);
fac41780
JW
1932 fprintf (file, " %x", em_data);
1933#endif
9602af51 1934 fprintf (file, "\n");
277d1b5e
ILT
1935 }
1936
1937 free (data);
1938
0a1b45a2 1939 return true;
2b5c217d 1940#undef PDATA_ROW_SIZE
277d1b5e
ILT
1941}
1942
799c00e0
NC
1943typedef struct sym_cache
1944{
07d6d2b8 1945 int symcount;
799c00e0
NC
1946 asymbol ** syms;
1947} sym_cache;
1948
1949static asymbol **
1950slurp_symtab (bfd *abfd, sym_cache *psc)
1951{
1952 asymbol ** sy = NULL;
1953 long storage;
1954
1955 if (!(bfd_get_file_flags (abfd) & HAS_SYMS))
1956 {
1957 psc->symcount = 0;
1958 return NULL;
1959 }
1960
1961 storage = bfd_get_symtab_upper_bound (abfd);
1962 if (storage < 0)
1963 return NULL;
1964 if (storage)
86eafac0
NC
1965 {
1966 sy = (asymbol **) bfd_malloc (storage);
1967 if (sy == NULL)
1968 return NULL;
1969 }
799c00e0
NC
1970
1971 psc->symcount = bfd_canonicalize_symtab (abfd, sy);
1972 if (psc->symcount < 0)
1973 return NULL;
1974 return sy;
1975}
1976
1977static const char *
1978my_symbol_for_address (bfd *abfd, bfd_vma func, sym_cache *psc)
1979{
1980 int i;
1981
1982 if (psc->syms == 0)
1983 psc->syms = slurp_symtab (abfd, psc);
1984
1985 for (i = 0; i < psc->symcount; i++)
1986 {
1987 if (psc->syms[i]->section->vma + psc->syms[i]->value == func)
1988 return psc->syms[i]->name;
1989 }
1990
1991 return NULL;
1992}
1993
1994static void
1995cleanup_syms (sym_cache *psc)
1996{
1997 psc->symcount = 0;
1998 free (psc->syms);
1999 psc->syms = NULL;
2000}
2001
2002/* This is the version for "compressed" pdata. */
2003
0a1b45a2 2004bool
799c00e0
NC
2005_bfd_XX_print_ce_compressed_pdata (bfd * abfd, void * vfile)
2006{
2007# define PDATA_ROW_SIZE (2 * 4)
2008 FILE *file = (FILE *) vfile;
2009 bfd_byte *data = NULL;
2010 asection *section = bfd_get_section_by_name (abfd, ".pdata");
2011 bfd_size_type datasize = 0;
2012 bfd_size_type i;
2013 bfd_size_type start, stop;
2014 int onaline = PDATA_ROW_SIZE;
91d6fa6a 2015 struct sym_cache cache = {0, 0} ;
799c00e0
NC
2016
2017 if (section == NULL
81ff113f 2018 || (section->flags & SEC_HAS_CONTENTS) == 0
799c00e0
NC
2019 || coff_section_data (abfd, section) == NULL
2020 || pei_section_data (abfd, section) == NULL)
0a1b45a2 2021 return true;
799c00e0
NC
2022
2023 stop = pei_section_data (abfd, section)->virt_size;
2024 if ((stop % onaline) != 0)
2025 fprintf (file,
695344c0 2026 /* xgettext:c-format */
59d08d6c 2027 _("warning, .pdata section size (%ld) is not a multiple of %d\n"),
799c00e0
NC
2028 (long) stop, onaline);
2029
2030 fprintf (file,
2031 _("\nThe Function Table (interpreted .pdata section contents)\n"));
2032
2033 fprintf (file, _("\
2034 vma:\t\tBegin Prolog Function Flags Exception EH\n\
2035 \t\tAddress Length Length 32b exc Handler Data\n"));
2036
2037 datasize = section->size;
2038 if (datasize == 0)
0a1b45a2 2039 return true;
799c00e0
NC
2040
2041 if (! bfd_malloc_and_get_section (abfd, section, &data))
2042 {
c9594989 2043 free (data);
0a1b45a2 2044 return false;
799c00e0
NC
2045 }
2046
2047 start = 0;
f84ffabb
AM
2048 if (stop > datasize)
2049 stop = datasize;
799c00e0
NC
2050
2051 for (i = start; i < stop; i += onaline)
2052 {
2053 bfd_vma begin_addr;
2054 bfd_vma other_data;
2055 bfd_vma prolog_length, function_length;
2056 int flag32bit, exception_flag;
799c00e0
NC
2057 asection *tsection;
2058
2059 if (i + PDATA_ROW_SIZE > stop)
2060 break;
2061
2062 begin_addr = GET_PDATA_ENTRY (abfd, data + i );
2063 other_data = GET_PDATA_ENTRY (abfd, data + i + 4);
2064
2065 if (begin_addr == 0 && other_data == 0)
2066 /* We are probably into the padding of the section now. */
2067 break;
2068
2069 prolog_length = (other_data & 0x000000FF);
2070 function_length = (other_data & 0x3FFFFF00) >> 8;
2071 flag32bit = (int)((other_data & 0x40000000) >> 30);
2072 exception_flag = (int)((other_data & 0x80000000) >> 31);
2073
2074 fputc (' ', file);
ebf12fbe
DK
2075 bfd_fprintf_vma (abfd, file, i + section->vma); fputc ('\t', file);
2076 bfd_fprintf_vma (abfd, file, begin_addr); fputc (' ', file);
2077 bfd_fprintf_vma (abfd, file, prolog_length); fputc (' ', file);
2078 bfd_fprintf_vma (abfd, file, function_length); fputc (' ', file);
799c00e0
NC
2079 fprintf (file, "%2d %2d ", flag32bit, exception_flag);
2080
2081 /* Get the exception handler's address and the data passed from the
07d6d2b8
AM
2082 .text section. This is really the data that belongs with the .pdata
2083 but got "compressed" out for the ARM and SH4 architectures. */
799c00e0
NC
2084 tsection = bfd_get_section_by_name (abfd, ".text");
2085 if (tsection && coff_section_data (abfd, tsection)
2086 && pei_section_data (abfd, tsection))
2087 {
4e1fc599
AM
2088 bfd_vma eh_off = (begin_addr - 8) - tsection->vma;
2089 bfd_byte *tdata;
799c00e0 2090
4e1fc599
AM
2091 tdata = (bfd_byte *) bfd_malloc (8);
2092 if (tdata)
2093 {
2094 if (bfd_get_section_contents (abfd, tsection, tdata, eh_off, 8))
799c00e0
NC
2095 {
2096 bfd_vma eh, eh_data;
2097
2098 eh = bfd_get_32 (abfd, tdata);
2099 eh_data = bfd_get_32 (abfd, tdata + 4);
2100 fprintf (file, "%08x ", (unsigned int) eh);
2101 fprintf (file, "%08x", (unsigned int) eh_data);
2102 if (eh != 0)
2103 {
91d6fa6a 2104 const char *s = my_symbol_for_address (abfd, eh, &cache);
799c00e0
NC
2105
2106 if (s)
2107 fprintf (file, " (%s) ", s);
2108 }
2109 }
2110 free (tdata);
2111 }
799c00e0
NC
2112 }
2113
2114 fprintf (file, "\n");
2115 }
2116
2117 free (data);
2118
91d6fa6a 2119 cleanup_syms (& cache);
799c00e0 2120
0a1b45a2 2121 return true;
799c00e0
NC
2122#undef PDATA_ROW_SIZE
2123}
c7c7219d 2124
799c00e0 2125\f
5933bdc9 2126#define IMAGE_REL_BASED_HIGHADJ 4
1725a96e 2127static const char * const tbl[] =
7920ce38
NC
2128{
2129 "ABSOLUTE",
2130 "HIGH",
2131 "LOW",
2132 "HIGHLOW",
2133 "HIGHADJ",
2134 "MIPS_JMPADDR",
2135 "SECTION",
2136 "REL32",
2137 "RESERVED1",
2138 "MIPS_JMPADDR16",
2139 "DIR64",
2bfd55ca 2140 "HIGH3ADJ",
7920ce38
NC
2141 "UNKNOWN", /* MUST be last. */
2142};
277d1b5e 2143
0a1b45a2 2144static bool
7920ce38 2145pe_print_reloc (bfd * abfd, void * vfile)
277d1b5e
ILT
2146{
2147 FILE *file = (FILE *) vfile;
2148 bfd_byte *data = 0;
2149 asection *section = bfd_get_section_by_name (abfd, ".reloc");
513ea82e 2150 bfd_byte *p, *end;
277d1b5e 2151
81ff113f
AM
2152 if (section == NULL
2153 || section->size == 0
2154 || (section->flags & SEC_HAS_CONTENTS) == 0)
0a1b45a2 2155 return true;
277d1b5e 2156
5933bdc9
ILT
2157 fprintf (file,
2158 _("\n\nPE File Base Relocations (interpreted .reloc section contents)\n"));
277d1b5e 2159
7920ce38 2160 if (! bfd_malloc_and_get_section (abfd, section, &data))
eea6121a 2161 {
c9594989 2162 free (data);
0a1b45a2 2163 return false;
eea6121a 2164 }
277d1b5e 2165
513ea82e
AM
2166 p = data;
2167 end = data + section->size;
2168 while (p + 8 <= end)
277d1b5e
ILT
2169 {
2170 int j;
2171 bfd_vma virtual_address;
77ef8654 2172 unsigned long number, size;
513ea82e 2173 bfd_byte *chunk_end;
277d1b5e
ILT
2174
2175 /* The .reloc section is a sequence of blocks, with a header consisting
1725a96e 2176 of two 32 bit quantities, followed by a number of 16 bit entries. */
513ea82e
AM
2177 virtual_address = bfd_get_32 (abfd, p);
2178 size = bfd_get_32 (abfd, p + 4);
2179 p += 8;
277d1b5e
ILT
2180 number = (size - 8) / 2;
2181
2182 if (size == 0)
1725a96e 2183 break;
277d1b5e
ILT
2184
2185 fprintf (file,
695344c0 2186 /* xgettext:c-format */
277d1b5e 2187 _("\nVirtual Address: %08lx Chunk size %ld (0x%lx) Number of fixups %ld\n"),
77ef8654 2188 (unsigned long) virtual_address, size, size, number);
277d1b5e 2189
10169134 2190 chunk_end = p - 8 + size;
513ea82e
AM
2191 if (chunk_end > end)
2192 chunk_end = end;
2193 j = 0;
2194 while (p + 2 <= chunk_end)
277d1b5e 2195 {
513ea82e 2196 unsigned short e = bfd_get_16 (abfd, p);
5933bdc9 2197 unsigned int t = (e & 0xF000) >> 12;
277d1b5e
ILT
2198 int off = e & 0x0FFF;
2199
5933bdc9
ILT
2200 if (t >= sizeof (tbl) / sizeof (tbl[0]))
2201 t = (sizeof (tbl) / sizeof (tbl[0])) - 1;
277d1b5e 2202
5933bdc9 2203 fprintf (file,
695344c0 2204 /* xgettext:c-format */
5933bdc9 2205 _("\treloc %4d offset %4x [%4lx] %s"),
0af1713e 2206 j, off, (unsigned long) (off + virtual_address), tbl[t]);
277d1b5e 2207
513ea82e
AM
2208 p += 2;
2209 j++;
2210
17505c5c 2211 /* HIGHADJ takes an argument, - the next record *is* the
9602af51 2212 low 16 bits of addend. */
513ea82e 2213 if (t == IMAGE_REL_BASED_HIGHADJ && p + 2 <= chunk_end)
5933bdc9 2214 {
513ea82e
AM
2215 fprintf (file, " (%4x)", (unsigned int) bfd_get_16 (abfd, p));
2216 p += 2;
6fa957a9 2217 j++;
5933bdc9 2218 }
9602af51 2219
17505c5c 2220 fprintf (file, "\n");
277d1b5e 2221 }
277d1b5e
ILT
2222 }
2223
2224 free (data);
2225
0a1b45a2 2226 return true;
277d1b5e 2227}
5879bb8f 2228\f
3714081c
NC
2229/* A data structure describing the regions of a .rsrc section.
2230 Some fields are filled in as the section is parsed. */
2231
2232typedef struct rsrc_regions
2233{
2234 bfd_byte * section_start;
2235 bfd_byte * section_end;
2236 bfd_byte * strings_start;
2237 bfd_byte * resource_start;
2238} rsrc_regions;
277d1b5e 2239
11a6da56 2240static bfd_byte *
3714081c
NC
2241rsrc_print_resource_directory (FILE * , bfd *, unsigned int, bfd_byte *,
2242 rsrc_regions *, bfd_vma);
11a6da56 2243
20ad5e28
NC
2244/* Print the resource entry at DATA, with the text indented by INDENT.
2245 Recusively calls rsrc_print_resource_directory to print the contents
2246 of directory entries.
2247 Returns the address of the end of the data associated with the entry
2248 or section_end + 1 upon failure. */
2249
11a6da56 2250static bfd_byte *
0a1b45a2
AM
2251rsrc_print_resource_entries (FILE *file,
2252 bfd *abfd,
2253 unsigned int indent,
2254 bool is_name,
2255 bfd_byte *data,
2256 rsrc_regions *regions,
2257 bfd_vma rva_bias)
11a6da56
NC
2258{
2259 unsigned long entry, addr, size;
5929c344 2260 bfd_byte * leaf;
11a6da56 2261
3714081c
NC
2262 if (data + 8 >= regions->section_end)
2263 return regions->section_end + 1;
11a6da56 2264
695344c0 2265 /* xgettext:c-format */
3714081c 2266 fprintf (file, _("%03x %*.s Entry: "), (int)(data - regions->section_start), indent, " ");
11a6da56 2267
20ad5e28 2268 entry = (unsigned long) bfd_get_32 (abfd, data);
11a6da56
NC
2269 if (is_name)
2270 {
5879bb8f
NC
2271 bfd_byte * name;
2272
3714081c 2273 /* Note - the documentation says that this field is an RVA value
5879bb8f
NC
2274 but windres appears to produce a section relative offset with
2275 the top bit set. Support both styles for now. */
2276 if (HighBitSet (entry))
3714081c 2277 name = regions->section_start + WithoutHighBit (entry);
5879bb8f 2278 else
3714081c 2279 name = regions->section_start + entry - rva_bias;
5879bb8f 2280
20ad5e28 2281 if (name + 2 < regions->section_end && name > regions->section_start)
11a6da56
NC
2282 {
2283 unsigned int len;
3714081c
NC
2284
2285 if (regions->strings_start == NULL)
2286 regions->strings_start = name;
2287
5879bb8f 2288 len = bfd_get_16 (abfd, name);
9373215c 2289
5879bb8f 2290 fprintf (file, _("name: [val: %08lx len %d]: "), entry, len);
20ad5e28 2291
3714081c 2292 if (name + 2 + len * 2 < regions->section_end)
5879bb8f
NC
2293 {
2294 /* This strange loop is to cope with multibyte characters. */
2295 while (len --)
2296 {
20ad5e28
NC
2297 char c;
2298
5879bb8f 2299 name += 2;
20ad5e28
NC
2300 c = * name;
2301 /* Avoid printing control characters. */
2302 if (c > 0 && c < 32)
2303 fprintf (file, "^%c", c + 64);
2304 else
2305 fprintf (file, "%.1s", name);
5879bb8f
NC
2306 }
2307 }
11a6da56 2308 else
20ad5e28
NC
2309 {
2310 fprintf (file, _("<corrupt string length: %#x>\n"), len);
2311 /* PR binutils/17512: Do not try to continue decoding a
2312 corrupted resource section. It is likely to end up with
2313 reams of extraneous output. FIXME: We could probably
2314 continue if we disable the printing of strings... */
2315 return regions->section_end + 1;
2316 }
11a6da56
NC
2317 }
2318 else
20ad5e28
NC
2319 {
2320 fprintf (file, _("<corrupt string offset: %#lx>\n"), entry);
2321 return regions->section_end + 1;
2322 }
11a6da56
NC
2323 }
2324 else
2325 fprintf (file, _("ID: %#08lx"), entry);
9373215c 2326
11a6da56 2327 entry = (long) bfd_get_32 (abfd, data + 4);
5879bb8f 2328 fprintf (file, _(", Value: %#08lx\n"), entry);
11a6da56 2329
5879bb8f 2330 if (HighBitSet (entry))
20ad5e28
NC
2331 {
2332 data = regions->section_start + WithoutHighBit (entry);
2333 if (data <= regions->section_start || data > regions->section_end)
2334 return regions->section_end + 1;
2335
2336 /* FIXME: PR binutils/17512: A corrupt file could contain a loop
2337 in the resource table. We need some way to detect this. */
2338 return rsrc_print_resource_directory (file, abfd, indent + 1, data,
2339 regions, rva_bias);
2340 }
11a6da56 2341
5929c344
NC
2342 leaf = regions->section_start + entry;
2343
2344 if (leaf + 16 >= regions->section_end
2345 /* PR 17512: file: 055dff7e. */
2346 || leaf < regions->section_start)
3714081c 2347 return regions->section_end + 1;
11a6da56 2348
695344c0 2349 /* xgettext:c-format */
3714081c 2350 fprintf (file, _("%03x %*.s Leaf: Addr: %#08lx, Size: %#08lx, Codepage: %d\n"),
5929c344
NC
2351 (int) (entry), indent, " ",
2352 addr = (long) bfd_get_32 (abfd, leaf),
2353 size = (long) bfd_get_32 (abfd, leaf + 4),
2354 (int) bfd_get_32 (abfd, leaf + 8));
9373215c 2355
11a6da56 2356 /* Check that the reserved entry is 0. */
5929c344 2357 if (bfd_get_32 (abfd, leaf + 12) != 0
11a6da56 2358 /* And that the data address/size is valid too. */
3714081c
NC
2359 || (regions->section_start + (addr - rva_bias) + size > regions->section_end))
2360 return regions->section_end + 1;
11a6da56 2361
3714081c
NC
2362 if (regions->resource_start == NULL)
2363 regions->resource_start = regions->section_start + (addr - rva_bias);
2364
2365 return regions->section_start + (addr - rva_bias) + size;
11a6da56
NC
2366}
2367
5879bb8f
NC
2368#define max(a,b) ((a) > (b) ? (a) : (b))
2369#define min(a,b) ((a) < (b) ? (a) : (b))
2370
11a6da56 2371static bfd_byte *
07d6d2b8
AM
2372rsrc_print_resource_directory (FILE * file,
2373 bfd * abfd,
3714081c
NC
2374 unsigned int indent,
2375 bfd_byte * data,
2376 rsrc_regions * regions,
07d6d2b8 2377 bfd_vma rva_bias)
11a6da56
NC
2378{
2379 unsigned int num_names, num_ids;
5879bb8f 2380 bfd_byte * highest_data = data;
11a6da56 2381
3714081c
NC
2382 if (data + 16 >= regions->section_end)
2383 return regions->section_end + 1;
11a6da56 2384
3714081c 2385 fprintf (file, "%03x %*.s ", (int)(data - regions->section_start), indent, " ");
11a6da56
NC
2386 switch (indent)
2387 {
2388 case 0: fprintf (file, "Type"); break;
2389 case 2: fprintf (file, "Name"); break;
2390 case 4: fprintf (file, "Language"); break;
20ad5e28
NC
2391 default:
2392 fprintf (file, _("<unknown directory type: %d>\n"), indent);
2393 /* FIXME: For now we end the printing here. If in the
2394 future more directory types are added to the RSRC spec
2395 then we will need to change this. */
2396 return regions->section_end + 1;
11a6da56
NC
2397 }
2398
695344c0 2399 /* xgettext:c-format */
11a6da56
NC
2400 fprintf (file, _(" Table: Char: %d, Time: %08lx, Ver: %d/%d, Num Names: %d, IDs: %d\n"),
2401 (int) bfd_get_32 (abfd, data),
2402 (long) bfd_get_32 (abfd, data + 4),
2403 (int) bfd_get_16 (abfd, data + 8),
2404 (int) bfd_get_16 (abfd, data + 10),
2405 num_names = (int) bfd_get_16 (abfd, data + 12),
2406 num_ids = (int) bfd_get_16 (abfd, data + 14));
2407 data += 16;
2408
5879bb8f 2409 while (num_names --)
11a6da56 2410 {
5879bb8f
NC
2411 bfd_byte * entry_end;
2412
0a1b45a2 2413 entry_end = rsrc_print_resource_entries (file, abfd, indent + 1, true,
3714081c 2414 data, regions, rva_bias);
5879bb8f
NC
2415 data += 8;
2416 highest_data = max (highest_data, entry_end);
3714081c 2417 if (entry_end >= regions->section_end)
5879bb8f 2418 return entry_end;
11a6da56
NC
2419 }
2420
5879bb8f 2421 while (num_ids --)
11a6da56 2422 {
5879bb8f
NC
2423 bfd_byte * entry_end;
2424
0a1b45a2 2425 entry_end = rsrc_print_resource_entries (file, abfd, indent + 1, false,
3714081c 2426 data, regions, rva_bias);
5879bb8f
NC
2427 data += 8;
2428 highest_data = max (highest_data, entry_end);
3714081c 2429 if (entry_end >= regions->section_end)
5879bb8f 2430 return entry_end;
11a6da56
NC
2431 }
2432
5879bb8f 2433 return max (highest_data, data);
11a6da56
NC
2434}
2435
2436/* Display the contents of a .rsrc section. We do not try to
2437 reproduce the resources, windres does that. Instead we dump
2438 the tables in a human readable format. */
2439
0a1b45a2 2440static bool
5879bb8f 2441rsrc_print_section (bfd * abfd, void * vfile)
11a6da56
NC
2442{
2443 bfd_vma rva_bias;
2444 pe_data_type * pe;
2445 FILE * file = (FILE *) vfile;
2446 bfd_size_type datasize;
2447 asection * section;
2448 bfd_byte * data;
3714081c 2449 rsrc_regions regions;
11a6da56 2450
11a6da56 2451 pe = pe_data (abfd);
5879bb8f 2452 if (pe == NULL)
0a1b45a2 2453 return true;
11a6da56 2454
5879bb8f
NC
2455 section = bfd_get_section_by_name (abfd, ".rsrc");
2456 if (section == NULL)
0a1b45a2 2457 return true;
b69c8728 2458 if (!(section->flags & SEC_HAS_CONTENTS))
0a1b45a2 2459 return true;
5879bb8f 2460
11a6da56
NC
2461 datasize = section->size;
2462 if (datasize == 0)
0a1b45a2 2463 return true;
11a6da56 2464
b69c8728
JT
2465 rva_bias = section->vma - pe->pe_opthdr.ImageBase;
2466
5879bb8f 2467 if (! bfd_malloc_and_get_section (abfd, section, & data))
11a6da56 2468 {
c9594989 2469 free (data);
0a1b45a2 2470 return false;
11a6da56 2471 }
3714081c
NC
2472
2473 regions.section_start = data;
2474 regions.section_end = data + datasize;
2475 regions.strings_start = NULL;
2476 regions.resource_start = NULL;
11a6da56
NC
2477
2478 fflush (file);
2479 fprintf (file, "\nThe .rsrc Resource Directory section:\n");
2480
3714081c 2481 while (data < regions.section_end)
11a6da56 2482 {
5879bb8f
NC
2483 bfd_byte * p = data;
2484
3714081c 2485 data = rsrc_print_resource_directory (file, abfd, 0, data, & regions, rva_bias);
11a6da56 2486
3714081c 2487 if (data == regions.section_end + 1)
11a6da56
NC
2488 fprintf (file, _("Corrupt .rsrc section detected!\n"));
2489 else
2490 {
2491 /* Align data before continuing. */
2492 int align = (1 << section->alignment_power) - 1;
5879bb8f 2493
b9e95fa2 2494 data = (bfd_byte *) (((ptrdiff_t) (data + align)) & ~ align);
5879bb8f 2495 rva_bias += data - p;
11a6da56
NC
2496
2497 /* For reasons that are unclear .rsrc sections are sometimes created
2498 aligned to a 1^3 boundary even when their alignment is set at
2499 1^2. Catch that case here before we issue a spurious warning
2500 message. */
3714081c
NC
2501 if (data == (regions.section_end - 4))
2502 data = regions.section_end;
2503 else if (data < regions.section_end)
c32abae8
NC
2504 {
2505 /* If the extra data is all zeros then do not complain.
2506 This is just padding so that the section meets the
2507 page size requirements. */
6937bb54 2508 while (++ data < regions.section_end)
c32abae8
NC
2509 if (*data != 0)
2510 break;
2511 if (data < regions.section_end)
2512 fprintf (file, _("\nWARNING: Extra data in .rsrc section - it will be ignored by Windows:\n"));
2513 }
11a6da56
NC
2514 }
2515 }
2516
3714081c 2517 if (regions.strings_start != NULL)
695344c0 2518 fprintf (file, _(" String table starts at offset: %#03x\n"),
08937d80 2519 (int) (regions.strings_start - regions.section_start));
3714081c 2520 if (regions.resource_start != NULL)
695344c0 2521 fprintf (file, _(" Resources start at offset: %#03x\n"),
08937d80 2522 (int) (regions.resource_start - regions.section_start));
1b786873 2523
3714081c 2524 free (regions.section_start);
0a1b45a2 2525 return true;
11a6da56
NC
2526}
2527
1957ab10 2528#define IMAGE_NUMBEROF_DEBUG_TYPES 17
61e2488c
JT
2529
2530static char * debug_type_names[IMAGE_NUMBEROF_DEBUG_TYPES] =
2531{
2532 "Unknown",
2533 "COFF",
2534 "CodeView",
2535 "FPO",
2536 "Misc",
2537 "Exception",
2538 "Fixup",
2539 "OMAP-to-SRC",
2540 "OMAP-from-SRC",
2541 "Borland",
2542 "Reserved",
2543 "CLSID",
1957ab10
JT
2544 "Feature",
2545 "CoffGrp",
2546 "ILTCG",
2547 "MPX",
2548 "Repro",
61e2488c
JT
2549};
2550
0a1b45a2 2551static bool
61e2488c
JT
2552pe_print_debugdata (bfd * abfd, void * vfile)
2553{
2554 FILE *file = (FILE *) vfile;
2555 pe_data_type *pe = pe_data (abfd);
2556 struct internal_extra_pe_aouthdr *extra = &pe->pe_opthdr;
2557 asection *section;
2558 bfd_byte *data = 0;
2559 bfd_size_type dataoff;
87b2920f 2560 unsigned int i, j;
61e2488c
JT
2561
2562 bfd_vma addr = extra->DataDirectory[PE_DEBUG_DATA].VirtualAddress;
2563 bfd_size_type size = extra->DataDirectory[PE_DEBUG_DATA].Size;
2564
2565 if (size == 0)
0a1b45a2 2566 return true;
61e2488c
JT
2567
2568 addr += extra->ImageBase;
2569 for (section = abfd->sections; section != NULL; section = section->next)
2570 {
2571 if ((addr >= section->vma) && (addr < (section->vma + section->size)))
07d6d2b8 2572 break;
61e2488c
JT
2573 }
2574
2575 if (section == NULL)
2576 {
2577 fprintf (file,
07d6d2b8 2578 _("\nThere is a debug directory, but the section containing it could not be found\n"));
0a1b45a2 2579 return true;
61e2488c 2580 }
6e6e7cfc
JT
2581 else if (!(section->flags & SEC_HAS_CONTENTS))
2582 {
2583 fprintf (file,
07d6d2b8
AM
2584 _("\nThere is a debug directory in %s, but that section has no contents\n"),
2585 section->name);
0a1b45a2 2586 return true;
6e6e7cfc 2587 }
5a3f568b
NC
2588 else if (section->size < size)
2589 {
2590 fprintf (file,
07d6d2b8
AM
2591 _("\nError: section %s contains the debug data starting address but it is too small\n"),
2592 section->name);
0a1b45a2 2593 return false;
5a3f568b 2594 }
61e2488c
JT
2595
2596 fprintf (file, _("\nThere is a debug directory in %s at 0x%lx\n\n"),
2597 section->name, (unsigned long) addr);
2598
2599 dataoff = addr - section->vma;
2600
a6f921c8
NC
2601 if (size > (section->size - dataoff))
2602 {
2603 fprintf (file, _("The debug data size field in the data directory is too big for the section"));
0a1b45a2 2604 return false;
a6f921c8
NC
2605 }
2606
61e2488c
JT
2607 fprintf (file,
2608 _("Type Size Rva Offset\n"));
2609
5a3f568b 2610 /* Read the whole section. */
61e2488c
JT
2611 if (!bfd_malloc_and_get_section (abfd, section, &data))
2612 {
c9594989 2613 free (data);
0a1b45a2 2614 return false;
61e2488c
JT
2615 }
2616
2617 for (i = 0; i < size / sizeof (struct external_IMAGE_DEBUG_DIRECTORY); i++)
2618 {
2619 const char *type_name;
2620 struct external_IMAGE_DEBUG_DIRECTORY *ext
2621 = &((struct external_IMAGE_DEBUG_DIRECTORY *)(data + dataoff))[i];
2622 struct internal_IMAGE_DEBUG_DIRECTORY idd;
2623
2624 _bfd_XXi_swap_debugdir_in (abfd, ext, &idd);
2625
20ad5e28 2626 if ((idd.Type) >= IMAGE_NUMBEROF_DEBUG_TYPES)
07d6d2b8 2627 type_name = debug_type_names[0];
61e2488c 2628 else
07d6d2b8 2629 type_name = debug_type_names[idd.Type];
61e2488c
JT
2630
2631 fprintf (file, " %2ld %14s %08lx %08lx %08lx\n",
2632 idd.Type, type_name, idd.SizeOfData,
2633 idd.AddressOfRawData, idd.PointerToRawData);
2634
2635 if (idd.Type == PE_IMAGE_DEBUG_TYPE_CODEVIEW)
07d6d2b8
AM
2636 {
2637 char signature[CV_INFO_SIGNATURE_LENGTH * 2 + 1];
77ef8654
NC
2638 /* PR 17512: file: 065-29434-0.001:0.1
2639 We need to use a 32-bit aligned buffer
2640 to safely read in a codeview record. */
07d6d2b8 2641 char buffer[256 + 1] ATTRIBUTE_ALIGNED_ALIGNOF (CODEVIEW_INFO);
f6f30f34 2642 char *pdb;
77ef8654 2643
07d6d2b8 2644 CODEVIEW_INFO *cvinfo = (CODEVIEW_INFO *) buffer;
61e2488c 2645
07d6d2b8 2646 /* The debug entry doesn't have to have to be in a section,
61e2488c 2647 in which case AddressOfRawData is 0, so always use PointerToRawData. */
07d6d2b8 2648 if (!_bfd_XXi_slurp_codeview_record (abfd, (file_ptr) idd.PointerToRawData,
f6f30f34 2649 idd.SizeOfData, cvinfo, &pdb))
07d6d2b8 2650 continue;
61e2488c 2651
87b2920f
JT
2652 for (j = 0; j < cvinfo->SignatureLength; j++)
2653 sprintf (&signature[j*2], "%02x", cvinfo->Signature[j] & 0xff);
61e2488c 2654
695344c0 2655 /* xgettext:c-format */
f6f30f34 2656 fprintf (file, _("(format %c%c%c%c signature %s age %ld pdb %s)\n"),
61e2488c 2657 buffer[0], buffer[1], buffer[2], buffer[3],
f6f30f34
MH
2658 signature, cvinfo->Age, pdb[0] ? pdb : "(none)");
2659
2660 free (pdb);
07d6d2b8 2661 }
61e2488c
JT
2662 }
2663
87b2920f
JT
2664 free(data);
2665
61e2488c
JT
2666 if (size % sizeof (struct external_IMAGE_DEBUG_DIRECTORY) != 0)
2667 fprintf (file,
07d6d2b8 2668 _("The debug directory size is not a multiple of the debug directory entry size\n"));
61e2488c 2669
0a1b45a2 2670 return true;
61e2488c
JT
2671}
2672
0a1b45a2 2673static bool
b5d36aaa
JT
2674pe_is_repro (bfd * abfd)
2675{
2676 pe_data_type *pe = pe_data (abfd);
2677 struct internal_extra_pe_aouthdr *extra = &pe->pe_opthdr;
2678 asection *section;
2679 bfd_byte *data = 0;
2680 bfd_size_type dataoff;
2681 unsigned int i;
0a1b45a2 2682 bool res = false;
b5d36aaa
JT
2683
2684 bfd_vma addr = extra->DataDirectory[PE_DEBUG_DATA].VirtualAddress;
2685 bfd_size_type size = extra->DataDirectory[PE_DEBUG_DATA].Size;
2686
2687 if (size == 0)
0a1b45a2 2688 return false;
b5d36aaa
JT
2689
2690 addr += extra->ImageBase;
2691 for (section = abfd->sections; section != NULL; section = section->next)
2692 {
2693 if ((addr >= section->vma) && (addr < (section->vma + section->size)))
2694 break;
2695 }
2696
2697 if ((section == NULL)
2698 || (!(section->flags & SEC_HAS_CONTENTS))
2699 || (section->size < size))
2700 {
0a1b45a2 2701 return false;
b5d36aaa
JT
2702 }
2703
2704 dataoff = addr - section->vma;
2705
2706 if (size > (section->size - dataoff))
2707 {
0a1b45a2 2708 return false;
b5d36aaa
JT
2709 }
2710
2711 if (!bfd_malloc_and_get_section (abfd, section, &data))
2712 {
c9594989 2713 free (data);
0a1b45a2 2714 return false;
b5d36aaa
JT
2715 }
2716
2717 for (i = 0; i < size / sizeof (struct external_IMAGE_DEBUG_DIRECTORY); i++)
2718 {
2719 struct external_IMAGE_DEBUG_DIRECTORY *ext
2720 = &((struct external_IMAGE_DEBUG_DIRECTORY *)(data + dataoff))[i];
2721 struct internal_IMAGE_DEBUG_DIRECTORY idd;
2722
2723 _bfd_XXi_swap_debugdir_in (abfd, ext, &idd);
2724
2725 if (idd.Type == PE_IMAGE_DEBUG_TYPE_REPRO)
2726 {
0a1b45a2 2727 res = true;
b5d36aaa
JT
2728 break;
2729 }
2730 }
2731
2732 free(data);
2733
2734 return res;
2735}
2736
277d1b5e
ILT
2737/* Print out the program headers. */
2738
0a1b45a2 2739bool
7920ce38 2740_bfd_XX_print_private_bfd_data_common (bfd * abfd, void * vfile)
277d1b5e
ILT
2741{
2742 FILE *file = (FILE *) vfile;
2743 int j;
2744 pe_data_type *pe = pe_data (abfd);
2745 struct internal_extra_pe_aouthdr *i = &pe->pe_opthdr;
fac41780 2746 const char *subsystem_name = NULL;
d13c9dc6 2747 const char *name;
277d1b5e
ILT
2748
2749 /* The MS dumpbin program reportedly ands with 0xff0f before
2750 printing the characteristics field. Not sure why. No reason to
2751 emulate it here. */
2752 fprintf (file, _("\nCharacteristics 0x%x\n"), pe->real_flags);
2753#undef PF
6fa957a9 2754#define PF(x, y) if (pe->real_flags & x) { fprintf (file, "\t%s\n", y); }
d70270c5
BF
2755 PF (IMAGE_FILE_RELOCS_STRIPPED, "relocations stripped");
2756 PF (IMAGE_FILE_EXECUTABLE_IMAGE, "executable");
2757 PF (IMAGE_FILE_LINE_NUMS_STRIPPED, "line numbers stripped");
2758 PF (IMAGE_FILE_LOCAL_SYMS_STRIPPED, "symbols stripped");
2759 PF (IMAGE_FILE_LARGE_ADDRESS_AWARE, "large address aware");
2760 PF (IMAGE_FILE_BYTES_REVERSED_LO, "little endian");
2761 PF (IMAGE_FILE_32BIT_MACHINE, "32 bit words");
2762 PF (IMAGE_FILE_DEBUG_STRIPPED, "debugging information removed");
18e9a809
EZ
2763 PF (IMAGE_FILE_REMOVABLE_RUN_FROM_SWAP, "copy to swap file if on removable media");
2764 PF (IMAGE_FILE_NET_RUN_FROM_SWAP, "copy to swap file if on network media");
d70270c5
BF
2765 PF (IMAGE_FILE_SYSTEM, "system file");
2766 PF (IMAGE_FILE_DLL, "DLL");
18e9a809 2767 PF (IMAGE_FILE_UP_SYSTEM_ONLY, "run only on uniprocessor machine");
d70270c5 2768 PF (IMAGE_FILE_BYTES_REVERSED_HI, "big endian");
277d1b5e
ILT
2769#undef PF
2770
b5d36aaa
JT
2771 /*
2772 If a PE_IMAGE_DEBUG_TYPE_REPRO entry is present in the debug directory, the
2773 timestamp is to be interpreted as the hash of a reproducible build.
2774 */
2775 if (pe_is_repro (abfd))
2776 {
2777 fprintf (file, "\nTime/Date\t\t%08lx", pe->coff.timestamp);
2778 fprintf (file, "\t(This is a reproducible build file hash, not a timestamp)\n");
2779 }
2780 else
2781 {
2782 /* ctime implies '\n'. */
2783 time_t t = pe->coff.timestamp;
2784 fprintf (file, "\nTime/Date\t\t%s", ctime (&t));
2785 }
d13c9dc6
L
2786
2787#ifndef IMAGE_NT_OPTIONAL_HDR_MAGIC
2788# define IMAGE_NT_OPTIONAL_HDR_MAGIC 0x10b
2789#endif
2790#ifndef IMAGE_NT_OPTIONAL_HDR64_MAGIC
2791# define IMAGE_NT_OPTIONAL_HDR64_MAGIC 0x20b
2792#endif
2793#ifndef IMAGE_NT_OPTIONAL_HDRROM_MAGIC
2794# define IMAGE_NT_OPTIONAL_HDRROM_MAGIC 0x107
2795#endif
2796
2797 switch (i->Magic)
2798 {
2799 case IMAGE_NT_OPTIONAL_HDR_MAGIC:
2800 name = "PE32";
2801 break;
2802 case IMAGE_NT_OPTIONAL_HDR64_MAGIC:
2803 name = "PE32+";
2804 break;
2805 case IMAGE_NT_OPTIONAL_HDRROM_MAGIC:
2806 name = "ROM";
2807 break;
2808 default:
2809 name = NULL;
2810 break;
2811 }
2812 fprintf (file, "Magic\t\t\t%04x", i->Magic);
2813 if (name)
2814 fprintf (file, "\t(%s)",name);
2815 fprintf (file, "\nMajorLinkerVersion\t%d\n", i->MajorLinkerVersion);
2816 fprintf (file, "MinorLinkerVersion\t%d\n", i->MinorLinkerVersion);
b24cc414
AM
2817 fprintf (file, "SizeOfCode\t\t");
2818 bfd_fprintf_vma (abfd, file, i->SizeOfCode);
2819 fprintf (file, "\nSizeOfInitializedData\t");
2820 bfd_fprintf_vma (abfd, file, i->SizeOfInitializedData);
2821 fprintf (file, "\nSizeOfUninitializedData\t");
2822 bfd_fprintf_vma (abfd, file, i->SizeOfUninitializedData);
2823 fprintf (file, "\nAddressOfEntryPoint\t");
ebf12fbe 2824 bfd_fprintf_vma (abfd, file, i->AddressOfEntryPoint);
d13c9dc6 2825 fprintf (file, "\nBaseOfCode\t\t");
ebf12fbe 2826 bfd_fprintf_vma (abfd, file, i->BaseOfCode);
31f60095 2827#if !defined(COFF_WITH_pep) && !defined(COFF_WITH_pex64) && !defined(COFF_WITH_peAArch64) && !defined(COFF_WITH_peLoongArch64)
d13c9dc6
L
2828 /* PE32+ does not have BaseOfData member! */
2829 fprintf (file, "\nBaseOfData\t\t");
ebf12fbe 2830 bfd_fprintf_vma (abfd, file, i->BaseOfData);
d13c9dc6
L
2831#endif
2832
9602af51 2833 fprintf (file, "\nImageBase\t\t");
ebf12fbe 2834 bfd_fprintf_vma (abfd, file, i->ImageBase);
b24cc414
AM
2835 fprintf (file, "\nSectionAlignment\t%08x\n", i->SectionAlignment);
2836 fprintf (file, "FileAlignment\t\t%08x\n", i->FileAlignment);
2837 fprintf (file, "MajorOSystemVersion\t%d\n", i->MajorOperatingSystemVersion);
9602af51
KH
2838 fprintf (file, "MinorOSystemVersion\t%d\n", i->MinorOperatingSystemVersion);
2839 fprintf (file, "MajorImageVersion\t%d\n", i->MajorImageVersion);
2840 fprintf (file, "MinorImageVersion\t%d\n", i->MinorImageVersion);
2841 fprintf (file, "MajorSubsystemVersion\t%d\n", i->MajorSubsystemVersion);
2842 fprintf (file, "MinorSubsystemVersion\t%d\n", i->MinorSubsystemVersion);
b24cc414
AM
2843 fprintf (file, "Win32Version\t\t%08x\n", i->Reserved1);
2844 fprintf (file, "SizeOfImage\t\t%08x\n", i->SizeOfImage);
2845 fprintf (file, "SizeOfHeaders\t\t%08x\n", i->SizeOfHeaders);
2846 fprintf (file, "CheckSum\t\t%08x\n", i->CheckSum);
1725a96e 2847
fac41780
JW
2848 switch (i->Subsystem)
2849 {
2850 case IMAGE_SUBSYSTEM_UNKNOWN:
2851 subsystem_name = "unspecified";
2852 break;
2853 case IMAGE_SUBSYSTEM_NATIVE:
2854 subsystem_name = "NT native";
2855 break;
2856 case IMAGE_SUBSYSTEM_WINDOWS_GUI:
2857 subsystem_name = "Windows GUI";
2858 break;
2859 case IMAGE_SUBSYSTEM_WINDOWS_CUI:
2860 subsystem_name = "Windows CUI";
2861 break;
2862 case IMAGE_SUBSYSTEM_POSIX_CUI:
2863 subsystem_name = "POSIX CUI";
2864 break;
2865 case IMAGE_SUBSYSTEM_WINDOWS_CE_GUI:
2866 subsystem_name = "Wince CUI";
2867 break;
279edac5 2868 /* These are from UEFI Platform Initialization Specification 1.1. */
fac41780
JW
2869 case IMAGE_SUBSYSTEM_EFI_APPLICATION:
2870 subsystem_name = "EFI application";
2871 break;
2872 case IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER:
2873 subsystem_name = "EFI boot service driver";
2874 break;
2875 case IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER:
9602af51 2876 subsystem_name = "EFI runtime driver";
fac41780 2877 break;
d9118602
L
2878 case IMAGE_SUBSYSTEM_SAL_RUNTIME_DRIVER:
2879 subsystem_name = "SAL runtime driver";
6c73cbb1 2880 break;
279edac5 2881 /* This is from revision 8.0 of the MS PE/COFF spec */
6c73cbb1
NC
2882 case IMAGE_SUBSYSTEM_XBOX:
2883 subsystem_name = "XBOX";
2884 break;
279edac5 2885 /* Added default case for clarity - subsystem_name is NULL anyway. */
6c73cbb1
NC
2886 default:
2887 subsystem_name = NULL;
fac41780 2888 }
1725a96e 2889
9602af51 2890 fprintf (file, "Subsystem\t\t%08x", i->Subsystem);
fac41780
JW
2891 if (subsystem_name)
2892 fprintf (file, "\t(%s)", subsystem_name);
9602af51 2893 fprintf (file, "\nDllCharacteristics\t%08x\n", i->DllCharacteristics);
18e9a809
EZ
2894 if (i->DllCharacteristics)
2895 {
2896 unsigned short dllch = i->DllCharacteristics;
2897 const char *indent = "\t\t\t\t\t";
2898
2899 if (dllch & IMAGE_DLL_CHARACTERISTICS_HIGH_ENTROPY_VA)
2900 fprintf (file, "%sHIGH_ENTROPY_VA\n", indent);
2901 if (dllch & IMAGE_DLL_CHARACTERISTICS_DYNAMIC_BASE)
2902 fprintf (file, "%sDYNAMIC_BASE\n", indent);
2903 if (dllch & IMAGE_DLL_CHARACTERISTICS_FORCE_INTEGRITY)
2904 fprintf (file, "%sFORCE_INTEGRITY\n", indent);
2905 if (dllch & IMAGE_DLL_CHARACTERISTICS_NX_COMPAT)
2906 fprintf (file, "%sNX_COMPAT\n", indent);
2907 if (dllch & IMAGE_DLLCHARACTERISTICS_NO_ISOLATION)
2908 fprintf (file, "%sNO_ISOLATION\n", indent);
2909 if (dllch & IMAGE_DLLCHARACTERISTICS_NO_SEH)
2910 fprintf (file, "%sNO_SEH\n", indent);
2911 if (dllch & IMAGE_DLLCHARACTERISTICS_NO_BIND)
2912 fprintf (file, "%sNO_BIND\n", indent);
2913 if (dllch & IMAGE_DLLCHARACTERISTICS_APPCONTAINER)
2914 fprintf (file, "%sAPPCONTAINER\n", indent);
2915 if (dllch & IMAGE_DLLCHARACTERISTICS_WDM_DRIVER)
2916 fprintf (file, "%sWDM_DRIVER\n", indent);
2917 if (dllch & IMAGE_DLLCHARACTERISTICS_GUARD_CF)
2918 fprintf (file, "%sGUARD_CF\n", indent);
2919 if (dllch & IMAGE_DLLCHARACTERISTICS_TERMINAL_SERVER_AWARE)
2920 fprintf (file, "%sTERMINAL_SERVICE_AWARE\n", indent);
2921 }
9602af51 2922 fprintf (file, "SizeOfStackReserve\t");
ebf12fbe 2923 bfd_fprintf_vma (abfd, file, i->SizeOfStackReserve);
9602af51 2924 fprintf (file, "\nSizeOfStackCommit\t");
ebf12fbe 2925 bfd_fprintf_vma (abfd, file, i->SizeOfStackCommit);
9602af51 2926 fprintf (file, "\nSizeOfHeapReserve\t");
ebf12fbe 2927 bfd_fprintf_vma (abfd, file, i->SizeOfHeapReserve);
9602af51 2928 fprintf (file, "\nSizeOfHeapCommit\t");
ebf12fbe 2929 bfd_fprintf_vma (abfd, file, i->SizeOfHeapCommit);
0af1713e
AM
2930 fprintf (file, "\nLoaderFlags\t\t%08lx\n", (unsigned long) i->LoaderFlags);
2931 fprintf (file, "NumberOfRvaAndSizes\t%08lx\n",
2932 (unsigned long) i->NumberOfRvaAndSizes);
277d1b5e 2933
9602af51 2934 fprintf (file, "\nThe Data Directory\n");
277d1b5e
ILT
2935 for (j = 0; j < IMAGE_NUMBEROF_DIRECTORY_ENTRIES; j++)
2936 {
2937 fprintf (file, "Entry %1x ", j);
ebf12fbe 2938 bfd_fprintf_vma (abfd, file, i->DataDirectory[j].VirtualAddress);
0af1713e 2939 fprintf (file, " %08lx ", (unsigned long) i->DataDirectory[j].Size);
277d1b5e
ILT
2940 fprintf (file, "%s\n", dir_names[j]);
2941 }
2942
2943 pe_print_idata (abfd, vfile);
2944 pe_print_edata (abfd, vfile);
2b5c217d
NC
2945 if (bfd_coff_have_print_pdata (abfd))
2946 bfd_coff_print_pdata (abfd, vfile);
2947 else
2948 pe_print_pdata (abfd, vfile);
277d1b5e 2949 pe_print_reloc (abfd, vfile);
61e2488c 2950 pe_print_debugdata (abfd, file);
277d1b5e 2951
5879bb8f 2952 rsrc_print_section (abfd, vfile);
9373215c 2953
0a1b45a2 2954 return true;
277d1b5e
ILT
2955}
2956
0a1b45a2 2957static bool
6e6e7cfc
JT
2958is_vma_in_section (bfd *abfd ATTRIBUTE_UNUSED, asection *sect, void *obj)
2959{
2960 bfd_vma addr = * (bfd_vma *) obj;
2961 return (addr >= sect->vma) && (addr < (sect->vma + sect->size));
2962}
2963
2964static asection *
2965find_section_by_vma (bfd *abfd, bfd_vma addr)
2966{
2967 return bfd_sections_find_if (abfd, is_vma_in_section, (void *) & addr);
2968}
2969
277d1b5e
ILT
2970/* Copy any private info we understand from the input bfd
2971 to the output bfd. */
2972
0a1b45a2 2973bool
7920ce38 2974_bfd_XX_bfd_copy_private_bfd_data_common (bfd * ibfd, bfd * obfd)
277d1b5e 2975{
4be8cddc 2976 pe_data_type *ipe, *ope;
fe69d4fc 2977 bfd_size_type size;
4be8cddc 2978
277d1b5e
ILT
2979 /* One day we may try to grok other private data. */
2980 if (ibfd->xvec->flavour != bfd_target_coff_flavour
2981 || obfd->xvec->flavour != bfd_target_coff_flavour)
0a1b45a2 2982 return true;
277d1b5e 2983
4be8cddc
L
2984 ipe = pe_data (ibfd);
2985 ope = pe_data (obfd);
4e1fc599 2986
325c681d 2987 /* pe_opthdr is copied in copy_object. */
4be8cddc
L
2988 ope->dll = ipe->dll;
2989
2990 /* Don't copy input subsystem if output is different from input. */
2991 if (obfd->xvec != ibfd->xvec)
2992 ope->pe_opthdr.Subsystem = IMAGE_SUBSYSTEM_UNKNOWN;
277d1b5e 2993
1725a96e 2994 /* For strip: if we removed .reloc, we'll make a real mess of things
5933bdc9
ILT
2995 if we don't remove this entry as well. */
2996 if (! pe_data (obfd)->has_reloc_section)
2997 {
6c73cbb1
NC
2998 pe_data (obfd)->pe_opthdr.DataDirectory[PE_BASE_RELOCATION_TABLE].VirtualAddress = 0;
2999 pe_data (obfd)->pe_opthdr.DataDirectory[PE_BASE_RELOCATION_TABLE].Size = 0;
5933bdc9 3000 }
441f34fa
L
3001
3002 /* For PIE, if there is .reloc, we won't add IMAGE_FILE_RELOCS_STRIPPED.
3003 But there is no .reloc, we make sure that IMAGE_FILE_RELOCS_STRIPPED
3004 won't be added. */
3005 if (! pe_data (ibfd)->has_reloc_section
3006 && ! (pe_data (ibfd)->real_flags & IMAGE_FILE_RELOCS_STRIPPED))
3007 pe_data (obfd)->dont_strip_reloc = 1;
3008
70cf6834
AE
3009 memcpy (ope->dos_message, ipe->dos_message, sizeof (ope->dos_message));
3010
6e6e7cfc 3011 /* The file offsets contained in the debug directory need rewriting. */
fe69d4fc
AM
3012 size = ope->pe_opthdr.DataDirectory[PE_DEBUG_DATA].Size;
3013 if (size != 0)
6e6e7cfc
JT
3014 {
3015 bfd_vma addr = ope->pe_opthdr.DataDirectory[PE_DEBUG_DATA].VirtualAddress
3016 + ope->pe_opthdr.ImageBase;
610ed3e0
JB
3017 /* In particular a .buildid section may overlap (in VA space) with
3018 whatever section comes ahead of it (largely because of section->size
3019 representing s_size, not virt_size). Therefore don't look for the
3020 section containing the first byte, but for that covering the last
3021 one. */
fe69d4fc 3022 bfd_vma last = addr + size - 1;
610ed3e0 3023 asection *section = find_section_by_vma (obfd, last);
6e6e7cfc 3024
c55f2b9c 3025 if (section != NULL)
610ed3e0 3026 {
c55f2b9c
AM
3027 bfd_byte *data;
3028 bfd_vma dataoff = addr - section->vma;
610ed3e0 3029
c55f2b9c
AM
3030 /* PR 17512: file: 0f15796a. */
3031 if (addr < section->vma
3032 || section->size < dataoff
3033 || section->size - dataoff < size)
3034 {
3035 /* xgettext:c-format */
3036 _bfd_error_handler
3037 (_("%pB: Data Directory (%lx bytes at %" PRIx64 ") "
3038 "extends across section boundary at %" PRIx64),
3039 obfd, ope->pe_opthdr.DataDirectory[PE_DEBUG_DATA].Size,
3040 (uint64_t) addr, (uint64_t) section->vma);
3041 return false;
3042 }
6e6e7cfc 3043
3f316bf8
AM
3044 if ((section->flags & SEC_HAS_CONTENTS) != 0
3045 && bfd_malloc_and_get_section (obfd, section, &data))
07d6d2b8 3046 {
c55f2b9c
AM
3047 unsigned int i;
3048 struct external_IMAGE_DEBUG_DIRECTORY *dd =
3049 (struct external_IMAGE_DEBUG_DIRECTORY *)(data + dataoff);
3050
3051 for (i = 0; i < ope->pe_opthdr.DataDirectory[PE_DEBUG_DATA].Size
3052 / sizeof (struct external_IMAGE_DEBUG_DIRECTORY); i++)
3053 {
3054 asection *ddsection;
3055 struct external_IMAGE_DEBUG_DIRECTORY *edd = &(dd[i]);
3056 struct internal_IMAGE_DEBUG_DIRECTORY idd;
3057 bfd_vma idd_vma;
6e6e7cfc 3058
c55f2b9c 3059 _bfd_XXi_swap_debugdir_in (obfd, edd, &idd);
6e6e7cfc 3060
c55f2b9c
AM
3061 /* RVA 0 means only offset is valid, not handled yet. */
3062 if (idd.AddressOfRawData == 0)
3063 continue;
6e6e7cfc 3064
c55f2b9c
AM
3065 idd_vma = idd.AddressOfRawData + ope->pe_opthdr.ImageBase;
3066 ddsection = find_section_by_vma (obfd, idd_vma);
3067 if (!ddsection)
3068 continue; /* Not in a section! */
6e6e7cfc 3069
c55f2b9c
AM
3070 idd.PointerToRawData
3071 = ddsection->filepos + idd_vma - ddsection->vma;
3072 _bfd_XXi_swap_debugdir_out (obfd, &idd, edd);
3073 }
6e6e7cfc 3074
c55f2b9c
AM
3075 if (!bfd_set_section_contents (obfd, section, data, 0,
3076 section->size))
3077 {
3078 _bfd_error_handler (_("failed to update file offsets"
3079 " in debug directory"));
3080 free (data);
3081 return false;
3082 }
3083 free (data);
07d6d2b8 3084 }
c55f2b9c 3085 else
86eafac0 3086 {
c55f2b9c
AM
3087 _bfd_error_handler (_("%pB: failed to read "
3088 "debug data section"), obfd);
0a1b45a2 3089 return false;
86eafac0 3090 }
86eafac0 3091 }
6e6e7cfc
JT
3092 }
3093
0a1b45a2 3094 return true;
277d1b5e
ILT
3095}
3096
9602af51 3097/* Copy private section data. */
1725a96e 3098
0a1b45a2 3099bool
7920ce38
NC
3100_bfd_XX_bfd_copy_private_section_data (bfd *ibfd,
3101 asection *isec,
3102 bfd *obfd,
3103 asection *osec)
277d1b5e
ILT
3104{
3105 if (bfd_get_flavour (ibfd) != bfd_target_coff_flavour
3106 || bfd_get_flavour (obfd) != bfd_target_coff_flavour)
0a1b45a2 3107 return true;
277d1b5e
ILT
3108
3109 if (coff_section_data (ibfd, isec) != NULL
3110 && pei_section_data (ibfd, isec) != NULL)
3111 {
3112 if (coff_section_data (obfd, osec) == NULL)
3113 {
986f0783 3114 size_t amt = sizeof (struct coff_section_tdata);
7920ce38 3115 osec->used_by_bfd = bfd_zalloc (obfd, amt);
277d1b5e 3116 if (osec->used_by_bfd == NULL)
0a1b45a2 3117 return false;
277d1b5e 3118 }
1725a96e 3119
277d1b5e
ILT
3120 if (pei_section_data (obfd, osec) == NULL)
3121 {
986f0783 3122 size_t amt = sizeof (struct pei_section_tdata);
7920ce38 3123 coff_section_data (obfd, osec)->tdata = bfd_zalloc (obfd, amt);
277d1b5e 3124 if (coff_section_data (obfd, osec)->tdata == NULL)
0a1b45a2 3125 return false;
277d1b5e 3126 }
1725a96e 3127
277d1b5e
ILT
3128 pei_section_data (obfd, osec)->virt_size =
3129 pei_section_data (ibfd, isec)->virt_size;
5933bdc9 3130 pei_section_data (obfd, osec)->pe_flags =
6fa957a9 3131 pei_section_data (ibfd, isec)->pe_flags;
277d1b5e
ILT
3132 }
3133
0a1b45a2 3134 return true;
277d1b5e 3135}
7d2b58d6
ILT
3136
3137void
7920ce38 3138_bfd_XX_get_symbol_info (bfd * abfd, asymbol *symbol, symbol_info *ret)
7d2b58d6
ILT
3139{
3140 coff_get_symbol_info (abfd, symbol, ret);
7d2b58d6 3141}
2fbadf2c 3142
31f60095 3143#if !defined(COFF_WITH_pep) && (defined(COFF_WITH_pex64) || defined(COFF_WITH_peAArch64) || defined(COFF_WITH_peLoongArch64))
5174d0fb
KT
3144static int
3145sort_x64_pdata (const void *l, const void *r)
3146{
3147 const char *lp = (const char *) l;
3148 const char *rp = (const char *) r;
3149 bfd_vma vl, vr;
3150 vl = bfd_getl32 (lp); vr = bfd_getl32 (rp);
3151 if (vl != vr)
3152 return (vl < vr ? -1 : 1);
3153 /* We compare just begin address. */
3154 return 0;
3155}
3156#endif
5879bb8f
NC
3157\f
3158/* Functions to process a .rsrc section. */
3159
3160static unsigned int sizeof_leaves;
3161static unsigned int sizeof_strings;
3162static unsigned int sizeof_tables_and_entries;
3163
3164static bfd_byte *
3165rsrc_count_directory (bfd *, bfd_byte *, bfd_byte *, bfd_byte *, bfd_vma);
3166
3167static bfd_byte *
0a1b45a2
AM
3168rsrc_count_entries (bfd *abfd,
3169 bool is_name,
3170 bfd_byte *datastart,
3171 bfd_byte *data,
3172 bfd_byte *dataend,
3173 bfd_vma rva_bias)
5879bb8f
NC
3174{
3175 unsigned long entry, addr, size;
3176
3177 if (data + 8 >= dataend)
3178 return dataend + 1;
3179
3180 if (is_name)
3181 {
3182 bfd_byte * name;
3183
3184 entry = (long) bfd_get_32 (abfd, data);
3185
3186 if (HighBitSet (entry))
3187 name = datastart + WithoutHighBit (entry);
3188 else
3189 name = datastart + entry - rva_bias;
3190
20ad5e28 3191 if (name + 2 >= dataend || name < datastart)
5879bb8f
NC
3192 return dataend + 1;
3193
3194 unsigned int len = bfd_get_16 (abfd, name);
3195 if (len == 0 || len > 256)
3196 return dataend + 1;
5879bb8f
NC
3197 }
3198
3199 entry = (long) bfd_get_32 (abfd, data + 4);
3200
3201 if (HighBitSet (entry))
20ad5e28
NC
3202 {
3203 data = datastart + WithoutHighBit (entry);
3204
3205 if (data <= datastart || data >= dataend)
3206 return dataend + 1;
3207
3208 return rsrc_count_directory (abfd, datastart, data, dataend, rva_bias);
3209 }
5879bb8f
NC
3210
3211 if (datastart + entry + 16 >= dataend)
3212 return dataend + 1;
3213
3214 addr = (long) bfd_get_32 (abfd, datastart + entry);
3215 size = (long) bfd_get_32 (abfd, datastart + entry + 4);
3216
5879bb8f
NC
3217 return datastart + addr - rva_bias + size;
3218}
9373215c 3219
5879bb8f 3220static bfd_byte *
07d6d2b8 3221rsrc_count_directory (bfd * abfd,
5879bb8f
NC
3222 bfd_byte * datastart,
3223 bfd_byte * data,
3224 bfd_byte * dataend,
07d6d2b8 3225 bfd_vma rva_bias)
5879bb8f
NC
3226{
3227 unsigned int num_entries, num_ids;
3228 bfd_byte * highest_data = data;
3229
3230 if (data + 16 >= dataend)
3231 return dataend + 1;
3232
3233 num_entries = (int) bfd_get_16 (abfd, data + 12);
3234 num_ids = (int) bfd_get_16 (abfd, data + 14);
3235
3236 num_entries += num_ids;
3237
3238 data += 16;
5879bb8f
NC
3239
3240 while (num_entries --)
3241 {
3242 bfd_byte * entry_end;
3243
3244 entry_end = rsrc_count_entries (abfd, num_entries >= num_ids,
3245 datastart, data, dataend, rva_bias);
3246 data += 8;
5879bb8f
NC
3247 highest_data = max (highest_data, entry_end);
3248 if (entry_end >= dataend)
3249 break;
3250 }
3251
3252 return max (highest_data, data);
3253}
3254
3255typedef struct rsrc_dir_chain
3256{
07d6d2b8 3257 unsigned int num_entries;
5879bb8f
NC
3258 struct rsrc_entry * first_entry;
3259 struct rsrc_entry * last_entry;
3260} rsrc_dir_chain;
3261
3262typedef struct rsrc_directory
3263{
3264 unsigned int characteristics;
3265 unsigned int time;
3266 unsigned int major;
3267 unsigned int minor;
3268
3269 rsrc_dir_chain names;
3270 rsrc_dir_chain ids;
3271
3272 struct rsrc_entry * entry;
3273} rsrc_directory;
3274
3275typedef struct rsrc_string
3276{
07d6d2b8
AM
3277 unsigned int len;
3278 bfd_byte * string;
5879bb8f 3279} rsrc_string;
9373215c 3280
5879bb8f
NC
3281typedef struct rsrc_leaf
3282{
07d6d2b8
AM
3283 unsigned int size;
3284 unsigned int codepage;
3285 bfd_byte * data;
5879bb8f
NC
3286} rsrc_leaf;
3287
3288typedef struct rsrc_entry
3289{
0a1b45a2 3290 bool is_name;
5879bb8f
NC
3291 union
3292 {
07d6d2b8
AM
3293 unsigned int id;
3294 struct rsrc_string name;
5879bb8f
NC
3295 } name_id;
3296
0a1b45a2 3297 bool is_dir;
5879bb8f
NC
3298 union
3299 {
3300 struct rsrc_directory * directory;
07d6d2b8 3301 struct rsrc_leaf * leaf;
5879bb8f
NC
3302 } value;
3303
07d6d2b8 3304 struct rsrc_entry * next_entry;
5879bb8f
NC
3305 struct rsrc_directory * parent;
3306} rsrc_entry;
3307
3308static bfd_byte *
3309rsrc_parse_directory (bfd *, rsrc_directory *, bfd_byte *,
3310 bfd_byte *, bfd_byte *, bfd_vma, rsrc_entry *);
3311
3312static bfd_byte *
0a1b45a2
AM
3313rsrc_parse_entry (bfd *abfd,
3314 bool is_name,
3315 rsrc_entry *entry,
3316 bfd_byte *datastart,
3317 bfd_byte * data,
3318 bfd_byte *dataend,
3319 bfd_vma rva_bias,
3320 rsrc_directory *parent)
5879bb8f
NC
3321{
3322 unsigned long val, addr, size;
3323
3324 val = bfd_get_32 (abfd, data);
3325
3326 entry->parent = parent;
3327 entry->is_name = is_name;
3328
3329 if (is_name)
3330 {
20ad5e28
NC
3331 bfd_byte * address;
3332
5879bb8f
NC
3333 if (HighBitSet (val))
3334 {
3335 val = WithoutHighBit (val);
3336
20ad5e28 3337 address = datastart + val;
5879bb8f
NC
3338 }
3339 else
3340 {
20ad5e28 3341 address = datastart + val - rva_bias;
5879bb8f 3342 }
20ad5e28
NC
3343
3344 if (address + 3 > dataend)
3345 return dataend;
3346
3347 entry->name_id.name.len = bfd_get_16 (abfd, address);
3348 entry->name_id.name.string = address + 2;
5879bb8f
NC
3349 }
3350 else
3351 entry->name_id.id = val;
3352
3353 val = bfd_get_32 (abfd, data + 4);
3354
3355 if (HighBitSet (val))
3356 {
0a1b45a2 3357 entry->is_dir = true;
10c38619 3358 entry->value.directory = bfd_malloc (sizeof (*entry->value.directory));
5879bb8f
NC
3359 if (entry->value.directory == NULL)
3360 return dataend;
3361
3362 return rsrc_parse_directory (abfd, entry->value.directory,
3363 datastart,
3364 datastart + WithoutHighBit (val),
3365 dataend, rva_bias, entry);
3366 }
3367
0a1b45a2 3368 entry->is_dir = false;
10c38619 3369 entry->value.leaf = bfd_malloc (sizeof (*entry->value.leaf));
5879bb8f
NC
3370 if (entry->value.leaf == NULL)
3371 return dataend;
3372
5929c344 3373 data = datastart + val;
10c38619 3374 if (data < datastart || data + 12 > dataend)
5929c344
NC
3375 return dataend;
3376
3377 addr = bfd_get_32 (abfd, data);
3378 size = entry->value.leaf->size = bfd_get_32 (abfd, data + 4);
3379 entry->value.leaf->codepage = bfd_get_32 (abfd, data + 8);
3380 /* FIXME: We assume that the reserved field (data + 12) is OK. */
5879bb8f 3381
10c38619
AM
3382 if (size > dataend - datastart - (addr - rva_bias))
3383 return dataend;
5879bb8f
NC
3384 entry->value.leaf->data = bfd_malloc (size);
3385 if (entry->value.leaf->data == NULL)
3386 return dataend;
3387
3388 memcpy (entry->value.leaf->data, datastart + addr - rva_bias, size);
3389 return datastart + (addr - rva_bias) + size;
3390}
3391
3392static bfd_byte *
0a1b45a2
AM
3393rsrc_parse_entries (bfd *abfd,
3394 rsrc_dir_chain *chain,
3395 bool is_name,
3396 bfd_byte *highest_data,
3397 bfd_byte *datastart,
3398 bfd_byte *data,
3399 bfd_byte *dataend,
3400 bfd_vma rva_bias,
3401 rsrc_directory *parent)
5879bb8f 3402{
9373215c 3403 unsigned int i;
5879bb8f
NC
3404 rsrc_entry * entry;
3405
3406 if (chain->num_entries == 0)
3407 {
3408 chain->first_entry = chain->last_entry = NULL;
3409 return highest_data;
3410 }
3411
10c38619 3412 entry = bfd_malloc (sizeof (*entry));
5879bb8f
NC
3413 if (entry == NULL)
3414 return dataend;
3415
3416 chain->first_entry = entry;
3417
5879bb8f
NC
3418 for (i = chain->num_entries; i--;)
3419 {
3420 bfd_byte * entry_end;
3421
3422 entry_end = rsrc_parse_entry (abfd, is_name, entry, datastart,
3423 data, dataend, rva_bias, parent);
3424 data += 8;
3425 highest_data = max (entry_end, highest_data);
3426 if (entry_end > dataend)
3427 return dataend;
3428
3429 if (i)
3430 {
10c38619 3431 entry->next_entry = bfd_malloc (sizeof (*entry));
5879bb8f
NC
3432 entry = entry->next_entry;
3433 if (entry == NULL)
3434 return dataend;
3435 }
3436 else
3437 entry->next_entry = NULL;
3438 }
3439
3440 chain->last_entry = entry;
3441
3442 return highest_data;
3443}
3444
3445static bfd_byte *
07d6d2b8 3446rsrc_parse_directory (bfd * abfd,
5879bb8f
NC
3447 rsrc_directory * table,
3448 bfd_byte * datastart,
3449 bfd_byte * data,
3450 bfd_byte * dataend,
07d6d2b8 3451 bfd_vma rva_bias,
5879bb8f
NC
3452 rsrc_entry * entry)
3453{
3454 bfd_byte * highest_data = data;
3455
3456 if (table == NULL)
3457 return dataend;
3458
3459 table->characteristics = bfd_get_32 (abfd, data);
3460 table->time = bfd_get_32 (abfd, data + 4);
3461 table->major = bfd_get_16 (abfd, data + 8);
3462 table->minor = bfd_get_16 (abfd, data + 10);
3463 table->names.num_entries = bfd_get_16 (abfd, data + 12);
3464 table->ids.num_entries = bfd_get_16 (abfd, data + 14);
3465 table->entry = entry;
3466
3467 data += 16;
3468
0a1b45a2 3469 highest_data = rsrc_parse_entries (abfd, & table->names, true, data,
5879bb8f
NC
3470 datastart, data, dataend, rva_bias, table);
3471 data += table->names.num_entries * 8;
3472
0a1b45a2 3473 highest_data = rsrc_parse_entries (abfd, & table->ids, false, highest_data,
5879bb8f
NC
3474 datastart, data, dataend, rva_bias, table);
3475 data += table->ids.num_entries * 8;
3476
3477 return max (highest_data, data);
3478}
3479
3480typedef struct rsrc_write_data
3481{
3482 bfd * abfd;
3483 bfd_byte * datastart;
3484 bfd_byte * next_table;
3485 bfd_byte * next_leaf;
3486 bfd_byte * next_string;
3487 bfd_byte * next_data;
3488 bfd_vma rva_bias;
9373215c
PM
3489} rsrc_write_data;
3490
5879bb8f
NC
3491static void
3492rsrc_write_string (rsrc_write_data * data,
3493 rsrc_string * string)
3494{
3495 bfd_put_16 (data->abfd, string->len, data->next_string);
3496 memcpy (data->next_string + 2, string->string, string->len * 2);
3497 data->next_string += (string->len + 1) * 2;
3498}
3499
3500static inline unsigned int
3501rsrc_compute_rva (rsrc_write_data * data,
07d6d2b8 3502 bfd_byte * addr)
5879bb8f
NC
3503{
3504 return (addr - data->datastart) + data->rva_bias;
3505}
3506
3507static void
3508rsrc_write_leaf (rsrc_write_data * data,
07d6d2b8 3509 rsrc_leaf * leaf)
5879bb8f 3510{
9373215c
PM
3511 bfd_put_32 (data->abfd, rsrc_compute_rva (data, data->next_data),
3512 data->next_leaf);
5879bb8f
NC
3513 bfd_put_32 (data->abfd, leaf->size, data->next_leaf + 4);
3514 bfd_put_32 (data->abfd, leaf->codepage, data->next_leaf + 8);
3515 bfd_put_32 (data->abfd, 0 /*reserved*/, data->next_leaf + 12);
3516 data->next_leaf += 16;
3517
3518 memcpy (data->next_data, leaf->data, leaf->size);
3714081c
NC
3519 /* An undocumented feature of Windows resources is that each unit
3520 of raw data is 8-byte aligned... */
3521 data->next_data += ((leaf->size + 7) & ~7);
5879bb8f
NC
3522}
3523
3524static void rsrc_write_directory (rsrc_write_data *, rsrc_directory *);
3525
3526static void
3527rsrc_write_entry (rsrc_write_data * data,
07d6d2b8
AM
3528 bfd_byte * where,
3529 rsrc_entry * entry)
5879bb8f
NC
3530{
3531 if (entry->is_name)
3532 {
3533 bfd_put_32 (data->abfd,
3534 SetHighBit (data->next_string - data->datastart),
3535 where);
3536 rsrc_write_string (data, & entry->name_id.name);
3537 }
3538 else
3539 bfd_put_32 (data->abfd, entry->name_id.id, where);
3540
3541 if (entry->is_dir)
3542 {
3543 bfd_put_32 (data->abfd,
3544 SetHighBit (data->next_table - data->datastart),
3545 where + 4);
3546 rsrc_write_directory (data, entry->value.directory);
3547 }
3548 else
3549 {
3550 bfd_put_32 (data->abfd, data->next_leaf - data->datastart, where + 4);
3551 rsrc_write_leaf (data, entry->value.leaf);
3552 }
3553}
3554
3714081c
NC
3555static void
3556rsrc_compute_region_sizes (rsrc_directory * dir)
3557{
3558 struct rsrc_entry * entry;
3559
3560 if (dir == NULL)
3561 return;
3562
3563 sizeof_tables_and_entries += 16;
3564
3565 for (entry = dir->names.first_entry; entry != NULL; entry = entry->next_entry)
3566 {
3567 sizeof_tables_and_entries += 8;
3568
3569 sizeof_strings += (entry->name_id.name.len + 1) * 2;
1b786873 3570
3714081c
NC
3571 if (entry->is_dir)
3572 rsrc_compute_region_sizes (entry->value.directory);
3573 else
3574 sizeof_leaves += 16;
3575 }
3576
3577 for (entry = dir->ids.first_entry; entry != NULL; entry = entry->next_entry)
3578 {
3579 sizeof_tables_and_entries += 8;
3580
3581 if (entry->is_dir)
3582 rsrc_compute_region_sizes (entry->value.directory);
3583 else
3584 sizeof_leaves += 16;
3585 }
3586}
3587
5879bb8f
NC
3588static void
3589rsrc_write_directory (rsrc_write_data * data,
3590 rsrc_directory * dir)
3591{
3592 rsrc_entry * entry;
3593 unsigned int i;
9373215c
PM
3594 bfd_byte * next_entry;
3595 bfd_byte * nt;
5879bb8f
NC
3596
3597 bfd_put_32 (data->abfd, dir->characteristics, data->next_table);
3598 bfd_put_32 (data->abfd, 0 /*dir->time*/, data->next_table + 4);
3599 bfd_put_16 (data->abfd, dir->major, data->next_table + 8);
3600 bfd_put_16 (data->abfd, dir->minor, data->next_table + 10);
3601 bfd_put_16 (data->abfd, dir->names.num_entries, data->next_table + 12);
3602 bfd_put_16 (data->abfd, dir->ids.num_entries, data->next_table + 14);
3603
3604 /* Compute where the entries and the next table will be placed. */
9373215c
PM
3605 next_entry = data->next_table + 16;
3606 data->next_table = next_entry + (dir->names.num_entries * 8)
3607 + (dir->ids.num_entries * 8);
3608 nt = data->next_table;
3609
5879bb8f
NC
3610 /* Write the entries. */
3611 for (i = dir->names.num_entries, entry = dir->names.first_entry;
3612 i > 0 && entry != NULL;
3613 i--, entry = entry->next_entry)
3614 {
3714081c 3615 BFD_ASSERT (entry->is_name);
5879bb8f
NC
3616 rsrc_write_entry (data, next_entry, entry);
3617 next_entry += 8;
3618 }
3619 BFD_ASSERT (i == 0);
3620 BFD_ASSERT (entry == NULL);
3621
3622 for (i = dir->ids.num_entries, entry = dir->ids.first_entry;
3623 i > 0 && entry != NULL;
3624 i--, entry = entry->next_entry)
3625 {
3714081c 3626 BFD_ASSERT (! entry->is_name);
5879bb8f
NC
3627 rsrc_write_entry (data, next_entry, entry);
3628 next_entry += 8;
3629 }
3630 BFD_ASSERT (i == 0);
3631 BFD_ASSERT (entry == NULL);
3632 BFD_ASSERT (nt == next_entry);
3633}
3634
83c79df8 3635#if ! defined __CYGWIN__ && ! defined __MINGW32__
5879bb8f
NC
3636/* Return the length (number of units) of the first character in S,
3637 putting its 'ucs4_t' representation in *PUC. */
3638
3639static unsigned int
31593e1b 3640u16_mbtouc (wint_t * puc, const unsigned short * s, unsigned int n)
5879bb8f
NC
3641{
3642 unsigned short c = * s;
3643
3644 if (c < 0xd800 || c >= 0xe000)
3645 {
3646 *puc = c;
3647 return 1;
3648 }
3649
3650 if (c < 0xdc00)
3651 {
3652 if (n >= 2)
07d6d2b8
AM
3653 {
3654 if (s[1] >= 0xdc00 && s[1] < 0xe000)
3655 {
3656 *puc = 0x10000 + ((c - 0xd800) << 10) + (s[1] - 0xdc00);
3657 return 2;
3658 }
3659 }
5879bb8f 3660 else
07d6d2b8
AM
3661 {
3662 /* Incomplete multibyte character. */
3663 *puc = 0xfffd;
3664 return n;
3665 }
5879bb8f
NC
3666 }
3667
3668 /* Invalid multibyte character. */
3669 *puc = 0xfffd;
3670 return 1;
3671}
83c79df8 3672#endif /* not Cygwin/Mingw */
5879bb8f
NC
3673
3674/* Perform a comparison of two entries. */
3675static signed int
0a1b45a2 3676rsrc_cmp (bool is_name, rsrc_entry * a, rsrc_entry * b)
5879bb8f 3677{
9373215c 3678 signed int res;
9373215c
PM
3679 bfd_byte * astring;
3680 unsigned int alen;
3681 bfd_byte * bstring;
3682 unsigned int blen;
3683
5879bb8f 3684 if (! is_name)
9373215c 3685 return a->name_id.id - b->name_id.id;
5879bb8f
NC
3686
3687 /* We have to perform a case insenstive, unicode string comparison... */
9373215c
PM
3688 astring = a->name_id.name.string;
3689 alen = a->name_id.name.len;
3690 bstring = b->name_id.name.string;
3691 blen = b->name_id.name.len;
5879bb8f 3692
9373215c
PM
3693#if defined __CYGWIN__ || defined __MINGW32__
3694 /* Under Windows hosts (both Cygwin and Mingw types),
3695 unicode == UTF-16 == wchar_t. The case insensitive string comparison
3696 function however goes by different names in the two environments... */
3697
3698#undef rscpcmp
5879bb8f 3699#ifdef __CYGWIN__
9373215c
PM
3700#define rscpcmp wcsncasecmp
3701#endif
3702#ifdef __MINGW32__
3703#define rscpcmp wcsnicmp
3704#endif
3705
3706 res = rscpcmp ((const wchar_t *) astring, (const wchar_t *) bstring,
3707 min (alen, blen));
5879bb8f 3708
83c79df8 3709#else
3f10b67a
PM
3710 {
3711 unsigned int i;
31593e1b 3712
3f10b67a
PM
3713 res = 0;
3714 for (i = min (alen, blen); i--; astring += 2, bstring += 2)
3715 {
31593e1b
NC
3716 wint_t awc;
3717 wint_t bwc;
3f10b67a 3718
31593e1b
NC
3719 /* Convert UTF-16 unicode characters into wchar_t characters
3720 so that we can then perform a case insensitive comparison. */
3721 unsigned int Alen = u16_mbtouc (& awc, (const unsigned short *) astring, 2);
3722 unsigned int Blen = u16_mbtouc (& bwc, (const unsigned short *) bstring, 2);
3f10b67a
PM
3723
3724 if (Alen != Blen)
3725 return Alen - Blen;
31593e1b 3726
31593e1b
NC
3727 awc = towlower (awc);
3728 bwc = towlower (bwc);
3729
3730 res = awc - bwc;
3f10b67a
PM
3731 if (res)
3732 break;
3733 }
3734 }
5879bb8f
NC
3735#endif
3736
3737 if (res == 0)
3738 res = alen - blen;
3739
3740 return res;
3741}
3742
3743static void
3744rsrc_print_name (char * buffer, rsrc_string string)
3745{
3746 unsigned int i;
3747 bfd_byte * name = string.string;
3748
3749 for (i = string.len; i--; name += 2)
3750 sprintf (buffer + strlen (buffer), "%.1s", name);
3751}
3752
3753static const char *
7fbd5f4e 3754rsrc_resource_name (rsrc_entry *entry, rsrc_directory *dir, char *buffer)
5879bb8f 3755{
0a1b45a2 3756 bool is_string = false;
5879bb8f
NC
3757
3758 buffer[0] = 0;
3759
9373215c
PM
3760 if (dir != NULL && dir->entry != NULL && dir->entry->parent != NULL
3761 && dir->entry->parent->entry != NULL)
5879bb8f
NC
3762 {
3763 strcpy (buffer, "type: ");
3764 if (dir->entry->parent->entry->is_name)
9373215c
PM
3765 rsrc_print_name (buffer + strlen (buffer),
3766 dir->entry->parent->entry->name_id.name);
5879bb8f
NC
3767 else
3768 {
3769 unsigned int id = dir->entry->parent->entry->name_id.id;
3770
3771 sprintf (buffer + strlen (buffer), "%x", id);
3772 switch (id)
3773 {
3774 case 1: strcat (buffer, " (CURSOR)"); break;
3775 case 2: strcat (buffer, " (BITMAP)"); break;
3776 case 3: strcat (buffer, " (ICON)"); break;
07d6d2b8 3777 case 4: strcat (buffer, " (MENU)"); break;
5879bb8f 3778 case 5: strcat (buffer, " (DIALOG)"); break;
0a1b45a2 3779 case 6: strcat (buffer, " (STRING)"); is_string = true; break;
5879bb8f
NC
3780 case 7: strcat (buffer, " (FONTDIR)"); break;
3781 case 8: strcat (buffer, " (FONT)"); break;
3782 case 9: strcat (buffer, " (ACCELERATOR)"); break;
3783 case 10: strcat (buffer, " (RCDATA)"); break;
3784 case 11: strcat (buffer, " (MESSAGETABLE)"); break;
3785 case 12: strcat (buffer, " (GROUP_CURSOR)"); break;
3786 case 14: strcat (buffer, " (GROUP_ICON)"); break;
3787 case 16: strcat (buffer, " (VERSION)"); break;
3788 case 17: strcat (buffer, " (DLGINCLUDE)"); break;
3789 case 19: strcat (buffer, " (PLUGPLAY)"); break;
3790 case 20: strcat (buffer, " (VXD)"); break;
3791 case 21: strcat (buffer, " (ANICURSOR)"); break;
3792 case 22: strcat (buffer, " (ANIICON)"); break;
3793 case 23: strcat (buffer, " (HTML)"); break;
3794 case 24: strcat (buffer, " (MANIFEST)"); break;
3795 case 240: strcat (buffer, " (DLGINIT)"); break;
3796 case 241: strcat (buffer, " (TOOLBAR)"); break;
3797 }
3798 }
3799 }
3800
3801 if (dir != NULL && dir->entry != NULL)
3802 {
3803 strcat (buffer, " name: ");
3804 if (dir->entry->is_name)
3805 rsrc_print_name (buffer + strlen (buffer), dir->entry->name_id.name);
3806 else
3807 {
3808 unsigned int id = dir->entry->name_id.id;
3809
3810 sprintf (buffer + strlen (buffer), "%x", id);
3811
3812 if (is_string)
3813 sprintf (buffer + strlen (buffer), " (resource id range: %d - %d)",
3814 (id - 1) << 4, (id << 4) - 1);
3815 }
3816 }
3817
3818 if (entry != NULL)
3819 {
3820 strcat (buffer, " lang: ");
3821
3822 if (entry->is_name)
3823 rsrc_print_name (buffer + strlen (buffer), entry->name_id.name);
3824 else
3825 sprintf (buffer + strlen (buffer), "%x", entry->name_id.id);
3826 }
3827
3828 return buffer;
3829}
3830
3831/* *sigh* Windows resource strings are special. Only the top 28-bits of
3832 their ID is stored in the NAME entry. The bottom four bits are used as
3833 an index into unicode string table that makes up the data of the leaf.
3834 So identical type-name-lang string resources may not actually be
3835 identical at all.
3836
3837 This function is called when we have detected two string resources with
3838 match top-28-bit IDs. We have to scan the string tables inside the leaves
3839 and discover if there are any real collisions. If there are then we report
9373215c
PM
3840 them and return FALSE. Otherwise we copy any strings from B into A and
3841 then return TRUE. */
5879bb8f 3842
0a1b45a2 3843static bool
5879bb8f
NC
3844rsrc_merge_string_entries (rsrc_entry * a ATTRIBUTE_UNUSED,
3845 rsrc_entry * b ATTRIBUTE_UNUSED)
3846{
3847 unsigned int copy_needed = 0;
3848 unsigned int i;
9373215c
PM
3849 bfd_byte * astring;
3850 bfd_byte * bstring;
3851 bfd_byte * new_data;
3852 bfd_byte * nstring;
5879bb8f
NC
3853
3854 /* Step one: Find out what we have to do. */
3855 BFD_ASSERT (! a->is_dir);
9373215c 3856 astring = a->value.leaf->data;
5879bb8f
NC
3857
3858 BFD_ASSERT (! b->is_dir);
9373215c 3859 bstring = b->value.leaf->data;
5879bb8f
NC
3860
3861 for (i = 0; i < 16; i++)
3862 {
3863 unsigned int alen = astring[0] + (astring[1] << 8);
3864 unsigned int blen = bstring[0] + (bstring[1] << 8);
3865
3866 if (alen == 0)
3867 {
3868 copy_needed += blen * 2;
3869 }
3870 else if (blen == 0)
3871 ;
3872 else if (alen != blen)
3873 /* FIXME: Should we continue the loop in order to report other duplicates ? */
3874 break;
3875 /* alen == blen != 0. We might have two identical strings. If so we
3876 can ignore the second one. There is no need for wchar_t vs UTF-16
3877 theatrics here - we are only interested in (case sensitive) equality. */
3878 else if (memcmp (astring + 2, bstring + 2, alen * 2) != 0)
3879 break;
3880
3881 astring += (alen + 1) * 2;
3882 bstring += (blen + 1) * 2;
3883 }
3884
3885 if (i != 16)
3886 {
3887 if (a->parent != NULL
3888 && a->parent->entry != NULL
535b785f 3889 && !a->parent->entry->is_name)
5879bb8f
NC
3890 _bfd_error_handler (_(".rsrc merge failure: duplicate string resource: %d"),
3891 ((a->parent->entry->name_id.id - 1) << 4) + i);
0a1b45a2 3892 return false;
5879bb8f
NC
3893 }
3894
3895 if (copy_needed == 0)
0a1b45a2 3896 return true;
5879bb8f
NC
3897
3898 /* If we reach here then A and B must both have non-colliding strings.
3899 (We never get string resources with fully empty string tables).
3900 We need to allocate an extra COPY_NEEDED bytes in A and then bring
3901 in B's strings. */
9373215c 3902 new_data = bfd_malloc (a->value.leaf->size + copy_needed);
5879bb8f 3903 if (new_data == NULL)
0a1b45a2 3904 return false;
5879bb8f 3905
9373215c 3906 nstring = new_data;
5879bb8f
NC
3907 astring = a->value.leaf->data;
3908 bstring = b->value.leaf->data;
3909
3910 for (i = 0; i < 16; i++)
3911 {
3912 unsigned int alen = astring[0] + (astring[1] << 8);
3913 unsigned int blen = bstring[0] + (bstring[1] << 8);
3914
3915 if (alen != 0)
3916 {
3917 memcpy (nstring, astring, (alen + 1) * 2);
3918 nstring += (alen + 1) * 2;
3919 }
3920 else if (blen != 0)
3921 {
3922 memcpy (nstring, bstring, (blen + 1) * 2);
3923 nstring += (blen + 1) * 2;
3924 }
3925 else
3926 {
3927 * nstring++ = 0;
3928 * nstring++ = 0;
3929 }
9373215c 3930
5879bb8f
NC
3931 astring += (alen + 1) * 2;
3932 bstring += (blen + 1) * 2;
3933 }
3934
3935 BFD_ASSERT (nstring - new_data == (signed) (a->value.leaf->size + copy_needed));
9373215c 3936
5879bb8f
NC
3937 free (a->value.leaf->data);
3938 a->value.leaf->data = new_data;
3939 a->value.leaf->size += copy_needed;
3940
0a1b45a2 3941 return true;
5879bb8f
NC
3942}
3943
3944static void rsrc_merge (rsrc_entry *, rsrc_entry *);
3945
3946/* Sort the entries in given part of the directory.
3947 We use an old fashioned bubble sort because we are dealing
9373215c 3948 with lists and we want to handle matches specially. */
5879bb8f
NC
3949
3950static void
0a1b45a2
AM
3951rsrc_sort_entries (rsrc_dir_chain *chain,
3952 bool is_name,
3953 rsrc_directory *dir)
5879bb8f
NC
3954{
3955 rsrc_entry * entry;
3956 rsrc_entry * next;
3957 rsrc_entry ** points_to_entry;
0a1b45a2 3958 bool swapped;
5879bb8f
NC
3959
3960 if (chain->num_entries < 2)
3961 return;
3962
3963 do
3964 {
0a1b45a2 3965 swapped = false;
5879bb8f
NC
3966 points_to_entry = & chain->first_entry;
3967 entry = * points_to_entry;
3968 next = entry->next_entry;
3969
3970 do
3971 {
3972 signed int cmp = rsrc_cmp (is_name, entry, next);
3973
3974 if (cmp > 0)
3975 {
3976 entry->next_entry = next->next_entry;
3977 next->next_entry = entry;
3978 * points_to_entry = next;
3979 points_to_entry = & next->next_entry;
3980 next = entry->next_entry;
0a1b45a2 3981 swapped = true;
5879bb8f
NC
3982 }
3983 else if (cmp == 0)
3984 {
3985 if (entry->is_dir && next->is_dir)
3986 {
3987 /* When we encounter identical directory entries we have to
3988 merge them together. The exception to this rule is for
3989 resource manifests - there can only be one of these,
3990 even if they differ in language. Zero-language manifests
3991 are assumed to be default manifests (provided by the
3714081c 3992 Cygwin/MinGW build system) and these can be silently dropped,
5879bb8f
NC
3993 unless that would reduce the number of manifests to zero.
3994 There should only ever be one non-zero lang manifest -
3995 if there are more it is an error. A non-zero lang
3996 manifest takes precedence over a default manifest. */
535b785f 3997 if (!entry->is_name
5879bb8f
NC
3998 && entry->name_id.id == 1
3999 && dir != NULL
4000 && dir->entry != NULL
535b785f 4001 && !dir->entry->is_name
5879bb8f
NC
4002 && dir->entry->name_id.id == 0x18)
4003 {
4004 if (next->value.directory->names.num_entries == 0
4005 && next->value.directory->ids.num_entries == 1
535b785f 4006 && !next->value.directory->ids.first_entry->is_name
5879bb8f
NC
4007 && next->value.directory->ids.first_entry->name_id.id == 0)
4008 /* Fall through so that NEXT is dropped. */
4009 ;
4010 else if (entry->value.directory->names.num_entries == 0
4011 && entry->value.directory->ids.num_entries == 1
535b785f 4012 && !entry->value.directory->ids.first_entry->is_name
5879bb8f
NC
4013 && entry->value.directory->ids.first_entry->name_id.id == 0)
4014 {
4015 /* Swap ENTRY and NEXT. Then fall through so that the old ENTRY is dropped. */
4016 entry->next_entry = next->next_entry;
4017 next->next_entry = entry;
4018 * points_to_entry = next;
4019 points_to_entry = & next->next_entry;
4020 next = entry->next_entry;
0a1b45a2 4021 swapped = true;
5879bb8f
NC
4022 }
4023 else
4024 {
4025 _bfd_error_handler (_(".rsrc merge failure: multiple non-default manifests"));
4026 bfd_set_error (bfd_error_file_truncated);
4027 return;
4028 }
9373215c 4029
5879bb8f
NC
4030 /* Unhook NEXT from the chain. */
4031 /* FIXME: memory loss here. */
4032 entry->next_entry = next->next_entry;
4033 chain->num_entries --;
4034 if (chain->num_entries < 2)
4035 return;
4036 next = next->next_entry;
4037 }
4038 else
4039 rsrc_merge (entry, next);
4040 }
4041 else if (entry->is_dir != next->is_dir)
4042 {
4043 _bfd_error_handler (_(".rsrc merge failure: a directory matches a leaf"));
4044 bfd_set_error (bfd_error_file_truncated);
4045 return;
4046 }
4047 else
4048 {
4049 /* Otherwise with identical leaves we issue an error
4050 message - because there should never be duplicates.
4051 The exception is Type 18/Name 1/Lang 0 which is the
4052 defaul manifest - this can just be dropped. */
535b785f 4053 if (!entry->is_name
5879bb8f
NC
4054 && entry->name_id.id == 0
4055 && dir != NULL
4056 && dir->entry != NULL
535b785f 4057 && !dir->entry->is_name
5879bb8f
NC
4058 && dir->entry->name_id.id == 1
4059 && dir->entry->parent != NULL
4060 && dir->entry->parent->entry != NULL
535b785f 4061 && !dir->entry->parent->entry->is_name
5879bb8f
NC
4062 && dir->entry->parent->entry->name_id.id == 0x18 /* RT_MANIFEST */)
4063 ;
4064 else if (dir != NULL
4065 && dir->entry != NULL
4066 && dir->entry->parent != NULL
4067 && dir->entry->parent->entry != NULL
535b785f 4068 && !dir->entry->parent->entry->is_name
5879bb8f
NC
4069 && dir->entry->parent->entry->name_id.id == 0x6 /* RT_STRING */)
4070 {
4071 /* Strings need special handling. */
4072 if (! rsrc_merge_string_entries (entry, next))
4073 {
4074 /* _bfd_error_handler should have been called inside merge_strings. */
4075 bfd_set_error (bfd_error_file_truncated);
4076 return;
4077 }
4078 }
4079 else
4080 {
4081 if (dir == NULL
4082 || dir->entry == NULL
4083 || dir->entry->parent == NULL
4084 || dir->entry->parent->entry == NULL)
4085 _bfd_error_handler (_(".rsrc merge failure: duplicate leaf"));
4086 else
7fbd5f4e
AM
4087 {
4088 char buff[256];
4089
4090 _bfd_error_handler (_(".rsrc merge failure: duplicate leaf: %s"),
4091 rsrc_resource_name (entry, dir, buff));
4092 }
5879bb8f
NC
4093 bfd_set_error (bfd_error_file_truncated);
4094 return;
4095 }
4096 }
4097
4098 /* Unhook NEXT from the chain. */
4099 entry->next_entry = next->next_entry;
4100 chain->num_entries --;
4101 if (chain->num_entries < 2)
4102 return;
4103 next = next->next_entry;
4104 }
4105 else
4106 {
4107 points_to_entry = & entry->next_entry;
4108 entry = next;
4109 next = next->next_entry;
4110 }
4111 }
4112 while (next);
4113
4114 chain->last_entry = entry;
4115 }
4116 while (swapped);
4117}
4118
4119/* Attach B's chain onto A. */
4120static void
9373215c 4121rsrc_attach_chain (rsrc_dir_chain * achain, rsrc_dir_chain * bchain)
5879bb8f
NC
4122{
4123 if (bchain->num_entries == 0)
4124 return;
4125
4126 achain->num_entries += bchain->num_entries;
4127
4128 if (achain->first_entry == NULL)
4129 {
4130 achain->first_entry = bchain->first_entry;
4131 achain->last_entry = bchain->last_entry;
4132 }
4133 else
4134 {
4135 achain->last_entry->next_entry = bchain->first_entry;
4136 achain->last_entry = bchain->last_entry;
4137 }
9373215c 4138
5879bb8f
NC
4139 bchain->num_entries = 0;
4140 bchain->first_entry = bchain->last_entry = NULL;
4141}
4142
4143static void
4144rsrc_merge (struct rsrc_entry * a, struct rsrc_entry * b)
4145{
9373215c
PM
4146 rsrc_directory * adir;
4147 rsrc_directory * bdir;
4148
5879bb8f
NC
4149 BFD_ASSERT (a->is_dir);
4150 BFD_ASSERT (b->is_dir);
4151
9373215c
PM
4152 adir = a->value.directory;
4153 bdir = b->value.directory;
4154
5879bb8f
NC
4155 if (adir->characteristics != bdir->characteristics)
4156 {
59d08d6c 4157 _bfd_error_handler (_(".rsrc merge failure: dirs with differing characteristics"));
5879bb8f
NC
4158 bfd_set_error (bfd_error_file_truncated);
4159 return;
4160 }
9373215c 4161
5879bb8f
NC
4162 if (adir->major != bdir->major || adir->minor != bdir->minor)
4163 {
59d08d6c 4164 _bfd_error_handler (_(".rsrc merge failure: differing directory versions"));
5879bb8f
NC
4165 bfd_set_error (bfd_error_file_truncated);
4166 return;
4167 }
4168
4169 /* Attach B's name chain to A. */
4170 rsrc_attach_chain (& adir->names, & bdir->names);
4171
4172 /* Attach B's ID chain to A. */
4173 rsrc_attach_chain (& adir->ids, & bdir->ids);
4174
4175 /* Now sort A's entries. */
0a1b45a2
AM
4176 rsrc_sort_entries (& adir->names, true, adir);
4177 rsrc_sort_entries (& adir->ids, false, adir);
5879bb8f
NC
4178}
4179
4180/* Check the .rsrc section. If it contains multiple concatenated
4181 resources then we must merge them properly. Otherwise Windows
4182 will ignore all but the first set. */
4183
4184static void
4185rsrc_process_section (bfd * abfd,
4186 struct coff_final_link_info * pfinfo)
4187{
9373215c 4188 rsrc_directory new_table;
07d6d2b8
AM
4189 bfd_size_type size;
4190 asection * sec;
9373215c 4191 pe_data_type * pe;
07d6d2b8
AM
4192 bfd_vma rva_bias;
4193 bfd_byte * data;
4194 bfd_byte * datastart;
4195 bfd_byte * dataend;
4196 bfd_byte * new_data;
4197 unsigned int num_resource_sets;
9373215c
PM
4198 rsrc_directory * type_tables;
4199 rsrc_write_data write_data;
07d6d2b8
AM
4200 unsigned int indx;
4201 bfd * input;
4202 unsigned int num_input_rsrc = 0;
4203 unsigned int max_num_input_rsrc = 4;
4204 ptrdiff_t * rsrc_sizes = NULL;
5879bb8f
NC
4205
4206 new_table.names.num_entries = 0;
4207 new_table.ids.num_entries = 0;
9373215c 4208
5879bb8f
NC
4209 sec = bfd_get_section_by_name (abfd, ".rsrc");
4210 if (sec == NULL || (size = sec->rawsize) == 0)
4211 return;
4212
9373215c 4213 pe = pe_data (abfd);
5879bb8f
NC
4214 if (pe == NULL)
4215 return;
4216
5879bb8f
NC
4217 rva_bias = sec->vma - pe->pe_opthdr.ImageBase;
4218
10c38619 4219 if (! bfd_malloc_and_get_section (abfd, sec, &datastart))
5879bb8f
NC
4220 goto end;
4221
6caf7111
NC
4222 /* Step zero: Scan the input bfds looking for .rsrc sections and record
4223 their lengths. Note - we rely upon the fact that the linker script
4224 does *not* sort the input .rsrc sections, so that the order in the
4225 linkinfo list matches the order in the output .rsrc section.
4226
4227 We need to know the lengths because each input .rsrc section has padding
4228 at the end of a variable amount. (It does not appear to be based upon
4229 the section alignment or the file alignment). We need to skip any
4230 padding bytes when parsing the input .rsrc sections. */
10c38619
AM
4231 data = datastart;
4232 rsrc_sizes = bfd_malloc (max_num_input_rsrc * sizeof (*rsrc_sizes));
6caf7111
NC
4233 if (rsrc_sizes == NULL)
4234 goto end;
4235
4236 for (input = pfinfo->info->input_bfds;
4237 input != NULL;
c72f2fb2 4238 input = input->link.next)
6caf7111
NC
4239 {
4240 asection * rsrc_sec = bfd_get_section_by_name (input, ".rsrc");
4241
9ac47a43
TS
4242 /* PR 18372 - skip discarded .rsrc sections. */
4243 if (rsrc_sec != NULL && !discarded_section (rsrc_sec))
6caf7111
NC
4244 {
4245 if (num_input_rsrc == max_num_input_rsrc)
4246 {
4247 max_num_input_rsrc += 10;
4248 rsrc_sizes = bfd_realloc (rsrc_sizes, max_num_input_rsrc
10c38619 4249 * sizeof (*rsrc_sizes));
6caf7111
NC
4250 if (rsrc_sizes == NULL)
4251 goto end;
4252 }
4253
4254 BFD_ASSERT (rsrc_sec->size > 0);
4255 rsrc_sizes [num_input_rsrc ++] = rsrc_sec->size;
4256 }
4257 }
4258
4259 if (num_input_rsrc < 2)
4260 goto end;
61e2488c 4261
5879bb8f
NC
4262 /* Step one: Walk the section, computing the size of the tables,
4263 leaves and data and decide if we need to do anything. */
1d63324c 4264 dataend = data + size;
9373215c 4265 num_resource_sets = 0;
5879bb8f
NC
4266
4267 while (data < dataend)
4268 {
4269 bfd_byte * p = data;
4270
4271 data = rsrc_count_directory (abfd, data, data, dataend, rva_bias);
1d63324c 4272
5879bb8f
NC
4273 if (data > dataend)
4274 {
4275 /* Corrupted .rsrc section - cannot merge. */
871b3ab2 4276 _bfd_error_handler (_("%pB: .rsrc merge failure: corrupt .rsrc section"),
dae82561 4277 abfd);
5879bb8f
NC
4278 bfd_set_error (bfd_error_file_truncated);
4279 goto end;
4280 }
4281
6caf7111
NC
4282 if ((data - p) > rsrc_sizes [num_resource_sets])
4283 {
871b3ab2 4284 _bfd_error_handler (_("%pB: .rsrc merge failure: unexpected .rsrc size"),
dae82561 4285 abfd);
6caf7111
NC
4286 bfd_set_error (bfd_error_file_truncated);
4287 goto end;
4288 }
4289 /* FIXME: Should we add a check for "data - p" being much smaller
4290 than rsrc_sizes[num_resource_sets] ? */
4291
4292 data = p + rsrc_sizes[num_resource_sets];
5879bb8f 4293 rva_bias += data - p;
5879bb8f
NC
4294 ++ num_resource_sets;
4295 }
6caf7111 4296 BFD_ASSERT (num_resource_sets == num_input_rsrc);
5879bb8f
NC
4297
4298 /* Step two: Walk the data again, building trees of the resources. */
4299 data = datastart;
4300 rva_bias = sec->vma - pe->pe_opthdr.ImageBase;
4301
10c38619 4302 type_tables = bfd_malloc (num_resource_sets * sizeof (*type_tables));
5879bb8f
NC
4303 if (type_tables == NULL)
4304 goto end;
4305
9373215c 4306 indx = 0;
5879bb8f
NC
4307 while (data < dataend)
4308 {
4309 bfd_byte * p = data;
4310
6caf7111 4311 (void) rsrc_parse_directory (abfd, type_tables + indx, data, data,
9373215c 4312 dataend, rva_bias, NULL);
6caf7111 4313 data = p + rsrc_sizes[indx];
5879bb8f 4314 rva_bias += data - p;
6caf7111 4315 ++ indx;
5879bb8f 4316 }
337e86d7 4317 BFD_ASSERT (indx == num_resource_sets);
9373215c 4318
5879bb8f 4319 /* Step three: Merge the top level tables (there can be only one).
9373215c 4320
5879bb8f 4321 We must ensure that the merged entries are in ascending order.
9373215c 4322
5879bb8f
NC
4323 We also thread the top level table entries from the old tree onto
4324 the new table, so that they can be pulled off later. */
4325
4326 /* FIXME: Should we verify that all type tables are the same ? */
4327 new_table.characteristics = type_tables[0].characteristics;
07d6d2b8
AM
4328 new_table.time = type_tables[0].time;
4329 new_table.major = type_tables[0].major;
4330 new_table.minor = type_tables[0].minor;
5879bb8f
NC
4331
4332 /* Chain the NAME entries onto the table. */
4333 new_table.names.first_entry = NULL;
4334 new_table.names.last_entry = NULL;
4335
337e86d7
L
4336 for (indx = 0; indx < num_resource_sets; indx++)
4337 rsrc_attach_chain (& new_table.names, & type_tables[indx].names);
5879bb8f 4338
0a1b45a2 4339 rsrc_sort_entries (& new_table.names, true, & new_table);
9373215c 4340
5879bb8f
NC
4341 /* Chain the ID entries onto the table. */
4342 new_table.ids.first_entry = NULL;
4343 new_table.ids.last_entry = NULL;
4344
337e86d7
L
4345 for (indx = 0; indx < num_resource_sets; indx++)
4346 rsrc_attach_chain (& new_table.ids, & type_tables[indx].ids);
5879bb8f 4347
0a1b45a2 4348 rsrc_sort_entries (& new_table.ids, false, & new_table);
5879bb8f
NC
4349
4350 /* Step four: Create new contents for the .rsrc section. */
3714081c
NC
4351 /* Step four point one: Compute the size of each region of the .rsrc section.
4352 We do this now, rather than earlier, as the merging above may have dropped
4353 some entries. */
4354 sizeof_leaves = sizeof_strings = sizeof_tables_and_entries = 0;
4355 rsrc_compute_region_sizes (& new_table);
4356 /* We increment sizeof_strings to make sure that resource data
4357 starts on an 8-byte boundary. FIXME: Is this correct ? */
4358 sizeof_strings = (sizeof_strings + 7) & ~ 7;
4359
c32abae8 4360 new_data = bfd_zalloc (abfd, size);
5879bb8f
NC
4361 if (new_data == NULL)
4362 goto end;
4363
07d6d2b8
AM
4364 write_data.abfd = abfd;
4365 write_data.datastart = new_data;
4366 write_data.next_table = new_data;
4367 write_data.next_leaf = new_data + sizeof_tables_and_entries;
5879bb8f 4368 write_data.next_string = write_data.next_leaf + sizeof_leaves;
07d6d2b8
AM
4369 write_data.next_data = write_data.next_string + sizeof_strings;
4370 write_data.rva_bias = sec->vma - pe->pe_opthdr.ImageBase;
5879bb8f
NC
4371
4372 rsrc_write_directory (& write_data, & new_table);
4373
4374 /* Step five: Replace the old contents with the new.
ec8f7688
JT
4375 We don't recompute the size as it's too late here to shrink section.
4376 See PR ld/20193 for more details. */
5879bb8f
NC
4377 bfd_set_section_contents (pfinfo->output_bfd, sec, new_data, 0, size);
4378 sec->size = sec->rawsize = size;
9373215c 4379
5879bb8f 4380 end:
3714081c 4381 /* Step six: Free all the memory that we have used. */
5879bb8f
NC
4382 /* FIXME: Free the resource tree, if we have one. */
4383 free (datastart);
6caf7111 4384 free (rsrc_sizes);
5879bb8f 4385}
5174d0fb 4386
2fbadf2c
ILT
4387/* Handle the .idata section and other things that need symbol table
4388 access. */
4389
0a1b45a2 4390bool
7920ce38 4391_bfd_XXi_final_link_postscript (bfd * abfd, struct coff_final_link_info *pfinfo)
2fbadf2c
ILT
4392{
4393 struct coff_link_hash_entry *h1;
4394 struct bfd_link_info *info = pfinfo->info;
0a1b45a2 4395 bool result = true;
2fbadf2c
ILT
4396
4397 /* There are a few fields that need to be filled in now while we
4398 have symbol table access.
4399
4400 The .idata subsections aren't directly available as sections, but
4401 they are in the symbol table, so get them from there. */
4402
4403 /* The import directory. This is the address of .idata$2, with size
4404 of .idata$2 + .idata$3. */
4405 h1 = coff_link_hash_lookup (coff_hash_table (info),
0a1b45a2 4406 ".idata$2", false, false, true);
2fbadf2c
ILT
4407 if (h1 != NULL)
4408 {
4e1fc599 4409 /* PR ld/2729: We cannot rely upon all the output sections having been
4e22f78d
NC
4410 created properly, so check before referencing them. Issue a warning
4411 message for any sections tht could not be found. */
b92997d6
AM
4412 if ((h1->root.type == bfd_link_hash_defined
4413 || h1->root.type == bfd_link_hash_defweak)
4414 && h1->root.u.def.section != NULL
4e22f78d 4415 && h1->root.u.def.section->output_section != NULL)
6c73cbb1 4416 pe_data (abfd)->pe_opthdr.DataDirectory[PE_IMPORT_TABLE].VirtualAddress =
4e22f78d
NC
4417 (h1->root.u.def.value
4418 + h1->root.u.def.section->output_section->vma
4419 + h1->root.u.def.section->output_offset);
4420 else
4421 {
4422 _bfd_error_handler
871b3ab2 4423 (_("%pB: unable to fill in DataDictionary[1] because .idata$2 is missing"),
4e22f78d 4424 abfd);
0a1b45a2 4425 result = false;
4e22f78d
NC
4426 }
4427
2fbadf2c 4428 h1 = coff_link_hash_lookup (coff_hash_table (info),
0a1b45a2 4429 ".idata$4", false, false, true);
4e22f78d 4430 if (h1 != NULL
b92997d6
AM
4431 && (h1->root.type == bfd_link_hash_defined
4432 || h1->root.type == bfd_link_hash_defweak)
4e22f78d
NC
4433 && h1->root.u.def.section != NULL
4434 && h1->root.u.def.section->output_section != NULL)
6c73cbb1 4435 pe_data (abfd)->pe_opthdr.DataDirectory[PE_IMPORT_TABLE].Size =
4e22f78d
NC
4436 ((h1->root.u.def.value
4437 + h1->root.u.def.section->output_section->vma
4438 + h1->root.u.def.section->output_offset)
6c73cbb1 4439 - pe_data (abfd)->pe_opthdr.DataDirectory[PE_IMPORT_TABLE].VirtualAddress);
4e22f78d
NC
4440 else
4441 {
4442 _bfd_error_handler
871b3ab2 4443 (_("%pB: unable to fill in DataDictionary[1] because .idata$4 is missing"),
4e22f78d 4444 abfd);
0a1b45a2 4445 result = false;
4e22f78d 4446 }
2fbadf2c
ILT
4447
4448 /* The import address table. This is the size/address of
07d6d2b8 4449 .idata$5. */
2fbadf2c 4450 h1 = coff_link_hash_lookup (coff_hash_table (info),
0a1b45a2 4451 ".idata$5", false, false, true);
4e22f78d 4452 if (h1 != NULL
b92997d6
AM
4453 && (h1->root.type == bfd_link_hash_defined
4454 || h1->root.type == bfd_link_hash_defweak)
4e22f78d
NC
4455 && h1->root.u.def.section != NULL
4456 && h1->root.u.def.section->output_section != NULL)
6c73cbb1 4457 pe_data (abfd)->pe_opthdr.DataDirectory[PE_IMPORT_ADDRESS_TABLE].VirtualAddress =
4e22f78d
NC
4458 (h1->root.u.def.value
4459 + h1->root.u.def.section->output_section->vma
4460 + h1->root.u.def.section->output_offset);
4461 else
4462 {
4463 _bfd_error_handler
871b3ab2 4464 (_("%pB: unable to fill in DataDictionary[12] because .idata$5 is missing"),
4e22f78d 4465 abfd);
0a1b45a2 4466 result = false;
4e22f78d
NC
4467 }
4468
2fbadf2c 4469 h1 = coff_link_hash_lookup (coff_hash_table (info),
0a1b45a2 4470 ".idata$6", false, false, true);
4e22f78d 4471 if (h1 != NULL
b92997d6
AM
4472 && (h1->root.type == bfd_link_hash_defined
4473 || h1->root.type == bfd_link_hash_defweak)
4e22f78d
NC
4474 && h1->root.u.def.section != NULL
4475 && h1->root.u.def.section->output_section != NULL)
6c73cbb1 4476 pe_data (abfd)->pe_opthdr.DataDirectory[PE_IMPORT_ADDRESS_TABLE].Size =
4e22f78d
NC
4477 ((h1->root.u.def.value
4478 + h1->root.u.def.section->output_section->vma
4479 + h1->root.u.def.section->output_offset)
4e1fc599 4480 - pe_data (abfd)->pe_opthdr.DataDirectory[PE_IMPORT_ADDRESS_TABLE].VirtualAddress);
4e22f78d
NC
4481 else
4482 {
4483 _bfd_error_handler
871b3ab2 4484 (_("%pB: unable to fill in DataDictionary[PE_IMPORT_ADDRESS_TABLE (12)] because .idata$6 is missing"),
4e22f78d 4485 abfd);
0a1b45a2 4486 result = false;
4e22f78d 4487 }
2fbadf2c 4488 }
cb2f80e6
KT
4489 else
4490 {
4491 h1 = coff_link_hash_lookup (coff_hash_table (info),
0a1b45a2 4492 "__IAT_start__", false, false, true);
cb2f80e6
KT
4493 if (h1 != NULL
4494 && (h1->root.type == bfd_link_hash_defined
4495 || h1->root.type == bfd_link_hash_defweak)
4496 && h1->root.u.def.section != NULL
4497 && h1->root.u.def.section->output_section != NULL)
4498 {
4499 bfd_vma iat_va;
4500
4501 iat_va =
4502 (h1->root.u.def.value
4503 + h1->root.u.def.section->output_section->vma
4504 + h1->root.u.def.section->output_offset);
4505
4506 h1 = coff_link_hash_lookup (coff_hash_table (info),
0a1b45a2 4507 "__IAT_end__", false, false, true);
cb2f80e6
KT
4508 if (h1 != NULL
4509 && (h1->root.type == bfd_link_hash_defined
4510 || h1->root.type == bfd_link_hash_defweak)
4511 && h1->root.u.def.section != NULL
4512 && h1->root.u.def.section->output_section != NULL)
4513 {
4514 pe_data (abfd)->pe_opthdr.DataDirectory[PE_IMPORT_ADDRESS_TABLE].Size =
4515 ((h1->root.u.def.value
4516 + h1->root.u.def.section->output_section->vma
4517 + h1->root.u.def.section->output_offset)
4518 - iat_va);
4519 if (pe_data (abfd)->pe_opthdr.DataDirectory[PE_IMPORT_ADDRESS_TABLE].Size != 0)
4520 pe_data (abfd)->pe_opthdr.DataDirectory[PE_IMPORT_ADDRESS_TABLE].VirtualAddress =
4521 iat_va - pe_data (abfd)->pe_opthdr.ImageBase;
4522 }
4523 else
4524 {
4525 _bfd_error_handler
871b3ab2 4526 (_("%pB: unable to fill in DataDictionary[PE_IMPORT_ADDRESS_TABLE(12)]"
cb2f80e6 4527 " because .idata$6 is missing"), abfd);
0a1b45a2 4528 result = false;
cb2f80e6 4529 }
07d6d2b8 4530 }
cb2f80e6 4531 }
ca6dee30
NC
4532
4533 h1 = coff_link_hash_lookup (coff_hash_table (info),
61e2488c 4534 (bfd_get_symbol_leading_char (abfd) != 0
c91a930c 4535 ? "__tls_used" : "_tls_used"),
0a1b45a2 4536 false, false, true);
ca6dee30
NC
4537 if (h1 != NULL)
4538 {
b92997d6
AM
4539 if ((h1->root.type == bfd_link_hash_defined
4540 || h1->root.type == bfd_link_hash_defweak)
4541 && h1->root.u.def.section != NULL
4e22f78d 4542 && h1->root.u.def.section->output_section != NULL)
6c73cbb1 4543 pe_data (abfd)->pe_opthdr.DataDirectory[PE_TLS_TABLE].VirtualAddress =
4e22f78d
NC
4544 (h1->root.u.def.value
4545 + h1->root.u.def.section->output_section->vma
4546 + h1->root.u.def.section->output_offset
4547 - pe_data (abfd)->pe_opthdr.ImageBase);
4548 else
4549 {
4550 _bfd_error_handler
871b3ab2 4551 (_("%pB: unable to fill in DataDictionary[9] because __tls_used is missing"),
4e22f78d 4552 abfd);
0a1b45a2 4553 result = false;
4e22f78d 4554 }
bc2b2990
PM
4555 /* According to PECOFF sepcifications by Microsoft version 8.2
4556 the TLS data directory consists of 4 pointers, followed
4557 by two 4-byte integer. This implies that the total size
68ffbac6 4558 is different for 32-bit and 64-bit executables. */
31f60095 4559#if !defined(COFF_WITH_pep) && !defined(COFF_WITH_pex64) && !defined(COFF_WITH_peAArch64) && !defined(COFF_WITH_peLoongArch64)
6c73cbb1 4560 pe_data (abfd)->pe_opthdr.DataDirectory[PE_TLS_TABLE].Size = 0x18;
bc2b2990
PM
4561#else
4562 pe_data (abfd)->pe_opthdr.DataDirectory[PE_TLS_TABLE].Size = 0x28;
4563#endif
ca6dee30
NC
4564 }
4565
5174d0fb
KT
4566/* If there is a .pdata section and we have linked pdata finally, we
4567 need to sort the entries ascending. */
31f60095 4568#if !defined(COFF_WITH_pep) && (defined(COFF_WITH_pex64) || defined(COFF_WITH_peAArch64) || defined(COFF_WITH_peLoongArch64))
5174d0fb
KT
4569 {
4570 asection *sec = bfd_get_section_by_name (abfd, ".pdata");
4571
4572 if (sec)
4573 {
21e68916 4574 bfd_size_type x = sec->rawsize;
10c38619 4575 bfd_byte *tmp_data;
21e68916 4576
10c38619 4577 if (bfd_malloc_and_get_section (abfd, sec, &tmp_data))
5174d0fb 4578 {
10c38619
AM
4579 qsort (tmp_data,
4580 (size_t) (x / 12),
4581 12, sort_x64_pdata);
4582 bfd_set_section_contents (pfinfo->output_bfd, sec,
4583 tmp_data, 0, x);
21e68916 4584 free (tmp_data);
5174d0fb 4585 }
86eafac0 4586 else
0a1b45a2 4587 result = false;
5174d0fb
KT
4588 }
4589 }
4590#endif
4591
5879bb8f
NC
4592 rsrc_process_section (abfd, pfinfo);
4593
2fbadf2c
ILT
4594 /* If we couldn't find idata$2, we either have an excessively
4595 trivial program or are in DEEP trouble; we have to assume trivial
4596 program.... */
4e22f78d 4597 return result;
2fbadf2c 4598}