Coverage Report

Created: 2026-09-14 08:07

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
69.6k
#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
0
#define NOTE_PSEUDO_SECTION_REG     ".reg"
100
220
#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
95
{
140
95
  dst->vd_version = H_GET_16 (abfd, src->vd_version);
141
95
  dst->vd_flags   = H_GET_16 (abfd, src->vd_flags);
142
95
  dst->vd_ndx     = H_GET_16 (abfd, src->vd_ndx);
143
95
  dst->vd_cnt     = H_GET_16 (abfd, src->vd_cnt);
144
95
  dst->vd_hash    = H_GET_32 (abfd, src->vd_hash);
145
95
  dst->vd_aux     = H_GET_32 (abfd, src->vd_aux);
146
95
  dst->vd_next    = H_GET_32 (abfd, src->vd_next);
147
95
}
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
10
{
172
10
  dst->vda_name = H_GET_32 (abfd, src->vda_name);
173
10
  dst->vda_next = H_GET_32 (abfd, src->vda_next);
174
10
}
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
246
{
194
246
  dst->vn_version = H_GET_16 (abfd, src->vn_version);
195
246
  dst->vn_cnt     = H_GET_16 (abfd, src->vn_cnt);
196
246
  dst->vn_file    = H_GET_32 (abfd, src->vn_file);
197
246
  dst->vn_aux     = H_GET_32 (abfd, src->vn_aux);
198
246
  dst->vn_next    = H_GET_32 (abfd, src->vn_next);
199
246
}
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
160
{
222
160
  dst->vna_hash  = H_GET_32 (abfd, src->vna_hash);
223
160
  dst->vna_flags = H_GET_16 (abfd, src->vna_flags);
224
160
  dst->vna_other = H_GET_16 (abfd, src->vna_other);
225
160
  dst->vna_name  = H_GET_32 (abfd, src->vna_name);
226
160
  dst->vna_next  = H_GET_32 (abfd, src->vna_next);
227
160
}
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
895
{
250
895
  dst->vs_vers = H_GET_16 (abfd, src->vs_vers);
251
895
}
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.45M
{
300
3.45M
  BFD_ASSERT (object_size >= sizeof (struct elf_obj_tdata));
301
3.45M
  abfd->tdata.any = bfd_zalloc (abfd, object_size);
302
3.45M
  if (abfd->tdata.any == NULL)
303
0
    return false;
304
305
3.45M
  elf_object_id (abfd) = get_elf_backend_data (abfd)->target_id;
306
3.45M
  if (abfd->direction != read_direction)
307
1.08k
    {
308
1.08k
      struct output_elf_obj_tdata *o = bfd_zalloc (abfd, sizeof *o);
309
1.08k
      if (o == NULL)
310
0
  return false;
311
1.08k
      elf_tdata (abfd)->o = o;
312
1.08k
      elf_program_header_size (abfd) = (bfd_size_type) -1;
313
1.08k
    }
314
3.45M
  return true;
315
3.45M
}
316
317
318
bool
319
bfd_elf_make_object (bfd *abfd)
320
1.69M
{
321
1.69M
  return bfd_elf_allocate_object (abfd, sizeof (struct elf_obj_tdata));
322
1.69M
}
323
324
bool
325
bfd_elf_mkcorefile (bfd *abfd)
326
991k
{
327
  /* I think this can be done just like an object file.  */
328
991k
  if (!abfd->xvec->_bfd_set_format[(int) bfd_object] (abfd))
329
0
    return false;
330
991k
  elf_tdata (abfd)->core = bfd_zalloc (abfd, sizeof (*elf_tdata (abfd)->core));
331
991k
  return elf_tdata (abfd)->core != NULL;
332
991k
}
333
334
char *
335
bfd_elf_get_str_section (bfd *abfd, unsigned int shindex)
336
70.3k
{
337
70.3k
  Elf_Internal_Shdr **i_shdrp;
338
70.3k
  bfd_byte *shstrtab = NULL;
339
70.3k
  file_ptr offset;
340
70.3k
  bfd_size_type shstrtabsize;
341
342
70.3k
  i_shdrp = elf_elfsections (abfd);
343
70.3k
  if (i_shdrp == 0
344
70.3k
      || shindex >= elf_numsections (abfd)
345
70.3k
      || i_shdrp[shindex] == 0)
346
0
    return NULL;
347
348
70.3k
  shstrtab = i_shdrp[shindex]->contents;
349
70.3k
  if (shstrtab == NULL)
350
70.3k
    {
351
      /* No cached one, attempt to read, and cache what we read.  */
352
70.3k
      offset = i_shdrp[shindex]->sh_offset;
353
70.3k
      shstrtabsize = i_shdrp[shindex]->sh_size;
354
355
70.3k
      if (shstrtabsize == 0
356
64.3k
    || bfd_seek (abfd, offset, SEEK_SET) != 0
357
63.9k
    || (shstrtab = _bfd_mmap_persistent (abfd, shstrtabsize)) == NULL)
358
9.18k
  {
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
9.18k
    i_shdrp[shindex]->sh_size = 0;
363
9.18k
  }
364
61.1k
      else if (shstrtab[shstrtabsize - 1] != 0)
365
18.4k
  {
366
    /* It is an error if a string table isn't terminated.  */
367
18.4k
    _bfd_error_handler
368
      /* xgettext:c-format */
369
18.4k
      (_("%pB: string table [%u] is corrupt"), abfd, shindex);
370
18.4k
    shstrtab[shstrtabsize - 1] = 0;
371
18.4k
  }
372
70.3k
      i_shdrp[shindex]->contents = shstrtab;
373
70.3k
    }
374
70.3k
  return (char *) shstrtab;
375
70.3k
}
376
377
char *
378
bfd_elf_string_from_elf_section (bfd *abfd,
379
         unsigned int shindex,
380
         unsigned int strindex)
