Coverage Report

Created: 2026-11-21 09:00

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
68.2k
#define streq(A,B) (strcmp ((A), (B)) == 0)
56
#endif
57
58
/* Core note names.  */
59
0
#define NOTE_NAME_CORE     "CORE"
60
0
#define NOTE_NAME_FREEBSD  "FreeBSD"
61
0
#define NOTE_NAME_GDB      "GDB"
62
0
#define NOTE_NAME_LINUX    "LINUX"
63
64
/* Names of a pseudo-section which represent core notes.  */
65
0
#define NOTE_PSEUDO_SECTION_AARCH_FPMR    ".reg-aarch-fpmr"
66
0
#define NOTE_PSEUDO_SECTION_AARCH_GCS   ".reg-aarch-gcs"
67
0
#define NOTE_PSEUDO_SECTION_AARCH_POE   ".reg-aarch-poe"
68
0
#define NOTE_PSEUDO_SECTION_AARCH_HW_BREAK  ".reg-aarch-hw-break"
69
0
#define NOTE_PSEUDO_SECTION_AARCH_HW_WATCH  ".reg-aarch-hw-watch"
70
0
#define NOTE_PSEUDO_SECTION_AARCH_MTE   ".reg-aarch-mte"
71
0
#define NOTE_PSEUDO_SECTION_AARCH_PAUTH   ".reg-aarch-pauth"
72
0
#define NOTE_PSEUDO_SECTION_AARCH_SSVE    ".reg-aarch-ssve"
73
0
#define NOTE_PSEUDO_SECTION_AARCH_SVE   ".reg-aarch-sve"
74
0
#define NOTE_PSEUDO_SECTION_AARCH_TLS   ".reg-aarch-tls"
75
0
#define NOTE_PSEUDO_SECTION_AARCH_ZA    ".reg-aarch-za"
76
0
#define NOTE_PSEUDO_SECTION_AARCH_ZT    ".reg-aarch-zt"
77
0
#define NOTE_PSEUDO_SECTION_ARC_V2    ".reg-arc-v2"
78
0
#define NOTE_PSEUDO_SECTION_ARM_VFP   ".reg-arm-vfp"
79
0
#define NOTE_PSEUDO_SECTION_I386_TLS    ".reg-i386-tls"
80
0
#define NOTE_PSEUDO_SECTION_LOONGARCH_CPUCFG  ".reg-loongarch-cpucfg"
81
0
#define NOTE_PSEUDO_SECTION_LOONGARCH_LASX  ".reg-loongarch-lasx"
82
0
#define NOTE_PSEUDO_SECTION_LOONGARCH_LBT ".reg-loongarch-lbt"
83
0
#define NOTE_PSEUDO_SECTION_LOONGARCH_LSX ".reg-loongarch-lsx"
84
0
#define NOTE_PSEUDO_SECTION_PPC_DMR   ".reg-ppc-dmr"
85
0
#define NOTE_PSEUDO_SECTION_PPC_DSCR    ".reg-ppc-dscr"
86
0
#define NOTE_PSEUDO_SECTION_PPC_EBB   ".reg-ppc-ebb"
87
0
#define NOTE_PSEUDO_SECTION_PPC_PMU   ".reg-ppc-pmu"
88
0
#define NOTE_PSEUDO_SECTION_PPC_PPR   ".reg-ppc-ppr"
89
0
#define NOTE_PSEUDO_SECTION_PPC_TAR   ".reg-ppc-tar"
90
0
#define NOTE_PSEUDO_SECTION_PPC_TM_CDSCR  ".reg-ppc-tm-cdscr"
91
0
#define NOTE_PSEUDO_SECTION_PPC_TM_CFPR   ".reg-ppc-tm-cfpr"
92
0
#define NOTE_PSEUDO_SECTION_PPC_TM_CGPR   ".reg-ppc-tm-cgpr"
93
0
#define NOTE_PSEUDO_SECTION_PPC_TM_CPPR   ".reg-ppc-tm-cppr"
94
0
#define NOTE_PSEUDO_SECTION_PPC_TM_CTAR   ".reg-ppc-tm-ctar"
95
0
#define NOTE_PSEUDO_SECTION_PPC_TM_CVMX   ".reg-ppc-tm-cvmx"
96
0
#define NOTE_PSEUDO_SECTION_PPC_TM_CVSX   ".reg-ppc-tm-cvsx"
97
0
#define NOTE_PSEUDO_SECTION_PPC_TM_SPR    ".reg-ppc-tm-spr"
98
0
#define NOTE_PSEUDO_SECTION_PPC_VSX   ".reg-ppc-vsx"
99
0
#define NOTE_PSEUDO_SECTION_PPC_VMX   ".reg-ppc-vmx"
100
16
#define NOTE_PSEUDO_SECTION_REG     ".reg"
101
233
#define NOTE_PSEUDO_SECTION_REG2    ".reg2"
102
0
#define NOTE_PSEUDO_SECTION_RISCV_CSR   ".reg-riscv-csr"
103
0
#define NOTE_PSEUDO_SECTION_S390_CTRS   ".reg-s390-ctrs"
104
0
#define NOTE_PSEUDO_SECTION_S390_GS_BC    ".reg-s390-gs-bc"
105
0
#define NOTE_PSEUDO_SECTION_S390_GS_CB    ".reg-s390-gs-cb"
106
0
#define NOTE_PSEUDO_SECTION_S390_HIGH_GPRS  ".reg-s390-high-gprs"
107
0
#define NOTE_PSEUDO_SECTION_S390_LAST_BREAK ".reg-s390-last-break"
108
0
#define NOTE_PSEUDO_SECTION_S390_PREFIX   ".reg-s390-prefix"
109
0
#define NOTE_PSEUDO_SECTION_S390_SYSTEM_CALL  ".reg-s390-system-call"
110
0
#define NOTE_PSEUDO_SECTION_S390_TDB    ".reg-s390-tdb"
111
0
#define NOTE_PSEUDO_SECTION_S390_TIMER    ".reg-s390-timer"
112
0
#define NOTE_PSEUDO_SECTION_S390_TODCMP   ".reg-s390-todcmp"
113
0
#define NOTE_PSEUDO_SECTION_S390_TODPREG  ".reg-s390-todpreg"
114
0
#define NOTE_PSEUDO_SECTION_S390_VXRS_HIGH  ".reg-s390-vxrs-high"
115
0
#define NOTE_PSEUDO_SECTION_S390_VXRS_LOW ".reg-s390-vxrs-low"
116
0
#define NOTE_PSEUDO_SECTION_SSP     ".reg-ssp"
117
0
#define NOTE_PSEUDO_SECTION_TDESC   ".gdb-tdesc"
118
0
#define NOTE_PSEUDO_SECTION_X86_SEGBASES  ".reg-x86-segbases"
119
0
#define NOTE_PSEUDO_SECTION_XFP           ".reg-xfp"
120
0
#define NOTE_PSEUDO_SECTION_XSTATE        ".reg-xstate"
121
0
#define NOTE_PSEUDO_SECTION_XSAVE_LAYOUT  ".reg-xsave-layout"
122
123
static int elf_sort_sections (const void *, const void *);
124
static bool assign_file_positions_except_relocs (bfd *, struct bfd_link_info *);
125
static bool swap_out_syms (bfd *, struct elf_strtab_hash **, int,
126
         struct bfd_link_info *);
127
static bool elf_parse_notes (bfd *abfd, char *buf, size_t size,
128
           file_ptr offset, size_t align);
129
130
/* Swap version information in and out.  The version information is
131
   currently size independent.  If that ever changes, this code will
132
   need to move into elfcode.h.  */
133
134
/* Swap in a Verdef structure.  */
135
136
void
137
_bfd_elf_swap_verdef_in (bfd *abfd,
138
       const Elf_External_Verdef *src,
139
       Elf_Internal_Verdef *dst)
140
81
{
141
81
  dst->vd_version = H_GET_16 (abfd, src->vd_version);
142
81
  dst->vd_flags   = H_GET_16 (abfd, src->vd_flags);
143
81
  dst->vd_ndx     = H_GET_16 (abfd, src->vd_ndx);
144
81
  dst->vd_cnt     = H_GET_16 (abfd, src->vd_cnt);
145
81
  dst->vd_hash    = H_GET_32 (abfd, src->vd_hash);
146
81
  dst->vd_aux     = H_GET_32 (abfd, src->vd_aux);
147
81
  dst->vd_next    = H_GET_32 (abfd, src->vd_next);
148
81
}
149
150
/* Swap out a Verdef structure.  */
151
152
void
153
_bfd_elf_swap_verdef_out (bfd *abfd,
154
        const Elf_Internal_Verdef *src,
155
        Elf_External_Verdef *dst)
156
0
{
157
0
  H_PUT_16 (abfd, src->vd_version, dst->vd_version);
158
0
  H_PUT_16 (abfd, src->vd_flags, dst->vd_flags);
159
0
  H_PUT_16 (abfd, src->vd_ndx, dst->vd_ndx);
160
0
  H_PUT_16 (abfd, src->vd_cnt, dst->vd_cnt);
161
0
  H_PUT_32 (abfd, src->vd_hash, dst->vd_hash);
162
0
  H_PUT_32 (abfd, src->vd_aux, dst->vd_aux);
163
0
  H_PUT_32 (abfd, src->vd_next, dst->vd_next);
164
0
}
165
166
/* Swap in a Verdaux structure.  */
167
168
void
169
_bfd_elf_swap_verdaux_in (bfd *abfd,
170
        const Elf_External_Verdaux *src,
171
        Elf_Internal_Verdaux *dst)
172
8
{
173
8
  dst->vda_name = H_GET_32 (abfd, src->vda_name);
174
8
  dst->vda_next = H_GET_32 (abfd, src->vda_next);
175
8
}
176
177
/* Swap out a Verdaux structure.  */
178
179
void
180
_bfd_elf_swap_verdaux_out (bfd *abfd,
181
         const Elf_Internal_Verdaux *src,
182
         Elf_External_Verdaux *dst)
183
0
{
184
0
  H_PUT_32 (abfd, src->vda_name, dst->vda_name);
185
0
  H_PUT_32 (abfd, src->vda_next, dst->vda_next);
186
0
}
187
188
/* Swap in a Verneed structure.  */
189
190
void
191
_bfd_elf_swap_verneed_in (bfd *abfd,
192
        const Elf_External_Verneed *src,
193
        Elf_Internal_Verneed *dst)
194
218
{
195
218
  dst->vn_version = H_GET_16 (abfd, src->vn_version);
196
218
  dst->vn_cnt     = H_GET_16 (abfd, src->vn_cnt);
197
218
  dst->vn_file    = H_GET_32 (abfd, src->vn_file);
198
218
  dst->vn_aux     = H_GET_32 (abfd, src->vn_aux);
199
218
  dst->vn_next    = H_GET_32 (abfd, src->vn_next);
200
218
}
201
202
/* Swap out a Verneed structure.  */
203
204
void
205
_bfd_elf_swap_verneed_out (bfd *abfd,
206
         const Elf_Internal_Verneed *src,
207
         Elf_External_Verneed *dst)
208
0
{
209
0
  H_PUT_16 (abfd, src->vn_version, dst->vn_version);
210
0
  H_PUT_16 (abfd, src->vn_cnt, dst->vn_cnt);
211
0
  H_PUT_32 (abfd, src->vn_file, dst->vn_file);
212
0
  H_PUT_32 (abfd, src->vn_aux, dst->vn_aux);
213
0
  H_PUT_32 (abfd, src->vn_next, dst->vn_next);
214
0
}
215
216
/* Swap in a Vernaux structure.  */
217
218
void
219
_bfd_elf_swap_vernaux_in (bfd *abfd,
220
        const Elf_External_Vernaux *src,
221
        Elf_Internal_Vernaux *dst)
222
140
{
223
140
  dst->vna_hash  = H_GET_32 (abfd, src->vna_hash);
224
140
  dst->vna_flags = H_GET_16 (abfd, src->vna_flags);
225
140
  dst->vna_other = H_GET_16 (abfd, src->vna_other);
226
140
  dst->vna_name  = H_GET_32 (abfd, src->vna_name);
227
140
  dst->vna_next  = H_GET_32 (abfd, src->vna_next);
228
140
}
229
230
/* Swap out a Vernaux structure.  */
231
232
void
233
_bfd_elf_swap_vernaux_out (bfd *abfd,
234
         const Elf_Internal_Vernaux *src,
235
         Elf_External_Vernaux *dst)
236
0
{
237
0
  H_PUT_32 (abfd, src->vna_hash, dst->vna_hash);
238
0
  H_PUT_16 (abfd, src->vna_flags, dst->vna_flags);
239
0
  H_PUT_16 (abfd, src->vna_other, dst->vna_other);
240
0
  H_PUT_32 (abfd, src->vna_name, dst->vna_name);
241
0
  H_PUT_32 (abfd, src->vna_next, dst->vna_next);
242
0
}
243
244
/* Swap in a Versym structure.  */
245
246
void
247
_bfd_elf_swap_versym_in (bfd *abfd,
248
       const Elf_External_Versym *src,
249
       Elf_Internal_Versym *dst)
250
700
{
251
700
  dst->vs_vers = H_GET_16 (abfd, src->vs_vers);
252
700
}
253
254
/* Swap out a Versym structure.  */
255
256
void
257
_bfd_elf_swap_versym_out (bfd *abfd,
258
        const Elf_Internal_Versym *src,
259
        Elf_External_Versym *dst)
260
0
{
261
0
  H_PUT_16 (abfd, src->vs_vers, dst->vs_vers);
262
0
}
263
264
/* Standard ELF hash function.  Do not change this function; you will
265
   cause invalid hash tables to be generated.  */
266
267
unsigned long
268
bfd_elf_hash (const char *namearg)
269
0
{
270
0
  uint32_t h = 0;
271
272
0
  for (const unsigned char *name = (const unsigned char *) namearg;
273
0
       *name; name++)
274
0
    {
275
0
      h = (h << 4) + *name;
276
0
      h ^= (h >> 24) & 0xf0;
277
0
    }
278
0
  return h & 0x0fffffff;
279
0
}
280
281
/* DT_GNU_HASH hash function.  Do not change this function; you will
282
   cause invalid hash tables to be generated.  */
283
284
unsigned long
285
bfd_elf_gnu_hash (const char *namearg)
286
0
{
287
0
  uint32_t h = 5381;
288
289
0
  for (const unsigned char *name = (const unsigned char *) namearg;
290
0
       *name; name++)
291
0
    h = (h << 5) + h + *name;
292
0
  return h;
293
0
}
294
295
/* Create a tdata field OBJECT_SIZE bytes in length, zeroed out and with
296
   the object_id field of an elf_obj_tdata field set.  */
297
bool
298
bfd_elf_allocate_object (bfd *abfd,
299
       size_t object_size)
300
3.56M
{
301
3.56M
  BFD_ASSERT (object_size >= sizeof (struct elf_obj_tdata));
302
3.56M
  abfd->tdata.any = bfd_zalloc (abfd, object_size);
303
3.56M
  if (abfd->tdata.any == NULL)
304
0
    return false;
305
306
3.56M
  elf_object_id (abfd) = get_elf_backend_data (abfd)->target_id;
307
3.56M
  if (abfd->direction != read_direction)
308
1.51k
    {
309
1.51k
      struct output_elf_obj_tdata *o = bfd_zalloc (abfd, sizeof *o);
310
1.51k
      if (o == NULL)
311
0
  return false;
312
1.51k
      elf_tdata (abfd)->o = o;
313
1.51k
      elf_program_header_size (abfd) = (bfd_size_type) -1;
314
1.51k
    }
315
3.56M
  return true;
316
3.56M
}
317
318
319
bool
320
bfd_elf_make_object (bfd *abfd)
321
1.75M
{
322
1.75M
  return bfd_elf_allocate_object (abfd, sizeof (struct elf_obj_tdata));
323
1.75M
}
324
325
bool
326
bfd_elf_mkcorefile (bfd *abfd)
327
1.04M
{
328
  /* I think this can be done just like an object file.  */
329
1.04M
  if (!abfd->xvec->_bfd_set_format[(int) bfd_object] (abfd))
330
0
    return false;
331
1.04M
  elf_tdata (abfd)->core = bfd_zalloc (abfd, sizeof (*elf_tdata (abfd)->core));
332
1.04M
  return elf_tdata (abfd)->core != NULL;
333
1.04M
}
334
335
char *
336
bfd_elf_get_str_section (bfd *abfd, unsigned int shindex)
337
74.3k
{
338
74.3k
  Elf_Internal_Shdr **i_shdrp;
339
74.3k
  bfd_byte *shstrtab = NULL;
340
74.3k
  file_ptr offset;
341
74.3k
  bfd_size_type shstrtabsize;
342
343
74.3k
  i_shdrp = elf_elfsections (abfd);
344
74.3k
  if (i_shdrp == 0
345
74.3k
      || shindex >= elf_numsections (abfd)
346
74.3k
      || i_shdrp[shindex] == 0)
347
0
    return NULL;
348
349
74.3k
  shstrtab = i_shdrp[shindex]->contents;
350
74.3k
  if (shstrtab == NULL)
351
74.3k
    {
352
      /* No cached one, attempt to read, and cache what we read.  */
353
74.3k
      offset = i_shdrp[shindex]->sh_offset;
354
74.3k
      shstrtabsize = i_shdrp[shindex]->sh_size;
355
356
74.3k
      if (shstrtabsize == 0
357
66.0k
    || bfd_seek (abfd, offset, SEEK_SET) != 0
358
65.6k
    || (shstrtab = _bfd_mmap_persistent (abfd, shstrtabsize)) == NULL)
359
11.9k
  {
360
    /* Once we've failed to read it, make sure we don't keep
361
       trying.  Otherwise, we'll keep allocating space for
362
       the string table over and over.  */
363
11.9k
    i_shdrp[shindex]->sh_size = 0;
364
11.9k
  }
365
62.4k
      else if (shstrtab[shstrtabsize - 1] != 0)
366
19.8k
  {
367
    /* It is an error if a string table isn't terminated.  */
368
19.8k
    _bfd_error_handler
369
      /* xgettext:c-format */
370
19.8k
      (_("%pB: string table [%u] is corrupt"), abfd, shindex);
371
19.8k
    shstrtab[shstrtabsize - 1] = 0;
372
19.8k
  }
373
74.3k
      i_shdrp[shindex]->contents = shstrtab;
374
74.3k
    }
375
74.3k
  return (char *) shstrtab;
376
74.3k
}
377
378
char *
379
bfd_elf_string_from_elf_section (bfd *abfd,
380
         unsigned int shindex,
381
         unsigned int strindex)
382
1.23M
{
383
1.23M
  Elf_Internal_Shdr *hdr;
384
385
1.23M
  if (strindex == 0)
386
221k
    return "";
387
388
1.01M
  if (elf_elfsections (abfd) == NULL || shindex >= elf_numsections (abfd))
389
595
    return NULL;
390
391
1.01M
  hdr = elf_elfsections (abfd)[shindex];
392
393
1.01M
  if (hdr->contents == NULL)
394
95.4k
    {
395
95.4k
      if (hdr->sh_type != SHT_STRTAB && hdr->sh_type < SHT_LOOS)
396
21.1k
  {
397
    /* PR 17512: file: f057ec89.  */
398
    /* xgettext:c-format */
399
21.1k
    _bfd_error_handler (_("%pB: attempt to load strings from"
400
21.1k
        " a non-string section (number %d)"),
401
21.1k
            abfd, shindex);
402
21.1k
    return NULL;
403
21.1k
  }
404
405
74.3k
      if (bfd_elf_get_str_section (abfd, shindex) == NULL)
406
11.9k
  return NULL;
407
74.3k
    }
408
917k
  else
409
917k
    {
410
      /* PR 24273: The string section's contents may have already
411
   been loaded elsewhere, eg because a corrupt file has the
412
   string section index in the ELF header pointing at a group
413
   section.  So be paranoid, and test that the last byte of
414
   the section is zero.  */
415
917k
      if (hdr->sh_size == 0 || hdr->contents[hdr->sh_size - 1] != 0)
416
0
  return NULL;
417
917k
    }
418
419
979k
  if (strindex >= hdr->sh_size)
420
29.6k
    {
421
29.6k
      unsigned int shstrndx = elf_elfheader(abfd)->e_shstrndx;
422
29.6k
      _bfd_error_handler
423
  /* xgettext:c-format */
424
29.6k
  (_("%pB: invalid string offset %u >= %" PRIu64 " for section `%s'"),
425
29.6k
   abfd, strindex, (uint64_t) hdr->sh_size,
426
29.6k
   (shindex == shstrndx && strindex == hdr->sh_name
427
29.6k
    ? ".shstrtab"
428
29.6k
    : bfd_elf_string_from_elf_section (abfd, shstrndx, hdr->sh_name)));
429
29.6k
      return NULL;
430
29.6k
    }
431
432
950k
  return ((char *) hdr->contents) + strindex;
433
979k
}
434
435
/* Read and convert symbols to internal format.
436
   SYMCOUNT specifies the number of symbols to read, starting from
437
   symbol SYMOFFSET.  If any of INTSYM_BUF, EXTSYM_BUF or EXTSHNDX_BUF
438
   are non-NULL, they are used to store the internal symbols, external
439
   symbols, and symbol section index extensions, respectively.
440
   Returns a pointer to the internal symbol buffer (malloced if necessary)
441
   or NULL if there were no symbols or some kind of problem.  */
442
443
Elf_Internal_Sym *
444
bfd_elf_get_elf_syms (bfd *ibfd,
445
          Elf_Internal_Shdr *symtab_hdr,
446
          size_t symcount,
447
          size_t symoffset,
448
          Elf_Internal_Sym *intsym_buf,
449
          void *extsym_buf,
450
          Elf_External_Sym_Shndx *extshndx_buf)
451
17.1k
{
452
17.1k
  Elf_Internal_Shdr *shndx_hdr;
453
17.1k
  void *alloc_ext;
454
17.1k
  const bfd_byte *esym;
455
17.1k
  Elf_External_Sym_Shndx *alloc_extshndx;
456
17.1k
  Elf_External_Sym_Shndx *shndx;
457
17.1k
  Elf_Internal_Sym *alloc_intsym;
458
17.1k
  Elf_Internal_Sym *isym;
459
17.1k
  Elf_Internal_Sym *isymend;
460
17.1k
  elf_backend_data *bed;
461
17.1k
  size_t extsym_size;
462
17.1k
  size_t amt;
463
17.1k
  file_ptr pos;
464
465
17.1k
  if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
466
0
    abort ();
467
468
17.1k
  if (symcount == 0)
469
0
    return intsym_buf;
470
471
17.1k
  if (elf_use_dt_symtab_p (ibfd))
472
3
    {
473
      /* Use dynamic symbol table.  */
474
3
      if (symoffset >= elf_tdata (ibfd)->dt_symtab_count
475
3
    || symcount > elf_tdata (ibfd)->dt_symtab_count - symoffset)
476
0
  {
477
0
    bfd_set_error (bfd_error_invalid_operation);
478
0
    return NULL;
479
0
  }
480
3
      return elf_tdata (ibfd)->dt_symtab + symoffset;
481
3
    }
482
483
  /* Normal syms might have section extension entries.  */
484
17.1k
  shndx_hdr = NULL;
485
17.1k
  if (elf_symtab_shndx_list (ibfd) != NULL)
486
2.91k
    {
487
2.91k
      elf_section_list * entry;
488
2.91k
      Elf_Internal_Shdr **sections = elf_elfsections (ibfd);
489
490
      /* Find an index section that is linked to this symtab section.  */
491
4.89k
      for (entry = elf_symtab_shndx_list (ibfd); entry != NULL; entry = entry->next)
492
3.22k
  {
493
    /* PR 20063.  */
494
3.22k
    if (entry->hdr.sh_link >= elf_numsections (ibfd))
495
65
      continue;
496
497
3.15k
    if (sections[entry->hdr.sh_link] == symtab_hdr)
498
1.24k
      {
499
1.24k
        shndx_hdr = & entry->hdr;
500
1.24k
        break;
501
1.91k
      };
502
1.91k
  }
503
504
2.91k
      if (shndx_hdr == NULL)
505
1.67k
  {
506
1.67k
    if (symtab_hdr == &elf_symtab_hdr (ibfd))
507
      /* Not really accurate, but this was how the old code used
508
         to work.  */
509
1.67k
      shndx_hdr = &elf_symtab_shndx_list (ibfd)->hdr;
510
    /* Otherwise we do nothing.  The assumption is that
511
       the index table will not be needed.  */
512
1.67k
  }
513
2.91k
    }
514
515
  /* Read the symbols.  */
516
17.1k
  alloc_ext = NULL;
517
17.1k
  alloc_extshndx = NULL;
518
17.1k
  alloc_intsym = NULL;
519
17.1k
  bed = get_elf_backend_data (ibfd);
520
17.1k
  extsym_size = bed->s->sizeof_sym;
521
17.1k
  if (_bfd_mul_overflow (symcount, extsym_size, &amt))
522
0
    {
523
0
      bfd_set_error (bfd_error_file_too_big);
524
0
      return NULL;
525
0
    }
526
17.1k
  pos = symtab_hdr->sh_offset + symoffset * extsym_size;
527
17.1k
  size_t alloc_ext_size = amt;
528
17.1k
  if (bfd_seek (ibfd, pos, SEEK_SET) != 0
529
16.8k
      || !_bfd_mmap_read_temporary (&extsym_buf, &alloc_ext_size,
530
16.8k
            &alloc_ext, ibfd, false))
531
1.26k
    {
532
1.26k
      intsym_buf = NULL;
533
1.26k
      goto out2;
534
1.26k
    }
535
536
15.9k
  size_t alloc_extshndx_size = 0;
537
15.9k
  if (shndx_hdr == NULL || shndx_hdr->sh_size == 0)
538
13.8k
    extshndx_buf = NULL;
539
2.08k
  else
540
2.08k
    {
541
2.08k
      if (_bfd_mul_overflow (symcount, sizeof (Elf_External_Sym_Shndx), &amt))
542
0
  {
543
0
    bfd_set_error (bfd_error_file_too_big);
544
0
    intsym_buf = NULL;
545
0
    goto out1;
546
0
  }
547
2.08k
      alloc_extshndx_size = amt;
548
2.08k
      pos = shndx_hdr->sh_offset + symoffset * sizeof (Elf_External_Sym_Shndx);
549
2.08k
      if (bfd_seek (ibfd, pos, SEEK_SET) != 0
550
1.89k
    || !_bfd_mmap_read_temporary ((void **) &extshndx_buf,
551
1.89k
          &alloc_extshndx_size,
552
1.89k
          (void **) &alloc_extshndx,
553
1.89k
          ibfd, false))
554
373
  {
555
373
    intsym_buf = NULL;
556
373
    goto out1;
557
373
  }
558
2.08k
    }
559
560
15.5k
  if (intsym_buf == NULL)
561
8.00k
    {
562
8.00k
      if (_bfd_mul_overflow (symcount, sizeof (Elf_Internal_Sym), &amt))
563
0
  {
564
0
    bfd_set_error (bfd_error_file_too_big);
565
0
    goto out1;
566
0
  }
567
8.00k
      alloc_intsym = bfd_malloc (amt);
568
8.00k
      intsym_buf = alloc_intsym;
569
8.00k
      if (intsym_buf == NULL)
570
0
  goto out1;
571
8.00k
    }
572
573
  /* Convert the symbols to internal form.  */
574
15.5k
  isymend = intsym_buf + symcount;
575
15.5k
  for (esym = (const bfd_byte *) extsym_buf, isym = intsym_buf,
576
15.5k
     shndx = extshndx_buf;
577
201k
       isym < isymend;
578
185k
       esym += extsym_size, isym++, shndx = shndx != NULL ? shndx + 1 : NULL)
579
187k
    {
580
187k
      if (!(*bed->s->swap_symbol_in) (ibfd, esym, shndx, isym))
581
499
  {
582
499
    symoffset += (esym - (bfd_byte *) extsym_buf) / extsym_size;
583
    /* xgettext:c-format */
584
499
    _bfd_error_handler (_("%pB symbol number %lu references"
585
499
        " nonexistent SHT_SYMTAB_SHNDX section"),
586
499
            ibfd, (unsigned long) symoffset);
587
499
    free (alloc_intsym);
588
499
    intsym_buf = NULL;
589
499
    goto out1;
590
499
  }
591
592
      /* PR 33019: Do not accept unsupported binding values - they will
593
   likely cause problems later on.  */
594
187k
      int bind = ELF_ST_BIND (isym->st_info);
595
187k
      if (bind > STB_WEAK && bind < STB_LOOS)
596
986
  { 
597
    /* xgettext:c-format */
598
986
    _bfd_error_handler (_("%pB symbol number %lu uses unsupported binding of %u"),
599
986
            ibfd, (unsigned long) (isym - intsym_buf), bind);
600
986
    free (alloc_intsym);
601
986
    intsym_buf = NULL;
602
986
    goto out1;
603
986
  }
604
605
      /* Paranoia: Also refuse to accept the only undefined symbol type: 7.  */
606
186k
      int t = ELF_ST_TYPE (isym->st_info);
607
186k
      if (t == 7)
608
238
  {
609
    /* xgettext:c-format */
610
238
    _bfd_error_handler (_("%pB symbol number %lu uses unsupported type of %u"),
611
238
            ibfd, (unsigned long) (isym - intsym_buf), t);
612
238
    free (alloc_intsym);
613
238
    intsym_buf = NULL;
614
238
    goto out1;
615
238
  }
616
186k
    }
617
618
15.9k
 out1:
619
15.9k
  _bfd_munmap_temporary (alloc_extshndx, alloc_extshndx_size);
620
17.1k
 out2:
621
17.1k
  _bfd_munmap_temporary (alloc_ext, alloc_ext_size);
622
623
17.1k
  return intsym_buf;
624
15.9k
}
625
626
/* Look up a symbol name.  */
627
static const char *
628
bfd_elf_sym_name_raw (bfd *abfd,
629
          Elf_Internal_Shdr *symtab_hdr,
630
          const Elf_Internal_Sym *isym)
631
172k
{
632
172k
  unsigned int iname = isym->st_name;
633
172k
  unsigned int shindex = symtab_hdr->sh_link;
634
635
172k
  if (iname == 0 && ELF_ST_TYPE (isym->st_info) == STT_SECTION
636
      /* Check for a bogus st_shndx to avoid crashing.  */
637
24.4k
      && isym->st_shndx < elf_numsections (abfd))
638
23.8k
    {
639
23.8k
      iname = elf_elfsections (abfd)[isym->st_shndx]->sh_name;
640
23.8k
      shindex = elf_elfheader (abfd)->e_shstrndx;
641
23.8k
    }
642
643
172k
  return bfd_elf_string_from_elf_section (abfd, shindex, iname);
644
172k
}
645
646
const char *
647
bfd_elf_sym_name (bfd *abfd,
648
      Elf_Internal_Shdr *symtab_hdr,
649
      const Elf_Internal_Sym *isym,
650
      asection *sym_sec)
651
166k
{
652
166k
  const char *name = bfd_elf_sym_name_raw (abfd, symtab_hdr, isym);
653
166k
  if (name == NULL)
654
52.7k
    name = bfd_symbol_error_name;
655
113k
  else if (sym_sec && *name == '\0')
656
0
    name = bfd_section_name (sym_sec);
657
658
166k
  return name;
659
166k
}
660
661
/* Return the name of the group signature symbol.  Why isn't the
662
   signature just a string?  */
663
664
static const char *
665
group_signature (bfd *abfd, Elf_Internal_Shdr *ghdr)
666
9.54k
{
667
9.54k
  Elf_Internal_Shdr *hdr;
668
9.54k
  unsigned char esym[sizeof (Elf64_External_Sym)];
669
9.54k
  Elf_External_Sym_Shndx eshndx;
670
9.54k
  Elf_Internal_Sym isym;
671
672
  /* First we need to ensure the symbol table is available.  Make sure
673
     that it is a symbol table section.  */
674
9.54k
  if (ghdr->sh_link >= elf_numsections (abfd))
675
25
    return NULL;
676
9.51k
  hdr = elf_elfsections (abfd) [ghdr->sh_link];
677
9.51k
  if (hdr->sh_type != SHT_SYMTAB
678
8.88k
      || ! bfd_section_from_shdr (abfd, ghdr->sh_link))
679
709
    return NULL;
680
681
  /* Go read the symbol.  */
682
8.80k
  hdr = &elf_symtab_hdr (abfd);
683
8.80k
  if (bfd_elf_get_elf_syms (abfd, hdr, 1, ghdr->sh_info,
684
8.80k
          &isym, esym, &eshndx) == NULL)
685
2.39k
    return NULL;
686
687
6.40k
  return bfd_elf_sym_name_raw (abfd, hdr, &isym);
688
8.80k
}
689
690
static bool
691
is_valid_group_section_header (Elf_Internal_Shdr *shdr, size_t minsize)
692
14.6k
{
693
14.6k
  return (shdr->sh_size >= minsize
694
14.4k
    && shdr->sh_entsize == GRP_ENTRY_SIZE
695
13.2k
    && shdr->sh_size % GRP_ENTRY_SIZE == 0
696
12.9k
    && shdr->bfd_section != NULL);
697
14.6k
}
698
699
700
/* Set next_in_group, sec_group list pointers, and group names.  */
701
702
static bool
703
process_sht_group_entries (bfd *abfd,
704
         Elf_Internal_Shdr *ghdr, unsigned int gidx)
705
12.8k
{
706
12.8k
  unsigned char *contents;
707
708
  /* Read the raw contents.  */
709
12.8k
  if (!bfd_malloc_and_get_section (abfd, ghdr->bfd_section, &contents))
710
1.03k
    {
711
1.03k
      _bfd_error_handler
712
  /* xgettext:c-format */
713
1.03k
  (_("%pB: could not read contents of group [%u]"), abfd, gidx);
714
1.03k
      return false;
715
1.03k
    }
716
717
11.7k
  asection *last_elt = NULL;
718
11.7k
  const char *gname = NULL;
719
11.7k
  unsigned char *p = contents + ghdr->sh_size;
720
600k
  while (1)
721
600k
    {
722
600k
      unsigned int idx;
723
600k
      Elf_Internal_Shdr *shdr;
724
600k
      asection *elt;
725
726
600k
      p -= 4;
727
600k
      idx = H_GET_32 (abfd, p);
728
600k
      if (p == contents)
729
7.29k
  {
730
7.29k
    if ((idx & GRP_COMDAT) != 0)
731
2.65k
      ghdr->bfd_section->flags
732
2.65k
        |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
733
7.29k
    break;
734
7.29k
  }
735
736
593k
      if (idx == 0
737
426k
    || idx >= elf_numsections (abfd)
738
65.1k
    || (shdr = elf_elfsections (abfd)[idx])->sh_type == SHT_GROUP
739
59.6k
    || ((elt = shdr->bfd_section) != NULL
740
39.3k
        && elf_sec_group (elt) != NULL
741
20.3k
        && elf_sec_group (elt) != ghdr->bfd_section))
742
538k
  {
743
538k
    _bfd_error_handler
744
538k
      (_("%pB: invalid entry (%#x) in group [%u]"),
745
538k
       abfd, idx, gidx);
746
538k
    continue;
747
538k
  }
748
749
      /* PR binutils/23199: According to the ELF gABI all sections in
750
   a group must be marked with SHF_GROUP, but some tools
751
   generate broken objects.  Fix them up here.  */
752
54.8k
      shdr->sh_flags |= SHF_GROUP;
753
754
54.8k
      if (elt == NULL)
755
20.2k
  {
756
20.2k
    if (shdr->sh_type != SHT_RELA && shdr->sh_type != SHT_REL)
757
8.79k
      {
758
8.79k
        const char *name = bfd_elf_string_from_elf_section
759
8.79k
    (abfd, elf_elfheader (abfd)->e_shstrndx, shdr->sh_name);
760
761
8.79k
        _bfd_error_handler
762
    /* xgettext:c-format */
763
8.79k
    (_("%pB: unexpected type (%#x) section `%s' in group [%u]"),
764
8.79k
     abfd, shdr->sh_type, name, gidx);
765
8.79k
      }
766
20.2k
    continue;
767
20.2k
  }
768
769
      /* Don't try to add a section to elf_next_in_group list twice.  */
770
34.6k
      if (elf_sec_group (elt) != NULL)
771
15.6k
  continue;
772
773
18.9k
      if (last_elt == NULL)
774
9.54k
  {
775
    /* Start a circular list with one element.
776
       It will be in reverse order to match what gas does.  */
777
9.54k
    elf_next_in_group (elt) = elt;
778
    /* Point the group section to it.  */
779
9.54k
    elf_next_in_group (ghdr->bfd_section) = elt;
780
9.54k
    gname = group_signature (abfd, ghdr);
781
9.54k
    if (gname == NULL)
782
4.48k
      {
783
4.48k
        free (contents);
784
4.48k
        return false;
785
4.48k
      }
786
9.54k
  }
787
9.45k
      else
788
9.45k
  {
789
9.45k
    elf_next_in_group (elt) = elf_next_in_group (last_elt);
790
9.45k
    elf_next_in_group (last_elt) = elt;
791
9.45k
  }
792
14.5k
      last_elt = elt;
793
14.5k
      elf_group_name (elt) = gname;
794
14.5k
      elf_sec_group (elt) = ghdr->bfd_section;
795
14.5k
    }
796
797
7.29k
  free (contents);
798
7.29k
  return last_elt != NULL;
799
11.7k
}
800
801
bool
802
_bfd_elf_setup_sections (bfd *abfd)
803
54.6k
{
804
54.6k
  bool result = true;
805
806
  /* Process SHF_LINK_ORDER.  */
807
499k
  for (asection *s = abfd->sections; s != NULL; s = s->next)
808
444k
    {
809
444k
      Elf_Internal_Shdr *this_hdr = &elf_section_data (s)->this_hdr;
810
444k
      if ((this_hdr->sh_flags & SHF_LINK_ORDER) != 0)
811
33.0k
  {
812
33.0k
    unsigned int elfsec = this_hdr->sh_link;
813
    /* An sh_link value of 0 is now allowed.  It indicates that linked
814
       to section has already been discarded, but that the current
815
       section has been retained for some other reason.  This linking
816
       section is still a candidate for later garbage collection
817
       however.  */
818
33.0k
    if (elfsec == 0)
819
27.2k
      {
820
27.2k
        elf_linked_to_section (s) = NULL;
821
27.2k
      }
822
5.80k
    else
823
5.80k
      {
824
5.80k
        asection *linksec = NULL;
825
826
5.80k
        if (elfsec < elf_numsections (abfd))
827
5.69k
    {
828
5.69k
      this_hdr = elf_elfsections (abfd)[elfsec];
829
5.69k
      linksec = this_hdr->bfd_section;
830
5.69k
    }
831
832
        /* PR 1991, 2008:
833
     Some strip/objcopy may leave an incorrect value in
834
     sh_link.  We don't want to proceed.  */
835
5.80k
        if (linksec == NULL)
836
1.80k
    {
837
1.80k
      _bfd_error_handler
838
        /* xgettext:c-format */
839
1.80k
        (_("%pB: sh_link [%d] in section `%pA' is incorrect"),
840
1.80k
         s->owner, elfsec, s);
841
1.80k
      result = false;
842
1.80k
    }
843
844
5.80k
        elf_linked_to_section (s) = linksec;
845
5.80k
      }
846
33.0k
  }
847
444k
    }
848
849
  /* Process section groups.  */
850
720k
  for (unsigned int i = 1; i < elf_numsections (abfd); i++)
851
666k
    {
852
666k
      Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
853
854
666k
      if (shdr && shdr->sh_type == SHT_GROUP)
855
14.6k
  {
856
14.6k
    if (is_valid_group_section_header (shdr, GRP_ENTRY_SIZE))
857
12.9k
      {
858
12.9k
        if (shdr->sh_size >= 2 * GRP_ENTRY_SIZE
859
12.8k
      && !process_sht_group_entries (abfd, shdr, i))
860
7.76k
    result = false;
861
12.9k
      }
862
1.69k
    else
863
1.69k
      {
864
        /* PR binutils/18758: Beware of corrupt binaries with
865
     invalid group data.  */
866
1.69k
        _bfd_error_handler
867
    /* xgettext:c-format */
868
1.69k
    (_("%pB: section group entry number %u is corrupt"), abfd, i);
869
1.69k
        result = false;
870
1.69k
      }
871
14.6k
  }
872
666k
    }
873
874
54.6k
  return result;
875
54.6k
}
876
877
bool
878
bfd_elf_is_group_section (bfd *abfd ATTRIBUTE_UNUSED, const asection *sec)
879
0
{
880
0
  return elf_next_in_group (sec) != NULL;
881
0
}
882
883
const char *
884
bfd_elf_group_name (bfd *abfd ATTRIBUTE_UNUSED, const asection *sec)
885
0
{
886
0
  if (elf_sec_group (sec) != NULL)
887
0
    return elf_group_name (sec);
888
0
  return NULL;
889
0
}
890
891
/* Make a BFD section from an ELF section.  We store a pointer to the
892
   BFD section in the bfd_section field of the header.  */
893
894
bool
895
_bfd_elf_make_section_from_shdr (bfd *abfd,
896
         Elf_Internal_Shdr *hdr,
897
         const char *name,
898
         int shindex)
899
632k
{
900
632k
  asection *newsect;
901
632k
  flagword flags;
902
632k
  elf_backend_data *bed;
903
632k
  unsigned int opb = bfd_octets_per_byte (abfd, NULL);
904
905
632k
  if (hdr->bfd_section != NULL)
906
88.4k
    return true;
907
908
544k
  newsect = bfd_make_section_anyway (abfd, name);
909
544k
  if (newsect == NULL)
910
0
    return false;
911
912
544k
  hdr->bfd_section = newsect;
913
544k
  elf_section_data (newsect)->this_hdr = *hdr;
914
544k
  elf_section_data (newsect)->this_idx = shindex;
915
916
  /* Always use the real type/flags.  */
917
544k
  elf_section_type (newsect) = hdr->sh_type;
918
544k
  elf_section_flags (newsect) = hdr->sh_flags;
919
920
544k
  newsect->filepos = hdr->sh_offset;
921
922
544k
  flags = SEC_NO_FLAGS;
923
544k
  if (hdr->sh_type != SHT_NOBITS)
924
529k
    flags |= SEC_HAS_CONTENTS;
925
544k
  if (hdr->sh_type == SHT_GROUP)
926
20.6k
    flags |= SEC_GROUP;
927
544k
  if ((hdr->sh_flags & SHF_ALLOC) != 0)
928
217k
    {
929
217k
      flags |= SEC_ALLOC;
930
217k
      if (hdr->sh_type != SHT_NOBITS)
931
207k
  flags |= SEC_LOAD;
932
217k
    }
933
544k
  if ((hdr->sh_flags & SHF_WRITE) == 0)
934
420k
    flags |= SEC_READONLY;
935
544k
  if ((hdr->sh_flags & SHF_EXECINSTR) != 0)
936
113k
    flags |= SEC_CODE;
937
430k
  else if ((flags & SEC_LOAD) != 0)
938
110k
    flags |= SEC_DATA;
939
544k
  if ((hdr->sh_flags & SHF_MERGE) != 0)
940
91.9k
    flags |= SEC_MERGE;
941
544k
  if ((hdr->sh_flags & SHF_STRINGS) != 0)
942
123k
    flags |= SEC_STRINGS;
943
544k
  if ((hdr->sh_flags & SHF_TLS) != 0)
944
44.8k
    flags |= SEC_THREAD_LOCAL;
945
544k
  if ((hdr->sh_flags & SHF_EXCLUDE) != 0)
946
42.6k
    flags |= SEC_EXCLUDE;
947
948
544k
  newsect->entsize = hdr->sh_entsize;
949
950
544k
  switch (elf_elfheader (abfd)->e_ident[EI_OSABI])
951
544k
    {
952
      /* FIXME: We should not recognize SHF_GNU_MBIND for ELFOSABI_NONE,
953
   but binutils as of 2019-07-23 did not set the EI_OSABI header
954
   byte.  */
955
142k
    case ELFOSABI_GNU:
956
248k
    case ELFOSABI_FREEBSD:
957
248k
      if ((hdr->sh_flags & SHF_GNU_RETAIN) != 0)
958
43.8k
  elf_tdata (abfd)->has_gnu_osabi |= elf_gnu_osabi_retain;
959
      /* Fall through */
960
425k
    case ELFOSABI_NONE:
961
425k
      if ((hdr->sh_flags & SHF_GNU_MBIND) != 0)
962
39.1k
  elf_tdata (abfd)->has_gnu_osabi |= elf_gnu_osabi_mbind;
963
425k
      break;
964
544k
    }
965
966
544k
  if ((flags & (SEC_ALLOC | SEC_GROUP)) == 0)
967
307k
    {
968
      /* The debugging sections appear to be recognized only by name,
969
   not any sort of flag.  Their SEC_ALLOC bits are cleared.  */
970
307k
      if (name [0] == '.')
971
121k
  {
972
121k
    if (startswith (name, ".debug")
973
73.7k
        || startswith (name, ".gnu.debuglto_.debug_")
974
73.3k
        || startswith (name, ".gnu.linkonce.wi.")
975
72.6k
        || startswith (name, ".zdebug"))
976
50.9k
      flags |= SEC_DEBUGGING | SEC_ELF_OCTETS;
977
70.9k
    else if (startswith (name, GNU_BUILD_ATTRS_SECTION_NAME)
978
70.5k
       || startswith (name, ".note.gnu"))
979
784
      {
980
784
        flags |= SEC_ELF_OCTETS;
981
784
        opb = 1;
982
784
      }
983
70.1k
    else if (startswith (name, ".line")
984
69.5k
       || startswith (name, ".stab")
985
67.5k
       || streq (name, ".gdb_index"))
986
3.03k
      flags |= SEC_DEBUGGING;
987
121k
  }
988
307k
    }
989
990
544k
  if (!bfd_set_section_vma (newsect, hdr->sh_addr / opb)
991
544k
      || !bfd_set_section_size (newsect, hdr->sh_size)
992
544k
      || !bfd_set_section_alignment (newsect, bfd_log2 (hdr->sh_addralign
993
544k
              & -hdr->sh_addralign)))
994
281
    return false;
995
996
  /* As a GNU extension, if the name begins with .gnu.linkonce, we
997
     only link a single copy of the section.  This is used to support
998
     g++.  g++ will emit each template expansion in its own section.
999
     The symbols will be defined as weak, so that multiple definitions
1000
     are permitted.  The GNU linker extension is to actually discard
1001
     all but one of the sections.  */
1002
544k
  if (startswith (name, ".gnu.linkonce")
1003
2.09k
      && elf_next_in_group (newsect) == NULL)
1004
2.09k
    flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
1005
1006
544k
  if (!bfd_set_section_flags (newsect, flags))
1007
0
    return false;
1008
1009
544k
  bed = get_elf_backend_data (abfd);
1010
544k
  if (bed->elf_backend_section_flags)
1011
127k
    if (!bed->elf_backend_section_flags (hdr))
1012
0
      return false;
1013
1014
  /* We do not parse the PT_NOTE segments as we are interested even in the
1015
     separate debug info files which may have the segments offsets corrupted.
1016
     PT_NOTEs from the core files are currently not parsed using BFD.  */
1017
544k
  if (hdr->sh_type == SHT_NOTE && hdr->sh_size != 0)
1018
8.14k
    {
1019
8.14k
      bfd_byte *contents;
1020
1021
8.14k
      if (!_bfd_elf_mmap_section_contents (abfd, newsect, &contents))
1022
1.55k
  return false;
1023
1024
6.58k
      elf_parse_notes (abfd, (char *) contents, hdr->sh_size,
1025
6.58k
           hdr->sh_offset, hdr->sh_addralign);
1026
6.58k
      _bfd_elf_munmap_section_contents (newsect, contents);
1027
6.58k
    }
1028
1029
542k
  if ((newsect->flags & SEC_ALLOC) != 0)
1030
216k
    {
1031
216k
      Elf_Internal_Phdr *phdr;
1032
216k
      unsigned int i, nload;
1033
1034
      /* Some ELF linkers produce binaries with all the program header
1035
   p_paddr fields zero.  If we have such a binary with more than
1036
   one PT_LOAD header, then leave the section lma equal to vma
1037
   so that we don't create sections with overlapping lma.  */
1038
216k
      phdr = elf_tdata (abfd)->phdr;
1039
238k
      for (nload = 0, i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
1040
75.5k
  if (phdr->p_paddr != 0)
1041
53.6k
    break;
1042
21.8k
  else if (phdr->p_type == PT_LOAD && phdr->p_memsz != 0)
1043
1.66k
    ++nload;
1044
216k
      if (i >= elf_elfheader (abfd)->e_phnum && nload > 1)
1045
166
  return true;
1046
1047
216k
      phdr = elf_tdata (abfd)->phdr;
1048
2.74M
      for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
1049
2.54M
  {
1050
2.54M
    if (((phdr->p_type == PT_LOAD
1051
76.9k
    && (hdr->sh_flags & SHF_TLS) == 0)
1052
2.47M
         || phdr->p_type == PT_TLS)
1053
75.7k
        && ELF_SECTION_IN_SEGMENT (hdr, phdr))
1054
11.4k
      {
1055
11.4k
        if ((newsect->flags & SEC_LOAD) == 0)
1056
752
    newsect->lma = (phdr->p_paddr
1057
752
        + hdr->sh_addr - phdr->p_vaddr) / opb;
1058
10.7k
        else
1059
    /* We used to use the same adjustment for SEC_LOAD
1060
       sections, but that doesn't work if the segment
1061
       is packed with code from multiple VMAs.
1062
       Instead we calculate the section LMA based on
1063
       the segment LMA.  It is assumed that the
1064
       segment will contain sections with contiguous
1065
       LMAs, even if the VMAs are not.  */
1066
10.7k
    newsect->lma = (phdr->p_paddr
1067
10.7k
        + hdr->sh_offset - phdr->p_offset) / opb;
1068
1069
        /* With contiguous segments, we can't tell from file
1070
     offsets whether a section with zero size should
1071
     be placed at the end of one segment or the
1072
     beginning of the next.  Decide based on vaddr.  */
1073
11.4k
        if (hdr->sh_addr >= phdr->p_vaddr
1074
11.4k
      && (hdr->sh_addr + hdr->sh_size
1075
11.4k
          <= phdr->p_vaddr + phdr->p_memsz))
1076
10.3k
    break;
1077
11.4k
      }
1078
2.54M
  }
1079
216k
    }
1080
1081
  /* Compress/decompress DWARF debug sections with names: .debug_*,
1082
     .zdebug_*, .gnu.debuglto_.debug_, after the section flags is set.  */
1083
542k
  if ((newsect->flags & SEC_DEBUGGING) != 0
1084
53.9k
      && (newsect->flags & SEC_HAS_CONTENTS) != 0
1085
53.8k
      && (newsect->flags & SEC_ELF_OCTETS) != 0)
1086
50.8k
    {
1087
50.8k
      enum { nothing, compress, decompress } action = nothing;
1088
50.8k
      int compression_header_size;
1089
50.8k
      bfd_size_type uncompressed_size;
1090
50.8k
      unsigned int uncompressed_align_power;
1091
50.8k
      enum compression_type ch_type = ch_none;
1092
50.8k
      bool compressed
1093
50.8k
  = bfd_is_section_compressed_info (abfd, newsect,
1094
50.8k
            &compression_header_size,
1095
50.8k
            &uncompressed_size,
1096
50.8k
            &uncompressed_align_power,
1097
50.8k
            &ch_type);
1098
1099
      /* Should we decompress?  */
1100
50.8k
      if ((abfd->flags & BFD_DECOMPRESS) != 0 && compressed)
1101
275
  action = decompress;
1102
1103
      /* Should we compress?  Or convert to a different compression?  */
1104
50.5k
      else if ((abfd->flags & BFD_COMPRESS) != 0
1105
3.33k
         && newsect->size != 0
1106
3.14k
         && compression_header_size >= 0
1107
3.04k
         && uncompressed_size > 0)
1108
3.03k
  {
1109
3.03k
    if (!compressed)
1110
2.97k
      action = compress;
1111
67
    else
1112
67
      {
1113
67
        enum compression_type new_ch_type = ch_none;
1114
67
        if ((abfd->flags & BFD_COMPRESS_GABI) != 0)
1115
67
    new_ch_type = ((abfd->flags & BFD_COMPRESS_ZSTD) != 0
1116
67
             ? ch_compress_zstd : ch_compress_zlib);
1117
67
        if (new_ch_type != ch_type)
1118
40
    action = compress;
1119
67
      }
1120
3.03k
  }
1121
1122
50.8k
      if (action == compress)
1123
3.01k
  {
1124
3.01k
    if (!bfd_init_section_compress_status (abfd, newsect))
1125
190
      {
1126
190
        _bfd_error_handler
1127
    /* xgettext:c-format */
1128
190
    (_("%pB: unable to compress section %s"), abfd, name);
1129
190
        return false;
1130
190
      }
1131
3.01k
  }
1132
47.7k
      else if (action == decompress)
1133
275
  {
1134
275
    if (!bfd_init_section_decompress_status (abfd, newsect))
1135
161
      {
1136
161
        _bfd_error_handler
1137
    /* xgettext:c-format */
1138
161
    (_("%pB: unable to decompress section %s"), abfd, name);
1139
161
        return false;
1140
161
      }
1141
114
#ifndef HAVE_ZSTD
1142
114
    if (newsect->compress_status == DECOMPRESS_SECTION_ZSTD)
1143
16
      {
1144
16
        _bfd_error_handler
1145
      /* xgettext:c-format */
1146
16
      (_ ("%pB: section %s is compressed with zstd, but BFD "
1147
16
          "is not built with zstd support"),
1148
16
       abfd, name);
1149
16
        newsect->compress_status = COMPRESS_SECTION_NONE;
1150
16
        return false;
1151
16
      }
1152
98
#endif
1153
98
    if (abfd->is_linker_input
1154
0
        && name[1] == 'z')
1155
0
      {
1156
        /* Rename section from .zdebug_* to .debug_* so that ld
1157
     scripts will see this section as a debug section.  */
1158
0
        char *new_name = bfd_zdebug_name_to_debug (abfd, name);
1159
0
        if (new_name == NULL)
1160
0
    return false;
1161
0
        bfd_rename_section (newsect, new_name);
1162
0
      }
1163
98
  }
1164
50.8k
    }
1165
1166
542k
  return true;
1167
542k
}
1168
1169
const char *const bfd_elf_section_type_names[] =
1170
{
1171
  "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
1172
  "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
1173
  "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
1174
};
1175
1176
/* ELF relocs are against symbols.  If we are producing relocatable
1177
   output, and the reloc is against an external symbol, and nothing
1178
   has given us any additional addend, the resulting reloc will also
1179
   be against the same symbol.  In such a case, we don't want to
1180
   change anything about the way the reloc is handled, since it will
1181
   all be done at final link time.  Rather than put special case code
1182
   into bfd_perform_relocation, all the reloc types use this howto
1183
   function, or should call this function for relocatable output.  */
1184
1185
bfd_reloc_status_type
1186
bfd_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED,
1187
           arelent *reloc_entry,
1188
           asymbol *symbol,
1189
           void *data ATTRIBUTE_UNUSED,
1190
           asection *input_section,
1191
           bfd *output_bfd,
1192
           char **error_message ATTRIBUTE_UNUSED)
1193
12.3k
{
1194
12.3k
  if (output_bfd != NULL
1195
0
      && (symbol->flags & BSF_SECTION_SYM) == 0
1196
0
      && (! reloc_entry->howto->partial_inplace
1197
0
    || reloc_entry->addend == 0))
1198
0
    {
1199
0
      reloc_entry->address += input_section->output_offset;
1200
0
      return bfd_reloc_ok;
1201
0
    }
1202
1203
  /* In some cases the relocation should be treated as output section
1204
     relative, as when linking ELF DWARF into PE COFF.  Many ELF
1205
     targets lack section relative relocations and instead use
1206
     ordinary absolute relocations for references between DWARF
1207
     sections.  That is arguably a bug in those targets but it happens
1208
     to work for the usual case of linking to non-loaded ELF debug
1209
     sections with VMAs forced to zero.  PE COFF on the other hand
1210
     doesn't allow a section VMA of zero.  */
1211
12.3k
  if (output_bfd == NULL
1212
12.3k
      && !reloc_entry->howto->pc_relative
1213
11.7k
      && (symbol->section->flags & SEC_DEBUGGING) != 0
1214
3.63k
      && (input_section->flags & SEC_DEBUGGING) != 0)
1215
3.39k
    reloc_entry->addend -= symbol->section->output_section->vma;
1216
1217
12.3k
  return bfd_reloc_continue;
1218
12.3k
}
1219

1220
/* Returns TRUE if section A matches section B.
1221
   Names, addresses and links may be different, but everything else
1222
   should be the same.  */
1223
1224
static bool
1225
section_match (const Elf_Internal_Shdr * a,
1226
         const Elf_Internal_Shdr * b)
1227
745
{
1228
745
  if (a->sh_type != b->sh_type
1229
101
      || ((a->sh_flags ^ b->sh_flags) & ~SHF_INFO_LINK) != 0
1230
48
      || a->sh_addralign != b->sh_addralign
1231
13
      || a->sh_entsize != b->sh_entsize)
1232
734
    return false;
1233
11
  if (a->sh_type == SHT_SYMTAB
1234
11
      || a->sh_type == SHT_STRTAB)
1235
11
    return true;
1236
0
  return a->sh_size == b->sh_size;
1237
11
}
1238
1239
/* Find a section in OBFD that has the same characteristics
1240
   as IHEADER.  Return the index of this section or SHN_UNDEF if
1241
   none can be found.  Check's section HINT first, as this is likely
1242
   to be the correct section.  */
1243
1244
static unsigned int
1245
find_link (const bfd *obfd, const Elf_Internal_Shdr *iheader,
1246
     const unsigned int hint)
1247
64
{
1248
64
  Elf_Internal_Shdr ** oheaders = elf_elfsections (obfd);
1249
64
  unsigned int i;
1250
1251
64
  BFD_ASSERT (iheader != NULL);
1252
1253
  /* See PR 20922 for a reproducer of the NULL test.  */
1254
64
  if (hint < elf_numsections (obfd)
1255
64
      && oheaders[hint] != NULL
1256
64
      && section_match (oheaders[hint], iheader))
1257
2
    return hint;
1258
1259
734
  for (i = 1; i < elf_numsections (obfd); i++)
1260
681
    {
1261
681
      Elf_Internal_Shdr * oheader = oheaders[i];
1262
1263
681
      if (oheader == NULL)
1264
0
  continue;
1265
681
      if (section_match (oheader, iheader))
1266
  /* FIXME: Do we care if there is a potential for
1267
     multiple matches ?  */
1268
9
  return i;
1269
681
    }
1270
1271
53
  return SHN_UNDEF;
1272
62
}
1273
1274
/* PR 19938: Attempt to set the ELF section header fields of an OS or
1275
   Processor specific section, based upon a matching input section.
1276
   Returns TRUE upon success, FALSE otherwise.  */
1277
1278
static bool
1279
copy_special_section_fields (const bfd *ibfd,
1280
           bfd *obfd,
1281
           const Elf_Internal_Shdr *iheader,
1282
           Elf_Internal_Shdr *oheader,
1283
           const unsigned int secnum)
1284
208
{
1285
208
  elf_backend_data *bed = get_elf_backend_data (obfd);
1286
208
  const Elf_Internal_Shdr **iheaders
1287
208
    = (const Elf_Internal_Shdr **) elf_elfsections (ibfd);
1288
208
  bool changed = false;
1289
208
  unsigned int sh_link;
1290
1291
208
  if (oheader->sh_type == SHT_NOBITS)
1292
23
    {
1293
      /* This is a feature for objcopy --only-keep-debug:
1294
   When a section's type is changed to NOBITS, we preserve
1295
   the sh_link and sh_info fields so that they can be
1296
   matched up with the original.
1297
1298
   Note: Strictly speaking these assignments are wrong.
1299
   The sh_link and sh_info fields should point to the
1300
   relevent sections in the output BFD, which may not be in
1301
   the same location as they were in the input BFD.  But
1302
   the whole point of this action is to preserve the
1303
   original values of the sh_link and sh_info fields, so
1304
   that they can be matched up with the section headers in
1305
   the original file.  So strictly speaking we may be
1306
   creating an invalid ELF file, but it is only for a file
1307
   that just contains debug info and only for sections
1308
   without any contents.  */
1309
23
      if (oheader->sh_link == 0)
1310
22
  oheader->sh_link = iheader->sh_link;
1311
23
      if (oheader->sh_info == 0)
1312
23
  oheader->sh_info = iheader->sh_info;
1313
23
      return true;
1314
23
    }
1315
1316
  /* Allow the target a chance to decide how these fields should be set.  */
1317
185
  if (bed->elf_backend_copy_special_section_fields (ibfd, obfd,
1318
185
                iheader, oheader))
1319
105
    return true;
1320
1321
  /* We have an iheader which might match oheader, and which has non-zero
1322
     sh_info and/or sh_link fields.  Attempt to follow those links and find
1323
     the section in the output bfd which corresponds to the linked section
1324
     in the input bfd.  */
1325
80
  if (iheader->sh_link != SHN_UNDEF)
1326
56
    {
1327
      /* See PR 20931 for a reproducer.  */
1328
56
      if (iheader->sh_link >= elf_numsections (ibfd))
1329
0
  {
1330
0
    _bfd_error_handler
1331
      /* xgettext:c-format */
1332
0
      (_("%pB: invalid sh_link field (%d) in section number %d"),
1333
0
       ibfd, iheader->sh_link, secnum);
1334
0
    return false;
1335
0
  }
1336
1337
56
      sh_link = find_link (obfd, iheaders[iheader->sh_link], iheader->sh_link);
1338
56
      if (sh_link != SHN_UNDEF)
1339
11
  {
1340
11
    oheader->sh_link = sh_link;
1341
11
    changed = true;
1342
11
  }
1343
45
      else
1344
  /* FIXME: Should we install iheader->sh_link
1345
     if we could not find a match ?  */
1346
45
  _bfd_error_handler
1347
    /* xgettext:c-format */
1348
45
    (_("%pB: failed to find link section for section %d"), obfd, secnum);
1349
56
    }
1350
1351
80
  if (iheader->sh_info)
1352
39
    {
1353
      /* The sh_info field can hold arbitrary information, but if the
1354
   SHF_LINK_INFO flag is set then it should be interpreted as a
1355
   section index.  */
1356
39
      if (iheader->sh_flags & SHF_INFO_LINK)
1357
8
  {
1358
8
    sh_link = find_link (obfd, iheaders[iheader->sh_info],
1359
8
             iheader->sh_info);
1360
8
    if (sh_link != SHN_UNDEF)
1361
0
      oheader->sh_flags |= SHF_INFO_LINK;
1362
8
  }
1363
31
      else
1364
  /* No idea what it means - just copy it.  */
1365
31
  sh_link = iheader->sh_info;
1366
1367
39
      if (sh_link != SHN_UNDEF)
1368
31
  {
1369
31
    oheader->sh_info = sh_link;
1370
31
    changed = true;
1371
31
  }
1372
8
      else
1373
8
  _bfd_error_handler
1374
    /* xgettext:c-format */
1375
8
    (_("%pB: failed to find info section for section %d"), obfd, secnum);
1376
39
    }
1377
1378
80
  return changed;
1379
80
}
1380
1381
/* Copy the program header and other data from one object module to
1382
   another.  */
1383
1384
bool
1385
_bfd_elf_copy_private_bfd_data (bfd *ibfd, bfd *obfd)
1386
664
{
1387
664
  const Elf_Internal_Shdr **iheaders;
1388
664
  Elf_Internal_Shdr **oheaders;
1389
664
  elf_backend_data *bed;
1390
664
  unsigned int i;
1391
1392
664
  if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
1393
0
    return true;
1394
1395
664
  if (!elf_flags_init (obfd))
1396
595
    {
1397
595
      elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
1398
595
      elf_flags_init (obfd) = true;
1399
595
    }
1400
1401
664
  elf_gp (obfd) = elf_gp (ibfd);
1402
1403
  /* Also copy the EI_OSABI field.  */
1404
664
  elf_elfheader (obfd)->e_ident[EI_OSABI] =
1405
664
    elf_elfheader (ibfd)->e_ident[EI_OSABI];
1406
1407
  /* If set, copy the EI_ABIVERSION field.  */
1408
664
  if (elf_elfheader (ibfd)->e_ident[EI_ABIVERSION])
1409
212
    elf_elfheader (obfd)->e_ident[EI_ABIVERSION]
1410
212
      = elf_elfheader (ibfd)->e_ident[EI_ABIVERSION];
1411
1412
  /* Copy object attributes.  */
1413
664
  _bfd_elf_copy_obj_attributes (ibfd, obfd);
1414
1415
664
  iheaders = (const Elf_Internal_Shdr **) elf_elfsections (ibfd);
1416
664
  oheaders = elf_elfsections (obfd);
1417
664
  if (iheaders == NULL || oheaders == NULL)
1418
504
    return true;
1419
1420
160
  bed = get_elf_backend_data (obfd);
1421
1422
  /* Possibly copy other fields in the section header.  */
1423
2.00k
  for (i = 1; i < elf_numsections (obfd); i++)
1424
1.84k
    {
1425
1.84k
      unsigned int j;
1426
1.84k
      Elf_Internal_Shdr * oheader = oheaders[i];
1427
1428
      /* Ignore ordinary sections.  SHT_NOBITS sections are considered however
1429
   because of a special case need for generating separate debug info
1430
   files.  See below for more details.  */
1431
1.84k
      if (oheader == NULL
1432
1.84k
    || (oheader->sh_type != SHT_NOBITS
1433
1.81k
        && oheader->sh_type < SHT_LOOS))
1434
1.61k
  continue;
1435
1436
      /* Ignore empty sections, and sections whose
1437
   fields have already been initialised.  */
1438
232
      if (oheader->sh_size == 0
1439
213
    || (oheader->sh_info != 0 && oheader->sh_link != 0))
1440
33
  continue;
1441
1442
      /* Scan for the matching section in the input bfd.
1443
   First we try for a direct mapping between the input and
1444
   output sections.  */
1445
1.96k
      for (j = 1; j < elf_numsections (ibfd); j++)
1446
1.96k
  {
1447
1.96k
    const Elf_Internal_Shdr * iheader = iheaders[j];
1448
1449
1.96k
    if (iheader == NULL)
1450
0
      continue;
1451
1452
1.96k
    if (oheader->bfd_section != NULL
1453
1.96k
        && iheader->bfd_section != NULL
1454
1.54k
        && iheader->bfd_section->output_section != NULL
1455
1.54k
        && iheader->bfd_section->output_section == oheader->bfd_section)
1456
199
      {
1457
        /* We have found a connection from the input section to
1458
     the output section.  Attempt to copy the header fields.
1459
     If this fails then do not try any further sections -
1460
     there should only be a one-to-one mapping between
1461
     input and output.  */
1462
199
        if (!copy_special_section_fields (ibfd, obfd,
1463
199
            iheader, oheader, i))
1464
34
    j = elf_numsections (ibfd);
1465
199
        break;
1466
199
      }
1467
1.96k
  }
1468
1469
199
      if (j < elf_numsections (ibfd))
1470
165
  continue;
1471
1472
      /* That failed.  So try to deduce the corresponding input section.
1473
   Unfortunately we cannot compare names as the output string table
1474
   is empty, so instead we check size, address and type.  */
1475
529
      for (j = 1; j < elf_numsections (ibfd); j++)
1476
495
  {
1477
495
    const Elf_Internal_Shdr * iheader = iheaders[j];
1478
1479
495
    if (iheader == NULL)
1480
0
      continue;
1481
1482
    /* Try matching fields in the input section's header.
1483
       Since --only-keep-debug turns all non-debug sections into
1484
       SHT_NOBITS sections, the output SHT_NOBITS type matches any
1485
       input type.  */
1486
495
    if ((oheader->sh_type == SHT_NOBITS
1487
495
         || iheader->sh_type == oheader->sh_type)
1488
38
        && (iheader->sh_flags & ~ SHF_INFO_LINK)
1489
38
        == (oheader->sh_flags & ~ SHF_INFO_LINK)
1490
38
        && iheader->sh_addralign == oheader->sh_addralign
1491
20
        && iheader->sh_entsize == oheader->sh_entsize
1492
18
        && iheader->sh_size == oheader->sh_size
1493
17
        && iheader->sh_addr == oheader->sh_addr
1494
16
        && (iheader->sh_info != oheader->sh_info
1495
13
      || iheader->sh_link != oheader->sh_link))
1496
9
      {
1497
9
        if (copy_special_section_fields (ibfd, obfd, iheader, oheader, i))
1498
0
    break;
1499
9
      }
1500
495
  }
1501
1502
34
      if (j == elf_numsections (ibfd) && oheader->sh_type >= SHT_LOOS)
1503
34
  {
1504
    /* Final attempt.  Call the backend copy function
1505
       with a NULL input section.  */
1506
34
    (void) bed->elf_backend_copy_special_section_fields (ibfd, obfd,
1507
34
                     NULL, oheader);
1508
34
  }
1509
34
    }
1510
1511
160
  return true;
1512
664
}
1513
1514
static const char *
1515
get_segment_type (unsigned int p_type)
1516
258k
{
1517
258k
  const char *pt;
1518
258k
  switch (p_type)
1519
258k
    {
1520
47.8k
    case PT_NULL: pt = "NULL"; break;
1521
2.93k
    case PT_LOAD: pt = "LOAD"; break;
1522
888
    case PT_DYNAMIC: pt = "DYNAMIC"; break;
1523
1.01k
    case PT_INTERP: pt = "INTERP"; break;
1524
319
    case PT_NOTE: pt = "NOTE"; break;
1525
229
    case PT_SHLIB: pt = "SHLIB"; break;
1526
4.96k
    case PT_PHDR: pt = "PHDR"; break;
1527
149
    case PT_TLS: pt = "TLS"; break;
1528
141
    case PT_GNU_EH_FRAME: pt = "EH_FRAME"; break;
1529
160
    case PT_GNU_STACK: pt = "STACK"; break;
1530
116
    case PT_GNU_RELRO: pt = "RELRO"; break;
1531
1
    case PT_GNU_SFRAME: pt = "SFRAME"; break;
1532
199k
    default: pt = NULL; break;
1533
258k
    }
1534
258k
  return pt;
1535
258k
}
1536
1537
/* Print out the program headers.  */
1538
1539
bool
1540
_bfd_elf_print_private_bfd_data (bfd *abfd, void *farg)
1541
17.5k
{
1542
17.5k
  FILE *f = (FILE *) farg;
1543
17.5k
  Elf_Internal_Phdr *p;
1544
17.5k
  asection *s;
1545
17.5k
  bfd_byte *dynbuf = NULL;
1546
1547
17.5k
  p = elf_tdata (abfd)->phdr;
1548
17.5k
  if (p != NULL)
1549
12.1k
    {
1550
12.1k
      unsigned int i, c;
1551
1552
12.1k
      fprintf (f, _("\nProgram Header:\n"));
1553
12.1k
      c = elf_elfheader (abfd)->e_phnum;
1554
270k
      for (i = 0; i < c; i++, p++)
1555
258k
  {
1556
258k
    const char *pt = get_segment_type (p->p_type);
1557
258k
    char buf[20];
1558
1559
258k
    if (pt == NULL)
1560
199k
      {
1561
199k
        sprintf (buf, "0x%lx", p->p_type);
1562
199k
        pt = buf;
1563
199k
      }
1564
258k
    fprintf (f, "%8s off    0x", pt);
1565
258k
    bfd_fprintf_vma (abfd, f, p->p_offset);
1566
258k
    fprintf (f, " vaddr 0x");
1567
258k
    bfd_fprintf_vma (abfd, f, p->p_vaddr);
1568
258k
    fprintf (f, " paddr 0x");
1569
258k
    bfd_fprintf_vma (abfd, f, p->p_paddr);
1570
258k
    fprintf (f, " align 2**%u\n", bfd_log2 (p->p_align));
1571
258k
    fprintf (f, "         filesz 0x");
1572
258k
    bfd_fprintf_vma (abfd, f, p->p_filesz);
1573
258k
    fprintf (f, " memsz 0x");
1574
258k
    bfd_fprintf_vma (abfd, f, p->p_memsz);
1575
258k
    fprintf (f, " flags %c%c%c",
1576
258k
       (p->p_flags & PF_R) != 0 ? 'r' : '-',
1577
258k
       (p->p_flags & PF_W) != 0 ? 'w' : '-',
1578
258k
       (p->p_flags & PF_X) != 0 ? 'x' : '-');
1579
258k
    if ((p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)) != 0)
1580
180k
      fprintf (f, " %lx", p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X));
1581
258k
    fprintf (f, "\n");
1582
258k
  }
1583
12.1k
    }
1584
1585
17.5k
  s = bfd_get_section_by_name (abfd, ".dynamic");
1586
17.5k
  if (s != NULL && (s->flags & SEC_HAS_CONTENTS) != 0)
1587
147
    {
1588
147
      unsigned int elfsec;
1589
147
      unsigned long shlink;
1590
147
      bfd_byte *extdyn, *extdynend;
1591
147
      size_t extdynsize;
1592
147
      void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
1593
1594
147
      fprintf (f, _("\nDynamic Section:\n"));
1595
1596
147
      if (!_bfd_elf_mmap_section_contents (abfd, s, &dynbuf))
1597
20
  goto error_return;
1598
1599
127
      elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
1600
127
      if (elfsec == SHN_BAD)
1601
0
  goto error_return;
1602
127
      shlink = elf_elfsections (abfd)[elfsec]->sh_link;
1603
1604
127
      extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
1605
127
      swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
1606
1607
127
      for (extdyn = dynbuf, extdynend = dynbuf + s->size;
1608
2.05k
     (size_t) (extdynend - extdyn) >= extdynsize;
1609
1.92k
     extdyn += extdynsize)
1610
2.03k
  {
1611
2.03k
    Elf_Internal_Dyn dyn;
1612
2.03k
    const char *name = "";
1613
2.03k
    char ab[20];
1614
2.03k
    bool stringp;
1615
2.03k
    elf_backend_data *bed = get_elf_backend_data (abfd);
1616
1617
2.03k
    (*swap_dyn_in) (abfd, extdyn, &dyn);
1618
1619
2.03k
    if (dyn.d_tag == DT_NULL)
1620
103
      break;
1621
1622
1.92k
    stringp = false;
1623
1.92k
    switch (dyn.d_tag)
1624
1.92k
      {
1625
617
      default:
1626
617
        if (bed->elf_backend_get_target_dtag)
1627
110
    name = (*bed->elf_backend_get_target_dtag) (dyn.d_tag);
1628
1629
617
        if (streq (name, ""))
1630
617
    {
1631
617
      sprintf (ab, "%#" PRIx64, (uint64_t) dyn.d_tag);
1632
617
      name = ab;
1633
617
    }
1634
617
        break;
1635
1636
80
      case DT_NEEDED: name = "NEEDED"; stringp = true; break;
1637
24
      case DT_PLTRELSZ: name = "PLTRELSZ"; break;
1638
46
      case DT_PLTGOT: name = "PLTGOT"; break;
1639
54
      case DT_HASH: name = "HASH"; break;
1640
73
      case DT_STRTAB: name = "STRTAB"; break;
1641
73
      case DT_SYMTAB: name = "SYMTAB"; break;
1642
46
      case DT_RELA: name = "RELA"; break;
1643
57
      case DT_RELASZ: name = "RELASZ"; break;
1644
48
      case DT_RELAENT: name = "RELAENT"; break;
1645
59
      case DT_STRSZ: name = "STRSZ"; break;
1646
66
      case DT_SYMENT: name = "SYMENT"; break;
1647
54
      case DT_INIT: name = "INIT"; break;
1648
54
      case DT_FINI: name = "FINI"; break;
1649
1
      case DT_SONAME: name = "SONAME"; stringp = true; break;
1650
10
      case DT_RPATH: name = "RPATH"; stringp = true; break;
1651
2
      case DT_SYMBOLIC: name = "SYMBOLIC"; break;
1652
2
      case DT_REL: name = "REL"; break;
1653
2
      case DT_RELSZ: name = "RELSZ"; break;
1654
3
      case DT_RELENT: name = "RELENT"; break;
1655
7
      case DT_RELR: name = "RELR"; break;
1656
3
      case DT_RELRSZ: name = "RELRSZ"; break;
1657
0
      case DT_RELRENT: name = "RELRENT"; break;
1658
31
      case DT_PLTREL: name = "PLTREL"; break;
1659
48
      case DT_DEBUG: name = "DEBUG"; break;
1660
3
      case DT_TEXTREL: name = "TEXTREL"; break;
1661
24
      case DT_JMPREL: name = "JMPREL"; break;
1662
0
      case DT_BIND_NOW: name = "BIND_NOW"; break;
1663
35
      case DT_INIT_ARRAY: name = "INIT_ARRAY"; break;
1664
30
      case DT_FINI_ARRAY: name = "FINI_ARRAY"; break;
1665
31
      case DT_INIT_ARRAYSZ: name = "INIT_ARRAYSZ"; break;
1666
30
      case DT_FINI_ARRAYSZ: name = "FINI_ARRAYSZ"; break;
1667
0
      case DT_RUNPATH: name = "RUNPATH"; stringp = true; break;
1668
25
      case DT_FLAGS: name = "FLAGS"; break;
1669
1
      case DT_PREINIT_ARRAY: name = "PREINIT_ARRAY"; break;
1670
1
      case DT_PREINIT_ARRAYSZ: name = "PREINIT_ARRAYSZ"; break;
1671
0
      case DT_CHECKSUM: name = "CHECKSUM"; break;
1672
0
      case DT_PLTPADSZ: name = "PLTPADSZ"; break;
1673
0
      case DT_MOVEENT: name = "MOVEENT"; break;
1674
0
      case DT_MOVESZ: name = "MOVESZ"; break;
1675
0
      case DT_FEATURE: name = "FEATURE"; break;
1676
0
      case DT_POSFLAG_1: name = "POSFLAG_1"; break;
1677
0
      case DT_SYMINSZ: name = "SYMINSZ"; break;
1678
0
      case DT_SYMINENT: name = "SYMINENT"; break;
1679
0
      case DT_CONFIG: name = "CONFIG"; stringp = true; break;
1680
0
      case DT_DEPAUDIT: name = "DEPAUDIT"; stringp = true; break;
1681
0
      case DT_AUDIT: name = "AUDIT"; stringp = true; break;
1682
0
      case DT_PLTPAD: name = "PLTPAD"; break;
1683
0
      case DT_MOVETAB: name = "MOVETAB"; break;
1684
0
      case DT_SYMINFO: name = "SYMINFO"; break;
1685
28
      case DT_RELACOUNT: name = "RELACOUNT"; break;
1686
2
      case DT_RELCOUNT: name = "RELCOUNT"; break;
1687
29
      case DT_FLAGS_1: name = "FLAGS_1"; break;
1688
59
      case DT_VERSYM: name = "VERSYM"; break;
1689
0
      case DT_VERDEF: name = "VERDEF"; break;
1690
1
      case DT_VERDEFNUM: name = "VERDEFNUM"; break;
1691
60
      case DT_VERNEED: name = "VERNEED"; break;
1692
50
      case DT_VERNEEDNUM: name = "VERNEEDNUM"; break;
1693
0
      case DT_AUXILIARY: name = "AUXILIARY"; stringp = true; break;
1694
0
      case DT_USED: name = "USED"; break;
1695
0
      case DT_FILTER: name = "FILTER"; stringp = true; break;
1696
58
      case DT_GNU_HASH: name = "GNU_HASH"; break;
1697
1.92k
      }
1698
1699
1.92k
    fprintf (f, "  %-20s ", name);
1700
1.92k
    if (! stringp)
1701
1.83k
      {
1702
1.83k
        fprintf (f, "0x");
1703
1.83k
        bfd_fprintf_vma (abfd, f, dyn.d_un.d_val);
1704
1.83k
      }
1705
91
    else
1706
91
      {
1707
91
        const char *string;
1708
91
        unsigned int tagv = dyn.d_un.d_val;
1709
1710
91
        string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
1711
91
        if (string == NULL)
1712
3
    goto error_return;
1713
88
        fprintf (f, "%s", string);
1714
88
      }
1715
1.92k
    fprintf (f, "\n");
1716
1.92k
  }
1717
1718
124
      _bfd_elf_munmap_section_contents (s, dynbuf);
1719
124
      dynbuf = NULL;
1720
124
    }
1721
1722
17.4k
  if ((elf_dynverdef (abfd) != 0 && elf_tdata (abfd)->verdef == NULL)
1723
17.4k
      || (elf_dynverref (abfd) != 0 && elf_tdata (abfd)->verref == NULL))
1724
148
    {
1725
148
      if (! _bfd_elf_slurp_version_tables (abfd, false))
1726
80
  return false;
1727
148
    }
1728
1729
17.4k
  if (elf_dynverdef (abfd) != 0)
1730
14
    {
1731
14
      Elf_Internal_Verdef *t;
1732
1733
14
      fprintf (f, _("\nVersion definitions:\n"));
1734
28
      for (t = elf_tdata (abfd)->verdef; t != NULL; t = t->vd_nextdef)
1735
14
  {
1736
14
    fprintf (f, "%d 0x%2.2x 0x%8.8lx %s\n", t->vd_ndx,
1737
14
       t->vd_flags, t->vd_hash,
1738
14
       t->vd_nodename ? t->vd_nodename : "<corrupt>");
1739
14
    if (t->vd_auxptr != NULL && t->vd_auxptr->vda_nextptr != NULL)
1740
0
      {
1741
0
        Elf_Internal_Verdaux *a;
1742
1743
0
        fprintf (f, "\t");
1744
0
        for (a = t->vd_auxptr->vda_nextptr;
1745
0
       a != NULL;
1746
0
       a = a->vda_nextptr)
1747
0
    fprintf (f, "%s ",
1748
0
       a->vda_nodename ? a->vda_nodename : "<corrupt>");
1749
0
        fprintf (f, "\n");
1750
0
      }
1751
14
  }
1752
14
    }
1753
1754
17.4k
  if (elf_dynverref (abfd) != 0)
1755
65
    {
1756
65
      Elf_Internal_Verneed *t;
1757
1758
65
      fprintf (f, _("\nVersion References:\n"));
1759
130
      for (t = elf_tdata (abfd)->verref; t != NULL; t = t->vn_nextref)
1760
65
  {
1761
65
    Elf_Internal_Vernaux *a;
1762
1763
65
    fprintf (f, _("  required from %s:\n"),
1764
65
       t->vn_filename ? t->vn_filename : "<corrupt>");
1765
150
    for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1766
85
      fprintf (f, "    0x%8.8lx 0x%2.2x %2.2d %s\n", a->vna_hash,
1767
85
         a->vna_flags, a->vna_other,
1768
85
         a->vna_nodename ? a->vna_nodename : "<corrupt>");
1769
65
  }
1770
65
    }
1771
1772
17.4k
  return true;
1773
1774
23
 error_return:
1775
23
  _bfd_elf_munmap_section_contents (s, dynbuf);
1776
23
  return false;
1777
17.4k
}
1778
1779
/* Find the file offset corresponding to VMA by using the program
1780
   headers.  */
1781
1782
static file_ptr
1783
offset_from_vma (Elf_Internal_Phdr *phdrs, size_t phnum, bfd_vma vma,
1784
     size_t size, size_t *max_size_p)
1785
3.09k
{
1786
3.09k
  Elf_Internal_Phdr *seg;
1787
3.09k
  size_t i;
1788
1789
11.2k
  for (seg = phdrs, i = 0; i < phnum; ++seg, ++i)
1790
10.9k
    if (seg->p_type == PT_LOAD
1791
4.69k
  && vma >= (seg->p_vaddr & -seg->p_align)
1792
3.61k
  && vma + size <= seg->p_vaddr + seg->p_filesz)
1793
2.77k
      {
1794
2.77k
  if (max_size_p)
1795
34
    *max_size_p = seg->p_vaddr + seg->p_filesz - vma;
1796
2.77k
  return vma - seg->p_vaddr + seg->p_offset;
1797
2.77k
      }
1798
1799
315
  if (max_size_p)
1800
29
    *max_size_p = 0;
1801
315
  bfd_set_error (bfd_error_invalid_operation);
1802
315
  return (file_ptr) -1;
1803
3.09k
}
1804
1805
/* Convert hash table to internal form.  */
1806
1807
static bfd_vma *
1808
get_hash_table_data (bfd *abfd, bfd_size_type number,
1809
         unsigned int ent_size, bfd_size_type filesize)
1810
454
{
1811
454
  unsigned char *e_data = NULL;
1812
454
  bfd_vma *i_data = NULL;
1813
454
  bfd_size_type size;
1814
454
  void *e_data_addr;
1815
454
  size_t e_data_size ATTRIBUTE_UNUSED;
1816
1817
454
  if (ent_size != 4 && ent_size != 8)
1818
0
    return NULL;
1819
1820
454
  if ((size_t) number != number)
1821
0
    {
1822
0
      bfd_set_error (bfd_error_file_too_big);
1823
0
      return NULL;
1824
0
    }
1825
1826
454
  size = ent_size * number;
1827
  /* Be kind to memory checkers (eg valgrind, address sanitizer) by not
1828
     attempting to allocate memory when the read is bound to fail.  */
1829
454
  if (size > filesize
1830
403
      || number >= ~(size_t) 0 / ent_size
1831
403
      || number >= ~(size_t) 0 / sizeof (*i_data))
1832
51
    {
1833
51
      bfd_set_error (bfd_error_file_too_big);
1834
51
      return NULL;
1835
51
    }
1836
1837
403
  e_data = _bfd_mmap_temporary (abfd, size, &e_data_addr, &e_data_size);
1838
403
  if (e_data == NULL)
1839
32
    return NULL;
1840
1841
371
  i_data = bfd_malloc (number * sizeof (*i_data));
1842
371
  if (i_data == NULL)
1843
0
    {
1844
0
      _bfd_munmap_temporary (e_data_addr, e_data_size);
1845
0
      return NULL;
1846
0
    }
1847
1848
371
  if (ent_size == 4)
1849
270k
    while (number--)
1850
269k
      i_data[number] = bfd_get_32 (abfd, e_data + number * ent_size);
1851
0
  else
1852
0
    while (number--)
1853
0
      i_data[number] = bfd_get_64 (abfd, e_data + number * ent_size);
1854
1855
371
  _bfd_munmap_temporary (e_data_addr, e_data_size);
1856
371
  return i_data;
1857
371
}
1858
1859
/* Address of .MIPS.xhash section.  FIXME: What is the best way to
1860
   support DT_MIPS_XHASH?  */
1861
120k
#define DT_MIPS_XHASH        0x70000036
1862
1863
/* Reconstruct dynamic symbol table from PT_DYNAMIC segment.  */
1864
1865
bool
1866
_bfd_elf_get_dynamic_symbols (bfd *abfd, Elf_Internal_Phdr *phdr,
1867
            Elf_Internal_Phdr *phdrs, size_t phnum,
1868
            bfd_size_type filesize)
1869
3.75k
{
1870
3.75k
  bfd_byte *extdyn, *extdynend;
1871
3.75k
  size_t extdynsize;
1872
3.75k
  void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
1873
3.75k
  bool (*swap_symbol_in) (bfd *, const void *, const void *,
1874
3.75k
        Elf_Internal_Sym *);
1875
3.75k
  Elf_Internal_Dyn dyn;
1876
3.75k
  bfd_vma dt_hash = 0;
1877
3.75k
  bfd_vma dt_gnu_hash = 0;
1878
3.75k
  bfd_vma dt_mips_xhash = 0;
1879
3.75k
  bfd_vma dt_strtab = 0;
1880
3.75k
  bfd_vma dt_symtab = 0;
1881
3.75k
  size_t dt_strsz = 0;
1882
3.75k
  bfd_vma dt_versym = 0;
1883
3.75k
  bfd_vma dt_verdef = 0;
1884
3.75k
  bfd_vma dt_verneed = 0;
1885
3.75k
  bfd_byte *dynbuf = NULL;
1886
3.75k
  char *strbuf = NULL;
1887
3.75k
  bfd_vma *gnubuckets = NULL;
1888
3.75k
  bfd_vma *gnuchains = NULL;
1889
3.75k
  bfd_vma *mipsxlat = NULL;
1890
3.75k
  file_ptr saved_filepos, filepos;
1891
3.75k
  bool res = false;
1892
3.75k
  size_t amt;
1893
3.75k
  bfd_byte *esymbuf = NULL, *esym;
1894
3.75k
  bfd_size_type symcount;
1895
3.75k
  Elf_Internal_Sym *isymbuf = NULL;
1896
3.75k
  Elf_Internal_Sym *isym, *isymend;
1897
3.75k
  bfd_byte *versym = NULL;
1898
3.75k
  bfd_byte *verdef = NULL;
1899
3.75k
  bfd_byte *verneed = NULL;
1900
3.75k
  size_t verdef_size = 0;
1901
3.75k
  size_t verneed_size = 0;
1902
3.75k
  size_t extsym_size;
1903
3.75k
  elf_backend_data *bed;
1904
3.75k
  void *dynbuf_addr = NULL;
1905
3.75k
  void *esymbuf_addr = NULL;
1906
3.75k
  size_t dynbuf_size = 0;
1907
3.75k
  size_t esymbuf_size = 0;
1908
1909
  /* Return TRUE if symbol table is bad.  */
1910
3.75k
  if (elf_bad_symtab (abfd))
1911
0
    return true;
1912
1913
  /* Return TRUE if DT_HASH/DT_GNU_HASH have bee processed before.  */
1914
3.75k
  if (elf_tdata (abfd)->dt_strtab != NULL)
1915
0
    return true;
1916
1917
3.75k
  bed = get_elf_backend_data (abfd);
1918
1919
  /* Save file position for elf_object_p.  */
1920
3.75k
  saved_filepos = bfd_tell (abfd);
1921
1922
3.75k
  if (bfd_seek (abfd, phdr->p_offset, SEEK_SET) != 0)
1923
0
    goto error_return;
1924
1925
3.75k
  dynbuf_size = phdr->p_filesz;
1926
3.75k
  dynbuf = _bfd_mmap_temporary (abfd, dynbuf_size, &dynbuf_addr, &dynbuf_size);
1927
3.75k
  if (dynbuf == NULL)
1928
82
    goto error_return;
1929
1930
3.67k
  extsym_size = bed->s->sizeof_sym;
1931
3.67k
  extdynsize = bed->s->sizeof_dyn;
1932
3.67k
  swap_dyn_in = bed->s->swap_dyn_in;
1933
1934
3.67k
  extdyn = dynbuf;
1935
3.67k
  if (phdr->p_filesz < extdynsize)
1936
95
    goto error_return;
1937
3.57k
  extdynend = extdyn + phdr->p_filesz;
1938
72.5k
  for (; extdyn <= (extdynend - extdynsize); extdyn += extdynsize)
1939
70.5k
    {
1940
70.5k
      swap_dyn_in (abfd, extdyn, &dyn);
1941
1942
70.5k
      if (dyn.d_tag == DT_NULL)
1943
1.46k
  break;
1944
1945
69.1k
      switch (dyn.d_tag)
1946
69.1k
  {
1947
1.04k
  case DT_HASH:
1948
1.04k
    dt_hash = dyn.d_un.d_val;
1949
1.04k
    break;
1950
1.13k
  case DT_GNU_HASH:
1951
1.13k
    if (bed->elf_machine_code != EM_MIPS
1952
931
        && bed->elf_machine_code != EM_MIPS_RS3_LE)
1953
931
      dt_gnu_hash = dyn.d_un.d_val;
1954
1.13k
    break;
1955
1.52k
  case DT_STRTAB:
1956
1.52k
    dt_strtab = dyn.d_un.d_val;
1957
1.52k
    break;
1958
1.46k
  case DT_SYMTAB:
1959
1.46k
    dt_symtab = dyn.d_un.d_val;
1960
1.46k
    break;
1961
1.45k
  case DT_STRSZ:
1962
1.45k
    dt_strsz = dyn.d_un.d_val;
1963
1.45k
    break;
1964
625
  case DT_SYMENT:
1965
625
    if (dyn.d_un.d_val != extsym_size)
1966
130
      goto error_return;
1967
495
    break;
1968
721
  case DT_VERSYM:
1969
721
    dt_versym = dyn.d_un.d_val;
1970
721
    break;
1971
311
  case DT_VERDEF:
1972
311
    dt_verdef = dyn.d_un.d_val;
1973
311
    break;
1974
409
  case DT_VERNEED:
1975
409
    dt_verneed = dyn.d_un.d_val;
1976
409
    break;
1977
60.4k
  default:
1978
60.4k
    if (dyn.d_tag == DT_MIPS_XHASH
1979
843
        && (bed->elf_machine_code == EM_MIPS
1980
512
      || bed->elf_machine_code == EM_MIPS_RS3_LE))
1981
331
      {
1982
331
        dt_gnu_hash = dyn.d_un.d_val;
1983
331
        dt_mips_xhash = dyn.d_un.d_val;
1984
331
      }
1985
60.4k
    break;
1986
69.1k
  }
1987
69.1k
    }
1988
1989
  /* Check if we can reconstruct dynamic symbol table from PT_DYNAMIC
1990
     segment.  */
1991
3.44k
  if ((!dt_hash && !dt_gnu_hash)
1992
1.74k
      || !dt_strtab
1993
1.23k
      || !dt_symtab
1994
1.09k
      || !dt_strsz)
1995
2.44k
    goto error_return;
1996
1997
  /* Get dynamic string table.  */
1998
999
  filepos = offset_from_vma (phdrs, phnum, dt_strtab, dt_strsz, NULL);
1999
999
  if (filepos == (file_ptr) -1
2000
908
      || bfd_seek (abfd, filepos, SEEK_SET) != 0)
2001
135
    goto error_return;
2002
2003
  /* Dynamic string table must be valid until ABFD is closed.  */
2004
864
  strbuf = (char *) _bfd_mmap_persistent (abfd, dt_strsz);
2005
864
  if (strbuf == NULL)
2006
25
    goto error_return;
2007
839
  if (strbuf[dt_strsz - 1] != 0)
2008
252
    {
2009
      /* It is an error if a string table is't terminated.  */
2010
252
      _bfd_error_handler
2011
  /* xgettext:c-format */
2012
252
  (_("%pB: DT_STRTAB table is corrupt"), abfd);
2013
252
      strbuf[dt_strsz - 1] = 0;
2014
252
    }
2015
2016
  /* Get the real symbol count from DT_HASH or DT_GNU_HASH.  Prefer
2017
     DT_HASH since it is simpler than DT_GNU_HASH.  */
2018
839
  if (dt_hash)
2019
344
    {
2020
344
      unsigned char nb[16];
2021
344
      unsigned int hash_ent_size;
2022
2023
344
      switch (bed->elf_machine_code)
2024
344
  {
2025
0
  case EM_ALPHA:
2026
2
  case EM_S390:
2027
2
  case EM_S390_OLD:
2028
2
    if (bed->s->elfclass == ELFCLASS64)
2029
0
      {
2030
0
        hash_ent_size = 8;
2031
0
        break;
2032
0
      }
2033
    /* FALLTHROUGH */
2034
344
  default:
2035
344
    hash_ent_size = 4;
2036
344
    break;
2037
344
  }
2038
2039
344
      filepos = offset_from_vma (phdrs, phnum, dt_hash, 2 * hash_ent_size,
2040
344
         NULL);
2041
344
      if (filepos == (file_ptr) -1
2042
306
    || bfd_seek (abfd, filepos, SEEK_SET) != 0
2043
284
    || bfd_read (nb, 2 * hash_ent_size, abfd) != 2 * hash_ent_size)
2044
74
  goto error_return;
2045
2046
      /* The number of dynamic symbol table entries equals the number
2047
   of chains.  */
2048
270
      if (hash_ent_size == 8)
2049
0
  symcount = bfd_get_64 (abfd, nb + hash_ent_size);
2050
270
      else
2051
270
  symcount = bfd_get_32 (abfd, nb + hash_ent_size);
2052
270
    }
2053
495
  else
2054
495
    {
2055
      /* For DT_GNU_HASH, only defined symbols with non-STB_LOCAL
2056
   bindings are in hash table.  Since in dynamic symbol table,
2057
   all symbols with STB_LOCAL binding are placed before symbols
2058
   with other bindings and all undefined symbols are placed
2059
   before defined ones, the highest symbol index in DT_GNU_HASH
2060
   is the highest dynamic symbol table index.  */
2061
495
      unsigned char nb[16];
2062
495
      bfd_vma ngnubuckets;
2063
495
      bfd_vma gnusymidx;
2064
495
      size_t i, ngnuchains;
2065
495
      bfd_vma maxchain = 0xffffffff, bitmaskwords;
2066
495
      bfd_vma buckets_vma;
2067
2068
495
      filepos = offset_from_vma (phdrs, phnum, dt_gnu_hash,
2069
495
         sizeof (nb), NULL);
2070
495
      if (filepos == (file_ptr) -1
2071
470
    || bfd_seek (abfd, filepos, SEEK_SET) != 0
2072
444
    || bfd_read (nb, sizeof (nb), abfd) != sizeof (nb))
2073
57
  goto error_return;
2074
2075
438
      ngnubuckets = bfd_get_32 (abfd, nb);
2076
438
      gnusymidx = bfd_get_32 (abfd, nb + 4);
2077
438
      bitmaskwords = bfd_get_32 (abfd, nb + 8);
2078
438
      buckets_vma = dt_gnu_hash + 16;
2079
438
      if (bed->s->elfclass == ELFCLASS32)
2080
0
  buckets_vma += bitmaskwords * 4;
2081
438
      else
2082
438
  buckets_vma += bitmaskwords * 8;
2083
438
      filepos = offset_from_vma (phdrs, phnum, buckets_vma, 4, NULL);
2084
438
      if (filepos == (file_ptr) -1
2085
412
    || bfd_seek (abfd, filepos, SEEK_SET) != 0)
2086
54
  goto error_return;
2087
2088
384
      gnubuckets = get_hash_table_data (abfd, ngnubuckets, 4, filesize);
2089
384
      if (gnubuckets == NULL)
2090
82
  goto error_return;
2091
2092
250k
      for (i = 0; i < ngnubuckets; i++)
2093
249k
  if (gnubuckets[i] != 0)
2094
195k
    {
2095
195k
      if (gnubuckets[i] < gnusymidx)
2096
29
        goto error_return;
2097
2098
195k
      if (maxchain == 0xffffffff || gnubuckets[i] > maxchain)
2099
23.2k
        maxchain = gnubuckets[i];
2100
195k
    }
2101
2102
273
      if (maxchain == 0xffffffff)
2103
48
  {
2104
48
    symcount = 0;
2105
48
    goto empty_gnu_hash;
2106
48
  }
2107
2108
225
      maxchain -= gnusymidx;
2109
225
      filepos = offset_from_vma (phdrs, phnum,
2110
225
         buckets_vma + 4 * (ngnubuckets + maxchain),
2111
225
         4, NULL);
2112
225
      if (filepos == (file_ptr) -1
2113
184
    || bfd_seek (abfd, filepos, SEEK_SET) != 0)
2114
135
  goto error_return;
2115
2116
90
      do
2117
1.97k
  {
2118
1.97k
    if (bfd_read (nb, 4, abfd) != 4)
2119
20
      goto error_return;
2120
1.95k
    ++maxchain;
2121
1.95k
    if (maxchain == 0)
2122
0
      goto error_return;
2123
1.95k
  }
2124
1.95k
      while ((bfd_get_32 (abfd, nb) & 1) == 0);
2125
2126
70
      filepos = offset_from_vma (phdrs, phnum,
2127
70
         buckets_vma + 4 * ngnubuckets,
2128
70
         4, NULL);
2129
70
      if (filepos == (file_ptr) -1
2130
70
    || bfd_seek (abfd, filepos, SEEK_SET) != 0)
2131
0
  goto error_return;
2132
2133
70
      gnuchains = get_hash_table_data (abfd, maxchain, 4, filesize);
2134
70
      if (gnuchains == NULL)
2135
1
  goto error_return;
2136
69
      ngnuchains = maxchain;
2137
2138
69
      if (dt_mips_xhash)
2139
0
  {
2140
0
    filepos = offset_from_vma (phdrs, phnum,
2141
0
             buckets_vma + 4 * (ngnubuckets + maxchain),
2142
0
             4, NULL);
2143
0
    if (filepos == (file_ptr) -1
2144
0
        || bfd_seek (abfd, filepos, SEEK_SET) != 0)
2145
0
      goto error_return;
2146
2147
0
    mipsxlat = get_hash_table_data (abfd, maxchain, 4, filesize);
2148
0
    if (mipsxlat == NULL)
2149
0
      goto error_return;
2150
0
  }
2151
2152
69
      symcount = 0;
2153
2.27k
      for (i = 0; i < ngnubuckets; ++i)
2154
2.20k
  if (gnubuckets[i] != 0)
2155
1.14k
    {
2156
1.14k
      bfd_vma si = gnubuckets[i];
2157
1.14k
      bfd_vma off = si - gnusymidx;
2158
1.14k
      do
2159
5.63k
        {
2160
5.63k
    if (mipsxlat)
2161
0
      {
2162
0
        if (mipsxlat[off] >= symcount)
2163
0
          symcount = mipsxlat[off] + 1;
2164
0
      }
2165
5.63k
    else
2166
5.63k
      {
2167
5.63k
        if (si >= symcount)
2168
1.85k
          symcount = si + 1;
2169
5.63k
      }
2170
5.63k
    si++;
2171
5.63k
        }
2172
5.63k
      while (off < ngnuchains && (gnuchains[off++] & 1) == 0);
2173
1.14k
    }
2174
69
    }
2175
2176
  /* Swap in dynamic symbol table.  */
2177
339
  if (_bfd_mul_overflow (symcount, extsym_size, &amt))
2178
0
    {
2179
0
      bfd_set_error (bfd_error_file_too_big);
2180
0
      goto error_return;
2181
0
    }
2182
2183
339
  filepos = offset_from_vma (phdrs, phnum, dt_symtab, amt, NULL);
2184
339
  if (filepos == (file_ptr) -1
2185
294
      || bfd_seek (abfd, filepos, SEEK_SET) != 0)
2186
84
    goto error_return;
2187
255
  esymbuf_size = amt;
2188
255
  esymbuf = _bfd_mmap_temporary (abfd, esymbuf_size,
2189
255
         &esymbuf_addr, &esymbuf_size);
2190
255
  if (esymbuf == NULL)
2191
29
    goto error_return;
2192
2193
226
  if (_bfd_mul_overflow (symcount, sizeof (Elf_Internal_Sym), &amt))
2194
0
    {
2195
0
      bfd_set_error (bfd_error_file_too_big);
2196
0
      goto error_return;
2197
0
    }
2198
2199
  /* Dynamic symbol table must be valid until ABFD is closed.  */
2200
226
  isymbuf = bfd_alloc (abfd, amt);
2201
226
  if (isymbuf == NULL)
2202
0
    goto error_return;
2203
2204
226
  swap_symbol_in = bed->s->swap_symbol_in;
2205
2206
  /* Convert the symbols to internal form.  */
2207
226
  isymend = isymbuf + symcount;
2208
226
  for (esym = esymbuf, isym = isymbuf;
2209
921
       isym < isymend;
2210
695
       esym += extsym_size, isym++)
2211
741
    if (!swap_symbol_in (abfd, esym, NULL, isym)
2212
722
  || isym->st_name >= dt_strsz)
2213
46
      {
2214
46
  bfd_set_error (bfd_error_invalid_operation);
2215
46
  goto error_return;
2216
46
      }
2217
2218
180
  if (dt_versym)
2219
121
    {
2220
      /* Swap in DT_VERSYM.  */
2221
121
      if (_bfd_mul_overflow (symcount, 2, &amt))
2222
0
  {
2223
0
    bfd_set_error (bfd_error_file_too_big);
2224
0
    goto error_return;
2225
0
  }
2226
2227
121
      filepos = offset_from_vma (phdrs, phnum, dt_versym, amt, NULL);
2228
121
      if (filepos == (file_ptr) -1
2229
101
    || bfd_seek (abfd, filepos, SEEK_SET) != 0)
2230
39
  goto error_return;
2231
2232
      /* DT_VERSYM info must be valid until ABFD is closed.  */
2233
82
      versym = _bfd_mmap_persistent (abfd, amt);
2234
2235
82
      if (dt_verdef)
2236
31
  {
2237
    /* Read in DT_VERDEF.  */
2238
31
    filepos = offset_from_vma (phdrs, phnum, dt_verdef,
2239
31
             0, &verdef_size);
2240
31
    if (filepos == (file_ptr) -1
2241
12
        || bfd_seek (abfd, filepos, SEEK_SET) != 0)
2242
28
      goto error_return;
2243
2244
    /* DT_VERDEF info must be valid until ABFD is closed.  */
2245
3
    verdef = _bfd_mmap_persistent (abfd, verdef_size);
2246
3
  }
2247
2248
54
      if (dt_verneed)
2249
32
  {
2250
    /* Read in DT_VERNEED.  */
2251
32
    filepos = offset_from_vma (phdrs, phnum, dt_verneed,
2252
32
             0, &verneed_size);
2253
32
    if (filepos == (file_ptr) -1
2254
22
        || bfd_seek (abfd, filepos, SEEK_SET) != 0)
2255
15
      goto error_return;
2256
2257
    /* DT_VERNEED info must be valid until ABFD is closed.  */
2258
17
    verneed = _bfd_mmap_persistent (abfd, verneed_size);
2259
17
  }
2260
54
    }
2261
2262
146
 empty_gnu_hash:
2263
146
  elf_tdata (abfd)->dt_strtab = strbuf;
2264
146
  elf_tdata (abfd)->dt_strsz = dt_strsz;
2265
146
  elf_tdata (abfd)->dt_symtab = isymbuf;
2266
146
  elf_tdata (abfd)->dt_symtab_count = symcount;
2267
146
  elf_tdata (abfd)->dt_versym = versym;
2268
146
  elf_tdata (abfd)->dt_verdef = verdef;
2269
146
  elf_tdata (abfd)->dt_verneed = verneed;
2270
146
  elf_tdata (abfd)->dt_verdef_count
2271
146
    = verdef_size / sizeof (Elf_External_Verdef);
2272
146
  elf_tdata (abfd)->dt_verneed_count
2273
146
    = verneed_size / sizeof (Elf_External_Verneed);
2274
2275
146
  res = true;
2276
2277
3.75k
 error_return:
2278
  /* Restore file position for elf_object_p.  */
2279
3.75k
  if (bfd_seek (abfd, saved_filepos, SEEK_SET) != 0)
2280
0
    res = false;
2281
3.75k
  _bfd_munmap_temporary (dynbuf_addr, dynbuf_size);
2282
3.75k
  _bfd_munmap_temporary (esymbuf_addr, esymbuf_size);
2283
3.75k
  free (gnubuckets);
2284
3.75k
  free (gnuchains);
2285
3.75k
  free (mipsxlat);
2286
3.75k
  return res;
2287
146
}
2288
2289
/* Reconstruct section from dynamic symbol.  */
2290
2291
asection *
2292
_bfd_elf_get_section_from_dynamic_symbol (bfd *abfd,
2293
            Elf_Internal_Sym *isym)
2294
0
{
2295
0
  asection *sec;
2296
0
  flagword flags;
2297
2298
0
  if (!elf_use_dt_symtab_p (abfd))
2299
0
    return NULL;
2300
2301
0
  flags = SEC_ALLOC | SEC_LOAD;
2302
0
  switch (ELF_ST_TYPE (isym->st_info))
2303
0
    {
2304
0
    case STT_FUNC:
2305
0
    case STT_GNU_IFUNC:
2306
0
      sec = bfd_get_section_by_name (abfd, ".text");
2307
0
      if (sec == NULL)
2308
0
  sec = bfd_make_section_with_flags (abfd,
2309
0
             ".text",
2310
0
             flags | SEC_CODE);
2311
0
      break;
2312
0
    case STT_COMMON:
2313
0
      sec = bfd_com_section_ptr;
2314
0
      break;
2315
0
    case STT_OBJECT:
2316
0
      sec = bfd_get_section_by_name (abfd, ".data");
2317
0
      if (sec == NULL)
2318
0
  sec = bfd_make_section_with_flags (abfd,
2319
0
             ".data",
2320
0
             flags | SEC_DATA);
2321
0
      break;
2322
0
    case STT_TLS:
2323
0
      sec = bfd_get_section_by_name (abfd, ".tdata");
2324
0
      if (sec == NULL)
2325
0
  sec = bfd_make_section_with_flags (abfd,
2326
0
             ".tdata",
2327
0
             (flags
2328
0
              | SEC_DATA
2329
0
              | SEC_THREAD_LOCAL));
2330
0
      break;
2331
0
    default:
2332
0
      sec = bfd_abs_section_ptr;
2333
0
      break;
2334
0
    }
2335
2336
0
  return sec;
2337
0
}
2338
2339
/* Get version name.  If BASE_P is TRUE, return "Base" for VER_FLG_BASE
2340
   and return symbol version for symbol version itself.   */
2341
2342
const char *
2343
_bfd_elf_get_symbol_version_string (bfd *abfd, asymbol *symbol,
2344
            bool base_p,
2345
            bool *hidden)
2346
55.3k
{
2347
55.3k
  const char *version_string = NULL;
2348
55.3k
  if ((elf_dynversym (abfd) != 0
2349
4.48k
       && (elf_dynverdef (abfd) != 0 || elf_dynverref (abfd) != 0))
2350
50.9k
      || (elf_tdata (abfd)->dt_versym != NULL
2351
0
    && (elf_tdata (abfd)->dt_verdef != NULL
2352
0
        || elf_tdata (abfd)->dt_verneed != NULL)))
2353
4.40k
    {
2354
4.40k
      unsigned int vernum = ((elf_symbol_type *) symbol)->version;
2355
2356
4.40k
      *hidden = (vernum & VERSYM_HIDDEN) != 0;
2357
4.40k
      vernum &= VERSYM_VERSION;
2358
2359
4.40k
      if (vernum == 0)
2360
1.85k
  version_string = "";
2361
2.55k
      else if (vernum == 1
2362
1.88k
         && (vernum > elf_tdata (abfd)->cverdefs
2363
0
       || (elf_tdata (abfd)->verdef[0].vd_flags
2364
0
           == VER_FLG_BASE)))
2365
1.88k
  version_string = base_p ? "Base" : "";
2366
668
      else if (vernum <= elf_tdata (abfd)->cverdefs)
2367
0
  {
2368
0
    const char *nodename
2369
0
      = elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
2370
0
    version_string = "";
2371
0
    if (base_p
2372
0
        || nodename == NULL
2373
0
        || symbol->name == NULL
2374
0
        || ! streq (symbol->name, nodename))
2375
0
      version_string = nodename;
2376
0
  }
2377
668
      else
2378
668
  {
2379
668
    Elf_Internal_Verneed *t;
2380
2381
668
    version_string = _("<corrupt>");
2382
668
    for (t = elf_tdata (abfd)->verref;
2383
1.33k
         t != NULL;
2384
668
         t = t->vn_nextref)
2385
667
      {
2386
667
        Elf_Internal_Vernaux *a;
2387
2388
1.22k
        for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
2389
978
    {
2390
978
      if (a->vna_other == vernum)
2391
419
        {
2392
419
          *hidden = true;
2393
419
          version_string = a->vna_nodename;
2394
419
          break;
2395
419
        }
2396
978
    }
2397
667
      }
2398
668
  }
2399
4.40k
    }
2400
55.3k
  return version_string;
2401
55.3k
}
2402
2403
/* Display ELF-specific fields of a symbol.  */
2404
2405
void
2406
_bfd_elf_print_symbol (bfd *abfd,
2407
           void *filep,
2408
           asymbol *symbol,
2409
           bfd_print_symbol_type how)
2410
0
{
2411
0
  FILE *file = (FILE *) filep;
2412
0
  const char *symname = (symbol->name != bfd_symbol_error_name
2413
0
       ? symbol->name : _("<corrupt>"));
2414
2415
0
  switch (how)
2416
0
    {
2417
0
    case bfd_print_symbol_name:
2418
0
      fprintf (file, "%s", symname);
2419
0
      break;
2420
0
    case bfd_print_symbol_more:
2421
0
      fprintf (file, "elf ");
2422
0
      bfd_fprintf_vma (abfd, file, symbol->value);
2423
0
      fprintf (file, " %x", symbol->flags);
2424
0
      break;
2425
0
    case bfd_print_symbol_all:
2426
0
      {
2427
0
  const char *section_name;
2428
0
  const char *name = NULL;
2429
0
  elf_backend_data *bed;
2430
0
  unsigned char st_other;
2431
0
  bfd_vma val;
2432
0
  const char *version_string;
2433
0
  bool hidden;
2434
2435
0
  section_name = symbol->section ? symbol->section->name : "(*none*)";
2436
2437
0
  bed = get_elf_backend_data (abfd);
2438
0
  if (bed->elf_backend_print_symbol_all)
2439
0
    name = (*bed->elf_backend_print_symbol_all) (abfd, filep, symbol);
2440
2441
0
  if (name != NULL)
2442
0
    symname = name;
2443
0
  else
2444
0
    bfd_print_symbol_vandf (abfd, file, symbol);
2445
2446
0
  fprintf (file, " %s\t", section_name);
2447
  /* Print the "other" value for a symbol.  For common symbols,
2448
     we've already printed the size; now print the alignment.
2449
     For other symbols, we have no specified alignment, and
2450
     we've printed the address; now print the size.  */
2451
0
  if (symbol->section && bfd_is_com_section (symbol->section))
2452
0
    val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_value;
2453
0
  else
2454
0
    val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_size;
2455
0
  bfd_fprintf_vma (abfd, file, val);
2456
2457
  /* If we have version information, print it.  */
2458
0
  version_string = _bfd_elf_get_symbol_version_string (abfd,
2459
0
                   symbol,
2460
0
                   true,
2461
0
                   &hidden);
2462
0
  if (version_string)
2463
0
    {
2464
0
      if (!hidden)
2465
0
        fprintf (file, "  %-11s", version_string);
2466
0
      else
2467
0
        {
2468
0
    int i;
2469
2470
0
    fprintf (file, " (%s)", version_string);
2471
0
    for (i = 10 - strlen (version_string); i > 0; --i)
2472
0
      putc (' ', file);
2473
0
        }
2474
0
    }
2475
2476
  /* If the st_other field is not zero, print it.  */
2477
0
  st_other = ((elf_symbol_type *) symbol)->internal_elf_sym.st_other;
2478
2479
0
  switch (st_other)
2480
0
    {
2481
0
    case 0: break;
2482
0
    case STV_INTERNAL:  fprintf (file, " .internal");  break;
2483
0
    case STV_HIDDEN:    fprintf (file, " .hidden");    break;
2484
0
    case STV_PROTECTED: fprintf (file, " .protected"); break;
2485
0
    default:
2486
      /* Some other non-defined flags are also present, so print
2487
         everything hex.  */
2488
0
      fprintf (file, " 0x%02x", (unsigned int) st_other);
2489
0
    }
2490
2491
0
  fprintf (file, " %s", symname);
2492
0
      }
2493
0
      break;
2494
0
    }
2495
0
}
2496

2497
/* ELF .o/exec file reading */
2498
2499
/* Create a new bfd section from an ELF section header.  */
2500
2501
bool
2502
bfd_section_from_shdr (bfd *abfd, unsigned int shindex)
2503
1.02M
{
2504
1.02M
  Elf_Internal_Shdr *hdr;
2505
1.02M
  Elf_Internal_Ehdr *ehdr;
2506
1.02M
  elf_backend_data *bed;
2507
1.02M
  const char *name;
2508
1.02M
  bool ret = true;
2509
2510
1.02M
  if (shindex >= elf_numsections (abfd))
2511
0
    return false;
2512
2513
  /* PR17512: A corrupt ELF binary might contain a loop of sections via
2514
     sh_link or sh_info.  Detect this here, by refusing to load a
2515
     section that we are already in the process of loading.  */
2516
1.02M
  if (elf_tdata (abfd)->being_created[shindex])
2517
44
    {
2518
44
      _bfd_error_handler
2519
44
  (_("%pB: warning: loop in section dependencies detected"), abfd);
2520
44
      return false;
2521
44
    }
2522
1.02M
  elf_tdata (abfd)->being_created[shindex] = true;
2523
2524
1.02M
  hdr = elf_elfsections (abfd)[shindex];
2525
1.02M
  ehdr = elf_elfheader (abfd);
2526
1.02M
  name = bfd_elf_string_from_elf_section (abfd, ehdr->e_shstrndx,
2527
1.02M
            hdr->sh_name);
2528
1.02M
  if (name == NULL)
2529
8.73k
    goto fail;
2530
2531
1.01M
  bed = get_elf_backend_data (abfd);
2532
1.01M
  switch (hdr->sh_type)
2533
1.01M
    {
2534
71.4k
    case SHT_NULL:
2535
      /* Inactive section. Throw it away.  */
2536
71.4k
      goto success;
2537
2538
301k
    case SHT_PROGBITS:   /* Normal section with contents.  */
2539
316k
    case SHT_NOBITS:   /* .bss section.  */
2540
317k
    case SHT_HASH:   /* .hash section.  */
2541
327k
    case SHT_NOTE:   /* .note section.  */
2542
343k
    case SHT_INIT_ARRAY: /* .init_array section.  */
2543
351k
    case SHT_FINI_ARRAY: /* .fini_array section.  */
2544
354k
    case SHT_PREINIT_ARRAY: /* .preinit_array section.  */
2545
354k
    case SHT_GNU_LIBLIST: /* .gnu.liblist section.  */
2546
355k
    case SHT_GNU_HASH:   /* .gnu.hash section.  */
2547
355k
    case SHT_GNU_SFRAME: /* .sframe section.  */
2548
355k
      ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2549
355k
      goto success;
2550
2551
3.18k
    case SHT_DYNAMIC: /* Dynamic linking information.  */
2552
3.18k
      if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
2553
41
  goto fail;
2554
2555
3.14k
      if (hdr->sh_link > elf_numsections (abfd))
2556
187
  {
2557
    /* PR 10478: Accept Solaris binaries with a sh_link field
2558
       set to SHN_BEFORE (LORESERVE) or SHN_AFTER (LORESERVE+1).  */
2559
187
    switch (bfd_get_arch (abfd))
2560
187
      {
2561
129
      case bfd_arch_i386:
2562
187
      case bfd_arch_sparc:
2563
187
        if (hdr->sh_link == (SHN_LORESERVE & 0xffff)
2564
39
      || hdr->sh_link == ((SHN_LORESERVE + 1) & 0xffff))
2565
187
    break;
2566
        /* Otherwise fall through.  */
2567
0
      default:
2568
0
        goto fail;
2569
187
      }
2570
187
  }
2571
2.95k
      else if (elf_elfsections (abfd)[hdr->sh_link] == NULL)
2572
0
  goto fail;
2573
2.95k
      else if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_STRTAB)
2574
1.42k
  {
2575
1.42k
    Elf_Internal_Shdr *dynsymhdr;
2576
2577
    /* The shared libraries distributed with hpux11 have a bogus
2578
       sh_link field for the ".dynamic" section.  Find the
2579
       string table for the ".dynsym" section instead.  */
2580
1.42k
    if (elf_dynsymtab (abfd) != 0)
2581
269
      {
2582
269
        dynsymhdr = elf_elfsections (abfd)[elf_dynsymtab (abfd)];
2583
269
        hdr->sh_link = dynsymhdr->sh_link;
2584
269
      }
2585
1.15k
    else
2586
1.15k
      {
2587
1.15k
        unsigned int i, num_sec;
2588
2589
1.15k
        num_sec = elf_numsections (abfd);
2590
18.6k
        for (i = 1; i < num_sec; i++)
2591
17.6k
    {
2592
17.6k
      dynsymhdr = elf_elfsections (abfd)[i];
2593
17.6k
      if (dynsymhdr->sh_type == SHT_DYNSYM)
2594
195
        {
2595
195
          hdr->sh_link = dynsymhdr->sh_link;
2596
195
          break;
2597
195
        }
2598
17.6k
    }
2599
1.15k
      }
2600
1.42k
  }
2601
3.14k
      goto success;
2602
2603
131k
    case SHT_SYMTAB:   /* A symbol table.  */
2604
131k
      if (elf_onesymtab (abfd) == shindex)
2605
99.2k
  goto success;
2606
2607
31.9k
      if (hdr->sh_entsize != bed->s->sizeof_sym)
2608
823
  goto fail;
2609
2610
      /* Ignore a zero sized symbol table.  */
2611
31.0k
      if (hdr->sh_size < hdr->sh_entsize)
2612
2.33k
  goto success;
2613
2614
      /* sh_info must always point past the first symbol.  */
2615
28.7k
      if (hdr->sh_info == 0)
2616
131
  goto fail;
2617
2618
28.6k
      if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size)
2619
278
  goto fail;
2620
2621
      /* PR 18854: A binary might contain more than one symbol table.
2622
   Unusual, but possible.  Warn, but continue.  */
2623
28.3k
      if (elf_onesymtab (abfd) != 0)
2624
3.68k
  {
2625
3.68k
    _bfd_error_handler
2626
      /* xgettext:c-format */
2627
3.68k
      (_("%pB: warning: multiple symbol tables detected"
2628
3.68k
         " - ignoring the table in section %u"),
2629
3.68k
       abfd, shindex);
2630
3.68k
    goto success;
2631
3.68k
  }
2632
24.6k
      elf_onesymtab (abfd) = shindex;
2633
24.6k
      elf_symtab_hdr (abfd) = *hdr;
2634
24.6k
      elf_elfsections (abfd)[shindex] = hdr = & elf_symtab_hdr (abfd);
2635
24.6k
      abfd->flags |= HAS_SYMS;
2636
2637
      /* Sometimes a shared object will map in the symbol table.  If
2638
   SHF_ALLOC is set, and this is a shared object, then we also
2639
   treat this section as a BFD section.  We can not base the
2640
   decision purely on SHF_ALLOC, because that flag is sometimes
2641
   set in a relocatable object file, which would confuse the
2642
   linker.  */
2643
24.6k
      if ((hdr->sh_flags & SHF_ALLOC) != 0
2644
1.12k
    && (abfd->flags & DYNAMIC) != 0
2645
258
    && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2646
258
            shindex))
2647
1
  goto fail;
2648
2649
      /* Go looking for SHT_SYMTAB_SHNDX too, since if there is one we
2650
   can't read symbols without that section loaded as well.  It
2651
   is most likely specified by the next section header.  */
2652
24.6k
      {
2653
24.6k
  elf_section_list * entry;
2654
24.6k
  unsigned int i, num_sec;
2655
2656
26.2k
  for (entry = elf_symtab_shndx_list (abfd); entry; entry = entry->next)
2657
4.37k
    if (entry->hdr.sh_link == shindex)
2658
2.73k
      goto success;
2659
2660
21.9k
  num_sec = elf_numsections (abfd);
2661
63.4k
  for (i = shindex + 1; i < num_sec; i++)
2662
41.5k
    {
2663
41.5k
      Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
2664
2665
41.5k
      if (hdr2->sh_type == SHT_SYMTAB_SHNDX
2666
410
    && hdr2->sh_link == shindex)
2667
74
        break;
2668
41.5k
    }
2669
2670
21.9k
  if (i == num_sec)
2671
421k
    for (i = 1; i < shindex; i++)
2672
400k
      {
2673
400k
        Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
2674
2675
400k
        if (hdr2->sh_type == SHT_SYMTAB_SHNDX
2676
2.60k
      && hdr2->sh_link == shindex)
2677
408
    break;
2678
400k
      }
2679
2680
21.9k
  if (i != shindex)
2681
482
    ret = bfd_section_from_shdr (abfd, i);
2682
21.9k
  goto success;
2683
24.6k
      }
2684
2685
4.02k
    case SHT_DYNSYM:   /* A dynamic symbol table.  */
2686
4.02k
      if (elf_dynsymtab (abfd) == shindex)
2687
1.63k
  goto success;
2688
2689
2.38k
      if (hdr->sh_entsize != bed->s->sizeof_sym)
2690
332
  goto fail;
2691
2692
      /* Ignore a zero sized symbol table.  */
2693
2.05k
      if (hdr->sh_size < hdr->sh_entsize)
2694
369
  goto success;
2695
2696
      /* sh_info must always point past the first symbol.  */
2697
1.68k
      if (hdr->sh_info == 0)
2698
42
  goto fail;
2699
2700
1.64k
      if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size)
2701
118
  goto fail;
2702
2703
      /* PR 18854: A binary might contain more than one dynamic symbol table.
2704
   Unusual, but possible.  Warn, but continue.  */
2705
1.52k
      if (elf_dynsymtab (abfd) != 0)
2706
78
  {
2707
78
    _bfd_error_handler
2708
      /* xgettext:c-format */
2709
78
      (_("%pB: warning: multiple dynamic symbol tables detected"
2710
78
         " - ignoring the table in section %u"),
2711
78
       abfd, shindex);
2712
78
    goto success;
2713
78
  }
2714
1.45k
      elf_dynsymtab (abfd) = shindex;
2715
1.45k
      elf_tdata (abfd)->dynsymtab_hdr = *hdr;
2716
1.45k
      elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr;
2717
1.45k
      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.45k
      ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2722
1.45k
      goto success;
2723
2724
9.78k
    case SHT_SYMTAB_SHNDX: /* Symbol section indices when >64k sections.  */
2725
9.78k
      {
2726
9.78k
  elf_section_list * entry;
2727
2728
12.5k
  for (entry = elf_symtab_shndx_list (abfd); entry; entry = entry->next)
2729
3.34k
    if (entry->ndx == shindex)
2730
549
      goto success;
2731
2732
9.23k
  entry = bfd_alloc (abfd, sizeof (*entry));
2733
9.23k
  if (entry == NULL)
2734
0
    goto fail;
2735
9.23k
  entry->ndx = shindex;
2736
9.23k
  entry->hdr = * hdr;
2737
9.23k
  entry->next = elf_symtab_shndx_list (abfd);
2738
9.23k
  elf_symtab_shndx_list (abfd) = entry;
2739
9.23k
  elf_elfsections (abfd)[shindex] = & entry->hdr;
2740
9.23k
  goto success;
2741
9.23k
      }
2742
2743
83.6k
    case SHT_STRTAB:   /* A string table.  */
2744
83.6k
      if (hdr->bfd_section != NULL)
2745
495
  goto success;
2746
2747
83.1k
      if (ehdr->e_shstrndx == shindex)
2748
67.0k
  {
2749
67.0k
    elf_tdata (abfd)->shstrtab_hdr = *hdr;
2750
67.0k
    elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr;
2751
67.0k
    goto success;
2752
67.0k
  }
2753
2754
16.1k
      if (elf_elfsections (abfd)[elf_onesymtab (abfd)]->sh_link == shindex)
2755
3.76k
  {
2756
8.89k
  symtab_strtab:
2757
8.89k
    elf_tdata (abfd)->strtab_hdr = *hdr;
2758
8.89k
    elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->strtab_hdr;
2759
8.89k
    goto success;
2760
3.76k
  }
2761
2762
12.3k
      if (elf_elfsections (abfd)[elf_dynsymtab (abfd)]->sh_link == shindex)
2763
517
  {
2764
1.04k
  dynsymtab_strtab:
2765
1.04k
    elf_tdata (abfd)->dynstrtab_hdr = *hdr;
2766
1.04k
    hdr = &elf_tdata (abfd)->dynstrtab_hdr;
2767
1.04k
    elf_elfsections (abfd)[shindex] = hdr;
2768
    /* We also treat this as a regular section, so that objcopy
2769
       can handle it.  */
2770
1.04k
    ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name,
2771
1.04k
             shindex);
2772
1.04k
    goto success;
2773
517
  }
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
11.8k
      if (elf_onesymtab (abfd) == 0 || elf_dynsymtab (abfd) == 0)
2779
11.7k
  {
2780
11.7k
    unsigned int i, num_sec;
2781
2782
11.7k
    num_sec = elf_numsections (abfd);
2783
130k
    for (i = 1; i < num_sec; i++)
2784
125k
      {
2785
125k
        Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
2786
125k
        if (hdr2->sh_link == shindex)
2787
10.9k
    {
2788
      /* Prevent endless recursion on broken objects.  */
2789
10.9k
      if (i == shindex)
2790
125
        goto fail;
2791
10.8k
      if (! bfd_section_from_shdr (abfd, i))
2792
408
        goto fail;
2793
10.4k
      if (elf_onesymtab (abfd) == i)
2794
5.12k
        goto symtab_strtab;
2795
5.33k
      if (elf_dynsymtab (abfd) == i)
2796
531
        goto dynsymtab_strtab;
2797
5.33k
    }
2798
125k
      }
2799
11.7k
  }
2800
5.67k
      ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2801
5.67k
      goto success;
2802
2803
24.0k
    case SHT_REL:
2804
118k
    case SHT_RELA:
2805
119k
    case SHT_RELR:
2806
      /* *These* do a lot of work -- but usually build no sections!  */
2807
119k
      {
2808
119k
  asection *target_sect;
2809
119k
  Elf_Internal_Shdr *hdr2, **p_hdr;
2810
119k
  unsigned int num_sec = elf_numsections (abfd);
2811
119k
  struct bfd_elf_section_data *esdt;
2812
119k
  bfd_size_type size;
2813
119k
  const char *bad_field;
2814
119k
  unsigned int bad_val;
2815
2816
119k
  if (hdr->sh_type == SHT_REL)
2817
24.0k
    size = bed->s->sizeof_rel;
2818
95.1k
  else if (hdr->sh_type == SHT_RELA)
2819
94.7k
    size = bed->s->sizeof_rela;
2820
408
  else
2821
408
    size = bed->s->arch_size / 8;
2822
119k
  if (hdr->sh_entsize != size)
2823
1.00k
    goto fail;
2824
2825
  /* Get the symbol table.  */
2826
118k
  if (hdr->sh_link < num_sec
2827
118k
      && (elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB
2828
25.6k
    || elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_DYNSYM)
2829
93.8k
      && ! bfd_section_from_shdr (abfd, hdr->sh_link))
2830
454
    goto fail;
2831
2832
  /* If this is an alloc section in an executable or shared
2833
     library, or the reloc section does not use the main symbol
2834
     table we don't treat it as a reloc section.  BFD can't
2835
     adequately represent such a section, so at least for now,
2836
     we don't try.  We just present it as a normal section.  We
2837
     also can't use it as a reloc section if it points to the
2838
     null section, an invalid section, another reloc section, or
2839
     its sh_link points to the null section.  */
2840
117k
  bad_field = NULL;
2841
117k
  bad_val = 0;
2842
117k
  if (((abfd->flags & (DYNAMIC | EXEC_P)) != 0
2843
7.30k
       && (hdr->sh_flags & SHF_ALLOC) != 0)
2844
116k
      || (hdr->sh_flags & SHF_COMPRESSED) != 0
2845
113k
      || hdr->sh_type == SHT_RELR)
2846
4.56k
    bad_field = "";
2847
113k
  else if (hdr->sh_link == SHN_UNDEF
2848
110k
     || hdr->sh_link != elf_onesymtab (abfd))
2849
26.0k
    {
2850
26.0k
      bad_field = "sh_link";
2851
26.0k
      bad_val = hdr->sh_link;
2852
26.0k
    }
2853
87.1k
  else if (hdr->sh_info == SHN_UNDEF
2854
86.3k
     || hdr->sh_info >= num_sec
2855
86.3k
     || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_REL
2856
86.1k
     || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_RELA)
2857
1.78k
    {
2858
1.78k
      bad_field = "sh_info";
2859
1.78k
      bad_val = hdr->sh_info;
2860
1.78k
    }
2861
2862
117k
  if (bad_field)
2863
32.3k
    {
2864
32.3k
      if (*bad_field)
2865
27.8k
        _bfd_error_handler
2866
    /* xgettext:c-format */
2867
27.8k
    (_("%pB: not using relocs in %s (index %u) due to %s %u"),
2868
27.8k
     abfd, name, shindex, bad_field, bad_val);
2869
32.3k
      ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2870
32.3k
      goto success;
2871
32.3k
    }
2872
2873
85.3k
  if (! bfd_section_from_shdr (abfd, hdr->sh_info))
2874
131
    goto fail;
2875
2876
85.2k
  target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info);
2877
85.2k
  if (target_sect == NULL)
2878
190
    goto fail;
2879
2880
85.0k
  esdt = elf_section_data (target_sect);
2881
85.0k
  if (hdr->sh_type == SHT_RELA)
2882
68.1k
    p_hdr = &esdt->rela.hdr;
2883
16.8k
  else
2884
16.8k
    p_hdr = &esdt->rel.hdr;
2885
2886
  /* PR 17512: file: 0b4f81b7.
2887
     Also see PR 24456, for a file which deliberately has two reloc
2888
     sections.  */
2889
85.0k
  if (*p_hdr != NULL)
2890
1.71k
    {
2891
1.71k
      if (!bed->init_secondary_reloc_section (abfd, hdr, name, shindex))
2892
247
        {
2893
247
    _bfd_error_handler
2894
      /* xgettext:c-format */
2895
247
      (_("%pB: warning: secondary relocation section '%s' "
2896
247
         "for section %pA found - ignoring"),
2897
247
       abfd, name, target_sect);
2898
247
        }
2899
1.47k
      else
2900
1.47k
        esdt->has_secondary_relocs = true;
2901
1.71k
      goto success;
2902
1.71k
    }
2903
2904
83.3k
  hdr2 = bfd_alloc (abfd, sizeof (*hdr2));
2905
83.3k
  if (hdr2 == NULL)
2906
0
    goto fail;
2907
83.3k
  *hdr2 = *hdr;
2908
83.3k
  *p_hdr = hdr2;
2909
83.3k
  elf_elfsections (abfd)[shindex] = hdr2;
2910
83.3k
  target_sect->reloc_count += (NUM_SHDR_ENTRIES (hdr)
2911
83.3k
             * bed->s->int_rels_per_ext_rel);
2912
83.3k
  target_sect->flags |= SEC_RELOC;
2913
83.3k
  target_sect->relocation = NULL;
2914
83.3k
  target_sect->rel_filepos = hdr->sh_offset;
2915
  /* In the section to which the relocations apply, mark whether
2916
     its relocations are of the REL or RELA variety.  */
2917
83.3k
  if (hdr->sh_size != 0)
2918
82.3k
    {
2919
82.3k
      if (hdr->sh_type == SHT_RELA)
2920
66.0k
        target_sect->use_rela_p = 1;
2921
82.3k
    }
2922
83.3k
  abfd->flags |= HAS_RELOC;
2923
83.3k
  goto success;
2924
83.3k
      }
2925
2926
1.10k
    case SHT_GNU_verdef:
2927
1.10k
      if (hdr->sh_info != 0)
2928
853
  elf_dynverdef (abfd) = shindex;
2929
1.10k
      elf_tdata (abfd)->dynverdef_hdr = *hdr;
2930
1.10k
      ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2931
1.10k
      goto success;
2932
2933
1.36k
    case SHT_GNU_versym:
2934
1.36k
      if (hdr->sh_entsize != sizeof (Elf_External_Versym))
2935
138
  goto fail;
2936
2937
1.22k
      elf_dynversym (abfd) = shindex;
2938
1.22k
      elf_tdata (abfd)->dynversym_hdr = *hdr;
2939
1.22k
      ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2940
1.22k
      goto success;
2941
2942
1.28k
    case SHT_GNU_verneed:
2943
1.28k
      if (hdr->sh_info != 0)
2944
1.17k
  elf_dynverref (abfd) = shindex;
2945
1.28k
      elf_tdata (abfd)->dynverref_hdr = *hdr;
2946
1.28k
      ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2947
1.28k
      goto success;
2948
2949
1.09k
    case SHT_SHLIB:
2950
1.09k
      goto success;
2951
2952
21.4k
    case SHT_GROUP:
2953
21.4k
      if (!_bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
2954
42
  goto fail;
2955
2956
21.3k
      goto success;
2957
2958
211k
    default:
2959
      /* Possibly an attributes section.  */
2960
211k
      if (get_elf_backend_data (abfd)->target_os != is_solaris
2961
    /* PR 33153: Solaris defines SHT_SUNW_cap which collides with SHT_GNU_ATTRIBUTES.  */
2962
207k
    && (hdr->sh_type == SHT_GNU_ATTRIBUTES
2963
200k
        || hdr->sh_type == bed->obj_attrs_section_type))
2964
6.91k
  {
2965
6.91k
    if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
2966
35
      goto fail;
2967
6.87k
    _bfd_elf_parse_attributes (abfd, hdr);
2968
6.87k
    goto success;
2969
6.91k
  }
2970
2971
      /* Check for any processor-specific section types.  */
2972
204k
      if (bed->elf_backend_section_from_shdr (abfd, hdr, name, shindex))
2973
189k
  goto success;
2974
2975
15.5k
      if (hdr->sh_type >= SHT_LOUSER && hdr->sh_type <= SHT_HIUSER)
2976
6.53k
  {
2977
6.53k
    if ((hdr->sh_flags & SHF_ALLOC) != 0)
2978
      /* FIXME: How to properly handle allocated section reserved
2979
         for applications?  */
2980
437
      _bfd_error_handler
2981
        /* xgettext:c-format */
2982
437
        (_("%pB: unknown type [%#x] section `%s'"),
2983
437
         abfd, hdr->sh_type, name);
2984
6.09k
    else
2985
6.09k
      {
2986
        /* Allow sections reserved for applications.  */
2987
6.09k
        ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
2988
6.09k
        goto success;
2989
6.09k
      }
2990
6.53k
  }
2991
8.96k
      else if (hdr->sh_type >= SHT_LOPROC
2992
1.35k
         && hdr->sh_type <= SHT_HIPROC)
2993
  /* FIXME: We should handle this section.  */
2994
1.35k
  _bfd_error_handler
2995
    /* xgettext:c-format */
2996
1.35k
    (_("%pB: unknown type [%#x] section `%s'"),
2997
1.35k
     abfd, hdr->sh_type, name);
2998
7.60k
      else if (hdr->sh_type >= SHT_LOOS && hdr->sh_type <= SHT_HIOS)
2999
4.39k
  {
3000
    /* Unrecognised OS-specific sections.  */
3001
4.39k
    if ((hdr->sh_flags & SHF_OS_NONCONFORMING) != 0)
3002
      /* SHF_OS_NONCONFORMING indicates that special knowledge is
3003
         required to correctly process the section and the file should
3004
         be rejected with an error message.  */
3005
255
      _bfd_error_handler
3006
        /* xgettext:c-format */
3007
255
        (_("%pB: unknown type [%#x] section `%s'"),
3008
255
         abfd, hdr->sh_type, name);
3009
4.14k
    else
3010
4.14k
      {
3011
        /* Otherwise it should be processed.  */
3012
4.14k
        ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
3013
4.14k
        goto success;
3014
4.14k
      }
3015
4.39k
  }
3016
3.20k
      else
3017
  /* FIXME: We should handle this section.  */
3018
3.20k
  _bfd_error_handler
3019
    /* xgettext:c-format */
3020
3.20k
    (_("%pB: unknown type [%#x] section `%s'"),
3021
3.20k
     abfd, hdr->sh_type, name);
3022
3023
5.26k
      goto fail;
3024
1.01M
    }
3025
3026
18.2k
 fail:
3027
18.2k
  ret = false;
3028
1.02M
 success:
3029
1.02M
  elf_tdata (abfd)->being_created[shindex] = false;
3030
1.02M
  return ret;
3031
18.2k
}
3032
3033
/* Return the local symbol specified by ABFD, R_SYMNDX.  */
3034
3035
Elf_Internal_Sym *
3036
bfd_sym_from_r_symndx (struct sym_cache *cache,
3037
           bfd *abfd,
3038
           unsigned long r_symndx)
3039
0
{
3040
0
  unsigned int ent = r_symndx % LOCAL_SYM_CACHE_SIZE;
3041
3042
0
  if (cache->abfd != abfd || cache->indx[ent] != r_symndx)
3043
0
    {
3044
0
      Elf_Internal_Shdr *symtab_hdr;
3045
0
      unsigned char esym[sizeof (Elf64_External_Sym)];
3046
0
      Elf_External_Sym_Shndx eshndx;
3047
3048
0
      symtab_hdr = &elf_symtab_hdr (abfd);
3049
0
      if (bfd_elf_get_elf_syms (abfd, symtab_hdr, 1, r_symndx,
3050
0
        &cache->sym[ent], esym, &eshndx) == NULL)
3051
0
  return NULL;
3052
3053
0
      if (cache->abfd != abfd)
3054
0
  {
3055
0
    memset (cache->indx, -1, sizeof (cache->indx));
3056
0
    cache->abfd = abfd;
3057
0
  }
3058
0
      cache->indx[ent] = r_symndx;
3059
0
    }
3060
3061
0
  return &cache->sym[ent];
3062
0
}
3063
3064
/* Given an ELF section number, retrieve the corresponding BFD
3065
   section.  */
3066
3067
asection *
3068
bfd_section_from_elf_index (bfd *abfd, unsigned int sec_index)
3069
178k
{
3070
178k
  if (sec_index >= elf_numsections (abfd))
3071
31.8k
    return NULL;
3072
146k
  return elf_elfsections (abfd)[sec_index]->bfd_section;
3073
178k
}
3074
3075
static const struct bfd_elf_special_section special_sections_b[] =
3076
{
3077
  { STRING_COMMA_LEN (".bss"), -2, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE },
3078
  { NULL,       0,  0, 0,    0 }
3079
};
3080
3081
static const struct bfd_elf_special_section special_sections_c[] =
3082
{
3083
  { STRING_COMMA_LEN (".comment"), 0, SHT_PROGBITS, 0 },
3084
  { STRING_COMMA_LEN (".ctf"),  0, SHT_PROGBITS,    0 },
3085
  { NULL,     0, 0, 0,      0 }
3086
};
3087
3088
static const struct bfd_elf_special_section special_sections_d[] =
3089
{
3090
  { STRING_COMMA_LEN (".data"),   -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
3091
  { STRING_COMMA_LEN (".data1"),   0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
3092
  /* There are more DWARF sections than these, but they needn't be added here
3093
     unless you have to cope with broken compilers that don't emit section
3094
     attributes or you want to help the user writing assembler.  */
3095
  { STRING_COMMA_LEN (".debug"),   0, SHT_PROGBITS, 0 },
3096
  { STRING_COMMA_LEN (".debug_line"),  0, SHT_PROGBITS, 0 },
3097
  { STRING_COMMA_LEN (".debug_info"),  0, SHT_PROGBITS, 0 },
3098
  { STRING_COMMA_LEN (".debug_abbrev"),  0, SHT_PROGBITS, 0 },
3099
  { STRING_COMMA_LEN (".debug_aranges"), 0, SHT_PROGBITS, 0 },
3100
  { STRING_COMMA_LEN (".dynamic"),   0, SHT_DYNAMIC,  SHF_ALLOC },
3101
  { STRING_COMMA_LEN (".dynstr"),  0, SHT_STRTAB,   SHF_ALLOC },
3102
  { STRING_COMMA_LEN (".dynsym"),  0, SHT_DYNSYM,   SHF_ALLOC },
3103
  { NULL,          0,  0, 0,      0 }
3104
};
3105
3106
static const struct bfd_elf_special_section special_sections_f[] =
3107
{
3108
  { STRING_COMMA_LEN (".fini"),        0, SHT_PROGBITS,   SHF_ALLOC + SHF_EXECINSTR },
3109
  { STRING_COMMA_LEN (".fini_array"), -2, SHT_FINI_ARRAY, SHF_ALLOC + SHF_WRITE },
3110
  { NULL,        0 , 0, 0,      0 }
3111
};
3112
3113
static const struct bfd_elf_special_section special_sections_g[] =
3114
{
3115
  { STRING_COMMA_LEN (".gnu.linkonce.b"), -2, SHT_NOBITS,      SHF_ALLOC + SHF_WRITE },
3116
  { STRING_COMMA_LEN (".gnu.linkonce.n"), -2, SHT_NOBITS,      SHF_ALLOC + SHF_WRITE },
3117
  { STRING_COMMA_LEN (".gnu.linkonce.p"), -2, SHT_PROGBITS,    SHF_ALLOC + SHF_WRITE },
3118
  { STRING_COMMA_LEN (".gnu.lto_"),   -1, SHT_PROGBITS,    SHF_EXCLUDE },
3119
  { STRING_COMMA_LEN (".got"),       0, SHT_PROGBITS,    SHF_ALLOC + SHF_WRITE },
3120
  { STRING_COMMA_LEN (".gnu_object_only"), 0, SHT_GNU_OBJECT_ONLY, SHF_EXCLUDE },
3121
  { STRING_COMMA_LEN (".gnu.version"),     0, SHT_GNU_versym,  0 },
3122
  { STRING_COMMA_LEN (".gnu.version_d"),   0, SHT_GNU_verdef,  0 },
3123
  { STRING_COMMA_LEN (".gnu.version_r"),   0, SHT_GNU_verneed, 0 },
3124
  { STRING_COMMA_LEN (".gnu.liblist"),     0, SHT_GNU_LIBLIST, SHF_ALLOC },
3125
  { STRING_COMMA_LEN (".gnu.conflict"),    0, SHT_RELA,        SHF_ALLOC },
3126
  { STRING_COMMA_LEN (".gnu.hash"),    0, SHT_GNU_HASH,    SHF_ALLOC },
3127
  { NULL,      0,    0, 0,         0 }
3128
};
3129
3130
static const struct bfd_elf_special_section special_sections_h[] =
3131
{
3132
  { STRING_COMMA_LEN (".hash"), 0, SHT_HASH,   SHF_ALLOC },
3133
  { NULL,        0, 0, 0,    0 }
3134
};
3135
3136
static const struct bfd_elf_special_section special_sections_i[] =
3137
{
3138
  { STRING_COMMA_LEN (".init"),        0, SHT_PROGBITS,   SHF_ALLOC + SHF_EXECINSTR },
3139
  { STRING_COMMA_LEN (".init_array"), -2, SHT_INIT_ARRAY, SHF_ALLOC + SHF_WRITE },
3140
  { STRING_COMMA_LEN (".interp"),      0, SHT_PROGBITS,   0 },
3141
  { NULL,          0,      0, 0,      0 }
3142
};
3143
3144
static const struct bfd_elf_special_section special_sections_l[] =
3145
{
3146
  { STRING_COMMA_LEN (".line"), 0, SHT_PROGBITS, 0 },
3147
  { NULL,        0, 0, 0,    0 }
3148
};
3149
3150
static const struct bfd_elf_special_section special_sections_n[] =
3151
{
3152
  { STRING_COMMA_LEN (".noinit"),  -2, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE },
3153
  { STRING_COMMA_LEN (".note.GNU-stack"), 0, SHT_NOTE,     0 },
3154
  { STRING_COMMA_LEN (".note"),    -1, SHT_NOTE,     0 },
3155
  { NULL,        0,     0, 0,      0 }
3156
};
3157
3158
static const struct bfd_elf_special_section special_sections_p[] =
3159
{
3160
  { STRING_COMMA_LEN (".persistent.bss"), 0, SHT_NOBITS,  SHF_ALLOC + SHF_WRITE },
3161
  { STRING_COMMA_LEN (".persistent"),  -2, SHT_PROGBITS,  SHF_ALLOC + SHF_WRITE },
3162
  { STRING_COMMA_LEN (".preinit_array"), -2, SHT_PREINIT_ARRAY, SHF_ALLOC + SHF_WRITE },
3163
  { STRING_COMMA_LEN (".plt"),      0, SHT_PROGBITS,  SHF_ALLOC + SHF_EXECINSTR },
3164
  { NULL,       0,      0, 0,     0 }
3165
};
3166
3167
static const struct bfd_elf_special_section special_sections_r[] =
3168
{
3169
  { STRING_COMMA_LEN (".rodata"), -2, SHT_PROGBITS, SHF_ALLOC },
3170
  { STRING_COMMA_LEN (".rodata1"), 0, SHT_PROGBITS, SHF_ALLOC },
3171
  { STRING_COMMA_LEN (".relr.dyn"), 0, SHT_RELR, SHF_ALLOC },
3172
  { STRING_COMMA_LEN (".rela"),   -1, SHT_RELA,     0 },
3173
  /* .relro_padding is generated by lld.  It should not be confused with a
3174
     reloc containing section, because otherwise elf_fake_sections() will
3175
     set the entsize to 8, which may not be an actual multiple of the
3176
     section's size.
3177
     Note - this entry must appear before the ".rel" entry below.  */
3178
  { STRING_COMMA_LEN (".relro_padding"), 0, SHT_NOBITS, SHF_ALLOC | SHF_WRITE },
3179
  { STRING_COMMA_LEN (".rel"),    -1, SHT_REL,      0 },
3180
  { NULL,       0,     0, 0,      0 }
3181
};
3182
3183
static const struct bfd_elf_special_section special_sections_s[] =
3184
{
3185
  { STRING_COMMA_LEN (".shstrtab"), 0, SHT_STRTAB, 0 },
3186
  { STRING_COMMA_LEN (".strtab"),   0, SHT_STRTAB, 0 },
3187
  { STRING_COMMA_LEN (".symtab"),   0, SHT_SYMTAB, 0 },
3188
  /* See struct bfd_elf_special_section declaration for the semantics of
3189
     this special case where .prefix_length != strlen (.prefix).  */
3190
  { ".stabstr",     5,  3, SHT_STRTAB, 0 },
3191
  { NULL,     0,  0, 0,    0 }
3192
};
3193
3194
static const struct bfd_elf_special_section special_sections_t[] =
3195
{
3196
  { STRING_COMMA_LEN (".text"),  -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
3197
  { STRING_COMMA_LEN (".tbss"),  -2, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE + SHF_TLS },
3198
  { STRING_COMMA_LEN (".tdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS },
3199
  { NULL,         0,  0, 0,      0 }
3200
};
3201
3202
static const struct bfd_elf_special_section special_sections_z[] =
3203
{
3204
  { STRING_COMMA_LEN (".zdebug_line"),    0, SHT_PROGBITS, 0 },
3205
  { STRING_COMMA_LEN (".zdebug_info"),    0, SHT_PROGBITS, 0 },
3206
  { STRING_COMMA_LEN (".zdebug_abbrev"),  0, SHT_PROGBITS, 0 },
3207
  { STRING_COMMA_LEN (".zdebug_aranges"), 0, SHT_PROGBITS, 0 },
3208
  { NULL,         0,  0, 0,      0 }
3209
};
3210
3211
static const struct bfd_elf_special_section * const special_sections[] =
3212
{
3213
  special_sections_b,   /* 'b' */
3214
  special_sections_c,   /* 'c' */
3215
  special_sections_d,   /* 'd' */
3216
  NULL,       /* 'e' */
3217
  special_sections_f,   /* 'f' */
3218
  special_sections_g,   /* 'g' */
3219
  special_sections_h,   /* 'h' */
3220
  special_sections_i,   /* 'i' */
3221
  NULL,       /* 'j' */
3222
  NULL,       /* 'k' */
3223
  special_sections_l,   /* 'l' */
3224
  NULL,       /* 'm' */
3225
  special_sections_n,   /* 'n' */
3226
  NULL,       /* 'o' */
3227
  special_sections_p,   /* 'p' */
3228
  NULL,       /* 'q' */
3229
  special_sections_r,   /* 'r' */
3230
  special_sections_s,   /* 's' */
3231
  special_sections_t,   /* 't' */
3232
  NULL,       /* 'u' */
3233
  NULL,       /* 'v' */
3234
  NULL,       /* 'w' */
3235
  NULL,       /* 'x' */
3236
  NULL,       /* 'y' */
3237
  special_sections_z    /* 'z' */
3238
};
3239
3240
const struct bfd_elf_special_section *
3241
_bfd_elf_get_special_section (const char *name,
3242
            const struct bfd_elf_special_section *spec,
3243
            unsigned int rela)
3244
949k
{
3245
949k
  int i;
3246
949k
  int len;
3247
3248
949k
  len = strlen (name);
3249
3250
5.47M
  for (i = 0; spec[i].prefix != NULL; i++)
3251
4.66M
    {
3252
4.66M
      int suffix_len;
3253
4.66M
      int prefix_len = spec[i].prefix_length;
3254
3255
4.66M
      if (len < prefix_len)
3256
2.51M
  continue;
3257
2.15M
      if (memcmp (name, spec[i].prefix, prefix_len) != 0)
3258
1.92M
  continue;
3259
3260
222k
      suffix_len = spec[i].suffix_length;
3261
222k
      if (suffix_len <= 0)
3262
219k
  {
3263
219k
    if (name[prefix_len] != 0)
3264
127k
      {
3265
127k
        if (suffix_len == 0)
3266
66.3k
    continue;
3267
60.8k
        if (name[prefix_len] != '.'
3268
11.3k
      && (suffix_len == -2
3269
5.58k
          || (rela && spec[i].type == SHT_REL)))
3270
7.54k
    continue;
3271
60.8k
      }
3272
219k
  }
3273
2.32k
      else
3274
2.32k
  {
3275
2.32k
    if (len < prefix_len + suffix_len)
3276
1.09k
      continue;
3277
1.23k
    if (memcmp (name + len - suffix_len,
3278
1.23k
          spec[i].prefix + prefix_len,
3279
1.23k
          suffix_len) != 0)
3280
1.00k
      continue;
3281
1.23k
  }
3282
146k
      return &spec[i];
3283
222k
    }
3284
3285
803k
  return NULL;
3286
949k
}
3287
3288
const struct bfd_elf_special_section *
3289
_bfd_elf_get_sec_type_attr (bfd *abfd, asection *sec)
3290
1.63M
{
3291
1.63M
  int i;
3292
1.63M
  const struct bfd_elf_special_section *spec;
3293
1.63M
  elf_backend_data *bed;
3294
3295
  /* See if this is one of the special sections.  */
3296
1.63M
  if (sec->name == NULL)
3297
0
    return NULL;
3298
3299
1.63M
  bed = get_elf_backend_data (abfd);
3300
1.63M
  spec = bed->special_sections;
3301
1.63M
  if (spec)
3302
700k
    {
3303
700k
      spec = _bfd_elf_get_special_section (sec->name,
3304
700k
             bed->special_sections,
3305
700k
             sec->use_rela_p);
3306
700k
      if (spec != NULL)
3307
2.25k
  return spec;
3308
700k
    }
3309
3310
1.63M
  if (sec->name[0] != '.')
3311
1.39M
    return NULL;
3312
3313
239k
  i = sec->name[1] - 'b';
3314
239k
  if (i < 0 || i > 'z' - 'b')
3315
11.8k
    return NULL;
3316
3317
227k
  spec = special_sections[i];
3318
3319
227k
  if (spec == NULL)
3320
12.6k
    return NULL;
3321
3322
214k
  return _bfd_elf_get_special_section (sec->name, spec, sec->use_rela_p);
3323
227k
}
3324
3325
bool
3326
_bfd_elf_new_section_hook (bfd *abfd, asection *sec)
3327
1.63M
{
3328
1.63M
  struct bfd_elf_section_data *sdata;
3329
1.63M
  elf_backend_data *bed;
3330
1.63M
  const struct bfd_elf_special_section *ssect;
3331
3332
1.63M
  sdata = sec->used_by_bfd;
3333
1.63M
  if (sdata == NULL)
3334
1.13M
    {
3335
1.13M
      sdata = bfd_zalloc (abfd, sizeof (*sdata));
3336
1.13M
      if (sdata == NULL)
3337
0
  return false;
3338
1.13M
      sec->used_by_bfd = sdata;
3339
1.13M
    }
3340
3341
  /* Indicate whether or not this section should use RELA relocations.  */
3342
1.63M
  bed = get_elf_backend_data (abfd);
3343
1.63M
  sec->use_rela_p = bed->default_use_rela_p;
3344
3345
  /* Set up ELF section type and flags for newly created sections, if
3346
     there is an ABI mandated section.  */
3347
1.63M
  ssect = (*bed->get_sec_type_attr) (abfd, sec);
3348
1.63M
  if (ssect != NULL)
3349
145k
    {
3350
145k
      elf_section_type (sec) = ssect->type;
3351
145k
      elf_section_flags (sec) = ssect->attr;
3352
145k
    }
3353
3354
1.63M
  return _bfd_generic_new_section_hook (abfd, sec);
3355
1.63M
}
3356
3357
/* Create a new bfd section from an ELF program header.
3358
3359
   Since program segments have no names, we generate a synthetic name
3360
   of the form segment<NUM>, where NUM is generally the index in the
3361
   program header table.  For segments that are split (see below) we
3362
   generate the names segment<NUM>a and segment<NUM>b.
3363
3364
   Note that some program segments may have a file size that is different than
3365
   (less than) the memory size.  All this means is that at execution the
3366
   system must allocate the amount of memory specified by the memory size,
3367
   but only initialize it with the first "file size" bytes read from the
3368
   file.  This would occur for example, with program segments consisting
3369
   of combined data+bss.
3370
3371
   To handle the above situation, this routine generates TWO bfd sections
3372
   for the single program segment.  The first has the length specified by
3373
   the file size of the segment, and the second has the length specified
3374
   by the difference between the two sizes.  In effect, the segment is split
3375
   into its initialized and uninitialized parts.  */
3376
3377
bool
3378
_bfd_elf_make_section_from_phdr (bfd *abfd,
3379
         Elf_Internal_Phdr *hdr,
3380
         int hdr_index,
3381
         const char *type_name)
3382
877k
{
3383
877k
  asection *newsect;
3384
877k
  char *name;
3385
877k
  char namebuf[64];
3386
877k
  size_t len;
3387
877k
  int split;
3388
877k
  unsigned int opb = bfd_octets_per_byte (abfd, NULL);
3389
3390
877k
  split = ((hdr->p_memsz > 0)
3391
657k
      && (hdr->p_filesz > 0)
3392
586k
      && (hdr->p_memsz > hdr->p_filesz));
3393
3394
877k
  if (hdr->p_filesz > 0)
3395
749k
    {
3396
749k
      sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "a" : "");
3397
749k
      len = strlen (namebuf) + 1;
3398
749k
      name = bfd_alloc (abfd, len);
3399
749k
      if (!name)
3400
0
  return false;
3401
749k
      memcpy (name, namebuf, len);
3402
749k
      newsect = bfd_make_section (abfd, name);
3403
749k
      if (newsect == NULL)
3404
0
  return false;
3405
749k
      newsect->vma = hdr->p_vaddr / opb;
3406
749k
      newsect->lma = hdr->p_paddr / opb;
3407
749k
      newsect->size = hdr->p_filesz;
3408
749k
      newsect->filepos = hdr->p_offset;
3409
749k
      newsect->flags |= SEC_HAS_CONTENTS;
3410
749k
      newsect->alignment_power = bfd_log2 (hdr->p_align);
3411
749k
      if (hdr->p_type == PT_LOAD)
3412
9.97k
  {
3413
9.97k
    newsect->flags |= SEC_ALLOC;
3414
9.97k
    newsect->flags |= SEC_LOAD;
3415
9.97k
    if (hdr->p_flags & PF_X)
3416
3.90k
      {
3417
        /* FIXME: all we known is that it has execute PERMISSION,
3418
     may be data.  */
3419
3.90k
        newsect->flags |= SEC_CODE;
3420
3.90k
      }
3421
9.97k
  }
3422
749k
      if (!(hdr->p_flags & PF_W))
3423
549k
  {
3424
549k
    newsect->flags |= SEC_READONLY;
3425
549k
  }
3426
749k
    }
3427
3428
877k
  if (hdr->p_memsz > hdr->p_filesz)
3429
328k
    {
3430
328k
      bfd_vma align;
3431
3432
328k
      sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "b" : "");
3433
328k
      len = strlen (namebuf) + 1;
3434
328k
      name = bfd_alloc (abfd, len);
3435
328k
      if (!name)
3436
0
  return false;
3437
328k
      memcpy (name, namebuf, len);
3438
328k
      newsect = bfd_make_section (abfd, name);
3439
328k
      if (newsect == NULL)
3440
0
  return false;
3441
328k
      newsect->vma = (hdr->p_vaddr + hdr->p_filesz) / opb;
3442
328k
      newsect->lma = (hdr->p_paddr + hdr->p_filesz) / opb;
3443
328k
      newsect->size = hdr->p_memsz - hdr->p_filesz;
3444
328k
      newsect->filepos = hdr->p_offset + hdr->p_filesz;
3445
328k
      align = newsect->vma & -newsect->vma;
3446
328k
      if (align == 0 || align > hdr->p_align)
3447
74.7k
  align = hdr->p_align;
3448
328k
      newsect->alignment_power = bfd_log2 (align);
3449
328k
      if (hdr->p_type == PT_LOAD)
3450
9.80k
  {
3451
9.80k
    newsect->flags |= SEC_ALLOC;
3452
9.80k
    if (hdr->p_flags & PF_X)
3453
2.92k
      newsect->flags |= SEC_CODE;
3454
9.80k
  }
3455
328k
      if (!(hdr->p_flags & PF_W))
3456
246k
  newsect->flags |= SEC_READONLY;
3457
328k
    }
3458
3459
877k
  return true;
3460
877k
}
3461
3462
static bool
3463
_bfd_elf_core_find_build_id (bfd *templ, bfd_vma offset)
3464
13.9k
{
3465
  /* The return value is ignored.  Build-ids are considered optional.  */
3466
13.9k
  if (templ->xvec->flavour == bfd_target_elf_flavour)
3467
13.9k
    return (*get_elf_backend_data (templ)->elf_backend_core_find_build_id)
3468
13.9k
      (templ, offset);
3469
0
  return false;
3470
13.9k
}
3471
3472
bool
3473
bfd_section_from_phdr (bfd *abfd, Elf_Internal_Phdr *hdr, int hdr_index)
3474
877k
{
3475
877k
  elf_backend_data *bed;
3476
3477
877k
  switch (hdr->p_type)
3478
877k
    {
3479
166k
    case PT_NULL:
3480
166k
      return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "null");
3481
3482
13.9k
    case PT_LOAD:
3483
13.9k
      if (! _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "load"))
3484
0
  return false;
3485
13.9k
      if (bfd_get_format (abfd) == bfd_core && abfd->build_id == NULL)
3486
13.9k
  _bfd_elf_core_find_build_id (abfd, hdr->p_offset);
3487
13.9k
      return true;
3488
3489
3.04k
    case PT_DYNAMIC:
3490
3.04k
      return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "dynamic");
3491
3492
2.80k
    case PT_INTERP:
3493
2.80k
      return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "interp");
3494
3495
2.30k
    case PT_NOTE:
3496
2.30k
      if (! _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "note"))
3497
0
  return false;
3498
2.30k
      if (! _bfd_elf_read_notes (abfd, hdr->p_offset, hdr->p_filesz,
3499
2.30k
         hdr->p_align))
3500
1.38k
  return false;
3501
921
      return true;
3502
3503
926
    case PT_SHLIB:
3504
926
      return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "shlib");
3505
3506
14.5k
    case PT_PHDR:
3507
14.5k
      return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "phdr");
3508
3509
70
    case PT_GNU_EH_FRAME:
3510
70
      return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index,
3511
70
                "eh_frame_hdr");
3512
3513
186
    case PT_GNU_STACK:
3514
186
      return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "stack");
3515
3516
305
    case PT_GNU_RELRO:
3517
305
      return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "relro");
3518
3519
29
    case PT_GNU_SFRAME:
3520
29
      return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index,
3521
29
                "sframe");
3522
3523
673k
    default:
3524
      /* Check for any processor-specific program segment types.  */
3525
673k
      bed = get_elf_backend_data (abfd);
3526
673k
      return bed->elf_backend_section_from_phdr (abfd, hdr, hdr_index, "proc");
3527
877k
    }
3528
877k
}
3529
3530
/* Return the REL_HDR for SEC, assuming there is only a single one, either
3531
   REL or RELA.  */
3532
3533
Elf_Internal_Shdr *
3534
_bfd_elf_single_rel_hdr (asection *sec)
3535
7
{
3536
7
  if (elf_section_data (sec)->rel.hdr)
3537
0
    {
3538
0
      BFD_ASSERT (elf_section_data (sec)->rela.hdr == NULL);
3539
0
      return elf_section_data (sec)->rel.hdr;
3540
0
    }
3541
7
  else
3542
7
    return elf_section_data (sec)->rela.hdr;
3543
7
}
3544
3545
static bool
3546
_bfd_elf_set_reloc_sh_name (bfd *abfd,
3547
          Elf_Internal_Shdr *rel_hdr,
3548
          const char *sec_name,
3549
          bool use_rela_p)
3550
143
{
3551
143
  char *name = bfd_alloc (abfd, sizeof ".rela" + strlen (sec_name));
3552
143
  if (name == NULL)
3553
0
    return false;
3554
3555
143
  sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", sec_name);
3556
143
  rel_hdr->sh_name =
3557
143
    (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), name,
3558
143
          false);
3559
143
  if (rel_hdr->sh_name == (unsigned int) -1)
3560
0
    return false;
3561
3562
143
  return true;
3563
143
}
3564
3565
/* Allocate and initialize a section-header for a new reloc section,
3566
   containing relocations against ASECT.  It is stored in RELDATA.  If
3567
   USE_RELA_P is TRUE, we use RELA relocations; otherwise, we use REL
3568
   relocations.  */
3569
3570
static bool
3571
_bfd_elf_init_reloc_shdr (bfd *abfd,
3572
        struct bfd_elf_section_reloc_data *reldata,
3573
        const char *sec_name,
3574
        bool use_rela_p,
3575
        bool delay_sh_name_p)
3576
143
{
3577
143
  Elf_Internal_Shdr *rel_hdr;
3578
143
  elf_backend_data *bed = get_elf_backend_data (abfd);
3579
3580
143
  BFD_ASSERT (reldata->hdr == NULL);
3581
143
  rel_hdr = bfd_zalloc (abfd, sizeof (*rel_hdr));
3582
143
  if (rel_hdr == NULL)
3583
0
    return false;
3584
143
  reldata->hdr = rel_hdr;
3585
3586
143
  if (delay_sh_name_p)
3587
0
    rel_hdr->sh_name = (unsigned int) -1;
3588
143
  else if (!_bfd_elf_set_reloc_sh_name (abfd, rel_hdr, sec_name,
3589
143
          use_rela_p))
3590
0
    return false;
3591
143
  rel_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL;
3592
143
  rel_hdr->sh_entsize = (use_rela_p
3593
143
       ? bed->s->sizeof_rela
3594
143
       : bed->s->sizeof_rel);
3595
143
  rel_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
3596
143
  rel_hdr->sh_flags = 0;
3597
143
  rel_hdr->sh_addr = 0;
3598
143
  rel_hdr->sh_size = 0;
3599
143
  rel_hdr->sh_offset = 0;
3600
3601
143
  return true;
3602
143
}
3603
3604
/* Return the default section type based on the passed in section flags.  */
3605
3606
int
3607
bfd_elf_get_default_section_type (flagword flags)
3608
1.94k
{
3609
1.94k
  if ((flags & (SEC_ALLOC | SEC_IS_COMMON)) != 0
3610
1.00k
      && (flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
3611
76
    return SHT_NOBITS;
3612
1.87k
  return SHT_PROGBITS;
3613
1.94k
}
3614
3615
struct fake_section_arg
3616
{
3617
  struct bfd_link_info *link_info;
3618
  bool failed;
3619
};
3620
3621
/* Set up an ELF internal section header for a section.  */
3622
3623
static void
3624
elf_fake_sections (bfd *abfd, asection *asect, void *fsarg)
3625
1.91k
{
3626
1.91k
  struct fake_section_arg *arg = (struct fake_section_arg *)fsarg;
3627
1.91k
  elf_backend_data *bed = get_elf_backend_data (abfd);
3628
1.91k
  struct bfd_elf_section_data *esd = elf_section_data (asect);
3629
1.91k
  Elf_Internal_Shdr *this_hdr;
3630
1.91k
  unsigned int sh_type;
3631
1.91k
  const char *name = asect->name;
3632
1.91k
  bool delay_sh_name_p = false;
3633
1.91k
  bfd_vma mask;
3634
3635
1.91k
  if (arg->failed)
3636
0
    {
3637
      /* We already failed; just get out of the bfd_map_over_sections
3638
   loop.  */
3639
0
      return;
3640
0
    }
3641
3642
1.91k
  this_hdr = &esd->this_hdr;
3643
3644
  /* ld: compress DWARF debug sections with names: .debug_*.  */
3645
1.91k
  if (arg->link_info
3646
0
      && (abfd->flags & BFD_COMPRESS) != 0
3647
0
      && (asect->flags & SEC_DEBUGGING) != 0
3648
0
      && (asect->flags & SEC_ALLOC) == 0
3649
0
      && (asect->flags & SEC_HAS_CONTENTS) != 0
3650
0
      && name[1] == 'd'
3651
0
      && name[6] == '_')
3652
0
    {
3653
      /* If this section will be compressed, delay adding section
3654
   name to section name section after it is compressed in
3655
   _bfd_elf_assign_file_positions_for_non_load.  */
3656
0
      delay_sh_name_p = true;
3657
0
    }
3658
3659
1.91k
  if (delay_sh_name_p)
3660
0
    this_hdr->sh_name = (unsigned int) -1;
3661
1.91k
  else
3662
1.91k
    {
3663
1.91k
      this_hdr->sh_name
3664
1.91k
  = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
3665
1.91k
                name, false);
3666
1.91k
      if (this_hdr->sh_name == (unsigned int) -1)
3667
0
  {
3668
0
    arg->failed = true;
3669
0
    return;
3670
0
  }
3671
1.91k
    }
3672
3673
  /* Don't clear sh_flags. Assembler may set additional bits.  */
3674
3675
1.91k
  if ((asect->flags & SEC_ALLOC) != 0
3676
911
      || asect->user_set_vma)
3677
1.91k
    this_hdr->sh_addr = asect->vma * bfd_octets_per_byte (abfd, asect);
3678
0
  else
3679
0
    this_hdr->sh_addr = 0;
3680
3681
1.91k
  this_hdr->sh_offset = 0;
3682
1.91k
  this_hdr->sh_size = asect->size;
3683
1.91k
  this_hdr->sh_link = 0;
3684
  /* PR 17512: file: 0eb809fe, 8b0535ee.  */
3685
1.91k
  if (asect->alignment_power >= (sizeof (bfd_vma) * 8) - 1)
3686
0
    {
3687
0
      _bfd_error_handler
3688
  /* xgettext:c-format */
3689
0
  (_("%pB: error: alignment power %d of section `%pA' is too big"),
3690
0
   abfd, asect->alignment_power, asect);
3691
0
      arg->failed = true;
3692
0
      return;
3693
0
    }
3694
  /* Set sh_addralign to the highest power of two given by alignment
3695
     consistent with the section VMA.  Linker scripts can force VMA.  */
3696
1.91k
  mask = ((bfd_vma) 1 << asect->alignment_power) | this_hdr->sh_addr;
3697
1.91k
  this_hdr->sh_addralign = mask & -mask;
3698
  /* The sh_entsize and sh_info fields may have been set already by
3699
     copy_private_section_data.  */
3700
3701
1.91k
  this_hdr->bfd_section = asect;
3702
1.91k
  this_hdr->contents = NULL;
3703
3704
  /* If the section type is unspecified, we set it based on
3705
     asect->flags.  */
3706
1.91k
  if (asect->type != 0)
3707
0
    sh_type = asect->type;
3708
1.91k
  else if ((asect->flags & SEC_GROUP) != 0)
3709
7
    sh_type = SHT_GROUP;
3710
1.90k
  else
3711
1.90k
    sh_type = bfd_elf_get_default_section_type (asect->flags);
3712
3713
1.91k
  if (this_hdr->sh_type == SHT_NULL)
3714
415
    this_hdr->sh_type = sh_type;
3715
1.49k
  else if (this_hdr->sh_type == SHT_NOBITS
3716
37
     && sh_type == SHT_PROGBITS
3717
4
     && (asect->flags & SEC_ALLOC) != 0)
3718
0
    {
3719
      /* Warn if we are changing a NOBITS section to PROGBITS, but
3720
   allow the link to proceed.  This can happen when users link
3721
   non-bss input sections to bss output sections, or emit data
3722
   to a bss output section via a linker script.  */
3723
0
      _bfd_error_handler
3724
0
  (_("warning: section `%pA' type changed to PROGBITS"), asect);
3725
0
      this_hdr->sh_type = sh_type;
3726
0
    }
3727
3728
1.91k
  switch (this_hdr->sh_type)
3729
1.91k
    {
3730
213
    default:
3731
213
      break;
3732
3733
213
    case SHT_STRTAB:
3734
169
    case SHT_NOTE:
3735
249
    case SHT_NOBITS:
3736
1.38k
    case SHT_PROGBITS:
3737
1.38k
      break;
3738
3739
23
    case SHT_INIT_ARRAY:
3740
48
    case SHT_FINI_ARRAY:
3741
48
    case SHT_PREINIT_ARRAY:
3742
48
      this_hdr->sh_entsize = bed->s->arch_size / 8;
3743
48
      break;
3744
3745
16
    case SHT_HASH:
3746
16
      this_hdr->sh_entsize = bed->s->sizeof_hash_entry;
3747
16
      break;
3748
3749
28
    case SHT_DYNSYM:
3750
28
      this_hdr->sh_entsize = bed->s->sizeof_sym;
3751
28
      break;
3752
3753
48
    case SHT_DYNAMIC:
3754
48
      this_hdr->sh_entsize = bed->s->sizeof_dyn;
3755
48
      break;
3756
3757
76
    case SHT_RELA:
3758
76
      if (get_elf_backend_data (abfd)->may_use_rela_p)
3759
72
  this_hdr->sh_entsize = bed->s->sizeof_rela;
3760
76
      break;
3761
3762
5
     case SHT_REL:
3763
5
      if (get_elf_backend_data (abfd)->may_use_rel_p)
3764
5
  this_hdr->sh_entsize = bed->s->sizeof_rel;
3765
5
      break;
3766
3767
18
     case SHT_GNU_versym:
3768
18
      this_hdr->sh_entsize = sizeof (Elf_External_Versym);
3769
18
      break;
3770
3771
26
     case SHT_GNU_verdef:
3772
26
      this_hdr->sh_entsize = 0;
3773
      /* objcopy or strip will copy over sh_info, but may not set
3774
   cverdefs.  The linker will set cverdefs, but sh_info will be
3775
   zero.  */
3776
26
      if (this_hdr->sh_info == 0)
3777
3
  this_hdr->sh_info = elf_tdata (abfd)->cverdefs;
3778
23
      else
3779
23
  BFD_ASSERT (elf_tdata (abfd)->cverdefs == 0
3780
26
        || this_hdr->sh_info == elf_tdata (abfd)->cverdefs);
3781
26
      break;
3782
3783
32
    case SHT_GNU_verneed:
3784
32
      this_hdr->sh_entsize = 0;
3785
      /* objcopy or strip will copy over sh_info, but may not set
3786
   cverrefs.  The linker will set cverrefs, but sh_info will be
3787
   zero.  */
3788
32
      if (this_hdr->sh_info == 0)
3789
0
  this_hdr->sh_info = elf_tdata (abfd)->cverrefs;
3790
32
      else
3791
32
  BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0
3792
32
        || this_hdr->sh_info == elf_tdata (abfd)->cverrefs);
3793
32
      break;
3794
3795
7
    case SHT_GROUP:
3796
7
      this_hdr->sh_entsize = GRP_ENTRY_SIZE;
3797
7
      break;
3798
3799
15
    case SHT_GNU_HASH:
3800
15
      this_hdr->sh_entsize = bed->s->arch_size == 64 ? 0 : 4;
3801
15
      break;
3802
1.91k
    }
3803
3804
1.91k
  if ((asect->flags & SEC_ALLOC) != 0)
3805
1.00k
    this_hdr->sh_flags |= SHF_ALLOC;
3806
1.91k
  if ((asect->flags & SEC_READONLY) == 0)
3807
435
    this_hdr->sh_flags |= SHF_WRITE;
3808
1.91k
  if ((asect->flags & SEC_CODE) != 0)
3809
301
    this_hdr->sh_flags |= SHF_EXECINSTR;
3810
1.91k
  if ((asect->flags & SEC_MERGE) != 0)
3811
143
    {
3812
143
      this_hdr->sh_flags |= SHF_MERGE;
3813
143
      this_hdr->sh_entsize = asect->entsize;
3814
143
    }
3815
1.91k
  if ((asect->flags & SEC_STRINGS) != 0)
3816
144
    {
3817
144
      this_hdr->sh_flags |= SHF_STRINGS;
3818
144
      this_hdr->sh_entsize = asect->entsize;
3819
144
    }
3820
1.91k
  if ((asect->flags & SEC_GROUP) == 0 && elf_group_name (asect) != NULL)
3821
17
    this_hdr->sh_flags |= SHF_GROUP;
3822
1.91k
  if ((asect->flags & SEC_THREAD_LOCAL) != 0)
3823
96
    {
3824
96
      this_hdr->sh_flags |= SHF_TLS;
3825
96
      if (asect->size == 0
3826
13
    && (asect->flags & SEC_HAS_CONTENTS) == 0)
3827
5
  {
3828
5
    struct bfd_link_order *o = asect->map_tail.link_order;
3829
3830
5
    this_hdr->sh_size = 0;
3831
5
    if (o != NULL)
3832
0
      {
3833
0
        this_hdr->sh_size = o->offset + o->size;
3834
0
        if (this_hdr->sh_size != 0)
3835
0
    this_hdr->sh_type = SHT_NOBITS;
3836
0
      }
3837
5
  }
3838
96
    }
3839
1.91k
  if ((asect->flags & (SEC_GROUP | SEC_EXCLUDE)) == SEC_EXCLUDE)
3840
103
    this_hdr->sh_flags |= SHF_EXCLUDE;
3841
3842
1.91k
  if (this_hdr->sh_entsize == 0)
3843
1.45k
    this_hdr->sh_entsize = asect->entsize;
3844
3845
  /* If the section has relocs, set up a section header for the
3846
     SHT_REL[A] section.  If two relocation sections are required for
3847
     this section, it is up to the processor-specific back-end to
3848
     create the other.  */
3849
1.91k
  if ((asect->flags & SEC_RELOC) != 0)
3850
143
    {
3851
      /* When doing a relocatable link, create both REL and RELA sections if
3852
   needed.  */
3853
143
      if (arg->link_info
3854
    /* Do the normal setup if we wouldn't create any sections here.  */
3855
0
    && esd->rel.count + esd->rela.count > 0
3856
0
    && (bfd_link_relocatable (arg->link_info)
3857
0
        || arg->link_info->emitrelocations))
3858
0
  {
3859
0
    if (esd->rel.count && esd->rel.hdr == NULL
3860
0
        && !_bfd_elf_init_reloc_shdr (abfd, &esd->rel, name,
3861
0
              false, delay_sh_name_p))
3862
0
      {
3863
0
        arg->failed = true;
3864
0
        return;
3865
0
      }
3866
0
    if (esd->rela.count && esd->rela.hdr == NULL
3867
0
        && !_bfd_elf_init_reloc_shdr (abfd, &esd->rela, name,
3868
0
              true, delay_sh_name_p))
3869
0
      {
3870
0
        arg->failed = true;
3871
0
        return;
3872
0
      }
3873
0
  }
3874
143
      else if (!_bfd_elf_init_reloc_shdr (abfd,
3875
143
            (asect->use_rela_p
3876
143
             ? &esd->rela : &esd->rel),
3877
143
            name,
3878
143
            asect->use_rela_p,
3879
143
            delay_sh_name_p))
3880
0
  {
3881
0
    arg->failed = true;
3882
0
    return;
3883
0
  }
3884
143
    }
3885
3886
  /* Check for processor-specific section types.  */
3887
1.91k
  sh_type = this_hdr->sh_type;
3888
1.91k
  if (bed->elf_backend_fake_sections
3889
1.52k
      && !(*bed->elf_backend_fake_sections) (abfd, this_hdr, asect))
3890
0
    {
3891
0
      arg->failed = true;
3892
0
      return;
3893
0
    }
3894
3895
1.91k
  if (sh_type == SHT_NOBITS && asect->size != 0)
3896
68
    {
3897
      /* Don't change the header type from NOBITS if we are being
3898
   called for objcopy --only-keep-debug.  */
3899
68
      this_hdr->sh_type = sh_type;
3900
68
    }
3901
1.91k
}
3902
3903
/* Fill in the contents of a SHT_GROUP section.  Called from
3904
   _bfd_elf_compute_section_file_positions for gas, objcopy, and
3905
   when ELF targets use the generic linker, ld.  Called for ld -r
3906
   from bfd_elf_final_link.  */
3907
3908
void
3909
bfd_elf_set_group_contents (bfd *abfd, asection *sec, void *failedptrarg)
3910
1.91k
{
3911
1.91k
  bool *failedptr = (bool *) failedptrarg;
3912
1.91k
  asection *elt, *first;
3913
1.91k
  unsigned char *loc;
3914
1.91k
  bool gas;
3915
3916
  /* Ignore linker created group section.  See elfNN_ia64_object_p in
3917
     elfxx-ia64.c.  */
3918
1.91k
  if ((sec->flags & (SEC_GROUP | SEC_LINKER_CREATED)) != SEC_GROUP
3919
7
      || sec->size == 0
3920
7
      || *failedptr)
3921
1.90k
    return;
3922
3923
6
  if (elf_section_data (sec)->this_hdr.sh_info == 0)
3924
6
    {
3925
6
      unsigned long symindx = 0;
3926
3927
      /* elf_group_id will have been set up by objcopy and the
3928
   generic linker.  */
3929
6
      if (elf_group_id (sec) != NULL)
3930
6
  symindx = elf_group_id (sec)->udata.i;
3931
3932
6
      if (symindx == 0)
3933
0
  {
3934
    /* If called from the assembler, swap_out_syms will have set up
3935
       elf_section_syms.
3936
       PR 25699: A corrupt input file could contain bogus group info.  */
3937
0
    if (sec->index >= elf_num_section_syms (abfd)
3938
0
        || elf_section_syms (abfd)[sec->index] == NULL)
3939
0
      {
3940
0
        *failedptr = true;
3941
0
        return;
3942
0
      }
3943
0
    symindx = elf_section_syms (abfd)[sec->index]->udata.i;
3944
0
  }
3945
6
      elf_section_data (sec)->this_hdr.sh_info = symindx;
3946
6
    }
3947
0
  else if (elf_section_data (sec)->this_hdr.sh_info == (unsigned int) -2)
3948
0
    {
3949
      /* The ELF backend linker sets sh_info to -2 when the group
3950
   signature symbol is global, and thus the index can't be
3951
   set until all local symbols are output.  */
3952
0
      asection *igroup;
3953
0
      struct bfd_elf_section_data *sec_data;
3954
0
      unsigned long symndx;
3955
0
      unsigned long extsymoff;
3956
0
      struct elf_link_hash_entry *h;
3957
3958
      /* The point of this little dance to the first SHF_GROUP section
3959
   then back to the SHT_GROUP section is that this gets us to
3960
   the SHT_GROUP in the input object.  */
3961
0
      igroup = elf_sec_group (elf_next_in_group (sec));
3962
0
      sec_data = elf_section_data (igroup);
3963
0
      symndx = sec_data->this_hdr.sh_info;
3964
0
      extsymoff = 0;
3965
0
      if (!elf_bad_symtab (igroup->owner))
3966
0
  {
3967
0
    Elf_Internal_Shdr *symtab_hdr;
3968
3969
0
    symtab_hdr = &elf_symtab_hdr (igroup->owner);
3970
0
    extsymoff = symtab_hdr->sh_info;
3971
0
  }
3972
0
      h = elf_sym_hashes (igroup->owner)[symndx - extsymoff];
3973
0
      while (h->root.type == bfd_link_hash_indirect
3974
0
       || h->root.type == bfd_link_hash_warning)
3975
0
  h = (struct elf_link_hash_entry *) h->root.u.i.link;
3976
3977
0
      elf_section_data (sec)->this_hdr.sh_info = h->indx;
3978
0
    }
3979
3980
  /* The contents won't be allocated for "ld -r" or objcopy.  */
3981
6
  gas = true;
3982
6
  if (sec->contents == NULL)
3983
6
    {
3984
6
      gas = false;
3985
6
      sec->contents = bfd_alloc (abfd, sec->size);
3986
3987
      /* Arrange for the section to be written out.  */
3988
6
      elf_section_data (sec)->this_hdr.contents = sec->contents;
3989
6
      if (sec->contents == NULL)
3990
0
  {
3991
0
    *failedptr = true;
3992
0
    return;
3993
0
  }
3994
6
      sec->alloced = 1;
3995
6
    }
3996
3997
6
  loc = sec->contents + sec->size;
3998
3999
  /* Get the pointer to the first section in the group that gas
4000
     squirreled away here.  objcopy arranges for this to be set to the
4001
     start of the input section group.  */
4002
6
  first = elt = elf_next_in_group (sec);
4003
4004
  /* First element is a flag word.  Rest of section is elf section
4005
     indices for all the sections of the group.  Write them backwards
4006
     just to keep the group in the same order as given in .section
4007
     directives, not that it matters.  */
4008
15
  while (elt != NULL)
4009
15
    {
4010
15
      asection *s;
4011
4012
15
      s = elt;
4013
15
      if (!gas)
4014
15
  s = s->output_section;
4015
15
      if (s != NULL
4016
15
    && !bfd_is_abs_section (s))
4017
15
  {
4018
15
    struct bfd_elf_section_data *elf_sec = elf_section_data (s);
4019
15
    struct bfd_elf_section_data *input_elf_sec = elf_section_data (elt);
4020
4021
15
    if (elf_sec->rel.hdr != NULL
4022
0
        && (gas
4023
0
      || (input_elf_sec->rel.hdr != NULL
4024
0
          && input_elf_sec->rel.hdr->sh_flags & SHF_GROUP) != 0))
4025
0
      {
4026
0
        elf_sec->rel.hdr->sh_flags |= SHF_GROUP;
4027
0
        loc -= 4;
4028
0
        if (loc == sec->contents)
4029
0
    break;
4030
0
        H_PUT_32 (abfd, elf_sec->rel.idx, loc);
4031
0
      }
4032
15
    if (elf_sec->rela.hdr != NULL
4033
12
        && (gas
4034
12
      || (input_elf_sec->rela.hdr != NULL
4035
12
          && input_elf_sec->rela.hdr->sh_flags & SHF_GROUP) != 0))
4036
12
      {
4037
12
        elf_sec->rela.hdr->sh_flags |= SHF_GROUP;
4038
12
        loc -= 4;
4039
12
        if (loc == sec->contents)
4040
0
    break;
4041
12
        H_PUT_32 (abfd, elf_sec->rela.idx, loc);
4042
12
      }
4043
15
    loc -= 4;
4044
15
    if (loc == sec->contents)
4045
0
      break;
4046
15
    H_PUT_32 (abfd, elf_sec->this_idx, loc);
4047
15
  }
4048
15
      elt = elf_next_in_group (elt);
4049
15
      if (elt == first)
4050
6
  break;
4051
15
    }
4052
4053
  /* We should always get here with loc == sec->contents + 4.  Return
4054
     an error for bogus SHT_GROUP sections.  */
4055
6
  loc -= 4;
4056
6
  if (loc != sec->contents)
4057
1
    {
4058
      /* xgettext:c-format */
4059
1
      _bfd_error_handler (_("%pB: corrupted group section: `%pA'"),
4060
1
        abfd, sec);
4061
1
      bfd_set_error (bfd_error_bad_value);
4062
1
      *failedptr = true;
4063
1
      return;
4064
1
    }
4065
4066
5
  H_PUT_32 (abfd, sec->flags & SEC_LINK_ONCE ? GRP_COMDAT : 0, loc);
4067
5
}
4068
4069
/* Given NAME, the name of a relocation section stripped of its
4070
   .rel/.rela prefix, return the section in ABFD to which the
4071
   relocations apply.  */
4072
4073
asection *
4074
_bfd_elf_plt_get_reloc_section (bfd *abfd, const char *name)
4075
43
{
4076
  /* If a target needs .got.plt section, relocations in rela.plt/rel.plt
4077
     section likely apply to .got.plt or .got section.  */
4078
43
  if (get_elf_backend_data (abfd)->want_got_plt
4079
21
      && streq (name, ".plt"))
4080
4
    {
4081
4
      asection *sec;
4082
4083
4
      name = ".got.plt";
4084
4
      sec = bfd_get_section_by_name (abfd, name);
4085
4
      if (sec != NULL)
4086
3
  return sec;
4087
1
      name = ".got";
4088
1
    }
4089
4090
40
  return bfd_get_section_by_name (abfd, name);
4091
43
}
4092
4093
/* Return the section to which RELOC_SEC applies.  */
4094
4095
static asection *
4096
elf_get_reloc_section (asection *reloc_sec)
4097
81
{
4098
81
  const char *name;
4099
81
  unsigned int type;
4100
81
  bfd *abfd;
4101
81
  elf_backend_data *bed;
4102
4103
81
  type = elf_section_data (reloc_sec)->this_hdr.sh_type;
4104
81
  if (type != SHT_REL && type != SHT_RELA)
4105
0
    return NULL;
4106
4107
  /* We look up the section the relocs apply to by name.  */
4108
81
  name = reloc_sec->name;
4109
81
  if (!startswith (name, ".rel"))
4110
35
    return NULL;
4111
46
  name += 4;
4112
46
  if (type == SHT_RELA && *name++ != 'a')
4113
0
    return NULL;
4114
4115
46
  abfd = reloc_sec->owner;
4116
46
  bed = get_elf_backend_data (abfd);
4117
46
  return bed->get_reloc_section (abfd, name);
4118
46
}
4119
4120
/* Assign all ELF section numbers.  The dummy first section is handled here
4121
   too.  The link/info pointers for the standard section types are filled
4122
   in here too, while we're at it.  LINK_INFO will be 0 when arriving
4123
   here for gas, objcopy, and when using the generic ELF linker.  */
4124
4125
static bool
4126
assign_section_numbers (bfd *abfd, struct bfd_link_info *link_info)
4127
687
{
4128
687
  struct elf_obj_tdata *t = elf_tdata (abfd);
4129
687
  asection *sec;
4130
687
  unsigned int section_number;
4131
687
  Elf_Internal_Shdr **i_shdrp;
4132
687
  struct bfd_elf_section_data *d;
4133
687
  bool need_symtab;
4134
4135
687
  section_number = 1;
4136
4137
687
  _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd));
4138
4139
  /* SHT_GROUP sections are in relocatable files only.  */
4140
687
  if (link_info == NULL || !link_info->resolve_section_groups)
4141
687
    {
4142
687
      size_t reloc_count = 0;
4143
4144
      /* Put SHT_GROUP sections first.  */
4145
2.59k
      for (sec = abfd->sections; sec != NULL; sec = sec->next)
4146
1.91k
  {
4147
1.91k
    d = elf_section_data (sec);
4148
4149
1.91k
    if (d->this_hdr.sh_type == SHT_GROUP)
4150
7
      {
4151
7
        if (sec->flags & SEC_LINKER_CREATED)
4152
0
    {
4153
      /* Remove the linker created SHT_GROUP sections.  */
4154
0
      bfd_section_list_remove (abfd, sec);
4155
0
      abfd->section_count--;
4156
0
    }
4157
7
        else
4158
7
    d->this_idx = section_number++;
4159
7
      }
4160
4161
    /* Count relocations.  */
4162
1.91k
    reloc_count += sec->reloc_count;
4163
1.91k
  }
4164
4165
      /* Set/clear HAS_RELOC depending on whether there are relocations.  */
4166
687
      if (reloc_count == 0)
4167
648
  abfd->flags &= ~HAS_RELOC;
4168
39
      else
4169
39
  abfd->flags |= HAS_RELOC;
4170
687
    }
4171
4172
2.59k
  for (sec = abfd->sections; sec; sec = sec->next)
4173
1.91k
    {
4174
1.91k
      d = elf_section_data (sec);
4175
4176
1.91k
      if (d->this_hdr.sh_type != SHT_GROUP)
4177
1.90k
  d->this_idx = section_number++;
4178
1.91k
      if (d->this_hdr.sh_name != (unsigned int) -1)
4179
1.91k
  _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->this_hdr.sh_name);
4180
1.91k
      if (d->rel.hdr)
4181
0
  {
4182
0
    d->rel.idx = section_number++;
4183
0
    if (d->rel.hdr->sh_name != (unsigned int) -1)
4184
0
      _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel.hdr->sh_name);
4185
0
  }
4186
1.91k
      else
4187
1.91k
  d->rel.idx = 0;
4188
4189
1.91k
      if (d->rela.hdr)
4190
143
  {
4191
143
    d->rela.idx = section_number++;
4192
143
    if (d->rela.hdr->sh_name != (unsigned int) -1)
4193
143
      _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rela.hdr->sh_name);
4194
143
  }
4195
1.76k
      else
4196
1.76k
  d->rela.idx = 0;
4197
1.91k
    }
4198
4199
687
  need_symtab = (bfd_get_symcount (abfd) > 0
4200
590
     || (link_info == NULL
4201
590
         && ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC))
4202
590
       == HAS_RELOC)));
4203
687
  if (need_symtab)
4204
97
    {
4205
97
      elf_onesymtab (abfd) = section_number++;
4206
97
      _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->symtab_hdr.sh_name);
4207
97
      if (section_number > ((SHN_LORESERVE - 2) & 0xFFFF))
4208
0
  {
4209
0
    elf_section_list *entry;
4210
4211
0
    BFD_ASSERT (elf_symtab_shndx_list (abfd) == NULL);
4212
4213
0
    entry = bfd_zalloc (abfd, sizeof (*entry));
4214
0
    if (entry == NULL)
4215
0
      return false;
4216
0
    entry->ndx = section_number++;
4217
0
    elf_symtab_shndx_list (abfd) = entry;
4218
0
    entry->hdr.sh_name = _bfd_elf_strtab_add (elf_shstrtab (abfd),
4219
0
                ".symtab_shndx", false);
4220
0
    if (entry->hdr.sh_name == -1u)
4221
0
      return false;
4222
0
  }
4223
97
      elf_strtab_sec (abfd) = section_number++;
4224
97
      _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->strtab_hdr.sh_name);
4225
97
    }
4226
4227
687
  elf_shstrtab_sec (abfd) = section_number++;
4228
687
  _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->shstrtab_hdr.sh_name);
4229
687
  elf_elfheader (abfd)->e_shstrndx = elf_shstrtab_sec (abfd);
4230
4231
687
  if (section_number >= SHN_LORESERVE)
4232
0
    {
4233
      /* xgettext:c-format */
4234
0
      _bfd_error_handler (_("%pB: too many sections: %u"),
4235
0
        abfd, section_number);
4236
0
      return false;
4237
0
    }
4238
4239
687
  elf_numsections (abfd) = section_number;
4240
687
  elf_elfheader (abfd)->e_shnum = section_number;
4241
4242
  /* Set up the list of section header pointers, in agreement with the
4243
     indices.  */
4244
687
  i_shdrp = bfd_zalloc (abfd, section_number * sizeof (*i_shdrp));
4245
687
  if (i_shdrp == NULL)
4246
0
    return false;
4247
4248
687
  i_shdrp[0] = bfd_zalloc (abfd, sizeof (*i_shdrp[0]));
4249
687
  if (i_shdrp[0] == NULL)
4250
0
    {
4251
0
      bfd_release (abfd, i_shdrp);
4252
0
      return false;
4253
0
    }
4254
4255
687
  elf_elfsections (abfd) = i_shdrp;
4256
4257
687
  i_shdrp[elf_shstrtab_sec (abfd)] = &t->shstrtab_hdr;
4258
687
  if (need_symtab)
4259
97
    {
4260
97
      i_shdrp[elf_onesymtab (abfd)] = &t->symtab_hdr;
4261
97
      if (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF))
4262
0
  {
4263
0
    elf_section_list * entry = elf_symtab_shndx_list (abfd);
4264
0
    BFD_ASSERT (entry != NULL);
4265
0
    i_shdrp[entry->ndx] = & entry->hdr;
4266
0
    entry->hdr.sh_link = elf_onesymtab (abfd);
4267
0
  }
4268
97
      i_shdrp[elf_strtab_sec (abfd)] = &t->strtab_hdr;
4269
97
      t->symtab_hdr.sh_link = elf_strtab_sec (abfd);
4270
97
    }
4271
4272
2.59k
  for (sec = abfd->sections; sec; sec = sec->next)
4273
1.91k
    {
4274
1.91k
      asection *s;
4275
4276
1.91k
      d = elf_section_data (sec);
4277
4278
1.91k
      i_shdrp[d->this_idx] = &d->this_hdr;
4279
1.91k
      if (d->rel.idx != 0)
4280
0
  i_shdrp[d->rel.idx] = d->rel.hdr;
4281
1.91k
      if (d->rela.idx != 0)
4282
143
  i_shdrp[d->rela.idx] = d->rela.hdr;
4283
4284
      /* Fill in the sh_link and sh_info fields while we're at it.  */
4285
4286
      /* sh_link of a reloc section is the section index of the symbol
4287
   table.  sh_info is the section index of the section to which
4288
   the relocation entries apply.  */
4289
1.91k
      if (d->rel.idx != 0)
4290
0
  {
4291
0
    d->rel.hdr->sh_link = elf_onesymtab (abfd);
4292
0
    d->rel.hdr->sh_info = d->this_idx;
4293
0
    d->rel.hdr->sh_flags |= SHF_INFO_LINK;
4294
0
  }
4295
1.91k
      if (d->rela.idx != 0)
4296
143
  {
4297
143
    d->rela.hdr->sh_link = elf_onesymtab (abfd);
4298
143
    d->rela.hdr->sh_info = d->this_idx;
4299
143
    d->rela.hdr->sh_flags |= SHF_INFO_LINK;
4300
143
  }
4301
4302
      /* We need to set up sh_link for SHF_LINK_ORDER.  */
4303
1.91k
      if ((d->this_hdr.sh_flags & SHF_LINK_ORDER) != 0)
4304
56
  {
4305
56
    s = elf_linked_to_section (sec);
4306
    /* We can now have a NULL linked section pointer.
4307
       This happens when the sh_link field is 0, which is done
4308
       when a linked to section is discarded but the linking
4309
       section has been retained for some reason.  */
4310
56
    if (s)
4311
7
      {
4312
        /* Check discarded linkonce section.  */
4313
7
        if (discarded_section (s))
4314
0
    {
4315
0
      asection *kept;
4316
0
      _bfd_error_handler
4317
        /* xgettext:c-format */
4318
0
        (_("%pB: sh_link of section `%pA' points to"
4319
0
           " discarded section `%pA' of `%pB'"),
4320
0
         abfd, d->this_hdr.bfd_section, s, s->owner);
4321
      /* Point to the kept section if it has the same
4322
         size as the discarded one.  */
4323
0
      kept = _bfd_elf_check_kept_section (s, link_info);
4324
0
      if (kept == NULL)
4325
0
        {
4326
0
          bfd_set_error (bfd_error_bad_value);
4327
0
          return false;
4328
0
        }
4329
0
      s = kept;
4330
0
    }
4331
        /* Handle objcopy. */
4332
7
        else if (s->output_section == NULL)
4333
0
    {
4334
0
      _bfd_error_handler
4335
        /* xgettext:c-format */
4336
0
        (_("%pB: sh_link of section `%pA' points to"
4337
0
           " removed section `%pA' of `%pB'"),
4338
0
         abfd, d->this_hdr.bfd_section, s, s->owner);
4339
0
      bfd_set_error (bfd_error_bad_value);
4340
0
      return false;
4341
0
    }
4342
7
        s = s->output_section;
4343
7
        d->this_hdr.sh_link
4344
7
    = _bfd_elf_section_from_bfd_section (abfd, s);
4345
7
      }
4346
56
  }
4347
4348
1.91k
      switch (d->this_hdr.sh_type)
4349
1.91k
  {
4350
5
  case SHT_REL:
4351
81
  case SHT_RELA:
4352
    /* sh_link is the section index of the symbol table.
4353
       sh_info is the section index of the section to which the
4354
       relocation entries apply.  */
4355
81
    if (d->this_hdr.sh_link == 0)
4356
81
      {
4357
        /* FIXME maybe: If this is a reloc section which we are
4358
     treating as a normal section then we likely should
4359
     not be assuming its sh_link is .dynsym or .symtab.  */
4360
81
        if ((sec->flags & SEC_ALLOC) != 0)
4361
28
    {
4362
28
      s = bfd_get_section_by_name (abfd, ".dynsym");
4363
28
      if (s != NULL)
4364
12
        d->this_hdr.sh_link = elf_section_data (s)->this_idx;
4365
28
    }
4366
53
        else
4367
53
    d->this_hdr.sh_link = elf_onesymtab (abfd);
4368
81
      }
4369
4370
81
    s = elf_get_reloc_section (sec);
4371
81
    if (s != NULL)
4372
31
      {
4373
31
        d->this_hdr.sh_info = elf_section_data (s)->this_idx;
4374
31
        d->this_hdr.sh_flags |= SHF_INFO_LINK;
4375
31
      }
4376
81
    break;
4377
4378
61
  case SHT_STRTAB:
4379
    /* We assume that a section named .stab*str is a stabs
4380
       string section.  We look for a section with the same name
4381
       but without the trailing ``str'', and set its sh_link
4382
       field to point to this section.  */
4383
61
    if (startswith (sec->name, ".stab")
4384
0
        && streq (sec->name + strlen (sec->name) - 3, "str"))
4385
0
      {
4386
0
        size_t len;
4387
0
        char *alc;
4388
4389
0
        len = strlen (sec->name);
4390
0
        alc = bfd_malloc (len - 2);
4391
0
        if (alc == NULL)
4392
0
    return false;
4393
0
        memcpy (alc, sec->name, len - 3);
4394
0
        alc[len - 3] = '\0';
4395
0
        s = bfd_get_section_by_name (abfd, alc);
4396
0
        free (alc);
4397
0
        if (s != NULL)
4398
0
    {
4399
0
      elf_section_data (s)->this_hdr.sh_link = d->this_idx;
4400
4401
      /* This is a .stab section.  */
4402
0
      elf_section_data (s)->this_hdr.sh_entsize = 12;
4403
0
    }
4404
0
      }
4405
61
    break;
4406
4407
61
  case SHT_DYNAMIC:
4408
76
  case SHT_DYNSYM:
4409
108
  case SHT_GNU_verneed:
4410
134
  case SHT_GNU_verdef:
4411
    /* sh_link is the section header index of the string table
4412
       used for the dynamic entries, or the symbol table, or the
4413
       version strings.  */
4414
134
    s = bfd_get_section_by_name (abfd, ".dynstr");
4415
134
    if (s != NULL)
4416
35
      d->this_hdr.sh_link = elf_section_data (s)->this_idx;
4417
134
    break;
4418
4419
0
  case SHT_GNU_LIBLIST:
4420
    /* sh_link is the section header index of the prelink library
4421
       list used for the dynamic entries, or the symbol table, or
4422
       the version strings.  */
4423
0
    s = bfd_get_section_by_name (abfd, ((sec->flags & SEC_ALLOC)
4424
0
                ? ".dynstr" : ".gnu.libstr"));
4425
0
    if (s != NULL)
4426
0
      d->this_hdr.sh_link = elf_section_data (s)->this_idx;
4427
0
    break;
4428
4429
16
  case SHT_HASH:
4430
31
  case SHT_GNU_HASH:
4431
49
  case SHT_GNU_versym:
4432
    /* sh_link is the section header index of the symbol table
4433
       this hash table or version table is for.  */
4434
49
    s = bfd_get_section_by_name (abfd, ".dynsym");
4435
49
    if (s != NULL)
4436
16
      d->this_hdr.sh_link = elf_section_data (s)->this_idx;
4437
49
    break;
4438
4439
7
  case SHT_GROUP:
4440
7
    d->this_hdr.sh_link = elf_onesymtab (abfd);
4441
1.91k
  }
4442
1.91k
    }
4443
4444
  /* Delay setting sh_name to _bfd_elf_write_object_contents so that
4445
     _bfd_elf_assign_file_positions_for_non_load can convert DWARF
4446
     debug section name from .debug_* to .zdebug_* if needed.  */
4447
4448
687
  return true;
4449
687
}
4450
4451
static bool
4452
sym_is_global (bfd *abfd, asymbol *sym)
4453
3.42k
{
4454
  /* If the backend has a special mapping, use it.  */
4455
3.42k
  elf_backend_data *bed = get_elf_backend_data (abfd);
4456
3.42k
  if (bed->elf_backend_sym_is_global)
4457
46
    return (*bed->elf_backend_sym_is_global) (abfd, sym);
4458
4459
3.38k
  return ((sym->flags & (BSF_GLOBAL | BSF_WEAK | BSF_GNU_UNIQUE)) != 0
4460
2.66k
    || bfd_is_und_section (bfd_asymbol_section (sym))
4461
1.41k
    || bfd_is_com_section (bfd_asymbol_section (sym)));
4462
3.42k
}
4463
4464
/* Filter global symbols of INFO->OUTPUT_BFD to include in the import
4465
   library.  SYMCOUNT symbols can be examined from their pointers in
4466
   SYMS.  Pointers of symbols to keep should be stored contiguously at
4467
   the beginning of that array.
4468
4469
   Returns the number of symbols to keep.  */
4470
4471
size_t
4472
_bfd_elf_filter_implib_symbols (struct bfd_link_info *info,
4473
        asymbol **syms, size_t symcount)
4474
0
{
4475
0
  size_t src_count, dst_count = 0;
4476
4477
0
  for (src_count = 0; src_count < symcount; src_count++)
4478
0
    {
4479
0
      asymbol *sym = syms[src_count];
4480
0
      const char *name = bfd_asymbol_name (sym);
4481
0
      struct bfd_link_hash_entry *h;
4482
4483
0
      if (!sym_is_global (info->output_bfd, sym))
4484
0
  continue;
4485
4486
0
      h = bfd_link_hash_lookup (info->hash, name, false, false, false);
4487
0
      if (h == NULL)
4488
0
  continue;
4489
0
      if (h->type != bfd_link_hash_defined && h->type != bfd_link_hash_defweak)
4490
0
  continue;
4491
0
      if (h->linker_def || h->ldscript_def)
4492
0
  continue;
4493
4494
0
      syms[dst_count++] = sym;
4495
0
    }
4496
4497
0
  syms[dst_count] = NULL;
4498
4499
0
  return dst_count;
4500
0
}
4501
4502
/* Don't output symbols for sections that are not going to be output,
4503
   that are duplicates or there is no BFD section.  */
4504
4505
static bool
4506
ignore_sym (asymbol *sym)
4507
5.46k
{
4508
5.46k
  if (sym == NULL)
4509
0
    return false;
4510
4511
5.46k
  if (sym->section == NULL)
4512
0
    return true;
4513
4514
5.46k
  if ((sym->flags & BSF_SECTION_SYM) != 0)
4515
2.46k
    {
4516
2.46k
      if ((sym->flags & BSF_SECTION_SYM_USED) == 0)
4517
1.92k
  return true;
4518
      /* With ld -r on generic elf targets it is possible to have
4519
   multiple section symbols in the output for a given section.
4520
   We'd like to get rid of all but the first one.  This drops
4521
   them if the first input section is non-zero size, but fails
4522
   to do so if the first input section is zero sized.  */
4523
541
      if (sym->section->output_offset != 0)
4524
0
  return true;
4525
541
    }
4526
4527
3.54k
  return discarded_section (sym->section);
4528
5.46k
}
4529
4530
/* Map symbol from it's internal number to the external number, moving
4531
   all local symbols to be at the head of the list.  */
4532
4533
static bool
4534
elf_map_symbols (bfd *abfd, unsigned int *pnum_locals)
4535
97
{
4536
97
  unsigned int symcount = bfd_get_symcount (abfd);
4537
97
  asymbol **syms = bfd_get_outsymbols (abfd);
4538
97
  asymbol **sect_syms;
4539
97
  unsigned int num_locals = 0;
4540
97
  unsigned int num_globals = 0;
4541
97
  unsigned int max_index = 0;
4542
97
  unsigned int idx;
4543
97
  asection *asect;
4544
97
  asymbol **new_syms;
4545
4546
#ifdef DEBUG
4547
  fprintf (stderr, "elf_map_symbols\n");
4548
  fflush (stderr);
4549
#endif
4550
4551
915
  for (asect = abfd->sections; asect; asect = asect->next)
4552
818
    {
4553
818
      if (max_index < asect->index)
4554
721
  max_index = asect->index;
4555
818
    }
4556
4557
97
  max_index++;
4558
97
  sect_syms = bfd_zalloc (abfd, max_index * sizeof (*sect_syms));
4559
97
  if (sect_syms == NULL)
4560
0
    return false;
4561
97
  elf_section_syms (abfd) = sect_syms;
4562
97
  elf_num_section_syms (abfd) = max_index;
4563
4564
  /* Init sect_syms entries for any section symbols we have already
4565
     decided to output.  */
4566
1.89k
  for (idx = 0; idx < symcount; idx++)
4567
1.79k
    {
4568
1.79k
      asymbol *sym = syms[idx];
4569
4570
1.79k
      if ((sym->flags & BSF_SECTION_SYM) != 0
4571
294
    && sym->value == 0
4572
240
    && !ignore_sym (sym)
4573
99
    && !bfd_is_abs_section (sym->section))
4574
86
  {
4575
86
    asection *sec = sym->section;
4576
4577
86
    if (sec->owner != abfd)
4578
86
      {
4579
86
        sec = sec->output_section;
4580
86
        if (sec == NULL)
4581
0
    return false;
4582
86
      }
4583
4584
86
    sect_syms[sec->index] = syms[idx];
4585
86
  }
4586
1.79k
    }
4587
4588
  /* Classify all of the symbols.  */
4589
1.89k
  for (idx = 0; idx < symcount; idx++)
4590
1.79k
    {
4591
1.79k
      if (ignore_sym (syms[idx]))
4592
161
  continue;
4593
1.63k
      if (sym_is_global (abfd, syms[idx]))
4594
1.01k
  num_globals++;
4595
620
      else
4596
620
  num_locals++;
4597
1.63k
    }
4598
4599
  /* We will be adding a section symbol for each normal BFD section.  Most
4600
     sections will already have a section symbol in outsymbols, but
4601
     eg. SHT_GROUP sections will not, and we need the section symbol mapped
4602
     at least in that case.  */
4603
915
  for (asect = abfd->sections; asect; asect = asect->next)
4604
818
    {
4605
818
      asymbol *sym = asect->symbol;
4606
      /* Don't include ignored section symbols.  */
4607
818
      if (!ignore_sym (sym)
4608
88
    && sect_syms[asect->index] == NULL)
4609
80
  {
4610
80
    if (sym_is_global (abfd, asect->symbol))
4611
1
      num_globals++;
4612
79
    else
4613
79
      num_locals++;
4614
80
  }
4615
818
    }
4616
4617
  /* Now sort the symbols so the local symbols are first.  */
4618
97
  new_syms = bfd_alloc (abfd, (num_locals + num_globals) * sizeof (*new_syms));
4619
97
  if (new_syms == NULL)
4620
0
    return false;
4621
4622
97
  unsigned int num_globals2 = 0;
4623
97
  unsigned int num_locals2 = 0;
4624
1.89k
  for (idx = 0; idx < symcount; idx++)
4625
1.79k
    {
4626
1.79k
      asymbol *sym = syms[idx];
4627
1.79k
      unsigned int i;
4628
4629
1.79k
      if (ignore_sym (sym))
4630
161
  continue;
4631
4632
1.63k
      if (sym_is_global (abfd, sym))
4633
1.01k
  i = num_locals + num_globals2++;
4634
620
      else
4635
620
  i = num_locals2++;
4636
1.63k
      new_syms[i] = sym;
4637
1.63k
      sym->udata.i = i + 1;
4638
1.63k
    }
4639
915
  for (asect = abfd->sections; asect; asect = asect->next)
4640
818
    {
4641
818
      asymbol *sym = asect->symbol;
4642
818
      if (!ignore_sym (sym)
4643
88
    && sect_syms[asect->index] == NULL)
4644
80
  {
4645
80
    unsigned int i;
4646
4647
80
    sect_syms[asect->index] = sym;
4648
80
    if (sym_is_global (abfd, sym))
4649
1
      i = num_locals + num_globals2++;
4650
79
    else
4651
79
      i = num_locals2++;
4652
80
    new_syms[i] = sym;
4653
80
    sym->udata.i = i + 1;
4654
80
  }
4655
818
    }
4656
4657
97
  bfd_set_symtab (abfd, new_syms, num_locals + num_globals);
4658
4659
97
  *pnum_locals = num_locals;
4660
97
  return true;
4661
97
}
4662
4663
/* Assign a file position to a section, optionally aligning to the
4664
   required section alignment.  */
4665
4666
file_ptr
4667
_bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr *i_shdrp,
4668
             file_ptr offset,
4669
             bool align,
4670
             unsigned char log_file_align)
4671
3.03k
{
4672
3.03k
  if (i_shdrp->sh_addralign > 1)
4673
989
    {
4674
989
      file_ptr salign = i_shdrp->sh_addralign & -i_shdrp->sh_addralign;
4675
4676
989
      if (align)
4677
289
  offset = BFD_ALIGN (offset, salign);
4678
700
      else if (log_file_align)
4679
700
  {
4680
    /* Heuristic: Cap alignment at log_file_align.  */
4681
700
    file_ptr falign = 1u << log_file_align;
4682
4683
700
    offset = BFD_ALIGN (offset, salign < falign ? salign : falign);
4684
700
  }
4685
989
    }
4686
3.03k
  i_shdrp->sh_offset = offset;
4687
3.03k
  if (i_shdrp->bfd_section != NULL)
4688
1.41k
    i_shdrp->bfd_section->filepos = offset;
4689
3.03k
  if (i_shdrp->sh_type != SHT_NOBITS)
4690
3.02k
    offset += i_shdrp->sh_size;
4691
3.03k
  return offset;
4692
3.03k
}
4693
4694
/* Compute the file positions we are going to put the sections at, and
4695
   otherwise prepare to begin writing out the ELF file.  If LINK_INFO
4696
   is not NULL, this is being called by the ELF backend linker.  */
4697
4698
bool
4699
_bfd_elf_compute_section_file_positions (bfd *abfd,
4700
           struct bfd_link_info *link_info)
4701
687
{
4702
687
  elf_backend_data *bed = get_elf_backend_data (abfd);
4703
687
  struct fake_section_arg fsargs;
4704
687
  bool failed;
4705
687
  struct elf_strtab_hash *strtab = NULL;
4706
687
  Elf_Internal_Shdr *shstrtab_hdr;
4707
687
  bool need_symtab;
4708
4709
687
  if (abfd->output_has_begun)
4710
0
    return true;
4711
4712
  /* Do any elf backend specific processing first.  */
4713
687
  if (bed->elf_backend_begin_write_processing)
4714
102
    (*bed->elf_backend_begin_write_processing) (abfd, link_info);
4715
4716
687
  if (!(*bed->elf_backend_init_file_header) (abfd, link_info))
4717
0
    return false;
4718
4719
687
  fsargs.failed = false;
4720
687
  fsargs.link_info = link_info;
4721
687
  bfd_map_over_sections (abfd, elf_fake_sections, &fsargs);
4722
687
  if (fsargs.failed)
4723
0
    return false;
4724
4725
687
  if (!assign_section_numbers (abfd, link_info))
4726
0
    return false;
4727
4728
  /* The backend linker builds symbol table information itself.  */
4729
687
  need_symtab = (link_info == NULL
4730
687
     && (bfd_get_symcount (abfd) > 0
4731
590
         || ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC))
4732
590
       == HAS_RELOC)));
4733
687
  if (need_symtab)
4734
97
    {
4735
      /* Non-zero if doing a relocatable link.  */
4736
97
      int relocatable_p = ! (abfd->flags & (EXEC_P | DYNAMIC));
4737
4738
97
      if (! swap_out_syms (abfd, &strtab, relocatable_p, link_info))
4739
0
  return false;
4740
97
    }
4741
4742
687
  failed = false;
4743
687
  if (link_info == NULL)
4744
687
    {
4745
687
      bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
4746
687
      if (failed)
4747
1
  goto err_free_strtab;
4748
687
    }
4749
4750
686
  shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr;
4751
  /* sh_name was set in init_file_header.  */
4752
686
  shstrtab_hdr->sh_type = SHT_STRTAB;
4753
  /* sh_flags, sh_addr, sh_entsize, sh_link, sh_info are all zeroed
4754
     when tdata is allocated.  */
4755
  /* sh_size is set in _bfd_elf_assign_file_positions_for_non_load.  */
4756
  /* sh_offset is set in _bfd_elf_assign_file_positions_for_non_load.  */
4757
686
  shstrtab_hdr->sh_addralign = 1;
4758
4759
686
  if (!assign_file_positions_except_relocs (abfd, link_info))
4760
28
    goto err_free_strtab;
4761
4762
658
  if (strtab != NULL)
4763
94
    {
4764
94
      file_ptr off;
4765
94
      Elf_Internal_Shdr *hdr;
4766
4767
94
      off = elf_next_file_pos (abfd);
4768
4769
94
      hdr = & elf_symtab_hdr (abfd);
4770
94
      off = _bfd_elf_assign_file_position_for_section (hdr, off, true, 0);
4771
4772
94
      if (elf_symtab_shndx_list (abfd) != NULL)
4773
0
  {
4774
0
    hdr = & elf_symtab_shndx_list (abfd)->hdr;
4775
0
    if (hdr->sh_size != 0)
4776
0
      off = _bfd_elf_assign_file_position_for_section (hdr, off, true, 0);
4777
    /* FIXME: What about other symtab_shndx sections in the list ?  */
4778
0
  }
4779
4780
94
      hdr = &elf_tdata (abfd)->strtab_hdr;
4781
94
      off = _bfd_elf_assign_file_position_for_section (hdr, off, true, 0);
4782
4783
94
      elf_next_file_pos (abfd) = off;
4784
4785
      /* Now that we know where the .strtab section goes, write it
4786
   out.  */
4787
94
      if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
4788
94
    || ! _bfd_elf_strtab_emit (abfd, strtab))
4789
0
  goto err_free_strtab;
4790
94
      _bfd_elf_strtab_free (strtab);
4791
94
    }
4792
4793
658
  abfd->output_has_begun = true;
4794
658
  return true;
4795
4796
29
 err_free_strtab:
4797
29
  if (strtab != NULL)
4798
3
    _bfd_elf_strtab_free (strtab);
4799
29
  return false;
4800
658
}
4801
4802
/* Retrieve .eh_frame_hdr.  Prior to size_dynamic_sections the
4803
   function effectively returns whether --eh-frame-hdr is given on the
4804
   command line.  After size_dynamic_sections the result reflects
4805
   whether .eh_frame_hdr will actually be output (sizing isn't done
4806
   until ldemul_after_allocation).  */
4807
4808
static asection *
4809
elf_eh_frame_hdr (const struct bfd_link_info *info)
4810
84
{
4811
84
  if (info != NULL && is_elf_hash_table (info->hash))
4812
0
    return elf_hash_table (info)->eh_info.hdr_sec;
4813
84
  return NULL;
4814
84
}
4815
4816
/* Make an initial estimate of the size of the program header.  If we
4817
   get the number wrong here, we'll redo section placement.  */
4818
4819
static bfd_size_type
4820
get_program_header_size (bfd *abfd, struct bfd_link_info *info)
4821
44
{
4822
44
  size_t segs;
4823
44
  asection *s;
4824
44
  elf_backend_data *bed;
4825
4826
  /* Assume we will need exactly two PT_LOAD segments: one for text
4827
     and one for data.  */
4828
44
  segs = 2;
4829
4830
44
  s = bfd_get_section_by_name (abfd, ".interp");
4831
44
  if (s != NULL && (s->flags & SEC_LOAD) != 0 && s->size != 0)
4832
0
    {
4833
      /* If we have a loadable interpreter section, we need a
4834
   PT_INTERP segment.  In this case, assume we also need a
4835
   PT_PHDR segment, although that may not be true for all
4836
   targets.  */
4837
0
      segs += 2;
4838
0
    }
4839
4840
44
  if (bfd_get_section_by_name (abfd, ".dynamic") != NULL)
4841
6
    {
4842
      /* We need a PT_DYNAMIC segment.  */
4843
6
      ++segs;
4844
6
    }
4845
4846
44
  if (info != NULL && info->relro)
4847
0
    {
4848
      /* We need a PT_GNU_RELRO segment.  */
4849
0
      ++segs;
4850
0
    }
4851
4852
44
  if (elf_eh_frame_hdr (info))
4853
0
    {
4854
      /* We need a PT_GNU_EH_FRAME segment.  */
4855
0
      ++segs;
4856
0
    }
4857
4858
44
  if (elf_stack_flags (abfd))
4859
0
    {
4860
      /* We need a PT_GNU_STACK segment.  */
4861
0
      ++segs;
4862
0
    }
4863
4864
44
  if (elf_sframe (abfd))
4865
0
    {
4866
      /* We need a PT_GNU_SFRAME segment.  */
4867
0
      ++segs;
4868
0
    }
4869
4870
44
  s = bfd_get_section_by_name (abfd,
4871
44
             NOTE_GNU_PROPERTY_SECTION_NAME);
4872
44
  if (s != NULL && s->size != 0)
4873
0
    {
4874
      /* We need a PT_GNU_PROPERTY segment.  */
4875
0
      ++segs;
4876
0
    }
4877
4878
286
  for (s = abfd->sections; s != NULL; s = s->next)
4879
242
    {
4880
242
      if ((s->flags & SEC_LOAD) != 0
4881
132
    && elf_section_type (s) == SHT_NOTE)
4882
15
  {
4883
15
    unsigned int alignment_power;
4884
    /* We need a PT_NOTE segment.  */
4885
15
    ++segs;
4886
    /* Try to create just one PT_NOTE segment for all adjacent
4887
       loadable SHT_NOTE sections.  gABI requires that within a
4888
       PT_NOTE segment (and also inside of each SHT_NOTE section)
4889
       each note should have the same alignment.  So we check
4890
       whether the sections are correctly aligned.  */
4891
15
    alignment_power = s->alignment_power;
4892
15
    while (s->next != NULL
4893
15
     && s->next->alignment_power == alignment_power
4894
11
     && (s->next->flags & SEC_LOAD) != 0
4895
7
     && elf_section_type (s->next) == SHT_NOTE)
4896
0
      s = s->next;
4897
15
  }
4898
242
    }
4899
4900
188
  for (s = abfd->sections; s != NULL; s = s->next)
4901
165
    {
4902
165
      if (s->flags & SEC_THREAD_LOCAL)
4903
21
  {
4904
    /* We need a PT_TLS segment.  */
4905
21
    ++segs;
4906
21
    break;
4907
21
  }
4908
165
    }
4909
4910
44
  bed = get_elf_backend_data (abfd);
4911
4912
44
  if ((abfd->flags & D_PAGED) != 0
4913
43
      && (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0)
4914
0
    {
4915
      /* Add a PT_GNU_MBIND segment for each mbind section.  */
4916
0
      bfd_vma commonpagesize;
4917
0
      unsigned int page_align_power;
4918
4919
0
      if (info != NULL)
4920
0
  commonpagesize = info->commonpagesize;
4921
0
      else
4922
0
  commonpagesize = bed->commonpagesize;
4923
0
      page_align_power = bfd_log2 (commonpagesize);
4924
0
      for (s = abfd->sections; s != NULL; s = s->next)
4925
0
  if (elf_section_flags (s) & SHF_GNU_MBIND)
4926
0
    {
4927
0
      if (elf_section_data (s)->this_hdr.sh_info > PT_GNU_MBIND_NUM)
4928
0
        {
4929
0
    _bfd_error_handler
4930
      /* xgettext:c-format */
4931
0
      (_("%pB: GNU_MBIND section `%pA' has invalid "
4932
0
         "sh_info field: %d"),
4933
0
       abfd, s, elf_section_data (s)->this_hdr.sh_info);
4934
0
    continue;
4935
0
        }
4936
      /* Align mbind section to page size.  */
4937
0
      if (s->alignment_power < page_align_power)
4938
0
        s->alignment_power = page_align_power;
4939
0
      segs ++;
4940
0
    }
4941
0
    }
4942
4943
  /* Let the backend count up any program headers it might need.  */
4944
44
  if (bed->elf_backend_additional_program_headers)
4945
35
    {
4946
35
      int a;
4947
4948
35
      a = (*bed->elf_backend_additional_program_headers) (abfd, info);
4949
35
      if (a == -1)
4950
0
  abort ();
4951
35
      segs += a;
4952
35
    }
4953
4954
44
  return segs * bed->s->sizeof_phdr;
4955
44
}
4956
4957
/* Find the segment that contains the output_section of section.  */
4958
4959
Elf_Internal_Phdr *
4960
_bfd_elf_find_segment_containing_section (bfd * abfd, asection * section)
4961
0
{
4962
0
  struct elf_segment_map *m;
4963
0
  Elf_Internal_Phdr *p;
4964
4965
0
  for (m = elf_seg_map (abfd), p = elf_tdata (abfd)->phdr;
4966
0
       m != NULL;
4967
0
       m = m->next, p++)
4968
0
    {
4969
0
      int i;
4970
4971
0
      for (i = m->count - 1; i >= 0; i--)
4972
0
  if (m->sections[i] == section)
4973
0
    return p;
4974
0
    }
4975
4976
0
  return NULL;
4977
0
}
4978
4979
/* Create a mapping from a set of sections to a program segment.  */
4980
4981
static struct elf_segment_map *
4982
make_mapping (bfd *abfd,
4983
        asection **sections,
4984
        unsigned int from,
4985
        unsigned int to,
4986
        bool phdr)
4987
115
{
4988
115
  struct elf_segment_map *m;
4989
115
  unsigned int i;
4990
115
  asection **hdrpp;
4991
115
  size_t amt;
4992
4993
115
  amt = sizeof (struct elf_segment_map) - sizeof (asection *);
4994
115
  amt += (to - from) * sizeof (asection *);
4995
115
  m = bfd_zalloc (abfd, amt);
4996
115
  if (m == NULL)
4997
0
    return NULL;
4998
115
  m->next = NULL;
4999
115
  m->p_type = PT_LOAD;
5000
263
  for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++)
5001
148
    m->sections[i - from] = *hdrpp;
5002
115
  m->count = to - from;
5003
5004
115
  if (from == 0 && phdr)
5005
5
    {
5006
      /* Include the headers in the first PT_LOAD segment.  */
5007
5
      m->includes_filehdr = 1;
5008
5
      m->includes_phdrs = 1;
5009
5
    }
5010
5011
115
  return m;
5012
115
}
5013
5014
/* Create the PT_DYNAMIC segment, which includes DYNSEC.  Returns NULL
5015
   on failure.  */
5016
5017
struct elf_segment_map *
5018
_bfd_elf_make_dynamic_segment (bfd *abfd, asection *dynsec)
5019
4
{
5020
4
  struct elf_segment_map *m;
5021
5022
4
  m = bfd_zalloc (abfd, sizeof (*m));
5023
4
  if (m == NULL)
5024
0
    return NULL;
5025
4
  m->next = NULL;
5026
4
  m->p_type = PT_DYNAMIC;
5027
4
  m->count = 1;
5028
4
  m->sections[0] = dynsec;
5029
5030
4
  return m;
5031
4
}
5032
5033
/* Possibly add or remove segments from the segment map.  */
5034
5035
static bool
5036
elf_modify_segment_map (bfd *abfd,
5037
      struct bfd_link_info *info,
5038
      bool remove_empty_load)
5039
395
{
5040
395
  struct elf_segment_map **m;
5041
395
  elf_backend_data *bed;
5042
5043
  /* The placement algorithm assumes that non allocated sections are
5044
     not in PT_LOAD segments.  We ensure this here by removing such
5045
     sections from the segment map.  We also remove excluded
5046
     sections.  Finally, any PT_LOAD segment without sections is
5047
     removed.  */
5048
395
  m = &elf_seg_map (abfd);
5049
1.08k
  while (*m)
5050
689
    {
5051
689
      unsigned int i, new_count;
5052
5053
2.16k
      for (new_count = 0, i = 0; i < (*m)->count; i++)
5054
1.47k
  {
5055
1.47k
    if (((*m)->sections[i]->flags & SEC_EXCLUDE) == 0
5056
1.43k
        && (((*m)->sections[i]->flags & SEC_ALLOC) != 0
5057
787
      || (*m)->p_type != PT_LOAD))
5058
1.43k
      {
5059
1.43k
        (*m)->sections[new_count] = (*m)->sections[i];
5060
1.43k
        new_count++;
5061
1.43k
      }
5062
1.47k
  }
5063
689
      (*m)->count = new_count;
5064
5065
689
      if (remove_empty_load
5066
689
    && (*m)->p_type == PT_LOAD
5067
177
    && (*m)->count == 0
5068
58
    && !(*m)->includes_phdrs)
5069
51
  *m = (*m)->next;
5070
638
      else
5071
638
  m = &(*m)->next;
5072
689
    }
5073
5074
395
  bed = get_elf_backend_data (abfd);
5075
395
  if (bed->elf_backend_modify_segment_map != NULL)
5076
155
    {
5077
155
      if (!(*bed->elf_backend_modify_segment_map) (abfd, info))
5078
0
  return false;
5079
155
    }
5080
5081
395
  return true;
5082
395
}
5083
5084
#define IS_TBSS(s) \
5085
190
  ((s->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) == SEC_THREAD_LOCAL)
5086
5087
/* Set up a mapping from BFD sections to program segments.  Update
5088
   NEED_LAYOUT if the section layout is changed.  */
5089
5090
bool
5091
bfd_elf_map_sections_to_segments (bfd *abfd,
5092
          struct bfd_link_info *info,
5093
          bool *need_layout)
5094
399
{
5095
399
  unsigned int count;
5096
399
  struct elf_segment_map *m;
5097
399
  asection **sections = NULL;
5098
399
  elf_backend_data *bed = get_elf_backend_data (abfd);
5099
399
  bool no_user_phdrs;
5100
5101
399
  no_user_phdrs = elf_seg_map (abfd) == NULL;
5102
5103
399
  if (info != NULL)
5104
0
    {
5105
0
      info->user_phdrs = !no_user_phdrs;
5106
5107
      /* Size the relative relocations if DT_RELR is enabled.  */
5108
0
      if (info->enable_dt_relr
5109
0
    && need_layout != NULL
5110
0
    && bed->size_relative_relocs
5111
0
    && !bed->size_relative_relocs (info, need_layout))
5112
0
  info->callbacks->fatal
5113
0
    (_("%P: failed to size relative relocations\n"));
5114
0
    }
5115
5116
399
  if (no_user_phdrs && bfd_count_sections (abfd) != 0)
5117
44
    {
5118
44
      asection *s;
5119
44
      unsigned int i;
5120
44
      struct elf_segment_map *mfirst;
5121
44
      struct elf_segment_map **pm;
5122
44
      asection *last_hdr;
5123
44
      bfd_vma last_size;
5124
44
      unsigned int hdr_index;
5125
44
      bfd_vma maxpagesize;
5126
44
      asection **hdrpp;
5127
44
      bool phdr_in_segment;
5128
44
      bool writable;
5129
44
      bool executable;
5130
44
      unsigned int tls_count = 0;
5131
44
      asection *first_tls = NULL;
5132
44
      asection *first_mbind = NULL;
5133
44
      asection *dynsec, *eh_frame_hdr;
5134
44
      asection *sframe;
5135
44
      size_t amt;
5136
44
      bfd_vma addr_mask, wrap_to = 0;  /* Bytes.  */
5137
44
      bfd_size_type phdr_size;  /* Octets/bytes.  */
5138
44
      unsigned int opb = bfd_octets_per_byte (abfd, NULL);
5139
5140
      /* Select the allocated sections, and sort them.  */
5141
5142
44
      sections = bfd_malloc (bfd_count_sections (abfd) * sizeof (*sections));
5143
44
      if (sections == NULL)
5144
0
  goto error_return;
5145
5146
      /* Calculate top address, avoiding undefined behaviour of shift
5147
   left operator when shift count is equal to size of type
5148
   being shifted.  */
5149
44
      addr_mask = ((bfd_vma) 1 << (bfd_arch_bits_per_address (abfd) - 1)) - 1;
5150
44
      addr_mask = (addr_mask << 1) + 1;
5151
5152
44
      i = 0;
5153
286
      for (s = abfd->sections; s != NULL; s = s->next)
5154
242
  {
5155
242
    if ((s->flags & SEC_ALLOC) != 0)
5156
150
      {
5157
        /* target_index is unused until bfd_elf_final_link
5158
     starts output of section symbols.  Use it to make
5159
     qsort stable.  */
5160
150
        s->target_index = i;
5161
150
        sections[i] = s;
5162
150
        ++i;
5163
        /* A wrapping section potentially clashes with header.  */
5164
150
        if (((s->lma + s->size / opb) & addr_mask) < (s->lma & addr_mask))
5165
1
    wrap_to = (s->lma + s->size / opb) & addr_mask;
5166
150
      }
5167
242
  }
5168
44
      BFD_ASSERT (i <= bfd_count_sections (abfd));
5169
44
      count = i;
5170
5171
44
      qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections);
5172
5173
44
      phdr_size = elf_program_header_size (abfd);
5174
44
      if (phdr_size == (bfd_size_type) -1)
5175
44
  phdr_size = get_program_header_size (abfd, info);
5176
44
      phdr_size += bed->s->sizeof_ehdr;
5177
      /* phdr_size is compared to LMA values which are in bytes.  */
5178
44
      phdr_size /= opb;
5179
44
      if (info != NULL)
5180
0
  maxpagesize = info->maxpagesize;
5181
44
      else
5182
44
  maxpagesize = bed->maxpagesize;
5183
44
      if (maxpagesize == 0)
5184
0
  maxpagesize = 1;
5185
44
      phdr_in_segment = info != NULL && info->load_phdrs;
5186
44
      if (count != 0
5187
40
    && (((sections[0]->lma & addr_mask) & (maxpagesize - 1))
5188
40
        >= (phdr_size & (maxpagesize - 1))))
5189
  /* For compatibility with old scripts that may not be using
5190
     SIZEOF_HEADERS, add headers when it looks like space has
5191
     been left for them.  */
5192
6
  phdr_in_segment = true;
5193
5194
      /* Build the mapping.  */
5195
44
      mfirst = NULL;
5196
44
      pm = &mfirst;
5197
5198
      /* If we have a .interp section, then create a PT_PHDR segment for
5199
   the program headers and a PT_INTERP segment for the .interp
5200
   section.  */
5201
44
      s = bfd_get_section_by_name (abfd, ".interp");
5202
44
      if (s != NULL && (s->flags & SEC_LOAD) != 0 && s->size != 0)
5203
0
  {
5204
0
    m = bfd_zalloc (abfd, sizeof (*m));
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
    m = bfd_zalloc (abfd, sizeof (*m));
5217
0
    if (m == NULL)
5218
0
      goto error_return;
5219
0
    m->next = NULL;
5220
0
    m->p_type = PT_INTERP;
5221
0
    m->count = 1;
5222
0
    m->sections[0] = s;
5223
5224
0
    *pm = m;
5225
0
    pm = &m->next;
5226
0
  }
5227
5228
      /* Look through the sections.  We put sections in the same program
5229
   segment when the start of the second section can be placed within
5230
   a few bytes of the end of the first section.  */
5231
44
      last_hdr = NULL;
5232
44
      last_size = 0;
5233
44
      hdr_index = 0;
5234
44
      writable = false;
5235
44
      executable = false;
5236
44
      dynsec = bfd_get_section_by_name (abfd, ".dynamic");
5237
44
      if (dynsec != NULL
5238
6
    && (dynsec->flags & SEC_LOAD) == 0)
5239
2
  dynsec = NULL;
5240
5241
44
      if ((abfd->flags & D_PAGED) == 0)
5242
1
  phdr_in_segment = false;
5243
5244
      /* Deal with -Ttext or something similar such that the first section
5245
   is not adjacent to the program headers.  This is an
5246
   approximation, since at this point we don't know exactly how many
5247
   program headers we will need.  */
5248
44
      if (phdr_in_segment && count > 0)
5249
6
  {
5250
6
    bfd_vma phdr_lma;  /* Bytes.  */
5251
6
    bool separate_phdr = false;
5252
5253
6
    phdr_lma = (sections[0]->lma - phdr_size) & addr_mask & -maxpagesize;
5254
6
    if (info != NULL
5255
0
        && info->separate_code
5256
0
        && (sections[0]->flags & SEC_CODE) != 0)
5257
0
      {
5258
        /* If data sections should be separate from code and
5259
     thus not executable, and the first section is
5260
     executable then put the file and program headers in
5261
     their own PT_LOAD.  */
5262
0
        if (!info->one_rosegment)
5263
0
    separate_phdr = true;
5264
5265
0
        if ((((phdr_lma + phdr_size - 1) & addr_mask & -maxpagesize)
5266
0
       == (sections[0]->lma & addr_mask & -maxpagesize)))
5267
0
    {
5268
      /* The file and program headers are currently on the
5269
         same page as the first section.  Put them on the
5270
         previous page if we can.  */
5271
0
      if (phdr_lma >= maxpagesize)
5272
0
        phdr_lma -= maxpagesize;
5273
0
      else
5274
0
        separate_phdr = false;
5275
0
    }
5276
0
      }
5277
6
    if ((sections[0]->lma & addr_mask) < phdr_lma
5278
5
        || (sections[0]->lma & addr_mask) < phdr_size)
5279
      /* If file and program headers would be placed at the end
5280
         of memory then it's probably better to omit them.  */
5281
1
      phdr_in_segment = false;
5282
5
    else if (phdr_lma < wrap_to)
5283
      /* If a section wraps around to where we'll be placing
5284
         file and program headers, then the headers will be
5285
         overwritten.  */
5286
0
      phdr_in_segment = false;
5287
5
    else if (separate_phdr)
5288
0
      {
5289
0
        m = make_mapping (abfd, sections, 0, 0, phdr_in_segment);
5290
0
        if (m == NULL)
5291
0
    goto error_return;
5292
0
        m->p_paddr = phdr_lma * opb;
5293
0
        m->p_vaddr_offset
5294
0
    = (sections[0]->vma - phdr_size) & addr_mask & -maxpagesize;
5295
0
        m->p_paddr_valid = 1;
5296
0
        *pm = m;
5297
0
        pm = &m->next;
5298
0
        phdr_in_segment = false;
5299
0
      }
5300
6
  }
5301
5302
194
      for (i = 0, hdrpp = sections; i < count; i++, hdrpp++)
5303
150
  {
5304
150
    asection *hdr;
5305
150
    bool new_segment;
5306
5307
150
    hdr = *hdrpp;
5308
5309
    /* See if this section and the last one will fit in the same
5310
       segment.  */
5311
5312
150
    if (last_hdr == NULL)
5313
40
      {
5314
        /* If we don't have a segment yet, then we don't need a new
5315
     one (we build the last one after this loop).  */
5316
40
        new_segment = false;
5317
40
      }
5318
110
    else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma)
5319
3
      {
5320
        /* If this section has a different relation between the
5321
     virtual address and the load address, then we need a new
5322
     segment.  */
5323
3
        new_segment = true;
5324
3
      }
5325
107
    else if (hdr->lma < last_hdr->lma + last_size
5326
73
       || last_hdr->lma + last_size < last_hdr->lma)
5327
34
      {
5328
        /* If this section has a load address that makes it overlap
5329
     the previous section, then we need a new segment.  */
5330
34
        new_segment = true;
5331
34
      }
5332
73
    else if ((abfd->flags & D_PAGED) != 0
5333
73
       && (((last_hdr->lma + last_size - 1) & -maxpagesize)
5334
73
           == (hdr->lma & -maxpagesize)))
5335
24
      {
5336
        /* If we are demand paged then we can't map two disk
5337
     pages onto the same memory page.  */
5338
24
        new_segment = false;
5339
24
      }
5340
    /* In the next test we have to be careful when last_hdr->lma is close
5341
       to the end of the address space.  If the aligned address wraps
5342
       around to the start of the address space, then there are no more
5343
       pages left in memory and it is OK to assume that the current
5344
       section can be included in the current segment.  */
5345
49
    else if ((BFD_ALIGN (last_hdr->lma + last_size, maxpagesize)
5346
49
        + maxpagesize > last_hdr->lma)
5347
49
       && (BFD_ALIGN (last_hdr->lma + last_size, maxpagesize)
5348
49
           + maxpagesize <= hdr->lma))
5349
37
      {
5350
        /* If putting this section in this segment would force us to
5351
     skip a page in the segment, then we need a new segment.  */
5352
37
        new_segment = true;
5353
37
      }
5354
12
    else if ((last_hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0
5355
3
       && (hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) != 0)
5356
0
      {
5357
        /* We don't want to put a loaded section after a
5358
     nonloaded (ie. bss style) section in the same segment
5359
     as that will force the non-loaded section to be loaded.
5360
     Consider .tbss sections as loaded for this purpose.  */
5361
0
        new_segment = true;
5362
0
      }
5363
12
    else if ((abfd->flags & D_PAGED) == 0)
5364
0
      {
5365
        /* If the file is not demand paged, which means that we
5366
     don't require the sections to be correctly aligned in the
5367
     file, then there is no other reason for a new segment.  */
5368
0
        new_segment = false;
5369
0
      }
5370
12
    else if (info != NULL
5371
0
       && info->separate_code
5372
0
       && executable != ((hdr->flags & SEC_CODE) != 0))
5373
0
      {
5374
0
        new_segment = true;
5375
0
      }
5376
12
    else if (! writable
5377
7
       && (hdr->flags & SEC_READONLY) == 0)
5378
3
      {
5379
        /* We don't want to put a writable section in a read only
5380
     segment.  */
5381
3
        new_segment = true;
5382
3
      }
5383
9
    else
5384
9
      {
5385
        /* Otherwise, we can use the same segment.  */
5386
9
        new_segment = false;
5387
9
      }
5388
5389
    /* Allow interested parties a chance to override our decision.  */
5390
150
    if (last_hdr != NULL
5391
110
        && info != NULL
5392
0
        && info->callbacks->override_segment_assignment != NULL)
5393
0
      new_segment
5394
0
        = info->callbacks->override_segment_assignment (info, abfd, hdr,
5395
0
                    last_hdr,
5396
0
                    new_segment);
5397
5398
150
    if (! new_segment)
5399
73
      {
5400
73
        if ((hdr->flags & SEC_READONLY) == 0)
5401
28
    writable = true;
5402
73
        if ((hdr->flags & SEC_CODE) != 0)
5403
22
    executable = true;
5404
73
        last_hdr = hdr;
5405
        /* .tbss sections effectively have zero size.  */
5406
73
        last_size = (!IS_TBSS (hdr) ? hdr->size : 0) / opb;
5407
73
        continue;
5408
73
      }
5409
5410
    /* We need a new program segment.  We must create a new program
5411
       header holding all the sections from hdr_index until hdr.  */
5412
5413
77
    m = make_mapping (abfd, sections, hdr_index, i, phdr_in_segment);
5414
77
    if (m == NULL)
5415
0
      goto error_return;
5416
5417
77
    *pm = m;
5418
77
    pm = &m->next;
5419
5420
77
    if ((hdr->flags & SEC_READONLY) == 0)
5421
24
      writable = true;
5422
53
    else
5423
53
      writable = false;
5424
5425
77
    if ((hdr->flags & SEC_CODE) == 0)
5426
61
      executable = false;
5427
16
    else
5428
16
      executable = true;
5429
5430
77
    last_hdr = hdr;
5431
    /* .tbss sections effectively have zero size.  */
5432
77
    last_size = (!IS_TBSS (hdr) ? hdr->size : 0) / opb;
5433
77
    hdr_index = i;
5434
77
    phdr_in_segment = false;
5435
77
  }
5436
5437
      /* Create a final PT_LOAD program segment, but not if it's just
5438
   for .tbss.  */
5439
44
      if (last_hdr != NULL
5440
40
    && (i - hdr_index != 1
5441
32
        || !IS_TBSS (last_hdr)))
5442
38
  {
5443
38
    m = make_mapping (abfd, sections, hdr_index, i, phdr_in_segment);
5444
38
    if (m == NULL)
5445
0
      goto error_return;
5446
5447
38
    *pm = m;
5448
38
    pm = &m->next;
5449
38
  }
5450
5451
      /* If there is a .dynamic section, throw in a PT_DYNAMIC segment.  */
5452
44
      if (dynsec != NULL)
5453
4
  {
5454
4
    m = _bfd_elf_make_dynamic_segment (abfd, dynsec);
5455
4
    if (m == NULL)
5456
0
      goto error_return;
5457
4
    *pm = m;
5458
4
    pm = &m->next;
5459
4
  }
5460
5461
      /* For each batch of consecutive loadable SHT_NOTE  sections,
5462
   add a PT_NOTE segment.  We don't use bfd_get_section_by_name,
5463
   because if we link together nonloadable .note sections and
5464
   loadable .note sections, we will generate two .note sections
5465
   in the output file.  */
5466
286
      for (s = abfd->sections; s != NULL; s = s->next)
5467
242
  {
5468
242
    if ((s->flags & SEC_LOAD) != 0
5469
132
        && elf_section_type (s) == SHT_NOTE)
5470
15
      {
5471
15
        asection *s2;
5472
15
        unsigned int alignment_power = s->alignment_power;
5473
5474
15
        count = 1;
5475
15
        for (s2 = s; s2->next != NULL; s2 = s2->next)
5476
15
    {
5477
15
      if (s2->next->alignment_power == alignment_power
5478
11
          && (s2->next->flags & SEC_LOAD) != 0
5479
7
          && elf_section_type (s2->next) == SHT_NOTE
5480
0
          && align_power (s2->lma + s2->size / opb,
5481
0
              alignment_power)
5482
0
          == s2->next->lma)
5483
0
        count++;
5484
15
      else
5485
15
        break;
5486
15
    }
5487
15
        amt = sizeof (struct elf_segment_map) - sizeof (asection *);
5488
15
        amt += count * sizeof (asection *);
5489
15
        m = bfd_zalloc (abfd, amt);
5490
15
        if (m == NULL)
5491
0
    goto error_return;
5492
15
        m->next = NULL;
5493
15
        m->p_type = PT_NOTE;
5494
15
        m->count = count;
5495
15
        while (count > 1)
5496
0
    {
5497
0
      m->sections[m->count - count--] = s;
5498
0
      BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
5499
0
      s = s->next;
5500
0
    }
5501
15
        m->sections[m->count - 1] = s;
5502
15
        BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0);
5503
15
        *pm = m;
5504
15
        pm = &m->next;
5505
15
      }
5506
242
    if (s->flags & SEC_THREAD_LOCAL)
5507
37
      {
5508
37
        if (! tls_count)
5509
21
    first_tls = s;
5510
37
        tls_count++;
5511
37
      }
5512
242
    if (first_mbind == NULL
5513
165
        && (elf_section_flags (s) & SHF_GNU_MBIND) != 0)
5514
24
      first_mbind = s;
5515
242
  }
5516
5517
      /* If there are any SHF_TLS output sections, add PT_TLS segment.  */
5518
44
      if (tls_count > 0)
5519
21
  {
5520
21
    amt = sizeof (struct elf_segment_map) - sizeof (asection *);
5521
21
    amt += tls_count * sizeof (asection *);
5522
21
    m = bfd_zalloc (abfd, amt);
5523
21
    if (m == NULL)
5524
0
      goto error_return;
5525
21
    m->next = NULL;
5526
21
    m->p_type = PT_TLS;
5527
21
    m->count = tls_count;
5528
    /* Mandated PF_R.  */
5529
21
    m->p_flags = PF_R;
5530
21
    m->p_flags_valid = 1;
5531
21
    s = first_tls;
5532
48
    for (i = 0; i < tls_count; ++i)
5533
31
      {
5534
31
        if ((s->flags & SEC_THREAD_LOCAL) == 0)
5535
4
    {
5536
4
      _bfd_error_handler
5537
4
        (_("%pB: TLS sections are not adjacent:"), abfd);
5538
4
      s = first_tls;
5539
4
      i = 0;
5540
35
      while (i < tls_count)
5541
31
        {
5542
31
          if ((s->flags & SEC_THREAD_LOCAL) != 0)
5543
14
      {
5544
14
        _bfd_error_handler (_("      TLS: %pA"), s);
5545
14
        i++;
5546
14
      }
5547
17
          else
5548
17
      _bfd_error_handler (_("  non-TLS: %pA"), s);
5549
31
          s = s->next;
5550
31
        }
5551
4
      bfd_set_error (bfd_error_bad_value);
5552
4
      goto error_return;
5553
4
    }
5554
27
        m->sections[i] = s;
5555
27
        s = s->next;
5556
27
      }
5557
5558
17
    *pm = m;
5559
17
    pm = &m->next;
5560
17
  }
5561
5562
40
      if (first_mbind
5563
20
    && (abfd->flags & D_PAGED) != 0
5564
20
    && (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0)
5565
0
  for (s = first_mbind; s != NULL; s = s->next)
5566
0
    if ((elf_section_flags (s) & SHF_GNU_MBIND) != 0
5567
0
        && elf_section_data (s)->this_hdr.sh_info <= PT_GNU_MBIND_NUM)
5568
0
      {
5569
        /* Mandated PF_R.  */
5570
0
        unsigned long p_flags = PF_R;
5571
0
        if ((s->flags & SEC_READONLY) == 0)
5572
0
    p_flags |= PF_W;
5573
0
        if ((s->flags & SEC_CODE) != 0)
5574
0
    p_flags |= PF_X;
5575
5576
0
        amt = sizeof (struct elf_segment_map) + sizeof (asection *);
5577
0
        m = bfd_zalloc (abfd, amt);
5578
0
        if (m == NULL)
5579
0
    goto error_return;
5580
0
        m->next = NULL;
5581
0
        m->p_type = (PT_GNU_MBIND_LO
5582
0
         + elf_section_data (s)->this_hdr.sh_info);
5583
0
        m->count = 1;
5584
0
        m->p_flags_valid = 1;
5585
0
        m->sections[0] = s;
5586
0
        m->p_flags = p_flags;
5587
5588
0
        *pm = m;
5589
0
        pm = &m->next;
5590
0
      }
5591
5592
40
      s = bfd_get_section_by_name (abfd,
5593
40
           NOTE_GNU_PROPERTY_SECTION_NAME);
5594
40
      if (s != NULL && s->size != 0)
5595
0
  {
5596
0
    amt = sizeof (struct elf_segment_map) + sizeof (asection *);
5597
0
    m = bfd_zalloc (abfd, amt);
5598
0
    if (m == NULL)
5599
0
      goto error_return;
5600
0
    m->next = NULL;
5601
0
    m->p_type = PT_GNU_PROPERTY;
5602
0
    m->count = 1;
5603
0
    m->p_flags_valid = 1;
5604
0
    m->sections[0] = s;
5605
0
    m->p_flags = PF_R;
5606
0
    *pm = m;
5607
0
    pm = &m->next;
5608
0
  }
5609
5610
      /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME
5611
   segment.  */
5612
40
      eh_frame_hdr = elf_eh_frame_hdr (info);
5613
40
      if (eh_frame_hdr != NULL
5614
0
    && (eh_frame_hdr->output_section->flags & SEC_LOAD) != 0)
5615
0
  {
5616
0
    m = bfd_zalloc (abfd, sizeof (*m));
5617
0
    if (m == NULL)
5618
0
      goto error_return;
5619
0
    m->next = NULL;
5620
0
    m->p_type = PT_GNU_EH_FRAME;
5621
0
    m->count = 1;
5622
0
    m->sections[0] = eh_frame_hdr->output_section;
5623
5624
0
    *pm = m;
5625
0
    pm = &m->next;
5626
0
  }
5627
5628
      /* If there is a .sframe section, throw in a PT_GNU_SFRAME
5629
   segment.  */
5630
40
      sframe = elf_sframe (abfd);
5631
40
      if (sframe != NULL
5632
0
    && (sframe->output_section->flags & SEC_LOAD) != 0
5633
0
    && sframe->size != 0)
5634
0
  {
5635
0
    m = bfd_zalloc (abfd, sizeof (*m));
5636
0
    if (m == NULL)
5637
0
      goto error_return;
5638
0
    m->next = NULL;
5639
0
    m->p_type = PT_GNU_SFRAME;
5640
0
    m->count = 1;
5641
0
    m->sections[0] = sframe->output_section;
5642
5643
0
    *pm = m;
5644
0
    pm = &m->next;
5645
0
  }
5646
5647
40
      if (elf_stack_flags (abfd))
5648
0
  {
5649
0
    m = bfd_zalloc (abfd, sizeof (*m));
5650
0
    if (m == NULL)
5651
0
      goto error_return;
5652
0
    m->next = NULL;
5653
0
    m->p_type = PT_GNU_STACK;
5654
0
    m->p_flags = elf_stack_flags (abfd);
5655
0
    m->p_align = bed->stack_align;
5656
0
    m->p_flags_valid = 1;
5657
0
    m->p_align_valid = m->p_align != 0;
5658
0
    if (info->stacksize > 0)
5659
0
      {
5660
0
        m->p_size = info->stacksize;
5661
0
        m->p_size_valid = 1;
5662
0
      }
5663
5664
0
    *pm = m;
5665
0
    pm = &m->next;
5666
0
  }
5667
5668
40
      if (info != NULL && info->relro)
5669
0
  {
5670
0
    for (m = mfirst; m != NULL; m = m->next)
5671
0
      {
5672
0
        if (m->p_type == PT_LOAD
5673
0
      && m->count != 0
5674
0
      && m->sections[0]->vma >= info->relro_start
5675
0
      && m->sections[0]->vma < info->relro_end)
5676
0
    {
5677
0
      i = m->count;
5678
0
      while (--i != (unsigned) -1)
5679
0
        {
5680
0
          if (m->sections[i]->size > 0
5681
0
        && (m->sections[i]->flags & SEC_LOAD) != 0
5682
0
        && (m->sections[i]->flags & SEC_HAS_CONTENTS) != 0)
5683
0
      break;
5684
0
        }
5685
5686
0
      if (i != (unsigned) -1)
5687
0
        break;
5688
0
    }
5689
0
      }
5690
5691
    /* Make a PT_GNU_RELRO segment only when it isn't empty.  */
5692
0
    if (m != NULL)
5693
0
      {
5694
0
        m = bfd_zalloc (abfd, sizeof (*m));
5695
0
        if (m == NULL)
5696
0
    goto error_return;
5697
0
        m->next = NULL;
5698
0
        m->p_type = PT_GNU_RELRO;
5699
0
        *pm = m;
5700
0
        pm = &m->next;
5701
0
      }
5702
0
  }
5703
5704
40
      free (sections);
5705
40
      elf_seg_map (abfd) = mfirst;
5706
40
    }
5707
5708
395
  if (!elf_modify_segment_map (abfd, info, no_user_phdrs || info == NULL))
5709
0
    return false;
5710
5711
1.03k
  for (count = 0, m = elf_seg_map (abfd); m != NULL; m = m->next)
5712
638
    ++count;
5713
395
  elf_program_header_size (abfd) = count * bed->s->sizeof_phdr;
5714
5715
395
  return true;
5716
5717
4
 error_return:
5718
4
  free (sections);
5719
4
  return false;
5720
395
}
5721
5722
/* Sort sections by address.  */
5723
5724
static int
5725
elf_sort_sections (const void *arg1, const void *arg2)
5726
3.18k
{
5727
3.18k
  const asection *sec1 = *(const asection **) arg1;
5728
3.18k
  const asection *sec2 = *(const asection **) arg2;
5729
3.18k
  bfd_size_type size1, size2;
5730
5731
  /* Sort by LMA first, since this is the address used to
5732
     place the section into a segment.  */
5733
3.18k
  if (sec1->lma < sec2->lma)
5734
1.62k
    return -1;
5735
1.56k
  else if (sec1->lma > sec2->lma)
5736
989
    return 1;
5737
5738
  /* Then sort by VMA.  Normally the LMA and the VMA will be
5739
     the same, and this will do nothing.  */
5740
575
  if (sec1->vma < sec2->vma)
5741
68
    return -1;
5742
507
  else if (sec1->vma > sec2->vma)
5743
181
    return 1;
5744
5745
  /* Put !SEC_LOAD sections after SEC_LOAD ones.  */
5746
5747
652
#define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0 \
5748
652
      && (x)->size != 0)
5749
5750
326
  if (TOEND (sec1))
5751
252
    {
5752
252
      if (!TOEND (sec2))
5753
21
  return 1;
5754
252
    }
5755
74
  else if (TOEND (sec2))
5756
0
    return -1;
5757
5758
305
#undef TOEND
5759
5760
  /* Sort by size, to put zero sized sections
5761
     before others at the same address.  */
5762
5763
305
  size1 = (sec1->flags & SEC_LOAD) ? sec1->size : 0;
5764
305
  size2 = (sec2->flags & SEC_LOAD) ? sec2->size : 0;
5765
5766
305
  if (size1 < size2)
5767
31
    return -1;
5768
274
  if (size1 > size2)
5769
22
    return 1;
5770
5771
252
  return sec1->target_index - sec2->target_index;
5772
274
}
5773
5774
/* This qsort comparison functions sorts PT_LOAD segments first and
5775
   by p_paddr, for assign_file_positions_for_load_sections.  */
5776
5777
static int
5778
elf_sort_segments (const void *arg1, const void *arg2)
5779
895
{
5780
895
  const struct elf_segment_map *m1 = *(const struct elf_segment_map **) arg1;
5781
895
  const struct elf_segment_map *m2 = *(const struct elf_segment_map **) arg2;
5782
5783
895
  if (m1->p_type != m2->p_type)
5784
715
    {
5785
715
      if (m1->p_type == PT_NULL)
5786
71
  return 1;
5787
644
      if (m2->p_type == PT_NULL)
5788
59
  return -1;
5789
585
      return m1->p_type < m2->p_type ? -1 : 1;
5790
644
    }
5791
180
  if (m1->includes_filehdr != m2->includes_filehdr)
5792
26
    return m1->includes_filehdr ? -1 : 1;
5793
154
  if (m1->no_sort_lma != m2->no_sort_lma)
5794
0
    return m1->no_sort_lma ? -1 : 1;
5795
154
  if (m1->p_type == PT_LOAD && !m1->no_sort_lma)
5796
67
    {
5797
67
      bfd_vma lma1, lma2;  /* Octets.  */
5798
67
      lma1 = 0;
5799
67
      if (m1->p_paddr_valid)
5800
16
  lma1 = m1->p_paddr;
5801
51
      else if (m1->count != 0)
5802
50
  {
5803
50
    unsigned int opb = bfd_octets_per_byte (m1->sections[0]->owner,
5804
50
              m1->sections[0]);
5805
50
    lma1 = (m1->sections[0]->lma + m1->p_vaddr_offset) * opb;
5806
50
  }
5807
67
      lma2 = 0;
5808
67
      if (m2->p_paddr_valid)
5809
16
  lma2 = m2->p_paddr;
5810
51
      else if (m2->count != 0)
5811
51
  {
5812
51
    unsigned int opb = bfd_octets_per_byte (m2->sections[0]->owner,
5813
51
              m2->sections[0]);
5814
51
    lma2 = (m2->sections[0]->lma + m2->p_vaddr_offset) * opb;
5815
51
  }
5816
67
      if (lma1 != lma2)
5817
39
  return lma1 < lma2 ? -1 : 1;
5818
67
    }
5819
115
  if (m1->idx != m2->idx)
5820
115
    return m1->idx < m2->idx ? -1 : 1;
5821
0
  return 0;
5822
115
}
5823
5824
/* Ian Lance Taylor writes:
5825
5826
   We shouldn't be using % with a negative signed number.  That's just
5827
   not good.  We have to make sure either that the number is not
5828
   negative, or that the number has an unsigned type.  When the types
5829
   are all the same size they wind up as unsigned.  When file_ptr is a
5830
   larger signed type, the arithmetic winds up as signed long long,
5831
   which is wrong.
5832
5833
   What we're trying to say here is something like ``increase OFF by
5834
   the least amount that will cause it to be equal to the VMA modulo
5835
   the page size.''  */
5836
/* In other words, something like:
5837
5838
   vma_offset = m->sections[0]->vma % bed->maxpagesize;
5839
   off_offset = off % bed->maxpagesize;
5840
   if (vma_offset < off_offset)
5841
     adjustment = vma_offset + bed->maxpagesize - off_offset;
5842
   else
5843
     adjustment = vma_offset - off_offset;
5844
5845
   which can be collapsed into the expression below.  */
5846
5847
static file_ptr
5848
vma_page_aligned_bias (bfd_vma vma, ufile_ptr off, bfd_vma maxpagesize)
5849
217
{
5850
  /* PR binutils/16199: Handle an alignment of zero.  */
5851
217
  if (maxpagesize == 0)
5852
1
    maxpagesize = 1;
5853
217
  return ((vma - off) % maxpagesize);
5854
217
}
5855
5856
static void
5857
print_segment_map (const struct elf_segment_map *m)
5858
46
{
5859
46
  unsigned int j;
5860
46
  const char *pt = get_segment_type (m->p_type);
5861
46
  char buf[32];
5862
5863
46
  if (pt == NULL)
5864
0
    {
5865
0
      if (m->p_type >= PT_LOPROC && m->p_type <= PT_HIPROC)
5866
0
  sprintf (buf, "LOPROC+%7.7x",
5867
0
     (unsigned int) (m->p_type - PT_LOPROC));
5868
0
      else if (m->p_type >= PT_LOOS && m->p_type <= PT_HIOS)
5869
0
  sprintf (buf, "LOOS+%7.7x",
5870
0
     (unsigned int) (m->p_type - PT_LOOS));
5871
0
      else
5872
0
  snprintf (buf, sizeof (buf), "%8.8x",
5873
0
      (unsigned int) m->p_type);
5874
0
      pt = buf;
5875
0
    }
5876
46
  fflush (stdout);
5877
46
  fprintf (stderr, "%s:", pt);
5878
722
  for (j = 0; j < m->count; j++)
5879
676
    fprintf (stderr, " %s", m->sections [j]->name);
5880
46
  putc ('\n',stderr);
5881
46
  fflush (stderr);
5882
46
}
5883
5884
/* Assign file positions to the sections based on the mapping from
5885
   sections to segments.  This function also sets up some fields in
5886
   the file header.  */
5887
5888
static bool
5889
assign_file_positions_for_load_sections (bfd *abfd,
5890
           struct bfd_link_info *link_info)
5891
399
{
5892
399
  elf_backend_data *bed = get_elf_backend_data (abfd);
5893
399
  struct elf_segment_map *m;
5894
399
  struct elf_segment_map *phdr_load_seg;
5895
399
  Elf_Internal_Phdr *phdrs;
5896
399
  Elf_Internal_Phdr *p;
5897
399
  ufile_ptr off;  /* Octets.  */
5898
399
  bfd_size_type maxpagesize;
5899
399
  unsigned int alloc, actual;
5900
399
  unsigned int i, j;
5901
399
  struct elf_segment_map **sorted_seg_map;
5902
399
  unsigned int opb = bfd_octets_per_byte (abfd, NULL);
5903
5904
399
  if (link_info == NULL
5905
399
      && !bfd_elf_map_sections_to_segments (abfd, link_info, NULL))
5906
4
    return false;
5907
5908
395
  alloc = 0;
5909
1.03k
  for (m = elf_seg_map (abfd); m != NULL; m = m->next)
5910
638
    m->idx = alloc++;
5911
5912
395
  if (alloc)
5913
189
    {
5914
189
      elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr;
5915
189
      elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr;
5916
189
    }
5917
206
  else
5918
206
    {
5919
      /* PR binutils/12467.  */
5920
206
      elf_elfheader (abfd)->e_phoff = 0;
5921
206
      elf_elfheader (abfd)->e_phentsize = 0;
5922
206
    }
5923
5924
395
  elf_elfheader (abfd)->e_phnum = alloc;
5925
5926
395
  if (elf_program_header_size (abfd) == (bfd_size_type) -1)
5927
0
    {
5928
0
      actual = alloc;
5929
0
      elf_program_header_size (abfd) = alloc * bed->s->sizeof_phdr;
5930
0
    }
5931
395
  else
5932
395
    {
5933
395
      actual = elf_program_header_size (abfd) / bed->s->sizeof_phdr;
5934
395
      BFD_ASSERT (elf_program_header_size (abfd)
5935
395
      == actual * bed->s->sizeof_phdr);
5936
395
      BFD_ASSERT (actual >= alloc);
5937
395
    }
5938
5939
395
  if (alloc == 0)
5940
206
    {
5941
206
      elf_next_file_pos (abfd) = bed->s->sizeof_ehdr;
5942
206
      return true;
5943
206
    }
5944
5945
  /* We're writing the size in elf_program_header_size (abfd),
5946
     see assign_file_positions_except_relocs, so make sure we have
5947
     that amount allocated, with trailing space cleared.
5948
     The variable alloc contains the computed need, while
5949
     elf_program_header_size (abfd) contains the size used for the
5950
     layout.
5951
     See ld/emultempl/elf-generic.em:gld${EMULATION_NAME}_map_segments
5952
     where the layout is forced to according to a larger size in the
5953
     last iterations for the testcase ld-elf/header.  */
5954
189
  phdrs = bfd_zalloc (abfd, (actual * sizeof (*phdrs)
5955
189
           + alloc * sizeof (*sorted_seg_map)));
5956
189
  sorted_seg_map = (struct elf_segment_map **) (phdrs + actual);
5957
189
  elf_tdata (abfd)->phdr = phdrs;
5958
189
  if (phdrs == NULL)
5959
0
    return false;
5960
5961
827
  for (m = elf_seg_map (abfd), j = 0; m != NULL; m = m->next, j++)
5962
638
    {
5963
638
      sorted_seg_map[j] = m;
5964
      /* If elf_segment_map is not from map_sections_to_segments, the
5965
   sections may not be correctly ordered.  NOTE: sorting should
5966
   not be done to the PT_NOTE section of a corefile, which may
5967
   contain several pseudo-sections artificially created by bfd.
5968
   Sorting these pseudo-sections breaks things badly.  */
5969
638
      if (m->count > 1
5970
187
    && !(elf_elfheader (abfd)->e_type == ET_CORE
5971
101
         && m->p_type == PT_NOTE))
5972
187
  {
5973
1.48k
    for (i = 0; i < m->count; i++)
5974
1.30k
      m->sections[i]->target_index = i;
5975
187
    qsort (m->sections, (size_t) m->count, sizeof (asection *),
5976
187
     elf_sort_sections);
5977
187
  }
5978
638
    }
5979
189
  if (alloc > 1)
5980
112
    qsort (sorted_seg_map, alloc, sizeof (*sorted_seg_map),
5981
112
     elf_sort_segments);
5982
5983
189
  maxpagesize = 1;
5984
189
  if ((abfd->flags & D_PAGED) != 0)
5985
69
    {
5986
69
      if (link_info != NULL)
5987
0
  maxpagesize = link_info->maxpagesize;
5988
69
      else
5989
69
  maxpagesize = bed->maxpagesize;
5990
69
    }
5991
5992
  /* Sections must map to file offsets past the ELF file header.  */
5993
189
  off = bed->s->sizeof_ehdr;
5994
  /* And if one of the PT_LOAD headers doesn't include the program
5995
     headers then we'll be mapping program headers in the usual
5996
     position after the ELF file header.  */
5997
189
  phdr_load_seg = NULL;
5998
280
  for (j = 0; j < alloc; j++)
5999
263
    {
6000
263
      m = sorted_seg_map[j];
6001
263
      if (m->p_type != PT_LOAD)
6002
144
  break;
6003
119
      if (m->includes_phdrs)
6004
28
  {
6005
28
    phdr_load_seg = m;
6006
28
    break;
6007
28
  }
6008
119
    }
6009
189
  if (phdr_load_seg == NULL)
6010
161
    off += actual * bed->s->sizeof_phdr;
6011
6012
775
  for (j = 0; j < alloc; j++)
6013
595
    {
6014
595
      asection **secpp;
6015
595
      bfd_vma off_adjust;  /* Octets.  */
6016
595
      bool no_contents;
6017
595
      bfd_size_type align_pagesize;
6018
6019
      /* An ELF segment (described by Elf_Internal_Phdr) may contain a
6020
   number of sections with contents contributing to both p_filesz
6021
   and p_memsz, followed by a number of sections with no contents
6022
   that just contribute to p_memsz.  In this loop, OFF tracks next
6023
   available file offset for PT_LOAD and PT_NOTE segments.  */
6024
595
      m = sorted_seg_map[j];
6025
595
      p = phdrs + m->idx;
6026
595
      p->p_type = m->p_type;
6027
595
      p->p_flags = m->p_flags;
6028
6029
595
      if (m->count == 0)
6030
294
  p->p_vaddr = m->p_vaddr_offset * opb;
6031
301
      else
6032
301
  p->p_vaddr = (m->sections[0]->vma + m->p_vaddr_offset) * opb;
6033
6034
595
      if (m->p_paddr_valid)
6035
405
  p->p_paddr = m->p_paddr;
6036
190
      else if (m->count == 0)
6037
51
  p->p_paddr = 0;
6038
139
      else
6039
139
  p->p_paddr = (m->sections[0]->lma + m->p_vaddr_offset) * opb;
6040
6041
595
      align_pagesize = 0;
6042
595
      if (p->p_type == PT_LOAD
6043
125
    && (abfd->flags & D_PAGED) != 0)
6044
107
  {
6045
    /* p_align in demand paged PT_LOAD segments effectively stores
6046
       the maximum page size.  When copying an executable with
6047
       objcopy, we set m->p_align from the input file.  Use this
6048
       value for maxpagesize rather than bed->maxpagesize, which
6049
       may be different.  Note that we use maxpagesize for PT_TLS
6050
       segment alignment later in this function, so we are relying
6051
       on at least one PT_LOAD segment appearing before a PT_TLS
6052
       segment.  */
6053
107
    if (m->p_align_valid)
6054
20
      maxpagesize = m->p_align;
6055
87
    else if (bed->p_align != 0
6056
27
       && (link_info == NULL
6057
0
           || !link_info->maxpagesize_is_set))
6058
      /* We will lay out this binary using maxpagesize but set
6059
         p->p_align later to the possibly smaller bed->p_align.
6060
         The run-time loader will then be able to load this
6061
         binary when the system page size is maxpagesize, but if
6062
         the system page size is smaller can use p->p_align.
6063
         In either case p->p_align will be increased if
6064
         necessary to match section alignment.  */
6065
27
      align_pagesize = bed->p_align;
6066
6067
107
    p->p_align = maxpagesize;
6068
107
  }
6069
488
      else if (m->p_align_valid)
6070
315
  p->p_align = m->p_align;
6071
173
      else if (m->count == 0)
6072
96
  p->p_align = 1 << bed->s->log_file_align;
6073
6074
595
      if (m == phdr_load_seg)
6075
28
  off += actual * bed->s->sizeof_phdr;
6076
6077
595
      no_contents = false;
6078
595
      off_adjust = 0;
6079
595
      if (p->p_type == PT_LOAD
6080
125
    && m->count > 0)
6081
118
  {
6082
118
    bfd_size_type align;  /* Bytes.  */
6083
118
    unsigned int align_power = 0;
6084
6085
118
    if (m->p_align_valid)
6086
14
      align = p->p_align;
6087
104
    else
6088
104
      {
6089
339
        for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
6090
235
    {
6091
235
      unsigned int secalign;
6092
6093
235
      secalign = bfd_section_alignment (*secpp);
6094
235
      if (secalign > align_power)
6095
103
        align_power = secalign;
6096
235
    }
6097
104
        align = (bfd_size_type) 1 << align_power;
6098
        /* If a section requires alignment higher than the
6099
     minimum p_align value, don't reduce a maxpagesize
6100
     p->p_align set earlier in this function.  */
6101
104
        if (align > bed->p_align)
6102
77
    align_pagesize = 0;
6103
104
        if (align < maxpagesize)
6104
82
    align = maxpagesize;
6105
22
        else
6106
22
    {
6107
      /* If a section requires alignment higher than the
6108
         maximum page size, set p_align to the section
6109
         alignment.  */
6110
22
      if ((abfd->flags & D_PAGED) != 0)
6111
4
        p->p_align = align;
6112
22
    }
6113
104
      }
6114
6115
476
    for (i = 0; i < m->count; i++)
6116
358
      if ((m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
6117
        /* If we aren't making room for this section, then
6118
     it must be SHT_NOBITS regardless of what we've
6119
     set via struct bfd_elf_special_section.  */
6120
34
        elf_section_type (m->sections[i]) = SHT_NOBITS;
6121
6122
    /* Find out whether this segment contains any loadable
6123
       sections.  */
6124
118
    no_contents = true;
6125
138
    for (i = 0; i < m->count; i++)
6126
130
      if (elf_section_type (m->sections[i]) != SHT_NOBITS)
6127
110
        {
6128
110
    no_contents = false;
6129
110
    break;
6130
110
        }
6131
6132
118
    off_adjust = vma_page_aligned_bias (p->p_vaddr, off, align * opb);
6133
6134
    /* Broken hardware and/or kernel require that files do not
6135
       map the same page with different permissions on some hppa
6136
       processors.  */
6137
118
    if (j != 0
6138
54
        && (abfd->flags & D_PAGED) != 0
6139
44
        && bed->no_page_alias
6140
0
        && (off & (maxpagesize - 1)) != 0
6141
0
        && ((off & -maxpagesize)
6142
0
      == ((off + off_adjust) & -maxpagesize)))
6143
0
      off_adjust += maxpagesize;
6144
118
    off += off_adjust;
6145
118
    if (no_contents)
6146
8
      {
6147
        /* We shouldn't need to align the segment on disk since
6148
     the segment doesn't need file space, but the gABI
6149
     arguably requires the alignment and glibc ld.so
6150
     checks it.  So to comply with the alignment
6151
     requirement but not waste file space, we adjust
6152
     p_offset for just this segment.  (OFF_ADJUST is
6153
     subtracted from OFF later.)  This may put p_offset
6154
     past the end of file, but that shouldn't matter.  */
6155
8
      }
6156
110
    else
6157
110
      off_adjust = 0;
6158
118
  }
6159
      /* Make sure the .dynamic section is the first section in the
6160
   PT_DYNAMIC segment.  */
6161
477
      else if (p->p_type == PT_DYNAMIC
6162
36
         && m->count > 1
6163
0
         && ! streq (m->sections[0]->name, ".dynamic"))
6164
0
  {
6165
0
    _bfd_error_handler
6166
0
      (_("%pB: The first section in the PT_DYNAMIC segment"
6167
0
         " is not the .dynamic section"),
6168
0
       abfd);
6169
0
    bfd_set_error (bfd_error_bad_value);
6170
0
    return false;
6171
0
  }
6172
      /* Set the note section type to SHT_NOTE.  */
6173
477
      else if (p->p_type == PT_NOTE)
6174
98
  for (i = 0; i < m->count; i++)
6175
58
    elf_section_type (m->sections[i]) = SHT_NOTE;
6176
6177
595
      if (m->includes_filehdr)
6178
72
  {
6179
72
    if (!m->p_flags_valid)
6180
3
      p->p_flags |= PF_R;
6181
72
    p->p_filesz = bed->s->sizeof_ehdr;
6182
72
    p->p_memsz = bed->s->sizeof_ehdr;
6183
72
    if (p->p_type == PT_LOAD)
6184
30
      {
6185
30
        if (m->count > 0)
6186
24
    {
6187
24
      if (p->p_vaddr < off
6188
22
          || (!m->p_paddr_valid
6189
3
        && p->p_paddr < off))
6190
2
        {
6191
2
          _bfd_error_handler
6192
2
      (_("%pB: not enough room for program headers,"
6193
2
         " try linking with -N"),
6194
2
       abfd);
6195
2
          bfd_set_error (bfd_error_bad_value);
6196
2
          return false;
6197
2
        }
6198
22
      p->p_vaddr -= off;
6199
22
      if (!m->p_paddr_valid)
6200
3
        p->p_paddr -= off;
6201
22
    }
6202
30
      }
6203
42
    else if (sorted_seg_map[0]->includes_filehdr)
6204
25
      {
6205
25
        Elf_Internal_Phdr *filehdr = phdrs + sorted_seg_map[0]->idx;
6206
25
        p->p_vaddr = filehdr->p_vaddr;
6207
25
        if (!m->p_paddr_valid)
6208
7
    p->p_paddr = filehdr->p_paddr;
6209
25
      }
6210
72
  }
6211
6212
593
      if (m->includes_phdrs)
6213
42
  {
6214
42
    if (!m->p_flags_valid)
6215
3
      p->p_flags |= PF_R;
6216
42
    p->p_filesz += actual * bed->s->sizeof_phdr;
6217
42
    p->p_memsz += actual * bed->s->sizeof_phdr;
6218
42
    if (!m->includes_filehdr)
6219
11
      {
6220
11
        if (p->p_type == PT_LOAD)
6221
1
    {
6222
1
      p->p_offset = off - actual * bed->s->sizeof_phdr;
6223
1
      elf_elfheader (abfd)->e_phoff = p->p_offset;
6224
1
      if (m->count > 0)
6225
0
        {
6226
0
          p->p_vaddr -= off - p->p_offset;
6227
0
          if (!m->p_paddr_valid)
6228
0
      p->p_paddr -= off - p->p_offset;
6229
0
        }
6230
1
    }
6231
10
        else if (phdr_load_seg != NULL)
6232
8
    {
6233
      /* Also set PT_PHDR to match phdr_load_seg.  We've
6234
         sorted segments so that phdr_load_seg will
6235
         already be set by the code immediately above.  */
6236
8
      Elf_Internal_Phdr *phdr = phdrs + phdr_load_seg->idx;
6237
8
      bfd_vma phdr_off = 0;  /* Octets.  */
6238
8
      if (phdr_load_seg->includes_filehdr)
6239
8
        phdr_off = bed->s->sizeof_ehdr;
6240
8
      p->p_vaddr = phdr->p_vaddr + phdr_off;
6241
8
      if (!m->p_paddr_valid)
6242
0
        p->p_paddr = phdr->p_paddr + phdr_off;
6243
8
      p->p_offset = phdr->p_offset + phdr_off;
6244
8
    }
6245
2
        else
6246
2
    p->p_offset = bed->s->sizeof_ehdr;
6247
11
      }
6248
42
  }
6249
6250
593
      if (p->p_type == PT_LOAD
6251
470
    || (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core))
6252
132
  {
6253
132
    if (!m->includes_filehdr && !m->includes_phdrs)
6254
103
      {
6255
103
        p->p_offset = off;
6256
103
        if (no_contents)
6257
7
    {
6258
      /* Put meaningless p_offset for PT_LOAD segments
6259
         without file contents somewhere within the first
6260
         page, in an attempt to not point past EOF.  */
6261
7
      bfd_size_type align = maxpagesize;
6262
7
      if (align < p->p_align)
6263
0
        align = p->p_align;
6264
7
      if (align < 1)
6265
0
        align = 1;
6266
      /* Avoid p_offset of zero, which might be wrongly
6267
         interpreted as the segment being the first one,
6268
         containing the file header.  PR32763.  */
6269
7
      p->p_offset = (off + align - 1) % align + 1;
6270
7
    }
6271
103
      }
6272
29
    else
6273
29
      {
6274
29
        file_ptr adjust;  /* Octets.  */
6275
6276
29
        adjust = off - (p->p_offset + p->p_filesz);
6277
29
        if (!no_contents)
6278
29
    p->p_filesz += adjust;
6279
29
        p->p_memsz += adjust;
6280
29
      }
6281
132
  }
6282
6283
593
      if (align_pagesize)
6284
27
  p->p_align = align_pagesize;
6285
6286
      /* Set up p_filesz, p_memsz, p_align and p_flags from the section
6287
   maps.  Set filepos for sections in PT_LOAD segments, and in
6288
   core files, for sections in PT_NOTE segments.
6289
   assign_file_positions_for_non_load_sections will set filepos
6290
   for other sections and update p_filesz for other segments.  */
6291
1.98k
      for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
6292
1.39k
  {
6293
1.39k
    asection *sec;
6294
1.39k
    bfd_size_type align;
6295
1.39k
    Elf_Internal_Shdr *this_hdr;
6296
6297
1.39k
    sec = *secpp;
6298
1.39k
    this_hdr = &elf_section_data (sec)->this_hdr;
6299
1.39k
    align = (bfd_size_type) 1 << bfd_section_alignment (sec);
6300
6301
1.39k
    if ((p->p_type == PT_LOAD
6302
1.03k
         || p->p_type == PT_DYNAMIC
6303
1.03k
         || p->p_type == PT_TLS)
6304
371
        && (this_hdr->sh_type != SHT_NOBITS
6305
32
      || ((this_hdr->sh_flags & SHF_ALLOC) != 0
6306
32
          && ((this_hdr->sh_flags & SHF_TLS) == 0
6307
3
        || p->p_type == PT_TLS))))
6308
368
      {
6309
368
        bfd_vma p_start = p->p_paddr;   /* Octets.  */
6310
368
        bfd_vma p_end = p_start + p->p_memsz; /* Octets.  */
6311
368
        bfd_vma s_start = sec->lma * opb;   /* Octets.  */
6312
368
        bfd_vma adjust = s_start - p_end;   /* Octets.  */
6313
6314
368
        if (adjust != 0
6315
171
      && (s_start < p_end
6316
97
          || p_end < p_start))
6317
74
    {
6318
74
      _bfd_error_handler
6319
        /* xgettext:c-format */
6320
74
        (_("%pB: section %pA lma %#" PRIx64
6321
74
           " adjusted to %#" PRIx64),
6322
74
         abfd, sec, (uint64_t) s_start / opb,
6323
74
         (uint64_t) p_end / opb);
6324
74
      adjust = 0;
6325
74
      sec->lma = p_end / opb;
6326
74
    }
6327
368
        p->p_memsz += adjust;
6328
6329
368
        if (p->p_type == PT_LOAD)
6330
349
    {
6331
349
      if (this_hdr->sh_type != SHT_NOBITS)
6332
320
        {
6333
320
          off -= off_adjust;
6334
320
          off_adjust = 0;
6335
320
          if (p->p_filesz + adjust < p->p_memsz)
6336
10
      {
6337
        /* We have a PROGBITS section following NOBITS ones.
6338
           Allocate file space for the NOBITS section(s).
6339
           We don't need to write out the zeros, posix
6340
           fseek past the end of data already written
6341
           followed by a write at that location is
6342
           guaranteed to result in zeros being read
6343
           from the gap.  */
6344
10
        adjust = p->p_memsz - p->p_filesz;
6345
10
      }
6346
320
        }
6347
      /* We only adjust sh_offset in SHT_NOBITS sections
6348
         as would seem proper for their address when the
6349
         section is first in the segment.  sh_offset
6350
         doesn't really have any significance for
6351
         SHT_NOBITS anyway, apart from a notional position
6352
         relative to other sections.  Historically we
6353
         didn't bother with adjusting sh_offset and some
6354
         programs depend on it not being adjusted.  See
6355
         pr12921 and pr25662.  */
6356
349
      if (this_hdr->sh_type != SHT_NOBITS || i == 0)
6357
335
        {
6358
335
          off += adjust;
6359
335
          if (this_hdr->sh_type == SHT_NOBITS)
6360
15
      off_adjust += adjust;
6361
335
        }
6362
349
    }
6363
368
        if (this_hdr->sh_type != SHT_NOBITS)
6364
339
    p->p_filesz += adjust;
6365
368
      }
6366
6367
1.39k
    if (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core)
6368
1
      {
6369
        /* The section at i == 0 is the one that actually contains
6370
     everything.  */
6371
1
        if (i == 0)
6372
1
    {
6373
1
      this_hdr->sh_offset = sec->filepos = off;
6374
1
      off += this_hdr->sh_size;
6375
1
      p->p_filesz = this_hdr->sh_size;
6376
1
      p->p_memsz = 0;
6377
1
      p->p_align = 1;
6378
1
    }
6379
0
        else
6380
0
    {
6381
      /* The rest are fake sections that shouldn't be written.  */
6382
0
      sec->filepos = 0;
6383
0
      sec->size = 0;
6384
0
      sec->flags = 0;
6385
0
      continue;
6386
0
    }
6387
1
      }
6388
1.39k
    else
6389
1.39k
      {
6390
1.39k
        if (this_hdr->sh_type == SHT_NOBITS
6391
72
      && (this_hdr->sh_flags & SHF_TLS) != 0
6392
3
      && this_hdr->sh_offset == 0)
6393
3
    {
6394
      /* Set sh_offset for .tbss sections to their nominal
6395
         offset after aligning.  They are not loaded from
6396
         disk so the value doesn't really matter, except
6397
         when the .tbss section is the first one in a
6398
         PT_TLS segment.  In that case it sets the
6399
         p_offset for the PT_TLS segment, which according
6400
         to the ELF gABI ought to satisfy
6401
         p_offset % p_align == p_vaddr % p_align.  */
6402
3
      bfd_vma adjust = vma_page_aligned_bias (this_hdr->sh_addr,
6403
3
                off, align);
6404
3
      this_hdr->sh_offset = sec->filepos = off + adjust;
6405
3
    }
6406
1.38k
        else if (p->p_type == PT_LOAD)
6407
349
    {
6408
349
      this_hdr->sh_offset = sec->filepos = off;
6409
349
      if (this_hdr->sh_type != SHT_NOBITS)
6410
320
        off += this_hdr->sh_size;
6411
349
    }
6412
6413
1.39k
        if (this_hdr->sh_type != SHT_NOBITS)
6414
1.31k
    {
6415
1.31k
      p->p_filesz += this_hdr->sh_size;
6416
      /* A load section without SHF_ALLOC is something like
6417
         a note section in a PT_NOTE segment.  These take
6418
         file space but are not loaded into memory.  */
6419
1.31k
      if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
6420
538
        p->p_memsz += this_hdr->sh_size;
6421
1.31k
    }
6422
72
        else if ((this_hdr->sh_flags & SHF_ALLOC) != 0)
6423
72
    {
6424
72
      if (p->p_type == PT_TLS)
6425
0
        p->p_memsz += this_hdr->sh_size;
6426
6427
      /* .tbss is special.  It doesn't contribute to p_memsz of
6428
         normal segments.  */
6429
72
      else if ((this_hdr->sh_flags & SHF_TLS) == 0)
6430
69
        p->p_memsz += this_hdr->sh_size;
6431
72
    }
6432
6433
1.39k
        if (align > p->p_align
6434
518
      && !m->p_align_valid
6435
106
      && (p->p_type != PT_LOAD
6436
22
          || (abfd->flags & D_PAGED) == 0))
6437
106
    p->p_align = align;
6438
1.39k
      }
6439
6440
1.39k
    if (!m->p_flags_valid)
6441
107
      {
6442
107
        p->p_flags |= PF_R;
6443
107
        if ((this_hdr->sh_flags & SHF_EXECINSTR) != 0)
6444
23
    p->p_flags |= PF_X;
6445
107
        if ((this_hdr->sh_flags & SHF_WRITE) != 0)
6446
41
    p->p_flags |= PF_W;
6447
107
      }
6448
1.39k
  }
6449
6450
593
      off -= off_adjust;
6451
6452
      /* PR ld/20815 - Check that the program header segment, if
6453
   present, will be loaded into memory.  */
6454
593
      if (p->p_type == PT_PHDR
6455
26
    && phdr_load_seg == NULL
6456
7
    && !(bed->elf_backend_allow_non_load_phdr != NULL
6457
0
         && bed->elf_backend_allow_non_load_phdr (abfd, phdrs, alloc)))
6458
7
  {
6459
    /* The fix for this error is usually to edit the linker script being
6460
       used and set up the program headers manually.  Either that or
6461
       leave room for the headers at the start of the SECTIONS.  */
6462
7
    _bfd_error_handler (_("%pB: error: PHDR segment not covered"
6463
7
        " by LOAD segment"),
6464
7
            abfd);
6465
7
    if (link_info == NULL)
6466
7
      return false;
6467
    /* Arrange for the linker to exit with an error, deleting
6468
       the output file unless --noinhibit-exec is given.  */
6469
0
    link_info->callbacks->info ("%X");
6470
0
  }
6471
6472
      /* Check that all sections are in a PT_LOAD segment.
6473
   Don't check funky gdb generated core files.  */
6474
586
      if (p->p_type == PT_LOAD && bfd_get_format (abfd) != bfd_core)
6475
106
  {
6476
106
    bool check_vma = true;
6477
6478
327
    for (i = 1; i < m->count; i++)
6479
221
      if (m->sections[i]->vma == m->sections[i - 1]->vma
6480
4
    && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i])
6481
4
               ->this_hdr), p) != 0
6482
1
    && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i - 1])
6483
1
               ->this_hdr), p) != 0)
6484
0
        {
6485
    /* Looks like we have overlays packed into the segment.  */
6486
0
    check_vma = false;
6487
0
    break;
6488
0
        }
6489
6490
426
    for (i = 0; i < m->count; i++)
6491
320
      {
6492
320
        Elf_Internal_Shdr *this_hdr;
6493
320
        asection *sec;
6494
6495
320
        sec = m->sections[i];
6496
320
        this_hdr = &(elf_section_data(sec)->this_hdr);
6497
320
        if (!ELF_SECTION_IN_SEGMENT_1 (this_hdr, p, check_vma, 0)
6498
47
      && !ELF_TBSS_SPECIAL (this_hdr, p))
6499
46
    {
6500
46
      _bfd_error_handler
6501
        /* xgettext:c-format */
6502
46
        (_("%pB: section `%pA' can't be allocated in segment %u"),
6503
46
         abfd, sec, m->idx);
6504
46
      print_segment_map (m);
6505
46
    }
6506
320
      }
6507
106
  }
6508
586
    }
6509
6510
180
  elf_next_file_pos (abfd) = off;
6511
6512
180
  if (link_info != NULL
6513
0
      && phdr_load_seg != NULL
6514
0
      && phdr_load_seg->includes_filehdr)
6515
0
    {
6516
      /* There is a segment that contains both the file headers and the
6517
   program headers, so provide a symbol __ehdr_start pointing there.
6518
   A program can use this to examine itself robustly.  */
6519
6520
0
      struct elf_link_hash_table *htab = elf_hash_table (link_info);
6521
0
      struct elf_link_hash_entry *hash = htab->hehdr_start;
6522
6523
      /* If the symbol was referenced and not defined, define it.  */
6524
0
      if (hash != NULL
6525
0
    && (hash->root.type == bfd_link_hash_new
6526
0
        || hash->root.type == bfd_link_hash_undefined
6527
0
        || hash->root.type == bfd_link_hash_undefweak
6528
0
        || hash->root.type == bfd_link_hash_common))
6529
0
  {
6530
0
    asection *s = NULL;
6531
0
    bfd_vma filehdr_vaddr = phdrs[phdr_load_seg->idx].p_vaddr / opb;
6532
6533
0
    if (phdr_load_seg->count != 0)
6534
      /* The segment contains sections, so use the first one.  */
6535
0
      s = phdr_load_seg->sections[0];
6536
0
    else
6537
      /* Use the first (i.e. lowest-addressed) section in any segment.  */
6538
0
      for (m = elf_seg_map (abfd); m != NULL; m = m->next)
6539
0
        if (m->p_type == PT_LOAD && m->count != 0)
6540
0
    {
6541
0
      s = m->sections[0];
6542
0
      break;
6543
0
    }
6544
6545
0
    if (s != NULL)
6546
0
      {
6547
0
        hash->root.u.def.value = filehdr_vaddr - s->vma;
6548
0
        hash->root.u.def.section = s;
6549
0
      }
6550
0
    else
6551
0
      {
6552
0
        hash->root.u.def.value = filehdr_vaddr;
6553
0
        hash->root.u.def.section = bfd_abs_section_ptr;
6554
0
      }
6555
6556
0
    hash->root.type = bfd_link_hash_defined;
6557
0
    hash->def_regular = 1;
6558
0
    hash->non_elf = 0;
6559
0
  }
6560
0
    }
6561
6562
180
  return true;
6563
189
}
6564
6565
/* Determine if a bfd is a debuginfo file.  Unfortunately there
6566
   is no defined method for detecting such files, so we have to
6567
   use heuristics instead.  */
6568
6569
bool
6570
is_debuginfo_file (bfd *abfd)
6571
90
{
6572
90
  if (abfd == NULL || bfd_get_flavour (abfd) != bfd_target_elf_flavour)
6573
0
    return false;
6574
6575
90
  Elf_Internal_Shdr **start_headers = elf_elfsections (abfd);
6576
90
  Elf_Internal_Shdr **end_headers = start_headers + elf_numsections (abfd);
6577
90
  Elf_Internal_Shdr **headerp;
6578
6579
233
  for (headerp = start_headers; headerp < end_headers; headerp ++)
6580
233
    {
6581
233
      Elf_Internal_Shdr *header = * headerp;
6582
6583
      /* Debuginfo files do not have any allocated SHT_PROGBITS sections.
6584
   The only allocated sections are SHT_NOBITS or SHT_NOTES.  */
6585
233
      if ((header->sh_flags & SHF_ALLOC) == SHF_ALLOC
6586
104
    && header->sh_type != SHT_NOBITS
6587
104
    && header->sh_type != SHT_NOTE)
6588
90
  return false;
6589
233
    }
6590
6591
0
  return true;
6592
90
}
6593
6594
/* Assign file positions for other sections, except for compressed debug
6595
   and sections assigned in _bfd_elf_assign_file_positions_for_non_load.  */
6596
6597
static bool
6598
assign_file_positions_for_non_load_sections (bfd *abfd,
6599
               struct bfd_link_info *link_info)
6600
386
{
6601
386
  elf_backend_data *bed = get_elf_backend_data (abfd);
6602
386
  Elf_Internal_Shdr **i_shdrpp;
6603
386
  Elf_Internal_Shdr **hdrpp, **end_hdrpp;
6604
386
  Elf_Internal_Phdr *phdrs;
6605
386
  Elf_Internal_Phdr *p;
6606
386
  struct elf_segment_map *m;
6607
386
  ufile_ptr off;
6608
386
  unsigned int opb = bfd_octets_per_byte (abfd, NULL);
6609
386
  bfd_vma maxpagesize;
6610
6611
386
  if (link_info != NULL)
6612
0
    maxpagesize = link_info->maxpagesize;
6613
386
  else
6614
386
    maxpagesize = bed->maxpagesize;
6615
386
  i_shdrpp = elf_elfsections (abfd);
6616
386
  end_hdrpp = i_shdrpp + elf_numsections (abfd);
6617
386
  off = elf_next_file_pos (abfd);
6618
1.76k
  for (hdrpp = i_shdrpp + 1; hdrpp < end_hdrpp; hdrpp++)
6619
1.38k
    {
6620
1.38k
      Elf_Internal_Shdr *hdr;
6621
1.38k
      bfd_vma align;
6622
6623
1.38k
      hdr = *hdrpp;
6624
1.38k
      if (hdr->bfd_section != NULL
6625
935
    && (hdr->bfd_section->filepos != 0
6626
597
        || (hdr->sh_type == SHT_NOBITS
6627
29
      && hdr->contents == NULL)))
6628
367
  BFD_ASSERT (hdr->sh_offset == hdr->bfd_section->filepos);
6629
1.01k
      else if ((hdr->sh_flags & SHF_ALLOC) != 0)
6630
96
  {
6631
96
    if (hdr->sh_size != 0
6632
        /* PR 24717 - debuginfo files are known to be not strictly
6633
     compliant with the ELF standard.  In particular they often
6634
     have .note.gnu.property sections that are outside of any
6635
     loadable segment.  This is not a problem for such files,
6636
     so do not warn about them.  */
6637
90
        && ! is_debuginfo_file (abfd))
6638
90
      _bfd_error_handler
6639
        /* xgettext:c-format */
6640
90
        (_("%pB: warning: allocated section `%s' not in segment"),
6641
90
         abfd,
6642
90
         (hdr->bfd_section == NULL
6643
90
    ? "*unknown*"
6644
90
    : hdr->bfd_section->name));
6645
    /* We don't need to page align empty sections.  */
6646
96
    if ((abfd->flags & D_PAGED) != 0 && hdr->sh_size != 0)
6647
79
      align = maxpagesize;
6648
17
    else
6649
17
      align = hdr->sh_addralign & -hdr->sh_addralign;
6650
96
    off += vma_page_aligned_bias (hdr->sh_addr, off, align);
6651
96
    off = _bfd_elf_assign_file_position_for_section (hdr, off, false,
6652
96
                 bed->s->log_file_align);
6653
96
  }
6654
918
      else if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
6655
13
    && hdr->bfd_section == NULL)
6656
         /* We don't know the offset of these sections yet:
6657
      their size has not been decided.  */
6658
916
         || (abfd->is_linker_output
6659
0
       && hdr->bfd_section != NULL
6660
0
       && (hdr->sh_name == -1u
6661
0
           || bfd_section_is_ctf (hdr->bfd_section)))
6662
916
         || hdr == i_shdrpp[elf_onesymtab (abfd)]
6663
887
         || (elf_symtab_shndx_list (abfd) != NULL
6664
0
       && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx])
6665
887
         || hdr == i_shdrpp[elf_strtab_sec (abfd)]
6666
858
         || hdr == i_shdrpp[elf_shstrtab_sec (abfd)])
6667
446
  hdr->sh_offset = -1;
6668
472
      else
6669
472
  off = _bfd_elf_assign_file_position_for_section (hdr, off, true, 0);
6670
1.38k
    }
6671
386
  elf_next_file_pos (abfd) = off;
6672
6673
  /* Now that we have set the section file positions, we can set up
6674
     the file positions for the non PT_LOAD segments.  */
6675
386
  phdrs = elf_tdata (abfd)->phdr;
6676
918
  for (m = elf_seg_map (abfd), p = phdrs; m != NULL; m = m->next, p++)
6677
547
    {
6678
547
      if (p->p_type == PT_GNU_RELRO)
6679
9
  {
6680
9
    bfd_vma start, end;  /* Bytes.  */
6681
9
    bool ok;
6682
6683
9
    if (link_info != NULL)
6684
0
      {
6685
        /* During linking the range of the RELRO segment is passed
6686
     in link_info.  Note that there may be padding between
6687
     relro_start and the first RELRO section.  */
6688
0
        start = link_info->relro_start;
6689
0
        end = link_info->relro_end;
6690
0
      }
6691
9
    else if (m->count != 0)
6692
6
      {
6693
6
        if (!m->p_size_valid)
6694
0
    abort ();
6695
6
        start = m->sections[0]->vma;
6696
6
        end = start + m->p_size / opb;
6697
6
      }
6698
3
    else
6699
3
      {
6700
3
        start = 0;
6701
3
        end = 0;
6702
3
      }
6703
6704
9
    ok = false;
6705
9
    if (start < end)
6706
6
      {
6707
6
        struct elf_segment_map *lm;
6708
6
        const Elf_Internal_Phdr *lp;
6709
6
        unsigned int i;
6710
6711
        /* Find a LOAD segment containing a section in the RELRO
6712
     segment.  */
6713
6
        for (lm = elf_seg_map (abfd), lp = phdrs;
6714
37
       lm != NULL;
6715
31
       lm = lm->next, lp++)
6716
34
    {
6717
34
      if (lp->p_type == PT_LOAD
6718
8
          && lm->count != 0
6719
8
          && (lm->sections[lm->count - 1]->vma
6720
8
        + (!IS_TBSS (lm->sections[lm->count - 1])
6721
8
           ? lm->sections[lm->count - 1]->size / opb
6722
8
           : 0)) > start
6723
3
          && lm->sections[0]->vma < end)
6724
3
        break;
6725
34
    }
6726
6727
6
        if (lm != NULL)
6728
3
    {
6729
      /* Find the section starting the RELRO segment.  */
6730
3
      for (i = 0; i < lm->count; i++)
6731
3
        {
6732
3
          asection *s = lm->sections[i];
6733
3
          if (s->vma >= start
6734
3
        && s->vma < end
6735
3
        && s->size != 0)
6736
3
      break;
6737
3
        }
6738
6739
3
      if (i < lm->count)
6740
3
        {
6741
3
          p->p_vaddr = lm->sections[i]->vma * opb;
6742
3
          p->p_paddr = lm->sections[i]->lma * opb;
6743
3
          p->p_offset = lm->sections[i]->filepos;
6744
3
          p->p_memsz = end * opb - p->p_vaddr;
6745
3
          p->p_filesz = p->p_memsz;
6746
6747
          /* The RELRO segment typically ends a few bytes
6748
       into .got.plt but other layouts are possible.
6749
       In cases where the end does not match any
6750
       loaded section (for instance is in file
6751
       padding), trim p_filesz back to correspond to
6752
       the end of loaded section contents.  */
6753
3
          if (p->p_filesz > lp->p_vaddr + lp->p_filesz - p->p_vaddr)
6754
1
      p->p_filesz = lp->p_vaddr + lp->p_filesz - p->p_vaddr;
6755
6756
          /* Preserve the alignment and flags if they are
6757
       valid.  The gold linker generates RW/4 for
6758
       the PT_GNU_RELRO section.  It is better for
6759
       objcopy/strip to honor these attributes
6760
       otherwise gdb will choke when using separate
6761
       debug files.  */
6762
3
          if (!m->p_align_valid)
6763
0
      p->p_align = 1;
6764
3
          if (!m->p_flags_valid)
6765
0
      p->p_flags = PF_R;
6766
3
          ok = true;
6767
3
        }
6768
3
    }
6769
6
      }
6770
6771
9
    if (!ok)
6772
6
      {
6773
6
        if (link_info != NULL)
6774
0
    _bfd_error_handler
6775
0
      (_("%pB: warning: unable to allocate any sections"
6776
0
         " to PT_GNU_RELRO segment"),
6777
0
       abfd);
6778
6
        memset (p, 0, sizeof *p);
6779
6
      }
6780
9
  }
6781
538
      else if (p->p_type == PT_GNU_STACK)
6782
6
  {
6783
6
    if (m->p_size_valid)
6784
2
      p->p_memsz = m->p_size;
6785
6
  }
6786
532
      else if (m->count != 0)
6787
273
  {
6788
273
    unsigned int i;
6789
6790
273
    if (p->p_type != PT_LOAD
6791
161
        && (p->p_type != PT_NOTE
6792
17
      || bfd_get_format (abfd) != bfd_core))
6793
160
      {
6794
        /* A user specified segment layout may include a PHDR
6795
     segment that overlaps with a LOAD segment...  */
6796
160
        if (p->p_type == PT_PHDR)
6797
6
    {
6798
6
      m->count = 0;
6799
6
      continue;
6800
6
    }
6801
6802
154
        if (m->includes_filehdr || m->includes_phdrs)
6803
15
    {
6804
      /* PR 17512: file: 2195325e.  */
6805
15
      _bfd_error_handler
6806
15
        (_("%pB: error: non-load segment %d includes file header "
6807
15
           "and/or program header"),
6808
15
         abfd, (int) (p - phdrs));
6809
15
      return false;
6810
15
    }
6811
6812
139
        p->p_filesz = 0;
6813
139
        p->p_offset = m->sections[0]->filepos;
6814
145
        for (i = m->count; i-- != 0;)
6815
145
    {
6816
145
      asection *sect = m->sections[i];
6817
145
      Elf_Internal_Shdr *hdr = &elf_section_data (sect)->this_hdr;
6818
145
      if (hdr->sh_type != SHT_NOBITS)
6819
139
        {
6820
139
          p->p_filesz = sect->filepos - p->p_offset + hdr->sh_size;
6821
          /* NB: p_memsz of the loadable PT_NOTE segment
6822
       should be the same as p_filesz.  */
6823
139
          if (p->p_type == PT_NOTE
6824
16
        && (hdr->sh_flags & SHF_ALLOC) != 0)
6825
16
      p->p_memsz = p->p_filesz;
6826
139
          break;
6827
139
        }
6828
145
    }
6829
139
      }
6830
273
  }
6831
547
    }
6832
6833
371
  return true;
6834
386
}
6835
6836
static elf_section_list *
6837
find_section_in_list (unsigned int i, elf_section_list * list)
6838
986
{
6839
1.01k
  for (;list != NULL; list = list->next)
6840
28
    if (list->ndx == i)
6841
1
      break;
6842
986
  return list;
6843
986
}
6844
6845
/* Work out the file positions of all the sections.  This is called by
6846
   _bfd_elf_compute_section_file_positions.  All the section sizes and
6847
   VMAs must be known before this is called.
6848
6849
   Reloc sections come in two flavours: Those processed specially as
6850
   "side-channel" data attached to a section to which they apply, and
6851
   those that bfd doesn't process as relocations.  The latter sort are
6852
   stored in a normal bfd section by bfd_section_from_shdr.  We don't
6853
   consider the former sort here, unless they form part of the loadable
6854
   image.  Reloc sections not assigned here (and compressed debugging
6855
   sections and CTF sections which nothing else in the file can rely
6856
   upon) will be handled later by assign_file_positions_for_relocs.
6857
6858
   We also don't set the positions of the .symtab and .strtab here.  */
6859
6860
static bool
6861
assign_file_positions_except_relocs (bfd *abfd,
6862
             struct bfd_link_info *link_info)
6863
686
{
6864
686
  struct elf_obj_tdata *tdata = elf_tdata (abfd);
6865
686
  Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd);
6866
686
  elf_backend_data *bed = get_elf_backend_data (abfd);
6867
686
  unsigned int alloc;
6868
6869
686
  if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0
6870
540
      && bfd_get_format (abfd) != bfd_core)
6871
287
    {
6872
287
      Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd);
6873
287
      unsigned int num_sec = elf_numsections (abfd);
6874
287
      Elf_Internal_Shdr **hdrpp;
6875
287
      unsigned int i;
6876
287
      ufile_ptr off;
6877
6878
      /* Start after the ELF header.  */
6879
287
      off = i_ehdrp->e_ehsize;
6880
6881
      /* We are not creating an executable, which means that we are
6882
   not creating a program header, and that the actual order of
6883
   the sections in the file is unimportant.  */
6884
1.67k
      for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++)
6885
1.39k
  {
6886
1.39k
    Elf_Internal_Shdr *hdr;
6887
6888
1.39k
    hdr = *hdrpp;
6889
1.39k
    if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
6890
172
         && hdr->bfd_section == NULL)
6891
        /* Do not assign offsets for these sections yet: we don't know
6892
     their sizes.  */
6893
1.26k
        || (abfd->is_linker_output
6894
0
      && hdr->bfd_section != NULL
6895
0
      && (hdr->sh_name == -1u
6896
0
          || bfd_section_is_ctf (hdr->bfd_section)))
6897
1.26k
        || i == elf_onesymtab (abfd)
6898
1.20k
        || (elf_symtab_shndx_list (abfd) != NULL
6899
0
      && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx])
6900
1.20k
        || i == elf_strtab_sec (abfd)
6901
1.13k
        || i == elf_shstrtab_sec (abfd))
6902
542
      {
6903
542
        hdr->sh_offset = -1;
6904
542
      }
6905
848
    else
6906
      /* There shouldn't be a need to effect "capped" file alignment here,
6907
         yet at least the Linux kernel's modpost utility was found to be
6908
         unhappy without.  While the issue was addressed there, let's be
6909
         kind for at least the foreseeable future ...  */
6910
848
      off = _bfd_elf_assign_file_position_for_section (hdr, off, false,
6911
848
                   bed->s->log_file_align);
6912
1.39k
  }
6913
6914
287
      elf_next_file_pos (abfd) = off;
6915
287
      elf_program_header_size (abfd) = 0;
6916
287
    }
6917
399
  else
6918
399
    {
6919
      /* Assign file positions for the loaded sections based on the
6920
   assignment of sections to segments.  */
6921
399
      if (!assign_file_positions_for_load_sections (abfd, link_info))
6922
13
  return false;
6923
6924
      /* And for non-load sections.  */
6925
386
      if (!assign_file_positions_for_non_load_sections (abfd, link_info))
6926
15
  return false;
6927
386
    }
6928
6929
658
  if (!(*bed->elf_backend_modify_headers) (abfd, link_info))
6930
0
    return false;
6931
6932
  /* Write out the program headers.  */
6933
658
  alloc = i_ehdrp->e_phnum;
6934
658
  if (alloc != 0)
6935
165
    {
6936
165
      if (link_info != NULL && ! link_info->no_warn_rwx_segments)
6937
0
  {
6938
0
    bool warned_tls = false;
6939
0
    bool warned_rwx = false;
6940
6941
    /* Memory resident segments with non-zero size and RWX
6942
       permissions are a security risk, so we generate a warning
6943
       here if we are creating any.  */
6944
0
    unsigned int i;
6945
6946
0
    for (i = 0; i < alloc; i++)
6947
0
      {
6948
0
        const Elf_Internal_Phdr * phdr = tdata->phdr + i;
6949
6950
0
        if (phdr->p_memsz == 0)
6951
0
    continue;
6952
6953
0
        if (! warned_tls
6954
0
      && phdr->p_type == PT_TLS
6955
0
      && (phdr->p_flags & PF_X))
6956
0
    {
6957
0
      if (link_info->warn_is_error_for_rwx_segments)
6958
0
        {
6959
0
          _bfd_error_handler (_("\
6960
0
error: %pB has a TLS segment with execute permission"),
6961
0
            abfd);
6962
0
          return false;
6963
0
        }
6964
6965
0
      _bfd_error_handler (_("\
6966
0
warning: %pB has a TLS segment with execute permission"),
6967
0
              abfd);
6968
0
      if (warned_rwx)
6969
0
        break;
6970
6971
0
      warned_tls = true;
6972
0
    }
6973
0
        else if (! warned_rwx
6974
0
           && phdr->p_type == PT_LOAD
6975
0
           && ((phdr->p_flags & (PF_R | PF_W | PF_X))
6976
0
         == (PF_R | PF_W | PF_X)))
6977
0
    {
6978
0
      if (link_info->warn_is_error_for_rwx_segments)
6979
0
        {
6980
0
          _bfd_error_handler (_("\
6981
0
error: %pB has a LOAD segment with RWX permissions"),
6982
0
            abfd);
6983
0
          return false;
6984
0
        }
6985
6986
0
      _bfd_error_handler (_("\
6987
0
warning: %pB has a LOAD segment with RWX permissions"),
6988
0
              abfd);
6989
0
      if (warned_tls)
6990
0
        break;
6991
6992
0
      warned_rwx = true;
6993
0
    }
6994
0
      }
6995
0
  }
6996
6997
165
      if (bfd_seek (abfd, i_ehdrp->e_phoff, SEEK_SET) != 0
6998
165
    || bed->s->write_out_phdrs (abfd, tdata->phdr, alloc) != 0)
6999
0
  return false;
7000
165
    }
7001
7002
658
  return true;
7003
658
}
7004
7005
bool
7006
_bfd_elf_init_file_header (bfd *abfd,
7007
         struct bfd_link_info *info ATTRIBUTE_UNUSED)
7008
687
{
7009
687
  Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form.  */
7010
687
  struct elf_strtab_hash *shstrtab;
7011
687
  elf_backend_data *bed = get_elf_backend_data (abfd);
7012
7013
687
  i_ehdrp = elf_elfheader (abfd);
7014
7015
687
  shstrtab = _bfd_elf_strtab_init ();
7016
687
  if (shstrtab == NULL)
7017
0
    return false;
7018
7019
687
  elf_shstrtab (abfd) = shstrtab;
7020
7021
687
  i_ehdrp->e_ident[EI_MAG0] = ELFMAG0;
7022
687
  i_ehdrp->e_ident[EI_MAG1] = ELFMAG1;
7023
687
  i_ehdrp->e_ident[EI_MAG2] = ELFMAG2;
7024
687
  i_ehdrp->e_ident[EI_MAG3] = ELFMAG3;
7025
7026
687
  i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass;
7027
687
  i_ehdrp->e_ident[EI_DATA] =
7028
687
    bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB;
7029
687
  i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current;
7030
7031
687
  if ((abfd->flags & DYNAMIC) != 0)
7032
52
    i_ehdrp->e_type = ET_DYN;
7033
635
  else if ((abfd->flags & EXEC_P) != 0)
7034
94
    i_ehdrp->e_type = ET_EXEC;
7035
541
  else if (bfd_get_format (abfd) == bfd_core)
7036
253
    i_ehdrp->e_type = ET_CORE;
7037
288
  else
7038
288
    i_ehdrp->e_type = ET_REL;
7039
7040
687
  switch (bfd_get_arch (abfd))
7041
687
    {
7042
0
    case bfd_arch_unknown:
7043
0
      i_ehdrp->e_machine = EM_NONE;
7044
0
      break;
7045
7046
      /* There used to be a long list of cases here, each one setting
7047
   e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
7048
   in the corresponding bfd definition.  To avoid duplication,
7049
   the switch was removed.  Machines that need special handling
7050
   can generally do it in elf_backend_final_write_processing(),
7051
   unless they need the information earlier than the final write.
7052
   Such need can generally be supplied by replacing the tests for
7053
   e_machine with the conditions used to determine it.  */
7054
687
    default:
7055
687
      i_ehdrp->e_machine = bed->elf_machine_code;
7056
687
    }
7057
7058
687
  i_ehdrp->e_version = bed->s->ev_current;
7059
687
  i_ehdrp->e_ehsize = bed->s->sizeof_ehdr;
7060
7061
  /* No program header, for now.  */
7062
687
  i_ehdrp->e_phoff = 0;
7063
687
  i_ehdrp->e_phentsize = 0;
7064
687
  i_ehdrp->e_phnum = 0;
7065
7066
  /* Each bfd section is section header entry.  */
7067
687
  i_ehdrp->e_entry = bfd_get_start_address (abfd);
7068
687
  i_ehdrp->e_shentsize = bed->s->sizeof_shdr;
7069
7070
687
  elf_tdata (abfd)->symtab_hdr.sh_name =
7071
687
    (unsigned int) _bfd_elf_strtab_add (shstrtab, ".symtab", false);
7072
687
  elf_tdata (abfd)->strtab_hdr.sh_name =
7073
687
    (unsigned int) _bfd_elf_strtab_add (shstrtab, ".strtab", false);
7074
687
  elf_tdata (abfd)->shstrtab_hdr.sh_name =
7075
687
    (unsigned int) _bfd_elf_strtab_add (shstrtab, ".shstrtab", false);
7076
687
  if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
7077
687
      || elf_tdata (abfd)->strtab_hdr.sh_name == (unsigned int) -1
7078
687
      || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1)
7079
0
    return false;
7080
7081
687
  return true;
7082
687
}
7083
7084
/* Set e_type in ELF header to ET_EXEC for -pie -Ttext-segment=.
7085
7086
   FIXME: We used to have code here to sort the PT_LOAD segments into
7087
   ascending order, as per the ELF spec.  But this breaks some programs,
7088
   including the Linux kernel.  But really either the spec should be
7089
   changed or the programs updated.  */
7090
7091
bool
7092
_bfd_elf_modify_headers (bfd *obfd, struct bfd_link_info *link_info)
7093
658
{
7094
658
  if (link_info != NULL && bfd_link_pie (link_info))
7095
0
    {
7096
0
      Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (obfd);
7097
0
      unsigned int num_segments = i_ehdrp->e_phnum;
7098
0
      struct elf_obj_tdata *tdata = elf_tdata (obfd);
7099
0
      Elf_Internal_Phdr *segment = tdata->phdr;
7100
0
      Elf_Internal_Phdr *end_segment = &segment[num_segments];
7101
7102
      /* Find the lowest p_vaddr in PT_LOAD segments.  */
7103
0
      bfd_vma p_vaddr = (bfd_vma) -1;
7104
0
      for (; segment < end_segment; segment++)
7105
0
  if (segment->p_type == PT_LOAD && p_vaddr > segment->p_vaddr)
7106
0
    p_vaddr = segment->p_vaddr;
7107
7108
      /* Set e_type to ET_EXEC if the lowest p_vaddr in PT_LOAD
7109
   segments is non-zero.  */
7110
0
      if (p_vaddr)
7111
0
  i_ehdrp->e_type = ET_EXEC;
7112
0
    }
7113
658
  return true;
7114
658
}
7115
7116
/* Assign file positions for all the reloc sections which are not part
7117
   of the loadable file image, and the file position of section headers.  */
7118
7119
static bool
7120
_bfd_elf_assign_file_positions_for_non_load (bfd *abfd)
7121
653
{
7122
653
  ufile_ptr off;
7123
653
  Elf_Internal_Shdr **shdrpp, **end_shdrpp;
7124
653
  Elf_Internal_Shdr *shdrp;
7125
653
  Elf_Internal_Ehdr *i_ehdrp;
7126
653
  elf_backend_data *bed = get_elf_backend_data (abfd);
7127
7128
  /* Skip non-load sections without section header.  */
7129
653
  if ((abfd->flags & BFD_NO_SECTION_HEADER) != 0)
7130
0
    return true;
7131
7132
653
  off = elf_next_file_pos (abfd);
7133
7134
653
  shdrpp = elf_elfsections (abfd);
7135
653
  end_shdrpp = shdrpp + elf_numsections (abfd);
7136
3.33k
  for (shdrpp++; shdrpp < end_shdrpp; shdrpp++)
7137
2.67k
    {
7138
2.67k
      shdrp = *shdrpp;
7139
2.67k
      if (shdrp->sh_offset == -1)
7140
780
  {
7141
780
    asection *sec = shdrp->bfd_section;
7142
780
    if (sec == NULL
7143
0
        || shdrp->sh_type == SHT_REL
7144
0
        || shdrp->sh_type == SHT_RELA)
7145
780
      ;
7146
0
    else if (bfd_section_is_ctf (sec))
7147
0
      {
7148
        /* Update section size and contents.  */
7149
0
        shdrp->sh_size = sec->size;
7150
0
        shdrp->contents = sec->contents;
7151
0
      }
7152
0
    else if (shdrp->sh_name == -1u)
7153
0
      {
7154
0
        const char *name = sec->name;
7155
0
        struct bfd_elf_section_data *d;
7156
7157
        /* Compress DWARF debug sections.  */
7158
0
        if (!bfd_compress_section (abfd, sec, shdrp->contents))
7159
0
    return false;
7160
7161
0
        if (sec->compress_status == COMPRESS_SECTION_DONE
7162
0
      && (abfd->flags & BFD_COMPRESS_GABI) == 0
7163
0
      && name[1] == 'd')
7164
0
    {
7165
      /* If section is compressed with zlib-gnu, convert
7166
         section name from .debug_* to .zdebug_*.  */
7167
0
      char *new_name = bfd_debug_name_to_zdebug (abfd, name);
7168
0
      if (new_name == NULL)
7169
0
        return false;
7170
0
      name = new_name;
7171
0
    }
7172
        /* Add section name to section name section.  */
7173
0
        shdrp->sh_name
7174
0
    = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
7175
0
                  name, false);
7176
0
        d = elf_section_data (sec);
7177
7178
        /* Add reloc section name to section name section.  */
7179
0
        if (d->rel.hdr
7180
0
      && !_bfd_elf_set_reloc_sh_name (abfd, d->rel.hdr,
7181
0
              name, false))
7182
0
    return false;
7183
0
        if (d->rela.hdr
7184
0
      && !_bfd_elf_set_reloc_sh_name (abfd, d->rela.hdr,
7185
0
              name, true))
7186
0
    return false;
7187
7188
        /* Update section size and contents.  */
7189
0
        shdrp->sh_size = sec->size;
7190
0
        shdrp->contents = sec->contents;
7191
0
        sec->contents = NULL;
7192
0
      }
7193
7194
780
    off = _bfd_elf_assign_file_position_for_section (shdrp, off,
7195
780
      (abfd->flags & (EXEC_P | DYNAMIC))
7196
641
      || bfd_get_format (abfd) == bfd_core,
7197
780
      bed->s->log_file_align);
7198
780
  }
7199
2.67k
    }
7200
7201
  /* Place section name section after DWARF debug sections have been
7202
     compressed.  */
7203
653
  _bfd_elf_strtab_finalize (elf_shstrtab (abfd));
7204
653
  shdrp = &elf_tdata (abfd)->shstrtab_hdr;
7205
653
  shdrp->sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd));
7206
653
  off = _bfd_elf_assign_file_position_for_section (shdrp, off, true, 0);
7207
7208
  /* Place the section headers.  */
7209
653
  i_ehdrp = elf_elfheader (abfd);
7210
653
  off = BFD_ALIGN (off, 1u << bed->s->log_file_align);
7211
653
  i_ehdrp->e_shoff = off;
7212
653
  off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize;
7213
653
  elf_next_file_pos (abfd) = off;
7214
7215
653
  return true;
7216
653
}
7217
7218
bool
7219
_bfd_elf_write_object_contents (bfd *abfd)
7220
664
{
7221
664
  elf_backend_data *bed = get_elf_backend_data (abfd);
7222
664
  Elf_Internal_Shdr **i_shdrp;
7223
664
  bool failed;
7224
664
  unsigned int count, num_sec;
7225
664
  struct elf_obj_tdata *t;
7226
7227
664
  if (! abfd->output_has_begun
7228
477
      && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
7229
11
    return false;
7230
  /* Do not rewrite ELF data when the BFD has been opened for update.
7231
     abfd->output_has_begun was set to TRUE on opening, so creation of
7232
     new sections, and modification of existing section sizes was
7233
     restricted.  This means the ELF header, program headers and
7234
     section headers can't have changed.  If the contents of any
7235
     sections has been modified, then those changes have already been
7236
     written to the BFD.  */
7237
653
  else if (abfd->direction == both_direction)
7238
0
    {
7239
0
      BFD_ASSERT (abfd->output_has_begun);
7240
0
      return true;
7241
0
    }
7242
7243
653
  i_shdrp = elf_elfsections (abfd);
7244
7245
653
  failed = false;
7246
653
  bfd_map_over_sections (abfd, bed->s->write_relocs, &failed);
7247
653
  if (failed)
7248
0
    return false;
7249
7250
653
  if (!_bfd_elf_assign_file_positions_for_non_load (abfd))
7251
0
    return false;
7252
7253
  /* After writing the headers, we need to write the sections too...  */
7254
653
  num_sec = elf_numsections (abfd);
7255
3.33k
  for (count = 1; count < num_sec; count++)
7256
2.67k
    {
7257
      /* Don't set the sh_name field without section header.  */
7258
2.67k
      if ((abfd->flags & BFD_NO_SECTION_HEADER) == 0)
7259
2.67k
  i_shdrp[count]->sh_name
7260
2.67k
    = _bfd_elf_strtab_offset (elf_shstrtab (abfd),
7261
2.67k
            i_shdrp[count]->sh_name);
7262
2.67k
      if (bed->elf_backend_section_processing)
7263
385
  if (!(*bed->elf_backend_section_processing) (abfd, i_shdrp[count]))
7264
0
    return false;
7265
2.67k
      if (i_shdrp[count]->contents)
7266
224
  {
7267
224
    bfd_size_type amt = i_shdrp[count]->sh_size;
7268
7269
224
    if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0
7270
224
        || bfd_write (i_shdrp[count]->contents, amt, abfd) != amt)
7271
0
      return false;
7272
224
  }
7273
2.67k
    }
7274
7275
  /* Write out the section header names.  */
7276
653
  t = elf_tdata (abfd);
7277
653
  if (elf_shstrtab (abfd) != NULL
7278
653
      && t->shstrtab_hdr.sh_offset != -1
7279
653
      && (bfd_seek (abfd, t->shstrtab_hdr.sh_offset, SEEK_SET) != 0
7280
633
    || !_bfd_elf_strtab_emit (abfd, elf_shstrtab (abfd))))
7281
20
    return false;
7282
7283
633
  if (!(*bed->elf_backend_final_write_processing) (abfd))
7284
0
    return false;
7285
7286
633
  if (!bed->s->write_shdrs_and_ehdr (abfd))
7287
0
    return false;
7288
7289
  /* This is last since write_shdrs_and_ehdr can touch i_shdrp[0].  */
7290
633
  if (t->o->build_id.after_write_object_contents != NULL
7291
0
      && !(*t->o->build_id.after_write_object_contents) (abfd))
7292
0
    return false;
7293
633
  if (t->o->package_metadata.after_write_object_contents != NULL
7294
0
      && !(*t->o->package_metadata.after_write_object_contents) (abfd))
7295
0
    return false;
7296
7297
633
  return true;
7298
633
}
7299
7300
bool
7301
_bfd_elf_write_corefile_contents (bfd *abfd)
7302
240
{
7303
  /* Hopefully this can be done just like an object file.  */
7304
240
  return _bfd_elf_write_object_contents (abfd);
7305
240
}
7306
7307
/* Given a section, search the header to find them.  */
7308
7309
unsigned int
7310
_bfd_elf_section_from_bfd_section (bfd *abfd, struct bfd_section *asect)
7311
1.30k
{
7312
1.30k
  elf_backend_data *bed;
7313
1.30k
  unsigned int sec_index;
7314
7315
1.30k
  if (elf_section_data (asect) != NULL
7316
599
      && elf_section_data (asect)->this_idx != 0)
7317
599
    return elf_section_data (asect)->this_idx;
7318
7319
704
  if (bfd_is_abs_section (asect))
7320
0
    sec_index = SHN_ABS;
7321
704
  else if (bfd_is_com_section (asect))
7322
20
    sec_index = SHN_COMMON;
7323
684
  else if (bfd_is_und_section (asect))
7324
683
    sec_index = SHN_UNDEF;
7325
1
  else
7326
1
    sec_index = SHN_BAD;
7327
7328
704
  bed = get_elf_backend_data (abfd);
7329
704
  if (bed->elf_backend_section_from_bfd_section)
7330
524
    {
7331
524
      int retval = sec_index;
7332
7333
524
      if ((*bed->elf_backend_section_from_bfd_section) (abfd, asect, &retval))
7334
21
  return retval;
7335
524
    }
7336
7337
683
  if (sec_index == SHN_BAD)
7338
0
    bfd_set_error (bfd_error_nonrepresentable_section);
7339
7340
683
  return sec_index;
7341
704
}
7342
7343
/* Given a BFD symbol, return the index in the ELF symbol table, or -1
7344
   on error.  */
7345
7346
int
7347
_bfd_elf_symbol_from_bfd_symbol (bfd *abfd, asymbol **asym_ptr_ptr)
7348
455
{
7349
455
  asymbol *asym_ptr = *asym_ptr_ptr;
7350
455
  int idx;
7351
455
  flagword flags = asym_ptr->flags;
7352
7353
  /* When gas creates relocations against local labels, it creates its
7354
     own symbol for the section, but does put the symbol into the
7355
     symbol chain, so udata is 0.  When the linker is generating
7356
     relocatable output, this section symbol may be for one of the
7357
     input sections rather than the output section.  */
7358
455
  if (asym_ptr->udata.i == 0
7359
0
      && (flags & BSF_SECTION_SYM)
7360
0
      && asym_ptr->section)
7361
0
    {
7362
0
      asection *sec;
7363
7364
0
      sec = asym_ptr->section;
7365
0
      if (sec->owner != abfd && sec->output_section != NULL)
7366
0
  sec = sec->output_section;
7367
0
      if (sec->owner == abfd
7368
0
    && sec->index < elf_num_section_syms (abfd)
7369
0
    && elf_section_syms (abfd)[sec->index] != NULL)
7370
0
  asym_ptr->udata.i = elf_section_syms (abfd)[sec->index]->udata.i;
7371
0
    }
7372
7373
455
  idx = asym_ptr->udata.i;
7374
7375
455
  if (idx == 0)
7376
0
    {
7377
      /* This case can occur when using --strip-symbol on a symbol
7378
   which is used in a relocation entry.  */
7379
0
      _bfd_error_handler
7380
  /* xgettext:c-format */
7381
0
  (_("%pB: symbol `%s' required but not present"),
7382
0
   abfd, bfd_asymbol_name (asym_ptr));
7383
0
      bfd_set_error (bfd_error_no_symbols);
7384
0
      return -1;
7385
0
    }
7386
7387
#if DEBUG & 4
7388
  {
7389
    fprintf (stderr,
7390
       "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d,"
7391
       " flags = 0x%.8x\n",
7392
       (long) asym_ptr, asym_ptr->name, idx, flags);
7393
    fflush (stderr);
7394
  }
7395
#endif
7396
7397
455
  return idx;
7398
455
}
7399
7400
static inline bfd_vma
7401
segment_size (Elf_Internal_Phdr *segment)
7402
6.03k
{
7403
6.03k
  return (segment->p_memsz > segment->p_filesz
7404
6.03k
    ? segment->p_memsz : segment->p_filesz);
7405
6.03k
}
7406
7407
7408
/* Returns the end address of the segment + 1.  */
7409
static inline bfd_vma
7410
segment_end (Elf_Internal_Phdr *segment, bfd_vma start)
7411
219
{
7412
219
  return start + segment_size (segment);
7413
219
}
7414
7415
static inline bfd_size_type
7416
section_size (asection *section, Elf_Internal_Phdr *segment)
7417
5.84k
{
7418
5.84k
  if ((section->flags & SEC_HAS_CONTENTS) != 0
7419
1.11k
      || (section->flags & SEC_THREAD_LOCAL) == 0
7420
32
      || segment->p_type == PT_TLS)
7421
5.81k
    return section->size;
7422
32
  return 0;
7423
5.84k
}
7424
7425
/* Returns TRUE if the given section is contained within the given
7426
   segment.  LMA addresses are compared against PADDR when
7427
   USE_VADDR is false, VMA against VADDR when true.  */
7428
static bool
7429
is_contained_by (asection *section, Elf_Internal_Phdr *segment,
7430
     bfd_vma paddr, bfd_vma vaddr, unsigned int opb,
7431
     bool use_vaddr)
7432
4.49k
{
7433
4.49k
  bfd_vma seg_addr = !use_vaddr ? paddr : vaddr;
7434
4.49k
  bfd_vma addr = !use_vaddr ? section->lma : section->vma;
7435
4.49k
  bfd_vma octet;
7436
4.49k
  if (_bfd_mul_overflow (addr, opb, &octet))
7437
0
    return false;
7438
  /* The third and fourth lines below are testing that the section end
7439
     address is within the segment.  It's written this way to avoid
7440
     overflow.  Add seg_addr + section_size to both sides of the
7441
     inequality to make it obvious.  */
7442
4.49k
  return (octet >= seg_addr
7443
3.16k
    && segment_size (segment) >= section_size (section, segment)
7444
2.65k
    && (octet - seg_addr
7445
2.65k
        <= segment_size (segment) - section_size (section, segment)));
7446
4.49k
}
7447
7448
/* Handle PT_NOTE segment.  */
7449
static bool
7450
is_note (asection *s, Elf_Internal_Phdr *p)
7451
2.94k
{
7452
2.94k
  return (p->p_type == PT_NOTE
7453
230
    && elf_section_type (s) == SHT_NOTE
7454
53
    && (ufile_ptr) s->filepos >= p->p_offset
7455
39
    && p->p_filesz >= s->size
7456
39
    && (ufile_ptr) s->filepos - p->p_offset <= p->p_filesz - s->size);
7457
2.94k
}
7458
7459
/* Rewrite program header information.  */
7460
7461
static bool
7462
rewrite_elf_program_header (bfd *ibfd, bfd *obfd, bfd_vma maxpagesize)
7463
36
{
7464
36
  Elf_Internal_Ehdr *iehdr;
7465
36
  struct elf_segment_map *map;
7466
36
  struct elf_segment_map *map_first;
7467
36
  struct elf_segment_map **pointer_to_map;
7468
36
  Elf_Internal_Phdr *segment;
7469
36
  asection *section;
7470
36
  unsigned int i;
7471
36
  unsigned int num_segments;
7472
36
  bool phdr_included = false;
7473
36
  bool p_paddr_valid;
7474
36
  struct elf_segment_map *phdr_adjust_seg = NULL;
7475
36
  unsigned int phdr_adjust_num = 0;
7476
36
  elf_backend_data *bed;
7477
36
  unsigned int opb = bfd_octets_per_byte (ibfd, NULL);
7478
7479
36
  bed = get_elf_backend_data (ibfd);
7480
36
  iehdr = elf_elfheader (ibfd);
7481
7482
36
  map_first = NULL;
7483
36
  pointer_to_map = &map_first;
7484
7485
36
  num_segments = elf_elfheader (ibfd)->e_phnum;
7486
7487
  /* The complicated case when p_vaddr is 0 is to handle the Solaris
7488
     linker, which generates a PT_INTERP section with p_vaddr and
7489
     p_memsz set to 0.  */
7490
36
#define IS_SOLARIS_PT_INTERP(p, s)          \
7491
435
  (p->p_vaddr == 0              \
7492
435
   && p->p_paddr == 0              \
7493
435
   && p->p_memsz == 0              \
7494
435
   && p->p_filesz > 0              \
7495
435
   && (s->flags & SEC_HAS_CONTENTS) != 0        \
7496
435
   && s->size > 0              \
7497
435
   && (bfd_vma) s->filepos >= p->p_offset        \
7498
435
   && ((bfd_vma) s->filepos + s->size         \
7499
2
       <= p->p_offset + p->p_filesz))
7500
7501
  /* Decide if the given section should be included in the given segment.
7502
     A section will be included if:
7503
       1. It is within the address space of the segment -- we use the LMA
7504
    if that is set for the segment and the VMA otherwise,
7505
       2. It is an allocated section or a NOTE section in a PT_NOTE
7506
    segment.
7507
       3. There is an output section associated with it,
7508
       4. The section has not already been allocated to a previous segment.
7509
       5. PT_GNU_STACK segments do not include any sections.
7510
       6. PT_TLS segment includes only SHF_TLS sections.
7511
       7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments.
7512
       8. PT_DYNAMIC should not contain empty sections at the beginning
7513
    (with the possible exception of .dynamic).  */
7514
36
#define IS_SECTION_IN_INPUT_SEGMENT(section, segment, opb, paddr_valid) \
7515
4.63k
  (((is_contained_by (section, segment, segment->p_paddr,    \
7516
3.97k
          segment->p_vaddr, opb, !paddr_valid)    \
7517
3.97k
     && (section->flags & SEC_ALLOC) != 0)       \
7518
3.97k
    || is_note (section, segment))         \
7519
3.97k
   && segment->p_type != PT_GNU_STACK          \
7520
3.97k
   && (segment->p_type != PT_TLS          \
7521
1.05k
       || (section->flags & SEC_THREAD_LOCAL))        \
7522
3.97k
   && (segment->p_type == PT_LOAD          \
7523
1.05k
       || segment->p_type == PT_TLS          \
7524
1.05k
       || (section->flags & SEC_THREAD_LOCAL) == 0)      \
7525
3.97k
   && (segment->p_type != PT_DYNAMIC          \
7526
1.04k
       || section_size (section, segment) > 0        \
7527
1.04k
       || (segment->p_paddr            \
7528
0
     ? segment->p_paddr != section->lma * (opb)      \
7529
0
     : segment->p_vaddr != section->vma * (opb))      \
7530
1.04k
       || (streq (bfd_section_name (section), ".dynamic")))    \
7531
4.63k
   && (segment->p_type != PT_LOAD || !section->segment_mark))
7532
7533
/* If the output section of a section in the input segment is NULL,
7534
   it is removed from the corresponding output segment.   */
7535
36
#define INCLUDE_SECTION_IN_SEGMENT(section, segment, opb, paddr_valid)  \
7536
662
  (IS_SECTION_IN_INPUT_SEGMENT (section, segment, opb, paddr_valid) \
7537
662
   && section->output_section != NULL)
7538
7539
  /* Returns TRUE iff seg1 starts after the end of seg2.  */
7540
36
#define SEGMENT_AFTER_SEGMENT(seg1, seg2, field)      \
7541
280
  (seg1->field >= segment_end (seg2, seg2->field))
7542
7543
  /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both
7544
     their VMA address ranges and their LMA address ranges overlap.
7545
     It is possible to have overlapping VMA ranges without overlapping LMA
7546
     ranges.  RedBoot images for example can have both .data and .bss mapped
7547
     to the same VMA range, but with the .data section mapped to a different
7548
     LMA.  */
7549
36
#define SEGMENT_OVERLAPS(seg1, seg2)          \
7550
66
  (   !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr)      \
7551
66
  || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr))     \
7552
66
   && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr)      \
7553
33
  || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr)))
7554
7555
  /* Initialise the segment mark field, and discard stupid alignment.  */
7556
446
  for (section = ibfd->sections; section != NULL; section = section->next)
7557
410
    {
7558
410
      asection *o = section->output_section;
7559
410
      if (o != NULL && o->alignment_power >= (sizeof (bfd_vma) * 8) - 1)
7560
0
  o->alignment_power = 0;
7561
410
      section->segment_mark = false;
7562
410
    }
7563
7564
  /* The Solaris linker creates program headers in which all the
7565
     p_paddr fields are zero.  When we try to objcopy or strip such a
7566
     file, we get confused.  Check for this case, and if we find it
7567
     don't set the p_paddr_valid fields.  */
7568
36
  p_paddr_valid = false;
7569
36
  for (i = 0, segment = elf_tdata (ibfd)->phdr;
7570
52
       i < num_segments;
7571
36
       i++, segment++)
7572
52
    if (segment->p_paddr != 0)
7573
36
      {
7574
36
  p_paddr_valid = true;
7575
36
  break;
7576
36
      }
7577
7578
  /* Scan through the segments specified in the program header
7579
     of the input BFD.  For this first scan we look for overlaps
7580
     in the loadable segments.  These can be created by weird
7581
     parameters to objcopy.  Also, fix some solaris weirdness.  */
7582
36
  for (i = 0, segment = elf_tdata (ibfd)->phdr;
7583
374
       i < num_segments;
7584
338
       i++, segment++)
7585
338
    {
7586
338
      unsigned int j;
7587
338
      Elf_Internal_Phdr *segment2;
7588
7589
338
      if (segment->p_type == PT_INTERP)
7590
463
  for (section = ibfd->sections; section; section = section->next)
7591
435
    if (IS_SOLARIS_PT_INTERP (segment, section))
7592
1
      {
7593
        /* Mininal change so that the normal section to segment
7594
     assignment code will work.  */
7595
1
        segment->p_vaddr = section->vma * opb;
7596
1
        break;
7597
1
      }
7598
7599
338
      if (segment->p_type != PT_LOAD)
7600
250
  {
7601
    /* Remove PT_GNU_RELRO segment.  */
7602
250
    if (segment->p_type == PT_GNU_RELRO)
7603
8
      segment->p_type = PT_NULL;
7604
250
    continue;
7605
250
  }
7606
7607
      /* Determine if this segment overlaps any previous segments.  */
7608
318
      for (j = 0, segment2 = elf_tdata (ibfd)->phdr; j < i; j++, segment2++)
7609
244
  {
7610
244
    bfd_signed_vma extra_length;
7611
7612
244
    if (segment2->p_type != PT_LOAD
7613
66
        || !SEGMENT_OVERLAPS (segment, segment2))
7614
225
      continue;
7615
7616
    /* Merge the two segments together.  */
7617
19
    if (segment2->p_vaddr < segment->p_vaddr)
7618
14
      {
7619
        /* Extend SEGMENT2 to include SEGMENT and then delete
7620
     SEGMENT.  */
7621
14
        extra_length = (segment_end (segment, segment->p_vaddr)
7622
14
            - segment_end (segment2, segment2->p_vaddr));
7623
7624
14
        if (extra_length > 0)
7625
6
    {
7626
6
      segment2->p_memsz += extra_length;
7627
6
      segment2->p_filesz += extra_length;
7628
6
    }
7629
7630
14
        segment->p_type = PT_NULL;
7631
7632
        /* Since we have deleted P we must restart the outer loop.  */
7633
14
        i = 0;
7634
14
        segment = elf_tdata (ibfd)->phdr;
7635
14
        break;
7636
14
      }
7637
5
    else
7638
5
      {
7639
        /* Extend SEGMENT to include SEGMENT2 and then delete
7640
     SEGMENT2.  */
7641
5
        extra_length = (segment_end (segment2, segment2->p_vaddr)
7642
5
            - segment_end (segment, segment->p_vaddr));
7643
7644
5
        if (extra_length > 0)
7645
3
    {
7646
3
      segment->p_memsz += extra_length;
7647
3
      segment->p_filesz += extra_length;
7648
3
    }
7649
7650
5
        segment2->p_type = PT_NULL;
7651
5
      }
7652
19
  }
7653
88
    }
7654
7655
  /* The second scan attempts to assign sections to segments.  */
7656
36
  for (i = 0, segment = elf_tdata (ibfd)->phdr;
7657
326
       i < num_segments;
7658
290
       i++, segment++)
7659
290
    {
7660
290
      unsigned int section_count;
7661
290
      asection **sections;
7662
290
      asection *output_section;
7663
290
      unsigned int isec;
7664
290
      asection *matching_lma;
7665
290
      asection *suggested_lma;
7666
290
      unsigned int j;
7667
290
      size_t amt;
7668
290
      asection *first_section;
7669
7670
290
      if (segment->p_type == PT_NULL)
7671
78
  continue;
7672
7673
212
      first_section = NULL;
7674
      /* Compute how many sections might be placed into this segment.  */
7675
212
      for (section = ibfd->sections, section_count = 0;
7676
3.52k
     section != NULL;
7677
3.31k
     section = section->next)
7678
3.31k
  {
7679
    /* Find the first section in the input segment, which may be
7680
       removed from the corresponding output segment.   */
7681
3.31k
    if (IS_SECTION_IN_INPUT_SEGMENT (section, segment, opb, p_paddr_valid))
7682
521
      {
7683
521
        if (first_section == NULL)
7684
95
    first_section = section;
7685
521
        if (section->output_section != NULL)
7686
521
    ++section_count;
7687
521
      }
7688
3.31k
  }
7689
7690
      /* Allocate a segment map big enough to contain
7691
   all of the sections we have selected.  */
7692
212
      amt = sizeof (struct elf_segment_map) - sizeof (asection *);
7693
212
      amt += section_count * sizeof (asection *);
7694
212
      map = bfd_zalloc (obfd, amt);
7695
212
      if (map == NULL)
7696
0
  return false;
7697
7698
      /* Initialise the fields of the segment map.  Default to
7699
   using the physical address of the segment in the input BFD.  */
7700
212
      map->next = NULL;
7701
212
      map->p_type = segment->p_type;
7702
212
      map->p_flags = segment->p_flags;
7703
212
      map->p_flags_valid = 1;
7704
7705
212
      if (map->p_type == PT_LOAD
7706
54
    && (ibfd->flags & D_PAGED) != 0
7707
13
    && maxpagesize > 1
7708
13
    && segment->p_align > 1)
7709
6
  {
7710
6
    map->p_align = segment->p_align;
7711
6
    if (segment->p_align > maxpagesize)
7712
0
      map->p_align = maxpagesize;
7713
6
    map->p_align_valid = 1;
7714
6
  }
7715
7716
      /* If the first section in the input segment is removed, there is
7717
   no need to preserve segment physical address in the corresponding
7718
   output segment.  */
7719
212
      if (!first_section || first_section->output_section != NULL)
7720
212
  {
7721
212
    map->p_paddr = segment->p_paddr;
7722
212
    map->p_paddr_valid = p_paddr_valid;
7723
212
  }
7724
7725
      /* Determine if this segment contains the ELF file header
7726
   and if it contains the program headers themselves.  */
7727
212
      map->includes_filehdr = (segment->p_offset == 0
7728
62
             && segment->p_filesz >= iehdr->e_ehsize);
7729
212
      map->includes_phdrs = 0;
7730
7731
212
      if (!phdr_included || segment->p_type != PT_LOAD)
7732
206
  {
7733
206
    map->includes_phdrs =
7734
206
      (segment->p_offset <= (bfd_vma) iehdr->e_phoff
7735
78
       && (segment->p_offset + segment->p_filesz
7736
78
     >= ((bfd_vma) iehdr->e_phoff
7737
78
         + iehdr->e_phnum * iehdr->e_phentsize)));
7738
7739
206
    if (segment->p_type == PT_LOAD && map->includes_phdrs)
7740
16
      phdr_included = true;
7741
206
  }
7742
7743
212
      if (section_count == 0)
7744
117
  {
7745
    /* Special segments, such as the PT_PHDR segment, may contain
7746
       no sections, but ordinary, loadable segments should contain
7747
       something.  They are allowed by the ELF spec however, so only
7748
       a warning is produced.
7749
       Don't warn if an empty PT_LOAD contains the program headers.
7750
       There is however the valid use case of embedded systems which
7751
       have segments with p_filesz of 0 and a p_memsz > 0 to initialize
7752
       flash memory with zeros.  No warning is shown for that case.  */
7753
117
    if (segment->p_type == PT_LOAD
7754
16
        && !map->includes_phdrs
7755
16
        && (segment->p_filesz > 0 || segment->p_memsz == 0))
7756
      /* xgettext:c-format */
7757
16
      _bfd_error_handler
7758
16
        (_("%pB: warning: empty loadable segment detected"
7759
16
     " at vaddr=%#" PRIx64 ", is this intentional?"),
7760
16
         ibfd, (uint64_t) segment->p_vaddr);
7761
7762
117
    map->p_vaddr_offset = segment->p_vaddr / opb;
7763
117
    map->count = 0;
7764
117
    *pointer_to_map = map;
7765
117
    pointer_to_map = &map->next;
7766
7767
117
    continue;
7768
117
  }
7769
7770
      /* Now scan the sections in the input BFD again and attempt
7771
   to add their corresponding output sections to the segment map.
7772
   The problem here is how to handle an output section which has
7773
   been moved (ie had its LMA changed).  There are four possibilities:
7774
7775
   1. None of the sections have been moved.
7776
      In this case we can continue to use the segment LMA from the
7777
      input BFD.
7778
7779
   2. All of the sections have been moved by the same amount.
7780
      In this case we can change the segment's LMA to match the LMA
7781
      of the first section.
7782
7783
   3. Some of the sections have been moved, others have not.
7784
      In this case those sections which have not been moved can be
7785
      placed in the current segment which will have to have its size,
7786
      and possibly its LMA changed, and a new segment or segments will
7787
      have to be created to contain the other sections.
7788
7789
   4. The sections have been moved, but not by the same amount.
7790
      In this case we can change the segment's LMA to match the LMA
7791
      of the first section and we will have to create a new segment
7792
      or segments to contain the other sections.
7793
7794
   In order to save time, we allocate an array to hold the section
7795
   pointers that we are interested in.  As these sections get assigned
7796
   to a segment, they are removed from this array.  */
7797
7798
95
      sections = bfd_malloc (section_count * sizeof (*sections));
7799
95
      if (sections == NULL)
7800
0
  return false;
7801
7802
      /* Step One: Scan for segment vs section LMA conflicts.
7803
   Also add the sections to the section array allocated above.
7804
   Also add the sections to the current segment.  In the common
7805
   case, where the sections have not been moved, this means that
7806
   we have completely filled the segment, and there is nothing
7807
   more to do.  */
7808
95
      isec = 0;
7809
95
      matching_lma = NULL;
7810
95
      suggested_lma = NULL;
7811
7812
95
      for (section = first_section, j = 0;
7813
662
     section != NULL;
7814
567
     section = section->next)
7815
662
  {
7816
662
    if (INCLUDE_SECTION_IN_SEGMENT (section, segment, opb, p_paddr_valid))
7817
521
      {
7818
521
        output_section = section->output_section;
7819
7820
521
        sections[j++] = section;
7821
7822
        /* The Solaris native linker always sets p_paddr to 0.
7823
     We try to catch that case here, and set it to the
7824
     correct value.  Note - some backends require that
7825
     p_paddr be left as zero.  */
7826
521
        if (!p_paddr_valid
7827
0
      && segment->p_vaddr != 0
7828
0
      && !bed->want_p_paddr_set_to_zero
7829
0
      && isec == 0
7830
0
      && output_section->lma != 0
7831
0
      && (align_power (segment->p_vaddr
7832
0
           + (map->includes_filehdr
7833
0
              ? iehdr->e_ehsize : 0)
7834
0
           + (map->includes_phdrs
7835
0
              ? iehdr->e_phnum * iehdr->e_phentsize
7836
0
              : 0),
7837
0
           output_section->alignment_power * opb)
7838
0
          == (output_section->vma * opb)))
7839
0
    map->p_paddr = segment->p_vaddr;
7840
7841
        /* Match up the physical address of the segment with the
7842
     LMA address of the output section.  */
7843
521
        if (is_contained_by (output_section, segment, map->p_paddr,
7844
521
           0, opb, false)
7845
13
      || is_note (section, segment))
7846
521
    {
7847
521
      if (matching_lma == NULL
7848
426
          || output_section->lma < matching_lma->lma)
7849
116
        matching_lma = output_section;
7850
7851
      /* We assume that if the section fits within the segment
7852
         then it does not overlap any other section within that
7853
         segment.  */
7854
521
      map->sections[isec++] = output_section;
7855
521
    }
7856
0
        else if (suggested_lma == NULL)
7857
0
    suggested_lma = output_section;
7858
7859
521
        if (j == section_count)
7860
95
    break;
7861
521
      }
7862
662
  }
7863
7864
95
      BFD_ASSERT (j == section_count);
7865
7866
      /* Step Two: Adjust the physical address of the current segment,
7867
   if necessary.  */
7868
95
      if (isec == section_count)
7869
95
  {
7870
    /* All of the sections fitted within the segment as currently
7871
       specified.  This is the default case.  Add the segment to
7872
       the list of built segments and carry on to process the next
7873
       program header in the input BFD.  */
7874
95
    map->count = section_count;
7875
95
    *pointer_to_map = map;
7876
95
    pointer_to_map = &map->next;
7877
7878
95
    if (p_paddr_valid
7879
95
        && !bed->want_p_paddr_set_to_zero)
7880
95
      {
7881
95
        bfd_vma hdr_size = 0;
7882
95
        if (map->includes_filehdr)
7883
23
    hdr_size = iehdr->e_ehsize;
7884
95
        if (map->includes_phdrs)
7885
22
    hdr_size += iehdr->e_phnum * iehdr->e_phentsize;
7886
7887
        /* Account for padding before the first section in the
7888
     segment.  */
7889
95
        map->p_vaddr_offset = ((map->p_paddr + hdr_size) / opb
7890
95
             - matching_lma->lma);
7891
95
      }
7892
7893
95
    free (sections);
7894
95
    continue;
7895
95
  }
7896
0
      else
7897
0
  {
7898
    /* Change the current segment's physical address to match
7899
       the LMA of the first section that fitted, or if no
7900
       section fitted, the first section.  */
7901
0
    if (matching_lma == NULL)
7902
0
      matching_lma = suggested_lma;
7903
7904
0
    map->p_paddr = matching_lma->lma * opb;
7905
7906
    /* Offset the segment physical address from the lma
7907
       to allow for space taken up by elf headers.  */
7908
0
    if (map->includes_phdrs)
7909
0
      {
7910
0
        map->p_paddr -= iehdr->e_phnum * iehdr->e_phentsize;
7911
7912
        /* iehdr->e_phnum is just an estimate of the number
7913
     of program headers that we will need.  Make a note
7914
     here of the number we used and the segment we chose
7915
     to hold these headers, so that we can adjust the
7916
     offset when we know the correct value.  */
7917
0
        phdr_adjust_num = iehdr->e_phnum;
7918
0
        phdr_adjust_seg = map;
7919
0
      }
7920
7921
0
    if (map->includes_filehdr)
7922
0
      {
7923
0
        bfd_vma align = (bfd_vma) 1 << matching_lma->alignment_power;
7924
0
        map->p_paddr -= iehdr->e_ehsize;
7925
        /* We've subtracted off the size of headers from the
7926
     first section lma, but there may have been some
7927
     alignment padding before that section too.  Try to
7928
     account for that by adjusting the segment lma down to
7929
     the same alignment.  */
7930
0
        if (segment->p_align != 0 && segment->p_align < align)
7931
0
    align = segment->p_align;
7932
0
        map->p_paddr &= -(align * opb);
7933
0
      }
7934
0
  }
7935
7936
      /* Step Three: Loop over the sections again, this time assigning
7937
   those that fit to the current segment and removing them from the
7938
   sections array; but making sure not to leave large gaps.  Once all
7939
   possible sections have been assigned to the current segment it is
7940
   added to the list of built segments and if sections still remain
7941
   to be assigned, a new segment is constructed before repeating
7942
   the loop.  */
7943
0
      isec = 0;
7944
0
      do
7945
0
  {
7946
0
    map->count = 0;
7947
0
    suggested_lma = NULL;
7948
7949
    /* Fill the current segment with sections that fit.  */
7950
0
    for (j = 0; j < section_count; j++)
7951
0
      {
7952
0
        section = sections[j];
7953
7954
0
        if (section == NULL)
7955
0
    continue;
7956
7957
0
        output_section = section->output_section;
7958
7959
0
        BFD_ASSERT (output_section != NULL);
7960
7961
0
        if (is_contained_by (output_section, segment, map->p_paddr,
7962
0
           0, opb, false)
7963
0
      || is_note (section, segment))
7964
0
    {
7965
0
      if (map->count == 0)
7966
0
        {
7967
          /* If the first section in a segment does not start at
7968
       the beginning of the segment, then something is
7969
       wrong.  */
7970
0
          if (align_power (map->p_paddr
7971
0
               + (map->includes_filehdr
7972
0
            ? iehdr->e_ehsize : 0)
7973
0
               + (map->includes_phdrs
7974
0
            ? iehdr->e_phnum * iehdr->e_phentsize
7975
0
            : 0),
7976
0
               output_section->alignment_power * opb)
7977
0
        != output_section->lma * opb)
7978
0
      goto sorry;
7979
0
        }
7980
0
      else
7981
0
        {
7982
0
          asection *prev_sec;
7983
7984
0
          prev_sec = map->sections[map->count - 1];
7985
7986
          /* If the gap between the end of the previous section
7987
       and the start of this section is more than
7988
       maxpagesize then we need to start a new segment.  */
7989
0
          if ((BFD_ALIGN (prev_sec->lma + prev_sec->size,
7990
0
              maxpagesize)
7991
0
         < BFD_ALIGN (output_section->lma, maxpagesize))
7992
0
        || (prev_sec->lma + prev_sec->size
7993
0
            > output_section->lma))
7994
0
      {
7995
0
        if (suggested_lma == NULL)
7996
0
          suggested_lma = output_section;
7997
7998
0
        continue;
7999
0
      }
8000
0
        }
8001
8002
0
      map->sections[map->count++] = output_section;
8003
0
      ++isec;
8004
0
      sections[j] = NULL;
8005
0
      if (segment->p_type == PT_LOAD)
8006
0
        section->segment_mark = true;
8007
0
    }
8008
0
        else if (suggested_lma == NULL)
8009
0
    suggested_lma = output_section;
8010
0
      }
8011
8012
    /* PR 23932.  A corrupt input file may contain sections that cannot
8013
       be assigned to any segment - because for example they have a
8014
       negative size - or segments that do not contain any sections.
8015
       But there are also valid reasons why a segment can be empty.
8016
       So allow a count of zero.  */
8017
8018
    /* Add the current segment to the list of built segments.  */
8019
0
    *pointer_to_map = map;
8020
0
    pointer_to_map = &map->next;
8021
8022
0
    if (isec < section_count)
8023
0
      {
8024
        /* We still have not allocated all of the sections to
8025
     segments.  Create a new segment here, initialise it
8026
     and carry on looping.  */
8027
0
        amt = sizeof (struct elf_segment_map) - sizeof (asection *);
8028
0
        amt += section_count * sizeof (asection *);
8029
0
        map = bfd_zalloc (obfd, amt);
8030
0
        if (map == NULL)
8031
0
    {
8032
0
      free (sections);
8033
0
      return false;
8034
0
    }
8035
8036
        /* Initialise the fields of the segment map.  Set the physical
8037
     physical address to the LMA of the first section that has
8038
     not yet been assigned.  */
8039
0
        map->next = NULL;
8040
0
        map->p_type = segment->p_type;
8041
0
        map->p_flags = segment->p_flags;
8042
0
        map->p_flags_valid = 1;
8043
0
        map->p_paddr = suggested_lma->lma * opb;
8044
0
        map->p_paddr_valid = p_paddr_valid;
8045
0
        map->includes_filehdr = 0;
8046
0
        map->includes_phdrs = 0;
8047
0
      }
8048
8049
0
    continue;
8050
0
  sorry:
8051
0
    bfd_set_error (bfd_error_sorry);
8052
0
    free (sections);
8053
0
    return false;
8054
0
  }
8055
0
      while (isec < section_count);
8056
8057
0
      free (sections);
8058
0
    }
8059
8060
36
  elf_seg_map (obfd) = map_first;
8061
8062
  /* If we had to estimate the number of program headers that were
8063
     going to be needed, then check our estimate now and adjust
8064
     the offset if necessary.  */
8065
36
  if (phdr_adjust_seg != NULL)
8066
0
    {
8067
0
      unsigned int count;
8068
8069
0
      for (count = 0, map = map_first; map != NULL; map = map->next)
8070
0
  count++;
8071
8072
0
      if (count > phdr_adjust_num)
8073
0
  phdr_adjust_seg->p_paddr
8074
0
    -= (count - phdr_adjust_num) * iehdr->e_phentsize;
8075
8076
0
      for (map = map_first; map != NULL; map = map->next)
8077
0
  if (map->p_type == PT_PHDR)
8078
0
    {
8079
0
      bfd_vma adjust
8080
0
        = phdr_adjust_seg->includes_filehdr ? iehdr->e_ehsize : 0;
8081
0
      map->p_paddr = phdr_adjust_seg->p_paddr + adjust;
8082
0
      break;
8083
0
    }
8084
0
    }
8085
8086
36
#undef IS_SOLARIS_PT_INTERP
8087
36
#undef IS_SECTION_IN_INPUT_SEGMENT
8088
36
#undef INCLUDE_SECTION_IN_SEGMENT
8089
36
#undef SEGMENT_AFTER_SEGMENT
8090
36
#undef SEGMENT_OVERLAPS
8091
36
  return true;
8092
36
}
8093
8094
/* Return true if p_align in the ELF program header in ABFD is valid.  */
8095
8096
static bool
8097
elf_is_p_align_valid (bfd *abfd)
8098
136
{
8099
136
  unsigned int i;
8100
136
  Elf_Internal_Phdr *segment;
8101
136
  unsigned int num_segments;
8102
136
  elf_backend_data *bed = get_elf_backend_data (abfd);
8103
136
  bfd_size_type maxpagesize = bed->maxpagesize;
8104
136
  bfd_size_type p_align = bed->p_align;
8105
8106
  /* Return true if the default p_align value isn't set or the maximum
8107
     page size is the same as the minimum page size.  */
8108
136
  if (p_align == 0 || maxpagesize == bed->minpagesize)
8109
136
    return true;
8110
8111
  /* When the default p_align value is set, p_align may be set to the
8112
     default p_align value while segments are aligned to the maximum
8113
     page size.  In this case, the input p_align will be ignored and
8114
     the maximum page size will be used to align the output segments.  */
8115
0
  segment = elf_tdata (abfd)->phdr;
8116
0
  num_segments = elf_elfheader (abfd)->e_phnum;
8117
0
  for (i = 0; i < num_segments; i++, segment++)
8118
0
    if (segment->p_type == PT_LOAD
8119
0
  && (segment->p_align != p_align
8120
0
      || vma_page_aligned_bias (segment->p_vaddr,
8121
0
              segment->p_offset,
8122
0
              maxpagesize) != 0))
8123
0
      return true;
8124
8125
0
  return false;
8126
0
}
8127
8128
/* Copy ELF program header information.  */
8129
8130
static bool
8131
copy_elf_program_header (bfd *ibfd, bfd *obfd)
8132
136
{
8133
136
  Elf_Internal_Ehdr *iehdr;
8134
136
  struct elf_segment_map *map;
8135
136
  struct elf_segment_map *map_first;
8136
136
  struct elf_segment_map **pointer_to_map;
8137
136
  Elf_Internal_Phdr *segment;
8138
136
  unsigned int i;
8139
136
  unsigned int num_segments;
8140
136
  bool phdr_included = false;
8141
136
  bool p_paddr_valid;
8142
136
  bool p_palign_valid;
8143
136
  unsigned int opb = bfd_octets_per_byte (ibfd, NULL);
8144
8145
136
  iehdr = elf_elfheader (ibfd);
8146
8147
136
  map_first = NULL;
8148
136
  pointer_to_map = &map_first;
8149
8150
  /* If all the segment p_paddr fields are zero, don't set
8151
     map->p_paddr_valid.  */
8152
136
  p_paddr_valid = false;
8153
136
  num_segments = elf_elfheader (ibfd)->e_phnum;
8154
136
  for (i = 0, segment = elf_tdata (ibfd)->phdr;
8155
229
       i < num_segments;
8156
136
       i++, segment++)
8157
202
    if (segment->p_paddr != 0)
8158
109
      {
8159
109
  p_paddr_valid = true;
8160
109
  break;
8161
109
      }
8162
8163
136
  p_palign_valid = elf_is_p_align_valid (ibfd);
8164
8165
136
  for (i = 0, segment = elf_tdata (ibfd)->phdr;
8166
536
       i < num_segments;
8167
400
       i++, segment++)
8168
400
    {
8169
400
      asection *section;
8170
400
      unsigned int section_count;
8171
400
      size_t amt;
8172
400
      Elf_Internal_Shdr *this_hdr;
8173
400
      asection *first_section = NULL;
8174
400
      asection *lowest_section;
8175
8176
      /* Compute how many sections are in this segment.  */
8177
400
      for (section = ibfd->sections, section_count = 0;
8178
4.24k
     section != NULL;
8179
3.84k
     section = section->next)
8180
3.84k
  {
8181
3.84k
    this_hdr = &(elf_section_data(section)->this_hdr);
8182
3.84k
    if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
8183
1.02k
      {
8184
1.02k
        if (first_section == NULL)
8185
157
    first_section = section;
8186
1.02k
        section_count++;
8187
1.02k
      }
8188
3.84k
  }
8189
8190
      /* Allocate a segment map big enough to contain
8191
   all of the sections we have selected.  */
8192
400
      amt = sizeof (struct elf_segment_map) - sizeof (asection *);
8193
400
      amt += section_count * sizeof (asection *);
8194
400
      map = bfd_zalloc (obfd, amt);
8195
400
      if (map == NULL)
8196
0
  return false;
8197
8198
      /* Initialize the fields of the output segment map with the
8199
   input segment.  */
8200
400
      map->next = NULL;
8201
400
      map->p_type = segment->p_type;
8202
400
      map->p_flags = segment->p_flags;
8203
400
      map->p_flags_valid = 1;
8204
400
      map->p_paddr = segment->p_paddr;
8205
400
      map->p_paddr_valid = p_paddr_valid;
8206
400
      map->p_align = segment->p_align;
8207
      /* Keep p_align of PT_GNU_STACK for stack alignment.  */
8208
400
      map->p_align_valid = (map->p_type == PT_GNU_STACK
8209
398
          || p_palign_valid);
8210
400
      map->p_vaddr_offset = 0;
8211
8212
400
      if (map->p_type == PT_GNU_RELRO
8213
391
    || map->p_type == PT_GNU_STACK)
8214
11
  {
8215
    /* The PT_GNU_RELRO segment may contain the first a few
8216
       bytes in the .got.plt section even if the whole .got.plt
8217
       section isn't in the PT_GNU_RELRO segment.  We won't
8218
       change the size of the PT_GNU_RELRO segment.
8219
       Similarly, PT_GNU_STACK size is significant on uclinux
8220
       systems.    */
8221
11
    map->p_size = segment->p_memsz;
8222
11
    map->p_size_valid = 1;
8223
11
  }
8224
8225
      /* Determine if this segment contains the ELF file header
8226
   and if it contains the program headers themselves.  */
8227
400
      map->includes_filehdr = (segment->p_offset == 0
8228
162
             && segment->p_filesz >= iehdr->e_ehsize);
8229
8230
400
      map->includes_phdrs = 0;
8231
400
      if (! phdr_included || segment->p_type != PT_LOAD)
8232
393
  {
8233
393
    map->includes_phdrs =
8234
393
      (segment->p_offset <= (bfd_vma) iehdr->e_phoff
8235
185
       && (segment->p_offset + segment->p_filesz
8236
185
     >= ((bfd_vma) iehdr->e_phoff
8237
185
         + iehdr->e_phnum * iehdr->e_phentsize)));
8238
8239
393
    if (segment->p_type == PT_LOAD && map->includes_phdrs)
8240
12
      phdr_included = true;
8241
393
  }
8242
8243
400
      lowest_section = NULL;
8244
400
      if (section_count != 0)
8245
157
  {
8246
157
    unsigned int isec = 0;
8247
8248
157
    for (section = first_section;
8249
1.11k
         section != NULL;
8250
958
         section = section->next)
8251
1.11k
      {
8252
1.11k
        this_hdr = &(elf_section_data(section)->this_hdr);
8253
1.11k
        if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
8254
1.02k
    {
8255
1.02k
      map->sections[isec++] = section->output_section;
8256
1.02k
      if ((section->flags & SEC_ALLOC) != 0)
8257
240
        {
8258
240
          bfd_vma seg_off;
8259
8260
240
          if (lowest_section == NULL
8261
161
        || section->lma < lowest_section->lma)
8262
84
      lowest_section = section;
8263
8264
          /* Section lmas are set up from PT_LOAD header
8265
       p_paddr in _bfd_elf_make_section_from_shdr.
8266
       If this header has a p_paddr that disagrees
8267
       with the section lma, flag the p_paddr as
8268
       invalid.  */
8269
240
          if ((section->flags & SEC_LOAD) != 0)
8270
201
      seg_off = this_hdr->sh_offset - segment->p_offset;
8271
39
          else
8272
39
      seg_off = this_hdr->sh_addr - segment->p_vaddr;
8273
240
          if (section->lma * opb - segment->p_paddr != seg_off)
8274
67
      map->p_paddr_valid = false;
8275
240
        }
8276
1.02k
      if (isec == section_count)
8277
157
        break;
8278
1.02k
    }
8279
1.11k
      }
8280
157
  }
8281
8282
400
      if (section_count == 0)
8283
243
  map->p_vaddr_offset = segment->p_vaddr / opb;
8284
157
      else if (map->p_paddr_valid)
8285
98
  {
8286
    /* Account for padding before the first section in the segment.  */
8287
98
    bfd_vma hdr_size = 0;
8288
98
    if (map->includes_filehdr)
8289
28
      hdr_size = iehdr->e_ehsize;
8290
98
    if (map->includes_phdrs)
8291
10
      hdr_size += iehdr->e_phnum * iehdr->e_phentsize;
8292
8293
98
    map->p_vaddr_offset = ((map->p_paddr + hdr_size) / opb
8294
98
         - (lowest_section ? lowest_section->lma : 0));
8295
98
  }
8296
8297
400
      map->count = section_count;
8298
400
      *pointer_to_map = map;
8299
400
      pointer_to_map = &map->next;
8300
400
    }
8301
8302
136
  elf_seg_map (obfd) = map_first;
8303
136
  return true;
8304
136
}
8305
8306
/* Copy private BFD data.  This copies or rewrites ELF program header
8307
   information.  */
8308
8309
static bool
8310
copy_private_bfd_data (bfd *ibfd, bfd *obfd)
8311
172
{
8312
172
  bfd_vma maxpagesize;
8313
8314
172
  if (elf_tdata (ibfd)->phdr == NULL)
8315
0
    return true;
8316
8317
172
  if (ibfd->xvec == obfd->xvec)
8318
172
    {
8319
      /* Check to see if any sections in the input BFD
8320
   covered by ELF program header have changed.  */
8321
172
      Elf_Internal_Phdr *segment;
8322
172
      asection * section;
8323
172
      asection * osec;
8324
172
      asection * prev;
8325
172
      unsigned int i, num_segments;
8326
172
      Elf_Internal_Shdr *this_hdr;
8327
172
      elf_backend_data *bed;
8328
8329
172
      bed = get_elf_backend_data (ibfd);
8330
8331
      /* Regenerate the segment map if p_paddr is set to 0.  */
8332
172
      if (bed->want_p_paddr_set_to_zero)
8333
5
  goto rewrite;
8334
8335
      /* Initialize the segment mark field.  */
8336
1.08k
      for (section = obfd->sections; section != NULL;
8337
913
     section = section->next)
8338
913
  section->segment_mark = false;
8339
8340
167
      num_segments = elf_elfheader (ibfd)->e_phnum;
8341
167
      for (i = 0, segment = elf_tdata (ibfd)->phdr;
8342
638
     i < num_segments;
8343
471
     i++, segment++)
8344
502
  {
8345
    /* PR binutils/3535.  The Solaris linker always sets the p_paddr
8346
       and p_memsz fields of special segments (DYNAMIC, INTERP) to 0
8347
       which severly confuses things, so always regenerate the segment
8348
       map in this case.  */
8349
502
    if (segment->p_paddr == 0
8350
181
        && segment->p_memsz == 0
8351
62
        && (segment->p_type == PT_INTERP
8352
62
      || segment->p_type == PT_DYNAMIC))
8353
0
      goto rewrite;
8354
8355
502
    for (section = ibfd->sections, prev = NULL;
8356
5.18k
         section != NULL; section = section->next)
8357
4.71k
      {
8358
        /* We mark the output section so that we know it comes
8359
     from the input BFD.  */
8360
4.71k
        osec = section->output_section;
8361
4.71k
        if (osec)
8362
4.71k
    osec->segment_mark = true;
8363
8364
        /* Check if this section is covered by the segment.  */
8365
4.71k
        this_hdr = &(elf_section_data(section)->this_hdr);
8366
4.71k
        if (ELF_SECTION_IN_SEGMENT (this_hdr, segment))
8367
1.14k
    {
8368
      /* FIXME: Check if its output section is changed or
8369
         removed.  What else do we need to check?  */
8370
1.14k
      if (osec == NULL
8371
1.14k
          || section->flags != osec->flags
8372
1.14k
          || section->lma != osec->lma
8373
1.14k
          || section->vma != osec->vma
8374
1.14k
          || section->size != osec->size
8375
1.14k
          || section->rawsize != osec->rawsize
8376
1.14k
          || section->alignment_power != osec->alignment_power)
8377
1
        goto rewrite;
8378
8379
      /* PR 31450: If this is an allocated section then make sure
8380
         that this section's vma to lma relationship is the same
8381
         as previous (allocated) section's.  */
8382
1.14k
      if (prev != NULL
8383
612
          && section->flags & SEC_ALLOC
8384
212
          && section->lma - section->vma != prev->lma - prev->vma)
8385
30
        goto rewrite;
8386
8387
1.11k
      if (section->flags & SEC_ALLOC)
8388
295
        prev = section;
8389
1.11k
    }
8390
4.71k
      }
8391
502
  }
8392
8393
      /* Check to see if any output section do not come from the
8394
   input BFD.  */
8395
653
      for (section = obfd->sections; section != NULL;
8396
517
     section = section->next)
8397
517
  {
8398
517
    if (!section->segment_mark)
8399
0
      goto rewrite;
8400
517
    else
8401
517
      section->segment_mark = false;
8402
517
  }
8403
8404
136
      return copy_elf_program_header (ibfd, obfd);
8405
136
    }
8406
8407
36
 rewrite:
8408
36
  maxpagesize = 0;
8409
36
  if (ibfd->xvec == obfd->xvec)
8410
36
    {
8411
      /* When rewriting program header, set the output maxpagesize to
8412
   the maximum alignment of input PT_LOAD segments.  */
8413
36
      Elf_Internal_Phdr *segment;
8414
36
      unsigned int i;
8415
36
      unsigned int num_segments = elf_elfheader (ibfd)->e_phnum;
8416
8417
36
      for (i = 0, segment = elf_tdata (ibfd)->phdr;
8418
326
     i < num_segments;
8419
290
     i++, segment++)
8420
290
  if (segment->p_type == PT_LOAD
8421
73
      && maxpagesize < segment->p_align)
8422
37
    {
8423
      /* PR 17512: file: f17299af.  */
8424
37
      if (segment->p_align > (bfd_vma) 1 << ((sizeof (bfd_vma) * 8) - 2))
8425
        /* xgettext:c-format */
8426
0
        _bfd_error_handler (_("%pB: warning: segment alignment of %#"
8427
0
            PRIx64 " is too large"),
8428
0
          ibfd, (uint64_t) segment->p_align);
8429
37
      else
8430
37
        maxpagesize = segment->p_align;
8431
37
    }
8432
36
    }
8433
36
  if (maxpagesize == 0)
8434
7
    maxpagesize = get_elf_backend_data (obfd)->maxpagesize;
8435
8436
36
  return rewrite_elf_program_header (ibfd, obfd, maxpagesize);
8437
172
}
8438
8439
/* Copy private section information from input to output.  This function
8440
   is called both by objcopy where LINK_INFO is NULL, and ld where
8441
   LINK_INFO is non-NULL in the usual case but NULL for a special case
8442
   when dealing with LTO IR or cloning output sections.  */
8443
8444
bool
8445
_bfd_elf_copy_private_section_data (bfd *ibfd,
8446
            asection *isec,
8447
            bfd *obfd ATTRIBUTE_UNUSED,
8448
            asection *osec,
8449
            struct bfd_link_info *link_info)
8450
2.36k
{
8451
2.36k
  if (ibfd->xvec->flavour != bfd_target_elf_flavour)
8452
0
    return true;
8453
8454
2.36k
  Elf_Internal_Shdr *ihdr = &elf_section_data (isec)->this_hdr;
8455
2.36k
  Elf_Internal_Shdr *ohdr = &elf_section_data (osec)->this_hdr;
8456
2.36k
  if (link_info == NULL)
8457
2.36k
    {
8458
2.36k
      ohdr->sh_entsize = ihdr->sh_entsize;
8459
8460
2.36k
      if (ihdr->sh_type == SHT_SYMTAB
8461
2.36k
    || ihdr->sh_type == SHT_DYNSYM
8462
2.33k
    || ihdr->sh_type == SHT_GNU_verneed
8463
2.30k
    || ihdr->sh_type == SHT_GNU_verdef)
8464
88
  ohdr->sh_info = ihdr->sh_info;
8465
2.36k
    }
8466
8467
  /* If this is a known ABI section, ELF section type and flags may
8468
     have been set up when OSEC was created.  For normal sections we
8469
     allow the user to override the type and flags other than
8470
     SHF_MASKOS and SHF_MASKPROC.  */
8471
2.36k
  if (elf_section_type (osec) == SHT_PROGBITS
8472
1.98k
      || elf_section_type (osec) == SHT_NOTE
8473
1.92k
      || elf_section_type (osec) == SHT_NOBITS)
8474
469
    elf_section_type (osec) = SHT_NULL;
8475
8476
  /* For objcopy and relocatable link, copy the ELF section type from
8477
     the input file if the BFD section flags are the same.  (If they
8478
     are different the user may be doing something like
8479
     "objcopy --set-section-flags .text=alloc,data".)  For a final
8480
     link allow some flags that the linker clears to differ.  */
8481
2.36k
  bool final_link = (link_info != NULL
8482
0
         && !bfd_link_relocatable (link_info));
8483
2.36k
  if (elf_section_type (osec) == SHT_NULL
8484
2.16k
      && (osec->flags == isec->flags
8485
0
    || (final_link
8486
0
        && ((osec->flags ^ isec->flags)
8487
0
      & ~(SEC_LINK_ONCE | SEC_LINK_DUPLICATES | SEC_RELOC)) == 0)))
8488
2.16k
    elf_section_type (osec) = elf_section_type (isec);
8489
8490
2.36k
  elf_section_flags (osec) = elf_section_flags (isec);
8491
  /* Like for type, retain flags for objcopy (yet unlike for type, don't do so
8492
     for relocatable link).  Same heuristic as there: If the BFD section flags
8493
     are different, assume --set-section-flags is in use for the section.
8494
8495
     FIXME: Is this correct for all OS/PROC specific flags?  */
8496
2.36k
  if (link_info != NULL || osec->flags != isec->flags)
8497
0
    elf_section_flags (osec) &= (SHF_MASKOS | SHF_MASKPROC);
8498
2.36k
  else
8499
2.36k
    {
8500
      /* Clear only flags which are set below or elsewhere.  */
8501
2.36k
      elf_section_flags (osec) &= ~(SHF_WRITE | SHF_ALLOC | SHF_EXECINSTR
8502
2.36k
            | SHF_MERGE | SHF_STRINGS | SHF_LINK_ORDER
8503
2.36k
            | SHF_INFO_LINK | SHF_GROUP | SHF_TLS
8504
2.36k
            | SHF_COMPRESSED);
8505
2.36k
      if (elf_section_flags (osec) & ~(SHF_MASKOS | SHF_MASKPROC))
8506
183
  _bfd_error_handler
8507
183
    (_("%pB:%pA: warning: retaining unknown section flag(s) %#" PRIx64),
8508
183
     ibfd, isec,
8509
183
     (uint64_t) (elf_section_flags (osec)
8510
183
           & ~(SHF_MASKOS | SHF_MASKPROC)));
8511
2.36k
    }
8512
8513
  /* Copy sh_info from input for mbind section.  */
8514
2.36k
  if ((elf_tdata (ibfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0
8515
449
      && elf_section_flags (isec) & SHF_GNU_MBIND)
8516
69
    elf_section_data (osec)->this_hdr.sh_info
8517
69
      = elf_section_data (isec)->this_hdr.sh_info;
8518
8519
  /* Set things up for objcopy and relocatable link.  The output
8520
     SHT_GROUP section will have its elf_next_in_group pointing back
8521
     to the input group members.  Ignore linker created group section.
8522
     See elfNN_ia64_object_p in elfxx-ia64.c.  */
8523
2.36k
  if ((link_info == NULL
8524
0
       || !link_info->resolve_section_groups)
8525
2.36k
      && (elf_sec_group (isec) == NULL
8526
40
    || (elf_sec_group (isec)->flags & SEC_LINKER_CREATED) == 0))
8527
2.36k
    {
8528
2.36k
      if (elf_section_flags (isec) & SHF_GROUP)
8529
135
  elf_section_flags (osec) |= SHF_GROUP;
8530
2.36k
      elf_next_in_group (osec) = elf_next_in_group (isec);
8531
2.36k
      elf_section_data (osec)->group = elf_section_data (isec)->group;
8532
2.36k
    }
8533
8534
  /* If not decompress, preserve SHF_COMPRESSED.  */
8535
2.36k
  if (!final_link && (ibfd->flags & BFD_DECOMPRESS) == 0)
8536
2.36k
    elf_section_flags (osec) |= (elf_section_flags (isec)
8537
2.36k
         & SHF_COMPRESSED);
8538
8539
  /* We need to handle elf_linked_to_section for SHF_LINK_ORDER. We
8540
     don't use the output section of the linked-to section since it
8541
     may be NULL at this point.  */
8542
2.36k
  if ((ihdr->sh_flags & SHF_LINK_ORDER) != 0)
8543
66
    {
8544
66
      ohdr->sh_flags |= SHF_LINK_ORDER;
8545
66
      elf_linked_to_section (osec) = elf_linked_to_section (isec);
8546
66
    }
8547
8548
2.36k
  osec->use_rela_p = isec->use_rela_p;
8549
8550
2.36k
  return true;
8551
2.36k
}
8552
8553
/* Look at all the SHT_GROUP sections in IBFD, making any adjustments
8554
   necessary if we are removing either the SHT_GROUP section or any of
8555
   the group member sections.  DISCARDED is the value that a section's
8556
   output_section has if the section will be discarded, NULL when this
8557
   function is called from objcopy, bfd_abs_section_ptr when called
8558
   from the linker.  */
8559
8560
bool
8561
_bfd_elf_fixup_group_sections (bfd *ibfd, asection *discarded)
8562
392
{
8563
392
  asection *isec;
8564
8565
2.70k
  for (isec = ibfd->sections; isec != NULL; isec = isec->next)
8566
2.31k
    if (elf_section_type (isec) == SHT_GROUP)
8567
19
      {
8568
19
  asection *first = elf_next_in_group (isec);
8569
19
  asection *s = first;
8570
19
  bfd_size_type removed = 0;
8571
8572
40
  while (s != NULL)
8573
40
    {
8574
      /* If this member section is being output but the
8575
         SHT_GROUP section is not, then clear the group info
8576
         set up by _bfd_elf_copy_private_section_data.  */
8577
40
      if (s->output_section != discarded
8578
40
    && isec->output_section == discarded)
8579
0
        {
8580
0
    elf_section_flags (s->output_section) &= ~SHF_GROUP;
8581
0
    elf_group_name (s->output_section) = NULL;
8582
0
        }
8583
40
      else
8584
40
        {
8585
40
    struct bfd_elf_section_data *elf_sec = elf_section_data (s);
8586
40
    if (s->output_section == discarded
8587
0
        && isec->output_section != discarded)
8588
0
      {
8589
        /* Conversely, if the member section is not being
8590
           output but the SHT_GROUP section is, then adjust
8591
           its size.  */
8592
0
        removed += 4;
8593
0
        if (elf_sec->rel.hdr != NULL
8594
0
      && (elf_sec->rel.hdr->sh_flags & SHF_GROUP) != 0)
8595
0
          removed += 4;
8596
0
        if (elf_sec->rela.hdr != NULL
8597
0
      && (elf_sec->rela.hdr->sh_flags & SHF_GROUP) != 0)
8598
0
          removed += 4;
8599
0
      }
8600
40
    else
8601
40
      {
8602
        /* Also adjust for zero-sized relocation member
8603
           section.  */
8604
40
        if (elf_sec->rel.hdr != NULL
8605
0
      && elf_sec->rel.hdr->sh_size == 0)
8606
0
          removed += 4;
8607
40
        if (elf_sec->rela.hdr != NULL
8608
27
      && elf_sec->rela.hdr->sh_size == 0)
8609
0
          removed += 4;
8610
40
      }
8611
40
        }
8612
40
      s = elf_next_in_group (s);
8613
40
      if (s == first)
8614
19
        break;
8615
40
    }
8616
19
  if (removed != 0)
8617
0
    {
8618
0
      if (discarded != NULL)
8619
0
        {
8620
    /* If we've been called for ld -r, then we need to
8621
       adjust the input section size.  */
8622
0
    if (isec->rawsize == 0)
8623
0
      isec->rawsize = isec->size;
8624
0
    isec->size = isec->rawsize - removed;
8625
0
    if (isec->size <= 4)
8626
0
      {
8627
0
        isec->size = 0;
8628
0
        isec->flags |= SEC_EXCLUDE;
8629
0
      }
8630
0
        }
8631
0
      else if (isec->output_section != NULL)
8632
0
        {
8633
    /* Adjust the output section size when called from
8634
       objcopy. */
8635
0
    isec->output_section->size -= removed;
8636
0
    if (isec->output_section->size <= 4)
8637
0
      {
8638
0
        isec->output_section->size = 0;
8639
0
        isec->output_section->flags |= SEC_EXCLUDE;
8640
0
      }
8641
0
        }
8642
0
    }
8643
19
      }
8644
8645
392
  return true;
8646
392
}
8647
8648
/* Copy private header information.  */
8649
8650
bool
8651
_bfd_elf_copy_private_header_data (bfd *ibfd, bfd *obfd)
8652
392
{
8653
392
  if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
8654
0
    return true;
8655
8656
  /* Copy over private BFD data if it has not already been copied.
8657
     This must be done here, rather than in the copy_private_bfd_data
8658
     entry point, because the latter is called after the section
8659
     contents have been set, which means that the program headers have
8660
     already been worked out.  */
8661
392
  if (elf_seg_map (obfd) == NULL && elf_tdata (ibfd)->phdr != NULL)
8662
172
    {
8663
172
      if (! copy_private_bfd_data (ibfd, obfd))
8664
0
  return false;
8665
172
    }
8666
8667
392
  return _bfd_elf_fixup_group_sections (ibfd, NULL);
8668
392
}
8669
8670
/* Copy private symbol information.  If this symbol is in a section
8671
   which we did not map into a BFD section, try to map the section
8672
   index correctly.  We use special macro definitions for the mapped
8673
   section indices; these definitions are interpreted by the
8674
   swap_out_syms function.  */
8675
8676
10
#define MAP_ONESYMTAB (SHN_HIOS + 1)
8677
0
#define MAP_DYNSYMTAB (SHN_HIOS + 2)
8678
46
#define MAP_STRTAB    (SHN_HIOS + 3)
8679
7
#define MAP_SHSTRTAB  (SHN_HIOS + 4)
8680
1
#define MAP_SYM_SHNDX (SHN_HIOS + 5)
8681
8682
bool
8683
_bfd_elf_copy_private_symbol_data (bfd *ibfd,
8684
           asymbol **isymarg,
8685
           bfd *obfd ATTRIBUTE_UNUSED,
8686
           asymbol **osymarg)
8687
8.15k
{
8688
8.15k
  if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
8689
0
    return true;
8690
8691
8.15k
  elf_symbol_type *isym = elf_symbol_from (*isymarg);
8692
8.15k
  elf_symbol_type *osym = elf_symbol_from (*osymarg);
8693
8.15k
  if (isym != NULL
8694
8.15k
      && isym->internal_elf_sym.st_shndx != 0
8695
1.83k
      && osym != NULL
8696
1.83k
      && bfd_is_abs_section (isym->symbol.section))
8697
1.03k
    {
8698
1.03k
      unsigned int shndx;
8699
8700
1.03k
      shndx = isym->internal_elf_sym.st_shndx;
8701
1.03k
      if (shndx == elf_onesymtab (ibfd))
8702
6
  shndx = MAP_ONESYMTAB;
8703
1.02k
      else if (shndx == elf_dynsymtab (ibfd))
8704
0
  shndx = MAP_DYNSYMTAB;
8705
1.02k
      else if (shndx == elf_elfsections (ibfd)[elf_onesymtab (ibfd)]->sh_link)
8706
37
  shndx = MAP_STRTAB;
8707
990
      else if (shndx == elf_elfheader (ibfd)->e_shstrndx)
8708
4
  shndx = MAP_SHSTRTAB;
8709
986
      else if (find_section_in_list (shndx, elf_symtab_shndx_list (ibfd)))
8710
1
  shndx = MAP_SYM_SHNDX;
8711
1.03k
      osym->internal_elf_sym.st_shndx = shndx;
8712
1.03k
    }
8713
8714
8.15k
  return true;
8715
8.15k
}
8716
8717
/* Swap out the symbols.  */
8718
8719
static bool
8720
swap_out_syms (bfd *abfd,
8721
         struct elf_strtab_hash **sttp,
8722
         int relocatable_p,
8723
         struct bfd_link_info *info)
8724
97
{
8725
97
  elf_backend_data *bed;
8726
97
  unsigned int symcount;
8727
97
  asymbol **syms;
8728
97
  struct elf_strtab_hash *stt;
8729
97
  Elf_Internal_Shdr *symtab_hdr;
8730
97
  Elf_Internal_Shdr *symtab_shndx_hdr;
8731
97
  Elf_Internal_Shdr *symstrtab_hdr;
8732
97
  struct elf_sym_strtab *symstrtab;
8733
97
  bfd_byte *outbound_syms;
8734
97
  bfd_byte *outbound_shndx;
8735
97
  unsigned long outbound_syms_index;
8736
97
  unsigned int idx;
8737
97
  unsigned int num_locals;
8738
97
  size_t amt;
8739
97
  bool name_local_sections;
8740
8741
97
  if (!elf_map_symbols (abfd, &num_locals))
8742
0
    return false;
8743
8744
  /* Dump out the symtabs.  */
8745
97
  stt = _bfd_elf_strtab_init ();
8746
97
  if (stt == NULL)
8747
0
    return false;
8748
8749
97
  bed = get_elf_backend_data (abfd);
8750
97
  symcount = bfd_get_symcount (abfd);
8751
97
  symtab_hdr = &elf_symtab_hdr (abfd);
8752
97
  symtab_hdr->sh_type = SHT_SYMTAB;
8753
97
  symtab_hdr->sh_entsize = bed->s->sizeof_sym;
8754
97
  symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1);
8755
97
  symtab_hdr->sh_info = num_locals + 1;
8756
97
  symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
8757
8758
97
  symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
8759
97
  symstrtab_hdr->sh_type = SHT_STRTAB;
8760
8761
  /* Allocate buffer to swap out the .strtab section.  */
8762
97
  if (_bfd_mul_overflow (symcount + 1, sizeof (*symstrtab), &amt)
8763
97
      || (symstrtab = bfd_malloc (amt)) == NULL)
8764
0
    {
8765
0
      bfd_set_error (bfd_error_no_memory);
8766
0
      _bfd_elf_strtab_free (stt);
8767
0
      return false;
8768
0
    }
8769
8770
97
  if (_bfd_mul_overflow (symcount + 1, bed->s->sizeof_sym, &amt)
8771
97
      || (outbound_syms = bfd_malloc (amt)) == NULL)
8772
0
    {
8773
0
    error_no_mem:
8774
0
      bfd_set_error (bfd_error_no_memory);
8775
0
    error_return:
8776
0
      free (symstrtab);
8777
0
      _bfd_elf_strtab_free (stt);
8778
0
      return false;
8779
0
    }
8780
97
  symtab_hdr->contents = outbound_syms;
8781
97
  outbound_syms_index = 0;
8782
8783
97
  outbound_shndx = NULL;
8784
8785
97
  if (elf_symtab_shndx_list (abfd))
8786
0
    {
8787
0
      symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
8788
0
      if (symtab_shndx_hdr->sh_name != 0)
8789
0
  {
8790
0
    if (_bfd_mul_overflow (symcount + 1,
8791
0
         sizeof (Elf_External_Sym_Shndx), &amt))
8792
0
      goto error_no_mem;
8793
0
    outbound_shndx = bfd_zalloc (abfd, amt);
8794
0
    if (outbound_shndx == NULL)
8795
0
      goto error_return;
8796
8797
0
    symtab_shndx_hdr->contents = outbound_shndx;
8798
0
    symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
8799
0
    symtab_shndx_hdr->sh_size = amt;
8800
0
    symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
8801
0
    symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
8802
0
  }
8803
      /* FIXME: What about any other headers in the list ?  */
8804
0
    }
8805
8806
  /* Now generate the data (for "contents").  */
8807
97
  {
8808
    /* Fill in zeroth symbol and swap it out.  */
8809
97
    Elf_Internal_Sym sym;
8810
97
    sym.st_name = 0;
8811
97
    sym.st_value = 0;
8812
97
    sym.st_size = 0;
8813
97
    sym.st_info = 0;
8814
97
    sym.st_other = 0;
8815
97
    sym.st_shndx = SHN_UNDEF;
8816
97
    sym.st_target_internal = 0;
8817
97
    symstrtab[outbound_syms_index].sym = sym;
8818
97
    symstrtab[outbound_syms_index].dest_index = outbound_syms_index;
8819
97
    outbound_syms_index++;
8820
97
  }
8821
8822
97
  name_local_sections
8823
97
    = (bed->elf_backend_name_local_section_symbols
8824
9
       && bed->elf_backend_name_local_section_symbols (abfd));
8825
8826
97
  syms = bfd_get_outsymbols (abfd);
8827
1.81k
  for (idx = 0; idx < symcount; idx++)
8828
1.71k
    {
8829
1.71k
      Elf_Internal_Sym sym;
8830
8831
1.71k
      flagword flags = syms[idx]->flags;
8832
1.71k
      if (!name_local_sections
8833
1.55k
    && (flags & (BSF_SECTION_SYM | BSF_GLOBAL)) == BSF_SECTION_SYM)
8834
192
  {
8835
    /* Local section symbols have no name.  */
8836
192
    sym.st_name = 0;
8837
192
  }
8838
1.52k
      else
8839
1.52k
  {
8840
    /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
8841
       to get the final offset for st_name.  */
8842
1.52k
    size_t stridx = _bfd_elf_strtab_add (stt, syms[idx]->name, false);
8843
1.52k
    if (stridx == (size_t) -1)
8844
0
      goto error_return;
8845
1.52k
    sym.st_name = stridx;
8846
1.52k
  }
8847
8848
1.71k
      bfd_vma value = syms[idx]->value;
8849
1.71k
      elf_symbol_type *type_ptr = elf_symbol_from (syms[idx]);
8850
1.71k
      asection *sec = syms[idx]->section;
8851
8852
1.71k
      if ((flags & BSF_SECTION_SYM) == 0 && bfd_is_com_section (sec))
8853
16
  {
8854
    /* ELF common symbols put the alignment into the `value' field,
8855
       and the size into the `size' field.  This is backwards from
8856
       how BFD handles it, so reverse it here.  */
8857
16
    sym.st_size = value;
8858
16
    if (type_ptr == NULL
8859
16
        || type_ptr->internal_elf_sym.st_value == 0)
8860
10
      sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value));
8861
6
    else
8862
6
      sym.st_value = type_ptr->internal_elf_sym.st_value;
8863
16
    sym.st_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
8864
16
  }
8865
1.69k
      else
8866
1.69k
  {
8867
1.69k
    unsigned int shndx;
8868
8869
1.69k
    if (sec->output_section)
8870
1.61k
      {
8871
1.61k
        value += sec->output_offset;
8872
1.61k
        sec = sec->output_section;
8873
1.61k
      }
8874
8875
    /* Don't add in the section vma for relocatable output.  */
8876
1.69k
    if (! relocatable_p)
8877
792
      value += sec->vma;
8878
1.69k
    sym.st_value = value;
8879
1.69k
    sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0;
8880
8881
1.69k
    if (bfd_is_abs_section (sec)
8882
544
        && type_ptr != NULL
8883
544
        && type_ptr->internal_elf_sym.st_shndx != 0)
8884
544
      {
8885
        /* This symbol is in a real ELF section which we did
8886
     not create as a BFD section.  Undo the mapping done
8887
     by copy_private_symbol_data.  */
8888
544
        shndx = type_ptr->internal_elf_sym.st_shndx;
8889
544
        switch (shndx)
8890
544
    {
8891
4
    case MAP_ONESYMTAB:
8892
4
      shndx = elf_onesymtab (abfd);
8893
4
      break;
8894
0
    case MAP_DYNSYMTAB:
8895
0
      shndx = elf_dynsymtab (abfd);
8896
0
      break;
8897
9
    case MAP_STRTAB:
8898
9
      shndx = elf_strtab_sec (abfd);
8899
9
      break;
8900
3
    case MAP_SHSTRTAB:
8901
3
      shndx = elf_shstrtab_sec (abfd);
8902
3
      break;
8903
0
    case MAP_SYM_SHNDX:
8904
0
      if (elf_symtab_shndx_list (abfd))
8905
0
        shndx = elf_symtab_shndx_list (abfd)->ndx;
8906
0
      break;
8907
0
    case SHN_COMMON:
8908
11
    case SHN_ABS:
8909
11
      shndx = SHN_ABS;
8910
11
      break;
8911
517
    default:
8912
517
      if (shndx >= SHN_LOPROC && shndx <= SHN_HIOS)
8913
10
        {
8914
10
          if (bed->symbol_section_index)
8915
0
      shndx = bed->symbol_section_index (abfd, type_ptr);
8916
          /* Otherwise just leave the index alone.  */
8917
10
        }
8918
507
      else
8919
507
        {
8920
507
          if (shndx > SHN_HIOS && shndx < SHN_HIRESERVE)
8921
9
      _bfd_error_handler (_("%pB: \
8922
9
Unable to handle section index %x in ELF symbol.  Using ABS instead."),
8923
9
            abfd, shndx);
8924
507
          shndx = SHN_ABS;
8925
507
        }
8926
517
      break;
8927
544
    }
8928
544
      }
8929
1.15k
    else
8930
1.15k
      {
8931
1.15k
        shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
8932
8933
1.15k
        if (shndx == SHN_BAD)
8934
0
    {
8935
0
      asection *sec2;
8936
8937
      /* Writing this would be a hell of a lot easier if
8938
         we had some decent documentation on bfd, and
8939
         knew what to expect of the library, and what to
8940
         demand of applications.  For example, it
8941
         appears that `objcopy' might not set the
8942
         section of a symbol to be a section that is
8943
         actually in the output file.  */
8944
0
      sec2 = bfd_get_section_by_name (abfd, sec->name);
8945
0
      if (sec2 != NULL)
8946
0
        shndx = _bfd_elf_section_from_bfd_section (abfd, sec2);
8947
0
      if (shndx == SHN_BAD)
8948
0
        {
8949
          /* xgettext:c-format */
8950
0
          _bfd_error_handler
8951
0
      (_("unable to find equivalent output section"
8952
0
         " for symbol '%s' from section '%s'"),
8953
0
       syms[idx]->name ? syms[idx]->name : "<Local sym>",
8954
0
       sec->name);
8955
0
          bfd_set_error (bfd_error_invalid_operation);
8956
0
          goto error_return;
8957
0
        }
8958
0
    }
8959
1.15k
      }
8960
8961
1.69k
    sym.st_shndx = shndx;
8962
1.69k
  }
8963
8964
1.71k
      int type;
8965
1.71k
      if ((flags & BSF_THREAD_LOCAL) != 0)
8966
63
  type = STT_TLS;
8967
1.65k
      else if ((flags & BSF_GNU_INDIRECT_FUNCTION) != 0)
8968
21
  type = STT_GNU_IFUNC;
8969
1.62k
      else if ((flags & BSF_FUNCTION) != 0)
8970
248
  type = STT_FUNC;
8971
1.38k
      else if ((flags & BSF_OBJECT) != 0)
8972
182
  type = STT_OBJECT;
8973
1.19k
      else if ((flags & BSF_RELC) != 0)
8974
8
  type = STT_RELC;
8975
1.19k
      else if ((flags & BSF_SRELC) != 0)
8976
34
  type = STT_SRELC;
8977
1.15k
      else
8978
1.15k
  type = STT_NOTYPE;
8979
8980
1.71k
      if (syms[idx]->section->flags & SEC_THREAD_LOCAL)
8981
5
  type = STT_TLS;
8982
8983
      /* Processor-specific types.  */
8984
1.71k
      if (type_ptr != NULL
8985
1.71k
    && bed->elf_backend_get_symbol_type)
8986
71
  type = ((*bed->elf_backend_get_symbol_type)
8987
71
    (&type_ptr->internal_elf_sym, type));
8988
8989
1.71k
      if (flags & BSF_SECTION_SYM)
8990
213
  {
8991
213
    if (flags & BSF_GLOBAL)
8992
17
      sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
8993
196
    else
8994
196
      sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
8995
213
  }
8996
1.50k
      else if (bfd_is_com_section (syms[idx]->section))
8997
16
  {
8998
16
    if (type != STT_TLS)
8999
13
      {
9000
13
        if ((abfd->flags & BFD_CONVERT_ELF_COMMON))
9001
0
    type = ((abfd->flags & BFD_USE_ELF_STT_COMMON)
9002
0
      ? STT_COMMON : STT_OBJECT);
9003
13
        else
9004
13
    type = ((flags & BSF_ELF_COMMON) != 0
9005
13
      ? STT_COMMON : STT_OBJECT);
9006
13
      }
9007
16
    sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
9008
16
  }
9009
1.48k
      else if (bfd_is_und_section (syms[idx]->section))
9010
679
  sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK)
9011
1.48k
            ? STB_WEAK
9012
1.48k
            : STB_GLOBAL),
9013
1.48k
           type);
9014
805
      else if (flags & BSF_FILE)
9015
16
  sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
9016
789
      else
9017
789
  {
9018
789
    int bind = STB_LOCAL;
9019
9020
789
    if (flags & BSF_LOCAL)
9021
383
      bind = STB_LOCAL;
9022
406
    else if (flags & BSF_GNU_UNIQUE)
9023
70
      bind = STB_GNU_UNIQUE;
9024
336
    else if (flags & BSF_WEAK)
9025
38
      bind = STB_WEAK;
9026
298
    else if (flags & BSF_GLOBAL)
9027
180
      bind = STB_GLOBAL;
9028
9029
789
    sym.st_info = ELF_ST_INFO (bind, type);
9030
789
  }
9031
9032
1.71k
      if (type_ptr != NULL)
9033
1.71k
  {
9034
1.71k
    sym.st_other = type_ptr->internal_elf_sym.st_other;
9035
1.71k
    sym.st_target_internal
9036
1.71k
      = type_ptr->internal_elf_sym.st_target_internal;
9037
1.71k
  }
9038
0
      else
9039
0
  {
9040
0
    sym.st_other = 0;
9041
0
    sym.st_target_internal = 0;
9042
0
  }
9043
9044
1.71k
      symstrtab[outbound_syms_index].sym = sym;
9045
1.71k
      symstrtab[outbound_syms_index].dest_index = outbound_syms_index;
9046
1.71k
      outbound_syms_index++;
9047
1.71k
    }
9048
9049
  /* Finalize the .strtab section.  */
9050
97
  _bfd_elf_strtab_finalize (stt);
9051
9052
  /* Swap out the .strtab section.  */
9053
1.90k
  for (idx = 0; idx < outbound_syms_index; idx++)
9054
1.81k
    {
9055
1.81k
      struct elf_sym_strtab *elfsym = &symstrtab[idx];
9056
1.81k
      if (elfsym->sym.st_name != 0)
9057
387
  elfsym->sym.st_name = _bfd_elf_strtab_offset (stt,
9058
387
                  elfsym->sym.st_name);
9059
1.81k
      if (info && info->callbacks->ctf_new_symbol)
9060
0
  info->callbacks->ctf_new_symbol (elfsym->dest_index,
9061
0
           &elfsym->sym);
9062
9063
      /* Inform the linker of the addition of this symbol.  */
9064
9065
1.81k
      bed->s->swap_symbol_out (abfd, &elfsym->sym,
9066
1.81k
             (outbound_syms
9067
1.81k
        + (elfsym->dest_index
9068
1.81k
           * bed->s->sizeof_sym)),
9069
1.81k
             NPTR_ADD (outbound_shndx,
9070
1.81k
           (elfsym->dest_index
9071
1.81k
            * sizeof (Elf_External_Sym_Shndx))));
9072
1.81k
    }
9073
97
  free (symstrtab);
9074
9075
97
  *sttp = stt;
9076
97
  symstrtab_hdr->sh_size = _bfd_elf_strtab_size (stt);
9077
97
  symstrtab_hdr->sh_type = SHT_STRTAB;
9078
97
  symstrtab_hdr->sh_addralign = 1;
9079
9080
97
  return true;
9081
97
}
9082
9083
/* Return the number of bytes required to hold the symtab vector.
9084
9085
   Note that we base it on the count plus 1, since we will null terminate
9086
   the vector allocated based on this size.  However, the ELF symbol table
9087
   always has a dummy entry as symbol #0, so it ends up even.  */
9088
9089
long
9090
_bfd_elf_get_symtab_upper_bound (bfd *abfd)
9091
9.13k
{
9092
9.13k
  bfd_size_type symcount;
9093
9.13k
  long symtab_size;
9094
9.13k
  Elf_Internal_Shdr *hdr = &elf_symtab_hdr (abfd);
9095
9096
9.13k
  symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
9097
9.13k
  if (symcount > LONG_MAX / sizeof (asymbol *))
9098
0
    {
9099
0
      bfd_set_error (bfd_error_file_too_big);
9100
0
      return -1;
9101
0
    }
9102
9.13k
  symtab_size = symcount * (sizeof (asymbol *));
9103
9.13k
  if (symcount == 0)
9104
698
    symtab_size = sizeof (asymbol *);
9105
8.43k
  else if (!bfd_write_p (abfd))
9106
8.43k
    {
9107
8.43k
      ufile_ptr filesize = bfd_get_file_size (abfd);
9108
9109
8.43k
      if (filesize != 0 && (unsigned long) symtab_size > filesize)
9110
211
  {
9111
211
    bfd_set_error (bfd_error_file_truncated);
9112
211
    return -1;
9113
211
  }
9114
8.43k
    }
9115
9116
8.92k
  return symtab_size;
9117
9.13k
}
9118
9119
long
9120
_bfd_elf_get_dynamic_symtab_upper_bound (bfd *abfd)
9121
17.4k
{
9122
17.4k
  bfd_size_type symcount;
9123
17.4k
  long symtab_size;
9124
17.4k
  Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr;
9125
9126
17.4k
  if (elf_dynsymtab (abfd) == 0)
9127
17.0k
    {
9128
      /* Check if there is dynamic symbol table.  */
9129
17.0k
      symcount = elf_tdata (abfd)->dt_symtab_count;
9130
17.0k
      if (symcount)
9131
8
  goto compute_symtab_size;
9132
9133
17.0k
      bfd_set_error (bfd_error_invalid_operation);
9134
17.0k
      return -1;
9135
17.0k
    }
9136
9137
384
  symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
9138
384
  if (symcount > LONG_MAX / sizeof (asymbol *))
9139
0
    {
9140
0
      bfd_set_error (bfd_error_file_too_big);
9141
0
      return -1;
9142
0
    }
9143
9144
392
 compute_symtab_size:
9145
392
  symtab_size = symcount * (sizeof (asymbol *));
9146
392
  if (symcount == 0)
9147
0
    symtab_size = sizeof (asymbol *);
9148
392
  else if (!bfd_write_p (abfd))
9149
392
    {
9150
392
      ufile_ptr filesize = bfd_get_file_size (abfd);
9151
9152
392
      if (filesize != 0 && (unsigned long) symtab_size > filesize)
9153
22
  {
9154
22
    bfd_set_error (bfd_error_file_truncated);
9155
22
    return -1;
9156
22
  }
9157
392
    }
9158
9159
370
  return symtab_size;
9160
392
}
9161
9162
long
9163
_bfd_elf_get_reloc_upper_bound (bfd *abfd, sec_ptr asect)
9164
21.2k
{
9165
21.2k
  if (asect->reloc_count != 0 && !bfd_write_p (abfd))
9166
18.6k
    {
9167
      /* Sanity check reloc section size.  */
9168
18.6k
      ufile_ptr filesize = bfd_get_file_size (abfd);
9169
9170
18.6k
      if (filesize != 0)
9171
18.6k
  {
9172
18.6k
    struct bfd_elf_section_data *d = elf_section_data (asect);
9173
18.6k
    bfd_size_type rel_size = d->rel.hdr ? d->rel.hdr->sh_size : 0;
9174
18.6k
    bfd_size_type rela_size = d->rela.hdr ? d->rela.hdr->sh_size : 0;
9175
9176
18.6k
    if (rel_size + rela_size > filesize
9177
17.7k
        || rel_size + rela_size < rel_size)
9178
850
      {
9179
850
        bfd_set_error (bfd_error_file_truncated);
9180
850
        return -1;
9181
850
      }
9182
18.6k
  }
9183
18.6k
    }
9184
9185
#if SIZEOF_LONG == SIZEOF_INT
9186
  if (asect->reloc_count >= LONG_MAX / sizeof (arelent *))
9187
    {
9188
      bfd_set_error (bfd_error_file_too_big);
9189
      return -1;
9190
    }
9191
#endif
9192
20.3k
  return (asect->reloc_count + 1L) * sizeof (arelent *);
9193
21.2k
}
9194
9195
/* Canonicalize the relocs.  */
9196
9197
long
9198
_bfd_elf_canonicalize_reloc (bfd *abfd,
9199
           sec_ptr section,
9200
           arelent **relptr,
9201
           asymbol **symbols)
9202
20.2k
{
9203
20.2k
  arelent *tblptr;
9204
20.2k
  unsigned int i;
9205
20.2k
  elf_backend_data *bed = get_elf_backend_data (abfd);
9206
9207
20.2k
  if (! bed->s->slurp_reloc_table (abfd, section, symbols, false))
9208
5.37k
    return -1;
9209
9210
14.9k
  tblptr = section->relocation;
9211
163k
  for (i = 0; i < section->reloc_count; i++)
9212
148k
    *relptr++ = tblptr++;
9213
9214
14.9k
  *relptr = NULL;
9215
9216
14.9k
  return section->reloc_count;
9217
20.2k
}
9218
9219
long
9220
_bfd_elf_canonicalize_symtab (bfd *abfd, asymbol **allocation)
9221
8.92k
{
9222
8.92k
  elf_backend_data *bed = get_elf_backend_data (abfd);
9223
8.92k
  long symcount = bed->s->slurp_symbol_table (abfd, allocation, false);
9224
9225
8.92k
  if (symcount >= 0)
9226
7.97k
    abfd->symcount = symcount;
9227
8.92k
  return symcount;
9228
8.92k
}
9229
9230
long
9231
_bfd_elf_canonicalize_dynamic_symtab (bfd *abfd,
9232
              asymbol **allocation)
9233
241
{
9234
241
  elf_backend_data *bed = get_elf_backend_data (abfd);
9235
241
  long symcount = bed->s->slurp_symbol_table (abfd, allocation, true);
9236
9237
241
  if (symcount >= 0)
9238
133
    abfd->dynsymcount = symcount;
9239
241
  return symcount;
9240
241
}
9241
9242
/* Return the size required for the dynamic reloc entries.  Any loadable
9243
   section that was actually installed in the BFD, and has type SHT_REL
9244
   or SHT_RELA, and uses the dynamic symbol table, is considered to be a
9245
   dynamic reloc section.  */
9246
9247
long
9248
_bfd_elf_get_dynamic_reloc_upper_bound (bfd *abfd)
9249
17.0k
{
9250
17.0k
  bfd_size_type count, ext_rel_size;
9251
17.0k
  asection *s;
9252
9253
17.0k
  if (elf_dynsymtab (abfd) == 0)
9254
16.8k
    {
9255
16.8k
      bfd_set_error (bfd_error_invalid_operation);
9256
16.8k
      return -1;
9257
16.8k
    }
9258
9259
171
  count = 1;
9260
171
  ext_rel_size = 0;
9261
3.67k
  for (s = abfd->sections; s != NULL; s = s->next)
9262
3.50k
    if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
9263
435
  && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
9264
419
      || elf_section_data (s)->this_hdr.sh_type == SHT_RELA)
9265
174
  && (elf_section_data (s)->this_hdr.sh_flags & SHF_COMPRESSED) == 0)
9266
174
      {
9267
174
  ext_rel_size += elf_section_data (s)->this_hdr.sh_size;
9268
174
  if (ext_rel_size < elf_section_data (s)->this_hdr.sh_size)
9269
0
    {
9270
0
      bfd_set_error (bfd_error_file_truncated);
9271
0
      return -1;
9272
0
    }
9273
174
  count += NUM_SHDR_ENTRIES (&elf_section_data (s)->this_hdr);
9274
174
  if (count > LONG_MAX / sizeof (arelent *))
9275
0
    {
9276
0
      bfd_set_error (bfd_error_file_too_big);
9277
0
      return -1;
9278
0
    }
9279
174
      }
9280
171
  if (count > 1 && !bfd_write_p (abfd))
9281
112
    {
9282
      /* Sanity check reloc section sizes.  */
9283
112
      ufile_ptr filesize = bfd_get_file_size (abfd);
9284
112
      if (filesize != 0 && ext_rel_size > filesize)
9285
1
  {
9286
1
    bfd_set_error (bfd_error_file_truncated);
9287
1
    return -1;
9288
1
  }
9289
112
    }
9290
170
  return count * sizeof (arelent *);
9291
171
}
9292
9293
/* Canonicalize the dynamic relocation entries.  Note that we return the
9294
   dynamic relocations as a single block, although they are actually
9295
   associated with particular sections; the interface, which was
9296
   designed for SunOS style shared libraries, expects that there is only
9297
   one set of dynamic relocs.  Any loadable section that was actually
9298
   installed in the BFD, and has type SHT_REL or SHT_RELA, and uses the
9299
   dynamic symbol table, is considered to be a dynamic reloc section.  */
9300
9301
long
9302
_bfd_elf_canonicalize_dynamic_reloc (bfd *abfd,
9303
             arelent **storage,
9304
             asymbol **syms)
9305
153
{
9306
153
  bool (*slurp_relocs) (bfd *, asection *, asymbol **, bool);
9307
153
  asection *s;
9308
153
  long ret;
9309
9310
153
  if (elf_dynsymtab (abfd) == 0)
9311
0
    {
9312
0
      bfd_set_error (bfd_error_invalid_operation);
9313
0
      return -1;
9314
0
    }
9315
9316
153
  slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
9317
153
  ret = 0;
9318
2.91k
  for (s = abfd->sections; s != NULL; s = s->next)
9319
2.77k
    {
9320
2.77k
      if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
9321
367
    && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
9322
354
        || elf_section_data (s)->this_hdr.sh_type == SHT_RELA)
9323
146
    && (elf_section_data (s)->this_hdr.sh_flags & SHF_COMPRESSED) == 0)
9324
146
  {
9325
146
    arelent *p;
9326
146
    long count, i;
9327
9328
146
    if (! (*slurp_relocs) (abfd, s, syms, true))
9329
15
      return -1;
9330
131
    count = NUM_SHDR_ENTRIES (&elf_section_data (s)->this_hdr);
9331
131
    p = s->relocation;
9332
1.14k
    for (i = 0; i < count; i++)
9333
1.01k
      *storage++ = p++;
9334
131
    ret += count;
9335
131
  }
9336
2.77k
    }
9337
9338
138
  *storage = NULL;
9339
9340
138
  return ret;
9341
153
}
9342

9343
/* Read in the version information.  */
9344
9345
bool
9346
_bfd_elf_slurp_version_tables (bfd *abfd, bool default_imported_symver)
9347
276
{
9348
276
  bfd_byte *contents = NULL;
9349
276
  unsigned int freeidx = 0;
9350
276
  size_t amt;
9351
276
  void *contents_addr = NULL;
9352
276
  size_t contents_size = 0;
9353
9354
276
  if (elf_dynverref (abfd) != 0 || elf_tdata (abfd)->dt_verneed != NULL)
9355
247
    {
9356
247
      Elf_Internal_Shdr *hdr;
9357
247
      Elf_External_Verneed *everneed;
9358
247
      Elf_Internal_Verneed *iverneed;
9359
247
      unsigned int i;
9360
247
      bfd_byte *contents_end;
9361
247
      size_t verneed_count;
9362
247
      size_t verneed_size;
9363
9364
247
      if (elf_tdata (abfd)->dt_verneed != NULL)
9365
2
  {
9366
2
    hdr = NULL;
9367
2
    contents = elf_tdata (abfd)->dt_verneed;
9368
2
    verneed_count = elf_tdata (abfd)->dt_verneed_count;
9369
2
    verneed_size = verneed_count * sizeof (Elf_External_Verneed);
9370
2
  }
9371
245
      else
9372
245
  {
9373
245
    hdr = &elf_tdata (abfd)->dynverref_hdr;
9374
9375
245
    if (hdr->sh_info > hdr->sh_size / sizeof (Elf_External_Verneed))
9376
6
      {
9377
110
      error_return_bad_verref:
9378
110
        _bfd_error_handler
9379
110
    (_("%pB: .gnu.version_r invalid entry"), abfd);
9380
110
        bfd_set_error (bfd_error_bad_value);
9381
133
      error_return_verref:
9382
133
        elf_tdata (abfd)->verref = NULL;
9383
133
        elf_tdata (abfd)->cverrefs = 0;
9384
133
        goto error_return;
9385
110
      }
9386
9387
239
    if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0)
9388
7
      goto error_return_verref;
9389
232
    contents_size = hdr->sh_size;
9390
232
    contents = _bfd_mmap_temporary (abfd, contents_size,
9391
232
            &contents_addr, &contents_size);
9392
232
    if (contents == NULL)
9393
16
      goto error_return_verref;
9394
9395
216
    verneed_size = hdr->sh_size;
9396
216
    verneed_count = hdr->sh_info;
9397
216
  }
9398
9399
218
      if (_bfd_mul_overflow (verneed_count,
9400
218
           sizeof (Elf_Internal_Verneed), &amt))
9401
0
  {
9402
0
    bfd_set_error (bfd_error_file_too_big);
9403
0
    goto error_return_verref;
9404
0
  }
9405
218
      if (amt == 0)
9406
0
  goto error_return_verref;
9407
218
      elf_tdata (abfd)->verref = bfd_zalloc (abfd, amt);
9408
218
      if (elf_tdata (abfd)->verref == NULL)
9409
0
  goto error_return_verref;
9410
9411
218
      BFD_ASSERT (sizeof (Elf_External_Verneed)
9412
218
      == sizeof (Elf_External_Vernaux));
9413
218
      contents_end = (contents + verneed_size
9414
218
          - sizeof (Elf_External_Verneed));
9415
218
      everneed = (Elf_External_Verneed *) contents;
9416
218
      iverneed = elf_tdata (abfd)->verref;
9417
242
      for (i = 0; i < verneed_count; i++, iverneed++)
9418
218
  {
9419
218
    Elf_External_Vernaux *evernaux;
9420
218
    Elf_Internal_Vernaux *ivernaux;
9421
218
    unsigned int j;
9422
9423
218
    _bfd_elf_swap_verneed_in (abfd, everneed, iverneed);
9424
9425
218
    iverneed->vn_bfd = abfd;
9426
9427
218
    if (elf_use_dt_symtab_p (abfd))
9428
2
      {
9429
2
        if (iverneed->vn_file < elf_tdata (abfd)->dt_strsz)
9430
1
    iverneed->vn_filename
9431
1
      = elf_tdata (abfd)->dt_strtab + iverneed->vn_file;
9432
1
        else
9433
1
    iverneed->vn_filename = NULL;
9434
2
      }
9435
216
    else if (hdr == NULL)
9436
0
      goto error_return_bad_verref;
9437
216
    else
9438
216
      iverneed->vn_filename
9439
216
        = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
9440
216
             iverneed->vn_file);
9441
218
    if (iverneed->vn_filename == NULL)
9442
62
      goto error_return_bad_verref;
9443
9444
156
    if (iverneed->vn_cnt == 0)
9445
59
      iverneed->vn_auxptr = NULL;
9446
97
    else
9447
97
      {
9448
97
        if (_bfd_mul_overflow (iverneed->vn_cnt,
9449
97
             sizeof (Elf_Internal_Vernaux), &amt))
9450
0
    {
9451
0
      bfd_set_error (bfd_error_file_too_big);
9452
0
      goto error_return_verref;
9453
0
    }
9454
97
        iverneed->vn_auxptr = bfd_alloc (abfd, amt);
9455
97
        if (iverneed->vn_auxptr == NULL)
9456
0
    goto error_return_verref;
9457
97
      }
9458
9459
156
    if (iverneed->vn_aux
9460
156
        > (size_t) (contents_end - (bfd_byte *) everneed))
9461
24
      goto error_return_bad_verref;
9462
9463
132
    evernaux = ((Elf_External_Vernaux *)
9464
132
          ((bfd_byte *) everneed + iverneed->vn_aux));
9465
132
    ivernaux = iverneed->vn_auxptr;
9466
193
    for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++)
9467
140
      {
9468
140
        _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux);
9469
9470
140
        if (elf_use_dt_symtab_p (abfd))
9471
0
    {
9472
0
      if (ivernaux->vna_name < elf_tdata (abfd)->dt_strsz)
9473
0
        ivernaux->vna_nodename
9474
0
          = elf_tdata (abfd)->dt_strtab + ivernaux->vna_name;
9475
0
      else
9476
0
        ivernaux->vna_nodename = NULL;
9477
0
    }
9478
140
        else if (hdr == NULL)
9479
0
    goto error_return_bad_verref;
9480
140
        else
9481
140
    ivernaux->vna_nodename
9482
140
      = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
9483
140
                 ivernaux->vna_name);
9484
140
        if (ivernaux->vna_nodename == NULL)
9485
5
    goto error_return_bad_verref;
9486
9487
135
        if (ivernaux->vna_other > freeidx)
9488
72
    freeidx = ivernaux->vna_other;
9489
9490
135
        ivernaux->vna_nextptr = NULL;
9491
135
        if (ivernaux->vna_next == 0)
9492
66
    {
9493
66
      iverneed->vn_cnt = j + 1;
9494
66
      break;
9495
66
    }
9496
69
        if (j + 1 < iverneed->vn_cnt)
9497
58
    ivernaux->vna_nextptr = ivernaux + 1;
9498
9499
69
        if (ivernaux->vna_next
9500
69
      > (size_t) (contents_end - (bfd_byte *) evernaux))
9501
8
    goto error_return_bad_verref;
9502
9503
61
        evernaux = ((Elf_External_Vernaux *)
9504
61
        ((bfd_byte *) evernaux + ivernaux->vna_next));
9505
61
      }
9506
9507
119
    iverneed->vn_nextref = NULL;
9508
119
    if (iverneed->vn_next == 0)
9509
90
      break;
9510
29
    if (hdr != NULL && (i + 1 < hdr->sh_info))
9511
1
      iverneed->vn_nextref = iverneed + 1;
9512
9513
29
    if (iverneed->vn_next
9514
29
        > (size_t) (contents_end - (bfd_byte *) everneed))
9515
5
      goto error_return_bad_verref;
9516
9517
24
    everneed = ((Elf_External_Verneed *)
9518
24
          ((bfd_byte *) everneed + iverneed->vn_next));
9519
24
  }
9520
114
      elf_tdata (abfd)->cverrefs = i;
9521
9522
114
      if (contents != elf_tdata (abfd)->dt_verneed)
9523
113
  _bfd_munmap_temporary (contents_addr, contents_size);
9524
114
      contents = NULL;
9525
114
      contents_addr = NULL;
9526
114
    }
9527
9528
143
  if (elf_dynverdef (abfd) != 0 || elf_tdata (abfd)->dt_verdef != NULL)
9529
59
    {
9530
59
      Elf_Internal_Shdr *hdr;
9531
59
      Elf_External_Verdef *everdef;
9532
59
      Elf_Internal_Verdef *iverdef;
9533
59
      Elf_Internal_Verdef *iverdefarr;
9534
59
      Elf_Internal_Verdef iverdefmem;
9535
59
      unsigned int i;
9536
59
      unsigned int maxidx;
9537
59
      bfd_byte *contents_end_def, *contents_end_aux;
9538
59
      size_t verdef_count;
9539
59
      size_t verdef_size;
9540
9541
59
      if (elf_tdata (abfd)->dt_verdef != NULL)
9542
0
  {
9543
0
    hdr = NULL;
9544
0
    contents = elf_tdata (abfd)->dt_verdef;
9545
0
    verdef_count = elf_tdata (abfd)->dt_verdef_count;
9546
0
    verdef_size = verdef_count * sizeof (Elf_External_Verdef);
9547
0
  }
9548
59
      else
9549
59
  {
9550
59
    hdr = &elf_tdata (abfd)->dynverdef_hdr;
9551
9552
59
    if (hdr->sh_size < sizeof (Elf_External_Verdef))
9553
4
      {
9554
25
      error_return_bad_verdef:
9555
25
        _bfd_error_handler
9556
25
    (_("%pB: .gnu.version_d invalid entry"), abfd);
9557
25
        bfd_set_error (bfd_error_bad_value);
9558
35
      error_return_verdef:
9559
35
        elf_tdata (abfd)->verdef = NULL;
9560
35
        elf_tdata (abfd)->cverdefs = 0;
9561
35
        goto error_return;
9562
25
      }
9563
9564
55
    if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0)
9565
4
      goto error_return_verdef;
9566
51
    contents_size = hdr->sh_size;
9567
51
    contents = _bfd_mmap_temporary (abfd, contents_size,
9568
51
            &contents_addr, &contents_size);
9569
51
    if (contents == NULL)
9570
6
      goto error_return_verdef;
9571
9572
45
    BFD_ASSERT (sizeof (Elf_External_Verdef)
9573
45
          >= sizeof (Elf_External_Verdaux));
9574
9575
45
    verdef_count = hdr->sh_info;
9576
45
    verdef_size = hdr->sh_size;
9577
45
  }
9578
9579
45
      contents_end_def = (contents + verdef_size
9580
45
        - sizeof (Elf_External_Verdef));
9581
45
      contents_end_aux = (contents + verdef_size
9582
45
        - sizeof (Elf_External_Verdaux));
9583
9584
      /* We know the number of entries in the section but not the maximum
9585
   index.  Therefore we have to run through all entries and find
9586
   the maximum.  */
9587
45
      everdef = (Elf_External_Verdef *) contents;
9588
45
      maxidx = 0;
9589
49
      for (i = 0; i < verdef_count; ++i)
9590
46
  {
9591
46
    _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
9592
9593
46
    if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) == 0)
9594
6
      goto error_return_bad_verdef;
9595
40
    if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) > maxidx)
9596
40
      maxidx = iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION);
9597
9598
40
    if (iverdefmem.vd_next == 0)
9599
32
      break;
9600
9601
8
    if (iverdefmem.vd_next
9602
8
        > (size_t) (contents_end_def - (bfd_byte *) everdef))
9603
4
      goto error_return_bad_verdef;
9604
9605
4
    everdef = ((Elf_External_Verdef *)
9606
4
         ((bfd_byte *) everdef + iverdefmem.vd_next));
9607
4
  }
9608
9609
35
      if (default_imported_symver)
9610
0
  {
9611
0
    if (freeidx > maxidx)
9612
0
      maxidx = ++freeidx;
9613
0
    else
9614
0
      freeidx = ++maxidx;
9615
0
  }
9616
35
      if (_bfd_mul_overflow (maxidx, sizeof (Elf_Internal_Verdef), &amt))
9617
0
  {
9618
0
    bfd_set_error (bfd_error_file_too_big);
9619
0
    goto error_return_verdef;
9620
0
  }
9621
9622
35
      if (amt == 0)
9623
0
  goto error_return_verdef;
9624
35
      elf_tdata (abfd)->verdef = bfd_zalloc (abfd, amt);
9625
35
      if (elf_tdata (abfd)->verdef == NULL)
9626
0
  goto error_return_verdef;
9627
9628
35
      elf_tdata (abfd)->cverdefs = maxidx;
9629
9630
35
      everdef = (Elf_External_Verdef *) contents;
9631
35
      iverdefarr = elf_tdata (abfd)->verdef;
9632
38
      for (i = 0; i < verdef_count; ++i)
9633
35
  {
9634
35
    Elf_External_Verdaux *everdaux;
9635
35
    Elf_Internal_Verdaux *iverdaux;
9636
35
    unsigned int j;
9637
9638
35
    _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
9639
9640
35
    if ((iverdefmem.vd_ndx & VERSYM_VERSION) == 0)
9641
0
      goto error_return_bad_verdef;
9642
9643
35
    iverdef = &iverdefarr[(iverdefmem.vd_ndx & VERSYM_VERSION) - 1];
9644
35
    memcpy (iverdef, &iverdefmem, offsetof (Elf_Internal_Verdef, vd_bfd));
9645
9646
35
    iverdef->vd_bfd = abfd;
9647
9648
35
    if (iverdef->vd_cnt == 0)
9649
25
      iverdef->vd_auxptr = NULL;
9650
10
    else
9651
10
      {
9652
10
        if (_bfd_mul_overflow (iverdef->vd_cnt,
9653
10
             sizeof (Elf_Internal_Verdaux), &amt))
9654
0
    {
9655
0
      bfd_set_error (bfd_error_file_too_big);
9656
0
      goto error_return_verdef;
9657
0
    }
9658
10
        iverdef->vd_auxptr = bfd_alloc (abfd, amt);
9659
10
        if (iverdef->vd_auxptr == NULL)
9660
0
    goto error_return_verdef;
9661
10
      }
9662
9663
35
    if (iverdef->vd_aux
9664
35
        > (size_t) (contents_end_aux - (bfd_byte *) everdef))
9665
4
      goto error_return_bad_verdef;
9666
9667
31
    everdaux = ((Elf_External_Verdaux *)
9668
31
          ((bfd_byte *) everdef + iverdef->vd_aux));
9669
31
    iverdaux = iverdef->vd_auxptr;
9670
31
    for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++)
9671
8
      {
9672
8
        _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux);
9673
9674
8
        if (elf_use_dt_symtab_p (abfd))
9675
0
    {
9676
0
      if (iverdaux->vda_name < elf_tdata (abfd)->dt_strsz)
9677
0
        iverdaux->vda_nodename
9678
0
          = elf_tdata (abfd)->dt_strtab + iverdaux->vda_name;
9679
0
      else
9680
0
        iverdaux->vda_nodename = NULL;
9681
0
    }
9682
8
        else
9683
8
    iverdaux->vda_nodename
9684
8
      = bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
9685
8
                 iverdaux->vda_name);
9686
8
        if (iverdaux->vda_nodename == NULL)
9687
5
    goto error_return_bad_verdef;
9688
9689
3
        iverdaux->vda_nextptr = NULL;
9690
3
        if (iverdaux->vda_next == 0)
9691
1
    {
9692
1
      iverdef->vd_cnt = j + 1;
9693
1
      break;
9694
1
    }
9695
2
        if (j + 1 < iverdef->vd_cnt)
9696
1
    iverdaux->vda_nextptr = iverdaux + 1;
9697
9698
2
        if (iverdaux->vda_next
9699
2
      > (size_t) (contents_end_aux - (bfd_byte *) everdaux))
9700
2
    goto error_return_bad_verdef;
9701
9702
0
        everdaux = ((Elf_External_Verdaux *)
9703
0
        ((bfd_byte *) everdaux + iverdaux->vda_next));
9704
0
      }
9705
9706
24
    iverdef->vd_nodename = NULL;
9707
24
    if (iverdef->vd_cnt)
9708
1
      iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename;
9709
9710
24
    iverdef->vd_nextdef = NULL;
9711
24
    if (iverdef->vd_next == 0)
9712
21
      break;
9713
3
    if ((size_t) (iverdef - iverdefarr) + 1 < maxidx)
9714
0
      iverdef->vd_nextdef = iverdef + 1;
9715
9716
3
    everdef = ((Elf_External_Verdef *)
9717
3
         ((bfd_byte *) everdef + iverdef->vd_next));
9718
3
  }
9719
9720
24
      if (contents != elf_tdata (abfd)->dt_verdef)
9721
24
  _bfd_munmap_temporary (contents_addr, contents_size);
9722
24
      contents = NULL;
9723
24
      contents_addr = NULL;
9724
24
    }
9725
84
  else if (default_imported_symver)
9726
0
    {
9727
0
      if (freeidx < 3)
9728
0
  freeidx = 3;
9729
0
      else
9730
0
  freeidx++;
9731
9732
0
      if (_bfd_mul_overflow (freeidx, sizeof (Elf_Internal_Verdef), &amt))
9733
0
  {
9734
0
    bfd_set_error (bfd_error_file_too_big);
9735
0
    goto error_return;
9736
0
  }
9737
0
      if (amt == 0)
9738
0
  goto error_return;
9739
0
      elf_tdata (abfd)->verdef = bfd_zalloc (abfd, amt);
9740
0
      if (elf_tdata (abfd)->verdef == NULL)
9741
0
  goto error_return;
9742
9743
0
      elf_tdata (abfd)->cverdefs = freeidx;
9744
0
    }
9745
9746
  /* Create a default version based on the soname.  */
9747
108
  if (default_imported_symver)
9748
0
    {
9749
0
      Elf_Internal_Verdef *iverdef;
9750
0
      Elf_Internal_Verdaux *iverdaux;
9751
9752
0
      iverdef = &elf_tdata (abfd)->verdef[freeidx - 1];
9753
9754
0
      iverdef->vd_version = VER_DEF_CURRENT;
9755
0
      iverdef->vd_flags = 0;
9756
0
      iverdef->vd_ndx = freeidx;
9757
0
      iverdef->vd_cnt = 1;
9758
9759
0
      iverdef->vd_bfd = abfd;
9760
9761
0
      iverdef->vd_nodename = bfd_elf_get_dt_soname (abfd);
9762
0
      if (iverdef->vd_nodename == NULL)
9763
0
  goto error_return_verdef;
9764
0
      iverdef->vd_nextdef = NULL;
9765
0
      iverdef->vd_auxptr = bfd_zalloc (abfd, sizeof (Elf_Internal_Verdaux));
9766
0
      if (iverdef->vd_auxptr == NULL)
9767
0
  goto error_return_verdef;
9768
9769
0
      iverdaux = iverdef->vd_auxptr;
9770
0
      iverdaux->vda_nodename = iverdef->vd_nodename;
9771
0
    }
9772
9773
108
  return true;
9774
9775
168
 error_return:
9776
168
  if (contents != elf_tdata (abfd)->dt_verneed
9777
124
      && contents != elf_tdata (abfd)->dt_verdef)
9778
124
    _bfd_munmap_temporary (contents_addr, contents_size);
9779
168
  return false;
9780
108
}
9781

9782
asymbol *
9783
_bfd_elf_make_empty_symbol (bfd *abfd)
9784
1.71M
{
9785
1.71M
  elf_symbol_type *newsym;
9786
9787
1.71M
  newsym = bfd_zalloc (abfd, sizeof (*newsym));
9788
1.71M
  if (!newsym)
9789
0
    return NULL;
9790
1.71M
  newsym->symbol.the_bfd = abfd;
9791
1.71M
  return &newsym->symbol;
9792
1.71M
}
9793
9794
void
9795
_bfd_elf_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED,
9796
        asymbol *symbol,
9797
        symbol_info *ret)
9798
34.9k
{
9799
34.9k
  bfd_symbol_info (symbol, ret);
9800
34.9k
}
9801
9802
/* Return whether a symbol name implies a local symbol.  Most targets
9803
   use this function for the is_local_label_name entry point, but some
9804
   override it.  */
9805
9806
bool
9807
_bfd_elf_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED,
9808
            const char *name)
9809
24.7k
{
9810
  /* Normal local symbols start with ``.L''.  */
9811
24.7k
  if (name[0] == '.' && name[1] == 'L')
9812
4.80k
    return true;
9813
9814
  /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
9815
     DWARF debugging symbols starting with ``..''.  */
9816
19.9k
  if (name[0] == '.' && name[1] == '.')
9817
704
    return true;
9818
9819
  /* gcc will sometimes generate symbols beginning with ``_.L_'' when
9820
     emitting DWARF debugging output.  I suspect this is actually a
9821
     small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
9822
     ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
9823
     underscore to be emitted on some ELF targets).  For ease of use,
9824
     we treat such symbols as local.  */
9825
19.2k
  if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_')
9826
3
    return true;
9827
9828
  /* Treat assembler generated fake symbols, dollar local labels and
9829
     forward-backward labels (aka local labels) as locals.
9830
     These labels have the form:
9831
9832
       L0^A.*              (fake symbols)
9833
9834
       [.]?L[0123456789]+{^A|^B}[0123456789]*  (local labels)
9835
9836
     Versions which start with .L will have already been matched above,
9837
     so we only need to match the rest.  */
9838
19.2k
  if (name[0] == 'L' && ISDIGIT (name[1]))
9839
127
    {
9840
127
      bool ret = false;
9841
127
      const char * p;
9842
127
      char c;
9843
9844
150
      for (p = name + 2; (c = *p); p++)
9845
148
  {
9846
148
    if (c == 1 || c == 2)
9847
13
      {
9848
13
        if (c == 1 && p == name + 2)
9849
    /* A fake symbol.  */
9850
13
    return true;
9851
9852
        /* FIXME: We are being paranoid here and treating symbols like
9853
     L0^Bfoo as if there were non-local, on the grounds that the
9854
     assembler will never generate them.  But can any symbol
9855
     containing an ASCII value in the range 1-31 ever be anything
9856
     other than some kind of local ?  */
9857
0
        ret = true;
9858
0
      }
9859
9860
135
    if (! ISDIGIT (c))
9861
112
      {
9862
112
        ret = false;
9863
112
        break;
9864
112
      }
9865
135
  }
9866
114
      return ret;
9867
127
    }
9868
9869
19.0k
  return false;
9870
19.2k
}
9871
9872
alent *
9873
_bfd_elf_get_lineno (bfd *abfd ATTRIBUTE_UNUSED,
9874
         asymbol *symbol ATTRIBUTE_UNUSED)
9875
0
{
9876
0
  abort ();
9877
0
  return NULL;
9878
0
}
9879
9880
bool
9881
_bfd_elf_set_arch_mach (bfd *abfd,
9882
      enum bfd_architecture arch,
9883
      unsigned long machine)
9884
1.19k
{
9885
  /* If this isn't the right architecture for this backend, and this
9886
     isn't the generic backend, fail.  */
9887
1.19k
  if (arch != get_elf_backend_data (abfd)->arch
9888
0
      && arch != bfd_arch_unknown
9889
0
      && get_elf_backend_data (abfd)->arch != bfd_arch_unknown)
9890
0
    return false;
9891
9892
1.19k
  return bfd_default_set_arch_mach (abfd, arch, machine);
9893
1.19k
}
9894
9895
/* Find the nearest line to a particular section and offset,
9896
   for error reporting.  */
9897
9898
bool
9899
_bfd_elf_find_nearest_line (bfd *abfd,
9900
          asymbol **symbols,
9901
          asection *section,
9902
          bfd_vma offset,
9903
          const char **filename_ptr,
9904
          const char **functionname_ptr,
9905
          unsigned int *line_ptr,
9906
          unsigned int *discriminator_ptr)
9907
30.6k
{
9908
30.6k
  return _bfd_elf_find_nearest_line_with_alt (abfd, NULL, symbols, section,
9909
30.6k
                offset, filename_ptr,
9910
30.6k
                functionname_ptr, line_ptr,
9911
30.6k
                discriminator_ptr);
9912
30.6k
}
9913
9914
/* Find the nearest line to a particular section and offset,
9915
   for error reporting.  ALT_BFD representing a .gnu_debugaltlink file
9916
   can be optionally specified.  */
9917
9918
bool
9919
_bfd_elf_find_nearest_line_with_alt (bfd *abfd,
9920
             const char *alt_filename,
9921
             asymbol **symbols,
9922
             asection *section,
9923
             bfd_vma offset,
9924
             const char **filename_ptr,
9925
             const char **functionname_ptr,
9926
             unsigned int *line_ptr,
9927
             unsigned int *discriminator_ptr)
9928
30.6k
{
9929
30.6k
  bool found;
9930
9931
30.6k
  if (_bfd_dwarf2_find_nearest_line_with_alt (abfd, alt_filename, symbols, NULL,
9932
30.6k
                section, offset, filename_ptr,
9933
30.6k
                functionname_ptr, line_ptr,
9934
30.6k
                discriminator_ptr,
9935
30.6k
                dwarf_debug_sections,
9936
30.6k
                &elf_tdata (abfd)->dwarf2_find_line_info))
9937
7.93k
    return true;
9938
9939
22.6k
  if (_bfd_dwarf1_find_nearest_line (abfd, symbols, section, offset,
9940
22.6k
             filename_ptr, functionname_ptr, line_ptr))
9941
0
    {
9942
0
      if (!*functionname_ptr)
9943
0
  _bfd_elf_find_function (abfd, symbols, section, offset,
9944
0
        *filename_ptr ? NULL : filename_ptr,
9945
0
        functionname_ptr);
9946
0
      return true;
9947
0
    }
9948
9949
22.6k
  if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
9950
22.6k
               &found, filename_ptr,
9951
22.6k
               functionname_ptr, line_ptr,
9952
22.6k
               &elf_tdata (abfd)->line_info))
9953
0
    return false;
9954
22.6k
  if (found && (*functionname_ptr || *line_ptr))
9955
0
    return true;
9956
9957
22.6k
  if (symbols == NULL)
9958
6.10k
    return false;
9959
9960
16.5k
  if (! _bfd_elf_find_function (abfd, symbols, section, offset,
9961
16.5k
        filename_ptr, functionname_ptr))
9962
13.4k
    return false;
9963
9964
3.19k
  *line_ptr = 0;
9965
3.19k
  return true;
9966
16.5k
}
9967
9968
/* Find the line for a symbol.  */
9969
9970
bool
9971
_bfd_elf_find_line (bfd *abfd, asymbol **symbols, asymbol *symbol,
9972
        const char **filename_ptr, unsigned int *line_ptr)
9973
5.04k
{
9974
5.04k
  struct elf_obj_tdata *tdata = elf_tdata (abfd);
9975
5.04k
  return _bfd_dwarf2_find_nearest_line (abfd, symbols, symbol, NULL, 0,
9976
5.04k
          filename_ptr, NULL, line_ptr, NULL,
9977
5.04k
          dwarf_debug_sections,
9978
5.04k
          &tdata->dwarf2_find_line_info);
9979
5.04k
}
9980
9981
/* After a call to bfd_find_nearest_line, successive calls to
9982
   bfd_find_inliner_info can be used to get source information about
9983
   each level of function inlining that terminated at the address
9984
   passed to bfd_find_nearest_line.  Currently this is only supported
9985
   for DWARF2 with appropriate DWARF3 extensions. */
9986
9987
bool
9988
_bfd_elf_find_inliner_info (bfd *abfd,
9989
          const char **filename_ptr,
9990
          const char **functionname_ptr,
9991
          unsigned int *line_ptr)
9992
0
{
9993
0
  struct elf_obj_tdata *tdata = elf_tdata (abfd);
9994
0
  return _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
9995
0
          functionname_ptr, line_ptr,
9996
0
          &tdata->dwarf2_find_line_info);
9997
0
}
9998
9999
int
10000
_bfd_elf_sizeof_headers (bfd *abfd, struct bfd_link_info *info)
10001
0
{
10002
0
  elf_backend_data *bed = get_elf_backend_data (abfd);
10003
0
  int ret = bed->s->sizeof_ehdr;
10004
10005
0
  if (!bfd_link_relocatable (info))
10006
0
    {
10007
0
      bfd_size_type phdr_size = elf_program_header_size (abfd);
10008
10009
0
      if (phdr_size == (bfd_size_type) -1)
10010
0
  {
10011
0
    struct elf_segment_map *m;
10012
10013
0
    phdr_size = 0;
10014
0
    for (m = elf_seg_map (abfd); m != NULL; m = m->next)
10015
0
      phdr_size += bed->s->sizeof_phdr;
10016
10017
0
    if (phdr_size == 0)
10018
0
      phdr_size = get_program_header_size (abfd, info);
10019
0
  }
10020
10021
0
      elf_program_header_size (abfd) = phdr_size;
10022
0
      ret += phdr_size;
10023
0
    }
10024
10025
0
  return ret;
10026
0
}
10027
10028
bool
10029
_bfd_elf_set_section_contents (bfd *abfd,
10030
             sec_ptr section,
10031
             const void *location,
10032
             file_ptr offset,
10033
             bfd_size_type count)
10034
1.36k
{
10035
1.36k
  Elf_Internal_Shdr *hdr;
10036
10037
1.36k
  if (! abfd->output_has_begun
10038
210
      && ! _bfd_elf_compute_section_file_positions (abfd, NULL))
10039
18
    return false;
10040
10041
1.34k
  if (!count)
10042
0
    return true;
10043
10044
1.34k
  hdr = &elf_section_data (section)->this_hdr;
10045
1.34k
  if (hdr->sh_offset == (file_ptr) -1)
10046
0
    {
10047
0
      unsigned char *contents;
10048
10049
0
      if (bfd_section_is_ctf (section))
10050
  /* Nothing to do with this section: the contents are generated
10051
     later.  */
10052
0
  return true;
10053
10054
0
      if ((offset + count) > hdr->sh_size)
10055
0
  {
10056
0
    _bfd_error_handler
10057
0
      (_("%pB:%pA: error: attempting to write"
10058
0
         " over the end of the section"),
10059
0
       abfd, section);
10060
10061
0
    bfd_set_error (bfd_error_invalid_operation);
10062
0
    return false;
10063
0
  }
10064
10065
0
      contents = hdr->contents;
10066
0
      if (contents == NULL)
10067
0
  {
10068
0
    _bfd_error_handler
10069
0
      (_("%pB:%pA: error: attempting to write"
10070
0
         " section into an empty buffer"),
10071
0
       abfd, section);
10072
10073
0
    bfd_set_error (bfd_error_invalid_operation);
10074
0
    return false;
10075
0
  }
10076
10077
0
      memcpy (contents + offset, location, count);
10078
0
      return true;
10079
0
    }
10080
10081
1.34k
  return _bfd_generic_set_section_contents (abfd, section,
10082
1.34k
              location, offset, count);
10083
1.34k
}
10084
10085
bool
10086
_bfd_elf_no_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED,
10087
         arelent *cache_ptr ATTRIBUTE_UNUSED,
10088
         Elf_Internal_Rela *dst ATTRIBUTE_UNUSED)
10089
0
{
10090
0
  abort ();
10091
0
  return false;
10092
0
}
10093
10094
/* Try to convert a non-ELF reloc into an ELF one.  */
10095
10096
bool
10097
_bfd_elf_validate_reloc (bfd *abfd, arelent *areloc)
10098
0
{
10099
  /* Check whether we really have an ELF howto.  */
10100
10101
0
  if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec)
10102
0
    {
10103
0
      bfd_reloc_code_real_type code;
10104
0
      reloc_howto_type *howto;
10105
10106
      /* Alien reloc: Try to determine its type to replace it with an
10107
   equivalent ELF reloc.  */
10108
10109
0
      if (areloc->howto->pc_relative)
10110
0
  {
10111
0
    switch (areloc->howto->bitsize)
10112
0
      {
10113
0
      case 8:
10114
0
        code = BFD_RELOC_8_PCREL;
10115
0
        break;
10116
0
      case 12:
10117
0
        code = BFD_RELOC_12_PCREL;
10118
0
        break;
10119
0
      case 16:
10120
0
        code = BFD_RELOC_16_PCREL;
10121
0
        break;
10122
0
      case 24:
10123
0
        code = BFD_RELOC_24_PCREL;
10124
0
        break;
10125
0
      case 32:
10126
0
        code = BFD_RELOC_32_PCREL;
10127
0
        break;
10128
0
      case 64:
10129
0
        code = BFD_RELOC_64_PCREL;
10130
0
        break;
10131
0
      default:
10132
0
        goto fail;
10133
0
      }
10134
10135
0
    howto = bfd_reloc_type_lookup (abfd, code);
10136
10137
0
    if (howto && areloc->howto->pcrel_offset != howto->pcrel_offset)
10138
0
      {
10139
0
        if (howto->pcrel_offset)
10140
0
    areloc->addend += areloc->address;
10141
0
        else
10142
0
    areloc->addend -= areloc->address; /* addend is unsigned!! */
10143
0
      }
10144
0
  }
10145
0
      else
10146
0
  {
10147
0
    switch (areloc->howto->bitsize)
10148
0
      {
10149
0
      case 8:
10150
0
        code = BFD_RELOC_8;
10151
0
        break;
10152
0
      case 14:
10153
0
        code = BFD_RELOC_14;
10154
0
        break;
10155
0
      case 16:
10156
0
        code = BFD_RELOC_16;
10157
0
        break;
10158
0
      case 26:
10159
0
        code = BFD_RELOC_26;
10160
0
        break;
10161
0
      case 32:
10162
0
        code = BFD_RELOC_32;
10163
0
        break;
10164
0
      case 64:
10165
0
        code = BFD_RELOC_64;
10166
0
        break;
10167
0
      default:
10168
0
        goto fail;
10169
0
      }
10170
10171
0
    howto = bfd_reloc_type_lookup (abfd, code);
10172
0
  }
10173
10174
0
      if (howto)
10175
0
  areloc->howto = howto;
10176
0
      else
10177
0
  goto fail;
10178
0
    }
10179
10180
0
  return true;
10181
10182
0
 fail:
10183
  /* xgettext:c-format */
10184
0
  _bfd_error_handler (_("%pB: %s unsupported"),
10185
0
          abfd, areloc->howto->name);
10186
0
  bfd_set_error (bfd_error_sorry);
10187
0
  return false;
10188
0
}
10189
10190
bool
10191
_bfd_elf_free_cached_info (bfd *abfd)
10192
7.26M
{
10193
7.26M
  struct elf_obj_tdata *tdata;
10194
10195
7.26M
  if ((bfd_get_format (abfd) == bfd_object
10196
7.23M
       || bfd_get_format (abfd) == bfd_core)
10197
43.4k
      && (tdata = elf_tdata (abfd)) != NULL)
10198
43.4k
    {
10199
43.4k
      if (tdata->o != NULL && elf_shstrtab (abfd) != NULL)
10200
687
  _bfd_elf_strtab_free (elf_shstrtab (abfd));
10201
43.4k
      _bfd_dwarf2_cleanup_debug_info (abfd, &tdata->dwarf2_find_line_info);
10202
43.4k
      _bfd_dwarf1_cleanup_debug_info (abfd, &tdata->dwarf1_find_line_info);
10203
43.4k
      _bfd_stab_cleanup (abfd, &tdata->line_info);
10204
43.4k
      _bfd_elf_cleanup_object_attributes (abfd);
10205
533k
      for (asection *sec = abfd->sections; sec != NULL; sec = sec->next)
10206
490k
  {
10207
490k
    _bfd_elf_munmap_section_contents (sec, sec->contents);
10208
490k
    if (!sec->alloced)
10209
489k
      {
10210
489k
        free (elf_section_data (sec)->this_hdr.contents);
10211
489k
        elf_section_data (sec)->this_hdr.contents = NULL;
10212
489k
      }
10213
490k
    free (elf_section_data (sec)->relocs);
10214
490k
    elf_section_data (sec)->relocs = NULL;
10215
490k
    if (sec->sec_info_type == SEC_INFO_TYPE_EH_FRAME)
10216
0
      {
10217
0
        struct eh_frame_sec_info *sec_info = sec->sec_info;
10218
0
        free (sec_info->cies);
10219
0
      }
10220
490k
    if (sec->sec_info_type == SEC_INFO_TYPE_SFRAME)
10221
0
      {
10222
0
        struct sframe_dec_info *sfd_info = sec->sec_info;
10223
0
        sframe_decoder_free (&sfd_info->sfd_ctx);
10224
0
      }
10225
490k
  }
10226
43.4k
      free (tdata->symtab_hdr.contents);
10227
43.4k
      tdata->symtab_hdr.contents = NULL;
10228
43.4k
    }
10229
10230
7.26M
  return _bfd_generic_bfd_free_cached_info (abfd);
10231
7.26M
}
10232
10233
/* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
10234
   in the relocation's offset.  Thus we cannot allow any sort of sanity
10235
   range-checking to interfere.  There is nothing else to do in processing
10236
   this reloc.  */
10237
10238
bfd_reloc_status_type
10239
_bfd_elf_rel_vtable_reloc_fn
10240
  (bfd *abfd ATTRIBUTE_UNUSED, arelent *re ATTRIBUTE_UNUSED,
10241
   struct bfd_symbol *symbol ATTRIBUTE_UNUSED,
10242
   void *data ATTRIBUTE_UNUSED, asection *is ATTRIBUTE_UNUSED,
10243
   bfd *obfd ATTRIBUTE_UNUSED, char **errmsg ATTRIBUTE_UNUSED)
10244
98
{
10245
98
  return bfd_reloc_ok;
10246
98
}
10247

10248
/* Elf core file support.  Much of this only works on native
10249
   toolchains, since we rely on knowing the
10250
   machine-dependent procfs structure in order to pick
10251
   out details about the corefile.  */
10252
10253
#ifdef HAVE_SYS_PROCFS_H
10254
# include <sys/procfs.h>
10255
#endif
10256
10257
/* Return a PID that identifies a "thread" for threaded cores, or the
10258
   PID of the main process for non-threaded cores.  */
10259
10260
static int
10261
elfcore_make_pid (bfd *abfd)
10262
233
{
10263
233
  int pid;
10264
10265
233
  pid = elf_tdata (abfd)->core->lwpid;
10266
233
  if (pid == 0)
10267
233
    pid = elf_tdata (abfd)->core->pid;
10268
10269
233
  return pid;
10270
233
}
10271
10272
/* If there isn't a section called NAME, make one, using data from
10273
   SECT.  Note, this function will generate a reference to NAME, so
10274
   you shouldn't deallocate or overwrite it.  */
10275
10276
static bool
10277
elfcore_maybe_make_sect (bfd *abfd, char *name, asection *sect)
10278
233
{
10279
233
  asection *sect2;
10280
10281
233
  if (bfd_get_section_by_name (abfd, name) != NULL)
10282
144
    return true;
10283
10284
89
  sect2 = bfd_make_section_with_flags (abfd, name, sect->flags);
10285
89
  if (sect2 == NULL)
10286
0
    return false;
10287
10288
89
  sect2->size = sect->size;
10289
89
  sect2->filepos = sect->filepos;
10290
89
  sect2->alignment_power = sect->alignment_power;
10291
89
  return true;
10292
89
}
10293
10294
/* Create a pseudosection containing SIZE bytes at FILEPOS.  This
10295
   actually creates up to two pseudosections:
10296
   - For the single-threaded case, a section named NAME, unless
10297
     such a section already exists.
10298
   - For the multi-threaded case, a section named "NAME/PID", where
10299
     PID is elfcore_make_pid (abfd).
10300
   Both pseudosections have identical contents.  */
10301
bool
10302
_bfd_elfcore_make_pseudosection (bfd *abfd,
10303
         char *name,
10304
         size_t size,
10305
         ufile_ptr filepos)
10306
233
{
10307
233
  char buf[100];
10308
233
  char *threaded_name;
10309
233
  size_t len;
10310
233
  asection *sect;
10311
10312
  /* Build the section name.  */
10313
10314
233
  sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd));
10315
233
  len = strlen (buf) + 1;
10316
233
  threaded_name = bfd_alloc (abfd, len);
10317
233
  if (threaded_name == NULL)
10318
0
    return false;
10319
233
  memcpy (threaded_name, buf, len);
10320
10321
233
  sect = bfd_make_section_anyway_with_flags (abfd, threaded_name,
10322
233
               SEC_HAS_CONTENTS);
10323
233
  if (sect == NULL)
10324
0
    return false;
10325
233
  sect->size = size;
10326
233
  sect->filepos = filepos;
10327
233
  sect->alignment_power = 2;
10328
10329
233
  return elfcore_maybe_make_sect (abfd, name, sect);
10330
233
}
10331
10332
static bool
10333
elfcore_make_auxv_note_section (bfd *abfd, Elf_Internal_Note *note,
10334
        size_t offs)
10335
86
{
10336
86
  asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv",
10337
86
                   SEC_HAS_CONTENTS);
10338
10339
86
  if (sect == NULL)
10340
0
    return false;
10341
10342
86
  sect->size = note->descsz - offs;
10343
86
  sect->filepos = note->descpos + offs;
10344
86
  sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
10345
10346
86
  return true;
10347
86
}
10348
10349
/* prstatus_t exists on:
10350
     solaris 2.5+
10351
     linux 2.[01] + glibc
10352
     unixware 4.2
10353
*/
10354
10355
#if defined (HAVE_PRSTATUS_T)
10356
10357
static bool
10358
elfcore_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
10359
467
{
10360
467
  size_t size;
10361
467
  int offset;
10362
10363
467
  if (note->descsz == sizeof (prstatus_t))
10364
0
    {
10365
0
      prstatus_t prstat;
10366
10367
0
      size = sizeof (prstat.pr_reg);
10368
0
      offset   = offsetof (prstatus_t, pr_reg);
10369
0
      memcpy (&prstat, note->descdata, sizeof (prstat));
10370
10371
      /* Do not overwrite the core signal if it
10372
   has already been set by another thread.  */
10373
0
      if (elf_tdata (abfd)->core->signal == 0)
10374
0
  elf_tdata (abfd)->core->signal = prstat.pr_cursig;
10375
0
      if (elf_tdata (abfd)->core->pid == 0)
10376
0
  elf_tdata (abfd)->core->pid = prstat.pr_pid;
10377
10378
      /* pr_who exists on:
10379
   solaris 2.5+
10380
   unixware 4.2
10381
   pr_who doesn't exist on:
10382
   linux 2.[01]
10383
   */
10384
#if defined (HAVE_PRSTATUS_T_PR_WHO)
10385
      elf_tdata (abfd)->core->lwpid = prstat.pr_who;
10386
#else
10387
0
      elf_tdata (abfd)->core->lwpid = prstat.pr_pid;
10388
0
#endif
10389
0
    }
10390
467
#if defined (HAVE_PRSTATUS32_T)
10391
467
  else if (note->descsz == sizeof (prstatus32_t))
10392
0
    {
10393
      /* 64-bit host, 32-bit corefile */
10394
0
      prstatus32_t prstat;
10395
10396
0
      size = sizeof (prstat.pr_reg);
10397
0
      offset   = offsetof (prstatus32_t, pr_reg);
10398
0
      memcpy (&prstat, note->descdata, sizeof (prstat));
10399
10400
      /* Do not overwrite the core signal if it
10401
   has already been set by another thread.  */
10402
0
      if (elf_tdata (abfd)->core->signal == 0)
10403
0
  elf_tdata (abfd)->core->signal = prstat.pr_cursig;
10404
0
      if (elf_tdata (abfd)->core->pid == 0)
10405
0
  elf_tdata (abfd)->core->pid = prstat.pr_pid;
10406
10407
      /* pr_who exists on:
10408
   solaris 2.5+
10409
   unixware 4.2
10410
   pr_who doesn't exist on:
10411
   linux 2.[01]
10412
   */
10413
#if defined (HAVE_PRSTATUS32_T_PR_WHO)
10414
      elf_tdata (abfd)->core->lwpid = prstat.pr_who;
10415
#else
10416
0
      elf_tdata (abfd)->core->lwpid = prstat.pr_pid;
10417
0
#endif
10418
0
    }
10419
467
#endif /* HAVE_PRSTATUS32_T */
10420
467
  else
10421
467
    {
10422
      /* Fail - we don't know how to handle any other
10423
   note size (ie. data object type).  */
10424
467
      return true;
10425
467
    }
10426
10427
  /* Make a ".reg/999" section and a ".reg" section.  */
10428
0
  return _bfd_elfcore_make_pseudosection (abfd, NOTE_PSEUDO_SECTION_REG,
10429
0
            size, note->descpos + offset);
10430
467
}
10431
#endif /* defined (HAVE_PRSTATUS_T) */
10432
10433
/* Create a pseudosection containing the exact contents of NOTE.  */
10434
static bool
10435
elfcore_make_note_pseudosection (bfd *abfd,
10436
         char *name,
10437
         Elf_Internal_Note *note)
10438
233
{
10439
233
  return _bfd_elfcore_make_pseudosection (abfd, name,
10440
233
            note->descsz, note->descpos);
10441
233
}
10442
10443
/* There isn't a consistent prfpregset_t across platforms,
10444
   but it doesn't matter, because we don't have to pick this
10445
   data structure apart.  */
10446
10447
static bool
10448
elfcore_grok_prfpreg (bfd *abfd, Elf_Internal_Note *note)
10449
233
{
10450
233
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_REG2, note);
10451
233
}
10452
10453
/* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
10454
   type of NT_PRXFPREG.  Just include the whole note's contents
10455
   literally.  */
10456
10457
static bool
10458
elfcore_grok_prxfpreg (bfd *abfd, Elf_Internal_Note *note)
10459
0
{
10460
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_XFP, note);
10461
0
}
10462
10463
/* Linux dumps the Intel XSAVE extended state in a note named "LINUX"
10464
   with a note type of NT_X86_XSTATE.  Just include the whole note's
10465
   contents literally.  */
10466
10467
static bool
10468
elfcore_grok_xstatereg (bfd *abfd, Elf_Internal_Note *note)
10469
0
{
10470
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_XSTATE, note);
10471
0
}
10472
10473
static bool
10474
elfcore_grok_sspreg (bfd *abfd, Elf_Internal_Note *note)
10475
0
{
10476
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_SSP, note);
10477
0
}
10478
10479
/* Linux dumps the XSAVE Layout description in a note named "LINUX"
10480
   with a note type of NT_X86_XSAVE_LAYOUT. */
10481
static bool
10482
elfcore_grok_xsave_layout_desc (bfd *abfd, Elf_Internal_Note *note)
10483
0
{
10484
0
  return elfcore_make_note_pseudosection (abfd,
10485
0
            NOTE_PSEUDO_SECTION_XSAVE_LAYOUT,
10486
0
            note);
10487
0
}
10488
10489
static bool
10490
elfcore_grok_ppc_vmx (bfd *abfd, Elf_Internal_Note *note)
10491
0
{
10492
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_PPC_VMX, note);
10493
0
}
10494
10495
static bool
10496
elfcore_grok_ppc_vsx (bfd *abfd, Elf_Internal_Note *note)
10497
0
{
10498
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_PPC_VSX, note);
10499
0
}
10500
10501
static bool
10502
elfcore_grok_ppc_tar (bfd *abfd, Elf_Internal_Note *note)
10503
0
{
10504
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_PPC_TAR, note);
10505
0
}
10506
10507
static bool
10508
elfcore_grok_ppc_ppr (bfd *abfd, Elf_Internal_Note *note)
10509
0
{
10510
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_PPC_PPR, note);
10511
0
}
10512
10513
static bool
10514
elfcore_grok_ppc_dscr (bfd *abfd, Elf_Internal_Note *note)
10515
0
{
10516
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_PPC_DSCR, note);
10517
0
}
10518
10519
static bool
10520
elfcore_grok_ppc_ebb (bfd *abfd, Elf_Internal_Note *note)
10521
0
{
10522
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_PPC_EBB, note);
10523
0
}
10524
10525
static bool
10526
elfcore_grok_ppc_pmu (bfd *abfd, Elf_Internal_Note *note)
10527
0
{
10528
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_PPC_PMU, note);
10529
0
}
10530
10531
static bool
10532
elfcore_grok_ppc_tm_cgpr (bfd *abfd, Elf_Internal_Note *note)
10533
0
{
10534
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_PPC_TM_CGPR, note);
10535
0
}
10536
10537
static bool
10538
elfcore_grok_ppc_tm_cfpr (bfd *abfd, Elf_Internal_Note *note)
10539
0
{
10540
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_PPC_TM_CFPR, note);
10541
0
}
10542
10543
static bool
10544
elfcore_grok_ppc_tm_cvmx (bfd *abfd, Elf_Internal_Note *note)
10545
0
{
10546
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_PPC_TM_CVMX, note);
10547
0
}
10548
10549
static bool
10550
elfcore_grok_ppc_tm_cvsx (bfd *abfd, Elf_Internal_Note *note)
10551
0
{
10552
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_PPC_TM_CVSX, note);
10553
0
}
10554
10555
static bool
10556
elfcore_grok_ppc_tm_spr (bfd *abfd, Elf_Internal_Note *note)
10557
0
{
10558
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_PPC_TM_SPR, note);
10559
0
}
10560
10561
static bool
10562
elfcore_grok_ppc_tm_ctar (bfd *abfd, Elf_Internal_Note *note)
10563
0
{
10564
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_PPC_TM_CTAR, note);
10565
0
}
10566
10567
static bool
10568
elfcore_grok_ppc_tm_cppr (bfd *abfd, Elf_Internal_Note *note)
10569
0
{
10570
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_PPC_TM_CPPR, note);
10571
0
}
10572
10573
static bool
10574
elfcore_grok_ppc_tm_cdscr (bfd *abfd, Elf_Internal_Note *note)
10575
0
{
10576
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_PPC_TM_CDSCR, note);
10577
0
}
10578
10579
static bool
10580
elfcore_grok_ppc_dmr (bfd *abfd, Elf_Internal_Note *note)
10581
0
{
10582
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_PPC_DMR, note);
10583
0
}
10584
10585
static bool
10586
elfcore_grok_s390_high_gprs (bfd *abfd, Elf_Internal_Note *note)
10587
0
{
10588
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_S390_HIGH_GPRS, note);
10589
0
}
10590
10591
static bool
10592
elfcore_grok_s390_timer (bfd *abfd, Elf_Internal_Note *note)
10593
0
{
10594
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_S390_TIMER, note);
10595
0
}
10596
10597
static bool
10598
elfcore_grok_s390_todcmp (bfd *abfd, Elf_Internal_Note *note)
10599
0
{
10600
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_S390_TODCMP, note);
10601
0
}
10602
10603
static bool
10604
elfcore_grok_s390_todpreg (bfd *abfd, Elf_Internal_Note *note)
10605
0
{
10606
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_S390_TODPREG, note);
10607
0
}
10608
10609
static bool
10610
elfcore_grok_s390_ctrs (bfd *abfd, Elf_Internal_Note *note)
10611
0
{
10612
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_S390_CTRS, note);
10613
0
}
10614
10615
static bool
10616
elfcore_grok_s390_prefix (bfd *abfd, Elf_Internal_Note *note)
10617
0
{
10618
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_S390_PREFIX, note);
10619
0
}
10620
10621
static bool
10622
elfcore_grok_s390_last_break (bfd *abfd, Elf_Internal_Note *note)
10623
0
{
10624
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_S390_LAST_BREAK, note);
10625
0
}
10626
10627
static bool
10628
elfcore_grok_s390_system_call (bfd *abfd, Elf_Internal_Note *note)
10629
0
{
10630
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_S390_SYSTEM_CALL, note);
10631
0
}
10632
10633
static bool
10634
elfcore_grok_s390_tdb (bfd *abfd, Elf_Internal_Note *note)
10635
0
{
10636
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_S390_TDB, note);
10637
0
}
10638
10639
static bool
10640
elfcore_grok_s390_vxrs_low (bfd *abfd, Elf_Internal_Note *note)
10641
0
{
10642
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_S390_VXRS_LOW, note);
10643
0
}
10644
10645
static bool
10646
elfcore_grok_s390_vxrs_high (bfd *abfd, Elf_Internal_Note *note)
10647
0
{
10648
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_S390_VXRS_HIGH, note);
10649
0
}
10650
10651
static bool
10652
elfcore_grok_s390_gs_cb (bfd *abfd, Elf_Internal_Note *note)
10653
0
{
10654
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_S390_GS_CB, note);
10655
0
}
10656
10657
static bool
10658
elfcore_grok_s390_gs_bc (bfd *abfd, Elf_Internal_Note *note)
10659
0
{
10660
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_S390_GS_BC, note);
10661
0
}
10662
10663
static bool
10664
elfcore_grok_arm_vfp (bfd *abfd, Elf_Internal_Note *note)
10665
0
{
10666
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_ARM_VFP, note);
10667
0
}
10668
10669
static bool
10670
elfcore_grok_aarch_tls (bfd *abfd, Elf_Internal_Note *note)
10671
0
{
10672
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_AARCH_TLS, note);
10673
0
}
10674
10675
static bool
10676
elfcore_grok_aarch_hw_break (bfd *abfd, Elf_Internal_Note *note)
10677
0
{
10678
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_AARCH_HW_BREAK, note);
10679
0
}
10680
10681
static bool
10682
elfcore_grok_aarch_hw_watch (bfd *abfd, Elf_Internal_Note *note)
10683
0
{
10684
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_AARCH_HW_WATCH, note);
10685
0
}
10686
10687
static bool
10688
elfcore_grok_aarch_sve (bfd *abfd, Elf_Internal_Note *note)
10689
0
{
10690
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_AARCH_SVE, note);
10691
0
}
10692
10693
static bool
10694
elfcore_grok_aarch_pauth (bfd *abfd, Elf_Internal_Note *note)
10695
0
{
10696
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_AARCH_PAUTH, note);
10697
0
}
10698
10699
static bool
10700
elfcore_grok_aarch_mte (bfd *abfd, Elf_Internal_Note *note)
10701
0
{
10702
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_AARCH_MTE, note);
10703
0
}
10704
10705
static bool
10706
elfcore_grok_aarch_ssve (bfd *abfd, Elf_Internal_Note *note)
10707
0
{
10708
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_AARCH_SSVE, note);
10709
0
}
10710
10711
static bool
10712
elfcore_grok_aarch_za (bfd *abfd, Elf_Internal_Note *note)
10713
0
{
10714
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_AARCH_ZA, note);
10715
0
}
10716
10717
/* Convert NOTE into a bfd_section called ".reg-aarch-zt".  Return TRUE if
10718
   successful, otherwise return FALSE.  */
10719
10720
static bool
10721
elfcore_grok_aarch_zt (bfd *abfd, Elf_Internal_Note *note)
10722
0
{
10723
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_AARCH_ZT, note);
10724
0
}
10725
10726
/* Convert NOTE into a bfd_section called ".reg-aarch-gcs".  Return TRUE if
10727
   successful, otherwise return FALSE.  */
10728
10729
static bool
10730
elfcore_grok_aarch_gcs (bfd *abfd, Elf_Internal_Note *note)
10731
0
{
10732
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_AARCH_GCS, note);
10733
0
}
10734
10735
/* Convert NOTE into a bfd_section called ".reg-aarch-poe".  Return TRUE if
10736
   successful, otherwise return FALSE.  */
10737
10738
static bool
10739
elfcore_grok_aarch_poe (bfd *abfd, Elf_Internal_Note *note)
10740
0
{
10741
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_AARCH_POE, note);
10742
0
}
10743
10744
/* Convert NOTE into the appropriate note pseudo-section for the AArch64 FPMR.
10745
 * Return TRUE if successful, otherwise return FALSE.  */
10746
10747
static bool
10748
elfcore_grok_aarch_fpmr (bfd *abfd, Elf_Internal_Note *note)
10749
0
{
10750
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_AARCH_FPMR, note);
10751
0
}
10752
10753
static bool
10754
elfcore_grok_arc_v2 (bfd *abfd, Elf_Internal_Note *note)
10755
0
{
10756
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_ARC_V2, note);
10757
0
}
10758
10759
/* Convert NOTE into a bfd_section called ".reg-riscv-csr".  Return TRUE if
10760
   successful otherwise, return FALSE.  */
10761
10762
static bool
10763
elfcore_grok_riscv_csr (bfd *abfd, Elf_Internal_Note *note)
10764
0
{
10765
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_RISCV_CSR, note);
10766
0
}
10767
10768
/* Convert NOTE into a bfd_section called ".gdb-tdesc".  Return TRUE if
10769
   successful otherwise, return FALSE.  */
10770
10771
static bool
10772
elfcore_grok_gdb_tdesc (bfd *abfd, Elf_Internal_Note *note)
10773
0
{
10774
0
  return elfcore_make_note_pseudosection (abfd, ".gdb-tdesc", note);
10775
0
}
10776
10777
static bool
10778
elfcore_grok_i386_tls (bfd *abfd, Elf_Internal_Note *note)
10779
0
{
10780
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_I386_TLS,
10781
0
            note);
10782
0
}
10783
10784
static bool
10785
elfcore_grok_loongarch_cpucfg (bfd *abfd, Elf_Internal_Note *note)
10786
0
{
10787
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_LOONGARCH_CPUCFG, note);
10788
0
}
10789
10790
static bool
10791
elfcore_grok_loongarch_lbt (bfd *abfd, Elf_Internal_Note *note)
10792
0
{
10793
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_LOONGARCH_LBT, note);
10794
0
}
10795
10796
static bool
10797
elfcore_grok_loongarch_lsx (bfd *abfd, Elf_Internal_Note *note)
10798
0
{
10799
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_LOONGARCH_LSX, note);
10800
0
}
10801
10802
static bool
10803
elfcore_grok_loongarch_lasx (bfd *abfd, Elf_Internal_Note *note)
10804
0
{
10805
0
  return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_LOONGARCH_LASX, note);
10806
0
}
10807
10808
#if defined (HAVE_PRPSINFO_T)
10809
typedef prpsinfo_t   elfcore_psinfo_t;
10810
#if defined (HAVE_PRPSINFO32_T)   /* Sparc64 cross Sparc32 */
10811
typedef prpsinfo32_t elfcore_psinfo32_t;
10812
#endif
10813
#endif
10814
10815
#if defined (HAVE_PSINFO_T)
10816
typedef psinfo_t   elfcore_psinfo_t;
10817
#if defined (HAVE_PSINFO32_T)   /* Sparc64 cross Sparc32 */
10818
typedef psinfo32_t elfcore_psinfo32_t;
10819
#endif
10820
#endif
10821
10822
/* return a malloc'ed copy of a string at START which is at
10823
   most MAX bytes long, possibly without a terminating '\0'.
10824
   the copy will always have a terminating '\0'.  */
10825
10826
char *
10827
_bfd_elfcore_strndup (bfd *abfd, char *start, size_t max)
10828
22
{
10829
22
  char *dups;
10830
22
  char *end = (char *) memchr (start, '\0', max);
10831
22
  size_t len;
10832
10833
22
  if (end == NULL)
10834
7
    len = max;
10835
15
  else
10836
15
    len = end - start;
10837
10838
22
  dups = bfd_alloc (abfd, len + 1);
10839
22
  if (dups == NULL)
10840
0
    return NULL;
10841
10842
22
  memcpy (dups, start, len);
10843
22
  dups[len] = '\0';
10844
10845
22
  return dups;
10846
22
}
10847
10848
#if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
10849
static bool
10850
elfcore_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
10851
264
{
10852
264
  if (note->descsz == sizeof (elfcore_psinfo_t))
10853
0
    {
10854
0
      elfcore_psinfo_t psinfo;
10855
10856
0
      memcpy (&psinfo, note->descdata, sizeof (psinfo));
10857
10858
0
#if defined (HAVE_PSINFO_T_PR_PID) || defined (HAVE_PRPSINFO_T_PR_PID)
10859
0
      elf_tdata (abfd)->core->pid = psinfo.pr_pid;
10860
0
#endif
10861
0
      elf_tdata (abfd)->core->program
10862
0
  = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
10863
0
        sizeof (psinfo.pr_fname));
10864
10865
0
      elf_tdata (abfd)->core->command
10866
0
  = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
10867
0
        sizeof (psinfo.pr_psargs));
10868
0
    }
10869
264
#if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
10870
264
  else if (note->descsz == sizeof (elfcore_psinfo32_t))
10871
11
    {
10872
      /* 64-bit host, 32-bit corefile */
10873
11
      elfcore_psinfo32_t psinfo;
10874
10875
11
      memcpy (&psinfo, note->descdata, sizeof (psinfo));
10876
10877
11
#if defined (HAVE_PSINFO32_T_PR_PID) || defined (HAVE_PRPSINFO32_T_PR_PID)
10878
11
      elf_tdata (abfd)->core->pid = psinfo.pr_pid;
10879
11
#endif
10880
11
      elf_tdata (abfd)->core->program
10881
11
  = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
10882
11
        sizeof (psinfo.pr_fname));
10883
10884
11
      elf_tdata (abfd)->core->command
10885
11
  = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
10886
11
        sizeof (psinfo.pr_psargs));
10887
11
    }
10888
253
#endif
10889
10890
253
  else
10891
253
    {
10892
      /* Fail - we don't know how to handle any other
10893
   note size (ie. data object type).  */
10894
253
      return true;
10895
253
    }
10896
10897
  /* Note that for some reason, a spurious space is tacked
10898
     onto the end of the args in some (at least one anyway)
10899
     implementations, so strip it off if it exists.  */
10900
10901
11
  {
10902
11
    char *command = elf_tdata (abfd)->core->command;
10903
11
    int n = strlen (command);
10904
10905
11
    if (0 < n && command[n - 1] == ' ')
10906
3
      command[n - 1] = '\0';
10907
11
  }
10908
10909
11
  return true;
10910
264
}
10911
#endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
10912
10913
#if defined (HAVE_PSTATUS_T)
10914
static bool
10915
elfcore_grok_pstatus (bfd *abfd, Elf_Internal_Note *note)
10916
{
10917
  if (note->descsz == sizeof (pstatus_t)
10918
#if defined (HAVE_PXSTATUS_T)
10919
      || note->descsz == sizeof (pxstatus_t)
10920
#endif
10921
      )
10922
    {
10923
      pstatus_t pstat;
10924
10925
      memcpy (&pstat, note->descdata, sizeof (pstat));
10926
10927
      elf_tdata (abfd)->core->pid = pstat.pr_pid;
10928
    }
10929
#if defined (HAVE_PSTATUS32_T)
10930
  else if (note->descsz == sizeof (pstatus32_t))
10931
    {
10932
      /* 64-bit host, 32-bit corefile */
10933
      pstatus32_t pstat;
10934
10935
      memcpy (&pstat, note->descdata, sizeof (pstat));
10936
10937
      elf_tdata (abfd)->core->pid = pstat.pr_pid;
10938
    }
10939
#endif
10940
  /* Could grab some more details from the "representative"
10941
     lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
10942
     NT_LWPSTATUS note, presumably.  */
10943
10944
  return true;
10945
}
10946
#endif /* defined (HAVE_PSTATUS_T) */
10947
10948
#if defined (HAVE_LWPSTATUS_T)
10949
static bool
10950
elfcore_grok_lwpstatus (bfd *abfd, Elf_Internal_Note *note)
10951
{
10952
  lwpstatus_t lwpstat;
10953
  char buf[100];
10954
  char *name;
10955
  size_t len;
10956
  asection *sect;
10957
10958
  if (note->descsz != sizeof (lwpstat)
10959
#if defined (HAVE_LWPXSTATUS_T)
10960
      && note->descsz != sizeof (lwpxstatus_t)
10961
#endif
10962
      )
10963
    return true;
10964
10965
  memcpy (&lwpstat, note->descdata, sizeof (lwpstat));
10966
10967
  elf_tdata (abfd)->core->lwpid = lwpstat.pr_lwpid;
10968
  /* Do not overwrite the core signal if it has already been set by
10969
     another thread.  */
10970
  if (elf_tdata (abfd)->core->signal == 0)
10971
    elf_tdata (abfd)->core->signal = lwpstat.pr_cursig;
10972
10973
  /* Make a ".reg/999" section.  */
10974
10975
  sprintf (buf, NOTE_PSEUDO_SECTION_REG "/%d", elfcore_make_pid (abfd));
10976
  len = strlen (buf) + 1;
10977
  name = bfd_alloc (abfd, len);
10978
  if (name == NULL)
10979
    return false;
10980
  memcpy (name, buf, len);
10981
10982
  sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
10983
  if (sect == NULL)
10984
    return false;
10985
10986
#if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
10987
  sect->size = sizeof (lwpstat.pr_context.uc_mcontext.gregs);
10988
  sect->filepos = note->descpos
10989
    + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs);
10990
#endif
10991
10992
#if defined (HAVE_LWPSTATUS_T_PR_REG)
10993
  sect->size = sizeof (lwpstat.pr_reg);
10994
  sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg);
10995
#endif
10996
10997
  sect->alignment_power = 2;
10998
10999
  if (!elfcore_maybe_make_sect (abfd, NOTE_PSEUDO_SECTION_REG, sect))
11000
    return false;
11001
11002
  /* Make a ".reg2/999" section */
11003
11004
  sprintf (buf, NOTE_PSEUDO_SECTION_REG2 "/%d", elfcore_make_pid (abfd));
11005
  len = strlen (buf) + 1;
11006
  name = bfd_alloc (abfd, len);
11007
  if (name == NULL)
11008
    return false;
11009
  memcpy (name, buf, len);
11010
11011
  sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
11012
  if (sect == NULL)
11013
    return false;
11014
11015
#if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
11016
  sect->size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs);
11017
  sect->filepos = note->descpos
11018
    + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs);
11019
#endif
11020
11021
#if defined (HAVE_LWPSTATUS_T_PR_FPREG)
11022
  sect->size = sizeof (lwpstat.pr_fpreg);
11023
  sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg);
11024
#endif
11025
11026
  sect->alignment_power = 2;
11027
11028
  return elfcore_maybe_make_sect (abfd, NOTE_PSEUDO_SECTION_REG2, sect);
11029
}
11030
#endif /* defined (HAVE_LWPSTATUS_T) */
11031
11032
/* These constants, and the structure offsets used below, are defined by
11033
   Cygwin's core_dump.h */
11034
0
#define NOTE_INFO_PROCESS  1
11035
0
#define NOTE_INFO_THREAD   2
11036
0
#define NOTE_INFO_MODULE   3
11037
0
#define NOTE_INFO_MODULE64 4
11038
11039
static bool
11040
elfcore_grok_win32pstatus (bfd *abfd, Elf_Internal_Note *note)
11041
38
{
11042
38
  char buf[30];
11043
38
  char *name;
11044
38
  size_t len;
11045
38
  unsigned int name_size;
11046
38
  asection *sect;
11047
38
  unsigned int type;
11048
38
  int is_active_thread;
11049
38
  bfd_vma base_addr;
11050
11051
38
  if (note->descsz < 4)
11052
14
    return true;
11053
11054
24
  if (! startswith (note->namedata, "win32"))
11055
24
    return true;
11056
11057
0
  type = bfd_get_32 (abfd, note->descdata);
11058
11059
0
  static const struct
11060
0
  {
11061
0
    const char *type_name;
11062
0
    unsigned long min_size;
11063
0
  } size_check[] =
11064
0
      {
11065
0
       { "NOTE_INFO_PROCESS", 12 },
11066
0
       { "NOTE_INFO_THREAD", 12 },
11067
0
       { "NOTE_INFO_MODULE", 12 },
11068
0
       { "NOTE_INFO_MODULE64", 16 },
11069
0
      };
11070
11071
0
  if (type == 0 || type > (sizeof(size_check)/sizeof(size_check[0])))
11072
0
      return true;
11073
11074
0
  if (note->descsz < size_check[type - 1].min_size)
11075
0
    {
11076
0
      _bfd_error_handler (_("%pB: warning: win32pstatus %s of size %lu bytes"
11077
0
          " is too small"),
11078
0
        abfd, size_check[type - 1].type_name, note->descsz);
11079
0
      return true;
11080
0
    }
11081
11082
0
  switch (type)
11083
0
    {
11084
0
    case NOTE_INFO_PROCESS:
11085
      /* FIXME: need to add ->core->command.  */
11086
0
      elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, note->descdata + 4);
11087
0
      elf_tdata (abfd)->core->signal = bfd_get_32 (abfd, note->descdata + 8);
11088
0
      break;
11089
11090
0
    case NOTE_INFO_THREAD:
11091
      /* Make a ".reg/<tid>" section containing the Win32 API thread CONTEXT
11092
   structure. */
11093
      /* thread_info.tid */
11094
0
      sprintf (buf, NOTE_PSEUDO_SECTION_REG "/%ld", (long) bfd_get_32 (abfd, note->descdata + 4));
11095
11096
0
      len = strlen (buf) + 1;
11097
0
      name = bfd_alloc (abfd, len);
11098
0
      if (name == NULL)
11099
0
  return false;
11100
11101
0
      memcpy (name, buf, len);
11102
11103
0
      sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
11104
0
      if (sect == NULL)
11105
0
  return false;
11106
11107
      /* sizeof (thread_info.thread_context) */
11108
0
      sect->size = note->descsz - 12;
11109
      /* offsetof (thread_info.thread_context) */
11110
0
      sect->filepos = note->descpos + 12;
11111
0
      sect->alignment_power = 2;
11112
11113
      /* thread_info.is_active_thread */
11114
0
      is_active_thread = bfd_get_32 (abfd, note->descdata + 8);
11115
11116
0
      if (is_active_thread)
11117
0
  if (! elfcore_maybe_make_sect (abfd, NOTE_PSEUDO_SECTION_REG, sect))
11118
0
    return false;
11119
0
      break;
11120
11121
0
    case NOTE_INFO_MODULE:
11122
0
    case NOTE_INFO_MODULE64:
11123
      /* Make a ".module/xxxxxxxx" section.  */
11124
0
      if (type == NOTE_INFO_MODULE)
11125
0
  {
11126
    /* module_info.base_address */
11127
0
    base_addr = bfd_get_32 (abfd, note->descdata + 4);
11128
0
    sprintf (buf, ".module/%08lx", (unsigned long) base_addr);
11129
    /* module_info.module_name_size */
11130
0
    name_size = bfd_get_32 (abfd, note->descdata + 8);
11131
0
  }
11132
0
      else /* NOTE_INFO_MODULE64 */
11133
0
  {
11134
    /* module_info.base_address */
11135
0
    base_addr = bfd_get_64 (abfd, note->descdata + 4);
11136
0
    sprintf (buf, ".module/%016lx", (unsigned long) base_addr);
11137
    /* module_info.module_name_size */
11138
0
    name_size = bfd_get_32 (abfd, note->descdata + 12);
11139
0
  }
11140
11141
0
      len = strlen (buf) + 1;
11142
0
      name = bfd_alloc (abfd, len);
11143
0
      if (name == NULL)
11144
0
  return false;
11145
11146
0
      memcpy (name, buf, len);
11147
11148
0
      sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
11149
11150
0
      if (sect == NULL)
11151
0
  return false;
11152
11153
0
      if (note->descsz < 12 + name_size)
11154
0
  {
11155
0
    _bfd_error_handler (_("%pB: win32pstatus NOTE_INFO_MODULE of size %lu"
11156
0
        " is too small to contain a name of size %u"),
11157
0
            abfd, note->descsz, name_size);
11158
0
    return true;
11159
0
  }
11160
11161
0
      sect->size = note->descsz;
11162
0
      sect->filepos = note->descpos;
11163
0
      sect->alignment_power = 2;
11164
0
      break;
11165
11166
0
    default:
11167
0
      return true;
11168
0
    }
11169
11170
0
  return true;
11171
0
}
11172
11173
static bool
11174
elfcore_grok_note (bfd *abfd, Elf_Internal_Note *note)
11175
3.84k
{
11176
3.84k
  elf_backend_data *bed = get_elf_backend_data (abfd);
11177
11178
  /* Short cut for LINUX notes.  */
11179
3.84k
  if (note->namesz == 6 /* strlen (NOTE_NAME_LINUX) + 1 */
11180
66
      && streq (note->namedata, NOTE_NAME_LINUX))
11181
0
    {
11182
0
      switch (note->type)
11183
0
  {
11184
0
  case NT_386_TLS: return elfcore_grok_i386_tls (abfd, note);
11185
0
  case NT_ARC_V2:   return elfcore_grok_arc_v2 (abfd, note);
11186
0
  case NT_ARM_FPMR: return elfcore_grok_aarch_fpmr (abfd, note);
11187
0
  case NT_ARM_GCS: return elfcore_grok_aarch_gcs (abfd, note);
11188
0
  case NT_ARM_POE: return elfcore_grok_aarch_poe (abfd, note);
11189
0
  case NT_ARM_HW_BREAK: return elfcore_grok_aarch_hw_break (abfd, note);
11190
0
  case NT_ARM_HW_WATCH: return elfcore_grok_aarch_hw_watch (abfd, note);
11191
0
  case NT_ARM_PAC_MASK: return elfcore_grok_aarch_pauth (abfd, note);
11192
0
  case NT_ARM_SSVE: return elfcore_grok_aarch_ssve (abfd, note);
11193
0
  case NT_ARM_SVE: return elfcore_grok_aarch_sve (abfd, note);
11194
0
  case NT_ARM_TAGGED_ADDR_CTRL: return elfcore_grok_aarch_mte (abfd, note);
11195
0
  case NT_ARM_TLS: return elfcore_grok_aarch_tls (abfd, note);
11196
0
  case NT_ARM_VFP: return elfcore_grok_arm_vfp (abfd, note);
11197
0
  case NT_ARM_ZA:   return elfcore_grok_aarch_za (abfd, note);
11198
0
  case NT_ARM_ZT:   return elfcore_grok_aarch_zt (abfd, note);
11199
0
  case NT_LARCH_CPUCFG: return elfcore_grok_loongarch_cpucfg (abfd, note);
11200
0
  case NT_LARCH_LASX: return elfcore_grok_loongarch_lasx (abfd, note);
11201
0
  case NT_LARCH_LBT: return elfcore_grok_loongarch_lbt (abfd, note);
11202
0
  case NT_LARCH_LSX: return elfcore_grok_loongarch_lsx (abfd, note);
11203
0
  case NT_PPC_DMR: return elfcore_grok_ppc_dmr (abfd, note);
11204
0
  case NT_PPC_DSCR: return elfcore_grok_ppc_dscr (abfd, note);
11205
0
  case NT_PPC_EBB: return elfcore_grok_ppc_ebb (abfd, note);
11206
0
  case NT_PPC_PMU: return elfcore_grok_ppc_pmu (abfd, note);
11207
0
  case NT_PPC_PPR: return elfcore_grok_ppc_ppr (abfd, note);
11208
0
  case NT_PPC_TAR: return elfcore_grok_ppc_tar (abfd, note);
11209
0
  case NT_PPC_TM_CDSCR: return elfcore_grok_ppc_tm_cdscr (abfd, note);
11210
0
  case NT_PPC_TM_CFPR: return elfcore_grok_ppc_tm_cfpr (abfd, note);
11211
0
  case NT_PPC_TM_CGPR: return elfcore_grok_ppc_tm_cgpr (abfd, note);
11212
0
  case NT_PPC_TM_CPPR: return elfcore_grok_ppc_tm_cppr (abfd, note);
11213
0
  case NT_PPC_TM_CTAR: return elfcore_grok_ppc_tm_ctar (abfd, note);
11214
0
  case NT_PPC_TM_CVMX: return elfcore_grok_ppc_tm_cvmx (abfd, note);
11215
0
  case NT_PPC_TM_CVSX: return elfcore_grok_ppc_tm_cvsx (abfd, note);
11216
0
  case NT_PPC_TM_SPR: return elfcore_grok_ppc_tm_spr (abfd, note);
11217
0
  case NT_PPC_VMX: return elfcore_grok_ppc_vmx (abfd, note);
11218
0
  case NT_PPC_VSX: return elfcore_grok_ppc_vsx (abfd, note);
11219
0
  case NT_PRXFPREG: return elfcore_grok_prxfpreg (abfd, note);
11220
0
  case NT_S390_CTRS: return elfcore_grok_s390_ctrs (abfd, note);
11221
0
  case NT_S390_GS_BC: return elfcore_grok_s390_gs_bc (abfd, note);
11222
0
  case NT_S390_GS_CB: return elfcore_grok_s390_gs_cb (abfd, note);
11223
0
  case NT_S390_HIGH_GPRS: return elfcore_grok_s390_high_gprs (abfd, note);
11224
0
  case NT_S390_LAST_BREAK:  return elfcore_grok_s390_last_break (abfd, note);
11225
0
  case NT_S390_PREFIX: return elfcore_grok_s390_prefix (abfd, note);
11226
0
  case NT_S390_SYSTEM_CALL: return elfcore_grok_s390_system_call (abfd, note);
11227
0
  case NT_S390_TDB: return elfcore_grok_s390_tdb (abfd, note);
11228
0
  case NT_S390_TIMER: return elfcore_grok_s390_timer (abfd, note);
11229
0
  case NT_S390_TODCMP: return elfcore_grok_s390_todcmp (abfd, note);
11230
0
  case NT_S390_TODPREG: return elfcore_grok_s390_todpreg (abfd, note);
11231
0
  case NT_S390_VXRS_HIGH: return elfcore_grok_s390_vxrs_high (abfd, note);
11232
0
  case NT_S390_VXRS_LOW: return elfcore_grok_s390_vxrs_low (abfd, note);
11233
0
  case NT_X86_SHSTK: return elfcore_grok_sspreg (abfd, note);
11234
0
  case NT_X86_XSTATE: return elfcore_grok_xstatereg (abfd, note);
11235
0
  case NT_X86_XSAVE_LAYOUT: return elfcore_grok_xsave_layout_desc (abfd, note);
11236
0
  default: break;
11237
0
  }
11238
0
    }
11239
  
11240
3.84k
  switch (note->type)
11241
3.84k
    {
11242
2.65k
    default:
11243
2.65k
      return true;
11244
11245
467
    case NT_PRSTATUS:
11246
467
      if (bed->elf_backend_grok_prstatus)
11247
293
  if ((*bed->elf_backend_grok_prstatus) (abfd, note))
11248
0
    return true;
11249
467
#if defined (HAVE_PRSTATUS_T)
11250
467
      return elfcore_grok_prstatus (abfd, note);
11251
#else
11252
      return true;
11253
#endif
11254
11255
#if defined (HAVE_PSTATUS_T)
11256
    case NT_PSTATUS:
11257
      return elfcore_grok_pstatus (abfd, note);
11258
#endif
11259
11260
#if defined (HAVE_LWPSTATUS_T)
11261
    case NT_LWPSTATUS:
11262
      return elfcore_grok_lwpstatus (abfd, note);
11263
#endif
11264
11265
226
    case NT_FPREGSET:   /* FIXME: rename to NT_PRFPREG.  */
11266
226
      return elfcore_grok_prfpreg (abfd, note);
11267
11268
38
    case NT_WIN32PSTATUS:
11269
38
      return elfcore_grok_win32pstatus (abfd, note);
11270
11271
54
    case NT_GDB_TDESC:
11272
54
      if (note->namesz == 4 && streq (note->namedata, NOTE_NAME_GDB))
11273
0
  return elfcore_grok_gdb_tdesc (abfd, note);
11274
54
      else
11275
54
  return true;
11276
11277
53
    case NT_RISCV_CSR:
11278
53
      if (note->namesz == 4 && streq (note->namedata, NOTE_NAME_GDB))
11279
0
  return elfcore_grok_riscv_csr (abfd, note);
11280
53
      else
11281
53
  return true;
11282
11283
240
    case NT_PRPSINFO:
11284
264
    case NT_PSINFO:
11285
264
      if (bed->elf_backend_grok_psinfo)
11286
153
  if ((*bed->elf_backend_grok_psinfo) (abfd, note))
11287
0
    return true;
11288
264
#if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
11289
264
      return elfcore_grok_psinfo (abfd, note);
11290
#else
11291
      return true;
11292
#endif
11293
11294
86
    case NT_AUXV:
11295
86
      return elfcore_make_auxv_note_section (abfd, note, 0);
11296
11297
0
    case NT_FILE:
11298
0
      return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.file",
11299
0
                note);
11300
11301
0
    case NT_SIGINFO:
11302
0
      return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.siginfo",
11303
0
                note);
11304
3.84k
    }
11305
3.84k
}
11306
11307
static bool
11308
elfobj_grok_gnu_build_id (bfd *abfd, Elf_Internal_Note *note)
11309
458
{
11310
458
  struct bfd_build_id* build_id;
11311
11312
458
  if (note->descsz == 0)
11313
48
    return false;
11314
11315
410
  build_id = bfd_alloc (abfd, sizeof (struct bfd_build_id) - 1 + note->descsz);
11316
410
  if (build_id == NULL)
11317
0
    return false;
11318
11319
410
  build_id->size = note->descsz;
11320
410
  memcpy (build_id->data, note->descdata, note->descsz);
11321
410
  abfd->build_id = build_id;
11322
11323
410
  return true;
11324
410
}
11325
11326
static bool
11327
elfobj_grok_gnu_note (bfd *abfd, Elf_Internal_Note *note)
11328
1.78k
{
11329
1.78k
  switch (note->type)
11330
1.78k
    {
11331
430
    default:
11332
430
      return true;
11333
11334
898
    case NT_GNU_PROPERTY_TYPE_0:
11335
898
      return _bfd_elf_parse_gnu_properties (abfd, note);
11336
11337
458
    case NT_GNU_BUILD_ID:
11338
458
      return elfobj_grok_gnu_build_id (abfd, note);
11339
1.78k
    }
11340
1.78k
}
11341
11342
static bool
11343
elfobj_grok_stapsdt_note_1 (bfd *abfd, Elf_Internal_Note *note)
11344
0
{
11345
0
  struct sdt_note *cur = bfd_alloc (abfd, sizeof (*cur) + note->descsz);
11346
11347
0
  if (cur == NULL)
11348
0
    return false;
11349
0
  cur->next = (struct sdt_note *) (elf_tdata (abfd))->sdt_note_head;
11350
0
  cur->size = note->descsz;
11351
0
  memcpy (cur->data, note->descdata, note->descsz);
11352
11353
0
  elf_tdata (abfd)->sdt_note_head = cur;
11354
11355
0
  return true;
11356
0
}
11357
11358
static bool
11359
elfobj_grok_stapsdt_note (bfd *abfd, Elf_Internal_Note *note)
11360
4
{
11361
4
  switch (note->type)
11362
4
    {
11363
0
    case NT_STAPSDT:
11364
0
      return elfobj_grok_stapsdt_note_1 (abfd, note);
11365
11366
4
    default:
11367
4
      return true;
11368
4
    }
11369
4
}
11370
11371
static bool
11372
elfcore_grok_freebsd_psinfo (bfd *abfd, Elf_Internal_Note *note)
11373
4
{
11374
4
  size_t offset;
11375
11376
4
  switch (elf_elfheader (abfd)->e_ident[EI_CLASS])
11377
4
    {
11378
0
    case ELFCLASS32:
11379
0
      if (note->descsz < 108)
11380
0
  return false;
11381
0
      break;
11382
11383
4
    case ELFCLASS64:
11384
4
      if (note->descsz < 120)
11385
1
  return false;
11386
3
      break;
11387
11388
3
    default:
11389
0
      return false;
11390
4
    }
11391
11392
  /* Check for version 1 in pr_version.  */
11393
3
  if (bfd_h_get_32 (abfd, (bfd_byte *) note->descdata) != 1)
11394
3
    return false;
11395
11396
0
  offset = 4;
11397
11398
  /* Skip over pr_psinfosz. */
11399
0
  if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS32)
11400
0
    offset += 4;
11401
0
  else
11402
0
    {
11403
0
      offset += 4;  /* Padding before pr_psinfosz. */
11404
0
      offset += 8;
11405
0
    }
11406
11407
  /* pr_fname is PRFNAMESZ (16) + 1 bytes in size.  */
11408
0
  elf_tdata (abfd)->core->program
11409
0
    = _bfd_elfcore_strndup (abfd, note->descdata + offset, 17);
11410
0
  offset += 17;
11411
11412
  /* pr_psargs is PRARGSZ (80) + 1 bytes in size.  */
11413
0
  elf_tdata (abfd)->core->command
11414
0
    = _bfd_elfcore_strndup (abfd, note->descdata + offset, 81);
11415
0
  offset += 81;
11416
11417
  /* Padding before pr_pid.  */
11418
0
  offset += 2;
11419
11420
  /* The pr_pid field was added in version "1a".  */
11421
0
  if (note->descsz < offset + 4)
11422
0
    return true;
11423
11424
0
  elf_tdata (abfd)->core->pid
11425
0
    = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
11426
11427
0
  return true;
11428
0
}
11429
11430
static bool
11431
elfcore_grok_freebsd_prstatus (bfd *abfd, Elf_Internal_Note *note)
11432
1
{
11433
1
  size_t offset;
11434
1
  size_t size;
11435
1
  size_t min_size;
11436
11437
  /* Compute offset of pr_getregsz, skipping over pr_statussz.
11438
     Also compute minimum size of this note.  */
11439
1
  switch (elf_elfheader (abfd)->e_ident[EI_CLASS])
11440
1
    {
11441
0
    case ELFCLASS32:
11442
0
      offset = 4 + 4;
11443
0
      min_size = offset + (4 * 2) + 4 + 4 + 4;
11444
0
      break;
11445
11446
1
    case ELFCLASS64:
11447
1
      offset = 4 + 4 + 8; /* Includes padding before pr_statussz.  */
11448
1
      min_size = offset + (8 * 2) + 4 + 4 + 4 + 4;
11449
1
      break;
11450
11451
0
    default:
11452
0
      return false;
11453
1
    }
11454
11455
1
  if (note->descsz < min_size)
11456
1
    return false;
11457
11458
  /* Check for version 1 in pr_version.  */
11459
0
  if (bfd_h_get_32 (abfd, (bfd_byte *) note->descdata) != 1)
11460
0
    return false;
11461
11462
  /* Extract size of pr_reg from pr_gregsetsz.  */
11463
  /* Skip over pr_gregsetsz and pr_fpregsetsz.  */
11464
0
  if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS32)
11465
0
    {
11466
0
      size = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
11467
0
      offset += 4 * 2;
11468
0
    }
11469
0
  else
11470
0
    {
11471
0
      size = bfd_h_get_64 (abfd, (bfd_byte *) note->descdata + offset);
11472
0
      offset += 8 * 2;
11473
0
    }
11474
11475
  /* Skip over pr_osreldate.  */
11476
0
  offset += 4;
11477
11478
  /* Read signal from pr_cursig.  */
11479
0
  if (elf_tdata (abfd)->core->signal == 0)
11480
0
    elf_tdata (abfd)->core->signal
11481
0
      = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
11482
0
  offset += 4;
11483
11484
  /* Read TID from pr_pid.  */
11485
0
  elf_tdata (abfd)->core->lwpid
11486
0
      = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset);
11487
0
  offset += 4;
11488
11489
  /* Padding before pr_reg.  */
11490
0
  if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS64)
11491
0
    offset += 4;
11492
11493
  /* Make sure that there is enough data remaining in the note.  */
11494
0
  if ((note->descsz - offset) < size)
11495
0
    return false;
11496
11497
  /* Make a ".reg/999" section and a ".reg" section.  */
11498
0
  return _bfd_elfcore_make_pseudosection (abfd, NOTE_PSEUDO_SECTION_REG,
11499
0
            size, note->descpos + offset);
11500
0
}
11501
11502
static bool
11503
elfcore_grok_freebsd_note (bfd *abfd, Elf_Internal_Note *note)
11504
29
{
11505
29
  elf_backend_data *bed = get_elf_backend_data (abfd);
11506
11507
29
  switch (note->type)
11508
29
    {
11509
1
    case NT_PRSTATUS:
11510
1
      if (bed->elf_backend_grok_freebsd_prstatus)
11511
0
  if ((*bed->elf_backend_grok_freebsd_prstatus) (abfd, note))
11512
0
    return true;
11513
1
      return elfcore_grok_freebsd_prstatus (abfd, note);
11514
11515
7
    case NT_FPREGSET:
11516
7
      return elfcore_grok_prfpreg (abfd, note);
11517
11518
4
    case NT_PRPSINFO:
11519
4
      return elfcore_grok_freebsd_psinfo (abfd, note);
11520
11521
0
    case NT_FREEBSD_THRMISC:
11522
0
      return elfcore_make_note_pseudosection (abfd, ".thrmisc", note);
11523
11524
0
    case NT_FREEBSD_PROCSTAT_PROC:
11525
0
      return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.proc",
11526
0
                note);
11527
11528
0
    case NT_FREEBSD_PROCSTAT_FILES:
11529
0
      return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.files",
11530
0
                note);
11531
11532
0
    case NT_FREEBSD_PROCSTAT_VMMAP:
11533
0
      return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.vmmap",
11534
0
                note);
11535
11536
0
    case NT_FREEBSD_PROCSTAT_AUXV:
11537
0
      return elfcore_make_auxv_note_section (abfd, note, 4);
11538
11539
0
    case NT_FREEBSD_X86_SEGBASES:
11540
0
      return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_X86_SEGBASES, note);
11541
11542
0
    case NT_X86_XSTATE:
11543
0
      return elfcore_grok_xstatereg (abfd, note);
11544
11545
0
    case NT_FREEBSD_PTLWPINFO:
11546
0
      return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.lwpinfo",
11547
0
                note);
11548
11549
0
    case NT_ARM_TLS:
11550
0
      return elfcore_grok_aarch_tls (abfd, note);
11551
11552
0
    case NT_ARM_VFP:
11553
0
      return elfcore_grok_arm_vfp (abfd, note);
11554
11555
17
    default:
11556
17
      return true;
11557
29
    }
11558
29
}
11559
11560
static bool
11561
elfcore_netbsd_get_lwpid (Elf_Internal_Note *note, int *lwpidp)
11562
12
{
11563
12
  char *cp;
11564
11565
12
  cp = strchr (note->namedata, '@');
11566
12
  if (cp != NULL)
11567
0
    {
11568
0
      *lwpidp = atoi(cp + 1);
11569
0
      return true;
11570
0
    }
11571
12
  return false;
11572
12
}
11573
11574
static bool
11575
elfcore_grok_netbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
11576
0
{
11577
0
  if (note->descsz <= 0x7c + 31)
11578
0
    return false;
11579
11580
  /* Signal number at offset 0x08. */
11581
0
  elf_tdata (abfd)->core->signal
11582
0
    = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
11583
11584
  /* Process ID at offset 0x50. */
11585
0
  elf_tdata (abfd)->core->pid
11586
0
    = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x50);
11587
11588
  /* Command name at 0x7c (max 32 bytes, including nul). */
11589
0
  elf_tdata (abfd)->core->command
11590
0
    = _bfd_elfcore_strndup (abfd, note->descdata + 0x7c, 31);
11591
11592
0
  return elfcore_make_note_pseudosection (abfd, ".note.netbsdcore.procinfo",
11593
0
            note);
11594
0
}
11595
11596
static bool
11597
elfcore_grok_netbsd_note (bfd *abfd, Elf_Internal_Note *note)
11598
12
{
11599
12
  int lwp;
11600
11601
12
  if (elfcore_netbsd_get_lwpid (note, &lwp))
11602
0
    elf_tdata (abfd)->core->lwpid = lwp;
11603
11604
12
  switch (note->type)
11605
12
    {
11606
0
    case NT_NETBSDCORE_PROCINFO:
11607
      /* NetBSD-specific core "procinfo".  Note that we expect to
11608
   find this note before any of the others, which is fine,
11609
   since the kernel writes this note out first when it
11610
   creates a core file.  */
11611
0
      return elfcore_grok_netbsd_procinfo (abfd, note);
11612
0
    case NT_NETBSDCORE_AUXV:
11613
      /* NetBSD-specific Elf Auxiliary Vector data. */
11614
0
      return elfcore_make_auxv_note_section (abfd, note, 4);
11615
0
    case NT_NETBSDCORE_LWPSTATUS:
11616
0
      return elfcore_make_note_pseudosection (abfd,
11617
0
                ".note.netbsdcore.lwpstatus",
11618
0
                note);
11619
12
    default:
11620
12
      break;
11621
12
    }
11622
11623
  /* As of March 2020 there are no other machine-independent notes
11624
     defined for NetBSD core files.  If the note type is less
11625
     than the start of the machine-dependent note types, we don't
11626
     understand it.  */
11627
11628
12
  if (note->type < NT_NETBSDCORE_FIRSTMACH)
11629
0
    return true;
11630
11631
11632
12
  switch (bfd_get_arch (abfd))
11633
12
    {
11634
      /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
11635
   PT_GETFPREGS == mach+2.  */
11636
11637
0
    case bfd_arch_aarch64:
11638
0
    case bfd_arch_alpha:
11639
0
    case bfd_arch_sparc:
11640
0
      switch (note->type)
11641
0
  {
11642
0
  case NT_NETBSDCORE_FIRSTMACH+0:
11643
0
    return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_REG, note);
11644
11645
0
  case NT_NETBSDCORE_FIRSTMACH+2:
11646
0
    return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_REG2, note);
11647
11648
0
  default:
11649
0
    return true;
11650
0
  }
11651
11652
      /* On SuperH, PT_GETREGS == mach+3 and PT_GETFPREGS == mach+5.
11653
   There's also old PT___GETREGS40 == mach + 1 for old reg
11654
   structure which lacks GBR.  */
11655
11656
0
    case bfd_arch_sh:
11657
0
      switch (note->type)
11658
0
  {
11659
0
  case NT_NETBSDCORE_FIRSTMACH+3:
11660
0
    return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_REG, note);
11661
11662
0
  case NT_NETBSDCORE_FIRSTMACH+5:
11663
0
    return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_REG2, note);
11664
11665
0
  default:
11666
0
    return true;
11667
0
  }
11668
11669
      /* On all other arch's, PT_GETREGS == mach+1 and
11670
   PT_GETFPREGS == mach+3.  */
11671
11672
12
    default:
11673
12
      switch (note->type)
11674
12
  {
11675
0
  case NT_NETBSDCORE_FIRSTMACH+1:
11676
0
    return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_REG, note);
11677
11678
0
  case NT_NETBSDCORE_FIRSTMACH+3:
11679
0
    return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_REG2, note);
11680
11681
12
  default:
11682
12
    return true;
11683
12
  }
11684
12
    }
11685
    /* NOTREACHED */
11686
12
}
11687
11688
static bool
11689
elfcore_grok_openbsd_procinfo (bfd *abfd, Elf_Internal_Note *note)
11690
0
{
11691
0
  if (note->descsz <= 0x48 + 31)
11692
0
    return false;
11693
11694
  /* Signal number at offset 0x08. */
11695
0
  elf_tdata (abfd)->core->signal
11696
0
    = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
11697
11698
  /* Process ID at offset 0x20. */
11699
0
  elf_tdata (abfd)->core->pid
11700
0
    = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x20);
11701
11702
  /* Command name at 0x48 (max 32 bytes, including nul). */
11703
0
  elf_tdata (abfd)->core->command
11704
0
    = _bfd_elfcore_strndup (abfd, note->descdata + 0x48, 31);
11705
11706
0
  return true;
11707
0
}
11708
11709
/* Processes Solaris's process status note.
11710
   sig_off ~ offsetof(prstatus_t, pr_cursig)
11711
   pid_off ~ offsetof(prstatus_t, pr_pid)
11712
   lwpid_off ~ offsetof(prstatus_t, pr_who)
11713
   gregset_size ~ sizeof(gregset_t)
11714
   gregset_offset ~ offsetof(prstatus_t, pr_reg)  */
11715
11716
static bool
11717
elfcore_grok_solaris_prstatus (bfd *abfd, Elf_Internal_Note* note, int sig_off,
11718
             int pid_off, int lwpid_off, size_t gregset_size,
11719
             size_t gregset_offset)
11720
0
{
11721
0
  asection *sect = NULL;
11722
0
  elf_tdata (abfd)->core->signal
11723
0
    = bfd_get_16 (abfd, note->descdata + sig_off);
11724
0
  elf_tdata (abfd)->core->pid
11725
0
    = bfd_get_32 (abfd, note->descdata + pid_off);
11726
0
  elf_tdata (abfd)->core->lwpid
11727
0
    = bfd_get_32 (abfd, note->descdata + lwpid_off);
11728
11729
0
  sect = bfd_get_section_by_name (abfd, NOTE_PSEUDO_SECTION_REG);
11730
0
  if (sect != NULL)
11731
0
    sect->size = gregset_size;
11732
11733
0
  return _bfd_elfcore_make_pseudosection (abfd, NOTE_PSEUDO_SECTION_REG, gregset_size,
11734
0
            note->descpos + gregset_offset);
11735
0
}
11736
11737
/* Gets program and arguments from a core.
11738
   prog_off ~ offsetof(prpsinfo | psinfo_t, pr_fname)
11739
   comm_off ~ offsetof(prpsinfo | psinfo_t, pr_psargs)  */
11740
11741
static bool
11742
elfcore_grok_solaris_info(bfd *abfd, Elf_Internal_Note* note,
11743
        int prog_off, int comm_off)
11744
0
{
11745
0
  elf_tdata (abfd)->core->program
11746
0
    = _bfd_elfcore_strndup (abfd, note->descdata + prog_off, 16);
11747
0
  elf_tdata (abfd)->core->command
11748
0
    = _bfd_elfcore_strndup (abfd, note->descdata + comm_off, 80);
11749
11750
0
  return true;
11751
0
}
11752
11753
/* Processes Solaris's LWP status note.
11754
   gregset_size ~ sizeof(gregset_t)
11755
   gregset_off ~ offsetof(lwpstatus_t, pr_reg)
11756
   fpregset_size ~ sizeof(fpregset_t)
11757
   fpregset_off ~ offsetof(lwpstatus_t, pr_fpreg)  */
11758
11759
static bool
11760
elfcore_grok_solaris_lwpstatus (bfd *abfd, Elf_Internal_Note* note,
11761
        size_t gregset_size, int gregset_off,
11762
        size_t fpregset_size, int fpregset_off)
11763
0
{
11764
0
  asection *sect = NULL;
11765
0
  char reg2_section_name[16] = { 0 };
11766
11767
0
  (void) snprintf (reg2_section_name, 16, "%s/%i", NOTE_PSEUDO_SECTION_REG2,
11768
0
       elf_tdata (abfd)->core->lwpid);
11769
11770
  /* offsetof(lwpstatus_t, pr_lwpid) */
11771
0
  elf_tdata (abfd)->core->lwpid
11772
0
    = bfd_get_32 (abfd, note->descdata + 4);
11773
  /* offsetof(lwpstatus_t, pr_cursig) */
11774
0
  elf_tdata (abfd)->core->signal
11775
0
    = bfd_get_16 (abfd, note->descdata + 12);
11776
11777
0
  sect = bfd_get_section_by_name (abfd, NOTE_PSEUDO_SECTION_REG);
11778
0
  if (sect != NULL)
11779
0
    sect->size = gregset_size;
11780
0
  else if (!_bfd_elfcore_make_pseudosection (abfd, NOTE_PSEUDO_SECTION_REG, gregset_size,
11781
0
               note->descpos + gregset_off))
11782
0
    return false;
11783
11784
0
  sect = bfd_get_section_by_name (abfd, reg2_section_name);
11785
0
  if (sect != NULL)
11786
0
    {
11787
0
      sect->size = fpregset_size;
11788
0
      sect->filepos = note->descpos + fpregset_off;
11789
0
      sect->alignment_power = 2;
11790
0
    }
11791
0
  else if (!_bfd_elfcore_make_pseudosection (abfd, NOTE_PSEUDO_SECTION_REG2, fpregset_size,
11792
0
               note->descpos + fpregset_off))
11793
0
    return false;
11794
11795
0
  return true;
11796
0
}
11797
11798
static bool
11799
elfcore_grok_solaris_note_impl (bfd *abfd, Elf_Internal_Note *note)
11800
45
{
11801
45
  if (note == NULL)
11802
0
    return false;
11803
11804
  /* core files are identified as 32- or 64-bit, SPARC or x86,
11805
     by the size of the descsz which matches the sizeof()
11806
     the type appropriate for that note type (e.g., prstatus_t for
11807
     SOLARIS_NT_PRSTATUS) for the corresponding architecture
11808
     on Solaris. The core file bitness may differ from the bitness of
11809
     gdb itself, so fixed values are used instead of sizeof().
11810
     Appropriate fixed offsets are also used to obtain data from
11811
     the note.  */
11812
11813
45
  switch ((int) note->type)
11814
45
    {
11815
23
    case SOLARIS_NT_PRSTATUS:
11816
23
      switch (note->descsz)
11817
23
  {
11818
0
  case 508: /* sizeof(prstatus_t) SPARC 32-bit */
11819
0
    return elfcore_grok_solaris_prstatus(abfd, note,
11820
0
                 136, 216, 308, 152, 356);
11821
0
  case 904: /* sizeof(prstatus_t) SPARC 64-bit */
11822
0
    return elfcore_grok_solaris_prstatus(abfd, note,
11823
0
                 264, 360, 520, 304, 600);
11824
0
  case 432: /* sizeof(prstatus_t) Intel 32-bit */
11825
0
    return elfcore_grok_solaris_prstatus(abfd, note,
11826
0
                 136, 216, 308, 76, 356);
11827
0
  case 824: /* sizeof(prstatus_t) Intel 64-bit */
11828
0
    return elfcore_grok_solaris_prstatus(abfd, note,
11829
0
                 264, 360, 520, 224, 600);
11830
23
  default:
11831
23
    return true;
11832
23
  }
11833
11834
0
    case SOLARIS_NT_PSINFO:
11835
7
    case SOLARIS_NT_PRPSINFO:
11836
7
      switch (note->descsz)
11837
7
  {
11838
0
  case 260: /* sizeof(prpsinfo_t) SPARC and Intel 32-bit */
11839
0
    return elfcore_grok_solaris_info(abfd, note, 84, 100);
11840
0
  case 328: /* sizeof(prpsinfo_t) SPARC and Intel 64-bit */
11841
0
    return elfcore_grok_solaris_info(abfd, note, 120, 136);
11842
0
  case 360: /* sizeof(psinfo_t) SPARC and Intel 32-bit */
11843
0
    return elfcore_grok_solaris_info(abfd, note, 88, 104);
11844
0
  case 440: /* sizeof(psinfo_t) SPARC and Intel 64-bit */
11845
0
    return elfcore_grok_solaris_info(abfd, note, 136, 152);
11846
7
  default:
11847
7
    return true;
11848
7
  }
11849
11850
0
    case SOLARIS_NT_LWPSTATUS:
11851
0
      switch (note->descsz)
11852
0
  {
11853
0
  case 896: /* sizeof(lwpstatus_t) SPARC 32-bit */
11854
0
    return elfcore_grok_solaris_lwpstatus(abfd, note,
11855
0
            152, 344, 400, 496);
11856
0
  case 1392: /* sizeof(lwpstatus_t) SPARC 64-bit */
11857
0
    return elfcore_grok_solaris_lwpstatus(abfd, note,
11858
0
            304, 544, 544, 848);
11859
0
  case 800: /* sizeof(lwpstatus_t) Intel 32-bit */
11860
0
    return elfcore_grok_solaris_lwpstatus(abfd, note,
11861
0
            76, 344, 380, 420);
11862
0
  case 1296: /* sizeof(lwpstatus_t) Intel 64-bit */
11863
0
    return elfcore_grok_solaris_lwpstatus(abfd, note,
11864
0
            224, 544, 528, 768);
11865
0
  default:
11866
0
    return true;
11867
0
  }
11868
11869
0
    case SOLARIS_NT_LWPSINFO:
11870
      /* sizeof(lwpsinfo_t) on 32- and 64-bit, respectively */
11871
0
      if (note->descsz == 128 || note->descsz == 152)
11872
0
  elf_tdata (abfd)->core->lwpid =
11873
0
    bfd_get_32 (abfd, note->descdata + 4);
11874
0
      break;
11875
11876
15
    default:
11877
15
      break;
11878
45
    }
11879
11880
15
  return true;
11881
45
}
11882
11883
/* For name starting with "CORE" this may be either a Solaris
11884
   core file or a gdb-generated core file.  Do Solaris-specific
11885
   processing on selected note types first with
11886
   elfcore_grok_solaris_note(), then process the note
11887
   in elfcore_grok_note().  */
11888
11889
static bool
11890
elfcore_grok_solaris_note (bfd *abfd, Elf_Internal_Note *note)
11891
45
{
11892
45
  if (!elfcore_grok_solaris_note_impl (abfd, note))
11893
0
    return false;
11894
11895
45
  return elfcore_grok_note (abfd, note);
11896
45
}
11897
11898
static bool
11899
elfcore_grok_openbsd_note (bfd *abfd, Elf_Internal_Note *note)
11900
1
{
11901
1
  if (note->type == NT_OPENBSD_PROCINFO)
11902
0
    return elfcore_grok_openbsd_procinfo (abfd, note);
11903
11904
1
  if (note->type == NT_OPENBSD_REGS)
11905
0
    return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_REG, note);
11906
11907
1
  if (note->type == NT_OPENBSD_FPREGS)
11908
0
    return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_REG2, note);
11909
11910
1
  if (note->type == NT_OPENBSD_XFPREGS)
11911
0
    return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_XFP, note);
11912
11913
1
  if (note->type == NT_OPENBSD_AUXV)
11914
0
    return elfcore_make_auxv_note_section (abfd, note, 0);
11915
11916
1
  if (note->type == NT_OPENBSD_WCOOKIE)
11917
0
    {
11918
0
      asection *sect = bfd_make_section_anyway_with_flags (abfd, ".wcookie",
11919
0
                 SEC_HAS_CONTENTS);
11920
11921
0
      if (sect == NULL)
11922
0
  return false;
11923
0
      sect->size = note->descsz;
11924
0
      sect->filepos = note->descpos;
11925
0
      sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32;
11926
11927
0
      return true;
11928
0
    }
11929
11930
1
  return true;
11931
1
}
11932
11933
static bool
11934
elfcore_grok_nto_status (bfd *abfd, Elf_Internal_Note *note, long *tid)
11935
0
{
11936
0
  void *ddata = note->descdata;
11937
0
  char buf[100];
11938
0
  char *name;
11939
0
  asection *sect;
11940
0
  short sig;
11941
0
  unsigned flags;
11942
11943
0
  if (note->descsz < 16)
11944
0
    return false;
11945
11946
  /* nto_procfs_status 'pid' field is at offset 0.  */
11947
0
  elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, (bfd_byte *) ddata);
11948
11949
  /* nto_procfs_status 'tid' field is at offset 4.  Pass it back.  */
11950
0
  *tid = bfd_get_32 (abfd, (bfd_byte *) ddata + 4);
11951
11952
  /* nto_procfs_status 'flags' field is at offset 8.  */
11953
0
  flags = bfd_get_32 (abfd, (bfd_byte *) ddata + 8);
11954
11955
  /* nto_procfs_status 'what' field is at offset 14.  */
11956
0
  if ((sig = bfd_get_16 (abfd, (bfd_byte *) ddata + 14)) > 0)
11957
0
    {
11958
0
      elf_tdata (abfd)->core->signal = sig;
11959
0
      elf_tdata (abfd)->core->lwpid = *tid;
11960
0
    }
11961
11962
  /* _DEBUG_FLAG_CURTID (current thread) is 0x80.  Some cores
11963
     do not come from signals so we make sure we set the current
11964
     thread just in case.  */
11965
0
  if (flags & 0x00000080)
11966
0
    elf_tdata (abfd)->core->lwpid = *tid;
11967
11968
  /* Make a ".qnx_core_status/%d" section.  */
11969
0
  sprintf (buf, ".qnx_core_status/%ld", *tid);
11970
11971
0
  name = bfd_alloc (abfd, strlen (buf) + 1);
11972
0
  if (name == NULL)
11973
0
    return false;
11974
0
  strcpy (name, buf);
11975
11976
0
  sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
11977
0
  if (sect == NULL)
11978
0
    return false;
11979
11980
0
  sect->size    = note->descsz;
11981
0
  sect->filepos   = note->descpos;
11982
0
  sect->alignment_power = 2;
11983
11984
0
  return (elfcore_maybe_make_sect (abfd, ".qnx_core_status", sect));
11985
0
}
11986
11987
static bool
11988
elfcore_grok_nto_regs (bfd *abfd,
11989
           Elf_Internal_Note *note,
11990
           long tid,
11991
           char *base)
11992
16
{
11993
16
  char buf[100];
11994
16
  char *name;
11995
16
  asection *sect;
11996
11997
  /* Make a "(base)/%d" section.  */
11998
16
  sprintf (buf, "%s/%ld", base, tid);
11999
12000
16
  name = bfd_alloc (abfd, strlen (buf) + 1);
12001
16
  if (name == NULL)
12002
0
    return false;
12003
16
  strcpy (name, buf);
12004
12005
16
  sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
12006
16
  if (sect == NULL)
12007
0
    return false;
12008
12009
16
  sect->size    = note->descsz;
12010
16
  sect->filepos   = note->descpos;
12011
16
  sect->alignment_power = 2;
12012
12013
  /* This is the current thread.  */
12014
16
  if (elf_tdata (abfd)->core->lwpid == tid)
12015
0
    return elfcore_maybe_make_sect (abfd, base, sect);
12016
12017
16
  return true;
12018
16
}
12019
12020
static bool
12021
elfcore_grok_nto_note (bfd *abfd, Elf_Internal_Note *note)
12022
112
{
12023
  /* Every GREG section has a STATUS section before it.  Store the
12024
     tid from the previous call to pass down to the next gregs
12025
     function.  */
12026
112
  static long tid = 1;
12027
12028
112
  switch (note->type)
12029
112
    {
12030
0
    case QNT_CORE_INFO:
12031
0
      return elfcore_make_note_pseudosection (abfd, ".qnx_core_info", note);
12032
0
    case QNT_CORE_STATUS:
12033
0
      return elfcore_grok_nto_status (abfd, note, &tid);
12034
16
    case QNT_CORE_GREG:
12035
16
      return elfcore_grok_nto_regs (abfd, note, tid, NOTE_PSEUDO_SECTION_REG);
12036
0
    case QNT_CORE_FPREG:
12037
0
      return elfcore_grok_nto_regs (abfd, note, tid, NOTE_PSEUDO_SECTION_REG2);
12038
96
    default:
12039
96
      return true;
12040
112
    }
12041
112
}
12042
12043
static bool
12044
elfcore_grok_spu_note (bfd *abfd, Elf_Internal_Note *note)
12045
15
{
12046
15
  char *name;
12047
15
  asection *sect;
12048
15
  size_t len;
12049
12050
  /* Use note name as section name.  */
12051
15
  len = note->namesz;
12052
15
  name = bfd_alloc (abfd, len);
12053
15
  if (name == NULL)
12054
0
    return false;
12055
15
  memcpy (name, note->namedata, len);
12056
15
  name[len - 1] = '\0';
12057
12058
15
  sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
12059
15
  if (sect == NULL)
12060
0
    return false;
12061
12062
15
  sect->size    = note->descsz;
12063
15
  sect->filepos   = note->descpos;
12064
15
  sect->alignment_power = 1;
12065
12066
15
  return true;
12067
15
}
12068
12069
/* Function: elfcore_write_note
12070
12071
   Inputs:
12072
     buffer to hold note, and current size of buffer
12073
     name of note
12074
     type of note
12075
     data for note
12076
     size of data for note
12077
12078
   Writes note to end of buffer.  ELF64 notes are written exactly as
12079
   for ELF32, despite the current (as of 2006) ELF gabi specifying
12080
   that they ought to have 8-byte namesz and descsz field, and have
12081
   8-byte alignment.  Other writers, eg. Linux kernel, do the same.
12082
12083
   Return:
12084
   Pointer to realloc'd buffer, *BUFSIZ updated.  */
12085
12086
char *
12087
elfcore_write_note (bfd *abfd,
12088
        char *buf,
12089
        int *bufsiz,
12090
        const char *name,
12091
        int type,
12092
        const void *input,
12093
        int size)
12094
0
{
12095
0
  Elf_External_Note *xnp;
12096
0
  size_t namesz;
12097
0
  size_t newspace;
12098
0
  char *dest;
12099
12100
0
  namesz = 0;
12101
0
  if (name != NULL)
12102
0
    namesz = strlen (name) + 1;
12103
12104
0
  newspace = 12 + ((namesz + 3) & -4) + ((size + 3) & -4);
12105
12106
0
  buf = realloc (buf, *bufsiz + newspace);
12107
0
  if (buf == NULL)
12108
0
    return buf;
12109
0
  dest = buf + *bufsiz;
12110
0
  *bufsiz += newspace;
12111
0
  xnp = (Elf_External_Note *) dest;
12112
0
  H_PUT_32 (abfd, namesz, xnp->namesz);
12113
0
  H_PUT_32 (abfd, size, xnp->descsz);
12114
0
  H_PUT_32 (abfd, type, xnp->type);
12115
0
  dest = xnp->name;
12116
0
  if (name != NULL)
12117
0
    {
12118
0
      memcpy (dest, name, namesz);
12119
0
      dest += namesz;
12120
0
      while (namesz & 3)
12121
0
  {
12122
0
    *dest++ = '\0';
12123
0
    ++namesz;
12124
0
  }
12125
0
    }
12126
0
  memcpy (dest, input, size);
12127
0
  dest += size;
12128
0
  while (size & 3)
12129
0
    {
12130
0
      *dest++ = '\0';
12131
0
      ++size;
12132
0
    }
12133
0
  return buf;
12134
0
}
12135
12136
/* gcc-8 warns (*) on all the strncpy calls in this function about
12137
   possible string truncation.  The "truncation" is not a bug.  We
12138
   have an external representation of structs with fields that are not
12139
   necessarily NULL terminated and corresponding internal
12140
   representation fields that are one larger so that they can always
12141
   be NULL terminated.
12142
   gcc versions between 4.2 and 4.6 do not allow pragma control of
12143
   diagnostics inside functions, giving a hard error if you try to use
12144
   the finer control available with later versions.
12145
   gcc prior to 4.2 warns about diagnostic push and pop.
12146
   gcc-5, gcc-6 and gcc-7 warn that -Wstringop-truncation is unknown,
12147
   unless you also add #pragma GCC diagnostic ignored "-Wpragma".
12148
   (*) Depending on your system header files!  */
12149
#if GCC_VERSION >= 8000
12150
# pragma GCC diagnostic push
12151
# pragma GCC diagnostic ignored "-Wstringop-truncation"
12152
#endif
12153
char *
12154
elfcore_write_prpsinfo (bfd  *abfd,
12155
      char *buf,
12156
      int  *bufsiz,
12157
      const char *fname,
12158
      const char *psargs)
12159
0
{
12160
0
  elf_backend_data *bed = get_elf_backend_data (abfd);
12161
12162
0
  if (bed->elf_backend_write_core_note != NULL)
12163
0
    {
12164
0
      char *ret;
12165
0
      ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
12166
0
             NT_PRPSINFO, fname, psargs);
12167
0
      if (ret != NULL)
12168
0
  return ret;
12169
0
    }
12170
12171
0
#if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
12172
0
# if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
12173
0
  if (bed->s->elfclass == ELFCLASS32)
12174
0
    {
12175
#  if defined (HAVE_PSINFO32_T)
12176
      psinfo32_t data;
12177
      int note_type = NT_PSINFO;
12178
#  else
12179
0
      prpsinfo32_t data;
12180
0
      int note_type = NT_PRPSINFO;
12181
0
#  endif
12182
12183
0
      memset (&data, 0, sizeof (data));
12184
0
      strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
12185
0
      strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
12186
0
      return elfcore_write_note (abfd, buf, bufsiz,
12187
0
         NOTE_NAME_CORE, note_type, &data, sizeof (data));
12188
0
    }
12189
0
  else
12190
0
# endif
12191
0
    {
12192
# if defined (HAVE_PSINFO_T)
12193
      psinfo_t data;
12194
      int note_type = NT_PSINFO;
12195
# else
12196
0
      prpsinfo_t data;
12197
0
      int note_type = NT_PRPSINFO;
12198
0
# endif
12199
12200
0
      memset (&data, 0, sizeof (data));
12201
0
      strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
12202
0
      strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
12203
0
      return elfcore_write_note (abfd, buf, bufsiz,
12204
0
         NOTE_NAME_CORE, note_type, &data, sizeof (data));
12205
0
    }
12206
0
#endif  /* PSINFO_T or PRPSINFO_T */
12207
12208
0
  free (buf);
12209
0
  return NULL;
12210
0
}
12211
#if GCC_VERSION >= 8000
12212
# pragma GCC diagnostic pop
12213
#endif
12214
12215
char *
12216
elfcore_write_linux_prpsinfo32
12217
  (bfd *abfd, char *buf, int *bufsiz,
12218
   const struct elf_internal_linux_prpsinfo *prpsinfo)
12219
0
{
12220
0
  if (get_elf_backend_data (abfd)->linux_prpsinfo32_ugid16)
12221
0
    {
12222
0
      struct elf_external_linux_prpsinfo32_ugid16 data;
12223
12224
0
      swap_linux_prpsinfo32_ugid16_out (abfd, prpsinfo, &data);
12225
0
      return elfcore_write_note (abfd, buf, bufsiz, NOTE_NAME_CORE, NT_PRPSINFO,
12226
0
         &data, sizeof (data));
12227
0
    }
12228
0
  else
12229
0
    {
12230
0
      struct elf_external_linux_prpsinfo32_ugid32 data;
12231
12232
0
      swap_linux_prpsinfo32_ugid32_out (abfd, prpsinfo, &data);
12233
0
      return elfcore_write_note (abfd, buf, bufsiz, NOTE_NAME_CORE, NT_PRPSINFO,
12234
0
         &data, sizeof (data));
12235
0
    }
12236
0
}
12237
12238
char *
12239
elfcore_write_linux_prpsinfo64
12240
  (bfd *abfd, char *buf, int *bufsiz,
12241
   const struct elf_internal_linux_prpsinfo *prpsinfo)
12242
0
{
12243
0
  if (get_elf_backend_data (abfd)->linux_prpsinfo64_ugid16)
12244
0
    {
12245
0
      struct elf_external_linux_prpsinfo64_ugid16 data;
12246
12247
0
      swap_linux_prpsinfo64_ugid16_out (abfd, prpsinfo, &data);
12248
0
      return elfcore_write_note (abfd, buf, bufsiz,
12249
0
         NOTE_NAME_CORE, NT_PRPSINFO, &data, sizeof (data));
12250
0
    }
12251
0
  else
12252
0
    {
12253
0
      struct elf_external_linux_prpsinfo64_ugid32 data;
12254
12255
0
      swap_linux_prpsinfo64_ugid32_out (abfd, prpsinfo, &data);
12256
0
      return elfcore_write_note (abfd, buf, bufsiz,
12257
0
         NOTE_NAME_CORE, NT_PRPSINFO, &data, sizeof (data));
12258
0
    }
12259
0
}
12260
12261
char *
12262
elfcore_write_prstatus (bfd *abfd,
12263
      char *buf,
12264
      int *bufsiz,
12265
      long pid,
12266
      int cursig,
12267
      const void *gregs)
12268
0
{
12269
0
  elf_backend_data *bed = get_elf_backend_data (abfd);
12270
12271
0
  if (bed->elf_backend_write_core_note != NULL)
12272
0
    {
12273
0
      char *ret;
12274
0
      ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz,
12275
0
             NT_PRSTATUS,
12276
0
             pid, cursig, gregs);
12277
0
      if (ret != NULL)
12278
0
  return ret;
12279
0
    }
12280
12281
0
#if defined (HAVE_PRSTATUS_T)
12282
0
#if defined (HAVE_PRSTATUS32_T)
12283
0
  if (bed->s->elfclass == ELFCLASS32)
12284
0
    {
12285
0
      prstatus32_t prstat;
12286
12287
0
      memset (&prstat, 0, sizeof (prstat));
12288
0
      prstat.pr_pid = pid;
12289
0
      prstat.pr_cursig = cursig;
12290
0
      memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
12291
0
      return elfcore_write_note (abfd, buf, bufsiz, NOTE_NAME_CORE,
12292
0
         NT_PRSTATUS, &prstat, sizeof (prstat));
12293
0
    }
12294
0
  else
12295
0
#endif
12296
0
    {
12297
0
      prstatus_t prstat;
12298
12299
0
      memset (&prstat, 0, sizeof (prstat));
12300
0
      prstat.pr_pid = pid;
12301
0
      prstat.pr_cursig = cursig;
12302
0
      memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
12303
0
      return elfcore_write_note (abfd, buf, bufsiz, NOTE_NAME_CORE,
12304
0
         NT_PRSTATUS, &prstat, sizeof (prstat));
12305
0
    }
12306
0
#endif /* HAVE_PRSTATUS_T */
12307
12308
0
  free (buf);
12309
0
  return NULL;
12310
0
}
12311
12312
#if defined (HAVE_LWPSTATUS_T)
12313
char *
12314
elfcore_write_lwpstatus (bfd *abfd,
12315
       char *buf,
12316
       int *bufsiz,
12317
       long pid,
12318
       int cursig,
12319
       const void *gregs)
12320
{
12321
  lwpstatus_t lwpstat;
12322
12323
  memset (&lwpstat, 0, sizeof (lwpstat));
12324
  lwpstat.pr_lwpid  = pid >> 16;
12325
  lwpstat.pr_cursig = cursig;
12326
#if defined (HAVE_LWPSTATUS_T_PR_REG)
12327
  memcpy (&lwpstat.pr_reg, gregs, sizeof (lwpstat.pr_reg));
12328
#elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
12329
#if !defined(gregs)
12330
  memcpy (lwpstat.pr_context.uc_mcontext.gregs,
12331
    gregs, sizeof (lwpstat.pr_context.uc_mcontext.gregs));
12332
#else
12333
  memcpy (lwpstat.pr_context.uc_mcontext.__gregs,
12334
    gregs, sizeof (lwpstat.pr_context.uc_mcontext.__gregs));
12335
#endif
12336
#endif
12337
  return elfcore_write_note (abfd, buf, bufsiz, NOTE_NAME_CORE,
12338
           NT_LWPSTATUS, &lwpstat, sizeof (lwpstat));
12339
}
12340
#endif /* HAVE_LWPSTATUS_T */
12341
12342
#if defined (HAVE_PSTATUS_T)
12343
char *
12344
elfcore_write_pstatus (bfd *abfd,
12345
           char *buf,
12346
           int *bufsiz,
12347
           long pid,
12348
           int cursig ATTRIBUTE_UNUSED,
12349
           const void *gregs ATTRIBUTE_UNUSED)
12350
{
12351
#if defined (HAVE_PSTATUS32_T)
12352
  elf_backend_data *bed = get_elf_backend_data (abfd);
12353
12354
  if (bed->s->elfclass == ELFCLASS32)
12355
    {
12356
      pstatus32_t pstat;
12357
12358
      memset (&pstat, 0, sizeof (pstat));
12359
      pstat.pr_pid = pid & 0xffff;
12360
      buf = elfcore_write_note (abfd, buf, bufsiz, NOTE_NAME_CORE,
12361
        NT_PSTATUS, &pstat, sizeof (pstat));
12362
      return buf;
12363
    }
12364
  else
12365
#endif
12366
    {
12367
      pstatus_t pstat;
12368
12369
      memset (&pstat, 0, sizeof (pstat));
12370
      pstat.pr_pid = pid & 0xffff;
12371
      buf = elfcore_write_note (abfd, buf, bufsiz, NOTE_NAME_CORE,
12372
        NT_PSTATUS, &pstat, sizeof (pstat));
12373
      return buf;
12374
    }
12375
}
12376
#endif /* HAVE_PSTATUS_T */
12377
12378
char *
12379
elfcore_write_prfpreg (bfd *abfd,
12380
           char *buf,
12381
           int *bufsiz,
12382
           const void *fpregs,
12383
           int size)
12384
0
{
12385
0
  return elfcore_write_note (abfd, buf, bufsiz,
12386
0
           NOTE_NAME_CORE, NT_FPREGSET, fpregs, size);
12387
0
}
12388
12389
char *
12390
elfcore_write_prxfpreg (bfd *abfd,
12391
      char *buf,
12392
      int *bufsiz,
12393
      const void *xfpregs,
12394
      int size)
12395
0
{
12396
0
  return elfcore_write_note (abfd, buf, bufsiz,
12397
0
           NOTE_NAME_LINUX, NT_PRXFPREG, xfpregs, size);
12398
0
}
12399
12400
char *
12401
elfcore_write_xstatereg (bfd *abfd, char *buf, int *bufsiz,
12402
       const void *xfpregs, int size)
12403
0
{
12404
0
  char *note_name;
12405
12406
0
  if (get_elf_backend_data (abfd)->elf_osabi == ELFOSABI_FREEBSD)
12407
0
    note_name = NOTE_NAME_FREEBSD;
12408
0
  else
12409
0
    note_name = NOTE_NAME_LINUX;
12410
12411
0
  return elfcore_write_note (abfd, buf, bufsiz,
12412
0
           note_name, NT_X86_XSTATE, xfpregs, size);
12413
0
}
12414
12415
char *
12416
elfcore_write_xsave_layout (bfd *abfd, char *buf, int *bufsiz,
12417
          const void *xsave_layout, int size)
12418
0
{
12419
0
  return elfcore_write_note (abfd, buf, bufsiz, NOTE_NAME_LINUX,
12420
0
           NT_X86_XSAVE_LAYOUT, xsave_layout, size);
12421
0
}
12422
12423
static char *
12424
elfcore_write_sspreg (bfd *abfd, char *buf, int *bufsiz,
12425
          const void *ssp, int size)
12426
0
{
12427
0
  return elfcore_write_note (abfd, buf, bufsiz,
12428
0
           NOTE_NAME_LINUX, NT_X86_SHSTK, ssp, size);
12429
0
}
12430
12431
char *
12432
elfcore_write_x86_segbases (bfd *abfd, char *buf, int *bufsiz,
12433
          const void *regs, int size)
12434
0
{
12435
0
  return elfcore_write_note (abfd, buf, bufsiz,
12436
0
           NOTE_NAME_FREEBSD, NT_FREEBSD_X86_SEGBASES,
12437
0
           regs, size);
12438
0
}
12439
12440
char *
12441
elfcore_write_i386_tls (bfd *abfd, char *buf, int *bufsiz,
12442
          const void *regs, int size)
12443
0
{
12444
0
  return elfcore_write_note (abfd, buf, bufsiz, NOTE_NAME_LINUX, NT_386_TLS,
12445
0
           regs, size);
12446
0
}
12447
12448
char *
12449
elfcore_write_ppc_vmx (bfd *abfd,
12450
           char *buf,
12451
           int *bufsiz,
12452
           const void *ppc_vmx,
12453
           int size)
12454
0
{
12455
0
  return elfcore_write_note (abfd, buf, bufsiz,
12456
0
           NOTE_NAME_LINUX, NT_PPC_VMX, ppc_vmx, size);
12457
0
}
12458
12459
char *
12460
elfcore_write_ppc_vsx (bfd *abfd,
12461
           char *buf,
12462
           int *bufsiz,
12463
           const void *ppc_vsx,
12464
           int size)
12465
0
{
12466
0
  return elfcore_write_note (abfd, buf, bufsiz,
12467
0
           NOTE_NAME_LINUX, NT_PPC_VSX, ppc_vsx, size);
12468
0
}
12469
12470
char *
12471
elfcore_write_ppc_tar (bfd *abfd,
12472
           char *buf,
12473
           int *bufsiz,
12474
           const void *ppc_tar,
12475
           int size)
12476
0
{
12477
0
  return elfcore_write_note (abfd, buf, bufsiz,
12478
0
           NOTE_NAME_LINUX, NT_PPC_TAR, ppc_tar, size);
12479
0
}
12480
12481
char *
12482
elfcore_write_ppc_ppr (bfd *abfd,
12483
           char *buf,
12484
           int *bufsiz,
12485
           const void *ppc_ppr,
12486
           int size)
12487
0
{
12488
0
  return elfcore_write_note (abfd, buf, bufsiz,
12489
0
           NOTE_NAME_LINUX, NT_PPC_PPR, ppc_ppr, size);
12490
0
}
12491
12492
char *
12493
elfcore_write_ppc_dscr (bfd *abfd,
12494
      char *buf,
12495
      int *bufsiz,
12496
      const void *ppc_dscr,
12497
      int size)
12498
0
{
12499
0
  return elfcore_write_note (abfd, buf, bufsiz,
12500
0
           NOTE_NAME_LINUX, NT_PPC_DSCR, ppc_dscr, size);
12501
0
}
12502
12503
char *
12504
elfcore_write_ppc_ebb (bfd *abfd,
12505
           char *buf,
12506
           int *bufsiz,
12507
           const void *ppc_ebb,
12508
           int size)
12509
0
{
12510
0
  return elfcore_write_note (abfd, buf, bufsiz,
12511
0
           NOTE_NAME_LINUX, NT_PPC_EBB, ppc_ebb, size);
12512
0
}
12513
12514
char *
12515
elfcore_write_ppc_pmu (bfd *abfd,
12516
           char *buf,
12517
           int *bufsiz,
12518
           const void *ppc_pmu,
12519
           int size)
12520
0
{
12521
0
  return elfcore_write_note (abfd, buf, bufsiz,
12522
0
           NOTE_NAME_LINUX, NT_PPC_PMU, ppc_pmu, size);
12523
0
}
12524
12525
char *
12526
elfcore_write_ppc_tm_cgpr (bfd *abfd,
12527
         char *buf,
12528
         int *bufsiz,
12529
         const void *ppc_tm_cgpr,
12530
         int size)
12531
0
{
12532
0
  return elfcore_write_note (abfd, buf, bufsiz,
12533
0
           NOTE_NAME_LINUX, NT_PPC_TM_CGPR,
12534
0
           ppc_tm_cgpr, size);
12535
0
}
12536
12537
char *
12538
elfcore_write_ppc_tm_cfpr (bfd *abfd,
12539
         char *buf,
12540
         int *bufsiz,
12541
         const void *ppc_tm_cfpr,
12542
         int size)
12543
0
{
12544
0
  return elfcore_write_note (abfd, buf, bufsiz,
12545
0
           NOTE_NAME_LINUX, NT_PPC_TM_CFPR,
12546
0
           ppc_tm_cfpr, size);
12547
0
}
12548
12549
char *
12550
elfcore_write_ppc_tm_cvmx (bfd *abfd,
12551
         char *buf,
12552
         int *bufsiz,
12553
         const void *ppc_tm_cvmx,
12554
         int size)
12555
0
{
12556
0
  return elfcore_write_note (abfd, buf, bufsiz,
12557
0
           NOTE_NAME_LINUX, NT_PPC_TM_CVMX,
12558
0
           ppc_tm_cvmx, size);
12559
0
}
12560
12561
char *
12562
elfcore_write_ppc_tm_cvsx (bfd *abfd,
12563
         char *buf,
12564
         int *bufsiz,
12565
         const void *ppc_tm_cvsx,
12566
         int size)
12567
0
{
12568
0
  return elfcore_write_note (abfd, buf, bufsiz,
12569
0
           NOTE_NAME_LINUX, NT_PPC_TM_CVSX,
12570
0
           ppc_tm_cvsx, size);
12571
0
}
12572
12573
char *
12574
elfcore_write_ppc_tm_spr (bfd *abfd,
12575
        char *buf,
12576
        int *bufsiz,
12577
        const void *ppc_tm_spr,
12578
        int size)
12579
0
{
12580
0
  return elfcore_write_note (abfd, buf, bufsiz,
12581
0
           NOTE_NAME_LINUX, NT_PPC_TM_SPR,
12582
0
           ppc_tm_spr, size);
12583
0
}
12584
12585
char *
12586
elfcore_write_ppc_tm_ctar (bfd *abfd,
12587
         char *buf,
12588
         int *bufsiz,
12589
         const void *ppc_tm_ctar,
12590
         int size)
12591
0
{
12592
0
  return elfcore_write_note (abfd, buf, bufsiz,
12593
0
           NOTE_NAME_LINUX, NT_PPC_TM_CTAR,
12594
0
           ppc_tm_ctar, size);
12595
0
}
12596
12597
char *
12598
elfcore_write_ppc_tm_cppr (bfd *abfd,
12599
         char *buf,
12600
         int *bufsiz,
12601
         const void *ppc_tm_cppr,
12602
         int size)
12603
0
{
12604
0
  return elfcore_write_note (abfd, buf, bufsiz,
12605
0
           NOTE_NAME_LINUX, NT_PPC_TM_CPPR,
12606
0
           ppc_tm_cppr, size);
12607
0
}
12608
12609
char *
12610
elfcore_write_ppc_tm_cdscr (bfd *abfd,
12611
          char *buf,
12612
          int *bufsiz,
12613
          const void *ppc_tm_cdscr,
12614
          int size)
12615
0
{
12616
0
  return elfcore_write_note (abfd, buf, bufsiz,
12617
0
           NOTE_NAME_LINUX, NT_PPC_TM_CDSCR,
12618
0
           ppc_tm_cdscr, size);
12619
0
}
12620
12621
char *
12622
elfcore_write_ppc_dmr (bfd *abfd,
12623
           char *buf,
12624
           int *bufsiz,
12625
           const void *ppc_dmr,
12626
           int size)
12627
0
{
12628
0
  return elfcore_write_note (abfd, buf, bufsiz,
12629
0
           NOTE_NAME_LINUX, NT_PPC_DMR, ppc_dmr, size);
12630
0
}
12631
12632
static char *
12633
elfcore_write_s390_high_gprs (bfd *abfd,
12634
            char *buf,
12635
            int *bufsiz,
12636
            const void *s390_high_gprs,
12637
            int size)
12638
0
{
12639
0
  return elfcore_write_note (abfd, buf, bufsiz,
12640
0
           NOTE_NAME_LINUX, NT_S390_HIGH_GPRS,
12641
0
           s390_high_gprs, size);
12642
0
}
12643
12644
char *
12645
elfcore_write_s390_timer (bfd *abfd,
12646
        char *buf,
12647
        int *bufsiz,
12648
        const void *s390_timer,
12649
        int size)
12650
0
{
12651
0
  return elfcore_write_note (abfd, buf, bufsiz,
12652
0
           NOTE_NAME_LINUX, NT_S390_TIMER,
12653
0
           s390_timer, size);
12654
0
}
12655
12656
char *
12657
elfcore_write_s390_todcmp (bfd *abfd,
12658
         char *buf,
12659
         int *bufsiz,
12660
         const void *s390_todcmp,
12661
         int size)
12662
0
{
12663
0
  return elfcore_write_note (abfd, buf, bufsiz,
12664
0
           NOTE_NAME_LINUX, NT_S390_TODCMP,
12665
0
           s390_todcmp, size);
12666
0
}
12667
12668
char *
12669
elfcore_write_s390_todpreg (bfd *abfd,
12670
          char *buf,
12671
          int *bufsiz,
12672
          const void *s390_todpreg,
12673
          int size)
12674
0
{
12675
0
  return elfcore_write_note (abfd, buf, bufsiz,
12676
0
           NOTE_NAME_LINUX, NT_S390_TODPREG,
12677
0
           s390_todpreg, size);
12678
0
}
12679
12680
char *
12681
elfcore_write_s390_ctrs (bfd *abfd,
12682
       char *buf,
12683
       int *bufsiz,
12684
       const void *s390_ctrs,
12685
       int size)
12686
0
{
12687
0
  return elfcore_write_note (abfd, buf, bufsiz,
12688
0
           NOTE_NAME_LINUX, NT_S390_CTRS, s390_ctrs, size);
12689
0
}
12690
12691
char *
12692
elfcore_write_s390_prefix (bfd *abfd,
12693
         char *buf,
12694
         int *bufsiz,
12695
         const void *s390_prefix,
12696
         int size)
12697
0
{
12698
0
  return elfcore_write_note (abfd, buf, bufsiz,
12699
0
           NOTE_NAME_LINUX, NT_S390_PREFIX,
12700
0
           s390_prefix, size);
12701
0
}
12702
12703
char *
12704
elfcore_write_s390_last_break (bfd *abfd,
12705
             char *buf,
12706
             int *bufsiz,
12707
             const void *s390_last_break,
12708
             int size)
12709
0
{
12710
0
  return elfcore_write_note (abfd, buf, bufsiz,
12711
0
           NOTE_NAME_LINUX, NT_S390_LAST_BREAK,
12712
0
           s390_last_break, size);
12713
0
}
12714
12715
char *
12716
elfcore_write_s390_system_call (bfd *abfd,
12717
        char *buf,
12718
        int *bufsiz,
12719
        const void *s390_system_call,
12720
        int size)
12721
0
{
12722
0
  return elfcore_write_note (abfd, buf, bufsiz,
12723
0
           NOTE_NAME_LINUX, NT_S390_SYSTEM_CALL,
12724
0
           s390_system_call, size);
12725
0
}
12726
12727
char *
12728
elfcore_write_s390_tdb (bfd *abfd,
12729
      char *buf,
12730
      int *bufsiz,
12731
      const void *s390_tdb,
12732
      int size)
12733
0
{
12734
0
  return elfcore_write_note (abfd, buf, bufsiz,
12735
0
           NOTE_NAME_LINUX, NT_S390_TDB, s390_tdb, size);
12736
0
}
12737
12738
char *
12739
elfcore_write_s390_vxrs_low (bfd *abfd,
12740
           char *buf,
12741
           int *bufsiz,
12742
           const void *s390_vxrs_low,
12743
           int size)
12744
0
{
12745
0
  return elfcore_write_note (abfd, buf, bufsiz,
12746
0
           NOTE_NAME_LINUX, NT_S390_VXRS_LOW,
12747
0
           s390_vxrs_low, size);
12748
0
}
12749
12750
char *
12751
elfcore_write_s390_vxrs_high (bfd *abfd,
12752
           char *buf,
12753
           int *bufsiz,
12754
           const void *s390_vxrs_high,
12755
           int size)
12756
0
{
12757
0
  return elfcore_write_note (abfd, buf, bufsiz,
12758
0
           NOTE_NAME_LINUX, NT_S390_VXRS_HIGH,
12759
0
           s390_vxrs_high, size);
12760
0
}
12761
12762
char *
12763
elfcore_write_s390_gs_cb (bfd *abfd,
12764
        char *buf,
12765
        int *bufsiz,
12766
        const void *s390_gs_cb,
12767
        int size)
12768
0
{
12769
0
  return elfcore_write_note (abfd, buf, bufsiz,
12770
0
           NOTE_NAME_LINUX, NT_S390_GS_CB,
12771
0
           s390_gs_cb, size);
12772
0
}
12773
12774
char *
12775
elfcore_write_s390_gs_bc (bfd *abfd,
12776
        char *buf,
12777
        int *bufsiz,
12778
        const void *s390_gs_bc,
12779
        int size)
12780
0
{
12781
0
  return elfcore_write_note (abfd, buf, bufsiz,
12782
0
           NOTE_NAME_LINUX, NT_S390_GS_BC,
12783
0
           s390_gs_bc, size);
12784
0
}
12785
12786
char *
12787
elfcore_write_arm_vfp (bfd *abfd,
12788
           char *buf,
12789
           int *bufsiz,
12790
           const void *arm_vfp,
12791
           int size)
12792
0
{
12793
0
  return elfcore_write_note (abfd, buf, bufsiz,
12794
0
           NOTE_NAME_LINUX, NT_ARM_VFP,
12795
0
           arm_vfp, size);
12796
0
}
12797
12798
char *
12799
elfcore_write_aarch_tls (bfd *abfd,
12800
           char *buf,
12801
           int *bufsiz,
12802
           const void *aarch_tls,
12803
           int size)
12804
0
{
12805
0
  return elfcore_write_note (abfd, buf, bufsiz,
12806
0
           NOTE_NAME_LINUX, NT_ARM_TLS, aarch_tls, size);
12807
0
}
12808
12809
char *
12810
elfcore_write_aarch_hw_break (bfd *abfd,
12811
          char *buf,
12812
          int *bufsiz,
12813
          const void *aarch_hw_break,
12814
          int size)
12815
0
{
12816
0
  return elfcore_write_note (abfd, buf, bufsiz,
12817
0
           NOTE_NAME_LINUX, NT_ARM_HW_BREAK,
12818
0
           aarch_hw_break, size);
12819
0
}
12820
12821
char *
12822
elfcore_write_aarch_hw_watch (bfd *abfd,
12823
          char *buf,
12824
          int *bufsiz,
12825
          const void *aarch_hw_watch,
12826
          int size)
12827
0
{
12828
0
  return elfcore_write_note (abfd, buf, bufsiz,
12829
0
           NOTE_NAME_LINUX, NT_ARM_HW_WATCH,
12830
0
           aarch_hw_watch, size);
12831
0
}
12832
12833
char *
12834
elfcore_write_aarch_sve (bfd *abfd,
12835
       char *buf,
12836
       int *bufsiz,
12837
       const void *aarch_sve,
12838
       int size)
12839
0
{
12840
0
  return elfcore_write_note (abfd, buf, bufsiz,
12841
0
           NOTE_NAME_LINUX, NT_ARM_SVE, aarch_sve, size);
12842
0
}
12843
12844
char *
12845
elfcore_write_aarch_pauth (bfd *abfd,
12846
         char *buf,
12847
         int *bufsiz,
12848
         const void *aarch_pauth,
12849
         int size)
12850
0
{
12851
0
  return elfcore_write_note (abfd, buf, bufsiz,
12852
0
           NOTE_NAME_LINUX, NT_ARM_PAC_MASK,
12853
0
           aarch_pauth, size);
12854
0
}
12855
12856
char *
12857
elfcore_write_aarch_mte (bfd *abfd,
12858
       char *buf,
12859
       int *bufsiz,
12860
       const void *aarch_mte,
12861
       int size)
12862
0
{
12863
0
  return elfcore_write_note (abfd, buf, bufsiz,
12864
0
           NOTE_NAME_LINUX, NT_ARM_TAGGED_ADDR_CTRL,
12865
0
           aarch_mte, size);
12866
0
}
12867
12868
char *
12869
elfcore_write_aarch_ssve (bfd *abfd,
12870
        char *buf,
12871
        int *bufsiz,
12872
        const void *aarch_ssve,
12873
        int size)
12874
0
{
12875
0
  return elfcore_write_note (abfd, buf, bufsiz,
12876
0
           NOTE_NAME_LINUX, NT_ARM_SSVE,
12877
0
           aarch_ssve, size);
12878
0
}
12879
12880
char *
12881
elfcore_write_aarch_za (bfd *abfd,
12882
      char *buf,
12883
      int *bufsiz,
12884
      const void *aarch_za,
12885
      int size)
12886
0
{
12887
0
  return elfcore_write_note (abfd, buf, bufsiz,
12888
0
           NOTE_NAME_LINUX, NT_ARM_ZA,
12889
0
           aarch_za, size);
12890
0
}
12891
12892
/* Write the buffer of zt register values in aarch_zt (length SIZE) into
12893
   the note buffer BUF and update *BUFSIZ.  ABFD is the bfd the note is being
12894
   written into.  Return a pointer to the new start of the note buffer, to
12895
   replace BUF which may no longer be valid.  */
12896
12897
char *
12898
elfcore_write_aarch_zt (bfd *abfd,
12899
      char *buf,
12900
      int *bufsiz,
12901
      const void *aarch_zt,
12902
      int size)
12903
0
{
12904
0
  return elfcore_write_note (abfd, buf, bufsiz,
12905
0
           NOTE_NAME_LINUX, NT_ARM_ZT,
12906
0
           aarch_zt, size);
12907
0
}
12908
12909
/* Write the buffer of GCS register values in AARCH_GCS (length SIZE) into
12910
   the note buffer BUF and update *BUFSIZ.  ABFD is the bfd the note is being
12911
   written into.  Return a pointer to the new start of the note buffer, to
12912
   replace BUF which may no longer be valid.  */
12913
12914
static char *
12915
elfcore_write_aarch_gcs (bfd *abfd, char *buf, int *bufsiz,
12916
       const void *aarch_gcs, int size)
12917
0
{
12918
0
  return elfcore_write_note (abfd, buf, bufsiz, NOTE_NAME_LINUX, NT_ARM_GCS,
12919
0
           aarch_gcs, size);
12920
0
}
12921
12922
/* Write the buffer of POE register values in AARCH_POE (length SIZE) into
12923
   the note buffer BUF and update *BUFSIZ.  ABFD is the bfd the note is being
12924
   written into.  Return a pointer to the new start of the note buffer, to
12925
   replace BUF which may no longer be valid.  */
12926
12927
static char *
12928
elfcore_write_aarch_poe (bfd *abfd, char *buf, int *bufsiz,
12929
       const void *aarch_poe, int size)
12930
0
{
12931
0
  return elfcore_write_note (abfd, buf, bufsiz, NOTE_NAME_LINUX, NT_ARM_POE,
12932
0
           aarch_poe, size);
12933
0
}
12934
12935
/* Write the buffer of FPMR value in AARCH_FPMR (length SIZE) into
12936
   the note buffer BUF and update *BUFSIZ.  ABFD is the bfd the note is being
12937
   written into.  Return a pointer to the new start of the note buffer, to
12938
   replace BUF which may no longer be valid.  */
12939
12940
char *
12941
elfcore_write_aarch_fpmr (bfd *abfd,
12942
      char *buf,
12943
      int *bufsiz,
12944
      const void *aarch_fpmr,
12945
      int size)
12946
0
{
12947
0
  return elfcore_write_note (abfd, buf, bufsiz,
12948
0
           NOTE_NAME_LINUX, NT_ARM_FPMR, aarch_fpmr, size);
12949
0
}
12950
12951
char *
12952
elfcore_write_arc_v2 (bfd *abfd,
12953
          char *buf,
12954
          int *bufsiz,
12955
          const void *arc_v2,
12956
          int size)
12957
0
{
12958
0
  return elfcore_write_note (abfd, buf, bufsiz,
12959
0
           NOTE_NAME_LINUX, NT_ARC_V2, arc_v2, size);
12960
0
}
12961
12962
char *
12963
elfcore_write_loongarch_cpucfg (bfd *abfd,
12964
        char *buf,
12965
        int *bufsiz,
12966
        const void *loongarch_cpucfg,
12967
        int size)
12968
0
{
12969
0
  return elfcore_write_note (abfd, buf, bufsiz,
12970
0
           NOTE_NAME_LINUX, NT_LARCH_CPUCFG,
12971
0
           loongarch_cpucfg, size);
12972
0
}
12973
12974
char *
12975
elfcore_write_loongarch_lbt (bfd *abfd,
12976
           char *buf,
12977
           int *bufsiz,
12978
           const void *loongarch_lbt,
12979
           int size)
12980
0
{
12981
0
  return elfcore_write_note (abfd, buf, bufsiz,
12982
0
           NOTE_NAME_LINUX, NT_LARCH_LBT,
12983
0
           loongarch_lbt, size);
12984
0
}
12985
12986
char *
12987
elfcore_write_loongarch_lsx (bfd *abfd,
12988
           char *buf,
12989
           int *bufsiz,
12990
           const void *loongarch_lsx,
12991
           int size)
12992
0
{
12993
0
  return elfcore_write_note (abfd, buf, bufsiz,
12994
0
           NOTE_NAME_LINUX, NT_LARCH_LSX,
12995
0
           loongarch_lsx, size);
12996
0
}
12997
12998
char *
12999
elfcore_write_loongarch_lasx (bfd *abfd,
13000
            char *buf,
13001
            int *bufsiz,
13002
            const void *loongarch_lasx,
13003
            int size)
13004
0
{
13005
0
  return elfcore_write_note (abfd, buf, bufsiz,
13006
0
           NOTE_NAME_LINUX, NT_LARCH_LASX,
13007
0
           loongarch_lasx, size);
13008
0
}
13009
13010
/* Write the buffer of csr values in CSRS (length SIZE) into the note
13011
   buffer BUF and update *BUFSIZ.  ABFD is the bfd the note is being
13012
   written into.  Return a pointer to the new start of the note buffer, to
13013
   replace BUF which may no longer be valid.  */
13014
13015
char *
13016
elfcore_write_riscv_csr (bfd *abfd,
13017
       char *buf,
13018
       int *bufsiz,
13019
       const void *csrs,
13020
       int size)
13021
0
{
13022
0
  return elfcore_write_note (abfd, buf, bufsiz,
13023
0
           NOTE_NAME_GDB, NT_RISCV_CSR, csrs, size);
13024
0
}
13025
13026
/* Write the target description (a string) pointed to by TDESC, length
13027
   SIZE, into the note buffer BUF, and update *BUFSIZ.  ABFD is the bfd the
13028
   note is being written into.  Return a pointer to the new start of the
13029
   note buffer, to replace BUF which may no longer be valid.  */
13030
13031
char *
13032
elfcore_write_gdb_tdesc (bfd *abfd,
13033
       char *buf,
13034
       int *bufsiz,
13035
       const void *tdesc,
13036
       int size)
13037
0
{
13038
0
  return elfcore_write_note (abfd, buf, bufsiz,
13039
0
           NOTE_NAME_GDB, NT_GDB_TDESC, tdesc, size);
13040
0
}
13041
13042
char *
13043
elfcore_write_register_note (bfd *abfd,
13044
           char *buf,
13045
           int *bufsiz,
13046
           const char *section,
13047
           const void *data,
13048
           int size)
13049
0
{
13050
0
  static const struct
13051
0
    {
13052
0
      const char * section_name;
13053
0
      char *       (*writer) (bfd *, char *, int *, const void *, int);
13054
0
    }
13055
0
  note_writers [] =
13056
0
    {
13057
0
      { NOTE_PSEUDO_SECTION_AARCH_FPMR,       elfcore_write_aarch_fpmr},
13058
0
      { NOTE_PSEUDO_SECTION_AARCH_GCS,        elfcore_write_aarch_gcs},
13059
0
      { NOTE_PSEUDO_SECTION_AARCH_POE,        elfcore_write_aarch_poe},
13060
0
      { NOTE_PSEUDO_SECTION_AARCH_HW_BREAK,   elfcore_write_aarch_hw_break},
13061
0
      { NOTE_PSEUDO_SECTION_AARCH_HW_WATCH,   elfcore_write_aarch_hw_watch},
13062
0
      { NOTE_PSEUDO_SECTION_AARCH_MTE,        elfcore_write_aarch_mte},
13063
0
      { NOTE_PSEUDO_SECTION_AARCH_PAUTH,      elfcore_write_aarch_pauth},
13064
0
      { NOTE_PSEUDO_SECTION_AARCH_SSVE,       elfcore_write_aarch_ssve},
13065
0
      { NOTE_PSEUDO_SECTION_AARCH_SVE,        elfcore_write_aarch_sve},
13066
0
      { NOTE_PSEUDO_SECTION_AARCH_TLS,        elfcore_write_aarch_tls},
13067
0
      { NOTE_PSEUDO_SECTION_AARCH_ZA,         elfcore_write_aarch_za},
13068
0
      { NOTE_PSEUDO_SECTION_AARCH_ZT,         elfcore_write_aarch_zt},
13069
0
      { NOTE_PSEUDO_SECTION_ARC_V2,           elfcore_write_arc_v2},
13070
0
      { NOTE_PSEUDO_SECTION_ARM_VFP,          elfcore_write_arm_vfp},
13071
0
      { NOTE_PSEUDO_SECTION_I386_TLS,         elfcore_write_i386_tls},
13072
0
      { NOTE_PSEUDO_SECTION_LOONGARCH_CPUCFG, elfcore_write_loongarch_cpucfg},
13073
0
      { NOTE_PSEUDO_SECTION_LOONGARCH_LASX,   elfcore_write_loongarch_lasx},
13074
0
      { NOTE_PSEUDO_SECTION_LOONGARCH_LBT,    elfcore_write_loongarch_lbt},
13075
0
      { NOTE_PSEUDO_SECTION_LOONGARCH_LSX,    elfcore_write_loongarch_lsx},
13076
0
      { NOTE_PSEUDO_SECTION_PPC_DMR,          elfcore_write_ppc_dmr},
13077
0
      { NOTE_PSEUDO_SECTION_PPC_DSCR,         elfcore_write_ppc_dscr},
13078
0
      { NOTE_PSEUDO_SECTION_PPC_EBB,          elfcore_write_ppc_ebb},
13079
0
      { NOTE_PSEUDO_SECTION_PPC_PMU,          elfcore_write_ppc_pmu},
13080
0
      { NOTE_PSEUDO_SECTION_PPC_PPR,          elfcore_write_ppc_ppr},
13081
0
      { NOTE_PSEUDO_SECTION_PPC_TAR,          elfcore_write_ppc_tar},
13082
0
      { NOTE_PSEUDO_SECTION_PPC_TM_CDSCR,     elfcore_write_ppc_tm_cdscr},
13083
0
      { NOTE_PSEUDO_SECTION_PPC_TM_CFPR,      elfcore_write_ppc_tm_cfpr},
13084
0
      { NOTE_PSEUDO_SECTION_PPC_TM_CGPR,      elfcore_write_ppc_tm_cgpr},
13085
0
      { NOTE_PSEUDO_SECTION_PPC_TM_CPPR,      elfcore_write_ppc_tm_cppr},
13086
0
      { NOTE_PSEUDO_SECTION_PPC_TM_CTAR,      elfcore_write_ppc_tm_ctar},
13087
0
      { NOTE_PSEUDO_SECTION_PPC_TM_CVMX,      elfcore_write_ppc_tm_cvmx},
13088
0
      { NOTE_PSEUDO_SECTION_PPC_TM_CVSX,      elfcore_write_ppc_tm_cvsx},
13089
0
      { NOTE_PSEUDO_SECTION_PPC_TM_SPR,       elfcore_write_ppc_tm_spr},
13090
0
      { NOTE_PSEUDO_SECTION_PPC_VMX,          elfcore_write_ppc_vmx},
13091
0
      { NOTE_PSEUDO_SECTION_PPC_VSX,          elfcore_write_ppc_vsx},
13092
0
      { NOTE_PSEUDO_SECTION_REG2,             elfcore_write_prfpreg},
13093
0
      { NOTE_PSEUDO_SECTION_RISCV_CSR,        elfcore_write_riscv_csr},
13094
0
      { NOTE_PSEUDO_SECTION_S390_CTRS,        elfcore_write_s390_ctrs},
13095
0
      { NOTE_PSEUDO_SECTION_S390_GS_BC,       elfcore_write_s390_gs_bc},
13096
0
      { NOTE_PSEUDO_SECTION_S390_GS_CB,       elfcore_write_s390_gs_cb},
13097
0
      { NOTE_PSEUDO_SECTION_S390_HIGH_GPRS,   elfcore_write_s390_high_gprs},
13098
0
      { NOTE_PSEUDO_SECTION_S390_LAST_BREAK,  elfcore_write_s390_last_break},
13099
0
      { NOTE_PSEUDO_SECTION_S390_PREFIX,      elfcore_write_s390_prefix},
13100
0
      { NOTE_PSEUDO_SECTION_S390_SYSTEM_CALL, elfcore_write_s390_system_call},
13101
0
      { NOTE_PSEUDO_SECTION_S390_TDB,         elfcore_write_s390_tdb},
13102
0
      { NOTE_PSEUDO_SECTION_S390_TIMER,       elfcore_write_s390_timer},
13103
0
      { NOTE_PSEUDO_SECTION_S390_TODCMP,      elfcore_write_s390_todcmp},
13104
0
      { NOTE_PSEUDO_SECTION_S390_TODPREG,     elfcore_write_s390_todpreg},
13105
0
      { NOTE_PSEUDO_SECTION_S390_VXRS_HIGH,   elfcore_write_s390_vxrs_high},
13106
0
      { NOTE_PSEUDO_SECTION_S390_VXRS_LOW,    elfcore_write_s390_vxrs_low},
13107
0
      { NOTE_PSEUDO_SECTION_SSP,              elfcore_write_sspreg},
13108
0
      { NOTE_PSEUDO_SECTION_TDESC,            elfcore_write_gdb_tdesc},
13109
0
      { NOTE_PSEUDO_SECTION_X86_SEGBASES,     elfcore_write_x86_segbases},
13110
0
      { NOTE_PSEUDO_SECTION_XFP,              elfcore_write_prxfpreg},
13111
0
      { NOTE_PSEUDO_SECTION_XSTATE,           elfcore_write_xstatereg},
13112
0
      { NOTE_PSEUDO_SECTION_XSAVE_LAYOUT,     elfcore_write_xsave_layout} /* NB/ No comma.  */
13113
0
    };
13114
13115
0
  int i;
13116
13117
0
  for (i = ARRAY_SIZE (note_writers); i--;)
13118
0
    if (streq (section, note_writers[i].section_name))
13119
0
      return  note_writers[i].writer (abfd, buf, bufsiz, data, size);
13120
13121
0
  return NULL;
13122
0
}
13123
13124
char *
13125
elfcore_write_file_note (bfd *obfd, char *note_data, int *note_size,
13126
       const void *buf, int bufsiz)
13127
0
{
13128
0
  return elfcore_write_note (obfd, note_data, note_size,
13129
0
           NOTE_NAME_CORE, NT_FILE, buf, bufsiz);
13130
0
}
13131
13132
static bool
13133
elf_parse_notes (bfd *abfd, char *buf, size_t size, file_ptr offset,
13134
     size_t align)
13135
8.86k
{
13136
8.86k
  char *p;
13137
13138
  /* NB: CORE PT_NOTE segments may have p_align values of 0 or 1.
13139
     gABI specifies that PT_NOTE alignment should be aligned to 4
13140
     bytes for 32-bit objects and to 8 bytes for 64-bit objects.  If
13141
     align is less than 4, we use 4 byte alignment.   */
13142
8.86k
  if (align < 4)
13143
3.26k
    align = 4;
13144
8.86k
  if (align != 4 && align != 8)
13145
2.45k
    return false;
13146
13147
6.40k
  p = buf;
13148
18.9k
  while (p < buf + size)
13149
18.0k
    {
13150
18.0k
      Elf_External_Note *xnp = (Elf_External_Note *) p;
13151
18.0k
      Elf_Internal_Note in;
13152
13153
18.0k
      if (offsetof (Elf_External_Note, name) > buf - p + size)
13154
830
  return false;
13155
13156
17.1k
      in.type = H_GET_32 (abfd, xnp->type);
13157
13158
17.1k
      in.namesz = H_GET_32 (abfd, xnp->namesz);
13159
17.1k
      in.namedata = xnp->name;
13160
17.1k
      if (in.namesz > buf - in.namedata + size)
13161
2.78k
  return false;
13162
13163
14.3k
      in.descsz = H_GET_32 (abfd, xnp->descsz);
13164
14.3k
      in.descdata = p + ELF_NOTE_DESC_OFFSET (in.namesz, align);
13165
14.3k
      in.descpos = offset + (in.descdata - buf);
13166
14.3k
      if (in.descsz != 0
13167
6.08k
    && (in.descdata >= buf + size
13168
5.83k
        || in.descsz > buf - in.descdata + size))
13169
1.23k
  return false;
13170
13171
13.1k
      switch (bfd_get_format (abfd))
13172
13.1k
  {
13173
0
  default:
13174
0
    return true;
13175
13176
4.40k
  case bfd_core:
13177
4.40k
    {
13178
35.2k
#define GROKER_ELEMENT(S,F) {S, sizeof (S) - 1, F}
13179
4.40k
      static const struct
13180
4.40k
      {
13181
4.40k
        const char * string;
13182
4.40k
        size_t len;
13183
4.40k
        bool (*func) (bfd *, Elf_Internal_Note *);
13184
4.40k
      }
13185
4.40k
      grokers[] =
13186
4.40k
      {
13187
4.40k
        GROKER_ELEMENT ("", elfcore_grok_note),
13188
4.40k
        GROKER_ELEMENT (NOTE_NAME_FREEBSD, elfcore_grok_freebsd_note),
13189
4.40k
        GROKER_ELEMENT ("NetBSD-CORE", elfcore_grok_netbsd_note),
13190
4.40k
        GROKER_ELEMENT ("OpenBSD", elfcore_grok_openbsd_note),
13191
4.40k
        GROKER_ELEMENT ("QNX", elfcore_grok_nto_note),
13192
4.40k
        GROKER_ELEMENT ("SPU/", elfcore_grok_spu_note),
13193
4.40k
        GROKER_ELEMENT ("GNU", elfobj_grok_gnu_note),
13194
4.40k
        GROKER_ELEMENT (NOTE_NAME_CORE, elfcore_grok_solaris_note)
13195
4.40k
      };
13196
4.40k
#undef GROKER_ELEMENT
13197
4.40k
      int i;
13198
13199
32.0k
      for (i = ARRAY_SIZE (grokers); i--;)
13200
32.0k
        {
13201
32.0k
    if (in.namesz >= grokers[i].len
13202
12.5k
        && strncmp (in.namedata, grokers[i].string,
13203
12.5k
        grokers[i].len) == 0)
13204
4.40k
      {
13205
4.40k
        if (! grokers[i].func (abfd, & in))
13206
200
          return false;
13207
4.20k
        break;
13208
4.40k
      }
13209
32.0k
        }
13210
4.20k
      break;
13211
4.40k
    }
13212
13213
8.75k
  case bfd_object:
13214
8.75k
    if (in.namesz == sizeof "GNU" && strcmp (in.namedata, "GNU") == 0)
13215
1.39k
      {
13216
1.39k
        if (! elfobj_grok_gnu_note (abfd, &in))
13217
444
    return false;
13218
1.39k
      }
13219
7.36k
    else if (in.namesz == sizeof "stapsdt"
13220
362
       && strcmp (in.namedata, "stapsdt") == 0)
13221
4
      {
13222
4
        if (! elfobj_grok_stapsdt_note (abfd, &in))
13223
0
    return false;
13224
4
      }
13225
8.31k
    break;
13226
13.1k
  }
13227
13228
12.5k
      p += ELF_NOTE_NEXT_OFFSET (in.namesz, in.descsz, align);
13229
12.5k
    }
13230
13231
915
  return true;
13232
6.40k
}
13233
13234
bool
13235
_bfd_elf_read_notes (bfd *abfd, file_ptr offset, bfd_size_type size,
13236
         size_t align)
13237
5.89k
{
13238
5.89k
  char *buf;
13239
13240
5.89k
  if (size == 0 || (size + 1) == 0)
13241
868
    return true;
13242
13243
5.03k
  if (bfd_seek (abfd, offset, SEEK_SET) != 0)
13244
852
    return false;
13245
13246
4.17k
  buf = _bfd_malloc_and_read (abfd, size + 1, size);
13247
4.17k
  if (buf == NULL)
13248
1.90k
    return false;
13249
13250
  /* PR 17512: file: ec08f814
13251
     0-termintate the buffer so that string searches will not overflow.  */
13252
2.27k
  buf[size] = 0;
13253
13254
2.27k
  if (!elf_parse_notes (abfd, buf, size, offset, align))
13255
2.10k
    {
13256
2.10k
      free (buf);
13257
2.10k
      return false;
13258
2.10k
    }
13259
13260
170
  free (buf);
13261
170
  return true;
13262
2.27k
}
13263

13264
/* Providing external access to the ELF program header table.  */
13265
13266
/* Return an upper bound on the number of bytes required to store a
13267
   copy of ABFD's program header table entries.  Return -1 if an error
13268
   occurs; bfd_get_error will return an appropriate code.  */
13269
13270
long
13271
bfd_get_elf_phdr_upper_bound (bfd *abfd)
13272
0
{
13273
0
  if (abfd->xvec->flavour != bfd_target_elf_flavour)
13274
0
    {
13275
0
      bfd_set_error (bfd_error_wrong_format);
13276
0
      return -1;
13277
0
    }
13278
13279
0
  return elf_elfheader (abfd)->e_phnum * sizeof (Elf_Internal_Phdr);
13280
0
}
13281
13282
/* Copy ABFD's program header table entries to *PHDRS.  The entries
13283
   will be stored as an array of Elf_Internal_Phdr structures, as
13284
   defined in include/elf/internal.h.  To find out how large the
13285
   buffer needs to be, call bfd_get_elf_phdr_upper_bound.
13286
13287
   Return the number of program header table entries read, or -1 if an
13288
   error occurs; bfd_get_error will return an appropriate code.  */
13289
13290
int
13291
bfd_get_elf_phdrs (bfd *abfd, void *phdrs)
13292
0
{
13293
0
  int num_phdrs;
13294
13295
0
  if (abfd->xvec->flavour != bfd_target_elf_flavour)
13296
0
    {
13297
0
      bfd_set_error (bfd_error_wrong_format);
13298
0
      return -1;
13299
0
    }
13300
13301
0
  num_phdrs = elf_elfheader (abfd)->e_phnum;
13302
0
  if (num_phdrs != 0)
13303
0
    memcpy (phdrs, elf_tdata (abfd)->phdr,
13304
0
      num_phdrs * sizeof (Elf_Internal_Phdr));
13305
13306
0
  return num_phdrs;
13307
0
}
13308
13309
enum elf_reloc_type_class
13310
_bfd_elf_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
13311
         const asection *rel_sec ATTRIBUTE_UNUSED,
13312
         const Elf_Internal_Rela *rela ATTRIBUTE_UNUSED)
13313
0
{
13314
0
  return reloc_class_normal;
13315
0
}
13316
13317
/* For RELA architectures, return the relocation value for a
13318
   relocation against a local symbol.  */
13319
13320
bfd_vma
13321
_bfd_elf_rela_local_sym (bfd *abfd,
13322
       const Elf_Internal_Sym *sym,
13323
       asection **psec,
13324
       Elf_Internal_Rela *rel)
13325
0
{
13326
0
  asection *sec = *psec;
13327
0
  bfd_vma relocation;
13328
13329
0
  relocation = (sec->output_section->vma
13330
0
    + sec->output_offset
13331
0
    + sym->st_value);
13332
0
  if ((sec->flags & SEC_MERGE)
13333
0
      && ELF_ST_TYPE (sym->st_info) == STT_SECTION
13334
0
      && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
13335
0
    {
13336
0
      rel->r_addend =
13337
0
  _bfd_merged_section_offset (abfd, psec,
13338
0
            sym->st_value + rel->r_addend);
13339
0
      if (sec != *psec)
13340
0
  {
13341
    /* If we have changed the section, and our original section is
13342
       marked with SEC_EXCLUDE, it means that the original
13343
       SEC_MERGE section has been completely subsumed in some
13344
       other SEC_MERGE section.  In this case, we need to leave
13345
       some info around for --emit-relocs.  */
13346
0
    if ((sec->flags & SEC_EXCLUDE) != 0)
13347
0
      sec->kept_section = *psec;
13348
0
    sec = *psec;
13349
0
  }
13350
0
      rel->r_addend -= relocation;
13351
0
      rel->r_addend += sec->output_section->vma + sec->output_offset;
13352
0
    }
13353
0
  return relocation;
13354
0
}
13355
13356
bfd_vma
13357
_bfd_elf_rel_local_sym (bfd *abfd,
13358
      const Elf_Internal_Sym *sym,
13359
      asection **psec,
13360
      bfd_vma addend)
13361
0
{
13362
0
  asection *sec = *psec;
13363
13364
0
  if (sec->sec_info_type != SEC_INFO_TYPE_MERGE)
13365
0
    return sym->st_value + addend;
13366
13367
0
  return _bfd_merged_section_offset (abfd, psec,
13368
0
             sym->st_value + addend);
13369
0
}
13370
13371
/* Adjust an address within a section.  Given OFFSET within SEC, return
13372
   the new offset within the section, based upon changes made to the
13373
   section.  Returns -1 if the offset is now invalid.
13374
   The offset (in abnd out) is in target sized bytes, however big a
13375
   byte may be.  */
13376
13377
bfd_vma
13378
_bfd_elf_section_offset (bfd *abfd,
13379
       struct bfd_link_info *info,
13380
       asection *sec,
13381
       bfd_vma offset)
13382
0
{
13383
0
  switch (sec->sec_info_type)
13384
0
    {
13385
0
    case SEC_INFO_TYPE_STABS:
13386
0
      return _bfd_stab_section_offset (sec, offset);
13387
13388
0
    case SEC_INFO_TYPE_EH_FRAME:
13389
0
      return _bfd_elf_eh_frame_section_offset (abfd, info, sec, offset);
13390
13391
0
    case SEC_INFO_TYPE_SFRAME:
13392
0
      return _bfd_elf_sframe_section_offset (abfd, info, sec, offset);
13393
13394
0
    default:
13395
0
      if ((sec->flags & SEC_ELF_REVERSE_COPY) != 0)
13396
0
  {
13397
    /* Reverse the offset.  */
13398
0
    elf_backend_data *bed = get_elf_backend_data (abfd);
13399
0
    bfd_size_type address_size = bed->s->arch_size / 8;
13400
13401
    /* address_size and sec->size are in octets.  Convert
13402
       to bytes before subtracting the original offset.  */
13403
0
    offset = ((sec->size - address_size)
13404
0
        / bfd_octets_per_byte (abfd, sec) - offset);
13405
0
  }
13406
0
      return offset;
13407
0
    }
13408
0
}
13409

13410
long
13411
_bfd_elf_get_synthetic_symtab (bfd *abfd,
13412
             long symcount ATTRIBUTE_UNUSED,
13413
             asymbol **syms ATTRIBUTE_UNUSED,
13414
             long dynsymcount,
13415
             asymbol **dynsyms,
13416
             asymbol **ret)
13417
15.4k
{
13418
15.4k
  elf_backend_data *bed = get_elf_backend_data (abfd);
13419
15.4k
  asection *relplt;
13420
15.4k
  asymbol *s;
13421
15.4k
  const char *relplt_name;
13422
15.4k
  bool (*slurp_relocs) (bfd *, asection *, asymbol **, bool);
13423
15.4k
  arelent *p;
13424
15.4k
  long count, i, n;
13425
15.4k
  size_t size;
13426
15.4k
  Elf_Internal_Shdr *hdr;
13427
15.4k
  char *names;
13428
15.4k
  asection *plt;
13429
13430
15.4k
  *ret = NULL;
13431
13432
15.4k
  if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0)
13433
15.3k
    return 0;
13434
13435
63
  if (dynsymcount <= 0)
13436
57
    return 0;
13437
13438
6
  if (!bed->plt_sym_val)
13439
2
    return 0;
13440
13441
4
  relplt_name = bed->relplt_name;
13442
4
  if (relplt_name == NULL)
13443
4
    relplt_name = bed->rela_plts_and_copies_p ? ".rela.plt" : ".rel.plt";
13444
4
  relplt = bfd_get_section_by_name (abfd, relplt_name);
13445
4
  if (relplt == NULL)
13446
4
    return 0;
13447
13448
0
  hdr = &elf_section_data (relplt)->this_hdr;
13449
0
  if (hdr->sh_link != elf_dynsymtab (abfd)
13450
0
      || (hdr->sh_type != SHT_REL && hdr->sh_type != SHT_RELA))
13451
0
    return 0;
13452
13453
0
  plt = bfd_get_section_by_name (abfd, ".plt");
13454
0
  if (plt == NULL)
13455
0
    return 0;
13456
13457
0
  slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
13458
0
  if (! (*slurp_relocs) (abfd, relplt, dynsyms, true))
13459
0
    return -1;
13460
13461
0
  count = NUM_SHDR_ENTRIES (hdr);
13462
0
  size = count * sizeof (asymbol);
13463
0
  p = relplt->relocation;
13464
0
  for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
13465
0
    {
13466
0
      size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
13467
0
      if (p->addend != 0)
13468
0
  {
13469
0
#ifdef BFD64
13470
0
    size += sizeof ("+0x") - 1 + 8 + 8 * (bed->s->elfclass == ELFCLASS64);
13471
#else
13472
    size += sizeof ("+0x") - 1 + 8;
13473
#endif
13474
0
  }
13475
0
    }
13476
13477
0
  s = *ret = bfd_malloc (size);
13478
0
  if (s == NULL)
13479
0
    return -1;
13480
13481
0
  names = (char *) (s + count);
13482
0
  p = relplt->relocation;
13483
0
  n = 0;
13484
0
  for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel)
13485
0
    {
13486
0
      size_t len;
13487
0
      bfd_vma addr;
13488
13489
0
      addr = bed->plt_sym_val (i, plt, p);
13490
0
      if (addr == (bfd_vma) -1)
13491
0
  continue;
13492
13493
0
      *s = **p->sym_ptr_ptr;
13494
      /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set.  Since
13495
   we are defining a symbol, ensure one of them is set.  */
13496
0
      if ((s->flags & BSF_LOCAL) == 0)
13497
0
  s->flags |= BSF_GLOBAL;
13498
0
      s->flags |= BSF_SYNTHETIC;
13499
0
      s->section = plt;
13500
0
      s->value = addr - plt->vma;
13501
0
      s->name = names;
13502
0
      s->udata.p = NULL;
13503
0
      len = strlen ((*p->sym_ptr_ptr)->name);
13504
0
      memcpy (names, (*p->sym_ptr_ptr)->name, len);
13505
0
      names += len;
13506
0
      if (p->addend != 0)
13507
0
  {
13508
0
    char buf[30], *a;
13509
13510
0
    memcpy (names, "+0x", sizeof ("+0x") - 1);
13511
0
    names += sizeof ("+0x") - 1;
13512
0
    bfd_sprintf_vma (abfd, buf, p->addend);
13513
0
    for (a = buf; *a == '0'; ++a)
13514
0
      ;
13515
0
    len = strlen (a);
13516
0
    memcpy (names, a, len);
13517
0
    names += len;
13518
0
  }
13519
0
      memcpy (names, "@plt", sizeof ("@plt"));
13520
0
      names += sizeof ("@plt");
13521
0
      ++s, ++n;
13522
0
    }
13523
13524
0
  return n;
13525
0
}
13526
13527
bool
13528
_bfd_elf_final_write_processing (bfd *abfd)
13529
633
{
13530
633
  Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd);
13531
633
  elf_backend_data *bed = get_elf_backend_data (abfd);
13532
13533
633
  if (bed->osabi_exact && i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE)
13534
2
    i_ehdrp->e_ident[EI_OSABI] = bed->elf_osabi;
13535
13536
633
  if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_SOLARIS
13537
622
      || bed->target_os == is_solaris)
13538
11
    {
13539
11
      elf_tdata (abfd)->strtab_hdr.sh_flags = SHF_STRINGS;
13540
11
      elf_tdata (abfd)->shstrtab_hdr.sh_flags = SHF_STRINGS;
13541
11
    }
13542
13543
  /* Set the osabi field to ELFOSABI_GNU if the binary contains
13544
     SHF_GNU_MBIND or SHF_GNU_RETAIN sections or symbols of STT_GNU_IFUNC type
13545
     or STB_GNU_UNIQUE binding.  */
13546
633
  if (elf_tdata (abfd)->has_gnu_osabi != 0)
13547
0
    {
13548
0
      if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE)
13549
0
  i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_GNU;
13550
0
      else if (i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_GNU
13551
0
         && i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_FREEBSD)
13552
0
  {
13553
0
    if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind)
13554
0
      _bfd_error_handler (_("GNU_MBIND section is supported only by GNU "
13555
0
          "and FreeBSD targets"));
13556
0
    if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_ifunc)
13557
0
      _bfd_error_handler (_("symbol type STT_GNU_IFUNC is supported "
13558
0
          "only by GNU and FreeBSD targets"));
13559
0
    if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_unique)
13560
0
      _bfd_error_handler (_("symbol binding STB_GNU_UNIQUE is supported "
13561
0
          "only by GNU and FreeBSD targets"));
13562
0
    if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_retain)
13563
0
      _bfd_error_handler (_("GNU_RETAIN section is supported "
13564
0
          "only by GNU and FreeBSD targets"));
13565
0
    bfd_set_error (bfd_error_sorry);
13566
0
    return false;
13567
0
  }
13568
0
    }
13569
633
  return true;
13570
633
}
13571
13572
13573
/* Return TRUE for ELF symbol types that represent functions.
13574
   This is the default version of this function, which is sufficient for
13575
   most targets.  It returns true if TYPE is STT_FUNC or STT_GNU_IFUNC.  */
13576
13577
bool
13578
_bfd_elf_is_function_type (unsigned int type)
13579
0
{
13580
0
  return (type == STT_FUNC
13581
0
    || type == STT_GNU_IFUNC);
13582
0
}
13583
13584
/* If the ELF symbol SYM might be a function in SEC, return the
13585
   function size and set *CODE_OFF to the function's entry point,
13586
   otherwise return zero.  */
13587
13588
bfd_size_type
13589
_bfd_elf_maybe_function_sym (const asymbol *sym, asection *sec,
13590
           bfd_vma *code_off)
13591
788k
{
13592
788k
  bfd_size_type size;
13593
788k
  elf_symbol_type * elf_sym = (elf_symbol_type *) sym;
13594
13595
788k
  if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
13596
788k
         | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0
13597
600k
      || sym->section != sec)
13598
778k
    return 0;
13599
13600
10.0k
  size = (sym->flags & BSF_SYNTHETIC) ? 0 : elf_sym->internal_elf_sym.st_size;
13601
13602
  /* In theory we should check that the symbol's type satisfies
13603
     _bfd_elf_is_function_type(), but there are some function-like
13604
     symbols which would fail this test.  (eg _start).  Instead
13605
     we check for hidden, local, notype symbols with zero size.
13606
     This type of symbol is generated by the annobin plugin for gcc
13607
     and clang, and should not be considered to be a function symbol.  */
13608
10.0k
  if (size == 0
13609
1.82k
      && ((sym->flags & (BSF_SYNTHETIC | BSF_LOCAL)) == BSF_LOCAL)
13610
916
      && ELF_ST_TYPE (elf_sym->internal_elf_sym.st_info) == STT_NOTYPE
13611
781
      && ELF_ST_VISIBILITY (elf_sym->internal_elf_sym.st_other) == STV_HIDDEN)
13612
54
    return 0;
13613
13614
9.97k
  *code_off = sym->value;
13615
  /* Do not return 0 for the function's size.  */
13616
9.97k
  return size ? size : 1;
13617
10.0k
}
13618
13619
/* Set to non-zero to enable some debug messages.  */
13620
#define DEBUG_SECONDARY_RELOCS   0
13621
13622
/* An internal-to-the-bfd-library only section type
13623
   used to indicate a cached secondary reloc section.  */
13624
5.07k
#define SHT_SECONDARY_RELOC  (SHT_LOOS + SHT_RELA)
13625
13626
/* Create a BFD section to hold a secondary reloc section.  */
13627
13628
bool
13629
_bfd_elf_init_secondary_reloc_section (bfd * abfd,
13630
               Elf_Internal_Shdr *hdr,
13631
               const char * name,
13632
               unsigned int shindex)
13633
1.71k
{
13634
  /* We only support RELA secondary relocs.  */
13635
1.71k
  if (hdr->sh_type != SHT_RELA)
13636
247
    return false;
13637
13638
#if DEBUG_SECONDARY_RELOCS
13639
  fprintf (stderr, "secondary reloc section %s encountered\n", name);
13640
#endif
13641
1.47k
  hdr->sh_type = SHT_SECONDARY_RELOC;
13642
1.47k
  return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
13643
1.71k
}
13644
13645
/* Read in any secondary relocs associated with SEC.  */
13646
13647
bool
13648
_bfd_elf_slurp_secondary_reloc_section (bfd *       abfd,
13649
          asection *  sec,
13650
          asymbol **  symbols,
13651
          bool dynamic)
13652
9.60k
{
13653
9.60k
  elf_backend_data *ebd = get_elf_backend_data (abfd);
13654
9.60k
  asection * relsec;
13655
9.60k
  bool result = true;
13656
9.60k
  bfd_vma (*r_sym) (bfd_vma);
13657
9.60k
  ufile_ptr filesize;
13658
13659
9.60k
#if BFD_DEFAULT_TARGET_SIZE > 32
13660
9.60k
  if (bfd_arch_bits_per_address (abfd) != 32)
13661
4.59k
    r_sym = elf64_r_sym;
13662
5.00k
  else
13663
5.00k
#endif
13664
5.00k
    r_sym = elf32_r_sym;
13665
13666
9.60k
  if (!elf_section_data (sec)->has_secondary_relocs)
13667
9.51k
    return true;
13668
13669
  /* Discover if there are any secondary reloc sections
13670
     associated with SEC.  */
13671
89
  filesize = bfd_get_file_size (abfd);
13672
1.83k
  for (relsec = abfd->sections; relsec != NULL; relsec = relsec->next)
13673
1.74k
    {
13674
1.74k
      Elf_Internal_Shdr * hdr = & elf_section_data (relsec)->this_hdr;
13675
13676
1.74k
      if (hdr->sh_type == SHT_SECONDARY_RELOC
13677
114
    && hdr->sh_info == (unsigned) elf_section_data (sec)->this_idx
13678
90
    && (hdr->sh_entsize == ebd->s->sizeof_rel
13679
90
        || hdr->sh_entsize == ebd->s->sizeof_rela))
13680
90
  {
13681
90
    bfd_byte * native_relocs;
13682
90
    bfd_byte * native_reloc;
13683
90
    arelent * internal_relocs;
13684
90
    arelent * internal_reloc;
13685
90
    size_t i;
13686
90
    unsigned int entsize;
13687
90
    unsigned int symcount;
13688
90
    bfd_size_type reloc_count;
13689
90
    size_t amt;
13690
13691
90
    if (ebd->elf_info_to_howto == NULL)
13692
0
      return false;
13693
13694
#if DEBUG_SECONDARY_RELOCS
13695
    fprintf (stderr, "read secondary relocs for %s from %s\n",
13696
       sec->name, relsec->name);
13697
#endif
13698
90
    entsize = hdr->sh_entsize;
13699
13700
90
    if (filesize != 0
13701
90
        && ((ufile_ptr) hdr->sh_offset > filesize
13702
83
      || hdr->sh_size > filesize - hdr->sh_offset))
13703
19
      {
13704
19
        bfd_set_error (bfd_error_file_truncated);
13705
19
        result = false;
13706
19
        continue;
13707
19
      }
13708
13709
71
    native_relocs = bfd_malloc (hdr->sh_size);
13710
71
    if (native_relocs == NULL)
13711
0
      {
13712
0
        result = false;
13713
0
        continue;
13714
0
      }
13715
13716
71
    reloc_count = NUM_SHDR_ENTRIES (hdr);
13717
71
    if (_bfd_mul_overflow (reloc_count, sizeof (arelent), & amt))
13718
0
      {
13719
0
        free (native_relocs);
13720
0
        bfd_set_error (bfd_error_file_too_big);
13721
0
        result = false;
13722
0
        continue;
13723
0
      }
13724
13725
71
    internal_relocs = bfd_alloc (abfd, amt);
13726
71
    if (internal_relocs == NULL)
13727
0
      {
13728
0
        free (native_relocs);
13729
0
        result = false;
13730
0
        continue;
13731
0
      }
13732
13733
71
    if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
13734
71
        || bfd_read (native_relocs, hdr->sh_size, abfd) != hdr->sh_size)
13735
0
      {
13736
0
        free (native_relocs);
13737
        /* The internal_relocs will be freed when
13738
     the memory for the bfd is released.  */
13739
0
        result = false;
13740
0
        continue;
13741
0
      }
13742
13743
71
    if (dynamic)
13744
0
      symcount = bfd_get_dynamic_symcount (abfd);
13745
71
    else
13746
71
      symcount = bfd_get_symcount (abfd);
13747
13748
71
    for (i = 0, internal_reloc = internal_relocs,
13749
71
     native_reloc = native_relocs;
13750
448
         i < reloc_count;
13751
377
         i++, internal_reloc++, native_reloc += entsize)
13752
377
      {
13753
377
        bool res;
13754
377
        Elf_Internal_Rela rela;
13755
13756
377
        if (entsize == ebd->s->sizeof_rel)
13757
0
    ebd->s->swap_reloc_in (abfd, native_reloc, & rela);
13758
377
        else /* entsize == ebd->s->sizeof_rela */
13759
377
    ebd->s->swap_reloca_in (abfd, native_reloc, & rela);
13760
13761
        /* The address of an ELF reloc is section relative for an object
13762
     file, and absolute for an executable file or shared library.
13763
     The address of a normal BFD reloc is always section relative,
13764
     and the address of a dynamic reloc is absolute..  */
13765
377
        if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0)
13766
328
    internal_reloc->address = rela.r_offset;
13767
49
        else
13768
49
    internal_reloc->address = rela.r_offset - sec->vma;
13769
13770
377
        if (r_sym (rela.r_info) == STN_UNDEF)
13771
106
    {
13772
      /* FIXME: This and the error case below mean that we
13773
         have a symbol on relocs that is not elf_symbol_type.  */
13774
106
      internal_reloc->sym_ptr_ptr = &bfd_abs_section_ptr->symbol;
13775
106
    }
13776
271
        else if (r_sym (rela.r_info) > symcount)
13777
0
    {
13778
0
      _bfd_error_handler
13779
        /* xgettext:c-format */
13780
0
        (_("%pB(%pA): relocation %zu has invalid symbol index %lu"),
13781
0
         abfd, sec, i, (long) r_sym (rela.r_info));
13782
0
      bfd_set_error (bfd_error_bad_value);
13783
0
      internal_reloc->sym_ptr_ptr = &bfd_abs_section_ptr->symbol;
13784
0
      result = false;
13785
0
    }
13786
271
        else
13787
271
    {
13788
271
      asymbol **ps;
13789
13790
271
      ps = symbols + r_sym (rela.r_info) - 1;
13791
271
      internal_reloc->sym_ptr_ptr = ps;
13792
      /* Make sure that this symbol is not removed by strip.  */
13793
271
      (*ps)->flags |= BSF_KEEP;
13794
271
    }
13795
13796
377
        internal_reloc->addend = rela.r_addend;
13797
13798
377
        res = ebd->elf_info_to_howto (abfd, internal_reloc, & rela);
13799
377
        if (! res || internal_reloc->howto == NULL)
13800
22
    {
13801
#if DEBUG_SECONDARY_RELOCS
13802
      fprintf (stderr,
13803
         "there is no howto associated with reloc %lx\n",
13804
         rela.r_info);
13805
#endif
13806
22
      result = false;
13807
22
    }
13808
377
      }
13809
13810
71
    free (native_relocs);
13811
    /* Store the internal relocs.  */
13812
71
    relsec->sec_info = internal_relocs;
13813
71
  }
13814
1.74k
    }
13815
13816
89
  return result;
13817
89
}
13818
13819
/* Set the ELF section header fields of an output secondary reloc section.  */
13820
13821
bool
13822
_bfd_elf_copy_special_section_fields (const bfd *ibfd ATTRIBUTE_UNUSED,
13823
              bfd *obfd ATTRIBUTE_UNUSED,
13824
              const Elf_Internal_Shdr *isection,
13825
              Elf_Internal_Shdr *osection)
13826
105
{
13827
105
  asection * isec;
13828
105
  asection * osec;
13829
105
  struct bfd_elf_section_data * esd;
13830
13831
105
  if (isection == NULL)
13832
0
    return false;
13833
13834
105
  if (isection->sh_type != SHT_SECONDARY_RELOC)
13835
104
    return true;
13836
13837
1
  isec = isection->bfd_section;
13838
1
  if (isec == NULL)
13839
0
    return false;
13840
13841
1
  osec = osection->bfd_section;
13842
1
  if (osec == NULL)
13843
0
    return false;
13844
13845
1
  BFD_ASSERT (osec->sec_info == NULL);
13846
1
  osec->sec_info = isec->sec_info;
13847
1
  osection->sh_type = SHT_RELA;
13848
1
  osection->sh_link = elf_onesymtab (obfd);
13849
1
  if (osection->sh_link == 0)
13850
0
    {
13851
      /* There is no symbol table - we are hosed...  */
13852
0
      _bfd_error_handler
13853
  /* xgettext:c-format */
13854
0
  (_("%pB(%pA): link section cannot be set"
13855
0
     " because the output file does not have a symbol table"),
13856
0
  obfd, osec);
13857
0
      bfd_set_error (bfd_error_bad_value);
13858
0
      return false;
13859
0
    }
13860
13861
  /* Find the output section that corresponds to the isection's
13862
     sh_info link.  */
13863
1
  if (isection->sh_info == 0
13864
1
      || isection->sh_info >= elf_numsections (ibfd))
13865
0
    {
13866
0
      _bfd_error_handler
13867
  /* xgettext:c-format */
13868
0
  (_("%pB(%pA): info section index is invalid"),
13869
0
  obfd, osec);
13870
0
      bfd_set_error (bfd_error_bad_value);
13871
0
      return false;
13872
0
    }
13873
13874
1
  isection = elf_elfsections (ibfd)[isection->sh_info];
13875
13876
1
  if (isection == NULL
13877
1
      || isection->bfd_section == NULL
13878
1
      || isection->bfd_section->output_section == NULL)
13879
0
    {
13880
0
      _bfd_error_handler
13881
  /* xgettext:c-format */
13882
0
  (_("%pB(%pA): info section index cannot be set"
13883
0
     " because the section is not in the output"),
13884
0
  obfd, osec);
13885
0
      bfd_set_error (bfd_error_bad_value);
13886
0
      return false;
13887
0
    }
13888
13889
1
  esd = elf_section_data (isection->bfd_section->output_section);
13890
1
  BFD_ASSERT (esd != NULL);
13891
1
  osection->sh_info = esd->this_idx;
13892
1
  esd->has_secondary_relocs = true;
13893
#if DEBUG_SECONDARY_RELOCS
13894
  fprintf (stderr, "update header of %s, sh_link = %u, sh_info = %u\n",
13895
     osec->name, osection->sh_link, osection->sh_info);
13896
  fprintf (stderr, "mark section %s as having secondary relocs\n",
13897
     bfd_section_name (isection->bfd_section->output_section));
13898
#endif
13899
13900
1
  return true;
13901
1
}
13902
13903
/* Write out a secondary reloc section.
13904
13905
   FIXME: Currently this function can result in a serious performance penalty
13906
   for files with secondary relocs and lots of sections.  The proper way to
13907
   fix this is for _bfd_elf_copy_special_section_fields() to chain secondary
13908
   relocs together and then to have this function just walk that chain.  */
13909
13910
bool
13911
_bfd_elf_write_secondary_reloc_section (bfd *abfd, asection *sec)
13912
1
{
13913
1
  elf_backend_data *ebd = get_elf_backend_data (abfd);
13914
1
  bfd_vma addr_offset;
13915
1
  asection * relsec;
13916
1
  bfd_vma (*r_info) (bfd_vma, bfd_vma);
13917
1
  bool result = true;
13918
13919
1
  if (sec == NULL)
13920
0
    return false;
13921
13922
1
#if BFD_DEFAULT_TARGET_SIZE > 32
13923
1
  if (bfd_arch_bits_per_address (abfd) != 32)
13924
1
    r_info = elf64_r_info;
13925
0
  else
13926
0
#endif
13927
0
    r_info = elf32_r_info;
13928
13929
  /* The address of an ELF reloc is section relative for an object
13930
     file, and absolute for an executable file or shared library.
13931
     The address of a BFD reloc is always section relative.  */
13932
1
  addr_offset = 0;
13933
1
  if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
13934
0
    addr_offset = sec->vma;
13935
13936
  /* Discover if there are any secondary reloc sections
13937
     associated with SEC.  */
13938
15
  for (relsec = abfd->sections; relsec != NULL; relsec = relsec->next)
13939
14
    {
13940
14
      const struct bfd_elf_section_data * const esd = elf_section_data (relsec);
13941
14
      Elf_Internal_Shdr * const hdr = (Elf_Internal_Shdr *) & esd->this_hdr;
13942
13943
14
      if (hdr->sh_type == SHT_RELA
13944
2
    && hdr->sh_info == (unsigned) elf_section_data (sec)->this_idx)
13945
1
  {
13946
1
    asymbol *    last_sym;
13947
1
    int          last_sym_idx;
13948
1
    size_t       reloc_count;
13949
1
    size_t       idx;
13950
1
    bfd_size_type entsize;
13951
1
    arelent *    src_irel;
13952
1
    bfd_byte *   dst_rela;
13953
13954
1
    if (hdr->contents != NULL)
13955
0
      {
13956
0
        _bfd_error_handler
13957
    /* xgettext:c-format */
13958
0
    (_("%pB(%pA): error: secondary reloc section processed twice"),
13959
0
     abfd, relsec);
13960
0
        bfd_set_error (bfd_error_bad_value);
13961
0
        result = false;
13962
0
        continue;
13963
0
      }
13964
13965
1
    entsize = hdr->sh_entsize;
13966
1
    if (entsize == 0)
13967
0
      {
13968
0
        _bfd_error_handler
13969
    /* xgettext:c-format */
13970
0
    (_("%pB(%pA): error: secondary reloc section"
13971
0
       " has zero sized entries"),
13972
0
     abfd, relsec);
13973
0
        bfd_set_error (bfd_error_bad_value);
13974
0
        result = false;
13975
0
        continue;
13976
0
      }
13977
1
    else if (entsize != ebd->s->sizeof_rel
13978
1
       && entsize != ebd->s->sizeof_rela)
13979
0
      {
13980
0
        _bfd_error_handler
13981
    /* xgettext:c-format */
13982
0
    (_("%pB(%pA): error: secondary reloc section"
13983
0
       " has non-standard sized entries"),
13984
0
     abfd, relsec);
13985
0
        bfd_set_error (bfd_error_bad_value);
13986
0
        result = false;
13987
0
        continue;
13988
0
      }
13989
13990
1
    reloc_count = hdr->sh_size / entsize;
13991
1
    hdr->sh_size = entsize * reloc_count;
13992
1
    if (reloc_count == 0)
13993
0
      {
13994
0
        _bfd_error_handler
13995
    /* xgettext:c-format */
13996
0
    (_("%pB(%pA): error: secondary reloc section is empty!"),
13997
0
     abfd, relsec);
13998
0
        bfd_set_error (bfd_error_bad_value);
13999
0
        result = false;
14000
0
        continue;
14001
0
      }
14002
14003
1
    hdr->contents = bfd_alloc (abfd, hdr->sh_size);
14004
1
    if (hdr->contents == NULL)
14005
0
      continue;
14006
1
    relsec->alloced = 1;
14007
14008
#if DEBUG_SECONDARY_RELOCS
14009
    fprintf (stderr, "write %u secondary relocs for %s from %s\n",
14010
       reloc_count, sec->name, relsec->name);
14011
#endif
14012
1
    last_sym = NULL;
14013
1
    last_sym_idx = 0;
14014
1
    dst_rela = hdr->contents;
14015
1
    src_irel = relsec->sec_info;
14016
1
    if (src_irel == NULL)
14017
0
      {
14018
0
        _bfd_error_handler
14019
    /* xgettext:c-format */
14020
0
    (_("%pB(%pA): error: internal relocs missing"
14021
0
       " for secondary reloc section"),
14022
0
     abfd, relsec);
14023
0
        bfd_set_error (bfd_error_bad_value);
14024
0
        result = false;
14025
0
        continue;
14026
0
      }
14027
14028
2
    for (idx = 0; idx < reloc_count; idx++, dst_rela += entsize)
14029
1
      {
14030
1
        Elf_Internal_Rela src_rela;
14031
1
        arelent *ptr;
14032
1
        asymbol *sym;
14033
1
        int n;
14034
14035
1
        ptr = src_irel + idx;
14036
1
        if (ptr == NULL)
14037
0
    {
14038
0
      _bfd_error_handler
14039
        /* xgettext:c-format */
14040
0
        (_("%pB(%pA): error: reloc table entry %zu is empty"),
14041
0
         abfd, relsec, idx);
14042
0
      bfd_set_error (bfd_error_bad_value);
14043
0
      result = false;
14044
0
      break;
14045
0
    }
14046
14047
1
        if (ptr->sym_ptr_ptr == NULL)
14048
0
    {
14049
      /* FIXME: Is this an error ? */
14050
0
      n = 0;
14051
0
    }
14052
1
        else
14053
1
    {
14054
1
      sym = *ptr->sym_ptr_ptr;
14055
14056
1
      if (sym == last_sym)
14057
0
        n = last_sym_idx;
14058
1
      else
14059
1
        {
14060
1
          n = _bfd_elf_symbol_from_bfd_symbol (abfd, & sym);
14061
1
          if (n < 0)
14062
0
      {
14063
0
        _bfd_error_handler
14064
          /* xgettext:c-format */
14065
0
          (_("%pB(%pA): error: secondary reloc %zu"
14066
0
             " references a missing symbol"),
14067
0
           abfd, relsec, idx);
14068
0
        bfd_set_error (bfd_error_bad_value);
14069
0
        result = false;
14070
0
        n = 0;
14071
0
      }
14072
14073
1
          last_sym = sym;
14074
1
          last_sym_idx = n;
14075
1
        }
14076
14077
1
      if (sym->the_bfd != NULL
14078
1
          && sym->the_bfd->xvec != abfd->xvec
14079
0
          && ! _bfd_elf_validate_reloc (abfd, ptr))
14080
0
        {
14081
0
          _bfd_error_handler
14082
      /* xgettext:c-format */
14083
0
      (_("%pB(%pA): error: secondary reloc %zu"
14084
0
         " references a deleted symbol"),
14085
0
       abfd, relsec, idx);
14086
0
          bfd_set_error (bfd_error_bad_value);
14087
0
          result = false;
14088
0
          n = 0;
14089
0
        }
14090
1
    }
14091
14092
1
        src_rela.r_offset = ptr->address + addr_offset;
14093
1
        if (ptr->howto == NULL)
14094
0
    {
14095
0
      _bfd_error_handler
14096
        /* xgettext:c-format */
14097
0
        (_("%pB(%pA): error: secondary reloc %zu"
14098
0
           " is of an unknown type"),
14099
0
         abfd, relsec, idx);
14100
0
      bfd_set_error (bfd_error_bad_value);
14101
0
      result = false;
14102
0
      src_rela.r_info = r_info (0, 0);
14103
0
    }
14104
1
        else
14105
1
    src_rela.r_info = r_info (n, ptr->howto->type);
14106
1
        src_rela.r_addend = ptr->addend;
14107
14108
1
        if (entsize == ebd->s->sizeof_rel)
14109
0
    ebd->s->swap_reloc_out (abfd, &src_rela, dst_rela);
14110
1
        else /* entsize == ebd->s->sizeof_rela */
14111
1
    ebd->s->swap_reloca_out (abfd, &src_rela, dst_rela);
14112
1
      }
14113
1
  }
14114
14
    }
14115
14116
1
  return result;
14117
1
}
14118
14119
/* Mmap in section contents.  */
14120
14121
static bool
14122
elf_mmap_section_contents (bfd *abfd, sec_ptr sec, bfd_byte **buf)
14123
8.34k
{
14124
8.34k
#ifdef USE_MMAP
14125
8.34k
  elf_backend_data *bed = get_elf_backend_data (abfd);
14126
8.34k
  if (bed->use_mmap
14127
2.99k
      && sec->compress_status == COMPRESS_SECTION_NONE
14128
2.99k
      && (sec->flags & SEC_LINKER_CREATED) == 0)
14129
2.99k
    {
14130
      /* Use mmap only if section size >= the minimum mmap section
14131
   size.  */
14132
2.99k
      size_t readsz = bfd_get_section_limit_octets (abfd, sec);
14133
2.99k
      size_t allocsz = bfd_get_section_alloc_size (abfd, sec);
14134
2.99k
      if (readsz == allocsz && readsz >= _bfd_minimum_mmap_size)
14135
725
  {
14136
725
    if (sec->contents != NULL)
14137
0
      {
14138
0
        if (!sec->mmapped_p)
14139
0
    abort ();
14140
0
        *buf = sec->contents;
14141
0
        return true;
14142
0
      }
14143
725
    if (sec->mmapped_p)
14144
0
      abort ();
14145
725
    sec->mmapped_p = 1;
14146
14147
    /* We can't use the final link preallocated buffer for mmap.  */
14148
725
    *buf = NULL;
14149
725
  }
14150
2.99k
    }
14151
8.34k
#endif
14152
  /* FIXME: We should not get here if sec->alloced is set.  */
14153
8.34k
  bool ret = bfd_get_full_section_contents (abfd, sec, buf);
14154
8.34k
  if (ret && sec->mmapped_p)
14155
0
    *buf = sec->contents;
14156
8.34k
  return ret;
14157
8.34k
}
14158
14159
/* Mmap in section contents.  */
14160
14161
bool
14162
_bfd_elf_mmap_section_contents (bfd *abfd, sec_ptr sec, bfd_byte **buf)
14163
8.34k
{
14164
8.34k
  *buf = NULL;
14165
8.34k
  return elf_mmap_section_contents (abfd, sec, buf);
14166
8.34k
}
14167
14168
/* Mmap in the full section contents for the final link.  */
14169
14170
bool
14171
_bfd_elf_link_mmap_section_contents (bfd *abfd, sec_ptr sec,
14172
             bfd_byte **buf)
14173
0
{
14174
0
  return elf_mmap_section_contents (abfd, sec, buf);
14175
0
}
14176
14177
/* Munmap section contents.  */
14178
14179
void
14180
_bfd_elf_munmap_section_contents (asection *sec, void *contents)
14181
497k
{
14182
  /* NB: Since _bfd_elf_munmap_section_contents is called like free,
14183
     CONTENTS may be NULL.  */
14184
497k
  if (contents == NULL)
14185
489k
    return;
14186
14187
7.21k
  if (sec->alloced
14188
      /* What a tangled web we weave with section contents.
14189
   FIXME: We shouldn't need to test anything but sec->alloced
14190
   here, but there are cases where a buffer is allocated for a
14191
   section but then another buffer is malloc'd anyway.  eg.
14192
   trace through ld-elf/eh4 testcase on x86_64.  */
14193
438
      && (sec->contents == contents
14194
0
    || elf_section_data (sec)->this_hdr.contents == contents))
14195
438
    return;
14196
14197
  /* Don't leave pointers to data we are about to munmap or free.  */
14198
6.77k
  if (sec->contents == contents)
14199
0
    sec->contents = NULL;
14200
6.77k
  if (elf_section_data (sec)->this_hdr.contents == contents)
14201
0
    elf_section_data (sec)->this_hdr.contents = NULL;
14202
14203
6.77k
#ifdef USE_MMAP
14204
6.77k
  if (sec->mmapped_p)
14205
0
    {
14206
      /* When _bfd_elf_mmap_section_contents returns CONTENTS as
14207
   malloced, CONTENTS_ADDR is set to NULL.  */
14208
0
      if (elf_section_data (sec)->contents_addr != NULL)
14209
0
  {
14210
    /* NB: CONTENTS_ADDR and CONTENTS_SIZE must be valid.  */
14211
0
    if (munmap (elf_section_data (sec)->contents_addr,
14212
0
          elf_section_data (sec)->contents_size) != 0)
14213
0
      abort ();
14214
0
    sec->mmapped_p = 0;
14215
0
    elf_section_data (sec)->contents_addr = NULL;
14216
0
    elf_section_data (sec)->contents_size = 0;
14217
0
    return;
14218
0
  }
14219
0
    }
14220
6.77k
#endif
14221
14222
6.77k
  free (contents);
14223
6.77k
}
14224
14225
/* Munmap the full section contents for the final link.  */
14226
14227
void
14228
_bfd_elf_link_munmap_section_contents (asection *sec ATTRIBUTE_UNUSED)
14229
0
{
14230
0
#ifdef USE_MMAP
14231
0
  if (sec->mmapped_p && elf_section_data (sec)->contents_addr != NULL)
14232
0
    {
14233
      /* When _bfd_elf_link_mmap_section_contents returns CONTENTS as
14234
   malloced, CONTENTS_ADDR is set to NULL.  */
14235
      /* NB: CONTENTS_ADDR and CONTENTS_SIZE must be valid.  */
14236
0
      if (munmap (elf_section_data (sec)->contents_addr,
14237
0
      elf_section_data (sec)->contents_size) != 0)
14238
0
  abort ();
14239
0
      sec->mmapped_p = 0;
14240
0
      sec->contents = NULL;
14241
0
      elf_section_data (sec)->this_hdr.contents = NULL;
14242
0
      elf_section_data (sec)->contents_addr = NULL;
14243
0
      elf_section_data (sec)->contents_size = 0;
14244
0
    }
14245
0
#endif
14246
0
}
14247
14248
/* Validate that deleting COUNT bytes starting at OFFS up to the upper
14249
   bound LIMIT is within the section bounds of SEC (OFFS <= LIMIT,
14250
   COUNT <= LIMIT - OFFS, and LIMIT <= SEC->SIZE).  OFFS is derived
14251
   from an unvalidated relocation offset; without this check the
14252
   subtraction (LIMIT - OFFS - COUNT) wraps around, corrupting the heap
14253
   buffer.  */
14254
14255
bool
14256
_bfd_elf_relax_delete_range_ok (const bfd *abfd,
14257
        const asection *sec,
14258
        bfd_vma offs,
14259
        bfd_vma count,
14260
        bfd_vma limit)
14261
0
{
14262
0
  if (offs <= limit
14263
0
      && count <= limit - offs
14264
0
      && limit <= sec->size)
14265
0
    return true;
14266
14267
0
  _bfd_error_handler
14268
    /* xgettext:c-format */
14269
0
    (_("%pB: relax: cannot delete %" PRIu64 " bytes at %#" PRIx64
14270
0
       " (limit %#" PRIx64 ") in section `%pA' of size %#" PRIx64),
14271
0
     abfd, (uint64_t) count, (uint64_t) offs, (uint64_t) limit, sec,
14272
0
     (uint64_t) sec->size);
14273
0
  bfd_set_error (bfd_error_bad_value);
14274
  return false;
14275
0
}
14276
14277
#ifndef OBJ_MAYBE_ELF_ATTRIBUTES
14278
14279
bfd *_bfd_elf_link_setup_object_attributes
14280
  (struct bfd_link_info *info ATTRIBUTE_UNUSED)
14281
{ return NULL; }
14282
14283
#endif /* !OBJ_MAYBE_ELF_ATTRIBUTES */