Coverage Report

Created: 2026-04-04 08:16

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
55.4k
#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_HW_BREAK  ".reg-aarch-hw-break"
68
0
#define NOTE_PSEUDO_SECTION_AARCH_HW_WATCH  ".reg-aarch-hw-watch"
69
0
#define NOTE_PSEUDO_SECTION_AARCH_MTE   ".reg-aarch-mte"
70
0
#define NOTE_PSEUDO_SECTION_AARCH_PAUTH   ".reg-aarch-pauth"
71
0
#define NOTE_PSEUDO_SECTION_AARCH_SSVE    ".reg-aarch-ssve"
72
0
#define NOTE_PSEUDO_SECTION_AARCH_SVE   ".reg-aarch-sve"
73
0
#define NOTE_PSEUDO_SECTION_AARCH_TLS   ".reg-aarch-tls"
74
0
#define NOTE_PSEUDO_SECTION_AARCH_ZA    ".reg-aarch-za"
75
0
#define NOTE_PSEUDO_SECTION_AARCH_ZT    ".reg-aarch-zt"
76
0
#define NOTE_PSEUDO_SECTION_ARC_V2    ".reg-arc-v2"
77
0
#define NOTE_PSEUDO_SECTION_ARM_VFP   ".reg-arm-vfp"
78
0
#define NOTE_PSEUDO_SECTION_I386_TLS    ".reg-i386-tls"
79
0
#define NOTE_PSEUDO_SECTION_LOONGARCH_CPUCFG  ".reg-loongarch-cpucfg"
80
0
#define NOTE_PSEUDO_SECTION_LOONGARCH_LASX  ".reg-loongarch-lasx"
81
0
#define NOTE_PSEUDO_SECTION_LOONGARCH_LBT ".reg-loongarch-lbt"
82
0
#define NOTE_PSEUDO_SECTION_LOONGARCH_LSX ".reg-loongarch-lsx"
83
0
#define NOTE_PSEUDO_SECTION_PPC_DSCR    ".reg-ppc-dscr"
84
0
#define NOTE_PSEUDO_SECTION_PPC_EBB   ".reg-ppc-ebb"
85
0
#define NOTE_PSEUDO_SECTION_PPC_PMU   ".reg-ppc-pmu"
86
0
#define NOTE_PSEUDO_SECTION_PPC_PPR   ".reg-ppc-ppr"
87
0
#define NOTE_PSEUDO_SECTION_PPC_TAR   ".reg-ppc-tar"
88
0
#define NOTE_PSEUDO_SECTION_PPC_TM_CDSCR  ".reg-ppc-tm-cdscr"
89
0
#define NOTE_PSEUDO_SECTION_PPC_TM_CFPR   ".reg-ppc-tm-cfpr"
90
0
#define NOTE_PSEUDO_SECTION_PPC_TM_CGPR   ".reg-ppc-tm-cgpr"
91
0
#define NOTE_PSEUDO_SECTION_PPC_TM_CPPR   ".reg-ppc-tm-cppr"
92
0
#define NOTE_PSEUDO_SECTION_PPC_TM_CTAR   ".reg-ppc-tm-ctar"
93
0
#define NOTE_PSEUDO_SECTION_PPC_TM_CVMX   ".reg-ppc-tm-cvmx"
94
0
#define NOTE_PSEUDO_SECTION_PPC_TM_CVSX   ".reg-ppc-tm-cvsx"
95
0
#define NOTE_PSEUDO_SECTION_PPC_TM_SPR    ".reg-ppc-tm-spr"
96
0
#define NOTE_PSEUDO_SECTION_PPC_VSX   ".reg-ppc-vsx"
97
0
#define NOTE_PSEUDO_SECTION_PPC_VMX   ".reg-ppc-vmx"
98
9
#define NOTE_PSEUDO_SECTION_REG     ".reg"
99
39
#define NOTE_PSEUDO_SECTION_REG2    ".reg2"
100
0
#define NOTE_PSEUDO_SECTION_RISCV_CSR   ".reg-riscv-csr"
101
0
#define NOTE_PSEUDO_SECTION_S390_CTRS   ".reg-s390-ctrs"
102
0
#define NOTE_PSEUDO_SECTION_S390_GS_BC    ".reg-s390-gs-bc"
103
0
#define NOTE_PSEUDO_SECTION_S390_GS_CB    ".reg-s390-gs-cb"
104
0
#define NOTE_PSEUDO_SECTION_S390_HIGH_GPRS  ".reg-s390-high-gprs"
105
0
#define NOTE_PSEUDO_SECTION_S390_LAST_BREAK ".reg-s390-last-break"
106
0
#define NOTE_PSEUDO_SECTION_S390_PREFIX   ".reg-s390-prefix"
107
0
#define NOTE_PSEUDO_SECTION_S390_SYSTEM_CALL  ".reg-s390-system-call"
108
0
#define NOTE_PSEUDO_SECTION_S390_TDB    ".reg-s390-tdb"
109
0
#define NOTE_PSEUDO_SECTION_S390_TIMER    ".reg-s390-timer"
110
0
#define NOTE_PSEUDO_SECTION_S390_TODCMP   ".reg-s390-todcmp"
111
0
#define NOTE_PSEUDO_SECTION_S390_TODPREG  ".reg-s390-todpreg"
112
0
#define NOTE_PSEUDO_SECTION_S390_VXRS_HIGH  ".reg-s390-vxrs-high"
113
0
#define NOTE_PSEUDO_SECTION_S390_VXRS_LOW ".reg-s390-vxrs-low"
114
0
#define NOTE_PSEUDO_SECTION_SSP     ".reg-ssp"
115
0
#define NOTE_PSEUDO_SECTION_TDESC   ".gdb-tdesc"
116
0
#define NOTE_PSEUDO_SECTION_X86_SEGBASES  ".reg-x86-segbases"
117
0
#define NOTE_PSEUDO_SECTION_XFP           ".reg-xfp"
118
0
#define NOTE_PSEUDO_SECTION_XSTATE        ".reg-xstate"
119
0
#define NOTE_PSEUDO_SECTION_XSAVE_LAYOUT  ".reg-xsave-layout"
120
121
static int elf_sort_sections (const void *, const void *);
122
static bool assign_file_positions_except_relocs (bfd *, struct bfd_link_info *);
123
static bool swap_out_syms (bfd *, struct elf_strtab_hash **, int,
124
         struct bfd_link_info *);
125
static bool elf_parse_notes (bfd *abfd, char *buf, size_t size,
126
           file_ptr offset, size_t align);
127
128
/* Swap version information in and out.  The version information is
129
   currently size independent.  If that ever changes, this code will
130
   need to move into elfcode.h.  */
131
132
/* Swap in a Verdef structure.  */
133
134
void
135
_bfd_elf_swap_verdef_in (bfd *abfd,
136
       const Elf_External_Verdef *src,
137
       Elf_Internal_Verdef *dst)
138
22
{
139
22
  dst->vd_version = H_GET_16 (abfd, src->vd_version);
140
22
  dst->vd_flags   = H_GET_16 (abfd, src->vd_flags);
141
22
  dst->vd_ndx     = H_GET_16 (abfd, src->vd_ndx);
142
22
  dst->vd_cnt     = H_GET_16 (abfd, src->vd_cnt);
143
22
  dst->vd_hash    = H_GET_32 (abfd, src->vd_hash);
144
22
  dst->vd_aux     = H_GET_32 (abfd, src->vd_aux);
145
22
  dst->vd_next    = H_GET_32 (abfd, src->vd_next);
146
22
}
147
148
/* Swap out a Verdef structure.  */
149
150
void
151
_bfd_elf_swap_verdef_out (bfd *abfd,
152
        const Elf_Internal_Verdef *src,
153
        Elf_External_Verdef *dst)
154
0
{
155
0
  H_PUT_16 (abfd, src->vd_version, dst->vd_version);
156
0
  H_PUT_16 (abfd, src->vd_flags, dst->vd_flags);
157
0
  H_PUT_16 (abfd, src->vd_ndx, dst->vd_ndx);
158
0
  H_PUT_16 (abfd, src->vd_cnt, dst->vd_cnt);
159
0
  H_PUT_32 (abfd, src->vd_hash, dst->vd_hash);
160
0
  H_PUT_32 (abfd, src->vd_aux, dst->vd_aux);
161
0
  H_PUT_32 (abfd, src->vd_next, dst->vd_next);
162
0
}
163
164
/* Swap in a Verdaux structure.  */
165
166
void
167
_bfd_elf_swap_verdaux_in (bfd *abfd,
168
        const Elf_External_Verdaux *src,
169
        Elf_Internal_Verdaux *dst)
170
3
{
171
3
  dst->vda_name = H_GET_32 (abfd, src->vda_name);
172
3
  dst->vda_next = H_GET_32 (abfd, src->vda_next);
173
3
}
174
175
/* Swap out a Verdaux structure.  */
176
177
void
178
_bfd_elf_swap_verdaux_out (bfd *abfd,
179
         const Elf_Internal_Verdaux *src,
180
         Elf_External_Verdaux *dst)
181
0
{
182
0
  H_PUT_32 (abfd, src->vda_name, dst->vda_name);
183
0
  H_PUT_32 (abfd, src->vda_next, dst->vda_next);
184
0
}
185
186
/* Swap in a Verneed structure.  */
187
188
void
189
_bfd_elf_swap_verneed_in (bfd *abfd,
190
        const Elf_External_Verneed *src,
191
        Elf_Internal_Verneed *dst)
192
230
{
193
230
  dst->vn_version = H_GET_16 (abfd, src->vn_version);
194
230
  dst->vn_cnt     = H_GET_16 (abfd, src->vn_cnt);
195
230
  dst->vn_file    = H_GET_32 (abfd, src->vn_file);
196
230
  dst->vn_aux     = H_GET_32 (abfd, src->vn_aux);
197
230
  dst->vn_next    = H_GET_32 (abfd, src->vn_next);
198
230
}
199
200
/* Swap out a Verneed structure.  */
201
202
void
203
_bfd_elf_swap_verneed_out (bfd *abfd,
204
         const Elf_Internal_Verneed *src,
205
         Elf_External_Verneed *dst)
206
0
{
207
0
  H_PUT_16 (abfd, src->vn_version, dst->vn_version);
208
0
  H_PUT_16 (abfd, src->vn_cnt, dst->vn_cnt);
209
0
  H_PUT_32 (abfd, src->vn_file, dst->vn_file);
210
0
  H_PUT_32 (abfd, src->vn_aux, dst->vn_aux);
211
0
  H_PUT_32 (abfd, src->vn_next, dst->vn_next);
212
0
}
213
214
/* Swap in a Vernaux structure.  */
215
216
void
217
_bfd_elf_swap_vernaux_in (bfd *abfd,
218
        const Elf_External_Vernaux *src,
219
        Elf_Internal_Vernaux *dst)
220
283
{
221
283
  dst->vna_hash  = H_GET_32 (abfd, src->vna_hash);
222
283
  dst->vna_flags = H_GET_16 (abfd, src->vna_flags);
223
283
  dst->vna_other = H_GET_16 (abfd, src->vna_other);
224
283
  dst->vna_name  = H_GET_32 (abfd, src->vna_name);
225
283
  dst->vna_next  = H_GET_32 (abfd, src->vna_next);
226
283
}
227
228
/* Swap out a Vernaux structure.  */
229
230
void
231
_bfd_elf_swap_vernaux_out (bfd *abfd,
232
         const Elf_Internal_Vernaux *src,
233
         Elf_External_Vernaux *dst)
234
0
{
235
0
  H_PUT_32 (abfd, src->vna_hash, dst->vna_hash);
236
0
  H_PUT_16 (abfd, src->vna_flags, dst->vna_flags);
237
0
  H_PUT_16 (abfd, src->vna_other, dst->vna_other);
238
0
  H_PUT_32 (abfd, src->vna_name, dst->vna_name);
239
0
  H_PUT_32 (abfd, src->vna_next, dst->vna_next);
240
0
}
241
242
/* Swap in a Versym structure.  */
243
244
void
245
_bfd_elf_swap_versym_in (bfd *abfd,
246
       const Elf_External_Versym *src,
247
       Elf_Internal_Versym *dst)
248
1.41k
{
249
1.41k
  dst->vs_vers = H_GET_16 (abfd, src->vs_vers);
250
1.41k
}
251
252
/* Swap out a Versym structure.  */
253
254
void
255
_bfd_elf_swap_versym_out (bfd *abfd,
256
        const Elf_Internal_Versym *src,
257
        Elf_External_Versym *dst)
258
0
{
259
0
  H_PUT_16 (abfd, src->vs_vers, dst->vs_vers);
260
0
}
261
262
/* Standard ELF hash function.  Do not change this function; you will
263
   cause invalid hash tables to be generated.  */
264
265
unsigned long
266
bfd_elf_hash (const char *namearg)
267
0
{
268
0
  uint32_t h = 0;
269
270
0
  for (const unsigned char *name = (const unsigned char *) namearg;
271
0
       *name; name++)
272
0
    {
273
0
      h = (h << 4) + *name;
274
0
      h ^= (h >> 24) & 0xf0;
275
0
    }
276
0
  return h & 0x0fffffff;
277
0
}
278
279
/* DT_GNU_HASH hash function.  Do not change this function; you will
280
   cause invalid hash tables to be generated.  */
281
282
unsigned long
283
bfd_elf_gnu_hash (const char *namearg)
284
0
{
285
0
  uint32_t h = 5381;
286
287
0
  for (const unsigned char *name = (const unsigned char *) namearg;
288
0
       *name; name++)
289
0
    h = (h << 5) + h + *name;
290
0
  return h;
291
0
}
292
293
/* Create a tdata field OBJECT_SIZE bytes in length, zeroed out and with
294
   the object_id field of an elf_obj_tdata field set.  */
295
bool
296
bfd_elf_allocate_object (bfd *abfd,
297
       size_t object_size)
298
2.11M
{
299
2.11M
  BFD_ASSERT (object_size >= sizeof (struct elf_obj_tdata));
300
2.11M
  abfd->tdata.any = bfd_zalloc (abfd, object_size);
301
2.11M
  if (abfd->tdata.any == NULL)
302
0
    return false;
303
304
2.11M
  elf_object_id (abfd) = get_elf_backend_data (abfd)->target_id;
305
2.11M
  if (abfd->direction != read_direction)
306
769
    {
307
769
      struct output_elf_obj_tdata *o = bfd_zalloc (abfd, sizeof *o);
308
769
      if (o == NULL)
309
0
  return false;
310
769
      elf_tdata (abfd)->o = o;
311
769
      elf_program_header_size (abfd) = (bfd_size_type) -1;
312
769
    }
313
2.11M
  return true;
314
2.11M
}
315
316
317
bool
318
bfd_elf_make_object (bfd *abfd)
319
1.01M
{
320
1.01M
  return bfd_elf_allocate_object (abfd, sizeof (struct elf_obj_tdata));
321
1.01M
}
322
323
bool
324
bfd_elf_mkcorefile (bfd *abfd)
325
588k
{
326
  /* I think this can be done just like an object file.  */
327
588k
  if (!abfd->xvec->_bfd_set_format[(int) bfd_object] (abfd))
328
0
    return false;
329
588k
  elf_tdata (abfd)->core = bfd_zalloc (abfd, sizeof (*elf_tdata (abfd)->core));
330
588k
  return elf_tdata (abfd)->core != NULL;
331
588k
}
332
333
char *
334
bfd_elf_get_str_section (bfd *abfd, unsigned int shindex)
335
43.3k
{
336
43.3k
  Elf_Internal_Shdr **i_shdrp;
337
43.3k
  bfd_byte *shstrtab = NULL;
338
43.3k
  file_ptr offset;
339
43.3k
  bfd_size_type shstrtabsize;
340
341
43.3k
  i_shdrp = elf_elfsections (abfd);
342
43.3k
  if (i_shdrp == 0
343
43.3k
      || shindex >= elf_numsections (abfd)
344
43.3k
      || i_shdrp[shindex] == 0)
345
0
    return NULL;
346
347
43.3k
  shstrtab = i_shdrp[shindex]->contents;
348
43.3k
  if (shstrtab == NULL)
349
43.3k
    {
350
      /* No cached one, attempt to read, and cache what we read.  */
351
43.3k
      offset = i_shdrp[shindex]->sh_offset;
352
43.3k
      shstrtabsize = i_shdrp[shindex]->sh_size;
353
354
43.3k
      if (shstrtabsize == 0
355
40.9k
    || bfd_seek (abfd, offset, SEEK_SET) != 0
356
40.6k
    || (shstrtab = _bfd_mmap_persistent (abfd, shstrtabsize)) == NULL)
357
4.49k
  {
358
    /* Once we've failed to read it, make sure we don't keep
359
       trying.  Otherwise, we'll keep allocating space for
360
       the string table over and over.  */
361
4.49k
    i_shdrp[shindex]->sh_size = 0;
362
4.49k
  }
363
38.8k
      else if (shstrtab[shstrtabsize - 1] != 0)
364
9.25k
  {
365
    /* It is an error if a string table isn't terminated.  */
366
9.25k
    _bfd_error_handler
367
      /* xgettext:c-format */
368
9.25k
      (_("%pB: string table [%u] is corrupt"), abfd, shindex);
369
9.25k
    shstrtab[shstrtabsize - 1] = 0;
370
9.25k
  }
371
43.3k
      i_shdrp[shindex]->contents = shstrtab;
372
43.3k
    }
373
43.3k
  return (char *) shstrtab;
374
43.3k
}
375
376
char *
377
bfd_elf_string_from_elf_section (bfd *abfd,
378
         unsigned int shindex,
379
         unsigned int strindex)
380
966k
{
381
966k
  Elf_Internal_Shdr *hdr;
382
383
966k
  if (strindex == 0)
384
174k
    return "";
385
386
792k
  if (elf_elfsections (abfd) == NULL || shindex >= elf_numsections (abfd))
387
181
    return NULL;
388
389
791k
  hdr = elf_elfsections (abfd)[shindex];
390
391
791k
  if (hdr->contents == NULL)
392
59.6k
    {
393
59.6k
      if (hdr->sh_type != SHT_STRTAB && hdr->sh_type < SHT_LOOS)
394
16.2k
  {
395
    /* PR 17512: file: f057ec89.  */
396
    /* xgettext:c-format */
397
16.2k
    _bfd_error_handler (_("%pB: attempt to load strings from"
398
16.2k
        " a non-string section (number %d)"),
399
16.2k
            abfd, shindex);
400
16.2k
    return NULL;
401
16.2k
  }
402
403
43.3k
      if (bfd_elf_get_str_section (abfd, shindex) == NULL)
404
4.49k
  return NULL;
405
43.3k
    }
406
732k
  else
407
732k
    {
408
      /* PR 24273: The string section's contents may have already
409
   been loaded elsewhere, eg because a corrupt file has the
410
   string section index in the ELF header pointing at a group
411
   section.  So be paranoid, and test that the last byte of
412
   the section is zero.  */
413
732k
      if (hdr->sh_size == 0 || hdr->contents[hdr->sh_size - 1] != 0)
414
0
  return NULL;
415
732k
    }
416
417
771k
  if (strindex >= hdr->sh_size)
418
14.4k
    {
419
14.4k
      unsigned int shstrndx = elf_elfheader(abfd)->e_shstrndx;
420
14.4k
      _bfd_error_handler
421
  /* xgettext:c-format */
422
14.4k
  (_("%pB: invalid string offset %u >= %" PRIu64 " for section `%s'"),
423
14.4k
   abfd, strindex, (uint64_t) hdr->sh_size,
424
14.4k
   (shindex == shstrndx && strindex == hdr->sh_name
425
14.4k
    ? ".shstrtab"
426
14.4k
    : bfd_elf_string_from_elf_section (abfd, shstrndx, hdr->sh_name)));
427
14.4k
      return NULL;
428
14.4k
    }
429
430
756k
  return ((char *) hdr->contents) + strindex;
431
771k
}
432
433
/* Read and convert symbols to internal format.
434
   SYMCOUNT specifies the number of symbols to read, starting from
435
   symbol SYMOFFSET.  If any of INTSYM_BUF, EXTSYM_BUF or EXTSHNDX_BUF
436
   are non-NULL, they are used to store the internal symbols, external
437
   symbols, and symbol section index extensions, respectively.
438
   Returns a pointer to the internal symbol buffer (malloced if necessary)
439
   or NULL if there were no symbols or some kind of problem.  */
440
441
Elf_Internal_Sym *
442
bfd_elf_get_elf_syms (bfd *ibfd,
443
          Elf_Internal_Shdr *symtab_hdr,
444
          size_t symcount,
445
          size_t symoffset,
446
          Elf_Internal_Sym *intsym_buf,
447
          void *extsym_buf,
448
          Elf_External_Sym_Shndx *extshndx_buf)
449
12.8k
{
450
12.8k
  Elf_Internal_Shdr *shndx_hdr;
451
12.8k
  void *alloc_ext;
452
12.8k
  const bfd_byte *esym;
453
12.8k
  Elf_External_Sym_Shndx *alloc_extshndx;
454
12.8k
  Elf_External_Sym_Shndx *shndx;
455
12.8k
  Elf_Internal_Sym *alloc_intsym;
456
12.8k
  Elf_Internal_Sym *isym;
457
12.8k
  Elf_Internal_Sym *isymend;
458
12.8k
  elf_backend_data *bed;
459
12.8k
  size_t extsym_size;
460
12.8k
  size_t amt;
461
12.8k
  file_ptr pos;
462
463
12.8k
  if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
464
0
    abort ();
465
466
12.8k
  if (symcount == 0)
467
0
    return intsym_buf;
468
469
12.8k
  if (elf_use_dt_symtab_p (ibfd))
470
3
    {
471
      /* Use dynamic symbol table.  */
472
3
      if (elf_tdata (ibfd)->dt_symtab_count != symcount + symoffset)
473
0
  {
474
0
    bfd_set_error (bfd_error_invalid_operation);
475
0
    return NULL;
476
0
  }
477
3
      return elf_tdata (ibfd)->dt_symtab + symoffset;
478
3
    }
479
480
  /* Normal syms might have section extension entries.  */
481
12.8k
  shndx_hdr = NULL;
482
12.8k
  if (elf_symtab_shndx_list (ibfd) != NULL)
483
2.04k
    {
484
2.04k
      elf_section_list * entry;
485
2.04k
      Elf_Internal_Shdr **sections = elf_elfsections (ibfd);
486
487
      /* Find an index section that is linked to this symtab section.  */
488
3.41k
      for (entry = elf_symtab_shndx_list (ibfd); entry != NULL; entry = entry->next)
489
2.18k
  {
490
    /* PR 20063.  */
491
2.18k
    if (entry->hdr.sh_link >= elf_numsections (ibfd))
492
100
      continue;
493
494
2.08k
    if (sections[entry->hdr.sh_link] == symtab_hdr)
495
825
      {
496
825
        shndx_hdr = & entry->hdr;
497
825
        break;
498
1.26k
      };
499
1.26k
  }
500
501
2.04k
      if (shndx_hdr == NULL)
502
1.22k
  {
503
1.22k
    if (symtab_hdr == &elf_symtab_hdr (ibfd))
504
      /* Not really accurate, but this was how the old code used
505
         to work.  */
506
1.21k
      shndx_hdr = &elf_symtab_shndx_list (ibfd)->hdr;
507
    /* Otherwise we do nothing.  The assumption is that
508
       the index table will not be needed.  */
509
1.22k
  }
510
2.04k
    }
511
512
  /* Read the symbols.  */
513
12.8k
  alloc_ext = NULL;
514
12.8k
  alloc_extshndx = NULL;
515
12.8k
  alloc_intsym = NULL;
516
12.8k
  bed = get_elf_backend_data (ibfd);
517
12.8k
  extsym_size = bed->s->sizeof_sym;
518
12.8k
  if (_bfd_mul_overflow (symcount, extsym_size, &amt))
519
0
    {
520
0
      bfd_set_error (bfd_error_file_too_big);
521
0
      return NULL;
522
0
    }
523
12.8k
  pos = symtab_hdr->sh_offset + symoffset * extsym_size;
524
12.8k
  size_t alloc_ext_size = amt;
525
12.8k
  if (bfd_seek (ibfd, pos, SEEK_SET) != 0
526
12.5k
      || !_bfd_mmap_read_temporary (&extsym_buf, &alloc_ext_size,
527
12.5k
            &alloc_ext, ibfd, false))
528
998
    {
529
998
      intsym_buf = NULL;
530
998
      goto out2;
531
998
    }
532
533
11.8k
  size_t alloc_extshndx_size = 0;
534
11.8k
  if (shndx_hdr == NULL || shndx_hdr->sh_size == 0)
535
10.4k
    extshndx_buf = NULL;
536
1.38k
  else
537
1.38k
    {
538
1.38k
      if (_bfd_mul_overflow (symcount, sizeof (Elf_External_Sym_Shndx), &amt))
539
0
  {
540
0
    bfd_set_error (bfd_error_file_too_big);
541
0
    intsym_buf = NULL;
542
0
    goto out1;
543
0
  }
544
1.38k
      alloc_extshndx_size = amt;
545
1.38k
      pos = shndx_hdr->sh_offset + symoffset * sizeof (Elf_External_Sym_Shndx);
546
1.38k
      if (bfd_seek (ibfd, pos, SEEK_SET) != 0
547
1.29k
    || !_bfd_mmap_read_temporary ((void **) &extshndx_buf,
548
1.29k
          &alloc_extshndx_size,
549
1.29k
          (void **) &alloc_extshndx,
550
1.29k
          ibfd, false))
551
217
  {
552
217
    intsym_buf = NULL;
553
217
    goto out1;
554
217
  }
555
1.38k
    }
556
557
11.6k
  if (intsym_buf == NULL)
558
4.31k
    {
559
4.31k
      if (_bfd_mul_overflow (symcount, sizeof (Elf_Internal_Sym), &amt))
560
0
  {
561
0
    bfd_set_error (bfd_error_file_too_big);
562
0
    goto out1;
563
0
  }
564
4.31k
      alloc_intsym = (Elf_Internal_Sym *) bfd_malloc (amt);
565
4.31k
      intsym_buf = alloc_intsym;
566
4.31k
      if (intsym_buf == NULL)
567
0
  goto out1;
568
4.31k
    }
569
570
  /* Convert the symbols to internal form.  */
571
11.6k
  isymend = intsym_buf + symcount;
572
11.6k
  for (esym = (const bfd_byte *) extsym_buf, isym = intsym_buf,
573
11.6k
     shndx = extshndx_buf;
574
149k
       isym < isymend;
575
138k
       esym += extsym_size, isym++, shndx = shndx != NULL ? shndx + 1 : NULL)
576
139k
    {
577
139k
      if (!(*bed->s->swap_symbol_in) (ibfd, esym, shndx, isym))
578
392
  {
579
392
    symoffset += (esym - (bfd_byte *) extsym_buf) / extsym_size;
580
    /* xgettext:c-format */
581
392
    _bfd_error_handler (_("%pB symbol number %lu references"
582
392
        " nonexistent SHT_SYMTAB_SHNDX section"),
583
392
            ibfd, (unsigned long) symoffset);
584
392
    free (alloc_intsym);
585
392
    intsym_buf = NULL;
586
392
    goto out1;
587
392
  }
588
589
      /* PR 33019: Do not accept unsupported binding values - they will
590
   likely cause problems later on.  */
591
139k
      int bind = ELF_ST_BIND (isym->st_info);
592
139k
      if (bind > STB_WEAK && bind < STB_LOOS)
593
745
  { 
594
    /* xgettext:c-format */
595
745
    _bfd_error_handler (_("%pB symbol number %lu uses unsupported binding of %u"),
596
745
            ibfd, (unsigned long) (isym - intsym_buf), bind);
597
745
    free (alloc_intsym);
598
745
    intsym_buf = NULL;
599
745
    goto out1;
600
745
  }
601
602
      /* Paranoia: Also refuse to accept the only undefined symbol type: 7.  */
603
138k
      int t = ELF_ST_TYPE (isym->st_info);
604
138k
      if (t == 7)
605
198
  {
606
    /* xgettext:c-format */
607
198
    _bfd_error_handler (_("%pB symbol number %lu uses unsupported type of %u"),
608
198
            ibfd, (unsigned long) (isym - intsym_buf), t);
609
198
    free (alloc_intsym);
610
198
    intsym_buf = NULL;
611
198
    goto out1;
612
198
  }
613
138k
    }
614
615
11.8k
 out1:
616
11.8k
  _bfd_munmap_temporary (alloc_extshndx, alloc_extshndx_size);
617
12.8k
 out2:
618
12.8k
  _bfd_munmap_temporary (alloc_ext, alloc_ext_size);
619
620
12.8k
  return intsym_buf;
621
11.8k
}
622
623
/* Look up a symbol name.  */
624
static const char *
625
bfd_elf_sym_name_raw (bfd *abfd,
626
          Elf_Internal_Shdr *symtab_hdr,
627
          Elf_Internal_Sym *isym)
628
129k
{
629
129k
  unsigned int iname = isym->st_name;
630
129k
  unsigned int shindex = symtab_hdr->sh_link;
631
632
129k
  if (iname == 0 && ELF_ST_TYPE (isym->st_info) == STT_SECTION
633
      /* Check for a bogus st_shndx to avoid crashing.  */
634
24.1k
      && isym->st_shndx < elf_numsections (abfd))
635
23.7k
    {
636
23.7k
      iname = elf_elfsections (abfd)[isym->st_shndx]->sh_name;
637
23.7k
      shindex = elf_elfheader (abfd)->e_shstrndx;
638
23.7k
    }
639
640
129k
  return bfd_elf_string_from_elf_section (abfd, shindex, iname);
641
129k
}
642
643
const char *
644
bfd_elf_sym_name (bfd *abfd,
645
      Elf_Internal_Shdr *symtab_hdr,
646
      Elf_Internal_Sym *isym,
647
      asection *sym_sec)
648
122k
{
649
122k
  const char *name = bfd_elf_sym_name_raw (abfd, symtab_hdr, isym);
650
122k
  if (name == NULL)
651
27.8k
    name = bfd_symbol_error_name;
652
95.1k
  else if (sym_sec && *name == '\0')
653
0
    name = bfd_section_name (sym_sec);
654
655
122k
  return name;
656
122k
}
657
658
/* Return the name of the group signature symbol.  Why isn't the
659
   signature just a string?  */
660
661
static const char *
662
group_signature (bfd *abfd, Elf_Internal_Shdr *ghdr)
663
9.00k
{
664
9.00k
  Elf_Internal_Shdr *hdr;
665
9.00k
  unsigned char esym[sizeof (Elf64_External_Sym)];
666
9.00k
  Elf_External_Sym_Shndx eshndx;
667
9.00k
  Elf_Internal_Sym isym;
668
669
  /* First we need to ensure the symbol table is available.  Make sure
670
     that it is a symbol table section.  */
671
9.00k
  if (ghdr->sh_link >= elf_numsections (abfd))
672
28
    return NULL;
673
8.98k
  hdr = elf_elfsections (abfd) [ghdr->sh_link];
674
8.98k
  if (hdr->sh_type != SHT_SYMTAB
675
8.35k
      || ! bfd_section_from_shdr (abfd, ghdr->sh_link))
676
653
    return NULL;
677
678
  /* Go read the symbol.  */
679
8.32k
  hdr = &elf_tdata (abfd)->symtab_hdr;
680
8.32k
  if (bfd_elf_get_elf_syms (abfd, hdr, 1, ghdr->sh_info,
681
8.32k
          &isym, esym, &eshndx) == NULL)
682
1.87k
    return NULL;
683
684
6.45k
  return bfd_elf_sym_name_raw (abfd, hdr, &isym);
685
8.32k
}
686
687
static bool
688
is_valid_group_section_header (Elf_Internal_Shdr *shdr, size_t minsize)
689
13.3k
{
690
13.3k
  return (shdr->sh_size >= minsize
691
13.1k
    && shdr->sh_entsize == GRP_ENTRY_SIZE
692
12.3k
    && shdr->sh_size % GRP_ENTRY_SIZE == 0
693
12.2k
    && shdr->bfd_section != NULL);
694
13.3k
}
695
696
697
/* Set next_in_group, sec_group list pointers, and group names.  */
698
699
static bool
700
process_sht_group_entries (bfd *abfd,
701
         Elf_Internal_Shdr *ghdr, unsigned int gidx)
702
12.1k
{
703
12.1k
  unsigned char *contents;
704
705
  /* Read the raw contents.  */
706
12.1k
  if (!bfd_malloc_and_get_section (abfd, ghdr->bfd_section, &contents))
707
809
    {
708
809
      _bfd_error_handler
709
  /* xgettext:c-format */
710
809
  (_("%pB: could not read contents of group [%u]"), abfd, gidx);
711
809
      return false;
712
809
    }
713
714
11.3k
  asection *last_elt = NULL;
715
11.3k
  const char *gname = NULL;
716
11.3k
  unsigned char *p = contents + ghdr->sh_size;
717
430k
  while (1)
718
430k
    {
719
430k
      unsigned int idx;
720
430k
      Elf_Internal_Shdr *shdr;
721
430k
      asection *elt;
722
723
430k
      p -= 4;
724
430k
      idx = H_GET_32 (abfd, p);
725
430k
      if (p == contents)
726
7.67k
  {
727
7.67k
    if ((idx & GRP_COMDAT) != 0)
728
2.85k
      ghdr->bfd_section->flags
729
2.85k
        |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
730
7.67k
    break;
731
7.67k
  }
732
733
422k
      if (idx == 0
734
305k
    || idx >= elf_numsections (abfd)
735
63.1k
    || (shdr = elf_elfsections (abfd)[idx])->sh_type == SHT_GROUP
736
59.8k
    || ((elt = shdr->bfd_section) != NULL
737
38.2k
        && elf_sec_group (elt) != NULL
738
19.4k
        && elf_sec_group (elt) != ghdr->bfd_section))
739
367k
  {
740
367k
    _bfd_error_handler
741
367k
      (_("%pB: invalid entry (%#x) in group [%u]"),
742
367k
       abfd, idx, gidx);
743
367k
    continue;
744
367k
  }
745
746
      /* PR binutils/23199: According to the ELF gABI all sections in
747
   a group must be marked with SHF_GROUP, but some tools
748
   generate broken objects.  Fix them up here.  */
749
55.5k
      shdr->sh_flags |= SHF_GROUP;
750
751
55.5k
      if (elt == NULL)
752
21.6k
  {
753
21.6k
    if (shdr->sh_type != SHT_RELA && shdr->sh_type != SHT_REL)
754
8.85k
      {
755
8.85k
        const char *name = bfd_elf_string_from_elf_section
756
8.85k
    (abfd, elf_elfheader (abfd)->e_shstrndx, shdr->sh_name);
757
758
8.85k
        _bfd_error_handler
759
    /* xgettext:c-format */
760
8.85k
    (_("%pB: unexpected type (%#x) section `%s' in group [%u]"),
761
8.85k
     abfd, shdr->sh_type, name, gidx);
762
8.85k
      }
763
21.6k
    continue;
764
21.6k
  }
765
766
      /* Don't try to add a section to elf_next_in_group list twice.  */
767
33.8k
      if (elf_sec_group (elt) != NULL)
768
15.1k
  continue;
769
770
18.7k
      if (last_elt == NULL)
771
9.00k
  {
772
    /* Start a circular list with one element.
773
       It will be in reverse order to match what gas does.  */
774
9.00k
    elf_next_in_group (elt) = elt;
775
    /* Point the group section to it.  */
776
9.00k
    elf_next_in_group (ghdr->bfd_section) = elt;
777
9.00k
    gname = group_signature (abfd, ghdr);
778
9.00k
    if (gname == NULL)
779
3.66k
      {
780
3.66k
        free (contents);
781
3.66k
        return false;
782
3.66k
      }
783
9.00k
  }
784
9.73k
      else
785
9.73k
  {
786
9.73k
    elf_next_in_group (elt) = elf_next_in_group (last_elt);
787
9.73k
    elf_next_in_group (last_elt) = elt;
788
9.73k
  }
789
15.0k
      last_elt = elt;
790
15.0k
      elf_group_name (elt) = gname;
791
15.0k
      elf_sec_group (elt) = ghdr->bfd_section;
792
15.0k
    }
793
794
7.67k
  free (contents);
795
7.67k
  return last_elt != NULL;
796
11.3k
}
797
798
bool
799
_bfd_elf_setup_sections (bfd *abfd)
800
32.0k
{
801
32.0k
  bool result = true;
802
803
  /* Process SHF_LINK_ORDER.  */
804
360k
  for (asection *s = abfd->sections; s != NULL; s = s->next)
805
328k
    {
806
328k
      Elf_Internal_Shdr *this_hdr = &elf_section_data (s)->this_hdr;
807
328k
      if ((this_hdr->sh_flags & SHF_LINK_ORDER) != 0)
808
20.3k
  {
809
20.3k
    unsigned int elfsec = this_hdr->sh_link;
810
    /* An sh_link value of 0 is now allowed.  It indicates that linked
811
       to section has already been discarded, but that the current
812
       section has been retained for some other reason.  This linking
813
       section is still a candidate for later garbage collection
814
       however.  */
815
20.3k
    if (elfsec == 0)
816
16.2k
      {
817
16.2k
        elf_linked_to_section (s) = NULL;
818
16.2k
      }
819
4.04k
    else
820
4.04k
      {
821
4.04k
        asection *linksec = NULL;
822
823
4.04k
        if (elfsec < elf_numsections (abfd))
824
3.94k
    {
825
3.94k
      this_hdr = elf_elfsections (abfd)[elfsec];
826
3.94k
      linksec = this_hdr->bfd_section;
827
3.94k
    }
828
829
        /* PR 1991, 2008:
830
     Some strip/objcopy may leave an incorrect value in
831
     sh_link.  We don't want to proceed.  */
832
4.04k
        if (linksec == NULL)
833
1.38k
    {
834
1.38k
      _bfd_error_handler
835
        /* xgettext:c-format */
836
1.38k
        (_("%pB: sh_link [%d] in section `%pA' is incorrect"),
837
1.38k
         s->owner, elfsec, s);
838
1.38k
      result = false;
839
1.38k
    }
840
841
4.04k
        elf_linked_to_section (s) = linksec;
842
4.04k
      }
843
20.3k
  }
844
328k
    }
845
846
  /* Process section groups.  */
847
540k
  for (unsigned int i = 1; i < elf_numsections (abfd); i++)
848
508k
    {
849
508k
      Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
850
851
508k
      if (shdr && shdr->sh_type == SHT_GROUP)
852
13.3k
  {
853
13.3k
    if (is_valid_group_section_header (shdr, GRP_ENTRY_SIZE))
854
12.2k
      {
855
12.2k
        if (shdr->sh_size >= 2 * GRP_ENTRY_SIZE
856
12.1k
      && !process_sht_group_entries (abfd, shdr, i))
857
6.80k
    result = false;
858
12.2k
      }
859
1.18k
    else
860
1.18k
      {
861
        /* PR binutils/18758: Beware of corrupt binaries with
862
     invalid group data.  */
863
1.18k
        _bfd_error_handler
864
    /* xgettext:c-format */
865
1.18k
    (_("%pB: section group entry number %u is corrupt"), abfd, i);
866
1.18k
        result = false;
867
1.18k
      }
868
13.3k
  }
869
508k
    }
870
871
32.0k
  return result;
872
32.0k
}
873
874
bool
875
bfd_elf_is_group_section (bfd *abfd ATTRIBUTE_UNUSED, const asection *sec)
876
0
{
877
0
  return elf_next_in_group (sec) != NULL;
878
0
}
879
880
const char *
881
bfd_elf_group_name (bfd *abfd ATTRIBUTE_UNUSED, const asection *sec)
882
0
{
883
0
  if (elf_sec_group (sec) != NULL)
884
0
    return elf_group_name (sec);
885
0
  return NULL;
886
0
}
887
888
/* Make a BFD section from an ELF section.  We store a pointer to the
889
   BFD section in the bfd_section field of the header.  */
890
891
bool
892
_bfd_elf_make_section_from_shdr (bfd *abfd,
893
         Elf_Internal_Shdr *hdr,
894
         const char *name,
895
         int shindex)
896
487k
{
897
487k
  asection *newsect;
898
487k
  flagword flags;
899
487k
  elf_backend_data *bed;
900
487k
  unsigned int opb = bfd_octets_per_byte (abfd, NULL);
901
902
487k
  if (hdr->bfd_section != NULL)
903
77.1k
    return true;
904
905
410k
  newsect = bfd_make_section_anyway (abfd, name);
906
410k
  if (newsect == NULL)
907
0
    return false;
908
909
410k
  hdr->bfd_section = newsect;
910
410k
  elf_section_data (newsect)->this_hdr = *hdr;
911
410k
  elf_section_data (newsect)->this_idx = shindex;
912
913
  /* Always use the real type/flags.  */
914
410k
  elf_section_type (newsect) = hdr->sh_type;
915
410k
  elf_section_flags (newsect) = hdr->sh_flags;
916
917
410k
  newsect->filepos = hdr->sh_offset;
918
919
410k
  flags = SEC_NO_FLAGS;
920
410k
  if (hdr->sh_type != SHT_NOBITS)
921
401k
    flags |= SEC_HAS_CONTENTS;
922
410k
  if (hdr->sh_type == SHT_GROUP)
923
17.9k
    flags |= SEC_GROUP;
924
410k
  if ((hdr->sh_flags & SHF_ALLOC) != 0)
925
186k
    {
926
186k
      flags |= SEC_ALLOC;
927
186k
      if (hdr->sh_type != SHT_NOBITS)
928
179k
  flags |= SEC_LOAD;
929
186k
    }
930
410k
  if ((hdr->sh_flags & SHF_WRITE) == 0)
931
318k
    flags |= SEC_READONLY;
932
410k
  if ((hdr->sh_flags & SHF_EXECINSTR) != 0)
933
91.0k
    flags |= SEC_CODE;
934
319k
  else if ((flags & SEC_LOAD) != 0)
935
100k
    flags |= SEC_DATA;
936
410k
  if ((hdr->sh_flags & SHF_MERGE) != 0)
937
64.1k
    flags |= SEC_MERGE;
938
410k
  if ((hdr->sh_flags & SHF_STRINGS) != 0)
939
76.2k
    flags |= SEC_STRINGS;
940
410k
  if ((hdr->sh_flags & SHF_TLS) != 0)
941
30.0k
    flags |= SEC_THREAD_LOCAL;
942
410k
  if ((hdr->sh_flags & SHF_EXCLUDE) != 0)
943
28.7k
    flags |= SEC_EXCLUDE;
944
945
410k
  newsect->entsize = hdr->sh_entsize;
946
947
410k
  switch (elf_elfheader (abfd)->e_ident[EI_OSABI])
948
410k
    {
949
      /* FIXME: We should not recognize SHF_GNU_MBIND for ELFOSABI_NONE,
950
   but binutils as of 2019-07-23 did not set the EI_OSABI header
951
   byte.  */
952
168k
    case ELFOSABI_GNU:
953
255k
    case ELFOSABI_FREEBSD:
954
255k
      if ((hdr->sh_flags & SHF_GNU_RETAIN) != 0)
955
41.4k
  elf_tdata (abfd)->has_gnu_osabi |= elf_gnu_osabi_retain;
956
      /* Fall through */
957
352k
    case ELFOSABI_NONE:
958
352k
      if ((hdr->sh_flags & SHF_GNU_MBIND) != 0)
959
22.2k
  elf_tdata (abfd)->has_gnu_osabi |= elf_gnu_osabi_mbind;
960
352k
      break;
961
410k
    }
962
963
410k
  if ((flags & (SEC_ALLOC | SEC_GROUP)) == 0)
964
206k
    {
965
      /* The debugging sections appear to be recognized only by name,
966
   not any sort of flag.  Their SEC_ALLOC bits are cleared.  */
967
206k
      if (name [0] == '.')
968
89.3k
  {
969
89.3k
    if (startswith (name, ".debug")
970
58.1k
        || startswith (name, ".gnu.debuglto_.debug_")
971
57.9k
        || startswith (name, ".gnu.linkonce.wi.")
972
57.1k
        || startswith (name, ".zdebug"))
973
32.8k
      flags |= SEC_DEBUGGING | SEC_ELF_OCTETS;
974
56.4k
    else if (startswith (name, GNU_BUILD_ATTRS_SECTION_NAME)
975
56.3k
       || startswith (name, ".note.gnu"))
976
427
      {
977
427
        flags |= SEC_ELF_OCTETS;
978
427
        opb = 1;
979
427
      }
980
56.0k
    else if (startswith (name, ".line")
981
55.6k
       || startswith (name, ".stab")
982
54.5k
       || streq (name, ".gdb_index"))
983
1.93k
      flags |= SEC_DEBUGGING;
984
89.3k
  }
985
206k
    }
986
987
410k
  if (!bfd_set_section_vma (newsect, hdr->sh_addr / opb)
988
410k
      || !bfd_set_section_size (newsect, hdr->sh_size)
989
410k
      || !bfd_set_section_alignment (newsect, bfd_log2 (hdr->sh_addralign
990
410k
              & -hdr->sh_addralign)))
991
220
    return false;
992
993
  /* As a GNU extension, if the name begins with .gnu.linkonce, we
994
     only link a single copy of the section.  This is used to support
995
     g++.  g++ will emit each template expansion in its own section.
996
     The symbols will be defined as weak, so that multiple definitions
997
     are permitted.  The GNU linker extension is to actually discard
998
     all but one of the sections.  */
999
410k
  if (startswith (name, ".gnu.linkonce")
1000
1.86k
      && elf_next_in_group (newsect) == NULL)
1001
1.86k
    flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
1002
1003
410k
  if (!bfd_set_section_flags (newsect, flags))
1004
0
    return false;
1005
1006
410k
  bed = get_elf_backend_data (abfd);
1007
410k
  if (bed->elf_backend_section_flags)
1008
132k
    if (!bed->elf_backend_section_flags (hdr))
1009
0
      return false;
1010
1011
  /* We do not parse the PT_NOTE segments as we are interested even in the
1012
     separate debug info files which may have the segments offsets corrupted.
1013
     PT_NOTEs from the core files are currently not parsed using BFD.  */
1014
410k
  if (hdr->sh_type == SHT_NOTE && hdr->sh_size != 0)
1015
6.58k
    {
1016
6.58k
      bfd_byte *contents;
1017
1018
6.58k
      if (!_bfd_elf_mmap_section_contents (abfd, newsect, &contents))
1019
1.27k
  return false;
1020
1021
5.30k
      elf_parse_notes (abfd, (char *) contents, hdr->sh_size,
1022
5.30k
           hdr->sh_offset, hdr->sh_addralign);
1023
5.30k
      _bfd_elf_munmap_section_contents (newsect, contents);
1024
5.30k
    }
1025
1026
409k
  if ((newsect->flags & SEC_ALLOC) != 0)
1027
186k
    {
1028
186k
      Elf_Internal_Phdr *phdr;
1029
186k
      unsigned int i, nload;
1030
1031
      /* Some ELF linkers produce binaries with all the program header
1032
   p_paddr fields zero.  If we have such a binary with more than
1033
   one PT_LOAD header, then leave the section lma equal to vma
1034
   so that we don't create sections with overlapping lma.  */
1035
186k
      phdr = elf_tdata (abfd)->phdr;
1036
206k
      for (nload = 0, i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
1037
74.1k
  if (phdr->p_paddr != 0)
1038
53.2k
    break;
1039
20.8k
  else if (phdr->p_type == PT_LOAD && phdr->p_memsz != 0)
1040
2.30k
    ++nload;
1041
186k
      if (i >= elf_elfheader (abfd)->e_phnum && nload > 1)
1042
131
  return true;
1043
1044
185k
      phdr = elf_tdata (abfd)->phdr;
1045
2.63M
      for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
1046
2.46M
  {
1047
2.46M
    if (((phdr->p_type == PT_LOAD
1048
93.3k
    && (hdr->sh_flags & SHF_TLS) == 0)
1049
2.37M
         || phdr->p_type == PT_TLS)
1050
94.3k
        && ELF_SECTION_IN_SEGMENT (hdr, phdr))
1051
18.3k
      {
1052
18.3k
        if ((newsect->flags & SEC_LOAD) == 0)
1053
998
    newsect->lma = (phdr->p_paddr
1054
998
        + hdr->sh_addr - phdr->p_vaddr) / opb;
1055
17.3k
        else
1056
    /* We used to use the same adjustment for SEC_LOAD
1057
       sections, but that doesn't work if the segment
1058
       is packed with code from multiple VMAs.
1059
       Instead we calculate the section LMA based on
1060
       the segment LMA.  It is assumed that the
1061
       segment will contain sections with contiguous
1062
       LMAs, even if the VMAs are not.  */
1063
17.3k
    newsect->lma = (phdr->p_paddr
1064
17.3k
        + hdr->sh_offset - phdr->p_offset) / opb;
1065
1066
        /* With contiguous segments, we can't tell from file
1067
     offsets whether a section with zero size should
1068
     be placed at the end of one segment or the
1069
     beginning of the next.  Decide based on vaddr.  */
1070
18.3k
        if (hdr->sh_addr >= phdr->p_vaddr
1071
18.3k
      && (hdr->sh_addr + hdr->sh_size
1072
18.3k
          <= phdr->p_vaddr + phdr->p_memsz))
1073
17.2k
    break;
1074
18.3k
      }
1075
2.46M
  }
1076
185k
    }
1077
1078
  /* Compress/decompress DWARF debug sections with names: .debug_*,
1079
     .zdebug_*, .gnu.debuglto_.debug_, after the section flags is set.  */
1080
409k
  if ((newsect->flags & SEC_DEBUGGING) != 0
1081
34.7k
      && (newsect->flags & SEC_HAS_CONTENTS) != 0
1082
34.6k
      && (newsect->flags & SEC_ELF_OCTETS) != 0)
1083
32.7k
    {
1084
32.7k
      enum { nothing, compress, decompress } action = nothing;
1085
32.7k
      int compression_header_size;
1086
32.7k
      bfd_size_type uncompressed_size;
1087
32.7k
      unsigned int uncompressed_align_power;
1088
32.7k
      enum compression_type ch_type = ch_none;
1089
32.7k
      bool compressed
1090
32.7k
  = bfd_is_section_compressed_info (abfd, newsect,
1091
32.7k
            &compression_header_size,
1092
32.7k
            &uncompressed_size,
1093
32.7k
            &uncompressed_align_power,
1094
32.7k
            &ch_type);
1095
1096
      /* Should we decompress?  */
1097
32.7k
      if ((abfd->flags & BFD_DECOMPRESS) != 0 && compressed)
1098
168
  action = decompress;
1099
1100
      /* Should we compress?  Or convert to a different compression?  */
1101
32.5k
      else if ((abfd->flags & BFD_COMPRESS) != 0
1102
3.05k
         && newsect->size != 0
1103
3.02k
         && compression_header_size >= 0
1104
3.00k
         && uncompressed_size > 0)
1105
3.00k
  {
1106
3.00k
    if (!compressed)
1107
2.98k
      action = compress;
1108
20
    else
1109
20
      {
1110
20
        enum compression_type new_ch_type = ch_none;
1111
20
        if ((abfd->flags & BFD_COMPRESS_GABI) != 0)
1112
20
    new_ch_type = ((abfd->flags & BFD_COMPRESS_ZSTD) != 0
1113
20
             ? ch_compress_zstd : ch_compress_zlib);
1114
20
        if (new_ch_type != ch_type)
1115
18
    action = compress;
1116
20
      }
1117
3.00k
  }
1118
1119
32.7k
      if (action == compress)
1120
3.00k
  {
1121
3.00k
    if (!bfd_init_section_compress_status (abfd, newsect))
1122
142
      {
1123
142
        _bfd_error_handler
1124
    /* xgettext:c-format */
1125
142
    (_("%pB: unable to compress section %s"), abfd, name);
1126
142
        return false;
1127
142
      }
1128
3.00k
  }
1129
29.7k
      else if (action == decompress)
1130
168
  {
1131
168
    if (!bfd_init_section_decompress_status (abfd, newsect))
1132
125
      {
1133
125
        _bfd_error_handler
1134
    /* xgettext:c-format */
1135
125
    (_("%pB: unable to decompress section %s"), abfd, name);
1136
125
        return false;
1137
125
      }
1138
43
#ifndef HAVE_ZSTD
1139
43
    if (newsect->compress_status == DECOMPRESS_SECTION_ZSTD)
1140
13
      {
1141
13
        _bfd_error_handler
1142
      /* xgettext:c-format */
1143
13
      (_ ("%pB: section %s is compressed with zstd, but BFD "
1144
13
          "is not built with zstd support"),
1145
13
       abfd, name);
1146
13
        newsect->compress_status = COMPRESS_SECTION_NONE;
1147
13
        return false;
1148
13
      }
1149
30
#endif
1150
30
    if (abfd->is_linker_input
1151
0
        && name[1] == 'z')
1152
0
      {
1153
        /* Rename section from .zdebug_* to .debug_* so that ld
1154
     scripts will see this section as a debug section.  */
1155
0
        char *new_name = bfd_zdebug_name_to_debug (abfd, name);
1156
0
        if (new_name == NULL)
1157
0
    return false;
1158
0
        bfd_rename_section (newsect, new_name);
1159
0
      }
1160
30
  }
1161
32.7k
    }
1162
1163
408k
  return true;
1164
409k
}
1165
1166
const char *const bfd_elf_section_type_names[] =
1167
{
1168
  "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
1169
  "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
1170
  "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
1171
};
1172
1173
/* ELF relocs are against symbols.  If we are producing relocatable
1174
   output, and the reloc is against an external symbol, and nothing
1175
   has given us any additional addend, the resulting reloc will also
1176
   be against the same symbol.  In such a case, we don't want to
1177
   change anything about the way the reloc is handled, since it will
1178
   all be done at final link time.  Rather than put special case code
1179
   into bfd_perform_relocation, all the reloc types use this howto
1180
   function, or should call this function for relocatable output.  */
1181
1182
bfd_reloc_status_type
1183
bfd_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED,
1184
           arelent *reloc_entry,
1185
           asymbol *symbol,
1186
           void *data ATTRIBUTE_UNUSED,
1187
           asection *input_section,
1188
           bfd *output_bfd,
1189
           char **error_message ATTRIBUTE_UNUSED)
1190
13.2k
{
1191
13.2k
  if (output_bfd != NULL
1192
0
      && (symbol->flags & BSF_SECTION_SYM) == 0
1193
0
      && (! reloc_entry->howto->partial_inplace
1194
0
    || reloc_entry->addend == 0))
1195
0
    {
1196
0
      reloc_entry->address += input_section->output_offset;
1197
0
      return bfd_reloc_ok;
1198
0
    }
1199
1200
  /* In some cases the relocation should be treated as output section
1201
     relative, as when linking ELF DWARF into PE COFF.  Many ELF
1202
     targets lack section relative relocations and instead use
1203
     ordinary absolute relocations for references between DWARF
1204
     sections.  That is arguably a bug in those targets but it happens
1205
     to work for the usual case of linking to non-loaded ELF debug
1206
     sections with VMAs forced to zero.  PE COFF on the other hand
1207
     doesn't allow a section VMA of zero.  */
1208
13.2k
  if (output_bfd == NULL
1209
13.2k
      && !reloc_entry->howto->pc_relative
1210
12.5k
      && (symbol->section->flags & SEC_DEBUGGING) != 0
1211
4.93k
      && (input_section->flags & SEC_DEBUGGING) != 0)
1212
4.68k
    reloc_entry->addend -= symbol->section->output_section->vma;
1213
1214
13.2k
  return bfd_reloc_continue;
1215
13.2k
}
1216

1217
/* Returns TRUE if section A matches section B.
1218
   Names, addresses and links may be different, but everything else
1219
   should be the same.  */
1220
1221
static bool
1222
section_match (const Elf_Internal_Shdr * a,
1223
         const Elf_Internal_Shdr * b)
1224
0
{
1225
0
  if (a->sh_type != b->sh_type
1226
0
      || ((a->sh_flags ^ b->sh_flags) & ~SHF_INFO_LINK) != 0
1227
0
      || a->sh_addralign != b->sh_addralign
1228
0
      || a->sh_entsize != b->sh_entsize)
1229
0
    return false;
1230
0
  if (a->sh_type == SHT_SYMTAB
1231
0
      || a->sh_type == SHT_STRTAB)
1232
0
    return true;
1233
0
  return a->sh_size == b->sh_size;
1234
0
}
1235
1236
/* Find a section in OBFD that has the same characteristics
1237
   as IHEADER.  Return the index of this section or SHN_UNDEF if
1238
   none can be found.  Check's section HINT first, as this is likely
1239
   to be the correct section.  */
1240
1241
static unsigned int
1242
find_link (const bfd *obfd, const Elf_Internal_Shdr *iheader,
1243
     const unsigned int hint)
1244
0
{
1245
0
  Elf_Internal_Shdr ** oheaders = elf_elfsections (obfd);
1246
0
  unsigned int i;
1247
1248
0
  BFD_ASSERT (iheader != NULL);
1249
1250
  /* See PR 20922 for a reproducer of the NULL test.  */
1251
0
  if (hint < elf_numsections (obfd)
1252
0
      && oheaders[hint] != NULL
1253
0
      && section_match (oheaders[hint], iheader))
1254
0
    return hint;
1255
1256
0
  for (i = 1; i < elf_numsections (obfd); i++)
1257
0
    {
1258
0
      Elf_Internal_Shdr * oheader = oheaders[i];
1259
1260
0
      if (oheader == NULL)
1261
0
  continue;
1262
0
      if (section_match (oheader, iheader))
1263
  /* FIXME: Do we care if there is a potential for
1264
     multiple matches ?  */
1265
0
  return i;
1266
0
    }
1267
1268
0
  return SHN_UNDEF;
1269
0
}
1270
1271
/* PR 19938: Attempt to set the ELF section header fields of an OS or
1272
   Processor specific section, based upon a matching input section.
1273
   Returns TRUE upon success, FALSE otherwise.  */
1274
1275
static bool
1276
copy_special_section_fields (const bfd *ibfd,
1277
           bfd *obfd,
1278
           const Elf_Internal_Shdr *iheader,
1279
           Elf_Internal_Shdr *oheader,
1280
           const unsigned int secnum)
1281
123
{
1282
123
  elf_backend_data *bed = get_elf_backend_data (obfd);
1283
123
  const Elf_Internal_Shdr **iheaders
1284
123
    = (const Elf_Internal_Shdr **) elf_elfsections (ibfd);
1285
123
  bool changed = false;
1286
123
  unsigned int sh_link;
1287
1288
123
  if (oheader->sh_type == SHT_NOBITS)
1289
31
    {
1290
      /* This is a feature for objcopy --only-keep-debug:
1291
   When a section's type is changed to NOBITS, we preserve
1292
   the sh_link and sh_info fields so that they can be
1293
   matched up with the original.
1294
1295
   Note: Strictly speaking these assignments are wrong.
1296
   The sh_link and sh_info fields should point to the
1297
   relevent sections in the output BFD, which may not be in
1298
   the same location as they were in the input BFD.  But
1299
   the whole point of this action is to preserve the
1300
   original values of the sh_link and sh_info fields, so
1301
   that they can be matched up with the section headers in
1302
   the original file.  So strictly speaking we may be
1303
   creating an invalid ELF file, but it is only for a file
1304
   that just contains debug info and only for sections
1305
   without any contents.  */
1306
31
      if (oheader->sh_link == 0)
1307
31
  oheader->sh_link = iheader->sh_link;
1308
31
      if (oheader->sh_info == 0)
1309
31
  oheader->sh_info = iheader->sh_info;
1310
31
      return true;
1311
31
    }
1312
1313
  /* Allow the target a chance to decide how these fields should be set.  */
1314
92
  if (bed->elf_backend_copy_special_section_fields (ibfd, obfd,
1315
92
                iheader, oheader))
1316
92
    return true;
1317
1318
  /* We have an iheader which might match oheader, and which has non-zero
1319
     sh_info and/or sh_link fields.  Attempt to follow those links and find
1320
     the section in the output bfd which corresponds to the linked section
1321
     in the input bfd.  */
1322
0
  if (iheader->sh_link != SHN_UNDEF)
1323
0
    {
1324
      /* See PR 20931 for a reproducer.  */
1325
0
      if (iheader->sh_link >= elf_numsections (ibfd))
1326
0
  {
1327
0
    _bfd_error_handler
1328
      /* xgettext:c-format */
1329
0
      (_("%pB: invalid sh_link field (%d) in section number %d"),
1330
0
       ibfd, iheader->sh_link, secnum);
1331
0
    return false;
1332
0
  }
1333
1334
0
      sh_link = find_link (obfd, iheaders[iheader->sh_link], iheader->sh_link);
1335
0
      if (sh_link != SHN_UNDEF)
1336
0
  {
1337
0
    oheader->sh_link = sh_link;
1338
0
    changed = true;
1339
0
  }
1340
0
      else
1341
  /* FIXME: Should we install iheader->sh_link
1342
     if we could not find a match ?  */
1343
0
  _bfd_error_handler
1344
    /* xgettext:c-format */
1345
0
    (_("%pB: failed to find link section for section %d"), obfd, secnum);
1346
0
    }
1347
1348
0
  if (iheader->sh_info)
1349
0
    {
1350
      /* The sh_info field can hold arbitrary information, but if the
1351
   SHF_LINK_INFO flag is set then it should be interpreted as a
1352
   section index.  */
1353
0
      if (iheader->sh_flags & SHF_INFO_LINK)
1354
0
  {
1355
0
    sh_link = find_link (obfd, iheaders[iheader->sh_info],
1356
0
             iheader->sh_info);
1357
0
    if (sh_link != SHN_UNDEF)
1358
0
      oheader->sh_flags |= SHF_INFO_LINK;
1359
0
  }
1360
0
      else
1361
  /* No idea what it means - just copy it.  */
1362
0
  sh_link = iheader->sh_info;
1363
1364
0
      if (sh_link != SHN_UNDEF)
1365
0
  {
1366
0
    oheader->sh_info = sh_link;
1367
0
    changed = true;
1368
0
  }
1369
0
      else
1370
0
  _bfd_error_handler
1371
    /* xgettext:c-format */
1372
0
    (_("%pB: failed to find info section for section %d"), obfd, secnum);
1373
0
    }
1374
1375
0
  return changed;
1376
0
}
1377
1378
/* Copy the program header and other data from one object module to
1379
   another.  */
1380
1381
bool
1382
_bfd_elf_copy_private_bfd_data (bfd *ibfd, bfd *obfd)
1383
153
{
1384
153
  const Elf_Internal_Shdr **iheaders;
1385
153
  Elf_Internal_Shdr **oheaders;
1386
153
  elf_backend_data *bed;
1387
153
  unsigned int i;
1388
1389
153
  if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
1390
0
    return true;
1391
1392
153
  if (!elf_flags_init (obfd))
1393
153
    {
1394
153
      elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
1395
153
      elf_flags_init (obfd) = true;
1396
153
    }
1397
1398
153
  elf_gp (obfd) = elf_gp (ibfd);
1399
1400
  /* Also copy the EI_OSABI field.  */
1401
153
  elf_elfheader (obfd)->e_ident[EI_OSABI] =
1402
153
    elf_elfheader (ibfd)->e_ident[EI_OSABI];
1403
1404
  /* If set, copy the EI_ABIVERSION field.  */
1405
153
  if (elf_elfheader (ibfd)->e_ident[EI_ABIVERSION])
1406
2
    elf_elfheader (obfd)->e_ident[EI_ABIVERSION]
1407
2
      = elf_elfheader (ibfd)->e_ident[EI_ABIVERSION];
1408
1409
  /* Copy object attributes.  */
1410
153
  _bfd_elf_copy_obj_attributes (ibfd, obfd);
1411
1412
153
  iheaders = (const Elf_Internal_Shdr **) elf_elfsections (ibfd);
1413
153
  oheaders = elf_elfsections (obfd);
1414
153
  if (iheaders == NULL || oheaders == NULL)
1415
89
    return true;
1416
1417
64
  bed = get_elf_backend_data (obfd);
1418
1419
  /* Possibly copy other fields in the section header.  */
1420
1.67k
  for (i = 1; i < elf_numsections (obfd); i++)
1421
1.61k
    {
1422
1.61k
      unsigned int j;
1423
1.61k
      Elf_Internal_Shdr * oheader = oheaders[i];
1424
1425
      /* Ignore ordinary sections.  SHT_NOBITS sections are considered however
1426
   because of a special case need for generating separate debug info
1427
   files.  See below for more details.  */
1428
1.61k
      if (oheader == NULL
1429
1.61k
    || (oheader->sh_type != SHT_NOBITS
1430
1.57k
        && oheader->sh_type < SHT_LOOS))
1431
1.48k
  continue;
1432
1433
      /* Ignore empty sections, and sections whose
1434
   fields have already been initialised.  */
1435
131
      if (oheader->sh_size == 0
1436
126
    || (oheader->sh_info != 0 && oheader->sh_link != 0))
1437
8
  continue;
1438
1439
      /* Scan for the matching section in the input bfd.
1440
   First we try for a direct mapping between the input and
1441
   output sections.  */
1442
2.34k
      for (j = 1; j < elf_numsections (ibfd); j++)
1443
2.34k
  {
1444
2.34k
    const Elf_Internal_Shdr * iheader = iheaders[j];
1445
1446
2.34k
    if (iheader == NULL)
1447
0
      continue;
1448
1449
2.34k
    if (oheader->bfd_section != NULL
1450
2.34k
        && iheader->bfd_section != NULL
1451
1.76k
        && iheader->bfd_section->output_section != NULL
1452
1.76k
        && iheader->bfd_section->output_section == oheader->bfd_section)
1453
123
      {
1454
        /* We have found a connection from the input section to
1455
     the output section.  Attempt to copy the header fields.
1456
     If this fails then do not try any further sections -
1457
     there should only be a one-to-one mapping between
1458
     input and output.  */
1459
123
        if (!copy_special_section_fields (ibfd, obfd,
1460
123
            iheader, oheader, i))
1461
0
    j = elf_numsections (ibfd);
1462
123
        break;
1463
123
      }
1464
2.34k
  }
1465
1466
123
      if (j < elf_numsections (ibfd))
1467
123
  continue;
1468
1469
      /* That failed.  So try to deduce the corresponding input section.
1470
   Unfortunately we cannot compare names as the output string table
1471
   is empty, so instead we check size, address and type.  */
1472
0
      for (j = 1; j < elf_numsections (ibfd); j++)
1473
0
  {
1474
0
    const Elf_Internal_Shdr * iheader = iheaders[j];
1475
1476
0
    if (iheader == NULL)
1477
0
      continue;
1478
1479
    /* Try matching fields in the input section's header.
1480
       Since --only-keep-debug turns all non-debug sections into
1481
       SHT_NOBITS sections, the output SHT_NOBITS type matches any
1482
       input type.  */
1483
0
    if ((oheader->sh_type == SHT_NOBITS
1484
0
         || iheader->sh_type == oheader->sh_type)
1485
0
        && (iheader->sh_flags & ~ SHF_INFO_LINK)
1486
0
        == (oheader->sh_flags & ~ SHF_INFO_LINK)
1487
0
        && iheader->sh_addralign == oheader->sh_addralign
1488
0
        && iheader->sh_entsize == oheader->sh_entsize
1489
0
        && iheader->sh_size == oheader->sh_size
1490
0
        && iheader->sh_addr == oheader->sh_addr
1491
0
        && (iheader->sh_info != oheader->sh_info
1492
0
      || iheader->sh_link != oheader->sh_link))
1493
0
      {
1494
0
        if (copy_special_section_fields (ibfd, obfd, iheader, oheader, i))
1495
0
    break;
1496
0
      }
1497
0
  }
1498
1499
0
      if (j == elf_numsections (ibfd) && oheader->sh_type >= SHT_LOOS)
1500
0
  {
1501
    /* Final attempt.  Call the backend copy function
1502
       with a NULL input section.  */
1503
0
    (void) bed->elf_backend_copy_special_section_fields (ibfd, obfd,
1504
0
                     NULL, oheader);
1505
0
  }
1506
0
    }
1507
1508
64
  return true;
1509
153
}
1510
1511
static const char *
1512
get_segment_type (unsigned int p_type)
1513
157k
{
1514
157k
  const char *pt;
1515
157k
  switch (p_type)
1516
157k
    {
1517
30.7k
    case PT_NULL: pt = "NULL"; break;
1518
2.29k
    case PT_LOAD: pt = "LOAD"; break;
1519
940
    case PT_DYNAMIC: pt = "DYNAMIC"; break;
1520
672
    case PT_INTERP: pt = "INTERP"; break;
1521
334
    case PT_NOTE: pt = "NOTE"; break;
1522
147
    case PT_SHLIB: pt = "SHLIB"; break;
1523
2.73k
    case PT_PHDR: pt = "PHDR"; break;
1524
65
    case PT_TLS: pt = "TLS"; break;
1525
304
    case PT_GNU_EH_FRAME: pt = "EH_FRAME"; break;
1526
233
    case PT_GNU_STACK: pt = "STACK"; break;
1527
211
    case PT_GNU_RELRO: pt = "RELRO"; break;
1528
0
    case PT_GNU_SFRAME: pt = "SFRAME"; break;
1529
119k
    default: pt = NULL; break;
1530
157k
    }
1531
157k
  return pt;
1532
157k
}
1533
1534
/* Print out the program headers.  */
1535
1536
bool
1537
_bfd_elf_print_private_bfd_data (bfd *abfd, void *farg)
1538
8.87k
{
1539
8.87k
  FILE *f = (FILE *) farg;
1540
8.87k
  Elf_Internal_Phdr *p;
1541
8.87k
  asection *s;
1542
8.87k
  bfd_byte *dynbuf = NULL;
1543
1544
8.87k
  p = elf_tdata (abfd)->phdr;
1545
8.87k
  if (p != NULL)
1546
6.25k
    {
1547
6.25k
      unsigned int i, c;
1548
1549
6.25k
      fprintf (f, _("\nProgram Header:\n"));
1550
6.25k
      c = elf_elfheader (abfd)->e_phnum;
1551
163k
      for (i = 0; i < c; i++, p++)
1552
157k
  {
1553
157k
    const char *pt = get_segment_type (p->p_type);
1554
157k
    char buf[20];
1555
1556
157k
    if (pt == NULL)
1557
119k
      {
1558
119k
        sprintf (buf, "0x%lx", p->p_type);
1559
119k
        pt = buf;
1560
119k
      }
1561
157k
    fprintf (f, "%8s off    0x", pt);
1562
157k
    bfd_fprintf_vma (abfd, f, p->p_offset);
1563
157k
    fprintf (f, " vaddr 0x");
1564
157k
    bfd_fprintf_vma (abfd, f, p->p_vaddr);
1565
157k
    fprintf (f, " paddr 0x");
1566
157k
    bfd_fprintf_vma (abfd, f, p->p_paddr);
1567
157k
    fprintf (f, " align 2**%u\n", bfd_log2 (p->p_align));
1568
157k
    fprintf (f, "         filesz 0x");
1569
157k
    bfd_fprintf_vma (abfd, f, p->p_filesz);
1570
157k
    fprintf (f, " memsz 0x");
1571
157k
    bfd_fprintf_vma (abfd, f, p->p_memsz);
1572
157k
    fprintf (f, " flags %c%c%c",
1573
157k
       (p->p_flags & PF_R) != 0 ? 'r' : '-',
1574
157k
       (p->p_flags & PF_W) != 0 ? 'w' : '-',
1575
157k
       (p->p_flags & PF_X) != 0 ? 'x' : '-');
1576
157k
    if ((p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)) != 0)
1577
109k
      fprintf (f, " %lx", p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X));
1578
157k
    fprintf (f, "\n");
1579
157k
  }
1580
6.25k
    }
1581
1582
8.87k
  s = bfd_get_section_by_name (abfd, ".dynamic");
1583
8.87k
  if (s != NULL && (s->flags & SEC_HAS_CONTENTS) != 0)
1584
254
    {
1585
254
      unsigned int elfsec;
1586
254
      unsigned long shlink;
1587
254
      bfd_byte *extdyn, *extdynend;
1588
254
      size_t extdynsize;
1589
254
      void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
1590
1591
254
      fprintf (f, _("\nDynamic Section:\n"));
1592
1593
254
      if (!_bfd_elf_mmap_section_contents (abfd, s, &dynbuf))
1594
13
  goto error_return;
1595
1596
241
      elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
1597
241
      if (elfsec == SHN_BAD)
1598
0
  goto error_return;
1599
241
      shlink = elf_elfsections (abfd)[elfsec]->sh_link;
1600
1601
241
      extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
1602
241
      swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
1603
1604
241
      for (extdyn = dynbuf, extdynend = dynbuf + s->size;
1605
4.51k
     (size_t) (extdynend - extdyn) >= extdynsize;
1606
4.27k
     extdyn += extdynsize)
1607
4.49k
  {
1608
4.49k
    Elf_Internal_Dyn dyn;
1609
4.49k
    const char *name = "";
1610
4.49k
    char ab[20];
1611
4.49k
    bool stringp;
1612
4.49k
    elf_backend_data *bed = get_elf_backend_data (abfd);
1613
1614
4.49k
    (*swap_dyn_in) (abfd, extdyn, &dyn);
1615
1616
4.49k
    if (dyn.d_tag == DT_NULL)
1617
214
      break;
1618
1619
4.28k
    stringp = false;
1620
4.28k
    switch (dyn.d_tag)
1621
4.28k
      {
1622
801
      default:
1623
801
        if (bed->elf_backend_get_target_dtag)
1624
75
    name = (*bed->elf_backend_get_target_dtag) (dyn.d_tag);
1625
1626
801
        if (streq (name, ""))
1627
801
    {
1628
801
      sprintf (ab, "%#" PRIx64, (uint64_t) dyn.d_tag);
1629
801
      name = ab;
1630
801
    }
1631
801
        break;
1632
1633
200
      case DT_NEEDED: name = "NEEDED"; stringp = true; break;
1634
75
      case DT_PLTRELSZ: name = "PLTRELSZ"; break;
1635
151
      case DT_PLTGOT: name = "PLTGOT"; break;
1636
84
      case DT_HASH: name = "HASH"; break;
1637
168
      case DT_STRTAB: name = "STRTAB"; break;
1638
164
      case DT_SYMTAB: name = "SYMTAB"; break;
1639
152
      case DT_RELA: name = "RELA"; break;
1640
160
      case DT_RELASZ: name = "RELASZ"; break;
1641
156
      case DT_RELAENT: name = "RELAENT"; break;
1642
159
      case DT_STRSZ: name = "STRSZ"; break;
1643
157
      case DT_SYMENT: name = "SYMENT"; break;
1644
163
      case DT_INIT: name = "INIT"; break;
1645
163
      case DT_FINI: name = "FINI"; break;
1646
3
      case DT_SONAME: name = "SONAME"; stringp = true; break;
1647
33
      case DT_RPATH: name = "RPATH"; stringp = true; break;
1648
4
      case DT_SYMBOLIC: name = "SYMBOLIC"; break;
1649
5
      case DT_REL: name = "REL"; break;
1650
4
      case DT_RELSZ: name = "RELSZ"; break;
1651
4
      case DT_RELENT: name = "RELENT"; break;
1652
2
      case DT_RELR: name = "RELR"; break;
1653
1
      case DT_RELRSZ: name = "RELRSZ"; break;
1654
0
      case DT_RELRENT: name = "RELRENT"; break;
1655
79
      case DT_PLTREL: name = "PLTREL"; break;
1656
154
      case DT_DEBUG: name = "DEBUG"; break;
1657
1
      case DT_TEXTREL: name = "TEXTREL"; break;
1658
75
      case DT_JMPREL: name = "JMPREL"; break;
1659
2
      case DT_BIND_NOW: name = "BIND_NOW"; break;
1660
95
      case DT_INIT_ARRAY: name = "INIT_ARRAY"; break;
1661
92
      case DT_FINI_ARRAY: name = "FINI_ARRAY"; break;
1662
90
      case DT_INIT_ARRAYSZ: name = "INIT_ARRAYSZ"; break;
1663
89
      case DT_FINI_ARRAYSZ: name = "FINI_ARRAYSZ"; break;
1664
1
      case DT_RUNPATH: name = "RUNPATH"; stringp = true; break;
1665
78
      case DT_FLAGS: name = "FLAGS"; break;
1666
2
      case DT_PREINIT_ARRAY: name = "PREINIT_ARRAY"; break;
1667
0
      case DT_PREINIT_ARRAYSZ: name = "PREINIT_ARRAYSZ"; break;
1668
0
      case DT_CHECKSUM: name = "CHECKSUM"; break;
1669
0
      case DT_PLTPADSZ: name = "PLTPADSZ"; break;
1670
0
      case DT_MOVEENT: name = "MOVEENT"; break;
1671
0
      case DT_MOVESZ: name = "MOVESZ"; break;
1672
0
      case DT_FEATURE: name = "FEATURE"; break;
1673
0
      case DT_POSFLAG_1: name = "POSFLAG_1"; break;
1674
0
      case DT_SYMINSZ: name = "SYMINSZ"; break;
1675
2
      case DT_SYMINENT: name = "SYMINENT"; break;
1676
0
      case DT_CONFIG: name = "CONFIG"; stringp = true; break;
1677
0
      case DT_DEPAUDIT: name = "DEPAUDIT"; stringp = true; break;
1678
0
      case DT_AUDIT: name = "AUDIT"; stringp = true; break;
1679
0
      case DT_PLTPAD: name = "PLTPAD"; break;
1680
0
      case DT_MOVETAB: name = "MOVETAB"; break;
1681
0
      case DT_SYMINFO: name = "SYMINFO"; break;
1682
103
      case DT_RELACOUNT: name = "RELACOUNT"; break;
1683
2
      case DT_RELCOUNT: name = "RELCOUNT"; break;
1684
107
      case DT_FLAGS_1: name = "FLAGS_1"; break;
1685
128
      case DT_VERSYM: name = "VERSYM"; break;
1686
0
      case DT_VERDEF: name = "VERDEF"; break;
1687
2
      case DT_VERDEFNUM: name = "VERDEFNUM"; break;
1688
129
      case DT_VERNEED: name = "VERNEED"; break;
1689
123
      case DT_VERNEEDNUM: name = "VERNEEDNUM"; break;
1690
0
      case DT_AUXILIARY: name = "AUXILIARY"; stringp = true; break;
1691
0
      case DT_USED: name = "USED"; break;
1692
0
      case DT_FILTER: name = "FILTER"; stringp = true; break;
1693
121
      case DT_GNU_HASH: name = "GNU_HASH"; break;
1694
4.28k
      }
1695
1696
4.28k
    fprintf (f, "  %-20s ", name);
1697
4.28k
    if (! stringp)
1698
4.04k
      {
1699
4.04k
        fprintf (f, "0x");
1700
4.04k
        bfd_fprintf_vma (abfd, f, dyn.d_un.d_val);
1701
4.04k
      }
1702
237
    else
1703
237
      {
1704
237
        const char *string;
1705
237
        unsigned int tagv = dyn.d_un.d_val;
1706
1707
237
        string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
1708
237
        if (string == NULL)
1709
10
    goto error_return;
1710
227
        fprintf (f, "%s", string);
1711
227
      }
1712
4.27k
    fprintf (f, "\n");
1713
4.27k
  }
1714
1715
231
      _bfd_elf_munmap_section_contents (s, dynbuf);
1716
231
      dynbuf = NULL;
1717
231
    }
1718
1719
8.85k
  if ((elf_dynverdef (abfd) != 0 && elf_tdata (abfd)->verdef == NULL)
1720
8.85k
      || (elf_dynverref (abfd) != 0 && elf_tdata (abfd)->verref == NULL))
1721
200
    {
1722
200
      if (! _bfd_elf_slurp_version_tables (abfd, false))
1723
53
  return false;
1724
200
    }
1725
1726
8.80k
  if (elf_dynverdef (abfd) != 0)
1727
1
    {
1728
1
      Elf_Internal_Verdef *t;
1729
1730
1
      fprintf (f, _("\nVersion definitions:\n"));
1731
2
      for (t = elf_tdata (abfd)->verdef; t != NULL; t = t->vd_nextdef)
1732
1
  {
1733
1
    fprintf (f, "%d 0x%2.2x 0x%8.8lx %s\n", t->vd_ndx,
1734
1
       t->vd_flags, t->vd_hash,
1735
1
       t->vd_nodename ? t->vd_nodename : "<corrupt>");
1736
1
    if (t->vd_auxptr != NULL && t->vd_auxptr->vda_nextptr != NULL)
1737
0
      {
1738
0
        Elf_Internal_Verdaux *a;
1739
1740
0
        fprintf (f, "\t");
1741
0
        for (a = t->vd_auxptr->vda_nextptr;
1742
0
       a != NULL;
1743
0
       a = a->vda_nextptr)
1744
0
    fprintf (f, "%s ",
1745
0
       a->vda_nodename ? a->vda_nodename : "<corrupt>");
1746
0
        fprintf (f, "\n");
1747
0
      }
1748
1
  }
1749
1
    }
1750
1751
8.80k
  if (elf_dynverref (abfd) != 0)
1752
146
    {
1753
146
      Elf_Internal_Verneed *t;
1754
1755
146
      fprintf (f, _("\nVersion References:\n"));
1756
292
      for (t = elf_tdata (abfd)->verref; t != NULL; t = t->vn_nextref)
1757
146
  {
1758
146
    Elf_Internal_Vernaux *a;
1759
1760
146
    fprintf (f, _("  required from %s:\n"),
1761
146
       t->vn_filename ? t->vn_filename : "<corrupt>");
1762
382
    for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1763
236
      fprintf (f, "    0x%8.8lx 0x%2.2x %2.2d %s\n", a->vna_hash,
1764
236
         a->vna_flags, a->vna_other,
1765
236
         a->vna_nodename ? a->vna_nodename : "<corrupt>");
1766
146
  }
1767
146
    }
1768
1769
8.80k
  return true;
1770
1771
23
 error_return:
1772
23
  _bfd_elf_munmap_section_contents (s, dynbuf);
1773
23
  return false;
1774
8.85k
}
1775
1776
/* Find the file offset corresponding to VMA by using the program
1777
   headers.  */
1778
1779
static file_ptr
1780
offset_from_vma (Elf_Internal_Phdr *phdrs, size_t phnum, bfd_vma vma,
1781
     size_t size, size_t *max_size_p)
1782
3.18k
{
1783
3.18k
  Elf_Internal_Phdr *seg;
1784
3.18k
  size_t i;
1785
1786
11.6k
  for (seg = phdrs, i = 0; i < phnum; ++seg, ++i)
1787
11.3k
    if (seg->p_type == PT_LOAD
1788
4.66k
  && vma >= (seg->p_vaddr & -seg->p_align)
1789
3.89k
  && vma + size <= seg->p_vaddr + seg->p_filesz)
1790
2.90k
      {
1791
2.90k
  if (max_size_p)
1792
45
    *max_size_p = seg->p_vaddr + seg->p_filesz - vma;
1793
2.90k
  return vma - seg->p_vaddr + seg->p_offset;
1794
2.90k
      }
1795
1796
283
  if (max_size_p)
1797
37
    *max_size_p = 0;
1798
283
  bfd_set_error (bfd_error_invalid_operation);
1799
283
  return (file_ptr) -1;
1800
3.18k
}
1801
1802
/* Convert hash table to internal form.  */
1803
1804
static bfd_vma *
1805
get_hash_table_data (bfd *abfd, bfd_size_type number,
1806
         unsigned int ent_size, bfd_size_type filesize)
1807
431
{
1808
431
  unsigned char *e_data = NULL;
1809
431
  bfd_vma *i_data = NULL;
1810
431
  bfd_size_type size;
1811
431
  void *e_data_addr;
1812
431
  size_t e_data_size ATTRIBUTE_UNUSED;
1813
1814
431
  if (ent_size != 4 && ent_size != 8)
1815
0
    return NULL;
1816
1817
431
  if ((size_t) number != number)
1818
0
    {
1819
0
      bfd_set_error (bfd_error_file_too_big);
1820
0
      return NULL;
1821
0
    }
1822
1823
431
  size = ent_size * number;
1824
  /* Be kind to memory checkers (eg valgrind, address sanitizer) by not
1825
     attempting to allocate memory when the read is bound to fail.  */
1826
431
  if (size > filesize
1827
389
      || number >= ~(size_t) 0 / ent_size
1828
389
      || number >= ~(size_t) 0 / sizeof (*i_data))
1829
42
    {
1830
42
      bfd_set_error (bfd_error_file_too_big);
1831
42
      return NULL;
1832
42
    }
1833
1834
389
  e_data = _bfd_mmap_temporary (abfd, size, &e_data_addr, &e_data_size);
1835
389
  if (e_data == NULL)
1836
18
    return NULL;
1837
1838
371
  i_data = (bfd_vma *) bfd_malloc (number * sizeof (*i_data));
1839
371
  if (i_data == NULL)
1840
0
    {
1841
0
      _bfd_munmap_temporary (e_data_addr, e_data_size);
1842
0
      return NULL;
1843
0
    }
1844
1845
371
  if (ent_size == 4)
1846
215k
    while (number--)
1847
215k
      i_data[number] = bfd_get_32 (abfd, e_data + number * ent_size);
1848
0
  else
1849
0
    while (number--)
1850
0
      i_data[number] = bfd_get_64 (abfd, e_data + number * ent_size);
1851
1852
371
  _bfd_munmap_temporary (e_data_addr, e_data_size);
1853
371
  return i_data;
1854
371
}
1855
1856
/* Address of .MIPS.xhash section.  FIXME: What is the best way to
1857
   support DT_MIPS_XHASH?  */
1858
103k
#define DT_MIPS_XHASH        0x70000036
1859
1860
/* Reconstruct dynamic symbol table from PT_DYNAMIC segment.  */
1861
1862
bool
1863
_bfd_elf_get_dynamic_symbols (bfd *abfd, Elf_Internal_Phdr *phdr,
1864
            Elf_Internal_Phdr *phdrs, size_t phnum,
1865
            bfd_size_type filesize)
1866
2.69k
{
1867
2.69k
  bfd_byte *extdyn, *extdynend;
1868
2.69k
  size_t extdynsize;
1869
2.69k
  void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
1870
2.69k
  bool (*swap_symbol_in) (bfd *, const void *, const void *,
1871
2.69k
        Elf_Internal_Sym *);
1872
2.69k
  Elf_Internal_Dyn dyn;
1873
2.69k
  bfd_vma dt_hash = 0;
1874
2.69k
  bfd_vma dt_gnu_hash = 0;
1875
2.69k
  bfd_vma dt_mips_xhash = 0;
1876
2.69k
  bfd_vma dt_strtab = 0;
1877
2.69k
  bfd_vma dt_symtab = 0;
1878
2.69k
  size_t dt_strsz = 0;
1879
2.69k
  bfd_vma dt_versym = 0;
1880
2.69k
  bfd_vma dt_verdef = 0;
1881
2.69k
  bfd_vma dt_verneed = 0;
1882
2.69k
  bfd_byte *dynbuf = NULL;
1883
2.69k
  char *strbuf = NULL;
1884
2.69k
  bfd_vma *gnubuckets = NULL;
1885
2.69k
  bfd_vma *gnuchains = NULL;
1886
2.69k
  bfd_vma *mipsxlat = NULL;
1887
2.69k
  file_ptr saved_filepos, filepos;
1888
2.69k
  bool res = false;
1889
2.69k
  size_t amt;
1890
2.69k
  bfd_byte *esymbuf = NULL, *esym;
1891
2.69k
  bfd_size_type symcount;
1892
2.69k
  Elf_Internal_Sym *isymbuf = NULL;
1893
2.69k
  Elf_Internal_Sym *isym, *isymend;
1894
2.69k
  bfd_byte *versym = NULL;
1895
2.69k
  bfd_byte *verdef = NULL;
1896
2.69k
  bfd_byte *verneed = NULL;
1897
2.69k
  size_t verdef_size = 0;
1898
2.69k
  size_t verneed_size = 0;
1899
2.69k
  size_t extsym_size;
1900
2.69k
  elf_backend_data *bed;
1901
2.69k
  void *dynbuf_addr = NULL;
1902
2.69k
  void *esymbuf_addr = NULL;
1903
2.69k
  size_t dynbuf_size = 0;
1904
2.69k
  size_t esymbuf_size = 0;
1905
1906
  /* Return TRUE if symbol table is bad.  */
1907
2.69k
  if (elf_bad_symtab (abfd))
1908
0
    return true;
1909
1910
  /* Return TRUE if DT_HASH/DT_GNU_HASH have bee processed before.  */
1911
2.69k
  if (elf_tdata (abfd)->dt_strtab != NULL)
1912
0
    return true;
1913
1914
2.69k
  bed = get_elf_backend_data (abfd);
1915
1916
  /* Save file position for elf_object_p.  */
1917
2.69k
  saved_filepos = bfd_tell (abfd);
1918
1919
2.69k
  if (bfd_seek (abfd, phdr->p_offset, SEEK_SET) != 0)
1920
0
    goto error_return;
1921
1922
2.69k
  dynbuf_size = phdr->p_filesz;
1923
2.69k
  dynbuf = _bfd_mmap_temporary (abfd, dynbuf_size, &dynbuf_addr, &dynbuf_size);
1924
2.69k
  if (dynbuf == NULL)
1925
44
    goto error_return;
1926
1927
2.65k
  extsym_size = bed->s->sizeof_sym;
1928
2.65k
  extdynsize = bed->s->sizeof_dyn;
1929
2.65k
  swap_dyn_in = bed->s->swap_dyn_in;
1930
1931
2.65k
  extdyn = dynbuf;
1932
2.65k
  if (phdr->p_filesz < extdynsize)
1933
27
    goto error_return;
1934
2.62k
  extdynend = extdyn + phdr->p_filesz;
1935
61.8k
  for (; extdyn <= (extdynend - extdynsize); extdyn += extdynsize)
1936
60.6k
    {
1937
60.6k
      swap_dyn_in (abfd, extdyn, &dyn);
1938
1939
60.6k
      if (dyn.d_tag == DT_NULL)
1940
1.37k
  break;
1941
1942
59.2k
      switch (dyn.d_tag)
1943
59.2k
  {
1944
1.05k
  case DT_HASH:
1945
1.05k
    dt_hash = dyn.d_un.d_val;
1946
1.05k
    break;
1947
1.06k
  case DT_GNU_HASH:
1948
1.06k
    if (bed->elf_machine_code != EM_MIPS
1949
900
        && bed->elf_machine_code != EM_MIPS_RS3_LE)
1950
900
      dt_gnu_hash = dyn.d_un.d_val;
1951
1.06k
    break;
1952
1.38k
  case DT_STRTAB:
1953
1.38k
    dt_strtab = dyn.d_un.d_val;
1954
1.38k
    break;
1955
1.19k
  case DT_SYMTAB:
1956
1.19k
    dt_symtab = dyn.d_un.d_val;
1957
1.19k
    break;
1958
1.29k
  case DT_STRSZ:
1959
1.29k
    dt_strsz = dyn.d_un.d_val;
1960
1.29k
    break;
1961
552
  case DT_SYMENT:
1962
552
    if (dyn.d_un.d_val != extsym_size)
1963
82
      goto error_return;
1964
470
    break;
1965
520
  case DT_VERSYM:
1966
520
    dt_versym = dyn.d_un.d_val;
1967
520
    break;
1968
339
  case DT_VERDEF:
1969
339
    dt_verdef = dyn.d_un.d_val;
1970
339
    break;
1971
265
  case DT_VERNEED:
1972
265
    dt_verneed = dyn.d_un.d_val;
1973
265
    break;
1974
51.5k
  default:
1975
51.5k
    if (dyn.d_tag == DT_MIPS_XHASH
1976
484
        && (bed->elf_machine_code == EM_MIPS
1977
238
      || bed->elf_machine_code == EM_MIPS_RS3_LE))
1978
246
      {
1979
246
        dt_gnu_hash = dyn.d_un.d_val;
1980
246
        dt_mips_xhash = dyn.d_un.d_val;
1981
246
      }
1982
51.5k
    break;
1983
59.2k
  }
1984
59.2k
    }
1985
1986
  /* Check if we can reconstruct dynamic symbol table from PT_DYNAMIC
1987
     segment.  */
1988
2.54k
  if ((!dt_hash && !dt_gnu_hash)
1989
1.69k
      || !dt_strtab
1990
1.24k
      || !dt_symtab
1991
1.09k
      || !dt_strsz)
1992
1.53k
    goto error_return;
1993
1994
  /* Get dynamic string table.  */
1995
1.00k
  filepos = offset_from_vma (phdrs, phnum, dt_strtab, dt_strsz, NULL);
1996
1.00k
  if (filepos == (file_ptr) -1
1997
953
      || bfd_seek (abfd, filepos, SEEK_SET) != 0)
1998
110
    goto error_return;
1999
2000
  /* Dynamic string table must be valid until ABFD is closed.  */
2001
898
  strbuf = (char *) _bfd_mmap_persistent (abfd, dt_strsz);
2002
898
  if (strbuf == NULL)
2003
35
    goto error_return;
2004
863
  if (strbuf[dt_strsz - 1] != 0)
2005
254
    {
2006
      /* It is an error if a string table is't terminated.  */
2007
254
      _bfd_error_handler
2008
  /* xgettext:c-format */
2009
254
  (_("%pB: DT_STRTAB table is corrupt"), abfd);
2010
254
      strbuf[dt_strsz - 1] = 0;
2011
254
    }
2012
2013
  /* Get the real symbol count from DT_HASH or DT_GNU_HASH.  Prefer
2014
     DT_HASH since it is simpler than DT_GNU_HASH.  */
2015
863
  if (dt_hash)
2016
385
    {
2017
385
      unsigned char nb[16];
2018
385
      unsigned int hash_ent_size;
2019
2020
385
      switch (bed->elf_machine_code)
2021
385
  {
2022
0
  case EM_ALPHA:
2023
0
  case EM_S390:
2024
0
  case EM_S390_OLD:
2025
0
    if (bed->s->elfclass == ELFCLASS64)
2026
0
      {
2027
0
        hash_ent_size = 8;
2028
0
        break;
2029
0
      }
2030
    /* FALLTHROUGH */
2031
385
  default:
2032
385
    hash_ent_size = 4;
2033
385
    break;
2034
385
  }
2035
2036
385
      filepos = offset_from_vma (phdrs, phnum, dt_hash, 2 * hash_ent_size,
2037
385
         NULL);
2038
385
      if (filepos == (file_ptr) -1
2039
353
    || bfd_seek (abfd, filepos, SEEK_SET) != 0
2040
327
    || bfd_read (nb, 2 * hash_ent_size, abfd) != 2 * hash_ent_size)
2041
76
  goto error_return;
2042
2043
      /* The number of dynamic symbol table entries equals the number
2044
   of chains.  */
2045
309
      if (hash_ent_size == 8)
2046
0
  symcount = bfd_get_64 (abfd, nb + hash_ent_size);
2047
309
      else
2048
309
  symcount = bfd_get_32 (abfd, nb + hash_ent_size);
2049
309
    }
2050
478
  else
2051
478
    {
2052
      /* For DT_GNU_HASH, only defined symbols with non-STB_LOCAL
2053
   bindings are in hash table.  Since in dynamic symbol table,
2054
   all symbols with STB_LOCAL binding are placed before symbols
2055
   with other bindings and all undefined symbols are placed
2056
   before defined ones, the highest symbol index in DT_GNU_HASH
2057
   is the highest dynamic symbol table index.  */
2058
478
      unsigned char nb[16];
2059
478
      bfd_vma ngnubuckets;
2060
478
      bfd_vma gnusymidx;
2061
478
      size_t i, ngnuchains;
2062
478
      bfd_vma maxchain = 0xffffffff, bitmaskwords;
2063
478
      bfd_vma buckets_vma;
2064
2065
478
      filepos = offset_from_vma (phdrs, phnum, dt_gnu_hash,
2066
478
         sizeof (nb), NULL);
2067
478
      if (filepos == (file_ptr) -1
2068
447
    || bfd_seek (abfd, filepos, SEEK_SET) != 0
2069
420
    || bfd_read (nb, sizeof (nb), abfd) != sizeof (nb))
2070
67
  goto error_return;
2071
2072
411
      ngnubuckets = bfd_get_32 (abfd, nb);
2073
411
      gnusymidx = bfd_get_32 (abfd, nb + 4);
2074
411
      bitmaskwords = bfd_get_32 (abfd, nb + 8);
2075
411
      buckets_vma = dt_gnu_hash + 16;
2076
411
      if (bed->s->elfclass == ELFCLASS32)
2077
0
  buckets_vma += bitmaskwords * 4;
2078
411
      else
2079
411
  buckets_vma += bitmaskwords * 8;
2080
411
      filepos = offset_from_vma (phdrs, phnum, buckets_vma, 4, NULL);
2081
411
      if (filepos == (file_ptr) -1
2082
390
    || bfd_seek (abfd, filepos, SEEK_SET) != 0)
2083
53
  goto error_return;
2084
2085
358
      gnubuckets = get_hash_table_data (abfd, ngnubuckets, 4, filesize);
2086
358
      if (gnubuckets == NULL)
2087
56
  goto error_return;
2088
2089
199k
      for (i = 0; i < ngnubuckets; i++)
2090
199k
  if (gnubuckets[i] != 0)
2091
157k
    {
2092
157k
      if (gnubuckets[i] < gnusymidx)
2093
35
        goto error_return;
2094
2095
157k
      if (maxchain == 0xffffffff || gnubuckets[i] > maxchain)
2096
12.8k
        maxchain = gnubuckets[i];
2097
157k
    }
2098
2099
267
      if (maxchain == 0xffffffff)
2100
32
  {
2101
32
    symcount = 0;
2102
32
    goto empty_gnu_hash;
2103
32
  }
2104
2105
235
      maxchain -= gnusymidx;
2106
235
      filepos = offset_from_vma (phdrs, phnum,
2107
235
         buckets_vma + 4 * (ngnubuckets + maxchain),
2108
235
         4, NULL);
2109
235
      if (filepos == (file_ptr) -1
2110
192
    || bfd_seek (abfd, filepos, SEEK_SET) != 0)
2111
153
  goto error_return;
2112
2113
82
      do
2114
1.25k
  {
2115
1.25k
    if (bfd_read (nb, 4, abfd) != 4)
2116
9
      goto error_return;
2117
1.24k
    ++maxchain;
2118
1.24k
    if (maxchain == 0)
2119
0
      goto error_return;
2120
1.24k
  }
2121
1.24k
      while ((bfd_get_32 (abfd, nb) & 1) == 0);
2122
2123
73
      filepos = offset_from_vma (phdrs, phnum,
2124
73
         buckets_vma + 4 * ngnubuckets,
2125
73
         4, NULL);
2126
73
      if (filepos == (file_ptr) -1
2127
73
    || bfd_seek (abfd, filepos, SEEK_SET) != 0)
2128
0
  goto error_return;
2129
2130
73
      gnuchains = get_hash_table_data (abfd, maxchain, 4, filesize);
2131
73
      if (gnuchains == NULL)
2132
4
  goto error_return;
2133
69
      ngnuchains = maxchain;
2134
2135
69
      if (dt_mips_xhash)
2136
0
  {
2137
0
    filepos = offset_from_vma (phdrs, phnum,
2138
0
             buckets_vma + 4 * (ngnubuckets + maxchain),
2139
0
             4, NULL);
2140
0
    if (filepos == (file_ptr) -1
2141
0
        || bfd_seek (abfd, filepos, SEEK_SET) != 0)
2142
0
      goto error_return;
2143
2144
0
    mipsxlat = get_hash_table_data (abfd, maxchain, 4, filesize);
2145
0
    if (mipsxlat == NULL)
2146
0
      goto error_return;
2147
0
  }
2148
2149
69
      symcount = 0;
2150
1.03k
      for (i = 0; i < ngnubuckets; ++i)
2151
963
  if (gnubuckets[i] != 0)
2152
252
    {
2153
252
      bfd_vma si = gnubuckets[i];
2154
252
      bfd_vma off = si - gnusymidx;
2155
252
      do
2156
2.01k
        {
2157
2.01k
    if (mipsxlat)
2158
0
      {
2159
0
        if (mipsxlat[off] >= symcount)
2160
0
          symcount = mipsxlat[off] + 1;
2161
0
      }
2162
2.01k
    else
2163
2.01k
      {
2164
2.01k
        if (si >= symcount)
2165
1.16k
          symcount = si + 1;
2166
2.01k
      }
2167
2.01k
    si++;
2168
2.01k
        }
2169
2.01k
      while (off < ngnuchains && (gnuchains[off++] & 1) == 0);
2170
252
    }
2171
69
    }
2172
2173
  /* Swap in dynamic symbol table.  */
2174
378
  if (_bfd_mul_overflow (symcount, extsym_size, &amt))
2175
0
    {
2176
0
      bfd_set_error (bfd_error_file_too_big);
2177
0
      goto error_return;
2178
0
    }
2179
2180
378
  filepos = offset_from_vma (phdrs, phnum, dt_symtab, amt, NULL);
2181
378
  if (filepos == (file_ptr) -1
2182
337
      || bfd_seek (abfd, filepos, SEEK_SET) != 0)
2183
85
    goto error_return;
2184
293
  esymbuf_size = amt;
2185
293
  esymbuf = _bfd_mmap_temporary (abfd, esymbuf_size,
2186
293
         &esymbuf_addr, &esymbuf_size);
2187
293
  if (esymbuf == NULL)
2188
40
    goto error_return;
2189
2190
253
  if (_bfd_mul_overflow (symcount, sizeof (Elf_Internal_Sym), &amt))
2191
0
    {
2192
0
      bfd_set_error (bfd_error_file_too_big);
2193
0
      goto error_return;
2194
0
    }
2195
2196
  /* Dynamic symbol table must be valid until ABFD is closed.  */
2197
253
  isymbuf = (Elf_Internal_Sym *) bfd_alloc (abfd, amt);
2198
253
  if (isymbuf == NULL)
2199
0
    goto error_return;
2200
2201
253
  swap_symbol_in = bed->s->swap_symbol_in;
2202
2203
  /* Convert the symbols to internal form.  */
2204
253
  isymend = isymbuf + symcount;
2205
253
  for (esym = esymbuf, isym = isymbuf;
2206
903
       isym < isymend;
2207
650
       esym += extsym_size, isym++)
2208
716
    if (!swap_symbol_in (abfd, esym, NULL, isym)
2209
697
  || isym->st_name >= dt_strsz)
2210
66
      {
2211
66
  bfd_set_error (bfd_error_invalid_operation);
2212
66
  goto error_return;
2213
66
      }
2214
2215
187
  if (dt_versym)
2216
139
    {
2217
      /* Swap in DT_VERSYM.  */
2218
139
      if (_bfd_mul_overflow (symcount, 2, &amt))
2219
0
  {
2220
0
    bfd_set_error (bfd_error_file_too_big);
2221
0
    goto error_return;
2222
0
  }
2223
2224
139
      filepos = offset_from_vma (phdrs, phnum, dt_versym, amt, NULL);
2225
139
      if (filepos == (file_ptr) -1
2226
116
    || bfd_seek (abfd, filepos, SEEK_SET) != 0)
2227
44
  goto error_return;
2228
2229
      /* DT_VERSYM info must be valid until ABFD is closed.  */
2230
95
      versym = _bfd_mmap_persistent (abfd, amt);
2231
2232
95
      if (dt_verdef)
2233
42
  {
2234
    /* Read in DT_VERDEF.  */
2235
42
    filepos = offset_from_vma (phdrs, phnum, dt_verdef,
2236
42
             0, &verdef_size);
2237
42
    if (filepos == (file_ptr) -1
2238
20
        || bfd_seek (abfd, filepos, SEEK_SET) != 0)
2239
33
      goto error_return;
2240
2241
    /* DT_VERDEF info must be valid until ABFD is closed.  */
2242
9
    verdef = _bfd_mmap_persistent (abfd, verdef_size);
2243
9
  }
2244
2245
62
      if (dt_verneed)
2246
40
  {
2247
    /* Read in DT_VERNEED.  */
2248
40
    filepos = offset_from_vma (phdrs, phnum, dt_verneed,
2249
40
             0, &verneed_size);
2250
40
    if (filepos == (file_ptr) -1
2251
25
        || bfd_seek (abfd, filepos, SEEK_SET) != 0)
2252
23
      goto error_return;
2253
2254
    /* DT_VERNEED info must be valid until ABFD is closed.  */
2255
17
    verneed = _bfd_mmap_persistent (abfd, verneed_size);
2256
17
  }
2257
62
    }
2258
2259
119
 empty_gnu_hash:
2260
119
  elf_tdata (abfd)->dt_strtab = strbuf;
2261
119
  elf_tdata (abfd)->dt_strsz = dt_strsz;
2262
119
  elf_tdata (abfd)->dt_symtab = isymbuf;
2263
119
  elf_tdata (abfd)->dt_symtab_count = symcount;
2264
119
  elf_tdata (abfd)->dt_versym = versym;
2265
119
  elf_tdata (abfd)->dt_verdef = verdef;
2266
119
  elf_tdata (abfd)->dt_verneed = verneed;
2267
119
  elf_tdata (abfd)->dt_verdef_count
2268
119
    = verdef_size / sizeof (Elf_External_Verdef);
2269
119
  elf_tdata (abfd)->dt_verneed_count
2270
119
    = verneed_size / sizeof (Elf_External_Verneed);
2271
2272
119
  res = true;
2273
2274
2.69k
 error_return:
2275
  /* Restore file position for elf_object_p.  */
2276
2.69k
  if (bfd_seek (abfd, saved_filepos, SEEK_SET) != 0)
2277
0
    res = false;
2278
2.69k
  _bfd_munmap_temporary (dynbuf_addr, dynbuf_size);
2279
2.69k
  _bfd_munmap_temporary (esymbuf_addr, esymbuf_size);
2280
2.69k
  free (gnubuckets);
2281
2.69k
  free (gnuchains);
2282
2.69k
  free (mipsxlat);
2283
2.69k
  return res;
2284
119
}
2285
2286
/* Reconstruct section from dynamic symbol.  */
2287
2288
asection *
2289
_bfd_elf_get_section_from_dynamic_symbol (bfd *abfd,
2290
            Elf_Internal_Sym *isym)
2291
1
{
2292
1
  asection *sec;
2293
1
  flagword flags;
2294
2295
1
  if (!elf_use_dt_symtab_p (abfd))
2296
0
    return NULL;
2297
2298
1
  flags = SEC_ALLOC | SEC_LOAD;
2299
1
  switch (ELF_ST_TYPE (isym->st_info))
2300
1
    {
2301
0
    case STT_FUNC:
2302
0
    case STT_GNU_IFUNC:
2303
0
      sec = bfd_get_section_by_name (abfd, ".text");
2304
0
      if (sec == NULL)
2305
0
  sec = bfd_make_section_with_flags (abfd,
2306
0
             ".text",
2307
0
             flags | SEC_CODE);
2308
0
      break;
2309
0
    case STT_COMMON:
2310
0
      sec = bfd_com_section_ptr;
2311
0
      break;
2312
0
    case STT_OBJECT:
2313
0
      sec = bfd_get_section_by_name (abfd, ".data");
2314
0
      if (sec == NULL)
2315
0
  sec = bfd_make_section_with_flags (abfd,
2316
0
             ".data",
2317
0
             flags | SEC_DATA);
2318
0
      break;
2319
0
    case STT_TLS:
2320
0
      sec = bfd_get_section_by_name (abfd, ".tdata");
2321
0
      if (sec == NULL)
2322
0
  sec = bfd_make_section_with_flags (abfd,
2323
0
             ".tdata",
2324
0
             (flags
2325
0
              | SEC_DATA
2326
0
              | SEC_THREAD_LOCAL));
2327
0
      break;
2328
1
    default:
2329
1
      sec = bfd_abs_section_ptr;
2330
1
      break;
2331
1
    }
2332
2333
1
  return sec;
2334
1
}
2335
2336
/* Get version name.  If BASE_P is TRUE, return "Base" for VER_FLG_BASE
2337
   and return symbol version for symbol version itself.   */
2338
2339
const char *
2340
_bfd_elf_get_symbol_version_string (bfd *abfd, asymbol *symbol,
2341
            bool base_p,
2342
            bool *hidden)
2343
47.5k
{
2344
47.5k
  const char *version_string = NULL;
2345
47.5k
  if ((elf_dynversym (abfd) != 0
2346
7.57k
       && (elf_dynverdef (abfd) != 0 || elf_dynverref (abfd) != 0))
2347
39.9k
      || (elf_tdata (abfd)->dt_versym != NULL
2348
0
    && (elf_tdata (abfd)->dt_verdef != NULL
2349
0
        || elf_tdata (abfd)->dt_verneed != NULL)))
2350
7.53k
    {
2351
7.53k
      unsigned int vernum = ((elf_symbol_type *) symbol)->version;
2352
2353
7.53k
      *hidden = (vernum & VERSYM_HIDDEN) != 0;
2354
7.53k
      vernum &= VERSYM_VERSION;
2355
2356
7.53k
      if (vernum == 0)
2357
2.09k
  version_string = "";
2358
5.43k
      else if (vernum == 1
2359
4.22k
         && (vernum > elf_tdata (abfd)->cverdefs
2360
0
       || (elf_tdata (abfd)->verdef[0].vd_flags
2361
0
           == VER_FLG_BASE)))
2362
4.22k
  version_string = base_p ? "Base" : "";
2363
1.21k
      else if (vernum <= elf_tdata (abfd)->cverdefs)
2364
0
  {
2365
0
    const char *nodename
2366
0
      = elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
2367
0
    version_string = "";
2368
0
    if (base_p
2369
0
        || nodename == NULL
2370
0
        || symbol->name == NULL
2371
0
        || ! streq (symbol->name, nodename))
2372
0
      version_string = nodename;
2373
0
  }
2374
1.21k
      else
2375
1.21k
  {
2376
1.21k
    Elf_Internal_Verneed *t;
2377
2378
1.21k
    version_string = _("<corrupt>");
2379
1.21k
    for (t = elf_tdata (abfd)->verref;
2380
2.42k
         t != NULL;
2381
1.21k
         t = t->vn_nextref)
2382
1.21k
      {
2383
1.21k
        Elf_Internal_Vernaux *a;
2384
2385
2.70k
        for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2386
2.37k
    {
2387
2.37k
      if (a->vna_other == vernum)
2388
887
        {
2389
887
          *hidden = true;
2390
887
          version_string = a->vna_nodename;
2391
887
          break;
2392
887
        }
2393
2.37k
    }
2394
1.21k
      }
2395
1.21k
  }
2396
7.53k
    }
2397
47.5k
  return version_string;
2398
47.5k
}
2399
2400
/* Display ELF-specific fields of a symbol.  */
2401
2402
void
2403
_bfd_elf_print_symbol (bfd *abfd,
2404
           void *filep,
2405
           asymbol *symbol,
2406
           bfd_print_symbol_type how)
2407
0
{
2408
0
  FILE *file = (FILE *) filep;
2409
0
  const char *symname = (symbol->name != bfd_symbol_error_name
2410
0
       ? symbol->name : _("<corrupt>"));
2411
2412
0
  switch (how)
2413
0
    {
2414
0
    case bfd_print_symbol_name:
2415
0
      fprintf (file, "%s", symname);
2416
0
      break;
2417
0
    case bfd_print_symbol_more:
2418
0
      fprintf (file, "elf ");
2419
0
      bfd_fprintf_vma (abfd, file, symbol->value);
2420
0
      fprintf (file, " %x", symbol->flags);
2421
0
      break;
2422
0
    case bfd_print_symbol_all:
2423
0
      {
2424
0
  const char *section_name;
2425
0
  const char *name = NULL;
2426
0
  elf_backend_data *bed;
2427
0
  unsigned char st_other;
2428
0
  bfd_vma val;
2429
0
  const char *version_string;
2430
0
  bool hidden;
2431
2432
0
  section_name = symbol->section ? symbol->section->name : "(*none*)";
2433
2434
0
  bed = get_elf_backend_data (abfd);
2435
0
  if (bed->elf_backend_print_symbol_all)
2436
0
    name = (*bed->elf_backend_print_symbol_all) (abfd, filep, symbol);
2437
2438
0
  if (name != NULL)
2439
0
    symname = name;
2440
0
  else
2441
0
    bfd_print_symbol_vandf (abfd, file, symbol);
2442
2443
0
  fprintf (file, " %s\t", section_name);
2444
  /* Print the "other" value for a symbol.  For common symbols,
2445
     we've already printed the size; now print the alignment.
2446
     For other symbols, we have no specified alignment, and
2447
     we've printed the address; now print the size.  */
2448
0
  if (symbol->section && bfd_is_com_section (symbol->section))
2449
0
    val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_value;
2450
0
  else
2451
0
    val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_size;
2452
0
  bfd_fprintf_vma (abfd, file, val);
2453
2454
  /* If we have version information, print it.  */
2455
0
  version_string = _bfd_elf_get_symbol_version_string (abfd,
2456
0
                   symbol,
2457
0
                   true,
2458
0
                   &hidden);
2459
0
  if (version_string)
2460
0
    {
2461
0
      if (!hidden)
2462
0
        fprintf (file, "  %-11s", version_string);
2463
0
      else
2464
0
        {
2465
0
    int i;
2466
2467
0
    fprintf (file, " (%s)", version_string);
2468
0
    for (i = 10 - strlen (version_string); i > 0; --i)
2469
0
      putc (' ', file);
2470
0
        }
2471
0
    }
2472
2473
  /* If the st_other field is not zero, print it.  */
2474
0
  st_other = ((elf_symbol_type *) symbol)->internal_elf_sym.st_other;
2475
2476
0
  switch (st_other)
2477
0
    {
2478
0
    case 0: break;
2479
0
    case STV_INTERNAL:  fprintf (file, " .internal");  break;
2480
0
    case STV_HIDDEN:    fprintf (file, " .hidden");    break;
2481
0
    case STV_PROTECTED: fprintf (file, " .protected"); break;
2482
0
    default:
2483
      /* Some other non-defined flags are also present, so print
2484
         everything hex.  */
2485
0
      fprintf (file, " 0x%02x", (unsigned int) st_other);
2486
0
    }
2487
2488
0
  fprintf (file, " %s", symname);
2489
0
      }
2490
0
      break;
2491
0
    }
2492
0
}
2493

2494
/* ELF .o/exec file reading */
2495
2496
/* Create a new bfd section from an ELF section header.  */
2497
2498
bool
2499
bfd_section_from_shdr (bfd *abfd, unsigned int shindex)
2500
813k
{
2501
813k
  Elf_Internal_Shdr *hdr;
2502
813k
  Elf_Internal_Ehdr *ehdr;
2503
813k
  elf_backend_data *bed;
2504
813k
  const char *name;
2505
813k
  bool ret = true;
2506
2507
813k
  if (shindex >= elf_numsections (abfd))
2508
0
    return false;
2509
2510
  /* PR17512: A corrupt ELF binary might contain a loop of sections via
2511
     sh_link or sh_info.  Detect this here, by refusing to load a
2512
     section that we are already in the process of loading.  */
2513
813k
  if (elf_tdata (abfd)->being_created[shindex])
2514
18
    {
2515
18
      _bfd_error_handler
2516
18
  (_("%pB: warning: loop in section dependencies detected"), abfd);
2517
18
      return false;
2518
18
    }
2519
813k
  elf_tdata (abfd)->being_created[shindex] = true;
2520
2521
813k
  hdr = elf_elfsections (abfd)[shindex];
2522
813k
  ehdr = elf_elfheader (abfd);
2523
813k
  name = bfd_elf_string_from_elf_section (abfd, ehdr->e_shstrndx,
2524
813k
            hdr->sh_name);
2525
813k
  if (name == NULL)
2526
6.14k
    goto fail;
2527
2528
807k
  bed = get_elf_backend_data (abfd);
2529
807k
  switch (hdr->sh_type)
2530
807k
    {
2531
80.2k
    case SHT_NULL:
2532
      /* Inactive section. Throw it away.  */
2533
80.2k
      goto success;
2534
2535
244k
    case SHT_PROGBITS:   /* Normal section with contents.  */
2536
253k
    case SHT_NOBITS:   /* .bss section.  */
2537
254k
    case SHT_HASH:   /* .hash section.  */
2538
263k
    case SHT_NOTE:   /* .note section.  */
2539
281k
    case SHT_INIT_ARRAY: /* .init_array section.  */
2540
292k
    case SHT_FINI_ARRAY: /* .fini_array section.  */
2541
294k
    case SHT_PREINIT_ARRAY: /* .preinit_array section.  */
2542
294k
    case SHT_GNU_LIBLIST: /* .gnu.liblist section.  */
2543
295k
    case SHT_GNU_HASH:   /* .gnu.hash section.  */
2544
295k
    case SHT_GNU_SFRAME: /* .sframe section.  */
2545
295k
      ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2546
295k
      goto success;
2547
2548
2.26k
    case SHT_DYNAMIC: /* Dynamic linking information.  */
2549
2.26k
      if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
2550
54
  goto fail;
2551
2552
2.20k
      if (hdr->sh_link > elf_numsections (abfd))
2553
203
  {
2554
    /* PR 10478: Accept Solaris binaries with a sh_link field
2555
       set to SHN_BEFORE (LORESERVE) or SHN_AFTER (LORESERVE+1).  */
2556
203
    switch (bfd_get_arch (abfd))
2557
203
      {
2558
168
      case bfd_arch_i386:
2559
203
      case bfd_arch_sparc:
2560
203
        if (hdr->sh_link == (SHN_LORESERVE & 0xffff)
2561
27
      || hdr->sh_link == ((SHN_LORESERVE + 1) & 0xffff))
2562
203
    break;
2563
        /* Otherwise fall through.  */
2564
0
      default:
2565
0
        goto fail;
2566
203
      }
2567
203
  }
2568
2.00k
      else if (elf_elfsections (abfd)[hdr->sh_link] == NULL)
2569
0
  goto fail;
2570
2.00k
      else if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_STRTAB)
2571
1.19k
  {
2572
1.19k
    Elf_Internal_Shdr *dynsymhdr;
2573
2574
    /* The shared libraries distributed with hpux11 have a bogus
2575
       sh_link field for the ".dynamic" section.  Find the
2576
       string table for the ".dynsym" section instead.  */
2577
1.19k
    if (elf_dynsymtab (abfd) != 0)
2578
228
      {
2579
228
        dynsymhdr = elf_elfsections (abfd)[elf_dynsymtab (abfd)];
2580
228
        hdr->sh_link = dynsymhdr->sh_link;
2581
228
      }
2582
970
    else
2583
970
      {
2584
970
        unsigned int i, num_sec;
2585
2586
970
        num_sec = elf_numsections (abfd);
2587
18.0k
        for (i = 1; i < num_sec; i++)
2588
17.2k
    {
2589
17.2k
      dynsymhdr = elf_elfsections (abfd)[i];
2590
17.2k
      if (dynsymhdr->sh_type == SHT_DYNSYM)
2591
163
        {
2592
163
          hdr->sh_link = dynsymhdr->sh_link;
2593
163
          break;
2594
163
        }
2595
17.2k
    }
2596
970
      }
2597
1.19k
  }
2598
2.20k
      goto success;
2599
2600
102k
    case SHT_SYMTAB:   /* A symbol table.  */
2601
102k
      if (elf_onesymtab (abfd) == shindex)
2602
83.2k
  goto success;
2603
2604
19.6k
      if (hdr->sh_entsize != bed->s->sizeof_sym)
2605
516
  goto fail;
2606
2607
19.1k
      if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size)
2608
985
  {
2609
985
    if (hdr->sh_size != 0)
2610
98
      goto fail;
2611
    /* Some assemblers erroneously set sh_info to one with a
2612
       zero sh_size.  ld sees this as a global symbol count
2613
       of (unsigned) -1.  Fix it here.  */
2614
887
    hdr->sh_info = 0;
2615
887
    goto success;
2616
985
  }
2617
2618
      /* PR 18854: A binary might contain more than one symbol table.
2619
   Unusual, but possible.  Warn, but continue.  */
2620
18.1k
      if (elf_onesymtab (abfd) != 0)
2621
3.17k
  {
2622
3.17k
    _bfd_error_handler
2623
      /* xgettext:c-format */
2624
3.17k
      (_("%pB: warning: multiple symbol tables detected"
2625
3.17k
         " - ignoring the table in section %u"),
2626
3.17k
       abfd, shindex);
2627
3.17k
    goto success;
2628
3.17k
  }
2629
14.9k
      elf_onesymtab (abfd) = shindex;
2630
14.9k
      elf_symtab_hdr (abfd) = *hdr;
2631
14.9k
      elf_elfsections (abfd)[shindex] = hdr = & elf_symtab_hdr (abfd);
2632
14.9k
      abfd->flags |= HAS_SYMS;
2633
2634
      /* Sometimes a shared object will map in the symbol table.  If
2635
   SHF_ALLOC is set, and this is a shared object, then we also
2636
   treat this section as a BFD section.  We can not base the
2637
   decision purely on SHF_ALLOC, because that flag is sometimes
2638
   set in a relocatable object file, which would confuse the
2639
   linker.  */
2640
14.9k
      if ((hdr->sh_flags & SHF_ALLOC) != 0
2641
784
    && (abfd->flags & DYNAMIC) != 0
2642
188
    && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2643
188
            shindex))
2644
13
  goto fail;
2645
2646
      /* Go looking for SHT_SYMTAB_SHNDX too, since if there is one we
2647
   can't read symbols without that section loaded as well.  It
2648
   is most likely specified by the next section header.  */
2649
14.9k
      {
2650
14.9k
  elf_section_list * entry;
2651
14.9k
  unsigned int i, num_sec;
2652
2653
16.5k
  for (entry = elf_symtab_shndx_list (abfd); entry; entry = entry->next)
2654
3.29k
    if (entry->hdr.sh_link == shindex)
2655
1.66k
      goto success;
2656
2657
13.2k
  num_sec = elf_numsections (abfd);
2658
40.5k
  for (i = shindex + 1; i < num_sec; i++)
2659
27.2k
    {
2660
27.2k
      Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
2661
2662
27.2k
      if (hdr2->sh_type == SHT_SYMTAB_SHNDX
2663
412
    && hdr2->sh_link == shindex)
2664
62
        break;
2665
27.2k
    }
2666
2667
13.2k
  if (i == num_sec)
2668
341k
    for (i = 1; i < shindex; i++)
2669
328k
      {
2670
328k
        Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
2671
2672
328k
        if (hdr2->sh_type == SHT_SYMTAB_SHNDX
2673
2.69k
      && hdr2->sh_link == shindex)
2674
346
    break;
2675
328k
      }
2676
2677
13.2k
  if (i != shindex)
2678
408
    ret = bfd_section_from_shdr (abfd, i);
2679
  /* else FIXME: we have failed to find the symbol table.
2680
     Should we issue an error?  */
2681
13.2k
  goto success;
2682
14.9k
      }
2683
2684
3.58k
    case SHT_DYNSYM:   /* A dynamic symbol table.  */
2685
3.58k
      if (elf_dynsymtab (abfd) == shindex)
2686
1.51k
  goto success;
2687
2688
2.07k
      if (hdr->sh_entsize != bed->s->sizeof_sym)
2689
291
  goto fail;
2690
2691
1.78k
      if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size)
2692
223
  {
2693
223
    if (hdr->sh_size != 0)
2694
102
      goto fail;
2695
2696
    /* Some linkers erroneously set sh_info to one with a
2697
       zero sh_size.  ld sees this as a global symbol count
2698
       of (unsigned) -1.  Fix it here.  */
2699
121
    hdr->sh_info = 0;
2700
121
    goto success;
2701
223
  }
2702
2703
      /* PR 18854: A binary might contain more than one dynamic symbol table.
2704
   Unusual, but possible.  Warn, but continue.  */
2705
1.56k
      if (elf_dynsymtab (abfd) != 0)
2706
177
  {
2707
177
    _bfd_error_handler
2708
      /* xgettext:c-format */
2709
177
      (_("%pB: warning: multiple dynamic symbol tables detected"
2710
177
         " - ignoring the table in section %u"),
2711
177
       abfd, shindex);
2712
177
    goto success;
2713
177
  }
2714
1.38k
      elf_dynsymtab (abfd) = shindex;
2715
1.38k
      elf_tdata (abfd)->dynsymtab_hdr = *hdr;
2716
1.38k
      elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr;
2717
1.38k
      abfd->flags |= HAS_SYMS;
2718
2719
      /* Besides being a symbol table, we also treat this as a regular
2720
   section, so that objcopy can handle it.  */
2721
1.38k
      ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2722
1.38k
      goto success;
2723
2724
7.51k
    case SHT_SYMTAB_SHNDX: /* Symbol section indices when >64k sections.  */
2725
7.51k
      {
2726
7.51k
  elf_section_list * entry;
2727
2728
9.61k
  for (entry = elf_symtab_shndx_list (abfd); entry; entry = entry->next)
2729
2.55k
    if (entry->ndx == shindex)
2730
456
      goto success;
2731
2732
7.05k
  entry = bfd_alloc (abfd, sizeof (*entry));
2733
7.05k
  if (entry == NULL)
2734
0
    goto fail;
2735
7.05k
  entry->ndx = shindex;
2736
7.05k
  entry->hdr = * hdr;
2737
7.05k
  entry->next = elf_symtab_shndx_list (abfd);
2738
7.05k
  elf_symtab_shndx_list (abfd) = entry;
2739
7.05k
  elf_elfsections (abfd)[shindex] = & entry->hdr;
2740
7.05k
  goto success;
2741
7.05k
      }
2742
2743
49.8k
    case SHT_STRTAB:   /* A string table.  */
2744
49.8k
      if (hdr->bfd_section != NULL)
2745
371
  goto success;
2746
2747
49.4k
      if (ehdr->e_shstrndx == shindex)
2748
42.7k
  {
2749
42.7k
    elf_tdata (abfd)->shstrtab_hdr = *hdr;
2750
42.7k
    elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr;
2751
42.7k
    goto success;
2752
42.7k
  }
2753
2754
6.72k
      if (elf_elfsections (abfd)[elf_onesymtab (abfd)]->sh_link == shindex)
2755
1.69k
  {
2756
1.89k
  symtab_strtab:
2757
1.89k
    elf_tdata (abfd)->strtab_hdr = *hdr;
2758
1.89k
    elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->strtab_hdr;
2759
1.89k
    goto success;
2760
1.69k
  }
2761
2762
5.03k
      if (elf_elfsections (abfd)[elf_dynsymtab (abfd)]->sh_link == shindex)
2763
869
  {
2764
939
  dynsymtab_strtab:
2765
939
    elf_tdata (abfd)->dynstrtab_hdr = *hdr;
2766
939
    hdr = &elf_tdata (abfd)->dynstrtab_hdr;
2767
939
    elf_elfsections (abfd)[shindex] = hdr;
2768
    /* We also treat this as a regular section, so that objcopy
2769
       can handle it.  */
2770
939
    ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2771
939
             shindex);
2772
939
    goto success;
2773
869
  }
2774
2775
      /* If the string table isn't one of the above, then treat it as a
2776
   regular section.  We need to scan all the headers to be sure,
2777
   just in case this strtab section appeared before the above.  */
2778
4.16k
      if (elf_onesymtab (abfd) == 0 || elf_dynsymtab (abfd) == 0)
2779
4.00k
  {
2780
4.00k
    unsigned int i, num_sec;
2781
2782
4.00k
    num_sec = elf_numsections (abfd);
2783
80.2k
    for (i = 1; i < num_sec; i++)
2784
76.6k
      {
2785
76.6k
        Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
2786
76.6k
        if (hdr2->sh_link == shindex)
2787
2.98k
    {
2788
      /* Prevent endless recursion on broken objects.  */
2789
2.98k
      if (i == shindex)
2790
56
        goto fail;
2791
2.92k
      if (! bfd_section_from_shdr (abfd, i))
2792
106
        goto fail;
2793
2.81k
      if (elf_onesymtab (abfd) == i)
2794
198
        goto symtab_strtab;
2795
2.62k
      if (elf_dynsymtab (abfd) == i)
2796
70
        goto dynsymtab_strtab;
2797
2.62k
    }
2798
76.6k
      }
2799
4.00k
  }
2800
3.73k
      ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2801
3.73k
      goto success;
2802
2803
14.9k
    case SHT_REL:
2804
107k
    case SHT_RELA:
2805
107k
    case SHT_RELR:
2806
      /* *These* do a lot of work -- but build no sections!  */
2807
107k
      {
2808
107k
  asection *target_sect;
2809
107k
  Elf_Internal_Shdr *hdr2, **p_hdr;
2810
107k
  unsigned int num_sec = elf_numsections (abfd);
2811
107k
  struct bfd_elf_section_data *esdt;
2812
107k
  bfd_size_type size;
2813
2814
107k
  if (hdr->sh_type == SHT_REL)
2815
14.9k
    size = bed->s->sizeof_rel;
2816
92.8k
  else if (hdr->sh_type == SHT_RELA)
2817
92.6k
    size = bed->s->sizeof_rela;
2818
278
  else
2819
278
    size = bed->s->arch_size / 8;
2820
107k
  if (hdr->sh_entsize != size)
2821
966
    goto fail;
2822
2823
  /* Check for a bogus link to avoid crashing.  */
2824
106k
  if (hdr->sh_link >= num_sec)
2825
104
    {
2826
104
      _bfd_error_handler
2827
        /* xgettext:c-format */
2828
104
        (_("%pB: invalid link %u for reloc section %s (index %u)"),
2829
104
         abfd, hdr->sh_link, name, shindex);
2830
104
      ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2831
104
      goto success;
2832
104
    }
2833
2834
  /* Get the symbol table.  */
2835
106k
  if ((elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB
2836
26.1k
       || elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_DYNSYM)
2837
82.1k
      && ! bfd_section_from_shdr (abfd, hdr->sh_link))
2838
336
    goto fail;
2839
2840
  /* If this is an alloc section in an executable or shared
2841
     library, or the reloc section does not use the main symbol
2842
     table we don't treat it as a reloc section.  BFD can't
2843
     adequately represent such a section, so at least for now,
2844
     we don't try.  We just present it as a normal section.  We
2845
     also can't use it as a reloc section if it points to the
2846
     null section, an invalid section, another reloc section, or
2847
     its sh_link points to the null section.  */
2848
106k
  if (((abfd->flags & (DYNAMIC | EXEC_P)) != 0
2849
5.48k
       && (hdr->sh_flags & SHF_ALLOC) != 0)
2850
104k
      || (hdr->sh_flags & SHF_COMPRESSED) != 0
2851
102k
      || hdr->sh_type == SHT_RELR
2852
102k
      || hdr->sh_link == SHN_UNDEF
2853
99.3k
      || hdr->sh_link != elf_onesymtab (abfd)
2854
76.9k
      || hdr->sh_info == SHN_UNDEF
2855
76.4k
      || hdr->sh_info >= num_sec
2856
76.4k
      || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_REL
2857
76.2k
      || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_RELA)
2858
30.6k
    {
2859
30.6k
      ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2860
30.6k
      goto success;
2861
30.6k
    }
2862
2863
75.7k
  if (! bfd_section_from_shdr (abfd, hdr->sh_info))
2864
104
    goto fail;
2865
2866
75.6k
  target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info);
2867
75.6k
  if (target_sect == NULL)
2868
123
    goto fail;
2869
2870
75.5k
  esdt = elf_section_data (target_sect);
2871
75.5k
  if (hdr->sh_type == SHT_RELA)
2872
66.0k
    p_hdr = &esdt->rela.hdr;
2873
9.48k
  else
2874
9.48k
    p_hdr = &esdt->rel.hdr;
2875
2876
  /* PR 17512: file: 0b4f81b7.
2877
     Also see PR 24456, for a file which deliberately has two reloc
2878
     sections.  */
2879
75.5k
  if (*p_hdr != NULL)
2880
1.40k
    {
2881
1.40k
      if (!bed->init_secondary_reloc_section (abfd, hdr, name, shindex))
2882
145
        {
2883
145
    _bfd_error_handler
2884
      /* xgettext:c-format */
2885
145
      (_("%pB: warning: secondary relocation section '%s' "
2886
145
         "for section %pA found - ignoring"),
2887
145
       abfd, name, target_sect);
2888
145
        }
2889
1.25k
      else
2890
1.25k
        esdt->has_secondary_relocs = true;
2891
1.40k
      goto success;
2892
1.40k
    }
2893
2894
74.1k
  hdr2 = (Elf_Internal_Shdr *) bfd_alloc (abfd, sizeof (*hdr2));
2895
74.1k
  if (hdr2 == NULL)
2896
0
    goto fail;
2897
74.1k
  *hdr2 = *hdr;
2898
74.1k
  *p_hdr = hdr2;
2899
74.1k
  elf_elfsections (abfd)[shindex] = hdr2;
2900
74.1k
  target_sect->reloc_count += (NUM_SHDR_ENTRIES (hdr)
2901
74.1k
             * bed->s->int_rels_per_ext_rel);
2902
74.1k
  target_sect->flags |= SEC_RELOC;
2903
74.1k
  target_sect->relocation = NULL;
2904
74.1k
  target_sect->rel_filepos = hdr->sh_offset;
2905
  /* In the section to which the relocations apply, mark whether
2906
     its relocations are of the REL or RELA variety.  */
2907
74.1k
  if (hdr->sh_size != 0)
2908
73.5k
    {
2909
73.5k
      if (hdr->sh_type == SHT_RELA)
2910
64.2k
        target_sect->use_rela_p = 1;
2911
73.5k
    }
2912
74.1k
  abfd->flags |= HAS_RELOC;
2913
74.1k
  goto success;
2914
74.1k
      }
2915
2916
243
    case SHT_GNU_verdef:
2917
243
      if (hdr->sh_info != 0)
2918
105
  elf_dynverdef (abfd) = shindex;
2919
243
      elf_tdata (abfd)->dynverdef_hdr = *hdr;
2920
243
      ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2921
243
      goto success;
2922
2923
802
    case SHT_GNU_versym:
2924
802
      if (hdr->sh_entsize != sizeof (Elf_External_Versym))
2925
91
  goto fail;
2926
2927
711
      elf_dynversym (abfd) = shindex;
2928
711
      elf_tdata (abfd)->dynversym_hdr = *hdr;
2929
711
      ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2930
711
      goto success;
2931
2932
843
    case SHT_GNU_verneed:
2933
843
      if (hdr->sh_info != 0)
2934
744
  elf_dynverref (abfd) = shindex;
2935
843
      elf_tdata (abfd)->dynverref_hdr = *hdr;
2936
843
      ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2937
843
      goto success;
2938
2939
975
    case SHT_SHLIB:
2940
975
      goto success;
2941
2942
18.3k
    case SHT_GROUP:
2943
18.3k
      if (!_bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
2944
32
  goto fail;
2945
2946
18.2k
      goto success;
2947
2948
136k
    default:
2949
      /* Possibly an attributes section.  */
2950
136k
      if (get_elf_backend_data (abfd)->target_os != is_solaris
2951
    /* PR 33153: Solaris defines SHT_SUNW_cap which collides with SHT_GNU_ATTRIBUTES.  */
2952
132k
    && (hdr->sh_type == SHT_GNU_ATTRIBUTES
2953
128k
        || hdr->sh_type == bed->obj_attrs_section_type))
2954
3.80k
  {
2955
3.80k
    if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
2956
21
      goto fail;
2957
3.78k
    _bfd_elf_parse_attributes (abfd, hdr);
2958
3.78k
    goto success;
2959
3.80k
  }
2960
2961
      /* Check for any processor-specific section types.  */
2962
132k
      if (bed->elf_backend_section_from_shdr (abfd, hdr, name, shindex))
2963
118k
  goto success;
2964
2965
14.0k
      if (hdr->sh_type >= SHT_LOUSER && hdr->sh_type <= SHT_HIUSER)
2966
5.28k
  {
2967
5.28k
    if ((hdr->sh_flags & SHF_ALLOC) != 0)
2968
      /* FIXME: How to properly handle allocated section reserved
2969
         for applications?  */
2970
389
      _bfd_error_handler
2971
        /* xgettext:c-format */
2972
389
        (_("%pB: unknown type [%#x] section `%s'"),
2973
389
         abfd, hdr->sh_type, name);
2974
4.89k
    else
2975
4.89k
      {
2976
        /* Allow sections reserved for applications.  */
2977
4.89k
        ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2978
4.89k
        goto success;
2979
4.89k
      }
2980
5.28k
  }
2981
8.76k
      else if (hdr->sh_type >= SHT_LOPROC
2982
946
         && hdr->sh_type <= SHT_HIPROC)
2983
  /* FIXME: We should handle this section.  */
2984
946
  _bfd_error_handler
2985
    /* xgettext:c-format */
2986
946
    (_("%pB: unknown type [%#x] section `%s'"),
2987
946
     abfd, hdr->sh_type, name);
2988
7.82k
      else if (hdr->sh_type >= SHT_LOOS && hdr->sh_type <= SHT_HIOS)
2989
4.62k
  {
2990
    /* Unrecognised OS-specific sections.  */
2991
4.62k
    if ((hdr->sh_flags & SHF_OS_NONCONFORMING) != 0)
2992
      /* SHF_OS_NONCONFORMING indicates that special knowledge is
2993
         required to correctly process the section and the file should
2994
         be rejected with an error message.  */
2995
321
      _bfd_error_handler
2996
        /* xgettext:c-format */
2997
321
        (_("%pB: unknown type [%#x] section `%s'"),
2998
321
         abfd, hdr->sh_type, name);
2999
4.30k
    else
3000
4.30k
      {
3001
        /* Otherwise it should be processed.  */
3002
4.30k
        ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
3003
4.30k
        goto success;
3004
4.30k
      }
3005
4.62k
  }
3006
3.19k
      else
3007
  /* FIXME: We should handle this section.  */
3008
3.19k
  _bfd_error_handler
3009
    /* xgettext:c-format */
3010
3.19k
    (_("%pB: unknown type [%#x] section `%s'"),
3011
3.19k
     abfd, hdr->sh_type, name);
3012
3013
4.85k
      goto fail;
3014
807k
    }
3015
3016
13.9k
 fail:
3017
13.9k
  ret = false;
3018
813k
 success:
3019
813k
  elf_tdata (abfd)->being_created[shindex] = false;
3020
813k
  return ret;
3021
13.9k
}
3022
3023
/* Return the local symbol specified by ABFD, R_SYMNDX.  */
3024
3025
Elf_Internal_Sym *
3026
bfd_sym_from_r_symndx (struct sym_cache *cache,
3027
           bfd *abfd,
3028
           unsigned long r_symndx)
3029
0
{
3030
0
  unsigned int ent = r_symndx % LOCAL_SYM_CACHE_SIZE;
3031
3032
0
  if (cache->abfd != abfd || cache->indx[ent] != r_symndx)
3033
0
    {
3034
0
      Elf_Internal_Shdr *symtab_hdr;
3035
0
      unsigned char esym[sizeof (Elf64_External_Sym)];
3036
0
      Elf_External_Sym_Shndx eshndx;
3037
3038
0
      symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
3039
0
      if (bfd_elf_get_elf_syms (abfd, symtab_hdr, 1, r_symndx,
3040
0
        &cache->sym[ent], esym, &eshndx) == NULL)
3041
0
  return NULL;
3042
3043
0
      if (cache->abfd != abfd)
3044
0
  {
3045
0
    memset (cache->indx, -1, sizeof (cache->indx));
3046
0
    cache->abfd = abfd;
3047
0
  }
3048
0
      cache->indx[ent] = r_symndx;
3049
0
    }
3050
3051
0
  return &cache->sym[ent];
3052
0
}
3053
3054
/* Given an ELF section number, retrieve the corresponding BFD
3055
   section.  */
3056
3057
asection *
3058
bfd_section_from_elf_index (bfd *abfd, unsigned int sec_index)
3059
149k
{
3060
149k
  if (sec_index >= elf_numsections (abfd))
3061
17.1k
    return NULL;
3062
132k
  return elf_elfsections (abfd)[sec_index]->bfd_section;
3063
149k
}
3064
3065
static const struct bfd_elf_special_section special_sections_b[] =
3066
{
3067
  { STRING_COMMA_LEN (".bss"), -2, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE },
3068
  { NULL,       0,  0, 0,    0 }
3069
};
3070
3071
static const struct bfd_elf_special_section special_sections_c[] =
3072
{
3073
  { STRING_COMMA_LEN (".comment"), 0, SHT_PROGBITS, 0 },
3074
  { STRING_COMMA_LEN (".ctf"),  0, SHT_PROGBITS,    0 },
3075
  { NULL,     0, 0, 0,      0 }
3076
};
3077
3078
static const struct bfd_elf_special_section special_sections_d[] =
3079
{
3080
  { STRING_COMMA_LEN (".data"),   -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
3081
  { STRING_COMMA_LEN (".data1"),   0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
3082
  /* There are more DWARF sections than these, but they needn't be added here
3083
     unless you have to cope with broken compilers that don't emit section
3084
     attributes or you want to help the user writing assembler.  */
3085
  { STRING_COMMA_LEN (".debug"),   0, SHT_PROGBITS, 0 },
3086
  { STRING_COMMA_LEN (".debug_line"),  0, SHT_PROGBITS, 0 },
3087
  { STRING_COMMA_LEN (".debug_info"),  0, SHT_PROGBITS, 0 },
3088
  { STRING_COMMA_LEN (".debug_abbrev"),  0, SHT_PROGBITS, 0 },
3089
  { STRING_COMMA_LEN (".debug_aranges"), 0, SHT_PROGBITS, 0 },
3090
  { STRING_COMMA_LEN (".dynamic"),   0, SHT_DYNAMIC,  SHF_ALLOC },
3091
  { STRING_COMMA_LEN (".dynstr"),  0, SHT_STRTAB,   SHF_ALLOC },
3092
  { STRING_COMMA_LEN (".dynsym"),  0, SHT_DYNSYM,   SHF_ALLOC },
3093
  { NULL,          0,  0, 0,      0 }
3094
};
3095
3096
static const struct bfd_elf_special_section special_sections_f[] =
3097
{
3098
  { STRING_COMMA_LEN (".fini"),        0, SHT_PROGBITS,   SHF_ALLOC + SHF_EXECINSTR },
3099
  { STRING_COMMA_LEN (".fini_array"), -2, SHT_FINI_ARRAY, SHF_ALLOC + SHF_WRITE },
3100
  { NULL,        0 , 0, 0,      0 }
3101
};
3102
3103
static const struct bfd_elf_special_section special_sections_g[] =
3104
{
3105
  { STRING_COMMA_LEN (".gnu.linkonce.b"), -2, SHT_NOBITS,      SHF_ALLOC + SHF_WRITE },
3106
  { STRING_COMMA_LEN (".gnu.linkonce.n"), -2, SHT_NOBITS,      SHF_ALLOC + SHF_WRITE },
3107
  { STRING_COMMA_LEN (".gnu.linkonce.p"), -2, SHT_PROGBITS,    SHF_ALLOC + SHF_WRITE },
3108
  { STRING_COMMA_LEN (".gnu.lto_"),   -1, SHT_PROGBITS,    SHF_EXCLUDE },
3109
  { STRING_COMMA_LEN (".got"),       0, SHT_PROGBITS,    SHF_ALLOC + SHF_WRITE },
3110
  { STRING_COMMA_LEN (".gnu_object_only"), 0, SHT_GNU_OBJECT_ONLY, SHF_EXCLUDE },
3111
  { STRING_COMMA_LEN (".gnu.version"),     0, SHT_GNU_versym,  0 },
3112
  { STRING_COMMA_LEN (".gnu.version_d"),   0, SHT_GNU_verdef,  0 },
3113
  { STRING_COMMA_LEN (".gnu.version_r"),   0, SHT_GNU_verneed, 0 },
3114
  { STRING_COMMA_LEN (".gnu.liblist"),     0, SHT_GNU_LIBLIST, SHF_ALLOC },
3115
  { STRING_COMMA_LEN (".gnu.conflict"),    0, SHT_RELA,        SHF_ALLOC },
3116
  { STRING_COMMA_LEN (".gnu.hash"),    0, SHT_GNU_HASH,    SHF_ALLOC },
3117
  { NULL,      0,    0, 0,         0 }
3118
};
3119
3120
static const struct bfd_elf_special_section special_sections_h[] =
3121
{
3122
  { STRING_COMMA_LEN (".hash"), 0, SHT_HASH,   SHF_ALLOC },
3123
  { NULL,        0, 0, 0,    0 }
3124
};
3125
3126
static const struct bfd_elf_special_section special_sections_i[] =
3127
{
3128
  { STRING_COMMA_LEN (".init"),        0, SHT_PROGBITS,   SHF_ALLOC + SHF_EXECINSTR },
3129
  { STRING_COMMA_LEN (".init_array"), -2, SHT_INIT_ARRAY, SHF_ALLOC + SHF_WRITE },
3130
  { STRING_COMMA_LEN (".interp"),      0, SHT_PROGBITS,   0 },
3131
  { NULL,          0,      0, 0,      0 }
3132
};
3133
3134
static const struct bfd_elf_special_section special_sections_l[] =
3135
{
3136
  { STRING_COMMA_LEN (".line"), 0, SHT_PROGBITS, 0 },
3137
  { NULL,        0, 0, 0,    0 }
3138
};
3139
3140
static const struct bfd_elf_special_section special_sections_n[] =
3141
{
3142
  { STRING_COMMA_LEN (".noinit"),  -2, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE },
3143
  { STRING_COMMA_LEN (".note.GNU-stack"), 0, SHT_NOTE,     0 },
3144
  { STRING_COMMA_LEN (".note"),    -1, SHT_NOTE,     0 },
3145
  { NULL,        0,     0, 0,      0 }
3146
};
3147
3148
static const struct bfd_elf_special_section special_sections_p[] =
3149
{
3150
  { STRING_COMMA_LEN (".persistent.bss"), 0, SHT_NOBITS,  SHF_ALLOC + SHF_WRITE },
3151
  { STRING_COMMA_LEN (".persistent"),  -2, SHT_PROGBITS,  SHF_ALLOC + SHF_WRITE },
3152
  { STRING_COMMA_LEN (".preinit_array"), -2, SHT_PREINIT_ARRAY, SHF_ALLOC + SHF_WRITE },
3153
  { STRING_COMMA_LEN (".plt"),      0, SHT_PROGBITS,  SHF_ALLOC + SHF_EXECINSTR },
3154
  { NULL,       0,      0, 0,     0 }
3155
};
3156
3157
static const struct bfd_elf_special_section special_sections_r[] =
3158
{
3159
  { STRING_COMMA_LEN (".rodata"), -2, SHT_PROGBITS, SHF_ALLOC },
3160
  { STRING_COMMA_LEN (".rodata1"), 0, SHT_PROGBITS, SHF_ALLOC },
3161
  { STRING_COMMA_LEN (".relr.dyn"), 0, SHT_RELR, SHF_ALLOC },
3162
  { STRING_COMMA_LEN (".rela"),   -1, SHT_RELA,     0 },
3163
  /* .relro_padding is generated by lld.  It should not be confused with a
3164
     reloc containing section, because otherwise elf_fake_sections() will
3165
     set the entsize to 8, which may not be an actual multiple of the
3166
     section's size.
3167
     Note - this entry must appear before the ".rel" entry below.  */
3168
  { STRING_COMMA_LEN (".relro_padding"), 0, SHT_NOBITS, SHF_ALLOC | SHF_WRITE },
3169
  { STRING_COMMA_LEN (".rel"),    -1, SHT_REL,      0 },
3170
  { NULL,       0,     0, 0,      0 }
3171
};
3172
3173
static const struct bfd_elf_special_section special_sections_s[] =
3174
{
3175
  { STRING_COMMA_LEN (".shstrtab"), 0, SHT_STRTAB, 0 },
3176
  { STRING_COMMA_LEN (".strtab"),   0, SHT_STRTAB, 0 },
3177
  { STRING_COMMA_LEN (".symtab"),   0, SHT_SYMTAB, 0 },
3178
  /* See struct bfd_elf_special_section declaration for the semantics of
3179
     this special case where .prefix_length != strlen (.prefix).  */
3180
  { ".stabstr",     5,  3, SHT_STRTAB, 0 },
3181
  { NULL,     0,  0, 0,    0 }
3182
};
3183
3184
static const struct bfd_elf_special_section special_sections_t[] =
3185
{
3186
  { STRING_COMMA_LEN (".text"),  -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
3187
  { STRING_COMMA_LEN (".tbss"),  -2, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE + SHF_TLS },
3188
  { STRING_COMMA_LEN (".tdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
3189
  { NULL,         0,  0, 0,      0 }
3190
};
3191
3192
static const struct bfd_elf_special_section special_sections_z[] =
3193
{
3194
  { STRING_COMMA_LEN (".zdebug_line"),    0, SHT_PROGBITS, 0 },
3195
  { STRING_COMMA_LEN (".zdebug_info"),    0, SHT_PROGBITS, 0 },
3196
  { STRING_COMMA_LEN (".zdebug_abbrev"),  0, SHT_PROGBITS, 0 },
3197
  { STRING_COMMA_LEN (".zdebug_aranges"), 0, SHT_PROGBITS, 0 },
3198
  { NULL,         0,  0, 0,      0 }
3199
};
3200
3201
static const struct bfd_elf_special_section * const special_sections[] =
3202
{
3203
  special_sections_b,   /* 'b' */
3204
  special_sections_c,   /* 'c' */
3205
  special_sections_d,   /* 'd' */
3206
  NULL,       /* 'e' */
3207
  special_sections_f,   /* 'f' */
3208
  special_sections_g,   /* 'g' */
3209
  special_sections_h,   /* 'h' */
3210
  special_sections_i,   /* 'i' */
3211
  NULL,       /* 'j' */
3212
  NULL,       /* 'k' */
3213
  special_sections_l,   /* 'l' */
3214
  NULL,       /* 'm' */
3215
  special_sections_n,   /* 'n' */
3216
  NULL,       /* 'o' */
3217
  special_sections_p,   /* 'p' */
3218
  NULL,       /* 'q' */
3219
  special_sections_r,   /* 'r' */
3220
  special_sections_s,   /* 's' */
3221
  special_sections_t,   /* 't' */
3222
  NULL,       /* 'u' */
3223
  NULL,       /* 'v' */
3224
  NULL,       /* 'w' */
3225
  NULL,       /* 'x' */
3226
  NULL,       /* 'y' */
3227
  special_sections_z    /* 'z' */
3228
};
3229
3230
const struct bfd_elf_special_section *
3231
_bfd_elf_get_special_section (const char *name,
3232
            const struct bfd_elf_special_section *spec,
3233
            unsigned int rela)
3234
759k
{
3235
759k
  int i;
3236
759k
  int len;
3237
3238
759k
  len = strlen (name);
3239
3240
4.28M
  for (i = 0; spec[i].prefix != NULL; i++)
3241
3.66M
    {
3242
3.66M
      int suffix_len;
3243
3.66M
      int prefix_len = spec[i].prefix_length;
3244
3245
3.66M
      if (len < prefix_len)
3246
1.83M
  continue;
3247
1.83M
      if (memcmp (name, spec[i].prefix, prefix_len) != 0)
3248
1.63M
  continue;
3249
3250
198k
      suffix_len = spec[i].suffix_length;
3251
198k
      if (suffix_len <= 0)
3252
197k
  {
3253
197k
    if (name[prefix_len] != 0)
3254
118k
      {
3255
118k
        if (suffix_len == 0)
3256
50.1k
    continue;
3257
68.4k
        if (name[prefix_len] != '.'
3258
10.1k
      && (suffix_len == -2
3259
3.73k
          || (rela && spec[i].type == SHT_REL)))
3260
7.34k
    continue;
3261
68.4k
      }
3262
197k
  }
3263
1.23k
      else
3264
1.23k
  {
3265
1.23k
    if (len < prefix_len + suffix_len)
3266
719
      continue;
3267
516
    if (memcmp (name + len - suffix_len,
3268
516
          spec[i].prefix + prefix_len,
3269
516
          suffix_len) != 0)
3270
332
      continue;
3271
516
  }
3272
139k
      return &spec[i];
3273
198k
    }
3274
3275
619k
  return NULL;
3276
759k
}
3277
3278
const struct bfd_elf_special_section *
3279
_bfd_elf_get_sec_type_attr (bfd *abfd, asection *sec)
3280
1.11M
{
3281
1.11M
  int i;
3282
1.11M
  const struct bfd_elf_special_section *spec;
3283
1.11M
  elf_backend_data *bed;
3284
3285
  /* See if this is one of the special sections.  */
3286
1.11M
  if (sec->name == NULL)
3287
0
    return NULL;
3288
3289
1.11M
  bed = get_elf_backend_data (abfd);
3290
1.11M
  spec = bed->special_sections;
3291
1.11M
  if (spec)
3292
552k
    {
3293
552k
      spec = _bfd_elf_get_special_section (sec->name,
3294
552k
             bed->special_sections,
3295
552k
             sec->use_rela_p);
3296
552k
      if (spec != NULL)
3297
2.66k
  return spec;
3298
552k
    }
3299
3300
1.11M
  if (sec->name[0] != '.')
3301
902k
    return NULL;
3302
3303
208k
  i = sec->name[1] - 'b';
3304
208k
  if (i < 0 || i > 'z' - 'b')
3305
7.47k
    return NULL;
3306
3307
201k
  spec = special_sections[i];
3308
3309
201k
  if (spec == NULL)
3310
11.5k
    return NULL;
3311
3312
189k
  return _bfd_elf_get_special_section (sec->name, spec, sec->use_rela_p);
3313
201k
}
3314
3315
bool
3316
_bfd_elf_new_section_hook (bfd *abfd, asection *sec)
3317
1.10M
{
3318
1.10M
  struct bfd_elf_section_data *sdata;
3319
1.10M
  elf_backend_data *bed;
3320
1.10M
  const struct bfd_elf_special_section *ssect;
3321
3322
1.10M
  sdata = (struct bfd_elf_section_data *) sec->used_by_bfd;
3323
1.10M
  if (sdata == NULL)
3324
773k
    {
3325
773k
      sdata = (struct bfd_elf_section_data *) bfd_zalloc (abfd,
3326
773k
                sizeof (*sdata));
3327
773k
      if (sdata == NULL)
3328
0
  return false;
3329
773k
      sec->used_by_bfd = sdata;
3330
773k
    }
3331
3332
  /* Indicate whether or not this section should use RELA relocations.  */
3333
1.10M
  bed = get_elf_backend_data (abfd);
3334
1.10M
  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.10M
  ssect = (*bed->get_sec_type_attr) (abfd, sec);
3339
1.10M
  if (ssect != NULL)
3340
139k
    {
3341
139k
      elf_section_type (sec) = ssect->type;
3342
139k
      elf_section_flags (sec) = ssect->attr;
3343
139k
    }
3344
3345
1.10M
  return _bfd_generic_new_section_hook (abfd, sec);
3346
1.10M
}
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
556k
{
3374
556k
  asection *newsect;
3375
556k
  char *name;
3376
556k
  char namebuf[64];
3377
556k
  size_t len;
3378
556k
  int split;
3379
556k
  unsigned int opb = bfd_octets_per_byte (abfd, NULL);
3380
3381
556k
  split = ((hdr->p_memsz > 0)
3382
423k
      && (hdr->p_filesz > 0)
3383
379k
      && (hdr->p_memsz > hdr->p_filesz));
3384
3385
556k
  if (hdr->p_filesz > 0)
3386
472k
    {
3387
472k
      sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "a" : "");
3388
472k
      len = strlen (namebuf) + 1;
3389
472k
      name = (char *) bfd_alloc (abfd, len);
3390
472k
      if (!name)
3391
0
  return false;
3392
472k
      memcpy (name, namebuf, len);
3393
472k
      newsect = bfd_make_section (abfd, name);
3394
472k
      if (newsect == NULL)
3395
0
  return false;
3396
472k
      newsect->vma = hdr->p_vaddr / opb;
3397
472k
      newsect->lma = hdr->p_paddr / opb;
3398
472k
      newsect->size = hdr->p_filesz;
3399
472k
      newsect->filepos = hdr->p_offset;
3400
472k
      newsect->flags |= SEC_HAS_CONTENTS;
3401
472k
      newsect->alignment_power = bfd_log2 (hdr->p_align);
3402
472k
      if (hdr->p_type == PT_LOAD)
3403
6.13k
  {
3404
6.13k
    newsect->flags |= SEC_ALLOC;
3405
6.13k
    newsect->flags |= SEC_LOAD;
3406
6.13k
    if (hdr->p_flags & PF_X)
3407
2.54k
      {
3408
        /* FIXME: all we known is that it has execute PERMISSION,
3409
     may be data.  */
3410
2.54k
        newsect->flags |= SEC_CODE;
3411
2.54k
      }
3412
6.13k
  }
3413
472k
      if (!(hdr->p_flags & PF_W))
3414
345k
  {
3415
345k
    newsect->flags |= SEC_READONLY;
3416
345k
  }
3417
472k
    }
3418
3419
556k
  if (hdr->p_memsz > hdr->p_filesz)
3420
210k
    {
3421
210k
      bfd_vma align;
3422
3423
210k
      sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "b" : "");
3424
210k
      len = strlen (namebuf) + 1;
3425
210k
      name = (char *) bfd_alloc (abfd, len);
3426
210k
      if (!name)
3427
0
  return false;
3428
210k
      memcpy (name, namebuf, len);
3429
210k
      newsect = bfd_make_section (abfd, name);
3430
210k
      if (newsect == NULL)
3431
0
  return false;
3432
210k
      newsect->vma = (hdr->p_vaddr + hdr->p_filesz) / opb;
3433
210k
      newsect->lma = (hdr->p_paddr + hdr->p_filesz) / opb;
3434
210k
      newsect->size = hdr->p_memsz - hdr->p_filesz;
3435
210k
      newsect->filepos = hdr->p_offset + hdr->p_filesz;
3436
210k
      align = newsect->vma & -newsect->vma;
3437
210k
      if (align == 0 || align > hdr->p_align)
3438
46.2k
  align = hdr->p_align;
3439
210k
      newsect->alignment_power = bfd_log2 (align);
3440
210k
      if (hdr->p_type == PT_LOAD)
3441
4.98k
  {
3442
4.98k
    newsect->flags |= SEC_ALLOC;
3443
4.98k
    if (hdr->p_flags & PF_X)
3444
1.73k
      newsect->flags |= SEC_CODE;
3445
4.98k
  }
3446
210k
      if (!(hdr->p_flags & PF_W))
3447
157k
  newsect->flags |= SEC_READONLY;
3448
210k
    }
3449
3450
556k
  return true;
3451
556k
}
3452
3453
static bool
3454
_bfd_elf_core_find_build_id (bfd *templ, bfd_vma offset)
3455
8.08k
{
3456
  /* The return value is ignored.  Build-ids are considered optional.  */
3457
8.08k
  if (templ->xvec->flavour == bfd_target_elf_flavour)
3458
8.08k
    return (*get_elf_backend_data (templ)->elf_backend_core_find_build_id)
3459
8.08k
      (templ, offset);
3460
0
  return false;
3461
8.08k
}
3462
3463
bool
3464
bfd_section_from_phdr (bfd *abfd, Elf_Internal_Phdr *hdr, int hdr_index)
3465
556k
{
3466
556k
  elf_backend_data *bed;
3467
3468
556k
  switch (hdr->p_type)
3469
556k
    {
3470
111k
    case PT_NULL:
3471
111k
      return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "null");
3472
3473
8.09k
    case PT_LOAD:
3474
8.09k
      if (! _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "load"))
3475
0
  return false;
3476
8.09k
      if (bfd_get_format (abfd) == bfd_core && abfd->build_id == NULL)
3477
8.08k
  _bfd_elf_core_find_build_id (abfd, hdr->p_offset);
3478
8.09k
      return true;
3479
3480
2.49k
    case PT_DYNAMIC:
3481
2.49k
      return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "dynamic");
3482
3483
1.06k
    case PT_INTERP:
3484
1.06k
      return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "interp");
3485
3486
677
    case PT_NOTE:
3487
677
      if (! _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "note"))
3488
0
  return false;
3489
677
      if (! _bfd_elf_read_notes (abfd, hdr->p_offset, hdr->p_filesz,
3490
677
         hdr->p_align))
3491
295
  return false;
3492
382
      return true;
3493
3494
570
    case PT_SHLIB:
3495
570
      return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "shlib");
3496
3497
7.30k
    case PT_PHDR:
3498
7.30k
      return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "phdr");
3499
3500
69
    case PT_GNU_EH_FRAME:
3501
69
      return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index,
3502
69
                "eh_frame_hdr");
3503
3504
50
    case PT_GNU_STACK:
3505
50
      return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "stack");
3506
3507
151
    case PT_GNU_RELRO:
3508
151
      return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "relro");
3509
3510
6
    case PT_GNU_SFRAME:
3511
6
      return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index,
3512
6
                "sframe");
3513
3514
424k
    default:
3515
      /* Check for any processor-specific program segment types.  */
3516
424k
      bed = get_elf_backend_data (abfd);
3517
424k
      return bed->elf_backend_section_from_phdr (abfd, hdr, hdr_index, "proc");
3518
556k
    }
3519
556k
}
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
26
{
3527
26
  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
26
  else
3533
26
    return elf_section_data (sec)->rela.hdr;
3534
26
}
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
255
{
3542
255
  char *name = (char *) bfd_alloc (abfd,
3543
255
           sizeof ".rela" + strlen (sec_name));
3544
255
  if (name == NULL)
3545
0
    return false;
3546
3547
255
  sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", sec_name);
3548
255
  rel_hdr->sh_name =
3549
255
    (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), name,
3550
255
          false);
3551
255
  if (rel_hdr->sh_name == (unsigned int) -1)
3552
0
    return false;
3553
3554
255
  return true;
3555
255
}
3556
3557
/* Allocate and initialize a section-header for a new reloc section,
3558
   containing relocations against ASECT.  It is stored in RELDATA.  If
3559
   USE_RELA_P is TRUE, we use RELA relocations; otherwise, we use REL
3560
   relocations.  */
3561
3562
static bool
3563
_bfd_elf_init_reloc_shdr (bfd *abfd,
3564
        struct bfd_elf_section_reloc_data *reldata,
3565
        const char *sec_name,
3566
        bool use_rela_p,
3567
        bool delay_sh_name_p)
3568
255
{
3569
255
  Elf_Internal_Shdr *rel_hdr;
3570
255
  elf_backend_data *bed = get_elf_backend_data (abfd);
3571
3572
255
  BFD_ASSERT (reldata->hdr == NULL);
3573
255
  rel_hdr = bfd_zalloc (abfd, sizeof (*rel_hdr));
3574
255
  if (rel_hdr == NULL)
3575
0
    return false;
3576
255
  reldata->hdr = rel_hdr;
3577
3578
255
  if (delay_sh_name_p)
3579
0
    rel_hdr->sh_name = (unsigned int) -1;
3580
255
  else if (!_bfd_elf_set_reloc_sh_name (abfd, rel_hdr, sec_name,
3581
255
          use_rela_p))
3582
0
    return false;
3583
255
  rel_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL;
3584
255
  rel_hdr->sh_entsize = (use_rela_p
3585
255
       ? bed->s->sizeof_rela
3586
255
       : bed->s->sizeof_rel);
3587
255
  rel_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
3588
255
  rel_hdr->sh_flags = 0;
3589
255
  rel_hdr->sh_addr = 0;
3590
255
  rel_hdr->sh_size = 0;
3591
255
  rel_hdr->sh_offset = 0;
3592
3593
255
  return true;
3594
255
}
3595
3596
/* Return the default section type based on the passed in section flags.  */
3597
3598
int
3599
bfd_elf_get_default_section_type (flagword flags)
3600
1.99k
{
3601
1.99k
  if ((flags & (SEC_ALLOC | SEC_IS_COMMON)) != 0
3602
1.55k
      && (flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
3603
40
    return SHT_NOBITS;
3604
1.95k
  return SHT_PROGBITS;
3605
1.99k
}
3606
3607
struct fake_section_arg
3608
{
3609
  struct bfd_link_info *link_info;
3610
  bool failed;
3611
};
3612
3613
/* Set up an ELF internal section header for a section.  */
3614
3615
static void
3616
elf_fake_sections (bfd *abfd, asection *asect, void *fsarg)
3617
1.36k
{
3618
1.36k
  struct fake_section_arg *arg = (struct fake_section_arg *)fsarg;
3619
1.36k
  elf_backend_data *bed = get_elf_backend_data (abfd);
3620
1.36k
  struct bfd_elf_section_data *esd = elf_section_data (asect);
3621
1.36k
  Elf_Internal_Shdr *this_hdr;
3622
1.36k
  unsigned int sh_type;
3623
1.36k
  const char *name = asect->name;
3624
1.36k
  bool delay_sh_name_p = false;
3625
1.36k
  bfd_vma mask;
3626
3627
1.36k
  if (arg->failed)
3628
0
    {
3629
      /* We already failed; just get out of the bfd_map_over_sections
3630
   loop.  */
3631
0
      return;
3632
0
    }
3633
3634
1.36k
  this_hdr = &esd->this_hdr;
3635
3636
  /* ld: compress DWARF debug sections with names: .debug_*.  */
3637
1.36k
  if (arg->link_info
3638
0
      && (abfd->flags & BFD_COMPRESS) != 0
3639
0
      && (asect->flags & SEC_DEBUGGING) != 0
3640
0
      && (asect->flags & SEC_ALLOC) == 0
3641
0
      && (asect->flags & SEC_HAS_CONTENTS) != 0
3642
0
      && name[1] == 'd'
3643
0
      && name[6] == '_')
3644
0
    {
3645
      /* If this section will be compressed, delay adding section
3646
   name to section name section after it is compressed in
3647
   _bfd_elf_assign_file_positions_for_non_load.  */
3648
0
      delay_sh_name_p = true;
3649
0
    }
3650
3651
1.36k
  if (delay_sh_name_p)
3652
0
    this_hdr->sh_name = (unsigned int) -1;
3653
1.36k
  else
3654
1.36k
    {
3655
1.36k
      this_hdr->sh_name
3656
1.36k
  = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
3657
1.36k
                name, false);
3658
1.36k
      if (this_hdr->sh_name == (unsigned int) -1)
3659
0
  {
3660
0
    arg->failed = true;
3661
0
    return;
3662
0
  }
3663
1.36k
    }
3664
3665
  /* Don't clear sh_flags. Assembler may set additional bits.  */
3666
3667
1.36k
  if ((asect->flags & SEC_ALLOC) != 0
3668
379
      || asect->user_set_vma)
3669
1.36k
    this_hdr->sh_addr = asect->vma * bfd_octets_per_byte (abfd, asect);
3670
0
  else
3671
0
    this_hdr->sh_addr = 0;
3672
3673
1.36k
  this_hdr->sh_offset = 0;
3674
1.36k
  this_hdr->sh_size = asect->size;
3675
1.36k
  this_hdr->sh_link = 0;
3676
  /* PR 17512: file: 0eb809fe, 8b0535ee.  */
3677
1.36k
  if (asect->alignment_power >= (sizeof (bfd_vma) * 8) - 1)
3678
0
    {
3679
0
      _bfd_error_handler
3680
  /* xgettext:c-format */
3681
0
  (_("%pB: error: alignment power %d of section `%pA' is too big"),
3682
0
   abfd, asect->alignment_power, asect);
3683
0
      arg->failed = true;
3684
0
      return;
3685
0
    }
3686
  /* Set sh_addralign to the highest power of two given by alignment
3687
     consistent with the section VMA.  Linker scripts can force VMA.  */
3688
1.36k
  mask = ((bfd_vma) 1 << asect->alignment_power) | this_hdr->sh_addr;
3689
1.36k
  this_hdr->sh_addralign = mask & -mask;
3690
  /* The sh_entsize and sh_info fields may have been set already by
3691
     copy_private_section_data.  */
3692
3693
1.36k
  this_hdr->bfd_section = asect;
3694
1.36k
  this_hdr->contents = NULL;
3695
3696
  /* If the section type is unspecified, we set it based on
3697
     asect->flags.  */
3698
1.36k
  if (asect->type != 0)
3699
0
    sh_type = asect->type;
3700
1.36k
  else if ((asect->flags & SEC_GROUP) != 0)
3701
17
    sh_type = SHT_GROUP;
3702
1.34k
  else
3703
1.34k
    sh_type = bfd_elf_get_default_section_type (asect->flags);
3704
3705
1.36k
  if (this_hdr->sh_type == SHT_NULL)
3706
3
    this_hdr->sh_type = sh_type;
3707
1.36k
  else if (this_hdr->sh_type == SHT_NOBITS
3708
40
     && sh_type == SHT_PROGBITS
3709
0
     && (asect->flags & SEC_ALLOC) != 0)
3710
0
    {
3711
      /* Warn if we are changing a NOBITS section to PROGBITS, but
3712
   allow the link to proceed.  This can happen when users link
3713
   non-bss input sections to bss output sections, or emit data
3714
   to a bss output section via a linker script.  */
3715
0
      _bfd_error_handler
3716
0
  (_("warning: section `%pA' type changed to PROGBITS"), asect);
3717
0
      this_hdr->sh_type = sh_type;
3718
0
    }
3719
3720
1.36k
  switch (this_hdr->sh_type)
3721
1.36k
    {
3722
100
    default:
3723
100
      break;
3724
3725
100
    case SHT_STRTAB:
3726
110
    case SHT_NOTE:
3727
150
    case SHT_NOBITS:
3728
998
    case SHT_PROGBITS:
3729
998
      break;
3730
3731
47
    case SHT_INIT_ARRAY:
3732
84
    case SHT_FINI_ARRAY:
3733
84
    case SHT_PREINIT_ARRAY:
3734
84
      this_hdr->sh_entsize = bed->s->arch_size / 8;
3735
84
      break;
3736
3737
20
    case SHT_HASH:
3738
20
      this_hdr->sh_entsize = bed->s->sizeof_hash_entry;
3739
20
      break;
3740
3741
23
    case SHT_DYNSYM:
3742
23
      this_hdr->sh_entsize = bed->s->sizeof_sym;
3743
23
      break;
3744
3745
25
    case SHT_DYNAMIC:
3746
25
      this_hdr->sh_entsize = bed->s->sizeof_dyn;
3747
25
      break;
3748
3749
77
    case SHT_RELA:
3750
77
      if (get_elf_backend_data (abfd)->may_use_rela_p)
3751
77
  this_hdr->sh_entsize = bed->s->sizeof_rela;
3752
77
      break;
3753
3754
0
     case SHT_REL:
3755
0
      if (get_elf_backend_data (abfd)->may_use_rel_p)
3756
0
  this_hdr->sh_entsize = bed->s->sizeof_rel;
3757
0
      break;
3758
3759
4
     case SHT_GNU_versym:
3760
4
      this_hdr->sh_entsize = sizeof (Elf_External_Versym);
3761
4
      break;
3762
3763
0
     case SHT_GNU_verdef:
3764
0
      this_hdr->sh_entsize = 0;
3765
      /* objcopy or strip will copy over sh_info, but may not set
3766
   cverdefs.  The linker will set cverdefs, but sh_info will be
3767
   zero.  */
3768
0
      if (this_hdr->sh_info == 0)
3769
0
  this_hdr->sh_info = elf_tdata (abfd)->cverdefs;
3770
0
      else
3771
0
  BFD_ASSERT (elf_tdata (abfd)->cverdefs == 0
3772
0
        || this_hdr->sh_info == elf_tdata (abfd)->cverdefs);
3773
0
      break;
3774
3775
4
    case SHT_GNU_verneed:
3776
4
      this_hdr->sh_entsize = 0;
3777
      /* objcopy or strip will copy over sh_info, but may not set
3778
   cverrefs.  The linker will set cverrefs, but sh_info will be
3779
   zero.  */
3780
4
      if (this_hdr->sh_info == 0)
3781
0
  this_hdr->sh_info = elf_tdata (abfd)->cverrefs;
3782
4
      else
3783
4
  BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0
3784
4
        || this_hdr->sh_info == elf_tdata (abfd)->cverrefs);
3785
4
      break;
3786
3787
17
    case SHT_GROUP:
3788
17
      this_hdr->sh_entsize = GRP_ENTRY_SIZE;
3789
17
      break;
3790
3791
12
    case SHT_GNU_HASH:
3792
12
      this_hdr->sh_entsize = bed->s->arch_size == 64 ? 0 : 4;
3793
12
      break;
3794
1.36k
    }
3795
3796
1.36k
  if ((asect->flags & SEC_ALLOC) != 0)
3797
985
    this_hdr->sh_flags |= SHF_ALLOC;
3798
1.36k
  if ((asect->flags & SEC_READONLY) == 0)
3799
352
    this_hdr->sh_flags |= SHF_WRITE;
3800
1.36k
  if ((asect->flags & SEC_CODE) != 0)
3801
253
    this_hdr->sh_flags |= SHF_EXECINSTR;
3802
1.36k
  if ((asect->flags & SEC_MERGE) != 0)
3803
148
    {
3804
148
      this_hdr->sh_flags |= SHF_MERGE;
3805
148
      this_hdr->sh_entsize = asect->entsize;
3806
148
    }
3807
1.36k
  if ((asect->flags & SEC_STRINGS) != 0)
3808
145
    {
3809
145
      this_hdr->sh_flags |= SHF_STRINGS;
3810
145
      this_hdr->sh_entsize = asect->entsize;
3811
145
    }
3812
1.36k
  if ((asect->flags & SEC_GROUP) == 0 && elf_group_name (asect) != NULL)
3813
42
    this_hdr->sh_flags |= SHF_GROUP;
3814
1.36k
  if ((asect->flags & SEC_THREAD_LOCAL) != 0)
3815
21
    {
3816
21
      this_hdr->sh_flags |= SHF_TLS;
3817
21
      if (asect->size == 0
3818
5
    && (asect->flags & SEC_HAS_CONTENTS) == 0)
3819
0
  {
3820
0
    struct bfd_link_order *o = asect->map_tail.link_order;
3821
3822
0
    this_hdr->sh_size = 0;
3823
0
    if (o != NULL)
3824
0
      {
3825
0
        this_hdr->sh_size = o->offset + o->size;
3826
0
        if (this_hdr->sh_size != 0)
3827
0
    this_hdr->sh_type = SHT_NOBITS;
3828
0
      }
3829
0
  }
3830
21
    }
3831
1.36k
  if ((asect->flags & (SEC_GROUP | SEC_EXCLUDE)) == SEC_EXCLUDE)
3832
49
    this_hdr->sh_flags |= SHF_EXCLUDE;
3833
3834
1.36k
  if (this_hdr->sh_entsize == 0)
3835
883
    this_hdr->sh_entsize = asect->entsize;
3836
3837
  /* If the section has relocs, set up a section header for the
3838
     SHT_REL[A] section.  If two relocation sections are required for
3839
     this section, it is up to the processor-specific back-end to
3840
     create the other.  */
3841
1.36k
  if ((asect->flags & SEC_RELOC) != 0)
3842
255
    {
3843
      /* When doing a relocatable link, create both REL and RELA sections if
3844
   needed.  */
3845
255
      if (arg->link_info
3846
    /* Do the normal setup if we wouldn't create any sections here.  */
3847
0
    && esd->rel.count + esd->rela.count > 0
3848
0
    && (bfd_link_relocatable (arg->link_info)
3849
0
        || arg->link_info->emitrelocations))
3850
0
  {
3851
0
    if (esd->rel.count && esd->rel.hdr == NULL
3852
0
        && !_bfd_elf_init_reloc_shdr (abfd, &esd->rel, name,
3853
0
              false, delay_sh_name_p))
3854
0
      {
3855
0
        arg->failed = true;
3856
0
        return;
3857
0
      }
3858
0
    if (esd->rela.count && esd->rela.hdr == NULL
3859
0
        && !_bfd_elf_init_reloc_shdr (abfd, &esd->rela, name,
3860
0
              true, delay_sh_name_p))
3861
0
      {
3862
0
        arg->failed = true;
3863
0
        return;
3864
0
      }
3865
0
  }
3866
255
      else if (!_bfd_elf_init_reloc_shdr (abfd,
3867
255
            (asect->use_rela_p
3868
255
             ? &esd->rela : &esd->rel),
3869
255
            name,
3870
255
            asect->use_rela_p,
3871
255
            delay_sh_name_p))
3872
0
  {
3873
0
    arg->failed = true;
3874
0
    return;
3875
0
  }
3876
255
    }
3877
3878
  /* Check for processor-specific section types.  */
3879
1.36k
  sh_type = this_hdr->sh_type;
3880
1.36k
  if (bed->elf_backend_fake_sections
3881
1.36k
      && !(*bed->elf_backend_fake_sections) (abfd, this_hdr, asect))
3882
0
    {
3883
0
      arg->failed = true;
3884
0
      return;
3885
0
    }
3886
3887
1.36k
  if (sh_type == SHT_NOBITS && asect->size != 0)
3888
35
    {
3889
      /* Don't change the header type from NOBITS if we are being
3890
   called for objcopy --only-keep-debug.  */
3891
35
      this_hdr->sh_type = sh_type;
3892
35
    }
3893
1.36k
}
3894
3895
/* Fill in the contents of a SHT_GROUP section.  Called from
3896
   _bfd_elf_compute_section_file_positions for gas, objcopy, and
3897
   when ELF targets use the generic linker, ld.  Called for ld -r
3898
   from bfd_elf_final_link.  */
3899
3900
void
3901
bfd_elf_set_group_contents (bfd *abfd, asection *sec, void *failedptrarg)
3902
1.36k
{
3903
1.36k
  bool *failedptr = (bool *) failedptrarg;
3904
1.36k
  asection *elt, *first;
3905
1.36k
  unsigned char *loc;
3906
1.36k
  bool gas;
3907
3908
  /* Ignore linker created group section.  See elfNN_ia64_object_p in
3909
     elfxx-ia64.c.  */
3910
1.36k
  if ((sec->flags & (SEC_GROUP | SEC_LINKER_CREATED)) != SEC_GROUP
3911
17
      || sec->size == 0
3912
17
      || *failedptr)
3913
1.34k
    return;
3914
3915
15
  if (elf_section_data (sec)->this_hdr.sh_info == 0)
3916
15
    {
3917
15
      unsigned long symindx = 0;
3918
3919
      /* elf_group_id will have been set up by objcopy and the
3920
   generic linker.  */
3921
15
      if (elf_group_id (sec) != NULL)
3922
15
  symindx = elf_group_id (sec)->udata.i;
3923
3924
15
      if (symindx == 0)
3925
0
  {
3926
    /* If called from the assembler, swap_out_syms will have set up
3927
       elf_section_syms.
3928
       PR 25699: A corrupt input file could contain bogus group info.  */
3929
0
    if (sec->index >= elf_num_section_syms (abfd)
3930
0
        || elf_section_syms (abfd)[sec->index] == NULL)
3931
0
      {
3932
0
        *failedptr = true;
3933
0
        return;
3934
0
      }
3935
0
    symindx = elf_section_syms (abfd)[sec->index]->udata.i;
3936
0
  }
3937
15
      elf_section_data (sec)->this_hdr.sh_info = symindx;
3938
15
    }
3939
0
  else if (elf_section_data (sec)->this_hdr.sh_info == (unsigned int) -2)
3940
0
    {
3941
      /* The ELF backend linker sets sh_info to -2 when the group
3942
   signature symbol is global, and thus the index can't be
3943
   set until all local symbols are output.  */
3944
0
      asection *igroup;
3945
0
      struct bfd_elf_section_data *sec_data;
3946
0
      unsigned long symndx;
3947
0
      unsigned long extsymoff;
3948
0
      struct elf_link_hash_entry *h;
3949
3950
      /* The point of this little dance to the first SHF_GROUP section
3951
   then back to the SHT_GROUP section is that this gets us to
3952
   the SHT_GROUP in the input object.  */
3953
0
      igroup = elf_sec_group (elf_next_in_group (sec));
3954
0
      sec_data = elf_section_data (igroup);
3955
0
      symndx = sec_data->this_hdr.sh_info;
3956
0
      extsymoff = 0;
3957
0
      if (!elf_bad_symtab (igroup->owner))
3958
0
  {
3959
0
    Elf_Internal_Shdr *symtab_hdr;
3960
3961
0
    symtab_hdr = &elf_tdata (igroup->owner)->symtab_hdr;
3962
0
    extsymoff = symtab_hdr->sh_info;
3963
0
  }
3964
0
      h = elf_sym_hashes (igroup->owner)[symndx - extsymoff];
3965
0
      while (h->root.type == bfd_link_hash_indirect
3966
0
       || h->root.type == bfd_link_hash_warning)
3967
0
  h = (struct elf_link_hash_entry *) h->root.u.i.link;
3968
3969
0
      elf_section_data (sec)->this_hdr.sh_info = h->indx;
3970
0
    }
3971
3972
  /* The contents won't be allocated for "ld -r" or objcopy.  */
3973
15
  gas = true;
3974
15
  if (sec->contents == NULL)
3975
15
    {
3976
15
      gas = false;
3977
15
      sec->contents = (unsigned char *) bfd_alloc (abfd, sec->size);
3978
3979
      /* Arrange for the section to be written out.  */
3980
15
      elf_section_data (sec)->this_hdr.contents = sec->contents;
3981
15
      if (sec->contents == NULL)
3982
0
  {
3983
0
    *failedptr = true;
3984
0
    return;
3985
0
  }
3986
15
      sec->alloced = 1;
3987
15
    }
3988
3989
15
  loc = sec->contents + sec->size;
3990
3991
  /* Get the pointer to the first section in the group that gas
3992
     squirreled away here.  objcopy arranges for this to be set to the
3993
     start of the input section group.  */
3994
15
  first = elt = elf_next_in_group (sec);
3995
3996
  /* First element is a flag word.  Rest of section is elf section
3997
     indices for all the sections of the group.  Write them backwards
3998
     just to keep the group in the same order as given in .section
3999
     directives, not that it matters.  */
4000
37
  while (elt != NULL)
4001
37
    {
4002
37
      asection *s;
4003
4004
37
      s = elt;
4005
37
      if (!gas)
4006
37
  s = s->output_section;
4007
37
      if (s != NULL
4008
37
    && !bfd_is_abs_section (s))
4009
37
  {
4010
37
    struct bfd_elf_section_data *elf_sec = elf_section_data (s);
4011
37
    struct bfd_elf_section_data *input_elf_sec = elf_section_data (elt);
4012
4013
37
    if (elf_sec->rel.hdr != NULL
4014
0
        && (gas
4015
0
      || (input_elf_sec->rel.hdr != NULL
4016
0
          && input_elf_sec->rel.hdr->sh_flags & SHF_GROUP) != 0))
4017
0
      {
4018
0
        elf_sec->rel.hdr->sh_flags |= SHF_GROUP;
4019
0
        loc -= 4;
4020
0
        if (loc == sec->contents)
4021
0
    break;
4022
0
        H_PUT_32 (abfd, elf_sec->rel.idx, loc);
4023
0
      }
4024
37
    if (elf_sec->rela.hdr != NULL
4025
29
        && (gas
4026
29
      || (input_elf_sec->rela.hdr != NULL
4027
29
          && input_elf_sec->rela.hdr->sh_flags & SHF_GROUP) != 0))
4028
29
      {
4029
29
        elf_sec->rela.hdr->sh_flags |= SHF_GROUP;
4030
29
        loc -= 4;
4031
29
        if (loc == sec->contents)
4032
0
    break;
4033
29
        H_PUT_32 (abfd, elf_sec->rela.idx, loc);
4034
29
      }
4035
37
    loc -= 4;
4036
37
    if (loc == sec->contents)
4037
0
      break;
4038
37
    H_PUT_32 (abfd, elf_sec->this_idx, loc);
4039
37
  }
4040
37
      elt = elf_next_in_group (elt);
4041
37
      if (elt == first)
4042
15
  break;
4043
37
    }
4044
4045
  /* We should always get here with loc == sec->contents + 4.  Return
4046
     an error for bogus SHT_GROUP sections.  */
4047
15
  loc -= 4;
4048
15
  if (loc != sec->contents)
4049
2
    {
4050
      /* xgettext:c-format */
4051
2
      _bfd_error_handler (_("%pB: corrupted group section: `%pA'"),
4052
2
        abfd, sec);
4053
2
      bfd_set_error (bfd_error_bad_value);
4054
2
      *failedptr = true;
4055
2
      return;
4056
2
    }
4057
4058
13
  H_PUT_32 (abfd, sec->flags & SEC_LINK_ONCE ? GRP_COMDAT : 0, loc);
4059
13
}
4060
4061
/* Given NAME, the name of a relocation section stripped of its
4062
   .rel/.rela prefix, return the section in ABFD to which the
4063
   relocations apply.  */
4064
4065
asection *
4066
_bfd_elf_plt_get_reloc_section (bfd *abfd, const char *name)
4067
51
{
4068
  /* If a target needs .got.plt section, relocations in rela.plt/rel.plt
4069
     section likely apply to .got.plt or .got section.  */
4070
51
  if (get_elf_backend_data (abfd)->want_got_plt
4071
39
      && streq (name, ".plt"))
4072
15
    {
4073
15
      asection *sec;
4074
4075
15
      name = ".got.plt";
4076
15
      sec = bfd_get_section_by_name (abfd, name);
4077
15
      if (sec != NULL)
4078
15
  return sec;
4079
0
      name = ".got";
4080
0
    }
4081
4082
36
  return bfd_get_section_by_name (abfd, name);
4083
51
}
4084
4085
/* Return the section to which RELOC_SEC applies.  */
4086
4087
static asection *
4088
elf_get_reloc_section (asection *reloc_sec)
4089
77
{
4090
77
  const char *name;
4091
77
  unsigned int type;
4092
77
  bfd *abfd;
4093
77
  elf_backend_data *bed;
4094
4095
77
  type = elf_section_data (reloc_sec)->this_hdr.sh_type;
4096
77
  if (type != SHT_REL && type != SHT_RELA)
4097
0
    return NULL;
4098
4099
  /* We look up the section the relocs apply to by name.  */
4100
77
  name = reloc_sec->name;
4101
77
  if (!startswith (name, ".rel"))
4102
21
    return NULL;
4103
56
  name += 4;
4104
56
  if (type == SHT_RELA && *name++ != 'a')
4105
4
    return NULL;
4106
4107
52
  abfd = reloc_sec->owner;
4108
52
  bed = get_elf_backend_data (abfd);
4109
52
  return bed->get_reloc_section (abfd, name);
4110
56
}
4111
4112
/* Assign all ELF section numbers.  The dummy first section is handled here
4113
   too.  The link/info pointers for the standard section types are filled
4114
   in here too, while we're at it.  LINK_INFO will be 0 when arriving
4115
   here for gas, objcopy, and when using the generic ELF linker.  */
4116
4117
static bool
4118
assign_section_numbers (bfd *abfd, struct bfd_link_info *link_info)
4119
159
{
4120
159
  struct elf_obj_tdata *t = elf_tdata (abfd);
4121
159
  asection *sec;
4122
159
  unsigned int section_number;
4123
159
  Elf_Internal_Shdr **i_shdrp;
4124
159
  struct bfd_elf_section_data *d;
4125
159
  bool need_symtab;
4126
159
  size_t amt;
4127
4128
159
  section_number = 1;
4129
4130
159
  _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd));
4131
4132
  /* SHT_GROUP sections are in relocatable files only.  */
4133
159
  if (link_info == NULL || !link_info->resolve_section_groups)
4134
159
    {
4135
159
      size_t reloc_count = 0;
4136
4137
      /* Put SHT_GROUP sections first.  */
4138
1.52k
      for (sec = abfd->sections; sec != NULL; sec = sec->next)
4139
1.36k
  {
4140
1.36k
    d = elf_section_data (sec);
4141
4142
1.36k
    if (d->this_hdr.sh_type == SHT_GROUP)
4143
17
      {
4144
17
        if (sec->flags & SEC_LINKER_CREATED)
4145
0
    {
4146
      /* Remove the linker created SHT_GROUP sections.  */
4147
0
      bfd_section_list_remove (abfd, sec);
4148
0
      abfd->section_count--;
4149
0
    }
4150
17
        else
4151
17
    d->this_idx = section_number++;
4152
17
      }
4153
4154
    /* Count relocations.  */
4155
1.36k
    reloc_count += sec->reloc_count;
4156
1.36k
  }
4157
4158
      /* Set/clear HAS_RELOC depending on whether there are relocations.  */
4159
159
      if (reloc_count == 0)
4160
126
  abfd->flags &= ~HAS_RELOC;
4161
33
      else
4162
33
  abfd->flags |= HAS_RELOC;
4163
159
    }
4164
4165
1.52k
  for (sec = abfd->sections; sec; sec = sec->next)
4166
1.36k
    {
4167
1.36k
      d = elf_section_data (sec);
4168
4169
1.36k
      if (d->this_hdr.sh_type != SHT_GROUP)
4170
1.34k
  d->this_idx = section_number++;
4171
1.36k
      if (d->this_hdr.sh_name != (unsigned int) -1)
4172
1.36k
  _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->this_hdr.sh_name);
4173
1.36k
      if (d->rel.hdr)
4174
0
  {
4175
0
    d->rel.idx = section_number++;
4176
0
    if (d->rel.hdr->sh_name != (unsigned int) -1)
4177
0
      _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel.hdr->sh_name);
4178
0
  }
4179
1.36k
      else
4180
1.36k
  d->rel.idx = 0;
4181
4182
1.36k
      if (d->rela.hdr)
4183
255
  {
4184
255
    d->rela.idx = section_number++;
4185
255
    if (d->rela.hdr->sh_name != (unsigned int) -1)
4186
255
      _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rela.hdr->sh_name);
4187
255
  }
4188
1.10k
      else
4189
1.10k
  d->rela.idx = 0;
4190
1.36k
    }
4191
4192
159
  need_symtab = (bfd_get_symcount (abfd) > 0
4193
103
     || (link_info == NULL
4194
103
         && ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC))
4195
103
       == HAS_RELOC)));
4196
159
  if (need_symtab)
4197
56
    {
4198
56
      elf_onesymtab (abfd) = section_number++;
4199
56
      _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->symtab_hdr.sh_name);
4200
56
      if (section_number > ((SHN_LORESERVE - 2) & 0xFFFF))
4201
0
  {
4202
0
    elf_section_list *entry;
4203
4204
0
    BFD_ASSERT (elf_symtab_shndx_list (abfd) == NULL);
4205
4206
0
    entry = bfd_zalloc (abfd, sizeof (*entry));
4207
0
    entry->ndx = section_number++;
4208
0
    elf_symtab_shndx_list (abfd) = entry;
4209
0
    entry->hdr.sh_name
4210
0
      = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
4211
0
              ".symtab_shndx", false);
4212
0
    if (entry->hdr.sh_name == (unsigned int) -1)
4213
0
      return false;
4214
0
  }
4215
56
      elf_strtab_sec (abfd) = section_number++;
4216
56
      _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->strtab_hdr.sh_name);
4217
56
    }
4218
4219
159
  elf_shstrtab_sec (abfd) = section_number++;
4220
159
  _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->shstrtab_hdr.sh_name);
4221
159
  elf_elfheader (abfd)->e_shstrndx = elf_shstrtab_sec (abfd);
4222
4223
159
  if (section_number >= SHN_LORESERVE)
4224
0
    {
4225
      /* xgettext:c-format */
4226
0
      _bfd_error_handler (_("%pB: too many sections: %u"),
4227
0
        abfd, section_number);
4228
0
      return false;
4229
0
    }
4230
4231
159
  elf_numsections (abfd) = section_number;
4232
159
  elf_elfheader (abfd)->e_shnum = section_number;
4233
4234
  /* Set up the list of section header pointers, in agreement with the
4235
     indices.  */
4236
159
  amt = section_number * sizeof (Elf_Internal_Shdr *);
4237
159
  i_shdrp = (Elf_Internal_Shdr **) bfd_zalloc (abfd, amt);
4238
159
  if (i_shdrp == NULL)
4239
0
    return false;
4240
4241
159
  i_shdrp[0] = (Elf_Internal_Shdr *) bfd_zalloc (abfd,
4242
159
             sizeof (Elf_Internal_Shdr));
4243
159
  if (i_shdrp[0] == NULL)
4244
0
    {
4245
0
      bfd_release (abfd, i_shdrp);
4246
0
      return false;
4247
0
    }
4248
4249
159
  elf_elfsections (abfd) = i_shdrp;
4250
4251
159
  i_shdrp[elf_shstrtab_sec (abfd)] = &t->shstrtab_hdr;
4252
159
  if (need_symtab)
4253
56
    {
4254
56
      i_shdrp[elf_onesymtab (abfd)] = &t->symtab_hdr;
4255
56
      if (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF))
4256
0
  {
4257
0
    elf_section_list * entry = elf_symtab_shndx_list (abfd);
4258
0
    BFD_ASSERT (entry != NULL);
4259
0
    i_shdrp[entry->ndx] = & entry->hdr;
4260
0
    entry->hdr.sh_link = elf_onesymtab (abfd);
4261
0
  }
4262
56
      i_shdrp[elf_strtab_sec (abfd)] = &t->strtab_hdr;
4263
56
      t->symtab_hdr.sh_link = elf_strtab_sec (abfd);
4264
56
    }
4265
4266
1.52k
  for (sec = abfd->sections; sec; sec = sec->next)
4267
1.36k
    {
4268
1.36k
      asection *s;
4269
4270
1.36k
      d = elf_section_data (sec);
4271
4272
1.36k
      i_shdrp[d->this_idx] = &d->this_hdr;
4273
1.36k
      if (d->rel.idx != 0)
4274
0
  i_shdrp[d->rel.idx] = d->rel.hdr;
4275
1.36k
      if (d->rela.idx != 0)
4276
255
  i_shdrp[d->rela.idx] = d->rela.hdr;
4277
4278
      /* Fill in the sh_link and sh_info fields while we're at it.  */
4279
4280
      /* sh_link of a reloc section is the section index of the symbol
4281
   table.  sh_info is the section index of the section to which
4282
   the relocation entries apply.  */
4283
1.36k
      if (d->rel.idx != 0)
4284
0
  {
4285
0
    d->rel.hdr->sh_link = elf_onesymtab (abfd);
4286
0
    d->rel.hdr->sh_info = d->this_idx;
4287
0
    d->rel.hdr->sh_flags |= SHF_INFO_LINK;
4288
0
  }
4289
1.36k
      if (d->rela.idx != 0)
4290
255
  {
4291
255
    d->rela.hdr->sh_link = elf_onesymtab (abfd);
4292
255
    d->rela.hdr->sh_info = d->this_idx;
4293
255
    d->rela.hdr->sh_flags |= SHF_INFO_LINK;
4294
255
  }
4295
4296
      /* We need to set up sh_link for SHF_LINK_ORDER.  */
4297
1.36k
      if ((d->this_hdr.sh_flags & SHF_LINK_ORDER) != 0)
4298
24
  {
4299
24
    s = elf_linked_to_section (sec);
4300
    /* We can now have a NULL linked section pointer.
4301
       This happens when the sh_link field is 0, which is done
4302
       when a linked to section is discarded but the linking
4303
       section has been retained for some reason.  */
4304
24
    if (s)
4305
0
      {
4306
        /* Check discarded linkonce section.  */
4307
0
        if (discarded_section (s))
4308
0
    {
4309
0
      asection *kept;
4310
0
      _bfd_error_handler
4311
        /* xgettext:c-format */
4312
0
        (_("%pB: sh_link of section `%pA' points to"
4313
0
           " discarded section `%pA' of `%pB'"),
4314
0
         abfd, d->this_hdr.bfd_section, s, s->owner);
4315
      /* Point to the kept section if it has the same
4316
         size as the discarded one.  */
4317
0
      kept = _bfd_elf_check_kept_section (s, link_info);
4318
0
      if (kept == NULL)
4319
0
        {
4320
0
          bfd_set_error (bfd_error_bad_value);
4321
0
          return false;
4322
0
        }
4323
0
      s = kept;
4324
0
    }
4325
        /* Handle objcopy. */
4326
0
        else if (s->output_section == NULL)
4327
0
    {
4328
0
      _bfd_error_handler
4329
        /* xgettext:c-format */
4330
0
        (_("%pB: sh_link of section `%pA' points to"
4331
0
           " removed section `%pA' of `%pB'"),
4332
0
         abfd, d->this_hdr.bfd_section, s, s->owner);
4333
0
      bfd_set_error (bfd_error_bad_value);
4334
0
      return false;
4335
0
    }
4336
0
        s = s->output_section;
4337
0
        d->this_hdr.sh_link
4338
0
    = _bfd_elf_section_from_bfd_section (abfd, s);
4339
0
      }
4340
24
  }
4341
4342
1.36k
      switch (d->this_hdr.sh_type)
4343
1.36k
  {
4344
0
  case SHT_REL:
4345
77
  case SHT_RELA:
4346
    /* sh_link is the section index of the symbol table.
4347
       sh_info is the section index of the section to which the
4348
       relocation entries apply.  */
4349
77
    if (d->this_hdr.sh_link == 0)
4350
77
      {
4351
        /* FIXME maybe: If this is a reloc section which we are
4352
     treating as a normal section then we likely should
4353
     not be assuming its sh_link is .dynsym or .symtab.  */
4354
77
        if ((sec->flags & SEC_ALLOC) != 0)
4355
42
    {
4356
42
      s = bfd_get_section_by_name (abfd, ".dynsym");
4357
42
      if (s != NULL)
4358
30
        d->this_hdr.sh_link = elf_section_data (s)->this_idx;
4359
42
    }
4360
35
        else
4361
35
    d->this_hdr.sh_link = elf_onesymtab (abfd);
4362
77
      }
4363
4364
77
    s = elf_get_reloc_section (sec);
4365
77
    if (s != NULL)
4366
36
      {
4367
36
        d->this_hdr.sh_info = elf_section_data (s)->this_idx;
4368
36
        d->this_hdr.sh_flags |= SHF_INFO_LINK;
4369
36
      }
4370
77
    break;
4371
4372
26
  case SHT_STRTAB:
4373
    /* We assume that a section named .stab*str is a stabs
4374
       string section.  We look for a section with the same name
4375
       but without the trailing ``str'', and set its sh_link
4376
       field to point to this section.  */
4377
26
    if (startswith (sec->name, ".stab")
4378
0
        && streq (sec->name + strlen (sec->name) - 3, "str"))
4379
0
      {
4380
0
        size_t len;
4381
0
        char *alc;
4382
4383
0
        len = strlen (sec->name);
4384
0
        alc = (char *) bfd_malloc (len - 2);
4385
0
        if (alc == NULL)
4386
0
    return false;
4387
0
        memcpy (alc, sec->name, len - 3);
4388
0
        alc[len - 3] = '\0';
4389
0
        s = bfd_get_section_by_name (abfd, alc);
4390
0
        free (alc);
4391
0
        if (s != NULL)
4392
0
    {
4393
0
      elf_section_data (s)->this_hdr.sh_link = d->this_idx;
4394
4395
      /* This is a .stab section.  */
4396
0
      elf_section_data (s)->this_hdr.sh_entsize = 12;
4397
0
    }
4398
0
      }
4399
26
    break;
4400
4401
26
  case SHT_DYNAMIC:
4402
48
  case SHT_DYNSYM:
4403
52
  case SHT_GNU_verneed:
4404
52
  case SHT_GNU_verdef:
4405
    /* sh_link is the section header index of the string table
4406
       used for the dynamic entries, or the symbol table, or the
4407
       version strings.  */
4408
52
    s = bfd_get_section_by_name (abfd, ".dynstr");
4409
52
    if (s != NULL)
4410
37
      d->this_hdr.sh_link = elf_section_data (s)->this_idx;
4411
52
    break;
4412
4413
0
  case SHT_GNU_LIBLIST:
4414
    /* sh_link is the section header index of the prelink library
4415
       list used for the dynamic entries, or the symbol table, or
4416
       the version strings.  */
4417
0
    s = bfd_get_section_by_name (abfd, ((sec->flags & SEC_ALLOC)
4418
0
                ? ".dynstr" : ".gnu.libstr"));
4419
0
    if (s != NULL)
4420
0
      d->this_hdr.sh_link = elf_section_data (s)->this_idx;
4421
0
    break;
4422
4423
20
  case SHT_HASH:
4424
32
  case SHT_GNU_HASH:
4425
36
  case SHT_GNU_versym:
4426
    /* sh_link is the section header index of the symbol table
4427
       this hash table or version table is for.  */
4428
36
    s = bfd_get_section_by_name (abfd, ".dynsym");
4429
36
    if (s != NULL)
4430
21
      d->this_hdr.sh_link = elf_section_data (s)->this_idx;
4431
36
    break;
4432
4433
17
  case SHT_GROUP:
4434
17
    d->this_hdr.sh_link = elf_onesymtab (abfd);
4435
1.36k
  }
4436
1.36k
    }
4437
4438
  /* Delay setting sh_name to _bfd_elf_write_object_contents so that
4439
     _bfd_elf_assign_file_positions_for_non_load can convert DWARF
4440
     debug section name from .debug_* to .zdebug_* if needed.  */
4441
4442
159
  return true;
4443
159
}
4444
4445
static bool
4446
sym_is_global (bfd *abfd, asymbol *sym)
4447
3.49k
{
4448
  /* If the backend has a special mapping, use it.  */
4449
3.49k
  elf_backend_data *bed = get_elf_backend_data (abfd);
4450
3.49k
  if (bed->elf_backend_sym_is_global)
4451
0
    return (*bed->elf_backend_sym_is_global) (abfd, sym);
4452
4453
3.49k
  return ((sym->flags & (BSF_GLOBAL | BSF_WEAK | BSF_GNU_UNIQUE)) != 0
4454
2.80k
    || bfd_is_und_section (bfd_asymbol_section (sym))
4455
1.39k
    || bfd_is_com_section (bfd_asymbol_section (sym)));
4456
3.49k
}
4457
4458
/* Filter global symbols of ABFD to include in the import library.  All
4459
   SYMCOUNT symbols of ABFD can be examined from their pointers in
4460
   SYMS.  Pointers of symbols to keep should be stored contiguously at
4461
   the beginning of that array.
4462
4463
   Returns the number of symbols to keep.  */
4464
4465
unsigned int
4466
_bfd_elf_filter_global_symbols (bfd *abfd, struct bfd_link_info *info,
4467
        asymbol **syms, long symcount)
4468
0
{
4469
0
  long src_count, dst_count = 0;
4470
4471
0
  for (src_count = 0; src_count < symcount; src_count++)
4472
0
    {
4473
0
      asymbol *sym = syms[src_count];
4474
0
      char *name = (char *) bfd_asymbol_name (sym);
4475
0
      struct bfd_link_hash_entry *h;
4476
4477
0
      if (!sym_is_global (abfd, sym))
4478
0
  continue;
4479
4480
0
      h = bfd_link_hash_lookup (info->hash, name, false, false, false);
4481
0
      if (h == NULL)
4482
0
  continue;
4483
0
      if (h->type != bfd_link_hash_defined && h->type != bfd_link_hash_defweak)
4484
0
  continue;
4485
0
      if (h->linker_def || h->ldscript_def)
4486
0
  continue;
4487
4488
0
      syms[dst_count++] = sym;
4489
0
    }
4490
4491
0
  syms[dst_count] = NULL;
4492
4493
0
  return dst_count;
4494
0
}
4495
4496
/* Don't output symbols for sections that are not going to be output,
4497
   that are duplicates or there is no BFD section.  */
4498
4499
static bool
4500
ignore_sym (asymbol *sym)
4501
6.52k
{
4502
6.52k
  if (sym == NULL)
4503
0
    return false;
4504
4505
6.52k
  if (sym->section == NULL)
4506
0
    return true;
4507
4508
6.52k
  if ((sym->flags & BSF_SECTION_SYM) != 0)
4509
3.31k
    {
4510
3.31k
      if ((sym->flags & BSF_SECTION_SYM_USED) == 0)
4511
2.88k
  return true;
4512
      /* With ld -r on generic elf targets it is possible to have
4513
   multiple section symbols in the output for a given section.
4514
   We'd like to get rid of all but the first one.  This drops
4515
   them if the first input section is non-zero size, but fails
4516
   to do so if the first input section is zero sized.  */
4517
426
      if (sym->section->output_offset != 0)
4518
0
  return true;
4519
426
    }
4520
4521
3.63k
  return discarded_section (sym->section);
4522
6.52k
}
4523
4524
/* Map symbol from it's internal number to the external number, moving
4525
   all local symbols to be at the head of the list.  */
4526
4527
static bool
4528
elf_map_symbols (bfd *abfd, unsigned int *pnum_locals)
4529
56
{
4530
56
  unsigned int symcount = bfd_get_symcount (abfd);
4531
56
  asymbol **syms = bfd_get_outsymbols (abfd);
4532
56
  asymbol **sect_syms;
4533
56
  unsigned int num_locals = 0;
4534
56
  unsigned int num_globals = 0;
4535
56
  unsigned int max_index = 0;
4536
56
  unsigned int idx;
4537
56
  asection *asect;
4538
56
  asymbol **new_syms;
4539
56
  size_t amt;
4540
4541
#ifdef DEBUG
4542
  fprintf (stderr, "elf_map_symbols\n");
4543
  fflush (stderr);
4544
#endif
4545
4546
1.23k
  for (asect = abfd->sections; asect; asect = asect->next)
4547
1.17k
    {
4548
1.17k
      if (max_index < asect->index)
4549
1.11k
  max_index = asect->index;
4550
1.17k
    }
4551
4552
56
  max_index++;
4553
56
  amt = max_index * sizeof (asymbol *);
4554
56
  sect_syms = (asymbol **) bfd_zalloc (abfd, amt);
4555
56
  if (sect_syms == NULL)
4556
0
    return false;
4557
56
  elf_section_syms (abfd) = sect_syms;
4558
56
  elf_num_section_syms (abfd) = max_index;
4559
4560
  /* Init sect_syms entries for any section symbols we have already
4561
     decided to output.  */
4562
1.99k
  for (idx = 0; idx < symcount; idx++)
4563
1.93k
    {
4564
1.93k
      asymbol *sym = syms[idx];
4565
4566
1.93k
      if ((sym->flags & BSF_SECTION_SYM) != 0
4567
329
    && sym->value == 0
4568
304
    && !ignore_sym (sym)
4569
138
    && !bfd_is_abs_section (sym->section))
4570
132
  {
4571
132
    asection *sec = sym->section;
4572
4573
132
    if (sec->owner != abfd)
4574
132
      {
4575
132
        sec = sec->output_section;
4576
132
        if (sec == NULL)
4577
0
    return false;
4578
132
      }
4579
4580
132
    sect_syms[sec->index] = syms[idx];
4581
132
  }
4582
1.93k
    }
4583
4584
  /* Classify all of the symbols.  */
4585
1.99k
  for (idx = 0; idx < symcount; idx++)
4586
1.93k
    {
4587
1.93k
      if (ignore_sym (syms[idx]))
4588
185
  continue;
4589
1.74k
      if (sym_is_global (abfd, syms[idx]))
4590
1.05k
  num_globals++;
4591
692
      else
4592
692
  num_locals++;
4593
1.74k
    }
4594
4595
  /* We will be adding a section symbol for each normal BFD section.  Most
4596
     sections will already have a section symbol in outsymbols, but
4597
     eg. SHT_GROUP sections will not, and we need the section symbol mapped
4598
     at least in that case.  */
4599
1.23k
  for (asect = abfd->sections; asect; asect = asect->next)
4600
1.17k
    {
4601
1.17k
      asymbol *sym = asect->symbol;
4602
      /* Don't include ignored section symbols.  */
4603
1.17k
      if (!ignore_sym (sym)
4604
0
    && sect_syms[asect->index] == NULL)
4605
0
  {
4606
0
    if (sym_is_global (abfd, asect->symbol))
4607
0
      num_globals++;
4608
0
    else
4609
0
      num_locals++;
4610
0
  }
4611
1.17k
    }
4612
4613
  /* Now sort the symbols so the local symbols are first.  */
4614
56
  amt = (num_locals + num_globals) * sizeof (asymbol *);
4615
56
  new_syms = (asymbol **) bfd_alloc (abfd, amt);
4616
56
  if (new_syms == NULL)
4617
0
    return false;
4618
4619
56
  unsigned int num_globals2 = 0;
4620
56
  unsigned int num_locals2 = 0;
4621
1.99k
  for (idx = 0; idx < symcount; idx++)
4622
1.93k
    {
4623
1.93k
      asymbol *sym = syms[idx];
4624
1.93k
      unsigned int i;
4625
4626
1.93k
      if (ignore_sym (sym))
4627
185
  continue;
4628
4629
1.74k
      if (sym_is_global (abfd, sym))
4630
1.05k
  i = num_locals + num_globals2++;
4631
692
      else
4632
692
  i = num_locals2++;
4633
1.74k
      new_syms[i] = sym;
4634
1.74k
      sym->udata.i = i + 1;
4635
1.74k
    }
4636
1.23k
  for (asect = abfd->sections; asect; asect = asect->next)
4637
1.17k
    {
4638
1.17k
      asymbol *sym = asect->symbol;
4639
1.17k
      if (!ignore_sym (sym)
4640
0
    && sect_syms[asect->index] == NULL)
4641
0
  {
4642
0
    unsigned int i;
4643
4644
0
    sect_syms[asect->index] = sym;
4645
0
    if (sym_is_global (abfd, sym))
4646
0
      i = num_locals + num_globals2++;
4647
0
    else
4648
0
      i = num_locals2++;
4649
0
    new_syms[i] = sym;
4650
0
    sym->udata.i = i + 1;
4651
0
  }
4652
1.17k
    }
4653
4654
56
  bfd_set_symtab (abfd, new_syms, num_locals + num_globals);
4655
4656
56
  *pnum_locals = num_locals;
4657
56
  return true;
4658
56
}
4659
4660
/* Assign a file position to a section, optionally aligning to the
4661
   required section alignment.  */
4662
4663
file_ptr
4664
_bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr *i_shdrp,
4665
             file_ptr offset,
4666
             bool align,
4667
             unsigned char log_file_align)
4668
1.45k
{
4669
1.45k
  if (i_shdrp->sh_addralign > 1)
4670
794
    {
4671
794
      file_ptr salign = i_shdrp->sh_addralign & -i_shdrp->sh_addralign;
4672
4673
794
      if (align)
4674
86
  offset = BFD_ALIGN (offset, salign);
4675
708
      else if (log_file_align)
4676
708
  {
4677
    /* Heuristic: Cap alignment at log_file_align.  */
4678
708
    file_ptr falign = 1u << log_file_align;
4679
4680
708
    offset = BFD_ALIGN (offset, salign < falign ? salign : falign);
4681
708
  }
4682
794
    }
4683
1.45k
  i_shdrp->sh_offset = offset;
4684
1.45k
  if (i_shdrp->bfd_section != NULL)
4685
805
    i_shdrp->bfd_section->filepos = offset;
4686
1.45k
  if (i_shdrp->sh_type != SHT_NOBITS)
4687
1.43k
    offset += i_shdrp->sh_size;
4688
1.45k
  return offset;
4689
1.45k
}
4690
4691
/* Compute the file positions we are going to put the sections at, and
4692
   otherwise prepare to begin writing out the ELF file.  If LINK_INFO
4693
   is not NULL, this is being called by the ELF backend linker.  */
4694
4695
bool
4696
_bfd_elf_compute_section_file_positions (bfd *abfd,
4697
           struct bfd_link_info *link_info)
4698
159
{
4699
159
  elf_backend_data *bed = get_elf_backend_data (abfd);
4700
159
  struct fake_section_arg fsargs;
4701
159
  bool failed;
4702
159
  struct elf_strtab_hash *strtab = NULL;
4703
159
  Elf_Internal_Shdr *shstrtab_hdr;
4704
159
  bool need_symtab;
4705
4706
159
  if (abfd->output_has_begun)
4707
0
    return true;
4708
4709
  /* Do any elf backend specific processing first.  */
4710
159
  if (bed->elf_backend_begin_write_processing)
4711
0
    (*bed->elf_backend_begin_write_processing) (abfd, link_info);
4712
4713
159
  if (!(*bed->elf_backend_init_file_header) (abfd, link_info))
4714
0
    return false;
4715
4716
159
  fsargs.failed = false;
4717
159
  fsargs.link_info = link_info;
4718
159
  bfd_map_over_sections (abfd, elf_fake_sections, &fsargs);
4719
159
  if (fsargs.failed)
4720
0
    return false;
4721
4722
159
  if (!assign_section_numbers (abfd, link_info))
4723
0
    return false;
4724
4725
  /* The backend linker builds symbol table information itself.  */
4726
159
  need_symtab = (link_info == NULL
4727
159
     && (bfd_get_symcount (abfd) > 0
4728
103
         || ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC))
4729
103
       == HAS_RELOC)));
4730
159
  if (need_symtab)
4731
56
    {
4732
      /* Non-zero if doing a relocatable link.  */
4733
56
      int relocatable_p = ! (abfd->flags & (EXEC_P | DYNAMIC));
4734
4735
56
      if (! swap_out_syms (abfd, &strtab, relocatable_p, link_info))
4736
0
  return false;
4737
56
    }
4738
4739
159
  failed = false;
4740
159
  if (link_info == NULL)
4741
159
    {
4742
159
      bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
4743
159
      if (failed)
4744
2
  goto err_free_strtab;
4745
159
    }
4746
4747
157
  shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr;
4748
  /* sh_name was set in init_file_header.  */
4749
157
  shstrtab_hdr->sh_type = SHT_STRTAB;
4750
  /* sh_flags, sh_addr, sh_entsize, sh_link, sh_info are all zeroed
4751
     when tdata is allocated.  */
4752
  /* sh_size is set in _bfd_elf_assign_file_positions_for_non_load.  */
4753
  /* sh_offset is set in _bfd_elf_assign_file_positions_for_non_load.  */
4754
157
  shstrtab_hdr->sh_addralign = 1;
4755
4756
157
  if (!assign_file_positions_except_relocs (abfd, link_info))
4757
4
    goto err_free_strtab;
4758
4759
153
  if (strtab != NULL)
4760
51
    {
4761
51
      file_ptr off;
4762
51
      Elf_Internal_Shdr *hdr;
4763
4764
51
      off = elf_next_file_pos (abfd);
4765
4766
51
      hdr = & elf_symtab_hdr (abfd);
4767
51
      off = _bfd_elf_assign_file_position_for_section (hdr, off, true, 0);
4768
4769
51
      if (elf_symtab_shndx_list (abfd) != NULL)
4770
0
  {
4771
0
    hdr = & elf_symtab_shndx_list (abfd)->hdr;
4772
0
    if (hdr->sh_size != 0)
4773
0
      off = _bfd_elf_assign_file_position_for_section (hdr, off, true, 0);
4774
    /* FIXME: What about other symtab_shndx sections in the list ?  */
4775
0
  }
4776
4777
51
      hdr = &elf_tdata (abfd)->strtab_hdr;
4778
51
      off = _bfd_elf_assign_file_position_for_section (hdr, off, true, 0);
4779
4780
51
      elf_next_file_pos (abfd) = off;
4781
4782
      /* Now that we know where the .strtab section goes, write it
4783
   out.  */
4784
51
      if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
4785
51
    || ! _bfd_elf_strtab_emit (abfd, strtab))
4786
0
  goto err_free_strtab;
4787
51
      _bfd_elf_strtab_free (strtab);
4788
51
    }
4789
4790
153
  abfd->output_has_begun = true;
4791
153
  return true;
4792
4793
6
 err_free_strtab:
4794
6
  if (strtab != NULL)
4795
5
    _bfd_elf_strtab_free (strtab);
4796
6
  return false;
4797
153
}
4798
4799
/* Retrieve .eh_frame_hdr.  Prior to size_dynamic_sections the
4800
   function effectively returns whether --eh-frame-hdr is given on the
4801
   command line.  After size_dynamic_sections the result reflects
4802
   whether .eh_frame_hdr will actually be output (sizing isn't done
4803
   until ldemul_after_allocation).  */
4804
4805
static asection *
4806
elf_eh_frame_hdr (const struct bfd_link_info *info)
4807
16
{
4808
16
  if (info != NULL && is_elf_hash_table (info->hash))
4809
0
    return elf_hash_table (info)->eh_info.hdr_sec;
4810
16
  return NULL;
4811
16
}
4812
4813
/* Make an initial estimate of the size of the program header.  If we
4814
   get the number wrong here, we'll redo section placement.  */
4815
4816
static bfd_size_type
4817
get_program_header_size (bfd *abfd, struct bfd_link_info *info)
4818
8
{
4819
8
  size_t segs;
4820
8
  asection *s;
4821
8
  elf_backend_data *bed;
4822
4823
  /* Assume we will need exactly two PT_LOAD segments: one for text
4824
     and one for data.  */
4825
8
  segs = 2;
4826
4827
8
  s = bfd_get_section_by_name (abfd, ".interp");
4828
8
  if (s != NULL && (s->flags & SEC_LOAD) != 0 && s->size != 0)
4829
0
    {
4830
      /* If we have a loadable interpreter section, we need a
4831
   PT_INTERP segment.  In this case, assume we also need a
4832
   PT_PHDR segment, although that may not be true for all
4833
   targets.  */
4834
0
      segs += 2;
4835
0
    }
4836
4837
8
  if (bfd_get_section_by_name (abfd, ".dynamic") != NULL)
4838
0
    {
4839
      /* We need a PT_DYNAMIC segment.  */
4840
0
      ++segs;
4841
0
    }
4842
4843
8
  if (info != NULL && info->relro)
4844
0
    {
4845
      /* We need a PT_GNU_RELRO segment.  */
4846
0
      ++segs;
4847
0
    }
4848
4849
8
  if (elf_eh_frame_hdr (info))
4850
0
    {
4851
      /* We need a PT_GNU_EH_FRAME segment.  */
4852
0
      ++segs;
4853
0
    }
4854
4855
8
  if (elf_stack_flags (abfd))
4856
0
    {
4857
      /* We need a PT_GNU_STACK segment.  */
4858
0
      ++segs;
4859
0
    }
4860
4861
8
  if (elf_sframe (abfd))
4862
0
    {
4863
      /* We need a PT_GNU_SFRAME segment.  */
4864
0
      ++segs;
4865
0
    }
4866
4867
8
  s = bfd_get_section_by_name (abfd,
4868
8
             NOTE_GNU_PROPERTY_SECTION_NAME);
4869
8
  if (s != NULL && s->size != 0)
4870
0
    {
4871
      /* We need a PT_GNU_PROPERTY segment.  */
4872
0
      ++segs;
4873
0
    }
4874
4875
38
  for (s = abfd->sections; s != NULL; s = s->next)
4876
30
    {
4877
30
      if ((s->flags & SEC_LOAD) != 0
4878
9
    && elf_section_type (s) == SHT_NOTE)
4879
0
  {
4880
0
    unsigned int alignment_power;
4881
    /* We need a PT_NOTE segment.  */
4882
0
    ++segs;
4883
    /* Try to create just one PT_NOTE segment for all adjacent
4884
       loadable SHT_NOTE sections.  gABI requires that within a
4885
       PT_NOTE segment (and also inside of each SHT_NOTE section)
4886
       each note should have the same alignment.  So we check
4887
       whether the sections are correctly aligned.  */
4888
0
    alignment_power = s->alignment_power;
4889
0
    while (s->next != NULL
4890
0
     && s->next->alignment_power == alignment_power
4891
0
     && (s->next->flags & SEC_LOAD) != 0
4892
0
     && elf_section_type (s->next) == SHT_NOTE)
4893
0
      s = s->next;
4894
0
  }
4895
30
    }
4896
4897
32
  for (s = abfd->sections; s != NULL; s = s->next)
4898
28
    {
4899
28
      if (s->flags & SEC_THREAD_LOCAL)
4900
4
  {
4901
    /* We need a PT_TLS segment.  */
4902
4
    ++segs;
4903
4
    break;
4904
4
  }
4905
28
    }
4906
4907
8
  bed = get_elf_backend_data (abfd);
4908
4909
8
  if ((abfd->flags & D_PAGED) != 0
4910
8
      && (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0)
4911
0
    {
4912
      /* Add a PT_GNU_MBIND segment for each mbind section.  */
4913
0
      bfd_vma commonpagesize;
4914
0
      unsigned int page_align_power;
4915
4916
0
      if (info != NULL)
4917
0
  commonpagesize = info->commonpagesize;
4918
0
      else
4919
0
  commonpagesize = bed->commonpagesize;
4920
0
      page_align_power = bfd_log2 (commonpagesize);
4921
0
      for (s = abfd->sections; s != NULL; s = s->next)
4922
0
  if (elf_section_flags (s) & SHF_GNU_MBIND)
4923
0
    {
4924
0
      if (elf_section_data (s)->this_hdr.sh_info > PT_GNU_MBIND_NUM)
4925
0
        {
4926
0
    _bfd_error_handler
4927
      /* xgettext:c-format */
4928
0
      (_("%pB: GNU_MBIND section `%pA' has invalid "
4929
0
         "sh_info field: %d"),
4930
0
       abfd, s, elf_section_data (s)->this_hdr.sh_info);
4931
0
    continue;
4932
0
        }
4933
      /* Align mbind section to page size.  */
4934
0
      if (s->alignment_power < page_align_power)
4935
0
        s->alignment_power = page_align_power;
4936
0
      segs ++;
4937
0
    }
4938
0
    }
4939
4940
  /* Let the backend count up any program headers it might need.  */
4941
8
  if (bed->elf_backend_additional_program_headers)
4942
8
    {
4943
8
      int a;
4944
4945
8
      a = (*bed->elf_backend_additional_program_headers) (abfd, info);
4946
8
      if (a == -1)
4947
0
  abort ();
4948
8
      segs += a;
4949
8
    }
4950
4951
8
  return segs * bed->s->sizeof_phdr;
4952
8
}
4953
4954
/* Find the segment that contains the output_section of section.  */
4955
4956
Elf_Internal_Phdr *
4957
_bfd_elf_find_segment_containing_section (bfd * abfd, asection * section)
4958
0
{
4959
0
  struct elf_segment_map *m;
4960
0
  Elf_Internal_Phdr *p;
4961
4962
0
  for (m = elf_seg_map (abfd), p = elf_tdata (abfd)->phdr;
4963
0
       m != NULL;
4964
0
       m = m->next, p++)
4965
0
    {
4966
0
      int i;
4967
4968
0
      for (i = m->count - 1; i >= 0; i--)
4969
0
  if (m->sections[i] == section)
4970
0
    return p;
4971
0
    }
4972
4973
0
  return NULL;
4974
0
}
4975
4976
/* Create a mapping from a set of sections to a program segment.  */
4977
4978
static struct elf_segment_map *
4979
make_mapping (bfd *abfd,
4980
        asection **sections,
4981
        unsigned int from,
4982
        unsigned int to,
4983
        bool phdr)
4984
16
{
4985
16
  struct elf_segment_map *m;
4986
16
  unsigned int i;
4987
16
  asection **hdrpp;
4988
16
  size_t amt;
4989
4990
16
  amt = sizeof (struct elf_segment_map) - sizeof (asection *);
4991
16
  amt += (to - from) * sizeof (asection *);
4992
16
  m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
4993
16
  if (m == NULL)
4994
0
    return NULL;
4995
16
  m->next = NULL;
4996
16
  m->p_type = PT_LOAD;
4997
32
  for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++)
4998
16
    m->sections[i - from] = *hdrpp;
4999
16
  m->count = to - from;
5000
5001
16
  if (from == 0 && phdr)
5002
3
    {
5003
      /* Include the headers in the first PT_LOAD segment.  */
5004
3
      m->includes_filehdr = 1;
5005
3
      m->includes_phdrs = 1;
5006
3
    }
5007
5008
16
  return m;
5009
16
}
5010
5011
/* Create the PT_DYNAMIC segment, which includes DYNSEC.  Returns NULL
5012
   on failure.  */
5013
5014
struct elf_segment_map *
5015
_bfd_elf_make_dynamic_segment (bfd *abfd, asection *dynsec)
5016
0
{
5017
0
  struct elf_segment_map *m;
5018
5019
0
  m = (struct elf_segment_map *) bfd_zalloc (abfd,
5020
0
               sizeof (struct elf_segment_map));
5021
0
  if (m == NULL)
5022
0
    return NULL;
5023
0
  m->next = NULL;
5024
0
  m->p_type = PT_DYNAMIC;
5025
0
  m->count = 1;
5026
0
  m->sections[0] = dynsec;
5027
5028
0
  return m;
5029
0
}
5030
5031
/* Possibly add or remove segments from the segment map.  */
5032
5033
static bool
5034
elf_modify_segment_map (bfd *abfd,
5035
      struct bfd_link_info *info,
5036
      bool remove_empty_load)
5037
77
{
5038
77
  struct elf_segment_map **m;
5039
77
  elf_backend_data *bed;
5040
5041
  /* The placement algorithm assumes that non allocated sections are
5042
     not in PT_LOAD segments.  We ensure this here by removing such
5043
     sections from the segment map.  We also remove excluded
5044
     sections.  Finally, any PT_LOAD segment without sections is
5045
     removed.  */
5046
77
  m = &elf_seg_map (abfd);
5047
367
  while (*m)
5048
290
    {
5049
290
      unsigned int i, new_count;
5050
5051
1.05k
      for (new_count = 0, i = 0; i < (*m)->count; i++)
5052
764
  {
5053
764
    if (((*m)->sections[i]->flags & SEC_EXCLUDE) == 0
5054
759
        && (((*m)->sections[i]->flags & SEC_ALLOC) != 0
5055
4
      || (*m)->p_type != PT_LOAD))
5056
759
      {
5057
759
        (*m)->sections[new_count] = (*m)->sections[i];
5058
759
        new_count++;
5059
759
      }
5060
764
  }
5061
290
      (*m)->count = new_count;
5062
5063
290
      if (remove_empty_load
5064
290
    && (*m)->p_type == PT_LOAD
5065
83
    && (*m)->count == 0
5066
17
    && !(*m)->includes_phdrs)
5067
14
  *m = (*m)->next;
5068
276
      else
5069
276
  m = &(*m)->next;
5070
290
    }
5071
5072
77
  bed = get_elf_backend_data (abfd);
5073
77
  if (bed->elf_backend_modify_segment_map != NULL)
5074
3
    {
5075
3
      if (!(*bed->elf_backend_modify_segment_map) (abfd, info))
5076
0
  return false;
5077
3
    }
5078
5079
77
  return true;
5080
77
}
5081
5082
#define IS_TBSS(s) \
5083
48
  ((s->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) == SEC_THREAD_LOCAL)
5084
5085
/* Set up a mapping from BFD sections to program segments.  Update
5086
   NEED_LAYOUT if the section layout is changed.  */
5087
5088
bool
5089
bfd_elf_map_sections_to_segments (bfd *abfd,
5090
          struct bfd_link_info *info,
5091
          bool *need_layout)
5092
77
{
5093
77
  unsigned int count;
5094
77
  struct elf_segment_map *m;
5095
77
  asection **sections = NULL;
5096
77
  elf_backend_data *bed = get_elf_backend_data (abfd);
5097
77
  bool no_user_phdrs;
5098
5099
77
  no_user_phdrs = elf_seg_map (abfd) == NULL;
5100
5101
77
  if (info != NULL)
5102
0
    {
5103
0
      info->user_phdrs = !no_user_phdrs;
5104
5105
      /* Size the relative relocations if DT_RELR is enabled.  */
5106
0
      if (info->enable_dt_relr
5107
0
    && need_layout != NULL
5108
0
    && bed->size_relative_relocs
5109
0
    && !bed->size_relative_relocs (info, need_layout))
5110
0
  info->callbacks->fatal
5111
0
    (_("%P: failed to size relative relocations\n"));
5112
0
    }
5113
5114
77
  if (no_user_phdrs && bfd_count_sections (abfd) != 0)
5115
8
    {
5116
8
      asection *s;
5117
8
      unsigned int i;
5118
8
      struct elf_segment_map *mfirst;
5119
8
      struct elf_segment_map **pm;
5120
8
      asection *last_hdr;
5121
8
      bfd_vma last_size;
5122
8
      unsigned int hdr_index;
5123
8
      bfd_vma maxpagesize;
5124
8
      asection **hdrpp;
5125
8
      bool phdr_in_segment;
5126
8
      bool writable;
5127
8
      bool executable;
5128
8
      unsigned int tls_count = 0;
5129
8
      asection *first_tls = NULL;
5130
8
      asection *first_mbind = NULL;
5131
8
      asection *dynsec, *eh_frame_hdr;
5132
8
      asection *sframe;
5133
8
      size_t amt;
5134
8
      bfd_vma addr_mask, wrap_to = 0;  /* Bytes.  */
5135
8
      bfd_size_type phdr_size;  /* Octets/bytes.  */
5136
8
      unsigned int opb = bfd_octets_per_byte (abfd, NULL);
5137
5138
      /* Select the allocated sections, and sort them.  */
5139
5140
8
      amt = bfd_count_sections (abfd) * sizeof (asection *);
5141
8
      sections = (asection **) bfd_malloc (amt);
5142
8
      if (sections == NULL)
5143
0
  goto error_return;
5144
5145
      /* Calculate top address, avoiding undefined behaviour of shift
5146
   left operator when shift count is equal to size of type
5147
   being shifted.  */
5148
8
      addr_mask = ((bfd_vma) 1 << (bfd_arch_bits_per_address (abfd) - 1)) - 1;
5149
8
      addr_mask = (addr_mask << 1) + 1;
5150
5151
8
      i = 0;
5152
38
      for (s = abfd->sections; s != NULL; s = s->next)
5153
30
  {
5154
30
    if ((s->flags & SEC_ALLOC) != 0)
5155
19
      {
5156
        /* target_index is unused until bfd_elf_final_link
5157
     starts output of section symbols.  Use it to make
5158
     qsort stable.  */
5159
19
        s->target_index = i;
5160
19
        sections[i] = s;
5161
19
        ++i;
5162
        /* A wrapping section potentially clashes with header.  */
5163
19
        if (((s->lma + s->size / opb) & addr_mask) < (s->lma & addr_mask))
5164
0
    wrap_to = (s->lma + s->size / opb) & addr_mask;
5165
19
      }
5166
30
  }
5167
8
      BFD_ASSERT (i <= bfd_count_sections (abfd));
5168
8
      count = i;
5169
5170
8
      qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections);
5171
5172
8
      phdr_size = elf_program_header_size (abfd);
5173
8
      if (phdr_size == (bfd_size_type) -1)
5174
8
  phdr_size = get_program_header_size (abfd, info);
5175
8
      phdr_size += bed->s->sizeof_ehdr;
5176
      /* phdr_size is compared to LMA values which are in bytes.  */
5177
8
      phdr_size /= opb;
5178
8
      if (info != NULL)
5179
0
  maxpagesize = info->maxpagesize;
5180
8
      else
5181
8
  maxpagesize = bed->maxpagesize;
5182
8
      if (maxpagesize == 0)
5183
0
  maxpagesize = 1;
5184
8
      phdr_in_segment = info != NULL && info->load_phdrs;
5185
8
      if (count != 0
5186
8
    && (((sections[0]->lma & addr_mask) & (maxpagesize - 1))
5187
8
        >= (phdr_size & (maxpagesize - 1))))
5188
  /* For compatibility with old scripts that may not be using
5189
     SIZEOF_HEADERS, add headers when it looks like space has
5190
     been left for them.  */
5191
3
  phdr_in_segment = true;
5192
5193
      /* Build the mapping.  */
5194
8
      mfirst = NULL;
5195
8
      pm = &mfirst;
5196
5197
      /* If we have a .interp section, then create a PT_PHDR segment for
5198
   the program headers and a PT_INTERP segment for the .interp
5199
   section.  */
5200
8
      s = bfd_get_section_by_name (abfd, ".interp");
5201
8
      if (s != NULL && (s->flags & SEC_LOAD) != 0 && s->size != 0)
5202
0
  {
5203
0
    amt = sizeof (struct elf_segment_map);
5204
0
    m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5205
0
    if (m == NULL)
5206
0
      goto error_return;
5207
0
    m->next = NULL;
5208
0
    m->p_type = PT_PHDR;
5209
0
    m->p_flags = PF_R;
5210
0
    m->p_flags_valid = 1;
5211
0
    m->includes_phdrs = 1;
5212
0
    phdr_in_segment = true;
5213
0
    *pm = m;
5214
0
    pm = &m->next;
5215
5216
0
    amt = sizeof (struct elf_segment_map);
5217
0
    m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5218
0
    if (m == NULL)
5219
0
      goto error_return;
5220
0
    m->next = NULL;
5221
0
    m->p_type = PT_INTERP;
5222
0
    m->count = 1;
5223
0
    m->sections[0] = s;
5224
5225
0
    *pm = m;
5226
0
    pm = &m->next;
5227
0
  }
5228
5229
      /* Look through the sections.  We put sections in the same program
5230
   segment when the start of the second section can be placed within
5231
   a few bytes of the end of the first section.  */
5232
8
      last_hdr = NULL;
5233
8
      last_size = 0;
5234
8
      hdr_index = 0;
5235
8
      writable = false;
5236
8
      executable = false;
5237
8
      dynsec = bfd_get_section_by_name (abfd, ".dynamic");
5238
8
      if (dynsec != NULL
5239
0
    && (dynsec->flags & SEC_LOAD) == 0)
5240
0
  dynsec = NULL;
5241
5242
8
      if ((abfd->flags & D_PAGED) == 0)
5243
0
  phdr_in_segment = false;
5244
5245
      /* Deal with -Ttext or something similar such that the first section
5246
   is not adjacent to the program headers.  This is an
5247
   approximation, since at this point we don't know exactly how many
5248
   program headers we will need.  */
5249
8
      if (phdr_in_segment && count > 0)
5250
3
  {
5251
3
    bfd_vma phdr_lma;  /* Bytes.  */
5252
3
    bool separate_phdr = false;
5253
5254
3
    phdr_lma = (sections[0]->lma - phdr_size) & addr_mask & -maxpagesize;
5255
3
    if (info != NULL
5256
0
        && info->separate_code
5257
0
        && (sections[0]->flags & SEC_CODE) != 0)
5258
0
      {
5259
        /* If data sections should be separate from code and
5260
     thus not executable, and the first section is
5261
     executable then put the file and program headers in
5262
     their own PT_LOAD.  */
5263
0
        if (!info->one_rosegment)
5264
0
    separate_phdr = true;
5265
5266
0
        if ((((phdr_lma + phdr_size - 1) & addr_mask & -maxpagesize)
5267
0
       == (sections[0]->lma & addr_mask & -maxpagesize)))
5268
0
    {
5269
      /* The file and program headers are currently on the
5270
         same page as the first section.  Put them on the
5271
         previous page if we can.  */
5272
0
      if (phdr_lma >= maxpagesize)
5273
0
        phdr_lma -= maxpagesize;
5274
0
      else
5275
0
        separate_phdr = false;
5276
0
    }
5277
0
      }
5278
3
    if ((sections[0]->lma & addr_mask) < phdr_lma
5279
3
        || (sections[0]->lma & addr_mask) < phdr_size)
5280
      /* If file and program headers would be placed at the end
5281
         of memory then it's probably better to omit them.  */
5282
0
      phdr_in_segment = false;
5283
3
    else if (phdr_lma < wrap_to)
5284
      /* If a section wraps around to where we'll be placing
5285
         file and program headers, then the headers will be
5286
         overwritten.  */
5287
0
      phdr_in_segment = false;
5288
3
    else if (separate_phdr)
5289
0
      {
5290
0
        m = make_mapping (abfd, sections, 0, 0, phdr_in_segment);
5291
0
        if (m == NULL)
5292
0
    goto error_return;
5293
0
        m->p_paddr = phdr_lma * opb;
5294
0
        m->p_vaddr_offset
5295
0
    = (sections[0]->vma - phdr_size) & addr_mask & -maxpagesize;
5296
0
        m->p_paddr_valid = 1;
5297
0
        *pm = m;
5298
0
        pm = &m->next;
5299
0
        phdr_in_segment = false;
5300
0
      }
5301
3
  }
5302
5303
27
      for (i = 0, hdrpp = sections; i < count; i++, hdrpp++)
5304
19
  {
5305
19
    asection *hdr;
5306
19
    bool new_segment;
5307
5308
19
    hdr = *hdrpp;
5309
5310
    /* See if this section and the last one will fit in the same
5311
       segment.  */
5312
5313
19
    if (last_hdr == NULL)
5314
8
      {
5315
        /* If we don't have a segment yet, then we don't need a new
5316
     one (we build the last one after this loop).  */
5317
8
        new_segment = false;
5318
8
      }
5319
11
    else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma)
5320
1
      {
5321
        /* If this section has a different relation between the
5322
     virtual address and the load address, then we need a new
5323
     segment.  */
5324
1
        new_segment = true;
5325
1
      }
5326
10
    else if (hdr->lma < last_hdr->lma + last_size
5327
2
       || last_hdr->lma + last_size < last_hdr->lma)
5328
8
      {
5329
        /* If this section has a load address that makes it overlap
5330
     the previous section, then we need a new segment.  */
5331
8
        new_segment = true;
5332
8
      }
5333
2
    else if ((abfd->flags & D_PAGED) != 0
5334
2
       && (((last_hdr->lma + last_size - 1) & -maxpagesize)
5335
2
           == (hdr->lma & -maxpagesize)))
5336
0
      {
5337
        /* If we are demand paged then we can't map two disk
5338
     pages onto the same memory page.  */
5339
0
        new_segment = false;
5340
0
      }
5341
    /* In the next test we have to be careful when last_hdr->lma is close
5342
       to the end of the address space.  If the aligned address wraps
5343
       around to the start of the address space, then there are no more
5344
       pages left in memory and it is OK to assume that the current
5345
       section can be included in the current segment.  */
5346
2
    else if ((BFD_ALIGN (last_hdr->lma + last_size, maxpagesize)
5347
2
        + maxpagesize > last_hdr->lma)
5348
2
       && (BFD_ALIGN (last_hdr->lma + last_size, maxpagesize)
5349
2
           + maxpagesize <= hdr->lma))
5350
1
      {
5351
        /* If putting this section in this segment would force us to
5352
     skip a page in the segment, then we need a new segment.  */
5353
1
        new_segment = true;
5354
1
      }
5355
1
    else if ((last_hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0
5356
1
       && (hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) != 0)
5357
1
      {
5358
        /* We don't want to put a loaded section after a
5359
     nonloaded (ie. bss style) section in the same segment
5360
     as that will force the non-loaded section to be loaded.
5361
     Consider .tbss sections as loaded for this purpose.  */
5362
1
        new_segment = true;
5363
1
      }
5364
0
    else if ((abfd->flags & D_PAGED) == 0)
5365
0
      {
5366
        /* If the file is not demand paged, which means that we
5367
     don't require the sections to be correctly aligned in the
5368
     file, then there is no other reason for a new segment.  */
5369
0
        new_segment = false;
5370
0
      }
5371
0
    else if (info != NULL
5372
0
       && info->separate_code
5373
0
       && executable != ((hdr->flags & SEC_CODE) != 0))
5374
0
      {
5375
0
        new_segment = true;
5376
0
      }
5377
0
    else if (! writable
5378
0
       && (hdr->flags & SEC_READONLY) == 0)
5379
0
      {
5380
        /* We don't want to put a writable section in a read only
5381
     segment.  */
5382
0
        new_segment = true;
5383
0
      }
5384
0
    else
5385
0
      {
5386
        /* Otherwise, we can use the same segment.  */
5387
0
        new_segment = false;
5388
0
      }
5389
5390
    /* Allow interested parties a chance to override our decision.  */
5391
19
    if (last_hdr != NULL
5392
11
        && info != NULL
5393
0
        && info->callbacks->override_segment_assignment != NULL)
5394
0
      new_segment
5395
0
        = info->callbacks->override_segment_assignment (info, abfd, hdr,
5396
0
                    last_hdr,
5397
0
                    new_segment);
5398
5399
19
    if (! new_segment)
5400
8
      {
5401
8
        if ((hdr->flags & SEC_READONLY) == 0)
5402
4
    writable = true;
5403
8
        if ((hdr->flags & SEC_CODE) != 0)
5404
2
    executable = true;
5405
8
        last_hdr = hdr;
5406
        /* .tbss sections effectively have zero size.  */
5407
8
        last_size = (!IS_TBSS (hdr) ? hdr->size : 0) / opb;
5408
8
        continue;
5409
8
      }
5410
5411
    /* We need a new program segment.  We must create a new program
5412
       header holding all the sections from hdr_index until hdr.  */
5413
5414
11
    m = make_mapping (abfd, sections, hdr_index, i, phdr_in_segment);
5415
11
    if (m == NULL)
5416
0
      goto error_return;
5417
5418
11
    *pm = m;
5419
11
    pm = &m->next;
5420
5421
11
    if ((hdr->flags & SEC_READONLY) == 0)
5422
3
      writable = true;
5423
8
    else
5424
8
      writable = false;
5425
5426
11
    if ((hdr->flags & SEC_CODE) == 0)
5427
8
      executable = false;
5428
3
    else
5429
3
      executable = true;
5430
5431
11
    last_hdr = hdr;
5432
    /* .tbss sections effectively have zero size.  */
5433
11
    last_size = (!IS_TBSS (hdr) ? hdr->size : 0) / opb;
5434
11
    hdr_index = i;
5435
11
    phdr_in_segment = false;
5436
11
  }
5437
5438
      /* Create a final PT_LOAD program segment, but not if it's just
5439
   for .tbss.  */
5440
8
      if (last_hdr != NULL
5441
8
    && (i - hdr_index != 1
5442
8
        || !IS_TBSS (last_hdr)))
5443
5
  {
5444
5
    m = make_mapping (abfd, sections, hdr_index, i, phdr_in_segment);
5445
5
    if (m == NULL)
5446
0
      goto error_return;
5447
5448
5
    *pm = m;
5449
5
    pm = &m->next;
5450
5
  }
5451
5452
      /* If there is a .dynamic section, throw in a PT_DYNAMIC segment.  */
5453
8
      if (dynsec != NULL)
5454
0
  {
5455
0
    m = _bfd_elf_make_dynamic_segment (abfd, dynsec);
5456
0
    if (m == NULL)
5457
0
      goto error_return;
5458
0
    *pm = m;
5459
0
    pm = &m->next;
5460
0
  }
5461
5462
      /* For each batch of consecutive loadable SHT_NOTE  sections,
5463
   add a PT_NOTE segment.  We don't use bfd_get_section_by_name,
5464
   because if we link together nonloadable .note sections and
5465
   loadable .note sections, we will generate two .note sections
5466
   in the output file.  */
5467
38
      for (s = abfd->sections; s != NULL; s = s->next)
5468
30
  {
5469
30
    if ((s->flags & SEC_LOAD) != 0
5470
9
        && elf_section_type (s) == SHT_NOTE)
5471
0
      {
5472
0
        asection *s2;
5473
0
        unsigned int alignment_power = s->alignment_power;
5474
5475
0
        count = 1;
5476
0
        for (s2 = s; s2->next != NULL; s2 = s2->next)
5477
0
    {
5478
0
      if (s2->next->alignment_power == alignment_power
5479
0
          && (s2->next->flags & SEC_LOAD) != 0
5480
0
          && elf_section_type (s2->next) == SHT_NOTE
5481
0
          && align_power (s2->lma + s2->size / opb,
5482
0
              alignment_power)
5483
0
          == s2->next->lma)
5484
0
        count++;
5485
0
      else
5486
0
        break;
5487
0
    }
5488
0
        amt = sizeof (struct elf_segment_map) - sizeof (asection *);
5489
0
        amt += count * sizeof (asection *);
5490
0
        m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5491
0
        if (m == NULL)
5492
0
    goto error_return;
5493
0
        m->next = NULL;
5494
0
        m->p_type = PT_NOTE;
5495
0
        m->count = count;
5496
0
        while (count > 1)
5497
0
    {
5498
0
      m->sections[m->count - count--] = s;
5499
0
      BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
5500
0
      s = s->next;
5501
0
    }
5502
0
        m->sections[m->count - 1] = s;
5503
0
        BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
5504
0
        *pm = m;
5505
0
        pm = &m->next;
5506
0
      }
5507
30
    if (s->flags & SEC_THREAD_LOCAL)
5508
4
      {
5509
4
        if (! tls_count)
5510
4
    first_tls = s;
5511
4
        tls_count++;
5512
4
      }
5513
30
    if (first_mbind == NULL
5514
30
        && (elf_section_flags (s) & SHF_GNU_MBIND) != 0)
5515
2
      first_mbind = s;
5516
30
  }
5517
5518
      /* If there are any SHF_TLS output sections, add PT_TLS segment.  */
5519
8
      if (tls_count > 0)
5520
4
  {
5521
4
    amt = sizeof (struct elf_segment_map) - sizeof (asection *);
5522
4
    amt += tls_count * sizeof (asection *);
5523
4
    m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5524
4
    if (m == NULL)
5525
0
      goto error_return;
5526
4
    m->next = NULL;
5527
4
    m->p_type = PT_TLS;
5528
4
    m->count = tls_count;
5529
    /* Mandated PF_R.  */
5530
4
    m->p_flags = PF_R;
5531
4
    m->p_flags_valid = 1;
5532
4
    s = first_tls;
5533
8
    for (i = 0; i < tls_count; ++i)
5534
4
      {
5535
4
        if ((s->flags & SEC_THREAD_LOCAL) == 0)
5536
0
    {
5537
0
      _bfd_error_handler
5538
0
        (_("%pB: TLS sections are not adjacent:"), abfd);
5539
0
      s = first_tls;
5540
0
      i = 0;
5541
0
      while (i < tls_count)
5542
0
        {
5543
0
          if ((s->flags & SEC_THREAD_LOCAL) != 0)
5544
0
      {
5545
0
        _bfd_error_handler (_("      TLS: %pA"), s);
5546
0
        i++;
5547
0
      }
5548
0
          else
5549
0
      _bfd_error_handler (_("  non-TLS: %pA"), s);
5550
0
          s = s->next;
5551
0
        }
5552
0
      bfd_set_error (bfd_error_bad_value);
5553
0
      goto error_return;
5554
0
    }
5555
4
        m->sections[i] = s;
5556
4
        s = s->next;
5557
4
      }
5558
5559
4
    *pm = m;
5560
4
    pm = &m->next;
5561
4
  }
5562
5563
8
      if (first_mbind
5564
2
    && (abfd->flags & D_PAGED) != 0
5565
2
    && (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0)
5566
0
  for (s = first_mbind; s != NULL; s = s->next)
5567
0
    if ((elf_section_flags (s) & SHF_GNU_MBIND) != 0
5568
0
        && elf_section_data (s)->this_hdr.sh_info <= PT_GNU_MBIND_NUM)
5569
0
      {
5570
        /* Mandated PF_R.  */
5571
0
        unsigned long p_flags = PF_R;
5572
0
        if ((s->flags & SEC_READONLY) == 0)
5573
0
    p_flags |= PF_W;
5574
0
        if ((s->flags & SEC_CODE) != 0)
5575
0
    p_flags |= PF_X;
5576
5577
0
        amt = sizeof (struct elf_segment_map) + sizeof (asection *);
5578
0
        m = bfd_zalloc (abfd, amt);
5579
0
        if (m == NULL)
5580
0
    goto error_return;
5581
0
        m->next = NULL;
5582
0
        m->p_type = (PT_GNU_MBIND_LO
5583
0
         + elf_section_data (s)->this_hdr.sh_info);
5584
0
        m->count = 1;
5585
0
        m->p_flags_valid = 1;
5586
0
        m->sections[0] = s;
5587
0
        m->p_flags = p_flags;
5588
5589
0
        *pm = m;
5590
0
        pm = &m->next;
5591
0
      }
5592
5593
8
      s = bfd_get_section_by_name (abfd,
5594
8
           NOTE_GNU_PROPERTY_SECTION_NAME);
5595
8
      if (s != NULL && s->size != 0)
5596
0
  {
5597
0
    amt = sizeof (struct elf_segment_map) + sizeof (asection *);
5598
0
    m = bfd_zalloc (abfd, amt);
5599
0
    if (m == NULL)
5600
0
      goto error_return;
5601
0
    m->next = NULL;
5602
0
    m->p_type = PT_GNU_PROPERTY;
5603
0
    m->count = 1;
5604
0
    m->p_flags_valid = 1;
5605
0
    m->sections[0] = s;
5606
0
    m->p_flags = PF_R;
5607
0
    *pm = m;
5608
0
    pm = &m->next;
5609
0
  }
5610
5611
      /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME
5612
   segment.  */
5613
8
      eh_frame_hdr = elf_eh_frame_hdr (info);
5614
8
      if (eh_frame_hdr != NULL
5615
0
    && (eh_frame_hdr->output_section->flags & SEC_LOAD) != 0)
5616
0
  {
5617
0
    amt = sizeof (struct elf_segment_map);
5618
0
    m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5619
0
    if (m == NULL)
5620
0
      goto error_return;
5621
0
    m->next = NULL;
5622
0
    m->p_type = PT_GNU_EH_FRAME;
5623
0
    m->count = 1;
5624
0
    m->sections[0] = eh_frame_hdr->output_section;
5625
5626
0
    *pm = m;
5627
0
    pm = &m->next;
5628
0
  }
5629
5630
      /* If there is a .sframe section, throw in a PT_GNU_SFRAME
5631
   segment.  */
5632
8
      sframe = elf_sframe (abfd);
5633
8
      if (sframe != NULL
5634
0
    && (sframe->output_section->flags & SEC_LOAD) != 0
5635
0
    && sframe->size != 0)
5636
0
  {
5637
0
    amt = sizeof (struct elf_segment_map);
5638
0
    m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5639
0
    if (m == NULL)
5640
0
      goto error_return;
5641
0
    m->next = NULL;
5642
0
    m->p_type = PT_GNU_SFRAME;
5643
0
    m->count = 1;
5644
0
    m->sections[0] = sframe->output_section;
5645
5646
0
    *pm = m;
5647
0
    pm = &m->next;
5648
0
  }
5649
5650
8
      if (elf_stack_flags (abfd))
5651
0
  {
5652
0
    amt = sizeof (struct elf_segment_map);
5653
0
    m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5654
0
    if (m == NULL)
5655
0
      goto error_return;
5656
0
    m->next = NULL;
5657
0
    m->p_type = PT_GNU_STACK;
5658
0
    m->p_flags = elf_stack_flags (abfd);
5659
0
    m->p_align = bed->stack_align;
5660
0
    m->p_flags_valid = 1;
5661
0
    m->p_align_valid = m->p_align != 0;
5662
0
    if (info->stacksize > 0)
5663
0
      {
5664
0
        m->p_size = info->stacksize;
5665
0
        m->p_size_valid = 1;
5666
0
      }
5667
5668
0
    *pm = m;
5669
0
    pm = &m->next;
5670
0
  }
5671
5672
8
      if (info != NULL && info->relro)
5673
0
  {
5674
0
    for (m = mfirst; m != NULL; m = m->next)
5675
0
      {
5676
0
        if (m->p_type == PT_LOAD
5677
0
      && m->count != 0
5678
0
      && m->sections[0]->vma >= info->relro_start
5679
0
      && m->sections[0]->vma < info->relro_end)
5680
0
    {
5681
0
      i = m->count;
5682
0
      while (--i != (unsigned) -1)
5683
0
        {
5684
0
          if (m->sections[i]->size > 0
5685
0
        && (m->sections[i]->flags & SEC_LOAD) != 0
5686
0
        && (m->sections[i]->flags & SEC_HAS_CONTENTS) != 0)
5687
0
      break;
5688
0
        }
5689
5690
0
      if (i != (unsigned) -1)
5691
0
        break;
5692
0
    }
5693
0
      }
5694
5695
    /* Make a PT_GNU_RELRO segment only when it isn't empty.  */
5696
0
    if (m != NULL)
5697
0
      {
5698
0
        amt = sizeof (struct elf_segment_map);
5699
0
        m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
5700
0
        if (m == NULL)
5701
0
    goto error_return;
5702
0
        m->next = NULL;
5703
0
        m->p_type = PT_GNU_RELRO;
5704
0
        *pm = m;
5705
0
        pm = &m->next;
5706
0
      }
5707
0
  }
5708
5709
8
      free (sections);
5710
8
      elf_seg_map (abfd) = mfirst;
5711
8
    }
5712
5713
77
  if (!elf_modify_segment_map (abfd, info, no_user_phdrs || info == NULL))
5714
0
    return false;
5715
5716
353
  for (count = 0, m = elf_seg_map (abfd); m != NULL; m = m->next)
5717
276
    ++count;
5718
77
  elf_program_header_size (abfd) = count * bed->s->sizeof_phdr;
5719
5720
77
  return true;
5721
5722
0
 error_return:
5723
0
  free (sections);
5724
0
  return false;
5725
77
}
5726
5727
/* Sort sections by address.  */
5728
5729
static int
5730
elf_sort_sections (const void *arg1, const void *arg2)
5731
1.09k
{
5732
1.09k
  const asection *sec1 = *(const asection **) arg1;
5733
1.09k
  const asection *sec2 = *(const asection **) arg2;
5734
1.09k
  bfd_size_type size1, size2;
5735
5736
  /* Sort by LMA first, since this is the address used to
5737
     place the section into a segment.  */
5738
1.09k
  if (sec1->lma < sec2->lma)
5739
996
    return -1;
5740
97
  else if (sec1->lma > sec2->lma)
5741
70
    return 1;
5742
5743
  /* Then sort by VMA.  Normally the LMA and the VMA will be
5744
     the same, and this will do nothing.  */
5745
27
  if (sec1->vma < sec2->vma)
5746
0
    return -1;
5747
27
  else if (sec1->vma > sec2->vma)
5748
0
    return 1;
5749
5750
  /* Put !SEC_LOAD sections after SEC_LOAD ones.  */
5751
5752
54
#define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0 \
5753
54
      && (x)->size != 0)
5754
5755
27
  if (TOEND (sec1))
5756
1
    {
5757
1
      if (!TOEND (sec2))
5758
0
  return 1;
5759
1
    }
5760
26
  else if (TOEND (sec2))
5761
0
    return -1;
5762
5763
27
#undef TOEND
5764
5765
  /* Sort by size, to put zero sized sections
5766
     before others at the same address.  */
5767
5768
27
  size1 = (sec1->flags & SEC_LOAD) ? sec1->size : 0;
5769
27
  size2 = (sec2->flags & SEC_LOAD) ? sec2->size : 0;
5770
5771
27
  if (size1 < size2)
5772
9
    return -1;
5773
18
  if (size1 > size2)
5774
10
    return 1;
5775
5776
8
  return sec1->target_index - sec2->target_index;
5777
18
}
5778
5779
/* This qsort comparison functions sorts PT_LOAD segments first and
5780
   by p_paddr, for assign_file_positions_for_load_sections.  */
5781
5782
static int
5783
elf_sort_segments (const void *arg1, const void *arg2)
5784
460
{
5785
460
  const struct elf_segment_map *m1 = *(const struct elf_segment_map **) arg1;
5786
460
  const struct elf_segment_map *m2 = *(const struct elf_segment_map **) arg2;
5787
5788
460
  if (m1->p_type != m2->p_type)
5789
422
    {
5790
422
      if (m1->p_type == PT_NULL)
5791
0
  return 1;
5792
422
      if (m2->p_type == PT_NULL)
5793
0
  return -1;
5794
422
      return m1->p_type < m2->p_type ? -1 : 1;
5795
422
    }
5796
38
  if (m1->includes_filehdr != m2->includes_filehdr)
5797
23
    return m1->includes_filehdr ? -1 : 1;
5798
15
  if (m1->no_sort_lma != m2->no_sort_lma)
5799
0
    return m1->no_sort_lma ? -1 : 1;
5800
15
  if (m1->p_type == PT_LOAD && !m1->no_sort_lma)
5801
14
    {
5802
14
      bfd_vma lma1, lma2;  /* Octets.  */
5803
14
      lma1 = 0;
5804
14
      if (m1->p_paddr_valid)
5805
1
  lma1 = m1->p_paddr;
5806
13
      else if (m1->count != 0)
5807
13
  {
5808
13
    unsigned int opb = bfd_octets_per_byte (m1->sections[0]->owner,
5809
13
              m1->sections[0]);
5810
13
    lma1 = (m1->sections[0]->lma + m1->p_vaddr_offset) * opb;
5811
13
  }
5812
14
      lma2 = 0;
5813
14
      if (m2->p_paddr_valid)
5814
1
  lma2 = m2->p_paddr;
5815
13
      else if (m2->count != 0)
5816
13
  {
5817
13
    unsigned int opb = bfd_octets_per_byte (m2->sections[0]->owner,
5818
13
              m2->sections[0]);
5819
13
    lma2 = (m2->sections[0]->lma + m2->p_vaddr_offset) * opb;
5820
13
  }
5821
14
      if (lma1 != lma2)
5822
1
  return lma1 < lma2 ? -1 : 1;
5823
14
    }
5824
14
  if (m1->idx != m2->idx)
5825
14
    return m1->idx < m2->idx ? -1 : 1;
5826
0
  return 0;
5827
14
}
5828
5829
/* Ian Lance Taylor writes:
5830
5831
   We shouldn't be using % with a negative signed number.  That's just
5832
   not good.  We have to make sure either that the number is not
5833
   negative, or that the number has an unsigned type.  When the types
5834
   are all the same size they wind up as unsigned.  When file_ptr is a
5835
   larger signed type, the arithmetic winds up as signed long long,
5836
   which is wrong.
5837
5838
   What we're trying to say here is something like ``increase OFF by
5839
   the least amount that will cause it to be equal to the VMA modulo
5840
   the page size.''  */
5841
/* In other words, something like:
5842
5843
   vma_offset = m->sections[0]->vma % bed->maxpagesize;
5844
   off_offset = off % bed->maxpagesize;
5845
   if (vma_offset < off_offset)
5846
     adjustment = vma_offset + bed->maxpagesize - off_offset;
5847
   else
5848
     adjustment = vma_offset - off_offset;
5849
5850
   which can be collapsed into the expression below.  */
5851
5852
static file_ptr
5853
vma_page_aligned_bias (bfd_vma vma, ufile_ptr off, bfd_vma maxpagesize)
5854
165
{
5855
  /* PR binutils/16199: Handle an alignment of zero.  */
5856
165
  if (maxpagesize == 0)
5857
0
    maxpagesize = 1;
5858
165
  return ((vma - off) % maxpagesize);
5859
165
}
5860
5861
static void
5862
print_segment_map (const struct elf_segment_map *m)
5863
1
{
5864
1
  unsigned int j;
5865
1
  const char *pt = get_segment_type (m->p_type);
5866
1
  char buf[32];
5867
5868
1
  if (pt == NULL)
5869
0
    {
5870
0
      if (m->p_type >= PT_LOPROC && m->p_type <= PT_HIPROC)
5871
0
  sprintf (buf, "LOPROC+%7.7x",
5872
0
     (unsigned int) (m->p_type - PT_LOPROC));
5873
0
      else if (m->p_type >= PT_LOOS && m->p_type <= PT_HIOS)
5874
0
  sprintf (buf, "LOOS+%7.7x",
5875
0
     (unsigned int) (m->p_type - PT_LOOS));
5876
0
      else
5877
0
  snprintf (buf, sizeof (buf), "%8.8x",
5878
0
      (unsigned int) m->p_type);
5879
0
      pt = buf;
5880
0
    }
5881
1
  fflush (stdout);
5882
1
  fprintf (stderr, "%s:", pt);
5883
9
  for (j = 0; j < m->count; j++)
5884
8
    fprintf (stderr, " %s", m->sections [j]->name);
5885
1
  putc ('\n',stderr);
5886
1
  fflush (stderr);
5887
1
}
5888
5889
/* Assign file positions to the sections based on the mapping from
5890
   sections to segments.  This function also sets up some fields in
5891
   the file header.  */
5892
5893
static bool
5894
assign_file_positions_for_load_sections (bfd *abfd,
5895
           struct bfd_link_info *link_info)
5896
77
{
5897
77
  elf_backend_data *bed = get_elf_backend_data (abfd);
5898
77
  struct elf_segment_map *m;
5899
77
  struct elf_segment_map *phdr_load_seg;
5900
77
  Elf_Internal_Phdr *phdrs;
5901
77
  Elf_Internal_Phdr *p;
5902
77
  file_ptr off;  /* Octets.  */
5903
77
  bfd_size_type maxpagesize;
5904
77
  unsigned int alloc, actual;
5905
77
  unsigned int i, j;
5906
77
  struct elf_segment_map **sorted_seg_map;
5907
77
  unsigned int opb = bfd_octets_per_byte (abfd, NULL);
5908
5909
77
  if (link_info == NULL
5910
77
      && !bfd_elf_map_sections_to_segments (abfd, link_info, NULL))
5911
0
    return false;
5912
5913
77
  alloc = 0;
5914
353
  for (m = elf_seg_map (abfd); m != NULL; m = m->next)
5915
276
    m->idx = alloc++;
5916
5917
77
  if (alloc)
5918
42
    {
5919
42
      elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr;
5920
42
      elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr;
5921
42
    }
5922
35
  else
5923
35
    {
5924
      /* PR binutils/12467.  */
5925
35
      elf_elfheader (abfd)->e_phoff = 0;
5926
35
      elf_elfheader (abfd)->e_phentsize = 0;
5927
35
    }
5928
5929
77
  elf_elfheader (abfd)->e_phnum = alloc;
5930
5931
77
  if (elf_program_header_size (abfd) == (bfd_size_type) -1)
5932
0
    {
5933
0
      actual = alloc;
5934
0
      elf_program_header_size (abfd) = alloc * bed->s->sizeof_phdr;
5935
0
    }
5936
77
  else
5937
77
    {
5938
77
      actual = elf_program_header_size (abfd) / bed->s->sizeof_phdr;
5939
77
      BFD_ASSERT (elf_program_header_size (abfd)
5940
77
      == actual * bed->s->sizeof_phdr);
5941
77
      BFD_ASSERT (actual >= alloc);
5942
77
    }
5943
5944
77
  if (alloc == 0)
5945
35
    {
5946
35
      elf_next_file_pos (abfd) = bed->s->sizeof_ehdr;
5947
35
      return true;
5948
35
    }
5949
5950
  /* We're writing the size in elf_program_header_size (abfd),
5951
     see assign_file_positions_except_relocs, so make sure we have
5952
     that amount allocated, with trailing space cleared.
5953
     The variable alloc contains the computed need, while
5954
     elf_program_header_size (abfd) contains the size used for the
5955
     layout.
5956
     See ld/emultempl/elf-generic.em:gld${EMULATION_NAME}_map_segments
5957
     where the layout is forced to according to a larger size in the
5958
     last iterations for the testcase ld-elf/header.  */
5959
42
  phdrs = bfd_zalloc (abfd, (actual * sizeof (*phdrs)
5960
42
           + alloc * sizeof (*sorted_seg_map)));
5961
42
  sorted_seg_map = (struct elf_segment_map **) (phdrs + actual);
5962
42
  elf_tdata (abfd)->phdr = phdrs;
5963
42
  if (phdrs == NULL)
5964
0
    return false;
5965
5966
318
  for (m = elf_seg_map (abfd), j = 0; m != NULL; m = m->next, j++)
5967
276
    {
5968
276
      sorted_seg_map[j] = m;
5969
      /* If elf_segment_map is not from map_sections_to_segments, the
5970
   sections may not be correctly ordered.  NOTE: sorting should
5971
   not be done to the PT_NOTE section of a corefile, which may
5972
   contain several pseudo-sections artificially created by bfd.
5973
   Sorting these pseudo-sections breaks things badly.  */
5974
276
      if (m->count > 1
5975
89
    && !(elf_elfheader (abfd)->e_type == ET_CORE
5976
0
         && m->p_type == PT_NOTE))
5977
89
  {
5978
752
    for (i = 0; i < m->count; i++)
5979
663
      m->sections[i]->target_index = i;
5980
89
    qsort (m->sections, (size_t) m->count, sizeof (asection *),
5981
89
     elf_sort_sections);
5982
89
  }
5983
276
    }
5984
42
  if (alloc > 1)
5985
38
    qsort (sorted_seg_map, alloc, sizeof (*sorted_seg_map),
5986
38
     elf_sort_segments);
5987
5988
42
  maxpagesize = 1;
5989
42
  if ((abfd->flags & D_PAGED) != 0)
5990
42
    {
5991
42
      if (link_info != NULL)
5992
0
  maxpagesize = link_info->maxpagesize;
5993
42
      else
5994
42
  maxpagesize = bed->maxpagesize;
5995
42
    }
5996
5997
  /* Sections must map to file offsets past the ELF file header.  */
5998
42
  off = bed->s->sizeof_ehdr;
5999
  /* And if one of the PT_LOAD headers doesn't include the program
6000
     headers then we'll be mapping program headers in the usual
6001
     position after the ELF file header.  */
6002
42
  phdr_load_seg = NULL;
6003
55
  for (j = 0; j < alloc; j++)
6004
53
    {
6005
53
      m = sorted_seg_map[j];
6006
53
      if (m->p_type != PT_LOAD)
6007
7
  break;
6008
46
      if (m->includes_phdrs)
6009
33
  {
6010
33
    phdr_load_seg = m;
6011
33
    break;
6012
33
  }
6013
46
    }
6014
42
  if (phdr_load_seg == NULL)
6015
9
    off += actual * bed->s->sizeof_phdr;
6016
6017
308
  for (j = 0; j < alloc; j++)
6018
269
    {
6019
269
      asection **secpp;
6020
269
      bfd_vma off_adjust;  /* Octets.  */
6021
269
      bool no_contents;
6022
269
      bfd_size_type align_pagesize;
6023
6024
      /* An ELF segment (described by Elf_Internal_Phdr) may contain a
6025
   number of sections with contents contributing to both p_filesz
6026
   and p_memsz, followed by a number of sections with no contents
6027
   that just contribute to p_memsz.  In this loop, OFF tracks next
6028
   available file offset for PT_LOAD and PT_NOTE segments.  */
6029
269
      m = sorted_seg_map[j];
6030
269
      p = phdrs + m->idx;
6031
269
      p->p_type = m->p_type;
6032
269
      p->p_flags = m->p_flags;
6033
6034
269
      if (m->count == 0)
6035
86
  p->p_vaddr = m->p_vaddr_offset * opb;
6036
183
      else
6037
183
  p->p_vaddr = (m->sections[0]->vma + m->p_vaddr_offset) * opb;
6038
6039
269
      if (m->p_paddr_valid)
6040
245
  p->p_paddr = m->p_paddr;
6041
24
      else if (m->count == 0)
6042
2
  p->p_paddr = 0;
6043
22
      else
6044
22
  p->p_paddr = (m->sections[0]->lma + m->p_vaddr_offset) * opb;
6045
6046
269
      align_pagesize = 0;
6047
269
      if (p->p_type == PT_LOAD
6048
68
    && (abfd->flags & D_PAGED) != 0)
6049
68
  {
6050
    /* p_align in demand paged PT_LOAD segments effectively stores
6051
       the maximum page size.  When copying an executable with
6052
       objcopy, we set m->p_align from the input file.  Use this
6053
       value for maxpagesize rather than bed->maxpagesize, which
6054
       may be different.  Note that we use maxpagesize for PT_TLS
6055
       segment alignment later in this function, so we are relying
6056
       on at least one PT_LOAD segment appearing before a PT_TLS
6057
       segment.  */
6058
68
    if (m->p_align_valid)
6059
31
      maxpagesize = m->p_align;
6060
37
    else if (bed->p_align != 0
6061
37
       && (link_info == NULL
6062
0
           || !link_info->maxpagesize_is_set))
6063
      /* We will lay out this binary using maxpagesize but set
6064
         p->p_align later to the possibly smaller bed->p_align.
6065
         The run-time loader will then be able to load this
6066
         binary when the system page size is maxpagesize, but if
6067
         the system page size is smaller can use p->p_align.
6068
         In either case p->p_align will be increased if
6069
         necessary to match section alignment.  */
6070
37
      align_pagesize = bed->p_align;
6071
6072
68
    p->p_align = maxpagesize;
6073
68
  }
6074
201
      else if (m->p_align_valid)
6075
131
  p->p_align = m->p_align;
6076
70
      else if (m->count == 0)
6077
32
  p->p_align = 1 << bed->s->log_file_align;
6078
6079
269
      if (m == phdr_load_seg)
6080
33
  off += actual * bed->s->sizeof_phdr;
6081
6082
269
      no_contents = false;
6083
269
      off_adjust = 0;
6084
269
      if (p->p_type == PT_LOAD
6085
68
    && m->count > 0)
6086
65
  {
6087
65
    bfd_size_type align;  /* Bytes.  */
6088
65
    unsigned int align_power = 0;
6089
6090
65
    if (m->p_align_valid)
6091
28
      align = p->p_align;
6092
37
    else
6093
37
      {
6094
228
        for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
6095
191
    {
6096
191
      unsigned int secalign;
6097
6098
191
      secalign = bfd_section_alignment (*secpp);
6099
191
      if (secalign > align_power)
6100
45
        align_power = secalign;
6101
191
    }
6102
37
        align = (bfd_size_type) 1 << align_power;
6103
        /* If a section requires alignment higher than the
6104
     minimum p_align value, don't reduce a maxpagesize
6105
     p->p_align set earlier in this function.  */
6106
37
        if (align > bed->p_align)
6107
1
    align_pagesize = 0;
6108
37
        if (align < maxpagesize)
6109
28
    align = maxpagesize;
6110
9
        else
6111
9
    {
6112
      /* If a section requires alignment higher than the
6113
         maximum page size, set p_align to the section
6114
         alignment.  */
6115
9
      if ((abfd->flags & D_PAGED) != 0)
6116
9
        p->p_align = align;
6117
9
    }
6118
37
      }
6119
6120
546
    for (i = 0; i < m->count; i++)
6121
481
      if ((m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
6122
        /* If we aren't making room for this section, then
6123
     it must be SHT_NOBITS regardless of what we've
6124
     set via struct bfd_elf_special_section.  */
6125
19
        elf_section_type (m->sections[i]) = SHT_NOBITS;
6126
6127
    /* Find out whether this segment contains any loadable
6128
       sections.  */
6129
65
    no_contents = true;
6130
66
    for (i = 0; i < m->count; i++)
6131
66
      if (elf_section_type (m->sections[i]) != SHT_NOBITS)
6132
65
        {
6133
65
    no_contents = false;
6134
65
    break;
6135
65
        }
6136
6137
65
    off_adjust = vma_page_aligned_bias (p->p_vaddr, off, align * opb);
6138
6139
    /* Broken hardware and/or kernel require that files do not
6140
       map the same page with different permissions on some hppa
6141
       processors.  */
6142
65
    if (j != 0
6143
31
        && (abfd->flags & D_PAGED) != 0
6144
31
        && bed->no_page_alias
6145
0
        && (off & (maxpagesize - 1)) != 0
6146
0
        && ((off & -maxpagesize)
6147
0
      == ((off + off_adjust) & -maxpagesize)))
6148
0
      off_adjust += maxpagesize;
6149
65
    off += off_adjust;
6150
65
    if (no_contents)
6151
0
      {
6152
        /* We shouldn't need to align the segment on disk since
6153
     the segment doesn't need file space, but the gABI
6154
     arguably requires the alignment and glibc ld.so
6155
     checks it.  So to comply with the alignment
6156
     requirement but not waste file space, we adjust
6157
     p_offset for just this segment.  (OFF_ADJUST is
6158
     subtracted from OFF later.)  This may put p_offset
6159
     past the end of file, but that shouldn't matter.  */
6160
0
      }
6161
65
    else
6162
65
      off_adjust = 0;
6163
65
  }
6164
      /* Make sure the .dynamic section is the first section in the
6165
   PT_DYNAMIC segment.  */
6166
204
      else if (p->p_type == PT_DYNAMIC
6167
33
         && m->count > 1
6168
1
         && ! streq (m->sections[0]->name, ".dynamic"))
6169
0
  {
6170
0
    _bfd_error_handler
6171
0
      (_("%pB: The first section in the PT_DYNAMIC segment"
6172
0
         " is not the .dynamic section"),
6173
0
       abfd);
6174
0
    bfd_set_error (bfd_error_bad_value);
6175
0
    return false;
6176
0
  }
6177
      /* Set the note section type to SHT_NOTE.  */
6178
204
      else if (p->p_type == PT_NOTE)
6179
84
  for (i = 0; i < m->count; i++)
6180
51
    elf_section_type (m->sections[i]) = SHT_NOTE;
6181
6182
269
      if (m->includes_filehdr)
6183
40
  {
6184
40
    if (!m->p_flags_valid)
6185
3
      p->p_flags |= PF_R;
6186
40
    p->p_filesz = bed->s->sizeof_ehdr;
6187
40
    p->p_memsz = bed->s->sizeof_ehdr;
6188
40
    if (p->p_type == PT_LOAD)
6189
33
      {
6190
33
        if (m->count > 0)
6191
30
    {
6192
30
      if (p->p_vaddr < (bfd_vma) off
6193
29
          || (!m->p_paddr_valid
6194
3
        && p->p_paddr < (bfd_vma) off))
6195
2
        {
6196
2
          _bfd_error_handler
6197
2
      (_("%pB: not enough room for program headers,"
6198
2
         " try linking with -N"),
6199
2
       abfd);
6200
2
          bfd_set_error (bfd_error_bad_value);
6201
2
          return false;
6202
2
        }
6203
28
      p->p_vaddr -= off;
6204
28
      if (!m->p_paddr_valid)
6205
2
        p->p_paddr -= off;
6206
28
    }
6207
33
      }
6208
7
    else if (sorted_seg_map[0]->includes_filehdr)
6209
5
      {
6210
5
        Elf_Internal_Phdr *filehdr = phdrs + sorted_seg_map[0]->idx;
6211
5
        p->p_vaddr = filehdr->p_vaddr;
6212
5
        if (!m->p_paddr_valid)
6213
0
    p->p_paddr = filehdr->p_paddr;
6214
5
      }
6215
40
  }
6216
6217
267
      if (m->includes_phdrs)
6218
57
  {
6219
57
    if (!m->p_flags_valid)
6220
2
      p->p_flags |= PF_R;
6221
57
    p->p_filesz += actual * bed->s->sizeof_phdr;
6222
57
    p->p_memsz += actual * bed->s->sizeof_phdr;
6223
57
    if (!m->includes_filehdr)
6224
20
      {
6225
20
        if (p->p_type == PT_LOAD)
6226
0
    {
6227
0
      p->p_offset = off - actual * bed->s->sizeof_phdr;
6228
0
      elf_elfheader (abfd)->e_phoff = p->p_offset;
6229
0
      if (m->count > 0)
6230
0
        {
6231
0
          p->p_vaddr -= off - p->p_offset;
6232
0
          if (!m->p_paddr_valid)
6233
0
      p->p_paddr -= off - p->p_offset;
6234
0
        }
6235
0
    }
6236
20
        else if (phdr_load_seg != NULL)
6237
17
    {
6238
      /* Also set PT_PHDR to match phdr_load_seg.  We've
6239
         sorted segments so that phdr_load_seg will
6240
         already be set by the code immediately above.  */
6241
17
      Elf_Internal_Phdr *phdr = phdrs + phdr_load_seg->idx;
6242
17
      bfd_vma phdr_off = 0;  /* Octets.  */
6243
17
      if (phdr_load_seg->includes_filehdr)
6244
17
        phdr_off = bed->s->sizeof_ehdr;
6245
17
      p->p_vaddr = phdr->p_vaddr + phdr_off;
6246
17
      if (!m->p_paddr_valid)
6247
0
        p->p_paddr = phdr->p_paddr + phdr_off;
6248
17
      p->p_offset = phdr->p_offset + phdr_off;
6249
17
    }
6250
3
        else
6251
3
    p->p_offset = bed->s->sizeof_ehdr;
6252
20
      }
6253
57
  }
6254
6255
267
      if (p->p_type == PT_LOAD
6256
201
    || (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core))
6257
66
  {
6258
66
    if (!m->includes_filehdr && !m->includes_phdrs)
6259
35
      {
6260
35
        p->p_offset = off;
6261
35
        if (no_contents)
6262
0
    {
6263
      /* Put meaningless p_offset for PT_LOAD segments
6264
         without file contents somewhere within the first
6265
         page, in an attempt to not point past EOF.  */
6266
0
      bfd_size_type align = maxpagesize;
6267
0
      if (align < p->p_align)
6268
0
        align = p->p_align;
6269
0
      if (align < 1)
6270
0
        align = 1;
6271
      /* Avoid p_offset of zero, which might be wrongly
6272
         interpreted as the segment being the first one,
6273
         containing the file header.  PR32763.  */
6274
0
      p->p_offset = (off + align - 1) % align + 1;
6275
0
    }
6276
35
      }
6277
31
    else
6278
31
      {
6279
31
        file_ptr adjust;  /* Octets.  */
6280
6281
31
        adjust = off - (p->p_offset + p->p_filesz);
6282
31
        if (!no_contents)
6283
31
    p->p_filesz += adjust;
6284
31
        p->p_memsz += adjust;
6285
31
      }
6286
66
  }
6287
6288
267
      if (align_pagesize)
6289
34
  p->p_align = align_pagesize;
6290
6291
      /* Set up p_filesz, p_memsz, p_align and p_flags from the section
6292
   maps.  Set filepos for sections in PT_LOAD segments, and in
6293
   core files, for sections in PT_NOTE segments.
6294
   assign_file_positions_for_non_load_sections will set filepos
6295
   for other sections and update p_filesz for other segments.  */
6296
1.00k
      for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
6297
739
  {
6298
739
    asection *sec;
6299
739
    bfd_size_type align;
6300
739
    Elf_Internal_Shdr *this_hdr;
6301
6302
739
    sec = *secpp;
6303
739
    this_hdr = &elf_section_data (sec)->this_hdr;
6304
739
    align = (bfd_size_type) 1 << bfd_section_alignment (sec);
6305
6306
739
    if ((p->p_type == PT_LOAD
6307
266
         || p->p_type == PT_DYNAMIC
6308
244
         || p->p_type == PT_TLS)
6309
497
        && (this_hdr->sh_type != SHT_NOBITS
6310
20
      || ((this_hdr->sh_flags & SHF_ALLOC) != 0
6311
20
          && ((this_hdr->sh_flags & SHF_TLS) == 0
6312
0
        || p->p_type == PT_TLS))))
6313
497
      {
6314
497
        bfd_vma p_start = p->p_paddr;   /* Octets.  */
6315
497
        bfd_vma p_end = p_start + p->p_memsz; /* Octets.  */
6316
497
        bfd_vma s_start = sec->lma * opb;   /* Octets.  */
6317
497
        bfd_vma adjust = s_start - p_end;   /* Octets.  */
6318
6319
497
        if (adjust != 0
6320
244
      && (s_start < p_end
6321
192
          || p_end < p_start))
6322
52
    {
6323
52
      _bfd_error_handler
6324
        /* xgettext:c-format */
6325
52
        (_("%pB: section %pA lma %#" PRIx64
6326
52
           " adjusted to %#" PRIx64),
6327
52
         abfd, sec, (uint64_t) s_start / opb,
6328
52
         (uint64_t) p_end / opb);
6329
52
      adjust = 0;
6330
52
      sec->lma = p_end / opb;
6331
52
    }
6332
497
        p->p_memsz += adjust;
6333
6334
497
        if (p->p_type == PT_LOAD)
6335
473
    {
6336
473
      if (this_hdr->sh_type != SHT_NOBITS)
6337
454
        {
6338
454
          off_adjust = 0;
6339
454
          if (p->p_filesz + adjust < p->p_memsz)
6340
1
      {
6341
        /* We have a PROGBITS section following NOBITS ones.
6342
           Allocate file space for the NOBITS section(s).
6343
           We don't need to write out the zeros, posix
6344
           fseek past the end of data already written
6345
           followed by a write at that location is
6346
           guaranteed to result in zeros being read
6347
           from the gap.  */
6348
1
        adjust = p->p_memsz - p->p_filesz;
6349
1
      }
6350
454
        }
6351
      /* We only adjust sh_offset in SHT_NOBITS sections
6352
         as would seem proper for their address when the
6353
         section is first in the segment.  sh_offset
6354
         doesn't really have any significance for
6355
         SHT_NOBITS anyway, apart from a notional position
6356
         relative to other sections.  Historically we
6357
         didn't bother with adjusting sh_offset and some
6358
         programs depend on it not being adjusted.  See
6359
         pr12921 and pr25662.  */
6360
473
      if (this_hdr->sh_type != SHT_NOBITS || i == 0)
6361
455
        {
6362
455
          off += adjust;
6363
455
          if (this_hdr->sh_type == SHT_NOBITS)
6364
1
      off_adjust += adjust;
6365
455
        }
6366
473
    }
6367
497
        if (this_hdr->sh_type != SHT_NOBITS)
6368
477
    p->p_filesz += adjust;
6369
497
      }
6370
6371
739
    if (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core)
6372
0
      {
6373
        /* The section at i == 0 is the one that actually contains
6374
     everything.  */
6375
0
        if (i == 0)
6376
0
    {
6377
0
      this_hdr->sh_offset = sec->filepos = off;
6378
0
      off += this_hdr->sh_size;
6379
0
      p->p_filesz = this_hdr->sh_size;
6380
0
      p->p_memsz = 0;
6381
0
      p->p_align = 1;
6382
0
    }
6383
0
        else
6384
0
    {
6385
      /* The rest are fake sections that shouldn't be written.  */
6386
0
      sec->filepos = 0;
6387
0
      sec->size = 0;
6388
0
      sec->flags = 0;
6389
0
      continue;
6390
0
    }
6391
0
      }
6392
739
    else
6393
739
      {
6394
739
        if (this_hdr->sh_type == SHT_NOBITS
6395
27
      && (this_hdr->sh_flags & SHF_TLS) != 0
6396
0
      && this_hdr->sh_offset == 0)
6397
0
    {
6398
      /* Set sh_offset for .tbss sections to their nominal
6399
         offset after aligning.  They are not loaded from
6400
         disk so the value doesn't really matter, except
6401
         when the .tbss section is the first one in a
6402
         PT_TLS segment.  In that case it sets the
6403
         p_offset for the PT_TLS segment, which according
6404
         to the ELF gABI ought to satisfy
6405
         p_offset % p_align == p_vaddr % p_align.  */
6406
0
      bfd_vma adjust = vma_page_aligned_bias (this_hdr->sh_addr,
6407
0
                off, align);
6408
0
      this_hdr->sh_offset = sec->filepos = off + adjust;
6409
0
    }
6410
739
        else if (p->p_type == PT_LOAD)
6411
473
    {
6412
473
      this_hdr->sh_offset = sec->filepos = off;
6413
473
      if (this_hdr->sh_type != SHT_NOBITS)
6414
454
        off += this_hdr->sh_size;
6415
473
    }
6416
6417
739
        if (this_hdr->sh_type != SHT_NOBITS)
6418
712
    {
6419
712
      p->p_filesz += this_hdr->sh_size;
6420
      /* A load section without SHF_ALLOC is something like
6421
         a note section in a PT_NOTE segment.  These take
6422
         file space but are not loaded into memory.  */
6423
712
      if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
6424
708
        p->p_memsz += this_hdr->sh_size;
6425
712
    }
6426
27
        else if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
6427
27
    {
6428
27
      if (p->p_type == PT_TLS)
6429
0
        p->p_memsz += this_hdr->sh_size;
6430
6431
      /* .tbss is special.  It doesn't contribute to p_memsz of
6432
         normal segments.  */
6433
27
      else if ((this_hdr->sh_flags & SHF_TLS) == 0)
6434
27
        p->p_memsz += this_hdr->sh_size;
6435
27
    }
6436
6437
739
        if (align > p->p_align
6438
114
      && !m->p_align_valid
6439
46
      && (p->p_type != PT_LOAD
6440
0
          || (abfd->flags & D_PAGED) == 0))
6441
46
    p->p_align = align;
6442
739
      }
6443
6444
739
    if (!m->p_flags_valid)
6445
15
      {
6446
15
        p->p_flags |= PF_R;
6447
15
        if ((this_hdr->sh_flags & SHF_EXECINSTR) != 0)
6448
3
    p->p_flags |= PF_X;
6449
15
        if ((this_hdr->sh_flags & SHF_WRITE) != 0)
6450
4
    p->p_flags |= PF_W;
6451
15
      }
6452
739
  }
6453
6454
267
      off -= off_adjust;
6455
6456
      /* PR ld/20815 - Check that the program header segment, if
6457
   present, will be loaded into memory.  */
6458
267
      if (p->p_type == PT_PHDR
6459
30
    && phdr_load_seg == NULL
6460
1
    && !(bed->elf_backend_allow_non_load_phdr != NULL
6461
0
         && bed->elf_backend_allow_non_load_phdr (abfd, phdrs, alloc)))
6462
1
  {
6463
    /* The fix for this error is usually to edit the linker script being
6464
       used and set up the program headers manually.  Either that or
6465
       leave room for the headers at the start of the SECTIONS.  */
6466
1
    _bfd_error_handler (_("%pB: error: PHDR segment not covered"
6467
1
        " by LOAD segment"),
6468
1
            abfd);
6469
1
    if (link_info == NULL)
6470
1
      return false;
6471
    /* Arrange for the linker to exit with an error, deleting
6472
       the output file unless --noinhibit-exec is given.  */
6473
0
    link_info->callbacks->info ("%X");
6474
0
  }
6475
6476
      /* Check that all sections are in a PT_LOAD segment.
6477
   Don't check funky gdb generated core files.  */
6478
266
      if (p->p_type == PT_LOAD && bfd_get_format (abfd) != bfd_core)
6479
66
  {
6480
66
    bool check_vma = true;
6481
6482
476
    for (i = 1; i < m->count; i++)
6483
410
      if (m->sections[i]->vma == m->sections[i - 1]->vma
6484
3
    && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i])
6485
3
               ->this_hdr), p) != 0
6486
0
    && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i - 1])
6487
0
               ->this_hdr), p) != 0)
6488
0
        {
6489
    /* Looks like we have overlays packed into the segment.  */
6490
0
    check_vma = false;
6491
0
    break;
6492
0
        }
6493
6494
539
    for (i = 0; i < m->count; i++)
6495
473
      {
6496
473
        Elf_Internal_Shdr *this_hdr;
6497
473
        asection *sec;
6498
6499
473
        sec = m->sections[i];
6500
473
        this_hdr = &(elf_section_data(sec)->this_hdr);
6501
473
        if (!ELF_SECTION_IN_SEGMENT_1 (this_hdr, p, check_vma, 0)
6502
1
      && !ELF_TBSS_SPECIAL (this_hdr, p))
6503
1
    {
6504
1
      _bfd_error_handler
6505
        /* xgettext:c-format */
6506
1
        (_("%pB: section `%pA' can't be allocated in segment %u"),
6507
1
         abfd, sec, m->idx);
6508
1
      print_segment_map (m);
6509
1
    }
6510
473
      }
6511
66
  }
6512
266
    }
6513
6514
39
  elf_next_file_pos (abfd) = off;
6515
6516
39
  if (link_info != NULL
6517
0
      && phdr_load_seg != NULL
6518
0
      && phdr_load_seg->includes_filehdr)
6519
0
    {
6520
      /* There is a segment that contains both the file headers and the
6521
   program headers, so provide a symbol __ehdr_start pointing there.
6522
   A program can use this to examine itself robustly.  */
6523
6524
0
      struct elf_link_hash_table *htab = elf_hash_table (link_info);
6525
0
      struct elf_link_hash_entry *hash = htab->hehdr_start;
6526
6527
      /* If the symbol was referenced and not defined, define it.  */
6528
0
      if (hash != NULL
6529
0
    && (hash->root.type == bfd_link_hash_new
6530
0
        || hash->root.type == bfd_link_hash_undefined
6531
0
        || hash->root.type == bfd_link_hash_undefweak
6532
0
        || hash->root.type == bfd_link_hash_common))
6533
0
  {
6534
0
    asection *s = NULL;
6535
0
    bfd_vma filehdr_vaddr = phdrs[phdr_load_seg->idx].p_vaddr / opb;
6536
6537
0
    if (phdr_load_seg->count != 0)
6538
      /* The segment contains sections, so use the first one.  */
6539
0
      s = phdr_load_seg->sections[0];
6540
0
    else
6541
      /* Use the first (i.e. lowest-addressed) section in any segment.  */
6542
0
      for (m = elf_seg_map (abfd); m != NULL; m = m->next)
6543
0
        if (m->p_type == PT_LOAD && m->count != 0)
6544
0
    {
6545
0
      s = m->sections[0];
6546
0
      break;
6547
0
    }
6548
6549
0
    if (s != NULL)
6550
0
      {
6551
0
        hash->root.u.def.value = filehdr_vaddr - s->vma;
6552
0
        hash->root.u.def.section = s;
6553
0
      }
6554
0
    else
6555
0
      {
6556
0
        hash->root.u.def.value = filehdr_vaddr;
6557
0
        hash->root.u.def.section = bfd_abs_section_ptr;
6558
0
      }
6559
6560
0
    hash->root.type = bfd_link_hash_defined;
6561
0
    hash->def_regular = 1;
6562
0
    hash->non_elf = 0;
6563
0
  }
6564
0
    }
6565
6566
39
  return true;
6567
42
}
6568
6569
/* Determine if a bfd is a debuginfo file.  Unfortunately there
6570
   is no defined method for detecting such files, so we have to
6571
   use heuristics instead.  */
6572
6573
bool
6574
is_debuginfo_file (bfd *abfd)
6575
96
{
6576
96
  if (abfd == NULL || bfd_get_flavour (abfd) != bfd_target_elf_flavour)
6577
0
    return false;
6578
6579
96
  Elf_Internal_Shdr **start_headers = elf_elfsections (abfd);
6580
96
  Elf_Internal_Shdr **end_headers = start_headers + elf_numsections (abfd);
6581
96
  Elf_Internal_Shdr **headerp;
6582
6583
213
  for (headerp = start_headers; headerp < end_headers; headerp ++)
6584
210
    {
6585
210
      Elf_Internal_Shdr *header = * headerp;
6586
6587
      /* Debuginfo files do not have any allocated SHT_PROGBITS sections.
6588
   The only allocated sections are SHT_NOBITS or SHT_NOTES.  */
6589
210
      if ((header->sh_flags & SHF_ALLOC) == SHF_ALLOC
6590
107
    && header->sh_type != SHT_NOBITS
6591
107
    && header->sh_type != SHT_NOTE)
6592
93
  return false;
6593
210
    }
6594
6595
3
  return true;
6596
96
}
6597
6598
/* Assign file positions for other sections, except for compressed debug
6599
   and sections assigned in _bfd_elf_assign_file_positions_for_non_load.  */
6600
6601
static bool
6602
assign_file_positions_for_non_load_sections (bfd *abfd,
6603
               struct bfd_link_info *link_info)
6604
74
{
6605
74
  elf_backend_data *bed = get_elf_backend_data (abfd);
6606
74
  Elf_Internal_Shdr **i_shdrpp;
6607
74
  Elf_Internal_Shdr **hdrpp, **end_hdrpp;
6608
74
  Elf_Internal_Phdr *phdrs;
6609
74
  Elf_Internal_Phdr *p;
6610
74
  struct elf_segment_map *m;
6611
74
  file_ptr off;
6612
74
  unsigned int opb = bfd_octets_per_byte (abfd, NULL);
6613
74
  bfd_vma maxpagesize;
6614
6615
74
  if (link_info != NULL)
6616
0
    maxpagesize = link_info->maxpagesize;
6617
74
  else
6618
74
    maxpagesize = bed->maxpagesize;
6619
74
  i_shdrpp = elf_elfsections (abfd);
6620
74
  end_hdrpp = i_shdrpp + elf_numsections (abfd);
6621
74
  off = elf_next_file_pos (abfd);
6622
864
  for (hdrpp = i_shdrpp + 1; hdrpp < end_hdrpp; hdrpp++)
6623
790
    {
6624
790
      Elf_Internal_Shdr *hdr;
6625
790
      bfd_vma align;
6626
6627
790
      hdr = *hdrpp;
6628
790
      if (hdr->bfd_section != NULL
6629
670
    && (hdr->bfd_section->filepos != 0
6630
221
        || (hdr->sh_type == SHT_NOBITS
6631
8
      && hdr->contents == NULL)))
6632
457
  BFD_ASSERT (hdr->sh_offset == hdr->bfd_section->filepos);
6633
333
      else if ((hdr->sh_flags & SHF_ALLOC) != 0)
6634
100
  {
6635
100
    if (hdr->sh_size != 0
6636
        /* PR 24717 - debuginfo files are known to be not strictly
6637
     compliant with the ELF standard.  In particular they often
6638
     have .note.gnu.property sections that are outside of any
6639
     loadable segment.  This is not a problem for such files,
6640
     so do not warn about them.  */
6641
96
        && ! is_debuginfo_file (abfd))
6642
93
      _bfd_error_handler
6643
        /* xgettext:c-format */
6644
93
        (_("%pB: warning: allocated section `%s' not in segment"),
6645
93
         abfd,
6646
93
         (hdr->bfd_section == NULL
6647
93
    ? "*unknown*"
6648
93
    : hdr->bfd_section->name));
6649
    /* We don't need to page align empty sections.  */
6650
100
    if ((abfd->flags & D_PAGED) != 0 && hdr->sh_size != 0)
6651
96
      align = maxpagesize;
6652
4
    else
6653
4
      align = hdr->sh_addralign & -hdr->sh_addralign;
6654
100
    off += vma_page_aligned_bias (hdr->sh_addr, off, align);
6655
100
    off = _bfd_elf_assign_file_position_for_section (hdr, off, false,
6656
100
                 bed->s->log_file_align);
6657
100
  }
6658
233
      else if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
6659
11
    && hdr->bfd_section == NULL)
6660
         /* We don't know the offset of these sections yet:
6661
      their size has not been decided.  */
6662
223
         || (abfd->is_linker_output
6663
0
       && hdr->bfd_section != NULL
6664
0
       && (hdr->sh_name == -1u
6665
0
           || bfd_section_is_ctf (hdr->bfd_section)))
6666
223
         || hdr == i_shdrpp[elf_onesymtab (abfd)]
6667
205
         || (elf_symtab_shndx_list (abfd) != NULL
6668
0
       && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx])
6669
205
         || hdr == i_shdrpp[elf_strtab_sec (abfd)]
6670
187
         || hdr == i_shdrpp[elf_shstrtab_sec (abfd)])
6671
120
  hdr->sh_offset = -1;
6672
113
      else
6673
113
  off = _bfd_elf_assign_file_position_for_section (hdr, off, true, 0);
6674
790
    }
6675
74
  elf_next_file_pos (abfd) = off;
6676
6677
  /* Now that we have set the section file positions, we can set up
6678
     the file positions for the non PT_LOAD segments.  */
6679
74
  phdrs = elf_tdata (abfd)->phdr;
6680
329
  for (m = elf_seg_map (abfd), p = phdrs; m != NULL; m = m->next, p++)
6681
256
    {
6682
256
      if (p->p_type == PT_GNU_RELRO)
6683
16
  {
6684
16
    bfd_vma start, end;  /* Bytes.  */
6685
16
    bool ok;
6686
6687
16
    if (link_info != NULL)
6688
0
      {
6689
        /* During linking the range of the RELRO segment is passed
6690
     in link_info.  Note that there may be padding between
6691
     relro_start and the first RELRO section.  */
6692
0
        start = link_info->relro_start;
6693
0
        end = link_info->relro_end;
6694
0
      }
6695
16
    else if (m->count != 0)
6696
13
      {
6697
13
        if (!m->p_size_valid)
6698
0
    abort ();
6699
13
        start = m->sections[0]->vma;
6700
13
        end = start + m->p_size / opb;
6701
13
      }
6702
3
    else
6703
3
      {
6704
3
        start = 0;
6705
3
        end = 0;
6706
3
      }
6707
6708
16
    ok = false;
6709
16
    if (start < end)
6710
13
      {
6711
13
        struct elf_segment_map *lm;
6712
13
        const Elf_Internal_Phdr *lp;
6713
13
        unsigned int i;
6714
6715
        /* Find a LOAD segment containing a section in the RELRO
6716
     segment.  */
6717
13
        for (lm = elf_seg_map (abfd), lp = phdrs;
6718
64
       lm != NULL;
6719
51
       lm = lm->next, lp++)
6720
61
    {
6721
61
      if (lp->p_type == PT_LOAD
6722
23
          && lm->count != 0
6723
21
          && (lm->sections[lm->count - 1]->vma
6724
21
        + (!IS_TBSS (lm->sections[lm->count - 1])
6725
21
           ? lm->sections[lm->count - 1]->size / opb
6726
21
           : 0)) > start
6727
10
          && lm->sections[0]->vma < end)
6728
10
        break;
6729
61
    }
6730
6731
13
        if (lm != NULL)
6732
10
    {
6733
      /* Find the section starting the RELRO segment.  */
6734
10
      for (i = 0; i < lm->count; i++)
6735
10
        {
6736
10
          asection *s = lm->sections[i];
6737
10
          if (s->vma >= start
6738
10
        && s->vma < end
6739
10
        && s->size != 0)
6740
10
      break;
6741
10
        }
6742
6743
10
      if (i < lm->count)
6744
10
        {
6745
10
          p->p_vaddr = lm->sections[i]->vma * opb;
6746
10
          p->p_paddr = lm->sections[i]->lma * opb;
6747
10
          p->p_offset = lm->sections[i]->filepos;
6748
10
          p->p_memsz = end * opb - p->p_vaddr;
6749
10
          p->p_filesz = p->p_memsz;
6750
6751
          /* The RELRO segment typically ends a few bytes
6752
       into .got.plt but other layouts are possible.
6753
       In cases where the end does not match any
6754
       loaded section (for instance is in file
6755
       padding), trim p_filesz back to correspond to
6756
       the end of loaded section contents.  */
6757
10
          if (p->p_filesz > lp->p_vaddr + lp->p_filesz - p->p_vaddr)
6758
3
      p->p_filesz = lp->p_vaddr + lp->p_filesz - p->p_vaddr;
6759
6760
          /* Preserve the alignment and flags if they are
6761
       valid.  The gold linker generates RW/4 for
6762
       the PT_GNU_RELRO section.  It is better for
6763
       objcopy/strip to honor these attributes
6764
       otherwise gdb will choke when using separate
6765
       debug files.  */
6766
10
          if (!m->p_align_valid)
6767
0
      p->p_align = 1;
6768
10
          if (!m->p_flags_valid)
6769
0
      p->p_flags = PF_R;
6770
10
          ok = true;
6771
10
        }
6772
10
    }
6773
13
      }
6774
6775
16
    if (!ok)
6776
6
      {
6777
6
        if (link_info != NULL)
6778
0
    _bfd_error_handler
6779
0
      (_("%pB: warning: unable to allocate any sections"
6780
0
         " to PT_GNU_RELRO segment"),
6781
0
       abfd);
6782
6
        memset (p, 0, sizeof *p);
6783
6
      }
6784
16
  }
6785
240
      else if (p->p_type == PT_GNU_STACK)
6786
12
  {
6787
12
    if (m->p_size_valid)
6788
9
      p->p_memsz = m->p_size;
6789
12
  }
6790
228
      else if (m->count != 0)
6791
155
  {
6792
155
    unsigned int i;
6793
6794
155
    if (p->p_type != PT_LOAD
6795
96
        && (p->p_type != PT_NOTE
6796
27
      || bfd_get_format (abfd) != bfd_core))
6797
96
      {
6798
        /* A user specified segment layout may include a PHDR
6799
     segment that overlaps with a LOAD segment...  */
6800
96
        if (p->p_type == PT_PHDR)
6801
3
    {
6802
3
      m->count = 0;
6803
3
      continue;
6804
3
    }
6805
6806
93
        if (m->includes_filehdr || m->includes_phdrs)
6807
1
    {
6808
      /* PR 17512: file: 2195325e.  */
6809
1
      _bfd_error_handler
6810
1
        (_("%pB: error: non-load segment %d includes file header "
6811
1
           "and/or program header"),
6812
1
         abfd, (int) (p - phdrs));
6813
1
      return false;
6814
1
    }
6815
6816
92
        p->p_filesz = 0;
6817
92
        p->p_offset = m->sections[0]->filepos;
6818
94
        for (i = m->count; i-- != 0;)
6819
93
    {
6820
93
      asection *sect = m->sections[i];
6821
93
      Elf_Internal_Shdr *hdr = &elf_section_data (sect)->this_hdr;
6822
93
      if (hdr->sh_type != SHT_NOBITS)
6823
91
        {
6824
91
          p->p_filesz = sect->filepos - p->p_offset + hdr->sh_size;
6825
          /* NB: p_memsz of the loadable PT_NOTE segment
6826
       should be the same as p_filesz.  */
6827
91
          if (p->p_type == PT_NOTE
6828
27
        && (hdr->sh_flags & SHF_ALLOC) != 0)
6829
24
      p->p_memsz = p->p_filesz;
6830
91
          break;
6831
91
        }
6832
93
    }
6833
92
      }
6834
155
  }
6835
256
    }
6836
6837
73
  return true;
6838
74
}
6839
6840
static elf_section_list *
6841
find_section_in_list (unsigned int i, elf_section_list * list)
6842
577
{
6843
588
  for (;list != NULL; list = list->next)
6844
14
    if (list->ndx == i)
6845
3
      break;
6846
577
  return list;
6847
577
}
6848
6849
/* Work out the file positions of all the sections.  This is called by
6850
   _bfd_elf_compute_section_file_positions.  All the section sizes and
6851
   VMAs must be known before this is called.
6852
6853
   Reloc sections come in two flavours: Those processed specially as
6854
   "side-channel" data attached to a section to which they apply, and
6855
   those that bfd doesn't process as relocations.  The latter sort are
6856
   stored in a normal bfd section by bfd_section_from_shdr.  We don't
6857
   consider the former sort here, unless they form part of the loadable
6858
   image.  Reloc sections not assigned here (and compressed debugging
6859
   sections and CTF sections which nothing else in the file can rely
6860
   upon) will be handled later by assign_file_positions_for_relocs.
6861
6862
   We also don't set the positions of the .symtab and .strtab here.  */
6863
6864
static bool
6865
assign_file_positions_except_relocs (bfd *abfd,
6866
             struct bfd_link_info *link_info)
6867
157
{
6868
157
  struct elf_obj_tdata *tdata = elf_tdata (abfd);
6869
157
  Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd);
6870
157
  elf_backend_data *bed = get_elf_backend_data (abfd);
6871
157
  unsigned int alloc;
6872
6873
157
  if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0
6874
82
      && bfd_get_format (abfd) != bfd_core)
6875
80
    {
6876
80
      Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd);
6877
80
      unsigned int num_sec = elf_numsections (abfd);
6878
80
      Elf_Internal_Shdr **hdrpp;
6879
80
      unsigned int i;
6880
80
      file_ptr off;
6881
6882
      /* Start after the ELF header.  */
6883
80
      off = i_ehdrp->e_ehsize;
6884
6885
      /* We are not creating an executable, which means that we are
6886
   not creating a program header, and that the actual order of
6887
   the sections in the file is unimportant.  */
6888
1.04k
      for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++)
6889
969
  {
6890
969
    Elf_Internal_Shdr *hdr;
6891
6892
969
    hdr = *hdrpp;
6893
969
    if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
6894
264
         && hdr->bfd_section == NULL)
6895
        /* Do not assign offsets for these sections yet: we don't know
6896
     their sizes.  */
6897
740
        || (abfd->is_linker_output
6898
0
      && hdr->bfd_section != NULL
6899
0
      && (hdr->sh_name == -1u
6900
0
          || bfd_section_is_ctf (hdr->bfd_section)))
6901
740
        || i == elf_onesymtab (abfd)
6902
706
        || (elf_symtab_shndx_list (abfd) != NULL
6903
0
      && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx])
6904
706
        || i == elf_strtab_sec (abfd)
6905
672
        || i == elf_shstrtab_sec (abfd))
6906
377
      {
6907
377
        hdr->sh_offset = -1;
6908
377
      }
6909
592
    else
6910
      /* There shouldn't be a need to effect "capped" file alignment here,
6911
         yet at least the Linux kernel's modpost utility was found to be
6912
         unhappy without.  While the issue was addressed there, let's be
6913
         kind for at least the foreseeable future ...  */
6914
592
      off = _bfd_elf_assign_file_position_for_section (hdr, off, false,
6915
592
                   bed->s->log_file_align);
6916
969
  }
6917
6918
80
      elf_next_file_pos (abfd) = off;
6919
80
      elf_program_header_size (abfd) = 0;
6920
80
    }
6921
77
  else
6922
77
    {
6923
      /* Assign file positions for the loaded sections based on the
6924
   assignment of sections to segments.  */
6925
77
      if (!assign_file_positions_for_load_sections (abfd, link_info))
6926
3
  return false;
6927
6928
      /* And for non-load sections.  */
6929
74
      if (!assign_file_positions_for_non_load_sections (abfd, link_info))
6930
1
  return false;
6931
74
    }
6932
6933
153
  if (!(*bed->elf_backend_modify_headers) (abfd, link_info))
6934
0
    return false;
6935
6936
  /* Write out the program headers.  */
6937
153
  alloc = i_ehdrp->e_phnum;
6938
153
  if (alloc != 0)
6939
38
    {
6940
38
      if (link_info != NULL && ! link_info->no_warn_rwx_segments)
6941
0
  {
6942
0
    bool warned_tls = false;
6943
0
    bool warned_rwx = false;
6944
6945
    /* Memory resident segments with non-zero size and RWX
6946
       permissions are a security risk, so we generate a warning
6947
       here if we are creating any.  */
6948
0
    unsigned int i;
6949
6950
0
    for (i = 0; i < alloc; i++)
6951
0
      {
6952
0
        const Elf_Internal_Phdr * phdr = tdata->phdr + i;
6953
6954
0
        if (phdr->p_memsz == 0)
6955
0
    continue;
6956
6957
0
        if (! warned_tls
6958
0
      && phdr->p_type == PT_TLS
6959
0
      && (phdr->p_flags & PF_X))
6960
0
    {
6961
0
      if (link_info->warn_is_error_for_rwx_segments)
6962
0
        {
6963
0
          _bfd_error_handler (_("\
6964
0
error: %pB has a TLS segment with execute permission"),
6965
0
            abfd);
6966
0
          return false;
6967
0
        }
6968
6969
0
      _bfd_error_handler (_("\
6970
0
warning: %pB has a TLS segment with execute permission"),
6971
0
              abfd);
6972
0
      if (warned_rwx)
6973
0
        break;
6974
6975
0
      warned_tls = true;
6976
0
    }
6977
0
        else if (! warned_rwx
6978
0
           && phdr->p_type == PT_LOAD
6979
0
           && ((phdr->p_flags & (PF_R | PF_W | PF_X))
6980
0
         == (PF_R | PF_W | PF_X)))
6981
0
    {
6982
0
      if (link_info->warn_is_error_for_rwx_segments)
6983
0
        {
6984
0
          _bfd_error_handler (_("\
6985
0
error: %pB has a LOAD segment with RWX permissions"),
6986
0
            abfd);
6987
0
          return false;
6988
0
        }
6989
6990
0
      _bfd_error_handler (_("\
6991
0
warning: %pB has a LOAD segment with RWX permissions"),
6992
0
              abfd);
6993
0
      if (warned_tls)
6994
0
        break;
6995
6996
0
      warned_rwx = true;
6997
0
    }
6998
0
      }
6999
0
  }
7000
7001
38
      if (bfd_seek (abfd, i_ehdrp->e_phoff, SEEK_SET) != 0
7002
38
    || bed->s->write_out_phdrs (abfd, tdata->phdr, alloc) != 0)
7003
0
  return false;
7004
38
    }
7005
7006
153
  return true;
7007
153
}
7008
7009
bool
7010
_bfd_elf_init_file_header (bfd *abfd,
7011
         struct bfd_link_info *info ATTRIBUTE_UNUSED)
7012
159
{
7013
159
  Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form.  */
7014
159
  struct elf_strtab_hash *shstrtab;
7015
159
  elf_backend_data *bed = get_elf_backend_data (abfd);
7016
7017
159
  i_ehdrp = elf_elfheader (abfd);
7018
7019
159
  shstrtab = _bfd_elf_strtab_init ();
7020
159
  if (shstrtab == NULL)
7021
0
    return false;
7022
7023
159
  elf_shstrtab (abfd) = shstrtab;
7024
7025
159
  i_ehdrp->e_ident[EI_MAG0] = ELFMAG0;
7026
159
  i_ehdrp->e_ident[EI_MAG1] = ELFMAG1;
7027
159
  i_ehdrp->e_ident[EI_MAG2] = ELFMAG2;
7028
159
  i_ehdrp->e_ident[EI_MAG3] = ELFMAG3;
7029
7030
159
  i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass;
7031
159
  i_ehdrp->e_ident[EI_DATA] =
7032
159
    bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB;
7033
159
  i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current;
7034
7035
159
  if ((abfd->flags & DYNAMIC) != 0)
7036
68
    i_ehdrp->e_type = ET_DYN;
7037
91
  else if ((abfd->flags & EXEC_P) != 0)
7038
7
    i_ehdrp->e_type = ET_EXEC;
7039
84
  else if (bfd_get_format (abfd) == bfd_core)
7040
2
    i_ehdrp->e_type = ET_CORE;
7041
82
  else
7042
82
    i_ehdrp->e_type = ET_REL;
7043
7044
159
  switch (bfd_get_arch (abfd))
7045
159
    {
7046
0
    case bfd_arch_unknown:
7047
0
      i_ehdrp->e_machine = EM_NONE;
7048
0
      break;
7049
7050
      /* There used to be a long list of cases here, each one setting
7051
   e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
7052
   in the corresponding bfd definition.  To avoid duplication,
7053
   the switch was removed.  Machines that need special handling
7054
   can generally do it in elf_backend_final_write_processing(),
7055
   unless they need the information earlier than the final write.
7056
   Such need can generally be supplied by replacing the tests for
7057
   e_machine with the conditions used to determine it.  */
7058
159
    default:
7059
159
      i_ehdrp->e_machine = bed->elf_machine_code;
7060
159
    }
7061
7062
159
  i_ehdrp->e_version = bed->s->ev_current;
7063
159
  i_ehdrp->e_ehsize = bed->s->sizeof_ehdr;
7064
7065
  /* No program header, for now.  */
7066
159
  i_ehdrp->e_phoff = 0;
7067
159
  i_ehdrp->e_phentsize = 0;
7068
159
  i_ehdrp->e_phnum = 0;
7069
7070
  /* Each bfd section is section header entry.  */
7071
159
  i_ehdrp->e_entry = bfd_get_start_address (abfd);
7072
159
  i_ehdrp->e_shentsize = bed->s->sizeof_shdr;
7073
7074
159
  elf_tdata (abfd)->symtab_hdr.sh_name =
7075
159
    (unsigned int) _bfd_elf_strtab_add (shstrtab, ".symtab", false);
7076
159
  elf_tdata (abfd)->strtab_hdr.sh_name =
7077
159
    (unsigned int) _bfd_elf_strtab_add (shstrtab, ".strtab", false);
7078
159
  elf_tdata (abfd)->shstrtab_hdr.sh_name =
7079
159
    (unsigned int) _bfd_elf_strtab_add (shstrtab, ".shstrtab", false);
7080
159
  if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
7081
159
      || elf_tdata (abfd)->strtab_hdr.sh_name == (unsigned int) -1
7082
159
      || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1)
7083
0
    return false;
7084
7085
159
  return true;
7086
159
}
7087
7088
/* Set e_type in ELF header to ET_EXEC for -pie -Ttext-segment=.
7089
7090
   FIXME: We used to have code here to sort the PT_LOAD segments into
7091
   ascending order, as per the ELF spec.  But this breaks some programs,
7092
   including the Linux kernel.  But really either the spec should be
7093
   changed or the programs updated.  */
7094
7095
bool
7096
_bfd_elf_modify_headers (bfd *obfd, struct bfd_link_info *link_info)
7097
153
{
7098
153
  if (link_info != NULL && bfd_link_pie (link_info))
7099
0
    {
7100
0
      Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (obfd);
7101
0
      unsigned int num_segments = i_ehdrp->e_phnum;
7102
0
      struct elf_obj_tdata *tdata = elf_tdata (obfd);
7103
0
      Elf_Internal_Phdr *segment = tdata->phdr;
7104
0
      Elf_Internal_Phdr *end_segment = &segment[num_segments];
7105
7106
      /* Find the lowest p_vaddr in PT_LOAD segments.  */
7107
0
      bfd_vma p_vaddr = (bfd_vma) -1;
7108
0
      for (; segment < end_segment; segment++)
7109
0
  if (segment->p_type == PT_LOAD && p_vaddr > segment->p_vaddr)
7110
0
    p_vaddr = segment->p_vaddr;
7111
7112
      /* Set e_type to ET_EXEC if the lowest p_vaddr in PT_LOAD
7113
   segments is non-zero.  */
7114
0
      if (p_vaddr)
7115
0
  i_ehdrp->e_type = ET_EXEC;
7116
0
    }
7117
153
  return true;
7118
153
}
7119
7120
/* Assign file positions for all the reloc sections which are not part
7121
   of the loadable file image, and the file position of section headers.  */
7122
7123
static bool
7124
_bfd_elf_assign_file_positions_for_non_load (bfd *abfd)
7125
152
{
7126
152
  file_ptr off;
7127
152
  Elf_Internal_Shdr **shdrpp, **end_shdrpp;
7128
152
  Elf_Internal_Shdr *shdrp;
7129
152
  Elf_Internal_Ehdr *i_ehdrp;
7130
152
  elf_backend_data *bed = get_elf_backend_data (abfd);
7131
7132
  /* Skip non-load sections without section header.  */
7133
152
  if ((abfd->flags & BFD_NO_SECTION_HEADER) != 0)
7134
0
    return true;
7135
7136
152
  off = elf_next_file_pos (abfd);
7137
7138
152
  shdrpp = elf_elfsections (abfd);
7139
152
  end_shdrpp = shdrpp + elf_numsections (abfd);
7140
1.87k
  for (shdrpp++; shdrpp < end_shdrpp; shdrpp++)
7141
1.72k
    {
7142
1.72k
      shdrp = *shdrpp;
7143
1.72k
      if (shdrp->sh_offset == -1)
7144
391
  {
7145
391
    asection *sec = shdrp->bfd_section;
7146
391
    if (sec == NULL
7147
0
        || shdrp->sh_type == SHT_REL
7148
0
        || shdrp->sh_type == SHT_RELA)
7149
391
      ;
7150
0
    else if (bfd_section_is_ctf (sec))
7151
0
      {
7152
        /* Update section size and contents.  */
7153
0
        shdrp->sh_size = sec->size;
7154
0
        shdrp->contents = sec->contents;
7155
0
      }
7156
0
    else if (shdrp->sh_name == -1u)
7157
0
      {
7158
0
        const char *name = sec->name;
7159
0
        struct bfd_elf_section_data *d;
7160
7161
        /* Compress DWARF debug sections.  */
7162
0
        if (!bfd_compress_section (abfd, sec, shdrp->contents))
7163
0
    return false;
7164
7165
0
        if (sec->compress_status == COMPRESS_SECTION_DONE
7166
0
      && (abfd->flags & BFD_COMPRESS_GABI) == 0
7167
0
      && name[1] == 'd')
7168
0
    {
7169
      /* If section is compressed with zlib-gnu, convert
7170
         section name from .debug_* to .zdebug_*.  */
7171
0
      char *new_name = bfd_debug_name_to_zdebug (abfd, name);
7172
0
      if (new_name == NULL)
7173
0
        return false;
7174
0
      name = new_name;
7175
0
    }
7176
        /* Add section name to section name section.  */
7177
0
        shdrp->sh_name
7178
0
    = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
7179
0
                  name, false);
7180
0
        d = elf_section_data (sec);
7181
7182
        /* Add reloc section name to section name section.  */
7183
0
        if (d->rel.hdr
7184
0
      && !_bfd_elf_set_reloc_sh_name (abfd, d->rel.hdr,
7185
0
              name, false))
7186
0
    return false;
7187
0
        if (d->rela.hdr
7188
0
      && !_bfd_elf_set_reloc_sh_name (abfd, d->rela.hdr,
7189
0
              name, true))
7190
0
    return false;
7191
7192
        /* Update section size and contents.  */
7193
0
        shdrp->sh_size = sec->size;
7194
0
        shdrp->contents = sec->contents;
7195
0
        sec->contents = NULL;
7196
0
      }
7197
7198
391
    off = _bfd_elf_assign_file_position_for_section (shdrp, off,
7199
391
      (abfd->flags & (EXEC_P | DYNAMIC))
7200
311
      || bfd_get_format (abfd) == bfd_core,
7201
391
      bed->s->log_file_align);
7202
391
  }
7203
1.72k
    }
7204
7205
  /* Place section name section after DWARF debug sections have been
7206
     compressed.  */
7207
152
  _bfd_elf_strtab_finalize (elf_shstrtab (abfd));
7208
152
  shdrp = &elf_tdata (abfd)->shstrtab_hdr;
7209
152
  shdrp->sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd));
7210
152
  off = _bfd_elf_assign_file_position_for_section (shdrp, off, true, 0);
7211
7212
  /* Place the section headers.  */
7213
152
  i_ehdrp = elf_elfheader (abfd);
7214
152
  off = BFD_ALIGN (off, 1u << bed->s->log_file_align);
7215
152
  i_ehdrp->e_shoff = off;
7216
152
  off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize;
7217
152
  elf_next_file_pos (abfd) = off;
7218
7219
152
  return true;
7220
152
}
7221
7222
bool
7223
_bfd_elf_write_object_contents (bfd *abfd)
7224
153
{
7225
153
  elf_backend_data *bed = get_elf_backend_data (abfd);
7226
153
  Elf_Internal_Shdr **i_shdrp;
7227
153
  bool failed;
7228
153
  unsigned int count, num_sec;
7229
153
  struct elf_obj_tdata *t;
7230
7231
153
  if (! abfd->output_has_begun
7232
87
      && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
7233
1
    return false;
7234
  /* Do not rewrite ELF data when the BFD has been opened for update.
7235
     abfd->output_has_begun was set to TRUE on opening, so creation of
7236
     new sections, and modification of existing section sizes was
7237
     restricted.  This means the ELF header, program headers and
7238
     section headers can't have changed.  If the contents of any
7239
     sections has been modified, then those changes have already been
7240
     written to the BFD.  */
7241
152
  else if (abfd->direction == both_direction)
7242
0
    {
7243
0
      BFD_ASSERT (abfd->output_has_begun);
7244
0
      return true;
7245
0
    }
7246
7247
152
  i_shdrp = elf_elfsections (abfd);
7248
7249
152
  failed = false;
7250
152
  bfd_map_over_sections (abfd, bed->s->write_relocs, &failed);
7251
152
  if (failed)
7252
0
    return false;
7253
7254
152
  if (!_bfd_elf_assign_file_positions_for_non_load (abfd))
7255
0
    return false;
7256
7257
  /* After writing the headers, we need to write the sections too...  */
7258
152
  num_sec = elf_numsections (abfd);
7259
1.87k
  for (count = 1; count < num_sec; count++)
7260
1.72k
    {
7261
      /* Don't set the sh_name field without section header.  */
7262
1.72k
      if ((abfd->flags & BFD_NO_SECTION_HEADER) == 0)
7263
1.72k
  i_shdrp[count]->sh_name
7264
1.72k
    = _bfd_elf_strtab_offset (elf_shstrtab (abfd),
7265
1.72k
            i_shdrp[count]->sh_name);
7266
1.72k
      if (bed->elf_backend_section_processing)
7267
244
  if (!(*bed->elf_backend_section_processing) (abfd, i_shdrp[count]))
7268
0
    return false;
7269
1.72k
      if (i_shdrp[count]->contents)
7270
303
  {
7271
303
    bfd_size_type amt = i_shdrp[count]->sh_size;
7272
7273
303
    if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0
7274
303
        || bfd_write (i_shdrp[count]->contents, amt, abfd) != amt)
7275
0
      return false;
7276
303
  }
7277
1.72k
    }
7278
7279
  /* Write out the section header names.  */
7280
152
  t = elf_tdata (abfd);
7281
152
  if (elf_shstrtab (abfd) != NULL
7282
152
      && t->shstrtab_hdr.sh_offset != -1
7283
152
      && (bfd_seek (abfd, t->shstrtab_hdr.sh_offset, SEEK_SET) != 0
7284
152
    || !_bfd_elf_strtab_emit (abfd, elf_shstrtab (abfd))))
7285
0
    return false;
7286
7287
152
  if (!(*bed->elf_backend_final_write_processing) (abfd))
7288
0
    return false;
7289
7290
152
  if (!bed->s->write_shdrs_and_ehdr (abfd))
7291
0
    return false;
7292
7293
  /* This is last since write_shdrs_and_ehdr can touch i_shdrp[0].  */
7294
152
  if (t->o->build_id.after_write_object_contents != NULL
7295
0
      && !(*t->o->build_id.after_write_object_contents) (abfd))
7296
0
    return false;
7297
152
  if (t->o->package_metadata.after_write_object_contents != NULL
7298
0
      && !(*t->o->package_metadata.after_write_object_contents) (abfd))
7299
0
    return false;
7300
7301
152
  return true;
7302
152
}
7303
7304
bool
7305
_bfd_elf_write_corefile_contents (bfd *abfd)
7306
2
{
7307
  /* Hopefully this can be done just like an object file.  */
7308
2
  return _bfd_elf_write_object_contents (abfd);
7309
2
}
7310
7311
/* Given a section, search the header to find them.  */
7312
7313
unsigned int
7314
_bfd_elf_section_from_bfd_section (bfd *abfd, struct bfd_section *asect)
7315
1.45k
{
7316
1.45k
  elf_backend_data *bed;
7317
1.45k
  unsigned int sec_index;
7318
7319
1.45k
  if (elf_section_data (asect) != NULL
7320
661
      && elf_section_data (asect)->this_idx != 0)
7321
661
    return elf_section_data (asect)->this_idx;
7322
7323
798
  if (bfd_is_abs_section (asect))
7324
0
    sec_index = SHN_ABS;
7325
798
  else if (bfd_is_com_section (asect))
7326
6
    sec_index = SHN_COMMON;
7327
792
  else if (bfd_is_und_section (asect))
7328
791
    sec_index = SHN_UNDEF;
7329
1
  else
7330
1
    sec_index = SHN_BAD;
7331
7332
798
  bed = get_elf_backend_data (abfd);
7333
798
  if (bed->elf_backend_section_from_bfd_section)
7334
798
    {
7335
798
      int retval = sec_index;
7336
7337
798
      if ((*bed->elf_backend_section_from_bfd_section) (abfd, asect, &retval))
7338
6
  return retval;
7339
798
    }
7340
7341
792
  if (sec_index == SHN_BAD)
7342
0
    bfd_set_error (bfd_error_nonrepresentable_section);
7343
7344
792
  return sec_index;
7345
798
}
7346
7347
/* Given a BFD symbol, return the index in the ELF symbol table, or -1
7348
   on error.  */
7349
7350
int
7351
_bfd_elf_symbol_from_bfd_symbol (bfd *abfd, asymbol **asym_ptr_ptr)
7352
664
{
7353
664
  asymbol *asym_ptr = *asym_ptr_ptr;
7354
664
  int idx;
7355
664
  flagword flags = asym_ptr->flags;
7356
7357
  /* When gas creates relocations against local labels, it creates its
7358
     own symbol for the section, but does put the symbol into the
7359
     symbol chain, so udata is 0.  When the linker is generating
7360
     relocatable output, this section symbol may be for one of the
7361
     input sections rather than the output section.  */
7362
664
  if (asym_ptr->udata.i == 0
7363
0
      && (flags & BSF_SECTION_SYM)
7364
0
      && asym_ptr->section)
7365
0
    {
7366
0
      asection *sec;
7367
7368
0
      sec = asym_ptr->section;
7369
0
      if (sec->owner != abfd && sec->output_section != NULL)
7370
0
  sec = sec->output_section;
7371
0
      if (sec->owner == abfd
7372
0
    && sec->index < elf_num_section_syms (abfd)
7373
0
    && elf_section_syms (abfd)[sec->index] != NULL)
7374
0
  asym_ptr->udata.i = elf_section_syms (abfd)[sec->index]->udata.i;
7375
0
    }
7376
7377
664
  idx = asym_ptr->udata.i;
7378
7379
664
  if (idx == 0)
7380
0
    {
7381
      /* This case can occur when using --strip-symbol on a symbol
7382
   which is used in a relocation entry.  */
7383
0
      _bfd_error_handler
7384
  /* xgettext:c-format */
7385
0
  (_("%pB: symbol `%s' required but not present"),
7386
0
   abfd, bfd_asymbol_name (asym_ptr));
7387
0
      bfd_set_error (bfd_error_no_symbols);
7388
0
      return -1;
7389
0
    }
7390
7391
#if DEBUG & 4
7392
  {
7393
    fprintf (stderr,
7394
       "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d,"
7395
       " flags = 0x%.8x\n",
7396
       (long) asym_ptr, asym_ptr->name, idx, flags);
7397
    fflush (stderr);
7398
  }
7399
#endif
7400
7401
664
  return idx;
7402
664
}
7403
7404
static inline bfd_vma
7405
segment_size (Elf_Internal_Phdr *segment)
7406
3.54k
{
7407
3.54k
  return (segment->p_memsz > segment->p_filesz
7408
3.54k
    ? segment->p_memsz : segment->p_filesz);
7409
3.54k
}
7410
7411
7412
/* Returns the end address of the segment + 1.  */
7413
static inline bfd_vma
7414
segment_end (Elf_Internal_Phdr *segment, bfd_vma start)
7415
23
{
7416
23
  return start + segment_size (segment);
7417
23
}
7418
7419
static inline bfd_size_type
7420
section_size (asection *section, Elf_Internal_Phdr *segment)
7421
3.54k
{
7422
3.54k
  if ((section->flags & SEC_HAS_CONTENTS) != 0
7423
200
      || (section->flags & SEC_THREAD_LOCAL) == 0
7424
0
      || segment->p_type == PT_TLS)
7425
3.54k
    return section->size;
7426
0
  return 0;
7427
3.54k
}
7428
7429
/* Returns TRUE if the given section is contained within the given
7430
   segment.  LMA addresses are compared against PADDR when
7431
   USE_VADDR is false, VMA against VADDR when true.  */
7432
static bool
7433
is_contained_by (asection *section, Elf_Internal_Phdr *segment,
7434
     bfd_vma paddr, bfd_vma vaddr, unsigned int opb,
7435
     bool use_vaddr)
7436
2.75k
{
7437
2.75k
  bfd_vma seg_addr = !use_vaddr ? paddr : vaddr;
7438
2.75k
  bfd_vma addr = !use_vaddr ? section->lma : section->vma;
7439
2.75k
  bfd_vma octet;
7440
2.75k
  if (_bfd_mul_overflow (addr, opb, &octet))
7441
0
    return false;
7442
  /* The third and fourth lines below are testing that the section end
7443
     address is within the segment.  It's written this way to avoid
7444
     overflow.  Add seg_addr + section_size to both sides of the
7445
     inequality to make it obvious.  */
7446
2.75k
  return (octet >= seg_addr
7447
1.91k
    && segment_size (segment) >= section_size (section, segment)
7448
1.61k
    && (octet - seg_addr
7449
1.61k
        <= segment_size (segment) - section_size (section, segment)));
7450
2.75k
}
7451
7452
/* Handle PT_NOTE segment.  */
7453
static bool
7454
is_note (asection *s, Elf_Internal_Phdr *p)
7455
1.74k
{
7456
1.74k
  return (p->p_type == PT_NOTE
7457
272
    && elf_section_type (s) == SHT_NOTE
7458
20
    && (ufile_ptr) s->filepos >= p->p_offset
7459
17
    && p->p_filesz >= s->size
7460
17
    && (ufile_ptr) s->filepos - p->p_offset <= p->p_filesz - s->size);
7461
1.74k
}
7462
7463
/* Rewrite program header information.  */
7464
7465
static bool
7466
rewrite_elf_program_header (bfd *ibfd, bfd *obfd, bfd_vma maxpagesize)
7467
14
{
7468
14
  Elf_Internal_Ehdr *iehdr;
7469
14
  struct elf_segment_map *map;
7470
14
  struct elf_segment_map *map_first;
7471
14
  struct elf_segment_map **pointer_to_map;
7472
14
  Elf_Internal_Phdr *segment;
7473
14
  asection *section;
7474
14
  unsigned int i;
7475
14
  unsigned int num_segments;
7476
14
  bool phdr_included = false;
7477
14
  bool p_paddr_valid;
7478
14
  struct elf_segment_map *phdr_adjust_seg = NULL;
7479
14
  unsigned int phdr_adjust_num = 0;
7480
14
  elf_backend_data *bed;
7481
14
  unsigned int opb = bfd_octets_per_byte (ibfd, NULL);
7482
7483
14
  bed = get_elf_backend_data (ibfd);
7484
14
  iehdr = elf_elfheader (ibfd);
7485
7486
14
  map_first = NULL;
7487
14
  pointer_to_map = &map_first;
7488
7489
14
  num_segments = elf_elfheader (ibfd)->e_phnum;
7490
7491
  /* The complicated case when p_vaddr is 0 is to handle the Solaris
7492
     linker, which generates a PT_INTERP section with p_vaddr and
7493
     p_memsz set to 0.  */
7494
14
#define IS_SOLARIS_PT_INTERP(p, s)          \
7495
314
  (p->p_vaddr == 0              \
7496
314
   && p->p_paddr == 0              \
7497
314
   && p->p_memsz == 0              \
7498
314
   && p->p_filesz > 0              \
7499
314
   && (s->flags & SEC_HAS_CONTENTS) != 0        \
7500
314
   && s->size > 0              \
7501
314
   && (bfd_vma) s->filepos >= p->p_offset        \
7502
314
   && ((bfd_vma) s->filepos + s->size         \
7503
0
       <= p->p_offset + p->p_filesz))
7504
7505
  /* Decide if the given section should be included in the given segment.
7506
     A section will be included if:
7507
       1. It is within the address space of the segment -- we use the LMA
7508
    if that is set for the segment and the VMA otherwise,
7509
       2. It is an allocated section or a NOTE section in a PT_NOTE
7510
    segment.
7511
       3. There is an output section associated with it,
7512
       4. The section has not already been allocated to a previous segment.
7513
       5. PT_GNU_STACK segments do not include any sections.
7514
       6. PT_TLS segment includes only SHF_TLS sections.
7515
       7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments.
7516
       8. PT_DYNAMIC should not contain empty sections at the beginning
7517
    (with the possible exception of .dynamic).  */
7518
14
#define IS_SECTION_IN_INPUT_SEGMENT(section, segment, opb, paddr_valid) \
7519
2.80k
  (((is_contained_by (section, segment, segment->p_paddr,    \
7520
2.42k
          segment->p_vaddr, opb, !paddr_valid)    \
7521
2.42k
     && (section->flags & SEC_ALLOC) != 0)       \
7522
2.42k
    || is_note (section, segment))         \
7523
2.42k
   && segment->p_type != PT_GNU_STACK          \
7524
2.42k
   && (segment->p_type != PT_TLS          \
7525
685
       || (section->flags & SEC_THREAD_LOCAL))        \
7526
2.42k
   && (segment->p_type == PT_LOAD          \
7527
685
       || segment->p_type == PT_TLS          \
7528
685
       || (section->flags & SEC_THREAD_LOCAL) == 0)      \
7529
2.42k
   && (segment->p_type != PT_DYNAMIC          \
7530
670
       || section_size (section, segment) > 0        \
7531
670
       || (segment->p_paddr            \
7532
0
     ? segment->p_paddr != section->lma * (opb)      \
7533
0
     : segment->p_vaddr != section->vma * (opb))      \
7534
670
       || (streq (bfd_section_name (section), ".dynamic")))    \
7535
2.80k
   && (segment->p_type != PT_LOAD || !section->segment_mark))
7536
7537
/* If the output section of a section in the input segment is NULL,
7538
   it is removed from the corresponding output segment.   */
7539
14
#define INCLUDE_SECTION_IN_SEGMENT(section, segment, opb, paddr_valid)  \
7540
377
  (IS_SECTION_IN_INPUT_SEGMENT (section, segment, opb, paddr_valid) \
7541
377
   && section->output_section != NULL)
7542
7543
  /* Returns TRUE iff seg1 starts after the end of seg2.  */
7544
14
#define SEGMENT_AFTER_SEGMENT(seg1, seg2, field)      \
7545
39
  (seg1->field >= segment_end (seg2, seg2->field))
7546
7547
  /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both
7548
     their VMA address ranges and their LMA address ranges overlap.
7549
     It is possible to have overlapping VMA ranges without overlapping LMA
7550
     ranges.  RedBoot images for example can have both .data and .bss mapped
7551
     to the same VMA range, but with the .data section mapped to a different
7552
     LMA.  */
7553
14
#define SEGMENT_OVERLAPS(seg1, seg2)          \
7554
14
  (   !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr)      \
7555
13
  || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr))     \
7556
13
   && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr)      \
7557
3
  || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr)))
7558
7559
  /* Initialise the segment mark field, and discard stupid alignment.  */
7560
299
  for (section = ibfd->sections; section != NULL; section = section->next)
7561
285
    {
7562
285
      asection *o = section->output_section;
7563
285
      if (o != NULL && o->alignment_power >= (sizeof (bfd_vma) * 8) - 1)
7564
0
  o->alignment_power = 0;
7565
285
      section->segment_mark = false;
7566
285
    }
7567
7568
  /* The Solaris linker creates program headers in which all the
7569
     p_paddr fields are zero.  When we try to objcopy or strip such a
7570
     file, we get confused.  Check for this case, and if we find it
7571
     don't set the p_paddr_valid fields.  */
7572
14
  p_paddr_valid = false;
7573
14
  for (i = 0, segment = elf_tdata (ibfd)->phdr;
7574
14
       i < num_segments;
7575
14
       i++, segment++)
7576
14
    if (segment->p_paddr != 0)
7577
14
      {
7578
14
  p_paddr_valid = true;
7579
14
  break;
7580
14
      }
7581
7582
  /* Scan through the segments specified in the program header
7583
     of the input BFD.  For this first scan we look for overlaps
7584
     in the loadable segments.  These can be created by weird
7585
     parameters to objcopy.  Also, fix some solaris weirdness.  */
7586
14
  for (i = 0, segment = elf_tdata (ibfd)->phdr;
7587
132
       i < num_segments;
7588
118
       i++, segment++)
7589
118
    {
7590
118
      unsigned int j;
7591
118
      Elf_Internal_Phdr *segment2;
7592
7593
118
      if (segment->p_type == PT_INTERP)
7594
329
  for (section = ibfd->sections; section; section = section->next)
7595
314
    if (IS_SOLARIS_PT_INTERP (segment, section))
7596
0
      {
7597
        /* Mininal change so that the normal section to segment
7598
     assignment code will work.  */
7599
0
        segment->p_vaddr = section->vma * opb;
7600
0
        break;
7601
0
      }
7602
7603
118
      if (segment->p_type != PT_LOAD)
7604
91
  {
7605
    /* Remove PT_GNU_RELRO segment.  */
7606
91
    if (segment->p_type == PT_GNU_RELRO)
7607
14
      segment->p_type = PT_NULL;
7608
91
    continue;
7609
91
  }
7610
7611
      /* Determine if this segment overlaps any previous segments.  */
7612
94
      for (j = 0, segment2 = elf_tdata (ibfd)->phdr; j < i; j++, segment2++)
7613
67
  {
7614
67
    bfd_signed_vma extra_length;
7615
7616
67
    if (segment2->p_type != PT_LOAD
7617
13
        || !SEGMENT_OVERLAPS (segment, segment2))
7618
67
      continue;
7619
7620
    /* Merge the two segments together.  */
7621
0
    if (segment2->p_vaddr < segment->p_vaddr)
7622
0
      {
7623
        /* Extend SEGMENT2 to include SEGMENT and then delete
7624
     SEGMENT.  */
7625
0
        extra_length = (segment_end (segment, segment->p_vaddr)
7626
0
            - segment_end (segment2, segment2->p_vaddr));
7627
7628
0
        if (extra_length > 0)
7629
0
    {
7630
0
      segment2->p_memsz += extra_length;
7631
0
      segment2->p_filesz += extra_length;
7632
0
    }
7633
7634
0
        segment->p_type = PT_NULL;
7635
7636
        /* Since we have deleted P we must restart the outer loop.  */
7637
0
        i = 0;
7638
0
        segment = elf_tdata (ibfd)->phdr;
7639
0
        break;
7640
0
      }
7641
0
    else
7642
0
      {
7643
        /* Extend SEGMENT to include SEGMENT2 and then delete
7644
     SEGMENT2.  */
7645
0
        extra_length = (segment_end (segment2, segment2->p_vaddr)
7646
0
            - segment_end (segment, segment->p_vaddr));
7647
7648
0
        if (extra_length > 0)
7649
0
    {
7650
0
      segment->p_memsz += extra_length;
7651
0
      segment->p_filesz += extra_length;
7652
0
    }
7653
7654
0
        segment2->p_type = PT_NULL;
7655
0
      }
7656
0
  }
7657
27
    }
7658
7659
  /* The second scan attempts to assign sections to segments.  */
7660
14
  for (i = 0, segment = elf_tdata (ibfd)->phdr;
7661
132
       i < num_segments;
7662
118
       i++, segment++)
7663
118
    {
7664
118
      unsigned int section_count;
7665
118
      asection **sections;
7666
118
      asection *output_section;
7667
118
      unsigned int isec;
7668
118
      asection *matching_lma;
7669
118
      asection *suggested_lma;
7670
118
      unsigned int j;
7671
118
      size_t amt;
7672
118
      asection *first_section;
7673
7674
118
      if (segment->p_type == PT_NULL)
7675
14
  continue;
7676
7677
104
      first_section = NULL;
7678
      /* Compute how many sections might be placed into this segment.  */
7679
104
      for (section = ibfd->sections, section_count = 0;
7680
2.15k
     section != NULL;
7681
2.04k
     section = section->next)
7682
2.04k
  {
7683
    /* Find the first section in the input segment, which may be
7684
       removed from the corresponding output segment.   */
7685
2.04k
    if (IS_SECTION_IN_INPUT_SEGMENT (section, segment, opb, p_paddr_valid))
7686
335
      {
7687
335
        if (first_section == NULL)
7688
64
    first_section = section;
7689
335
        if (section->output_section != NULL)
7690
335
    ++section_count;
7691
335
      }
7692
2.04k
  }
7693
7694
      /* Allocate a segment map big enough to contain
7695
   all of the sections we have selected.  */
7696
104
      amt = sizeof (struct elf_segment_map) - sizeof (asection *);
7697
104
      amt += section_count * sizeof (asection *);
7698
104
      map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
7699
104
      if (map == NULL)
7700
0
  return false;
7701
7702
      /* Initialise the fields of the segment map.  Default to
7703
   using the physical address of the segment in the input BFD.  */
7704
104
      map->next = NULL;
7705
104
      map->p_type = segment->p_type;
7706
104
      map->p_flags = segment->p_flags;
7707
104
      map->p_flags_valid = 1;
7708
7709
104
      if (map->p_type == PT_LOAD
7710
27
    && (ibfd->flags & D_PAGED) != 0
7711
0
    && maxpagesize > 1
7712
0
    && segment->p_align > 1)
7713
0
  {
7714
0
    map->p_align = segment->p_align;
7715
0
    if (segment->p_align > maxpagesize)
7716
0
      map->p_align = maxpagesize;
7717
0
    map->p_align_valid = 1;
7718
0
  }
7719
7720
      /* If the first section in the input segment is removed, there is
7721
   no need to preserve segment physical address in the corresponding
7722
   output segment.  */
7723
104
      if (!first_section || first_section->output_section != NULL)
7724
104
  {
7725
104
    map->p_paddr = segment->p_paddr;
7726
104
    map->p_paddr_valid = p_paddr_valid;
7727
104
  }
7728
7729
      /* Determine if this segment contains the ELF file header
7730
   and if it contains the program headers themselves.  */
7731
104
      map->includes_filehdr = (segment->p_offset == 0
7732
22
             && segment->p_filesz >= iehdr->e_ehsize);
7733
104
      map->includes_phdrs = 0;
7734
7735
104
      if (!phdr_included || segment->p_type != PT_LOAD)
7736
92
  {
7737
92
    map->includes_phdrs =
7738
92
      (segment->p_offset <= (bfd_vma) iehdr->e_phoff
7739
28
       && (segment->p_offset + segment->p_filesz
7740
28
     >= ((bfd_vma) iehdr->e_phoff
7741
28
         + iehdr->e_phnum * iehdr->e_phentsize)));
7742
7743
92
    if (segment->p_type == PT_LOAD && map->includes_phdrs)
7744
13
      phdr_included = true;
7745
92
  }
7746
7747
104
      if (section_count == 0)
7748
40
  {
7749
    /* Special segments, such as the PT_PHDR segment, may contain
7750
       no sections, but ordinary, loadable segments should contain
7751
       something.  They are allowed by the ELF spec however, so only
7752
       a warning is produced.
7753
       Don't warn if an empty PT_LOAD contains the program headers.
7754
       There is however the valid use case of embedded systems which
7755
       have segments with p_filesz of 0 and a p_memsz > 0 to initialize
7756
       flash memory with zeros.  No warning is shown for that case.  */
7757
40
    if (segment->p_type == PT_LOAD
7758
3
        && !map->includes_phdrs
7759
3
        && (segment->p_filesz > 0 || segment->p_memsz == 0))
7760
      /* xgettext:c-format */
7761
3
      _bfd_error_handler
7762
3
        (_("%pB: warning: empty loadable segment detected"
7763
3
     " at vaddr=%#" PRIx64 ", is this intentional?"),
7764
3
         ibfd, (uint64_t) segment->p_vaddr);
7765
7766
40
    map->p_vaddr_offset = segment->p_vaddr / opb;
7767
40
    map->count = 0;
7768
40
    *pointer_to_map = map;
7769
40
    pointer_to_map = &map->next;
7770
7771
40
    continue;
7772
40
  }
7773
7774
      /* Now scan the sections in the input BFD again and attempt
7775
   to add their corresponding output sections to the segment map.
7776
   The problem here is how to handle an output section which has
7777
   been moved (ie had its LMA changed).  There are four possibilities:
7778
7779
   1. None of the sections have been moved.
7780
      In this case we can continue to use the segment LMA from the
7781
      input BFD.
7782
7783
   2. All of the sections have been moved by the same amount.
7784
      In this case we can change the segment's LMA to match the LMA
7785
      of the first section.
7786
7787
   3. Some of the sections have been moved, others have not.
7788
      In this case those sections which have not been moved can be
7789
      placed in the current segment which will have to have its size,
7790
      and possibly its LMA changed, and a new segment or segments will
7791
      have to be created to contain the other sections.
7792
7793
   4. The sections have been moved, but not by the same amount.
7794
      In this case we can change the segment's LMA to match the LMA
7795
      of the first section and we will have to create a new segment
7796
      or segments to contain the other sections.
7797
7798
   In order to save time, we allocate an array to hold the section
7799
   pointers that we are interested in.  As these sections get assigned
7800
   to a segment, they are removed from this array.  */
7801
7802
64
      amt = section_count * sizeof (asection *);
7803
64
      sections = (asection **) bfd_malloc (amt);
7804
64
      if (sections == NULL)
7805
0
  return false;
7806
7807
      /* Step One: Scan for segment vs section LMA conflicts.
7808
   Also add the sections to the section array allocated above.
7809
   Also add the sections to the current segment.  In the common
7810
   case, where the sections have not been moved, this means that
7811
   we have completely filled the segment, and there is nothing
7812
   more to do.  */
7813
64
      isec = 0;
7814
64
      matching_lma = NULL;
7815
64
      suggested_lma = NULL;
7816
7817
64
      for (section = first_section, j = 0;
7818
377
     section != NULL;
7819
313
     section = section->next)
7820
377
  {
7821
377
    if (INCLUDE_SECTION_IN_SEGMENT (section, segment, opb, p_paddr_valid))
7822
335
      {
7823
335
        output_section = section->output_section;
7824
7825
335
        sections[j++] = section;
7826
7827
        /* The Solaris native linker always sets p_paddr to 0.
7828
     We try to catch that case here, and set it to the
7829
     correct value.  Note - some backends require that
7830
     p_paddr be left as zero.  */
7831
335
        if (!p_paddr_valid
7832
0
      && segment->p_vaddr != 0
7833
0
      && !bed->want_p_paddr_set_to_zero
7834
0
      && isec == 0
7835
0
      && output_section->lma != 0
7836
0
      && (align_power (segment->p_vaddr
7837
0
           + (map->includes_filehdr
7838
0
              ? iehdr->e_ehsize : 0)
7839
0
           + (map->includes_phdrs
7840
0
              ? iehdr->e_phnum * iehdr->e_phentsize
7841
0
              : 0),
7842
0
           output_section->alignment_power * opb)
7843
0
          == (output_section->vma * opb)))
7844
0
    map->p_paddr = segment->p_vaddr;
7845
7846
        /* Match up the physical address of the segment with the
7847
     LMA address of the output section.  */
7848
335
        if (is_contained_by (output_section, segment, map->p_paddr,
7849
335
           0, opb, false)
7850
5
      || is_note (section, segment))
7851
335
    {
7852
335
      if (matching_lma == NULL
7853
271
          || output_section->lma < matching_lma->lma)
7854
79
        matching_lma = output_section;
7855
7856
      /* We assume that if the section fits within the segment
7857
         then it does not overlap any other section within that
7858
         segment.  */
7859
335
      map->sections[isec++] = output_section;
7860
335
    }
7861
0
        else if (suggested_lma == NULL)
7862
0
    suggested_lma = output_section;
7863
7864
335
        if (j == section_count)
7865
64
    break;
7866
335
      }
7867
377
  }
7868
7869
64
      BFD_ASSERT (j == section_count);
7870
7871
      /* Step Two: Adjust the physical address of the current segment,
7872
   if necessary.  */
7873
64
      if (isec == section_count)
7874
64
  {
7875
    /* All of the sections fitted within the segment as currently
7876
       specified.  This is the default case.  Add the segment to
7877
       the list of built segments and carry on to process the next
7878
       program header in the input BFD.  */
7879
64
    map->count = section_count;
7880
64
    *pointer_to_map = map;
7881
64
    pointer_to_map = &map->next;
7882
7883
64
    if (p_paddr_valid
7884
64
        && !bed->want_p_paddr_set_to_zero)
7885
64
      {
7886
64
        bfd_vma hdr_size = 0;
7887
64
        if (map->includes_filehdr)
7888
14
    hdr_size = iehdr->e_ehsize;
7889
64
        if (map->includes_phdrs)
7890
15
    hdr_size += iehdr->e_phnum * iehdr->e_phentsize;
7891
7892
        /* Account for padding before the first section in the
7893
     segment.  */
7894
64
        map->p_vaddr_offset = ((map->p_paddr + hdr_size) / opb
7895
64
             - matching_lma->lma);
7896
64
      }
7897
7898
64
    free (sections);
7899
64
    continue;
7900
64
  }
7901
0
      else
7902
0
  {
7903
    /* Change the current segment's physical address to match
7904
       the LMA of the first section that fitted, or if no
7905
       section fitted, the first section.  */
7906
0
    if (matching_lma == NULL)
7907
0
      matching_lma = suggested_lma;
7908
7909
0
    map->p_paddr = matching_lma->lma * opb;
7910
7911
    /* Offset the segment physical address from the lma
7912
       to allow for space taken up by elf headers.  */
7913
0
    if (map->includes_phdrs)
7914
0
      {
7915
0
        map->p_paddr -= iehdr->e_phnum * iehdr->e_phentsize;
7916
7917
        /* iehdr->e_phnum is just an estimate of the number
7918
     of program headers that we will need.  Make a note
7919
     here of the number we used and the segment we chose
7920
     to hold these headers, so that we can adjust the
7921
     offset when we know the correct value.  */
7922
0
        phdr_adjust_num = iehdr->e_phnum;
7923
0
        phdr_adjust_seg = map;
7924
0
      }
7925
7926
0
    if (map->includes_filehdr)
7927
0
      {
7928
0
        bfd_vma align = (bfd_vma) 1 << matching_lma->alignment_power;
7929
0
        map->p_paddr -= iehdr->e_ehsize;
7930
        /* We've subtracted off the size of headers from the
7931
     first section lma, but there may have been some
7932
     alignment padding before that section too.  Try to
7933
     account for that by adjusting the segment lma down to
7934
     the same alignment.  */
7935
0
        if (segment->p_align != 0 && segment->p_align < align)
7936
0
    align = segment->p_align;
7937
0
        map->p_paddr &= -(align * opb);
7938
0
      }
7939
0
  }
7940
7941
      /* Step Three: Loop over the sections again, this time assigning
7942
   those that fit to the current segment and removing them from the
7943
   sections array; but making sure not to leave large gaps.  Once all
7944
   possible sections have been assigned to the current segment it is
7945
   added to the list of built segments and if sections still remain
7946
   to be assigned, a new segment is constructed before repeating
7947
   the loop.  */
7948
0
      isec = 0;
7949
0
      do
7950
0
  {
7951
0
    map->count = 0;
7952
0
    suggested_lma = NULL;
7953
7954
    /* Fill the current segment with sections that fit.  */
7955
0
    for (j = 0; j < section_count; j++)
7956
0
      {
7957
0
        section = sections[j];
7958
7959
0
        if (section == NULL)
7960
0
    continue;
7961
7962
0
        output_section = section->output_section;
7963
7964
0
        BFD_ASSERT (output_section != NULL);
7965
7966
0
        if (is_contained_by (output_section, segment, map->p_paddr,
7967
0
           0, opb, false)
7968
0
      || is_note (section, segment))
7969
0
    {
7970
0
      if (map->count == 0)
7971
0
        {
7972
          /* If the first section in a segment does not start at
7973
       the beginning of the segment, then something is
7974
       wrong.  */
7975
0
          if (align_power (map->p_paddr
7976
0
               + (map->includes_filehdr
7977
0
            ? iehdr->e_ehsize : 0)
7978
0
               + (map->includes_phdrs
7979
0
            ? iehdr->e_phnum * iehdr->e_phentsize
7980
0
            : 0),
7981
0
               output_section->alignment_power * opb)
7982
0
        != output_section->lma * opb)
7983
0
      goto sorry;
7984
0
        }
7985
0
      else
7986
0
        {
7987
0
          asection *prev_sec;
7988
7989
0
          prev_sec = map->sections[map->count - 1];
7990
7991
          /* If the gap between the end of the previous section
7992
       and the start of this section is more than
7993
       maxpagesize then we need to start a new segment.  */
7994
0
          if ((BFD_ALIGN (prev_sec->lma + prev_sec->size,
7995
0
              maxpagesize)
7996
0
         < BFD_ALIGN (output_section->lma, maxpagesize))
7997
0
        || (prev_sec->lma + prev_sec->size
7998
0
            > output_section->lma))
7999
0
      {
8000
0
        if (suggested_lma == NULL)
8001
0
          suggested_lma = output_section;
8002
8003
0
        continue;
8004
0
      }
8005
0
        }
8006
8007
0
      map->sections[map->count++] = output_section;
8008
0
      ++isec;
8009
0
      sections[j] = NULL;
8010
0
      if (segment->p_type == PT_LOAD)
8011
0
        section->segment_mark = true;
8012
0
    }
8013
0
        else if (suggested_lma == NULL)
8014
0
    suggested_lma = output_section;
8015
0
      }
8016
8017
    /* PR 23932.  A corrupt input file may contain sections that cannot
8018
       be assigned to any segment - because for example they have a
8019
       negative size - or segments that do not contain any sections.
8020
       But there are also valid reasons why a segment can be empty.
8021
       So allow a count of zero.  */
8022
8023
    /* Add the current segment to the list of built segments.  */
8024
0
    *pointer_to_map = map;
8025
0
    pointer_to_map = &map->next;
8026
8027
0
    if (isec < section_count)
8028
0
      {
8029
        /* We still have not allocated all of the sections to
8030
     segments.  Create a new segment here, initialise it
8031
     and carry on looping.  */
8032
0
        amt = sizeof (struct elf_segment_map) - sizeof (asection *);
8033
0
        amt += section_count * sizeof (asection *);
8034
0
        map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
8035
0
        if (map == NULL)
8036
0
    {
8037
0
      free (sections);
8038
0
      return false;
8039
0
    }
8040
8041
        /* Initialise the fields of the segment map.  Set the physical
8042
     physical address to the LMA of the first section that has
8043
     not yet been assigned.  */
8044
0
        map->next = NULL;
8045
0
        map->p_type = segment->p_type;
8046
0
        map->p_flags = segment->p_flags;
8047
0
        map->p_flags_valid = 1;
8048
0
        map->p_paddr = suggested_lma->lma * opb;
8049
0
        map->p_paddr_valid = p_paddr_valid;
8050
0
        map->includes_filehdr = 0;
8051
0
        map->includes_phdrs = 0;
8052
0
      }
8053
8054
0
    continue;
8055
0
  sorry:
8056
0
    bfd_set_error (bfd_error_sorry);
8057
0
    free (sections);
8058
0
    return false;
8059
0
  }
8060
0
      while (isec < section_count);
8061
8062
0
      free (sections);
8063
0
    }
8064
8065
14
  elf_seg_map (obfd) = map_first;
8066
8067
  /* If we had to estimate the number of program headers that were
8068
     going to be needed, then check our estimate now and adjust
8069
     the offset if necessary.  */
8070
14
  if (phdr_adjust_seg != NULL)
8071
0
    {
8072
0
      unsigned int count;
8073
8074
0
      for (count = 0, map = map_first; map != NULL; map = map->next)
8075
0
  count++;
8076
8077
0
      if (count > phdr_adjust_num)
8078
0
  phdr_adjust_seg->p_paddr
8079
0
    -= (count - phdr_adjust_num) * iehdr->e_phentsize;
8080
8081
0
      for (map = map_first; map != NULL; map = map->next)
8082
0
  if (map->p_type == PT_PHDR)
8083
0
    {
8084
0
      bfd_vma adjust
8085
0
        = phdr_adjust_seg->includes_filehdr ? iehdr->e_ehsize : 0;
8086
0
      map->p_paddr = phdr_adjust_seg->p_paddr + adjust;
8087
0
      break;
8088
0
    }
8089
0
    }
8090
8091
14
#undef IS_SOLARIS_PT_INTERP
8092
14
#undef IS_SECTION_IN_INPUT_SEGMENT
8093
14
#undef INCLUDE_SECTION_IN_SEGMENT
8094
14
#undef SEGMENT_AFTER_SEGMENT
8095
14
#undef SEGMENT_OVERLAPS
8096
14
  return true;
8097
14
}
8098
8099
/* Return true if p_align in the ELF program header in ABFD is valid.  */
8100
8101
static bool
8102
elf_is_p_align_valid (bfd *abfd)
8103
21
{
8104
21
  unsigned int i;
8105
21
  Elf_Internal_Phdr *segment;
8106
21
  unsigned int num_segments;
8107
21
  elf_backend_data *bed = get_elf_backend_data (abfd);
8108
21
  bfd_size_type maxpagesize = bed->maxpagesize;
8109
21
  bfd_size_type p_align = bed->p_align;
8110
8111
  /* Return true if the default p_align value isn't set or the maximum
8112
     page size is the same as the minimum page size.  */
8113
21
  if (p_align == 0 || maxpagesize == bed->minpagesize)
8114
21
    return true;
8115
8116
  /* When the default p_align value is set, p_align may be set to the
8117
     default p_align value while segments are aligned to the maximum
8118
     page size.  In this case, the input p_align will be ignored and
8119
     the maximum page size will be used to align the output segments.  */
8120
0
  segment = elf_tdata (abfd)->phdr;
8121
0
  num_segments = elf_elfheader (abfd)->e_phnum;
8122
0
  for (i = 0; i < num_segments; i++, segment++)
8123
0
    if (segment->p_type == PT_LOAD
8124
0
  && (segment->p_align != p_align
8125
0
      || vma_page_aligned_bias (segment->p_vaddr,
8126
0
              segment->p_offset,
8127
0
              maxpagesize) != 0))
8128
0
      return true;
8129
8130
0
  return false;
8131
0
}
8132
8133
/* Copy ELF program header information.  */
8134
8135
static bool
8136
copy_elf_program_header (bfd *ibfd, bfd *obfd)
8137
21
{
8138
21
  Elf_Internal_Ehdr *iehdr;
8139
21
  struct elf_segment_map *map;
8140
21
  struct elf_segment_map *map_first;
8141
21
  struct elf_segment_map **pointer_to_map;
8142
21
  Elf_Internal_Phdr *segment;
8143
21
  unsigned int i;
8144
21
  unsigned int num_segments;
8145
21
  bool phdr_included = false;
8146
21
  bool p_paddr_valid;
8147
21
  bool p_palign_valid;
8148
21
  unsigned int opb = bfd_octets_per_byte (ibfd, NULL);
8149
8150
21
  iehdr = elf_elfheader (ibfd);
8151
8152
21
  map_first = NULL;
8153
21
  pointer_to_map = &map_first;
8154
8155
  /* If all the segment p_paddr fields are zero, don't set
8156
     map->p_paddr_valid.  */
8157
21
  p_paddr_valid = false;
8158
21
  num_segments = elf_elfheader (ibfd)->e_phnum;
8159
21
  for (i = 0, segment = elf_tdata (ibfd)->phdr;
8160
21
       i < num_segments;
8161
21
       i++, segment++)
8162
21
    if (segment->p_paddr != 0)
8163
21
      {
8164
21
  p_paddr_valid = true;
8165
21
  break;
8166
21
      }
8167
8168
21
  p_palign_valid = elf_is_p_align_valid (ibfd);
8169
8170
21
  for (i = 0, segment = elf_tdata (ibfd)->phdr;
8171
194
       i < num_segments;
8172
173
       i++, segment++)
8173
173
    {
8174
173
      asection *section;
8175
173
      unsigned int section_count;
8176
173
      size_t amt;
8177
173
      Elf_Internal_Shdr *this_hdr;
8178
173
      asection *first_section = NULL;
8179
173
      asection *lowest_section;
8180
8181
      /* Compute how many sections are in this segment.  */
8182
173
      for (section = ibfd->sections, section_count = 0;
8183
3.81k
     section != NULL;
8184
3.64k
     section = section->next)
8185
3.64k
  {
8186
3.64k
    this_hdr = &(elf_section_data(section)->this_hdr);
8187
3.64k
    if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
8188
436
      {
8189
436
        if (first_section == NULL)
8190
110
    first_section = section;
8191
436
        section_count++;
8192
436
      }
8193
3.64k
  }
8194
8195
      /* Allocate a segment map big enough to contain
8196
   all of the sections we have selected.  */
8197
173
      amt = sizeof (struct elf_segment_map) - sizeof (asection *);
8198
173
      amt += section_count * sizeof (asection *);
8199
173
      map = (struct elf_segment_map *) bfd_zalloc (obfd, amt);
8200
173
      if (map == NULL)
8201
0
  return false;
8202
8203
      /* Initialize the fields of the output segment map with the
8204
   input segment.  */
8205
173
      map->next = NULL;
8206
173
      map->p_type = segment->p_type;
8207
173
      map->p_flags = segment->p_flags;
8208
173
      map->p_flags_valid = 1;
8209
173
      map->p_paddr = segment->p_paddr;
8210
173
      map->p_paddr_valid = p_paddr_valid;
8211
173
      map->p_align = segment->p_align;
8212
      /* Keep p_align of PT_GNU_STACK for stack alignment.  */
8213
173
      map->p_align_valid = (map->p_type == PT_GNU_STACK
8214
164
          || p_palign_valid);
8215
173
      map->p_vaddr_offset = 0;
8216
8217
173
      if (map->p_type == PT_GNU_RELRO
8218
157
    || map->p_type == PT_GNU_STACK)
8219
25
  {
8220
    /* The PT_GNU_RELRO segment may contain the first a few
8221
       bytes in the .got.plt section even if the whole .got.plt
8222
       section isn't in the PT_GNU_RELRO segment.  We won't
8223
       change the size of the PT_GNU_RELRO segment.
8224
       Similarly, PT_GNU_STACK size is significant on uclinux
8225
       systems.    */
8226
25
    map->p_size = segment->p_memsz;
8227
25
    map->p_size_valid = 1;
8228
25
  }
8229
8230
      /* Determine if this segment contains the ELF file header
8231
   and if it contains the program headers themselves.  */
8232
173
      map->includes_filehdr = (segment->p_offset == 0
8233
26
             && segment->p_filesz >= iehdr->e_ehsize);
8234
8235
173
      map->includes_phdrs = 0;
8236
173
      if (! phdr_included || segment->p_type != PT_LOAD)
8237
155
  {
8238
155
    map->includes_phdrs =
8239
155
      (segment->p_offset <= (bfd_vma) iehdr->e_phoff
8240
43
       && (segment->p_offset + segment->p_filesz
8241
43
     >= ((bfd_vma) iehdr->e_phoff
8242
43
         + iehdr->e_phnum * iehdr->e_phentsize)));
8243
8244
155
    if (segment->p_type == PT_LOAD && map->includes_phdrs)
8245
18
      phdr_included = true;
8246
155
  }
8247
8248
173
      lowest_section = NULL;
8249
173
      if (section_count != 0)
8250
110
  {
8251
110
    unsigned int isec = 0;
8252
8253
110
    for (section = first_section;
8254
468
         section != NULL;
8255
358
         section = section->next)
8256
468
      {
8257
468
        this_hdr = &(elf_section_data(section)->this_hdr);
8258
468
        if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
8259
436
    {
8260
436
      map->sections[isec++] = section->output_section;
8261
436
      if ((section->flags & SEC_ALLOC) != 0)
8262
433
        {
8263
433
          bfd_vma seg_off;
8264
8265
433
          if (lowest_section == NULL
8266
324
        || section->lma < lowest_section->lma)
8267
112
      lowest_section = section;
8268
8269
          /* Section lmas are set up from PT_LOAD header
8270
       p_paddr in _bfd_elf_make_section_from_shdr.
8271
       If this header has a p_paddr that disagrees
8272
       with the section lma, flag the p_paddr as
8273
       invalid.  */
8274
433
          if ((section->flags & SEC_LOAD) != 0)
8275
418
      seg_off = this_hdr->sh_offset - segment->p_offset;
8276
15
          else
8277
15
      seg_off = this_hdr->sh_addr - segment->p_vaddr;
8278
433
          if (section->lma * opb - segment->p_paddr != seg_off)
8279
7
      map->p_paddr_valid = false;
8280
433
        }
8281
436
      if (isec == section_count)
8282
110
        break;
8283
436
    }
8284
468
      }
8285
110
  }
8286
8287
173
      if (section_count == 0)
8288
63
  map->p_vaddr_offset = segment->p_vaddr / opb;
8289
110
      else if (map->p_paddr_valid)
8290
106
  {
8291
    /* Account for padding before the first section in the segment.  */
8292
106
    bfd_vma hdr_size = 0;
8293
106
    if (map->includes_filehdr)
8294
17
      hdr_size = iehdr->e_ehsize;
8295
106
    if (map->includes_phdrs)
8296
16
      hdr_size += iehdr->e_phnum * iehdr->e_phentsize;
8297
8298
106
    map->p_vaddr_offset = ((map->p_paddr + hdr_size) / opb
8299
106
         - (lowest_section ? lowest_section->lma : 0));
8300
106
  }
8301
8302
173
      map->count = section_count;
8303
173
      *pointer_to_map = map;
8304
173
      pointer_to_map = &map->next;
8305
173
    }
8306
8307
21
  elf_seg_map (obfd) = map_first;
8308
21
  return true;
8309
21
}
8310
8311
/* Copy private BFD data.  This copies or rewrites ELF program header
8312
   information.  */
8313
8314
static bool
8315
copy_private_bfd_data (bfd *ibfd, bfd *obfd)
8316
35
{
8317
35
  bfd_vma maxpagesize;
8318
8319
35
  if (elf_tdata (ibfd)->phdr == NULL)
8320
0
    return true;
8321
8322
35
  if (ibfd->xvec == obfd->xvec)
8323
35
    {
8324
      /* Check to see if any sections in the input BFD
8325
   covered by ELF program header have changed.  */
8326
35
      Elf_Internal_Phdr *segment;
8327
35
      asection * section;
8328
35
      asection * osec;
8329
35
      asection * prev;
8330
35
      unsigned int i, num_segments;
8331
35
      Elf_Internal_Shdr *this_hdr;
8332
35
      elf_backend_data *bed;
8333
8334
35
      bed = get_elf_backend_data (ibfd);
8335
8336
      /* Regenerate the segment map if p_paddr is set to 0.  */
8337
35
      if (bed->want_p_paddr_set_to_zero)
8338
0
  goto rewrite;
8339
8340
      /* Initialize the segment mark field.  */
8341
767
      for (section = obfd->sections; section != NULL;
8342
732
     section = section->next)
8343
732
  section->segment_mark = false;
8344
8345
35
      num_segments = elf_elfheader (ibfd)->e_phnum;
8346
35
      for (i = 0, segment = elf_tdata (ibfd)->phdr;
8347
238
     i < num_segments;
8348
203
     i++, segment++)
8349
217
  {
8350
    /* PR binutils/3535.  The Solaris linker always sets the p_paddr
8351
       and p_memsz fields of special segments (DYNAMIC, INTERP) to 0
8352
       which severly confuses things, so always regenerate the segment
8353
       map in this case.  */
8354
217
    if (segment->p_paddr == 0
8355
40
        && segment->p_memsz == 0
8356
8
        && (segment->p_type == PT_INTERP
8357
8
      || segment->p_type == PT_DYNAMIC))
8358
0
      goto rewrite;
8359
8360
217
    for (section = ibfd->sections, prev = NULL;
8361
4.57k
         section != NULL; section = section->next)
8362
4.36k
      {
8363
        /* We mark the output section so that we know it comes
8364
     from the input BFD.  */
8365
4.36k
        osec = section->output_section;
8366
4.36k
        if (osec)
8367
4.36k
    osec->segment_mark = true;
8368
8369
        /* Check if this section is covered by the segment.  */
8370
4.36k
        this_hdr = &(elf_section_data(section)->this_hdr);
8371
4.36k
        if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
8372
508
    {
8373
      /* FIXME: Check if its output section is changed or
8374
         removed.  What else do we need to check?  */
8375
508
      if (osec == NULL
8376
508
          || section->flags != osec->flags
8377
508
          || section->lma != osec->lma
8378
508
          || section->vma != osec->vma
8379
508
          || section->size != osec->size
8380
508
          || section->rawsize != osec->rawsize
8381
508
          || section->alignment_power != osec->alignment_power)
8382
0
        goto rewrite;
8383
8384
      /* PR 31450: If this is an allocated section then make sure
8385
         that this section's vma to lma relationship is the same
8386
         as previous (allocated) section's.  */
8387
508
      if (prev != NULL
8388
378
          && section->flags & SEC_ALLOC
8389
372
          && section->lma - section->vma != prev->lma - prev->vma)
8390
14
        goto rewrite;
8391
8392
494
      if (section->flags & SEC_ALLOC)
8393
485
        prev = section;
8394
494
    }
8395
4.36k
      }
8396
217
  }
8397
8398
      /* Check to see if any output section do not come from the
8399
   input BFD.  */
8400
468
      for (section = obfd->sections; section != NULL;
8401
447
     section = section->next)
8402
447
  {
8403
447
    if (!section->segment_mark)
8404
0
      goto rewrite;
8405
447
    else
8406
447
      section->segment_mark = false;
8407
447
  }
8408
8409
21
      return copy_elf_program_header (ibfd, obfd);
8410
21
    }
8411
8412
14
 rewrite:
8413
14
  maxpagesize = 0;
8414
14
  if (ibfd->xvec == obfd->xvec)
8415
14
    {
8416
      /* When rewriting program header, set the output maxpagesize to
8417
   the maximum alignment of input PT_LOAD segments.  */
8418
14
      Elf_Internal_Phdr *segment;
8419
14
      unsigned int i;
8420
14
      unsigned int num_segments = elf_elfheader (ibfd)->e_phnum;
8421
8422
14
      for (i = 0, segment = elf_tdata (ibfd)->phdr;
8423
132
     i < num_segments;
8424
118
     i++, segment++)
8425
118
  if (segment->p_type == PT_LOAD
8426
27
      && maxpagesize < segment->p_align)
8427
14
    {
8428
      /* PR 17512: file: f17299af.  */
8429
14
      if (segment->p_align > (bfd_vma) 1 << ((sizeof (bfd_vma) * 8) - 2))
8430
        /* xgettext:c-format */
8431
0
        _bfd_error_handler (_("%pB: warning: segment alignment of %#"
8432
0
            PRIx64 " is too large"),
8433
0
          ibfd, (uint64_t) segment->p_align);
8434
14
      else
8435
14
        maxpagesize = segment->p_align;
8436
14
    }
8437
14
    }
8438
14
  if (maxpagesize == 0)
8439
0
    maxpagesize = get_elf_backend_data (obfd)->maxpagesize;
8440
8441
14
  return rewrite_elf_program_header (ibfd, obfd, maxpagesize);
8442
35
}
8443
8444
/* Copy private section information from input to output.  This function
8445
   is called both by objcopy where LINK_INFO is NULL, and ld where
8446
   LINK_INFO is non-NULL in the usual case but NULL for a special case
8447
   when dealing with LTO IR or cloning output sections.  */
8448
8449
bool
8450
_bfd_elf_copy_private_section_data (bfd *ibfd,
8451
            asection *isec,
8452
            bfd *obfd ATTRIBUTE_UNUSED,
8453
            asection *osec,
8454
            struct bfd_link_info *link_info)
8455
1.72k
{
8456
1.72k
  if (ibfd->xvec->flavour != bfd_target_elf_flavour)
8457
0
    return true;
8458
8459
1.72k
  Elf_Internal_Shdr *ihdr = &elf_section_data (isec)->this_hdr;
8460
1.72k
  Elf_Internal_Shdr *ohdr = &elf_section_data (osec)->this_hdr;
8461
1.72k
  if (link_info == NULL)
8462
1.72k
    {
8463
1.72k
      ohdr->sh_entsize = ihdr->sh_entsize;
8464
8465
1.72k
      if (ihdr->sh_type == SHT_SYMTAB
8466
1.72k
    || ihdr->sh_type == SHT_DYNSYM
8467
1.69k
    || ihdr->sh_type == SHT_GNU_verneed
8468
1.69k
    || ihdr->sh_type == SHT_GNU_verdef)
8469
28
  ohdr->sh_info = ihdr->sh_info;
8470
1.72k
    }
8471
8472
  /* If this is a known ABI section, ELF section type and flags may
8473
     have been set up when OSEC was created.  For normal sections we
8474
     allow the user to override the type and flags other than
8475
     SHF_MASKOS and SHF_MASKPROC.  */
8476
1.72k
  if (elf_section_type (osec) == SHT_PROGBITS
8477
1.17k
      || elf_section_type (osec) == SHT_NOTE
8478
1.06k
      || elf_section_type (osec) == SHT_NOBITS)
8479
692
    elf_section_type (osec) = SHT_NULL;
8480
8481
  /* For objcopy and relocatable link, copy the ELF section type from
8482
     the input file if the BFD section flags are the same.  (If they
8483
     are different the user may be doing something like
8484
     "objcopy --set-section-flags .text=alloc,data".)  For a final
8485
     link allow some flags that the linker clears to differ.  */
8486
1.72k
  bool final_link = (link_info != NULL
8487
0
         && !bfd_link_relocatable (link_info));
8488
1.72k
  if (elf_section_type (osec) == SHT_NULL
8489
1.47k
      && (osec->flags == isec->flags
8490
0
    || (final_link
8491
0
        && ((osec->flags ^ isec->flags)
8492
0
      & ~(SEC_LINK_ONCE | SEC_LINK_DUPLICATES | SEC_RELOC)) == 0)))
8493
1.47k
    elf_section_type (osec) = elf_section_type (isec);
8494
8495
1.72k
  elf_section_flags (osec) = elf_section_flags (isec);
8496
  /* Like for type, retain flags for objcopy (yet unlike for type, don't do so
8497
     for relocatable link).  Same heuristic as there: If the BFD section flags
8498
     are different, assume --set-section-flags is in use for the section.
8499
8500
     FIXME: Is this correct for all OS/PROC specific flags?  */
8501
1.72k
  if (link_info != NULL || osec->flags != isec->flags)
8502
0
    elf_section_flags (osec) &= (SHF_MASKOS | SHF_MASKPROC);
8503
1.72k
  else
8504
1.72k
    {
8505
      /* Clear only flags which are set below or elsewhere.  */
8506
1.72k
      elf_section_flags (osec) &= ~(SHF_WRITE | SHF_ALLOC | SHF_EXECINSTR
8507
1.72k
            | SHF_MERGE | SHF_STRINGS | SHF_LINK_ORDER
8508
1.72k
            | SHF_INFO_LINK | SHF_GROUP | SHF_TLS
8509
1.72k
            | SHF_COMPRESSED);
8510
1.72k
      if (elf_section_flags (osec) & ~(SHF_MASKOS | SHF_MASKPROC))
8511
36
  _bfd_error_handler
8512
36
    (_("%pB:%pA: warning: retaining unknown section flag(s) %#" PRIx64),
8513
36
     ibfd, isec,
8514
36
     (uint64_t) (elf_section_flags (osec)
8515
36
           & ~(SHF_MASKOS | SHF_MASKPROC)));
8516
1.72k
    }
8517
8518
  /* Copy sh_info from input for mbind section.  */
8519
1.72k
  if ((elf_tdata (ibfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0
8520
207
      && elf_section_flags (isec) & SHF_GNU_MBIND)
8521
16
    elf_section_data (osec)->this_hdr.sh_info
8522
16
      = elf_section_data (isec)->this_hdr.sh_info;
8523
8524
  /* Set things up for objcopy and relocatable link.  The output
8525
     SHT_GROUP section will have its elf_next_in_group pointing back
8526
     to the input group members.  Ignore linker created group section.
8527
     See elfNN_ia64_object_p in elfxx-ia64.c.  */
8528
1.72k
  if ((link_info == NULL
8529
0
       || !link_info->resolve_section_groups)
8530
1.72k
      && (elf_sec_group (isec) == NULL
8531
81
    || (elf_sec_group (isec)->flags & SEC_LINKER_CREATED) == 0))
8532
1.72k
    {
8533
1.72k
      if (elf_section_flags (isec) & SHF_GROUP)
8534
105
  elf_section_flags (osec) |= SHF_GROUP;
8535
1.72k
      elf_next_in_group (osec) = elf_next_in_group (isec);
8536
1.72k
      elf_section_data (osec)->group = elf_section_data (isec)->group;
8537
1.72k
    }
8538
8539
  /* If not decompress, preserve SHF_COMPRESSED.  */
8540
1.72k
  if (!final_link && (ibfd->flags & BFD_DECOMPRESS) == 0)
8541
1.72k
    elf_section_flags (osec) |= (elf_section_flags (isec)
8542
1.72k
         & SHF_COMPRESSED);
8543
8544
  /* We need to handle elf_linked_to_section for SHF_LINK_ORDER. We
8545
     don't use the output section of the linked-to section since it
8546
     may be NULL at this point.  */
8547
1.72k
  if ((ihdr->sh_flags & SHF_LINK_ORDER) != 0)
8548
31
    {
8549
31
      ohdr->sh_flags |= SHF_LINK_ORDER;
8550
31
      elf_linked_to_section (osec) = elf_linked_to_section (isec);
8551
31
    }
8552
8553
1.72k
  osec->use_rela_p = isec->use_rela_p;
8554
8555
1.72k
  return true;
8556
1.72k
}
8557
8558
/* Look at all the SHT_GROUP sections in IBFD, making any adjustments
8559
   necessary if we are removing either the SHT_GROUP section or any of
8560
   the group member sections.  DISCARDED is the value that a section's
8561
   output_section has if the section will be discarded, NULL when this
8562
   function is called from objcopy, bfd_abs_section_ptr when called
8563
   from the linker.  */
8564
8565
bool
8566
_bfd_elf_fixup_group_sections (bfd *ibfd, asection *discarded)
8567
91
{
8568
91
  asection *isec;
8569
8570
1.79k
  for (isec = ibfd->sections; isec != NULL; isec = isec->next)
8571
1.70k
    if (elf_section_type (isec) == SHT_GROUP)
8572
32
      {
8573
32
  asection *first = elf_next_in_group (isec);
8574
32
  asection *s = first;
8575
32
  bfd_size_type removed = 0;
8576
8577
81
  while (s != NULL)
8578
81
    {
8579
      /* If this member section is being output but the
8580
         SHT_GROUP section is not, then clear the group info
8581
         set up by _bfd_elf_copy_private_section_data.  */
8582
81
      if (s->output_section != discarded
8583
81
    && isec->output_section == discarded)
8584
0
        {
8585
0
    elf_section_flags (s->output_section) &= ~SHF_GROUP;
8586
0
    elf_group_name (s->output_section) = NULL;
8587
0
        }
8588
81
      else
8589
81
        {
8590
81
    struct bfd_elf_section_data *elf_sec = elf_section_data (s);
8591
81
    if (s->output_section == discarded
8592
0
        && isec->output_section != discarded)
8593
0
      {
8594
        /* Conversely, if the member section is not being
8595
           output but the SHT_GROUP section is, then adjust
8596
           its size.  */
8597
0
        removed += 4;
8598
0
        if (elf_sec->rel.hdr != NULL
8599
0
      && (elf_sec->rel.hdr->sh_flags & SHF_GROUP) != 0)
8600
0
          removed += 4;
8601
0
        if (elf_sec->rela.hdr != NULL
8602
0
      && (elf_sec->rela.hdr->sh_flags & SHF_GROUP) != 0)
8603
0
          removed += 4;
8604
0
      }
8605
81
    else
8606
81
      {
8607
        /* Also adjust for zero-sized relocation member
8608
           section.  */
8609
81
        if (elf_sec->rel.hdr != NULL
8610
0
      && elf_sec->rel.hdr->sh_size == 0)
8611
0
          removed += 4;
8612
81
        if (elf_sec->rela.hdr != NULL
8613
59
      && elf_sec->rela.hdr->sh_size == 0)
8614
0
          removed += 4;
8615
81
      }
8616
81
        }
8617
81
      s = elf_next_in_group (s);
8618
81
      if (s == first)
8619
32
        break;
8620
81
    }
8621
32
  if (removed != 0)
8622
0
    {
8623
0
      if (discarded != NULL)
8624
0
        {
8625
    /* If we've been called for ld -r, then we need to
8626
       adjust the input section size.  */
8627
0
    if (isec->rawsize == 0)
8628
0
      isec->rawsize = isec->size;
8629
0
    isec->size = isec->rawsize - removed;
8630
0
    if (isec->size <= 4)
8631
0
      {
8632
0
        isec->size = 0;
8633
0
        isec->flags |= SEC_EXCLUDE;
8634
0
      }
8635
0
        }
8636
0
      else if (isec->output_section != NULL)
8637
0
        {
8638
    /* Adjust the output section size when called from
8639
       objcopy. */
8640
0
    isec->output_section->size -= removed;
8641
0
    if (isec->output_section->size <= 4)
8642
0
      {
8643
0
        isec->output_section->size = 0;
8644
0
        isec->output_section->flags |= SEC_EXCLUDE;
8645
0
      }
8646
0
        }
8647
0
    }
8648
32
      }
8649
8650
91
  return true;
8651
91
}
8652
8653
/* Copy private header information.  */
8654
8655
bool
8656
_bfd_elf_copy_private_header_data (bfd *ibfd, bfd *obfd)
8657
91
{
8658
91
  if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
8659
0
    return true;
8660
8661
  /* Copy over private BFD data if it has not already been copied.
8662
     This must be done here, rather than in the copy_private_bfd_data
8663
     entry point, because the latter is called after the section
8664
     contents have been set, which means that the program headers have
8665
     already been worked out.  */
8666
91
  if (elf_seg_map (obfd) == NULL && elf_tdata (ibfd)->phdr != NULL)
8667
35
    {
8668
35
      if (! copy_private_bfd_data (ibfd, obfd))
8669
0
  return false;
8670
35
    }
8671
8672
91
  return _bfd_elf_fixup_group_sections (ibfd, NULL);
8673
91
}
8674
8675
/* Copy private symbol information.  If this symbol is in a section
8676
   which we did not map into a BFD section, try to map the section
8677
   index correctly.  We use special macro definitions for the mapped
8678
   section indices; these definitions are interpreted by the
8679
   swap_out_syms function.  */
8680
8681
8
#define MAP_ONESYMTAB (SHN_HIOS + 1)
8682
0
#define MAP_DYNSYMTAB (SHN_HIOS + 2)
8683
45
#define MAP_STRTAB    (SHN_HIOS + 3)
8684
5
#define MAP_SHSTRTAB  (SHN_HIOS + 4)
8685
5
#define MAP_SYM_SHNDX (SHN_HIOS + 5)
8686
8687
bool
8688
_bfd_elf_copy_private_symbol_data (bfd *ibfd,
8689
           asymbol **isymarg,
8690
           bfd *obfd ATTRIBUTE_UNUSED,
8691
           asymbol **osymarg)
8692
18.4k
{
8693
18.4k
  if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
8694
0
    return true;
8695
8696
18.4k
  elf_symbol_type *isym = elf_symbol_from (*isymarg);
8697
18.4k
  elf_symbol_type *osym = elf_symbol_from (*osymarg);
8698
18.4k
  if (isym != NULL
8699
18.4k
      && isym->internal_elf_sym.st_shndx != 0
8700
1.42k
      && osym != NULL
8701
1.42k
      && bfd_is_abs_section (isym->symbol.section))
8702
609
    {
8703
609
      unsigned int shndx;
8704
8705
609
      shndx = isym->internal_elf_sym.st_shndx;
8706
609
      if (shndx == elf_onesymtab (ibfd))
8707
4
  shndx = MAP_ONESYMTAB;
8708
605
      else if (shndx == elf_dynsymtab (ibfd))
8709
0
  shndx = MAP_DYNSYMTAB;
8710
605
      else if (shndx == elf_elfsections (ibfd)[elf_onesymtab (ibfd)]->sh_link)
8711
23
  shndx = MAP_STRTAB;
8712
582
      else if (shndx == elf_elfheader (ibfd)->e_shstrndx)
8713
5
  shndx = MAP_SHSTRTAB;
8714
577
      else if (find_section_in_list (shndx, elf_symtab_shndx_list (ibfd)))
8715
3
  shndx = MAP_SYM_SHNDX;
8716
609
      osym->internal_elf_sym.st_shndx = shndx;
8717
609
    }
8718
8719
18.4k
  return true;
8720
18.4k
}
8721
8722
/* Swap out the symbols.  */
8723
8724
static bool
8725
swap_out_syms (bfd *abfd,
8726
         struct elf_strtab_hash **sttp,
8727
         int relocatable_p,
8728
         struct bfd_link_info *info)
8729
56
{
8730
56
  elf_backend_data *bed;
8731
56
  unsigned int symcount;
8732
56
  asymbol **syms;
8733
56
  struct elf_strtab_hash *stt;
8734
56
  Elf_Internal_Shdr *symtab_hdr;
8735
56
  Elf_Internal_Shdr *symtab_shndx_hdr;
8736
56
  Elf_Internal_Shdr *symstrtab_hdr;
8737
56
  struct elf_sym_strtab *symstrtab;
8738
56
  bfd_byte *outbound_syms;
8739
56
  bfd_byte *outbound_shndx;
8740
56
  unsigned long outbound_syms_index;
8741
56
  unsigned int idx;
8742
56
  unsigned int num_locals;
8743
56
  size_t amt;
8744
56
  bool name_local_sections;
8745
8746
56
  if (!elf_map_symbols (abfd, &num_locals))
8747
0
    return false;
8748
8749
  /* Dump out the symtabs.  */
8750
56
  stt = _bfd_elf_strtab_init ();
8751
56
  if (stt == NULL)
8752
0
    return false;
8753
8754
56
  bed = get_elf_backend_data (abfd);
8755
56
  symcount = bfd_get_symcount (abfd);
8756
56
  symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
8757
56
  symtab_hdr->sh_type = SHT_SYMTAB;
8758
56
  symtab_hdr->sh_entsize = bed->s->sizeof_sym;
8759
56
  symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1);
8760
56
  symtab_hdr->sh_info = num_locals + 1;
8761
56
  symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
8762
8763
56
  symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
8764
56
  symstrtab_hdr->sh_type = SHT_STRTAB;
8765
8766
  /* Allocate buffer to swap out the .strtab section.  */
8767
56
  if (_bfd_mul_overflow (symcount + 1, sizeof (*symstrtab), &amt)
8768
56
      || (symstrtab = (struct elf_sym_strtab *) bfd_malloc (amt)) == NULL)
8769
0
    {
8770
0
      bfd_set_error (bfd_error_no_memory);
8771
0
      _bfd_elf_strtab_free (stt);
8772
0
      return false;
8773
0
    }
8774
8775
56
  if (_bfd_mul_overflow (symcount + 1, bed->s->sizeof_sym, &amt)
8776
56
      || (outbound_syms = bfd_malloc (amt)) == NULL)
8777
0
    {
8778
0
    error_no_mem:
8779
0
      bfd_set_error (bfd_error_no_memory);
8780
0
    error_return:
8781
0
      free (symstrtab);
8782
0
      _bfd_elf_strtab_free (stt);
8783
0
      return false;
8784
0
    }
8785
56
  symtab_hdr->contents = outbound_syms;
8786
56
  outbound_syms_index = 0;
8787
8788
56
  outbound_shndx = NULL;
8789
8790
56
  if (elf_symtab_shndx_list (abfd))
8791
0
    {
8792
0
      symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
8793
0
      if (symtab_shndx_hdr->sh_name != 0)
8794
0
  {
8795
0
    if (_bfd_mul_overflow (symcount + 1,
8796
0
         sizeof (Elf_External_Sym_Shndx), &amt))
8797
0
      goto error_no_mem;
8798
0
    outbound_shndx =  (bfd_byte *) bfd_zalloc (abfd, amt);
8799
0
    if (outbound_shndx == NULL)
8800
0
      goto error_return;
8801
8802
0
    symtab_shndx_hdr->contents = outbound_shndx;
8803
0
    symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
8804
0
    symtab_shndx_hdr->sh_size = amt;
8805
0
    symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
8806
0
    symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
8807
0
  }
8808
      /* FIXME: What about any other headers in the list ?  */
8809
0
    }
8810
8811
  /* Now generate the data (for "contents").  */
8812
56
  {
8813
    /* Fill in zeroth symbol and swap it out.  */
8814
56
    Elf_Internal_Sym sym;
8815
56
    sym.st_name = 0;
8816
56
    sym.st_value = 0;
8817
56
    sym.st_size = 0;
8818
56
    sym.st_info = 0;
8819
56
    sym.st_other = 0;
8820
56
    sym.st_shndx = SHN_UNDEF;
8821
56
    sym.st_target_internal = 0;
8822
56
    symstrtab[outbound_syms_index].sym = sym;
8823
56
    symstrtab[outbound_syms_index].dest_index = outbound_syms_index;
8824
56
    outbound_syms_index++;
8825
56
  }
8826
8827
56
  name_local_sections
8828
56
    = (bed->elf_backend_name_local_section_symbols
8829
10
       && bed->elf_backend_name_local_section_symbols (abfd));
8830
8831
56
  syms = bfd_get_outsymbols (abfd);
8832
1.80k
  for (idx = 0; idx < symcount; idx++)
8833
1.74k
    {
8834
1.74k
      Elf_Internal_Sym sym;
8835
8836
1.74k
      flagword flags = syms[idx]->flags;
8837
1.74k
      if (!name_local_sections
8838
1.63k
    && (flags & (BSF_SECTION_SYM | BSF_GLOBAL)) == BSF_SECTION_SYM)
8839
122
  {
8840
    /* Local section symbols have no name.  */
8841
122
    sym.st_name = 0;
8842
122
  }
8843
1.62k
      else
8844
1.62k
  {
8845
    /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
8846
       to get the final offset for st_name.  */
8847
1.62k
    size_t stridx = _bfd_elf_strtab_add (stt, syms[idx]->name, false);
8848
1.62k
    if (stridx == (size_t) -1)
8849
0
      goto error_return;
8850
1.62k
    sym.st_name = stridx;
8851
1.62k
  }
8852
8853
1.74k
      bfd_vma value = syms[idx]->value;
8854
1.74k
      elf_symbol_type *type_ptr = elf_symbol_from (syms[idx]);
8855
1.74k
      asection *sec = syms[idx]->section;
8856
8857
1.74k
      if ((flags & BSF_SECTION_SYM) == 0 && bfd_is_com_section (sec))
8858
3
  {
8859
    /* ELF common symbols put the alignment into the `value' field,
8860
       and the size into the `size' field.  This is backwards from
8861
       how BFD handles it, so reverse it here.  */
8862
3
    sym.st_size = value;
8863
3
    if (type_ptr == NULL
8864
3
        || type_ptr->internal_elf_sym.st_value == 0)
8865
1
      sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value));
8866
2
    else
8867
2
      sym.st_value = type_ptr->internal_elf_sym.st_value;
8868
3
    sym.st_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
8869
3
  }
8870
1.74k
      else
8871
1.74k
  {
8872
1.74k
    unsigned int shndx;
8873
8874
1.74k
    if (sec->output_section)
8875
1.74k
      {
8876
1.74k
        value += sec->output_offset;
8877
1.74k
        sec = sec->output_section;
8878
1.74k
      }
8879
8880
    /* Don't add in the section vma for relocatable output.  */
8881
1.74k
    if (! relocatable_p)
8882
946
      value += sec->vma;
8883
1.74k
    sym.st_value = value;
8884
1.74k
    sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0;
8885
8886
1.74k
    if (bfd_is_abs_section (sec)
8887
531
        && type_ptr != NULL
8888
531
        && type_ptr->internal_elf_sym.st_shndx != 0)
8889
531
      {
8890
        /* This symbol is in a real ELF section which we did
8891
     not create as a BFD section.  Undo the mapping done
8892
     by copy_private_symbol_data.  */
8893
531
        shndx = type_ptr->internal_elf_sym.st_shndx;
8894
531
        switch (shndx)
8895
531
    {
8896
4
    case MAP_ONESYMTAB:
8897
4
      shndx = elf_onesymtab (abfd);
8898
4
      break;
8899
0
    case MAP_DYNSYMTAB:
8900
0
      shndx = elf_dynsymtab (abfd);
8901
0
      break;
8902
22
    case MAP_STRTAB:
8903
22
      shndx = elf_strtab_sec (abfd);
8904
22
      break;
8905
0
    case MAP_SHSTRTAB:
8906
0
      shndx = elf_shstrtab_sec (abfd);
8907
0
      break;
8908
2
    case MAP_SYM_SHNDX:
8909
2
      if (elf_symtab_shndx_list (abfd))
8910
0
        shndx = elf_symtab_shndx_list (abfd)->ndx;
8911
2
      break;
8912
0
    case SHN_COMMON:
8913
22
    case SHN_ABS:
8914
22
      shndx = SHN_ABS;
8915
22
      break;
8916
481
    default:
8917
481
      if (shndx >= SHN_LOPROC && shndx <= SHN_HIOS)
8918
0
        {
8919
0
          if (bed->symbol_section_index)
8920
0
      shndx = bed->symbol_section_index (abfd, type_ptr);
8921
          /* Otherwise just leave the index alone.  */
8922
0
        }
8923
481
      else
8924
481
        {
8925
481
          if (shndx > SHN_HIOS && shndx < SHN_HIRESERVE)
8926
1
      _bfd_error_handler (_("%pB: \
8927
1
Unable to handle section index %x in ELF symbol.  Using ABS instead."),
8928
1
            abfd, shndx);
8929
481
          shndx = SHN_ABS;
8930
481
        }
8931
481
      break;
8932
531
    }
8933
531
      }
8934
1.21k
    else
8935
1.21k
      {
8936
1.21k
        shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
8937
8938
1.21k
        if (shndx == SHN_BAD)
8939
0
    {
8940
0
      asection *sec2;
8941
8942
      /* Writing this would be a hell of a lot easier if
8943
         we had some decent documentation on bfd, and
8944
         knew what to expect of the library, and what to
8945
         demand of applications.  For example, it
8946
         appears that `objcopy' might not set the
8947
         section of a symbol to be a section that is
8948
         actually in the output file.  */
8949
0
      sec2 = bfd_get_section_by_name (abfd, sec->name);
8950
0
      if (sec2 != NULL)
8951
0
        shndx = _bfd_elf_section_from_bfd_section (abfd, sec2);
8952
0
      if (shndx == SHN_BAD)
8953
0
        {
8954
          /* xgettext:c-format */
8955
0
          _bfd_error_handler
8956
0
      (_("unable to find equivalent output section"
8957
0
         " for symbol '%s' from section '%s'"),
8958
0
       syms[idx]->name ? syms[idx]->name : "<Local sym>",
8959
0
       sec->name);
8960
0
          bfd_set_error (bfd_error_invalid_operation);
8961
0
          goto error_return;
8962
0
        }
8963
0
    }
8964
1.21k
      }
8965
8966
1.74k
    sym.st_shndx = shndx;
8967
1.74k
  }
8968
8969
1.74k
      int type;
8970
1.74k
      if ((flags & BSF_THREAD_LOCAL) != 0)
8971
34
  type = STT_TLS;
8972
1.71k
      else if ((flags & BSF_GNU_INDIRECT_FUNCTION) != 0)
8973
14
  type = STT_GNU_IFUNC;
8974
1.70k
      else if ((flags & BSF_FUNCTION) != 0)
8975
207
  type = STT_FUNC;
8976
1.49k
      else if ((flags & BSF_OBJECT) != 0)
8977
264
  type = STT_OBJECT;
8978
1.23k
      else if ((flags & BSF_RELC) != 0)
8979
11
  type = STT_RELC;
8980
1.21k
      else if ((flags & BSF_SRELC) != 0)
8981
167
  type = STT_SRELC;
8982
1.05k
      else
8983
1.05k
  type = STT_NOTYPE;
8984
8985
1.74k
      if (syms[idx]->section->flags & SEC_THREAD_LOCAL)
8986
0
  type = STT_TLS;
8987
8988
      /* Processor-specific types.  */
8989
1.74k
      if (type_ptr != NULL
8990
1.74k
    && bed->elf_backend_get_symbol_type)
8991
0
  type = ((*bed->elf_backend_get_symbol_type)
8992
0
    (&type_ptr->internal_elf_sym, type));
8993
8994
1.74k
      if (flags & BSF_SECTION_SYM)
8995
144
  {
8996
144
    if (flags & BSF_GLOBAL)
8997
0
      sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
8998
144
    else
8999
144
      sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
9000
144
  }
9001
1.60k
      else if (bfd_is_com_section (syms[idx]->section))
9002
3
  {
9003
3
    if (type != STT_TLS)
9004
3
      {
9005
3
        if ((abfd->flags & BFD_CONVERT_ELF_COMMON))
9006
0
    type = ((abfd->flags & BFD_USE_ELF_STT_COMMON)
9007
0
      ? STT_COMMON : STT_OBJECT);
9008
3
        else
9009
3
    type = ((flags & BSF_ELF_COMMON) != 0
9010
3
      ? STT_COMMON : STT_OBJECT);
9011
3
      }
9012
3
    sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
9013
3
  }
9014
1.60k
      else if (bfd_is_und_section (syms[idx]->section))
9015
790
  sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK)
9016
1.60k
            ? STB_WEAK
9017
1.60k
            : STB_GLOBAL),
9018
1.60k
           type);
9019
812
      else if (flags & BSF_FILE)
9020
22
  sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
9021
790
      else
9022
790
  {
9023
790
    int bind = STB_LOCAL;
9024
9025
790
    if (flags & BSF_LOCAL)
9026
504
      bind = STB_LOCAL;
9027
286
    else if (flags & BSF_GNU_UNIQUE)
9028
0
      bind = STB_GNU_UNIQUE;
9029
286
    else if (flags & BSF_WEAK)
9030
13
      bind = STB_WEAK;
9031
273
    else if (flags & BSF_GLOBAL)
9032
247
      bind = STB_GLOBAL;
9033
9034
790
    sym.st_info = ELF_ST_INFO (bind, type);
9035
790
  }
9036
9037
1.74k
      if (type_ptr != NULL)
9038
1.74k
  {
9039
1.74k
    sym.st_other = type_ptr->internal_elf_sym.st_other;
9040
1.74k
    sym.st_target_internal
9041
1.74k
      = type_ptr->internal_elf_sym.st_target_internal;
9042
1.74k
  }
9043
0
      else
9044
0
  {
9045
0
    sym.st_other = 0;
9046
0
    sym.st_target_internal = 0;
9047
0
  }
9048
9049
1.74k
      symstrtab[outbound_syms_index].sym = sym;
9050
1.74k
      symstrtab[outbound_syms_index].dest_index = outbound_syms_index;
9051
1.74k
      outbound_syms_index++;
9052
1.74k
    }
9053
9054
  /* Finalize the .strtab section.  */
9055
56
  _bfd_elf_strtab_finalize (stt);
9056
9057
  /* Swap out the .strtab section.  */
9058
1.86k
  for (idx = 0; idx < outbound_syms_index; idx++)
9059
1.80k
    {
9060
1.80k
      struct elf_sym_strtab *elfsym = &symstrtab[idx];
9061
1.80k
      if (elfsym->sym.st_name != 0)
9062
507
  elfsym->sym.st_name = _bfd_elf_strtab_offset (stt,
9063
507
                  elfsym->sym.st_name);
9064
1.80k
      if (info && info->callbacks->ctf_new_symbol)
9065
0
  info->callbacks->ctf_new_symbol (elfsym->dest_index,
9066
0
           &elfsym->sym);
9067
9068
      /* Inform the linker of the addition of this symbol.  */
9069
9070
1.80k
      bed->s->swap_symbol_out (abfd, &elfsym->sym,
9071
1.80k
             (outbound_syms
9072
1.80k
        + (elfsym->dest_index
9073
1.80k
           * bed->s->sizeof_sym)),
9074
1.80k
             NPTR_ADD (outbound_shndx,
9075
1.80k
           (elfsym->dest_index
9076
1.80k
            * sizeof (Elf_External_Sym_Shndx))));
9077
1.80k
    }
9078
56
  free (symstrtab);
9079
9080
56
  *sttp = stt;
9081
56
  symstrtab_hdr->sh_size = _bfd_elf_strtab_size (stt);
9082
56
  symstrtab_hdr->sh_type = SHT_STRTAB;
9083
56
  symstrtab_hdr->sh_addralign = 1;
9084
9085
56
  return true;
9086
56
}
9087
9088
/* Return the number of bytes required to hold the symtab vector.
9089
9090
   Note that we base it on the count plus 1, since we will null terminate
9091
   the vector allocated based on this size.  However, the ELF symbol table
9092
   always has a dummy entry as symbol #0, so it ends up even.  */
9093
9094
long
9095
_bfd_elf_get_symtab_upper_bound (bfd *abfd)
9096
4.90k
{
9097
4.90k
  bfd_size_type symcount;
9098
4.90k
  long symtab_size;
9099
4.90k
  Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr;
9100
9101
4.90k
  symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
9102
4.90k
  if (symcount > LONG_MAX / sizeof (asymbol *))
9103
0
    {
9104
0
      bfd_set_error (bfd_error_file_too_big);
9105
0
      return -1;
9106
0
    }
9107
4.90k
  symtab_size = symcount * (sizeof (asymbol *));
9108
4.90k
  if (symcount == 0)
9109
383
    symtab_size = sizeof (asymbol *);
9110
4.52k
  else if (!bfd_write_p (abfd))
9111
4.52k
    {
9112
4.52k
      ufile_ptr filesize = bfd_get_file_size (abfd);
9113
9114
4.52k
      if (filesize != 0 && (unsigned long) symtab_size > filesize)
9115
154
  {
9116
154
    bfd_set_error (bfd_error_file_truncated);
9117
154
    return -1;
9118
154
  }
9119
4.52k
    }
9120
9121
4.75k
  return symtab_size;
9122
4.90k
}
9123
9124
long
9125
_bfd_elf_get_dynamic_symtab_upper_bound (bfd *abfd)
9126
8.72k
{
9127
8.72k
  bfd_size_type symcount;
9128
8.72k
  long symtab_size;
9129
8.72k
  Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr;
9130
9131
8.72k
  if (elf_dynsymtab (abfd) == 0)
9132
8.31k
    {
9133
      /* Check if there is dynamic symbol table.  */
9134
8.31k
      symcount = elf_tdata (abfd)->dt_symtab_count;
9135
8.31k
      if (symcount)
9136
6
  goto compute_symtab_size;
9137
9138
8.30k
      bfd_set_error (bfd_error_invalid_operation);
9139
8.30k
      return -1;
9140
8.31k
    }
9141
9142
410
  symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
9143
410
  if (symcount > LONG_MAX / sizeof (asymbol *))
9144
0
    {
9145
0
      bfd_set_error (bfd_error_file_too_big);
9146
0
      return -1;
9147
0
    }
9148
9149
416
 compute_symtab_size:
9150
416
  symtab_size = symcount * (sizeof (asymbol *));
9151
416
  if (symcount == 0)
9152
7
    symtab_size = sizeof (asymbol *);
9153
409
  else if (!bfd_write_p (abfd))
9154
409
    {
9155
409
      ufile_ptr filesize = bfd_get_file_size (abfd);
9156
9157
409
      if (filesize != 0 && (unsigned long) symtab_size > filesize)
9158
23
  {
9159
23
    bfd_set_error (bfd_error_file_truncated);
9160
23
    return -1;
9161
23
  }
9162
409
    }
9163
9164
393
  return symtab_size;
9165
416
}
9166
9167
long
9168
_bfd_elf_get_reloc_upper_bound (bfd *abfd, sec_ptr asect)
9169
16.6k
{
9170
16.6k
  if (asect->reloc_count != 0 && !bfd_write_p (abfd))
9171
15.1k
    {
9172
      /* Sanity check reloc section size.  */
9173
15.1k
      ufile_ptr filesize = bfd_get_file_size (abfd);
9174
9175
15.1k
      if (filesize != 0)
9176
15.1k
  {
9177
15.1k
    struct bfd_elf_section_data *d = elf_section_data (asect);
9178
15.1k
    bfd_size_type rel_size = d->rel.hdr ? d->rel.hdr->sh_size : 0;
9179
15.1k
    bfd_size_type rela_size = d->rela.hdr ? d->rela.hdr->sh_size : 0;
9180
9181
15.1k
    if (rel_size + rela_size > filesize
9182
14.7k
        || rel_size + rela_size < rel_size)
9183
414
      {
9184
414
        bfd_set_error (bfd_error_file_truncated);
9185
414
        return -1;
9186
414
      }
9187
15.1k
  }
9188
15.1k
    }
9189
9190
#if SIZEOF_LONG == SIZEOF_INT
9191
  if (asect->reloc_count >= LONG_MAX / sizeof (arelent *))
9192
    {
9193
      bfd_set_error (bfd_error_file_too_big);
9194
      return -1;
9195
    }
9196
#endif
9197
16.1k
  return (asect->reloc_count + 1L) * sizeof (arelent *);
9198
16.6k
}
9199
9200
/* Canonicalize the relocs.  */
9201
9202
long
9203
_bfd_elf_canonicalize_reloc (bfd *abfd,
9204
           sec_ptr section,
9205
           arelent **relptr,
9206
           asymbol **symbols)
9207
16.1k
{
9208
16.1k
  arelent *tblptr;
9209
16.1k
  unsigned int i;
9210
16.1k
  elf_backend_data *bed = get_elf_backend_data (abfd);
9211
9212
16.1k
  if (! bed->s->slurp_reloc_table (abfd, section, symbols, false))
9213
2.15k
    return -1;
9214
9215
14.0k
  tblptr = section->relocation;
9216
124k
  for (i = 0; i < section->reloc_count; i++)
9217
110k
    *relptr++ = tblptr++;
9218
9219
14.0k
  *relptr = NULL;
9220
9221
14.0k
  return section->reloc_count;
9222
16.1k
}
9223
9224
long
9225
_bfd_elf_canonicalize_symtab (bfd *abfd, asymbol **allocation)
9226
4.75k
{
9227
4.75k
  elf_backend_data *bed = get_elf_backend_data (abfd);
9228
4.75k
  long symcount = bed->s->slurp_symbol_table (abfd, allocation, false);
9229
9230
4.75k
  if (symcount >= 0)
9231
4.08k
    abfd->symcount = symcount;
9232
4.75k
  return symcount;
9233
4.75k
}
9234
9235
long
9236
_bfd_elf_canonicalize_dynamic_symtab (bfd *abfd,
9237
              asymbol **allocation)
9238
224
{
9239
224
  elf_backend_data *bed = get_elf_backend_data (abfd);
9240
224
  long symcount = bed->s->slurp_symbol_table (abfd, allocation, true);
9241
9242
224
  if (symcount >= 0)
9243
165
    abfd->dynsymcount = symcount;
9244
224
  return symcount;
9245
224
}
9246
9247
/* Return the size required for the dynamic reloc entries.  Any loadable
9248
   section that was actually installed in the BFD, and has type SHT_REL
9249
   or SHT_RELA, and uses the dynamic symbol table, is considered to be a
9250
   dynamic reloc section.  */
9251
9252
long
9253
_bfd_elf_get_dynamic_reloc_upper_bound (bfd *abfd)
9254
8.46k
{
9255
8.46k
  bfd_size_type count, ext_rel_size;
9256
8.46k
  asection *s;
9257
9258
8.46k
  if (elf_dynsymtab (abfd) == 0)
9259
8.19k
    {
9260
8.19k
      bfd_set_error (bfd_error_invalid_operation);
9261
8.19k
      return -1;
9262
8.19k
    }
9263
9264
276
  count = 1;
9265
276
  ext_rel_size = 0;
9266
7.35k
  for (s = abfd->sections; s != NULL; s = s->next)
9267
7.07k
    if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
9268
999
  && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
9269
977
      || elf_section_data (s)->this_hdr.sh_type == SHT_RELA)
9270
408
  && (elf_section_data (s)->this_hdr.sh_flags & SHF_COMPRESSED) == 0)
9271
406
      {
9272
406
  ext_rel_size += elf_section_data (s)->this_hdr.sh_size;
9273
406
  if (ext_rel_size < elf_section_data (s)->this_hdr.sh_size)
9274
0
    {
9275
0
      bfd_set_error (bfd_error_file_truncated);
9276
0
      return -1;
9277
0
    }
9278
406
  count += NUM_SHDR_ENTRIES (&elf_section_data (s)->this_hdr);
9279
406
  if (count > LONG_MAX / sizeof (arelent *))
9280
0
    {
9281
0
      bfd_set_error (bfd_error_file_too_big);
9282
0
      return -1;
9283
0
    }
9284
406
      }
9285
276
  if (count > 1 && !bfd_write_p (abfd))
9286
270
    {
9287
      /* Sanity check reloc section sizes.  */
9288
270
      ufile_ptr filesize = bfd_get_file_size (abfd);
9289
270
      if (filesize != 0 && ext_rel_size > filesize)
9290
5
  {
9291
5
    bfd_set_error (bfd_error_file_truncated);
9292
5
    return -1;
9293
5
  }
9294
270
    }
9295
271
  return count * sizeof (arelent *);
9296
276
}
9297
9298
/* Canonicalize the dynamic relocation entries.  Note that we return the
9299
   dynamic relocations as a single block, although they are actually
9300
   associated with particular sections; the interface, which was
9301
   designed for SunOS style shared libraries, expects that there is only
9302
   one set of dynamic relocs.  Any loadable section that was actually
9303
   installed in the BFD, and has type SHT_REL or SHT_RELA, and uses the
9304
   dynamic symbol table, is considered to be a dynamic reloc section.  */
9305
9306
long
9307
_bfd_elf_canonicalize_dynamic_reloc (bfd *abfd,
9308
             arelent **storage,
9309
             asymbol **syms)
9310
241
{
9311
241
  bool (*slurp_relocs) (bfd *, asection *, asymbol **, bool);
9312
241
  asection *s;
9313
241
  long ret;
9314
9315
241
  if (elf_dynsymtab (abfd) == 0)
9316
0
    {
9317
0
      bfd_set_error (bfd_error_invalid_operation);
9318
0
      return -1;
9319
0
    }
9320
9321
241
  slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
9322
241
  ret = 0;
9323
6.07k
  for (s = abfd->sections; s != NULL; s = s->next)
9324
5.84k
    {
9325
5.84k
      if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
9326
863
    && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
9327
843
        || elf_section_data (s)->this_hdr.sh_type == SHT_RELA)
9328
355
    && (elf_section_data (s)->this_hdr.sh_flags & SHF_COMPRESSED) == 0)
9329
353
  {
9330
353
    arelent *p;
9331
353
    long count, i;
9332
9333
353
    if (! (*slurp_relocs) (abfd, s, syms, true))
9334
19
      return -1;
9335
334
    count = NUM_SHDR_ENTRIES (&elf_section_data (s)->this_hdr);
9336
334
    p = s->relocation;
9337
3.05k
    for (i = 0; i < count; i++)
9338
2.72k
      *storage++ = p++;
9339
334
    ret += count;
9340
334
  }
9341
5.84k
    }
9342
9343
222
  *storage = NULL;
9344
9345
222
  return ret;
9346
241
}
9347

9348
/* Read in the version information.  */
9349
9350
bool
9351
_bfd_elf_slurp_version_tables (bfd *abfd, bool default_imported_symver)
9352
267
{
9353
267
  bfd_byte *contents = NULL;
9354
267
  unsigned int freeidx = 0;
9355
267
  size_t amt;
9356
267
  void *contents_addr = NULL;
9357
267
  size_t contents_size = 0;
9358
9359
267
  if (elf_dynverref (abfd) != 0 || elf_tdata (abfd)->dt_verneed != NULL)
9360
252
    {
9361
252
      Elf_Internal_Shdr *hdr;
9362
252
      Elf_External_Verneed *everneed;
9363
252
      Elf_Internal_Verneed *iverneed;
9364
252
      unsigned int i;
9365
252
      bfd_byte *contents_end;
9366
252
      size_t verneed_count;
9367
252
      size_t verneed_size;
9368
9369
252
      if (elf_tdata (abfd)->dt_verneed != NULL)
9370
1
  {
9371
1
    hdr = NULL;
9372
1
    contents = elf_tdata (abfd)->dt_verneed;
9373
1
    verneed_count = elf_tdata (abfd)->dt_verneed_count;
9374
1
    verneed_size = verneed_count * sizeof (Elf_External_Verneed);
9375
1
  }
9376
251
      else
9377
251
  {
9378
251
    hdr = &elf_tdata (abfd)->dynverref_hdr;
9379
9380
251
    if (hdr->sh_info > hdr->sh_size / sizeof (Elf_External_Verneed))
9381
4
      {
9382
67
      error_return_bad_verref:
9383
67
        _bfd_error_handler
9384
67
    (_("%pB: .gnu.version_r invalid entry"), abfd);
9385
67
        bfd_set_error (bfd_error_bad_value);
9386
85
      error_return_verref:
9387
85
        elf_tdata (abfd)->verref = NULL;
9388
85
        elf_tdata (abfd)->cverrefs = 0;
9389
85
        goto error_return;
9390
67
      }
9391
9392
247
    if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0)
9393
1
      goto error_return_verref;
9394
246
    contents_size = hdr->sh_size;
9395
246
    contents = _bfd_mmap_temporary (abfd, contents_size,
9396
246
            &contents_addr, &contents_size);
9397
246
    if (contents == NULL)
9398
17
      goto error_return_verref;
9399
9400
229
    verneed_size = hdr->sh_size;
9401
229
    verneed_count = hdr->sh_info;
9402
229
  }
9403
9404
230
      if (_bfd_mul_overflow (verneed_count,
9405
230
           sizeof (Elf_Internal_Verneed), &amt))
9406
0
  {
9407
0
    bfd_set_error (bfd_error_file_too_big);
9408
0
    goto error_return_verref;
9409
0
  }
9410
230
      if (amt == 0)
9411
0
  goto error_return_verref;
9412
230
      elf_tdata (abfd)->verref = (Elf_Internal_Verneed *) bfd_zalloc (abfd, amt);
9413
230
      if (elf_tdata (abfd)->verref == NULL)
9414
0
  goto error_return_verref;
9415
9416
230
      BFD_ASSERT (sizeof (Elf_External_Verneed)
9417
230
      == sizeof (Elf_External_Vernaux));
9418
230
      contents_end = (contents + verneed_size
9419
230
          - sizeof (Elf_External_Verneed));
9420
230
      everneed = (Elf_External_Verneed *) contents;
9421
230
      iverneed = elf_tdata (abfd)->verref;
9422
233
      for (i = 0; i < verneed_count; i++, iverneed++)
9423
230
  {
9424
230
    Elf_External_Vernaux *evernaux;
9425
230
    Elf_Internal_Vernaux *ivernaux;
9426
230
    unsigned int j;
9427
9428
230
    _bfd_elf_swap_verneed_in (abfd, everneed, iverneed);
9429
9430
230
    iverneed->vn_bfd = abfd;
9431
9432
230
    if (elf_use_dt_symtab_p (abfd))
9433
1
      {
9434
1
        if (iverneed->vn_file < elf_tdata (abfd)->dt_strsz)
9435
1
    iverneed->vn_filename
9436
1
      = elf_tdata (abfd)->dt_strtab + iverneed->vn_file;
9437
0
        else
9438
0
    iverneed->vn_filename = NULL;
9439
1
      }
9440
229
    else if (hdr == NULL)
9441
0
      goto error_return_bad_verref;
9442
229
    else
9443
229
      iverneed->vn_filename
9444
229
        = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
9445
229
             iverneed->vn_file);
9446
230
    if (iverneed->vn_filename == NULL)
9447
30
      goto error_return_bad_verref;
9448
9449
200
    if (iverneed->vn_cnt == 0)
9450
21
      iverneed->vn_auxptr = NULL;
9451
179
    else
9452
179
      {
9453
179
        if (_bfd_mul_overflow (iverneed->vn_cnt,
9454
179
             sizeof (Elf_Internal_Vernaux), &amt))
9455
0
    {
9456
0
      bfd_set_error (bfd_error_file_too_big);
9457
0
      goto error_return_verref;
9458
0
    }
9459
179
        iverneed->vn_auxptr = (struct elf_internal_vernaux *)
9460
179
    bfd_alloc (abfd, amt);
9461
179
        if (iverneed->vn_auxptr == NULL)
9462
0
    goto error_return_verref;
9463
179
      }
9464
9465
200
    if (iverneed->vn_aux
9466
200
        > (size_t) (contents_end - (bfd_byte *) everneed))
9467
8
      goto error_return_bad_verref;
9468
9469
192
    evernaux = ((Elf_External_Vernaux *)
9470
192
          ((bfd_byte *) everneed + iverneed->vn_aux));
9471
192
    ivernaux = iverneed->vn_auxptr;
9472
298
    for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++)
9473
283
      {
9474
283
        _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux);
9475
9476
283
        if (elf_use_dt_symtab_p (abfd))
9477
0
    {
9478
0
      if (ivernaux->vna_name < elf_tdata (abfd)->dt_strsz)
9479
0
        ivernaux->vna_nodename
9480
0
          = elf_tdata (abfd)->dt_strtab + ivernaux->vna_name;
9481
0
      else
9482
0
        ivernaux->vna_nodename = NULL;
9483
0
    }
9484
283
        else if (hdr == NULL)
9485
0
    goto error_return_bad_verref;
9486
283
        else
9487
283
    ivernaux->vna_nodename
9488
283
      = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
9489
283
                 ivernaux->vna_name);
9490
283
        if (ivernaux->vna_nodename == NULL)
9491
5
    goto error_return_bad_verref;
9492
9493
278
        if (ivernaux->vna_other > freeidx)
9494
168
    freeidx = ivernaux->vna_other;
9495
9496
278
        ivernaux->vna_nextptr = NULL;
9497
278
        if (ivernaux->vna_next == 0)
9498
160
    {
9499
160
      iverneed->vn_cnt = j + 1;
9500
160
      break;
9501
160
    }
9502
118
        if (j + 1 < iverneed->vn_cnt)
9503
113
    ivernaux->vna_nextptr = ivernaux + 1;
9504
9505
118
        if (ivernaux->vna_next
9506
118
      > (size_t) (contents_end - (bfd_byte *) evernaux))
9507
12
    goto error_return_bad_verref;
9508
9509
106
        evernaux = ((Elf_External_Vernaux *)
9510
106
        ((bfd_byte *) evernaux + ivernaux->vna_next));
9511
106
      }
9512
9513
175
    iverneed->vn_nextref = NULL;
9514
175
    if (iverneed->vn_next == 0)
9515
164
      break;
9516
11
    if (hdr != NULL && (i + 1 < hdr->sh_info))
9517
0
      iverneed->vn_nextref = iverneed + 1;
9518
9519
11
    if (iverneed->vn_next
9520
11
        > (size_t) (contents_end - (bfd_byte *) everneed))
9521
8
      goto error_return_bad_verref;
9522
9523
3
    everneed = ((Elf_External_Verneed *)
9524
3
          ((bfd_byte *) everneed + iverneed->vn_next));
9525
3
  }
9526
167
      elf_tdata (abfd)->cverrefs = i;
9527
9528
167
      if (contents != elf_tdata (abfd)->dt_verneed)
9529
166
  _bfd_munmap_temporary (contents_addr, contents_size);
9530
167
      contents = NULL;
9531
167
      contents_addr = NULL;
9532
167
    }
9533
9534
182
  if (elf_dynverdef (abfd) != 0 || elf_tdata (abfd)->dt_verdef != NULL)
9535
15
    {
9536
15
      Elf_Internal_Shdr *hdr;
9537
15
      Elf_External_Verdef *everdef;
9538
15
      Elf_Internal_Verdef *iverdef;
9539
15
      Elf_Internal_Verdef *iverdefarr;
9540
15
      Elf_Internal_Verdef iverdefmem;
9541
15
      unsigned int i;
9542
15
      unsigned int maxidx;
9543
15
      bfd_byte *contents_end_def, *contents_end_aux;
9544
15
      size_t verdef_count;
9545
15
      size_t verdef_size;
9546
9547
15
      if (elf_tdata (abfd)->dt_verdef != NULL)
9548
0
  {
9549
0
    hdr = NULL;
9550
0
    contents = elf_tdata (abfd)->dt_verdef;
9551
0
    verdef_count = elf_tdata (abfd)->dt_verdef_count;
9552
0
    verdef_size = verdef_count * sizeof (Elf_External_Verdef);
9553
0
  }
9554
15
      else
9555
15
  {
9556
15
    hdr = &elf_tdata (abfd)->dynverdef_hdr;
9557
9558
15
    if (hdr->sh_size < sizeof (Elf_External_Verdef))
9559
1
      {
9560
8
      error_return_bad_verdef:
9561
8
        _bfd_error_handler
9562
8
    (_("%pB: .gnu.version_d invalid entry"), abfd);
9563
8
        bfd_set_error (bfd_error_bad_value);
9564
11
      error_return_verdef:
9565
11
        elf_tdata (abfd)->verdef = NULL;
9566
11
        elf_tdata (abfd)->cverdefs = 0;
9567
11
        goto error_return;
9568
8
      }
9569
9570
14
    if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0)
9571
1
      goto error_return_verdef;
9572
13
    contents_size = hdr->sh_size;
9573
13
    contents = _bfd_mmap_temporary (abfd, contents_size,
9574
13
            &contents_addr, &contents_size);
9575
13
    if (contents == NULL)
9576
2
      goto error_return_verdef;
9577
9578
11
    BFD_ASSERT (sizeof (Elf_External_Verdef)
9579
11
          >= sizeof (Elf_External_Verdaux));
9580
9581
11
    verdef_count = hdr->sh_info;
9582
11
    verdef_size = hdr->sh_size;
9583
11
  }
9584
9585
11
      contents_end_def = (contents + verdef_size
9586
11
        - sizeof (Elf_External_Verdef));
9587
11
      contents_end_aux = (contents + verdef_size
9588
11
        - sizeof (Elf_External_Verdaux));
9589
9590
      /* We know the number of entries in the section but not the maximum
9591
   index.  Therefore we have to run through all entries and find
9592
   the maximum.  */
9593
11
      everdef = (Elf_External_Verdef *) contents;
9594
11
      maxidx = 0;
9595
17
      for (i = 0; i < verdef_count; ++i)
9596
13
  {
9597
13
    _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
9598
9599
13
    if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) == 0)
9600
1
      goto error_return_bad_verdef;
9601
12
    if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) > maxidx)
9602
11
      maxidx = iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION);
9603
9604
12
    if (iverdefmem.vd_next == 0)
9605
4
      break;
9606
9607
8
    if (iverdefmem.vd_next
9608
8
        > (size_t) (contents_end_def - (bfd_byte *) everdef))
9609
2
      goto error_return_bad_verdef;
9610
9611
6
    everdef = ((Elf_External_Verdef *)
9612
6
         ((bfd_byte *) everdef + iverdefmem.vd_next));
9613
6
  }
9614
9615
8
      if (default_imported_symver)
9616
0
  {
9617
0
    if (freeidx > maxidx)
9618
0
      maxidx = ++freeidx;
9619
0
    else
9620
0
      freeidx = ++maxidx;
9621
0
  }
9622
8
      if (_bfd_mul_overflow (maxidx, sizeof (Elf_Internal_Verdef), &amt))
9623
0
  {
9624
0
    bfd_set_error (bfd_error_file_too_big);
9625
0
    goto error_return_verdef;
9626
0
  }
9627
9628
8
      if (amt == 0)
9629
0
  goto error_return_verdef;
9630
8
      elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *) bfd_zalloc (abfd, amt);
9631
8
      if (elf_tdata (abfd)->verdef == NULL)
9632
0
  goto error_return_verdef;
9633
9634
8
      elf_tdata (abfd)->cverdefs = maxidx;
9635
9636
8
      everdef = (Elf_External_Verdef *) contents;
9637
8
      iverdefarr = elf_tdata (abfd)->verdef;
9638
11
      for (i = 0; i < verdef_count; ++i)
9639
9
  {
9640
9
    Elf_External_Verdaux *everdaux;
9641
9
    Elf_Internal_Verdaux *iverdaux;
9642
9
    unsigned int j;
9643
9644
9
    _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
9645
9646
9
    if ((iverdefmem.vd_ndx & VERSYM_VERSION) == 0)
9647
0
      goto error_return_bad_verdef;
9648
9649
9
    iverdef = &iverdefarr[(iverdefmem.vd_ndx & VERSYM_VERSION) - 1];
9650
9
    memcpy (iverdef, &iverdefmem, offsetof (Elf_Internal_Verdef, vd_bfd));
9651
9652
9
    iverdef->vd_bfd = abfd;
9653
9654
9
    if (iverdef->vd_cnt == 0)
9655
6
      iverdef->vd_auxptr = NULL;
9656
3
    else
9657
3
      {
9658
3
        if (_bfd_mul_overflow (iverdef->vd_cnt,
9659
3
             sizeof (Elf_Internal_Verdaux), &amt))
9660
0
    {
9661
0
      bfd_set_error (bfd_error_file_too_big);
9662
0
      goto error_return_verdef;
9663
0
    }
9664
3
        iverdef->vd_auxptr = (struct elf_internal_verdaux *)
9665
3
    bfd_alloc (abfd, amt);
9666
3
        if (iverdef->vd_auxptr == NULL)
9667
0
    goto error_return_verdef;
9668
3
      }
9669
9670
9
    if (iverdef->vd_aux
9671
9
        > (size_t) (contents_end_aux - (bfd_byte *) everdef))
9672
1
      goto error_return_bad_verdef;
9673
9674
8
    everdaux = ((Elf_External_Verdaux *)
9675
8
          ((bfd_byte *) everdef + iverdef->vd_aux));
9676
8
    iverdaux = iverdef->vd_auxptr;
9677
8
    for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++)
9678
3
      {
9679
3
        _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux);
9680
9681
3
        if (elf_use_dt_symtab_p (abfd))
9682
0
    {
9683
0
      if (iverdaux->vda_name < elf_tdata (abfd)->dt_strsz)
9684
0
        iverdaux->vda_nodename
9685
0
          = elf_tdata (abfd)->dt_strtab + iverdaux->vda_name;
9686
0
      else
9687
0
        iverdaux->vda_nodename = NULL;
9688
0
    }
9689
3
        else
9690
3
    iverdaux->vda_nodename
9691
3
      = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
9692
3
                 iverdaux->vda_name);
9693
3
        if (iverdaux->vda_nodename == NULL)
9694
2
    goto error_return_bad_verdef;
9695
9696
1
        iverdaux->vda_nextptr = NULL;
9697
1
        if (iverdaux->vda_next == 0)
9698
0
    {
9699
0
      iverdef->vd_cnt = j + 1;
9700
0
      break;
9701
0
    }
9702
1
        if (j + 1 < iverdef->vd_cnt)
9703
1
    iverdaux->vda_nextptr = iverdaux + 1;
9704
9705
1
        if (iverdaux->vda_next
9706
1
      > (size_t) (contents_end_aux - (bfd_byte *) everdaux))
9707
1
    goto error_return_bad_verdef;
9708
9709
0
        everdaux = ((Elf_External_Verdaux *)
9710
0
        ((bfd_byte *) everdaux + iverdaux->vda_next));
9711
0
      }
9712
9713
5
    iverdef->vd_nodename = NULL;
9714
5
    if (iverdef->vd_cnt)
9715
0
      iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename;
9716
9717
5
    iverdef->vd_nextdef = NULL;
9718
5
    if (iverdef->vd_next == 0)
9719
2
      break;
9720
3
    if ((size_t) (iverdef - iverdefarr) + 1 < maxidx)
9721
1
      iverdef->vd_nextdef = iverdef + 1;
9722
9723
3
    everdef = ((Elf_External_Verdef *)
9724
3
         ((bfd_byte *) everdef + iverdef->vd_next));
9725
3
  }
9726
9727
4
      if (contents != elf_tdata (abfd)->dt_verdef)
9728
4
  _bfd_munmap_temporary (contents_addr, contents_size);
9729
4
      contents = NULL;
9730
4
      contents_addr = NULL;
9731
4
    }
9732
167
  else if (default_imported_symver)
9733
0
    {
9734
0
      if (freeidx < 3)
9735
0
  freeidx = 3;
9736
0
      else
9737
0
  freeidx++;
9738
9739
0
      if (_bfd_mul_overflow (freeidx, sizeof (Elf_Internal_Verdef), &amt))
9740
0
  {
9741
0
    bfd_set_error (bfd_error_file_too_big);
9742
0
    goto error_return;
9743
0
  }
9744
0
      if (amt == 0)
9745
0
  goto error_return;
9746
0
      elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *) bfd_zalloc (abfd, amt);
9747
0
      if (elf_tdata (abfd)->verdef == NULL)
9748
0
  goto error_return;
9749
9750
0
      elf_tdata (abfd)->cverdefs = freeidx;
9751
0
    }
9752
9753
  /* Create a default version based on the soname.  */
9754
171
  if (default_imported_symver)
9755
0
    {
9756
0
      Elf_Internal_Verdef *iverdef;
9757
0
      Elf_Internal_Verdaux *iverdaux;
9758
9759
0
      iverdef = &elf_tdata (abfd)->verdef[freeidx - 1];
9760
9761
0
      iverdef->vd_version = VER_DEF_CURRENT;
9762
0
      iverdef->vd_flags = 0;
9763
0
      iverdef->vd_ndx = freeidx;
9764
0
      iverdef->vd_cnt = 1;
9765
9766
0
      iverdef->vd_bfd = abfd;
9767
9768
0
      iverdef->vd_nodename = bfd_elf_get_dt_soname (abfd);
9769
0
      if (iverdef->vd_nodename == NULL)
9770
0
  goto error_return_verdef;
9771
0
      iverdef->vd_nextdef = NULL;
9772
0
      iverdef->vd_auxptr = ((struct elf_internal_verdaux *)
9773
0
          bfd_zalloc (abfd, sizeof (Elf_Internal_Verdaux)));
9774
0
      if (iverdef->vd_auxptr == NULL)
9775
0
  goto error_return_verdef;
9776
9777
0
      iverdaux = iverdef->vd_auxptr;
9778
0
      iverdaux->vda_nodename = iverdef->vd_nodename;
9779
0
    }
9780
9781
171
  return true;
9782
9783
96
 error_return:
9784
96
  if (contents != elf_tdata (abfd)->dt_verneed
9785
70
      && contents != elf_tdata (abfd)->dt_verdef)
9786
70
    _bfd_munmap_temporary (contents_addr, contents_size);
9787
96
  return false;
9788
171
}
9789

9790
asymbol *
9791
_bfd_elf_make_empty_symbol (bfd *abfd)
9792
1.17M
{
9793
1.17M
  elf_symbol_type *newsym;
9794
9795
1.17M
  newsym = (elf_symbol_type *) bfd_zalloc (abfd, sizeof (*newsym));
9796
1.17M
  if (!newsym)
9797
0
    return NULL;
9798
1.17M
  newsym->symbol.the_bfd = abfd;
9799
1.17M
  return &newsym->symbol;
9800
1.17M
}
9801
9802
void
9803
_bfd_elf_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED,
9804
        asymbol *symbol,
9805
        symbol_info *ret)
9806
24.0k
{
9807
24.0k
  bfd_symbol_info (symbol, ret);
9808
24.0k
}
9809
9810
/* Return whether a symbol name implies a local symbol.  Most targets
9811
   use this function for the is_local_label_name entry point, but some
9812
   override it.  */
9813
9814
bool
9815
_bfd_elf_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED,
9816
            const char *name)
9817
23.0k
{
9818
  /* Normal local symbols start with ``.L''.  */
9819
23.0k
  if (name[0] == '.' && name[1] == 'L')
9820
4.67k
    return true;
9821
9822
  /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
9823
     DWARF debugging symbols starting with ``..''.  */
9824
18.3k
  if (name[0] == '.' && name[1] == '.')
9825
773
    return true;
9826
9827
  /* gcc will sometimes generate symbols beginning with ``_.L_'' when
9828
     emitting DWARF debugging output.  I suspect this is actually a
9829
     small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
9830
     ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
9831
     underscore to be emitted on some ELF targets).  For ease of use,
9832
     we treat such symbols as local.  */
9833
17.6k
  if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_')
9834
0
    return true;
9835
9836
  /* Treat assembler generated fake symbols, dollar local labels and
9837
     forward-backward labels (aka local labels) as locals.
9838
     These labels have the form:
9839
9840
       L0^A.*              (fake symbols)
9841
9842
       [.]?L[0123456789]+{^A|^B}[0123456789]*  (local labels)
9843
9844
     Versions which start with .L will have already been matched above,
9845
     so we only need to match the rest.  */
9846
17.6k
  if (name[0] == 'L' && ISDIGIT (name[1]))
9847
2
    {
9848
2
      bool ret = false;
9849
2
      const char * p;
9850
2
      char c;
9851
9852
5
      for (p = name + 2; (c = *p); p++)
9853
3
  {
9854
3
    if (c == 1 || c == 2)
9855
0
      {
9856
0
        if (c == 1 && p == name + 2)
9857
    /* A fake symbol.  */
9858
0
    return true;
9859
9860
        /* FIXME: We are being paranoid here and treating symbols like
9861
     L0^Bfoo as if there were non-local, on the grounds that the
9862
     assembler will never generate them.  But can any symbol
9863
     containing an ASCII value in the range 1-31 ever be anything
9864
     other than some kind of local ?  */
9865
0
        ret = true;
9866
0
      }
9867
9868
3
    if (! ISDIGIT (c))
9869
0
      {
9870
0
        ret = false;
9871
0
        break;
9872
0
      }
9873
3
  }
9874
2
      return ret;
9875
2
    }
9876
9877
17.5k
  return false;
9878
17.6k
}
9879
9880
alent *
9881
_bfd_elf_get_lineno (bfd *abfd ATTRIBUTE_UNUSED,
9882
         asymbol *symbol ATTRIBUTE_UNUSED)
9883
0
{
9884
0
  abort ();
9885
0
  return NULL;
9886
0
}
9887
9888
bool
9889
_bfd_elf_set_arch_mach (bfd *abfd,
9890
      enum bfd_architecture arch,
9891
      unsigned long machine)
9892
753
{
9893
  /* If this isn't the right architecture for this backend, and this
9894
     isn't the generic backend, fail.  */
9895
753
  if (arch != get_elf_backend_data (abfd)->arch
9896
0
      && arch != bfd_arch_unknown
9897
0
      && get_elf_backend_data (abfd)->arch != bfd_arch_unknown)
9898
0
    return false;
9899
9900
753
  return bfd_default_set_arch_mach (abfd, arch, machine);
9901
753
}
9902
9903
/* Find the nearest line to a particular section and offset,
9904
   for error reporting.  */
9905
9906
bool
9907
_bfd_elf_find_nearest_line (bfd *abfd,
9908
          asymbol **symbols,
9909
          asection *section,
9910
          bfd_vma offset,
9911
          const char **filename_ptr,
9912
          const char **functionname_ptr,
9913
          unsigned int *line_ptr,
9914
          unsigned int *discriminator_ptr)
9915
24.0k
{
9916
24.0k
  return _bfd_elf_find_nearest_line_with_alt (abfd, NULL, symbols, section,
9917
24.0k
                offset, filename_ptr,
9918
24.0k
                functionname_ptr, line_ptr,
9919
24.0k
                discriminator_ptr);
9920
24.0k
}
9921
9922
/* Find the nearest line to a particular section and offset,
9923
   for error reporting.  ALT_BFD representing a .gnu_debugaltlink file
9924
   can be optionally specified.  */
9925
9926
bool
9927
_bfd_elf_find_nearest_line_with_alt (bfd *abfd,
9928
             const char *alt_filename,
9929
             asymbol **symbols,
9930
             asection *section,
9931
             bfd_vma offset,
9932
             const char **filename_ptr,
9933
             const char **functionname_ptr,
9934
             unsigned int *line_ptr,
9935
             unsigned int *discriminator_ptr)
9936
24.0k
{
9937
24.0k
  bool found;
9938
9939
24.0k
  if (_bfd_dwarf2_find_nearest_line_with_alt (abfd, alt_filename, symbols, NULL,
9940
24.0k
                section, offset, filename_ptr,
9941
24.0k
                functionname_ptr, line_ptr,
9942
24.0k
                discriminator_ptr,
9943
24.0k
                dwarf_debug_sections,
9944
24.0k
                &elf_tdata (abfd)->dwarf2_find_line_info))
9945
8.57k
    return true;
9946
9947
15.4k
  if (_bfd_dwarf1_find_nearest_line (abfd, symbols, section, offset,
9948
15.4k
             filename_ptr, functionname_ptr, line_ptr))
9949
0
    {
9950
0
      if (!*functionname_ptr)
9951
0
  _bfd_elf_find_function (abfd, symbols, section, offset,
9952
0
        *filename_ptr ? NULL : filename_ptr,
9953
0
        functionname_ptr);
9954
0
      return true;
9955
0
    }
9956
9957
15.4k
  if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
9958
15.4k
               &found, filename_ptr,
9959
15.4k
               functionname_ptr, line_ptr,
9960
15.4k
               &elf_tdata (abfd)->line_info))
9961
0
    return false;
9962
15.4k
  if (found && (*functionname_ptr || *line_ptr))
9963
0
    return true;
9964
9965
15.4k
  if (symbols == NULL)
9966
5.35k
    return false;
9967
9968
10.1k
  if (! _bfd_elf_find_function (abfd, symbols, section, offset,
9969
10.1k
        filename_ptr, functionname_ptr))
9970
7.81k
    return false;
9971
9972
2.32k
  *line_ptr = 0;
9973
2.32k
  return true;
9974
10.1k
}
9975
9976
/* Find the line for a symbol.  */
9977
9978
bool
9979
_bfd_elf_find_line (bfd *abfd, asymbol **symbols, asymbol *symbol,
9980
        const char **filename_ptr, unsigned int *line_ptr)
9981
4.45k
{
9982
4.45k
  struct elf_obj_tdata *tdata = elf_tdata (abfd);
9983
4.45k
  return _bfd_dwarf2_find_nearest_line (abfd, symbols, symbol, NULL, 0,
9984
4.45k
          filename_ptr, NULL, line_ptr, NULL,
9985
4.45k
          dwarf_debug_sections,
9986
4.45k
          &tdata->dwarf2_find_line_info);
9987
4.45k
}
9988
9989
/* After a call to bfd_find_nearest_line, successive calls to
9990
   bfd_find_inliner_info can be used to get source information about
9991
   each level of function inlining that terminated at the address
9992
   passed to bfd_find_nearest_line.  Currently this is only supported
9993
   for DWARF2 with appropriate DWARF3 extensions. */
9994
9995
bool
9996
_bfd_elf_find_inliner_info (bfd *abfd,
9997
          const char **filename_ptr,
9998
          const char **functionname_ptr,
9999
          unsigned int *line_ptr)
10000
0
{
10001
0
  struct elf_obj_tdata *tdata = elf_tdata (abfd);
10002
0
  return _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
10003
0
          functionname_ptr, line_ptr,
10004
0
          &tdata->dwarf2_find_line_info);
10005
0
}
10006
10007
int
10008
_bfd_elf_sizeof_headers (bfd *abfd, struct bfd_link_info *info)
10009
0
{
10010
0
  elf_backend_data *bed = get_elf_backend_data (abfd);
10011
0
  int ret = bed->s->sizeof_ehdr;
10012
10013
0
  if (!bfd_link_relocatable (info))
10014
0
    {
10015
0
      bfd_size_type phdr_size = elf_program_header_size (abfd);
10016
10017
0
      if (phdr_size == (bfd_size_type) -1)
10018
0
  {
10019
0
    struct elf_segment_map *m;
10020
10021
0
    phdr_size = 0;
10022
0
    for (m = elf_seg_map (abfd); m != NULL; m = m->next)
10023
0
      phdr_size += bed->s->sizeof_phdr;
10024
10025
0
    if (phdr_size == 0)
10026
0
      phdr_size = get_program_header_size (abfd, info);
10027
0
  }
10028
10029
0
      elf_program_header_size (abfd) = phdr_size;
10030
0
      ret += phdr_size;
10031
0
    }
10032
10033
0
  return ret;
10034
0
}
10035
10036
bool
10037
_bfd_elf_set_section_contents (bfd *abfd,
10038
             sec_ptr section,
10039
             const void *location,
10040
             file_ptr offset,
10041
             bfd_size_type count)
10042
1.09k
{
10043
1.09k
  Elf_Internal_Shdr *hdr;
10044
10045
1.09k
  if (! abfd->output_has_begun
10046
72
      && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
10047
5
    return false;
10048
10049
1.08k
  if (!count)
10050
0
    return true;
10051
10052
1.08k
  hdr = &elf_section_data (section)->this_hdr;
10053
1.08k
  if (hdr->sh_offset == (file_ptr) -1)
10054
0
    {
10055
0
      unsigned char *contents;
10056
10057
0
      if (bfd_section_is_ctf (section))
10058
  /* Nothing to do with this section: the contents are generated
10059
     later.  */
10060
0
  return true;
10061
10062
0
      if ((offset + count) > hdr->sh_size)
10063
0
  {
10064
0
    _bfd_error_handler
10065
0
      (_("%pB:%pA: error: attempting to write"
10066
0
         " over the end of the section"),
10067
0
       abfd, section);
10068
10069
0
    bfd_set_error (bfd_error_invalid_operation);
10070
0
    return false;
10071
0
  }
10072
10073
0
      contents = hdr->contents;
10074
0
      if (contents == NULL)
10075
0
  {
10076
0
    _bfd_error_handler
10077
0
      (_("%pB:%pA: error: attempting to write"
10078
0
         " section into an empty buffer"),
10079
0
       abfd, section);
10080
10081
0
    bfd_set_error (bfd_error_invalid_operation);
10082
0
    return false;
10083
0
  }
10084
10085
0
      memcpy (contents + offset, location, count);
10086
0
      return true;
10087
0
    }
10088
10089
1.08k
  return _bfd_generic_set_section_contents (abfd, section,
10090
1.08k
              location, offset, count);
10091
1.08k
}
10092
10093
bool
10094
_bfd_elf_no_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED,
10095
         arelent *cache_ptr ATTRIBUTE_UNUSED,
10096
         Elf_Internal_Rela *dst ATTRIBUTE_UNUSED)
10097
0
{
10098
0
  abort ();
10099
0
  return false;
10100
0
}
10101
10102
/* Try to convert a non-ELF reloc into an ELF one.  */
10103
10104
bool
10105
_bfd_elf_validate_reloc (bfd *abfd, arelent *areloc)
10106
0
{
10107
  /* Check whether we really have an ELF howto.  */
10108
10109
0
  if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec)
10110
0
    {
10111
0
      bfd_reloc_code_real_type code;
10112
0
      reloc_howto_type *howto;
10113
10114
      /* Alien reloc: Try to determine its type to replace it with an
10115
   equivalent ELF reloc.  */
10116
10117
0
      if (areloc->howto->pc_relative)
10118
0
  {
10119
0
    switch (areloc->howto->bitsize)
10120
0
      {
10121
0
      case 8:
10122
0
        code = BFD_RELOC_8_PCREL;
10123
0
        break;
10124
0
      case 12:
10125
0
        code = BFD_RELOC_12_PCREL;
10126
0
        break;
10127
0
      case 16:
10128
0
        code = BFD_RELOC_16_PCREL;
10129
0
        break;
10130
0
      case 24:
10131
0
        code = BFD_RELOC_24_PCREL;
10132
0
        break;
10133
0
      case 32:
10134
0
        code = BFD_RELOC_32_PCREL;
10135
0
        break;
10136
0
      case 64:
10137
0
        code = BFD_RELOC_64_PCREL;
10138
0
        break;
10139
0
      default:
10140
0
        goto fail;
10141
0
      }
10142
10143
0
    howto = bfd_reloc_type_lookup (abfd, code);
10144
10145
0
    if (howto && areloc->howto->pcrel_offset != howto->pcrel_offset)
10146
0
      {
10147
0
        if (howto->pcrel_offset)
10148
0
    areloc->addend += areloc->address;
10149
0
        else
10150
0
    areloc->addend -= areloc->address; /* addend is unsigned!! */
10151
0
      }
10152
0
  }
10153
0
      else
10154
0
  {
10155
0
    switch (areloc->howto->bitsize)
10156
0
      {
10157
0
      case 8:
10158
0
        code = BFD_RELOC_8;
10159
0
        break;
10160
0
      case 14:
10161
0
        code = BFD_RELOC_14;
10162
0
        break;
10163
0
      case 16:
10164
0
        code = BFD_RELOC_16;
10165
0
        break;
10166
0
      case 26:
10167
0
        code = BFD_RELOC_26;
10168
0
        break;
10169
0
      case 32:
10170
0
        code = BFD_RELOC_32;
10171
0
        break;
10172
0
      case 64:
10173
0
        code = BFD_RELOC_64;
10174
0
        break;
10175
0
      default:
10176
0
        goto fail;
10177
0
      }
10178
10179
0
    howto = bfd_reloc_type_lookup (abfd, code);
10180
0
  }
10181
10182
0
      if (howto)
10183
0
  areloc->howto = howto;
10184
0
      else
10185
0
  goto fail;
10186
0
    }
10187
10188
0
  return true;
10189
10190
0
 fail:
10191
  /* xgettext:c-format */
10192
0
  _bfd_error_handler (_("%pB: %s unsupported"),
10193
0
          abfd, areloc->howto->name);
10194
0
  bfd_set_error (bfd_error_sorry);
10195
0
  return false;
10196
0
}
10197
10198
bool
10199
_bfd_elf_free_cached_info (bfd *abfd)
10200
6.32M
{
10201
6.32M
  struct elf_obj_tdata *tdata;
10202
10203
6.32M
  if ((bfd_get_format (abfd) == bfd_object
10204
6.30M
       || bfd_get_format (abfd) == bfd_core)
10205
26.0k
      && (tdata = elf_tdata (abfd)) != NULL)
10206
26.0k
    {
10207
26.0k
      if (tdata->o != NULL && elf_shstrtab (abfd) != NULL)
10208
159
  _bfd_elf_strtab_free (elf_shstrtab (abfd));
10209
26.0k
      _bfd_dwarf2_cleanup_debug_info (abfd, &tdata->dwarf2_find_line_info);
10210
26.0k
      _bfd_dwarf1_cleanup_debug_info (abfd, &tdata->dwarf1_find_line_info);
10211
26.0k
      _bfd_stab_cleanup (abfd, &tdata->line_info);
10212
26.0k
      _bfd_elf_cleanup_object_attributes (abfd);
10213
358k
      for (asection *sec = abfd->sections; sec != NULL; sec = sec->next)
10214
332k
  {
10215
332k
    _bfd_elf_munmap_section_contents (sec, sec->contents);
10216
332k
    if (!sec->alloced)
10217
331k
      {
10218
331k
        free (elf_section_data (sec)->this_hdr.contents);
10219
331k
        elf_section_data (sec)->this_hdr.contents = NULL;
10220
331k
      }
10221
332k
    free (elf_section_data (sec)->relocs);
10222
332k
    elf_section_data (sec)->relocs = NULL;
10223
332k
    if (sec->sec_info_type == SEC_INFO_TYPE_EH_FRAME)
10224
0
      {
10225
0
        struct eh_frame_sec_info *sec_info = sec->sec_info;
10226
0
        free (sec_info->cies);
10227
0
      }
10228
332k
    if (sec->sec_info_type == SEC_INFO_TYPE_SFRAME)
10229
0
      {
10230
0
        struct sframe_dec_info *sfd_info = sec->sec_info;
10231
0
        sframe_decoder_free (&sfd_info->sfd_ctx);
10232
0
      }
10233
332k
  }
10234
26.0k
      free (tdata->symtab_hdr.contents);
10235
26.0k
      tdata->symtab_hdr.contents = NULL;
10236
26.0k
    }
10237
10238
6.32M
  return _bfd_generic_bfd_free_cached_info (abfd);
10239
6.32M
}
10240
10241
/* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
10242
   in the relocation's offset.  Thus we cannot allow any sort of sanity
10243
   range-checking to interfere.  There is nothing else to do in processing
10244
   this reloc.  */
10245
10246
bfd_reloc_status_type
10247
_bfd_elf_rel_vtable_reloc_fn
10248
  (bfd *abfd ATTRIBUTE_UNUSED, arelent *re ATTRIBUTE_UNUSED,
10249
   struct bfd_symbol *symbol ATTRIBUTE_UNUSED,
10250
   void *data ATTRIBUTE_UNUSED, asection *is ATTRIBUTE_UNUSED,
10251
   bfd *obfd ATTRIBUTE_UNUSED, char **errmsg ATTRIBUTE_UNUSED)
10252
44
{
10253
44
  return bfd_reloc_ok;
10254
44
}
10255

10256
/* Elf core file support.  Much of this only works on native
10257
   toolchains, since we rely on knowing the
10258
   machine-dependent procfs structure in order to pick
10259
   out details about the corefile.  */
10260
10261
#ifdef HAVE_SYS_PROCFS_H
10262
# include <sys/procfs.h>
10263
#endif
10264
10265
/* Return a PID that identifies a "thread" for threaded cores, or the
10266
   PID of the main process for non-threaded cores.  */
10267
10268
static int
10269
elfcore_make_pid (bfd *abfd)
10270
48
{
10271
48
  int pid;
10272
10273
48
  pid = elf_tdata (abfd)->core->lwpid;
10274
48
  if (pid == 0)
10275
39
    pid = elf_tdata (abfd)->core->pid;
10276
10277
48
  return pid;
10278
48
}
10279
10280
/* If there isn't a section called NAME, make one, using data from
10281
   SECT.  Note, this function will generate a reference to NAME, so
10282
   you shouldn't deallocate or overwrite it.  */
10283
10284
static bool
10285
elfcore_maybe_make_sect (bfd *abfd, char *name, asection *sect)
10286
48
{
10287
48
  asection *sect2;
10288
10289
48
  if (bfd_get_section_by_name (abfd, name) != NULL)
10290
24
    return true;
10291
10292
24
  sect2 = bfd_make_section_with_flags (abfd, name, sect->flags);
10293
24
  if (sect2 == NULL)
10294
0
    return false;
10295
10296
24
  sect2->size = sect->size;
10297
24
  sect2->filepos = sect->filepos;
10298
24
  sect2->alignment_power = sect->alignment_power;
10299
24
  return true;
10300
24
}
10301
10302
/* Create a pseudosection containing SIZE bytes at FILEPOS.  This
10303
   actually creates up to two pseudosections:
10304
   - For the single-threaded case, a section named NAME, unless
10305
     such a section already exists.
10306
   - For the multi-threaded case, a section named "NAME/PID", where
10307
     PID is elfcore_make_pid (abfd).
10308
   Both pseudosections have identical contents.  */
10309
bool
10310
_bfd_elfcore_make_pseudosection (bfd *abfd,
10311
         char *name,
10312
         size_t size,
10313
         ufile_ptr filepos)
10314
48
{
10315
48
  char buf[100];
10316
48
  char *threaded_name;
10317
48
  size_t len;
10318
48
  asection *sect;
10319
10320
  /* Build the section name.  */
10321
10322
48
  sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd));
10323
48
  len = strlen (buf) + 1;
10324
48
  threaded_name = (char *) bfd_alloc (abfd, len);
10325
48
  if (threaded_name == NULL)
10326
0
    return false;
10327
48
  memcpy (threaded_name, buf, len);
10328
10329
48
  sect = bfd_make_section_anyway_with_flags (abfd, threaded_name,
10330
48
               SEC_HAS_CONTENTS);
10331
48
  if (sect == NULL)
10332
0
    return false;
10333
48
  sect->size = size;
10334
48
  sect->filepos = filepos;
10335
48
  sect->alignment_power = 2;
10336
10337
48
  return elfcore_maybe_make_sect (abfd, name, sect);
10338
48
}
10339
10340
static bool
10341
elfcore_make_auxv_note_section (bfd *abfd, Elf_Internal_Note *note,
10342
        size_t offs)
10343
0
{
10344
0
  asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv",
10345
0
                   SEC_HAS_CONTENTS);
10346
10347
0
  if (sect == NULL)
10348
0
    return false;
10349
10350
0
  sect->size = note->descsz - offs;
10351
0
  sect->filepos = note->descpos + offs;
10352
0
  sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
10353
10354
0
  return true;
10355
0
}
10356
10357
/* prstatus_t exists on:
10358
     solaris 2.5+
10359
     linux 2.[01] + glibc
10360
     unixware 4.2
10361
*/
10362
10363
#if defined (HAVE_PRSTATUS_T)
10364
10365
static bool
10366
elfcore_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
10367
70
{
10368
70
  size_t size;
10369
70
  int offset;
10370
10371
70
  if (note->descsz == sizeof (prstatus_t))
10372
0
    {
10373
0
      prstatus_t prstat;
10374
10375
0
      size = sizeof (prstat.pr_reg);
10376
0
      offset   = offsetof (prstatus_t, pr_reg);
10377
0
      memcpy (&prstat, note->descdata, sizeof (prstat));
10378
10379
      /* Do not overwrite the core signal if it
10380
   has already been set by another thread.  */
10381
0
      if (elf_tdata (abfd)->core->signal == 0)
10382
0
  elf_tdata (abfd)->core->signal = prstat.pr_cursig;
10383
0
      if (elf_tdata (abfd)->core->pid == 0)
10384
0
  elf_tdata (abfd)->core->pid = prstat.pr_pid;
10385
10386
      /* pr_who exists on:
10387
   solaris 2.5+
10388
   unixware 4.2
10389
   pr_who doesn't exist on:
10390
   linux 2.[01]
10391
   */
10392
#if defined (HAVE_PRSTATUS_T_PR_WHO)
10393
      elf_tdata (abfd)->core->lwpid = prstat.pr_who;
10394
#else
10395
0
      elf_tdata (abfd)->core->lwpid = prstat.pr_pid;
10396
0
#endif
10397
0
    }
10398
70
#if defined (HAVE_PRSTATUS32_T)
10399
70
  else if (note->descsz == sizeof (prstatus32_t))
10400
9
    {
10401
      /* 64-bit host, 32-bit corefile */
10402
9
      prstatus32_t prstat;
10403
10404
9
      size = sizeof (prstat.pr_reg);
10405
9
      offset   = offsetof (prstatus32_t, pr_reg);
10406
9
      memcpy (&prstat, note->descdata, sizeof (prstat));
10407
10408
      /* Do not overwrite the core signal if it
10409
   has already been set by another thread.  */
10410
9
      if (elf_tdata (abfd)->core->signal == 0)
10411
6
  elf_tdata (abfd)->core->signal = prstat.pr_cursig;
10412
9
      if (elf_tdata (abfd)->core->pid == 0)
10413
6
  elf_tdata (abfd)->core->pid = prstat.pr_pid;
10414
10415
      /* pr_who exists on:
10416
   solaris 2.5+
10417
   unixware 4.2
10418
   pr_who doesn't exist on:
10419
   linux 2.[01]
10420
   */
10421
#if defined (HAVE_PRSTATUS32_T_PR_WHO)
10422
      elf_tdata (abfd)->core->lwpid = prstat.pr_who;
10423
#else
10424
9
      elf_tdata (abfd)->core->lwpid = prstat.pr_pid;
10425
9
#endif
10426
9
    }
10427
61
#endif /* HAVE_PRSTATUS32_T */
10428
61
  else
10429
61
    {
10430
      /* Fail - we don't know how to handle any other
10431
   note size (ie. data object type).  */
10432
61
      return true;
10433
61
    }
10434
10435
  /* Make a ".reg/999" section and a ".reg" section.  */
10436
9
  return _bfd_elfcore_make_pseudosection (abfd, NOTE_PSEUDO_SECTION_REG,
10437
9
            size, note->descpos + offset);
10438
70
}
10439
#endif /* defined (HAVE_PRSTATUS_T) */
10440
10441
/* Create a pseudosection containing the exact contents of NOTE.  */
10442
static bool
10443
elfcore_make_note_pseudosection (bfd *abfd,
10444
         char *name,
10445
         Elf_Internal_Note *note)
10446
39
{
10447
39
  return _bfd_elfcore_make_pseudosection (abfd, name,
10448
39
            note->descsz, note->descpos);
10449
39
}
10450
10451
/* There isn't a consistent prfpregset_t across platforms,
10452
   but it doesn't matter, because we don't have to pick this
10453
   data structure apart.  */
10454
10455
static bool
10456
elfcore_grok_prfpreg (bfd *abfd, Elf_Internal_Note *note)
10457
39
{
10458
39
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_REG2, note);
10459
39
}
10460
10461
/* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
10462
   type of NT_PRXFPREG.  Just include the whole note's contents
10463
   literally.  */
10464
10465
static bool
10466
elfcore_grok_prxfpreg (bfd *abfd, Elf_Internal_Note *note)
10467
0
{
10468
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_XFP, note);
10469
0
}
10470
10471
/* Linux dumps the Intel XSAVE extended state in a note named "LINUX"
10472
   with a note type of NT_X86_XSTATE.  Just include the whole note's
10473
   contents literally.  */
10474
10475
static bool
10476
elfcore_grok_xstatereg (bfd *abfd, Elf_Internal_Note *note)
10477
0
{
10478
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_XSTATE, note);
10479
0
}
10480
10481
static bool
10482
elfcore_grok_sspreg (bfd *abfd, Elf_Internal_Note *note)
10483
0
{
10484
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_SSP, note);
10485
0
}
10486
10487
/* Linux dumps the XSAVE Layout description in a note named "LINUX"
10488
   with a note type of NT_X86_XSAVE_LAYOUT. */
10489
static bool
10490
elfcore_grok_xsave_layout_desc (bfd *abfd, Elf_Internal_Note *note)
10491
0
{
10492
0
  return elfcore_make_note_pseudosection (abfd,
10493
0
            NOTE_PSEUDO_SECTION_XSAVE_LAYOUT,
10494
0
            note);
10495
0
}
10496
10497
static bool
10498
elfcore_grok_ppc_vmx (bfd *abfd, Elf_Internal_Note *note)
10499
0
{
10500
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_PPC_VMX, note);
10501
0
}
10502
10503
static bool
10504
elfcore_grok_ppc_vsx (bfd *abfd, Elf_Internal_Note *note)
10505
0
{
10506
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_PPC_VSX, note);
10507
0
}
10508
10509
static bool
10510
elfcore_grok_ppc_tar (bfd *abfd, Elf_Internal_Note *note)
10511
0
{
10512
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_PPC_TAR, note);
10513
0
}
10514
10515
static bool
10516
elfcore_grok_ppc_ppr (bfd *abfd, Elf_Internal_Note *note)
10517
0
{
10518
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_PPC_PPR, note);
10519
0
}
10520
10521
static bool
10522
elfcore_grok_ppc_dscr (bfd *abfd, Elf_Internal_Note *note)
10523
0
{
10524
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_PPC_DSCR, note);
10525
0
}
10526
10527
static bool
10528
elfcore_grok_ppc_ebb (bfd *abfd, Elf_Internal_Note *note)
10529
0
{
10530
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_PPC_EBB, note);
10531
0
}
10532
10533
static bool
10534
elfcore_grok_ppc_pmu (bfd *abfd, Elf_Internal_Note *note)
10535
0
{
10536
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_PPC_PMU, note);
10537
0
}
10538
10539
static bool
10540
elfcore_grok_ppc_tm_cgpr (bfd *abfd, Elf_Internal_Note *note)
10541
0
{
10542
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_PPC_TM_CGPR, note);
10543
0
}
10544
10545
static bool
10546
elfcore_grok_ppc_tm_cfpr (bfd *abfd, Elf_Internal_Note *note)
10547
0
{
10548
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_PPC_TM_CFPR, note);
10549
0
}
10550
10551
static bool
10552
elfcore_grok_ppc_tm_cvmx (bfd *abfd, Elf_Internal_Note *note)
10553
0
{
10554
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_PPC_TM_CVMX, note);
10555
0
}
10556
10557
static bool
10558
elfcore_grok_ppc_tm_cvsx (bfd *abfd, Elf_Internal_Note *note)
10559
0
{
10560
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_PPC_TM_CVSX, note);
10561
0
}
10562
10563
static bool
10564
elfcore_grok_ppc_tm_spr (bfd *abfd, Elf_Internal_Note *note)
10565
0
{
10566
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_PPC_TM_SPR, note);
10567
0
}
10568
10569
static bool
10570
elfcore_grok_ppc_tm_ctar (bfd *abfd, Elf_Internal_Note *note)
10571
0
{
10572
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_PPC_TM_CTAR, note);
10573
0
}
10574
10575
static bool
10576
elfcore_grok_ppc_tm_cppr (bfd *abfd, Elf_Internal_Note *note)
10577
0
{
10578
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_PPC_TM_CPPR, note);
10579
0
}
10580
10581
static bool
10582
elfcore_grok_ppc_tm_cdscr (bfd *abfd, Elf_Internal_Note *note)
10583
0
{
10584
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_PPC_TM_CDSCR, note);
10585
0
}
10586
10587
static bool
10588
elfcore_grok_s390_high_gprs (bfd *abfd, Elf_Internal_Note *note)
10589
0
{
10590
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_S390_HIGH_GPRS, note);
10591
0
}
10592
10593
static bool
10594
elfcore_grok_s390_timer (bfd *abfd, Elf_Internal_Note *note)
10595
0
{
10596
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_S390_TIMER, note);
10597
0
}
10598
10599
static bool
10600
elfcore_grok_s390_todcmp (bfd *abfd, Elf_Internal_Note *note)
10601
0
{
10602
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_S390_TODCMP, note);
10603
0
}
10604
10605
static bool
10606
elfcore_grok_s390_todpreg (bfd *abfd, Elf_Internal_Note *note)
10607
0
{
10608
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_S390_TODPREG, note);
10609
0
}
10610
10611
static bool
10612
elfcore_grok_s390_ctrs (bfd *abfd, Elf_Internal_Note *note)
10613
0
{
10614
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_S390_CTRS, note);
10615
0
}
10616
10617
static bool
10618
elfcore_grok_s390_prefix (bfd *abfd, Elf_Internal_Note *note)
10619
0
{
10620
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_S390_PREFIX, note);
10621
0
}
10622
10623
static bool
10624
elfcore_grok_s390_last_break (bfd *abfd, Elf_Internal_Note *note)
10625
0
{
10626
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_S390_LAST_BREAK, note);
10627
0
}
10628
10629
static bool
10630
elfcore_grok_s390_system_call (bfd *abfd, Elf_Internal_Note *note)
10631
0
{
10632
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_S390_SYSTEM_CALL, note);
10633
0
}
10634
10635
static bool
10636
elfcore_grok_s390_tdb (bfd *abfd, Elf_Internal_Note *note)
10637
0
{
10638
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_S390_TDB, note);
10639
0
}
10640
10641
static bool
10642
elfcore_grok_s390_vxrs_low (bfd *abfd, Elf_Internal_Note *note)
10643
0
{
10644
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_S390_VXRS_LOW, note);
10645
0
}
10646
10647
static bool
10648
elfcore_grok_s390_vxrs_high (bfd *abfd, Elf_Internal_Note *note)
10649
0
{
10650
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_S390_VXRS_HIGH, note);
10651
0
}
10652
10653
static bool
10654
elfcore_grok_s390_gs_cb (bfd *abfd, Elf_Internal_Note *note)
10655
0
{
10656
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_S390_GS_CB, note);
10657
0
}
10658
10659
static bool
10660
elfcore_grok_s390_gs_bc (bfd *abfd, Elf_Internal_Note *note)
10661
0
{
10662
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_S390_GS_BC, note);
10663
0
}
10664
10665
static bool
10666
elfcore_grok_arm_vfp (bfd *abfd, Elf_Internal_Note *note)
10667
0
{
10668
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_ARM_VFP, note);
10669
0
}
10670
10671
static bool
10672
elfcore_grok_aarch_tls (bfd *abfd, Elf_Internal_Note *note)
10673
0
{
10674
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_AARCH_TLS, note);
10675
0
}
10676
10677
static bool
10678
elfcore_grok_aarch_hw_break (bfd *abfd, Elf_Internal_Note *note)
10679
0
{
10680
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_AARCH_HW_BREAK, note);
10681
0
}
10682
10683
static bool
10684
elfcore_grok_aarch_hw_watch (bfd *abfd, Elf_Internal_Note *note)
10685
0
{
10686
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_AARCH_HW_WATCH, note);
10687
0
}
10688
10689
static bool
10690
elfcore_grok_aarch_sve (bfd *abfd, Elf_Internal_Note *note)
10691
0
{
10692
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_AARCH_SVE, note);
10693
0
}
10694
10695
static bool
10696
elfcore_grok_aarch_pauth (bfd *abfd, Elf_Internal_Note *note)
10697
0
{
10698
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_AARCH_PAUTH, note);
10699
0
}
10700
10701
static bool
10702
elfcore_grok_aarch_mte (bfd *abfd, Elf_Internal_Note *note)
10703
0
{
10704
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_AARCH_MTE, note);
10705
0
}
10706
10707
static bool
10708
elfcore_grok_aarch_ssve (bfd *abfd, Elf_Internal_Note *note)
10709
0
{
10710
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_AARCH_SSVE, note);
10711
0
}
10712
10713
static bool
10714
elfcore_grok_aarch_za (bfd *abfd, Elf_Internal_Note *note)
10715
0
{
10716
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_AARCH_ZA, note);
10717
0
}
10718
10719
/* Convert NOTE into a bfd_section called ".reg-aarch-zt".  Return TRUE if
10720
   successful, otherwise return FALSE.  */
10721
10722
static bool
10723
elfcore_grok_aarch_zt (bfd *abfd, Elf_Internal_Note *note)
10724
0
{
10725
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_AARCH_ZT, note);
10726
0
}
10727
10728
/* Convert NOTE into a bfd_section called ".reg-aarch-gcs".  Return TRUE if
10729
   successful, otherwise return FALSE.  */
10730
10731
static bool
10732
elfcore_grok_aarch_gcs (bfd *abfd, Elf_Internal_Note *note)
10733
0
{
10734
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_AARCH_GCS, note);
10735
0
}
10736
10737
/* Convert NOTE into the appropriate note pseudo-section for the AArch64 FPMR.
10738
 * Return TRUE if successful, otherwise return FALSE.  */
10739
10740
static bool
10741
elfcore_grok_aarch_fpmr (bfd *abfd, Elf_Internal_Note *note)
10742
0
{
10743
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_AARCH_FPMR, note);
10744
0
}
10745
10746
static bool
10747
elfcore_grok_arc_v2 (bfd *abfd, Elf_Internal_Note *note)
10748
0
{
10749
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_ARC_V2, note);
10750
0
}
10751
10752
/* Convert NOTE into a bfd_section called ".reg-riscv-csr".  Return TRUE if
10753
   successful otherwise, return FALSE.  */
10754
10755
static bool
10756
elfcore_grok_riscv_csr (bfd *abfd, Elf_Internal_Note *note)
10757
0
{
10758
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_RISCV_CSR, note);
10759
0
}
10760
10761
/* Convert NOTE into a bfd_section called ".gdb-tdesc".  Return TRUE if
10762
   successful otherwise, return FALSE.  */
10763
10764
static bool
10765
elfcore_grok_gdb_tdesc (bfd *abfd, Elf_Internal_Note *note)
10766
0
{
10767
0
  return elfcore_make_note_pseudosection (abfd, ".gdb-tdesc", note);
10768
0
}
10769
10770
static bool
10771
elfcore_grok_i386_tls (bfd *abfd, Elf_Internal_Note *note)
10772
0
{
10773
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_I386_TLS,
10774
0
            note);
10775
0
}
10776
10777
static bool
10778
elfcore_grok_loongarch_cpucfg (bfd *abfd, Elf_Internal_Note *note)
10779
0
{
10780
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_LOONGARCH_CPUCFG, note);
10781
0
}
10782
10783
static bool
10784
elfcore_grok_loongarch_lbt (bfd *abfd, Elf_Internal_Note *note)
10785
0
{
10786
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_LOONGARCH_LBT, note);
10787
0
}
10788
10789
static bool
10790
elfcore_grok_loongarch_lsx (bfd *abfd, Elf_Internal_Note *note)
10791
0
{
10792
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_LOONGARCH_LSX, note);
10793
0
}
10794
10795
static bool
10796
elfcore_grok_loongarch_lasx (bfd *abfd, Elf_Internal_Note *note)
10797
0
{
10798
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_LOONGARCH_LASX, note);
10799
0
}
10800
10801
#if defined (HAVE_PRPSINFO_T)
10802
typedef prpsinfo_t   elfcore_psinfo_t;
10803
#if defined (HAVE_PRPSINFO32_T)   /* Sparc64 cross Sparc32 */
10804
typedef prpsinfo32_t elfcore_psinfo32_t;
10805
#endif
10806
#endif
10807
10808
#if defined (HAVE_PSINFO_T)
10809
typedef psinfo_t   elfcore_psinfo_t;
10810
#if defined (HAVE_PSINFO32_T)   /* Sparc64 cross Sparc32 */
10811
typedef psinfo32_t elfcore_psinfo32_t;
10812
#endif
10813
#endif
10814
10815
/* return a malloc'ed copy of a string at START which is at
10816
   most MAX bytes long, possibly without a terminating '\0'.
10817
   the copy will always have a terminating '\0'.  */
10818
10819
char *
10820
_bfd_elfcore_strndup (bfd *abfd, char *start, size_t max)
10821
0
{
10822
0
  char *dups;
10823
0
  char *end = (char *) memchr (start, '\0', max);
10824
0
  size_t len;
10825
10826
0
  if (end == NULL)
10827
0
    len = max;
10828
0
  else
10829
0
    len = end - start;
10830
10831
0
  dups = (char *) bfd_alloc (abfd, len + 1);
10832
0
  if (dups == NULL)
10833
0
    return NULL;
10834
10835
0
  memcpy (dups, start, len);
10836
0
  dups[len] = '\0';
10837
10838
0
  return dups;
10839
0
}
10840
10841
#if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
10842
static bool
10843
elfcore_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
10844
26
{
10845
26
  if (note->descsz == sizeof (elfcore_psinfo_t))
10846
0
    {
10847
0
      elfcore_psinfo_t psinfo;
10848
10849
0
      memcpy (&psinfo, note->descdata, sizeof (psinfo));
10850
10851
0
#if defined (HAVE_PSINFO_T_PR_PID) || defined (HAVE_PRPSINFO_T_PR_PID)
10852
0
      elf_tdata (abfd)->core->pid = psinfo.pr_pid;
10853
0
#endif
10854
0
      elf_tdata (abfd)->core->program
10855
0
  = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
10856
0
        sizeof (psinfo.pr_fname));
10857
10858
0
      elf_tdata (abfd)->core->command
10859
0
  = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
10860
0
        sizeof (psinfo.pr_psargs));
10861
0
    }
10862
26
#if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
10863
26
  else if (note->descsz == sizeof (elfcore_psinfo32_t))
10864
0
    {
10865
      /* 64-bit host, 32-bit corefile */
10866
0
      elfcore_psinfo32_t psinfo;
10867
10868
0
      memcpy (&psinfo, note->descdata, sizeof (psinfo));
10869
10870
0
#if defined (HAVE_PSINFO32_T_PR_PID) || defined (HAVE_PRPSINFO32_T_PR_PID)
10871
0
      elf_tdata (abfd)->core->pid = psinfo.pr_pid;
10872
0
#endif
10873
0
      elf_tdata (abfd)->core->program
10874
0
  = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
10875
0
        sizeof (psinfo.pr_fname));
10876
10877
0
      elf_tdata (abfd)->core->command
10878
0
  = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
10879
0
        sizeof (psinfo.pr_psargs));
10880
0
    }
10881
26
#endif
10882
10883
26
  else
10884
26
    {
10885
      /* Fail - we don't know how to handle any other
10886
   note size (ie. data object type).  */
10887
26
      return true;
10888
26
    }
10889
10890
  /* Note that for some reason, a spurious space is tacked
10891
     onto the end of the args in some (at least one anyway)
10892
     implementations, so strip it off if it exists.  */
10893
10894
0
  {
10895
0
    char *command = elf_tdata (abfd)->core->command;
10896
0
    int n = strlen (command);
10897
10898
0
    if (0 < n && command[n - 1] == ' ')
10899
0
      command[n - 1] = '\0';
10900
0
  }
10901
10902
0
  return true;
10903
26
}
10904
#endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
10905
10906
#if defined (HAVE_PSTATUS_T)
10907
static bool
10908
elfcore_grok_pstatus (bfd *abfd, Elf_Internal_Note *note)
10909
{
10910
  if (note->descsz == sizeof (pstatus_t)
10911
#if defined (HAVE_PXSTATUS_T)
10912
      || note->descsz == sizeof (pxstatus_t)
10913
#endif
10914
      )
10915
    {
10916
      pstatus_t pstat;
10917
10918
      memcpy (&pstat, note->descdata, sizeof (pstat));
10919
10920
      elf_tdata (abfd)->core->pid = pstat.pr_pid;
10921
    }
10922
#if defined (HAVE_PSTATUS32_T)
10923
  else if (note->descsz == sizeof (pstatus32_t))
10924
    {
10925
      /* 64-bit host, 32-bit corefile */
10926
      pstatus32_t pstat;
10927
10928
      memcpy (&pstat, note->descdata, sizeof (pstat));
10929
10930
      elf_tdata (abfd)->core->pid = pstat.pr_pid;
10931
    }
10932
#endif
10933
  /* Could grab some more details from the "representative"
10934
     lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
10935
     NT_LWPSTATUS note, presumably.  */
10936
10937
  return true;
10938
}
10939
#endif /* defined (HAVE_PSTATUS_T) */
10940
10941
#if defined (HAVE_LWPSTATUS_T)
10942
static bool
10943
elfcore_grok_lwpstatus (bfd *abfd, Elf_Internal_Note *note)
10944
{
10945
  lwpstatus_t lwpstat;
10946
  char buf[100];
10947
  char *name;
10948
  size_t len;
10949
  asection *sect;
10950
10951
  if (note->descsz != sizeof (lwpstat)
10952
#if defined (HAVE_LWPXSTATUS_T)
10953
      && note->descsz != sizeof (lwpxstatus_t)
10954
#endif
10955
      )
10956
    return true;
10957
10958
  memcpy (&lwpstat, note->descdata, sizeof (lwpstat));
10959
10960
  elf_tdata (abfd)->core->lwpid = lwpstat.pr_lwpid;
10961
  /* Do not overwrite the core signal if it has already been set by
10962
     another thread.  */
10963
  if (elf_tdata (abfd)->core->signal == 0)
10964
    elf_tdata (abfd)->core->signal = lwpstat.pr_cursig;
10965
10966
  /* Make a ".reg/999" section.  */
10967
10968
  sprintf (buf, NOTE_PSEUDO_SECTION_REG "/%d", elfcore_make_pid (abfd));
10969
  len = strlen (buf) + 1;
10970
  name = bfd_alloc (abfd, len);
10971
  if (name == NULL)
10972
    return false;
10973
  memcpy (name, buf, len);
10974
10975
  sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
10976
  if (sect == NULL)
10977
    return false;
10978
10979
#if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
10980
  sect->size = sizeof (lwpstat.pr_context.uc_mcontext.gregs);
10981
  sect->filepos = note->descpos
10982
    + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs);
10983
#endif
10984
10985
#if defined (HAVE_LWPSTATUS_T_PR_REG)
10986
  sect->size = sizeof (lwpstat.pr_reg);
10987
  sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg);
10988
#endif
10989
10990
  sect->alignment_power = 2;
10991
10992
  if (!elfcore_maybe_make_sect (abfd, NOTE_PSEUDO_SECTION_REG, sect))
10993
    return false;
10994
10995
  /* Make a ".reg2/999" section */
10996
10997
  sprintf (buf, NOTE_PSEUDO_SECTION_REG2 "/%d", elfcore_make_pid (abfd));
10998
  len = strlen (buf) + 1;
10999
  name = bfd_alloc (abfd, len);
11000
  if (name == NULL)
11001
    return false;
11002
  memcpy (name, buf, len);
11003
11004
  sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
11005
  if (sect == NULL)
11006
    return false;
11007
11008
#if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
11009
  sect->size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs);
11010
  sect->filepos = note->descpos
11011
    + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs);
11012
#endif
11013
11014
#if defined (HAVE_LWPSTATUS_T_PR_FPREG)
11015
  sect->size = sizeof (lwpstat.pr_fpreg);
11016
  sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg);
11017
#endif
11018
11019
  sect->alignment_power = 2;
11020
11021
  return elfcore_maybe_make_sect (abfd, NOTE_PSEUDO_SECTION_REG2, sect);
11022
}
11023
#endif /* defined (HAVE_LWPSTATUS_T) */
11024
11025
/* These constants, and the structure offsets used below, are defined by
11026
   Cygwin's core_dump.h */
11027
0
#define NOTE_INFO_PROCESS  1
11028
0
#define NOTE_INFO_THREAD   2
11029
0
#define NOTE_INFO_MODULE   3
11030
0
#define NOTE_INFO_MODULE64 4
11031
11032
static bool
11033
elfcore_grok_win32pstatus (bfd *abfd, Elf_Internal_Note *note)
11034
15
{
11035
15
  char buf[30];
11036
15
  char *name;
11037
15
  size_t len;
11038
15
  unsigned int name_size;
11039
15
  asection *sect;
11040
15
  unsigned int type;
11041
15
  int is_active_thread;
11042
15
  bfd_vma base_addr;
11043
11044
15
  if (note->descsz < 4)
11045
0
    return true;
11046
11047
15
  if (! startswith (note->namedata, "win32"))
11048
15
    return true;
11049
11050
0
  type = bfd_get_32 (abfd, note->descdata);
11051
11052
0
  static const struct
11053
0
  {
11054
0
    const char *type_name;
11055
0
    unsigned long min_size;
11056
0
  } size_check[] =
11057
0
      {
11058
0
       { "NOTE_INFO_PROCESS", 12 },
11059
0
       { "NOTE_INFO_THREAD", 12 },
11060
0
       { "NOTE_INFO_MODULE", 12 },
11061
0
       { "NOTE_INFO_MODULE64", 16 },
11062
0
      };
11063
11064
0
  if (type == 0 || type > (sizeof(size_check)/sizeof(size_check[0])))
11065
0
      return true;
11066
11067
0
  if (note->descsz < size_check[type - 1].min_size)
11068
0
    {
11069
0
      _bfd_error_handler (_("%pB: warning: win32pstatus %s of size %lu bytes"
11070
0
          " is too small"),
11071
0
        abfd, size_check[type - 1].type_name, note->descsz);
11072
0
      return true;
11073
0
    }
11074
11075
0
  switch (type)
11076
0
    {
11077
0
    case NOTE_INFO_PROCESS:
11078
      /* FIXME: need to add ->core->command.  */
11079
0
      elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, note->descdata + 4);
11080
0
      elf_tdata (abfd)->core->signal = bfd_get_32 (abfd, note->descdata + 8);
11081
0
      break;
11082
11083
0
    case NOTE_INFO_THREAD:
11084
      /* Make a ".reg/<tid>" section containing the Win32 API thread CONTEXT
11085
   structure. */
11086
      /* thread_info.tid */
11087
0
      sprintf (buf, NOTE_PSEUDO_SECTION_REG "/%ld", (long) bfd_get_32 (abfd, note->descdata + 4));
11088
11089
0
      len = strlen (buf) + 1;
11090
0
      name = (char *) bfd_alloc (abfd, len);
11091
0
      if (name == NULL)
11092
0
  return false;
11093
11094
0
      memcpy (name, buf, len);
11095
11096
0
      sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
11097
0
      if (sect == NULL)
11098
0
  return false;
11099
11100
      /* sizeof (thread_info.thread_context) */
11101
0
      sect->size = note->descsz - 12;
11102
      /* offsetof (thread_info.thread_context) */
11103
0
      sect->filepos = note->descpos + 12;
11104
0
      sect->alignment_power = 2;
11105
11106
      /* thread_info.is_active_thread */
11107
0
      is_active_thread = bfd_get_32 (abfd, note->descdata + 8);
11108
11109
0
      if (is_active_thread)
11110
0
  if (! elfcore_maybe_make_sect (abfd, NOTE_PSEUDO_SECTION_REG, sect))
11111
0
    return false;
11112
0
      break;
11113
11114
0
    case NOTE_INFO_MODULE:
11115
0
    case NOTE_INFO_MODULE64:
11116
      /* Make a ".module/xxxxxxxx" section.  */
11117
0
      if (type == NOTE_INFO_MODULE)
11118
0
  {
11119
    /* module_info.base_address */
11120
0
    base_addr = bfd_get_32 (abfd, note->descdata + 4);
11121
0
    sprintf (buf, ".module/%08lx", (unsigned long) base_addr);
11122
    /* module_info.module_name_size */
11123
0
    name_size = bfd_get_32 (abfd, note->descdata + 8);
11124
0
  }
11125
0
      else /* NOTE_INFO_MODULE64 */
11126
0
  {
11127
    /* module_info.base_address */
11128
0
    base_addr = bfd_get_64 (abfd, note->descdata + 4);
11129
0
    sprintf (buf, ".module/%016lx", (unsigned long) base_addr);
11130
    /* module_info.module_name_size */
11131
0
    name_size = bfd_get_32 (abfd, note->descdata + 12);
11132
0
  }
11133
11134
0
      len = strlen (buf) + 1;
11135
0
      name = (char *) bfd_alloc (abfd, len);
11136
0
      if (name == NULL)
11137
0
  return false;
11138
11139
0
      memcpy (name, buf, len);
11140
11141
0
      sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
11142
11143
0
      if (sect == NULL)
11144
0
  return false;
11145
11146
0
      if (note->descsz < 12 + name_size)
11147
0
  {
11148
0
    _bfd_error_handler (_("%pB: win32pstatus NOTE_INFO_MODULE of size %lu"
11149
0
        " is too small to contain a name of size %u"),
11150
0
            abfd, note->descsz, name_size);
11151
0
    return true;
11152
0
  }
11153
11154
0
      sect->size = note->descsz;
11155
0
      sect->filepos = note->descpos;
11156
0
      sect->alignment_power = 2;
11157
0
      break;
11158
11159
0
    default:
11160
0
      return true;
11161
0
    }
11162
11163
0
  return true;
11164
0
}
11165
11166
static bool
11167
elfcore_grok_note (bfd *abfd, Elf_Internal_Note *note)
11168
990
{
11169
990
  elf_backend_data *bed = get_elf_backend_data (abfd);
11170
11171
  /* Short cut for LINUX notes.  */
11172
990
  if (note->namesz == 6 /* strlen (NOTE_NAME_LINUX) + 1 */
11173
6
      && streq (note->namedata, NOTE_NAME_LINUX))
11174
0
    {
11175
0
      switch (note->type)
11176
0
  {
11177
0
  case NT_386_TLS: return elfcore_grok_i386_tls (abfd, note);
11178
0
  case NT_ARC_V2:   return elfcore_grok_arc_v2 (abfd, note);
11179
0
  case NT_ARM_FPMR: return elfcore_grok_aarch_fpmr (abfd, note);
11180
0
  case NT_ARM_GCS: return elfcore_grok_aarch_gcs (abfd, note);
11181
0
  case NT_ARM_HW_BREAK: return elfcore_grok_aarch_hw_break (abfd, note);
11182
0
  case NT_ARM_HW_WATCH: return elfcore_grok_aarch_hw_watch (abfd, note);
11183
0
  case NT_ARM_PAC_MASK: return elfcore_grok_aarch_pauth (abfd, note);
11184
0
  case NT_ARM_SSVE: return elfcore_grok_aarch_ssve (abfd, note);
11185
0
  case NT_ARM_SVE: return elfcore_grok_aarch_sve (abfd, note);
11186
0
  case NT_ARM_TAGGED_ADDR_CTRL: return elfcore_grok_aarch_mte (abfd, note);
11187
0
  case NT_ARM_TLS: return elfcore_grok_aarch_tls (abfd, note);
11188
0
  case NT_ARM_VFP: return elfcore_grok_arm_vfp (abfd, note);
11189
0
  case NT_ARM_ZA:   return elfcore_grok_aarch_za (abfd, note);
11190
0
  case NT_ARM_ZT:   return elfcore_grok_aarch_zt (abfd, note);
11191
0
  case NT_LARCH_CPUCFG: return elfcore_grok_loongarch_cpucfg (abfd, note);
11192
0
  case NT_LARCH_LASX: return elfcore_grok_loongarch_lasx (abfd, note);
11193
0
  case NT_LARCH_LBT: return elfcore_grok_loongarch_lbt (abfd, note);
11194
0
  case NT_LARCH_LSX: return elfcore_grok_loongarch_lsx (abfd, note);
11195
0
  case NT_PPC_DSCR: return elfcore_grok_ppc_dscr (abfd, note);
11196
0
  case NT_PPC_EBB: return elfcore_grok_ppc_ebb (abfd, note);
11197
0
  case NT_PPC_PMU: return elfcore_grok_ppc_pmu (abfd, note);
11198
0
  case NT_PPC_PPR: return elfcore_grok_ppc_ppr (abfd, note);
11199
0
  case NT_PPC_TAR: return elfcore_grok_ppc_tar (abfd, note);
11200
0
  case NT_PPC_TM_CDSCR: return elfcore_grok_ppc_tm_cdscr (abfd, note);
11201
0
  case NT_PPC_TM_CFPR: return elfcore_grok_ppc_tm_cfpr (abfd, note);
11202
0
  case NT_PPC_TM_CGPR: return elfcore_grok_ppc_tm_cgpr (abfd, note);
11203
0
  case NT_PPC_TM_CPPR: return elfcore_grok_ppc_tm_cppr (abfd, note);
11204
0
  case NT_PPC_TM_CTAR: return elfcore_grok_ppc_tm_ctar (abfd, note);
11205
0
  case NT_PPC_TM_CVMX: return elfcore_grok_ppc_tm_cvmx (abfd, note);
11206
0
  case NT_PPC_TM_CVSX: return elfcore_grok_ppc_tm_cvsx (abfd, note);
11207
0
  case NT_PPC_TM_SPR: return elfcore_grok_ppc_tm_spr (abfd, note);
11208
0
  case NT_PPC_VMX: return elfcore_grok_ppc_vmx (abfd, note);
11209
0
  case NT_PPC_VSX: return elfcore_grok_ppc_vsx (abfd, note);
11210
0
  case NT_PRXFPREG: return elfcore_grok_prxfpreg (abfd, note);
11211
0
  case NT_S390_CTRS: return elfcore_grok_s390_ctrs (abfd, note);
11212
0
  case NT_S390_GS_BC: return elfcore_grok_s390_gs_bc (abfd, note);
11213
0
  case NT_S390_GS_CB: return elfcore_grok_s390_gs_cb (abfd, note);
11214
0
  case NT_S390_HIGH_GPRS: return elfcore_grok_s390_high_gprs (abfd, note);
11215
0
  case NT_S390_LAST_BREAK:  return elfcore_grok_s390_last_break (abfd, note);
11216
0
  case NT_S390_PREFIX: return elfcore_grok_s390_prefix (abfd, note);
11217
0
  case NT_S390_SYSTEM_CALL: return elfcore_grok_s390_system_call (abfd, note);
11218
0
  case NT_S390_TDB: return elfcore_grok_s390_tdb (abfd, note);
11219
0
  case NT_S390_TIMER: return elfcore_grok_s390_timer (abfd, note);
11220
0
  case NT_S390_TODCMP: return elfcore_grok_s390_todcmp (abfd, note);
11221
0
  case NT_S390_TODPREG: return elfcore_grok_s390_todpreg (abfd, note);
11222
0
  case NT_S390_VXRS_HIGH: return elfcore_grok_s390_vxrs_high (abfd, note);
11223
0
  case NT_S390_VXRS_LOW: return elfcore_grok_s390_vxrs_low (abfd, note);
11224
0
  case NT_X86_SHSTK: return elfcore_grok_sspreg (abfd, note);
11225
0
  case NT_X86_XSTATE: return elfcore_grok_xstatereg (abfd, note);
11226
0
  case NT_X86_XSAVE_LAYOUT: return elfcore_grok_xsave_layout_desc (abfd, note);
11227
0
  default: break;
11228
0
  }
11229
0
    }
11230
  
11231
990
  switch (note->type)
11232
990
    {
11233
831
    default:
11234
831
      return true;
11235
11236
70
    case NT_PRSTATUS:
11237
70
      if (bed->elf_backend_grok_prstatus)
11238
42
  if ((*bed->elf_backend_grok_prstatus) (abfd, note))
11239
0
    return true;
11240
70
#if defined (HAVE_PRSTATUS_T)
11241
70
      return elfcore_grok_prstatus (abfd, note);
11242
#else
11243
      return true;
11244
#endif
11245
11246
#if defined (HAVE_PSTATUS_T)
11247
    case NT_PSTATUS:
11248
      return elfcore_grok_pstatus (abfd, note);
11249
#endif
11250
11251
#if defined (HAVE_LWPSTATUS_T)
11252
    case NT_LWPSTATUS:
11253
      return elfcore_grok_lwpstatus (abfd, note);
11254
#endif
11255
11256
39
    case NT_FPREGSET:   /* FIXME: rename to NT_PRFPREG.  */
11257
39
      return elfcore_grok_prfpreg (abfd, note);
11258
11259
15
    case NT_WIN32PSTATUS:
11260
15
      return elfcore_grok_win32pstatus (abfd, note);
11261
11262
3
    case NT_GDB_TDESC:
11263
3
      if (note->namesz == 4 && streq (note->namedata, NOTE_NAME_GDB))
11264
0
  return elfcore_grok_gdb_tdesc (abfd, note);
11265
3
      else
11266
3
  return true;
11267
11268
6
    case NT_RISCV_CSR:
11269
6
      if (note->namesz == 4 && streq (note->namedata, NOTE_NAME_GDB))
11270
0
  return elfcore_grok_riscv_csr (abfd, note);
11271
6
      else
11272
6
  return true;
11273
11274
26
    case NT_PRPSINFO:
11275
26
    case NT_PSINFO:
11276
26
      if (bed->elf_backend_grok_psinfo)
11277
18
  if ((*bed->elf_backend_grok_psinfo) (abfd, note))
11278
0
    return true;
11279
26
#if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
11280
26
      return elfcore_grok_psinfo (abfd, note);
11281
#else
11282
      return true;
11283
#endif
11284
11285
0
    case NT_AUXV:
11286
0
      return elfcore_make_auxv_note_section (abfd, note, 0);
11287
11288
0
    case NT_FILE:
11289
0
      return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.file",
11290
0
                note);
11291
11292
0
    case NT_SIGINFO:
11293
0
      return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.siginfo",
11294
0
                note);
11295
990
    }
11296
990
}
11297
11298
static bool
11299
elfobj_grok_gnu_build_id (bfd *abfd, Elf_Internal_Note *note)
11300
371
{
11301
371
  struct bfd_build_id* build_id;
11302
11303
371
  if (note->descsz == 0)
11304
54
    return false;
11305
11306
317
  build_id = bfd_alloc (abfd, sizeof (struct bfd_build_id) - 1 + note->descsz);
11307
317
  if (build_id == NULL)
11308
0
    return false;
11309
11310
317
  build_id->size = note->descsz;
11311
317
  memcpy (build_id->data, note->descdata, note->descsz);
11312
317
  abfd->build_id = build_id;
11313
11314
317
  return true;
11315
317
}
11316
11317
static bool
11318
elfobj_grok_gnu_note (bfd *abfd, Elf_Internal_Note *note)
11319
1.11k
{
11320
1.11k
  switch (note->type)
11321
1.11k
    {
11322
349
    default:
11323
349
      return true;
11324
11325
393
    case NT_GNU_PROPERTY_TYPE_0:
11326
393
      return _bfd_elf_parse_gnu_properties (abfd, note);
11327
11328
371
    case NT_GNU_BUILD_ID:
11329
371
      return elfobj_grok_gnu_build_id (abfd, note);
11330
1.11k
    }
11331
1.11k
}
11332
11333
static bool
11334
elfobj_grok_stapsdt_note_1 (bfd *abfd, Elf_Internal_Note *note)
11335
0
{
11336
0
  struct sdt_note *cur =
11337
0
    (struct sdt_note *) bfd_alloc (abfd,
11338
0
           sizeof (struct sdt_note) + note->descsz);
11339
11340
0
  cur->next = (struct sdt_note *) (elf_tdata (abfd))->sdt_note_head;
11341
0
  cur->size = (bfd_size_type) note->descsz;
11342
0
  memcpy (cur->data, note->descdata, note->descsz);
11343
11344
0
  elf_tdata (abfd)->sdt_note_head = cur;
11345
11346
0
  return true;
11347
0
}
11348
11349
static bool
11350
elfobj_grok_stapsdt_note (bfd *abfd, Elf_Internal_Note *note)
11351
4
{
11352
4
  switch (note->type)
11353
4
    {
11354
0
    case NT_STAPSDT:
11355
0
      return elfobj_grok_stapsdt_note_1 (abfd, note);
11356
11357
4
    default:
11358
4
      return true;
11359
4
    }
11360
4
}
11361
11362
static bool
11363
elfcore_grok_freebsd_psinfo (bfd *abfd, Elf_Internal_Note *note)
11364
0
{
11365
0
  size_t offset;
11366
11367
0
  switch (elf_elfheader (abfd)->e_ident[EI_CLASS])
11368
0
    {
11369
0
    case ELFCLASS32:
11370
0
      if (note->descsz < 108)
11371
0
  return false;
11372
0
      break;
11373
11374
0
    case ELFCLASS64:
11375
0
      if (note->descsz < 120)
11376
0
  return false;
11377
0
      break;
11378
11379
0
    default:
11380
0
      return false;
11381
0
    }
11382
11383
  /* Check for version 1 in pr_version.  */
11384
0
  if (bfd_h_get_32 (abfd, (bfd_byte *) note->descdata) != 1)
11385
0
    return false;
11386
11387
0
  offset = 4;
11388
11389
  /* Skip over pr_psinfosz. */
11390
0
  if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS32)
11391
0
    offset += 4;
11392
0
  else
11393
0
    {
11394
0
      offset += 4;  /* Padding before pr_psinfosz. */
11395
0
      offset += 8;
11396
0
    }
11397
11398
  /* pr_fname is PRFNAMESZ (16) + 1 bytes in size.  */
11399
0
  elf_tdata (abfd)->core->program
11400
0
    = _bfd_elfcore_strndup (abfd, note->descdata + offset, 17);
11401
0
  offset += 17;
11402
11403
  /* pr_psargs is PRARGSZ (80) + 1 bytes in size.  */
11404
0
  elf_tdata (abfd)->core->command
11405
0
    = _bfd_elfcore_strndup (abfd, note->descdata + offset, 81);
11406
0
  offset += 81;
11407
11408
  /* Padding before pr_pid.  */
11409
0
  offset += 2;
11410
11411
  /* The pr_pid field was added in version "1a".  */
11412
0
  if (note->descsz < offset + 4)
11413
0
    return true;
11414
11415
0
  elf_tdata (abfd)->core->pid
11416
0
    = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
11417
11418
0
  return true;
11419
0
}
11420
11421
static bool
11422
elfcore_grok_freebsd_prstatus (bfd *abfd, Elf_Internal_Note *note)
11423
0
{
11424
0
  size_t offset;
11425
0
  size_t size;
11426
0
  size_t min_size;
11427
11428
  /* Compute offset of pr_getregsz, skipping over pr_statussz.
11429
     Also compute minimum size of this note.  */
11430
0
  switch (elf_elfheader (abfd)->e_ident[EI_CLASS])
11431
0
    {
11432
0
    case ELFCLASS32:
11433
0
      offset = 4 + 4;
11434
0
      min_size = offset + (4 * 2) + 4 + 4 + 4;
11435
0
      break;
11436
11437
0
    case ELFCLASS64:
11438
0
      offset = 4 + 4 + 8; /* Includes padding before pr_statussz.  */
11439
0
      min_size = offset + (8 * 2) + 4 + 4 + 4 + 4;
11440
0
      break;
11441
11442
0
    default:
11443
0
      return false;
11444
0
    }
11445
11446
0
  if (note->descsz < min_size)
11447
0
    return false;
11448
11449
  /* Check for version 1 in pr_version.  */
11450
0
  if (bfd_h_get_32 (abfd, (bfd_byte *) note->descdata) != 1)
11451
0
    return false;
11452
11453
  /* Extract size of pr_reg from pr_gregsetsz.  */
11454
  /* Skip over pr_gregsetsz and pr_fpregsetsz.  */
11455
0
  if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS32)
11456
0
    {
11457
0
      size = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
11458
0
      offset += 4 * 2;
11459
0
    }
11460
0
  else
11461
0
    {
11462
0
      size = bfd_h_get_64 (abfd, (bfd_byte *) note->descdata + offset);
11463
0
      offset += 8 * 2;
11464
0
    }
11465
11466
  /* Skip over pr_osreldate.  */
11467
0
  offset += 4;
11468
11469
  /* Read signal from pr_cursig.  */
11470
0
  if (elf_tdata (abfd)->core->signal == 0)
11471
0
    elf_tdata (abfd)->core->signal
11472
0
      = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
11473
0
  offset += 4;
11474
11475
  /* Read TID from pr_pid.  */
11476
0
  elf_tdata (abfd)->core->lwpid
11477
0
      = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
11478
0
  offset += 4;
11479
11480
  /* Padding before pr_reg.  */
11481
0
  if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS64)
11482
0
    offset += 4;
11483
11484
  /* Make sure that there is enough data remaining in the note.  */
11485
0
  if ((note->descsz - offset) < size)
11486
0
    return false;
11487
11488
  /* Make a ".reg/999" section and a ".reg" section.  */
11489
0
  return _bfd_elfcore_make_pseudosection (abfd, NOTE_PSEUDO_SECTION_REG,
11490
0
            size, note->descpos + offset);
11491
0
}
11492
11493
static bool
11494
elfcore_grok_freebsd_note (bfd *abfd, Elf_Internal_Note *note)
11495
0
{
11496
0
  elf_backend_data *bed = get_elf_backend_data (abfd);
11497
11498
0
  switch (note->type)
11499
0
    {
11500
0
    case NT_PRSTATUS:
11501
0
      if (bed->elf_backend_grok_freebsd_prstatus)
11502
0
  if ((*bed->elf_backend_grok_freebsd_prstatus) (abfd, note))
11503
0
    return true;
11504
0
      return elfcore_grok_freebsd_prstatus (abfd, note);
11505
11506
0
    case NT_FPREGSET:
11507
0
      return elfcore_grok_prfpreg (abfd, note);
11508
11509
0
    case NT_PRPSINFO:
11510
0
      return elfcore_grok_freebsd_psinfo (abfd, note);
11511
11512
0
    case NT_FREEBSD_THRMISC:
11513
0
      return elfcore_make_note_pseudosection (abfd, ".thrmisc", note);
11514
11515
0
    case NT_FREEBSD_PROCSTAT_PROC:
11516
0
      return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.proc",
11517
0
                note);
11518
11519
0
    case NT_FREEBSD_PROCSTAT_FILES:
11520
0
      return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.files",
11521
0
                note);
11522
11523
0
    case NT_FREEBSD_PROCSTAT_VMMAP:
11524
0
      return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.vmmap",
11525
0
                note);
11526
11527
0
    case NT_FREEBSD_PROCSTAT_AUXV:
11528
0
      return elfcore_make_auxv_note_section (abfd, note, 4);
11529
11530
0
    case NT_FREEBSD_X86_SEGBASES:
11531
0
      return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_X86_SEGBASES, note);
11532
11533
0
    case NT_X86_XSTATE:
11534
0
      return elfcore_grok_xstatereg (abfd, note);
11535
11536
0
    case NT_FREEBSD_PTLWPINFO:
11537
0
      return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.lwpinfo",
11538
0
                note);
11539
11540
0
    case NT_ARM_TLS:
11541
0
      return elfcore_grok_aarch_tls (abfd, note);
11542
11543
0
    case NT_ARM_VFP:
11544
0
      return elfcore_grok_arm_vfp (abfd, note);
11545
11546
0
    default:
11547
0
      return true;
11548
0
    }
11549
0
}
11550
11551
static bool
11552
elfcore_netbsd_get_lwpid (Elf_Internal_Note *note, int *lwpidp)
11553
0
{
11554
0
  char *cp;
11555
11556
0
  cp = strchr (note->namedata, '@');
11557
0
  if (cp != NULL)
11558
0
    {
11559
0
      *lwpidp = atoi(cp + 1);
11560
0
      return true;
11561
0
    }
11562
0
  return false;
11563
0
}
11564
11565
static bool
11566
elfcore_grok_netbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
11567
0
{
11568
0
  if (note->descsz <= 0x7c + 31)
11569
0
    return false;
11570
11571
  /* Signal number at offset 0x08. */
11572
0
  elf_tdata (abfd)->core->signal
11573
0
    = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
11574
11575
  /* Process ID at offset 0x50. */
11576
0
  elf_tdata (abfd)->core->pid
11577
0
    = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x50);
11578
11579
  /* Command name at 0x7c (max 32 bytes, including nul). */
11580
0
  elf_tdata (abfd)->core->command
11581
0
    = _bfd_elfcore_strndup (abfd, note->descdata + 0x7c, 31);
11582
11583
0
  return elfcore_make_note_pseudosection (abfd, ".note.netbsdcore.procinfo",
11584
0
            note);
11585
0
}
11586
11587
static bool
11588
elfcore_grok_netbsd_note (bfd *abfd, Elf_Internal_Note *note)
11589
0
{
11590
0
  int lwp;
11591
11592
0
  if (elfcore_netbsd_get_lwpid (note, &lwp))
11593
0
    elf_tdata (abfd)->core->lwpid = lwp;
11594
11595
0
  switch (note->type)
11596
0
    {
11597
0
    case NT_NETBSDCORE_PROCINFO:
11598
      /* NetBSD-specific core "procinfo".  Note that we expect to
11599
   find this note before any of the others, which is fine,
11600
   since the kernel writes this note out first when it
11601
   creates a core file.  */
11602
0
      return elfcore_grok_netbsd_procinfo (abfd, note);
11603
0
    case NT_NETBSDCORE_AUXV:
11604
      /* NetBSD-specific Elf Auxiliary Vector data. */
11605
0
      return elfcore_make_auxv_note_section (abfd, note, 4);
11606
0
    case NT_NETBSDCORE_LWPSTATUS:
11607
0
      return elfcore_make_note_pseudosection (abfd,
11608
0
                ".note.netbsdcore.lwpstatus",
11609
0
                note);
11610
0
    default:
11611
0
      break;
11612
0
    }
11613
11614
  /* As of March 2020 there are no other machine-independent notes
11615
     defined for NetBSD core files.  If the note type is less
11616
     than the start of the machine-dependent note types, we don't
11617
     understand it.  */
11618
11619
0
  if (note->type < NT_NETBSDCORE_FIRSTMACH)
11620
0
    return true;
11621
11622
11623
0
  switch (bfd_get_arch (abfd))
11624
0
    {
11625
      /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
11626
   PT_GETFPREGS == mach+2.  */
11627
11628
0
    case bfd_arch_aarch64:
11629
0
    case bfd_arch_alpha:
11630
0
    case bfd_arch_sparc:
11631
0
      switch (note->type)
11632
0
  {
11633
0
  case NT_NETBSDCORE_FIRSTMACH+0:
11634
0
    return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_REG, note);
11635
11636
0
  case NT_NETBSDCORE_FIRSTMACH+2:
11637
0
    return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_REG2, note);
11638
11639
0
  default:
11640
0
    return true;
11641
0
  }
11642
11643
      /* On SuperH, PT_GETREGS == mach+3 and PT_GETFPREGS == mach+5.
11644
   There's also old PT___GETREGS40 == mach + 1 for old reg
11645
   structure which lacks GBR.  */
11646
11647
0
    case bfd_arch_sh:
11648
0
      switch (note->type)
11649
0
  {
11650
0
  case NT_NETBSDCORE_FIRSTMACH+3:
11651
0
    return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_REG, note);
11652
11653
0
  case NT_NETBSDCORE_FIRSTMACH+5:
11654
0
    return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_REG2, note);
11655
11656
0
  default:
11657
0
    return true;
11658
0
  }
11659
11660
      /* On all other arch's, PT_GETREGS == mach+1 and
11661
   PT_GETFPREGS == mach+3.  */
11662
11663
0
    default:
11664
0
      switch (note->type)
11665
0
  {
11666
0
  case NT_NETBSDCORE_FIRSTMACH+1:
11667
0
    return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_REG, note);
11668
11669
0
  case NT_NETBSDCORE_FIRSTMACH+3:
11670
0
    return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_REG2, note);
11671
11672
0
  default:
11673
0
    return true;
11674
0
  }
11675
0
    }
11676
    /* NOTREACHED */
11677
0
}
11678
11679
static bool
11680
elfcore_grok_openbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
11681
0
{
11682
0
  if (note->descsz <= 0x48 + 31)
11683
0
    return false;
11684
11685
  /* Signal number at offset 0x08. */
11686
0
  elf_tdata (abfd)->core->signal
11687
0
    = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
11688
11689
  /* Process ID at offset 0x20. */
11690
0
  elf_tdata (abfd)->core->pid
11691
0
    = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x20);
11692
11693
  /* Command name at 0x48 (max 32 bytes, including nul). */
11694
0
  elf_tdata (abfd)->core->command
11695
0
    = _bfd_elfcore_strndup (abfd, note->descdata + 0x48, 31);
11696
11697
0
  return true;
11698
0
}
11699
11700
/* Processes Solaris's process status note.
11701
   sig_off ~ offsetof(prstatus_t, pr_cursig)
11702
   pid_off ~ offsetof(prstatus_t, pr_pid)
11703
   lwpid_off ~ offsetof(prstatus_t, pr_who)
11704
   gregset_size ~ sizeof(gregset_t)
11705
   gregset_offset ~ offsetof(prstatus_t, pr_reg)  */
11706
11707
static bool
11708
elfcore_grok_solaris_prstatus (bfd *abfd, Elf_Internal_Note* note, int sig_off,
11709
             int pid_off, int lwpid_off, size_t gregset_size,
11710
             size_t gregset_offset)
11711
0
{
11712
0
  asection *sect = NULL;
11713
0
  elf_tdata (abfd)->core->signal
11714
0
    = bfd_get_16 (abfd, note->descdata + sig_off);
11715
0
  elf_tdata (abfd)->core->pid
11716
0
    = bfd_get_32 (abfd, note->descdata + pid_off);
11717
0
  elf_tdata (abfd)->core->lwpid
11718
0
    = bfd_get_32 (abfd, note->descdata + lwpid_off);
11719
11720
0
  sect = bfd_get_section_by_name (abfd, NOTE_PSEUDO_SECTION_REG);
11721
0
  if (sect != NULL)
11722
0
    sect->size = gregset_size;
11723
11724
0
  return _bfd_elfcore_make_pseudosection (abfd, NOTE_PSEUDO_SECTION_REG, gregset_size,
11725
0
            note->descpos + gregset_offset);
11726
0
}
11727
11728
/* Gets program and arguments from a core.
11729
   prog_off ~ offsetof(prpsinfo | psinfo_t, pr_fname)
11730
   comm_off ~ offsetof(prpsinfo | psinfo_t, pr_psargs)  */
11731
11732
static bool
11733
elfcore_grok_solaris_info(bfd *abfd, Elf_Internal_Note* note,
11734
        int prog_off, int comm_off)
11735
0
{
11736
0
  elf_tdata (abfd)->core->program
11737
0
    = _bfd_elfcore_strndup (abfd, note->descdata + prog_off, 16);
11738
0
  elf_tdata (abfd)->core->command
11739
0
    = _bfd_elfcore_strndup (abfd, note->descdata + comm_off, 80);
11740
11741
0
  return true;
11742
0
}
11743
11744
/* Processes Solaris's LWP status note.
11745
   gregset_size ~ sizeof(gregset_t)
11746
   gregset_off ~ offsetof(lwpstatus_t, pr_reg)
11747
   fpregset_size ~ sizeof(fpregset_t)
11748
   fpregset_off ~ offsetof(lwpstatus_t, pr_fpreg)  */
11749
11750
static bool
11751
elfcore_grok_solaris_lwpstatus (bfd *abfd, Elf_Internal_Note* note,
11752
        size_t gregset_size, int gregset_off,
11753
        size_t fpregset_size, int fpregset_off)
11754
0
{
11755
0
  asection *sect = NULL;
11756
0
  char reg2_section_name[16] = { 0 };
11757
11758
0
  (void) snprintf (reg2_section_name, 16, "%s/%i", NOTE_PSEUDO_SECTION_REG2,
11759
0
       elf_tdata (abfd)->core->lwpid);
11760
11761
  /* offsetof(lwpstatus_t, pr_lwpid) */
11762
0
  elf_tdata (abfd)->core->lwpid
11763
0
    = bfd_get_32 (abfd, note->descdata + 4);
11764
  /* offsetof(lwpstatus_t, pr_cursig) */
11765
0
  elf_tdata (abfd)->core->signal
11766
0
    = bfd_get_16 (abfd, note->descdata + 12);
11767
11768
0
  sect = bfd_get_section_by_name (abfd, NOTE_PSEUDO_SECTION_REG);
11769
0
  if (sect != NULL)
11770
0
    sect->size = gregset_size;
11771
0
  else if (!_bfd_elfcore_make_pseudosection (abfd, NOTE_PSEUDO_SECTION_REG, gregset_size,
11772
0
               note->descpos + gregset_off))
11773
0
    return false;
11774
11775
0
  sect = bfd_get_section_by_name (abfd, reg2_section_name);
11776
0
  if (sect != NULL)
11777
0
    {
11778
0
      sect->size = fpregset_size;
11779
0
      sect->filepos = note->descpos + fpregset_off;
11780
0
      sect->alignment_power = 2;
11781
0
    }
11782
0
  else if (!_bfd_elfcore_make_pseudosection (abfd, NOTE_PSEUDO_SECTION_REG2, fpregset_size,
11783
0
               note->descpos + fpregset_off))
11784
0
    return false;
11785
11786
0
  return true;
11787
0
}
11788
11789
static bool
11790
elfcore_grok_solaris_note_impl (bfd *abfd, Elf_Internal_Note *note)
11791
0
{
11792
0
  if (note == NULL)
11793
0
    return false;
11794
11795
  /* core files are identified as 32- or 64-bit, SPARC or x86,
11796
     by the size of the descsz which matches the sizeof()
11797
     the type appropriate for that note type (e.g., prstatus_t for
11798
     SOLARIS_NT_PRSTATUS) for the corresponding architecture
11799
     on Solaris. The core file bitness may differ from the bitness of
11800
     gdb itself, so fixed values are used instead of sizeof().
11801
     Appropriate fixed offsets are also used to obtain data from
11802
     the note.  */
11803
11804
0
  switch ((int) note->type)
11805
0
    {
11806
0
    case SOLARIS_NT_PRSTATUS:
11807
0
      switch (note->descsz)
11808
0
  {
11809
0
  case 508: /* sizeof(prstatus_t) SPARC 32-bit */
11810
0
    return elfcore_grok_solaris_prstatus(abfd, note,
11811
0
                 136, 216, 308, 152, 356);
11812
0
  case 904: /* sizeof(prstatus_t) SPARC 64-bit */
11813
0
    return elfcore_grok_solaris_prstatus(abfd, note,
11814
0
                 264, 360, 520, 304, 600);
11815
0
  case 432: /* sizeof(prstatus_t) Intel 32-bit */
11816
0
    return elfcore_grok_solaris_prstatus(abfd, note,
11817
0
                 136, 216, 308, 76, 356);
11818
0
  case 824: /* sizeof(prstatus_t) Intel 64-bit */
11819
0
    return elfcore_grok_solaris_prstatus(abfd, note,
11820
0
                 264, 360, 520, 224, 600);
11821
0
  default:
11822
0
    return true;
11823
0
  }
11824
11825
0
    case SOLARIS_NT_PSINFO:
11826
0
    case SOLARIS_NT_PRPSINFO:
11827
0
      switch (note->descsz)
11828
0
  {
11829
0
  case 260: /* sizeof(prpsinfo_t) SPARC and Intel 32-bit */
11830
0
    return elfcore_grok_solaris_info(abfd, note, 84, 100);
11831
0
  case 328: /* sizeof(prpsinfo_t) SPARC and Intel 64-bit */
11832
0
    return elfcore_grok_solaris_info(abfd, note, 120, 136);
11833
0
  case 360: /* sizeof(psinfo_t) SPARC and Intel 32-bit */
11834
0
    return elfcore_grok_solaris_info(abfd, note, 88, 104);
11835
0
  case 440: /* sizeof(psinfo_t) SPARC and Intel 64-bit */
11836
0
    return elfcore_grok_solaris_info(abfd, note, 136, 152);
11837
0
  default:
11838
0
    return true;
11839
0
  }
11840
11841
0
    case SOLARIS_NT_LWPSTATUS:
11842
0
      switch (note->descsz)
11843
0
  {
11844
0
  case 896: /* sizeof(lwpstatus_t) SPARC 32-bit */
11845
0
    return elfcore_grok_solaris_lwpstatus(abfd, note,
11846
0
            152, 344, 400, 496);
11847
0
  case 1392: /* sizeof(lwpstatus_t) SPARC 64-bit */
11848
0
    return elfcore_grok_solaris_lwpstatus(abfd, note,
11849
0
            304, 544, 544, 848);
11850
0
  case 800: /* sizeof(lwpstatus_t) Intel 32-bit */
11851
0
    return elfcore_grok_solaris_lwpstatus(abfd, note,
11852
0
            76, 344, 380, 420);
11853
0
  case 1296: /* sizeof(lwpstatus_t) Intel 64-bit */
11854
0
    return elfcore_grok_solaris_lwpstatus(abfd, note,
11855
0
            224, 544, 528, 768);
11856
0
  default:
11857
0
    return true;
11858
0
  }
11859
11860
0
    case SOLARIS_NT_LWPSINFO:
11861
      /* sizeof(lwpsinfo_t) on 32- and 64-bit, respectively */
11862
0
      if (note->descsz == 128 || note->descsz == 152)
11863
0
  elf_tdata (abfd)->core->lwpid =
11864
0
    bfd_get_32 (abfd, note->descdata + 4);
11865
0
      break;
11866
11867
0
    default:
11868
0
      break;
11869
0
    }
11870
11871
0
  return true;
11872
0
}
11873
11874
/* For name starting with "CORE" this may be either a Solaris
11875
   core file or a gdb-generated core file.  Do Solaris-specific
11876
   processing on selected note types first with
11877
   elfcore_grok_solaris_note(), then process the note
11878
   in elfcore_grok_note().  */
11879
11880
static bool
11881
elfcore_grok_solaris_note (bfd *abfd, Elf_Internal_Note *note)
11882
0
{
11883
0
  if (!elfcore_grok_solaris_note_impl (abfd, note))
11884
0
    return false;
11885
11886
0
  return elfcore_grok_note (abfd, note);
11887
0
}
11888
11889
static bool
11890
elfcore_grok_openbsd_note (bfd *abfd, Elf_Internal_Note *note)
11891
0
{
11892
0
  if (note->type == NT_OPENBSD_PROCINFO)
11893
0
    return elfcore_grok_openbsd_procinfo (abfd, note);
11894
11895
0
  if (note->type == NT_OPENBSD_REGS)
11896
0
    return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_REG, note);
11897
11898
0
  if (note->type == NT_OPENBSD_FPREGS)
11899
0
    return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_REG2, note);
11900
11901
0
  if (note->type == NT_OPENBSD_XFPREGS)
11902
0
    return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_XFP, note);
11903
11904
0
  if (note->type == NT_OPENBSD_AUXV)
11905
0
    return elfcore_make_auxv_note_section (abfd, note, 0);
11906
11907
0
  if (note->type == NT_OPENBSD_WCOOKIE)
11908
0
    {
11909
0
      asection *sect = bfd_make_section_anyway_with_flags (abfd, ".wcookie",
11910
0
                 SEC_HAS_CONTENTS);
11911
11912
0
      if (sect == NULL)
11913
0
  return false;
11914
0
      sect->size = note->descsz;
11915
0
      sect->filepos = note->descpos;
11916
0
      sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
11917
11918
0
      return true;
11919
0
    }
11920
11921
0
  return true;
11922
0
}
11923
11924
static bool
11925
elfcore_grok_nto_status (bfd *abfd, Elf_Internal_Note *note, long *tid)
11926
0
{
11927
0
  void *ddata = note->descdata;
11928
0
  char buf[100];
11929
0
  char *name;
11930
0
  asection *sect;
11931
0
  short sig;
11932
0
  unsigned flags;
11933
11934
0
  if (note->descsz < 16)
11935
0
    return false;
11936
11937
  /* nto_procfs_status 'pid' field is at offset 0.  */
11938
0
  elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, (bfd_byte *) ddata);
11939
11940
  /* nto_procfs_status 'tid' field is at offset 4.  Pass it back.  */
11941
0
  *tid = bfd_get_32 (abfd, (bfd_byte *) ddata + 4);
11942
11943
  /* nto_procfs_status 'flags' field is at offset 8.  */
11944
0
  flags = bfd_get_32 (abfd, (bfd_byte *) ddata + 8);
11945
11946
  /* nto_procfs_status 'what' field is at offset 14.  */
11947
0
  if ((sig = bfd_get_16 (abfd, (bfd_byte *) ddata + 14)) > 0)
11948
0
    {
11949
0
      elf_tdata (abfd)->core->signal = sig;
11950
0
      elf_tdata (abfd)->core->lwpid = *tid;
11951
0
    }
11952
11953
  /* _DEBUG_FLAG_CURTID (current thread) is 0x80.  Some cores
11954
     do not come from signals so we make sure we set the current
11955
     thread just in case.  */
11956
0
  if (flags & 0x00000080)
11957
0
    elf_tdata (abfd)->core->lwpid = *tid;
11958
11959
  /* Make a ".qnx_core_status/%d" section.  */
11960
0
  sprintf (buf, ".qnx_core_status/%ld", *tid);
11961
11962
0
  name = (char *) bfd_alloc (abfd, strlen (buf) + 1);
11963
0
  if (name == NULL)
11964
0
    return false;
11965
0
  strcpy (name, buf);
11966
11967
0
  sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
11968
0
  if (sect == NULL)
11969
0
    return false;
11970
11971
0
  sect->size    = note->descsz;
11972
0
  sect->filepos   = note->descpos;
11973
0
  sect->alignment_power = 2;
11974
11975
0
  return (elfcore_maybe_make_sect (abfd, ".qnx_core_status", sect));
11976
0
}
11977
11978
static bool
11979
elfcore_grok_nto_regs (bfd *abfd,
11980
           Elf_Internal_Note *note,
11981
           long tid,
11982
           char *base)
11983
0
{
11984
0
  char buf[100];
11985
0
  char *name;
11986
0
  asection *sect;
11987
11988
  /* Make a "(base)/%d" section.  */
11989
0
  sprintf (buf, "%s/%ld", base, tid);
11990
11991
0
  name = (char *) bfd_alloc (abfd, strlen (buf) + 1);
11992
0
  if (name == NULL)
11993
0
    return false;
11994
0
  strcpy (name, buf);
11995
11996
0
  sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
11997
0
  if (sect == NULL)
11998
0
    return false;
11999
12000
0
  sect->size    = note->descsz;
12001
0
  sect->filepos   = note->descpos;
12002
0
  sect->alignment_power = 2;
12003
12004
  /* This is the current thread.  */
12005
0
  if (elf_tdata (abfd)->core->lwpid == tid)
12006
0
    return elfcore_maybe_make_sect (abfd, base, sect);
12007
12008
0
  return true;
12009
0
}
12010
12011
static bool
12012
elfcore_grok_nto_note (bfd *abfd, Elf_Internal_Note *note)
12013
3
{
12014
  /* Every GREG section has a STATUS section before it.  Store the
12015
     tid from the previous call to pass down to the next gregs
12016
     function.  */
12017
3
  static long tid = 1;
12018
12019
3
  switch (note->type)
12020
3
    {
12021
0
    case QNT_CORE_INFO:
12022
0
      return elfcore_make_note_pseudosection (abfd, ".qnx_core_info", note);
12023
0
    case QNT_CORE_STATUS:
12024
0
      return elfcore_grok_nto_status (abfd, note, &tid);
12025
0
    case QNT_CORE_GREG:
12026
0
      return elfcore_grok_nto_regs (abfd, note, tid, NOTE_PSEUDO_SECTION_REG);
12027
0
    case QNT_CORE_FPREG:
12028
0
      return elfcore_grok_nto_regs (abfd, note, tid, NOTE_PSEUDO_SECTION_REG2);
12029
3
    default:
12030
3
      return true;
12031
3
    }
12032
3
}
12033
12034
static bool
12035
elfcore_grok_spu_note (bfd *abfd, Elf_Internal_Note *note)
12036
0
{
12037
0
  char *name;
12038
0
  asection *sect;
12039
0
  size_t len;
12040
12041
  /* Use note name as section name.  */
12042
0
  len = note->namesz;
12043
0
  name = (char *) bfd_alloc (abfd, len);
12044
0
  if (name == NULL)
12045
0
    return false;
12046
0
  memcpy (name, note->namedata, len);
12047
0
  name[len - 1] = '\0';
12048
12049
0
  sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
12050
0
  if (sect == NULL)
12051
0
    return false;
12052
12053
0
  sect->size    = note->descsz;
12054
0
  sect->filepos   = note->descpos;
12055
0
  sect->alignment_power = 1;
12056
12057
0
  return true;
12058
0
}
12059
12060
/* Function: elfcore_write_note
12061
12062
   Inputs:
12063
     buffer to hold note, and current size of buffer
12064
     name of note
12065
     type of note
12066
     data for note
12067
     size of data for note
12068
12069
   Writes note to end of buffer.  ELF64 notes are written exactly as
12070
   for ELF32, despite the current (as of 2006) ELF gabi specifying
12071
   that they ought to have 8-byte namesz and descsz field, and have
12072
   8-byte alignment.  Other writers, eg. Linux kernel, do the same.
12073
12074
   Return:
12075
   Pointer to realloc'd buffer, *BUFSIZ updated.  */
12076
12077
char *
12078
elfcore_write_note (bfd *abfd,
12079
        char *buf,
12080
        int *bufsiz,
12081
        const char *name,
12082
        int type,
12083
        const void *input,
12084
        int size)
12085
0
{
12086
0
  Elf_External_Note *xnp;
12087
0
  size_t namesz;
12088
0
  size_t newspace;
12089
0
  char *dest;
12090
12091
0
  namesz = 0;
12092
0
  if (name != NULL)
12093
0
    namesz = strlen (name) + 1;
12094
12095
0
  newspace = 12 + ((namesz + 3) & -4) + ((size + 3) & -4);
12096
12097
0
  buf = (char *) realloc (buf, *bufsiz + newspace);
12098
0
  if (buf == NULL)
12099
0
    return buf;
12100
0
  dest = buf + *bufsiz;
12101
0
  *bufsiz += newspace;
12102
0
  xnp = (Elf_External_Note *) dest;
12103
0
  H_PUT_32 (abfd, namesz, xnp->namesz);
12104
0
  H_PUT_32 (abfd, size, xnp->descsz);
12105
0
  H_PUT_32 (abfd, type, xnp->type);
12106
0
  dest = xnp->name;
12107
0
  if (name != NULL)
12108
0
    {
12109
0
      memcpy (dest, name, namesz);
12110
0
      dest += namesz;
12111
0
      while (namesz & 3)
12112
0
  {
12113
0
    *dest++ = '\0';
12114
0
    ++namesz;
12115
0
  }
12116
0
    }
12117
0
  memcpy (dest, input, size);
12118
0
  dest += size;
12119
0
  while (size & 3)
12120
0
    {
12121
0
      *dest++ = '\0';
12122
0
      ++size;
12123
0
    }
12124
0
  return buf;
12125
0
}
12126
12127
/* gcc-8 warns (*) on all the strncpy calls in this function about
12128
   possible string truncation.  The "truncation" is not a bug.  We
12129
   have an external representation of structs with fields that are not
12130
   necessarily NULL terminated and corresponding internal
12131
   representation fields that are one larger so that they can always
12132
   be NULL terminated.
12133
   gcc versions between 4.2 and 4.6 do not allow pragma control of
12134
   diagnostics inside functions, giving a hard error if you try to use
12135
   the finer control available with later versions.
12136
   gcc prior to 4.2 warns about diagnostic push and pop.
12137
   gcc-5, gcc-6 and gcc-7 warn that -Wstringop-truncation is unknown,
12138
   unless you also add #pragma GCC diagnostic ignored "-Wpragma".
12139
   (*) Depending on your system header files!  */
12140
#if GCC_VERSION >= 8000
12141
# pragma GCC diagnostic push
12142
# pragma GCC diagnostic ignored "-Wstringop-truncation"
12143
#endif
12144
char *
12145
elfcore_write_prpsinfo (bfd  *abfd,
12146
      char *buf,
12147
      int  *bufsiz,
12148
      const char *fname,
12149
      const char *psargs)
12150
0
{
12151
0
  elf_backend_data *bed = get_elf_backend_data (abfd);
12152
12153
0
  if (bed->elf_backend_write_core_note != NULL)
12154
0
    {
12155
0
      char *ret;
12156
0
      ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
12157
0
             NT_PRPSINFO, fname, psargs);
12158
0
      if (ret != NULL)
12159
0
  return ret;
12160
0
    }
12161
12162
0
#if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
12163
0
# if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
12164
0
  if (bed->s->elfclass == ELFCLASS32)
12165
0
    {
12166
#  if defined (HAVE_PSINFO32_T)
12167
      psinfo32_t data;
12168
      int note_type = NT_PSINFO;
12169
#  else
12170
0
      prpsinfo32_t data;
12171
0
      int note_type = NT_PRPSINFO;
12172
0
#  endif
12173
12174
0
      memset (&data, 0, sizeof (data));
12175
0
      strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
12176
0
      strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
12177
0
      return elfcore_write_note (abfd, buf, bufsiz,
12178
0
         NOTE_NAME_CORE, note_type, &data, sizeof (data));
12179
0
    }
12180
0
  else
12181
0
# endif
12182
0
    {
12183
# if defined (HAVE_PSINFO_T)
12184
      psinfo_t data;
12185
      int note_type = NT_PSINFO;
12186
# else
12187
0
      prpsinfo_t data;
12188
0
      int note_type = NT_PRPSINFO;
12189
0
# endif
12190
12191
0
      memset (&data, 0, sizeof (data));
12192
0
      strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
12193
0
      strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
12194
0
      return elfcore_write_note (abfd, buf, bufsiz,
12195
0
         NOTE_NAME_CORE, note_type, &data, sizeof (data));
12196
0
    }
12197
0
#endif  /* PSINFO_T or PRPSINFO_T */
12198
12199
0
  free (buf);
12200
0
  return NULL;
12201
0
}
12202
#if GCC_VERSION >= 8000
12203
# pragma GCC diagnostic pop
12204
#endif
12205
12206
char *
12207
elfcore_write_linux_prpsinfo32
12208
  (bfd *abfd, char *buf, int *bufsiz,
12209
   const struct elf_internal_linux_prpsinfo *prpsinfo)
12210
0
{
12211
0
  if (get_elf_backend_data (abfd)->linux_prpsinfo32_ugid16)
12212
0
    {
12213
0
      struct elf_external_linux_prpsinfo32_ugid16 data;
12214
12215
0
      swap_linux_prpsinfo32_ugid16_out (abfd, prpsinfo, &data);
12216
0
      return elfcore_write_note (abfd, buf, bufsiz, NOTE_NAME_CORE, NT_PRPSINFO,
12217
0
         &data, sizeof (data));
12218
0
    }
12219
0
  else
12220
0
    {
12221
0
      struct elf_external_linux_prpsinfo32_ugid32 data;
12222
12223
0
      swap_linux_prpsinfo32_ugid32_out (abfd, prpsinfo, &data);
12224
0
      return elfcore_write_note (abfd, buf, bufsiz, NOTE_NAME_CORE, NT_PRPSINFO,
12225
0
         &data, sizeof (data));
12226
0
    }
12227
0
}
12228
12229
char *
12230
elfcore_write_linux_prpsinfo64
12231
  (bfd *abfd, char *buf, int *bufsiz,
12232
   const struct elf_internal_linux_prpsinfo *prpsinfo)
12233
0
{
12234
0
  if (get_elf_backend_data (abfd)->linux_prpsinfo64_ugid16)
12235
0
    {
12236
0
      struct elf_external_linux_prpsinfo64_ugid16 data;
12237
12238
0
      swap_linux_prpsinfo64_ugid16_out (abfd, prpsinfo, &data);
12239
0
      return elfcore_write_note (abfd, buf, bufsiz,
12240
0
         NOTE_NAME_CORE, NT_PRPSINFO, &data, sizeof (data));
12241
0
    }
12242
0
  else
12243
0
    {
12244
0
      struct elf_external_linux_prpsinfo64_ugid32 data;
12245
12246
0
      swap_linux_prpsinfo64_ugid32_out (abfd, prpsinfo, &data);
12247
0
      return elfcore_write_note (abfd, buf, bufsiz,
12248
0
         NOTE_NAME_CORE, NT_PRPSINFO, &data, sizeof (data));
12249
0
    }
12250
0
}
12251
12252
char *
12253
elfcore_write_prstatus (bfd *abfd,
12254
      char *buf,
12255
      int *bufsiz,
12256
      long pid,
12257
      int cursig,
12258
      const void *gregs)
12259
0
{
12260
0
  elf_backend_data *bed = get_elf_backend_data (abfd);
12261
12262
0
  if (bed->elf_backend_write_core_note != NULL)
12263
0
    {
12264
0
      char *ret;
12265
0
      ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
12266
0
             NT_PRSTATUS,
12267
0
             pid, cursig, gregs);
12268
0
      if (ret != NULL)
12269
0
  return ret;
12270
0
    }
12271
12272
0
#if defined (HAVE_PRSTATUS_T)
12273
0
#if defined (HAVE_PRSTATUS32_T)
12274
0
  if (bed->s->elfclass == ELFCLASS32)
12275
0
    {
12276
0
      prstatus32_t prstat;
12277
12278
0
      memset (&prstat, 0, sizeof (prstat));
12279
0
      prstat.pr_pid = pid;
12280
0
      prstat.pr_cursig = cursig;
12281
0
      memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
12282
0
      return elfcore_write_note (abfd, buf, bufsiz, NOTE_NAME_CORE,
12283
0
         NT_PRSTATUS, &prstat, sizeof (prstat));
12284
0
    }
12285
0
  else
12286
0
#endif
12287
0
    {
12288
0
      prstatus_t prstat;
12289
12290
0
      memset (&prstat, 0, sizeof (prstat));
12291
0
      prstat.pr_pid = pid;
12292
0
      prstat.pr_cursig = cursig;
12293
0
      memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
12294
0
      return elfcore_write_note (abfd, buf, bufsiz, NOTE_NAME_CORE,
12295
0
         NT_PRSTATUS, &prstat, sizeof (prstat));
12296
0
    }
12297
0
#endif /* HAVE_PRSTATUS_T */
12298
12299
0
  free (buf);
12300
0
  return NULL;
12301
0
}
12302
12303
#if defined (HAVE_LWPSTATUS_T)
12304
char *
12305
elfcore_write_lwpstatus (bfd *abfd,
12306
       char *buf,
12307
       int *bufsiz,
12308
       long pid,
12309
       int cursig,
12310
       const void *gregs)
12311
{
12312
  lwpstatus_t lwpstat;
12313
12314
  memset (&lwpstat, 0, sizeof (lwpstat));
12315
  lwpstat.pr_lwpid  = pid >> 16;
12316
  lwpstat.pr_cursig = cursig;
12317
#if defined (HAVE_LWPSTATUS_T_PR_REG)
12318
  memcpy (&lwpstat.pr_reg, gregs, sizeof (lwpstat.pr_reg));
12319
#elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
12320
#if !defined(gregs)
12321
  memcpy (lwpstat.pr_context.uc_mcontext.gregs,
12322
    gregs, sizeof (lwpstat.pr_context.uc_mcontext.gregs));
12323
#else
12324
  memcpy (lwpstat.pr_context.uc_mcontext.__gregs,
12325
    gregs, sizeof (lwpstat.pr_context.uc_mcontext.__gregs));
12326
#endif
12327
#endif
12328
  return elfcore_write_note (abfd, buf, bufsiz, NOTE_NAME_CORE,
12329
           NT_LWPSTATUS, &lwpstat, sizeof (lwpstat));
12330
}
12331
#endif /* HAVE_LWPSTATUS_T */
12332
12333
#if defined (HAVE_PSTATUS_T)
12334
char *
12335
elfcore_write_pstatus (bfd *abfd,
12336
           char *buf,
12337
           int *bufsiz,
12338
           long pid,
12339
           int cursig ATTRIBUTE_UNUSED,
12340
           const void *gregs ATTRIBUTE_UNUSED)
12341
{
12342
#if defined (HAVE_PSTATUS32_T)
12343
  elf_backend_data *bed = get_elf_backend_data (abfd);
12344
12345
  if (bed->s->elfclass == ELFCLASS32)
12346
    {
12347
      pstatus32_t pstat;
12348
12349
      memset (&pstat, 0, sizeof (pstat));
12350
      pstat.pr_pid = pid & 0xffff;
12351
      buf = elfcore_write_note (abfd, buf, bufsiz, NOTE_NAME_CORE,
12352
        NT_PSTATUS, &pstat, sizeof (pstat));
12353
      return buf;
12354
    }
12355
  else
12356
#endif
12357
    {
12358
      pstatus_t pstat;
12359
12360
      memset (&pstat, 0, sizeof (pstat));
12361
      pstat.pr_pid = pid & 0xffff;
12362
      buf = elfcore_write_note (abfd, buf, bufsiz, NOTE_NAME_CORE,
12363
        NT_PSTATUS, &pstat, sizeof (pstat));
12364
      return buf;
12365
    }
12366
}
12367
#endif /* HAVE_PSTATUS_T */
12368
12369
char *
12370
elfcore_write_prfpreg (bfd *abfd,
12371
           char *buf,
12372
           int *bufsiz,
12373
           const void *fpregs,
12374
           int size)
12375
0
{
12376
0
  return elfcore_write_note (abfd, buf, bufsiz,
12377
0
           NOTE_NAME_CORE, NT_FPREGSET, fpregs, size);
12378
0
}
12379
12380
char *
12381
elfcore_write_prxfpreg (bfd *abfd,
12382
      char *buf,
12383
      int *bufsiz,
12384
      const void *xfpregs,
12385
      int size)
12386
0
{
12387
0
  return elfcore_write_note (abfd, buf, bufsiz,
12388
0
           NOTE_NAME_LINUX, NT_PRXFPREG, xfpregs, size);
12389
0
}
12390
12391
char *
12392
elfcore_write_xstatereg (bfd *abfd, char *buf, int *bufsiz,
12393
       const void *xfpregs, int size)
12394
0
{
12395
0
  char *note_name;
12396
12397
0
  if (get_elf_backend_data (abfd)->elf_osabi == ELFOSABI_FREEBSD)
12398
0
    note_name = NOTE_NAME_FREEBSD;
12399
0
  else
12400
0
    note_name = NOTE_NAME_LINUX;
12401
12402
0
  return elfcore_write_note (abfd, buf, bufsiz,
12403
0
           note_name, NT_X86_XSTATE, xfpregs, size);
12404
0
}
12405
12406
char *
12407
elfcore_write_xsave_layout (bfd *abfd, char *buf, int *bufsiz,
12408
          const void *xsave_layout, int size)
12409
0
{
12410
0
  return elfcore_write_note (abfd, buf, bufsiz, NOTE_NAME_LINUX,
12411
0
           NT_X86_XSAVE_LAYOUT, xsave_layout, size);
12412
0
}
12413
12414
static char *
12415
elfcore_write_sspreg (bfd *abfd, char *buf, int *bufsiz,
12416
          const void *ssp, int size)
12417
0
{
12418
0
  return elfcore_write_note (abfd, buf, bufsiz,
12419
0
           NOTE_NAME_LINUX, NT_X86_SHSTK, ssp, size);
12420
0
}
12421
12422
char *
12423
elfcore_write_x86_segbases (bfd *abfd, char *buf, int *bufsiz,
12424
          const void *regs, int size)
12425
0
{
12426
0
  return elfcore_write_note (abfd, buf, bufsiz,
12427
0
           NOTE_NAME_FREEBSD, NT_FREEBSD_X86_SEGBASES,
12428
0
           regs, size);
12429
0
}
12430
12431
char *
12432
elfcore_write_i386_tls (bfd *abfd, char *buf, int *bufsiz,
12433
          const void *regs, int size)
12434
0
{
12435
0
  return elfcore_write_note (abfd, buf, bufsiz, NOTE_NAME_LINUX, NT_386_TLS,
12436
0
           regs, size);
12437
0
}
12438
12439
char *
12440
elfcore_write_ppc_vmx (bfd *abfd,
12441
           char *buf,
12442
           int *bufsiz,
12443
           const void *ppc_vmx,
12444
           int size)
12445
0
{
12446
0
  return elfcore_write_note (abfd, buf, bufsiz,
12447
0
           NOTE_NAME_LINUX, NT_PPC_VMX, ppc_vmx, size);
12448
0
}
12449
12450
char *
12451
elfcore_write_ppc_vsx (bfd *abfd,
12452
           char *buf,
12453
           int *bufsiz,
12454
           const void *ppc_vsx,
12455
           int size)
12456
0
{
12457
0
  return elfcore_write_note (abfd, buf, bufsiz,
12458
0
           NOTE_NAME_LINUX, NT_PPC_VSX, ppc_vsx, size);
12459
0
}
12460
12461
char *
12462
elfcore_write_ppc_tar (bfd *abfd,
12463
           char *buf,
12464
           int *bufsiz,
12465
           const void *ppc_tar,
12466
           int size)
12467
0
{
12468
0
  return elfcore_write_note (abfd, buf, bufsiz,
12469
0
           NOTE_NAME_LINUX, NT_PPC_TAR, ppc_tar, size);
12470
0
}
12471
12472
char *
12473
elfcore_write_ppc_ppr (bfd *abfd,
12474
           char *buf,
12475
           int *bufsiz,
12476
           const void *ppc_ppr,
12477
           int size)
12478
0
{
12479
0
  return elfcore_write_note (abfd, buf, bufsiz,
12480
0
           NOTE_NAME_LINUX, NT_PPC_PPR, ppc_ppr, size);
12481
0
}
12482
12483
char *
12484
elfcore_write_ppc_dscr (bfd *abfd,
12485
      char *buf,
12486
      int *bufsiz,
12487
      const void *ppc_dscr,
12488
      int size)
12489
0
{
12490
0
  return elfcore_write_note (abfd, buf, bufsiz,
12491
0
           NOTE_NAME_LINUX, NT_PPC_DSCR, ppc_dscr, size);
12492
0
}
12493
12494
char *
12495
elfcore_write_ppc_ebb (bfd *abfd,
12496
           char *buf,
12497
           int *bufsiz,
12498
           const void *ppc_ebb,
12499
           int size)
12500
0
{
12501
0
  return elfcore_write_note (abfd, buf, bufsiz,
12502
0
           NOTE_NAME_LINUX, NT_PPC_EBB, ppc_ebb, size);
12503
0
}
12504
12505
char *
12506
elfcore_write_ppc_pmu (bfd *abfd,
12507
           char *buf,
12508
           int *bufsiz,
12509
           const void *ppc_pmu,
12510
           int size)
12511
0
{
12512
0
  return elfcore_write_note (abfd, buf, bufsiz,
12513
0
           NOTE_NAME_LINUX, NT_PPC_PMU, ppc_pmu, size);
12514
0
}
12515
12516
char *
12517
elfcore_write_ppc_tm_cgpr (bfd *abfd,
12518
         char *buf,
12519
         int *bufsiz,
12520
         const void *ppc_tm_cgpr,
12521
         int size)
12522
0
{
12523
0
  return elfcore_write_note (abfd, buf, bufsiz,
12524
0
           NOTE_NAME_LINUX, NT_PPC_TM_CGPR,
12525
0
           ppc_tm_cgpr, size);
12526
0
}
12527
12528
char *
12529
elfcore_write_ppc_tm_cfpr (bfd *abfd,
12530
         char *buf,
12531
         int *bufsiz,
12532
         const void *ppc_tm_cfpr,
12533
         int size)
12534
0
{
12535
0
  return elfcore_write_note (abfd, buf, bufsiz,
12536
0
           NOTE_NAME_LINUX, NT_PPC_TM_CFPR,
12537
0
           ppc_tm_cfpr, size);
12538
0
}
12539
12540
char *
12541
elfcore_write_ppc_tm_cvmx (bfd *abfd,
12542
         char *buf,
12543
         int *bufsiz,
12544
         const void *ppc_tm_cvmx,
12545
         int size)
12546
0
{
12547
0
  return elfcore_write_note (abfd, buf, bufsiz,
12548
0
           NOTE_NAME_LINUX, NT_PPC_TM_CVMX,
12549
0
           ppc_tm_cvmx, size);
12550
0
}
12551
12552
char *
12553
elfcore_write_ppc_tm_cvsx (bfd *abfd,
12554
         char *buf,
12555
         int *bufsiz,
12556
         const void *ppc_tm_cvsx,
12557
         int size)
12558
0
{
12559
0
  return elfcore_write_note (abfd, buf, bufsiz,
12560
0
           NOTE_NAME_LINUX, NT_PPC_TM_CVSX,
12561
0
           ppc_tm_cvsx, size);
12562
0
}
12563
12564
char *
12565
elfcore_write_ppc_tm_spr (bfd *abfd,
12566
        char *buf,
12567
        int *bufsiz,
12568
        const void *ppc_tm_spr,
12569
        int size)
12570
0
{
12571
0
  return elfcore_write_note (abfd, buf, bufsiz,
12572
0
           NOTE_NAME_LINUX, NT_PPC_TM_SPR,
12573
0
           ppc_tm_spr, size);
12574
0
}
12575
12576
char *
12577
elfcore_write_ppc_tm_ctar (bfd *abfd,
12578
         char *buf,
12579
         int *bufsiz,
12580
         const void *ppc_tm_ctar,
12581
         int size)
12582
0
{
12583
0
  return elfcore_write_note (abfd, buf, bufsiz,
12584
0
           NOTE_NAME_LINUX, NT_PPC_TM_CTAR,
12585
0
           ppc_tm_ctar, size);
12586
0
}
12587
12588
char *
12589
elfcore_write_ppc_tm_cppr (bfd *abfd,
12590
         char *buf,
12591
         int *bufsiz,
12592
         const void *ppc_tm_cppr,
12593
         int size)
12594
0
{
12595
0
  return elfcore_write_note (abfd, buf, bufsiz,
12596
0
           NOTE_NAME_LINUX, NT_PPC_TM_CPPR,
12597
0
           ppc_tm_cppr, size);
12598
0
}
12599
12600
char *
12601
elfcore_write_ppc_tm_cdscr (bfd *abfd,
12602
          char *buf,
12603
          int *bufsiz,
12604
          const void *ppc_tm_cdscr,
12605
          int size)
12606
0
{
12607
0
  return elfcore_write_note (abfd, buf, bufsiz,
12608
0
           NOTE_NAME_LINUX, NT_PPC_TM_CDSCR,
12609
0
           ppc_tm_cdscr, size);
12610
0
}
12611
12612
static char *
12613
elfcore_write_s390_high_gprs (bfd *abfd,
12614
            char *buf,
12615
            int *bufsiz,
12616
            const void *s390_high_gprs,
12617
            int size)
12618
0
{
12619
0
  return elfcore_write_note (abfd, buf, bufsiz,
12620
0
           NOTE_NAME_LINUX, NT_S390_HIGH_GPRS,
12621
0
           s390_high_gprs, size);
12622
0
}
12623
12624
char *
12625
elfcore_write_s390_timer (bfd *abfd,
12626
        char *buf,
12627
        int *bufsiz,
12628
        const void *s390_timer,
12629
        int size)
12630
0
{
12631
0
  return elfcore_write_note (abfd, buf, bufsiz,
12632
0
           NOTE_NAME_LINUX, NT_S390_TIMER,
12633
0
           s390_timer, size);
12634
0
}
12635
12636
char *
12637
elfcore_write_s390_todcmp (bfd *abfd,
12638
         char *buf,
12639
         int *bufsiz,
12640
         const void *s390_todcmp,
12641
         int size)
12642
0
{
12643
0
  return elfcore_write_note (abfd, buf, bufsiz,
12644
0
           NOTE_NAME_LINUX, NT_S390_TODCMP,
12645
0
           s390_todcmp, size);
12646
0
}
12647
12648
char *
12649
elfcore_write_s390_todpreg (bfd *abfd,
12650
          char *buf,
12651
          int *bufsiz,
12652
          const void *s390_todpreg,
12653
          int size)
12654
0
{
12655
0
  return elfcore_write_note (abfd, buf, bufsiz,
12656
0
           NOTE_NAME_LINUX, NT_S390_TODPREG,
12657
0
           s390_todpreg, size);
12658
0
}
12659
12660
char *
12661
elfcore_write_s390_ctrs (bfd *abfd,
12662
       char *buf,
12663
       int *bufsiz,
12664
       const void *s390_ctrs,
12665
       int size)
12666
0
{
12667
0
  return elfcore_write_note (abfd, buf, bufsiz,
12668
0
           NOTE_NAME_LINUX, NT_S390_CTRS, s390_ctrs, size);
12669
0
}
12670
12671
char *
12672
elfcore_write_s390_prefix (bfd *abfd,
12673
         char *buf,
12674
         int *bufsiz,
12675
         const void *s390_prefix,
12676
         int size)
12677
0
{
12678
0
  return elfcore_write_note (abfd, buf, bufsiz,
12679
0
           NOTE_NAME_LINUX, NT_S390_PREFIX,
12680
0
           s390_prefix, size);
12681
0
}
12682
12683
char *
12684
elfcore_write_s390_last_break (bfd *abfd,
12685
             char *buf,
12686
             int *bufsiz,
12687
             const void *s390_last_break,
12688
             int size)
12689
0
{
12690
0
  return elfcore_write_note (abfd, buf, bufsiz,
12691
0
           NOTE_NAME_LINUX, NT_S390_LAST_BREAK,
12692
0
           s390_last_break, size);
12693
0
}
12694
12695
char *
12696
elfcore_write_s390_system_call (bfd *abfd,
12697
        char *buf,
12698
        int *bufsiz,
12699
        const void *s390_system_call,
12700
        int size)
12701
0
{
12702
0
  return elfcore_write_note (abfd, buf, bufsiz,
12703
0
           NOTE_NAME_LINUX, NT_S390_SYSTEM_CALL,
12704
0
           s390_system_call, size);
12705
0
}
12706
12707
char *
12708
elfcore_write_s390_tdb (bfd *abfd,
12709
      char *buf,
12710
      int *bufsiz,
12711
      const void *s390_tdb,
12712
      int size)
12713
0
{
12714
0
  return elfcore_write_note (abfd, buf, bufsiz,
12715
0
           NOTE_NAME_LINUX, NT_S390_TDB, s390_tdb, size);
12716
0
}
12717
12718
char *
12719
elfcore_write_s390_vxrs_low (bfd *abfd,
12720
           char *buf,
12721
           int *bufsiz,
12722
           const void *s390_vxrs_low,
12723
           int size)
12724
0
{
12725
0
  return elfcore_write_note (abfd, buf, bufsiz,
12726
0
           NOTE_NAME_LINUX, NT_S390_VXRS_LOW,
12727
0
           s390_vxrs_low, size);
12728
0
}
12729
12730
char *
12731
elfcore_write_s390_vxrs_high (bfd *abfd,
12732
           char *buf,
12733
           int *bufsiz,
12734
           const void *s390_vxrs_high,
12735
           int size)
12736
0
{
12737
0
  return elfcore_write_note (abfd, buf, bufsiz,
12738
0
           NOTE_NAME_LINUX, NT_S390_VXRS_HIGH,
12739
0
           s390_vxrs_high, size);
12740
0
}
12741
12742
char *
12743
elfcore_write_s390_gs_cb (bfd *abfd,
12744
        char *buf,
12745
        int *bufsiz,
12746
        const void *s390_gs_cb,
12747
        int size)
12748
0
{
12749
0
  return elfcore_write_note (abfd, buf, bufsiz,
12750
0
           NOTE_NAME_LINUX, NT_S390_GS_CB,
12751
0
           s390_gs_cb, size);
12752
0
}
12753
12754
char *
12755
elfcore_write_s390_gs_bc (bfd *abfd,
12756
        char *buf,
12757
        int *bufsiz,
12758
        const void *s390_gs_bc,
12759
        int size)
12760
0
{
12761
0
  return elfcore_write_note (abfd, buf, bufsiz,
12762
0
           NOTE_NAME_LINUX, NT_S390_GS_BC,
12763
0
           s390_gs_bc, size);
12764
0
}
12765
12766
char *
12767
elfcore_write_arm_vfp (bfd *abfd,
12768
           char *buf,
12769
           int *bufsiz,
12770
           const void *arm_vfp,
12771
           int size)
12772
0
{
12773
0
  return elfcore_write_note (abfd, buf, bufsiz,
12774
0
           NOTE_NAME_LINUX, NT_ARM_VFP,
12775
0
           arm_vfp, size);
12776
0
}
12777
12778
char *
12779
elfcore_write_aarch_tls (bfd *abfd,
12780
           char *buf,
12781
           int *bufsiz,
12782
           const void *aarch_tls,
12783
           int size)
12784
0
{
12785
0
  return elfcore_write_note (abfd, buf, bufsiz,
12786
0
           NOTE_NAME_LINUX, NT_ARM_TLS, aarch_tls, size);
12787
0
}
12788
12789
char *
12790
elfcore_write_aarch_hw_break (bfd *abfd,
12791
          char *buf,
12792
          int *bufsiz,
12793
          const void *aarch_hw_break,
12794
          int size)
12795
0
{
12796
0
  return elfcore_write_note (abfd, buf, bufsiz,
12797
0
           NOTE_NAME_LINUX, NT_ARM_HW_BREAK,
12798
0
           aarch_hw_break, size);
12799
0
}
12800
12801
char *
12802
elfcore_write_aarch_hw_watch (bfd *abfd,
12803
          char *buf,
12804
          int *bufsiz,
12805
          const void *aarch_hw_watch,
12806
          int size)
12807
0
{
12808
0
  return elfcore_write_note (abfd, buf, bufsiz,
12809
0
           NOTE_NAME_LINUX, NT_ARM_HW_WATCH,
12810
0
           aarch_hw_watch, size);
12811
0
}
12812
12813
char *
12814
elfcore_write_aarch_sve (bfd *abfd,
12815
       char *buf,
12816
       int *bufsiz,
12817
       const void *aarch_sve,
12818
       int size)
12819
0
{
12820
0
  return elfcore_write_note (abfd, buf, bufsiz,
12821
0
           NOTE_NAME_LINUX, NT_ARM_SVE, aarch_sve, size);
12822
0
}
12823
12824
char *
12825
elfcore_write_aarch_pauth (bfd *abfd,
12826
         char *buf,
12827
         int *bufsiz,
12828
         const void *aarch_pauth,
12829
         int size)
12830
0
{
12831
0
  return elfcore_write_note (abfd, buf, bufsiz,
12832
0
           NOTE_NAME_LINUX, NT_ARM_PAC_MASK,
12833
0
           aarch_pauth, size);
12834
0
}
12835
12836
char *
12837
elfcore_write_aarch_mte (bfd *abfd,
12838
       char *buf,
12839
       int *bufsiz,
12840
       const void *aarch_mte,
12841
       int size)
12842
0
{
12843
0
  return elfcore_write_note (abfd, buf, bufsiz,
12844
0
           NOTE_NAME_LINUX, NT_ARM_TAGGED_ADDR_CTRL,
12845
0
           aarch_mte, size);
12846
0
}
12847
12848
char *
12849
elfcore_write_aarch_ssve (bfd *abfd,
12850
        char *buf,
12851
        int *bufsiz,
12852
        const void *aarch_ssve,
12853
        int size)
12854
0
{
12855
0
  return elfcore_write_note (abfd, buf, bufsiz,
12856
0
           NOTE_NAME_LINUX, NT_ARM_SSVE,
12857
0
           aarch_ssve, size);
12858
0
}
12859
12860
char *
12861
elfcore_write_aarch_za (bfd *abfd,
12862
      char *buf,
12863
      int *bufsiz,
12864
      const void *aarch_za,
12865
      int size)
12866
0
{
12867
0
  return elfcore_write_note (abfd, buf, bufsiz,
12868
0
           NOTE_NAME_LINUX, NT_ARM_ZA,
12869
0
           aarch_za, size);
12870
0
}
12871
12872
/* Write the buffer of zt register values in aarch_zt (length SIZE) into
12873
   the note buffer BUF and update *BUFSIZ.  ABFD is the bfd the note is being
12874
   written into.  Return a pointer to the new start of the note buffer, to
12875
   replace BUF which may no longer be valid.  */
12876
12877
char *
12878
elfcore_write_aarch_zt (bfd *abfd,
12879
      char *buf,
12880
      int *bufsiz,
12881
      const void *aarch_zt,
12882
      int size)
12883
0
{
12884
0
  return elfcore_write_note (abfd, buf, bufsiz,
12885
0
           NOTE_NAME_LINUX, NT_ARM_ZT,
12886
0
           aarch_zt, size);
12887
0
}
12888
12889
/* Write the buffer of GCS register values in AARCH_GCS (length SIZE) into
12890
   the note buffer BUF and update *BUFSIZ.  ABFD is the bfd the note is being
12891
   written into.  Return a pointer to the new start of the note buffer, to
12892
   replace BUF which may no longer be valid.  */
12893
12894
static char *
12895
elfcore_write_aarch_gcs (bfd *abfd, char *buf, int *bufsiz,
12896
       const void *aarch_gcs, int size)
12897
0
{
12898
0
  return elfcore_write_note (abfd, buf, bufsiz, NOTE_NAME_LINUX, NT_ARM_GCS,
12899
0
           aarch_gcs, size);
12900
0
}
12901
12902
/* Write the buffer of FPMR value in AARCH_FPMR (length SIZE) into
12903
   the note buffer BUF and update *BUFSIZ.  ABFD is the bfd the note is being
12904
   written into.  Return a pointer to the new start of the note buffer, to
12905
   replace BUF which may no longer be valid.  */
12906
12907
char *
12908
elfcore_write_aarch_fpmr (bfd *abfd,
12909
      char *buf,
12910
      int *bufsiz,
12911
      const void *aarch_fpmr,
12912
      int size)
12913
0
{
12914
0
  return elfcore_write_note (abfd, buf, bufsiz,
12915
0
           NOTE_NAME_LINUX, NT_ARM_FPMR, aarch_fpmr, size);
12916
0
}
12917
12918
char *
12919
elfcore_write_arc_v2 (bfd *abfd,
12920
          char *buf,
12921
          int *bufsiz,
12922
          const void *arc_v2,
12923
          int size)
12924
0
{
12925
0
  return elfcore_write_note (abfd, buf, bufsiz,
12926
0
           NOTE_NAME_LINUX, NT_ARC_V2, arc_v2, size);
12927
0
}
12928
12929
char *
12930
elfcore_write_loongarch_cpucfg (bfd *abfd,
12931
        char *buf,
12932
        int *bufsiz,
12933
        const void *loongarch_cpucfg,
12934
        int size)
12935
0
{
12936
0
  return elfcore_write_note (abfd, buf, bufsiz,
12937
0
           NOTE_NAME_LINUX, NT_LARCH_CPUCFG,
12938
0
           loongarch_cpucfg, size);
12939
0
}
12940
12941
char *
12942
elfcore_write_loongarch_lbt (bfd *abfd,
12943
           char *buf,
12944
           int *bufsiz,
12945
           const void *loongarch_lbt,
12946
           int size)
12947
0
{
12948
0
  return elfcore_write_note (abfd, buf, bufsiz,
12949
0
           NOTE_NAME_LINUX, NT_LARCH_LBT,
12950
0
           loongarch_lbt, size);
12951
0
}
12952
12953
char *
12954
elfcore_write_loongarch_lsx (bfd *abfd,
12955
           char *buf,
12956
           int *bufsiz,
12957
           const void *loongarch_lsx,
12958
           int size)
12959
0
{
12960
0
  return elfcore_write_note (abfd, buf, bufsiz,
12961
0
           NOTE_NAME_LINUX, NT_LARCH_LSX,
12962
0
           loongarch_lsx, size);
12963
0
}
12964
12965
char *
12966
elfcore_write_loongarch_lasx (bfd *abfd,
12967
            char *buf,
12968
            int *bufsiz,
12969
            const void *loongarch_lasx,
12970
            int size)
12971
0
{
12972
0
  return elfcore_write_note (abfd, buf, bufsiz,
12973
0
           NOTE_NAME_LINUX, NT_LARCH_LASX,
12974
0
           loongarch_lasx, size);
12975
0
}
12976
12977
/* Write the buffer of csr values in CSRS (length SIZE) into the note
12978
   buffer BUF and update *BUFSIZ.  ABFD is the bfd the note is being
12979
   written into.  Return a pointer to the new start of the note buffer, to
12980
   replace BUF which may no longer be valid.  */
12981
12982
char *
12983
elfcore_write_riscv_csr (bfd *abfd,
12984
       char *buf,
12985
       int *bufsiz,
12986
       const void *csrs,
12987
       int size)
12988
0
{
12989
0
  return elfcore_write_note (abfd, buf, bufsiz,
12990
0
           NOTE_NAME_GDB, NT_RISCV_CSR, csrs, size);
12991
0
}
12992
12993
/* Write the target description (a string) pointed to by TDESC, length
12994
   SIZE, into the note buffer BUF, and update *BUFSIZ.  ABFD is the bfd the
12995
   note is being written into.  Return a pointer to the new start of the
12996
   note buffer, to replace BUF which may no longer be valid.  */
12997
12998
char *
12999
elfcore_write_gdb_tdesc (bfd *abfd,
13000
       char *buf,
13001
       int *bufsiz,
13002
       const void *tdesc,
13003
       int size)
13004
0
{
13005
0
  return elfcore_write_note (abfd, buf, bufsiz,
13006
0
           NOTE_NAME_GDB, NT_GDB_TDESC, tdesc, size);
13007
0
}
13008
13009
char *
13010
elfcore_write_register_note (bfd *abfd,
13011
           char *buf,
13012
           int *bufsiz,
13013
           const char *section,
13014
           const void *data,
13015
           int size)
13016
0
{
13017
0
  static const struct
13018
0
    {
13019
0
      const char * section_name;
13020
0
      char *       (*writer) (bfd *, char *, int *, const void *, int);
13021
0
    }
13022
0
  note_writers [] =
13023
0
    {
13024
0
      { NOTE_PSEUDO_SECTION_AARCH_FPMR,       elfcore_write_aarch_fpmr},
13025
0
      { NOTE_PSEUDO_SECTION_AARCH_GCS,        elfcore_write_aarch_gcs},
13026
0
      { NOTE_PSEUDO_SECTION_AARCH_HW_BREAK,   elfcore_write_aarch_hw_break},
13027
0
      { NOTE_PSEUDO_SECTION_AARCH_HW_WATCH,   elfcore_write_aarch_hw_watch},
13028
0
      { NOTE_PSEUDO_SECTION_AARCH_MTE,        elfcore_write_aarch_mte},
13029
0
      { NOTE_PSEUDO_SECTION_AARCH_PAUTH,      elfcore_write_aarch_pauth},
13030
0
      { NOTE_PSEUDO_SECTION_AARCH_SSVE,       elfcore_write_aarch_ssve},
13031
0
      { NOTE_PSEUDO_SECTION_AARCH_SVE,        elfcore_write_aarch_sve},
13032
0
      { NOTE_PSEUDO_SECTION_AARCH_TLS,        elfcore_write_aarch_tls},
13033
0
      { NOTE_PSEUDO_SECTION_AARCH_ZA,         elfcore_write_aarch_za},
13034
0
      { NOTE_PSEUDO_SECTION_AARCH_ZT,         elfcore_write_aarch_zt},
13035
0
      { NOTE_PSEUDO_SECTION_ARC_V2,           elfcore_write_arc_v2},
13036
0
      { NOTE_PSEUDO_SECTION_ARM_VFP,          elfcore_write_arm_vfp},
13037
0
      { NOTE_PSEUDO_SECTION_I386_TLS,         elfcore_write_i386_tls},
13038
0
      { NOTE_PSEUDO_SECTION_LOONGARCH_CPUCFG, elfcore_write_loongarch_cpucfg},
13039
0
      { NOTE_PSEUDO_SECTION_LOONGARCH_LASX,   elfcore_write_loongarch_lasx},
13040
0
      { NOTE_PSEUDO_SECTION_LOONGARCH_LBT,    elfcore_write_loongarch_lbt},
13041
0
      { NOTE_PSEUDO_SECTION_LOONGARCH_LSX,    elfcore_write_loongarch_lsx},
13042
0
      { NOTE_PSEUDO_SECTION_PPC_DSCR,         elfcore_write_ppc_dscr},
13043
0
      { NOTE_PSEUDO_SECTION_PPC_EBB,          elfcore_write_ppc_ebb},
13044
0
      { NOTE_PSEUDO_SECTION_PPC_PMU,          elfcore_write_ppc_pmu},
13045
0
      { NOTE_PSEUDO_SECTION_PPC_PPR,          elfcore_write_ppc_ppr},
13046
0
      { NOTE_PSEUDO_SECTION_PPC_TAR,          elfcore_write_ppc_tar},
13047
0
      { NOTE_PSEUDO_SECTION_PPC_TM_CDSCR,     elfcore_write_ppc_tm_cdscr},
13048
0
      { NOTE_PSEUDO_SECTION_PPC_TM_CFPR,      elfcore_write_ppc_tm_cfpr},
13049
0
      { NOTE_PSEUDO_SECTION_PPC_TM_CGPR,      elfcore_write_ppc_tm_cgpr},
13050
0
      { NOTE_PSEUDO_SECTION_PPC_TM_CPPR,      elfcore_write_ppc_tm_cppr},
13051
0
      { NOTE_PSEUDO_SECTION_PPC_TM_CTAR,      elfcore_write_ppc_tm_ctar},
13052
0
      { NOTE_PSEUDO_SECTION_PPC_TM_CVMX,      elfcore_write_ppc_tm_cvmx},
13053
0
      { NOTE_PSEUDO_SECTION_PPC_TM_CVSX,      elfcore_write_ppc_tm_cvsx},
13054
0
      { NOTE_PSEUDO_SECTION_PPC_TM_SPR,       elfcore_write_ppc_tm_spr},
13055
0
      { NOTE_PSEUDO_SECTION_PPC_VMX,          elfcore_write_ppc_vmx},
13056
0
      { NOTE_PSEUDO_SECTION_PPC_VSX,          elfcore_write_ppc_vsx},
13057
0
      { NOTE_PSEUDO_SECTION_REG2,             elfcore_write_prfpreg},
13058
0
      { NOTE_PSEUDO_SECTION_RISCV_CSR,        elfcore_write_riscv_csr},
13059
0
      { NOTE_PSEUDO_SECTION_S390_CTRS,        elfcore_write_s390_ctrs},
13060
0
      { NOTE_PSEUDO_SECTION_S390_GS_BC,       elfcore_write_s390_gs_bc},
13061
0
      { NOTE_PSEUDO_SECTION_S390_GS_CB,       elfcore_write_s390_gs_cb},
13062
0
      { NOTE_PSEUDO_SECTION_S390_HIGH_GPRS,   elfcore_write_s390_high_gprs},
13063
0
      { NOTE_PSEUDO_SECTION_S390_LAST_BREAK,  elfcore_write_s390_last_break},
13064
0
      { NOTE_PSEUDO_SECTION_S390_PREFIX,      elfcore_write_s390_prefix},
13065
0
      { NOTE_PSEUDO_SECTION_S390_SYSTEM_CALL, elfcore_write_s390_system_call},
13066
0
      { NOTE_PSEUDO_SECTION_S390_TDB,         elfcore_write_s390_tdb},
13067
0
      { NOTE_PSEUDO_SECTION_S390_TIMER,       elfcore_write_s390_timer},
13068
0
      { NOTE_PSEUDO_SECTION_S390_TODCMP,      elfcore_write_s390_todcmp},
13069
0
      { NOTE_PSEUDO_SECTION_S390_TODPREG,     elfcore_write_s390_todpreg},
13070
0
      { NOTE_PSEUDO_SECTION_S390_VXRS_HIGH,   elfcore_write_s390_vxrs_high},
13071
0
      { NOTE_PSEUDO_SECTION_S390_VXRS_LOW,    elfcore_write_s390_vxrs_low},
13072
0
      { NOTE_PSEUDO_SECTION_SSP,              elfcore_write_sspreg},
13073
0
      { NOTE_PSEUDO_SECTION_TDESC,            elfcore_write_gdb_tdesc},
13074
0
      { NOTE_PSEUDO_SECTION_X86_SEGBASES,     elfcore_write_x86_segbases},
13075
0
      { NOTE_PSEUDO_SECTION_XFP,              elfcore_write_prxfpreg},
13076
0
      { NOTE_PSEUDO_SECTION_XSTATE,           elfcore_write_xstatereg},
13077
0
      { NOTE_PSEUDO_SECTION_XSAVE_LAYOUT,     elfcore_write_xsave_layout} /* NB/ No comma.  */
13078
0
    };
13079
13080
0
  int i;
13081
13082
0
  for (i = ARRAY_SIZE (note_writers); i--;)
13083
0
    if (streq (section, note_writers[i].section_name))
13084
0
      return  note_writers[i].writer (abfd, buf, bufsiz, data, size);
13085
13086
0
  return NULL;
13087
0
}
13088
13089
char *
13090
elfcore_write_file_note (bfd *obfd, char *note_data, int *note_size,
13091
       const void *buf, int bufsiz)
13092
0
{
13093
0
  return elfcore_write_note (obfd, note_data, note_size,
13094
0
           NOTE_NAME_CORE, NT_FILE, buf, bufsiz);
13095
0
}
13096
13097
static bool
13098
elf_parse_notes (bfd *abfd, char *buf, size_t size, file_ptr offset,
13099
     size_t align)
13100
6.23k
{
13101
6.23k
  char *p;
13102
13103
  /* NB: CORE PT_NOTE segments may have p_align values of 0 or 1.
13104
     gABI specifies that PT_NOTE alignment should be aligned to 4
13105
     bytes for 32-bit objects and to 8 bytes for 64-bit objects.  If
13106
     align is less than 4, we use 4 byte alignment.   */
13107
6.23k
  if (align < 4)
13108
2.28k
    align = 4;
13109
6.23k
  if (align != 4 && align != 8)
13110
1.60k
    return false;
13111
13112
4.63k
  p = buf;
13113
11.8k
  while (p < buf + size)
13114
10.6k
    {
13115
10.6k
      Elf_External_Note *xnp = (Elf_External_Note *) p;
13116
10.6k
      Elf_Internal_Note in;
13117
13118
10.6k
      if (offsetof (Elf_External_Note, name) > buf - p + size)
13119
561
  return false;
13120
13121
10.0k
      in.type = H_GET_32 (abfd, xnp->type);
13122
13123
10.0k
      in.namesz = H_GET_32 (abfd, xnp->namesz);
13124
10.0k
      in.namedata = xnp->name;
13125
10.0k
      if (in.namesz > buf - in.namedata + size)
13126
1.48k
  return false;
13127
13128
8.58k
      in.descsz = H_GET_32 (abfd, xnp->descsz);
13129
8.58k
      in.descdata = p + ELF_NOTE_DESC_OFFSET (in.namesz, align);
13130
8.58k
      in.descpos = offset + (in.descdata - buf);
13131
8.58k
      if (in.descsz != 0
13132
4.29k
    && (in.descdata >= buf + size
13133
4.18k
        || in.descsz > buf - in.descdata + size))
13134
1.00k
  return false;
13135
13136
7.57k
      switch (bfd_get_format (abfd))
13137
7.57k
  {
13138
0
  default:
13139
0
    return true;
13140
13141
1.01k
  case bfd_core:
13142
1.01k
    {
13143
8.11k
#define GROKER_ELEMENT(S,F) {S, sizeof (S) - 1, F}
13144
1.01k
      static const struct
13145
1.01k
      {
13146
1.01k
        const char * string;
13147
1.01k
        size_t len;
13148
1.01k
        bool (*func) (bfd *, Elf_Internal_Note *);
13149
1.01k
      }
13150
1.01k
      grokers[] =
13151
1.01k
      {
13152
1.01k
        GROKER_ELEMENT ("", elfcore_grok_note),
13153
1.01k
        GROKER_ELEMENT (NOTE_NAME_FREEBSD, elfcore_grok_freebsd_note),
13154
1.01k
        GROKER_ELEMENT ("NetBSD-CORE", elfcore_grok_netbsd_note),
13155
1.01k
        GROKER_ELEMENT ("OpenBSD", elfcore_grok_openbsd_note),
13156
1.01k
        GROKER_ELEMENT ("QNX", elfcore_grok_nto_note),
13157
1.01k
        GROKER_ELEMENT ("SPU/", elfcore_grok_spu_note),
13158
1.01k
        GROKER_ELEMENT ("GNU", elfobj_grok_gnu_note),
13159
1.01k
        GROKER_ELEMENT (NOTE_NAME_CORE, elfcore_grok_solaris_note)
13160
1.01k
      };
13161
1.01k
#undef GROKER_ELEMENT
13162
1.01k
      int i;
13163
13164
7.97k
      for (i = ARRAY_SIZE (grokers); i--;)
13165
7.97k
        {
13166
7.97k
    if (in.namesz >= grokers[i].len
13167
3.18k
        && strncmp (in.namedata, grokers[i].string,
13168
3.18k
        grokers[i].len) == 0)
13169
1.01k
      {
13170
1.01k
        if (! grokers[i].func (abfd, & in))
13171
0
          return false;
13172
1.01k
        break;
13173
1.01k
      }
13174
7.97k
        }
13175
1.01k
      break;
13176
1.01k
    }
13177
13178
6.55k
  case bfd_object:
13179
6.55k
    if (in.namesz == sizeof "GNU" && strcmp (in.namedata, "GNU") == 0)
13180
1.09k
      {
13181
1.09k
        if (! elfobj_grok_gnu_note (abfd, &in))
13182
337
    return false;
13183
1.09k
      }
13184
5.46k
    else if (in.namesz == sizeof "stapsdt"
13185
394
       && strcmp (in.namedata, "stapsdt") == 0)
13186
4
      {
13187
4
        if (! elfobj_grok_stapsdt_note (abfd, &in))
13188
0
    return false;
13189
4
      }
13190
6.22k
    break;
13191
7.57k
  }
13192
13193
7.23k
      p += ELF_NOTE_NEXT_OFFSET (in.namesz, in.descsz, align);
13194
7.23k
    }
13195
13196
1.24k
  return true;
13197
4.63k
}
13198
13199
bool
13200
_bfd_elf_read_notes (bfd *abfd, file_ptr offset, bfd_size_type size,
13201
         size_t align)
13202
3.20k
{
13203
3.20k
  char *buf;
13204
13205
3.20k
  if (size == 0 || (size + 1) == 0)
13206
436
    return true;
13207
13208
2.77k
  if (bfd_seek (abfd, offset, SEEK_SET) != 0)
13209
1.32k
    return false;
13210
13211
1.44k
  buf = (char *) _bfd_malloc_and_read (abfd, size + 1, size);
13212
1.44k
  if (buf == NULL)
13213
515
    return false;
13214
13215
  /* PR 17512: file: ec08f814
13216
     0-termintate the buffer so that string searches will not overflow.  */
13217
929
  buf[size] = 0;
13218
13219
929
  if (!elf_parse_notes (abfd, buf, size, offset, align))
13220
912
    {
13221
912
      free (buf);
13222
912
      return false;
13223
912
    }
13224
13225
17
  free (buf);
13226
17
  return true;
13227
929
}
13228

13229
/* Providing external access to the ELF program header table.  */
13230
13231
/* Return an upper bound on the number of bytes required to store a
13232
   copy of ABFD's program header table entries.  Return -1 if an error
13233
   occurs; bfd_get_error will return an appropriate code.  */
13234
13235
long
13236
bfd_get_elf_phdr_upper_bound (bfd *abfd)
13237
0
{
13238
0
  if (abfd->xvec->flavour != bfd_target_elf_flavour)
13239
0
    {
13240
0
      bfd_set_error (bfd_error_wrong_format);
13241
0
      return -1;
13242
0
    }
13243
13244
0
  return elf_elfheader (abfd)->e_phnum * sizeof (Elf_Internal_Phdr);
13245
0
}
13246
13247
/* Copy ABFD's program header table entries to *PHDRS.  The entries
13248
   will be stored as an array of Elf_Internal_Phdr structures, as
13249
   defined in include/elf/internal.h.  To find out how large the
13250
   buffer needs to be, call bfd_get_elf_phdr_upper_bound.
13251
13252
   Return the number of program header table entries read, or -1 if an
13253
   error occurs; bfd_get_error will return an appropriate code.  */
13254
13255
int
13256
bfd_get_elf_phdrs (bfd *abfd, void *phdrs)
13257
0
{
13258
0
  int num_phdrs;
13259
13260
0
  if (abfd->xvec->flavour != bfd_target_elf_flavour)
13261
0
    {
13262
0
      bfd_set_error (bfd_error_wrong_format);
13263
0
      return -1;
13264
0
    }
13265
13266
0
  num_phdrs = elf_elfheader (abfd)->e_phnum;
13267
0
  if (num_phdrs != 0)
13268
0
    memcpy (phdrs, elf_tdata (abfd)->phdr,
13269
0
      num_phdrs * sizeof (Elf_Internal_Phdr));
13270
13271
0
  return num_phdrs;
13272
0
}
13273
13274
enum elf_reloc_type_class
13275
_bfd_elf_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
13276
         const asection *rel_sec ATTRIBUTE_UNUSED,
13277
         const Elf_Internal_Rela *rela ATTRIBUTE_UNUSED)
13278
0
{
13279
0
  return reloc_class_normal;
13280
0
}
13281
13282
/* For RELA architectures, return the relocation value for a
13283
   relocation against a local symbol.  */
13284
13285
bfd_vma
13286
_bfd_elf_rela_local_sym (bfd *abfd,
13287
       Elf_Internal_Sym *sym,
13288
       asection **psec,
13289
       Elf_Internal_Rela *rel)
13290
0
{
13291
0
  asection *sec = *psec;
13292
0
  bfd_vma relocation;
13293
13294
0
  relocation = (sec->output_section->vma
13295
0
    + sec->output_offset
13296
0
    + sym->st_value);
13297
0
  if ((sec->flags & SEC_MERGE)
13298
0
      && ELF_ST_TYPE (sym->st_info) == STT_SECTION
13299
0
      && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
13300
0
    {
13301
0
      rel->r_addend =
13302
0
  _bfd_merged_section_offset (abfd, psec,
13303
0
            sym->st_value + rel->r_addend);
13304
0
      if (sec != *psec)
13305
0
  {
13306
    /* If we have changed the section, and our original section is
13307
       marked with SEC_EXCLUDE, it means that the original
13308
       SEC_MERGE section has been completely subsumed in some
13309
       other SEC_MERGE section.  In this case, we need to leave
13310
       some info around for --emit-relocs.  */
13311
0
    if ((sec->flags & SEC_EXCLUDE) != 0)
13312
0
      sec->kept_section = *psec;
13313
0
    sec = *psec;
13314
0
  }
13315
0
      rel->r_addend -= relocation;
13316
0
      rel->r_addend += sec->output_section->vma + sec->output_offset;
13317
0
    }
13318
0
  return relocation;
13319
0
}
13320
13321
bfd_vma
13322
_bfd_elf_rel_local_sym (bfd *abfd,
13323
      Elf_Internal_Sym *sym,
13324
      asection **psec,
13325
      bfd_vma addend)
13326
0
{
13327
0
  asection *sec = *psec;
13328
13329
0
  if (sec->sec_info_type != SEC_INFO_TYPE_MERGE)
13330
0
    return sym->st_value + addend;
13331
13332
0
  return _bfd_merged_section_offset (abfd, psec,
13333
0
             sym->st_value + addend);
13334
0
}
13335
13336
/* Adjust an address within a section.  Given OFFSET within SEC, return
13337
   the new offset within the section, based upon changes made to the
13338
   section.  Returns -1 if the offset is now invalid.
13339
   The offset (in abnd out) is in target sized bytes, however big a
13340
   byte may be.  */
13341
13342
bfd_vma
13343
_bfd_elf_section_offset (bfd *abfd,
13344
       struct bfd_link_info *info,
13345
       asection *sec,
13346
       bfd_vma offset)
13347
0
{
13348
0
  switch (sec->sec_info_type)
13349
0
    {
13350
0
    case SEC_INFO_TYPE_STABS:
13351
0
      return _bfd_stab_section_offset (sec, offset);
13352
13353
0
    case SEC_INFO_TYPE_EH_FRAME:
13354
0
      return _bfd_elf_eh_frame_section_offset (abfd, info, sec, offset);
13355
13356
0
    case SEC_INFO_TYPE_SFRAME:
13357
0
      return _bfd_elf_sframe_section_offset (abfd, info, sec, offset);
13358
13359
0
    default:
13360
0
      if ((sec->flags & SEC_ELF_REVERSE_COPY) != 0)
13361
0
  {
13362
    /* Reverse the offset.  */
13363
0
    elf_backend_data *bed = get_elf_backend_data (abfd);
13364
0
    bfd_size_type address_size = bed->s->arch_size / 8;
13365
13366
    /* address_size and sec->size are in octets.  Convert
13367
       to bytes before subtracting the original offset.  */
13368
0
    offset = ((sec->size - address_size)
13369
0
        / bfd_octets_per_byte (abfd, sec) - offset);
13370
0
  }
13371
0
      return offset;
13372
0
    }
13373
0
}
13374

13375
long
13376
_bfd_elf_get_synthetic_symtab (bfd *abfd,
13377
             long symcount ATTRIBUTE_UNUSED,
13378
             asymbol **syms ATTRIBUTE_UNUSED,
13379
             long dynsymcount,
13380
             asymbol **dynsyms,
13381
             asymbol **ret)
13382
7.33k
{
13383
7.33k
  elf_backend_data *bed = get_elf_backend_data (abfd);
13384
7.33k
  asection *relplt;
13385
7.33k
  asymbol *s;
13386
7.33k
  const char *relplt_name;
13387
7.33k
  bool (*slurp_relocs) (bfd *, asection *, asymbol **, bool);
13388
7.33k
  arelent *p;
13389
7.33k
  long count, i, n;
13390
7.33k
  size_t size;
13391
7.33k
  Elf_Internal_Shdr *hdr;
13392
7.33k
  char *names;
13393
7.33k
  asection *plt;
13394
13395
7.33k
  *ret = NULL;
13396
13397
7.33k
  if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0)
13398
7.28k
    return 0;
13399
13400
56
  if (dynsymcount <= 0)
13401
44
    return 0;
13402
13403
12
  if (!bed->plt_sym_val)
13404
12
    return 0;
13405
13406
0
  relplt_name = bed->relplt_name;
13407
0
  if (relplt_name == NULL)
13408
0
    relplt_name = bed->rela_plts_and_copies_p ? ".rela.plt" : ".rel.plt";
13409
0
  relplt = bfd_get_section_by_name (abfd, relplt_name);
13410
0
  if (relplt == NULL)
13411
0
    return 0;
13412
13413
0
  hdr = &elf_section_data (relplt)->this_hdr;
13414
0
  if (hdr->sh_link != elf_dynsymtab (abfd)
13415
0
      || (hdr->sh_type != SHT_REL && hdr->sh_type != SHT_RELA))
13416
0
    return 0;
13417
13418
0
  plt = bfd_get_section_by_name (abfd, ".plt");
13419
0
  if (plt == NULL)
13420
0
    return 0;
13421
13422
0
  slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
13423
0
  if (! (*slurp_relocs) (abfd, relplt, dynsyms, true))
13424
0
    return -1;
13425
13426
0
  count = NUM_SHDR_ENTRIES (hdr);
13427
0
  size = count * sizeof (asymbol);
13428
0
  p = relplt->relocation;
13429
0
  for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
13430
0
    {
13431
0
      size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
13432
0
      if (p->addend != 0)
13433
0
  {
13434
0
#ifdef BFD64
13435
0
    size += sizeof ("+0x") - 1 + 8 + 8 * (bed->s->elfclass == ELFCLASS64);
13436
#else
13437
    size += sizeof ("+0x") - 1 + 8;
13438
#endif
13439
0
  }
13440
0
    }
13441
13442
0
  s = *ret = (asymbol *) bfd_malloc (size);
13443
0
  if (s == NULL)
13444
0
    return -1;
13445
13446
0
  names = (char *) (s + count);
13447
0
  p = relplt->relocation;
13448
0
  n = 0;
13449
0
  for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
13450
0
    {
13451
0
      size_t len;
13452
0
      bfd_vma addr;
13453
13454
0
      addr = bed->plt_sym_val (i, plt, p);
13455
0
      if (addr == (bfd_vma) -1)
13456
0
  continue;
13457
13458
0
      *s = **p->sym_ptr_ptr;
13459
      /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set.  Since
13460
   we are defining a symbol, ensure one of them is set.  */
13461
0
      if ((s->flags & BSF_LOCAL) == 0)
13462
0
  s->flags |= BSF_GLOBAL;
13463
0
      s->flags |= BSF_SYNTHETIC;
13464
0
      s->section = plt;
13465
0
      s->value = addr - plt->vma;
13466
0
      s->name = names;
13467
0
      s->udata.p = NULL;
13468
0
      len = strlen ((*p->sym_ptr_ptr)->name);
13469
0
      memcpy (names, (*p->sym_ptr_ptr)->name, len);
13470
0
      names += len;
13471
0
      if (p->addend != 0)
13472
0
  {
13473
0
    char buf[30], *a;
13474
13475
0
    memcpy (names, "+0x", sizeof ("+0x") - 1);
13476
0
    names += sizeof ("+0x") - 1;
13477
0
    bfd_sprintf_vma (abfd, buf, p->addend);
13478
0
    for (a = buf; *a == '0'; ++a)
13479
0
      ;
13480
0
    len = strlen (a);
13481
0
    memcpy (names, a, len);
13482
0
    names += len;
13483
0
  }
13484
0
      memcpy (names, "@plt", sizeof ("@plt"));
13485
0
      names += sizeof ("@plt");
13486
0
      ++s, ++n;
13487
0
    }
13488
13489
0
  return n;
13490
0
}
13491
13492
bool
13493
_bfd_elf_final_write_processing (bfd *abfd)
13494
152
{
13495
152
  Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd);
13496
152
  elf_backend_data *bed = get_elf_backend_data (abfd);
13497
13498
152
  if (bed->osabi_exact && i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE)
13499
0
    i_ehdrp->e_ident[EI_OSABI] = bed->elf_osabi;
13500
13501
152
  if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_SOLARIS
13502
149
      || bed->target_os == is_solaris)
13503
3
    {
13504
3
      elf_tdata (abfd)->strtab_hdr.sh_flags = SHF_STRINGS;
13505
3
      elf_tdata (abfd)->shstrtab_hdr.sh_flags = SHF_STRINGS;
13506
3
    }
13507
13508
  /* Set the osabi field to ELFOSABI_GNU if the binary contains
13509
     SHF_GNU_MBIND or SHF_GNU_RETAIN sections or symbols of STT_GNU_IFUNC type
13510
     or STB_GNU_UNIQUE binding.  */
13511
152
  if (elf_tdata (abfd)->has_gnu_osabi != 0)
13512
0
    {
13513
0
      if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE)
13514
0
  i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_GNU;
13515
0
      else if (i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_GNU
13516
0
         && i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_FREEBSD)
13517
0
  {
13518
0
    if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind)
13519
0
      _bfd_error_handler (_("GNU_MBIND section is supported only by GNU "
13520
0
          "and FreeBSD targets"));
13521
0
    if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_ifunc)
13522
0
      _bfd_error_handler (_("symbol type STT_GNU_IFUNC is supported "
13523
0
          "only by GNU and FreeBSD targets"));
13524
0
    if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_unique)
13525
0
      _bfd_error_handler (_("symbol binding STB_GNU_UNIQUE is supported "
13526
0
          "only by GNU and FreeBSD targets"));
13527
0
    if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_retain)
13528
0
      _bfd_error_handler (_("GNU_RETAIN section is supported "
13529
0
          "only by GNU and FreeBSD targets"));
13530
0
    bfd_set_error (bfd_error_sorry);
13531
0
    return false;
13532
0
  }
13533
0
    }
13534
152
  return true;
13535
152
}
13536
13537
13538
/* Return TRUE for ELF symbol types that represent functions.
13539
   This is the default version of this function, which is sufficient for
13540
   most targets.  It returns true if TYPE is STT_FUNC or STT_GNU_IFUNC.  */
13541
13542
bool
13543
_bfd_elf_is_function_type (unsigned int type)
13544
0
{
13545
0
  return (type == STT_FUNC
13546
0
    || type == STT_GNU_IFUNC);
13547
0
}
13548
13549
/* If the ELF symbol SYM might be a function in SEC, return the
13550
   function size and set *CODE_OFF to the function's entry point,
13551
   otherwise return zero.  */
13552
13553
bfd_size_type
13554
_bfd_elf_maybe_function_sym (const asymbol *sym, asection *sec,
13555
           bfd_vma *code_off)
13556
646k
{
13557
646k
  bfd_size_type size;
13558
646k
  elf_symbol_type * elf_sym = (elf_symbol_type *) sym;
13559
13560
646k
  if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
13561
646k
         | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0
13562
447k
      || sym->section != sec)
13563
635k
    return 0;
13564
13565
10.8k
  size = (sym->flags & BSF_SYNTHETIC) ? 0 : elf_sym->internal_elf_sym.st_size;
13566
13567
  /* In theory we should check that the symbol's type satisfies
13568
     _bfd_elf_is_function_type(), but there are some function-like
13569
     symbols which would fail this test.  (eg _start).  Instead
13570
     we check for hidden, local, notype symbols with zero size.
13571
     This type of symbol is generated by the annobin plugin for gcc
13572
     and clang, and should not be considered to be a function symbol.  */
13573
10.8k
  if (size == 0
13574
2.72k
      && ((sym->flags & (BSF_SYNTHETIC | BSF_LOCAL)) == BSF_LOCAL)
13575
2.00k
      && ELF_ST_TYPE (elf_sym->internal_elf_sym.st_info) == STT_NOTYPE
13576
1.90k
      && ELF_ST_VISIBILITY (elf_sym->internal_elf_sym.st_other) == STV_HIDDEN)
13577
137
    return 0;
13578
13579
10.7k
  *code_off = sym->value;
13580
  /* Do not return 0 for the function's size.  */
13581
10.7k
  return size ? size : 1;
13582
10.8k
}
13583
13584
/* Set to non-zero to enable some debug messages.  */
13585
#define DEBUG_SECONDARY_RELOCS   0
13586
13587
/* An internal-to-the-bfd-library only section type
13588
   used to indicate a cached secondary reloc section.  */
13589
4.66k
#define SHT_SECONDARY_RELOC  (SHT_LOOS + SHT_RELA)
13590
13591
/* Create a BFD section to hold a secondary reloc section.  */
13592
13593
bool
13594
_bfd_elf_init_secondary_reloc_section (bfd * abfd,
13595
               Elf_Internal_Shdr *hdr,
13596
               const char * name,
13597
               unsigned int shindex)
13598
1.40k
{
13599
  /* We only support RELA secondary relocs.  */
13600
1.40k
  if (hdr->sh_type != SHT_RELA)
13601
145
    return false;
13602
13603
#if DEBUG_SECONDARY_RELOCS
13604
  fprintf (stderr, "secondary reloc section %s encountered\n", name);
13605
#endif
13606
1.25k
  hdr->sh_type = SHT_SECONDARY_RELOC;
13607
1.25k
  return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
13608
1.40k
}
13609
13610
/* Read in any secondary relocs associated with SEC.  */
13611
13612
bool
13613
_bfd_elf_slurp_secondary_reloc_section (bfd *       abfd,
13614
          asection *  sec,
13615
          asymbol **  symbols,
13616
          bool dynamic)
13617
10.0k
{
13618
10.0k
  elf_backend_data *ebd = get_elf_backend_data (abfd);
13619
10.0k
  asection * relsec;
13620
10.0k
  bool result = true;
13621
10.0k
  bfd_vma (*r_sym) (bfd_vma);
13622
10.0k
  ufile_ptr filesize;
13623
13624
10.0k
#if BFD_DEFAULT_TARGET_SIZE > 32
13625
10.0k
  if (bfd_arch_bits_per_address (abfd) != 32)
13626
6.86k
    r_sym = elf64_r_sym;
13627
3.20k
  else
13628
3.20k
#endif
13629
3.20k
    r_sym = elf32_r_sym;
13630
13631
10.0k
  if (!elf_section_data (sec)->has_secondary_relocs)
13632
9.99k
    return true;
13633
13634
  /* Discover if there are any secondary reloc sections
13635
     associated with SEC.  */
13636
78
  filesize = bfd_get_file_size (abfd);
13637
1.77k
  for (relsec = abfd->sections; relsec != NULL; relsec = relsec->next)
13638
1.69k
    {
13639
1.69k
      Elf_Internal_Shdr * hdr = & elf_section_data (relsec)->this_hdr;
13640
13641
1.69k
      if (hdr->sh_type == SHT_SECONDARY_RELOC
13642
83
    && hdr->sh_info == (unsigned) elf_section_data (sec)->this_idx
13643
78
    && (hdr->sh_entsize == ebd->s->sizeof_rel
13644
78
        || hdr->sh_entsize == ebd->s->sizeof_rela))
13645
78
  {
13646
78
    bfd_byte * native_relocs;
13647
78
    bfd_byte * native_reloc;
13648
78
    arelent * internal_relocs;
13649
78
    arelent * internal_reloc;
13650
78
    size_t i;
13651
78
    unsigned int entsize;
13652
78
    unsigned int symcount;
13653
78
    bfd_size_type reloc_count;
13654
78
    size_t amt;
13655
13656
78
    if (ebd->elf_info_to_howto == NULL)
13657
0
      return false;
13658
13659
#if DEBUG_SECONDARY_RELOCS
13660
    fprintf (stderr, "read secondary relocs for %s from %s\n",
13661
       sec->name, relsec->name);
13662
#endif
13663
78
    entsize = hdr->sh_entsize;
13664
13665
78
    if (filesize != 0
13666
78
        && ((ufile_ptr) hdr->sh_offset > filesize
13667
71
      || hdr->sh_size > filesize - hdr->sh_offset))
13668
21
      {
13669
21
        bfd_set_error (bfd_error_file_truncated);
13670
21
        result = false;
13671
21
        continue;
13672
21
      }
13673
13674
57
    native_relocs = bfd_malloc (hdr->sh_size);
13675
57
    if (native_relocs == NULL)
13676
0
      {
13677
0
        result = false;
13678
0
        continue;
13679
0
      }
13680
13681
57
    reloc_count = NUM_SHDR_ENTRIES (hdr);
13682
57
    if (_bfd_mul_overflow (reloc_count, sizeof (arelent), & amt))
13683
0
      {
13684
0
        free (native_relocs);
13685
0
        bfd_set_error (bfd_error_file_too_big);
13686
0
        result = false;
13687
0
        continue;
13688
0
      }
13689
13690
57
    internal_relocs = (arelent *) bfd_alloc (abfd, amt);
13691
57
    if (internal_relocs == NULL)
13692
0
      {
13693
0
        free (native_relocs);
13694
0
        result = false;
13695
0
        continue;
13696
0
      }
13697
13698
57
    if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
13699
57
        || bfd_read (native_relocs, hdr->sh_size, abfd) != hdr->sh_size)
13700
0
      {
13701
0
        free (native_relocs);
13702
        /* The internal_relocs will be freed when
13703
     the memory for the bfd is released.  */
13704
0
        result = false;
13705
0
        continue;
13706
0
      }
13707
13708
57
    if (dynamic)
13709
0
      symcount = bfd_get_dynamic_symcount (abfd);
13710
57
    else
13711
57
      symcount = bfd_get_symcount (abfd);
13712
13713
57
    for (i = 0, internal_reloc = internal_relocs,
13714
57
     native_reloc = native_relocs;
13715
640
         i < reloc_count;
13716
583
         i++, internal_reloc++, native_reloc += entsize)
13717
583
      {
13718
583
        bool res;
13719
583
        Elf_Internal_Rela rela;
13720
13721
583
        if (entsize == ebd->s->sizeof_rel)
13722
0
    ebd->s->swap_reloc_in (abfd, native_reloc, & rela);
13723
583
        else /* entsize == ebd->s->sizeof_rela */
13724
583
    ebd->s->swap_reloca_in (abfd, native_reloc, & rela);
13725
13726
        /* The address of an ELF reloc is section relative for an object
13727
     file, and absolute for an executable file or shared library.
13728
     The address of a normal BFD reloc is always section relative,
13729
     and the address of a dynamic reloc is absolute..  */
13730
583
        if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0)
13731
552
    internal_reloc->address = rela.r_offset;
13732
31
        else
13733
31
    internal_reloc->address = rela.r_offset - sec->vma;
13734
13735
583
        if (r_sym (rela.r_info) == STN_UNDEF)
13736
49
    {
13737
      /* FIXME: This and the error case below mean that we
13738
         have a symbol on relocs that is not elf_symbol_type.  */
13739
49
      internal_reloc->sym_ptr_ptr = &bfd_abs_section_ptr->symbol;
13740
49
    }
13741
534
        else if (r_sym (rela.r_info) > symcount)
13742
0
    {
13743
0
      _bfd_error_handler
13744
        /* xgettext:c-format */
13745
0
        (_("%pB(%pA): relocation %zu has invalid symbol index %lu"),
13746
0
         abfd, sec, i, (long) r_sym (rela.r_info));
13747
0
      bfd_set_error (bfd_error_bad_value);
13748
0
      internal_reloc->sym_ptr_ptr = &bfd_abs_section_ptr->symbol;
13749
0
      result = false;
13750
0
    }
13751
534
        else
13752
534
    {
13753
534
      asymbol **ps;
13754
13755
534
      ps = symbols + r_sym (rela.r_info) - 1;
13756
534
      internal_reloc->sym_ptr_ptr = ps;
13757
      /* Make sure that this symbol is not removed by strip.  */
13758
534
      (*ps)->flags |= BSF_KEEP;
13759
534
    }
13760
13761
583
        internal_reloc->addend = rela.r_addend;
13762
13763
583
        res = ebd->elf_info_to_howto (abfd, internal_reloc, & rela);
13764
583
        if (! res || internal_reloc->howto == NULL)
13765
39
    {
13766
#if DEBUG_SECONDARY_RELOCS
13767
      fprintf (stderr,
13768
         "there is no howto associated with reloc %lx\n",
13769
         rela.r_info);
13770
#endif
13771
39
      result = false;
13772
39
    }
13773
583
      }
13774
13775
57
    free (native_relocs);
13776
    /* Store the internal relocs.  */
13777
57
    relsec->sec_info = internal_relocs;
13778
57
  }
13779
1.69k
    }
13780
13781
78
  return result;
13782
78
}
13783
13784
/* Set the ELF section header fields of an output secondary reloc section.  */
13785
13786
bool
13787
_bfd_elf_copy_special_section_fields (const bfd *ibfd ATTRIBUTE_UNUSED,
13788
              bfd *obfd ATTRIBUTE_UNUSED,
13789
              const Elf_Internal_Shdr *isection,
13790
              Elf_Internal_Shdr *osection)
13791
21
{
13792
21
  asection * isec;
13793
21
  asection * osec;
13794
21
  struct bfd_elf_section_data * esd;
13795
13796
21
  if (isection == NULL)
13797
0
    return false;
13798
13799
21
  if (isection->sh_type != SHT_SECONDARY_RELOC)
13800
20
    return true;
13801
13802
1
  isec = isection->bfd_section;
13803
1
  if (isec == NULL)
13804
0
    return false;
13805
13806
1
  osec = osection->bfd_section;
13807
1
  if (osec == NULL)
13808
0
    return false;
13809
13810
1
  BFD_ASSERT (osec->sec_info == NULL);
13811
1
  osec->sec_info = isec->sec_info;
13812
1
  osection->sh_type = SHT_RELA;
13813
1
  osection->sh_link = elf_onesymtab (obfd);
13814
1
  if (osection->sh_link == 0)
13815
0
    {
13816
      /* There is no symbol table - we are hosed...  */
13817
0
      _bfd_error_handler
13818
  /* xgettext:c-format */
13819
0
  (_("%pB(%pA): link section cannot be set"
13820
0
     " because the output file does not have a symbol table"),
13821
0
  obfd, osec);
13822
0
      bfd_set_error (bfd_error_bad_value);
13823
0
      return false;
13824
0
    }
13825
13826
  /* Find the output section that corresponds to the isection's
13827
     sh_info link.  */
13828
1
  if (isection->sh_info == 0
13829
1
      || isection->sh_info >= elf_numsections (ibfd))
13830
0
    {
13831
0
      _bfd_error_handler
13832
  /* xgettext:c-format */
13833
0
  (_("%pB(%pA): info section index is invalid"),
13834
0
  obfd, osec);
13835
0
      bfd_set_error (bfd_error_bad_value);
13836
0
      return false;
13837
0
    }
13838
13839
1
  isection = elf_elfsections (ibfd)[isection->sh_info];
13840
13841
1
  if (isection == NULL
13842
1
      || isection->bfd_section == NULL
13843
1
      || isection->bfd_section->output_section == NULL)
13844
0
    {
13845
0
      _bfd_error_handler
13846
  /* xgettext:c-format */
13847
0
  (_("%pB(%pA): info section index cannot be set"
13848
0
     " because the section is not in the output"),
13849
0
  obfd, osec);
13850
0
      bfd_set_error (bfd_error_bad_value);
13851
0
      return false;
13852
0
    }
13853
13854
1
  esd = elf_section_data (isection->bfd_section->output_section);
13855
1
  BFD_ASSERT (esd != NULL);
13856
1
  osection->sh_info = esd->this_idx;
13857
1
  esd->has_secondary_relocs = true;
13858
#if DEBUG_SECONDARY_RELOCS
13859
  fprintf (stderr, "update header of %s, sh_link = %u, sh_info = %u\n",
13860
     osec->name, osection->sh_link, osection->sh_info);
13861
  fprintf (stderr, "mark section %s as having secondary relocs\n",
13862
     bfd_section_name (isection->bfd_section->output_section));
13863
#endif
13864
13865
1
  return true;
13866
1
}
13867
13868
/* Write out a secondary reloc section.
13869
13870
   FIXME: Currently this function can result in a serious performance penalty
13871
   for files with secondary relocs and lots of sections.  The proper way to
13872
   fix this is for _bfd_elf_copy_special_section_fields() to chain secondary
13873
   relocs together and then to have this function just walk that chain.  */
13874
13875
bool
13876
_bfd_elf_write_secondary_reloc_section (bfd *abfd, asection *sec)
13877
0
{
13878
0
  elf_backend_data *ebd = get_elf_backend_data (abfd);
13879
0
  bfd_vma addr_offset;
13880
0
  asection * relsec;
13881
0
  bfd_vma (*r_info) (bfd_vma, bfd_vma);
13882
0
  bool result = true;
13883
13884
0
  if (sec == NULL)
13885
0
    return false;
13886
13887
0
#if BFD_DEFAULT_TARGET_SIZE > 32
13888
0
  if (bfd_arch_bits_per_address (abfd) != 32)
13889
0
    r_info = elf64_r_info;
13890
0
  else
13891
0
#endif
13892
0
    r_info = elf32_r_info;
13893
13894
  /* The address of an ELF reloc is section relative for an object
13895
     file, and absolute for an executable file or shared library.
13896
     The address of a BFD reloc is always section relative.  */
13897
0
  addr_offset = 0;
13898
0
  if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
13899
0
    addr_offset = sec->vma;
13900
13901
  /* Discover if there are any secondary reloc sections
13902
     associated with SEC.  */
13903
0
  for (relsec = abfd->sections; relsec != NULL; relsec = relsec->next)
13904
0
    {
13905
0
      const struct bfd_elf_section_data * const esd = elf_section_data (relsec);
13906
0
      Elf_Internal_Shdr * const hdr = (Elf_Internal_Shdr *) & esd->this_hdr;
13907
13908
0
      if (hdr->sh_type == SHT_RELA
13909
0
    && hdr->sh_info == (unsigned) elf_section_data (sec)->this_idx)
13910
0
  {
13911
0
    asymbol *    last_sym;
13912
0
    int          last_sym_idx;
13913
0
    size_t       reloc_count;
13914
0
    size_t       idx;
13915
0
    bfd_size_type entsize;
13916
0
    arelent *    src_irel;
13917
0
    bfd_byte *   dst_rela;
13918
13919
0
    if (hdr->contents != NULL)
13920
0
      {
13921
0
        _bfd_error_handler
13922
    /* xgettext:c-format */
13923
0
    (_("%pB(%pA): error: secondary reloc section processed twice"),
13924
0
     abfd, relsec);
13925
0
        bfd_set_error (bfd_error_bad_value);
13926
0
        result = false;
13927
0
        continue;
13928
0
      }
13929
13930
0
    entsize = hdr->sh_entsize;
13931
0
    if (entsize == 0)
13932
0
      {
13933
0
        _bfd_error_handler
13934
    /* xgettext:c-format */
13935
0
    (_("%pB(%pA): error: secondary reloc section"
13936
0
       " has zero sized entries"),
13937
0
     abfd, relsec);
13938
0
        bfd_set_error (bfd_error_bad_value);
13939
0
        result = false;
13940
0
        continue;
13941
0
      }
13942
0
    else if (entsize != ebd->s->sizeof_rel
13943
0
       && entsize != ebd->s->sizeof_rela)
13944
0
      {
13945
0
        _bfd_error_handler
13946
    /* xgettext:c-format */
13947
0
    (_("%pB(%pA): error: secondary reloc section"
13948
0
       " has non-standard sized entries"),
13949
0
     abfd, relsec);
13950
0
        bfd_set_error (bfd_error_bad_value);
13951
0
        result = false;
13952
0
        continue;
13953
0
      }
13954
13955
0
    reloc_count = hdr->sh_size / entsize;
13956
0
    hdr->sh_size = entsize * reloc_count;
13957
0
    if (reloc_count == 0)
13958
0
      {
13959
0
        _bfd_error_handler
13960
    /* xgettext:c-format */
13961
0
    (_("%pB(%pA): error: secondary reloc section is empty!"),
13962
0
     abfd, relsec);
13963
0
        bfd_set_error (bfd_error_bad_value);
13964
0
        result = false;
13965
0
        continue;
13966
0
      }
13967
13968
0
    hdr->contents = bfd_alloc (abfd, hdr->sh_size);
13969
0
    if (hdr->contents == NULL)
13970
0
      continue;
13971
0
    relsec->alloced = 1;
13972
13973
#if DEBUG_SECONDARY_RELOCS
13974
    fprintf (stderr, "write %u secondary relocs for %s from %s\n",
13975
       reloc_count, sec->name, relsec->name);
13976
#endif
13977
0
    last_sym = NULL;
13978
0
    last_sym_idx = 0;
13979
0
    dst_rela = hdr->contents;
13980
0
    src_irel = sec->sec_info;
13981
0
    if (src_irel == NULL)
13982
0
      {
13983
0
        _bfd_error_handler
13984
    /* xgettext:c-format */
13985
0
    (_("%pB(%pA): error: internal relocs missing"
13986
0
       " for secondary reloc section"),
13987
0
     abfd, relsec);
13988
0
        bfd_set_error (bfd_error_bad_value);
13989
0
        result = false;
13990
0
        continue;
13991
0
      }
13992
13993
0
    for (idx = 0; idx < reloc_count; idx++, dst_rela += entsize)
13994
0
      {
13995
0
        Elf_Internal_Rela src_rela;
13996
0
        arelent *ptr;
13997
0
        asymbol *sym;
13998
0
        int n;
13999
14000
0
        ptr = src_irel + idx;
14001
0
        if (ptr == NULL)
14002
0
    {
14003
0
      _bfd_error_handler
14004
        /* xgettext:c-format */
14005
0
        (_("%pB(%pA): error: reloc table entry %zu is empty"),
14006
0
         abfd, relsec, idx);
14007
0
      bfd_set_error (bfd_error_bad_value);
14008
0
      result = false;
14009
0
      break;
14010
0
    }
14011
14012
0
        if (ptr->sym_ptr_ptr == NULL)
14013
0
    {
14014
      /* FIXME: Is this an error ? */
14015
0
      n = 0;
14016
0
    }
14017
0
        else
14018
0
    {
14019
0
      sym = *ptr->sym_ptr_ptr;
14020
14021
0
      if (sym == last_sym)
14022
0
        n = last_sym_idx;
14023
0
      else
14024
0
        {
14025
0
          n = _bfd_elf_symbol_from_bfd_symbol (abfd, & sym);
14026
0
          if (n < 0)
14027
0
      {
14028
0
        _bfd_error_handler
14029
          /* xgettext:c-format */
14030
0
          (_("%pB(%pA): error: secondary reloc %zu"
14031
0
             " references a missing symbol"),
14032
0
           abfd, relsec, idx);
14033
0
        bfd_set_error (bfd_error_bad_value);
14034
0
        result = false;
14035
0
        n = 0;
14036
0
      }
14037
14038
0
          last_sym = sym;
14039
0
          last_sym_idx = n;
14040
0
        }
14041
14042
0
      if (sym->the_bfd != NULL
14043
0
          && sym->the_bfd->xvec != abfd->xvec
14044
0
          && ! _bfd_elf_validate_reloc (abfd, ptr))
14045
0
        {
14046
0
          _bfd_error_handler
14047
      /* xgettext:c-format */
14048
0
      (_("%pB(%pA): error: secondary reloc %zu"
14049
0
         " references a deleted symbol"),
14050
0
       abfd, relsec, idx);
14051
0
          bfd_set_error (bfd_error_bad_value);
14052
0
          result = false;
14053
0
          n = 0;
14054
0
        }
14055
0
    }
14056
14057
0
        src_rela.r_offset = ptr->address + addr_offset;
14058
0
        if (ptr->howto == NULL)
14059
0
    {
14060
0
      _bfd_error_handler
14061
        /* xgettext:c-format */
14062
0
        (_("%pB(%pA): error: secondary reloc %zu"
14063
0
           " is of an unknown type"),
14064
0
         abfd, relsec, idx);
14065
0
      bfd_set_error (bfd_error_bad_value);
14066
0
      result = false;
14067
0
      src_rela.r_info = r_info (0, 0);
14068
0
    }
14069
0
        else
14070
0
    src_rela.r_info = r_info (n, ptr->howto->type);
14071
0
        src_rela.r_addend = ptr->addend;
14072
14073
0
        if (entsize == ebd->s->sizeof_rel)
14074
0
    ebd->s->swap_reloc_out (abfd, &src_rela, dst_rela);
14075
0
        else /* entsize == ebd->s->sizeof_rela */
14076
0
    ebd->s->swap_reloca_out (abfd, &src_rela, dst_rela);
14077
0
      }
14078
0
  }
14079
0
    }
14080
14081
0
  return result;
14082
0
}
14083
14084
/* Mmap in section contents.  */
14085
14086
static bool
14087
elf_mmap_section_contents (bfd *abfd, sec_ptr sec, bfd_byte **buf)
14088
7.00k
{
14089
7.00k
#ifdef USE_MMAP
14090
7.00k
  elf_backend_data *bed = get_elf_backend_data (abfd);
14091
7.00k
  if (bed->use_mmap
14092
3.33k
      && sec->compress_status == COMPRESS_SECTION_NONE
14093
3.33k
      && (sec->flags & SEC_LINKER_CREATED) == 0)
14094
3.33k
    {
14095
      /* Use mmap only if section size >= the minimum mmap section
14096
   size.  */
14097
3.33k
      size_t readsz = bfd_get_section_limit_octets (abfd, sec);
14098
3.33k
      size_t allocsz = bfd_get_section_alloc_size (abfd, sec);
14099
3.33k
      if (readsz == allocsz && readsz >= _bfd_minimum_mmap_size)
14100
554
  {
14101
554
    if (sec->contents != NULL)
14102
0
      {
14103
0
        if (!sec->mmapped_p)
14104
0
    abort ();
14105
0
        *buf = sec->contents;
14106
0
        return true;
14107
0
      }
14108
554
    if (sec->mmapped_p)
14109
0
      abort ();
14110
554
    sec->mmapped_p = 1;
14111
14112
    /* We can't use the final link preallocated buffer for mmap.  */
14113
554
    *buf = NULL;
14114
554
  }
14115
3.33k
    }
14116
7.00k
#endif
14117
  /* FIXME: We should not get here if sec->alloced is set.  */
14118
7.00k
  bool ret = bfd_get_full_section_contents (abfd, sec, buf);
14119
7.00k
  if (ret && sec->mmapped_p)
14120
0
    *buf = sec->contents;
14121
7.00k
  return ret;
14122
7.00k
}
14123
14124
/* Mmap in section contents.  */
14125
14126
bool
14127
_bfd_elf_mmap_section_contents (bfd *abfd, sec_ptr sec, bfd_byte **buf)
14128
7.00k
{
14129
7.00k
  *buf = NULL;
14130
7.00k
  return elf_mmap_section_contents (abfd, sec, buf);
14131
7.00k
}
14132
14133
/* Mmap in the full section contents for the final link.  */
14134
14135
bool
14136
_bfd_elf_link_mmap_section_contents (bfd *abfd, sec_ptr sec,
14137
             bfd_byte **buf)
14138
0
{
14139
0
  return elf_mmap_section_contents (abfd, sec, buf);
14140
0
}
14141
14142
/* Munmap section contents.  */
14143
14144
void
14145
_bfd_elf_munmap_section_contents (asection *sec, void *contents)
14146
338k
{
14147
  /* NB: Since _bfd_elf_munmap_section_contents is called like free,
14148
     CONTENTS may be NULL.  */
14149
338k
  if (contents == NULL)
14150
332k
    return;
14151
14152
6.15k
  if (sec->alloced
14153
      /* What a tangled web we weave with section contents.
14154
   FIXME: We shouldn't need to test anything but sec->alloced
14155
   here, but there are cases where a buffer is allocated for a
14156
   section but then another buffer is malloc'd anyway.  eg.
14157
   trace through ld-elf/eh4 testcase on x86_64.  */
14158
436
      && (sec->contents == contents
14159
0
    || elf_section_data (sec)->this_hdr.contents == contents))
14160
436
    return;
14161
14162
  /* Don't leave pointers to data we are about to munmap or free.  */
14163
5.71k
  if (sec->contents == contents)
14164
0
    sec->contents = NULL;
14165
5.71k
  if (elf_section_data (sec)->this_hdr.contents == contents)
14166
0
    elf_section_data (sec)->this_hdr.contents = NULL;
14167
14168
5.71k
#ifdef USE_MMAP
14169
5.71k
  if (sec->mmapped_p)
14170
0
    {
14171
      /* When _bfd_elf_mmap_section_contents returns CONTENTS as
14172
   malloced, CONTENTS_ADDR is set to NULL.  */
14173
0
      if (elf_section_data (sec)->contents_addr != NULL)
14174
0
  {
14175
    /* NB: CONTENTS_ADDR and CONTENTS_SIZE must be valid.  */
14176
0
    if (munmap (elf_section_data (sec)->contents_addr,
14177
0
          elf_section_data (sec)->contents_size) != 0)
14178
0
      abort ();
14179
0
    sec->mmapped_p = 0;
14180
0
    elf_section_data (sec)->contents_addr = NULL;
14181
0
    elf_section_data (sec)->contents_size = 0;
14182
0
    return;
14183
0
  }
14184
0
    }
14185
5.71k
#endif
14186
14187
5.71k
  free (contents);
14188
5.71k
}
14189
14190
/* Munmap the full section contents for the final link.  */
14191
14192
void
14193
_bfd_elf_link_munmap_section_contents (asection *sec ATTRIBUTE_UNUSED)
14194
0
{
14195
0
#ifdef USE_MMAP
14196
0
  if (sec->mmapped_p && elf_section_data (sec)->contents_addr != NULL)
14197
0
    {
14198
      /* When _bfd_elf_link_mmap_section_contents returns CONTENTS as
14199
   malloced, CONTENTS_ADDR is set to NULL.  */
14200
      /* NB: CONTENTS_ADDR and CONTENTS_SIZE must be valid.  */
14201
0
      if (munmap (elf_section_data (sec)->contents_addr,
14202
0
      elf_section_data (sec)->contents_size) != 0)
14203
0
  abort ();
14204
0
      sec->mmapped_p = 0;
14205
0
      sec->contents = NULL;
14206
0
      elf_section_data (sec)->this_hdr.contents = NULL;
14207
0
      elf_section_data (sec)->contents_addr = NULL;
14208
0
      elf_section_data (sec)->contents_size = 0;
14209
0
    }
14210
0
#endif
14211
0
}
14212
14213
#ifndef OBJ_MAYBE_ELF_ATTRIBUTES
14214
14215
bfd *_bfd_elf_link_setup_object_attributes
14216
  (struct bfd_link_info *info ATTRIBUTE_UNUSED)
14217
{ return NULL; }
14218
14219
#endif /* !OBJ_MAYBE_ELF_ATTRIBUTES */