381
1.23M
{
382
1.23M
  Elf_Internal_Shdr *hdr;
383
384
1.23M
  if (strindex == 0)
385
226k
    return "";
386
387
1.00M
  if (elf_elfsections (abfd) == NULL || shindex >= elf_numsections (abfd))
388
557
    return NULL;
389
390
1.00M
  hdr = elf_elfsections (abfd)[shindex];
391
392
1.00M
  if (hdr->contents == NULL)
393
92.3k
    {
394
92.3k
      if (hdr->sh_type != SHT_STRTAB && hdr->sh_type < SHT_LOOS)
395
22.0k
  {
396
    /* PR 17512: file: f057ec89.  */
397
    /* xgettext:c-format */
398
22.0k
    _bfd_error_handler (_("%pB: attempt to load strings from"
399
22.0k
        " a non-string section (number %d)"),
400
22.0k
            abfd, shindex);
401
22.0k
    return NULL;
402
22.0k
  }
403
404
70.3k
      if (bfd_elf_get_str_section (abfd, shindex) == NULL)
405
9.18k
  return NULL;
406
70.3k
    }
407
914k
  else
408
914k
    {
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
914k
      if (hdr->sh_size == 0 || hdr->contents[hdr->sh_size - 1] != 0)
415
0
  return NULL;
416
914k
    }
417
418
975k
  if (strindex >= hdr->sh_size)
419
27.3k
    {
420
27.3k
      unsigned int shstrndx = elf_elfheader(abfd)->e_shstrndx;
421
27.3k
      _bfd_error_handler
422
  /* xgettext:c-format */
423
27.3k
  (_("%pB: invalid string offset %u >= %" PRIu64 " for section `%s'"),
424
27.3k
   abfd, strindex, (uint64_t) hdr->sh_size,
425
27.3k
   (shindex == shstrndx && strindex == hdr->sh_name
426
27.3k
    ? ".shstrtab"
427
27.3k
    : bfd_elf_string_from_elf_section (abfd, shstrndx, hdr->sh_name)));
428
27.3k
      return NULL;
429
27.3k
    }
430
431
948k
  return ((char *) hdr->contents) + strindex;
432
975k
}
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
17.1k
{
451
17.1k
  Elf_Internal_Shdr *shndx_hdr;
452
17.1k
  void *alloc_ext;
453
17.1k
  const bfd_byte *esym;
454
17.1k
  Elf_External_Sym_Shndx *alloc_extshndx;
455
17.1k
  Elf_External_Sym_Shndx *shndx;
456
17.1k
  Elf_Internal_Sym *alloc_intsym;
457
17.1k
  Elf_Internal_Sym *isym;
458
17.1k
  Elf_Internal_Sym *isymend;
459
17.1k
  elf_backend_data *bed;
460
17.1k
  size_t extsym_size;
461
17.1k
  size_t amt;
462
17.1k
  file_ptr pos;
463
464
17.1k
  if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
465
0
    abort ();
466
467
17.1k
  if (symcount == 0)
468
0
    return intsym_buf;
469
470
17.1k
  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
17.1k
  shndx_hdr = NULL;
483
17.1k
  if (elf_symtab_shndx_list (ibfd) != NULL)
484
2.97k
    {
485
2.97k
      elf_section_list * entry;
486
2.97k
      Elf_Internal_Shdr **sections = elf_elfsections (ibfd);
487
488
      /* Find an index section that is linked to this symtab section.  */
489
5.00k
      for (entry = elf_symtab_shndx_list (ibfd); entry != NULL; entry = entry->next)
490
3.35k
  {
491
    /* PR 20063.  */
492
3.35k
    if (entry->hdr.sh_link >= elf_numsections (ibfd))
493
59
      continue;
494
495
3.29k
    if (sections[entry->hdr.sh_link] == symtab_hdr)
496
1.32k
      {
497
1.32k
        shndx_hdr = & entry->hdr;
498
1.32k
        break;
499
1.97k
      };
500
1.97k
  }
501
502
2.97k
      if (shndx_hdr == NULL)
503
1.65k
  {
504
1.65k
    if (symtab_hdr == &elf_symtab_hdr (ibfd))
505
      /* Not really accurate, but this was how the old code used
506
         to work.  */
507
1.64k
      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.65k
  }
511
2.97k
    }
512
513
  /* Read the symbols.  */
514
17.1k
  alloc_ext = NULL;
515
17.1k
  alloc_extshndx = NULL;
516
17.1k
  alloc_intsym = NULL;
517
17.1k
  bed = get_elf_backend_data (ibfd);
518
17.1k
  extsym_size = bed->s->sizeof_sym;
519
17.1k
  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
17.1k
  pos = symtab_hdr->sh_offset + symoffset * extsym_size;
525
17.1k
  size_t alloc_ext_size = amt;
526
17.1k
  if (bfd_seek (ibfd, pos, SEEK_SET) != 0
527
16.7k
      || !_bfd_mmap_read_temporary (&extsym_buf, &alloc_ext_size,
528
16.7k
            &alloc_ext, ibfd, false))
529
1.37k
    {
530
1.37k
      intsym_buf = NULL;
531
1.37k
      goto out2;
532
1.37k
    }
533
534
15.7k
  size_t alloc_extshndx_size = 0;
535
15.7k
  if (shndx_hdr == NULL || shndx_hdr->sh_size == 0)
536
13.7k
    extshndx_buf = NULL;
537
1.98k
  else
538
1.98k
    {
539
1.98k
      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
1.98k
      alloc_extshndx_size = amt;
546
1.98k
      pos = shndx_hdr->sh_offset + symoffset * sizeof (Elf_External_Sym_Shndx);
547
1.98k
      if (bfd_seek (ibfd, pos, SEEK_SET) != 0
548
1.82k
    || !_bfd_mmap_read_temporary ((void **) &extshndx_buf,
549
1.82k
          &alloc_extshndx_size,
550
1.82k
          (void **) &alloc_extshndx,
551
1.82k
          ibfd, false))
552
340
  {
553
340
    intsym_buf = NULL;
554
340
    goto out1;
555
340
  }
556
1.98k
    }
557
558
15.3k
  if (intsym_buf == NULL)
559
7.43k
    {
560
7.43k
      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.43k
      alloc_intsym = bfd_malloc (amt);
566
7.43k
      intsym_buf = alloc_intsym;
567
7.43k
      if (intsym_buf == NULL)
568
0
  goto out1;
569
7.43k
    }
570
571
  /* Convert the symbols to internal form.  */
572
15.3k
  isymend = intsym_buf + symcount;
573
15.3k
  for (esym = (const bfd_byte *) extsym_buf, isym = intsym_buf,
574
15.3k
     shndx = extshndx_buf;
575
200k
       isym < isymend;
576
184k
       esym += extsym_size, isym++, shndx = shndx != NULL ? shndx + 1 : NULL)
577
186k
    {
578
186k
      if (!(*bed->s->swap_symbol_in) (ibfd, esym, shndx, isym))
579
494
  {
580
494
    symoffset += (esym - (bfd_byte *) extsym_buf) / extsym_size;
581
    /* xgettext:c-format */
582
494
    _bfd_error_handler (_("%pB symbol number %lu references"
583
494
        " nonexistent SHT_SYMTAB_SHNDX section"),
584
494
            ibfd, (unsigned long) symoffset);
585
494
    free (alloc_intsym);
586
494
    intsym_buf = NULL;
587
494
    goto out1;
588
494
  }
589
590
      /* PR 33019: Do not accept unsupported binding values - they will
591
   likely cause problems later on.  */
592
186k
      int bind = ELF_ST_BIND (isym->st_info);
593
186k
      if (bind > STB_WEAK && bind < STB_LOOS)
594
988
  { 
595
    /* xgettext:c-format */
596
988
    _bfd_error_handler (_("%pB symbol number %lu uses unsupported binding of %u"),
597
988
            ibfd, (unsigned long) (isym - intsym_buf), bind);
598
988
    free (alloc_intsym);
599
988
    intsym_buf = NULL;
600
988
    goto out1;
601
988
  }
602
603
      /* Paranoia: Also refuse to accept the only undefined symbol type: 7.  */
604
185k
      int t = ELF_ST_TYPE (isym->st_info);
605
185k
      if (t == 7)
606
237
  {
607
    /* xgettext:c-format */
608
237
    _bfd_error_handler (_("%pB symbol number %lu uses unsupported type of %u"),
609
237
            ibfd, (unsigned long) (isym - intsym_buf), t);
610
237
    free (alloc_intsym);
611
237
    intsym_buf = NULL;
612
237
    goto out1;
613
237
  }
614
185k
    }
615
616
15.7k
 out1:
617
15.7k
  _bfd_munmap_temporary (alloc_extshndx, alloc_extshndx_size);
618
17.1k
 out2:
619
17.1k
  _bfd_munmap_temporary (alloc_ext, alloc_ext_size);
620
621
17.1k
  return intsym_buf;
622
15.7k
}
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
171k
{
630
171k
  unsigned int iname = isym->st_name;
631
171k
  unsigned int shindex = symtab_hdr->sh_link;
632
633
171k
  if (iname == 0 && ELF_ST_TYPE (isym->st_info) == STT_SECTION
634
      /* Check for a bogus st_shndx to avoid crashing.  */
635
25.6k
      && isym->st_shndx < elf_numsections (abfd))
636
25.1k
    {
637
25.1k
      iname = elf_elfsections (abfd)[isym->st_shndx]->sh_name;
638
25.1k
      shindex = elf_elfheader (abfd)->e_shstrndx;
639
25.1k
    }
640
641
171k
  return bfd_elf_string_from_elf_section (abfd, shindex, iname);
642
171k
}
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
164k
{
650
164k
  const char *name = bfd_elf_sym_name_raw (abfd, symtab_hdr, isym);
651
164k
  if (name == NULL)
652
48.8k
    name = bfd_symbol_error_name;
653
115k
  else if (sym_sec && *name == '\0')
654
0
    name = bfd_section_name (sym_sec);
655
656
164k
  return name;
657
164k
}
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.1k
{
665
10.1k
  Elf_Internal_Shdr *hdr;
666
10.1k
  unsigned char esym[sizeof (Elf64_External_Sym)];
667
10.1k
  Elf_External_Sym_Shndx eshndx;
668
10.1k
  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.1k
  if (ghdr->sh_link >= elf_numsections (abfd))
673
27
    return NULL;
674
10.1k
  hdr = elf_elfsections (abfd) [ghdr->sh_link];
675
10.1k
  if (hdr->sh_type != SHT_SYMTAB
676
9.39k
      || ! bfd_section_from_shdr (abfd, ghdr->sh_link))
677
822
    return NULL;
678
679
  /* Go read the symbol.  */
680
9.33k
  hdr = &elf_symtab_hdr (abfd);
681
9.33k
  if (bfd_elf_get_elf_syms (abfd, hdr, 1, ghdr->sh_info,
682
9.33k
          &isym, esym, &eshndx) == NULL)
683
2.53k
    return NULL;
684
685
6.80k
  return bfd_elf_sym_name_raw (abfd, hdr, &isym);
686
9.33k
}
687
688
static bool
689
is_valid_group_section_header (Elf_Internal_Shdr *shdr, size_t minsize)
690
15.7k
{
691
15.7k
  return (shdr->sh_size >= minsize
692
15.5k
    && shdr->sh_entsize == GRP_ENTRY_SIZE
693
14.1k
    && shdr->sh_size % GRP_ENTRY_SIZE == 0
694
13.8k
    && shdr->bfd_section != NULL);
695
15.7k
}
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
13.6k
{
704
13.6k
  unsigned char *contents;
705
706
  /* Read the raw contents.  */
707
13.6k
  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
12.6k
  asection *last_elt = NULL;
716
12.6k
  const char *gname = NULL;
717
12.6k
  unsigned char *p = contents + ghdr->sh_size;
718
568k
  while (1)
719
568k
    {
720
568k
      unsigned int idx;
721
568k
      Elf_Internal_Shdr *shdr;
722
568k
      asection *elt;
723
724
568k
      p -= 4;
725
568k
      idx = H_GET_32 (abfd, p);
726
568k
      if (p == contents)
727
8.03k
  {
728
8.03k
    if ((idx & GRP_COMDAT) != 0)
729
2.93k
      ghdr->bfd_section->flags
730
2.93k
        |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
731
8.03k
    break;
732
8.03k
  }
733
734
560k
      if (idx == 0
735
416k
    || idx >= elf_numsections (abfd)
736
65.4k
    || (shdr = elf_elfsections (abfd)[idx])->sh_type == SHT_GROUP
737
58.8k
    || ((elt = shdr->bfd_section) != NULL
738
38.2k
        && elf_sec_group (elt) != NULL
739
17.9k
        && elf_sec_group (elt) != ghdr->bfd_section))
740
504k
  {
741
504k
    _bfd_error_handler
742
504k
      (_("%pB: invalid entry (%#x) in group [%u]"),
743
504k
       abfd, idx, gidx);
744
504k
    continue;
745
504k
  }
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
56.1k
      shdr->sh_flags |= SHF_GROUP;
751
752
56.1k
      if (elt == NULL)
753
20.6k
  {
754
20.6k
    if (shdr->sh_type != SHT_RELA && shdr->sh_type != SHT_REL)
755
9.04k
      {
756
9.04k
        const char *name = bfd_elf_string_from_elf_section
757
9.04k
    (abfd, elf_elfheader (abfd)->e_shstrndx, shdr->sh_name);
758
759
9.04k
        _bfd_error_handler
760
    /* xgettext:c-format */
761
9.04k
    (_("%pB: unexpected type (%#x) section `%s' in group [%u]"),
762
9.04k
     abfd, shdr->sh_type, name, gidx);
763
9.04k
      }
764
20.6k
    continue;
765
20.6k
  }
766
767
      /* Don't try to add a section to elf_next_in_group list twice.  */
768
35.4k
      if (elf_sec_group (elt) != NULL)
769
15.2k
  continue;
770
771
20.2k
      if (last_elt == NULL)
772
10.1k
  {
773
    /* Start a circular list with one element.
774
       It will be in reverse order to match what gas does.  */
775
10.1k
    elf_next_in_group (elt) = elt;
776
    /* Point the group section to it.  */
777
10.1k
    elf_next_in_group (ghdr->bfd_section) = elt;
778
10.1k
    gname = group_signature (abfd, ghdr);
779
10.1k
    if (gname == NULL)
780
4.62k
      {
781
4.62k
        free (contents);
782
4.62k
        return false;
783
4.62k
      }
784
10.1k
  }
785
10.0k
      else
786
10.0k
  {
787
10.0k
    elf_next_in_group (elt) = elf_next_in_group (last_elt);
788
10.0k
    elf_next_in_group (last_elt) = elt;
789
10.0k
  }
790
15.6k
      last_elt = elt;
791
15.6k
      elf_group_name (elt) = gname;
792
15.6k
      elf_sec_group (elt) = ghdr->bfd_section;
793
15.6k
    }
794
795
8.03k
  free (contents);
796
8.03k
  return last_elt != NULL;
797
12.6k
}
798
799
bool
800
_bfd_elf_setup_sections (bfd *abfd)
801
54.3k
{
802
54.3k
  bool result = true;
803
804
  /* Process SHF_LINK_ORDER.  */
805
497k
  for (asection *s = abfd->sections; s != NULL; s = s->next)
806
442k
    {
807
442k
      Elf_Internal_Shdr *this_hdr = &elf_section_data (s)->this_hdr;
808
442k
      if ((this_hdr->sh_flags & SHF_LINK_ORDER) != 0)
809
32.1k
  {
810
32.1k
    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
32.1k
    if (elfsec == 0)
817
26.2k
      {
818
26.2k
        elf_linked_to_section (s) = NULL;
819
26.2k
      }
820
5.93k
    else
821
5.93k
      {
822
5.93k
        asection *linksec = NULL;
823
824
5.93k
        if (elfsec < elf_numsections (abfd))
825
5.78k
    {
826
5.78k
      this_hdr = elf_elfsections (abfd)[elfsec];
827
5.78k
      linksec = this_hdr->bfd_section;
828
5.78k
    }
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
5.93k
        if (linksec == NULL)
834
1.89k
    {
835
1.89k
      _bfd_error_handler
836
        /* xgettext:c-format */
837
1.89k
        (_("%pB: sh_link [%d] in section `%pA' is incorrect"),
838
1.89k
         s->owner, elfsec, s);
839
1.89k
      result = false;
840
1.89k
    }
841
842
5.93k
        elf_linked_to_section (s) = linksec;
843
5.93k
      }
844
32.1k
  }
845
442k
    }
846
847
  /* Process section groups.  */
848
719k
  for (unsigned int i = 1; i < elf_numsections (abfd); i++)
849
665k
    {
850
665k
      Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
851
852
665k
      if (shdr && shdr->sh_type == SHT_GROUP)
853
15.7k
  {
854
15.7k
    if (is_valid_group_section_header (shdr, GRP_ENTRY_SIZE))
855
13.8k
      {
856
13.8k
        if (shdr->sh_size >= 2 * GRP_ENTRY_SIZE
857
13.6k
      && !process_sht_group_entries (abfd, shdr, i))
858
8.11k
    result = false;
859
13.8k
      }
860
1.96k
    else
861
1.96k
      {
862
        /* PR binutils/18758: Beware of corrupt binaries with
863
     invalid group data.  */
864
1.96k
        _bfd_error_handler
865
    /* xgettext:c-format */
866
1.96k
    (_("%pB: section group entry number %u is corrupt"), abfd, i);
867
1.96k
        result = false;
868
1.96k
      }
869
15.7k
  }
870
665k
    }
871
872
54.3k
  return result;
873
54.3k
}
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
632k
{
898
632k
  asection *newsect;
899
632k
  flagword flags;
900
632k
  elf_backend_data *bed;
901
632k
  unsigned int opb = bfd_octets_per_byte (abfd, NULL);
902
903
632k
  if (hdr->bfd_section != NULL)
904
90.2k
    return true;
905
906
541k
  newsect = bfd_make_section_anyway (abfd, name);
907
541k
  if (newsect == NULL)
908
0
    return false;
909
910
541k
  hdr->bfd_section = newsect;
911
541k
  elf_section_data (newsect)->this_hdr = *hdr;
912
541k
  elf_section_data (newsect)->this_idx = shindex;
913
914
  /* Always use the real type/flags.  */
915
541k
  elf_section_type (newsect) = hdr->sh_type;
916
541k
  elf_section_flags (newsect) = hdr->sh_flags;
917
918
541k
  newsect->filepos = hdr->sh_offset;
919
920
541k
  flags = SEC_NO_FLAGS;
921
541k
  if (hdr->sh_type != SHT_NOBITS)
922
527k
    flags |= SEC_HAS_CONTENTS;
923
541k
  if (hdr->sh_type == SHT_GROUP)
924
21.4k
    flags |= SEC_GROUP;
925
541k
  if ((hdr->sh_flags & SHF_ALLOC) != 0)
926
222k
    {
927
222k
      flags |= SEC_ALLOC;
928
222k
      if (hdr->sh_type != SHT_NOBITS)
929
212k
  flags |= SEC_LOAD;
930
222k
    }
931
541k
  if ((hdr->sh_flags & SHF_WRITE) == 0)
932
416k
    flags |= SEC_READONLY;
933
541k
  if ((hdr->sh_flags & SHF_EXECINSTR) != 0)
934
113k
    flags |= SEC_CODE;
935
428k
  else if ((flags & SEC_LOAD) != 0)
936
116k
    flags |= SEC_DATA;
937
541k
  if ((hdr->sh_flags & SHF_MERGE) != 0)
938
90.6k
    flags |= SEC_MERGE;
939
541k
  if ((hdr->sh_flags & SHF_STRINGS) != 0)
940
118k
    flags |= SEC_STRINGS;
941
541k
  if ((hdr->sh_flags & SHF_TLS) != 0)
942
45.1k
    flags |= SEC_THREAD_LOCAL;
943
541k
  if ((hdr->sh_flags & SHF_EXCLUDE) != 0)
944
42.1k
    flags |= SEC_EXCLUDE;
945
946
541k
  newsect->entsize = hdr->sh_entsize;
947
948
541k
  switch (elf_elfheader (abfd)->e_ident[EI_OSABI])
949
541k
    {
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
162k
    case ELFOSABI_GNU:
954
266k
    case ELFOSABI_FREEBSD:
955
266k
      if ((hdr->sh_flags & SHF_GNU_RETAIN) != 0)
956
46.5k
  elf_tdata (abfd)->has_gnu_osabi |= elf_gnu_osabi_retain;
957
      /* Fall through */
958
428k
    case ELFOSABI_NONE:
959
428k
      if ((hdr->sh_flags & SHF_GNU_MBIND) != 0)
960
36.9k
  elf_tdata (abfd)->has_gnu_osabi |= elf_gnu_osabi_mbind;
961
428k
      break;
962
541k
    }
963
964
541k
  if ((flags & (SEC_ALLOC | SEC_GROUP)) == 0)
965
298k
    {
966
      /* The debugging sections appear to be recognized only by name,
967
   not any sort of flag.  Their SEC_ALLOC bits are cleared.  */
968
298k
      if (name [0] == '.')
969
118k
  {
970
118k
    if (startswith (name, ".debug")
971
74.6k
        || startswith (name, ".gnu.debuglto_.debug_")
972
74.2k
        || startswith (name, ".gnu.linkonce.wi.")
973
73.7k
        || startswith (name, ".zdebug"))
974
46.4k
      flags |= SEC_DEBUGGING | SEC_ELF_OCTETS;
975
72.1k
    else if (startswith (name, GNU_BUILD_ATTRS_SECTION_NAME)
976
71.8k
       || startswith (name, ".note.gnu"))
977
774
      {
978
774
        flags |= SEC_ELF_OCTETS;
979
774
        opb = 1;
980
774
      }
981
71.4k
    else if (startswith (name, ".line")
982
70.4k
       || startswith (name, ".stab")
983
68.7k
       || streq (name, ".gdb_index"))
984
3.23k
      flags |= SEC_DEBUGGING;
985
118k
  }
986
298k
    }
987
988
541k
  if (!bfd_set_section_vma (newsect, hdr->sh_addr / opb)
989
541k
      || !bfd_set_section_size (newsect, hdr->sh_size)
990
541k
      || !bfd_set_section_alignment (newsect, bfd_log2 (hdr->sh_addralign
991
541k
              & -hdr->sh_addralign)))
992
270
    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
541k
  if (startswith (name, ".gnu.linkonce")
1001
2.10k
      && elf_next_in_group (newsect) == NULL)
1002
2.10k
    flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
1003
1004
541k
  if (!bfd_set_section_flags (newsect, flags))
1005
0
    return false;
1006
1007
541k
  bed = get_elf_backend_data (abfd);
1008
541k
  if (bed->elf_backend_section_flags)
1009
138k
    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
541k
  if (hdr->sh_type == SHT_NOTE && hdr->sh_size != 0)
1016
8.85k
    {
1017
8.85k
      bfd_byte *contents;
1018
1019
8.85k
      if (!_bfd_elf_mmap_section_contents (abfd, newsect, &contents))
1020
1.69k
  return false;
1021
1022
7.16k
      elf_parse_notes (abfd, (char *) contents, hdr->sh_size,
1023
7.16k
           hdr->sh_offset, hdr->sh_addralign);
1024
7.16k
      _bfd_elf_munmap_section_contents (newsect, contents);
1025
7.16k
    }
1026
1027
539k
  if ((newsect->flags & SEC_ALLOC) != 0)
1028
221k
    {
1029
221k
      Elf_Internal_Phdr *phdr;
1030
221k
      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
221k
      phdr = elf_tdata (abfd)->phdr;
1037
247k
      for (nload = 0, i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
1038
81.5k
  if (phdr->p_paddr != 0)
1039
56.1k
    break;
1040
25.4k
  else if (phdr->p_type == PT_LOAD && phdr->p_memsz != 0)
1041
2.27k
    ++nload;
1042
221k
      if (i >= elf_elfheader (abfd)->e_phnum && nload > 1)
1043
161
  return true;
1044
1045
221k
      phdr = elf_tdata (abfd)->phdr;
1046
2.74M
      for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
1047
2.53M
  {
1048
2.53M
    if (((phdr->p_type == PT_LOAD
1049
85.9k
    && (hdr->sh_flags & SHF_TLS) == 0)
1050
2.45M
         || phdr->p_type == PT_TLS)
1051
86.7k
        && ELF_SECTION_IN_SEGMENT (hdr, phdr))
1052
12.6k
      {
1053
12.6k
        if ((newsect->flags & SEC_LOAD) == 0)
1054
854
    newsect->lma = (phdr->p_paddr
1055
854
        + hdr->sh_addr - phdr->p_vaddr) / opb;
1056
11.7k
        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
11.7k
    newsect->lma = (phdr->p_paddr
1065
11.7k
        + 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
12.6k
        if (hdr->sh_addr >= phdr->p_vaddr
1072
12.6k
      && (hdr->sh_addr + hdr->sh_size
1073
12.6k
          <= phdr->p_vaddr + phdr->p_memsz))
1074
11.4k
    break;
1075
12.6k
      }
1076
2.53M
  }
1077
221k
    }
1078
1079
  /* Compress/decompress DWARF debug sections with names: .debug_*,
1080
     .zdebug_*, .gnu.debuglto_.debug_, after the section flags is set.  */
1081
539k
  if ((newsect->flags & SEC_DEBUGGING) != 0
1082
49.6k
      && (newsect->flags & SEC_HAS_CONTENTS) != 0
1083
49.5k
      && (newsect->flags & SEC_ELF_OCTETS) != 0)
1084
46.3k
    {
1085
46.3k
      enum { nothing, compress, decompress } action = nothing;
1086
46.3k
      int compression_header_size;
1087
46.3k
      bfd_size_type uncompressed_size;
1088
46.3k
      unsigned int uncompressed_align_power;
1089
46.3k
      enum compression_type ch_type = ch_none;
1090
46.3k
      bool compressed
1091
46.3k
  = bfd_is_section_compressed_info (abfd, newsect,
1092
46.3k
            &compression_header_size,
1093
46.3k
            &uncompressed_size,
1094
46.3k
            &uncompressed_align_power,
1095
46.3k
            &ch_type);
1096
1097
      /* Should we decompress?  */
1098
46.3k
      if ((abfd->flags & BFD_DECOMPRESS) != 0 && compressed)
1099
283
  action = decompress;
1100
1101
      /* Should we compress?  Or convert to a different compression?  */
1102
46.0k
      else if ((abfd->flags & BFD_COMPRESS) != 0
1103
3.89k
         && newsect->size != 0
1104
3.77k
         && compression_header_size >= 0
1105
3.66k
         && uncompressed_size > 0)
1106
3.64k
  {
1107
3.64k
    if (!compressed)
1108
3.58k
      action = compress;
1109
60
    else
1110
60
      {
1111
60
        enum compression_type new_ch_type = ch_none;
1112
60
        if ((abfd->flags & BFD_COMPRESS_GABI) != 0)
1113
60
    new_ch_type = ((abfd->flags & BFD_COMPRESS_ZSTD) != 0
1114
60
             ? ch_compress_zstd : ch_compress_zlib);
1115
60
        if (new_ch_type != ch_type)
1116
45
    action = compress;
1117
60
      }
1118
3.64k
  }
1119
1120
46.3k
      if (action == compress)
1121
3.63k
  {
1122
3.63k
    if (!bfd_init_section_compress_status (abfd, newsect))
1123
194
      {
1124
194
        _bfd_error_handler
1125
    /* xgettext:c-format */
1126
194
    (_("%pB: unable to compress section %s"), abfd, name);
1127
194
        return false;
1128
194
      }
1129
3.63k
  }
1130
42.7k
      else if (action == decompress)
1131
283
  {
1132
283
    if (!bfd_init_section_decompress_status (abfd, newsect))
1133
185
      {
1134
185
        _bfd_error_handler
1135
    /* xgettext:c-format */
1136
185
    (_("%pB: unable to decompress section %s"), abfd, name);
1137
185
        return false;
1138
185
      }
1139
98
#ifndef HAVE_ZSTD
1140
98
    if (newsect->compress_status == DECOMPRESS_SECTION_ZSTD)
1141
4
      {
1142
4
        _bfd_error_handler
1143
      /* xgettext:c-format */
1144
4
      (_ ("%pB: section %s is compressed with zstd, but BFD "
1145
4
          "is not built with zstd support"),
1146
4
       abfd, name);
1147
4
        newsect->compress_status = COMPRESS_SECTION_NONE;
1148
4
        return false;
1149
4
      }
1150
94
#endif
1151
94
    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
94
  }
1162
46.3k
    }
1163
1164
539k
  return true;
1165
539k
}
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.2k
{
1192
15.2k
  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.2k
  if (output_bfd == NULL
1210
15.2k
      && !reloc_entry->howto->pc_relative
1211
14.5k
      && (symbol->section->flags & SEC_DEBUGGING) != 0
1212
4.33k
      && (input_section->flags & SEC_DEBUGGING) != 0)
1213
4.15k
    reloc_entry->addend -= symbol->section->output_section->vma;
1214
1215
15.2k
  return bfd_reloc_continue;
1216
15.2k
}
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
549
{
1226
549
  if (a->sh_type != b->sh_type
1227
57
      || ((a->sh_flags ^ b->sh_flags) & ~SHF_INFO_LINK) != 0
1228
46
      || a->sh_addralign != b->sh_addralign
1229
19
      || a->sh_entsize != b->sh_entsize)
1230
533
    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
48
{
1246
48
  Elf_Internal_Shdr ** oheaders = elf_elfsections (obfd);
1247
48
  unsigned int i;
1248
1249
48
  BFD_ASSERT (iheader != NULL);
1250
1251
  /* See PR 20922 for a reproducer of the NULL test.  */
1252
48
  if (hint < elf_numsections (obfd)
1253
48
      && oheaders[hint] != NULL
1254
48
      && section_match (oheaders[hint], iheader))
1255
0
    return hint;
1256
1257
533
  for (i = 1; i < elf_numsections (obfd); i++)
1258
501
    {
1259
501
      Elf_Internal_Shdr * oheader = oheaders[i];
1260
1261
501
      if (oheader == NULL)
1262
0
  continue;
1263
501
      if (section_match (oheader, iheader))
1264
  /* FIXME: Do we care if there is a potential for
1265
     multiple matches ?  */
1266
16
  return i;
1267
501
    }
1268
1269
32
  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
174
{
1283
174
  elf_backend_data *bed = get_elf_backend_data (obfd);
1284
174
  const Elf_Internal_Shdr **iheaders
1285
174
    = (const Elf_Internal_Shdr **) elf_elfsections (ibfd);
1286
174
  bool changed = false;
1287
174
  unsigned int sh_link;
1288
1289
174
  if (oheader->sh_type == SHT_NOBITS)
1290
22
    {
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
22
      if (oheader->sh_link == 0)
1308
21
  oheader->sh_link = iheader->sh_link;
1309
22
      if (oheader->sh_info == 0)
1310
22
  oheader->sh_info = iheader->sh_info;
1311
22
      return true;
1312
22
    }
1313
1314
  /* Allow the target a chance to decide how these fields should be set.  */
1315
152
  if (bed->elf_backend_copy_special_section_fields (ibfd, obfd,
1316
152
                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
69
  if (iheader->sh_link != SHN_UNDEF)
1324
42
    {
1325
      /* See PR 20931 for a reproducer.  */
1326
42
      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
42
      sh_link = find_link (obfd, iheaders[iheader->sh_link], iheader->sh_link);
1336
42
      if (sh_link != SHN_UNDEF)
1337
14
  {
1338
14
    oheader->sh_link = sh_link;
1339
14
    changed = true;
1340
14
  }
1341
28
      else
1342
  /* FIXME: Should we install iheader->sh_link
1343
     if we could not find a match ?  */
1344
28
  _bfd_error_handler
1345
    /* xgettext:c-format */
1346
28
    (_("%pB: failed to find link section for section %d"), obfd, secnum);
1347
42
    }
1348
1349
69
  if (iheader->sh_info)
1350
33
    {
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
33
      if (iheader->sh_flags & SHF_INFO_LINK)
1355
6
  {
1356
6
    sh_link = find_link (obfd, iheaders[iheader->sh_info],
1357
6
             iheader->sh_info);
1358
6
    if (sh_link != SHN_UNDEF)
1359
2
      oheader->sh_flags |= SHF_INFO_LINK;
1360
6
  }
1361
27
      else
1362
  /* No idea what it means - just copy it.  */
1363
27
  sh_link = iheader->sh_info;
1364
1365
33
      if (sh_link != SHN_UNDEF)
1366
29
  {
1367
29
    oheader->sh_info = sh_link;
1368
29
    changed = true;
1369
29
  }
1370
4
      else
1371
4
  _bfd_error_handler
1372
    /* xgettext:c-format */
1373
4
    (_("%pB: failed to find info section for section %d"), obfd, secnum);
1374
33
    }
1375
1376
69
  return changed;
1377
69
}
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
498
{
1385
498
  const Elf_Internal_Shdr **iheaders;
1386
498
  Elf_Internal_Shdr **oheaders;
1387
498
  elf_backend_data *bed;
1388
498
  unsigned int i;
1389
1390
498
  if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
1391
0
    return true;
1392
1393
498
  if (!elf_flags_init (obfd))
1394
443
    {
1395
443
      elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
1396
443
      elf_flags_init (obfd) = true;
1397
443
    }
1398
1399
498
  elf_gp (obfd) = elf_gp (ibfd);
1400
1401
  /* Also copy the EI_OSABI field.  */
1402
498
  elf_elfheader (obfd)->e_ident[EI_OSABI] =
1403
498
    elf_elfheader (ibfd)->e_ident[EI_OSABI];
1404
1405
  /* If set, copy the EI_ABIVERSION field.  */
1406
498
  if (elf_elfheader (ibfd)->e_ident[EI_ABIVERSION])
1407
100
    elf_elfheader (obfd)->e_ident[EI_ABIVERSION]
1408
100
      = elf_elfheader (ibfd)->e_ident[EI_ABIVERSION];
1409
1410
  /* Copy object attributes.  */
1411
498
  _bfd_elf_copy_obj_attributes (ibfd, obfd);
1412
1413
498
  iheaders = (const Elf_Internal_Shdr **) elf_elfsections (ibfd);
1414
498
  oheaders = elf_elfsections (obfd);
1415
498
  if (iheaders == NULL || oheaders == NULL)
1416
343
    return true;
1417
1418
155
  bed = get_elf_backend_data (obfd);
1419
1420
  /* Possibly copy other fields in the section header.  */
1421
1.91k
  for (i = 1; i < elf_numsections (obfd); i++)
1422
1.76k
    {
1423
1.76k
      unsigned int j;
1424
1.76k
      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.76k
      if (oheader == NULL
1430
1.76k
    || (oheader->sh_type != SHT_NOBITS
1431
1.73k
        && oheader->sh_type < SHT_LOOS))
1432
1.54k
  continue;
1433
1434
      /* Ignore empty sections, and sections whose
1435
   fields have already been initialised.  */
1436
213
      if (oheader->sh_size == 0
1437
197
    || (oheader->sh_info != 0 && oheader->sh_link != 0))
1438
45
  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.64k
      for (j = 1; j < elf_numsections (ibfd); j++)
1444
1.64k
  {
1445
1.64k
    const Elf_Internal_Shdr * iheader = iheaders[j];
1446
1447
1.64k
    if (iheader == NULL)
1448
0
      continue;
1449
1450
1.64k
    if (oheader->bfd_section != NULL
1451
1.64k
        && iheader->bfd_section != NULL
1452
1.39k
        && iheader->bfd_section->output_section != NULL
1453
1.39k
        && iheader->bfd_section->output_section == oheader->bfd_section)
1454
168
      {
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
168
        if (!copy_special_section_fields (ibfd, obfd,
1461
168
            iheader, oheader, i))
1462
27
    j = elf_numsections (ibfd);
1463
168
        break;
1464
168
      }
1465
1.64k
  }
1466
1467
168
      if (j < elf_numsections (ibfd))
1468
141
  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
408
      for (j = 1; j < elf_numsections (ibfd); j++)
1474
382
  {
1475
382
    const Elf_Internal_Shdr * iheader = iheaders[j];
1476
1477
382
    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
382
    if ((oheader->sh_type == SHT_NOBITS
1485
382
         || 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
17
        && iheader->sh_entsize == oheader->sh_entsize
1490
16
        && iheader->sh_size == oheader->sh_size
1491
16
        && iheader->sh_addr == oheader->sh_addr
1492
16
        && (iheader->sh_info != oheader->sh_info
1493
14
      || iheader->sh_link != oheader->sh_link))
1494
6
      {
1495
6
        if (copy_special_section_fields (ibfd, obfd, iheader, oheader, i))
1496
1
    break;
1497
6
      }
1498
382
  }
1499
1500
27
      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
27
    }
1508
1509
155
  return true;
1510
498
}
1511
1512
static const char *
1513
get_segment_type (unsigned int p_type)
1514
247k
{
1515
247k
  const char *pt;
1516
247k
  switch (p_type)
1517
247k
    {
1518
48.0k
    case PT_NULL: pt = "NULL"; break;
1519
2.92k
    case PT_LOAD: pt = "LOAD"; break;
1520
920
    case PT_DYNAMIC: pt = "DYNAMIC"; break;
1521
876
    case PT_INTERP: pt = "INTERP"; break;
1522
314
    case PT_NOTE: pt = "NOTE"; break;
1523
226
    case PT_SHLIB: pt = "SHLIB"; break;
1524
4.74k
    case PT_PHDR: pt = "PHDR"; break;
1525
152
    case PT_TLS: pt = "TLS"; break;
1526
157
    case PT_GNU_EH_FRAME: pt = "EH_FRAME"; break;
1527
132
    case PT_GNU_STACK: pt = "STACK"; break;
1528
112
    case PT_GNU_RELRO: pt = "RELRO"; break;
1529
1
    case PT_GNU_SFRAME: pt = "SFRAME"; break;
1530
188k
    default: pt = NULL; break;
1531
247k
    }
1532
247k
  return pt;
1533
247k
}
1534
1535
/* Print out the program headers.  */
1536
1537
bool
1538
_bfd_elf_print_private_bfd_data (bfd *abfd, void *farg)
1539
16.9k
{
1540
16.9k
  FILE *f = (FILE *) farg;
1541
16.9k
  Elf_Internal_Phdr *p;
1542
16.9k
  asection *s;
1543
16.9k
  bfd_byte *dynbuf = NULL;
1544
1545
16.9k
  p = elf_tdata (abfd)->phdr;
1546
16.9k
  if (p != NULL)
1547
11.6k
    {
1548
11.6k
      unsigned int i, c;
1549
1550
11.6k
      fprintf (f, _("\nProgram Header:\n"));
1551
11.6k
      c = elf_elfheader (abfd)->e_phnum;
1552
259k
      for (i = 0; i < c; i++, p++)
1553
247k
  {
1554
247k
    const char *pt = get_segment_type (p->p_type);
1555
247k
    char buf[20];
1556
1557
247k
    if (pt == NULL)
1558
188k
      {
1559
188k
        sprintf (buf, "0x%lx", p->p_type);
1560
188k
        pt = buf;
1561
188k
      }
1562
247k
    fprintf (f, "%8s off    0x", pt);
1563
247k
    bfd_fprintf_vma (abfd, f, p->p_offset);
1564
247k
    fprintf (f, " vaddr 0x");
1565
247k
    bfd_fprintf_vma (abfd, f, p->p_vaddr);
1566
247k
    fprintf (f, " paddr 0x");
1567
247k
    bfd_fprintf_vma (abfd, f, p->p_paddr);
1568
247k
    fprintf (f, " align 2**%u\n", bfd_log2 (p->p_align));
1569
247k
    fprintf (f, "         filesz 0x");
1570
247k
    bfd_fprintf_vma (abfd, f, p->p_filesz);
1571
247k
    fprintf (f, " memsz 0x");
1572
247k
    bfd_fprintf_vma (abfd, f, p->p_memsz);
1573
247k
    fprintf (f, " flags %c%c%c",
1574
247k
       (p->p_flags & PF_R) != 0 ? 'r' : '-',
1575
247k
       (p->p_flags & PF_W) != 0 ? 'w' : '-',
1576
247k
       (p->p_flags & PF_X) != 0 ? 'x' : '-');
1577
247k
    if ((p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)) != 0)
1578
171k
      fprintf (f, " %lx", p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X));
1579
247k
    fprintf (f, "\n");
1580
247k
  }
1581
11.6k
    }
1582
1583
16.9k
  s = bfd_get_section_by_name (abfd, ".dynamic");
1584
16.9k
  if (s != NULL && (s->flags & SEC_HAS_CONTENTS) != 0)
1585
173
    {
1586
173
      unsigned int elfsec;
1587
173
      unsigned long shlink;
1588
173
      bfd_byte *extdyn, *extdynend;
1589
173
      size_t extdynsize;
1590
173
      void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
1591
1592
173
      fprintf (f, _("\nDynamic Section:\n"));
1593
1594
173
      if (!_bfd_elf_mmap_section_contents (abfd, s, &dynbuf))
1595
15
  goto error_return;
1596
1597
158
      elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
1598
158
      if (elfsec == SHN_BAD)
1599
0
  goto error_return;
1600
158
      shlink = elf_elfsections (abfd)[elfsec]->sh_link;
1601
1602
158
      extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
1603
158
      swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
1604
1605
158
      for (extdyn = dynbuf, extdynend = dynbuf + s->size;
1606
2.55k
     (size_t) (extdynend - extdyn) >= extdynsize;
1607
2.39k
     extdyn += extdynsize)
1608
2.52k
  {
1609
2.52k
    Elf_Internal_Dyn dyn;
1610
2.52k
    const char *name = "";
1611
2.52k
    char ab[20];
1612
2.52k
    bool stringp;
1613
2.52k
    elf_backend_data *bed = get_elf_backend_data (abfd);
1614
1615
2.52k
    (*swap_dyn_in) (abfd, extdyn, &dyn);
1616
1617
2.52k
    if (dyn.d_tag == DT_NULL)
1618
125
      break;
1619
1620
2.39k
    stringp = false;
1621
2.39k
    switch (dyn.d_tag)
1622
2.39k
      {
1623
794
      default:
1624
794
        if (bed->elf_backend_get_target_dtag)
1625
96
    name = (*bed->elf_backend_get_target_dtag) (dyn.d_tag);
1626
1627
794
        if (streq (name, ""))
1628
794
    {
1629
794
      sprintf (ab, "%#" PRIx64, (uint64_t) dyn.d_tag);
1630
794
      name = ab;
1631
794
    }
1632
794
        break;
1633
1634
87
      case DT_NEEDED: name = "NEEDED"; stringp = true; break;
1635
38
      case DT_PLTRELSZ: name = "PLTRELSZ"; break;
1636
62
      case DT_PLTGOT: name = "PLTGOT"; break;
1637
68
      case DT_HASH: name = "HASH"; break;
1638
88
      case DT_STRTAB: name = "STRTAB"; break;
1639
93
      case DT_SYMTAB: name = "SYMTAB"; break;
1640
58
      case DT_RELA: name = "RELA"; break;
1641
76
      case DT_RELASZ: name = "RELASZ"; break;
1642
60
      case DT_RELAENT: name = "RELAENT"; break;
1643
74
      case DT_STRSZ: name = "STRSZ"; break;
1644
78
      case DT_SYMENT: name = "SYMENT"; break;
1645
66
      case DT_INIT: name = "INIT"; break;
1646
64
      case DT_FINI: name = "FINI"; break;
1647
3
      case DT_SONAME: name = "SONAME"; stringp = true; break;
1648
14
      case DT_RPATH: name = "RPATH"; stringp = true; break;
1649
4
      case DT_SYMBOLIC: name = "SYMBOLIC"; break;
1650
4
      case DT_REL: name = "REL"; break;
1651
4
      case DT_RELSZ: name = "RELSZ"; break;
1652
6
      case DT_RELENT: name = "RELENT"; break;
1653
5
      case DT_RELR: name = "RELR"; break;
1654
1
      case DT_RELRSZ: name = "RELRSZ"; break;
1655
0
      case DT_RELRENT: name = "RELRENT"; break;
1656
38
      case DT_PLTREL: name = "PLTREL"; break;
1657
59
      case DT_DEBUG: name = "DEBUG"; break;
1658
1
      case DT_TEXTREL: name = "TEXTREL"; break;
1659
33
      case DT_JMPREL: name = "JMPREL"; break;
1660
5
      case DT_BIND_NOW: name = "BIND_NOW"; break;
1661
36
      case DT_INIT_ARRAY: name = "INIT_ARRAY"; break;
1662
34
      case DT_FINI_ARRAY: name = "FINI_ARRAY"; break;
1663
34
      case DT_INIT_ARRAYSZ: name = "INIT_ARRAYSZ"; break;
1664
34
      case DT_FINI_ARRAYSZ: name = "FINI_ARRAYSZ"; break;
1665
0
      case DT_RUNPATH: name = "RUNPATH"; stringp = true; break;
1666
29
      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
3
      case DT_RELCOUNT: name = "RELCOUNT"; break;
1685
38
      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
69
      case DT_VERNEED: name = "VERNEED"; break;
1690
60
      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
67
      case DT_GNU_HASH: name = "GNU_HASH"; break;
1695
2.39k
      }
1696
1697
2.39k
    fprintf (f, "  %-20s ", name);
1698
2.39k
    if (! stringp)
1699
2.29k
      {
1700
2.29k
        fprintf (f, "0x");
1701
2.29k
        bfd_fprintf_vma (abfd, f, dyn.d_un.d_val);
1702
2.29k
      }
1703
104
    else
1704
104
      {
1705
104
        const char *string;
1706
104
        unsigned int tagv = dyn.d_un.d_val;
1707
1708
104
        string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
1709
104
        if (string == NULL)
1710
4
    goto error_return;
1711
100
        fprintf (f, "%s", string);
1712
100
      }
1713
2.39k
    fprintf (f, "\n");
1714
2.39k
  }
1715
1716
154
      _bfd_elf_munmap_section_contents (s, dynbuf);
1717
154
      dynbuf = NULL;
1718
154
    }
1719
1720
16.9k
  if ((elf_dynverdef (abfd) != 0 && elf_tdata (abfd)->verdef == NULL)
1721
16.8k
      || (elf_dynverref (abfd) != 0 && elf_tdata (abfd)->verref == NULL))
1722
165
    {
1723
165
      if (! _bfd_elf_slurp_version_tables (abfd, false))
1724
83
  return false;
1725
165
    }
1726
1727
16.8k
  if (elf_dynverdef (abfd) != 0)
1728
11
    {
1729
11
      Elf_Internal_Verdef *t;
1730
1731
11
      fprintf (f, _("\nVersion definitions:\n"));
1732
22
      for (t = elf_tdata (abfd)->verdef; t != NULL; t = t->vd_nextdef)
1733
11
  {
1734
11
    fprintf (f, "%d 0x%2.2x 0x%8.8lx %s\n", t->vd_ndx,
1735
11
       t->vd_flags, t->vd_hash,
1736
11
       t->vd_nodename ? t->vd_nodename : "<corrupt>");
1737
11
    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
11
  }
1750
11
    }
1751
1752
16.8k
  if (elf_dynverref (abfd) != 0)
1753
81
    {
1754
81
      Elf_Internal_Verneed *t;
1755
1756
81
      fprintf (f, _("\nVersion References:\n"));
1757
162
      for (t = elf_tdata (abfd)->verref; t != NULL; t = t->vn_nextref)
1758
81
  {
1759
81
    Elf_Internal_Vernaux *a;
1760
1761
81
    fprintf (f, _("  required from %s:\n"),
1762
81
       t->vn_filename ? t->vn_filename : "<corrupt>");
1763
196
    for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1764
115
      fprintf (f, "    0x%8.8lx 0x%2.2x %2.2d %s\n", a->vna_hash,
1765
115
         a->vna_flags, a->vna_other,
1766
115
         a->vna_nodename ? a->vna_nodename : "<corrupt>");
1767
81
  }
1768
81
    }
1769
1770
16.8k
  return true;
1771
1772
19
 error_return:
1773
19
  _bfd_elf_munmap_section_contents (s, dynbuf);
1774
19
  return false;
1775
16.9k
}
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
3.93k
{
1784
3.93k
  Elf_Internal_Phdr *seg;
1785
3.93k
  size_t i;
1786
1787
14.2k
  for (seg = phdrs, i = 0; i < phnum; ++seg, ++i)
1788
13.8k
    if (seg->p_type == PT_LOAD
1789
5.56k
  && vma >= (seg->p_vaddr & -seg->p_align)
1790
4.62k
  && vma + size <= seg->p_vaddr + seg->p_filesz)
1791
3.56k
      {
1792
3.56k
  if (max_size_p)
1793
59
    *max_size_p = seg->p_vaddr + seg->p_filesz - vma;
1794
3.56k
  return vma - seg->p_vaddr + seg->p_offset;
1795
3.56k
      }
1796
1797
374
  if (max_size_p)
1798
42
    *max_size_p = 0;
1799
374
  bfd_set_error (bfd_error_invalid_operation);
1800
374
  return (file_ptr) -1;
1801
3.93k
}
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
554
{
1809
554
  unsigned char *e_data = NULL;
1810
554
  bfd_vma *i_data = NULL;
1811
554
  bfd_size_type size;
1812
554
  void *e_data_addr;
1813
554
  size_t e_data_size ATTRIBUTE_UNUSED;
1814
1815
554
  if (ent_size != 4 && ent_size != 8)
1816
0
    return NULL;
1817
1818
554
  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
554
  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
554
  if (size > filesize
1828
513
      || number >= ~(size_t) 0 / ent_size
1829
513
      || number >= ~(size_t) 0 / sizeof (*i_data))
1830
41
    {
1831
41
      bfd_set_error (bfd_error_file_too_big);
1832
41
      return NULL;
1833
41
    }
1834
1835
513
  e_data = _bfd_mmap_temporary (abfd, size, &e_data_addr, &e_data_size);
1836
513
  if (e_data == NULL)
1837
31
    return NULL;
1838
1839
482
  i_data = bfd_malloc (number * sizeof (*i_data));
1840
482
  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
482
  if (ent_size == 4)
1847
240k
    while (number--)
1848
240k
      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
482
  _bfd_munmap_temporary (e_data_addr, e_data_size);
1854
482
  return i_data;
1855
482
}
1856
1857
/* Address of .MIPS.xhash section.  FIXME: What is the best way to
1858
   support DT_MIPS_XHASH?  */
1859
118k
#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.15k
{
1868
4.15k
  bfd_byte *extdyn, *extdynend;
1869
4.15k
  size_t extdynsize;
1870
4.15k
  void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
1871
4.15k
  bool (*swap_symbol_in) (bfd *, const void *, const void *,
1872
4.15k
        Elf_Internal_Sym *);
1873
4.15k
  Elf_Internal_Dyn dyn;
1874
4.15k
  bfd_vma dt_hash = 0;
1875
4.15k
  bfd_vma dt_gnu_hash = 0;
1876
4.15k
  bfd_vma dt_mips_xhash = 0;
1877
4.15k
  bfd_vma dt_strtab = 0;
1878
4.15k
  bfd_vma dt_symtab = 0;
1879
4.15k
  size_t dt_strsz = 0;
1880
4.15k
  bfd_vma dt_versym = 0;
1881
4.15k
  bfd_vma dt_verdef = 0;
1882
4.15k
  bfd_vma dt_verneed = 0;
1883
4.15k
  bfd_byte *dynbuf = NULL;
1884
4.15k
  char *strbuf = NULL;
1885
4.15k
  bfd_vma *gnubuckets = NULL;
1886
4.15k
  bfd_vma *gnuchains = NULL;
1887
4.15k
  bfd_vma *mipsxlat = NULL;
1888
4.15k
  file_ptr saved_filepos, filepos;
1889
4.15k
  bool res = false;
1890
4.15k
  size_t amt;
1891
4.15k
  bfd_byte *esymbuf = NULL, *esym;
1892
4.15k
  bfd_size_type symcount;
1893
4.15k
  Elf_Internal_Sym *isymbuf = NULL;
1894
4.15k
  Elf_Internal_Sym *isym, *isymend;
1895
4.15k
  bfd_byte *versym = NULL;
1896
4.15k
  bfd_byte *verdef = NULL;
1897
4.15k
  bfd_byte *verneed = NULL;
1898
4.15k
  size_t verdef_size = 0;
1899
4.15k
  size_t verneed_size = 0;
1900
4.15k
  size_t extsym_size;
1901
4.15k
  elf_backend_data *bed;
1902
4.15k
  void *dynbuf_addr = NULL;
1903
4.15k
  void *esymbuf_addr = NULL;
1904
4.15k
  size_t dynbuf_size = 0;
1905
4.15k
  size_t esymbuf_size = 0;
1906
1907
  /* Return TRUE if symbol table is bad.  */
1908
4.15k
  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.15k
  if (elf_tdata (abfd)->dt_strtab != NULL)
1913
0
    return true;
1914
1915
4.15k
  bed = get_elf_backend_data (abfd);
1916
1917
  /* Save file position for elf_object_p.  */
1918
4.15k
  saved_filepos = bfd_tell (abfd);
1919
1920
4.15k
  if (bfd_seek (abfd, phdr->p_offset, SEEK_SET) != 0)
1921
0
    goto error_return;
1922
1923
4.15k
  dynbuf_size = phdr->p_filesz;
1924
4.15k
  dynbuf = _bfd_mmap_temporary (abfd, dynbuf_size, &dynbuf_addr, &dynbuf_size);
1925
4.15k
  if (dynbuf == NULL)
1926
85
    goto error_return;
1927
1928
4.06k
  extsym_size = bed->s->sizeof_sym;
1929
4.06k
  extdynsize = bed->s->sizeof_dyn;
1930
4.06k
  swap_dyn_in = bed->s->swap_dyn_in;
1931
1932
4.06k
  extdyn = dynbuf;
1933
4.06k
  if (phdr->p_filesz < extdynsize)
1934
91
    goto error_return;
1935
3.97k
  extdynend = extdyn + phdr->p_filesz;
1936
73.6k
  for (; extdyn <= (extdynend - extdynsize); extdyn += extdynsize)
1937
71.6k
    {
1938
71.6k
      swap_dyn_in (abfd, extdyn, &dyn);
1939
1940
71.6k
      if (dyn.d_tag == DT_NULL)
1941
1.79k
  break;
1942
1943
69.8k
      switch (dyn.d_tag)
1944
69.8k
  {
1945
1.27k
  case DT_HASH:
1946
1.27k
    dt_hash = dyn.d_un.d_val;
1947
1.27k
    break;
1948
1.19k
  case DT_GNU_HASH:
1949
1.19k
    if (bed->elf_machine_code != EM_MIPS
1950
976
        && bed->elf_machine_code != EM_MIPS_RS3_LE)
1951
976
      dt_gnu_hash = dyn.d_un.d_val;
1952
1.19k
    break;
1953
1.87k
  case DT_STRTAB:
1954
1.87k
    dt_strtab = dyn.d_un.d_val;
1955
1.87k
    break;
1956
1.86k
  case DT_SYMTAB:
1957
1.86k
    dt_symtab = dyn.d_un.d_val;
1958
1.86k
    break;
1959
1.72k
  case DT_STRSZ:
1960
1.72k
    dt_strsz = dyn.d_un.d_val;
1961
1.72k
    break;
1962
826
  case DT_SYMENT:
1963
826
    if (dyn.d_un.d_val != extsym_size)
1964
202
      goto error_return;
1965
624
    break;
1966
846
  case DT_VERSYM:
1967
846
    dt_versym = dyn.d_un.d_val;
1968
846
    break;
1969
442
  case DT_VERDEF:
1970
442
    dt_verdef = dyn.d_un.d_val;
1971
442
    break;
1972
461
  case DT_VERNEED:
1973
461
    dt_verneed = dyn.d_un.d_val;
1974
461
    break;
1975
59.3k
  default:
1976
59.3k
    if (dyn.d_tag == DT_MIPS_XHASH
1977
710
        && (bed->elf_machine_code == EM_MIPS
1978
359
      || bed->elf_machine_code == EM_MIPS_RS3_LE))
1979
351
      {
1980
351
        dt_gnu_hash = dyn.d_un.d_val;
1981
351
        dt_mips_xhash = dyn.d_un.d_val;
1982
351
      }
1983
59.3k
    break;
1984
69.8k
  }
1985
69.8k
    }
1986
1987
  /* Check if we can reconstruct dynamic symbol table from PT_DYNAMIC
1988
     segment.  */
1989
3.77k
  if ((!dt_hash && !dt_gnu_hash)
1990
2.08k
      || !dt_strtab
1991
1.54k
      || !dt_symtab
1992
1.36k
      || !dt_strsz)
1993
2.54k
    goto error_return;
1994
1995
  /* Get dynamic string table.  */
1996
1.23k
  filepos = offset_from_vma (phdrs, phnum, dt_strtab, dt_strsz, NULL);
1997
1.23k
  if (filepos == (file_ptr) -1
1998
1.14k
      || 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
1.08k
  strbuf = (char *) _bfd_mmap_persistent (abfd, dt_strsz);
2003
1.08k
  if (strbuf == NULL)
2004
24
    goto error_return;
2005
1.06k
  if (strbuf[dt_strsz - 1] != 0)
2006
386
    {
2007
      /* It is an error if a string table is't terminated.  */
2008
386
      _bfd_error_handler
2009
  /* xgettext:c-format */
2010
386
  (_("%pB: DT_STRTAB table is corrupt"), abfd);
2011
386
      strbuf[dt_strsz - 1] = 0;
2012
386
    }
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
1.06k
  if (dt_hash)
2017
479
    {
2018
479
      unsigned char nb[16];
2019
479
      unsigned int hash_ent_size;
2020
2021
479
      switch (bed->elf_machine_code)
2022
479
  {
2023
0
  case EM_ALPHA:
2024
2
  case EM_S390:
2025
2
  case EM_S390_OLD:
2026
2
    if (bed->s->elfclass == ELFCLASS64)
2027
0
      {
2028
0
        hash_ent_size = 8;
2029
0
        break;
2030
0
      }
2031
    /* FALLTHROUGH */
2032
479
  default:
2033
479
    hash_ent_size = 4;
2034
479
    break;
2035
479
  }
2036
2037
479
      filepos = offset_from_vma (phdrs, phnum, dt_hash, 2 * hash_ent_size,
2038
479
         NULL);
2039
479
      if (filepos == (file_ptr) -1
2040
428
    || bfd_seek (abfd, filepos, SEEK_SET) != 0
2041
391
    || bfd_read (nb, 2 * hash_ent_size, abfd) != 2 * hash_ent_size)
2042
107
  goto error_return;
2043
2044
      /* The number of dynamic symbol table entries equals the number
2045
   of chains.  */
2046
372
      if (hash_ent_size == 8)
2047
0
  symcount = bfd_get_64 (abfd, nb + hash_ent_size);
2048
372
      else
2049
372
  symcount = bfd_get_32 (abfd, nb + hash_ent_size);
2050
372
    }
2051
582
  else
2052
582
    {
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
582
      unsigned char nb[16];
2060
582
      bfd_vma ngnubuckets;
2061
582
      bfd_vma gnusymidx;
2062
582
      size_t i, ngnuchains;
2063
582
      bfd_vma maxchain = 0xffffffff, bitmaskwords;
2064
582
      bfd_vma buckets_vma;
2065
2066
582
      filepos = offset_from_vma (phdrs, phnum, dt_gnu_hash,
2067
582
         sizeof (nb), NULL);
2068
582
      if (filepos == (file_ptr) -1
2069
549
    || bfd_seek (abfd, filepos, SEEK_SET) != 0
2070
525
    || bfd_read (nb, sizeof (nb), abfd) != sizeof (nb))
2071
64
  goto error_return;
2072
2073
518
      ngnubuckets = bfd_get_32 (abfd, nb);
2074
518
      gnusymidx = bfd_get_32 (abfd, nb + 4);
2075
518
      bitmaskwords = bfd_get_32 (abfd, nb + 8);
2076
518
      buckets_vma = dt_gnu_hash + 16;
2077
518
      if (bed->s->elfclass == ELFCLASS32)
2078
0
  buckets_vma += bitmaskwords * 4;
2079
518
      else
2080
518
  buckets_vma += bitmaskwords * 8;
2081
518
      filepos = offset_from_vma (phdrs, phnum, buckets_vma, 4, NULL);
2082
518
      if (filepos == (file_ptr) -1
2083
488
    || bfd_seek (abfd, filepos, SEEK_SET) != 0)
2084
70
  goto error_return;
2085
2086
448
      gnubuckets = get_hash_table_data (abfd, ngnubuckets, 4, filesize);
2087
448
      if (gnubuckets == NULL)
2088
72
  goto error_return;
2089
2090
212k
      for (i = 0; i < ngnubuckets; i++)
2091
211k
  if (gnubuckets[i] != 0)
2092
162k
    {
2093
162k
      if (gnubuckets[i] < gnusymidx)
2094
31
        goto error_return;
2095
2096
162k
      if (maxchain == 0xffffffff || gnubuckets[i] > maxchain)
2097
18.4k
        maxchain = gnubuckets[i];
2098
162k
    }
2099
2100
345
      if (maxchain == 0xffffffff)
2101
65
  {
2102
65
    symcount = 0;
2103
65
    goto empty_gnu_hash;
2104
65
  }
2105
2106
280
      maxchain -= gnusymidx;
2107
280
      filepos = offset_from_vma (phdrs, phnum,
2108
280
         buckets_vma + 4 * (ngnubuckets + maxchain),
2109
280
         4, NULL);
2110
280
      if (filepos == (file_ptr) -1
2111
234
    || bfd_seek (abfd, filepos, SEEK_SET) != 0)
2112
159
  goto error_return;
2113
2114
121
      do
2115
1.83k
  {
2116
1.83k
    if (bfd_read (nb, 4, abfd) != 4)
2117
15
      goto error_return;
2118
1.81k
    ++maxchain;
2119
1.81k
    if (maxchain == 0)
2120
0
      goto error_return;
2121
1.81k
  }
2122
1.81k
      while ((bfd_get_32 (abfd, nb) & 1) == 0);
2123
2124
106
      filepos = offset_from_vma (phdrs, phnum,
2125
106
         buckets_vma + 4 * ngnubuckets,
2126
106
         4, NULL);
2127
106
      if (filepos == (file_ptr) -1
2128
106
    || bfd_seek (abfd, filepos, SEEK_SET) != 0)
2129
0
  goto error_return;
2130
2131
106
      gnuchains = get_hash_table_data (abfd, maxchain, 4, filesize);
2132
106
      if (gnuchains == NULL)
2133
0
  goto error_return;
2134
106
      ngnuchains = maxchain;
2135
2136
106
      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
106
      symcount = 0;
2151
3.76k
      for (i = 0; i < ngnubuckets; ++i)
2152
3.66k
  if (gnubuckets[i] != 0)
2153
1.78k
    {
2154
1.78k
      bfd_vma si = gnubuckets[i];
2155
1.78k
      bfd_vma off = si - gnusymidx;
2156
1.78k
      do
2157
5.34k
        {
2158
5.34k
    if (mipsxlat)
2159
0
      {
2160
0
        if (mipsxlat[off] >= symcount)
2161
0
          symcount = mipsxlat[off] + 1;
2162
0
      }
2163
5.34k
    else
2164
5.34k
      {
2165
5.34k
        if (si >= symcount)
2166
1.80k
          symcount = si + 1;
2167
5.34k
      }
2168
5.34k
    si++;
2169
5.34k
        }
2170
5.34k
      while (off < ngnuchains && (gnuchains[off++] & 1) == 0);
2171
1.78k
    }
2172
106
    }
2173
2174
  /* Swap in dynamic symbol table.  */
2175
478
  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
478
  filepos = offset_from_vma (phdrs, phnum, dt_symtab, amt, NULL);
2182
478
  if (filepos == (file_ptr) -1
2183
421
      || bfd_seek (abfd, filepos, SEEK_SET) != 0)
2184
122
    goto error_return;
2185
356
  esymbuf_size = amt;
2186
356
  esymbuf = _bfd_mmap_temporary (abfd, esymbuf_size,
2187
356
         &esymbuf_addr, &esymbuf_size);
2188
356
  if (esymbuf == NULL)
2189
47
    goto error_return;
2190
2191
309
  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
309
  isymbuf = bfd_alloc (abfd, amt);
2199
309
  if (isymbuf == NULL)
2200
0
    goto error_return;
2201
2202
309
  swap_symbol_in = bed->s->swap_symbol_in;
2203
2204
  /* Convert the symbols to internal form.  */
2205
309
  isymend = isymbuf + symcount;
2206
309
  for (esym = esymbuf, isym = isymbuf;
2207
1.27k
       isym < isymend;
2208
962
       esym += extsym_size, isym++)
2209
1.04k
    if (!swap_symbol_in (abfd, esym, NULL, isym)
2210
1.01k
  || isym->st_name >= dt_strsz)
2211
79
      {
2212
79
  bfd_set_error (bfd_error_invalid_operation);
2213
79
  goto error_return;
2214
79
      }
2215
2216
230
  if (dt_versym)
2217
157
    {
2218
      /* Swap in DT_VERSYM.  */
2219
157
      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
157
      filepos = offset_from_vma (phdrs, phnum, dt_versym, amt, NULL);
2226
157
      if (filepos == (file_ptr) -1
2227
136
    || bfd_seek (abfd, filepos, SEEK_SET) != 0)
2228
38
  goto error_return;
2229
2230
      /* DT_VERSYM info must be valid until ABFD is closed.  */
2231
119
      versym = _bfd_mmap_persistent (abfd, amt);
2232
2233
119
      if (dt_verdef)
2234
57
  {
2235
    /* Read in DT_VERDEF.  */
2236
57
    filepos = offset_from_vma (phdrs, phnum, dt_verdef,
2237
57
             0, &verdef_size);
2238
57
    if (filepos == (file_ptr) -1
2239
32
        || bfd_seek (abfd, filepos, SEEK_SET) != 0)
2240
42
      goto error_return;
2241
2242
    /* DT_VERDEF info must be valid until ABFD is closed.  */
2243
15
    verdef = _bfd_mmap_persistent (abfd, verdef_size);
2244
15
  }
2245
2246
77
      if (dt_verneed)
2247
44
  {
2248
    /* Read in DT_VERNEED.  */
2249
44
    filepos = offset_from_vma (phdrs, phnum, dt_verneed,
2250
44
             0, &verneed_size);
2251
44
    if (filepos == (file_ptr) -1
2252
27
        || bfd_seek (abfd, filepos, SEEK_SET) != 0)
2253
22
      goto error_return;
2254
2255
    /* DT_VERNEED info must be valid until ABFD is closed.  */
2256
22
    verneed = _bfd_mmap_persistent (abfd, verneed_size);
2257
22
  }
2258
77
    }
2259
2260
193
 empty_gnu_hash:
2261
193
  elf_tdata (abfd)->dt_strtab = strbuf;
2262
193
  elf_tdata (abfd)->dt_strsz = dt_strsz;
2263
193
  elf_tdata (abfd)->dt_symtab = isymbuf;
2264
193
  elf_tdata (abfd)->dt_symtab_count = symcount;
2265
193
  elf_tdata (abfd)->dt_versym = versym;
2266
193
  elf_tdata (abfd)->dt_verdef = verdef;
2267
193
  elf_tdata (abfd)->dt_verneed = verneed;
2268
193
  elf_tdata (abfd)->dt_verdef_count
2269
193
    = verdef_size / sizeof (Elf_External_Verdef);
2270
193
  elf_tdata (abfd)->dt_verneed_count
2271
193
    = verneed_size / sizeof (Elf_External_Verneed);
2272
2273
193
  res = true;
2274
2275
4.15k
 error_return:
2276
  /* Restore file position for elf_object_p.  */
2277
4.15k
  if (bfd_seek (abfd, saved_filepos, SEEK_SET) != 0)
2278
0
    res = false;
2279
4.15k
  _bfd_munmap_temporary (dynbuf_addr, dynbuf_size);
2280
4.15k
  _bfd_munmap_temporary (esymbuf_addr, esymbuf_size);
2281
4.15k
  free (gnubuckets);
2282
4.15k
  free (gnuchains);
2283
4.15k
  free (mipsxlat);
2284
4.15k
  return res;
2285
193
}
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
54.2k
{
2345
54.2k
  const char *version_string = NULL;
2346
54.2k
  if ((elf_dynversym (abfd) != 0
2347
5.56k
       && (elf_dynverdef (abfd) != 0 || elf_dynverref (abfd) != 0))
2348
48.7k
      || (elf_tdata (abfd)->dt_versym != NULL
2349
0
    && (elf_tdata (abfd)->dt_verdef != NULL
2350
0
        || elf_tdata (abfd)->dt_verneed != NULL)))
2351
5.49k
    {
2352
5.49k
      unsigned int vernum = ((elf_symbol_type *) symbol)->version;
2353
2354
5.49k
      *hidden = (vernum & VERSYM_HIDDEN) != 0;
2355
5.49k
      vernum &= VERSYM_VERSION;
2356
2357
5.49k
      if (vernum == 0)
2358
1.63k
  version_string = "";
2359
3.85k
      else if (vernum == 1
2360
2.77k
         && (vernum > elf_tdata (abfd)->cverdefs
2361
0
       || (elf_tdata (abfd)->verdef[0].vd_flags
2362
0
           == VER_FLG_BASE)))
2363
2.77k
  version_string = base_p ? "Base" : "";
2364
1.08k
      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
1.08k
      else
2376
1.08k
  {
2377
1.08k
    Elf_Internal_Verneed *t;
2378
2379
1.08k
    version_string = _("<corrupt>");
2380
1.08k
    for (t = elf_tdata (abfd)->verref;
2381
2.17k
         t != NULL;
2382
1.08k
         t = t->vn_nextref)
2383
1.08k
      {
2384
1.08k
        Elf_Internal_Vernaux *a;
2385
2386
2.30k
        for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2387
2.05k
    {
2388
2.05k
      if (a->vna_other == vernum)
2389
838
        {
2390
838
          *hidden = true;
2391
838
          version_string = a->vna_nodename;
2392
838
          break;
2393
838
        }
2394
2.05k
    }
2395
1.08k
      }
2396
1.08k
  }
2397
5.49k
    }
2398
54.2k
  return version_string;
2399
54.2k
}
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.02M
{
2502
1.02M
  Elf_Internal_Shdr *hdr;
2503
1.02M
  Elf_Internal_Ehdr *ehdr;
2504
1.02M
  elf_backend_data *bed;
2505
1.02M
  const char *name;
2506
1.02M
  bool ret = true;
2507
2508
1.02M
  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.02M
  if (elf_tdata (abfd)->being_created[shindex])
2515
28
    {
2516
28
      _bfd_error_handler
2517
28
  (_("%pB: warning: loop in section dependencies detected"), abfd);
2518
28
      return false;
2519
28
    }
2520
1.02M
  elf_tdata (abfd)->being_created[shindex] = true;
2521
2522
1.02M
  hdr = elf_elfsections (abfd)[shindex];
2523
1.02M
  ehdr = elf_elfheader (abfd);
2524
1.02M
  name = bfd_elf_string_from_elf_section (abfd, ehdr->e_shstrndx,
2525
1.02M
            hdr->sh_name);
2526
1.02M
  if (name == NULL)
2527
8.64k
    goto fail;
2528
2529
1.01M
  bed = get_elf_backend_data (abfd);
2530
1.01M
  switch (hdr->sh_type)
2531
1.01M
    {
2532
75.1k
    case SHT_NULL:
2533
      /* Inactive section. Throw it away.  */
2534
75.1k
      goto success;
2535
2536
299k
    case SHT_PROGBITS:   /* Normal section with contents.  */
2537
313k
    case SHT_NOBITS:   /* .bss section.  */
2538
314k
    case SHT_HASH:   /* .hash section.  */
2539
325k
    case SHT_NOTE:   /* .note section.  */
2540
343k
    case SHT_INIT_ARRAY: /* .init_array section.  */
2541
352k
    case SHT_FINI_ARRAY: /* .fini_array section.  */
2542
355k
    case SHT_PREINIT_ARRAY: /* .preinit_array section.  */
2543
356k
    case SHT_GNU_LIBLIST: /* .gnu.liblist section.  */
2544
356k
    case SHT_GNU_HASH:   /* .gnu.hash section.  */
2545
356k
    case SHT_GNU_SFRAME: /* .sframe section.  */
2546
356k
      ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2547
356k
      goto success;
2548
2549
3.44k
    case SHT_DYNAMIC: /* Dynamic linking information.  */
2550
3.44k
      if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
2551
49
  goto fail;
2552
2553
3.39k
      if (hdr->sh_link > elf_numsections (abfd))
2554
203
  {
2555
    /* PR 10478: Accept Solaris binaries with a sh_link field
2556
       set to SHN_BEFORE (LORESERVE) or SHN_AFTER (LORESERVE+1).  */
2557
203
    switch (bfd_get_arch (abfd))
2558
203
      {
2559
143
      case bfd_arch_i386:
2560
203
      case bfd_arch_sparc:
2561
203
        if (hdr->sh_link == (SHN_LORESERVE & 0xffff)
2562
48
      || hdr->sh_link == ((SHN_LORESERVE + 1) & 0xffff))
2563
203
    break;
2564
        /* Otherwise fall through.  */
2565
0
      default:
2566
0
        goto fail;
2567
203
      }
2568
203
  }
2569
3.19k
      else if (elf_elfsections (abfd)[hdr->sh_link] == NULL)
2570
0
  goto fail;
2571
3.19k
      else if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_STRTAB)
2572
1.49k
  {
2573
1.49k
    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.49k
    if (elf_dynsymtab (abfd) != 0)
2579
318
      {
2580
318
        dynsymhdr = elf_elfsections (abfd)[elf_dynsymtab (abfd)];
2581
318
        hdr->sh_link = dynsymhdr->sh_link;
2582
318
      }
2583
1.17k
    else
2584
1.17k
      {
2585
1.17k
        unsigned int i, num_sec;
2586
2587
1.17k
        num_sec = elf_numsections (abfd);
2588
19.9k
        for (i = 1; i < num_sec; i++)
2589
18.9k
    {
2590
18.9k
      dynsymhdr = elf_elfsections (abfd)[i];
2591
18.9k
      if (dynsymhdr->sh_type == SHT_DYNSYM)
2592
173
        {
2593
173
          hdr->sh_link = dynsymhdr->sh_link;
2594
173
          break;
2595
173
        }
2596
18.9k
    }
2597
1.17k
      }
2598
1.49k
  }
2599
3.39k
      goto success;
2600
2601
129k
    case SHT_SYMTAB:   /* A symbol table.  */
2602
129k
      if (elf_onesymtab (abfd) == shindex)
2603
100k
  goto success;
2604
2605
29.3k
      if (hdr->sh_entsize != bed->s->sizeof_sym)
2606
697
  goto fail;
2607
2608
28.6k
      if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size)
2609
1.35k
  {
2610
1.35k
    if (hdr->sh_size != 0)
2611
180
      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.17k
    hdr->sh_info = 0;
2616
1.17k
    goto success;
2617
1.35k
  }
2618
2619
      /* PR 18854: A binary might contain more than one symbol table.
2620
   Unusual, but possible.  Warn, but continue.  */
2621
27.3k
      if (elf_onesymtab (abfd) != 0)
2622
3.41k
  {
2623
3.41k
    _bfd_error_handler
2624
      /* xgettext:c-format */
2625
3.41k
      (_("%pB: warning: multiple symbol tables detected"
2626
3.41k
         " - ignoring the table in section %u"),
2627
3.41k
       abfd, shindex);
2628
3.41k
    goto success;
2629
3.41k
  }
2630
23.9k
      elf_onesymtab (abfd) = shindex;
2631
23.9k
      elf_symtab_hdr (abfd) = *hdr;
2632
23.9k
      elf_elfsections (abfd)[shindex] = hdr = & elf_symtab_hdr (abfd);
2633
23.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
23.9k
      if ((hdr->sh_flags & SHF_ALLOC) != 0
2642
1.08k
    && (abfd->flags & DYNAMIC) != 0
2643
259
    && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2644
259
            shindex))
2645
6
  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
23.8k
      {
2651
23.8k
  elf_section_list * entry;
2652
23.8k
  unsigned int i, num_sec;
2653
2654
25.9k
  for (entry = elf_symtab_shndx_list (abfd); entry; entry = entry->next)
2655
4.55k
    if (entry->hdr.sh_link == shindex)
2656
2.46k
      goto success;
2657
2658
21.4k
  num_sec = elf_numsections (abfd);
2659
62.7k
  for (i = shindex + 1; i < num_sec; i++)
2660
41.4k
    {
2661
41.4k
      Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
2662
2663
41.4k
      if (hdr2->sh_type == SHT_SYMTAB_SHNDX
2664
573
    && hdr2->sh_link == shindex)
2665
76
        break;
2666
41.4k
    }
2667
2668
21.4k
  if (i == num_sec)
2669
429k
    for (i = 1; i < shindex; i++)
2670
408k
      {
2671
408k
        Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
2672
2673
408k
        if (hdr2->sh_type == SHT_SYMTAB_SHNDX
2674
3.31k
      && hdr2->sh_link == shindex)
2675
436
    break;
2676
408k
      }
2677
2678
21.4k
  if (i != shindex)
2679
512
    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.4k
  goto success;
2683
23.8k
      }
2684
2685
4.44k
    case SHT_DYNSYM:   /* A dynamic symbol table.  */
2686
4.44k
      if (elf_dynsymtab (abfd) == shindex)
2687
1.85k
  goto success;
2688
2689
2.59k
      if (hdr->sh_entsize != bed->s->sizeof_sym)
2690
374
  goto fail;
2691
2692
2.22k
      if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size)
2693
309
  {
2694
309
    if (hdr->sh_size != 0)
2695
143
      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
166
    hdr->sh_info = 0;
2701
166
    goto success;
2702
309
  }
2703
2704
      /* PR 18854: A binary might contain more than one dynamic symbol table.
2705
   Unusual, but possible.  Warn, but continue.  */
2706
1.91k
      if (elf_dynsymtab (abfd) != 0)
2707
166
  {
2708
166
    _bfd_error_handler
2709
      /* xgettext:c-format */
2710
166
      (_("%pB: warning: multiple dynamic symbol tables detected"
2711
166
         " - ignoring the table in section %u"),
2712
166
       abfd, shindex);
2713
166
    goto success;
2714
166
  }
2715
1.75k
      elf_dynsymtab (abfd) = shindex;
2716
1.75k
      elf_tdata (abfd)->dynsymtab_hdr = *hdr;
2717
1.75k
      elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr;
2718
1.75k
      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.75k
      ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2723
1.75k
      goto success;
2724
2725
9.70k
    case SHT_SYMTAB_SHNDX: /* Symbol section indices when >64k sections.  */
2726
9.70k
      {
2727
9.70k
  elf_section_list * entry;
2728
2729
12.5k
  for (entry = elf_symtab_shndx_list (abfd); entry; entry = entry->next)
2730
3.41k
    if (entry->ndx == shindex)
2731
584
      goto success;
2732
2733
9.12k
  entry = bfd_alloc (abfd, sizeof (*entry));
2734
9.12k
  if (entry == NULL)
2735
0
    goto fail;
2736
9.12k
  entry->ndx = shindex;
2737
9.12k
  entry->hdr = * hdr;
2738
9.12k
  entry->next = elf_symtab_shndx_list (abfd);
2739
9.12k
  elf_symtab_shndx_list (abfd) = entry;
2740
9.12k
  elf_elfsections (abfd)[shindex] = & entry->hdr;
2741
9.12k
  goto success;
2742
9.12k
      }
2743
2744
83.0k
    case SHT_STRTAB:   /* A string table.  */
2745
83.0k
      if (hdr->bfd_section != NULL)
2746
523
  goto success;
2747
2748
82.5k
      if (ehdr->e_shstrndx == shindex)
2749
67.0k
  {
2750
67.0k
    elf_tdata (abfd)->shstrtab_hdr = *hdr;
2751
67.0k
    elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr;
2752
67.0k
    goto success;
2753
67.0k
  }
2754
2755
15.5k
      if (elf_elfsections (abfd)[elf_onesymtab (abfd)]->sh_link == shindex)
2756
3.05k
  {
2757
7.58k
  symtab_strtab:
2758
7.58k
    elf_tdata (abfd)->strtab_hdr = *hdr;
2759
7.58k
    elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->strtab_hdr;
2760
7.58k
    goto success;
2761
3.05k
  }
2762
2763
12.4k
      if (elf_elfsections (abfd)[elf_dynsymtab (abfd)]->sh_link == shindex)
2764
630
  {
2765
1.21k
  dynsymtab_strtab:
2766
1.21k
    elf_tdata (abfd)->dynstrtab_hdr = *hdr;
2767
1.21k
    hdr = &elf_tdata (abfd)->dynstrtab_hdr;
2768
1.21k
    elf_elfsections (abfd)[shindex] = hdr;
2769
    /* We also treat this as a regular section, so that objcopy
2770
       can handle it.  */
2771
1.21k
    ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2772
1.21k
             shindex);
2773
1.21k
    goto success;
2774
630
  }
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
11.8k
      if (elf_onesymtab (abfd) == 0 || elf_dynsymtab (abfd) == 0)
2780
11.6k
  {
2781
11.6k
    unsigned int i, num_sec;
2782
2783
11.6k
    num_sec = elf_numsections (abfd);
2784
138k
    for (i = 1; i < num_sec; i++)
2785
132k
      {
2786
132k
        Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
2787
132k
        if (hdr2->sh_link == shindex)
2788
10.8k
    {
2789
      /* Prevent endless recursion on broken objects.  */
2790
10.8k
      if (i == shindex)
2791
96
        goto fail;
2792
10.7k
      if (! bfd_section_from_shdr (abfd, i))
2793
334
        goto fail;
2794
10.4k
      if (elf_onesymtab (abfd) == i)
2795
4.53k
        goto symtab_strtab;
2796
5.92k
      if (elf_dynsymtab (abfd) == i)
2797
588
        goto dynsymtab_strtab;
2798
5.92k
    }
2799
132k
      }
2800
11.6k
  }
2801
6.26k
      ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2802
6.26k
      goto success;
2803
2804
23.0k
    case SHT_REL:
2805
121k
    case SHT_RELA:
2806
122k
    case SHT_RELR:
2807
      /* *These* do a lot of work -- but build no sections!  */
2808
122k
      {
2809
122k
  asection *target_sect;
2810
122k
  Elf_Internal_Shdr *hdr2, **p_hdr;
2811
122k
  unsigned int num_sec = elf_numsections (abfd);
2812
122k
  struct bfd_elf_section_data *esdt;
2813
122k
  bfd_size_type size;
2814
2815
122k
  if (hdr->sh_type == SHT_REL)
2816
23.0k
    size = bed->s->sizeof_rel;
2817
99.0k
  else if (hdr->sh_type == SHT_RELA)
2818
98.6k
    size = bed->s->sizeof_rela;
2819
430
  else
2820
430
    size = bed->s->arch_size / 8;
2821
122k
  if (hdr->sh_entsize != size)
2822
945
    goto fail;
2823
2824
  /* Check for a bogus link to avoid crashing.  */
2825
121k
  if (hdr->sh_link >= num_sec)
2826
113
    {
2827
113
      _bfd_error_handler
2828
        /* xgettext:c-format */
2829
113
        (_("%pB: invalid link %u for reloc section %s (index %u)"),
2830
113
         abfd, hdr->sh_link, name, shindex);
2831
113
      ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2832
113
      goto success;
2833
113
    }
2834
2835
  /* Get the symbol table.  */
2836
121k
  if ((elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB
2837
28.0k
       || elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_DYNSYM)
2838
94.4k
      && ! bfd_section_from_shdr (abfd, hdr->sh_link))
2839
395
    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
120k
  if (((abfd->flags & (DYNAMIC | EXEC_P)) != 0
2850
7.57k
       && (hdr->sh_flags & SHF_ALLOC) != 0)
2851
119k
      || (hdr->sh_flags & SHF_COMPRESSED) != 0
2852
116k
      || hdr->sh_type == SHT_RELR
2853
115k
      || hdr->sh_link == SHN_UNDEF
2854
112k
      || hdr->sh_link != elf_onesymtab (abfd)
2855
88.7k
      || hdr->sh_info == SHN_UNDEF
2856
87.9k
      || hdr->sh_info >= num_sec
2857
87.9k
      || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_REL
2858
87.6k
      || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_RELA)
2859
33.9k
    {
2860
33.9k
      ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2861
33.9k
      goto success;
2862
33.9k
    }
2863
2864
86.7k
  if (! bfd_section_from_shdr (abfd, hdr->sh_info))
2865
144
    goto fail;
2866
2867
86.6k
  target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info);
2868
86.6k
  if (target_sect == NULL)
2869
172
    goto fail;
2870
2871
86.4k
  esdt = elf_section_data (target_sect);
2872
86.4k
  if (hdr->sh_type == SHT_RELA)
2873
71.3k
    p_hdr = &esdt->rela.hdr;
2874
15.0k
  else
2875
15.0k
    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
86.4k
  if (*p_hdr != NULL)
2881
1.96k
    {
2882
1.96k
      if (!bed->init_secondary_reloc_section (abfd, hdr, name, shindex))
2883
255
        {
2884
255
    _bfd_error_handler
2885
      /* xgettext:c-format */
2886
255
      (_("%pB: warning: secondary relocation section '%s' "
2887
255
         "for section %pA found - ignoring"),
2888
255
       abfd, name, target_sect);
2889
255
        }
2890
1.71k
      else
2891
1.71k
        esdt->has_secondary_relocs = true;
2892
1.96k
      goto success;
2893
1.96k
    }
2894
2895
84.4k
  hdr2 = bfd_alloc (abfd, sizeof (*hdr2));
2896
84.4k
  if (hdr2 == NULL)
2897
0
    goto fail;
2898
84.4k
  *hdr2 = *hdr;
2899
84.4k
  *p_hdr = hdr2;
2900
84.4k
  elf_elfsections (abfd)[shindex] = hdr2;
2901
84.4k
  target_sect->reloc_count += (NUM_SHDR_ENTRIES (hdr)
2902
84.4k
             * bed->s->int_rels_per_ext_rel);
2903
84.4k
  target_sect->flags |= SEC_RELOC;
2904
84.4k
  target_sect->relocation = NULL;
2905
84.4k
  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
84.4k
  if (hdr->sh_size != 0)
2909
83.5k
    {
2910
83.5k
      if (hdr->sh_type == SHT_RELA)
2911
68.9k
        target_sect->use_rela_p = 1;
2912
83.5k
    }
2913
84.4k
  abfd->flags |= HAS_RELOC;
2914
84.4k
  goto success;
2915
84.4k
      }
2916
2917
1.30k
    case SHT_GNU_verdef:
2918
1.30k
      if (hdr->sh_info != 0)
2919
1.04k
  elf_dynverdef (abfd) = shindex;
2920
1.30k
      elf_tdata (abfd)->dynverdef_hdr = *hdr;
2921
1.30k
      ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2922
1.30k
      goto success;
2923
2924
1.50k
    case SHT_GNU_versym:
2925
1.50k
      if (hdr->sh_entsize != sizeof (Elf_External_Versym))
2926
143
  goto fail;
2927
2928
1.36k
      elf_dynversym (abfd) = shindex;
2929
1.36k
      elf_tdata (abfd)->dynversym_hdr = *hdr;
2930
1.36k
      ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2931
1.36k
      goto success;
2932
2933
1.37k
    case SHT_GNU_verneed:
2934
1.37k
      if (hdr->sh_info != 0)
2935
1.25k
  elf_dynverref (abfd) = shindex;
2936
1.37k
      elf_tdata (abfd)->dynverref_hdr = *hdr;
2937
1.37k
      ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2938
1.37k
      goto success;
2939
2940
1.13k
    case SHT_SHLIB:
2941
1.13k
      goto success;
2942
2943
22.1k
    case SHT_GROUP:
2944
22.1k
      if (!_bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
2945
32
  goto fail;
2946
2947
22.1k
      goto success;
2948
2949
205k
    default:
2950
      /* Possibly an attributes section.  */
2951
205k
      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
200k
    && (hdr->sh_type == SHT_GNU_ATTRIBUTES
2954
193k
        || hdr->sh_type == bed->obj_attrs_section_type))
2955
7.05k
  {
2956
7.05k
    if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
2957
40
      goto fail;
2958
7.01k
    _bfd_elf_parse_attributes (abfd, hdr);
2959
7.01k
    goto success;
2960
7.05k
  }
2961
2962
      /* Check for any processor-specific section types.  */
2963
198k
      if (bed->elf_backend_section_from_shdr (abfd, hdr, name, shindex))
2964
180k
  goto success;
2965
2966
17.6k
      if (hdr->sh_type >= SHT_LOUSER && hdr->sh_type <= SHT_HIUSER)
2967
7.92k
  {
2968
7.92k
    if ((hdr->sh_flags & SHF_ALLOC) != 0)
2969
      /* FIXME: How to properly handle allocated section reserved
2970
         for applications?  */
2971
492
      _bfd_error_handler
2972
        /* xgettext:c-format */
2973
492
        (_("%pB: unknown type [%#x] section `%s'"),
2974
492
         abfd, hdr->sh_type, name);
2975
7.43k
    else
2976
7.43k
      {
2977
        /* Allow sections reserved for applications.  */
2978
7.43k
        ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2979
7.43k
        goto success;
2980
7.43k
      }
2981
7.92k
  }
2982
9.77k
      else if (hdr->sh_type >= SHT_LOPROC
2983
1.26k
         && hdr->sh_type <= SHT_HIPROC)
2984
  /* FIXME: We should handle this section.  */
2985
1.26k
  _bfd_error_handler
2986
    /* xgettext:c-format */
2987
1.26k
    (_("%pB: unknown type [%#x] section `%s'"),
2988
1.26k
     abfd, hdr->sh_type, name);
2989
8.51k
      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
263
      _bfd_error_handler
2997
        /* xgettext:c-format */
2998
263
        (_("%pB: unknown type [%#x] section `%s'"),
2999
263
         abfd, hdr->sh_type, name);
3000
4.66k
    else
3001
4.66k
      {
3002
        /* Otherwise it should be processed.  */
3003
4.66k
        ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
3004
4.66k
        goto success;
3005
4.66k
      }
3006
4.92k
  }
3007
3.58k
      else
3008
  /* FIXME: We should handle this section.  */
3009
3.58k
  _bfd_error_handler
3010
    /* xgettext:c-format */
3011
3.58k
    (_("%pB: unknown type [%#x] section `%s'"),
3012
3.58k
     abfd, hdr->sh_type, name);
3013
3014
5.59k
      goto fail;
3015
1.01M
    }
