Coverage Report

Created: 2026-11-21 09:00

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