Coverage Report

Created: 2026-07-25 10:20

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/binutils-gdb/bfd/elf.c
Line
Count
Source
1
/* ELF executable support for BFD.
2
3
   Copyright (C) 1993-2026 Free Software Foundation, Inc.
4
5
   This file is part of BFD, the Binary File Descriptor library.
6
7
   This program is free software; you can redistribute it and/or modify
8
   it under the terms of the GNU General Public License as published by
9
   the Free Software Foundation; either version 3 of the License, or
10
   (at your option) any later version.
11
12
   This program is distributed in the hope that it will be useful,
13
   but WITHOUT ANY WARRANTY; without even the implied warranty of
14
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
   GNU General Public License for more details.
16
17
   You should have received a copy of the GNU General Public License
18
   along with this program; if not, write to the Free Software
19
   Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
20
   MA 02110-1301, USA.  */
21
22
23
/*
24
SECTION
25
  ELF backends
26
27
  BFD support for ELF formats is being worked on.
28
  Currently, the best supported back ends are for sparc and i386
29
  (running svr4 or Solaris 2).
30
31
  Documentation of the internals of the support code still needs
32
  to be written.  The code is changing quickly enough that we
33
  haven't bothered yet.  */
34
35
/* For sparc64-cross-sparc32.  */
36
#define _SYSCALL32
37
#include "sysdep.h"
38
#include <limits.h>
39
#include "bfd.h"
40
#include "bfdlink.h"
41
#include "libbfd.h"
42
#define ARCH_SIZE 0
43
#include "elf-bfd.h"
44
#include "sframe-api.h"
45
#include "libiberty.h"
46
#include "safe-ctype.h"
47
#include "elf-linux-core.h"
48
49
#ifdef CORE_HEADER
50
#include CORE_HEADER
51
#endif
52
53
/* Utility macro to make testing for string equality easier to read.  */
54
#ifndef streq
55
76.2k
#define streq(A,B) (strcmp ((A), (B)) == 0)
56
#endif
57
58
/* Core note names.  */
59
0
#define NOTE_NAME_CORE     "CORE"
60
0
#define NOTE_NAME_FREEBSD  "FreeBSD"
61
0
#define NOTE_NAME_GDB      "GDB"
62
0
#define NOTE_NAME_LINUX    "LINUX"
63
64
/* Names of a pseudo-section which represent core notes.  */
65
0
#define NOTE_PSEUDO_SECTION_AARCH_FPMR    ".reg-aarch-fpmr"
66
0
#define NOTE_PSEUDO_SECTION_AARCH_GCS   ".reg-aarch-gcs"
67
0
#define NOTE_PSEUDO_SECTION_AARCH_POE   ".reg-aarch-poe"
68
0
#define NOTE_PSEUDO_SECTION_AARCH_HW_BREAK  ".reg-aarch-hw-break"
69
0
#define NOTE_PSEUDO_SECTION_AARCH_HW_WATCH  ".reg-aarch-hw-watch"
70
0
#define NOTE_PSEUDO_SECTION_AARCH_MTE   ".reg-aarch-mte"
71
0
#define NOTE_PSEUDO_SECTION_AARCH_PAUTH   ".reg-aarch-pauth"
72
0
#define NOTE_PSEUDO_SECTION_AARCH_SSVE    ".reg-aarch-ssve"
73
0
#define NOTE_PSEUDO_SECTION_AARCH_SVE   ".reg-aarch-sve"
74
0
#define NOTE_PSEUDO_SECTION_AARCH_TLS   ".reg-aarch-tls"
75
0
#define NOTE_PSEUDO_SECTION_AARCH_ZA    ".reg-aarch-za"
76
0
#define NOTE_PSEUDO_SECTION_AARCH_ZT    ".reg-aarch-zt"
77
0
#define NOTE_PSEUDO_SECTION_ARC_V2    ".reg-arc-v2"
78
0
#define NOTE_PSEUDO_SECTION_ARM_VFP   ".reg-arm-vfp"
79
0
#define NOTE_PSEUDO_SECTION_I386_TLS    ".reg-i386-tls"
80
0
#define NOTE_PSEUDO_SECTION_LOONGARCH_CPUCFG  ".reg-loongarch-cpucfg"
81
0
#define NOTE_PSEUDO_SECTION_LOONGARCH_LASX  ".reg-loongarch-lasx"
82
0
#define NOTE_PSEUDO_SECTION_LOONGARCH_LBT ".reg-loongarch-lbt"
83
0
#define NOTE_PSEUDO_SECTION_LOONGARCH_LSX ".reg-loongarch-lsx"
84
0
#define NOTE_PSEUDO_SECTION_PPC_DSCR    ".reg-ppc-dscr"
85
0
#define NOTE_PSEUDO_SECTION_PPC_EBB   ".reg-ppc-ebb"
86
0
#define NOTE_PSEUDO_SECTION_PPC_PMU   ".reg-ppc-pmu"
87
0
#define NOTE_PSEUDO_SECTION_PPC_PPR   ".reg-ppc-ppr"
88
0
#define NOTE_PSEUDO_SECTION_PPC_TAR   ".reg-ppc-tar"
89
0
#define NOTE_PSEUDO_SECTION_PPC_TM_CDSCR  ".reg-ppc-tm-cdscr"
90
0
#define NOTE_PSEUDO_SECTION_PPC_TM_CFPR   ".reg-ppc-tm-cfpr"
91
0
#define NOTE_PSEUDO_SECTION_PPC_TM_CGPR   ".reg-ppc-tm-cgpr"
92
0
#define NOTE_PSEUDO_SECTION_PPC_TM_CPPR   ".reg-ppc-tm-cppr"
93
0
#define NOTE_PSEUDO_SECTION_PPC_TM_CTAR   ".reg-ppc-tm-ctar"
94
0
#define NOTE_PSEUDO_SECTION_PPC_TM_CVMX   ".reg-ppc-tm-cvmx"
95
0
#define NOTE_PSEUDO_SECTION_PPC_TM_CVSX   ".reg-ppc-tm-cvsx"
96
0
#define NOTE_PSEUDO_SECTION_PPC_TM_SPR    ".reg-ppc-tm-spr"
97
0
#define NOTE_PSEUDO_SECTION_PPC_VSX   ".reg-ppc-vsx"
98
0
#define NOTE_PSEUDO_SECTION_PPC_VMX   ".reg-ppc-vmx"
99
1
#define NOTE_PSEUDO_SECTION_REG     ".reg"
100
174
#define NOTE_PSEUDO_SECTION_REG2    ".reg2"
101
0
#define NOTE_PSEUDO_SECTION_RISCV_CSR   ".reg-riscv-csr"
102
0
#define NOTE_PSEUDO_SECTION_S390_CTRS   ".reg-s390-ctrs"
103
0
#define NOTE_PSEUDO_SECTION_S390_GS_BC    ".reg-s390-gs-bc"
104
0
#define NOTE_PSEUDO_SECTION_S390_GS_CB    ".reg-s390-gs-cb"
105
0
#define NOTE_PSEUDO_SECTION_S390_HIGH_GPRS  ".reg-s390-high-gprs"
106
0
#define NOTE_PSEUDO_SECTION_S390_LAST_BREAK ".reg-s390-last-break"
107
0
#define NOTE_PSEUDO_SECTION_S390_PREFIX   ".reg-s390-prefix"
108
0
#define NOTE_PSEUDO_SECTION_S390_SYSTEM_CALL  ".reg-s390-system-call"
109
0
#define NOTE_PSEUDO_SECTION_S390_TDB    ".reg-s390-tdb"
110
0
#define NOTE_PSEUDO_SECTION_S390_TIMER    ".reg-s390-timer"
111
0
#define NOTE_PSEUDO_SECTION_S390_TODCMP   ".reg-s390-todcmp"
112
0
#define NOTE_PSEUDO_SECTION_S390_TODPREG  ".reg-s390-todpreg"
113
0
#define NOTE_PSEUDO_SECTION_S390_VXRS_HIGH  ".reg-s390-vxrs-high"
114
0
#define NOTE_PSEUDO_SECTION_S390_VXRS_LOW ".reg-s390-vxrs-low"
115
0
#define NOTE_PSEUDO_SECTION_SSP     ".reg-ssp"
116
0
#define NOTE_PSEUDO_SECTION_TDESC   ".gdb-tdesc"
117
0
#define NOTE_PSEUDO_SECTION_X86_SEGBASES  ".reg-x86-segbases"
118
0
#define NOTE_PSEUDO_SECTION_XFP           ".reg-xfp"
119
0
#define NOTE_PSEUDO_SECTION_XSTATE        ".reg-xstate"
120
0
#define NOTE_PSEUDO_SECTION_XSAVE_LAYOUT  ".reg-xsave-layout"
121
122
static int elf_sort_sections (const void *, const void *);
123
static bool assign_file_positions_except_relocs (bfd *, struct bfd_link_info *);
124
static bool swap_out_syms (bfd *, struct elf_strtab_hash **, int,
125
         struct bfd_link_info *);
126
static bool elf_parse_notes (bfd *abfd, char *buf, size_t size,
127
           file_ptr offset, size_t align);
128
129
/* Swap version information in and out.  The version information is
130
   currently size independent.  If that ever changes, this code will
131
   need to move into elfcode.h.  */
132
133
/* Swap in a Verdef structure.  */
134
135
void
136
_bfd_elf_swap_verdef_in (bfd *abfd,
137
       const Elf_External_Verdef *src,
138
       Elf_Internal_Verdef *dst)
139
80
{
140
80
  dst->vd_version = H_GET_16 (abfd, src->vd_version);
141
80
  dst->vd_flags   = H_GET_16 (abfd, src->vd_flags);
142
80
  dst->vd_ndx     = H_GET_16 (abfd, src->vd_ndx);
143
80
  dst->vd_cnt     = H_GET_16 (abfd, src->vd_cnt);
144
80
  dst->vd_hash    = H_GET_32 (abfd, src->vd_hash);
145
80
  dst->vd_aux     = H_GET_32 (abfd, src->vd_aux);
146
80
  dst->vd_next    = H_GET_32 (abfd, src->vd_next);
147
80
}
148
149
/* Swap out a Verdef structure.  */
150
151
void
152
_bfd_elf_swap_verdef_out (bfd *abfd,
153
        const Elf_Internal_Verdef *src,
154
        Elf_External_Verdef *dst)
155
0
{
156
0
  H_PUT_16 (abfd, src->vd_version, dst->vd_version);
157
0
  H_PUT_16 (abfd, src->vd_flags, dst->vd_flags);
158
0
  H_PUT_16 (abfd, src->vd_ndx, dst->vd_ndx);
159
0
  H_PUT_16 (abfd, src->vd_cnt, dst->vd_cnt);
160
0
  H_PUT_32 (abfd, src->vd_hash, dst->vd_hash);
161
0
  H_PUT_32 (abfd, src->vd_aux, dst->vd_aux);
162
0
  H_PUT_32 (abfd, src->vd_next, dst->vd_next);
163
0
}
164
165
/* Swap in a Verdaux structure.  */
166
167
void
168
_bfd_elf_swap_verdaux_in (bfd *abfd,
169
        const Elf_External_Verdaux *src,
170
        Elf_Internal_Verdaux *dst)
171
8
{
172
8
  dst->vda_name = H_GET_32 (abfd, src->vda_name);
173
8
  dst->vda_next = H_GET_32 (abfd, src->vda_next);
174
8
}
175
176
/* Swap out a Verdaux structure.  */
177
178
void
179
_bfd_elf_swap_verdaux_out (bfd *abfd,
180
         const Elf_Internal_Verdaux *src,
181
         Elf_External_Verdaux *dst)
182
0
{
183
0
  H_PUT_32 (abfd, src->vda_name, dst->vda_name);
184
0
  H_PUT_32 (abfd, src->vda_next, dst->vda_next);
185
0
}
186
187
/* Swap in a Verneed structure.  */
188
189
void
190
_bfd_elf_swap_verneed_in (bfd *abfd,
191
        const Elf_External_Verneed *src,
192
        Elf_Internal_Verneed *dst)
193
253
{
194
253
  dst->vn_version = H_GET_16 (abfd, src->vn_version);
195
253
  dst->vn_cnt     = H_GET_16 (abfd, src->vn_cnt);
196
253
  dst->vn_file    = H_GET_32 (abfd, src->vn_file);
197
253
  dst->vn_aux     = H_GET_32 (abfd, src->vn_aux);
198
253
  dst->vn_next    = H_GET_32 (abfd, src->vn_next);
199
253
}
200
201
/* Swap out a Verneed structure.  */
202
203
void
204
_bfd_elf_swap_verneed_out (bfd *abfd,
205
         const Elf_Internal_Verneed *src,
206
         Elf_External_Verneed *dst)
207
0
{
208
0
  H_PUT_16 (abfd, src->vn_version, dst->vn_version);
209
0
  H_PUT_16 (abfd, src->vn_cnt, dst->vn_cnt);
210
0
  H_PUT_32 (abfd, src->vn_file, dst->vn_file);
211
0
  H_PUT_32 (abfd, src->vn_aux, dst->vn_aux);
212
0
  H_PUT_32 (abfd, src->vn_next, dst->vn_next);
213
0
}
214
215
/* Swap in a Vernaux structure.  */
216
217
void
218
_bfd_elf_swap_vernaux_in (bfd *abfd,
219
        const Elf_External_Vernaux *src,
220
        Elf_Internal_Vernaux *dst)
221
149
{
222
149
  dst->vna_hash  = H_GET_32 (abfd, src->vna_hash);
223
149
  dst->vna_flags = H_GET_16 (abfd, src->vna_flags);
224
149
  dst->vna_other = H_GET_16 (abfd, src->vna_other);
225
149
  dst->vna_name  = H_GET_32 (abfd, src->vna_name);
226
149
  dst->vna_next  = H_GET_32 (abfd, src->vna_next);
227
149
}
228
229
/* Swap out a Vernaux structure.  */
230
231
void
232
_bfd_elf_swap_vernaux_out (bfd *abfd,
233
         const Elf_Internal_Vernaux *src,
234
         Elf_External_Vernaux *dst)
235
0
{
236
0
  H_PUT_32 (abfd, src->vna_hash, dst->vna_hash);
237
0
  H_PUT_16 (abfd, src->vna_flags, dst->vna_flags);
238
0
  H_PUT_16 (abfd, src->vna_other, dst->vna_other);
239
0
  H_PUT_32 (abfd, src->vna_name, dst->vna_name);
240
0
  H_PUT_32 (abfd, src->vna_next, dst->vna_next);
241
0
}
242
243
/* Swap in a Versym structure.  */
244
245
void
246
_bfd_elf_swap_versym_in (bfd *abfd,
247
       const Elf_External_Versym *src,
248
       Elf_Internal_Versym *dst)
249
888
{
250
888
  dst->vs_vers = H_GET_16 (abfd, src->vs_vers);
251
888
}
252
253
/* Swap out a Versym structure.  */
254
255
void
256
_bfd_elf_swap_versym_out (bfd *abfd,
257
        const Elf_Internal_Versym *src,
258
        Elf_External_Versym *dst)
259
0
{
260
0
  H_PUT_16 (abfd, src->vs_vers, dst->vs_vers);
261
0
}
262
263
/* Standard ELF hash function.  Do not change this function; you will
264
   cause invalid hash tables to be generated.  */
265
266
unsigned long
267
bfd_elf_hash (const char *namearg)
268
0
{
269
0
  uint32_t h = 0;
270
271
0
  for (const unsigned char *name = (const unsigned char *) namearg;
272
0
       *name; name++)
273
0
    {
274
0
      h = (h << 4) + *name;
275
0
      h ^= (h >> 24) & 0xf0;
276
0
    }
277
0
  return h & 0x0fffffff;
278
0
}
279
280
/* DT_GNU_HASH hash function.  Do not change this function; you will
281
   cause invalid hash tables to be generated.  */
282
283
unsigned long
284
bfd_elf_gnu_hash (const char *namearg)
285
0
{
286
0
  uint32_t h = 5381;
287
288
0
  for (const unsigned char *name = (const unsigned char *) namearg;
289
0
       *name; name++)
290
0
    h = (h << 5) + h + *name;
291
0
  return h;
292
0
}
293
294
/* Create a tdata field OBJECT_SIZE bytes in length, zeroed out and with
295
   the object_id field of an elf_obj_tdata field set.  */
296
bool
297
bfd_elf_allocate_object (bfd *abfd,
298
       size_t object_size)
299
3.61M
{
300
3.61M
  BFD_ASSERT (object_size >= sizeof (struct elf_obj_tdata));
301
3.61M
  abfd->tdata.any = bfd_zalloc (abfd, object_size);
302
3.61M
  if (abfd->tdata.any == NULL)
303
0
    return false;
304
305
3.61M
  elf_object_id (abfd) = get_elf_backend_data (abfd)->target_id;
306
3.61M
  if (abfd->direction != read_direction)
307
1.49k
    {
308
1.49k
      struct output_elf_obj_tdata *o = bfd_zalloc (abfd, sizeof *o);
309
1.49k
      if (o == NULL)
310
0
  return false;
311
1.49k
      elf_tdata (abfd)->o = o;
312
1.49k
      elf_program_header_size (abfd) = (bfd_size_type) -1;
313
1.49k
    }
314
3.61M
  return true;
315
3.61M
}
316
317
318
bool
319
bfd_elf_make_object (bfd *abfd)
320
1.76M
{
321
1.76M
  return bfd_elf_allocate_object (abfd, sizeof (struct elf_obj_tdata));
322
1.76M
}
323
324
bool
325
bfd_elf_mkcorefile (bfd *abfd)
326
1.04M
{
327
  /* I think this can be done just like an object file.  */
328
1.04M
  if (!abfd->xvec->_bfd_set_format[(int) bfd_object] (abfd))
329
0
    return false;
330
1.04M
  elf_tdata (abfd)->core = bfd_zalloc (abfd, sizeof (*elf_tdata (abfd)->core));
331
1.04M
  return elf_tdata (abfd)->core != NULL;
332
1.04M
}
333
334
char *
335
bfd_elf_get_str_section (bfd *abfd, unsigned int shindex)
336
74.1k
{
337
74.1k
  Elf_Internal_Shdr **i_shdrp;
338
74.1k
  bfd_byte *shstrtab = NULL;
339
74.1k
  file_ptr offset;
340
74.1k
  bfd_size_type shstrtabsize;
341
342
74.1k
  i_shdrp = elf_elfsections (abfd);
343
74.1k
  if (i_shdrp == 0
344
74.1k
      || shindex >= elf_numsections (abfd)
345
74.1k
      || i_shdrp[shindex] == 0)
346
0
    return NULL;
347
348
74.1k
  shstrtab = i_shdrp[shindex]->contents;
349
74.1k
  if (shstrtab == NULL)
350
74.1k
    {
351
      /* No cached one, attempt to read, and cache what we read.  */
352
74.1k
      offset = i_shdrp[shindex]->sh_offset;
353
74.1k
      shstrtabsize = i_shdrp[shindex]->sh_size;
354
355
74.1k
      if (shstrtabsize == 0
356
67.6k
    || bfd_seek (abfd, offset, SEEK_SET) != 0
357
67.1k
    || (shstrtab = _bfd_mmap_persistent (abfd, shstrtabsize)) == NULL)
358
10.2k
  {
359
    /* Once we've failed to read it, make sure we don't keep
360
       trying.  Otherwise, we'll keep allocating space for
361
       the string table over and over.  */
362
10.2k
    i_shdrp[shindex]->sh_size = 0;
363
10.2k
  }
364
63.9k
      else if (shstrtab[shstrtabsize - 1] != 0)
365
18.8k
  {
366
    /* It is an error if a string table isn't terminated.  */
367
18.8k
    _bfd_error_handler
368
      /* xgettext:c-format */
369
18.8k
      (_("%pB: string table [%u] is corrupt"), abfd, shindex);
370
18.8k
    shstrtab[shstrtabsize - 1] = 0;
371
18.8k
  }
372
74.1k
      i_shdrp[shindex]->contents = shstrtab;
373
74.1k
    }
374
74.1k
  return (char *) shstrtab;
375
74.1k
}
376
377
char *
378
bfd_elf_string_from_elf_section (bfd *abfd,
379
         unsigned int shindex,
380
         unsigned int strindex)
381
1.32M
{
382
1.32M
  Elf_Internal_Shdr *hdr;
383
384
1.32M
  if (strindex == 0)
385
248k
    return "";
386
387
1.08M
  if (elf_elfsections (abfd) == NULL || shindex >= elf_numsections (abfd))
388
438
    return NULL;
389
390
1.08M
  hdr = elf_elfsections (abfd)[shindex];
391
392
1.08M
  if (hdr->contents == NULL)
393
101k
    {
394
101k
      if (hdr->sh_type != SHT_STRTAB && hdr->sh_type < SHT_LOOS)
395
27.4k
  {
396
    /* PR 17512: file: f057ec89.  */
397
    /* xgettext:c-format */
398
27.4k
    _bfd_error_handler (_("%pB: attempt to load strings from"
399
27.4k
        " a non-string section (number %d)"),
400
27.4k
            abfd, shindex);
401
27.4k
    return NULL;
402
27.4k
  }
403
404
74.1k
      if (bfd_elf_get_str_section (abfd, shindex) == NULL)
405
10.2k
  return NULL;
406
74.1k
    }
407
978k
  else
408
978k
    {
409
      /* PR 24273: The string section's contents may have already
410
   been loaded elsewhere, eg because a corrupt file has the
411
   string section index in the ELF header pointing at a group
412
   section.  So be paranoid, and test that the last byte of
413
   the section is zero.  */
414
978k
      if (hdr->sh_size == 0 || hdr->contents[hdr->sh_size - 1] != 0)
415
0
  return NULL;
416
978k
    }
417
418
1.04M
  if (strindex >= hdr->sh_size)
419
26.2k
    {
420
26.2k
      unsigned int shstrndx = elf_elfheader(abfd)->e_shstrndx;
421
26.2k
      _bfd_error_handler
422
  /* xgettext:c-format */
423
26.2k
  (_("%pB: invalid string offset %u >= %" PRIu64 " for section `%s'"),
424
26.2k
   abfd, strindex, (uint64_t) hdr->sh_size,
425
26.2k
   (shindex == shstrndx && strindex == hdr->sh_name
426
26.2k
    ? ".shstrtab"
427
26.2k
    : bfd_elf_string_from_elf_section (abfd, shstrndx, hdr->sh_name)));
428
26.2k
      return NULL;
429
26.2k
    }
430
431
1.01M
  return ((char *) hdr->contents) + strindex;
432
1.04M
}
433
434
/* Read and convert symbols to internal format.
435
   SYMCOUNT specifies the number of symbols to read, starting from
436
   symbol SYMOFFSET.  If any of INTSYM_BUF, EXTSYM_BUF or EXTSHNDX_BUF
437
   are non-NULL, they are used to store the internal symbols, external
438
   symbols, and symbol section index extensions, respectively.
439
   Returns a pointer to the internal symbol buffer (malloced if necessary)
440
   or NULL if there were no symbols or some kind of problem.  */
441
442
Elf_Internal_Sym *
443
bfd_elf_get_elf_syms (bfd *ibfd,
444
          Elf_Internal_Shdr *symtab_hdr,
445
          size_t symcount,
446
          size_t symoffset,
447
          Elf_Internal_Sym *intsym_buf,
448
          void *extsym_buf,
449
          Elf_External_Sym_Shndx *extshndx_buf)
450
18.0k
{
451
18.0k
  Elf_Internal_Shdr *shndx_hdr;
452
18.0k
  void *alloc_ext;
453
18.0k
  const bfd_byte *esym;
454
18.0k
  Elf_External_Sym_Shndx *alloc_extshndx;
455
18.0k
  Elf_External_Sym_Shndx *shndx;
456
18.0k
  Elf_Internal_Sym *alloc_intsym;
457
18.0k
  Elf_Internal_Sym *isym;
458
18.0k
  Elf_Internal_Sym *isymend;
459
18.0k
  elf_backend_data *bed;
460
18.0k
  size_t extsym_size;
461
18.0k
  size_t amt;
462
18.0k
  file_ptr pos;
463
464
18.0k
  if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
465
0
    abort ();
466
467
18.0k
  if (symcount == 0)
468
0
    return intsym_buf;
469
470
18.0k
  if (elf_use_dt_symtab_p (ibfd))
471
4
    {
472
      /* Use dynamic symbol table.  */
473
4
      if (elf_tdata (ibfd)->dt_symtab_count != symcount + symoffset)
474
0
  {
475
0
    bfd_set_error (bfd_error_invalid_operation);
476
0
    return NULL;
477
0
  }
478
4
      return elf_tdata (ibfd)->dt_symtab + symoffset;
479
4
    }
480
481
  /* Normal syms might have section extension entries.  */
482
18.0k
  shndx_hdr = NULL;
483
18.0k
  if (elf_symtab_shndx_list (ibfd) != NULL)
484
3.17k
    {
485
3.17k
      elf_section_list * entry;
486
3.17k
      Elf_Internal_Shdr **sections = elf_elfsections (ibfd);
487
488
      /* Find an index section that is linked to this symtab section.  */
489
5.38k
      for (entry = elf_symtab_shndx_list (ibfd); entry != NULL; entry = entry->next)
490
3.56k
  {
491
    /* PR 20063.  */
492
3.56k
    if (entry->hdr.sh_link >= elf_numsections (ibfd))
493
80
      continue;
494
495
3.48k
    if (sections[entry->hdr.sh_link] == symtab_hdr)
496
1.35k
      {
497
1.35k
        shndx_hdr = & entry->hdr;
498
1.35k
        break;
499
2.13k
      };
500
2.13k
  }
501
502
3.17k
      if (shndx_hdr == NULL)
503
1.81k
  {
504
1.81k
    if (symtab_hdr == &elf_symtab_hdr (ibfd))
505
      /* Not really accurate, but this was how the old code used
506
         to work.  */
507
1.81k
      shndx_hdr = &elf_symtab_shndx_list (ibfd)->hdr;
508
    /* Otherwise we do nothing.  The assumption is that
509
       the index table will not be needed.  */
510
1.81k
  }
511
3.17k
    }
512
513
  /* Read the symbols.  */
514
18.0k
  alloc_ext = NULL;
515
18.0k
  alloc_extshndx = NULL;
516
18.0k
  alloc_intsym = NULL;
517
18.0k
  bed = get_elf_backend_data (ibfd);
518
18.0k
  extsym_size = bed->s->sizeof_sym;
519
18.0k
  if (_bfd_mul_overflow (symcount, extsym_size, &amt))
520
0
    {
521
0
      bfd_set_error (bfd_error_file_too_big);
522
0
      return NULL;
523
0
    }
524
18.0k
  pos = symtab_hdr->sh_offset + symoffset * extsym_size;
525
18.0k
  size_t alloc_ext_size = amt;
526
18.0k
  if (bfd_seek (ibfd, pos, SEEK_SET) != 0
527
17.6k
      || !_bfd_mmap_read_temporary (&extsym_buf, &alloc_ext_size,
528
17.6k
            &alloc_ext, ibfd, false))
529
1.50k
    {
530
1.50k
      intsym_buf = NULL;
531
1.50k
      goto out2;
532
1.50k
    }
533
534
16.5k
  size_t alloc_extshndx_size = 0;
535
16.5k
  if (shndx_hdr == NULL || shndx_hdr->sh_size == 0)
536
14.4k
    extshndx_buf = NULL;
537
2.10k
  else
538
2.10k
    {
539
2.10k
      if (_bfd_mul_overflow (symcount, sizeof (Elf_External_Sym_Shndx), &amt))
540
0
  {
541
0
    bfd_set_error (bfd_error_file_too_big);
542
0
    intsym_buf = NULL;
543
0
    goto out1;
544
0
  }
545
2.10k
      alloc_extshndx_size = amt;
546
2.10k
      pos = shndx_hdr->sh_offset + symoffset * sizeof (Elf_External_Sym_Shndx);
547
2.10k
      if (bfd_seek (ibfd, pos, SEEK_SET) != 0
548
1.95k
    || !_bfd_mmap_read_temporary ((void **) &extshndx_buf,
549
1.95k
          &alloc_extshndx_size,
550
1.95k
          (void **) &alloc_extshndx,
551
1.95k
          ibfd, false))
552
370
  {
553
370
    intsym_buf = NULL;
554
370
    goto out1;
555
370
  }
556
2.10k
    }
557
558
16.2k
  if (intsym_buf == NULL)
559
7.54k
    {
560
7.54k
      if (_bfd_mul_overflow (symcount, sizeof (Elf_Internal_Sym), &amt))
561
0
  {
562
0
    bfd_set_error (bfd_error_file_too_big);
563
0
    goto out1;
564
0
  }
565
7.54k
      alloc_intsym = bfd_malloc (amt);
566
7.54k
      intsym_buf = alloc_intsym;
567
7.54k
      if (intsym_buf == NULL)
568
0
  goto out1;
569
7.54k
    }
570
571
  /* Convert the symbols to internal form.  */
572
16.2k
  isymend = intsym_buf + symcount;
573
16.2k
  for (esym = (const bfd_byte *) extsym_buf, isym = intsym_buf,
574
16.2k
     shndx = extshndx_buf;
575
219k
       isym < isymend;
576
202k
       esym += extsym_size, isym++, shndx = shndx != NULL ? shndx + 1 : NULL)
577
204k
    {
578
204k
      if (!(*bed->s->swap_symbol_in) (ibfd, esym, shndx, isym))
579
527
  {
580
527
    symoffset += (esym - (bfd_byte *) extsym_buf) / extsym_size;
581
    /* xgettext:c-format */
582
527
    _bfd_error_handler (_("%pB symbol number %lu references"
583
527
        " nonexistent SHT_SYMTAB_SHNDX section"),
584
527
            ibfd, (unsigned long) symoffset);
585
527
    free (alloc_intsym);
586
527
    intsym_buf = NULL;
587
527
    goto out1;
588
527
  }
589
590
      /* PR 33019: Do not accept unsupported binding values - they will
591
   likely cause problems later on.  */
592
204k
      int bind = ELF_ST_BIND (isym->st_info);
593
204k
      if (bind > STB_WEAK && bind < STB_LOOS)
594
1.04k
  { 
595
    /* xgettext:c-format */
596
1.04k
    _bfd_error_handler (_("%pB symbol number %lu uses unsupported binding of %u"),
597
1.04k
            ibfd, (unsigned long) (isym - intsym_buf), bind);
598
1.04k
    free (alloc_intsym);
599
1.04k
    intsym_buf = NULL;
600
1.04k
    goto out1;
601
1.04k
  }
602
603
      /* Paranoia: Also refuse to accept the only undefined symbol type: 7.  */
604
203k
      int t = ELF_ST_TYPE (isym->st_info);
605
203k
      if (t == 7)
606
284
  {
607
    /* xgettext:c-format */
608
284
    _bfd_error_handler (_("%pB symbol number %lu uses unsupported type of %u"),
609
284
            ibfd, (unsigned long) (isym - intsym_buf), t);
610
284
    free (alloc_intsym);
611
284
    intsym_buf = NULL;
612
284
    goto out1;
613
284
  }
614
203k
    }
615
616
16.5k
 out1:
617
16.5k
  _bfd_munmap_temporary (alloc_extshndx, alloc_extshndx_size);
618
18.0k
 out2:
619
18.0k
  _bfd_munmap_temporary (alloc_ext, alloc_ext_size);
620
621
18.0k
  return intsym_buf;
622
16.5k
}
623
624
/* Look up a symbol name.  */
625
static const char *
626
bfd_elf_sym_name_raw (bfd *abfd,
627
          Elf_Internal_Shdr *symtab_hdr,
628
          Elf_Internal_Sym *isym)
629
188k
{
630
188k
  unsigned int iname = isym->st_name;
631
188k
  unsigned int shindex = symtab_hdr->sh_link;
632
633
188k
  if (iname == 0 && ELF_ST_TYPE (isym->st_info) == STT_SECTION
634
      /* Check for a bogus st_shndx to avoid crashing.  */
635
27.8k
      && isym->st_shndx < elf_numsections (abfd))
636
27.3k
    {
637
27.3k
      iname = elf_elfsections (abfd)[isym->st_shndx]->sh_name;
638
27.3k
      shindex = elf_elfheader (abfd)->e_shstrndx;
639
27.3k
    }
640
641
188k
  return bfd_elf_string_from_elf_section (abfd, shindex, iname);
642
188k
}
643
644
const char *
645
bfd_elf_sym_name (bfd *abfd,
646
      Elf_Internal_Shdr *symtab_hdr,
647
      Elf_Internal_Sym *isym,
648
      asection *sym_sec)
649
180k
{
650
180k
  const char *name = bfd_elf_sym_name_raw (abfd, symtab_hdr, isym);
651
180k
  if (name == NULL)
652
52.8k
    name = bfd_symbol_error_name;
653
127k
  else if (sym_sec && *name == '\0')
654
0
    name = bfd_section_name (sym_sec);
655
656
180k
  return name;
657
180k
}
658
659
/* Return the name of the group signature symbol.  Why isn't the
660
   signature just a string?  */
661
662
static const char *
663
group_signature (bfd *abfd, Elf_Internal_Shdr *ghdr)
664
10.9k
{
665
10.9k
  Elf_Internal_Shdr *hdr;
666
10.9k
  unsigned char esym[sizeof (Elf64_External_Sym)];
667
10.9k
  Elf_External_Sym_Shndx eshndx;
668
10.9k
  Elf_Internal_Sym isym;
669
670
  /* First we need to ensure the symbol table is available.  Make sure
671
     that it is a symbol table section.  */
672
10.9k
  if (ghdr->sh_link >= elf_numsections (abfd))
673
48
    return NULL;
674
10.9k
  hdr = elf_elfsections (abfd) [ghdr->sh_link];
675
10.9k
  if (hdr->sh_type != SHT_SYMTAB
676
10.1k
      || ! bfd_section_from_shdr (abfd, ghdr->sh_link))
677
810
    return NULL;
678
679
  /* Go read the symbol.  */
680
10.0k
  hdr = &elf_symtab_hdr (abfd);
681
10.0k
  if (bfd_elf_get_elf_syms (abfd, hdr, 1, ghdr->sh_info,
682
10.0k
          &isym, esym, &eshndx) == NULL)
683
2.64k
    return NULL;
684
685
7.45k
  return bfd_elf_sym_name_raw (abfd, hdr, &isym);
686
10.0k
}
687
688
static bool
689
is_valid_group_section_header (Elf_Internal_Shdr *shdr, size_t minsize)
690
16.6k
{
691
16.6k
  return (shdr->sh_size >= minsize
692
16.3k
    && shdr->sh_entsize == GRP_ENTRY_SIZE
693
14.9k
    && shdr->sh_size % GRP_ENTRY_SIZE == 0
694
14.5k
    && shdr->bfd_section != NULL);
695
16.6k
}
696
697
698
/* Set next_in_group, sec_group list pointers, and group names.  */
699
700
static bool
701
process_sht_group_entries (bfd *abfd,
702
         Elf_Internal_Shdr *ghdr, unsigned int gidx)
703
14.4k
{
704
14.4k
  unsigned char *contents;
705
706
  /* Read the raw contents.  */
707
14.4k
  if (!bfd_malloc_and_get_section (abfd, ghdr->bfd_section, &contents))
708
1.02k
    {
709
1.02k
      _bfd_error_handler
710
  /* xgettext:c-format */
711
1.02k
  (_("%pB: could not read contents of group [%u]"), abfd, gidx);
712
1.02k
      return false;
713
1.02k
    }
714
715
13.4k
  asection *last_elt = NULL;
716
13.4k
  const char *gname = NULL;
717
13.4k
  unsigned char *p = contents + ghdr->sh_size;
718
531k
  while (1)
719
531k
    {
720
531k
      unsigned int idx;
721
531k
      Elf_Internal_Shdr *shdr;
722
531k
      asection *elt;
723
724
531k
      p -= 4;
725
531k
      idx = H_GET_32 (abfd, p);
726
531k
      if (p == contents)
727
8.59k
  {
728
8.59k
    if ((idx & GRP_COMDAT) != 0)
729
3.05k
      ghdr->bfd_section->flags
730
3.05k
        |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
731
8.59k
    break;
732
8.59k
  }
733
734
523k
      if (idx == 0
735
394k
    || idx >= elf_numsections (abfd)
736
67.0k
    || (shdr = elf_elfsections (abfd)[idx])->sh_type == SHT_GROUP
737
60.4k
    || ((elt = shdr->bfd_section) != NULL
738
39.8k
        && elf_sec_group (elt) != NULL
739
17.6k
        && elf_sec_group (elt) != ghdr->bfd_section))
740
466k
  {
741
466k
    _bfd_error_handler
742
466k
      (_("%pB: invalid entry (%#x) in group [%u]"),
743
466k
       abfd, idx, gidx);
744
466k
    continue;
745
466k
  }
746
747
      /* PR binutils/23199: According to the ELF gABI all sections in
748
   a group must be marked with SHF_GROUP, but some tools
749
   generate broken objects.  Fix them up here.  */
750
57.2k
      shdr->sh_flags |= SHF_GROUP;
751
752
57.2k
      if (elt == NULL)
753
20.6k
  {
754
20.6k
    if (shdr->sh_type != SHT_RELA && shdr->sh_type != SHT_REL)
755
8.20k
      {
756
8.20k
        const char *name = bfd_elf_string_from_elf_section
757
8.20k
    (abfd, elf_elfheader (abfd)->e_shstrndx, shdr->sh_name);
758
759
8.20k
        _bfd_error_handler
760
    /* xgettext:c-format */
761
8.20k
    (_("%pB: unexpected type (%#x) section `%s' in group [%u]"),
762
8.20k
     abfd, shdr->sh_type, name, gidx);
763
8.20k
      }
764
20.6k
    continue;
765
20.6k
  }
766
767
      /* Don't try to add a section to elf_next_in_group list twice.  */
768
36.6k
      if (elf_sec_group (elt) != NULL)
769
14.4k
  continue;
770
771
22.1k
      if (last_elt == NULL)
772
10.9k
  {
773
    /* Start a circular list with one element.
774
       It will be in reverse order to match what gas does.  */
775
10.9k
    elf_next_in_group (elt) = elt;
776
    /* Point the group section to it.  */
777
10.9k
    elf_next_in_group (ghdr->bfd_section) = elt;
778
10.9k
    gname = group_signature (abfd, ghdr);
779
10.9k
    if (gname == NULL)
780
4.81k
      {
781
4.81k
        free (contents);
782
4.81k
        return false;
783
4.81k
      }
784
10.9k
  }
785
11.1k
      else
786
11.1k
  {
787
11.1k
    elf_next_in_group (elt) = elf_next_in_group (last_elt);
788
11.1k
    elf_next_in_group (last_elt) = elt;
789
11.1k
  }
790
17.3k
      last_elt = elt;
791
17.3k
      elf_group_name (elt) = gname;
792
17.3k
      elf_sec_group (elt) = ghdr->bfd_section;
793
17.3k
    }
794
795
8.59k
  free (contents);
796
8.59k
  return last_elt != NULL;
797
13.4k
}
798
799
bool
800
_bfd_elf_setup_sections (bfd *abfd)
801
55.1k
{
802
55.1k
  bool result = true;
803
804
  /* Process SHF_LINK_ORDER.  */
805
528k
  for (asection *s = abfd->sections; s != NULL; s = s->next)
806
473k
    {
807
473k
      Elf_Internal_Shdr *this_hdr = &elf_section_data (s)->this_hdr;
808
473k
      if ((this_hdr->sh_flags & SHF_LINK_ORDER) != 0)
809
34.9k
  {
810
34.9k
    unsigned int elfsec = this_hdr->sh_link;
811
    /* An sh_link value of 0 is now allowed.  It indicates that linked
812
       to section has already been discarded, but that the current
813
       section has been retained for some other reason.  This linking
814
       section is still a candidate for later garbage collection
815
       however.  */
816
34.9k
    if (elfsec == 0)
817
28.5k
      {
818
28.5k
        elf_linked_to_section (s) = NULL;
819
28.5k
      }
820
6.39k
    else
821
6.39k
      {
822
6.39k
        asection *linksec = NULL;
823
824
6.39k
        if (elfsec < elf_numsections (abfd))
825
6.25k
    {
826
6.25k
      this_hdr = elf_elfsections (abfd)[elfsec];
827
6.25k
      linksec = this_hdr->bfd_section;
828
6.25k
    }
829
830
        /* PR 1991, 2008:
831
     Some strip/objcopy may leave an incorrect value in
832
     sh_link.  We don't want to proceed.  */
833
6.39k
        if (linksec == NULL)
834
1.84k
    {
835
1.84k
      _bfd_error_handler
836
        /* xgettext:c-format */
837
1.84k
        (_("%pB: sh_link [%d] in section `%pA' is incorrect"),
838
1.84k
         s->owner, elfsec, s);
839
1.84k
      result = false;
840
1.84k
    }
841
842
6.39k
        elf_linked_to_section (s) = linksec;
843
6.39k
      }
844
34.9k
  }
845
473k
    }
846
847
  /* Process section groups.  */
848
774k
  for (unsigned int i = 1; i < elf_numsections (abfd); i++)
849
719k
    {
850
719k
      Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
851
852
719k
      if (shdr && shdr->sh_type == SHT_GROUP)
853
16.6k
  {
854
16.6k
    if (is_valid_group_section_header (shdr, GRP_ENTRY_SIZE))
855
14.5k
      {
856
14.5k
        if (shdr->sh_size >= 2 * GRP_ENTRY_SIZE
857
14.4k
      && !process_sht_group_entries (abfd, shdr, i))
858
8.30k
    result = false;
859
14.5k
      }
860
2.10k
    else
861
2.10k
      {
862
        /* PR binutils/18758: Beware of corrupt binaries with
863
     invalid group data.  */
864
2.10k
        _bfd_error_handler
865
    /* xgettext:c-format */
866
2.10k
    (_("%pB: section group entry number %u is corrupt"), abfd, i);
867
2.10k
        result = false;
868
2.10k
      }
869
16.6k
  }
870
719k
    }
871
872
55.1k
  return result;
873
55.1k
}
874
875
bool
876
bfd_elf_is_group_section (bfd *abfd ATTRIBUTE_UNUSED, const asection *sec)
877
0
{
878
0
  return elf_next_in_group (sec) != NULL;
879
0
}
880
881
const char *
882
bfd_elf_group_name (bfd *abfd ATTRIBUTE_UNUSED, const asection *sec)
883
0
{
884
0
  if (elf_sec_group (sec) != NULL)
885
0
    return elf_group_name (sec);
886
0
  return NULL;
887
0
}
888
889
/* Make a BFD section from an ELF section.  We store a pointer to the
890
   BFD section in the bfd_section field of the header.  */
891
892
bool
893
_bfd_elf_make_section_from_shdr (bfd *abfd,
894
         Elf_Internal_Shdr *hdr,
895
         const char *name,
896
         int shindex)
897
676k
{
898
676k
  asection *newsect;
899
676k
  flagword flags;
900
676k
  elf_backend_data *bed;
901
676k
  unsigned int opb = bfd_octets_per_byte (abfd, NULL);
902
903
676k
  if (hdr->bfd_section != NULL)
904
96.1k
    return true;
905
906
580k
  newsect = bfd_make_section_anyway (abfd, name);
907
580k
  if (newsect == NULL)
908
0
    return false;
909
910
580k
  hdr->bfd_section = newsect;
911
580k
  elf_section_data (newsect)->this_hdr = *hdr;
912
580k
  elf_section_data (newsect)->this_idx = shindex;
913
914
  /* Always use the real type/flags.  */
915
580k
  elf_section_type (newsect) = hdr->sh_type;
916
580k
  elf_section_flags (newsect) = hdr->sh_flags;
917
918
580k
  newsect->filepos = hdr->sh_offset;
919
920
580k
  flags = SEC_NO_FLAGS;
921
580k
  if (hdr->sh_type != SHT_NOBITS)
922
564k
    flags |= SEC_HAS_CONTENTS;
923
580k
  if (hdr->sh_type == SHT_GROUP)
924
23.0k
    flags |= SEC_GROUP;
925
580k
  if ((hdr->sh_flags & SHF_ALLOC) != 0)
926
238k
    {
927
238k
      flags |= SEC_ALLOC;
928
238k
      if (hdr->sh_type != SHT_NOBITS)
929
227k
  flags |= SEC_LOAD;
930
238k
    }
931
580k
  if ((hdr->sh_flags & SHF_WRITE) == 0)
932
447k
    flags |= SEC_READONLY;
933
580k
  if ((hdr->sh_flags & SHF_EXECINSTR) != 0)
934
122k
    flags |= SEC_CODE;
935
458k
  else if ((flags & SEC_LOAD) != 0)
936
123k
    flags |= SEC_DATA;
937
580k
  if ((hdr->sh_flags & SHF_MERGE) != 0)
938
97.6k
    flags |= SEC_MERGE;
939
580k
  if ((hdr->sh_flags & SHF_STRINGS) != 0)
940
130k
    flags |= SEC_STRINGS;
941
580k
  if ((hdr->sh_flags & SHF_TLS) != 0)
942
47.4k
    flags |= SEC_THREAD_LOCAL;
943
580k
  if ((hdr->sh_flags & SHF_EXCLUDE) != 0)
944
45.4k
    flags |= SEC_EXCLUDE;
945
946
580k
  newsect->entsize = hdr->sh_entsize;
947
948
580k
  switch (elf_elfheader (abfd)->e_ident[EI_OSABI])
949
580k
    {
950
      /* FIXME: We should not recognize SHF_GNU_MBIND for ELFOSABI_NONE,
951
   but binutils as of 2019-07-23 did not set the EI_OSABI header
952
   byte.  */
953
183k
    case ELFOSABI_GNU:
954
292k
    case ELFOSABI_FREEBSD:
955
292k
      if ((hdr->sh_flags & SHF_GNU_RETAIN) != 0)
956
52.1k
  elf_tdata (abfd)->has_gnu_osabi |= elf_gnu_osabi_retain;
957
      /* Fall through */
958
464k
    case ELFOSABI_NONE:
959
464k
      if ((hdr->sh_flags & SHF_GNU_MBIND) != 0)
960
39.0k
  elf_tdata (abfd)->has_gnu_osabi |= elf_gnu_osabi_mbind;
961
464k
      break;
962
580k
    }
963
964
580k
  if ((flags & (SEC_ALLOC | SEC_GROUP)) == 0)
965
319k
    {
966
      /* The debugging sections appear to be recognized only by name,
967
   not any sort of flag.  Their SEC_ALLOC bits are cleared.  */
968
319k
      if (name [0] == '.')
969
127k
  {
970
127k
    if (startswith (name, ".debug")
971
80.9k
        || startswith (name, ".gnu.debuglto_.debug_")
972
80.5k
        || startswith (name, ".gnu.linkonce.wi.")
973
79.9k
        || startswith (name, ".zdebug"))
974
48.9k
      flags |= SEC_DEBUGGING | SEC_ELF_OCTETS;
975
78.3k
    else if (startswith (name, GNU_BUILD_ATTRS_SECTION_NAME)
976
78.1k
       || startswith (name, ".note.gnu"))
977
651
      {
978
651
        flags |= SEC_ELF_OCTETS;
979
651
        opb = 1;
980
651
      }
981
77.7k
    else if (startswith (name, ".line")
982
76.9k
       || startswith (name, ".stab")
983
75.2k
       || streq (name, ".gdb_index"))
984
3.05k
      flags |= SEC_DEBUGGING;
985
127k
  }
986
319k
    }
987
988
580k
  if (!bfd_set_section_vma (newsect, hdr->sh_addr / opb)
989
580k
      || !bfd_set_section_size (newsect, hdr->sh_size)
990
580k
      || !bfd_set_section_alignment (newsect, bfd_log2 (hdr->sh_addralign
991
580k
              & -hdr->sh_addralign)))
992
277
    return false;
993
994
  /* As a GNU extension, if the name begins with .gnu.linkonce, we
995
     only link a single copy of the section.  This is used to support
996
     g++.  g++ will emit each template expansion in its own section.
997
     The symbols will be defined as weak, so that multiple definitions
998
     are permitted.  The GNU linker extension is to actually discard
999
     all but one of the sections.  */
1000
580k
  if (startswith (name, ".gnu.linkonce")
1001
2.35k
      && elf_next_in_group (newsect) == NULL)
1002
2.35k
    flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
1003
1004
580k
  if (!bfd_set_section_flags (newsect, flags))
1005
0
    return false;
1006
1007
580k
  bed = get_elf_backend_data (abfd);
1008
580k
  if (bed->elf_backend_section_flags)
1009
145k
    if (!bed->elf_backend_section_flags (hdr))
1010
0
      return false;
1011
1012
  /* We do not parse the PT_NOTE segments as we are interested even in the
1013
     separate debug info files which may have the segments offsets corrupted.
1014
     PT_NOTEs from the core files are currently not parsed using BFD.  */
1015
580k
  if (hdr->sh_type == SHT_NOTE && hdr->sh_size != 0)
1016
8.93k
    {
1017
8.93k
      bfd_byte *contents;
1018
1019
8.93k
      if (!_bfd_elf_mmap_section_contents (abfd, newsect, &contents))
1020
1.96k
  return false;
1021
1022
6.96k
      elf_parse_notes (abfd, (char *) contents, hdr->sh_size,
1023
6.96k
           hdr->sh_offset, hdr->sh_addralign);
1024
6.96k
      _bfd_elf_munmap_section_contents (newsect, contents);
1025
6.96k
    }
1026
1027
578k
  if ((newsect->flags & SEC_ALLOC) != 0)
1028
237k
    {
1029
237k
      Elf_Internal_Phdr *phdr;
1030
237k
      unsigned int i, nload;
1031
1032
      /* Some ELF linkers produce binaries with all the program header
1033
   p_paddr fields zero.  If we have such a binary with more than
1034
   one PT_LOAD header, then leave the section lma equal to vma
1035
   so that we don't create sections with overlapping lma.  */
1036
237k
      phdr = elf_tdata (abfd)->phdr;
1037
264k
      for (nload = 0, i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
1038
89.0k
  if (phdr->p_paddr != 0)
1039
62.9k
    break;
1040
26.0k
  else if (phdr->p_type == PT_LOAD && phdr->p_memsz != 0)
1041
3.04k
    ++nload;
1042
237k
      if (i >= elf_elfheader (abfd)->e_phnum && nload > 1)
1043
182
  return true;
1044
1045
237k
      phdr = elf_tdata (abfd)->phdr;
1046
3.39M
      for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
1047
3.17M
  {
1048
3.17M
    if (((phdr->p_type == PT_LOAD
1049
108k
    && (hdr->sh_flags & SHF_TLS) == 0)
1050
3.07M
         || phdr->p_type == PT_TLS)
1051
108k
        && ELF_SECTION_IN_SEGMENT (hdr, phdr))
1052
15.0k
      {
1053
15.0k
        if ((newsect->flags & SEC_LOAD) == 0)
1054
1.00k
    newsect->lma = (phdr->p_paddr
1055
1.00k
        + hdr->sh_addr - phdr->p_vaddr) / opb;
1056
14.0k
        else
1057
    /* We used to use the same adjustment for SEC_LOAD
1058
       sections, but that doesn't work if the segment
1059
       is packed with code from multiple VMAs.
1060
       Instead we calculate the section LMA based on
1061
       the segment LMA.  It is assumed that the
1062
       segment will contain sections with contiguous
1063
       LMAs, even if the VMAs are not.  */
1064
14.0k
    newsect->lma = (phdr->p_paddr
1065
14.0k
        + hdr->sh_offset - phdr->p_offset) / opb;
1066
1067
        /* With contiguous segments, we can't tell from file
1068
     offsets whether a section with zero size should
1069
     be placed at the end of one segment or the
1070
     beginning of the next.  Decide based on vaddr.  */
1071
15.0k
        if (hdr->sh_addr >= phdr->p_vaddr
1072
15.0k
      && (hdr->sh_addr + hdr->sh_size
1073
15.0k
          <= phdr->p_vaddr + phdr->p_memsz))
1074
13.1k
    break;
1075
15.0k
      }
1076
3.17M
  }
1077
237k
    }
1078
1079
  /* Compress/decompress DWARF debug sections with names: .debug_*,
1080
     .zdebug_*, .gnu.debuglto_.debug_, after the section flags is set.  */
1081
578k
  if ((newsect->flags & SEC_DEBUGGING) != 0
1082
52.0k
      && (newsect->flags & SEC_HAS_CONTENTS) != 0
1083
51.8k
      && (newsect->flags & SEC_ELF_OCTETS) != 0)
1084
48.8k
    {
1085
48.8k
      enum { nothing, compress, decompress } action = nothing;
1086
48.8k
      int compression_header_size;
1087
48.8k
      bfd_size_type uncompressed_size;
1088
48.8k
      unsigned int uncompressed_align_power;
1089
48.8k
      enum compression_type ch_type = ch_none;
1090
48.8k
      bool compressed
1091
48.8k
  = bfd_is_section_compressed_info (abfd, newsect,
1092
48.8k
            &compression_header_size,
1093
48.8k
            &uncompressed_size,
1094
48.8k
            &uncompressed_align_power,
1095
48.8k
            &ch_type);
1096
1097
      /* Should we decompress?  */
1098
48.8k
      if ((abfd->flags & BFD_DECOMPRESS) != 0 && compressed)
1099
395
  action = decompress;
1100
1101
      /* Should we compress?  Or convert to a different compression?  */
1102
48.4k
      else if ((abfd->flags & BFD_COMPRESS) != 0
1103
2.86k
         && newsect->size != 0
1104
2.77k
         && compression_header_size >= 0
1105
2.69k
         && uncompressed_size > 0)
1106
2.69k
  {
1107
2.69k
    if (!compressed)
1108
2.64k
      action = compress;
1109
41
    else
1110
41
      {
1111
41
        enum compression_type new_ch_type = ch_none;
1112
41
        if ((abfd->flags & BFD_COMPRESS_GABI) != 0)
1113
41
    new_ch_type = ((abfd->flags & BFD_COMPRESS_ZSTD) != 0
1114
41
             ? ch_compress_zstd : ch_compress_zlib);
1115
41
        if (new_ch_type != ch_type)
1116
29
    action = compress;
1117
41
      }
1118
2.69k
  }
1119
1120
48.8k
      if (action == compress)
1121
2.67k
  {
1122
2.67k
    if (!bfd_init_section_compress_status (abfd, newsect))
1123
196
      {
1124
196
        _bfd_error_handler
1125
    /* xgettext:c-format */
1126
196
    (_("%pB: unable to compress section %s"), abfd, name);
1127
196
        return false;
1128
196
      }
1129
2.67k
  }
1130
46.1k
      else if (action == decompress)
1131
395
  {
1132
395
    if (!bfd_init_section_decompress_status (abfd, newsect))
1133
245
      {
1134
245
        _bfd_error_handler
1135
    /* xgettext:c-format */
1136
245
    (_("%pB: unable to decompress section %s"), abfd, name);
1137
245
        return false;
1138
245
      }
1139
150
#ifndef HAVE_ZSTD
1140
150
    if (newsect->compress_status == DECOMPRESS_SECTION_ZSTD)
1141
12
      {
1142
12
        _bfd_error_handler
1143
      /* xgettext:c-format */
1144
12
      (_ ("%pB: section %s is compressed with zstd, but BFD "
1145
12
          "is not built with zstd support"),
1146
12
       abfd, name);
1147
12
        newsect->compress_status = COMPRESS_SECTION_NONE;
1148
12
        return false;
1149
12
      }
1150
138
#endif
1151
138
    if (abfd->is_linker_input
1152
0
        && name[1] == 'z')
1153
0
      {
1154
        /* Rename section from .zdebug_* to .debug_* so that ld
1155
     scripts will see this section as a debug section.  */
1156
0
        char *new_name = bfd_zdebug_name_to_debug (abfd, name);
1157
0
        if (new_name == NULL)
1158
0
    return false;
1159
0
        bfd_rename_section (newsect, new_name);
1160
0
      }
1161
138
  }
1162
48.8k
    }
1163
1164
577k
  return true;
1165
578k
}
1166
1167
const char *const bfd_elf_section_type_names[] =
1168
{
1169
  "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
1170
  "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
1171
  "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
1172
};
1173
1174
/* ELF relocs are against symbols.  If we are producing relocatable
1175
   output, and the reloc is against an external symbol, and nothing
1176
   has given us any additional addend, the resulting reloc will also
1177
   be against the same symbol.  In such a case, we don't want to
1178
   change anything about the way the reloc is handled, since it will
1179
   all be done at final link time.  Rather than put special case code
1180
   into bfd_perform_relocation, all the reloc types use this howto
1181
   function, or should call this function for relocatable output.  */
1182
1183
bfd_reloc_status_type
1184
bfd_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED,
1185
           arelent *reloc_entry,
1186
           asymbol *symbol,
1187
           void *data ATTRIBUTE_UNUSED,
1188
           asection *input_section,
1189
           bfd *output_bfd,
1190
           char **error_message ATTRIBUTE_UNUSED)
1191
15.5k
{
1192
15.5k
  if (output_bfd != NULL
1193
0
      && (symbol->flags & BSF_SECTION_SYM) == 0
1194
0
      && (! reloc_entry->howto->partial_inplace
1195
0
    || reloc_entry->addend == 0))
1196
0
    {
1197
0
      reloc_entry->address += input_section->output_offset;
1198
0
      return bfd_reloc_ok;
1199
0
    }
1200
1201
  /* In some cases the relocation should be treated as output section
1202
     relative, as when linking ELF DWARF into PE COFF.  Many ELF
1203
     targets lack section relative relocations and instead use
1204
     ordinary absolute relocations for references between DWARF
1205
     sections.  That is arguably a bug in those targets but it happens
1206
     to work for the usual case of linking to non-loaded ELF debug
1207
     sections with VMAs forced to zero.  PE COFF on the other hand
1208
     doesn't allow a section VMA of zero.  */
1209
15.5k
  if (output_bfd == NULL
1210
15.5k
      && !reloc_entry->howto->pc_relative
1211
14.8k
      && (symbol->section->flags & SEC_DEBUGGING) != 0
1212
4.50k
      && (input_section->flags & SEC_DEBUGGING) != 0)
1213
4.36k
    reloc_entry->addend -= symbol->section->output_section->vma;
1214
1215
15.5k
  return bfd_reloc_continue;
1216
15.5k
}
1217

1218
/* Returns TRUE if section A matches section B.
1219
   Names, addresses and links may be different, but everything else
1220
   should be the same.  */
1221
1222
static bool
1223
section_match (const Elf_Internal_Shdr * a,
1224
         const Elf_Internal_Shdr * b)
1225
530
{
1226
530
  if (a->sh_type != b->sh_type
1227
53
      || ((a->sh_flags ^ b->sh_flags) & ~SHF_INFO_LINK) != 0
1228
35
      || a->sh_addralign != b->sh_addralign
1229
18
      || a->sh_entsize != b->sh_entsize)
1230
514
    return false;
1231
16
  if (a->sh_type == SHT_SYMTAB
1232
16
      || a->sh_type == SHT_STRTAB)
1233
14
    return true;
1234
2
  return a->sh_size == b->sh_size;
1235
16
}
1236
1237
/* Find a section in OBFD that has the same characteristics
1238
   as IHEADER.  Return the index of this section or SHN_UNDEF if
1239
   none can be found.  Check's section HINT first, as this is likely
1240
   to be the correct section.  */
1241
1242
static unsigned int
1243
find_link (const bfd *obfd, const Elf_Internal_Shdr *iheader,
1244
     const unsigned int hint)
1245
49
{
1246
49
  Elf_Internal_Shdr ** oheaders = elf_elfsections (obfd);
1247
49
  unsigned int i;
1248
1249
49
  BFD_ASSERT (iheader != NULL);
1250
1251
  /* See PR 20922 for a reproducer of the NULL test.  */
1252
49
  if (hint < elf_numsections (obfd)
1253
49
      && oheaders[hint] != NULL
1254
49
      && section_match (oheaders[hint], iheader))
1255
1
    return hint;
1256
1257
514
  for (i = 1; i < elf_numsections (obfd); i++)
1258
481
    {
1259
481
      Elf_Internal_Shdr * oheader = oheaders[i];
1260
1261
481
      if (oheader == NULL)
1262
0
  continue;
1263
481
      if (section_match (oheader, iheader))
1264
  /* FIXME: Do we care if there is a potential for
1265
     multiple matches ?  */
1266
15
  return i;
1267
481
    }
1268
1269
33
  return SHN_UNDEF;
1270
48
}
1271
1272
/* PR 19938: Attempt to set the ELF section header fields of an OS or
1273
   Processor specific section, based upon a matching input section.
1274
   Returns TRUE upon success, FALSE otherwise.  */
1275
1276
static bool
1277
copy_special_section_fields (const bfd *ibfd,
1278
           bfd *obfd,
1279
           const Elf_Internal_Shdr *iheader,
1280
           Elf_Internal_Shdr *oheader,
1281
           const unsigned int secnum)
1282
168
{
1283
168
  elf_backend_data *bed = get_elf_backend_data (obfd);
1284
168
  const Elf_Internal_Shdr **iheaders
1285
168
    = (const Elf_Internal_Shdr **) elf_elfsections (ibfd);
1286
168
  bool changed = false;
1287
168
  unsigned int sh_link;
1288
1289
168
  if (oheader->sh_type == SHT_NOBITS)
1290
20
    {
1291
      /* This is a feature for objcopy --only-keep-debug:
1292
   When a section's type is changed to NOBITS, we preserve
1293
   the sh_link and sh_info fields so that they can be
1294
   matched up with the original.
1295
1296
   Note: Strictly speaking these assignments are wrong.
1297
   The sh_link and sh_info fields should point to the
1298
   relevent sections in the output BFD, which may not be in
1299
   the same location as they were in the input BFD.  But
1300
   the whole point of this action is to preserve the
1301
   original values of the sh_link and sh_info fields, so
1302
   that they can be matched up with the section headers in
1303
   the original file.  So strictly speaking we may be
1304
   creating an invalid ELF file, but it is only for a file
1305
   that just contains debug info and only for sections
1306
   without any contents.  */
1307
20
      if (oheader->sh_link == 0)
1308
20
  oheader->sh_link = iheader->sh_link;
1309
20
      if (oheader->sh_info == 0)
1310
20
  oheader->sh_info = iheader->sh_info;
1311
20
      return true;
1312
20
    }
1313
1314
  /* Allow the target a chance to decide how these fields should be set.  */
1315
148
  if (bed->elf_backend_copy_special_section_fields (ibfd, obfd,
1316
148
                iheader, oheader))
1317
83
    return true;
1318
1319
  /* We have an iheader which might match oheader, and which has non-zero
1320
     sh_info and/or sh_link fields.  Attempt to follow those links and find
1321
     the section in the output bfd which corresponds to the linked section
1322
     in the input bfd.  */
1323
65
  if (iheader->sh_link != SHN_UNDEF)
1324
45
    {
1325
      /* See PR 20931 for a reproducer.  */
1326
45
      if (iheader->sh_link >= elf_numsections (ibfd))
1327
0
  {
1328
0
    _bfd_error_handler
1329
      /* xgettext:c-format */
1330
0
      (_("%pB: invalid sh_link field (%d) in section number %d"),
1331
0
       ibfd, iheader->sh_link, secnum);
1332
0
    return false;
1333
0
  }
1334
1335
45
      sh_link = find_link (obfd, iheaders[iheader->sh_link], iheader->sh_link);
1336
45
      if (sh_link != SHN_UNDEF)
1337
15
  {
1338
15
    oheader->sh_link = sh_link;
1339
15
    changed = true;
1340
15
  }
1341
30
      else
1342
  /* FIXME: Should we install iheader->sh_link
1343
     if we could not find a match ?  */
1344
30
  _bfd_error_handler
1345
    /* xgettext:c-format */
1346
30
    (_("%pB: failed to find link section for section %d"), obfd, secnum);
1347
45
    }
1348
1349
65
  if (iheader->sh_info)
1350
26
    {
1351
      /* The sh_info field can hold arbitrary information, but if the
1352
   SHF_LINK_INFO flag is set then it should be interpreted as a
1353
   section index.  */
1354
26
      if (iheader->sh_flags & SHF_INFO_LINK)
1355
4
  {
1356
4
    sh_link = find_link (obfd, iheaders[iheader->sh_info],
1357
4
             iheader->sh_info);
1358
4
    if (sh_link != SHN_UNDEF)
1359
1
      oheader->sh_flags |= SHF_INFO_LINK;
1360
4
  }
1361
22
      else
1362
  /* No idea what it means - just copy it.  */
1363
22
  sh_link = iheader->sh_info;
1364
1365
26
      if (sh_link != SHN_UNDEF)
1366
23
  {
1367
23
    oheader->sh_info = sh_link;
1368
23
    changed = true;
1369
23
  }
1370
3
      else
1371
3
  _bfd_error_handler
1372
    /* xgettext:c-format */
1373
3
    (_("%pB: failed to find info section for section %d"), obfd, secnum);
1374
26
    }
1375
1376
65
  return changed;
1377
65
}
1378
1379
/* Copy the program header and other data from one object module to
1380
   another.  */
1381
1382
bool
1383
_bfd_elf_copy_private_bfd_data (bfd *ibfd, bfd *obfd)
1384
544
{
1385
544
  const Elf_Internal_Shdr **iheaders;
1386
544
  Elf_Internal_Shdr **oheaders;
1387
544
  elf_backend_data *bed;
1388
544
  unsigned int i;
1389
1390
544
  if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
1391
0
    return true;
1392
1393
544
  if (!elf_flags_init (obfd))
1394
502
    {
1395
502
      elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
1396
502
      elf_flags_init (obfd) = true;
1397
502
    }
1398
1399
544
  elf_gp (obfd) = elf_gp (ibfd);
1400
1401
  /* Also copy the EI_OSABI field.  */
1402
544
  elf_elfheader (obfd)->e_ident[EI_OSABI] =
1403
544
    elf_elfheader (ibfd)->e_ident[EI_OSABI];
1404
1405
  /* If set, copy the EI_ABIVERSION field.  */
1406
544
  if (elf_elfheader (ibfd)->e_ident[EI_ABIVERSION])
1407
161
    elf_elfheader (obfd)->e_ident[EI_ABIVERSION]
1408
161
      = elf_elfheader (ibfd)->e_ident[EI_ABIVERSION];
1409
1410
  /* Copy object attributes.  */
1411
544
  _bfd_elf_copy_obj_attributes (ibfd, obfd);
1412
1413
544
  iheaders = (const Elf_Internal_Shdr **) elf_elfsections (ibfd);
1414
544
  oheaders = elf_elfsections (obfd);
1415
544
  if (iheaders == NULL || oheaders == NULL)
1416
422
    return true;
1417
1418
122
  bed = get_elf_backend_data (obfd);
1419
1420
  /* Possibly copy other fields in the section header.  */
1421
1.70k
  for (i = 1; i < elf_numsections (obfd); i++)
1422
1.58k
    {
1423
1.58k
      unsigned int j;
1424
1.58k
      Elf_Internal_Shdr * oheader = oheaders[i];
1425
1426
      /* Ignore ordinary sections.  SHT_NOBITS sections are considered however
1427
   because of a special case need for generating separate debug info
1428
   files.  See below for more details.  */
1429
1.58k
      if (oheader == NULL
1430
1.58k
    || (oheader->sh_type != SHT_NOBITS
1431
1.55k
        && oheader->sh_type < SHT_LOOS))
1432
1.38k
  continue;
1433
1434
      /* Ignore empty sections, and sections whose
1435
   fields have already been initialised.  */
1436
195
      if (oheader->sh_size == 0
1437
181
    || (oheader->sh_info != 0 && oheader->sh_link != 0))
1438
34
  continue;
1439
1440
      /* Scan for the matching section in the input bfd.
1441
   First we try for a direct mapping between the input and
1442
   output sections.  */
1443
1.86k
      for (j = 1; j < elf_numsections (ibfd); j++)
1444
1.86k
  {
1445
1.86k
    const Elf_Internal_Shdr * iheader = iheaders[j];
1446
1447
1.86k
    if (iheader == NULL)
1448
0
      continue;
1449
1450
1.86k
    if (oheader->bfd_section != NULL
1451
1.86k
        && iheader->bfd_section != NULL
1452
1.45k
        && iheader->bfd_section->output_section != NULL
1453
1.45k
        && iheader->bfd_section->output_section == oheader->bfd_section)
1454
161
      {
1455
        /* We have found a connection from the input section to
1456
     the output section.  Attempt to copy the header fields.
1457
     If this fails then do not try any further sections -
1458
     there should only be a one-to-one mapping between
1459
     input and output.  */
1460
161
        if (!copy_special_section_fields (ibfd, obfd,
1461
161
            iheader, oheader, i))
1462
26
    j = elf_numsections (ibfd);
1463
161
        break;
1464
161
      }
1465
1.86k
  }
1466
1467
161
      if (j < elf_numsections (ibfd))
1468
135
  continue;
1469
1470
      /* That failed.  So try to deduce the corresponding input section.
1471
   Unfortunately we cannot compare names as the output string table
1472
   is empty, so instead we check size, address and type.  */
1473
397
      for (j = 1; j < elf_numsections (ibfd); j++)
1474
371
  {
1475
371
    const Elf_Internal_Shdr * iheader = iheaders[j];
1476
1477
371
    if (iheader == NULL)
1478
0
      continue;
1479
1480
    /* Try matching fields in the input section's header.
1481
       Since --only-keep-debug turns all non-debug sections into
1482
       SHT_NOBITS sections, the output SHT_NOBITS type matches any
1483
       input type.  */
1484
371
    if ((oheader->sh_type == SHT_NOBITS
1485
371
         || iheader->sh_type == oheader->sh_type)
1486
27
        && (iheader->sh_flags & ~ SHF_INFO_LINK)
1487
27
        == (oheader->sh_flags & ~ SHF_INFO_LINK)
1488
27
        && iheader->sh_addralign == oheader->sh_addralign
1489
23
        && iheader->sh_entsize == oheader->sh_entsize
1490
22
        && iheader->sh_size == oheader->sh_size
1491
22
        && iheader->sh_addr == oheader->sh_addr
1492
22
        && (iheader->sh_info != oheader->sh_info
1493
21
      || iheader->sh_link != oheader->sh_link))
1494
7
      {
1495
7
        if (copy_special_section_fields (ibfd, obfd, iheader, oheader, i))
1496
0
    break;
1497
7
      }
1498
371
  }
1499
1500
26
      if (j == elf_numsections (ibfd) && oheader->sh_type >= SHT_LOOS)
1501
26
  {
1502
    /* Final attempt.  Call the backend copy function
1503
       with a NULL input section.  */
1504
26
    (void) bed->elf_backend_copy_special_section_fields (ibfd, obfd,
1505
26
                     NULL, oheader);
1506
26
  }
1507
26
    }
1508
1509
122
  return true;
1510
544
}
1511
1512
static const char *
1513
get_segment_type (unsigned int p_type)
1514
256k
{
1515
256k
  const char *pt;
1516
256k
  switch (p_type)
1517
256k
    {
1518
49.4k
    case PT_NULL: pt = "NULL"; break;
1519
3.16k
    case PT_LOAD: pt = "LOAD"; break;
1520
963
    case PT_DYNAMIC: pt = "DYNAMIC"; break;
1521
904
    case PT_INTERP: pt = "INTERP"; break;
1522
312
    case PT_NOTE: pt = "NOTE"; break;
1523
225
    case PT_SHLIB: pt = "SHLIB"; break;
1524
4.51k
    case PT_PHDR: pt = "PHDR"; break;
1525
175
    case PT_TLS: pt = "TLS"; break;
1526
167
    case PT_GNU_EH_FRAME: pt = "EH_FRAME"; break;
1527
139
    case PT_GNU_STACK: pt = "STACK"; break;
1528
111
    case PT_GNU_RELRO: pt = "RELRO"; break;
1529
0
    case PT_GNU_SFRAME: pt = "SFRAME"; break;
1530
196k
    default: pt = NULL; break;
1531
256k
    }
1532
256k
  return pt;
1533
256k
}
1534
1535
/* Print out the program headers.  */
1536
1537
bool
1538
_bfd_elf_print_private_bfd_data (bfd *abfd, void *farg)
1539
17.2k
{
1540
17.2k
  FILE *f = (FILE *) farg;
1541
17.2k
  Elf_Internal_Phdr *p;
1542
17.2k
  asection *s;
1543
17.2k
  bfd_byte *dynbuf = NULL;
1544
1545
17.2k
  p = elf_tdata (abfd)->phdr;
1546
17.2k
  if (p != NULL)
1547
12.1k
    {
1548
12.1k
      unsigned int i, c;
1549
1550
12.1k
      fprintf (f, _("\nProgram Header:\n"));
1551
12.1k
      c = elf_elfheader (abfd)->e_phnum;
1552
268k
      for (i = 0; i < c; i++, p++)
1553
256k
  {
1554
256k
    const char *pt = get_segment_type (p->p_type);
1555
256k
    char buf[20];
1556
1557
256k
    if (pt == NULL)
1558
196k
      {
1559
196k
        sprintf (buf, "0x%lx", p->p_type);
1560
196k
        pt = buf;
1561
196k
      }
1562
256k
    fprintf (f, "%8s off    0x", pt);
1563
256k
    bfd_fprintf_vma (abfd, f, p->p_offset);
1564
256k
    fprintf (f, " vaddr 0x");
1565
256k
    bfd_fprintf_vma (abfd, f, p->p_vaddr);
1566
256k
    fprintf (f, " paddr 0x");
1567
256k
    bfd_fprintf_vma (abfd, f, p->p_paddr);
1568
256k
    fprintf (f, " align 2**%u\n", bfd_log2 (p->p_align));
1569
256k
    fprintf (f, "         filesz 0x");
1570
256k
    bfd_fprintf_vma (abfd, f, p->p_filesz);
1571
256k
    fprintf (f, " memsz 0x");
1572
256k
    bfd_fprintf_vma (abfd, f, p->p_memsz);
1573
256k
    fprintf (f, " flags %c%c%c",
1574
256k
       (p->p_flags & PF_R) != 0 ? 'r' : '-',
1575
256k
       (p->p_flags & PF_W) != 0 ? 'w' : '-',
1576
256k
       (p->p_flags & PF_X) != 0 ? 'x' : '-');
1577
256k
    if ((p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)) != 0)
1578
177k
      fprintf (f, " %lx", p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X));
1579
256k
    fprintf (f, "\n");
1580
256k
  }
1581
12.1k
    }
1582
1583
17.2k
  s = bfd_get_section_by_name (abfd, ".dynamic");
1584
17.2k
  if (s != NULL && (s->flags & SEC_HAS_CONTENTS) != 0)
1585
170
    {
1586
170
      unsigned int elfsec;
1587
170
      unsigned long shlink;
1588
170
      bfd_byte *extdyn, *extdynend;
1589
170
      size_t extdynsize;
1590
170
      void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
1591
1592
170
      fprintf (f, _("\nDynamic Section:\n"));
1593
1594
170
      if (!_bfd_elf_mmap_section_contents (abfd, s, &dynbuf))
1595
11
  goto error_return;
1596
1597
159
      elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
1598
159
      if (elfsec == SHN_BAD)
1599
0
  goto error_return;
1600
159
      shlink = elf_elfsections (abfd)[elfsec]->sh_link;
1601
1602
159
      extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
1603
159
      swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
1604
1605
159
      for (extdyn = dynbuf, extdynend = dynbuf + s->size;
1606
2.47k
     (size_t) (extdynend - extdyn) >= extdynsize;
1607
2.32k
     extdyn += extdynsize)
1608
2.45k
  {
1609
2.45k
    Elf_Internal_Dyn dyn;
1610
2.45k
    const char *name = "";
1611
2.45k
    char ab[20];
1612
2.45k
    bool stringp;
1613
2.45k
    elf_backend_data *bed = get_elf_backend_data (abfd);
1614
1615
2.45k
    (*swap_dyn_in) (abfd, extdyn, &dyn);
1616
1617
2.45k
    if (dyn.d_tag == DT_NULL)
1618
132
      break;
1619
1620
2.32k
    stringp = false;
1621
2.32k
    switch (dyn.d_tag)
1622
2.32k
      {
1623
762
      default:
1624
762
        if (bed->elf_backend_get_target_dtag)
1625
86
    name = (*bed->elf_backend_get_target_dtag) (dyn.d_tag);
1626
1627
762
        if (streq (name, ""))
1628
762
    {
1629
762
      sprintf (ab, "%#" PRIx64, (uint64_t) dyn.d_tag);
1630
762
      name = ab;
1631
762
    }
1632
762
        break;
1633
1634
85
      case DT_NEEDED: name = "NEEDED"; stringp = true; break;
1635
36
      case DT_PLTRELSZ: name = "PLTRELSZ"; break;
1636
61
      case DT_PLTGOT: name = "PLTGOT"; break;
1637
57
      case DT_HASH: name = "HASH"; break;
1638
83
      case DT_STRTAB: name = "STRTAB"; break;
1639
86
      case DT_SYMTAB: name = "SYMTAB"; break;
1640
58
      case DT_RELA: name = "RELA"; break;
1641
63
      case DT_RELASZ: name = "RELASZ"; break;
1642
60
      case DT_RELAENT: name = "RELAENT"; break;
1643
74
      case DT_STRSZ: name = "STRSZ"; break;
1644
76
      case DT_SYMENT: name = "SYMENT"; break;
1645
68
      case DT_INIT: name = "INIT"; break;
1646
65
      case DT_FINI: name = "FINI"; break;
1647
2
      case DT_SONAME: name = "SONAME"; stringp = true; break;
1648
13
      case DT_RPATH: name = "RPATH"; stringp = true; break;
1649
3
      case DT_SYMBOLIC: name = "SYMBOLIC"; break;
1650
5
      case DT_REL: name = "REL"; break;
1651
5
      case DT_RELSZ: name = "RELSZ"; break;
1652
5
      case DT_RELENT: name = "RELENT"; break;
1653
4
      case DT_RELR: name = "RELR"; break;
1654
1
      case DT_RELRSZ: name = "RELRSZ"; break;
1655
0
      case DT_RELRENT: name = "RELRENT"; break;
1656
39
      case DT_PLTREL: name = "PLTREL"; break;
1657
59
      case DT_DEBUG: name = "DEBUG"; break;
1658
1
      case DT_TEXTREL: name = "TEXTREL"; break;
1659
34
      case DT_JMPREL: name = "JMPREL"; break;
1660
3
      case DT_BIND_NOW: name = "BIND_NOW"; break;
1661
37
      case DT_INIT_ARRAY: name = "INIT_ARRAY"; break;
1662
35
      case DT_FINI_ARRAY: name = "FINI_ARRAY"; break;
1663
35
      case DT_INIT_ARRAYSZ: name = "INIT_ARRAYSZ"; break;
1664
34
      case DT_FINI_ARRAYSZ: name = "FINI_ARRAYSZ"; break;
1665
1
      case DT_RUNPATH: name = "RUNPATH"; stringp = true; break;
1666
30
      case DT_FLAGS: name = "FLAGS"; break;
1667
2
      case DT_PREINIT_ARRAY: name = "PREINIT_ARRAY"; break;
1668
1
      case DT_PREINIT_ARRAYSZ: name = "PREINIT_ARRAYSZ"; break;
1669
0
      case DT_CHECKSUM: name = "CHECKSUM"; break;
1670
0
      case DT_PLTPADSZ: name = "PLTPADSZ"; break;
1671
0
      case DT_MOVEENT: name = "MOVEENT"; break;
1672
0
      case DT_MOVESZ: name = "MOVESZ"; break;
1673
0
      case DT_FEATURE: name = "FEATURE"; break;
1674
0
      case DT_POSFLAG_1: name = "POSFLAG_1"; break;
1675
0
      case DT_SYMINSZ: name = "SYMINSZ"; break;
1676
1
      case DT_SYMINENT: name = "SYMINENT"; break;
1677
0
      case DT_CONFIG: name = "CONFIG"; stringp = true; break;
1678
0
      case DT_DEPAUDIT: name = "DEPAUDIT"; stringp = true; break;
1679
0
      case DT_AUDIT: name = "AUDIT"; stringp = true; break;
1680
0
      case DT_PLTPAD: name = "PLTPAD"; break;
1681
0
      case DT_MOVETAB: name = "MOVETAB"; break;
1682
0
      case DT_SYMINFO: name = "SYMINFO"; break;
1683
35
      case DT_RELACOUNT: name = "RELACOUNT"; break;
1684
4
      case DT_RELCOUNT: name = "RELCOUNT"; break;
1685
39
      case DT_FLAGS_1: name = "FLAGS_1"; break;
1686
69
      case DT_VERSYM: name = "VERSYM"; break;
1687
0
      case DT_VERDEF: name = "VERDEF"; break;
1688
2
      case DT_VERDEFNUM: name = "VERDEFNUM"; break;
1689
67
      case DT_VERNEED: name = "VERNEED"; break;
1690
61
      case DT_VERNEEDNUM: name = "VERNEEDNUM"; break;
1691
0
      case DT_AUXILIARY: name = "AUXILIARY"; stringp = true; break;
1692
0
      case DT_USED: name = "USED"; break;
1693
0
      case DT_FILTER: name = "FILTER"; stringp = true; break;
1694
64
      case DT_GNU_HASH: name = "GNU_HASH"; break;
1695
2.32k
      }
1696
1697
2.32k
    fprintf (f, "  %-20s ", name);
1698
2.32k
    if (! stringp)
1699
2.22k
      {
1700
2.22k
        fprintf (f, "0x");
1701
2.22k
        bfd_fprintf_vma (abfd, f, dyn.d_un.d_val);
1702
2.22k
      }
1703
101
    else
1704
101
      {
1705
101
        const char *string;
1706
101
        unsigned int tagv = dyn.d_un.d_val;
1707
1708
101
        string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
1709
101
        if (string == NULL)
1710
5
    goto error_return;
1711
96
        fprintf (f, "%s", string);
1712
96
      }
1713
2.32k
    fprintf (f, "\n");
1714
2.32k
  }
1715
1716
154
      _bfd_elf_munmap_section_contents (s, dynbuf);
1717
154
      dynbuf = NULL;
1718
154
    }
1719
1720
17.2k
  if ((elf_dynverdef (abfd) != 0 && elf_tdata (abfd)->verdef == NULL)
1721
17.1k
      || (elf_dynverref (abfd) != 0 && elf_tdata (abfd)->verref == NULL))
1722
161
    {
1723
161
      if (! _bfd_elf_slurp_version_tables (abfd, false))
1724
83
  return false;
1725
161
    }
1726
1727
17.1k
  if (elf_dynverdef (abfd) != 0)
1728
8
    {
1729
8
      Elf_Internal_Verdef *t;
1730
1731
8
      fprintf (f, _("\nVersion definitions:\n"));
1732
16
      for (t = elf_tdata (abfd)->verdef; t != NULL; t = t->vd_nextdef)
1733
8
  {
1734
8
    fprintf (f, "%d 0x%2.2x 0x%8.8lx %s\n", t->vd_ndx,
1735
8
       t->vd_flags, t->vd_hash,
1736
8
       t->vd_nodename ? t->vd_nodename : "<corrupt>");
1737
8
    if (t->vd_auxptr != NULL && t->vd_auxptr->vda_nextptr != NULL)
1738
0
      {
1739
0
        Elf_Internal_Verdaux *a;
1740
1741
0
        fprintf (f, "\t");
1742
0
        for (a = t->vd_auxptr->vda_nextptr;
1743
0
       a != NULL;
1744
0
       a = a->vda_nextptr)
1745
0
    fprintf (f, "%s ",
1746
0
       a->vda_nodename ? a->vda_nodename : "<corrupt>");
1747
0
        fprintf (f, "\n");
1748
0
      }
1749
8
  }
1750
8
    }
1751
1752
17.1k
  if (elf_dynverref (abfd) != 0)
1753
78
    {
1754
78
      Elf_Internal_Verneed *t;
1755
1756
78
      fprintf (f, _("\nVersion References:\n"));
1757
156
      for (t = elf_tdata (abfd)->verref; t != NULL; t = t->vn_nextref)
1758
78
  {
1759
78
    Elf_Internal_Vernaux *a;
1760
1761
78
    fprintf (f, _("  required from %s:\n"),
1762
78
       t->vn_filename ? t->vn_filename : "<corrupt>");
1763
187
    for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1764
109
      fprintf (f, "    0x%8.8lx 0x%2.2x %2.2d %s\n", a->vna_hash,
1765
109
         a->vna_flags, a->vna_other,
1766
109
         a->vna_nodename ? a->vna_nodename : "<corrupt>");
1767
78
  }
1768
78
    }
1769
1770
17.1k
  return true;
1771
1772
16
 error_return:
1773
16
  _bfd_elf_munmap_section_contents (s, dynbuf);
1774
16
  return false;
1775
17.2k
}
1776
1777
/* Find the file offset corresponding to VMA by using the program
1778
   headers.  */
1779
1780
static file_ptr
1781
offset_from_vma (Elf_Internal_Phdr *phdrs, size_t phnum, bfd_vma vma,
1782
     size_t size, size_t *max_size_p)
1783
2.61k
{
1784
2.61k
  Elf_Internal_Phdr *seg;
1785
2.61k
  size_t i;
1786
1787
9.57k
  for (seg = phdrs, i = 0; i < phnum; ++seg, ++i)
1788
9.27k
    if (seg->p_type == PT_LOAD
1789
3.75k
  && vma >= (seg->p_vaddr & -seg->p_align)
1790
3.05k
  && vma + size <= seg->p_vaddr + seg->p_filesz)
1791
2.31k
      {
1792
2.31k
  if (max_size_p)
1793
33
    *max_size_p = seg->p_vaddr + seg->p_filesz - vma;
1794
2.31k
  return vma - seg->p_vaddr + seg->p_offset;
1795
2.31k
      }
1796
1797
300
  if (max_size_p)
1798
27
    *max_size_p = 0;
1799
300
  bfd_set_error (bfd_error_invalid_operation);
1800
300
  return (file_ptr) -1;
1801
2.61k
}
1802
1803
/* Convert hash table to internal form.  */
1804
1805
static bfd_vma *
1806
get_hash_table_data (bfd *abfd, bfd_size_type number,
1807
         unsigned int ent_size, bfd_size_type filesize)
1808
381
{
1809
381
  unsigned char *e_data = NULL;
1810
381
  bfd_vma *i_data = NULL;
1811
381
  bfd_size_type size;
1812
381
  void *e_data_addr;
1813
381
  size_t e_data_size ATTRIBUTE_UNUSED;
1814
1815
381
  if (ent_size != 4 && ent_size != 8)
1816
0
    return NULL;
1817
1818
381
  if ((size_t) number != number)
1819
0
    {
1820
0
      bfd_set_error (bfd_error_file_too_big);
1821
0
      return NULL;
1822
0
    }
1823
1824
381
  size = ent_size * number;
1825
  /* Be kind to memory checkers (eg valgrind, address sanitizer) by not
1826
     attempting to allocate memory when the read is bound to fail.  */
1827
381
  if (size > filesize
1828
355
      || number >= ~(size_t) 0 / ent_size
1829
355
      || number >= ~(size_t) 0 / sizeof (*i_data))
1830
26
    {
1831
26
      bfd_set_error (bfd_error_file_too_big);
1832
26
      return NULL;
1833
26
    }
1834
1835
355
  e_data = _bfd_mmap_temporary (abfd, size, &e_data_addr, &e_data_size);
1836
355
  if (e_data == NULL)
1837
23
    return NULL;
1838
1839
332
  i_data = bfd_malloc (number * sizeof (*i_data));
1840
332
  if (i_data == NULL)
1841
0
    {
1842
0
      _bfd_munmap_temporary (e_data_addr, e_data_size);
1843
0
      return NULL;
1844
0
    }
1845
1846
332
  if (ent_size == 4)
1847
220k
    while (number--)
1848
219k
      i_data[number] = bfd_get_32 (abfd, e_data + number * ent_size);
1849
0
  else
1850
0
    while (number--)
1851
0
      i_data[number] = bfd_get_64 (abfd, e_data + number * ent_size);
1852
1853
332
  _bfd_munmap_temporary (e_data_addr, e_data_size);
1854
332
  return i_data;
1855
332
}
1856
1857
/* Address of .MIPS.xhash section.  FIXME: What is the best way to
1858
   support DT_MIPS_XHASH?  */
1859
139k
#define DT_MIPS_XHASH        0x70000036
1860
1861
/* Reconstruct dynamic symbol table from PT_DYNAMIC segment.  */
1862
1863
bool
1864
_bfd_elf_get_dynamic_symbols (bfd *abfd, Elf_Internal_Phdr *phdr,
1865
            Elf_Internal_Phdr *phdrs, size_t phnum,
1866
            bfd_size_type filesize)
1867
4.19k
{
1868
4.19k
  bfd_byte *extdyn, *extdynend;
1869
4.19k
  size_t extdynsize;
1870
4.19k
  void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
1871
4.19k
  bool (*swap_symbol_in) (bfd *, const void *, const void *,
1872
4.19k
        Elf_Internal_Sym *);
1873
4.19k
  Elf_Internal_Dyn dyn;
1874
4.19k
  bfd_vma dt_hash = 0;
1875
4.19k
  bfd_vma dt_gnu_hash = 0;
1876
4.19k
  bfd_vma dt_mips_xhash = 0;
1877
4.19k
  bfd_vma dt_strtab = 0;
1878
4.19k
  bfd_vma dt_symtab = 0;
1879
4.19k
  size_t dt_strsz = 0;
1880
4.19k
  bfd_vma dt_versym = 0;
1881
4.19k
  bfd_vma dt_verdef = 0;
1882
4.19k
  bfd_vma dt_verneed = 0;
1883
4.19k
  bfd_byte *dynbuf = NULL;
1884
4.19k
  char *strbuf = NULL;
1885
4.19k
  bfd_vma *gnubuckets = NULL;
1886
4.19k
  bfd_vma *gnuchains = NULL;
1887
4.19k
  bfd_vma *mipsxlat = NULL;
1888
4.19k
  file_ptr saved_filepos, filepos;
1889
4.19k
  bool res = false;
1890
4.19k
  size_t amt;
1891
4.19k
  bfd_byte *esymbuf = NULL, *esym;
1892
4.19k
  bfd_size_type symcount;
1893
4.19k
  Elf_Internal_Sym *isymbuf = NULL;
1894
4.19k
  Elf_Internal_Sym *isym, *isymend;
1895
4.19k
  bfd_byte *versym = NULL;
1896
4.19k
  bfd_byte *verdef = NULL;
1897
4.19k
  bfd_byte *verneed = NULL;
1898
4.19k
  size_t verdef_size = 0;
1899
4.19k
  size_t verneed_size = 0;
1900
4.19k
  size_t extsym_size;
1901
4.19k
  elf_backend_data *bed;
1902
4.19k
  void *dynbuf_addr = NULL;
1903
4.19k
  void *esymbuf_addr = NULL;
1904
4.19k
  size_t dynbuf_size = 0;
1905
4.19k
  size_t esymbuf_size = 0;
1906
1907
  /* Return TRUE if symbol table is bad.  */
1908
4.19k
  if (elf_bad_symtab (abfd))
1909
0
    return true;
1910
1911
  /* Return TRUE if DT_HASH/DT_GNU_HASH have bee processed before.  */
1912
4.19k
  if (elf_tdata (abfd)->dt_strtab != NULL)
1913
0
    return true;
1914
1915
4.19k
  bed = get_elf_backend_data (abfd);
1916
1917
  /* Save file position for elf_object_p.  */
1918
4.19k
  saved_filepos = bfd_tell (abfd);
1919
1920
4.19k
  if (bfd_seek (abfd, phdr->p_offset, SEEK_SET) != 0)
1921
0
    goto error_return;
1922
1923
4.19k
  dynbuf_size = phdr->p_filesz;
1924
4.19k
  dynbuf = _bfd_mmap_temporary (abfd, dynbuf_size, &dynbuf_addr, &dynbuf_size);
1925
4.19k
  if (dynbuf == NULL)
1926
91
    goto error_return;
1927
1928
4.10k
  extsym_size = bed->s->sizeof_sym;
1929
4.10k
  extdynsize = bed->s->sizeof_dyn;
1930
4.10k
  swap_dyn_in = bed->s->swap_dyn_in;
1931
1932
4.10k
  extdyn = dynbuf;
1933
4.10k
  if (phdr->p_filesz < extdynsize)
1934
102
    goto error_return;
1935
4.00k
  extdynend = extdyn + phdr->p_filesz;
1936
81.7k
  for (; extdyn <= (extdynend - extdynsize); extdyn += extdynsize)
1937
79.5k
    {
1938
79.5k
      swap_dyn_in (abfd, extdyn, &dyn);
1939
1940
79.5k
      if (dyn.d_tag == DT_NULL)
1941
1.50k
  break;
1942
1943
78.0k
      switch (dyn.d_tag)
1944
78.0k
  {
1945
1.16k
  case DT_HASH:
1946
1.16k
    dt_hash = dyn.d_un.d_val;
1947
1.16k
    break;
1948
1.00k
  case DT_GNU_HASH:
1949
1.00k
    if (bed->elf_machine_code != EM_MIPS
1950
823
        && bed->elf_machine_code != EM_MIPS_RS3_LE)
1951
823
      dt_gnu_hash = dyn.d_un.d_val;
1952
1.00k
    break;
1953
1.43k
  case DT_STRTAB:
1954
1.43k
    dt_strtab = dyn.d_un.d_val;
1955
1.43k
    break;
1956
1.32k
  case DT_SYMTAB:
1957
1.32k
    dt_symtab = dyn.d_un.d_val;
1958
1.32k
    break;
1959
1.28k
  case DT_STRSZ:
1960
1.28k
    dt_strsz = dyn.d_un.d_val;
1961
1.28k
    break;
1962
713
  case DT_SYMENT:
1963
713
    if (dyn.d_un.d_val != extsym_size)
1964
270
      goto error_return;
1965
443
    break;
1966
690
  case DT_VERSYM:
1967
690
    dt_versym = dyn.d_un.d_val;
1968
690
    break;
1969
406
  case DT_VERDEF:
1970
406
    dt_verdef = dyn.d_un.d_val;
1971
406
    break;
1972
402
  case DT_VERNEED:
1973
402
    dt_verneed = dyn.d_un.d_val;
1974
402
    break;
1975
69.5k
  default:
1976
69.5k
    if (dyn.d_tag == DT_MIPS_XHASH
1977
1.04k
        && (bed->elf_machine_code == EM_MIPS
1978
578
      || bed->elf_machine_code == EM_MIPS_RS3_LE))
1979
462
      {
1980
462
        dt_gnu_hash = dyn.d_un.d_val;
1981
462
        dt_mips_xhash = dyn.d_un.d_val;
1982
462
      }
1983
69.5k
    break;
1984
78.0k
  }
1985
78.0k
    }
1986
1987
  /* Check if we can reconstruct dynamic symbol table from PT_DYNAMIC
1988
     segment.  */
1989
3.73k
  if ((!dt_hash && !dt_gnu_hash)
1990
1.86k
      || !dt_strtab
1991
1.23k
      || !dt_symtab
1992
989
      || !dt_strsz)
1993
2.87k
    goto error_return;
1994
1995
  /* Get dynamic string table.  */
1996
855
  filepos = offset_from_vma (phdrs, phnum, dt_strtab, dt_strsz, NULL);
1997
855
  if (filepos == (file_ptr) -1
1998
743
      || bfd_seek (abfd, filepos, SEEK_SET) != 0)
1999
149
    goto error_return;
2000
2001
  /* Dynamic string table must be valid until ABFD is closed.  */
2002
706
  strbuf = (char *) _bfd_mmap_persistent (abfd, dt_strsz);
2003
706
  if (strbuf == NULL)
2004
19
    goto error_return;
2005
687
  if (strbuf[dt_strsz - 1] != 0)
2006
212
    {
2007
      /* It is an error if a string table is't terminated.  */
2008
212
      _bfd_error_handler
2009
  /* xgettext:c-format */
2010
212
  (_("%pB: DT_STRTAB table is corrupt"), abfd);
2011
212
      strbuf[dt_strsz - 1] = 0;
2012
212
    }
2013
2014
  /* Get the real symbol count from DT_HASH or DT_GNU_HASH.  Prefer
2015
     DT_HASH since it is simpler than DT_GNU_HASH.  */
2016
687
  if (dt_hash)
2017
261
    {
2018
261
      unsigned char nb[16];
2019
261
      unsigned int hash_ent_size;
2020
2021
261
      switch (bed->elf_machine_code)
2022
261
  {
2023
0
  case EM_ALPHA:
2024
0
  case EM_S390:
2025
0
  case EM_S390_OLD:
2026
0
    if (bed->s->elfclass == ELFCLASS64)
2027
0
      {
2028
0
        hash_ent_size = 8;
2029
0
        break;
2030
0
      }
2031
    /* FALLTHROUGH */
2032
261
  default:
2033
261
    hash_ent_size = 4;
2034
261
    break;
2035
261
  }
2036
2037
261
      filepos = offset_from_vma (phdrs, phnum, dt_hash, 2 * hash_ent_size,
2038
261
         NULL);
2039
261
      if (filepos == (file_ptr) -1
2040
244
    || bfd_seek (abfd, filepos, SEEK_SET) != 0
2041
232
    || bfd_read (nb, 2 * hash_ent_size, abfd) != 2 * hash_ent_size)
2042
43
  goto error_return;
2043
2044
      /* The number of dynamic symbol table entries equals the number
2045
   of chains.  */
2046
218
      if (hash_ent_size == 8)
2047
0
  symcount = bfd_get_64 (abfd, nb + hash_ent_size);
2048
218
      else
2049
218
  symcount = bfd_get_32 (abfd, nb + hash_ent_size);
2050
218
    }
2051
426
  else
2052
426
    {
2053
      /* For DT_GNU_HASH, only defined symbols with non-STB_LOCAL
2054
   bindings are in hash table.  Since in dynamic symbol table,
2055
   all symbols with STB_LOCAL binding are placed before symbols
2056
   with other bindings and all undefined symbols are placed
2057
   before defined ones, the highest symbol index in DT_GNU_HASH
2058
   is the highest dynamic symbol table index.  */
2059
426
      unsigned char nb[16];
2060
426
      bfd_vma ngnubuckets;
2061
426
      bfd_vma gnusymidx;
2062
426
      size_t i, ngnuchains;
2063
426
      bfd_vma maxchain = 0xffffffff, bitmaskwords;
2064
426
      bfd_vma buckets_vma;
2065
2066
426
      filepos = offset_from_vma (phdrs, phnum, dt_gnu_hash,
2067
426
         sizeof (nb), NULL);
2068
426
      if (filepos == (file_ptr) -1
2069
404
    || bfd_seek (abfd, filepos, SEEK_SET) != 0
2070
381
    || bfd_read (nb, sizeof (nb), abfd) != sizeof (nb))
2071
48
  goto error_return;
2072
2073
378
      ngnubuckets = bfd_get_32 (abfd, nb);
2074
378
      gnusymidx = bfd_get_32 (abfd, nb + 4);
2075
378
      bitmaskwords = bfd_get_32 (abfd, nb + 8);
2076
378
      buckets_vma = dt_gnu_hash + 16;
2077
378
      if (bed->s->elfclass == ELFCLASS32)
2078
0
  buckets_vma += bitmaskwords * 4;
2079
378
      else
2080
378
  buckets_vma += bitmaskwords * 8;
2081
378
      filepos = offset_from_vma (phdrs, phnum, buckets_vma, 4, NULL);
2082
378
      if (filepos == (file_ptr) -1
2083
355
    || bfd_seek (abfd, filepos, SEEK_SET) != 0)
2084
54
  goto error_return;
2085
2086
324
      gnubuckets = get_hash_table_data (abfd, ngnubuckets, 4, filesize);
2087
324
      if (gnubuckets == NULL)
2088
49
  goto error_return;
2089
2090
197k
      for (i = 0; i < ngnubuckets; i++)
2091
197k
  if (gnubuckets[i] != 0)
2092
153k
    {
2093
153k
      if (gnubuckets[i] < gnusymidx)
2094
24
        goto error_return;
2095
2096
153k
      if (maxchain == 0xffffffff || gnubuckets[i] > maxchain)
2097
16.9k
        maxchain = gnubuckets[i];
2098
153k
    }
2099
2100
251
      if (maxchain == 0xffffffff)
2101
49
  {
2102
49
    symcount = 0;
2103
49
    goto empty_gnu_hash;
2104
49
  }
2105
2106
202
      maxchain -= gnusymidx;
2107
202
      filepos = offset_from_vma (phdrs, phnum,
2108
202
         buckets_vma + 4 * (ngnubuckets + maxchain),
2109
202
         4, NULL);
2110
202
      if (filepos == (file_ptr) -1
2111
159
    || bfd_seek (abfd, filepos, SEEK_SET) != 0)
2112
134
  goto error_return;
2113
2114
68
      do
2115
1.07k
  {
2116
1.07k
    if (bfd_read (nb, 4, abfd) != 4)
2117
11
      goto error_return;
2118
1.06k
    ++maxchain;
2119
1.06k
    if (maxchain == 0)
2120
0
      goto error_return;
2121
1.06k
  }
2122
1.06k
      while ((bfd_get_32 (abfd, nb) & 1) == 0);
2123
2124
57
      filepos = offset_from_vma (phdrs, phnum,
2125
57
         buckets_vma + 4 * ngnubuckets,
2126
57
         4, NULL);
2127
57
      if (filepos == (file_ptr) -1
2128
57
    || bfd_seek (abfd, filepos, SEEK_SET) != 0)
2129
0
  goto error_return;
2130
2131
57
      gnuchains = get_hash_table_data (abfd, maxchain, 4, filesize);
2132
57
      if (gnuchains == NULL)
2133
0
  goto error_return;
2134
57
      ngnuchains = maxchain;
2135
2136
57
      if (dt_mips_xhash)
2137
0
  {
2138
0
    filepos = offset_from_vma (phdrs, phnum,
2139
0
             buckets_vma + 4 * (ngnubuckets + maxchain),
2140
0
             4, NULL);
2141
0
    if (filepos == (file_ptr) -1
2142
0
        || bfd_seek (abfd, filepos, SEEK_SET) != 0)
2143
0
      goto error_return;
2144
2145
0
    mipsxlat = get_hash_table_data (abfd, maxchain, 4, filesize);
2146
0
    if (mipsxlat == NULL)
2147
0
      goto error_return;
2148
0
  }
2149
2150
57
      symcount = 0;
2151
1.57k
      for (i = 0; i < ngnubuckets; ++i)
2152
1.51k
  if (gnubuckets[i] != 0)
2153
860
    {
2154
860
      bfd_vma si = gnubuckets[i];
2155
860
      bfd_vma off = si - gnusymidx;
2156
860
      do
2157
2.84k
        {
2158
2.84k
    if (mipsxlat)
2159
0
      {
2160
0
        if (mipsxlat[off] >= symcount)
2161
0
          symcount = mipsxlat[off] + 1;
2162
0
      }
2163
2.84k
    else
2164
2.84k
      {
2165
2.84k
        if (si >= symcount)
2166
915
          symcount = si + 1;
2167
2.84k
      }
2168
2.84k
    si++;
2169
2.84k
        }
2170
2.84k
      while (off < ngnuchains && (gnuchains[off++] & 1) == 0);
2171
860
    }
2172
57
    }
2173
2174
  /* Swap in dynamic symbol table.  */
2175
275
  if (_bfd_mul_overflow (symcount, extsym_size, &amt))
2176
0
    {
2177
0
      bfd_set_error (bfd_error_file_too_big);
2178
0
      goto error_return;
2179
0
    }
2180
2181
275
  filepos = offset_from_vma (phdrs, phnum, dt_symtab, amt, NULL);
2182
275
  if (filepos == (file_ptr) -1
2183
236
      || bfd_seek (abfd, filepos, SEEK_SET) != 0)
2184
65
    goto error_return;
2185
210
  esymbuf_size = amt;
2186
210
  esymbuf = _bfd_mmap_temporary (abfd, esymbuf_size,
2187
210
         &esymbuf_addr, &esymbuf_size);
2188
210
  if (esymbuf == NULL)
2189
29
    goto error_return;
2190
2191
181
  if (_bfd_mul_overflow (symcount, sizeof (Elf_Internal_Sym), &amt))
2192
0
    {
2193
0
      bfd_set_error (bfd_error_file_too_big);
2194
0
      goto error_return;
2195
0
    }
2196
2197
  /* Dynamic symbol table must be valid until ABFD is closed.  */
2198
181
  isymbuf = bfd_alloc (abfd, amt);
2199
181
  if (isymbuf == NULL)
2200
0
    goto error_return;
2201
2202
181
  swap_symbol_in = bed->s->swap_symbol_in;
2203
2204
  /* Convert the symbols to internal form.  */
2205
181
  isymend = isymbuf + symcount;
2206
181
  for (esym = esymbuf, isym = isymbuf;
2207
594
       isym < isymend;
2208
413
       esym += extsym_size, isym++)
2209
457
    if (!swap_symbol_in (abfd, esym, NULL, isym)
2210
442
  || isym->st_name >= dt_strsz)
2211
44
      {
2212
44
  bfd_set_error (bfd_error_invalid_operation);
2213
44
  goto error_return;
2214
44
      }
2215
2216
137
  if (dt_versym)
2217
103
    {
2218
      /* Swap in DT_VERSYM.  */
2219
103
      if (_bfd_mul_overflow (symcount, 2, &amt))
2220
0
  {
2221
0
    bfd_set_error (bfd_error_file_too_big);
2222
0
    goto error_return;
2223
0
  }
2224
2225
103
      filepos = offset_from_vma (phdrs, phnum, dt_versym, amt, NULL);
2226
103
      if (filepos == (file_ptr) -1
2227
86
    || bfd_seek (abfd, filepos, SEEK_SET) != 0)
2228
30
  goto error_return;
2229
2230
      /* DT_VERSYM info must be valid until ABFD is closed.  */
2231
73
      versym = _bfd_mmap_persistent (abfd, amt);
2232
2233
73
      if (dt_verdef)
2234
28
  {
2235
    /* Read in DT_VERDEF.  */
2236
28
    filepos = offset_from_vma (phdrs, phnum, dt_verdef,
2237
28
             0, &verdef_size);
2238
28
    if (filepos == (file_ptr) -1
2239
12
        || bfd_seek (abfd, filepos, SEEK_SET) != 0)
2240
26
      goto error_return;
2241
2242
    /* DT_VERDEF info must be valid until ABFD is closed.  */
2243
2
    verdef = _bfd_mmap_persistent (abfd, verdef_size);
2244
2
  }
2245
2246
47
      if (dt_verneed)
2247
32
  {
2248
    /* Read in DT_VERNEED.  */
2249
32
    filepos = offset_from_vma (phdrs, phnum, dt_verneed,
2250
32
             0, &verneed_size);
2251
32
    if (filepos == (file_ptr) -1
2252
21
        || bfd_seek (abfd, filepos, SEEK_SET) != 0)
2253
14
      goto error_return;
2254
2255
    /* DT_VERNEED info must be valid until ABFD is closed.  */
2256
18
    verneed = _bfd_mmap_persistent (abfd, verneed_size);
2257
18
  }
2258
47
    }
2259
2260
116
 empty_gnu_hash:
2261
116
  elf_tdata (abfd)->dt_strtab = strbuf;
2262
116
  elf_tdata (abfd)->dt_strsz = dt_strsz;
2263
116
  elf_tdata (abfd)->dt_symtab = isymbuf;
2264
116
  elf_tdata (abfd)->dt_symtab_count = symcount;
2265
116
  elf_tdata (abfd)->dt_versym = versym;
2266
116
  elf_tdata (abfd)->dt_verdef = verdef;
2267
116
  elf_tdata (abfd)->dt_verneed = verneed;
2268
116
  elf_tdata (abfd)->dt_verdef_count
2269
116
    = verdef_size / sizeof (Elf_External_Verdef);
2270
116
  elf_tdata (abfd)->dt_verneed_count
2271
116
    = verneed_size / sizeof (Elf_External_Verneed);
2272
2273
116
  res = true;
2274
2275
4.19k
 error_return:
2276
  /* Restore file position for elf_object_p.  */
2277
4.19k
  if (bfd_seek (abfd, saved_filepos, SEEK_SET) != 0)
2278
0
    res = false;
2279
4.19k
  _bfd_munmap_temporary (dynbuf_addr, dynbuf_size);
2280
4.19k
  _bfd_munmap_temporary (esymbuf_addr, esymbuf_size);
2281
4.19k
  free (gnubuckets);
2282
4.19k
  free (gnuchains);
2283
4.19k
  free (mipsxlat);
2284
4.19k
  return res;
2285
116
}
2286
2287
/* Reconstruct section from dynamic symbol.  */
2288
2289
asection *
2290
_bfd_elf_get_section_from_dynamic_symbol (bfd *abfd,
2291
            Elf_Internal_Sym *isym)
2292
1
{
2293
1
  asection *sec;
2294
1
  flagword flags;
2295
2296
1
  if (!elf_use_dt_symtab_p (abfd))
2297
0
    return NULL;
2298
2299
1
  flags = SEC_ALLOC | SEC_LOAD;
2300
1
  switch (ELF_ST_TYPE (isym->st_info))
2301
1
    {
2302
0
    case STT_FUNC:
2303
0
    case STT_GNU_IFUNC:
2304
0
      sec = bfd_get_section_by_name (abfd, ".text");
2305
0
      if (sec == NULL)
2306
0
  sec = bfd_make_section_with_flags (abfd,
2307
0
             ".text",
2308
0
             flags | SEC_CODE);
2309
0
      break;
2310
0
    case STT_COMMON:
2311
0
      sec = bfd_com_section_ptr;
2312
0
      break;
2313
0
    case STT_OBJECT:
2314
0
      sec = bfd_get_section_by_name (abfd, ".data");
2315
0
      if (sec == NULL)
2316
0
  sec = bfd_make_section_with_flags (abfd,
2317
0
             ".data",
2318
0
             flags | SEC_DATA);
2319
0
      break;
2320
0
    case STT_TLS:
2321
0
      sec = bfd_get_section_by_name (abfd, ".tdata");
2322
0
      if (sec == NULL)
2323
0
  sec = bfd_make_section_with_flags (abfd,
2324
0
             ".tdata",
2325
0
             (flags
2326
0
              | SEC_DATA
2327
0
              | SEC_THREAD_LOCAL));
2328
0
      break;
2329
1
    default:
2330
1
      sec = bfd_abs_section_ptr;
2331
1
      break;
2332
1
    }
2333
2334
1
  return sec;
2335
1
}
2336
2337
/* Get version name.  If BASE_P is TRUE, return "Base" for VER_FLG_BASE
2338
   and return symbol version for symbol version itself.   */
2339
2340
const char *
2341
_bfd_elf_get_symbol_version_string (bfd *abfd, asymbol *symbol,
2342
            bool base_p,
2343
            bool *hidden)
2344
52.8k
{
2345
52.8k
  const char *version_string = NULL;
2346
52.8k
  if ((elf_dynversym (abfd) != 0
2347
5.22k
       && (elf_dynverdef (abfd) != 0 || elf_dynverref (abfd) != 0))
2348
47.6k
      || (elf_tdata (abfd)->dt_versym != NULL
2349
0
    && (elf_tdata (abfd)->dt_verdef != NULL
2350
0
        || elf_tdata (abfd)->dt_verneed != NULL)))
2351
5.18k
    {
2352
5.18k
      unsigned int vernum = ((elf_symbol_type *) symbol)->version;
2353
2354
5.18k
      *hidden = (vernum & VERSYM_HIDDEN) != 0;
2355
5.18k
      vernum &= VERSYM_VERSION;
2356
2357
5.18k
      if (vernum == 0)
2358
1.73k
  version_string = "";
2359
3.45k
      else if (vernum == 1
2360
2.53k
         && (vernum > elf_tdata (abfd)->cverdefs
2361
0
       || (elf_tdata (abfd)->verdef[0].vd_flags
2362
0
           == VER_FLG_BASE)))
2363
2.53k
  version_string = base_p ? "Base" : "";
2364
918
      else if (vernum <= elf_tdata (abfd)->cverdefs)
2365
0
  {
2366
0
    const char *nodename
2367
0
      = elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
2368
0
    version_string = "";
2369
0
    if (base_p
2370
0
        || nodename == NULL
2371
0
        || symbol->name == NULL
2372
0
        || ! streq (symbol->name, nodename))
2373
0
      version_string = nodename;
2374
0
  }
2375
918
      else
2376
918
  {
2377
918
    Elf_Internal_Verneed *t;
2378
2379
918
    version_string = _("<corrupt>");
2380
918
    for (t = elf_tdata (abfd)->verref;
2381
1.83k
         t != NULL;
2382
918
         t = t->vn_nextref)
2383
918
      {
2384
918
        Elf_Internal_Vernaux *a;
2385
2386
1.97k
        for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2387
1.72k
    {
2388
1.72k
      if (a->vna_other == vernum)
2389
670
        {
2390
670
          *hidden = true;
2391
670
          version_string = a->vna_nodename;
2392
670
          break;
2393
670
        }
2394
1.72k
    }
2395
918
      }
2396
918
  }
2397
5.18k
    }
2398
52.8k
  return version_string;
2399
52.8k
}
2400
2401
/* Display ELF-specific fields of a symbol.  */
2402
2403
void
2404
_bfd_elf_print_symbol (bfd *abfd,
2405
           void *filep,
2406
           asymbol *symbol,
2407
           bfd_print_symbol_type how)
2408
0
{
2409
0
  FILE *file = (FILE *) filep;
2410
0
  const char *symname = (symbol->name != bfd_symbol_error_name
2411
0
       ? symbol->name : _("<corrupt>"));
2412
2413
0
  switch (how)
2414
0
    {
2415
0
    case bfd_print_symbol_name:
2416
0
      fprintf (file, "%s", symname);
2417
0
      break;
2418
0
    case bfd_print_symbol_more:
2419
0
      fprintf (file, "elf ");
2420
0
      bfd_fprintf_vma (abfd, file, symbol->value);
2421
0
      fprintf (file, " %x", symbol->flags);
2422
0
      break;
2423
0
    case bfd_print_symbol_all:
2424
0
      {
2425
0
  const char *section_name;
2426
0
  const char *name = NULL;
2427
0
  elf_backend_data *bed;
2428
0
  unsigned char st_other;
2429
0
  bfd_vma val;
2430
0
  const char *version_string;
2431
0
  bool hidden;
2432
2433
0
  section_name = symbol->section ? symbol->section->name : "(*none*)";
2434
2435
0
  bed = get_elf_backend_data (abfd);
2436
0
  if (bed->elf_backend_print_symbol_all)
2437
0
    name = (*bed->elf_backend_print_symbol_all) (abfd, filep, symbol);
2438
2439
0
  if (name != NULL)
2440
0
    symname = name;
2441
0
  else
2442
0
    bfd_print_symbol_vandf (abfd, file, symbol);
2443
2444
0
  fprintf (file, " %s\t", section_name);
2445
  /* Print the "other" value for a symbol.  For common symbols,
2446
     we've already printed the size; now print the alignment.
2447
     For other symbols, we have no specified alignment, and
2448
     we've printed the address; now print the size.  */
2449
0
  if (symbol->section && bfd_is_com_section (symbol->section))
2450
0
    val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_value;
2451
0
  else
2452
0
    val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_size;
2453
0
  bfd_fprintf_vma (abfd, file, val);
2454
2455
  /* If we have version information, print it.  */
2456
0
  version_string = _bfd_elf_get_symbol_version_string (abfd,
2457
0
                   symbol,
2458
0
                   true,
2459
0
                   &hidden);
2460
0
  if (version_string)
2461
0
    {
2462
0
      if (!hidden)
2463
0
        fprintf (file, "  %-11s", version_string);
2464
0
      else
2465
0
        {
2466
0
    int i;
2467
2468
0
    fprintf (file, " (%s)", version_string);
2469
0
    for (i = 10 - strlen (version_string); i > 0; --i)
2470
0
      putc (' ', file);
2471
0
        }
2472
0
    }
2473
2474
  /* If the st_other field is not zero, print it.  */
2475
0
  st_other = ((elf_symbol_type *) symbol)->internal_elf_sym.st_other;
2476
2477
0
  switch (st_other)
2478
0
    {
2479
0
    case 0: break;
2480
0
    case STV_INTERNAL:  fprintf (file, " .internal");  break;
2481
0
    case STV_HIDDEN:    fprintf (file, " .hidden");    break;
2482
0
    case STV_PROTECTED: fprintf (file, " .protected"); break;
2483
0
    default:
2484
      /* Some other non-defined flags are also present, so print
2485
         everything hex.  */
2486
0
      fprintf (file, " 0x%02x", (unsigned int) st_other);
2487
0
    }
2488
2489
0
  fprintf (file, " %s", symname);
2490
0
      }
2491
0
      break;
2492
0
    }
2493
0
}
2494

2495
/* ELF .o/exec file reading */
2496
2497
/* Create a new bfd section from an ELF section header.  */
2498
2499
bool
2500
bfd_section_from_shdr (bfd *abfd, unsigned int shindex)
2501
1.10M
{
2502
1.10M
  Elf_Internal_Shdr *hdr;
2503
1.10M
  Elf_Internal_Ehdr *ehdr;
2504
1.10M
  elf_backend_data *bed;
2505
1.10M
  const char *name;
2506
1.10M
  bool ret = true;
2507
2508
1.10M
  if (shindex >= elf_numsections (abfd))
2509
0
    return false;
2510
2511
  /* PR17512: A corrupt ELF binary might contain a loop of sections via
2512
     sh_link or sh_info.  Detect this here, by refusing to load a
2513
     section that we are already in the process of loading.  */
2514
1.10M
  if (elf_tdata (abfd)->being_created[shindex])
2515
25
    {
2516
25
      _bfd_error_handler
2517
25
  (_("%pB: warning: loop in section dependencies detected"), abfd);
2518
25
      return false;
2519
25
    }
2520
1.10M
  elf_tdata (abfd)->being_created[shindex] = true;
2521
2522
1.10M
  hdr = elf_elfsections (abfd)[shindex];
2523
1.10M
  ehdr = elf_elfheader (abfd);
2524
1.10M
  name = bfd_elf_string_from_elf_section (abfd, ehdr->e_shstrndx,
2525
1.10M
            hdr->sh_name);
2526
1.10M
  if (name == NULL)
2527
9.71k
    goto fail;
2528
2529
1.09M
  bed = get_elf_backend_data (abfd);
2530
1.09M
  switch (hdr->sh_type)
2531
1.09M
    {
2532
91.4k
    case SHT_NULL:
2533
      /* Inactive section. Throw it away.  */
2534
91.4k
      goto success;
2535
2536
319k
    case SHT_PROGBITS:   /* Normal section with contents.  */
2537
335k
    case SHT_NOBITS:   /* .bss section.  */
2538
337k
    case SHT_HASH:   /* .hash section.  */
2539
347k
    case SHT_NOTE:   /* .note section.  */
2540
366k
    case SHT_INIT_ARRAY: /* .init_array section.  */
2541
377k
    case SHT_FINI_ARRAY: /* .fini_array section.  */
2542
379k
    case SHT_PREINIT_ARRAY: /* .preinit_array section.  */
2543
379k
    case SHT_GNU_LIBLIST: /* .gnu.liblist section.  */
2544
380k
    case SHT_GNU_HASH:   /* .gnu.hash section.  */
2545
380k
    case SHT_GNU_SFRAME: /* .sframe section.  */
2546
380k
      ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2547
380k
      goto success;
2548
2549
3.20k
    case SHT_DYNAMIC: /* Dynamic linking information.  */
2550
3.20k
      if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
2551
55
  goto fail;
2552
2553
3.14k
      if (hdr->sh_link > elf_numsections (abfd))
2554
184
  {
2555
    /* PR 10478: Accept Solaris binaries with a sh_link field
2556
       set to SHN_BEFORE (LORESERVE) or SHN_AFTER (LORESERVE+1).  */
2557
184
    switch (bfd_get_arch (abfd))
2558
184
      {
2559
131
      case bfd_arch_i386:
2560
184
      case bfd_arch_sparc:
2561
184
        if (hdr->sh_link == (SHN_LORESERVE & 0xffff)
2562
43
      || hdr->sh_link == ((SHN_LORESERVE + 1) & 0xffff))
2563
184
    break;
2564
        /* Otherwise fall through.  */
2565
0
      default:
2566
0
        goto fail;
2567
184
      }
2568
184
  }
2569
2.96k
      else if (elf_elfsections (abfd)[hdr->sh_link] == NULL)
2570
0
  goto fail;
2571
2.96k
      else if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_STRTAB)
2572
1.62k
  {
2573
1.62k
    Elf_Internal_Shdr *dynsymhdr;
2574
2575
    /* The shared libraries distributed with hpux11 have a bogus
2576
       sh_link field for the ".dynamic" section.  Find the
2577
       string table for the ".dynsym" section instead.  */
2578
1.62k
    if (elf_dynsymtab (abfd) != 0)
2579
306
      {
2580
306
        dynsymhdr = elf_elfsections (abfd)[elf_dynsymtab (abfd)];
2581
306
        hdr->sh_link = dynsymhdr->sh_link;
2582
306
      }
2583
1.32k
    else
2584
1.32k
      {
2585
1.32k
        unsigned int i, num_sec;
2586
2587
1.32k
        num_sec = elf_numsections (abfd);
2588
21.8k
        for (i = 1; i < num_sec; i++)
2589
20.7k
    {
2590
20.7k
      dynsymhdr = elf_elfsections (abfd)[i];
2591
20.7k
      if (dynsymhdr->sh_type == SHT_DYNSYM)
2592
169
        {
2593
169
          hdr->sh_link = dynsymhdr->sh_link;
2594
169
          break;
2595
169
        }
2596
20.7k
    }
2597
1.32k
      }
2598
1.62k
  }
2599
3.14k
      goto success;
2600
2601
137k
    case SHT_SYMTAB:   /* A symbol table.  */
2602
137k
      if (elf_onesymtab (abfd) == shindex)
2603
106k
  goto success;
2604
2605
30.3k
      if (hdr->sh_entsize != bed->s->sizeof_sym)
2606
751
  goto fail;
2607
2608
29.6k
      if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size)
2609
1.29k
  {
2610
1.29k
    if (hdr->sh_size != 0)
2611
235
      goto fail;
2612
    /* Some assemblers erroneously set sh_info to one with a
2613
       zero sh_size.  ld sees this as a global symbol count
2614
       of (unsigned) -1.  Fix it here.  */
2615
1.05k
    hdr->sh_info = 0;
2616
1.05k
    goto success;
2617
1.29k
  }
2618
2619
      /* PR 18854: A binary might contain more than one symbol table.
2620
   Unusual, but possible.  Warn, but continue.  */
2621
28.3k
      if (elf_onesymtab (abfd) != 0)
2622
3.39k
  {
2623
3.39k
    _bfd_error_handler
2624
      /* xgettext:c-format */
2625
3.39k
      (_("%pB: warning: multiple symbol tables detected"
2626
3.39k
         " - ignoring the table in section %u"),
2627
3.39k
       abfd, shindex);
2628
3.39k
    goto success;
2629
3.39k
  }
2630
24.9k
      elf_onesymtab (abfd) = shindex;
2631
24.9k
      elf_symtab_hdr (abfd) = *hdr;
2632
24.9k
      elf_elfsections (abfd)[shindex] = hdr = & elf_symtab_hdr (abfd);
2633
24.9k
      abfd->flags |= HAS_SYMS;
2634
2635
      /* Sometimes a shared object will map in the symbol table.  If
2636
   SHF_ALLOC is set, and this is a shared object, then we also
2637
   treat this section as a BFD section.  We can not base the
2638
   decision purely on SHF_ALLOC, because that flag is sometimes
2639
   set in a relocatable object file, which would confuse the
2640
   linker.  */
2641
24.9k
      if ((hdr->sh_flags & SHF_ALLOC) != 0
2642
1.13k
    && (abfd->flags & DYNAMIC) != 0
2643
270
    && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2644
270
            shindex))
2645
4
  goto fail;
2646
2647
      /* Go looking for SHT_SYMTAB_SHNDX too, since if there is one we
2648
   can't read symbols without that section loaded as well.  It
2649
   is most likely specified by the next section header.  */
2650
24.9k
      {
2651
24.9k
  elf_section_list * entry;
2652
24.9k
  unsigned int i, num_sec;
2653
2654
27.1k
  for (entry = elf_symtab_shndx_list (abfd); entry; entry = entry->next)
2655
5.25k
    if (entry->hdr.sh_link == shindex)
2656
3.04k
      goto success;
2657
2658
21.8k
  num_sec = elf_numsections (abfd);
2659
63.9k
  for (i = shindex + 1; i < num_sec; i++)
2660
42.1k
    {
2661
42.1k
      Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
2662
2663
42.1k
      if (hdr2->sh_type == SHT_SYMTAB_SHNDX
2664
652
    && hdr2->sh_link == shindex)
2665
73
        break;
2666
42.1k
    }
2667
2668
21.8k
  if (i == num_sec)
2669
459k
    for (i = 1; i < shindex; i++)
2670
437k
      {
2671
437k
        Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
2672
2673
437k
        if (hdr2->sh_type == SHT_SYMTAB_SHNDX
2674
3.39k
      && hdr2->sh_link == shindex)
2675
443
    break;
2676
437k
      }
2677
2678
21.8k
  if (i != shindex)
2679
516
    ret = bfd_section_from_shdr (abfd, i);
2680
  /* else FIXME: we have failed to find the symbol table.
2681
     Should we issue an error?  */
2682
21.8k
  goto success;
2683
24.9k
      }
2684
2685
4.05k
    case SHT_DYNSYM:   /* A dynamic symbol table.  */
2686
4.05k
      if (elf_dynsymtab (abfd) == shindex)
2687
1.61k
  goto success;
2688
2689
2.43k
      if (hdr->sh_entsize != bed->s->sizeof_sym)
2690
393
  goto fail;
2691
2692
2.04k
      if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size)
2693
289
  {
2694
289
    if (hdr->sh_size != 0)
2695
125
      goto fail;
2696
2697
    /* Some linkers erroneously set sh_info to one with a
2698
       zero sh_size.  ld sees this as a global symbol count
2699
       of (unsigned) -1.  Fix it here.  */
2700
164
    hdr->sh_info = 0;
2701
164
    goto success;
2702
289
  }
2703
2704
      /* PR 18854: A binary might contain more than one dynamic symbol table.
2705
   Unusual, but possible.  Warn, but continue.  */
2706
1.75k
      if (elf_dynsymtab (abfd) != 0)
2707
139
  {
2708
139
    _bfd_error_handler
2709
      /* xgettext:c-format */
2710
139
      (_("%pB: warning: multiple dynamic symbol tables detected"
2711
139
         " - ignoring the table in section %u"),
2712
139
       abfd, shindex);
2713
139
    goto success;
2714
139
  }
2715
1.61k
      elf_dynsymtab (abfd) = shindex;
2716
1.61k
      elf_tdata (abfd)->dynsymtab_hdr = *hdr;
2717
1.61k
      elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr;
2718
1.61k
      abfd->flags |= HAS_SYMS;
2719
2720
      /* Besides being a symbol table, we also treat this as a regular
2721
   section, so that objcopy can handle it.  */
2722
1.61k
      ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2723
1.61k
      goto success;
2724
2725
10.4k
    case SHT_SYMTAB_SHNDX: /* Symbol section indices when >64k sections.  */
2726
10.4k
      {
2727
10.4k
  elf_section_list * entry;
2728
2729
12.9k
  for (entry = elf_symtab_shndx_list (abfd); entry; entry = entry->next)
2730
3.10k
    if (entry->ndx == shindex)
2731
571
      goto success;
2732
2733
9.87k
  entry = bfd_alloc (abfd, sizeof (*entry));
2734
9.87k
  if (entry == NULL)
2735
0
    goto fail;
2736
9.87k
  entry->ndx = shindex;
2737
9.87k
  entry->hdr = * hdr;
2738
9.87k
  entry->next = elf_symtab_shndx_list (abfd);
2739
9.87k
  elf_symtab_shndx_list (abfd) = entry;
2740
9.87k
  elf_elfsections (abfd)[shindex] = & entry->hdr;
2741
9.87k
  goto success;
2742
9.87k
      }
2743
2744
85.2k
    case SHT_STRTAB:   /* A string table.  */
2745
85.2k
      if (hdr->bfd_section != NULL)
2746
398
  goto success;
2747
2748
84.8k
      if (ehdr->e_shstrndx == shindex)
2749
69.9k
  {
2750
69.9k
    elf_tdata (abfd)->shstrtab_hdr = *hdr;
2751
69.9k
    elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr;
2752
69.9k
    goto success;
2753
69.9k
  }
2754
2755
14.9k
      if (elf_elfsections (abfd)[elf_onesymtab (abfd)]->sh_link == shindex)
2756
3.57k
  {
2757
7.34k
  symtab_strtab:
2758
7.34k
    elf_tdata (abfd)->strtab_hdr = *hdr;
2759
7.34k
    elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->strtab_hdr;
2760
7.34k
    goto success;
2761
3.57k
  }
2762
2763
11.3k
      if (elf_elfsections (abfd)[elf_dynsymtab (abfd)]->sh_link == shindex)
2764
653
  {
2765
1.09k
  dynsymtab_strtab:
2766
1.09k
    elf_tdata (abfd)->dynstrtab_hdr = *hdr;
2767
1.09k
    hdr = &elf_tdata (abfd)->dynstrtab_hdr;
2768
1.09k
    elf_elfsections (abfd)[shindex] = hdr;
2769
    /* We also treat this as a regular section, so that objcopy
2770
       can handle it.  */
2771
1.09k
    ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2772
1.09k
             shindex);
2773
1.09k
    goto success;
2774
653
  }
2775
2776
      /* If the string table isn't one of the above, then treat it as a
2777
   regular section.  We need to scan all the headers to be sure,
2778
   just in case this strtab section appeared before the above.  */
2779
10.6k
      if (elf_onesymtab (abfd) == 0 || elf_dynsymtab (abfd) == 0)
2780
10.4k
  {
2781
10.4k
    unsigned int i, num_sec;
2782
2783
10.4k
    num_sec = elf_numsections (abfd);
2784
133k
    for (i = 1; i < num_sec; i++)
2785
127k
      {
2786
127k
        Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
2787
127k
        if (hdr2->sh_link == shindex)
2788
8.82k
    {
2789
      /* Prevent endless recursion on broken objects.  */
2790
8.82k
      if (i == shindex)
2791
117
        goto fail;
2792
8.70k
      if (! bfd_section_from_shdr (abfd, i))
2793
272
        goto fail;
2794
8.43k
      if (elf_onesymtab (abfd) == i)
2795
3.76k
        goto symtab_strtab;
2796
4.66k
      if (elf_dynsymtab (abfd) == i)
2797
438
        goto dynsymtab_strtab;
2798
4.66k
    }
2799
127k
      }
2800
10.4k
  }
2801
6.08k
      ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2802
6.08k
      goto success;
2803
2804
25.7k
    case SHT_REL:
2805
131k
    case SHT_RELA:
2806
131k
    case SHT_RELR:
2807
      /* *These* do a lot of work -- but build no sections!  */
2808
131k
      {
2809
131k
  asection *target_sect;
2810
131k
  Elf_Internal_Shdr *hdr2, **p_hdr;
2811
131k
  unsigned int num_sec = elf_numsections (abfd);
2812
131k
  struct bfd_elf_section_data *esdt;
2813
131k
  bfd_size_type size;
2814
2815
131k
  if (hdr->sh_type == SHT_REL)
2816
25.7k
    size = bed->s->sizeof_rel;
2817
105k
  else if (hdr->sh_type == SHT_RELA)
2818
105k
    size = bed->s->sizeof_rela;
2819
396
  else
2820
396
    size = bed->s->arch_size / 8;
2821
131k
  if (hdr->sh_entsize != size)
2822
1.05k
    goto fail;
2823
2824
  /* Check for a bogus link to avoid crashing.  */
2825
130k
  if (hdr->sh_link >= num_sec)
2826
111
    {
2827
111
      _bfd_error_handler
2828
        /* xgettext:c-format */
2829
111
        (_("%pB: invalid link %u for reloc section %s (index %u)"),
2830
111
         abfd, hdr->sh_link, name, shindex);
2831
111
      ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2832
111
      goto success;
2833
111
    }
2834
2835
  /* Get the symbol table.  */
2836
130k
  if ((elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB
2837
30.7k
       || elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_DYNSYM)
2838
100k
      && ! bfd_section_from_shdr (abfd, hdr->sh_link))
2839
412
    goto fail;
2840
2841
  /* If this is an alloc section in an executable or shared
2842
     library, or the reloc section does not use the main symbol
2843
     table we don't treat it as a reloc section.  BFD can't
2844
     adequately represent such a section, so at least for now,
2845
     we don't try.  We just present it as a normal section.  We
2846
     also can't use it as a reloc section if it points to the
2847
     null section, an invalid section, another reloc section, or
2848
     its sh_link points to the null section.  */
2849
129k
  if (((abfd->flags & (DYNAMIC | EXEC_P)) != 0
2850
6.02k
       && (hdr->sh_flags & SHF_ALLOC) != 0)
2851
128k
      || (hdr->sh_flags & SHF_COMPRESSED) != 0
2852
125k
      || hdr->sh_type == SHT_RELR
2853
124k
      || hdr->sh_link == SHN_UNDEF
2854
121k
      || hdr->sh_link != elf_onesymtab (abfd)
2855
95.1k
      || hdr->sh_info == SHN_UNDEF
2856
94.3k
      || hdr->sh_info >= num_sec
2857
94.3k
      || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_REL
2858
93.9k
      || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_RELA)
2859
36.5k
    {
2860
36.5k
      ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2861
36.5k
      goto success;
2862
36.5k
    }
2863
2864
93.3k
  if (! bfd_section_from_shdr (abfd, hdr->sh_info))
2865
144
    goto fail;
2866
2867
93.1k
  target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info);
2868
93.1k
  if (target_sect == NULL)
2869
158
    goto fail;
2870
2871
93.0k
  esdt = elf_section_data (target_sect);
2872
93.0k
  if (hdr->sh_type == SHT_RELA)
2873
75.5k
    p_hdr = &esdt->rela.hdr;
2874
17.4k
  else
2875
17.4k
    p_hdr = &esdt->rel.hdr;
2876
2877
  /* PR 17512: file: 0b4f81b7.
2878
     Also see PR 24456, for a file which deliberately has two reloc
2879
     sections.  */
2880
93.0k
  if (*p_hdr != NULL)
2881
1.71k
    {
2882
1.71k
      if (!bed->init_secondary_reloc_section (abfd, hdr, name, shindex))
2883
165
        {
2884
165
    _bfd_error_handler
2885
      /* xgettext:c-format */
2886
165
      (_("%pB: warning: secondary relocation section '%s' "
2887
165
         "for section %pA found - ignoring"),
2888
165
       abfd, name, target_sect);
2889
165
        }
2890
1.55k
      else
2891
1.55k
        esdt->has_secondary_relocs = true;
2892
1.71k
      goto success;
2893
1.71k
    }
2894
2895
91.2k
  hdr2 = bfd_alloc (abfd, sizeof (*hdr2));
2896
91.2k
  if (hdr2 == NULL)
2897
0
    goto fail;
2898
91.2k
  *hdr2 = *hdr;
2899
91.2k
  *p_hdr = hdr2;
2900
91.2k
  elf_elfsections (abfd)[shindex] = hdr2;
2901
91.2k
  target_sect->reloc_count += (NUM_SHDR_ENTRIES (hdr)
2902
91.2k
             * bed->s->int_rels_per_ext_rel);
2903
91.2k
  target_sect->flags |= SEC_RELOC;
2904
91.2k
  target_sect->relocation = NULL;
2905
91.2k
  target_sect->rel_filepos = hdr->sh_offset;
2906
  /* In the section to which the relocations apply, mark whether
2907
     its relocations are of the REL or RELA variety.  */
2908
91.2k
  if (hdr->sh_size != 0)
2909
90.3k
    {
2910
90.3k
      if (hdr->sh_type == SHT_RELA)
2911
73.2k
        target_sect->use_rela_p = 1;
2912
90.3k
    }
2913
91.2k
  abfd->flags |= HAS_RELOC;
2914
91.2k
  goto success;
2915
91.2k
      }
2916
2917
970
    case SHT_GNU_verdef:
2918
970
      if (hdr->sh_info != 0)
2919
756
  elf_dynverdef (abfd) = shindex;
2920
970
      elf_tdata (abfd)->dynverdef_hdr = *hdr;
2921
970
      ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2922
970
      goto success;
2923
2924
1.33k
    case SHT_GNU_versym:
2925
1.33k
      if (hdr->sh_entsize != sizeof (Elf_External_Versym))
2926
129
  goto fail;
2927
2928
1.20k
      elf_dynversym (abfd) = shindex;
2929
1.20k
      elf_tdata (abfd)->dynversym_hdr = *hdr;
2930
1.20k
      ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2931
1.20k
      goto success;
2932
2933
1.31k
    case SHT_GNU_verneed:
2934
1.31k
      if (hdr->sh_info != 0)
2935
1.17k
  elf_dynverref (abfd) = shindex;
2936
1.31k
      elf_tdata (abfd)->dynverref_hdr = *hdr;
2937
1.31k
      ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2938
1.31k
      goto success;
2939
2940
1.33k
    case SHT_SHLIB:
2941
1.33k
      goto success;
2942
2943
24.0k
    case SHT_GROUP:
2944
24.0k
      if (!_bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
2945
32
  goto fail;
2946
2947
23.9k
      goto success;
2948
2949
224k
    default:
2950
      /* Possibly an attributes section.  */
2951
224k
      if (get_elf_backend_data (abfd)->target_os != is_solaris
2952
    /* PR 33153: Solaris defines SHT_SUNW_cap which collides with SHT_GNU_ATTRIBUTES.  */
2953
218k
    && (hdr->sh_type == SHT_GNU_ATTRIBUTES
2954
212k
        || hdr->sh_type == bed->obj_attrs_section_type))
2955
6.79k
  {
2956
6.79k
    if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
2957
42
      goto fail;
2958
6.75k
    _bfd_elf_parse_attributes (abfd, hdr);
2959
6.75k
    goto success;
2960
6.79k
  }
2961
2962
      /* Check for any processor-specific section types.  */
2963
217k
      if (bed->elf_backend_section_from_shdr (abfd, hdr, name, shindex))
2964
198k
  goto success;
2965
2966
18.8k
      if (hdr->sh_type >= SHT_LOUSER && hdr->sh_type <= SHT_HIUSER)
2967
8.42k
  {
2968
8.42k
    if ((hdr->sh_flags & SHF_ALLOC) != 0)
2969
      /* FIXME: How to properly handle allocated section reserved
2970
         for applications?  */
2971
604
      _bfd_error_handler
2972
        /* xgettext:c-format */
2973
604
        (_("%pB: unknown type [%#x] section `%s'"),
2974
604
         abfd, hdr->sh_type, name);
2975
7.81k
    else
2976
7.81k
      {
2977
        /* Allow sections reserved for applications.  */
2978
7.81k
        ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2979
7.81k
        goto success;
2980
7.81k
      }
2981
8.42k
  }
2982
10.4k
      else if (hdr->sh_type >= SHT_LOPROC
2983
1.58k
         && hdr->sh_type <= SHT_HIPROC)
2984
  /* FIXME: We should handle this section.  */
2985
1.58k
  _bfd_error_handler
2986
    /* xgettext:c-format */
2987
1.58k
    (_("%pB: unknown type [%#x] section `%s'"),
2988
1.58k
     abfd, hdr->sh_type, name);
2989
8.86k
      else if (hdr->sh_type >= SHT_LOOS && hdr->sh_type <= SHT_HIOS)
2990
4.92k
  {
2991
    /* Unrecognised OS-specific sections.  */
2992
4.92k
    if ((hdr->sh_flags & SHF_OS_NONCONFORMING) != 0)
2993
      /* SHF_OS_NONCONFORMING indicates that special knowledge is
2994
         required to correctly process the section and the file should
2995
         be rejected with an error message.  */
2996
359
      _bfd_error_handler
2997
        /* xgettext:c-format */
2998
359
        (_("%pB: unknown type [%#x] section `%s'"),
2999
359
         abfd, hdr->sh_type, name);
3000
4.56k
    else
3001
4.56k
      {
3002
        /* Otherwise it should be processed.  */
3003
4.56k
        ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
3004
4.56k
        goto success;
3005
4.56k
      }
3006
4.92k
  }
3007
3.94k
      else
3008
  /* FIXME: We should handle this section.  */
3009
3.94k
  _bfd_error_handler
3010
    /* xgettext:c-format */
3011
3.94k
    (_("%pB: unknown type [%#x] section `%s'"),
3012
3.94k
     abfd, hdr->sh_type, name);
3013
3014
6.48k
      goto fail;
3015
1.09M
    }
3016
3017
20.1k
 fail:
3018
20.1k
  ret = false;
3019
1.10M
 success:
3020
1.10M
  elf_tdata (abfd)->being_created[shindex] = false;
3021
1.10M
  return ret;
3022
20.1k
}
3023
3024
/* Return the local symbol specified by ABFD, R_SYMNDX.  */
3025
3026
Elf_Internal_Sym *
3027
bfd_sym_from_r_symndx (struct sym_cache *cache,
3028
           bfd *abfd,
3029
           unsigned long r_symndx)
3030
0
{
3031
0
  unsigned int ent = r_symndx % LOCAL_SYM_CACHE_SIZE;
3032
3033
0
  if (cache->abfd != abfd || cache->indx[ent] != r_symndx)
3034
0
    {
3035
0
      Elf_Internal_Shdr *symtab_hdr;
3036
0
      unsigned char esym[sizeof (Elf64_External_Sym)];
3037
0
      Elf_External_Sym_Shndx eshndx;
3038
3039
0
      symtab_hdr = &elf_symtab_hdr (abfd);
3040
0
      if (bfd_elf_get_elf_syms (abfd, symtab_hdr, 1, r_symndx,
3041
0
        &cache->sym[ent], esym, &eshndx) == NULL)
3042
0
  return NULL;
3043
3044
0
      if (cache->abfd != abfd)
3045
0
  {
3046
0
    memset (cache->indx, -1, sizeof (cache->indx));
3047
0
    cache->abfd = abfd;
3048
0
  }
3049
0
      cache->indx[ent] = r_symndx;
3050
0
    }
3051
3052
0
  return &cache->sym[ent];
3053
0
}
3054
3055
/* Given an ELF section number, retrieve the corresponding BFD
3056
   section.  */
3057
3058
asection *
3059
bfd_section_from_elf_index (bfd *abfd, unsigned int sec_index)
3060
196k
{
3061
196k
  if (sec_index >= elf_numsections (abfd))
3062
32.5k
    return NULL;
3063
163k
  return elf_elfsections (abfd)[sec_index]->bfd_section;
3064
196k
}
3065
3066
static const struct bfd_elf_special_section special_sections_b[] =
3067
{
3068
  { STRING_COMMA_LEN (".bss"), -2, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE },
3069
  { NULL,       0,  0, 0,    0 }
3070
};
3071
3072
static const struct bfd_elf_special_section special_sections_c[] =
3073
{
3074
  { STRING_COMMA_LEN (".comment"), 0, SHT_PROGBITS, 0 },
3075
  { STRING_COMMA_LEN (".ctf"),  0, SHT_PROGBITS,    0 },
3076
  { NULL,     0, 0, 0,      0 }
3077
};
3078
3079
static const struct bfd_elf_special_section special_sections_d[] =
3080
{
3081
  { STRING_COMMA_LEN (".data"),   -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
3082
  { STRING_COMMA_LEN (".data1"),   0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
3083
  /* There are more DWARF sections than these, but they needn't be added here
3084
     unless you have to cope with broken compilers that don't emit section
3085
     attributes or you want to help the user writing assembler.  */
3086
  { STRING_COMMA_LEN (".debug"),   0, SHT_PROGBITS, 0 },
3087
  { STRING_COMMA_LEN (".debug_line"),  0, SHT_PROGBITS, 0 },
3088
  { STRING_COMMA_LEN (".debug_info"),  0, SHT_PROGBITS, 0 },
3089
  { STRING_COMMA_LEN (".debug_abbrev"),  0, SHT_PROGBITS, 0 },
3090
  { STRING_COMMA_LEN (".debug_aranges"), 0, SHT_PROGBITS, 0 },
3091
  { STRING_COMMA_LEN (".dynamic"),   0, SHT_DYNAMIC,  SHF_ALLOC },
3092
  { STRING_COMMA_LEN (".dynstr"),  0, SHT_STRTAB,   SHF_ALLOC },
3093
  { STRING_COMMA_LEN (".dynsym"),  0, SHT_DYNSYM,   SHF_ALLOC },
3094
  { NULL,          0,  0, 0,      0 }
3095
};
3096
3097
static const struct bfd_elf_special_section special_sections_f[] =
3098
{
3099
  { STRING_COMMA_LEN (".fini"),        0, SHT_PROGBITS,   SHF_ALLOC + SHF_EXECINSTR },
3100
  { STRING_COMMA_LEN (".fini_array"), -2, SHT_FINI_ARRAY, SHF_ALLOC + SHF_WRITE },
3101
  { NULL,        0 , 0, 0,      0 }
3102
};
3103
3104
static const struct bfd_elf_special_section special_sections_g[] =
3105
{
3106
  { STRING_COMMA_LEN (".gnu.linkonce.b"), -2, SHT_NOBITS,      SHF_ALLOC + SHF_WRITE },
3107
  { STRING_COMMA_LEN (".gnu.linkonce.n"), -2, SHT_NOBITS,      SHF_ALLOC + SHF_WRITE },
3108
  { STRING_COMMA_LEN (".gnu.linkonce.p"), -2, SHT_PROGBITS,    SHF_ALLOC + SHF_WRITE },
3109
  { STRING_COMMA_LEN (".gnu.lto_"),   -1, SHT_PROGBITS,    SHF_EXCLUDE },
3110
  { STRING_COMMA_LEN (".got"),       0, SHT_PROGBITS,    SHF_ALLOC + SHF_WRITE },
3111
  { STRING_COMMA_LEN (".gnu_object_only"), 0, SHT_GNU_OBJECT_ONLY, SHF_EXCLUDE },
3112
  { STRING_COMMA_LEN (".gnu.version"),     0, SHT_GNU_versym,  0 },
3113
  { STRING_COMMA_LEN (".gnu.version_d"),   0, SHT_GNU_verdef,  0 },
3114
  { STRING_COMMA_LEN (".gnu.version_r"),   0, SHT_GNU_verneed, 0 },
3115
  { STRING_COMMA_LEN (".gnu.liblist"),     0, SHT_GNU_LIBLIST, SHF_ALLOC },
3116
  { STRING_COMMA_LEN (".gnu.conflict"),    0, SHT_RELA,        SHF_ALLOC },
3117
  { STRING_COMMA_LEN (".gnu.hash"),    0, SHT_GNU_HASH,    SHF_ALLOC },
3118
  { NULL,      0,    0, 0,         0 }
3119
};
3120
3121
static const struct bfd_elf_special_section special_sections_h[] =
3122
{
3123
  { STRING_COMMA_LEN (".hash"), 0, SHT_HASH,   SHF_ALLOC },
3124
  { NULL,        0, 0, 0,    0 }
3125
};
3126
3127
static const struct bfd_elf_special_section special_sections_i[] =
3128
{
3129
  { STRING_COMMA_LEN (".init"),        0, SHT_PROGBITS,   SHF_ALLOC + SHF_EXECINSTR },
3130
  { STRING_COMMA_LEN (".init_array"), -2, SHT_INIT_ARRAY, SHF_ALLOC + SHF_WRITE },
3131
  { STRING_COMMA_LEN (".interp"),      0, SHT_PROGBITS,   0 },
3132
  { NULL,          0,      0, 0,      0 }
3133
};
3134
3135
static const struct bfd_elf_special_section special_sections_l[] =
3136
{
3137
  { STRING_COMMA_LEN (".line"), 0, SHT_PROGBITS, 0 },
3138
  { NULL,        0, 0, 0,    0 }
3139
};
3140
3141
static const struct bfd_elf_special_section special_sections_n[] =
3142
{
3143
  { STRING_COMMA_LEN (".noinit"),  -2, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE },
3144
  { STRING_COMMA_LEN (".note.GNU-stack"), 0, SHT_NOTE,     0 },
3145
  { STRING_COMMA_LEN (".note"),    -1, SHT_NOTE,     0 },
3146
  { NULL,        0,     0, 0,      0 }
3147
};
3148
3149
static const struct bfd_elf_special_section special_sections_p[] =
3150
{
3151
  { STRING_COMMA_LEN (".persistent.bss"), 0, SHT_NOBITS,  SHF_ALLOC + SHF_WRITE },
3152
  { STRING_COMMA_LEN (".persistent"),  -2, SHT_PROGBITS,  SHF_ALLOC + SHF_WRITE },
3153
  { STRING_COMMA_LEN (".preinit_array"), -2, SHT_PREINIT_ARRAY, SHF_ALLOC + SHF_WRITE },
3154
  { STRING_COMMA_LEN (".plt"),      0, SHT_PROGBITS,  SHF_ALLOC + SHF_EXECINSTR },
3155
  { NULL,       0,      0, 0,     0 }
3156
};
3157
3158
static const struct bfd_elf_special_section special_sections_r[] =
3159
{
3160
  { STRING_COMMA_LEN (".rodata"), -2, SHT_PROGBITS, SHF_ALLOC },
3161
  { STRING_COMMA_LEN (".rodata1"), 0, SHT_PROGBITS, SHF_ALLOC },
3162
  { STRING_COMMA_LEN (".relr.dyn"), 0, SHT_RELR, SHF_ALLOC },
3163
  { STRING_COMMA_LEN (".rela"),   -1, SHT_RELA,     0 },
3164
  /* .relro_padding is generated by lld.  It should not be confused with a
3165
     reloc containing section, because otherwise elf_fake_sections() will
3166
     set the entsize to 8, which may not be an actual multiple of the
3167
     section's size.
3168
     Note - this entry must appear before the ".rel" entry below.  */
3169
  { STRING_COMMA_LEN (".relro_padding"), 0, SHT_NOBITS, SHF_ALLOC | SHF_WRITE },
3170
  { STRING_COMMA_LEN (".rel"),    -1, SHT_REL,      0 },
3171
  { NULL,       0,     0, 0,      0 }
3172
};
3173
3174
static const struct bfd_elf_special_section special_sections_s[] =
3175
{
3176
  { STRING_COMMA_LEN (".shstrtab"), 0, SHT_STRTAB, 0 },
3177
  { STRING_COMMA_LEN (".strtab"),   0, SHT_STRTAB, 0 },
3178
  { STRING_COMMA_LEN (".symtab"),   0, SHT_SYMTAB, 0 },
3179
  /* See struct bfd_elf_special_section declaration for the semantics of
3180
     this special case where .prefix_length != strlen (.prefix).  */
3181
  { ".stabstr",     5,  3, SHT_STRTAB, 0 },
3182
  { NULL,     0,  0, 0,    0 }
3183
};
3184
3185
static const struct bfd_elf_special_section special_sections_t[] =
3186
{
3187
  { STRING_COMMA_LEN (".text"),  -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
3188
  { STRING_COMMA_LEN (".tbss"),  -2, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE + SHF_TLS },
3189
  { STRING_COMMA_LEN (".tdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
3190
  { NULL,         0,  0, 0,      0 }
3191
};
3192
3193
static const struct bfd_elf_special_section special_sections_z[] =
3194
{
3195
  { STRING_COMMA_LEN (".zdebug_line"),    0, SHT_PROGBITS, 0 },
3196
  { STRING_COMMA_LEN (".zdebug_info"),    0, SHT_PROGBITS, 0 },
3197
  { STRING_COMMA_LEN (".zdebug_abbrev"),  0, SHT_PROGBITS, 0 },
3198
  { STRING_COMMA_LEN (".zdebug_aranges"), 0, SHT_PROGBITS, 0 },
3199
  { NULL,         0,  0, 0,      0 }
3200
};
3201
3202
static const struct bfd_elf_special_section * const special_sections[] =
3203
{
3204
  special_sections_b,   /* 'b' */
3205
  special_sections_c,   /* 'c' */
3206
  special_sections_d,   /* 'd' */
3207
  NULL,       /* 'e' */
3208
  special_sections_f,   /* 'f' */
3209
  special_sections_g,   /* 'g' */
3210
  special_sections_h,   /* 'h' */
3211
  special_sections_i,   /* 'i' */
3212
  NULL,       /* 'j' */
3213
  NULL,       /* 'k' */
3214
  special_sections_l,   /* 'l' */
3215
  NULL,       /* 'm' */
3216
  special_sections_n,   /* 'n' */
3217
  NULL,       /* 'o' */
3218
  special_sections_p,   /* 'p' */
3219
  NULL,       /* 'q' */
3220
  special_sections_r,   /* 'r' */
3221
  special_sections_s,   /* 's' */
3222
  special_sections_t,   /* 't' */
3223
  NULL,       /* 'u' */
3224
  NULL,       /* 'v' */
3225
  NULL,       /* 'w' */
3226
  NULL,       /* 'x' */
3227
  NULL,       /* 'y' */
3228
  special_sections_z    /* 'z' */
3229
};
3230
3231
const struct bfd_elf_special_section *
3232
_bfd_elf_get_special_section (const char *name,
3233
            const struct bfd_elf_special_section *spec,
3234
            unsigned int rela)
3235
1.09M
{
3236
1.09M
  int i;
3237
1.09M
  int len;
3238
3239
1.09M
  len = strlen (name);
3240
3241
6.19M
  for (i = 0; spec[i].prefix != NULL; i++)
3242
5.26M
    {
3243
5.26M
      int suffix_len;
3244
5.26M
      int prefix_len = spec[i].prefix_length;
3245
3246
5.26M
      if (len < prefix_len)
3247
2.96M
  continue;
3248
2.30M
      if (memcmp (name, spec[i].prefix, prefix_len) != 0)
3249
2.05M
  continue;
3250
3251
246k
      suffix_len = spec[i].suffix_length;
3252
246k
      if (suffix_len <= 0)
3253
244k
  {
3254
244k
    if (name[prefix_len] != 0)
3255
142k
      {
3256
142k
        if (suffix_len == 0)
3257
69.0k
    continue;
3258
73.5k
        if (name[prefix_len] != '.'
3259
13.3k
      && (suffix_len == -2
3260
5.74k
          || (rela && spec[i].type == SHT_REL)))
3261
9.36k
    continue;
3262
73.5k
      }
3263
244k
  }
3264
1.95k
      else
3265
1.95k
  {
3266
1.95k
    if (len < prefix_len + suffix_len)
3267
939
      continue;
3268
1.02k
    if (memcmp (name + len - suffix_len,
3269
1.02k
          spec[i].prefix + prefix_len,
3270
1.02k
          suffix_len) != 0)
3271
787
      continue;
3272
1.02k
  }
3273
166k
      return &spec[i];
3274
246k
    }
3275
3276
926k
  return NULL;
3277
1.09M
}
3278
3279
const struct bfd_elf_special_section *
3280
_bfd_elf_get_sec_type_attr (bfd *abfd, asection *sec)
3281
1.75M
{
3282
1.75M
  int i;
3283
1.75M
  const struct bfd_elf_special_section *spec;
3284
1.75M
  elf_backend_data *bed;
3285
3286
  /* See if this is one of the special sections.  */
3287
1.75M
  if (sec->name == NULL)
3288
0
    return NULL;
3289
3290
1.75M
  bed = get_elf_backend_data (abfd);
3291
1.75M
  spec = bed->special_sections;
3292
1.75M
  if (spec)
3293
828k
    {
3294
828k
      spec = _bfd_elf_get_special_section (sec->name,
3295
828k
             bed->special_sections,
3296
828k
             sec->use_rela_p);
3297
828k
      if (spec != NULL)
3298
2.84k
  return spec;
3299
828k
    }
3300
3301
1.75M
  if (sec->name[0] != '.')
3302
1.48M
    return NULL;
3303
3304
264k
  i = sec->name[1] - 'b';
3305
264k
  if (i < 0 || i > 'z' - 'b')
3306
11.3k
    return NULL;
3307
3308
252k
  spec = special_sections[i];
3309
3310
252k
  if (spec == NULL)
3311
15.6k
    return NULL;
3312
3313
236k
  return _bfd_elf_get_special_section (sec->name, spec, sec->use_rela_p);
3314
252k
}
3315
3316
bool
3317
_bfd_elf_new_section_hook (bfd *abfd, asection *sec)
3318
1.69M
{
3319
1.69M
  struct bfd_elf_section_data *sdata;
3320
1.69M
  elf_backend_data *bed;
3321
1.69M
  const struct bfd_elf_special_section *ssect;
3322
3323
1.69M
  sdata = sec->used_by_bfd;
3324
1.69M
  if (sdata == NULL)
3325
1.21M
    {
3326
1.21M
      sdata = bfd_zalloc (abfd, sizeof (*sdata));
3327
1.21M
      if (sdata == NULL)
3328
0
  return false;
3329
1.21M
      sec->used_by_bfd = sdata;
3330
1.21M
    }
3331
3332
  /* Indicate whether or not this section should use RELA relocations.  */
3333
1.69M
  bed = get_elf_backend_data (abfd);
3334
1.69M
  sec->use_rela_p = bed->default_use_rela_p;
3335
3336
  /* Set up ELF section type and flags for newly created sections, if
3337
     there is an ABI mandated section.  */
3338
1.69M
  ssect = (*bed->get_sec_type_attr) (abfd, sec);
3339
1.69M
  if (ssect != NULL)
3340
166k
    {
3341
166k
      elf_section_type (sec) = ssect->type;
3342
166k
      elf_section_flags (sec) = ssect->attr;
3343
166k
    }
3344
3345
1.69M
  return _bfd_generic_new_section_hook (abfd, sec);
3346
1.69M
}
3347
3348
/* Create a new bfd section from an ELF program header.
3349
3350
   Since program segments have no names, we generate a synthetic name
3351
   of the form segment<NUM>, where NUM is generally the index in the
3352
   program header table.  For segments that are split (see below) we
3353
   generate the names segment<NUM>a and segment<NUM>b.
3354
3355
   Note that some program segments may have a file size that is different than
3356
   (less than) the memory size.  All this means is that at execution the
3357
   system must allocate the amount of memory specified by the memory size,
3358
   but only initialize it with the first "file size" bytes read from the
3359
   file.  This would occur for example, with program segments consisting
3360
   of combined data+bss.
3361
3362
   To handle the above situation, this routine generates TWO bfd sections
3363
   for the single program segment.  The first has the length specified by
3364
   the file size of the segment, and the second has the length specified
3365
   by the difference between the two sizes.  In effect, the segment is split
3366
   into its initialized and uninitialized parts.  */
3367
3368
bool
3369
_bfd_elf_make_section_from_phdr (bfd *abfd,
3370
         Elf_Internal_Phdr *hdr,
3371
         int hdr_index,
3372
         const char *type_name)
3373
896k
{
3374
896k
  asection *newsect;
3375
896k
  char *name;
3376
896k
  char namebuf[64];
3377
896k
  size_t len;
3378
896k
  int split;
3379
896k
  unsigned int opb = bfd_octets_per_byte (abfd, NULL);
3380
3381
896k
  split = ((hdr->p_memsz > 0)
3382
668k
      && (hdr->p_filesz > 0)
3383
599k
      && (hdr->p_memsz > hdr->p_filesz));
3384
3385
896k
  if (hdr->p_filesz > 0)
3386
762k
    {
3387
762k
      sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "a" : "");
3388
762k
      len = strlen (namebuf) + 1;
3389
762k
      name = bfd_alloc (abfd, len);
3390
762k
      if (!name)
3391
0
  return false;
3392
762k
      memcpy (name, namebuf, len);
3393
762k
      newsect = bfd_make_section (abfd, name);
3394
762k
      if (newsect == NULL)
3395
0
  return false;
3396
762k
      newsect->vma = hdr->p_vaddr / opb;
3397
762k
      newsect->lma = hdr->p_paddr / opb;
3398
762k
      newsect->size = hdr->p_filesz;
3399
762k
      newsect->filepos = hdr->p_offset;
3400
762k
      newsect->flags |= SEC_HAS_CONTENTS;
3401
762k
      newsect->alignment_power = bfd_log2 (hdr->p_align);
3402
762k
      if (hdr->p_type == PT_LOAD)
3403
11.0k
  {
3404
11.0k
    newsect->flags |= SEC_ALLOC;
3405
11.0k
    newsect->flags |= SEC_LOAD;
3406
11.0k
    if (hdr->p_flags & PF_X)
3407
4.49k
      {
3408
        /* FIXME: all we known is that it has execute PERMISSION,
3409
     may be data.  */
3410
4.49k
        newsect->flags |= SEC_CODE;
3411
4.49k
      }
3412
11.0k
  }
3413
762k
      if (!(hdr->p_flags & PF_W))
3414
563k
  {
3415
563k
    newsect->flags |= SEC_READONLY;
3416
563k
  }
3417
762k
    }
3418
3419
896k
  if (hdr->p_memsz > hdr->p_filesz)
3420
334k
    {
3421
334k
      bfd_vma align;
3422
3423
334k
      sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "b" : "");
3424
334k
      len = strlen (namebuf) + 1;
3425
334k
      name = bfd_alloc (abfd, len);
3426
334k
      if (!name)
3427
0
  return false;
3428
334k
      memcpy (name, namebuf, len);
3429
334k
      newsect = bfd_make_section (abfd, name);
3430
334k
      if (newsect == NULL)
3431
0
  return false;
3432
334k
      newsect->vma = (hdr->p_vaddr + hdr->p_filesz) / opb;
3433
334k
      newsect->lma = (hdr->p_paddr + hdr->p_filesz) / opb;
3434
334k
      newsect->size = hdr->p_memsz - hdr->p_filesz;
3435
334k
      newsect->filepos = hdr->p_offset + hdr->p_filesz;
3436
334k
      align = newsect->vma & -newsect->vma;
3437
334k
      if (align == 0 || align > hdr->p_align)
3438
75.3k
  align = hdr->p_align;
3439
334k
      newsect->alignment_power = bfd_log2 (align);
3440
334k
      if (hdr->p_type == PT_LOAD)
3441
9.59k
  {
3442
9.59k
    newsect->flags |= SEC_ALLOC;
3443
9.59k
    if (hdr->p_flags & PF_X)
3444
3.27k
      newsect->flags |= SEC_CODE;
3445
9.59k
  }
3446
334k
      if (!(hdr->p_flags & PF_W))
3447
251k
  newsect->flags |= SEC_READONLY;
3448
334k
    }
3449
3450
896k
  return true;
3451
896k
}
3452
3453
static bool
3454
_bfd_elf_core_find_build_id (bfd *templ, bfd_vma offset)
3455
14.2k
{
3456
  /* The return value is ignored.  Build-ids are considered optional.  */
3457
14.2k
  if (templ->xvec->flavour == bfd_target_elf_flavour)
3458
14.2k
    return (*get_elf_backend_data (templ)->elf_backend_core_find_build_id)
3459
14.2k
      (templ, offset);
3460
0
  return false;
3461
14.2k
}
3462
3463
bool
3464
bfd_section_from_phdr (bfd *abfd, Elf_Internal_Phdr *hdr, int hdr_index)
3465
896k
{
3466
896k
  elf_backend_data *bed;
3467
3468
896k
  switch (hdr->p_type)
3469
896k
    {
3470
176k
    case PT_NULL:
3471
176k
      return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "null");
3472
3473
14.2k
    case PT_LOAD:
3474
14.2k
      if (! _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "load"))
3475
0
  return false;
3476
14.2k
      if (bfd_get_format (abfd) == bfd_core && abfd->build_id == NULL)
3477
14.2k
  _bfd_elf_core_find_build_id (abfd, hdr->p_offset);
3478
14.2k
      return true;
3479
3480
3.22k
    case PT_DYNAMIC:
3481
3.22k
      return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "dynamic");
3482
3483
2.61k
    case PT_INTERP:
3484
2.61k
      return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "interp");
3485
3486
2.21k
    case PT_NOTE:
3487
2.21k
      if (! _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "note"))
3488
0
  return false;
3489
2.21k
      if (! _bfd_elf_read_notes (abfd, hdr->p_offset, hdr->p_filesz,
3490
2.21k
         hdr->p_align))
3491
1.25k
  return false;
3492
955
      return true;
3493
3494
993
    case PT_SHLIB:
3495
993
      return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "shlib");
3496
3497
13.4k
    case PT_PHDR:
3498
13.4k
      return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "phdr");
3499
3500
74
    case PT_GNU_EH_FRAME:
3501
74
      return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index,
3502
74
                "eh_frame_hdr");
3503
3504
97
    case PT_GNU_STACK:
3505
97
      return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "stack");
3506
3507
209
    case PT_GNU_RELRO:
3508
209
      return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "relro");
3509
3510
13
    case PT_GNU_SFRAME:
3511
13
      return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index,
3512
13
                "sframe");
3513
3514
682k
    default:
3515
      /* Check for any processor-specific program segment types.  */
3516
682k
      bed = get_elf_backend_data (abfd);
3517
682k
      return bed->elf_backend_section_from_phdr (abfd, hdr, hdr_index, "proc");
3518
896k
    }
3519
896k
}
3520
3521
/* Return the REL_HDR for SEC, assuming there is only a single one, either
3522
   REL or RELA.  */
3523
3524
Elf_Internal_Shdr *
3525
_bfd_elf_single_rel_hdr (asection *sec)
3526
4
{
3527
4
  if (elf_section_data (sec)->rel.hdr)
3528
0
    {
3529
0
      BFD_ASSERT (elf_section_data (sec)->rela.hdr == NULL);
3530
0
      return elf_section_data (sec)->rel.hdr;
3531
0
    }
3532
4
  else
3533
4
    return elf_section_data (sec)->rela.hdr;
3534
4
}
3535
3536
static bool
3537
_bfd_elf_set_reloc_sh_name (bfd *abfd,
3538
          Elf_Internal_Shdr *rel_hdr,
3539
          const char *sec_name,
3540
          bool use_rela_p)
3541
178
{
3542
178
  char *name = bfd_alloc (abfd, sizeof ".rela" + strlen (sec_name));
3543
178
  if (name == NULL)
3544
0
    return false;
3545
3546
178
  sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", sec_name);
3547
178
  rel_hdr->sh_name =
3548
178
    (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), name,
3549
178
          false);
3550
178
  if (rel_hdr->sh_name == (unsigned int) -1)
3551
0
    return false;
3552
3553
178
  return true;
3554
178
}
3555
3556
/* Allocate and initialize a section-header for a new reloc section,
3557
   containing relocations against ASECT.  It is stored in RELDATA.  If
3558
   USE_RELA_P is TRUE, we use RELA relocations; otherwise, we use REL
3559
   relocations.  */
3560
3561
static bool
3562
_bfd_elf_init_reloc_shdr (bfd *abfd,
3563
        struct bfd_elf_section_reloc_data *reldata,
3564
        const char *sec_name,
3565
        bool use_rela_p,
3566
        bool delay_sh_name_p)
3567
178
{
3568
178
  Elf_Internal_Shdr *rel_hdr;
3569
178
  elf_backend_data *bed = get_elf_backend_data (abfd);
3570
3571
178
  BFD_ASSERT (reldata->hdr == NULL);
3572
178
  rel_hdr = bfd_zalloc (abfd, sizeof (*rel_hdr));
3573
178
  if (rel_hdr == NULL)
3574
0
    return false;
3575
178
  reldata->hdr = rel_hdr;
3576
3577
178
  if (delay_sh_name_p)
3578
0
    rel_hdr->sh_name = (unsigned int) -1;
3579
178
  else if (!_bfd_elf_set_reloc_sh_name (abfd, rel_hdr, sec_name,
3580
178
          use_rela_p))
3581
0
    return false;
3582
178
  rel_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL;
3583
178
  rel_hdr->sh_entsize = (use_rela_p
3584
178
       ? bed->s->sizeof_rela
3585
178
       : bed->s->sizeof_rel);
3586
178
  rel_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
3587
178
  rel_hdr->sh_flags = 0;
3588
178
  rel_hdr->sh_addr = 0;
3589
178
  rel_hdr->sh_size = 0;
3590
178
  rel_hdr->sh_offset = 0;
3591
3592
178
  return true;
3593
178
}
3594
3595
/* Return the default section type based on the passed in section flags.  */
3596
3597
int
3598
bfd_elf_get_default_section_type (flagword flags)
3599
3.77k
{
3600
3.77k
  if ((flags & (SEC_ALLOC | SEC_IS_COMMON)) != 0
3601
1.03k
      && (flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
3602
63
    return SHT_NOBITS;
3603
3.70k
  return SHT_PROGBITS;
3604
3.77k
}
3605
3606
struct fake_section_arg
3607
{
3608
  struct bfd_link_info *link_info;
3609
  bool failed;
3610
};
3611
3612
/* Set up an ELF internal section header for a section.  */
3613
3614
static void
3615
elf_fake_sections (bfd *abfd, asection *asect, void *fsarg)
3616
1.83k
{
3617
1.83k
  struct fake_section_arg *arg = (struct fake_section_arg *)fsarg;
3618
1.83k
  elf_backend_data *bed = get_elf_backend_data (abfd);
3619
1.83k
  struct bfd_elf_section_data *esd = elf_section_data (asect);
3620
1.83k
  Elf_Internal_Shdr *this_hdr;
3621
1.83k
  unsigned int sh_type;
3622
1.83k
  const char *name = asect->name;
3623
1.83k
  bool delay_sh_name_p = false;
3624
1.83k
  bfd_vma mask;
3625
3626
1.83k
  if (arg->failed)
3627
0
    {
3628
      /* We already failed; just get out of the bfd_map_over_sections
3629
   loop.  */
3630
0
      return;
3631
0
    }
3632
3633
1.83k
  this_hdr = &esd->this_hdr;
3634
3635
  /* ld: compress DWARF debug sections with names: .debug_*.  */
3636
1.83k
  if (arg->link_info
3637
0
      && (abfd->flags & BFD_COMPRESS) != 0
3638
0
      && (asect->flags & SEC_DEBUGGING) != 0
3639
0
      && (asect->flags & SEC_ALLOC) == 0
3640
0
      && (asect->flags & SEC_HAS_CONTENTS) != 0
3641
0
      && name[1] == 'd'
3642
0
      && name[6] == '_')
3643
0
    {
3644
      /* If this section will be compressed, delay adding section
3645
   name to section name section after it is compressed in
3646
   _bfd_elf_assign_file_positions_for_non_load.  */
3647
0
      delay_sh_name_p = true;
3648
0
    }
3649
3650
1.83k
  if (delay_sh_name_p)
3651
0
    this_hdr->sh_name = (unsigned int) -1;
3652
1.83k
  else
3653
1.83k
    {
3654
1.83k
      this_hdr->sh_name
3655
1.83k
  = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
3656
1.83k
                name, false);
3657
1.83k
      if (this_hdr->sh_name == (unsigned int) -1)
3658
0
  {
3659
0
    arg->failed = true;
3660
0
    return;
3661
0
  }
3662
1.83k
    }
3663
3664
  /* Don't clear sh_flags. Assembler may set additional bits.  */
3665
3666
1.83k
  if ((asect->flags & SEC_ALLOC) != 0
3667
807
      || asect->user_set_vma)
3668
1.83k
    this_hdr->sh_addr = asect->vma * bfd_octets_per_byte (abfd, asect);
3669
0
  else
3670
0
    this_hdr->sh_addr = 0;
3671
3672
1.83k
  this_hdr->sh_offset = 0;
3673
1.83k
  this_hdr->sh_size = asect->size;
3674
1.83k
  this_hdr->sh_link = 0;
3675
  /* PR 17512: file: 0eb809fe, 8b0535ee.  */
3676
1.83k
  if (asect->alignment_power >= (sizeof (bfd_vma) * 8) - 1)
3677
0
    {
3678
0
      _bfd_error_handler
3679
  /* xgettext:c-format */
3680
0
  (_("%pB: error: alignment power %d of section `%pA' is too big"),
3681
0
   abfd, asect->alignment_power, asect);
3682
0
      arg->failed = true;
3683
0
      return;
3684
0
    }
3685
  /* Set sh_addralign to the highest power of two given by alignment
3686
     consistent with the section VMA.  Linker scripts can force VMA.  */
3687
1.83k
  mask = ((bfd_vma) 1 << asect->alignment_power) | this_hdr->sh_addr;
3688
1.83k
  this_hdr->sh_addralign = mask & -mask;
3689
  /* The sh_entsize and sh_info fields may have been set already by
3690
     copy_private_section_data.  */
3691
3692
1.83k
  this_hdr->bfd_section = asect;
3693
1.83k
  this_hdr->contents = NULL;
3694
3695
  /* If the section type is unspecified, we set it based on
3696
     asect->flags.  */
3697
1.83k
  if (asect->type != 0)
3698
0
    sh_type = asect->type;
3699
1.83k
  else if ((asect->flags & SEC_GROUP) != 0)
3700
9
    sh_type = SHT_GROUP;
3701
1.82k
  else
3702
1.82k
    sh_type = bfd_elf_get_default_section_type (asect->flags);
3703
3704
1.83k
  if (this_hdr->sh_type == SHT_NULL)
3705
385
    this_hdr->sh_type = sh_type;
3706
1.44k
  else if (this_hdr->sh_type == SHT_NOBITS
3707
33
     && sh_type == SHT_PROGBITS
3708
2
     && (asect->flags & SEC_ALLOC) != 0)
3709
0
    {
3710
      /* Warn if we are changing a NOBITS section to PROGBITS, but
3711
   allow the link to proceed.  This can happen when users link
3712
   non-bss input sections to bss output sections, or emit data
3713
   to a bss output section via a linker script.  */
3714
0
      _bfd_error_handler
3715
0
  (_("warning: section `%pA' type changed to PROGBITS"), asect);
3716
0
      this_hdr->sh_type = sh_type;
3717
0
    }
3718
3719
1.83k
  switch (this_hdr->sh_type)
3720
1.83k
    {
3721
163
    default:
3722
163
      break;
3723
3724
163
    case SHT_STRTAB:
3725
148
    case SHT_NOTE:
3726
213
    case SHT_NOBITS:
3727
1.33k
    case SHT_PROGBITS:
3728
1.33k
      break;
3729
3730
37
    case SHT_INIT_ARRAY:
3731
65
    case SHT_FINI_ARRAY:
3732
68
    case SHT_PREINIT_ARRAY:
3733
68
      this_hdr->sh_entsize = bed->s->arch_size / 8;
3734
68
      break;
3735
3736
17
    case SHT_HASH:
3737
17
      this_hdr->sh_entsize = bed->s->sizeof_hash_entry;
3738
17
      break;
3739
3740
29
    case SHT_DYNSYM:
3741
29
      this_hdr->sh_entsize = bed->s->sizeof_sym;
3742
29
      break;
3743
3744
41
    case SHT_DYNAMIC:
3745
41
      this_hdr->sh_entsize = bed->s->sizeof_dyn;
3746
41
      break;
3747
3748
84
    case SHT_RELA:
3749
84
      if (get_elf_backend_data (abfd)->may_use_rela_p)
3750
84
  this_hdr->sh_entsize = bed->s->sizeof_rela;
3751
84
      break;
3752
3753
5
     case SHT_REL:
3754
5
      if (get_elf_backend_data (abfd)->may_use_rel_p)
3755
3
  this_hdr->sh_entsize = bed->s->sizeof_rel;
3756
5
      break;
3757
3758
21
     case SHT_GNU_versym:
3759
21
      this_hdr->sh_entsize = sizeof (Elf_External_Versym);
3760
21
      break;
3761
3762
27
     case SHT_GNU_verdef:
3763
27
      this_hdr->sh_entsize = 0;
3764
      /* objcopy or strip will copy over sh_info, but may not set
3765
   cverdefs.  The linker will set cverdefs, but sh_info will be
3766
   zero.  */
3767
27
      if (this_hdr->sh_info == 0)
3768
0
  this_hdr->sh_info = elf_tdata (abfd)->cverdefs;
3769
27
      else
3770
27
  BFD_ASSERT (elf_tdata (abfd)->cverdefs == 0
3771
27
        || this_hdr->sh_info == elf_tdata (abfd)->cverdefs);
3772
27
      break;
3773
3774
25
    case SHT_GNU_verneed:
3775
25
      this_hdr->sh_entsize = 0;
3776
      /* objcopy or strip will copy over sh_info, but may not set
3777
   cverrefs.  The linker will set cverrefs, but sh_info will be
3778
   zero.  */
3779
25
      if (this_hdr->sh_info == 0)
3780
1
  this_hdr->sh_info = elf_tdata (abfd)->cverrefs;
3781
24
      else
3782
24
  BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0
3783
25
        || this_hdr->sh_info == elf_tdata (abfd)->cverrefs);
3784
25
      break;
3785
3786
9
    case SHT_GROUP:
3787
9
      this_hdr->sh_entsize = GRP_ENTRY_SIZE;
3788
9
      break;
3789
3790
13
    case SHT_GNU_HASH:
3791
13
      this_hdr->sh_entsize = bed->s->arch_size == 64 ? 0 : 4;
3792
13
      break;
3793
1.83k
    }
3794
3795
1.83k
  if ((asect->flags & SEC_ALLOC) != 0)
3796
1.02k
    this_hdr->sh_flags |= SHF_ALLOC;
3797
1.83k
  if ((asect->flags & SEC_READONLY) == 0)
3798
440
    this_hdr->sh_flags |= SHF_WRITE;
3799
1.83k
  if ((asect->flags & SEC_CODE) != 0)
3800
285
    this_hdr->sh_flags |= SHF_EXECINSTR;
3801
1.83k
  if ((asect->flags & SEC_MERGE) != 0)
3802
136
    {
3803
136
      this_hdr->sh_flags |= SHF_MERGE;
3804
136
      this_hdr->sh_entsize = asect->entsize;
3805
136
    }
3806
1.83k
  if ((asect->flags & SEC_STRINGS) != 0)
3807
132
    {
3808
132
      this_hdr->sh_flags |= SHF_STRINGS;
3809
132
      this_hdr->sh_entsize = asect->entsize;
3810
132
    }
3811
1.83k
  if ((asect->flags & SEC_GROUP) == 0 && elf_group_name (asect) != NULL)
3812
22
    this_hdr->sh_flags |= SHF_GROUP;
3813
1.83k
  if ((asect->flags & SEC_THREAD_LOCAL) != 0)
3814
59
    {
3815
59
      this_hdr->sh_flags |= SHF_TLS;
3816
59
      if (asect->size == 0
3817
16
    && (asect->flags & SEC_HAS_CONTENTS) == 0)
3818
4
  {
3819
4
    struct bfd_link_order *o = asect->map_tail.link_order;
3820
3821
4
    this_hdr->sh_size = 0;
3822
4
    if (o != NULL)
3823
0
      {
3824
0
        this_hdr->sh_size = o->offset + o->size;
3825
0
        if (this_hdr->sh_size != 0)
3826
0
    this_hdr->sh_type = SHT_NOBITS;
3827
0
      }
3828
4
  }
3829
59
    }
3830
1.83k
  if ((asect->flags & (SEC_GROUP | SEC_EXCLUDE)) == SEC_EXCLUDE)
3831
74
    this_hdr->sh_flags |= SHF_EXCLUDE;
3832
3833
1.83k
  if (this_hdr->sh_entsize == 0)
3834
1.36k
    this_hdr->sh_entsize = asect->entsize;
3835
3836
  /* If the section has relocs, set up a section header for the
3837
     SHT_REL[A] section.  If two relocation sections are required for
3838
     this section, it is up to the processor-specific back-end to
3839
     create the other.  */
3840
1.83k
  if ((asect->flags & SEC_RELOC) != 0)
3841
178
    {
3842
      /* When doing a relocatable link, create both REL and RELA sections if
3843
   needed.  */
3844
178
      if (arg->link_info
3845
    /* Do the normal setup if we wouldn't create any sections here.  */
3846
0
    && esd->rel.count + esd->rela.count > 0
3847
0
    && (bfd_link_relocatable (arg->link_info)
3848
0
        || arg->link_info->emitrelocations))
3849
0
  {
3850
0
    if (esd->rel.count && esd->rel.hdr == NULL
3851
0
        && !_bfd_elf_init_reloc_shdr (abfd, &esd->rel, name,
3852
0
              false, delay_sh_name_p))
3853
0
      {
3854
0
        arg->failed = true;
3855
0
        return;
3856
0
      }
3857
0
    if (esd->rela.count && esd->rela.hdr == NULL
3858
0
        && !_bfd_elf_init_reloc_shdr (abfd, &esd->rela, name,
3859
0
              true, delay_sh_name_p))
3860
0
      {
3861
0
        arg->failed = true;
3862
0
        return;
3863
0
      }
3864
0
  }
3865
178
      else if (!_bfd_elf_init_reloc_shdr (abfd,
3866
178
            (asect->use_rela_p
3867
178
             ? &esd->rela : &esd->rel),
3868
178
            name,
3869
178
            asect->use_rela_p,
3870
178
            delay_sh_name_p))
3871
0
  {
3872
0
    arg->failed = true;
3873
0
    return;
3874
0
  }
3875
178
    }
3876
3877
  /* Check for processor-specific section types.  */
3878
1.83k
  sh_type = this_hdr->sh_type;
3879
1.83k
  if (bed->elf_backend_fake_sections
3880
1.44k
      && !(*bed->elf_backend_fake_sections) (abfd, this_hdr, asect))
3881
0
    {
3882
0
      arg->failed = true;
3883
0
      return;
3884
0
    }
3885
3886
1.83k
  if (sh_type == SHT_NOBITS && asect->size != 0)
3887
55
    {
3888
      /* Don't change the header type from NOBITS if we are being
3889
   called for objcopy --only-keep-debug.  */
3890
55
      this_hdr->sh_type = sh_type;
3891
55
    }
3892
1.83k
}
3893
3894
/* Fill in the contents of a SHT_GROUP section.  Called from
3895
   _bfd_elf_compute_section_file_positions for gas, objcopy, and
3896
   when ELF targets use the generic linker, ld.  Called for ld -r
3897
   from bfd_elf_final_link.  */
3898
3899
void
3900
bfd_elf_set_group_contents (bfd *abfd, asection *sec, void *failedptrarg)
3901
1.83k
{
3902
1.83k
  bool *failedptr = (bool *) failedptrarg;
3903
1.83k
  asection *elt, *first;
3904
1.83k
  unsigned char *loc;
3905
1.83k
  bool gas;
3906
3907
  /* Ignore linker created group section.  See elfNN_ia64_object_p in
3908
     elfxx-ia64.c.  */
3909
1.83k
  if ((sec->flags & (SEC_GROUP | SEC_LINKER_CREATED)) != SEC_GROUP
3910
9
      || sec->size == 0
3911
9
      || *failedptr)
3912
1.82k
    return;
3913
3914
8
  if (elf_section_data (sec)->this_hdr.sh_info == 0)
3915
8
    {
3916
8
      unsigned long symindx = 0;
3917
3918
      /* elf_group_id will have been set up by objcopy and the
3919
   generic linker.  */
3920
8
      if (elf_group_id (sec) != NULL)
3921
8
  symindx = elf_group_id (sec)->udata.i;
3922
3923
8
      if (symindx == 0)
3924
0
  {
3925
    /* If called from the assembler, swap_out_syms will have set up
3926
       elf_section_syms.
3927
       PR 25699: A corrupt input file could contain bogus group info.  */
3928
0
    if (sec->index >= elf_num_section_syms (abfd)
3929
0
        || elf_section_syms (abfd)[sec->index] == NULL)
3930
0
      {
3931
0
        *failedptr = true;
3932
0
        return;
3933
0
      }
3934
0
    symindx = elf_section_syms (abfd)[sec->index]->udata.i;
3935
0
  }
3936
8
      elf_section_data (sec)->this_hdr.sh_info = symindx;
3937
8
    }
3938
0
  else if (elf_section_data (sec)->this_hdr.sh_info == (unsigned int) -2)
3939
0
    {
3940
      /* The ELF backend linker sets sh_info to -2 when the group
3941
   signature symbol is global, and thus the index can't be
3942
   set until all local symbols are output.  */
3943
0
      asection *igroup;
3944
0
      struct bfd_elf_section_data *sec_data;
3945
0
      unsigned long symndx;
3946
0
      unsigned long extsymoff;
3947
0
      struct elf_link_hash_entry *h;
3948
3949
      /* The point of this little dance to the first SHF_GROUP section
3950
   then back to the SHT_GROUP section is that this gets us to
3951
   the SHT_GROUP in the input object.  */
3952
0
      igroup = elf_sec_group (elf_next_in_group (sec));
3953
0
      sec_data = elf_section_data (igroup);
3954
0
      symndx = sec_data->this_hdr.sh_info;
3955
0
      extsymoff = 0;
3956
0
      if (!elf_bad_symtab (igroup->owner))
3957
0
  {
3958
0
    Elf_Internal_Shdr *symtab_hdr;
3959
3960
0
    symtab_hdr = &elf_symtab_hdr (igroup->owner);
3961
0
    extsymoff = symtab_hdr->sh_info;
3962
0
  }
3963
0
      h = elf_sym_hashes (igroup->owner)[symndx - extsymoff];
3964
0
      while (h->root.type == bfd_link_hash_indirect
3965
0
       || h->root.type == bfd_link_hash_warning)
3966
0
  h = (struct elf_link_hash_entry *) h->root.u.i.link;
3967
3968
0
      elf_section_data (sec)->this_hdr.sh_info = h->indx;
3969
0
    }
3970
3971
  /* The contents won't be allocated for "ld -r" or objcopy.  */
3972
8
  gas = true;
3973
8
  if (sec->contents == NULL)
3974
8
    {
3975
8
      gas = false;
3976
8
      sec->contents = bfd_alloc (abfd, sec->size);
3977
3978
      /* Arrange for the section to be written out.  */
3979
8
      elf_section_data (sec)->this_hdr.contents = sec->contents;
3980
8
      if (sec->contents == NULL)
3981
0
  {
3982
0
    *failedptr = true;
3983
0
    return;
3984
0
  }
3985
8
      sec->alloced = 1;
3986
8
    }
3987
3988
8
  loc = sec->contents + sec->size;
3989
3990
  /* Get the pointer to the first section in the group that gas
3991
     squirreled away here.  objcopy arranges for this to be set to the
3992
     start of the input section group.  */
3993
8
  first = elt = elf_next_in_group (sec);
3994
3995
  /* First element is a flag word.  Rest of section is elf section
3996
     indices for all the sections of the group.  Write them backwards
3997
     just to keep the group in the same order as given in .section
3998
     directives, not that it matters.  */
3999
20
  while (elt != NULL)
4000
20
    {
4001
20
      asection *s;
4002
4003
20
      s = elt;
4004
20
      if (!gas)
4005
20
  s = s->output_section;
4006
20
      if (s != NULL
4007
20
    && !bfd_is_abs_section (s))
4008
20
  {
4009
20
    struct bfd_elf_section_data *elf_sec = elf_section_data (s);
4010
20
    struct bfd_elf_section_data *input_elf_sec = elf_section_data (elt);
4011
4012
20
    if (elf_sec->rel.hdr != NULL
4013
0
        && (gas
4014
0
      || (input_elf_sec->rel.hdr != NULL
4015
0
          && input_elf_sec->rel.hdr->sh_flags & SHF_GROUP) != 0))
4016
0
      {
4017
0
        elf_sec->rel.hdr->sh_flags |= SHF_GROUP;
4018
0
        loc -= 4;
4019
0
        if (loc == sec->contents)
4020
0
    break;
4021
0
        H_PUT_32 (abfd, elf_sec->rel.idx, loc);
4022
0
      }
4023
20
    if (elf_sec->rela.hdr != NULL
4024
16
        && (gas
4025
16
      || (input_elf_sec->rela.hdr != NULL
4026
16
          && input_elf_sec->rela.hdr->sh_flags & SHF_GROUP) != 0))
4027
16
      {
4028
16
        elf_sec->rela.hdr->sh_flags |= SHF_GROUP;
4029
16
        loc -= 4;
4030
16
        if (loc == sec->contents)
4031
0
    break;
4032
16
        H_PUT_32 (abfd, elf_sec->rela.idx, loc);
4033
16
      }
4034
20
    loc -= 4;
4035
20
    if (loc == sec->contents)
4036
0
      break;
4037
20
    H_PUT_32 (abfd, elf_sec->this_idx, loc);
4038
20
  }
4039
20
      elt = elf_next_in_group (elt);
4040
20
      if (elt == first)
4041
8
  break;
4042
20
    }
4043
4044
  /* We should always get here with loc == sec->contents + 4.  Return
4045
     an error for bogus SHT_GROUP sections.  */
4046
8
  loc -= 4;
4047
8
  if (loc != sec->contents)
4048
1
    {
4049
      /* xgettext:c-format */
4050
1
      _bfd_error_handler (_("%pB: corrupted group section: `%pA'"),
4051
1
        abfd, sec);
4052
1
      bfd_set_error (bfd_error_bad_value);
4053
1
      *failedptr = true;
4054
1
      return;
4055
1
    }
4056
4057
7
  H_PUT_32 (abfd, sec->flags & SEC_LINK_ONCE ? GRP_COMDAT : 0, loc);
4058
7
}
4059
4060
/* Given NAME, the name of a relocation section stripped of its
4061
   .rel/.rela prefix, return the section in ABFD to which the
4062
   relocations apply.  */
4063
4064
asection *
4065
_bfd_elf_plt_get_reloc_section (bfd *abfd, const char *name)
4066
54
{
4067
  /* If a target needs .got.plt section, relocations in rela.plt/rel.plt
4068
     section likely apply to .got.plt or .got section.  */
4069
54
  if (get_elf_backend_data (abfd)->want_got_plt
4070
37
      && streq (name, ".plt"))
4071
7
    {
4072
7
      asection *sec;
4073
4074
7
      name = ".got.plt";
4075
7
      sec = bfd_get_section_by_name (abfd, name);
4076
7
      if (sec != NULL)
4077
7
  return sec;
4078
0
      name = ".got";
4079
0
    }
4080
4081
47
  return bfd_get_section_by_name (abfd, name);
4082
54
}
4083
4084
/* Return the section to which RELOC_SEC applies.  */
4085
4086
static asection *
4087
elf_get_reloc_section (asection *reloc_sec)
4088
89
{
4089
89
  const char *name;
4090
89
  unsigned int type;
4091
89
  bfd *abfd;
4092
89
  elf_backend_data *bed;
4093
4094
89
  type = elf_section_data (reloc_sec)->this_hdr.sh_type;
4095
89
  if (type != SHT_REL && type != SHT_RELA)
4096
0
    return NULL;
4097
4098
  /* We look up the section the relocs apply to by name.  */
4099
89
  name = reloc_sec->name;
4100
89
  if (!startswith (name, ".rel"))
4101
27
    return NULL;
4102
62
  name += 4;
4103
62
  if (type == SHT_RELA && *name++ != 'a')
4104
0
    return NULL;
4105
4106
62
  abfd = reloc_sec->owner;
4107
62
  bed = get_elf_backend_data (abfd);
4108
62
  return bed->get_reloc_section (abfd, name);
4109
62
}
4110
4111
/* Assign all ELF section numbers.  The dummy first section is handled here
4112
   too.  The link/info pointers for the standard section types are filled
4113
   in here too, while we're at it.  LINK_INFO will be 0 when arriving
4114
   here for gas, objcopy, and when using the generic ELF linker.  */
4115
4116
static bool
4117
assign_section_numbers (bfd *abfd, struct bfd_link_info *link_info)
4118
574
{
4119
574
  struct elf_obj_tdata *t = elf_tdata (abfd);
4120
574
  asection *sec;
4121
574
  unsigned int section_number;
4122
574
  Elf_Internal_Shdr **i_shdrp;
4123
574
  struct bfd_elf_section_data *d;
4124
574
  bool need_symtab;
4125
4126
574
  section_number = 1;
4127
4128
574
  _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd));
4129
4130
  /* SHT_GROUP sections are in relocatable files only.  */
4131
574
  if (link_info == NULL || !link_info->resolve_section_groups)
4132
574
    {
4133
574
      size_t reloc_count = 0;
4134
4135
      /* Put SHT_GROUP sections first.  */
4136
2.40k
      for (sec = abfd->sections; sec != NULL; sec = sec->next)
4137
1.83k
  {
4138
1.83k
    d = elf_section_data (sec);
4139
4140
1.83k
    if (d->this_hdr.sh_type == SHT_GROUP)
4141
9
      {
4142
9
        if (sec->flags & SEC_LINKER_CREATED)
4143
0
    {
4144
      /* Remove the linker created SHT_GROUP sections.  */
4145
0
      bfd_section_list_remove (abfd, sec);
4146
0
      abfd->section_count--;
4147
0
    }
4148
9
        else
4149
9
    d->this_idx = section_number++;
4150
9
      }
4151
4152
    /* Count relocations.  */
4153
1.83k
    reloc_count += sec->reloc_count;
4154
1.83k
  }
4155
4156
      /* Set/clear HAS_RELOC depending on whether there are relocations.  */
4157
574
      if (reloc_count == 0)
4158
538
  abfd->flags &= ~HAS_RELOC;
4159
36
      else
4160
36
  abfd->flags |= HAS_RELOC;
4161
574
    }
4162
4163
2.40k
  for (sec = abfd->sections; sec; sec = sec->next)
4164
1.83k
    {
4165
1.83k
      d = elf_section_data (sec);
4166
4167
1.83k
      if (d->this_hdr.sh_type != SHT_GROUP)
4168
1.82k
  d->this_idx = section_number++;
4169
1.83k
      if (d->this_hdr.sh_name != (unsigned int) -1)
4170
1.83k
  _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->this_hdr.sh_name);
4171
1.83k
      if (d->rel.hdr)
4172
0
  {
4173
0
    d->rel.idx = section_number++;
4174
0
    if (d->rel.hdr->sh_name != (unsigned int) -1)
4175
0
      _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel.hdr->sh_name);
4176
0
  }
4177
1.83k
      else
4178
1.83k
  d->rel.idx = 0;
4179
4180
1.83k
      if (d->rela.hdr)
4181
178
  {
4182
178
    d->rela.idx = section_number++;
4183
178
    if (d->rela.hdr->sh_name != (unsigned int) -1)
4184
178
      _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rela.hdr->sh_name);
4185
178
  }
4186
1.65k
      else
4187
1.65k
  d->rela.idx = 0;
4188
1.83k
    }
4189
4190
574
  need_symtab = (bfd_get_symcount (abfd) > 0
4191
500
     || (link_info == NULL
4192
500
         && ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC))
4193
500
       == HAS_RELOC)));
4194
574
  if (need_symtab)
4195
74
    {
4196
74
      elf_onesymtab (abfd) = section_number++;
4197
74
      _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->symtab_hdr.sh_name);
4198
74
      if (section_number > ((SHN_LORESERVE - 2) & 0xFFFF))
4199
0
  {
4200
0
    elf_section_list *entry;
4201
4202
0
    BFD_ASSERT (elf_symtab_shndx_list (abfd) == NULL);
4203
4204
0
    entry = bfd_zalloc (abfd, sizeof (*entry));
4205
0
    if (entry == NULL)
4206
0
      return false;
4207
0
    entry->ndx = section_number++;
4208
0
    elf_symtab_shndx_list (abfd) = entry;
4209
0
    entry->hdr.sh_name = _bfd_elf_strtab_add (elf_shstrtab (abfd),
4210
0
                ".symtab_shndx", false);
4211
0
    if (entry->hdr.sh_name == -1u)
4212
0
      return false;
4213
0
  }
4214
74
      elf_strtab_sec (abfd) = section_number++;
4215
74
      _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->strtab_hdr.sh_name);
4216
74
    }
4217
4218
574
  elf_shstrtab_sec (abfd) = section_number++;
4219
574
  _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->shstrtab_hdr.sh_name);
4220
574
  elf_elfheader (abfd)->e_shstrndx = elf_shstrtab_sec (abfd);
4221
4222
574
  if (section_number >= SHN_LORESERVE)
4223
0
    {
4224
      /* xgettext:c-format */
4225
0
      _bfd_error_handler (_("%pB: too many sections: %u"),
4226
0
        abfd, section_number);
4227
0
      return false;
4228
0
    }
4229
4230
574
  elf_numsections (abfd) = section_number;
4231
574
  elf_elfheader (abfd)->e_shnum = section_number;
4232
4233
  /* Set up the list of section header pointers, in agreement with the
4234
     indices.  */
4235
574
  i_shdrp = bfd_zalloc (abfd, section_number * sizeof (*i_shdrp));
4236
574
  if (i_shdrp == NULL)
4237
0
    return false;
4238
4239
574
  i_shdrp[0] = bfd_zalloc (abfd, sizeof (*i_shdrp[0]));
4240
574
  if (i_shdrp[0] == NULL)
4241
0
    {
4242
0
      bfd_release (abfd, i_shdrp);
4243
0
      return false;
4244
0
    }
4245
4246
574
  elf_elfsections (abfd) = i_shdrp;
4247
4248
574
  i_shdrp[elf_shstrtab_sec (abfd)] = &t->shstrtab_hdr;
4249
574
  if (need_symtab)
4250
74
    {
4251
74
      i_shdrp[elf_onesymtab (abfd)] = &t->symtab_hdr;
4252
74
      if (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF))
4253
0
  {
4254
0
    elf_section_list * entry = elf_symtab_shndx_list (abfd);
4255
0
    BFD_ASSERT (entry != NULL);
4256
0
    i_shdrp[entry->ndx] = & entry->hdr;
4257
0
    entry->hdr.sh_link = elf_onesymtab (abfd);
4258
0
  }
4259
74
      i_shdrp[elf_strtab_sec (abfd)] = &t->strtab_hdr;
4260
74
      t->symtab_hdr.sh_link = elf_strtab_sec (abfd);
4261
74
    }
4262
4263
2.40k
  for (sec = abfd->sections; sec; sec = sec->next)
4264
1.83k
    {
4265
1.83k
      asection *s;
4266
4267
1.83k
      d = elf_section_data (sec);
4268
4269
1.83k
      i_shdrp[d->this_idx] = &d->this_hdr;
4270
1.83k
      if (d->rel.idx != 0)
4271
0
  i_shdrp[d->rel.idx] = d->rel.hdr;
4272
1.83k
      if (d->rela.idx != 0)
4273
178
  i_shdrp[d->rela.idx] = d->rela.hdr;
4274
4275
      /* Fill in the sh_link and sh_info fields while we're at it.  */
4276
4277
      /* sh_link of a reloc section is the section index of the symbol
4278
   table.  sh_info is the section index of the section to which
4279
   the relocation entries apply.  */
4280
1.83k
      if (d->rel.idx != 0)
4281
0
  {
4282
0
    d->rel.hdr->sh_link = elf_onesymtab (abfd);
4283
0
    d->rel.hdr->sh_info = d->this_idx;
4284
0
    d->rel.hdr->sh_flags |= SHF_INFO_LINK;
4285
0
  }
4286
1.83k
      if (d->rela.idx != 0)
4287
178
  {
4288
178
    d->rela.hdr->sh_link = elf_onesymtab (abfd);
4289
178
    d->rela.hdr->sh_info = d->this_idx;
4290
178
    d->rela.hdr->sh_flags |= SHF_INFO_LINK;
4291
178
  }
4292
4293
      /* We need to set up sh_link for SHF_LINK_ORDER.  */
4294
1.83k
      if ((d->this_hdr.sh_flags & SHF_LINK_ORDER) != 0)
4295
30
  {
4296
30
    s = elf_linked_to_section (sec);
4297
    /* We can now have a NULL linked section pointer.
4298
       This happens when the sh_link field is 0, which is done
4299
       when a linked to section is discarded but the linking
4300
       section has been retained for some reason.  */
4301
30
    if (s)
4302
4
      {
4303
        /* Check discarded linkonce section.  */
4304
4
        if (discarded_section (s))
4305
0
    {
4306
0
      asection *kept;
4307
0
      _bfd_error_handler
4308
        /* xgettext:c-format */
4309
0
        (_("%pB: sh_link of section `%pA' points to"
4310
0
           " discarded section `%pA' of `%pB'"),
4311
0
         abfd, d->this_hdr.bfd_section, s, s->owner);
4312
      /* Point to the kept section if it has the same
4313
         size as the discarded one.  */
4314
0
      kept = _bfd_elf_check_kept_section (s, link_info);
4315
0
      if (kept == NULL)
4316
0
        {
4317
0
          bfd_set_error (bfd_error_bad_value);
4318
0
          return false;
4319
0
        }
4320
0
      s = kept;
4321
0
    }
4322
        /* Handle objcopy. */
4323
4
        else if (s->output_section == NULL)
4324
0
    {
4325
0
      _bfd_error_handler
4326
        /* xgettext:c-format */
4327
0
        (_("%pB: sh_link of section `%pA' points to"
4328
0
           " removed section `%pA' of `%pB'"),
4329
0
         abfd, d->this_hdr.bfd_section, s, s->owner);
4330
0
      bfd_set_error (bfd_error_bad_value);
4331
0
      return false;
4332
0
    }
4333
4
        s = s->output_section;
4334
4
        d->this_hdr.sh_link
4335
4
    = _bfd_elf_section_from_bfd_section (abfd, s);
4336
4
      }
4337
30
  }
4338
4339
1.83k
      switch (d->this_hdr.sh_type)
4340
1.83k
  {
4341
5
  case SHT_REL:
4342
89
  case SHT_RELA:
4343
    /* sh_link is the section index of the symbol table.
4344
       sh_info is the section index of the section to which the
4345
       relocation entries apply.  */
4346
89
    if (d->this_hdr.sh_link == 0)
4347
89
      {
4348
        /* FIXME maybe: If this is a reloc section which we are
4349
     treating as a normal section then we likely should
4350
     not be assuming its sh_link is .dynsym or .symtab.  */
4351
89
        if ((sec->flags & SEC_ALLOC) != 0)
4352
32
    {
4353
32
      s = bfd_get_section_by_name (abfd, ".dynsym");
4354
32
      if (s != NULL)
4355
16
        d->this_hdr.sh_link = elf_section_data (s)->this_idx;
4356
32
    }
4357
57
        else
4358
57
    d->this_hdr.sh_link = elf_onesymtab (abfd);
4359
89
      }
4360
4361
89
    s = elf_get_reloc_section (sec);
4362
89
    if (s != NULL)
4363
45
      {
4364
45
        d->this_hdr.sh_info = elf_section_data (s)->this_idx;
4365
45
        d->this_hdr.sh_flags |= SHF_INFO_LINK;
4366
45
      }
4367
89
    break;
4368
4369
51
  case SHT_STRTAB:
4370
    /* We assume that a section named .stab*str is a stabs
4371
       string section.  We look for a section with the same name
4372
       but without the trailing ``str'', and set its sh_link
4373
       field to point to this section.  */
4374
51
    if (startswith (sec->name, ".stab")
4375
0
        && streq (sec->name + strlen (sec->name) - 3, "str"))
4376
0
      {
4377
0
        size_t len;
4378
0
        char *alc;
4379
4380
0
        len = strlen (sec->name);
4381
0
        alc = bfd_malloc (len - 2);
4382
0
        if (alc == NULL)
4383
0
    return false;
4384
0
        memcpy (alc, sec->name, len - 3);
4385
0
        alc[len - 3] = '\0';
4386
0
        s = bfd_get_section_by_name (abfd, alc);
4387
0
        free (alc);
4388
0
        if (s != NULL)
4389
0
    {
4390
0
      elf_section_data (s)->this_hdr.sh_link = d->this_idx;
4391
4392
      /* This is a .stab section.  */
4393
0
      elf_section_data (s)->this_hdr.sh_entsize = 12;
4394
0
    }
4395
0
      }
4396
51
    break;
4397
4398
51
  case SHT_DYNAMIC:
4399
70
  case SHT_DYNSYM:
4400
95
  case SHT_GNU_verneed:
4401
122
  case SHT_GNU_verdef:
4402
    /* sh_link is the section header index of the string table
4403
       used for the dynamic entries, or the symbol table, or the
4404
       version strings.  */
4405
122
    s = bfd_get_section_by_name (abfd, ".dynstr");
4406
122
    if (s != NULL)
4407
55
      d->this_hdr.sh_link = elf_section_data (s)->this_idx;
4408
122
    break;
4409
4410
0
  case SHT_GNU_LIBLIST:
4411
    /* sh_link is the section header index of the prelink library
4412
       list used for the dynamic entries, or the symbol table, or
4413
       the version strings.  */
4414
0
    s = bfd_get_section_by_name (abfd, ((sec->flags & SEC_ALLOC)
4415
0
                ? ".dynstr" : ".gnu.libstr"));
4416
0
    if (s != NULL)
4417
0
      d->this_hdr.sh_link = elf_section_data (s)->this_idx;
4418
0
    break;
4419
4420
17
  case SHT_HASH:
4421
30
  case SHT_GNU_HASH:
4422
51
  case SHT_GNU_versym:
4423
    /* sh_link is the section header index of the symbol table
4424
       this hash table or version table is for.  */
4425
51
    s = bfd_get_section_by_name (abfd, ".dynsym");
4426
51
    if (s != NULL)
4427
16
      d->this_hdr.sh_link = elf_section_data (s)->this_idx;
4428
51
    break;
4429
4430
9
  case SHT_GROUP:
4431
9
    d->this_hdr.sh_link = elf_onesymtab (abfd);
4432
1.83k
  }
4433
1.83k
    }
4434
4435
  /* Delay setting sh_name to _bfd_elf_write_object_contents so that
4436
     _bfd_elf_assign_file_positions_for_non_load can convert DWARF
4437
     debug section name from .debug_* to .zdebug_* if needed.  */
4438
4439
574
  return true;
4440
574
}
4441
4442
static bool
4443
sym_is_global (bfd *abfd, asymbol *sym)
4444
2.78k
{
4445
  /* If the backend has a special mapping, use it.  */
4446
2.78k
  elf_backend_data *bed = get_elf_backend_data (abfd);
4447
2.78k
  if (bed->elf_backend_sym_is_global)
4448
20
    return (*bed->elf_backend_sym_is_global) (abfd, sym);
4449
4450
2.76k
  return ((sym->flags & (BSF_GLOBAL | BSF_WEAK | BSF_GNU_UNIQUE)) != 0
4451
2.14k
    || bfd_is_und_section (bfd_asymbol_section (sym))
4452
1.04k
    || bfd_is_com_section (bfd_asymbol_section (sym)));
4453
2.78k
}
4454
4455
/* Filter global symbols of INFO->OUTPUT_BFD to include in the import
4456
   library.  SYMCOUNT symbols can be examined from their pointers in
4457
   SYMS.  Pointers of symbols to keep should be stored contiguously at
4458
   the beginning of that array.
4459
4460
   Returns the number of symbols to keep.  */
4461
4462
size_t
4463
_bfd_elf_filter_implib_symbols (struct bfd_link_info *info,
4464
        asymbol **syms, size_t symcount)
4465
0
{
4466
0
  size_t src_count, dst_count = 0;
4467
4468
0
  for (src_count = 0; src_count < symcount; src_count++)
4469
0
    {
4470
0
      asymbol *sym = syms[src_count];
4471
0
      const char *name = bfd_asymbol_name (sym);
4472
0
      struct bfd_link_hash_entry *h;
4473
4474
0
      if (!sym_is_global (info->output_bfd, sym))
4475
0
  continue;
4476
4477
0
      h = bfd_link_hash_lookup (info->hash, name, false, false, false);
4478
0
      if (h == NULL)
4479
0
  continue;
4480
0
      if (h->type != bfd_link_hash_defined && h->type != bfd_link_hash_defweak)
4481
0
  continue;
4482
0
      if (h->linker_def || h->ldscript_def)
4483
0
  continue;
4484
4485
0
      syms[dst_count++] = sym;
4486
0
    }
4487
4488
0
  syms[dst_count] = NULL;
4489
4490
0
  return dst_count;
4491
0
}
4492
4493
/* Don't output symbols for sections that are not going to be output,
4494
   that are duplicates or there is no BFD section.  */
4495
4496
static bool
4497
ignore_sym (asymbol *sym)
4498
4.88k
{
4499
4.88k
  if (sym == NULL)
4500
0
    return false;
4501
4502
4.88k
  if (sym->section == NULL)
4503
0
    return true;
4504
4505
4.88k
  if ((sym->flags & BSF_SECTION_SYM) != 0)
4506
2.44k
    {
4507
2.44k
      if ((sym->flags & BSF_SECTION_SYM_USED) == 0)
4508
1.96k
  return true;
4509
      /* With ld -r on generic elf targets it is possible to have
4510
   multiple section symbols in the output for a given section.
4511
   We'd like to get rid of all but the first one.  This drops
4512
   them if the first input section is non-zero size, but fails
4513
   to do so if the first input section is zero sized.  */
4514
482
      if (sym->section->output_offset != 0)
4515
0
  return true;
4516
482
    }
4517
4518
2.92k
  return discarded_section (sym->section);
4519
4.88k
}
4520
4521
/* Map symbol from it's internal number to the external number, moving
4522
   all local symbols to be at the head of the list.  */
4523
4524
static bool
4525
elf_map_symbols (bfd *abfd, unsigned int *pnum_locals)
4526
74
{
4527
74
  unsigned int symcount = bfd_get_symcount (abfd);
4528
74
  asymbol **syms = bfd_get_outsymbols (abfd);
4529
74
  asymbol **sect_syms;
4530
74
  unsigned int num_locals = 0;
4531
74
  unsigned int num_globals = 0;
4532
74
  unsigned int max_index = 0;
4533
74
  unsigned int idx;
4534
74
  asection *asect;
4535
74
  asymbol **new_syms;
4536
4537
#ifdef DEBUG
4538
  fprintf (stderr, "elf_map_symbols\n");
4539
  fflush (stderr);
4540
#endif
4541
4542
947
  for (asect = abfd->sections; asect; asect = asect->next)
4543
873
    {
4544
873
      if (max_index < asect->index)
4545
799
  max_index = asect->index;
4546
873
    }
4547
4548
74
  max_index++;
4549
74
  sect_syms = bfd_zalloc (abfd, max_index * sizeof (*sect_syms));
4550
74
  if (sect_syms == NULL)
4551
0
    return false;
4552
74
  elf_section_syms (abfd) = sect_syms;
4553
74
  elf_num_section_syms (abfd) = max_index;
4554
4555
  /* Init sect_syms entries for any section symbols we have already
4556
     decided to output.  */
4557
1.54k
  for (idx = 0; idx < symcount; idx++)
4558
1.47k
    {
4559
1.47k
      asymbol *sym = syms[idx];
4560
4561
1.47k
      if ((sym->flags & BSF_SECTION_SYM) != 0
4562
250
    && sym->value == 0
4563
197
    && !ignore_sym (sym)
4564
102
    && !bfd_is_abs_section (sym->section))
4565
94
  {
4566
94
    asection *sec = sym->section;
4567
4568
94
    if (sec->owner != abfd)
4569
94
      {
4570
94
        sec = sec->output_section;
4571
94
        if (sec == NULL)
4572
0
    return false;
4573
94
      }
4574
4575
94
    sect_syms[sec->index] = syms[idx];
4576
94
  }
4577
1.47k
    }
4578
4579
  /* Classify all of the symbols.  */
4580
1.54k
  for (idx = 0; idx < symcount; idx++)
4581
1.47k
    {
4582
1.47k
      if (ignore_sym (syms[idx]))
4583
115
  continue;
4584
1.35k
      if (sym_is_global (abfd, syms[idx]))
4585
874
  num_globals++;
4586
482
      else
4587
482
  num_locals++;
4588
1.35k
    }
4589
4590
  /* We will be adding a section symbol for each normal BFD section.  Most
4591
     sections will already have a section symbol in outsymbols, but
4592
     eg. SHT_GROUP sections will not, and we need the section symbol mapped
4593
     at least in that case.  */
4594
947
  for (asect = abfd->sections; asect; asect = asect->next)
4595
873
    {
4596
873
      asymbol *sym = asect->symbol;
4597
      /* Don't include ignored section symbols.  */
4598
873
      if (!ignore_sym (sym)
4599
55
    && sect_syms[asect->index] == NULL)
4600
38
  {
4601
38
    if (sym_is_global (abfd, asect->symbol))
4602
0
      num_globals++;
4603
38
    else
4604
38
      num_locals++;
4605
38
  }
4606
873
    }
4607
4608
  /* Now sort the symbols so the local symbols are first.  */
4609
74
  new_syms = bfd_alloc (abfd, (num_locals + num_globals) * sizeof (*new_syms));
4610
74
  if (new_syms == NULL)
4611
0
    return false;
4612
4613
74
  unsigned int num_globals2 = 0;
4614
74
  unsigned int num_locals2 = 0;
4615
1.54k
  for (idx = 0; idx < symcount; idx++)
4616
1.47k
    {
4617
1.47k
      asymbol *sym = syms[idx];
4618
1.47k
      unsigned int i;
4619
4620
1.47k
      if (ignore_sym (sym))
4621
115
  continue;
4622
4623
1.35k
      if (sym_is_global (abfd, sym))
4624
874
  i = num_locals + num_globals2++;
4625
482
      else
4626
482
  i = num_locals2++;
4627
1.35k
      new_syms[i] = sym;
4628
1.35k
      sym->udata.i = i + 1;
4629
1.35k
    }
4630
947
  for (asect = abfd->sections; asect; asect = asect->next)
4631
873
    {
4632
873
      asymbol *sym = asect->symbol;
4633
873
      if (!ignore_sym (sym)
4634
55
    && sect_syms[asect->index] == NULL)
4635
38
  {
4636
38
    unsigned int i;
4637
4638
38
    sect_syms[asect->index] = sym;
4639
38
    if (sym_is_global (abfd, sym))
4640
0
      i = num_locals + num_globals2++;
4641
38
    else
4642
38
      i = num_locals2++;
4643
38
    new_syms[i] = sym;
4644
38
    sym->udata.i = i + 1;
4645
38
  }
4646
873
    }
4647
4648
74
  bfd_set_symtab (abfd, new_syms, num_locals + num_globals);
4649
4650
74
  *pnum_locals = num_locals;
4651
74
  return true;
4652
74
}
4653
4654
/* Assign a file position to a section, optionally aligning to the
4655
   required section alignment.  */
4656
4657
file_ptr
4658
_bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr *i_shdrp,
4659
             file_ptr offset,
4660
             bool align,
4661
             unsigned char log_file_align)
4662
2.59k
{
4663
2.59k
  if (i_shdrp->sh_addralign > 1)
4664
940
    {
4665
940
      file_ptr salign = i_shdrp->sh_addralign & -i_shdrp->sh_addralign;
4666
4667
940
      if (align)
4668
256
  offset = BFD_ALIGN (offset, salign);
4669
684
      else if (log_file_align)
4670
684
  {
4671
    /* Heuristic: Cap alignment at log_file_align.  */
4672
684
    file_ptr falign = 1u << log_file_align;
4673
4674
684
    offset = BFD_ALIGN (offset, salign < falign ? salign : falign);
4675
684
  }
4676
940
    }
4677
2.59k
  i_shdrp->sh_offset = offset;
4678
2.59k
  if (i_shdrp->bfd_section != NULL)
4679
1.24k
    i_shdrp->bfd_section->filepos = offset;
4680
2.59k
  if (i_shdrp->sh_type != SHT_NOBITS)
4681
2.58k
    offset += i_shdrp->sh_size;
4682
2.59k
  return offset;
4683
2.59k
}
4684
4685
/* Compute the file positions we are going to put the sections at, and
4686
   otherwise prepare to begin writing out the ELF file.  If LINK_INFO
4687
   is not NULL, this is being called by the ELF backend linker.  */
4688
4689
bool
4690
_bfd_elf_compute_section_file_positions (bfd *abfd,
4691
           struct bfd_link_info *link_info)
4692
574
{
4693
574
  elf_backend_data *bed = get_elf_backend_data (abfd);
4694
574
  struct fake_section_arg fsargs;
4695
574
  bool failed;
4696
574
  struct elf_strtab_hash *strtab = NULL;
4697
574
  Elf_Internal_Shdr *shstrtab_hdr;
4698
574
  bool need_symtab;
4699
4700
574
  if (abfd->output_has_begun)
4701
0
    return true;
4702
4703
  /* Do any elf backend specific processing first.  */
4704
574
  if (bed->elf_backend_begin_write_processing)
4705
60
    (*bed->elf_backend_begin_write_processing) (abfd, link_info);
4706
4707
574
  if (!(*bed->elf_backend_init_file_header) (abfd, link_info))
4708
0
    return false;
4709
4710
574
  fsargs.failed = false;
4711
574
  fsargs.link_info = link_info;
4712
574
  bfd_map_over_sections (abfd, elf_fake_sections, &fsargs);
4713
574
  if (fsargs.failed)
4714
0
    return false;
4715
4716
574
  if (!assign_section_numbers (abfd, link_info))
4717
0
    return false;
4718
4719
  /* The backend linker builds symbol table information itself.  */
4720
574
  need_symtab = (link_info == NULL
4721
574
     && (bfd_get_symcount (abfd) > 0
4722
500
         || ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC))
4723
500
       == HAS_RELOC)));
4724
574
  if (need_symtab)
4725
74
    {
4726
      /* Non-zero if doing a relocatable link.  */
4727
74
      int relocatable_p = ! (abfd->flags & (EXEC_P | DYNAMIC));
4728
4729
74
      if (! swap_out_syms (abfd, &strtab, relocatable_p, link_info))
4730
0
  return false;
4731
74
    }
4732
4733
574
  failed = false;
4734
574
  if (link_info == NULL)
4735
574
    {
4736
574
      bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
4737
574
      if (failed)
4738
1
  goto err_free_strtab;
4739
574
    }
4740
4741
573
  shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr;
4742
  /* sh_name was set in init_file_header.  */
4743
573
  shstrtab_hdr->sh_type = SHT_STRTAB;
4744
  /* sh_flags, sh_addr, sh_entsize, sh_link, sh_info are all zeroed
4745
     when tdata is allocated.  */
4746
  /* sh_size is set in _bfd_elf_assign_file_positions_for_non_load.  */
4747
  /* sh_offset is set in _bfd_elf_assign_file_positions_for_non_load.  */
4748
573
  shstrtab_hdr->sh_addralign = 1;
4749
4750
573
  if (!assign_file_positions_except_relocs (abfd, link_info))
4751
31
    goto err_free_strtab;
4752
4753
542
  if (strtab != NULL)
4754
64
    {
4755
64
      file_ptr off;
4756
64
      Elf_Internal_Shdr *hdr;
4757
4758
64
      off = elf_next_file_pos (abfd);
4759
4760
64
      hdr = & elf_symtab_hdr (abfd);
4761
64
      off = _bfd_elf_assign_file_position_for_section (hdr, off, true, 0);
4762
4763
64
      if (elf_symtab_shndx_list (abfd) != NULL)
4764
0
  {
4765
0
    hdr = & elf_symtab_shndx_list (abfd)->hdr;
4766
0
    if (hdr->sh_size != 0)
4767
0
      off = _bfd_elf_assign_file_position_for_section (hdr, off, true, 0);
4768
    /* FIXME: What about other symtab_shndx sections in the list ?  */
4769
0
  }
4770
4771
64
      hdr = &elf_tdata (abfd)->strtab_hdr;
4772
64
      off = _bfd_elf_assign_file_position_for_section (hdr, off, true, 0);
4773
4774
64
      elf_next_file_pos (abfd) = off;
4775
4776
      /* Now that we know where the .strtab section goes, write it
4777
   out.  */
4778
64
      if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
4779
64
    || ! _bfd_elf_strtab_emit (abfd, strtab))
4780
0
  goto err_free_strtab;
4781
64
      _bfd_elf_strtab_free (strtab);
4782
64
    }
4783
4784
542
  abfd->output_has_begun = true;
4785
542
  return true;
4786
4787
32
 err_free_strtab:
4788
32
  if (strtab != NULL)
4789
10
    _bfd_elf_strtab_free (strtab);
4790
32
  return false;
4791
542
}
4792
4793
/* Retrieve .eh_frame_hdr.  Prior to size_dynamic_sections the
4794
   function effectively returns whether --eh-frame-hdr is given on the
4795
   command line.  After size_dynamic_sections the result reflects
4796
   whether .eh_frame_hdr will actually be output (sizing isn't done
4797
   until ldemul_after_allocation).  */
4798
4799
static asection *
4800
elf_eh_frame_hdr (const struct bfd_link_info *info)
4801
58
{
4802
58
  if (info != NULL && is_elf_hash_table (info->hash))
4803
0
    return elf_hash_table (info)->eh_info.hdr_sec;
4804
58
  return NULL;
4805
58
}
4806
4807
/* Make an initial estimate of the size of the program header.  If we
4808
   get the number wrong here, we'll redo section placement.  */
4809
4810
static bfd_size_type
4811
get_program_header_size (bfd *abfd, struct bfd_link_info *info)
4812
32
{
4813
32
  size_t segs;
4814
32
  asection *s;
4815
32
  elf_backend_data *bed;
4816
4817
  /* Assume we will need exactly two PT_LOAD segments: one for text
4818
     and one for data.  */
4819
32
  segs = 2;
4820
4821
32
  s = bfd_get_section_by_name (abfd, ".interp");
4822
32
  if (s != NULL && (s->flags & SEC_LOAD) != 0 && s->size != 0)
4823
1
    {
4824
      /* If we have a loadable interpreter section, we need a
4825
   PT_INTERP segment.  In this case, assume we also need a
4826
   PT_PHDR segment, although that may not be true for all
4827
   targets.  */
4828
1
      segs += 2;
4829
1
    }
4830
4831
32
  if (bfd_get_section_by_name (abfd, ".dynamic") != NULL)
4832
5
    {
4833
      /* We need a PT_DYNAMIC segment.  */
4834
5
      ++segs;
4835
5
    }
4836
4837
32
  if (info != NULL && info->relro)
4838
0
    {
4839
      /* We need a PT_GNU_RELRO segment.  */
4840
0
      ++segs;
4841
0
    }
4842
4843
32
  if (elf_eh_frame_hdr (info))
4844
0
    {
4845
      /* We need a PT_GNU_EH_FRAME segment.  */
4846
0
      ++segs;
4847
0
    }
4848
4849
32
  if (elf_stack_flags (abfd))
4850
0
    {
4851
      /* We need a PT_GNU_STACK segment.  */
4852
0
      ++segs;
4853
0
    }
4854
4855
32
  if (elf_sframe (abfd))
4856
0
    {
4857
      /* We need a PT_GNU_SFRAME segment.  */
4858
0
      ++segs;
4859
0
    }
4860
4861
32
  s = bfd_get_section_by_name (abfd,
4862
32
             NOTE_GNU_PROPERTY_SECTION_NAME);
4863
32
  if (s != NULL && s->size != 0)
4864
0
    {
4865
      /* We need a PT_GNU_PROPERTY segment.  */
4866
0
      ++segs;
4867
0
    }
4868
4869
291
  for (s = abfd->sections; s != NULL; s = s->next)
4870
259
    {
4871
259
      if ((s->flags & SEC_LOAD) != 0
4872
145
    && elf_section_type (s) == SHT_NOTE)
4873
10
  {
4874
10
    unsigned int alignment_power;
4875
    /* We need a PT_NOTE segment.  */
4876
10
    ++segs;
4877
    /* Try to create just one PT_NOTE segment for all adjacent
4878
       loadable SHT_NOTE sections.  gABI requires that within a
4879
       PT_NOTE segment (and also inside of each SHT_NOTE section)
4880
       each note should have the same alignment.  So we check
4881
       whether the sections are correctly aligned.  */
4882
10
    alignment_power = s->alignment_power;
4883
10
    while (s->next != NULL
4884
10
     && s->next->alignment_power == alignment_power
4885
7
     && (s->next->flags & SEC_LOAD) != 0
4886
6
     && elf_section_type (s->next) == SHT_NOTE)
4887
0
      s = s->next;
4888
10
  }
4889
259
    }
4890
4891
158
  for (s = abfd->sections; s != NULL; s = s->next)
4892
143
    {
4893
143
      if (s->flags & SEC_THREAD_LOCAL)
4894
17
  {
4895
    /* We need a PT_TLS segment.  */
4896
17
    ++segs;
4897
17
    break;
4898
17
  }
4899
143
    }
4900
4901
32
  bed = get_elf_backend_data (abfd);
4902
4903
32
  if ((abfd->flags & D_PAGED) != 0
4904
31
      && (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0)
4905
0
    {
4906
      /* Add a PT_GNU_MBIND segment for each mbind section.  */
4907
0
      bfd_vma commonpagesize;
4908
0
      unsigned int page_align_power;
4909
4910
0
      if (info != NULL)
4911
0
  commonpagesize = info->commonpagesize;
4912
0
      else
4913
0
  commonpagesize = bed->commonpagesize;
4914
0
      page_align_power = bfd_log2 (commonpagesize);
4915
0
      for (s = abfd->sections; s != NULL; s = s->next)
4916
0
  if (elf_section_flags (s) & SHF_GNU_MBIND)
4917
0
    {
4918
0
      if (elf_section_data (s)->this_hdr.sh_info > PT_GNU_MBIND_NUM)
4919
0
        {
4920
0
    _bfd_error_handler
4921
      /* xgettext:c-format */
4922
0
      (_("%pB: GNU_MBIND section `%pA' has invalid "
4923
0
         "sh_info field: %d"),
4924
0
       abfd, s, elf_section_data (s)->this_hdr.sh_info);
4925
0
    continue;
4926
0
        }
4927
      /* Align mbind section to page size.  */
4928
0
      if (s->alignment_power < page_align_power)
4929
0
        s->alignment_power = page_align_power;
4930
0
      segs ++;
4931
0
    }
4932
0
    }
4933
4934
  /* Let the backend count up any program headers it might need.  */
4935
32
  if (bed->elf_backend_additional_program_headers)
4936
28
    {
4937
28
      int a;
4938
4939
28
      a = (*bed->elf_backend_additional_program_headers) (abfd, info);
4940
28
      if (a == -1)
4941
0
  abort ();
4942
28
      segs += a;
4943
28
    }
4944
4945
32
  return segs * bed->s->sizeof_phdr;
4946
32
}
4947
4948
/* Find the segment that contains the output_section of section.  */
4949
4950
Elf_Internal_Phdr *
4951
_bfd_elf_find_segment_containing_section (bfd * abfd, asection * section)
4952
0
{
4953
0
  struct elf_segment_map *m;
4954
0
  Elf_Internal_Phdr *p;
4955
4956
0
  for (m = elf_seg_map (abfd), p = elf_tdata (abfd)->phdr;
4957
0
       m != NULL;
4958
0
       m = m->next, p++)
4959
0
    {
4960
0
      int i;
4961
4962
0
      for (i = m->count - 1; i >= 0; i--)
4963
0
  if (m->sections[i] == section)
4964
0
    return p;
4965
0
    }
4966
4967
0
  return NULL;
4968
0
}
4969
4970
/* Create a mapping from a set of sections to a program segment.  */
4971
4972
static struct elf_segment_map *
4973
make_mapping (bfd *abfd,
4974
        asection **sections,
4975
        unsigned int from,
4976
        unsigned int to,
4977
        bool phdr)
4978
139
{
4979
139
  struct elf_segment_map *m;
4980
139
  unsigned int i;
4981
139
  asection **hdrpp;
4982
139
  size_t amt;
4983
4984
139
  amt = sizeof (struct elf_segment_map) - sizeof (asection *);
4985
139
  amt += (to - from) * sizeof (asection *);
4986
139
  m = bfd_zalloc (abfd, amt);
4987
139
  if (m == NULL)
4988
0
    return NULL;
4989
139
  m->next = NULL;
4990
139
  m->p_type = PT_LOAD;
4991
298
  for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++)
4992
159
    m->sections[i - from] = *hdrpp;
4993
139
  m->count = to - from;
4994
4995
139
  if (from == 0 && phdr)
4996
4
    {
4997
      /* Include the headers in the first PT_LOAD segment.  */
4998
4
      m->includes_filehdr = 1;
4999
4
      m->includes_phdrs = 1;
5000
4
    }
5001
5002
139
  return m;
5003
139
}
5004
5005
/* Create the PT_DYNAMIC segment, which includes DYNSEC.  Returns NULL
5006
   on failure.  */
5007
5008
struct elf_segment_map *
5009
_bfd_elf_make_dynamic_segment (bfd *abfd, asection *dynsec)
5010
5
{
5011
5
  struct elf_segment_map *m;
5012
5013
5
  m = bfd_zalloc (abfd, sizeof (*m));
5014
5
  if (m == NULL)
5015
0
    return NULL;
5016
5
  m->next = NULL;
5017
5
  m->p_type = PT_DYNAMIC;
5018
5
  m->count = 1;
5019
5
  m->sections[0] = dynsec;
5020
5021
5
  return m;
5022
5
}
5023
5024
/* Possibly add or remove segments from the segment map.  */
5025
5026
static bool
5027
elf_modify_segment_map (bfd *abfd,
5028
      struct bfd_link_info *info,
5029
      bool remove_empty_load)
5030
319
{
5031
319
  struct elf_segment_map **m;
5032
319
  elf_backend_data *bed;
5033
5034
  /* The placement algorithm assumes that non allocated sections are
5035
     not in PT_LOAD segments.  We ensure this here by removing such
5036
     sections from the segment map.  We also remove excluded
5037
     sections.  Finally, any PT_LOAD segment without sections is
5038
     removed.  */
5039
319
  m = &elf_seg_map (abfd);
5040
1.25k
  while (*m)
5041
935
    {
5042
935
      unsigned int i, new_count;
5043
5044
3.91k
      for (new_count = 0, i = 0; i < (*m)->count; i++)
5045
2.98k
  {
5046
2.98k
    if (((*m)->sections[i]->flags & SEC_EXCLUDE) == 0
5047
2.97k
        && (((*m)->sections[i]->flags & SEC_ALLOC) != 0
5048
2.30k
      || (*m)->p_type != PT_LOAD))
5049
2.97k
      {
5050
2.97k
        (*m)->sections[new_count] = (*m)->sections[i];
5051
2.97k
        new_count++;
5052
2.97k
      }
5053
2.98k
  }
5054
935
      (*m)->count = new_count;
5055
5056
935
      if (remove_empty_load
5057
935
    && (*m)->p_type == PT_LOAD
5058
184
    && (*m)->count == 0
5059
35
    && !(*m)->includes_phdrs)
5060
29
  *m = (*m)->next;
5061
906
      else
5062
906
  m = &(*m)->next;
5063
935
    }
5064
5065
319
  bed = get_elf_backend_data (abfd);
5066
319
  if (bed->elf_backend_modify_segment_map != NULL)
5067
86
    {
5068
86
      if (!(*bed->elf_backend_modify_segment_map) (abfd, info))
5069
0
  return false;
5070
86
    }
5071
5072
319
  return true;
5073
319
}
5074
5075
#define IS_TBSS(s) \
5076
202
  ((s->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) == SEC_THREAD_LOCAL)
5077
5078
/* Set up a mapping from BFD sections to program segments.  Update
5079
   NEED_LAYOUT if the section layout is changed.  */
5080
5081
bool
5082
bfd_elf_map_sections_to_segments (bfd *abfd,
5083
          struct bfd_link_info *info,
5084
          bool *need_layout)
5085
325
{
5086
325
  unsigned int count;
5087
325
  struct elf_segment_map *m;
5088
325
  asection **sections = NULL;
5089
325
  elf_backend_data *bed = get_elf_backend_data (abfd);
5090
325
  bool no_user_phdrs;
5091
5092
325
  no_user_phdrs = elf_seg_map (abfd) == NULL;
5093
5094
325
  if (info != NULL)
5095
0
    {
5096
0
      info->user_phdrs = !no_user_phdrs;
5097
5098
      /* Size the relative relocations if DT_RELR is enabled.  */
5099
0
      if (info->enable_dt_relr
5100
0
    && need_layout != NULL
5101
0
    && bed->size_relative_relocs
5102
0
    && !bed->size_relative_relocs (info, need_layout))
5103
0
  info->callbacks->fatal
5104
0
    (_("%P: failed to size relative relocations\n"));
5105
0
    }
5106
5107
325
  if (no_user_phdrs && bfd_count_sections (abfd) != 0)
5108
32
    {
5109
32
      asection *s;
5110
32
      unsigned int i;
5111
32
      struct elf_segment_map *mfirst;
5112
32
      struct elf_segment_map **pm;
5113
32
      asection *last_hdr;
5114
32
      bfd_vma last_size;
5115
32
      unsigned int hdr_index;
5116
32
      bfd_vma maxpagesize;
5117
32
      asection **hdrpp;
5118
32
      bool phdr_in_segment;
5119
32
      bool writable;
5120
32
      bool executable;
5121
32
      unsigned int tls_count = 0;
5122
32
      asection *first_tls = NULL;
5123
32
      asection *first_mbind = NULL;
5124
32
      asection *dynsec, *eh_frame_hdr;
5125
32
      asection *sframe;
5126
32
      size_t amt;
5127
32
      bfd_vma addr_mask, wrap_to = 0;  /* Bytes.  */
5128
32
      bfd_size_type phdr_size;  /* Octets/bytes.  */
5129
32
      unsigned int opb = bfd_octets_per_byte (abfd, NULL);
5130
5131
      /* Select the allocated sections, and sort them.  */
5132
5133
32
      sections = bfd_malloc (bfd_count_sections (abfd) * sizeof (*sections));
5134
32
      if (sections == NULL)
5135
0
  goto error_return;
5136
5137
      /* Calculate top address, avoiding undefined behaviour of shift
5138
   left operator when shift count is equal to size of type
5139
   being shifted.  */
5140
32
      addr_mask = ((bfd_vma) 1 << (bfd_arch_bits_per_address (abfd) - 1)) - 1;
5141
32
      addr_mask = (addr_mask << 1) + 1;
5142
5143
32
      i = 0;
5144
291
      for (s = abfd->sections; s != NULL; s = s->next)
5145
259
  {
5146
259
    if ((s->flags & SEC_ALLOC) != 0)
5147
161
      {
5148
        /* target_index is unused until bfd_elf_final_link
5149
     starts output of section symbols.  Use it to make
5150
     qsort stable.  */
5151
161
        s->target_index = i;
5152
161
        sections[i] = s;
5153
161
        ++i;
5154
        /* A wrapping section potentially clashes with header.  */
5155
161
        if (((s->lma + s->size / opb) & addr_mask) < (s->lma & addr_mask))
5156
0
    wrap_to = (s->lma + s->size / opb) & addr_mask;
5157
161
      }
5158
259
  }
5159
32
      BFD_ASSERT (i <= bfd_count_sections (abfd));
5160
32
      count = i;
5161
5162
32
      qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections);
5163
5164
32
      phdr_size = elf_program_header_size (abfd);
5165
32
      if (phdr_size == (bfd_size_type) -1)
5166
32
  phdr_size = get_program_header_size (abfd, info);
5167
32
      phdr_size += bed->s->sizeof_ehdr;
5168
      /* phdr_size is compared to LMA values which are in bytes.  */
5169
32
      phdr_size /= opb;
5170
32
      if (info != NULL)
5171
0
  maxpagesize = info->maxpagesize;
5172
32
      else
5173
32
  maxpagesize = bed->maxpagesize;
5174
32
      if (maxpagesize == 0)
5175
0
  maxpagesize = 1;
5176
32
      phdr_in_segment = info != NULL && info->load_phdrs;
5177
32
      if (count != 0
5178
31
    && (((sections[0]->lma & addr_mask) & (maxpagesize - 1))
5179
31
        >= (phdr_size & (maxpagesize - 1))))
5180
  /* For compatibility with old scripts that may not be using
5181
     SIZEOF_HEADERS, add headers when it looks like space has
5182
     been left for them.  */
5183
4
  phdr_in_segment = true;
5184
5185
      /* Build the mapping.  */
5186
32
      mfirst = NULL;
5187
32
      pm = &mfirst;
5188
5189
      /* If we have a .interp section, then create a PT_PHDR segment for
5190
   the program headers and a PT_INTERP segment for the .interp
5191
   section.  */
5192
32
      s = bfd_get_section_by_name (abfd, ".interp");
5193
32
      if (s != NULL && (s->flags & SEC_LOAD) != 0 && s->size != 0)
5194
1
  {
5195
1
    m = bfd_zalloc (abfd, sizeof (*m));
5196
1
    if (m == NULL)
5197
0
      goto error_return;
5198
1
    m->next = NULL;
5199
1
    m->p_type = PT_PHDR;
5200
1
    m->p_flags = PF_R;
5201
1
    m->p_flags_valid = 1;
5202
1
    m->includes_phdrs = 1;
5203
1
    phdr_in_segment = true;
5204
1
    *pm = m;
5205
1
    pm = &m->next;
5206
5207
1
    m = bfd_zalloc (abfd, sizeof (*m));
5208
1
    if (m == NULL)
5209
0
      goto error_return;
5210
1
    m->next = NULL;
5211
1
    m->p_type = PT_INTERP;
5212
1
    m->count = 1;
5213
1
    m->sections[0] = s;
5214
5215
1
    *pm = m;
5216
1
    pm = &m->next;
5217
1
  }
5218
5219
      /* Look through the sections.  We put sections in the same program
5220
   segment when the start of the second section can be placed within
5221
   a few bytes of the end of the first section.  */
5222
32
      last_hdr = NULL;
5223
32
      last_size = 0;
5224
32
      hdr_index = 0;
5225
32
      writable = false;
5226
32
      executable = false;
5227
32
      dynsec = bfd_get_section_by_name (abfd, ".dynamic");
5228
32
      if (dynsec != NULL
5229
5
    && (dynsec->flags & SEC_LOAD) == 0)
5230
0
  dynsec = NULL;
5231
5232
32
      if ((abfd->flags & D_PAGED) == 0)
5233
1
  phdr_in_segment = false;
5234
5235
      /* Deal with -Ttext or something similar such that the first section
5236
   is not adjacent to the program headers.  This is an
5237
   approximation, since at this point we don't know exactly how many
5238
   program headers we will need.  */
5239
32
      if (phdr_in_segment && count > 0)
5240
5
  {
5241
5
    bfd_vma phdr_lma;  /* Bytes.  */
5242
5
    bool separate_phdr = false;
5243
5244
5
    phdr_lma = (sections[0]->lma - phdr_size) & addr_mask & -maxpagesize;
5245
5
    if (info != NULL
5246
0
        && info->separate_code
5247
0
        && (sections[0]->flags & SEC_CODE) != 0)
5248
0
      {
5249
        /* If data sections should be separate from code and
5250
     thus not executable, and the first section is
5251
     executable then put the file and program headers in
5252
     their own PT_LOAD.  */
5253
0
        if (!info->one_rosegment)
5254
0
    separate_phdr = true;
5255
5256
0
        if ((((phdr_lma + phdr_size - 1) & addr_mask & -maxpagesize)
5257
0
       == (sections[0]->lma & addr_mask & -maxpagesize)))
5258
0
    {
5259
      /* The file and program headers are currently on the
5260
         same page as the first section.  Put them on the
5261
         previous page if we can.  */
5262
0
      if (phdr_lma >= maxpagesize)
5263
0
        phdr_lma -= maxpagesize;
5264
0
      else
5265
0
        separate_phdr = false;
5266
0
    }
5267
0
      }
5268
5
    if ((sections[0]->lma & addr_mask) < phdr_lma
5269
4
        || (sections[0]->lma & addr_mask) < phdr_size)
5270
      /* If file and program headers would be placed at the end
5271
         of memory then it's probably better to omit them.  */
5272
1
      phdr_in_segment = false;
5273
4
    else if (phdr_lma < wrap_to)
5274
      /* If a section wraps around to where we'll be placing
5275
         file and program headers, then the headers will be
5276
         overwritten.  */
5277
0
      phdr_in_segment = false;
5278
4
    else if (separate_phdr)
5279
0
      {
5280
0
        m = make_mapping (abfd, sections, 0, 0, phdr_in_segment);
5281
0
        if (m == NULL)
5282
0
    goto error_return;
5283
0
        m->p_paddr = phdr_lma * opb;
5284
0
        m->p_vaddr_offset
5285
0
    = (sections[0]->vma - phdr_size) & addr_mask & -maxpagesize;
5286
0
        m->p_paddr_valid = 1;
5287
0
        *pm = m;
5288
0
        pm = &m->next;
5289
0
        phdr_in_segment = false;
5290
0
      }
5291
5
  }
5292
5293
193
      for (i = 0, hdrpp = sections; i < count; i++, hdrpp++)
5294
161
  {
5295
161
    asection *hdr;
5296
161
    bool new_segment;
5297
5298
161
    hdr = *hdrpp;
5299
5300
    /* See if this section and the last one will fit in the same
5301
       segment.  */
5302
5303
161
    if (last_hdr == NULL)
5304
31
      {
5305
        /* If we don't have a segment yet, then we don't need a new
5306
     one (we build the last one after this loop).  */
5307
31
        new_segment = false;
5308
31
      }
5309
130
    else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma)
5310
4
      {
5311
        /* If this section has a different relation between the
5312
     virtual address and the load address, then we need a new
5313
     segment.  */
5314
4
        new_segment = true;
5315
4
      }
5316
126
    else if (hdr->lma < last_hdr->lma + last_size
5317
57
       || last_hdr->lma + last_size < last_hdr->lma)
5318
69
      {
5319
        /* If this section has a load address that makes it overlap
5320
     the previous section, then we need a new segment.  */
5321
69
        new_segment = true;
5322
69
      }
5323
57
    else if ((abfd->flags & D_PAGED) != 0
5324
57
       && (((last_hdr->lma + last_size - 1) & -maxpagesize)
5325
57
           == (hdr->lma & -maxpagesize)))
5326
12
      {
5327
        /* If we are demand paged then we can't map two disk
5328
     pages onto the same memory page.  */
5329
12
        new_segment = false;
5330
12
      }
5331
    /* In the next test we have to be careful when last_hdr->lma is close
5332
       to the end of the address space.  If the aligned address wraps
5333
       around to the start of the address space, then there are no more
5334
       pages left in memory and it is OK to assume that the current
5335
       section can be included in the current segment.  */
5336
45
    else if ((BFD_ALIGN (last_hdr->lma + last_size, maxpagesize)
5337
45
        + maxpagesize > last_hdr->lma)
5338
45
       && (BFD_ALIGN (last_hdr->lma + last_size, maxpagesize)
5339
45
           + maxpagesize <= hdr->lma))
5340
30
      {
5341
        /* If putting this section in this segment would force us to
5342
     skip a page in the segment, then we need a new segment.  */
5343
30
        new_segment = true;
5344
30
      }
5345
15
    else if ((last_hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0
5346
2
       && (hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) != 0)
5347
1
      {
5348
        /* We don't want to put a loaded section after a
5349
     nonloaded (ie. bss style) section in the same segment
5350
     as that will force the non-loaded section to be loaded.
5351
     Consider .tbss sections as loaded for this purpose.  */
5352
1
        new_segment = true;
5353
1
      }
5354
14
    else if ((abfd->flags & D_PAGED) == 0)
5355
0
      {
5356
        /* If the file is not demand paged, which means that we
5357
     don't require the sections to be correctly aligned in the
5358
     file, then there is no other reason for a new segment.  */
5359
0
        new_segment = false;
5360
0
      }
5361
14
    else if (info != NULL
5362
0
       && info->separate_code
5363
0
       && executable != ((hdr->flags & SEC_CODE) != 0))
5364
0
      {
5365
0
        new_segment = true;
5366
0
      }
5367
14
    else if (! writable
5368
13
       && (hdr->flags & SEC_READONLY) == 0)
5369
6
      {
5370
        /* We don't want to put a writable section in a read only
5371
     segment.  */
5372
6
        new_segment = true;
5373
6
      }
5374
8
    else
5375
8
      {
5376
        /* Otherwise, we can use the same segment.  */
5377
8
        new_segment = false;
5378
8
      }
5379
5380
    /* Allow interested parties a chance to override our decision.  */
5381
161
    if (last_hdr != NULL
5382
130
        && info != NULL
5383
0
        && info->callbacks->override_segment_assignment != NULL)
5384
0
      new_segment
5385
0
        = info->callbacks->override_segment_assignment (info, abfd, hdr,
5386
0
                    last_hdr,
5387
0
                    new_segment);
5388
5389
161
    if (! new_segment)
5390
51
      {
5391
51
        if ((hdr->flags & SEC_READONLY) == 0)
5392
9
    writable = true;
5393
51
        if ((hdr->flags & SEC_CODE) != 0)
5394
18
    executable = true;
5395
51
        last_hdr = hdr;
5396
        /* .tbss sections effectively have zero size.  */
5397
51
        last_size = (!IS_TBSS (hdr) ? hdr->size : 0) / opb;
5398
51
        continue;
5399
51
      }
5400
5401
    /* We need a new program segment.  We must create a new program
5402
       header holding all the sections from hdr_index until hdr.  */
5403
5404
110
    m = make_mapping (abfd, sections, hdr_index, i, phdr_in_segment);
5405
110
    if (m == NULL)
5406
0
      goto error_return;
5407
5408
110
    *pm = m;
5409
110
    pm = &m->next;
5410
5411
110
    if ((hdr->flags & SEC_READONLY) == 0)
5412
41
      writable = true;
5413
69
    else
5414
69
      writable = false;
5415
5416
110
    if ((hdr->flags & SEC_CODE) == 0)
5417
85
      executable = false;
5418
25
    else
5419
25
      executable = true;
5420
5421
110
    last_hdr = hdr;
5422
    /* .tbss sections effectively have zero size.  */
5423
110
    last_size = (!IS_TBSS (hdr) ? hdr->size : 0) / opb;
5424
110
    hdr_index = i;
5425
110
    phdr_in_segment = false;
5426
110
  }
5427
5428
      /* Create a final PT_LOAD program segment, but not if it's just
5429
   for .tbss.  */
5430
32
      if (last_hdr != NULL
5431
31
    && (i - hdr_index != 1
5432
28
        || !IS_TBSS (last_hdr)))
5433
29
  {
5434
29
    m = make_mapping (abfd, sections, hdr_index, i, phdr_in_segment);
5435
29
    if (m == NULL)
5436
0
      goto error_return;
5437
5438
29
    *pm = m;
5439
29
    pm = &m->next;
5440
29
  }
5441
5442
      /* If there is a .dynamic section, throw in a PT_DYNAMIC segment.  */
5443
32
      if (dynsec != NULL)
5444
5
  {
5445
5
    m = _bfd_elf_make_dynamic_segment (abfd, dynsec);
5446
5
    if (m == NULL)
5447
0
      goto error_return;
5448
5
    *pm = m;
5449
5
    pm = &m->next;
5450
5
  }
5451
5452
      /* For each batch of consecutive loadable SHT_NOTE  sections,
5453
   add a PT_NOTE segment.  We don't use bfd_get_section_by_name,
5454
   because if we link together nonloadable .note sections and
5455
   loadable .note sections, we will generate two .note sections
5456
   in the output file.  */
5457
291
      for (s = abfd->sections; s != NULL; s = s->next)
5458
259
  {
5459
259
    if ((s->flags & SEC_LOAD) != 0
5460
145
        && elf_section_type (s) == SHT_NOTE)
5461
10
      {
5462
10
        asection *s2;
5463
10
        unsigned int alignment_power = s->alignment_power;
5464
5465
10
        count = 1;
5466
10
        for (s2 = s; s2->next != NULL; s2 = s2->next)
5467
10
    {
5468
10
      if (s2->next->alignment_power == alignment_power
5469
7
          && (s2->next->flags & SEC_LOAD) != 0
5470
6
          && elf_section_type (s2->next) == SHT_NOTE
5471
0
          && align_power (s2->lma + s2->size / opb,
5472
0
              alignment_power)
5473
0
          == s2->next->lma)
5474
0
        count++;
5475
10
      else
5476
10
        break;
5477
10
    }
5478
10
        amt = sizeof (struct elf_segment_map) - sizeof (asection *);
5479
10
        amt += count * sizeof (asection *);
5480
10
        m = bfd_zalloc (abfd, amt);
5481
10
        if (m == NULL)
5482
0
    goto error_return;
5483
10
        m->next = NULL;
5484
10
        m->p_type = PT_NOTE;
5485
10
        m->count = count;
5486
10
        while (count > 1)
5487
0
    {
5488
0
      m->sections[m->count - count--] = s;
5489
0
      BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
5490
0
      s = s->next;
5491
0
    }
5492
10
        m->sections[m->count - 1] = s;
5493
10
        BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
5494
10
        *pm = m;
5495
10
        pm = &m->next;
5496
10
      }
5497
259
    if (s->flags & SEC_THREAD_LOCAL)
5498
27
      {
5499
27
        if (! tls_count)
5500
17
    first_tls = s;
5501
27
        tls_count++;
5502
27
      }
5503
259
    if (first_mbind == NULL
5504
211
        && (elf_section_flags (s) & SHF_GNU_MBIND) != 0)
5505
11
      first_mbind = s;
5506
259
  }
5507
5508
      /* If there are any SHF_TLS output sections, add PT_TLS segment.  */
5509
32
      if (tls_count > 0)
5510
17
  {
5511
17
    amt = sizeof (struct elf_segment_map) - sizeof (asection *);
5512
17
    amt += tls_count * sizeof (asection *);
5513
17
    m = bfd_zalloc (abfd, amt);
5514
17
    if (m == NULL)
5515
0
      goto error_return;
5516
17
    m->next = NULL;
5517
17
    m->p_type = PT_TLS;
5518
17
    m->count = tls_count;
5519
    /* Mandated PF_R.  */
5520
17
    m->p_flags = PF_R;
5521
17
    m->p_flags_valid = 1;
5522
17
    s = first_tls;
5523
34
    for (i = 0; i < tls_count; ++i)
5524
23
      {
5525
23
        if ((s->flags & SEC_THREAD_LOCAL) == 0)
5526
6
    {
5527
6
      _bfd_error_handler
5528
6
        (_("%pB: TLS sections are not adjacent:"), abfd);
5529
6
      s = first_tls;
5530
6
      i = 0;
5531
47
      while (i < tls_count)
5532
41
        {
5533
41
          if ((s->flags & SEC_THREAD_LOCAL) != 0)
5534
16
      {
5535
16
        _bfd_error_handler (_("      TLS: %pA"), s);
5536
16
        i++;
5537
16
      }
5538
25
          else
5539
25
      _bfd_error_handler (_("  non-TLS: %pA"), s);
5540
41
          s = s->next;
5541
41
        }
5542
6
      bfd_set_error (bfd_error_bad_value);
5543
6
      goto error_return;
5544
6
    }
5545
17
        m->sections[i] = s;
5546
17
        s = s->next;
5547
17
      }
5548
5549
11
    *pm = m;
5550
11
    pm = &m->next;
5551
11
  }
5552
5553
26
      if (first_mbind
5554
5
    && (abfd->flags & D_PAGED) != 0
5555
5
    && (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0)
5556
0
  for (s = first_mbind; s != NULL; s = s->next)
5557
0
    if ((elf_section_flags (s) & SHF_GNU_MBIND) != 0
5558
0
        && elf_section_data (s)->this_hdr.sh_info <= PT_GNU_MBIND_NUM)
5559
0
      {
5560
        /* Mandated PF_R.  */
5561
0
        unsigned long p_flags = PF_R;
5562
0
        if ((s->flags & SEC_READONLY) == 0)
5563
0
    p_flags |= PF_W;
5564
0
        if ((s->flags & SEC_CODE) != 0)
5565
0
    p_flags |= PF_X;
5566
5567
0
        amt = sizeof (struct elf_segment_map) + sizeof (asection *);
5568
0
        m = bfd_zalloc (abfd, amt);
5569
0
        if (m == NULL)
5570
0
    goto error_return;
5571
0
        m->next = NULL;
5572
0
        m->p_type = (PT_GNU_MBIND_LO
5573
0
         + elf_section_data (s)->this_hdr.sh_info);
5574
0
        m->count = 1;
5575
0
        m->p_flags_valid = 1;
5576
0
        m->sections[0] = s;
5577
0
        m->p_flags = p_flags;
5578
5579
0
        *pm = m;
5580
0
        pm = &m->next;
5581
0
      }
5582
5583
26
      s = bfd_get_section_by_name (abfd,
5584
26
           NOTE_GNU_PROPERTY_SECTION_NAME);
5585
26
      if (s != NULL && s->size != 0)
5586
0
  {
5587
0
    amt = sizeof (struct elf_segment_map) + sizeof (asection *);
5588
0
    m = bfd_zalloc (abfd, amt);
5589
0
    if (m == NULL)
5590
0
      goto error_return;
5591
0
    m->next = NULL;
5592
0
    m->p_type = PT_GNU_PROPERTY;
5593
0
    m->count = 1;
5594
0
    m->p_flags_valid = 1;
5595
0
    m->sections[0] = s;
5596
0
    m->p_flags = PF_R;
5597
0
    *pm = m;
5598
0
    pm = &m->next;
5599
0
  }
5600
5601
      /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME
5602
   segment.  */
5603
26
      eh_frame_hdr = elf_eh_frame_hdr (info);
5604
26
      if (eh_frame_hdr != NULL
5605
0
    && (eh_frame_hdr->output_section->flags & SEC_LOAD) != 0)
5606
0
  {
5607
0
    m = bfd_zalloc (abfd, sizeof (*m));
5608
0
    if (m == NULL)
5609
0
      goto error_return;
5610
0
    m->next = NULL;
5611
0
    m->p_type = PT_GNU_EH_FRAME;
5612
0
    m->count = 1;
5613
0
    m->sections[0] = eh_frame_hdr->output_section;
5614
5615
0
    *pm = m;
5616
0
    pm = &m->next;
5617
0
  }
5618
5619
      /* If there is a .sframe section, throw in a PT_GNU_SFRAME
5620
   segment.  */
5621
26
      sframe = elf_sframe (abfd);
5622
26
      if (sframe != NULL
5623
0
    && (sframe->output_section->flags & SEC_LOAD) != 0
5624
0
    && sframe->size != 0)
5625
0
  {
5626
0
    m = bfd_zalloc (abfd, sizeof (*m));
5627
0
    if (m == NULL)
5628
0
      goto error_return;
5629
0
    m->next = NULL;
5630
0
    m->p_type = PT_GNU_SFRAME;
5631
0
    m->count = 1;
5632
0
    m->sections[0] = sframe->output_section;
5633
5634
0
    *pm = m;
5635
0
    pm = &m->next;
5636
0
  }
5637
5638
26
      if (elf_stack_flags (abfd))
5639
0
  {
5640
0
    m = bfd_zalloc (abfd, sizeof (*m));
5641
0
    if (m == NULL)
5642
0
      goto error_return;
5643
0
    m->next = NULL;
5644
0
    m->p_type = PT_GNU_STACK;
5645
0
    m->p_flags = elf_stack_flags (abfd);
5646
0
    m->p_align = bed->stack_align;
5647
0
    m->p_flags_valid = 1;
5648
0
    m->p_align_valid = m->p_align != 0;
5649
0
    if (info->stacksize > 0)
5650
0
      {
5651
0
        m->p_size = info->stacksize;
5652
0
        m->p_size_valid = 1;
5653
0
      }
5654
5655
0
    *pm = m;
5656
0
    pm = &m->next;
5657
0
  }
5658
5659
26
      if (info != NULL && info->relro)
5660
0
  {
5661
0
    for (m = mfirst; m != NULL; m = m->next)
5662
0
      {
5663
0
        if (m->p_type == PT_LOAD
5664
0
      && m->count != 0
5665
0
      && m->sections[0]->vma >= info->relro_start
5666
0
      && m->sections[0]->vma < info->relro_end)
5667
0
    {
5668
0
      i = m->count;
5669
0
      while (--i != (unsigned) -1)
5670
0
        {
5671
0
          if (m->sections[i]->size > 0
5672
0
        && (m->sections[i]->flags & SEC_LOAD) != 0
5673
0
        && (m->sections[i]->flags & SEC_HAS_CONTENTS) != 0)
5674
0
      break;
5675
0
        }
5676
5677
0
      if (i != (unsigned) -1)
5678
0
        break;
5679
0
    }
5680
0
      }
5681
5682
    /* Make a PT_GNU_RELRO segment only when it isn't empty.  */
5683
0
    if (m != NULL)
5684
0
      {
5685
0
        m = bfd_zalloc (abfd, sizeof (*m));
5686
0
        if (m == NULL)
5687
0
    goto error_return;
5688
0
        m->next = NULL;
5689
0
        m->p_type = PT_GNU_RELRO;
5690
0
        *pm = m;
5691
0
        pm = &m->next;
5692
0
      }
5693
0
  }
5694
5695
26
      free (sections);
5696
26
      elf_seg_map (abfd) = mfirst;
5697
26
    }
5698
5699
319
  if (!elf_modify_segment_map (abfd, info, no_user_phdrs || info == NULL))
5700
0
    return false;
5701
5702
1.22k
  for (count = 0, m = elf_seg_map (abfd); m != NULL; m = m->next)
5703
906
    ++count;
5704
319
  elf_program_header_size (abfd) = count * bed->s->sizeof_phdr;
5705
5706
319
  return true;
5707
5708
6
 error_return:
5709
6
  free (sections);
5710
6
  return false;
5711
319
}
5712
5713
/* Sort sections by address.  */
5714
5715
static int
5716
elf_sort_sections (const void *arg1, const void *arg2)
5717
5.84k
{
5718
5.84k
  const asection *sec1 = *(const asection **) arg1;
5719
5.84k
  const asection *sec2 = *(const asection **) arg2;
5720
5.84k
  bfd_size_type size1, size2;
5721
5722
  /* Sort by LMA first, since this is the address used to
5723
     place the section into a segment.  */
5724
5.84k
  if (sec1->lma < sec2->lma)
5725
2.09k
    return -1;
5726
3.74k
  else if (sec1->lma > sec2->lma)
5727
2.04k
    return 1;
5728
5729
  /* Then sort by VMA.  Normally the LMA and the VMA will be
5730
     the same, and this will do nothing.  */
5731
1.69k
  if (sec1->vma < sec2->vma)
5732
129
    return -1;
5733
1.56k
  else if (sec1->vma > sec2->vma)
5734
295
    return 1;
5735
5736
  /* Put !SEC_LOAD sections after SEC_LOAD ones.  */
5737
5738
2.54k
#define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0 \
5739
2.54k
      && (x)->size != 0)
5740
5741
1.27k
  if (TOEND (sec1))
5742
1.10k
    {
5743
1.10k
      if (!TOEND (sec2))
5744
0
  return 1;
5745
1.10k
    }
5746
170
  else if (TOEND (sec2))
5747
0
    return -1;
5748
5749
1.27k
#undef TOEND
5750
5751
  /* Sort by size, to put zero sized sections
5752
     before others at the same address.  */
5753
5754
1.27k
  size1 = (sec1->flags & SEC_LOAD) ? sec1->size : 0;
5755
1.27k
  size2 = (sec2->flags & SEC_LOAD) ? sec2->size : 0;
5756
5757
1.27k
  if (size1 < size2)
5758
79
    return -1;
5759
1.19k
  if (size1 > size2)
5760
69
    return 1;
5761
5762
1.12k
  return sec1->target_index - sec2->target_index;
5763
1.19k
}
5764
5765
/* This qsort comparison functions sorts PT_LOAD segments first and
5766
   by p_paddr, for assign_file_positions_for_load_sections.  */
5767
5768
static int
5769
elf_sort_segments (const void *arg1, const void *arg2)
5770
1.97k
{
5771
1.97k
  const struct elf_segment_map *m1 = *(const struct elf_segment_map **) arg1;
5772
1.97k
  const struct elf_segment_map *m2 = *(const struct elf_segment_map **) arg2;
5773
5774
1.97k
  if (m1->p_type != m2->p_type)
5775
785
    {
5776
785
      if (m1->p_type == PT_NULL)
5777
53
  return 1;
5778
732
      if (m2->p_type == PT_NULL)
5779
92
  return -1;
5780
640
      return m1->p_type < m2->p_type ? -1 : 1;
5781
732
    }
5782
1.19k
  if (m1->includes_filehdr != m2->includes_filehdr)
5783
26
    return m1->includes_filehdr ? -1 : 1;
5784
1.16k
  if (m1->no_sort_lma != m2->no_sort_lma)
5785
0
    return m1->no_sort_lma ? -1 : 1;
5786
1.16k
  if (m1->p_type == PT_LOAD && !m1->no_sort_lma)
5787
135
    {
5788
135
      bfd_vma lma1, lma2;  /* Octets.  */
5789
135
      lma1 = 0;
5790
135
      if (m1->p_paddr_valid)
5791
25
  lma1 = m1->p_paddr;
5792
110
      else if (m1->count != 0)
5793
110
  {
5794
110
    unsigned int opb = bfd_octets_per_byte (m1->sections[0]->owner,
5795
110
              m1->sections[0]);
5796
110
    lma1 = (m1->sections[0]->lma + m1->p_vaddr_offset) * opb;
5797
110
  }
5798
135
      lma2 = 0;
5799
135
      if (m2->p_paddr_valid)
5800
25
  lma2 = m2->p_paddr;
5801
110
      else if (m2->count != 0)
5802
110
  {
5803
110
    unsigned int opb = bfd_octets_per_byte (m2->sections[0]->owner,
5804
110
              m2->sections[0]);
5805
110
    lma2 = (m2->sections[0]->lma + m2->p_vaddr_offset) * opb;
5806
110
  }
5807
135
      if (lma1 != lma2)
5808
59
  return lma1 < lma2 ? -1 : 1;
5809
135
    }
5810
1.10k
  if (m1->idx != m2->idx)
5811
1.10k
    return m1->idx < m2->idx ? -1 : 1;
5812
0
  return 0;
5813
1.10k
}
5814
5815
/* Ian Lance Taylor writes:
5816
5817
   We shouldn't be using % with a negative signed number.  That's just
5818
   not good.  We have to make sure either that the number is not
5819
   negative, or that the number has an unsigned type.  When the types
5820
   are all the same size they wind up as unsigned.  When file_ptr is a
5821
   larger signed type, the arithmetic winds up as signed long long,
5822
   which is wrong.
5823
5824
   What we're trying to say here is something like ``increase OFF by
5825
   the least amount that will cause it to be equal to the VMA modulo
5826
   the page size.''  */
5827
/* In other words, something like:
5828
5829
   vma_offset = m->sections[0]->vma % bed->maxpagesize;
5830
   off_offset = off % bed->maxpagesize;
5831
   if (vma_offset < off_offset)
5832
     adjustment = vma_offset + bed->maxpagesize - off_offset;
5833
   else
5834
     adjustment = vma_offset - off_offset;
5835
5836
   which can be collapsed into the expression below.  */
5837
5838
static file_ptr
5839
vma_page_aligned_bias (bfd_vma vma, ufile_ptr off, bfd_vma maxpagesize)
5840
237
{
5841
  /* PR binutils/16199: Handle an alignment of zero.  */
5842
237
  if (maxpagesize == 0)
5843
1
    maxpagesize = 1;
5844
237
  return ((vma - off) % maxpagesize);
5845
237
}
5846
5847
static void
5848
print_segment_map (const struct elf_segment_map *m)
5849
28
{
5850
28
  unsigned int j;
5851
28
  const char *pt = get_segment_type (m->p_type);
5852
28
  char buf[32];
5853
5854
28
  if (pt == NULL)
5855
0
    {
5856
0
      if (m->p_type >= PT_LOPROC && m->p_type <= PT_HIPROC)
5857
0
  sprintf (buf, "LOPROC+%7.7x",
5858
0
     (unsigned int) (m->p_type - PT_LOPROC));
5859
0
      else if (m->p_type >= PT_LOOS && m->p_type <= PT_HIOS)
5860
0
  sprintf (buf, "LOOS+%7.7x",
5861
0
     (unsigned int) (m->p_type - PT_LOOS));
5862
0
      else
5863
0
  snprintf (buf, sizeof (buf), "%8.8x",
5864
0
      (unsigned int) m->p_type);
5865
0
      pt = buf;
5866
0
    }
5867
28
  fflush (stdout);
5868
28
  fprintf (stderr, "%s:", pt);
5869
546
  for (j = 0; j < m->count; j++)
5870
518
    fprintf (stderr, " %s", m->sections [j]->name);
5871
28
  putc ('\n',stderr);
5872
28
  fflush (stderr);
5873
28
}
5874
5875
/* Assign file positions to the sections based on the mapping from
5876
   sections to segments.  This function also sets up some fields in
5877
   the file header.  */
5878
5879
static bool
5880
assign_file_positions_for_load_sections (bfd *abfd,
5881
           struct bfd_link_info *link_info)
5882
325
{
5883
325
  elf_backend_data *bed = get_elf_backend_data (abfd);
5884
325
  struct elf_segment_map *m;
5885
325
  struct elf_segment_map *phdr_load_seg;
5886
325
  Elf_Internal_Phdr *phdrs;
5887
325
  Elf_Internal_Phdr *p;
5888
325
  file_ptr off;  /* Octets.  */
5889
325
  bfd_size_type maxpagesize;
5890
325
  unsigned int alloc, actual;
5891
325
  unsigned int i, j;
5892
325
  struct elf_segment_map **sorted_seg_map;
5893
325
  unsigned int opb = bfd_octets_per_byte (abfd, NULL);
5894
5895
325
  if (link_info == NULL
5896
325
      && !bfd_elf_map_sections_to_segments (abfd, link_info, NULL))
5897
6
    return false;
5898
5899
319
  alloc = 0;
5900
1.22k
  for (m = elf_seg_map (abfd); m != NULL; m = m->next)
5901
906
    m->idx = alloc++;
5902
5903
319
  if (alloc)
5904
163
    {
5905
163
      elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr;
5906
163
      elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr;
5907
163
    }
5908
156
  else
5909
156
    {
5910
      /* PR binutils/12467.  */
5911
156
      elf_elfheader (abfd)->e_phoff = 0;
5912
156
      elf_elfheader (abfd)->e_phentsize = 0;
5913
156
    }
5914
5915
319
  elf_elfheader (abfd)->e_phnum = alloc;
5916
5917
319
  if (elf_program_header_size (abfd) == (bfd_size_type) -1)
5918
0
    {
5919
0
      actual = alloc;
5920
0
      elf_program_header_size (abfd) = alloc * bed->s->sizeof_phdr;
5921
0
    }
5922
319
  else
5923
319
    {
5924
319
      actual = elf_program_header_size (abfd) / bed->s->sizeof_phdr;
5925
319
      BFD_ASSERT (elf_program_header_size (abfd)
5926
319
      == actual * bed->s->sizeof_phdr);
5927
319
      BFD_ASSERT (actual >= alloc);
5928
319
    }
5929
5930
319
  if (alloc == 0)
5931
156
    {
5932
156
      elf_next_file_pos (abfd) = bed->s->sizeof_ehdr;
5933
156
      return true;
5934
156
    }
5935
5936
  /* We're writing the size in elf_program_header_size (abfd),
5937
     see assign_file_positions_except_relocs, so make sure we have
5938
     that amount allocated, with trailing space cleared.
5939
     The variable alloc contains the computed need, while
5940
     elf_program_header_size (abfd) contains the size used for the
5941
     layout.
5942
     See ld/emultempl/elf-generic.em:gld${EMULATION_NAME}_map_segments
5943
     where the layout is forced to according to a larger size in the
5944
     last iterations for the testcase ld-elf/header.  */
5945
163
  phdrs = bfd_zalloc (abfd, (actual * sizeof (*phdrs)
5946
163
           + alloc * sizeof (*sorted_seg_map)));
5947
163
  sorted_seg_map = (struct elf_segment_map **) (phdrs + actual);
5948
163
  elf_tdata (abfd)->phdr = phdrs;
5949
163
  if (phdrs == NULL)
5950
0
    return false;
5951
5952
1.06k
  for (m = elf_seg_map (abfd), j = 0; m != NULL; m = m->next, j++)
5953
906
    {
5954
906
      sorted_seg_map[j] = m;
5955
      /* If elf_segment_map is not from map_sections_to_segments, the
5956
   sections may not be correctly ordered.  NOTE: sorting should
5957
   not be done to the PT_NOTE section of a corefile, which may
5958
   contain several pseudo-sections artificially created by bfd.
5959
   Sorting these pseudo-sections breaks things badly.  */
5960
906
      if (m->count > 1
5961
432
    && !(elf_elfheader (abfd)->e_type == ET_CORE
5962
361
         && m->p_type == PT_NOTE))
5963
430
  {
5964
3.19k
    for (i = 0; i < m->count; i++)
5965
2.76k
      m->sections[i]->target_index = i;
5966
430
    qsort (m->sections, (size_t) m->count, sizeof (asection *),
5967
430
     elf_sort_sections);
5968
430
  }
5969
906
    }
5970
163
  if (alloc > 1)
5971
102
    qsort (sorted_seg_map, alloc, sizeof (*sorted_seg_map),
5972
102
     elf_sort_segments);
5973
5974
163
  maxpagesize = 1;
5975
163
  if ((abfd->flags & D_PAGED) != 0)
5976
55
    {
5977
55
      if (link_info != NULL)
5978
0
  maxpagesize = link_info->maxpagesize;
5979
55
      else
5980
55
  maxpagesize = bed->maxpagesize;
5981
55
    }
5982
5983
  /* Sections must map to file offsets past the ELF file header.  */
5984
163
  off = bed->s->sizeof_ehdr;
5985
  /* And if one of the PT_LOAD headers doesn't include the program
5986
     headers then we'll be mapping program headers in the usual
5987
     position after the ELF file header.  */
5988
163
  phdr_load_seg = NULL;
5989
269
  for (j = 0; j < alloc; j++)
5990
261
    {
5991
261
      m = sorted_seg_map[j];
5992
261
      if (m->p_type != PT_LOAD)
5993
126
  break;
5994
135
      if (m->includes_phdrs)
5995
29
  {
5996
29
    phdr_load_seg = m;
5997
29
    break;
5998
29
  }
5999
135
    }
6000
163
  if (phdr_load_seg == NULL)
6001
134
    off += actual * bed->s->sizeof_phdr;
6002
6003
772
  for (j = 0; j < alloc; j++)
6004
622
    {
6005
622
      asection **secpp;
6006
622
      bfd_vma off_adjust;  /* Octets.  */
6007
622
      bool no_contents;
6008
622
      bfd_size_type align_pagesize;
6009
6010
      /* An ELF segment (described by Elf_Internal_Phdr) may contain a
6011
   number of sections with contents contributing to both p_filesz
6012
   and p_memsz, followed by a number of sections with no contents
6013
   that just contribute to p_memsz.  In this loop, OFF tracks next
6014
   available file offset for PT_LOAD and PT_NOTE segments.  */
6015
622
      m = sorted_seg_map[j];
6016
622
      p = phdrs + m->idx;
6017
622
      p->p_type = m->p_type;
6018
622
      p->p_flags = m->p_flags;
6019
6020
622
      if (m->count == 0)
6021
269
  p->p_vaddr = m->p_vaddr_offset * opb;
6022
353
      else
6023
353
  p->p_vaddr = (m->sections[0]->vma + m->p_vaddr_offset) * opb;
6024
6025
622
      if (m->p_paddr_valid)
6026
440
  p->p_paddr = m->p_paddr;
6027
182
      else if (m->count == 0)
6028
20
  p->p_paddr = 0;
6029
162
      else
6030
162
  p->p_paddr = (m->sections[0]->lma + m->p_vaddr_offset) * opb;
6031
6032
622
      align_pagesize = 0;
6033
622
      if (p->p_type == PT_LOAD
6034
152
    && (abfd->flags & D_PAGED) != 0)
6035
129
  {
6036
    /* p_align in demand paged PT_LOAD segments effectively stores
6037
       the maximum page size.  When copying an executable with
6038
       objcopy, we set m->p_align from the input file.  Use this
6039
       value for maxpagesize rather than bed->maxpagesize, which
6040
       may be different.  Note that we use maxpagesize for PT_TLS
6041
       segment alignment later in this function, so we are relying
6042
       on at least one PT_LOAD segment appearing before a PT_TLS
6043
       segment.  */
6044
129
    if (m->p_align_valid)
6045
25
      maxpagesize = m->p_align;
6046
104
    else if (bed->p_align != 0
6047
51
       && (link_info == NULL
6048
0
           || !link_info->maxpagesize_is_set))
6049
      /* We will lay out this binary using maxpagesize but set
6050
         p->p_align later to the possibly smaller bed->p_align.
6051
         The run-time loader will then be able to load this
6052
         binary when the system page size is maxpagesize, but if
6053
         the system page size is smaller can use p->p_align.
6054
         In either case p->p_align will be increased if
6055
         necessary to match section alignment.  */
6056
51
      align_pagesize = bed->p_align;
6057
6058
129
    p->p_align = maxpagesize;
6059
129
  }
6060
493
      else if (m->p_align_valid)
6061
347
  p->p_align = m->p_align;
6062
146
      else if (m->count == 0)
6063
65
  p->p_align = 1 << bed->s->log_file_align;
6064
6065
622
      if (m == phdr_load_seg)
6066
29
  off += actual * bed->s->sizeof_phdr;
6067
6068
622
      no_contents = false;
6069
622
      off_adjust = 0;
6070
622
      if (p->p_type == PT_LOAD
6071
152
    && m->count > 0)
6072
146
  {
6073
146
    bfd_size_type align;  /* Bytes.  */
6074
146
    unsigned int align_power = 0;
6075
6076
146
    if (m->p_align_valid)
6077
20
      align = p->p_align;
6078
126
    else
6079
126
      {
6080
348
        for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
6081
222
    {
6082
222
      unsigned int secalign;
6083
6084
222
      secalign = bfd_section_alignment (*secpp);
6085
222
      if (secalign > align_power)
6086
117
        align_power = secalign;
6087
222
    }
6088
126
        align = (bfd_size_type) 1 << align_power;
6089
        /* If a section requires alignment higher than the
6090
     minimum p_align value, don't reduce a maxpagesize
6091
     p->p_align set earlier in this function.  */
6092
126
        if (align > bed->p_align)
6093
75
    align_pagesize = 0;
6094
126
        if (align < maxpagesize)
6095
99
    align = maxpagesize;
6096
27
        else
6097
27
    {
6098
      /* If a section requires alignment higher than the
6099
         maximum page size, set p_align to the section
6100
         alignment.  */
6101
27
      if ((abfd->flags & D_PAGED) != 0)
6102
5
        p->p_align = align;
6103
27
    }
6104
126
      }
6105
6106
503
    for (i = 0; i < m->count; i++)
6107
357
      if ((m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
6108
        /* If we aren't making room for this section, then
6109
     it must be SHT_NOBITS regardless of what we've
6110
     set via struct bfd_elf_special_section.  */
6111
31
        elf_section_type (m->sections[i]) = SHT_NOBITS;
6112
6113
    /* Find out whether this segment contains any loadable
6114
       sections.  */
6115
146
    no_contents = true;
6116
160
    for (i = 0; i < m->count; i++)
6117
154
      if (elf_section_type (m->sections[i]) != SHT_NOBITS)
6118
140
        {
6119
140
    no_contents = false;
6120
140
    break;
6121
140
        }
6122
6123
146
    off_adjust = vma_page_aligned_bias (p->p_vaddr, off, align * opb);
6124
6125
    /* Broken hardware and/or kernel require that files do not
6126
       map the same page with different permissions on some hppa
6127
       processors.  */
6128
146
    if (j != 0
6129
93
        && (abfd->flags & D_PAGED) != 0
6130
80
        && bed->no_page_alias
6131
0
        && (off & (maxpagesize - 1)) != 0
6132
0
        && ((off & -maxpagesize)
6133
0
      == ((off + off_adjust) & -maxpagesize)))
6134
0
      off_adjust += maxpagesize;
6135
146
    off += off_adjust;
6136
146
    if (no_contents)
6137
6
      {
6138
        /* We shouldn't need to align the segment on disk since
6139
     the segment doesn't need file space, but the gABI
6140
     arguably requires the alignment and glibc ld.so
6141
     checks it.  So to comply with the alignment
6142
     requirement but not waste file space, we adjust
6143
     p_offset for just this segment.  (OFF_ADJUST is
6144
     subtracted from OFF later.)  This may put p_offset
6145
     past the end of file, but that shouldn't matter.  */
6146
6
      }
6147
140
    else
6148
140
      off_adjust = 0;
6149
146
  }
6150
      /* Make sure the .dynamic section is the first section in the
6151
   PT_DYNAMIC segment.  */
6152
476
      else if (p->p_type == PT_DYNAMIC
6153
28
         && m->count > 1
6154
1
         && ! streq (m->sections[0]->name, ".dynamic"))
6155
0
  {
6156
0
    _bfd_error_handler
6157
0
      (_("%pB: The first section in the PT_DYNAMIC segment"
6158
0
         " is not the .dynamic section"),
6159
0
       abfd);
6160
0
    bfd_set_error (bfd_error_bad_value);
6161
0
    return false;
6162
0
  }
6163
      /* Set the note section type to SHT_NOTE.  */
6164
476
      else if (p->p_type == PT_NOTE)
6165
75
  for (i = 0; i < m->count; i++)
6166
37
    elf_section_type (m->sections[i]) = SHT_NOTE;
6167
6168
622
      if (m->includes_filehdr)
6169
54
  {
6170
54
    if (!m->p_flags_valid)
6171
4
      p->p_flags |= PF_R;
6172
54
    p->p_filesz = bed->s->sizeof_ehdr;
6173
54
    p->p_memsz = bed->s->sizeof_ehdr;
6174
54
    if (p->p_type == PT_LOAD)
6175
29
      {
6176
29
        if (m->count > 0)
6177
23
    {
6178
23
      if (p->p_vaddr < (bfd_vma) off
6179
21
          || (!m->p_paddr_valid
6180
5
        && p->p_paddr < (bfd_vma) off))
6181
3
        {
6182
3
          _bfd_error_handler
6183
3
      (_("%pB: not enough room for program headers,"
6184
3
         " try linking with -N"),
6185
3
       abfd);
6186
3
          bfd_set_error (bfd_error_bad_value);
6187
3
          return false;
6188
3
        }
6189
20
      p->p_vaddr -= off;
6190
20
      if (!m->p_paddr_valid)
6191
4
        p->p_paddr -= off;
6192
20
    }
6193
29
      }
6194
25
    else if (sorted_seg_map[0]->includes_filehdr)
6195
16
      {
6196
16
        Elf_Internal_Phdr *filehdr = phdrs + sorted_seg_map[0]->idx;
6197
16
        p->p_vaddr = filehdr->p_vaddr;
6198
16
        if (!m->p_paddr_valid)
6199
4
    p->p_paddr = filehdr->p_paddr;
6200
16
      }
6201
54
  }
6202
6203
619
      if (m->includes_phdrs)
6204
52
  {
6205
52
    if (!m->p_flags_valid)
6206
3
      p->p_flags |= PF_R;
6207
52
    p->p_filesz += actual * bed->s->sizeof_phdr;
6208
52
    p->p_memsz += actual * bed->s->sizeof_phdr;
6209
52
    if (!m->includes_filehdr)
6210
22
      {
6211
22
        if (p->p_type == PT_LOAD)
6212
2
    {
6213
2
      p->p_offset = off - actual * bed->s->sizeof_phdr;
6214
2
      elf_elfheader (abfd)->e_phoff = p->p_offset;
6215
2
      if (m->count > 0)
6216
2
        {
6217
2
          p->p_vaddr -= off - p->p_offset;
6218
2
          if (!m->p_paddr_valid)
6219
0
      p->p_paddr -= off - p->p_offset;
6220
2
        }
6221
2
    }
6222
20
        else if (phdr_load_seg != NULL)
6223
12
    {
6224
      /* Also set PT_PHDR to match phdr_load_seg.  We've
6225
         sorted segments so that phdr_load_seg will
6226
         already be set by the code immediately above.  */
6227
12
      Elf_Internal_Phdr *phdr = phdrs + phdr_load_seg->idx;
6228
12
      bfd_vma phdr_off = 0;  /* Octets.  */
6229
12
      if (phdr_load_seg->includes_filehdr)
6230
12
        phdr_off = bed->s->sizeof_ehdr;
6231
12
      p->p_vaddr = phdr->p_vaddr + phdr_off;
6232
12
      if (!m->p_paddr_valid)
6233
0
        p->p_paddr = phdr->p_paddr + phdr_off;
6234
12
      p->p_offset = phdr->p_offset + phdr_off;
6235
12
    }
6236
8
        else
6237
8
    p->p_offset = bed->s->sizeof_ehdr;
6238
22
      }
6239
52
  }
6240
6241
619
      if (p->p_type == PT_LOAD
6242
470
    || (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core))
6243
156
  {
6244
156
    if (!m->includes_filehdr && !m->includes_phdrs)
6245
128
      {
6246
128
        p->p_offset = off;
6247
128
        if (no_contents)
6248
5
    {
6249
      /* Put meaningless p_offset for PT_LOAD segments
6250
         without file contents somewhere within the first
6251
         page, in an attempt to not point past EOF.  */
6252
5
      bfd_size_type align = maxpagesize;
6253
5
      if (align < p->p_align)
6254
0
        align = p->p_align;
6255
5
      if (align < 1)
6256
1
        align = 1;
6257
      /* Avoid p_offset of zero, which might be wrongly
6258
         interpreted as the segment being the first one,
6259
         containing the file header.  PR32763.  */
6260
5
      p->p_offset = (off + align - 1) % align + 1;
6261
5
    }
6262
128
      }
6263
28
    else
6264
28
      {
6265
28
        file_ptr adjust;  /* Octets.  */
6266
6267
28
        adjust = off - (p->p_offset + p->p_filesz);
6268
28
        if (!no_contents)
6269
28
    p->p_filesz += adjust;
6270
28
        p->p_memsz += adjust;
6271
28
      }
6272
156
  }
6273
6274
619
      if (align_pagesize)
6275
50
  p->p_align = align_pagesize;
6276
6277
      /* Set up p_filesz, p_memsz, p_align and p_flags from the section
6278
   maps.  Set filepos for sections in PT_LOAD segments, and in
6279
   core files, for sections in PT_NOTE segments.
6280
   assign_file_positions_for_non_load_sections will set filepos
6281
   for other sections and update p_filesz for other segments.  */
6282
2.06k
      for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
6283
1.45k
  {
6284
1.45k
    asection *sec;
6285
1.45k
    bfd_size_type align;
6286
1.45k
    Elf_Internal_Shdr *this_hdr;
6287
6288
1.45k
    sec = *secpp;
6289
1.45k
    this_hdr = &elf_section_data (sec)->this_hdr;
6290
1.45k
    align = (bfd_size_type) 1 << bfd_section_alignment (sec);
6291
6292
1.45k
    if ((p->p_type == PT_LOAD
6293
1.10k
         || p->p_type == PT_DYNAMIC
6294
1.08k
         || p->p_type == PT_TLS)
6295
369
        && (this_hdr->sh_type != SHT_NOBITS
6296
28
      || ((this_hdr->sh_flags & SHF_ALLOC) != 0
6297
28
          && ((this_hdr->sh_flags & SHF_TLS) == 0
6298
2
        || p->p_type == PT_TLS))))
6299
367
      {
6300
367
        bfd_vma p_start = p->p_paddr;   /* Octets.  */
6301
367
        bfd_vma p_end = p_start + p->p_memsz; /* Octets.  */
6302
367
        bfd_vma s_start = sec->lma * opb;   /* Octets.  */
6303
367
        bfd_vma adjust = s_start - p_end;   /* Octets.  */
6304
6305
367
        if (adjust != 0
6306
135
      && (s_start < p_end
6307
97
          || p_end < p_start))
6308
38
    {
6309
38
      _bfd_error_handler
6310
        /* xgettext:c-format */
6311
38
        (_("%pB: section %pA lma %#" PRIx64
6312
38
           " adjusted to %#" PRIx64),
6313
38
         abfd, sec, (uint64_t) s_start / opb,
6314
38
         (uint64_t) p_end / opb);
6315
38
      adjust = 0;
6316
38
      sec->lma = p_end / opb;
6317
38
    }
6318
367
        p->p_memsz += adjust;
6319
6320
367
        if (p->p_type == PT_LOAD)
6321
347
    {
6322
347
      if (this_hdr->sh_type != SHT_NOBITS)
6323
322
        {
6324
322
          off -= off_adjust;
6325
322
          off_adjust = 0;
6326
322
          if (p->p_filesz + adjust < p->p_memsz)
6327
7
      {
6328
        /* We have a PROGBITS section following NOBITS ones.
6329
           Allocate file space for the NOBITS section(s).
6330
           We don't need to write out the zeros, posix
6331
           fseek past the end of data already written
6332
           followed by a write at that location is
6333
           guaranteed to result in zeros being read
6334
           from the gap.  */
6335
7
        adjust = p->p_memsz - p->p_filesz;
6336
7
      }
6337
322
        }
6338
      /* We only adjust sh_offset in SHT_NOBITS sections
6339
         as would seem proper for their address when the
6340
         section is first in the segment.  sh_offset
6341
         doesn't really have any significance for
6342
         SHT_NOBITS anyway, apart from a notional position
6343
         relative to other sections.  Historically we
6344
         didn't bother with adjusting sh_offset and some
6345
         programs depend on it not being adjusted.  See
6346
         pr12921 and pr25662.  */
6347
347
      if (this_hdr->sh_type != SHT_NOBITS || i == 0)
6348
332
        {
6349
332
          off += adjust;
6350
332
          if (this_hdr->sh_type == SHT_NOBITS)
6351
10
      off_adjust += adjust;
6352
332
        }
6353
347
    }
6354
367
        if (this_hdr->sh_type != SHT_NOBITS)
6355
341
    p->p_filesz += adjust;
6356
367
      }
6357
6358
1.45k
    if (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core)
6359
8
      {
6360
        /* The section at i == 0 is the one that actually contains
6361
     everything.  */
6362
8
        if (i == 0)
6363
3
    {
6364
3
      this_hdr->sh_offset = sec->filepos = off;
6365
3
      off += this_hdr->sh_size;
6366
3
      p->p_filesz = this_hdr->sh_size;
6367
3
      p->p_memsz = 0;
6368
3
      p->p_align = 1;
6369
3
    }
6370
5
        else
6371
5
    {
6372
      /* The rest are fake sections that shouldn't be written.  */
6373
5
      sec->filepos = 0;
6374
5
      sec->size = 0;
6375
5
      sec->flags = 0;
6376
5
      continue;
6377
5
    }
6378
8
      }
6379
1.44k
    else
6380
1.44k
      {
6381
1.44k
        if (this_hdr->sh_type == SHT_NOBITS
6382
93
      && (this_hdr->sh_flags & SHF_TLS) != 0
6383
2
      && this_hdr->sh_offset == 0)
6384
2
    {
6385
      /* Set sh_offset for .tbss sections to their nominal
6386
         offset after aligning.  They are not loaded from
6387
         disk so the value doesn't really matter, except
6388
         when the .tbss section is the first one in a
6389
         PT_TLS segment.  In that case it sets the
6390
         p_offset for the PT_TLS segment, which according
6391
         to the ELF gABI ought to satisfy
6392
         p_offset % p_align == p_vaddr % p_align.  */
6393
2
      bfd_vma adjust = vma_page_aligned_bias (this_hdr->sh_addr,
6394
2
                off, align);
6395
2
      this_hdr->sh_offset = sec->filepos = off + adjust;
6396
2
    }
6397
1.44k
        else if (p->p_type == PT_LOAD)
6398
347
    {
6399
347
      this_hdr->sh_offset = sec->filepos = off;
6400
347
      if (this_hdr->sh_type != SHT_NOBITS)
6401
322
        off += this_hdr->sh_size;
6402
347
    }
6403
6404
1.44k
        if (this_hdr->sh_type != SHT_NOBITS)
6405
1.34k
    {
6406
1.34k
      p->p_filesz += this_hdr->sh_size;
6407
      /* A load section without SHF_ALLOC is something like
6408
         a note section in a PT_NOTE segment.  These take
6409
         file space but are not loaded into memory.  */
6410
1.34k
      if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
6411
509
        p->p_memsz += this_hdr->sh_size;
6412
1.34k
    }
6413
93
        else if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
6414
93
    {
6415
93
      if (p->p_type == PT_TLS)
6416
0
        p->p_memsz += this_hdr->sh_size;
6417
6418
      /* .tbss is special.  It doesn't contribute to p_memsz of
6419
         normal segments.  */
6420
93
      else if ((this_hdr->sh_flags & SHF_TLS) == 0)
6421
91
        p->p_memsz += this_hdr->sh_size;
6422
93
    }
6423
6424
1.44k
        if (align > p->p_align
6425
643
      && !m->p_align_valid
6426
93
      && (p->p_type != PT_LOAD
6427
25
          || (abfd->flags & D_PAGED) == 0))
6428
93
    p->p_align = align;
6429
1.44k
      }
6430
6431
1.44k
    if (!m->p_flags_valid)
6432
124
      {
6433
124
        p->p_flags |= PF_R;
6434
124
        if ((this_hdr->sh_flags & SHF_EXECINSTR) != 0)
6435
23
    p->p_flags |= PF_X;
6436
124
        if ((this_hdr->sh_flags & SHF_WRITE) != 0)
6437
31
    p->p_flags |= PF_W;
6438
124
      }
6439
1.44k
  }
6440
6441
619
      off -= off_adjust;
6442
6443
      /* PR ld/20815 - Check that the program header segment, if
6444
   present, will be loaded into memory.  */
6445
619
      if (p->p_type == PT_PHDR
6446
24
    && phdr_load_seg == NULL
6447
10
    && !(bed->elf_backend_allow_non_load_phdr != NULL
6448
0
         && bed->elf_backend_allow_non_load_phdr (abfd, phdrs, alloc)))
6449
10
  {
6450
    /* The fix for this error is usually to edit the linker script being
6451
       used and set up the program headers manually.  Either that or
6452
       leave room for the headers at the start of the SECTIONS.  */
6453
10
    _bfd_error_handler (_("%pB: error: PHDR segment not covered"
6454
10
        " by LOAD segment"),
6455
10
            abfd);
6456
10
    if (link_info == NULL)
6457
10
      return false;
6458
    /* Arrange for the linker to exit with an error, deleting
6459
       the output file unless --noinhibit-exec is given.  */
6460
0
    link_info->callbacks->info ("%X");
6461
0
  }
6462
6463
      /* Check that all sections are in a PT_LOAD segment.
6464
   Don't check funky gdb generated core files.  */
6465
609
      if (p->p_type == PT_LOAD && bfd_get_format (abfd) != bfd_core)
6466
128
  {
6467
128
    bool check_vma = true;
6468
6469
318
    for (i = 1; i < m->count; i++)
6470
190
      if (m->sections[i]->vma == m->sections[i - 1]->vma
6471
0
    && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i])
6472
0
               ->this_hdr), p) != 0
6473
0
    && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i - 1])
6474
0
               ->this_hdr), p) != 0)
6475
0
        {
6476
    /* Looks like we have overlays packed into the segment.  */
6477
0
    check_vma = false;
6478
0
    break;
6479
0
        }
6480
6481
441
    for (i = 0; i < m->count; i++)
6482
313
      {
6483
313
        Elf_Internal_Shdr *this_hdr;
6484
313
        asection *sec;
6485
6486
313
        sec = m->sections[i];
6487
313
        this_hdr = &(elf_section_data(sec)->this_hdr);
6488
313
        if (!ELF_SECTION_IN_SEGMENT_1 (this_hdr, p, check_vma, 0)
6489
28
      && !ELF_TBSS_SPECIAL (this_hdr, p))
6490
28
    {
6491
28
      _bfd_error_handler
6492
        /* xgettext:c-format */
6493
28
        (_("%pB: section `%pA' can't be allocated in segment %u"),
6494
28
         abfd, sec, m->idx);
6495
28
      print_segment_map (m);
6496
28
    }
6497
313
      }
6498
128
  }
6499
609
    }
6500
6501
150
  elf_next_file_pos (abfd) = off;
6502
6503
150
  if (link_info != NULL
6504
0
      && phdr_load_seg != NULL
6505
0
      && phdr_load_seg->includes_filehdr)
6506
0
    {
6507
      /* There is a segment that contains both the file headers and the
6508
   program headers, so provide a symbol __ehdr_start pointing there.
6509
   A program can use this to examine itself robustly.  */
6510
6511
0
      struct elf_link_hash_table *htab = elf_hash_table (link_info);
6512
0
      struct elf_link_hash_entry *hash = htab->hehdr_start;
6513
6514
      /* If the symbol was referenced and not defined, define it.  */
6515
0
      if (hash != NULL
6516
0
    && (hash->root.type == bfd_link_hash_new
6517
0
        || hash->root.type == bfd_link_hash_undefined
6518
0
        || hash->root.type == bfd_link_hash_undefweak
6519
0
        || hash->root.type == bfd_link_hash_common))
6520
0
  {
6521
0
    asection *s = NULL;
6522
0
    bfd_vma filehdr_vaddr = phdrs[phdr_load_seg->idx].p_vaddr / opb;
6523
6524
0
    if (phdr_load_seg->count != 0)
6525
      /* The segment contains sections, so use the first one.  */
6526
0
      s = phdr_load_seg->sections[0];
6527
0
    else
6528
      /* Use the first (i.e. lowest-addressed) section in any segment.  */
6529
0
      for (m = elf_seg_map (abfd); m != NULL; m = m->next)
6530
0
        if (m->p_type == PT_LOAD && m->count != 0)
6531
0
    {
6532
0
      s = m->sections[0];
6533
0
      break;
6534
0
    }
6535
6536
0
    if (s != NULL)
6537
0
      {
6538
0
        hash->root.u.def.value = filehdr_vaddr - s->vma;
6539
0
        hash->root.u.def.section = s;
6540
0
      }
6541
0
    else
6542
0
      {
6543
0
        hash->root.u.def.value = filehdr_vaddr;
6544
0
        hash->root.u.def.section = bfd_abs_section_ptr;
6545
0
      }
6546
6547
0
    hash->root.type = bfd_link_hash_defined;
6548
0
    hash->def_regular = 1;
6549
0
    hash->non_elf = 0;
6550
0
  }
6551
0
    }
6552
6553
150
  return true;
6554
163
}
6555
6556
/* Determine if a bfd is a debuginfo file.  Unfortunately there
6557
   is no defined method for detecting such files, so we have to
6558
   use heuristics instead.  */
6559
6560
bool
6561
is_debuginfo_file (bfd *abfd)
6562
84
{
6563
84
  if (abfd == NULL || bfd_get_flavour (abfd) != bfd_target_elf_flavour)
6564
0
    return false;
6565
6566
84
  Elf_Internal_Shdr **start_headers = elf_elfsections (abfd);
6567
84
  Elf_Internal_Shdr **end_headers = start_headers + elf_numsections (abfd);
6568
84
  Elf_Internal_Shdr **headerp;
6569
6570
209
  for (headerp = start_headers; headerp < end_headers; headerp ++)
6571
209
    {
6572
209
      Elf_Internal_Shdr *header = * headerp;
6573
6574
      /* Debuginfo files do not have any allocated SHT_PROGBITS sections.
6575
   The only allocated sections are SHT_NOBITS or SHT_NOTES.  */
6576
209
      if ((header->sh_flags & SHF_ALLOC) == SHF_ALLOC
6577
116
    && header->sh_type != SHT_NOBITS
6578
116
    && header->sh_type != SHT_NOTE)
6579
84
  return false;
6580
209
    }
6581
6582
0
  return true;
6583
84
}
6584
6585
/* Assign file positions for other sections, except for compressed debug
6586
   and sections assigned in _bfd_elf_assign_file_positions_for_non_load.  */
6587
6588
static bool
6589
assign_file_positions_for_non_load_sections (bfd *abfd,
6590
               struct bfd_link_info *link_info)
6591
306
{
6592
306
  elf_backend_data *bed = get_elf_backend_data (abfd);
6593
306
  Elf_Internal_Shdr **i_shdrpp;
6594
306
  Elf_Internal_Shdr **hdrpp, **end_hdrpp;
6595
306
  Elf_Internal_Phdr *phdrs;
6596
306
  Elf_Internal_Phdr *p;
6597
306
  struct elf_segment_map *m;
6598
306
  file_ptr off;
6599
306
  unsigned int opb = bfd_octets_per_byte (abfd, NULL);
6600
306
  bfd_vma maxpagesize;
6601
6602
306
  if (link_info != NULL)
6603
0
    maxpagesize = link_info->maxpagesize;
6604
306
  else
6605
306
    maxpagesize = bed->maxpagesize;
6606
306
  i_shdrpp = elf_elfsections (abfd);
6607
306
  end_hdrpp = i_shdrpp + elf_numsections (abfd);
6608
306
  off = elf_next_file_pos (abfd);
6609
1.50k
  for (hdrpp = i_shdrpp + 1; hdrpp < end_hdrpp; hdrpp++)
6610
1.19k
    {
6611
1.19k
      Elf_Internal_Shdr *hdr;
6612
1.19k
      bfd_vma align;
6613
6614
1.19k
      hdr = *hdrpp;
6615
1.19k
      if (hdr->bfd_section != NULL
6616
845
    && (hdr->bfd_section->filepos != 0
6617
522
        || (hdr->sh_type == SHT_NOBITS
6618
15
      && hdr->contents == NULL)))
6619
338
  BFD_ASSERT (hdr->sh_offset == hdr->bfd_section->filepos);
6620
861
      else if ((hdr->sh_flags & SHF_ALLOC) != 0)
6621
89
  {
6622
89
    if (hdr->sh_size != 0
6623
        /* PR 24717 - debuginfo files are known to be not strictly
6624
     compliant with the ELF standard.  In particular they often
6625
     have .note.gnu.property sections that are outside of any
6626
     loadable segment.  This is not a problem for such files,
6627
     so do not warn about them.  */
6628
84
        && ! is_debuginfo_file (abfd))
6629
84
      _bfd_error_handler
6630
        /* xgettext:c-format */
6631
84
        (_("%pB: warning: allocated section `%s' not in segment"),
6632
84
         abfd,
6633
84
         (hdr->bfd_section == NULL
6634
84
    ? "*unknown*"
6635
84
    : hdr->bfd_section->name));
6636
    /* We don't need to page align empty sections.  */
6637
89
    if ((abfd->flags & D_PAGED) != 0 && hdr->sh_size != 0)
6638
78
      align = maxpagesize;
6639
11
    else
6640
11
      align = hdr->sh_addralign & -hdr->sh_addralign;
6641
89
    off += vma_page_aligned_bias (hdr->sh_addr, off, align);
6642
89
    off = _bfd_elf_assign_file_position_for_section (hdr, off, false,
6643
89
                 bed->s->log_file_align);
6644
89
  }
6645
772
      else if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
6646
25
    && hdr->bfd_section == NULL)
6647
         /* We don't know the offset of these sections yet:
6648
      their size has not been decided.  */
6649
758
         || (abfd->is_linker_output
6650
0
       && hdr->bfd_section != NULL
6651
0
       && (hdr->sh_name == -1u
6652
0
           || bfd_section_is_ctf (hdr->bfd_section)))
6653
758
         || hdr == i_shdrpp[elf_onesymtab (abfd)]
6654
741
         || (elf_symtab_shndx_list (abfd) != NULL
6655
0
       && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx])
6656
741
         || hdr == i_shdrpp[elf_strtab_sec (abfd)]
6657
724
         || hdr == i_shdrpp[elf_shstrtab_sec (abfd)])
6658
354
  hdr->sh_offset = -1;
6659
418
      else
6660
418
  off = _bfd_elf_assign_file_position_for_section (hdr, off, true, 0);
6661
1.19k
    }
6662
306
  elf_next_file_pos (abfd) = off;
6663
6664
  /* Now that we have set the section file positions, we can set up
6665
     the file positions for the non PT_LOAD segments.  */
6666
306
  phdrs = elf_tdata (abfd)->phdr;
6667
858
  for (m = elf_seg_map (abfd), p = phdrs; m != NULL; m = m->next, p++)
6668
564
    {
6669
564
      if (p->p_type == PT_GNU_RELRO)
6670
8
  {
6671
8
    bfd_vma start, end;  /* Bytes.  */
6672
8
    bool ok;
6673
6674
8
    if (link_info != NULL)
6675
0
      {
6676
        /* During linking the range of the RELRO segment is passed
6677
     in link_info.  Note that there may be padding between
6678
     relro_start and the first RELRO section.  */
6679
0
        start = link_info->relro_start;
6680
0
        end = link_info->relro_end;
6681
0
      }
6682
8
    else if (m->count != 0)
6683
8
      {
6684
8
        if (!m->p_size_valid)
6685
0
    abort ();
6686
8
        start = m->sections[0]->vma;
6687
8
        end = start + m->p_size / opb;
6688
8
      }
6689
0
    else
6690
0
      {
6691
0
        start = 0;
6692
0
        end = 0;
6693
0
      }
6694
6695
8
    ok = false;
6696
8
    if (start < end)
6697
8
      {
6698
8
        struct elf_segment_map *lm;
6699
8
        const Elf_Internal_Phdr *lp;
6700
8
        unsigned int i;
6701
6702
        /* Find a LOAD segment containing a section in the RELRO
6703
     segment.  */
6704
8
        for (lm = elf_seg_map (abfd), lp = phdrs;
6705
32
       lm != NULL;
6706
24
       lm = lm->next, lp++)
6707
32
    {
6708
32
      if (lp->p_type == PT_LOAD
6709
16
          && lm->count != 0
6710
13
          && (lm->sections[lm->count - 1]->vma
6711
13
        + (!IS_TBSS (lm->sections[lm->count - 1])
6712
13
           ? lm->sections[lm->count - 1]->size / opb
6713
13
           : 0)) > start
6714
8
          && lm->sections[0]->vma < end)
6715
8
        break;
6716
32
    }
6717
6718
8
        if (lm != NULL)
6719
8
    {
6720
      /* Find the section starting the RELRO segment.  */
6721
8
      for (i = 0; i < lm->count; i++)
6722
8
        {
6723
8
          asection *s = lm->sections[i];
6724
8
          if (s->vma >= start
6725
8
        && s->vma < end
6726
8
        && s->size != 0)
6727
8
      break;
6728
8
        }
6729
6730
8
      if (i < lm->count)
6731
8
        {
6732
8
          p->p_vaddr = lm->sections[i]->vma * opb;
6733
8
          p->p_paddr = lm->sections[i]->lma * opb;
6734
8
          p->p_offset = lm->sections[i]->filepos;
6735
8
          p->p_memsz = end * opb - p->p_vaddr;
6736
8
          p->p_filesz = p->p_memsz;
6737
6738
          /* The RELRO segment typically ends a few bytes
6739
       into .got.plt but other layouts are possible.
6740
       In cases where the end does not match any
6741
       loaded section (for instance is in file
6742
       padding), trim p_filesz back to correspond to
6743
       the end of loaded section contents.  */
6744
8
          if (p->p_filesz > lp->p_vaddr + lp->p_filesz - p->p_vaddr)
6745
2
      p->p_filesz = lp->p_vaddr + lp->p_filesz - p->p_vaddr;
6746
6747
          /* Preserve the alignment and flags if they are
6748
       valid.  The gold linker generates RW/4 for
6749
       the PT_GNU_RELRO section.  It is better for
6750
       objcopy/strip to honor these attributes
6751
       otherwise gdb will choke when using separate
6752
       debug files.  */
6753
8
          if (!m->p_align_valid)
6754
0
      p->p_align = 1;
6755
8
          if (!m->p_flags_valid)
6756
0
      p->p_flags = PF_R;
6757
8
          ok = true;
6758
8
        }
6759
8
    }
6760
8
      }
6761
6762
8
    if (!ok)
6763
0
      {
6764
0
        if (link_info != NULL)
6765
0
    _bfd_error_handler
6766
0
      (_("%pB: warning: unable to allocate any sections"
6767
0
         " to PT_GNU_RELRO segment"),
6768
0
       abfd);
6769
0
        memset (p, 0, sizeof *p);
6770
0
      }
6771
8
  }
6772
556
      else if (p->p_type == PT_GNU_STACK)
6773
4
  {
6774
4
    if (m->p_size_valid)
6775
2
      p->p_memsz = m->p_size;
6776
4
  }
6777
552
      else if (m->count != 0)
6778
313
  {
6779
313
    unsigned int i;
6780
6781
313
    if (p->p_type != PT_LOAD
6782
179
        && (p->p_type != PT_NOTE
6783
20
      || bfd_get_format (abfd) != bfd_core))
6784
176
      {
6785
        /* A user specified segment layout may include a PHDR
6786
     segment that overlaps with a LOAD segment...  */
6787
176
        if (p->p_type == PT_PHDR)
6788
1
    {
6789
1
      m->count = 0;
6790
1
      continue;
6791
1
    }
6792
6793
175
        if (m->includes_filehdr || m->includes_phdrs)
6794
12
    {
6795
      /* PR 17512: file: 2195325e.  */
6796
12
      _bfd_error_handler
6797
12
        (_("%pB: error: non-load segment %d includes file header "
6798
12
           "and/or program header"),
6799
12
         abfd, (int) (p - phdrs));
6800
12
      return false;
6801
12
    }
6802
6803
163
        p->p_filesz = 0;
6804
163
        p->p_offset = m->sections[0]->filepos;
6805
169
        for (i = m->count; i-- != 0;)
6806
169
    {
6807
169
      asection *sect = m->sections[i];
6808
169
      Elf_Internal_Shdr *hdr = &elf_section_data (sect)->this_hdr;
6809
169
      if (hdr->sh_type != SHT_NOBITS)
6810
163
        {
6811
163
          p->p_filesz = sect->filepos - p->p_offset + hdr->sh_size;
6812
          /* NB: p_memsz of the loadable PT_NOTE segment
6813
       should be the same as p_filesz.  */
6814
163
          if (p->p_type == PT_NOTE
6815
17
        && (hdr->sh_flags & SHF_ALLOC) != 0)
6816
16
      p->p_memsz = p->p_filesz;
6817
163
          break;
6818
163
        }
6819
169
    }
6820
163
      }
6821
313
  }
6822
564
    }
6823
6824
294
  return true;
6825
306
}
6826
6827
static elf_section_list *
6828
find_section_in_list (unsigned int i, elf_section_list * list)
6829
575
{
6830
595
  for (;list != NULL; list = list->next)
6831
21
    if (list->ndx == i)
6832
1
      break;
6833
575
  return list;
6834
575
}
6835
6836
/* Work out the file positions of all the sections.  This is called by
6837
   _bfd_elf_compute_section_file_positions.  All the section sizes and
6838
   VMAs must be known before this is called.
6839
6840
   Reloc sections come in two flavours: Those processed specially as
6841
   "side-channel" data attached to a section to which they apply, and
6842
   those that bfd doesn't process as relocations.  The latter sort are
6843
   stored in a normal bfd section by bfd_section_from_shdr.  We don't
6844
   consider the former sort here, unless they form part of the loadable
6845
   image.  Reloc sections not assigned here (and compressed debugging
6846
   sections and CTF sections which nothing else in the file can rely
6847
   upon) will be handled later by assign_file_positions_for_relocs.
6848
6849
   We also don't set the positions of the .symtab and .strtab here.  */
6850
6851
static bool
6852
assign_file_positions_except_relocs (bfd *abfd,
6853
             struct bfd_link_info *link_info)
6854
573
{
6855
573
  struct elf_obj_tdata *tdata = elf_tdata (abfd);
6856
573
  Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd);
6857
573
  elf_backend_data *bed = get_elf_backend_data (abfd);
6858
573
  unsigned int alloc;
6859
6860
573
  if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0
6861
471
      && bfd_get_format (abfd) != bfd_core)
6862
248
    {
6863
248
      Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd);
6864
248
      unsigned int num_sec = elf_numsections (abfd);
6865
248
      Elf_Internal_Shdr **hdrpp;
6866
248
      unsigned int i;
6867
248
      file_ptr off;
6868
6869
      /* Start after the ELF header.  */
6870
248
      off = i_ehdrp->e_ehsize;
6871
6872
      /* We are not creating an executable, which means that we are
6873
   not creating a program header, and that the actual order of
6874
   the sections in the file is unimportant.  */
6875
1.46k
      for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++)
6876
1.21k
  {
6877
1.21k
    Elf_Internal_Shdr *hdr;
6878
6879
1.21k
    hdr = *hdrpp;
6880
1.21k
    if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
6881
180
         && hdr->bfd_section == NULL)
6882
        /* Do not assign offsets for these sections yet: we don't know
6883
     their sizes.  */
6884
1.07k
        || (abfd->is_linker_output
6885
0
      && hdr->bfd_section != NULL
6886
0
      && (hdr->sh_name == -1u
6887
0
          || bfd_section_is_ctf (hdr->bfd_section)))
6888
1.07k
        || i == elf_onesymtab (abfd)
6889
1.03k
        || (elf_symtab_shndx_list (abfd) != NULL
6890
0
      && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx])
6891
1.03k
        || i == elf_strtab_sec (abfd)
6892
985
        || i == elf_shstrtab_sec (abfd))
6893
478
      {
6894
478
        hdr->sh_offset = -1;
6895
478
      }
6896
737
    else
6897
      /* There shouldn't be a need to effect "capped" file alignment here,
6898
         yet at least the Linux kernel's modpost utility was found to be
6899
         unhappy without.  While the issue was addressed there, let's be
6900
         kind for at least the foreseeable future ...  */
6901
737
      off = _bfd_elf_assign_file_position_for_section (hdr, off, false,
6902
737
                   bed->s->log_file_align);
6903
1.21k
  }
6904
6905
248
      elf_next_file_pos (abfd) = off;
6906
248
      elf_program_header_size (abfd) = 0;
6907
248
    }
6908
325
  else
6909
325
    {
6910
      /* Assign file positions for the loaded sections based on the
6911
   assignment of sections to segments.  */
6912
325
      if (!assign_file_positions_for_load_sections (abfd, link_info))
6913
19
  return false;
6914
6915
      /* And for non-load sections.  */
6916
306
      if (!assign_file_positions_for_non_load_sections (abfd, link_info))
6917
12
  return false;
6918
306
    }
6919
6920
542
  if (!(*bed->elf_backend_modify_headers) (abfd, link_info))
6921
0
    return false;
6922
6923
  /* Write out the program headers.  */
6924
542
  alloc = i_ehdrp->e_phnum;
6925
542
  if (alloc != 0)
6926
138
    {
6927
138
      if (link_info != NULL && ! link_info->no_warn_rwx_segments)
6928
0
  {
6929
0
    bool warned_tls = false;
6930
0
    bool warned_rwx = false;
6931
6932
    /* Memory resident segments with non-zero size and RWX
6933
       permissions are a security risk, so we generate a warning
6934
       here if we are creating any.  */
6935
0
    unsigned int i;
6936
6937
0
    for (i = 0; i < alloc; i++)
6938
0
      {
6939
0
        const Elf_Internal_Phdr * phdr = tdata->phdr + i;
6940
6941
0
        if (phdr->p_memsz == 0)
6942
0
    continue;
6943
6944
0
        if (! warned_tls
6945
0
      && phdr->p_type == PT_TLS
6946
0
      && (phdr->p_flags & PF_X))
6947
0
    {
6948
0
      if (link_info->warn_is_error_for_rwx_segments)
6949
0
        {
6950
0
          _bfd_error_handler (_("\
6951
0
error: %pB has a TLS segment with execute permission"),
6952
0
            abfd);
6953
0
          return false;
6954
0
        }
6955
6956
0
      _bfd_error_handler (_("\
6957
0
warning: %pB has a TLS segment with execute permission"),
6958
0
              abfd);
6959
0
      if (warned_rwx)
6960
0
        break;
6961
6962
0
      warned_tls = true;
6963
0
    }
6964
0
        else if (! warned_rwx
6965
0
           && phdr->p_type == PT_LOAD
6966
0
           && ((phdr->p_flags & (PF_R | PF_W | PF_X))
6967
0
         == (PF_R | PF_W | PF_X)))
6968
0
    {
6969
0
      if (link_info->warn_is_error_for_rwx_segments)
6970
0
        {
6971
0
          _bfd_error_handler (_("\
6972
0
error: %pB has a LOAD segment with RWX permissions"),
6973
0
            abfd);
6974
0
          return false;
6975
0
        }
6976
6977
0
      _bfd_error_handler (_("\
6978
0
warning: %pB has a LOAD segment with RWX permissions"),
6979
0
              abfd);
6980
0
      if (warned_tls)
6981
0
        break;
6982
6983
0
      warned_rwx = true;
6984
0
    }
6985
0
      }
6986
0
  }
6987
6988
138
      if (bfd_seek (abfd, i_ehdrp->e_phoff, SEEK_SET) != 0
6989
138
    || bed->s->write_out_phdrs (abfd, tdata->phdr, alloc) != 0)
6990
0
  return false;
6991
138
    }
6992
6993
542
  return true;
6994
542
}
6995
6996
bool
6997
_bfd_elf_init_file_header (bfd *abfd,
6998
         struct bfd_link_info *info ATTRIBUTE_UNUSED)
6999
574
{
7000
574
  Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form.  */
7001
574
  struct elf_strtab_hash *shstrtab;
7002
574
  elf_backend_data *bed = get_elf_backend_data (abfd);
7003
7004
574
  i_ehdrp = elf_elfheader (abfd);
7005
7006
574
  shstrtab = _bfd_elf_strtab_init ();
7007
574
  if (shstrtab == NULL)
7008
0
    return false;
7009
7010
574
  elf_shstrtab (abfd) = shstrtab;
7011
7012
574
  i_ehdrp->e_ident[EI_MAG0] = ELFMAG0;
7013
574
  i_ehdrp->e_ident[EI_MAG1] = ELFMAG1;
7014
574
  i_ehdrp->e_ident[EI_MAG2] = ELFMAG2;
7015
574
  i_ehdrp->e_ident[EI_MAG3] = ELFMAG3;
7016
7017
574
  i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass;
7018
574
  i_ehdrp->e_ident[EI_DATA] =
7019
574
    bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB;
7020
574
  i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current;
7021
7022
574
  if ((abfd->flags & DYNAMIC) != 0)
7023
54
    i_ehdrp->e_type = ET_DYN;
7024
520
  else if ((abfd->flags & EXEC_P) != 0)
7025
48
    i_ehdrp->e_type = ET_EXEC;
7026
472
  else if (bfd_get_format (abfd) == bfd_core)
7027
223
    i_ehdrp->e_type = ET_CORE;
7028
249
  else
7029
249
    i_ehdrp->e_type = ET_REL;
7030
7031
574
  switch (bfd_get_arch (abfd))
7032
574
    {
7033
0
    case bfd_arch_unknown:
7034
0
      i_ehdrp->e_machine = EM_NONE;
7035
0
      break;
7036
7037
      /* There used to be a long list of cases here, each one setting
7038
   e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
7039
   in the corresponding bfd definition.  To avoid duplication,
7040
   the switch was removed.  Machines that need special handling
7041
   can generally do it in elf_backend_final_write_processing(),
7042
   unless they need the information earlier than the final write.
7043
   Such need can generally be supplied by replacing the tests for
7044
   e_machine with the conditions used to determine it.  */
7045
574
    default:
7046
574
      i_ehdrp->e_machine = bed->elf_machine_code;
7047
574
    }
7048
7049
574
  i_ehdrp->e_version = bed->s->ev_current;
7050
574
  i_ehdrp->e_ehsize = bed->s->sizeof_ehdr;
7051
7052
  /* No program header, for now.  */
7053
574
  i_ehdrp->e_phoff = 0;
7054
574
  i_ehdrp->e_phentsize = 0;
7055
574
  i_ehdrp->e_phnum = 0;
7056
7057
  /* Each bfd section is section header entry.  */
7058
574
  i_ehdrp->e_entry = bfd_get_start_address (abfd);
7059
574
  i_ehdrp->e_shentsize = bed->s->sizeof_shdr;
7060
7061
574
  elf_tdata (abfd)->symtab_hdr.sh_name =
7062
574
    (unsigned int) _bfd_elf_strtab_add (shstrtab, ".symtab", false);
7063
574
  elf_tdata (abfd)->strtab_hdr.sh_name =
7064
574
    (unsigned int) _bfd_elf_strtab_add (shstrtab, ".strtab", false);
7065
574
  elf_tdata (abfd)->shstrtab_hdr.sh_name =
7066
574
    (unsigned int) _bfd_elf_strtab_add (shstrtab, ".shstrtab", false);
7067
574
  if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
7068
574
      || elf_tdata (abfd)->strtab_hdr.sh_name == (unsigned int) -1
7069
574
      || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1)
7070
0
    return false;
7071
7072
574
  return true;
7073
574
}
7074
7075
/* Set e_type in ELF header to ET_EXEC for -pie -Ttext-segment=.
7076
7077
   FIXME: We used to have code here to sort the PT_LOAD segments into
7078
   ascending order, as per the ELF spec.  But this breaks some programs,
7079
   including the Linux kernel.  But really either the spec should be
7080
   changed or the programs updated.  */
7081
7082
bool
7083
_bfd_elf_modify_headers (bfd *obfd, struct bfd_link_info *link_info)
7084
542
{
7085
542
  if (link_info != NULL && bfd_link_pie (link_info))
7086
0
    {
7087
0
      Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (obfd);
7088
0
      unsigned int num_segments = i_ehdrp->e_phnum;
7089
0
      struct elf_obj_tdata *tdata = elf_tdata (obfd);
7090
0
      Elf_Internal_Phdr *segment = tdata->phdr;
7091
0
      Elf_Internal_Phdr *end_segment = &segment[num_segments];
7092
7093
      /* Find the lowest p_vaddr in PT_LOAD segments.  */
7094
0
      bfd_vma p_vaddr = (bfd_vma) -1;
7095
0
      for (; segment < end_segment; segment++)
7096
0
  if (segment->p_type == PT_LOAD && p_vaddr > segment->p_vaddr)
7097
0
    p_vaddr = segment->p_vaddr;
7098
7099
      /* Set e_type to ET_EXEC if the lowest p_vaddr in PT_LOAD
7100
   segments is non-zero.  */
7101
0
      if (p_vaddr)
7102
0
  i_ehdrp->e_type = ET_EXEC;
7103
0
    }
7104
542
  return true;
7105
542
}
7106
7107
/* Assign file positions for all the reloc sections which are not part
7108
   of the loadable file image, and the file position of section headers.  */
7109
7110
static bool
7111
_bfd_elf_assign_file_positions_for_non_load (bfd *abfd)
7112
538
{
7113
538
  file_ptr off;
7114
538
  Elf_Internal_Shdr **shdrpp, **end_shdrpp;
7115
538
  Elf_Internal_Shdr *shdrp;
7116
538
  Elf_Internal_Ehdr *i_ehdrp;
7117
538
  elf_backend_data *bed = get_elf_backend_data (abfd);
7118
7119
  /* Skip non-load sections without section header.  */
7120
538
  if ((abfd->flags & BFD_NO_SECTION_HEADER) != 0)
7121
0
    return true;
7122
7123
538
  off = elf_next_file_pos (abfd);
7124
7125
538
  shdrpp = elf_elfsections (abfd);
7126
538
  end_shdrpp = shdrpp + elf_numsections (abfd);
7127
2.86k
  for (shdrpp++; shdrpp < end_shdrpp; shdrpp++)
7128
2.32k
    {
7129
2.32k
      shdrp = *shdrpp;
7130
2.32k
      if (shdrp->sh_offset == -1)
7131
688
  {
7132
688
    asection *sec = shdrp->bfd_section;
7133
688
    if (sec == NULL
7134
0
        || shdrp->sh_type == SHT_REL
7135
0
        || shdrp->sh_type == SHT_RELA)
7136
688
      ;
7137
0
    else if (bfd_section_is_ctf (sec))
7138
0
      {
7139
        /* Update section size and contents.  */
7140
0
        shdrp->sh_size = sec->size;
7141
0
        shdrp->contents = sec->contents;
7142
0
      }
7143
0
    else if (shdrp->sh_name == -1u)
7144
0
      {
7145
0
        const char *name = sec->name;
7146
0
        struct bfd_elf_section_data *d;
7147
7148
        /* Compress DWARF debug sections.  */
7149
0
        if (!bfd_compress_section (abfd, sec, shdrp->contents))
7150
0
    return false;
7151
7152
0
        if (sec->compress_status == COMPRESS_SECTION_DONE
7153
0
      && (abfd->flags & BFD_COMPRESS_GABI) == 0
7154
0
      && name[1] == 'd')
7155
0
    {
7156
      /* If section is compressed with zlib-gnu, convert
7157
         section name from .debug_* to .zdebug_*.  */
7158
0
      char *new_name = bfd_debug_name_to_zdebug (abfd, name);
7159
0
      if (new_name == NULL)
7160
0
        return false;
7161
0
      name = new_name;
7162
0
    }
7163
        /* Add section name to section name section.  */
7164
0
        shdrp->sh_name
7165
0
    = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
7166
0
                  name, false);
7167
0
        d = elf_section_data (sec);
7168
7169
        /* Add reloc section name to section name section.  */
7170
0
        if (d->rel.hdr
7171
0
      && !_bfd_elf_set_reloc_sh_name (abfd, d->rel.hdr,
7172
0
              name, false))
7173
0
    return false;
7174
0
        if (d->rela.hdr
7175
0
      && !_bfd_elf_set_reloc_sh_name (abfd, d->rela.hdr,
7176
0
              name, true))
7177
0
    return false;
7178
7179
        /* Update section size and contents.  */
7180
0
        shdrp->sh_size = sec->size;
7181
0
        shdrp->contents = sec->contents;
7182
0
        sec->contents = NULL;
7183
0
      }
7184
7185
688
    off = _bfd_elf_assign_file_position_for_section (shdrp, off,
7186
688
      (abfd->flags & (EXEC_P | DYNAMIC))
7187
590
      || bfd_get_format (abfd) == bfd_core,
7188
688
      bed->s->log_file_align);
7189
688
  }
7190
2.32k
    }
7191
7192
  /* Place section name section after DWARF debug sections have been
7193
     compressed.  */
7194
538
  _bfd_elf_strtab_finalize (elf_shstrtab (abfd));
7195
538
  shdrp = &elf_tdata (abfd)->shstrtab_hdr;
7196
538
  shdrp->sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd));
7197
538
  off = _bfd_elf_assign_file_position_for_section (shdrp, off, true, 0);
7198
7199
  /* Place the section headers.  */
7200
538
  i_ehdrp = elf_elfheader (abfd);
7201
538
  off = BFD_ALIGN (off, 1u << bed->s->log_file_align);
7202
538
  i_ehdrp->e_shoff = off;
7203
538
  off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize;
7204
538
  elf_next_file_pos (abfd) = off;
7205
7206
538
  return true;
7207
538
}
7208
7209
bool
7210
_bfd_elf_write_object_contents (bfd *abfd)
7211
544
{
7212
544
  elf_backend_data *bed = get_elf_backend_data (abfd);
7213
544
  Elf_Internal_Shdr **i_shdrp;
7214
544
  bool failed;
7215
544
  unsigned int count, num_sec;
7216
544
  struct elf_obj_tdata *t;
7217
7218
544
  if (! abfd->output_has_begun
7219
399
      && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
7220
6
    return false;
7221
  /* Do not rewrite ELF data when the BFD has been opened for update.
7222
     abfd->output_has_begun was set to TRUE on opening, so creation of
7223
     new sections, and modification of existing section sizes was
7224
     restricted.  This means the ELF header, program headers and
7225
     section headers can't have changed.  If the contents of any
7226
     sections has been modified, then those changes have already been
7227
     written to the BFD.  */
7228
538
  else if (abfd->direction == both_direction)
7229
0
    {
7230
0
      BFD_ASSERT (abfd->output_has_begun);
7231
0
      return true;
7232
0
    }
7233
7234
538
  i_shdrp = elf_elfsections (abfd);
7235
7236
538
  failed = false;
7237
538
  bfd_map_over_sections (abfd, bed->s->write_relocs, &failed);
7238
538
  if (failed)
7239
0
    return false;
7240
7241
538
  if (!_bfd_elf_assign_file_positions_for_non_load (abfd))
7242
0
    return false;
7243
7244
  /* After writing the headers, we need to write the sections too...  */
7245
538
  num_sec = elf_numsections (abfd);
7246
2.86k
  for (count = 1; count < num_sec; count++)
7247
2.32k
    {
7248
      /* Don't set the sh_name field without section header.  */
7249
2.32k
      if ((abfd->flags & BFD_NO_SECTION_HEADER) == 0)
7250
2.32k
  i_shdrp[count]->sh_name
7251
2.32k
    = _bfd_elf_strtab_offset (elf_shstrtab (abfd),
7252
2.32k
            i_shdrp[count]->sh_name);
7253
2.32k
      if (bed->elf_backend_section_processing)
7254
282
  if (!(*bed->elf_backend_section_processing) (abfd, i_shdrp[count]))
7255
0
    return false;
7256
2.32k
      if (i_shdrp[count]->contents)
7257
219
  {
7258
219
    bfd_size_type amt = i_shdrp[count]->sh_size;
7259
7260
219
    if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0
7261
219
        || bfd_write (i_shdrp[count]->contents, amt, abfd) != amt)
7262
0
      return false;
7263
219
  }
7264
2.32k
    }
7265
7266
  /* Write out the section header names.  */
7267
538
  t = elf_tdata (abfd);
7268
538
  if (elf_shstrtab (abfd) != NULL
7269
538
      && t->shstrtab_hdr.sh_offset != -1
7270
538
      && (bfd_seek (abfd, t->shstrtab_hdr.sh_offset, SEEK_SET) != 0
7271
521
    || !_bfd_elf_strtab_emit (abfd, elf_shstrtab (abfd))))
7272
17
    return false;
7273
7274
521
  if (!(*bed->elf_backend_final_write_processing) (abfd))
7275
0
    return false;
7276
7277
521
  if (!bed->s->write_shdrs_and_ehdr (abfd))
7278
0
    return false;
7279
7280
  /* This is last since write_shdrs_and_ehdr can touch i_shdrp[0].  */
7281
521
  if (t->o->build_id.after_write_object_contents != NULL
7282
0
      && !(*t->o->build_id.after_write_object_contents) (abfd))
7283
0
    return false;
7284
521
  if (t->o->package_metadata.after_write_object_contents != NULL
7285
0
      && !(*t->o->package_metadata.after_write_object_contents) (abfd))
7286
0
    return false;
7287
7288
521
  return true;
7289
521
}
7290
7291
bool
7292
_bfd_elf_write_corefile_contents (bfd *abfd)
7293
211
{
7294
  /* Hopefully this can be done just like an object file.  */
7295
211
  return _bfd_elf_write_object_contents (abfd);
7296
211
}
7297
7298
/* Given a section, search the header to find them.  */
7299
7300
unsigned int
7301
_bfd_elf_section_from_bfd_section (bfd *abfd, struct bfd_section *asect)
7302
1.14k
{
7303
1.14k
  elf_backend_data *bed;
7304
1.14k
  unsigned int sec_index;
7305
7306
1.14k
  if (elf_section_data (asect) != NULL
7307
528
      && elf_section_data (asect)->this_idx != 0)
7308
528
    return elf_section_data (asect)->this_idx;
7309
7310
615
  if (bfd_is_abs_section (asect))
7311
0
    sec_index = SHN_ABS;
7312
615
  else if (bfd_is_com_section (asect))
7313
6
    sec_index = SHN_COMMON;
7314
609
  else if (bfd_is_und_section (asect))
7315
608
    sec_index = SHN_UNDEF;
7316
1
  else
7317
1
    sec_index = SHN_BAD;
7318
7319
615
  bed = get_elf_backend_data (abfd);
7320
615
  if (bed->elf_backend_section_from_bfd_section)
7321
491
    {
7322
491
      int retval = sec_index;
7323
7324
491
      if ((*bed->elf_backend_section_from_bfd_section) (abfd, asect, &retval))
7325
7
  return retval;
7326
491
    }
7327
7328
608
  if (sec_index == SHN_BAD)
7329
0
    bfd_set_error (bfd_error_nonrepresentable_section);
7330
7331
608
  return sec_index;
7332
615
}
7333
7334
/* Given a BFD symbol, return the index in the ELF symbol table, or -1
7335
   on error.  */
7336
7337
int
7338
_bfd_elf_symbol_from_bfd_symbol (bfd *abfd, asymbol **asym_ptr_ptr)
7339
895
{
7340
895
  asymbol *asym_ptr = *asym_ptr_ptr;
7341
895
  int idx;
7342
895
  flagword flags = asym_ptr->flags;
7343
7344
  /* When gas creates relocations against local labels, it creates its
7345
     own symbol for the section, but does put the symbol into the
7346
     symbol chain, so udata is 0.  When the linker is generating
7347
     relocatable output, this section symbol may be for one of the
7348
     input sections rather than the output section.  */
7349
895
  if (asym_ptr->udata.i == 0
7350
1
      && (flags & BSF_SECTION_SYM)
7351
1
      && asym_ptr->section)
7352
1
    {
7353
1
      asection *sec;
7354
7355
1
      sec = asym_ptr->section;
7356
1
      if (sec->owner != abfd && sec->output_section != NULL)
7357
1
  sec = sec->output_section;
7358
1
      if (sec->owner == abfd
7359
1
    && sec->index < elf_num_section_syms (abfd)
7360
1
    && elf_section_syms (abfd)[sec->index] != NULL)
7361
1
  asym_ptr->udata.i = elf_section_syms (abfd)[sec->index]->udata.i;
7362
1
    }
7363
7364
895
  idx = asym_ptr->udata.i;
7365
7366
895
  if (idx == 0)
7367
0
    {
7368
      /* This case can occur when using --strip-symbol on a symbol
7369
   which is used in a relocation entry.  */
7370
0
      _bfd_error_handler
7371
  /* xgettext:c-format */
7372
0
  (_("%pB: symbol `%s' required but not present"),
7373
0
   abfd, bfd_asymbol_name (asym_ptr));
7374
0
      bfd_set_error (bfd_error_no_symbols);
7375
0
      return -1;
7376
0
    }
7377
7378
#if DEBUG & 4
7379
  {
7380
    fprintf (stderr,
7381
       "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d,"
7382
       " flags = 0x%.8x\n",
7383
       (long) asym_ptr, asym_ptr->name, idx, flags);
7384
    fflush (stderr);
7385
  }
7386
#endif
7387
7388
895
  return idx;
7389
895
}
7390
7391
static inline bfd_vma
7392
segment_size (Elf_Internal_Phdr *segment)
7393
4.64k
{
7394
4.64k
  return (segment->p_memsz > segment->p_filesz
7395
4.64k
    ? segment->p_memsz : segment->p_filesz);
7396
4.64k
}
7397
7398
7399
/* Returns the end address of the segment + 1.  */
7400
static inline bfd_vma
7401
segment_end (Elf_Internal_Phdr *segment, bfd_vma start)
7402
169
{
7403
169
  return start + segment_size (segment);
7404
169
}
7405
7406
static inline bfd_size_type
7407
section_size (asection *section, Elf_Internal_Phdr *segment)
7408
4.48k
{
7409
4.48k
  if ((section->flags & SEC_HAS_CONTENTS) != 0
7410
1.24k
      || (section->flags & SEC_THREAD_LOCAL) == 0
7411
12
      || segment->p_type == PT_TLS)
7412
4.47k
    return section->size;
7413
12
  return 0;
7414
4.48k
}
7415
7416
/* Returns TRUE if the given section is contained within the given
7417
   segment.  LMA addresses are compared against PADDR when
7418
   USE_VADDR is false, VMA against VADDR when true.  */
7419
static bool
7420
is_contained_by (asection *section, Elf_Internal_Phdr *segment,
7421
     bfd_vma paddr, bfd_vma vaddr, unsigned int opb,
7422
     bool use_vaddr)
7423
3.77k
{
7424
3.77k
  bfd_vma seg_addr = !use_vaddr ? paddr : vaddr;
7425
3.77k
  bfd_vma addr = !use_vaddr ? section->lma : section->vma;
7426
3.77k
  bfd_vma octet;
7427
3.77k
  if (_bfd_mul_overflow (addr, opb, &octet))
7428
0
    return false;
7429
  /* The third and fourth lines below are testing that the section end
7430
     address is within the segment.  It's written this way to avoid
7431
     overflow.  Add seg_addr + section_size to both sides of the
7432
     inequality to make it obvious.  */
7433
3.77k
  return (octet >= seg_addr
7434
2.54k
    && segment_size (segment) >= section_size (section, segment)
7435
1.92k
    && (octet - seg_addr
7436
1.92k
        <= segment_size (segment) - section_size (section, segment)));
7437
3.77k
}
7438
7439
/* Handle PT_NOTE segment.  */
7440
static bool
7441
is_note (asection *s, Elf_Internal_Phdr *p)
7442
2.90k
{
7443
2.90k
  return (p->p_type == PT_NOTE
7444
136
    && elf_section_type (s) == SHT_NOTE
7445
25
    && (ufile_ptr) s->filepos >= p->p_offset
7446
19
    && p->p_filesz >= s->size
7447
19
    && (ufile_ptr) s->filepos - p->p_offset <= p->p_filesz - s->size);
7448
2.90k
}
7449
7450
/* Rewrite program header information.  */
7451
7452
static bool
7453
rewrite_elf_program_header (bfd *ibfd, bfd *obfd, bfd_vma maxpagesize)
7454
29
{
7455
29
  Elf_Internal_Ehdr *iehdr;
7456
29
  struct elf_segment_map *map;
7457
29
  struct elf_segment_map *map_first;
7458
29
  struct elf_segment_map **pointer_to_map;
7459
29
  Elf_Internal_Phdr *segment;
7460
29
  asection *section;
7461
29
  unsigned int i;
7462
29
  unsigned int num_segments;
7463
29
  bool phdr_included = false;
7464
29
  bool p_paddr_valid;
7465
29
  struct elf_segment_map *phdr_adjust_seg = NULL;
7466
29
  unsigned int phdr_adjust_num = 0;
7467
29
  elf_backend_data *bed;
7468
29
  unsigned int opb = bfd_octets_per_byte (ibfd, NULL);
7469
7470
29
  bed = get_elf_backend_data (ibfd);
7471
29
  iehdr = elf_elfheader (ibfd);
7472
7473
29
  map_first = NULL;
7474
29
  pointer_to_map = &map_first;
7475
7476
29
  num_segments = elf_elfheader (ibfd)->e_phnum;
7477
7478
  /* The complicated case when p_vaddr is 0 is to handle the Solaris
7479
     linker, which generates a PT_INTERP section with p_vaddr and
7480
     p_memsz set to 0.  */
7481
29
#define IS_SOLARIS_PT_INTERP(p, s)          \
7482
329
  (p->p_vaddr == 0              \
7483
329
   && p->p_paddr == 0              \
7484
329
   && p->p_memsz == 0              \
7485
329
   && p->p_filesz > 0              \
7486
329
   && (s->flags & SEC_HAS_CONTENTS) != 0        \
7487
329
   && s->size > 0              \
7488
329
   && (bfd_vma) s->filepos >= p->p_offset        \
7489
329
   && ((bfd_vma) s->filepos + s->size         \
7490
4
       <= p->p_offset + p->p_filesz))
7491
7492
  /* Decide if the given section should be included in the given segment.
7493
     A section will be included if:
7494
       1. It is within the address space of the segment -- we use the LMA
7495
    if that is set for the segment and the VMA otherwise,
7496
       2. It is an allocated section or a NOTE section in a PT_NOTE
7497
    segment.
7498
       3. There is an output section associated with it,
7499
       4. The section has not already been allocated to a previous segment.
7500
       5. PT_GNU_STACK segments do not include any sections.
7501
       6. PT_TLS segment includes only SHF_TLS sections.
7502
       7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments.
7503
       8. PT_DYNAMIC should not contain empty sections at the beginning
7504
    (with the possible exception of .dynamic).  */
7505
29
#define IS_SECTION_IN_INPUT_SEGMENT(section, segment, opb, paddr_valid) \
7506
4.14k
  (((is_contained_by (section, segment, segment->p_paddr,    \
7507
3.48k
          segment->p_vaddr, opb, !paddr_valid)    \
7508
3.48k
     && (section->flags & SEC_ALLOC) != 0)       \
7509
3.48k
    || is_note (section, segment))         \
7510
3.48k
   && segment->p_type != PT_GNU_STACK          \
7511
3.48k
   && (segment->p_type != PT_TLS          \
7512
592
       || (section->flags & SEC_THREAD_LOCAL))        \
7513
3.48k
   && (segment->p_type == PT_LOAD          \
7514
588
       || segment->p_type == PT_TLS          \
7515
588
       || (section->flags & SEC_THREAD_LOCAL) == 0)      \
7516
3.48k
   && (segment->p_type != PT_DYNAMIC          \
7517
582
       || section_size (section, segment) > 0        \
7518
582
       || (segment->p_paddr            \
7519
0
     ? segment->p_paddr != section->lma * (opb)      \
7520
0
     : segment->p_vaddr != section->vma * (opb))      \
7521
582
       || (streq (bfd_section_name (section), ".dynamic")))    \
7522
4.14k
   && (segment->p_type != PT_LOAD || !section->segment_mark))
7523
7524
/* If the output section of a section in the input segment is NULL,
7525
   it is removed from the corresponding output segment.   */
7526
29
#define INCLUDE_SECTION_IN_SEGMENT(section, segment, opb, paddr_valid)  \
7527
665
  (IS_SECTION_IN_INPUT_SEGMENT (section, segment, opb, paddr_valid) \
7528
665
   && section->output_section != NULL)
7529
7530
  /* Returns TRUE iff seg1 starts after the end of seg2.  */
7531
29
#define SEGMENT_AFTER_SEGMENT(seg1, seg2, field)      \
7532
241
  (seg1->field >= segment_end (seg2, seg2->field))
7533
7534
  /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both
7535
     their VMA address ranges and their LMA address ranges overlap.
7536
     It is possible to have overlapping VMA ranges without overlapping LMA
7537
     ranges.  RedBoot images for example can have both .data and .bss mapped
7538
     to the same VMA range, but with the .data section mapped to a different
7539
     LMA.  */
7540
29
#define SEGMENT_OVERLAPS(seg1, seg2)          \
7541
71
  (   !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr)      \
7542
71
  || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr))     \
7543
71
   && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr)      \
7544
19
  || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr)))
7545
7546
  /* Initialise the segment mark field, and discard stupid alignment.  */
7547
302
  for (section = ibfd->sections; section != NULL; section = section->next)
7548
273
    {
7549
273
      asection *o = section->output_section;
7550
273
      if (o != NULL && o->alignment_power >= (sizeof (bfd_vma) * 8) - 1)
7551
0
  o->alignment_power = 0;
7552
273
      section->segment_mark = false;
7553
273
    }
7554
7555
  /* The Solaris linker creates program headers in which all the
7556
     p_paddr fields are zero.  When we try to objcopy or strip such a
7557
     file, we get confused.  Check for this case, and if we find it
7558
     don't set the p_paddr_valid fields.  */
7559
29
  p_paddr_valid = false;
7560
29
  for (i = 0, segment = elf_tdata (ibfd)->phdr;
7561
44
       i < num_segments;
7562
29
       i++, segment++)
7563
41
    if (segment->p_paddr != 0)
7564
26
      {
7565
26
  p_paddr_valid = true;
7566
26
  break;
7567
26
      }
7568
7569
  /* Scan through the segments specified in the program header
7570
     of the input BFD.  For this first scan we look for overlaps
7571
     in the loadable segments.  These can be created by weird
7572
     parameters to objcopy.  Also, fix some solaris weirdness.  */
7573
29
  for (i = 0, segment = elf_tdata (ibfd)->phdr;
7574
270
       i < num_segments;
7575
241
       i++, segment++)
7576
241
    {
7577
241
      unsigned int j;
7578
241
      Elf_Internal_Phdr *segment2;
7579
7580
241
      if (segment->p_type == PT_INTERP)
7581
343
  for (section = ibfd->sections; section; section = section->next)
7582
329
    if (IS_SOLARIS_PT_INTERP (segment, section))
7583
2
      {
7584
        /* Mininal change so that the normal section to segment
7585
     assignment code will work.  */
7586
2
        segment->p_vaddr = section->vma * opb;
7587
2
        break;
7588
2
      }
7589
7590
241
      if (segment->p_type != PT_LOAD)
7591
178
  {
7592
    /* Remove PT_GNU_RELRO segment.  */
7593
178
    if (segment->p_type == PT_GNU_RELRO)
7594
6
      segment->p_type = PT_NULL;
7595
178
    continue;
7596
178
  }
7597
7598
      /* Determine if this segment overlaps any previous segments.  */
7599
339
      for (j = 0, segment2 = elf_tdata (ibfd)->phdr; j < i; j++, segment2++)
7600
281
  {
7601
281
    bfd_signed_vma extra_length;
7602
7603
281
    if (segment2->p_type != PT_LOAD
7604
71
        || !SEGMENT_OVERLAPS (segment, segment2))
7605
272
      continue;
7606
7607
    /* Merge the two segments together.  */
7608
9
    if (segment2->p_vaddr < segment->p_vaddr)
7609
5
      {
7610
        /* Extend SEGMENT2 to include SEGMENT and then delete
7611
     SEGMENT.  */
7612
5
        extra_length = (segment_end (segment, segment->p_vaddr)
7613
5
            - segment_end (segment2, segment2->p_vaddr));
7614
7615
5
        if (extra_length > 0)
7616
2
    {
7617
2
      segment2->p_memsz += extra_length;
7618
2
      segment2->p_filesz += extra_length;
7619
2
    }
7620
7621
5
        segment->p_type = PT_NULL;
7622
7623
        /* Since we have deleted P we must restart the outer loop.  */
7624
5
        i = 0;
7625
5
        segment = elf_tdata (ibfd)->phdr;
7626
5
        break;
7627
5
      }
7628
4
    else
7629
4
      {
7630
        /* Extend SEGMENT to include SEGMENT2 and then delete
7631
     SEGMENT2.  */
7632
4
        extra_length = (segment_end (segment2, segment2->p_vaddr)
7633
4
            - segment_end (segment, segment->p_vaddr));
7634
7635
4
        if (extra_length > 0)
7636
2
    {
7637
2
      segment->p_memsz += extra_length;
7638
2
      segment->p_filesz += extra_length;
7639
2
    }
7640
7641
4
        segment2->p_type = PT_NULL;
7642
4
      }
7643
9
  }
7644
63
    }
7645
7646
  /* The second scan attempts to assign sections to segments.  */
7647
29
  for (i = 0, segment = elf_tdata (ibfd)->phdr;
7648
224
       i < num_segments;
7649
195
       i++, segment++)
7650
195
    {
7651
195
      unsigned int section_count;
7652
195
      asection **sections;
7653
195
      asection *output_section;
7654
195
      unsigned int isec;
7655
195
      asection *matching_lma;
7656
195
      asection *suggested_lma;
7657
195
      unsigned int j;
7658
195
      size_t amt;
7659
195
      asection *first_section;
7660
7661
195
      if (segment->p_type == PT_NULL)
7662
40
  continue;
7663
7664
155
      first_section = NULL;
7665
      /* Compute how many sections might be placed into this segment.  */
7666
155
      for (section = ibfd->sections, section_count = 0;
7667
2.97k
     section != NULL;
7668
2.81k
     section = section->next)
7669
2.81k
  {
7670
    /* Find the first section in the input segment, which may be
7671
       removed from the corresponding output segment.   */
7672
2.81k
    if (IS_SECTION_IN_INPUT_SEGMENT (section, segment, opb, p_paddr_valid))
7673
291
      {
7674
291
        if (first_section == NULL)
7675
86
    first_section = section;
7676
291
        if (section->output_section != NULL)
7677
290
    ++section_count;
7678
291
      }
7679
2.81k
  }
7680
7681
      /* Allocate a segment map big enough to contain
7682
   all of the sections we have selected.  */
7683
155
      amt = sizeof (struct elf_segment_map) - sizeof (asection *);
7684
155
      amt += section_count * sizeof (asection *);
7685
155
      map = bfd_zalloc (obfd, amt);
7686
155
      if (map == NULL)
7687
0
  return false;
7688
7689
      /* Initialise the fields of the segment map.  Default to
7690
   using the physical address of the segment in the input BFD.  */
7691
155
      map->next = NULL;
7692
155
      map->p_type = segment->p_type;
7693
155
      map->p_flags = segment->p_flags;
7694
155
      map->p_flags_valid = 1;
7695
7696
155
      if (map->p_type == PT_LOAD
7697
46
    && (ibfd->flags & D_PAGED) != 0
7698
2
    && maxpagesize > 1
7699
2
    && segment->p_align > 1)
7700
2
  {
7701
2
    map->p_align = segment->p_align;
7702
2
    if (segment->p_align > maxpagesize)
7703
0
      map->p_align = maxpagesize;
7704
2
    map->p_align_valid = 1;
7705
2
  }
7706
7707
      /* If the first section in the input segment is removed, there is
7708
   no need to preserve segment physical address in the corresponding
7709
   output segment.  */
7710
155
      if (!first_section || first_section->output_section != NULL)
7711
155
  {
7712
155
    map->p_paddr = segment->p_paddr;
7713
155
    map->p_paddr_valid = p_paddr_valid;
7714
155
  }
7715
7716
      /* Determine if this segment contains the ELF file header
7717
   and if it contains the program headers themselves.  */
7718
155
      map->includes_filehdr = (segment->p_offset == 0
7719
61
             && segment->p_filesz >= iehdr->e_ehsize);
7720
155
      map->includes_phdrs = 0;
7721
7722
155
      if (!phdr_included || segment->p_type != PT_LOAD)
7723
144
  {
7724
144
    map->includes_phdrs =
7725
144
      (segment->p_offset <= (bfd_vma) iehdr->e_phoff
7726
71
       && (segment->p_offset + segment->p_filesz
7727
71
     >= ((bfd_vma) iehdr->e_phoff
7728
71
         + iehdr->e_phnum * iehdr->e_phentsize)));
7729
7730
144
    if (segment->p_type == PT_LOAD && map->includes_phdrs)
7731
15
      phdr_included = true;
7732
144
  }
7733
7734
155
      if (section_count == 0)
7735
69
  {
7736
    /* Special segments, such as the PT_PHDR segment, may contain
7737
       no sections, but ordinary, loadable segments should contain
7738
       something.  They are allowed by the ELF spec however, so only
7739
       a warning is produced.
7740
       Don't warn if an empty PT_LOAD contains the program headers.
7741
       There is however the valid use case of embedded systems which
7742
       have segments with p_filesz of 0 and a p_memsz > 0 to initialize
7743
       flash memory with zeros.  No warning is shown for that case.  */
7744
69
    if (segment->p_type == PT_LOAD
7745
4
        && !map->includes_phdrs
7746
4
        && (segment->p_filesz > 0 || segment->p_memsz == 0))
7747
      /* xgettext:c-format */
7748
4
      _bfd_error_handler
7749
4
        (_("%pB: warning: empty loadable segment detected"
7750
4
     " at vaddr=%#" PRIx64 ", is this intentional?"),
7751
4
         ibfd, (uint64_t) segment->p_vaddr);
7752
7753
69
    map->p_vaddr_offset = segment->p_vaddr / opb;
7754
69
    map->count = 0;
7755
69
    *pointer_to_map = map;
7756
69
    pointer_to_map = &map->next;
7757
7758
69
    continue;
7759
69
  }
7760
7761
      /* Now scan the sections in the input BFD again and attempt
7762
   to add their corresponding output sections to the segment map.
7763
   The problem here is how to handle an output section which has
7764
   been moved (ie had its LMA changed).  There are four possibilities:
7765
7766
   1. None of the sections have been moved.
7767
      In this case we can continue to use the segment LMA from the
7768
      input BFD.
7769
7770
   2. All of the sections have been moved by the same amount.
7771
      In this case we can change the segment's LMA to match the LMA
7772
      of the first section.
7773
7774
   3. Some of the sections have been moved, others have not.
7775
      In this case those sections which have not been moved can be
7776
      placed in the current segment which will have to have its size,
7777
      and possibly its LMA changed, and a new segment or segments will
7778
      have to be created to contain the other sections.
7779
7780
   4. The sections have been moved, but not by the same amount.
7781
      In this case we can change the segment's LMA to match the LMA
7782
      of the first section and we will have to create a new segment
7783
      or segments to contain the other sections.
7784
7785
   In order to save time, we allocate an array to hold the section
7786
   pointers that we are interested in.  As these sections get assigned
7787
   to a segment, they are removed from this array.  */
7788
7789
86
      sections = bfd_malloc (section_count * sizeof (*sections));
7790
86
      if (sections == NULL)
7791
0
  return false;
7792
7793
      /* Step One: Scan for segment vs section LMA conflicts.
7794
   Also add the sections to the section array allocated above.
7795
   Also add the sections to the current segment.  In the common
7796
   case, where the sections have not been moved, this means that
7797
   we have completely filled the segment, and there is nothing
7798
   more to do.  */
7799
86
      isec = 0;
7800
86
      matching_lma = NULL;
7801
86
      suggested_lma = NULL;
7802
7803
86
      for (section = first_section, j = 0;
7804
665
     section != NULL;
7805
579
     section = section->next)
7806
665
  {
7807
665
    if (INCLUDE_SECTION_IN_SEGMENT (section, segment, opb, p_paddr_valid))
7808
290
      {
7809
290
        output_section = section->output_section;
7810
7811
290
        sections[j++] = section;
7812
7813
        /* The Solaris native linker always sets p_paddr to 0.
7814
     We try to catch that case here, and set it to the
7815
     correct value.  Note - some backends require that
7816
     p_paddr be left as zero.  */
7817
290
        if (!p_paddr_valid
7818
19
      && segment->p_vaddr != 0
7819
19
      && !bed->want_p_paddr_set_to_zero
7820
18
      && isec == 0
7821
5
      && output_section->lma != 0
7822
2
      && (align_power (segment->p_vaddr
7823
2
           + (map->includes_filehdr
7824
2
              ? iehdr->e_ehsize : 0)
7825
2
           + (map->includes_phdrs
7826
2
              ? iehdr->e_phnum * iehdr->e_phentsize
7827
2
              : 0),
7828
2
           output_section->alignment_power * opb)
7829
2
          == (output_section->vma * opb)))
7830
0
    map->p_paddr = segment->p_vaddr;
7831
7832
        /* Match up the physical address of the segment with the
7833
     LMA address of the output section.  */
7834
290
        if (is_contained_by (output_section, segment, map->p_paddr,
7835
290
           0, opb, false)
7836
6
      || is_note (section, segment))
7837
290
    {
7838
290
      if (matching_lma == NULL
7839
204
          || output_section->lma < matching_lma->lma)
7840
113
        matching_lma = output_section;
7841
7842
      /* We assume that if the section fits within the segment
7843
         then it does not overlap any other section within that
7844
         segment.  */
7845
290
      map->sections[isec++] = output_section;
7846
290
    }
7847
0
        else if (suggested_lma == NULL)
7848
0
    suggested_lma = output_section;
7849
7850
290
        if (j == section_count)
7851
86
    break;
7852
290
      }
7853
665
  }
7854
7855
86
      BFD_ASSERT (j == section_count);
7856
7857
      /* Step Two: Adjust the physical address of the current segment,
7858
   if necessary.  */
7859
86
      if (isec == section_count)
7860
86
  {
7861
    /* All of the sections fitted within the segment as currently
7862
       specified.  This is the default case.  Add the segment to
7863
       the list of built segments and carry on to process the next
7864
       program header in the input BFD.  */
7865
86
    map->count = section_count;
7866
86
    *pointer_to_map = map;
7867
86
    pointer_to_map = &map->next;
7868
7869
86
    if (p_paddr_valid
7870
80
        && !bed->want_p_paddr_set_to_zero)
7871
80
      {
7872
80
        bfd_vma hdr_size = 0;
7873
80
        if (map->includes_filehdr)
7874
14
    hdr_size = iehdr->e_ehsize;
7875
80
        if (map->includes_phdrs)
7876
18
    hdr_size += iehdr->e_phnum * iehdr->e_phentsize;
7877
7878
        /* Account for padding before the first section in the
7879
     segment.  */
7880
80
        map->p_vaddr_offset = ((map->p_paddr + hdr_size) / opb
7881
80
             - matching_lma->lma);
7882
80
      }
7883
7884
86
    free (sections);
7885
86
    continue;
7886
86
  }
7887
0
      else
7888
0
  {
7889
    /* Change the current segment's physical address to match
7890
       the LMA of the first section that fitted, or if no
7891
       section fitted, the first section.  */
7892
0
    if (matching_lma == NULL)
7893
0
      matching_lma = suggested_lma;
7894
7895
0
    map->p_paddr = matching_lma->lma * opb;
7896
7897
    /* Offset the segment physical address from the lma
7898
       to allow for space taken up by elf headers.  */
7899
0
    if (map->includes_phdrs)
7900
0
      {
7901
0
        map->p_paddr -= iehdr->e_phnum * iehdr->e_phentsize;
7902
7903
        /* iehdr->e_phnum is just an estimate of the number
7904
     of program headers that we will need.  Make a note
7905
     here of the number we used and the segment we chose
7906
     to hold these headers, so that we can adjust the
7907
     offset when we know the correct value.  */
7908
0
        phdr_adjust_num = iehdr->e_phnum;
7909
0
        phdr_adjust_seg = map;
7910
0
      }
7911
7912
0
    if (map->includes_filehdr)
7913
0
      {
7914
0
        bfd_vma align = (bfd_vma) 1 << matching_lma->alignment_power;
7915
0
        map->p_paddr -= iehdr->e_ehsize;
7916
        /* We've subtracted off the size of headers from the
7917
     first section lma, but there may have been some
7918
     alignment padding before that section too.  Try to
7919
     account for that by adjusting the segment lma down to
7920
     the same alignment.  */
7921
0
        if (segment->p_align != 0 && segment->p_align < align)
7922
0
    align = segment->p_align;
7923
0
        map->p_paddr &= -(align * opb);
7924
0
      }
7925
0
  }
7926
7927
      /* Step Three: Loop over the sections again, this time assigning
7928
   those that fit to the current segment and removing them from the
7929
   sections array; but making sure not to leave large gaps.  Once all
7930
   possible sections have been assigned to the current segment it is
7931
   added to the list of built segments and if sections still remain
7932
   to be assigned, a new segment is constructed before repeating
7933
   the loop.  */
7934
0
      isec = 0;
7935
0
      do
7936
0
  {
7937
0
    map->count = 0;
7938
0
    suggested_lma = NULL;
7939
7940
    /* Fill the current segment with sections that fit.  */
7941
0
    for (j = 0; j < section_count; j++)
7942
0
      {
7943
0
        section = sections[j];
7944
7945
0
        if (section == NULL)
7946
0
    continue;
7947
7948
0
        output_section = section->output_section;
7949
7950
0
        BFD_ASSERT (output_section != NULL);
7951
7952
0
        if (is_contained_by (output_section, segment, map->p_paddr,
7953
0
           0, opb, false)
7954
0
      || is_note (section, segment))
7955
0
    {
7956
0
      if (map->count == 0)
7957
0
        {
7958
          /* If the first section in a segment does not start at
7959
       the beginning of the segment, then something is
7960
       wrong.  */
7961
0
          if (align_power (map->p_paddr
7962
0
               + (map->includes_filehdr
7963
0
            ? iehdr->e_ehsize : 0)
7964
0
               + (map->includes_phdrs
7965
0
            ? iehdr->e_phnum * iehdr->e_phentsize
7966
0
            : 0),
7967
0
               output_section->alignment_power * opb)
7968
0
        != output_section->lma * opb)
7969
0
      goto sorry;
7970
0
        }
7971
0
      else
7972
0
        {
7973
0
          asection *prev_sec;
7974
7975
0
          prev_sec = map->sections[map->count - 1];
7976
7977
          /* If the gap between the end of the previous section
7978
       and the start of this section is more than
7979
       maxpagesize then we need to start a new segment.  */
7980
0
          if ((BFD_ALIGN (prev_sec->lma + prev_sec->size,
7981
0
              maxpagesize)
7982
0
         < BFD_ALIGN (output_section->lma, maxpagesize))
7983
0
        || (prev_sec->lma + prev_sec->size
7984
0
            > output_section->lma))
7985
0
      {
7986
0
        if (suggested_lma == NULL)
7987
0
          suggested_lma = output_section;
7988
7989
0
        continue;
7990
0
      }
7991
0
        }
7992
7993
0
      map->sections[map->count++] = output_section;
7994
0
      ++isec;
7995
0
      sections[j] = NULL;
7996
0
      if (segment->p_type == PT_LOAD)
7997
0
        section->segment_mark = true;
7998
0
    }
7999
0
        else if (suggested_lma == NULL)
8000
0
    suggested_lma = output_section;
8001
0
      }
8002
8003
    /* PR 23932.  A corrupt input file may contain sections that cannot
8004
       be assigned to any segment - because for example they have a
8005
       negative size - or segments that do not contain any sections.
8006
       But there are also valid reasons why a segment can be empty.
8007
       So allow a count of zero.  */
8008
8009
    /* Add the current segment to the list of built segments.  */
8010
0
    *pointer_to_map = map;
8011
0
    pointer_to_map = &map->next;
8012
8013
0
    if (isec < section_count)
8014
0
      {
8015
        /* We still have not allocated all of the sections to
8016
     segments.  Create a new segment here, initialise it
8017
     and carry on looping.  */
8018
0
        amt = sizeof (struct elf_segment_map) - sizeof (asection *);
8019
0
        amt += section_count * sizeof (asection *);
8020
0
        map = bfd_zalloc (obfd, amt);
8021
0
        if (map == NULL)
8022
0
    {
8023
0
      free (sections);
8024
0
      return false;
8025
0
    }
8026
8027
        /* Initialise the fields of the segment map.  Set the physical
8028
     physical address to the LMA of the first section that has
8029
     not yet been assigned.  */
8030
0
        map->next = NULL;
8031
0
        map->p_type = segment->p_type;
8032
0
        map->p_flags = segment->p_flags;
8033
0
        map->p_flags_valid = 1;
8034
0
        map->p_paddr = suggested_lma->lma * opb;
8035
0
        map->p_paddr_valid = p_paddr_valid;
8036
0
        map->includes_filehdr = 0;
8037
0
        map->includes_phdrs = 0;
8038
0
      }
8039
8040
0
    continue;
8041
0
  sorry:
8042
0
    bfd_set_error (bfd_error_sorry);
8043
0
    free (sections);
8044
0
    return false;
8045
0
  }
8046
0
      while (isec < section_count);
8047
8048
0
      free (sections);
8049
0
    }
8050
8051
29
  elf_seg_map (obfd) = map_first;
8052
8053
  /* If we had to estimate the number of program headers that were
8054
     going to be needed, then check our estimate now and adjust
8055
     the offset if necessary.  */
8056
29
  if (phdr_adjust_seg != NULL)
8057
0
    {
8058
0
      unsigned int count;
8059
8060
0
      for (count = 0, map = map_first; map != NULL; map = map->next)
8061
0
  count++;
8062
8063
0
      if (count > phdr_adjust_num)
8064
0
  phdr_adjust_seg->p_paddr
8065
0
    -= (count - phdr_adjust_num) * iehdr->e_phentsize;
8066
8067
0
      for (map = map_first; map != NULL; map = map->next)
8068
0
  if (map->p_type == PT_PHDR)
8069
0
    {
8070
0
      bfd_vma adjust
8071
0
        = phdr_adjust_seg->includes_filehdr ? iehdr->e_ehsize : 0;
8072
0
      map->p_paddr = phdr_adjust_seg->p_paddr + adjust;
8073
0
      break;
8074
0
    }
8075
0
    }
8076
8077
29
#undef IS_SOLARIS_PT_INTERP
8078
29
#undef IS_SECTION_IN_INPUT_SEGMENT
8079
29
#undef INCLUDE_SECTION_IN_SEGMENT
8080
29
#undef SEGMENT_AFTER_SEGMENT
8081
29
#undef SEGMENT_OVERLAPS
8082
29
  return true;
8083
29
}
8084
8085
/* Return true if p_align in the ELF program header in ABFD is valid.  */
8086
8087
static bool
8088
elf_is_p_align_valid (bfd *abfd)
8089
128
{
8090
128
  unsigned int i;
8091
128
  Elf_Internal_Phdr *segment;
8092
128
  unsigned int num_segments;
8093
128
  elf_backend_data *bed = get_elf_backend_data (abfd);
8094
128
  bfd_size_type maxpagesize = bed->maxpagesize;
8095
128
  bfd_size_type p_align = bed->p_align;
8096
8097
  /* Return true if the default p_align value isn't set or the maximum
8098
     page size is the same as the minimum page size.  */
8099
128
  if (p_align == 0 || maxpagesize == bed->minpagesize)
8100
128
    return true;
8101
8102
  /* When the default p_align value is set, p_align may be set to the
8103
     default p_align value while segments are aligned to the maximum
8104
     page size.  In this case, the input p_align will be ignored and
8105
     the maximum page size will be used to align the output segments.  */
8106
0
  segment = elf_tdata (abfd)->phdr;
8107
0
  num_segments = elf_elfheader (abfd)->e_phnum;
8108
0
  for (i = 0; i < num_segments; i++, segment++)
8109
0
    if (segment->p_type == PT_LOAD
8110
0
  && (segment->p_align != p_align
8111
0
      || vma_page_aligned_bias (segment->p_vaddr,
8112
0
              segment->p_offset,
8113
0
              maxpagesize) != 0))
8114
0
      return true;
8115
8116
0
  return false;
8117
0
}
8118
8119
/* Copy ELF program header information.  */
8120
8121
static bool
8122
copy_elf_program_header (bfd *ibfd, bfd *obfd)
8123
128
{
8124
128
  Elf_Internal_Ehdr *iehdr;
8125
128
  struct elf_segment_map *map;
8126
128
  struct elf_segment_map *map_first;
8127
128
  struct elf_segment_map **pointer_to_map;
8128
128
  Elf_Internal_Phdr *segment;
8129
128
  unsigned int i;
8130
128
  unsigned int num_segments;
8131
128
  bool phdr_included = false;
8132
128
  bool p_paddr_valid;
8133
128
  bool p_palign_valid;
8134
128
  unsigned int opb = bfd_octets_per_byte (ibfd, NULL);
8135
8136
128
  iehdr = elf_elfheader (ibfd);
8137
8138
128
  map_first = NULL;
8139
128
  pointer_to_map = &map_first;
8140
8141
  /* If all the segment p_paddr fields are zero, don't set
8142
     map->p_paddr_valid.  */
8143
128
  p_paddr_valid = false;
8144
128
  num_segments = elf_elfheader (ibfd)->e_phnum;
8145
128
  for (i = 0, segment = elf_tdata (ibfd)->phdr;
8146
197
       i < num_segments;
8147
128
       i++, segment++)
8148
177
    if (segment->p_paddr != 0)
8149
108
      {
8150
108
  p_paddr_valid = true;
8151
108
  break;
8152
108
      }
8153
8154
128
  p_palign_valid = elf_is_p_align_valid (ibfd);
8155
8156
128
  for (i = 0, segment = elf_tdata (ibfd)->phdr;
8157
837
       i < num_segments;
8158
709
       i++, segment++)
8159
709
    {
8160
709
      asection *section;
8161
709
      unsigned int section_count;
8162
709
      size_t amt;
8163
709
      Elf_Internal_Shdr *this_hdr;
8164
709
      asection *first_section = NULL;
8165
709
      asection *lowest_section;
8166
8167
      /* Compute how many sections are in this segment.  */
8168
709
      for (section = ibfd->sections, section_count = 0;
8169
7.18k
     section != NULL;
8170
6.47k
     section = section->next)
8171
6.47k
  {
8172
6.47k
    this_hdr = &(elf_section_data(section)->this_hdr);
8173
6.47k
    if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
8174
2.65k
      {
8175
2.65k
        if (first_section == NULL)
8176
447
    first_section = section;
8177
2.65k
        section_count++;
8178
2.65k
      }
8179
6.47k
  }
8180
8181
      /* Allocate a segment map big enough to contain
8182
   all of the sections we have selected.  */
8183
709
      amt = sizeof (struct elf_segment_map) - sizeof (asection *);
8184
709
      amt += section_count * sizeof (asection *);
8185
709
      map = bfd_zalloc (obfd, amt);
8186
709
      if (map == NULL)
8187
0
  return false;
8188
8189
      /* Initialize the fields of the output segment map with the
8190
   input segment.  */
8191
709
      map->next = NULL;
8192
709
      map->p_type = segment->p_type;
8193
709
      map->p_flags = segment->p_flags;
8194
709
      map->p_flags_valid = 1;
8195
709
      map->p_paddr = segment->p_paddr;
8196
709
      map->p_paddr_valid = p_paddr_valid;
8197
709
      map->p_align = segment->p_align;
8198
      /* Keep p_align of PT_GNU_STACK for stack alignment.  */
8199
709
      map->p_align_valid = (map->p_type == PT_GNU_STACK
8200
706
          || p_palign_valid);
8201
709
      map->p_vaddr_offset = 0;
8202
8203
709
      if (map->p_type == PT_GNU_RELRO
8204
697
    || map->p_type == PT_GNU_STACK)
8205
15
  {
8206
    /* The PT_GNU_RELRO segment may contain the first a few
8207
       bytes in the .got.plt section even if the whole .got.plt
8208
       section isn't in the PT_GNU_RELRO segment.  We won't
8209
       change the size of the PT_GNU_RELRO segment.
8210
       Similarly, PT_GNU_STACK size is significant on uclinux
8211
       systems.    */
8212
15
    map->p_size = segment->p_memsz;
8213
15
    map->p_size_valid = 1;
8214
15
  }
8215
8216
      /* Determine if this segment contains the ELF file header
8217
   and if it contains the program headers themselves.  */
8218
709
      map->includes_filehdr = (segment->p_offset == 0
8219
421
             && segment->p_filesz >= iehdr->e_ehsize);
8220
8221
709
      map->includes_phdrs = 0;
8222
709
      if (! phdr_included || segment->p_type != PT_LOAD)
8223
698
  {
8224
698
    map->includes_phdrs =
8225
698
      (segment->p_offset <= (bfd_vma) iehdr->e_phoff
8226
453
       && (segment->p_offset + segment->p_filesz
8227
453
     >= ((bfd_vma) iehdr->e_phoff
8228
453
         + iehdr->e_phnum * iehdr->e_phentsize)));
8229
8230
698
    if (segment->p_type == PT_LOAD && map->includes_phdrs)
8231
14
      phdr_included = true;
8232
698
  }
8233
8234
709
      lowest_section = NULL;
8235
709
      if (section_count != 0)
8236
447
  {
8237
447
    unsigned int isec = 0;
8238
8239
447
    for (section = first_section;
8240
2.72k
         section != NULL;
8241
2.27k
         section = section->next)
8242
2.72k
      {
8243
2.72k
        this_hdr = &(elf_section_data(section)->this_hdr);
8244
2.72k
        if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
8245
2.65k
    {
8246
2.65k
      map->sections[isec++] = section->output_section;
8247
2.65k
      if ((section->flags & SEC_ALLOC) != 0)
8248
321
        {
8249
321
          bfd_vma seg_off;
8250
8251
321
          if (lowest_section == NULL
8252
204
        || section->lma < lowest_section->lma)
8253
118
      lowest_section = section;
8254
8255
          /* Section lmas are set up from PT_LOAD header
8256
       p_paddr in _bfd_elf_make_section_from_shdr.
8257
       If this header has a p_paddr that disagrees
8258
       with the section lma, flag the p_paddr as
8259
       invalid.  */
8260
321
          if ((section->flags & SEC_LOAD) != 0)
8261
266
      seg_off = this_hdr->sh_offset - segment->p_offset;
8262
55
          else
8263
55
      seg_off = this_hdr->sh_addr - segment->p_vaddr;
8264
321
          if (section->lma * opb - segment->p_paddr != seg_off)
8265
85
      map->p_paddr_valid = false;
8266
321
        }
8267
2.65k
      if (isec == section_count)
8268
447
        break;
8269
2.65k
    }
8270
2.72k
      }
8271
447
  }
8272
8273
709
      if (section_count == 0)
8274
262
  map->p_vaddr_offset = segment->p_vaddr / opb;
8275
447
      else if (map->p_paddr_valid)
8276
389
  {
8277
    /* Account for padding before the first section in the segment.  */
8278
389
    bfd_vma hdr_size = 0;
8279
389
    if (map->includes_filehdr)
8280
28
      hdr_size = iehdr->e_ehsize;
8281
389
    if (map->includes_phdrs)
8282
10
      hdr_size += iehdr->e_phnum * iehdr->e_phentsize;
8283
8284
389
    map->p_vaddr_offset = ((map->p_paddr + hdr_size) / opb
8285
389
         - (lowest_section ? lowest_section->lma : 0));
8286
389
  }
8287
8288
709
      map->count = section_count;
8289
709
      *pointer_to_map = map;
8290
709
      pointer_to_map = &map->next;
8291
709
    }
8292
8293
128
  elf_seg_map (obfd) = map_first;
8294
128
  return true;
8295
128
}
8296
8297
/* Copy private BFD data.  This copies or rewrites ELF program header
8298
   information.  */
8299
8300
static bool
8301
copy_private_bfd_data (bfd *ibfd, bfd *obfd)
8302
157
{
8303
157
  bfd_vma maxpagesize;
8304
8305
157
  if (elf_tdata (ibfd)->phdr == NULL)
8306
0
    return true;
8307
8308
157
  if (ibfd->xvec == obfd->xvec)
8309
152
    {
8310
      /* Check to see if any sections in the input BFD
8311
   covered by ELF program header have changed.  */
8312
152
      Elf_Internal_Phdr *segment;
8313
152
      asection * section;
8314
152
      asection * osec;
8315
152
      asection * prev;
8316
152
      unsigned int i, num_segments;
8317
152
      Elf_Internal_Shdr *this_hdr;
8318
152
      elf_backend_data *bed;
8319
8320
152
      bed = get_elf_backend_data (ibfd);
8321
8322
      /* Regenerate the segment map if p_paddr is set to 0.  */
8323
152
      if (bed->want_p_paddr_set_to_zero)
8324
2
  goto rewrite;
8325
8326
      /* Initialize the segment mark field.  */
8327
1.05k
      for (section = obfd->sections; section != NULL;
8328
907
     section = section->next)
8329
907
  section->segment_mark = false;
8330
8331
150
      num_segments = elf_elfheader (ibfd)->e_phnum;
8332
150
      for (i = 0, segment = elf_tdata (ibfd)->phdr;
8333
892
     i < num_segments;
8334
742
     i++, segment++)
8335
764
  {
8336
    /* PR binutils/3535.  The Solaris linker always sets the p_paddr
8337
       and p_memsz fields of special segments (DYNAMIC, INTERP) to 0
8338
       which severly confuses things, so always regenerate the segment
8339
       map in this case.  */
8340
764
    if (segment->p_paddr == 0
8341
447
        && segment->p_memsz == 0
8342
320
        && (segment->p_type == PT_INTERP
8343
320
      || segment->p_type == PT_DYNAMIC))
8344
0
      goto rewrite;
8345
8346
764
    for (section = ibfd->sections, prev = NULL;
8347
7.73k
         section != NULL; section = section->next)
8348
6.99k
      {
8349
        /* We mark the output section so that we know it comes
8350
     from the input BFD.  */
8351
6.99k
        osec = section->output_section;
8352
6.99k
        if (osec)
8353
6.99k
    osec->segment_mark = true;
8354
8355
        /* Check if this section is covered by the segment.  */
8356
6.99k
        this_hdr = &(elf_section_data(section)->this_hdr);
8357
6.99k
        if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
8358
2.73k
    {
8359
      /* FIXME: Check if its output section is changed or
8360
         removed.  What else do we need to check?  */
8361
2.73k
      if (osec == NULL
8362
2.73k
          || section->flags != osec->flags
8363
2.73k
          || section->lma != osec->lma
8364
2.73k
          || section->vma != osec->vma
8365
2.73k
          || section->size != osec->size
8366
2.73k
          || section->rawsize != osec->rawsize
8367
2.73k
          || section->alignment_power != osec->alignment_power)
8368
3
        goto rewrite;
8369
8370
      /* PR 31450: If this is an allocated section then make sure
8371
         that this section's vma to lma relationship is the same
8372
         as previous (allocated) section's.  */
8373
2.73k
      if (prev != NULL
8374
847
          && section->flags & SEC_ALLOC
8375
235
          && section->lma - section->vma != prev->lma - prev->vma)
8376
19
        goto rewrite;
8377
8378
2.71k
      if (section->flags & SEC_ALLOC)
8379
357
        prev = section;
8380
2.71k
    }
8381
6.99k
      }
8382
764
  }
8383
8384
      /* Check to see if any output section do not come from the
8385
   input BFD.  */
8386
772
      for (section = obfd->sections; section != NULL;
8387
644
     section = section->next)
8388
644
  {
8389
644
    if (!section->segment_mark)
8390
0
      goto rewrite;
8391
644
    else
8392
644
      section->segment_mark = false;
8393
644
  }
8394
8395
128
      return copy_elf_program_header (ibfd, obfd);
8396
128
    }
8397
8398
29
 rewrite:
8399
29
  maxpagesize = 0;
8400
29
  if (ibfd->xvec == obfd->xvec)
8401
24
    {
8402
      /* When rewriting program header, set the output maxpagesize to
8403
   the maximum alignment of input PT_LOAD segments.  */
8404
24
      Elf_Internal_Phdr *segment;
8405
24
      unsigned int i;
8406
24
      unsigned int num_segments = elf_elfheader (ibfd)->e_phnum;
8407
8408
24
      for (i = 0, segment = elf_tdata (ibfd)->phdr;
8409
214
     i < num_segments;
8410
190
     i++, segment++)
8411
190
  if (segment->p_type == PT_LOAD
8412
54
      && maxpagesize < segment->p_align)
8413
26
    {
8414
      /* PR 17512: file: f17299af.  */
8415
26
      if (segment->p_align > (bfd_vma) 1 << ((sizeof (bfd_vma) * 8) - 2))
8416
        /* xgettext:c-format */
8417
0
        _bfd_error_handler (_("%pB: warning: segment alignment of %#"
8418
0
            PRIx64 " is too large"),
8419
0
          ibfd, (uint64_t) segment->p_align);
8420
26
      else
8421
26
        maxpagesize = segment->p_align;
8422
26
    }
8423
24
    }
8424
29
  if (maxpagesize == 0)
8425
9
    maxpagesize = get_elf_backend_data (obfd)->maxpagesize;
8426
8427
29
  return rewrite_elf_program_header (ibfd, obfd, maxpagesize);
8428
157
}
8429
8430
/* Copy private section information from input to output.  This function
8431
   is called both by objcopy where LINK_INFO is NULL, and ld where
8432
   LINK_INFO is non-NULL in the usual case but NULL for a special case
8433
   when dealing with LTO IR or cloning output sections.  */
8434
8435
bool
8436
_bfd_elf_copy_private_section_data (bfd *ibfd,
8437
            asection *isec,
8438
            bfd *obfd ATTRIBUTE_UNUSED,
8439
            asection *osec,
8440
            struct bfd_link_info *link_info)
8441
2.24k
{
8442
2.24k
  if (ibfd->xvec->flavour != bfd_target_elf_flavour)
8443
0
    return true;
8444
8445
2.24k
  Elf_Internal_Shdr *ihdr = &elf_section_data (isec)->this_hdr;
8446
2.24k
  Elf_Internal_Shdr *ohdr = &elf_section_data (osec)->this_hdr;
8447
2.24k
  if (link_info == NULL)
8448
2.24k
    {
8449
2.24k
      ohdr->sh_entsize = ihdr->sh_entsize;
8450
8451
2.24k
      if (ihdr->sh_type == SHT_SYMTAB
8452
2.23k
    || ihdr->sh_type == SHT_DYNSYM
8453
2.21k
    || ihdr->sh_type == SHT_GNU_verneed
8454
2.18k
    || ihdr->sh_type == SHT_GNU_verdef)
8455
81
  ohdr->sh_info = ihdr->sh_info;
8456
2.24k
    }
8457
8458
  /* If this is a known ABI section, ELF section type and flags may
8459
     have been set up when OSEC was created.  For normal sections we
8460
     allow the user to override the type and flags other than
8461
     SHF_MASKOS and SHF_MASKPROC.  */
8462
2.24k
  if (elf_section_type (osec) == SHT_PROGBITS
8463
1.74k
      || elf_section_type (osec) == SHT_NOTE
8464
1.67k
      || elf_section_type (osec) == SHT_NOBITS)
8465
597
    elf_section_type (osec) = SHT_NULL;
8466
8467
  /* For objcopy and relocatable link, copy the ELF section type from
8468
     the input file if the BFD section flags are the same.  (If they
8469
     are different the user may be doing something like
8470
     "objcopy --set-section-flags .text=alloc,data".)  For a final
8471
     link allow some flags that the linker clears to differ.  */
8472
2.24k
  bool final_link = (link_info != NULL
8473
0
         && !bfd_link_relocatable (link_info));
8474
2.24k
  if (elf_section_type (osec) == SHT_NULL
8475
2.00k
      && (osec->flags == isec->flags
8476
0
    || (final_link
8477
0
        && ((osec->flags ^ isec->flags)
8478
0
      & ~(SEC_LINK_ONCE | SEC_LINK_DUPLICATES | SEC_RELOC)) == 0)))
8479
2.00k
    elf_section_type (osec) = elf_section_type (isec);
8480
8481
2.24k
  elf_section_flags (osec) = elf_section_flags (isec);
8482
  /* Like for type, retain flags for objcopy (yet unlike for type, don't do so
8483
     for relocatable link).  Same heuristic as there: If the BFD section flags
8484
     are different, assume --set-section-flags is in use for the section.
8485
8486
     FIXME: Is this correct for all OS/PROC specific flags?  */
8487
2.24k
  if (link_info != NULL || osec->flags != isec->flags)
8488
0
    elf_section_flags (osec) &= (SHF_MASKOS | SHF_MASKPROC);
8489
2.24k
  else
8490
2.24k
    {
8491
      /* Clear only flags which are set below or elsewhere.  */
8492
2.24k
      elf_section_flags (osec) &= ~(SHF_WRITE | SHF_ALLOC | SHF_EXECINSTR
8493
2.24k
            | SHF_MERGE | SHF_STRINGS | SHF_LINK_ORDER
8494
2.24k
            | SHF_INFO_LINK | SHF_GROUP | SHF_TLS
8495
2.24k
            | SHF_COMPRESSED);
8496
2.24k
      if (elf_section_flags (osec) & ~(SHF_MASKOS | SHF_MASKPROC))
8497
122
  _bfd_error_handler
8498
122
    (_("%pB:%pA: warning: retaining unknown section flag(s) %#" PRIx64),
8499
122
     ibfd, isec,
8500
122
     (uint64_t) (elf_section_flags (osec)
8501
122
           & ~(SHF_MASKOS | SHF_MASKPROC)));
8502
2.24k
    }
8503
8504
  /* Copy sh_info from input for mbind section.  */
8505
2.24k
  if ((elf_tdata (ibfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0
8506
248
      && elf_section_flags (isec) & SHF_GNU_MBIND)
8507
33
    elf_section_data (osec)->this_hdr.sh_info
8508
33
      = elf_section_data (isec)->this_hdr.sh_info;
8509
8510
  /* Set things up for objcopy and relocatable link.  The output
8511
     SHT_GROUP section will have its elf_next_in_group pointing back
8512
     to the input group members.  Ignore linker created group section.
8513
     See elfNN_ia64_object_p in elfxx-ia64.c.  */
8514
2.24k
  if ((link_info == NULL
8515
0
       || !link_info->resolve_section_groups)
8516
2.24k
      && (elf_sec_group (isec) == NULL
8517
40
    || (elf_sec_group (isec)->flags & SEC_LINKER_CREATED) == 0))
8518
2.24k
    {
8519
2.24k
      if (elf_section_flags (isec) & SHF_GROUP)
8520
97
  elf_section_flags (osec) |= SHF_GROUP;
8521
2.24k
      elf_next_in_group (osec) = elf_next_in_group (isec);
8522
2.24k
      elf_section_data (osec)->group = elf_section_data (isec)->group;
8523
2.24k
    }
8524
8525
  /* If not decompress, preserve SHF_COMPRESSED.  */
8526
2.24k
  if (!final_link && (ibfd->flags & BFD_DECOMPRESS) == 0)
8527
2.24k
    elf_section_flags (osec) |= (elf_section_flags (isec)
8528
2.24k
         & SHF_COMPRESSED);
8529
8530
  /* We need to handle elf_linked_to_section for SHF_LINK_ORDER. We
8531
     don't use the output section of the linked-to section since it
8532
     may be NULL at this point.  */
8533
2.24k
  if ((ihdr->sh_flags & SHF_LINK_ORDER) != 0)
8534
38
    {
8535
38
      ohdr->sh_flags |= SHF_LINK_ORDER;
8536
38
      elf_linked_to_section (osec) = elf_linked_to_section (isec);
8537
38
    }
8538
8539
2.24k
  osec->use_rela_p = isec->use_rela_p;
8540
8541
2.24k
  return true;
8542
2.24k
}
8543
8544
/* Look at all the SHT_GROUP sections in IBFD, making any adjustments
8545
   necessary if we are removing either the SHT_GROUP section or any of
8546
   the group member sections.  DISCARDED is the value that a section's
8547
   output_section has if the section will be discarded, NULL when this
8548
   function is called from objcopy, bfd_abs_section_ptr when called
8549
   from the linker.  */
8550
8551
bool
8552
_bfd_elf_fixup_group_sections (bfd *ibfd, asection *discarded)
8553
338
{
8554
338
  asection *isec;
8555
8556
2.52k
  for (isec = ibfd->sections; isec != NULL; isec = isec->next)
8557
2.19k
    if (elf_section_type (isec) == SHT_GROUP)
8558
19
      {
8559
19
  asection *first = elf_next_in_group (isec);
8560
19
  asection *s = first;
8561
19
  bfd_size_type removed = 0;
8562
8563
41
  while (s != NULL)
8564
40
    {
8565
      /* If this member section is being output but the
8566
         SHT_GROUP section is not, then clear the group info
8567
         set up by _bfd_elf_copy_private_section_data.  */
8568
40
      if (s->output_section != discarded
8569
40
    && isec->output_section == discarded)
8570
0
        {
8571
0
    elf_section_flags (s->output_section) &= ~SHF_GROUP;
8572
0
    elf_group_name (s->output_section) = NULL;
8573
0
        }
8574
40
      else
8575
40
        {
8576
40
    struct bfd_elf_section_data *elf_sec = elf_section_data (s);
8577
40
    if (s->output_section == discarded
8578
0
        && isec->output_section != discarded)
8579
0
      {
8580
        /* Conversely, if the member section is not being
8581
           output but the SHT_GROUP section is, then adjust
8582
           its size.  */
8583
0
        removed += 4;
8584
0
        if (elf_sec->rel.hdr != NULL
8585
0
      && (elf_sec->rel.hdr->sh_flags & SHF_GROUP) != 0)
8586
0
          removed += 4;
8587
0
        if (elf_sec->rela.hdr != NULL
8588
0
      && (elf_sec->rela.hdr->sh_flags & SHF_GROUP) != 0)
8589
0
          removed += 4;
8590
0
      }
8591
40
    else
8592
40
      {
8593
        /* Also adjust for zero-sized relocation member
8594
           section.  */
8595
40
        if (elf_sec->rel.hdr != NULL
8596
0
      && elf_sec->rel.hdr->sh_size == 0)
8597
0
          removed += 4;
8598
40
        if (elf_sec->rela.hdr != NULL
8599
29
      && elf_sec->rela.hdr->sh_size == 0)
8600
0
          removed += 4;
8601
40
      }
8602
40
        }
8603
40
      s = elf_next_in_group (s);
8604
40
      if (s == first)
8605
18
        break;
8606
40
    }
8607
19
  if (removed != 0)
8608
0
    {
8609
0
      if (discarded != NULL)
8610
0
        {
8611
    /* If we've been called for ld -r, then we need to
8612
       adjust the input section size.  */
8613
0
    if (isec->rawsize == 0)
8614
0
      isec->rawsize = isec->size;
8615
0
    isec->size = isec->rawsize - removed;
8616
0
    if (isec->size <= 4)
8617
0
      {
8618
0
        isec->size = 0;
8619
0
        isec->flags |= SEC_EXCLUDE;
8620
0
      }
8621
0
        }
8622
0
      else if (isec->output_section != NULL)
8623
0
        {
8624
    /* Adjust the output section size when called from
8625
       objcopy. */
8626
0
    isec->output_section->size -= removed;
8627
0
    if (isec->output_section->size <= 4)
8628
0
      {
8629
0
        isec->output_section->size = 0;
8630
0
        isec->output_section->flags |= SEC_EXCLUDE;
8631
0
      }
8632
0
        }
8633
0
    }
8634
19
      }
8635
8636
338
  return true;
8637
338
}
8638
8639
/* Copy private header information.  */
8640
8641
bool
8642
_bfd_elf_copy_private_header_data (bfd *ibfd, bfd *obfd)
8643
338
{
8644
338
  if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
8645
0
    return true;
8646
8647
  /* Copy over private BFD data if it has not already been copied.
8648
     This must be done here, rather than in the copy_private_bfd_data
8649
     entry point, because the latter is called after the section
8650
     contents have been set, which means that the program headers have
8651
     already been worked out.  */
8652
338
  if (elf_seg_map (obfd) == NULL && elf_tdata (ibfd)->phdr != NULL)
8653
157
    {
8654
157
      if (! copy_private_bfd_data (ibfd, obfd))
8655
0
  return false;
8656
157
    }
8657
8658
338
  return _bfd_elf_fixup_group_sections (ibfd, NULL);
8659
338
}
8660
8661
/* Copy private symbol information.  If this symbol is in a section
8662
   which we did not map into a BFD section, try to map the section
8663
   index correctly.  We use special macro definitions for the mapped
8664
   section indices; these definitions are interpreted by the
8665
   swap_out_syms function.  */
8666
8667
7
#define MAP_ONESYMTAB (SHN_HIOS + 1)
8668
0
#define MAP_DYNSYMTAB (SHN_HIOS + 2)
8669
52
#define MAP_STRTAB    (SHN_HIOS + 3)
8670
6
#define MAP_SHSTRTAB  (SHN_HIOS + 4)
8671
2
#define MAP_SYM_SHNDX (SHN_HIOS + 5)
8672
8673
bool
8674
_bfd_elf_copy_private_symbol_data (bfd *ibfd,
8675
           asymbol **isymarg,
8676
           bfd *obfd ATTRIBUTE_UNUSED,
8677
           asymbol **osymarg)
8678
89.2k
{
8679
89.2k
  if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
8680
0
    return true;
8681
8682
89.2k
  elf_symbol_type *isym = elf_symbol_from (*isymarg);
8683
89.2k
  elf_symbol_type *osym = elf_symbol_from (*osymarg);
8684
89.2k
  if (isym != NULL
8685
89.2k
      && isym->internal_elf_sym.st_shndx != 0
8686
1.28k
      && osym != NULL
8687
1.28k
      && bfd_is_abs_section (isym->symbol.section))
8688
611
    {
8689
611
      unsigned int shndx;
8690
8691
611
      shndx = isym->internal_elf_sym.st_shndx;
8692
611
      if (shndx == elf_onesymtab (ibfd))
8693
4
  shndx = MAP_ONESYMTAB;
8694
607
      else if (shndx == elf_dynsymtab (ibfd))
8695
0
  shndx = MAP_DYNSYMTAB;
8696
607
      else if (shndx == elf_elfsections (ibfd)[elf_onesymtab (ibfd)]->sh_link)
8697
26
  shndx = MAP_STRTAB;
8698
581
      else if (shndx == elf_elfheader (ibfd)->e_shstrndx)
8699
6
  shndx = MAP_SHSTRTAB;
8700
575
      else if (find_section_in_list (shndx, elf_symtab_shndx_list (ibfd)))
8701
1
  shndx = MAP_SYM_SHNDX;
8702
611
      osym->internal_elf_sym.st_shndx = shndx;
8703
611
    }
8704
8705
89.2k
  return true;
8706
89.2k
}
8707
8708
/* Swap out the symbols.  */
8709
8710
static bool
8711
swap_out_syms (bfd *abfd,
8712
         struct elf_strtab_hash **sttp,
8713
         int relocatable_p,
8714
         struct bfd_link_info *info)
8715
74
{
8716
74
  elf_backend_data *bed;
8717
74
  unsigned int symcount;
8718
74
  asymbol **syms;
8719
74
  struct elf_strtab_hash *stt;
8720
74
  Elf_Internal_Shdr *symtab_hdr;
8721
74
  Elf_Internal_Shdr *symtab_shndx_hdr;
8722
74
  Elf_Internal_Shdr *symstrtab_hdr;
8723
74
  struct elf_sym_strtab *symstrtab;
8724
74
  bfd_byte *outbound_syms;
8725
74
  bfd_byte *outbound_shndx;
8726
74
  unsigned long outbound_syms_index;
8727
74
  unsigned int idx;
8728
74
  unsigned int num_locals;
8729
74
  size_t amt;
8730
74
  bool name_local_sections;
8731
8732
74
  if (!elf_map_symbols (abfd, &num_locals))
8733
0
    return false;
8734
8735
  /* Dump out the symtabs.  */
8736
74
  stt = _bfd_elf_strtab_init ();
8737
74
  if (stt == NULL)
8738
0
    return false;
8739
8740
74
  bed = get_elf_backend_data (abfd);
8741
74
  symcount = bfd_get_symcount (abfd);
8742
74
  symtab_hdr = &elf_symtab_hdr (abfd);
8743
74
  symtab_hdr->sh_type = SHT_SYMTAB;
8744
74
  symtab_hdr->sh_entsize = bed->s->sizeof_sym;
8745
74
  symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1);
8746
74
  symtab_hdr->sh_info = num_locals + 1;
8747
74
  symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
8748
8749
74
  symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
8750
74
  symstrtab_hdr->sh_type = SHT_STRTAB;
8751
8752
  /* Allocate buffer to swap out the .strtab section.  */
8753
74
  if (_bfd_mul_overflow (symcount + 1, sizeof (*symstrtab), &amt)
8754
74
      || (symstrtab = bfd_malloc (amt)) == NULL)
8755
0
    {
8756
0
      bfd_set_error (bfd_error_no_memory);
8757
0
      _bfd_elf_strtab_free (stt);
8758
0
      return false;
8759
0
    }
8760
8761
74
  if (_bfd_mul_overflow (symcount + 1, bed->s->sizeof_sym, &amt)
8762
74
      || (outbound_syms = bfd_malloc (amt)) == NULL)
8763
0
    {
8764
0
    error_no_mem:
8765
0
      bfd_set_error (bfd_error_no_memory);
8766
0
    error_return:
8767
0
      free (symstrtab);
8768
0
      _bfd_elf_strtab_free (stt);
8769
0
      return false;
8770
0
    }
8771
74
  symtab_hdr->contents = outbound_syms;
8772
74
  outbound_syms_index = 0;
8773
8774
74
  outbound_shndx = NULL;
8775
8776
74
  if (elf_symtab_shndx_list (abfd))
8777
0
    {
8778
0
      symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
8779
0
      if (symtab_shndx_hdr->sh_name != 0)
8780
0
  {
8781
0
    if (_bfd_mul_overflow (symcount + 1,
8782
0
         sizeof (Elf_External_Sym_Shndx), &amt))
8783
0
      goto error_no_mem;
8784
0
    outbound_shndx = bfd_zalloc (abfd, amt);
8785
0
    if (outbound_shndx == NULL)
8786
0
      goto error_return;
8787
8788
0
    symtab_shndx_hdr->contents = outbound_shndx;
8789
0
    symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
8790
0
    symtab_shndx_hdr->sh_size = amt;
8791
0
    symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
8792
0
    symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
8793
0
  }
8794
      /* FIXME: What about any other headers in the list ?  */
8795
0
    }
8796
8797
  /* Now generate the data (for "contents").  */
8798
74
  {
8799
    /* Fill in zeroth symbol and swap it out.  */
8800
74
    Elf_Internal_Sym sym;
8801
74
    sym.st_name = 0;
8802
74
    sym.st_value = 0;
8803
74
    sym.st_size = 0;
8804
74
    sym.st_info = 0;
8805
74
    sym.st_other = 0;
8806
74
    sym.st_shndx = SHN_UNDEF;
8807
74
    sym.st_target_internal = 0;
8808
74
    symstrtab[outbound_syms_index].sym = sym;
8809
74
    symstrtab[outbound_syms_index].dest_index = outbound_syms_index;
8810
74
    outbound_syms_index++;
8811
74
  }
8812
8813
74
  name_local_sections
8814
74
    = (bed->elf_backend_name_local_section_symbols
8815
5
       && bed->elf_backend_name_local_section_symbols (abfd));
8816
8817
74
  syms = bfd_get_outsymbols (abfd);
8818
1.46k
  for (idx = 0; idx < symcount; idx++)
8819
1.39k
    {
8820
1.39k
      Elf_Internal_Sym sym;
8821
8822
1.39k
      flagword flags = syms[idx]->flags;
8823
1.39k
      if (!name_local_sections
8824
1.30k
    && (flags & (BSF_SECTION_SYM | BSF_GLOBAL)) == BSF_SECTION_SYM)
8825
161
  {
8826
    /* Local section symbols have no name.  */
8827
161
    sym.st_name = 0;
8828
161
  }
8829
1.23k
      else
8830
1.23k
  {
8831
    /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
8832
       to get the final offset for st_name.  */
8833
1.23k
    size_t stridx = _bfd_elf_strtab_add (stt, syms[idx]->name, false);
8834
1.23k
    if (stridx == (size_t) -1)
8835
0
      goto error_return;
8836
1.23k
    sym.st_name = stridx;
8837
1.23k
  }
8838
8839
1.39k
      bfd_vma value = syms[idx]->value;
8840
1.39k
      elf_symbol_type *type_ptr = elf_symbol_from (syms[idx]);
8841
1.39k
      asection *sec = syms[idx]->section;
8842
8843
1.39k
      if ((flags & BSF_SECTION_SYM) == 0 && bfd_is_com_section (sec))
8844
6
  {
8845
    /* ELF common symbols put the alignment into the `value' field,
8846
       and the size into the `size' field.  This is backwards from
8847
       how BFD handles it, so reverse it here.  */
8848
6
    sym.st_size = value;
8849
6
    if (type_ptr == NULL
8850
6
        || type_ptr->internal_elf_sym.st_value == 0)
8851
6
      sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value));
8852
0
    else
8853
0
      sym.st_value = type_ptr->internal_elf_sym.st_value;
8854
6
    sym.st_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
8855
6
  }
8856
1.38k
      else
8857
1.38k
  {
8858
1.38k
    unsigned int shndx;
8859
8860
1.38k
    if (sec->output_section)
8861
1.35k
      {
8862
1.35k
        value += sec->output_offset;
8863
1.35k
        sec = sec->output_section;
8864
1.35k
      }
8865
8866
    /* Don't add in the section vma for relocatable output.  */
8867
1.38k
    if (! relocatable_p)
8868
810
      value += sec->vma;
8869
1.38k
    sym.st_value = value;
8870
1.38k
    sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0;
8871
8872
1.38k
    if (bfd_is_abs_section (sec)
8873
414
        && type_ptr != NULL
8874
414
        && type_ptr->internal_elf_sym.st_shndx != 0)
8875
414
      {
8876
        /* This symbol is in a real ELF section which we did
8877
     not create as a BFD section.  Undo the mapping done
8878
     by copy_private_symbol_data.  */
8879
414
        shndx = type_ptr->internal_elf_sym.st_shndx;
8880
414
        switch (shndx)
8881
414
    {
8882
3
    case MAP_ONESYMTAB:
8883
3
      shndx = elf_onesymtab (abfd);
8884
3
      break;
8885
0
    case MAP_DYNSYMTAB:
8886
0
      shndx = elf_dynsymtab (abfd);
8887
0
      break;
8888
26
    case MAP_STRTAB:
8889
26
      shndx = elf_strtab_sec (abfd);
8890
26
      break;
8891
0
    case MAP_SHSTRTAB:
8892
0
      shndx = elf_shstrtab_sec (abfd);
8893
0
      break;
8894
1
    case MAP_SYM_SHNDX:
8895
1
      if (elf_symtab_shndx_list (abfd))
8896
0
        shndx = elf_symtab_shndx_list (abfd)->ndx;
8897
1
      break;
8898
0
    case SHN_COMMON:
8899
10
    case SHN_ABS:
8900
10
      shndx = SHN_ABS;
8901
10
      break;
8902
374
    default:
8903
374
      if (shndx >= SHN_LOPROC && shndx <= SHN_HIOS)
8904
5
        {
8905
5
          if (bed->symbol_section_index)
8906
0
      shndx = bed->symbol_section_index (abfd, type_ptr);
8907
          /* Otherwise just leave the index alone.  */
8908
5
        }
8909
369
      else
8910
369
        {
8911
369
          if (shndx > SHN_HIOS && shndx < SHN_HIRESERVE)
8912
4
      _bfd_error_handler (_("%pB: \
8913
4
Unable to handle section index %x in ELF symbol.  Using ABS instead."),
8914
4
            abfd, shndx);
8915
369
          shndx = SHN_ABS;
8916
369
        }
8917
374
      break;
8918
414
    }
8919
414
      }
8920
974
    else
8921
974
      {
8922
974
        shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
8923
8924
974
        if (shndx == SHN_BAD)
8925
0
    {
8926
0
      asection *sec2;
8927
8928
      /* Writing this would be a hell of a lot easier if
8929
         we had some decent documentation on bfd, and
8930
         knew what to expect of the library, and what to
8931
         demand of applications.  For example, it
8932
         appears that `objcopy' might not set the
8933
         section of a symbol to be a section that is
8934
         actually in the output file.  */
8935
0
      sec2 = bfd_get_section_by_name (abfd, sec->name);
8936
0
      if (sec2 != NULL)
8937
0
        shndx = _bfd_elf_section_from_bfd_section (abfd, sec2);
8938
0
      if (shndx == SHN_BAD)
8939
0
        {
8940
          /* xgettext:c-format */
8941
0
          _bfd_error_handler
8942
0
      (_("unable to find equivalent output section"
8943
0
         " for symbol '%s' from section '%s'"),
8944
0
       syms[idx]->name ? syms[idx]->name : "<Local sym>",
8945
0
       sec->name);
8946
0
          bfd_set_error (bfd_error_invalid_operation);
8947
0
          goto error_return;
8948
0
        }
8949
0
    }
8950
974
      }
8951
8952
1.38k
    sym.st_shndx = shndx;
8953
1.38k
  }
8954
8955
1.39k
      int type;
8956
1.39k
      if ((flags & BSF_THREAD_LOCAL) != 0)
8957
74
  type = STT_TLS;
8958
1.32k
      else if ((flags & BSF_GNU_INDIRECT_FUNCTION) != 0)
8959
15
  type = STT_GNU_IFUNC;
8960
1.30k
      else if ((flags & BSF_FUNCTION) != 0)
8961
200
  type = STT_FUNC;
8962
1.10k
      else if ((flags & BSF_OBJECT) != 0)
8963
180
  type = STT_OBJECT;
8964
925
      else if ((flags & BSF_RELC) != 0)
8965
5
  type = STT_RELC;
8966
920
      else if ((flags & BSF_SRELC) != 0)
8967
9
  type = STT_SRELC;
8968
911
      else
8969
911
  type = STT_NOTYPE;
8970
8971
1.39k
      if (syms[idx]->section->flags & SEC_THREAD_LOCAL)
8972
0
  type = STT_TLS;
8973
8974
      /* Processor-specific types.  */
8975
1.39k
      if (type_ptr != NULL
8976
1.39k
    && bed->elf_backend_get_symbol_type)
8977
23
  type = ((*bed->elf_backend_get_symbol_type)
8978
23
    (&type_ptr->internal_elf_sym, type));
8979
8980
1.39k
      if (flags & BSF_SECTION_SYM)
8981
173
  {
8982
173
    if (flags & BSF_GLOBAL)
8983
9
      sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
8984
164
    else
8985
164
      sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
8986
173
  }
8987
1.22k
      else if (bfd_is_com_section (syms[idx]->section))
8988
6
  {
8989
6
    if (type != STT_TLS)
8990
6
      {
8991
6
        if ((abfd->flags & BFD_CONVERT_ELF_COMMON))
8992
0
    type = ((abfd->flags & BFD_USE_ELF_STT_COMMON)
8993
0
      ? STT_COMMON : STT_OBJECT);
8994
6
        else
8995
6
    type = ((flags & BSF_ELF_COMMON) != 0
8996
6
      ? STT_COMMON : STT_OBJECT);
8997
6
      }
8998
6
    sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
8999
6
  }
9000
1.21k
      else if (bfd_is_und_section (syms[idx]->section))
9001
602
  sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK)
9002
1.21k
            ? STB_WEAK
9003
1.21k
            : STB_GLOBAL),
9004
1.21k
           type);
9005
613
      else if (flags & BSF_FILE)
9006
18
  sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
9007
595
      else
9008
595
  {
9009
595
    int bind = STB_LOCAL;
9010
9011
595
    if (flags & BSF_LOCAL)
9012
324
      bind = STB_LOCAL;
9013
271
    else if (flags & BSF_GNU_UNIQUE)
9014
29
      bind = STB_GNU_UNIQUE;
9015
242
    else if (flags & BSF_WEAK)
9016
17
      bind = STB_WEAK;
9017
225
    else if (flags & BSF_GLOBAL)
9018
194
      bind = STB_GLOBAL;
9019
9020
595
    sym.st_info = ELF_ST_INFO (bind, type);
9021
595
  }
9022
9023
1.39k
      if (type_ptr != NULL)
9024
1.39k
  {
9025
1.39k
    sym.st_other = type_ptr->internal_elf_sym.st_other;
9026
1.39k
    sym.st_target_internal
9027
1.39k
      = type_ptr->internal_elf_sym.st_target_internal;
9028
1.39k
  }
9029
0
      else
9030
0
  {
9031
0
    sym.st_other = 0;
9032
0
    sym.st_target_internal = 0;
9033
0
  }
9034
9035
1.39k
      symstrtab[outbound_syms_index].sym = sym;
9036
1.39k
      symstrtab[outbound_syms_index].dest_index = outbound_syms_index;
9037
1.39k
      outbound_syms_index++;
9038
1.39k
    }
9039
9040
  /* Finalize the .strtab section.  */
9041
74
  _bfd_elf_strtab_finalize (stt);
9042
9043
  /* Swap out the .strtab section.  */
9044
1.54k
  for (idx = 0; idx < outbound_syms_index; idx++)
9045
1.46k
    {
9046
1.46k
      struct elf_sym_strtab *elfsym = &symstrtab[idx];
9047
1.46k
      if (elfsym->sym.st_name != 0)
9048
355
  elfsym->sym.st_name = _bfd_elf_strtab_offset (stt,
9049
355
                  elfsym->sym.st_name);
9050
1.46k
      if (info && info->callbacks->ctf_new_symbol)
9051
0
  info->callbacks->ctf_new_symbol (elfsym->dest_index,
9052
0
           &elfsym->sym);
9053
9054
      /* Inform the linker of the addition of this symbol.  */
9055
9056
1.46k
      bed->s->swap_symbol_out (abfd, &elfsym->sym,
9057
1.46k
             (outbound_syms
9058
1.46k
        + (elfsym->dest_index
9059
1.46k
           * bed->s->sizeof_sym)),
9060
1.46k
             NPTR_ADD (outbound_shndx,
9061
1.46k
           (elfsym->dest_index
9062
1.46k
            * sizeof (Elf_External_Sym_Shndx))));
9063
1.46k
    }
9064
74
  free (symstrtab);
9065
9066
74
  *sttp = stt;
9067
74
  symstrtab_hdr->sh_size = _bfd_elf_strtab_size (stt);
9068
74
  symstrtab_hdr->sh_type = SHT_STRTAB;
9069
74
  symstrtab_hdr->sh_addralign = 1;
9070
9071
74
  return true;
9072
74
}
9073
9074
/* Return the number of bytes required to hold the symtab vector.
9075
9076
   Note that we base it on the count plus 1, since we will null terminate
9077
   the vector allocated based on this size.  However, the ELF symbol table
9078
   always has a dummy entry as symbol #0, so it ends up even.  */
9079
9080
long
9081
_bfd_elf_get_symtab_upper_bound (bfd *abfd)
9082
8.79k
{
9083
8.79k
  bfd_size_type symcount;
9084
8.79k
  long symtab_size;
9085
8.79k
  Elf_Internal_Shdr *hdr = &elf_symtab_hdr (abfd);
9086
9087
8.79k
  symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
9088
8.79k
  if (symcount > LONG_MAX / sizeof (asymbol *))
9089
0
    {
9090
0
      bfd_set_error (bfd_error_file_too_big);
9091
0
      return -1;
9092
0
    }
9093
8.79k
  symtab_size = symcount * (sizeof (asymbol *));
9094
8.79k
  if (symcount == 0)
9095
748
    symtab_size = sizeof (asymbol *);
9096
8.04k
  else if (!bfd_write_p (abfd))
9097
8.04k
    {
9098
8.04k
      ufile_ptr filesize = bfd_get_file_size (abfd);
9099
9100
8.04k
      if (filesize != 0 && (unsigned long) symtab_size > filesize)
9101
206
  {
9102
206
    bfd_set_error (bfd_error_file_truncated);
9103
206
    return -1;
9104
206
  }
9105
8.04k
    }
9106
9107
8.58k
  return symtab_size;
9108
8.79k
}
9109
9110
long
9111
_bfd_elf_get_dynamic_symtab_upper_bound (bfd *abfd)
9112
17.1k
{
9113
17.1k
  bfd_size_type symcount;
9114
17.1k
  long symtab_size;
9115
17.1k
  Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr;
9116
9117
17.1k
  if (elf_dynsymtab (abfd) == 0)
9118
16.7k
    {
9119
      /* Check if there is dynamic symbol table.  */
9120
16.7k
      symcount = elf_tdata (abfd)->dt_symtab_count;
9121
16.7k
      if (symcount)
9122
10
  goto compute_symtab_size;
9123
9124
16.6k
      bfd_set_error (bfd_error_invalid_operation);
9125
16.6k
      return -1;
9126
16.7k
    }
9127
9128
411
  symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
9129
411
  if (symcount > LONG_MAX / sizeof (asymbol *))
9130
0
    {
9131
0
      bfd_set_error (bfd_error_file_too_big);
9132
0
      return -1;
9133
0
    }
9134
9135
421
 compute_symtab_size:
9136
421
  symtab_size = symcount * (sizeof (asymbol *));
9137
421
  if (symcount == 0)
9138
5
    symtab_size = sizeof (asymbol *);
9139
416
  else if (!bfd_write_p (abfd))
9140
416
    {
9141
416
      ufile_ptr filesize = bfd_get_file_size (abfd);
9142
9143
416
      if (filesize != 0 && (unsigned long) symtab_size > filesize)
9144
27
  {
9145
27
    bfd_set_error (bfd_error_file_truncated);
9146
27
    return -1;
9147
27
  }
9148
416
    }
9149
9150
394
  return symtab_size;
9151
421
}
9152
9153
long
9154
_bfd_elf_get_reloc_upper_bound (bfd *abfd, sec_ptr asect)
9155
21.7k
{
9156
21.7k
  if (asect->reloc_count != 0 && !bfd_write_p (abfd))
9157
19.3k
    {
9158
      /* Sanity check reloc section size.  */
9159
19.3k
      ufile_ptr filesize = bfd_get_file_size (abfd);
9160
9161
19.3k
      if (filesize != 0)
9162
19.3k
  {
9163
19.3k
    struct bfd_elf_section_data *d = elf_section_data (asect);
9164
19.3k
    bfd_size_type rel_size = d->rel.hdr ? d->rel.hdr->sh_size : 0;
9165
19.3k
    bfd_size_type rela_size = d->rela.hdr ? d->rela.hdr->sh_size : 0;
9166
9167
19.3k
    if (rel_size + rela_size > filesize
9168
18.7k
        || rel_size + rela_size < rel_size)
9169
618
      {
9170
618
        bfd_set_error (bfd_error_file_truncated);
9171
618
        return -1;
9172
618
      }
9173
19.3k
  }
9174
19.3k
    }
9175
9176
#if SIZEOF_LONG == SIZEOF_INT
9177
  if (asect->reloc_count >= LONG_MAX / sizeof (arelent *))
9178
    {
9179
      bfd_set_error (bfd_error_file_too_big);
9180
      return -1;
9181
    }
9182
#endif
9183
21.1k
  return (asect->reloc_count + 1L) * sizeof (arelent *);
9184
21.7k
}
9185
9186
/* Canonicalize the relocs.  */
9187
9188
long
9189
_bfd_elf_canonicalize_reloc (bfd *abfd,
9190
           sec_ptr section,
9191
           arelent **relptr,
9192
           asymbol **symbols)
9193
21.0k
{
9194
21.0k
  arelent *tblptr;
9195
21.0k
  unsigned int i;
9196
21.0k
  elf_backend_data *bed = get_elf_backend_data (abfd);
9197
9198
21.0k
  if (! bed->s->slurp_reloc_table (abfd, section, symbols, false))
9199
4.21k
    return -1;
9200
9201
16.8k
  tblptr = section->relocation;
9202
184k
  for (i = 0; i < section->reloc_count; i++)
9203
167k
    *relptr++ = tblptr++;
9204
9205
16.8k
  *relptr = NULL;
9206
9207
16.8k
  return section->reloc_count;
9208
21.0k
}
9209
9210
long
9211
_bfd_elf_canonicalize_symtab (bfd *abfd, asymbol **allocation)
9212
8.58k
{
9213
8.58k
  elf_backend_data *bed = get_elf_backend_data (abfd);
9214
8.58k
  long symcount = bed->s->slurp_symbol_table (abfd, allocation, false);
9215
9216
8.58k
  if (symcount >= 0)
9217
7.52k
    abfd->symcount = symcount;
9218
8.58k
  return symcount;
9219
8.58k
}
9220
9221
long
9222
_bfd_elf_canonicalize_dynamic_symtab (bfd *abfd,
9223
              asymbol **allocation)
9224
260
{
9225
260
  elf_backend_data *bed = get_elf_backend_data (abfd);
9226
260
  long symcount = bed->s->slurp_symbol_table (abfd, allocation, true);
9227
9228
260
  if (symcount >= 0)
9229
121
    abfd->dynsymcount = symcount;
9230
260
  return symcount;
9231
260
}
9232
9233
/* Return the size required for the dynamic reloc entries.  Any loadable
9234
   section that was actually installed in the BFD, and has type SHT_REL
9235
   or SHT_RELA, and uses the dynamic symbol table, is considered to be a
9236
   dynamic reloc section.  */
9237
9238
long
9239
_bfd_elf_get_dynamic_reloc_upper_bound (bfd *abfd)
9240
16.7k
{
9241
16.7k
  bfd_size_type count, ext_rel_size;
9242
16.7k
  asection *s;
9243
9244
16.7k
  if (elf_dynsymtab (abfd) == 0)
9245
16.5k
    {
9246
16.5k
      bfd_set_error (bfd_error_invalid_operation);
9247
16.5k
      return -1;
9248
16.5k
    }
9249
9250
181
  count = 1;
9251
181
  ext_rel_size = 0;
9252
4.26k
  for (s = abfd->sections; s != NULL; s = s->next)
9253
4.08k
    if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
9254
533
  && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
9255
501
      || elf_section_data (s)->this_hdr.sh_type == SHT_RELA)
9256
221
  && (elf_section_data (s)->this_hdr.sh_flags & SHF_COMPRESSED) == 0)
9257
221
      {
9258
221
  ext_rel_size += elf_section_data (s)->this_hdr.sh_size;
9259
221
  if (ext_rel_size < elf_section_data (s)->this_hdr.sh_size)
9260
0
    {
9261
0
      bfd_set_error (bfd_error_file_truncated);
9262
0
      return -1;
9263
0
    }
9264
221
  count += NUM_SHDR_ENTRIES (&elf_section_data (s)->this_hdr);
9265
221
  if (count > LONG_MAX / sizeof (arelent *))
9266
0
    {
9267
0
      bfd_set_error (bfd_error_file_too_big);
9268
0
      return -1;
9269
0
    }
9270
221
      }
9271
181
  if (count > 1 && !bfd_write_p (abfd))
9272
138
    {
9273
      /* Sanity check reloc section sizes.  */
9274
138
      ufile_ptr filesize = bfd_get_file_size (abfd);
9275
138
      if (filesize != 0 && ext_rel_size > filesize)
9276
3
  {
9277
3
    bfd_set_error (bfd_error_file_truncated);
9278
3
    return -1;
9279
3
  }
9280
138
    }
9281
178
  return count * sizeof (arelent *);
9282
181
}
9283
9284
/* Canonicalize the dynamic relocation entries.  Note that we return the
9285
   dynamic relocations as a single block, although they are actually
9286
   associated with particular sections; the interface, which was
9287
   designed for SunOS style shared libraries, expects that there is only
9288
   one set of dynamic relocs.  Any loadable section that was actually
9289
   installed in the BFD, and has type SHT_REL or SHT_RELA, and uses the
9290
   dynamic symbol table, is considered to be a dynamic reloc section.  */
9291
9292
long
9293
_bfd_elf_canonicalize_dynamic_reloc (bfd *abfd,
9294
             arelent **storage,
9295
             asymbol **syms)
9296
159
{
9297
159
  bool (*slurp_relocs) (bfd *, asection *, asymbol **, bool);
9298
159
  asection *s;
9299
159
  long ret;
9300
9301
159
  if (elf_dynsymtab (abfd) == 0)
9302
0
    {
9303
0
      bfd_set_error (bfd_error_invalid_operation);
9304
0
      return -1;
9305
0
    }
9306
9307
159
  slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
9308
159
  ret = 0;
9309
3.32k
  for (s = abfd->sections; s != NULL; s = s->next)
9310
3.18k
    {
9311
3.18k
      if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
9312
446
    && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
9313
417
        || elf_section_data (s)->this_hdr.sh_type == SHT_RELA)
9314
185
    && (elf_section_data (s)->this_hdr.sh_flags & SHF_COMPRESSED) == 0)
9315
185
  {
9316
185
    arelent *p;
9317
185
    long count, i;
9318
9319
185
    if (! (*slurp_relocs) (abfd, s, syms, true))
9320
19
      return -1;
9321
166
    count = NUM_SHDR_ENTRIES (&elf_section_data (s)->this_hdr);
9322
166
    p = s->relocation;
9323
1.40k
    for (i = 0; i < count; i++)
9324
1.23k
      *storage++ = p++;
9325
166
    ret += count;
9326
166
  }
9327
3.18k
    }
9328
9329
140
  *storage = NULL;
9330
9331
140
  return ret;
9332
159
}
9333

9334
/* Read in the version information.  */
9335
9336
bool
9337
_bfd_elf_slurp_version_tables (bfd *abfd, bool default_imported_symver)
9338
319
{
9339
319
  bfd_byte *contents = NULL;
9340
319
  unsigned int freeidx = 0;
9341
319
  size_t amt;
9342
319
  void *contents_addr = NULL;
9343
319
  size_t contents_size = 0;
9344
9345
319
  if (elf_dynverref (abfd) != 0 || elf_tdata (abfd)->dt_verneed != NULL)
9346
284
    {
9347
284
      Elf_Internal_Shdr *hdr;
9348
284
      Elf_External_Verneed *everneed;
9349
284
      Elf_Internal_Verneed *iverneed;
9350
284
      unsigned int i;
9351
284
      bfd_byte *contents_end;
9352
284
      size_t verneed_count;
9353
284
      size_t verneed_size;
9354
9355
284
      if (elf_tdata (abfd)->dt_verneed != NULL)
9356
2
  {
9357
2
    hdr = NULL;
9358
2
    contents = elf_tdata (abfd)->dt_verneed;
9359
2
    verneed_count = elf_tdata (abfd)->dt_verneed_count;
9360
2
    verneed_size = verneed_count * sizeof (Elf_External_Verneed);
9361
2
  }
9362
282
      else
9363
282
  {
9364
282
    hdr = &elf_tdata (abfd)->dynverref_hdr;
9365
9366
282
    if (hdr->sh_info > hdr->sh_size / sizeof (Elf_External_Verneed))
9367
8
      {
9368
134
      error_return_bad_verref:
9369
134
        _bfd_error_handler
9370
134
    (_("%pB: .gnu.version_r invalid entry"), abfd);
9371
134
        bfd_set_error (bfd_error_bad_value);
9372
157
      error_return_verref:
9373
157
        elf_tdata (abfd)->verref = NULL;
9374
157
        elf_tdata (abfd)->cverrefs = 0;
9375
157
        goto error_return;
9376
134
      }
9377
9378
274
    if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0)
9379
6
      goto error_return_verref;
9380
268
    contents_size = hdr->sh_size;
9381
268
    contents = _bfd_mmap_temporary (abfd, contents_size,
9382
268
            &contents_addr, &contents_size);
9383
268
    if (contents == NULL)
9384
17
      goto error_return_verref;
9385
9386
251
    verneed_size = hdr->sh_size;
9387
251
    verneed_count = hdr->sh_info;
9388
251
  }
9389
9390
253
      if (_bfd_mul_overflow (verneed_count,
9391
253
           sizeof (Elf_Internal_Verneed), &amt))
9392
0
  {
9393
0
    bfd_set_error (bfd_error_file_too_big);
9394
0
    goto error_return_verref;
9395
0
  }
9396
253
      if (amt == 0)
9397
0
  goto error_return_verref;
9398
253
      elf_tdata (abfd)->verref = bfd_zalloc (abfd, amt);
9399
253
      if (elf_tdata (abfd)->verref == NULL)
9400
0
  goto error_return_verref;
9401
9402
253
      BFD_ASSERT (sizeof (Elf_External_Verneed)
9403
253
      == sizeof (Elf_External_Vernaux));
9404
253
      contents_end = (contents + verneed_size
9405
253
          - sizeof (Elf_External_Verneed));
9406
253
      everneed = (Elf_External_Verneed *) contents;
9407
253
      iverneed = elf_tdata (abfd)->verref;
9408
275
      for (i = 0; i < verneed_count; i++, iverneed++)
9409
253
  {
9410
253
    Elf_External_Vernaux *evernaux;
9411
253
    Elf_Internal_Vernaux *ivernaux;
9412
253
    unsigned int j;
9413
9414
253
    _bfd_elf_swap_verneed_in (abfd, everneed, iverneed);
9415
9416
253
    iverneed->vn_bfd = abfd;
9417
9418
253
    if (elf_use_dt_symtab_p (abfd))
9419
2
      {
9420
2
        if (iverneed->vn_file < elf_tdata (abfd)->dt_strsz)
9421
1
    iverneed->vn_filename
9422
1
      = elf_tdata (abfd)->dt_strtab + iverneed->vn_file;
9423
1
        else
9424
1
    iverneed->vn_filename = NULL;
9425
2
      }
9426
251
    else if (hdr == NULL)
9427
0
      goto error_return_bad_verref;
9428
251
    else
9429
251
      iverneed->vn_filename
9430
251
        = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
9431
251
             iverneed->vn_file);
9432
253
    if (iverneed->vn_filename == NULL)
9433
89
      goto error_return_bad_verref;
9434
9435
164
    if (iverneed->vn_cnt == 0)
9436
57
      iverneed->vn_auxptr = NULL;
9437
107
    else
9438
107
      {
9439
107
        if (_bfd_mul_overflow (iverneed->vn_cnt,
9440
107
             sizeof (Elf_Internal_Vernaux), &amt))
9441
0
    {
9442
0
      bfd_set_error (bfd_error_file_too_big);
9443
0
      goto error_return_verref;
9444
0
    }
9445
107
        iverneed->vn_auxptr = bfd_alloc (abfd, amt);
9446
107
        if (iverneed->vn_auxptr == NULL)
9447
0
    goto error_return_verref;
9448
107
      }
9449
9450
164
    if (iverneed->vn_aux
9451
164
        > (size_t) (contents_end - (bfd_byte *) everneed))
9452
22
      goto error_return_bad_verref;
9453
9454
142
    evernaux = ((Elf_External_Vernaux *)
9455
142
          ((bfd_byte *) everneed + iverneed->vn_aux));
9456
142
    ivernaux = iverneed->vn_auxptr;
9457
203
    for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++)
9458
149
      {
9459
149
        _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux);
9460
9461
149
        if (elf_use_dt_symtab_p (abfd))
9462
0
    {
9463
0
      if (ivernaux->vna_name < elf_tdata (abfd)->dt_strsz)
9464
0
        ivernaux->vna_nodename
9465
0
          = elf_tdata (abfd)->dt_strtab + ivernaux->vna_name;
9466
0
      else
9467
0
        ivernaux->vna_nodename = NULL;
9468
0
    }
9469
149
        else if (hdr == NULL)
9470
0
    goto error_return_bad_verref;
9471
149
        else
9472
149
    ivernaux->vna_nodename
9473
149
      = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
9474
149
                 ivernaux->vna_name);
9475
149
        if (ivernaux->vna_nodename == NULL)
9476
4
    goto error_return_bad_verref;
9477
9478
145
        if (ivernaux->vna_other > freeidx)
9479
83
    freeidx = ivernaux->vna_other;
9480
9481
145
        ivernaux->vna_nextptr = NULL;
9482
145
        if (ivernaux->vna_next == 0)
9483
78
    {
9484
78
      iverneed->vn_cnt = j + 1;
9485
78
      break;
9486
78
    }
9487
67
        if (j + 1 < iverneed->vn_cnt)
9488
55
    ivernaux->vna_nextptr = ivernaux + 1;
9489
9490
67
        if (ivernaux->vna_next
9491
67
      > (size_t) (contents_end - (bfd_byte *) evernaux))
9492
6
    goto error_return_bad_verref;
9493
9494
61
        evernaux = ((Elf_External_Vernaux *)
9495
61
        ((bfd_byte *) evernaux + ivernaux->vna_next));
9496
61
      }
9497
9498
132
    iverneed->vn_nextref = NULL;
9499
132
    if (iverneed->vn_next == 0)
9500
105
      break;
9501
27
    if (hdr != NULL && (i + 1 < hdr->sh_info))
9502
0
      iverneed->vn_nextref = iverneed + 1;
9503
9504
27
    if (iverneed->vn_next
9505
27
        > (size_t) (contents_end - (bfd_byte *) everneed))
9506
5
      goto error_return_bad_verref;
9507
9508
22
    everneed = ((Elf_External_Verneed *)
9509
22
          ((bfd_byte *) everneed + iverneed->vn_next));
9510
22
  }
9511
127
      elf_tdata (abfd)->cverrefs = i;
9512
9513
127
      if (contents != elf_tdata (abfd)->dt_verneed)
9514
126
  _bfd_munmap_temporary (contents_addr, contents_size);
9515
127
      contents = NULL;
9516
127
      contents_addr = NULL;
9517
127
    }
9518
9519
162
  if (elf_dynverdef (abfd) != 0 || elf_tdata (abfd)->dt_verdef != NULL)
9520
58
    {
9521
58
      Elf_Internal_Shdr *hdr;
9522
58
      Elf_External_Verdef *everdef;
9523
58
      Elf_Internal_Verdef *iverdef;
9524
58
      Elf_Internal_Verdef *iverdefarr;
9525
58
      Elf_Internal_Verdef iverdefmem;
9526
58
      unsigned int i;
9527
58
      unsigned int maxidx;
9528
58
      bfd_byte *contents_end_def, *contents_end_aux;
9529
58
      size_t verdef_count;
9530
58
      size_t verdef_size;
9531
9532
58
      if (elf_tdata (abfd)->dt_verdef != NULL)
9533
0
  {
9534
0
    hdr = NULL;
9535
0
    contents = elf_tdata (abfd)->dt_verdef;
9536
0
    verdef_count = elf_tdata (abfd)->dt_verdef_count;
9537
0
    verdef_size = verdef_count * sizeof (Elf_External_Verdef);
9538
0
  }
9539
58
      else
9540
58
  {
9541
58
    hdr = &elf_tdata (abfd)->dynverdef_hdr;
9542
9543
58
    if (hdr->sh_size < sizeof (Elf_External_Verdef))
9544
2
      {
9545
34
      error_return_bad_verdef:
9546
34
        _bfd_error_handler
9547
34
    (_("%pB: .gnu.version_d invalid entry"), abfd);
9548
34
        bfd_set_error (bfd_error_bad_value);
9549
42
      error_return_verdef:
9550
42
        elf_tdata (abfd)->verdef = NULL;
9551
42
        elf_tdata (abfd)->cverdefs = 0;
9552
42
        goto error_return;
9553
34
      }
9554
9555
56
    if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0)
9556
3
      goto error_return_verdef;
9557
53
    contents_size = hdr->sh_size;
9558
53
    contents = _bfd_mmap_temporary (abfd, contents_size,
9559
53
            &contents_addr, &contents_size);
9560
53
    if (contents == NULL)
9561
5
      goto error_return_verdef;
9562
9563
48
    BFD_ASSERT (sizeof (Elf_External_Verdef)
9564
48
          >= sizeof (Elf_External_Verdaux));
9565
9566
48
    verdef_count = hdr->sh_info;
9567
48
    verdef_size = hdr->sh_size;
9568
48
  }
9569
9570
48
      contents_end_def = (contents + verdef_size
9571
48
        - sizeof (Elf_External_Verdef));
9572
48
      contents_end_aux = (contents + verdef_size
9573
48
        - sizeof (Elf_External_Verdaux));
9574
9575
      /* We know the number of entries in the section but not the maximum
9576
   index.  Therefore we have to run through all entries and find
9577
   the maximum.  */
9578
48
      everdef = (Elf_External_Verdef *) contents;
9579
48
      maxidx = 0;
9580
56
      for (i = 0; i < verdef_count; ++i)
9581
51
  {
9582
51
    _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
9583
9584
51
    if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) == 0)
9585
8
      goto error_return_bad_verdef;
9586
43
    if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) > maxidx)
9587
42
      maxidx = iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION);
9588
9589
43
    if (iverdefmem.vd_next == 0)
9590
23
      break;
9591
9592
20
    if (iverdefmem.vd_next
9593
20
        > (size_t) (contents_end_def - (bfd_byte *) everdef))
9594
12
      goto error_return_bad_verdef;
9595
9596
8
    everdef = ((Elf_External_Verdef *)
9597
8
         ((bfd_byte *) everdef + iverdefmem.vd_next));
9598
8
  }
9599
9600
28
      if (default_imported_symver)
9601
0
  {
9602
0
    if (freeidx > maxidx)
9603
0
      maxidx = ++freeidx;
9604
0
    else
9605
0
      freeidx = ++maxidx;
9606
0
  }
9607
28
      if (_bfd_mul_overflow (maxidx, sizeof (Elf_Internal_Verdef), &amt))
9608
0
  {
9609
0
    bfd_set_error (bfd_error_file_too_big);
9610
0
    goto error_return_verdef;
9611
0
  }
9612
9613
28
      if (amt == 0)
9614
0
  goto error_return_verdef;
9615
28
      elf_tdata (abfd)->verdef = bfd_zalloc (abfd, amt);
9616
28
      if (elf_tdata (abfd)->verdef == NULL)
9617
0
  goto error_return_verdef;
9618
9619
28
      elf_tdata (abfd)->cverdefs = maxidx;
9620
9621
28
      everdef = (Elf_External_Verdef *) contents;
9622
28
      iverdefarr = elf_tdata (abfd)->verdef;
9623
32
      for (i = 0; i < verdef_count; ++i)
9624
29
  {
9625
29
    Elf_External_Verdaux *everdaux;
9626
29
    Elf_Internal_Verdaux *iverdaux;
9627
29
    unsigned int j;
9628
9629
29
    _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
9630
9631
29
    if ((iverdefmem.vd_ndx & VERSYM_VERSION) == 0)
9632
0
      goto error_return_bad_verdef;
9633
9634
29
    iverdef = &iverdefarr[(iverdefmem.vd_ndx & VERSYM_VERSION) - 1];
9635
29
    memcpy (iverdef, &iverdefmem, offsetof (Elf_Internal_Verdef, vd_bfd));
9636
9637
29
    iverdef->vd_bfd = abfd;
9638
9639
29
    if (iverdef->vd_cnt == 0)
9640
18
      iverdef->vd_auxptr = NULL;
9641
11
    else
9642
11
      {
9643
11
        if (_bfd_mul_overflow (iverdef->vd_cnt,
9644
11
             sizeof (Elf_Internal_Verdaux), &amt))
9645
0
    {
9646
0
      bfd_set_error (bfd_error_file_too_big);
9647
0
      goto error_return_verdef;
9648
0
    }
9649
11
        iverdef->vd_auxptr = bfd_alloc (abfd, amt);
9650
11
        if (iverdef->vd_auxptr == NULL)
9651
0
    goto error_return_verdef;
9652
11
      }
9653
9654
29
    if (iverdef->vd_aux
9655
29
        > (size_t) (contents_end_aux - (bfd_byte *) everdef))
9656
4
      goto error_return_bad_verdef;
9657
9658
25
    everdaux = ((Elf_External_Verdaux *)
9659
25
          ((bfd_byte *) everdef + iverdef->vd_aux));
9660
25
    iverdaux = iverdef->vd_auxptr;
9661
25
    for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++)
9662
8
      {
9663
8
        _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux);
9664
9665
8
        if (elf_use_dt_symtab_p (abfd))
9666
0
    {
9667
0
      if (iverdaux->vda_name < elf_tdata (abfd)->dt_strsz)
9668
0
        iverdaux->vda_nodename
9669
0
          = elf_tdata (abfd)->dt_strtab + iverdaux->vda_name;
9670
0
      else
9671
0
        iverdaux->vda_nodename = NULL;
9672
0
    }
9673
8
        else
9674
8
    iverdaux->vda_nodename
9675
8
      = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
9676
8
                 iverdaux->vda_name);
9677
8
        if (iverdaux->vda_nodename == NULL)
9678
5
    goto error_return_bad_verdef;
9679
9680
3
        iverdaux->vda_nextptr = NULL;
9681
3
        if (iverdaux->vda_next == 0)
9682
0
    {
9683
0
      iverdef->vd_cnt = j + 1;
9684
0
      break;
9685
0
    }
9686
3
        if (j + 1 < iverdef->vd_cnt)
9687
2
    iverdaux->vda_nextptr = iverdaux + 1;
9688
9689
3
        if (iverdaux->vda_next
9690
3
      > (size_t) (contents_end_aux - (bfd_byte *) everdaux))
9691
3
    goto error_return_bad_verdef;
9692
9693
0
        everdaux = ((Elf_External_Verdaux *)
9694
0
        ((bfd_byte *) everdaux + iverdaux->vda_next));
9695
0
      }
9696
9697
17
    iverdef->vd_nodename = NULL;
9698
17
    if (iverdef->vd_cnt)
9699
0
      iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename;
9700
9701
17
    iverdef->vd_nextdef = NULL;
9702
17
    if (iverdef->vd_next == 0)
9703
13
      break;
9704
4
    if ((size_t) (iverdef - iverdefarr) + 1 < maxidx)
9705
1
      iverdef->vd_nextdef = iverdef + 1;
9706
9707
4
    everdef = ((Elf_External_Verdef *)
9708
4
         ((bfd_byte *) everdef + iverdef->vd_next));
9709
4
  }
9710
9711
16
      if (contents != elf_tdata (abfd)->dt_verdef)
9712
16
  _bfd_munmap_temporary (contents_addr, contents_size);
9713
16
      contents = NULL;
9714
16
      contents_addr = NULL;
9715
16
    }
9716
104
  else if (default_imported_symver)
9717
0
    {
9718
0
      if (freeidx < 3)
9719
0
  freeidx = 3;
9720
0
      else
9721
0
  freeidx++;
9722
9723
0
      if (_bfd_mul_overflow (freeidx, sizeof (Elf_Internal_Verdef), &amt))
9724
0
  {
9725
0
    bfd_set_error (bfd_error_file_too_big);
9726
0
    goto error_return;
9727
0
  }
9728
0
      if (amt == 0)
9729
0
  goto error_return;
9730
0
      elf_tdata (abfd)->verdef = bfd_zalloc (abfd, amt);
9731
0
      if (elf_tdata (abfd)->verdef == NULL)
9732
0
  goto error_return;
9733
9734
0
      elf_tdata (abfd)->cverdefs = freeidx;
9735
0
    }
9736
9737
  /* Create a default version based on the soname.  */
9738
120
  if (default_imported_symver)
9739
0
    {
9740
0
      Elf_Internal_Verdef *iverdef;
9741
0
      Elf_Internal_Verdaux *iverdaux;
9742
9743
0
      iverdef = &elf_tdata (abfd)->verdef[freeidx - 1];
9744
9745
0
      iverdef->vd_version = VER_DEF_CURRENT;
9746
0
      iverdef->vd_flags = 0;
9747
0
      iverdef->vd_ndx = freeidx;
9748
0
      iverdef->vd_cnt = 1;
9749
9750
0
      iverdef->vd_bfd = abfd;
9751
9752
0
      iverdef->vd_nodename = bfd_elf_get_dt_soname (abfd);
9753
0
      if (iverdef->vd_nodename == NULL)
9754
0
  goto error_return_verdef;
9755
0
      iverdef->vd_nextdef = NULL;
9756
0
      iverdef->vd_auxptr = bfd_zalloc (abfd, sizeof (Elf_Internal_Verdaux));
9757
0
      if (iverdef->vd_auxptr == NULL)
9758
0
  goto error_return_verdef;
9759
9760
0
      iverdaux = iverdef->vd_auxptr;
9761
0
      iverdaux->vda_nodename = iverdef->vd_nodename;
9762
0
    }
9763
9764
120
  return true;
9765
9766
199
 error_return:
9767
199
  if (contents != elf_tdata (abfd)->dt_verneed
9768
157
      && contents != elf_tdata (abfd)->dt_verdef)
9769
157
    _bfd_munmap_temporary (contents_addr, contents_size);
9770
199
  return false;
9771
120
}
9772

9773
asymbol *
9774
_bfd_elf_make_empty_symbol (bfd *abfd)
9775
1.94M
{
9776
1.94M
  elf_symbol_type *newsym;
9777
9778
1.94M
  newsym = bfd_zalloc (abfd, sizeof (*newsym));
9779
1.94M
  if (!newsym)
9780
0
    return NULL;
9781
1.94M
  newsym->symbol.the_bfd = abfd;
9782
1.94M
  return &newsym->symbol;
9783
1.94M
}
9784
9785
void
9786
_bfd_elf_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED,
9787
        asymbol *symbol,
9788
        symbol_info *ret)
9789
36.9k
{
9790
36.9k
  bfd_symbol_info (symbol, ret);
9791
36.9k
}
9792
9793
/* Return whether a symbol name implies a local symbol.  Most targets
9794
   use this function for the is_local_label_name entry point, but some
9795
   override it.  */
9796
9797
bool
9798
_bfd_elf_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED,
9799
            const char *name)
9800
18.7k
{
9801
  /* Normal local symbols start with ``.L''.  */
9802
18.7k
  if (name[0] == '.' && name[1] == 'L')
9803
391
    return true;
9804
9805
  /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
9806
     DWARF debugging symbols starting with ``..''.  */
9807
18.3k
  if (name[0] == '.' && name[1] == '.')
9808
487
    return true;
9809
9810
  /* gcc will sometimes generate symbols beginning with ``_.L_'' when
9811
     emitting DWARF debugging output.  I suspect this is actually a
9812
     small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
9813
     ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
9814
     underscore to be emitted on some ELF targets).  For ease of use,
9815
     we treat such symbols as local.  */
9816
17.8k
  if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_')
9817
1
    return true;
9818
9819
  /* Treat assembler generated fake symbols, dollar local labels and
9820
     forward-backward labels (aka local labels) as locals.
9821
     These labels have the form:
9822
9823
       L0^A.*              (fake symbols)
9824
9825
       [.]?L[0123456789]+{^A|^B}[0123456789]*  (local labels)
9826
9827
     Versions which start with .L will have already been matched above,
9828
     so we only need to match the rest.  */
9829
17.8k
  if (name[0] == 'L' && ISDIGIT (name[1]))
9830
59
    {
9831
59
      bool ret = false;
9832
59
      const char * p;
9833
59
      char c;
9834
9835
82
      for (p = name + 2; (c = *p); p++)
9836
74
  {
9837
74
    if (c == 1 || c == 2)
9838
0
      {
9839
0
        if (c == 1 && p == name + 2)
9840
    /* A fake symbol.  */
9841
0
    return true;
9842
9843
        /* FIXME: We are being paranoid here and treating symbols like
9844
     L0^Bfoo as if there were non-local, on the grounds that the
9845
     assembler will never generate them.  But can any symbol
9846
     containing an ASCII value in the range 1-31 ever be anything
9847
     other than some kind of local ?  */
9848
0
        ret = true;
9849
0
      }
9850
9851
74
    if (! ISDIGIT (c))
9852
51
      {
9853
51
        ret = false;
9854
51
        break;
9855
51
      }
9856
74
  }
9857
59
      return ret;
9858
59
    }
9859
9860
17.8k
  return false;
9861
17.8k
}
9862
9863
alent *
9864
_bfd_elf_get_lineno (bfd *abfd ATTRIBUTE_UNUSED,
9865
         asymbol *symbol ATTRIBUTE_UNUSED)
9866
0
{
9867
0
  abort ();
9868
0
  return NULL;
9869
0
}
9870
9871
bool
9872
_bfd_elf_set_arch_mach (bfd *abfd,
9873
      enum bfd_architecture arch,
9874
      unsigned long machine)
9875
1.22k
{
9876
  /* If this isn't the right architecture for this backend, and this
9877
     isn't the generic backend, fail.  */
9878
1.22k
  if (arch != get_elf_backend_data (abfd)->arch
9879
0
      && arch != bfd_arch_unknown
9880
0
      && get_elf_backend_data (abfd)->arch != bfd_arch_unknown)
9881
0
    return false;
9882
9883
1.22k
  return bfd_default_set_arch_mach (abfd, arch, machine);
9884
1.22k
}
9885
9886
/* Find the nearest line to a particular section and offset,
9887
   for error reporting.  */
9888
9889
bool
9890
_bfd_elf_find_nearest_line (bfd *abfd,
9891
          asymbol **symbols,
9892
          asection *section,
9893
          bfd_vma offset,
9894
          const char **filename_ptr,
9895
          const char **functionname_ptr,
9896
          unsigned int *line_ptr,
9897
          unsigned int *discriminator_ptr)
9898
34.5k
{
9899
34.5k
  return _bfd_elf_find_nearest_line_with_alt (abfd, NULL, symbols, section,
9900
34.5k
                offset, filename_ptr,
9901
34.5k
                functionname_ptr, line_ptr,
9902
34.5k
                discriminator_ptr);
9903
34.5k
}
9904
9905
/* Find the nearest line to a particular section and offset,
9906
   for error reporting.  ALT_BFD representing a .gnu_debugaltlink file
9907
   can be optionally specified.  */
9908
9909
bool
9910
_bfd_elf_find_nearest_line_with_alt (bfd *abfd,
9911
             const char *alt_filename,
9912
             asymbol **symbols,
9913
             asection *section,
9914
             bfd_vma offset,
9915
             const char **filename_ptr,
9916
             const char **functionname_ptr,
9917
             unsigned int *line_ptr,
9918
             unsigned int *discriminator_ptr)
9919
34.5k
{
9920
34.5k
  bool found;
9921
9922
34.5k
  if (_bfd_dwarf2_find_nearest_line_with_alt (abfd, alt_filename, symbols, NULL,
9923
34.5k
                section, offset, filename_ptr,
9924
34.5k
                functionname_ptr, line_ptr,
9925
34.5k
                discriminator_ptr,
9926
34.5k
                dwarf_debug_sections,
9927
34.5k
                &elf_tdata (abfd)->dwarf2_find_line_info))
9928
9.68k
    return true;
9929
9930
24.8k
  if (_bfd_dwarf1_find_nearest_line (abfd, symbols, section, offset,
9931
24.8k
             filename_ptr, functionname_ptr, line_ptr))
9932
0
    {
9933
0
      if (!*functionname_ptr)
9934
0
  _bfd_elf_find_function (abfd, symbols, section, offset,
9935
0
        *filename_ptr ? NULL : filename_ptr,
9936
0
        functionname_ptr);
9937
0
      return true;
9938
0
    }
9939
9940
24.8k
  if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
9941
24.8k
               &found, filename_ptr,
9942
24.8k
               functionname_ptr, line_ptr,
9943
24.8k
               &elf_tdata (abfd)->line_info))
9944
0
    return false;
9945
24.8k
  if (found && (*functionname_ptr || *line_ptr))
9946
0
    return true;
9947
9948
24.8k
  if (symbols == NULL)
9949
7.52k
    return false;
9950
9951
17.3k
  if (! _bfd_elf_find_function (abfd, symbols, section, offset,
9952
17.3k
        filename_ptr, functionname_ptr))
9953
13.9k
    return false;
9954
9955
3.37k
  *line_ptr = 0;
9956
3.37k
  return true;
9957
17.3k
}
9958
9959
/* Find the line for a symbol.  */
9960
9961
bool
9962
_bfd_elf_find_line (bfd *abfd, asymbol **symbols, asymbol *symbol,
9963
        const char **filename_ptr, unsigned int *line_ptr)
9964
5.79k
{
9965
5.79k
  struct elf_obj_tdata *tdata = elf_tdata (abfd);
9966
5.79k
  return _bfd_dwarf2_find_nearest_line (abfd, symbols, symbol, NULL, 0,
9967
5.79k
          filename_ptr, NULL, line_ptr, NULL,
9968
5.79k
          dwarf_debug_sections,
9969
5.79k
          &tdata->dwarf2_find_line_info);
9970
5.79k
}
9971
9972
/* After a call to bfd_find_nearest_line, successive calls to
9973
   bfd_find_inliner_info can be used to get source information about
9974
   each level of function inlining that terminated at the address
9975
   passed to bfd_find_nearest_line.  Currently this is only supported
9976
   for DWARF2 with appropriate DWARF3 extensions. */
9977
9978
bool
9979
_bfd_elf_find_inliner_info (bfd *abfd,
9980
          const char **filename_ptr,
9981
          const char **functionname_ptr,
9982
          unsigned int *line_ptr)
9983
0
{
9984
0
  struct elf_obj_tdata *tdata = elf_tdata (abfd);
9985
0
  return _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
9986
0
          functionname_ptr, line_ptr,
9987
0
          &tdata->dwarf2_find_line_info);
9988
0
}
9989
9990
int
9991
_bfd_elf_sizeof_headers (bfd *abfd, struct bfd_link_info *info)
9992
0
{
9993
0
  elf_backend_data *bed = get_elf_backend_data (abfd);
9994
0
  int ret = bed->s->sizeof_ehdr;
9995
9996
0
  if (!bfd_link_relocatable (info))
9997
0
    {
9998
0
      bfd_size_type phdr_size = elf_program_header_size (abfd);
9999
10000
0
      if (phdr_size == (bfd_size_type) -1)
10001
0
  {
10002
0
    struct elf_segment_map *m;
10003
10004
0
    phdr_size = 0;
10005
0
    for (m = elf_seg_map (abfd); m != NULL; m = m->next)
10006
0
      phdr_size += bed->s->sizeof_phdr;
10007
10008
0
    if (phdr_size == 0)
10009
0
      phdr_size = get_program_header_size (abfd, info);
10010
0
  }
10011
10012
0
      elf_program_header_size (abfd) = phdr_size;
10013
0
      ret += phdr_size;
10014
0
    }
10015
10016
0
  return ret;
10017
0
}
10018
10019
bool
10020
_bfd_elf_set_section_contents (bfd *abfd,
10021
             sec_ptr section,
10022
             const void *location,
10023
             file_ptr offset,
10024
             bfd_size_type count)
10025
1.20k
{
10026
1.20k
  Elf_Internal_Shdr *hdr;
10027
10028
1.20k
  if (! abfd->output_has_begun
10029
175
      && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
10030
26
    return false;
10031
10032
1.17k
  if (!count)
10033
0
    return true;
10034
10035
1.17k
  hdr = &elf_section_data (section)->this_hdr;
10036
1.17k
  if (hdr->sh_offset == (file_ptr) -1)
10037
0
    {
10038
0
      unsigned char *contents;
10039
10040
0
      if (bfd_section_is_ctf (section))
10041
  /* Nothing to do with this section: the contents are generated
10042
     later.  */
10043
0
  return true;
10044
10045
0
      if ((offset + count) > hdr->sh_size)
10046
0
  {
10047
0
    _bfd_error_handler
10048
0
      (_("%pB:%pA: error: attempting to write"
10049
0
         " over the end of the section"),
10050
0
       abfd, section);
10051
10052
0
    bfd_set_error (bfd_error_invalid_operation);
10053
0
    return false;
10054
0
  }
10055
10056
0
      contents = hdr->contents;
10057
0
      if (contents == NULL)
10058
0
  {
10059
0
    _bfd_error_handler
10060
0
      (_("%pB:%pA: error: attempting to write"
10061
0
         " section into an empty buffer"),
10062
0
       abfd, section);
10063
10064
0
    bfd_set_error (bfd_error_invalid_operation);
10065
0
    return false;
10066
0
  }
10067
10068
0
      memcpy (contents + offset, location, count);
10069
0
      return true;
10070
0
    }
10071
10072
1.17k
  return _bfd_generic_set_section_contents (abfd, section,
10073
1.17k
              location, offset, count);
10074
1.17k
}
10075
10076
bool
10077
_bfd_elf_no_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED,
10078
         arelent *cache_ptr ATTRIBUTE_UNUSED,
10079
         Elf_Internal_Rela *dst ATTRIBUTE_UNUSED)
10080
0
{
10081
0
  abort ();
10082
0
  return false;
10083
0
}
10084
10085
/* Try to convert a non-ELF reloc into an ELF one.  */
10086
10087
bool
10088
_bfd_elf_validate_reloc (bfd *abfd, arelent *areloc)
10089
0
{
10090
  /* Check whether we really have an ELF howto.  */
10091
10092
0
  if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec)
10093
0
    {
10094
0
      bfd_reloc_code_real_type code;
10095
0
      reloc_howto_type *howto;
10096
10097
      /* Alien reloc: Try to determine its type to replace it with an
10098
   equivalent ELF reloc.  */
10099
10100
0
      if (areloc->howto->pc_relative)
10101
0
  {
10102
0
    switch (areloc->howto->bitsize)
10103
0
      {
10104
0
      case 8:
10105
0
        code = BFD_RELOC_8_PCREL;
10106
0
        break;
10107
0
      case 12:
10108
0
        code = BFD_RELOC_12_PCREL;
10109
0
        break;
10110
0
      case 16:
10111
0
        code = BFD_RELOC_16_PCREL;
10112
0
        break;
10113
0
      case 24:
10114
0
        code = BFD_RELOC_24_PCREL;
10115
0
        break;
10116
0
      case 32:
10117
0
        code = BFD_RELOC_32_PCREL;
10118
0
        break;
10119
0
      case 64:
10120
0
        code = BFD_RELOC_64_PCREL;
10121
0
        break;
10122
0
      default:
10123
0
        goto fail;
10124
0
      }
10125
10126
0
    howto = bfd_reloc_type_lookup (abfd, code);
10127
10128
0
    if (howto && areloc->howto->pcrel_offset != howto->pcrel_offset)
10129
0
      {
10130
0
        if (howto->pcrel_offset)
10131
0
    areloc->addend += areloc->address;
10132
0
        else
10133
0
    areloc->addend -= areloc->address; /* addend is unsigned!! */
10134
0
      }
10135
0
  }
10136
0
      else
10137
0
  {
10138
0
    switch (areloc->howto->bitsize)
10139
0
      {
10140
0
      case 8:
10141
0
        code = BFD_RELOC_8;
10142
0
        break;
10143
0
      case 14:
10144
0
        code = BFD_RELOC_14;
10145
0
        break;
10146
0
      case 16:
10147
0
        code = BFD_RELOC_16;
10148
0
        break;
10149
0
      case 26:
10150
0
        code = BFD_RELOC_26;
10151
0
        break;
10152
0
      case 32:
10153
0
        code = BFD_RELOC_32;
10154
0
        break;
10155
0
      case 64:
10156
0
        code = BFD_RELOC_64;
10157
0
        break;
10158
0
      default:
10159
0
        goto fail;
10160
0
      }
10161
10162
0
    howto = bfd_reloc_type_lookup (abfd, code);
10163
0
  }
10164
10165
0
      if (howto)
10166
0
  areloc->howto = howto;
10167
0
      else
10168
0
  goto fail;
10169
0
    }
10170
10171
0
  return true;
10172
10173
0
 fail:
10174
  /* xgettext:c-format */
10175
0
  _bfd_error_handler (_("%pB: %s unsupported"),
10176
0
          abfd, areloc->howto->name);
10177
0
  bfd_set_error (bfd_error_sorry);
10178
0
  return false;
10179
0
}
10180
10181
bool
10182
_bfd_elf_free_cached_info (bfd *abfd)
10183
8.35M
{
10184
8.35M
  struct elf_obj_tdata *tdata;
10185
10186
8.35M
  if ((bfd_get_format (abfd) == bfd_object
10187
8.32M
       || bfd_get_format (abfd) == bfd_core)
10188
45.1k
      && (tdata = elf_tdata (abfd)) != NULL)
10189
45.1k
    {
10190
45.1k
      if (tdata->o != NULL && elf_shstrtab (abfd) != NULL)
10191
574
  _bfd_elf_strtab_free (elf_shstrtab (abfd));
10192
45.1k
      _bfd_dwarf2_cleanup_debug_info (abfd, &tdata->dwarf2_find_line_info);
10193
45.1k
      _bfd_dwarf1_cleanup_debug_info (abfd, &tdata->dwarf1_find_line_info);
10194
45.1k
      _bfd_stab_cleanup (abfd, &tdata->line_info);
10195
45.1k
      _bfd_elf_cleanup_object_attributes (abfd);
10196
560k
      for (asection *sec = abfd->sections; sec != NULL; sec = sec->next)
10197
514k
  {
10198
514k
    _bfd_elf_munmap_section_contents (sec, sec->contents);
10199
514k
    if (!sec->alloced)
10200
514k
      {
10201
514k
        free (elf_section_data (sec)->this_hdr.contents);
10202
514k
        elf_section_data (sec)->this_hdr.contents = NULL;
10203
514k
      }
10204
514k
    free (elf_section_data (sec)->relocs);
10205
514k
    elf_section_data (sec)->relocs = NULL;
10206
514k
    if (sec->sec_info_type == SEC_INFO_TYPE_EH_FRAME)
10207
0
      {
10208
0
        struct eh_frame_sec_info *sec_info = sec->sec_info;
10209
0
        free (sec_info->cies);
10210
0
      }
10211
514k
    if (sec->sec_info_type == SEC_INFO_TYPE_SFRAME)
10212
0
      {
10213
0
        struct sframe_dec_info *sfd_info = sec->sec_info;
10214
0
        sframe_decoder_free (&sfd_info->sfd_ctx);
10215
0
      }
10216
514k
  }
10217
45.1k
      free (tdata->symtab_hdr.contents);
10218
45.1k
      tdata->symtab_hdr.contents = NULL;
10219
45.1k
    }
10220
10221
8.35M
  return _bfd_generic_bfd_free_cached_info (abfd);
10222
8.35M
}
10223
10224
/* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
10225
   in the relocation's offset.  Thus we cannot allow any sort of sanity
10226
   range-checking to interfere.  There is nothing else to do in processing
10227
   this reloc.  */
10228
10229
bfd_reloc_status_type
10230
_bfd_elf_rel_vtable_reloc_fn
10231
  (bfd *abfd ATTRIBUTE_UNUSED, arelent *re ATTRIBUTE_UNUSED,
10232
   struct bfd_symbol *symbol ATTRIBUTE_UNUSED,
10233
   void *data ATTRIBUTE_UNUSED, asection *is ATTRIBUTE_UNUSED,
10234
   bfd *obfd ATTRIBUTE_UNUSED, char **errmsg ATTRIBUTE_UNUSED)
10235
41
{
10236
41
  return bfd_reloc_ok;
10237
41
}
10238

10239
/* Elf core file support.  Much of this only works on native
10240
   toolchains, since we rely on knowing the
10241
   machine-dependent procfs structure in order to pick
10242
   out details about the corefile.  */
10243
10244
#ifdef HAVE_SYS_PROCFS_H
10245
# include <sys/procfs.h>
10246
#endif
10247
10248
/* Return a PID that identifies a "thread" for threaded cores, or the
10249
   PID of the main process for non-threaded cores.  */
10250
10251
static int
10252
elfcore_make_pid (bfd *abfd)
10253
175
{
10254
175
  int pid;
10255
10256
175
  pid = elf_tdata (abfd)->core->lwpid;
10257
175
  if (pid == 0)
10258
174
    pid = elf_tdata (abfd)->core->pid;
10259
10260
175
  return pid;
10261
175
}
10262
10263
/* If there isn't a section called NAME, make one, using data from
10264
   SECT.  Note, this function will generate a reference to NAME, so
10265
   you shouldn't deallocate or overwrite it.  */
10266
10267
static bool
10268
elfcore_maybe_make_sect (bfd *abfd, char *name, asection *sect)
10269
175
{
10270
175
  asection *sect2;
10271
10272
175
  if (bfd_get_section_by_name (abfd, name) != NULL)
10273
102
    return true;
10274
10275
73
  sect2 = bfd_make_section_with_flags (abfd, name, sect->flags);
10276
73
  if (sect2 == NULL)
10277
0
    return false;
10278
10279
73
  sect2->size = sect->size;
10280
73
  sect2->filepos = sect->filepos;
10281
73
  sect2->alignment_power = sect->alignment_power;
10282
73
  return true;
10283
73
}
10284
10285
/* Create a pseudosection containing SIZE bytes at FILEPOS.  This
10286
   actually creates up to two pseudosections:
10287
   - For the single-threaded case, a section named NAME, unless
10288
     such a section already exists.
10289
   - For the multi-threaded case, a section named "NAME/PID", where
10290
     PID is elfcore_make_pid (abfd).
10291
   Both pseudosections have identical contents.  */
10292
bool
10293
_bfd_elfcore_make_pseudosection (bfd *abfd,
10294
         char *name,
10295
         size_t size,
10296
         ufile_ptr filepos)
10297
175
{
10298
175
  char buf[100];
10299
175
  char *threaded_name;
10300
175
  size_t len;
10301
175
  asection *sect;
10302
10303
  /* Build the section name.  */
10304
10305
175
  sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd));
10306
175
  len = strlen (buf) + 1;
10307
175
  threaded_name = bfd_alloc (abfd, len);
10308
175
  if (threaded_name == NULL)
10309
0
    return false;
10310
175
  memcpy (threaded_name, buf, len);
10311
10312
175
  sect = bfd_make_section_anyway_with_flags (abfd, threaded_name,
10313
175
               SEC_HAS_CONTENTS);
10314
175
  if (sect == NULL)
10315
0
    return false;
10316
175
  sect->size = size;
10317
175
  sect->filepos = filepos;
10318
175
  sect->alignment_power = 2;
10319
10320
175
  return elfcore_maybe_make_sect (abfd, name, sect);
10321
175
}
10322
10323
static bool
10324
elfcore_make_auxv_note_section (bfd *abfd, Elf_Internal_Note *note,
10325
        size_t offs)
10326
33
{
10327
33
  asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv",
10328
33
                   SEC_HAS_CONTENTS);
10329
10330
33
  if (sect == NULL)
10331
0
    return false;
10332
10333
33
  sect->size = note->descsz - offs;
10334
33
  sect->filepos = note->descpos + offs;
10335
33
  sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
10336
10337
33
  return true;
10338
33
}
10339
10340
/* prstatus_t exists on:
10341
     solaris 2.5+
10342
     linux 2.[01] + glibc
10343
     unixware 4.2
10344
*/
10345
10346
#if defined (HAVE_PRSTATUS_T)
10347
10348
static bool
10349
elfcore_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
10350
422
{
10351
422
  size_t size;
10352
422
  int offset;
10353
10354
422
  if (note->descsz == sizeof (prstatus_t))
10355
0
    {
10356
0
      prstatus_t prstat;
10357
10358
0
      size = sizeof (prstat.pr_reg);
10359
0
      offset   = offsetof (prstatus_t, pr_reg);
10360
0
      memcpy (&prstat, note->descdata, sizeof (prstat));
10361
10362
      /* Do not overwrite the core signal if it
10363
   has already been set by another thread.  */
10364
0
      if (elf_tdata (abfd)->core->signal == 0)
10365
0
  elf_tdata (abfd)->core->signal = prstat.pr_cursig;
10366
0
      if (elf_tdata (abfd)->core->pid == 0)
10367
0
  elf_tdata (abfd)->core->pid = prstat.pr_pid;
10368
10369
      /* pr_who exists on:
10370
   solaris 2.5+
10371
   unixware 4.2
10372
   pr_who doesn't exist on:
10373
   linux 2.[01]
10374
   */
10375
#if defined (HAVE_PRSTATUS_T_PR_WHO)
10376
      elf_tdata (abfd)->core->lwpid = prstat.pr_who;
10377
#else
10378
0
      elf_tdata (abfd)->core->lwpid = prstat.pr_pid;
10379
0
#endif
10380
0
    }
10381
422
#if defined (HAVE_PRSTATUS32_T)
10382
422
  else if (note->descsz == sizeof (prstatus32_t))
10383
1
    {
10384
      /* 64-bit host, 32-bit corefile */
10385
1
      prstatus32_t prstat;
10386
10387
1
      size = sizeof (prstat.pr_reg);
10388
1
      offset   = offsetof (prstatus32_t, pr_reg);
10389
1
      memcpy (&prstat, note->descdata, sizeof (prstat));
10390
10391
      /* Do not overwrite the core signal if it
10392
   has already been set by another thread.  */
10393
1
      if (elf_tdata (abfd)->core->signal == 0)
10394
1
  elf_tdata (abfd)->core->signal = prstat.pr_cursig;
10395
1
      if (elf_tdata (abfd)->core->pid == 0)
10396
1
  elf_tdata (abfd)->core->pid = prstat.pr_pid;
10397
10398
      /* pr_who exists on:
10399
   solaris 2.5+
10400
   unixware 4.2
10401
   pr_who doesn't exist on:
10402
   linux 2.[01]
10403
   */
10404
#if defined (HAVE_PRSTATUS32_T_PR_WHO)
10405
      elf_tdata (abfd)->core->lwpid = prstat.pr_who;
10406
#else
10407
1
      elf_tdata (abfd)->core->lwpid = prstat.pr_pid;
10408
1
#endif
10409
1
    }
10410
421
#endif /* HAVE_PRSTATUS32_T */
10411
421
  else
10412
421
    {
10413
      /* Fail - we don't know how to handle any other
10414
   note size (ie. data object type).  */
10415
421
      return true;
10416
421
    }
10417
10418
  /* Make a ".reg/999" section and a ".reg" section.  */
10419
1
  return _bfd_elfcore_make_pseudosection (abfd, NOTE_PSEUDO_SECTION_REG,
10420
1
            size, note->descpos + offset);
10421
422
}
10422
#endif /* defined (HAVE_PRSTATUS_T) */
10423
10424
/* Create a pseudosection containing the exact contents of NOTE.  */
10425
static bool
10426
elfcore_make_note_pseudosection (bfd *abfd,
10427
         char *name,
10428
         Elf_Internal_Note *note)
10429
174
{
10430
174
  return _bfd_elfcore_make_pseudosection (abfd, name,
10431
174
            note->descsz, note->descpos);
10432
174
}
10433
10434
/* There isn't a consistent prfpregset_t across platforms,
10435
   but it doesn't matter, because we don't have to pick this
10436
   data structure apart.  */
10437
10438
static bool
10439
elfcore_grok_prfpreg (bfd *abfd, Elf_Internal_Note *note)
10440
174
{
10441
174
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_REG2, note);
10442
174
}
10443
10444
/* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
10445
   type of NT_PRXFPREG.  Just include the whole note's contents
10446
   literally.  */
10447
10448
static bool
10449
elfcore_grok_prxfpreg (bfd *abfd, Elf_Internal_Note *note)
10450
0
{
10451
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_XFP, note);
10452
0
}
10453
10454
/* Linux dumps the Intel XSAVE extended state in a note named "LINUX"
10455
   with a note type of NT_X86_XSTATE.  Just include the whole note's
10456
   contents literally.  */
10457
10458
static bool
10459
elfcore_grok_xstatereg (bfd *abfd, Elf_Internal_Note *note)
10460
0
{
10461
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_XSTATE, note);
10462
0
}
10463
10464
static bool
10465
elfcore_grok_sspreg (bfd *abfd, Elf_Internal_Note *note)
10466
0
{
10467
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_SSP, note);
10468
0
}
10469
10470
/* Linux dumps the XSAVE Layout description in a note named "LINUX"
10471
   with a note type of NT_X86_XSAVE_LAYOUT. */
10472
static bool
10473
elfcore_grok_xsave_layout_desc (bfd *abfd, Elf_Internal_Note *note)
10474
0
{
10475
0
  return elfcore_make_note_pseudosection (abfd,
10476
0
            NOTE_PSEUDO_SECTION_XSAVE_LAYOUT,
10477
0
            note);
10478
0
}
10479
10480
static bool
10481
elfcore_grok_ppc_vmx (bfd *abfd, Elf_Internal_Note *note)
10482
0
{
10483
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_PPC_VMX, note);
10484
0
}
10485
10486
static bool
10487
elfcore_grok_ppc_vsx (bfd *abfd, Elf_Internal_Note *note)
10488
0
{
10489
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_PPC_VSX, note);
10490
0
}
10491
10492
static bool
10493
elfcore_grok_ppc_tar (bfd *abfd, Elf_Internal_Note *note)
10494
0
{
10495
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_PPC_TAR, note);
10496
0
}
10497
10498
static bool
10499
elfcore_grok_ppc_ppr (bfd *abfd, Elf_Internal_Note *note)
10500
0
{
10501
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_PPC_PPR, note);
10502
0
}
10503
10504
static bool
10505
elfcore_grok_ppc_dscr (bfd *abfd, Elf_Internal_Note *note)
10506
0
{
10507
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_PPC_DSCR, note);
10508
0
}
10509
10510
static bool
10511
elfcore_grok_ppc_ebb (bfd *abfd, Elf_Internal_Note *note)
10512
0
{
10513
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_PPC_EBB, note);
10514
0
}
10515
10516
static bool
10517
elfcore_grok_ppc_pmu (bfd *abfd, Elf_Internal_Note *note)
10518
0
{
10519
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_PPC_PMU, note);
10520
0
}
10521
10522
static bool
10523
elfcore_grok_ppc_tm_cgpr (bfd *abfd, Elf_Internal_Note *note)
10524
0
{
10525
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_PPC_TM_CGPR, note);
10526
0
}
10527
10528
static bool
10529
elfcore_grok_ppc_tm_cfpr (bfd *abfd, Elf_Internal_Note *note)
10530
0
{
10531
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_PPC_TM_CFPR, note);
10532
0
}
10533
10534
static bool
10535
elfcore_grok_ppc_tm_cvmx (bfd *abfd, Elf_Internal_Note *note)
10536
0
{
10537
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_PPC_TM_CVMX, note);
10538
0
}
10539
10540
static bool
10541
elfcore_grok_ppc_tm_cvsx (bfd *abfd, Elf_Internal_Note *note)
10542
0
{
10543
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_PPC_TM_CVSX, note);
10544
0
}
10545
10546
static bool
10547
elfcore_grok_ppc_tm_spr (bfd *abfd, Elf_Internal_Note *note)
10548
0
{
10549
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_PPC_TM_SPR, note);
10550
0
}
10551
10552
static bool
10553
elfcore_grok_ppc_tm_ctar (bfd *abfd, Elf_Internal_Note *note)
10554
0
{
10555
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_PPC_TM_CTAR, note);
10556
0
}
10557
10558
static bool
10559
elfcore_grok_ppc_tm_cppr (bfd *abfd, Elf_Internal_Note *note)
10560
0
{
10561
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_PPC_TM_CPPR, note);
10562
0
}
10563
10564
static bool
10565
elfcore_grok_ppc_tm_cdscr (bfd *abfd, Elf_Internal_Note *note)
10566
0
{
10567
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_PPC_TM_CDSCR, note);
10568
0
}
10569
10570
static bool
10571
elfcore_grok_s390_high_gprs (bfd *abfd, Elf_Internal_Note *note)
10572
0
{
10573
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_S390_HIGH_GPRS, note);
10574
0
}
10575
10576
static bool
10577
elfcore_grok_s390_timer (bfd *abfd, Elf_Internal_Note *note)
10578
0
{
10579
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_S390_TIMER, note);
10580
0
}
10581
10582
static bool
10583
elfcore_grok_s390_todcmp (bfd *abfd, Elf_Internal_Note *note)
10584
0
{
10585
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_S390_TODCMP, note);
10586
0
}
10587
10588
static bool
10589
elfcore_grok_s390_todpreg (bfd *abfd, Elf_Internal_Note *note)
10590
0
{
10591
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_S390_TODPREG, note);
10592
0
}
10593
10594
static bool
10595
elfcore_grok_s390_ctrs (bfd *abfd, Elf_Internal_Note *note)
10596
0
{
10597
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_S390_CTRS, note);
10598
0
}
10599
10600
static bool
10601
elfcore_grok_s390_prefix (bfd *abfd, Elf_Internal_Note *note)
10602
0
{
10603
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_S390_PREFIX, note);
10604
0
}
10605
10606
static bool
10607
elfcore_grok_s390_last_break (bfd *abfd, Elf_Internal_Note *note)
10608
0
{
10609
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_S390_LAST_BREAK, note);
10610
0
}
10611
10612
static bool
10613
elfcore_grok_s390_system_call (bfd *abfd, Elf_Internal_Note *note)
10614
0
{
10615
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_S390_SYSTEM_CALL, note);
10616
0
}
10617
10618
static bool
10619
elfcore_grok_s390_tdb (bfd *abfd, Elf_Internal_Note *note)
10620
0
{
10621
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_S390_TDB, note);
10622
0
}
10623
10624
static bool
10625
elfcore_grok_s390_vxrs_low (bfd *abfd, Elf_Internal_Note *note)
10626
0
{
10627
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_S390_VXRS_LOW, note);
10628
0
}
10629
10630
static bool
10631
elfcore_grok_s390_vxrs_high (bfd *abfd, Elf_Internal_Note *note)
10632
0
{
10633
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_S390_VXRS_HIGH, note);
10634
0
}
10635
10636
static bool
10637
elfcore_grok_s390_gs_cb (bfd *abfd, Elf_Internal_Note *note)
10638
0
{
10639
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_S390_GS_CB, note);
10640
0
}
10641
10642
static bool
10643
elfcore_grok_s390_gs_bc (bfd *abfd, Elf_Internal_Note *note)
10644
0
{
10645
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_S390_GS_BC, note);
10646
0
}
10647
10648
static bool
10649
elfcore_grok_arm_vfp (bfd *abfd, Elf_Internal_Note *note)
10650
0
{
10651
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_ARM_VFP, note);
10652
0
}
10653
10654
static bool
10655
elfcore_grok_aarch_tls (bfd *abfd, Elf_Internal_Note *note)
10656
0
{
10657
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_AARCH_TLS, note);
10658
0
}
10659
10660
static bool
10661
elfcore_grok_aarch_hw_break (bfd *abfd, Elf_Internal_Note *note)
10662
0
{
10663
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_AARCH_HW_BREAK, note);
10664
0
}
10665
10666
static bool
10667
elfcore_grok_aarch_hw_watch (bfd *abfd, Elf_Internal_Note *note)
10668
0
{
10669
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_AARCH_HW_WATCH, note);
10670
0
}
10671
10672
static bool
10673
elfcore_grok_aarch_sve (bfd *abfd, Elf_Internal_Note *note)
10674
0
{
10675
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_AARCH_SVE, note);
10676
0
}
10677
10678
static bool
10679
elfcore_grok_aarch_pauth (bfd *abfd, Elf_Internal_Note *note)
10680
0
{
10681
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_AARCH_PAUTH, note);
10682
0
}
10683
10684
static bool
10685
elfcore_grok_aarch_mte (bfd *abfd, Elf_Internal_Note *note)
10686
0
{
10687
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_AARCH_MTE, note);
10688
0
}
10689
10690
static bool
10691
elfcore_grok_aarch_ssve (bfd *abfd, Elf_Internal_Note *note)
10692
0
{
10693
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_AARCH_SSVE, note);
10694
0
}
10695
10696
static bool
10697
elfcore_grok_aarch_za (bfd *abfd, Elf_Internal_Note *note)
10698
0
{
10699
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_AARCH_ZA, note);
10700
0
}
10701
10702
/* Convert NOTE into a bfd_section called ".reg-aarch-zt".  Return TRUE if
10703
   successful, otherwise return FALSE.  */
10704
10705
static bool
10706
elfcore_grok_aarch_zt (bfd *abfd, Elf_Internal_Note *note)
10707
0
{
10708
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_AARCH_ZT, note);
10709
0
}
10710
10711
/* Convert NOTE into a bfd_section called ".reg-aarch-gcs".  Return TRUE if
10712
   successful, otherwise return FALSE.  */
10713
10714
static bool
10715
elfcore_grok_aarch_gcs (bfd *abfd, Elf_Internal_Note *note)
10716
0
{
10717
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_AARCH_GCS, note);
10718
0
}
10719
10720
/* Convert NOTE into a bfd_section called ".reg-aarch-poe".  Return TRUE if
10721
   successful, otherwise return FALSE.  */
10722
10723
static bool
10724
elfcore_grok_aarch_poe (bfd *abfd, Elf_Internal_Note *note)
10725
0
{
10726
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_AARCH_POE, note);
10727
0
}
10728
10729
/* Convert NOTE into the appropriate note pseudo-section for the AArch64 FPMR.
10730
 * Return TRUE if successful, otherwise return FALSE.  */
10731
10732
static bool
10733
elfcore_grok_aarch_fpmr (bfd *abfd, Elf_Internal_Note *note)
10734
0
{
10735
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_AARCH_FPMR, note);
10736
0
}
10737
10738
static bool
10739
elfcore_grok_arc_v2 (bfd *abfd, Elf_Internal_Note *note)
10740
0
{
10741
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_ARC_V2, note);
10742
0
}
10743
10744
/* Convert NOTE into a bfd_section called ".reg-riscv-csr".  Return TRUE if
10745
   successful otherwise, return FALSE.  */
10746
10747
static bool
10748
elfcore_grok_riscv_csr (bfd *abfd, Elf_Internal_Note *note)
10749
0
{
10750
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_RISCV_CSR, note);
10751
0
}
10752
10753
/* Convert NOTE into a bfd_section called ".gdb-tdesc".  Return TRUE if
10754
   successful otherwise, return FALSE.  */
10755
10756
static bool
10757
elfcore_grok_gdb_tdesc (bfd *abfd, Elf_Internal_Note *note)
10758
0
{
10759
0
  return elfcore_make_note_pseudosection (abfd, ".gdb-tdesc", note);
10760
0
}
10761
10762
static bool
10763
elfcore_grok_i386_tls (bfd *abfd, Elf_Internal_Note *note)
10764
0
{
10765
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_I386_TLS,
10766
0
            note);
10767
0
}
10768
10769
static bool
10770
elfcore_grok_loongarch_cpucfg (bfd *abfd, Elf_Internal_Note *note)
10771
0
{
10772
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_LOONGARCH_CPUCFG, note);
10773
0
}
10774
10775
static bool
10776
elfcore_grok_loongarch_lbt (bfd *abfd, Elf_Internal_Note *note)
10777
0
{
10778
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_LOONGARCH_LBT, note);
10779
0
}
10780
10781
static bool
10782
elfcore_grok_loongarch_lsx (bfd *abfd, Elf_Internal_Note *note)
10783
0
{
10784
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_LOONGARCH_LSX, note);
10785
0
}
10786
10787
static bool
10788
elfcore_grok_loongarch_lasx (bfd *abfd, Elf_Internal_Note *note)
10789
0
{
10790
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_LOONGARCH_LASX, note);
10791
0
}
10792
10793
#if defined (HAVE_PRPSINFO_T)
10794
typedef prpsinfo_t   elfcore_psinfo_t;
10795
#if defined (HAVE_PRPSINFO32_T)   /* Sparc64 cross Sparc32 */
10796
typedef prpsinfo32_t elfcore_psinfo32_t;
10797
#endif
10798
#endif
10799
10800
#if defined (HAVE_PSINFO_T)
10801
typedef psinfo_t   elfcore_psinfo_t;
10802
#if defined (HAVE_PSINFO32_T)   /* Sparc64 cross Sparc32 */
10803
typedef psinfo32_t elfcore_psinfo32_t;
10804
#endif
10805
#endif
10806
10807
/* return a malloc'ed copy of a string at START which is at
10808
   most MAX bytes long, possibly without a terminating '\0'.
10809
   the copy will always have a terminating '\0'.  */
10810
10811
char *
10812
_bfd_elfcore_strndup (bfd *abfd, char *start, size_t max)
10813
30
{
10814
30
  char *dups;
10815
30
  char *end = (char *) memchr (start, '\0', max);
10816
30
  size_t len;
10817
10818
30
  if (end == NULL)
10819
5
    len = max;
10820
25
  else
10821
25
    len = end - start;
10822
10823
30
  dups = bfd_alloc (abfd, len + 1);
10824
30
  if (dups == NULL)
10825
0
    return NULL;
10826
10827
30
  memcpy (dups, start, len);
10828
30
  dups[len] = '\0';
10829
10830
30
  return dups;
10831
30
}
10832
10833
#if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
10834
static bool
10835
elfcore_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
10836
250
{
10837
250
  if (note->descsz == sizeof (elfcore_psinfo_t))
10838
0
    {
10839
0
      elfcore_psinfo_t psinfo;
10840
10841
0
      memcpy (&psinfo, note->descdata, sizeof (psinfo));
10842
10843
0
#if defined (HAVE_PSINFO_T_PR_PID) || defined (HAVE_PRPSINFO_T_PR_PID)
10844
0
      elf_tdata (abfd)->core->pid = psinfo.pr_pid;
10845
0
#endif
10846
0
      elf_tdata (abfd)->core->program
10847
0
  = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
10848
0
        sizeof (psinfo.pr_fname));
10849
10850
0
      elf_tdata (abfd)->core->command
10851
0
  = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
10852
0
        sizeof (psinfo.pr_psargs));
10853
0
    }
10854
250
#if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
10855
250
  else if (note->descsz == sizeof (elfcore_psinfo32_t))
10856
15
    {
10857
      /* 64-bit host, 32-bit corefile */
10858
15
      elfcore_psinfo32_t psinfo;
10859
10860
15
      memcpy (&psinfo, note->descdata, sizeof (psinfo));
10861
10862
15
#if defined (HAVE_PSINFO32_T_PR_PID) || defined (HAVE_PRPSINFO32_T_PR_PID)
10863
15
      elf_tdata (abfd)->core->pid = psinfo.pr_pid;
10864
15
#endif
10865
15
      elf_tdata (abfd)->core->program
10866
15
  = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
10867
15
        sizeof (psinfo.pr_fname));
10868
10869
15
      elf_tdata (abfd)->core->command
10870
15
  = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
10871
15
        sizeof (psinfo.pr_psargs));
10872
15
    }
10873
235
#endif
10874
10875
235
  else
10876
235
    {
10877
      /* Fail - we don't know how to handle any other
10878
   note size (ie. data object type).  */
10879
235
      return true;
10880
235
    }
10881
10882
  /* Note that for some reason, a spurious space is tacked
10883
     onto the end of the args in some (at least one anyway)
10884
     implementations, so strip it off if it exists.  */
10885
10886
15
  {
10887
15
    char *command = elf_tdata (abfd)->core->command;
10888
15
    int n = strlen (command);
10889
10890
15
    if (0 < n && command[n - 1] == ' ')
10891
4
      command[n - 1] = '\0';
10892
15
  }
10893
10894
15
  return true;
10895
250
}
10896
#endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
10897
10898
#if defined (HAVE_PSTATUS_T)
10899
static bool
10900
elfcore_grok_pstatus (bfd *abfd, Elf_Internal_Note *note)
10901
{
10902
  if (note->descsz == sizeof (pstatus_t)
10903
#if defined (HAVE_PXSTATUS_T)
10904
      || note->descsz == sizeof (pxstatus_t)
10905
#endif
10906
      )
10907
    {
10908
      pstatus_t pstat;
10909
10910
      memcpy (&pstat, note->descdata, sizeof (pstat));
10911
10912
      elf_tdata (abfd)->core->pid = pstat.pr_pid;
10913
    }
10914
#if defined (HAVE_PSTATUS32_T)
10915
  else if (note->descsz == sizeof (pstatus32_t))
10916
    {
10917
      /* 64-bit host, 32-bit corefile */
10918
      pstatus32_t pstat;
10919
10920
      memcpy (&pstat, note->descdata, sizeof (pstat));
10921
10922
      elf_tdata (abfd)->core->pid = pstat.pr_pid;
10923
    }
10924
#endif
10925
  /* Could grab some more details from the "representative"
10926
     lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
10927
     NT_LWPSTATUS note, presumably.  */
10928
10929
  return true;
10930
}
10931
#endif /* defined (HAVE_PSTATUS_T) */
10932
10933
#if defined (HAVE_LWPSTATUS_T)
10934
static bool
10935
elfcore_grok_lwpstatus (bfd *abfd, Elf_Internal_Note *note)
10936
{
10937
  lwpstatus_t lwpstat;
10938
  char buf[100];
10939
  char *name;
10940
  size_t len;
10941
  asection *sect;
10942
10943
  if (note->descsz != sizeof (lwpstat)
10944
#if defined (HAVE_LWPXSTATUS_T)
10945
      && note->descsz != sizeof (lwpxstatus_t)
10946
#endif
10947
      )
10948
    return true;
10949
10950
  memcpy (&lwpstat, note->descdata, sizeof (lwpstat));
10951
10952
  elf_tdata (abfd)->core->lwpid = lwpstat.pr_lwpid;
10953
  /* Do not overwrite the core signal if it has already been set by
10954
     another thread.  */
10955
  if (elf_tdata (abfd)->core->signal == 0)
10956
    elf_tdata (abfd)->core->signal = lwpstat.pr_cursig;
10957
10958
  /* Make a ".reg/999" section.  */
10959
10960
  sprintf (buf, NOTE_PSEUDO_SECTION_REG "/%d", elfcore_make_pid (abfd));
10961
  len = strlen (buf) + 1;
10962
  name = bfd_alloc (abfd, len);
10963
  if (name == NULL)
10964
    return false;
10965
  memcpy (name, buf, len);
10966
10967
  sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
10968
  if (sect == NULL)
10969
    return false;
10970
10971
#if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
10972
  sect->size = sizeof (lwpstat.pr_context.uc_mcontext.gregs);
10973
  sect->filepos = note->descpos
10974
    + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs);
10975
#endif
10976
10977
#if defined (HAVE_LWPSTATUS_T_PR_REG)
10978
  sect->size = sizeof (lwpstat.pr_reg);
10979
  sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg);
10980
#endif
10981
10982
  sect->alignment_power = 2;
10983
10984
  if (!elfcore_maybe_make_sect (abfd, NOTE_PSEUDO_SECTION_REG, sect))
10985
    return false;
10986
10987
  /* Make a ".reg2/999" section */
10988
10989
  sprintf (buf, NOTE_PSEUDO_SECTION_REG2 "/%d", elfcore_make_pid (abfd));
10990
  len = strlen (buf) + 1;
10991
  name = bfd_alloc (abfd, len);
10992
  if (name == NULL)
10993
    return false;
10994
  memcpy (name, buf, len);
10995
10996
  sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
10997
  if (sect == NULL)
10998
    return false;
10999
11000
#if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
11001
  sect->size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs);
11002
  sect->filepos = note->descpos
11003
    + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs);
11004
#endif
11005
11006
#if defined (HAVE_LWPSTATUS_T_PR_FPREG)
11007
  sect->size = sizeof (lwpstat.pr_fpreg);
11008
  sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg);
11009
#endif
11010
11011
  sect->alignment_power = 2;
11012
11013
  return elfcore_maybe_make_sect (abfd, NOTE_PSEUDO_SECTION_REG2, sect);
11014
}
11015
#endif /* defined (HAVE_LWPSTATUS_T) */
11016
11017
/* These constants, and the structure offsets used below, are defined by
11018
   Cygwin's core_dump.h */
11019
0
#define NOTE_INFO_PROCESS  1
11020
0
#define NOTE_INFO_THREAD   2
11021
0
#define NOTE_INFO_MODULE   3
11022
0
#define NOTE_INFO_MODULE64 4
11023
11024
static bool
11025
elfcore_grok_win32pstatus (bfd *abfd, Elf_Internal_Note *note)
11026
49
{
11027
49
  char buf[30];
11028
49
  char *name;
11029
49
  size_t len;
11030
49
  unsigned int name_size;
11031
49
  asection *sect;
11032
49
  unsigned int type;
11033
49
  int is_active_thread;
11034
49
  bfd_vma base_addr;
11035
11036
49
  if (note->descsz < 4)
11037
14
    return true;
11038
11039
35
  if (! startswith (note->namedata, "win32"))
11040
35
    return true;
11041
11042
0
  type = bfd_get_32 (abfd, note->descdata);
11043
11044
0
  static const struct
11045
0
  {
11046
0
    const char *type_name;
11047
0
    unsigned long min_size;
11048
0
  } size_check[] =
11049
0
      {
11050
0
       { "NOTE_INFO_PROCESS", 12 },
11051
0
       { "NOTE_INFO_THREAD", 12 },
11052
0
       { "NOTE_INFO_MODULE", 12 },
11053
0
       { "NOTE_INFO_MODULE64", 16 },
11054
0
      };
11055
11056
0
  if (type == 0 || type > (sizeof(size_check)/sizeof(size_check[0])))
11057
0
      return true;
11058
11059
0
  if (note->descsz < size_check[type - 1].min_size)
11060
0
    {
11061
0
      _bfd_error_handler (_("%pB: warning: win32pstatus %s of size %lu bytes"
11062
0
          " is too small"),
11063
0
        abfd, size_check[type - 1].type_name, note->descsz);
11064
0
      return true;
11065
0
    }
11066
11067
0
  switch (type)
11068
0
    {
11069
0
    case NOTE_INFO_PROCESS:
11070
      /* FIXME: need to add ->core->command.  */
11071
0
      elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, note->descdata + 4);
11072
0
      elf_tdata (abfd)->core->signal = bfd_get_32 (abfd, note->descdata + 8);
11073
0
      break;
11074
11075
0
    case NOTE_INFO_THREAD:
11076
      /* Make a ".reg/<tid>" section containing the Win32 API thread CONTEXT
11077
   structure. */
11078
      /* thread_info.tid */
11079
0
      sprintf (buf, NOTE_PSEUDO_SECTION_REG "/%ld", (long) bfd_get_32 (abfd, note->descdata + 4));
11080
11081
0
      len = strlen (buf) + 1;
11082
0
      name = bfd_alloc (abfd, len);
11083
0
      if (name == NULL)
11084
0
  return false;
11085
11086
0
      memcpy (name, buf, len);
11087
11088
0
      sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
11089
0
      if (sect == NULL)
11090
0
  return false;
11091
11092
      /* sizeof (thread_info.thread_context) */
11093
0
      sect->size = note->descsz - 12;
11094
      /* offsetof (thread_info.thread_context) */
11095
0
      sect->filepos = note->descpos + 12;
11096
0
      sect->alignment_power = 2;
11097
11098
      /* thread_info.is_active_thread */
11099
0
      is_active_thread = bfd_get_32 (abfd, note->descdata + 8);
11100
11101
0
      if (is_active_thread)
11102
0
  if (! elfcore_maybe_make_sect (abfd, NOTE_PSEUDO_SECTION_REG, sect))
11103
0
    return false;
11104
0
      break;
11105
11106
0
    case NOTE_INFO_MODULE:
11107
0
    case NOTE_INFO_MODULE64:
11108
      /* Make a ".module/xxxxxxxx" section.  */
11109
0
      if (type == NOTE_INFO_MODULE)
11110
0
  {
11111
    /* module_info.base_address */
11112
0
    base_addr = bfd_get_32 (abfd, note->descdata + 4);
11113
0
    sprintf (buf, ".module/%08lx", (unsigned long) base_addr);
11114
    /* module_info.module_name_size */
11115
0
    name_size = bfd_get_32 (abfd, note->descdata + 8);
11116
0
  }
11117
0
      else /* NOTE_INFO_MODULE64 */
11118
0
  {
11119
    /* module_info.base_address */
11120
0
    base_addr = bfd_get_64 (abfd, note->descdata + 4);
11121
0
    sprintf (buf, ".module/%016lx", (unsigned long) base_addr);
11122
    /* module_info.module_name_size */
11123
0
    name_size = bfd_get_32 (abfd, note->descdata + 12);
11124
0
  }
11125
11126
0
      len = strlen (buf) + 1;
11127
0
      name = bfd_alloc (abfd, len);
11128
0
      if (name == NULL)
11129
0
  return false;
11130
11131
0
      memcpy (name, buf, len);
11132
11133
0
      sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
11134
11135
0
      if (sect == NULL)
11136
0
  return false;
11137
11138
0
      if (note->descsz < 12 + name_size)
11139
0
  {
11140
0
    _bfd_error_handler (_("%pB: win32pstatus NOTE_INFO_MODULE of size %lu"
11141
0
        " is too small to contain a name of size %u"),
11142
0
            abfd, note->descsz, name_size);
11143
0
    return true;
11144
0
  }
11145
11146
0
      sect->size = note->descsz;
11147
0
      sect->filepos = note->descpos;
11148
0
      sect->alignment_power = 2;
11149
0
      break;
11150
11151
0
    default:
11152
0
      return true;
11153
0
    }
11154
11155
0
  return true;
11156
0
}
11157
11158
static bool
11159
elfcore_grok_note (bfd *abfd, Elf_Internal_Note *note)
11160
4.16k
{
11161
4.16k
  elf_backend_data *bed = get_elf_backend_data (abfd);
11162
11163
  /* Short cut for LINUX notes.  */
11164
4.16k
  if (note->namesz == 6 /* strlen (NOTE_NAME_LINUX) + 1 */
11165
116
      && streq (note->namedata, NOTE_NAME_LINUX))
11166
0
    {
11167
0
      switch (note->type)
11168
0
  {
11169
0
  case NT_386_TLS: return elfcore_grok_i386_tls (abfd, note);
11170
0
  case NT_ARC_V2:   return elfcore_grok_arc_v2 (abfd, note);
11171
0
  case NT_ARM_FPMR: return elfcore_grok_aarch_fpmr (abfd, note);
11172
0
  case NT_ARM_GCS: return elfcore_grok_aarch_gcs (abfd, note);
11173
0
  case NT_ARM_POE: return elfcore_grok_aarch_poe (abfd, note);
11174
0
  case NT_ARM_HW_BREAK: return elfcore_grok_aarch_hw_break (abfd, note);
11175
0
  case NT_ARM_HW_WATCH: return elfcore_grok_aarch_hw_watch (abfd, note);
11176
0
  case NT_ARM_PAC_MASK: return elfcore_grok_aarch_pauth (abfd, note);
11177
0
  case NT_ARM_SSVE: return elfcore_grok_aarch_ssve (abfd, note);
11178
0
  case NT_ARM_SVE: return elfcore_grok_aarch_sve (abfd, note);
11179
0
  case NT_ARM_TAGGED_ADDR_CTRL: return elfcore_grok_aarch_mte (abfd, note);
11180
0
  case NT_ARM_TLS: return elfcore_grok_aarch_tls (abfd, note);
11181
0
  case NT_ARM_VFP: return elfcore_grok_arm_vfp (abfd, note);
11182
0
  case NT_ARM_ZA:   return elfcore_grok_aarch_za (abfd, note);
11183
0
  case NT_ARM_ZT:   return elfcore_grok_aarch_zt (abfd, note);
11184
0
  case NT_LARCH_CPUCFG: return elfcore_grok_loongarch_cpucfg (abfd, note);
11185
0
  case NT_LARCH_LASX: return elfcore_grok_loongarch_lasx (abfd, note);
11186
0
  case NT_LARCH_LBT: return elfcore_grok_loongarch_lbt (abfd, note);
11187
0
  case NT_LARCH_LSX: return elfcore_grok_loongarch_lsx (abfd, note);
11188
0
  case NT_PPC_DSCR: return elfcore_grok_ppc_dscr (abfd, note);
11189
0
  case NT_PPC_EBB: return elfcore_grok_ppc_ebb (abfd, note);
11190
0
  case NT_PPC_PMU: return elfcore_grok_ppc_pmu (abfd, note);
11191
0
  case NT_PPC_PPR: return elfcore_grok_ppc_ppr (abfd, note);
11192
0
  case NT_PPC_TAR: return elfcore_grok_ppc_tar (abfd, note);
11193
0
  case NT_PPC_TM_CDSCR: return elfcore_grok_ppc_tm_cdscr (abfd, note);
11194
0
  case NT_PPC_TM_CFPR: return elfcore_grok_ppc_tm_cfpr (abfd, note);
11195
0
  case NT_PPC_TM_CGPR: return elfcore_grok_ppc_tm_cgpr (abfd, note);
11196
0
  case NT_PPC_TM_CPPR: return elfcore_grok_ppc_tm_cppr (abfd, note);
11197
0
  case NT_PPC_TM_CTAR: return elfcore_grok_ppc_tm_ctar (abfd, note);
11198
0
  case NT_PPC_TM_CVMX: return elfcore_grok_ppc_tm_cvmx (abfd, note);
11199
0
  case NT_PPC_TM_CVSX: return elfcore_grok_ppc_tm_cvsx (abfd, note);
11200
0
  case NT_PPC_TM_SPR: return elfcore_grok_ppc_tm_spr (abfd, note);
11201
0
  case NT_PPC_VMX: return elfcore_grok_ppc_vmx (abfd, note);
11202
0
  case NT_PPC_VSX: return elfcore_grok_ppc_vsx (abfd, note);
11203
0
  case NT_PRXFPREG: return elfcore_grok_prxfpreg (abfd, note);
11204
0
  case NT_S390_CTRS: return elfcore_grok_s390_ctrs (abfd, note);
11205
0
  case NT_S390_GS_BC: return elfcore_grok_s390_gs_bc (abfd, note);
11206
0
  case NT_S390_GS_CB: return elfcore_grok_s390_gs_cb (abfd, note);
11207
0
  case NT_S390_HIGH_GPRS: return elfcore_grok_s390_high_gprs (abfd, note);
11208
0
  case NT_S390_LAST_BREAK:  return elfcore_grok_s390_last_break (abfd, note);
11209
0
  case NT_S390_PREFIX: return elfcore_grok_s390_prefix (abfd, note);
11210
0
  case NT_S390_SYSTEM_CALL: return elfcore_grok_s390_system_call (abfd, note);
11211
0
  case NT_S390_TDB: return elfcore_grok_s390_tdb (abfd, note);
11212
0
  case NT_S390_TIMER: return elfcore_grok_s390_timer (abfd, note);
11213
0
  case NT_S390_TODCMP: return elfcore_grok_s390_todcmp (abfd, note);
11214
0
  case NT_S390_TODPREG: return elfcore_grok_s390_todpreg (abfd, note);
11215
0
  case NT_S390_VXRS_HIGH: return elfcore_grok_s390_vxrs_high (abfd, note);
11216
0
  case NT_S390_VXRS_LOW: return elfcore_grok_s390_vxrs_low (abfd, note);
11217
0
  case NT_X86_SHSTK: return elfcore_grok_sspreg (abfd, note);
11218
0
  case NT_X86_XSTATE: return elfcore_grok_xstatereg (abfd, note);
11219
0
  case NT_X86_XSAVE_LAYOUT: return elfcore_grok_xsave_layout_desc (abfd, note);
11220
0
  default: break;
11221
0
  }
11222
0
    }
11223
  
11224
4.16k
  switch (note->type)
11225
4.16k
    {
11226
3.10k
    default:
11227
3.10k
      return true;
11228
11229
422
    case NT_PRSTATUS:
11230
422
      if (bed->elf_backend_grok_prstatus)
11231
274
  if ((*bed->elf_backend_grok_prstatus) (abfd, note))
11232
0
    return true;
11233
422
#if defined (HAVE_PRSTATUS_T)
11234
422
      return elfcore_grok_prstatus (abfd, note);
11235
#else
11236
      return true;
11237
#endif
11238
11239
#if defined (HAVE_PSTATUS_T)
11240
    case NT_PSTATUS:
11241
      return elfcore_grok_pstatus (abfd, note);
11242
#endif
11243
11244
#if defined (HAVE_LWPSTATUS_T)
11245
    case NT_LWPSTATUS:
11246
      return elfcore_grok_lwpstatus (abfd, note);
11247
#endif
11248
11249
169
    case NT_FPREGSET:   /* FIXME: rename to NT_PRFPREG.  */
11250
169
      return elfcore_grok_prfpreg (abfd, note);
11251
11252
49
    case NT_WIN32PSTATUS:
11253
49
      return elfcore_grok_win32pstatus (abfd, note);
11254
11255
46
    case NT_GDB_TDESC:
11256
46
      if (note->namesz == 4 && streq (note->namedata, NOTE_NAME_GDB))
11257
0
  return elfcore_grok_gdb_tdesc (abfd, note);
11258
46
      else
11259
46
  return true;
11260
11261
83
    case NT_RISCV_CSR:
11262
83
      if (note->namesz == 4 && streq (note->namedata, NOTE_NAME_GDB))
11263
0
  return elfcore_grok_riscv_csr (abfd, note);
11264
83
      else
11265
83
  return true;
11266
11267
218
    case NT_PRPSINFO:
11268
250
    case NT_PSINFO:
11269
250
      if (bed->elf_backend_grok_psinfo)
11270
153
  if ((*bed->elf_backend_grok_psinfo) (abfd, note))
11271
0
    return true;
11272
250
#if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
11273
250
      return elfcore_grok_psinfo (abfd, note);
11274
#else
11275
      return true;
11276
#endif
11277
11278
33
    case NT_AUXV:
11279
33
      return elfcore_make_auxv_note_section (abfd, note, 0);
11280
11281
0
    case NT_FILE:
11282
0
      return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.file",
11283
0
                note);
11284
11285
0
    case NT_SIGINFO:
11286
0
      return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.siginfo",
11287
0
                note);
11288
4.16k
    }
11289
4.16k
}
11290
11291
static bool
11292
elfobj_grok_gnu_build_id (bfd *abfd, Elf_Internal_Note *note)
11293
464
{
11294
464
  struct bfd_build_id* build_id;
11295
11296
464
  if (note->descsz == 0)
11297
18
    return false;
11298
11299
446
  build_id = bfd_alloc (abfd, sizeof (struct bfd_build_id) - 1 + note->descsz);
11300
446
  if (build_id == NULL)
11301
0
    return false;
11302
11303
446
  build_id->size = note->descsz;
11304
446
  memcpy (build_id->data, note->descdata, note->descsz);
11305
446
  abfd->build_id = build_id;
11306
11307
446
  return true;
11308
446
}
11309
11310
static bool
11311
elfobj_grok_gnu_note (bfd *abfd, Elf_Internal_Note *note)
11312
1.54k
{
11313
1.54k
  switch (note->type)
11314
1.54k
    {
11315
474
    default:
11316
474
      return true;
11317
11318
605
    case NT_GNU_PROPERTY_TYPE_0:
11319
605
      return _bfd_elf_parse_gnu_properties (abfd, note);
11320
11321
464
    case NT_GNU_BUILD_ID:
11322
464
      return elfobj_grok_gnu_build_id (abfd, note);
11323
1.54k
    }
11324
1.54k
}
11325
11326
static bool
11327
elfobj_grok_stapsdt_note_1 (bfd *abfd, Elf_Internal_Note *note)
11328
0
{
11329
0
  struct sdt_note *cur = bfd_alloc (abfd, sizeof (*cur) + note->descsz);
11330
11331
0
  if (cur == NULL)
11332
0
    return false;
11333
0
  cur->next = (struct sdt_note *) (elf_tdata (abfd))->sdt_note_head;
11334
0
  cur->size = note->descsz;
11335
0
  memcpy (cur->data, note->descdata, note->descsz);
11336
11337
0
  elf_tdata (abfd)->sdt_note_head = cur;
11338
11339
0
  return true;
11340
0
}
11341
11342
static bool
11343
elfobj_grok_stapsdt_note (bfd *abfd, Elf_Internal_Note *note)
11344
0
{
11345
0
  switch (note->type)
11346
0
    {
11347
0
    case NT_STAPSDT:
11348
0
      return elfobj_grok_stapsdt_note_1 (abfd, note);
11349
11350
0
    default:
11351
0
      return true;
11352
0
    }
11353
0
}
11354
11355
static bool
11356
elfcore_grok_freebsd_psinfo (bfd *abfd, Elf_Internal_Note *note)
11357
1
{
11358
1
  size_t offset;
11359
11360
1
  switch (elf_elfheader (abfd)->e_ident[EI_CLASS])
11361
1
    {
11362
0
    case ELFCLASS32:
11363
0
      if (note->descsz < 108)
11364
0
  return false;
11365
0
      break;
11366
11367
1
    case ELFCLASS64:
11368
1
      if (note->descsz < 120)
11369
0
  return false;
11370
1
      break;
11371
11372
1
    default:
11373
0
      return false;
11374
1
    }
11375
11376
  /* Check for version 1 in pr_version.  */
11377
1
  if (bfd_h_get_32 (abfd, (bfd_byte *) note->descdata) != 1)
11378
1
    return false;
11379
11380
0
  offset = 4;
11381
11382
  /* Skip over pr_psinfosz. */
11383
0
  if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS32)
11384
0
    offset += 4;
11385
0
  else
11386
0
    {
11387
0
      offset += 4;  /* Padding before pr_psinfosz. */
11388
0
      offset += 8;
11389
0
    }
11390
11391
  /* pr_fname is PRFNAMESZ (16) + 1 bytes in size.  */
11392
0
  elf_tdata (abfd)->core->program
11393
0
    = _bfd_elfcore_strndup (abfd, note->descdata + offset, 17);
11394
0
  offset += 17;
11395
11396
  /* pr_psargs is PRARGSZ (80) + 1 bytes in size.  */
11397
0
  elf_tdata (abfd)->core->command
11398
0
    = _bfd_elfcore_strndup (abfd, note->descdata + offset, 81);
11399
0
  offset += 81;
11400
11401
  /* Padding before pr_pid.  */
11402
0
  offset += 2;
11403
11404
  /* The pr_pid field was added in version "1a".  */
11405
0
  if (note->descsz < offset + 4)
11406
0
    return true;
11407
11408
0
  elf_tdata (abfd)->core->pid
11409
0
    = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
11410
11411
0
  return true;
11412
0
}
11413
11414
static bool
11415
elfcore_grok_freebsd_prstatus (bfd *abfd, Elf_Internal_Note *note)
11416
0
{
11417
0
  size_t offset;
11418
0
  size_t size;
11419
0
  size_t min_size;
11420
11421
  /* Compute offset of pr_getregsz, skipping over pr_statussz.
11422
     Also compute minimum size of this note.  */
11423
0
  switch (elf_elfheader (abfd)->e_ident[EI_CLASS])
11424
0
    {
11425
0
    case ELFCLASS32:
11426
0
      offset = 4 + 4;
11427
0
      min_size = offset + (4 * 2) + 4 + 4 + 4;
11428
0
      break;
11429
11430
0
    case ELFCLASS64:
11431
0
      offset = 4 + 4 + 8; /* Includes padding before pr_statussz.  */
11432
0
      min_size = offset + (8 * 2) + 4 + 4 + 4 + 4;
11433
0
      break;
11434
11435
0
    default:
11436
0
      return false;
11437
0
    }
11438
11439
0
  if (note->descsz < min_size)
11440
0
    return false;
11441
11442
  /* Check for version 1 in pr_version.  */
11443
0
  if (bfd_h_get_32 (abfd, (bfd_byte *) note->descdata) != 1)
11444
0
    return false;
11445
11446
  /* Extract size of pr_reg from pr_gregsetsz.  */
11447
  /* Skip over pr_gregsetsz and pr_fpregsetsz.  */
11448
0
  if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS32)
11449
0
    {
11450
0
      size = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
11451
0
      offset += 4 * 2;
11452
0
    }
11453
0
  else
11454
0
    {
11455
0
      size = bfd_h_get_64 (abfd, (bfd_byte *) note->descdata + offset);
11456
0
      offset += 8 * 2;
11457
0
    }
11458
11459
  /* Skip over pr_osreldate.  */
11460
0
  offset += 4;
11461
11462
  /* Read signal from pr_cursig.  */
11463
0
  if (elf_tdata (abfd)->core->signal == 0)
11464
0
    elf_tdata (abfd)->core->signal
11465
0
      = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
11466
0
  offset += 4;
11467
11468
  /* Read TID from pr_pid.  */
11469
0
  elf_tdata (abfd)->core->lwpid
11470
0
      = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
11471
0
  offset += 4;
11472
11473
  /* Padding before pr_reg.  */
11474
0
  if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS64)
11475
0
    offset += 4;
11476
11477
  /* Make sure that there is enough data remaining in the note.  */
11478
0
  if ((note->descsz - offset) < size)
11479
0
    return false;
11480
11481
  /* Make a ".reg/999" section and a ".reg" section.  */
11482
0
  return _bfd_elfcore_make_pseudosection (abfd, NOTE_PSEUDO_SECTION_REG,
11483
0
            size, note->descpos + offset);
11484
0
}
11485
11486
static bool
11487
elfcore_grok_freebsd_note (bfd *abfd, Elf_Internal_Note *note)
11488
21
{
11489
21
  elf_backend_data *bed = get_elf_backend_data (abfd);
11490
11491
21
  switch (note->type)
11492
21
    {
11493
0
    case NT_PRSTATUS:
11494
0
      if (bed->elf_backend_grok_freebsd_prstatus)
11495
0
  if ((*bed->elf_backend_grok_freebsd_prstatus) (abfd, note))
11496
0
    return true;
11497
0
      return elfcore_grok_freebsd_prstatus (abfd, note);
11498
11499
5
    case NT_FPREGSET:
11500
5
      return elfcore_grok_prfpreg (abfd, note);
11501
11502
1
    case NT_PRPSINFO:
11503
1
      return elfcore_grok_freebsd_psinfo (abfd, note);
11504
11505
0
    case NT_FREEBSD_THRMISC:
11506
0
      return elfcore_make_note_pseudosection (abfd, ".thrmisc", note);
11507
11508
0
    case NT_FREEBSD_PROCSTAT_PROC:
11509
0
      return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.proc",
11510
0
                note);
11511
11512
0
    case NT_FREEBSD_PROCSTAT_FILES:
11513
0
      return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.files",
11514
0
                note);
11515
11516
0
    case NT_FREEBSD_PROCSTAT_VMMAP:
11517
0
      return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.vmmap",
11518
0
                note);
11519
11520
0
    case NT_FREEBSD_PROCSTAT_AUXV:
11521
0
      return elfcore_make_auxv_note_section (abfd, note, 4);
11522
11523
0
    case NT_FREEBSD_X86_SEGBASES:
11524
0
      return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_X86_SEGBASES, note);
11525
11526
0
    case NT_X86_XSTATE:
11527
0
      return elfcore_grok_xstatereg (abfd, note);
11528
11529
0
    case NT_FREEBSD_PTLWPINFO:
11530
0
      return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.lwpinfo",
11531
0
                note);
11532
11533
0
    case NT_ARM_TLS:
11534
0
      return elfcore_grok_aarch_tls (abfd, note);
11535
11536
0
    case NT_ARM_VFP:
11537
0
      return elfcore_grok_arm_vfp (abfd, note);
11538
11539
15
    default:
11540
15
      return true;
11541
21
    }
11542
21
}
11543
11544
static bool
11545
elfcore_netbsd_get_lwpid (Elf_Internal_Note *note, int *lwpidp)
11546
0
{
11547
0
  char *cp;
11548
11549
0
  cp = strchr (note->namedata, '@');
11550
0
  if (cp != NULL)
11551
0
    {
11552
0
      *lwpidp = atoi(cp + 1);
11553
0
      return true;
11554
0
    }
11555
0
  return false;
11556
0
}
11557
11558
static bool
11559
elfcore_grok_netbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
11560
0
{
11561
0
  if (note->descsz <= 0x7c + 31)
11562
0
    return false;
11563
11564
  /* Signal number at offset 0x08. */
11565
0
  elf_tdata (abfd)->core->signal
11566
0
    = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
11567
11568
  /* Process ID at offset 0x50. */
11569
0
  elf_tdata (abfd)->core->pid
11570
0
    = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x50);
11571
11572
  /* Command name at 0x7c (max 32 bytes, including nul). */
11573
0
  elf_tdata (abfd)->core->command
11574
0
    = _bfd_elfcore_strndup (abfd, note->descdata + 0x7c, 31);
11575
11576
0
  return elfcore_make_note_pseudosection (abfd, ".note.netbsdcore.procinfo",
11577
0
            note);
11578
0
}
11579
11580
static bool
11581
elfcore_grok_netbsd_note (bfd *abfd, Elf_Internal_Note *note)
11582
0
{
11583
0
  int lwp;
11584
11585
0
  if (elfcore_netbsd_get_lwpid (note, &lwp))
11586
0
    elf_tdata (abfd)->core->lwpid = lwp;
11587
11588
0
  switch (note->type)
11589
0
    {
11590
0
    case NT_NETBSDCORE_PROCINFO:
11591
      /* NetBSD-specific core "procinfo".  Note that we expect to
11592
   find this note before any of the others, which is fine,
11593
   since the kernel writes this note out first when it
11594
   creates a core file.  */
11595
0
      return elfcore_grok_netbsd_procinfo (abfd, note);
11596
0
    case NT_NETBSDCORE_AUXV:
11597
      /* NetBSD-specific Elf Auxiliary Vector data. */
11598
0
      return elfcore_make_auxv_note_section (abfd, note, 4);
11599
0
    case NT_NETBSDCORE_LWPSTATUS:
11600
0
      return elfcore_make_note_pseudosection (abfd,
11601
0
                ".note.netbsdcore.lwpstatus",
11602
0
                note);
11603
0
    default:
11604
0
      break;
11605
0
    }
11606
11607
  /* As of March 2020 there are no other machine-independent notes
11608
     defined for NetBSD core files.  If the note type is less
11609
     than the start of the machine-dependent note types, we don't
11610
     understand it.  */
11611
11612
0
  if (note->type < NT_NETBSDCORE_FIRSTMACH)
11613
0
    return true;
11614
11615
11616
0
  switch (bfd_get_arch (abfd))
11617
0
    {
11618
      /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
11619
   PT_GETFPREGS == mach+2.  */
11620
11621
0
    case bfd_arch_aarch64:
11622
0
    case bfd_arch_alpha:
11623
0
    case bfd_arch_sparc:
11624
0
      switch (note->type)
11625
0
  {
11626
0
  case NT_NETBSDCORE_FIRSTMACH+0:
11627
0
    return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_REG, note);
11628
11629
0
  case NT_NETBSDCORE_FIRSTMACH+2:
11630
0
    return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_REG2, note);
11631
11632
0
  default:
11633
0
    return true;
11634
0
  }
11635
11636
      /* On SuperH, PT_GETREGS == mach+3 and PT_GETFPREGS == mach+5.
11637
   There's also old PT___GETREGS40 == mach + 1 for old reg
11638
   structure which lacks GBR.  */
11639
11640
0
    case bfd_arch_sh:
11641
0
      switch (note->type)
11642
0
  {
11643
0
  case NT_NETBSDCORE_FIRSTMACH+3:
11644
0
    return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_REG, note);
11645
11646
0
  case NT_NETBSDCORE_FIRSTMACH+5:
11647
0
    return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_REG2, note);
11648
11649
0
  default:
11650
0
    return true;
11651
0
  }
11652
11653
      /* On all other arch's, PT_GETREGS == mach+1 and
11654
   PT_GETFPREGS == mach+3.  */
11655
11656
0
    default:
11657
0
      switch (note->type)
11658
0
  {
11659
0
  case NT_NETBSDCORE_FIRSTMACH+1:
11660
0
    return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_REG, note);
11661
11662
0
  case NT_NETBSDCORE_FIRSTMACH+3:
11663
0
    return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_REG2, note);
11664
11665
0
  default:
11666
0
    return true;
11667
0
  }
11668
0
    }
11669
    /* NOTREACHED */
11670
0
}
11671
11672
static bool
11673
elfcore_grok_openbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
11674
0
{
11675
0
  if (note->descsz <= 0x48 + 31)
11676
0
    return false;
11677
11678
  /* Signal number at offset 0x08. */
11679
0
  elf_tdata (abfd)->core->signal
11680
0
    = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
11681
11682
  /* Process ID at offset 0x20. */
11683
0
  elf_tdata (abfd)->core->pid
11684
0
    = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x20);
11685
11686
  /* Command name at 0x48 (max 32 bytes, including nul). */
11687
0
  elf_tdata (abfd)->core->command
11688
0
    = _bfd_elfcore_strndup (abfd, note->descdata + 0x48, 31);
11689
11690
0
  return true;
11691
0
}
11692
11693
/* Processes Solaris's process status note.
11694
   sig_off ~ offsetof(prstatus_t, pr_cursig)
11695
   pid_off ~ offsetof(prstatus_t, pr_pid)
11696
   lwpid_off ~ offsetof(prstatus_t, pr_who)
11697
   gregset_size ~ sizeof(gregset_t)
11698
   gregset_offset ~ offsetof(prstatus_t, pr_reg)  */
11699
11700
static bool
11701
elfcore_grok_solaris_prstatus (bfd *abfd, Elf_Internal_Note* note, int sig_off,
11702
             int pid_off, int lwpid_off, size_t gregset_size,
11703
             size_t gregset_offset)
11704
0
{
11705
0
  asection *sect = NULL;
11706
0
  elf_tdata (abfd)->core->signal
11707
0
    = bfd_get_16 (abfd, note->descdata + sig_off);
11708
0
  elf_tdata (abfd)->core->pid
11709
0
    = bfd_get_32 (abfd, note->descdata + pid_off);
11710
0
  elf_tdata (abfd)->core->lwpid
11711
0
    = bfd_get_32 (abfd, note->descdata + lwpid_off);
11712
11713
0
  sect = bfd_get_section_by_name (abfd, NOTE_PSEUDO_SECTION_REG);
11714
0
  if (sect != NULL)
11715
0
    sect->size = gregset_size;
11716
11717
0
  return _bfd_elfcore_make_pseudosection (abfd, NOTE_PSEUDO_SECTION_REG, gregset_size,
11718
0
            note->descpos + gregset_offset);
11719
0
}
11720
11721
/* Gets program and arguments from a core.
11722
   prog_off ~ offsetof(prpsinfo | psinfo_t, pr_fname)
11723
   comm_off ~ offsetof(prpsinfo | psinfo_t, pr_psargs)  */
11724
11725
static bool
11726
elfcore_grok_solaris_info(bfd *abfd, Elf_Internal_Note* note,
11727
        int prog_off, int comm_off)
11728
0
{
11729
0
  elf_tdata (abfd)->core->program
11730
0
    = _bfd_elfcore_strndup (abfd, note->descdata + prog_off, 16);
11731
0
  elf_tdata (abfd)->core->command
11732
0
    = _bfd_elfcore_strndup (abfd, note->descdata + comm_off, 80);
11733
11734
0
  return true;
11735
0
}
11736
11737
/* Processes Solaris's LWP status note.
11738
   gregset_size ~ sizeof(gregset_t)
11739
   gregset_off ~ offsetof(lwpstatus_t, pr_reg)
11740
   fpregset_size ~ sizeof(fpregset_t)
11741
   fpregset_off ~ offsetof(lwpstatus_t, pr_fpreg)  */
11742
11743
static bool
11744
elfcore_grok_solaris_lwpstatus (bfd *abfd, Elf_Internal_Note* note,
11745
        size_t gregset_size, int gregset_off,
11746
        size_t fpregset_size, int fpregset_off)
11747
0
{
11748
0
  asection *sect = NULL;
11749
0
  char reg2_section_name[16] = { 0 };
11750
11751
0
  (void) snprintf (reg2_section_name, 16, "%s/%i", NOTE_PSEUDO_SECTION_REG2,
11752
0
       elf_tdata (abfd)->core->lwpid);
11753
11754
  /* offsetof(lwpstatus_t, pr_lwpid) */
11755
0
  elf_tdata (abfd)->core->lwpid
11756
0
    = bfd_get_32 (abfd, note->descdata + 4);
11757
  /* offsetof(lwpstatus_t, pr_cursig) */
11758
0
  elf_tdata (abfd)->core->signal
11759
0
    = bfd_get_16 (abfd, note->descdata + 12);
11760
11761
0
  sect = bfd_get_section_by_name (abfd, NOTE_PSEUDO_SECTION_REG);
11762
0
  if (sect != NULL)
11763
0
    sect->size = gregset_size;
11764
0
  else if (!_bfd_elfcore_make_pseudosection (abfd, NOTE_PSEUDO_SECTION_REG, gregset_size,
11765
0
               note->descpos + gregset_off))
11766
0
    return false;
11767
11768
0
  sect = bfd_get_section_by_name (abfd, reg2_section_name);
11769
0
  if (sect != NULL)
11770
0
    {
11771
0
      sect->size = fpregset_size;
11772
0
      sect->filepos = note->descpos + fpregset_off;
11773
0
      sect->alignment_power = 2;
11774
0
    }
11775
0
  else if (!_bfd_elfcore_make_pseudosection (abfd, NOTE_PSEUDO_SECTION_REG2, fpregset_size,
11776
0
               note->descpos + fpregset_off))
11777
0
    return false;
11778
11779
0
  return true;
11780
0
}
11781
11782
static bool
11783
elfcore_grok_solaris_note_impl (bfd *abfd, Elf_Internal_Note *note)
11784
19
{
11785
19
  if (note == NULL)
11786
0
    return false;
11787
11788
  /* core files are identified as 32- or 64-bit, SPARC or x86,
11789
     by the size of the descsz which matches the sizeof()
11790
     the type appropriate for that note type (e.g., prstatus_t for
11791
     SOLARIS_NT_PRSTATUS) for the corresponding architecture
11792
     on Solaris. The core file bitness may differ from the bitness of
11793
     gdb itself, so fixed values are used instead of sizeof().
11794
     Appropriate fixed offsets are also used to obtain data from
11795
     the note.  */
11796
11797
19
  switch ((int) note->type)
11798
19
    {
11799
12
    case SOLARIS_NT_PRSTATUS:
11800
12
      switch (note->descsz)
11801
12
  {
11802
0
  case 508: /* sizeof(prstatus_t) SPARC 32-bit */
11803
0
    return elfcore_grok_solaris_prstatus(abfd, note,
11804
0
                 136, 216, 308, 152, 356);
11805
0
  case 904: /* sizeof(prstatus_t) SPARC 64-bit */
11806
0
    return elfcore_grok_solaris_prstatus(abfd, note,
11807
0
                 264, 360, 520, 304, 600);
11808
0
  case 432: /* sizeof(prstatus_t) Intel 32-bit */
11809
0
    return elfcore_grok_solaris_prstatus(abfd, note,
11810
0
                 136, 216, 308, 76, 356);
11811
0
  case 824: /* sizeof(prstatus_t) Intel 64-bit */
11812
0
    return elfcore_grok_solaris_prstatus(abfd, note,
11813
0
                 264, 360, 520, 224, 600);
11814
12
  default:
11815
12
    return true;
11816
12
  }
11817
11818
0
    case SOLARIS_NT_PSINFO:
11819
0
    case SOLARIS_NT_PRPSINFO:
11820
0
      switch (note->descsz)
11821
0
  {
11822
0
  case 260: /* sizeof(prpsinfo_t) SPARC and Intel 32-bit */
11823
0
    return elfcore_grok_solaris_info(abfd, note, 84, 100);
11824
0
  case 328: /* sizeof(prpsinfo_t) SPARC and Intel 64-bit */
11825
0
    return elfcore_grok_solaris_info(abfd, note, 120, 136);
11826
0
  case 360: /* sizeof(psinfo_t) SPARC and Intel 32-bit */
11827
0
    return elfcore_grok_solaris_info(abfd, note, 88, 104);
11828
0
  case 440: /* sizeof(psinfo_t) SPARC and Intel 64-bit */
11829
0
    return elfcore_grok_solaris_info(abfd, note, 136, 152);
11830
0
  default:
11831
0
    return true;
11832
0
  }
11833
11834
0
    case SOLARIS_NT_LWPSTATUS:
11835
0
      switch (note->descsz)
11836
0
  {
11837
0
  case 896: /* sizeof(lwpstatus_t) SPARC 32-bit */
11838
0
    return elfcore_grok_solaris_lwpstatus(abfd, note,
11839
0
            152, 344, 400, 496);
11840
0
  case 1392: /* sizeof(lwpstatus_t) SPARC 64-bit */
11841
0
    return elfcore_grok_solaris_lwpstatus(abfd, note,
11842
0
            304, 544, 544, 848);
11843
0
  case 800: /* sizeof(lwpstatus_t) Intel 32-bit */
11844
0
    return elfcore_grok_solaris_lwpstatus(abfd, note,
11845
0
            76, 344, 380, 420);
11846
0
  case 1296: /* sizeof(lwpstatus_t) Intel 64-bit */
11847
0
    return elfcore_grok_solaris_lwpstatus(abfd, note,
11848
0
            224, 544, 528, 768);
11849
0
  default:
11850
0
    return true;
11851
0
  }
11852
11853
0
    case SOLARIS_NT_LWPSINFO:
11854
      /* sizeof(lwpsinfo_t) on 32- and 64-bit, respectively */
11855
0
      if (note->descsz == 128 || note->descsz == 152)
11856
0
  elf_tdata (abfd)->core->lwpid =
11857
0
    bfd_get_32 (abfd, note->descdata + 4);
11858
0
      break;
11859
11860
7
    default:
11861
7
      break;
11862
19
    }
11863
11864
7
  return true;
11865
19
}
11866
11867
/* For name starting with "CORE" this may be either a Solaris
11868
   core file or a gdb-generated core file.  Do Solaris-specific
11869
   processing on selected note types first with
11870
   elfcore_grok_solaris_note(), then process the note
11871
   in elfcore_grok_note().  */
11872
11873
static bool
11874
elfcore_grok_solaris_note (bfd *abfd, Elf_Internal_Note *note)
11875
19
{
11876
19
  if (!elfcore_grok_solaris_note_impl (abfd, note))
11877
0
    return false;
11878
11879
19
  return elfcore_grok_note (abfd, note);
11880
19
}
11881
11882
static bool
11883
elfcore_grok_openbsd_note (bfd *abfd, Elf_Internal_Note *note)
11884
1
{
11885
1
  if (note->type == NT_OPENBSD_PROCINFO)
11886
0
    return elfcore_grok_openbsd_procinfo (abfd, note);
11887
11888
1
  if (note->type == NT_OPENBSD_REGS)
11889
0
    return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_REG, note);
11890
11891
1
  if (note->type == NT_OPENBSD_FPREGS)
11892
0
    return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_REG2, note);
11893
11894
1
  if (note->type == NT_OPENBSD_XFPREGS)
11895
0
    return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_XFP, note);
11896
11897
1
  if (note->type == NT_OPENBSD_AUXV)
11898
0
    return elfcore_make_auxv_note_section (abfd, note, 0);
11899
11900
1
  if (note->type == NT_OPENBSD_WCOOKIE)
11901
0
    {
11902
0
      asection *sect = bfd_make_section_anyway_with_flags (abfd, ".wcookie",
11903
0
                 SEC_HAS_CONTENTS);
11904
11905
0
      if (sect == NULL)
11906
0
  return false;
11907
0
      sect->size = note->descsz;
11908
0
      sect->filepos = note->descpos;
11909
0
      sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
11910
11911
0
      return true;
11912
0
    }
11913
11914
1
  return true;
11915
1
}
11916
11917
static bool
11918
elfcore_grok_nto_status (bfd *abfd, Elf_Internal_Note *note, long *tid)
11919
0
{
11920
0
  void *ddata = note->descdata;
11921
0
  char buf[100];
11922
0
  char *name;
11923
0
  asection *sect;
11924
0
  short sig;
11925
0
  unsigned flags;
11926
11927
0
  if (note->descsz < 16)
11928
0
    return false;
11929
11930
  /* nto_procfs_status 'pid' field is at offset 0.  */
11931
0
  elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, (bfd_byte *) ddata);
11932
11933
  /* nto_procfs_status 'tid' field is at offset 4.  Pass it back.  */
11934
0
  *tid = bfd_get_32 (abfd, (bfd_byte *) ddata + 4);
11935
11936
  /* nto_procfs_status 'flags' field is at offset 8.  */
11937
0
  flags = bfd_get_32 (abfd, (bfd_byte *) ddata + 8);
11938
11939
  /* nto_procfs_status 'what' field is at offset 14.  */
11940
0
  if ((sig = bfd_get_16 (abfd, (bfd_byte *) ddata + 14)) > 0)
11941
0
    {
11942
0
      elf_tdata (abfd)->core->signal = sig;
11943
0
      elf_tdata (abfd)->core->lwpid = *tid;
11944
0
    }
11945
11946
  /* _DEBUG_FLAG_CURTID (current thread) is 0x80.  Some cores
11947
     do not come from signals so we make sure we set the current
11948
     thread just in case.  */
11949
0
  if (flags & 0x00000080)
11950
0
    elf_tdata (abfd)->core->lwpid = *tid;
11951
11952
  /* Make a ".qnx_core_status/%d" section.  */
11953
0
  sprintf (buf, ".qnx_core_status/%ld", *tid);
11954
11955
0
  name = bfd_alloc (abfd, strlen (buf) + 1);
11956
0
  if (name == NULL)
11957
0
    return false;
11958
0
  strcpy (name, buf);
11959
11960
0
  sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
11961
0
  if (sect == NULL)
11962
0
    return false;
11963
11964
0
  sect->size    = note->descsz;
11965
0
  sect->filepos   = note->descpos;
11966
0
  sect->alignment_power = 2;
11967
11968
0
  return (elfcore_maybe_make_sect (abfd, ".qnx_core_status", sect));
11969
0
}
11970
11971
static bool
11972
elfcore_grok_nto_regs (bfd *abfd,
11973
           Elf_Internal_Note *note,
11974
           long tid,
11975
           char *base)
11976
0
{
11977
0
  char buf[100];
11978
0
  char *name;
11979
0
  asection *sect;
11980
11981
  /* Make a "(base)/%d" section.  */
11982
0
  sprintf (buf, "%s/%ld", base, tid);
11983
11984
0
  name = bfd_alloc (abfd, strlen (buf) + 1);
11985
0
  if (name == NULL)
11986
0
    return false;
11987
0
  strcpy (name, buf);
11988
11989
0
  sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
11990
0
  if (sect == NULL)
11991
0
    return false;
11992
11993
0
  sect->size    = note->descsz;
11994
0
  sect->filepos   = note->descpos;
11995
0
  sect->alignment_power = 2;
11996
11997
  /* This is the current thread.  */
11998
0
  if (elf_tdata (abfd)->core->lwpid == tid)
11999
0
    return elfcore_maybe_make_sect (abfd, base, sect);
12000
12001
0
  return true;
12002
0
}
12003
12004
static bool
12005
elfcore_grok_nto_note (bfd *abfd, Elf_Internal_Note *note)
12006
66
{
12007
  /* Every GREG section has a STATUS section before it.  Store the
12008
     tid from the previous call to pass down to the next gregs
12009
     function.  */
12010
66
  static long tid = 1;
12011
12012
66
  switch (note->type)
12013
66
    {
12014
0
    case QNT_CORE_INFO:
12015
0
      return elfcore_make_note_pseudosection (abfd, ".qnx_core_info", note);
12016
0
    case QNT_CORE_STATUS:
12017
0
      return elfcore_grok_nto_status (abfd, note, &tid);
12018
0
    case QNT_CORE_GREG:
12019
0
      return elfcore_grok_nto_regs (abfd, note, tid, NOTE_PSEUDO_SECTION_REG);
12020
0
    case QNT_CORE_FPREG:
12021
0
      return elfcore_grok_nto_regs (abfd, note, tid, NOTE_PSEUDO_SECTION_REG2);
12022
66
    default:
12023
66
      return true;
12024
66
    }
12025
66
}
12026
12027
static bool
12028
elfcore_grok_spu_note (bfd *abfd, Elf_Internal_Note *note)
12029
9
{
12030
9
  char *name;
12031
9
  asection *sect;
12032
9
  size_t len;
12033
12034
  /* Use note name as section name.  */
12035
9
  len = note->namesz;
12036
9
  name = bfd_alloc (abfd, len);
12037
9
  if (name == NULL)
12038
0
    return false;
12039
9
  memcpy (name, note->namedata, len);
12040
9
  name[len - 1] = '\0';
12041
12042
9
  sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
12043
9
  if (sect == NULL)
12044
0
    return false;
12045
12046
9
  sect->size    = note->descsz;
12047
9
  sect->filepos   = note->descpos;
12048
9
  sect->alignment_power = 1;
12049
12050
9
  return true;
12051
9
}
12052
12053
/* Function: elfcore_write_note
12054
12055
   Inputs:
12056
     buffer to hold note, and current size of buffer
12057
     name of note
12058
     type of note
12059
     data for note
12060
     size of data for note
12061
12062
   Writes note to end of buffer.  ELF64 notes are written exactly as
12063
   for ELF32, despite the current (as of 2006) ELF gabi specifying
12064
   that they ought to have 8-byte namesz and descsz field, and have
12065
   8-byte alignment.  Other writers, eg. Linux kernel, do the same.
12066
12067
   Return:
12068
   Pointer to realloc'd buffer, *BUFSIZ updated.  */
12069
12070
char *
12071
elfcore_write_note (bfd *abfd,
12072
        char *buf,
12073
        int *bufsiz,
12074
        const char *name,
12075
        int type,
12076
        const void *input,
12077
        int size)
12078
0
{
12079
0
  Elf_External_Note *xnp;
12080
0
  size_t namesz;
12081
0
  size_t newspace;
12082
0
  char *dest;
12083
12084
0
  namesz = 0;
12085
0
  if (name != NULL)
12086
0
    namesz = strlen (name) + 1;
12087
12088
0
  newspace = 12 + ((namesz + 3) & -4) + ((size + 3) & -4);
12089
12090
0
  buf = realloc (buf, *bufsiz + newspace);
12091
0
  if (buf == NULL)
12092
0
    return buf;
12093
0
  dest = buf + *bufsiz;
12094
0
  *bufsiz += newspace;
12095
0
  xnp = (Elf_External_Note *) dest;
12096
0
  H_PUT_32 (abfd, namesz, xnp->namesz);
12097
0
  H_PUT_32 (abfd, size, xnp->descsz);
12098
0
  H_PUT_32 (abfd, type, xnp->type);
12099
0
  dest = xnp->name;
12100
0
  if (name != NULL)
12101
0
    {
12102
0
      memcpy (dest, name, namesz);
12103
0
      dest += namesz;
12104
0
      while (namesz & 3)
12105
0
  {
12106
0
    *dest++ = '\0';
12107
0
    ++namesz;
12108
0
  }
12109
0
    }
12110
0
  memcpy (dest, input, size);
12111
0
  dest += size;
12112
0
  while (size & 3)
12113
0
    {
12114
0
      *dest++ = '\0';
12115
0
      ++size;
12116
0
    }
12117
0
  return buf;
12118
0
}
12119
12120
/* gcc-8 warns (*) on all the strncpy calls in this function about
12121
   possible string truncation.  The "truncation" is not a bug.  We
12122
   have an external representation of structs with fields that are not
12123
   necessarily NULL terminated and corresponding internal
12124
   representation fields that are one larger so that they can always
12125
   be NULL terminated.
12126
   gcc versions between 4.2 and 4.6 do not allow pragma control of
12127
   diagnostics inside functions, giving a hard error if you try to use
12128
   the finer control available with later versions.
12129
   gcc prior to 4.2 warns about diagnostic push and pop.
12130
   gcc-5, gcc-6 and gcc-7 warn that -Wstringop-truncation is unknown,
12131
   unless you also add #pragma GCC diagnostic ignored "-Wpragma".
12132
   (*) Depending on your system header files!  */
12133
#if GCC_VERSION >= 8000
12134
# pragma GCC diagnostic push
12135
# pragma GCC diagnostic ignored "-Wstringop-truncation"
12136
#endif
12137
char *
12138
elfcore_write_prpsinfo (bfd  *abfd,
12139
      char *buf,
12140
      int  *bufsiz,
12141
      const char *fname,
12142
      const char *psargs)
12143
0
{
12144
0
  elf_backend_data *bed = get_elf_backend_data (abfd);
12145
12146
0
  if (bed->elf_backend_write_core_note != NULL)
12147
0
    {
12148
0
      char *ret;
12149
0
      ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
12150
0
             NT_PRPSINFO, fname, psargs);
12151
0
      if (ret != NULL)
12152
0
  return ret;
12153
0
    }
12154
12155
0
#if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
12156
0
# if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
12157
0
  if (bed->s->elfclass == ELFCLASS32)
12158
0
    {
12159
#  if defined (HAVE_PSINFO32_T)
12160
      psinfo32_t data;
12161
      int note_type = NT_PSINFO;
12162
#  else
12163
0
      prpsinfo32_t data;
12164
0
      int note_type = NT_PRPSINFO;
12165
0
#  endif
12166
12167
0
      memset (&data, 0, sizeof (data));
12168
0
      strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
12169
0
      strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
12170
0
      return elfcore_write_note (abfd, buf, bufsiz,
12171
0
         NOTE_NAME_CORE, note_type, &data, sizeof (data));
12172
0
    }
12173
0
  else
12174
0
# endif
12175
0
    {
12176
# if defined (HAVE_PSINFO_T)
12177
      psinfo_t data;
12178
      int note_type = NT_PSINFO;
12179
# else
12180
0
      prpsinfo_t data;
12181
0
      int note_type = NT_PRPSINFO;
12182
0
# endif
12183
12184
0
      memset (&data, 0, sizeof (data));
12185
0
      strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
12186
0
      strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
12187
0
      return elfcore_write_note (abfd, buf, bufsiz,
12188
0
         NOTE_NAME_CORE, note_type, &data, sizeof (data));
12189
0
    }
12190
0
#endif  /* PSINFO_T or PRPSINFO_T */
12191
12192
0
  free (buf);
12193
0
  return NULL;
12194
0
}
12195
#if GCC_VERSION >= 8000
12196
# pragma GCC diagnostic pop
12197
#endif
12198
12199
char *
12200
elfcore_write_linux_prpsinfo32
12201
  (bfd *abfd, char *buf, int *bufsiz,
12202
   const struct elf_internal_linux_prpsinfo *prpsinfo)
12203
0
{
12204
0
  if (get_elf_backend_data (abfd)->linux_prpsinfo32_ugid16)
12205
0
    {
12206
0
      struct elf_external_linux_prpsinfo32_ugid16 data;
12207
12208
0
      swap_linux_prpsinfo32_ugid16_out (abfd, prpsinfo, &data);
12209
0
      return elfcore_write_note (abfd, buf, bufsiz, NOTE_NAME_CORE, NT_PRPSINFO,
12210
0
         &data, sizeof (data));
12211
0
    }
12212
0
  else
12213
0
    {
12214
0
      struct elf_external_linux_prpsinfo32_ugid32 data;
12215
12216
0
      swap_linux_prpsinfo32_ugid32_out (abfd, prpsinfo, &data);
12217
0
      return elfcore_write_note (abfd, buf, bufsiz, NOTE_NAME_CORE, NT_PRPSINFO,
12218
0
         &data, sizeof (data));
12219
0
    }
12220
0
}
12221
12222
char *
12223
elfcore_write_linux_prpsinfo64
12224
  (bfd *abfd, char *buf, int *bufsiz,
12225
   const struct elf_internal_linux_prpsinfo *prpsinfo)
12226
0
{
12227
0
  if (get_elf_backend_data (abfd)->linux_prpsinfo64_ugid16)
12228
0
    {
12229
0
      struct elf_external_linux_prpsinfo64_ugid16 data;
12230
12231
0
      swap_linux_prpsinfo64_ugid16_out (abfd, prpsinfo, &data);
12232
0
      return elfcore_write_note (abfd, buf, bufsiz,
12233
0
         NOTE_NAME_CORE, NT_PRPSINFO, &data, sizeof (data));
12234
0
    }
12235
0
  else
12236
0
    {
12237
0
      struct elf_external_linux_prpsinfo64_ugid32 data;
12238
12239
0
      swap_linux_prpsinfo64_ugid32_out (abfd, prpsinfo, &data);
12240
0
      return elfcore_write_note (abfd, buf, bufsiz,
12241
0
         NOTE_NAME_CORE, NT_PRPSINFO, &data, sizeof (data));
12242
0
    }
12243
0
}
12244
12245
char *
12246
elfcore_write_prstatus (bfd *abfd,
12247
      char *buf,
12248
      int *bufsiz,
12249
      long pid,
12250
      int cursig,
12251
      const void *gregs)
12252
0
{
12253
0
  elf_backend_data *bed = get_elf_backend_data (abfd);
12254
12255
0
  if (bed->elf_backend_write_core_note != NULL)
12256
0
    {
12257
0
      char *ret;
12258
0
      ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
12259
0
             NT_PRSTATUS,
12260
0
             pid, cursig, gregs);
12261
0
      if (ret != NULL)
12262
0
  return ret;
12263
0
    }
12264
12265
0
#if defined (HAVE_PRSTATUS_T)
12266
0
#if defined (HAVE_PRSTATUS32_T)
12267
0
  if (bed->s->elfclass == ELFCLASS32)
12268
0
    {
12269
0
      prstatus32_t prstat;
12270
12271
0
      memset (&prstat, 0, sizeof (prstat));
12272
0
      prstat.pr_pid = pid;
12273
0
      prstat.pr_cursig = cursig;
12274
0
      memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
12275
0
      return elfcore_write_note (abfd, buf, bufsiz, NOTE_NAME_CORE,
12276
0
         NT_PRSTATUS, &prstat, sizeof (prstat));
12277
0
    }
12278
0
  else
12279
0
#endif
12280
0
    {
12281
0
      prstatus_t prstat;
12282
12283
0
      memset (&prstat, 0, sizeof (prstat));
12284
0
      prstat.pr_pid = pid;
12285
0
      prstat.pr_cursig = cursig;
12286
0
      memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
12287
0
      return elfcore_write_note (abfd, buf, bufsiz, NOTE_NAME_CORE,
12288
0
         NT_PRSTATUS, &prstat, sizeof (prstat));
12289
0
    }
12290
0
#endif /* HAVE_PRSTATUS_T */
12291
12292
0
  free (buf);
12293
0
  return NULL;
12294
0
}
12295
12296
#if defined (HAVE_LWPSTATUS_T)
12297
char *
12298
elfcore_write_lwpstatus (bfd *abfd,
12299
       char *buf,
12300
       int *bufsiz,
12301
       long pid,
12302
       int cursig,
12303
       const void *gregs)
12304
{
12305
  lwpstatus_t lwpstat;
12306
12307
  memset (&lwpstat, 0, sizeof (lwpstat));
12308
  lwpstat.pr_lwpid  = pid >> 16;
12309
  lwpstat.pr_cursig = cursig;
12310
#if defined (HAVE_LWPSTATUS_T_PR_REG)
12311
  memcpy (&lwpstat.pr_reg, gregs, sizeof (lwpstat.pr_reg));
12312
#elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
12313
#if !defined(gregs)
12314
  memcpy (lwpstat.pr_context.uc_mcontext.gregs,
12315
    gregs, sizeof (lwpstat.pr_context.uc_mcontext.gregs));
12316
#else
12317
  memcpy (lwpstat.pr_context.uc_mcontext.__gregs,
12318
    gregs, sizeof (lwpstat.pr_context.uc_mcontext.__gregs));
12319
#endif
12320
#endif
12321
  return elfcore_write_note (abfd, buf, bufsiz, NOTE_NAME_CORE,
12322
           NT_LWPSTATUS, &lwpstat, sizeof (lwpstat));
12323
}
12324
#endif /* HAVE_LWPSTATUS_T */
12325
12326
#if defined (HAVE_PSTATUS_T)
12327
char *
12328
elfcore_write_pstatus (bfd *abfd,
12329
           char *buf,
12330
           int *bufsiz,
12331
           long pid,
12332
           int cursig ATTRIBUTE_UNUSED,
12333
           const void *gregs ATTRIBUTE_UNUSED)
12334
{
12335
#if defined (HAVE_PSTATUS32_T)
12336
  elf_backend_data *bed = get_elf_backend_data (abfd);
12337
12338
  if (bed->s->elfclass == ELFCLASS32)
12339
    {
12340
      pstatus32_t pstat;
12341
12342
      memset (&pstat, 0, sizeof (pstat));
12343
      pstat.pr_pid = pid & 0xffff;
12344
      buf = elfcore_write_note (abfd, buf, bufsiz, NOTE_NAME_CORE,
12345
        NT_PSTATUS, &pstat, sizeof (pstat));
12346
      return buf;
12347
    }
12348
  else
12349
#endif
12350
    {
12351
      pstatus_t pstat;
12352
12353
      memset (&pstat, 0, sizeof (pstat));
12354
      pstat.pr_pid = pid & 0xffff;
12355
      buf = elfcore_write_note (abfd, buf, bufsiz, NOTE_NAME_CORE,
12356
        NT_PSTATUS, &pstat, sizeof (pstat));
12357
      return buf;
12358
    }
12359
}
12360
#endif /* HAVE_PSTATUS_T */
12361
12362
char *
12363
elfcore_write_prfpreg (bfd *abfd,
12364
           char *buf,
12365
           int *bufsiz,
12366
           const void *fpregs,
12367
           int size)
12368
0
{
12369
0
  return elfcore_write_note (abfd, buf, bufsiz,
12370
0
           NOTE_NAME_CORE, NT_FPREGSET, fpregs, size);
12371
0
}
12372
12373
char *
12374
elfcore_write_prxfpreg (bfd *abfd,
12375
      char *buf,
12376
      int *bufsiz,
12377
      const void *xfpregs,
12378
      int size)
12379
0
{
12380
0
  return elfcore_write_note (abfd, buf, bufsiz,
12381
0
           NOTE_NAME_LINUX, NT_PRXFPREG, xfpregs, size);
12382
0
}
12383
12384
char *
12385
elfcore_write_xstatereg (bfd *abfd, char *buf, int *bufsiz,
12386
       const void *xfpregs, int size)
12387
0
{
12388
0
  char *note_name;
12389
12390
0
  if (get_elf_backend_data (abfd)->elf_osabi == ELFOSABI_FREEBSD)
12391
0
    note_name = NOTE_NAME_FREEBSD;
12392
0
  else
12393
0
    note_name = NOTE_NAME_LINUX;
12394
12395
0
  return elfcore_write_note (abfd, buf, bufsiz,
12396
0
           note_name, NT_X86_XSTATE, xfpregs, size);
12397
0
}
12398
12399
char *
12400
elfcore_write_xsave_layout (bfd *abfd, char *buf, int *bufsiz,
12401
          const void *xsave_layout, int size)
12402
0
{
12403
0
  return elfcore_write_note (abfd, buf, bufsiz, NOTE_NAME_LINUX,
12404
0
           NT_X86_XSAVE_LAYOUT, xsave_layout, size);
12405
0
}
12406
12407
static char *
12408
elfcore_write_sspreg (bfd *abfd, char *buf, int *bufsiz,
12409
          const void *ssp, int size)
12410
0
{
12411
0
  return elfcore_write_note (abfd, buf, bufsiz,
12412
0
           NOTE_NAME_LINUX, NT_X86_SHSTK, ssp, size);
12413
0
}
12414
12415
char *
12416
elfcore_write_x86_segbases (bfd *abfd, char *buf, int *bufsiz,
12417
          const void *regs, int size)
12418
0
{
12419
0
  return elfcore_write_note (abfd, buf, bufsiz,
12420
0
           NOTE_NAME_FREEBSD, NT_FREEBSD_X86_SEGBASES,
12421
0
           regs, size);
12422
0
}
12423
12424
char *
12425
elfcore_write_i386_tls (bfd *abfd, char *buf, int *bufsiz,
12426
          const void *regs, int size)
12427
0
{
12428
0
  return elfcore_write_note (abfd, buf, bufsiz, NOTE_NAME_LINUX, NT_386_TLS,
12429
0
           regs, size);
12430
0
}
12431
12432
char *
12433
elfcore_write_ppc_vmx (bfd *abfd,
12434
           char *buf,
12435
           int *bufsiz,
12436
           const void *ppc_vmx,
12437
           int size)
12438
0
{
12439
0
  return elfcore_write_note (abfd, buf, bufsiz,
12440
0
           NOTE_NAME_LINUX, NT_PPC_VMX, ppc_vmx, size);
12441
0
}
12442
12443
char *
12444
elfcore_write_ppc_vsx (bfd *abfd,
12445
           char *buf,
12446
           int *bufsiz,
12447
           const void *ppc_vsx,
12448
           int size)
12449
0
{
12450
0
  return elfcore_write_note (abfd, buf, bufsiz,
12451
0
           NOTE_NAME_LINUX, NT_PPC_VSX, ppc_vsx, size);
12452
0
}
12453
12454
char *
12455
elfcore_write_ppc_tar (bfd *abfd,
12456
           char *buf,
12457
           int *bufsiz,
12458
           const void *ppc_tar,
12459
           int size)
12460
0
{
12461
0
  return elfcore_write_note (abfd, buf, bufsiz,
12462
0
           NOTE_NAME_LINUX, NT_PPC_TAR, ppc_tar, size);
12463
0
}
12464
12465
char *
12466
elfcore_write_ppc_ppr (bfd *abfd,
12467
           char *buf,
12468
           int *bufsiz,
12469
           const void *ppc_ppr,
12470
           int size)
12471
0
{
12472
0
  return elfcore_write_note (abfd, buf, bufsiz,
12473
0
           NOTE_NAME_LINUX, NT_PPC_PPR, ppc_ppr, size);
12474
0
}
12475
12476
char *
12477
elfcore_write_ppc_dscr (bfd *abfd,
12478
      char *buf,
12479
      int *bufsiz,
12480
      const void *ppc_dscr,
12481
      int size)
12482
0
{
12483
0
  return elfcore_write_note (abfd, buf, bufsiz,
12484
0
           NOTE_NAME_LINUX, NT_PPC_DSCR, ppc_dscr, size);
12485
0
}
12486
12487
char *
12488
elfcore_write_ppc_ebb (bfd *abfd,
12489
           char *buf,
12490
           int *bufsiz,
12491
           const void *ppc_ebb,
12492
           int size)
12493
0
{
12494
0
  return elfcore_write_note (abfd, buf, bufsiz,
12495
0
           NOTE_NAME_LINUX, NT_PPC_EBB, ppc_ebb, size);
12496
0
}
12497
12498
char *
12499
elfcore_write_ppc_pmu (bfd *abfd,
12500
           char *buf,
12501
           int *bufsiz,
12502
           const void *ppc_pmu,
12503
           int size)
12504
0
{
12505
0
  return elfcore_write_note (abfd, buf, bufsiz,
12506
0
           NOTE_NAME_LINUX, NT_PPC_PMU, ppc_pmu, size);
12507
0
}
12508
12509
char *
12510
elfcore_write_ppc_tm_cgpr (bfd *abfd,
12511
         char *buf,
12512
         int *bufsiz,
12513
         const void *ppc_tm_cgpr,
12514
         int size)
12515
0
{
12516
0
  return elfcore_write_note (abfd, buf, bufsiz,
12517
0
           NOTE_NAME_LINUX, NT_PPC_TM_CGPR,
12518
0
           ppc_tm_cgpr, size);
12519
0
}
12520
12521
char *
12522
elfcore_write_ppc_tm_cfpr (bfd *abfd,
12523
         char *buf,
12524
         int *bufsiz,
12525
         const void *ppc_tm_cfpr,
12526
         int size)
12527
0
{
12528
0
  return elfcore_write_note (abfd, buf, bufsiz,
12529
0
           NOTE_NAME_LINUX, NT_PPC_TM_CFPR,
12530
0
           ppc_tm_cfpr, size);
12531
0
}
12532
12533
char *
12534
elfcore_write_ppc_tm_cvmx (bfd *abfd,
12535
         char *buf,
12536
         int *bufsiz,
12537
         const void *ppc_tm_cvmx,
12538
         int size)
12539
0
{
12540
0
  return elfcore_write_note (abfd, buf, bufsiz,
12541
0
           NOTE_NAME_LINUX, NT_PPC_TM_CVMX,
12542
0
           ppc_tm_cvmx, size);
12543
0
}
12544
12545
char *
12546
elfcore_write_ppc_tm_cvsx (bfd *abfd,
12547
         char *buf,
12548
         int *bufsiz,
12549
         const void *ppc_tm_cvsx,
12550
         int size)
12551
0
{
12552
0
  return elfcore_write_note (abfd, buf, bufsiz,
12553
0
           NOTE_NAME_LINUX, NT_PPC_TM_CVSX,
12554
0
           ppc_tm_cvsx, size);
12555
0
}
12556
12557
char *
12558
elfcore_write_ppc_tm_spr (bfd *abfd,
12559
        char *buf,
12560
        int *bufsiz,
12561
        const void *ppc_tm_spr,
12562
        int size)
12563
0
{
12564
0
  return elfcore_write_note (abfd, buf, bufsiz,
12565
0
           NOTE_NAME_LINUX, NT_PPC_TM_SPR,
12566
0
           ppc_tm_spr, size);
12567
0
}
12568
12569
char *
12570
elfcore_write_ppc_tm_ctar (bfd *abfd,
12571
         char *buf,
12572
         int *bufsiz,
12573
         const void *ppc_tm_ctar,
12574
         int size)
12575
0
{
12576
0
  return elfcore_write_note (abfd, buf, bufsiz,
12577
0
           NOTE_NAME_LINUX, NT_PPC_TM_CTAR,
12578
0
           ppc_tm_ctar, size);
12579
0
}
12580
12581
char *
12582
elfcore_write_ppc_tm_cppr (bfd *abfd,
12583
         char *buf,
12584
         int *bufsiz,
12585
         const void *ppc_tm_cppr,
12586
         int size)
12587
0
{
12588
0
  return elfcore_write_note (abfd, buf, bufsiz,
12589
0
           NOTE_NAME_LINUX, NT_PPC_TM_CPPR,
12590
0
           ppc_tm_cppr, size);
12591
0
}
12592
12593
char *
12594
elfcore_write_ppc_tm_cdscr (bfd *abfd,
12595
          char *buf,
12596
          int *bufsiz,
12597
          const void *ppc_tm_cdscr,
12598
          int size)
12599
0
{
12600
0
  return elfcore_write_note (abfd, buf, bufsiz,
12601
0
           NOTE_NAME_LINUX, NT_PPC_TM_CDSCR,
12602
0
           ppc_tm_cdscr, size);
12603
0
}
12604
12605
static char *
12606
elfcore_write_s390_high_gprs (bfd *abfd,
12607
            char *buf,
12608
            int *bufsiz,
12609
            const void *s390_high_gprs,
12610
            int size)
12611
0
{
12612
0
  return elfcore_write_note (abfd, buf, bufsiz,
12613
0
           NOTE_NAME_LINUX, NT_S390_HIGH_GPRS,
12614
0
           s390_high_gprs, size);
12615
0
}
12616
12617
char *
12618
elfcore_write_s390_timer (bfd *abfd,
12619
        char *buf,
12620
        int *bufsiz,
12621
        const void *s390_timer,
12622
        int size)
12623
0
{
12624
0
  return elfcore_write_note (abfd, buf, bufsiz,
12625
0
           NOTE_NAME_LINUX, NT_S390_TIMER,
12626
0
           s390_timer, size);
12627
0
}
12628
12629
char *
12630
elfcore_write_s390_todcmp (bfd *abfd,
12631
         char *buf,
12632
         int *bufsiz,
12633
         const void *s390_todcmp,
12634
         int size)
12635
0
{
12636
0
  return elfcore_write_note (abfd, buf, bufsiz,
12637
0
           NOTE_NAME_LINUX, NT_S390_TODCMP,
12638
0
           s390_todcmp, size);
12639
0
}
12640
12641
char *
12642
elfcore_write_s390_todpreg (bfd *abfd,
12643
          char *buf,
12644
          int *bufsiz,
12645
          const void *s390_todpreg,
12646
          int size)
12647
0
{
12648
0
  return elfcore_write_note (abfd, buf, bufsiz,
12649
0
           NOTE_NAME_LINUX, NT_S390_TODPREG,
12650
0
           s390_todpreg, size);
12651
0
}
12652
12653
char *
12654
elfcore_write_s390_ctrs (bfd *abfd,
12655
       char *buf,
12656
       int *bufsiz,
12657
       const void *s390_ctrs,
12658
       int size)
12659
0
{
12660
0
  return elfcore_write_note (abfd, buf, bufsiz,
12661
0
           NOTE_NAME_LINUX, NT_S390_CTRS, s390_ctrs, size);
12662
0
}
12663
12664
char *
12665
elfcore_write_s390_prefix (bfd *abfd,
12666
         char *buf,
12667
         int *bufsiz,
12668
         const void *s390_prefix,
12669
         int size)
12670
0
{
12671
0
  return elfcore_write_note (abfd, buf, bufsiz,
12672
0
           NOTE_NAME_LINUX, NT_S390_PREFIX,
12673
0
           s390_prefix, size);
12674
0
}
12675
12676
char *
12677
elfcore_write_s390_last_break (bfd *abfd,
12678
             char *buf,
12679
             int *bufsiz,
12680
             const void *s390_last_break,
12681
             int size)
12682
0
{
12683
0
  return elfcore_write_note (abfd, buf, bufsiz,
12684
0
           NOTE_NAME_LINUX, NT_S390_LAST_BREAK,
12685
0
           s390_last_break, size);
12686
0
}
12687
12688
char *
12689
elfcore_write_s390_system_call (bfd *abfd,
12690
        char *buf,
12691
        int *bufsiz,
12692
        const void *s390_system_call,
12693
        int size)
12694
0
{
12695
0
  return elfcore_write_note (abfd, buf, bufsiz,
12696
0
           NOTE_NAME_LINUX, NT_S390_SYSTEM_CALL,
12697
0
           s390_system_call, size);
12698
0
}
12699
12700
char *
12701
elfcore_write_s390_tdb (bfd *abfd,
12702
      char *buf,
12703
      int *bufsiz,
12704
      const void *s390_tdb,
12705
      int size)
12706
0
{
12707
0
  return elfcore_write_note (abfd, buf, bufsiz,
12708
0
           NOTE_NAME_LINUX, NT_S390_TDB, s390_tdb, size);
12709
0
}
12710
12711
char *
12712
elfcore_write_s390_vxrs_low (bfd *abfd,
12713
           char *buf,
12714
           int *bufsiz,
12715
           const void *s390_vxrs_low,
12716
           int size)
12717
0
{
12718
0
  return elfcore_write_note (abfd, buf, bufsiz,
12719
0
           NOTE_NAME_LINUX, NT_S390_VXRS_LOW,
12720
0
           s390_vxrs_low, size);
12721
0
}
12722
12723
char *
12724
elfcore_write_s390_vxrs_high (bfd *abfd,
12725
           char *buf,
12726
           int *bufsiz,
12727
           const void *s390_vxrs_high,
12728
           int size)
12729
0
{
12730
0
  return elfcore_write_note (abfd, buf, bufsiz,
12731
0
           NOTE_NAME_LINUX, NT_S390_VXRS_HIGH,
12732
0
           s390_vxrs_high, size);
12733
0
}
12734
12735
char *
12736
elfcore_write_s390_gs_cb (bfd *abfd,
12737
        char *buf,
12738
        int *bufsiz,
12739
        const void *s390_gs_cb,
12740
        int size)
12741
0
{
12742
0
  return elfcore_write_note (abfd, buf, bufsiz,
12743
0
           NOTE_NAME_LINUX, NT_S390_GS_CB,
12744
0
           s390_gs_cb, size);
12745
0
}
12746
12747
char *
12748
elfcore_write_s390_gs_bc (bfd *abfd,
12749
        char *buf,
12750
        int *bufsiz,
12751
        const void *s390_gs_bc,
12752
        int size)
12753
0
{
12754
0
  return elfcore_write_note (abfd, buf, bufsiz,
12755
0
           NOTE_NAME_LINUX, NT_S390_GS_BC,
12756
0
           s390_gs_bc, size);
12757
0
}
12758
12759
char *
12760
elfcore_write_arm_vfp (bfd *abfd,
12761
           char *buf,
12762
           int *bufsiz,
12763
           const void *arm_vfp,
12764
           int size)
12765
0
{
12766
0
  return elfcore_write_note (abfd, buf, bufsiz,
12767
0
           NOTE_NAME_LINUX, NT_ARM_VFP,
12768
0
           arm_vfp, size);
12769
0
}
12770
12771
char *
12772
elfcore_write_aarch_tls (bfd *abfd,
12773
           char *buf,
12774
           int *bufsiz,
12775
           const void *aarch_tls,
12776
           int size)
12777
0
{
12778
0
  return elfcore_write_note (abfd, buf, bufsiz,
12779
0
           NOTE_NAME_LINUX, NT_ARM_TLS, aarch_tls, size);
12780
0
}
12781
12782
char *
12783
elfcore_write_aarch_hw_break (bfd *abfd,
12784
          char *buf,
12785
          int *bufsiz,
12786
          const void *aarch_hw_break,
12787
          int size)
12788
0
{
12789
0
  return elfcore_write_note (abfd, buf, bufsiz,
12790
0
           NOTE_NAME_LINUX, NT_ARM_HW_BREAK,
12791
0
           aarch_hw_break, size);
12792
0
}
12793
12794
char *
12795
elfcore_write_aarch_hw_watch (bfd *abfd,
12796
          char *buf,
12797
          int *bufsiz,
12798
          const void *aarch_hw_watch,
12799
          int size)
12800
0
{
12801
0
  return elfcore_write_note (abfd, buf, bufsiz,
12802
0
           NOTE_NAME_LINUX, NT_ARM_HW_WATCH,
12803
0
           aarch_hw_watch, size);
12804
0
}
12805
12806
char *
12807
elfcore_write_aarch_sve (bfd *abfd,
12808
       char *buf,
12809
       int *bufsiz,
12810
       const void *aarch_sve,
12811
       int size)
12812
0
{
12813
0
  return elfcore_write_note (abfd, buf, bufsiz,
12814
0
           NOTE_NAME_LINUX, NT_ARM_SVE, aarch_sve, size);
12815
0
}
12816
12817
char *
12818
elfcore_write_aarch_pauth (bfd *abfd,
12819
         char *buf,
12820
         int *bufsiz,
12821
         const void *aarch_pauth,
12822
         int size)
12823
0
{
12824
0
  return elfcore_write_note (abfd, buf, bufsiz,
12825
0
           NOTE_NAME_LINUX, NT_ARM_PAC_MASK,
12826
0
           aarch_pauth, size);
12827
0
}
12828
12829
char *
12830
elfcore_write_aarch_mte (bfd *abfd,
12831
       char *buf,
12832
       int *bufsiz,
12833
       const void *aarch_mte,
12834
       int size)
12835
0
{
12836
0
  return elfcore_write_note (abfd, buf, bufsiz,
12837
0
           NOTE_NAME_LINUX, NT_ARM_TAGGED_ADDR_CTRL,
12838
0
           aarch_mte, size);
12839
0
}
12840
12841
char *
12842
elfcore_write_aarch_ssve (bfd *abfd,
12843
        char *buf,
12844
        int *bufsiz,
12845
        const void *aarch_ssve,
12846
        int size)
12847
0
{
12848
0
  return elfcore_write_note (abfd, buf, bufsiz,
12849
0
           NOTE_NAME_LINUX, NT_ARM_SSVE,
12850
0
           aarch_ssve, size);
12851
0
}
12852
12853
char *
12854
elfcore_write_aarch_za (bfd *abfd,
12855
      char *buf,
12856
      int *bufsiz,
12857
      const void *aarch_za,
12858
      int size)
12859
0
{
12860
0
  return elfcore_write_note (abfd, buf, bufsiz,
12861
0
           NOTE_NAME_LINUX, NT_ARM_ZA,
12862
0
           aarch_za, size);
12863
0
}
12864
12865
/* Write the buffer of zt register values in aarch_zt (length SIZE) into
12866
   the note buffer BUF and update *BUFSIZ.  ABFD is the bfd the note is being
12867
   written into.  Return a pointer to the new start of the note buffer, to
12868
   replace BUF which may no longer be valid.  */
12869
12870
char *
12871
elfcore_write_aarch_zt (bfd *abfd,
12872
      char *buf,
12873
      int *bufsiz,
12874
      const void *aarch_zt,
12875
      int size)
12876
0
{
12877
0
  return elfcore_write_note (abfd, buf, bufsiz,
12878
0
           NOTE_NAME_LINUX, NT_ARM_ZT,
12879
0
           aarch_zt, size);
12880
0
}
12881
12882
/* Write the buffer of GCS register values in AARCH_GCS (length SIZE) into
12883
   the note buffer BUF and update *BUFSIZ.  ABFD is the bfd the note is being
12884
   written into.  Return a pointer to the new start of the note buffer, to
12885
   replace BUF which may no longer be valid.  */
12886
12887
static char *
12888
elfcore_write_aarch_gcs (bfd *abfd, char *buf, int *bufsiz,
12889
       const void *aarch_gcs, int size)
12890
0
{
12891
0
  return elfcore_write_note (abfd, buf, bufsiz, NOTE_NAME_LINUX, NT_ARM_GCS,
12892
0
           aarch_gcs, size);
12893
0
}
12894
12895
/* Write the buffer of POE register values in AARCH_POE (length SIZE) into
12896
   the note buffer BUF and update *BUFSIZ.  ABFD is the bfd the note is being
12897
   written into.  Return a pointer to the new start of the note buffer, to
12898
   replace BUF which may no longer be valid.  */
12899
12900
static char *
12901
elfcore_write_aarch_poe (bfd *abfd, char *buf, int *bufsiz,
12902
       const void *aarch_poe, int size)
12903
0
{
12904
0
  return elfcore_write_note (abfd, buf, bufsiz, NOTE_NAME_LINUX, NT_ARM_POE,
12905
0
           aarch_poe, size);
12906
0
}
12907
12908
/* Write the buffer of FPMR value in AARCH_FPMR (length SIZE) into
12909
   the note buffer BUF and update *BUFSIZ.  ABFD is the bfd the note is being
12910
   written into.  Return a pointer to the new start of the note buffer, to
12911
   replace BUF which may no longer be valid.  */
12912
12913
char *
12914
elfcore_write_aarch_fpmr (bfd *abfd,
12915
      char *buf,
12916
      int *bufsiz,
12917
      const void *aarch_fpmr,
12918
      int size)
12919
0
{
12920
0
  return elfcore_write_note (abfd, buf, bufsiz,
12921
0
           NOTE_NAME_LINUX, NT_ARM_FPMR, aarch_fpmr, size);
12922
0
}
12923
12924
char *
12925
elfcore_write_arc_v2 (bfd *abfd,
12926
          char *buf,
12927
          int *bufsiz,
12928
          const void *arc_v2,
12929
          int size)
12930
0
{
12931
0
  return elfcore_write_note (abfd, buf, bufsiz,
12932
0
           NOTE_NAME_LINUX, NT_ARC_V2, arc_v2, size);
12933
0
}
12934
12935
char *
12936
elfcore_write_loongarch_cpucfg (bfd *abfd,
12937
        char *buf,
12938
        int *bufsiz,
12939
        const void *loongarch_cpucfg,
12940
        int size)
12941
0
{
12942
0
  return elfcore_write_note (abfd, buf, bufsiz,
12943
0
           NOTE_NAME_LINUX, NT_LARCH_CPUCFG,
12944
0
           loongarch_cpucfg, size);
12945
0
}
12946
12947
char *
12948
elfcore_write_loongarch_lbt (bfd *abfd,
12949
           char *buf,
12950
           int *bufsiz,
12951
           const void *loongarch_lbt,
12952
           int size)
12953
0
{
12954
0
  return elfcore_write_note (abfd, buf, bufsiz,
12955
0
           NOTE_NAME_LINUX, NT_LARCH_LBT,
12956
0
           loongarch_lbt, size);
12957
0
}
12958
12959
char *
12960
elfcore_write_loongarch_lsx (bfd *abfd,
12961
           char *buf,
12962
           int *bufsiz,
12963
           const void *loongarch_lsx,
12964
           int size)
12965
0
{
12966
0
  return elfcore_write_note (abfd, buf, bufsiz,
12967
0
           NOTE_NAME_LINUX, NT_LARCH_LSX,
12968
0
           loongarch_lsx, size);
12969
0
}
12970
12971
char *
12972
elfcore_write_loongarch_lasx (bfd *abfd,
12973
            char *buf,
12974
            int *bufsiz,
12975
            const void *loongarch_lasx,
12976
            int size)
12977
0
{
12978
0
  return elfcore_write_note (abfd, buf, bufsiz,
12979
0
           NOTE_NAME_LINUX, NT_LARCH_LASX,
12980
0
           loongarch_lasx, size);
12981
0
}
12982
12983
/* Write the buffer of csr values in CSRS (length SIZE) into the note
12984
   buffer BUF and update *BUFSIZ.  ABFD is the bfd the note is being
12985
   written into.  Return a pointer to the new start of the note buffer, to
12986
   replace BUF which may no longer be valid.  */
12987
12988
char *
12989
elfcore_write_riscv_csr (bfd *abfd,
12990
       char *buf,
12991
       int *bufsiz,
12992
       const void *csrs,
12993
       int size)
12994
0
{
12995
0
  return elfcore_write_note (abfd, buf, bufsiz,
12996
0
           NOTE_NAME_GDB, NT_RISCV_CSR, csrs, size);
12997
0
}
12998
12999
/* Write the target description (a string) pointed to by TDESC, length
13000
   SIZE, into the note buffer BUF, and update *BUFSIZ.  ABFD is the bfd the
13001
   note is being written into.  Return a pointer to the new start of the
13002
   note buffer, to replace BUF which may no longer be valid.  */
13003
13004
char *
13005
elfcore_write_gdb_tdesc (bfd *abfd,
13006
       char *buf,
13007
       int *bufsiz,
13008
       const void *tdesc,
13009
       int size)
13010
0
{
13011
0
  return elfcore_write_note (abfd, buf, bufsiz,
13012
0
           NOTE_NAME_GDB, NT_GDB_TDESC, tdesc, size);
13013
0
}
13014
13015
char *
13016
elfcore_write_register_note (bfd *abfd,
13017
           char *buf,
13018
           int *bufsiz,
13019
           const char *section,
13020
           const void *data,
13021
           int size)
13022
0
{
13023
0
  static const struct
13024
0
    {
13025
0
      const char * section_name;
13026
0
      char *       (*writer) (bfd *, char *, int *, const void *, int);
13027
0
    }
13028
0
  note_writers [] =
13029
0
    {
13030
0
      { NOTE_PSEUDO_SECTION_AARCH_FPMR,       elfcore_write_aarch_fpmr},
13031
0
      { NOTE_PSEUDO_SECTION_AARCH_GCS,        elfcore_write_aarch_gcs},
13032
0
      { NOTE_PSEUDO_SECTION_AARCH_POE,        elfcore_write_aarch_poe},
13033
0
      { NOTE_PSEUDO_SECTION_AARCH_HW_BREAK,   elfcore_write_aarch_hw_break},
13034
0
      { NOTE_PSEUDO_SECTION_AARCH_HW_WATCH,   elfcore_write_aarch_hw_watch},
13035
0
      { NOTE_PSEUDO_SECTION_AARCH_MTE,        elfcore_write_aarch_mte},
13036
0
      { NOTE_PSEUDO_SECTION_AARCH_PAUTH,      elfcore_write_aarch_pauth},
13037
0
      { NOTE_PSEUDO_SECTION_AARCH_SSVE,       elfcore_write_aarch_ssve},
13038
0
      { NOTE_PSEUDO_SECTION_AARCH_SVE,        elfcore_write_aarch_sve},
13039
0
      { NOTE_PSEUDO_SECTION_AARCH_TLS,        elfcore_write_aarch_tls},
13040
0
      { NOTE_PSEUDO_SECTION_AARCH_ZA,         elfcore_write_aarch_za},
13041
0
      { NOTE_PSEUDO_SECTION_AARCH_ZT,         elfcore_write_aarch_zt},
13042
0
      { NOTE_PSEUDO_SECTION_ARC_V2,           elfcore_write_arc_v2},
13043
0
      { NOTE_PSEUDO_SECTION_ARM_VFP,          elfcore_write_arm_vfp},
13044
0
      { NOTE_PSEUDO_SECTION_I386_TLS,         elfcore_write_i386_tls},
13045
0
      { NOTE_PSEUDO_SECTION_LOONGARCH_CPUCFG, elfcore_write_loongarch_cpucfg},
13046
0
      { NOTE_PSEUDO_SECTION_LOONGARCH_LASX,   elfcore_write_loongarch_lasx},
13047
0
      { NOTE_PSEUDO_SECTION_LOONGARCH_LBT,    elfcore_write_loongarch_lbt},
13048
0
      { NOTE_PSEUDO_SECTION_LOONGARCH_LSX,    elfcore_write_loongarch_lsx},
13049
0
      { NOTE_PSEUDO_SECTION_PPC_DSCR,         elfcore_write_ppc_dscr},
13050
0
      { NOTE_PSEUDO_SECTION_PPC_EBB,          elfcore_write_ppc_ebb},
13051
0
      { NOTE_PSEUDO_SECTION_PPC_PMU,          elfcore_write_ppc_pmu},
13052
0
      { NOTE_PSEUDO_SECTION_PPC_PPR,          elfcore_write_ppc_ppr},
13053
0
      { NOTE_PSEUDO_SECTION_PPC_TAR,          elfcore_write_ppc_tar},
13054
0
      { NOTE_PSEUDO_SECTION_PPC_TM_CDSCR,     elfcore_write_ppc_tm_cdscr},
13055
0
      { NOTE_PSEUDO_SECTION_PPC_TM_CFPR,      elfcore_write_ppc_tm_cfpr},
13056
0
      { NOTE_PSEUDO_SECTION_PPC_TM_CGPR,      elfcore_write_ppc_tm_cgpr},
13057
0
      { NOTE_PSEUDO_SECTION_PPC_TM_CPPR,      elfcore_write_ppc_tm_cppr},
13058
0
      { NOTE_PSEUDO_SECTION_PPC_TM_CTAR,      elfcore_write_ppc_tm_ctar},
13059
0
      { NOTE_PSEUDO_SECTION_PPC_TM_CVMX,      elfcore_write_ppc_tm_cvmx},
13060
0
      { NOTE_PSEUDO_SECTION_PPC_TM_CVSX,      elfcore_write_ppc_tm_cvsx},
13061
0
      { NOTE_PSEUDO_SECTION_PPC_TM_SPR,       elfcore_write_ppc_tm_spr},
13062
0
      { NOTE_PSEUDO_SECTION_PPC_VMX,          elfcore_write_ppc_vmx},
13063
0
      { NOTE_PSEUDO_SECTION_PPC_VSX,          elfcore_write_ppc_vsx},
13064
0
      { NOTE_PSEUDO_SECTION_REG2,             elfcore_write_prfpreg},
13065
0
      { NOTE_PSEUDO_SECTION_RISCV_CSR,        elfcore_write_riscv_csr},
13066
0
      { NOTE_PSEUDO_SECTION_S390_CTRS,        elfcore_write_s390_ctrs},
13067
0
      { NOTE_PSEUDO_SECTION_S390_GS_BC,       elfcore_write_s390_gs_bc},
13068
0
      { NOTE_PSEUDO_SECTION_S390_GS_CB,       elfcore_write_s390_gs_cb},
13069
0
      { NOTE_PSEUDO_SECTION_S390_HIGH_GPRS,   elfcore_write_s390_high_gprs},
13070
0
      { NOTE_PSEUDO_SECTION_S390_LAST_BREAK,  elfcore_write_s390_last_break},
13071
0
      { NOTE_PSEUDO_SECTION_S390_PREFIX,      elfcore_write_s390_prefix},
13072
0
      { NOTE_PSEUDO_SECTION_S390_SYSTEM_CALL, elfcore_write_s390_system_call},
13073
0
      { NOTE_PSEUDO_SECTION_S390_TDB,         elfcore_write_s390_tdb},
13074
0
      { NOTE_PSEUDO_SECTION_S390_TIMER,       elfcore_write_s390_timer},
13075
0
      { NOTE_PSEUDO_SECTION_S390_TODCMP,      elfcore_write_s390_todcmp},
13076
0
      { NOTE_PSEUDO_SECTION_S390_TODPREG,     elfcore_write_s390_todpreg},
13077
0
      { NOTE_PSEUDO_SECTION_S390_VXRS_HIGH,   elfcore_write_s390_vxrs_high},
13078
0
      { NOTE_PSEUDO_SECTION_S390_VXRS_LOW,    elfcore_write_s390_vxrs_low},
13079
0
      { NOTE_PSEUDO_SECTION_SSP,              elfcore_write_sspreg},
13080
0
      { NOTE_PSEUDO_SECTION_TDESC,            elfcore_write_gdb_tdesc},
13081
0
      { NOTE_PSEUDO_SECTION_X86_SEGBASES,     elfcore_write_x86_segbases},
13082
0
      { NOTE_PSEUDO_SECTION_XFP,              elfcore_write_prxfpreg},
13083
0
      { NOTE_PSEUDO_SECTION_XSTATE,           elfcore_write_xstatereg},
13084
0
      { NOTE_PSEUDO_SECTION_XSAVE_LAYOUT,     elfcore_write_xsave_layout} /* NB/ No comma.  */
13085
0
    };
13086
13087
0
  int i;
13088
13089
0
  for (i = ARRAY_SIZE (note_writers); i--;)
13090
0
    if (streq (section, note_writers[i].section_name))
13091
0
      return  note_writers[i].writer (abfd, buf, bufsiz, data, size);
13092
13093
0
  return NULL;
13094
0
}
13095
13096
char *
13097
elfcore_write_file_note (bfd *obfd, char *note_data, int *note_size,
13098
       const void *buf, int bufsiz)
13099
0
{
13100
0
  return elfcore_write_note (obfd, note_data, note_size,
13101
0
           NOTE_NAME_CORE, NT_FILE, buf, bufsiz);
13102
0
}
13103
13104
static bool
13105
elf_parse_notes (bfd *abfd, char *buf, size_t size, file_ptr offset,
13106
     size_t align)
13107
8.75k
{
13108
8.75k
  char *p;
13109
13110
  /* NB: CORE PT_NOTE segments may have p_align values of 0 or 1.
13111
     gABI specifies that PT_NOTE alignment should be aligned to 4
13112
     bytes for 32-bit objects and to 8 bytes for 64-bit objects.  If
13113
     align is less than 4, we use 4 byte alignment.   */
13114
8.75k
  if (align < 4)
13115
3.19k
    align = 4;
13116
8.75k
  if (align != 4 && align != 8)
13117
2.18k
    return false;
13118
13119
6.57k
  p = buf;
13120
19.2k
  while (p < buf + size)
13121
18.1k
    {
13122
18.1k
      Elf_External_Note *xnp = (Elf_External_Note *) p;
13123
18.1k
      Elf_Internal_Note in;
13124
13125
18.1k
      if (offsetof (Elf_External_Note, name) > buf - p + size)
13126
1.06k
  return false;
13127
13128
17.0k
      in.type = H_GET_32 (abfd, xnp->type);
13129
13130
17.0k
      in.namesz = H_GET_32 (abfd, xnp->namesz);
13131
17.0k
      in.namedata = xnp->name;
13132
17.0k
      if (in.namesz > buf - in.namedata + size)
13133
2.54k
  return false;
13134
13135
14.5k
      in.descsz = H_GET_32 (abfd, xnp->descsz);
13136
14.5k
      in.descdata = p + ELF_NOTE_DESC_OFFSET (in.namesz, align);
13137
14.5k
      in.descpos = offset + (in.descdata - buf);
13138
14.5k
      if (in.descsz != 0
13139
6.37k
    && (in.descdata >= buf + size
13140
6.09k
        || in.descsz > buf - in.descdata + size))
13141
1.42k
  return false;
13142
13143
13.0k
      switch (bfd_get_format (abfd))
13144
13.0k
  {
13145
0
  default:
13146
0
    return true;
13147
13148
4.54k
  case bfd_core:
13149
4.54k
    {
13150
36.3k
#define GROKER_ELEMENT(S,F) {S, sizeof (S) - 1, F}
13151
4.54k
      static const struct
13152
4.54k
      {
13153
4.54k
        const char * string;
13154
4.54k
        size_t len;
13155
4.54k
        bool (*func) (bfd *, Elf_Internal_Note *);
13156
4.54k
      }
13157
4.54k
      grokers[] =
13158
4.54k
      {
13159
4.54k
        GROKER_ELEMENT ("", elfcore_grok_note),
13160
4.54k
        GROKER_ELEMENT (NOTE_NAME_FREEBSD, elfcore_grok_freebsd_note),
13161
4.54k
        GROKER_ELEMENT ("NetBSD-CORE", elfcore_grok_netbsd_note),
13162
4.54k
        GROKER_ELEMENT ("OpenBSD", elfcore_grok_openbsd_note),
13163
4.54k
        GROKER_ELEMENT ("QNX", elfcore_grok_nto_note),
13164
4.54k
        GROKER_ELEMENT ("SPU/", elfcore_grok_spu_note),
13165
4.54k
        GROKER_ELEMENT ("GNU", elfobj_grok_gnu_note),
13166
4.54k
        GROKER_ELEMENT (NOTE_NAME_CORE, elfcore_grok_solaris_note)
13167
4.54k
      };
13168
4.54k
#undef GROKER_ELEMENT
13169
4.54k
      int i;
13170
13171
34.1k
      for (i = ARRAY_SIZE (grokers); i--;)
13172
34.1k
        {
13173
34.1k
    if (in.namesz >= grokers[i].len
13174
13.4k
        && strncmp (in.namedata, grokers[i].string,
13175
13.4k
        grokers[i].len) == 0)
13176
4.54k
      {
13177
4.54k
        if (! grokers[i].func (abfd, & in))
13178
145
          return false;
13179
4.39k
        break;
13180
4.54k
      }
13181
34.1k
        }
13182
4.39k
      break;
13183
4.54k
    }
13184
13185
8.53k
  case bfd_object:
13186
8.53k
    if (in.namesz == sizeof "GNU" && strcmp (in.namedata, "GNU") == 0)
13187
1.25k
      {
13188
1.25k
        if (! elfobj_grok_gnu_note (abfd, &in))
13189
275
    return false;
13190
1.25k
      }
13191
7.28k
    else if (in.namesz == sizeof "stapsdt"
13192
463
       && strcmp (in.namedata, "stapsdt") == 0)
13193
0
      {
13194
0
        if (! elfobj_grok_stapsdt_note (abfd, &in))
13195
0
    return false;
13196
0
      }
13197
8.26k
    break;
13198
13.0k
  }
13199
13200
12.6k
      p += ELF_NOTE_NEXT_OFFSET (in.namesz, in.descsz, align);
13201
12.6k
    }
13202
13203
1.12k
  return true;
13204
6.57k
}
13205
13206
bool
13207
_bfd_elf_read_notes (bfd *abfd, file_ptr offset, bfd_size_type size,
13208
         size_t align)
13209
5.21k
{
13210
5.21k
  char *buf;
13211
13212
5.21k
  if (size == 0 || (size + 1) == 0)
13213
938
    return true;
13214
13215
4.27k
  if (bfd_seek (abfd, offset, SEEK_SET) != 0)
13216
1.16k
    return false;
13217
13218
3.11k
  buf = _bfd_malloc_and_read (abfd, size + 1, size);
13219
3.11k
  if (buf == NULL)
13220
1.32k
    return false;
13221
13222
  /* PR 17512: file: ec08f814
13223
     0-termintate the buffer so that string searches will not overflow.  */
13224
1.79k
  buf[size] = 0;
13225
13226
1.79k
  if (!elf_parse_notes (abfd, buf, size, offset, align))
13227
1.60k
    {
13228
1.60k
      free (buf);
13229
1.60k
      return false;
13230
1.60k
    }
13231
13232
186
  free (buf);
13233
186
  return true;
13234
1.79k
}
13235

13236
/* Providing external access to the ELF program header table.  */
13237
13238
/* Return an upper bound on the number of bytes required to store a
13239
   copy of ABFD's program header table entries.  Return -1 if an error
13240
   occurs; bfd_get_error will return an appropriate code.  */
13241
13242
long
13243
bfd_get_elf_phdr_upper_bound (bfd *abfd)
13244
0
{
13245
0
  if (abfd->xvec->flavour != bfd_target_elf_flavour)
13246
0
    {
13247
0
      bfd_set_error (bfd_error_wrong_format);
13248
0
      return -1;
13249
0
    }
13250
13251
0
  return elf_elfheader (abfd)->e_phnum * sizeof (Elf_Internal_Phdr);
13252
0
}
13253
13254
/* Copy ABFD's program header table entries to *PHDRS.  The entries
13255
   will be stored as an array of Elf_Internal_Phdr structures, as
13256
   defined in include/elf/internal.h.  To find out how large the
13257
   buffer needs to be, call bfd_get_elf_phdr_upper_bound.
13258
13259
   Return the number of program header table entries read, or -1 if an
13260
   error occurs; bfd_get_error will return an appropriate code.  */
13261
13262
int
13263
bfd_get_elf_phdrs (bfd *abfd, void *phdrs)
13264
0
{
13265
0
  int num_phdrs;
13266
13267
0
  if (abfd->xvec->flavour != bfd_target_elf_flavour)
13268
0
    {
13269
0
      bfd_set_error (bfd_error_wrong_format);
13270
0
      return -1;
13271
0
    }
13272
13273
0
  num_phdrs = elf_elfheader (abfd)->e_phnum;
13274
0
  if (num_phdrs != 0)
13275
0
    memcpy (phdrs, elf_tdata (abfd)->phdr,
13276
0
      num_phdrs * sizeof (Elf_Internal_Phdr));
13277
13278
0
  return num_phdrs;
13279
0
}
13280
13281
enum elf_reloc_type_class
13282
_bfd_elf_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
13283
         const asection *rel_sec ATTRIBUTE_UNUSED,
13284
         const Elf_Internal_Rela *rela ATTRIBUTE_UNUSED)
13285
0
{
13286
0
  return reloc_class_normal;
13287
0
}
13288
13289
/* For RELA architectures, return the relocation value for a
13290
   relocation against a local symbol.  */
13291
13292
bfd_vma
13293
_bfd_elf_rela_local_sym (bfd *abfd,
13294
       Elf_Internal_Sym *sym,
13295
       asection **psec,
13296
       Elf_Internal_Rela *rel)
13297
0
{
13298
0
  asection *sec = *psec;
13299
0
  bfd_vma relocation;
13300
13301
0
  relocation = (sec->output_section->vma
13302
0
    + sec->output_offset
13303
0
    + sym->st_value);
13304
0
  if ((sec->flags & SEC_MERGE)
13305
0
      && ELF_ST_TYPE (sym->st_info) == STT_SECTION
13306
0
      && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
13307
0
    {
13308
0
      rel->r_addend =
13309
0
  _bfd_merged_section_offset (abfd, psec,
13310
0
            sym->st_value + rel->r_addend);
13311
0
      if (sec != *psec)
13312
0
  {
13313
    /* If we have changed the section, and our original section is
13314
       marked with SEC_EXCLUDE, it means that the original
13315
       SEC_MERGE section has been completely subsumed in some
13316
       other SEC_MERGE section.  In this case, we need to leave
13317
       some info around for --emit-relocs.  */
13318
0
    if ((sec->flags & SEC_EXCLUDE) != 0)
13319
0
      sec->kept_section = *psec;
13320
0
    sec = *psec;
13321
0
  }
13322
0
      rel->r_addend -= relocation;
13323
0
      rel->r_addend += sec->output_section->vma + sec->output_offset;
13324
0
    }
13325
0
  return relocation;
13326
0
}
13327
13328
bfd_vma
13329
_bfd_elf_rel_local_sym (bfd *abfd,
13330
      Elf_Internal_Sym *sym,
13331
      asection **psec,
13332
      bfd_vma addend)
13333
0
{
13334
0
  asection *sec = *psec;
13335
13336
0
  if (sec->sec_info_type != SEC_INFO_TYPE_MERGE)
13337
0
    return sym->st_value + addend;
13338
13339
0
  return _bfd_merged_section_offset (abfd, psec,
13340
0
             sym->st_value + addend);
13341
0
}
13342
13343
/* Adjust an address within a section.  Given OFFSET within SEC, return
13344
   the new offset within the section, based upon changes made to the
13345
   section.  Returns -1 if the offset is now invalid.
13346
   The offset (in abnd out) is in target sized bytes, however big a
13347
   byte may be.  */
13348
13349
bfd_vma
13350
_bfd_elf_section_offset (bfd *abfd,
13351
       struct bfd_link_info *info,
13352
       asection *sec,
13353
       bfd_vma offset)
13354
0
{
13355
0
  switch (sec->sec_info_type)
13356
0
    {
13357
0
    case SEC_INFO_TYPE_STABS:
13358
0
      return _bfd_stab_section_offset (sec, offset);
13359
13360
0
    case SEC_INFO_TYPE_EH_FRAME:
13361
0
      return _bfd_elf_eh_frame_section_offset (abfd, info, sec, offset);
13362
13363
0
    case SEC_INFO_TYPE_SFRAME:
13364
0
      return _bfd_elf_sframe_section_offset (abfd, info, sec, offset);
13365
13366
0
    default:
13367
0
      if ((sec->flags & SEC_ELF_REVERSE_COPY) != 0)
13368
0
  {
13369
    /* Reverse the offset.  */
13370
0
    elf_backend_data *bed = get_elf_backend_data (abfd);
13371
0
    bfd_size_type address_size = bed->s->arch_size / 8;
13372
13373
    /* address_size and sec->size are in octets.  Convert
13374
       to bytes before subtracting the original offset.  */
13375
0
    offset = ((sec->size - address_size)
13376
0
        / bfd_octets_per_byte (abfd, sec) - offset);
13377
0
  }
13378
0
      return offset;
13379
0
    }
13380
0
}
13381

13382
long
13383
_bfd_elf_get_synthetic_symtab (bfd *abfd,
13384
             long symcount ATTRIBUTE_UNUSED,
13385
             asymbol **syms ATTRIBUTE_UNUSED,
13386
             long dynsymcount,
13387
             asymbol **dynsyms,
13388
             asymbol **ret)
13389
15.2k
{
13390
15.2k
  elf_backend_data *bed = get_elf_backend_data (abfd);
13391
15.2k
  asection *relplt;
13392
15.2k
  asymbol *s;
13393
15.2k
  const char *relplt_name;
13394
15.2k
  bool (*slurp_relocs) (bfd *, asection *, asymbol **, bool);
13395
15.2k
  arelent *p;
13396
15.2k
  long count, i, n;
13397
15.2k
  size_t size;
13398
15.2k
  Elf_Internal_Shdr *hdr;
13399
15.2k
  char *names;
13400
15.2k
  asection *plt;
13401
13402
15.2k
  *ret = NULL;
13403
13404
15.2k
  if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0)
13405
15.2k
    return 0;
13406
13407
61
  if (dynsymcount <= 0)
13408
55
    return 0;
13409
13410
6
  if (!bed->plt_sym_val)
13411
4
    return 0;
13412
13413
2
  relplt_name = bed->relplt_name;
13414
2
  if (relplt_name == NULL)
13415
2
    relplt_name = bed->rela_plts_and_copies_p ? ".rela.plt" : ".rel.plt";
13416
2
  relplt = bfd_get_section_by_name (abfd, relplt_name);
13417
2
  if (relplt == NULL)
13418
2
    return 0;
13419
13420
0
  hdr = &elf_section_data (relplt)->this_hdr;
13421
0
  if (hdr->sh_link != elf_dynsymtab (abfd)
13422
0
      || (hdr->sh_type != SHT_REL && hdr->sh_type != SHT_RELA))
13423
0
    return 0;
13424
13425
0
  plt = bfd_get_section_by_name (abfd, ".plt");
13426
0
  if (plt == NULL)
13427
0
    return 0;
13428
13429
0
  slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
13430
0
  if (! (*slurp_relocs) (abfd, relplt, dynsyms, true))
13431
0
    return -1;
13432
13433
0
  count = NUM_SHDR_ENTRIES (hdr);
13434
0
  size = count * sizeof (asymbol);
13435
0
  p = relplt->relocation;
13436
0
  for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
13437
0
    {
13438
0
      size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
13439
0
      if (p->addend != 0)
13440
0
  {
13441
0
#ifdef BFD64
13442
0
    size += sizeof ("+0x") - 1 + 8 + 8 * (bed->s->elfclass == ELFCLASS64);
13443
#else
13444
    size += sizeof ("+0x") - 1 + 8;
13445
#endif
13446
0
  }
13447
0
    }
13448
13449
0
  s = *ret = bfd_malloc (size);
13450
0
  if (s == NULL)
13451
0
    return -1;
13452
13453
0
  names = (char *) (s + count);
13454
0
  p = relplt->relocation;
13455
0
  n = 0;
13456
0
  for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
13457
0
    {
13458
0
      size_t len;
13459
0
      bfd_vma addr;
13460
13461
0
      addr = bed->plt_sym_val (i, plt, p);
13462
0
      if (addr == (bfd_vma) -1)
13463
0
  continue;
13464
13465
0
      *s = **p->sym_ptr_ptr;
13466
      /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set.  Since
13467
   we are defining a symbol, ensure one of them is set.  */
13468
0
      if ((s->flags & BSF_LOCAL) == 0)
13469
0
  s->flags |= BSF_GLOBAL;
13470
0
      s->flags |= BSF_SYNTHETIC;
13471
0
      s->section = plt;
13472
0
      s->value = addr - plt->vma;
13473
0
      s->name = names;
13474
0
      s->udata.p = NULL;
13475
0
      len = strlen ((*p->sym_ptr_ptr)->name);
13476
0
      memcpy (names, (*p->sym_ptr_ptr)->name, len);
13477
0
      names += len;
13478
0
      if (p->addend != 0)
13479
0
  {
13480
0
    char buf[30], *a;
13481
13482
0
    memcpy (names, "+0x", sizeof ("+0x") - 1);
13483
0
    names += sizeof ("+0x") - 1;
13484
0
    bfd_sprintf_vma (abfd, buf, p->addend);
13485
0
    for (a = buf; *a == '0'; ++a)
13486
0
      ;
13487
0
    len = strlen (a);
13488
0
    memcpy (names, a, len);
13489
0
    names += len;
13490
0
  }
13491
0
      memcpy (names, "@plt", sizeof ("@plt"));
13492
0
      names += sizeof ("@plt");
13493
0
      ++s, ++n;
13494
0
    }
13495
13496
0
  return n;
13497
0
}
13498
13499
bool
13500
_bfd_elf_final_write_processing (bfd *abfd)
13501
521
{
13502
521
  Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd);
13503
521
  elf_backend_data *bed = get_elf_backend_data (abfd);
13504
13505
521
  if (bed->osabi_exact && i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE)
13506
5
    i_ehdrp->e_ident[EI_OSABI] = bed->elf_osabi;
13507
13508
521
  if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_SOLARIS
13509
502
      || bed->target_os == is_solaris)
13510
19
    {
13511
19
      elf_tdata (abfd)->strtab_hdr.sh_flags = SHF_STRINGS;
13512
19
      elf_tdata (abfd)->shstrtab_hdr.sh_flags = SHF_STRINGS;
13513
19
    }
13514
13515
  /* Set the osabi field to ELFOSABI_GNU if the binary contains
13516
     SHF_GNU_MBIND or SHF_GNU_RETAIN sections or symbols of STT_GNU_IFUNC type
13517
     or STB_GNU_UNIQUE binding.  */
13518
521
  if (elf_tdata (abfd)->has_gnu_osabi != 0)
13519
0
    {
13520
0
      if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE)
13521
0
  i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_GNU;
13522
0
      else if (i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_GNU
13523
0
         && i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_FREEBSD)
13524
0
  {
13525
0
    if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind)
13526
0
      _bfd_error_handler (_("GNU_MBIND section is supported only by GNU "
13527
0
          "and FreeBSD targets"));
13528
0
    if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_ifunc)
13529
0
      _bfd_error_handler (_("symbol type STT_GNU_IFUNC is supported "
13530
0
          "only by GNU and FreeBSD targets"));
13531
0
    if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_unique)
13532
0
      _bfd_error_handler (_("symbol binding STB_GNU_UNIQUE is supported "
13533
0
          "only by GNU and FreeBSD targets"));
13534
0
    if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_retain)
13535
0
      _bfd_error_handler (_("GNU_RETAIN section is supported "
13536
0
          "only by GNU and FreeBSD targets"));
13537
0
    bfd_set_error (bfd_error_sorry);
13538
0
    return false;
13539
0
  }
13540
0
    }
13541
521
  return true;
13542
521
}
13543
13544
13545
/* Return TRUE for ELF symbol types that represent functions.
13546
   This is the default version of this function, which is sufficient for
13547
   most targets.  It returns true if TYPE is STT_FUNC or STT_GNU_IFUNC.  */
13548
13549
bool
13550
_bfd_elf_is_function_type (unsigned int type)
13551
0
{
13552
0
  return (type == STT_FUNC
13553
0
    || type == STT_GNU_IFUNC);
13554
0
}
13555
13556
/* If the ELF symbol SYM might be a function in SEC, return the
13557
   function size and set *CODE_OFF to the function's entry point,
13558
   otherwise return zero.  */
13559
13560
bfd_size_type
13561
_bfd_elf_maybe_function_sym (const asymbol *sym, asection *sec,
13562
           bfd_vma *code_off)
13563
959k
{
13564
959k
  bfd_size_type size;
13565
959k
  elf_symbol_type * elf_sym = (elf_symbol_type *) sym;
13566
13567
959k
  if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
13568
959k
         | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0
13569
712k
      || sym->section != sec)
13570
942k
    return 0;
13571
13572
16.5k
  size = (sym->flags & BSF_SYNTHETIC) ? 0 : elf_sym->internal_elf_sym.st_size;
13573
13574
  /* In theory we should check that the symbol's type satisfies
13575
     _bfd_elf_is_function_type(), but there are some function-like
13576
     symbols which would fail this test.  (eg _start).  Instead
13577
     we check for hidden, local, notype symbols with zero size.
13578
     This type of symbol is generated by the annobin plugin for gcc
13579
     and clang, and should not be considered to be a function symbol.  */
13580
16.5k
  if (size == 0
13581
2.99k
      && ((sym->flags & (BSF_SYNTHETIC | BSF_LOCAL)) == BSF_LOCAL)
13582
1.91k
      && ELF_ST_TYPE (elf_sym->internal_elf_sym.st_info) == STT_NOTYPE
13583
1.72k
      && ELF_ST_VISIBILITY (elf_sym->internal_elf_sym.st_other) == STV_HIDDEN)
13584
122
    return 0;
13585
13586
16.4k
  *code_off = sym->value;
13587
  /* Do not return 0 for the function's size.  */
13588
16.4k
  return size ? size : 1;
13589
16.5k
}
13590
13591
/* Set to non-zero to enable some debug messages.  */
13592
#define DEBUG_SECONDARY_RELOCS   0
13593
13594
/* An internal-to-the-bfd-library only section type
13595
   used to indicate a cached secondary reloc section.  */
13596
5.43k
#define SHT_SECONDARY_RELOC  (SHT_LOOS + SHT_RELA)
13597
13598
/* Create a BFD section to hold a secondary reloc section.  */
13599
13600
bool
13601
_bfd_elf_init_secondary_reloc_section (bfd * abfd,
13602
               Elf_Internal_Shdr *hdr,
13603
               const char * name,
13604
               unsigned int shindex)
13605
1.71k
{
13606
  /* We only support RELA secondary relocs.  */
13607
1.71k
  if (hdr->sh_type != SHT_RELA)
13608
165
    return false;
13609
13610
#if DEBUG_SECONDARY_RELOCS
13611
  fprintf (stderr, "secondary reloc section %s encountered\n", name);
13612
#endif
13613
1.55k
  hdr->sh_type = SHT_SECONDARY_RELOC;
13614
1.55k
  return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
13615
1.71k
}
13616
13617
/* Read in any secondary relocs associated with SEC.  */
13618
13619
bool
13620
_bfd_elf_slurp_secondary_reloc_section (bfd *       abfd,
13621
          asection *  sec,
13622
          asymbol **  symbols,
13623
          bool dynamic)
13624
11.4k
{
13625
11.4k
  elf_backend_data *ebd = get_elf_backend_data (abfd);
13626
11.4k
  asection * relsec;
13627
11.4k
  bool result = true;
13628
11.4k
  bfd_vma (*r_sym) (bfd_vma);
13629
11.4k
  ufile_ptr filesize;
13630
13631
11.4k
#if BFD_DEFAULT_TARGET_SIZE > 32
13632
11.4k
  if (bfd_arch_bits_per_address (abfd) != 32)
13633
5.70k
    r_sym = elf64_r_sym;
13634
5.78k
  else
13635
5.78k
#endif
13636
5.78k
    r_sym = elf32_r_sym;
13637
13638
11.4k
  if (!elf_section_data (sec)->has_secondary_relocs)
13639
11.3k
    return true;
13640
13641
  /* Discover if there are any secondary reloc sections
13642
     associated with SEC.  */
13643
91
  filesize = bfd_get_file_size (abfd);
13644
2.01k
  for (relsec = abfd->sections; relsec != NULL; relsec = relsec->next)
13645
1.92k
    {
13646
1.92k
      Elf_Internal_Shdr * hdr = & elf_section_data (relsec)->this_hdr;
13647
13648
1.92k
      if (hdr->sh_type == SHT_SECONDARY_RELOC
13649
105
    && hdr->sh_info == (unsigned) elf_section_data (sec)->this_idx
13650
91
    && (hdr->sh_entsize == ebd->s->sizeof_rel
13651
91
        || hdr->sh_entsize == ebd->s->sizeof_rela))
13652
91
  {
13653
91
    bfd_byte * native_relocs;
13654
91
    bfd_byte * native_reloc;
13655
91
    arelent * internal_relocs;
13656
91
    arelent * internal_reloc;
13657
91
    size_t i;
13658
91
    unsigned int entsize;
13659
91
    unsigned int symcount;
13660
91
    bfd_size_type reloc_count;
13661
91
    size_t amt;
13662
13663
91
    if (ebd->elf_info_to_howto == NULL)
13664
0
      return false;
13665
13666
#if DEBUG_SECONDARY_RELOCS
13667
    fprintf (stderr, "read secondary relocs for %s from %s\n",
13668
       sec->name, relsec->name);
13669
#endif
13670
91
    entsize = hdr->sh_entsize;
13671
13672
91
    if (filesize != 0
13673
91
        && ((ufile_ptr) hdr->sh_offset > filesize
13674
77
      || hdr->sh_size > filesize - hdr->sh_offset))
13675
25
      {
13676
25
        bfd_set_error (bfd_error_file_truncated);
13677
25
        result = false;
13678
25
        continue;
13679
25
      }
13680
13681
66
    native_relocs = bfd_malloc (hdr->sh_size);
13682
66
    if (native_relocs == NULL)
13683
0
      {
13684
0
        result = false;
13685
0
        continue;
13686
0
      }
13687
13688
66
    reloc_count = NUM_SHDR_ENTRIES (hdr);
13689
66
    if (_bfd_mul_overflow (reloc_count, sizeof (arelent), & amt))
13690
0
      {
13691
0
        free (native_relocs);
13692
0
        bfd_set_error (bfd_error_file_too_big);
13693
0
        result = false;
13694
0
        continue;
13695
0
      }
13696
13697
66
    internal_relocs = bfd_alloc (abfd, amt);
13698
66
    if (internal_relocs == NULL)
13699
0
      {
13700
0
        free (native_relocs);
13701
0
        result = false;
13702
0
        continue;
13703
0
      }
13704
13705
66
    if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
13706
66
        || bfd_read (native_relocs, hdr->sh_size, abfd) != hdr->sh_size)
13707
0
      {
13708
0
        free (native_relocs);
13709
        /* The internal_relocs will be freed when
13710
     the memory for the bfd is released.  */
13711
0
        result = false;
13712
0
        continue;
13713
0
      }
13714
13715
66
    if (dynamic)
13716
0
      symcount = bfd_get_dynamic_symcount (abfd);
13717
66
    else
13718
66
      symcount = bfd_get_symcount (abfd);
13719
13720
66
    for (i = 0, internal_reloc = internal_relocs,
13721
66
     native_reloc = native_relocs;
13722
514
         i < reloc_count;
13723
448
         i++, internal_reloc++, native_reloc += entsize)
13724
448
      {
13725
448
        bool res;
13726
448
        Elf_Internal_Rela rela;
13727
13728
448
        if (entsize == ebd->s->sizeof_rel)
13729
0
    ebd->s->swap_reloc_in (abfd, native_reloc, & rela);
13730
448
        else /* entsize == ebd->s->sizeof_rela */
13731
448
    ebd->s->swap_reloca_in (abfd, native_reloc, & rela);
13732
13733
        /* The address of an ELF reloc is section relative for an object
13734
     file, and absolute for an executable file or shared library.
13735
     The address of a normal BFD reloc is always section relative,
13736
     and the address of a dynamic reloc is absolute..  */
13737
448
        if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0)
13738
400
    internal_reloc->address = rela.r_offset;
13739
48
        else
13740
48
    internal_reloc->address = rela.r_offset - sec->vma;
13741
13742
448
        if (r_sym (rela.r_info) == STN_UNDEF)
13743
76
    {
13744
      /* FIXME: This and the error case below mean that we
13745
         have a symbol on relocs that is not elf_symbol_type.  */
13746
76
      internal_reloc->sym_ptr_ptr = &bfd_abs_section_ptr->symbol;
13747
76
    }
13748
372
        else if (r_sym (rela.r_info) > symcount)
13749
0
    {
13750
0
      _bfd_error_handler
13751
        /* xgettext:c-format */
13752
0
        (_("%pB(%pA): relocation %zu has invalid symbol index %lu"),
13753
0
         abfd, sec, i, (long) r_sym (rela.r_info));
13754
0
      bfd_set_error (bfd_error_bad_value);
13755
0
      internal_reloc->sym_ptr_ptr = &bfd_abs_section_ptr->symbol;
13756
0
      result = false;
13757
0
    }
13758
372
        else
13759
372
    {
13760
372
      asymbol **ps;
13761
13762
372
      ps = symbols + r_sym (rela.r_info) - 1;
13763
372
      internal_reloc->sym_ptr_ptr = ps;
13764
      /* Make sure that this symbol is not removed by strip.  */
13765
372
      (*ps)->flags |= BSF_KEEP;
13766
372
    }
13767
13768
448
        internal_reloc->addend = rela.r_addend;
13769
13770
448
        res = ebd->elf_info_to_howto (abfd, internal_reloc, & rela);
13771
448
        if (! res || internal_reloc->howto == NULL)
13772
26
    {
13773
#if DEBUG_SECONDARY_RELOCS
13774
      fprintf (stderr,
13775
         "there is no howto associated with reloc %lx\n",
13776
         rela.r_info);
13777
#endif
13778
26
      result = false;
13779
26
    }
13780
448
      }
13781
13782
66
    free (native_relocs);
13783
    /* Store the internal relocs.  */
13784
66
    relsec->sec_info = internal_relocs;
13785
66
  }
13786
1.92k
    }
13787
13788
91
  return result;
13789
91
}
13790
13791
/* Set the ELF section header fields of an output secondary reloc section.  */
13792
13793
bool
13794
_bfd_elf_copy_special_section_fields (const bfd *ibfd ATTRIBUTE_UNUSED,
13795
              bfd *obfd ATTRIBUTE_UNUSED,
13796
              const Elf_Internal_Shdr *isection,
13797
              Elf_Internal_Shdr *osection)
13798
39
{
13799
39
  asection * isec;
13800
39
  asection * osec;
13801
39
  struct bfd_elf_section_data * esd;
13802
13803
39
  if (isection == NULL)
13804
0
    return false;
13805
13806
39
  if (isection->sh_type != SHT_SECONDARY_RELOC)
13807
39
    return true;
13808
13809
0
  isec = isection->bfd_section;
13810
0
  if (isec == NULL)
13811
0
    return false;
13812
13813
0
  osec = osection->bfd_section;
13814
0
  if (osec == NULL)
13815
0
    return false;
13816
13817
0
  BFD_ASSERT (osec->sec_info == NULL);
13818
0
  osec->sec_info = isec->sec_info;
13819
0
  osection->sh_type = SHT_RELA;
13820
0
  osection->sh_link = elf_onesymtab (obfd);
13821
0
  if (osection->sh_link == 0)
13822
0
    {
13823
      /* There is no symbol table - we are hosed...  */
13824
0
      _bfd_error_handler
13825
  /* xgettext:c-format */
13826
0
  (_("%pB(%pA): link section cannot be set"
13827
0
     " because the output file does not have a symbol table"),
13828
0
  obfd, osec);
13829
0
      bfd_set_error (bfd_error_bad_value);
13830
0
      return false;
13831
0
    }
13832
13833
  /* Find the output section that corresponds to the isection's
13834
     sh_info link.  */
13835
0
  if (isection->sh_info == 0
13836
0
      || isection->sh_info >= elf_numsections (ibfd))
13837
0
    {
13838
0
      _bfd_error_handler
13839
  /* xgettext:c-format */
13840
0
  (_("%pB(%pA): info section index is invalid"),
13841
0
  obfd, osec);
13842
0
      bfd_set_error (bfd_error_bad_value);
13843
0
      return false;
13844
0
    }
13845
13846
0
  isection = elf_elfsections (ibfd)[isection->sh_info];
13847
13848
0
  if (isection == NULL
13849
0
      || isection->bfd_section == NULL
13850
0
      || isection->bfd_section->output_section == NULL)
13851
0
    {
13852
0
      _bfd_error_handler
13853
  /* xgettext:c-format */
13854
0
  (_("%pB(%pA): info section index cannot be set"
13855
0
     " because the section is not in the output"),
13856
0
  obfd, osec);
13857
0
      bfd_set_error (bfd_error_bad_value);
13858
0
      return false;
13859
0
    }
13860
13861
0
  esd = elf_section_data (isection->bfd_section->output_section);
13862
0
  BFD_ASSERT (esd != NULL);
13863
0
  osection->sh_info = esd->this_idx;
13864
0
  esd->has_secondary_relocs = true;
13865
#if DEBUG_SECONDARY_RELOCS
13866
  fprintf (stderr, "update header of %s, sh_link = %u, sh_info = %u\n",
13867
     osec->name, osection->sh_link, osection->sh_info);
13868
  fprintf (stderr, "mark section %s as having secondary relocs\n",
13869
     bfd_section_name (isection->bfd_section->output_section));
13870
#endif
13871
13872
0
  return true;
13873
0
}
13874
13875
/* Write out a secondary reloc section.
13876
13877
   FIXME: Currently this function can result in a serious performance penalty
13878
   for files with secondary relocs and lots of sections.  The proper way to
13879
   fix this is for _bfd_elf_copy_special_section_fields() to chain secondary
13880
   relocs together and then to have this function just walk that chain.  */
13881
13882
bool
13883
_bfd_elf_write_secondary_reloc_section (bfd *abfd, asection *sec)
13884
0
{
13885
0
  elf_backend_data *ebd = get_elf_backend_data (abfd);
13886
0
  bfd_vma addr_offset;
13887
0
  asection * relsec;
13888
0
  bfd_vma (*r_info) (bfd_vma, bfd_vma);
13889
0
  bool result = true;
13890
13891
0
  if (sec == NULL)
13892
0
    return false;
13893
13894
0
#if BFD_DEFAULT_TARGET_SIZE > 32
13895
0
  if (bfd_arch_bits_per_address (abfd) != 32)
13896
0
    r_info = elf64_r_info;
13897
0
  else
13898
0
#endif
13899
0
    r_info = elf32_r_info;
13900
13901
  /* The address of an ELF reloc is section relative for an object
13902
     file, and absolute for an executable file or shared library.
13903
     The address of a BFD reloc is always section relative.  */
13904
0
  addr_offset = 0;
13905
0
  if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
13906
0
    addr_offset = sec->vma;
13907
13908
  /* Discover if there are any secondary reloc sections
13909
     associated with SEC.  */
13910
0
  for (relsec = abfd->sections; relsec != NULL; relsec = relsec->next)
13911
0
    {
13912
0
      const struct bfd_elf_section_data * const esd = elf_section_data (relsec);
13913
0
      Elf_Internal_Shdr * const hdr = (Elf_Internal_Shdr *) & esd->this_hdr;
13914
13915
0
      if (hdr->sh_type == SHT_RELA
13916
0
    && hdr->sh_info == (unsigned) elf_section_data (sec)->this_idx)
13917
0
  {
13918
0
    asymbol *    last_sym;
13919
0
    int          last_sym_idx;
13920
0
    size_t       reloc_count;
13921
0
    size_t       idx;
13922
0
    bfd_size_type entsize;
13923
0
    arelent *    src_irel;
13924
0
    bfd_byte *   dst_rela;
13925
13926
0
    if (hdr->contents != NULL)
13927
0
      {
13928
0
        _bfd_error_handler
13929
    /* xgettext:c-format */
13930
0
    (_("%pB(%pA): error: secondary reloc section processed twice"),
13931
0
     abfd, relsec);
13932
0
        bfd_set_error (bfd_error_bad_value);
13933
0
        result = false;
13934
0
        continue;
13935
0
      }
13936
13937
0
    entsize = hdr->sh_entsize;
13938
0
    if (entsize == 0)
13939
0
      {
13940
0
        _bfd_error_handler
13941
    /* xgettext:c-format */
13942
0
    (_("%pB(%pA): error: secondary reloc section"
13943
0
       " has zero sized entries"),
13944
0
     abfd, relsec);
13945
0
        bfd_set_error (bfd_error_bad_value);
13946
0
        result = false;
13947
0
        continue;
13948
0
      }
13949
0
    else if (entsize != ebd->s->sizeof_rel
13950
0
       && entsize != ebd->s->sizeof_rela)
13951
0
      {
13952
0
        _bfd_error_handler
13953
    /* xgettext:c-format */
13954
0
    (_("%pB(%pA): error: secondary reloc section"
13955
0
       " has non-standard sized entries"),
13956
0
     abfd, relsec);
13957
0
        bfd_set_error (bfd_error_bad_value);
13958
0
        result = false;
13959
0
        continue;
13960
0
      }
13961
13962
0
    reloc_count = hdr->sh_size / entsize;
13963
0
    hdr->sh_size = entsize * reloc_count;
13964
0
    if (reloc_count == 0)
13965
0
      {
13966
0
        _bfd_error_handler
13967
    /* xgettext:c-format */
13968
0
    (_("%pB(%pA): error: secondary reloc section is empty!"),
13969
0
     abfd, relsec);
13970
0
        bfd_set_error (bfd_error_bad_value);
13971
0
        result = false;
13972
0
        continue;
13973
0
      }
13974
13975
0
    hdr->contents = bfd_alloc (abfd, hdr->sh_size);
13976
0
    if (hdr->contents == NULL)
13977
0
      continue;
13978
0
    relsec->alloced = 1;
13979
13980
#if DEBUG_SECONDARY_RELOCS
13981
    fprintf (stderr, "write %u secondary relocs for %s from %s\n",
13982
       reloc_count, sec->name, relsec->name);
13983
#endif
13984
0
    last_sym = NULL;
13985
0
    last_sym_idx = 0;
13986
0
    dst_rela = hdr->contents;
13987
0
    src_irel = sec->sec_info;
13988
0
    if (src_irel == NULL)
13989
0
      {
13990
0
        _bfd_error_handler
13991
    /* xgettext:c-format */
13992
0
    (_("%pB(%pA): error: internal relocs missing"
13993
0
       " for secondary reloc section"),
13994
0
     abfd, relsec);
13995
0
        bfd_set_error (bfd_error_bad_value);
13996
0
        result = false;
13997
0
        continue;
13998
0
      }
13999
14000
0
    for (idx = 0; idx < reloc_count; idx++, dst_rela += entsize)
14001
0
      {
14002
0
        Elf_Internal_Rela src_rela;
14003
0
        arelent *ptr;
14004
0
        asymbol *sym;
14005
0
        int n;
14006
14007
0
        ptr = src_irel + idx;
14008
0
        if (ptr == NULL)
14009
0
    {
14010
0
      _bfd_error_handler
14011
        /* xgettext:c-format */
14012
0
        (_("%pB(%pA): error: reloc table entry %zu is empty"),
14013
0
         abfd, relsec, idx);
14014
0
      bfd_set_error (bfd_error_bad_value);
14015
0
      result = false;
14016
0
      break;
14017
0
    }
14018
14019
0
        if (ptr->sym_ptr_ptr == NULL)
14020
0
    {
14021
      /* FIXME: Is this an error ? */
14022
0
      n = 0;
14023
0
    }
14024
0
        else
14025
0
    {
14026
0
      sym = *ptr->sym_ptr_ptr;
14027
14028
0
      if (sym == last_sym)
14029
0
        n = last_sym_idx;
14030
0
      else
14031
0
        {
14032
0
          n = _bfd_elf_symbol_from_bfd_symbol (abfd, & sym);
14033
0
          if (n < 0)
14034
0
      {
14035
0
        _bfd_error_handler
14036
          /* xgettext:c-format */
14037
0
          (_("%pB(%pA): error: secondary reloc %zu"
14038
0
             " references a missing symbol"),
14039
0
           abfd, relsec, idx);
14040
0
        bfd_set_error (bfd_error_bad_value);
14041
0
        result = false;
14042
0
        n = 0;
14043
0
      }
14044
14045
0
          last_sym = sym;
14046
0
          last_sym_idx = n;
14047
0
        }
14048
14049
0
      if (sym->the_bfd != NULL
14050
0
          && sym->the_bfd->xvec != abfd->xvec
14051
0
          && ! _bfd_elf_validate_reloc (abfd, ptr))
14052
0
        {
14053
0
          _bfd_error_handler
14054
      /* xgettext:c-format */
14055
0
      (_("%pB(%pA): error: secondary reloc %zu"
14056
0
         " references a deleted symbol"),
14057
0
       abfd, relsec, idx);
14058
0
          bfd_set_error (bfd_error_bad_value);
14059
0
          result = false;
14060
0
          n = 0;
14061
0
        }
14062
0
    }
14063
14064
0
        src_rela.r_offset = ptr->address + addr_offset;
14065
0
        if (ptr->howto == NULL)
14066
0
    {
14067
0
      _bfd_error_handler
14068
        /* xgettext:c-format */
14069
0
        (_("%pB(%pA): error: secondary reloc %zu"
14070
0
           " is of an unknown type"),
14071
0
         abfd, relsec, idx);
14072
0
      bfd_set_error (bfd_error_bad_value);
14073
0
      result = false;
14074
0
      src_rela.r_info = r_info (0, 0);
14075
0
    }
14076
0
        else
14077
0
    src_rela.r_info = r_info (n, ptr->howto->type);
14078
0
        src_rela.r_addend = ptr->addend;
14079
14080
0
        if (entsize == ebd->s->sizeof_rel)
14081
0
    ebd->s->swap_reloc_out (abfd, &src_rela, dst_rela);
14082
0
        else /* entsize == ebd->s->sizeof_rela */
14083
0
    ebd->s->swap_reloca_out (abfd, &src_rela, dst_rela);
14084
0
      }
14085
0
  }
14086
0
    }
14087
14088
0
  return result;
14089
0
}
14090
14091
/* Mmap in section contents.  */
14092
14093
static bool
14094
elf_mmap_section_contents (bfd *abfd, sec_ptr sec, bfd_byte **buf)
14095
9.16k
{
14096
9.16k
#ifdef USE_MMAP
14097
9.16k
  elf_backend_data *bed = get_elf_backend_data (abfd);
14098
9.16k
  if (bed->use_mmap
14099
3.63k
      && sec->compress_status == COMPRESS_SECTION_NONE
14100
3.63k
      && (sec->flags & SEC_LINKER_CREATED) == 0)
14101
3.63k
    {
14102
      /* Use mmap only if section size >= the minimum mmap section
14103
   size.  */
14104
3.63k
      size_t readsz = bfd_get_section_limit_octets (abfd, sec);
14105
3.63k
      size_t allocsz = bfd_get_section_alloc_size (abfd, sec);
14106
3.63k
      if (readsz == allocsz && readsz >= _bfd_minimum_mmap_size)
14107
993
  {
14108
993
    if (sec->contents != NULL)
14109
0
      {
14110
0
        if (!sec->mmapped_p)
14111
0
    abort ();
14112
0
        *buf = sec->contents;
14113
0
        return true;
14114
0
      }
14115
993
    if (sec->mmapped_p)
14116
0
      abort ();
14117
993
    sec->mmapped_p = 1;
14118
14119
    /* We can't use the final link preallocated buffer for mmap.  */
14120
993
    *buf = NULL;
14121
993
  }
14122
3.63k
    }
14123
9.16k
#endif
14124
  /* FIXME: We should not get here if sec->alloced is set.  */
14125
9.16k
  bool ret = bfd_get_full_section_contents (abfd, sec, buf);
14126
9.16k
  if (ret && sec->mmapped_p)
14127
0
    *buf = sec->contents;
14128
9.16k
  return ret;
14129
9.16k
}
14130
14131
/* Mmap in section contents.  */
14132
14133
bool
14134
_bfd_elf_mmap_section_contents (bfd *abfd, sec_ptr sec, bfd_byte **buf)
14135
9.16k
{
14136
9.16k
  *buf = NULL;
14137
9.16k
  return elf_mmap_section_contents (abfd, sec, buf);
14138
9.16k
}
14139
14140
/* Mmap in the full section contents for the final link.  */
14141
14142
bool
14143
_bfd_elf_link_mmap_section_contents (bfd *abfd, sec_ptr sec,
14144
             bfd_byte **buf)
14145
0
{
14146
0
  return elf_mmap_section_contents (abfd, sec, buf);
14147
0
}
14148
14149
/* Munmap section contents.  */
14150
14151
void
14152
_bfd_elf_munmap_section_contents (asection *sec, void *contents)
14153
522k
{
14154
  /* NB: Since _bfd_elf_munmap_section_contents is called like free,
14155
     CONTENTS may be NULL.  */
14156
522k
  if (contents == NULL)
14157
514k
    return;
14158
14159
7.62k
  if (sec->alloced
14160
      /* What a tangled web we weave with section contents.
14161
   FIXME: We shouldn't need to test anything but sec->alloced
14162
   here, but there are cases where a buffer is allocated for a
14163
   section but then another buffer is malloc'd anyway.  eg.
14164
   trace through ld-elf/eh4 testcase on x86_64.  */
14165
426
      && (sec->contents == contents
14166
0
    || elf_section_data (sec)->this_hdr.contents == contents))
14167
426
    return;
14168
14169
  /* Don't leave pointers to data we are about to munmap or free.  */
14170
7.19k
  if (sec->contents == contents)
14171
0
    sec->contents = NULL;
14172
7.19k
  if (elf_section_data (sec)->this_hdr.contents == contents)
14173
0
    elf_section_data (sec)->this_hdr.contents = NULL;
14174
14175
7.19k
#ifdef USE_MMAP
14176
7.19k
  if (sec->mmapped_p)
14177
0
    {
14178
      /* When _bfd_elf_mmap_section_contents returns CONTENTS as
14179
   malloced, CONTENTS_ADDR is set to NULL.  */
14180
0
      if (elf_section_data (sec)->contents_addr != NULL)
14181
0
  {
14182
    /* NB: CONTENTS_ADDR and CONTENTS_SIZE must be valid.  */
14183
0
    if (munmap (elf_section_data (sec)->contents_addr,
14184
0
          elf_section_data (sec)->contents_size) != 0)
14185
0
      abort ();
14186
0
    sec->mmapped_p = 0;
14187
0
    elf_section_data (sec)->contents_addr = NULL;
14188
0
    elf_section_data (sec)->contents_size = 0;
14189
0
    return;
14190
0
  }
14191
0
    }
14192
7.19k
#endif
14193
14194
7.19k
  free (contents);
14195
7.19k
}
14196
14197
/* Munmap the full section contents for the final link.  */
14198
14199
void
14200
_bfd_elf_link_munmap_section_contents (asection *sec ATTRIBUTE_UNUSED)
14201
0
{
14202
0
#ifdef USE_MMAP
14203
0
  if (sec->mmapped_p && elf_section_data (sec)->contents_addr != NULL)
14204
0
    {
14205
      /* When _bfd_elf_link_mmap_section_contents returns CONTENTS as
14206
   malloced, CONTENTS_ADDR is set to NULL.  */
14207
      /* NB: CONTENTS_ADDR and CONTENTS_SIZE must be valid.  */
14208
0
      if (munmap (elf_section_data (sec)->contents_addr,
14209
0
      elf_section_data (sec)->contents_size) != 0)
14210
0
  abort ();
14211
0
      sec->mmapped_p = 0;
14212
0
      sec->contents = NULL;
14213
0
      elf_section_data (sec)->this_hdr.contents = NULL;
14214
0
      elf_section_data (sec)->contents_addr = NULL;
14215
0
      elf_section_data (sec)->contents_size = 0;
14216
0
    }
14217
0
#endif
14218
0
}
14219
14220
#ifndef OBJ_MAYBE_ELF_ATTRIBUTES
14221
14222
bfd *_bfd_elf_link_setup_object_attributes
14223
  (struct bfd_link_info *info ATTRIBUTE_UNUSED)
14224
{ return NULL; }
14225
14226
#endif /* !OBJ_MAYBE_ELF_ATTRIBUTES */