3016
3017
17.9k
 fail:
3018
17.9k
  ret = false;
3019
1.02M
 success:
3020
1.02M
  elf_tdata (abfd)->being_created[shindex] = false;
3021
1.02M
  return ret;
3022
17.9k
}
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
180k
{
3061
180k
  if (sec_index >= elf_numsections (abfd))
3062
30.1k
    return NULL;
3063
149k
  return elf_elfsections (abfd)[sec_index]->bfd_section;
3064
180k
}
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
983k
{
3236
983k
  int i;
3237
983k
  int len;
3238
3239
983k
  len = strlen (name);
3240
3241
5.62M
  for (i = 0; spec[i].prefix != NULL; i++)
3242
4.79M
    {
3243
4.79M
      int suffix_len;
3244
4.79M
      int prefix_len = spec[i].prefix_length;
3245
3246
4.79M
      if (len < prefix_len)
3247
2.65M
  continue;
3248
2.13M
      if (memcmp (name, spec[i].prefix, prefix_len) != 0)
3249
1.91M
  continue;
3250
3251
225k
      suffix_len = spec[i].suffix_length;
3252
225k
      if (suffix_len <= 0)
3253
223k
  {
3254
223k
    if (name[prefix_len] != 0)
3255
131k
      {
3256
131k
        if (suffix_len == 0)
3257
64.8k
    continue;
3258
67.0k
        if (name[prefix_len] != '.'
3259
11.7k
      && (suffix_len == -2
3260
5.96k
          || (rela && spec[i].type == SHT_REL)))
3261
7.95k
    continue;
3262
67.0k
      }
3263
223k
  }
3264
2.22k
      else
3265
2.22k
  {
3266
2.22k
    if (len < prefix_len + suffix_len)
3267
1.13k
      continue;
3268
1.08k
    if (memcmp (name + len - suffix_len,
3269
1.08k
          spec[i].prefix + prefix_len,
3270
1.08k
          suffix_len) != 0)
3271
802
      continue;
3272
1.08k
  }
3273
150k
      return &spec[i];
3274
225k
    }
3275
3276
832k
  return NULL;
3277
983k
}
3278
3279
const struct bfd_elf_special_section *
3280
_bfd_elf_get_sec_type_attr (bfd *abfd, asection *sec)
3281
1.63M
{
3282
1.63M
  int i;
3283
1.63M
  const struct bfd_elf_special_section *spec;
3284
1.63M
  elf_backend_data *bed;
3285
3286
  /* See if this is one of the special sections.  */
3287
1.63M
  if (sec->name == NULL)
3288
0
    return NULL;
3289
3290
1.63M
  bed = get_elf_backend_data (abfd);
3291
1.63M
  spec = bed->special_sections;
3292
1.63M
  if (spec)
3293
735k
    {
3294
735k
      spec = _bfd_elf_get_special_section (sec->name,
3295
735k
             bed->special_sections,
3296
735k
             sec->use_rela_p);
3297
735k
      if (spec != NULL)
3298
1.89k
  return spec;
3299
735k
    }
3300
3301
1.63M
  if (sec->name[0] != '.')
3302
1.39M
    return NULL;
3303
3304
243k
  i = sec->name[1] - 'b';
3305
243k
  if (i < 0 || i > 'z' - 'b')
3306
12.1k
    return NULL;
3307
3308
230k
  spec = special_sections[i];
3309
3310
230k
  if (spec == NULL)
3311
12.8k
    return NULL;
3312
3313
218k
  return _bfd_elf_get_special_section (sec->name, spec, sec->use_rela_p);
3314
230k
}
3315
3316
bool
3317
_bfd_elf_new_section_hook (bfd *abfd, asection *sec)
3318
1.60M
{
3319
1.60M
  struct bfd_elf_section_data *sdata;
3320
1.60M
  elf_backend_data *bed;
3321
1.60M
  const struct bfd_elf_special_section *ssect;
3322
3323
1.60M
  sdata = sec->used_by_bfd;
3324
1.60M
  if (sdata == NULL)
3325
1.14M
    {
3326
1.14M
      sdata = bfd_zalloc (abfd, sizeof (*sdata));
3327
1.14M
      if (sdata == NULL)
3328
0
  return false;
3329
1.14M
      sec->used_by_bfd = sdata;
3330
1.14M
    }
3331
3332
  /* Indicate whether or not this section should use RELA relocations.  */
3333
1.60M
  bed = get_elf_backend_data (abfd);
3334
1.60M
  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.60M
  ssect = (*bed->get_sec_type_attr) (abfd, sec);
3339
1.60M
  if (ssect != NULL)
3340
150k
    {
3341
150k
      elf_section_type (sec) = ssect->type;
3342
150k
      elf_section_flags (sec) = ssect->attr;
3343
150k
    }
3344
3345
1.60M
  return _bfd_generic_new_section_hook (abfd, sec);
3346
1.60M
}
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
860k
{
3374
860k
  asection *newsect;
3375
860k
  char *name;
3376
860k
  char namebuf[64];
3377
860k
  size_t len;
3378
860k
  int split;
3379
860k
  unsigned int opb = bfd_octets_per_byte (abfd, NULL);
3380
3381
860k
  split = ((hdr->p_memsz > 0)
3382
642k
      && (hdr->p_filesz > 0)
3383
571k
      && (hdr->p_memsz > hdr->p_filesz));
3384
3385
860k
  if (hdr->p_filesz > 0)
3386
729k
    {
3387
729k
      sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "a" : "");
3388
729k
      len = strlen (namebuf) + 1;
3389
729k
      name = bfd_alloc (abfd, len);
3390
729k
      if (!name)
3391
0
  return false;
3392
729k
      memcpy (name, namebuf, len);
3393
729k
      newsect = bfd_make_section (abfd, name);
3394
729k
      if (newsect == NULL)
3395
0
  return false;
3396
729k
      newsect->vma = hdr->p_vaddr / opb;
3397
729k
      newsect->lma = hdr->p_paddr / opb;
3398
729k
      newsect->size = hdr->p_filesz;
3399
729k
      newsect->filepos = hdr->p_offset;
3400
729k
      newsect->flags |= SEC_HAS_CONTENTS;
3401
729k
      newsect->alignment_power = bfd_log2 (hdr->p_align);
3402
729k
      if (hdr->p_type == PT_LOAD)
3403
9.91k
  {
3404
9.91k
    newsect->flags |= SEC_ALLOC;
3405
9.91k
    newsect->flags |= SEC_LOAD;
3406
9.91k
    if (hdr->p_flags & PF_X)
3407
4.11k
      {
3408
        /* FIXME: all we known is that it has execute PERMISSION,
3409
     may be data.  */
3410
4.11k
        newsect->flags |= SEC_CODE;
3411
4.11k
      }
3412
9.91k
  }
3413
729k
      if (!(hdr->p_flags & PF_W))
3414
535k
  {
3415
535k
    newsect->flags |= SEC_READONLY;
3416
535k
  }
3417
729k
    }
3418
3419
860k
  if (hdr->p_memsz > hdr->p_filesz)
3420
323k
    {
3421
323k
      bfd_vma align;
3422
3423
323k
      sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "b" : "");
3424
323k
      len = strlen (namebuf) + 1;
3425
323k
      name = bfd_alloc (abfd, len);
3426
323k
      if (!name)
3427
0
  return false;
3428
323k
      memcpy (name, namebuf, len);
3429
323k
      newsect = bfd_make_section (abfd, name);
3430
323k
      if (newsect == NULL)
3431
0
  return false;
3432
323k
      newsect->vma = (hdr->p_vaddr + hdr->p_filesz) / opb;
3433
323k
      newsect->lma = (hdr->p_paddr + hdr->p_filesz) / opb;
3434
323k
      newsect->size = hdr->p_memsz - hdr->p_filesz;
3435
323k
      newsect->filepos = hdr->p_offset + hdr->p_filesz;
3436
323k
      align = newsect->vma & -newsect->vma;
3437
323k
      if (align == 0 || align > hdr->p_align)
3438
76.2k
  align = hdr->p_align;
3439
323k
      newsect->alignment_power = bfd_log2 (align);
3440
323k
      if (hdr->p_type == PT_LOAD)
3441
9.10k
  {
3442
9.10k
    newsect->flags |= SEC_ALLOC;
3443
9.10k
    if (hdr->p_flags & PF_X)
3444
3.09k
      newsect->flags |= SEC_CODE;
3445
9.10k
  }
3446
323k
      if (!(hdr->p_flags & PF_W))
3447
242k
  newsect->flags |= SEC_READONLY;
3448
323k
    }
3449
3450
860k
  return true;
3451
860k
}
3452
3453
static bool
3454
_bfd_elf_core_find_build_id (bfd *templ, bfd_vma offset)
3455
13.1k
{
3456
  /* The return value is ignored.  Build-ids are considered optional.  */
3457
13.1k
  if (templ->xvec->flavour == bfd_target_elf_flavour)
3458
13.1k
    return (*get_elf_backend_data (templ)->elf_backend_core_find_build_id)
3459
13.1k
      (templ, offset);
3460
0
  return false;
3461
13.1k
}
3462
3463
bool
3464
bfd_section_from_phdr (bfd *abfd, Elf_Internal_Phdr *hdr, int hdr_index)
3465
860k
{
3466
860k
  elf_backend_data *bed;
3467
3468
860k
  switch (hdr->p_type)
3469
860k
    {
3470
169k
    case PT_NULL:
3471
169k
      return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "null");
3472
3473
13.2k
    case PT_LOAD:
3474
13.2k
      if (! _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "load"))
3475
0
  return false;
3476
13.2k
      if (bfd_get_format (abfd) == bfd_core && abfd->build_id == NULL)
3477
13.1k
  _bfd_elf_core_find_build_id (abfd, hdr->p_offset);
3478
13.2k
      return true;
3479
3480
3.06k
    case PT_DYNAMIC:
3481
3.06k
      return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "dynamic");
3482
3483
2.58k
    case PT_INTERP:
3484
2.58k
      return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "interp");
3485
3486
2.31k
    case PT_NOTE:
3487
2.31k
      if (! _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "note"))
3488
0
  return false;
3489
2.31k
      if (! _bfd_elf_read_notes (abfd, hdr->p_offset, hdr->p_filesz,
3490
2.31k
         hdr->p_align))
3491
1.37k
  return false;
3492
939
      return true;
3493
3494
1.01k
    case PT_SHLIB:
3495
1.01k
      return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "shlib");
3496
3497
14.1k
    case PT_PHDR:
3498
14.1k
      return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "phdr");
3499
3500
80
    case PT_GNU_EH_FRAME:
3501
80
      return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index,
3502
80
                "eh_frame_hdr");
3503
3504
70
    case PT_GNU_STACK:
3505
70
      return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "stack");
3506
3507
212
    case PT_GNU_RELRO:
3508
212
      return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "relro");
3509
3510
16
    case PT_GNU_SFRAME:
3511
16
      return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index,
3512
16
                "sframe");
3513
3514
654k
    default:
3515
      /* Check for any processor-specific program segment types.  */
3516
654k
      bed = get_elf_backend_data (abfd);
3517
654k
      return bed->elf_backend_section_from_phdr (abfd, hdr, hdr_index, "proc");
3518
860k
    }
3519
860k
}
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
10
{
3527
10
  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
10
  else
3533
10
    return elf_section_data (sec)->rela.hdr;
3534
10
}
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
102
{
3542
102
  char *name = bfd_alloc (abfd, sizeof ".rela" + strlen (sec_name));
3543
102
  if (name == NULL)
3544
0
    return false;
3545
3546
102
  sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", sec_name);
3547
102
  rel_hdr->sh_name =
3548
102
    (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), name,
3549
102
          false);
3550
102
  if (rel_hdr->sh_name == (unsigned int) -1)
3551
0
    return false;
3552
3553
102
  return true;
3554
102
}
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
102
{
3568
102
  Elf_Internal_Shdr *rel_hdr;
3569
102
  elf_backend_data *bed = get_elf_backend_data (abfd);
3570
3571
102
  BFD_ASSERT (reldata->hdr == NULL);
3572
102
  rel_hdr = bfd_zalloc (abfd, sizeof (*rel_hdr));
3573
102
  if (rel_hdr == NULL)
3574
0
    return false;
3575
102
  reldata->hdr = rel_hdr;
3576
3577
102
  if (delay_sh_name_p)
3578
0
    rel_hdr->sh_name = (unsigned int) -1;
3579
102
  else if (!_bfd_elf_set_reloc_sh_name (abfd, rel_hdr, sec_name,
3580
102
          use_rela_p))
3581
0
    return false;
3582
102
  rel_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL;
3583
102
  rel_hdr->sh_entsize = (use_rela_p
3584
102
       ? bed->s->sizeof_rela
3585
102
       : bed->s->sizeof_rel);
3586
102
  rel_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
3587
102
  rel_hdr->sh_flags = 0;
3588
102
  rel_hdr->sh_addr = 0;
3589
102
  rel_hdr->sh_size = 0;
3590
102
  rel_hdr->sh_offset = 0;
3591
3592
102
  return true;
3593
102
}
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
2.76k
{
3600
2.76k
  if ((flags & (SEC_ALLOC | SEC_IS_COMMON)) != 0
3601
1.02k
      && (flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
3602
79
    return SHT_NOBITS;
3603
2.68k
  return SHT_PROGBITS;
3604
2.76k
}
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.88k
{
3617
1.88k
  struct fake_section_arg *arg = (struct fake_section_arg *)fsarg;
3618
1.88k
  elf_backend_data *bed = get_elf_backend_data (abfd);
3619
1.88k
  struct bfd_elf_section_data *esd = elf_section_data (asect);
3620
1.88k
  Elf_Internal_Shdr *this_hdr;
3621
1.88k
  unsigned int sh_type;
3622
1.88k
  const char *name = asect->name;
3623
1.88k
  bool delay_sh_name_p = false;
3624
1.88k
  bfd_vma mask;
3625
3626
1.88k
  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.88k
  this_hdr = &esd->this_hdr;
3634
3635
  /* ld: compress DWARF debug sections with names: .debug_*.  */
3636
1.88k
  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.88k
  if (delay_sh_name_p)
3651
0
    this_hdr->sh_name = (unsigned int) -1;
3652
1.88k
  else
3653
1.88k
    {
3654
1.88k
      this_hdr->sh_name
3655
1.88k
  = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
3656
1.88k
                name, false);
3657
1.88k
      if (this_hdr->sh_name == (unsigned int) -1)
3658
0
  {
3659
0
    arg->failed = true;
3660
0
    return;
3661
0
  }
3662
1.88k
    }
3663
3664
  /* Don't clear sh_flags. Assembler may set additional bits.  */
3665
3666
1.88k
  if ((asect->flags & SEC_ALLOC) != 0
3667
876
      || asect->user_set_vma)
3668
1.88k
    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.88k
  this_hdr->sh_offset = 0;
3673
1.88k
  this_hdr->sh_size = asect->size;
3674
1.88k
  this_hdr->sh_link = 0;
3675
  /* PR 17512: file: 0eb809fe, 8b0535ee.  */
3676
1.88k
  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.88k
  mask = ((bfd_vma) 1 << asect->alignment_power) | this_hdr->sh_addr;
3688
1.88k
  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.88k
  this_hdr->bfd_section = asect;
3693
1.88k
  this_hdr->contents = NULL;
3694
3695
  /* If the section type is unspecified, we set it based on
3696
     asect->flags.  */
3697
1.88k
  if (asect->type != 0)
3698
0
    sh_type = asect->type;
3699
1.88k
  else if ((asect->flags & SEC_GROUP) != 0)
3700
4
    sh_type = SHT_GROUP;
3701
1.87k
  else
3702
1.87k
    sh_type = bfd_elf_get_default_section_type (asect->flags);
3703
3704
1.88k
  if (this_hdr->sh_type == SHT_NULL)
3705
354
    this_hdr->sh_type = sh_type;
3706
1.52k
  else if (this_hdr->sh_type == SHT_NOBITS
3707
31
     && sh_type == SHT_PROGBITS
3708
0
     && (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.88k
  switch (this_hdr->sh_type)
3720
1.88k
    {
3721
210
    default:
3722
210
      break;
3723
3724
210
    case SHT_STRTAB:
3725
162
    case SHT_NOTE:
3726
241
    case SHT_NOBITS:
3727
1.32k
    case SHT_PROGBITS:
3728
1.32k
      break;
3729
3730
31
    case SHT_INIT_ARRAY:
3731
57
    case SHT_FINI_ARRAY:
3732
57
    case SHT_PREINIT_ARRAY:
3733
57
      this_hdr->sh_entsize = bed->s->arch_size / 8;
3734
57
      break;
3735
3736
15
    case SHT_HASH:
3737
15
      this_hdr->sh_entsize = bed->s->sizeof_hash_entry;
3738
15
      break;
3739
3740
27
    case SHT_DYNSYM:
3741
27
      this_hdr->sh_entsize = bed->s->sizeof_sym;
3742
27
      break;
3743
3744
40
    case SHT_DYNAMIC:
3745
40
      this_hdr->sh_entsize = bed->s->sizeof_dyn;
3746
40
      break;
3747
3748
107
    case SHT_RELA:
3749
107
      if (get_elf_backend_data (abfd)->may_use_rela_p)
3750
104
  this_hdr->sh_entsize = bed->s->sizeof_rela;
3751
107
      break;
3752
3753
6
     case SHT_REL:
3754
6
      if (get_elf_backend_data (abfd)->may_use_rel_p)
3755
6
  this_hdr->sh_entsize = bed->s->sizeof_rel;
3756
6
      break;
3757
3758
18
     case SHT_GNU_versym:
3759
18
      this_hdr->sh_entsize = sizeof (Elf_External_Versym);
3760
18
      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
1
  this_hdr->sh_info = elf_tdata (abfd)->cverdefs;
3769
26
      else
3770
26
  BFD_ASSERT (elf_tdata (abfd)->cverdefs == 0
3771
27
        || this_hdr->sh_info == elf_tdata (abfd)->cverdefs);
3772
27
      break;
3773
3774
30
    case SHT_GNU_verneed:
3775
30
      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
30
      if (this_hdr->sh_info == 0)
3780
2
  this_hdr->sh_info = elf_tdata (abfd)->cverrefs;
3781
28
      else
3782
28
  BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0
3783
30
        || this_hdr->sh_info == elf_tdata (abfd)->cverrefs);
3784
30
      break;
3785
3786
4
    case SHT_GROUP:
3787
4
      this_hdr->sh_entsize = GRP_ENTRY_SIZE;
3788
4
      break;
3789
3790
15
    case SHT_GNU_HASH:
3791
15
      this_hdr->sh_entsize = bed->s->arch_size == 64 ? 0 : 4;
3792
15
      break;
3793
1.88k
    }
3794
3795
1.88k
  if ((asect->flags & SEC_ALLOC) != 0)
3796
1.00k
    this_hdr->sh_flags |= SHF_ALLOC;
3797
1.88k
  if ((asect->flags & SEC_READONLY) == 0)
3798
420
    this_hdr->sh_flags |= SHF_WRITE;
3799
1.88k
  if ((asect->flags & SEC_CODE) != 0)
3800
305
    this_hdr->sh_flags |= SHF_EXECINSTR;
3801
1.88k
  if ((asect->flags & SEC_MERGE) != 0)
3802
119
    {
3803
119
      this_hdr->sh_flags |= SHF_MERGE;
3804
119
      this_hdr->sh_entsize = asect->entsize;
3805
119
    }
3806
1.88k
  if ((asect->flags & SEC_STRINGS) != 0)
3807
118
    {
3808
118
      this_hdr->sh_flags |= SHF_STRINGS;
3809
118
      this_hdr->sh_entsize = asect->entsize;
3810
118
    }
3811
1.88k
  if ((asect->flags & SEC_GROUP) == 0 && elf_group_name (asect) != NULL)
3812
10
    this_hdr->sh_flags |= SHF_GROUP;
3813
1.88k
  if ((asect->flags & SEC_THREAD_LOCAL) != 0)
3814
90
    {
3815
90
      this_hdr->sh_flags |= SHF_TLS;
3816
90
      if (asect->size == 0
3817
11
    && (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
90
    }
3830
1.88k
  if ((asect->flags & (SEC_GROUP | SEC_EXCLUDE)) == SEC_EXCLUDE)
3831
91
    this_hdr->sh_flags |= SHF_EXCLUDE;
3832
3833
1.88k
  if (this_hdr->sh_entsize == 0)
3834
1.39k
    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.88k
  if ((asect->flags & SEC_RELOC) != 0)
3841
102
    {
3842
      /* When doing a relocatable link, create both REL and RELA sections if
3843
   needed.  */
3844
102
      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
102
      else if (!_bfd_elf_init_reloc_shdr (abfd,
3866
102
            (asect->use_rela_p
3867
102
             ? &esd->rela : &esd->rel),
3868
102
            name,
3869
102
            asect->use_rela_p,
3870
102
            delay_sh_name_p))
3871
0
  {
3872
0
    arg->failed = true;
3873
0
    return;
3874
0
  }
3875
102
    }
3876
3877
  /* Check for processor-specific section types.  */
3878
1.88k
  sh_type = this_hdr->sh_type;
3879
1.88k
  if (bed->elf_backend_fake_sections
3880
1.48k
      && !(*bed->elf_backend_fake_sections) (abfd, this_hdr, asect))
3881
0
    {
3882
0
      arg->failed = true;
3883
0
      return;
3884
0
    }
3885
3886
1.88k
  if (sh_type == SHT_NOBITS && asect->size != 0)
3887
70
    {
3888
      /* Don't change the header type from NOBITS if we are being
3889
   called for objcopy --only-keep-debug.  */
3890
70
      this_hdr->sh_type = sh_type;
3891
70
    }
3892
1.88k
}
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.88k
{
3902
1.88k
  bool *failedptr = (bool *) failedptrarg;
3903
1.88k
  asection *elt, *first;
3904
1.88k
  unsigned char *loc;
3905
1.88k
  bool gas;
3906
3907
  /* Ignore linker created group section.  See elfNN_ia64_object_p in
3908
     elfxx-ia64.c.  */
3909
1.88k
  if ((sec->flags & (SEC_GROUP | SEC_LINKER_CREATED)) != SEC_GROUP
3910
4
      || sec->size == 0
3911
4
      || *failedptr)
3912
1.87k
    return;
3913
3914
3
  if (elf_section_data (sec)->this_hdr.sh_info == 0)
3915
3
    {
3916
3
      unsigned long symindx = 0;
3917
3918
      /* elf_group_id will have been set up by objcopy and the
3919
   generic linker.  */
3920
3
      if (elf_group_id (sec) != NULL)
3921
3
  symindx = elf_group_id (sec)->udata.i;
3922
3923
3
      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
3
      elf_section_data (sec)->this_hdr.sh_info = symindx;
3937
3
    }
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
3
  gas = true;
3973
3
  if (sec->contents == NULL)
3974
3
    {
3975
3
      gas = false;
3976
3
      sec->contents = bfd_alloc (abfd, sec->size);
3977
3978
      /* Arrange for the section to be written out.  */
3979
3
      elf_section_data (sec)->this_hdr.contents = sec->contents;
3980
3
      if (sec->contents == NULL)
3981
0
  {
3982
0
    *failedptr = true;
3983
0
    return;
3984
0
  }
3985
3
      sec->alloced = 1;
3986
3
    }
3987
3988
3
  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
3
  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
7
  while (elt != NULL)
4000
7
    {
4001
7
      asection *s;
4002
4003
7
      s = elt;
4004
7
      if (!gas)
4005
7
  s = s->output_section;
4006
7
      if (s != NULL
4007
7
    && !bfd_is_abs_section (s))
4008
7
  {
4009
7
    struct bfd_elf_section_data *elf_sec = elf_section_data (s);
4010
7
    struct bfd_elf_section_data *input_elf_sec = elf_section_data (elt);
4011
4012
7
    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
7
    if (elf_sec->rela.hdr != NULL
4024
5
        && (gas
4025
5
      || (input_elf_sec->rela.hdr != NULL
4026
5
          && input_elf_sec->rela.hdr->sh_flags & SHF_GROUP) != 0))
4027
5
      {
4028
5
        elf_sec->rela.hdr->sh_flags |= SHF_GROUP;
4029
5
        loc -= 4;
4030
5
        if (loc == sec->contents)
4031
0
    break;
4032
5
        H_PUT_32 (abfd, elf_sec->rela.idx, loc);
4033
5
      }
4034
7
    loc -= 4;
4035
7
    if (loc == sec->contents)
4036
0
      break;
4037
7
    H_PUT_32 (abfd, elf_sec->this_idx, loc);
4038
7
  }
4039
7
      elt = elf_next_in_group (elt);
4040
7
      if (elt == first)
4041
3
  break;
4042
7
    }
4043
4044
  /* We should always get here with loc == sec->contents + 4.  Return
4045
     an error for bogus SHT_GROUP sections.  */
4046
3
  loc -= 4;
4047
3
  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
2
  H_PUT_32 (abfd, sec->flags & SEC_LINK_ONCE ? GRP_COMDAT : 0, loc);
4058
2
}
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
46
{
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
46
  if (get_elf_backend_data (abfd)->want_got_plt
4070
16
      && streq (name, ".plt"))
4071
4
    {
4072
4
      asection *sec;
4073
4074
4
      name = ".got.plt";
4075
4
      sec = bfd_get_section_by_name (abfd, name);
4076
4
      if (sec != NULL)
4077
3
  return sec;
4078
1
      name = ".got";
4079
1
    }
4080
4081
43
  return bfd_get_section_by_name (abfd, name);
4082
46
}
4083
4084
/* Return the section to which RELOC_SEC applies.  */
4085
4086
static asection *
4087
elf_get_reloc_section (asection *reloc_sec)
4088
113
{
4089
113
  const char *name;
4090
113
  unsigned int type;
4091
113
  bfd *abfd;
4092
113
  elf_backend_data *bed;
4093
4094
113
  type = elf_section_data (reloc_sec)->this_hdr.sh_type;
4095
113
  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
113
  name = reloc_sec->name;
4100
113
  if (!startswith (name, ".rel"))
4101
62
    return NULL;
4102
51
  name += 4;
4103
51
  if (type == SHT_RELA && *name++ != 'a')
4104
0
    return NULL;
4105
4106
51
  abfd = reloc_sec->owner;
4107
51
  bed = get_elf_backend_data (abfd);
4108
51
  return bed->get_reloc_section (abfd, name);
4109
51
}
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
515
{
4119
515
  struct elf_obj_tdata *t = elf_tdata (abfd);
4120
515
  asection *sec;
4121
515
  unsigned int section_number;
4122
515
  Elf_Internal_Shdr **i_shdrp;
4123
515
  struct bfd_elf_section_data *d;
4124
515
  bool need_symtab;
4125
4126
515
  section_number = 1;
4127
4128
515
  _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd));
4129
4130
  /* SHT_GROUP sections are in relocatable files only.  */
4131
515
  if (link_info == NULL || !link_info->resolve_section_groups)
4132
515
    {
4133
515
      size_t reloc_count = 0;
4134
4135
      /* Put SHT_GROUP sections first.  */
4136
2.39k
      for (sec = abfd->sections; sec != NULL; sec = sec->next)
4137
1.88k
  {
4138
1.88k
    d = elf_section_data (sec);
4139
4140
1.88k
    if (d->this_hdr.sh_type == SHT_GROUP)
4141
4
      {
4142
4
        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
4
        else
4149
4
    d->this_idx = section_number++;
4150
4
      }
4151
4152
    /* Count relocations.  */
4153
1.88k
    reloc_count += sec->reloc_count;
4154
1.88k
  }
4155
4156
      /* Set/clear HAS_RELOC depending on whether there are relocations.  */
4157
515
      if (reloc_count == 0)
4158
481
  abfd->flags &= ~HAS_RELOC;
4159
34
      else
4160
34
  abfd->flags |= HAS_RELOC;
4161
515
    }
4162
4163
2.39k
  for (sec = abfd->sections; sec; sec = sec->next)
4164
1.88k
    {
4165
1.88k
      d = elf_section_data (sec);
4166
4167
1.88k
      if (d->this_hdr.sh_type != SHT_GROUP)
4168
1.87k
  d->this_idx = section_number++;
4169
1.88k
      if (d->this_hdr.sh_name != (unsigned int) -1)
4170
1.88k
  _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->this_hdr.sh_name);
4171
1.88k
      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.88k
      else
4178
1.88k
  d->rel.idx = 0;
4179
4180
1.88k
      if (d->rela.hdr)
4181
102
  {
4182
102
    d->rela.idx = section_number++;
4183
102
    if (d->rela.hdr->sh_name != (unsigned int) -1)
4184
102
      _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rela.hdr->sh_name);
4185
102
  }
4186
1.78k
      else
4187
1.78k
  d->rela.idx = 0;
4188
1.88k
    }
4189
4190
515
  need_symtab = (bfd_get_symcount (abfd) > 0
4191
439
     || (link_info == NULL
4192
439
         && ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC))
4193
439
       == HAS_RELOC)));
4194
515
  if (need_symtab)
4195
76
    {
4196
76
      elf_onesymtab (abfd) = section_number++;
4197
76
      _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->symtab_hdr.sh_name);
4198
76
      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
76
      elf_strtab_sec (abfd) = section_number++;
4215
76
      _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->strtab_hdr.sh_name);
4216
76
    }
4217
4218
515
  elf_shstrtab_sec (abfd) = section_number++;
4219
515
  _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->shstrtab_hdr.sh_name);
4220
515
  elf_elfheader (abfd)->e_shstrndx = elf_shstrtab_sec (abfd);
4221
4222
515
  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
515
  elf_numsections (abfd) = section_number;
4231
515
  elf_elfheader (abfd)->e_shnum = section_number;
4232
4233
  /* Set up the list of section header pointers, in agreement with the
4234
     indices.  */
4235
515
  i_shdrp = bfd_zalloc (abfd, section_number * sizeof (*i_shdrp));
4236
515
  if (i_shdrp == NULL)
4237
0
    return false;
4238
4239
515
  i_shdrp[0] = bfd_zalloc (abfd, sizeof (*i_shdrp[0]));
4240
515
  if (i_shdrp[0] == NULL)
4241
0
    {
4242
0
      bfd_release (abfd, i_shdrp);
4243
0
      return false;
4244
0
    }
4245
4246
515
  elf_elfsections (abfd) = i_shdrp;
4247
4248
515
  i_shdrp[elf_shstrtab_sec (abfd)] = &t->shstrtab_hdr;
4249
515
  if (need_symtab)
4250
76
    {
4251
76
      i_shdrp[elf_onesymtab (abfd)] = &t->symtab_hdr;
4252
76
      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
76
      i_shdrp[elf_strtab_sec (abfd)] = &t->strtab_hdr;
4260
76
      t->symtab_hdr.sh_link = elf_strtab_sec (abfd);
4261
76
    }
4262
4263
2.39k
  for (sec = abfd->sections; sec; sec = sec->next)
4264
1.88k
    {
4265
1.88k
      asection *s;
4266
4267
1.88k
      d = elf_section_data (sec);
4268
4269
1.88k
      i_shdrp[d->this_idx] = &d->this_hdr;
4270
1.88k
      if (d->rel.idx != 0)
4271
0
  i_shdrp[d->rel.idx] = d->rel.hdr;
4272
1.88k
      if (d->rela.idx != 0)
4273
102
  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.88k
      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.88k
      if (d->rela.idx != 0)
4287
102
  {
4288
102
    d->rela.hdr->sh_link = elf_onesymtab (abfd);
4289
102
    d->rela.hdr->sh_info = d->this_idx;
4290
102
    d->rela.hdr->sh_flags |= SHF_INFO_LINK;
4291
102
  }
4292
4293
      /* We need to set up sh_link for SHF_LINK_ORDER.  */
4294
1.88k
      if ((d->this_hdr.sh_flags & SHF_LINK_ORDER) != 0)
4295
48
  {
4296
48
    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
48
    if (s)
4302
5
      {
4303
        /* Check discarded linkonce section.  */
4304
5
        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
5
        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
5
        s = s->output_section;
4334
5
        d->this_hdr.sh_link
4335
5
    = _bfd_elf_section_from_bfd_section (abfd, s);
4336
5
      }
4337
48
  }
4338
4339
1.88k
      switch (d->this_hdr.sh_type)
4340
1.88k
  {
4341
6
  case SHT_REL:
4342
113
  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
113
    if (d->this_hdr.sh_link == 0)
4347
113
      {
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
113
        if ((sec->flags & SEC_ALLOC) != 0)
4352
26
    {
4353
26
      s = bfd_get_section_by_name (abfd, ".dynsym");
4354
26
      if (s != NULL)
4355
11
        d->this_hdr.sh_link = elf_section_data (s)->this_idx;
4356
26
    }
4357
87
        else
4358
87
    d->this_hdr.sh_link = elf_onesymtab (abfd);
4359
113
      }
4360
4361
113
    s = elf_get_reloc_section (sec);
4362
113
    if (s != NULL)
4363
36
      {
4364
36
        d->this_hdr.sh_info = elf_section_data (s)->this_idx;
4365
36
        d->this_hdr.sh_flags |= SHF_INFO_LINK;
4366
36
      }
4367
113
    break;
4368
4369
66
  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
66
    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
66
    break;
4397
4398
66
  case SHT_DYNAMIC:
4399
67
  case SHT_DYNSYM:
4400
97
  case SHT_GNU_verneed:
4401
124
  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
124
    s = bfd_get_section_by_name (abfd, ".dynstr");
4406
124
    if (s != NULL)
4407
61
      d->this_hdr.sh_link = elf_section_data (s)->this_idx;
4408
124
    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
15
  case SHT_HASH:
4421
30
  case SHT_GNU_HASH:
4422
48
  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
48
    s = bfd_get_section_by_name (abfd, ".dynsym");
4426
48
    if (s != NULL)
4427
18
      d->this_hdr.sh_link = elf_section_data (s)->this_idx;
4428
48
    break;
4429
4430
4
  case SHT_GROUP:
4431
4
    d->this_hdr.sh_link = elf_onesymtab (abfd);
4432
1.88k
  }
4433
1.88k
    }
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
515
  return true;
4440
515
}
4441
4442
static bool
4443
sym_is_global (bfd *abfd, asymbol *sym)
4444
2.77k
{
4445
  /* If the backend has a special mapping, use it.  */
4446
2.77k
  elf_backend_data *bed = get_elf_backend_data (abfd);
4447
2.77k
  if (bed->elf_backend_sym_is_global)
4448
10
    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.11k
    || bfd_is_und_section (bfd_asymbol_section (sym))
4452
1.30k
    || bfd_is_com_section (bfd_asymbol_section (sym)));
4453
2.77k
}
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.10k
{
4499
4.10k
  if (sym == NULL)
4500
0
    return false;
4501
4502
4.10k
  if (sym->section == NULL)
4503
0
    return true;
4504
4505
4.10k
  if ((sym->flags & BSF_SECTION_SYM) != 0)
4506
1.51k
    {
4507
1.51k
      if ((sym->flags & BSF_SECTION_SYM_USED) == 0)
4508
1.28k
  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
235
      if (sym->section->output_offset != 0)
4515
0
  return true;
4516
235
    }
4517
4518
2.82k
  return discarded_section (sym->section);
4519
4.10k
}
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
76
{
4527
76
  unsigned int symcount = bfd_get_symcount (abfd);
4528
76
  asymbol **syms = bfd_get_outsymbols (abfd);
4529
76
  asymbol **sect_syms;
4530
76
  unsigned int num_locals = 0;
4531
76
  unsigned int num_globals = 0;
4532
76
  unsigned int max_index = 0;
4533
76
  unsigned int idx;
4534
76
  asection *asect;
4535
76
  asymbol **new_syms;
4536
4537
#ifdef DEBUG
4538
  fprintf (stderr, "elf_map_symbols\n");
4539
  fflush (stderr);
4540
#endif
4541
4542
714
  for (asect = abfd->sections; asect; asect = asect->next)
4543
638
    {
4544
638
      if (max_index < asect->index)
4545
562
  max_index = asect->index;
4546
638
    }
4547
4548
76
  max_index++;
4549
76
  sect_syms = bfd_zalloc (abfd, max_index * sizeof (*sect_syms));
4550
76
  if (sect_syms == NULL)
4551
0
    return false;
4552
76
  elf_section_syms (abfd) = sect_syms;
4553
76
  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.45k
  for (idx = 0; idx < symcount; idx++)
4558
1.37k
    {
4559
1.37k
      asymbol *sym = syms[idx];
4560
4561
1.37k
      if ((sym->flags & BSF_SECTION_SYM) != 0
4562
84
    && sym->value == 0
4563
73
    && !ignore_sym (sym)
4564
45
    && !bfd_is_abs_section (sym->section))
4565
41
  {
4566
41
    asection *sec = sym->section;
4567
4568
41
    if (sec->owner != abfd)
4569
41
      {
4570
41
        sec = sec->output_section;
4571
41
        if (sec == NULL)
4572
0
    return false;
4573
41
      }
4574
4575
41
    sect_syms[sec->index] = syms[idx];
4576
41
  }
4577
1.37k
    }
4578
4579
  /* Classify all of the symbols.  */
4580
1.45k
  for (idx = 0; idx < symcount; idx++)
4581
1.37k
    {
4582
1.37k
      if (ignore_sym (syms[idx]))
4583
35
  continue;
4584
1.34k
      if (sym_is_global (abfd, syms[idx]))
4585
743
  num_globals++;
4586
599
      else
4587
599
  num_locals++;
4588
1.34k
    }
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
714
  for (asect = abfd->sections; asect; asect = asect->next)
4595
638
    {
4596
638
      asymbol *sym = asect->symbol;
4597
      /* Don't include ignored section symbols.  */
4598
638
      if (!ignore_sym (sym)
4599
46
    && sect_syms[asect->index] == NULL)
4600
46
  {
4601
46
    if (sym_is_global (abfd, asect->symbol))
4602
0
      num_globals++;
4603
46
    else
4604
46
      num_locals++;
4605
46
  }
4606
638
    }
4607
4608
  /* Now sort the symbols so the local symbols are first.  */
4609
76
  new_syms = bfd_alloc (abfd, (num_locals + num_globals) * sizeof (*new_syms));
4610
76
  if (new_syms == NULL)
4611
0
    return false;
4612
4613
76
  unsigned int num_globals2 = 0;
4614
76
  unsigned int num_locals2 = 0;
4615
1.45k
  for (idx = 0; idx < symcount; idx++)
4616
1.37k
    {
4617
1.37k
      asymbol *sym = syms[idx];
4618
1.37k
      unsigned int i;
4619
4620
1.37k
      if (ignore_sym (sym))
4621
35
  continue;
4622
4623
1.34k
      if (sym_is_global (abfd, sym))
4624
743
  i = num_locals + num_globals2++;
4625
599
      else
4626
599
  i = num_locals2++;
4627
1.34k
      new_syms[i] = sym;
4628
1.34k
      sym->udata.i = i + 1;
4629
1.34k
    }
4630
714
  for (asect = abfd->sections; asect; asect = asect->next)
4631
638
    {
4632
638
      asymbol *sym = asect->symbol;
4633
638
      if (!ignore_sym (sym)
4634
46
    && sect_syms[asect->index] == NULL)
4635
46
  {
4636
46
    unsigned int i;
4637
4638
46
    sect_syms[asect->index] = sym;
4639
46
    if (sym_is_global (abfd, sym))
4640
0
      i = num_locals + num_globals2++;
4641
46
    else
4642
46
      i = num_locals2++;
4643
46
    new_syms[i] = sym;
4644
46
    sym->udata.i = i + 1;
4645
46
  }
4646
638
    }
4647
4648
76
  bfd_set_symtab (abfd, new_syms, num_locals + num_globals);
4649
4650
76
  *pnum_locals = num_locals;
4651
76
  return true;
4652
76
}
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
899
    {
4665
899
      file_ptr salign = i_shdrp->sh_addralign & -i_shdrp->sh_addralign;
4666
4667
899
      if (align)
4668
203
  offset = BFD_ALIGN (offset, salign);
4669
696
      else if (log_file_align)
4670
696
  {
4671
    /* Heuristic: Cap alignment at log_file_align.  */
4672
696
    file_ptr falign = 1u << log_file_align;
4673
4674
696
    offset = BFD_ALIGN (offset, salign < falign ? salign : falign);
4675
696
  }
4676
899
    }
4677
2.59k
  i_shdrp->sh_offset = offset;
4678
2.59k
  if (i_shdrp->bfd_section != NULL)
4679
1.38k
    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
515
{
4693
515
  elf_backend_data *bed = get_elf_backend_data (abfd);
4694
515
  struct fake_section_arg fsargs;
4695
515
  bool failed;
4696
515
  struct elf_strtab_hash *strtab = NULL;
4697
515
  Elf_Internal_Shdr *shstrtab_hdr;
4698
515
  bool need_symtab;
4699
4700
515
  if (abfd->output_has_begun)
4701
0
    return true;
4702
4703
  /* Do any elf backend specific processing first.  */
4704
515
  if (bed->elf_backend_begin_write_processing)
4705
78
    (*bed->elf_backend_begin_write_processing) (abfd, link_info);
4706
4707
515
  if (!(*bed->elf_backend_init_file_header) (abfd, link_info))
4708
0
    return false;
4709
4710
515
  fsargs.failed = false;
4711
515
  fsargs.link_info = link_info;
4712
515
  bfd_map_over_sections (abfd, elf_fake_sections, &fsargs);
4713
515
  if (fsargs.failed)
4714
0
    return false;
4715
4716
515
  if (!assign_section_numbers (abfd, link_info))
4717
0
    return false;
4718
4719
  /* The backend linker builds symbol table information itself.  */
4720
515
  need_symtab = (link_info == NULL
4721
515
     && (bfd_get_symcount (abfd) > 0
4722
439
         || ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC))
4723
439
       == HAS_RELOC)));
4724
515
  if (need_symtab)
4725
76
    {
4726
      /* Non-zero if doing a relocatable link.  */
4727
76
      int relocatable_p = ! (abfd->flags & (EXEC_P | DYNAMIC));
4728
4729
76
      if (! swap_out_syms (abfd, &strtab, relocatable_p, link_info))
4730
0
  return false;
4731
76
    }
4732
4733
515
  failed = false;
4734
515
  if (link_info == NULL)
4735
515
    {
4736
515
      bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
4737
515
      if (failed)
4738
1
  goto err_free_strtab;
4739
515
    }
4740
4741
514
  shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr;
4742
  /* sh_name was set in init_file_header.  */
4743
514
  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
514
  shstrtab_hdr->sh_addralign = 1;
4749
4750
514
  if (!assign_file_positions_except_relocs (abfd, link_info))
4751
22
    goto err_free_strtab;
4752
4753
492
  if (strtab != NULL)
4754
72
    {
4755
72
      file_ptr off;
4756
72
      Elf_Internal_Shdr *hdr;
4757
4758
72
      off = elf_next_file_pos (abfd);
4759
4760
72
      hdr = & elf_symtab_hdr (abfd);
4761
72
      off = _bfd_elf_assign_file_position_for_section (hdr, off, true, 0);
4762
4763
72
      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
72
      hdr = &elf_tdata (abfd)->strtab_hdr;
4772
72
      off = _bfd_elf_assign_file_position_for_section (hdr, off, true, 0);
4773
4774
72
      elf_next_file_pos (abfd) = off;
4775
4776
      /* Now that we know where the .strtab section goes, write it
4777
   out.  */
4778
72
      if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
4779
72
    || ! _bfd_elf_strtab_emit (abfd, strtab))
4780
0
  goto err_free_strtab;
4781
72
      _bfd_elf_strtab_free (strtab);
4782
72
    }
4783
4784
492
  abfd->output_has_begun = true;
4785
492
  return true;
4786
4787
23
 err_free_strtab:
4788
23
  if (strtab != NULL)
4789
4
    _bfd_elf_strtab_free (strtab);
4790
23
  return false;
4791
492
}
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
69
{
4802
69
  if (info != NULL && is_elf_hash_table (info->hash))
4803
0
    return elf_hash_table (info)->eh_info.hdr_sec;
4804
69
  return NULL;
4805
69
}
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
36
{
4813
36
  size_t segs;
4814
36
  asection *s;
4815
36
  elf_backend_data *bed;
4816
4817
  /* Assume we will need exactly two PT_LOAD segments: one for text
4818
     and one for data.  */
4819
36
  segs = 2;
4820
4821
36
  s = bfd_get_section_by_name (abfd, ".interp");
4822
36
  if (s != NULL && (s->flags & SEC_LOAD) != 0 && s->size != 0)
4823
0
    {
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
0
      segs += 2;
4829
0
    }
4830
4831
36
  if (bfd_get_section_by_name (abfd, ".dynamic") != NULL)
4832
9
    {
4833
      /* We need a PT_DYNAMIC segment.  */
4834
9
      ++segs;
4835
9
    }
4836
4837
36
  if (info != NULL && info->relro)
4838
0
    {
4839
      /* We need a PT_GNU_RELRO segment.  */
4840
0
      ++segs;
4841
0
    }
4842
4843
36
  if (elf_eh_frame_hdr (info))
4844
0
    {
4845
      /* We need a PT_GNU_EH_FRAME segment.  */
4846
0
      ++segs;
4847
0
    }
4848
4849
36
  if (elf_stack_flags (abfd))
4850
0
    {
4851
      /* We need a PT_GNU_STACK segment.  */
4852
0
      ++segs;
4853
0
    }
4854
4855
36
  if (elf_sframe (abfd))
4856
0
    {
4857
      /* We need a PT_GNU_SFRAME segment.  */
4858
0
      ++segs;
4859
0
    }
4860
4861
36
  s = bfd_get_section_by_name (abfd,
4862
36
             NOTE_GNU_PROPERTY_SECTION_NAME);
4863
36
  if (s != NULL && s->size != 0)
4864
0
    {
4865
      /* We need a PT_GNU_PROPERTY segment.  */
4866
0
      ++segs;
4867
0
    }
4868
4869
276
  for (s = abfd->sections; s != NULL; s = s->next)
4870
240
    {
4871
240
      if ((s->flags & SEC_LOAD) != 0
4872
151
    && elf_section_type (s) == SHT_NOTE)
4873
15
  {
4874
15
    unsigned int alignment_power;
4875
    /* We need a PT_NOTE segment.  */
4876
15
    ++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
15
    alignment_power = s->alignment_power;
4883
15
    while (s->next != NULL
4884
15
     && s->next->alignment_power == alignment_power
4885
11
     && (s->next->flags & SEC_LOAD) != 0
4886
9
     && elf_section_type (s->next) == SHT_NOTE)
4887
0
      s = s->next;
4888
15
  }
4889
240
    }
4890
4891
162
  for (s = abfd->sections; s != NULL; s = s->next)
4892
145
    {
4893
145
      if (s->flags & SEC_THREAD_LOCAL)
4894
19
  {
4895
    /* We need a PT_TLS segment.  */
4896
19
    ++segs;
4897
19
    break;
4898
19
  }
4899
145
    }
4900
4901
36
  bed = get_elf_backend_data (abfd);
4902
4903
36
  if ((abfd->flags & D_PAGED) != 0
4904
35
      && (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
36
  if (bed->elf_backend_additional_program_headers)
4936
31
    {
4937
31
      int a;
4938
4939
31
      a = (*bed->elf_backend_additional_program_headers) (abfd, info);
4940
31
      if (a == -1)
4941
0
  abort ();
4942
31
      segs += a;
4943
31
    }
4944
4945
36
  return segs * bed->s->sizeof_phdr;
4946
36
}
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
124
{
4979
124
  struct elf_segment_map *m;
4980
124
  unsigned int i;
4981
124
  asection **hdrpp;
4982
124
  size_t amt;
4983
4984
124
  amt = sizeof (struct elf_segment_map) - sizeof (asection *);
4985
124
  amt += (to - from) * sizeof (asection *);
4986
124
  m = bfd_zalloc (abfd, amt);
4987
124
  if (m == NULL)
4988
0
    return NULL;
4989
124
  m->next = NULL;
4990
124
  m->p_type = PT_LOAD;
4991
283
  for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++)
4992
159
    m->sections[i - from] = *hdrpp;
4993
124
  m->count = to - from;
4994
4995
124
  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
124
  return m;
5003
124
}
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
8
{
5011
8
  struct elf_segment_map *m;
5012
5013
8
  m = bfd_zalloc (abfd, sizeof (*m));
5014
8
  if (m == NULL)
5015
0
    return NULL;
5016
8
  m->next = NULL;
5017
8
  m->p_type = PT_DYNAMIC;
5018
8
  m->count = 1;
5019
8
  m->sections[0] = dynsec;
5020
5021
8
  return m;
5022
8
}
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
216
{
5031
216
  struct elf_segment_map **m;
5032
216
  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
216
  m = &elf_seg_map (abfd);
5040
1.13k
  while (*m)
5041
922
    {
5042
922
      unsigned int i, new_count;
5043
5044
3.69k
      for (new_count = 0, i = 0; i < (*m)->count; i++)
5045
2.77k
  {
5046
2.77k
    if (((*m)->sections[i]->flags & SEC_EXCLUDE) == 0
5047
2.73k
        && (((*m)->sections[i]->flags & SEC_ALLOC) != 0
5048
1.96k
      || (*m)->p_type != PT_LOAD))
5049
2.73k
      {
5050
2.73k
        (*m)->sections[new_count] = (*m)->sections[i];
5051
2.73k
        new_count++;
5052
2.73k
      }
5053
2.77k
  }
5054
922
      (*m)->count = new_count;
5055
5056
922
      if (remove_empty_load
5057
922
    && (*m)->p_type == PT_LOAD
5058
212
    && (*m)->count == 0
5059
52
    && !(*m)->includes_phdrs)
5060
41
  *m = (*m)->next;
5061
881
      else
5062
881
  m = &(*m)->next;
5063
922
    }
5064
5065
216
  bed = get_elf_backend_data (abfd);
5066
216
  if (bed->elf_backend_modify_segment_map != NULL)
5067
80
    {
5068
80
      if (!(*bed->elf_backend_modify_segment_map) (abfd, info))
5069
0
  return false;
5070
80
    }
5071
5072
216
  return true;
5073
216
}
5074
5075
#define IS_TBSS(s) \
5076
194
  ((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
219
{
5086
219
  unsigned int count;
5087
219
  struct elf_segment_map *m;
5088
219
  asection **sections = NULL;
5089
219
  elf_backend_data *bed = get_elf_backend_data (abfd);
5090
219
  bool no_user_phdrs;
5091
5092
219
  no_user_phdrs = elf_seg_map (abfd) == NULL;
5093
5094
219
  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
219
  if (no_user_phdrs && bfd_count_sections (abfd) != 0)
5108
36
    {
5109
36
      asection *s;
5110
36
      unsigned int i;
5111
36
      struct elf_segment_map *mfirst;
5112
36
      struct elf_segment_map **pm;
5113
36
      asection *last_hdr;
5114
36
      bfd_vma last_size;
5115
36
      unsigned int hdr_index;
5116
36
      bfd_vma maxpagesize;
5117
36
      asection **hdrpp;
5118
36
      bool phdr_in_segment;
5119
36
      bool writable;
5120
36
      bool executable;
5121
36
      unsigned int tls_count = 0;
5122
36
      asection *first_tls = NULL;
5123
36
      asection *first_mbind = NULL;
5124
36
      asection *dynsec, *eh_frame_hdr;
5125
36
      asection *sframe;
5126
36
      size_t amt;
5127
36
      bfd_vma addr_mask, wrap_to = 0;  /* Bytes.  */
5128
36
      bfd_size_type phdr_size;  /* Octets/bytes.  */
5129
36
      unsigned int opb = bfd_octets_per_byte (abfd, NULL);
5130
5131
      /* Select the allocated sections, and sort them.  */
5132
5133
36
      sections = bfd_malloc (bfd_count_sections (abfd) * sizeof (*sections));
5134
36
      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
36
      addr_mask = ((bfd_vma) 1 << (bfd_arch_bits_per_address (abfd) - 1)) - 1;
5141
36
      addr_mask = (addr_mask << 1) + 1;
5142
5143
36
      i = 0;
5144
276
      for (s = abfd->sections; s != NULL; s = s->next)
5145
240
  {
5146
240
    if ((s->flags & SEC_ALLOC) != 0)
5147
160
      {
5148
        /* target_index is unused until bfd_elf_final_link
5149
     starts output of section symbols.  Use it to make
5150
     qsort stable.  */
5151
160
        s->target_index = i;
5152
160
        sections[i] = s;
5153
160
        ++i;
5154
        /* A wrapping section potentially clashes with header.  */
5155
160
        if (((s->lma + s->size / opb) & addr_mask) < (s->lma & addr_mask))
5156
0
    wrap_to = (s->lma + s->size / opb) & addr_mask;
5157
160
      }
5158
240
  }
5159
36
      BFD_ASSERT (i <= bfd_count_sections (abfd));
5160
36
      count = i;
5161
5162
36
      qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections);
5163
5164
36
      phdr_size = elf_program_header_size (abfd);
5165
36
      if (phdr_size == (bfd_size_type) -1)
5166
36
  phdr_size = get_program_header_size (abfd, info);
5167
36
      phdr_size += bed->s->sizeof_ehdr;
5168
      /* phdr_size is compared to LMA values which are in bytes.  */
5169
36
      phdr_size /= opb;
5170
36
      if (info != NULL)
5171
0
  maxpagesize = info->maxpagesize;
5172
36
      else
5173
36
  maxpagesize = bed->maxpagesize;
5174
36
      if (maxpagesize == 0)
5175
0
  maxpagesize = 1;
5176
36
      phdr_in_segment = info != NULL && info->load_phdrs;
5177
36
      if (count != 0
5178
34
    && (((sections[0]->lma & addr_mask) & (maxpagesize - 1))
5179
34
        >= (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
36
      mfirst = NULL;
5187
36
      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
36
      s = bfd_get_section_by_name (abfd, ".interp");
5193
36
      if (s != NULL && (s->flags & SEC_LOAD) != 0 && s->size != 0)
5194
0
  {
5195
0
    m = bfd_zalloc (abfd, sizeof (*m));
5196
0
    if (m == NULL)
5197
0
      goto error_return;
5198
0
    m->next = NULL;
5199
0
    m->p_type = PT_PHDR;
5200
0
    m->p_flags = PF_R;
5201
0
    m->p_flags_valid = 1;
5202
0
    m->includes_phdrs = 1;
5203
0
    phdr_in_segment = true;
5204
0
    *pm = m;
5205
0
    pm = &m->next;
5206
5207
0
    m = bfd_zalloc (abfd, sizeof (*m));
5208
0
    if (m == NULL)
5209
0
      goto error_return;
5210
0
    m->next = NULL;
5211
0
    m->p_type = PT_INTERP;
5212
0
    m->count = 1;
5213
0
    m->sections[0] = s;
5214
5215
0
    *pm = m;
5216
0
    pm = &m->next;
5217
0
  }
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
36
      last_hdr = NULL;
5223
36
      last_size = 0;
5224
36
      hdr_index = 0;
5225
36
      writable = false;
5226
36
      executable = false;
5227
36
      dynsec = bfd_get_section_by_name (abfd, ".dynamic");
5228
36
      if (dynsec != NULL
5229
9
    && (dynsec->flags & SEC_LOAD) == 0)
5230
1
  dynsec = NULL;
5231
5232
36
      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
36
      if (phdr_in_segment && count > 0)
5240
4
  {
5241
4
    bfd_vma phdr_lma;  /* Bytes.  */
5242
4
    bool separate_phdr = false;
5243
5244
4
    phdr_lma = (sections[0]->lma - phdr_size) & addr_mask & -maxpagesize;
5245
4
    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
4
    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
0
      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
4
  }
5292
5293
196
      for (i = 0, hdrpp = sections; i < count; i++, hdrpp++)
5294
160
  {
5295
160
    asection *hdr;
5296
160
    bool new_segment;
5297
5298
160
    hdr = *hdrpp;
5299
5300
    /* See if this section and the last one will fit in the same
5301
       segment.  */
5302
5303
160
    if (last_hdr == NULL)
5304
34
      {
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
34
        new_segment = false;
5308
34
      }
5309
126
    else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma)
5310
0
      {
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
0
        new_segment = true;
5315
0
      }
5316
126
    else if (hdr->lma < last_hdr->lma + last_size
5317
71
       || last_hdr->lma + last_size < last_hdr->lma)
5318
55
      {
5319
        /* If this section has a load address that makes it overlap
5320
     the previous section, then we need a new segment.  */
5321
55
        new_segment = true;
5322
55
      }
5323
71
    else if ((abfd->flags & D_PAGED) != 0
5324
71
       && (((last_hdr->lma + last_size - 1) & -maxpagesize)
5325
71
           == (hdr->lma & -maxpagesize)))
5326
17
      {
5327
        /* If we are demand paged then we can't map two disk
5328
     pages onto the same memory page.  */
5329
17
        new_segment = false;
5330
17
      }
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
54
    else if ((BFD_ALIGN (last_hdr->lma + last_size, maxpagesize)
5337
54
        + maxpagesize > last_hdr->lma)
5338
54
       && (BFD_ALIGN (last_hdr->lma + last_size, maxpagesize)
5339
54
           + maxpagesize <= hdr->lma))
5340
32
      {
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
32
        new_segment = true;
5344
32
      }
5345
22
    else if ((last_hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0
5346
3
       && (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
21
    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
21
    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
21
    else if (! writable
5368
11
       && (hdr->flags & SEC_READONLY) == 0)
5369
3
      {
5370
        /* We don't want to put a writable section in a read only
5371
     segment.  */
5372
3
        new_segment = true;
5373
3
      }
5374
18
    else
5375
18
      {
5376
        /* Otherwise, we can use the same segment.  */
5377
18
        new_segment = false;
5378
18
      }
5379
5380
    /* Allow interested parties a chance to override our decision.  */
5381
160
    if (last_hdr != NULL
5382
126
        && 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
160
    if (! new_segment)
5390
69
      {
5391
69
        if ((hdr->flags & SEC_READONLY) == 0)
5392
18
    writable = true;
5393
69
        if ((hdr->flags & SEC_CODE) != 0)
5394
24
    executable = true;
5395
69
        last_hdr = hdr;
5396
        /* .tbss sections effectively have zero size.  */
5397
69
        last_size = (!IS_TBSS (hdr) ? hdr->size : 0) / opb;
5398
69
        continue;
5399
69
      }
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
91
    m = make_mapping (abfd, sections, hdr_index, i, phdr_in_segment);
5405
91
    if (m == NULL)
5406
0
      goto error_return;
5407
5408
91
    *pm = m;
5409
91
    pm = &m->next;
5410
5411
91
    if ((hdr->flags & SEC_READONLY) == 0)
5412
29
      writable = true;
5413
62
    else
5414
62
      writable = false;
5415
5416
91
    if ((hdr->flags & SEC_CODE) == 0)
5417
75
      executable = false;
5418
16
    else
5419
16
      executable = true;
5420
5421
91
    last_hdr = hdr;
5422
    /* .tbss sections effectively have zero size.  */
5423
91
    last_size = (!IS_TBSS (hdr) ? hdr->size : 0) / opb;
5424
91
    hdr_index = i;
5425
91
    phdr_in_segment = false;
5426
91
  }
5427
5428
      /* Create a final PT_LOAD program segment, but not if it's just
5429
   for .tbss.  */
5430
36
      if (last_hdr != NULL
5431
34
    && (i - hdr_index != 1
5432
31
        || !IS_TBSS (last_hdr)))
5433
33
  {
5434
33
    m = make_mapping (abfd, sections, hdr_index, i, phdr_in_segment);
5435
33
    if (m == NULL)
5436
0
      goto error_return;
5437
5438
33
    *pm = m;
5439
33
    pm = &m->next;
5440
33
  }
5441
5442
      /* If there is a .dynamic section, throw in a PT_DYNAMIC segment.  */
5443
36
      if (dynsec != NULL)
5444
8
  {
5445
8
    m = _bfd_elf_make_dynamic_segment (abfd, dynsec);
5446
8
    if (m == NULL)
5447
0
      goto error_return;
5448
8
    *pm = m;
5449
8
    pm = &m->next;
5450
8
  }
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
276
      for (s = abfd->sections; s != NULL; s = s->next)
5458
240
  {
5459
240
    if ((s->flags & SEC_LOAD) != 0
5460
151
        && elf_section_type (s) == SHT_NOTE)
5461
15
      {
5462
15
        asection *s2;
5463
15
        unsigned int alignment_power = s->alignment_power;
5464
5465
15
        count = 1;
5466
15
        for (s2 = s; s2->next != NULL; s2 = s2->next)
5467
15
    {
5468
15
      if (s2->next->alignment_power == alignment_power
5469
11
          && (s2->next->flags & SEC_LOAD) != 0
5470
9
          && 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
15
      else
5476
15
        break;
5477
15
    }
5478
15
        amt = sizeof (struct elf_segment_map) - sizeof (asection *);
5479
15
        amt += count * sizeof (asection *);
5480
15
        m = bfd_zalloc (abfd, amt);
5481
15
        if (m == NULL)
5482
0
    goto error_return;
5483
15
        m->next = NULL;
5484
15
        m->p_type = PT_NOTE;
5485
15
        m->count = count;
5486
15
        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
15
        m->sections[m->count - 1] = s;
5493
15
        BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
5494
15
        *pm = m;
5495
15
        pm = &m->next;
5496
15
      }
5497
240
    if (s->flags & SEC_THREAD_LOCAL)
5498
27
      {
5499
27
        if (! tls_count)
5500
19
    first_tls = s;
5501
27
        tls_count++;
5502
27
      }
5503
240
    if (first_mbind == NULL
5504
179
        && (elf_section_flags (s) & SHF_GNU_MBIND) != 0)
5505
16
      first_mbind = s;
5506
240
  }
5507
5508
      /* If there are any SHF_TLS output sections, add PT_TLS segment.  */
5509
36
      if (tls_count > 0)
5510
19
  {
5511
19
    amt = sizeof (struct elf_segment_map) - sizeof (asection *);
5512
19
    amt += tls_count * sizeof (asection *);
5513
19
    m = bfd_zalloc (abfd, amt);
5514
19
    if (m == NULL)
5515
0
      goto error_return;
5516
19
    m->next = NULL;
5517
19
    m->p_type = PT_TLS;
5518
19
    m->count = tls_count;
5519
    /* Mandated PF_R.  */
5520
19
    m->p_flags = PF_R;
5521
19
    m->p_flags_valid = 1;
5522
19
    s = first_tls;
5523
41
    for (i = 0; i < tls_count; ++i)
5524
25
      {
5525
25
        if ((s->flags & SEC_THREAD_LOCAL) == 0)
5526
3
    {
5527
3
      _bfd_error_handler
5528
3
        (_("%pB: TLS sections are not adjacent:"), abfd);
5529
3
      s = first_tls;
5530
3
      i = 0;
5531
26
      while (i < tls_count)
5532
23
        {
5533
23
          if ((s->flags & SEC_THREAD_LOCAL) != 0)
5534
8
      {
5535
8
        _bfd_error_handler (_("      TLS: %pA"), s);
5536
8
        i++;
5537
8
      }
5538
15
          else
5539
15
      _bfd_error_handler (_("  non-TLS: %pA"), s);
5540
23
          s = s->next;
5541
23
        }
5542
3
      bfd_set_error (bfd_error_bad_value);
5543
3
      goto error_return;
5544
3
    }
5545
22
        m->sections[i] = s;
5546
22
        s = s->next;
5547
22
      }
5548
5549
16
    *pm = m;
5550
16
    pm = &m->next;
5551
16
  }
5552
5553
33
      if (first_mbind
5554
13
    && (abfd->flags & D_PAGED) != 0
5555
13
    && (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
33
      s = bfd_get_section_by_name (abfd,
5584
33
           NOTE_GNU_PROPERTY_SECTION_NAME);
5585
33
      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
33
      eh_frame_hdr = elf_eh_frame_hdr (info);
5604
33
      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
33
      sframe = elf_sframe (abfd);
5622
33
      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
33
      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
33
      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
33
      free (sections);
5696
33
      elf_seg_map (abfd) = mfirst;
5697
33
    }
5698
5699
216
  if (!elf_modify_segment_map (abfd, info, no_user_phdrs || info == NULL))
5700
0
    return false;
5701
5702
1.09k
  for (count = 0, m = elf_seg_map (abfd); m != NULL; m = m->next)
5703
881
    ++count;
5704
216
  elf_program_header_size (abfd) = count * bed->s->sizeof_phdr;
5705
5706
216
  return true;
5707
5708
3
 error_return:
5709
3
  free (sections);
5710
3
  return false;
5711
216
}
5712
5713
/* Sort sections by address.  */
5714
5715
static int
5716
elf_sort_sections (const void *arg1, const void *arg2)
5717
4.69k
{
5718
4.69k
  const asection *sec1 = *(const asection **) arg1;
5719
4.69k
  const asection *sec2 = *(const asection **) arg2;
5720
4.69k
  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
4.69k
  if (sec1->lma < sec2->lma)
5725
1.67k
    return -1;
5726
3.01k
  else if (sec1->lma > sec2->lma)
5727
1.65k
    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.36k
  if (sec1->vma < sec2->vma)
5732
65
    return -1;
5733
1.30k
  else if (sec1->vma > sec2->vma)
5734
144
    return 1;
5735
5736
  /* Put !SEC_LOAD sections after SEC_LOAD ones.  */
5737
5738
2.31k
#define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0 \
5739
2.31k
      && (x)->size != 0)
5740
5741
1.15k
  if (TOEND (sec1))
5742
1.02k
    {
5743
1.02k
      if (!TOEND (sec2))
5744
0
  return 1;
5745
1.02k
    }
5746
137
  else if (TOEND (sec2))
5747
4
    return -1;
5748
5749
1.15k
#undef TOEND
5750
5751
  /* Sort by size, to put zero sized sections
5752
     before others at the same address.  */
5753
5754
1.15k
  size1 = (sec1->flags & SEC_LOAD) ? sec1->size : 0;
5755
1.15k
  size2 = (sec2->flags & SEC_LOAD) ? sec2->size : 0;
5756
5757
1.15k
  if (size1 < size2)
5758
61
    return -1;
5759
1.09k
  if (size1 > size2)
5760
40
    return 1;
5761
5762
1.05k
  return sec1->target_index - sec2->target_index;
5763
1.09k
}
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.99k
{
5771
1.99k
  const struct elf_segment_map *m1 = *(const struct elf_segment_map **) arg1;
5772
1.99k
  const struct elf_segment_map *m2 = *(const struct elf_segment_map **) arg2;
5773
5774
1.99k
  if (m1->p_type != m2->p_type)
5775
851
    {
5776
851
      if (m1->p_type == PT_NULL)
5777
50
  return 1;
5778
801
      if (m2->p_type == PT_NULL)
5779
96
  return -1;
5780
705
      return m1->p_type < m2->p_type ? -1 : 1;
5781
801
    }
5782
1.14k
  if (m1->includes_filehdr != m2->includes_filehdr)
5783
15
    return m1->includes_filehdr ? -1 : 1;
5784
1.13k
  if (m1->no_sort_lma != m2->no_sort_lma)
5785
0
    return m1->no_sort_lma ? -1 : 1;
5786
1.13k
  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
47
  lma1 = m1->p_paddr;
5792
88
      else if (m1->count != 0)
5793
88
  {
5794
88
    unsigned int opb = bfd_octets_per_byte (m1->sections[0]->owner,
5795
88
              m1->sections[0]);
5796
88
    lma1 = (m1->sections[0]->lma + m1->p_vaddr_offset) * opb;
5797
88
  }
5798
135
      lma2 = 0;
5799
135
      if (m2->p_paddr_valid)
5800
47
  lma2 = m2->p_paddr;
5801
88
      else if (m2->count != 0)
5802
88
  {
5803
88
    unsigned int opb = bfd_octets_per_byte (m2->sections[0]->owner,
5804
88
              m2->sections[0]);
5805
88
    lma2 = (m2->sections[0]->lma + m2->p_vaddr_offset) * opb;
5806
88
  }
5807
135
      if (lma1 != lma2)
5808
74
  return lma1 < lma2 ? -1 : 1;
5809
135
    }
5810
1.05k
  if (m1->idx != m2->idx)
5811
1.05k
    return m1->idx < m2->idx ? -1 : 1;
5812
0
  return 0;
5813
1.05k
}
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
300
{
5841
  /* PR binutils/16199: Handle an alignment of zero.  */
5842
300
  if (maxpagesize == 0)
5843
1
    maxpagesize = 1;
5844
300
  return ((vma - off) % maxpagesize);
5845
300
}
5846
5847
static void
5848
print_segment_map (const struct elf_segment_map *m)
5849
41
{
5850
41
  unsigned int j;
5851
41
  const char *pt = get_segment_type (m->p_type);
5852
41
  char buf[32];
5853
5854
41
  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
41
  fflush (stdout);
5868
41
  fprintf (stderr, "%s:", pt);
5869
611
  for (j = 0; j < m->count; j++)
5870
570
    fprintf (stderr, " %s", m->sections [j]->name);
5871
41
  putc ('\n',stderr);
5872
41
  fflush (stderr);
5873
41
}
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
219
{
5883
219
  elf_backend_data *bed = get_elf_backend_data (abfd);
5884
219
  struct elf_segment_map *m;
5885
219
  struct elf_segment_map *phdr_load_seg;
5886
219
  Elf_Internal_Phdr *phdrs;
5887
219
  Elf_Internal_Phdr *p;
5888
219
  ufile_ptr off;  /* Octets.  */
5889
219
  bfd_size_type maxpagesize;
5890
219
  unsigned int alloc, actual;
5891
219
  unsigned int i, j;
5892
219
  struct elf_segment_map **sorted_seg_map;
5893
219
  unsigned int opb = bfd_octets_per_byte (abfd, NULL);
5894
5895
219
  if (link_info == NULL
5896
219
      && !bfd_elf_map_sections_to_segments (abfd, link_info, NULL))
5897
3
    return false;
5898
5899
216
  alloc = 0;
5900
1.09k
  for (m = elf_seg_map (abfd); m != NULL; m = m->next)
5901
881
    m->idx = alloc++;
5902
5903
216
  if (alloc)
5904
125
    {
5905
125
      elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr;
5906
125
      elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr;
5907
125
    }
5908
91
  else
5909
91
    {
5910
      /* PR binutils/12467.  */
5911
91
      elf_elfheader (abfd)->e_phoff = 0;
5912
91
      elf_elfheader (abfd)->e_phentsize = 0;
5913
91
    }
5914
5915
216
  elf_elfheader (abfd)->e_phnum = alloc;
5916
5917
216
  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
216
  else
5923
216
    {
5924
216
      actual = elf_program_header_size (abfd) / bed->s->sizeof_phdr;
5925
216
      BFD_ASSERT (elf_program_header_size (abfd)
5926
216
      == actual * bed->s->sizeof_phdr);
5927
216
      BFD_ASSERT (actual >= alloc);
5928
216
    }
5929
5930
216
  if (alloc == 0)
5931
91
    {
5932
91
      elf_next_file_pos (abfd) = bed->s->sizeof_ehdr;
5933
91
      return true;
5934
91
    }
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
125
  phdrs = bfd_zalloc (abfd, (actual * sizeof (*phdrs)
5946
125
           + alloc * sizeof (*sorted_seg_map)));
5947
125
  sorted_seg_map = (struct elf_segment_map **) (phdrs + actual);
5948
125
  elf_tdata (abfd)->phdr = phdrs;
5949
125
  if (phdrs == NULL)
5950
0
    return false;
5951
5952
1.00k
  for (m = elf_seg_map (abfd), j = 0; m != NULL; m = m->next, j++)
5953
881
    {
5954
881
      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
881
      if (m->count > 1
5961
450
    && !(elf_elfheader (abfd)->e_type == ET_CORE
5962
371
         && m->p_type == PT_NOTE))
5963
450
  {
5964
3.02k
    for (i = 0; i < m->count; i++)
5965
2.57k
      m->sections[i]->target_index = i;
5966
450
    qsort (m->sections, (size_t) m->count, sizeof (asection *),
5967
450
     elf_sort_sections);
5968
450
  }
5969
881
    }
5970
125
  if (alloc > 1)
5971
109
    qsort (sorted_seg_map, alloc, sizeof (*sorted_seg_map),
5972
109
     elf_sort_segments);
5973
5974
125
  maxpagesize = 1;
5975
125
  if ((abfd->flags & D_PAGED) != 0)
5976
68
    {
5977
68
      if (link_info != NULL)
5978
0
  maxpagesize = link_info->maxpagesize;
5979
68
      else
5980
68
  maxpagesize = bed->maxpagesize;
5981
68
    }
5982
5983
  /* Sections must map to file offsets past the ELF file header.  */
5984
125
  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
125
  phdr_load_seg = NULL;
5989
248
  for (j = 0; j < alloc; j++)
5990
238
    {
5991
238
      m = sorted_seg_map[j];
5992
238
      if (m->p_type != PT_LOAD)
5993
77
  break;
5994
161
      if (m->includes_phdrs)
5995
38
  {
5996
38
    phdr_load_seg = m;
5997
38
    break;
5998
38
  }
5999
161
    }
6000
125
  if (phdr_load_seg == NULL)
6001
87
    off += actual * bed->s->sizeof_phdr;
6002
6003
750
  for (j = 0; j < alloc; j++)
6004
630
    {
6005
630
      asection **secpp;
6006
630
      bfd_vma off_adjust;  /* Octets.  */
6007
630
      bool no_contents;
6008
630
      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
630
      m = sorted_seg_map[j];
6016
630
      p = phdrs + m->idx;
6017
630
      p->p_type = m->p_type;
6018
630
      p->p_flags = m->p_flags;
6019
6020
630
      if (m->count == 0)
6021
255
  p->p_vaddr = m->p_vaddr_offset * opb;
6022
375
      else
6023
375
  p->p_vaddr = (m->sections[0]->vma + m->p_vaddr_offset) * opb;
6024
6025
630
      if (m->p_paddr_valid)
6026
447
  p->p_paddr = m->p_paddr;
6027
183
      else if (m->count == 0)
6028
29
  p->p_paddr = 0;
6029
154
      else
6030
154
  p->p_paddr = (m->sections[0]->lma + m->p_vaddr_offset) * opb;
6031
6032
630
      align_pagesize = 0;
6033
630
      if (p->p_type == PT_LOAD
6034
171
    && (abfd->flags & D_PAGED) != 0)
6035
132
  {
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
132
    if (m->p_align_valid)
6045
26
      maxpagesize = m->p_align;
6046
106
    else if (bed->p_align != 0
6047
29
       && (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
29
      align_pagesize = bed->p_align;
6057
6058
132
    p->p_align = maxpagesize;
6059
132
  }
6060
498
      else if (m->p_align_valid)
6061
252
  p->p_align = m->p_align;
6062
246
      else if (m->count == 0)
6063
116
  p->p_align = 1 << bed->s->log_file_align;
6064
6065
630
      if (m == phdr_load_seg)
6066
38
  off += actual * bed->s->sizeof_phdr;
6067
6068
630
      no_contents = false;
6069
630
      off_adjust = 0;
6070
630
      if (p->p_type == PT_LOAD
6071
171
    && m->count > 0)
6072
160
  {
6073
160
    bfd_size_type align;  /* Bytes.  */
6074
160
    unsigned int align_power = 0;
6075
6076
160
    if (m->p_align_valid)
6077
17
      align = p->p_align;
6078
143
    else
6079
143
      {
6080
514
        for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
6081
371
    {
6082
371
      unsigned int secalign;
6083
6084
371
      secalign = bfd_section_alignment (*secpp);
6085
371
      if (secalign > align_power)
6086
144
        align_power = secalign;
6087
371
    }
6088
143
        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
143
        if (align > bed->p_align)
6093
115
    align_pagesize = 0;
6094
143
        if (align < maxpagesize)
6095
100
    align = maxpagesize;
6096
43
        else
6097
43
    {
6098
      /* If a section requires alignment higher than the
6099
         maximum page size, set p_align to the section
6100
         alignment.  */
6101
43
      if ((abfd->flags & D_PAGED) != 0)
6102
5
        p->p_align = align;
6103
43
    }
6104
143
      }
6105
6106
591
    for (i = 0; i < m->count; i++)
6107
431
      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
60
        elf_section_type (m->sections[i]) = SHT_NOBITS;
6112
6113
    /* Find out whether this segment contains any loadable
6114
       sections.  */
6115
160
    no_contents = true;
6116
185
    for (i = 0; i < m->count; i++)
6117
173
      if (elf_section_type (m->sections[i]) != SHT_NOBITS)
6118
148
        {
6119
148
    no_contents = false;
6120
148
    break;
6121
148
        }
6122
6123
160
    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
160
    if (j != 0
6129
92
        && (abfd->flags & D_PAGED) != 0
6130
65
        && 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
160
    off += off_adjust;
6136
160
    if (no_contents)
6137
12
      {
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
12
      }
6147
148
    else
6148
148
      off_adjust = 0;
6149
160
  }
6150
      /* Make sure the .dynamic section is the first section in the
6151
   PT_DYNAMIC segment.  */
6152
470
      else if (p->p_type == PT_DYNAMIC
6153
33
         && m->count > 1
6154
4
         && ! streq (m->sections[0]->name, ".dynamic"))
6155
1
  {
6156
1
    _bfd_error_handler
6157
1
      (_("%pB: The first section in the PT_DYNAMIC segment"
6158
1
         " is not the .dynamic section"),
6159
1
       abfd);
6160
1
    bfd_set_error (bfd_error_bad_value);
6161
1
    return false;
6162
1
  }
6163
      /* Set the note section type to SHT_NOTE.  */
6164
469
      else if (p->p_type == PT_NOTE)
6165
106
  for (i = 0; i < m->count; i++)
6166
66
    elf_section_type (m->sections[i]) = SHT_NOTE;
6167
6168
629
      if (m->includes_filehdr)
6169
76
  {
6170
76
    if (!m->p_flags_valid)
6171
4
      p->p_flags |= PF_R;
6172
76
    p->p_filesz = bed->s->sizeof_ehdr;
6173
76
    p->p_memsz = bed->s->sizeof_ehdr;
6174
76
    if (p->p_type == PT_LOAD)
6175
36
      {
6176
36
        if (m->count > 0)
6177
27
    {
6178
27
      if (p->p_vaddr < off
6179
27
          || (!m->p_paddr_valid
6180
5
        && p->p_paddr < off))
6181
1
        {
6182
1
          _bfd_error_handler
6183
1
      (_("%pB: not enough room for program headers,"
6184
1
         " try linking with -N"),
6185
1
       abfd);
6186
1
          bfd_set_error (bfd_error_bad_value);
6187
1
          return false;
6188
1
        }
6189
26
      p->p_vaddr -= off;
6190
26
      if (!m->p_paddr_valid)
6191
4
        p->p_paddr -= off;
6192
26
    }
6193
36
      }
6194
40
    else if (sorted_seg_map[0]->includes_filehdr)
6195
23
      {
6196
23
        Elf_Internal_Phdr *filehdr = phdrs + sorted_seg_map[0]->idx;
6197
23
        p->p_vaddr = filehdr->p_vaddr;
6198
23
        if (!m->p_paddr_valid)
6199
8
    p->p_paddr = filehdr->p_paddr;
6200
23
      }
6201
76
  }
6202
6203
628
      if (m->includes_phdrs)
6204
59
  {
6205
59
    if (!m->p_flags_valid)
6206
3
      p->p_flags |= PF_R;
6207
59
    p->p_filesz += actual * bed->s->sizeof_phdr;
6208
59
    p->p_memsz += actual * bed->s->sizeof_phdr;
6209
59
    if (!m->includes_filehdr)
6210
21
      {
6211
21
        if (p->p_type == PT_LOAD)
6212
4
    {
6213
4
      p->p_offset = off - actual * bed->s->sizeof_phdr;
6214
4
      elf_elfheader (abfd)->e_phoff = p->p_offset;
6215
4
      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
4
    }
6222
17
        else if (phdr_load_seg != NULL)
6223
13
    {
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
13
      Elf_Internal_Phdr *phdr = phdrs + phdr_load_seg->idx;
6228
13
      bfd_vma phdr_off = 0;  /* Octets.  */
6229
13
      if (phdr_load_seg->includes_filehdr)
6230
11
        phdr_off = bed->s->sizeof_ehdr;
6231
13
      p->p_vaddr = phdr->p_vaddr + phdr_off;
6232
13
      if (!m->p_paddr_valid)
6233
1
        p->p_paddr = phdr->p_paddr + phdr_off;
6234
13
      p->p_offset = phdr->p_offset + phdr_off;
6235
13
    }
6236
4
        else
6237
4
    p->p_offset = bed->s->sizeof_ehdr;
6238
21
      }
6239
59
  }
6240
6241
628
      if (p->p_type == PT_LOAD
6242
458
    || (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core))
6243
173
  {
6244
173
    if (!m->includes_filehdr && !m->includes_phdrs)
6245
132
      {
6246
132
        p->p_offset = off;
6247
132
        if (no_contents)
6248
9
    {
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
9
      bfd_size_type align = maxpagesize;
6253
9
      if (align < p->p_align)
6254
0
        align = p->p_align;
6255
9
      if (align < 1)
6256
0
        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
9
      p->p_offset = (off + align - 1) % align + 1;
6261
9
    }
6262
132
      }
6263
41
    else
6264
41
      {
6265
41
        file_ptr adjust;  /* Octets.  */
6266
6267
41
        adjust = off - (p->p_offset + p->p_filesz);
6268
41
        if (!no_contents)
6269
38
    p->p_filesz += adjust;
6270
41
        p->p_memsz += adjust;
6271
41
      }
6272
173
  }
6273
6274
628
      if (align_pagesize)
6275
27
  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
1.92k
      for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
6283
1.29k
  {
6284
1.29k
    asection *sec;
6285
1.29k
    bfd_size_type align;
6286
1.29k
    Elf_Internal_Shdr *this_hdr;
6287
6288
1.29k
    sec = *secpp;
6289
1.29k
    this_hdr = &elf_section_data (sec)->this_hdr;
6290
1.29k
    align = (bfd_size_type) 1 << bfd_section_alignment (sec);
6291
6292
1.29k
    if ((p->p_type == PT_LOAD
6293
868
         || p->p_type == PT_DYNAMIC
6294
843
         || p->p_type == PT_TLS)
6295
464
        && (this_hdr->sh_type != SHT_NOBITS
6296
61
      || ((this_hdr->sh_flags & SHF_ALLOC) != 0
6297
61
          && ((this_hdr->sh_flags & SHF_TLS) == 0
6298
2
        || p->p_type == PT_TLS))))
6299
462
      {
6300
462
        bfd_vma p_start = p->p_paddr;   /* Octets.  */
6301
462
        bfd_vma p_end = p_start + p->p_memsz; /* Octets.  */
6302
462
        bfd_vma s_start = sec->lma * opb;   /* Octets.  */
6303
462
        bfd_vma adjust = s_start - p_end;   /* Octets.  */
6304
6305
462
        if (adjust != 0
6306
202
      && (s_start < p_end
6307
119
          || p_end < p_start))
6308
83
    {
6309
83
      _bfd_error_handler
6310
        /* xgettext:c-format */
6311
83
        (_("%pB: section %pA lma %#" PRIx64
6312
83
           " adjusted to %#" PRIx64),
6313
83
         abfd, sec, (uint64_t) s_start / opb,
6314
83
         (uint64_t) p_end / opb);
6315
83
      adjust = 0;
6316
83
      sec->lma = p_end / opb;
6317
83
    }
6318
462
        p->p_memsz += adjust;
6319
6320
462
        if (p->p_type == PT_LOAD)
6321
428
    {
6322
428
      if (this_hdr->sh_type != SHT_NOBITS)
6323
370
        {
6324
370
          off -= off_adjust;
6325
370
          off_adjust = 0;
6326
370
          if (p->p_filesz + adjust < p->p_memsz)
6327
11
      {
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
11
        adjust = p->p_memsz - p->p_filesz;
6336
11
      }
6337
370
        }
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
428
      if (this_hdr->sh_type != SHT_NOBITS || i == 0)
6348
391
        {
6349
391
          off += adjust;
6350
391
          if (this_hdr->sh_type == SHT_NOBITS)
6351
21
      off_adjust += adjust;
6352
391
        }
6353
428
    }
6354
462
        if (this_hdr->sh_type != SHT_NOBITS)
6355
403
    p->p_filesz += adjust;
6356
462
      }
6357
6358
1.29k
    if (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core)
6359
0
      {
6360
        /* The section at i == 0 is the one that actually contains
6361
     everything.  */
6362
0
        if (i == 0)
6363
0
    {
6364
0
      this_hdr->sh_offset = sec->filepos = off;
6365
0
      off += this_hdr->sh_size;
6366
0
      p->p_filesz = this_hdr->sh_size;
6367
0
      p->p_memsz = 0;
6368
0
      p->p_align = 1;
6369
0
    }
6370
0
        else
6371
0
    {
6372
      /* The rest are fake sections that shouldn't be written.  */
6373
0
      sec->filepos = 0;
6374
0
      sec->size = 0;
6375
0
      sec->flags = 0;
6376
0
      continue;
6377
0
    }
6378
0
      }
6379
1.29k
    else
6380
1.29k
      {
6381
1.29k
        if (this_hdr->sh_type == SHT_NOBITS
6382
161
      && (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.29k
        else if (p->p_type == PT_LOAD)
6398
428
    {
6399
428
      this_hdr->sh_offset = sec->filepos = off;
6400
428
      if (this_hdr->sh_type != SHT_NOBITS)
6401
370
        off += this_hdr->sh_size;
6402
428
    }
6403
6404
1.29k
        if (this_hdr->sh_type != SHT_NOBITS)
6405
1.13k
    {
6406
1.13k
      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.13k
      if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
6411
609
        p->p_memsz += this_hdr->sh_size;
6412
1.13k
    }
6413
161
        else if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
6414
161
    {
6415
161
      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
161
      else if ((this_hdr->sh_flags & SHF_TLS) == 0)
6421
159
        p->p_memsz += this_hdr->sh_size;
6422
161
    }
6423
6424
1.29k
        if (align > p->p_align
6425
583
      && !m->p_align_valid
6426
165
      && (p->p_type != PT_LOAD
6427
46
          || (abfd->flags & D_PAGED) == 0))
6428
165
    p->p_align = align;
6429
1.29k
      }
6430
6431
1.29k
    if (!m->p_flags_valid)
6432
130
      {
6433
130
        p->p_flags |= PF_R;
6434
130
        if ((this_hdr->sh_flags & SHF_EXECINSTR) != 0)
6435
26
    p->p_flags |= PF_X;
6436
130
        if ((this_hdr->sh_flags & SHF_WRITE) != 0)
6437
40
    p->p_flags |= PF_W;
6438
130
      }
6439
1.29k
  }
6440
6441
628
      off -= off_adjust;
6442
6443
      /* PR ld/20815 - Check that the program header segment, if
6444
   present, will be loaded into memory.  */
6445
628
      if (p->p_type == PT_PHDR
6446
25
    && phdr_load_seg == NULL
6447
3
    && !(bed->elf_backend_allow_non_load_phdr != NULL
6448
0
         && bed->elf_backend_allow_non_load_phdr (abfd, phdrs, alloc)))
6449
3
  {
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
3
    _bfd_error_handler (_("%pB: error: PHDR segment not covered"
6454
3
        " by LOAD segment"),
6455
3
            abfd);
6456
3
    if (link_info == NULL)
6457
3
      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
625
      if (p->p_type == PT_LOAD && bfd_get_format (abfd) != bfd_core)
6466
131
  {
6467
131
    bool check_vma = true;
6468
6469
342
    for (i = 1; i < m->count; i++)
6470
211
      if (m->sections[i]->vma == m->sections[i - 1]->vma
6471
8
    && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i])
6472
8
               ->this_hdr), p) != 0
6473
4
    && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i - 1])
6474
4
               ->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
463
    for (i = 0; i < m->count; i++)
6482
332
      {
6483
332
        Elf_Internal_Shdr *this_hdr;
6484
332
        asection *sec;
6485
6486
332
        sec = m->sections[i];
6487
332
        this_hdr = &(elf_section_data(sec)->this_hdr);
6488
332
        if (!ELF_SECTION_IN_SEGMENT_1 (this_hdr, p, check_vma, 0)
6489
41
      && !ELF_TBSS_SPECIAL (this_hdr, p))
6490
41
    {
6491
41
      _bfd_error_handler
6492
        /* xgettext:c-format */
6493
41
        (_("%pB: section `%pA' can't be allocated in segment %u"),
6494
41
         abfd, sec, m->idx);
6495
41
      print_segment_map (m);
6496
41
    }
6497
332
      }
6498
131
  }
6499
625
    }
6500
6501
120
  elf_next_file_pos (abfd) = off;
6502
6503
120
  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
120
  return true;
6554
125
}
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
132
{
6563
132
  if (abfd == NULL || bfd_get_flavour (abfd) != bfd_target_elf_flavour)
6564
0
    return false;
6565
6566
132
  Elf_Internal_Shdr **start_headers = elf_elfsections (abfd);
6567
132
  Elf_Internal_Shdr **end_headers = start_headers + elf_numsections (abfd);
6568
132
  Elf_Internal_Shdr **headerp;
6569
6570
367
  for (headerp = start_headers; headerp < end_headers; headerp ++)
6571
367
    {
6572
367
      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
367
      if ((header->sh_flags & SHF_ALLOC) == SHF_ALLOC
6577
227
    && header->sh_type != SHT_NOBITS
6578
226
    && header->sh_type != SHT_NOTE)
6579
132
  return false;
6580
367
    }
6581
6582
0
  return true;
6583
132
}
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
211
{
6592
211
  elf_backend_data *bed = get_elf_backend_data (abfd);
6593
211
  Elf_Internal_Shdr **i_shdrpp;
6594
211
  Elf_Internal_Shdr **hdrpp, **end_hdrpp;
6595
211
  Elf_Internal_Phdr *phdrs;
6596
211
  Elf_Internal_Phdr *p;
6597
211
  struct elf_segment_map *m;
6598
211
  ufile_ptr off;
6599
211
  unsigned int opb = bfd_octets_per_byte (abfd, NULL);
6600
211
  bfd_vma maxpagesize;
6601
6602
211
  if (link_info != NULL)
6603
0
    maxpagesize = link_info->maxpagesize;
6604
211
  else
6605
211
    maxpagesize = bed->maxpagesize;
6606
211
  i_shdrpp = elf_elfsections (abfd);
6607
211
  end_hdrpp = i_shdrpp + elf_numsections (abfd);
6608
211
  off = elf_next_file_pos (abfd);
6609
1.40k
  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
938
    && (hdr->bfd_section->filepos != 0
6617
551
        || (hdr->sh_type == SHT_NOBITS
6618
19
      && hdr->contents == NULL)))
6619
406
  BFD_ASSERT (hdr->sh_offset == hdr->bfd_section->filepos);
6620
790
      else if ((hdr->sh_flags & SHF_ALLOC) != 0)
6621
138
  {
6622
138
    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
132
        && ! is_debuginfo_file (abfd))
6629
132
      _bfd_error_handler
6630
        /* xgettext:c-format */
6631
132
        (_("%pB: warning: allocated section `%s' not in segment"),
6632
132
         abfd,
6633
132
         (hdr->bfd_section == NULL
6634
132
    ? "*unknown*"
6635
132
    : hdr->bfd_section->name));
6636
    /* We don't need to page align empty sections.  */
6637
138
    if ((abfd->flags & D_PAGED) != 0 && hdr->sh_size != 0)
6638
127
      align = maxpagesize;
6639
11
    else
6640
11
      align = hdr->sh_addralign & -hdr->sh_addralign;
6641
138
    off += vma_page_aligned_bias (hdr->sh_addr, off, align);
6642
138
    off = _bfd_elf_assign_file_position_for_section (hdr, off, false,
6643
138
                 bed->s->log_file_align);
6644
138
  }
6645
652
      else if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
6646
11
    && hdr->bfd_section == NULL)
6647
         /* We don't know the offset of these sections yet:
6648
      their size has not been decided.  */
6649
645
         || (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
645
         || hdr == i_shdrpp[elf_onesymtab (abfd)]
6654
625
         || (elf_symtab_shndx_list (abfd) != NULL
6655
0
       && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx])
6656
625
         || hdr == i_shdrpp[elf_strtab_sec (abfd)]
6657
605
         || hdr == i_shdrpp[elf_shstrtab_sec (abfd)])
6658
258
  hdr->sh_offset = -1;
6659
394
      else
6660
394
  off = _bfd_elf_assign_file_position_for_section (hdr, off, true, 0);
6661
1.19k
    }
6662
211
  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
211
  phdrs = elf_tdata (abfd)->phdr;
6667
788
  for (m = elf_seg_map (abfd), p = phdrs; m != NULL; m = m->next, p++)
6668
591
    {
6669
591
      if (p->p_type == PT_GNU_RELRO)
6670
6
  {
6671
6
    bfd_vma start, end;  /* Bytes.  */
6672
6
    bool ok;
6673
6674
6
    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
6
    else if (m->count != 0)
6683
4
      {
6684
4
        if (!m->p_size_valid)
6685
0
    abort ();
6686
4
        start = m->sections[0]->vma;
6687
4
        end = start + m->p_size / opb;
6688
4
      }
6689
2
    else
6690
2
      {
6691
2
        start = 0;
6692
2
        end = 0;
6693
2
      }
6694
6695
6
    ok = false;
6696
6
    if (start < end)
6697
4
      {
6698
4
        struct elf_segment_map *lm;
6699
4
        const Elf_Internal_Phdr *lp;
6700
4
        unsigned int i;
6701
6702
        /* Find a LOAD segment containing a section in the RELRO
6703
     segment.  */
6704
4
        for (lm = elf_seg_map (abfd), lp = phdrs;
6705
23
       lm != NULL;
6706
19
       lm = lm->next, lp++)
6707
21
    {
6708
21
      if (lp->p_type == PT_LOAD
6709
5
          && lm->count != 0
6710
3
          && (lm->sections[lm->count - 1]->vma
6711
3
        + (!IS_TBSS (lm->sections[lm->count - 1])
6712
3
           ? lm->sections[lm->count - 1]->size / opb
6713
3
           : 0)) > start
6714
2
          && lm->sections[0]->vma < end)
6715
2
        break;
6716
21
    }
6717
6718
4
        if (lm != NULL)
6719
2
    {
6720
      /* Find the section starting the RELRO segment.  */
6721
16
      for (i = 0; i < lm->count; i++)
6722
15
        {
6723
15
          asection *s = lm->sections[i];
6724
15
          if (s->vma >= start
6725
2
        && s->vma < end
6726
1
        && s->size != 0)
6727
1
      break;
6728
15
        }
6729
6730
2
      if (i < lm->count)
6731
1
        {
6732
1
          p->p_vaddr = lm->sections[i]->vma * opb;
6733
1
          p->p_paddr = lm->sections[i]->lma * opb;
6734
1
          p->p_offset = lm->sections[i]->filepos;
6735
1
          p->p_memsz = end * opb - p->p_vaddr;
6736
1
          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
1
          if (p->p_filesz > lp->p_vaddr + lp->p_filesz - p->p_vaddr)
6745
1
      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
1
          if (!m->p_align_valid)
6754
0
      p->p_align = 1;
6755
1
          if (!m->p_flags_valid)
6756
0
      p->p_flags = PF_R;
6757
1
          ok = true;
6758
1
        }
6759
2
    }
6760
4
      }
6761
6762
6
    if (!ok)
6763
5
      {
6764
5
        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
5
        memset (p, 0, sizeof *p);
6770
5
      }
6771
6
  }
6772
585
      else if (p->p_type == PT_GNU_STACK)
6773
6
  {
6774
6
    if (m->p_size_valid)
6775
2
      p->p_memsz = m->p_size;
6776
6
  }
6777
579
      else if (m->count != 0)
6778
343
  {
6779
343
    unsigned int i;
6780
6781
343
    if (p->p_type != PT_LOAD
6782
187
        && (p->p_type != PT_NOTE
6783
23
      || bfd_get_format (abfd) != bfd_core))
6784
187
      {
6785
        /* A user specified segment layout may include a PHDR
6786
     segment that overlaps with a LOAD segment...  */
6787
187
        if (p->p_type == PT_PHDR)
6788
0
    {
6789
0
      m->count = 0;
6790
0
      continue;
6791
0
    }
6792
6793
187
        if (m->includes_filehdr || m->includes_phdrs)
6794
14
    {
6795
      /* PR 17512: file: 2195325e.  */
6796
14
      _bfd_error_handler
6797
14
        (_("%pB: error: non-load segment %d includes file header "
6798
14
           "and/or program header"),
6799
14
         abfd, (int) (p - phdrs));
6800
14
      return false;
6801
14
    }
6802
6803
173
        p->p_filesz = 0;
6804
173
        p->p_offset = m->sections[0]->filepos;
6805
202
        for (i = m->count; i-- != 0;)
6806
197
    {
6807
197
      asection *sect = m->sections[i];
6808
197
      Elf_Internal_Shdr *hdr = &elf_section_data (sect)->this_hdr;
6809
197
      if (hdr->sh_type != SHT_NOBITS)
6810
168
        {
6811
168
          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
168
          if (p->p_type == PT_NOTE
6815
23
        && (hdr->sh_flags & SHF_ALLOC) != 0)
6816
19
      p->p_memsz = p->p_filesz;
6817
168
          break;
6818
168
        }
6819
197
    }
6820
173
      }
6821
343
  }
6822
591
    }
6823
6824
197
  return true;
6825
211
}
6826
6827
static elf_section_list *
6828
find_section_in_list (unsigned int i, elf_section_list * list)
6829
989
{
6830
1.01k
  for (;list != NULL; list = list->next)
6831
32
    if (list->ndx == i)
6832
4
      break;
6833
989
  return list;
6834
989
}
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
514
{
6855
514
  struct elf_obj_tdata *tdata = elf_tdata (abfd);
6856
514
  Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd);
6857
514
  elf_backend_data *bed = get_elf_backend_data (abfd);
6858
514
  unsigned int alloc;
6859
6860
514
  if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0
6861
399
      && bfd_get_format (abfd) != bfd_core)
6862
295
    {
6863
295
      Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd);
6864
295
      unsigned int num_sec = elf_numsections (abfd);
6865
295
      Elf_Internal_Shdr **hdrpp;
6866
295
      unsigned int i;
6867
295
      ufile_ptr off;
6868
6869
      /* Start after the ELF header.  */
6870
295
      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.62k
      for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++)
6876
1.32k
  {
6877
1.32k
    Elf_Internal_Shdr *hdr;
6878
6879
1.32k
    hdr = *hdrpp;
6880
1.32k
    if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
6881
157
         && hdr->bfd_section == NULL)
6882
        /* Do not assign offsets for these sections yet: we don't know
6883
     their sizes.  */
6884
1.25k
        || (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.25k
        || i == elf_onesymtab (abfd)
6889
1.20k
        || (elf_symtab_shndx_list (abfd) != NULL
6890
0
      && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx])
6891
1.20k
        || i == elf_strtab_sec (abfd)
6892
1.14k
        || i == elf_shstrtab_sec (abfd))
6893
474
      {
6894
474
        hdr->sh_offset = -1;
6895
474
      }
6896
853
    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
853
      off = _bfd_elf_assign_file_position_for_section (hdr, off, false,
6902
853
                   bed->s->log_file_align);
6903
1.32k
  }
6904
6905
295
      elf_next_file_pos (abfd) = off;
6906
295
      elf_program_header_size (abfd) = 0;
6907
295
    }
6908
219
  else
6909
219
    {
6910
      /* Assign file positions for the loaded sections based on the
6911
   assignment of sections to segments.  */
6912
219
      if (!assign_file_positions_for_load_sections (abfd, link_info))
6913
8
  return false;
6914
6915
      /* And for non-load sections.  */
6916
211
      if (!assign_file_positions_for_non_load_sections (abfd, link_info))
6917
14
  return false;
6918
211
    }
6919
6920
492
  if (!(*bed->elf_backend_modify_headers) (abfd, link_info))
6921
0
    return false;
6922
6923
  /* Write out the program headers.  */
6924
492
  alloc = i_ehdrp->e_phnum;
6925
492
  if (alloc != 0)
6926
106
    {
6927
106
      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
106
      if (bfd_seek (abfd, i_ehdrp->e_phoff, SEEK_SET) != 0
6989
106
    || bed->s->write_out_phdrs (abfd, tdata->phdr, alloc) != 0)
6990
0
  return false;
6991
106
    }
6992
6993
492
  return true;
6994
492
}
6995
6996
bool
6997
_bfd_elf_init_file_header (bfd *abfd,
6998
         struct bfd_link_info *info ATTRIBUTE_UNUSED)
6999
515
{
7000
515
  Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form.  */
7001
515
  struct elf_strtab_hash *shstrtab;
7002
515
  elf_backend_data *bed = get_elf_backend_data (abfd);
7003
7004
515
  i_ehdrp = elf_elfheader (abfd);
7005
7006
515
  shstrtab = _bfd_elf_strtab_init ();
7007
515
  if (shstrtab == NULL)
7008
0
    return false;
7009
7010
515
  elf_shstrtab (abfd) = shstrtab;
7011
7012
515
  i_ehdrp->e_ident[EI_MAG0] = ELFMAG0;
7013
515
  i_ehdrp->e_ident[EI_MAG1] = ELFMAG1;
7014
515
  i_ehdrp->e_ident[EI_MAG2] = ELFMAG2;
7015
515
  i_ehdrp->e_ident[EI_MAG3] = ELFMAG3;
7016
7017
515
  i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass;
7018
515
  i_ehdrp->e_ident[EI_DATA] =
7019
515
    bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB;
7020
515
  i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current;
7021
7022
515
  if ((abfd->flags & DYNAMIC) != 0)
7023
50
    i_ehdrp->e_type = ET_DYN;
7024
465
  else if ((abfd->flags & EXEC_P) != 0)
7025
65
    i_ehdrp->e_type = ET_EXEC;
7026
400
  else if (bfd_get_format (abfd) == bfd_core)
7027
104
    i_ehdrp->e_type = ET_CORE;
7028
296
  else
7029
296
    i_ehdrp->e_type = ET_REL;
7030
7031
515
  switch (bfd_get_arch (abfd))
7032
515
    {
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
515
    default:
7046
515
      i_ehdrp->e_machine = bed->elf_machine_code;
7047
515
    }
7048
7049
515
  i_ehdrp->e_version = bed->s->ev_current;
7050
515
  i_ehdrp->e_ehsize = bed->s->sizeof_ehdr;
7051
7052
  /* No program header, for now.  */
7053
515
  i_ehdrp->e_phoff = 0;
7054
515
  i_ehdrp->e_phentsize = 0;
7055
515
  i_ehdrp->e_phnum = 0;
7056
7057
  /* Each bfd section is section header entry.  */
7058
515
  i_ehdrp->e_entry = bfd_get_start_address (abfd);
7059
515
  i_ehdrp->e_shentsize = bed->s->sizeof_shdr;
7060
7061
515
  elf_tdata (abfd)->symtab_hdr.sh_name =
7062
515
    (unsigned int) _bfd_elf_strtab_add (shstrtab, ".symtab", false);
7063
515
  elf_tdata (abfd)->strtab_hdr.sh_name =
7064
515
    (unsigned int) _bfd_elf_strtab_add (shstrtab, ".strtab", false);
7065
515
  elf_tdata (abfd)->shstrtab_hdr.sh_name =
7066
515
    (unsigned int) _bfd_elf_strtab_add (shstrtab, ".shstrtab", false);
7067
515
  if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
7068
515
      || elf_tdata (abfd)->strtab_hdr.sh_name == (unsigned int) -1
7069
515
      || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1)
7070
0
    return false;
7071
7072
515
  return true;
7073
515
}
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
492
{
7085
492
  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
492
  return true;
7105
492
}
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
490
{
7113
490
  ufile_ptr off;
7114
490
  Elf_Internal_Shdr **shdrpp, **end_shdrpp;
7115
490
  Elf_Internal_Shdr *shdrp;
7116
490
  Elf_Internal_Ehdr *i_ehdrp;
7117
490
  elf_backend_data *bed = get_elf_backend_data (abfd);
7118
7119
  /* Skip non-load sections without section header.  */
7120
490
  if ((abfd->flags & BFD_NO_SECTION_HEADER) != 0)
7121
0
    return true;
7122
7123
490
  off = elf_next_file_pos (abfd);
7124
7125
490
  shdrpp = elf_elfsections (abfd);
7126
490
  end_shdrpp = shdrpp + elf_numsections (abfd);
7127
2.92k
  for (shdrpp++; shdrpp < end_shdrpp; shdrpp++)
7128
2.43k
    {
7129
2.43k
      shdrp = *shdrpp;
7130
2.43k
      if (shdrp->sh_offset == -1)
7131
572
  {
7132
572
    asection *sec = shdrp->bfd_section;
7133
572
    if (sec == NULL
7134
0
        || shdrp->sh_type == SHT_REL
7135
0
        || shdrp->sh_type == SHT_RELA)
7136
572
      ;
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
572
    off = _bfd_elf_assign_file_position_for_section (shdrp, off,
7186
572
      (abfd->flags & (EXEC_P | DYNAMIC))
7187
460
      || bfd_get_format (abfd) == bfd_core,
7188
572
      bed->s->log_file_align);
7189
572
  }
7190
2.43k
    }
7191
7192
  /* Place section name section after DWARF debug sections have been
7193
     compressed.  */
7194
490
  _bfd_elf_strtab_finalize (elf_shstrtab (abfd));
7195
490
  shdrp = &elf_tdata (abfd)->shstrtab_hdr;
7196
490
  shdrp->sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd));
7197
490
  off = _bfd_elf_assign_file_position_for_section (shdrp, off, true, 0);
7198
7199
  /* Place the section headers.  */
7200
490
  i_ehdrp = elf_elfheader (abfd);
7201
490
  off = BFD_ALIGN (off, 1u << bed->s->log_file_align);
7202
490
  i_ehdrp->e_shoff = off;
7203
490
  off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize;
7204
490
  elf_next_file_pos (abfd) = off;
7205
7206
490
  return true;
7207
490
}
7208
7209
bool
7210
_bfd_elf_write_object_contents (bfd *abfd)
7211
498
{
7212
498
  elf_backend_data *bed = get_elf_backend_data (abfd);
7213
498
  Elf_Internal_Shdr **i_shdrp;
7214
498
  bool failed;
7215
498
  unsigned int count, num_sec;
7216
498
  struct elf_obj_tdata *t;
7217
7218
498
  if (! abfd->output_has_begun
7219
322
      && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
7220
8
    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
490
  else if (abfd->direction == both_direction)
7229
0
    {
7230
0
      BFD_ASSERT (abfd->output_has_begun);
7231
0
      return true;
7232
0
    }
7233
7234
490
  i_shdrp = elf_elfsections (abfd);
7235
7236
490
  failed = false;
7237
490
  bfd_map_over_sections (abfd, bed->s->write_relocs, &failed);
7238
490
  if (failed)
7239
0
    return false;
7240
7241
490
  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
490
  num_sec = elf_numsections (abfd);
7246
2.92k
  for (count = 1; count < num_sec; count++)
7247
2.43k
    {
7248
      /* Don't set the sh_name field without section header.  */
7249
2.43k
      if ((abfd->flags & BFD_NO_SECTION_HEADER) == 0)
7250
2.43k
  i_shdrp[count]->sh_name
7251
2.43k
    = _bfd_elf_strtab_offset (elf_shstrtab (abfd),
7252
2.43k
            i_shdrp[count]->sh_name);
7253
2.43k
      if (bed->elf_backend_section_processing)
7254
374
  if (!(*bed->elf_backend_section_processing) (abfd, i_shdrp[count]))
7255
0
    return false;
7256
2.43k
      if (i_shdrp[count]->contents)
7257
150
  {
7258
150
    bfd_size_type amt = i_shdrp[count]->sh_size;
7259
7260
150
    if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0
7261
150
        || bfd_write (i_shdrp[count]->contents, amt, abfd) != amt)
7262
0
      return false;
7263
150
  }
7264
2.43k
    }
7265
7266
  /* Write out the section header names.  */
7267
490
  t = elf_tdata (abfd);
7268
490
  if (elf_shstrtab (abfd) != NULL
7269
490
      && t->shstrtab_hdr.sh_offset != -1
7270
490
      && (bfd_seek (abfd, t->shstrtab_hdr.sh_offset, SEEK_SET) != 0
7271
484
    || !_bfd_elf_strtab_emit (abfd, elf_shstrtab (abfd))))
7272
6
    return false;
7273
7274
484
  if (!(*bed->elf_backend_final_write_processing) (abfd))
7275
0
    return false;
7276
7277
484
  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
484
  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
484
  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
484
  return true;
7289
484
}
7290
7291
bool
7292
_bfd_elf_write_corefile_contents (bfd *abfd)
7293
97
{
7294
  /* Hopefully this can be done just like an object file.  */
7295
97
  return _bfd_elf_write_object_contents (abfd);
7296
97
}
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
875
{
7303
875
  elf_backend_data *bed;
7304
875
  unsigned int sec_index;
7305
7306
875
  if (elf_section_data (asect) != NULL
7307
424
      && elf_section_data (asect)->this_idx != 0)
7308
424
    return elf_section_data (asect)->this_idx;
7309
7310
451
  if (bfd_is_abs_section (asect))
7311
0
    sec_index = SHN_ABS;
7312
451
  else if (bfd_is_com_section (asect))
7313
7
    sec_index = SHN_COMMON;
7314
444
  else if (bfd_is_und_section (asect))
7315
441
    sec_index = SHN_UNDEF;
7316
3
  else
7317
3
    sec_index = SHN_BAD;
7318
7319
451
  bed = get_elf_backend_data (abfd);
7320
451
  if (bed->elf_backend_section_from_bfd_section)
7321
328
    {
7322
328
      int retval = sec_index;
7323
7324
328
      if ((*bed->elf_backend_section_from_bfd_section) (abfd, asect, &retval))
7325
10
  return retval;
7326
328
    }
7327
7328
441
  if (sec_index == SHN_BAD)
7329
0
    bfd_set_error (bfd_error_nonrepresentable_section);
7330
7331
441
  return sec_index;
7332
451
}
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
629
{
7340
629
  asymbol *asym_ptr = *asym_ptr_ptr;
7341
629
  int idx;
7342
629
  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
629
  if (asym_ptr->udata.i == 0
7350
0
      && (flags & BSF_SECTION_SYM)
7351
0
      && asym_ptr->section)
7352
0
    {
7353
0
      asection *sec;
7354
7355
0
      sec = asym_ptr->section;
7356
0
      if (sec->owner != abfd && sec->output_section != NULL)
7357
0
  sec = sec->output_section;
7358
0
      if (sec->owner == abfd
7359
0
    && sec->index < elf_num_section_syms (abfd)
7360
0
    && elf_section_syms (abfd)[sec->index] != NULL)
7361
0
  asym_ptr->udata.i = elf_section_syms (abfd)[sec->index]->udata.i;
7362
0
    }
7363
7364
629
  idx = asym_ptr->udata.i;
7365
7366
629
  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
629
  return idx;
7389
629
}
7390
7391
static inline bfd_vma
7392
segment_size (Elf_Internal_Phdr *segment)
7393
8.03k
{
7394
8.03k
  return (segment->p_memsz > segment->p_filesz
7395
8.03k
    ? segment->p_memsz : segment->p_filesz);
7396
8.03k
}
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
251
{
7403
251
  return start + segment_size (segment);
7404
251
}
7405
7406
static inline bfd_size_type
7407
section_size (asection *section, Elf_Internal_Phdr *segment)
7408
7.82k
{
7409
7.82k
  if ((section->flags & SEC_HAS_CONTENTS) != 0
7410
2.25k
      || (section->flags & SEC_THREAD_LOCAL) == 0
7411
24
      || segment->p_type == PT_TLS)
7412
7.79k
    return section->size;
7413
24
  return 0;
7414
7.82k
}
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
6.42k
{
7424
6.42k
  bfd_vma seg_addr = !use_vaddr ? paddr : vaddr;
7425
6.42k
  bfd_vma addr = !use_vaddr ? section->lma : section->vma;
7426
6.42k
  bfd_vma octet;
7427
6.42k
  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
6.42k
  return (octet >= seg_addr
7434
4.41k
    && segment_size (segment) >= section_size (section, segment)
7435
3.37k
    && (octet - seg_addr
7436
3.37k
        <= segment_size (segment) - section_size (section, segment)));
7437
6.42k
}
7438
7439
/* Handle PT_NOTE segment.  */
7440
static bool
7441
is_note (asection *s, Elf_Internal_Phdr *p)
7442
4.84k
{
7443
4.84k
  return (p->p_type == PT_NOTE
7444
217
    && elf_section_type (s) == SHT_NOTE
7445
31
    && (ufile_ptr) s->filepos >= p->p_offset
7446
18
    && p->p_filesz >= s->size
7447
18
    && (ufile_ptr) s->filepos - p->p_offset <= p->p_filesz - s->size);
7448
4.84k
}
7449
7450
/* Rewrite program header information.  */
7451
7452
static bool
7453
rewrite_elf_program_header (bfd *ibfd, bfd *obfd, bfd_vma maxpagesize)
7454
39
{
7455
39
  Elf_Internal_Ehdr *iehdr;
7456
39
  struct elf_segment_map *map;
7457
39
  struct elf_segment_map *map_first;
7458
39
  struct elf_segment_map **pointer_to_map;
7459
39
  Elf_Internal_Phdr *segment;
7460
39
  asection *section;
7461
39
  unsigned int i;
7462
39
  unsigned int num_segments;
7463
39
  bool phdr_included = false;
7464
39
  bool p_paddr_valid;
7465
39
  struct elf_segment_map *phdr_adjust_seg = NULL;
7466
39
  unsigned int phdr_adjust_num = 0;
7467
39
  elf_backend_data *bed;
7468
39
  unsigned int opb = bfd_octets_per_byte (ibfd, NULL);
7469
7470
39
  bed = get_elf_backend_data (ibfd);
7471
39
  iehdr = elf_elfheader (ibfd);
7472
7473
39
  map_first = NULL;
7474
39
  pointer_to_map = &map_first;
7475
7476
39
  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
39
#define IS_SOLARIS_PT_INTERP(p, s)          \
7482
509
  (p->p_vaddr == 0              \
7483
509
   && p->p_paddr == 0              \
7484
509
   && p->p_memsz == 0              \
7485
509
   && p->p_filesz > 0              \
7486
509
   && (s->flags & SEC_HAS_CONTENTS) != 0        \
7487
509
   && s->size > 0              \
7488
509
   && (bfd_vma) s->filepos >= p->p_offset        \
7489
509
   && ((bfd_vma) s->filepos + s->size         \
7490
5
       <= 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
39
#define IS_SECTION_IN_INPUT_SEGMENT(section, segment, opb, paddr_valid) \
7506
7.13k
  (((is_contained_by (section, segment, segment->p_paddr,    \
7507
5.89k
          segment->p_vaddr, opb, !paddr_valid)    \
7508
5.89k
     && (section->flags & SEC_ALLOC) != 0)       \
7509
5.89k
    || is_note (section, segment))         \
7510
5.89k
   && segment->p_type != PT_GNU_STACK          \
7511
5.89k
   && (segment->p_type != PT_TLS          \
7512
1.06k
       || (section->flags & SEC_THREAD_LOCAL))        \
7513
5.89k
   && (segment->p_type == PT_LOAD          \
7514
1.05k
       || segment->p_type == PT_TLS          \
7515
1.05k
       || (section->flags & SEC_THREAD_LOCAL) == 0)      \
7516
5.89k
   && (segment->p_type != PT_DYNAMIC          \
7517
1.04k
       || section_size (section, segment) > 0        \
7518
1.04k
       || (segment->p_paddr            \
7519
2
     ? segment->p_paddr != section->lma * (opb)      \
7520
2
     : segment->p_vaddr != section->vma * (opb))      \
7521
1.04k
       || (streq (bfd_section_name (section), ".dynamic")))    \
7522
7.13k
   && (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
39
#define INCLUDE_SECTION_IN_SEGMENT(section, segment, opb, paddr_valid)  \
7527
1.23k
  (IS_SECTION_IN_INPUT_SEGMENT (section, segment, opb, paddr_valid) \
7528
1.23k
   && section->output_section != NULL)
7529
7530
  /* Returns TRUE iff seg1 starts after the end of seg2.  */
7531
39
#define SEGMENT_AFTER_SEGMENT(seg1, seg2, field)      \
7532
348
  (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
39
#define SEGMENT_OVERLAPS(seg1, seg2)          \
7541
100
  (   !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr)      \
7542
100
  || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr))     \
7543
100
   && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr)      \
7544
27
  || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr)))
7545
7546
  /* Initialise the segment mark field, and discard stupid alignment.  */
7547
499
  for (section = ibfd->sections; section != NULL; section = section->next)
7548
460
    {
7549
460
      asection *o = section->output_section;
7550
460
      if (o != NULL && o->alignment_power >= (sizeof (bfd_vma) * 8) - 1)
7551
0
  o->alignment_power = 0;
7552
460
      section->segment_mark = false;
7553
460
    }
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
39
  p_paddr_valid = false;
7560
39
  for (i = 0, segment = elf_tdata (ibfd)->phdr;
7561
75
       i < num_segments;
7562
39
       i++, segment++)
7563
70
    if (segment->p_paddr != 0)
7564
34
      {
7565
34
  p_paddr_valid = true;
7566
34
  break;
7567
34
      }
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
39
  for (i = 0, segment = elf_tdata (ibfd)->phdr;
7574
383
       i < num_segments;
7575
344
       i++, segment++)
7576
344
    {
7577
344
      unsigned int j;
7578
344
      Elf_Internal_Phdr *segment2;
7579
7580
344
      if (segment->p_type == PT_INTERP)
7581
537
  for (section = ibfd->sections; section; section = section->next)
7582
509
    if (IS_SOLARIS_PT_INTERP (segment, section))
7583
3
      {
7584
        /* Mininal change so that the normal section to segment
7585
     assignment code will work.  */
7586
3
        segment->p_vaddr = section->vma * opb;
7587
3
        break;
7588
3
      }
7589
7590
344
      if (segment->p_type != PT_LOAD)
7591
252
  {
7592
    /* Remove PT_GNU_RELRO segment.  */
7593
252
    if (segment->p_type == PT_GNU_RELRO)
7594
8
      segment->p_type = PT_NULL;
7595
252
    continue;
7596
252
  }
7597
7598
      /* Determine if this segment overlaps any previous segments.  */
7599
482
      for (j = 0, segment2 = elf_tdata (ibfd)->phdr; j < i; j++, segment2++)
7600
400
  {
7601
400
    bfd_signed_vma extra_length;
7602
7603
400
    if (segment2->p_type != PT_LOAD
7604
100
        || !SEGMENT_OVERLAPS (segment, segment2))
7605
385
      continue;
7606
7607
    /* Merge the two segments together.  */
7608
15
    if (segment2->p_vaddr < segment->p_vaddr)
7609
10
      {
7610
        /* Extend SEGMENT2 to include SEGMENT and then delete
7611
     SEGMENT.  */
7612
10
        extra_length = (segment_end (segment, segment->p_vaddr)
7613
10
            - segment_end (segment2, segment2->p_vaddr));
7614
7615
10
        if (extra_length > 0)
7616
4
    {
7617
4
      segment2->p_memsz += extra_length;
7618
4
      segment2->p_filesz += extra_length;
7619
4
    }
7620
7621
10
        segment->p_type = PT_NULL;
7622
7623
        /* Since we have deleted P we must restart the outer loop.  */
7624
10
        i = 0;
7625
10
        segment = elf_tdata (ibfd)->phdr;
7626
10
        break;
7627
10
      }
7628
5
    else
7629
5
      {
7630
        /* Extend SEGMENT to include SEGMENT2 and then delete
7631
     SEGMENT2.  */
7632
5
        extra_length = (segment_end (segment2, segment2->p_vaddr)
7633
5
            - segment_end (segment, segment->p_vaddr));
7634
7635
5
        if (extra_length > 0)
7636
2
    {
7637
2
      segment->p_memsz += extra_length;
7638
2
      segment->p_filesz += extra_length;
7639
2
    }
7640
7641
5
        segment2->p_type = PT_NULL;
7642
5
      }
7643
15
  }
7644
92
    }
7645
7646
  /* The second scan attempts to assign sections to segments.  */
7647
39
  for (i = 0, segment = elf_tdata (ibfd)->phdr;
7648
340
       i < num_segments;
7649
301
       i++, segment++)
7650
301
    {
7651
301
      unsigned int section_count;
7652
301
      asection **sections;
7653
301
      asection *output_section;
7654
301
      unsigned int isec;
7655
301
      asection *matching_lma;
7656
301
      asection *suggested_lma;
7657
301
      unsigned int j;
7658
301
      size_t amt;
7659
301
      asection *first_section;
7660
7661
301
      if (segment->p_type == PT_NULL)
7662
55
  continue;
7663
7664
246
      first_section = NULL;
7665
      /* Compute how many sections might be placed into this segment.  */
7666
246
      for (section = ibfd->sections, section_count = 0;
7667
4.90k
     section != NULL;
7668
4.66k
     section = section->next)
7669
4.66k
  {
7670
    /* Find the first section in the input segment, which may be
7671
       removed from the corresponding output segment.   */
7672
4.66k
    if (IS_SECTION_IN_INPUT_SEGMENT (section, segment, opb, p_paddr_valid))
7673
520
      {
7674
520
        if (first_section == NULL)
7675
135
    first_section = section;
7676
520
        if (section->output_section != NULL)
7677
520
    ++section_count;
7678
520
      }
7679
4.66k
  }
7680
7681
      /* Allocate a segment map big enough to contain
7682
   all of the sections we have selected.  */
7683
246
      amt = sizeof (struct elf_segment_map) - sizeof (asection *);
7684
246
      amt += section_count * sizeof (asection *);
7685
246
      map = bfd_zalloc (obfd, amt);
7686
246
      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
246
      map->next = NULL;
7692
246
      map->p_type = segment->p_type;
7693
246
      map->p_flags = segment->p_flags;
7694
246
      map->p_flags_valid = 1;
7695
7696
246
      if (map->p_type == PT_LOAD
7697
67
    && (ibfd->flags & D_PAGED) != 0
7698
4
    && maxpagesize > 1
7699
4
    && segment->p_align > 1)
7700
3
  {
7701
3
    map->p_align = segment->p_align;
7702
3
    if (segment->p_align > maxpagesize)
7703
0
      map->p_align = maxpagesize;
7704
3
    map->p_align_valid = 1;
7705
3
  }
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
246
      if (!first_section || first_section->output_section != NULL)
7711
246
  {
7712
246
    map->p_paddr = segment->p_paddr;
7713
246
    map->p_paddr_valid = p_paddr_valid;
7714
246
  }
7715
7716
      /* Determine if this segment contains the ELF file header
7717
   and if it contains the program headers themselves.  */
7718
246
      map->includes_filehdr = (segment->p_offset == 0
7719
86
             && segment->p_filesz >= iehdr->e_ehsize);
7720
246
      map->includes_phdrs = 0;
7721
7722
246
      if (!phdr_included || segment->p_type != PT_LOAD)
7723
239
  {
7724
239
    map->includes_phdrs =
7725
239
      (segment->p_offset <= (bfd_vma) iehdr->e_phoff
7726
109
       && (segment->p_offset + segment->p_filesz
7727
109
     >= ((bfd_vma) iehdr->e_phoff
7728
109
         + iehdr->e_phnum * iehdr->e_phentsize)));
7729
7730
239
    if (segment->p_type == PT_LOAD && map->includes_phdrs)
7731
18
      phdr_included = true;
7732
239
  }
7733
7734
246
      if (section_count == 0)
7735
111
  {
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
111
    if (segment->p_type == PT_LOAD
7745
6
        && !map->includes_phdrs
7746
6
        && (segment->p_filesz > 0 || segment->p_memsz == 0))
7747
      /* xgettext:c-format */
7748
5
      _bfd_error_handler
7749
5
        (_("%pB: warning: empty loadable segment detected"
7750
5
     " at vaddr=%#" PRIx64 ", is this intentional?"),
7751
5
         ibfd, (uint64_t) segment->p_vaddr);
7752
7753
111
    map->p_vaddr_offset = segment->p_vaddr / opb;
7754
111
    map->count = 0;
7755
111
    *pointer_to_map = map;
7756
111
    pointer_to_map = &map->next;
7757
7758
111
    continue;
7759
111
  }
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
135
      sections = bfd_malloc (section_count * sizeof (*sections));
7790
135
      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
135
      isec = 0;
7800
135
      matching_lma = NULL;
7801
135
      suggested_lma = NULL;
7802
7803
135
      for (section = first_section, j = 0;
7804
1.23k
     section != NULL;
7805
1.10k
     section = section->next)
7806
1.23k
  {
7807
1.23k
    if (INCLUDE_SECTION_IN_SEGMENT (section, segment, opb, p_paddr_valid))
7808
520
      {
7809
520
        output_section = section->output_section;
7810
7811
520
        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
520
        if (!p_paddr_valid
7818
21
      && segment->p_vaddr != 0
7819
21
      && !bed->want_p_paddr_set_to_zero
7820
20
      && isec == 0
7821
10
      && output_section->lma != 0
7822
5
      && (align_power (segment->p_vaddr
7823
5
           + (map->includes_filehdr
7824
5
              ? iehdr->e_ehsize : 0)
7825
5
           + (map->includes_phdrs
7826
5
              ? iehdr->e_phnum * iehdr->e_phentsize
7827
5
              : 0),
7828
5
           output_section->alignment_power * opb)
7829
5
          == (output_section->vma * opb)))
7830
1
    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
520
        if (is_contained_by (output_section, segment, map->p_paddr,
7835
520
           0, opb, false)
7836
8
      || is_note (section, segment))
7837
518
    {
7838
518
      if (matching_lma == NULL
7839
383
          || output_section->lma < matching_lma->lma)
7840
186
        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
518
      map->sections[isec++] = output_section;
7846
518
    }
7847
2
        else if (suggested_lma == NULL)
7848
1
    suggested_lma = output_section;
7849
7850
520
        if (j == section_count)
7851
135
    break;
7852
520
      }
7853
1.23k
  }
7854
7855
135
      BFD_ASSERT (j == section_count);
7856
7857
      /* Step Two: Adjust the physical address of the current segment,
7858
   if necessary.  */
7859
135
      if (isec == section_count)
7860
134
  {
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
134
    map->count = section_count;
7866
134
    *pointer_to_map = map;
7867
134
    pointer_to_map = &map->next;
7868
7869
134
    if (p_paddr_valid
7870
126
        && !bed->want_p_paddr_set_to_zero)
7871
126
      {
7872
126
        bfd_vma hdr_size = 0;
7873
126
        if (map->includes_filehdr)
7874
17
    hdr_size = iehdr->e_ehsize;
7875
126
        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
126
        map->p_vaddr_offset = ((map->p_paddr + hdr_size) / opb
7881
126
             - matching_lma->lma);
7882
126
      }
7883
7884
134
    free (sections);
7885
134
    continue;
7886
134
  }
7887
1
      else
7888
1
  {
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
1
    if (matching_lma == NULL)
7893
0
      matching_lma = suggested_lma;
7894
7895
1
    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
1
    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
1
    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
1
  }
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
1
      isec = 0;
7935
1
      do
7936
3
  {
7937
3
    map->count = 0;
7938
3
    suggested_lma = NULL;
7939
7940
    /* Fill the current segment with sections that fit.  */
7941
12
    for (j = 0; j < section_count; j++)
7942
9
      {
7943
9
        section = sections[j];
7944
7945
9
        if (section == NULL)
7946
3
    continue;
7947
7948
6
        output_section = section->output_section;
7949
7950
6
        BFD_ASSERT (output_section != NULL);
7951
7952
6
        if (is_contained_by (output_section, segment, map->p_paddr,
7953
6
           0, opb, false)
7954
3
      || is_note (section, segment))
7955
3
    {
7956
3
      if (map->count == 0)
7957
3
        {
7958
          /* If the first section in a segment does not start at
7959
       the beginning of the segment, then something is
7960
       wrong.  */
7961
3
          if (align_power (map->p_paddr
7962
3
               + (map->includes_filehdr
7963
3
            ? iehdr->e_ehsize : 0)
7964
3
               + (map->includes_phdrs
7965
3
            ? iehdr->e_phnum * iehdr->e_phentsize
7966
3
            : 0),
7967
3
               output_section->alignment_power * opb)
7968
3
        != output_section->lma * opb)
7969
0
      goto sorry;
7970
3
        }
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
3
      map->sections[map->count++] = output_section;
7994
3
      ++isec;
7995
3
      sections[j] = NULL;
7996
3
      if (segment->p_type == PT_LOAD)
7997
0
        section->segment_mark = true;
7998
3
    }
7999
3
        else if (suggested_lma == NULL)
8000
2
    suggested_lma = output_section;
8001
6
      }
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
3
    *pointer_to_map = map;
8011
3
    pointer_to_map = &map->next;
8012
8013
3
    if (isec < section_count)
8014
2
      {
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
2
        amt = sizeof (struct elf_segment_map) - sizeof (asection *);
8019
2
        amt += section_count * sizeof (asection *);
8020
2
        map = bfd_zalloc (obfd, amt);
8021
2
        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
2
        map->next = NULL;
8031
2
        map->p_type = segment->p_type;
8032
2
        map->p_flags = segment->p_flags;
8033
2
        map->p_flags_valid = 1;
8034
2
        map->p_paddr = suggested_lma->lma * opb;
8035
2
        map->p_paddr_valid = p_paddr_valid;
8036
2
        map->includes_filehdr = 0;
8037
2
        map->includes_phdrs = 0;
8038
2
      }
8039
8040
3
    continue;
8041
3
  sorry:
8042
0
    bfd_set_error (bfd_error_sorry);
8043
0
    free (sections);
8044
0
    return false;
8045
3
  }
8046
3
      while (isec < section_count);
8047
8048
1
      free (sections);
8049
1
    }
8050
8051
39
  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
39
  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
39
#undef IS_SOLARIS_PT_INTERP
8078
39
#undef IS_SECTION_IN_INPUT_SEGMENT
8079
39
#undef INCLUDE_SECTION_IN_SEGMENT
8080
39
#undef SEGMENT_AFTER_SEGMENT
8081
39
#undef SEGMENT_OVERLAPS
8082
39
  return true;
8083
39
}
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
64
{
8090
64
  unsigned int i;
8091
64
  Elf_Internal_Phdr *segment;
8092
64
  unsigned int num_segments;
8093
64
  elf_backend_data *bed = get_elf_backend_data (abfd);
8094
64
  bfd_size_type maxpagesize = bed->maxpagesize;
8095
64
  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
64
  if (p_align == 0 || maxpagesize == bed->minpagesize)
8100
64
    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
64
{
8124
64
  Elf_Internal_Ehdr *iehdr;
8125
64
  struct elf_segment_map *map;
8126
64
  struct elf_segment_map *map_first;
8127
64
  struct elf_segment_map **pointer_to_map;
8128
64
  Elf_Internal_Phdr *segment;
8129
64
  unsigned int i;
8130
64
  unsigned int num_segments;
8131
64
  bool phdr_included = false;
8132
64
  bool p_paddr_valid;
8133
64
  bool p_palign_valid;
8134
64
  unsigned int opb = bfd_octets_per_byte (ibfd, NULL);
8135
8136
64
  iehdr = elf_elfheader (ibfd);
8137
8138
64
  map_first = NULL;
8139
64
  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
64
  p_paddr_valid = false;
8144
64
  num_segments = elf_elfheader (ibfd)->e_phnum;
8145
64
  for (i = 0, segment = elf_tdata (ibfd)->phdr;
8146
116
       i < num_segments;
8147
64
       i++, segment++)
8148
108
    if (segment->p_paddr != 0)
8149
56
      {
8150
56
  p_paddr_valid = true;
8151
56
  break;
8152
56
      }
8153
8154
64
  p_palign_valid = elf_is_p_align_valid (ibfd);
8155
8156
64
  for (i = 0, segment = elf_tdata (ibfd)->phdr;
8157
615
       i < num_segments;
8158
551
       i++, segment++)
8159
551
    {
8160
551
      asection *section;
8161
551
      unsigned int section_count;
8162
551
      size_t amt;
8163
551
      Elf_Internal_Shdr *this_hdr;
8164
551
      asection *first_section = NULL;
8165
551
      asection *lowest_section;
8166
8167
      /* Compute how many sections are in this segment.  */
8168
551
      for (section = ibfd->sections, section_count = 0;
8169
4.71k
     section != NULL;
8170
4.16k
     section = section->next)
8171
4.16k
  {
8172
4.16k
    this_hdr = &(elf_section_data(section)->this_hdr);
8173
4.16k
    if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
8174
2.11k
      {
8175
2.11k
        if (first_section == NULL)
8176
380
    first_section = section;
8177
2.11k
        section_count++;
8178
2.11k
      }
8179
4.16k
  }
8180
8181
      /* Allocate a segment map big enough to contain
8182
   all of the sections we have selected.  */
8183
551
      amt = sizeof (struct elf_segment_map) - sizeof (asection *);
8184
551
      amt += section_count * sizeof (asection *);
8185
551
      map = bfd_zalloc (obfd, amt);
8186
551
      if (map == NULL)
8187
0
  return false;
8188
8189
      /* Initialize the fields of the output segment map with the
8190
   input segment.  */
8191
551
      map->next = NULL;
8192
551
      map->p_type = segment->p_type;
8193
551
      map->p_flags = segment->p_flags;
8194
551
      map->p_flags_valid = 1;
8195
551
      map->p_paddr = segment->p_paddr;
8196
551
      map->p_paddr_valid = p_paddr_valid;
8197
551
      map->p_align = segment->p_align;
8198
      /* Keep p_align of PT_GNU_STACK for stack alignment.  */
8199
551
      map->p_align_valid = (map->p_type == PT_GNU_STACK
8200
549
          || p_palign_valid);
8201
551
      map->p_vaddr_offset = 0;
8202
8203
551
      if (map->p_type == PT_GNU_RELRO
8204
545
    || map->p_type == PT_GNU_STACK)
8205
8
  {
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
8
    map->p_size = segment->p_memsz;
8213
8
    map->p_size_valid = 1;
8214
8
  }
8215
8216
      /* Determine if this segment contains the ELF file header
8217
   and if it contains the program headers themselves.  */
8218
551
      map->includes_filehdr = (segment->p_offset == 0
8219
379
             && segment->p_filesz >= iehdr->e_ehsize);
8220
8221
551
      map->includes_phdrs = 0;
8222
551
      if (! phdr_included || segment->p_type != PT_LOAD)
8223
536
  {
8224
536
    map->includes_phdrs =
8225
536
      (segment->p_offset <= (bfd_vma) iehdr->e_phoff
8226
399
       && (segment->p_offset + segment->p_filesz
8227
399
     >= ((bfd_vma) iehdr->e_phoff
8228
399
         + iehdr->e_phnum * iehdr->e_phentsize)));
8229
8230
536
    if (segment->p_type == PT_LOAD && map->includes_phdrs)
8231
19
      phdr_included = true;
8232
536
  }
8233
8234
551
      lowest_section = NULL;
8235
551
      if (section_count != 0)
8236
380
  {
8237
380
    unsigned int isec = 0;
8238
8239
380
    for (section = first_section;
8240
2.15k
         section != NULL;
8241
1.77k
         section = section->next)
8242
2.15k
      {
8243
2.15k
        this_hdr = &(elf_section_data(section)->this_hdr);
8244
2.15k
        if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
8245
2.11k
    {
8246
2.11k
      map->sections[isec++] = section->output_section;
8247
2.11k
      if ((section->flags & SEC_ALLOC) != 0)
8248
157
        {
8249
157
          bfd_vma seg_off;
8250
8251
157
          if (lowest_section == NULL
8252
85
        || section->lma < lowest_section->lma)
8253
76
      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
157
          if ((section->flags & SEC_LOAD) != 0)
8261
116
      seg_off = this_hdr->sh_offset - segment->p_offset;
8262
41
          else
8263
41
      seg_off = this_hdr->sh_addr - segment->p_vaddr;
8264
157
          if (section->lma * opb - segment->p_paddr != seg_off)
8265
52
      map->p_paddr_valid = false;
8266
157
        }
8267
2.11k
      if (isec == section_count)
8268
380
        break;
8269
2.11k
    }
8270
2.15k
      }
8271
380
  }
8272
8273
551
      if (section_count == 0)
8274
171
  map->p_vaddr_offset = segment->p_vaddr / opb;
8275
380
      else if (map->p_paddr_valid)
8276
340
  {
8277
    /* Account for padding before the first section in the segment.  */
8278
340
    bfd_vma hdr_size = 0;
8279
340
    if (map->includes_filehdr)
8280
29
      hdr_size = iehdr->e_ehsize;
8281
340
    if (map->includes_phdrs)
8282
7
      hdr_size += iehdr->e_phnum * iehdr->e_phentsize;
8283
8284
340
    map->p_vaddr_offset = ((map->p_paddr + hdr_size) / opb
8285
340
         - (lowest_section ? lowest_section->lma : 0));
8286
340
  }
8287
8288
551
      map->count = section_count;
8289
551
      *pointer_to_map = map;
8290
551
      pointer_to_map = &map->next;
8291
551
    }
8292
8293
64
  elf_seg_map (obfd) = map_first;
8294
64
  return true;
8295
64
}
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
103
{
8303
103
  bfd_vma maxpagesize;
8304
8305
103
  if (elf_tdata (ibfd)->phdr == NULL)
8306
0
    return true;
8307
8308
103
  if (ibfd->xvec == obfd->xvec)
8309
102
    {
8310
      /* Check to see if any sections in the input BFD
8311
   covered by ELF program header have changed.  */
8312
102
      Elf_Internal_Phdr *segment;
8313
102
      asection * section;
8314
102
      asection * osec;
8315
102
      asection * prev;
8316
102
      unsigned int i, num_segments;
8317
102
      Elf_Internal_Shdr *this_hdr;
8318
102
      elf_backend_data *bed;
8319
8320
102
      bed = get_elf_backend_data (ibfd);
8321
8322
      /* Regenerate the segment map if p_paddr is set to 0.  */
8323
102
      if (bed->want_p_paddr_set_to_zero)
8324
2
  goto rewrite;
8325
8326
      /* Initialize the segment mark field.  */
8327
914
      for (section = obfd->sections; section != NULL;
8328
814
     section = section->next)
8329
814
  section->segment_mark = false;
8330
8331
100
      num_segments = elf_elfheader (ibfd)->e_phnum;
8332
100
      for (i = 0, segment = elf_tdata (ibfd)->phdr;
8333
719
     i < num_segments;
8334
619
     i++, segment++)
8335
655
  {
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
655
    if (segment->p_paddr == 0
8341
435
        && segment->p_memsz == 0
8342
319
        && (segment->p_type == PT_INTERP
8343
316
      || segment->p_type == PT_DYNAMIC))
8344
4
      goto rewrite;
8345
8346
651
    for (section = ibfd->sections, prev = NULL;
8347
5.82k
         section != NULL; section = section->next)
8348
5.20k
      {
8349
        /* We mark the output section so that we know it comes
8350
     from the input BFD.  */
8351
5.20k
        osec = section->output_section;
8352
5.20k
        if (osec)
8353
5.20k
    osec->segment_mark = true;
8354
8355
        /* Check if this section is covered by the segment.  */
8356
5.20k
        this_hdr = &(elf_section_data(section)->this_hdr);
8357
5.20k
        if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
8358
2.26k
    {
8359
      /* FIXME: Check if its output section is changed or
8360
         removed.  What else do we need to check?  */
8361
2.26k
      if (osec == NULL
8362
2.26k
          || section->flags != osec->flags
8363
2.26k
          || section->lma != osec->lma
8364
2.26k
          || section->vma != osec->vma
8365
2.26k
          || section->size != osec->size
8366
2.26k
          || section->rawsize != osec->rawsize
8367
2.26k
          || section->alignment_power != osec->alignment_power)
8368
2
        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.26k
      if (prev != NULL
8374
420
          && section->flags & SEC_ALLOC
8375
170
          && section->lma - section->vma != prev->lma - prev->vma)
8376
30
        goto rewrite;
8377
8378
2.23k
      if (section->flags & SEC_ALLOC)
8379
247
        prev = section;
8380
2.23k
    }
8381
5.20k
      }
8382
651
  }
8383
8384
      /* Check to see if any output section do not come from the
8385
   input BFD.  */
8386
424
      for (section = obfd->sections; section != NULL;
8387
360
     section = section->next)
8388
360
  {
8389
360
    if (!section->segment_mark)
8390
0
      goto rewrite;
8391
360
    else
8392
360
      section->segment_mark = false;
8393
360
  }
8394
8395
64
      return copy_elf_program_header (ibfd, obfd);
8396
64
    }
8397
8398
39
 rewrite:
8399
39
  maxpagesize = 0;
8400
39
  if (ibfd->xvec == obfd->xvec)
8401
38
    {
8402
      /* When rewriting program header, set the output maxpagesize to
8403
   the maximum alignment of input PT_LOAD segments.  */
8404
38
      Elf_Internal_Phdr *segment;
8405
38
      unsigned int i;
8406
38
      unsigned int num_segments = elf_elfheader (ibfd)->e_phnum;
8407
8408
38
      for (i = 0, segment = elf_tdata (ibfd)->phdr;
8409
337
     i < num_segments;
8410
299
     i++, segment++)
8411
299
  if (segment->p_type == PT_LOAD
8412
82
      && maxpagesize < segment->p_align)
8413
39
    {
8414
      /* PR 17512: file: f17299af.  */
8415
39
      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
39
      else
8421
39
        maxpagesize = segment->p_align;
8422
39
    }
8423
38
    }
8424
39
  if (maxpagesize == 0)
8425
9
    maxpagesize = get_elf_backend_data (obfd)->maxpagesize;
8426
8427
39
  return rewrite_elf_program_header (ibfd, obfd, maxpagesize);
8428
103
}
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.31k
{
8442
2.31k
  if (ibfd->xvec->flavour != bfd_target_elf_flavour)
8443
0
    return true;
8444
8445
2.31k
  Elf_Internal_Shdr *ihdr = &elf_section_data (isec)->this_hdr;
8446
2.31k
  Elf_Internal_Shdr *ohdr = &elf_section_data (osec)->this_hdr;
8447
2.31k
  if (link_info == NULL)
8448
2.31k
    {
8449
2.31k
      ohdr->sh_entsize = ihdr->sh_entsize;
8450
8451
2.31k
      if (ihdr->sh_type == SHT_SYMTAB
8452
2.31k
    || ihdr->sh_type == SHT_DYNSYM
8453
2.28k
    || ihdr->sh_type == SHT_GNU_verneed
8454
2.25k
    || ihdr->sh_type == SHT_GNU_verdef)
8455
86
  ohdr->sh_info = ihdr->sh_info;
8456
2.31k
    }
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.31k
  if (elf_section_type (osec) == SHT_PROGBITS
8463
1.91k
      || elf_section_type (osec) == SHT_NOTE
8464
1.86k
      || elf_section_type (osec) == SHT_NOBITS)
8465
464
    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.31k
  bool final_link = (link_info != NULL
8473
0
         && !bfd_link_relocatable (link_info));
8474
2.31k
  if (elf_section_type (osec) == SHT_NULL
8475
2.07k
      && (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.07k
    elf_section_type (osec) = elf_section_type (isec);
8480
8481
2.31k
  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.31k
  if (link_info != NULL || osec->flags != isec->flags)
8488
0
    elf_section_flags (osec) &= (SHF_MASKOS | SHF_MASKPROC);
8489
2.31k
  else
8490
2.31k
    {
8491
      /* Clear only flags which are set below or elsewhere.  */
8492
2.31k
      elf_section_flags (osec) &= ~(SHF_WRITE | SHF_ALLOC | SHF_EXECINSTR
8493
2.31k
            | SHF_MERGE | SHF_STRINGS | SHF_LINK_ORDER
8494
2.31k
            | SHF_INFO_LINK | SHF_GROUP | SHF_TLS
8495
2.31k
            | SHF_COMPRESSED);
8496
2.31k
      if (elf_section_flags (osec) & ~(SHF_MASKOS | SHF_MASKPROC))
8497
171
  _bfd_error_handler
8498
171
    (_("%pB:%pA: warning: retaining unknown section flag(s) %#" PRIx64),
8499
171
     ibfd, isec,
8500
171
     (uint64_t) (elf_section_flags (osec)
8501
171
           & ~(SHF_MASKOS | SHF_MASKPROC)));
8502
2.31k
    }
8503
8504
  /* Copy sh_info from input for mbind section.  */
8505
2.31k
  if ((elf_tdata (ibfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0
8506
408
      && elf_section_flags (isec) & SHF_GNU_MBIND)
8507
56
    elf_section_data (osec)->this_hdr.sh_info
8508
56
      = 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.31k
  if ((link_info == NULL
8515
0
       || !link_info->resolve_section_groups)
8516
2.31k
      && (elf_sec_group (isec) == NULL
8517
26
    || (elf_sec_group (isec)->flags & SEC_LINKER_CREATED) == 0))
8518
2.31k
    {
8519
2.31k
      if (elf_section_flags (isec) & SHF_GROUP)
8520
109
  elf_section_flags (osec) |= SHF_GROUP;
8521
2.31k
      elf_next_in_group (osec) = elf_next_in_group (isec);
8522
2.31k
      elf_section_data (osec)->group = elf_section_data (isec)->group;
8523
2.31k
    }
8524
8525
  /* If not decompress, preserve SHF_COMPRESSED.  */
8526
2.31k
  if (!final_link && (ibfd->flags & BFD_DECOMPRESS) == 0)
8527
2.31k
    elf_section_flags (osec) |= (elf_section_flags (isec)
8528
2.31k
         & 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.31k
  if ((ihdr->sh_flags & SHF_LINK_ORDER) != 0)
8534
53
    {
8535
53
      ohdr->sh_flags |= SHF_LINK_ORDER;
8536
53
      elf_linked_to_section (osec) = elf_linked_to_section (isec);
8537
53
    }
8538
8539
2.31k
  osec->use_rela_p = isec->use_rela_p;
8540
8541
2.31k
  return true;
8542
2.31k
}
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
328
{
8554
328
  asection *isec;
8555
8556
2.57k
  for (isec = ibfd->sections; isec != NULL; isec = isec->next)
8557
2.24k
    if (elf_section_type (isec) == SHT_GROUP)
8558
10
      {
8559
10
  asection *first = elf_next_in_group (isec);
8560
10
  asection *s = first;
8561
10
  bfd_size_type removed = 0;
8562
8563
26
  while (s != NULL)
8564
26
    {
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
26
      if (s->output_section != discarded
8569
26
    && 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
26
      else
8575
26
        {
8576
26
    struct bfd_elf_section_data *elf_sec = elf_section_data (s);
8577
26
    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
26
    else
8592
26
      {
8593
        /* Also adjust for zero-sized relocation member
8594
           section.  */
8595
26
        if (elf_sec->rel.hdr != NULL
8596
0
      && elf_sec->rel.hdr->sh_size == 0)
8597
0
          removed += 4;
8598
26
        if (elf_sec->rela.hdr != NULL
8599
16
      && elf_sec->rela.hdr->sh_size == 0)
8600
0
          removed += 4;
8601
26
      }
8602
26
        }
8603
26
      s = elf_next_in_group (s);
8604
26
      if (s == first)
8605
10
        break;
8606
26
    }
8607
10
  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
10
      }
8635
8636
328
  return true;
8637
328
}
8638
8639
/* Copy private header information.  */
8640
8641
bool
8642
_bfd_elf_copy_private_header_data (bfd *ibfd, bfd *obfd)
8643
328
{
8644
328
  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
328
  if (elf_seg_map (obfd) == NULL && elf_tdata (ibfd)->phdr != NULL)
8653
103
    {
8654
103
      if (! copy_private_bfd_data (ibfd, obfd))
8655
0
  return false;
8656
103
    }
8657
8658
328
  return _bfd_elf_fixup_group_sections (ibfd, NULL);
8659
328
}
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
9
#define MAP_ONESYMTAB (SHN_HIOS + 1)
8668
0
#define MAP_DYNSYMTAB (SHN_HIOS + 2)
8669
17
#define MAP_STRTAB    (SHN_HIOS + 3)
8670
15
#define MAP_SHSTRTAB  (SHN_HIOS + 4)
8671
4
#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
17.3k
{
8679
17.3k
  if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
8680
0
    return true;
8681
8682
17.3k
  elf_symbol_type *isym = elf_symbol_from (*isymarg);
8683
17.3k
  elf_symbol_type *osym = elf_symbol_from (*osymarg);
8684
17.3k
  if (isym != NULL
8685
17.3k
      && isym->internal_elf_sym.st_shndx != 0
8686
1.54k
      && osym != NULL
8687
1.54k
      && bfd_is_abs_section (isym->symbol.section))
8688
1.01k
    {
8689
1.01k
      unsigned int shndx;
8690
8691
1.01k
      shndx = isym->internal_elf_sym.st_shndx;
8692
1.01k
      if (shndx == elf_onesymtab (ibfd))
8693
5
  shndx = MAP_ONESYMTAB;
8694
1.01k
      else if (shndx == elf_dynsymtab (ibfd))
8695
0
  shndx = MAP_DYNSYMTAB;
8696
1.01k
      else if (shndx == elf_elfsections (ibfd)[elf_onesymtab (ibfd)]->sh_link)
8697
10
  shndx = MAP_STRTAB;
8698
1.00k
      else if (shndx == elf_elfheader (ibfd)->e_shstrndx)
8699
14
  shndx = MAP_SHSTRTAB;
8700
989
      else if (find_section_in_list (shndx, elf_symtab_shndx_list (ibfd)))
8701
4
  shndx = MAP_SYM_SHNDX;
8702
1.01k
      osym->internal_elf_sym.st_shndx = shndx;
8703
1.01k
    }
8704
8705
17.3k
  return true;
8706
17.3k
}
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
76
{
8716
76
  elf_backend_data *bed;
8717
76
  unsigned int symcount;
8718
76
  asymbol **syms;
8719
76
  struct elf_strtab_hash *stt;
8720
76
  Elf_Internal_Shdr *symtab_hdr;
8721
76
  Elf_Internal_Shdr *symtab_shndx_hdr;
8722
76
  Elf_Internal_Shdr *symstrtab_hdr;
8723
76
  struct elf_sym_strtab *symstrtab;
8724
76
  bfd_byte *outbound_syms;
8725
76
  bfd_byte *outbound_shndx;
8726
76
  unsigned long outbound_syms_index;
8727
76
  unsigned int idx;
8728
76
  unsigned int num_locals;
8729
76
  size_t amt;
8730
76
  bool name_local_sections;
8731
8732
76
  if (!elf_map_symbols (abfd, &num_locals))
8733
0
    return false;
8734
8735
  /* Dump out the symtabs.  */
8736
76
  stt = _bfd_elf_strtab_init ();
8737
76
  if (stt == NULL)
8738
0
    return false;
8739
8740
76
  bed = get_elf_backend_data (abfd);
8741
76
  symcount = bfd_get_symcount (abfd);
8742
76
  symtab_hdr = &elf_symtab_hdr (abfd);
8743
76
  symtab_hdr->sh_type = SHT_SYMTAB;
8744
76
  symtab_hdr->sh_entsize = bed->s->sizeof_sym;
8745
76
  symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1);
8746
76
  symtab_hdr->sh_info = num_locals + 1;
8747
76
  symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
8748
8749
76
  symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
8750
76
  symstrtab_hdr->sh_type = SHT_STRTAB;
8751
8752
  /* Allocate buffer to swap out the .strtab section.  */
8753
76
  if (_bfd_mul_overflow (symcount + 1, sizeof (*symstrtab), &amt)
8754
76
      || (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
76
  if (_bfd_mul_overflow (symcount + 1, bed->s->sizeof_sym, &amt)
8762
76
      || (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
76
  symtab_hdr->contents = outbound_syms;
8772
76
  outbound_syms_index = 0;
8773
8774
76
  outbound_shndx = NULL;
8775
8776
76
  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
76
  {
8799
    /* Fill in zeroth symbol and swap it out.  */
8800
76
    Elf_Internal_Sym sym;
8801
76
    sym.st_name = 0;
8802
76
    sym.st_value = 0;
8803
76
    sym.st_size = 0;
8804
76
    sym.st_info = 0;
8805
76
    sym.st_other = 0;
8806
76
    sym.st_shndx = SHN_UNDEF;
8807
76
    sym.st_target_internal = 0;
8808
76
    symstrtab[outbound_syms_index].sym = sym;
8809
76
    symstrtab[outbound_syms_index].dest_index = outbound_syms_index;
8810
76
    outbound_syms_index++;
8811
76
  }
8812
8813
76
  name_local_sections
8814
76
    = (bed->elf_backend_name_local_section_symbols
8815
6
       && bed->elf_backend_name_local_section_symbols (abfd));
8816
8817
76
  syms = bfd_get_outsymbols (abfd);
8818
1.46k
  for (idx = 0; idx < symcount; idx++)
8819
1.38k
    {
8820
1.38k
      Elf_Internal_Sym sym;
8821
8822
1.38k
      flagword flags = syms[idx]->flags;
8823
1.38k
      if (!name_local_sections
8824
1.28k
    && (flags & (BSF_SECTION_SYM | BSF_GLOBAL)) == BSF_SECTION_SYM)
8825
77
  {
8826
    /* Local section symbols have no name.  */
8827
77
    sym.st_name = 0;
8828
77
  }
8829
1.31k
      else
8830
1.31k
  {
8831
    /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
8832
       to get the final offset for st_name.  */
8833
1.31k
    size_t stridx = _bfd_elf_strtab_add (stt, syms[idx]->name, false);
8834
1.31k
    if (stridx == (size_t) -1)
8835
0
      goto error_return;
8836
1.31k
    sym.st_name = stridx;
8837
1.31k
  }
8838
8839
1.38k
      bfd_vma value = syms[idx]->value;
8840
1.38k
      elf_symbol_type *type_ptr = elf_symbol_from (syms[idx]);
8841
1.38k
      asection *sec = syms[idx]->section;
8842
8843
1.38k
      if ((flags & BSF_SECTION_SYM) == 0 && bfd_is_com_section (sec))
8844
7
  {
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
7
    sym.st_size = value;
8849
7
    if (type_ptr == NULL
8850
7
        || type_ptr->internal_elf_sym.st_value == 0)
8851
4
      sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value));
8852
3
    else
8853
3
      sym.st_value = type_ptr->internal_elf_sym.st_value;
8854
7
    sym.st_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
8855
7
  }
8856
1.38k
      else
8857
1.38k
  {
8858
1.38k
    unsigned int shndx;
8859
8860
1.38k
    if (sec->output_section)
8861
1.33k
      {
8862
1.33k
        value += sec->output_offset;
8863
1.33k
        sec = sec->output_section;
8864
1.33k
      }
8865
8866
    /* Don't add in the section vma for relocatable output.  */
8867
1.38k
    if (! relocatable_p)
8868
714
      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
676
        && type_ptr != NULL
8874
676
        && type_ptr->internal_elf_sym.st_shndx != 0)
8875
676
      {
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
676
        shndx = type_ptr->internal_elf_sym.st_shndx;
8880
676
        switch (shndx)
8881
676
    {
8882
4
    case MAP_ONESYMTAB:
8883
4
      shndx = elf_onesymtab (abfd);
8884
4
      break;
8885
0
    case MAP_DYNSYMTAB:
8886
0
      shndx = elf_dynsymtab (abfd);
8887
0
      break;
8888
7
    case MAP_STRTAB:
8889
7
      shndx = elf_strtab_sec (abfd);
8890
7
      break;
8891
1
    case MAP_SHSTRTAB:
8892
1
      shndx = elf_shstrtab_sec (abfd);
8893
1
      break;
8894
0
    case MAP_SYM_SHNDX:
8895
0
      if (elf_symtab_shndx_list (abfd))
8896
0
        shndx = elf_symtab_shndx_list (abfd)->ndx;
8897
0
      break;
8898
0
    case SHN_COMMON:
8899
6
    case SHN_ABS:
8900
6
      shndx = SHN_ABS;
8901
6
      break;
8902
658
    default:
8903
658
      if (shndx >= SHN_LOPROC && shndx <= SHN_HIOS)
8904
6
        {
8905
6
          if (bed->symbol_section_index)
8906
0
      shndx = bed->symbol_section_index (abfd, type_ptr);
8907
          /* Otherwise just leave the index alone.  */
8908
6
        }
8909
652
      else
8910
652
        {
8911
652
          if (shndx > SHN_HIOS && shndx < SHN_HIRESERVE)
8912
5
      _bfd_error_handler (_("%pB: \
8913
5
Unable to handle section index %x in ELF symbol.  Using ABS instead."),
8914
5
            abfd, shndx);
8915
652
          shndx = SHN_ABS;
8916
652
        }
8917
658
      break;
8918
676
    }
8919
676
      }
8920
705
    else
8921
705
      {
8922
705
        shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
8923
8924
705
        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
705
      }
8951
8952
1.38k
    sym.st_shndx = shndx;
8953
1.38k
  }
8954
8955
1.38k
      int type;
8956
1.38k
      if ((flags & BSF_THREAD_LOCAL) != 0)
8957
72
  type = STT_TLS;
8958
1.31k
      else if ((flags & BSF_GNU_INDIRECT_FUNCTION) != 0)
8959
104
  type = STT_GNU_IFUNC;
8960
1.21k
      else if ((flags & BSF_FUNCTION) != 0)
8961
172
  type = STT_FUNC;
8962
1.04k
      else if ((flags & BSF_OBJECT) != 0)
8963
186
  type = STT_OBJECT;
8964
854
      else if ((flags & BSF_RELC) != 0)
8965
26
  type = STT_RELC;
8966
828
      else if ((flags & BSF_SRELC) != 0)
8967
67
  type = STT_SRELC;
8968
761
      else
8969
761
  type = STT_NOTYPE;
8970
8971
1.38k
      if (syms[idx]->section->flags & SEC_THREAD_LOCAL)
8972
1
  type = STT_TLS;
8973
8974
      /* Processor-specific types.  */
8975
1.38k
      if (type_ptr != NULL
8976
1.38k
    && bed->elf_backend_get_symbol_type)
8977
31
  type = ((*bed->elf_backend_get_symbol_type)
8978
31
    (&type_ptr->internal_elf_sym, type));
8979
8980
1.38k
      if (flags & BSF_SECTION_SYM)
8981
95
  {
8982
95
    if (flags & BSF_GLOBAL)
8983
15
      sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
8984
80
    else
8985
80
      sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
8986
95
  }
8987
1.29k
      else if (bfd_is_com_section (syms[idx]->section))
8988
7
  {
8989
7
    if (type != STT_TLS)
8990
7
      {
8991
7
        if ((abfd->flags & BFD_CONVERT_ELF_COMMON))
8992
0
    type = ((abfd->flags & BFD_USE_ELF_STT_COMMON)
8993
0
      ? STT_COMMON : STT_OBJECT);
8994
7
        else
8995
7
    type = ((flags & BSF_ELF_COMMON) != 0
8996
7
      ? STT_COMMON : STT_OBJECT);
8997
7
      }
8998
7
    sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
8999
7
  }
9000
1.28k
      else if (bfd_is_und_section (syms[idx]->section))
9001
439
  sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK)
9002
1.28k
            ? STB_WEAK
9003
1.28k
            : STB_GLOBAL),
9004
1.28k
           type);
9005
847
      else if (flags & BSF_FILE)
9006
15
  sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
9007
832
      else
9008
832
  {
9009
832
    int bind = STB_LOCAL;
9010
9011
832
    if (flags & BSF_LOCAL)
9012
468
      bind = STB_LOCAL;
9013
364
    else if (flags & BSF_GNU_UNIQUE)
9014
19
      bind = STB_GNU_UNIQUE;
9015
345
    else if (flags & BSF_WEAK)
9016
25
      bind = STB_WEAK;
9017
320
    else if (flags & BSF_GLOBAL)
9018
237
      bind = STB_GLOBAL;
9019
9020
832
    sym.st_info = ELF_ST_INFO (bind, type);
9021
832
  }
9022
9023
1.38k
      if (type_ptr != NULL)
9024
1.38k
  {
9025
1.38k
    sym.st_other = type_ptr->internal_elf_sym.st_other;
9026
1.38k
    sym.st_target_internal
9027
1.38k
      = type_ptr->internal_elf_sym.st_target_internal;
9028
1.38k
  }
9029
0
      else
9030
0
  {
9031
0
    sym.st_other = 0;
9032
0
    sym.st_target_internal = 0;
9033
0
  }
9034
9035
1.38k
      symstrtab[outbound_syms_index].sym = sym;
9036
1.38k
      symstrtab[outbound_syms_index].dest_index = outbound_syms_index;
9037
1.38k
      outbound_syms_index++;
9038
1.38k
    }
9039
9040
  /* Finalize the .strtab section.  */
9041
76
  _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
163
  elfsym->sym.st_name = _bfd_elf_strtab_offset (stt,
9049
163
                  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
76
  free (symstrtab);
9065
9066
76
  *sttp = stt;
9067
76
  symstrtab_hdr->sh_size = _bfd_elf_strtab_size (stt);
9068
76
  symstrtab_hdr->sh_type = SHT_STRTAB;
9069
76
  symstrtab_hdr->sh_addralign = 1;
9070
9071
76
  return true;
9072
76
}
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.48k
{
9083
8.48k
  bfd_size_type symcount;
9084
8.48k
  long symtab_size;
9085
8.48k
  Elf_Internal_Shdr *hdr = &elf_symtab_hdr (abfd);
9086
9087
8.48k
  symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
9088
8.48k
  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.48k
  symtab_size = symcount * (sizeof (asymbol *));
9094
8.48k
  if (symcount == 0)
9095
662
    symtab_size = sizeof (asymbol *);
9096
7.82k
  else if (!bfd_write_p (abfd))
9097
7.82k
    {
9098
7.82k
      ufile_ptr filesize = bfd_get_file_size (abfd);
9099
9100
7.82k
      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
7.82k
    }
9106
9107
8.27k
  return symtab_size;
9108
8.48k
}
9109
9110
long
9111
_bfd_elf_get_dynamic_symtab_upper_bound (bfd *abfd)
9112
16.8k
{
9113
16.8k
  bfd_size_type symcount;
9114
16.8k
  long symtab_size;
9115
16.8k
  Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr;
9116
9117
16.8k
  if (elf_dynsymtab (abfd) == 0)
9118
16.4k
    {
9119
      /* Check if there is dynamic symbol table.  */
9120
16.4k
      symcount = elf_tdata (abfd)->dt_symtab_count;
9121
16.4k
      if (symcount)
9122
10
  goto compute_symtab_size;
9123
9124
16.4k
      bfd_set_error (bfd_error_invalid_operation);
9125
16.4k
      return -1;
9126
16.4k
    }
9127
9128
425
  symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
9129
425
  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
435
 compute_symtab_size:
9136
435
  symtab_size = symcount * (sizeof (asymbol *));
9137
435
  if (symcount == 0)
9138
7
    symtab_size = sizeof (asymbol *);
9139
428
  else if (!bfd_write_p (abfd))
9140
428
    {
9141
428
      ufile_ptr filesize = bfd_get_file_size (abfd);
9142
9143
428
      if (filesize != 0 && (unsigned long) symtab_size > filesize)
9144
18
  {
9145
18
    bfd_set_error (bfd_error_file_truncated);
9146
18
    return -1;
9147
18
  }
9148
428
    }
9149
9150
417
  return symtab_size;
9151
435
}
9152
9153
long
9154
_bfd_elf_get_reloc_upper_bound (bfd *abfd, sec_ptr asect)
9155
20.8k
{
9156
20.8k
  if (asect->reloc_count != 0 && !bfd_write_p (abfd))
9157
18.3k
    {
9158
      /* Sanity check reloc section size.  */
9159
18.3k
      ufile_ptr filesize = bfd_get_file_size (abfd);
9160
9161
18.3k
      if (filesize != 0)
9162
18.3k
  {
9163
18.3k
    struct bfd_elf_section_data *d = elf_section_data (asect);
9164
18.3k
    bfd_size_type rel_size = d->rel.hdr ? d->rel.hdr->sh_size : 0;
9165
18.3k
    bfd_size_type rela_size = d->rela.hdr ? d->rela.hdr->sh_size : 0;
9166
9167
18.3k
    if (rel_size + rela_size > filesize
9168
17.6k
        || rel_size + rela_size < rel_size)
9169
677
      {
9170
677
        bfd_set_error (bfd_error_file_truncated);
9171
677
        return -1;
9172
677
      }
9173
18.3k
  }
9174
18.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
20.1k
  return (asect->reloc_count + 1L) * sizeof (arelent *);
9184
20.8k
}
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
20.1k
{
9194
20.1k
  arelent *tblptr;
9195
20.1k
  unsigned int i;
9196
20.1k
  elf_backend_data *bed = get_elf_backend_data (abfd);
9197
9198
20.1k
  if (! bed->s->slurp_reloc_table (abfd, section, symbols, false))
9199
4.49k
    return -1;
9200
9201
15.6k
  tblptr = section->relocation;
9202
166k
  for (i = 0; i < section->reloc_count; i++)
9203
150k
    *relptr++ = tblptr++;
9204
9205
15.6k
  *relptr = NULL;
9206
9207
15.6k
  return section->reloc_count;
9208
20.1k
}
9209
9210
long
9211
_bfd_elf_canonicalize_symtab (bfd *abfd, asymbol **allocation)
9212
8.27k
{
9213
8.27k
  elf_backend_data *bed = get_elf_backend_data (abfd);
9214
8.27k
  long symcount = bed->s->slurp_symbol_table (abfd, allocation, false);
9215
9216
8.27k
  if (symcount >= 0)
9217
7.40k
    abfd->symcount = symcount;
9218
8.27k
  return symcount;
9219
8.27k
}
9220
9221
long
9222
_bfd_elf_canonicalize_dynamic_symtab (bfd *abfd,
9223
              asymbol **allocation)
9224
271
{
9225
271
  elf_backend_data *bed = get_elf_backend_data (abfd);
9226
271
  long symcount = bed->s->slurp_symbol_table (abfd, allocation, true);
9227
9228
271
  if (symcount >= 0)
9229
129
    abfd->dynsymcount = symcount;
9230
271
  return symcount;
9231
271
}
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.4k
{
9241
16.4k
  bfd_size_type count, ext_rel_size;
9242
16.4k
  asection *s;
9243
9244
16.4k
  if (elf_dynsymtab (abfd) == 0)
9245
16.2k
    {
9246
16.2k
      bfd_set_error (bfd_error_invalid_operation);
9247
16.2k
      return -1;
9248
16.2k
    }
9249
9250
191
  count = 1;
9251
191
  ext_rel_size = 0;
9252
4.35k
  for (s = abfd->sections; s != NULL; s = s->next)
9253
4.16k
    if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
9254
540
  && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
9255
516
      || elf_section_data (s)->this_hdr.sh_type == SHT_RELA)
9256
222
  && (elf_section_data (s)->this_hdr.sh_flags & SHF_COMPRESSED) == 0)
9257
222
      {
9258
222
  ext_rel_size += elf_section_data (s)->this_hdr.sh_size;
9259
222
  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
222
  count += NUM_SHDR_ENTRIES (&elf_section_data (s)->this_hdr);
9265
222
  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
222
      }
9271
191
  if (count > 1 && !bfd_write_p (abfd))
9272
139
    {
9273
      /* Sanity check reloc section sizes.  */
9274
139
      ufile_ptr filesize = bfd_get_file_size (abfd);
9275
139
      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
139
    }
9281
188
  return count * sizeof (arelent *);
9282
191
}
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
169
{
9297
169
  bool (*slurp_relocs) (bfd *, asection *, asymbol **, bool);
9298
169
  asection *s;
9299
169
  long ret;
9300
9301
169
  if (elf_dynsymtab (abfd) == 0)
9302
0
    {
9303
0
      bfd_set_error (bfd_error_invalid_operation);
9304
0
      return -1;
9305
0
    }
9306
9307
169
  slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
9308
169
  ret = 0;
9309
3.42k
  for (s = abfd->sections; s != NULL; s = s->next)
9310
3.27k
    {
9311
3.27k
      if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
9312
452
    && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
9313
431
        || elf_section_data (s)->this_hdr.sh_type == SHT_RELA)
9314
186
    && (elf_section_data (s)->this_hdr.sh_flags & SHF_COMPRESSED) == 0)
9315
186
  {
9316
186
    arelent *p;
9317
186
    long count, i;
9318
9319
186
    if (! (*slurp_relocs) (abfd, s, syms, true))
9320
19
      return -1;
9321
167
    count = NUM_SHDR_ENTRIES (&elf_section_data (s)->this_hdr);
9322
167
    p = s->relocation;
9323
1.46k
    for (i = 0; i < count; i++)
9324
1.29k
      *storage++ = p++;
9325
167
    ret += count;
9326
167
  }
9327
3.27k
    }
9328
9329
150
  *storage = NULL;
9330
9331
150
  return ret;
9332
169
}
9333

9334
/* Read in the version information.  */
9335
9336
bool
9337
_bfd_elf_slurp_version_tables (bfd *abfd, bool default_imported_symver)
9338
315
{
9339
315
  bfd_byte *contents = NULL;
9340
315
  unsigned int freeidx = 0;
9341
315
  size_t amt;
9342
315
  void *contents_addr = NULL;
9343
315
  size_t contents_size = 0;
9344
9345
315
  if (elf_dynverref (abfd) != 0 || elf_tdata (abfd)->dt_verneed != NULL)
9346
279
    {
9347
279
      Elf_Internal_Shdr *hdr;
9348
279
      Elf_External_Verneed *everneed;
9349
279
      Elf_Internal_Verneed *iverneed;
9350
279
      unsigned int i;
9351
279
      bfd_byte *contents_end;
9352
279
      size_t verneed_count;
9353
279
      size_t verneed_size;
9354
9355
279
      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
277
      else
9363
277
  {
9364
277
    hdr = &elf_tdata (abfd)->dynverref_hdr;
9365
9366
277
    if (hdr->sh_info > hdr->sh_size / sizeof (Elf_External_Verneed))
9367
6
      {
9368
127
      error_return_bad_verref:
9369
127
        _bfd_error_handler
9370
127
    (_("%pB: .gnu.version_r invalid entry"), abfd);
9371
127
        bfd_set_error (bfd_error_bad_value);
9372
154
      error_return_verref:
9373
154
        elf_tdata (abfd)->verref = NULL;
9374
154
        elf_tdata (abfd)->cverrefs = 0;
9375
154
        goto error_return;
9376
127
      }
9377
9378
271
    if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0)
9379
5
      goto error_return_verref;
9380
266
    contents_size = hdr->sh_size;
9381
266
    contents = _bfd_mmap_temporary (abfd, contents_size,
9382
266
            &contents_addr, &contents_size);
9383
266
    if (contents == NULL)
9384
22
      goto error_return_verref;
9385
9386
244
    verneed_size = hdr->sh_size;
9387
244
    verneed_count = hdr->sh_info;
9388
244
  }
9389
9390
246
      if (_bfd_mul_overflow (verneed_count,
9391
246
           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
246
      if (amt == 0)
9397
0
  goto error_return_verref;
9398
246
      elf_tdata (abfd)->verref = bfd_zalloc (abfd, amt);
9399
246
      if (elf_tdata (abfd)->verref == NULL)
9400
0
  goto error_return_verref;
9401
9402
246
      BFD_ASSERT (sizeof (Elf_External_Verneed)
9403
246
      == sizeof (Elf_External_Vernaux));
9404
246
      contents_end = (contents + verneed_size
9405
246
          - sizeof (Elf_External_Verneed));
9406
246
      everneed = (Elf_External_Verneed *) contents;
9407
246
      iverneed = elf_tdata (abfd)->verref;
9408
270
      for (i = 0; i < verneed_count; i++, iverneed++)
9409
246
  {
9410
246
    Elf_External_Vernaux *evernaux;
9411
246
    Elf_Internal_Vernaux *ivernaux;
9412
246
    unsigned int j;
9413
9414
246
    _bfd_elf_swap_verneed_in (abfd, everneed, iverneed);
9415
9416
246
    iverneed->vn_bfd = abfd;
9417
9418
246
    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
244
    else if (hdr == NULL)
9427
0
      goto error_return_bad_verref;
9428
244
    else
9429
244
      iverneed->vn_filename
9430
244
        = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
9431
244
             iverneed->vn_file);
9432
246
    if (iverneed->vn_filename == NULL)
9433
80
      goto error_return_bad_verref;
9434
9435
166
    if (iverneed->vn_cnt == 0)
9436
58
      iverneed->vn_auxptr = NULL;
9437
108
    else
9438
108
      {
9439
108
        if (_bfd_mul_overflow (iverneed->vn_cnt,
9440
108
             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
108
        iverneed->vn_auxptr = bfd_alloc (abfd, amt);
9446
108
        if (iverneed->vn_auxptr == NULL)
9447
0
    goto error_return_verref;
9448
108
      }
9449
9450
166
    if (iverneed->vn_aux
9451
166
        > (size_t) (contents_end - (bfd_byte *) everneed))
9452
21
      goto error_return_bad_verref;
9453
9454
145
    evernaux = ((Elf_External_Vernaux *)
9455
145
          ((bfd_byte *) everneed + iverneed->vn_aux));
9456
145
    ivernaux = iverneed->vn_auxptr;
9457
216
    for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++)
9458
160
      {
9459
160
        _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux);
9460
9461
160
        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
160
        else if (hdr == NULL)
9470
0
    goto error_return_bad_verref;
9471
160
        else
9472
160
    ivernaux->vna_nodename
9473
160
      = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
9474
160
                 ivernaux->vna_name);
9475
160
        if (ivernaux->vna_nodename == NULL)
9476
4
    goto error_return_bad_verref;
9477
9478
156
        if (ivernaux->vna_other > freeidx)
9479
79
    freeidx = ivernaux->vna_other;
9480
9481
156
        ivernaux->vna_nextptr = NULL;
9482
156
        if (ivernaux->vna_next == 0)
9483
75
    {
9484
75
      iverneed->vn_cnt = j + 1;
9485
75
      break;
9486
75
    }
9487
81
        if (j + 1 < iverneed->vn_cnt)
9488
67
    ivernaux->vna_nextptr = ivernaux + 1;
9489
9490
81
        if (ivernaux->vna_next
9491
81
      > (size_t) (contents_end - (bfd_byte *) evernaux))
9492
10
    goto error_return_bad_verref;
9493
9494
71
        evernaux = ((Elf_External_Vernaux *)
9495
71
        ((bfd_byte *) evernaux + ivernaux->vna_next));
9496
71
      }
9497
9498
131
    iverneed->vn_nextref = NULL;
9499
131
    if (iverneed->vn_next == 0)
9500
101
      break;
9501
30
    if (hdr != NULL && (i + 1 < hdr->sh_info))
9502
1
      iverneed->vn_nextref = iverneed + 1;
9503
9504
30
    if (iverneed->vn_next
9505
30
        > (size_t) (contents_end - (bfd_byte *) everneed))
9506
6
      goto error_return_bad_verref;
9507
9508
24
    everneed = ((Elf_External_Verneed *)
9509
24
          ((bfd_byte *) everneed + iverneed->vn_next));
9510
24
  }
9511
125
      elf_tdata (abfd)->cverrefs = i;
9512
9513
125
      if (contents != elf_tdata (abfd)->dt_verneed)
9514
124
  _bfd_munmap_temporary (contents_addr, contents_size);
9515
125
      contents = NULL;
9516
125
      contents_addr = NULL;
9517
125
    }
9518
9519
161
  if (elf_dynverdef (abfd) != 0 || elf_tdata (abfd)->dt_verdef != NULL)
9520
65
    {
9521
65
      Elf_Internal_Shdr *hdr;
9522
65
      Elf_External_Verdef *everdef;
9523
65
      Elf_Internal_Verdef *iverdef;
9524
65
      Elf_Internal_Verdef *iverdefarr;
9525
65
      Elf_Internal_Verdef iverdefmem;
9526
65
      unsigned int i;
9527
65
      unsigned int maxidx;
9528
65
      bfd_byte *contents_end_def, *contents_end_aux;
9529
65
      size_t verdef_count;
9530
65
      size_t verdef_size;
9531
9532
65
      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
65
      else
9540
65
  {
9541
65
    hdr = &elf_tdata (abfd)->dynverdef_hdr;
9542
9543
65
    if (hdr->sh_size < sizeof (Elf_External_Verdef))
9544
4
      {
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
44
      error_return_verdef:
9550
44
        elf_tdata (abfd)->verdef = NULL;
9551
44
        elf_tdata (abfd)->cverdefs = 0;
9552
44
        goto error_return;
9553
34
      }
9554
9555
61
    if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0)
9556
2
      goto error_return_verdef;
9557
59
    contents_size = hdr->sh_size;
9558
59
    contents = _bfd_mmap_temporary (abfd, contents_size,
9559
59
            &contents_addr, &contents_size);
9560
59
    if (contents == NULL)
9561
8
      goto error_return_verdef;
9562
9563
51
    BFD_ASSERT (sizeof (Elf_External_Verdef)
9564
51
          >= sizeof (Elf_External_Verdaux));
9565
9566
51
    verdef_count = hdr->sh_info;
9567
51
    verdef_size = hdr->sh_size;
9568
51
  }
9569
9570
51
      contents_end_def = (contents + verdef_size
9571
51
        - sizeof (Elf_External_Verdef));
9572
51
      contents_end_aux = (contents + verdef_size
9573
51
        - 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
51
      everdef = (Elf_External_Verdef *) contents;
9579
51
      maxidx = 0;
9580
63
      for (i = 0; i < verdef_count; ++i)
9581
55
  {
9582
55
    _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
9583
9584
55
    if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) == 0)
9585
7
      goto error_return_bad_verdef;
9586
48
    if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) > maxidx)
9587
46
      maxidx = iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION);
9588
9589
48
    if (iverdefmem.vd_next == 0)
9590
30
      break;
9591
9592
18
    if (iverdefmem.vd_next
9593
18
        > (size_t) (contents_end_def - (bfd_byte *) everdef))
9594
6
      goto error_return_bad_verdef;
9595
9596
12
    everdef = ((Elf_External_Verdef *)
9597
12
         ((bfd_byte *) everdef + iverdefmem.vd_next));
9598
12
  }
9599
9600
38
      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
38
      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
38
      if (amt == 0)
9614
0
  goto error_return_verdef;
9615
38
      elf_tdata (abfd)->verdef = bfd_zalloc (abfd, amt);
9616
38
      if (elf_tdata (abfd)->verdef == NULL)
9617
0
  goto error_return_verdef;
9618
9619
38
      elf_tdata (abfd)->cverdefs = maxidx;
9620
9621
38
      everdef = (Elf_External_Verdef *) contents;
9622
38
      iverdefarr = elf_tdata (abfd)->verdef;
9623
43
      for (i = 0; i < verdef_count; ++i)
9624
40
  {
9625
40
    Elf_External_Verdaux *everdaux;
9626
40
    Elf_Internal_Verdaux *iverdaux;
9627
40
    unsigned int j;
9628
9629
40
    _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
9630
9631
40
    if ((iverdefmem.vd_ndx & VERSYM_VERSION) == 0)
9632
0
      goto error_return_bad_verdef;
9633
9634
40
    iverdef = &iverdefarr[(iverdefmem.vd_ndx & VERSYM_VERSION) - 1];
9635
40
    memcpy (iverdef, &iverdefmem, offsetof (Elf_Internal_Verdef, vd_bfd));
9636
9637
40
    iverdef->vd_bfd = abfd;
9638
9639
40
    if (iverdef->vd_cnt == 0)
9640
27
      iverdef->vd_auxptr = NULL;
9641
13
    else
9642
13
      {
9643
13
        if (_bfd_mul_overflow (iverdef->vd_cnt,
9644
13
             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
13
        iverdef->vd_auxptr = bfd_alloc (abfd, amt);
9650
13
        if (iverdef->vd_auxptr == NULL)
9651
0
    goto error_return_verdef;
9652
13
      }
9653
9654
40
    if (iverdef->vd_aux
9655
40
        > (size_t) (contents_end_aux - (bfd_byte *) everdef))
9656
7
      goto error_return_bad_verdef;
9657
9658
33
    everdaux = ((Elf_External_Verdaux *)
9659
33
          ((bfd_byte *) everdef + iverdef->vd_aux));
9660
33
    iverdaux = iverdef->vd_auxptr;
9661
33
    for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++)
9662
10
      {
9663
10
        _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux);
9664
9665
10
        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
10
        else
9674
10
    iverdaux->vda_nodename
9675
10
      = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
9676
10
                 iverdaux->vda_name);
9677
10
        if (iverdaux->vda_nodename == NULL)
9678
6
    goto error_return_bad_verdef;
9679
9680
4
        iverdaux->vda_nextptr = NULL;
9681
4
        if (iverdaux->vda_next == 0)
9682
0
    {
9683
0
      iverdef->vd_cnt = j + 1;
9684
0
      break;
9685
0
    }
9686
4
        if (j + 1 < iverdef->vd_cnt)
9687
2
    iverdaux->vda_nextptr = iverdaux + 1;
9688
9689
4
        if (iverdaux->vda_next
9690
4
      > (size_t) (contents_end_aux - (bfd_byte *) everdaux))
9691
4
    goto error_return_bad_verdef;
9692
9693
0
        everdaux = ((Elf_External_Verdaux *)
9694
0
        ((bfd_byte *) everdaux + iverdaux->vda_next));
9695
0
      }
9696
9697
23
    iverdef->vd_nodename = NULL;
9698
23
    if (iverdef->vd_cnt)
9699
0
      iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename;
9700
9701
23
    iverdef->vd_nextdef = NULL;
9702
23
    if (iverdef->vd_next == 0)
9703
18
      break;
9704
5
    if ((size_t) (iverdef - iverdefarr) + 1 < maxidx)
9705
1
      iverdef->vd_nextdef = iverdef + 1;
9706
9707
5
    everdef = ((Elf_External_Verdef *)
9708
5
         ((bfd_byte *) everdef + iverdef->vd_next));
9709
5
  }
9710
9711
21
      if (contents != elf_tdata (abfd)->dt_verdef)
9712
21
  _bfd_munmap_temporary (contents_addr, contents_size);
9713
21
      contents = NULL;
9714
21
      contents_addr = NULL;
9715
21
    }
9716
96
  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
117
  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
117
  return true;
9765
9766
198
 error_return:
9767
198
  if (contents != elf_tdata (abfd)->dt_verneed
9768
150
      && contents != elf_tdata (abfd)->dt_verdef)
9769
150
    _bfd_munmap_temporary (contents_addr, contents_size);
9770
198
  return false;
9771
117
}
9772

9773
asymbol *
9774
_bfd_elf_make_empty_symbol (bfd *abfd)
9775
1.77M
{
9776
1.77M
  elf_symbol_type *newsym;
9777
9778
1.77M
  newsym = bfd_zalloc (abfd, sizeof (*newsym));
9779
1.77M
  if (!newsym)
9780
0
    return NULL;
9781
1.77M
  newsym->symbol.the_bfd = abfd;
9782
1.77M
  return &newsym->symbol;
9783
1.77M
}
9784
9785
void
9786
_bfd_elf_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED,
9787
        asymbol *symbol,
9788
        symbol_info *ret)
9789
34.8k
{
9790
34.8k
  bfd_symbol_info (symbol, ret);
9791
34.8k
}
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
24.3k
{
9801
  /* Normal local symbols start with ``.L''.  */
9802
24.3k
  if (name[0] == '.' && name[1] == 'L')
9803
6.52k
    return true;
9804
9805
  /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
9806
     DWARF debugging symbols starting with ``..''.  */
9807
17.7k
  if (name[0] == '.' && name[1] == '.')
9808
576
    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.2k
  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.2k
  if (name[0] == 'L' && ISDIGIT (name[1]))
9830
65
    {
9831
65
      bool ret = false;
9832
65
      const char * p;
9833
65
      char c;
9834
9835
79
      for (p = name + 2; (c = *p); p++)
9836
79
  {
9837
79
    if (c == 1 || c == 2)
9838
1
      {
9839
1
        if (c == 1 && p == name + 2)
9840
    /* A fake symbol.  */
9841
1
    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
78
    if (! ISDIGIT (c))
9852
64
      {
9853
64
        ret = false;
9854
64
        break;
9855
64
      }
9856
78
  }
9857
64
      return ret;
9858
65
    }
9859
9860
17.1k
  return false;
9861
17.2k
}
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
989
{
9876
  /* If this isn't the right architecture for this backend, and this
9877
     isn't the generic backend, fail.  */
9878
989
  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
989
  return bfd_default_set_arch_mach (abfd, arch, machine);
9884
989
}
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
29.2k
{
9899
29.2k
  return _bfd_elf_find_nearest_line_with_alt (abfd, NULL, symbols, section,
9900
29.2k
                offset, filename_ptr,
9901
29.2k
                functionname_ptr, line_ptr,
9902
29.2k
                discriminator_ptr);
9903
29.2k
}
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
29.2k
{
9920
29.2k
  bool found;
9921
9922
29.2k
  if (_bfd_dwarf2_find_nearest_line_with_alt (abfd, alt_filename, symbols, NULL,
9923
29.2k
                section, offset, filename_ptr,
9924
29.2k
                functionname_ptr, line_ptr,
9925
29.2k
                discriminator_ptr,
9926
29.2k
                dwarf_debug_sections,
9927
29.2k
                &elf_tdata (abfd)->dwarf2_find_line_info))
9928
8.61k
    return true;
9929
9930
20.5k
  if (_bfd_dwarf1_find_nearest_line (abfd, symbols, section, offset,
9931
20.5k
             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
20.5k
  if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
9941
20.5k
               &found, filename_ptr,
9942
20.5k
               functionname_ptr, line_ptr,
9943
20.5k
               &elf_tdata (abfd)->line_info))
9944
0
    return false;
9945
20.5k
  if (found && (*functionname_ptr || *line_ptr))
9946
0
    return true;
9947
9948
20.5k
  if (symbols == NULL)
9949
5.69k
    return false;
9950
9951
14.9k
  if (! _bfd_elf_find_function (abfd, symbols, section, offset,
9952
14.9k
        filename_ptr, functionname_ptr))
9953
11.6k
    return false;
9954
9955
3.23k
  *line_ptr = 0;
9956
3.23k
  return true;
9957
14.9k
}
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.31k
{
9965
5.31k
  struct elf_obj_tdata *tdata = elf_tdata (abfd);
9966
5.31k
  return _bfd_dwarf2_find_nearest_line (abfd, symbols, symbol, NULL, 0,
9967
5.31k
          filename_ptr, NULL, line_ptr, NULL,
9968
5.31k
          dwarf_debug_sections,
9969
5.31k
          &tdata->dwarf2_find_line_info);
9970
5.31k
}
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.42k
{
10026
1.42k
  Elf_Internal_Shdr *hdr;
10027
10028
1.42k
  if (! abfd->output_has_begun
10029
193
      && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
10030
15
    return false;
10031
10032
1.40k
  if (!count)
10033
0
    return true;
10034
10035
1.40k
  hdr = &elf_section_data (section)->this_hdr;
10036
1.40k
  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.40k
  return _bfd_generic_set_section_contents (abfd, section,
10073
1.40k
              location, offset, count);
10074
1.40k
}
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
7.49M
{
10184
7.49M
  struct elf_obj_tdata *tdata;
10185
10186
7.49M
  if ((bfd_get_format (abfd) == bfd_object
10187
7.46M
       || bfd_get_format (abfd) == bfd_core)
10188
42.1k
      && (tdata = elf_tdata (abfd)) != NULL)
10189
42.1k
    {
10190
42.1k
      if (tdata->o != NULL && elf_shstrtab (abfd) != NULL)
10191
515
  _bfd_elf_strtab_free (elf_shstrtab (abfd));
10192
42.1k
      _bfd_dwarf2_cleanup_debug_info (abfd, &tdata->dwarf2_find_line_info);
10193
42.1k
      _bfd_dwarf1_cleanup_debug_info (abfd, &tdata->dwarf1_find_line_info);
10194
42.1k
      _bfd_stab_cleanup (abfd, &tdata->line_info);
10195
42.1k
      _bfd_elf_cleanup_object_attributes (abfd);
10196
522k
      for (asection *sec = abfd->sections; sec != NULL; sec = sec->next)
10197
480k
  {
10198
480k
    _bfd_elf_munmap_section_contents (sec, sec->contents);
10199
480k
    if (!sec->alloced)
10200
479k
      {
10201
479k
        free (elf_section_data (sec)->this_hdr.contents);
10202
479k
        elf_section_data (sec)->this_hdr.contents = NULL;
10203
479k
      }
10204
480k
    free (elf_section_data (sec)->relocs);
10205
480k
    elf_section_data (sec)->relocs = NULL;
10206
480k
    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
480k
    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
480k
  }
10217
42.1k
      free (tdata->symtab_hdr.contents);
10218
42.1k
      tdata->symtab_hdr.contents = NULL;
10219
42.1k
    }
10220
10221
7.49M
  return _bfd_generic_bfd_free_cached_info (abfd);
10222
7.49M
}
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
24
{
10236
24
  return bfd_reloc_ok;
10237
24
}
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
220
{
10254
220
  int pid;
10255
10256
220
  pid = elf_tdata (abfd)->core->lwpid;
10257
220
  if (pid == 0)
10258
220
    pid = elf_tdata (abfd)->core->pid;
10259
10260
220
  return pid;
10261
220
}
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
220
{
10270
220
  asection *sect2;
10271
10272
220
  if (bfd_get_section_by_name (abfd, name) != NULL)
10273
132
    return true;
10274
10275
88
  sect2 = bfd_make_section_with_flags (abfd, name, sect->flags);
10276
88
  if (sect2 == NULL)
10277
0
    return false;
10278
10279
88
  sect2->size = sect->size;
10280
88
  sect2->filepos = sect->filepos;
10281
88
  sect2->alignment_power = sect->alignment_power;
10282
88
  return true;
10283
88
}
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
220
{
10298
220
  char buf[100];
10299
220
  char *threaded_name;
10300
220
  size_t len;
10301
220
  asection *sect;
10302
10303
  /* Build the section name.  */
10304
10305
220
  sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd));
10306
220
  len = strlen (buf) + 1;
10307
220
  threaded_name = bfd_alloc (abfd, len);
10308
220
  if (threaded_name == NULL)
10309
0
    return false;
10310
220
  memcpy (threaded_name, buf, len);
10311
10312
220
  sect = bfd_make_section_anyway_with_flags (abfd, threaded_name,
10313
220
               SEC_HAS_CONTENTS);
10314
220
  if (sect == NULL)
10315
0
    return false;
10316
220
  sect->size = size;
10317
220
  sect->filepos = filepos;
10318
220
  sect->alignment_power = 2;
10319
10320
220
  return elfcore_maybe_make_sect (abfd, name, sect);
10321
220
}
10322
10323
static bool
10324
elfcore_make_auxv_note_section (bfd *abfd, Elf_Internal_Note *note,
10325
        size_t offs)
10326
57
{
10327
57
  asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv",
10328
57
                   SEC_HAS_CONTENTS);
10329
10330
57
  if (sect == NULL)
10331
0
    return false;
10332
10333
57
  sect->size = note->descsz - offs;
10334
57
  sect->filepos = note->descpos + offs;
10335
57
  sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
10336
10337
57
  return true;
10338
57
}
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
480
{
10351
480
  size_t size;
10352
480
  int offset;
10353
10354
480
  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
480
#if defined (HAVE_PRSTATUS32_T)
10382
480
  else if (note->descsz == sizeof (prstatus32_t))
10383
0
    {
10384
      /* 64-bit host, 32-bit corefile */
10385
0
      prstatus32_t prstat;
10386
10387
0
      size = sizeof (prstat.pr_reg);
10388
0
      offset   = offsetof (prstatus32_t, pr_reg);
10389
0
      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
0
      if (elf_tdata (abfd)->core->signal == 0)
10394
0
  elf_tdata (abfd)->core->signal = prstat.pr_cursig;
10395
0
      if (elf_tdata (abfd)->core->pid == 0)
10396
0
  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
0
      elf_tdata (abfd)->core->lwpid = prstat.pr_pid;
10408
0
#endif
10409
0
    }
10410
480
#endif /* HAVE_PRSTATUS32_T */
10411
480
  else
10412
480
    {
10413
      /* Fail - we don't know how to handle any other
10414
   note size (ie. data object type).  */
10415
480
      return true;
10416
480
    }
10417
10418
  /* Make a ".reg/999" section and a ".reg" section.  */
10419
0
  return _bfd_elfcore_make_pseudosection (abfd, NOTE_PSEUDO_SECTION_REG,
10420
0
            size, note->descpos + offset);
10421
480
}
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
220
{
10430
220
  return _bfd_elfcore_make_pseudosection (abfd, name,
10431
220
            note->descsz, note->descpos);
10432
220
}
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
220
{
10441
220
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_REG2, note);
10442
220
}
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
26
{
10814
26
  char *dups;
10815
26
  char *end = (char *) memchr (start, '\0', max);
10816
26
  size_t len;
10817
10818
26
  if (end == NULL)
10819
7
    len = max;
10820
19
  else
10821
19
    len = end - start;
10822
10823
26
  dups = bfd_alloc (abfd, len + 1);
10824
26
  if (dups == NULL)
10825
0
    return NULL;
10826
10827
26
  memcpy (dups, start, len);
10828
26
  dups[len] = '\0';
10829
10830
26
  return dups;
10831
26
}
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
297
{
10837
297
  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
297
#if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
10855
297
  else if (note->descsz == sizeof (elfcore_psinfo32_t))
10856
13
    {
10857
      /* 64-bit host, 32-bit corefile */
10858
13
      elfcore_psinfo32_t psinfo;
10859
10860
13
      memcpy (&psinfo, note->descdata, sizeof (psinfo));
10861
10862
13
#if defined (HAVE_PSINFO32_T_PR_PID) || defined (HAVE_PRPSINFO32_T_PR_PID)
10863
13
      elf_tdata (abfd)->core->pid = psinfo.pr_pid;
10864
13
#endif
10865
13
      elf_tdata (abfd)->core->program
10866
13
  = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
10867
13
        sizeof (psinfo.pr_fname));
10868
10869
13
      elf_tdata (abfd)->core->command
10870
13
  = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
10871
13
        sizeof (psinfo.pr_psargs));
10872
13
    }
10873
284
#endif
10874
10875
284
  else
10876
284
    {
10877
      /* Fail - we don't know how to handle any other
10878
   note size (ie. data object type).  */
10879
284
      return true;
10880
284
    }
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
13
  {
10887
13
    char *command = elf_tdata (abfd)->core->command;
10888
13
    int n = strlen (command);
10889
10890
13
    if (0 < n && command[n - 1] == ' ')
10891
3
      command[n - 1] = '\0';
10892
13
  }
10893
10894
13
  return true;
10895
297
}
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
64
{
11027
64
  char buf[30];
11028
64
  char *name;
11029
64
  size_t len;
11030
64
  unsigned int name_size;
11031
64
  asection *sect;
11032
64
  unsigned int type;
11033
64
  int is_active_thread;
11034
64
  bfd_vma base_addr;
11035
11036
64
  if (note->descsz < 4)
11037
11
    return true;
11038
11039
53
  if (! startswith (note->namedata, "win32"))
11040
53
    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.61k
{
11161
4.61k
  elf_backend_data *bed = get_elf_backend_data (abfd);
11162
11163
  /* Short cut for LINUX notes.  */
11164
4.61k
  if (note->namesz == 6 /* strlen (NOTE_NAME_LINUX) + 1 */
11165
122
      && 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.61k
  switch (note->type)
11225
4.61k
    {
11226
3.39k
    default:
11227
3.39k
      return true;
11228
11229
480
    case NT_PRSTATUS:
11230
480
      if (bed->elf_backend_grok_prstatus)
11231
303
  if ((*bed->elf_backend_grok_prstatus) (abfd, note))
11232
0
    return true;
11233
480
#if defined (HAVE_PRSTATUS_T)
11234
480
      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
213
    case NT_FPREGSET:   /* FIXME: rename to NT_PRFPREG.  */
11250
213
      return elfcore_grok_prfpreg (abfd, note);
11251
11252
64
    case NT_WIN32PSTATUS:
11253
64
      return elfcore_grok_win32pstatus (abfd, note);
11254
11255
33
    case NT_GDB_TDESC:
11256
33
      if (note->namesz == 4 && streq (note->namedata, NOTE_NAME_GDB))
11257
0
  return elfcore_grok_gdb_tdesc (abfd, note);
11258
33
      else
11259
33
  return true;
11260
11261
78
    case NT_RISCV_CSR:
11262
78
      if (note->namesz == 4 && streq (note->namedata, NOTE_NAME_GDB))
11263
0
  return elfcore_grok_riscv_csr (abfd, note);
11264
78
      else
11265
78
  return true;
11266
11267
273
    case NT_PRPSINFO:
11268
297
    case NT_PSINFO:
11269
297
      if (bed->elf_backend_grok_psinfo)
11270
179
  if ((*bed->elf_backend_grok_psinfo) (abfd, note))
11271
0
    return true;
11272
297
#if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
11273
297
      return elfcore_grok_psinfo (abfd, note);
11274
#else
11275
      return true;
11276
#endif
11277
11278
57
    case NT_AUXV:
11279
57
      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.61k
    }
11289
4.61k
}
11290
11291
static bool
11292
elfobj_grok_gnu_build_id (bfd *abfd, Elf_Internal_Note *note)
11293
438
{
11294
438
  struct bfd_build_id* build_id;
11295
11296
438
  if (note->descsz == 0)
11297
48
    return false;
11298
11299
390
  build_id = bfd_alloc (abfd, sizeof (struct bfd_build_id) - 1 + note->descsz);
11300
390
  if (build_id == NULL)
11301
0
    return false;
11302
11303
390
  build_id->size = note->descsz;
11304
390
  memcpy (build_id->data, note->descdata, note->descsz);
11305
390
  abfd->build_id = build_id;
11306
11307
390
  return true;
11308
390
}
11309
11310
static bool
11311
elfobj_grok_gnu_note (bfd *abfd, Elf_Internal_Note *note)
11312
1.73k
{
11313
1.73k
  switch (note->type)
11314
1.73k
    {
11315
456
    default:
11316
456
      return true;
11317
11318
845
    case NT_GNU_PROPERTY_TYPE_0:
11319
845
      return _bfd_elf_parse_gnu_properties (abfd, note);
11320
11321
438
    case NT_GNU_BUILD_ID:
11322
438
      return elfobj_grok_gnu_build_id (abfd, note);
11323
1.73k
    }
11324
1.73k
}
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
5
{
11417
5
  size_t offset;
11418
5
  size_t size;
11419
5
  size_t min_size;
11420
11421
  /* Compute offset of pr_getregsz, skipping over pr_statussz.
11422
     Also compute minimum size of this note.  */
11423
5
  switch (elf_elfheader (abfd)->e_ident[EI_CLASS])
11424
5
    {
11425
0
    case ELFCLASS32:
11426
0
      offset = 4 + 4;
11427
0
      min_size = offset + (4 * 2) + 4 + 4 + 4;
11428
0
      break;
11429
11430
5
    case ELFCLASS64:
11431
5
      offset = 4 + 4 + 8; /* Includes padding before pr_statussz.  */
11432
5
      min_size = offset + (8 * 2) + 4 + 4 + 4 + 4;
11433
5
      break;
11434
11435
0
    default:
11436
0
      return false;
11437
5
    }
11438
11439
5
  if (note->descsz < min_size)
11440
5
    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
29
{
11489
29
  elf_backend_data *bed = get_elf_backend_data (abfd);
11490
11491
29
  switch (note->type)
11492
29
    {
11493
5
    case NT_PRSTATUS:
11494
5
      if (bed->elf_backend_grok_freebsd_prstatus)
11495
0
  if ((*bed->elf_backend_grok_freebsd_prstatus) (abfd, note))
11496
0
    return true;
11497
5
      return elfcore_grok_freebsd_prstatus (abfd, note);
11498
11499
7
    case NT_FPREGSET:
11500
7
      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
16
    default:
11540
16
      return true;
11541
29
    }
11542
29
}
11543
11544
static bool
11545
elfcore_netbsd_get_lwpid (Elf_Internal_Note *note, int *lwpidp)
11546
15
{
11547
15
  char *cp;
11548
11549
15
  cp = strchr (note->namedata, '@');
11550
15
  if (cp != NULL)
11551
0
    {
11552
0
      *lwpidp = atoi(cp + 1);
11553
0
      return true;
11554
0
    }
11555
15
  return false;
11556
15
}
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
15
{
11583
15
  int lwp;
11584
11585
15
  if (elfcore_netbsd_get_lwpid (note, &lwp))
11586
0
    elf_tdata (abfd)->core->lwpid = lwp;
11587
11588
15
  switch (note->type)
11589
15
    {
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
15
    default:
11604
15
      break;
11605
15
    }
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
15
  if (note->type < NT_NETBSDCORE_FIRSTMACH)
11613
0
    return true;
11614
11615
11616
15
  switch (bfd_get_arch (abfd))
11617
15
    {
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
15
    default:
11657
15
      switch (note->type)
11658
15
  {
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
15
  default:
11666
15
    return true;
11667
15
  }
11668
15
    }
11669
    /* NOTREACHED */
11670
15
}
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
40
{
11785
40
  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
40
  switch ((int) note->type)
11798
40
    {
11799
23
    case SOLARIS_NT_PRSTATUS:
11800
23
      switch (note->descsz)
11801
23
  {
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
23
  default:
11815
23
    return true;
11816
23
  }
11817
11818
0
    case SOLARIS_NT_PSINFO:
11819
2
    case SOLARIS_NT_PRPSINFO:
11820
2
      switch (note->descsz)
11821
2
  {
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
2
  default:
11831
2
    return true;
11832
2
  }
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
15
    default:
11861
15
      break;
11862
40
    }
11863
11864
15
  return true;
11865
40
}
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
40
{
11876
40
  if (!elfcore_grok_solaris_note_impl (abfd, note))
11877
0
    return false;
11878
11879
40
  return elfcore_grok_note (abfd, note);
11880
40
}
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
86
{
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
86
  static long tid = 1;
12011
12012
86
  switch (note->type)
12013
86
    {
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
86
    default:
12023
86
      return true;
12024
86
    }
12025
86
}
12026
12027
static bool
12028
elfcore_grok_spu_note (bfd *abfd, Elf_Internal_Note *note)
12029
18
{
12030
18
  char *name;
12031
18
  asection *sect;
12032
18
  size_t len;
12033
12034
  /* Use note name as section name.  */
12035
18
  len = note->namesz;
12036
18
  name = bfd_alloc (abfd, len);
12037
18
  if (name == NULL)
12038
0
    return false;
12039
18
  memcpy (name, note->namedata, len);
12040
18
  name[len - 1] = '\0';
12041
12042
18
  sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
12043
18
  if (sect == NULL)
12044
0
    return false;
12045
12046
18
  sect->size    = note->descsz;
12047
18
  sect->filepos   = note->descpos;
12048
18
  sect->alignment_power = 1;
12049
12050
18
  return true;
12051
18
}
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
9.33k
{
13108
9.33k
  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
9.33k
  if (align < 4)
13115
3.48k
    align = 4;
13116
9.33k
  if (align != 4 && align != 8)
13117
2.30k
    return false;
13118
13119
7.03k
  p = buf;
13120
19.9k
  while (p < buf + size)
13121
18.8k
    {
13122
18.8k
      Elf_External_Note *xnp = (Elf_External_Note *) p;
13123
18.8k
      Elf_Internal_Note in;
13124
13125
18.8k
      if (offsetof (Elf_External_Note, name) > buf - p + size)
13126
975
  return false;
13127
13128
17.8k
      in.type = H_GET_32 (abfd, xnp->type);
13129
13130
17.8k
      in.namesz = H_GET_32 (abfd, xnp->namesz);
13131
17.8k
      in.namedata = xnp->name;
13132
17.8k
      if (in.namesz > buf - in.namedata + size)
13133
2.71k
  return false;
13134
13135
15.1k
      in.descsz = H_GET_32 (abfd, xnp->descsz);
13136
15.1k
      in.descdata = p + ELF_NOTE_DESC_OFFSET (in.namesz, align);
13137
15.1k
      in.descpos = offset + (in.descdata - buf);
13138
15.1k
      if (in.descsz != 0
13139
6.59k
    && (in.descdata >= buf + size
13140
6.33k
        || in.descsz > buf - in.descdata + size))
13141
1.61k
  return false;
13142
13143
13.5k
      switch (bfd_get_format (abfd))
13144
13.5k
  {
13145
0
  default:
13146
0
    return true;
13147
13148
5.14k
  case bfd_core:
13149
5.14k
    {
13150
41.1k
#define GROKER_ELEMENT(S,F) {S, sizeof (S) - 1, F}
13151
5.14k
      static const struct
13152
5.14k
      {
13153
5.14k
        const char * string;
13154
5.14k
        size_t len;
13155
5.14k
        bool (*func) (bfd *, Elf_Internal_Note *);
13156
5.14k
      }
13157
5.14k
      grokers[] =
13158
5.14k
      {
13159
5.14k
        GROKER_ELEMENT ("", elfcore_grok_note),
13160
5.14k
        GROKER_ELEMENT (NOTE_NAME_FREEBSD, elfcore_grok_freebsd_note),
13161
5.14k
        GROKER_ELEMENT ("NetBSD-CORE", elfcore_grok_netbsd_note),
13162
5.14k
        GROKER_ELEMENT ("OpenBSD", elfcore_grok_openbsd_note),
13163
5.14k
        GROKER_ELEMENT ("QNX", elfcore_grok_nto_note),
13164
5.14k
        GROKER_ELEMENT ("SPU/", elfcore_grok_spu_note),
13165
5.14k
        GROKER_ELEMENT ("GNU", elfobj_grok_gnu_note),
13166
5.14k
        GROKER_ELEMENT (NOTE_NAME_CORE, elfcore_grok_solaris_note)
13167
5.14k
      };
13168
5.14k
#undef GROKER_ELEMENT
13169
5.14k
      int i;
13170
13171
38.0k
      for (i = ARRAY_SIZE (grokers); i--;)
13172
38.0k
        {
13173
38.0k
    if (in.namesz >= grokers[i].len
13174
14.8k
        && strncmp (in.namedata, grokers[i].string,
13175
14.8k
        grokers[i].len) == 0)
13176
5.14k
      {
13177
5.14k
        if (! grokers[i].func (abfd, & in))
13178
219
          return false;
13179
4.92k
        break;
13180
5.14k
      }
13181
38.0k
        }
13182
4.92k
      break;
13183
5.14k
    }
13184
13185
8.39k
  case bfd_object:
13186
8.39k
    if (in.namesz == sizeof "GNU" && strcmp (in.namedata, "GNU") == 0)
13187
1.35k
      {
13188
1.35k
        if (! elfobj_grok_gnu_note (abfd, &in))
13189
424
    return false;
13190
1.35k
      }
13191
7.04k
    else if (in.namesz == sizeof "stapsdt"
13192
435
       && strcmp (in.namedata, "stapsdt") == 0)
13193
0
      {
13194
0
        if (! elfobj_grok_stapsdt_note (abfd, &in))
13195
0
    return false;
13196
0
      }
13197
7.97k
    break;
13198
13.5k
  }
13199
13200
12.9k
      p += ELF_NOTE_NEXT_OFFSET (in.namesz, in.descsz, align);
13201
12.9k
    }
13202
13203
1.08k
  return true;
13204
7.03k
}
13205
13206
bool
13207
_bfd_elf_read_notes (bfd *abfd, file_ptr offset, bfd_size_type size,
13208
         size_t align)
13209
5.83k
{
13210
5.83k
  char *buf;
13211
13212
5.83k
  if (size == 0 || (size + 1) == 0)
13213
927
    return true;
13214
13215
4.90k
  if (bfd_seek (abfd, offset, SEEK_SET) != 0)
13216
1.16k
    return false;
13217
13218
3.74k
  buf = _bfd_malloc_and_read (abfd, size + 1, size);
13219
3.74k
  if (buf == NULL)
13220
1.57k
    return false;
13221
13222
  /* PR 17512: file: ec08f814
13223
     0-termintate the buffer so that string searches will not overflow.  */
13224
2.17k
  buf[size] = 0;
13225
13226
2.17k
  if (!elf_parse_notes (abfd, buf, size, offset, align))
13227
1.98k
    {
13228
1.98k
      free (buf);
13229
1.98k
      return false;
13230
1.98k
    }
13231
13232
185
  free (buf);
13233
185
  return true;
13234
2.17k
}
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
14.8k
{
13390
14.8k
  elf_backend_data *bed = get_elf_backend_data (abfd);
13391
14.8k
  asection *relplt;
13392
14.8k
  asymbol *s;
13393
14.8k
  const char *relplt_name;
13394
14.8k
  bool (*slurp_relocs) (bfd *, asection *, asymbol **, bool);
13395
14.8k
  arelent *p;
13396
14.8k
  long count, i, n;
13397
14.8k
  size_t size;
13398
14.8k
  Elf_Internal_Shdr *hdr;
13399
14.8k
  char *names;
13400
14.8k
  asection *plt;
13401
13402
14.8k
  *ret = NULL;
13403
13404
14.8k
  if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0)
13405
14.8k
    return 0;
13406
13407
58
  if (dynsymcount <= 0)
13408
53
    return 0;
13409
13410
5
  if (!bed->plt_sym_val)
13411
3
    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
484
{
13502
484
  Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd);
13503
484
  elf_backend_data *bed = get_elf_backend_data (abfd);
13504
13505
484
  if (bed->osabi_exact && i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE)
13506
2
    i_ehdrp->e_ident[EI_OSABI] = bed->elf_osabi;
13507
13508
484
  if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_SOLARIS
13509
473
      || bed->target_os == is_solaris)
13510
11
    {
13511
11
      elf_tdata (abfd)->strtab_hdr.sh_flags = SHF_STRINGS;
13512
11
      elf_tdata (abfd)->shstrtab_hdr.sh_flags = SHF_STRINGS;
13513
11
    }
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
484
  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
484
  return true;
13542
484
}
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
776k
{
13564
776k
  bfd_size_type size;
13565
776k
  elf_symbol_type * elf_sym = (elf_symbol_type *) sym;
13566
13567
776k
  if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
13568
776k
         | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0
13569
580k
      || sym->section != sec)
13570
763k
    return 0;
13571
13572
12.9k
  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
12.9k
  if (size == 0
13581
2.35k
      && ((sym->flags & (BSF_SYNTHETIC | BSF_LOCAL)) == BSF_LOCAL)
13582
1.50k
      && ELF_ST_TYPE (elf_sym->internal_elf_sym.st_info) == STT_NOTYPE
13583
1.35k
      && ELF_ST_VISIBILITY (elf_sym->internal_elf_sym.st_other) == STV_HIDDEN)
13584
101
    return 0;
13585
13586
12.8k
  *code_off = sym->value;
13587
  /* Do not return 0 for the function's size.  */
13588
12.8k
  return size ? size : 1;
13589
12.9k
}
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.79k
#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.96k
{
13606
  /* We only support RELA secondary relocs.  */
13607
1.96k
  if (hdr->sh_type != SHT_RELA)
13608
255
    return false;
13609
13610
#if DEBUG_SECONDARY_RELOCS
13611
  fprintf (stderr, "secondary reloc section %s encountered\n", name);
13612
#endif
13613
1.71k
  hdr->sh_type = SHT_SECONDARY_RELOC;
13614
1.71k
  return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
13615
1.96k
}
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
10.2k
{
13625
10.2k
  elf_backend_data *ebd = get_elf_backend_data (abfd);
13626
10.2k
  asection * relsec;
13627
10.2k
  bool result = true;
13628
10.2k
  bfd_vma (*r_sym) (bfd_vma);
13629
10.2k
  ufile_ptr filesize;
13630
13631
10.2k
#if BFD_DEFAULT_TARGET_SIZE > 32
13632
10.2k
  if (bfd_arch_bits_per_address (abfd) != 32)
13633
5.38k
    r_sym = elf64_r_sym;
13634
4.81k
  else
13635
4.81k
#endif
13636
4.81k
    r_sym = elf32_r_sym;
13637
13638
10.2k
  if (!elf_section_data (sec)->has_secondary_relocs)
13639
10.1k
    return true;
13640
13641
  /* Discover if there are any secondary reloc sections
13642
     associated with SEC.  */
13643
97
  filesize = bfd_get_file_size (abfd);
13644
2.11k
  for (relsec = abfd->sections; relsec != NULL; relsec = relsec->next)
13645
2.01k
    {
13646
2.01k
      Elf_Internal_Shdr * hdr = & elf_section_data (relsec)->this_hdr;
13647
13648
2.01k
      if (hdr->sh_type == SHT_SECONDARY_RELOC
13649
118
    && hdr->sh_info == (unsigned) elf_section_data (sec)->this_idx
13650
97
    && (hdr->sh_entsize == ebd->s->sizeof_rel
13651
97
        || hdr->sh_entsize == ebd->s->sizeof_rela))
13652
97
  {
13653
97
    bfd_byte * native_relocs;
13654
97
    bfd_byte * native_reloc;
13655
97
    arelent * internal_relocs;
13656
97
    arelent * internal_reloc;
13657
97
    size_t i;
13658
97
    unsigned int entsize;
13659
97
    unsigned int symcount;
13660
97
    bfd_size_type reloc_count;
13661
97
    size_t amt;
13662
13663
97
    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
97
    entsize = hdr->sh_entsize;
13671
13672
97
    if (filesize != 0
13673
97
        && ((ufile_ptr) hdr->sh_offset > filesize
13674
84
      || hdr->sh_size > filesize - hdr->sh_offset))
13675
23
      {
13676
23
        bfd_set_error (bfd_error_file_truncated);
13677
23
        result = false;
13678
23
        continue;
13679
23
      }
13680
13681
74
    native_relocs = bfd_malloc (hdr->sh_size);
13682
74
    if (native_relocs == NULL)
13683
0
      {
13684
0
        result = false;
13685
0
        continue;
13686
0
      }
13687
13688
74
    reloc_count = NUM_SHDR_ENTRIES (hdr);
13689
74
    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
74
    internal_relocs = bfd_alloc (abfd, amt);
13698
74
    if (internal_relocs == NULL)
13699
0
      {
13700
0
        free (native_relocs);
13701
0
        result = false;
13702
0
        continue;
13703
0
      }
13704
13705
74
    if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
13706
74
        || 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
74
    if (dynamic)
13716
0
      symcount = bfd_get_dynamic_symcount (abfd);
13717
74
    else
13718
74
      symcount = bfd_get_symcount (abfd);
13719
13720
74
    for (i = 0, internal_reloc = internal_relocs,
13721
74
     native_reloc = native_relocs;
13722
577
         i < reloc_count;
13723
503
         i++, internal_reloc++, native_reloc += entsize)
13724
503
      {
13725
503
        bool res;
13726
503
        Elf_Internal_Rela rela;
13727
13728
503
        if (entsize == ebd->s->sizeof_rel)
13729
0
    ebd->s->swap_reloc_in (abfd, native_reloc, & rela);
13730
503
        else /* entsize == ebd->s->sizeof_rela */
13731
503
    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
503
        if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0)
13738
421
    internal_reloc->address = rela.r_offset;
13739
82
        else
13740
82
    internal_reloc->address = rela.r_offset - sec->vma;
13741
13742
503
        if (r_sym (rela.r_info) == STN_UNDEF)
13743
128
    {
13744
      /* FIXME: This and the error case below mean that we
13745
         have a symbol on relocs that is not elf_symbol_type.  */
13746
128
      internal_reloc->sym_ptr_ptr = &bfd_abs_section_ptr->symbol;
13747
128
    }
13748
375
        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
375
        else
13759
375
    {
13760
375
      asymbol **ps;
13761
13762
375
      ps = symbols + r_sym (rela.r_info) - 1;
13763
375
      internal_reloc->sym_ptr_ptr = ps;
13764
      /* Make sure that this symbol is not removed by strip.  */
13765
375
      (*ps)->flags |= BSF_KEEP;
13766
375
    }
13767
13768
503
        internal_reloc->addend = rela.r_addend;
13769
13770
503
        res = ebd->elf_info_to_howto (abfd, internal_reloc, & rela);
13771
503
        if (! res || internal_reloc->howto == NULL)
13772
34
    {
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
34
      result = false;
13779
34
    }
13780
503
      }
13781
13782
74
    free (native_relocs);
13783
    /* Store the internal relocs.  */
13784
74
    relsec->sec_info = internal_relocs;
13785
74
  }
13786
2.01k
    }
13787
13788
97
  return result;
13789
97
}
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
48
{
13799
48
  asection * isec;
13800
48
  asection * osec;
13801
48
  struct bfd_elf_section_data * esd;
13802
13803
48
  if (isection == NULL)
13804
0
    return false;
13805
13806
48
  if (isection->sh_type != SHT_SECONDARY_RELOC)
13807
48
    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.09k
{
14096
9.09k
#ifdef USE_MMAP
14097
9.09k
  elf_backend_data *bed = get_elf_backend_data (abfd);
14098
9.09k
  if (bed->use_mmap
14099
3.37k
      && sec->compress_status == COMPRESS_SECTION_NONE
14100
3.37k
      && (sec->flags & SEC_LINKER_CREATED) == 0)
14101
3.37k
    {
14102
      /* Use mmap only if section size >= the minimum mmap section
14103
   size.  */
14104
3.37k
      size_t readsz = bfd_get_section_limit_octets (abfd, sec);
14105
3.37k
      size_t allocsz = bfd_get_section_alloc_size (abfd, sec);
14106
3.37k
      if (readsz == allocsz && readsz >= _bfd_minimum_mmap_size)
14107
821
  {
14108
821
    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
821
    if (sec->mmapped_p)
14116
0
      abort ();
14117
821
    sec->mmapped_p = 1;
14118
14119
    /* We can't use the final link preallocated buffer for mmap.  */
14120
821
    *buf = NULL;
14121
821
  }
14122
3.37k
    }
14123
9.09k
#endif
14124
  /* FIXME: We should not get here if sec->alloced is set.  */
14125
9.09k
  bool ret = bfd_get_full_section_contents (abfd, sec, buf);
14126
9.09k
  if (ret && sec->mmapped_p)
14127
0
    *buf = sec->contents;
14128
9.09k
  return ret;
14129
9.09k
}
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.09k
{
14136
9.09k
  *buf = NULL;
14137
9.09k
  return elf_mmap_section_contents (abfd, sec, buf);
14138
9.09k
}
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
488k
{
14154
  /* NB: Since _bfd_elf_munmap_section_contents is called like free,
14155
     CONTENTS may be NULL.  */
14156
488k
  if (contents == NULL)
14157
480k
    return;
14158
14159
7.99k
  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
597
      && (sec->contents == contents
14166
0
    || elf_section_data (sec)->this_hdr.contents == contents))
14167
597
    return;
14168
14169
  /* Don't leave pointers to data we are about to munmap or free.  */
14170
7.39k
  if (sec->contents == contents)
14171
0
    sec->contents = NULL;
14172
7.39k
  if (elf_section_data (sec)->this_hdr.contents == contents)
14173
0
    elf_section_data (sec)->this_hdr.contents = NULL;
14174
14175
7.39k
#ifdef USE_MMAP
14176
7.39k
  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.39k
#endif
14193
14194
7.39k
  free (contents);
14195
7.39k
}
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 */